pwm-sun4i.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. bool has_rdy;
  66. unsigned int npwm;
  67. };
  68. struct sun4i_pwm_chip {
  69. struct pwm_chip chip;
  70. struct clk *clk;
  71. void __iomem *base;
  72. spinlock_t ctrl_lock;
  73. const struct sun4i_pwm_data *data;
  74. unsigned long next_period[2];
  75. bool needs_delay[2];
  76. };
  77. static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
  78. {
  79. return container_of(chip, struct sun4i_pwm_chip, chip);
  80. }
  81. static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip,
  82. unsigned long offset)
  83. {
  84. return readl(chip->base + offset);
  85. }
  86. static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip,
  87. u32 val, unsigned long offset)
  88. {
  89. writel(val, chip->base + offset);
  90. }
  91. static void sun4i_pwm_get_state(struct pwm_chip *chip,
  92. struct pwm_device *pwm,
  93. struct pwm_state *state)
  94. {
  95. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  96. u64 clk_rate, tmp;
  97. u32 val;
  98. unsigned int prescaler;
  99. clk_rate = clk_get_rate(sun4i_pwm->clk);
  100. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  101. if ((val == PWM_PRESCAL_MASK) && 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. state->enabled = true;
  113. else
  114. state->enabled = false;
  115. val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm));
  116. tmp = prescaler * NSEC_PER_SEC * PWM_REG_DTY(val);
  117. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  118. tmp = prescaler * NSEC_PER_SEC * PWM_REG_PRD(val);
  119. state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  120. }
  121. static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
  122. struct pwm_state *state,
  123. u32 *dty, u32 *prd, unsigned int *prsclr)
  124. {
  125. u64 clk_rate, div = 0;
  126. unsigned int pval, prescaler = 0;
  127. clk_rate = clk_get_rate(sun4i_pwm->clk);
  128. if (sun4i_pwm->data->has_prescaler_bypass) {
  129. /* First, test without any prescaler when available */
  130. prescaler = PWM_PRESCAL_MASK;
  131. pval = 1;
  132. /*
  133. * When not using any prescaler, the clock period in nanoseconds
  134. * is not an integer so round it half up instead of
  135. * truncating to get less surprising values.
  136. */
  137. div = clk_rate * state->period + NSEC_PER_SEC / 2;
  138. do_div(div, NSEC_PER_SEC);
  139. if (div - 1 > PWM_PRD_MASK)
  140. prescaler = 0;
  141. }
  142. if (prescaler == 0) {
  143. /* Go up from the first divider */
  144. for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) {
  145. if (!prescaler_table[prescaler])
  146. continue;
  147. pval = prescaler_table[prescaler];
  148. div = clk_rate;
  149. do_div(div, pval);
  150. div = div * state->period;
  151. do_div(div, NSEC_PER_SEC);
  152. if (div - 1 <= PWM_PRD_MASK)
  153. break;
  154. }
  155. if (div - 1 > PWM_PRD_MASK)
  156. return -EINVAL;
  157. }
  158. *prd = div;
  159. div *= state->duty_cycle;
  160. do_div(div, state->period);
  161. *dty = div;
  162. *prsclr = prescaler;
  163. div = (u64)pval * NSEC_PER_SEC * *prd;
  164. state->period = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
  165. div = (u64)pval * NSEC_PER_SEC * *dty;
  166. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
  167. return 0;
  168. }
  169. static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  170. struct pwm_state *state)
  171. {
  172. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  173. struct pwm_state cstate;
  174. u32 ctrl;
  175. int ret;
  176. unsigned int delay_us;
  177. unsigned long now;
  178. pwm_get_state(pwm, &cstate);
  179. if (!cstate.enabled) {
  180. ret = clk_prepare_enable(sun4i_pwm->clk);
  181. if (ret) {
  182. dev_err(chip->dev, "failed to enable PWM clock\n");
  183. return ret;
  184. }
  185. }
  186. spin_lock(&sun4i_pwm->ctrl_lock);
  187. ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  188. if ((cstate.period != state->period) ||
  189. (cstate.duty_cycle != state->duty_cycle)) {
  190. u32 period, duty, val;
  191. unsigned int prescaler;
  192. ret = sun4i_pwm_calculate(sun4i_pwm, state,
  193. &duty, &period, &prescaler);
  194. if (ret) {
  195. dev_err(chip->dev, "period exceeds the maximum value\n");
  196. spin_unlock(&sun4i_pwm->ctrl_lock);
  197. if (!cstate.enabled)
  198. clk_disable_unprepare(sun4i_pwm->clk);
  199. return ret;
  200. }
  201. if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
  202. /* Prescaler changed, the clock has to be gated */
  203. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  204. sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  205. ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  206. ctrl |= BIT_CH(prescaler, pwm->hwpwm);
  207. }
  208. val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
  209. sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
  210. sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
  211. usecs_to_jiffies(cstate.period / 1000 + 1);
  212. sun4i_pwm->needs_delay[pwm->hwpwm] = true;
  213. }
  214. if (state->polarity != PWM_POLARITY_NORMAL)
  215. ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  216. else
  217. ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  218. ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  219. if (state->enabled) {
  220. ctrl |= BIT_CH(PWM_EN, pwm->hwpwm);
  221. } else if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
  222. ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  223. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  224. }
  225. sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  226. spin_unlock(&sun4i_pwm->ctrl_lock);
  227. if (state->enabled)
  228. return 0;
  229. if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
  230. clk_disable_unprepare(sun4i_pwm->clk);
  231. return 0;
  232. }
  233. /* We need a full period to elapse before disabling the channel. */
  234. now = jiffies;
  235. if (sun4i_pwm->needs_delay[pwm->hwpwm] &&
  236. time_before(now, sun4i_pwm->next_period[pwm->hwpwm])) {
  237. delay_us = jiffies_to_usecs(sun4i_pwm->next_period[pwm->hwpwm] -
  238. now);
  239. if ((delay_us / 500) > MAX_UDELAY_MS)
  240. msleep(delay_us / 1000 + 1);
  241. else
  242. usleep_range(delay_us, delay_us * 2);
  243. }
  244. sun4i_pwm->needs_delay[pwm->hwpwm] = false;
  245. spin_lock(&sun4i_pwm->ctrl_lock);
  246. ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  247. ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  248. ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  249. sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  250. spin_unlock(&sun4i_pwm->ctrl_lock);
  251. clk_disable_unprepare(sun4i_pwm->clk);
  252. return 0;
  253. }
  254. static const struct pwm_ops sun4i_pwm_ops = {
  255. .apply = sun4i_pwm_apply,
  256. .get_state = sun4i_pwm_get_state,
  257. .owner = THIS_MODULE,
  258. };
  259. static const struct sun4i_pwm_data sun4i_pwm_data_a10 = {
  260. .has_prescaler_bypass = false,
  261. .has_rdy = false,
  262. .npwm = 2,
  263. };
  264. static const struct sun4i_pwm_data sun4i_pwm_data_a10s = {
  265. .has_prescaler_bypass = true,
  266. .has_rdy = true,
  267. .npwm = 2,
  268. };
  269. static const struct sun4i_pwm_data sun4i_pwm_data_a13 = {
  270. .has_prescaler_bypass = true,
  271. .has_rdy = true,
  272. .npwm = 1,
  273. };
  274. static const struct sun4i_pwm_data sun4i_pwm_data_a20 = {
  275. .has_prescaler_bypass = true,
  276. .has_rdy = true,
  277. .npwm = 2,
  278. };
  279. static const struct sun4i_pwm_data sun4i_pwm_data_h3 = {
  280. .has_prescaler_bypass = true,
  281. .has_rdy = true,
  282. .npwm = 1,
  283. };
  284. static const struct of_device_id sun4i_pwm_dt_ids[] = {
  285. {
  286. .compatible = "allwinner,sun4i-a10-pwm",
  287. .data = &sun4i_pwm_data_a10,
  288. }, {
  289. .compatible = "allwinner,sun5i-a10s-pwm",
  290. .data = &sun4i_pwm_data_a10s,
  291. }, {
  292. .compatible = "allwinner,sun5i-a13-pwm",
  293. .data = &sun4i_pwm_data_a13,
  294. }, {
  295. .compatible = "allwinner,sun7i-a20-pwm",
  296. .data = &sun4i_pwm_data_a20,
  297. }, {
  298. .compatible = "allwinner,sun8i-h3-pwm",
  299. .data = &sun4i_pwm_data_h3,
  300. }, {
  301. /* sentinel */
  302. },
  303. };
  304. MODULE_DEVICE_TABLE(of, sun4i_pwm_dt_ids);
  305. static int sun4i_pwm_probe(struct platform_device *pdev)
  306. {
  307. struct sun4i_pwm_chip *pwm;
  308. struct resource *res;
  309. int ret;
  310. const struct of_device_id *match;
  311. match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);
  312. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  313. if (!pwm)
  314. return -ENOMEM;
  315. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  316. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  317. if (IS_ERR(pwm->base))
  318. return PTR_ERR(pwm->base);
  319. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  320. if (IS_ERR(pwm->clk))
  321. return PTR_ERR(pwm->clk);
  322. pwm->data = match->data;
  323. pwm->chip.dev = &pdev->dev;
  324. pwm->chip.ops = &sun4i_pwm_ops;
  325. pwm->chip.base = -1;
  326. pwm->chip.npwm = pwm->data->npwm;
  327. pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  328. pwm->chip.of_pwm_n_cells = 3;
  329. spin_lock_init(&pwm->ctrl_lock);
  330. ret = pwmchip_add(&pwm->chip);
  331. if (ret < 0) {
  332. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  333. return ret;
  334. }
  335. platform_set_drvdata(pdev, pwm);
  336. return 0;
  337. }
  338. static int sun4i_pwm_remove(struct platform_device *pdev)
  339. {
  340. struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
  341. return pwmchip_remove(&pwm->chip);
  342. }
  343. static struct platform_driver sun4i_pwm_driver = {
  344. .driver = {
  345. .name = "sun4i-pwm",
  346. .of_match_table = sun4i_pwm_dt_ids,
  347. },
  348. .probe = sun4i_pwm_probe,
  349. .remove = sun4i_pwm_remove,
  350. };
  351. module_platform_driver(sun4i_pwm_driver);
  352. MODULE_ALIAS("platform:sun4i-pwm");
  353. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  354. MODULE_DESCRIPTION("Allwinner sun4i PWM driver");
  355. MODULE_LICENSE("GPL v2");