pwm-img.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Imagination Technologies Pulse Width Modulator driver
  3. *
  4. * Copyright (c) 2014-2015, Imagination Technologies
  5. *
  6. * Based on drivers/pwm/pwm-tegra.c, Copyright (c) 2010, NVIDIA Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pwm.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. /* PWM registers */
  23. #define PWM_CTRL_CFG 0x0000
  24. #define PWM_CTRL_CFG_NO_SUB_DIV 0
  25. #define PWM_CTRL_CFG_SUB_DIV0 1
  26. #define PWM_CTRL_CFG_SUB_DIV1 2
  27. #define PWM_CTRL_CFG_SUB_DIV0_DIV1 3
  28. #define PWM_CTRL_CFG_DIV_SHIFT(ch) ((ch) * 2 + 4)
  29. #define PWM_CTRL_CFG_DIV_MASK 0x3
  30. #define PWM_CH_CFG(ch) (0x4 + (ch) * 4)
  31. #define PWM_CH_CFG_TMBASE_SHIFT 0
  32. #define PWM_CH_CFG_DUTY_SHIFT 16
  33. #define PERIP_PWM_PDM_CONTROL 0x0140
  34. #define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1
  35. #define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4)
  36. #define MAX_TMBASE_STEPS 65536
  37. struct img_pwm_chip {
  38. struct device *dev;
  39. struct pwm_chip chip;
  40. struct clk *pwm_clk;
  41. struct clk *sys_clk;
  42. void __iomem *base;
  43. struct regmap *periph_regs;
  44. };
  45. static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
  46. {
  47. return container_of(chip, struct img_pwm_chip, chip);
  48. }
  49. static inline void img_pwm_writel(struct img_pwm_chip *chip,
  50. u32 reg, u32 val)
  51. {
  52. writel(val, chip->base + reg);
  53. }
  54. static inline u32 img_pwm_readl(struct img_pwm_chip *chip,
  55. u32 reg)
  56. {
  57. return readl(chip->base + reg);
  58. }
  59. static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  60. int duty_ns, int period_ns)
  61. {
  62. u32 val, div, duty, timebase;
  63. unsigned long mul, output_clk_hz, input_clk_hz;
  64. struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
  65. input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
  66. output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
  67. mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
  68. if (mul <= MAX_TMBASE_STEPS) {
  69. div = PWM_CTRL_CFG_NO_SUB_DIV;
  70. timebase = DIV_ROUND_UP(mul, 1);
  71. } else if (mul <= MAX_TMBASE_STEPS * 8) {
  72. div = PWM_CTRL_CFG_SUB_DIV0;
  73. timebase = DIV_ROUND_UP(mul, 8);
  74. } else if (mul <= MAX_TMBASE_STEPS * 64) {
  75. div = PWM_CTRL_CFG_SUB_DIV1;
  76. timebase = DIV_ROUND_UP(mul, 64);
  77. } else if (mul <= MAX_TMBASE_STEPS * 512) {
  78. div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
  79. timebase = DIV_ROUND_UP(mul, 512);
  80. } else if (mul > MAX_TMBASE_STEPS * 512) {
  81. dev_err(chip->dev,
  82. "failed to configure timebase steps/divider value\n");
  83. return -EINVAL;
  84. }
  85. duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
  86. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  87. val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
  88. val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
  89. PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
  90. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  91. val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
  92. (timebase << PWM_CH_CFG_TMBASE_SHIFT);
  93. img_pwm_writel(pwm_chip, PWM_CH_CFG(pwm->hwpwm), val);
  94. return 0;
  95. }
  96. static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  97. {
  98. u32 val;
  99. struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
  100. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  101. val |= BIT(pwm->hwpwm);
  102. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  103. regmap_update_bits(pwm_chip->periph_regs, PERIP_PWM_PDM_CONTROL,
  104. PERIP_PWM_PDM_CONTROL_CH_MASK <<
  105. PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0);
  106. return 0;
  107. }
  108. static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  109. {
  110. u32 val;
  111. struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
  112. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  113. val &= ~BIT(pwm->hwpwm);
  114. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  115. }
  116. static const struct pwm_ops img_pwm_ops = {
  117. .config = img_pwm_config,
  118. .enable = img_pwm_enable,
  119. .disable = img_pwm_disable,
  120. .owner = THIS_MODULE,
  121. };
  122. static int img_pwm_probe(struct platform_device *pdev)
  123. {
  124. int ret;
  125. struct resource *res;
  126. struct img_pwm_chip *pwm;
  127. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  128. if (!pwm)
  129. return -ENOMEM;
  130. pwm->dev = &pdev->dev;
  131. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  132. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  133. if (IS_ERR(pwm->base))
  134. return PTR_ERR(pwm->base);
  135. pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  136. "img,cr-periph");
  137. if (IS_ERR(pwm->periph_regs))
  138. return PTR_ERR(pwm->periph_regs);
  139. pwm->sys_clk = devm_clk_get(&pdev->dev, "sys");
  140. if (IS_ERR(pwm->sys_clk)) {
  141. dev_err(&pdev->dev, "failed to get system clock\n");
  142. return PTR_ERR(pwm->sys_clk);
  143. }
  144. pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
  145. if (IS_ERR(pwm->pwm_clk)) {
  146. dev_err(&pdev->dev, "failed to get pwm clock\n");
  147. return PTR_ERR(pwm->pwm_clk);
  148. }
  149. ret = clk_prepare_enable(pwm->sys_clk);
  150. if (ret < 0) {
  151. dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
  152. return ret;
  153. }
  154. ret = clk_prepare_enable(pwm->pwm_clk);
  155. if (ret < 0) {
  156. dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
  157. goto disable_sysclk;
  158. }
  159. pwm->chip.dev = &pdev->dev;
  160. pwm->chip.ops = &img_pwm_ops;
  161. pwm->chip.base = -1;
  162. pwm->chip.npwm = 4;
  163. ret = pwmchip_add(&pwm->chip);
  164. if (ret < 0) {
  165. dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
  166. goto disable_pwmclk;
  167. }
  168. platform_set_drvdata(pdev, pwm);
  169. return 0;
  170. disable_pwmclk:
  171. clk_disable_unprepare(pwm->pwm_clk);
  172. disable_sysclk:
  173. clk_disable_unprepare(pwm->sys_clk);
  174. return ret;
  175. }
  176. static int img_pwm_remove(struct platform_device *pdev)
  177. {
  178. struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
  179. u32 val;
  180. unsigned int i;
  181. for (i = 0; i < pwm_chip->chip.npwm; i++) {
  182. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  183. val &= ~BIT(i);
  184. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  185. }
  186. clk_disable_unprepare(pwm_chip->pwm_clk);
  187. clk_disable_unprepare(pwm_chip->sys_clk);
  188. return pwmchip_remove(&pwm_chip->chip);
  189. }
  190. static const struct of_device_id img_pwm_of_match[] = {
  191. { .compatible = "img,pistachio-pwm", },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(of, img_pwm_of_match);
  195. static struct platform_driver img_pwm_driver = {
  196. .driver = {
  197. .name = "img-pwm",
  198. .of_match_table = img_pwm_of_match,
  199. },
  200. .probe = img_pwm_probe,
  201. .remove = img_pwm_remove,
  202. };
  203. module_platform_driver(img_pwm_driver);
  204. MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>");
  205. MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
  206. MODULE_LICENSE("GPL v2");