Commit | Line | Data |
---|---|---|
a59ec1e7 MH |
1 | /* |
2 | * Backlight driver for Analog Devices ADP8870 Backlight Devices | |
3 | * | |
4 | * Copyright 2009-2011 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
8 | ||
9 | #include <linux/module.h> | |
a59ec1e7 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> | |
18 | #include <linux/workqueue.h> | |
19 | #include <linux/slab.h> | |
20 | ||
21 | #include <linux/i2c/adp8870.h> | |
22 | #define ADP8870_EXT_FEATURES | |
23 | #define ADP8870_USE_LEDS | |
24 | ||
25 | ||
26 | #define ADP8870_MFDVID 0x00 /* Manufacturer and device ID */ | |
27 | #define ADP8870_MDCR 0x01 /* Device mode and status */ | |
28 | #define ADP8870_INT_STAT 0x02 /* Interrupts status */ | |
29 | #define ADP8870_INT_EN 0x03 /* Interrupts enable */ | |
30 | #define ADP8870_CFGR 0x04 /* Configuration register */ | |
31 | #define ADP8870_BLSEL 0x05 /* Sink enable backlight or independent */ | |
32 | #define ADP8870_PWMLED 0x06 /* PWM Enable Selection Register */ | |
33 | #define ADP8870_BLOFF 0x07 /* Backlight off timeout */ | |
34 | #define ADP8870_BLDIM 0x08 /* Backlight dim timeout */ | |
35 | #define ADP8870_BLFR 0x09 /* Backlight fade in and out rates */ | |
36 | #define ADP8870_BLMX1 0x0A /* Backlight (Brightness Level 1-daylight) maximum current */ | |
37 | #define ADP8870_BLDM1 0x0B /* Backlight (Brightness Level 1-daylight) dim current */ | |
38 | #define ADP8870_BLMX2 0x0C /* Backlight (Brightness Level 2-bright) maximum current */ | |
39 | #define ADP8870_BLDM2 0x0D /* Backlight (Brightness Level 2-bright) dim current */ | |
40 | #define ADP8870_BLMX3 0x0E /* Backlight (Brightness Level 3-office) maximum current */ | |
41 | #define ADP8870_BLDM3 0x0F /* Backlight (Brightness Level 3-office) dim current */ | |
42 | #define ADP8870_BLMX4 0x10 /* Backlight (Brightness Level 4-indoor) maximum current */ | |
43 | #define ADP8870_BLDM4 0x11 /* Backlight (Brightness Level 4-indoor) dim current */ | |
44 | #define ADP8870_BLMX5 0x12 /* Backlight (Brightness Level 5-dark) maximum current */ | |
45 | #define ADP8870_BLDM5 0x13 /* Backlight (Brightness Level 5-dark) dim current */ | |
46 | #define ADP8870_ISCLAW 0x1A /* Independent sink current fade law register */ | |
47 | #define ADP8870_ISCC 0x1B /* Independent sink current control register */ | |
48 | #define ADP8870_ISCT1 0x1C /* Independent Sink Current Timer Register LED[7:5] */ | |
49 | #define ADP8870_ISCT2 0x1D /* Independent Sink Current Timer Register LED[4:1] */ | |
50 | #define ADP8870_ISCF 0x1E /* Independent sink current fade register */ | |
51 | #define ADP8870_ISC1 0x1F /* Independent Sink Current LED1 */ | |
52 | #define ADP8870_ISC2 0x20 /* Independent Sink Current LED2 */ | |
53 | #define ADP8870_ISC3 0x21 /* Independent Sink Current LED3 */ | |
54 | #define ADP8870_ISC4 0x22 /* Independent Sink Current LED4 */ | |
55 | #define ADP8870_ISC5 0x23 /* Independent Sink Current LED5 */ | |
56 | #define ADP8870_ISC6 0x24 /* Independent Sink Current LED6 */ | |
57 | #define ADP8870_ISC7 0x25 /* Independent Sink Current LED7 (Brightness Level 1-daylight) */ | |
58 | #define ADP8870_ISC7_L2 0x26 /* Independent Sink Current LED7 (Brightness Level 2-bright) */ | |
59 | #define ADP8870_ISC7_L3 0x27 /* Independent Sink Current LED7 (Brightness Level 3-office) */ | |
60 | #define ADP8870_ISC7_L4 0x28 /* Independent Sink Current LED7 (Brightness Level 4-indoor) */ | |
61 | #define ADP8870_ISC7_L5 0x29 /* Independent Sink Current LED7 (Brightness Level 5-dark) */ | |
62 | #define ADP8870_CMP_CTL 0x2D /* ALS Comparator Control Register */ | |
63 | #define ADP8870_ALS1_EN 0x2E /* Main ALS comparator level enable */ | |
64 | #define ADP8870_ALS2_EN 0x2F /* Second ALS comparator level enable */ | |
65 | #define ADP8870_ALS1_STAT 0x30 /* Main ALS Comparator Status Register */ | |
66 | #define ADP8870_ALS2_STAT 0x31 /* Second ALS Comparator Status Register */ | |
67 | #define ADP8870_L2TRP 0x32 /* L2 comparator reference */ | |
68 | #define ADP8870_L2HYS 0x33 /* L2 hysteresis */ | |
69 | #define ADP8870_L3TRP 0x34 /* L3 comparator reference */ | |
70 | #define ADP8870_L3HYS 0x35 /* L3 hysteresis */ | |
71 | #define ADP8870_L4TRP 0x36 /* L4 comparator reference */ | |
72 | #define ADP8870_L4HYS 0x37 /* L4 hysteresis */ | |
73 | #define ADP8870_L5TRP 0x38 /* L5 comparator reference */ | |
74 | #define ADP8870_L5HYS 0x39 /* L5 hysteresis */ | |
75 | #define ADP8870_PH1LEVL 0x40 /* First phototransistor ambient light level-low byte register */ | |
76 | #define ADP8870_PH1LEVH 0x41 /* First phototransistor ambient light level-high byte register */ | |
77 | #define ADP8870_PH2LEVL 0x42 /* Second phototransistor ambient light level-low byte register */ | |
78 | #define ADP8870_PH2LEVH 0x43 /* Second phototransistor ambient light level-high byte register */ | |
79 | ||
80 | #define ADP8870_MANUFID 0x3 /* Analog Devices AD8870 Manufacturer and device ID */ | |
81 | #define ADP8870_DEVID(x) ((x) & 0xF) | |
82 | #define ADP8870_MANID(x) ((x) >> 4) | |
83 | ||
84 | /* MDCR Device mode and status */ | |
85 | #define D7ALSEN (1 << 7) | |
86 | #define INT_CFG (1 << 6) | |
87 | #define NSTBY (1 << 5) | |
88 | #define DIM_EN (1 << 4) | |
89 | #define GDWN_DIS (1 << 3) | |
90 | #define SIS_EN (1 << 2) | |
91 | #define CMP_AUTOEN (1 << 1) | |
92 | #define BLEN (1 << 0) | |
93 | ||
94 | /* ADP8870_ALS1_EN Main ALS comparator level enable */ | |
95 | #define L5_EN (1 << 3) | |
96 | #define L4_EN (1 << 2) | |
97 | #define L3_EN (1 << 1) | |
98 | #define L2_EN (1 << 0) | |
99 | ||
100 | #define CFGR_BLV_SHIFT 3 | |
101 | #define CFGR_BLV_MASK 0x7 | |
102 | #define ADP8870_FLAG_LED_MASK 0xFF | |
103 | ||
104 | #define FADE_VAL(in, out) ((0xF & (in)) | ((0xF & (out)) << 4)) | |
105 | #define BL_CFGR_VAL(law, blv) ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1)) | |
106 | #define ALS_CMPR_CFG_VAL(filt) ((0x7 & (filt)) << 1) | |
107 | ||
108 | struct adp8870_bl { | |
109 | struct i2c_client *client; | |
110 | struct backlight_device *bl; | |
111 | struct adp8870_led *led; | |
112 | struct adp8870_backlight_platform_data *pdata; | |
113 | struct mutex lock; | |
114 | unsigned long cached_daylight_max; | |
115 | int id; | |
116 | int revid; | |
117 | int current_brightness; | |
118 | }; | |
119 | ||
120 | struct adp8870_led { | |
121 | struct led_classdev cdev; | |
122 | struct work_struct work; | |
123 | struct i2c_client *client; | |
124 | enum led_brightness new_brightness; | |
125 | int id; | |
126 | int flags; | |
127 | }; | |
128 | ||
129 | static int adp8870_read(struct i2c_client *client, int reg, uint8_t *val) | |
130 | { | |
131 | int ret; | |
132 | ||
133 | ret = i2c_smbus_read_byte_data(client, reg); | |
134 | if (ret < 0) { | |
135 | dev_err(&client->dev, "failed reading at 0x%02x\n", reg); | |
136 | return ret; | |
137 | } | |
138 | ||
139 | *val = ret; | |
140 | return 0; | |
141 | } | |
142 | ||
143 | ||
144 | static int adp8870_write(struct i2c_client *client, u8 reg, u8 val) | |
145 | { | |
146 | int ret = i2c_smbus_write_byte_data(client, reg, val); | |
147 | if (ret) | |
148 | dev_err(&client->dev, "failed to write\n"); | |
149 | ||
150 | return ret; | |
151 | } | |
152 | ||
153 | static int adp8870_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask) | |
154 | { | |
155 | struct adp8870_bl *data = i2c_get_clientdata(client); | |
156 | uint8_t reg_val; | |
157 | int ret; | |
158 | ||
159 | mutex_lock(&data->lock); | |
160 | ||
161 | ret = adp8870_read(client, reg, ®_val); | |
162 | ||
36c3e759 | 163 | if (!ret && ((reg_val & bit_mask) != bit_mask)) { |
a59ec1e7 MH |
164 | reg_val |= bit_mask; |
165 | ret = adp8870_write(client, reg, reg_val); | |
166 | } | |
167 | ||
168 | mutex_unlock(&data->lock); | |
169 | return ret; | |
170 | } | |
171 | ||
172 | static int adp8870_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask) | |
173 | { | |
174 | struct adp8870_bl *data = i2c_get_clientdata(client); | |
175 | uint8_t reg_val; | |
176 | int ret; | |
177 | ||
178 | mutex_lock(&data->lock); | |
179 | ||
180 | ret = adp8870_read(client, reg, ®_val); | |
181 | ||
182 | if (!ret && (reg_val & bit_mask)) { | |
183 | reg_val &= ~bit_mask; | |
184 | ret = adp8870_write(client, reg, reg_val); | |
185 | } | |
186 | ||
187 | mutex_unlock(&data->lock); | |
188 | return ret; | |
189 | } | |
190 | ||
191 | /* | |
192 | * Independent sink / LED | |
193 | */ | |
194 | #if defined(ADP8870_USE_LEDS) | |
195 | static void adp8870_led_work(struct work_struct *work) | |
196 | { | |
197 | struct adp8870_led *led = container_of(work, struct adp8870_led, work); | |
198 | adp8870_write(led->client, ADP8870_ISC1 + led->id - 1, | |
199 | led->new_brightness >> 1); | |
200 | } | |
201 | ||
202 | static void adp8870_led_set(struct led_classdev *led_cdev, | |
203 | enum led_brightness value) | |
204 | { | |
205 | struct adp8870_led *led; | |
206 | ||
207 | led = container_of(led_cdev, struct adp8870_led, cdev); | |
208 | led->new_brightness = value; | |
209 | /* | |
210 | * Use workqueue for IO since I2C operations can sleep. | |
211 | */ | |
212 | schedule_work(&led->work); | |
213 | } | |
214 | ||
215 | static int adp8870_led_setup(struct adp8870_led *led) | |
216 | { | |
217 | struct i2c_client *client = led->client; | |
218 | int ret = 0; | |
219 | ||
220 | ret = adp8870_write(client, ADP8870_ISC1 + led->id - 1, 0); | |
221 | if (ret) | |
222 | return ret; | |
223 | ||
224 | ret = adp8870_set_bits(client, ADP8870_ISCC, 1 << (led->id - 1)); | |
225 | if (ret) | |
226 | return ret; | |
227 | ||
228 | if (led->id > 4) | |
229 | ret = adp8870_set_bits(client, ADP8870_ISCT1, | |
230 | (led->flags & 0x3) << ((led->id - 5) * 2)); | |
231 | else | |
232 | ret = adp8870_set_bits(client, ADP8870_ISCT2, | |
233 | (led->flags & 0x3) << ((led->id - 1) * 2)); | |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
1b9e450d | 238 | static int adp8870_led_probe(struct i2c_client *client) |
a59ec1e7 MH |
239 | { |
240 | struct adp8870_backlight_platform_data *pdata = | |
241 | client->dev.platform_data; | |
242 | struct adp8870_bl *data = i2c_get_clientdata(client); | |
243 | struct adp8870_led *led, *led_dat; | |
244 | struct led_info *cur_led; | |
245 | int ret, i; | |
246 | ||
91cdb239 JH |
247 | led = devm_kzalloc(&client->dev, pdata->num_leds * sizeof(*led), |
248 | GFP_KERNEL); | |
a59ec1e7 MH |
249 | if (led == NULL) { |
250 | dev_err(&client->dev, "failed to alloc memory\n"); | |
251 | return -ENOMEM; | |
252 | } | |
253 | ||
254 | ret = adp8870_write(client, ADP8870_ISCLAW, pdata->led_fade_law); | |
255 | if (ret) | |
91cdb239 | 256 | return ret; |
a59ec1e7 MH |
257 | |
258 | ret = adp8870_write(client, ADP8870_ISCT1, | |
259 | (pdata->led_on_time & 0x3) << 6); | |
260 | if (ret) | |
91cdb239 | 261 | return ret; |
a59ec1e7 MH |
262 | |
263 | ret = adp8870_write(client, ADP8870_ISCF, | |
264 | FADE_VAL(pdata->led_fade_in, pdata->led_fade_out)); | |
265 | if (ret) | |
91cdb239 | 266 | return ret; |
a59ec1e7 MH |
267 | |
268 | for (i = 0; i < pdata->num_leds; ++i) { | |
269 | cur_led = &pdata->leds[i]; | |
270 | led_dat = &led[i]; | |
271 | ||
272 | led_dat->id = cur_led->flags & ADP8870_FLAG_LED_MASK; | |
273 | ||
274 | if (led_dat->id > 7 || led_dat->id < 1) { | |
275 | dev_err(&client->dev, "Invalid LED ID %d\n", | |
276 | led_dat->id); | |
514f920a | 277 | ret = -EINVAL; |
a59ec1e7 MH |
278 | goto err; |
279 | } | |
280 | ||
281 | if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) { | |
282 | dev_err(&client->dev, "LED %d used by Backlight\n", | |
283 | led_dat->id); | |
514f920a | 284 | ret = -EBUSY; |
a59ec1e7 MH |
285 | goto err; |
286 | } | |
287 | ||
288 | led_dat->cdev.name = cur_led->name; | |
289 | led_dat->cdev.default_trigger = cur_led->default_trigger; | |
290 | led_dat->cdev.brightness_set = adp8870_led_set; | |
291 | led_dat->cdev.brightness = LED_OFF; | |
292 | led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT; | |
293 | led_dat->client = client; | |
294 | led_dat->new_brightness = LED_OFF; | |
295 | INIT_WORK(&led_dat->work, adp8870_led_work); | |
296 | ||
297 | ret = led_classdev_register(&client->dev, &led_dat->cdev); | |
298 | if (ret) { | |
299 | dev_err(&client->dev, "failed to register LED %d\n", | |
300 | led_dat->id); | |
301 | goto err; | |
302 | } | |
303 | ||
304 | ret = adp8870_led_setup(led_dat); | |
305 | if (ret) { | |
306 | dev_err(&client->dev, "failed to write\n"); | |
307 | i++; | |
308 | goto err; | |
309 | } | |
310 | } | |
311 | ||
312 | data->led = led; | |
313 | ||
314 | return 0; | |
315 | ||
316 | err: | |
317 | for (i = i - 1; i >= 0; --i) { | |
318 | led_classdev_unregister(&led[i].cdev); | |
319 | cancel_work_sync(&led[i].work); | |
320 | } | |
321 | ||
a59ec1e7 MH |
322 | return ret; |
323 | } | |
324 | ||
7e4b9d0b | 325 | static int adp8870_led_remove(struct i2c_client *client) |
a59ec1e7 MH |
326 | { |
327 | struct adp8870_backlight_platform_data *pdata = | |
328 | client->dev.platform_data; | |
329 | struct adp8870_bl *data = i2c_get_clientdata(client); | |
330 | int i; | |
331 | ||
332 | for (i = 0; i < pdata->num_leds; i++) { | |
333 | led_classdev_unregister(&data->led[i].cdev); | |
334 | cancel_work_sync(&data->led[i].work); | |
335 | } | |
336 | ||
a59ec1e7 MH |
337 | return 0; |
338 | } | |
339 | #else | |
1b9e450d | 340 | static int adp8870_led_probe(struct i2c_client *client) |
a59ec1e7 MH |
341 | { |
342 | return 0; | |
343 | } | |
344 | ||
7e4b9d0b | 345 | static int adp8870_led_remove(struct i2c_client *client) |
a59ec1e7 MH |
346 | { |
347 | return 0; | |
348 | } | |
349 | #endif | |
350 | ||
351 | static int adp8870_bl_set(struct backlight_device *bl, int brightness) | |
352 | { | |
353 | struct adp8870_bl *data = bl_get_data(bl); | |
354 | struct i2c_client *client = data->client; | |
355 | int ret = 0; | |
356 | ||
357 | if (data->pdata->en_ambl_sens) { | |
358 | if ((brightness > 0) && (brightness < ADP8870_MAX_BRIGHTNESS)) { | |
359 | /* Disable Ambient Light auto adjust */ | |
360 | ret = adp8870_clr_bits(client, ADP8870_MDCR, | |
361 | CMP_AUTOEN); | |
362 | if (ret) | |
363 | return ret; | |
364 | ret = adp8870_write(client, ADP8870_BLMX1, brightness); | |
365 | if (ret) | |
366 | return ret; | |
367 | } else { | |
368 | /* | |
369 | * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust | |
370 | * restore daylight l1 sysfs brightness | |
371 | */ | |
372 | ret = adp8870_write(client, ADP8870_BLMX1, | |
373 | data->cached_daylight_max); | |
374 | if (ret) | |
375 | return ret; | |
376 | ||
377 | ret = adp8870_set_bits(client, ADP8870_MDCR, | |
378 | CMP_AUTOEN); | |
379 | if (ret) | |
380 | return ret; | |
381 | } | |
382 | } else { | |
383 | ret = adp8870_write(client, ADP8870_BLMX1, brightness); | |
384 | if (ret) | |
385 | return ret; | |
386 | } | |
387 | ||
388 | if (data->current_brightness && brightness == 0) | |
389 | ret = adp8870_set_bits(client, | |
390 | ADP8870_MDCR, DIM_EN); | |
391 | else if (data->current_brightness == 0 && brightness) | |
392 | ret = adp8870_clr_bits(client, | |
393 | ADP8870_MDCR, DIM_EN); | |
394 | ||
395 | if (!ret) | |
396 | data->current_brightness = brightness; | |
397 | ||
398 | return ret; | |
399 | } | |
400 | ||
401 | static int adp8870_bl_update_status(struct backlight_device *bl) | |
402 | { | |
403 | int brightness = bl->props.brightness; | |
404 | if (bl->props.power != FB_BLANK_UNBLANK) | |
405 | brightness = 0; | |
406 | ||
407 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) | |
408 | brightness = 0; | |
409 | ||
410 | return adp8870_bl_set(bl, brightness); | |
411 | } | |
412 | ||
413 | static int adp8870_bl_get_brightness(struct backlight_device *bl) | |
414 | { | |
415 | struct adp8870_bl *data = bl_get_data(bl); | |
416 | ||
417 | return data->current_brightness; | |
418 | } | |
419 | ||
420 | static const struct backlight_ops adp8870_bl_ops = { | |
421 | .update_status = adp8870_bl_update_status, | |
422 | .get_brightness = adp8870_bl_get_brightness, | |
423 | }; | |
424 | ||
425 | static int adp8870_bl_setup(struct backlight_device *bl) | |
426 | { | |
427 | struct adp8870_bl *data = bl_get_data(bl); | |
428 | struct i2c_client *client = data->client; | |
429 | struct adp8870_backlight_platform_data *pdata = data->pdata; | |
430 | int ret = 0; | |
431 | ||
432 | ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign); | |
433 | if (ret) | |
434 | return ret; | |
435 | ||
436 | ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign); | |
437 | if (ret) | |
438 | return ret; | |
439 | ||
440 | ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max); | |
441 | if (ret) | |
442 | return ret; | |
443 | ||
444 | ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim); | |
445 | if (ret) | |
446 | return ret; | |
447 | ||
448 | if (pdata->en_ambl_sens) { | |
449 | data->cached_daylight_max = pdata->l1_daylight_max; | |
450 | ret = adp8870_write(client, ADP8870_BLMX2, | |
451 | pdata->l2_bright_max); | |
452 | if (ret) | |
453 | return ret; | |
454 | ret = adp8870_write(client, ADP8870_BLDM2, | |
455 | pdata->l2_bright_dim); | |
456 | if (ret) | |
457 | return ret; | |
458 | ||
459 | ret = adp8870_write(client, ADP8870_BLMX3, | |
460 | pdata->l3_office_max); | |
461 | if (ret) | |
462 | return ret; | |
463 | ret = adp8870_write(client, ADP8870_BLDM3, | |
464 | pdata->l3_office_dim); | |
465 | if (ret) | |
466 | return ret; | |
467 | ||
468 | ret = adp8870_write(client, ADP8870_BLMX4, | |
469 | pdata->l4_indoor_max); | |
470 | if (ret) | |
471 | return ret; | |
472 | ||
473 | ret = adp8870_write(client, ADP8870_BLDM4, | |
474 | pdata->l4_indor_dim); | |
475 | if (ret) | |
476 | return ret; | |
477 | ||
478 | ret = adp8870_write(client, ADP8870_BLMX5, | |
479 | pdata->l5_dark_max); | |
480 | if (ret) | |
481 | return ret; | |
482 | ||
483 | ret = adp8870_write(client, ADP8870_BLDM5, | |
484 | pdata->l5_dark_dim); | |
485 | if (ret) | |
486 | return ret; | |
487 | ||
488 | ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip); | |
489 | if (ret) | |
490 | return ret; | |
491 | ||
492 | ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst); | |
493 | if (ret) | |
494 | return ret; | |
495 | ||
496 | ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip); | |
497 | if (ret) | |
498 | return ret; | |
499 | ||
500 | ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst); | |
501 | if (ret) | |
502 | return ret; | |
503 | ||
504 | ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip); | |
505 | if (ret) | |
506 | return ret; | |
507 | ||
508 | ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst); | |
509 | if (ret) | |
510 | return ret; | |
511 | ||
512 | ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip); | |
513 | if (ret) | |
514 | return ret; | |
515 | ||
516 | ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst); | |
517 | if (ret) | |
518 | return ret; | |
519 | ||
520 | ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN | | |
521 | L3_EN | L2_EN); | |
522 | if (ret) | |
523 | return ret; | |
524 | ||
525 | ret = adp8870_write(client, ADP8870_CMP_CTL, | |
526 | ALS_CMPR_CFG_VAL(pdata->abml_filt)); | |
527 | if (ret) | |
528 | return ret; | |
529 | } | |
530 | ||
531 | ret = adp8870_write(client, ADP8870_CFGR, | |
532 | BL_CFGR_VAL(pdata->bl_fade_law, 0)); | |
533 | if (ret) | |
534 | return ret; | |
535 | ||
536 | ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in, | |
537 | pdata->bl_fade_out)); | |
538 | if (ret) | |
539 | return ret; | |
540 | /* | |
541 | * ADP8870 Rev0 requires GDWN_DIS bit set | |
542 | */ | |
543 | ||
544 | ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY | | |
545 | (data->revid == 0 ? GDWN_DIS : 0)); | |
546 | ||
547 | return ret; | |
548 | } | |
549 | ||
550 | static ssize_t adp8870_show(struct device *dev, char *buf, int reg) | |
551 | { | |
552 | struct adp8870_bl *data = dev_get_drvdata(dev); | |
553 | int error; | |
554 | uint8_t reg_val; | |
555 | ||
556 | mutex_lock(&data->lock); | |
557 | error = adp8870_read(data->client, reg, ®_val); | |
558 | mutex_unlock(&data->lock); | |
559 | ||
560 | if (error < 0) | |
561 | return error; | |
562 | ||
563 | return sprintf(buf, "%u\n", reg_val); | |
564 | } | |
565 | ||
566 | static ssize_t adp8870_store(struct device *dev, const char *buf, | |
567 | size_t count, int reg) | |
568 | { | |
569 | struct adp8870_bl *data = dev_get_drvdata(dev); | |
570 | unsigned long val; | |
571 | int ret; | |
572 | ||
dc406f56 | 573 | ret = kstrtoul(buf, 10, &val); |
a59ec1e7 MH |
574 | if (ret) |
575 | return ret; | |
576 | ||
577 | mutex_lock(&data->lock); | |
578 | adp8870_write(data->client, reg, val); | |
579 | mutex_unlock(&data->lock); | |
580 | ||
581 | return count; | |
582 | } | |
583 | ||
584 | static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev, | |
585 | struct device_attribute *attr, char *buf) | |
586 | { | |
587 | return adp8870_show(dev, buf, ADP8870_BLMX5); | |
588 | } | |
589 | ||
590 | static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev, | |
591 | struct device_attribute *attr, const char *buf, size_t count) | |
592 | { | |
593 | return adp8870_store(dev, buf, count, ADP8870_BLMX5); | |
594 | } | |
595 | static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show, | |
596 | adp8870_bl_l5_dark_max_store); | |
597 | ||
598 | ||
599 | static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev, | |
600 | struct device_attribute *attr, char *buf) | |
601 | { | |
602 | return adp8870_show(dev, buf, ADP8870_BLMX4); | |
603 | } | |
604 | ||
605 | static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev, | |
606 | struct device_attribute *attr, const char *buf, size_t count) | |
607 | { | |
608 | return adp8870_store(dev, buf, count, ADP8870_BLMX4); | |
609 | } | |
610 | static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show, | |
611 | adp8870_bl_l4_indoor_max_store); | |
612 | ||
613 | ||
614 | static ssize_t adp8870_bl_l3_office_max_show(struct device *dev, | |
615 | struct device_attribute *attr, char *buf) | |
616 | { | |
617 | return adp8870_show(dev, buf, ADP8870_BLMX3); | |
618 | } | |
619 | ||
620 | static ssize_t adp8870_bl_l3_office_max_store(struct device *dev, | |
621 | struct device_attribute *attr, const char *buf, size_t count) | |
622 | { | |
623 | return adp8870_store(dev, buf, count, ADP8870_BLMX3); | |
624 | } | |
625 | ||
626 | static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show, | |
627 | adp8870_bl_l3_office_max_store); | |
628 | ||
629 | static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev, | |
630 | struct device_attribute *attr, char *buf) | |
631 | { | |
632 | return adp8870_show(dev, buf, ADP8870_BLMX2); | |
633 | } | |
634 | ||
635 | static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev, | |
636 | struct device_attribute *attr, const char *buf, size_t count) | |
637 | { | |
638 | return adp8870_store(dev, buf, count, ADP8870_BLMX2); | |
639 | } | |
640 | static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show, | |
641 | adp8870_bl_l2_bright_max_store); | |
642 | ||
643 | static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev, | |
644 | struct device_attribute *attr, char *buf) | |
645 | { | |
646 | return adp8870_show(dev, buf, ADP8870_BLMX1); | |
647 | } | |
648 | ||
649 | static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev, | |
650 | struct device_attribute *attr, const char *buf, size_t count) | |
651 | { | |
652 | struct adp8870_bl *data = dev_get_drvdata(dev); | |
dc406f56 | 653 | int ret = kstrtoul(buf, 10, &data->cached_daylight_max); |
a59ec1e7 MH |
654 | if (ret) |
655 | return ret; | |
656 | ||
657 | return adp8870_store(dev, buf, count, ADP8870_BLMX1); | |
658 | } | |
659 | static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show, | |
660 | adp8870_bl_l1_daylight_max_store); | |
661 | ||
662 | static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev, | |
663 | struct device_attribute *attr, char *buf) | |
664 | { | |
665 | return adp8870_show(dev, buf, ADP8870_BLDM5); | |
666 | } | |
667 | ||
668 | static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev, | |
669 | struct device_attribute *attr, | |
670 | const char *buf, size_t count) | |
671 | { | |
672 | return adp8870_store(dev, buf, count, ADP8870_BLDM5); | |
673 | } | |
674 | static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show, | |
675 | adp8870_bl_l5_dark_dim_store); | |
676 | ||
677 | static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev, | |
678 | struct device_attribute *attr, char *buf) | |
679 | { | |
680 | return adp8870_show(dev, buf, ADP8870_BLDM4); | |
681 | } | |
682 | ||
683 | static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev, | |
684 | struct device_attribute *attr, | |
685 | const char *buf, size_t count) | |
686 | { | |
687 | return adp8870_store(dev, buf, count, ADP8870_BLDM4); | |
688 | } | |
689 | static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show, | |
690 | adp8870_bl_l4_indoor_dim_store); | |
691 | ||
692 | ||
693 | static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev, | |
694 | struct device_attribute *attr, char *buf) | |
695 | { | |
696 | return adp8870_show(dev, buf, ADP8870_BLDM3); | |
697 | } | |
698 | ||
699 | static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev, | |
700 | struct device_attribute *attr, | |
701 | const char *buf, size_t count) | |
702 | { | |
703 | return adp8870_store(dev, buf, count, ADP8870_BLDM3); | |
704 | } | |
705 | static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show, | |
706 | adp8870_bl_l3_office_dim_store); | |
707 | ||
708 | static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev, | |
709 | struct device_attribute *attr, char *buf) | |
710 | { | |
711 | return adp8870_show(dev, buf, ADP8870_BLDM2); | |
712 | } | |
713 | ||
714 | static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev, | |
715 | struct device_attribute *attr, | |
716 | const char *buf, size_t count) | |
717 | { | |
718 | return adp8870_store(dev, buf, count, ADP8870_BLDM2); | |
719 | } | |
720 | static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show, | |
721 | adp8870_bl_l2_bright_dim_store); | |
722 | ||
723 | static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev, | |
724 | struct device_attribute *attr, char *buf) | |
725 | { | |
726 | return adp8870_show(dev, buf, ADP8870_BLDM1); | |
727 | } | |
728 | ||
729 | static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev, | |
730 | struct device_attribute *attr, | |
731 | const char *buf, size_t count) | |
732 | { | |
733 | return adp8870_store(dev, buf, count, ADP8870_BLDM1); | |
734 | } | |
735 | static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show, | |
736 | adp8870_bl_l1_daylight_dim_store); | |
737 | ||
738 | #ifdef ADP8870_EXT_FEATURES | |
739 | static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev, | |
740 | struct device_attribute *attr, char *buf) | |
741 | { | |
742 | struct adp8870_bl *data = dev_get_drvdata(dev); | |
743 | int error; | |
744 | uint8_t reg_val; | |
745 | uint16_t ret_val; | |
746 | ||
747 | mutex_lock(&data->lock); | |
748 | error = adp8870_read(data->client, ADP8870_PH1LEVL, ®_val); | |
749 | if (error < 0) { | |
750 | mutex_unlock(&data->lock); | |
751 | return error; | |
752 | } | |
753 | ret_val = reg_val; | |
754 | error = adp8870_read(data->client, ADP8870_PH1LEVH, ®_val); | |
755 | mutex_unlock(&data->lock); | |
756 | ||
757 | if (error < 0) | |
758 | return error; | |
759 | ||
760 | /* Return 13-bit conversion value for the first light sensor */ | |
761 | ret_val += (reg_val & 0x1F) << 8; | |
762 | ||
763 | return sprintf(buf, "%u\n", ret_val); | |
764 | } | |
765 | static DEVICE_ATTR(ambient_light_level, 0444, | |
766 | adp8870_bl_ambient_light_level_show, NULL); | |
767 | ||
768 | static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev, | |
769 | struct device_attribute *attr, char *buf) | |
770 | { | |
771 | struct adp8870_bl *data = dev_get_drvdata(dev); | |
772 | int error; | |
773 | uint8_t reg_val; | |
774 | ||
775 | mutex_lock(&data->lock); | |
776 | error = adp8870_read(data->client, ADP8870_CFGR, ®_val); | |
777 | mutex_unlock(&data->lock); | |
778 | ||
779 | if (error < 0) | |
780 | return error; | |
781 | ||
782 | return sprintf(buf, "%u\n", | |
783 | ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1); | |
784 | } | |
785 | ||
786 | static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev, | |
787 | struct device_attribute *attr, | |
788 | const char *buf, size_t count) | |
789 | { | |
790 | struct adp8870_bl *data = dev_get_drvdata(dev); | |
791 | unsigned long val; | |
792 | uint8_t reg_val; | |
793 | int ret; | |
794 | ||
dc406f56 | 795 | ret = kstrtoul(buf, 10, &val); |
a59ec1e7 MH |
796 | if (ret) |
797 | return ret; | |
798 | ||
799 | if (val == 0) { | |
800 | /* Enable automatic ambient light sensing */ | |
801 | adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN); | |
802 | } else if ((val > 0) && (val < 6)) { | |
803 | /* Disable automatic ambient light sensing */ | |
804 | adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN); | |
805 | ||
806 | /* Set user supplied ambient light zone */ | |
807 | mutex_lock(&data->lock); | |
808 | adp8870_read(data->client, ADP8870_CFGR, ®_val); | |
809 | reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT); | |
810 | reg_val |= (val - 1) << CFGR_BLV_SHIFT; | |
811 | adp8870_write(data->client, ADP8870_CFGR, reg_val); | |
812 | mutex_unlock(&data->lock); | |
813 | } | |
814 | ||
815 | return count; | |
816 | } | |
817 | static DEVICE_ATTR(ambient_light_zone, 0664, | |
818 | adp8870_bl_ambient_light_zone_show, | |
819 | adp8870_bl_ambient_light_zone_store); | |
820 | #endif | |
821 | ||
822 | static struct attribute *adp8870_bl_attributes[] = { | |
823 | &dev_attr_l5_dark_max.attr, | |
824 | &dev_attr_l5_dark_dim.attr, | |
825 | &dev_attr_l4_indoor_max.attr, | |
826 | &dev_attr_l4_indoor_dim.attr, | |
827 | &dev_attr_l3_office_max.attr, | |
828 | &dev_attr_l3_office_dim.attr, | |
829 | &dev_attr_l2_bright_max.attr, | |
830 | &dev_attr_l2_bright_dim.attr, | |
831 | &dev_attr_l1_daylight_max.attr, | |
832 | &dev_attr_l1_daylight_dim.attr, | |
833 | #ifdef ADP8870_EXT_FEATURES | |
834 | &dev_attr_ambient_light_level.attr, | |
835 | &dev_attr_ambient_light_zone.attr, | |
836 | #endif | |
837 | NULL | |
838 | }; | |
839 | ||
840 | static const struct attribute_group adp8870_bl_attr_group = { | |
841 | .attrs = adp8870_bl_attributes, | |
842 | }; | |
843 | ||
1b9e450d | 844 | static int adp8870_probe(struct i2c_client *client, |
a59ec1e7 MH |
845 | const struct i2c_device_id *id) |
846 | { | |
847 | struct backlight_properties props; | |
848 | struct backlight_device *bl; | |
849 | struct adp8870_bl *data; | |
850 | struct adp8870_backlight_platform_data *pdata = | |
851 | client->dev.platform_data; | |
852 | uint8_t reg_val; | |
853 | int ret; | |
854 | ||
855 | if (!i2c_check_functionality(client->adapter, | |
856 | I2C_FUNC_SMBUS_BYTE_DATA)) { | |
857 | dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); | |
858 | return -EIO; | |
859 | } | |
860 | ||
861 | if (!pdata) { | |
862 | dev_err(&client->dev, "no platform data?\n"); | |
863 | return -EINVAL; | |
864 | } | |
865 | ||
866 | ret = adp8870_read(client, ADP8870_MFDVID, ®_val); | |
867 | if (ret < 0) | |
868 | return -EIO; | |
869 | ||
870 | if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) { | |
871 | dev_err(&client->dev, "failed to probe\n"); | |
872 | return -ENODEV; | |
873 | } | |
874 | ||
91cdb239 | 875 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
a59ec1e7 MH |
876 | if (data == NULL) |
877 | return -ENOMEM; | |
878 | ||
879 | data->revid = ADP8870_DEVID(reg_val); | |
880 | data->client = client; | |
881 | data->pdata = pdata; | |
882 | data->id = id->driver_data; | |
883 | data->current_brightness = 0; | |
884 | i2c_set_clientdata(client, data); | |
885 | ||
886 | mutex_init(&data->lock); | |
887 | ||
888 | memset(&props, 0, sizeof(props)); | |
e1bbd19b | 889 | props.type = BACKLIGHT_RAW; |
a59ec1e7 MH |
890 | props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS; |
891 | bl = backlight_device_register(dev_driver_string(&client->dev), | |
892 | &client->dev, data, &adp8870_bl_ops, &props); | |
893 | if (IS_ERR(bl)) { | |
894 | dev_err(&client->dev, "failed to register backlight\n"); | |
91cdb239 | 895 | return PTR_ERR(bl); |
a59ec1e7 MH |
896 | } |
897 | ||
898 | data->bl = bl; | |
899 | ||
8541b827 | 900 | if (pdata->en_ambl_sens) { |
a59ec1e7 MH |
901 | ret = sysfs_create_group(&bl->dev.kobj, |
902 | &adp8870_bl_attr_group); | |
8541b827 JH |
903 | if (ret) { |
904 | dev_err(&client->dev, "failed to register sysfs\n"); | |
905 | goto out1; | |
906 | } | |
a59ec1e7 MH |
907 | } |
908 | ||
909 | ret = adp8870_bl_setup(bl); | |
910 | if (ret) { | |
911 | ret = -EIO; | |
912 | goto out; | |
913 | } | |
914 | ||
915 | backlight_update_status(bl); | |
916 | ||
917 | dev_info(&client->dev, "Rev.%d Backlight\n", data->revid); | |
918 | ||
919 | if (pdata->num_leds) | |
920 | adp8870_led_probe(client); | |
921 | ||
922 | return 0; | |
923 | ||
924 | out: | |
925 | if (data->pdata->en_ambl_sens) | |
926 | sysfs_remove_group(&data->bl->dev.kobj, | |
927 | &adp8870_bl_attr_group); | |
928 | out1: | |
929 | backlight_device_unregister(bl); | |
a59ec1e7 MH |
930 | |
931 | return ret; | |
932 | } | |
933 | ||
7e4b9d0b | 934 | static int adp8870_remove(struct i2c_client *client) |
a59ec1e7 MH |
935 | { |
936 | struct adp8870_bl *data = i2c_get_clientdata(client); | |
937 | ||
938 | adp8870_clr_bits(client, ADP8870_MDCR, NSTBY); | |
939 | ||
940 | if (data->led) | |
941 | adp8870_led_remove(client); | |
942 | ||
943 | if (data->pdata->en_ambl_sens) | |
944 | sysfs_remove_group(&data->bl->dev.kobj, | |
945 | &adp8870_bl_attr_group); | |
946 | ||
947 | backlight_device_unregister(data->bl); | |
a59ec1e7 MH |
948 | |
949 | return 0; | |
950 | } | |
951 | ||
3cd4291a JH |
952 | #ifdef CONFIG_PM_SLEEP |
953 | static int adp8870_i2c_suspend(struct device *dev) | |
a59ec1e7 | 954 | { |
3cd4291a JH |
955 | struct i2c_client *client = to_i2c_client(dev); |
956 | ||
a59ec1e7 MH |
957 | adp8870_clr_bits(client, ADP8870_MDCR, NSTBY); |
958 | ||
959 | return 0; | |
960 | } | |
961 | ||
3cd4291a | 962 | static int adp8870_i2c_resume(struct device *dev) |
a59ec1e7 | 963 | { |
3cd4291a JH |
964 | struct i2c_client *client = to_i2c_client(dev); |
965 | ||
5eb02c01 | 966 | adp8870_set_bits(client, ADP8870_MDCR, NSTBY | BLEN); |
a59ec1e7 MH |
967 | |
968 | return 0; | |
969 | } | |
a59ec1e7 MH |
970 | #endif |
971 | ||
3cd4291a JH |
972 | static SIMPLE_DEV_PM_OPS(adp8870_i2c_pm_ops, adp8870_i2c_suspend, |
973 | adp8870_i2c_resume); | |
974 | ||
a59ec1e7 MH |
975 | static const struct i2c_device_id adp8870_id[] = { |
976 | { "adp8870", 0 }, | |
977 | { } | |
978 | }; | |
979 | MODULE_DEVICE_TABLE(i2c, adp8870_id); | |
980 | ||
981 | static struct i2c_driver adp8870_driver = { | |
982 | .driver = { | |
3cd4291a JH |
983 | .name = KBUILD_MODNAME, |
984 | .pm = &adp8870_i2c_pm_ops, | |
a59ec1e7 MH |
985 | }, |
986 | .probe = adp8870_probe, | |
d1723fa2 | 987 | .remove = adp8870_remove, |
a59ec1e7 MH |
988 | .id_table = adp8870_id, |
989 | }; | |
990 | ||
81ce6864 | 991 | module_i2c_driver(adp8870_driver); |
a59ec1e7 MH |
992 | |
993 | MODULE_LICENSE("GPL v2"); | |
994 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | |
995 | MODULE_DESCRIPTION("ADP8870 Backlight driver"); | |
c53252b7 | 996 | MODULE_ALIAS("i2c:adp8870-backlight"); |