pwm-zx.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2017 Sanechips Technology Co., Ltd.
  3. * Copyright 2017 Linaro Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pwm.h>
  16. #include <linux/slab.h>
  17. #define ZX_PWM_MODE 0x0
  18. #define ZX_PWM_CLKDIV_SHIFT 2
  19. #define ZX_PWM_CLKDIV_MASK GENMASK(11, 2)
  20. #define ZX_PWM_CLKDIV(x) (((x) << ZX_PWM_CLKDIV_SHIFT) & \
  21. ZX_PWM_CLKDIV_MASK)
  22. #define ZX_PWM_POLAR BIT(1)
  23. #define ZX_PWM_EN BIT(0)
  24. #define ZX_PWM_PERIOD 0x4
  25. #define ZX_PWM_DUTY 0x8
  26. #define ZX_PWM_CLKDIV_MAX 1023
  27. #define ZX_PWM_PERIOD_MAX 65535
  28. struct zx_pwm_chip {
  29. struct pwm_chip chip;
  30. struct clk *pclk;
  31. struct clk *wclk;
  32. void __iomem *base;
  33. };
  34. static inline struct zx_pwm_chip *to_zx_pwm_chip(struct pwm_chip *chip)
  35. {
  36. return container_of(chip, struct zx_pwm_chip, chip);
  37. }
  38. static inline u32 zx_pwm_readl(struct zx_pwm_chip *zpc, unsigned int hwpwm,
  39. unsigned int offset)
  40. {
  41. return readl(zpc->base + (hwpwm + 1) * 0x10 + offset);
  42. }
  43. static inline void zx_pwm_writel(struct zx_pwm_chip *zpc, unsigned int hwpwm,
  44. unsigned int offset, u32 value)
  45. {
  46. writel(value, zpc->base + (hwpwm + 1) * 0x10 + offset);
  47. }
  48. static void zx_pwm_set_mask(struct zx_pwm_chip *zpc, unsigned int hwpwm,
  49. unsigned int offset, u32 mask, u32 value)
  50. {
  51. u32 data;
  52. data = zx_pwm_readl(zpc, hwpwm, offset);
  53. data &= ~mask;
  54. data |= value & mask;
  55. zx_pwm_writel(zpc, hwpwm, offset, data);
  56. }
  57. static void zx_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  58. struct pwm_state *state)
  59. {
  60. struct zx_pwm_chip *zpc = to_zx_pwm_chip(chip);
  61. unsigned long rate;
  62. unsigned int div;
  63. u32 value;
  64. u64 tmp;
  65. value = zx_pwm_readl(zpc, pwm->hwpwm, ZX_PWM_MODE);
  66. if (value & ZX_PWM_POLAR)
  67. state->polarity = PWM_POLARITY_NORMAL;
  68. else
  69. state->polarity = PWM_POLARITY_INVERSED;
  70. if (value & ZX_PWM_EN)
  71. state->enabled = true;
  72. else
  73. state->enabled = false;
  74. div = (value & ZX_PWM_CLKDIV_MASK) >> ZX_PWM_CLKDIV_SHIFT;
  75. rate = clk_get_rate(zpc->wclk);
  76. tmp = zx_pwm_readl(zpc, pwm->hwpwm, ZX_PWM_PERIOD);
  77. tmp *= div * NSEC_PER_SEC;
  78. state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  79. tmp = zx_pwm_readl(zpc, pwm->hwpwm, ZX_PWM_DUTY);
  80. tmp *= div * NSEC_PER_SEC;
  81. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  82. }
  83. static int zx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  84. unsigned int duty_ns, unsigned int period_ns)
  85. {
  86. struct zx_pwm_chip *zpc = to_zx_pwm_chip(chip);
  87. unsigned int period_cycles, duty_cycles;
  88. unsigned long long c;
  89. unsigned int div = 1;
  90. unsigned long rate;
  91. /* Find out the best divider */
  92. rate = clk_get_rate(zpc->wclk);
  93. while (1) {
  94. c = rate / div;
  95. c = c * period_ns;
  96. do_div(c, NSEC_PER_SEC);
  97. if (c < ZX_PWM_PERIOD_MAX)
  98. break;
  99. div++;
  100. if (div > ZX_PWM_CLKDIV_MAX)
  101. return -ERANGE;
  102. }
  103. /* Calculate duty cycles */
  104. period_cycles = c;
  105. c *= duty_ns;
  106. do_div(c, period_ns);
  107. duty_cycles = c;
  108. /*
  109. * If the PWM is being enabled, we have to temporarily disable it
  110. * before configuring the registers.
  111. */
  112. if (pwm_is_enabled(pwm))
  113. zx_pwm_set_mask(zpc, pwm->hwpwm, ZX_PWM_MODE, ZX_PWM_EN, 0);
  114. /* Set up registers */
  115. zx_pwm_set_mask(zpc, pwm->hwpwm, ZX_PWM_MODE, ZX_PWM_CLKDIV_MASK,
  116. ZX_PWM_CLKDIV(div));
  117. zx_pwm_writel(zpc, pwm->hwpwm, ZX_PWM_PERIOD, period_cycles);
  118. zx_pwm_writel(zpc, pwm->hwpwm, ZX_PWM_DUTY, duty_cycles);
  119. /* Re-enable the PWM if needed */
  120. if (pwm_is_enabled(pwm))
  121. zx_pwm_set_mask(zpc, pwm->hwpwm, ZX_PWM_MODE,
  122. ZX_PWM_EN, ZX_PWM_EN);
  123. return 0;
  124. }
  125. static int zx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  126. struct pwm_state *state)
  127. {
  128. struct zx_pwm_chip *zpc = to_zx_pwm_chip(chip);
  129. struct pwm_state cstate;
  130. int ret;
  131. pwm_get_state(pwm, &cstate);
  132. if (state->polarity != cstate.polarity)
  133. zx_pwm_set_mask(zpc, pwm->hwpwm, ZX_PWM_MODE, ZX_PWM_POLAR,
  134. (state->polarity == PWM_POLARITY_INVERSED) ?
  135. 0 : ZX_PWM_POLAR);
  136. if (state->period != cstate.period ||
  137. state->duty_cycle != cstate.duty_cycle) {
  138. ret = zx_pwm_config(chip, pwm, state->duty_cycle,
  139. state->period);
  140. if (ret)
  141. return ret;
  142. }
  143. if (state->enabled != cstate.enabled) {
  144. if (state->enabled) {
  145. ret = clk_prepare_enable(zpc->wclk);
  146. if (ret)
  147. return ret;
  148. zx_pwm_set_mask(zpc, pwm->hwpwm, ZX_PWM_MODE,
  149. ZX_PWM_EN, ZX_PWM_EN);
  150. } else {
  151. zx_pwm_set_mask(zpc, pwm->hwpwm, ZX_PWM_MODE,
  152. ZX_PWM_EN, 0);
  153. clk_disable_unprepare(zpc->wclk);
  154. }
  155. }
  156. return 0;
  157. }
  158. static const struct pwm_ops zx_pwm_ops = {
  159. .apply = zx_pwm_apply,
  160. .get_state = zx_pwm_get_state,
  161. .owner = THIS_MODULE,
  162. };
  163. static int zx_pwm_probe(struct platform_device *pdev)
  164. {
  165. struct zx_pwm_chip *zpc;
  166. struct resource *res;
  167. unsigned int i;
  168. int ret;
  169. zpc = devm_kzalloc(&pdev->dev, sizeof(*zpc), GFP_KERNEL);
  170. if (!zpc)
  171. return -ENOMEM;
  172. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  173. zpc->base = devm_ioremap_resource(&pdev->dev, res);
  174. if (IS_ERR(zpc->base))
  175. return PTR_ERR(zpc->base);
  176. zpc->pclk = devm_clk_get(&pdev->dev, "pclk");
  177. if (IS_ERR(zpc->pclk))
  178. return PTR_ERR(zpc->pclk);
  179. zpc->wclk = devm_clk_get(&pdev->dev, "wclk");
  180. if (IS_ERR(zpc->wclk))
  181. return PTR_ERR(zpc->wclk);
  182. ret = clk_prepare_enable(zpc->pclk);
  183. if (ret)
  184. return ret;
  185. zpc->chip.dev = &pdev->dev;
  186. zpc->chip.ops = &zx_pwm_ops;
  187. zpc->chip.base = -1;
  188. zpc->chip.npwm = 4;
  189. zpc->chip.of_xlate = of_pwm_xlate_with_flags;
  190. zpc->chip.of_pwm_n_cells = 3;
  191. /*
  192. * PWM devices may be enabled by firmware, and let's disable all of
  193. * them initially to save power.
  194. */
  195. for (i = 0; i < zpc->chip.npwm; i++)
  196. zx_pwm_set_mask(zpc, i, ZX_PWM_MODE, ZX_PWM_EN, 0);
  197. ret = pwmchip_add(&zpc->chip);
  198. if (ret < 0) {
  199. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  200. return ret;
  201. }
  202. platform_set_drvdata(pdev, zpc);
  203. return 0;
  204. }
  205. static int zx_pwm_remove(struct platform_device *pdev)
  206. {
  207. struct zx_pwm_chip *zpc = platform_get_drvdata(pdev);
  208. int ret;
  209. ret = pwmchip_remove(&zpc->chip);
  210. clk_disable_unprepare(zpc->pclk);
  211. return ret;
  212. }
  213. static const struct of_device_id zx_pwm_dt_ids[] = {
  214. { .compatible = "zte,zx296718-pwm", },
  215. { /* sentinel */ }
  216. };
  217. MODULE_DEVICE_TABLE(of, zx_pwm_dt_ids);
  218. static struct platform_driver zx_pwm_driver = {
  219. .driver = {
  220. .name = "zx-pwm",
  221. .of_match_table = zx_pwm_dt_ids,
  222. },
  223. .probe = zx_pwm_probe,
  224. .remove = zx_pwm_remove,
  225. };
  226. module_platform_driver(zx_pwm_driver);
  227. MODULE_ALIAS("platform:zx-pwm");
  228. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  229. MODULE_DESCRIPTION("ZTE ZX PWM Driver");
  230. MODULE_LICENSE("GPL v2");