pwm-rcar.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * R-Car PWM Timer driver
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/pwm.h>
  18. #include <linux/slab.h>
  19. #define RCAR_PWM_MAX_DIVISION 24
  20. #define RCAR_PWM_MAX_CYCLE 1023
  21. #define RCAR_PWMCR 0x00
  22. #define RCAR_PWMCR_CC0_MASK 0x000f0000
  23. #define RCAR_PWMCR_CC0_SHIFT 16
  24. #define RCAR_PWMCR_CCMD BIT(15)
  25. #define RCAR_PWMCR_SYNC BIT(11)
  26. #define RCAR_PWMCR_SS0 BIT(4)
  27. #define RCAR_PWMCR_EN0 BIT(0)
  28. #define RCAR_PWMCNT 0x04
  29. #define RCAR_PWMCNT_CYC0_MASK 0x03ff0000
  30. #define RCAR_PWMCNT_CYC0_SHIFT 16
  31. #define RCAR_PWMCNT_PH0_MASK 0x000003ff
  32. #define RCAR_PWMCNT_PH0_SHIFT 0
  33. struct rcar_pwm_chip {
  34. struct pwm_chip chip;
  35. void __iomem *base;
  36. struct clk *clk;
  37. };
  38. static inline struct rcar_pwm_chip *to_rcar_pwm_chip(struct pwm_chip *chip)
  39. {
  40. return container_of(chip, struct rcar_pwm_chip, chip);
  41. }
  42. static void rcar_pwm_write(struct rcar_pwm_chip *rp, u32 data,
  43. unsigned int offset)
  44. {
  45. writel(data, rp->base + offset);
  46. }
  47. static u32 rcar_pwm_read(struct rcar_pwm_chip *rp, unsigned int offset)
  48. {
  49. return readl(rp->base + offset);
  50. }
  51. static void rcar_pwm_update(struct rcar_pwm_chip *rp, u32 mask, u32 data,
  52. unsigned int offset)
  53. {
  54. u32 value;
  55. value = rcar_pwm_read(rp, offset);
  56. value &= ~mask;
  57. value |= data & mask;
  58. rcar_pwm_write(rp, value, offset);
  59. }
  60. static int rcar_pwm_get_clock_division(struct rcar_pwm_chip *rp, int period_ns)
  61. {
  62. unsigned long clk_rate = clk_get_rate(rp->clk);
  63. unsigned long long max; /* max cycle / nanoseconds */
  64. unsigned int div;
  65. if (clk_rate == 0)
  66. return -EINVAL;
  67. for (div = 0; div <= RCAR_PWM_MAX_DIVISION; div++) {
  68. max = (unsigned long long)NSEC_PER_SEC * RCAR_PWM_MAX_CYCLE *
  69. (1 << div);
  70. do_div(max, clk_rate);
  71. if (period_ns <= max)
  72. break;
  73. }
  74. return (div <= RCAR_PWM_MAX_DIVISION) ? div : -ERANGE;
  75. }
  76. static void rcar_pwm_set_clock_control(struct rcar_pwm_chip *rp,
  77. unsigned int div)
  78. {
  79. u32 value;
  80. value = rcar_pwm_read(rp, RCAR_PWMCR);
  81. value &= ~(RCAR_PWMCR_CCMD | RCAR_PWMCR_CC0_MASK);
  82. if (div & 1)
  83. value |= RCAR_PWMCR_CCMD;
  84. div >>= 1;
  85. value |= div << RCAR_PWMCR_CC0_SHIFT;
  86. rcar_pwm_write(rp, value, RCAR_PWMCR);
  87. }
  88. static int rcar_pwm_set_counter(struct rcar_pwm_chip *rp, int div, int duty_ns,
  89. int period_ns)
  90. {
  91. unsigned long long one_cycle, tmp; /* 0.01 nanoseconds */
  92. unsigned long clk_rate = clk_get_rate(rp->clk);
  93. u32 cyc, ph;
  94. one_cycle = (unsigned long long)NSEC_PER_SEC * 100ULL * (1 << div);
  95. do_div(one_cycle, clk_rate);
  96. tmp = period_ns * 100ULL;
  97. do_div(tmp, one_cycle);
  98. cyc = (tmp << RCAR_PWMCNT_CYC0_SHIFT) & RCAR_PWMCNT_CYC0_MASK;
  99. tmp = duty_ns * 100ULL;
  100. do_div(tmp, one_cycle);
  101. ph = tmp & RCAR_PWMCNT_PH0_MASK;
  102. /* Avoid prohibited setting */
  103. if (cyc == 0 || ph == 0)
  104. return -EINVAL;
  105. rcar_pwm_write(rp, cyc | ph, RCAR_PWMCNT);
  106. return 0;
  107. }
  108. static int rcar_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  109. {
  110. struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
  111. return clk_prepare_enable(rp->clk);
  112. }
  113. static void rcar_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  114. {
  115. struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
  116. clk_disable_unprepare(rp->clk);
  117. }
  118. static int rcar_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  119. int duty_ns, int period_ns)
  120. {
  121. struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
  122. int div, ret;
  123. div = rcar_pwm_get_clock_division(rp, period_ns);
  124. if (div < 0)
  125. return div;
  126. /*
  127. * Let the core driver set pwm->period if disabled and duty_ns == 0.
  128. * But, this driver should prevent to set the new duty_ns if current
  129. * duty_cycle is not set
  130. */
  131. if (!pwm_is_enabled(pwm) && !duty_ns && !pwm->state.duty_cycle)
  132. return 0;
  133. rcar_pwm_update(rp, RCAR_PWMCR_SYNC, RCAR_PWMCR_SYNC, RCAR_PWMCR);
  134. ret = rcar_pwm_set_counter(rp, div, duty_ns, period_ns);
  135. if (!ret)
  136. rcar_pwm_set_clock_control(rp, div);
  137. /* The SYNC should be set to 0 even if rcar_pwm_set_counter failed */
  138. rcar_pwm_update(rp, RCAR_PWMCR_SYNC, 0, RCAR_PWMCR);
  139. return ret;
  140. }
  141. static int rcar_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  142. {
  143. struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
  144. u32 value;
  145. /* Don't enable the PWM device if CYC0 or PH0 is 0 */
  146. value = rcar_pwm_read(rp, RCAR_PWMCNT);
  147. if ((value & RCAR_PWMCNT_CYC0_MASK) == 0 ||
  148. (value & RCAR_PWMCNT_PH0_MASK) == 0)
  149. return -EINVAL;
  150. rcar_pwm_update(rp, RCAR_PWMCR_EN0, RCAR_PWMCR_EN0, RCAR_PWMCR);
  151. return 0;
  152. }
  153. static void rcar_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  154. {
  155. struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
  156. rcar_pwm_update(rp, RCAR_PWMCR_EN0, 0, RCAR_PWMCR);
  157. }
  158. static const struct pwm_ops rcar_pwm_ops = {
  159. .request = rcar_pwm_request,
  160. .free = rcar_pwm_free,
  161. .config = rcar_pwm_config,
  162. .enable = rcar_pwm_enable,
  163. .disable = rcar_pwm_disable,
  164. .owner = THIS_MODULE,
  165. };
  166. static int rcar_pwm_probe(struct platform_device *pdev)
  167. {
  168. struct rcar_pwm_chip *rcar_pwm;
  169. struct resource *res;
  170. int ret;
  171. rcar_pwm = devm_kzalloc(&pdev->dev, sizeof(*rcar_pwm), GFP_KERNEL);
  172. if (rcar_pwm == NULL)
  173. return -ENOMEM;
  174. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  175. rcar_pwm->base = devm_ioremap_resource(&pdev->dev, res);
  176. if (IS_ERR(rcar_pwm->base))
  177. return PTR_ERR(rcar_pwm->base);
  178. rcar_pwm->clk = devm_clk_get(&pdev->dev, NULL);
  179. if (IS_ERR(rcar_pwm->clk)) {
  180. dev_err(&pdev->dev, "cannot get clock\n");
  181. return PTR_ERR(rcar_pwm->clk);
  182. }
  183. platform_set_drvdata(pdev, rcar_pwm);
  184. rcar_pwm->chip.dev = &pdev->dev;
  185. rcar_pwm->chip.ops = &rcar_pwm_ops;
  186. rcar_pwm->chip.base = -1;
  187. rcar_pwm->chip.npwm = 1;
  188. ret = pwmchip_add(&rcar_pwm->chip);
  189. if (ret < 0) {
  190. dev_err(&pdev->dev, "failed to register PWM chip: %d\n", ret);
  191. return ret;
  192. }
  193. pm_runtime_enable(&pdev->dev);
  194. return 0;
  195. }
  196. static int rcar_pwm_remove(struct platform_device *pdev)
  197. {
  198. struct rcar_pwm_chip *rcar_pwm = platform_get_drvdata(pdev);
  199. pm_runtime_disable(&pdev->dev);
  200. return pwmchip_remove(&rcar_pwm->chip);
  201. }
  202. static const struct of_device_id rcar_pwm_of_table[] = {
  203. { .compatible = "renesas,pwm-rcar", },
  204. { },
  205. };
  206. MODULE_DEVICE_TABLE(of, rcar_pwm_of_table);
  207. static struct platform_driver rcar_pwm_driver = {
  208. .probe = rcar_pwm_probe,
  209. .remove = rcar_pwm_remove,
  210. .driver = {
  211. .name = "pwm-rcar",
  212. .of_match_table = of_match_ptr(rcar_pwm_of_table),
  213. }
  214. };
  215. module_platform_driver(rcar_pwm_driver);
  216. MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
  217. MODULE_DESCRIPTION("Renesas PWM Timer Driver");
  218. MODULE_LICENSE("GPL v2");
  219. MODULE_ALIAS("platform:pwm-rcar");