pwm-sun4i.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Driver for Allwinner sun4i Pulse Width Modulation Controller
  3. *
  4. * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com>
  5. *
  6. * Licensed under GPLv2.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/time.h>
  22. #define PWM_CTRL_REG 0x0
  23. #define PWM_CH_PRD_BASE 0x4
  24. #define PWM_CH_PRD_OFFSET 0x4
  25. #define PWM_CH_PRD(ch) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFFSET * (ch))
  26. #define PWMCH_OFFSET 15
  27. #define PWM_PRESCAL_MASK GENMASK(3, 0)
  28. #define PWM_PRESCAL_OFF 0
  29. #define PWM_EN BIT(4)
  30. #define PWM_ACT_STATE BIT(5)
  31. #define PWM_CLK_GATING BIT(6)
  32. #define PWM_MODE BIT(7)
  33. #define PWM_PULSE BIT(8)
  34. #define PWM_BYPASS BIT(9)
  35. #define PWM_RDY_BASE 28
  36. #define PWM_RDY_OFFSET 1
  37. #define PWM_RDY(ch) BIT(PWM_RDY_BASE + PWM_RDY_OFFSET * (ch))
  38. #define PWM_PRD(prd) (((prd) - 1) << 16)
  39. #define PWM_PRD_MASK GENMASK(15, 0)
  40. #define PWM_DTY_MASK GENMASK(15, 0)
  41. #define PWM_REG_PRD(reg) ((((reg) >> 16) & PWM_PRD_MASK) + 1)
  42. #define PWM_REG_DTY(reg) ((reg) & PWM_DTY_MASK)
  43. #define PWM_REG_PRESCAL(reg, chan) (((reg) >> ((chan) * PWMCH_OFFSET)) & PWM_PRESCAL_MASK)
  44. #define BIT_CH(bit, chan) ((bit) << ((chan) * PWMCH_OFFSET))
  45. static const u32 prescaler_table[] = {
  46. 120,
  47. 180,
  48. 240,
  49. 360,
  50. 480,
  51. 0,
  52. 0,
  53. 0,
  54. 12000,
  55. 24000,
  56. 36000,
  57. 48000,
  58. 72000,
  59. 0,
  60. 0,
  61. 0, /* Actually 1 but tested separately */
  62. };
  63. struct sun4i_pwm_data {
  64. bool has_prescaler_bypass;
  65. unsigned int npwm;
  66. };
  67. struct sun4i_pwm_chip {
  68. struct pwm_chip chip;
  69. struct clk *clk;
  70. void __iomem *base;
  71. spinlock_t ctrl_lock;
  72. const struct sun4i_pwm_data *data;
  73. unsigned long next_period[2];
  74. bool needs_delay[2];
  75. };
  76. static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
  77. {
  78. return container_of(chip, struct sun4i_pwm_chip, chip);
  79. }
  80. static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip,
  81. unsigned long offset)
  82. {
  83. return readl(chip->base + offset);
  84. }
  85. static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip,
  86. u32 val, unsigned long offset)
  87. {
  88. writel(val, chip->base + offset);
  89. }
  90. static void sun4i_pwm_get_state(struct pwm_chip *chip,
  91. struct pwm_device *pwm,
  92. struct pwm_state *state)
  93. {
  94. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  95. u64 clk_rate, tmp;
  96. u32 val;
  97. unsigned int prescaler;
  98. clk_rate = clk_get_rate(sun4i_pwm->clk);
  99. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  100. if ((PWM_REG_PRESCAL(val, pwm->hwpwm) == PWM_PRESCAL_MASK) &&
  101. sun4i_pwm->data->has_prescaler_bypass)
  102. prescaler = 1;
  103. else
  104. prescaler = prescaler_table[PWM_REG_PRESCAL(val, pwm->hwpwm)];
  105. if (prescaler == 0)
  106. return;
  107. if (val & BIT_CH(PWM_ACT_STATE, pwm->hwpwm))
  108. state->polarity = PWM_POLARITY_NORMAL;
  109. else
  110. state->polarity = PWM_POLARITY_INVERSED;
  111. if ((val & BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm)) ==
  112. BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm))
  113. state->enabled = true;
  114. else
  115. state->enabled = false;
  116. val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm));
  117. tmp = prescaler * NSEC_PER_SEC * PWM_REG_DTY(val);
  118. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  119. tmp = prescaler * NSEC_PER_SEC * PWM_REG_PRD(val);
  120. state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  121. }
  122. static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
  123. struct pwm_state *state,
  124. u32 *dty, u32 *prd, unsigned int *prsclr)
  125. {
  126. u64 clk_rate, div = 0;
  127. unsigned int pval, prescaler = 0;
  128. clk_rate = clk_get_rate(sun4i_pwm->clk);
  129. if (sun4i_pwm->data->has_prescaler_bypass) {
  130. /* First, test without any prescaler when available */
  131. prescaler = PWM_PRESCAL_MASK;
  132. pval = 1;
  133. /*
  134. * When not using any prescaler, the clock period in nanoseconds
  135. * is not an integer so round it half up instead of
  136. * truncating to get less surprising values.
  137. */
  138. div = clk_rate * state->period + NSEC_PER_SEC / 2;
  139. do_div(div, NSEC_PER_SEC);
  140. if (div - 1 > PWM_PRD_MASK)
  141. prescaler = 0;
  142. }
  143. if (prescaler == 0) {
  144. /* Go up from the first divider */
  145. for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) {
  146. if (!prescaler_table[prescaler])
  147. continue;
  148. pval = prescaler_table[prescaler];
  149. div = clk_rate;
  150. do_div(div, pval);
  151. div = div * state->period;
  152. do_div(div, NSEC_PER_SEC);
  153. if (div - 1 <= PWM_PRD_MASK)
  154. break;
  155. }
  156. if (div - 1 > PWM_PRD_MASK)
  157. return -EINVAL;
  158. }
  159. *prd = div;
  160. div *= state->duty_cycle;
  161. do_div(div, state->period);
  162. *dty = div;
  163. *prsclr = prescaler;
  164. div = (u64)pval * NSEC_PER_SEC * *prd;
  165. state->period = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
  166. div = (u64)pval * NSEC_PER_SEC * *dty;
  167. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
  168. return 0;
  169. }
  170. static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  171. struct pwm_state *state)
  172. {
  173. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  174. struct pwm_state cstate;
  175. u32 ctrl;
  176. int ret;
  177. unsigned int delay_us;
  178. unsigned long now;
  179. pwm_get_state(pwm, &cstate);
  180. if (!cstate.enabled) {
  181. ret = clk_prepare_enable(sun4i_pwm->clk);
  182. if (ret) {
  183. dev_err(chip->dev, "failed to enable PWM clock\n");
  184. return ret;
  185. }
  186. }
  187. spin_lock(&sun4i_pwm->ctrl_lock);
  188. ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  189. if ((cstate.period != state->period) ||
  190. (cstate.duty_cycle != state->duty_cycle)) {
  191. u32 period, duty, val;
  192. unsigned int prescaler;
  193. ret = sun4i_pwm_calculate(sun4i_pwm, state,
  194. &duty, &period, &prescaler);
  195. if (ret) {
  196. dev_err(chip->dev, "period exceeds the maximum value\n");
  197. spin_unlock(&sun4i_pwm->ctrl_lock);
  198. if (!cstate.enabled)
  199. clk_disable_unprepare(sun4i_pwm->clk);
  200. return ret;
  201. }
  202. if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
  203. /* Prescaler changed, the clock has to be gated */
  204. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  205. sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  206. ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  207. ctrl |= BIT_CH(prescaler, pwm->hwpwm);
  208. }
  209. val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
  210. sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
  211. sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
  212. usecs_to_jiffies(cstate.period / 1000 + 1);
  213. sun4i_pwm->needs_delay[pwm->hwpwm] = true;
  214. }
  215. if (state->polarity != PWM_POLARITY_NORMAL)
  216. ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  217. else
  218. ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  219. ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  220. if (state->enabled) {
  221. ctrl |= BIT_CH(PWM_EN, pwm->hwpwm);
  222. } else if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
  223. ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  224. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  225. }
  226. sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  227. spin_unlock(&sun4i_pwm->ctrl_lock);
  228. if (state->enabled)
  229. return 0;
  230. if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
  231. clk_disable_unprepare(sun4i_pwm->clk);
  232. return 0;
  233. }
  234. /* We need a full period to elapse before disabling the channel. */
  235. now = jiffies;
  236. if (sun4i_pwm->needs_delay[pwm->hwpwm] &&
  237. time_before(now, sun4i_pwm->next_period[pwm->hwpwm])) {
  238. delay_us = jiffies_to_usecs(sun4i_pwm->next_period[pwm->hwpwm] -
  239. now);
  240. if ((delay_us / 500) > MAX_UDELAY_MS)
  241. msleep(delay_us / 1000 + 1);
  242. else
  243. usleep_range(delay_us, delay_us * 2);
  244. }
  245. sun4i_pwm->needs_delay[pwm->hwpwm] = false;
  246. spin_lock(&sun4i_pwm->ctrl_lock);
  247. ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  248. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  249. ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  250. sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  251. spin_unlock(&sun4i_pwm->ctrl_lock);
  252. clk_disable_unprepare(sun4i_pwm->clk);
  253. return 0;
  254. }
  255. static const struct pwm_ops sun4i_pwm_ops = {
  256. .apply = sun4i_pwm_apply,
  257. .get_state = sun4i_pwm_get_state,
  258. .owner = THIS_MODULE,
  259. };
  260. static const struct sun4i_pwm_data sun4i_pwm_dual_nobypass = {
  261. .has_prescaler_bypass = false,
  262. .npwm = 2,
  263. };
  264. static const struct sun4i_pwm_data sun4i_pwm_dual_bypass = {
  265. .has_prescaler_bypass = true,
  266. .npwm = 2,
  267. };
  268. static const struct sun4i_pwm_data sun4i_pwm_single_bypass = {
  269. .has_prescaler_bypass = true,
  270. .npwm = 1,
  271. };
  272. static const struct of_device_id sun4i_pwm_dt_ids[] = {
  273. {
  274. .compatible = "allwinner,sun4i-a10-pwm",
  275. .data = &sun4i_pwm_dual_nobypass,
  276. }, {
  277. .compatible = "allwinner,sun5i-a10s-pwm",
  278. .data = &sun4i_pwm_dual_bypass,
  279. }, {
  280. .compatible = "allwinner,sun5i-a13-pwm",
  281. .data = &sun4i_pwm_single_bypass,
  282. }, {
  283. .compatible = "allwinner,sun7i-a20-pwm",
  284. .data = &sun4i_pwm_dual_bypass,
  285. }, {
  286. .compatible = "allwinner,sun8i-h3-pwm",
  287. .data = &sun4i_pwm_single_bypass,
  288. }, {
  289. /* sentinel */
  290. },
  291. };
  292. MODULE_DEVICE_TABLE(of, sun4i_pwm_dt_ids);
  293. static int sun4i_pwm_probe(struct platform_device *pdev)
  294. {
  295. struct sun4i_pwm_chip *pwm;
  296. struct resource *res;
  297. int ret;
  298. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  299. if (!pwm)
  300. return -ENOMEM;
  301. pwm->data = of_device_get_match_data(&pdev->dev);
  302. if (!pwm->data)
  303. return -ENODEV;
  304. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  305. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  306. if (IS_ERR(pwm->base))
  307. return PTR_ERR(pwm->base);
  308. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  309. if (IS_ERR(pwm->clk))
  310. return PTR_ERR(pwm->clk);
  311. pwm->chip.dev = &pdev->dev;
  312. pwm->chip.ops = &sun4i_pwm_ops;
  313. pwm->chip.base = -1;
  314. pwm->chip.npwm = pwm->data->npwm;
  315. pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  316. pwm->chip.of_pwm_n_cells = 3;
  317. spin_lock_init(&pwm->ctrl_lock);
  318. ret = pwmchip_add(&pwm->chip);
  319. if (ret < 0) {
  320. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  321. return ret;
  322. }
  323. platform_set_drvdata(pdev, pwm);
  324. return 0;
  325. }
  326. static int sun4i_pwm_remove(struct platform_device *pdev)
  327. {
  328. struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
  329. return pwmchip_remove(&pwm->chip);
  330. }
  331. static struct platform_driver sun4i_pwm_driver = {
  332. .driver = {
  333. .name = "sun4i-pwm",
  334. .of_match_table = sun4i_pwm_dt_ids,
  335. },
  336. .probe = sun4i_pwm_probe,
  337. .remove = sun4i_pwm_remove,
  338. };
  339. module_platform_driver(sun4i_pwm_driver);
  340. MODULE_ALIAS("platform:sun4i-pwm");
  341. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  342. MODULE_DESCRIPTION("Allwinner sun4i PWM driver");
  343. MODULE_LICENSE("GPL v2");