leds-lp5521.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * LP5521 LED chip 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/platform_data/leds-lp55xx.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include "leds-lp55xx-common.h"
  22. #define LP5521_MAX_LEDS 3
  23. #define LP5521_CMD_DIRECT 0x3F
  24. /* Registers */
  25. #define LP5521_REG_ENABLE 0x00
  26. #define LP5521_REG_OP_MODE 0x01
  27. #define LP5521_REG_R_PWM 0x02
  28. #define LP5521_REG_G_PWM 0x03
  29. #define LP5521_REG_B_PWM 0x04
  30. #define LP5521_REG_R_CURRENT 0x05
  31. #define LP5521_REG_G_CURRENT 0x06
  32. #define LP5521_REG_B_CURRENT 0x07
  33. #define LP5521_REG_CONFIG 0x08
  34. #define LP5521_REG_STATUS 0x0C
  35. #define LP5521_REG_RESET 0x0D
  36. #define LP5521_REG_R_PROG_MEM 0x10
  37. #define LP5521_REG_G_PROG_MEM 0x30
  38. #define LP5521_REG_B_PROG_MEM 0x50
  39. /* Base register to set LED current */
  40. #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
  41. /* Base register to set the brightness */
  42. #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
  43. /* Bits in ENABLE register */
  44. #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
  45. #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  46. #define LP5521_EXEC_RUN 0x2A
  47. #define LP5521_ENABLE_DEFAULT \
  48. (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
  49. #define LP5521_ENABLE_RUN_PROGRAM \
  50. (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
  51. /* CONFIG register */
  52. #define LP5521_PWM_HF 0x40 /* PWM: 0 = 256Hz, 1 = 558Hz */
  53. #define LP5521_PWRSAVE_EN 0x20 /* 1 = Power save mode */
  54. #define LP5521_CP_MODE_MASK 0x18 /* Charge pump mode */
  55. #define LP5521_CP_MODE_SHIFT 3
  56. #define LP5521_R_TO_BATT 0x04 /* R out: 0 = CP, 1 = Vbat */
  57. #define LP5521_CLK_INT 0x01 /* Internal clock */
  58. #define LP5521_DEFAULT_CFG (LP5521_PWM_HF | LP5521_PWRSAVE_EN)
  59. /* Status */
  60. #define LP5521_EXT_CLK_USED 0x08
  61. /* default R channel current register value */
  62. #define LP5521_REG_R_CURR_DEFAULT 0xAF
  63. /* Reset register value */
  64. #define LP5521_RESET 0xFF
  65. static inline void lp5521_wait_opmode_done(void)
  66. {
  67. /* operation mode change needs to be longer than 153 us */
  68. usleep_range(200, 300);
  69. }
  70. static inline void lp5521_wait_enable_done(void)
  71. {
  72. /* it takes more 488 us to update ENABLE register */
  73. usleep_range(500, 600);
  74. }
  75. static void lp5521_run_engine(struct lp55xx_chip *chip, bool start)
  76. {
  77. int ret;
  78. /* stop engine */
  79. if (!start) {
  80. lp55xx_stop_engine(chip);
  81. lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  82. lp5521_wait_opmode_done();
  83. return;
  84. }
  85. ret = lp55xx_run_engine_common(chip);
  86. if (!ret)
  87. lp5521_wait_enable_done();
  88. }
  89. static int lp5521_post_init_device(struct lp55xx_chip *chip)
  90. {
  91. int ret;
  92. u8 val;
  93. /*
  94. * Make sure that the chip is reset by reading back the r channel
  95. * current reg. This is dummy read is required on some platforms -
  96. * otherwise further access to the R G B channels in the
  97. * LP5521_REG_ENABLE register will not have any effect - strange!
  98. */
  99. ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val);
  100. if (ret) {
  101. dev_err(&chip->cl->dev, "error in resetting chip\n");
  102. return ret;
  103. }
  104. if (val != LP5521_REG_R_CURR_DEFAULT) {
  105. dev_err(&chip->cl->dev,
  106. "unexpected data in register (expected 0x%x got 0x%x)\n",
  107. LP5521_REG_R_CURR_DEFAULT, val);
  108. ret = -EINVAL;
  109. return ret;
  110. }
  111. usleep_range(10000, 20000);
  112. /* Set all PWMs to direct control mode */
  113. ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  114. if (ret)
  115. return ret;
  116. /* Update configuration for the clock setting */
  117. val = LP5521_DEFAULT_CFG;
  118. if (!lp55xx_is_extclk_used(chip))
  119. val |= LP5521_CLK_INT;
  120. val |= (chip->pdata->charge_pump_mode << LP5521_CP_MODE_SHIFT) & LP5521_CP_MODE_MASK;
  121. ret = lp55xx_write(chip, LP5521_REG_CONFIG, val);
  122. if (ret)
  123. return ret;
  124. /* Initialize all channels PWM to zero -> leds off */
  125. lp55xx_write(chip, LP5521_REG_R_PWM, 0);
  126. lp55xx_write(chip, LP5521_REG_G_PWM, 0);
  127. lp55xx_write(chip, LP5521_REG_B_PWM, 0);
  128. /* Set engines are set to run state when OP_MODE enables engines */
  129. ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM);
  130. if (ret)
  131. return ret;
  132. lp5521_wait_enable_done();
  133. return 0;
  134. }
  135. static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf)
  136. {
  137. struct lp55xx_platform_data *pdata = chip->pdata;
  138. int ret;
  139. u8 status;
  140. ret = lp55xx_read(chip, LP5521_REG_STATUS, &status);
  141. if (ret < 0)
  142. return ret;
  143. if (pdata->clock_mode != LP55XX_CLOCK_EXT)
  144. return 0;
  145. /* Check that ext clock is really in use if requested */
  146. if ((status & LP5521_EXT_CLK_USED) == 0)
  147. return -EIO;
  148. return 0;
  149. }
  150. static ssize_t lp5521_selftest(struct device *dev,
  151. struct device_attribute *attr,
  152. char *buf)
  153. {
  154. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  155. struct lp55xx_chip *chip = led->chip;
  156. int ret;
  157. guard(mutex)(&chip->lock);
  158. ret = lp5521_run_selftest(chip, buf);
  159. return sysfs_emit(buf, "%s\n", ret ? "FAIL" : "OK");
  160. }
  161. /* device attributes */
  162. LP55XX_DEV_ATTR_ENGINE_MODE(1);
  163. LP55XX_DEV_ATTR_ENGINE_MODE(2);
  164. LP55XX_DEV_ATTR_ENGINE_MODE(3);
  165. LP55XX_DEV_ATTR_ENGINE_LOAD(1);
  166. LP55XX_DEV_ATTR_ENGINE_LOAD(2);
  167. LP55XX_DEV_ATTR_ENGINE_LOAD(3);
  168. static LP55XX_DEV_ATTR_RO(selftest, lp5521_selftest);
  169. static struct attribute *lp5521_attributes[] = {
  170. &dev_attr_engine1_mode.attr,
  171. &dev_attr_engine2_mode.attr,
  172. &dev_attr_engine3_mode.attr,
  173. &dev_attr_engine1_load.attr,
  174. &dev_attr_engine2_load.attr,
  175. &dev_attr_engine3_load.attr,
  176. &dev_attr_selftest.attr,
  177. NULL
  178. };
  179. static const struct attribute_group lp5521_group = {
  180. .attrs = lp5521_attributes,
  181. };
  182. /* Chip specific configurations */
  183. static struct lp55xx_device_config lp5521_cfg = {
  184. .reg_op_mode = {
  185. .addr = LP5521_REG_OP_MODE,
  186. },
  187. .reg_exec = {
  188. .addr = LP5521_REG_ENABLE,
  189. },
  190. .reset = {
  191. .addr = LP5521_REG_RESET,
  192. .val = LP5521_RESET,
  193. },
  194. .enable = {
  195. .addr = LP5521_REG_ENABLE,
  196. .val = LP5521_ENABLE_DEFAULT,
  197. },
  198. .prog_mem_base = {
  199. .addr = LP5521_REG_R_PROG_MEM,
  200. },
  201. .reg_led_pwm_base = {
  202. .addr = LP5521_REG_LED_PWM_BASE,
  203. },
  204. .reg_led_current_base = {
  205. .addr = LP5521_REG_LED_CURRENT_BASE,
  206. },
  207. .max_channel = LP5521_MAX_LEDS,
  208. .post_init_device = lp5521_post_init_device,
  209. .brightness_fn = lp55xx_led_brightness,
  210. .multicolor_brightness_fn = lp55xx_multicolor_brightness,
  211. .set_led_current = lp55xx_set_led_current,
  212. .firmware_cb = lp55xx_firmware_loaded_cb,
  213. .run_engine = lp5521_run_engine,
  214. .dev_attr_group = &lp5521_group,
  215. };
  216. static const struct i2c_device_id lp5521_id[] = {
  217. { "lp5521", .driver_data = (kernel_ulong_t)&lp5521_cfg, }, /* Three channel chip */
  218. { }
  219. };
  220. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  221. static const struct of_device_id of_lp5521_leds_match[] = {
  222. { .compatible = "national,lp5521", .data = &lp5521_cfg, },
  223. {},
  224. };
  225. MODULE_DEVICE_TABLE(of, of_lp5521_leds_match);
  226. static struct i2c_driver lp5521_driver = {
  227. .driver = {
  228. .name = "lp5521",
  229. .of_match_table = of_lp5521_leds_match,
  230. },
  231. .probe = lp55xx_probe,
  232. .remove = lp55xx_remove,
  233. .id_table = lp5521_id,
  234. };
  235. module_i2c_driver(lp5521_driver);
  236. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  237. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  238. MODULE_DESCRIPTION("LP5521 LED engine");
  239. MODULE_LICENSE("GPL v2");