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>
16 #include <linux/module.h>
19 struct device
*master
;
20 struct adp5520_backlight_platform_data
*pdata
;
22 unsigned long cached_daylight_max
;
24 int current_brightness
;
27 static int adp5520_bl_set(struct backlight_device
*bl
, int brightness
)
29 struct adp5520_bl
*data
= bl_get_data(bl
);
30 struct device
*master
= data
->master
;
33 if (data
->pdata
->en_ambl_sens
) {
34 if ((brightness
> 0) && (brightness
< ADP5020_MAX_BRIGHTNESS
)) {
35 /* Disable Ambient Light auto adjust */
36 ret
|= adp5520_clr_bits(master
, ADP5520_BL_CONTROL
,
38 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
,
42 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
43 * restore daylight l3 sysfs brightness
45 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
,
46 data
->cached_daylight_max
);
47 ret
|= adp5520_set_bits(master
, ADP5520_BL_CONTROL
,
51 ret
|= adp5520_write(master
, ADP5520_DAYLIGHT_MAX
, brightness
);
54 if (data
->current_brightness
&& brightness
== 0)
55 ret
|= adp5520_set_bits(master
,
56 ADP5520_MODE_STATUS
, ADP5520_DIM_EN
);
57 else if (data
->current_brightness
== 0 && brightness
)
58 ret
|= adp5520_clr_bits(master
,
59 ADP5520_MODE_STATUS
, ADP5520_DIM_EN
);
62 data
->current_brightness
= brightness
;
67 static int adp5520_bl_update_status(struct backlight_device
*bl
)
69 int brightness
= bl
->props
.brightness
;
70 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
73 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
76 return adp5520_bl_set(bl
, brightness
);
79 static int adp5520_bl_get_brightness(struct backlight_device
*bl
)
81 struct adp5520_bl
*data
= bl_get_data(bl
);
85 error
= adp5520_read(data
->master
, ADP5520_BL_VALUE
, ®_val
);
87 return error
? data
->current_brightness
: reg_val
;
90 static const struct backlight_ops adp5520_bl_ops
= {
91 .update_status
= adp5520_bl_update_status
,
92 .get_brightness
= adp5520_bl_get_brightness
,
95 static int adp5520_bl_setup(struct backlight_device
*bl
)
97 struct adp5520_bl
*data
= bl_get_data(bl
);
98 struct device
*master
= data
->master
;
99 struct adp5520_backlight_platform_data
*pdata
= data
->pdata
;
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
);
107 if (pdata
->en_ambl_sens
) {
108 data
->cached_daylight_max
= pdata
->l1_daylight_max
;
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
,
115 ret
|= adp5520_write(master
, ADP5520_DARK_DIM
,
117 ret
|= adp5520_write(master
, ADP5520_L2_TRIP
,
119 ret
|= adp5520_write(master
, ADP5520_L2_HYS
,
121 ret
|= adp5520_write(master
, ADP5520_L3_TRIP
,
123 ret
|= adp5520_write(master
, ADP5520_L3_HYS
,
125 ret
|= adp5520_write(master
, ADP5520_ALS_CMPR_CFG
,
126 ALS_CMPR_CFG_VAL(pdata
->abml_filt
,
130 ret
|= adp5520_write(master
, ADP5520_BL_CONTROL
,
131 BL_CTRL_VAL(pdata
->fade_led_law
,
132 pdata
->en_ambl_sens
));
134 ret
|= adp5520_write(master
, ADP5520_BL_FADE
, FADE_VAL(pdata
->fade_in
,
137 ret
|= adp5520_set_bits(master
, ADP5520_MODE_STATUS
,
138 ADP5520_BL_EN
| ADP5520_DIM_EN
);
143 static ssize_t
adp5520_show(struct device
*dev
, char *buf
, int reg
)
145 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
149 mutex_lock(&data
->lock
);
150 ret
= adp5520_read(data
->master
, reg
, ®_val
);
151 mutex_unlock(&data
->lock
);
156 return sprintf(buf
, "%u\n", reg_val
);
159 static ssize_t
adp5520_store(struct device
*dev
, const char *buf
,
160 size_t count
, int reg
)
162 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
166 ret
= kstrtoul(buf
, 10, &val
);
170 mutex_lock(&data
->lock
);
171 adp5520_write(data
->master
, reg
, val
);
172 mutex_unlock(&data
->lock
);
177 static ssize_t
adp5520_bl_dark_max_show(struct device
*dev
,
178 struct device_attribute
*attr
, char *buf
)
180 return adp5520_show(dev
, buf
, ADP5520_DARK_MAX
);
183 static ssize_t
adp5520_bl_dark_max_store(struct device
*dev
,
184 struct device_attribute
*attr
,
185 const char *buf
, size_t count
)
187 return adp5520_store(dev
, buf
, count
, ADP5520_DARK_MAX
);
189 static DEVICE_ATTR(dark_max
, 0664, adp5520_bl_dark_max_show
,
190 adp5520_bl_dark_max_store
);
192 static ssize_t
adp5520_bl_office_max_show(struct device
*dev
,
193 struct device_attribute
*attr
, char *buf
)
195 return adp5520_show(dev
, buf
, ADP5520_OFFICE_MAX
);
198 static ssize_t
adp5520_bl_office_max_store(struct device
*dev
,
199 struct device_attribute
*attr
,
200 const char *buf
, size_t count
)
202 return adp5520_store(dev
, buf
, count
, ADP5520_OFFICE_MAX
);
204 static DEVICE_ATTR(office_max
, 0664, adp5520_bl_office_max_show
,
205 adp5520_bl_office_max_store
);
207 static ssize_t
adp5520_bl_daylight_max_show(struct device
*dev
,
208 struct device_attribute
*attr
, char *buf
)
210 return adp5520_show(dev
, buf
, ADP5520_DAYLIGHT_MAX
);
213 static ssize_t
adp5520_bl_daylight_max_store(struct device
*dev
,
214 struct device_attribute
*attr
,
215 const char *buf
, size_t count
)
217 struct adp5520_bl
*data
= dev_get_drvdata(dev
);
220 ret
= kstrtoul(buf
, 10, &data
->cached_daylight_max
);
224 return adp5520_store(dev
, buf
, count
, ADP5520_DAYLIGHT_MAX
);
226 static DEVICE_ATTR(daylight_max
, 0664, adp5520_bl_daylight_max_show
,
227 adp5520_bl_daylight_max_store
);
229 static ssize_t
adp5520_bl_dark_dim_show(struct device
*dev
,
230 struct device_attribute
*attr
, char *buf
)
232 return adp5520_show(dev
, buf
, ADP5520_DARK_DIM
);
235 static ssize_t
adp5520_bl_dark_dim_store(struct device
*dev
,
236 struct device_attribute
*attr
,
237 const char *buf
, size_t count
)
239 return adp5520_store(dev
, buf
, count
, ADP5520_DARK_DIM
);
241 static DEVICE_ATTR(dark_dim
, 0664, adp5520_bl_dark_dim_show
,
242 adp5520_bl_dark_dim_store
);
244 static ssize_t
adp5520_bl_office_dim_show(struct device
*dev
,
245 struct device_attribute
*attr
, char *buf
)
247 return adp5520_show(dev
, buf
, ADP5520_OFFICE_DIM
);
250 static ssize_t
adp5520_bl_office_dim_store(struct device
*dev
,
251 struct device_attribute
*attr
,
252 const char *buf
, size_t count
)
254 return adp5520_store(dev
, buf
, count
, ADP5520_OFFICE_DIM
);
256 static DEVICE_ATTR(office_dim
, 0664, adp5520_bl_office_dim_show
,
257 adp5520_bl_office_dim_store
);
259 static ssize_t
adp5520_bl_daylight_dim_show(struct device
*dev
,
260 struct device_attribute
*attr
, char *buf
)
262 return adp5520_show(dev
, buf
, ADP5520_DAYLIGHT_DIM
);
265 static ssize_t
adp5520_bl_daylight_dim_store(struct device
*dev
,
266 struct device_attribute
*attr
,
267 const char *buf
, size_t count
)
269 return adp5520_store(dev
, buf
, count
, ADP5520_DAYLIGHT_DIM
);
271 static DEVICE_ATTR(daylight_dim
, 0664, adp5520_bl_daylight_dim_show
,
272 adp5520_bl_daylight_dim_store
);
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
,
284 static const struct attribute_group adp5520_bl_attr_group
= {
285 .attrs
= adp5520_bl_attributes
,
288 static int adp5520_bl_probe(struct platform_device
*pdev
)
290 struct backlight_properties props
;
291 struct backlight_device
*bl
;
292 struct adp5520_bl
*data
;
295 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
299 data
->master
= pdev
->dev
.parent
;
300 data
->pdata
= dev_get_platdata(&pdev
->dev
);
302 if (data
->pdata
== NULL
) {
303 dev_err(&pdev
->dev
, "missing platform data\n");
308 data
->current_brightness
= 0;
310 mutex_init(&data
->lock
);
312 memset(&props
, 0, sizeof(struct backlight_properties
));
313 props
.type
= BACKLIGHT_RAW
;
314 props
.max_brightness
= ADP5020_MAX_BRIGHTNESS
;
315 bl
= devm_backlight_device_register(&pdev
->dev
, pdev
->name
,
316 data
->master
, data
, &adp5520_bl_ops
,
319 dev_err(&pdev
->dev
, "failed to register backlight\n");
323 bl
->props
.brightness
= ADP5020_MAX_BRIGHTNESS
;
324 if (data
->pdata
->en_ambl_sens
)
325 ret
= sysfs_create_group(&bl
->dev
.kobj
,
326 &adp5520_bl_attr_group
);
329 dev_err(&pdev
->dev
, "failed to register sysfs\n");
333 platform_set_drvdata(pdev
, bl
);
334 ret
|= adp5520_bl_setup(bl
);
335 backlight_update_status(bl
);
340 static int adp5520_bl_remove(struct platform_device
*pdev
)
342 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
343 struct adp5520_bl
*data
= bl_get_data(bl
);
345 adp5520_clr_bits(data
->master
, ADP5520_MODE_STATUS
, ADP5520_BL_EN
);
347 if (data
->pdata
->en_ambl_sens
)
348 sysfs_remove_group(&bl
->dev
.kobj
,
349 &adp5520_bl_attr_group
);
354 #ifdef CONFIG_PM_SLEEP
355 static int adp5520_bl_suspend(struct device
*dev
)
357 struct backlight_device
*bl
= dev_get_drvdata(dev
);
359 return adp5520_bl_set(bl
, 0);
362 static int adp5520_bl_resume(struct device
*dev
)
364 struct backlight_device
*bl
= dev_get_drvdata(dev
);
366 backlight_update_status(bl
);
371 static SIMPLE_DEV_PM_OPS(adp5520_bl_pm_ops
, adp5520_bl_suspend
,
374 static struct platform_driver adp5520_bl_driver
= {
376 .name
= "adp5520-backlight",
377 .owner
= THIS_MODULE
,
378 .pm
= &adp5520_bl_pm_ops
,
380 .probe
= adp5520_bl_probe
,
381 .remove
= adp5520_bl_remove
,
384 module_platform_driver(adp5520_bl_driver
);
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");