leds-lp8501.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * TI LP8501 9 channel LED Driver
  3. *
  4. * Copyright (C) 2013 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/firmware.h>
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/leds.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_data/leds-lp55xx.h>
  22. #include <linux/slab.h>
  23. #include "leds-lp55xx-common.h"
  24. #define LP8501_PROGRAM_LENGTH 32
  25. #define LP8501_MAX_LEDS 9
  26. /* Registers */
  27. #define LP8501_REG_ENABLE 0x00
  28. #define LP8501_ENABLE BIT(6)
  29. #define LP8501_EXEC_M 0x3F
  30. #define LP8501_EXEC_ENG1_M 0x30
  31. #define LP8501_EXEC_ENG2_M 0x0C
  32. #define LP8501_EXEC_ENG3_M 0x03
  33. #define LP8501_RUN_ENG1 0x20
  34. #define LP8501_RUN_ENG2 0x08
  35. #define LP8501_RUN_ENG3 0x02
  36. #define LP8501_REG_OP_MODE 0x01
  37. #define LP8501_MODE_ENG1_M 0x30
  38. #define LP8501_MODE_ENG2_M 0x0C
  39. #define LP8501_MODE_ENG3_M 0x03
  40. #define LP8501_LOAD_ENG1 0x10
  41. #define LP8501_LOAD_ENG2 0x04
  42. #define LP8501_LOAD_ENG3 0x01
  43. #define LP8501_REG_PWR_CONFIG 0x05
  44. #define LP8501_PWR_CONFIG_M 0x03
  45. #define LP8501_REG_LED_PWM_BASE 0x16
  46. #define LP8501_REG_LED_CURRENT_BASE 0x26
  47. #define LP8501_REG_CONFIG 0x36
  48. #define LP8501_PWM_PSAVE BIT(7)
  49. #define LP8501_AUTO_INC BIT(6)
  50. #define LP8501_PWR_SAVE BIT(5)
  51. #define LP8501_CP_AUTO 0x18
  52. #define LP8501_INT_CLK BIT(0)
  53. #define LP8501_DEFAULT_CFG \
  54. (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE | LP8501_CP_AUTO)
  55. #define LP8501_REG_RESET 0x3D
  56. #define LP8501_RESET 0xFF
  57. #define LP8501_REG_PROG_PAGE_SEL 0x4F
  58. #define LP8501_PAGE_ENG1 0
  59. #define LP8501_PAGE_ENG2 1
  60. #define LP8501_PAGE_ENG3 2
  61. #define LP8501_REG_PROG_MEM 0x50
  62. #define LP8501_ENG1_IS_LOADING(mode) \
  63. ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
  64. #define LP8501_ENG2_IS_LOADING(mode) \
  65. ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
  66. #define LP8501_ENG3_IS_LOADING(mode) \
  67. ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
  68. static inline void lp8501_wait_opmode_done(void)
  69. {
  70. usleep_range(1000, 2000);
  71. }
  72. static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
  73. {
  74. led->led_current = led_current;
  75. lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
  76. led_current);
  77. }
  78. static int lp8501_post_init_device(struct lp55xx_chip *chip)
  79. {
  80. int ret;
  81. u8 val = LP8501_DEFAULT_CFG;
  82. ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
  83. if (ret)
  84. return ret;
  85. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  86. usleep_range(1000, 2000);
  87. if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
  88. val |= LP8501_INT_CLK;
  89. ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
  90. if (ret)
  91. return ret;
  92. /* Power selection for each output */
  93. return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
  94. LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
  95. }
  96. static void lp8501_load_engine(struct lp55xx_chip *chip)
  97. {
  98. enum lp55xx_engine_index idx = chip->engine_idx;
  99. u8 mask[] = {
  100. [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
  101. [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
  102. [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
  103. };
  104. u8 val[] = {
  105. [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
  106. [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
  107. [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
  108. };
  109. u8 page_sel[] = {
  110. [LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
  111. [LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
  112. [LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
  113. };
  114. lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]);
  115. lp8501_wait_opmode_done();
  116. lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
  117. }
  118. static void lp8501_stop_engine(struct lp55xx_chip *chip)
  119. {
  120. lp55xx_write(chip, LP8501_REG_OP_MODE, 0);
  121. lp8501_wait_opmode_done();
  122. }
  123. static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
  124. {
  125. int i;
  126. for (i = 0; i < LP8501_MAX_LEDS; i++)
  127. lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
  128. }
  129. static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
  130. {
  131. int ret;
  132. u8 mode;
  133. u8 exec;
  134. /* stop engine */
  135. if (!start) {
  136. lp8501_stop_engine(chip);
  137. lp8501_turn_off_channels(chip);
  138. return;
  139. }
  140. /*
  141. * To run the engine,
  142. * operation mode and enable register should updated at the same time
  143. */
  144. ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
  145. if (ret)
  146. return;
  147. ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
  148. if (ret)
  149. return;
  150. /* change operation mode to RUN only when each engine is loading */
  151. if (LP8501_ENG1_IS_LOADING(mode)) {
  152. mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1;
  153. exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1;
  154. }
  155. if (LP8501_ENG2_IS_LOADING(mode)) {
  156. mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2;
  157. exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2;
  158. }
  159. if (LP8501_ENG3_IS_LOADING(mode)) {
  160. mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3;
  161. exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3;
  162. }
  163. lp55xx_write(chip, LP8501_REG_OP_MODE, mode);
  164. lp8501_wait_opmode_done();
  165. lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec);
  166. }
  167. static int lp8501_update_program_memory(struct lp55xx_chip *chip,
  168. const u8 *data, size_t size)
  169. {
  170. u8 pattern[LP8501_PROGRAM_LENGTH] = {0};
  171. unsigned cmd;
  172. char c[3];
  173. int update_size;
  174. int nrchars;
  175. int offset = 0;
  176. int ret;
  177. int i;
  178. /* clear program memory before updating */
  179. for (i = 0; i < LP8501_PROGRAM_LENGTH; i++)
  180. lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0);
  181. i = 0;
  182. while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) {
  183. /* separate sscanfs because length is working only for %s */
  184. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  185. if (ret != 1)
  186. goto err;
  187. ret = sscanf(c, "%2x", &cmd);
  188. if (ret != 1)
  189. goto err;
  190. pattern[i] = (u8)cmd;
  191. offset += nrchars;
  192. i++;
  193. }
  194. /* Each instruction is 16bit long. Check that length is even */
  195. if (i % 2)
  196. goto err;
  197. update_size = i;
  198. for (i = 0; i < update_size; i++)
  199. lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
  200. return 0;
  201. err:
  202. dev_err(&chip->cl->dev, "wrong pattern format\n");
  203. return -EINVAL;
  204. }
  205. static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
  206. {
  207. const struct firmware *fw = chip->fw;
  208. if (fw->size > LP8501_PROGRAM_LENGTH) {
  209. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  210. fw->size);
  211. return;
  212. }
  213. /*
  214. * Program memory sequence
  215. * 1) set engine mode to "LOAD"
  216. * 2) write firmware data into program memory
  217. */
  218. lp8501_load_engine(chip);
  219. lp8501_update_program_memory(chip, fw->data, fw->size);
  220. }
  221. static void lp8501_led_brightness_work(struct work_struct *work)
  222. {
  223. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  224. brightness_work);
  225. struct lp55xx_chip *chip = led->chip;
  226. mutex_lock(&chip->lock);
  227. lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
  228. led->brightness);
  229. mutex_unlock(&chip->lock);
  230. }
  231. /* Chip specific configurations */
  232. static struct lp55xx_device_config lp8501_cfg = {
  233. .reset = {
  234. .addr = LP8501_REG_RESET,
  235. .val = LP8501_RESET,
  236. },
  237. .enable = {
  238. .addr = LP8501_REG_ENABLE,
  239. .val = LP8501_ENABLE,
  240. },
  241. .max_channel = LP8501_MAX_LEDS,
  242. .post_init_device = lp8501_post_init_device,
  243. .brightness_work_fn = lp8501_led_brightness_work,
  244. .set_led_current = lp8501_set_led_current,
  245. .firmware_cb = lp8501_firmware_loaded,
  246. .run_engine = lp8501_run_engine,
  247. };
  248. static int lp8501_probe(struct i2c_client *client,
  249. const struct i2c_device_id *id)
  250. {
  251. int ret;
  252. struct lp55xx_chip *chip;
  253. struct lp55xx_led *led;
  254. struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
  255. struct device_node *np = client->dev.of_node;
  256. if (!pdata) {
  257. if (np) {
  258. pdata = lp55xx_of_populate_pdata(&client->dev, np);
  259. if (IS_ERR(pdata))
  260. return PTR_ERR(pdata);
  261. } else {
  262. dev_err(&client->dev, "no platform data\n");
  263. return -EINVAL;
  264. }
  265. }
  266. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  267. if (!chip)
  268. return -ENOMEM;
  269. led = devm_kzalloc(&client->dev,
  270. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  271. if (!led)
  272. return -ENOMEM;
  273. chip->cl = client;
  274. chip->pdata = pdata;
  275. chip->cfg = &lp8501_cfg;
  276. mutex_init(&chip->lock);
  277. i2c_set_clientdata(client, led);
  278. ret = lp55xx_init_device(chip);
  279. if (ret)
  280. goto err_init;
  281. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  282. ret = lp55xx_register_leds(led, chip);
  283. if (ret)
  284. goto err_register_leds;
  285. ret = lp55xx_register_sysfs(chip);
  286. if (ret) {
  287. dev_err(&client->dev, "registering sysfs failed\n");
  288. goto err_register_sysfs;
  289. }
  290. return 0;
  291. err_register_sysfs:
  292. lp55xx_unregister_leds(led, chip);
  293. err_register_leds:
  294. lp55xx_deinit_device(chip);
  295. err_init:
  296. return ret;
  297. }
  298. static int lp8501_remove(struct i2c_client *client)
  299. {
  300. struct lp55xx_led *led = i2c_get_clientdata(client);
  301. struct lp55xx_chip *chip = led->chip;
  302. lp8501_stop_engine(chip);
  303. lp55xx_unregister_sysfs(chip);
  304. lp55xx_unregister_leds(led, chip);
  305. lp55xx_deinit_device(chip);
  306. return 0;
  307. }
  308. static const struct i2c_device_id lp8501_id[] = {
  309. { "lp8501", 0 },
  310. { }
  311. };
  312. MODULE_DEVICE_TABLE(i2c, lp8501_id);
  313. #ifdef CONFIG_OF
  314. static const struct of_device_id of_lp8501_leds_match[] = {
  315. { .compatible = "ti,lp8501", },
  316. {},
  317. };
  318. MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
  319. #endif
  320. static struct i2c_driver lp8501_driver = {
  321. .driver = {
  322. .name = "lp8501",
  323. .of_match_table = of_match_ptr(of_lp8501_leds_match),
  324. },
  325. .probe = lp8501_probe,
  326. .remove = lp8501_remove,
  327. .id_table = lp8501_id,
  328. };
  329. module_i2c_driver(lp8501_driver);
  330. MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
  331. MODULE_AUTHOR("Milo Kim");
  332. MODULE_LICENSE("GPL");