blob: f13a3f7e2e8fc251c8d598a4033f8439972ae031 [file] [log] [blame]
Jin Park4b0d7112011-06-29 23:06:51 +09001/*
2 * linux/drivers/video/backlight/aat2870_bl.c
3 *
4 * Copyright (c) 2011, NVIDIA Corporation.
5 * Author: Jin Park <jinyoungp@nvidia.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/platform_device.h>
26#include <linux/mutex.h>
27#include <linux/delay.h>
28#include <linux/fb.h>
29#include <linux/backlight.h>
30#include <linux/mfd/aat2870.h>
31
32struct aat2870_bl_driver_data {
33 struct platform_device *pdev;
34 struct backlight_device *bd;
35
36 int channels;
37 int max_current;
38 int brightness; /* current brightness */
39};
40
41static inline int aat2870_brightness(struct aat2870_bl_driver_data *aat2870_bl,
42 int brightness)
43{
44 struct backlight_device *bd = aat2870_bl->bd;
45 int val;
46
47 val = brightness * aat2870_bl->max_current;
48 val /= bd->props.max_brightness;
49
50 return val;
51}
52
53static inline int aat2870_bl_enable(struct aat2870_bl_driver_data *aat2870_bl)
54{
55 struct aat2870_data *aat2870
56 = dev_get_drvdata(aat2870_bl->pdev->dev.parent);
57
58 return aat2870->write(aat2870, AAT2870_BL_CH_EN,
59 (u8)aat2870_bl->channels);
60}
61
62static inline int aat2870_bl_disable(struct aat2870_bl_driver_data *aat2870_bl)
63{
64 struct aat2870_data *aat2870
65 = dev_get_drvdata(aat2870_bl->pdev->dev.parent);
66
67 return aat2870->write(aat2870, AAT2870_BL_CH_EN, 0x0);
68}
69
70static int aat2870_bl_get_brightness(struct backlight_device *bd)
71{
72 return bd->props.brightness;
73}
74
75static int aat2870_bl_update_status(struct backlight_device *bd)
76{
77 struct aat2870_bl_driver_data *aat2870_bl = dev_get_drvdata(&bd->dev);
78 struct aat2870_data *aat2870 =
79 dev_get_drvdata(aat2870_bl->pdev->dev.parent);
80 int brightness = bd->props.brightness;
81 int ret;
82
83 if ((brightness < 0) || (bd->props.max_brightness < brightness)) {
84 dev_err(&bd->dev, "invalid brightness, %d\n", brightness);
85 return -EINVAL;
86 }
87
88 dev_dbg(&bd->dev, "brightness=%d, power=%d, state=%d\n",
89 bd->props.brightness, bd->props.power, bd->props.state);
90
91 if ((bd->props.power != FB_BLANK_UNBLANK) ||
92 (bd->props.state & BL_CORE_FBBLANK) ||
93 (bd->props.state & BL_CORE_SUSPENDED))
94 brightness = 0;
95
96 ret = aat2870->write(aat2870, AAT2870_BLM,
97 (u8)aat2870_brightness(aat2870_bl, brightness));
98 if (ret < 0)
99 return ret;
100
101 if (brightness == 0) {
102 ret = aat2870_bl_disable(aat2870_bl);
103 if (ret < 0)
104 return ret;
105 } else if (aat2870_bl->brightness == 0) {
106 ret = aat2870_bl_enable(aat2870_bl);
107 if (ret < 0)
108 return ret;
109 }
110
111 aat2870_bl->brightness = brightness;
112
113 return 0;
114}
115
116static int aat2870_bl_check_fb(struct backlight_device *bd, struct fb_info *fi)
117{
118 return 1;
119}
120
121static const struct backlight_ops aat2870_bl_ops = {
122 .options = BL_CORE_SUSPENDRESUME,
123 .get_brightness = aat2870_bl_get_brightness,
124 .update_status = aat2870_bl_update_status,
125 .check_fb = aat2870_bl_check_fb,
126};
127
128static int aat2870_bl_probe(struct platform_device *pdev)
129{
130 struct aat2870_bl_platform_data *pdata = pdev->dev.platform_data;
131 struct aat2870_bl_driver_data *aat2870_bl;
132 struct backlight_device *bd;
133 struct backlight_properties props;
134 int ret = 0;
135
136 if (!pdata) {
137 dev_err(&pdev->dev, "No platform data\n");
138 ret = -ENXIO;
139 goto out;
140 }
141
142 if (pdev->id != AAT2870_ID_BL) {
143 dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
144 ret = -EINVAL;
145 goto out;
146 }
147
148 aat2870_bl = kzalloc(sizeof(struct aat2870_bl_driver_data), GFP_KERNEL);
149 if (!aat2870_bl) {
150 dev_err(&pdev->dev,
151 "Failed to allocate memory for aat2870 backlight\n");
152 ret = -ENOMEM;
153 goto out;
154 }
155
156 memset(&props, 0, sizeof(struct backlight_properties));
157
158 props.type = BACKLIGHT_RAW;
159 bd = backlight_device_register("aat2870-backlight", &pdev->dev,
160 aat2870_bl, &aat2870_bl_ops, &props);
Axel Lin4c4dd902011-08-03 16:21:16 -0700161 if (IS_ERR(bd)) {
Jin Park4b0d7112011-06-29 23:06:51 +0900162 dev_err(&pdev->dev,
163 "Failed allocate memory for backlight device\n");
Axel Lin4c4dd902011-08-03 16:21:16 -0700164 ret = PTR_ERR(bd);
Jin Park4b0d7112011-06-29 23:06:51 +0900165 goto out_kfree;
166 }
167
168 aat2870_bl->pdev = pdev;
169 platform_set_drvdata(pdev, aat2870_bl);
170
171 aat2870_bl->bd = bd;
172
173 if (pdata->channels > 0)
174 aat2870_bl->channels = pdata->channels;
175 else
176 aat2870_bl->channels = AAT2870_BL_CH_ALL;
177
178 if (pdata->max_brightness > 0)
179 aat2870_bl->max_current = pdata->max_current;
180 else
181 aat2870_bl->max_current = AAT2870_CURRENT_27_9;
182
183 if (pdata->max_brightness > 0)
184 bd->props.max_brightness = pdata->max_brightness;
185 else
186 bd->props.max_brightness = 255;
187
188 aat2870_bl->brightness = 0;
189 bd->props.power = FB_BLANK_UNBLANK;
190 bd->props.brightness = bd->props.max_brightness;
191
192 ret = aat2870_bl_update_status(bd);
193 if (ret < 0) {
194 dev_err(&pdev->dev, "Failed to initialize\n");
195 goto out_bl_dev_unregister;
196 }
197
198 return 0;
199
200out_bl_dev_unregister:
201 backlight_device_unregister(bd);
202out_kfree:
203 kfree(aat2870_bl);
204out:
205 return ret;
206}
207
208static int aat2870_bl_remove(struct platform_device *pdev)
209{
210 struct aat2870_bl_driver_data *aat2870_bl = platform_get_drvdata(pdev);
211 struct backlight_device *bd = aat2870_bl->bd;
212
213 bd->props.power = FB_BLANK_POWERDOWN;
214 bd->props.brightness = 0;
215 backlight_update_status(bd);
216
217 backlight_device_unregister(bd);
218 kfree(aat2870_bl);
219
220 return 0;
221}
222
223static struct platform_driver aat2870_bl_driver = {
224 .driver = {
225 .name = "aat2870-backlight",
226 .owner = THIS_MODULE,
227 },
228 .probe = aat2870_bl_probe,
229 .remove = aat2870_bl_remove,
230};
231
232static int __init aat2870_bl_init(void)
233{
234 return platform_driver_register(&aat2870_bl_driver);
235}
236subsys_initcall(aat2870_bl_init);
237
238static void __exit aat2870_bl_exit(void)
239{
240 platform_driver_unregister(&aat2870_bl_driver);
241}
242module_exit(aat2870_bl_exit);
243
244MODULE_DESCRIPTION("AnalogicTech AAT2870 Backlight");
245MODULE_LICENSE("GPL");
246MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");