leds-lp5569.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 Christian Marangi <ansuelsmth@gmail.com>
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/cleanup.h>
  7. #include <linux/delay.h>
  8. #include <linux/firmware.h>
  9. #include <linux/i2c.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/leds.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_data/leds-lp55xx.h>
  16. #include <linux/slab.h>
  17. #include <dt-bindings/leds/leds-lp55xx.h>
  18. #include "leds-lp55xx-common.h"
  19. #define LP5569_MAX_LEDS 9
  20. /* Memory is used like this:
  21. * 0x00 engine 1 program (4 pages)
  22. * 0x40 engine 2 program (4 pages)
  23. * 0x80 engine 3 program (4 pages)
  24. * 0xc0 engine 1 muxing info (1 page)
  25. * 0xd0 engine 2 muxing info (1 page)
  26. * 0xe0 engine 3 muxing info (1 page)
  27. */
  28. #define LP5569_PAGES_PER_ENGINE 4
  29. #define LP5569_REG_ENABLE 0x00
  30. #define LP5569_ENABLE BIT(6)
  31. #define LP5569_REG_EXEC_CTRL 0x01
  32. #define LP5569_MODE_ENG_SHIFT 2
  33. #define LP5569_REG_OP_MODE 0x02
  34. #define LP5569_EXEC_ENG_SHIFT 2
  35. #define LP5569_REG_ENABLE_LEDS_MSB 0x04
  36. #define LP5569_REG_ENABLE_LEDS_LSB 0x05
  37. #define LP5569_REG_LED_CTRL_BASE 0x07
  38. #define LP5569_FADER_MAPPING_MASK GENMASK(7, 5)
  39. #define LP5569_REG_LED_PWM_BASE 0x16
  40. #define LP5569_REG_LED_CURRENT_BASE 0x22
  41. #define LP5569_REG_MISC 0x2F
  42. #define LP5569_AUTO_INC BIT(6)
  43. #define LP5569_PWR_SAVE BIT(5)
  44. #define LP5569_CP_MODE_MASK GENMASK(4, 3)
  45. #define LP5569_PWM_PWR_SAVE BIT(2)
  46. #define LP5569_INTERNAL_CLK BIT(0)
  47. #define LP5569_REG_MISC2 0x33
  48. #define LP5569_LED_SHORT_TEST BIT(4)
  49. #define LP5569_LED_OPEN_TEST BIT(3)
  50. #define LP5569_REG_STATUS 0x3C
  51. #define LP5569_MASK_BUSY BIT(7)
  52. #define LP5569_STARTUP_BUSY BIT(6)
  53. #define LP5569_ENGINE_BUSY BIT(5)
  54. #define LP5569_ENGINE1_INT BIT(2)
  55. #define LP5569_ENGINE2_INT BIT(1)
  56. #define LP5569_ENGINE3_INT BIT(0)
  57. #define LP5569_ENG_STATUS_MASK (LP5569_ENGINE1_INT | LP5569_ENGINE2_INT | \
  58. LP5569_ENGINE3_INT)
  59. #define LP5569_REG_IO_CONTROL 0x3D
  60. #define LP5569_CLK_OUTPUT BIT(3)
  61. #define LP5569_REG_RESET 0x3F
  62. #define LP5569_RESET 0xFF
  63. #define LP5569_REG_MASTER_FADER_BASE 0x46
  64. #define LP5569_REG_CH1_PROG_START 0x4B
  65. #define LP5569_REG_CH2_PROG_START 0x4C
  66. #define LP5569_REG_CH3_PROG_START 0x4D
  67. #define LP5569_REG_PROG_PAGE_SEL 0x4F
  68. #define LP5569_REG_PROG_MEM 0x50
  69. #define LP5569_REG_LED_FAULT1 0x81
  70. #define LP5569_LED_FAULT8 BIT(0)
  71. #define LP5569_REG_LED_FAULT2 0x82
  72. #define LP5569_LED_FAULT7 BIT(7)
  73. #define LP5569_LED_FAULT6 BIT(6)
  74. #define LP5569_LED_FAULT5 BIT(5)
  75. #define LP5569_LED_FAULT4 BIT(4)
  76. #define LP5569_LED_FAULT3 BIT(3)
  77. #define LP5569_LED_FAULT2 BIT(2)
  78. #define LP5569_LED_FAULT1 BIT(1)
  79. #define LP5569_LED_FAULT0 BIT(0)
  80. #define LP5569_ENG1_PROG_ADDR 0x0
  81. #define LP5569_ENG2_PROG_ADDR 0x40
  82. #define LP5569_ENG3_PROG_ADDR 0x80
  83. #define LP5569_ENG1_MUX_ADDR 0xc0
  84. #define LP5569_ENG2_MUX_ADDR 0xd0
  85. #define LP5569_ENG3_MUX_ADDR 0xe0
  86. #define LP5569_STARTUP_SLEEP 500
  87. #define LEDn_STATUS_FAULT(n, status) ((status) >> (n) & BIT(0))
  88. #define LP5569_DEFAULT_CONFIG \
  89. (LP5569_AUTO_INC | LP5569_PWR_SAVE | LP5569_PWM_PWR_SAVE)
  90. static void lp5569_run_engine(struct lp55xx_chip *chip, bool start)
  91. {
  92. if (!start) {
  93. lp55xx_stop_engine(chip);
  94. lp55xx_turn_off_channels(chip);
  95. return;
  96. }
  97. lp55xx_run_engine_common(chip);
  98. }
  99. static int lp5569_init_program_engine(struct lp55xx_chip *chip)
  100. {
  101. int i;
  102. int j;
  103. int ret;
  104. u8 status;
  105. /* Precompiled pattern per ENGINE setting LED MUX start and stop addresses */
  106. static const u8 pattern[][LP55xx_BYTES_PER_PAGE] = {
  107. { 0x9c, LP5569_ENG1_MUX_ADDR, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
  108. { 0x9c, LP5569_ENG2_MUX_ADDR, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
  109. { 0x9c, LP5569_ENG3_MUX_ADDR, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
  110. };
  111. /* Setup each ENGINE program start address */
  112. ret = lp55xx_write(chip, LP5569_REG_CH1_PROG_START, LP5569_ENG1_PROG_ADDR);
  113. if (ret)
  114. return ret;
  115. ret = lp55xx_write(chip, LP5569_REG_CH2_PROG_START, LP5569_ENG2_PROG_ADDR);
  116. if (ret)
  117. return ret;
  118. ret = lp55xx_write(chip, LP5569_REG_CH3_PROG_START, LP5569_ENG3_PROG_ADDR);
  119. if (ret)
  120. return ret;
  121. /* Write precompiled pattern for LED MUX address space for each ENGINE */
  122. for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
  123. chip->engine_idx = i;
  124. lp55xx_load_engine(chip);
  125. for (j = 0; j < LP55xx_BYTES_PER_PAGE; j++) {
  126. ret = lp55xx_write(chip, LP5569_REG_PROG_MEM + j,
  127. pattern[i - 1][j]);
  128. if (ret)
  129. goto out;
  130. }
  131. }
  132. lp5569_run_engine(chip, true);
  133. /* Let the programs run for couple of ms and check the engine status */
  134. usleep_range(3000, 6000);
  135. lp55xx_read(chip, LP5569_REG_STATUS, &status);
  136. status = FIELD_GET(LP5569_ENG_STATUS_MASK, status);
  137. if (status != LP5569_ENG_STATUS_MASK) {
  138. dev_err(&chip->cl->dev,
  139. "could not configure LED engine, status = 0x%.2x\n",
  140. status);
  141. ret = -EINVAL;
  142. }
  143. out:
  144. lp55xx_stop_all_engine(chip);
  145. return ret;
  146. }
  147. static int lp5569_post_init_device(struct lp55xx_chip *chip)
  148. {
  149. int ret;
  150. u8 val;
  151. val = LP5569_DEFAULT_CONFIG;
  152. val |= FIELD_PREP(LP5569_CP_MODE_MASK, chip->pdata->charge_pump_mode);
  153. ret = lp55xx_write(chip, LP5569_REG_MISC, val);
  154. if (ret)
  155. return ret;
  156. if (chip->pdata->clock_mode == LP55XX_CLOCK_INT) {
  157. /* Internal clock MUST be configured before CLK output */
  158. ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
  159. LP5569_INTERNAL_CLK,
  160. LP5569_INTERNAL_CLK);
  161. if (ret)
  162. return ret;
  163. ret = lp55xx_update_bits(chip, LP5569_REG_IO_CONTROL,
  164. LP5569_CLK_OUTPUT,
  165. LP5569_CLK_OUTPUT);
  166. if (ret)
  167. return ret;
  168. }
  169. ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
  170. if (ret)
  171. return ret;
  172. read_poll_timeout(lp55xx_read, ret, !(val & LP5569_STARTUP_BUSY),
  173. LP5569_STARTUP_SLEEP, LP5569_STARTUP_SLEEP * 10, false,
  174. chip, LP5569_REG_STATUS, &val);
  175. return lp5569_init_program_engine(chip);
  176. }
  177. static ssize_t lp5569_led_open_test(struct lp55xx_led *led, char *buf)
  178. {
  179. struct lp55xx_chip *chip = led->chip;
  180. struct lp55xx_platform_data *pdata = chip->pdata;
  181. bool leds_fault[LP5569_MAX_LEDS];
  182. struct lp55xx_led *led_tmp = led;
  183. int i, ret, pos = 0;
  184. u8 status;
  185. /* Set in STANDBY state */
  186. ret = lp55xx_write(chip, LP5569_REG_ENABLE, 0);
  187. if (ret)
  188. goto exit;
  189. /* Wait 1ms for device to enter STANDBY state */
  190. usleep_range(1000, 2000);
  191. /* Set Charge Pump to 1.5x */
  192. ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
  193. FIELD_PREP(LP5569_CP_MODE_MASK, LP55XX_CP_BOOST),
  194. LP5569_CP_MODE_MASK);
  195. if (ret)
  196. goto exit;
  197. /* Enable LED Open Test */
  198. ret = lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
  199. LP5569_LED_OPEN_TEST);
  200. if (ret)
  201. goto exit;
  202. /* Put Device in NORMAL state */
  203. ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
  204. if (ret)
  205. goto exit;
  206. /* Wait 500 us for device to enter NORMAL state */
  207. usleep_range(500, 750);
  208. /* Enable LED and set to 100% brightness */
  209. for (i = 0; i < pdata->num_channels; i++) {
  210. ret = lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
  211. LED_FULL);
  212. if (ret)
  213. goto exit;
  214. led_tmp++;
  215. }
  216. /* Wait 500 us for device to fill status regs */
  217. usleep_range(500, 750);
  218. /* Parse status led fault 1 regs */
  219. ret = lp55xx_read(chip, LP5569_REG_LED_FAULT1, &status);
  220. if (ret < 0)
  221. goto exit;
  222. for (i = 0; i < 8; i++)
  223. leds_fault[i] = !!((status >> i) & 0x1);
  224. /* Parse status led fault 2 regs */
  225. ret = lp55xx_read(chip, LP5569_REG_LED_FAULT2, &status);
  226. if (ret < 0)
  227. goto exit;
  228. for (i = 0; i < 1; i++)
  229. leds_fault[i + 8] = !!((status >> i) & 0x1);
  230. /* Report LED fault */
  231. led_tmp = led;
  232. for (i = 0; i < pdata->num_channels; i++) {
  233. if (leds_fault[led_tmp->chan_nr])
  234. pos += sysfs_emit_at(buf, pos, "LED %d OPEN FAIL\n",
  235. led_tmp->chan_nr);
  236. led_tmp++;
  237. }
  238. ret = pos;
  239. exit:
  240. /* Disable LED Open Test */
  241. lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST, 0);
  242. led_tmp = led;
  243. for (i = 0; i < pdata->num_channels; i++) {
  244. lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr, 0);
  245. led_tmp++;
  246. }
  247. return ret;
  248. }
  249. static ssize_t lp5569_led_short_test(struct lp55xx_led *led, char *buf)
  250. {
  251. struct lp55xx_chip *chip = led->chip;
  252. struct lp55xx_platform_data *pdata = chip->pdata;
  253. bool leds_fault[LP5569_MAX_LEDS];
  254. struct lp55xx_led *led_tmp = led;
  255. int i, ret, pos = 0;
  256. u8 status;
  257. /* Set in STANDBY state */
  258. ret = lp55xx_write(chip, LP5569_REG_ENABLE, 0);
  259. if (ret)
  260. goto exit;
  261. /* Wait 1ms for device to enter STANDBY state */
  262. usleep_range(1000, 2000);
  263. /* Set Charge Pump to 1x */
  264. ret = lp55xx_update_bits(chip, LP5569_REG_MISC,
  265. FIELD_PREP(LP5569_CP_MODE_MASK, LP55XX_CP_BYPASS),
  266. LP5569_CP_MODE_MASK);
  267. if (ret)
  268. goto exit;
  269. /* Enable LED and set to 100% brightness and current to 100% (25.5mA) */
  270. for (i = 0; i < pdata->num_channels; i++) {
  271. ret = lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr,
  272. LED_FULL);
  273. if (ret)
  274. goto exit;
  275. ret = lp55xx_write(chip, LP5569_REG_LED_CURRENT_BASE + led_tmp->chan_nr,
  276. LED_FULL);
  277. if (ret)
  278. goto exit;
  279. led_tmp++;
  280. }
  281. /* Put Device in NORMAL state */
  282. ret = lp55xx_write(chip, LP5569_REG_ENABLE, LP5569_ENABLE);
  283. if (ret)
  284. goto exit;
  285. /* Wait 500 us for device to enter NORMAL state */
  286. usleep_range(500, 750);
  287. /* Enable LED Shorted Test */
  288. ret = lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_OPEN_TEST,
  289. LP5569_LED_SHORT_TEST);
  290. if (ret)
  291. goto exit;
  292. /* Wait 500 us for device to fill status regs */
  293. usleep_range(500, 750);
  294. /* Parse status led fault 1 regs */
  295. ret = lp55xx_read(chip, LP5569_REG_LED_FAULT1, &status);
  296. if (ret < 0)
  297. goto exit;
  298. for (i = 0; i < 8; i++)
  299. leds_fault[i] = !!LEDn_STATUS_FAULT(i, status);
  300. /* Parse status led fault 2 regs */
  301. ret = lp55xx_read(chip, LP5569_REG_LED_FAULT2, &status);
  302. if (ret < 0)
  303. goto exit;
  304. for (i = 0; i < 1; i++)
  305. leds_fault[i + 8] = !!LEDn_STATUS_FAULT(i, status);
  306. /* Report LED fault */
  307. led_tmp = led;
  308. for (i = 0; i < pdata->num_channels; i++) {
  309. if (leds_fault[led_tmp->chan_nr])
  310. pos += sysfs_emit_at(buf, pos, "LED %d SHORTED FAIL\n",
  311. led_tmp->chan_nr);
  312. led_tmp++;
  313. }
  314. ret = pos;
  315. exit:
  316. /* Disable LED Shorted Test */
  317. lp55xx_update_bits(chip, LP5569_REG_MISC2, LP5569_LED_SHORT_TEST, 0);
  318. led_tmp = led;
  319. for (i = 0; i < pdata->num_channels; i++) {
  320. lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led_tmp->chan_nr, 0);
  321. led_tmp++;
  322. }
  323. return ret;
  324. }
  325. static ssize_t lp5569_selftest(struct device *dev,
  326. struct device_attribute *attr,
  327. char *buf)
  328. {
  329. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  330. struct lp55xx_chip *chip = led->chip;
  331. int i, pos = 0;
  332. guard(mutex)(&chip->lock);
  333. /* Test LED Open */
  334. pos = lp5569_led_open_test(led, buf);
  335. if (pos < 0)
  336. return sprintf(buf, "FAIL\n");
  337. /* Test LED Shorted */
  338. pos += lp5569_led_short_test(led, buf);
  339. if (pos < 0)
  340. return sprintf(buf, "FAIL\n");
  341. for (i = 0; i < chip->pdata->num_channels; i++) {
  342. /* Restore current */
  343. lp55xx_write(chip, LP5569_REG_LED_CURRENT_BASE + led->chan_nr,
  344. led->led_current);
  345. /* Restore brightness */
  346. lp55xx_write(chip, LP5569_REG_LED_PWM_BASE + led->chan_nr,
  347. led->brightness);
  348. led++;
  349. }
  350. return pos == 0 ? sysfs_emit(buf, "OK\n") : pos;
  351. }
  352. LP55XX_DEV_ATTR_ENGINE_MODE(1);
  353. LP55XX_DEV_ATTR_ENGINE_MODE(2);
  354. LP55XX_DEV_ATTR_ENGINE_MODE(3);
  355. LP55XX_DEV_ATTR_ENGINE_LEDS(1);
  356. LP55XX_DEV_ATTR_ENGINE_LEDS(2);
  357. LP55XX_DEV_ATTR_ENGINE_LEDS(3);
  358. LP55XX_DEV_ATTR_ENGINE_LOAD(1);
  359. LP55XX_DEV_ATTR_ENGINE_LOAD(2);
  360. LP55XX_DEV_ATTR_ENGINE_LOAD(3);
  361. static LP55XX_DEV_ATTR_RO(selftest, lp5569_selftest);
  362. LP55XX_DEV_ATTR_MASTER_FADER(1);
  363. LP55XX_DEV_ATTR_MASTER_FADER(2);
  364. LP55XX_DEV_ATTR_MASTER_FADER(3);
  365. static LP55XX_DEV_ATTR_RW(master_fader_leds, lp55xx_show_master_fader_leds,
  366. lp55xx_store_master_fader_leds);
  367. static struct attribute *lp5569_attributes[] = {
  368. &dev_attr_engine1_mode.attr,
  369. &dev_attr_engine2_mode.attr,
  370. &dev_attr_engine3_mode.attr,
  371. &dev_attr_engine1_load.attr,
  372. &dev_attr_engine2_load.attr,
  373. &dev_attr_engine3_load.attr,
  374. &dev_attr_engine1_leds.attr,
  375. &dev_attr_engine2_leds.attr,
  376. &dev_attr_engine3_leds.attr,
  377. &dev_attr_selftest.attr,
  378. &dev_attr_master_fader1.attr,
  379. &dev_attr_master_fader2.attr,
  380. &dev_attr_master_fader3.attr,
  381. &dev_attr_master_fader_leds.attr,
  382. NULL,
  383. };
  384. static const struct attribute_group lp5569_group = {
  385. .attrs = lp5569_attributes,
  386. };
  387. /* Chip specific configurations */
  388. static struct lp55xx_device_config lp5569_cfg = {
  389. .reg_op_mode = {
  390. .addr = LP5569_REG_OP_MODE,
  391. .shift = LP5569_MODE_ENG_SHIFT,
  392. },
  393. .reg_exec = {
  394. .addr = LP5569_REG_EXEC_CTRL,
  395. .shift = LP5569_EXEC_ENG_SHIFT,
  396. },
  397. .reset = {
  398. .addr = LP5569_REG_RESET,
  399. .val = LP5569_RESET,
  400. },
  401. .enable = {
  402. .addr = LP5569_REG_ENABLE,
  403. .val = LP5569_ENABLE,
  404. },
  405. .prog_mem_base = {
  406. .addr = LP5569_REG_PROG_MEM,
  407. },
  408. .reg_led_pwm_base = {
  409. .addr = LP5569_REG_LED_PWM_BASE,
  410. },
  411. .reg_led_current_base = {
  412. .addr = LP5569_REG_LED_CURRENT_BASE,
  413. },
  414. .reg_master_fader_base = {
  415. .addr = LP5569_REG_MASTER_FADER_BASE,
  416. },
  417. .reg_led_ctrl_base = {
  418. .addr = LP5569_REG_LED_CTRL_BASE,
  419. },
  420. .pages_per_engine = LP5569_PAGES_PER_ENGINE,
  421. .max_channel = LP5569_MAX_LEDS,
  422. .post_init_device = lp5569_post_init_device,
  423. .brightness_fn = lp55xx_led_brightness,
  424. .multicolor_brightness_fn = lp55xx_multicolor_brightness,
  425. .set_led_current = lp55xx_set_led_current,
  426. .firmware_cb = lp55xx_firmware_loaded_cb,
  427. .run_engine = lp5569_run_engine,
  428. .dev_attr_group = &lp5569_group,
  429. };
  430. static const struct i2c_device_id lp5569_id[] = {
  431. { "lp5569", .driver_data = (kernel_ulong_t)&lp5569_cfg, },
  432. { }
  433. };
  434. MODULE_DEVICE_TABLE(i2c, lp5569_id);
  435. static const struct of_device_id of_lp5569_leds_match[] = {
  436. { .compatible = "ti,lp5569", .data = &lp5569_cfg, },
  437. {},
  438. };
  439. MODULE_DEVICE_TABLE(of, of_lp5569_leds_match);
  440. static struct i2c_driver lp5569_driver = {
  441. .driver = {
  442. .name = "lp5569",
  443. .of_match_table = of_lp5569_leds_match,
  444. },
  445. .probe = lp55xx_probe,
  446. .remove = lp55xx_remove,
  447. .id_table = lp5569_id,
  448. };
  449. module_i2c_driver(lp5569_driver);
  450. MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
  451. MODULE_DESCRIPTION("LP5569 LED engine");
  452. MODULE_LICENSE("GPL");