leds-lp5521.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * LP5521 LED chip 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-lp5521.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 LP5521_PROGRAM_LENGTH 32 /* in bytes */
  39. #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
  40. #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
  41. #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
  42. #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
  43. #define LP5521_CMD_LOAD 0x15 /* 00010101 */
  44. #define LP5521_CMD_RUN 0x2a /* 00101010 */
  45. #define LP5521_CMD_DIRECT 0x3f /* 00111111 */
  46. #define LP5521_CMD_DISABLED 0x00 /* 00000000 */
  47. /* Registers */
  48. #define LP5521_REG_ENABLE 0x00
  49. #define LP5521_REG_OP_MODE 0x01
  50. #define LP5521_REG_R_PWM 0x02
  51. #define LP5521_REG_G_PWM 0x03
  52. #define LP5521_REG_B_PWM 0x04
  53. #define LP5521_REG_R_CURRENT 0x05
  54. #define LP5521_REG_G_CURRENT 0x06
  55. #define LP5521_REG_B_CURRENT 0x07
  56. #define LP5521_REG_CONFIG 0x08
  57. #define LP5521_REG_R_CHANNEL_PC 0x09
  58. #define LP5521_REG_G_CHANNEL_PC 0x0A
  59. #define LP5521_REG_B_CHANNEL_PC 0x0B
  60. #define LP5521_REG_STATUS 0x0C
  61. #define LP5521_REG_RESET 0x0D
  62. #define LP5521_REG_GPO 0x0E
  63. #define LP5521_REG_R_PROG_MEM 0x10
  64. #define LP5521_REG_G_PROG_MEM 0x30
  65. #define LP5521_REG_B_PROG_MEM 0x50
  66. #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
  67. #define LP5521_PROG_MEM_SIZE 0x20
  68. /* Base register to set LED current */
  69. #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
  70. /* Base register to set the brightness */
  71. #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
  72. /* Bits in ENABLE register */
  73. #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
  74. #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  75. #define LP5521_EXEC_RUN 0x2A
  76. #define LP5521_ENABLE_DEFAULT \
  77. (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
  78. #define LP5521_ENABLE_RUN_PROGRAM \
  79. (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
  80. /* Status */
  81. #define LP5521_EXT_CLK_USED 0x08
  82. /* default R channel current register value */
  83. #define LP5521_REG_R_CURR_DEFAULT 0xAF
  84. /* Pattern Mode */
  85. #define PATTERN_OFF 0
  86. /* Reset register value */
  87. #define LP5521_RESET 0xFF
  88. struct lp5521_engine {
  89. int id;
  90. u8 mode;
  91. u8 prog_page;
  92. u8 engine_mask;
  93. };
  94. struct lp5521_led {
  95. int id;
  96. u8 chan_nr;
  97. u8 led_current;
  98. u8 max_current;
  99. struct led_classdev cdev;
  100. struct work_struct brightness_work;
  101. u8 brightness;
  102. };
  103. struct lp5521_chip {
  104. struct lp5521_platform_data *pdata;
  105. struct mutex lock; /* Serialize control */
  106. struct i2c_client *client;
  107. struct lp5521_engine engines[LP5521_MAX_ENGINES];
  108. struct lp5521_led leds[LP5521_MAX_LEDS];
  109. u8 num_channels;
  110. u8 num_leds;
  111. };
  112. static inline void lp5521_wait_enable_done(void)
  113. {
  114. /* it takes more 488 us to update ENABLE register */
  115. usleep_range(500, 600);
  116. }
  117. static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
  118. {
  119. return container_of(cdev, struct lp5521_led, cdev);
  120. }
  121. static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
  122. {
  123. return container_of(engine, struct lp5521_chip,
  124. engines[engine->id - 1]);
  125. }
  126. static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
  127. {
  128. return container_of(led, struct lp5521_chip,
  129. leds[led->id]);
  130. }
  131. static void lp5521_led_brightness_work(struct work_struct *work);
  132. static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
  133. {
  134. return i2c_smbus_write_byte_data(client, reg, value);
  135. }
  136. static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
  137. {
  138. s32 ret;
  139. ret = i2c_smbus_read_byte_data(client, reg);
  140. if (ret < 0)
  141. return ret;
  142. *buf = ret;
  143. return 0;
  144. }
  145. static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
  146. {
  147. struct lp5521_chip *chip = engine_to_lp5521(engine);
  148. struct i2c_client *client = chip->client;
  149. int ret;
  150. u8 engine_state;
  151. /* Only transition between RUN and DIRECT mode are handled here */
  152. if (mode == LP5521_CMD_LOAD)
  153. return 0;
  154. if (mode == LP5521_CMD_DISABLED)
  155. mode = LP5521_CMD_DIRECT;
  156. ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
  157. if (ret < 0)
  158. return ret;
  159. /* set mode only for this engine */
  160. engine_state &= ~(engine->engine_mask);
  161. mode &= engine->engine_mask;
  162. engine_state |= mode;
  163. return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
  164. }
  165. static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
  166. {
  167. struct lp5521_chip *chip = engine_to_lp5521(eng);
  168. struct i2c_client *client = chip->client;
  169. int ret;
  170. int addr;
  171. u8 mode;
  172. /* move current engine to direct mode and remember the state */
  173. ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
  174. if (ret)
  175. return ret;
  176. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  177. usleep_range(1000, 2000);
  178. ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode);
  179. if (ret)
  180. return ret;
  181. /* For loading, all the engines to load mode */
  182. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  183. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  184. usleep_range(1000, 2000);
  185. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
  186. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  187. usleep_range(1000, 2000);
  188. addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
  189. i2c_smbus_write_i2c_block_data(client,
  190. addr,
  191. LP5521_PROG_MEM_SIZE,
  192. pattern);
  193. return lp5521_write(client, LP5521_REG_OP_MODE, mode);
  194. }
  195. static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
  196. {
  197. return lp5521_write(chip->client,
  198. LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
  199. curr);
  200. }
  201. static int lp5521_configure(struct i2c_client *client)
  202. {
  203. struct lp5521_chip *chip = i2c_get_clientdata(client);
  204. int ret;
  205. u8 cfg;
  206. u8 val;
  207. /*
  208. * Make sure that the chip is reset by reading back the r channel
  209. * current reg. This is dummy read is required on some platforms -
  210. * otherwise further access to the R G B channels in the
  211. * LP5521_REG_ENABLE register will not have any effect - strange!
  212. */
  213. ret = lp5521_read(client, LP5521_REG_R_CURRENT, &val);
  214. if (ret) {
  215. dev_err(&client->dev, "error in resetting chip\n");
  216. return ret;
  217. }
  218. if (val != LP5521_REG_R_CURR_DEFAULT) {
  219. dev_err(&client->dev,
  220. "unexpected data in register (expected 0x%x got 0x%x)\n",
  221. LP5521_REG_R_CURR_DEFAULT, val);
  222. ret = -EINVAL;
  223. return ret;
  224. }
  225. usleep_range(10000, 20000);
  226. /* Set all PWMs to direct control mode */
  227. ret = lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  228. cfg = chip->pdata->update_config ?
  229. : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
  230. ret = lp5521_write(client, LP5521_REG_CONFIG, cfg);
  231. if (ret)
  232. return ret;
  233. /* Initialize all channels PWM to zero -> leds off */
  234. lp5521_write(client, LP5521_REG_R_PWM, 0);
  235. lp5521_write(client, LP5521_REG_G_PWM, 0);
  236. lp5521_write(client, LP5521_REG_B_PWM, 0);
  237. /* Set engines are set to run state when OP_MODE enables engines */
  238. ret = lp5521_write(client, LP5521_REG_ENABLE,
  239. LP5521_ENABLE_RUN_PROGRAM);
  240. if (ret)
  241. return ret;
  242. lp5521_wait_enable_done();
  243. return 0;
  244. }
  245. static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
  246. {
  247. int ret;
  248. u8 status;
  249. ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
  250. if (ret < 0)
  251. return ret;
  252. /* Check that ext clock is really in use if requested */
  253. if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
  254. if ((status & LP5521_EXT_CLK_USED) == 0)
  255. return -EIO;
  256. return 0;
  257. }
  258. static void lp5521_set_brightness(struct led_classdev *cdev,
  259. enum led_brightness brightness)
  260. {
  261. struct lp5521_led *led = cdev_to_led(cdev);
  262. led->brightness = (u8)brightness;
  263. schedule_work(&led->brightness_work);
  264. }
  265. static void lp5521_led_brightness_work(struct work_struct *work)
  266. {
  267. struct lp5521_led *led = container_of(work,
  268. struct lp5521_led,
  269. brightness_work);
  270. struct lp5521_chip *chip = led_to_lp5521(led);
  271. struct i2c_client *client = chip->client;
  272. mutex_lock(&chip->lock);
  273. lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
  274. led->brightness);
  275. mutex_unlock(&chip->lock);
  276. }
  277. /* Set engine mode and create appropriate sysfs attributes, if required. */
  278. static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
  279. {
  280. int ret = 0;
  281. /* if in that mode already do nothing, except for run */
  282. if (mode == engine->mode && mode != LP5521_CMD_RUN)
  283. return 0;
  284. if (mode == LP5521_CMD_RUN) {
  285. ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
  286. } else if (mode == LP5521_CMD_LOAD) {
  287. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  288. lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
  289. } else if (mode == LP5521_CMD_DISABLED) {
  290. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  291. }
  292. engine->mode = mode;
  293. return ret;
  294. }
  295. static int lp5521_do_store_load(struct lp5521_engine *engine,
  296. const char *buf, size_t len)
  297. {
  298. struct lp5521_chip *chip = engine_to_lp5521(engine);
  299. struct i2c_client *client = chip->client;
  300. int ret, nrchars, offset = 0, i = 0;
  301. char c[3];
  302. unsigned cmd;
  303. u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
  304. while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
  305. /* separate sscanfs because length is working only for %s */
  306. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  307. if (ret != 2)
  308. goto fail;
  309. ret = sscanf(c, "%2x", &cmd);
  310. if (ret != 1)
  311. goto fail;
  312. pattern[i] = (u8)cmd;
  313. offset += nrchars;
  314. i++;
  315. }
  316. /* Each instruction is 16bit long. Check that length is even */
  317. if (i % 2)
  318. goto fail;
  319. mutex_lock(&chip->lock);
  320. if (engine->mode == LP5521_CMD_LOAD)
  321. ret = lp5521_load_program(engine, pattern);
  322. else
  323. ret = -EINVAL;
  324. mutex_unlock(&chip->lock);
  325. if (ret) {
  326. dev_err(&client->dev, "failed loading pattern\n");
  327. return ret;
  328. }
  329. return len;
  330. fail:
  331. dev_err(&client->dev, "wrong pattern format\n");
  332. return -EINVAL;
  333. }
  334. static ssize_t store_engine_load(struct device *dev,
  335. struct device_attribute *attr,
  336. const char *buf, size_t len, int nr)
  337. {
  338. struct i2c_client *client = to_i2c_client(dev);
  339. struct lp5521_chip *chip = i2c_get_clientdata(client);
  340. return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
  341. }
  342. #define store_load(nr) \
  343. static ssize_t store_engine##nr##_load(struct device *dev, \
  344. struct device_attribute *attr, \
  345. const char *buf, size_t len) \
  346. { \
  347. return store_engine_load(dev, attr, buf, len, nr); \
  348. }
  349. store_load(1)
  350. store_load(2)
  351. store_load(3)
  352. static ssize_t show_engine_mode(struct device *dev,
  353. struct device_attribute *attr,
  354. char *buf, int nr)
  355. {
  356. struct i2c_client *client = to_i2c_client(dev);
  357. struct lp5521_chip *chip = i2c_get_clientdata(client);
  358. switch (chip->engines[nr - 1].mode) {
  359. case LP5521_CMD_RUN:
  360. return sprintf(buf, "run\n");
  361. case LP5521_CMD_LOAD:
  362. return sprintf(buf, "load\n");
  363. case LP5521_CMD_DISABLED:
  364. return sprintf(buf, "disabled\n");
  365. default:
  366. return sprintf(buf, "disabled\n");
  367. }
  368. }
  369. #define show_mode(nr) \
  370. static ssize_t show_engine##nr##_mode(struct device *dev, \
  371. struct device_attribute *attr, \
  372. char *buf) \
  373. { \
  374. return show_engine_mode(dev, attr, buf, nr); \
  375. }
  376. show_mode(1)
  377. show_mode(2)
  378. show_mode(3)
  379. static ssize_t store_engine_mode(struct device *dev,
  380. struct device_attribute *attr,
  381. const char *buf, size_t len, int nr)
  382. {
  383. struct i2c_client *client = to_i2c_client(dev);
  384. struct lp5521_chip *chip = i2c_get_clientdata(client);
  385. struct lp5521_engine *engine = &chip->engines[nr - 1];
  386. mutex_lock(&chip->lock);
  387. if (!strncmp(buf, "run", 3))
  388. lp5521_set_mode(engine, LP5521_CMD_RUN);
  389. else if (!strncmp(buf, "load", 4))
  390. lp5521_set_mode(engine, LP5521_CMD_LOAD);
  391. else if (!strncmp(buf, "disabled", 8))
  392. lp5521_set_mode(engine, LP5521_CMD_DISABLED);
  393. mutex_unlock(&chip->lock);
  394. return len;
  395. }
  396. #define store_mode(nr) \
  397. static ssize_t store_engine##nr##_mode(struct device *dev, \
  398. struct device_attribute *attr, \
  399. const char *buf, size_t len) \
  400. { \
  401. return store_engine_mode(dev, attr, buf, len, nr); \
  402. }
  403. store_mode(1)
  404. store_mode(2)
  405. store_mode(3)
  406. static ssize_t show_max_current(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  411. struct lp5521_led *led = cdev_to_led(led_cdev);
  412. return sprintf(buf, "%d\n", led->max_current);
  413. }
  414. static ssize_t show_current(struct device *dev,
  415. struct device_attribute *attr,
  416. char *buf)
  417. {
  418. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  419. struct lp5521_led *led = cdev_to_led(led_cdev);
  420. return sprintf(buf, "%d\n", led->led_current);
  421. }
  422. static ssize_t store_current(struct device *dev,
  423. struct device_attribute *attr,
  424. const char *buf, size_t len)
  425. {
  426. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  427. struct lp5521_led *led = cdev_to_led(led_cdev);
  428. struct lp5521_chip *chip = led_to_lp5521(led);
  429. ssize_t ret;
  430. unsigned long curr;
  431. if (kstrtoul(buf, 0, &curr))
  432. return -EINVAL;
  433. if (curr > led->max_current)
  434. return -EINVAL;
  435. mutex_lock(&chip->lock);
  436. ret = lp5521_set_led_current(chip, led->id, curr);
  437. mutex_unlock(&chip->lock);
  438. if (ret < 0)
  439. return ret;
  440. led->led_current = (u8)curr;
  441. return len;
  442. }
  443. static ssize_t lp5521_selftest(struct device *dev,
  444. struct device_attribute *attr,
  445. char *buf)
  446. {
  447. struct i2c_client *client = to_i2c_client(dev);
  448. struct lp5521_chip *chip = i2c_get_clientdata(client);
  449. int ret;
  450. mutex_lock(&chip->lock);
  451. ret = lp5521_run_selftest(chip, buf);
  452. mutex_unlock(&chip->lock);
  453. return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
  454. }
  455. static void lp5521_clear_program_memory(struct i2c_client *cl)
  456. {
  457. int i;
  458. u8 rgb_mem[] = {
  459. LP5521_REG_R_PROG_MEM,
  460. LP5521_REG_G_PROG_MEM,
  461. LP5521_REG_B_PROG_MEM,
  462. };
  463. for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
  464. lp5521_write(cl, rgb_mem[i], 0);
  465. lp5521_write(cl, rgb_mem[i] + 1, 0);
  466. }
  467. }
  468. static void lp5521_write_program_memory(struct i2c_client *cl,
  469. u8 base, u8 *rgb, int size)
  470. {
  471. int i;
  472. if (!rgb || size <= 0)
  473. return;
  474. for (i = 0; i < size; i++)
  475. lp5521_write(cl, base + i, *(rgb + i));
  476. lp5521_write(cl, base + i, 0);
  477. lp5521_write(cl, base + i + 1, 0);
  478. }
  479. static inline struct lp5521_led_pattern *lp5521_get_pattern
  480. (struct lp5521_chip *chip, u8 offset)
  481. {
  482. struct lp5521_led_pattern *ptn;
  483. ptn = chip->pdata->patterns + (offset - 1);
  484. return ptn;
  485. }
  486. static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
  487. {
  488. struct lp5521_led_pattern *ptn;
  489. struct i2c_client *cl = chip->client;
  490. int num_patterns = chip->pdata->num_patterns;
  491. if (mode > num_patterns || !(chip->pdata->patterns))
  492. return;
  493. if (mode == PATTERN_OFF) {
  494. lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
  495. usleep_range(1000, 2000);
  496. lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  497. } else {
  498. ptn = lp5521_get_pattern(chip, mode);
  499. if (!ptn)
  500. return;
  501. lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
  502. usleep_range(1000, 2000);
  503. lp5521_clear_program_memory(cl);
  504. lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
  505. ptn->r, ptn->size_r);
  506. lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
  507. ptn->g, ptn->size_g);
  508. lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
  509. ptn->b, ptn->size_b);
  510. lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
  511. usleep_range(1000, 2000);
  512. lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
  513. }
  514. }
  515. static ssize_t store_led_pattern(struct device *dev,
  516. struct device_attribute *attr,
  517. const char *buf, size_t len)
  518. {
  519. struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
  520. unsigned long val;
  521. int ret;
  522. ret = kstrtoul(buf, 16, &val);
  523. if (ret)
  524. return ret;
  525. lp5521_run_led_pattern(val, chip);
  526. return len;
  527. }
  528. /* led class device attributes */
  529. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
  530. static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
  531. static struct attribute *lp5521_led_attributes[] = {
  532. &dev_attr_led_current.attr,
  533. &dev_attr_max_current.attr,
  534. NULL,
  535. };
  536. static struct attribute_group lp5521_led_attribute_group = {
  537. .attrs = lp5521_led_attributes
  538. };
  539. /* device attributes */
  540. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
  541. show_engine1_mode, store_engine1_mode);
  542. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
  543. show_engine2_mode, store_engine2_mode);
  544. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
  545. show_engine3_mode, store_engine3_mode);
  546. static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
  547. static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
  548. static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
  549. static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
  550. static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
  551. static struct attribute *lp5521_attributes[] = {
  552. &dev_attr_engine1_mode.attr,
  553. &dev_attr_engine2_mode.attr,
  554. &dev_attr_engine3_mode.attr,
  555. &dev_attr_selftest.attr,
  556. &dev_attr_engine1_load.attr,
  557. &dev_attr_engine2_load.attr,
  558. &dev_attr_engine3_load.attr,
  559. &dev_attr_led_pattern.attr,
  560. NULL
  561. };
  562. static const struct attribute_group lp5521_group = {
  563. .attrs = lp5521_attributes,
  564. };
  565. static int lp5521_register_sysfs(struct i2c_client *client)
  566. {
  567. struct device *dev = &client->dev;
  568. return sysfs_create_group(&dev->kobj, &lp5521_group);
  569. }
  570. static void lp5521_unregister_sysfs(struct i2c_client *client)
  571. {
  572. struct lp5521_chip *chip = i2c_get_clientdata(client);
  573. struct device *dev = &client->dev;
  574. int i;
  575. sysfs_remove_group(&dev->kobj, &lp5521_group);
  576. for (i = 0; i < chip->num_leds; i++)
  577. sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
  578. &lp5521_led_attribute_group);
  579. }
  580. static void lp5521_deinit_device(struct lp5521_chip *chip);
  581. static int lp5521_init_device(struct lp5521_chip *chip)
  582. {
  583. struct i2c_client *client = chip->client;
  584. int ret;
  585. ret = lp5521_configure(client);
  586. if (ret < 0) {
  587. dev_err(&client->dev, "error configuring chip\n");
  588. goto err_config;
  589. }
  590. return 0;
  591. err_config:
  592. lp5521_deinit_device(chip);
  593. return ret;
  594. }
  595. static void lp5521_deinit_device(struct lp5521_chip *chip)
  596. {
  597. struct lp5521_platform_data *pdata = chip->pdata;
  598. if (pdata->enable)
  599. pdata->enable(0);
  600. if (pdata->release_resources)
  601. pdata->release_resources();
  602. }
  603. static int lp5521_init_led(struct lp5521_led *led,
  604. struct i2c_client *client,
  605. int chan, struct lp5521_platform_data *pdata)
  606. {
  607. struct device *dev = &client->dev;
  608. char name[32];
  609. int res;
  610. if (chan >= LP5521_MAX_LEDS)
  611. return -EINVAL;
  612. if (pdata->led_config[chan].led_current == 0)
  613. return 0;
  614. led->led_current = pdata->led_config[chan].led_current;
  615. led->max_current = pdata->led_config[chan].max_current;
  616. led->chan_nr = pdata->led_config[chan].chan_nr;
  617. if (led->chan_nr >= LP5521_MAX_LEDS) {
  618. dev_err(dev, "Use channel numbers between 0 and %d\n",
  619. LP5521_MAX_LEDS - 1);
  620. return -EINVAL;
  621. }
  622. led->cdev.brightness_set = lp5521_set_brightness;
  623. if (pdata->led_config[chan].name) {
  624. led->cdev.name = pdata->led_config[chan].name;
  625. } else {
  626. snprintf(name, sizeof(name), "%s:channel%d",
  627. pdata->label ?: client->name, chan);
  628. led->cdev.name = name;
  629. }
  630. res = led_classdev_register(dev, &led->cdev);
  631. if (res < 0) {
  632. dev_err(dev, "couldn't register led on channel %d\n", chan);
  633. return res;
  634. }
  635. res = sysfs_create_group(&led->cdev.dev->kobj,
  636. &lp5521_led_attribute_group);
  637. if (res < 0) {
  638. dev_err(dev, "couldn't register current attribute\n");
  639. led_classdev_unregister(&led->cdev);
  640. return res;
  641. }
  642. return 0;
  643. }
  644. static int lp5521_register_leds(struct lp5521_chip *chip)
  645. {
  646. struct lp5521_platform_data *pdata = chip->pdata;
  647. struct i2c_client *client = chip->client;
  648. int i;
  649. int led;
  650. int ret;
  651. /* Initialize leds */
  652. chip->num_channels = pdata->num_channels;
  653. chip->num_leds = 0;
  654. led = 0;
  655. for (i = 0; i < pdata->num_channels; i++) {
  656. /* Do not initialize channels that are not connected */
  657. if (pdata->led_config[i].led_current == 0)
  658. continue;
  659. ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
  660. if (ret) {
  661. dev_err(&client->dev, "error initializing leds\n");
  662. return ret;
  663. }
  664. chip->num_leds++;
  665. chip->leds[led].id = led;
  666. /* Set initial LED current */
  667. lp5521_set_led_current(chip, led,
  668. chip->leds[led].led_current);
  669. INIT_WORK(&(chip->leds[led].brightness_work),
  670. lp5521_led_brightness_work);
  671. led++;
  672. }
  673. return 0;
  674. }
  675. static void lp5521_unregister_leds(struct lp5521_chip *chip)
  676. {
  677. int i;
  678. for (i = 0; i < chip->num_leds; i++) {
  679. led_classdev_unregister(&chip->leds[i].cdev);
  680. cancel_work_sync(&chip->leds[i].brightness_work);
  681. }
  682. }
  683. /* Chip specific configurations */
  684. static struct lp55xx_device_config lp5521_cfg = {
  685. .reset = {
  686. .addr = LP5521_REG_RESET,
  687. .val = LP5521_RESET,
  688. },
  689. .enable = {
  690. .addr = LP5521_REG_ENABLE,
  691. .val = LP5521_ENABLE_DEFAULT,
  692. },
  693. };
  694. static int lp5521_probe(struct i2c_client *client,
  695. const struct i2c_device_id *id)
  696. {
  697. struct lp5521_chip *old_chip = NULL;
  698. int ret;
  699. struct lp55xx_chip *chip;
  700. struct lp55xx_led *led;
  701. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  702. if (!pdata) {
  703. dev_err(&client->dev, "no platform data\n");
  704. return -EINVAL;
  705. }
  706. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  707. if (!chip)
  708. return -ENOMEM;
  709. led = devm_kzalloc(&client->dev,
  710. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  711. if (!led)
  712. return -ENOMEM;
  713. chip->cl = client;
  714. chip->pdata = pdata;
  715. chip->cfg = &lp5521_cfg;
  716. mutex_init(&chip->lock);
  717. i2c_set_clientdata(client, led);
  718. ret = lp5521_init_device(old_chip);
  719. if (ret)
  720. goto err_init;
  721. dev_info(&client->dev, "%s programmable led chip found\n", id->name);
  722. ret = lp5521_register_leds(old_chip);
  723. if (ret)
  724. goto fail2;
  725. ret = lp5521_register_sysfs(client);
  726. if (ret) {
  727. dev_err(&client->dev, "registering sysfs failed\n");
  728. goto fail2;
  729. }
  730. return ret;
  731. fail2:
  732. lp5521_unregister_leds(old_chip);
  733. lp5521_deinit_device(old_chip);
  734. err_init:
  735. return ret;
  736. }
  737. static int lp5521_remove(struct i2c_client *client)
  738. {
  739. struct lp5521_chip *old_chip = i2c_get_clientdata(client);
  740. lp5521_run_led_pattern(PATTERN_OFF, old_chip);
  741. lp5521_unregister_sysfs(client);
  742. lp5521_unregister_leds(old_chip);
  743. lp5521_deinit_device(old_chip);
  744. return 0;
  745. }
  746. static const struct i2c_device_id lp5521_id[] = {
  747. { "lp5521", 0 }, /* Three channel chip */
  748. { }
  749. };
  750. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  751. static struct i2c_driver lp5521_driver = {
  752. .driver = {
  753. .name = "lp5521",
  754. },
  755. .probe = lp5521_probe,
  756. .remove = lp5521_remove,
  757. .id_table = lp5521_id,
  758. };
  759. module_i2c_driver(lp5521_driver);
  760. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  761. MODULE_DESCRIPTION("LP5521 LED engine");
  762. MODULE_LICENSE("GPL v2");