leds-lp5523.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. lp55xx_read(chip, LP5523_REG_STATUS, &status);
  268. status &= LP5523_ENG_STATUS_MASK;
  269. if (status != LP5523_ENG_STATUS_MASK) {
  270. dev_err(&chip->cl->dev,
  271. "cound not configure LED engine, status = 0x%.2x\n",
  272. status);
  273. ret = -1;
  274. }
  275. out:
  276. lp5523_stop_all_engines(chip);
  277. return ret;
  278. }
  279. static int lp5523_update_program_memory(struct lp55xx_chip *chip,
  280. const u8 *data, size_t size)
  281. {
  282. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  283. unsigned cmd;
  284. char c[3];
  285. int nrchars;
  286. int ret;
  287. int offset = 0;
  288. int i = 0;
  289. while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  290. /* separate sscanfs because length is working only for %s */
  291. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  292. if (ret != 1)
  293. goto err;
  294. ret = sscanf(c, "%2x", &cmd);
  295. if (ret != 1)
  296. goto err;
  297. pattern[i] = (u8)cmd;
  298. offset += nrchars;
  299. i++;
  300. }
  301. /* Each instruction is 16bit long. Check that length is even */
  302. if (i % 2)
  303. goto err;
  304. for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) {
  305. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
  306. if (ret)
  307. return -EINVAL;
  308. }
  309. return size;
  310. err:
  311. dev_err(&chip->cl->dev, "wrong pattern format\n");
  312. return -EINVAL;
  313. }
  314. static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
  315. {
  316. const struct firmware *fw = chip->fw;
  317. if (fw->size > LP5523_PROGRAM_LENGTH) {
  318. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  319. fw->size);
  320. return;
  321. }
  322. /*
  323. * Program momery sequence
  324. * 1) set engine mode to "LOAD"
  325. * 2) write firmware data into program memory
  326. */
  327. lp5523_load_engine_and_select_page(chip);
  328. lp5523_update_program_memory(chip, fw->data, fw->size);
  329. }
  330. static ssize_t show_engine_mode(struct device *dev,
  331. struct device_attribute *attr,
  332. char *buf, int nr)
  333. {
  334. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  335. struct lp55xx_chip *chip = led->chip;
  336. enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
  337. switch (mode) {
  338. case LP55XX_ENGINE_RUN:
  339. return sprintf(buf, "run\n");
  340. case LP55XX_ENGINE_LOAD:
  341. return sprintf(buf, "load\n");
  342. case LP55XX_ENGINE_DISABLED:
  343. default:
  344. return sprintf(buf, "disabled\n");
  345. }
  346. }
  347. show_mode(1)
  348. show_mode(2)
  349. show_mode(3)
  350. static ssize_t store_engine_mode(struct device *dev,
  351. struct device_attribute *attr,
  352. const char *buf, size_t len, int nr)
  353. {
  354. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  355. struct lp55xx_chip *chip = led->chip;
  356. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  357. mutex_lock(&chip->lock);
  358. chip->engine_idx = nr;
  359. if (!strncmp(buf, "run", 3)) {
  360. lp5523_run_engine(chip, true);
  361. engine->mode = LP55XX_ENGINE_RUN;
  362. } else if (!strncmp(buf, "load", 4)) {
  363. lp5523_stop_engine(chip);
  364. lp5523_load_engine(chip);
  365. engine->mode = LP55XX_ENGINE_LOAD;
  366. } else if (!strncmp(buf, "disabled", 8)) {
  367. lp5523_stop_engine(chip);
  368. engine->mode = LP55XX_ENGINE_DISABLED;
  369. }
  370. mutex_unlock(&chip->lock);
  371. return len;
  372. }
  373. store_mode(1)
  374. store_mode(2)
  375. store_mode(3)
  376. static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
  377. {
  378. u16 tmp_mux = 0;
  379. int i;
  380. len = min_t(int, len, LP5523_MAX_LEDS);
  381. for (i = 0; i < len; i++) {
  382. switch (buf[i]) {
  383. case '1':
  384. tmp_mux |= (1 << i);
  385. break;
  386. case '0':
  387. break;
  388. case '\n':
  389. i = len;
  390. break;
  391. default:
  392. return -1;
  393. }
  394. }
  395. *mux = tmp_mux;
  396. return 0;
  397. }
  398. static void lp5523_mux_to_array(u16 led_mux, char *array)
  399. {
  400. int i, pos = 0;
  401. for (i = 0; i < LP5523_MAX_LEDS; i++)
  402. pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
  403. array[pos] = '\0';
  404. }
  405. static ssize_t show_engine_leds(struct device *dev,
  406. struct device_attribute *attr,
  407. char *buf, int nr)
  408. {
  409. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  410. struct lp55xx_chip *chip = led->chip;
  411. char mux[LP5523_MAX_LEDS + 1];
  412. lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
  413. return sprintf(buf, "%s\n", mux);
  414. }
  415. show_leds(1)
  416. show_leds(2)
  417. show_leds(3)
  418. static int lp5523_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
  419. {
  420. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  421. int ret;
  422. u8 mux_page[] = {
  423. [LP55XX_ENGINE_1] = LP5523_PAGE_MUX1,
  424. [LP55XX_ENGINE_2] = LP5523_PAGE_MUX2,
  425. [LP55XX_ENGINE_3] = LP5523_PAGE_MUX3,
  426. };
  427. lp5523_load_engine(chip);
  428. ret = lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, mux_page[nr]);
  429. if (ret)
  430. return ret;
  431. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM , (u8)(mux >> 8));
  432. if (ret)
  433. return ret;
  434. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + 1, (u8)(mux));
  435. if (ret)
  436. return ret;
  437. engine->led_mux = mux;
  438. return 0;
  439. }
  440. static ssize_t store_engine_leds(struct device *dev,
  441. struct device_attribute *attr,
  442. const char *buf, size_t len, int nr)
  443. {
  444. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  445. struct lp55xx_chip *chip = led->chip;
  446. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  447. u16 mux = 0;
  448. ssize_t ret;
  449. if (lp5523_mux_parse(buf, &mux, len))
  450. return -EINVAL;
  451. mutex_lock(&chip->lock);
  452. chip->engine_idx = nr;
  453. ret = -EINVAL;
  454. if (engine->mode != LP55XX_ENGINE_LOAD)
  455. goto leave;
  456. if (lp5523_load_mux(chip, mux, nr))
  457. goto leave;
  458. ret = len;
  459. leave:
  460. mutex_unlock(&chip->lock);
  461. return ret;
  462. }
  463. store_leds(1)
  464. store_leds(2)
  465. store_leds(3)
  466. static ssize_t store_engine_load(struct device *dev,
  467. struct device_attribute *attr,
  468. const char *buf, size_t len, int nr)
  469. {
  470. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  471. struct lp55xx_chip *chip = led->chip;
  472. int ret;
  473. mutex_lock(&chip->lock);
  474. chip->engine_idx = nr;
  475. lp5523_load_engine_and_select_page(chip);
  476. ret = lp5523_update_program_memory(chip, buf, len);
  477. mutex_unlock(&chip->lock);
  478. return ret;
  479. }
  480. store_load(1)
  481. store_load(2)
  482. store_load(3)
  483. static ssize_t lp5523_selftest(struct device *dev,
  484. struct device_attribute *attr,
  485. char *buf)
  486. {
  487. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  488. struct lp55xx_chip *chip = led->chip;
  489. struct lp55xx_platform_data *pdata = chip->pdata;
  490. int i, ret, pos = 0;
  491. u8 status, adc, vdd;
  492. mutex_lock(&chip->lock);
  493. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  494. if (ret < 0)
  495. goto fail;
  496. /* Check that ext clock is really in use if requested */
  497. if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
  498. if ((status & LP5523_EXT_CLK_USED) == 0)
  499. goto fail;
  500. }
  501. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  502. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
  503. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  504. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  505. if (ret < 0)
  506. goto fail;
  507. if (!(status & LP5523_LEDTEST_DONE))
  508. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  509. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
  510. if (ret < 0)
  511. goto fail;
  512. vdd--; /* There may be some fluctuation in measurement */
  513. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  514. /* Skip non-existing channels */
  515. if (pdata->led_config[i].led_current == 0)
  516. continue;
  517. /* Set default current */
  518. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  519. pdata->led_config[i].led_current);
  520. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
  521. /* let current stabilize 2 - 4ms before measurements start */
  522. usleep_range(2000, 4000);
  523. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
  524. LP5523_EN_LEDTEST | i);
  525. /* ADC conversion time is 2.7 ms typically */
  526. usleep_range(3000, 6000);
  527. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  528. if (ret < 0)
  529. goto fail;
  530. if (!(status & LP5523_LEDTEST_DONE))
  531. usleep_range(3000, 6000);/* Was not ready. Wait. */
  532. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
  533. if (ret < 0)
  534. goto fail;
  535. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  536. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  537. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
  538. /* Restore current */
  539. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
  540. led->led_current);
  541. led++;
  542. }
  543. if (pos == 0)
  544. pos = sprintf(buf, "OK\n");
  545. goto release_lock;
  546. fail:
  547. pos = sprintf(buf, "FAIL\n");
  548. release_lock:
  549. mutex_unlock(&chip->lock);
  550. return pos;
  551. }
  552. #define show_fader(nr) \
  553. static ssize_t show_master_fader##nr(struct device *dev, \
  554. struct device_attribute *attr, \
  555. char *buf) \
  556. { \
  557. return show_master_fader(dev, attr, buf, nr); \
  558. }
  559. #define store_fader(nr) \
  560. static ssize_t store_master_fader##nr(struct device *dev, \
  561. struct device_attribute *attr, \
  562. const char *buf, size_t len) \
  563. { \
  564. return store_master_fader(dev, attr, buf, len, nr); \
  565. }
  566. static ssize_t show_master_fader(struct device *dev,
  567. struct device_attribute *attr,
  568. char *buf, int nr)
  569. {
  570. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  571. struct lp55xx_chip *chip = led->chip;
  572. int ret;
  573. u8 val;
  574. mutex_lock(&chip->lock);
  575. ret = lp55xx_read(chip, LP5523_REG_MASTER_FADER_BASE + nr - 1, &val);
  576. mutex_unlock(&chip->lock);
  577. if (ret == 0)
  578. ret = sprintf(buf, "%u\n", val);
  579. return ret;
  580. }
  581. show_fader(1)
  582. show_fader(2)
  583. show_fader(3)
  584. static ssize_t store_master_fader(struct device *dev,
  585. struct device_attribute *attr,
  586. const char *buf, size_t len, int nr)
  587. {
  588. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  589. struct lp55xx_chip *chip = led->chip;
  590. int ret;
  591. unsigned long val;
  592. if (kstrtoul(buf, 0, &val))
  593. return -EINVAL;
  594. if (val > 0xff)
  595. return -EINVAL;
  596. mutex_lock(&chip->lock);
  597. ret = lp55xx_write(chip, LP5523_REG_MASTER_FADER_BASE + nr - 1,
  598. (u8)val);
  599. mutex_unlock(&chip->lock);
  600. if (ret == 0)
  601. ret = len;
  602. return ret;
  603. }
  604. store_fader(1)
  605. store_fader(2)
  606. store_fader(3)
  607. static ssize_t show_master_fader_leds(struct device *dev,
  608. struct device_attribute *attr,
  609. char *buf)
  610. {
  611. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  612. struct lp55xx_chip *chip = led->chip;
  613. int i, ret, pos = 0;
  614. u8 val;
  615. mutex_lock(&chip->lock);
  616. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  617. ret = lp55xx_read(chip, LP5523_REG_LED_CTRL_BASE + i, &val);
  618. if (ret)
  619. goto leave;
  620. val = (val & LP5523_FADER_MAPPING_MASK)
  621. >> LP5523_FADER_MAPPING_SHIFT;
  622. if (val > 3) {
  623. ret = -EINVAL;
  624. goto leave;
  625. }
  626. buf[pos++] = val + '0';
  627. }
  628. buf[pos++] = '\n';
  629. ret = pos;
  630. leave:
  631. mutex_unlock(&chip->lock);
  632. return ret;
  633. }
  634. static ssize_t store_master_fader_leds(struct device *dev,
  635. struct device_attribute *attr,
  636. const char *buf, size_t len)
  637. {
  638. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  639. struct lp55xx_chip *chip = led->chip;
  640. int i, n, ret;
  641. u8 val;
  642. n = min_t(int, len, LP5523_MAX_LEDS);
  643. mutex_lock(&chip->lock);
  644. for (i = 0; i < n; i++) {
  645. if (buf[i] >= '0' && buf[i] <= '3') {
  646. val = (buf[i] - '0') << LP5523_FADER_MAPPING_SHIFT;
  647. ret = lp55xx_update_bits(chip,
  648. LP5523_REG_LED_CTRL_BASE + i,
  649. LP5523_FADER_MAPPING_MASK,
  650. val);
  651. if (ret)
  652. goto leave;
  653. } else {
  654. ret = -EINVAL;
  655. goto leave;
  656. }
  657. }
  658. ret = len;
  659. leave:
  660. mutex_unlock(&chip->lock);
  661. return ret;
  662. }
  663. static int lp5523_led_brightness(struct lp55xx_led *led)
  664. {
  665. struct lp55xx_chip *chip = led->chip;
  666. int ret;
  667. mutex_lock(&chip->lock);
  668. ret = lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  669. led->brightness);
  670. mutex_unlock(&chip->lock);
  671. return ret;
  672. }
  673. static LP55XX_DEV_ATTR_RW(engine1_mode, show_engine1_mode, store_engine1_mode);
  674. static LP55XX_DEV_ATTR_RW(engine2_mode, show_engine2_mode, store_engine2_mode);
  675. static LP55XX_DEV_ATTR_RW(engine3_mode, show_engine3_mode, store_engine3_mode);
  676. static LP55XX_DEV_ATTR_RW(engine1_leds, show_engine1_leds, store_engine1_leds);
  677. static LP55XX_DEV_ATTR_RW(engine2_leds, show_engine2_leds, store_engine2_leds);
  678. static LP55XX_DEV_ATTR_RW(engine3_leds, show_engine3_leds, store_engine3_leds);
  679. static LP55XX_DEV_ATTR_WO(engine1_load, store_engine1_load);
  680. static LP55XX_DEV_ATTR_WO(engine2_load, store_engine2_load);
  681. static LP55XX_DEV_ATTR_WO(engine3_load, store_engine3_load);
  682. static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
  683. static LP55XX_DEV_ATTR_RW(master_fader1, show_master_fader1,
  684. store_master_fader1);
  685. static LP55XX_DEV_ATTR_RW(master_fader2, show_master_fader2,
  686. store_master_fader2);
  687. static LP55XX_DEV_ATTR_RW(master_fader3, show_master_fader3,
  688. store_master_fader3);
  689. static LP55XX_DEV_ATTR_RW(master_fader_leds, show_master_fader_leds,
  690. store_master_fader_leds);
  691. static struct attribute *lp5523_attributes[] = {
  692. &dev_attr_engine1_mode.attr,
  693. &dev_attr_engine2_mode.attr,
  694. &dev_attr_engine3_mode.attr,
  695. &dev_attr_engine1_load.attr,
  696. &dev_attr_engine2_load.attr,
  697. &dev_attr_engine3_load.attr,
  698. &dev_attr_engine1_leds.attr,
  699. &dev_attr_engine2_leds.attr,
  700. &dev_attr_engine3_leds.attr,
  701. &dev_attr_selftest.attr,
  702. &dev_attr_master_fader1.attr,
  703. &dev_attr_master_fader2.attr,
  704. &dev_attr_master_fader3.attr,
  705. &dev_attr_master_fader_leds.attr,
  706. NULL,
  707. };
  708. static const struct attribute_group lp5523_group = {
  709. .attrs = lp5523_attributes,
  710. };
  711. /* Chip specific configurations */
  712. static struct lp55xx_device_config lp5523_cfg = {
  713. .reset = {
  714. .addr = LP5523_REG_RESET,
  715. .val = LP5523_RESET,
  716. },
  717. .enable = {
  718. .addr = LP5523_REG_ENABLE,
  719. .val = LP5523_ENABLE,
  720. },
  721. .max_channel = LP5523_MAX_LEDS,
  722. .post_init_device = lp5523_post_init_device,
  723. .brightness_fn = lp5523_led_brightness,
  724. .set_led_current = lp5523_set_led_current,
  725. .firmware_cb = lp5523_firmware_loaded,
  726. .run_engine = lp5523_run_engine,
  727. .dev_attr_group = &lp5523_group,
  728. };
  729. static int lp5523_probe(struct i2c_client *client,
  730. const struct i2c_device_id *id)
  731. {
  732. int ret;
  733. struct lp55xx_chip *chip;
  734. struct lp55xx_led *led;
  735. struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
  736. struct device_node *np = client->dev.of_node;
  737. if (!pdata) {
  738. if (np) {
  739. pdata = lp55xx_of_populate_pdata(&client->dev, np);
  740. if (IS_ERR(pdata))
  741. return PTR_ERR(pdata);
  742. } else {
  743. dev_err(&client->dev, "no platform data\n");
  744. return -EINVAL;
  745. }
  746. }
  747. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  748. if (!chip)
  749. return -ENOMEM;
  750. led = devm_kzalloc(&client->dev,
  751. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  752. if (!led)
  753. return -ENOMEM;
  754. chip->cl = client;
  755. chip->pdata = pdata;
  756. chip->cfg = &lp5523_cfg;
  757. mutex_init(&chip->lock);
  758. i2c_set_clientdata(client, led);
  759. ret = lp55xx_init_device(chip);
  760. if (ret)
  761. goto err_init;
  762. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  763. ret = lp55xx_register_leds(led, chip);
  764. if (ret)
  765. goto err_register_leds;
  766. ret = lp55xx_register_sysfs(chip);
  767. if (ret) {
  768. dev_err(&client->dev, "registering sysfs failed\n");
  769. goto err_register_sysfs;
  770. }
  771. return 0;
  772. err_register_sysfs:
  773. lp55xx_unregister_leds(led, chip);
  774. err_register_leds:
  775. lp55xx_deinit_device(chip);
  776. err_init:
  777. return ret;
  778. }
  779. static int lp5523_remove(struct i2c_client *client)
  780. {
  781. struct lp55xx_led *led = i2c_get_clientdata(client);
  782. struct lp55xx_chip *chip = led->chip;
  783. lp5523_stop_all_engines(chip);
  784. lp55xx_unregister_sysfs(chip);
  785. lp55xx_unregister_leds(led, chip);
  786. lp55xx_deinit_device(chip);
  787. return 0;
  788. }
  789. static const struct i2c_device_id lp5523_id[] = {
  790. { "lp5523", LP5523 },
  791. { "lp55231", LP55231 },
  792. { }
  793. };
  794. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  795. #ifdef CONFIG_OF
  796. static const struct of_device_id of_lp5523_leds_match[] = {
  797. { .compatible = "national,lp5523", },
  798. { .compatible = "ti,lp55231", },
  799. {},
  800. };
  801. MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
  802. #endif
  803. static struct i2c_driver lp5523_driver = {
  804. .driver = {
  805. .name = "lp5523x",
  806. .of_match_table = of_match_ptr(of_lp5523_leds_match),
  807. },
  808. .probe = lp5523_probe,
  809. .remove = lp5523_remove,
  810. .id_table = lp5523_id,
  811. };
  812. module_i2c_driver(lp5523_driver);
  813. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  814. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  815. MODULE_DESCRIPTION("LP5523 LED engine");
  816. MODULE_LICENSE("GPL");