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/platform_data/leds-lp55xx.h>
  21. #include <linux/slab.h>
  22. #include "leds-lp55xx-common.h"
  23. #define LP8501_PROGRAM_LENGTH 32
  24. #define LP8501_MAX_LEDS 9
  25. /* Registers */
  26. #define LP8501_REG_ENABLE 0x00
  27. #define LP8501_ENABLE BIT(6)
  28. #define LP8501_EXEC_M 0x3F
  29. #define LP8501_EXEC_ENG1_M 0x30
  30. #define LP8501_EXEC_ENG2_M 0x0C
  31. #define LP8501_EXEC_ENG3_M 0x03
  32. #define LP8501_RUN_ENG1 0x20
  33. #define LP8501_RUN_ENG2 0x08
  34. #define LP8501_RUN_ENG3 0x02
  35. #define LP8501_REG_OP_MODE 0x01
  36. #define LP8501_MODE_ENG1_M 0x30
  37. #define LP8501_MODE_ENG2_M 0x0C
  38. #define LP8501_MODE_ENG3_M 0x03
  39. #define LP8501_LOAD_ENG1 0x10
  40. #define LP8501_LOAD_ENG2 0x04
  41. #define LP8501_LOAD_ENG3 0x01
  42. #define LP8501_REG_PWR_CONFIG 0x05
  43. #define LP8501_PWR_CONFIG_M 0x03
  44. #define LP8501_REG_LED_PWM_BASE 0x16
  45. #define LP8501_REG_LED_CURRENT_BASE 0x26
  46. #define LP8501_REG_CONFIG 0x36
  47. #define LP8501_PWM_PSAVE BIT(7)
  48. #define LP8501_AUTO_INC BIT(6)
  49. #define LP8501_PWR_SAVE BIT(5)
  50. #define LP8501_CP_AUTO 0x18
  51. #define LP8501_INT_CLK BIT(0)
  52. #define LP8501_DEFAULT_CFG \
  53. (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE | LP8501_CP_AUTO)
  54. #define LP8501_REG_RESET 0x3D
  55. #define LP8501_RESET 0xFF
  56. #define LP8501_REG_PROG_PAGE_SEL 0x4F
  57. #define LP8501_PAGE_ENG1 0
  58. #define LP8501_PAGE_ENG2 1
  59. #define LP8501_PAGE_ENG3 2
  60. #define LP8501_REG_PROG_MEM 0x50
  61. #define LP8501_ENG1_IS_LOADING(mode) \
  62. ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
  63. #define LP8501_ENG2_IS_LOADING(mode) \
  64. ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
  65. #define LP8501_ENG3_IS_LOADING(mode) \
  66. ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
  67. static inline void lp8501_wait_opmode_done(void)
  68. {
  69. usleep_range(1000, 2000);
  70. }
  71. static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
  72. {
  73. led->led_current = led_current;
  74. lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
  75. led_current);
  76. }
  77. static int lp8501_post_init_device(struct lp55xx_chip *chip)
  78. {
  79. int ret;
  80. u8 val = LP8501_DEFAULT_CFG;
  81. ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
  82. if (ret)
  83. return ret;
  84. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  85. usleep_range(1000, 2000);
  86. if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
  87. val |= LP8501_INT_CLK;
  88. ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
  89. if (ret)
  90. return ret;
  91. /* Power selection for each output */
  92. return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
  93. LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
  94. }
  95. static void lp8501_load_engine(struct lp55xx_chip *chip)
  96. {
  97. enum lp55xx_engine_index idx = chip->engine_idx;
  98. u8 mask[] = {
  99. [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
  100. [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
  101. [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
  102. };
  103. u8 val[] = {
  104. [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
  105. [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
  106. [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
  107. };
  108. u8 page_sel[] = {
  109. [LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
  110. [LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
  111. [LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
  112. };
  113. lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]);
  114. lp8501_wait_opmode_done();
  115. lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
  116. }
  117. static void lp8501_stop_engine(struct lp55xx_chip *chip)
  118. {
  119. lp55xx_write(chip, LP8501_REG_OP_MODE, 0);
  120. lp8501_wait_opmode_done();
  121. }
  122. static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
  123. {
  124. int i;
  125. for (i = 0; i < LP8501_MAX_LEDS; i++)
  126. lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
  127. }
  128. static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
  129. {
  130. int ret;
  131. u8 mode;
  132. u8 exec;
  133. /* stop engine */
  134. if (!start) {
  135. lp8501_stop_engine(chip);
  136. lp8501_turn_off_channels(chip);
  137. return;
  138. }
  139. /*
  140. * To run the engine,
  141. * operation mode and enable register should updated at the same time
  142. */
  143. ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
  144. if (ret)
  145. return;
  146. ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
  147. if (ret)
  148. return;
  149. /* change operation mode to RUN only when each engine is loading */
  150. if (LP8501_ENG1_IS_LOADING(mode)) {
  151. mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1;
  152. exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1;
  153. }
  154. if (LP8501_ENG2_IS_LOADING(mode)) {
  155. mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2;
  156. exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2;
  157. }
  158. if (LP8501_ENG3_IS_LOADING(mode)) {
  159. mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3;
  160. exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3;
  161. }
  162. lp55xx_write(chip, LP8501_REG_OP_MODE, mode);
  163. lp8501_wait_opmode_done();
  164. lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec);
  165. }
  166. static int lp8501_update_program_memory(struct lp55xx_chip *chip,
  167. const u8 *data, size_t size)
  168. {
  169. u8 pattern[LP8501_PROGRAM_LENGTH] = {0};
  170. unsigned cmd;
  171. char c[3];
  172. int update_size;
  173. int nrchars;
  174. int offset = 0;
  175. int ret;
  176. int i;
  177. /* clear program memory before updating */
  178. for (i = 0; i < LP8501_PROGRAM_LENGTH; i++)
  179. lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0);
  180. i = 0;
  181. while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) {
  182. /* separate sscanfs because length is working only for %s */
  183. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  184. if (ret != 1)
  185. goto err;
  186. ret = sscanf(c, "%2x", &cmd);
  187. if (ret != 1)
  188. goto err;
  189. pattern[i] = (u8)cmd;
  190. offset += nrchars;
  191. i++;
  192. }
  193. /* Each instruction is 16bit long. Check that length is even */
  194. if (i % 2)
  195. goto err;
  196. update_size = i;
  197. for (i = 0; i < update_size; i++)
  198. lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
  199. return 0;
  200. err:
  201. dev_err(&chip->cl->dev, "wrong pattern format\n");
  202. return -EINVAL;
  203. }
  204. static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
  205. {
  206. const struct firmware *fw = chip->fw;
  207. if (fw->size > LP8501_PROGRAM_LENGTH) {
  208. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  209. fw->size);
  210. return;
  211. }
  212. /*
  213. * Program momery sequence
  214. * 1) set engine mode to "LOAD"
  215. * 2) write firmware data into program memory
  216. */
  217. lp8501_load_engine(chip);
  218. lp8501_update_program_memory(chip, fw->data, fw->size);
  219. }
  220. static void lp8501_led_brightness_work(struct work_struct *work)
  221. {
  222. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  223. brightness_work);
  224. struct lp55xx_chip *chip = led->chip;
  225. mutex_lock(&chip->lock);
  226. lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
  227. led->brightness);
  228. mutex_unlock(&chip->lock);
  229. }
  230. /* Chip specific configurations */
  231. static struct lp55xx_device_config lp8501_cfg = {
  232. .reset = {
  233. .addr = LP8501_REG_RESET,
  234. .val = LP8501_RESET,
  235. },
  236. .enable = {
  237. .addr = LP8501_REG_ENABLE,
  238. .val = LP8501_ENABLE,
  239. },
  240. .max_channel = LP8501_MAX_LEDS,
  241. .post_init_device = lp8501_post_init_device,
  242. .brightness_work_fn = lp8501_led_brightness_work,
  243. .set_led_current = lp8501_set_led_current,
  244. .firmware_cb = lp8501_firmware_loaded,
  245. .run_engine = lp8501_run_engine,
  246. };
  247. static int lp8501_probe(struct i2c_client *client,
  248. const struct i2c_device_id *id)
  249. {
  250. int ret;
  251. struct lp55xx_chip *chip;
  252. struct lp55xx_led *led;
  253. struct lp55xx_platform_data *pdata;
  254. struct device_node *np = client->dev.of_node;
  255. if (!dev_get_platdata(&client->dev)) {
  256. if (np) {
  257. ret = lp55xx_of_populate_pdata(&client->dev, np);
  258. if (ret < 0)
  259. return ret;
  260. } else {
  261. dev_err(&client->dev, "no platform data\n");
  262. return -EINVAL;
  263. }
  264. }
  265. pdata = dev_get_platdata(&client->dev);
  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 drvier");
  331. MODULE_AUTHOR("Milo Kim");
  332. MODULE_LICENSE("GPL");