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