]> Pileus Git - ~andy/linux/blob - drivers/video/backlight/adp8860_bl.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[~andy/linux] / drivers / video / backlight / adp8860_bl.c
1 /*
2  * Backlight driver for Analog Devices ADP8860 Backlight Devices
3  *
4  * Copyright 2009-2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/pm.h>
13 #include <linux/platform_device.h>
14 #include <linux/i2c.h>
15 #include <linux/fb.h>
16 #include <linux/backlight.h>
17 #include <linux/leds.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20
21 #include <linux/i2c/adp8860.h>
22 #define ADP8860_EXT_FEATURES
23 #define ADP8860_USE_LEDS
24
25 #define ADP8860_MFDVID 0x00 /* Manufacturer and device ID */
26 #define ADP8860_MDCR 0x01 /* Device mode and status */
27 #define ADP8860_MDCR2 0x02 /* Device mode and Status Register 2 */
28 #define ADP8860_INTR_EN 0x03 /* Interrupts enable */
29 #define ADP8860_CFGR 0x04 /* Configuration register */
30 #define ADP8860_BLSEN 0x05 /* Sink enable backlight or independent */
31 #define ADP8860_BLOFF 0x06 /* Backlight off timeout */
32 #define ADP8860_BLDIM 0x07 /* Backlight dim timeout */
33 #define ADP8860_BLFR 0x08 /* Backlight fade in and out rates */
34 #define ADP8860_BLMX1 0x09 /* Backlight (Brightness Level 1-daylight) maximum current */
35 #define ADP8860_BLDM1 0x0A /* Backlight (Brightness Level 1-daylight) dim current */
36 #define ADP8860_BLMX2 0x0B /* Backlight (Brightness Level 2-office) maximum current */
37 #define ADP8860_BLDM2 0x0C /* Backlight (Brightness Level 2-office) dim current */
38 #define ADP8860_BLMX3 0x0D /* Backlight (Brightness Level 3-dark) maximum current */
39 #define ADP8860_BLDM3 0x0E /* Backlight (Brightness Level 3-dark) dim current */
40 #define ADP8860_ISCFR 0x0F /* Independent sink current fade control register */
41 #define ADP8860_ISCC 0x10 /* Independent sink current control register */
42 #define ADP8860_ISCT1 0x11 /* Independent Sink Current Timer Register LED[7:5] */
43 #define ADP8860_ISCT2 0x12 /* Independent Sink Current Timer Register LED[4:1] */
44 #define ADP8860_ISCF 0x13 /* Independent sink current fade register */
45 #define ADP8860_ISC7 0x14 /* Independent Sink Current LED7 */
46 #define ADP8860_ISC6 0x15 /* Independent Sink Current LED6 */
47 #define ADP8860_ISC5 0x16 /* Independent Sink Current LED5 */
48 #define ADP8860_ISC4 0x17 /* Independent Sink Current LED4 */
49 #define ADP8860_ISC3 0x18 /* Independent Sink Current LED3 */
50 #define ADP8860_ISC2 0x19 /* Independent Sink Current LED2 */
51 #define ADP8860_ISC1 0x1A /* Independent Sink Current LED1 */
52 #define ADP8860_CCFG 0x1B /* Comparator configuration */
53 #define ADP8860_CCFG2 0x1C /* Second comparator configuration */
54 #define ADP8860_L2_TRP 0x1D /* L2 comparator reference */
55 #define ADP8860_L2_HYS 0x1E /* L2 hysteresis */
56 #define ADP8860_L3_TRP 0x1F /* L3 comparator reference */
57 #define ADP8860_L3_HYS 0x20 /* L3 hysteresis */
58 #define ADP8860_PH1LEVL 0x21 /* First phototransistor ambient light level-low byte register */
59 #define ADP8860_PH1LEVH 0x22 /* First phototransistor ambient light level-high byte register */
60 #define ADP8860_PH2LEVL 0x23 /* Second phototransistor ambient light level-low byte register */
61 #define ADP8860_PH2LEVH 0x24 /* Second phototransistor ambient light level-high byte register */
62
63 #define ADP8860_MANUFID         0x0  /* Analog Devices ADP8860 Manufacturer ID */
64 #define ADP8861_MANUFID         0x4  /* Analog Devices ADP8861 Manufacturer ID */
65 #define ADP8863_MANUFID         0x2  /* Analog Devices ADP8863 Manufacturer ID */
66
67 #define ADP8860_DEVID(x)        ((x) & 0xF)
68 #define ADP8860_MANID(x)        ((x) >> 4)
69
70 /* MDCR Device mode and status */
71 #define INT_CFG                 (1 << 6)
72 #define NSTBY                   (1 << 5)
73 #define DIM_EN                  (1 << 4)
74 #define GDWN_DIS                (1 << 3)
75 #define SIS_EN                  (1 << 2)
76 #define CMP_AUTOEN              (1 << 1)
77 #define BLEN                    (1 << 0)
78
79 /* ADP8860_CCFG Main ALS comparator level enable */
80 #define L3_EN                   (1 << 1)
81 #define L2_EN                   (1 << 0)
82
83 #define CFGR_BLV_SHIFT          3
84 #define CFGR_BLV_MASK           0x3
85 #define ADP8860_FLAG_LED_MASK   0xFF
86
87 #define FADE_VAL(in, out)       ((0xF & (in)) | ((0xF & (out)) << 4))
88 #define BL_CFGR_VAL(law, blv)   ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
89 #define ALS_CCFG_VAL(filt)      ((0x7 & filt) << 5)
90
91 enum {
92         adp8860,
93         adp8861,
94         adp8863
95 };
96
97 struct adp8860_led {
98         struct led_classdev     cdev;
99         struct work_struct      work;
100         struct i2c_client       *client;
101         enum led_brightness     new_brightness;
102         int                     id;
103         int                     flags;
104 };
105
106 struct adp8860_bl {
107         struct i2c_client *client;
108         struct backlight_device *bl;
109         struct adp8860_led *led;
110         struct adp8860_backlight_platform_data *pdata;
111         struct mutex lock;
112         unsigned long cached_daylight_max;
113         int id;
114         int revid;
115         int current_brightness;
116         unsigned en_ambl_sens:1;
117         unsigned gdwn_dis:1;
118 };
119
120 static int adp8860_read(struct i2c_client *client, int reg, uint8_t *val)
121 {
122         int ret;
123
124         ret = i2c_smbus_read_byte_data(client, reg);
125         if (ret < 0) {
126                 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
127                 return ret;
128         }
129
130         *val = (uint8_t)ret;
131         return 0;
132 }
133
134 static int adp8860_write(struct i2c_client *client, u8 reg, u8 val)
135 {
136         return i2c_smbus_write_byte_data(client, reg, val);
137 }
138
139 static int adp8860_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
140 {
141         struct adp8860_bl *data = i2c_get_clientdata(client);
142         uint8_t reg_val;
143         int ret;
144
145         mutex_lock(&data->lock);
146
147         ret = adp8860_read(client, reg, &reg_val);
148
149         if (!ret && ((reg_val & bit_mask) != bit_mask)) {
150                 reg_val |= bit_mask;
151                 ret = adp8860_write(client, reg, reg_val);
152         }
153
154         mutex_unlock(&data->lock);
155         return ret;
156 }
157
158 static int adp8860_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
159 {
160         struct adp8860_bl *data = i2c_get_clientdata(client);
161         uint8_t reg_val;
162         int ret;
163
164         mutex_lock(&data->lock);
165
166         ret = adp8860_read(client, reg, &reg_val);
167
168         if (!ret && (reg_val & bit_mask)) {
169                 reg_val &= ~bit_mask;
170                 ret = adp8860_write(client, reg, reg_val);
171         }
172
173         mutex_unlock(&data->lock);
174         return ret;
175 }
176
177 /*
178  * Independent sink / LED
179  */
180 #if defined(ADP8860_USE_LEDS)
181 static void adp8860_led_work(struct work_struct *work)
182 {
183         struct adp8860_led *led = container_of(work, struct adp8860_led, work);
184         adp8860_write(led->client, ADP8860_ISC1 - led->id + 1,
185                          led->new_brightness >> 1);
186 }
187
188 static void adp8860_led_set(struct led_classdev *led_cdev,
189                            enum led_brightness value)
190 {
191         struct adp8860_led *led;
192
193         led = container_of(led_cdev, struct adp8860_led, cdev);
194         led->new_brightness = value;
195         schedule_work(&led->work);
196 }
197
198 static int adp8860_led_setup(struct adp8860_led *led)
199 {
200         struct i2c_client *client = led->client;
201         int ret = 0;
202
203         ret = adp8860_write(client, ADP8860_ISC1 - led->id + 1, 0);
204         ret |= adp8860_set_bits(client, ADP8860_ISCC, 1 << (led->id - 1));
205
206         if (led->id > 4)
207                 ret |= adp8860_set_bits(client, ADP8860_ISCT1,
208                                 (led->flags & 0x3) << ((led->id - 5) * 2));
209         else
210                 ret |= adp8860_set_bits(client, ADP8860_ISCT2,
211                                 (led->flags & 0x3) << ((led->id - 1) * 2));
212
213         return ret;
214 }
215
216 static int __devinit adp8860_led_probe(struct i2c_client *client)
217 {
218         struct adp8860_backlight_platform_data *pdata =
219                 client->dev.platform_data;
220         struct adp8860_bl *data = i2c_get_clientdata(client);
221         struct adp8860_led *led, *led_dat;
222         struct led_info *cur_led;
223         int ret, i;
224
225         led = devm_kzalloc(&client->dev, sizeof(*led) * pdata->num_leds,
226                                 GFP_KERNEL);
227         if (led == NULL) {
228                 dev_err(&client->dev, "failed to alloc memory\n");
229                 return -ENOMEM;
230         }
231
232         ret = adp8860_write(client, ADP8860_ISCFR, pdata->led_fade_law);
233         ret = adp8860_write(client, ADP8860_ISCT1,
234                         (pdata->led_on_time & 0x3) << 6);
235         ret |= adp8860_write(client, ADP8860_ISCF,
236                         FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
237
238         if (ret) {
239                 dev_err(&client->dev, "failed to write\n");
240                 return ret;
241         }
242
243         for (i = 0; i < pdata->num_leds; ++i) {
244                 cur_led = &pdata->leds[i];
245                 led_dat = &led[i];
246
247                 led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
248
249                 if (led_dat->id > 7 || led_dat->id < 1) {
250                         dev_err(&client->dev, "Invalid LED ID %d\n",
251                                 led_dat->id);
252                         goto err;
253                 }
254
255                 if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
256                         dev_err(&client->dev, "LED %d used by Backlight\n",
257                                 led_dat->id);
258                         goto err;
259                 }
260
261                 led_dat->cdev.name = cur_led->name;
262                 led_dat->cdev.default_trigger = cur_led->default_trigger;
263                 led_dat->cdev.brightness_set = adp8860_led_set;
264                 led_dat->cdev.brightness = LED_OFF;
265                 led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
266                 led_dat->client = client;
267                 led_dat->new_brightness = LED_OFF;
268                 INIT_WORK(&led_dat->work, adp8860_led_work);
269
270                 ret = led_classdev_register(&client->dev, &led_dat->cdev);
271                 if (ret) {
272                         dev_err(&client->dev, "failed to register LED %d\n",
273                                 led_dat->id);
274                         goto err;
275                 }
276
277                 ret = adp8860_led_setup(led_dat);
278                 if (ret) {
279                         dev_err(&client->dev, "failed to write\n");
280                         i++;
281                         goto err;
282                 }
283         }
284
285         data->led = led;
286
287         return 0;
288
289  err:
290         for (i = i - 1; i >= 0; --i) {
291                 led_classdev_unregister(&led[i].cdev);
292                 cancel_work_sync(&led[i].work);
293         }
294
295         return ret;
296 }
297
298 static int __devexit adp8860_led_remove(struct i2c_client *client)
299 {
300         struct adp8860_backlight_platform_data *pdata =
301                 client->dev.platform_data;
302         struct adp8860_bl *data = i2c_get_clientdata(client);
303         int i;
304
305         for (i = 0; i < pdata->num_leds; i++) {
306                 led_classdev_unregister(&data->led[i].cdev);
307                 cancel_work_sync(&data->led[i].work);
308         }
309
310         return 0;
311 }
312 #else
313 static int __devinit adp8860_led_probe(struct i2c_client *client)
314 {
315         return 0;
316 }
317
318 static int __devexit adp8860_led_remove(struct i2c_client *client)
319 {
320         return 0;
321 }
322 #endif
323
324 static int adp8860_bl_set(struct backlight_device *bl, int brightness)
325 {
326         struct adp8860_bl *data = bl_get_data(bl);
327         struct i2c_client *client = data->client;
328         int ret = 0;
329
330         if (data->en_ambl_sens) {
331                 if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
332                         /* Disable Ambient Light auto adjust */
333                         ret |= adp8860_clr_bits(client, ADP8860_MDCR,
334                                         CMP_AUTOEN);
335                         ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
336                 } else {
337                         /*
338                          * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
339                          * restore daylight l1 sysfs brightness
340                          */
341                         ret |= adp8860_write(client, ADP8860_BLMX1,
342                                          data->cached_daylight_max);
343                         ret |= adp8860_set_bits(client, ADP8860_MDCR,
344                                          CMP_AUTOEN);
345                 }
346         } else
347                 ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
348
349         if (data->current_brightness && brightness == 0)
350                 ret |= adp8860_set_bits(client,
351                                 ADP8860_MDCR, DIM_EN);
352         else if (data->current_brightness == 0 && brightness)
353                 ret |= adp8860_clr_bits(client,
354                                 ADP8860_MDCR, DIM_EN);
355
356         if (!ret)
357                 data->current_brightness = brightness;
358
359         return ret;
360 }
361
362 static int adp8860_bl_update_status(struct backlight_device *bl)
363 {
364         int brightness = bl->props.brightness;
365         if (bl->props.power != FB_BLANK_UNBLANK)
366                 brightness = 0;
367
368         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
369                 brightness = 0;
370
371         return adp8860_bl_set(bl, brightness);
372 }
373
374 static int adp8860_bl_get_brightness(struct backlight_device *bl)
375 {
376         struct adp8860_bl *data = bl_get_data(bl);
377
378         return data->current_brightness;
379 }
380
381 static const struct backlight_ops adp8860_bl_ops = {
382         .update_status  = adp8860_bl_update_status,
383         .get_brightness = adp8860_bl_get_brightness,
384 };
385
386 static int adp8860_bl_setup(struct backlight_device *bl)
387 {
388         struct adp8860_bl *data = bl_get_data(bl);
389         struct i2c_client *client = data->client;
390         struct adp8860_backlight_platform_data *pdata = data->pdata;
391         int ret = 0;
392
393         ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
394         ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
395         ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
396
397         if (data->en_ambl_sens) {
398                 data->cached_daylight_max = pdata->l1_daylight_max;
399                 ret |= adp8860_write(client, ADP8860_BLMX2,
400                                                 pdata->l2_office_max);
401                 ret |= adp8860_write(client, ADP8860_BLDM2,
402                                                 pdata->l2_office_dim);
403                 ret |= adp8860_write(client, ADP8860_BLMX3,
404                                                 pdata->l3_dark_max);
405                 ret |= adp8860_write(client, ADP8860_BLDM3,
406                                                 pdata->l3_dark_dim);
407
408                 ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
409                 ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
410                 ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
411                 ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
412                 ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
413                                                 ALS_CCFG_VAL(pdata->abml_filt));
414         }
415
416         ret |= adp8860_write(client, ADP8860_CFGR,
417                         BL_CFGR_VAL(pdata->bl_fade_law, 0));
418
419         ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
420                         pdata->bl_fade_out));
421
422         ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
423                         (data->gdwn_dis ? GDWN_DIS : 0));
424
425         return ret;
426 }
427
428 static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
429 {
430         struct adp8860_bl *data = dev_get_drvdata(dev);
431         int error;
432         uint8_t reg_val;
433
434         mutex_lock(&data->lock);
435         error = adp8860_read(data->client, reg, &reg_val);
436         mutex_unlock(&data->lock);
437
438         if (error < 0)
439                 return error;
440
441         return sprintf(buf, "%u\n", reg_val);
442 }
443
444 static ssize_t adp8860_store(struct device *dev, const char *buf,
445                          size_t count, int reg)
446 {
447         struct adp8860_bl *data = dev_get_drvdata(dev);
448         unsigned long val;
449         int ret;
450
451         ret = kstrtoul(buf, 10, &val);
452         if (ret)
453                 return ret;
454
455         mutex_lock(&data->lock);
456         adp8860_write(data->client, reg, val);
457         mutex_unlock(&data->lock);
458
459         return count;
460 }
461
462 static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
463                                      struct device_attribute *attr, char *buf)
464 {
465         return adp8860_show(dev, buf, ADP8860_BLMX3);
466 }
467
468 static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
469                 struct device_attribute *attr, const char *buf, size_t count)
470 {
471         return adp8860_store(dev, buf, count, ADP8860_BLMX3);
472 }
473
474 static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
475                         adp8860_bl_l3_dark_max_store);
476
477 static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
478                 struct device_attribute *attr, char *buf)
479 {
480         return adp8860_show(dev, buf, ADP8860_BLMX2);
481 }
482
483 static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
484                 struct device_attribute *attr, const char *buf, size_t count)
485 {
486         return adp8860_store(dev, buf, count, ADP8860_BLMX2);
487 }
488 static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
489                         adp8860_bl_l2_office_max_store);
490
491 static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
492                         struct device_attribute *attr, char *buf)
493 {
494         return adp8860_show(dev, buf, ADP8860_BLMX1);
495 }
496
497 static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
498                 struct device_attribute *attr, const char *buf, size_t count)
499 {
500         struct adp8860_bl *data = dev_get_drvdata(dev);
501         int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
502         if (ret)
503                 return ret;
504
505         return adp8860_store(dev, buf, count, ADP8860_BLMX1);
506 }
507 static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
508                         adp8860_bl_l1_daylight_max_store);
509
510 static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
511                         struct device_attribute *attr, char *buf)
512 {
513         return adp8860_show(dev, buf, ADP8860_BLDM3);
514 }
515
516 static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
517                                      struct device_attribute *attr,
518                                      const char *buf, size_t count)
519 {
520         return adp8860_store(dev, buf, count, ADP8860_BLDM3);
521 }
522 static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
523                         adp8860_bl_l3_dark_dim_store);
524
525 static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
526                         struct device_attribute *attr, char *buf)
527 {
528         return adp8860_show(dev, buf, ADP8860_BLDM2);
529 }
530
531 static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
532                                      struct device_attribute *attr,
533                                      const char *buf, size_t count)
534 {
535         return adp8860_store(dev, buf, count, ADP8860_BLDM2);
536 }
537 static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
538                         adp8860_bl_l2_office_dim_store);
539
540 static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
541                                      struct device_attribute *attr, char *buf)
542 {
543         return adp8860_show(dev, buf, ADP8860_BLDM1);
544 }
545
546 static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
547                                      struct device_attribute *attr,
548                                      const char *buf, size_t count)
549 {
550         return adp8860_store(dev, buf, count, ADP8860_BLDM1);
551 }
552 static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
553                         adp8860_bl_l1_daylight_dim_store);
554
555 #ifdef ADP8860_EXT_FEATURES
556 static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
557                                      struct device_attribute *attr, char *buf)
558 {
559         struct adp8860_bl *data = dev_get_drvdata(dev);
560         int error;
561         uint8_t reg_val;
562         uint16_t ret_val;
563
564         mutex_lock(&data->lock);
565         error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
566         ret_val = reg_val;
567         error |= adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
568         mutex_unlock(&data->lock);
569
570         if (error < 0)
571                 return error;
572
573         /* Return 13-bit conversion value for the first light sensor */
574         ret_val += (reg_val & 0x1F) << 8;
575
576         return sprintf(buf, "%u\n", ret_val);
577 }
578 static DEVICE_ATTR(ambient_light_level, 0444,
579                 adp8860_bl_ambient_light_level_show, NULL);
580
581 static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
582                                      struct device_attribute *attr, char *buf)
583 {
584         struct adp8860_bl *data = dev_get_drvdata(dev);
585         int error;
586         uint8_t reg_val;
587
588         mutex_lock(&data->lock);
589         error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
590         mutex_unlock(&data->lock);
591
592         if (error < 0)
593                 return error;
594
595         return sprintf(buf, "%u\n",
596                 ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
597 }
598
599 static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
600                                      struct device_attribute *attr,
601                                      const char *buf, size_t count)
602 {
603         struct adp8860_bl *data = dev_get_drvdata(dev);
604         unsigned long val;
605         uint8_t reg_val;
606         int ret;
607
608         ret = kstrtoul(buf, 10, &val);
609         if (ret)
610                 return ret;
611
612         if (val == 0) {
613                 /* Enable automatic ambient light sensing */
614                 adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
615         } else if ((val > 0) && (val <= 3)) {
616                 /* Disable automatic ambient light sensing */
617                 adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
618
619                 /* Set user supplied ambient light zone */
620                 mutex_lock(&data->lock);
621                 adp8860_read(data->client, ADP8860_CFGR, &reg_val);
622                 reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
623                 reg_val |= (val - 1) << CFGR_BLV_SHIFT;
624                 adp8860_write(data->client, ADP8860_CFGR, reg_val);
625                 mutex_unlock(&data->lock);
626         }
627
628         return count;
629 }
630 static DEVICE_ATTR(ambient_light_zone, 0664,
631                 adp8860_bl_ambient_light_zone_show,
632                 adp8860_bl_ambient_light_zone_store);
633 #endif
634
635 static struct attribute *adp8860_bl_attributes[] = {
636         &dev_attr_l3_dark_max.attr,
637         &dev_attr_l3_dark_dim.attr,
638         &dev_attr_l2_office_max.attr,
639         &dev_attr_l2_office_dim.attr,
640         &dev_attr_l1_daylight_max.attr,
641         &dev_attr_l1_daylight_dim.attr,
642 #ifdef ADP8860_EXT_FEATURES
643         &dev_attr_ambient_light_level.attr,
644         &dev_attr_ambient_light_zone.attr,
645 #endif
646         NULL
647 };
648
649 static const struct attribute_group adp8860_bl_attr_group = {
650         .attrs = adp8860_bl_attributes,
651 };
652
653 static int __devinit adp8860_probe(struct i2c_client *client,
654                                         const struct i2c_device_id *id)
655 {
656         struct backlight_device *bl;
657         struct adp8860_bl *data;
658         struct adp8860_backlight_platform_data *pdata =
659                 client->dev.platform_data;
660         struct backlight_properties props;
661         uint8_t reg_val;
662         int ret;
663
664         if (!i2c_check_functionality(client->adapter,
665                                         I2C_FUNC_SMBUS_BYTE_DATA)) {
666                 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
667                 return -EIO;
668         }
669
670         if (!pdata) {
671                 dev_err(&client->dev, "no platform data?\n");
672                 return -EINVAL;
673         }
674
675         data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
676         if (data == NULL)
677                 return -ENOMEM;
678
679         ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
680         if (ret < 0)
681                 return ret;
682
683         switch (ADP8860_MANID(reg_val)) {
684         case ADP8863_MANUFID:
685                 data->gdwn_dis = !!pdata->gdwn_dis;
686         case ADP8860_MANUFID:
687                 data->en_ambl_sens = !!pdata->en_ambl_sens;
688                 break;
689         case ADP8861_MANUFID:
690                 data->gdwn_dis = !!pdata->gdwn_dis;
691                 break;
692         default:
693                 dev_err(&client->dev, "failed to probe\n");
694                 return -ENODEV;
695         }
696
697         /* It's confirmed that the DEVID field is actually a REVID */
698
699         data->revid = ADP8860_DEVID(reg_val);
700         data->client = client;
701         data->pdata = pdata;
702         data->id = id->driver_data;
703         data->current_brightness = 0;
704         i2c_set_clientdata(client, data);
705
706         memset(&props, 0, sizeof(props));
707         props.type = BACKLIGHT_RAW;
708         props.max_brightness = ADP8860_MAX_BRIGHTNESS;
709
710         mutex_init(&data->lock);
711
712         bl = backlight_device_register(dev_driver_string(&client->dev),
713                         &client->dev, data, &adp8860_bl_ops, &props);
714         if (IS_ERR(bl)) {
715                 dev_err(&client->dev, "failed to register backlight\n");
716                 return PTR_ERR(bl);
717         }
718
719         bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
720
721         data->bl = bl;
722
723         if (data->en_ambl_sens)
724                 ret = sysfs_create_group(&bl->dev.kobj,
725                         &adp8860_bl_attr_group);
726
727         if (ret) {
728                 dev_err(&client->dev, "failed to register sysfs\n");
729                 goto out1;
730         }
731
732         ret = adp8860_bl_setup(bl);
733         if (ret) {
734                 ret = -EIO;
735                 goto out;
736         }
737
738         backlight_update_status(bl);
739
740         dev_info(&client->dev, "%s Rev.%d Backlight\n",
741                 client->name, data->revid);
742
743         if (pdata->num_leds)
744                 adp8860_led_probe(client);
745
746         return 0;
747
748 out:
749         if (data->en_ambl_sens)
750                 sysfs_remove_group(&data->bl->dev.kobj,
751                         &adp8860_bl_attr_group);
752 out1:
753         backlight_device_unregister(bl);
754
755         return ret;
756 }
757
758 static int __devexit adp8860_remove(struct i2c_client *client)
759 {
760         struct adp8860_bl *data = i2c_get_clientdata(client);
761
762         adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
763
764         if (data->led)
765                 adp8860_led_remove(client);
766
767         if (data->en_ambl_sens)
768                 sysfs_remove_group(&data->bl->dev.kobj,
769                         &adp8860_bl_attr_group);
770
771         backlight_device_unregister(data->bl);
772
773         return 0;
774 }
775
776 #ifdef CONFIG_PM
777 static int adp8860_i2c_suspend(struct i2c_client *client, pm_message_t message)
778 {
779         adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
780
781         return 0;
782 }
783
784 static int adp8860_i2c_resume(struct i2c_client *client)
785 {
786         adp8860_set_bits(client, ADP8860_MDCR, NSTBY);
787
788         return 0;
789 }
790 #else
791 #define adp8860_i2c_suspend NULL
792 #define adp8860_i2c_resume NULL
793 #endif
794
795 static const struct i2c_device_id adp8860_id[] = {
796         { "adp8860", adp8860 },
797         { "adp8861", adp8861 },
798         { "adp8863", adp8863 },
799         { }
800 };
801 MODULE_DEVICE_TABLE(i2c, adp8860_id);
802
803 static struct i2c_driver adp8860_driver = {
804         .driver = {
805                 .name = KBUILD_MODNAME,
806         },
807         .probe    = adp8860_probe,
808         .remove   = __devexit_p(adp8860_remove),
809         .suspend = adp8860_i2c_suspend,
810         .resume  = adp8860_i2c_resume,
811         .id_table = adp8860_id,
812 };
813
814 module_i2c_driver(adp8860_driver);
815
816 MODULE_LICENSE("GPL v2");
817 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
818 MODULE_DESCRIPTION("ADP8860 Backlight driver");
819 MODULE_ALIAS("i2c:adp8860-backlight");