leds-lp5523.c 26 KB

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