pwm-spear.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * ST Microelectronics SPEAr Pulse Width Modulator driver
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Shiraz Hashim <shiraz.hashim@st.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/kernel.h>
  16. #include <linux/math64.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pwm.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #define NUM_PWM 4
  24. /* PWM registers and bits definitions */
  25. #define PWMCR 0x00 /* Control Register */
  26. #define PWMCR_PWM_ENABLE 0x1
  27. #define PWMCR_PRESCALE_SHIFT 2
  28. #define PWMCR_MIN_PRESCALE 0x00
  29. #define PWMCR_MAX_PRESCALE 0x3FFF
  30. #define PWMDCR 0x04 /* Duty Cycle Register */
  31. #define PWMDCR_MIN_DUTY 0x0001
  32. #define PWMDCR_MAX_DUTY 0xFFFF
  33. #define PWMPCR 0x08 /* Period Register */
  34. #define PWMPCR_MIN_PERIOD 0x0001
  35. #define PWMPCR_MAX_PERIOD 0xFFFF
  36. /* Following only available on 13xx SoCs */
  37. #define PWMMCR 0x3C /* Master Control Register */
  38. #define PWMMCR_PWM_ENABLE 0x1
  39. /**
  40. * struct spear_pwm_chip - struct representing pwm chip
  41. *
  42. * @mmio_base: base address of pwm chip
  43. * @clk: pointer to clk structure of pwm chip
  44. * @chip: linux pwm chip representation
  45. * @dev: pointer to device structure of pwm chip
  46. */
  47. struct spear_pwm_chip {
  48. void __iomem *mmio_base;
  49. struct clk *clk;
  50. struct pwm_chip chip;
  51. struct device *dev;
  52. };
  53. static inline struct spear_pwm_chip *to_spear_pwm_chip(struct pwm_chip *chip)
  54. {
  55. return container_of(chip, struct spear_pwm_chip, chip);
  56. }
  57. static inline u32 spear_pwm_readl(struct spear_pwm_chip *chip, unsigned int num,
  58. unsigned long offset)
  59. {
  60. return readl_relaxed(chip->mmio_base + (num << 4) + offset);
  61. }
  62. static inline void spear_pwm_writel(struct spear_pwm_chip *chip,
  63. unsigned int num, unsigned long offset,
  64. unsigned long val)
  65. {
  66. writel_relaxed(val, chip->mmio_base + (num << 4) + offset);
  67. }
  68. static int spear_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  69. int duty_ns, int period_ns)
  70. {
  71. struct spear_pwm_chip *pc = to_spear_pwm_chip(chip);
  72. u64 val, div, clk_rate;
  73. unsigned long prescale = PWMCR_MIN_PRESCALE, pv, dc;
  74. int ret;
  75. /*
  76. * Find pv, dc and prescale to suit duty_ns and period_ns. This is done
  77. * according to formulas described below:
  78. *
  79. * period_ns = 10^9 * (PRESCALE + 1) * PV / PWM_CLK_RATE
  80. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  81. *
  82. * PV = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  83. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  84. */
  85. clk_rate = clk_get_rate(pc->clk);
  86. while (1) {
  87. div = 1000000000;
  88. div *= 1 + prescale;
  89. val = clk_rate * period_ns;
  90. pv = div64_u64(val, div);
  91. val = clk_rate * duty_ns;
  92. dc = div64_u64(val, div);
  93. /* if duty_ns and period_ns are not achievable then return */
  94. if (pv < PWMPCR_MIN_PERIOD || dc < PWMDCR_MIN_DUTY)
  95. return -EINVAL;
  96. /*
  97. * if pv and dc have crossed their upper limit, then increase
  98. * prescale and recalculate pv and dc.
  99. */
  100. if (pv > PWMPCR_MAX_PERIOD || dc > PWMDCR_MAX_DUTY) {
  101. if (++prescale > PWMCR_MAX_PRESCALE)
  102. return -EINVAL;
  103. continue;
  104. }
  105. break;
  106. }
  107. /*
  108. * NOTE: the clock to PWM has to be enabled first before writing to the
  109. * registers.
  110. */
  111. ret = clk_enable(pc->clk);
  112. if (ret)
  113. return ret;
  114. spear_pwm_writel(pc, pwm->hwpwm, PWMCR,
  115. prescale << PWMCR_PRESCALE_SHIFT);
  116. spear_pwm_writel(pc, pwm->hwpwm, PWMDCR, dc);
  117. spear_pwm_writel(pc, pwm->hwpwm, PWMPCR, pv);
  118. clk_disable(pc->clk);
  119. return 0;
  120. }
  121. static int spear_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  122. {
  123. struct spear_pwm_chip *pc = to_spear_pwm_chip(chip);
  124. int rc = 0;
  125. u32 val;
  126. rc = clk_enable(pc->clk);
  127. if (!rc)
  128. return rc;
  129. val = spear_pwm_readl(pc, pwm->hwpwm, PWMCR);
  130. val |= PWMCR_PWM_ENABLE;
  131. spear_pwm_writel(pc, pwm->hwpwm, PWMCR, val);
  132. return 0;
  133. }
  134. static void spear_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  135. {
  136. struct spear_pwm_chip *pc = to_spear_pwm_chip(chip);
  137. u32 val;
  138. val = spear_pwm_readl(pc, pwm->hwpwm, PWMCR);
  139. val &= ~PWMCR_PWM_ENABLE;
  140. spear_pwm_writel(pc, pwm->hwpwm, PWMCR, val);
  141. clk_disable(pc->clk);
  142. }
  143. static const struct pwm_ops spear_pwm_ops = {
  144. .config = spear_pwm_config,
  145. .enable = spear_pwm_enable,
  146. .disable = spear_pwm_disable,
  147. .owner = THIS_MODULE,
  148. };
  149. static int spear_pwm_probe(struct platform_device *pdev)
  150. {
  151. struct device_node *np = pdev->dev.of_node;
  152. struct spear_pwm_chip *pc;
  153. struct resource *r;
  154. int ret;
  155. u32 val;
  156. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. if (!r) {
  158. dev_err(&pdev->dev, "no memory resources defined\n");
  159. return -ENODEV;
  160. }
  161. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  162. if (!pc) {
  163. dev_err(&pdev->dev, "failed to allocate memory\n");
  164. return -ENOMEM;
  165. }
  166. pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  167. if (IS_ERR(pc->mmio_base))
  168. return PTR_ERR(pc->mmio_base);
  169. pc->clk = devm_clk_get(&pdev->dev, NULL);
  170. if (IS_ERR(pc->clk))
  171. return PTR_ERR(pc->clk);
  172. pc->dev = &pdev->dev;
  173. platform_set_drvdata(pdev, pc);
  174. pc->chip.dev = &pdev->dev;
  175. pc->chip.ops = &spear_pwm_ops;
  176. pc->chip.base = -1;
  177. pc->chip.npwm = NUM_PWM;
  178. ret = clk_prepare(pc->clk);
  179. if (!ret)
  180. return ret;
  181. if (of_device_is_compatible(np, "st,spear1340-pwm")) {
  182. ret = clk_enable(pc->clk);
  183. if (!ret) {
  184. clk_unprepare(pc->clk);
  185. return ret;
  186. }
  187. /*
  188. * Following enables PWM chip, channels would still be
  189. * enabled individually through their control register
  190. */
  191. val = readl_relaxed(pc->mmio_base + PWMMCR);
  192. val |= PWMMCR_PWM_ENABLE;
  193. writel_relaxed(val, pc->mmio_base + PWMMCR);
  194. clk_disable(pc->clk);
  195. }
  196. ret = pwmchip_add(&pc->chip);
  197. if (!ret) {
  198. clk_unprepare(pc->clk);
  199. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  200. }
  201. return ret;
  202. }
  203. static int spear_pwm_remove(struct platform_device *pdev)
  204. {
  205. struct spear_pwm_chip *pc = platform_get_drvdata(pdev);
  206. int i;
  207. for (i = 0; i < NUM_PWM; i++)
  208. pwm_disable(&pc->chip.pwms[i]);
  209. /* clk was prepared in probe, hence unprepare it here */
  210. clk_unprepare(pc->clk);
  211. return pwmchip_remove(&pc->chip);
  212. }
  213. static struct of_device_id spear_pwm_of_match[] = {
  214. { .compatible = "st,spear320-pwm" },
  215. { .compatible = "st,spear1340-pwm" },
  216. { }
  217. };
  218. MODULE_DEVICE_TABLE(of, spear_pwm_of_match);
  219. static struct platform_driver spear_pwm_driver = {
  220. .driver = {
  221. .name = "spear-pwm",
  222. .of_match_table = spear_pwm_of_match,
  223. },
  224. .probe = spear_pwm_probe,
  225. .remove = spear_pwm_remove,
  226. };
  227. module_platform_driver(spear_pwm_driver);
  228. MODULE_LICENSE("GPL");
  229. MODULE_AUTHOR("Shiraz Hashim <shiraz.hashim@st.com>");
  230. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.com>");
  231. MODULE_ALIAS("platform:spear-pwm");