leds-lp5521.c 13 KB

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