pwm-stmpe.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2016 Linaro Ltd.
  3. *
  4. * Author: Linus Walleij <linus.walleij@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/mfd/stmpe.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #define STMPE24XX_PWMCS 0x30
  21. #define PWMCS_EN_PWM0 BIT(0)
  22. #define PWMCS_EN_PWM1 BIT(1)
  23. #define PWMCS_EN_PWM2 BIT(2)
  24. #define STMPE24XX_PWMIC0 0x38
  25. #define STMPE24XX_PWMIC1 0x39
  26. #define STMPE24XX_PWMIC2 0x3a
  27. #define STMPE_PWM_24XX_PINBASE 21
  28. struct stmpe_pwm {
  29. struct stmpe *stmpe;
  30. struct pwm_chip chip;
  31. u8 last_duty;
  32. };
  33. static inline struct stmpe_pwm *to_stmpe_pwm(struct pwm_chip *chip)
  34. {
  35. return container_of(chip, struct stmpe_pwm, chip);
  36. }
  37. static int stmpe_24xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  38. {
  39. struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
  40. u8 value;
  41. int ret;
  42. ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS);
  43. if (ret < 0) {
  44. dev_err(chip->dev, "error reading PWM#%u control\n",
  45. pwm->hwpwm);
  46. return ret;
  47. }
  48. value = ret | BIT(pwm->hwpwm);
  49. ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value);
  50. if (ret) {
  51. dev_err(chip->dev, "error writing PWM#%u control\n",
  52. pwm->hwpwm);
  53. return ret;
  54. }
  55. return 0;
  56. }
  57. static void stmpe_24xx_pwm_disable(struct pwm_chip *chip,
  58. struct pwm_device *pwm)
  59. {
  60. struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
  61. u8 value;
  62. int ret;
  63. ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS);
  64. if (ret < 0) {
  65. dev_err(chip->dev, "error reading PWM#%u control\n",
  66. pwm->hwpwm);
  67. return;
  68. }
  69. value = ret & ~BIT(pwm->hwpwm);
  70. ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value);
  71. if (ret) {
  72. dev_err(chip->dev, "error writing PWM#%u control\n",
  73. pwm->hwpwm);
  74. return;
  75. }
  76. }
  77. /* STMPE 24xx PWM instructions */
  78. #define SMAX 0x007f
  79. #define SMIN 0x00ff
  80. #define GTS 0x0000
  81. #define LOAD BIT(14) /* Only available on 2403 */
  82. #define RAMPUP 0x0000
  83. #define RAMPDOWN BIT(7)
  84. #define PRESCALE_512 BIT(14)
  85. #define STEPTIME_1 BIT(8)
  86. #define BRANCH (BIT(15) | BIT(13))
  87. static int stmpe_24xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  88. int duty_ns, int period_ns)
  89. {
  90. struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
  91. unsigned int i, pin;
  92. u16 program[3] = {
  93. SMAX,
  94. GTS,
  95. GTS,
  96. };
  97. u8 offset;
  98. int ret;
  99. /* Make sure we are disabled */
  100. if (pwm_is_enabled(pwm)) {
  101. stmpe_24xx_pwm_disable(chip, pwm);
  102. } else {
  103. /* Connect the PWM to the pin */
  104. pin = pwm->hwpwm;
  105. /* On STMPE2401 and 2403 pins 21,22,23 are used */
  106. if (stmpe_pwm->stmpe->partnum == STMPE2401 ||
  107. stmpe_pwm->stmpe->partnum == STMPE2403)
  108. pin += STMPE_PWM_24XX_PINBASE;
  109. ret = stmpe_set_altfunc(stmpe_pwm->stmpe, BIT(pin),
  110. STMPE_BLOCK_PWM);
  111. if (ret) {
  112. dev_err(chip->dev, "unable to connect PWM#%u to pin\n",
  113. pwm->hwpwm);
  114. return ret;
  115. }
  116. }
  117. /* STMPE24XX */
  118. switch (pwm->hwpwm) {
  119. case 0:
  120. offset = STMPE24XX_PWMIC0;
  121. break;
  122. case 1:
  123. offset = STMPE24XX_PWMIC1;
  124. break;
  125. case 2:
  126. offset = STMPE24XX_PWMIC2;
  127. break;
  128. default:
  129. /* Should not happen as npwm is 3 */
  130. return -ENODEV;
  131. }
  132. dev_dbg(chip->dev, "PWM#%u: config duty %d ns, period %d ns\n",
  133. pwm->hwpwm, duty_ns, period_ns);
  134. if (duty_ns == 0) {
  135. if (stmpe_pwm->stmpe->partnum == STMPE2401)
  136. program[0] = SMAX; /* off all the time */
  137. if (stmpe_pwm->stmpe->partnum == STMPE2403)
  138. program[0] = LOAD | 0xff; /* LOAD 0xff */
  139. stmpe_pwm->last_duty = 0x00;
  140. } else if (duty_ns == period_ns) {
  141. if (stmpe_pwm->stmpe->partnum == STMPE2401)
  142. program[0] = SMIN; /* on all the time */
  143. if (stmpe_pwm->stmpe->partnum == STMPE2403)
  144. program[0] = LOAD | 0x00; /* LOAD 0x00 */
  145. stmpe_pwm->last_duty = 0xff;
  146. } else {
  147. u8 value, last = stmpe_pwm->last_duty;
  148. unsigned long duty;
  149. /*
  150. * Counter goes from 0x00 to 0xff repeatedly at 32768 Hz,
  151. * (means a period of 30517 ns) then this is compared to the
  152. * counter from the ramp, if this is >= PWM counter the output
  153. * is high. With LOAD we can define how much of the cycle it
  154. * is on.
  155. *
  156. * Prescale = 0 -> 2 kHz -> T = 1/f = 488281.25 ns
  157. */
  158. /* Scale to 0..0xff */
  159. duty = duty_ns * 256;
  160. duty = DIV_ROUND_CLOSEST(duty, period_ns);
  161. value = duty;
  162. if (value == last) {
  163. /* Run the old program */
  164. if (pwm_is_enabled(pwm))
  165. stmpe_24xx_pwm_enable(chip, pwm);
  166. return 0;
  167. } else if (stmpe_pwm->stmpe->partnum == STMPE2403) {
  168. /* STMPE2403 can simply set the right PWM value */
  169. program[0] = LOAD | value;
  170. program[1] = 0x0000;
  171. } else if (stmpe_pwm->stmpe->partnum == STMPE2401) {
  172. /* STMPE2401 need a complex program */
  173. u16 incdec = 0x0000;
  174. if (last < value)
  175. /* Count up */
  176. incdec = RAMPUP | (value - last);
  177. else
  178. /* Count down */
  179. incdec = RAMPDOWN | (last - value);
  180. /* Step to desired value, smoothly */
  181. program[0] = PRESCALE_512 | STEPTIME_1 | incdec;
  182. /* Loop eternally to 0x00 */
  183. program[1] = BRANCH;
  184. }
  185. dev_dbg(chip->dev,
  186. "PWM#%u: value = %02x, last_duty = %02x, program=%04x,%04x,%04x\n",
  187. pwm->hwpwm, value, last, program[0], program[1],
  188. program[2]);
  189. stmpe_pwm->last_duty = value;
  190. }
  191. /*
  192. * We can write programs of up to 64 16-bit words into this channel.
  193. */
  194. for (i = 0; i < ARRAY_SIZE(program); i++) {
  195. u8 value;
  196. value = (program[i] >> 8) & 0xff;
  197. ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value);
  198. if (ret) {
  199. dev_err(chip->dev, "error writing register %02x: %d\n",
  200. offset, ret);
  201. return ret;
  202. }
  203. value = program[i] & 0xff;
  204. ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value);
  205. if (ret) {
  206. dev_err(chip->dev, "error writing register %02x: %d\n",
  207. offset, ret);
  208. return ret;
  209. }
  210. }
  211. /* If we were enabled, re-enable this PWM */
  212. if (pwm_is_enabled(pwm))
  213. stmpe_24xx_pwm_enable(chip, pwm);
  214. /* Sleep for 200ms so we're sure it will take effect */
  215. msleep(200);
  216. dev_dbg(chip->dev, "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i);
  217. return 0;
  218. }
  219. static const struct pwm_ops stmpe_24xx_pwm_ops = {
  220. .config = stmpe_24xx_pwm_config,
  221. .enable = stmpe_24xx_pwm_enable,
  222. .disable = stmpe_24xx_pwm_disable,
  223. .owner = THIS_MODULE,
  224. };
  225. static int __init stmpe_pwm_probe(struct platform_device *pdev)
  226. {
  227. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  228. struct stmpe_pwm *pwm;
  229. int ret;
  230. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  231. if (!pwm)
  232. return -ENOMEM;
  233. pwm->stmpe = stmpe;
  234. pwm->chip.dev = &pdev->dev;
  235. pwm->chip.base = -1;
  236. if (stmpe->partnum == STMPE2401 || stmpe->partnum == STMPE2403) {
  237. pwm->chip.ops = &stmpe_24xx_pwm_ops;
  238. pwm->chip.npwm = 3;
  239. } else {
  240. if (stmpe->partnum == STMPE1601)
  241. dev_err(&pdev->dev, "STMPE1601 not yet supported\n");
  242. else
  243. dev_err(&pdev->dev, "Unknown STMPE PWM\n");
  244. return -ENODEV;
  245. }
  246. ret = stmpe_enable(stmpe, STMPE_BLOCK_PWM);
  247. if (ret)
  248. return ret;
  249. ret = pwmchip_add(&pwm->chip);
  250. if (ret) {
  251. stmpe_disable(stmpe, STMPE_BLOCK_PWM);
  252. return ret;
  253. }
  254. platform_set_drvdata(pdev, pwm);
  255. return 0;
  256. }
  257. static struct platform_driver stmpe_pwm_driver = {
  258. .driver = {
  259. .name = "stmpe-pwm",
  260. },
  261. };
  262. builtin_platform_driver_probe(stmpe_pwm_driver, stmpe_pwm_probe);