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