leds-tlc591xx.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <linux/i2c.h>
  10. #include <linux/leds.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/slab.h>
  16. #define TLC591XX_MAX_LEDS 16
  17. #define TLC591XX_REG_MODE1 0x00
  18. #define MODE1_RESPON_ADDR_MASK 0xF0
  19. #define MODE1_NORMAL_MODE (0 << 4)
  20. #define MODE1_SPEED_MODE (1 << 4)
  21. #define TLC591XX_REG_MODE2 0x01
  22. #define MODE2_DIM (0 << 5)
  23. #define MODE2_BLINK (1 << 5)
  24. #define MODE2_OCH_STOP (0 << 3)
  25. #define MODE2_OCH_ACK (1 << 3)
  26. #define TLC591XX_REG_PWM(x) (0x02 + (x))
  27. #define TLC591XX_REG_GRPPWM 0x12
  28. #define TLC591XX_REG_GRPFREQ 0x13
  29. /* LED Driver Output State, determine the source that drives LED outputs */
  30. #define LEDOUT_OFF 0x0 /* Output LOW */
  31. #define LEDOUT_ON 0x1 /* Output HI-Z */
  32. #define LEDOUT_DIM 0x2 /* Dimming */
  33. #define LEDOUT_BLINK 0x3 /* Blinking */
  34. #define LEDOUT_MASK 0x3
  35. #define ldev_to_led(c) container_of(c, struct tlc591xx_led, ldev)
  36. struct tlc591xx_led {
  37. bool active;
  38. unsigned int led_no;
  39. struct led_classdev ldev;
  40. struct tlc591xx_priv *priv;
  41. };
  42. struct tlc591xx_priv {
  43. struct tlc591xx_led leds[TLC591XX_MAX_LEDS];
  44. struct regmap *regmap;
  45. unsigned int reg_ledout_offset;
  46. };
  47. struct tlc591xx {
  48. unsigned int max_leds;
  49. unsigned int reg_ledout_offset;
  50. };
  51. static const struct tlc591xx tlc59116 = {
  52. .max_leds = 16,
  53. .reg_ledout_offset = 0x14,
  54. };
  55. static const struct tlc591xx tlc59108 = {
  56. .max_leds = 8,
  57. .reg_ledout_offset = 0x0c,
  58. };
  59. static int
  60. tlc591xx_set_mode(struct regmap *regmap, u8 mode)
  61. {
  62. int err;
  63. u8 val;
  64. err = regmap_write(regmap, TLC591XX_REG_MODE1, MODE1_NORMAL_MODE);
  65. if (err)
  66. return err;
  67. val = MODE2_OCH_STOP | mode;
  68. return regmap_write(regmap, TLC591XX_REG_MODE2, val);
  69. }
  70. static int
  71. tlc591xx_set_ledout(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
  72. u8 val)
  73. {
  74. unsigned int i = (led->led_no % 4) * 2;
  75. unsigned int mask = LEDOUT_MASK << i;
  76. unsigned int addr = priv->reg_ledout_offset + (led->led_no >> 2);
  77. val = val << i;
  78. return regmap_update_bits(priv->regmap, addr, mask, val);
  79. }
  80. static int
  81. tlc591xx_set_pwm(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
  82. u8 brightness)
  83. {
  84. u8 pwm = TLC591XX_REG_PWM(led->led_no);
  85. return regmap_write(priv->regmap, pwm, brightness);
  86. }
  87. static int
  88. tlc591xx_brightness_set(struct led_classdev *led_cdev,
  89. enum led_brightness brightness)
  90. {
  91. struct tlc591xx_led *led = ldev_to_led(led_cdev);
  92. struct tlc591xx_priv *priv = led->priv;
  93. int err;
  94. switch (brightness) {
  95. case 0:
  96. err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF);
  97. break;
  98. case LED_FULL:
  99. err = tlc591xx_set_ledout(priv, led, LEDOUT_ON);
  100. break;
  101. default:
  102. err = tlc591xx_set_ledout(priv, led, LEDOUT_DIM);
  103. if (!err)
  104. err = tlc591xx_set_pwm(priv, led, brightness);
  105. }
  106. return err;
  107. }
  108. static void
  109. tlc591xx_destroy_devices(struct tlc591xx_priv *priv, unsigned int j)
  110. {
  111. int i = j;
  112. while (--i >= 0) {
  113. if (priv->leds[i].active)
  114. led_classdev_unregister(&priv->leds[i].ldev);
  115. }
  116. }
  117. static int
  118. tlc591xx_configure(struct device *dev,
  119. struct tlc591xx_priv *priv,
  120. const struct tlc591xx *tlc591xx)
  121. {
  122. unsigned int i;
  123. int err = 0;
  124. tlc591xx_set_mode(priv->regmap, MODE2_DIM);
  125. for (i = 0; i < TLC591XX_MAX_LEDS; i++) {
  126. struct tlc591xx_led *led = &priv->leds[i];
  127. if (!led->active)
  128. continue;
  129. led->priv = priv;
  130. led->led_no = i;
  131. led->ldev.brightness_set_blocking = tlc591xx_brightness_set;
  132. led->ldev.max_brightness = LED_FULL;
  133. err = led_classdev_register(dev, &led->ldev);
  134. if (err < 0) {
  135. dev_err(dev, "couldn't register LED %s\n",
  136. led->ldev.name);
  137. goto exit;
  138. }
  139. }
  140. return 0;
  141. exit:
  142. tlc591xx_destroy_devices(priv, i);
  143. return err;
  144. }
  145. static const struct regmap_config tlc591xx_regmap = {
  146. .reg_bits = 8,
  147. .val_bits = 8,
  148. .max_register = 0x1e,
  149. };
  150. static const struct of_device_id of_tlc591xx_leds_match[] = {
  151. { .compatible = "ti,tlc59116",
  152. .data = &tlc59116 },
  153. { .compatible = "ti,tlc59108",
  154. .data = &tlc59108 },
  155. {},
  156. };
  157. MODULE_DEVICE_TABLE(of, of_tlc591xx_leds_match);
  158. static int
  159. tlc591xx_probe(struct i2c_client *client,
  160. const struct i2c_device_id *id)
  161. {
  162. struct device_node *np = client->dev.of_node, *child;
  163. struct device *dev = &client->dev;
  164. const struct of_device_id *match;
  165. const struct tlc591xx *tlc591xx;
  166. struct tlc591xx_priv *priv;
  167. int err, count, reg;
  168. match = of_match_device(of_tlc591xx_leds_match, dev);
  169. if (!match)
  170. return -ENODEV;
  171. tlc591xx = match->data;
  172. if (!np)
  173. return -ENODEV;
  174. count = of_get_child_count(np);
  175. if (!count || count > tlc591xx->max_leds)
  176. return -EINVAL;
  177. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  178. if (!priv)
  179. return -ENOMEM;
  180. priv->regmap = devm_regmap_init_i2c(client, &tlc591xx_regmap);
  181. if (IS_ERR(priv->regmap)) {
  182. err = PTR_ERR(priv->regmap);
  183. dev_err(dev, "Failed to allocate register map: %d\n", err);
  184. return err;
  185. }
  186. priv->reg_ledout_offset = tlc591xx->reg_ledout_offset;
  187. i2c_set_clientdata(client, priv);
  188. for_each_child_of_node(np, child) {
  189. err = of_property_read_u32(child, "reg", &reg);
  190. if (err)
  191. return err;
  192. if (reg < 0 || reg >= tlc591xx->max_leds)
  193. return -EINVAL;
  194. if (priv->leds[reg].active)
  195. return -EINVAL;
  196. priv->leds[reg].active = true;
  197. priv->leds[reg].ldev.name =
  198. of_get_property(child, "label", NULL) ? : child->name;
  199. priv->leds[reg].ldev.default_trigger =
  200. of_get_property(child, "linux,default-trigger", NULL);
  201. }
  202. return tlc591xx_configure(dev, priv, tlc591xx);
  203. }
  204. static int
  205. tlc591xx_remove(struct i2c_client *client)
  206. {
  207. struct tlc591xx_priv *priv = i2c_get_clientdata(client);
  208. tlc591xx_destroy_devices(priv, TLC591XX_MAX_LEDS);
  209. return 0;
  210. }
  211. static const struct i2c_device_id tlc591xx_id[] = {
  212. { "tlc59116" },
  213. { "tlc59108" },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(i2c, tlc591xx_id);
  217. static struct i2c_driver tlc591xx_driver = {
  218. .driver = {
  219. .name = "tlc591xx",
  220. .of_match_table = of_match_ptr(of_tlc591xx_leds_match),
  221. },
  222. .probe = tlc591xx_probe,
  223. .remove = tlc591xx_remove,
  224. .id_table = tlc591xx_id,
  225. };
  226. module_i2c_driver(tlc591xx_driver);
  227. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  228. MODULE_LICENSE("GPL");
  229. MODULE_DESCRIPTION("TLC591XX LED driver");