leds-lp5523.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * lp5523.c - LP5523 LED Driver
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/mutex.h>
  26. #include <linux/gpio.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/ctype.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/wait.h>
  32. #include <linux/leds.h>
  33. #include <linux/leds-lp5523.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/slab.h>
  36. #include <linux/platform_data/leds-lp55xx.h>
  37. #include "leds-lp55xx-common.h"
  38. #define LP5523_REG_ENABLE 0x00
  39. #define LP5523_REG_OP_MODE 0x01
  40. #define LP5523_REG_RATIOMETRIC_MSB 0x02
  41. #define LP5523_REG_RATIOMETRIC_LSB 0x03
  42. #define LP5523_REG_ENABLE_LEDS_MSB 0x04
  43. #define LP5523_REG_ENABLE_LEDS_LSB 0x05
  44. #define LP5523_REG_LED_CNTRL_BASE 0x06
  45. #define LP5523_REG_LED_PWM_BASE 0x16
  46. #define LP5523_REG_LED_CURRENT_BASE 0x26
  47. #define LP5523_REG_CONFIG 0x36
  48. #define LP5523_REG_CHANNEL1_PC 0x37
  49. #define LP5523_REG_CHANNEL2_PC 0x38
  50. #define LP5523_REG_CHANNEL3_PC 0x39
  51. #define LP5523_REG_STATUS 0x3a
  52. #define LP5523_REG_GPO 0x3b
  53. #define LP5523_REG_VARIABLE 0x3c
  54. #define LP5523_REG_RESET 0x3d
  55. #define LP5523_REG_TEMP_CTRL 0x3e
  56. #define LP5523_REG_TEMP_READ 0x3f
  57. #define LP5523_REG_TEMP_WRITE 0x40
  58. #define LP5523_REG_LED_TEST_CTRL 0x41
  59. #define LP5523_REG_LED_TEST_ADC 0x42
  60. #define LP5523_REG_ENG1_VARIABLE 0x45
  61. #define LP5523_REG_ENG2_VARIABLE 0x46
  62. #define LP5523_REG_ENG3_VARIABLE 0x47
  63. #define LP5523_REG_MASTER_FADER1 0x48
  64. #define LP5523_REG_MASTER_FADER2 0x49
  65. #define LP5523_REG_MASTER_FADER3 0x4a
  66. #define LP5523_REG_CH1_PROG_START 0x4c
  67. #define LP5523_REG_CH2_PROG_START 0x4d
  68. #define LP5523_REG_CH3_PROG_START 0x4e
  69. #define LP5523_REG_PROG_PAGE_SEL 0x4f
  70. #define LP5523_REG_PROG_MEM 0x50
  71. #define LP5523_CMD_LOAD 0x15 /* 00010101 */
  72. #define LP5523_CMD_RUN 0x2a /* 00101010 */
  73. #define LP5523_CMD_DISABLED 0x00 /* 00000000 */
  74. #define LP5523_ENABLE 0x40
  75. #define LP5523_AUTO_INC 0x40
  76. #define LP5523_PWR_SAVE 0x20
  77. #define LP5523_PWM_PWR_SAVE 0x04
  78. #define LP5523_CP_1 0x08
  79. #define LP5523_CP_1_5 0x10
  80. #define LP5523_CP_AUTO 0x18
  81. #define LP5523_INT_CLK 0x01
  82. #define LP5523_AUTO_CLK 0x02
  83. #define LP5523_EN_LEDTEST 0x80
  84. #define LP5523_LEDTEST_DONE 0x80
  85. #define LP5523_RESET 0xFF
  86. #define LP5523_DEFAULT_CURRENT 50 /* microAmps */
  87. #define LP5523_PROGRAM_LENGTH 32 /* in bytes */
  88. #define LP5523_PROGRAM_PAGES 6
  89. #define LP5523_ADC_SHORTCIRC_LIM 80
  90. #define LP5523_MAX_LEDS 9
  91. #define LP5523_ENGINES 3
  92. #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */
  93. #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */
  94. #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
  95. #define LP5523_EXT_CLK_USED 0x08
  96. #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
  97. #define SHIFT_MASK(id) (((id) - 1) * 2)
  98. enum lp5523_chip_id {
  99. LP5523,
  100. LP55231,
  101. };
  102. struct lp5523_engine {
  103. int id;
  104. u8 mode;
  105. u8 prog_page;
  106. u8 mux_page;
  107. u16 led_mux;
  108. u8 engine_mask;
  109. };
  110. struct lp5523_led {
  111. int id;
  112. u8 chan_nr;
  113. u8 led_current;
  114. u8 max_current;
  115. struct led_classdev cdev;
  116. struct work_struct brightness_work;
  117. u8 brightness;
  118. };
  119. struct lp5523_chip {
  120. struct mutex lock; /* Serialize control */
  121. struct i2c_client *client;
  122. struct lp5523_engine engines[LP5523_ENGINES];
  123. struct lp5523_led leds[LP5523_MAX_LEDS];
  124. struct lp5523_platform_data *pdata;
  125. u8 num_channels;
  126. u8 num_leds;
  127. };
  128. static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
  129. {
  130. return container_of(cdev, struct lp5523_led, cdev);
  131. }
  132. static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
  133. {
  134. return container_of(engine, struct lp5523_chip,
  135. engines[engine->id - 1]);
  136. }
  137. static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
  138. {
  139. return container_of(led, struct lp5523_chip,
  140. leds[led->id]);
  141. }
  142. static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
  143. static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
  144. static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern);
  145. static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
  146. {
  147. return i2c_smbus_write_byte_data(client, reg, value);
  148. }
  149. static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf)
  150. {
  151. s32 ret = i2c_smbus_read_byte_data(client, reg);
  152. if (ret < 0)
  153. return ret;
  154. *buf = ret;
  155. return 0;
  156. }
  157. static int lp5523_post_init_device(struct lp55xx_chip *chip)
  158. {
  159. int ret;
  160. ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
  161. if (ret)
  162. return ret;
  163. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  164. usleep_range(1000, 2000);
  165. ret = lp55xx_write(chip, LP5523_REG_CONFIG,
  166. LP5523_AUTO_INC | LP5523_PWR_SAVE |
  167. LP5523_CP_AUTO | LP5523_AUTO_CLK |
  168. LP5523_PWM_PWR_SAVE);
  169. if (ret)
  170. return ret;
  171. /* turn on all leds */
  172. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
  173. if (ret)
  174. return ret;
  175. return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
  176. }
  177. static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
  178. {
  179. struct lp5523_chip *chip = engine_to_lp5523(engine);
  180. struct i2c_client *client = chip->client;
  181. int ret;
  182. u8 engine_state;
  183. ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
  184. if (ret)
  185. goto fail;
  186. engine_state &= ~(engine->engine_mask);
  187. /* set mode only for this engine */
  188. mode &= engine->engine_mask;
  189. engine_state |= mode;
  190. ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
  191. fail:
  192. return ret;
  193. }
  194. static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
  195. {
  196. struct lp5523_chip *chip = engine_to_lp5523(engine);
  197. struct i2c_client *client = chip->client;
  198. int ret = 0;
  199. ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  200. ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
  201. ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
  202. (u8)(mux >> 8));
  203. ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
  204. engine->led_mux = mux;
  205. return ret;
  206. }
  207. static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern)
  208. {
  209. struct lp5523_chip *chip = engine_to_lp5523(engine);
  210. struct i2c_client *client = chip->client;
  211. int ret = 0;
  212. ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  213. ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
  214. engine->prog_page);
  215. ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
  216. LP5523_PROGRAM_LENGTH, pattern);
  217. return ret;
  218. }
  219. static int lp5523_run_program(struct lp5523_engine *engine)
  220. {
  221. struct lp5523_chip *chip = engine_to_lp5523(engine);
  222. struct i2c_client *client = chip->client;
  223. int ret;
  224. ret = lp5523_write(client, LP5523_REG_ENABLE,
  225. LP5523_CMD_RUN | LP5523_ENABLE);
  226. if (ret)
  227. goto fail;
  228. ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
  229. fail:
  230. return ret;
  231. }
  232. static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
  233. {
  234. int i;
  235. u16 tmp_mux = 0;
  236. len = min_t(int, len, LP5523_MAX_LEDS);
  237. for (i = 0; i < len; i++) {
  238. switch (buf[i]) {
  239. case '1':
  240. tmp_mux |= (1 << i);
  241. break;
  242. case '0':
  243. break;
  244. case '\n':
  245. i = len;
  246. break;
  247. default:
  248. return -1;
  249. }
  250. }
  251. *mux = tmp_mux;
  252. return 0;
  253. }
  254. static void lp5523_mux_to_array(u16 led_mux, char *array)
  255. {
  256. int i, pos = 0;
  257. for (i = 0; i < LP5523_MAX_LEDS; i++)
  258. pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
  259. array[pos] = '\0';
  260. }
  261. /*--------------------------------------------------------------*/
  262. /* Sysfs interface */
  263. /*--------------------------------------------------------------*/
  264. static ssize_t show_engine_leds(struct device *dev,
  265. struct device_attribute *attr,
  266. char *buf, int nr)
  267. {
  268. struct i2c_client *client = to_i2c_client(dev);
  269. struct lp5523_chip *chip = i2c_get_clientdata(client);
  270. char mux[LP5523_MAX_LEDS + 1];
  271. lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
  272. return sprintf(buf, "%s\n", mux);
  273. }
  274. #define show_leds(nr) \
  275. static ssize_t show_engine##nr##_leds(struct device *dev, \
  276. struct device_attribute *attr, \
  277. char *buf) \
  278. { \
  279. return show_engine_leds(dev, attr, buf, nr); \
  280. }
  281. show_leds(1)
  282. show_leds(2)
  283. show_leds(3)
  284. static ssize_t store_engine_leds(struct device *dev,
  285. struct device_attribute *attr,
  286. const char *buf, size_t len, int nr)
  287. {
  288. struct i2c_client *client = to_i2c_client(dev);
  289. struct lp5523_chip *chip = i2c_get_clientdata(client);
  290. u16 mux = 0;
  291. ssize_t ret;
  292. if (lp5523_mux_parse(buf, &mux, len))
  293. return -EINVAL;
  294. mutex_lock(&chip->lock);
  295. ret = -EINVAL;
  296. if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
  297. goto leave;
  298. if (lp5523_load_mux(&chip->engines[nr - 1], mux))
  299. goto leave;
  300. ret = len;
  301. leave:
  302. mutex_unlock(&chip->lock);
  303. return ret;
  304. }
  305. #define store_leds(nr) \
  306. static ssize_t store_engine##nr##_leds(struct device *dev, \
  307. struct device_attribute *attr, \
  308. const char *buf, size_t len) \
  309. { \
  310. return store_engine_leds(dev, attr, buf, len, nr); \
  311. }
  312. store_leds(1)
  313. store_leds(2)
  314. store_leds(3)
  315. static ssize_t lp5523_selftest(struct device *dev,
  316. struct device_attribute *attr,
  317. char *buf)
  318. {
  319. struct i2c_client *client = to_i2c_client(dev);
  320. struct lp5523_chip *chip = i2c_get_clientdata(client);
  321. int i, ret, pos = 0;
  322. int led = 0;
  323. u8 status, adc, vdd;
  324. mutex_lock(&chip->lock);
  325. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  326. if (ret < 0)
  327. goto fail;
  328. /* Check that ext clock is really in use if requested */
  329. if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT))
  330. if ((status & LP5523_EXT_CLK_USED) == 0)
  331. goto fail;
  332. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  333. lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL,
  334. LP5523_EN_LEDTEST | 16);
  335. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  336. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  337. if (ret < 0)
  338. goto fail;
  339. if (!(status & LP5523_LEDTEST_DONE))
  340. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  341. ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
  342. if (ret < 0)
  343. goto fail;
  344. vdd--; /* There may be some fluctuation in measurement */
  345. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  346. /* Skip non-existing channels */
  347. if (chip->pdata->led_config[i].led_current == 0)
  348. continue;
  349. /* Set default current */
  350. lp5523_write(chip->client,
  351. LP5523_REG_LED_CURRENT_BASE + i,
  352. chip->pdata->led_config[i].led_current);
  353. lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff);
  354. /* let current stabilize 2 - 4ms before measurements start */
  355. usleep_range(2000, 4000);
  356. lp5523_write(chip->client,
  357. LP5523_REG_LED_TEST_CTRL,
  358. LP5523_EN_LEDTEST | i);
  359. /* ADC conversion time is 2.7 ms typically */
  360. usleep_range(3000, 6000);
  361. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  362. if (ret < 0)
  363. goto fail;
  364. if (!(status & LP5523_LEDTEST_DONE))
  365. usleep_range(3000, 6000);/* Was not ready. Wait. */
  366. ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
  367. if (ret < 0)
  368. goto fail;
  369. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  370. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  371. lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
  372. /* Restore current */
  373. lp5523_write(chip->client,
  374. LP5523_REG_LED_CURRENT_BASE + i,
  375. chip->leds[led].led_current);
  376. led++;
  377. }
  378. if (pos == 0)
  379. pos = sprintf(buf, "OK\n");
  380. goto release_lock;
  381. fail:
  382. pos = sprintf(buf, "FAIL\n");
  383. release_lock:
  384. mutex_unlock(&chip->lock);
  385. return pos;
  386. }
  387. static void lp5523_led_brightness_work(struct work_struct *work)
  388. {
  389. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  390. brightness_work);
  391. struct lp55xx_chip *chip = led->chip;
  392. mutex_lock(&chip->lock);
  393. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  394. led->brightness);
  395. mutex_unlock(&chip->lock);
  396. }
  397. static int lp5523_do_store_load(struct lp5523_engine *engine,
  398. const char *buf, size_t len)
  399. {
  400. struct lp5523_chip *chip = engine_to_lp5523(engine);
  401. struct i2c_client *client = chip->client;
  402. int ret, nrchars, offset = 0, i = 0;
  403. char c[3];
  404. unsigned cmd;
  405. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  406. if (engine->mode != LP5523_CMD_LOAD)
  407. return -EINVAL;
  408. while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  409. /* separate sscanfs because length is working only for %s */
  410. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  411. ret = sscanf(c, "%2x", &cmd);
  412. if (ret != 1)
  413. goto fail;
  414. pattern[i] = (u8)cmd;
  415. offset += nrchars;
  416. i++;
  417. }
  418. /* Each instruction is 16bit long. Check that length is even */
  419. if (i % 2)
  420. goto fail;
  421. mutex_lock(&chip->lock);
  422. ret = lp5523_load_program(engine, pattern);
  423. mutex_unlock(&chip->lock);
  424. if (ret) {
  425. dev_err(&client->dev, "failed loading pattern\n");
  426. return ret;
  427. }
  428. return len;
  429. fail:
  430. dev_err(&client->dev, "wrong pattern format\n");
  431. return -EINVAL;
  432. }
  433. static ssize_t store_engine_load(struct device *dev,
  434. struct device_attribute *attr,
  435. const char *buf, size_t len, int nr)
  436. {
  437. struct i2c_client *client = to_i2c_client(dev);
  438. struct lp5523_chip *chip = i2c_get_clientdata(client);
  439. return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
  440. }
  441. #define store_load(nr) \
  442. static ssize_t store_engine##nr##_load(struct device *dev, \
  443. struct device_attribute *attr, \
  444. const char *buf, size_t len) \
  445. { \
  446. return store_engine_load(dev, attr, buf, len, nr); \
  447. }
  448. store_load(1)
  449. store_load(2)
  450. store_load(3)
  451. static ssize_t show_engine_mode(struct device *dev,
  452. struct device_attribute *attr,
  453. char *buf, int nr)
  454. {
  455. struct i2c_client *client = to_i2c_client(dev);
  456. struct lp5523_chip *chip = i2c_get_clientdata(client);
  457. switch (chip->engines[nr - 1].mode) {
  458. case LP5523_CMD_RUN:
  459. return sprintf(buf, "run\n");
  460. case LP5523_CMD_LOAD:
  461. return sprintf(buf, "load\n");
  462. case LP5523_CMD_DISABLED:
  463. return sprintf(buf, "disabled\n");
  464. default:
  465. return sprintf(buf, "disabled\n");
  466. }
  467. }
  468. #define show_mode(nr) \
  469. static ssize_t show_engine##nr##_mode(struct device *dev, \
  470. struct device_attribute *attr, \
  471. char *buf) \
  472. { \
  473. return show_engine_mode(dev, attr, buf, nr); \
  474. }
  475. show_mode(1)
  476. show_mode(2)
  477. show_mode(3)
  478. static ssize_t store_engine_mode(struct device *dev,
  479. struct device_attribute *attr,
  480. const char *buf, size_t len, int nr)
  481. {
  482. struct i2c_client *client = to_i2c_client(dev);
  483. struct lp5523_chip *chip = i2c_get_clientdata(client);
  484. struct lp5523_engine *engine = &chip->engines[nr - 1];
  485. mutex_lock(&chip->lock);
  486. if (!strncmp(buf, "run", 3))
  487. lp5523_set_mode(engine, LP5523_CMD_RUN);
  488. else if (!strncmp(buf, "load", 4))
  489. lp5523_set_mode(engine, LP5523_CMD_LOAD);
  490. else if (!strncmp(buf, "disabled", 8))
  491. lp5523_set_mode(engine, LP5523_CMD_DISABLED);
  492. mutex_unlock(&chip->lock);
  493. return len;
  494. }
  495. #define store_mode(nr) \
  496. static ssize_t store_engine##nr##_mode(struct device *dev, \
  497. struct device_attribute *attr, \
  498. const char *buf, size_t len) \
  499. { \
  500. return store_engine_mode(dev, attr, buf, len, nr); \
  501. }
  502. store_mode(1)
  503. store_mode(2)
  504. store_mode(3)
  505. static ssize_t show_max_current(struct device *dev,
  506. struct device_attribute *attr,
  507. char *buf)
  508. {
  509. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  510. struct lp5523_led *led = cdev_to_led(led_cdev);
  511. return sprintf(buf, "%d\n", led->max_current);
  512. }
  513. static ssize_t show_current(struct device *dev,
  514. struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  518. struct lp5523_led *led = cdev_to_led(led_cdev);
  519. return sprintf(buf, "%d\n", led->led_current);
  520. }
  521. static ssize_t store_current(struct device *dev,
  522. struct device_attribute *attr,
  523. const char *buf, size_t len)
  524. {
  525. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  526. struct lp5523_led *led = cdev_to_led(led_cdev);
  527. struct lp5523_chip *chip = led_to_lp5523(led);
  528. ssize_t ret;
  529. unsigned long curr;
  530. if (kstrtoul(buf, 0, &curr))
  531. return -EINVAL;
  532. if (curr > led->max_current)
  533. return -EINVAL;
  534. mutex_lock(&chip->lock);
  535. ret = lp5523_write(chip->client,
  536. LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  537. (u8)curr);
  538. mutex_unlock(&chip->lock);
  539. if (ret < 0)
  540. return ret;
  541. led->led_current = (u8)curr;
  542. return len;
  543. }
  544. /* led class device attributes */
  545. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
  546. static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
  547. static struct attribute *lp5523_led_attributes[] = {
  548. &dev_attr_led_current.attr,
  549. &dev_attr_max_current.attr,
  550. NULL,
  551. };
  552. static struct attribute_group lp5523_led_attribute_group = {
  553. .attrs = lp5523_led_attributes
  554. };
  555. /* device attributes */
  556. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
  557. show_engine1_mode, store_engine1_mode);
  558. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
  559. show_engine2_mode, store_engine2_mode);
  560. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
  561. show_engine3_mode, store_engine3_mode);
  562. static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR,
  563. show_engine1_leds, store_engine1_leds);
  564. static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR,
  565. show_engine2_leds, store_engine2_leds);
  566. static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR,
  567. show_engine3_leds, store_engine3_leds);
  568. static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
  569. static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
  570. static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
  571. static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
  572. static struct attribute *lp5523_attributes[] = {
  573. &dev_attr_engine1_mode.attr,
  574. &dev_attr_engine2_mode.attr,
  575. &dev_attr_engine3_mode.attr,
  576. &dev_attr_selftest.attr,
  577. &dev_attr_engine1_load.attr,
  578. &dev_attr_engine1_leds.attr,
  579. &dev_attr_engine2_load.attr,
  580. &dev_attr_engine2_leds.attr,
  581. &dev_attr_engine3_load.attr,
  582. &dev_attr_engine3_leds.attr,
  583. NULL,
  584. };
  585. static const struct attribute_group lp5523_group = {
  586. .attrs = lp5523_attributes,
  587. };
  588. static int lp5523_register_sysfs(struct i2c_client *client)
  589. {
  590. struct device *dev = &client->dev;
  591. int ret;
  592. ret = sysfs_create_group(&dev->kobj, &lp5523_group);
  593. if (ret < 0)
  594. return ret;
  595. return 0;
  596. }
  597. static void lp5523_unregister_sysfs(struct i2c_client *client)
  598. {
  599. struct lp5523_chip *chip = i2c_get_clientdata(client);
  600. struct device *dev = &client->dev;
  601. int i;
  602. sysfs_remove_group(&dev->kobj, &lp5523_group);
  603. for (i = 0; i < chip->num_leds; i++)
  604. sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
  605. &lp5523_led_attribute_group);
  606. }
  607. /*--------------------------------------------------------------*/
  608. /* Set chip operating mode */
  609. /*--------------------------------------------------------------*/
  610. static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
  611. {
  612. /* if in that mode already do nothing, except for run */
  613. if (mode == engine->mode && mode != LP5523_CMD_RUN)
  614. return;
  615. switch (mode) {
  616. case LP5523_CMD_RUN:
  617. lp5523_run_program(engine);
  618. break;
  619. case LP5523_CMD_LOAD:
  620. lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
  621. lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  622. break;
  623. case LP5523_CMD_DISABLED:
  624. lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
  625. break;
  626. default:
  627. return;
  628. }
  629. engine->mode = mode;
  630. }
  631. /*--------------------------------------------------------------*/
  632. /* Probe, Attach, Remove */
  633. /*--------------------------------------------------------------*/
  634. static void lp5523_unregister_leds(struct lp5523_chip *chip)
  635. {
  636. int i;
  637. for (i = 0; i < chip->num_leds; i++) {
  638. led_classdev_unregister(&chip->leds[i].cdev);
  639. flush_work(&chip->leds[i].brightness_work);
  640. }
  641. }
  642. /* Chip specific configurations */
  643. static struct lp55xx_device_config lp5523_cfg = {
  644. .reset = {
  645. .addr = LP5523_REG_RESET,
  646. .val = LP5523_RESET,
  647. },
  648. .enable = {
  649. .addr = LP5523_REG_ENABLE,
  650. .val = LP5523_ENABLE,
  651. },
  652. .max_channel = LP5523_MAX_LEDS,
  653. .post_init_device = lp5523_post_init_device,
  654. .brightness_work_fn = lp5523_led_brightness_work,
  655. };
  656. static int lp5523_probe(struct i2c_client *client,
  657. const struct i2c_device_id *id)
  658. {
  659. struct lp5523_chip *old_chip = NULL;
  660. int ret;
  661. struct lp55xx_chip *chip;
  662. struct lp55xx_led *led;
  663. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  664. if (!pdata) {
  665. dev_err(&client->dev, "no platform data\n");
  666. return -EINVAL;
  667. }
  668. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  669. if (!chip)
  670. return -ENOMEM;
  671. led = devm_kzalloc(&client->dev,
  672. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  673. if (!led)
  674. return -ENOMEM;
  675. chip->cl = client;
  676. chip->pdata = pdata;
  677. chip->cfg = &lp5523_cfg;
  678. mutex_init(&chip->lock);
  679. i2c_set_clientdata(client, led);
  680. ret = lp55xx_init_device(chip);
  681. if (ret)
  682. goto err_init;
  683. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  684. ret = lp55xx_register_leds(led, chip);
  685. if (ret)
  686. goto err_register_leds;
  687. ret = lp5523_register_sysfs(client);
  688. if (ret) {
  689. dev_err(&client->dev, "registering sysfs failed\n");
  690. goto fail2;
  691. }
  692. return ret;
  693. fail2:
  694. lp5523_unregister_leds(old_chip);
  695. err_register_leds:
  696. lp55xx_deinit_device(chip);
  697. err_init:
  698. return ret;
  699. }
  700. static int lp5523_remove(struct i2c_client *client)
  701. {
  702. struct lp5523_chip *old_chip = i2c_get_clientdata(client);
  703. struct lp55xx_led *led = i2c_get_clientdata(client);
  704. struct lp55xx_chip *chip = led->chip;
  705. /* Disable engine mode */
  706. lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
  707. lp5523_unregister_sysfs(client);
  708. lp5523_unregister_leds(old_chip);
  709. lp55xx_deinit_device(chip);
  710. return 0;
  711. }
  712. static const struct i2c_device_id lp5523_id[] = {
  713. { "lp5523", LP5523 },
  714. { "lp55231", LP55231 },
  715. { }
  716. };
  717. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  718. static struct i2c_driver lp5523_driver = {
  719. .driver = {
  720. .name = "lp5523x",
  721. },
  722. .probe = lp5523_probe,
  723. .remove = lp5523_remove,
  724. .id_table = lp5523_id,
  725. };
  726. module_i2c_driver(lp5523_driver);
  727. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  728. MODULE_DESCRIPTION("LP5523 LED engine");
  729. MODULE_LICENSE("GPL");