]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/video/backlight/adp5520_bl.c
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[mirror_ubuntu-bionic-kernel.git] / drivers / video / backlight / adp5520_bl.c
CommitLineData
a7998cec
MH
1/*
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>
5a0e3ad6 15#include <linux/slab.h>
355b200b 16#include <linux/module.h>
a7998cec
MH
17
18struct adp5520_bl {
19 struct device *master;
c24b6b6a 20 struct adp5520_backlight_platform_data *pdata;
a7998cec
MH
21 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 */
c24b6b6a
MH
36 ret |= adp5520_clr_bits(master, ADP5520_BL_CONTROL,
37 ADP5520_BL_AUTO_ADJ);
38 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
39 brightness);
a7998cec
MH
40 } else {
41 /*
42 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
43 * restore daylight l3 sysfs brightness
44 */
c24b6b6a 45 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
a7998cec 46 data->cached_daylight_max);
c24b6b6a
MH
47 ret |= adp5520_set_bits(master, ADP5520_BL_CONTROL,
48 ADP5520_BL_AUTO_ADJ);
a7998cec
MH
49 }
50 } else {
c24b6b6a 51 ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, brightness);
a7998cec
MH
52 }
53
54 if (data->current_brightness && brightness == 0)
55 ret |= adp5520_set_bits(master,
c24b6b6a 56 ADP5520_MODE_STATUS, ADP5520_DIM_EN);
a7998cec
MH
57 else if (data->current_brightness == 0 && brightness)
58 ret |= adp5520_clr_bits(master,
c24b6b6a 59 ADP5520_MODE_STATUS, ADP5520_DIM_EN);
a7998cec
MH
60
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;
f9bda39d 70
a7998cec
MH
71 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
c24b6b6a 86 error = adp5520_read(data->master, ADP5520_BL_VALUE, &reg_val);
a7998cec
MH
87
88 return error ? data->current_brightness : reg_val;
89}
90
9905a43b 91static const struct backlight_ops adp5520_bl_ops = {
a7998cec
MH
92 .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;
c24b6b6a 100 struct adp5520_backlight_platform_data *pdata = data->pdata;
a7998cec
MH
101 int ret = 0;
102
c24b6b6a
MH
103 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);
a7998cec
MH
107
108 if (pdata->en_ambl_sens) {
109 data->cached_daylight_max = pdata->l1_daylight_max;
c24b6b6a
MH
110 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));
a7998cec
MH
129 }
130
c24b6b6a
MH
131 ret |= adp5520_write(master, ADP5520_BL_CONTROL,
132 BL_CTRL_VAL(pdata->fade_led_law,
133 pdata->en_ambl_sens));
a7998cec 134
c24b6b6a 135 ret |= adp5520_write(master, ADP5520_BL_FADE, FADE_VAL(pdata->fade_in,
a7998cec
MH
136 pdata->fade_out));
137
c24b6b6a
MH
138 ret |= adp5520_set_bits(master, ADP5520_MODE_STATUS,
139 ADP5520_BL_EN | ADP5520_DIM_EN);
a7998cec
MH
140
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);
359177e0 147 int ret;
a7998cec
MH
148 uint8_t reg_val;
149
150 mutex_lock(&data->lock);
359177e0 151 ret = adp5520_read(data->master, reg, &reg_val);
a7998cec
MH
152 mutex_unlock(&data->lock);
153
359177e0
DN
154 if (ret < 0)
155 return ret;
156
a7998cec
MH
157 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
71d7225c 167 ret = kstrtoul(buf, 10, &val);
a7998cec
MH
168 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,
c24b6b6a 179 struct device_attribute *attr, char *buf)
a7998cec 180{
c24b6b6a 181 return adp5520_show(dev, buf, ADP5520_DARK_MAX);
a7998cec
MH
182}
183
184static ssize_t adp5520_bl_dark_max_store(struct device *dev,
c24b6b6a
MH
185 struct device_attribute *attr,
186 const char *buf, size_t count)
a7998cec 187{
c24b6b6a 188 return adp5520_store(dev, buf, count, ADP5520_DARK_MAX);
a7998cec
MH
189}
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,
c24b6b6a 194 struct device_attribute *attr, char *buf)
a7998cec 195{
c24b6b6a 196 return adp5520_show(dev, buf, ADP5520_OFFICE_MAX);
a7998cec
MH
197}
198
199static ssize_t adp5520_bl_office_max_store(struct device *dev,
c24b6b6a
MH
200 struct device_attribute *attr,
201 const char *buf, size_t count)
a7998cec 202{
c24b6b6a 203 return adp5520_store(dev, buf, count, ADP5520_OFFICE_MAX);
a7998cec
MH
204}
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{
c24b6b6a 211 return adp5520_show(dev, buf, ADP5520_DAYLIGHT_MAX);
a7998cec
MH
212}
213
214static ssize_t adp5520_bl_daylight_max_store(struct device *dev,
c24b6b6a
MH
215 struct device_attribute *attr,
216 const char *buf, size_t count)
a7998cec
MH
217{
218 struct adp5520_bl *data = dev_get_drvdata(dev);
877947bc
LY
219 int ret;
220
71d7225c 221 ret = kstrtoul(buf, 10, &data->cached_daylight_max);
877947bc
LY
222 if (ret < 0)
223 return ret;
a7998cec 224
c24b6b6a 225 return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_MAX);
a7998cec
MH
226}
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{
c24b6b6a 233 return adp5520_show(dev, buf, ADP5520_DARK_DIM);
a7998cec
MH
234}
235
236static ssize_t adp5520_bl_dark_dim_store(struct device *dev,
c24b6b6a
MH
237 struct device_attribute *attr,
238 const char *buf, size_t count)
a7998cec 239{
c24b6b6a 240 return adp5520_store(dev, buf, count, ADP5520_DARK_DIM);
a7998cec
MH
241}
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{
c24b6b6a 248 return adp5520_show(dev, buf, ADP5520_OFFICE_DIM);
a7998cec
MH
249}
250
251static ssize_t adp5520_bl_office_dim_store(struct device *dev,
c24b6b6a
MH
252 struct device_attribute *attr,
253 const char *buf, size_t count)
a7998cec 254{
c24b6b6a 255 return adp5520_store(dev, buf, count, ADP5520_OFFICE_DIM);
a7998cec
MH
256}
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,
c24b6b6a 261 struct device_attribute *attr, char *buf)
a7998cec 262{
c24b6b6a 263 return adp5520_show(dev, buf, ADP5520_DAYLIGHT_DIM);
a7998cec
MH
264}
265
266static ssize_t adp5520_bl_daylight_dim_store(struct device *dev,
c24b6b6a
MH
267 struct device_attribute *attr,
268 const char *buf, size_t count)
a7998cec 269{
c24b6b6a 270 return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_DIM);
a7998cec
MH
271}
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
1b9e450d 289static int adp5520_bl_probe(struct platform_device *pdev)
a7998cec 290{
a19a6ee6 291 struct backlight_properties props;
a7998cec
MH
292 struct backlight_device *bl;
293 struct adp5520_bl *data;
294 int ret = 0;
295
050ea48b 296 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
a7998cec
MH
297 if (data == NULL)
298 return -ENOMEM;
299
300 data->master = pdev->dev.parent;
c512794c 301 data->pdata = dev_get_platdata(&pdev->dev);
a7998cec
MH
302
303 if (data->pdata == NULL) {
304 dev_err(&pdev->dev, "missing platform data\n");
a7998cec
MH
305 return -ENODEV;
306 }
307
308 data->id = pdev->id;
309 data->current_brightness = 0;
310
311 mutex_init(&data->lock);
312
a19a6ee6 313 memset(&props, 0, sizeof(struct backlight_properties));
bb7ca747 314 props.type = BACKLIGHT_RAW;
a19a6ee6 315 props.max_brightness = ADP5020_MAX_BRIGHTNESS;
5f652c71
JH
316 bl = devm_backlight_device_register(&pdev->dev, pdev->name,
317 data->master, data, &adp5520_bl_ops,
318 &props);
a7998cec
MH
319 if (IS_ERR(bl)) {
320 dev_err(&pdev->dev, "failed to register backlight\n");
a7998cec
MH
321 return PTR_ERR(bl);
322 }
323
a19a6ee6 324 bl->props.brightness = ADP5020_MAX_BRIGHTNESS;
a7998cec
MH
325 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");
5f652c71 331 return ret;
a7998cec
MH
332 }
333
334 platform_set_drvdata(pdev, bl);
0eb3fba8
AK
335 ret = adp5520_bl_setup(bl);
336 if (ret) {
337 dev_err(&pdev->dev, "failed to setup\n");
338 if (data->pdata->en_ambl_sens)
339 sysfs_remove_group(&bl->dev.kobj,
340 &adp5520_bl_attr_group);
341 return ret;
342 }
343
a7998cec
MH
344 backlight_update_status(bl);
345
0eb3fba8 346 return 0;
a7998cec
MH
347}
348
7e4b9d0b 349static int adp5520_bl_remove(struct platform_device *pdev)
a7998cec
MH
350{
351 struct backlight_device *bl = platform_get_drvdata(pdev);
352 struct adp5520_bl *data = bl_get_data(bl);
353
c24b6b6a 354 adp5520_clr_bits(data->master, ADP5520_MODE_STATUS, ADP5520_BL_EN);
a7998cec
MH
355
356 if (data->pdata->en_ambl_sens)
357 sysfs_remove_group(&bl->dev.kobj,
358 &adp5520_bl_attr_group);
359
a7998cec
MH
360 return 0;
361}
362
14c1b778
JH
363#ifdef CONFIG_PM_SLEEP
364static int adp5520_bl_suspend(struct device *dev)
a7998cec 365{
14c1b778
JH
366 struct backlight_device *bl = dev_get_drvdata(dev);
367
a7998cec
MH
368 return adp5520_bl_set(bl, 0);
369}
370
14c1b778 371static int adp5520_bl_resume(struct device *dev)
a7998cec 372{
14c1b778 373 struct backlight_device *bl = dev_get_drvdata(dev);
a7998cec
MH
374
375 backlight_update_status(bl);
376 return 0;
377}
a7998cec
MH
378#endif
379
14c1b778
JH
380static SIMPLE_DEV_PM_OPS(adp5520_bl_pm_ops, adp5520_bl_suspend,
381 adp5520_bl_resume);
382
a7998cec
MH
383static struct platform_driver adp5520_bl_driver = {
384 .driver = {
385 .name = "adp5520-backlight",
14c1b778 386 .pm = &adp5520_bl_pm_ops,
a7998cec
MH
387 },
388 .probe = adp5520_bl_probe,
d1723fa2 389 .remove = adp5520_bl_remove,
a7998cec
MH
390};
391
81178e02 392module_platform_driver(adp5520_bl_driver);
a7998cec
MH
393
394MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
395MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
396MODULE_LICENSE("GPL");
397MODULE_ALIAS("platform:adp5520-backlight");