leds-lp5521.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
  118. {
  119. led->led_current = led_current;
  120. lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
  121. led_current);
  122. }
  123. static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
  124. {
  125. return container_of(cdev, struct lp5521_led, cdev);
  126. }
  127. static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
  128. {
  129. return container_of(engine, struct lp5521_chip,
  130. engines[engine->id - 1]);
  131. }
  132. static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
  133. {
  134. return container_of(led, struct lp5521_chip,
  135. leds[led->id]);
  136. }
  137. static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
  138. {
  139. return i2c_smbus_write_byte_data(client, reg, value);
  140. }
  141. static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
  142. {
  143. s32 ret;
  144. ret = i2c_smbus_read_byte_data(client, reg);
  145. if (ret < 0)
  146. return ret;
  147. *buf = ret;
  148. return 0;
  149. }
  150. static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
  151. {
  152. struct lp5521_chip *chip = engine_to_lp5521(engine);
  153. struct i2c_client *client = chip->client;
  154. int ret;
  155. u8 engine_state;
  156. /* Only transition between RUN and DIRECT mode are handled here */
  157. if (mode == LP5521_CMD_LOAD)
  158. return 0;
  159. if (mode == LP5521_CMD_DISABLED)
  160. mode = LP5521_CMD_DIRECT;
  161. ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
  162. if (ret < 0)
  163. return ret;
  164. /* set mode only for this engine */
  165. engine_state &= ~(engine->engine_mask);
  166. mode &= engine->engine_mask;
  167. engine_state |= mode;
  168. return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
  169. }
  170. static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
  171. {
  172. struct lp5521_chip *chip = engine_to_lp5521(eng);
  173. struct i2c_client *client = chip->client;
  174. int ret;
  175. int addr;
  176. u8 mode;
  177. /* move current engine to direct mode and remember the state */
  178. ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
  179. if (ret)
  180. return ret;
  181. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  182. usleep_range(1000, 2000);
  183. ret = lp5521_read(client, LP5521_REG_OP_MODE, &mode);
  184. if (ret)
  185. return ret;
  186. /* For loading, all the engines to load mode */
  187. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  188. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  189. usleep_range(1000, 2000);
  190. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
  191. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  192. usleep_range(1000, 2000);
  193. addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
  194. i2c_smbus_write_i2c_block_data(client,
  195. addr,
  196. LP5521_PROG_MEM_SIZE,
  197. pattern);
  198. return lp5521_write(client, LP5521_REG_OP_MODE, mode);
  199. }
  200. static int lp5521_post_init_device(struct lp55xx_chip *chip)
  201. {
  202. int ret;
  203. u8 val;
  204. /*
  205. * Make sure that the chip is reset by reading back the r channel
  206. * current reg. This is dummy read is required on some platforms -
  207. * otherwise further access to the R G B channels in the
  208. * LP5521_REG_ENABLE register will not have any effect - strange!
  209. */
  210. ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
  211. if (ret) {
  212. dev_err(&chip->cl->dev, "error in resetting chip\n");
  213. return ret;
  214. }
  215. if (val != LP5521_REG_R_CURR_DEFAULT) {
  216. dev_err(&chip->cl->dev,
  217. "unexpected data in register (expected 0x%x got 0x%x)\n",
  218. LP5521_REG_R_CURR_DEFAULT, val);
  219. ret = -EINVAL;
  220. return ret;
  221. }
  222. usleep_range(10000, 20000);
  223. /* Set all PWMs to direct control mode */
  224. ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  225. val = chip->pdata->update_config ?
  226. : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
  227. ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
  228. if (ret)
  229. return ret;
  230. /* Initialize all channels PWM to zero -> leds off */
  231. lp55xx_write(chip, LP5521_REG_R_PWM, 0);
  232. lp55xx_write(chip, LP5521_REG_G_PWM, 0);
  233. lp55xx_write(chip, LP5521_REG_B_PWM, 0);
  234. /* Set engines are set to run state when OP_MODE enables engines */
  235. ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
  236. if (ret)
  237. return ret;
  238. lp5521_wait_enable_done();
  239. return 0;
  240. }
  241. static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
  242. {
  243. int ret;
  244. u8 status;
  245. ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
  246. if (ret < 0)
  247. return ret;
  248. /* Check that ext clock is really in use if requested */
  249. if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
  250. if ((status & LP5521_EXT_CLK_USED) == 0)
  251. return -EIO;
  252. return 0;
  253. }
  254. static void lp5521_led_brightness_work(struct work_struct *work)
  255. {
  256. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  257. brightness_work);
  258. struct lp55xx_chip *chip = led->chip;
  259. mutex_lock(&chip->lock);
  260. lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
  261. led->brightness);
  262. mutex_unlock(&chip->lock);
  263. }
  264. /* Set engine mode and create appropriate sysfs attributes, if required. */
  265. static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
  266. {
  267. int ret = 0;
  268. /* if in that mode already do nothing, except for run */
  269. if (mode == engine->mode && mode != LP5521_CMD_RUN)
  270. return 0;
  271. if (mode == LP5521_CMD_RUN) {
  272. ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
  273. } else if (mode == LP5521_CMD_LOAD) {
  274. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  275. lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
  276. } else if (mode == LP5521_CMD_DISABLED) {
  277. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  278. }
  279. engine->mode = mode;
  280. return ret;
  281. }
  282. static int lp5521_do_store_load(struct lp5521_engine *engine,
  283. const char *buf, size_t len)
  284. {
  285. struct lp5521_chip *chip = engine_to_lp5521(engine);
  286. struct i2c_client *client = chip->client;
  287. int ret, nrchars, offset = 0, i = 0;
  288. char c[3];
  289. unsigned cmd;
  290. u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
  291. while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
  292. /* separate sscanfs because length is working only for %s */
  293. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  294. if (ret != 2)
  295. goto fail;
  296. ret = sscanf(c, "%2x", &cmd);
  297. if (ret != 1)
  298. goto fail;
  299. pattern[i] = (u8)cmd;
  300. offset += nrchars;
  301. i++;
  302. }
  303. /* Each instruction is 16bit long. Check that length is even */
  304. if (i % 2)
  305. goto fail;
  306. mutex_lock(&chip->lock);
  307. if (engine->mode == LP5521_CMD_LOAD)
  308. ret = lp5521_load_program(engine, pattern);
  309. else
  310. ret = -EINVAL;
  311. mutex_unlock(&chip->lock);
  312. if (ret) {
  313. dev_err(&client->dev, "failed loading pattern\n");
  314. return ret;
  315. }
  316. return len;
  317. fail:
  318. dev_err(&client->dev, "wrong pattern format\n");
  319. return -EINVAL;
  320. }
  321. static ssize_t store_engine_load(struct device *dev,
  322. struct device_attribute *attr,
  323. const char *buf, size_t len, int nr)
  324. {
  325. struct i2c_client *client = to_i2c_client(dev);
  326. struct lp5521_chip *chip = i2c_get_clientdata(client);
  327. return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
  328. }
  329. #define store_load(nr) \
  330. static ssize_t store_engine##nr##_load(struct device *dev, \
  331. struct device_attribute *attr, \
  332. const char *buf, size_t len) \
  333. { \
  334. return store_engine_load(dev, attr, buf, len, nr); \
  335. }
  336. store_load(1)
  337. store_load(2)
  338. store_load(3)
  339. static ssize_t show_engine_mode(struct device *dev,
  340. struct device_attribute *attr,
  341. char *buf, int nr)
  342. {
  343. struct i2c_client *client = to_i2c_client(dev);
  344. struct lp5521_chip *chip = i2c_get_clientdata(client);
  345. switch (chip->engines[nr - 1].mode) {
  346. case LP5521_CMD_RUN:
  347. return sprintf(buf, "run\n");
  348. case LP5521_CMD_LOAD:
  349. return sprintf(buf, "load\n");
  350. case LP5521_CMD_DISABLED:
  351. return sprintf(buf, "disabled\n");
  352. default:
  353. return sprintf(buf, "disabled\n");
  354. }
  355. }
  356. #define show_mode(nr) \
  357. static ssize_t show_engine##nr##_mode(struct device *dev, \
  358. struct device_attribute *attr, \
  359. char *buf) \
  360. { \
  361. return show_engine_mode(dev, attr, buf, nr); \
  362. }
  363. show_mode(1)
  364. show_mode(2)
  365. show_mode(3)
  366. static ssize_t store_engine_mode(struct device *dev,
  367. struct device_attribute *attr,
  368. const char *buf, size_t len, int nr)
  369. {
  370. struct i2c_client *client = to_i2c_client(dev);
  371. struct lp5521_chip *chip = i2c_get_clientdata(client);
  372. struct lp5521_engine *engine = &chip->engines[nr - 1];
  373. mutex_lock(&chip->lock);
  374. if (!strncmp(buf, "run", 3))
  375. lp5521_set_mode(engine, LP5521_CMD_RUN);
  376. else if (!strncmp(buf, "load", 4))
  377. lp5521_set_mode(engine, LP5521_CMD_LOAD);
  378. else if (!strncmp(buf, "disabled", 8))
  379. lp5521_set_mode(engine, LP5521_CMD_DISABLED);
  380. mutex_unlock(&chip->lock);
  381. return len;
  382. }
  383. #define store_mode(nr) \
  384. static ssize_t store_engine##nr##_mode(struct device *dev, \
  385. struct device_attribute *attr, \
  386. const char *buf, size_t len) \
  387. { \
  388. return store_engine_mode(dev, attr, buf, len, nr); \
  389. }
  390. store_mode(1)
  391. store_mode(2)
  392. store_mode(3)
  393. static ssize_t lp5521_selftest(struct device *dev,
  394. struct device_attribute *attr,
  395. char *buf)
  396. {
  397. struct i2c_client *client = to_i2c_client(dev);
  398. struct lp5521_chip *chip = i2c_get_clientdata(client);
  399. int ret;
  400. mutex_lock(&chip->lock);
  401. ret = lp5521_run_selftest(chip, buf);
  402. mutex_unlock(&chip->lock);
  403. return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
  404. }
  405. static void lp5521_clear_program_memory(struct i2c_client *cl)
  406. {
  407. int i;
  408. u8 rgb_mem[] = {
  409. LP5521_REG_R_PROG_MEM,
  410. LP5521_REG_G_PROG_MEM,
  411. LP5521_REG_B_PROG_MEM,
  412. };
  413. for (i = 0; i < ARRAY_SIZE(rgb_mem); i++) {
  414. lp5521_write(cl, rgb_mem[i], 0);
  415. lp5521_write(cl, rgb_mem[i] + 1, 0);
  416. }
  417. }
  418. static void lp5521_write_program_memory(struct i2c_client *cl,
  419. u8 base, u8 *rgb, int size)
  420. {
  421. int i;
  422. if (!rgb || size <= 0)
  423. return;
  424. for (i = 0; i < size; i++)
  425. lp5521_write(cl, base + i, *(rgb + i));
  426. lp5521_write(cl, base + i, 0);
  427. lp5521_write(cl, base + i + 1, 0);
  428. }
  429. static inline struct lp5521_led_pattern *lp5521_get_pattern
  430. (struct lp5521_chip *chip, u8 offset)
  431. {
  432. struct lp5521_led_pattern *ptn;
  433. ptn = chip->pdata->patterns + (offset - 1);
  434. return ptn;
  435. }
  436. static void lp5521_run_led_pattern(int mode, struct lp5521_chip *chip)
  437. {
  438. struct lp5521_led_pattern *ptn;
  439. struct i2c_client *cl = chip->client;
  440. int num_patterns = chip->pdata->num_patterns;
  441. if (mode > num_patterns || !(chip->pdata->patterns))
  442. return;
  443. if (mode == PATTERN_OFF) {
  444. lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_DEFAULT);
  445. usleep_range(1000, 2000);
  446. lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  447. } else {
  448. ptn = lp5521_get_pattern(chip, mode);
  449. if (!ptn)
  450. return;
  451. lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
  452. usleep_range(1000, 2000);
  453. lp5521_clear_program_memory(cl);
  454. lp5521_write_program_memory(cl, LP5521_REG_R_PROG_MEM,
  455. ptn->r, ptn->size_r);
  456. lp5521_write_program_memory(cl, LP5521_REG_G_PROG_MEM,
  457. ptn->g, ptn->size_g);
  458. lp5521_write_program_memory(cl, LP5521_REG_B_PROG_MEM,
  459. ptn->b, ptn->size_b);
  460. lp5521_write(cl, LP5521_REG_OP_MODE, LP5521_CMD_RUN);
  461. usleep_range(1000, 2000);
  462. lp5521_write(cl, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
  463. }
  464. }
  465. static ssize_t store_led_pattern(struct device *dev,
  466. struct device_attribute *attr,
  467. const char *buf, size_t len)
  468. {
  469. struct lp5521_chip *chip = i2c_get_clientdata(to_i2c_client(dev));
  470. unsigned long val;
  471. int ret;
  472. ret = kstrtoul(buf, 16, &val);
  473. if (ret)
  474. return ret;
  475. lp5521_run_led_pattern(val, chip);
  476. return len;
  477. }
  478. static struct attribute_group lp5521_led_attribute_group = {
  479. };
  480. /* device attributes */
  481. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
  482. show_engine1_mode, store_engine1_mode);
  483. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
  484. show_engine2_mode, store_engine2_mode);
  485. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
  486. show_engine3_mode, store_engine3_mode);
  487. static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
  488. static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
  489. static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
  490. static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
  491. static DEVICE_ATTR(led_pattern, S_IWUSR, NULL, store_led_pattern);
  492. static struct attribute *lp5521_attributes[] = {
  493. &dev_attr_engine1_mode.attr,
  494. &dev_attr_engine2_mode.attr,
  495. &dev_attr_engine3_mode.attr,
  496. &dev_attr_selftest.attr,
  497. &dev_attr_engine1_load.attr,
  498. &dev_attr_engine2_load.attr,
  499. &dev_attr_engine3_load.attr,
  500. &dev_attr_led_pattern.attr,
  501. NULL
  502. };
  503. static const struct attribute_group lp5521_group = {
  504. .attrs = lp5521_attributes,
  505. };
  506. static int lp5521_register_sysfs(struct i2c_client *client)
  507. {
  508. struct device *dev = &client->dev;
  509. return sysfs_create_group(&dev->kobj, &lp5521_group);
  510. }
  511. static void lp5521_unregister_sysfs(struct i2c_client *client)
  512. {
  513. struct lp5521_chip *chip = i2c_get_clientdata(client);
  514. struct device *dev = &client->dev;
  515. int i;
  516. sysfs_remove_group(&dev->kobj, &lp5521_group);
  517. for (i = 0; i < chip->num_leds; i++)
  518. sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
  519. &lp5521_led_attribute_group);
  520. }
  521. static void lp5521_unregister_leds(struct lp5521_chip *chip)
  522. {
  523. int i;
  524. for (i = 0; i < chip->num_leds; i++) {
  525. led_classdev_unregister(&chip->leds[i].cdev);
  526. cancel_work_sync(&chip->leds[i].brightness_work);
  527. }
  528. }
  529. /* Chip specific configurations */
  530. static struct lp55xx_device_config lp5521_cfg = {
  531. .reset = {
  532. .addr = LP5521_REG_RESET,
  533. .val = LP5521_RESET,
  534. },
  535. .enable = {
  536. .addr = LP5521_REG_ENABLE,
  537. .val = LP5521_ENABLE_DEFAULT,
  538. },
  539. .max_channel = LP5521_MAX_LEDS,
  540. .post_init_device = lp5521_post_init_device,
  541. .brightness_work_fn = lp5521_led_brightness_work,
  542. .set_led_current = lp5521_set_led_current,
  543. };
  544. static int lp5521_probe(struct i2c_client *client,
  545. const struct i2c_device_id *id)
  546. {
  547. struct lp5521_chip *old_chip = NULL;
  548. int ret;
  549. struct lp55xx_chip *chip;
  550. struct lp55xx_led *led;
  551. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  552. if (!pdata) {
  553. dev_err(&client->dev, "no platform data\n");
  554. return -EINVAL;
  555. }
  556. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  557. if (!chip)
  558. return -ENOMEM;
  559. led = devm_kzalloc(&client->dev,
  560. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  561. if (!led)
  562. return -ENOMEM;
  563. chip->cl = client;
  564. chip->pdata = pdata;
  565. chip->cfg = &lp5521_cfg;
  566. mutex_init(&chip->lock);
  567. i2c_set_clientdata(client, led);
  568. ret = lp55xx_init_device(chip);
  569. if (ret)
  570. goto err_init;
  571. dev_info(&client->dev, "%s programmable led chip found\n", id->name);
  572. ret = lp55xx_register_leds(led, chip);
  573. if (ret)
  574. goto err_register_leds;
  575. ret = lp5521_register_sysfs(client);
  576. if (ret) {
  577. dev_err(&client->dev, "registering sysfs failed\n");
  578. goto fail2;
  579. }
  580. return ret;
  581. fail2:
  582. lp5521_unregister_leds(old_chip);
  583. err_register_leds:
  584. lp55xx_deinit_device(chip);
  585. err_init:
  586. return ret;
  587. }
  588. static int lp5521_remove(struct i2c_client *client)
  589. {
  590. struct lp5521_chip *old_chip = i2c_get_clientdata(client);
  591. struct lp55xx_led *led = i2c_get_clientdata(client);
  592. struct lp55xx_chip *chip = led->chip;
  593. lp5521_run_led_pattern(PATTERN_OFF, old_chip);
  594. lp5521_unregister_sysfs(client);
  595. lp5521_unregister_leds(old_chip);
  596. lp55xx_deinit_device(chip);
  597. return 0;
  598. }
  599. static const struct i2c_device_id lp5521_id[] = {
  600. { "lp5521", 0 }, /* Three channel chip */
  601. { }
  602. };
  603. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  604. static struct i2c_driver lp5521_driver = {
  605. .driver = {
  606. .name = "lp5521",
  607. },
  608. .probe = lp5521_probe,
  609. .remove = lp5521_remove,
  610. .id_table = lp5521_id,
  611. };
  612. module_i2c_driver(lp5521_driver);
  613. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  614. MODULE_DESCRIPTION("LP5521 LED engine");
  615. MODULE_LICENSE("GPL v2");