pwm-sun4i.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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/err.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pwm.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/time.h>
  20. #define PWM_CTRL_REG 0x0
  21. #define PWM_CH_PRD_BASE 0x4
  22. #define PWM_CH_PRD_OFFSET 0x4
  23. #define PWM_CH_PRD(ch) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFFSET * (ch))
  24. #define PWMCH_OFFSET 15
  25. #define PWM_PRESCAL_MASK GENMASK(3, 0)
  26. #define PWM_PRESCAL_OFF 0
  27. #define PWM_EN BIT(4)
  28. #define PWM_ACT_STATE BIT(5)
  29. #define PWM_CLK_GATING BIT(6)
  30. #define PWM_MODE BIT(7)
  31. #define PWM_PULSE BIT(8)
  32. #define PWM_BYPASS BIT(9)
  33. #define PWM_RDY_BASE 28
  34. #define PWM_RDY_OFFSET 1
  35. #define PWM_RDY(ch) BIT(PWM_RDY_BASE + PWM_RDY_OFFSET * (ch))
  36. #define PWM_PRD(prd) (((prd) - 1) << 16)
  37. #define PWM_PRD_MASK GENMASK(15, 0)
  38. #define PWM_DTY_MASK GENMASK(15, 0)
  39. #define BIT_CH(bit, chan) ((bit) << ((chan) * PWMCH_OFFSET))
  40. static const u32 prescaler_table[] = {
  41. 120,
  42. 180,
  43. 240,
  44. 360,
  45. 480,
  46. 0,
  47. 0,
  48. 0,
  49. 12000,
  50. 24000,
  51. 36000,
  52. 48000,
  53. 72000,
  54. 0,
  55. 0,
  56. 0, /* Actually 1 but tested separately */
  57. };
  58. struct sun4i_pwm_data {
  59. bool has_prescaler_bypass;
  60. bool has_rdy;
  61. };
  62. struct sun4i_pwm_chip {
  63. struct pwm_chip chip;
  64. struct clk *clk;
  65. void __iomem *base;
  66. spinlock_t ctrl_lock;
  67. const struct sun4i_pwm_data *data;
  68. };
  69. static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
  70. {
  71. return container_of(chip, struct sun4i_pwm_chip, chip);
  72. }
  73. static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip,
  74. unsigned long offset)
  75. {
  76. return readl(chip->base + offset);
  77. }
  78. static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip,
  79. u32 val, unsigned long offset)
  80. {
  81. writel(val, chip->base + offset);
  82. }
  83. static int sun4i_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  84. int duty_ns, int period_ns)
  85. {
  86. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  87. u32 prd, dty, val, clk_gate;
  88. u64 clk_rate, div = 0;
  89. unsigned int prescaler = 0;
  90. int err;
  91. clk_rate = clk_get_rate(sun4i_pwm->clk);
  92. if (sun4i_pwm->data->has_prescaler_bypass) {
  93. /* First, test without any prescaler when available */
  94. prescaler = PWM_PRESCAL_MASK;
  95. /*
  96. * When not using any prescaler, the clock period in nanoseconds
  97. * is not an integer so round it half up instead of
  98. * truncating to get less surprising values.
  99. */
  100. div = clk_rate * period_ns + NSEC_PER_SEC/2;
  101. do_div(div, NSEC_PER_SEC);
  102. if (div - 1 > PWM_PRD_MASK)
  103. prescaler = 0;
  104. }
  105. if (prescaler == 0) {
  106. /* Go up from the first divider */
  107. for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) {
  108. if (!prescaler_table[prescaler])
  109. continue;
  110. div = clk_rate;
  111. do_div(div, prescaler_table[prescaler]);
  112. div = div * period_ns;
  113. do_div(div, NSEC_PER_SEC);
  114. if (div - 1 <= PWM_PRD_MASK)
  115. break;
  116. }
  117. if (div - 1 > PWM_PRD_MASK) {
  118. dev_err(chip->dev, "period exceeds the maximum value\n");
  119. return -EINVAL;
  120. }
  121. }
  122. prd = div;
  123. div *= duty_ns;
  124. do_div(div, period_ns);
  125. dty = div;
  126. err = clk_prepare_enable(sun4i_pwm->clk);
  127. if (err) {
  128. dev_err(chip->dev, "failed to enable PWM clock\n");
  129. return err;
  130. }
  131. spin_lock(&sun4i_pwm->ctrl_lock);
  132. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  133. if (sun4i_pwm->data->has_rdy && (val & PWM_RDY(pwm->hwpwm))) {
  134. spin_unlock(&sun4i_pwm->ctrl_lock);
  135. clk_disable_unprepare(sun4i_pwm->clk);
  136. return -EBUSY;
  137. }
  138. clk_gate = val & BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  139. if (clk_gate) {
  140. val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  141. sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
  142. }
  143. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  144. val &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  145. val |= BIT_CH(prescaler, pwm->hwpwm);
  146. sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
  147. val = (dty & PWM_DTY_MASK) | PWM_PRD(prd);
  148. sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
  149. if (clk_gate) {
  150. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  151. val |= clk_gate;
  152. sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
  153. }
  154. spin_unlock(&sun4i_pwm->ctrl_lock);
  155. clk_disable_unprepare(sun4i_pwm->clk);
  156. return 0;
  157. }
  158. static int sun4i_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  159. enum pwm_polarity polarity)
  160. {
  161. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  162. u32 val;
  163. int ret;
  164. ret = clk_prepare_enable(sun4i_pwm->clk);
  165. if (ret) {
  166. dev_err(chip->dev, "failed to enable PWM clock\n");
  167. return ret;
  168. }
  169. spin_lock(&sun4i_pwm->ctrl_lock);
  170. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  171. if (polarity != PWM_POLARITY_NORMAL)
  172. val &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  173. else
  174. val |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  175. sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
  176. spin_unlock(&sun4i_pwm->ctrl_lock);
  177. clk_disable_unprepare(sun4i_pwm->clk);
  178. return 0;
  179. }
  180. static int sun4i_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  181. {
  182. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  183. u32 val;
  184. int ret;
  185. ret = clk_prepare_enable(sun4i_pwm->clk);
  186. if (ret) {
  187. dev_err(chip->dev, "failed to enable PWM clock\n");
  188. return ret;
  189. }
  190. spin_lock(&sun4i_pwm->ctrl_lock);
  191. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  192. val |= BIT_CH(PWM_EN, pwm->hwpwm);
  193. val |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  194. sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
  195. spin_unlock(&sun4i_pwm->ctrl_lock);
  196. return 0;
  197. }
  198. static void sun4i_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  199. {
  200. struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  201. u32 val;
  202. spin_lock(&sun4i_pwm->ctrl_lock);
  203. val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  204. val &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  205. val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  206. sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
  207. spin_unlock(&sun4i_pwm->ctrl_lock);
  208. clk_disable_unprepare(sun4i_pwm->clk);
  209. }
  210. static const struct pwm_ops sun4i_pwm_ops = {
  211. .config = sun4i_pwm_config,
  212. .set_polarity = sun4i_pwm_set_polarity,
  213. .enable = sun4i_pwm_enable,
  214. .disable = sun4i_pwm_disable,
  215. .owner = THIS_MODULE,
  216. };
  217. static const struct sun4i_pwm_data sun4i_pwm_data_a10 = {
  218. .has_prescaler_bypass = false,
  219. .has_rdy = false,
  220. };
  221. static const struct sun4i_pwm_data sun4i_pwm_data_a20 = {
  222. .has_prescaler_bypass = true,
  223. .has_rdy = true,
  224. };
  225. static const struct of_device_id sun4i_pwm_dt_ids[] = {
  226. {
  227. .compatible = "allwinner,sun4i-a10-pwm",
  228. .data = &sun4i_pwm_data_a10,
  229. }, {
  230. .compatible = "allwinner,sun7i-a20-pwm",
  231. .data = &sun4i_pwm_data_a20,
  232. }, {
  233. /* sentinel */
  234. },
  235. };
  236. MODULE_DEVICE_TABLE(of, sun4i_pwm_dt_ids);
  237. static int sun4i_pwm_probe(struct platform_device *pdev)
  238. {
  239. struct sun4i_pwm_chip *pwm;
  240. struct resource *res;
  241. u32 val;
  242. int i, ret;
  243. const struct of_device_id *match;
  244. match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);
  245. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  246. if (!pwm)
  247. return -ENOMEM;
  248. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  249. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  250. if (IS_ERR(pwm->base))
  251. return PTR_ERR(pwm->base);
  252. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  253. if (IS_ERR(pwm->clk))
  254. return PTR_ERR(pwm->clk);
  255. pwm->chip.dev = &pdev->dev;
  256. pwm->chip.ops = &sun4i_pwm_ops;
  257. pwm->chip.base = -1;
  258. pwm->chip.npwm = 2;
  259. pwm->chip.can_sleep = true;
  260. pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  261. pwm->chip.of_pwm_n_cells = 3;
  262. pwm->data = match->data;
  263. spin_lock_init(&pwm->ctrl_lock);
  264. ret = pwmchip_add(&pwm->chip);
  265. if (ret < 0) {
  266. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  267. return ret;
  268. }
  269. platform_set_drvdata(pdev, pwm);
  270. ret = clk_prepare_enable(pwm->clk);
  271. if (ret) {
  272. dev_err(&pdev->dev, "failed to enable PWM clock\n");
  273. goto clk_error;
  274. }
  275. val = sun4i_pwm_readl(pwm, PWM_CTRL_REG);
  276. for (i = 0; i < pwm->chip.npwm; i++)
  277. if (!(val & BIT_CH(PWM_ACT_STATE, i)))
  278. pwm->chip.pwms[i].polarity = PWM_POLARITY_INVERSED;
  279. clk_disable_unprepare(pwm->clk);
  280. return 0;
  281. clk_error:
  282. pwmchip_remove(&pwm->chip);
  283. return ret;
  284. }
  285. static int sun4i_pwm_remove(struct platform_device *pdev)
  286. {
  287. struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
  288. return pwmchip_remove(&pwm->chip);
  289. }
  290. static struct platform_driver sun4i_pwm_driver = {
  291. .driver = {
  292. .name = "sun4i-pwm",
  293. .of_match_table = sun4i_pwm_dt_ids,
  294. },
  295. .probe = sun4i_pwm_probe,
  296. .remove = sun4i_pwm_remove,
  297. };
  298. module_platform_driver(sun4i_pwm_driver);
  299. MODULE_ALIAS("platform:sun4i-pwm");
  300. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  301. MODULE_DESCRIPTION("Allwinner sun4i PWM driver");
  302. MODULE_LICENSE("GPL v2");