Commit | Line | Data |
---|---|---|
a7998cec MH |
1 | /* |
2 | * Backlight driver for Analog Devices ADP5520/ADP5501 MFD PMICs | |
3 | * | |
4 | * Copyright 2009 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/init.h> | |
11 | #include <linux/platform_device.h> | |
12 | #include <linux/fb.h> | |
13 | #include <linux/backlight.h> | |
14 | #include <linux/mfd/adp5520.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
355b200b | 16 | #include <linux/module.h> |
a7998cec MH |
17 | |
18 | struct adp5520_bl { | |
19 | struct device *master; | |
c24b6b6a | 20 | struct adp5520_backlight_platform_data *pdata; |
a7998cec MH |
21 | struct mutex lock; |
22 | unsigned long cached_daylight_max; | |
23 | int id; | |
24 | int current_brightness; | |
25 | }; | |
26 | ||
27 | static int adp5520_bl_set(struct backlight_device *bl, int brightness) | |
28 | { | |
29 | struct adp5520_bl *data = bl_get_data(bl); | |
30 | struct device *master = data->master; | |
31 | int ret = 0; | |
32 | ||
33 | if (data->pdata->en_ambl_sens) { | |
34 | if ((brightness > 0) && (brightness < ADP5020_MAX_BRIGHTNESS)) { | |
35 | /* Disable Ambient Light auto adjust */ | |
c24b6b6a MH |
36 | ret |= adp5520_clr_bits(master, ADP5520_BL_CONTROL, |
37 | ADP5520_BL_AUTO_ADJ); | |
38 | ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, | |
39 | brightness); | |
a7998cec MH |
40 | } else { |
41 | /* | |
42 | * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust | |
43 | * restore daylight l3 sysfs brightness | |
44 | */ | |
c24b6b6a | 45 | ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, |
a7998cec | 46 | data->cached_daylight_max); |
c24b6b6a MH |
47 | ret |= adp5520_set_bits(master, ADP5520_BL_CONTROL, |
48 | ADP5520_BL_AUTO_ADJ); | |
a7998cec MH |
49 | } |
50 | } else { | |
c24b6b6a | 51 | ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, brightness); |
a7998cec MH |
52 | } |
53 | ||
54 | if (data->current_brightness && brightness == 0) | |
55 | ret |= adp5520_set_bits(master, | |
c24b6b6a | 56 | ADP5520_MODE_STATUS, ADP5520_DIM_EN); |
a7998cec MH |
57 | else if (data->current_brightness == 0 && brightness) |
58 | ret |= adp5520_clr_bits(master, | |
c24b6b6a | 59 | ADP5520_MODE_STATUS, ADP5520_DIM_EN); |
a7998cec MH |
60 | |
61 | if (!ret) | |
62 | data->current_brightness = brightness; | |
63 | ||
64 | return ret; | |
65 | } | |
66 | ||
67 | static int adp5520_bl_update_status(struct backlight_device *bl) | |
68 | { | |
69 | int brightness = bl->props.brightness; | |
70 | if (bl->props.power != FB_BLANK_UNBLANK) | |
71 | brightness = 0; | |
72 | ||
73 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) | |
74 | brightness = 0; | |
75 | ||
76 | return adp5520_bl_set(bl, brightness); | |
77 | } | |
78 | ||
79 | static int adp5520_bl_get_brightness(struct backlight_device *bl) | |
80 | { | |
81 | struct adp5520_bl *data = bl_get_data(bl); | |
82 | int error; | |
83 | uint8_t reg_val; | |
84 | ||
c24b6b6a | 85 | error = adp5520_read(data->master, ADP5520_BL_VALUE, ®_val); |
a7998cec MH |
86 | |
87 | return error ? data->current_brightness : reg_val; | |
88 | } | |
89 | ||
9905a43b | 90 | static const struct backlight_ops adp5520_bl_ops = { |
a7998cec MH |
91 | .update_status = adp5520_bl_update_status, |
92 | .get_brightness = adp5520_bl_get_brightness, | |
93 | }; | |
94 | ||
95 | static int adp5520_bl_setup(struct backlight_device *bl) | |
96 | { | |
97 | struct adp5520_bl *data = bl_get_data(bl); | |
98 | struct device *master = data->master; | |
c24b6b6a | 99 | struct adp5520_backlight_platform_data *pdata = data->pdata; |
a7998cec MH |
100 | int ret = 0; |
101 | ||
c24b6b6a MH |
102 | ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, |
103 | pdata->l1_daylight_max); | |
104 | ret |= adp5520_write(master, ADP5520_DAYLIGHT_DIM, | |
105 | pdata->l1_daylight_dim); | |
a7998cec MH |
106 | |
107 | if (pdata->en_ambl_sens) { | |
108 | data->cached_daylight_max = pdata->l1_daylight_max; | |
c24b6b6a MH |
109 | ret |= adp5520_write(master, ADP5520_OFFICE_MAX, |
110 | pdata->l2_office_max); | |
111 | ret |= adp5520_write(master, ADP5520_OFFICE_DIM, | |
112 | pdata->l2_office_dim); | |
113 | ret |= adp5520_write(master, ADP5520_DARK_MAX, | |
114 | pdata->l3_dark_max); | |
115 | ret |= adp5520_write(master, ADP5520_DARK_DIM, | |
116 | pdata->l3_dark_dim); | |
117 | ret |= adp5520_write(master, ADP5520_L2_TRIP, | |
118 | pdata->l2_trip); | |
119 | ret |= adp5520_write(master, ADP5520_L2_HYS, | |
120 | pdata->l2_hyst); | |
121 | ret |= adp5520_write(master, ADP5520_L3_TRIP, | |
122 | pdata->l3_trip); | |
123 | ret |= adp5520_write(master, ADP5520_L3_HYS, | |
124 | pdata->l3_hyst); | |
125 | ret |= adp5520_write(master, ADP5520_ALS_CMPR_CFG, | |
126 | ALS_CMPR_CFG_VAL(pdata->abml_filt, | |
127 | ADP5520_L3_EN)); | |
a7998cec MH |
128 | } |
129 | ||
c24b6b6a MH |
130 | ret |= adp5520_write(master, ADP5520_BL_CONTROL, |
131 | BL_CTRL_VAL(pdata->fade_led_law, | |
132 | pdata->en_ambl_sens)); | |
a7998cec | 133 | |
c24b6b6a | 134 | ret |= adp5520_write(master, ADP5520_BL_FADE, FADE_VAL(pdata->fade_in, |
a7998cec MH |
135 | pdata->fade_out)); |
136 | ||
c24b6b6a MH |
137 | ret |= adp5520_set_bits(master, ADP5520_MODE_STATUS, |
138 | ADP5520_BL_EN | ADP5520_DIM_EN); | |
a7998cec MH |
139 | |
140 | return ret; | |
141 | } | |
142 | ||
143 | static ssize_t adp5520_show(struct device *dev, char *buf, int reg) | |
144 | { | |
145 | struct adp5520_bl *data = dev_get_drvdata(dev); | |
359177e0 | 146 | int ret; |
a7998cec MH |
147 | uint8_t reg_val; |
148 | ||
149 | mutex_lock(&data->lock); | |
359177e0 | 150 | ret = adp5520_read(data->master, reg, ®_val); |
a7998cec MH |
151 | mutex_unlock(&data->lock); |
152 | ||
359177e0 DN |
153 | if (ret < 0) |
154 | return ret; | |
155 | ||
a7998cec MH |
156 | return sprintf(buf, "%u\n", reg_val); |
157 | } | |
158 | ||
159 | static ssize_t adp5520_store(struct device *dev, const char *buf, | |
160 | size_t count, int reg) | |
161 | { | |
162 | struct adp5520_bl *data = dev_get_drvdata(dev); | |
163 | unsigned long val; | |
164 | int ret; | |
165 | ||
71d7225c | 166 | ret = kstrtoul(buf, 10, &val); |
a7998cec MH |
167 | if (ret) |
168 | return ret; | |
169 | ||
170 | mutex_lock(&data->lock); | |
171 | adp5520_write(data->master, reg, val); | |
172 | mutex_unlock(&data->lock); | |
173 | ||
174 | return count; | |
175 | } | |
176 | ||
177 | static ssize_t adp5520_bl_dark_max_show(struct device *dev, | |
c24b6b6a | 178 | struct device_attribute *attr, char *buf) |
a7998cec | 179 | { |
c24b6b6a | 180 | return adp5520_show(dev, buf, ADP5520_DARK_MAX); |
a7998cec MH |
181 | } |
182 | ||
183 | static ssize_t adp5520_bl_dark_max_store(struct device *dev, | |
c24b6b6a MH |
184 | struct device_attribute *attr, |
185 | const char *buf, size_t count) | |
a7998cec | 186 | { |
c24b6b6a | 187 | return adp5520_store(dev, buf, count, ADP5520_DARK_MAX); |
a7998cec MH |
188 | } |
189 | static DEVICE_ATTR(dark_max, 0664, adp5520_bl_dark_max_show, | |
190 | adp5520_bl_dark_max_store); | |
191 | ||
192 | static ssize_t adp5520_bl_office_max_show(struct device *dev, | |
c24b6b6a | 193 | struct device_attribute *attr, char *buf) |
a7998cec | 194 | { |
c24b6b6a | 195 | return adp5520_show(dev, buf, ADP5520_OFFICE_MAX); |
a7998cec MH |
196 | } |
197 | ||
198 | static ssize_t adp5520_bl_office_max_store(struct device *dev, | |
c24b6b6a MH |
199 | struct device_attribute *attr, |
200 | const char *buf, size_t count) | |
a7998cec | 201 | { |
c24b6b6a | 202 | return adp5520_store(dev, buf, count, ADP5520_OFFICE_MAX); |
a7998cec MH |
203 | } |
204 | static DEVICE_ATTR(office_max, 0664, adp5520_bl_office_max_show, | |
205 | adp5520_bl_office_max_store); | |
206 | ||
207 | static ssize_t adp5520_bl_daylight_max_show(struct device *dev, | |
208 | struct device_attribute *attr, char *buf) | |
209 | { | |
c24b6b6a | 210 | return adp5520_show(dev, buf, ADP5520_DAYLIGHT_MAX); |
a7998cec MH |
211 | } |
212 | ||
213 | static ssize_t adp5520_bl_daylight_max_store(struct device *dev, | |
c24b6b6a MH |
214 | struct device_attribute *attr, |
215 | const char *buf, size_t count) | |
a7998cec MH |
216 | { |
217 | struct adp5520_bl *data = dev_get_drvdata(dev); | |
877947bc LY |
218 | int ret; |
219 | ||
71d7225c | 220 | ret = kstrtoul(buf, 10, &data->cached_daylight_max); |
877947bc LY |
221 | if (ret < 0) |
222 | return ret; | |
a7998cec | 223 | |
c24b6b6a | 224 | return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_MAX); |
a7998cec MH |
225 | } |
226 | static DEVICE_ATTR(daylight_max, 0664, adp5520_bl_daylight_max_show, | |
227 | adp5520_bl_daylight_max_store); | |
228 | ||
229 | static ssize_t adp5520_bl_dark_dim_show(struct device *dev, | |
230 | struct device_attribute *attr, char *buf) | |
231 | { | |
c24b6b6a | 232 | return adp5520_show(dev, buf, ADP5520_DARK_DIM); |
a7998cec MH |
233 | } |
234 | ||
235 | static ssize_t adp5520_bl_dark_dim_store(struct device *dev, | |
c24b6b6a MH |
236 | struct device_attribute *attr, |
237 | const char *buf, size_t count) | |
a7998cec | 238 | { |
c24b6b6a | 239 | return adp5520_store(dev, buf, count, ADP5520_DARK_DIM); |
a7998cec MH |
240 | } |
241 | static DEVICE_ATTR(dark_dim, 0664, adp5520_bl_dark_dim_show, | |
242 | adp5520_bl_dark_dim_store); | |
243 | ||
244 | static ssize_t adp5520_bl_office_dim_show(struct device *dev, | |
245 | struct device_attribute *attr, char *buf) | |
246 | { | |
c24b6b6a | 247 | return adp5520_show(dev, buf, ADP5520_OFFICE_DIM); |
a7998cec MH |
248 | } |
249 | ||
250 | static ssize_t adp5520_bl_office_dim_store(struct device *dev, | |
c24b6b6a MH |
251 | struct device_attribute *attr, |
252 | const char *buf, size_t count) | |
a7998cec | 253 | { |
c24b6b6a | 254 | return adp5520_store(dev, buf, count, ADP5520_OFFICE_DIM); |
a7998cec MH |
255 | } |
256 | static DEVICE_ATTR(office_dim, 0664, adp5520_bl_office_dim_show, | |
257 | adp5520_bl_office_dim_store); | |
258 | ||
259 | static ssize_t adp5520_bl_daylight_dim_show(struct device *dev, | |
c24b6b6a | 260 | struct device_attribute *attr, char *buf) |
a7998cec | 261 | { |
c24b6b6a | 262 | return adp5520_show(dev, buf, ADP5520_DAYLIGHT_DIM); |
a7998cec MH |
263 | } |
264 | ||
265 | static ssize_t adp5520_bl_daylight_dim_store(struct device *dev, | |
c24b6b6a MH |
266 | struct device_attribute *attr, |
267 | const char *buf, size_t count) | |
a7998cec | 268 | { |
c24b6b6a | 269 | return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_DIM); |
a7998cec MH |
270 | } |
271 | static DEVICE_ATTR(daylight_dim, 0664, adp5520_bl_daylight_dim_show, | |
272 | adp5520_bl_daylight_dim_store); | |
273 | ||
274 | static struct attribute *adp5520_bl_attributes[] = { | |
275 | &dev_attr_dark_max.attr, | |
276 | &dev_attr_dark_dim.attr, | |
277 | &dev_attr_office_max.attr, | |
278 | &dev_attr_office_dim.attr, | |
279 | &dev_attr_daylight_max.attr, | |
280 | &dev_attr_daylight_dim.attr, | |
281 | NULL | |
282 | }; | |
283 | ||
284 | static const struct attribute_group adp5520_bl_attr_group = { | |
285 | .attrs = adp5520_bl_attributes, | |
286 | }; | |
287 | ||
1b9e450d | 288 | static int adp5520_bl_probe(struct platform_device *pdev) |
a7998cec | 289 | { |
a19a6ee6 | 290 | struct backlight_properties props; |
a7998cec MH |
291 | struct backlight_device *bl; |
292 | struct adp5520_bl *data; | |
293 | int ret = 0; | |
294 | ||
050ea48b | 295 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
a7998cec MH |
296 | if (data == NULL) |
297 | return -ENOMEM; | |
298 | ||
299 | data->master = pdev->dev.parent; | |
c512794c | 300 | data->pdata = dev_get_platdata(&pdev->dev); |
a7998cec MH |
301 | |
302 | if (data->pdata == NULL) { | |
303 | dev_err(&pdev->dev, "missing platform data\n"); | |
a7998cec MH |
304 | return -ENODEV; |
305 | } | |
306 | ||
307 | data->id = pdev->id; | |
308 | data->current_brightness = 0; | |
309 | ||
310 | mutex_init(&data->lock); | |
311 | ||
a19a6ee6 | 312 | memset(&props, 0, sizeof(struct backlight_properties)); |
bb7ca747 | 313 | props.type = BACKLIGHT_RAW; |
a19a6ee6 | 314 | props.max_brightness = ADP5020_MAX_BRIGHTNESS; |
5f652c71 JH |
315 | bl = devm_backlight_device_register(&pdev->dev, pdev->name, |
316 | data->master, data, &adp5520_bl_ops, | |
317 | &props); | |
a7998cec MH |
318 | if (IS_ERR(bl)) { |
319 | dev_err(&pdev->dev, "failed to register backlight\n"); | |
a7998cec MH |
320 | return PTR_ERR(bl); |
321 | } | |
322 | ||
a19a6ee6 | 323 | bl->props.brightness = ADP5020_MAX_BRIGHTNESS; |
a7998cec MH |
324 | if (data->pdata->en_ambl_sens) |
325 | ret = sysfs_create_group(&bl->dev.kobj, | |
326 | &adp5520_bl_attr_group); | |
327 | ||
328 | if (ret) { | |
329 | dev_err(&pdev->dev, "failed to register sysfs\n"); | |
5f652c71 | 330 | return ret; |
a7998cec MH |
331 | } |
332 | ||
333 | platform_set_drvdata(pdev, bl); | |
334 | ret |= adp5520_bl_setup(bl); | |
335 | backlight_update_status(bl); | |
336 | ||
337 | return ret; | |
338 | } | |
339 | ||
7e4b9d0b | 340 | static int adp5520_bl_remove(struct platform_device *pdev) |
a7998cec MH |
341 | { |
342 | struct backlight_device *bl = platform_get_drvdata(pdev); | |
343 | struct adp5520_bl *data = bl_get_data(bl); | |
344 | ||
c24b6b6a | 345 | adp5520_clr_bits(data->master, ADP5520_MODE_STATUS, ADP5520_BL_EN); |
a7998cec MH |
346 | |
347 | if (data->pdata->en_ambl_sens) | |
348 | sysfs_remove_group(&bl->dev.kobj, | |
349 | &adp5520_bl_attr_group); | |
350 | ||
a7998cec MH |
351 | return 0; |
352 | } | |
353 | ||
14c1b778 JH |
354 | #ifdef CONFIG_PM_SLEEP |
355 | static int adp5520_bl_suspend(struct device *dev) | |
a7998cec | 356 | { |
14c1b778 JH |
357 | struct backlight_device *bl = dev_get_drvdata(dev); |
358 | ||
a7998cec MH |
359 | return adp5520_bl_set(bl, 0); |
360 | } | |
361 | ||
14c1b778 | 362 | static int adp5520_bl_resume(struct device *dev) |
a7998cec | 363 | { |
14c1b778 | 364 | struct backlight_device *bl = dev_get_drvdata(dev); |
a7998cec MH |
365 | |
366 | backlight_update_status(bl); | |
367 | return 0; | |
368 | } | |
a7998cec MH |
369 | #endif |
370 | ||
14c1b778 JH |
371 | static SIMPLE_DEV_PM_OPS(adp5520_bl_pm_ops, adp5520_bl_suspend, |
372 | adp5520_bl_resume); | |
373 | ||
a7998cec MH |
374 | static struct platform_driver adp5520_bl_driver = { |
375 | .driver = { | |
376 | .name = "adp5520-backlight", | |
377 | .owner = THIS_MODULE, | |
14c1b778 | 378 | .pm = &adp5520_bl_pm_ops, |
a7998cec MH |
379 | }, |
380 | .probe = adp5520_bl_probe, | |
d1723fa2 | 381 | .remove = adp5520_bl_remove, |
a7998cec MH |
382 | }; |
383 | ||
81178e02 | 384 | module_platform_driver(adp5520_bl_driver); |
a7998cec MH |
385 | |
386 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | |
387 | MODULE_DESCRIPTION("ADP5520(01) Backlight Driver"); | |
388 | MODULE_LICENSE("GPL"); | |
389 | MODULE_ALIAS("platform:adp5520-backlight"); |