pwm-twl-led.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <[email protected]>
  7. *
  8. * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
  9. * Hemanth V <[email protected]>
  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 pwm_chip chip;
  58. struct mutex mutex;
  59. };
  60. static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
  61. {
  62. return container_of(chip, struct twl_pwmled_chip, chip);
  63. }
  64. static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
  65. int duty_ns, int period_ns)
  66. {
  67. int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
  68. u8 pwm_config[2] = { 1, 0 };
  69. int base, ret;
  70. /*
  71. * To configure the duty period:
  72. * On-cycle is set to 1 (the minimum allowed value)
  73. * The off time of 0 is not configurable, so the mapping is:
  74. * 0 -> off cycle = 2,
  75. * 1 -> off cycle = 2,
  76. * 2 -> off cycle = 3,
  77. * 126 - > off cycle 127,
  78. * 127 - > off cycle 1
  79. * When on cycle == off cycle the PWM will be always on
  80. */
  81. if (duty_cycle == 1)
  82. duty_cycle = 2;
  83. else if (duty_cycle > TWL4030_LED_MAX)
  84. duty_cycle = 1;
  85. base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
  86. pwm_config[1] = duty_cycle;
  87. ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
  88. if (ret < 0)
  89. dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
  90. return ret;
  91. }
  92. static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  93. {
  94. struct twl_pwmled_chip *twl = to_twl(chip);
  95. int ret;
  96. u8 val;
  97. mutex_lock(&twl->mutex);
  98. ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
  99. if (ret < 0) {
  100. dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
  101. goto out;
  102. }
  103. val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
  104. ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
  105. if (ret < 0)
  106. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  107. out:
  108. mutex_unlock(&twl->mutex);
  109. return ret;
  110. }
  111. static void twl4030_pwmled_disable(struct pwm_chip *chip,
  112. struct pwm_device *pwm)
  113. {
  114. struct twl_pwmled_chip *twl = to_twl(chip);
  115. int ret;
  116. u8 val;
  117. mutex_lock(&twl->mutex);
  118. ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
  119. if (ret < 0) {
  120. dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
  121. goto out;
  122. }
  123. val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
  124. ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
  125. if (ret < 0)
  126. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  127. out:
  128. mutex_unlock(&twl->mutex);
  129. }
  130. static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  131. const struct pwm_state *state)
  132. {
  133. int ret;
  134. if (state->polarity != PWM_POLARITY_NORMAL)
  135. return -EINVAL;
  136. if (!state->enabled) {
  137. if (pwm->state.enabled)
  138. twl4030_pwmled_disable(chip, pwm);
  139. return 0;
  140. }
  141. /*
  142. * We cannot skip calling ->config even if state->period ==
  143. * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
  144. * because we might have exited early in the last call to
  145. * pwm_apply_state because of !state->enabled and so the two values in
  146. * pwm->state might not be configured in hardware.
  147. */
  148. ret = twl4030_pwmled_config(pwm->chip, pwm,
  149. state->duty_cycle, state->period);
  150. if (ret)
  151. return ret;
  152. if (!pwm->state.enabled)
  153. ret = twl4030_pwmled_enable(chip, pwm);
  154. return ret;
  155. }
  156. static const struct pwm_ops twl4030_pwmled_ops = {
  157. .apply = twl4030_pwmled_apply,
  158. .owner = THIS_MODULE,
  159. };
  160. static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
  161. int duty_ns, int period_ns)
  162. {
  163. int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
  164. u8 on_time;
  165. int ret;
  166. on_time = duty_cycle & 0xff;
  167. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
  168. TWL6030_LED_PWM_CTRL1);
  169. if (ret < 0)
  170. dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
  171. return ret;
  172. }
  173. static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  174. {
  175. struct twl_pwmled_chip *twl = to_twl(chip);
  176. int ret;
  177. u8 val;
  178. mutex_lock(&twl->mutex);
  179. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  180. if (ret < 0) {
  181. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  182. pwm->label);
  183. goto out;
  184. }
  185. val &= ~TWL6040_LED_MODE_MASK;
  186. val |= TWL6040_LED_MODE_ON;
  187. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  188. if (ret < 0)
  189. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  190. out:
  191. mutex_unlock(&twl->mutex);
  192. return ret;
  193. }
  194. static void twl6030_pwmled_disable(struct pwm_chip *chip,
  195. struct pwm_device *pwm)
  196. {
  197. struct twl_pwmled_chip *twl = to_twl(chip);
  198. int ret;
  199. u8 val;
  200. mutex_lock(&twl->mutex);
  201. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  202. if (ret < 0) {
  203. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  204. pwm->label);
  205. goto out;
  206. }
  207. val &= ~TWL6040_LED_MODE_MASK;
  208. val |= TWL6040_LED_MODE_OFF;
  209. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  210. if (ret < 0)
  211. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  212. out:
  213. mutex_unlock(&twl->mutex);
  214. }
  215. static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  216. const struct pwm_state *state)
  217. {
  218. int err;
  219. if (state->polarity != pwm->state.polarity)
  220. return -EINVAL;
  221. if (!state->enabled) {
  222. if (pwm->state.enabled)
  223. twl6030_pwmled_disable(chip, pwm);
  224. return 0;
  225. }
  226. err = twl6030_pwmled_config(pwm->chip, pwm,
  227. state->duty_cycle, state->period);
  228. if (err)
  229. return err;
  230. if (!pwm->state.enabled)
  231. err = twl6030_pwmled_enable(chip, pwm);
  232. return err;
  233. }
  234. static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
  235. {
  236. struct twl_pwmled_chip *twl = to_twl(chip);
  237. int ret;
  238. u8 val;
  239. mutex_lock(&twl->mutex);
  240. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  241. if (ret < 0) {
  242. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  243. pwm->label);
  244. goto out;
  245. }
  246. val &= ~TWL6040_LED_MODE_MASK;
  247. val |= TWL6040_LED_MODE_OFF;
  248. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  249. if (ret < 0)
  250. dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
  251. out:
  252. mutex_unlock(&twl->mutex);
  253. return ret;
  254. }
  255. static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
  256. {
  257. struct twl_pwmled_chip *twl = to_twl(chip);
  258. int ret;
  259. u8 val;
  260. mutex_lock(&twl->mutex);
  261. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  262. if (ret < 0) {
  263. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  264. pwm->label);
  265. goto out;
  266. }
  267. val &= ~TWL6040_LED_MODE_MASK;
  268. val |= TWL6040_LED_MODE_HW;
  269. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  270. if (ret < 0)
  271. dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
  272. out:
  273. mutex_unlock(&twl->mutex);
  274. }
  275. static const struct pwm_ops twl6030_pwmled_ops = {
  276. .apply = twl6030_pwmled_apply,
  277. .request = twl6030_pwmled_request,
  278. .free = twl6030_pwmled_free,
  279. .owner = THIS_MODULE,
  280. };
  281. static int twl_pwmled_probe(struct platform_device *pdev)
  282. {
  283. struct twl_pwmled_chip *twl;
  284. twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
  285. if (!twl)
  286. return -ENOMEM;
  287. if (twl_class_is_4030()) {
  288. twl->chip.ops = &twl4030_pwmled_ops;
  289. twl->chip.npwm = 2;
  290. } else {
  291. twl->chip.ops = &twl6030_pwmled_ops;
  292. twl->chip.npwm = 1;
  293. }
  294. twl->chip.dev = &pdev->dev;
  295. mutex_init(&twl->mutex);
  296. return devm_pwmchip_add(&pdev->dev, &twl->chip);
  297. }
  298. #ifdef CONFIG_OF
  299. static const struct of_device_id twl_pwmled_of_match[] = {
  300. { .compatible = "ti,twl4030-pwmled" },
  301. { .compatible = "ti,twl6030-pwmled" },
  302. { },
  303. };
  304. MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
  305. #endif
  306. static struct platform_driver twl_pwmled_driver = {
  307. .driver = {
  308. .name = "twl-pwmled",
  309. .of_match_table = of_match_ptr(twl_pwmled_of_match),
  310. },
  311. .probe = twl_pwmled_probe,
  312. };
  313. module_platform_driver(twl_pwmled_driver);
  314. MODULE_AUTHOR("Peter Ujfalusi <[email protected]>");
  315. MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
  316. MODULE_ALIAS("platform:twl-pwmled");
  317. MODULE_LICENSE("GPL");