leds-lp5523.c 26 KB

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