leds-lp5523.c 28 KB

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