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