leds-lp5523.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * lp5523.c - LP5523 LED Driver
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. * Copyright (C) 2012 Texas Instruments
  6. *
  7. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  8. * Milo(Woogyom) Kim <milo.kim@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/firmware.h>
  26. #include <linux/i2c.h>
  27. #include <linux/init.h>
  28. #include <linux/leds.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/platform_data/leds-lp55xx.h>
  32. #include <linux/slab.h>
  33. #include "leds-lp55xx-common.h"
  34. #define LP5523_PROGRAM_LENGTH 32
  35. #define LP5523_MAX_LEDS 9
  36. /* Registers */
  37. #define LP5523_REG_ENABLE 0x00
  38. #define LP5523_REG_OP_MODE 0x01
  39. #define LP5523_REG_ENABLE_LEDS_MSB 0x04
  40. #define LP5523_REG_ENABLE_LEDS_LSB 0x05
  41. #define LP5523_REG_LED_PWM_BASE 0x16
  42. #define LP5523_REG_LED_CURRENT_BASE 0x26
  43. #define LP5523_REG_CONFIG 0x36
  44. #define LP5523_REG_STATUS 0x3A
  45. #define LP5523_REG_RESET 0x3D
  46. #define LP5523_REG_LED_TEST_CTRL 0x41
  47. #define LP5523_REG_LED_TEST_ADC 0x42
  48. #define LP5523_REG_PROG_PAGE_SEL 0x4F
  49. #define LP5523_REG_PROG_MEM 0x50
  50. /* Bit description in registers */
  51. #define LP5523_ENABLE 0x40
  52. #define LP5523_AUTO_INC 0x40
  53. #define LP5523_PWR_SAVE 0x20
  54. #define LP5523_PWM_PWR_SAVE 0x04
  55. #define LP5523_CP_AUTO 0x18
  56. #define LP5523_AUTO_CLK 0x02
  57. #define LP5523_EN_LEDTEST 0x80
  58. #define LP5523_LEDTEST_DONE 0x80
  59. #define LP5523_RESET 0xFF
  60. #define LP5523_ADC_SHORTCIRC_LIM 80
  61. #define LP5523_EXT_CLK_USED 0x08
  62. /* Memory Page Selection */
  63. #define LP5523_PAGE_ENG1 0
  64. #define LP5523_PAGE_ENG2 1
  65. #define LP5523_PAGE_ENG3 2
  66. /* Program Memory Operations */
  67. #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
  68. #define LP5523_MODE_ENG2_M 0x0C
  69. #define LP5523_MODE_ENG3_M 0x03
  70. #define LP5523_LOAD_ENG1 0x10
  71. #define LP5523_LOAD_ENG2 0x04
  72. #define LP5523_LOAD_ENG3 0x01
  73. #define LP5523_ENG1_IS_LOADING(mode) \
  74. ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
  75. #define LP5523_ENG2_IS_LOADING(mode) \
  76. ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
  77. #define LP5523_ENG3_IS_LOADING(mode) \
  78. ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
  79. #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
  80. #define LP5523_EXEC_ENG2_M 0x0C
  81. #define LP5523_EXEC_ENG3_M 0x03
  82. #define LP5523_EXEC_M 0x3F
  83. #define LP5523_RUN_ENG1 0x20
  84. #define LP5523_RUN_ENG2 0x08
  85. #define LP5523_RUN_ENG3 0x02
  86. enum lp5523_chip_id {
  87. LP5523,
  88. LP55231,
  89. };
  90. static inline void lp5523_wait_opmode_done(void)
  91. {
  92. usleep_range(1000, 2000);
  93. }
  94. static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
  95. {
  96. led->led_current = led_current;
  97. lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  98. led_current);
  99. }
  100. static int lp5523_post_init_device(struct lp55xx_chip *chip)
  101. {
  102. int ret;
  103. ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
  104. if (ret)
  105. return ret;
  106. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  107. usleep_range(1000, 2000);
  108. ret = lp55xx_write(chip, LP5523_REG_CONFIG,
  109. LP5523_AUTO_INC | LP5523_PWR_SAVE |
  110. LP5523_CP_AUTO | LP5523_AUTO_CLK |
  111. LP5523_PWM_PWR_SAVE);
  112. if (ret)
  113. return ret;
  114. /* turn on all leds */
  115. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
  116. if (ret)
  117. return ret;
  118. return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
  119. }
  120. static void lp5523_load_engine(struct lp55xx_chip *chip)
  121. {
  122. enum lp55xx_engine_index idx = chip->engine_idx;
  123. u8 mask[] = {
  124. [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
  125. [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
  126. [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
  127. };
  128. u8 val[] = {
  129. [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
  130. [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
  131. [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
  132. };
  133. lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
  134. lp5523_wait_opmode_done();
  135. }
  136. static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
  137. {
  138. enum lp55xx_engine_index idx = chip->engine_idx;
  139. u8 page_sel[] = {
  140. [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
  141. [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
  142. [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
  143. };
  144. lp5523_load_engine(chip);
  145. lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
  146. }
  147. static void lp5523_stop_engine(struct lp55xx_chip *chip)
  148. {
  149. lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
  150. lp5523_wait_opmode_done();
  151. }
  152. static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
  153. {
  154. int i;
  155. for (i = 0; i < LP5523_MAX_LEDS; i++)
  156. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
  157. }
  158. static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
  159. {
  160. int ret;
  161. u8 mode;
  162. u8 exec;
  163. /* stop engine */
  164. if (!start) {
  165. lp5523_stop_engine(chip);
  166. lp5523_turn_off_channels(chip);
  167. return;
  168. }
  169. /*
  170. * To run the engine,
  171. * operation mode and enable register should updated at the same time
  172. */
  173. ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
  174. if (ret)
  175. return;
  176. ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
  177. if (ret)
  178. return;
  179. /* change operation mode to RUN only when each engine is loading */
  180. if (LP5523_ENG1_IS_LOADING(mode)) {
  181. mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
  182. exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
  183. }
  184. if (LP5523_ENG2_IS_LOADING(mode)) {
  185. mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
  186. exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
  187. }
  188. if (LP5523_ENG3_IS_LOADING(mode)) {
  189. mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
  190. exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
  191. }
  192. lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
  193. lp5523_wait_opmode_done();
  194. lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
  195. }
  196. static int lp5523_update_program_memory(struct lp55xx_chip *chip,
  197. const u8 *data, size_t size)
  198. {
  199. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  200. unsigned cmd;
  201. char c[3];
  202. int update_size;
  203. int nrchars;
  204. int offset = 0;
  205. int ret;
  206. int i;
  207. /* clear program memory before updating */
  208. for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
  209. lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
  210. i = 0;
  211. while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  212. /* separate sscanfs because length is working only for %s */
  213. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  214. if (ret != 1)
  215. goto err;
  216. ret = sscanf(c, "%2x", &cmd);
  217. if (ret != 1)
  218. goto err;
  219. pattern[i] = (u8)cmd;
  220. offset += nrchars;
  221. i++;
  222. }
  223. /* Each instruction is 16bit long. Check that length is even */
  224. if (i % 2)
  225. goto err;
  226. update_size = i;
  227. for (i = 0; i < update_size; i++)
  228. lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
  229. return 0;
  230. err:
  231. dev_err(&chip->cl->dev, "wrong pattern format\n");
  232. return -EINVAL;
  233. }
  234. static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
  235. {
  236. const struct firmware *fw = chip->fw;
  237. if (fw->size > LP5523_PROGRAM_LENGTH) {
  238. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  239. fw->size);
  240. return;
  241. }
  242. /*
  243. * Program momery sequence
  244. * 1) set engine mode to "LOAD"
  245. * 2) write firmware data into program memory
  246. */
  247. lp5523_load_engine_and_select_page(chip);
  248. lp5523_update_program_memory(chip, fw->data, fw->size);
  249. }
  250. static ssize_t lp5523_selftest(struct device *dev,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  255. struct lp55xx_chip *chip = led->chip;
  256. struct lp55xx_platform_data *pdata = chip->pdata;
  257. int i, ret, pos = 0;
  258. u8 status, adc, vdd;
  259. mutex_lock(&chip->lock);
  260. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  261. if (ret < 0)
  262. goto fail;
  263. /* Check that ext clock is really in use if requested */
  264. if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
  265. if ((status & LP5523_EXT_CLK_USED) == 0)
  266. goto fail;
  267. }
  268. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  269. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
  270. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  271. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  272. if (ret < 0)
  273. goto fail;
  274. if (!(status & LP5523_LEDTEST_DONE))
  275. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  276. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
  277. if (ret < 0)
  278. goto fail;
  279. vdd--; /* There may be some fluctuation in measurement */
  280. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  281. /* Skip non-existing channels */
  282. if (pdata->led_config[i].led_current == 0)
  283. continue;
  284. /* Set default current */
  285. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  286. pdata->led_config[i].led_current);
  287. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
  288. /* let current stabilize 2 - 4ms before measurements start */
  289. usleep_range(2000, 4000);
  290. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
  291. LP5523_EN_LEDTEST | i);
  292. /* ADC conversion time is 2.7 ms typically */
  293. usleep_range(3000, 6000);
  294. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  295. if (ret < 0)
  296. goto fail;
  297. if (!(status & LP5523_LEDTEST_DONE))
  298. usleep_range(3000, 6000);/* Was not ready. Wait. */
  299. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
  300. if (ret < 0)
  301. goto fail;
  302. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  303. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  304. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
  305. /* Restore current */
  306. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  307. led->led_current);
  308. led++;
  309. }
  310. if (pos == 0)
  311. pos = sprintf(buf, "OK\n");
  312. goto release_lock;
  313. fail:
  314. pos = sprintf(buf, "FAIL\n");
  315. release_lock:
  316. mutex_unlock(&chip->lock);
  317. return pos;
  318. }
  319. static void lp5523_led_brightness_work(struct work_struct *work)
  320. {
  321. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  322. brightness_work);
  323. struct lp55xx_chip *chip = led->chip;
  324. mutex_lock(&chip->lock);
  325. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  326. led->brightness);
  327. mutex_unlock(&chip->lock);
  328. }
  329. static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
  330. static struct attribute *lp5523_attributes[] = {
  331. &dev_attr_selftest.attr,
  332. NULL,
  333. };
  334. static const struct attribute_group lp5523_group = {
  335. .attrs = lp5523_attributes,
  336. };
  337. /* Chip specific configurations */
  338. static struct lp55xx_device_config lp5523_cfg = {
  339. .reset = {
  340. .addr = LP5523_REG_RESET,
  341. .val = LP5523_RESET,
  342. },
  343. .enable = {
  344. .addr = LP5523_REG_ENABLE,
  345. .val = LP5523_ENABLE,
  346. },
  347. .max_channel = LP5523_MAX_LEDS,
  348. .post_init_device = lp5523_post_init_device,
  349. .brightness_work_fn = lp5523_led_brightness_work,
  350. .set_led_current = lp5523_set_led_current,
  351. .firmware_cb = lp5523_firmware_loaded,
  352. .run_engine = lp5523_run_engine,
  353. .dev_attr_group = &lp5523_group,
  354. };
  355. static int lp5523_probe(struct i2c_client *client,
  356. const struct i2c_device_id *id)
  357. {
  358. int ret;
  359. struct lp55xx_chip *chip;
  360. struct lp55xx_led *led;
  361. struct lp55xx_platform_data *pdata;
  362. struct device_node *np = client->dev.of_node;
  363. if (!dev_get_platdata(&client->dev)) {
  364. if (np) {
  365. ret = lp55xx_of_populate_pdata(&client->dev, np);
  366. if (ret < 0)
  367. return ret;
  368. } else {
  369. dev_err(&client->dev, "no platform data\n");
  370. return -EINVAL;
  371. }
  372. }
  373. pdata = dev_get_platdata(&client->dev);
  374. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  375. if (!chip)
  376. return -ENOMEM;
  377. led = devm_kzalloc(&client->dev,
  378. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  379. if (!led)
  380. return -ENOMEM;
  381. chip->cl = client;
  382. chip->pdata = pdata;
  383. chip->cfg = &lp5523_cfg;
  384. mutex_init(&chip->lock);
  385. i2c_set_clientdata(client, led);
  386. ret = lp55xx_init_device(chip);
  387. if (ret)
  388. goto err_init;
  389. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  390. ret = lp55xx_register_leds(led, chip);
  391. if (ret)
  392. goto err_register_leds;
  393. ret = lp55xx_register_sysfs(chip);
  394. if (ret) {
  395. dev_err(&client->dev, "registering sysfs failed\n");
  396. goto err_register_sysfs;
  397. }
  398. return 0;
  399. err_register_sysfs:
  400. lp55xx_unregister_leds(led, chip);
  401. err_register_leds:
  402. lp55xx_deinit_device(chip);
  403. err_init:
  404. return ret;
  405. }
  406. static int lp5523_remove(struct i2c_client *client)
  407. {
  408. struct lp55xx_led *led = i2c_get_clientdata(client);
  409. struct lp55xx_chip *chip = led->chip;
  410. lp5523_stop_engine(chip);
  411. lp55xx_unregister_sysfs(chip);
  412. lp55xx_unregister_leds(led, chip);
  413. lp55xx_deinit_device(chip);
  414. return 0;
  415. }
  416. static const struct i2c_device_id lp5523_id[] = {
  417. { "lp5523", LP5523 },
  418. { "lp55231", LP55231 },
  419. { }
  420. };
  421. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  422. #ifdef CONFIG_OF
  423. static const struct of_device_id of_lp5523_leds_match[] = {
  424. { .compatible = "national,lp5523", },
  425. {},
  426. };
  427. MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
  428. #endif
  429. static struct i2c_driver lp5523_driver = {
  430. .driver = {
  431. .name = "lp5523x",
  432. .of_match_table = of_match_ptr(of_lp5523_leds_match),
  433. },
  434. .probe = lp5523_probe,
  435. .remove = lp5523_remove,
  436. .id_table = lp5523_id,
  437. };
  438. module_i2c_driver(lp5523_driver);
  439. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  440. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  441. MODULE_DESCRIPTION("LP5523 LED engine");
  442. MODULE_LICENSE("GPL");