xref: /openbmc/linux/drivers/leds/leds-lp8860.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27a8685acSDan Murphy /*
37a8685acSDan Murphy  * TI LP8860 4-Channel LED Driver
47a8685acSDan Murphy  *
57a8685acSDan Murphy  * Copyright (C) 2014 Texas Instruments
67a8685acSDan Murphy  *
77a8685acSDan Murphy  * Author: Dan Murphy <dmurphy@ti.com>
87a8685acSDan Murphy  */
97a8685acSDan Murphy 
107a8685acSDan Murphy #include <linux/i2c.h>
117a8685acSDan Murphy #include <linux/init.h>
127a8685acSDan Murphy #include <linux/leds.h>
137a8685acSDan Murphy #include <linux/regmap.h>
147a8685acSDan Murphy #include <linux/regulator/consumer.h>
157a8685acSDan Murphy #include <linux/module.h>
167a8685acSDan Murphy #include <linux/mutex.h>
177a8685acSDan Murphy #include <linux/of.h>
187a8685acSDan Murphy #include <linux/gpio/consumer.h>
197a8685acSDan Murphy #include <linux/slab.h>
207a8685acSDan Murphy 
217a8685acSDan Murphy #define LP8860_DISP_CL1_BRT_MSB		0x00
227a8685acSDan Murphy #define LP8860_DISP_CL1_BRT_LSB		0x01
237a8685acSDan Murphy #define LP8860_DISP_CL1_CURR_MSB	0x02
247a8685acSDan Murphy #define LP8860_DISP_CL1_CURR_LSB	0x03
257a8685acSDan Murphy #define LP8860_CL2_BRT_MSB		0x04
267a8685acSDan Murphy #define LP8860_CL2_BRT_LSB		0x05
277a8685acSDan Murphy #define LP8860_CL2_CURRENT		0x06
287a8685acSDan Murphy #define LP8860_CL3_BRT_MSB		0x07
297a8685acSDan Murphy #define LP8860_CL3_BRT_LSB		0x08
307a8685acSDan Murphy #define LP8860_CL3_CURRENT		0x09
317a8685acSDan Murphy #define LP8860_CL4_BRT_MSB		0x0a
327a8685acSDan Murphy #define LP8860_CL4_BRT_LSB		0x0b
337a8685acSDan Murphy #define LP8860_CL4_CURRENT		0x0c
347a8685acSDan Murphy #define LP8860_CONFIG			0x0d
357a8685acSDan Murphy #define LP8860_STATUS			0x0e
367a8685acSDan Murphy #define LP8860_FAULT			0x0f
377a8685acSDan Murphy #define LP8860_LED_FAULT		0x10
387a8685acSDan Murphy #define LP8860_FAULT_CLEAR		0x11
397a8685acSDan Murphy #define LP8860_ID			0x12
407a8685acSDan Murphy #define LP8860_TEMP_MSB			0x13
417a8685acSDan Murphy #define LP8860_TEMP_LSB			0x14
427a8685acSDan Murphy #define LP8860_DISP_LED_CURR_MSB	0x15
437a8685acSDan Murphy #define LP8860_DISP_LED_CURR_LSB	0x16
447a8685acSDan Murphy #define LP8860_DISP_LED_PWM_MSB		0x17
457a8685acSDan Murphy #define LP8860_DISP_LED_PWM_LSB		0x18
467a8685acSDan Murphy #define LP8860_EEPROM_CNTRL		0x19
477a8685acSDan Murphy #define LP8860_EEPROM_UNLOCK		0x1a
487a8685acSDan Murphy 
497a8685acSDan Murphy #define LP8860_EEPROM_REG_0		0x60
507a8685acSDan Murphy #define LP8860_EEPROM_REG_1		0x61
517a8685acSDan Murphy #define LP8860_EEPROM_REG_2		0x62
527a8685acSDan Murphy #define LP8860_EEPROM_REG_3		0x63
537a8685acSDan Murphy #define LP8860_EEPROM_REG_4		0x64
547a8685acSDan Murphy #define LP8860_EEPROM_REG_5		0x65
557a8685acSDan Murphy #define LP8860_EEPROM_REG_6		0x66
567a8685acSDan Murphy #define LP8860_EEPROM_REG_7		0x67
577a8685acSDan Murphy #define LP8860_EEPROM_REG_8		0x68
587a8685acSDan Murphy #define LP8860_EEPROM_REG_9		0x69
597a8685acSDan Murphy #define LP8860_EEPROM_REG_10		0x6a
607a8685acSDan Murphy #define LP8860_EEPROM_REG_11		0x6b
617a8685acSDan Murphy #define LP8860_EEPROM_REG_12		0x6c
627a8685acSDan Murphy #define LP8860_EEPROM_REG_13		0x6d
637a8685acSDan Murphy #define LP8860_EEPROM_REG_14		0x6e
647a8685acSDan Murphy #define LP8860_EEPROM_REG_15		0x6f
657a8685acSDan Murphy #define LP8860_EEPROM_REG_16		0x70
667a8685acSDan Murphy #define LP8860_EEPROM_REG_17		0x71
677a8685acSDan Murphy #define LP8860_EEPROM_REG_18		0x72
687a8685acSDan Murphy #define LP8860_EEPROM_REG_19		0x73
697a8685acSDan Murphy #define LP8860_EEPROM_REG_20		0x74
707a8685acSDan Murphy #define LP8860_EEPROM_REG_21		0x75
717a8685acSDan Murphy #define LP8860_EEPROM_REG_22		0x76
727a8685acSDan Murphy #define LP8860_EEPROM_REG_23		0x77
737a8685acSDan Murphy #define LP8860_EEPROM_REG_24		0x78
747a8685acSDan Murphy 
757a8685acSDan Murphy #define LP8860_LOCK_EEPROM		0x00
767a8685acSDan Murphy #define LP8860_UNLOCK_EEPROM		0x01
777a8685acSDan Murphy #define LP8860_PROGRAM_EEPROM		0x02
787a8685acSDan Murphy #define LP8860_EEPROM_CODE_1		0x08
797a8685acSDan Murphy #define LP8860_EEPROM_CODE_2		0xba
807a8685acSDan Murphy #define LP8860_EEPROM_CODE_3		0xef
817a8685acSDan Murphy 
827a8685acSDan Murphy #define LP8860_CLEAR_FAULTS		0x01
837a8685acSDan Murphy 
8499ca0ea5SJacek Anaszewski #define LP8860_NAME			"lp8860"
8599ca0ea5SJacek Anaszewski 
867a8685acSDan Murphy /**
8746cc5941SLee Jones  * struct lp8860_led
8846cc5941SLee Jones  * @lock: Lock for reading/writing the device
8946cc5941SLee Jones  * @client: Pointer to the I2C client
9046cc5941SLee Jones  * @led_dev: led class device pointer
9146cc5941SLee Jones  * @regmap: Devices register map
9246cc5941SLee Jones  * @eeprom_regmap: EEPROM register map
9346cc5941SLee Jones  * @enable_gpio: VDDIO/EN gpio to enable communication interface
9446cc5941SLee Jones  * @regulator: LED supply regulator pointer
958a7a76c8SDan Murphy  */
967a8685acSDan Murphy struct lp8860_led {
977a8685acSDan Murphy 	struct mutex lock;
987a8685acSDan Murphy 	struct i2c_client *client;
997a8685acSDan Murphy 	struct led_classdev led_dev;
1007a8685acSDan Murphy 	struct regmap *regmap;
1017a8685acSDan Murphy 	struct regmap *eeprom_regmap;
1027a8685acSDan Murphy 	struct gpio_desc *enable_gpio;
1037a8685acSDan Murphy 	struct regulator *regulator;
1047a8685acSDan Murphy };
1057a8685acSDan Murphy 
1067a8685acSDan Murphy struct lp8860_eeprom_reg {
1077a8685acSDan Murphy 	uint8_t reg;
1087a8685acSDan Murphy 	uint8_t value;
1097a8685acSDan Murphy };
1107a8685acSDan Murphy 
1117a8685acSDan Murphy static struct lp8860_eeprom_reg lp8860_eeprom_disp_regs[] = {
1127a8685acSDan Murphy 	{ LP8860_EEPROM_REG_0, 0xed },
1137a8685acSDan Murphy 	{ LP8860_EEPROM_REG_1, 0xdf },
1147a8685acSDan Murphy 	{ LP8860_EEPROM_REG_2, 0xdc },
1157a8685acSDan Murphy 	{ LP8860_EEPROM_REG_3, 0xf0 },
1167a8685acSDan Murphy 	{ LP8860_EEPROM_REG_4, 0xdf },
1177a8685acSDan Murphy 	{ LP8860_EEPROM_REG_5, 0xe5 },
1187a8685acSDan Murphy 	{ LP8860_EEPROM_REG_6, 0xf2 },
1197a8685acSDan Murphy 	{ LP8860_EEPROM_REG_7, 0x77 },
1207a8685acSDan Murphy 	{ LP8860_EEPROM_REG_8, 0x77 },
1217a8685acSDan Murphy 	{ LP8860_EEPROM_REG_9, 0x71 },
1227a8685acSDan Murphy 	{ LP8860_EEPROM_REG_10, 0x3f },
1237a8685acSDan Murphy 	{ LP8860_EEPROM_REG_11, 0xb7 },
1247a8685acSDan Murphy 	{ LP8860_EEPROM_REG_12, 0x17 },
1257a8685acSDan Murphy 	{ LP8860_EEPROM_REG_13, 0xef },
1267a8685acSDan Murphy 	{ LP8860_EEPROM_REG_14, 0xb0 },
1277a8685acSDan Murphy 	{ LP8860_EEPROM_REG_15, 0x87 },
1287a8685acSDan Murphy 	{ LP8860_EEPROM_REG_16, 0xce },
1297a8685acSDan Murphy 	{ LP8860_EEPROM_REG_17, 0x72 },
1307a8685acSDan Murphy 	{ LP8860_EEPROM_REG_18, 0xe5 },
1317a8685acSDan Murphy 	{ LP8860_EEPROM_REG_19, 0xdf },
1327a8685acSDan Murphy 	{ LP8860_EEPROM_REG_20, 0x35 },
1337a8685acSDan Murphy 	{ LP8860_EEPROM_REG_21, 0x06 },
1347a8685acSDan Murphy 	{ LP8860_EEPROM_REG_22, 0xdc },
1357a8685acSDan Murphy 	{ LP8860_EEPROM_REG_23, 0x88 },
1367a8685acSDan Murphy 	{ LP8860_EEPROM_REG_24, 0x3E },
1377a8685acSDan Murphy };
1387a8685acSDan Murphy 
lp8860_unlock_eeprom(struct lp8860_led * led,int lock)1397a8685acSDan Murphy static int lp8860_unlock_eeprom(struct lp8860_led *led, int lock)
1407a8685acSDan Murphy {
1417a8685acSDan Murphy 	int ret;
1427a8685acSDan Murphy 
1437a8685acSDan Murphy 	mutex_lock(&led->lock);
1447a8685acSDan Murphy 
1457a8685acSDan Murphy 	if (lock == LP8860_UNLOCK_EEPROM) {
1467a8685acSDan Murphy 		ret = regmap_write(led->regmap,
1477a8685acSDan Murphy 			LP8860_EEPROM_UNLOCK,
1487a8685acSDan Murphy 			LP8860_EEPROM_CODE_1);
1497a8685acSDan Murphy 		if (ret) {
1507a8685acSDan Murphy 			dev_err(&led->client->dev, "EEPROM Unlock failed\n");
1517a8685acSDan Murphy 			goto out;
1527a8685acSDan Murphy 		}
1537a8685acSDan Murphy 
1547a8685acSDan Murphy 		ret = regmap_write(led->regmap,
1557a8685acSDan Murphy 			LP8860_EEPROM_UNLOCK,
1567a8685acSDan Murphy 			LP8860_EEPROM_CODE_2);
1577a8685acSDan Murphy 		if (ret) {
1587a8685acSDan Murphy 			dev_err(&led->client->dev, "EEPROM Unlock failed\n");
1597a8685acSDan Murphy 			goto out;
1607a8685acSDan Murphy 		}
1617a8685acSDan Murphy 		ret = regmap_write(led->regmap,
1627a8685acSDan Murphy 			LP8860_EEPROM_UNLOCK,
1637a8685acSDan Murphy 			LP8860_EEPROM_CODE_3);
1647a8685acSDan Murphy 		if (ret) {
1657a8685acSDan Murphy 			dev_err(&led->client->dev, "EEPROM Unlock failed\n");
1667a8685acSDan Murphy 			goto out;
1677a8685acSDan Murphy 		}
1687a8685acSDan Murphy 	} else {
1697a8685acSDan Murphy 		ret = regmap_write(led->regmap,
1707a8685acSDan Murphy 			LP8860_EEPROM_UNLOCK,
1717a8685acSDan Murphy 			LP8860_LOCK_EEPROM);
1727a8685acSDan Murphy 	}
1737a8685acSDan Murphy 
1747a8685acSDan Murphy out:
1757a8685acSDan Murphy 	mutex_unlock(&led->lock);
1767a8685acSDan Murphy 	return ret;
1777a8685acSDan Murphy }
1787a8685acSDan Murphy 
lp8860_fault_check(struct lp8860_led * led)1797a8685acSDan Murphy static int lp8860_fault_check(struct lp8860_led *led)
1807a8685acSDan Murphy {
1817a8685acSDan Murphy 	int ret, fault;
1827a8685acSDan Murphy 	unsigned int read_buf;
1837a8685acSDan Murphy 
1847a8685acSDan Murphy 	ret = regmap_read(led->regmap, LP8860_LED_FAULT, &read_buf);
1857a8685acSDan Murphy 	if (ret)
1867a8685acSDan Murphy 		goto out;
1877a8685acSDan Murphy 
1887a8685acSDan Murphy 	fault = read_buf;
1897a8685acSDan Murphy 
1907a8685acSDan Murphy 	ret = regmap_read(led->regmap, LP8860_FAULT, &read_buf);
1917a8685acSDan Murphy 	if (ret)
1927a8685acSDan Murphy 		goto out;
1937a8685acSDan Murphy 
1947a8685acSDan Murphy 	fault |= read_buf;
1957a8685acSDan Murphy 
1967a8685acSDan Murphy 	/* Attempt to clear any faults */
1977a8685acSDan Murphy 	if (fault)
1987a8685acSDan Murphy 		ret = regmap_write(led->regmap, LP8860_FAULT_CLEAR,
1997a8685acSDan Murphy 			LP8860_CLEAR_FAULTS);
2007a8685acSDan Murphy out:
2017a8685acSDan Murphy 	return ret;
2027a8685acSDan Murphy }
2037a8685acSDan Murphy 
lp8860_brightness_set(struct led_classdev * led_cdev,enum led_brightness brt_val)2047c4b10a2SAndrew Lunn static int lp8860_brightness_set(struct led_classdev *led_cdev,
2057c4b10a2SAndrew Lunn 				enum led_brightness brt_val)
2067a8685acSDan Murphy {
2077c4b10a2SAndrew Lunn 	struct lp8860_led *led =
2087c4b10a2SAndrew Lunn 			container_of(led_cdev, struct lp8860_led, led_dev);
2097c4b10a2SAndrew Lunn 	int disp_brightness = brt_val * 255;
2107a8685acSDan Murphy 	int ret;
2117a8685acSDan Murphy 
2127a8685acSDan Murphy 	mutex_lock(&led->lock);
2137a8685acSDan Murphy 
2147a8685acSDan Murphy 	ret = lp8860_fault_check(led);
2157a8685acSDan Murphy 	if (ret) {
2167a8685acSDan Murphy 		dev_err(&led->client->dev, "Cannot read/clear faults\n");
2177a8685acSDan Murphy 		goto out;
2187a8685acSDan Murphy 	}
2197a8685acSDan Murphy 
2207a8685acSDan Murphy 	ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_MSB,
2217a8685acSDan Murphy 			(disp_brightness & 0xff00) >> 8);
2227a8685acSDan Murphy 	if (ret) {
2237a8685acSDan Murphy 		dev_err(&led->client->dev, "Cannot write CL1 MSB\n");
2247a8685acSDan Murphy 		goto out;
2257a8685acSDan Murphy 	}
2267a8685acSDan Murphy 
2277a8685acSDan Murphy 	ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_LSB,
2287a8685acSDan Murphy 			disp_brightness & 0xff);
2297a8685acSDan Murphy 	if (ret) {
2307a8685acSDan Murphy 		dev_err(&led->client->dev, "Cannot write CL1 LSB\n");
2317a8685acSDan Murphy 		goto out;
2327a8685acSDan Murphy 	}
2337a8685acSDan Murphy out:
2347a8685acSDan Murphy 	mutex_unlock(&led->lock);
2357c4b10a2SAndrew Lunn 	return ret;
2367a8685acSDan Murphy }
2377a8685acSDan Murphy 
lp8860_init(struct lp8860_led * led)2387a8685acSDan Murphy static int lp8860_init(struct lp8860_led *led)
2397a8685acSDan Murphy {
2407a8685acSDan Murphy 	unsigned int read_buf;
2417a8685acSDan Murphy 	int ret, i, reg_count;
2427a8685acSDan Murphy 
243b12ef03aSDan Murphy 	if (led->regulator) {
244b12ef03aSDan Murphy 		ret = regulator_enable(led->regulator);
245b12ef03aSDan Murphy 		if (ret) {
246b12ef03aSDan Murphy 			dev_err(&led->client->dev,
247b12ef03aSDan Murphy 				"Failed to enable regulator\n");
248b12ef03aSDan Murphy 			return ret;
249b12ef03aSDan Murphy 		}
250b12ef03aSDan Murphy 	}
251b12ef03aSDan Murphy 
2527a8685acSDan Murphy 	gpiod_direction_output(led->enable_gpio, 1);
2537a8685acSDan Murphy 
2547a8685acSDan Murphy 	ret = lp8860_fault_check(led);
2557a8685acSDan Murphy 	if (ret)
2567a8685acSDan Murphy 		goto out;
2577a8685acSDan Murphy 
2587a8685acSDan Murphy 	ret = regmap_read(led->regmap, LP8860_STATUS, &read_buf);
2597a8685acSDan Murphy 	if (ret)
2607a8685acSDan Murphy 		goto out;
2617a8685acSDan Murphy 
2627a8685acSDan Murphy 	ret = lp8860_unlock_eeprom(led, LP8860_UNLOCK_EEPROM);
2637a8685acSDan Murphy 	if (ret) {
2647a8685acSDan Murphy 		dev_err(&led->client->dev, "Failed unlocking EEPROM\n");
2657a8685acSDan Murphy 		goto out;
2667a8685acSDan Murphy 	}
2677a8685acSDan Murphy 
2687a8685acSDan Murphy 	reg_count = ARRAY_SIZE(lp8860_eeprom_disp_regs) / sizeof(lp8860_eeprom_disp_regs[0]);
2697a8685acSDan Murphy 	for (i = 0; i < reg_count; i++) {
2707a8685acSDan Murphy 		ret = regmap_write(led->eeprom_regmap,
2717a8685acSDan Murphy 				lp8860_eeprom_disp_regs[i].reg,
2727a8685acSDan Murphy 				lp8860_eeprom_disp_regs[i].value);
2737a8685acSDan Murphy 		if (ret) {
2747a8685acSDan Murphy 			dev_err(&led->client->dev, "Failed writing EEPROM\n");
2757a8685acSDan Murphy 			goto out;
2767a8685acSDan Murphy 		}
2777a8685acSDan Murphy 	}
2787a8685acSDan Murphy 
2797a8685acSDan Murphy 	ret = lp8860_unlock_eeprom(led, LP8860_LOCK_EEPROM);
2807a8685acSDan Murphy 	if (ret)
2817a8685acSDan Murphy 		goto out;
2827a8685acSDan Murphy 
2837a8685acSDan Murphy 	ret = regmap_write(led->regmap,
2847a8685acSDan Murphy 			LP8860_EEPROM_CNTRL,
2857a8685acSDan Murphy 			LP8860_PROGRAM_EEPROM);
286b12ef03aSDan Murphy 	if (ret) {
2877a8685acSDan Murphy 		dev_err(&led->client->dev, "Failed programming EEPROM\n");
288b12ef03aSDan Murphy 		goto out;
289b12ef03aSDan Murphy 	}
290b12ef03aSDan Murphy 
291b12ef03aSDan Murphy 	return ret;
292b12ef03aSDan Murphy 
2937a8685acSDan Murphy out:
2947a8685acSDan Murphy 	if (ret)
2957a8685acSDan Murphy 		gpiod_direction_output(led->enable_gpio, 0);
296b12ef03aSDan Murphy 
297b12ef03aSDan Murphy 	if (led->regulator) {
298b12ef03aSDan Murphy 		ret = regulator_disable(led->regulator);
299b12ef03aSDan Murphy 		if (ret)
300b12ef03aSDan Murphy 			dev_err(&led->client->dev,
301b12ef03aSDan Murphy 				"Failed to disable regulator\n");
302b12ef03aSDan Murphy 	}
303b12ef03aSDan Murphy 
3047a8685acSDan Murphy 	return ret;
3057a8685acSDan Murphy }
3067a8685acSDan Murphy 
307de88e71aSAxel Lin static const struct reg_default lp8860_reg_defs[] = {
3087a8685acSDan Murphy 	{ LP8860_DISP_CL1_BRT_MSB, 0x00},
3097a8685acSDan Murphy 	{ LP8860_DISP_CL1_BRT_LSB, 0x00},
3107a8685acSDan Murphy 	{ LP8860_DISP_CL1_CURR_MSB, 0x00},
3117a8685acSDan Murphy 	{ LP8860_DISP_CL1_CURR_LSB, 0x00},
3127a8685acSDan Murphy 	{ LP8860_CL2_BRT_MSB, 0x00},
3137a8685acSDan Murphy 	{ LP8860_CL2_BRT_LSB, 0x00},
3147a8685acSDan Murphy 	{ LP8860_CL2_CURRENT, 0x00},
3157a8685acSDan Murphy 	{ LP8860_CL3_BRT_MSB, 0x00},
3167a8685acSDan Murphy 	{ LP8860_CL3_BRT_LSB, 0x00},
3177a8685acSDan Murphy 	{ LP8860_CL3_CURRENT, 0x00},
3187a8685acSDan Murphy 	{ LP8860_CL4_BRT_MSB, 0x00},
3197a8685acSDan Murphy 	{ LP8860_CL4_BRT_LSB, 0x00},
3207a8685acSDan Murphy 	{ LP8860_CL4_CURRENT, 0x00},
3217a8685acSDan Murphy 	{ LP8860_CONFIG, 0x00},
3227a8685acSDan Murphy 	{ LP8860_FAULT_CLEAR, 0x00},
3237a8685acSDan Murphy 	{ LP8860_EEPROM_CNTRL, 0x80},
3247a8685acSDan Murphy 	{ LP8860_EEPROM_UNLOCK, 0x00},
3257a8685acSDan Murphy };
3267a8685acSDan Murphy 
3277a8685acSDan Murphy static const struct regmap_config lp8860_regmap_config = {
3287a8685acSDan Murphy 	.reg_bits = 8,
3297a8685acSDan Murphy 	.val_bits = 8,
3307a8685acSDan Murphy 
3317a8685acSDan Murphy 	.max_register = LP8860_EEPROM_UNLOCK,
3327a8685acSDan Murphy 	.reg_defaults = lp8860_reg_defs,
3337a8685acSDan Murphy 	.num_reg_defaults = ARRAY_SIZE(lp8860_reg_defs),
3347a8685acSDan Murphy 	.cache_type = REGCACHE_NONE,
3357a8685acSDan Murphy };
3367a8685acSDan Murphy 
337de88e71aSAxel Lin static const struct reg_default lp8860_eeprom_defs[] = {
3387a8685acSDan Murphy 	{ LP8860_EEPROM_REG_0, 0x00 },
3397a8685acSDan Murphy 	{ LP8860_EEPROM_REG_1, 0x00 },
3407a8685acSDan Murphy 	{ LP8860_EEPROM_REG_2, 0x00 },
3417a8685acSDan Murphy 	{ LP8860_EEPROM_REG_3, 0x00 },
3427a8685acSDan Murphy 	{ LP8860_EEPROM_REG_4, 0x00 },
3437a8685acSDan Murphy 	{ LP8860_EEPROM_REG_5, 0x00 },
3447a8685acSDan Murphy 	{ LP8860_EEPROM_REG_6, 0x00 },
3457a8685acSDan Murphy 	{ LP8860_EEPROM_REG_7, 0x00 },
3467a8685acSDan Murphy 	{ LP8860_EEPROM_REG_8, 0x00 },
3477a8685acSDan Murphy 	{ LP8860_EEPROM_REG_9, 0x00 },
3487a8685acSDan Murphy 	{ LP8860_EEPROM_REG_10, 0x00 },
3497a8685acSDan Murphy 	{ LP8860_EEPROM_REG_11, 0x00 },
3507a8685acSDan Murphy 	{ LP8860_EEPROM_REG_12, 0x00 },
3517a8685acSDan Murphy 	{ LP8860_EEPROM_REG_13, 0x00 },
3527a8685acSDan Murphy 	{ LP8860_EEPROM_REG_14, 0x00 },
3537a8685acSDan Murphy 	{ LP8860_EEPROM_REG_15, 0x00 },
3547a8685acSDan Murphy 	{ LP8860_EEPROM_REG_16, 0x00 },
3557a8685acSDan Murphy 	{ LP8860_EEPROM_REG_17, 0x00 },
3567a8685acSDan Murphy 	{ LP8860_EEPROM_REG_18, 0x00 },
3577a8685acSDan Murphy 	{ LP8860_EEPROM_REG_19, 0x00 },
3587a8685acSDan Murphy 	{ LP8860_EEPROM_REG_20, 0x00 },
3597a8685acSDan Murphy 	{ LP8860_EEPROM_REG_21, 0x00 },
3607a8685acSDan Murphy 	{ LP8860_EEPROM_REG_22, 0x00 },
3617a8685acSDan Murphy 	{ LP8860_EEPROM_REG_23, 0x00 },
3627a8685acSDan Murphy 	{ LP8860_EEPROM_REG_24, 0x00 },
3637a8685acSDan Murphy };
3647a8685acSDan Murphy 
3657a8685acSDan Murphy static const struct regmap_config lp8860_eeprom_regmap_config = {
3667a8685acSDan Murphy 	.reg_bits = 8,
3677a8685acSDan Murphy 	.val_bits = 8,
3687a8685acSDan Murphy 
3697a8685acSDan Murphy 	.max_register = LP8860_EEPROM_REG_24,
3707a8685acSDan Murphy 	.reg_defaults = lp8860_eeprom_defs,
3717a8685acSDan Murphy 	.num_reg_defaults = ARRAY_SIZE(lp8860_eeprom_defs),
3727a8685acSDan Murphy 	.cache_type = REGCACHE_NONE,
3737a8685acSDan Murphy };
3747a8685acSDan Murphy 
lp8860_probe(struct i2c_client * client)375*c3dab3a9SUwe Kleine-König static int lp8860_probe(struct i2c_client *client)
3767a8685acSDan Murphy {
3777a8685acSDan Murphy 	int ret;
3787a8685acSDan Murphy 	struct lp8860_led *led;
3798853c95eSMarek Behún 	struct device_node *np = dev_of_node(&client->dev);
380c6b218c9SDan Murphy 	struct device_node *child_node;
38199ca0ea5SJacek Anaszewski 	struct led_init_data init_data = {};
3827a8685acSDan Murphy 
3837a8685acSDan Murphy 	led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
3847a8685acSDan Murphy 	if (!led)
3857a8685acSDan Murphy 		return -ENOMEM;
3867a8685acSDan Murphy 
38799ca0ea5SJacek Anaszewski 	child_node = of_get_next_available_child(np, NULL);
38899ca0ea5SJacek Anaszewski 	if (!child_node)
38999ca0ea5SJacek Anaszewski 		return -EINVAL;
39099ca0ea5SJacek Anaszewski 
391eb2294c3SUwe Kleine-König 	led->enable_gpio = devm_gpiod_get_optional(&client->dev,
392eb2294c3SUwe Kleine-König 						   "enable", GPIOD_OUT_LOW);
393eb2294c3SUwe Kleine-König 	if (IS_ERR(led->enable_gpio)) {
394eb2294c3SUwe Kleine-König 		ret = PTR_ERR(led->enable_gpio);
395eb2294c3SUwe Kleine-König 		dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
396eb2294c3SUwe Kleine-König 		return ret;
397eb2294c3SUwe Kleine-König 	}
3987a8685acSDan Murphy 
3997a8685acSDan Murphy 	led->regulator = devm_regulator_get(&client->dev, "vled");
4007a8685acSDan Murphy 	if (IS_ERR(led->regulator))
4017a8685acSDan Murphy 		led->regulator = NULL;
4027a8685acSDan Murphy 
4037a8685acSDan Murphy 	led->client = client;
4047c4b10a2SAndrew Lunn 	led->led_dev.brightness_set_blocking = lp8860_brightness_set;
4057a8685acSDan Murphy 
4067a8685acSDan Murphy 	mutex_init(&led->lock);
4077a8685acSDan Murphy 
4087a8685acSDan Murphy 	i2c_set_clientdata(client, led);
4097a8685acSDan Murphy 
4107a8685acSDan Murphy 	led->regmap = devm_regmap_init_i2c(client, &lp8860_regmap_config);
4117a8685acSDan Murphy 	if (IS_ERR(led->regmap)) {
4127a8685acSDan Murphy 		ret = PTR_ERR(led->regmap);
4137a8685acSDan Murphy 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
4147a8685acSDan Murphy 			ret);
4157a8685acSDan Murphy 		return ret;
4167a8685acSDan Murphy 	}
4177a8685acSDan Murphy 
4187a8685acSDan Murphy 	led->eeprom_regmap = devm_regmap_init_i2c(client, &lp8860_eeprom_regmap_config);
4197a8685acSDan Murphy 	if (IS_ERR(led->eeprom_regmap)) {
4207a8685acSDan Murphy 		ret = PTR_ERR(led->eeprom_regmap);
4217a8685acSDan Murphy 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
4227a8685acSDan Murphy 			ret);
4237a8685acSDan Murphy 		return ret;
4247a8685acSDan Murphy 	}
4257a8685acSDan Murphy 
4267a8685acSDan Murphy 	ret = lp8860_init(led);
4277a8685acSDan Murphy 	if (ret)
4287a8685acSDan Murphy 		return ret;
4297a8685acSDan Murphy 
43099ca0ea5SJacek Anaszewski 	init_data.fwnode = of_fwnode_handle(child_node);
43199ca0ea5SJacek Anaszewski 	init_data.devicename = LP8860_NAME;
43299ca0ea5SJacek Anaszewski 	init_data.default_label = ":display_cluster";
43399ca0ea5SJacek Anaszewski 
43499ca0ea5SJacek Anaszewski 	ret = devm_led_classdev_register_ext(&client->dev, &led->led_dev,
43599ca0ea5SJacek Anaszewski 					     &init_data);
4367a8685acSDan Murphy 	if (ret) {
4377a8685acSDan Murphy 		dev_err(&client->dev, "led register err: %d\n", ret);
4387a8685acSDan Murphy 		return ret;
4397a8685acSDan Murphy 	}
4407a8685acSDan Murphy 
4417a8685acSDan Murphy 	return 0;
4427a8685acSDan Murphy }
4437a8685acSDan Murphy 
lp8860_remove(struct i2c_client * client)444ed5c2f5fSUwe Kleine-König static void lp8860_remove(struct i2c_client *client)
4457a8685acSDan Murphy {
4467a8685acSDan Murphy 	struct lp8860_led *led = i2c_get_clientdata(client);
4477a8685acSDan Murphy 	int ret;
4487a8685acSDan Murphy 
4497a8685acSDan Murphy 	gpiod_direction_output(led->enable_gpio, 0);
4507a8685acSDan Murphy 
4517a8685acSDan Murphy 	if (led->regulator) {
4527a8685acSDan Murphy 		ret = regulator_disable(led->regulator);
4537a8685acSDan Murphy 		if (ret)
4547a8685acSDan Murphy 			dev_err(&led->client->dev,
4557a8685acSDan Murphy 				"Failed to disable regulator\n");
4567a8685acSDan Murphy 	}
4577a8685acSDan Murphy 
458a2169c9bSDan Murphy 	mutex_destroy(&led->lock);
4597a8685acSDan Murphy }
4607a8685acSDan Murphy 
4617a8685acSDan Murphy static const struct i2c_device_id lp8860_id[] = {
4627a8685acSDan Murphy 	{ "lp8860", 0 },
4637a8685acSDan Murphy 	{ }
4647a8685acSDan Murphy };
4657a8685acSDan Murphy MODULE_DEVICE_TABLE(i2c, lp8860_id);
4667a8685acSDan Murphy 
4677a8685acSDan Murphy static const struct of_device_id of_lp8860_leds_match[] = {
4687a8685acSDan Murphy 	{ .compatible = "ti,lp8860", },
4697a8685acSDan Murphy 	{},
4707a8685acSDan Murphy };
4717a8685acSDan Murphy MODULE_DEVICE_TABLE(of, of_lp8860_leds_match);
4727a8685acSDan Murphy 
4737a8685acSDan Murphy static struct i2c_driver lp8860_driver = {
4747a8685acSDan Murphy 	.driver = {
4757a8685acSDan Murphy 		.name	= "lp8860",
476a2169c9bSDan Murphy 		.of_match_table = of_lp8860_leds_match,
4777a8685acSDan Murphy 	},
478*c3dab3a9SUwe Kleine-König 	.probe		= lp8860_probe,
4797a8685acSDan Murphy 	.remove		= lp8860_remove,
4807a8685acSDan Murphy 	.id_table	= lp8860_id,
4817a8685acSDan Murphy };
4827a8685acSDan Murphy module_i2c_driver(lp8860_driver);
4837a8685acSDan Murphy 
4845ee047fbSMasanari Iida MODULE_DESCRIPTION("Texas Instruments LP8860 LED driver");
4857a8685acSDan Murphy MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
486a2169c9bSDan Murphy MODULE_LICENSE("GPL v2");
487