leds-lp5523.c 26 KB

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