blob: 546d94df21d53ac436891273828058043acb2c2d [file] [log] [blame]
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +00001/*
2 * Backlight driver for OMAP based boards.
3 *
4 * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
5 *
6 * This package is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This package is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this package; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/platform_device.h>
25#include <linux/fb.h>
26#include <linux/backlight.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Igor Grinbergcc012722012-08-29 02:18:54 +030028#include <linux/platform_data/omap1_bl.h>
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000029
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/hardware.h>
Tony Lindgren70c494c2012-09-19 10:46:56 -070031#include <mach/mux.h>
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000032
33#define OMAPBL_MAX_INTENSITY 0xff
34
35struct omap_backlight {
36 int powermode;
37 int current_intensity;
38
39 struct device *dev;
40 struct omap_backlight_config *pdata;
41};
42
Jingoo Han7a94f652012-12-17 16:00:29 -080043static inline void omapbl_send_intensity(int intensity)
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000044{
45 omap_writeb(intensity, OMAP_PWL_ENABLE);
46}
47
Jingoo Han7a94f652012-12-17 16:00:29 -080048static inline void omapbl_send_enable(int enable)
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000049{
50 omap_writeb(enable, OMAP_PWL_CLK_ENABLE);
51}
52
53static void omapbl_blank(struct omap_backlight *bl, int mode)
54{
55 if (bl->pdata->set_power)
56 bl->pdata->set_power(bl->dev, mode);
57
58 switch (mode) {
59 case FB_BLANK_NORMAL:
60 case FB_BLANK_VSYNC_SUSPEND:
61 case FB_BLANK_HSYNC_SUSPEND:
62 case FB_BLANK_POWERDOWN:
63 omapbl_send_intensity(0);
64 omapbl_send_enable(0);
65 break;
66
67 case FB_BLANK_UNBLANK:
68 omapbl_send_intensity(bl->current_intensity);
69 omapbl_send_enable(1);
70 break;
71 }
72}
73
Jingoo Han07cac9d2013-04-29 16:17:59 -070074#ifdef CONFIG_PM_SLEEP
75static int omapbl_suspend(struct device *dev)
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000076{
Jingoo Han07cac9d2013-04-29 16:17:59 -070077 struct backlight_device *bl_dev = dev_get_drvdata(dev);
78 struct omap_backlight *bl = bl_get_data(bl_dev);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000079
80 omapbl_blank(bl, FB_BLANK_POWERDOWN);
81 return 0;
82}
83
Jingoo Han07cac9d2013-04-29 16:17:59 -070084static int omapbl_resume(struct device *dev)
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000085{
Jingoo Han07cac9d2013-04-29 16:17:59 -070086 struct backlight_device *bl_dev = dev_get_drvdata(dev);
87 struct omap_backlight *bl = bl_get_data(bl_dev);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000088
89 omapbl_blank(bl, bl->powermode);
90 return 0;
91}
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000092#endif
93
94static int omapbl_set_power(struct backlight_device *dev, int state)
95{
Jingoo Han5f42d5b2013-02-21 16:43:50 -080096 struct omap_backlight *bl = bl_get_data(dev);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +000097
98 omapbl_blank(bl, state);
99 bl->powermode = state;
100
101 return 0;
102}
103
104static int omapbl_update_status(struct backlight_device *dev)
105{
Jingoo Han5f42d5b2013-02-21 16:43:50 -0800106 struct omap_backlight *bl = bl_get_data(dev);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000107
108 if (bl->current_intensity != dev->props.brightness) {
109 if (bl->powermode == FB_BLANK_UNBLANK)
110 omapbl_send_intensity(dev->props.brightness);
111 bl->current_intensity = dev->props.brightness;
112 }
113
114 if (dev->props.fb_blank != bl->powermode)
115 omapbl_set_power(dev, dev->props.fb_blank);
116
117 return 0;
118}
119
120static int omapbl_get_intensity(struct backlight_device *dev)
121{
Jingoo Han5f42d5b2013-02-21 16:43:50 -0800122 struct omap_backlight *bl = bl_get_data(dev);
Jingoo Hanb6af73b2014-08-27 10:15:01 +0900123
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000124 return bl->current_intensity;
125}
126
Emese Revfy9905a432009-12-14 00:58:57 +0100127static const struct backlight_ops omapbl_ops = {
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000128 .get_brightness = omapbl_get_intensity,
129 .update_status = omapbl_update_status,
130};
131
132static int omapbl_probe(struct platform_device *pdev)
133{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500134 struct backlight_properties props;
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000135 struct backlight_device *dev;
136 struct omap_backlight *bl;
Jingoo Hanc512794c2013-11-12 15:09:04 -0800137 struct omap_backlight_config *pdata = dev_get_platdata(&pdev->dev);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000138
139 if (!pdata)
140 return -ENXIO;
141
Julia Lawallce969222012-03-23 15:02:00 -0700142 bl = devm_kzalloc(&pdev->dev, sizeof(struct omap_backlight),
143 GFP_KERNEL);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000144 if (unlikely(!bl))
145 return -ENOMEM;
146
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500147 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700148 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500149 props.max_brightness = OMAPBL_MAX_INTENSITY;
Jingoo Hanc76d1022014-01-23 15:54:26 -0800150 dev = devm_backlight_device_register(&pdev->dev, "omap-bl", &pdev->dev,
151 bl, &omapbl_ops, &props);
Julia Lawallce969222012-03-23 15:02:00 -0700152 if (IS_ERR(dev))
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000153 return PTR_ERR(dev);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000154
155 bl->powermode = FB_BLANK_POWERDOWN;
156 bl->current_intensity = 0;
157
158 bl->pdata = pdata;
159 bl->dev = &pdev->dev;
160
161 platform_set_drvdata(pdev, dev);
162
163 omap_cfg_reg(PWL); /* Conflicts with UART3 */
164
165 dev->props.fb_blank = FB_BLANK_UNBLANK;
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000166 dev->props.brightness = pdata->default_intensity;
167 omapbl_update_status(dev);
168
Jingoo Han8e1274a2013-04-29 16:17:32 -0700169 dev_info(&pdev->dev, "OMAP LCD backlight initialised\n");
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000170
171 return 0;
172}
173
Jingoo Han07cac9d2013-04-29 16:17:59 -0700174static SIMPLE_DEV_PM_OPS(omapbl_pm_ops, omapbl_suspend, omapbl_resume);
175
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000176static struct platform_driver omapbl_driver = {
177 .probe = omapbl_probe,
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000178 .driver = {
179 .name = "omap-bl",
Jingoo Han07cac9d2013-04-29 16:17:59 -0700180 .pm = &omapbl_pm_ops,
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000181 },
182};
183
Axel Lin81178e02012-01-10 15:09:11 -0800184module_platform_driver(omapbl_driver);
Andrzej Zaborowskiec072b02008-01-13 23:15:43 +0000185
186MODULE_AUTHOR("Andrzej Zaborowski <balrog@zabor.org>");
187MODULE_DESCRIPTION("OMAP LCD Backlight driver");
188MODULE_LICENSE("GPL");