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>
17 struct device
*master
;
18 struct adp5520_backlight_platfrom_data
*pdata
;
20 unsigned long cached_daylight_max
;
22 int current_brightness
;
25 static int adp5520_bl_set(struct backlight_device
*bl
, int brightness
)
27 struct adp5520_bl
*data
= bl_get_data(bl
);
28 struct device
*master
= data
->master
;
31 if (data
->pdata
->en_ambl_sens
) {
32 if ((brightness
> 0) && (brightness
< ADP5020_MAX_BRIGHTNESS
)) {
33 /* Disable Ambient Light auto adjust */
34 ret
|= adp5520_clr_bits(master
, BL_CONTROL
,
36 ret
|= adp5520_write(master
, DAYLIGHT_MAX
, brightness
);
39 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
40 * restore daylight l3 sysfs brightness
42 ret
|= adp5520_write(master
, DAYLIGHT_MAX
,
43 data
->cached_daylight_max
);
44 ret
|= adp5520_set_bits(master
, BL_CONTROL
,
48 ret
|= adp5520_write(master
, DAYLIGHT_MAX
, brightness
);
51 if (data
->current_brightness
&& brightness
== 0)
52 ret
|= adp5520_set_bits(master
,
54 else if (data
->current_brightness
== 0 && brightness
)
55 ret
|= adp5520_clr_bits(master
,
59 data
->current_brightness
= brightness
;
64 static int adp5520_bl_update_status(struct backlight_device
*bl
)
66 int brightness
= bl
->props
.brightness
;
67 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
70 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
73 return adp5520_bl_set(bl
, brightness
);
76 static int adp5520_bl_get_brightness(struct backlight_device
*bl
)
78 struct adp5520_bl
*data
= bl_get_data(bl
);
82 error
= adp5520_read(data
->master
, BL_VALUE
, ®_val
);
84 return error
? data
->current_brightness
: reg_val
;
87 static struct backlight_ops adp5520_bl_ops
= {
88 .update_status
= adp5520_bl_update_status
,
89 .get_brightness
= adp5520_bl_get_brightness
,
92 static int adp5520_bl_setup(struct backlight_device
*bl
)
94 struct adp5520_bl
*data
= bl_get_data(bl
);
95 struct device
*master
= data
->master
;
96 struct adp5520_backlight_platfrom_data
*pdata
= data
->pdata
;
99 ret
|= adp5520_write(master
, DAYLIGHT_MAX
, pdata
->l1_daylight_max
);
100 ret
|= adp5520_write(master
, DAYLIGHT_DIM
, pdata
->l1_daylight_dim
);
102 if (pdata
->en_ambl_sens
) {
103 data
->cached_daylight_max
= pdata
->l1_daylight_max
;
104 ret
|= adp5520_write(master
, OFFICE_MAX
, pdata
->l2_office_max
);
105 ret
|= adp5520_write(master
, OFFICE_DIM
, pdata
->l2_office_dim
);
106 ret
|= adp5520_write(master
, DARK_MAX
, pdata
->l3_dark_max
);
107 ret
|= adp5520_write(master
, DARK_DIM
, pdata
->l3_dark_dim
);
108 ret
|= adp5520_write(master
, L2_TRIP
, pdata
->l2_trip
);
109 ret
|= adp5520_write(master
, L2_HYS
, pdata
->l2_hyst
);
110 ret
|= adp5520_write(master
, L3_TRIP
, pdata
->l3_trip
);
111 ret
|= adp5520_write(master
, L3_HYS
, pdata
->l3_hyst
);
112 ret
|= adp5520_write(master
, ALS_CMPR_CFG
,
113 ALS_CMPR_CFG_VAL(pdata
->abml_filt
, L3_EN
));
116 ret
|= adp5520_write(master
, BL_CONTROL
,
117 BL_CTRL_VAL(pdata
->fade_led_law
, pdata
->en_ambl_sens
));
119 ret
|= adp5520_write(master
, BL_FADE
, FADE_VAL(pdata
->fade_in
,
122 ret
|= adp5520_set_bits(master
, MODE_STATUS
, BL_EN
| DIM_EN
);
127 static ssize_t
adp5520_show(struct device
*dev
, char *buf
, int reg
)
129 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
133 mutex_lock(&data
->lock
);
134 error
= adp5520_read(data
->master
, reg
, ®_val
);
135 mutex_unlock(&data
->lock
);
137 return sprintf(buf
, "%u\n", reg_val
);
140 static ssize_t
adp5520_store(struct device
*dev
, const char *buf
,
141 size_t count
, int reg
)
143 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
147 ret
= strict_strtoul(buf
, 10, &val
);
151 mutex_lock(&data
->lock
);
152 adp5520_write(data
->master
, reg
, val
);
153 mutex_unlock(&data
->lock
);
158 static ssize_t
adp5520_bl_dark_max_show(struct device
*dev
,
159 struct device_attribute
*attr
, char *buf
)
161 return adp5520_show(dev
, buf
, DARK_MAX
);
164 static ssize_t
adp5520_bl_dark_max_store(struct device
*dev
,
165 struct device_attribute
*attr
, const char *buf
, size_t count
)
167 return adp5520_store(dev
, buf
, count
, DARK_MAX
);
169 static DEVICE_ATTR(dark_max
, 0664, adp5520_bl_dark_max_show
,
170 adp5520_bl_dark_max_store
);
172 static ssize_t
adp5520_bl_office_max_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
175 return adp5520_show(dev
, buf
, OFFICE_MAX
);
178 static ssize_t
adp5520_bl_office_max_store(struct device
*dev
,
179 struct device_attribute
*attr
, const char *buf
, size_t count
)
181 return adp5520_store(dev
, buf
, count
, OFFICE_MAX
);
183 static DEVICE_ATTR(office_max
, 0664, adp5520_bl_office_max_show
,
184 adp5520_bl_office_max_store
);
186 static ssize_t
adp5520_bl_daylight_max_show(struct device
*dev
,
187 struct device_attribute
*attr
, char *buf
)
189 return adp5520_show(dev
, buf
, DAYLIGHT_MAX
);
192 static ssize_t
adp5520_bl_daylight_max_store(struct device
*dev
,
193 struct device_attribute
*attr
, const char *buf
, size_t count
)
195 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
197 strict_strtoul(buf
, 10, &data
->cached_daylight_max
);
198 return adp5520_store(dev
, buf
, count
, DAYLIGHT_MAX
);
200 static DEVICE_ATTR(daylight_max
, 0664, adp5520_bl_daylight_max_show
,
201 adp5520_bl_daylight_max_store
);
203 static ssize_t
adp5520_bl_dark_dim_show(struct device
*dev
,
204 struct device_attribute
*attr
, char *buf
)
206 return adp5520_show(dev
, buf
, DARK_DIM
);
209 static ssize_t
adp5520_bl_dark_dim_store(struct device
*dev
,
210 struct device_attribute
*attr
,
211 const char *buf
, size_t count
)
213 return adp5520_store(dev
, buf
, count
, DARK_DIM
);
215 static DEVICE_ATTR(dark_dim
, 0664, adp5520_bl_dark_dim_show
,
216 adp5520_bl_dark_dim_store
);
218 static ssize_t
adp5520_bl_office_dim_show(struct device
*dev
,
219 struct device_attribute
*attr
, char *buf
)
221 return adp5520_show(dev
, buf
, OFFICE_DIM
);
224 static ssize_t
adp5520_bl_office_dim_store(struct device
*dev
,
225 struct device_attribute
*attr
,
226 const char *buf
, size_t count
)
228 return adp5520_store(dev
, buf
, count
, OFFICE_DIM
);
230 static DEVICE_ATTR(office_dim
, 0664, adp5520_bl_office_dim_show
,
231 adp5520_bl_office_dim_store
);
233 static ssize_t
adp5520_bl_daylight_dim_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 return adp5520_show(dev
, buf
, DAYLIGHT_DIM
);
239 static ssize_t
adp5520_bl_daylight_dim_store(struct device
*dev
,
240 struct device_attribute
*attr
,
241 const char *buf
, size_t count
)
243 return adp5520_store(dev
, buf
, count
, DAYLIGHT_DIM
);
245 static DEVICE_ATTR(daylight_dim
, 0664, adp5520_bl_daylight_dim_show
,
246 adp5520_bl_daylight_dim_store
);
248 static struct attribute
*adp5520_bl_attributes
[] = {
249 &dev_attr_dark_max
.attr
,
250 &dev_attr_dark_dim
.attr
,
251 &dev_attr_office_max
.attr
,
252 &dev_attr_office_dim
.attr
,
253 &dev_attr_daylight_max
.attr
,
254 &dev_attr_daylight_dim
.attr
,
258 static const struct attribute_group adp5520_bl_attr_group
= {
259 .attrs
= adp5520_bl_attributes
,
262 static int __devinit
adp5520_bl_probe(struct platform_device
*pdev
)
264 struct backlight_device
*bl
;
265 struct adp5520_bl
*data
;
268 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
272 data
->master
= pdev
->dev
.parent
;
273 data
->pdata
= pdev
->dev
.platform_data
;
275 if (data
->pdata
== NULL
) {
276 dev_err(&pdev
->dev
, "missing platform data\n");
282 data
->current_brightness
= 0;
284 mutex_init(&data
->lock
);
286 bl
= backlight_device_register(pdev
->name
, data
->master
,
287 data
, &adp5520_bl_ops
);
289 dev_err(&pdev
->dev
, "failed to register backlight\n");
294 bl
->props
.max_brightness
=
295 bl
->props
.brightness
= ADP5020_MAX_BRIGHTNESS
;
297 if (data
->pdata
->en_ambl_sens
)
298 ret
= sysfs_create_group(&bl
->dev
.kobj
,
299 &adp5520_bl_attr_group
);
302 dev_err(&pdev
->dev
, "failed to register sysfs\n");
303 backlight_device_unregister(bl
);
307 platform_set_drvdata(pdev
, bl
);
308 ret
|= adp5520_bl_setup(bl
);
309 backlight_update_status(bl
);
314 static int __devexit
adp5520_bl_remove(struct platform_device
*pdev
)
316 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
317 struct adp5520_bl
*data
= bl_get_data(bl
);
319 adp5520_clr_bits(data
->master
, MODE_STATUS
, BL_EN
);
321 if (data
->pdata
->en_ambl_sens
)
322 sysfs_remove_group(&bl
->dev
.kobj
,
323 &adp5520_bl_attr_group
);
325 backlight_device_unregister(bl
);
332 static int adp5520_bl_suspend(struct platform_device
*pdev
,
335 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
336 return adp5520_bl_set(bl
, 0);
339 static int adp5520_bl_resume(struct platform_device
*pdev
)
341 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
343 backlight_update_status(bl
);
347 #define adp5520_bl_suspend NULL
348 #define adp5520_bl_resume NULL
351 static struct platform_driver adp5520_bl_driver
= {
353 .name
= "adp5520-backlight",
354 .owner
= THIS_MODULE
,
356 .probe
= adp5520_bl_probe
,
357 .remove
= __devexit_p(adp5520_bl_remove
),
358 .suspend
= adp5520_bl_suspend
,
359 .resume
= adp5520_bl_resume
,
362 static int __init
adp5520_bl_init(void)
364 return platform_driver_register(&adp5520_bl_driver
);
366 module_init(adp5520_bl_init
);
368 static void __exit
adp5520_bl_exit(void)
370 platform_driver_unregister(&adp5520_bl_driver
);
372 module_exit(adp5520_bl_exit
);
374 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
375 MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
376 MODULE_LICENSE("GPL");
377 MODULE_ALIAS("platform:adp5520-backlight");