leds-lp5523.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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/of.h>
  32. #include <linux/platform_data/leds-lp55xx.h>
  33. #include <linux/slab.h>
  34. #include "leds-lp55xx-common.h"
  35. #define LP5523_PROGRAM_LENGTH 32
  36. #define LP5523_MAX_LEDS 9
  37. /* Registers */
  38. #define LP5523_REG_ENABLE 0x00
  39. #define LP5523_REG_OP_MODE 0x01
  40. #define LP5523_REG_ENABLE_LEDS_MSB 0x04
  41. #define LP5523_REG_ENABLE_LEDS_LSB 0x05
  42. #define LP5523_REG_LED_PWM_BASE 0x16
  43. #define LP5523_REG_LED_CURRENT_BASE 0x26
  44. #define LP5523_REG_CONFIG 0x36
  45. #define LP5523_REG_STATUS 0x3A
  46. #define LP5523_REG_RESET 0x3D
  47. #define LP5523_REG_LED_TEST_CTRL 0x41
  48. #define LP5523_REG_LED_TEST_ADC 0x42
  49. #define LP5523_REG_CH1_PROG_START 0x4C
  50. #define LP5523_REG_CH2_PROG_START 0x4D
  51. #define LP5523_REG_CH3_PROG_START 0x4E
  52. #define LP5523_REG_PROG_PAGE_SEL 0x4F
  53. #define LP5523_REG_PROG_MEM 0x50
  54. /* Bit description in registers */
  55. #define LP5523_ENABLE 0x40
  56. #define LP5523_AUTO_INC 0x40
  57. #define LP5523_PWR_SAVE 0x20
  58. #define LP5523_PWM_PWR_SAVE 0x04
  59. #define LP5523_CP_AUTO 0x18
  60. #define LP5523_AUTO_CLK 0x02
  61. #define LP5523_EN_LEDTEST 0x80
  62. #define LP5523_LEDTEST_DONE 0x80
  63. #define LP5523_RESET 0xFF
  64. #define LP5523_ADC_SHORTCIRC_LIM 80
  65. #define LP5523_EXT_CLK_USED 0x08
  66. #define LP5523_ENG_STATUS_MASK 0x07
  67. /* Memory Page Selection */
  68. #define LP5523_PAGE_ENG1 0
  69. #define LP5523_PAGE_ENG2 1
  70. #define LP5523_PAGE_ENG3 2
  71. #define LP5523_PAGE_MUX1 3
  72. #define LP5523_PAGE_MUX2 4
  73. #define LP5523_PAGE_MUX3 5
  74. /* Program Memory Operations */
  75. #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
  76. #define LP5523_MODE_ENG2_M 0x0C
  77. #define LP5523_MODE_ENG3_M 0x03
  78. #define LP5523_LOAD_ENG1 0x10
  79. #define LP5523_LOAD_ENG2 0x04
  80. #define LP5523_LOAD_ENG3 0x01
  81. #define LP5523_ENG1_IS_LOADING(mode) \
  82. ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
  83. #define LP5523_ENG2_IS_LOADING(mode) \
  84. ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
  85. #define LP5523_ENG3_IS_LOADING(mode) \
  86. ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
  87. #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
  88. #define LP5523_EXEC_ENG2_M 0x0C
  89. #define LP5523_EXEC_ENG3_M 0x03
  90. #define LP5523_EXEC_M 0x3F
  91. #define LP5523_RUN_ENG1 0x20
  92. #define LP5523_RUN_ENG2 0x08
  93. #define LP5523_RUN_ENG3 0x02
  94. #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
  95. enum lp5523_chip_id {
  96. LP5523,
  97. LP55231,
  98. };
  99. static int lp5523_init_program_engine(struct lp55xx_chip *chip);
  100. static inline void lp5523_wait_opmode_done(void)
  101. {
  102. usleep_range(1000, 2000);
  103. }
  104. static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
  105. {
  106. led->led_current = led_current;
  107. lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  108. led_current);
  109. }
  110. static int lp5523_post_init_device(struct lp55xx_chip *chip)
  111. {
  112. int ret;
  113. ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
  114. if (ret)
  115. return ret;
  116. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  117. usleep_range(1000, 2000);
  118. ret = lp55xx_write(chip, LP5523_REG_CONFIG,
  119. LP5523_AUTO_INC | LP5523_PWR_SAVE |
  120. LP5523_CP_AUTO | LP5523_AUTO_CLK |
  121. LP5523_PWM_PWR_SAVE);
  122. if (ret)
  123. return ret;
  124. /* turn on all leds */
  125. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
  126. if (ret)
  127. return ret;
  128. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
  129. if (ret)
  130. return ret;
  131. return lp5523_init_program_engine(chip);
  132. }
  133. static void lp5523_load_engine(struct lp55xx_chip *chip)
  134. {
  135. enum lp55xx_engine_index idx = chip->engine_idx;
  136. u8 mask[] = {
  137. [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
  138. [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
  139. [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
  140. };
  141. u8 val[] = {
  142. [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
  143. [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
  144. [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
  145. };
  146. lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
  147. lp5523_wait_opmode_done();
  148. }
  149. static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
  150. {
  151. enum lp55xx_engine_index idx = chip->engine_idx;
  152. u8 page_sel[] = {
  153. [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
  154. [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
  155. [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
  156. };
  157. lp5523_load_engine(chip);
  158. lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
  159. }
  160. static void lp5523_stop_engine(struct lp55xx_chip *chip)
  161. {
  162. lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
  163. lp5523_wait_opmode_done();
  164. }
  165. static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
  166. {
  167. int i;
  168. for (i = 0; i < LP5523_MAX_LEDS; i++)
  169. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
  170. }
  171. static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
  172. {
  173. int ret;
  174. u8 mode;
  175. u8 exec;
  176. /* stop engine */
  177. if (!start) {
  178. lp5523_stop_engine(chip);
  179. lp5523_turn_off_channels(chip);
  180. return;
  181. }
  182. /*
  183. * To run the engine,
  184. * operation mode and enable register should updated at the same time
  185. */
  186. ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
  187. if (ret)
  188. return;
  189. ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
  190. if (ret)
  191. return;
  192. /* change operation mode to RUN only when each engine is loading */
  193. if (LP5523_ENG1_IS_LOADING(mode)) {
  194. mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
  195. exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
  196. }
  197. if (LP5523_ENG2_IS_LOADING(mode)) {
  198. mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
  199. exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
  200. }
  201. if (LP5523_ENG3_IS_LOADING(mode)) {
  202. mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
  203. exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
  204. }
  205. lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
  206. lp5523_wait_opmode_done();
  207. lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
  208. }
  209. static int lp5523_init_program_engine(struct lp55xx_chip *chip)
  210. {
  211. int i;
  212. int j;
  213. int ret;
  214. u8 status;
  215. /* one pattern per engine setting LED MUX start and stop addresses */
  216. static const u8 pattern[][LP5523_PROGRAM_LENGTH] = {
  217. { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
  218. { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
  219. { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
  220. };
  221. /* hardcode 32 bytes of memory for each engine from program memory */
  222. ret = lp55xx_write(chip, LP5523_REG_CH1_PROG_START, 0x00);
  223. if (ret)
  224. return ret;
  225. ret = lp55xx_write(chip, LP5523_REG_CH2_PROG_START, 0x10);
  226. if (ret)
  227. return ret;
  228. ret = lp55xx_write(chip, LP5523_REG_CH3_PROG_START, 0x20);
  229. if (ret)
  230. return ret;
  231. /* write LED MUX address space for each engine */
  232. for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
  233. chip->engine_idx = i;
  234. lp5523_load_engine_and_select_page(chip);
  235. for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
  236. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
  237. pattern[i - 1][j]);
  238. if (ret)
  239. goto out;
  240. }
  241. }
  242. lp5523_run_engine(chip, true);
  243. /* Let the programs run for couple of ms and check the engine status */
  244. usleep_range(3000, 6000);
  245. lp55xx_read(chip, LP5523_REG_STATUS, &status);
  246. status &= LP5523_ENG_STATUS_MASK;
  247. if (status != LP5523_ENG_STATUS_MASK) {
  248. dev_err(&chip->cl->dev,
  249. "cound not configure LED engine, status = 0x%.2x\n",
  250. status);
  251. ret = -1;
  252. }
  253. out:
  254. lp5523_stop_engine(chip);
  255. return ret;
  256. }
  257. static int lp5523_update_program_memory(struct lp55xx_chip *chip,
  258. const u8 *data, size_t size)
  259. {
  260. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  261. unsigned cmd;
  262. char c[3];
  263. int nrchars;
  264. int ret;
  265. int offset = 0;
  266. int i = 0;
  267. while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  268. /* separate sscanfs because length is working only for %s */
  269. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  270. if (ret != 1)
  271. goto err;
  272. ret = sscanf(c, "%2x", &cmd);
  273. if (ret != 1)
  274. goto err;
  275. pattern[i] = (u8)cmd;
  276. offset += nrchars;
  277. i++;
  278. }
  279. /* Each instruction is 16bit long. Check that length is even */
  280. if (i % 2)
  281. goto err;
  282. mutex_lock(&chip->lock);
  283. for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) {
  284. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
  285. if (ret) {
  286. mutex_unlock(&chip->lock);
  287. return -EINVAL;
  288. }
  289. }
  290. mutex_unlock(&chip->lock);
  291. return size;
  292. err:
  293. dev_err(&chip->cl->dev, "wrong pattern format\n");
  294. return -EINVAL;
  295. }
  296. static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
  297. {
  298. const struct firmware *fw = chip->fw;
  299. if (fw->size > LP5523_PROGRAM_LENGTH) {
  300. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  301. fw->size);
  302. return;
  303. }
  304. /*
  305. * Program momery sequence
  306. * 1) set engine mode to "LOAD"
  307. * 2) write firmware data into program memory
  308. */
  309. lp5523_load_engine_and_select_page(chip);
  310. lp5523_update_program_memory(chip, fw->data, fw->size);
  311. }
  312. static ssize_t show_engine_mode(struct device *dev,
  313. struct device_attribute *attr,
  314. char *buf, int nr)
  315. {
  316. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  317. struct lp55xx_chip *chip = led->chip;
  318. enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
  319. switch (mode) {
  320. case LP55XX_ENGINE_RUN:
  321. return sprintf(buf, "run\n");
  322. case LP55XX_ENGINE_LOAD:
  323. return sprintf(buf, "load\n");
  324. case LP55XX_ENGINE_DISABLED:
  325. default:
  326. return sprintf(buf, "disabled\n");
  327. }
  328. }
  329. show_mode(1)
  330. show_mode(2)
  331. show_mode(3)
  332. static ssize_t store_engine_mode(struct device *dev,
  333. struct device_attribute *attr,
  334. const char *buf, size_t len, int nr)
  335. {
  336. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  337. struct lp55xx_chip *chip = led->chip;
  338. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  339. mutex_lock(&chip->lock);
  340. chip->engine_idx = nr;
  341. if (!strncmp(buf, "run", 3)) {
  342. lp5523_run_engine(chip, true);
  343. engine->mode = LP55XX_ENGINE_RUN;
  344. } else if (!strncmp(buf, "load", 4)) {
  345. lp5523_stop_engine(chip);
  346. lp5523_load_engine(chip);
  347. engine->mode = LP55XX_ENGINE_LOAD;
  348. } else if (!strncmp(buf, "disabled", 8)) {
  349. lp5523_stop_engine(chip);
  350. engine->mode = LP55XX_ENGINE_DISABLED;
  351. }
  352. mutex_unlock(&chip->lock);
  353. return len;
  354. }
  355. store_mode(1)
  356. store_mode(2)
  357. store_mode(3)
  358. static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
  359. {
  360. u16 tmp_mux = 0;
  361. int i;
  362. len = min_t(int, len, LP5523_MAX_LEDS);
  363. for (i = 0; i < len; i++) {
  364. switch (buf[i]) {
  365. case '1':
  366. tmp_mux |= (1 << i);
  367. break;
  368. case '0':
  369. break;
  370. case '\n':
  371. i = len;
  372. break;
  373. default:
  374. return -1;
  375. }
  376. }
  377. *mux = tmp_mux;
  378. return 0;
  379. }
  380. static void lp5523_mux_to_array(u16 led_mux, char *array)
  381. {
  382. int i, pos = 0;
  383. for (i = 0; i < LP5523_MAX_LEDS; i++)
  384. pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
  385. array[pos] = '\0';
  386. }
  387. static ssize_t show_engine_leds(struct device *dev,
  388. struct device_attribute *attr,
  389. char *buf, int nr)
  390. {
  391. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  392. struct lp55xx_chip *chip = led->chip;
  393. char mux[LP5523_MAX_LEDS + 1];
  394. lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
  395. return sprintf(buf, "%s\n", mux);
  396. }
  397. show_leds(1)
  398. show_leds(2)
  399. show_leds(3)
  400. static int lp5523_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
  401. {
  402. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  403. int ret;
  404. u8 mux_page[] = {
  405. [LP55XX_ENGINE_1] = LP5523_PAGE_MUX1,
  406. [LP55XX_ENGINE_2] = LP5523_PAGE_MUX2,
  407. [LP55XX_ENGINE_3] = LP5523_PAGE_MUX3,
  408. };
  409. lp5523_load_engine(chip);
  410. ret = lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, mux_page[nr]);
  411. if (ret)
  412. return ret;
  413. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM , (u8)(mux >> 8));
  414. if (ret)
  415. return ret;
  416. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + 1, (u8)(mux));
  417. if (ret)
  418. return ret;
  419. engine->led_mux = mux;
  420. return 0;
  421. }
  422. static ssize_t store_engine_leds(struct device *dev,
  423. struct device_attribute *attr,
  424. const char *buf, size_t len, int nr)
  425. {
  426. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  427. struct lp55xx_chip *chip = led->chip;
  428. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  429. u16 mux = 0;
  430. ssize_t ret;
  431. if (lp5523_mux_parse(buf, &mux, len))
  432. return -EINVAL;
  433. mutex_lock(&chip->lock);
  434. chip->engine_idx = nr;
  435. ret = -EINVAL;
  436. if (engine->mode != LP55XX_ENGINE_LOAD)
  437. goto leave;
  438. if (lp5523_load_mux(chip, mux, nr))
  439. goto leave;
  440. ret = len;
  441. leave:
  442. mutex_unlock(&chip->lock);
  443. return ret;
  444. }
  445. store_leds(1)
  446. store_leds(2)
  447. store_leds(3)
  448. static ssize_t store_engine_load(struct device *dev,
  449. struct device_attribute *attr,
  450. const char *buf, size_t len, int nr)
  451. {
  452. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  453. struct lp55xx_chip *chip = led->chip;
  454. mutex_lock(&chip->lock);
  455. chip->engine_idx = nr;
  456. lp5523_load_engine_and_select_page(chip);
  457. mutex_unlock(&chip->lock);
  458. return lp5523_update_program_memory(chip, buf, len);
  459. }
  460. store_load(1)
  461. store_load(2)
  462. store_load(3)
  463. static ssize_t lp5523_selftest(struct device *dev,
  464. struct device_attribute *attr,
  465. char *buf)
  466. {
  467. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  468. struct lp55xx_chip *chip = led->chip;
  469. struct lp55xx_platform_data *pdata = chip->pdata;
  470. int i, ret, pos = 0;
  471. u8 status, adc, vdd;
  472. mutex_lock(&chip->lock);
  473. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  474. if (ret < 0)
  475. goto fail;
  476. /* Check that ext clock is really in use if requested */
  477. if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
  478. if ((status & LP5523_EXT_CLK_USED) == 0)
  479. goto fail;
  480. }
  481. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  482. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
  483. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  484. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  485. if (ret < 0)
  486. goto fail;
  487. if (!(status & LP5523_LEDTEST_DONE))
  488. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  489. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
  490. if (ret < 0)
  491. goto fail;
  492. vdd--; /* There may be some fluctuation in measurement */
  493. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  494. /* Skip non-existing channels */
  495. if (pdata->led_config[i].led_current == 0)
  496. continue;
  497. /* Set default current */
  498. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  499. pdata->led_config[i].led_current);
  500. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
  501. /* let current stabilize 2 - 4ms before measurements start */
  502. usleep_range(2000, 4000);
  503. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
  504. LP5523_EN_LEDTEST | i);
  505. /* ADC conversion time is 2.7 ms typically */
  506. usleep_range(3000, 6000);
  507. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  508. if (ret < 0)
  509. goto fail;
  510. if (!(status & LP5523_LEDTEST_DONE))
  511. usleep_range(3000, 6000);/* Was not ready. Wait. */
  512. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
  513. if (ret < 0)
  514. goto fail;
  515. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  516. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  517. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
  518. /* Restore current */
  519. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  520. led->led_current);
  521. led++;
  522. }
  523. if (pos == 0)
  524. pos = sprintf(buf, "OK\n");
  525. goto release_lock;
  526. fail:
  527. pos = sprintf(buf, "FAIL\n");
  528. release_lock:
  529. mutex_unlock(&chip->lock);
  530. return pos;
  531. }
  532. static void lp5523_led_brightness_work(struct work_struct *work)
  533. {
  534. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  535. brightness_work);
  536. struct lp55xx_chip *chip = led->chip;
  537. mutex_lock(&chip->lock);
  538. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  539. led->brightness);
  540. mutex_unlock(&chip->lock);
  541. }
  542. static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
  543. static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
  544. static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
  545. static LP55XX_DEV_ATTR_RW(engine1_leds, show_engine1_leds, store_engine1_leds);
  546. static LP55XX_DEV_ATTR_RW(engine2_leds, show_engine2_leds, store_engine2_leds);
  547. static LP55XX_DEV_ATTR_RW(engine3_leds, show_engine3_leds, store_engine3_leds);
  548. static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
  549. static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
  550. static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
  551. static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
  552. static struct attribute *lp5523_attributes[] = {
  553. &dev_attr_engine1_mode.attr,
  554. &dev_attr_engine2_mode.attr,
  555. &dev_attr_engine3_mode.attr,
  556. &dev_attr_engine1_load.attr,
  557. &dev_attr_engine2_load.attr,
  558. &dev_attr_engine3_load.attr,
  559. &dev_attr_engine1_leds.attr,
  560. &dev_attr_engine2_leds.attr,
  561. &dev_attr_engine3_leds.attr,
  562. &dev_attr_selftest.attr,
  563. NULL,
  564. };
  565. static const struct attribute_group lp5523_group = {
  566. .attrs = lp5523_attributes,
  567. };
  568. /* Chip specific configurations */
  569. static struct lp55xx_device_config lp5523_cfg = {
  570. .reset = {
  571. .addr = LP5523_REG_RESET,
  572. .val = LP5523_RESET,
  573. },
  574. .enable = {
  575. .addr = LP5523_REG_ENABLE,
  576. .val = LP5523_ENABLE,
  577. },
  578. .max_channel = LP5523_MAX_LEDS,
  579. .post_init_device = lp5523_post_init_device,
  580. .brightness_work_fn = lp5523_led_brightness_work,
  581. .set_led_current = lp5523_set_led_current,
  582. .firmware_cb = lp5523_firmware_loaded,
  583. .run_engine = lp5523_run_engine,
  584. .dev_attr_group = &lp5523_group,
  585. };
  586. static int lp5523_probe(struct i2c_client *client,
  587. const struct i2c_device_id *id)
  588. {
  589. int ret;
  590. struct lp55xx_chip *chip;
  591. struct lp55xx_led *led;
  592. struct lp55xx_platform_data *pdata;
  593. struct device_node *np = client->dev.of_node;
  594. if (!dev_get_platdata(&client->dev)) {
  595. if (np) {
  596. ret = lp55xx_of_populate_pdata(&client->dev, np);
  597. if (ret < 0)
  598. return ret;
  599. } else {
  600. dev_err(&client->dev, "no platform data\n");
  601. return -EINVAL;
  602. }
  603. }
  604. pdata = dev_get_platdata(&client->dev);
  605. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  606. if (!chip)
  607. return -ENOMEM;
  608. led = devm_kzalloc(&client->dev,
  609. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  610. if (!led)
  611. return -ENOMEM;
  612. chip->cl = client;
  613. chip->pdata = pdata;
  614. chip->cfg = &lp5523_cfg;
  615. mutex_init(&chip->lock);
  616. i2c_set_clientdata(client, led);
  617. ret = lp55xx_init_device(chip);
  618. if (ret)
  619. goto err_init;
  620. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  621. ret = lp55xx_register_leds(led, chip);
  622. if (ret)
  623. goto err_register_leds;
  624. ret = lp55xx_register_sysfs(chip);
  625. if (ret) {
  626. dev_err(&client->dev, "registering sysfs failed\n");
  627. goto err_register_sysfs;
  628. }
  629. return 0;
  630. err_register_sysfs:
  631. lp55xx_unregister_leds(led, chip);
  632. err_register_leds:
  633. lp55xx_deinit_device(chip);
  634. err_init:
  635. return ret;
  636. }
  637. static int lp5523_remove(struct i2c_client *client)
  638. {
  639. struct lp55xx_led *led = i2c_get_clientdata(client);
  640. struct lp55xx_chip *chip = led->chip;
  641. lp5523_stop_engine(chip);
  642. lp55xx_unregister_sysfs(chip);
  643. lp55xx_unregister_leds(led, chip);
  644. lp55xx_deinit_device(chip);
  645. return 0;
  646. }
  647. static const struct i2c_device_id lp5523_id[] = {
  648. { "lp5523", LP5523 },
  649. { "lp55231", LP55231 },
  650. { }
  651. };
  652. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  653. #ifdef CONFIG_OF
  654. static const struct of_device_id of_lp5523_leds_match[] = {
  655. { .compatible = "national,lp5523", },
  656. {},
  657. };
  658. MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
  659. #endif
  660. static struct i2c_driver lp5523_driver = {
  661. .driver = {
  662. .name = "lp5523x",
  663. .of_match_table = of_match_ptr(of_lp5523_leds_match),
  664. },
  665. .probe = lp5523_probe,
  666. .remove = lp5523_remove,
  667. .id_table = lp5523_id,
  668. };
  669. module_i2c_driver(lp5523_driver);
  670. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  671. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  672. MODULE_DESCRIPTION("LP5523 LED engine");
  673. MODULE_LICENSE("GPL");