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