pwm-sun4i.c 9.2 KB

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