blob: fe8e3d1c97980c4d5db5f4dd344774576ededa2f [file] [log] [blame]
Michael Hennericha7998ce2009-09-17 14:29:52 -04001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker355b2002011-07-03 16:17:28 -040016#include <linux/module.h>
Michael Hennericha7998ce2009-09-17 14:29:52 -040017
18struct adp5520_bl {
19 struct device *master;
Michael Hennerichc24b6b62009-10-10 13:54:04 -040020 struct adp5520_backlight_platform_data *pdata;
Michael Hennericha7998ce2009-09-17 14:29:52 -040021 struct mutex lock;
22 unsigned long cached_daylight_max;
23 int id;
24 int current_brightness;
25};
26
27static 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 */
Michael Hennerichc24b6b62009-10-10 13:54:04 -040036 ret |= adp5520_clr_bits(master, ADP5520_BL_CONTROL,
37 ADP5520_BL_AUTO_ADJ);
38 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
39 brightness);
Michael Hennericha7998ce2009-09-17 14:29:52 -040040 } else {
41 /*
42 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
43 * restore daylight l3 sysfs brightness
44 */
Michael Hennerichc24b6b62009-10-10 13:54:04 -040045 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
Michael Hennericha7998ce2009-09-17 14:29:52 -040046 data->cached_daylight_max);
Michael Hennerichc24b6b62009-10-10 13:54:04 -040047 ret |= adp5520_set_bits(master, ADP5520_BL_CONTROL,
48 ADP5520_BL_AUTO_ADJ);
Michael Hennericha7998ce2009-09-17 14:29:52 -040049 }
50 } else {
Michael Hennerichc24b6b62009-10-10 13:54:04 -040051 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, brightness);
Michael Hennericha7998ce2009-09-17 14:29:52 -040052 }
53
54 if (data->current_brightness && brightness == 0)
55 ret |= adp5520_set_bits(master,
Michael Hennerichc24b6b62009-10-10 13:54:04 -040056 ADP5520_MODE_STATUS, ADP5520_DIM_EN);
Michael Hennericha7998ce2009-09-17 14:29:52 -040057 else if (data->current_brightness == 0 && brightness)
58 ret |= adp5520_clr_bits(master,
Michael Hennerichc24b6b62009-10-10 13:54:04 -040059 ADP5520_MODE_STATUS, ADP5520_DIM_EN);
Michael Hennericha7998ce2009-09-17 14:29:52 -040060
61 if (!ret)
62 data->current_brightness = brightness;
63
64 return ret;
65}
66
67static 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
79static 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
Michael Hennerichc24b6b62009-10-10 13:54:04 -040085 error = adp5520_read(data->master, ADP5520_BL_VALUE, &reg_val);
Michael Hennericha7998ce2009-09-17 14:29:52 -040086
87 return error ? data->current_brightness : reg_val;
88}
89
Emese Revfy9905a432009-12-14 00:58:57 +010090static const struct backlight_ops adp5520_bl_ops = {
Michael Hennericha7998ce2009-09-17 14:29:52 -040091 .update_status = adp5520_bl_update_status,
92 .get_brightness = adp5520_bl_get_brightness,
93};
94
95static int adp5520_bl_setup(struct backlight_device *bl)
96{
97 struct adp5520_bl *data = bl_get_data(bl);
98 struct device *master = data->master;
Michael Hennerichc24b6b62009-10-10 13:54:04 -040099 struct adp5520_backlight_platform_data *pdata = data->pdata;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400100 int ret = 0;
101
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400102 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);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400106
107 if (pdata->en_ambl_sens) {
108 data->cached_daylight_max = pdata->l1_daylight_max;
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400109 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));
Michael Hennericha7998ce2009-09-17 14:29:52 -0400128 }
129
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400130 ret |= adp5520_write(master, ADP5520_BL_CONTROL,
131 BL_CTRL_VAL(pdata->fade_led_law,
132 pdata->en_ambl_sens));
Michael Hennericha7998ce2009-09-17 14:29:52 -0400133
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400134 ret |= adp5520_write(master, ADP5520_BL_FADE, FADE_VAL(pdata->fade_in,
Michael Hennericha7998ce2009-09-17 14:29:52 -0400135 pdata->fade_out));
136
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400137 ret |= adp5520_set_bits(master, ADP5520_MODE_STATUS,
138 ADP5520_BL_EN | ADP5520_DIM_EN);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400139
140 return ret;
141}
142
143static ssize_t adp5520_show(struct device *dev, char *buf, int reg)
144{
145 struct adp5520_bl *data = dev_get_drvdata(dev);
Devendra Naga359177e2013-04-29 16:17:49 -0700146 int ret;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400147 uint8_t reg_val;
148
149 mutex_lock(&data->lock);
Devendra Naga359177e2013-04-29 16:17:49 -0700150 ret = adp5520_read(data->master, reg, &reg_val);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400151 mutex_unlock(&data->lock);
152
Devendra Naga359177e2013-04-29 16:17:49 -0700153 if (ret < 0)
154 return ret;
155
Michael Hennericha7998ce2009-09-17 14:29:52 -0400156 return sprintf(buf, "%u\n", reg_val);
157}
158
159static 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
Jingoo Han71d72252012-05-29 15:07:15 -0700166 ret = kstrtoul(buf, 10, &val);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400167 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
177static ssize_t adp5520_bl_dark_max_show(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400178 struct device_attribute *attr, char *buf)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400179{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400180 return adp5520_show(dev, buf, ADP5520_DARK_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400181}
182
183static ssize_t adp5520_bl_dark_max_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400184 struct device_attribute *attr,
185 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400186{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400187 return adp5520_store(dev, buf, count, ADP5520_DARK_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400188}
189static DEVICE_ATTR(dark_max, 0664, adp5520_bl_dark_max_show,
190 adp5520_bl_dark_max_store);
191
192static ssize_t adp5520_bl_office_max_show(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400193 struct device_attribute *attr, char *buf)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400194{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400195 return adp5520_show(dev, buf, ADP5520_OFFICE_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400196}
197
198static ssize_t adp5520_bl_office_max_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400199 struct device_attribute *attr,
200 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400201{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400202 return adp5520_store(dev, buf, count, ADP5520_OFFICE_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400203}
204static DEVICE_ATTR(office_max, 0664, adp5520_bl_office_max_show,
205 adp5520_bl_office_max_store);
206
207static ssize_t adp5520_bl_daylight_max_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400210 return adp5520_show(dev, buf, ADP5520_DAYLIGHT_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400211}
212
213static ssize_t adp5520_bl_daylight_max_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400214 struct device_attribute *attr,
215 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400216{
217 struct adp5520_bl *data = dev_get_drvdata(dev);
Liu Yuan877947b2011-05-24 17:11:12 -0700218 int ret;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400219
Jingoo Han71d72252012-05-29 15:07:15 -0700220 ret = kstrtoul(buf, 10, &data->cached_daylight_max);
Liu Yuan877947b2011-05-24 17:11:12 -0700221 if (ret < 0)
222 return ret;
223
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400224 return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400225}
226static DEVICE_ATTR(daylight_max, 0664, adp5520_bl_daylight_max_show,
227 adp5520_bl_daylight_max_store);
228
229static ssize_t adp5520_bl_dark_dim_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400232 return adp5520_show(dev, buf, ADP5520_DARK_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400233}
234
235static ssize_t adp5520_bl_dark_dim_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400236 struct device_attribute *attr,
237 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400238{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400239 return adp5520_store(dev, buf, count, ADP5520_DARK_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400240}
241static DEVICE_ATTR(dark_dim, 0664, adp5520_bl_dark_dim_show,
242 adp5520_bl_dark_dim_store);
243
244static ssize_t adp5520_bl_office_dim_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
246{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400247 return adp5520_show(dev, buf, ADP5520_OFFICE_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400248}
249
250static ssize_t adp5520_bl_office_dim_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400251 struct device_attribute *attr,
252 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400253{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400254 return adp5520_store(dev, buf, count, ADP5520_OFFICE_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400255}
256static DEVICE_ATTR(office_dim, 0664, adp5520_bl_office_dim_show,
257 adp5520_bl_office_dim_store);
258
259static ssize_t adp5520_bl_daylight_dim_show(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400260 struct device_attribute *attr, char *buf)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400261{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400262 return adp5520_show(dev, buf, ADP5520_DAYLIGHT_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400263}
264
265static ssize_t adp5520_bl_daylight_dim_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400266 struct device_attribute *attr,
267 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400268{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400269 return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400270}
271static DEVICE_ATTR(daylight_dim, 0664, adp5520_bl_daylight_dim_show,
272 adp5520_bl_daylight_dim_store);
273
274static 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
284static const struct attribute_group adp5520_bl_attr_group = {
285 .attrs = adp5520_bl_attributes,
286};
287
Bill Pemberton1b9e4502012-11-19 13:21:46 -0500288static int adp5520_bl_probe(struct platform_device *pdev)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400289{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500290 struct backlight_properties props;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400291 struct backlight_device *bl;
292 struct adp5520_bl *data;
293 int ret = 0;
294
Julia Lawall050ea482012-03-23 15:02:01 -0700295 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400296 if (data == NULL)
297 return -ENOMEM;
298
299 data->master = pdev->dev.parent;
Jingoo Hanc512794c2013-11-12 15:09:04 -0800300 data->pdata = dev_get_platdata(&pdev->dev);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400301
302 if (data->pdata == NULL) {
303 dev_err(&pdev->dev, "missing platform data\n");
Michael Hennericha7998ce2009-09-17 14:29:52 -0400304 return -ENODEV;
305 }
306
307 data->id = pdev->id;
308 data->current_brightness = 0;
309
310 mutex_init(&data->lock);
311
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500312 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700313 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500314 props.max_brightness = ADP5020_MAX_BRIGHTNESS;
315 bl = backlight_device_register(pdev->name, data->master, data,
316 &adp5520_bl_ops, &props);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400317 if (IS_ERR(bl)) {
318 dev_err(&pdev->dev, "failed to register backlight\n");
Michael Hennericha7998ce2009-09-17 14:29:52 -0400319 return PTR_ERR(bl);
320 }
321
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500322 bl->props.brightness = ADP5020_MAX_BRIGHTNESS;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400323 if (data->pdata->en_ambl_sens)
324 ret = sysfs_create_group(&bl->dev.kobj,
325 &adp5520_bl_attr_group);
326
327 if (ret) {
328 dev_err(&pdev->dev, "failed to register sysfs\n");
329 backlight_device_unregister(bl);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400330 }
331
332 platform_set_drvdata(pdev, bl);
333 ret |= adp5520_bl_setup(bl);
334 backlight_update_status(bl);
335
336 return ret;
337}
338
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500339static int adp5520_bl_remove(struct platform_device *pdev)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400340{
341 struct backlight_device *bl = platform_get_drvdata(pdev);
342 struct adp5520_bl *data = bl_get_data(bl);
343
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400344 adp5520_clr_bits(data->master, ADP5520_MODE_STATUS, ADP5520_BL_EN);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400345
346 if (data->pdata->en_ambl_sens)
347 sysfs_remove_group(&bl->dev.kobj,
348 &adp5520_bl_attr_group);
349
350 backlight_device_unregister(bl);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400351
352 return 0;
353}
354
Jingoo Han14c1b772013-04-29 16:17:38 -0700355#ifdef CONFIG_PM_SLEEP
356static int adp5520_bl_suspend(struct device *dev)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400357{
Jingoo Han14c1b772013-04-29 16:17:38 -0700358 struct backlight_device *bl = dev_get_drvdata(dev);
359
Michael Hennericha7998ce2009-09-17 14:29:52 -0400360 return adp5520_bl_set(bl, 0);
361}
362
Jingoo Han14c1b772013-04-29 16:17:38 -0700363static int adp5520_bl_resume(struct device *dev)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400364{
Jingoo Han14c1b772013-04-29 16:17:38 -0700365 struct backlight_device *bl = dev_get_drvdata(dev);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400366
367 backlight_update_status(bl);
368 return 0;
369}
Michael Hennericha7998ce2009-09-17 14:29:52 -0400370#endif
371
Jingoo Han14c1b772013-04-29 16:17:38 -0700372static SIMPLE_DEV_PM_OPS(adp5520_bl_pm_ops, adp5520_bl_suspend,
373 adp5520_bl_resume);
374
Michael Hennericha7998ce2009-09-17 14:29:52 -0400375static struct platform_driver adp5520_bl_driver = {
376 .driver = {
377 .name = "adp5520-backlight",
378 .owner = THIS_MODULE,
Jingoo Han14c1b772013-04-29 16:17:38 -0700379 .pm = &adp5520_bl_pm_ops,
Michael Hennericha7998ce2009-09-17 14:29:52 -0400380 },
381 .probe = adp5520_bl_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500382 .remove = adp5520_bl_remove,
Michael Hennericha7998ce2009-09-17 14:29:52 -0400383};
384
Axel Lin81178e02012-01-10 15:09:11 -0800385module_platform_driver(adp5520_bl_driver);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400386
387MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
388MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
389MODULE_LICENSE("GPL");
390MODULE_ALIAS("platform:adp5520-backlight");