leds-lp5562.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * LP5562 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 modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/firmware.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/leds.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_data/leds-lp55xx.h>
  20. #include <linux/slab.h>
  21. #include "leds-lp55xx-common.h"
  22. #define LP5562_PROGRAM_LENGTH 32
  23. #define LP5562_MAX_LEDS 4
  24. /* ENABLE Register 00h */
  25. #define LP5562_REG_ENABLE 0x00
  26. #define LP5562_EXEC_ENG1_M 0x30
  27. #define LP5562_EXEC_ENG2_M 0x0C
  28. #define LP5562_EXEC_ENG3_M 0x03
  29. #define LP5562_EXEC_M 0x3F
  30. #define LP5562_MASTER_ENABLE 0x40 /* Chip master enable */
  31. #define LP5562_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  32. #define LP5562_EXEC_RUN 0x2A
  33. #define LP5562_ENABLE_DEFAULT \
  34. (LP5562_MASTER_ENABLE | LP5562_LOGARITHMIC_PWM)
  35. #define LP5562_ENABLE_RUN_PROGRAM \
  36. (LP5562_ENABLE_DEFAULT | LP5562_EXEC_RUN)
  37. /* OPMODE Register 01h */
  38. #define LP5562_REG_OP_MODE 0x01
  39. #define LP5562_MODE_ENG1_M 0x30
  40. #define LP5562_MODE_ENG2_M 0x0C
  41. #define LP5562_MODE_ENG3_M 0x03
  42. #define LP5562_LOAD_ENG1 0x10
  43. #define LP5562_LOAD_ENG2 0x04
  44. #define LP5562_LOAD_ENG3 0x01
  45. #define LP5562_RUN_ENG1 0x20
  46. #define LP5562_RUN_ENG2 0x08
  47. #define LP5562_RUN_ENG3 0x02
  48. #define LP5562_ENG1_IS_LOADING(mode) \
  49. ((mode & LP5562_MODE_ENG1_M) == LP5562_LOAD_ENG1)
  50. #define LP5562_ENG2_IS_LOADING(mode) \
  51. ((mode & LP5562_MODE_ENG2_M) == LP5562_LOAD_ENG2)
  52. #define LP5562_ENG3_IS_LOADING(mode) \
  53. ((mode & LP5562_MODE_ENG3_M) == LP5562_LOAD_ENG3)
  54. /* BRIGHTNESS Registers */
  55. #define LP5562_REG_R_PWM 0x04
  56. #define LP5562_REG_G_PWM 0x03
  57. #define LP5562_REG_B_PWM 0x02
  58. #define LP5562_REG_W_PWM 0x0E
  59. /* CURRENT Registers */
  60. #define LP5562_REG_R_CURRENT 0x07
  61. #define LP5562_REG_G_CURRENT 0x06
  62. #define LP5562_REG_B_CURRENT 0x05
  63. #define LP5562_REG_W_CURRENT 0x0F
  64. /* CONFIG Register 08h */
  65. #define LP5562_REG_CONFIG 0x08
  66. #define LP5562_DEFAULT_CFG \
  67. (LP5562_PWM_HF | LP5562_PWRSAVE_EN | LP5562_CLK_INT)
  68. /* RESET Register 0Dh */
  69. #define LP5562_REG_RESET 0x0D
  70. #define LP5562_RESET 0xFF
  71. /* PROGRAM ENGINE Registers */
  72. #define LP5562_REG_PROG_MEM_ENG1 0x10
  73. #define LP5562_REG_PROG_MEM_ENG2 0x30
  74. #define LP5562_REG_PROG_MEM_ENG3 0x50
  75. /* LEDMAP Register 70h */
  76. #define LP5562_REG_ENG_SEL 0x70
  77. #define LP5562_ENG_SEL_PWM 0
  78. #define LP5562_ENG_FOR_RGB_M 0x3F
  79. #define LP5562_ENG_SEL_RGB 0x1B /* R:ENG1, G:ENG2, B:ENG3 */
  80. #define LP5562_ENG_FOR_W_M 0xC0
  81. #define LP5562_ENG1_FOR_W 0x40 /* W:ENG1 */
  82. #define LP5562_ENG2_FOR_W 0x80 /* W:ENG2 */
  83. #define LP5562_ENG3_FOR_W 0xC0 /* W:ENG3 */
  84. /* Program Commands */
  85. #define LP5562_CMD_DISABLE 0x00
  86. #define LP5562_CMD_LOAD 0x15
  87. #define LP5562_CMD_RUN 0x2A
  88. #define LP5562_CMD_DIRECT 0x3F
  89. #define LP5562_PATTERN_OFF 0
  90. static inline void lp5562_wait_opmode_done(void)
  91. {
  92. /* operation mode change needs to be longer than 153 us */
  93. usleep_range(200, 300);
  94. }
  95. static inline void lp5562_wait_enable_done(void)
  96. {
  97. /* it takes more 488 us to update ENABLE register */
  98. usleep_range(500, 600);
  99. }
  100. static void lp5562_set_led_current(struct lp55xx_led *led, u8 led_current)
  101. {
  102. u8 addr[] = {
  103. LP5562_REG_R_CURRENT,
  104. LP5562_REG_G_CURRENT,
  105. LP5562_REG_B_CURRENT,
  106. LP5562_REG_W_CURRENT,
  107. };
  108. led->led_current = led_current;
  109. lp55xx_write(led->chip, addr[led->chan_nr], led_current);
  110. }
  111. static void lp5562_load_engine(struct lp55xx_chip *chip)
  112. {
  113. enum lp55xx_engine_index idx = chip->engine_idx;
  114. u8 mask[] = {
  115. [LP55XX_ENGINE_1] = LP5562_MODE_ENG1_M,
  116. [LP55XX_ENGINE_2] = LP5562_MODE_ENG2_M,
  117. [LP55XX_ENGINE_3] = LP5562_MODE_ENG3_M,
  118. };
  119. u8 val[] = {
  120. [LP55XX_ENGINE_1] = LP5562_LOAD_ENG1,
  121. [LP55XX_ENGINE_2] = LP5562_LOAD_ENG2,
  122. [LP55XX_ENGINE_3] = LP5562_LOAD_ENG3,
  123. };
  124. lp55xx_update_bits(chip, LP5562_REG_OP_MODE, mask[idx], val[idx]);
  125. lp5562_wait_opmode_done();
  126. }
  127. static void lp5562_stop_engine(struct lp55xx_chip *chip)
  128. {
  129. lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DISABLE);
  130. lp5562_wait_opmode_done();
  131. }
  132. static void lp5562_run_engine(struct lp55xx_chip *chip, bool start)
  133. {
  134. int ret;
  135. u8 mode;
  136. u8 exec;
  137. /* stop engine */
  138. if (!start) {
  139. lp55xx_write(chip, LP5562_REG_ENABLE, LP5562_ENABLE_DEFAULT);
  140. lp5562_wait_enable_done();
  141. lp5562_stop_engine(chip);
  142. lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
  143. lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
  144. lp5562_wait_opmode_done();
  145. return;
  146. }
  147. /*
  148. * To run the engine,
  149. * operation mode and enable register should updated at the same time
  150. */
  151. ret = lp55xx_read(chip, LP5562_REG_OP_MODE, &mode);
  152. if (ret)
  153. return;
  154. ret = lp55xx_read(chip, LP5562_REG_ENABLE, &exec);
  155. if (ret)
  156. return;
  157. /* change operation mode to RUN only when each engine is loading */
  158. if (LP5562_ENG1_IS_LOADING(mode)) {
  159. mode = (mode & ~LP5562_MODE_ENG1_M) | LP5562_RUN_ENG1;
  160. exec = (exec & ~LP5562_EXEC_ENG1_M) | LP5562_RUN_ENG1;
  161. }
  162. if (LP5562_ENG2_IS_LOADING(mode)) {
  163. mode = (mode & ~LP5562_MODE_ENG2_M) | LP5562_RUN_ENG2;
  164. exec = (exec & ~LP5562_EXEC_ENG2_M) | LP5562_RUN_ENG2;
  165. }
  166. if (LP5562_ENG3_IS_LOADING(mode)) {
  167. mode = (mode & ~LP5562_MODE_ENG3_M) | LP5562_RUN_ENG3;
  168. exec = (exec & ~LP5562_EXEC_ENG3_M) | LP5562_RUN_ENG3;
  169. }
  170. lp55xx_write(chip, LP5562_REG_OP_MODE, mode);
  171. lp5562_wait_opmode_done();
  172. lp55xx_update_bits(chip, LP5562_REG_ENABLE, LP5562_EXEC_M, exec);
  173. lp5562_wait_enable_done();
  174. }
  175. static int lp5562_update_firmware(struct lp55xx_chip *chip,
  176. const u8 *data, size_t size)
  177. {
  178. enum lp55xx_engine_index idx = chip->engine_idx;
  179. u8 pattern[LP5562_PROGRAM_LENGTH] = {0};
  180. u8 addr[] = {
  181. [LP55XX_ENGINE_1] = LP5562_REG_PROG_MEM_ENG1,
  182. [LP55XX_ENGINE_2] = LP5562_REG_PROG_MEM_ENG2,
  183. [LP55XX_ENGINE_3] = LP5562_REG_PROG_MEM_ENG3,
  184. };
  185. unsigned cmd;
  186. char c[3];
  187. int program_size;
  188. int nrchars;
  189. int offset = 0;
  190. int ret;
  191. int i;
  192. /* clear program memory before updating */
  193. for (i = 0; i < LP5562_PROGRAM_LENGTH; i++)
  194. lp55xx_write(chip, addr[idx] + i, 0);
  195. i = 0;
  196. while ((offset < size - 1) && (i < LP5562_PROGRAM_LENGTH)) {
  197. /* separate sscanfs because length is working only for %s */
  198. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  199. if (ret != 1)
  200. goto err;
  201. ret = sscanf(c, "%2x", &cmd);
  202. if (ret != 1)
  203. goto err;
  204. pattern[i] = (u8)cmd;
  205. offset += nrchars;
  206. i++;
  207. }
  208. /* Each instruction is 16bit long. Check that length is even */
  209. if (i % 2)
  210. goto err;
  211. program_size = i;
  212. for (i = 0; i < program_size; i++)
  213. lp55xx_write(chip, addr[idx] + i, pattern[i]);
  214. return 0;
  215. err:
  216. dev_err(&chip->cl->dev, "wrong pattern format\n");
  217. return -EINVAL;
  218. }
  219. static void lp5562_firmware_loaded(struct lp55xx_chip *chip)
  220. {
  221. const struct firmware *fw = chip->fw;
  222. if (fw->size > LP5562_PROGRAM_LENGTH) {
  223. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  224. fw->size);
  225. return;
  226. }
  227. /*
  228. * Program momery sequence
  229. * 1) set engine mode to "LOAD"
  230. * 2) write firmware data into program memory
  231. */
  232. lp5562_load_engine(chip);
  233. lp5562_update_firmware(chip, fw->data, fw->size);
  234. }
  235. static int lp5562_post_init_device(struct lp55xx_chip *chip)
  236. {
  237. int ret;
  238. u8 update_cfg = chip->pdata->update_config ? : LP5562_DEFAULT_CFG;
  239. /* Set all PWMs to direct control mode */
  240. ret = lp55xx_write(chip, LP5562_REG_OP_MODE, LP5562_CMD_DIRECT);
  241. if (ret)
  242. return ret;
  243. lp5562_wait_opmode_done();
  244. ret = lp55xx_write(chip, LP5562_REG_CONFIG, update_cfg);
  245. if (ret)
  246. return ret;
  247. /* Initialize all channels PWM to zero -> leds off */
  248. lp55xx_write(chip, LP5562_REG_R_PWM, 0);
  249. lp55xx_write(chip, LP5562_REG_G_PWM, 0);
  250. lp55xx_write(chip, LP5562_REG_B_PWM, 0);
  251. lp55xx_write(chip, LP5562_REG_W_PWM, 0);
  252. /* Set LED map as register PWM by default */
  253. lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_PWM);
  254. return 0;
  255. }
  256. static void lp5562_led_brightness_work(struct work_struct *work)
  257. {
  258. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  259. brightness_work);
  260. struct lp55xx_chip *chip = led->chip;
  261. u8 addr[] = {
  262. LP5562_REG_R_PWM,
  263. LP5562_REG_G_PWM,
  264. LP5562_REG_B_PWM,
  265. LP5562_REG_W_PWM,
  266. };
  267. mutex_lock(&chip->lock);
  268. lp55xx_write(chip, addr[led->chan_nr], led->brightness);
  269. mutex_unlock(&chip->lock);
  270. }
  271. static void lp5562_write_program_memory(struct lp55xx_chip *chip,
  272. u8 base, const u8 *rgb, int size)
  273. {
  274. int i;
  275. if (!rgb || size <= 0)
  276. return;
  277. for (i = 0; i < size; i++)
  278. lp55xx_write(chip, base + i, *(rgb + i));
  279. lp55xx_write(chip, base + i, 0);
  280. lp55xx_write(chip, base + i + 1, 0);
  281. }
  282. /* check the size of program count */
  283. static inline bool _is_pc_overflow(struct lp55xx_predef_pattern *ptn)
  284. {
  285. return (ptn->size_r >= LP5562_PROGRAM_LENGTH ||
  286. ptn->size_g >= LP5562_PROGRAM_LENGTH ||
  287. ptn->size_b >= LP5562_PROGRAM_LENGTH);
  288. }
  289. static int lp5562_run_predef_led_pattern(struct lp55xx_chip *chip, int mode)
  290. {
  291. struct lp55xx_predef_pattern *ptn;
  292. int i;
  293. if (mode == LP5562_PATTERN_OFF) {
  294. lp5562_run_engine(chip, false);
  295. return 0;
  296. }
  297. ptn = chip->pdata->patterns + (mode - 1);
  298. if (!ptn || _is_pc_overflow(ptn)) {
  299. dev_err(&chip->cl->dev, "invalid pattern data\n");
  300. return -EINVAL;
  301. }
  302. lp5562_stop_engine(chip);
  303. /* Set LED map as RGB */
  304. lp55xx_write(chip, LP5562_REG_ENG_SEL, LP5562_ENG_SEL_RGB);
  305. /* Load engines */
  306. for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
  307. chip->engine_idx = i;
  308. lp5562_load_engine(chip);
  309. }
  310. /* Clear program registers */
  311. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1, 0);
  312. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG1 + 1, 0);
  313. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2, 0);
  314. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG2 + 1, 0);
  315. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3, 0);
  316. lp55xx_write(chip, LP5562_REG_PROG_MEM_ENG3 + 1, 0);
  317. /* Program engines */
  318. lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG1,
  319. ptn->r, ptn->size_r);
  320. lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG2,
  321. ptn->g, ptn->size_g);
  322. lp5562_write_program_memory(chip, LP5562_REG_PROG_MEM_ENG3,
  323. ptn->b, ptn->size_b);
  324. /* Run engines */
  325. lp5562_run_engine(chip, true);
  326. return 0;
  327. }
  328. static ssize_t lp5562_store_pattern(struct device *dev,
  329. struct device_attribute *attr,
  330. const char *buf, size_t len)
  331. {
  332. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  333. struct lp55xx_chip *chip = led->chip;
  334. struct lp55xx_predef_pattern *ptn = chip->pdata->patterns;
  335. int num_patterns = chip->pdata->num_patterns;
  336. unsigned long mode;
  337. int ret;
  338. ret = kstrtoul(buf, 0, &mode);
  339. if (ret)
  340. return ret;
  341. if (mode > num_patterns || !ptn)
  342. return -EINVAL;
  343. mutex_lock(&chip->lock);
  344. ret = lp5562_run_predef_led_pattern(chip, mode);
  345. mutex_unlock(&chip->lock);
  346. if (ret)
  347. return ret;
  348. return len;
  349. }
  350. static ssize_t lp5562_store_engine_mux(struct device *dev,
  351. struct device_attribute *attr,
  352. const char *buf, size_t len)
  353. {
  354. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  355. struct lp55xx_chip *chip = led->chip;
  356. u8 mask;
  357. u8 val;
  358. /* LED map
  359. * R ... Engine 1 (fixed)
  360. * G ... Engine 2 (fixed)
  361. * B ... Engine 3 (fixed)
  362. * W ... Engine 1 or 2 or 3
  363. */
  364. if (sysfs_streq(buf, "RGB")) {
  365. mask = LP5562_ENG_FOR_RGB_M;
  366. val = LP5562_ENG_SEL_RGB;
  367. } else if (sysfs_streq(buf, "W")) {
  368. enum lp55xx_engine_index idx = chip->engine_idx;
  369. mask = LP5562_ENG_FOR_W_M;
  370. switch (idx) {
  371. case LP55XX_ENGINE_1:
  372. val = LP5562_ENG1_FOR_W;
  373. break;
  374. case LP55XX_ENGINE_2:
  375. val = LP5562_ENG2_FOR_W;
  376. break;
  377. case LP55XX_ENGINE_3:
  378. val = LP5562_ENG3_FOR_W;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. } else {
  384. dev_err(dev, "choose RGB or W\n");
  385. return -EINVAL;
  386. }
  387. mutex_lock(&chip->lock);
  388. lp55xx_update_bits(chip, LP5562_REG_ENG_SEL, mask, val);
  389. mutex_unlock(&chip->lock);
  390. return len;
  391. }
  392. static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, lp5562_store_pattern);
  393. static DEVICE_ATTR(engine_mux, S_IWUSR, NULL, lp5562_store_engine_mux);
  394. static struct attribute *lp5562_attributes[] = {
  395. &dev_attr_led_pattern.attr,
  396. &dev_attr_engine_mux.attr,
  397. NULL,
  398. };
  399. static const struct attribute_group lp5562_group = {
  400. .attrs = lp5562_attributes,
  401. };
  402. /* Chip specific configurations */
  403. static struct lp55xx_device_config lp5562_cfg = {
  404. .max_channel = LP5562_MAX_LEDS,
  405. .reset = {
  406. .addr = LP5562_REG_RESET,
  407. .val = LP5562_RESET,
  408. },
  409. .enable = {
  410. .addr = LP5562_REG_ENABLE,
  411. .val = LP5562_ENABLE_DEFAULT,
  412. },
  413. .post_init_device = lp5562_post_init_device,
  414. .set_led_current = lp5562_set_led_current,
  415. .brightness_work_fn = lp5562_led_brightness_work,
  416. .run_engine = lp5562_run_engine,
  417. .firmware_cb = lp5562_firmware_loaded,
  418. .dev_attr_group = &lp5562_group,
  419. };
  420. static int lp5562_probe(struct i2c_client *client,
  421. const struct i2c_device_id *id)
  422. {
  423. int ret;
  424. struct lp55xx_chip *chip;
  425. struct lp55xx_led *led;
  426. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  427. if (!pdata) {
  428. dev_err(&client->dev, "no platform data\n");
  429. return -EINVAL;
  430. }
  431. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  432. if (!chip)
  433. return -ENOMEM;
  434. led = devm_kzalloc(&client->dev,
  435. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  436. if (!led)
  437. return -ENOMEM;
  438. chip->cl = client;
  439. chip->pdata = pdata;
  440. chip->cfg = &lp5562_cfg;
  441. mutex_init(&chip->lock);
  442. i2c_set_clientdata(client, led);
  443. ret = lp55xx_init_device(chip);
  444. if (ret)
  445. goto err_init;
  446. ret = lp55xx_register_leds(led, chip);
  447. if (ret)
  448. goto err_register_leds;
  449. ret = lp55xx_register_sysfs(chip);
  450. if (ret) {
  451. dev_err(&client->dev, "registering sysfs failed\n");
  452. goto err_register_sysfs;
  453. }
  454. return 0;
  455. err_register_sysfs:
  456. lp55xx_unregister_leds(led, chip);
  457. err_register_leds:
  458. lp55xx_deinit_device(chip);
  459. err_init:
  460. return ret;
  461. }
  462. static int lp5562_remove(struct i2c_client *client)
  463. {
  464. struct lp55xx_led *led = i2c_get_clientdata(client);
  465. struct lp55xx_chip *chip = led->chip;
  466. lp5562_stop_engine(chip);
  467. lp55xx_unregister_sysfs(chip);
  468. lp55xx_unregister_leds(led, chip);
  469. lp55xx_deinit_device(chip);
  470. return 0;
  471. }
  472. static const struct i2c_device_id lp5562_id[] = {
  473. { "lp5562", 0 },
  474. { }
  475. };
  476. MODULE_DEVICE_TABLE(i2c, lp5562_id);
  477. static struct i2c_driver lp5562_driver = {
  478. .driver = {
  479. .name = "lp5562",
  480. },
  481. .probe = lp5562_probe,
  482. .remove = lp5562_remove,
  483. .id_table = lp5562_id,
  484. };
  485. module_i2c_driver(lp5562_driver);
  486. MODULE_DESCRIPTION("Texas Instruments LP5562 LED Driver");
  487. MODULE_AUTHOR("Milo Kim");
  488. MODULE_LICENSE("GPL");