leds-lp5523.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. #define LP5523_REG_ENABLE 0x00
  37. #define LP5523_REG_OP_MODE 0x01
  38. #define LP5523_REG_RATIOMETRIC_MSB 0x02
  39. #define LP5523_REG_RATIOMETRIC_LSB 0x03
  40. #define LP5523_REG_ENABLE_LEDS_MSB 0x04
  41. #define LP5523_REG_ENABLE_LEDS_LSB 0x05
  42. #define LP5523_REG_LED_CNTRL_BASE 0x06
  43. #define LP5523_REG_LED_PWM_BASE 0x16
  44. #define LP5523_REG_LED_CURRENT_BASE 0x26
  45. #define LP5523_REG_CONFIG 0x36
  46. #define LP5523_REG_CHANNEL1_PC 0x37
  47. #define LP5523_REG_CHANNEL2_PC 0x38
  48. #define LP5523_REG_CHANNEL3_PC 0x39
  49. #define LP5523_REG_STATUS 0x3a
  50. #define LP5523_REG_GPO 0x3b
  51. #define LP5523_REG_VARIABLE 0x3c
  52. #define LP5523_REG_RESET 0x3d
  53. #define LP5523_REG_TEMP_CTRL 0x3e
  54. #define LP5523_REG_TEMP_READ 0x3f
  55. #define LP5523_REG_TEMP_WRITE 0x40
  56. #define LP5523_REG_LED_TEST_CTRL 0x41
  57. #define LP5523_REG_LED_TEST_ADC 0x42
  58. #define LP5523_REG_ENG1_VARIABLE 0x45
  59. #define LP5523_REG_ENG2_VARIABLE 0x46
  60. #define LP5523_REG_ENG3_VARIABLE 0x47
  61. #define LP5523_REG_MASTER_FADER1 0x48
  62. #define LP5523_REG_MASTER_FADER2 0x49
  63. #define LP5523_REG_MASTER_FADER3 0x4a
  64. #define LP5523_REG_CH1_PROG_START 0x4c
  65. #define LP5523_REG_CH2_PROG_START 0x4d
  66. #define LP5523_REG_CH3_PROG_START 0x4e
  67. #define LP5523_REG_PROG_PAGE_SEL 0x4f
  68. #define LP5523_REG_PROG_MEM 0x50
  69. #define LP5523_CMD_LOAD 0x15 /* 00010101 */
  70. #define LP5523_CMD_RUN 0x2a /* 00101010 */
  71. #define LP5523_CMD_DISABLED 0x00 /* 00000000 */
  72. #define LP5523_ENABLE 0x40
  73. #define LP5523_AUTO_INC 0x40
  74. #define LP5523_PWR_SAVE 0x20
  75. #define LP5523_PWM_PWR_SAVE 0x04
  76. #define LP5523_CP_1 0x08
  77. #define LP5523_CP_1_5 0x10
  78. #define LP5523_CP_AUTO 0x18
  79. #define LP5523_INT_CLK 0x01
  80. #define LP5523_AUTO_CLK 0x02
  81. #define LP5523_EN_LEDTEST 0x80
  82. #define LP5523_LEDTEST_DONE 0x80
  83. #define LP5523_DEFAULT_CURRENT 50 /* microAmps */
  84. #define LP5523_PROGRAM_LENGTH 32 /* in bytes */
  85. #define LP5523_PROGRAM_PAGES 6
  86. #define LP5523_ADC_SHORTCIRC_LIM 80
  87. #define LP5523_LEDS 9
  88. #define LP5523_ENGINES 3
  89. #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */
  90. #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */
  91. #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
  92. #define LP5523_EXT_CLK_USED 0x08
  93. #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
  94. #define SHIFT_MASK(id) (((id) - 1) * 2)
  95. struct lp5523_engine {
  96. const struct attribute_group *attributes;
  97. int id;
  98. u8 mode;
  99. u8 prog_page;
  100. u8 mux_page;
  101. u16 led_mux;
  102. u8 engine_mask;
  103. };
  104. struct lp5523_led {
  105. int id;
  106. u8 chan_nr;
  107. u8 led_current;
  108. u8 max_current;
  109. struct led_classdev cdev;
  110. struct work_struct brightness_work;
  111. u8 brightness;
  112. };
  113. struct lp5523_chip {
  114. struct mutex lock; /* Serialize control */
  115. struct i2c_client *client;
  116. struct lp5523_engine engines[LP5523_ENGINES];
  117. struct lp5523_led leds[LP5523_LEDS];
  118. struct lp5523_platform_data *pdata;
  119. u8 num_channels;
  120. u8 num_leds;
  121. };
  122. #define cdev_to_led(c) container_of(c, struct lp5523_led, cdev)
  123. static struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
  124. {
  125. return container_of(engine, struct lp5523_chip,
  126. engines[engine->id - 1]);
  127. }
  128. static struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
  129. {
  130. return container_of(led, struct lp5523_chip,
  131. leds[led->id]);
  132. }
  133. static int lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
  134. static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
  135. static int lp5523_load_program(struct lp5523_engine *engine, u8 *pattern);
  136. static void lp5523_led_brightness_work(struct work_struct *work);
  137. static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
  138. {
  139. return i2c_smbus_write_byte_data(client, reg, value);
  140. }
  141. static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf)
  142. {
  143. s32 ret = i2c_smbus_read_byte_data(client, reg);
  144. if (ret < 0)
  145. return -EIO;
  146. *buf = ret;
  147. return 0;
  148. }
  149. static int lp5523_detect(struct i2c_client *client)
  150. {
  151. int ret;
  152. u8 buf;
  153. ret = lp5523_write(client, LP5523_REG_ENABLE, 0x40);
  154. if (ret)
  155. return ret;
  156. ret = lp5523_read(client, LP5523_REG_ENABLE, &buf);
  157. if (ret)
  158. return ret;
  159. if (buf == 0x40)
  160. return 0;
  161. else
  162. return -ENODEV;
  163. }
  164. static int lp5523_configure(struct i2c_client *client)
  165. {
  166. struct lp5523_chip *chip = i2c_get_clientdata(client);
  167. int ret = 0;
  168. u8 status;
  169. /* one pattern per engine setting led mux start and stop addresses */
  170. u8 pattern[][LP5523_PROGRAM_LENGTH] = {
  171. { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
  172. { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
  173. { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
  174. };
  175. lp5523_write(client, LP5523_REG_RESET, 0xff);
  176. usleep_range(10000, 100000);
  177. ret |= lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
  178. /* Chip startup time after reset is 500 us */
  179. usleep_range(1000, 10000);
  180. ret |= lp5523_write(client, LP5523_REG_CONFIG,
  181. LP5523_AUTO_INC | LP5523_PWR_SAVE |
  182. LP5523_CP_AUTO | LP5523_AUTO_CLK |
  183. LP5523_PWM_PWR_SAVE);
  184. /* turn on all leds */
  185. ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
  186. ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
  187. /* hardcode 32 bytes of memory for each engine from program memory */
  188. ret |= lp5523_write(client, LP5523_REG_CH1_PROG_START, 0x00);
  189. ret |= lp5523_write(client, LP5523_REG_CH2_PROG_START, 0x10);
  190. ret |= lp5523_write(client, LP5523_REG_CH3_PROG_START, 0x20);
  191. /* write led mux address space for each channel */
  192. ret |= lp5523_load_program(&chip->engines[0], pattern[0]);
  193. ret |= lp5523_load_program(&chip->engines[1], pattern[1]);
  194. ret |= lp5523_load_program(&chip->engines[2], pattern[2]);
  195. if (ret) {
  196. dev_err(&client->dev, "could not load mux programs\n");
  197. return -1;
  198. }
  199. /* set all engines exec state and mode to run 00101010 */
  200. ret |= lp5523_write(client, LP5523_REG_ENABLE,
  201. (LP5523_CMD_RUN | LP5523_ENABLE));
  202. ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_RUN);
  203. if (ret) {
  204. dev_err(&client->dev, "could not start mux programs\n");
  205. return -1;
  206. }
  207. /* Wait 3ms and check the engine status */
  208. usleep_range(3000, 20000);
  209. lp5523_read(client, LP5523_REG_STATUS, &status);
  210. status &= LP5523_ENG_STATUS_MASK;
  211. if (status == LP5523_ENG_STATUS_MASK) {
  212. dev_dbg(&client->dev, "all engines configured\n");
  213. } else {
  214. dev_info(&client->dev, "status == %x\n", status);
  215. dev_err(&client->dev, "cound not configure LED engine\n");
  216. return -1;
  217. }
  218. dev_info(&client->dev, "disabling engines\n");
  219. ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
  220. return ret;
  221. }
  222. static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
  223. {
  224. struct lp5523_chip *chip = engine_to_lp5523(engine);
  225. struct i2c_client *client = chip->client;
  226. int ret;
  227. u8 engine_state;
  228. ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
  229. if (ret)
  230. goto fail;
  231. engine_state &= ~(engine->engine_mask);
  232. /* set mode only for this engine */
  233. mode &= engine->engine_mask;
  234. engine_state |= mode;
  235. ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
  236. fail:
  237. return ret;
  238. }
  239. static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
  240. {
  241. struct lp5523_chip *chip = engine_to_lp5523(engine);
  242. struct i2c_client *client = chip->client;
  243. int ret = 0;
  244. ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  245. ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
  246. ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
  247. (u8)(mux >> 8));
  248. ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
  249. engine->led_mux = mux;
  250. return ret;
  251. }
  252. static int lp5523_load_program(struct lp5523_engine *engine, u8 *pattern)
  253. {
  254. struct lp5523_chip *chip = engine_to_lp5523(engine);
  255. struct i2c_client *client = chip->client;
  256. int ret = 0;
  257. ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  258. ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
  259. engine->prog_page);
  260. ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
  261. LP5523_PROGRAM_LENGTH, pattern);
  262. return ret;
  263. }
  264. static int lp5523_run_program(struct lp5523_engine *engine)
  265. {
  266. struct lp5523_chip *chip = engine_to_lp5523(engine);
  267. struct i2c_client *client = chip->client;
  268. int ret;
  269. ret = lp5523_write(client, LP5523_REG_ENABLE,
  270. LP5523_CMD_RUN | LP5523_ENABLE);
  271. if (ret)
  272. goto fail;
  273. ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
  274. fail:
  275. return ret;
  276. }
  277. static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
  278. {
  279. int i;
  280. u16 tmp_mux = 0;
  281. len = len < LP5523_LEDS ? len : LP5523_LEDS;
  282. for (i = 0; i < len; i++) {
  283. switch (buf[i]) {
  284. case '1':
  285. tmp_mux |= (1 << i);
  286. break;
  287. case '0':
  288. break;
  289. case '\n':
  290. i = len;
  291. break;
  292. default:
  293. return -1;
  294. }
  295. }
  296. *mux = tmp_mux;
  297. return 0;
  298. }
  299. static void lp5523_mux_to_array(u16 led_mux, char *array)
  300. {
  301. int i, pos = 0;
  302. for (i = 0; i < LP5523_LEDS; i++)
  303. pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
  304. array[pos] = '\0';
  305. }
  306. /*--------------------------------------------------------------*/
  307. /* Sysfs interface */
  308. /*--------------------------------------------------------------*/
  309. static ssize_t show_engine_leds(struct device *dev,
  310. struct device_attribute *attr,
  311. char *buf, int nr)
  312. {
  313. struct i2c_client *client = to_i2c_client(dev);
  314. struct lp5523_chip *chip = i2c_get_clientdata(client);
  315. char mux[LP5523_LEDS + 1];
  316. lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
  317. return sprintf(buf, "%s\n", mux);
  318. }
  319. #define show_leds(nr) \
  320. static ssize_t show_engine##nr##_leds(struct device *dev, \
  321. struct device_attribute *attr, \
  322. char *buf) \
  323. { \
  324. return show_engine_leds(dev, attr, buf, nr); \
  325. }
  326. show_leds(1)
  327. show_leds(2)
  328. show_leds(3)
  329. static ssize_t store_engine_leds(struct device *dev,
  330. struct device_attribute *attr,
  331. const char *buf, size_t len, int nr)
  332. {
  333. struct i2c_client *client = to_i2c_client(dev);
  334. struct lp5523_chip *chip = i2c_get_clientdata(client);
  335. u16 mux = 0;
  336. if (lp5523_mux_parse(buf, &mux, len))
  337. return -EINVAL;
  338. if (lp5523_load_mux(&chip->engines[nr - 1], mux))
  339. return -EINVAL;
  340. return len;
  341. }
  342. #define store_leds(nr) \
  343. static ssize_t store_engine##nr##_leds(struct device *dev, \
  344. struct device_attribute *attr, \
  345. const char *buf, size_t len) \
  346. { \
  347. return store_engine_leds(dev, attr, buf, len, nr); \
  348. }
  349. store_leds(1)
  350. store_leds(2)
  351. store_leds(3)
  352. static ssize_t lp5523_selftest(struct device *dev,
  353. struct device_attribute *attr,
  354. char *buf)
  355. {
  356. struct i2c_client *client = to_i2c_client(dev);
  357. struct lp5523_chip *chip = i2c_get_clientdata(client);
  358. int i, ret, pos = 0;
  359. int led = 0;
  360. u8 status, adc, vdd;
  361. mutex_lock(&chip->lock);
  362. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  363. if (ret < 0)
  364. goto fail;
  365. /* Check that ext clock is really in use if requested */
  366. if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT))
  367. if ((status & LP5523_EXT_CLK_USED) == 0)
  368. goto fail;
  369. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  370. lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL,
  371. LP5523_EN_LEDTEST | 16);
  372. usleep_range(3000, 10000);
  373. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  374. if (!(status & LP5523_LEDTEST_DONE))
  375. usleep_range(3000, 10000);
  376. ret |= lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
  377. vdd--; /* There may be some fluctuation in measurement */
  378. for (i = 0; i < LP5523_LEDS; i++) {
  379. /* Skip non-existing channels */
  380. if (chip->pdata->led_config[i].led_current == 0)
  381. continue;
  382. /* Set default current */
  383. lp5523_write(chip->client,
  384. LP5523_REG_LED_CURRENT_BASE + i,
  385. chip->pdata->led_config[i].led_current);
  386. lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff);
  387. /* let current stabilize 2ms before measurements start */
  388. usleep_range(2000, 10000);
  389. lp5523_write(chip->client,
  390. LP5523_REG_LED_TEST_CTRL,
  391. LP5523_EN_LEDTEST | i);
  392. /* ledtest takes 2.7ms */
  393. usleep_range(3000, 10000);
  394. ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
  395. if (!(status & LP5523_LEDTEST_DONE))
  396. usleep_range(3000, 10000);
  397. ret |= lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
  398. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  399. pos += sprintf(buf + pos, "LED %d FAIL\n", i);
  400. lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
  401. /* Restore current */
  402. lp5523_write(chip->client,
  403. LP5523_REG_LED_CURRENT_BASE + i,
  404. chip->leds[led].led_current);
  405. led++;
  406. }
  407. if (pos == 0)
  408. pos = sprintf(buf, "OK\n");
  409. goto release_lock;
  410. fail:
  411. pos = sprintf(buf, "FAIL\n");
  412. release_lock:
  413. mutex_unlock(&chip->lock);
  414. return pos;
  415. }
  416. static void lp5523_set_brightness(struct led_classdev *cdev,
  417. enum led_brightness brightness)
  418. {
  419. struct lp5523_led *led = cdev_to_led(cdev);
  420. led->brightness = (u8)brightness;
  421. schedule_work(&led->brightness_work);
  422. }
  423. static void lp5523_led_brightness_work(struct work_struct *work)
  424. {
  425. struct lp5523_led *led = container_of(work,
  426. struct lp5523_led,
  427. brightness_work);
  428. struct lp5523_chip *chip = led_to_lp5523(led);
  429. struct i2c_client *client = chip->client;
  430. mutex_lock(&chip->lock);
  431. lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  432. led->brightness);
  433. mutex_unlock(&chip->lock);
  434. }
  435. static int lp5523_do_store_load(struct lp5523_engine *engine,
  436. const char *buf, size_t len)
  437. {
  438. struct lp5523_chip *chip = engine_to_lp5523(engine);
  439. struct i2c_client *client = chip->client;
  440. int ret, nrchars, offset = 0, i = 0;
  441. char c[3];
  442. unsigned cmd;
  443. u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
  444. while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
  445. /* separate sscanfs because length is working only for %s */
  446. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  447. ret = sscanf(c, "%2x", &cmd);
  448. if (ret != 1)
  449. goto fail;
  450. pattern[i] = (u8)cmd;
  451. offset += nrchars;
  452. i++;
  453. }
  454. /* Each instruction is 16bit long. Check that length is even */
  455. if (i % 2)
  456. goto fail;
  457. mutex_lock(&chip->lock);
  458. ret = lp5523_load_program(engine, pattern);
  459. mutex_unlock(&chip->lock);
  460. if (ret) {
  461. dev_err(&client->dev, "failed loading pattern\n");
  462. return ret;
  463. }
  464. return len;
  465. fail:
  466. dev_err(&client->dev, "wrong pattern format\n");
  467. return -EINVAL;
  468. }
  469. static ssize_t store_engine_load(struct device *dev,
  470. struct device_attribute *attr,
  471. const char *buf, size_t len, int nr)
  472. {
  473. struct i2c_client *client = to_i2c_client(dev);
  474. struct lp5523_chip *chip = i2c_get_clientdata(client);
  475. return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
  476. }
  477. #define store_load(nr) \
  478. static ssize_t store_engine##nr##_load(struct device *dev, \
  479. struct device_attribute *attr, \
  480. const char *buf, size_t len) \
  481. { \
  482. return store_engine_load(dev, attr, buf, len, nr); \
  483. }
  484. store_load(1)
  485. store_load(2)
  486. store_load(3)
  487. static ssize_t show_engine_mode(struct device *dev,
  488. struct device_attribute *attr,
  489. char *buf, int nr)
  490. {
  491. struct i2c_client *client = to_i2c_client(dev);
  492. struct lp5523_chip *chip = i2c_get_clientdata(client);
  493. switch (chip->engines[nr - 1].mode) {
  494. case LP5523_CMD_RUN:
  495. return sprintf(buf, "run\n");
  496. case LP5523_CMD_LOAD:
  497. return sprintf(buf, "load\n");
  498. case LP5523_CMD_DISABLED:
  499. return sprintf(buf, "disabled\n");
  500. default:
  501. return sprintf(buf, "disabled\n");
  502. }
  503. }
  504. #define show_mode(nr) \
  505. static ssize_t show_engine##nr##_mode(struct device *dev, \
  506. struct device_attribute *attr, \
  507. char *buf) \
  508. { \
  509. return show_engine_mode(dev, attr, buf, nr); \
  510. }
  511. show_mode(1)
  512. show_mode(2)
  513. show_mode(3)
  514. static ssize_t store_engine_mode(struct device *dev,
  515. struct device_attribute *attr,
  516. const char *buf, size_t len, int nr)
  517. {
  518. struct i2c_client *client = to_i2c_client(dev);
  519. struct lp5523_chip *chip = i2c_get_clientdata(client);
  520. struct lp5523_engine *engine = &chip->engines[nr - 1];
  521. mutex_lock(&chip->lock);
  522. if (!strncmp(buf, "run", 3))
  523. lp5523_set_mode(engine, LP5523_CMD_RUN);
  524. else if (!strncmp(buf, "load", 4))
  525. lp5523_set_mode(engine, LP5523_CMD_LOAD);
  526. else if (!strncmp(buf, "disabled", 8))
  527. lp5523_set_mode(engine, LP5523_CMD_DISABLED);
  528. mutex_unlock(&chip->lock);
  529. return len;
  530. }
  531. #define store_mode(nr) \
  532. static ssize_t store_engine##nr##_mode(struct device *dev, \
  533. struct device_attribute *attr, \
  534. const char *buf, size_t len) \
  535. { \
  536. return store_engine_mode(dev, attr, buf, len, nr); \
  537. }
  538. store_mode(1)
  539. store_mode(2)
  540. store_mode(3)
  541. static ssize_t show_max_current(struct device *dev,
  542. struct device_attribute *attr,
  543. char *buf)
  544. {
  545. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  546. struct lp5523_led *led = cdev_to_led(led_cdev);
  547. return sprintf(buf, "%d\n", led->max_current);
  548. }
  549. static ssize_t show_current(struct device *dev,
  550. struct device_attribute *attr,
  551. char *buf)
  552. {
  553. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  554. struct lp5523_led *led = cdev_to_led(led_cdev);
  555. return sprintf(buf, "%d\n", led->led_current);
  556. }
  557. static ssize_t store_current(struct device *dev,
  558. struct device_attribute *attr,
  559. const char *buf, size_t len)
  560. {
  561. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  562. struct lp5523_led *led = cdev_to_led(led_cdev);
  563. struct lp5523_chip *chip = led_to_lp5523(led);
  564. ssize_t ret;
  565. unsigned long curr;
  566. if (strict_strtoul(buf, 0, &curr))
  567. return -EINVAL;
  568. if (curr > led->max_current)
  569. return -EINVAL;
  570. mutex_lock(&chip->lock);
  571. ret = lp5523_write(chip->client,
  572. LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  573. (u8)curr);
  574. mutex_unlock(&chip->lock);
  575. if (ret < 0)
  576. return ret;
  577. led->led_current = (u8)curr;
  578. return len;
  579. }
  580. /* led class device attributes */
  581. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUGO, show_current, store_current);
  582. static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
  583. static struct attribute *lp5523_led_attributes[] = {
  584. &dev_attr_led_current.attr,
  585. &dev_attr_max_current.attr,
  586. NULL,
  587. };
  588. static struct attribute_group lp5523_led_attribute_group = {
  589. .attrs = lp5523_led_attributes
  590. };
  591. /* device attributes */
  592. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUGO,
  593. show_engine1_mode, store_engine1_mode);
  594. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUGO,
  595. show_engine2_mode, store_engine2_mode);
  596. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUGO,
  597. show_engine3_mode, store_engine3_mode);
  598. static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUGO,
  599. show_engine1_leds, store_engine1_leds);
  600. static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUGO,
  601. show_engine2_leds, store_engine2_leds);
  602. static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUGO,
  603. show_engine3_leds, store_engine3_leds);
  604. static DEVICE_ATTR(engine1_load, S_IWUGO, NULL, store_engine1_load);
  605. static DEVICE_ATTR(engine2_load, S_IWUGO, NULL, store_engine2_load);
  606. static DEVICE_ATTR(engine3_load, S_IWUGO, NULL, store_engine3_load);
  607. static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
  608. static struct attribute *lp5523_attributes[] = {
  609. &dev_attr_engine1_mode.attr,
  610. &dev_attr_engine2_mode.attr,
  611. &dev_attr_engine3_mode.attr,
  612. &dev_attr_selftest.attr,
  613. NULL
  614. };
  615. static struct attribute *lp5523_engine1_attributes[] = {
  616. &dev_attr_engine1_load.attr,
  617. &dev_attr_engine1_leds.attr,
  618. NULL
  619. };
  620. static struct attribute *lp5523_engine2_attributes[] = {
  621. &dev_attr_engine2_load.attr,
  622. &dev_attr_engine2_leds.attr,
  623. NULL
  624. };
  625. static struct attribute *lp5523_engine3_attributes[] = {
  626. &dev_attr_engine3_load.attr,
  627. &dev_attr_engine3_leds.attr,
  628. NULL
  629. };
  630. static const struct attribute_group lp5523_group = {
  631. .attrs = lp5523_attributes,
  632. };
  633. static const struct attribute_group lp5523_engine_group[] = {
  634. {.attrs = lp5523_engine1_attributes },
  635. {.attrs = lp5523_engine2_attributes },
  636. {.attrs = lp5523_engine3_attributes },
  637. };
  638. static int lp5523_register_sysfs(struct i2c_client *client)
  639. {
  640. struct device *dev = &client->dev;
  641. int ret;
  642. ret = sysfs_create_group(&dev->kobj, &lp5523_group);
  643. if (ret < 0)
  644. return ret;
  645. return 0;
  646. }
  647. static void lp5523_unregister_sysfs(struct i2c_client *client)
  648. {
  649. struct lp5523_chip *chip = i2c_get_clientdata(client);
  650. struct device *dev = &client->dev;
  651. int i;
  652. sysfs_remove_group(&dev->kobj, &lp5523_group);
  653. for (i = 0; i < ARRAY_SIZE(chip->engines); i++)
  654. if (chip->engines[i].mode == LP5523_CMD_LOAD)
  655. sysfs_remove_group(&dev->kobj, &lp5523_engine_group[i]);
  656. for (i = 0; i < chip->num_leds; i++)
  657. sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
  658. &lp5523_led_attribute_group);
  659. }
  660. /*--------------------------------------------------------------*/
  661. /* Set chip operating mode */
  662. /*--------------------------------------------------------------*/
  663. static int lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
  664. {
  665. /* engine to chip */
  666. struct lp5523_chip *chip = engine_to_lp5523(engine);
  667. struct i2c_client *client = chip->client;
  668. struct device *dev = &client->dev;
  669. int ret = 0;
  670. /* if in that mode already do nothing, except for run */
  671. if (mode == engine->mode && mode != LP5523_CMD_RUN)
  672. return 0;
  673. if (mode == LP5523_CMD_RUN) {
  674. ret = lp5523_run_program(engine);
  675. } else if (mode == LP5523_CMD_LOAD) {
  676. lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
  677. lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
  678. ret = sysfs_create_group(&dev->kobj, engine->attributes);
  679. if (ret)
  680. return ret;
  681. } else if (mode == LP5523_CMD_DISABLED) {
  682. lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
  683. }
  684. /* remove load attribute from sysfs if not in load mode */
  685. if (engine->mode == LP5523_CMD_LOAD && mode != LP5523_CMD_LOAD)
  686. sysfs_remove_group(&dev->kobj, engine->attributes);
  687. engine->mode = mode;
  688. return ret;
  689. }
  690. /*--------------------------------------------------------------*/
  691. /* Probe, Attach, Remove */
  692. /*--------------------------------------------------------------*/
  693. static int __init lp5523_init_engine(struct lp5523_engine *engine, int id)
  694. {
  695. if (id < 1 || id > LP5523_ENGINES)
  696. return -1;
  697. engine->id = id;
  698. engine->engine_mask = LP5523_ENG_MASK_BASE >> SHIFT_MASK(id);
  699. engine->prog_page = id - 1;
  700. engine->mux_page = id + 2;
  701. engine->attributes = &lp5523_engine_group[id - 1];
  702. return 0;
  703. }
  704. static int __init lp5523_init_led(struct lp5523_led *led, struct device *dev,
  705. int chan, struct lp5523_platform_data *pdata)
  706. {
  707. char name[32];
  708. int res;
  709. if (chan >= LP5523_LEDS)
  710. return -EINVAL;
  711. if (pdata->led_config[chan].led_current) {
  712. led->led_current = pdata->led_config[chan].led_current;
  713. led->max_current = pdata->led_config[chan].max_current;
  714. led->chan_nr = pdata->led_config[chan].chan_nr;
  715. if (led->chan_nr >= LP5523_LEDS) {
  716. dev_err(dev, "Use channel numbers between 0 and %d\n",
  717. LP5523_LEDS - 1);
  718. return -EINVAL;
  719. }
  720. snprintf(name, 32, "lp5523:channel%d", chan);
  721. led->cdev.name = name;
  722. led->cdev.brightness_set = lp5523_set_brightness;
  723. res = led_classdev_register(dev, &led->cdev);
  724. if (res < 0) {
  725. dev_err(dev, "couldn't register led on channel %d\n",
  726. chan);
  727. return res;
  728. }
  729. res = sysfs_create_group(&led->cdev.dev->kobj,
  730. &lp5523_led_attribute_group);
  731. if (res < 0) {
  732. dev_err(dev, "couldn't register current attribute\n");
  733. led_classdev_unregister(&led->cdev);
  734. return res;
  735. }
  736. } else {
  737. led->led_current = 0;
  738. }
  739. return 0;
  740. }
  741. static struct i2c_driver lp5523_driver;
  742. static int lp5523_probe(struct i2c_client *client,
  743. const struct i2c_device_id *id)
  744. {
  745. struct lp5523_chip *chip;
  746. struct lp5523_platform_data *pdata;
  747. int ret, i, led;
  748. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  749. if (!chip)
  750. return -ENOMEM;
  751. i2c_set_clientdata(client, chip);
  752. chip->client = client;
  753. pdata = client->dev.platform_data;
  754. if (!pdata) {
  755. dev_err(&client->dev, "no platform data\n");
  756. ret = -EINVAL;
  757. goto fail1;
  758. }
  759. mutex_init(&chip->lock);
  760. chip->pdata = pdata;
  761. if (pdata->setup_resources) {
  762. ret = pdata->setup_resources();
  763. if (ret < 0)
  764. goto fail1;
  765. }
  766. if (pdata->enable) {
  767. pdata->enable(0);
  768. usleep_range(1000, 10000);
  769. pdata->enable(1);
  770. usleep_range(1000, 10000); /* Spec says min 500us */
  771. }
  772. ret = lp5523_detect(client);
  773. if (ret)
  774. goto fail2;
  775. dev_info(&client->dev, "LP5523 Programmable led chip found\n");
  776. /* Initialize engines */
  777. for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
  778. ret = lp5523_init_engine(&chip->engines[i], i + 1);
  779. if (ret) {
  780. dev_err(&client->dev, "error initializing engine\n");
  781. goto fail2;
  782. }
  783. }
  784. ret = lp5523_configure(client);
  785. if (ret < 0) {
  786. dev_err(&client->dev, "error configuring chip\n");
  787. goto fail2;
  788. }
  789. /* Initialize leds */
  790. chip->num_channels = pdata->num_channels;
  791. chip->num_leds = 0;
  792. led = 0;
  793. for (i = 0; i < pdata->num_channels; i++) {
  794. /* Do not initialize channels that are not connected */
  795. if (pdata->led_config[i].led_current == 0)
  796. continue;
  797. ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata);
  798. if (ret) {
  799. dev_err(&client->dev, "error initializing leds\n");
  800. goto fail3;
  801. }
  802. chip->num_leds++;
  803. chip->leds[led].id = led;
  804. /* Set LED current */
  805. lp5523_write(client,
  806. LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
  807. chip->leds[led].led_current);
  808. INIT_WORK(&(chip->leds[led].brightness_work),
  809. lp5523_led_brightness_work);
  810. led++;
  811. }
  812. ret = lp5523_register_sysfs(client);
  813. if (ret) {
  814. dev_err(&client->dev, "registering sysfs failed\n");
  815. goto fail3;
  816. }
  817. return ret;
  818. fail3:
  819. for (i = 0; i < chip->num_leds; i++) {
  820. led_classdev_unregister(&chip->leds[i].cdev);
  821. cancel_work_sync(&chip->leds[i].brightness_work);
  822. }
  823. fail2:
  824. if (pdata->enable)
  825. pdata->enable(0);
  826. if (pdata->release_resources)
  827. pdata->release_resources();
  828. fail1:
  829. kfree(chip);
  830. return ret;
  831. }
  832. static int lp5523_remove(struct i2c_client *client)
  833. {
  834. struct lp5523_chip *chip = i2c_get_clientdata(client);
  835. int i;
  836. lp5523_unregister_sysfs(client);
  837. for (i = 0; i < chip->num_leds; i++) {
  838. led_classdev_unregister(&chip->leds[i].cdev);
  839. cancel_work_sync(&chip->leds[i].brightness_work);
  840. }
  841. if (chip->pdata->enable)
  842. chip->pdata->enable(0);
  843. if (chip->pdata->release_resources)
  844. chip->pdata->release_resources();
  845. kfree(chip);
  846. return 0;
  847. }
  848. static const struct i2c_device_id lp5523_id[] = {
  849. { "lp5523", 0 },
  850. { }
  851. };
  852. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  853. static struct i2c_driver lp5523_driver = {
  854. .driver = {
  855. .name = "lp5523",
  856. },
  857. .probe = lp5523_probe,
  858. .remove = lp5523_remove,
  859. .id_table = lp5523_id,
  860. };
  861. static int __init lp5523_init(void)
  862. {
  863. int ret;
  864. ret = i2c_add_driver(&lp5523_driver);
  865. if (ret < 0)
  866. printk(KERN_ALERT "Adding lp5523 driver failed\n");
  867. return ret;
  868. }
  869. static void __exit lp5523_exit(void)
  870. {
  871. i2c_del_driver(&lp5523_driver);
  872. }
  873. module_init(lp5523_init);
  874. module_exit(lp5523_exit);
  875. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  876. MODULE_DESCRIPTION("LP5523 LED engine");
  877. MODULE_LICENSE("GPL");