leds-lp5523.c 27 KB

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