leds-lp5523.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lp5523.c - LP5523, LP55231 LED Driver
  4. *
  5. * Copyright (C) 2010 Nokia Corporation
  6. * Copyright (C) 2012 Texas Instruments
  7. *
  8. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  9. * Milo(Woogyom) Kim <milo.kim@ti.com>
  10. */
  11. #include <linux/cleanup.h>
  12. #include <linux/delay.h>
  13. #include <linux/firmware.h>
  14. #include <linux/i2c.h>
  15. #include <linux/leds.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_data/leds-lp55xx.h>
  20. #include <linux/slab.h>
  21. #include "leds-lp55xx-common.h"
  22. /* Memory is used like this:
  23. * 0x00 engine 1 program
  24. * 0x10 engine 2 program
  25. * 0x20 engine 3 program
  26. * 0x30 engine 1 muxing info
  27. * 0x40 engine 2 muxing info
  28. * 0x50 engine 3 muxing info
  29. */
  30. #define LP5523_PAGES_PER_ENGINE 1
  31. #define LP5523_MAX_LEDS 9
  32. /* Registers */
  33. #define LP5523_REG_ENABLE 0x00
  34. #define LP5523_REG_OP_MODE 0x01
  35. #define LP5523_REG_ENABLE_LEDS_MSB 0x04
  36. #define LP5523_REG_ENABLE_LEDS_LSB 0x05
  37. #define LP5523_REG_LED_CTRL_BASE 0x06
  38. #define LP5523_REG_LED_PWM_BASE 0x16
  39. #define LP5523_REG_LED_CURRENT_BASE 0x26
  40. #define LP5523_REG_CONFIG 0x36
  41. #define LP5523_REG_STATUS 0x3A
  42. #define LP5523_ENGINE_BUSY BIT(4)
  43. #define LP5523_REG_RESET 0x3D
  44. #define LP5523_REG_LED_TEST_CTRL 0x41
  45. #define LP5523_REG_LED_TEST_ADC 0x42
  46. #define LP5523_REG_MASTER_FADER_BASE 0x48
  47. #define LP5523_REG_CH1_PROG_START 0x4C
  48. #define LP5523_REG_CH2_PROG_START 0x4D
  49. #define LP5523_REG_CH3_PROG_START 0x4E
  50. #define LP5523_REG_PROG_PAGE_SEL 0x4F
  51. #define LP5523_REG_PROG_MEM 0x50
  52. /* Bit description in registers */
  53. #define LP5523_ENABLE 0x40
  54. #define LP5523_AUTO_INC 0x40
  55. #define LP5523_PWR_SAVE 0x20
  56. #define LP5523_PWM_PWR_SAVE 0x04
  57. #define LP5523_CP_MODE_MASK 0x18
  58. #define LP5523_CP_MODE_SHIFT 3
  59. #define LP5523_AUTO_CLK 0x02
  60. #define LP5523_DEFAULT_CONFIG \
  61. (LP5523_AUTO_INC | LP5523_PWR_SAVE | LP5523_AUTO_CLK | LP5523_PWM_PWR_SAVE)
  62. #define LP5523_EN_LEDTEST 0x80
  63. #define LP5523_LEDTEST_DONE 0x80
  64. #define LP5523_RESET 0xFF
  65. #define LP5523_ADC_SHORTCIRC_LIM 80
  66. #define LP5523_EXT_CLK_USED 0x08
  67. #define LP5523_ENG_STATUS_MASK 0x07
  68. static int lp5523_init_program_engine(struct lp55xx_chip *chip);
  69. static int lp5523_post_init_device(struct lp55xx_chip *chip)
  70. {
  71. int ret;
  72. int val;
  73. ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
  74. if (ret)
  75. return ret;
  76. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  77. usleep_range(1000, 2000);
  78. val = LP5523_DEFAULT_CONFIG;
  79. val |= (chip->pdata->charge_pump_mode << LP5523_CP_MODE_SHIFT) & LP5523_CP_MODE_MASK;
  80. ret = lp55xx_write(chip, LP5523_REG_CONFIG, val);
  81. if (ret)
  82. return ret;
  83. /* turn on all leds */
  84. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
  85. if (ret)
  86. return ret;
  87. ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
  88. if (ret)
  89. return ret;
  90. return lp5523_init_program_engine(chip);
  91. }
  92. static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
  93. {
  94. /* stop engine */
  95. if (!start) {
  96. lp55xx_stop_engine(chip);
  97. lp55xx_turn_off_channels(chip);
  98. return;
  99. }
  100. lp55xx_run_engine_common(chip);
  101. }
  102. static int lp5523_init_program_engine(struct lp55xx_chip *chip)
  103. {
  104. int i;
  105. int j;
  106. int ret;
  107. u8 status;
  108. /* one pattern per engine setting LED MUX start and stop addresses */
  109. static const u8 pattern[][LP55xx_BYTES_PER_PAGE] = {
  110. { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
  111. { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
  112. { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
  113. };
  114. /* hardcode 32 bytes of memory for each engine from program memory */
  115. ret = lp55xx_write(chip, LP5523_REG_CH1_PROG_START, 0x00);
  116. if (ret)
  117. return ret;
  118. ret = lp55xx_write(chip, LP5523_REG_CH2_PROG_START, 0x10);
  119. if (ret)
  120. return ret;
  121. ret = lp55xx_write(chip, LP5523_REG_CH3_PROG_START, 0x20);
  122. if (ret)
  123. return ret;
  124. /* write LED MUX address space for each engine */
  125. for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
  126. chip->engine_idx = i;
  127. lp55xx_load_engine(chip);
  128. for (j = 0; j < LP55xx_BYTES_PER_PAGE; j++) {
  129. ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
  130. pattern[i - 1][j]);
  131. if (ret)
  132. goto out;
  133. }
  134. }
  135. lp5523_run_engine(chip, true);
  136. /* Let the programs run for couple of ms and check the engine status */
  137. usleep_range(3000, 6000);
  138. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  139. if (ret)
  140. goto out;
  141. status &= LP5523_ENG_STATUS_MASK;
  142. if (status != LP5523_ENG_STATUS_MASK) {
  143. dev_err(&chip->cl->dev,
  144. "could not configure LED engine, status = 0x%.2x\n",
  145. status);
  146. ret = -1;
  147. }
  148. out:
  149. lp55xx_stop_all_engine(chip);
  150. return ret;
  151. }
  152. static ssize_t lp5523_selftest(struct device *dev,
  153. struct device_attribute *attr,
  154. char *buf)
  155. {
  156. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  157. struct lp55xx_chip *chip = led->chip;
  158. struct lp55xx_platform_data *pdata = chip->pdata;
  159. int ret, pos = 0;
  160. u8 status, adc, vdd, i;
  161. guard(mutex)(&chip->lock);
  162. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  163. if (ret < 0)
  164. return sysfs_emit(buf, "FAIL\n");
  165. /* Check that ext clock is really in use if requested */
  166. if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
  167. if ((status & LP5523_EXT_CLK_USED) == 0)
  168. return sysfs_emit(buf, "FAIL\n");
  169. }
  170. /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
  171. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
  172. usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
  173. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  174. if (ret < 0)
  175. return sysfs_emit(buf, "FAIL\n");
  176. if (!(status & LP5523_LEDTEST_DONE))
  177. usleep_range(3000, 6000); /* Was not ready. Wait little bit */
  178. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
  179. if (ret < 0)
  180. return sysfs_emit(buf, "FAIL\n");
  181. vdd--; /* There may be some fluctuation in measurement */
  182. for (i = 0; i < pdata->num_channels; i++) {
  183. /* Skip disabled channels */
  184. if (pdata->led_config[i].led_current == 0)
  185. continue;
  186. /* Set default current */
  187. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  188. pdata->led_config[i].led_current);
  189. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  190. 0xff);
  191. /* let current stabilize 2 - 4ms before measurements start */
  192. usleep_range(2000, 4000);
  193. lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
  194. LP5523_EN_LEDTEST | led->chan_nr);
  195. /* ADC conversion time is 2.7 ms typically */
  196. usleep_range(3000, 6000);
  197. ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
  198. if (ret < 0)
  199. return sysfs_emit(buf, "FAIL\n");
  200. if (!(status & LP5523_LEDTEST_DONE))
  201. usleep_range(3000, 6000); /* Was not ready. Wait. */
  202. ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
  203. if (ret < 0)
  204. return sysfs_emit(buf, "FAIL\n");
  205. if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
  206. pos += sysfs_emit_at(buf, pos, "LED %d FAIL\n",
  207. led->chan_nr);
  208. lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
  209. 0x00);
  210. /* Restore current */
  211. lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
  212. led->led_current);
  213. led++;
  214. }
  215. return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
  216. }
  217. LP55XX_DEV_ATTR_ENGINE_MODE(1);
  218. LP55XX_DEV_ATTR_ENGINE_MODE(2);
  219. LP55XX_DEV_ATTR_ENGINE_MODE(3);
  220. LP55XX_DEV_ATTR_ENGINE_LEDS(1);
  221. LP55XX_DEV_ATTR_ENGINE_LEDS(2);
  222. LP55XX_DEV_ATTR_ENGINE_LEDS(3);
  223. LP55XX_DEV_ATTR_ENGINE_LOAD(1);
  224. LP55XX_DEV_ATTR_ENGINE_LOAD(2);
  225. LP55XX_DEV_ATTR_ENGINE_LOAD(3);
  226. static LP55XX_DEV_ATTR_RO(selftest, lp5523_selftest);
  227. LP55XX_DEV_ATTR_MASTER_FADER(1);
  228. LP55XX_DEV_ATTR_MASTER_FADER(2);
  229. LP55XX_DEV_ATTR_MASTER_FADER(3);
  230. static LP55XX_DEV_ATTR_RW(master_fader_leds, lp55xx_show_master_fader_leds,
  231. lp55xx_store_master_fader_leds);
  232. static struct attribute *lp5523_attributes[] = {
  233. &dev_attr_engine1_mode.attr,
  234. &dev_attr_engine2_mode.attr,
  235. &dev_attr_engine3_mode.attr,
  236. &dev_attr_engine1_load.attr,
  237. &dev_attr_engine2_load.attr,
  238. &dev_attr_engine3_load.attr,
  239. &dev_attr_engine1_leds.attr,
  240. &dev_attr_engine2_leds.attr,
  241. &dev_attr_engine3_leds.attr,
  242. &dev_attr_selftest.attr,
  243. &dev_attr_master_fader1.attr,
  244. &dev_attr_master_fader2.attr,
  245. &dev_attr_master_fader3.attr,
  246. &dev_attr_master_fader_leds.attr,
  247. NULL,
  248. };
  249. static const struct attribute_group lp5523_group = {
  250. .attrs = lp5523_attributes,
  251. };
  252. /* Chip specific configurations */
  253. static struct lp55xx_device_config lp5523_cfg = {
  254. .reg_op_mode = {
  255. .addr = LP5523_REG_OP_MODE,
  256. },
  257. .reg_exec = {
  258. .addr = LP5523_REG_ENABLE,
  259. },
  260. .engine_busy = {
  261. .addr = LP5523_REG_STATUS,
  262. .mask = LP5523_ENGINE_BUSY,
  263. },
  264. .reset = {
  265. .addr = LP5523_REG_RESET,
  266. .val = LP5523_RESET,
  267. },
  268. .enable = {
  269. .addr = LP5523_REG_ENABLE,
  270. .val = LP5523_ENABLE,
  271. },
  272. .prog_mem_base = {
  273. .addr = LP5523_REG_PROG_MEM,
  274. },
  275. .reg_led_pwm_base = {
  276. .addr = LP5523_REG_LED_PWM_BASE,
  277. },
  278. .reg_led_current_base = {
  279. .addr = LP5523_REG_LED_CURRENT_BASE,
  280. },
  281. .reg_master_fader_base = {
  282. .addr = LP5523_REG_MASTER_FADER_BASE,
  283. },
  284. .reg_led_ctrl_base = {
  285. .addr = LP5523_REG_LED_CTRL_BASE,
  286. },
  287. .pages_per_engine = LP5523_PAGES_PER_ENGINE,
  288. .max_channel = LP5523_MAX_LEDS,
  289. .post_init_device = lp5523_post_init_device,
  290. .brightness_fn = lp55xx_led_brightness,
  291. .multicolor_brightness_fn = lp55xx_multicolor_brightness,
  292. .set_led_current = lp55xx_set_led_current,
  293. .firmware_cb = lp55xx_firmware_loaded_cb,
  294. .run_engine = lp5523_run_engine,
  295. .dev_attr_group = &lp5523_group,
  296. };
  297. static const struct i2c_device_id lp5523_id[] = {
  298. { "lp5523", .driver_data = (kernel_ulong_t)&lp5523_cfg, },
  299. { "lp55231", .driver_data = (kernel_ulong_t)&lp5523_cfg, },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(i2c, lp5523_id);
  303. static const struct of_device_id of_lp5523_leds_match[] = {
  304. { .compatible = "national,lp5523", .data = &lp5523_cfg, },
  305. { .compatible = "ti,lp55231", .data = &lp5523_cfg, },
  306. {},
  307. };
  308. MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
  309. static struct i2c_driver lp5523_driver = {
  310. .driver = {
  311. .name = "lp5523x",
  312. .of_match_table = of_lp5523_leds_match,
  313. },
  314. .probe = lp55xx_probe,
  315. .remove = lp55xx_remove,
  316. .id_table = lp5523_id,
  317. };
  318. module_i2c_driver(lp5523_driver);
  319. MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
  320. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  321. MODULE_DESCRIPTION("LP5523 LED engine");
  322. MODULE_LICENSE("GPL");