leds-lp5523.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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 void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
  129. {
  130. led->led_current = led_current;
  131. lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  132. led_current);
  133. }
  134. static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
  135. {
  136. return container_of(cdev, struct lp5523_led, cdev);
  137. }
  138. static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
  139. {
  140. return container_of(engine, struct lp5523_chip,
  141. engines[engine->id - 1]);
  142. }
  143. static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
  144. {
  145. return container_of(led, struct lp5523_chip,
  146. leds[led->id]);
  147. }
  148. static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
  149. static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
  150. static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern);
  151. static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
  152. {
  153. return i2c_smbus_write_byte_data(client, reg, value);
  154. }
  155. static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf)
  156. {
  157. s32 ret = i2c_smbus_read_byte_data(client, reg);
  158. if (ret < 0)
  159. return ret;
  160. *buf = ret;
  161. return 0;
  162. }
  163. static int lp5523_post_init_device(struct lp55xx_chip *chip)
  164. {
  165. int ret;
  166. ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
  167. if (ret)
  168. return ret;
  169. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  170. usleep_range(1000, 2000);
  171. ret = lp55xx_write(chip, LP5523_REG_CONFIG,
  172. LP5523_AUTO_INC | LP5523_PWR_SAVE |
  173. LP5523_CP_AUTO | LP5523_AUTO_CLK |
  174. LP5523_PWM_PWR_SAVE);
  175. if (ret)
  176. return ret;
  177. /* turn on all leds */
  178. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
  179. if (ret)
  180. return ret;
  181. return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
  182. }
  183. static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
  184. {
  185. struct lp5523_chip *chip = engine_to_lp5523(engine);
  186. struct i2c_client *client = chip->client;
  187. int ret;
  188. u8 engine_state;
  189. ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
  190. if (ret)
  191. goto fail;
  192. engine_state &= ~(engine->engine_mask);
  193. /* set mode only for this engine */
  194. mode &= engine->engine_mask;
  195. engine_state |= mode;
  196. ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
  197. fail:
  198. return ret;
  199. }
  200. static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
  201. {
  202. struct lp5523_chip *chip = engine_to_lp5523(engine);
  203. struct i2c_client *client = chip->client;
  204. int ret = 0;
  205. ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  206. ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
  207. ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
  208. (u8)(mux >> 8));
  209. ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
  210. engine->led_mux = mux;
  211. return ret;
  212. }
  213. static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern)
  214. {
  215. struct lp5523_chip *chip = engine_to_lp5523(engine);
  216. struct i2c_client *client = chip->client;
  217. int ret = 0;
  218. ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  219. ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
  220. engine->prog_page);
  221. ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
  222. LP5523_PROGRAM_LENGTH, pattern);
  223. return ret;
  224. }
  225. static int lp5523_run_program(struct lp5523_engine *engine)
  226. {
  227. struct lp5523_chip *chip = engine_to_lp5523(engine);
  228. struct i2c_client *client = chip->client;
  229. int ret;
  230. ret = lp5523_write(client, LP5523_REG_ENABLE,
  231. LP5523_CMD_RUN | LP5523_ENABLE);
  232. if (ret)
  233. goto fail;
  234. ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
  235. fail:
  236. return ret;
  237. }
  238. static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
  239. {
  240. int i;
  241. u16 tmp_mux = 0;
  242. len = min_t(int, len, LP5523_MAX_LEDS);
  243. for (i = 0; i < len; i++) {
  244. switch (buf[i]) {
  245. case '1':
  246. tmp_mux |= (1 << i);
  247. break;
  248. case '0':
  249. break;
  250. case '\n':
  251. i = len;
  252. break;
  253. default:
  254. return -1;
  255. }
  256. }
  257. *mux = tmp_mux;
  258. return 0;
  259. }
  260. static void lp5523_mux_to_array(u16 led_mux, char *array)
  261. {
  262. int i, pos = 0;
  263. for (i = 0; i < LP5523_MAX_LEDS; i++)
  264. pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
  265. array[pos] = '\0';
  266. }
  267. /*--------------------------------------------------------------*/
  268. /* Sysfs interface */
  269. /*--------------------------------------------------------------*/
  270. static ssize_t show_engine_leds(struct device *dev,
  271. struct device_attribute *attr,
  272. char *buf, int nr)
  273. {
  274. struct i2c_client *client = to_i2c_client(dev);
  275. struct lp5523_chip *chip = i2c_get_clientdata(client);
  276. char mux[LP5523_MAX_LEDS + 1];
  277. lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
  278. return sprintf(buf, "%s\n", mux);
  279. }
  280. #define show_leds(nr) \
  281. static ssize_t show_engine##nr##_leds(struct device *dev, \
  282. struct device_attribute *attr, \
  283. char *buf) \
  284. { \
  285. return show_engine_leds(dev, attr, buf, nr); \
  286. }
  287. show_leds(1)
  288. show_leds(2)
  289. show_leds(3)
  290. static ssize_t store_engine_leds(struct device *dev,
  291. struct device_attribute *attr,
  292. const char *buf, size_t len, int nr)
  293. {
  294. struct i2c_client *client = to_i2c_client(dev);
  295. struct lp5523_chip *chip = i2c_get_clientdata(client);
  296. u16 mux = 0;
  297. ssize_t ret;
  298. if (lp5523_mux_parse(buf, &mux, len))
  299. return -EINVAL;
  300. mutex_lock(&chip->lock);
  301. ret = -EINVAL;
  302. if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
  303. goto leave;
  304. if (lp5523_load_mux(&chip->engines[nr - 1], mux))
  305. goto leave;
  306. ret = len;
  307. leave:
  308. mutex_unlock(&chip->lock);
  309. return ret;
  310. }
  311. #define store_leds(nr) \
  312. static ssize_t store_engine##nr##_leds(struct device *dev, \
  313. struct device_attribute *attr, \
  314. const char *buf, size_t len) \
  315. { \
  316. return store_engine_leds(dev, attr, buf, len, nr); \
  317. }
  318. store_leds(1)
  319. store_leds(2)
  320. store_leds(3)
  321. static ssize_t lp5523_selftest(struct device *dev,
  322. struct device_attribute *attr,
  323. char *buf)
  324. {
  325. struct i2c_client *client = to_i2c_client(dev);
  326. struct lp5523_chip *chip = i2c_get_clientdata(client);
  327. int i, ret, pos = 0;
  328. int led = 0;
  329. u8 status, adc, vdd;
  330. mutex_lock(&chip->lock);
  331. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  332. if (ret < 0)
  333. goto fail;
  334. /* Check that ext clock is really in use if requested */
  335. if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT))
  336. if ((status & LP5523_EXT_CLK_USED) == 0)
  337. goto fail;
  338. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  339. lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL,
  340. LP5523_EN_LEDTEST | 16);
  341. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  342. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  343. if (ret < 0)
  344. goto fail;
  345. if (!(status & LP5523_LEDTEST_DONE))
  346. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  347. ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
  348. if (ret < 0)
  349. goto fail;
  350. vdd--; /* There may be some fluctuation in measurement */
  351. for (i = 0; i < LP5523_MAX_LEDS; i++) {
  352. /* Skip non-existing channels */
  353. if (chip->pdata->led_config[i].led_current == 0)
  354. continue;
  355. /* Set default current */
  356. lp5523_write(chip->client,
  357. LP5523_REG_LED_CURRENT_BASE + i,
  358. chip->pdata->led_config[i].led_current);
  359. lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff);
  360. /* let current stabilize 2 - 4ms before measurements start */
  361. usleep_range(2000, 4000);
  362. lp5523_write(chip->client,
  363. LP5523_REG_LED_TEST_CTRL,
  364. LP5523_EN_LEDTEST | i);
  365. /* ADC conversion time is 2.7 ms typically */
  366. usleep_range(3000, 6000);
  367. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  368. if (ret < 0)
  369. goto fail;
  370. if (!(status & LP5523_LEDTEST_DONE))
  371. usleep_range(3000, 6000);/* Was not ready. Wait. */
  372. ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
  373. if (ret < 0)
  374. goto fail;
  375. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  376. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  377. lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
  378. /* Restore current */
  379. lp5523_write(chip->client,
  380. LP5523_REG_LED_CURRENT_BASE + i,
  381. chip->leds[led].led_current);
  382. led++;
  383. }
  384. if (pos == 0)
  385. pos = sprintf(buf, "OK\n");
  386. goto release_lock;
  387. fail:
  388. pos = sprintf(buf, "FAIL\n");
  389. release_lock:
  390. mutex_unlock(&chip->lock);
  391. return pos;
  392. }
  393. static void lp5523_led_brightness_work(struct work_struct *work)
  394. {
  395. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  396. brightness_work);
  397. struct lp55xx_chip *chip = led->chip;
  398. mutex_lock(&chip->lock);
  399. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  400. led->brightness);
  401. mutex_unlock(&chip->lock);
  402. }
  403. static int lp5523_do_store_load(struct lp5523_engine *engine,
  404. const char *buf, size_t len)
  405. {
  406. struct lp5523_chip *chip = engine_to_lp5523(engine);
  407. struct i2c_client *client = chip->client;
  408. int ret, nrchars, offset = 0, i = 0;
  409. char c[3];
  410. unsigned cmd;
  411. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  412. if (engine->mode != LP5523_CMD_LOAD)
  413. return -EINVAL;
  414. while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  415. /* separate sscanfs because length is working only for %s */
  416. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  417. ret = sscanf(c, "%2x", &cmd);
  418. if (ret != 1)
  419. goto fail;
  420. pattern[i] = (u8)cmd;
  421. offset += nrchars;
  422. i++;
  423. }
  424. /* Each instruction is 16bit long. Check that length is even */
  425. if (i % 2)
  426. goto fail;
  427. mutex_lock(&chip->lock);
  428. ret = lp5523_load_program(engine, pattern);
  429. mutex_unlock(&chip->lock);
  430. if (ret) {
  431. dev_err(&client->dev, "failed loading pattern\n");
  432. return ret;
  433. }
  434. return len;
  435. fail:
  436. dev_err(&client->dev, "wrong pattern format\n");
  437. return -EINVAL;
  438. }
  439. static ssize_t store_engine_load(struct device *dev,
  440. struct device_attribute *attr,
  441. const char *buf, size_t len, int nr)
  442. {
  443. struct i2c_client *client = to_i2c_client(dev);
  444. struct lp5523_chip *chip = i2c_get_clientdata(client);
  445. return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
  446. }
  447. #define store_load(nr) \
  448. static ssize_t store_engine##nr##_load(struct device *dev, \
  449. struct device_attribute *attr, \
  450. const char *buf, size_t len) \
  451. { \
  452. return store_engine_load(dev, attr, buf, len, nr); \
  453. }
  454. store_load(1)
  455. store_load(2)
  456. store_load(3)
  457. static ssize_t show_engine_mode(struct device *dev,
  458. struct device_attribute *attr,
  459. char *buf, int nr)
  460. {
  461. struct i2c_client *client = to_i2c_client(dev);
  462. struct lp5523_chip *chip = i2c_get_clientdata(client);
  463. switch (chip->engines[nr - 1].mode) {
  464. case LP5523_CMD_RUN:
  465. return sprintf(buf, "run\n");
  466. case LP5523_CMD_LOAD:
  467. return sprintf(buf, "load\n");
  468. case LP5523_CMD_DISABLED:
  469. return sprintf(buf, "disabled\n");
  470. default:
  471. return sprintf(buf, "disabled\n");
  472. }
  473. }
  474. #define show_mode(nr) \
  475. static ssize_t show_engine##nr##_mode(struct device *dev, \
  476. struct device_attribute *attr, \
  477. char *buf) \
  478. { \
  479. return show_engine_mode(dev, attr, buf, nr); \
  480. }
  481. show_mode(1)
  482. show_mode(2)
  483. show_mode(3)
  484. static ssize_t store_engine_mode(struct device *dev,
  485. struct device_attribute *attr,
  486. const char *buf, size_t len, int nr)
  487. {
  488. struct i2c_client *client = to_i2c_client(dev);
  489. struct lp5523_chip *chip = i2c_get_clientdata(client);
  490. struct lp5523_engine *engine = &chip->engines[nr - 1];
  491. mutex_lock(&chip->lock);
  492. if (!strncmp(buf, "run", 3))
  493. lp5523_set_mode(engine, LP5523_CMD_RUN);
  494. else if (!strncmp(buf, "load", 4))
  495. lp5523_set_mode(engine, LP5523_CMD_LOAD);
  496. else if (!strncmp(buf, "disabled", 8))
  497. lp5523_set_mode(engine, LP5523_CMD_DISABLED);
  498. mutex_unlock(&chip->lock);
  499. return len;
  500. }
  501. #define store_mode(nr) \
  502. static ssize_t store_engine##nr##_mode(struct device *dev, \
  503. struct device_attribute *attr, \
  504. const char *buf, size_t len) \
  505. { \
  506. return store_engine_mode(dev, attr, buf, len, nr); \
  507. }
  508. store_mode(1)
  509. store_mode(2)
  510. store_mode(3)
  511. /* device attributes */
  512. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
  513. show_engine1_mode, store_engine1_mode);
  514. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
  515. show_engine2_mode, store_engine2_mode);
  516. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
  517. show_engine3_mode, store_engine3_mode);
  518. static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR,
  519. show_engine1_leds, store_engine1_leds);
  520. static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR,
  521. show_engine2_leds, store_engine2_leds);
  522. static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR,
  523. show_engine3_leds, store_engine3_leds);
  524. static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
  525. static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
  526. static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
  527. static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
  528. static struct attribute *lp5523_attributes[] = {
  529. &dev_attr_engine1_mode.attr,
  530. &dev_attr_engine2_mode.attr,
  531. &dev_attr_engine3_mode.attr,
  532. &dev_attr_selftest.attr,
  533. &dev_attr_engine1_load.attr,
  534. &dev_attr_engine1_leds.attr,
  535. &dev_attr_engine2_load.attr,
  536. &dev_attr_engine2_leds.attr,
  537. &dev_attr_engine3_load.attr,
  538. &dev_attr_engine3_leds.attr,
  539. NULL,
  540. };
  541. static const struct attribute_group lp5523_group = {
  542. .attrs = lp5523_attributes,
  543. };
  544. static int lp5523_register_sysfs(struct i2c_client *client)
  545. {
  546. struct device *dev = &client->dev;
  547. int ret;
  548. ret = sysfs_create_group(&dev->kobj, &lp5523_group);
  549. if (ret < 0)
  550. return ret;
  551. return 0;
  552. }
  553. static void lp5523_unregister_sysfs(struct i2c_client *client)
  554. {
  555. struct device *dev = &client->dev;
  556. sysfs_remove_group(&dev->kobj, &lp5523_group);
  557. }
  558. /*--------------------------------------------------------------*/
  559. /* Set chip operating mode */
  560. /*--------------------------------------------------------------*/
  561. static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
  562. {
  563. /* if in that mode already do nothing, except for run */
  564. if (mode == engine->mode && mode != LP5523_CMD_RUN)
  565. return;
  566. switch (mode) {
  567. case LP5523_CMD_RUN:
  568. lp5523_run_program(engine);
  569. break;
  570. case LP5523_CMD_LOAD:
  571. lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
  572. lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  573. break;
  574. case LP5523_CMD_DISABLED:
  575. lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
  576. break;
  577. default:
  578. return;
  579. }
  580. engine->mode = mode;
  581. }
  582. /*--------------------------------------------------------------*/
  583. /* Probe, Attach, Remove */
  584. /*--------------------------------------------------------------*/
  585. /* Chip specific configurations */
  586. static struct lp55xx_device_config lp5523_cfg = {
  587. .reset = {
  588. .addr = LP5523_REG_RESET,
  589. .val = LP5523_RESET,
  590. },
  591. .enable = {
  592. .addr = LP5523_REG_ENABLE,
  593. .val = LP5523_ENABLE,
  594. },
  595. .max_channel = LP5523_MAX_LEDS,
  596. .post_init_device = lp5523_post_init_device,
  597. .brightness_work_fn = lp5523_led_brightness_work,
  598. .set_led_current = lp5523_set_led_current,
  599. };
  600. static int lp5523_probe(struct i2c_client *client,
  601. const struct i2c_device_id *id)
  602. {
  603. int ret;
  604. struct lp55xx_chip *chip;
  605. struct lp55xx_led *led;
  606. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  607. if (!pdata) {
  608. dev_err(&client->dev, "no platform data\n");
  609. return -EINVAL;
  610. }
  611. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  612. if (!chip)
  613. return -ENOMEM;
  614. led = devm_kzalloc(&client->dev,
  615. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  616. if (!led)
  617. return -ENOMEM;
  618. chip->cl = client;
  619. chip->pdata = pdata;
  620. chip->cfg = &lp5523_cfg;
  621. mutex_init(&chip->lock);
  622. i2c_set_clientdata(client, led);
  623. ret = lp55xx_init_device(chip);
  624. if (ret)
  625. goto err_init;
  626. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  627. ret = lp55xx_register_leds(led, chip);
  628. if (ret)
  629. goto err_register_leds;
  630. ret = lp5523_register_sysfs(client);
  631. if (ret) {
  632. dev_err(&client->dev, "registering sysfs failed\n");
  633. goto fail2;
  634. }
  635. return ret;
  636. fail2:
  637. lp55xx_unregister_leds(led, chip);
  638. err_register_leds:
  639. lp55xx_deinit_device(chip);
  640. err_init:
  641. return ret;
  642. }
  643. static int lp5523_remove(struct i2c_client *client)
  644. {
  645. struct lp55xx_led *led = i2c_get_clientdata(client);
  646. struct lp55xx_chip *chip = led->chip;
  647. /* Disable engine mode */
  648. lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
  649. lp5523_unregister_sysfs(client);
  650. lp55xx_unregister_leds(led, chip);
  651. lp55xx_deinit_device(chip);
  652. return 0;
  653. }
  654. static const struct i2c_device_id lp5523_id[] = {
  655. { "lp5523", LP5523 },
  656. { "lp55231", LP55231 },
  657. { }
  658. };
  659. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  660. static struct i2c_driver lp5523_driver = {
  661. .driver = {
  662. .name = "lp5523x",
  663. },
  664. .probe = lp5523_probe,
  665. .remove = lp5523_remove,
  666. .id_table = lp5523_id,
  667. };
  668. module_i2c_driver(lp5523_driver);
  669. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  670. MODULE_DESCRIPTION("LP5523 LED engine");
  671. MODULE_LICENSE("GPL");