PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / video / backlight / adp8860_bl.c
blob9d656717d0f784cef243630d559ab93d674c45a8
1 /*
2 * Backlight driver for Analog Devices ADP8860 Backlight Devices
4 * Copyright 2009-2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
21 #include <linux/i2c/adp8860.h>
22 #define ADP8860_EXT_FEATURES
23 #define ADP8860_USE_LEDS
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 */
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 */
67 #define ADP8860_DEVID(x) ((x) & 0xF)
68 #define ADP8860_MANID(x) ((x) >> 4)
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)
79 /* ADP8860_CCFG Main ALS comparator level enable */
80 #define L3_EN (1 << 1)
81 #define L2_EN (1 << 0)
83 #define CFGR_BLV_SHIFT 3
84 #define CFGR_BLV_MASK 0x3
85 #define ADP8860_FLAG_LED_MASK 0xFF
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)
91 enum {
92 adp8860,
93 adp8861,
94 adp8863
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;
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;
120 static int adp8860_read(struct i2c_client *client, int reg, uint8_t *val)
122 int ret;
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;
130 *val = (uint8_t)ret;
131 return 0;
134 static int adp8860_write(struct i2c_client *client, u8 reg, u8 val)
136 return i2c_smbus_write_byte_data(client, reg, val);
139 static int adp8860_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
141 struct adp8860_bl *data = i2c_get_clientdata(client);
142 uint8_t reg_val;
143 int ret;
145 mutex_lock(&data->lock);
147 ret = adp8860_read(client, reg, &reg_val);
149 if (!ret && ((reg_val & bit_mask) != bit_mask)) {
150 reg_val |= bit_mask;
151 ret = adp8860_write(client, reg, reg_val);
154 mutex_unlock(&data->lock);
155 return ret;
158 static int adp8860_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
160 struct adp8860_bl *data = i2c_get_clientdata(client);
161 uint8_t reg_val;
162 int ret;
164 mutex_lock(&data->lock);
166 ret = adp8860_read(client, reg, &reg_val);
168 if (!ret && (reg_val & bit_mask)) {
169 reg_val &= ~bit_mask;
170 ret = adp8860_write(client, reg, reg_val);
173 mutex_unlock(&data->lock);
174 return ret;
178 * Independent sink / LED
180 #if defined(ADP8860_USE_LEDS)
181 static void adp8860_led_work(struct work_struct *work)
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);
188 static void adp8860_led_set(struct led_classdev *led_cdev,
189 enum led_brightness value)
191 struct adp8860_led *led;
193 led = container_of(led_cdev, struct adp8860_led, cdev);
194 led->new_brightness = value;
195 schedule_work(&led->work);
198 static int adp8860_led_setup(struct adp8860_led *led)
200 struct i2c_client *client = led->client;
201 int ret = 0;
203 ret = adp8860_write(client, ADP8860_ISC1 - led->id + 1, 0);
204 ret |= adp8860_set_bits(client, ADP8860_ISCC, 1 << (led->id - 1));
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));
213 return ret;
216 static int adp8860_led_probe(struct i2c_client *client)
218 struct adp8860_backlight_platform_data *pdata =
219 dev_get_platdata(&client->dev);
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;
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;
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));
238 if (ret) {
239 dev_err(&client->dev, "failed to write\n");
240 return ret;
243 for (i = 0; i < pdata->num_leds; ++i) {
244 cur_led = &pdata->leds[i];
245 led_dat = &led[i];
247 led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
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 ret = -EINVAL;
253 goto err;
256 if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
257 dev_err(&client->dev, "LED %d used by Backlight\n",
258 led_dat->id);
259 ret = -EBUSY;
260 goto err;
263 led_dat->cdev.name = cur_led->name;
264 led_dat->cdev.default_trigger = cur_led->default_trigger;
265 led_dat->cdev.brightness_set = adp8860_led_set;
266 led_dat->cdev.brightness = LED_OFF;
267 led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
268 led_dat->client = client;
269 led_dat->new_brightness = LED_OFF;
270 INIT_WORK(&led_dat->work, adp8860_led_work);
272 ret = led_classdev_register(&client->dev, &led_dat->cdev);
273 if (ret) {
274 dev_err(&client->dev, "failed to register LED %d\n",
275 led_dat->id);
276 goto err;
279 ret = adp8860_led_setup(led_dat);
280 if (ret) {
281 dev_err(&client->dev, "failed to write\n");
282 i++;
283 goto err;
287 data->led = led;
289 return 0;
291 err:
292 for (i = i - 1; i >= 0; --i) {
293 led_classdev_unregister(&led[i].cdev);
294 cancel_work_sync(&led[i].work);
297 return ret;
300 static int adp8860_led_remove(struct i2c_client *client)
302 struct adp8860_backlight_platform_data *pdata =
303 dev_get_platdata(&client->dev);
304 struct adp8860_bl *data = i2c_get_clientdata(client);
305 int i;
307 for (i = 0; i < pdata->num_leds; i++) {
308 led_classdev_unregister(&data->led[i].cdev);
309 cancel_work_sync(&data->led[i].work);
312 return 0;
314 #else
315 static int adp8860_led_probe(struct i2c_client *client)
317 return 0;
320 static int adp8860_led_remove(struct i2c_client *client)
322 return 0;
324 #endif
326 static int adp8860_bl_set(struct backlight_device *bl, int brightness)
328 struct adp8860_bl *data = bl_get_data(bl);
329 struct i2c_client *client = data->client;
330 int ret = 0;
332 if (data->en_ambl_sens) {
333 if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
334 /* Disable Ambient Light auto adjust */
335 ret |= adp8860_clr_bits(client, ADP8860_MDCR,
336 CMP_AUTOEN);
337 ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
338 } else {
340 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
341 * restore daylight l1 sysfs brightness
343 ret |= adp8860_write(client, ADP8860_BLMX1,
344 data->cached_daylight_max);
345 ret |= adp8860_set_bits(client, ADP8860_MDCR,
346 CMP_AUTOEN);
348 } else
349 ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
351 if (data->current_brightness && brightness == 0)
352 ret |= adp8860_set_bits(client,
353 ADP8860_MDCR, DIM_EN);
354 else if (data->current_brightness == 0 && brightness)
355 ret |= adp8860_clr_bits(client,
356 ADP8860_MDCR, DIM_EN);
358 if (!ret)
359 data->current_brightness = brightness;
361 return ret;
364 static int adp8860_bl_update_status(struct backlight_device *bl)
366 int brightness = bl->props.brightness;
367 if (bl->props.power != FB_BLANK_UNBLANK)
368 brightness = 0;
370 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
371 brightness = 0;
373 return adp8860_bl_set(bl, brightness);
376 static int adp8860_bl_get_brightness(struct backlight_device *bl)
378 struct adp8860_bl *data = bl_get_data(bl);
380 return data->current_brightness;
383 static const struct backlight_ops adp8860_bl_ops = {
384 .update_status = adp8860_bl_update_status,
385 .get_brightness = adp8860_bl_get_brightness,
388 static int adp8860_bl_setup(struct backlight_device *bl)
390 struct adp8860_bl *data = bl_get_data(bl);
391 struct i2c_client *client = data->client;
392 struct adp8860_backlight_platform_data *pdata = data->pdata;
393 int ret = 0;
395 ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
396 ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
397 ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
399 if (data->en_ambl_sens) {
400 data->cached_daylight_max = pdata->l1_daylight_max;
401 ret |= adp8860_write(client, ADP8860_BLMX2,
402 pdata->l2_office_max);
403 ret |= adp8860_write(client, ADP8860_BLDM2,
404 pdata->l2_office_dim);
405 ret |= adp8860_write(client, ADP8860_BLMX3,
406 pdata->l3_dark_max);
407 ret |= adp8860_write(client, ADP8860_BLDM3,
408 pdata->l3_dark_dim);
410 ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
411 ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
412 ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
413 ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
414 ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
415 ALS_CCFG_VAL(pdata->abml_filt));
418 ret |= adp8860_write(client, ADP8860_CFGR,
419 BL_CFGR_VAL(pdata->bl_fade_law, 0));
421 ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
422 pdata->bl_fade_out));
424 ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
425 (data->gdwn_dis ? GDWN_DIS : 0));
427 return ret;
430 static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
432 struct adp8860_bl *data = dev_get_drvdata(dev);
433 int error;
434 uint8_t reg_val;
436 mutex_lock(&data->lock);
437 error = adp8860_read(data->client, reg, &reg_val);
438 mutex_unlock(&data->lock);
440 if (error < 0)
441 return error;
443 return sprintf(buf, "%u\n", reg_val);
446 static ssize_t adp8860_store(struct device *dev, const char *buf,
447 size_t count, int reg)
449 struct adp8860_bl *data = dev_get_drvdata(dev);
450 unsigned long val;
451 int ret;
453 ret = kstrtoul(buf, 10, &val);
454 if (ret)
455 return ret;
457 mutex_lock(&data->lock);
458 adp8860_write(data->client, reg, val);
459 mutex_unlock(&data->lock);
461 return count;
464 static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
467 return adp8860_show(dev, buf, ADP8860_BLMX3);
470 static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
471 struct device_attribute *attr, const char *buf, size_t count)
473 return adp8860_store(dev, buf, count, ADP8860_BLMX3);
476 static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
477 adp8860_bl_l3_dark_max_store);
479 static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 return adp8860_show(dev, buf, ADP8860_BLMX2);
485 static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
486 struct device_attribute *attr, const char *buf, size_t count)
488 return adp8860_store(dev, buf, count, ADP8860_BLMX2);
490 static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
491 adp8860_bl_l2_office_max_store);
493 static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
496 return adp8860_show(dev, buf, ADP8860_BLMX1);
499 static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
500 struct device_attribute *attr, const char *buf, size_t count)
502 struct adp8860_bl *data = dev_get_drvdata(dev);
503 int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
504 if (ret)
505 return ret;
507 return adp8860_store(dev, buf, count, ADP8860_BLMX1);
509 static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
510 adp8860_bl_l1_daylight_max_store);
512 static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
515 return adp8860_show(dev, buf, ADP8860_BLDM3);
518 static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
522 return adp8860_store(dev, buf, count, ADP8860_BLDM3);
524 static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
525 adp8860_bl_l3_dark_dim_store);
527 static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
530 return adp8860_show(dev, buf, ADP8860_BLDM2);
533 static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
537 return adp8860_store(dev, buf, count, ADP8860_BLDM2);
539 static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
540 adp8860_bl_l2_office_dim_store);
542 static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
543 struct device_attribute *attr, char *buf)
545 return adp8860_show(dev, buf, ADP8860_BLDM1);
548 static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
549 struct device_attribute *attr,
550 const char *buf, size_t count)
552 return adp8860_store(dev, buf, count, ADP8860_BLDM1);
554 static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
555 adp8860_bl_l1_daylight_dim_store);
557 #ifdef ADP8860_EXT_FEATURES
558 static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
561 struct adp8860_bl *data = dev_get_drvdata(dev);
562 int error;
563 uint8_t reg_val;
564 uint16_t ret_val;
566 mutex_lock(&data->lock);
567 error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
568 ret_val = reg_val;
569 error |= adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
570 mutex_unlock(&data->lock);
572 if (error < 0)
573 return error;
575 /* Return 13-bit conversion value for the first light sensor */
576 ret_val += (reg_val & 0x1F) << 8;
578 return sprintf(buf, "%u\n", ret_val);
580 static DEVICE_ATTR(ambient_light_level, 0444,
581 adp8860_bl_ambient_light_level_show, NULL);
583 static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct adp8860_bl *data = dev_get_drvdata(dev);
587 int error;
588 uint8_t reg_val;
590 mutex_lock(&data->lock);
591 error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
592 mutex_unlock(&data->lock);
594 if (error < 0)
595 return error;
597 return sprintf(buf, "%u\n",
598 ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
601 static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
602 struct device_attribute *attr,
603 const char *buf, size_t count)
605 struct adp8860_bl *data = dev_get_drvdata(dev);
606 unsigned long val;
607 uint8_t reg_val;
608 int ret;
610 ret = kstrtoul(buf, 10, &val);
611 if (ret)
612 return ret;
614 if (val == 0) {
615 /* Enable automatic ambient light sensing */
616 adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
617 } else if ((val > 0) && (val <= 3)) {
618 /* Disable automatic ambient light sensing */
619 adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
621 /* Set user supplied ambient light zone */
622 mutex_lock(&data->lock);
623 adp8860_read(data->client, ADP8860_CFGR, &reg_val);
624 reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
625 reg_val |= (val - 1) << CFGR_BLV_SHIFT;
626 adp8860_write(data->client, ADP8860_CFGR, reg_val);
627 mutex_unlock(&data->lock);
630 return count;
632 static DEVICE_ATTR(ambient_light_zone, 0664,
633 adp8860_bl_ambient_light_zone_show,
634 adp8860_bl_ambient_light_zone_store);
635 #endif
637 static struct attribute *adp8860_bl_attributes[] = {
638 &dev_attr_l3_dark_max.attr,
639 &dev_attr_l3_dark_dim.attr,
640 &dev_attr_l2_office_max.attr,
641 &dev_attr_l2_office_dim.attr,
642 &dev_attr_l1_daylight_max.attr,
643 &dev_attr_l1_daylight_dim.attr,
644 #ifdef ADP8860_EXT_FEATURES
645 &dev_attr_ambient_light_level.attr,
646 &dev_attr_ambient_light_zone.attr,
647 #endif
648 NULL
651 static const struct attribute_group adp8860_bl_attr_group = {
652 .attrs = adp8860_bl_attributes,
655 static int adp8860_probe(struct i2c_client *client,
656 const struct i2c_device_id *id)
658 struct backlight_device *bl;
659 struct adp8860_bl *data;
660 struct adp8860_backlight_platform_data *pdata =
661 dev_get_platdata(&client->dev);
662 struct backlight_properties props;
663 uint8_t reg_val;
664 int ret;
666 if (!i2c_check_functionality(client->adapter,
667 I2C_FUNC_SMBUS_BYTE_DATA)) {
668 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
669 return -EIO;
672 if (!pdata) {
673 dev_err(&client->dev, "no platform data?\n");
674 return -EINVAL;
677 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
678 if (data == NULL)
679 return -ENOMEM;
681 ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
682 if (ret < 0)
683 return ret;
685 switch (ADP8860_MANID(reg_val)) {
686 case ADP8863_MANUFID:
687 data->gdwn_dis = !!pdata->gdwn_dis;
688 case ADP8860_MANUFID:
689 data->en_ambl_sens = !!pdata->en_ambl_sens;
690 break;
691 case ADP8861_MANUFID:
692 data->gdwn_dis = !!pdata->gdwn_dis;
693 break;
694 default:
695 dev_err(&client->dev, "failed to probe\n");
696 return -ENODEV;
699 /* It's confirmed that the DEVID field is actually a REVID */
701 data->revid = ADP8860_DEVID(reg_val);
702 data->client = client;
703 data->pdata = pdata;
704 data->id = id->driver_data;
705 data->current_brightness = 0;
706 i2c_set_clientdata(client, data);
708 memset(&props, 0, sizeof(props));
709 props.type = BACKLIGHT_RAW;
710 props.max_brightness = ADP8860_MAX_BRIGHTNESS;
712 mutex_init(&data->lock);
714 bl = devm_backlight_device_register(&client->dev,
715 dev_driver_string(&client->dev),
716 &client->dev, data, &adp8860_bl_ops, &props);
717 if (IS_ERR(bl)) {
718 dev_err(&client->dev, "failed to register backlight\n");
719 return PTR_ERR(bl);
722 bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
724 data->bl = bl;
726 if (data->en_ambl_sens)
727 ret = sysfs_create_group(&bl->dev.kobj,
728 &adp8860_bl_attr_group);
730 if (ret) {
731 dev_err(&client->dev, "failed to register sysfs\n");
732 return ret;
735 ret = adp8860_bl_setup(bl);
736 if (ret) {
737 ret = -EIO;
738 goto out;
741 backlight_update_status(bl);
743 dev_info(&client->dev, "%s Rev.%d Backlight\n",
744 client->name, data->revid);
746 if (pdata->num_leds)
747 adp8860_led_probe(client);
749 return 0;
751 out:
752 if (data->en_ambl_sens)
753 sysfs_remove_group(&data->bl->dev.kobj,
754 &adp8860_bl_attr_group);
756 return ret;
759 static int adp8860_remove(struct i2c_client *client)
761 struct adp8860_bl *data = i2c_get_clientdata(client);
763 adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
765 if (data->led)
766 adp8860_led_remove(client);
768 if (data->en_ambl_sens)
769 sysfs_remove_group(&data->bl->dev.kobj,
770 &adp8860_bl_attr_group);
772 return 0;
775 #ifdef CONFIG_PM_SLEEP
776 static int adp8860_i2c_suspend(struct device *dev)
778 struct i2c_client *client = to_i2c_client(dev);
780 adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
782 return 0;
785 static int adp8860_i2c_resume(struct device *dev)
787 struct i2c_client *client = to_i2c_client(dev);
789 adp8860_set_bits(client, ADP8860_MDCR, NSTBY | BLEN);
791 return 0;
793 #endif
795 static SIMPLE_DEV_PM_OPS(adp8860_i2c_pm_ops, adp8860_i2c_suspend,
796 adp8860_i2c_resume);
798 static const struct i2c_device_id adp8860_id[] = {
799 { "adp8860", adp8860 },
800 { "adp8861", adp8861 },
801 { "adp8863", adp8863 },
804 MODULE_DEVICE_TABLE(i2c, adp8860_id);
806 static struct i2c_driver adp8860_driver = {
807 .driver = {
808 .name = KBUILD_MODNAME,
809 .pm = &adp8860_i2c_pm_ops,
811 .probe = adp8860_probe,
812 .remove = adp8860_remove,
813 .id_table = adp8860_id,
816 module_i2c_driver(adp8860_driver);
818 MODULE_LICENSE("GPL v2");
819 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
820 MODULE_DESCRIPTION("ADP8860 Backlight driver");
821 MODULE_ALIAS("i2c:adp8860-backlight");