leds-lp5521.c 12 KB

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