2 * Backlight driver for Analog Devices ADP5520/ADP5501 MFD PMICs
4 * Copyright 2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
13 #include <linux/backlight.h>
14 #include <linux/mfd/adp5520.h>
15 #include <linux/slab.h>
18 struct device
*master
;
19 struct adp5520_backlight_platform_data
*pdata
;
21 unsigned long cached_daylight_max
;
23 int current_brightness
;
26 static int adp5520_bl_set(struct backlight_device
*bl
, int brightness
)
28 struct adp5520_bl
*data
= bl_get_data(bl
);
29 struct device
*master
= data
->master
;
32 if (data
->pdata
->en_ambl_sens
) {
33 if ((brightness
> 0) && (brightness
< ADP5020_MAX_BRIGHTNESS
)) {
34 /* Disable Ambient Light auto adjust */
35 ret
|= adp5520_clr_bits(master
, ADP5520_BL_CONTROL
,
37 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
,
41 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
42 * restore daylight l3 sysfs brightness
44 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
,
45 data
->cached_daylight_max
);
46 ret
|= adp5520_set_bits(master
, ADP5520_BL_CONTROL
,
50 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
, brightness
);
53 if (data
->current_brightness
&& brightness
== 0)
54 ret
|= adp5520_set_bits(master
,
55 ADP5520_MODE_STATUS
, ADP5520_DIM_EN
);
56 else if (data
->current_brightness
== 0 && brightness
)
57 ret
|= adp5520_clr_bits(master
,
58 ADP5520_MODE_STATUS
, ADP5520_DIM_EN
);
61 data
->current_brightness
= brightness
;
66 static int adp5520_bl_update_status(struct backlight_device
*bl
)
68 int brightness
= bl
->props
.brightness
;
69 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
72 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
75 return adp5520_bl_set(bl
, brightness
);
78 static int adp5520_bl_get_brightness(struct backlight_device
*bl
)
80 struct adp5520_bl
*data
= bl_get_data(bl
);
84 error
= adp5520_read(data
->master
, ADP5520_BL_VALUE
, ®_val
);
86 return error
? data
->current_brightness
: reg_val
;
89 static const struct backlight_ops adp5520_bl_ops
= {
90 .update_status
= adp5520_bl_update_status
,
91 .get_brightness
= adp5520_bl_get_brightness
,
94 static int adp5520_bl_setup(struct backlight_device
*bl
)
96 struct adp5520_bl
*data
= bl_get_data(bl
);
97 struct device
*master
= data
->master
;
98 struct adp5520_backlight_platform_data
*pdata
= data
->pdata
;
101 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
,
102 pdata
->l1_daylight_max
);
103 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_DIM
,
104 pdata
->l1_daylight_dim
);
106 if (pdata
->en_ambl_sens
) {
107 data
->cached_daylight_max
= pdata
->l1_daylight_max
;
108 ret
|= adp5520_write(master
, ADP5520_OFFICE_MAX
,
109 pdata
->l2_office_max
);
110 ret
|= adp5520_write(master
, ADP5520_OFFICE_DIM
,
111 pdata
->l2_office_dim
);
112 ret
|= adp5520_write(master
, ADP5520_DARK_MAX
,
114 ret
|= adp5520_write(master
, ADP5520_DARK_DIM
,
116 ret
|= adp5520_write(master
, ADP5520_L2_TRIP
,
118 ret
|= adp5520_write(master
, ADP5520_L2_HYS
,
120 ret
|= adp5520_write(master
, ADP5520_L3_TRIP
,
122 ret
|= adp5520_write(master
, ADP5520_L3_HYS
,
124 ret
|= adp5520_write(master
, ADP5520_ALS_CMPR_CFG
,
125 ALS_CMPR_CFG_VAL(pdata
->abml_filt
,
129 ret
|= adp5520_write(master
, ADP5520_BL_CONTROL
,
130 BL_CTRL_VAL(pdata
->fade_led_law
,
131 pdata
->en_ambl_sens
));
133 ret
|= adp5520_write(master
, ADP5520_BL_FADE
, FADE_VAL(pdata
->fade_in
,
136 ret
|= adp5520_set_bits(master
, ADP5520_MODE_STATUS
,
137 ADP5520_BL_EN
| ADP5520_DIM_EN
);
142 static ssize_t
adp5520_show(struct device
*dev
, char *buf
, int reg
)
144 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
148 mutex_lock(&data
->lock
);
149 error
= adp5520_read(data
->master
, reg
, ®_val
);
150 mutex_unlock(&data
->lock
);
152 return sprintf(buf
, "%u\n", reg_val
);
155 static ssize_t
adp5520_store(struct device
*dev
, const char *buf
,
156 size_t count
, int reg
)
158 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
162 ret
= strict_strtoul(buf
, 10, &val
);
166 mutex_lock(&data
->lock
);
167 adp5520_write(data
->master
, reg
, val
);
168 mutex_unlock(&data
->lock
);
173 static ssize_t
adp5520_bl_dark_max_show(struct device
*dev
,
174 struct device_attribute
*attr
, char *buf
)
176 return adp5520_show(dev
, buf
, ADP5520_DARK_MAX
);
179 static ssize_t
adp5520_bl_dark_max_store(struct device
*dev
,
180 struct device_attribute
*attr
,
181 const char *buf
, size_t count
)
183 return adp5520_store(dev
, buf
, count
, ADP5520_DARK_MAX
);
185 static DEVICE_ATTR(dark_max
, 0664, adp5520_bl_dark_max_show
,
186 adp5520_bl_dark_max_store
);
188 static ssize_t
adp5520_bl_office_max_show(struct device
*dev
,
189 struct device_attribute
*attr
, char *buf
)
191 return adp5520_show(dev
, buf
, ADP5520_OFFICE_MAX
);
194 static ssize_t
adp5520_bl_office_max_store(struct device
*dev
,
195 struct device_attribute
*attr
,
196 const char *buf
, size_t count
)
198 return adp5520_store(dev
, buf
, count
, ADP5520_OFFICE_MAX
);
200 static DEVICE_ATTR(office_max
, 0664, adp5520_bl_office_max_show
,
201 adp5520_bl_office_max_store
);
203 static ssize_t
adp5520_bl_daylight_max_show(struct device
*dev
,
204 struct device_attribute
*attr
, char *buf
)
206 return adp5520_show(dev
, buf
, ADP5520_DAYLIGHT_MAX
);
209 static ssize_t
adp5520_bl_daylight_max_store(struct device
*dev
,
210 struct device_attribute
*attr
,
211 const char *buf
, size_t count
)
213 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
216 ret
= strict_strtoul(buf
, 10, &data
->cached_daylight_max
);
220 return adp5520_store(dev
, buf
, count
, ADP5520_DAYLIGHT_MAX
);
222 static DEVICE_ATTR(daylight_max
, 0664, adp5520_bl_daylight_max_show
,
223 adp5520_bl_daylight_max_store
);
225 static ssize_t
adp5520_bl_dark_dim_show(struct device
*dev
,
226 struct device_attribute
*attr
, char *buf
)
228 return adp5520_show(dev
, buf
, ADP5520_DARK_DIM
);
231 static ssize_t
adp5520_bl_dark_dim_store(struct device
*dev
,
232 struct device_attribute
*attr
,
233 const char *buf
, size_t count
)
235 return adp5520_store(dev
, buf
, count
, ADP5520_DARK_DIM
);
237 static DEVICE_ATTR(dark_dim
, 0664, adp5520_bl_dark_dim_show
,
238 adp5520_bl_dark_dim_store
);
240 static ssize_t
adp5520_bl_office_dim_show(struct device
*dev
,
241 struct device_attribute
*attr
, char *buf
)
243 return adp5520_show(dev
, buf
, ADP5520_OFFICE_DIM
);
246 static ssize_t
adp5520_bl_office_dim_store(struct device
*dev
,
247 struct device_attribute
*attr
,
248 const char *buf
, size_t count
)
250 return adp5520_store(dev
, buf
, count
, ADP5520_OFFICE_DIM
);
252 static DEVICE_ATTR(office_dim
, 0664, adp5520_bl_office_dim_show
,
253 adp5520_bl_office_dim_store
);
255 static ssize_t
adp5520_bl_daylight_dim_show(struct device
*dev
,
256 struct device_attribute
*attr
, char *buf
)
258 return adp5520_show(dev
, buf
, ADP5520_DAYLIGHT_DIM
);
261 static ssize_t
adp5520_bl_daylight_dim_store(struct device
*dev
,
262 struct device_attribute
*attr
,
263 const char *buf
, size_t count
)
265 return adp5520_store(dev
, buf
, count
, ADP5520_DAYLIGHT_DIM
);
267 static DEVICE_ATTR(daylight_dim
, 0664, adp5520_bl_daylight_dim_show
,
268 adp5520_bl_daylight_dim_store
);
270 static struct attribute
*adp5520_bl_attributes
[] = {
271 &dev_attr_dark_max
.attr
,
272 &dev_attr_dark_dim
.attr
,
273 &dev_attr_office_max
.attr
,
274 &dev_attr_office_dim
.attr
,
275 &dev_attr_daylight_max
.attr
,
276 &dev_attr_daylight_dim
.attr
,
280 static const struct attribute_group adp5520_bl_attr_group
= {
281 .attrs
= adp5520_bl_attributes
,
284 static int __devinit
adp5520_bl_probe(struct platform_device
*pdev
)
286 struct backlight_properties props
;
287 struct backlight_device
*bl
;
288 struct adp5520_bl
*data
;
291 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
295 data
->master
= pdev
->dev
.parent
;
296 data
->pdata
= pdev
->dev
.platform_data
;
298 if (data
->pdata
== NULL
) {
299 dev_err(&pdev
->dev
, "missing platform data\n");
305 data
->current_brightness
= 0;
307 mutex_init(&data
->lock
);
309 memset(&props
, 0, sizeof(struct backlight_properties
));
310 props
.type
= BACKLIGHT_RAW
;
311 props
.max_brightness
= ADP5020_MAX_BRIGHTNESS
;
312 bl
= backlight_device_register(pdev
->name
, data
->master
, data
,
313 &adp5520_bl_ops
, &props
);
315 dev_err(&pdev
->dev
, "failed to register backlight\n");
320 bl
->props
.brightness
= ADP5020_MAX_BRIGHTNESS
;
321 if (data
->pdata
->en_ambl_sens
)
322 ret
= sysfs_create_group(&bl
->dev
.kobj
,
323 &adp5520_bl_attr_group
);
326 dev_err(&pdev
->dev
, "failed to register sysfs\n");
327 backlight_device_unregister(bl
);
331 platform_set_drvdata(pdev
, bl
);
332 ret
|= adp5520_bl_setup(bl
);
333 backlight_update_status(bl
);
338 static int __devexit
adp5520_bl_remove(struct platform_device
*pdev
)
340 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
341 struct adp5520_bl
*data
= bl_get_data(bl
);
343 adp5520_clr_bits(data
->master
, ADP5520_MODE_STATUS
, ADP5520_BL_EN
);
345 if (data
->pdata
->en_ambl_sens
)
346 sysfs_remove_group(&bl
->dev
.kobj
,
347 &adp5520_bl_attr_group
);
349 backlight_device_unregister(bl
);
356 static int adp5520_bl_suspend(struct platform_device
*pdev
,
359 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
360 return adp5520_bl_set(bl
, 0);
363 static int adp5520_bl_resume(struct platform_device
*pdev
)
365 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
367 backlight_update_status(bl
);
371 #define adp5520_bl_suspend NULL
372 #define adp5520_bl_resume NULL
375 static struct platform_driver adp5520_bl_driver
= {
377 .name
= "adp5520-backlight",
378 .owner
= THIS_MODULE
,
380 .probe
= adp5520_bl_probe
,
381 .remove
= __devexit_p(adp5520_bl_remove
),
382 .suspend
= adp5520_bl_suspend
,
383 .resume
= adp5520_bl_resume
,
386 static int __init
adp5520_bl_init(void)
388 return platform_driver_register(&adp5520_bl_driver
);
390 module_init(adp5520_bl_init
);
392 static void __exit
adp5520_bl_exit(void)
394 platform_driver_unregister(&adp5520_bl_driver
);
396 module_exit(adp5520_bl_exit
);
398 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
399 MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
400 MODULE_LICENSE("GPL");
401 MODULE_ALIAS("platform:adp5520-backlight");