leds-lp5521.c 24 KB

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