leds-cr0014114.c 6.7 KB

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