leds-lp5562.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * LP5562 LED driver
  4. *
  5. * Copyright (C) 2013 Texas Instruments
  6. *
  7. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  8. */
  9. #include <linux/cleanup.h>
  10. #include <linux/delay.h>
  11. #include <linux/firmware.h>
  12. #include <linux/i2c.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_data/leds-lp55xx.h>
  18. #include <linux/slab.h>
  19. #include "leds-lp55xx-common.h"
  20. #define LP5562_MAX_LEDS 4
  21. /* ENABLE Register 00h */
  22. #define LP5562_REG_ENABLE 0x00
  23. #define LP5562_EXEC_ENG1_M 0x30
  24. #define LP5562_EXEC_ENG2_M 0x0C
  25. #define LP5562_EXEC_ENG3_M 0x03
  26. #define LP5562_EXEC_M 0x3F
  27. #define LP5562_MASTER_ENABLE 0x40 /* Chip master enable */
  28. #define LP5562_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  29. #define LP5562_EXEC_RUN 0x2A
  30. #define LP5562_ENABLE_DEFAULT \
  31. (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM)
  32. #define LP5562_ENABLE_RUN_PROGRAM \
  33. (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN)
  34. /* OPMODE Register 01h */
  35. #define LP5562_REG_OP_MODE 0x01
  36. /* BRIGHTNESS Registers */
  37. #define LP5562_REG_R_PWM 0x04
  38. #define LP5562_REG_G_PWM 0x03
  39. #define LP5562_REG_B_PWM 0x02
  40. #define LP5562_REG_W_PWM 0x0E
  41. /* CURRENT Registers */
  42. #define LP5562_REG_R_CURRENT 0x07
  43. #define LP5562_REG_G_CURRENT 0x06
  44. #define LP5562_REG_B_CURRENT 0x05
  45. #define LP5562_REG_W_CURRENT 0x0F
  46. /* CONFIG Register 08h */
  47. #define LP5562_REG_CONFIG 0x08
  48. #define LP5562_PWM_HF 0x40
  49. #define LP5562_PWRSAVE_EN 0x20
  50. #define LP5562_CLK_INT 0x01 /* Internal clock */
  51. #define LP5562_DEFAULT_CFG (LP5562_PWM_HF | LP5562_PWRSAVE_EN)
  52. /* RESET Register 0Dh */
  53. #define LP5562_REG_RESET 0x0D
  54. #define LP5562_RESET 0xFF
  55. /* PROGRAM ENGINE Registers */
  56. #define LP5562_REG_PROG_MEM_ENG1 0x10
  57. #define LP5562_REG_PROG_MEM_ENG2 0x30
  58. #define LP5562_REG_PROG_MEM_ENG3 0x50
  59. /* LEDMAP Register 70h */
  60. #define LP5562_REG_ENG_SEL 0x70
  61. #define LP5562_ENG_SEL_PWM 0
  62. #define LP5562_ENG_FOR_RGB_M 0x3F
  63. #define LP5562_ENG_SEL_RGB 0x1B /* R:ENG1, G:ENG2, B:ENG3 */
  64. #define LP5562_ENG_FOR_W_M 0xC0
  65. #define LP5562_ENG1_FOR_W 0x40 /* W:ENG1 */
  66. #define LP5562_ENG2_FOR_W 0x80 /* W:ENG2 */
  67. #define LP5562_ENG3_FOR_W 0xC0 /* W:ENG3 */
  68. /* Program Commands */
  69. #define LP5562_CMD_DISABLE 0x00
  70. #define LP5562_CMD_LOAD 0x15
  71. #define LP5562_CMD_RUN 0x2A
  72. #define LP5562_CMD_DIRECT 0x3F
  73. #define LP5562_PATTERN_OFF 0
  74. static inline void lp5562_wait_opmode_done(void)
  75. {
  76. /* operation mode change needs to be longer than 153 us */
  77. usleep_range(200, 300);
  78. }
  79. static inline void lp5562_wait_enable_done(void)
  80. {
  81. /* it takes more 488 us to update ENABLE register */
  82. usleep_range(500, 600);
  83. }
  84. static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
  85. {
  86. static const u8 addr[] = {
  87. LP5562_REG_R_CURRENT,
  88. LP5562_REG_G_CURRENT,
  89. LP5562_REG_B_CURRENT,
  90. LP5562_REG_W_CURRENT,
  91. };
  92. led->led_current = led_current;
  93. lp55xx_write(led->chip, addr[led->chan_nr], led_current);
  94. }
  95. static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
  96. {
  97. int ret;
  98. /* stop engine */
  99. if (!start) {
  100. lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
  101. lp5562_wait_enable_done();
  102. lp55xx_stop_all_engine(chip);
  103. lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
  104. lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
  105. lp5562_wait_opmode_done();
  106. return;
  107. }
  108. ret = lp55xx_run_engine_common(chip);
  109. if (!ret)
  110. lp5562_wait_enable_done();
  111. }
  112. static int lp5562_post_init_device(struct lp55xx_chip *chip)
  113. {
  114. int ret;
  115. u8 cfg = LP5562_DEFAULT_CFG;
  116. /* Set all PWMs to direct control mode */
  117. ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
  118. if (ret)
  119. return ret;
  120. lp5562_wait_opmode_done();
  121. /* Update configuration for the clock setting */
  122. if (!lp55xx_is_extclk_used(chip))
  123. cfg |= LP5562_CLK_INT;
  124. ret = lp55xx_write(chip, LP5562_REG_CONFIG, cfg);
  125. if (ret)
  126. return ret;
  127. /* Initialize all channels PWM to zero -> leds off */
  128. lp55xx_write(chip, LP5562_REG_R_PWM, 0);
  129. lp55xx_write(chip, LP5562_REG_G_PWM, 0);
  130. lp55xx_write(chip, LP5562_REG_B_PWM, 0);
  131. lp55xx_write(chip, LP5562_REG_W_PWM, 0);
  132. /* Set LED map as register PWM by default */
  133. lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
  134. return 0;
  135. }
  136. static int lp5562_led_brightness(struct lp55xx_led *led)
  137. {
  138. struct lp55xx_chip *chip = led->chip;
  139. static const u8 addr[] = {
  140. LP5562_REG_R_PWM,
  141. LP5562_REG_G_PWM,
  142. LP5562_REG_B_PWM,
  143. LP5562_REG_W_PWM,
  144. };
  145. int ret;
  146. guard(mutex)(&chip->lock);
  147. ret = lp55xx_write(chip, addr[led->chan_nr], led->brightness);
  148. return ret;
  149. }
  150. static void lp5562_write_program_memory(struct lp55xx_chip *chip,
  151. u8 base, const u8 *rgb, int size)
  152. {
  153. int i;
  154. if (!rgb || size <= 0)
  155. return;
  156. for (i = 0; i < size; i++)
  157. lp55xx_write(chip, base + i, *(rgb + i));
  158. lp55xx_write(chip, base + i, 0);
  159. lp55xx_write(chip, base + i + 1, 0);
  160. }
  161. /* check the size of program count */
  162. static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
  163. {
  164. return ptn->size_r >= LP55xx_BYTES_PER_PAGE ||
  165. ptn->size_g >= LP55xx_BYTES_PER_PAGE ||
  166. ptn->size_b >= LP55xx_BYTES_PER_PAGE;
  167. }
  168. static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
  169. {
  170. struct lp55xx_predef_pattern *ptn;
  171. int i;
  172. if (mode == LP5562_PATTERN_OFF) {
  173. lp5562_run_engine(chip, false);
  174. return 0;
  175. }
  176. ptn = chip->pdata->patterns + (mode - 1);
  177. if (!ptn || _is_pc_overflow(ptn)) {
  178. dev_err(&chip->cl->dev, "invalid pattern data\n");
  179. return -EINVAL;
  180. }
  181. lp55xx_stop_all_engine(chip);
  182. /* Set LED map as RGB */
  183. lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
  184. /* Load engines */
  185. for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
  186. chip->engine_idx = i;
  187. lp55xx_load_engine(chip);
  188. }
  189. /* Clear program registers */
  190. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
  191. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
  192. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
  193. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
  194. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
  195. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
  196. /* Program engines */
  197. lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
  198. ptn->r, ptn->size_r);
  199. lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
  200. ptn->g, ptn->size_g);
  201. lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
  202. ptn->b, ptn->size_b);
  203. /* Run engines */
  204. lp5562_run_engine(chip, true);
  205. return 0;
  206. }
  207. static ssize_t lp5562_store_pattern(struct device *dev,
  208. struct device_attribute *attr,
  209. const char *buf, size_t len)
  210. {
  211. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  212. struct lp55xx_chip *chip = led->chip;
  213. struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
  214. int num_patterns = chip->pdata->num_patterns;
  215. unsigned long mode;
  216. int ret;
  217. ret = kstrtoul(buf, 0, &mode);
  218. if (ret)
  219. return ret;
  220. if (mode > num_patterns || !ptn)
  221. return -EINVAL;
  222. guard(mutex)(&chip->lock);
  223. ret = lp5562_run_predef_led_pattern(chip, mode);
  224. if (ret)
  225. return ret;
  226. return len;
  227. }
  228. static ssize_t lp5562_store_engine_mux(struct device *dev,
  229. struct device_attribute *attr,
  230. const char *buf, size_t len)
  231. {
  232. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  233. struct lp55xx_chip *chip = led->chip;
  234. u8 mask;
  235. u8 val;
  236. /* LED map
  237. * R ... Engine 1 (fixed)
  238. * G ... Engine 2 (fixed)
  239. * B ... Engine 3 (fixed)
  240. * W ... Engine 1 or 2 or 3
  241. */
  242. if (sysfs_streq(buf, "RGB")) {
  243. mask = LP5562_ENG_FOR_RGB_M;
  244. val = LP5562_ENG_SEL_RGB;
  245. } else if (sysfs_streq(buf, "W")) {
  246. enum lp55xx_engine_index idx = chip->engine_idx;
  247. mask = LP5562_ENG_FOR_W_M;
  248. switch (idx) {
  249. case LP55XX_ENGINE_1:
  250. val = LP5562_ENG1_FOR_W;
  251. break;
  252. case LP55XX_ENGINE_2:
  253. val = LP5562_ENG2_FOR_W;
  254. break;
  255. case LP55XX_ENGINE_3:
  256. val = LP5562_ENG3_FOR_W;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. } else {
  262. dev_err(dev, "choose RGB or W\n");
  263. return -EINVAL;
  264. }
  265. guard(mutex)(&chip->lock);
  266. lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
  267. return len;
  268. }
  269. static LP55XX_DEV_ATTR_WO(led_pattern, lp5562_store_pattern);
  270. static LP55XX_DEV_ATTR_WO(engine_mux, lp5562_store_engine_mux);
  271. static struct attribute *lp5562_attributes[] = {
  272. &dev_attr_led_pattern.attr,
  273. &dev_attr_engine_mux.attr,
  274. NULL,
  275. };
  276. static const struct attribute_group lp5562_group = {
  277. .attrs = lp5562_attributes,
  278. };
  279. /* Chip specific configurations */
  280. static struct lp55xx_device_config lp5562_cfg = {
  281. .max_channel = LP5562_MAX_LEDS,
  282. .reg_op_mode = {
  283. .addr = LP5562_REG_OP_MODE,
  284. },
  285. .reg_exec = {
  286. .addr = LP5562_REG_ENABLE,
  287. },
  288. .reset = {
  289. .addr = LP5562_REG_RESET,
  290. .val = LP5562_RESET,
  291. },
  292. .enable = {
  293. .addr = LP5562_REG_ENABLE,
  294. .val = LP5562_ENABLE_DEFAULT,
  295. },
  296. .prog_mem_base = {
  297. .addr = LP5562_REG_PROG_MEM_ENG1,
  298. },
  299. .post_init_device = lp5562_post_init_device,
  300. .set_led_current = lp5562_set_led_current,
  301. .brightness_fn = lp5562_led_brightness,
  302. .run_engine = lp5562_run_engine,
  303. .firmware_cb = lp55xx_firmware_loaded_cb,
  304. .dev_attr_group = &lp5562_group,
  305. };
  306. static const struct i2c_device_id lp5562_id[] = {
  307. { "lp5562", .driver_data = (kernel_ulong_t)&lp5562_cfg, },
  308. { }
  309. };
  310. MODULE_DEVICE_TABLE(i2c, lp5562_id);
  311. static const struct of_device_id of_lp5562_leds_match[] = {
  312. { .compatible = "ti,lp5562", .data = &lp5562_cfg, },
  313. {},
  314. };
  315. MODULE_DEVICE_TABLE(of, of_lp5562_leds_match);
  316. static struct i2c_driver lp5562_driver = {
  317. .driver = {
  318. .name = "lp5562",
  319. .of_match_table = of_lp5562_leds_match,
  320. },
  321. .probe = lp55xx_probe,
  322. .remove = lp55xx_remove,
  323. .id_table = lp5562_id,
  324. };
  325. module_i2c_driver(lp5562_driver);
  326. MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
  327. MODULE_AUTHOR("Milo Kim");
  328. MODULE_LICENSE("GPL");