leds-lp5521.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * LP5521 LED chip driver.
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. * Copyright (C) 2012 Texas Instruments
  6. *
  7. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  8. * Milo(Woogyom) Kim <milo.kim@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/firmware.h>
  26. #include <linux/i2c.h>
  27. #include <linux/init.h>
  28. #include <linux/leds.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/platform_data/leds-lp55xx.h>
  32. #include <linux/slab.h>
  33. #include <linux/of.h>
  34. #include "leds-lp55xx-common.h"
  35. #define LP5521_PROGRAM_LENGTH 32
  36. #define LP5521_MAX_LEDS 3
  37. #define LP5521_CMD_DIRECT 0x3F
  38. /* Registers */
  39. #define LP5521_REG_ENABLE 0x00
  40. #define LP5521_REG_OP_MODE 0x01
  41. #define LP5521_REG_R_PWM 0x02
  42. #define LP5521_REG_G_PWM 0x03
  43. #define LP5521_REG_B_PWM 0x04
  44. #define LP5521_REG_R_CURRENT 0x05
  45. #define LP5521_REG_G_CURRENT 0x06
  46. #define LP5521_REG_B_CURRENT 0x07
  47. #define LP5521_REG_CONFIG 0x08
  48. #define LP5521_REG_STATUS 0x0C
  49. #define LP5521_REG_RESET 0x0D
  50. #define LP5521_REG_R_PROG_MEM 0x10
  51. #define LP5521_REG_G_PROG_MEM 0x30
  52. #define LP5521_REG_B_PROG_MEM 0x50
  53. /* Base register to set LED current */
  54. #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
  55. /* Base register to set the brightness */
  56. #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
  57. /* Bits in ENABLE register */
  58. #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
  59. #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  60. #define LP5521_EXEC_RUN 0x2A
  61. #define LP5521_ENABLE_DEFAULT \
  62. (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
  63. #define LP5521_ENABLE_RUN_PROGRAM \
  64. (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
  65. /* CONFIG register */
  66. #define LP5521_PWM_HF 0x40 /* PWM: 0 = 256Hz, 1 = 558Hz */
  67. #define LP5521_PWRSAVE_EN 0x20 /* 1 = Power save mode */
  68. #define LP5521_CP_MODE_OFF 0 /* Charge pump (CP) off */
  69. #define LP5521_CP_MODE_BYPASS 8 /* CP forced to bypass mode */
  70. #define LP5521_CP_MODE_1X5 0x10 /* CP forced to 1.5x mode */
  71. #define LP5521_CP_MODE_AUTO 0x18 /* Automatic mode selection */
  72. #define LP5521_R_TO_BATT 0x04 /* R out: 0 = CP, 1 = Vbat */
  73. #define LP5521_CLK_INT 0x01 /* Internal clock */
  74. #define LP5521_DEFAULT_CFG \
  75. (LP5521_PWM_HF | LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO)
  76. /* Status */
  77. #define LP5521_EXT_CLK_USED 0x08
  78. /* default R channel current register value */
  79. #define LP5521_REG_R_CURR_DEFAULT 0xAF
  80. /* Reset register value */
  81. #define LP5521_RESET 0xFF
  82. /* Program Memory Operations */
  83. #define LP5521_MODE_R_M 0x30 /* Operation Mode Register */
  84. #define LP5521_MODE_G_M 0x0C
  85. #define LP5521_MODE_B_M 0x03
  86. #define LP5521_LOAD_R 0x10
  87. #define LP5521_LOAD_G 0x04
  88. #define LP5521_LOAD_B 0x01
  89. #define LP5521_R_IS_LOADING(mode) \
  90. ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R)
  91. #define LP5521_G_IS_LOADING(mode) \
  92. ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G)
  93. #define LP5521_B_IS_LOADING(mode) \
  94. ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B)
  95. #define LP5521_EXEC_R_M 0x30 /* Enable Register */
  96. #define LP5521_EXEC_G_M 0x0C
  97. #define LP5521_EXEC_B_M 0x03
  98. #define LP5521_EXEC_M 0x3F
  99. #define LP5521_RUN_R 0x20
  100. #define LP5521_RUN_G 0x08
  101. #define LP5521_RUN_B 0x02
  102. static inline void lp5521_wait_opmode_done(void)
  103. {
  104. /* operation mode change needs to be longer than 153 us */
  105. usleep_range(200, 300);
  106. }
  107. static inline void lp5521_wait_enable_done(void)
  108. {
  109. /* it takes more 488 us to update ENABLE register */
  110. usleep_range(500, 600);
  111. }
  112. static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
  113. {
  114. led->led_current = led_current;
  115. lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
  116. led_current);
  117. }
  118. static void lp5521_load_engine(struct lp55xx_chip *chip)
  119. {
  120. enum lp55xx_engine_index idx = chip->engine_idx;
  121. u8 mask[] = {
  122. [LP55XX_ENGINE_1] = LP5521_MODE_R_M,
  123. [LP55XX_ENGINE_2] = LP5521_MODE_G_M,
  124. [LP55XX_ENGINE_3] = LP5521_MODE_B_M,
  125. };
  126. u8 val[] = {
  127. [LP55XX_ENGINE_1] = LP5521_LOAD_R,
  128. [LP55XX_ENGINE_2] = LP5521_LOAD_G,
  129. [LP55XX_ENGINE_3] = LP5521_LOAD_B,
  130. };
  131. lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]);
  132. lp5521_wait_opmode_done();
  133. }
  134. static void lp5521_stop_engine(struct lp55xx_chip *chip)
  135. {
  136. lp55xx_write(chip, LP5521_REG_OP_MODE, 0);
  137. lp5521_wait_opmode_done();
  138. }
  139. static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
  140. {
  141. int ret;
  142. u8 mode;
  143. u8 exec;
  144. /* stop engine */
  145. if (!start) {
  146. lp5521_stop_engine(chip);
  147. lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  148. lp5521_wait_opmode_done();
  149. return;
  150. }
  151. /*
  152. * To run the engine,
  153. * operation mode and enable register should updated at the same time
  154. */
  155. ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode);
  156. if (ret)
  157. return;
  158. ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec);
  159. if (ret)
  160. return;
  161. /* change operation mode to RUN only when each engine is loading */
  162. if (LP5521_R_IS_LOADING(mode)) {
  163. mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R;
  164. exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R;
  165. }
  166. if (LP5521_G_IS_LOADING(mode)) {
  167. mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G;
  168. exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G;
  169. }
  170. if (LP5521_B_IS_LOADING(mode)) {
  171. mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B;
  172. exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B;
  173. }
  174. lp55xx_write(chip, LP5521_REG_OP_MODE, mode);
  175. lp5521_wait_opmode_done();
  176. lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec);
  177. lp5521_wait_enable_done();
  178. }
  179. static int lp5521_update_program_memory(struct lp55xx_chip *chip,
  180. const u8 *data, size_t size)
  181. {
  182. enum lp55xx_engine_index idx = chip->engine_idx;
  183. u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
  184. u8 addr[] = {
  185. [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM,
  186. [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM,
  187. [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM,
  188. };
  189. unsigned cmd;
  190. char c[3];
  191. int program_size;
  192. int nrchars;
  193. int offset = 0;
  194. int ret;
  195. int i;
  196. /* clear program memory before updating */
  197. for (i = 0; i < LP5521_PROGRAM_LENGTH; i++)
  198. lp55xx_write(chip, addr[idx] + i, 0);
  199. i = 0;
  200. while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) {
  201. /* separate sscanfs because length is working only for %s */
  202. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  203. if (ret != 1)
  204. goto err;
  205. ret = sscanf(c, "%2x", &cmd);
  206. if (ret != 1)
  207. goto err;
  208. pattern[i] = (u8)cmd;
  209. offset += nrchars;
  210. i++;
  211. }
  212. /* Each instruction is 16bit long. Check that length is even */
  213. if (i % 2)
  214. goto err;
  215. program_size = i;
  216. for (i = 0; i < program_size; i++)
  217. lp55xx_write(chip, addr[idx] + i, pattern[i]);
  218. return 0;
  219. err:
  220. dev_err(&chip->cl->dev, "wrong pattern format\n");
  221. return -EINVAL;
  222. }
  223. static void lp5521_firmware_loaded(struct lp55xx_chip *chip)
  224. {
  225. const struct firmware *fw = chip->fw;
  226. if (fw->size > LP5521_PROGRAM_LENGTH) {
  227. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  228. fw->size);
  229. return;
  230. }
  231. /*
  232. * Program momery sequence
  233. * 1) set engine mode to "LOAD"
  234. * 2) write firmware data into program memory
  235. */
  236. lp5521_load_engine(chip);
  237. lp5521_update_program_memory(chip, fw->data, fw->size);
  238. }
  239. static int lp5521_post_init_device(struct lp55xx_chip *chip)
  240. {
  241. int ret;
  242. u8 val;
  243. /*
  244. * Make sure that the chip is reset by reading back the r channel
  245. * current reg. This is dummy read is required on some platforms -
  246. * otherwise further access to the R G B channels in the
  247. * LP5521_REG_ENABLE register will not have any effect - strange!
  248. */
  249. ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
  250. if (ret) {
  251. dev_err(&chip->cl->dev, "error in resetting chip\n");
  252. return ret;
  253. }
  254. if (val != LP5521_REG_R_CURR_DEFAULT) {
  255. dev_err(&chip->cl->dev,
  256. "unexpected data in register (expected 0x%x got 0x%x)\n",
  257. LP5521_REG_R_CURR_DEFAULT, val);
  258. ret = -EINVAL;
  259. return ret;
  260. }
  261. usleep_range(10000, 20000);
  262. /* Set all PWMs to direct control mode */
  263. ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  264. /* Update configuration for the clock setting */
  265. val = LP5521_DEFAULT_CFG;
  266. if (!lp55xx_is_extclk_used(chip))
  267. val |= LP5521_CLK_INT;
  268. ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
  269. if (ret)
  270. return ret;
  271. /* Initialize all channels PWM to zero -> leds off */
  272. lp55xx_write(chip, LP5521_REG_R_PWM, 0);
  273. lp55xx_write(chip, LP5521_REG_G_PWM, 0);
  274. lp55xx_write(chip, LP5521_REG_B_PWM, 0);
  275. /* Set engines are set to run state when OP_MODE enables engines */
  276. ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
  277. if (ret)
  278. return ret;
  279. lp5521_wait_enable_done();
  280. return 0;
  281. }
  282. static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
  283. {
  284. struct lp55xx_platform_data *pdata = chip->pdata;
  285. int ret;
  286. u8 status;
  287. ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
  288. if (ret < 0)
  289. return ret;
  290. if (pdata->clock_mode != LP55XX_CLOCK_EXT)
  291. return 0;
  292. /* Check that ext clock is really in use if requested */
  293. if ((status & LP5521_EXT_CLK_USED) == 0)
  294. return -EIO;
  295. return 0;
  296. }
  297. static void lp5521_led_brightness_work(struct work_struct *work)
  298. {
  299. struct lp55xx_led *led = container_of(work, struct lp55xx_led,
  300. brightness_work);
  301. struct lp55xx_chip *chip = led->chip;
  302. mutex_lock(&chip->lock);
  303. lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr,
  304. led->brightness);
  305. mutex_unlock(&chip->lock);
  306. }
  307. static ssize_t lp5521_selftest(struct device *dev,
  308. struct device_attribute *attr,
  309. char *buf)
  310. {
  311. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  312. struct lp55xx_chip *chip = led->chip;
  313. int ret;
  314. mutex_lock(&chip->lock);
  315. ret = lp5521_run_selftest(chip, buf);
  316. mutex_unlock(&chip->lock);
  317. return scnprintf(buf, PAGE_SIZE, "%s\n", ret ? "FAIL" : "OK");
  318. }
  319. /* device attributes */
  320. static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
  321. static struct attribute *lp5521_attributes[] = {
  322. &dev_attr_selftest.attr,
  323. NULL
  324. };
  325. static const struct attribute_group lp5521_group = {
  326. .attrs = lp5521_attributes,
  327. };
  328. /* Chip specific configurations */
  329. static struct lp55xx_device_config lp5521_cfg = {
  330. .reset = {
  331. .addr = LP5521_REG_RESET,
  332. .val = LP5521_RESET,
  333. },
  334. .enable = {
  335. .addr = LP5521_REG_ENABLE,
  336. .val = LP5521_ENABLE_DEFAULT,
  337. },
  338. .max_channel = LP5521_MAX_LEDS,
  339. .post_init_device = lp5521_post_init_device,
  340. .brightness_work_fn = lp5521_led_brightness_work,
  341. .set_led_current = lp5521_set_led_current,
  342. .firmware_cb = lp5521_firmware_loaded,
  343. .run_engine = lp5521_run_engine,
  344. .dev_attr_group = &lp5521_group,
  345. };
  346. static int lp5521_probe(struct i2c_client *client,
  347. const struct i2c_device_id *id)
  348. {
  349. int ret;
  350. struct lp55xx_chip *chip;
  351. struct lp55xx_led *led;
  352. struct lp55xx_platform_data *pdata;
  353. struct device_node *np = client->dev.of_node;
  354. if (!client->dev.platform_data) {
  355. if (np) {
  356. ret = lp55xx_of_populate_pdata(&client->dev, np);
  357. if (ret < 0)
  358. return ret;
  359. } else {
  360. dev_err(&client->dev, "no platform data\n");
  361. return -EINVAL;
  362. }
  363. }
  364. pdata = client->dev.platform_data;
  365. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  366. if (!chip)
  367. return -ENOMEM;
  368. led = devm_kzalloc(&client->dev,
  369. sizeof(*led) * pdata->num_channels, GFP_KERNEL);
  370. if (!led)
  371. return -ENOMEM;
  372. chip->cl = client;
  373. chip->pdata = pdata;
  374. chip->cfg = &lp5521_cfg;
  375. mutex_init(&chip->lock);
  376. i2c_set_clientdata(client, led);
  377. ret = lp55xx_init_device(chip);
  378. if (ret)
  379. goto err_init;
  380. dev_info(&client->dev, "%s programmable led chip found\n", id->name);
  381. ret = lp55xx_register_leds(led, chip);
  382. if (ret)
  383. goto err_register_leds;
  384. ret = lp55xx_register_sysfs(chip);
  385. if (ret) {
  386. dev_err(&client->dev, "registering sysfs failed\n");
  387. goto err_register_sysfs;
  388. }
  389. return 0;
  390. err_register_sysfs:
  391. lp55xx_unregister_leds(led, chip);
  392. err_register_leds:
  393. lp55xx_deinit_device(chip);
  394. err_init:
  395. return ret;
  396. }
  397. static int lp5521_remove(struct i2c_client *client)
  398. {
  399. struct lp55xx_led *led = i2c_get_clientdata(client);
  400. struct lp55xx_chip *chip = led->chip;
  401. lp5521_stop_engine(chip);
  402. lp55xx_unregister_sysfs(chip);
  403. lp55xx_unregister_leds(led, chip);
  404. lp55xx_deinit_device(chip);
  405. return 0;
  406. }
  407. static const struct i2c_device_id lp5521_id[] = {
  408. { "lp5521", 0 }, /* Three channel chip */
  409. { }
  410. };
  411. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  412. #ifdef CONFIG_OF
  413. static const struct of_device_id of_lp5521_leds_match[] = {
  414. { .compatible = "national,lp5521", },
  415. {},
  416. };
  417. MODULE_DEVICE_TABLE(of, of_lp5521_leds_match);
  418. #endif
  419. static struct i2c_driver lp5521_driver = {
  420. .driver = {
  421. .name = "lp5521",
  422. .of_match_table = of_match_ptr(of_lp5521_leds_match),
  423. },
  424. .probe = lp5521_probe,
  425. .remove = lp5521_remove,
  426. .id_table = lp5521_id,
  427. };
  428. module_i2c_driver(lp5521_driver);
  429. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  430. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  431. MODULE_DESCRIPTION("LP5521 LED engine");
  432. MODULE_LICENSE("GPL v2");