leds-lp5523.c 25 KB

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