leds-lp5521.c 12 KB

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