pwm-twl-led.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
  4. *
  5. * Copyright (C) 2012 Texas Instruments
  6. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  7. *
  8. * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
  9. * Hemanth V <hemanthv@ti.com>
  10. *
  11. * Reference manual for the twl6030 is available at:
  12. * https://www.ti.com/lit/ds/symlink/twl6030.pdf
  13. *
  14. * Limitations:
  15. * - The twl6030 hardware only supports two period lengths (128 clock ticks and
  16. * 64 clock ticks), the driver only uses 128 ticks
  17. * - The hardware doesn't support ON = 0, so the active part of a period doesn't
  18. * start at its beginning.
  19. * - The hardware could support inverted polarity (with a similar limitation as
  20. * for normal: the last clock tick is always inactive).
  21. * - The hardware emits a constant low output when disabled.
  22. * - A request for .duty_cycle = 0 results in an output wave with one active
  23. * clock tick per period. This should better use the disabled state.
  24. * - The driver only implements setting the relative duty cycle.
  25. * - The driver doesn't implement .get_state().
  26. */
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pwm.h>
  31. #include <linux/mfd/twl.h>
  32. #include <linux/slab.h>
  33. /*
  34. * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
  35. * To generate the signal on TWL4030:
  36. * - LEDA uses PWMA
  37. * - LEDB uses PWMB
  38. * TWL6030 has one LED pin with dedicated LEDPWM
  39. */
  40. #define TWL4030_LED_MAX 0x7f
  41. #define TWL6030_LED_MAX 0xff
  42. /* Registers, bits and macro for TWL4030 */
  43. #define TWL4030_LEDEN_REG 0x00
  44. #define TWL4030_PWMA_REG 0x01
  45. #define TWL4030_LEDXON (1 << 0)
  46. #define TWL4030_LEDXPWM (1 << 4)
  47. #define TWL4030_LED_PINS (TWL4030_LEDXON | TWL4030_LEDXPWM)
  48. #define TWL4030_LED_TOGGLE(led, x) ((x) << (led))
  49. /* Register, bits and macro for TWL6030 */
  50. #define TWL6030_LED_PWM_CTRL1 0xf4
  51. #define TWL6030_LED_PWM_CTRL2 0xf5
  52. #define TWL6040_LED_MODE_HW 0x00
  53. #define TWL6040_LED_MODE_ON 0x01
  54. #define TWL6040_LED_MODE_OFF 0x02
  55. #define TWL6040_LED_MODE_MASK 0x03
  56. struct twl_pwmled_chip {
  57. struct mutex mutex;
  58. };
  59. static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
  60. {
  61. return pwmchip_get_drvdata(chip);
  62. }
  63. static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
  64. int duty_ns, int period_ns)
  65. {
  66. int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
  67. u8 pwm_config[2] = { 1, 0 };
  68. int base, ret;
  69. /*
  70. * To configure the duty period:
  71. * On-cycle is set to 1 (the minimum allowed value)
  72. * The off time of 0 is not configurable, so the mapping is:
  73. * 0 -> off cycle = 2,
  74. * 1 -> off cycle = 2,
  75. * 2 -> off cycle = 3,
  76. * 126 - > off cycle 127,
  77. * 127 - > off cycle 1
  78. * When on cycle == off cycle the PWM will be always on
  79. */
  80. if (duty_cycle == 1)
  81. duty_cycle = 2;
  82. else if (duty_cycle > TWL4030_LED_MAX)
  83. duty_cycle = 1;
  84. base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
  85. pwm_config[1] = duty_cycle;
  86. ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
  87. if (ret < 0)
  88. dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label);
  89. return ret;
  90. }
  91. static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  92. {
  93. struct twl_pwmled_chip *twl = to_twl(chip);
  94. int ret;
  95. u8 val;
  96. mutex_lock(&twl->mutex);
  97. ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
  98. if (ret < 0) {
  99. dev_err(pwmchip_parent(chip), "%s: Failed to read LEDEN\n", pwm->label);
  100. goto out;
  101. }
  102. val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
  103. ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
  104. if (ret < 0)
  105. dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label);
  106. out:
  107. mutex_unlock(&twl->mutex);
  108. return ret;
  109. }
  110. static void twl4030_pwmled_disable(struct pwm_chip *chip,
  111. struct pwm_device *pwm)
  112. {
  113. struct twl_pwmled_chip *twl = to_twl(chip);
  114. int ret;
  115. u8 val;
  116. mutex_lock(&twl->mutex);
  117. ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
  118. if (ret < 0) {
  119. dev_err(pwmchip_parent(chip), "%s: Failed to read LEDEN\n", pwm->label);
  120. goto out;
  121. }
  122. val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
  123. ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
  124. if (ret < 0)
  125. dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label);
  126. out:
  127. mutex_unlock(&twl->mutex);
  128. }
  129. static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  130. const struct pwm_state *state)
  131. {
  132. int ret;
  133. if (state->polarity != PWM_POLARITY_NORMAL)
  134. return -EINVAL;
  135. if (!state->enabled) {
  136. if (pwm->state.enabled)
  137. twl4030_pwmled_disable(chip, pwm);
  138. return 0;
  139. }
  140. /*
  141. * We cannot skip calling ->config even if state->period ==
  142. * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
  143. * because we might have exited early in the last call to
  144. * pwm_apply_might_sleep because of !state->enabled and so the two values in
  145. * pwm->state might not be configured in hardware.
  146. */
  147. ret = twl4030_pwmled_config(chip, pwm,
  148. state->duty_cycle, state->period);
  149. if (ret)
  150. return ret;
  151. if (!pwm->state.enabled)
  152. ret = twl4030_pwmled_enable(chip, pwm);
  153. return ret;
  154. }
  155. static const struct pwm_ops twl4030_pwmled_ops = {
  156. .apply = twl4030_pwmled_apply,
  157. };
  158. static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
  159. int duty_ns, int period_ns)
  160. {
  161. int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
  162. u8 on_time;
  163. int ret;
  164. on_time = duty_cycle & 0xff;
  165. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
  166. TWL6030_LED_PWM_CTRL1);
  167. if (ret < 0)
  168. dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label);
  169. return ret;
  170. }
  171. static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  172. {
  173. struct twl_pwmled_chip *twl = to_twl(chip);
  174. int ret;
  175. u8 val;
  176. mutex_lock(&twl->mutex);
  177. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  178. if (ret < 0) {
  179. dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
  180. pwm->label);
  181. goto out;
  182. }
  183. val &= ~TWL6040_LED_MODE_MASK;
  184. val |= TWL6040_LED_MODE_ON;
  185. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  186. if (ret < 0)
  187. dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label);
  188. out:
  189. mutex_unlock(&twl->mutex);
  190. return ret;
  191. }
  192. static void twl6030_pwmled_disable(struct pwm_chip *chip,
  193. struct pwm_device *pwm)
  194. {
  195. struct twl_pwmled_chip *twl = to_twl(chip);
  196. int ret;
  197. u8 val;
  198. mutex_lock(&twl->mutex);
  199. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  200. if (ret < 0) {
  201. dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
  202. pwm->label);
  203. goto out;
  204. }
  205. val &= ~TWL6040_LED_MODE_MASK;
  206. val |= TWL6040_LED_MODE_OFF;
  207. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  208. if (ret < 0)
  209. dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label);
  210. out:
  211. mutex_unlock(&twl->mutex);
  212. }
  213. static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  214. const struct pwm_state *state)
  215. {
  216. int err;
  217. if (state->polarity != pwm->state.polarity)
  218. return -EINVAL;
  219. if (!state->enabled) {
  220. if (pwm->state.enabled)
  221. twl6030_pwmled_disable(chip, pwm);
  222. return 0;
  223. }
  224. err = twl6030_pwmled_config(chip, pwm,
  225. state->duty_cycle, state->period);
  226. if (err)
  227. return err;
  228. if (!pwm->state.enabled)
  229. err = twl6030_pwmled_enable(chip, pwm);
  230. return err;
  231. }
  232. static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
  233. {
  234. struct twl_pwmled_chip *twl = to_twl(chip);
  235. int ret;
  236. u8 val;
  237. mutex_lock(&twl->mutex);
  238. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  239. if (ret < 0) {
  240. dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
  241. pwm->label);
  242. goto out;
  243. }
  244. val &= ~TWL6040_LED_MODE_MASK;
  245. val |= TWL6040_LED_MODE_OFF;
  246. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  247. if (ret < 0)
  248. dev_err(pwmchip_parent(chip), "%s: Failed to request PWM\n", pwm->label);
  249. out:
  250. mutex_unlock(&twl->mutex);
  251. return ret;
  252. }
  253. static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
  254. {
  255. struct twl_pwmled_chip *twl = to_twl(chip);
  256. int ret;
  257. u8 val;
  258. mutex_lock(&twl->mutex);
  259. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  260. if (ret < 0) {
  261. dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n",
  262. pwm->label);
  263. goto out;
  264. }
  265. val &= ~TWL6040_LED_MODE_MASK;
  266. val |= TWL6040_LED_MODE_HW;
  267. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  268. if (ret < 0)
  269. dev_err(pwmchip_parent(chip), "%s: Failed to free PWM\n", pwm->label);
  270. out:
  271. mutex_unlock(&twl->mutex);
  272. }
  273. static const struct pwm_ops twl6030_pwmled_ops = {
  274. .apply = twl6030_pwmled_apply,
  275. .request = twl6030_pwmled_request,
  276. .free = twl6030_pwmled_free,
  277. };
  278. static int twl_pwmled_probe(struct platform_device *pdev)
  279. {
  280. struct pwm_chip *chip;
  281. struct twl_pwmled_chip *twl;
  282. unsigned int npwm;
  283. const struct pwm_ops *ops;
  284. if (twl_class_is_4030()) {
  285. ops = &twl4030_pwmled_ops;
  286. npwm = 2;
  287. } else {
  288. ops = &twl6030_pwmled_ops;
  289. npwm = 1;
  290. }
  291. chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*twl));
  292. if (IS_ERR(chip))
  293. return PTR_ERR(chip);
  294. twl = to_twl(chip);
  295. chip->ops = ops;
  296. mutex_init(&twl->mutex);
  297. return devm_pwmchip_add(&pdev->dev, chip);
  298. }
  299. #ifdef CONFIG_OF
  300. static const struct of_device_id twl_pwmled_of_match[] = {
  301. { .compatible = "ti,twl4030-pwmled" },
  302. { .compatible = "ti,twl6030-pwmled" },
  303. { },
  304. };
  305. MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
  306. #endif
  307. static struct platform_driver twl_pwmled_driver = {
  308. .driver = {
  309. .name = "twl-pwmled",
  310. .of_match_table = of_match_ptr(twl_pwmled_of_match),
  311. },
  312. .probe = twl_pwmled_probe,
  313. };
  314. module_platform_driver(twl_pwmled_driver);
  315. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  316. MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
  317. MODULE_ALIAS("platform:twl-pwmled");
  318. MODULE_LICENSE("GPL");