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