leds-lp5523.c 27 KB

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