leds-lp5523.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. u8 page_sel[] = {
  134. [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
  135. [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
  136. [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
  137. };
  138. lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
  139. lp5523_wait_opmode_done();
  140. lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
  141. }
  142. static void lp5523_stop_engine(struct lp55xx_chip *chip)
  143. {
  144. lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
  145. lp5523_wait_opmode_done();
  146. }
  147. static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
  148. {
  149. int i;
  150. for (i = 0; i < LP5523_MAX_LEDS; i++)
  151. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
  152. }
  153. static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
  154. {
  155. int ret;
  156. u8 mode;
  157. u8 exec;
  158. /* stop engine */
  159. if (!start) {
  160. lp5523_stop_engine(chip);
  161. lp5523_turn_off_channels(chip);
  162. return;
  163. }
  164. /*
  165. * To run the engine,
  166. * operation mode and enable register should updated at the same time
  167. */
  168. ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
  169. if (ret)
  170. return;
  171. ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
  172. if (ret)
  173. return;
  174. /* change operation mode to RUN only when each engine is loading */
  175. if (LP5523_ENG1_IS_LOADING(mode)) {
  176. mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
  177. exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
  178. }
  179. if (LP5523_ENG2_IS_LOADING(mode)) {
  180. mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
  181. exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
  182. }
  183. if (LP5523_ENG3_IS_LOADING(mode)) {
  184. mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
  185. exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
  186. }
  187. lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
  188. lp5523_wait_opmode_done();
  189. lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
  190. }
  191. static int lp5523_update_program_memory(struct lp55xx_chip *chip,
  192. const u8 *data, size_t size)
  193. {
  194. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  195. unsigned cmd;
  196. char c[3];
  197. int update_size;
  198. int nrchars;
  199. int offset = 0;
  200. int ret;
  201. int i;
  202. /* clear program memory before updating */
  203. for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
  204. lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
  205. i = 0;
  206. while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  207. /* separate sscanfs because length is working only for %s */
  208. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  209. if (ret != 1)
  210. goto err;
  211. ret = sscanf(c, "%2x", &cmd);
  212. if (ret != 1)
  213. goto err;
  214. pattern[i] = (u8)cmd;
  215. offset += nrchars;
  216. i++;
  217. }
  218. /* Each instruction is 16bit long. Check that length is even */
  219. if (i % 2)
  220. goto err;
  221. update_size = i;
  222. for (i = 0; i < update_size; i++)
  223. lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
  224. return 0;
  225. err:
  226. dev_err(&chip->cl->dev, "wrong pattern format\n");
  227. return -EINVAL;
  228. }
  229. static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
  230. {
  231. const struct firmware *fw = chip->fw;
  232. if (fw->size > LP5523_PROGRAM_LENGTH) {
  233. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  234. fw->size);
  235. return;
  236. }
  237. /*
  238. * Program momery sequence
  239. * 1) set engine mode to "LOAD"
  240. * 2) write firmware data into program memory
  241. */
  242. lp5523_load_engine(chip);
  243. lp5523_update_program_memory(chip, fw->data, fw->size);
  244. }
  245. static ssize_t lp5523_selftest(struct device *dev,
  246. struct device_attribute *attr,
  247. char *buf)
  248. {
  249. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  250. struct lp55xx_chip *chip = led->chip;
  251. struct lp55xx_platform_data *pdata = chip->pdata;
  252. int i, ret, pos = 0;
  253. u8 status, adc, vdd;
  254. mutex_lock(&chip->lock);
  255. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  256. if (ret < 0)
  257. goto fail;
  258. /* Check that ext clock is really in use if requested */
  259. if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
  260. if ((status & LP5523_EXT_CLK_USED) == 0)
  261. goto fail;
  262. }
  263. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  264. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
  265. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  266. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  267. if (ret < 0)
  268. goto fail;
  269. if (!(status & LP5523_LEDTEST_DONE))
  270. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  271. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
  272. if (ret < 0)
  273. goto fail;
  274. vdd--; /* There may be some fluctuation in measurement */
  275. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  276. /* Skip non-existing channels */
  277. if (pdata->led_config[i].led_current == 0)
  278. continue;
  279. /* Set default current */
  280. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  281. pdata->led_config[i].led_current);
  282. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
  283. /* let current stabilize 2 - 4ms before measurements start */
  284. usleep_range(2000, 4000);
  285. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
  286. LP5523_EN_LEDTEST | i);
  287. /* ADC conversion time is 2.7 ms typically */
  288. usleep_range(3000, 6000);
  289. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  290. if (ret < 0)
  291. goto fail;
  292. if (!(status & LP5523_LEDTEST_DONE))
  293. usleep_range(3000, 6000);/* Was not ready. Wait. */
  294. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
  295. if (ret < 0)
  296. goto fail;
  297. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  298. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  299. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
  300. /* Restore current */
  301. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  302. led->led_current);
  303. led++;
  304. }
  305. if (pos == 0)
  306. pos = sprintf(buf, "OK\n");
  307. goto release_lock;
  308. fail:
  309. pos = sprintf(buf, "FAIL\n");
  310. release_lock:
  311. mutex_unlock(&chip->lock);
  312. return pos;
  313. }
  314. static void lp5523_led_brightness_work(struct work_struct *work)
  315. {
  316. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  317. brightness_work);
  318. struct lp55xx_chip *chip = led->chip;
  319. mutex_lock(&chip->lock);
  320. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  321. led->brightness);
  322. mutex_unlock(&chip->lock);
  323. }
  324. static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
  325. static struct attribute *lp5523_attributes[] = {
  326. &dev_attr_selftest.attr,
  327. NULL,
  328. };
  329. static const struct attribute_group lp5523_group = {
  330. .attrs = lp5523_attributes,
  331. };
  332. /* Chip specific configurations */
  333. static struct lp55xx_device_config lp5523_cfg = {
  334. .reset = {
  335. .addr = LP5523_REG_RESET,
  336. .val = LP5523_RESET,
  337. },
  338. .enable = {
  339. .addr = LP5523_REG_ENABLE,
  340. .val = LP5523_ENABLE,
  341. },
  342. .max_channel = LP5523_MAX_LEDS,
  343. .post_init_device = lp5523_post_init_device,
  344. .brightness_work_fn = lp5523_led_brightness_work,
  345. .set_led_current = lp5523_set_led_current,
  346. .firmware_cb = lp5523_firmware_loaded,
  347. .run_engine = lp5523_run_engine,
  348. .dev_attr_group = &lp5523_group,
  349. };
  350. static int lp5523_probe(struct i2c_client *client,
  351. const struct i2c_device_id *id)
  352. {
  353. int ret;
  354. struct lp55xx_chip *chip;
  355. struct lp55xx_led *led;
  356. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  357. if (!pdata) {
  358. dev_err(&client->dev, "no platform data\n");
  359. return -EINVAL;
  360. }
  361. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  362. if (!chip)
  363. return -ENOMEM;
  364. led = devm_kzalloc(&client->dev,
  365. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  366. if (!led)
  367. return -ENOMEM;
  368. chip->cl = client;
  369. chip->pdata = pdata;
  370. chip->cfg = &lp5523_cfg;
  371. mutex_init(&chip->lock);
  372. i2c_set_clientdata(client, led);
  373. ret = lp55xx_init_device(chip);
  374. if (ret)
  375. goto err_init;
  376. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  377. ret = lp55xx_register_leds(led, chip);
  378. if (ret)
  379. goto err_register_leds;
  380. ret = lp55xx_register_sysfs(chip);
  381. if (ret) {
  382. dev_err(&client->dev, "registering sysfs failed\n");
  383. goto err_register_sysfs;
  384. }
  385. return 0;
  386. err_register_sysfs:
  387. lp55xx_unregister_leds(led, chip);
  388. err_register_leds:
  389. lp55xx_deinit_device(chip);
  390. err_init:
  391. return ret;
  392. }
  393. static int lp5523_remove(struct i2c_client *client)
  394. {
  395. struct lp55xx_led *led = i2c_get_clientdata(client);
  396. struct lp55xx_chip *chip = led->chip;
  397. lp5523_stop_engine(chip);
  398. lp55xx_unregister_sysfs(chip);
  399. lp55xx_unregister_leds(led, chip);
  400. lp55xx_deinit_device(chip);
  401. return 0;
  402. }
  403. static const struct i2c_device_id lp5523_id[] = {
  404. { "lp5523", LP5523 },
  405. { "lp55231", LP55231 },
  406. { }
  407. };
  408. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  409. static struct i2c_driver lp5523_driver = {
  410. .driver = {
  411. .name = "lp5523x",
  412. },
  413. .probe = lp5523_probe,
  414. .remove = lp5523_remove,
  415. .id_table = lp5523_id,
  416. };
  417. module_i2c_driver(lp5523_driver);
  418. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  419. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  420. MODULE_DESCRIPTION("LP5523 LED engine");
  421. MODULE_LICENSE("GPL");