1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29d7cffafSDavid Rivshin /*
39d7cffafSDavid Rivshin  * Driver for ISSI IS31FL32xx family of I2C LED controllers
49d7cffafSDavid Rivshin  *
59d7cffafSDavid Rivshin  * Copyright 2015 Allworx Corp.
69d7cffafSDavid Rivshin  *
7e0442d7dSDavid Rivshin  * Datasheets:
8e0442d7dSDavid Rivshin  *   http://www.issi.com/US/product-analog-fxled-driver.shtml
9e0442d7dSDavid Rivshin  *   http://www.si-en.com/product.asp?parentid=890
109d7cffafSDavid Rivshin  */
119d7cffafSDavid Rivshin 
129d7cffafSDavid Rivshin #include <linux/device.h>
139d7cffafSDavid Rivshin #include <linux/i2c.h>
149d7cffafSDavid Rivshin #include <linux/kernel.h>
159d7cffafSDavid Rivshin #include <linux/leds.h>
169d7cffafSDavid Rivshin #include <linux/module.h>
179d7cffafSDavid Rivshin #include <linux/of.h>
189d7cffafSDavid Rivshin #include <linux/of_device.h>
199d7cffafSDavid Rivshin 
209d7cffafSDavid Rivshin /* Used to indicate a device has no such register */
219d7cffafSDavid Rivshin #define IS31FL32XX_REG_NONE 0xFF
229d7cffafSDavid Rivshin 
239d7cffafSDavid Rivshin /* Software Shutdown bit in Shutdown Register */
249d7cffafSDavid Rivshin #define IS31FL32XX_SHUTDOWN_SSD_ENABLE  0
259d7cffafSDavid Rivshin #define IS31FL32XX_SHUTDOWN_SSD_DISABLE BIT(0)
269d7cffafSDavid Rivshin 
279d7cffafSDavid Rivshin /* IS31FL3216 has a number of unique registers */
289d7cffafSDavid Rivshin #define IS31FL3216_CONFIG_REG 0x00
299d7cffafSDavid Rivshin #define IS31FL3216_LIGHTING_EFFECT_REG 0x03
309d7cffafSDavid Rivshin #define IS31FL3216_CHANNEL_CONFIG_REG 0x04
319d7cffafSDavid Rivshin 
329d7cffafSDavid Rivshin /* Software Shutdown bit in 3216 Config Register */
339d7cffafSDavid Rivshin #define IS31FL3216_CONFIG_SSD_ENABLE  BIT(7)
349d7cffafSDavid Rivshin #define IS31FL3216_CONFIG_SSD_DISABLE 0
359d7cffafSDavid Rivshin 
369d7cffafSDavid Rivshin struct is31fl32xx_priv;
379d7cffafSDavid Rivshin struct is31fl32xx_led_data {
389d7cffafSDavid Rivshin 	struct led_classdev cdev;
399d7cffafSDavid Rivshin 	u8 channel; /* 1-based, max priv->cdef->channels */
409d7cffafSDavid Rivshin 	struct is31fl32xx_priv *priv;
419d7cffafSDavid Rivshin };
429d7cffafSDavid Rivshin 
439d7cffafSDavid Rivshin struct is31fl32xx_priv {
449d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef;
459d7cffafSDavid Rivshin 	struct i2c_client *client;
469d7cffafSDavid Rivshin 	unsigned int num_leds;
477a630367SGustavo A. R. Silva 	struct is31fl32xx_led_data leds[];
489d7cffafSDavid Rivshin };
499d7cffafSDavid Rivshin 
509d7cffafSDavid Rivshin /**
519d7cffafSDavid Rivshin  * struct is31fl32xx_chipdef - chip-specific attributes
529d7cffafSDavid Rivshin  * @channels            : Number of LED channels
539d7cffafSDavid Rivshin  * @shutdown_reg        : address of Shutdown register (optional)
549d7cffafSDavid Rivshin  * @pwm_update_reg      : address of PWM Update register
559d7cffafSDavid Rivshin  * @global_control_reg  : address of Global Control register (optional)
569d7cffafSDavid Rivshin  * @reset_reg           : address of Reset register (optional)
579d7cffafSDavid Rivshin  * @pwm_register_base   : address of first PWM register
589d7cffafSDavid Rivshin  * @pwm_registers_reversed: : true if PWM registers count down instead of up
599d7cffafSDavid Rivshin  * @led_control_register_base : address of first LED control register (optional)
609d7cffafSDavid Rivshin  * @enable_bits_per_led_control_register: number of LEDs enable bits in each
61bf588389SLee Jones  * @reset_func          : pointer to reset function
62bf588389SLee Jones  * @sw_shutdown_func    : pointer to software shutdown function
639d7cffafSDavid Rivshin  *
649d7cffafSDavid Rivshin  * For all optional register addresses, the sentinel value %IS31FL32XX_REG_NONE
659d7cffafSDavid Rivshin  * indicates that this chip has no such register.
669d7cffafSDavid Rivshin  *
679d7cffafSDavid Rivshin  * If non-NULL, @reset_func will be called during probing to set all
689d7cffafSDavid Rivshin  * necessary registers to a known initialization state. This is needed
699d7cffafSDavid Rivshin  * for chips that do not have a @reset_reg.
709d7cffafSDavid Rivshin  *
719d7cffafSDavid Rivshin  * @enable_bits_per_led_control_register must be >=1 if
729d7cffafSDavid Rivshin  * @led_control_register_base != %IS31FL32XX_REG_NONE.
739d7cffafSDavid Rivshin  */
749d7cffafSDavid Rivshin struct is31fl32xx_chipdef {
759d7cffafSDavid Rivshin 	u8	channels;
769d7cffafSDavid Rivshin 	u8	shutdown_reg;
779d7cffafSDavid Rivshin 	u8	pwm_update_reg;
789d7cffafSDavid Rivshin 	u8	global_control_reg;
799d7cffafSDavid Rivshin 	u8	reset_reg;
809d7cffafSDavid Rivshin 	u8	pwm_register_base;
819d7cffafSDavid Rivshin 	bool	pwm_registers_reversed;
829d7cffafSDavid Rivshin 	u8	led_control_register_base;
839d7cffafSDavid Rivshin 	u8	enable_bits_per_led_control_register;
849d7cffafSDavid Rivshin 	int (*reset_func)(struct is31fl32xx_priv *priv);
859d7cffafSDavid Rivshin 	int (*sw_shutdown_func)(struct is31fl32xx_priv *priv, bool enable);
869d7cffafSDavid Rivshin };
879d7cffafSDavid Rivshin 
889d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3236_cdef = {
899d7cffafSDavid Rivshin 	.channels				= 36,
909d7cffafSDavid Rivshin 	.shutdown_reg				= 0x00,
919d7cffafSDavid Rivshin 	.pwm_update_reg				= 0x25,
929d7cffafSDavid Rivshin 	.global_control_reg			= 0x4a,
939d7cffafSDavid Rivshin 	.reset_reg				= 0x4f,
949d7cffafSDavid Rivshin 	.pwm_register_base			= 0x01,
959d7cffafSDavid Rivshin 	.led_control_register_base		= 0x26,
969d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 1,
979d7cffafSDavid Rivshin };
989d7cffafSDavid Rivshin 
999d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3235_cdef = {
1009d7cffafSDavid Rivshin 	.channels				= 28,
1019d7cffafSDavid Rivshin 	.shutdown_reg				= 0x00,
1029d7cffafSDavid Rivshin 	.pwm_update_reg				= 0x25,
1039d7cffafSDavid Rivshin 	.global_control_reg			= 0x4a,
1049d7cffafSDavid Rivshin 	.reset_reg				= 0x4f,
1059d7cffafSDavid Rivshin 	.pwm_register_base			= 0x05,
1069d7cffafSDavid Rivshin 	.led_control_register_base		= 0x2a,
1079d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 1,
1089d7cffafSDavid Rivshin };
1099d7cffafSDavid Rivshin 
1109d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3218_cdef = {
1119d7cffafSDavid Rivshin 	.channels				= 18,
1129d7cffafSDavid Rivshin 	.shutdown_reg				= 0x00,
1139d7cffafSDavid Rivshin 	.pwm_update_reg				= 0x16,
1149d7cffafSDavid Rivshin 	.global_control_reg			= IS31FL32XX_REG_NONE,
1159d7cffafSDavid Rivshin 	.reset_reg				= 0x17,
1169d7cffafSDavid Rivshin 	.pwm_register_base			= 0x01,
1179d7cffafSDavid Rivshin 	.led_control_register_base		= 0x13,
1189d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 6,
1199d7cffafSDavid Rivshin };
1209d7cffafSDavid Rivshin 
1219d7cffafSDavid Rivshin static int is31fl3216_reset(struct is31fl32xx_priv *priv);
1229d7cffafSDavid Rivshin static int is31fl3216_software_shutdown(struct is31fl32xx_priv *priv,
1239d7cffafSDavid Rivshin 					bool enable);
1249d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3216_cdef = {
1259d7cffafSDavid Rivshin 	.channels				= 16,
1269d7cffafSDavid Rivshin 	.shutdown_reg				= IS31FL32XX_REG_NONE,
1279d7cffafSDavid Rivshin 	.pwm_update_reg				= 0xB0,
1289d7cffafSDavid Rivshin 	.global_control_reg			= IS31FL32XX_REG_NONE,
1299d7cffafSDavid Rivshin 	.reset_reg				= IS31FL32XX_REG_NONE,
1309d7cffafSDavid Rivshin 	.pwm_register_base			= 0x10,
1319d7cffafSDavid Rivshin 	.pwm_registers_reversed			= true,
1329d7cffafSDavid Rivshin 	.led_control_register_base		= 0x01,
1339d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 8,
1349d7cffafSDavid Rivshin 	.reset_func				= is31fl3216_reset,
1359d7cffafSDavid Rivshin 	.sw_shutdown_func			= is31fl3216_software_shutdown,
1369d7cffafSDavid Rivshin };
1379d7cffafSDavid Rivshin 
1389d7cffafSDavid Rivshin static int is31fl32xx_write(struct is31fl32xx_priv *priv, u8 reg, u8 val)
1399d7cffafSDavid Rivshin {
1409d7cffafSDavid Rivshin 	int ret;
1419d7cffafSDavid Rivshin 
1429d7cffafSDavid Rivshin 	dev_dbg(&priv->client->dev, "writing register 0x%02X=0x%02X", reg, val);
1439d7cffafSDavid Rivshin 
1449d7cffafSDavid Rivshin 	ret =  i2c_smbus_write_byte_data(priv->client, reg, val);
1459d7cffafSDavid Rivshin 	if (ret) {
1469d7cffafSDavid Rivshin 		dev_err(&priv->client->dev,
1479d7cffafSDavid Rivshin 			"register write to 0x%02X failed (error %d)",
1489d7cffafSDavid Rivshin 			reg, ret);
1499d7cffafSDavid Rivshin 	}
1509d7cffafSDavid Rivshin 	return ret;
1519d7cffafSDavid Rivshin }
1529d7cffafSDavid Rivshin 
1539d7cffafSDavid Rivshin /*
1549d7cffafSDavid Rivshin  * Custom reset function for IS31FL3216 because it does not have a RESET
1559d7cffafSDavid Rivshin  * register the way that the other IS31FL32xx chips do. We don't bother
1569d7cffafSDavid Rivshin  * writing the GPIO and animation registers, because the registers we
1579d7cffafSDavid Rivshin  * do write ensure those will have no effect.
1589d7cffafSDavid Rivshin  */
1599d7cffafSDavid Rivshin static int is31fl3216_reset(struct is31fl32xx_priv *priv)
1609d7cffafSDavid Rivshin {
1619d7cffafSDavid Rivshin 	unsigned int i;
1629d7cffafSDavid Rivshin 	int ret;
1639d7cffafSDavid Rivshin 
1649d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, IS31FL3216_CONFIG_REG,
1659d7cffafSDavid Rivshin 			       IS31FL3216_CONFIG_SSD_ENABLE);
1669d7cffafSDavid Rivshin 	if (ret)
1679d7cffafSDavid Rivshin 		return ret;
1689d7cffafSDavid Rivshin 	for (i = 0; i < priv->cdef->channels; i++) {
1699d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, priv->cdef->pwm_register_base+i,
1709d7cffafSDavid Rivshin 				       0x00);
1719d7cffafSDavid Rivshin 		if (ret)
1729d7cffafSDavid Rivshin 			return ret;
1739d7cffafSDavid Rivshin 	}
1749d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, priv->cdef->pwm_update_reg, 0);
1759d7cffafSDavid Rivshin 	if (ret)
1769d7cffafSDavid Rivshin 		return ret;
1779d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, IS31FL3216_LIGHTING_EFFECT_REG, 0x00);
1789d7cffafSDavid Rivshin 	if (ret)
1799d7cffafSDavid Rivshin 		return ret;
1809d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, IS31FL3216_CHANNEL_CONFIG_REG, 0x00);
1819d7cffafSDavid Rivshin 	if (ret)
1829d7cffafSDavid Rivshin 		return ret;
1839d7cffafSDavid Rivshin 
1849d7cffafSDavid Rivshin 	return 0;
1859d7cffafSDavid Rivshin }
1869d7cffafSDavid Rivshin 
1879d7cffafSDavid Rivshin /*
1889d7cffafSDavid Rivshin  * Custom Software-Shutdown function for IS31FL3216 because it does not have
1899d7cffafSDavid Rivshin  * a SHUTDOWN register the way that the other IS31FL32xx chips do.
1909d7cffafSDavid Rivshin  * We don't bother doing a read/modify/write on the CONFIG register because
1919d7cffafSDavid Rivshin  * we only ever use a value of '0' for the other fields in that register.
1929d7cffafSDavid Rivshin  */
1939d7cffafSDavid Rivshin static int is31fl3216_software_shutdown(struct is31fl32xx_priv *priv,
1949d7cffafSDavid Rivshin 					bool enable)
1959d7cffafSDavid Rivshin {
1969d7cffafSDavid Rivshin 	u8 value = enable ? IS31FL3216_CONFIG_SSD_ENABLE :
1979d7cffafSDavid Rivshin 			    IS31FL3216_CONFIG_SSD_DISABLE;
1989d7cffafSDavid Rivshin 
1999d7cffafSDavid Rivshin 	return is31fl32xx_write(priv, IS31FL3216_CONFIG_REG, value);
2009d7cffafSDavid Rivshin }
2019d7cffafSDavid Rivshin 
2029d7cffafSDavid Rivshin /*
2039d7cffafSDavid Rivshin  * NOTE: A mutex is not needed in this function because:
2049d7cffafSDavid Rivshin  * - All referenced data is read-only after probe()
2059d7cffafSDavid Rivshin  * - The I2C core has a mutex on to protect the bus
2069d7cffafSDavid Rivshin  * - There are no read/modify/write operations
2079d7cffafSDavid Rivshin  * - Intervening operations between the write of the PWM register
2089d7cffafSDavid Rivshin  *   and the Update register are harmless.
2099d7cffafSDavid Rivshin  *
2109d7cffafSDavid Rivshin  * Example:
2119d7cffafSDavid Rivshin  *	PWM_REG_1 write 16
2129d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2139d7cffafSDavid Rivshin  *	PWM_REG_2 write 128
2149d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2159d7cffafSDavid Rivshin  *   vs:
2169d7cffafSDavid Rivshin  *	PWM_REG_1 write 16
2179d7cffafSDavid Rivshin  *	PWM_REG_2 write 128
2189d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2199d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2209d7cffafSDavid Rivshin  * are equivalent. Poking the Update register merely applies all PWM
2219d7cffafSDavid Rivshin  * register writes up to that point.
2229d7cffafSDavid Rivshin  */
2239d7cffafSDavid Rivshin static int is31fl32xx_brightness_set(struct led_classdev *led_cdev,
2249d7cffafSDavid Rivshin 				     enum led_brightness brightness)
2259d7cffafSDavid Rivshin {
2269d7cffafSDavid Rivshin 	const struct is31fl32xx_led_data *led_data =
2279d7cffafSDavid Rivshin 		container_of(led_cdev, struct is31fl32xx_led_data, cdev);
2289d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = led_data->priv->cdef;
2299d7cffafSDavid Rivshin 	u8 pwm_register_offset;
2309d7cffafSDavid Rivshin 	int ret;
2319d7cffafSDavid Rivshin 
2329d7cffafSDavid Rivshin 	dev_dbg(led_cdev->dev, "%s: %d\n", __func__, brightness);
2339d7cffafSDavid Rivshin 
2349d7cffafSDavid Rivshin 	/* NOTE: led_data->channel is 1-based */
2359d7cffafSDavid Rivshin 	if (cdef->pwm_registers_reversed)
2369d7cffafSDavid Rivshin 		pwm_register_offset = cdef->channels - led_data->channel;
2379d7cffafSDavid Rivshin 	else
2389d7cffafSDavid Rivshin 		pwm_register_offset = led_data->channel - 1;
2399d7cffafSDavid Rivshin 
2409d7cffafSDavid Rivshin 	ret = is31fl32xx_write(led_data->priv,
2419d7cffafSDavid Rivshin 			       cdef->pwm_register_base + pwm_register_offset,
2429d7cffafSDavid Rivshin 			       brightness);
2439d7cffafSDavid Rivshin 	if (ret)
2449d7cffafSDavid Rivshin 		return ret;
2459d7cffafSDavid Rivshin 
2469d7cffafSDavid Rivshin 	return is31fl32xx_write(led_data->priv, cdef->pwm_update_reg, 0);
2479d7cffafSDavid Rivshin }
2489d7cffafSDavid Rivshin 
2499d7cffafSDavid Rivshin static int is31fl32xx_reset_regs(struct is31fl32xx_priv *priv)
2509d7cffafSDavid Rivshin {
2519d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = priv->cdef;
2529d7cffafSDavid Rivshin 	int ret;
2539d7cffafSDavid Rivshin 
2549d7cffafSDavid Rivshin 	if (cdef->reset_reg != IS31FL32XX_REG_NONE) {
2559d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, cdef->reset_reg, 0);
2569d7cffafSDavid Rivshin 		if (ret)
2579d7cffafSDavid Rivshin 			return ret;
2589d7cffafSDavid Rivshin 	}
2599d7cffafSDavid Rivshin 
2609d7cffafSDavid Rivshin 	if (cdef->reset_func)
2619d7cffafSDavid Rivshin 		return cdef->reset_func(priv);
2629d7cffafSDavid Rivshin 
2639d7cffafSDavid Rivshin 	return 0;
2649d7cffafSDavid Rivshin }
2659d7cffafSDavid Rivshin 
2669d7cffafSDavid Rivshin static int is31fl32xx_software_shutdown(struct is31fl32xx_priv *priv,
2679d7cffafSDavid Rivshin 					bool enable)
2689d7cffafSDavid Rivshin {
2699d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = priv->cdef;
2709d7cffafSDavid Rivshin 	int ret;
2719d7cffafSDavid Rivshin 
2729d7cffafSDavid Rivshin 	if (cdef->shutdown_reg != IS31FL32XX_REG_NONE) {
2739d7cffafSDavid Rivshin 		u8 value = enable ? IS31FL32XX_SHUTDOWN_SSD_ENABLE :
2749d7cffafSDavid Rivshin 				    IS31FL32XX_SHUTDOWN_SSD_DISABLE;
2759d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, cdef->shutdown_reg, value);
2769d7cffafSDavid Rivshin 		if (ret)
2779d7cffafSDavid Rivshin 			return ret;
2789d7cffafSDavid Rivshin 	}
2799d7cffafSDavid Rivshin 
2809d7cffafSDavid Rivshin 	if (cdef->sw_shutdown_func)
2819d7cffafSDavid Rivshin 		return cdef->sw_shutdown_func(priv, enable);
2829d7cffafSDavid Rivshin 
2839d7cffafSDavid Rivshin 	return 0;
2849d7cffafSDavid Rivshin }
2859d7cffafSDavid Rivshin 
2869d7cffafSDavid Rivshin static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)
2879d7cffafSDavid Rivshin {
2889d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = priv->cdef;
2899d7cffafSDavid Rivshin 	int ret;
2909d7cffafSDavid Rivshin 
2919d7cffafSDavid Rivshin 	ret = is31fl32xx_reset_regs(priv);
2929d7cffafSDavid Rivshin 	if (ret)
2939d7cffafSDavid Rivshin 		return ret;
2949d7cffafSDavid Rivshin 
2959d7cffafSDavid Rivshin 	/*
2969d7cffafSDavid Rivshin 	 * Set enable bit for all channels.
2979d7cffafSDavid Rivshin 	 * We will control state with PWM registers alone.
2989d7cffafSDavid Rivshin 	 */
2999d7cffafSDavid Rivshin 	if (cdef->led_control_register_base != IS31FL32XX_REG_NONE) {
3009d7cffafSDavid Rivshin 		u8 value =
3019d7cffafSDavid Rivshin 		    GENMASK(cdef->enable_bits_per_led_control_register-1, 0);
3029d7cffafSDavid Rivshin 		u8 num_regs = cdef->channels /
3039d7cffafSDavid Rivshin 				cdef->enable_bits_per_led_control_register;
3049d7cffafSDavid Rivshin 		int i;
3059d7cffafSDavid Rivshin 
3069d7cffafSDavid Rivshin 		for (i = 0; i < num_regs; i++) {
3079d7cffafSDavid Rivshin 			ret = is31fl32xx_write(priv,
3089d7cffafSDavid Rivshin 					       cdef->led_control_register_base+i,
3099d7cffafSDavid Rivshin 					       value);
3109d7cffafSDavid Rivshin 			if (ret)
3119d7cffafSDavid Rivshin 				return ret;
3129d7cffafSDavid Rivshin 		}
3139d7cffafSDavid Rivshin 	}
3149d7cffafSDavid Rivshin 
3159d7cffafSDavid Rivshin 	ret = is31fl32xx_software_shutdown(priv, false);
3169d7cffafSDavid Rivshin 	if (ret)
3179d7cffafSDavid Rivshin 		return ret;
3189d7cffafSDavid Rivshin 
3199d7cffafSDavid Rivshin 	if (cdef->global_control_reg != IS31FL32XX_REG_NONE) {
3209d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, cdef->global_control_reg, 0x00);
3219d7cffafSDavid Rivshin 		if (ret)
3229d7cffafSDavid Rivshin 			return ret;
3239d7cffafSDavid Rivshin 	}
3249d7cffafSDavid Rivshin 
3259d7cffafSDavid Rivshin 	return 0;
3269d7cffafSDavid Rivshin }
3279d7cffafSDavid Rivshin 
3289d7cffafSDavid Rivshin static int is31fl32xx_parse_child_dt(const struct device *dev,
3299d7cffafSDavid Rivshin 				     const struct device_node *child,
3309d7cffafSDavid Rivshin 				     struct is31fl32xx_led_data *led_data)
3319d7cffafSDavid Rivshin {
3329d7cffafSDavid Rivshin 	struct led_classdev *cdev = &led_data->cdev;
3339d7cffafSDavid Rivshin 	int ret = 0;
3349d7cffafSDavid Rivshin 	u32 reg;
3359d7cffafSDavid Rivshin 
3369d7cffafSDavid Rivshin 	ret = of_property_read_u32(child, "reg", &reg);
3379d7cffafSDavid Rivshin 	if (ret || reg < 1 || reg > led_data->priv->cdef->channels) {
3389d7cffafSDavid Rivshin 		dev_err(dev,
3390571753eSRob Herring 			"Child node %pOF does not have a valid reg property\n",
3400571753eSRob Herring 			child);
3419d7cffafSDavid Rivshin 		return -EINVAL;
3429d7cffafSDavid Rivshin 	}
3439d7cffafSDavid Rivshin 	led_data->channel = reg;
3449d7cffafSDavid Rivshin 
3459d7cffafSDavid Rivshin 	cdev->brightness_set_blocking = is31fl32xx_brightness_set;
3469d7cffafSDavid Rivshin 
3479d7cffafSDavid Rivshin 	return 0;
3489d7cffafSDavid Rivshin }
3499d7cffafSDavid Rivshin 
3509d7cffafSDavid Rivshin static struct is31fl32xx_led_data *is31fl32xx_find_led_data(
3519d7cffafSDavid Rivshin 					struct is31fl32xx_priv *priv,
3529d7cffafSDavid Rivshin 					u8 channel)
3539d7cffafSDavid Rivshin {
3549d7cffafSDavid Rivshin 	size_t i;
3559d7cffafSDavid Rivshin 
3569d7cffafSDavid Rivshin 	for (i = 0; i < priv->num_leds; i++) {
3579d7cffafSDavid Rivshin 		if (priv->leds[i].channel == channel)
3589d7cffafSDavid Rivshin 			return &priv->leds[i];
3599d7cffafSDavid Rivshin 	}
3609d7cffafSDavid Rivshin 
3619d7cffafSDavid Rivshin 	return NULL;
3629d7cffafSDavid Rivshin }
3639d7cffafSDavid Rivshin 
3649d7cffafSDavid Rivshin static int is31fl32xx_parse_dt(struct device *dev,
3659d7cffafSDavid Rivshin 			       struct is31fl32xx_priv *priv)
3669d7cffafSDavid Rivshin {
3679d7cffafSDavid Rivshin 	struct device_node *child;
3689d7cffafSDavid Rivshin 	int ret = 0;
3699d7cffafSDavid Rivshin 
37099a013c8SMarek Behún 	for_each_available_child_of_node(dev_of_node(dev), child) {
3716be2030dSMarek Behún 		struct led_init_data init_data = {};
3729d7cffafSDavid Rivshin 		struct is31fl32xx_led_data *led_data =
3739d7cffafSDavid Rivshin 			&priv->leds[priv->num_leds];
3749d7cffafSDavid Rivshin 		const struct is31fl32xx_led_data *other_led_data;
3759d7cffafSDavid Rivshin 
3769d7cffafSDavid Rivshin 		led_data->priv = priv;
3779d7cffafSDavid Rivshin 
3789d7cffafSDavid Rivshin 		ret = is31fl32xx_parse_child_dt(dev, child, led_data);
3799d7cffafSDavid Rivshin 		if (ret)
3809d7cffafSDavid Rivshin 			goto err;
3819d7cffafSDavid Rivshin 
3829d7cffafSDavid Rivshin 		/* Detect if channel is already in use by another child */
3839d7cffafSDavid Rivshin 		other_led_data = is31fl32xx_find_led_data(priv,
3849d7cffafSDavid Rivshin 							  led_data->channel);
3859d7cffafSDavid Rivshin 		if (other_led_data) {
3869d7cffafSDavid Rivshin 			dev_err(dev,
3876be2030dSMarek Behún 				"Node %pOF 'reg' conflicts with another LED\n",
3886be2030dSMarek Behún 				child);
389e6421975SJiapeng Chong 			ret = -EINVAL;
3909d7cffafSDavid Rivshin 			goto err;
3919d7cffafSDavid Rivshin 		}
3929d7cffafSDavid Rivshin 
3936be2030dSMarek Behún 		init_data.fwnode = of_fwnode_handle(child);
3946be2030dSMarek Behún 
3956be2030dSMarek Behún 		ret = devm_led_classdev_register_ext(dev, &led_data->cdev,
3966be2030dSMarek Behún 						     &init_data);
3979d7cffafSDavid Rivshin 		if (ret) {
3986be2030dSMarek Behún 			dev_err(dev, "Failed to register LED for %pOF: %d\n",
3996be2030dSMarek Behún 				child, ret);
4009d7cffafSDavid Rivshin 			goto err;
4019d7cffafSDavid Rivshin 		}
4029d7cffafSDavid Rivshin 
4039d7cffafSDavid Rivshin 		priv->num_leds++;
4049d7cffafSDavid Rivshin 	}
4059d7cffafSDavid Rivshin 
4069d7cffafSDavid Rivshin 	return 0;
4079d7cffafSDavid Rivshin 
4089d7cffafSDavid Rivshin err:
4099d7cffafSDavid Rivshin 	of_node_put(child);
4109d7cffafSDavid Rivshin 	return ret;
4119d7cffafSDavid Rivshin }
4129d7cffafSDavid Rivshin 
4131d991b71SH. Nikolaus Schaller static const struct of_device_id of_is31fl32xx_match[] = {
4149d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3236", .data = &is31fl3236_cdef, },
4159d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3235", .data = &is31fl3235_cdef, },
4169d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3218", .data = &is31fl3218_cdef, },
417e0442d7dSDavid Rivshin 	{ .compatible = "si-en,sn3218",    .data = &is31fl3218_cdef, },
4189d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3216", .data = &is31fl3216_cdef, },
419e0442d7dSDavid Rivshin 	{ .compatible = "si-en,sn3216",    .data = &is31fl3216_cdef, },
4209d7cffafSDavid Rivshin 	{},
4219d7cffafSDavid Rivshin };
4229d7cffafSDavid Rivshin 
4231d991b71SH. Nikolaus Schaller MODULE_DEVICE_TABLE(of, of_is31fl32xx_match);
4249d7cffafSDavid Rivshin 
4259d7cffafSDavid Rivshin static int is31fl32xx_probe(struct i2c_client *client,
4269d7cffafSDavid Rivshin 			    const struct i2c_device_id *id)
4279d7cffafSDavid Rivshin {
4289d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef;
4299d7cffafSDavid Rivshin 	struct device *dev = &client->dev;
4309d7cffafSDavid Rivshin 	struct is31fl32xx_priv *priv;
4319d7cffafSDavid Rivshin 	int count;
4329d7cffafSDavid Rivshin 	int ret = 0;
4339d7cffafSDavid Rivshin 
4342779f472SMarek Behún 	cdef = device_get_match_data(dev);
4359d7cffafSDavid Rivshin 
43699a013c8SMarek Behún 	count = of_get_available_child_count(dev_of_node(dev));
4379d7cffafSDavid Rivshin 	if (!count)
4389d7cffafSDavid Rivshin 		return -EINVAL;
4399d7cffafSDavid Rivshin 
4401669ec78SGustavo A. R. Silva 	priv = devm_kzalloc(dev, struct_size(priv, leds, count),
4419d7cffafSDavid Rivshin 			    GFP_KERNEL);
4429d7cffafSDavid Rivshin 	if (!priv)
4439d7cffafSDavid Rivshin 		return -ENOMEM;
4449d7cffafSDavid Rivshin 
4459d7cffafSDavid Rivshin 	priv->client = client;
4469d7cffafSDavid Rivshin 	priv->cdef = cdef;
4479d7cffafSDavid Rivshin 	i2c_set_clientdata(client, priv);
4489d7cffafSDavid Rivshin 
4499d7cffafSDavid Rivshin 	ret = is31fl32xx_init_regs(priv);
4509d7cffafSDavid Rivshin 	if (ret)
4519d7cffafSDavid Rivshin 		return ret;
4529d7cffafSDavid Rivshin 
4539d7cffafSDavid Rivshin 	ret = is31fl32xx_parse_dt(dev, priv);
4549d7cffafSDavid Rivshin 	if (ret)
4559d7cffafSDavid Rivshin 		return ret;
4569d7cffafSDavid Rivshin 
4579d7cffafSDavid Rivshin 	return 0;
4589d7cffafSDavid Rivshin }
4599d7cffafSDavid Rivshin 
460*ed5c2f5fSUwe Kleine-König static void is31fl32xx_remove(struct i2c_client *client)
4619d7cffafSDavid Rivshin {
4629d7cffafSDavid Rivshin 	struct is31fl32xx_priv *priv = i2c_get_clientdata(client);
463e1347bd4SUwe Kleine-König 	int ret;
4649d7cffafSDavid Rivshin 
465e1347bd4SUwe Kleine-König 	ret = is31fl32xx_reset_regs(priv);
466e1347bd4SUwe Kleine-König 	if (ret)
467e1347bd4SUwe Kleine-König 		dev_err(&client->dev, "Failed to reset registers on removal (%pe)\n",
468e1347bd4SUwe Kleine-König 			ERR_PTR(ret));
4699d7cffafSDavid Rivshin }
4709d7cffafSDavid Rivshin 
4719d7cffafSDavid Rivshin /*
4725706c01fSH. Nikolaus Schaller  * i2c-core (and modalias) requires that id_table be properly filled,
4735706c01fSH. Nikolaus Schaller  * even though it is not used for DeviceTree based instantiation.
4749d7cffafSDavid Rivshin  */
4751d991b71SH. Nikolaus Schaller static const struct i2c_device_id is31fl32xx_id[] = {
4765706c01fSH. Nikolaus Schaller 	{ "is31fl3236" },
4775706c01fSH. Nikolaus Schaller 	{ "is31fl3235" },
4785706c01fSH. Nikolaus Schaller 	{ "is31fl3218" },
4795706c01fSH. Nikolaus Schaller 	{ "sn3218" },
4805706c01fSH. Nikolaus Schaller 	{ "is31fl3216" },
4815706c01fSH. Nikolaus Schaller 	{ "sn3216" },
4829d7cffafSDavid Rivshin 	{},
4839d7cffafSDavid Rivshin };
4849d7cffafSDavid Rivshin 
4851d991b71SH. Nikolaus Schaller MODULE_DEVICE_TABLE(i2c, is31fl32xx_id);
4869d7cffafSDavid Rivshin 
4879d7cffafSDavid Rivshin static struct i2c_driver is31fl32xx_driver = {
4889d7cffafSDavid Rivshin 	.driver = {
4899d7cffafSDavid Rivshin 		.name	= "is31fl32xx",
4901d991b71SH. Nikolaus Schaller 		.of_match_table = of_is31fl32xx_match,
4919d7cffafSDavid Rivshin 	},
4929d7cffafSDavid Rivshin 	.probe		= is31fl32xx_probe,
4939d7cffafSDavid Rivshin 	.remove		= is31fl32xx_remove,
4941d991b71SH. Nikolaus Schaller 	.id_table	= is31fl32xx_id,
4959d7cffafSDavid Rivshin };
4969d7cffafSDavid Rivshin 
4979d7cffafSDavid Rivshin module_i2c_driver(is31fl32xx_driver);
4989d7cffafSDavid Rivshin 
4999d7cffafSDavid Rivshin MODULE_AUTHOR("David Rivshin <drivshin@allworx.com>");
5009d7cffafSDavid Rivshin MODULE_DESCRIPTION("ISSI IS31FL32xx LED driver");
5019d7cffafSDavid Rivshin MODULE_LICENSE("GPL v2");
502