pwm-vt8500.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * drivers/pwm/pwm-vt8500.c
  3. *
  4. * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
  5. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/pwm.h>
  23. #include <linux/delay.h>
  24. #include <linux/clk.h>
  25. #include <asm/div64.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_address.h>
  29. /*
  30. * SoC architecture allocates register space for 4 PWMs but only
  31. * 2 are currently implemented.
  32. */
  33. #define VT8500_NR_PWMS 2
  34. #define REG_CTRL(pwm) (((pwm) << 4) + 0x00)
  35. #define REG_SCALAR(pwm) (((pwm) << 4) + 0x04)
  36. #define REG_PERIOD(pwm) (((pwm) << 4) + 0x08)
  37. #define REG_DUTY(pwm) (((pwm) << 4) + 0x0C)
  38. #define REG_STATUS 0x40
  39. #define CTRL_ENABLE BIT(0)
  40. #define CTRL_INVERT BIT(1)
  41. #define CTRL_AUTOLOAD BIT(2)
  42. #define CTRL_STOP_IMM BIT(3)
  43. #define CTRL_LOAD_PRESCALE BIT(4)
  44. #define CTRL_LOAD_PERIOD BIT(5)
  45. #define STATUS_CTRL_UPDATE BIT(0)
  46. #define STATUS_SCALAR_UPDATE BIT(1)
  47. #define STATUS_PERIOD_UPDATE BIT(2)
  48. #define STATUS_DUTY_UPDATE BIT(3)
  49. #define STATUS_ALL_UPDATE 0x0F
  50. struct vt8500_chip {
  51. struct pwm_chip chip;
  52. void __iomem *base;
  53. struct clk *clk;
  54. };
  55. #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
  56. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  57. static inline void pwm_busy_wait(struct vt8500_chip *vt8500, int nr, u8 bitmask)
  58. {
  59. int loops = msecs_to_loops(10);
  60. u32 mask = bitmask << (nr << 8);
  61. while ((readl(vt8500->base + REG_STATUS) & mask) && --loops)
  62. cpu_relax();
  63. if (unlikely(!loops))
  64. dev_warn(vt8500->chip.dev, "Waiting for status bits 0x%x to clear timed out\n",
  65. mask);
  66. }
  67. static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  68. int duty_ns, int period_ns)
  69. {
  70. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  71. unsigned long long c;
  72. unsigned long period_cycles, prescale, pv, dc;
  73. int err;
  74. u32 val;
  75. err = clk_enable(vt8500->clk);
  76. if (err < 0) {
  77. dev_err(chip->dev, "failed to enable clock\n");
  78. return err;
  79. }
  80. c = clk_get_rate(vt8500->clk);
  81. c = c * period_ns;
  82. do_div(c, 1000000000);
  83. period_cycles = c;
  84. if (period_cycles < 1)
  85. period_cycles = 1;
  86. prescale = (period_cycles - 1) / 4096;
  87. pv = period_cycles / (prescale + 1) - 1;
  88. if (pv > 4095)
  89. pv = 4095;
  90. if (prescale > 1023) {
  91. clk_disable(vt8500->clk);
  92. return -EINVAL;
  93. }
  94. c = (unsigned long long)pv * duty_ns;
  95. do_div(c, period_ns);
  96. dc = c;
  97. writel(prescale, vt8500->base + REG_SCALAR(pwm->hwpwm));
  98. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_SCALAR_UPDATE);
  99. writel(pv, vt8500->base + REG_PERIOD(pwm->hwpwm));
  100. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_PERIOD_UPDATE);
  101. writel(dc, vt8500->base + REG_DUTY(pwm->hwpwm));
  102. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_DUTY_UPDATE);
  103. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  104. val |= CTRL_AUTOLOAD;
  105. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  106. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  107. clk_disable(vt8500->clk);
  108. return 0;
  109. }
  110. static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  111. {
  112. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  113. int err;
  114. u32 val;
  115. err = clk_enable(vt8500->clk);
  116. if (err < 0) {
  117. dev_err(chip->dev, "failed to enable clock\n");
  118. return err;
  119. }
  120. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  121. val |= CTRL_ENABLE;
  122. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  123. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  124. return 0;
  125. }
  126. static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  127. {
  128. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  129. u32 val;
  130. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  131. val &= ~CTRL_ENABLE;
  132. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  133. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  134. clk_disable(vt8500->clk);
  135. }
  136. static int vt8500_pwm_set_polarity(struct pwm_chip *chip,
  137. struct pwm_device *pwm,
  138. enum pwm_polarity polarity)
  139. {
  140. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  141. u32 val;
  142. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  143. if (polarity == PWM_POLARITY_INVERSED)
  144. val |= CTRL_INVERT;
  145. else
  146. val &= ~CTRL_INVERT;
  147. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  148. pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  149. return 0;
  150. }
  151. static struct pwm_ops vt8500_pwm_ops = {
  152. .enable = vt8500_pwm_enable,
  153. .disable = vt8500_pwm_disable,
  154. .config = vt8500_pwm_config,
  155. .set_polarity = vt8500_pwm_set_polarity,
  156. .owner = THIS_MODULE,
  157. };
  158. static const struct of_device_id vt8500_pwm_dt_ids[] = {
  159. { .compatible = "via,vt8500-pwm", },
  160. { /* Sentinel */ }
  161. };
  162. MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids);
  163. static int vt8500_pwm_probe(struct platform_device *pdev)
  164. {
  165. struct vt8500_chip *chip;
  166. struct resource *r;
  167. struct device_node *np = pdev->dev.of_node;
  168. int ret;
  169. if (!np) {
  170. dev_err(&pdev->dev, "invalid devicetree node\n");
  171. return -EINVAL;
  172. }
  173. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  174. if (chip == NULL)
  175. return -ENOMEM;
  176. chip->chip.dev = &pdev->dev;
  177. chip->chip.ops = &vt8500_pwm_ops;
  178. chip->chip.of_xlate = of_pwm_xlate_with_flags;
  179. chip->chip.of_pwm_n_cells = 3;
  180. chip->chip.base = -1;
  181. chip->chip.npwm = VT8500_NR_PWMS;
  182. chip->clk = devm_clk_get(&pdev->dev, NULL);
  183. if (IS_ERR(chip->clk)) {
  184. dev_err(&pdev->dev, "clock source not specified\n");
  185. return PTR_ERR(chip->clk);
  186. }
  187. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  188. chip->base = devm_ioremap_resource(&pdev->dev, r);
  189. if (IS_ERR(chip->base))
  190. return PTR_ERR(chip->base);
  191. ret = clk_prepare(chip->clk);
  192. if (ret < 0) {
  193. dev_err(&pdev->dev, "failed to prepare clock\n");
  194. return ret;
  195. }
  196. ret = pwmchip_add(&chip->chip);
  197. if (ret < 0) {
  198. dev_err(&pdev->dev, "failed to add PWM chip\n");
  199. return ret;
  200. }
  201. platform_set_drvdata(pdev, chip);
  202. return ret;
  203. }
  204. static int vt8500_pwm_remove(struct platform_device *pdev)
  205. {
  206. struct vt8500_chip *chip;
  207. chip = platform_get_drvdata(pdev);
  208. if (chip == NULL)
  209. return -ENODEV;
  210. clk_unprepare(chip->clk);
  211. return pwmchip_remove(&chip->chip);
  212. }
  213. static struct platform_driver vt8500_pwm_driver = {
  214. .probe = vt8500_pwm_probe,
  215. .remove = vt8500_pwm_remove,
  216. .driver = {
  217. .name = "vt8500-pwm",
  218. .of_match_table = vt8500_pwm_dt_ids,
  219. },
  220. };
  221. module_platform_driver(vt8500_pwm_driver);
  222. MODULE_DESCRIPTION("VT8500 PWM Driver");
  223. MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>");
  224. MODULE_LICENSE("GPL v2");