leds-cr0014114.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018 Crane Merchandising Systems. All rights reserved.
  3. // Copyright (C) 2018 Oleh Kravchenko <oleg@kaa.org.ua>
  4. #include <linux/delay.h>
  5. #include <linux/leds.h>
  6. #include <linux/module.h>
  7. #include <linux/of_device.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/workqueue.h>
  10. #include <uapi/linux/uleds.h>
  11. /*
  12. * CR0014114 SPI protocol descrtiption:
  13. * +----+-----------------------------------+----+
  14. * | CMD| BRIGHTNESS |CRC |
  15. * +----+-----------------------------------+----+
  16. * | | LED0| LED1| LED2| LED3| LED4| LED5| |
  17. * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  18. * | |R|G|B|R|G|B|R|G|B|R|G|B|R|G|B|R|G|B| |
  19. * | 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1 |
  20. * | |1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1| |
  21. * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
  22. * | | 18 | |
  23. * +----+-----------------------------------+----+
  24. * | 20 |
  25. * +---------------------------------------------+
  26. *
  27. * PS: Boards can be connected to the chain:
  28. * SPI -> board0 -> board1 -> board2 ..
  29. */
  30. /* CR0014114 SPI commands */
  31. #define CR_SET_BRIGHTNESS 0x80
  32. #define CR_INIT_REENUMERATE 0x81
  33. #define CR_NEXT_REENUMERATE 0x82
  34. /* CR0014114 default settings */
  35. #define CR_MAX_BRIGHTNESS GENMASK(6, 0)
  36. #define CR_FW_DELAY_MSEC 10
  37. #define CR_RECOUNT_DELAY (HZ * 3600)
  38. struct cr0014114_led {
  39. char name[LED_MAX_NAME_SIZE];
  40. struct cr0014114 *priv;
  41. struct led_classdev ldev;
  42. u8 brightness;
  43. };
  44. struct cr0014114 {
  45. bool do_recount;
  46. size_t count;
  47. struct delayed_work work;
  48. struct device *dev;
  49. struct mutex lock;
  50. struct spi_device *spi;
  51. u8 *buf;
  52. unsigned long delay;
  53. struct cr0014114_led leds[];
  54. };
  55. static void cr0014114_calc_crc(u8 *buf, const size_t len)
  56. {
  57. size_t i;
  58. u8 crc;
  59. for (i = 1, crc = 1; i < len - 1; i++)
  60. crc += buf[i];
  61. crc |= BIT(7);
  62. /* special case when CRC matches the SPI commands */
  63. if (crc == CR_SET_BRIGHTNESS ||
  64. crc == CR_INIT_REENUMERATE ||
  65. crc == CR_NEXT_REENUMERATE)
  66. crc = 0xfe;
  67. buf[len - 1] = crc;
  68. }
  69. static int cr0014114_recount(struct cr0014114 *priv)
  70. {
  71. int ret;
  72. size_t i;
  73. u8 cmd;
  74. dev_dbg(priv->dev, "LEDs recount is started\n");
  75. cmd = CR_INIT_REENUMERATE;
  76. ret = spi_write(priv->spi, &cmd, sizeof(cmd));
  77. if (ret)
  78. goto err;
  79. cmd = CR_NEXT_REENUMERATE;
  80. for (i = 0; i < priv->count; i++) {
  81. msleep(CR_FW_DELAY_MSEC);
  82. ret = spi_write(priv->spi, &cmd, sizeof(cmd));
  83. if (ret)
  84. goto err;
  85. }
  86. err:
  87. dev_dbg(priv->dev, "LEDs recount is finished\n");
  88. if (ret)
  89. dev_err(priv->dev, "with error %d", ret);
  90. return ret;
  91. }
  92. static int cr0014114_sync(struct cr0014114 *priv)
  93. {
  94. int ret;
  95. size_t i;
  96. unsigned long udelay, now = jiffies;
  97. /* to avoid SPI mistiming with firmware we should wait some time */
  98. if (time_after(priv->delay, now)) {
  99. udelay = jiffies_to_usecs(priv->delay - now);
  100. usleep_range(udelay, udelay + 1);
  101. }
  102. if (unlikely(priv->do_recount)) {
  103. ret = cr0014114_recount(priv);
  104. if (ret)
  105. goto err;
  106. priv->do_recount = false;
  107. msleep(CR_FW_DELAY_MSEC);
  108. }
  109. priv->buf[0] = CR_SET_BRIGHTNESS;
  110. for (i = 0; i < priv->count; i++)
  111. priv->buf[i + 1] = priv->leds[i].brightness;
  112. cr0014114_calc_crc(priv->buf, priv->count + 2);
  113. ret = spi_write(priv->spi, priv->buf, priv->count + 2);
  114. err:
  115. priv->delay = jiffies + msecs_to_jiffies(CR_FW_DELAY_MSEC);
  116. return ret;
  117. }
  118. static void cr0014114_recount_work(struct work_struct *work)
  119. {
  120. int ret;
  121. struct cr0014114 *priv = container_of(work,
  122. struct cr0014114,
  123. work.work);
  124. mutex_lock(&priv->lock);
  125. priv->do_recount = true;
  126. ret = cr0014114_sync(priv);
  127. mutex_unlock(&priv->lock);
  128. if (ret)
  129. dev_warn(priv->dev, "sync of LEDs failed %d\n", ret);
  130. schedule_delayed_work(&priv->work, CR_RECOUNT_DELAY);
  131. }
  132. static int cr0014114_set_sync(struct led_classdev *ldev,
  133. enum led_brightness brightness)
  134. {
  135. int ret;
  136. struct cr0014114_led *led = container_of(ldev,
  137. struct cr0014114_led,
  138. ldev);
  139. dev_dbg(led->priv->dev, "Set brightness of %s to %d\n",
  140. led->name, brightness);
  141. mutex_lock(&led->priv->lock);
  142. led->brightness = (u8)brightness;
  143. ret = cr0014114_sync(led->priv);
  144. mutex_unlock(&led->priv->lock);
  145. return ret;
  146. }
  147. static int cr0014114_probe_dt(struct cr0014114 *priv)
  148. {
  149. size_t i = 0;
  150. struct cr0014114_led *led;
  151. struct fwnode_handle *child;
  152. struct device_node *np;
  153. int ret;
  154. const char *str;
  155. device_for_each_child_node(priv->dev, child) {
  156. np = to_of_node(child);
  157. led = &priv->leds[i];
  158. ret = fwnode_property_read_string(child, "label", &str);
  159. if (ret)
  160. snprintf(led->name, sizeof(led->name),
  161. "cr0014114::");
  162. else
  163. snprintf(led->name, sizeof(led->name),
  164. "cr0014114:%s", str);
  165. fwnode_property_read_string(child, "linux,default-trigger",
  166. &led->ldev.default_trigger);
  167. led->priv = priv;
  168. led->ldev.name = led->name;
  169. led->ldev.max_brightness = CR_MAX_BRIGHTNESS;
  170. led->ldev.brightness_set_blocking = cr0014114_set_sync;
  171. ret = devm_of_led_classdev_register(priv->dev, np,
  172. &led->ldev);
  173. if (ret) {
  174. dev_err(priv->dev,
  175. "failed to register LED device %s, err %d",
  176. led->name, ret);
  177. fwnode_handle_put(child);
  178. return ret;
  179. }
  180. led->ldev.dev->of_node = np;
  181. i++;
  182. }
  183. return 0;
  184. }
  185. static int cr0014114_probe(struct spi_device *spi)
  186. {
  187. struct cr0014114 *priv;
  188. size_t count;
  189. int ret;
  190. count = device_get_child_node_count(&spi->dev);
  191. if (!count) {
  192. dev_err(&spi->dev, "LEDs are not defined in device tree!");
  193. return -ENODEV;
  194. }
  195. priv = devm_kzalloc(&spi->dev, struct_size(priv, leds, count),
  196. GFP_KERNEL);
  197. if (!priv)
  198. return -ENOMEM;
  199. priv->buf = devm_kzalloc(&spi->dev, count + 2, GFP_KERNEL);
  200. if (!priv->buf)
  201. return -ENOMEM;
  202. mutex_init(&priv->lock);
  203. INIT_DELAYED_WORK(&priv->work, cr0014114_recount_work);
  204. priv->count = count;
  205. priv->dev = &spi->dev;
  206. priv->spi = spi;
  207. priv->delay = jiffies -
  208. msecs_to_jiffies(CR_FW_DELAY_MSEC);
  209. priv->do_recount = true;
  210. ret = cr0014114_sync(priv);
  211. if (ret) {
  212. dev_err(priv->dev, "first recount failed %d\n", ret);
  213. return ret;
  214. }
  215. priv->do_recount = true;
  216. ret = cr0014114_sync(priv);
  217. if (ret) {
  218. dev_err(priv->dev, "second recount failed %d\n", ret);
  219. return ret;
  220. }
  221. ret = cr0014114_probe_dt(priv);
  222. if (ret)
  223. return ret;
  224. /* setup recount work to workaround buggy firmware */
  225. schedule_delayed_work(&priv->work, CR_RECOUNT_DELAY);
  226. spi_set_drvdata(spi, priv);
  227. return 0;
  228. }
  229. static int cr0014114_remove(struct spi_device *spi)
  230. {
  231. struct cr0014114 *priv = spi_get_drvdata(spi);
  232. cancel_delayed_work_sync(&priv->work);
  233. mutex_destroy(&priv->lock);
  234. return 0;
  235. }
  236. static const struct of_device_id cr0014114_dt_ids[] = {
  237. { .compatible = "crane,cr0014114", },
  238. {},
  239. };
  240. MODULE_DEVICE_TABLE(of, cr0014114_dt_ids);
  241. static struct spi_driver cr0014114_driver = {
  242. .probe = cr0014114_probe,
  243. .remove = cr0014114_remove,
  244. .driver = {
  245. .name = KBUILD_MODNAME,
  246. .of_match_table = cr0014114_dt_ids,
  247. },
  248. };
  249. module_spi_driver(cr0014114_driver);
  250. MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
  251. MODULE_DESCRIPTION("cr0014114 LED driver");
  252. MODULE_LICENSE("GPL v2");
  253. MODULE_ALIAS("spi:cr0014114");