051-leds_tlc59116_add_driver.patch 7.6 KB

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