leds-lp5521.c 23 KB

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