File Index
Files and directories
Sign in
FILE

adp8860_bl.c

File preview with syntax highlighting.

linux-6.19.14/drivers/video/backlight/adp8860_bl.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Backlight driver for Analog Devices ADP8860 Backlight Devices
  4 *
  5 * Copyright 2009-2010 Analog Devices Inc.
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/init.h>
 10#include <linux/errno.h>
 11#include <linux/pm.h>
 12#include <linux/platform_device.h>
 13#include <linux/i2c.h>
 14#include <linux/backlight.h>
 15#include <linux/leds.h>
 16#include <linux/slab.h>
 17#include <linux/workqueue.h>
 18
 19#include <linux/platform_data/adp8860.h>
 20#define ADP8860_EXT_FEATURES
 21#define ADP8860_USE_LEDS
 22
 23#define ADP8860_MFDVID 0x00 /* Manufacturer and device ID */
 24#define ADP8860_MDCR 0x01 /* Device mode and status */
 25#define ADP8860_MDCR2 0x02 /* Device mode and Status Register 2 */
 26#define ADP8860_INTR_EN 0x03 /* Interrupts enable */
 27#define ADP8860_CFGR 0x04 /* Configuration register */
 28#define ADP8860_BLSEN 0x05 /* Sink enable backlight or independent */
 29#define ADP8860_BLOFF 0x06 /* Backlight off timeout */
 30#define ADP8860_BLDIM 0x07 /* Backlight dim timeout */
 31#define ADP8860_BLFR 0x08 /* Backlight fade in and out rates */
 32#define ADP8860_BLMX1 0x09 /* Backlight (Brightness Level 1-daylight) maximum current */
 33#define ADP8860_BLDM1 0x0A /* Backlight (Brightness Level 1-daylight) dim current */
 34#define ADP8860_BLMX2 0x0B /* Backlight (Brightness Level 2-office) maximum current */
 35#define ADP8860_BLDM2 0x0C /* Backlight (Brightness Level 2-office) dim current */
 36#define ADP8860_BLMX3 0x0D /* Backlight (Brightness Level 3-dark) maximum current */
 37#define ADP8860_BLDM3 0x0E /* Backlight (Brightness Level 3-dark) dim current */
 38#define ADP8860_ISCFR 0x0F /* Independent sink current fade control register */
 39#define ADP8860_ISCC 0x10 /* Independent sink current control register */
 40#define ADP8860_ISCT1 0x11 /* Independent Sink Current Timer Register LED[7:5] */
 41#define ADP8860_ISCT2 0x12 /* Independent Sink Current Timer Register LED[4:1] */
 42#define ADP8860_ISCF 0x13 /* Independent sink current fade register */
 43#define ADP8860_ISC7 0x14 /* Independent Sink Current LED7 */
 44#define ADP8860_ISC6 0x15 /* Independent Sink Current LED6 */
 45#define ADP8860_ISC5 0x16 /* Independent Sink Current LED5 */
 46#define ADP8860_ISC4 0x17 /* Independent Sink Current LED4 */
 47#define ADP8860_ISC3 0x18 /* Independent Sink Current LED3 */
 48#define ADP8860_ISC2 0x19 /* Independent Sink Current LED2 */
 49#define ADP8860_ISC1 0x1A /* Independent Sink Current LED1 */
 50#define ADP8860_CCFG 0x1B /* Comparator configuration */
 51#define ADP8860_CCFG2 0x1C /* Second comparator configuration */
 52#define ADP8860_L2_TRP 0x1D /* L2 comparator reference */
 53#define ADP8860_L2_HYS 0x1E /* L2 hysteresis */
 54#define ADP8860_L3_TRP 0x1F /* L3 comparator reference */
 55#define ADP8860_L3_HYS 0x20 /* L3 hysteresis */
 56#define ADP8860_PH1LEVL 0x21 /* First phototransistor ambient light level-low byte register */
 57#define ADP8860_PH1LEVH 0x22 /* First phototransistor ambient light level-high byte register */
 58#define ADP8860_PH2LEVL 0x23 /* Second phototransistor ambient light level-low byte register */
 59#define ADP8860_PH2LEVH 0x24 /* Second phototransistor ambient light level-high byte register */
 60
 61#define ADP8860_MANUFID		0x0  /* Analog Devices ADP8860 Manufacturer ID */
 62#define ADP8861_MANUFID		0x4  /* Analog Devices ADP8861 Manufacturer ID */
 63#define ADP8863_MANUFID		0x2  /* Analog Devices ADP8863 Manufacturer ID */
 64
 65#define ADP8860_DEVID(x)	((x) & 0xF)
 66#define ADP8860_MANID(x)	((x) >> 4)
 67
 68/* MDCR Device mode and status */
 69#define INT_CFG			(1 << 6)
 70#define NSTBY			(1 << 5)
 71#define DIM_EN			(1 << 4)
 72#define GDWN_DIS		(1 << 3)
 73#define SIS_EN			(1 << 2)
 74#define CMP_AUTOEN		(1 << 1)
 75#define BLEN			(1 << 0)
 76
 77/* ADP8860_CCFG Main ALS comparator level enable */
 78#define L3_EN			(1 << 1)
 79#define L2_EN			(1 << 0)
 80
 81#define CFGR_BLV_SHIFT		3
 82#define CFGR_BLV_MASK		0x3
 83#define ADP8860_FLAG_LED_MASK	0xFF
 84
 85#define FADE_VAL(in, out)	((0xF & (in)) | ((0xF & (out)) << 4))
 86#define BL_CFGR_VAL(law, blv)	((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
 87#define ALS_CCFG_VAL(filt)	((0x7 & filt) << 5)
 88
 89enum {
 90	adp8860,
 91	adp8861,
 92	adp8863
 93};
 94
 95struct adp8860_led {
 96	struct led_classdev	cdev;
 97	struct work_struct	work;
 98	struct i2c_client	*client;
 99	enum led_brightness	new_brightness;
100	int			id;
101	int			flags;
102};
103
104struct adp8860_bl {
105	struct i2c_client *client;
106	struct backlight_device *bl;
107	struct adp8860_led *led;
108	struct adp8860_backlight_platform_data *pdata;
109	struct mutex lock;
110	unsigned long cached_daylight_max;
111	int id;
112	int revid;
113	int current_brightness;
114	unsigned en_ambl_sens:1;
115	unsigned gdwn_dis:1;
116};
117
118static int adp8860_read(struct i2c_client *client, int reg, uint8_t *val)
119{
120	int ret;
121
122	ret = i2c_smbus_read_byte_data(client, reg);
123	if (ret < 0) {
124		dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
125		return ret;
126	}
127
128	*val = (uint8_t)ret;
129	return 0;
130}
131
132static int adp8860_write(struct i2c_client *client, u8 reg, u8 val)
133{
134	return i2c_smbus_write_byte_data(client, reg, val);
135}
136
137static int adp8860_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
138{
139	struct adp8860_bl *data = i2c_get_clientdata(client);
140	uint8_t reg_val;
141	int ret;
142
143	mutex_lock(&data->lock);
144
145	ret = adp8860_read(client, reg, &reg_val);
146
147	if (!ret && ((reg_val & bit_mask) != bit_mask)) {
148		reg_val |= bit_mask;
149		ret = adp8860_write(client, reg, reg_val);
150	}
151
152	mutex_unlock(&data->lock);
153	return ret;
154}
155
156static int adp8860_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
157{
158	struct adp8860_bl *data = i2c_get_clientdata(client);
159	uint8_t reg_val;
160	int ret;
161
162	mutex_lock(&data->lock);
163
164	ret = adp8860_read(client, reg, &reg_val);
165
166	if (!ret && (reg_val & bit_mask)) {
167		reg_val &= ~bit_mask;
168		ret = adp8860_write(client, reg, reg_val);
169	}
170
171	mutex_unlock(&data->lock);
172	return ret;
173}
174
175/*
176 * Independent sink / LED
177 */
178#if defined(ADP8860_USE_LEDS)
179static void adp8860_led_work(struct work_struct *work)
180{
181	struct adp8860_led *led = container_of(work, struct adp8860_led, work);
182
183	adp8860_write(led->client, ADP8860_ISC1 - led->id + 1,
184			 led->new_brightness >> 1);
185}
186
187static void adp8860_led_set(struct led_classdev *led_cdev,
188			   enum led_brightness value)
189{
190	struct adp8860_led *led;
191
192	led = container_of(led_cdev, struct adp8860_led, cdev);
193	led->new_brightness = value;
194	schedule_work(&led->work);
195}
196
197static int adp8860_led_setup(struct adp8860_led *led)
198{
199	struct i2c_client *client = led->client;
200	int ret = 0;
201
202	ret = adp8860_write(client, ADP8860_ISC1 - led->id + 1, 0);
203	ret |= adp8860_set_bits(client, ADP8860_ISCC, 1 << (led->id - 1));
204
205	if (led->id > 4)
206		ret |= adp8860_set_bits(client, ADP8860_ISCT1,
207				(led->flags & 0x3) << ((led->id - 5) * 2));
208	else
209		ret |= adp8860_set_bits(client, ADP8860_ISCT2,
210				(led->flags & 0x3) << ((led->id - 1) * 2));
211
212	return ret;
213}
214
215static int adp8860_led_probe(struct i2c_client *client)
216{
217	struct adp8860_backlight_platform_data *pdata =
218		dev_get_platdata(&client->dev);
219	struct adp8860_bl *data = i2c_get_clientdata(client);
220	struct adp8860_led *led, *led_dat;
221	struct led_info *cur_led;
222	int ret, i;
223
224	led = devm_kcalloc(&client->dev, pdata->num_leds, sizeof(*led),
225				GFP_KERNEL);
226	if (led == NULL)
227		return -ENOMEM;
228
229	ret = adp8860_write(client, ADP8860_ISCFR, pdata->led_fade_law);
230	ret = adp8860_write(client, ADP8860_ISCT1,
231			(pdata->led_on_time & 0x3) << 6);
232	ret |= adp8860_write(client, ADP8860_ISCF,
233			FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
234
235	if (ret) {
236		dev_err(&client->dev, "failed to write\n");
237		return ret;
238	}
239
240	for (i = 0; i < pdata->num_leds; ++i) {
241		cur_led = &pdata->leds[i];
242		led_dat = &led[i];
243
244		led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
245
246		if (led_dat->id > 7 || led_dat->id < 1) {
247			dev_err(&client->dev, "Invalid LED ID %d\n",
248				led_dat->id);
249			ret = -EINVAL;
250			goto err;
251		}
252
253		if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
254			dev_err(&client->dev, "LED %d used by Backlight\n",
255				led_dat->id);
256			ret = -EBUSY;
257			goto err;
258		}
259
260		led_dat->cdev.name = cur_led->name;
261		led_dat->cdev.default_trigger = cur_led->default_trigger;
262		led_dat->cdev.brightness_set = adp8860_led_set;
263		led_dat->cdev.brightness = LED_OFF;
264		led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
265		led_dat->client = client;
266		led_dat->new_brightness = LED_OFF;
267		INIT_WORK(&led_dat->work, adp8860_led_work);
268
269		ret = led_classdev_register(&client->dev, &led_dat->cdev);
270		if (ret) {
271			dev_err(&client->dev, "failed to register LED %d\n",
272				led_dat->id);
273			goto err;
274		}
275
276		ret = adp8860_led_setup(led_dat);
277		if (ret) {
278			dev_err(&client->dev, "failed to write\n");
279			i++;
280			goto err;
281		}
282	}
283
284	data->led = led;
285
286	return 0;
287
288 err:
289	for (i = i - 1; i >= 0; --i) {
290		led_classdev_unregister(&led[i].cdev);
291		cancel_work_sync(&led[i].work);
292	}
293
294	return ret;
295}
296
297static int adp8860_led_remove(struct i2c_client *client)
298{
299	struct adp8860_backlight_platform_data *pdata =
300		dev_get_platdata(&client->dev);
301	struct adp8860_bl *data = i2c_get_clientdata(client);
302	int i;
303
304	for (i = 0; i < pdata->num_leds; i++) {
305		led_classdev_unregister(&data->led[i].cdev);
306		cancel_work_sync(&data->led[i].work);
307	}
308
309	return 0;
310}
311#else
312static int adp8860_led_probe(struct i2c_client *client)
313{
314	return 0;
315}
316
317static int adp8860_led_remove(struct i2c_client *client)
318{
319	return 0;
320}
321#endif
322
323static int adp8860_bl_set(struct backlight_device *bl, int brightness)
324{
325	struct adp8860_bl *data = bl_get_data(bl);
326	struct i2c_client *client = data->client;
327	int ret = 0;
328
329	if (data->en_ambl_sens) {
330		if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
331			/* Disable Ambient Light auto adjust */
332			ret |= adp8860_clr_bits(client, ADP8860_MDCR,
333					CMP_AUTOEN);
334			ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
335		} else {
336			/*
337			 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
338			 * restore daylight l1 sysfs brightness
339			 */
340			ret |= adp8860_write(client, ADP8860_BLMX1,
341					 data->cached_daylight_max);
342			ret |= adp8860_set_bits(client, ADP8860_MDCR,
343					 CMP_AUTOEN);
344		}
345	} else
346		ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
347
348	if (data->current_brightness && brightness == 0)
349		ret |= adp8860_set_bits(client,
350				ADP8860_MDCR, DIM_EN);
351	else if (data->current_brightness == 0 && brightness)
352		ret |= adp8860_clr_bits(client,
353				ADP8860_MDCR, DIM_EN);
354
355	if (!ret)
356		data->current_brightness = brightness;
357
358	return ret;
359}
360
361static int adp8860_bl_update_status(struct backlight_device *bl)
362{
363	return adp8860_bl_set(bl, backlight_get_brightness(bl));
364}
365
366static int adp8860_bl_get_brightness(struct backlight_device *bl)
367{
368	struct adp8860_bl *data = bl_get_data(bl);
369
370	return data->current_brightness;
371}
372
373static const struct backlight_ops adp8860_bl_ops = {
374	.update_status	= adp8860_bl_update_status,
375	.get_brightness	= adp8860_bl_get_brightness,
376};
377
378static int adp8860_bl_setup(struct backlight_device *bl)
379{
380	struct adp8860_bl *data = bl_get_data(bl);
381	struct i2c_client *client = data->client;
382	struct adp8860_backlight_platform_data *pdata = data->pdata;
383	int ret = 0;
384
385	ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
386	ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
387	ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
388
389	if (data->en_ambl_sens) {
390		data->cached_daylight_max = pdata->l1_daylight_max;
391		ret |= adp8860_write(client, ADP8860_BLMX2,
392						pdata->l2_office_max);
393		ret |= adp8860_write(client, ADP8860_BLDM2,
394						pdata->l2_office_dim);
395		ret |= adp8860_write(client, ADP8860_BLMX3,
396						pdata->l3_dark_max);
397		ret |= adp8860_write(client, ADP8860_BLDM3,
398						pdata->l3_dark_dim);
399
400		ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
401		ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
402		ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
403		ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
404		ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
405						ALS_CCFG_VAL(pdata->abml_filt));
406	}
407
408	ret |= adp8860_write(client, ADP8860_CFGR,
409			BL_CFGR_VAL(pdata->bl_fade_law, 0));
410
411	ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
412			pdata->bl_fade_out));
413
414	ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
415			(data->gdwn_dis ? GDWN_DIS : 0));
416
417	return ret;
418}
419
420static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
421{
422	struct adp8860_bl *data = dev_get_drvdata(dev);
423	int error;
424	uint8_t reg_val;
425
426	mutex_lock(&data->lock);
427	error = adp8860_read(data->client, reg, &reg_val);
428	mutex_unlock(&data->lock);
429
430	if (error < 0)
431		return error;
432
433	return sprintf(buf, "%u\n", reg_val);
434}
435
436static ssize_t adp8860_store(struct device *dev, const char *buf,
437			 size_t count, int reg)
438{
439	struct adp8860_bl *data = dev_get_drvdata(dev);
440	unsigned long val;
441	int ret;
442
443	ret = kstrtoul(buf, 10, &val);
444	if (ret)
445		return ret;
446
447	mutex_lock(&data->lock);
448	adp8860_write(data->client, reg, val);
449	mutex_unlock(&data->lock);
450
451	return count;
452}
453
454static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
455				     struct device_attribute *attr, char *buf)
456{
457	return adp8860_show(dev, buf, ADP8860_BLMX3);
458}
459
460static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
461		struct device_attribute *attr, const char *buf, size_t count)
462{
463	return adp8860_store(dev, buf, count, ADP8860_BLMX3);
464}
465
466static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
467			adp8860_bl_l3_dark_max_store);
468
469static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
470		struct device_attribute *attr, char *buf)
471{
472	return adp8860_show(dev, buf, ADP8860_BLMX2);
473}
474
475static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
476		struct device_attribute *attr, const char *buf, size_t count)
477{
478	return adp8860_store(dev, buf, count, ADP8860_BLMX2);
479}
480static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
481			adp8860_bl_l2_office_max_store);
482
483static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
484			struct device_attribute *attr, char *buf)
485{
486	return adp8860_show(dev, buf, ADP8860_BLMX1);
487}
488
489static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
490		struct device_attribute *attr, const char *buf, size_t count)
491{
492	struct adp8860_bl *data = dev_get_drvdata(dev);
493	int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
494
495	if (ret)
496		return ret;
497
498	return adp8860_store(dev, buf, count, ADP8860_BLMX1);
499}
500static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
501			adp8860_bl_l1_daylight_max_store);
502
503static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
504			struct device_attribute *attr, char *buf)
505{
506	return adp8860_show(dev, buf, ADP8860_BLDM3);
507}
508
509static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
510				     struct device_attribute *attr,
511				     const char *buf, size_t count)
512{
513	return adp8860_store(dev, buf, count, ADP8860_BLDM3);
514}
515static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
516			adp8860_bl_l3_dark_dim_store);
517
518static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
519			struct device_attribute *attr, char *buf)
520{
521	return adp8860_show(dev, buf, ADP8860_BLDM2);
522}
523
524static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
525				     struct device_attribute *attr,
526				     const char *buf, size_t count)
527{
528	return adp8860_store(dev, buf, count, ADP8860_BLDM2);
529}
530static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
531			adp8860_bl_l2_office_dim_store);
532
533static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
534				     struct device_attribute *attr, char *buf)
535{
536	return adp8860_show(dev, buf, ADP8860_BLDM1);
537}
538
539static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
540				     struct device_attribute *attr,
541				     const char *buf, size_t count)
542{
543	return adp8860_store(dev, buf, count, ADP8860_BLDM1);
544}
545static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
546			adp8860_bl_l1_daylight_dim_store);
547
548#ifdef ADP8860_EXT_FEATURES
549static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
550				     struct device_attribute *attr, char *buf)
551{
552	struct adp8860_bl *data = dev_get_drvdata(dev);
553	int error;
554	uint8_t reg_val;
555	uint16_t ret_val;
556
557	mutex_lock(&data->lock);
558	error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
559	if (!error) {
560		ret_val = reg_val;
561		error = adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
562	}
563	mutex_unlock(&data->lock);
564
565	if (error)
566		return error;
567
568	/* Return 13-bit conversion value for the first light sensor */
569	ret_val += (reg_val & 0x1F) << 8;
570
571	return sprintf(buf, "%u\n", ret_val);
572}
573static DEVICE_ATTR(ambient_light_level, 0444,
574		adp8860_bl_ambient_light_level_show, NULL);
575
576static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
577				     struct device_attribute *attr, char *buf)
578{
579	struct adp8860_bl *data = dev_get_drvdata(dev);
580	int error;
581	uint8_t reg_val;
582
583	mutex_lock(&data->lock);
584	error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
585	mutex_unlock(&data->lock);
586
587	if (error < 0)
588		return error;
589
590	return sprintf(buf, "%u\n",
591		((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
592}
593
594static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
595				     struct device_attribute *attr,
596				     const char *buf, size_t count)
597{
598	struct adp8860_bl *data = dev_get_drvdata(dev);
599	unsigned long val;
600	uint8_t reg_val;
601	int ret;
602
603	ret = kstrtoul(buf, 10, &val);
604	if (ret)
605		return ret;
606
607	if (val == 0) {
608		/* Enable automatic ambient light sensing */
609		adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
610	} else if ((val > 0) && (val <= 3)) {
611		/* Disable automatic ambient light sensing */
612		adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
613
614		/* Set user supplied ambient light zone */
615		mutex_lock(&data->lock);
616		ret = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
617		if (!ret) {
618			reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
619			reg_val |= (val - 1) << CFGR_BLV_SHIFT;
620			adp8860_write(data->client, ADP8860_CFGR, reg_val);
621		}
622		mutex_unlock(&data->lock);
623	}
624
625	return count;
626}
627static DEVICE_ATTR(ambient_light_zone, 0664,
628		adp8860_bl_ambient_light_zone_show,
629		adp8860_bl_ambient_light_zone_store);
630#endif
631
632static struct attribute *adp8860_bl_attributes[] = {
633	&dev_attr_l3_dark_max.attr,
634	&dev_attr_l3_dark_dim.attr,
635	&dev_attr_l2_office_max.attr,
636	&dev_attr_l2_office_dim.attr,
637	&dev_attr_l1_daylight_max.attr,
638	&dev_attr_l1_daylight_dim.attr,
639#ifdef ADP8860_EXT_FEATURES
640	&dev_attr_ambient_light_level.attr,
641	&dev_attr_ambient_light_zone.attr,
642#endif
643	NULL
644};
645
646static const struct attribute_group adp8860_bl_attr_group = {
647	.attrs = adp8860_bl_attributes,
648};
649
650static int adp8860_probe(struct i2c_client *client)
651{
652	const struct i2c_device_id *id = i2c_client_get_device_id(client);
653	struct backlight_device *bl;
654	struct adp8860_bl *data;
655	struct adp8860_backlight_platform_data *pdata =
656		dev_get_platdata(&client->dev);
657	struct backlight_properties props;
658	uint8_t reg_val;
659	int ret;
660
661	if (!i2c_check_functionality(client->adapter,
662					I2C_FUNC_SMBUS_BYTE_DATA)) {
663		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
664		return -EIO;
665	}
666
667	if (!pdata) {
668		dev_err(&client->dev, "no platform data?\n");
669		return -EINVAL;
670	}
671
672	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
673	if (data == NULL)
674		return -ENOMEM;
675
676	ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
677	if (ret < 0)
678		return ret;
679
680	switch (ADP8860_MANID(reg_val)) {
681	case ADP8863_MANUFID:
682		data->gdwn_dis = !!pdata->gdwn_dis;
683		fallthrough;
684	case ADP8860_MANUFID:
685		data->en_ambl_sens = !!pdata->en_ambl_sens;
686		break;
687	case ADP8861_MANUFID:
688		data->gdwn_dis = !!pdata->gdwn_dis;
689		break;
690	default:
691		dev_err(&client->dev, "failed to probe\n");
692		return -ENODEV;
693	}
694
695	/* It's confirmed that the DEVID field is actually a REVID */
696
697	data->revid = ADP8860_DEVID(reg_val);
698	data->client = client;
699	data->pdata = pdata;
700	data->id = id->driver_data;
701	data->current_brightness = 0;
702	i2c_set_clientdata(client, data);
703
704	memset(&props, 0, sizeof(props));
705	props.type = BACKLIGHT_RAW;
706	props.max_brightness = ADP8860_MAX_BRIGHTNESS;
707
708	mutex_init(&data->lock);
709
710	bl = devm_backlight_device_register(&client->dev,
711				dev_driver_string(&client->dev),
712				&client->dev, data, &adp8860_bl_ops, &props);
713	if (IS_ERR(bl)) {
714		dev_err(&client->dev, "failed to register backlight\n");
715		return PTR_ERR(bl);
716	}
717
718	bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
719
720	data->bl = bl;
721
722	if (data->en_ambl_sens)
723		ret = sysfs_create_group(&bl->dev.kobj,
724			&adp8860_bl_attr_group);
725
726	if (ret) {
727		dev_err(&client->dev, "failed to register sysfs\n");
728		return ret;
729	}
730
731	ret = adp8860_bl_setup(bl);
732	if (ret) {
733		ret = -EIO;
734		goto out;
735	}
736
737	backlight_update_status(bl);
738
739	dev_info(&client->dev, "%s Rev.%d Backlight\n",
740		client->name, data->revid);
741
742	if (pdata->num_leds)
743		adp8860_led_probe(client);
744
745	return 0;
746
747out:
748	if (data->en_ambl_sens)
749		sysfs_remove_group(&data->bl->dev.kobj,
750			&adp8860_bl_attr_group);
751
752	return ret;
753}
754
755static void adp8860_remove(struct i2c_client *client)
756{
757	struct adp8860_bl *data = i2c_get_clientdata(client);
758
759	adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
760
761	if (data->led)
762		adp8860_led_remove(client);
763
764	if (data->en_ambl_sens)
765		sysfs_remove_group(&data->bl->dev.kobj,
766			&adp8860_bl_attr_group);
767}
768
769#ifdef CONFIG_PM_SLEEP
770static int adp8860_i2c_suspend(struct device *dev)
771{
772	struct i2c_client *client = to_i2c_client(dev);
773
774	adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
775
776	return 0;
777}
778
779static int adp8860_i2c_resume(struct device *dev)
780{
781	struct i2c_client *client = to_i2c_client(dev);
782
783	adp8860_set_bits(client, ADP8860_MDCR, NSTBY | BLEN);
784
785	return 0;
786}
787#endif
788
789static SIMPLE_DEV_PM_OPS(adp8860_i2c_pm_ops, adp8860_i2c_suspend,
790			adp8860_i2c_resume);
791
792static const struct i2c_device_id adp8860_id[] = {
793	{ "adp8860", adp8860 },
794	{ "adp8861", adp8861 },
795	{ "adp8863", adp8863 },
796	{ }
797};
798MODULE_DEVICE_TABLE(i2c, adp8860_id);
799
800static struct i2c_driver adp8860_driver = {
801	.driver = {
802		.name	= KBUILD_MODNAME,
803		.pm	= &adp8860_i2c_pm_ops,
804	},
805	.probe = adp8860_probe,
806	.remove = adp8860_remove,
807	.id_table = adp8860_id,
808};
809
810module_i2c_driver(adp8860_driver);
811
812MODULE_LICENSE("GPL v2");
813MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
814MODULE_DESCRIPTION("ADP8860 Backlight driver");
~/ Read-only file preview.