leds-lp5523.c 20 KB

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