Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

leds: tlc591xx: Driver for the TI 8/16 Channel i2c LED driver

The TLC59116 is an I2C bus controlled 16-channel LED driver. The
TLC59108 is an I2C bus controlled 8-channel LED driver, which is very
similar to the TLC59116. Each LED output has its own 8-bit
fixed-frequency PWM controller to control the brightness of the LED.
The LEDs can also be fixed off and on, making them suitable for use as
GPOs.

This is based on a driver from Belkin, but has been extensively
rewritten and extended to support both 08 and 16 versions.

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
Tested-by: Imre Kaloz <kaloz@openwrt.org>
Cc: Matthew.Fatheree@belkin.com
Acked-by: Jacek Anaszewski <j.anaszewski@samsung.com>
Signed-off-by: Bryan Wu <cooloney@gmail.com>

authored by

Andrew Lunn and committed by
Bryan Wu
e370d010 e0d4ffc3

+309
+8
drivers/leds/Kconfig
··· 467 467 LED driver chips accessed via the I2C bus. 468 468 Driver support brightness control and hardware-assisted blinking. 469 469 470 + config LEDS_TLC591XX 471 + tristate "LED driver for TLC59108 and TLC59116 controllers" 472 + depends on LEDS_CLASS && I2C 473 + select REGMAP_I2C 474 + help 475 + This option enables support for Texas Instruments TLC59108 476 + and TLC59116 LED controllers. 477 + 470 478 config LEDS_MAX8997 471 479 tristate "LED support for MAX8997 PMIC" 472 480 depends on LEDS_CLASS && MFD_MAX8997
+1
drivers/leds/Makefile
··· 31 31 obj-$(CONFIG_LEDS_LP8788) += leds-lp8788.o 32 32 obj-$(CONFIG_LEDS_LP8860) += leds-lp8860.o 33 33 obj-$(CONFIG_LEDS_TCA6507) += leds-tca6507.o 34 + obj-$(CONFIG_LEDS_TLC591XX) += leds-tlc591xx.o 34 35 obj-$(CONFIG_LEDS_CLEVO_MAIL) += leds-clevo-mail.o 35 36 obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o 36 37 obj-$(CONFIG_LEDS_HP6XX) += leds-hp6xx.o
+300
drivers/leds/leds-tlc591xx.c
··· 1 + /* 2 + * Copyright 2014 Belkin Inc. 3 + * Copyright 2015 Andrew Lunn <andrew@lunn.ch> 4 + * 5 + * This program is free software; you can redistribute it and/or modify 6 + * it under the terms of the GNU General Public License as published by 7 + * the Free Software Foundation; version 2 of the License. 8 + */ 9 + 10 + #include <linux/i2c.h> 11 + #include <linux/leds.h> 12 + #include <linux/module.h> 13 + #include <linux/of.h> 14 + #include <linux/of_device.h> 15 + #include <linux/regmap.h> 16 + #include <linux/slab.h> 17 + #include <linux/workqueue.h> 18 + 19 + #define TLC591XX_MAX_LEDS 16 20 + 21 + #define TLC591XX_REG_MODE1 0x00 22 + #define MODE1_RESPON_ADDR_MASK 0xF0 23 + #define MODE1_NORMAL_MODE (0 << 4) 24 + #define MODE1_SPEED_MODE (1 << 4) 25 + 26 + #define TLC591XX_REG_MODE2 0x01 27 + #define MODE2_DIM (0 << 5) 28 + #define MODE2_BLINK (1 << 5) 29 + #define MODE2_OCH_STOP (0 << 3) 30 + #define MODE2_OCH_ACK (1 << 3) 31 + 32 + #define TLC591XX_REG_PWM(x) (0x02 + (x)) 33 + 34 + #define TLC591XX_REG_GRPPWM 0x12 35 + #define TLC591XX_REG_GRPFREQ 0x13 36 + 37 + /* LED Driver Output State, determine the source that drives LED outputs */ 38 + #define LEDOUT_OFF 0x0 /* Output LOW */ 39 + #define LEDOUT_ON 0x1 /* Output HI-Z */ 40 + #define LEDOUT_DIM 0x2 /* Dimming */ 41 + #define LEDOUT_BLINK 0x3 /* Blinking */ 42 + #define LEDOUT_MASK 0x3 43 + 44 + #define ldev_to_led(c) container_of(c, struct tlc591xx_led, ldev) 45 + #define work_to_led(work) container_of(work, struct tlc591xx_led, work) 46 + 47 + struct tlc591xx_led { 48 + bool active; 49 + unsigned int led_no; 50 + struct led_classdev ldev; 51 + struct work_struct work; 52 + struct tlc591xx_priv *priv; 53 + }; 54 + 55 + struct tlc591xx_priv { 56 + struct tlc591xx_led leds[TLC591XX_MAX_LEDS]; 57 + struct regmap *regmap; 58 + unsigned int reg_ledout_offset; 59 + }; 60 + 61 + struct tlc591xx { 62 + unsigned int max_leds; 63 + unsigned int reg_ledout_offset; 64 + }; 65 + 66 + static const struct tlc591xx tlc59116 = { 67 + .max_leds = 16, 68 + .reg_ledout_offset = 0x14, 69 + }; 70 + 71 + static const struct tlc591xx tlc59108 = { 72 + .max_leds = 8, 73 + .reg_ledout_offset = 0x0c, 74 + }; 75 + 76 + static int 77 + tlc591xx_set_mode(struct regmap *regmap, u8 mode) 78 + { 79 + int err; 80 + u8 val; 81 + 82 + err = regmap_write(regmap, TLC591XX_REG_MODE1, MODE1_NORMAL_MODE); 83 + if (err) 84 + return err; 85 + 86 + val = MODE2_OCH_STOP | mode; 87 + 88 + return regmap_write(regmap, TLC591XX_REG_MODE2, val); 89 + } 90 + 91 + static int 92 + tlc591xx_set_ledout(struct tlc591xx_priv *priv, struct tlc591xx_led *led, 93 + u8 val) 94 + { 95 + unsigned int i = (led->led_no % 4) * 2; 96 + unsigned int mask = LEDOUT_MASK << i; 97 + unsigned int addr = priv->reg_ledout_offset + (led->led_no >> 2); 98 + 99 + val = val << i; 100 + 101 + return regmap_update_bits(priv->regmap, addr, mask, val); 102 + } 103 + 104 + static int 105 + tlc591xx_set_pwm(struct tlc591xx_priv *priv, struct tlc591xx_led *led, 106 + u8 brightness) 107 + { 108 + u8 pwm = TLC591XX_REG_PWM(led->led_no); 109 + 110 + return regmap_write(priv->regmap, pwm, brightness); 111 + } 112 + 113 + static void 114 + tlc591xx_led_work(struct work_struct *work) 115 + { 116 + struct tlc591xx_led *led = work_to_led(work); 117 + struct tlc591xx_priv *priv = led->priv; 118 + enum led_brightness brightness = led->ldev.brightness; 119 + int err; 120 + 121 + switch (brightness) { 122 + case 0: 123 + err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF); 124 + break; 125 + case LED_FULL: 126 + err = tlc591xx_set_ledout(priv, led, LEDOUT_ON); 127 + break; 128 + default: 129 + err = tlc591xx_set_ledout(priv, led, LEDOUT_DIM); 130 + if (!err) 131 + err = tlc591xx_set_pwm(priv, led, brightness); 132 + } 133 + 134 + if (err) 135 + dev_err(led->ldev.dev, "Failed setting brightness\n"); 136 + } 137 + 138 + static void 139 + tlc591xx_brightness_set(struct led_classdev *led_cdev, 140 + enum led_brightness brightness) 141 + { 142 + struct tlc591xx_led *led = ldev_to_led(led_cdev); 143 + 144 + led->ldev.brightness = brightness; 145 + schedule_work(&led->work); 146 + } 147 + 148 + static void 149 + tlc591xx_destroy_devices(struct tlc591xx_priv *priv, unsigned int j) 150 + { 151 + int i = j; 152 + 153 + while (--i >= 0) { 154 + if (priv->leds[i].active) { 155 + led_classdev_unregister(&priv->leds[i].ldev); 156 + cancel_work_sync(&priv->leds[i].work); 157 + } 158 + } 159 + } 160 + 161 + static int 162 + tlc591xx_configure(struct device *dev, 163 + struct tlc591xx_priv *priv, 164 + const struct tlc591xx *tlc591xx) 165 + { 166 + unsigned int i; 167 + int err = 0; 168 + 169 + tlc591xx_set_mode(priv->regmap, MODE2_DIM); 170 + for (i = 0; i < TLC591XX_MAX_LEDS; i++) { 171 + struct tlc591xx_led *led = &priv->leds[i]; 172 + 173 + if (!led->active) 174 + continue; 175 + 176 + led->priv = priv; 177 + led->led_no = i; 178 + led->ldev.brightness_set = tlc591xx_brightness_set; 179 + led->ldev.max_brightness = LED_FULL; 180 + INIT_WORK(&led->work, tlc591xx_led_work); 181 + err = led_classdev_register(dev, &led->ldev); 182 + if (err < 0) { 183 + dev_err(dev, "couldn't register LED %s\n", 184 + led->ldev.name); 185 + goto exit; 186 + } 187 + } 188 + 189 + return 0; 190 + 191 + exit: 192 + tlc591xx_destroy_devices(priv, i); 193 + return err; 194 + } 195 + 196 + static const struct regmap_config tlc591xx_regmap = { 197 + .reg_bits = 8, 198 + .val_bits = 8, 199 + .max_register = 0x1e, 200 + }; 201 + 202 + static const struct of_device_id of_tlc591xx_leds_match[] = { 203 + { .compatible = "ti,tlc59116", 204 + .data = &tlc59116 }, 205 + { .compatible = "ti,tlc59108", 206 + .data = &tlc59108 }, 207 + {}, 208 + }; 209 + MODULE_DEVICE_TABLE(of, of_tlc591xx_leds_match); 210 + 211 + static int 212 + tlc591xx_probe(struct i2c_client *client, 213 + const struct i2c_device_id *id) 214 + { 215 + struct device_node *np = client->dev.of_node, *child; 216 + struct device *dev = &client->dev; 217 + const struct of_device_id *match; 218 + const struct tlc591xx *tlc591xx; 219 + struct tlc591xx_priv *priv; 220 + int err, count, reg; 221 + 222 + match = of_match_device(of_tlc591xx_leds_match, dev); 223 + if (!match) 224 + return -ENODEV; 225 + 226 + tlc591xx = match->data; 227 + if (!np) 228 + return -ENODEV; 229 + 230 + count = of_get_child_count(np); 231 + if (!count || count > tlc591xx->max_leds) 232 + return -EINVAL; 233 + 234 + if (!i2c_check_functionality(client->adapter, 235 + I2C_FUNC_SMBUS_BYTE_DATA)) 236 + return -EIO; 237 + 238 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 239 + if (!priv) 240 + return -ENOMEM; 241 + 242 + priv->regmap = devm_regmap_init_i2c(client, &tlc591xx_regmap); 243 + if (IS_ERR(priv->regmap)) { 244 + err = PTR_ERR(priv->regmap); 245 + dev_err(dev, "Failed to allocate register map: %d\n", err); 246 + return err; 247 + } 248 + priv->reg_ledout_offset = tlc591xx->reg_ledout_offset; 249 + 250 + i2c_set_clientdata(client, priv); 251 + 252 + for_each_child_of_node(np, child) { 253 + err = of_property_read_u32(child, "reg", &reg); 254 + if (err) 255 + return err; 256 + if (reg < 0 || reg >= tlc591xx->max_leds) 257 + return -EINVAL; 258 + if (priv->leds[reg].active) 259 + return -EINVAL; 260 + priv->leds[reg].active = true; 261 + priv->leds[reg].ldev.name = 262 + of_get_property(child, "label", NULL) ? : child->name; 263 + priv->leds[reg].ldev.default_trigger = 264 + of_get_property(child, "linux,default-trigger", NULL); 265 + } 266 + return tlc591xx_configure(dev, priv, tlc591xx); 267 + } 268 + 269 + static int 270 + tlc591xx_remove(struct i2c_client *client) 271 + { 272 + struct tlc591xx_priv *priv = i2c_get_clientdata(client); 273 + 274 + tlc591xx_destroy_devices(priv, TLC591XX_MAX_LEDS); 275 + 276 + return 0; 277 + } 278 + 279 + static const struct i2c_device_id tlc591xx_id[] = { 280 + { "tlc59116" }, 281 + { "tlc59108" }, 282 + {}, 283 + }; 284 + MODULE_DEVICE_TABLE(i2c, tlc591xx_id); 285 + 286 + static struct i2c_driver tlc591xx_driver = { 287 + .driver = { 288 + .name = "tlc591xx", 289 + .of_match_table = of_match_ptr(of_tlc591xx_leds_match), 290 + }, 291 + .probe = tlc591xx_probe, 292 + .remove = tlc591xx_remove, 293 + .id_table = tlc591xx_id, 294 + }; 295 + 296 + module_i2c_driver(tlc591xx_driver); 297 + 298 + MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>"); 299 + MODULE_LICENSE("GPL"); 300 + MODULE_DESCRIPTION("TLC591XX LED driver");