leds-lp5521.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 <linux/firmware.h>
  38. #include "leds-lp55xx-common.h"
  39. #define LP5521_PROGRAM_LENGTH 32 /* in bytes */
  40. #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
  41. #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
  42. #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
  43. #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
  44. #define LP5521_CMD_LOAD 0x15 /* 00010101 */
  45. #define LP5521_CMD_RUN 0x2a /* 00101010 */
  46. #define LP5521_CMD_DIRECT 0x3f /* 00111111 */
  47. #define LP5521_CMD_DISABLED 0x00 /* 00000000 */
  48. /* Registers */
  49. #define LP5521_REG_ENABLE 0x00
  50. #define LP5521_REG_OP_MODE 0x01
  51. #define LP5521_REG_R_PWM 0x02
  52. #define LP5521_REG_G_PWM 0x03
  53. #define LP5521_REG_B_PWM 0x04
  54. #define LP5521_REG_R_CURRENT 0x05
  55. #define LP5521_REG_G_CURRENT 0x06
  56. #define LP5521_REG_B_CURRENT 0x07
  57. #define LP5521_REG_CONFIG 0x08
  58. #define LP5521_REG_R_CHANNEL_PC 0x09
  59. #define LP5521_REG_G_CHANNEL_PC 0x0A
  60. #define LP5521_REG_B_CHANNEL_PC 0x0B
  61. #define LP5521_REG_STATUS 0x0C
  62. #define LP5521_REG_RESET 0x0D
  63. #define LP5521_REG_GPO 0x0E
  64. #define LP5521_REG_R_PROG_MEM 0x10
  65. #define LP5521_REG_G_PROG_MEM 0x30
  66. #define LP5521_REG_B_PROG_MEM 0x50
  67. #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
  68. #define LP5521_PROG_MEM_SIZE 0x20
  69. /* Base register to set LED current */
  70. #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
  71. /* Base register to set the brightness */
  72. #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
  73. /* Bits in ENABLE register */
  74. #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
  75. #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  76. #define LP5521_EXEC_RUN 0x2A
  77. #define LP5521_ENABLE_DEFAULT \
  78. (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
  79. #define LP5521_ENABLE_RUN_PROGRAM \
  80. (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
  81. /* Status */
  82. #define LP5521_EXT_CLK_USED 0x08
  83. /* default R channel current register value */
  84. #define LP5521_REG_R_CURR_DEFAULT 0xAF
  85. /* Pattern Mode */
  86. #define PATTERN_OFF 0
  87. /* Reset register value */
  88. #define LP5521_RESET 0xFF
  89. /* Program Memory Operations */
  90. #define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
  91. #define LP5521_MODE_G_M 0x0C
  92. #define LP5521_MODE_B_M 0x03
  93. #define LP5521_LOAD_R 0x10
  94. #define LP5521_LOAD_G 0x04
  95. #define LP5521_LOAD_B 0x01
  96. #define LP5521_R_IS_LOADING(mode) \
  97. ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
  98. #define LP5521_G_IS_LOADING(mode) \
  99. ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
  100. #define LP5521_B_IS_LOADING(mode) \
  101. ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
  102. #define LP5521_EXEC_R_M 0x30 /* Enable Register */
  103. #define LP5521_EXEC_G_M 0x0C
  104. #define LP5521_EXEC_B_M 0x03
  105. #define LP5521_EXEC_M 0x3F
  106. #define LP5521_RUN_R 0x20
  107. #define LP5521_RUN_G 0x08
  108. #define LP5521_RUN_B 0x02
  109. static inline void lp5521_wait_opmode_done(void)
  110. {
  111. /* operation mode change needs to be longer than 153 us */
  112. usleep_range(200, 300);
  113. }
  114. static inline void lp5521_wait_enable_done(void)
  115. {
  116. /* it takes more 488 us to update ENABLE register */
  117. usleep_range(500, 600);
  118. }
  119. static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
  120. {
  121. led->led_current = led_current;
  122. lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
  123. led_current);
  124. }
  125. static void lp5521_load_engine(struct lp55xx_chip *chip)
  126. {
  127. enum lp55xx_engine_index idx = chip->engine_idx;
  128. u8 mask[] = {
  129. [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
  130. [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
  131. [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
  132. };
  133. u8 val[] = {
  134. [LP55XX_ENGINE_1] = LP5521_LOAD_R,
  135. [LP55XX_ENGINE_2] = LP5521_LOAD_G,
  136. [LP55XX_ENGINE_3] = LP5521_LOAD_B,
  137. };
  138. lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
  139. lp5521_wait_opmode_done();
  140. }
  141. static void lp5521_stop_engine(struct lp55xx_chip *chip)
  142. {
  143. lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
  144. lp5521_wait_opmode_done();
  145. }
  146. static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
  147. {
  148. int ret;
  149. u8 mode;
  150. u8 exec;
  151. /* stop engine */
  152. if (!start) {
  153. lp5521_stop_engine(chip);
  154. lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  155. lp5521_wait_opmode_done();
  156. return;
  157. }
  158. /*
  159. * To run the engine,
  160. * operation mode and enable register should updated at the same time
  161. */
  162. ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
  163. if (ret)
  164. return;
  165. ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
  166. if (ret)
  167. return;
  168. /* change operation mode to RUN only when each engine is loading */
  169. if (LP5521_R_IS_LOADING(mode)) {
  170. mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
  171. exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
  172. }
  173. if (LP5521_G_IS_LOADING(mode)) {
  174. mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
  175. exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
  176. }
  177. if (LP5521_B_IS_LOADING(mode)) {
  178. mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
  179. exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
  180. }
  181. lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
  182. lp5521_wait_opmode_done();
  183. lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
  184. lp5521_wait_enable_done();
  185. }
  186. static int lp5521_update_program_memory(struct lp55xx_chip *chip,
  187. const u8 *data, size_t size)
  188. {
  189. enum lp55xx_engine_index idx = chip->engine_idx;
  190. u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
  191. u8 addr[] = {
  192. [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
  193. [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
  194. [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
  195. };
  196. unsigned cmd;
  197. char c[3];
  198. int program_size;
  199. int nrchars;
  200. int offset = 0;
  201. int ret;
  202. int i;
  203. /* clear program memory before updating */
  204. for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
  205. lp55xx_write(chip, addr[idx] + i, 0);
  206. i = 0;
  207. while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
  208. /* separate sscanfs because length is working only for %s */
  209. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  210. if (ret != 1)
  211. goto err;
  212. ret = sscanf(c, "%2x", &cmd);
  213. if (ret != 1)
  214. goto err;
  215. pattern[i] = (u8)cmd;
  216. offset += nrchars;
  217. i++;
  218. }
  219. /* Each instruction is 16bit long. Check that length is even */
  220. if (i % 2)
  221. goto err;
  222. program_size = i;
  223. for (i = 0; i < program_size; i++)
  224. lp55xx_write(chip, addr[idx] + i, pattern[i]);
  225. return 0;
  226. err:
  227. dev_err(&chip->cl->dev, "wrong pattern format\n");
  228. return -EINVAL;
  229. }
  230. static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
  231. {
  232. const struct firmware *fw = chip->fw;
  233. if (fw->size > LP5521_PROGRAM_LENGTH) {
  234. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  235. fw->size);
  236. return;
  237. }
  238. /*
  239. * Program momery sequence
  240. * 1) set engine mode to "LOAD"
  241. * 2) write firmware data into program memory
  242. */
  243. lp5521_load_engine(chip);
  244. lp5521_update_program_memory(chip, fw->data, fw->size);
  245. }
  246. static int lp5521_post_init_device(struct lp55xx_chip *chip)
  247. {
  248. int ret;
  249. u8 val;
  250. /*
  251. * Make sure that the chip is reset by reading back the r channel
  252. * current reg. This is dummy read is required on some platforms -
  253. * otherwise further access to the R G B channels in the
  254. * LP5521_REG_ENABLE register will not have any effect - strange!
  255. */
  256. ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
  257. if (ret) {
  258. dev_err(&chip->cl->dev, "error in resetting chip\n");
  259. return ret;
  260. }
  261. if (val != LP5521_REG_R_CURR_DEFAULT) {
  262. dev_err(&chip->cl->dev,
  263. "unexpected data in register (expected 0x%x got 0x%x)\n",
  264. LP5521_REG_R_CURR_DEFAULT, val);
  265. ret = -EINVAL;
  266. return ret;
  267. }
  268. usleep_range(10000, 20000);
  269. /* Set all PWMs to direct control mode */
  270. ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  271. val = chip->pdata->update_config ?
  272. : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
  273. ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
  274. if (ret)
  275. return ret;
  276. /* Initialize all channels PWM to zero -> leds off */
  277. lp55xx_write(chip, LP5521_REG_R_PWM, 0);
  278. lp55xx_write(chip, LP5521_REG_G_PWM, 0);
  279. lp55xx_write(chip, LP5521_REG_B_PWM, 0);
  280. /* Set engines are set to run state when OP_MODE enables engines */
  281. ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
  282. if (ret)
  283. return ret;
  284. lp5521_wait_enable_done();
  285. return 0;
  286. }
  287. static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
  288. {
  289. struct lp55xx_platform_data *pdata = chip->pdata;
  290. int ret;
  291. u8 status;
  292. ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
  293. if (ret < 0)
  294. return ret;
  295. if (pdata->clock_mode != LP55XX_CLOCK_EXT)
  296. return 0;
  297. /* Check that ext clock is really in use if requested */
  298. if ((status & LP5521_EXT_CLK_USED) == 0)
  299. return -EIO;
  300. return 0;
  301. }
  302. static void lp5521_led_brightness_work(struct work_struct *work)
  303. {
  304. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  305. brightness_work);
  306. struct lp55xx_chip *chip = led->chip;
  307. mutex_lock(&chip->lock);
  308. lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
  309. led->brightness);
  310. mutex_unlock(&chip->lock);
  311. }
  312. static ssize_t lp5521_selftest(struct device *dev,
  313. struct device_attribute *attr,
  314. char *buf)
  315. {
  316. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  317. struct lp55xx_chip *chip = led->chip;
  318. int ret;
  319. mutex_lock(&chip->lock);
  320. ret = lp5521_run_selftest(chip, buf);
  321. mutex_unlock(&chip->lock);
  322. return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
  323. }
  324. /* device attributes */
  325. static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
  326. static struct attribute *lp5521_attributes[] = {
  327. &dev_attr_selftest.attr,
  328. NULL
  329. };
  330. static const struct attribute_group lp5521_group = {
  331. .attrs = lp5521_attributes,
  332. };
  333. /* Chip specific configurations */
  334. static struct lp55xx_device_config lp5521_cfg = {
  335. .reset = {
  336. .addr = LP5521_REG_RESET,
  337. .val = LP5521_RESET,
  338. },
  339. .enable = {
  340. .addr = LP5521_REG_ENABLE,
  341. .val = LP5521_ENABLE_DEFAULT,
  342. },
  343. .max_channel = LP5521_MAX_LEDS,
  344. .post_init_device = lp5521_post_init_device,
  345. .brightness_work_fn = lp5521_led_brightness_work,
  346. .set_led_current = lp5521_set_led_current,
  347. .firmware_cb = lp5521_firmware_loaded,
  348. .run_engine = lp5521_run_engine,
  349. .dev_attr_group = &lp5521_group,
  350. };
  351. static int lp5521_probe(struct i2c_client *client,
  352. const struct i2c_device_id *id)
  353. {
  354. int ret;
  355. struct lp55xx_chip *chip;
  356. struct lp55xx_led *led;
  357. struct lp55xx_platform_data *pdata = client->dev.platform_data;
  358. if (!pdata) {
  359. dev_err(&client->dev, "no platform data\n");
  360. return -EINVAL;
  361. }
  362. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  363. if (!chip)
  364. return -ENOMEM;
  365. led = devm_kzalloc(&client->dev,
  366. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  367. if (!led)
  368. return -ENOMEM;
  369. chip->cl = client;
  370. chip->pdata = pdata;
  371. chip->cfg = &lp5521_cfg;
  372. mutex_init(&chip->lock);
  373. i2c_set_clientdata(client, led);
  374. ret = lp55xx_init_device(chip);
  375. if (ret)
  376. goto err_init;
  377. dev_info(&client->dev, "%s programmable led chip found\n", id->name);
  378. ret = lp55xx_register_leds(led, chip);
  379. if (ret)
  380. goto err_register_leds;
  381. ret = lp55xx_register_sysfs(chip);
  382. if (ret) {
  383. dev_err(&client->dev, "registering sysfs failed\n");
  384. goto err_register_sysfs;
  385. }
  386. return 0;
  387. err_register_sysfs:
  388. lp55xx_unregister_leds(led, chip);
  389. err_register_leds:
  390. lp55xx_deinit_device(chip);
  391. err_init:
  392. return ret;
  393. }
  394. static int lp5521_remove(struct i2c_client *client)
  395. {
  396. struct lp55xx_led *led = i2c_get_clientdata(client);
  397. struct lp55xx_chip *chip = led->chip;
  398. lp5521_stop_engine(chip);
  399. lp55xx_unregister_sysfs(chip);
  400. lp55xx_unregister_leds(led, chip);
  401. lp55xx_deinit_device(chip);
  402. return 0;
  403. }
  404. static const struct i2c_device_id lp5521_id[] = {
  405. { "lp5521", 0 }, /* Three channel chip */
  406. { }
  407. };
  408. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  409. static struct i2c_driver lp5521_driver = {
  410. .driver = {
  411. .name = "lp5521",
  412. },
  413. .probe = lp5521_probe,
  414. .remove = lp5521_remove,
  415. .id_table = lp5521_id,
  416. };
  417. module_i2c_driver(lp5521_driver);
  418. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  419. MODULE_DESCRIPTION("LP5521 LED engine");
  420. MODULE_LICENSE("GPL v2");