leds-lp5523.c 20 KB

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