leds-lp5523.c 13 KB

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