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