leds-lp5523.c 20 KB

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