blob: dd88ba1d71ceb64849535542a8cb3289d89df4c1 [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;
Jingoo Hanf9bda392014-08-27 10:09:14 +090070
Michael Hennericha7998ce2009-09-17 14:29:52 -040071 if (bl->props.power != FB_BLANK_UNBLANK)
72 brightness = 0;
73
74 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
75 brightness = 0;
76
77 return adp5520_bl_set(bl, brightness);
78}
79
80static int adp5520_bl_get_brightness(struct backlight_device *bl)
81{
82 struct adp5520_bl *data = bl_get_data(bl);
83 int error;
84 uint8_t reg_val;
85
Michael Hennerichc24b6b62009-10-10 13:54:04 -040086 error = adp5520_read(data->master, ADP5520_BL_VALUE, &reg_val);
Michael Hennericha7998ce2009-09-17 14:29:52 -040087
88 return error ? data->current_brightness : reg_val;
89}
90
Emese Revfy9905a432009-12-14 00:58:57 +010091static const struct backlight_ops adp5520_bl_ops = {
Michael Hennericha7998ce2009-09-17 14:29:52 -040092 .update_status = adp5520_bl_update_status,
93 .get_brightness = adp5520_bl_get_brightness,
94};
95
96static int adp5520_bl_setup(struct backlight_device *bl)
97{
98 struct adp5520_bl *data = bl_get_data(bl);
99 struct device *master = data->master;
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400100 struct adp5520_backlight_platform_data *pdata = data->pdata;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400101 int ret = 0;
102
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400103 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
104 pdata->l1_daylight_max);
105 ret |= adp5520_write(master, ADP5520_DAYLIGHT_DIM,
106 pdata->l1_daylight_dim);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400107
108 if (pdata->en_ambl_sens) {
109 data->cached_daylight_max = pdata->l1_daylight_max;
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400110 ret |= adp5520_write(master, ADP5520_OFFICE_MAX,
111 pdata->l2_office_max);
112 ret |= adp5520_write(master, ADP5520_OFFICE_DIM,
113 pdata->l2_office_dim);
114 ret |= adp5520_write(master, ADP5520_DARK_MAX,
115 pdata->l3_dark_max);
116 ret |= adp5520_write(master, ADP5520_DARK_DIM,
117 pdata->l3_dark_dim);
118 ret |= adp5520_write(master, ADP5520_L2_TRIP,
119 pdata->l2_trip);
120 ret |= adp5520_write(master, ADP5520_L2_HYS,
121 pdata->l2_hyst);
122 ret |= adp5520_write(master, ADP5520_L3_TRIP,
123 pdata->l3_trip);
124 ret |= adp5520_write(master, ADP5520_L3_HYS,
125 pdata->l3_hyst);
126 ret |= adp5520_write(master, ADP5520_ALS_CMPR_CFG,
127 ALS_CMPR_CFG_VAL(pdata->abml_filt,
128 ADP5520_L3_EN));
Michael Hennericha7998ce2009-09-17 14:29:52 -0400129 }
130
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400131 ret |= adp5520_write(master, ADP5520_BL_CONTROL,
132 BL_CTRL_VAL(pdata->fade_led_law,
133 pdata->en_ambl_sens));
Michael Hennericha7998ce2009-09-17 14:29:52 -0400134
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400135 ret |= adp5520_write(master, ADP5520_BL_FADE, FADE_VAL(pdata->fade_in,
Michael Hennericha7998ce2009-09-17 14:29:52 -0400136 pdata->fade_out));
137
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400138 ret |= adp5520_set_bits(master, ADP5520_MODE_STATUS,
139 ADP5520_BL_EN | ADP5520_DIM_EN);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400140
141 return ret;
142}
143
144static ssize_t adp5520_show(struct device *dev, char *buf, int reg)
145{
146 struct adp5520_bl *data = dev_get_drvdata(dev);
Devendra Naga359177e2013-04-29 16:17:49 -0700147 int ret;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400148 uint8_t reg_val;
149
150 mutex_lock(&data->lock);
Devendra Naga359177e2013-04-29 16:17:49 -0700151 ret = adp5520_read(data->master, reg, &reg_val);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400152 mutex_unlock(&data->lock);
153
Devendra Naga359177e2013-04-29 16:17:49 -0700154 if (ret < 0)
155 return ret;
156
Michael Hennericha7998ce2009-09-17 14:29:52 -0400157 return sprintf(buf, "%u\n", reg_val);
158}
159
160static ssize_t adp5520_store(struct device *dev, const char *buf,
161 size_t count, int reg)
162{
163 struct adp5520_bl *data = dev_get_drvdata(dev);
164 unsigned long val;
165 int ret;
166
Jingoo Han71d72252012-05-29 15:07:15 -0700167 ret = kstrtoul(buf, 10, &val);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400168 if (ret)
169 return ret;
170
171 mutex_lock(&data->lock);
172 adp5520_write(data->master, reg, val);
173 mutex_unlock(&data->lock);
174
175 return count;
176}
177
178static ssize_t adp5520_bl_dark_max_show(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400179 struct device_attribute *attr, char *buf)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400180{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400181 return adp5520_show(dev, buf, ADP5520_DARK_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400182}
183
184static ssize_t adp5520_bl_dark_max_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400185 struct device_attribute *attr,
186 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400187{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400188 return adp5520_store(dev, buf, count, ADP5520_DARK_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400189}
190static DEVICE_ATTR(dark_max, 0664, adp5520_bl_dark_max_show,
191 adp5520_bl_dark_max_store);
192
193static ssize_t adp5520_bl_office_max_show(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400194 struct device_attribute *attr, char *buf)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400195{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400196 return adp5520_show(dev, buf, ADP5520_OFFICE_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400197}
198
199static ssize_t adp5520_bl_office_max_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400200 struct device_attribute *attr,
201 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400202{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400203 return adp5520_store(dev, buf, count, ADP5520_OFFICE_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400204}
205static DEVICE_ATTR(office_max, 0664, adp5520_bl_office_max_show,
206 adp5520_bl_office_max_store);
207
208static ssize_t adp5520_bl_daylight_max_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400211 return adp5520_show(dev, buf, ADP5520_DAYLIGHT_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400212}
213
214static ssize_t adp5520_bl_daylight_max_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400215 struct device_attribute *attr,
216 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400217{
218 struct adp5520_bl *data = dev_get_drvdata(dev);
Liu Yuan877947b2011-05-24 17:11:12 -0700219 int ret;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400220
Jingoo Han71d72252012-05-29 15:07:15 -0700221 ret = kstrtoul(buf, 10, &data->cached_daylight_max);
Liu Yuan877947b2011-05-24 17:11:12 -0700222 if (ret < 0)
223 return ret;
224
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400225 return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_MAX);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400226}
227static DEVICE_ATTR(daylight_max, 0664, adp5520_bl_daylight_max_show,
228 adp5520_bl_daylight_max_store);
229
230static ssize_t adp5520_bl_dark_dim_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400233 return adp5520_show(dev, buf, ADP5520_DARK_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400234}
235
236static ssize_t adp5520_bl_dark_dim_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400237 struct device_attribute *attr,
238 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400239{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400240 return adp5520_store(dev, buf, count, ADP5520_DARK_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400241}
242static DEVICE_ATTR(dark_dim, 0664, adp5520_bl_dark_dim_show,
243 adp5520_bl_dark_dim_store);
244
245static ssize_t adp5520_bl_office_dim_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
247{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400248 return adp5520_show(dev, buf, ADP5520_OFFICE_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400249}
250
251static ssize_t adp5520_bl_office_dim_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400252 struct device_attribute *attr,
253 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400254{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400255 return adp5520_store(dev, buf, count, ADP5520_OFFICE_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400256}
257static DEVICE_ATTR(office_dim, 0664, adp5520_bl_office_dim_show,
258 adp5520_bl_office_dim_store);
259
260static ssize_t adp5520_bl_daylight_dim_show(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400261 struct device_attribute *attr, char *buf)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400262{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400263 return adp5520_show(dev, buf, ADP5520_DAYLIGHT_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400264}
265
266static ssize_t adp5520_bl_daylight_dim_store(struct device *dev,
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400267 struct device_attribute *attr,
268 const char *buf, size_t count)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400269{
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400270 return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_DIM);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400271}
272static DEVICE_ATTR(daylight_dim, 0664, adp5520_bl_daylight_dim_show,
273 adp5520_bl_daylight_dim_store);
274
275static struct attribute *adp5520_bl_attributes[] = {
276 &dev_attr_dark_max.attr,
277 &dev_attr_dark_dim.attr,
278 &dev_attr_office_max.attr,
279 &dev_attr_office_dim.attr,
280 &dev_attr_daylight_max.attr,
281 &dev_attr_daylight_dim.attr,
282 NULL
283};
284
285static const struct attribute_group adp5520_bl_attr_group = {
286 .attrs = adp5520_bl_attributes,
287};
288
Bill Pemberton1b9e4502012-11-19 13:21:46 -0500289static int adp5520_bl_probe(struct platform_device *pdev)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400290{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500291 struct backlight_properties props;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400292 struct backlight_device *bl;
293 struct adp5520_bl *data;
294 int ret = 0;
295
Julia Lawall050ea482012-03-23 15:02:01 -0700296 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400297 if (data == NULL)
298 return -ENOMEM;
299
300 data->master = pdev->dev.parent;
Jingoo Hanc512794c2013-11-12 15:09:04 -0800301 data->pdata = dev_get_platdata(&pdev->dev);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400302
303 if (data->pdata == NULL) {
304 dev_err(&pdev->dev, "missing platform data\n");
Michael Hennericha7998ce2009-09-17 14:29:52 -0400305 return -ENODEV;
306 }
307
308 data->id = pdev->id;
309 data->current_brightness = 0;
310
311 mutex_init(&data->lock);
312
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500313 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700314 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500315 props.max_brightness = ADP5020_MAX_BRIGHTNESS;
Jingoo Han5f652c72013-11-12 15:09:07 -0800316 bl = devm_backlight_device_register(&pdev->dev, pdev->name,
317 data->master, data, &adp5520_bl_ops,
318 &props);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400319 if (IS_ERR(bl)) {
320 dev_err(&pdev->dev, "failed to register backlight\n");
Michael Hennericha7998ce2009-09-17 14:29:52 -0400321 return PTR_ERR(bl);
322 }
323
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500324 bl->props.brightness = ADP5020_MAX_BRIGHTNESS;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400325 if (data->pdata->en_ambl_sens)
326 ret = sysfs_create_group(&bl->dev.kobj,
327 &adp5520_bl_attr_group);
328
329 if (ret) {
330 dev_err(&pdev->dev, "failed to register sysfs\n");
Jingoo Han5f652c72013-11-12 15:09:07 -0800331 return ret;
Michael Hennericha7998ce2009-09-17 14:29:52 -0400332 }
333
334 platform_set_drvdata(pdev, bl);
335 ret |= adp5520_bl_setup(bl);
336 backlight_update_status(bl);
337
338 return ret;
339}
340
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500341static int adp5520_bl_remove(struct platform_device *pdev)
Michael Hennericha7998ce2009-09-17 14:29:52 -0400342{
343 struct backlight_device *bl = platform_get_drvdata(pdev);
344 struct adp5520_bl *data = bl_get_data(bl);
345
Michael Hennerichc24b6b62009-10-10 13:54:04 -0400346 adp5520_clr_bits(data->master, ADP5520_MODE_STATUS, ADP5520_BL_EN);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400347
348 if (data->pdata->en_ambl_sens)
349 sysfs_remove_group(&bl->dev.kobj,
350 &adp5520_bl_attr_group);
351
Michael Hennericha7998ce2009-09-17 14:29:52 -0400352 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",
Jingoo Han14c1b772013-04-29 16:17:38 -0700378 .pm = &adp5520_bl_pm_ops,
Michael Hennericha7998ce2009-09-17 14:29:52 -0400379 },
380 .probe = adp5520_bl_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500381 .remove = adp5520_bl_remove,
Michael Hennericha7998ce2009-09-17 14:29:52 -0400382};
383
Axel Lin81178e02012-01-10 15:09:11 -0800384module_platform_driver(adp5520_bl_driver);
Michael Hennericha7998ce2009-09-17 14:29:52 -0400385
386MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
387MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
388MODULE_LICENSE("GPL");
389MODULE_ALIAS("platform:adp5520-backlight");