leds-lp5523.c 24 KB

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