leds-lp5523.c 24 KB

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