19d7cffafSDavid Rivshin /*
29d7cffafSDavid Rivshin  * Driver for ISSI IS31FL32xx family of I2C LED controllers
39d7cffafSDavid Rivshin  *
49d7cffafSDavid Rivshin  * Copyright 2015 Allworx Corp.
59d7cffafSDavid Rivshin  *
69d7cffafSDavid Rivshin  *
79d7cffafSDavid Rivshin  * This program is free software; you can redistribute it and/or modify
89d7cffafSDavid Rivshin  * it under the terms of the GNU General Public License version 2 as
99d7cffafSDavid Rivshin  * published by the Free Software Foundation.
109d7cffafSDavid Rivshin  *
11e0442d7dSDavid Rivshin  * Datasheets:
12e0442d7dSDavid Rivshin  *   http://www.issi.com/US/product-analog-fxled-driver.shtml
13e0442d7dSDavid Rivshin  *   http://www.si-en.com/product.asp?parentid=890
149d7cffafSDavid Rivshin  */
159d7cffafSDavid Rivshin 
169d7cffafSDavid Rivshin #include <linux/device.h>
179d7cffafSDavid Rivshin #include <linux/i2c.h>
189d7cffafSDavid Rivshin #include <linux/kernel.h>
199d7cffafSDavid Rivshin #include <linux/leds.h>
209d7cffafSDavid Rivshin #include <linux/module.h>
219d7cffafSDavid Rivshin #include <linux/of.h>
229d7cffafSDavid Rivshin #include <linux/of_device.h>
239d7cffafSDavid Rivshin 
249d7cffafSDavid Rivshin /* Used to indicate a device has no such register */
259d7cffafSDavid Rivshin #define IS31FL32XX_REG_NONE 0xFF
269d7cffafSDavid Rivshin 
279d7cffafSDavid Rivshin /* Software Shutdown bit in Shutdown Register */
289d7cffafSDavid Rivshin #define IS31FL32XX_SHUTDOWN_SSD_ENABLE  0
299d7cffafSDavid Rivshin #define IS31FL32XX_SHUTDOWN_SSD_DISABLE BIT(0)
309d7cffafSDavid Rivshin 
319d7cffafSDavid Rivshin /* IS31FL3216 has a number of unique registers */
329d7cffafSDavid Rivshin #define IS31FL3216_CONFIG_REG 0x00
339d7cffafSDavid Rivshin #define IS31FL3216_LIGHTING_EFFECT_REG 0x03
349d7cffafSDavid Rivshin #define IS31FL3216_CHANNEL_CONFIG_REG 0x04
359d7cffafSDavid Rivshin 
369d7cffafSDavid Rivshin /* Software Shutdown bit in 3216 Config Register */
379d7cffafSDavid Rivshin #define IS31FL3216_CONFIG_SSD_ENABLE  BIT(7)
389d7cffafSDavid Rivshin #define IS31FL3216_CONFIG_SSD_DISABLE 0
399d7cffafSDavid Rivshin 
409d7cffafSDavid Rivshin struct is31fl32xx_priv;
419d7cffafSDavid Rivshin struct is31fl32xx_led_data {
429d7cffafSDavid Rivshin 	struct led_classdev cdev;
439d7cffafSDavid Rivshin 	u8 channel; /* 1-based, max priv->cdef->channels */
449d7cffafSDavid Rivshin 	struct is31fl32xx_priv *priv;
459d7cffafSDavid Rivshin };
469d7cffafSDavid Rivshin 
479d7cffafSDavid Rivshin struct is31fl32xx_priv {
489d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef;
499d7cffafSDavid Rivshin 	struct i2c_client *client;
509d7cffafSDavid Rivshin 	unsigned int num_leds;
519d7cffafSDavid Rivshin 	struct is31fl32xx_led_data leds[0];
529d7cffafSDavid Rivshin };
539d7cffafSDavid Rivshin 
549d7cffafSDavid Rivshin /**
559d7cffafSDavid Rivshin  * struct is31fl32xx_chipdef - chip-specific attributes
569d7cffafSDavid Rivshin  * @channels            : Number of LED channels
579d7cffafSDavid Rivshin  * @shutdown_reg        : address of Shutdown register (optional)
589d7cffafSDavid Rivshin  * @pwm_update_reg      : address of PWM Update register
599d7cffafSDavid Rivshin  * @global_control_reg  : address of Global Control register (optional)
609d7cffafSDavid Rivshin  * @reset_reg           : address of Reset register (optional)
619d7cffafSDavid Rivshin  * @pwm_register_base   : address of first PWM register
629d7cffafSDavid Rivshin  * @pwm_registers_reversed: : true if PWM registers count down instead of up
639d7cffafSDavid Rivshin  * @led_control_register_base : address of first LED control register (optional)
649d7cffafSDavid Rivshin  * @enable_bits_per_led_control_register: number of LEDs enable bits in each
659d7cffafSDavid Rivshin  * @reset_func:         : pointer to reset function
669d7cffafSDavid Rivshin  *
679d7cffafSDavid Rivshin  * For all optional register addresses, the sentinel value %IS31FL32XX_REG_NONE
689d7cffafSDavid Rivshin  * indicates that this chip has no such register.
699d7cffafSDavid Rivshin  *
709d7cffafSDavid Rivshin  * If non-NULL, @reset_func will be called during probing to set all
719d7cffafSDavid Rivshin  * necessary registers to a known initialization state. This is needed
729d7cffafSDavid Rivshin  * for chips that do not have a @reset_reg.
739d7cffafSDavid Rivshin  *
749d7cffafSDavid Rivshin  * @enable_bits_per_led_control_register must be >=1 if
759d7cffafSDavid Rivshin  * @led_control_register_base != %IS31FL32XX_REG_NONE.
769d7cffafSDavid Rivshin  */
779d7cffafSDavid Rivshin struct is31fl32xx_chipdef {
789d7cffafSDavid Rivshin 	u8	channels;
799d7cffafSDavid Rivshin 	u8	shutdown_reg;
809d7cffafSDavid Rivshin 	u8	pwm_update_reg;
819d7cffafSDavid Rivshin 	u8	global_control_reg;
829d7cffafSDavid Rivshin 	u8	reset_reg;
839d7cffafSDavid Rivshin 	u8	pwm_register_base;
849d7cffafSDavid Rivshin 	bool	pwm_registers_reversed;
859d7cffafSDavid Rivshin 	u8	led_control_register_base;
869d7cffafSDavid Rivshin 	u8	enable_bits_per_led_control_register;
879d7cffafSDavid Rivshin 	int (*reset_func)(struct is31fl32xx_priv *priv);
889d7cffafSDavid Rivshin 	int (*sw_shutdown_func)(struct is31fl32xx_priv *priv, bool enable);
899d7cffafSDavid Rivshin };
909d7cffafSDavid Rivshin 
919d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3236_cdef = {
929d7cffafSDavid Rivshin 	.channels				= 36,
939d7cffafSDavid Rivshin 	.shutdown_reg				= 0x00,
949d7cffafSDavid Rivshin 	.pwm_update_reg				= 0x25,
959d7cffafSDavid Rivshin 	.global_control_reg			= 0x4a,
969d7cffafSDavid Rivshin 	.reset_reg				= 0x4f,
979d7cffafSDavid Rivshin 	.pwm_register_base			= 0x01,
989d7cffafSDavid Rivshin 	.led_control_register_base		= 0x26,
999d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 1,
1009d7cffafSDavid Rivshin };
1019d7cffafSDavid Rivshin 
1029d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3235_cdef = {
1039d7cffafSDavid Rivshin 	.channels				= 28,
1049d7cffafSDavid Rivshin 	.shutdown_reg				= 0x00,
1059d7cffafSDavid Rivshin 	.pwm_update_reg				= 0x25,
1069d7cffafSDavid Rivshin 	.global_control_reg			= 0x4a,
1079d7cffafSDavid Rivshin 	.reset_reg				= 0x4f,
1089d7cffafSDavid Rivshin 	.pwm_register_base			= 0x05,
1099d7cffafSDavid Rivshin 	.led_control_register_base		= 0x2a,
1109d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 1,
1119d7cffafSDavid Rivshin };
1129d7cffafSDavid Rivshin 
1139d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3218_cdef = {
1149d7cffafSDavid Rivshin 	.channels				= 18,
1159d7cffafSDavid Rivshin 	.shutdown_reg				= 0x00,
1169d7cffafSDavid Rivshin 	.pwm_update_reg				= 0x16,
1179d7cffafSDavid Rivshin 	.global_control_reg			= IS31FL32XX_REG_NONE,
1189d7cffafSDavid Rivshin 	.reset_reg				= 0x17,
1199d7cffafSDavid Rivshin 	.pwm_register_base			= 0x01,
1209d7cffafSDavid Rivshin 	.led_control_register_base		= 0x13,
1219d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 6,
1229d7cffafSDavid Rivshin };
1239d7cffafSDavid Rivshin 
1249d7cffafSDavid Rivshin static int is31fl3216_reset(struct is31fl32xx_priv *priv);
1259d7cffafSDavid Rivshin static int is31fl3216_software_shutdown(struct is31fl32xx_priv *priv,
1269d7cffafSDavid Rivshin 					bool enable);
1279d7cffafSDavid Rivshin static const struct is31fl32xx_chipdef is31fl3216_cdef = {
1289d7cffafSDavid Rivshin 	.channels				= 16,
1299d7cffafSDavid Rivshin 	.shutdown_reg				= IS31FL32XX_REG_NONE,
1309d7cffafSDavid Rivshin 	.pwm_update_reg				= 0xB0,
1319d7cffafSDavid Rivshin 	.global_control_reg			= IS31FL32XX_REG_NONE,
1329d7cffafSDavid Rivshin 	.reset_reg				= IS31FL32XX_REG_NONE,
1339d7cffafSDavid Rivshin 	.pwm_register_base			= 0x10,
1349d7cffafSDavid Rivshin 	.pwm_registers_reversed			= true,
1359d7cffafSDavid Rivshin 	.led_control_register_base		= 0x01,
1369d7cffafSDavid Rivshin 	.enable_bits_per_led_control_register	= 8,
1379d7cffafSDavid Rivshin 	.reset_func				= is31fl3216_reset,
1389d7cffafSDavid Rivshin 	.sw_shutdown_func			= is31fl3216_software_shutdown,
1399d7cffafSDavid Rivshin };
1409d7cffafSDavid Rivshin 
1419d7cffafSDavid Rivshin static int is31fl32xx_write(struct is31fl32xx_priv *priv, u8 reg, u8 val)
1429d7cffafSDavid Rivshin {
1439d7cffafSDavid Rivshin 	int ret;
1449d7cffafSDavid Rivshin 
1459d7cffafSDavid Rivshin 	dev_dbg(&priv->client->dev, "writing register 0x%02X=0x%02X", reg, val);
1469d7cffafSDavid Rivshin 
1479d7cffafSDavid Rivshin 	ret =  i2c_smbus_write_byte_data(priv->client, reg, val);
1489d7cffafSDavid Rivshin 	if (ret) {
1499d7cffafSDavid Rivshin 		dev_err(&priv->client->dev,
1509d7cffafSDavid Rivshin 			"register write to 0x%02X failed (error %d)",
1519d7cffafSDavid Rivshin 			reg, ret);
1529d7cffafSDavid Rivshin 	}
1539d7cffafSDavid Rivshin 	return ret;
1549d7cffafSDavid Rivshin }
1559d7cffafSDavid Rivshin 
1569d7cffafSDavid Rivshin /*
1579d7cffafSDavid Rivshin  * Custom reset function for IS31FL3216 because it does not have a RESET
1589d7cffafSDavid Rivshin  * register the way that the other IS31FL32xx chips do. We don't bother
1599d7cffafSDavid Rivshin  * writing the GPIO and animation registers, because the registers we
1609d7cffafSDavid Rivshin  * do write ensure those will have no effect.
1619d7cffafSDavid Rivshin  */
1629d7cffafSDavid Rivshin static int is31fl3216_reset(struct is31fl32xx_priv *priv)
1639d7cffafSDavid Rivshin {
1649d7cffafSDavid Rivshin 	unsigned int i;
1659d7cffafSDavid Rivshin 	int ret;
1669d7cffafSDavid Rivshin 
1679d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, IS31FL3216_CONFIG_REG,
1689d7cffafSDavid Rivshin 			       IS31FL3216_CONFIG_SSD_ENABLE);
1699d7cffafSDavid Rivshin 	if (ret)
1709d7cffafSDavid Rivshin 		return ret;
1719d7cffafSDavid Rivshin 	for (i = 0; i < priv->cdef->channels; i++) {
1729d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, priv->cdef->pwm_register_base+i,
1739d7cffafSDavid Rivshin 				       0x00);
1749d7cffafSDavid Rivshin 		if (ret)
1759d7cffafSDavid Rivshin 			return ret;
1769d7cffafSDavid Rivshin 	}
1779d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, priv->cdef->pwm_update_reg, 0);
1789d7cffafSDavid Rivshin 	if (ret)
1799d7cffafSDavid Rivshin 		return ret;
1809d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, IS31FL3216_LIGHTING_EFFECT_REG, 0x00);
1819d7cffafSDavid Rivshin 	if (ret)
1829d7cffafSDavid Rivshin 		return ret;
1839d7cffafSDavid Rivshin 	ret = is31fl32xx_write(priv, IS31FL3216_CHANNEL_CONFIG_REG, 0x00);
1849d7cffafSDavid Rivshin 	if (ret)
1859d7cffafSDavid Rivshin 		return ret;
1869d7cffafSDavid Rivshin 
1879d7cffafSDavid Rivshin 	return 0;
1889d7cffafSDavid Rivshin }
1899d7cffafSDavid Rivshin 
1909d7cffafSDavid Rivshin /*
1919d7cffafSDavid Rivshin  * Custom Software-Shutdown function for IS31FL3216 because it does not have
1929d7cffafSDavid Rivshin  * a SHUTDOWN register the way that the other IS31FL32xx chips do.
1939d7cffafSDavid Rivshin  * We don't bother doing a read/modify/write on the CONFIG register because
1949d7cffafSDavid Rivshin  * we only ever use a value of '0' for the other fields in that register.
1959d7cffafSDavid Rivshin  */
1969d7cffafSDavid Rivshin static int is31fl3216_software_shutdown(struct is31fl32xx_priv *priv,
1979d7cffafSDavid Rivshin 					bool enable)
1989d7cffafSDavid Rivshin {
1999d7cffafSDavid Rivshin 	u8 value = enable ? IS31FL3216_CONFIG_SSD_ENABLE :
2009d7cffafSDavid Rivshin 			    IS31FL3216_CONFIG_SSD_DISABLE;
2019d7cffafSDavid Rivshin 
2029d7cffafSDavid Rivshin 	return is31fl32xx_write(priv, IS31FL3216_CONFIG_REG, value);
2039d7cffafSDavid Rivshin }
2049d7cffafSDavid Rivshin 
2059d7cffafSDavid Rivshin /*
2069d7cffafSDavid Rivshin  * NOTE: A mutex is not needed in this function because:
2079d7cffafSDavid Rivshin  * - All referenced data is read-only after probe()
2089d7cffafSDavid Rivshin  * - The I2C core has a mutex on to protect the bus
2099d7cffafSDavid Rivshin  * - There are no read/modify/write operations
2109d7cffafSDavid Rivshin  * - Intervening operations between the write of the PWM register
2119d7cffafSDavid Rivshin  *   and the Update register are harmless.
2129d7cffafSDavid Rivshin  *
2139d7cffafSDavid Rivshin  * Example:
2149d7cffafSDavid Rivshin  *	PWM_REG_1 write 16
2159d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2169d7cffafSDavid Rivshin  *	PWM_REG_2 write 128
2179d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2189d7cffafSDavid Rivshin  *   vs:
2199d7cffafSDavid Rivshin  *	PWM_REG_1 write 16
2209d7cffafSDavid Rivshin  *	PWM_REG_2 write 128
2219d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2229d7cffafSDavid Rivshin  *	UPDATE_REG write 0
2239d7cffafSDavid Rivshin  * are equivalent. Poking the Update register merely applies all PWM
2249d7cffafSDavid Rivshin  * register writes up to that point.
2259d7cffafSDavid Rivshin  */
2269d7cffafSDavid Rivshin static int is31fl32xx_brightness_set(struct led_classdev *led_cdev,
2279d7cffafSDavid Rivshin 				     enum led_brightness brightness)
2289d7cffafSDavid Rivshin {
2299d7cffafSDavid Rivshin 	const struct is31fl32xx_led_data *led_data =
2309d7cffafSDavid Rivshin 		container_of(led_cdev, struct is31fl32xx_led_data, cdev);
2319d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = led_data->priv->cdef;
2329d7cffafSDavid Rivshin 	u8 pwm_register_offset;
2339d7cffafSDavid Rivshin 	int ret;
2349d7cffafSDavid Rivshin 
2359d7cffafSDavid Rivshin 	dev_dbg(led_cdev->dev, "%s: %d\n", __func__, brightness);
2369d7cffafSDavid Rivshin 
2379d7cffafSDavid Rivshin 	/* NOTE: led_data->channel is 1-based */
2389d7cffafSDavid Rivshin 	if (cdef->pwm_registers_reversed)
2399d7cffafSDavid Rivshin 		pwm_register_offset = cdef->channels - led_data->channel;
2409d7cffafSDavid Rivshin 	else
2419d7cffafSDavid Rivshin 		pwm_register_offset = led_data->channel - 1;
2429d7cffafSDavid Rivshin 
2439d7cffafSDavid Rivshin 	ret = is31fl32xx_write(led_data->priv,
2449d7cffafSDavid Rivshin 			       cdef->pwm_register_base + pwm_register_offset,
2459d7cffafSDavid Rivshin 			       brightness);
2469d7cffafSDavid Rivshin 	if (ret)
2479d7cffafSDavid Rivshin 		return ret;
2489d7cffafSDavid Rivshin 
2499d7cffafSDavid Rivshin 	return is31fl32xx_write(led_data->priv, cdef->pwm_update_reg, 0);
2509d7cffafSDavid Rivshin }
2519d7cffafSDavid Rivshin 
2529d7cffafSDavid Rivshin static int is31fl32xx_reset_regs(struct is31fl32xx_priv *priv)
2539d7cffafSDavid Rivshin {
2549d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = priv->cdef;
2559d7cffafSDavid Rivshin 	int ret;
2569d7cffafSDavid Rivshin 
2579d7cffafSDavid Rivshin 	if (cdef->reset_reg != IS31FL32XX_REG_NONE) {
2589d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, cdef->reset_reg, 0);
2599d7cffafSDavid Rivshin 		if (ret)
2609d7cffafSDavid Rivshin 			return ret;
2619d7cffafSDavid Rivshin 	}
2629d7cffafSDavid Rivshin 
2639d7cffafSDavid Rivshin 	if (cdef->reset_func)
2649d7cffafSDavid Rivshin 		return cdef->reset_func(priv);
2659d7cffafSDavid Rivshin 
2669d7cffafSDavid Rivshin 	return 0;
2679d7cffafSDavid Rivshin }
2689d7cffafSDavid Rivshin 
2699d7cffafSDavid Rivshin static int is31fl32xx_software_shutdown(struct is31fl32xx_priv *priv,
2709d7cffafSDavid Rivshin 					bool enable)
2719d7cffafSDavid Rivshin {
2729d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = priv->cdef;
2739d7cffafSDavid Rivshin 	int ret;
2749d7cffafSDavid Rivshin 
2759d7cffafSDavid Rivshin 	if (cdef->shutdown_reg != IS31FL32XX_REG_NONE) {
2769d7cffafSDavid Rivshin 		u8 value = enable ? IS31FL32XX_SHUTDOWN_SSD_ENABLE :
2779d7cffafSDavid Rivshin 				    IS31FL32XX_SHUTDOWN_SSD_DISABLE;
2789d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, cdef->shutdown_reg, value);
2799d7cffafSDavid Rivshin 		if (ret)
2809d7cffafSDavid Rivshin 			return ret;
2819d7cffafSDavid Rivshin 	}
2829d7cffafSDavid Rivshin 
2839d7cffafSDavid Rivshin 	if (cdef->sw_shutdown_func)
2849d7cffafSDavid Rivshin 		return cdef->sw_shutdown_func(priv, enable);
2859d7cffafSDavid Rivshin 
2869d7cffafSDavid Rivshin 	return 0;
2879d7cffafSDavid Rivshin }
2889d7cffafSDavid Rivshin 
2899d7cffafSDavid Rivshin static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)
2909d7cffafSDavid Rivshin {
2919d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef = priv->cdef;
2929d7cffafSDavid Rivshin 	int ret;
2939d7cffafSDavid Rivshin 
2949d7cffafSDavid Rivshin 	ret = is31fl32xx_reset_regs(priv);
2959d7cffafSDavid Rivshin 	if (ret)
2969d7cffafSDavid Rivshin 		return ret;
2979d7cffafSDavid Rivshin 
2989d7cffafSDavid Rivshin 	/*
2999d7cffafSDavid Rivshin 	 * Set enable bit for all channels.
3009d7cffafSDavid Rivshin 	 * We will control state with PWM registers alone.
3019d7cffafSDavid Rivshin 	 */
3029d7cffafSDavid Rivshin 	if (cdef->led_control_register_base != IS31FL32XX_REG_NONE) {
3039d7cffafSDavid Rivshin 		u8 value =
3049d7cffafSDavid Rivshin 		    GENMASK(cdef->enable_bits_per_led_control_register-1, 0);
3059d7cffafSDavid Rivshin 		u8 num_regs = cdef->channels /
3069d7cffafSDavid Rivshin 				cdef->enable_bits_per_led_control_register;
3079d7cffafSDavid Rivshin 		int i;
3089d7cffafSDavid Rivshin 
3099d7cffafSDavid Rivshin 		for (i = 0; i < num_regs; i++) {
3109d7cffafSDavid Rivshin 			ret = is31fl32xx_write(priv,
3119d7cffafSDavid Rivshin 					       cdef->led_control_register_base+i,
3129d7cffafSDavid Rivshin 					       value);
3139d7cffafSDavid Rivshin 			if (ret)
3149d7cffafSDavid Rivshin 				return ret;
3159d7cffafSDavid Rivshin 		}
3169d7cffafSDavid Rivshin 	}
3179d7cffafSDavid Rivshin 
3189d7cffafSDavid Rivshin 	ret = is31fl32xx_software_shutdown(priv, false);
3199d7cffafSDavid Rivshin 	if (ret)
3209d7cffafSDavid Rivshin 		return ret;
3219d7cffafSDavid Rivshin 
3229d7cffafSDavid Rivshin 	if (cdef->global_control_reg != IS31FL32XX_REG_NONE) {
3239d7cffafSDavid Rivshin 		ret = is31fl32xx_write(priv, cdef->global_control_reg, 0x00);
3249d7cffafSDavid Rivshin 		if (ret)
3259d7cffafSDavid Rivshin 			return ret;
3269d7cffafSDavid Rivshin 	}
3279d7cffafSDavid Rivshin 
3289d7cffafSDavid Rivshin 	return 0;
3299d7cffafSDavid Rivshin }
3309d7cffafSDavid Rivshin 
3319d7cffafSDavid Rivshin static inline size_t sizeof_is31fl32xx_priv(int num_leds)
3329d7cffafSDavid Rivshin {
3339d7cffafSDavid Rivshin 	return sizeof(struct is31fl32xx_priv) +
3349d7cffafSDavid Rivshin 		      (sizeof(struct is31fl32xx_led_data) * num_leds);
3359d7cffafSDavid Rivshin }
3369d7cffafSDavid Rivshin 
3379d7cffafSDavid Rivshin static int is31fl32xx_parse_child_dt(const struct device *dev,
3389d7cffafSDavid Rivshin 				     const struct device_node *child,
3399d7cffafSDavid Rivshin 				     struct is31fl32xx_led_data *led_data)
3409d7cffafSDavid Rivshin {
3419d7cffafSDavid Rivshin 	struct led_classdev *cdev = &led_data->cdev;
3429d7cffafSDavid Rivshin 	int ret = 0;
3439d7cffafSDavid Rivshin 	u32 reg;
3449d7cffafSDavid Rivshin 
3459d7cffafSDavid Rivshin 	if (of_property_read_string(child, "label", &cdev->name))
3469d7cffafSDavid Rivshin 		cdev->name = child->name;
3479d7cffafSDavid Rivshin 
3489d7cffafSDavid Rivshin 	ret = of_property_read_u32(child, "reg", &reg);
3499d7cffafSDavid Rivshin 	if (ret || reg < 1 || reg > led_data->priv->cdef->channels) {
3509d7cffafSDavid Rivshin 		dev_err(dev,
3519d7cffafSDavid Rivshin 			"Child node %s does not have a valid reg property\n",
3529d7cffafSDavid Rivshin 			child->full_name);
3539d7cffafSDavid Rivshin 		return -EINVAL;
3549d7cffafSDavid Rivshin 	}
3559d7cffafSDavid Rivshin 	led_data->channel = reg;
3569d7cffafSDavid Rivshin 
3579d7cffafSDavid Rivshin 	of_property_read_string(child, "linux,default-trigger",
3589d7cffafSDavid Rivshin 				&cdev->default_trigger);
3599d7cffafSDavid Rivshin 
3609d7cffafSDavid Rivshin 	cdev->brightness_set_blocking = is31fl32xx_brightness_set;
3619d7cffafSDavid Rivshin 
3629d7cffafSDavid Rivshin 	return 0;
3639d7cffafSDavid Rivshin }
3649d7cffafSDavid Rivshin 
3659d7cffafSDavid Rivshin static struct is31fl32xx_led_data *is31fl32xx_find_led_data(
3669d7cffafSDavid Rivshin 					struct is31fl32xx_priv *priv,
3679d7cffafSDavid Rivshin 					u8 channel)
3689d7cffafSDavid Rivshin {
3699d7cffafSDavid Rivshin 	size_t i;
3709d7cffafSDavid Rivshin 
3719d7cffafSDavid Rivshin 	for (i = 0; i < priv->num_leds; i++) {
3729d7cffafSDavid Rivshin 		if (priv->leds[i].channel == channel)
3739d7cffafSDavid Rivshin 			return &priv->leds[i];
3749d7cffafSDavid Rivshin 	}
3759d7cffafSDavid Rivshin 
3769d7cffafSDavid Rivshin 	return NULL;
3779d7cffafSDavid Rivshin }
3789d7cffafSDavid Rivshin 
3799d7cffafSDavid Rivshin static int is31fl32xx_parse_dt(struct device *dev,
3809d7cffafSDavid Rivshin 			       struct is31fl32xx_priv *priv)
3819d7cffafSDavid Rivshin {
3829d7cffafSDavid Rivshin 	struct device_node *child;
3839d7cffafSDavid Rivshin 	int ret = 0;
3849d7cffafSDavid Rivshin 
3859d7cffafSDavid Rivshin 	for_each_child_of_node(dev->of_node, child) {
3869d7cffafSDavid Rivshin 		struct is31fl32xx_led_data *led_data =
3879d7cffafSDavid Rivshin 			&priv->leds[priv->num_leds];
3889d7cffafSDavid Rivshin 		const struct is31fl32xx_led_data *other_led_data;
3899d7cffafSDavid Rivshin 
3909d7cffafSDavid Rivshin 		led_data->priv = priv;
3919d7cffafSDavid Rivshin 
3929d7cffafSDavid Rivshin 		ret = is31fl32xx_parse_child_dt(dev, child, led_data);
3939d7cffafSDavid Rivshin 		if (ret)
3949d7cffafSDavid Rivshin 			goto err;
3959d7cffafSDavid Rivshin 
3969d7cffafSDavid Rivshin 		/* Detect if channel is already in use by another child */
3979d7cffafSDavid Rivshin 		other_led_data = is31fl32xx_find_led_data(priv,
3989d7cffafSDavid Rivshin 							  led_data->channel);
3999d7cffafSDavid Rivshin 		if (other_led_data) {
4009d7cffafSDavid Rivshin 			dev_err(dev,
4019d7cffafSDavid Rivshin 				"%s and %s both attempting to use channel %d\n",
4029d7cffafSDavid Rivshin 				led_data->cdev.name,
4039d7cffafSDavid Rivshin 				other_led_data->cdev.name,
4049d7cffafSDavid Rivshin 				led_data->channel);
4059d7cffafSDavid Rivshin 			goto err;
4069d7cffafSDavid Rivshin 		}
4079d7cffafSDavid Rivshin 
4089d7cffafSDavid Rivshin 		ret = devm_led_classdev_register(dev, &led_data->cdev);
4099d7cffafSDavid Rivshin 		if (ret) {
4109d7cffafSDavid Rivshin 			dev_err(dev, "failed to register PWM led for %s: %d\n",
4119d7cffafSDavid Rivshin 				led_data->cdev.name, ret);
4129d7cffafSDavid Rivshin 			goto err;
4139d7cffafSDavid Rivshin 		}
4149d7cffafSDavid Rivshin 
4159d7cffafSDavid Rivshin 		priv->num_leds++;
4169d7cffafSDavid Rivshin 	}
4179d7cffafSDavid Rivshin 
4189d7cffafSDavid Rivshin 	return 0;
4199d7cffafSDavid Rivshin 
4209d7cffafSDavid Rivshin err:
4219d7cffafSDavid Rivshin 	of_node_put(child);
4229d7cffafSDavid Rivshin 	return ret;
4239d7cffafSDavid Rivshin }
4249d7cffafSDavid Rivshin 
4251d991b71SH. Nikolaus Schaller static const struct of_device_id of_is31fl32xx_match[] = {
4269d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3236", .data = &is31fl3236_cdef, },
4279d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3235", .data = &is31fl3235_cdef, },
4289d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3218", .data = &is31fl3218_cdef, },
429e0442d7dSDavid Rivshin 	{ .compatible = "si-en,sn3218",    .data = &is31fl3218_cdef, },
4309d7cffafSDavid Rivshin 	{ .compatible = "issi,is31fl3216", .data = &is31fl3216_cdef, },
431e0442d7dSDavid Rivshin 	{ .compatible = "si-en,sn3216",    .data = &is31fl3216_cdef, },
4329d7cffafSDavid Rivshin 	{},
4339d7cffafSDavid Rivshin };
4349d7cffafSDavid Rivshin 
4351d991b71SH. Nikolaus Schaller MODULE_DEVICE_TABLE(of, of_is31fl32xx_match);
4369d7cffafSDavid Rivshin 
4379d7cffafSDavid Rivshin static int is31fl32xx_probe(struct i2c_client *client,
4389d7cffafSDavid Rivshin 			    const struct i2c_device_id *id)
4399d7cffafSDavid Rivshin {
4409d7cffafSDavid Rivshin 	const struct is31fl32xx_chipdef *cdef;
4419d7cffafSDavid Rivshin 	const struct of_device_id *of_dev_id;
4429d7cffafSDavid Rivshin 	struct device *dev = &client->dev;
4439d7cffafSDavid Rivshin 	struct is31fl32xx_priv *priv;
4449d7cffafSDavid Rivshin 	int count;
4459d7cffafSDavid Rivshin 	int ret = 0;
4469d7cffafSDavid Rivshin 
4471d991b71SH. Nikolaus Schaller 	of_dev_id = of_match_device(of_is31fl32xx_match, dev);
4489d7cffafSDavid Rivshin 	if (!of_dev_id)
4499d7cffafSDavid Rivshin 		return -EINVAL;
4509d7cffafSDavid Rivshin 
4519d7cffafSDavid Rivshin 	cdef = of_dev_id->data;
4529d7cffafSDavid Rivshin 
4539d7cffafSDavid Rivshin 	count = of_get_child_count(dev->of_node);
4549d7cffafSDavid Rivshin 	if (!count)
4559d7cffafSDavid Rivshin 		return -EINVAL;
4569d7cffafSDavid Rivshin 
4579d7cffafSDavid Rivshin 	priv = devm_kzalloc(dev, sizeof_is31fl32xx_priv(count),
4589d7cffafSDavid Rivshin 			    GFP_KERNEL);
4599d7cffafSDavid Rivshin 	if (!priv)
4609d7cffafSDavid Rivshin 		return -ENOMEM;
4619d7cffafSDavid Rivshin 
4629d7cffafSDavid Rivshin 	priv->client = client;
4639d7cffafSDavid Rivshin 	priv->cdef = cdef;
4649d7cffafSDavid Rivshin 	i2c_set_clientdata(client, priv);
4659d7cffafSDavid Rivshin 
4669d7cffafSDavid Rivshin 	ret = is31fl32xx_init_regs(priv);
4679d7cffafSDavid Rivshin 	if (ret)
4689d7cffafSDavid Rivshin 		return ret;
4699d7cffafSDavid Rivshin 
4709d7cffafSDavid Rivshin 	ret = is31fl32xx_parse_dt(dev, priv);
4719d7cffafSDavid Rivshin 	if (ret)
4729d7cffafSDavid Rivshin 		return ret;
4739d7cffafSDavid Rivshin 
4749d7cffafSDavid Rivshin 	return 0;
4759d7cffafSDavid Rivshin }
4769d7cffafSDavid Rivshin 
4779d7cffafSDavid Rivshin static int is31fl32xx_remove(struct i2c_client *client)
4789d7cffafSDavid Rivshin {
4799d7cffafSDavid Rivshin 	struct is31fl32xx_priv *priv = i2c_get_clientdata(client);
4809d7cffafSDavid Rivshin 
4819d7cffafSDavid Rivshin 	return is31fl32xx_reset_regs(priv);
4829d7cffafSDavid Rivshin }
4839d7cffafSDavid Rivshin 
4849d7cffafSDavid Rivshin /*
4855706c01fSH. Nikolaus Schaller  * i2c-core (and modalias) requires that id_table be properly filled,
4865706c01fSH. Nikolaus Schaller  * even though it is not used for DeviceTree based instantiation.
4879d7cffafSDavid Rivshin  */
4881d991b71SH. Nikolaus Schaller static const struct i2c_device_id is31fl32xx_id[] = {
4895706c01fSH. Nikolaus Schaller 	{ "is31fl3236" },
4905706c01fSH. Nikolaus Schaller 	{ "is31fl3235" },
4915706c01fSH. Nikolaus Schaller 	{ "is31fl3218" },
4925706c01fSH. Nikolaus Schaller 	{ "sn3218" },
4935706c01fSH. Nikolaus Schaller 	{ "is31fl3216" },
4945706c01fSH. Nikolaus Schaller 	{ "sn3216" },
4959d7cffafSDavid Rivshin 	{},
4969d7cffafSDavid Rivshin };
4979d7cffafSDavid Rivshin 
4981d991b71SH. Nikolaus Schaller MODULE_DEVICE_TABLE(i2c, is31fl32xx_id);
4999d7cffafSDavid Rivshin 
5009d7cffafSDavid Rivshin static struct i2c_driver is31fl32xx_driver = {
5019d7cffafSDavid Rivshin 	.driver = {
5029d7cffafSDavid Rivshin 		.name	= "is31fl32xx",
5031d991b71SH. Nikolaus Schaller 		.of_match_table = of_is31fl32xx_match,
5049d7cffafSDavid Rivshin 	},
5059d7cffafSDavid Rivshin 	.probe		= is31fl32xx_probe,
5069d7cffafSDavid Rivshin 	.remove		= is31fl32xx_remove,
5071d991b71SH. Nikolaus Schaller 	.id_table	= is31fl32xx_id,
5089d7cffafSDavid Rivshin };
5099d7cffafSDavid Rivshin 
5109d7cffafSDavid Rivshin module_i2c_driver(is31fl32xx_driver);
5119d7cffafSDavid Rivshin 
5129d7cffafSDavid Rivshin MODULE_AUTHOR("David Rivshin <drivshin@allworx.com>");
5139d7cffafSDavid Rivshin MODULE_DESCRIPTION("ISSI IS31FL32xx LED driver");
5149d7cffafSDavid Rivshin MODULE_LICENSE("GPL v2");
515