leds-lp5523.c 27 KB

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