2 * Driver for ISSI IS31FL32xx family of I2C LED controllers
4 * Copyright 2015 Allworx Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 * http://www.issi.com/US/product-analog-fxled-driver.shtml
13 * http://www.si-en.com/product.asp?parentid=890
16 #include <linux/device.h>
17 #include <linux/i2c.h>
18 #include <linux/kernel.h>
19 #include <linux/leds.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
24 /* Used to indicate a device has no such register */
25 #define IS31FL32XX_REG_NONE 0xFF
27 /* Software Shutdown bit in Shutdown Register */
28 #define IS31FL32XX_SHUTDOWN_SSD_ENABLE 0
29 #define IS31FL32XX_SHUTDOWN_SSD_DISABLE BIT(0)
31 /* IS31FL3216 has a number of unique registers */
32 #define IS31FL3216_CONFIG_REG 0x00
33 #define IS31FL3216_LIGHTING_EFFECT_REG 0x03
34 #define IS31FL3216_CHANNEL_CONFIG_REG 0x04
36 /* Software Shutdown bit in 3216 Config Register */
37 #define IS31FL3216_CONFIG_SSD_ENABLE BIT(7)
38 #define IS31FL3216_CONFIG_SSD_DISABLE 0
40 struct is31fl32xx_priv
;
41 struct is31fl32xx_led_data
{
42 struct led_classdev cdev
;
43 u8 channel
; /* 1-based, max priv->cdef->channels */
44 struct is31fl32xx_priv
*priv
;
47 struct is31fl32xx_priv
{
48 const struct is31fl32xx_chipdef
*cdef
;
49 struct i2c_client
*client
;
50 unsigned int num_leds
;
51 struct is31fl32xx_led_data leds
[0];
55 * struct is31fl32xx_chipdef - chip-specific attributes
56 * @channels : Number of LED channels
57 * @shutdown_reg : address of Shutdown register (optional)
58 * @pwm_update_reg : address of PWM Update register
59 * @global_control_reg : address of Global Control register (optional)
60 * @reset_reg : address of Reset register (optional)
61 * @pwm_register_base : address of first PWM register
62 * @pwm_registers_reversed: : true if PWM registers count down instead of up
63 * @led_control_register_base : address of first LED control register (optional)
64 * @enable_bits_per_led_control_register: number of LEDs enable bits in each
65 * @reset_func: : pointer to reset function
67 * For all optional register addresses, the sentinel value %IS31FL32XX_REG_NONE
68 * indicates that this chip has no such register.
70 * If non-NULL, @reset_func will be called during probing to set all
71 * necessary registers to a known initialization state. This is needed
72 * for chips that do not have a @reset_reg.
74 * @enable_bits_per_led_control_register must be >=1 if
75 * @led_control_register_base != %IS31FL32XX_REG_NONE.
77 struct is31fl32xx_chipdef
{
81 u8 global_control_reg
;
84 bool pwm_registers_reversed
;
85 u8 led_control_register_base
;
86 u8 enable_bits_per_led_control_register
;
87 int (*reset_func
)(struct is31fl32xx_priv
*priv
);
88 int (*sw_shutdown_func
)(struct is31fl32xx_priv
*priv
, bool enable
);
91 static const struct is31fl32xx_chipdef is31fl3236_cdef
= {
94 .pwm_update_reg
= 0x25,
95 .global_control_reg
= 0x4a,
97 .pwm_register_base
= 0x01,
98 .led_control_register_base
= 0x26,
99 .enable_bits_per_led_control_register
= 1,
102 static const struct is31fl32xx_chipdef is31fl3235_cdef
= {
104 .shutdown_reg
= 0x00,
105 .pwm_update_reg
= 0x25,
106 .global_control_reg
= 0x4a,
108 .pwm_register_base
= 0x05,
109 .led_control_register_base
= 0x2a,
110 .enable_bits_per_led_control_register
= 1,
113 static const struct is31fl32xx_chipdef is31fl3218_cdef
= {
115 .shutdown_reg
= 0x00,
116 .pwm_update_reg
= 0x16,
117 .global_control_reg
= IS31FL32XX_REG_NONE
,
119 .pwm_register_base
= 0x01,
120 .led_control_register_base
= 0x13,
121 .enable_bits_per_led_control_register
= 6,
124 static int is31fl3216_reset(struct is31fl32xx_priv
*priv
);
125 static int is31fl3216_software_shutdown(struct is31fl32xx_priv
*priv
,
127 static const struct is31fl32xx_chipdef is31fl3216_cdef
= {
129 .shutdown_reg
= IS31FL32XX_REG_NONE
,
130 .pwm_update_reg
= 0xB0,
131 .global_control_reg
= IS31FL32XX_REG_NONE
,
132 .reset_reg
= IS31FL32XX_REG_NONE
,
133 .pwm_register_base
= 0x10,
134 .pwm_registers_reversed
= true,
135 .led_control_register_base
= 0x01,
136 .enable_bits_per_led_control_register
= 8,
137 .reset_func
= is31fl3216_reset
,
138 .sw_shutdown_func
= is31fl3216_software_shutdown
,
141 static int is31fl32xx_write(struct is31fl32xx_priv
*priv
, u8 reg
, u8 val
)
145 dev_dbg(&priv
->client
->dev
, "writing register 0x%02X=0x%02X", reg
, val
);
147 ret
= i2c_smbus_write_byte_data(priv
->client
, reg
, val
);
149 dev_err(&priv
->client
->dev
,
150 "register write to 0x%02X failed (error %d)",
157 * Custom reset function for IS31FL3216 because it does not have a RESET
158 * register the way that the other IS31FL32xx chips do. We don't bother
159 * writing the GPIO and animation registers, because the registers we
160 * do write ensure those will have no effect.
162 static int is31fl3216_reset(struct is31fl32xx_priv
*priv
)
167 ret
= is31fl32xx_write(priv
, IS31FL3216_CONFIG_REG
,
168 IS31FL3216_CONFIG_SSD_ENABLE
);
171 for (i
= 0; i
< priv
->cdef
->channels
; i
++) {
172 ret
= is31fl32xx_write(priv
, priv
->cdef
->pwm_register_base
+i
,
177 ret
= is31fl32xx_write(priv
, priv
->cdef
->pwm_update_reg
, 0);
180 ret
= is31fl32xx_write(priv
, IS31FL3216_LIGHTING_EFFECT_REG
, 0x00);
183 ret
= is31fl32xx_write(priv
, IS31FL3216_CHANNEL_CONFIG_REG
, 0x00);
191 * Custom Software-Shutdown function for IS31FL3216 because it does not have
192 * a SHUTDOWN register the way that the other IS31FL32xx chips do.
193 * We don't bother doing a read/modify/write on the CONFIG register because
194 * we only ever use a value of '0' for the other fields in that register.
196 static int is31fl3216_software_shutdown(struct is31fl32xx_priv
*priv
,
199 u8 value
= enable
? IS31FL3216_CONFIG_SSD_ENABLE
:
200 IS31FL3216_CONFIG_SSD_DISABLE
;
202 return is31fl32xx_write(priv
, IS31FL3216_CONFIG_REG
, value
);
206 * NOTE: A mutex is not needed in this function because:
207 * - All referenced data is read-only after probe()
208 * - The I2C core has a mutex on to protect the bus
209 * - There are no read/modify/write operations
210 * - Intervening operations between the write of the PWM register
211 * and the Update register are harmless.
216 * PWM_REG_2 write 128
220 * PWM_REG_2 write 128
223 * are equivalent. Poking the Update register merely applies all PWM
224 * register writes up to that point.
226 static int is31fl32xx_brightness_set(struct led_classdev
*led_cdev
,
227 enum led_brightness brightness
)
229 const struct is31fl32xx_led_data
*led_data
=
230 container_of(led_cdev
, struct is31fl32xx_led_data
, cdev
);
231 const struct is31fl32xx_chipdef
*cdef
= led_data
->priv
->cdef
;
232 u8 pwm_register_offset
;
235 dev_dbg(led_cdev
->dev
, "%s: %d\n", __func__
, brightness
);
237 /* NOTE: led_data->channel is 1-based */
238 if (cdef
->pwm_registers_reversed
)
239 pwm_register_offset
= cdef
->channels
- led_data
->channel
;
241 pwm_register_offset
= led_data
->channel
- 1;
243 ret
= is31fl32xx_write(led_data
->priv
,
244 cdef
->pwm_register_base
+ pwm_register_offset
,
249 return is31fl32xx_write(led_data
->priv
, cdef
->pwm_update_reg
, 0);
252 static int is31fl32xx_reset_regs(struct is31fl32xx_priv
*priv
)
254 const struct is31fl32xx_chipdef
*cdef
= priv
->cdef
;
257 if (cdef
->reset_reg
!= IS31FL32XX_REG_NONE
) {
258 ret
= is31fl32xx_write(priv
, cdef
->reset_reg
, 0);
263 if (cdef
->reset_func
)
264 return cdef
->reset_func(priv
);
269 static int is31fl32xx_software_shutdown(struct is31fl32xx_priv
*priv
,
272 const struct is31fl32xx_chipdef
*cdef
= priv
->cdef
;
275 if (cdef
->shutdown_reg
!= IS31FL32XX_REG_NONE
) {
276 u8 value
= enable
? IS31FL32XX_SHUTDOWN_SSD_ENABLE
:
277 IS31FL32XX_SHUTDOWN_SSD_DISABLE
;
278 ret
= is31fl32xx_write(priv
, cdef
->shutdown_reg
, value
);
283 if (cdef
->sw_shutdown_func
)
284 return cdef
->sw_shutdown_func(priv
, enable
);
289 static int is31fl32xx_init_regs(struct is31fl32xx_priv
*priv
)
291 const struct is31fl32xx_chipdef
*cdef
= priv
->cdef
;
294 ret
= is31fl32xx_reset_regs(priv
);
299 * Set enable bit for all channels.
300 * We will control state with PWM registers alone.
302 if (cdef
->led_control_register_base
!= IS31FL32XX_REG_NONE
) {
304 GENMASK(cdef
->enable_bits_per_led_control_register
-1, 0);
305 u8 num_regs
= cdef
->channels
/
306 cdef
->enable_bits_per_led_control_register
;
309 for (i
= 0; i
< num_regs
; i
++) {
310 ret
= is31fl32xx_write(priv
,
311 cdef
->led_control_register_base
+i
,
318 ret
= is31fl32xx_software_shutdown(priv
, false);
322 if (cdef
->global_control_reg
!= IS31FL32XX_REG_NONE
) {
323 ret
= is31fl32xx_write(priv
, cdef
->global_control_reg
, 0x00);
331 static inline size_t sizeof_is31fl32xx_priv(int num_leds
)
333 return sizeof(struct is31fl32xx_priv
) +
334 (sizeof(struct is31fl32xx_led_data
) * num_leds
);
337 static int is31fl32xx_parse_child_dt(const struct device
*dev
,
338 const struct device_node
*child
,
339 struct is31fl32xx_led_data
*led_data
)
341 struct led_classdev
*cdev
= &led_data
->cdev
;
345 if (of_property_read_string(child
, "label", &cdev
->name
))
346 cdev
->name
= child
->name
;
348 ret
= of_property_read_u32(child
, "reg", ®
);
349 if (ret
|| reg
< 1 || reg
> led_data
->priv
->cdef
->channels
) {
351 "Child node %s does not have a valid reg property\n",
355 led_data
->channel
= reg
;
357 of_property_read_string(child
, "linux,default-trigger",
358 &cdev
->default_trigger
);
360 cdev
->brightness_set_blocking
= is31fl32xx_brightness_set
;
365 static struct is31fl32xx_led_data
*is31fl32xx_find_led_data(
366 struct is31fl32xx_priv
*priv
,
371 for (i
= 0; i
< priv
->num_leds
; i
++) {
372 if (priv
->leds
[i
].channel
== channel
)
373 return &priv
->leds
[i
];
379 static int is31fl32xx_parse_dt(struct device
*dev
,
380 struct is31fl32xx_priv
*priv
)
382 struct device_node
*child
;
385 for_each_child_of_node(dev
->of_node
, child
) {
386 struct is31fl32xx_led_data
*led_data
=
387 &priv
->leds
[priv
->num_leds
];
388 const struct is31fl32xx_led_data
*other_led_data
;
390 led_data
->priv
= priv
;
392 ret
= is31fl32xx_parse_child_dt(dev
, child
, led_data
);
396 /* Detect if channel is already in use by another child */
397 other_led_data
= is31fl32xx_find_led_data(priv
,
399 if (other_led_data
) {
401 "%s and %s both attempting to use channel %d\n",
403 other_led_data
->cdev
.name
,
408 ret
= devm_led_classdev_register(dev
, &led_data
->cdev
);
410 dev_err(dev
, "failed to register PWM led for %s: %d\n",
411 led_data
->cdev
.name
, ret
);
425 static const struct of_device_id of_is31fl32xx_match
[] = {
426 { .compatible
= "issi,is31fl3236", .data
= &is31fl3236_cdef
, },
427 { .compatible
= "issi,is31fl3235", .data
= &is31fl3235_cdef
, },
428 { .compatible
= "issi,is31fl3218", .data
= &is31fl3218_cdef
, },
429 { .compatible
= "si-en,sn3218", .data
= &is31fl3218_cdef
, },
430 { .compatible
= "issi,is31fl3216", .data
= &is31fl3216_cdef
, },
431 { .compatible
= "si-en,sn3216", .data
= &is31fl3216_cdef
, },
435 MODULE_DEVICE_TABLE(of
, of_is31fl32xx_match
);
437 static int is31fl32xx_probe(struct i2c_client
*client
,
438 const struct i2c_device_id
*id
)
440 const struct is31fl32xx_chipdef
*cdef
;
441 const struct of_device_id
*of_dev_id
;
442 struct device
*dev
= &client
->dev
;
443 struct is31fl32xx_priv
*priv
;
447 of_dev_id
= of_match_device(of_is31fl32xx_match
, dev
);
451 cdef
= of_dev_id
->data
;
453 count
= of_get_child_count(dev
->of_node
);
457 priv
= devm_kzalloc(dev
, sizeof_is31fl32xx_priv(count
),
462 priv
->client
= client
;
464 i2c_set_clientdata(client
, priv
);
466 ret
= is31fl32xx_init_regs(priv
);
470 ret
= is31fl32xx_parse_dt(dev
, priv
);
477 static int is31fl32xx_remove(struct i2c_client
*client
)
479 struct is31fl32xx_priv
*priv
= i2c_get_clientdata(client
);
481 return is31fl32xx_reset_regs(priv
);
485 * i2c-core (and modalias) requires that id_table be properly filled,
486 * even though it is not used for DeviceTree based instantiation.
488 static const struct i2c_device_id is31fl32xx_id
[] = {
498 MODULE_DEVICE_TABLE(i2c
, is31fl32xx_id
);
500 static struct i2c_driver is31fl32xx_driver
= {
502 .name
= "is31fl32xx",
503 .of_match_table
= of_is31fl32xx_match
,
505 .probe
= is31fl32xx_probe
,
506 .remove
= is31fl32xx_remove
,
507 .id_table
= is31fl32xx_id
,
510 module_i2c_driver(is31fl32xx_driver
);
512 MODULE_AUTHOR("David Rivshin <drivshin@allworx.com>");
513 MODULE_DESCRIPTION("ISSI IS31FL32xx LED driver");
514 MODULE_LICENSE("GPL v2");