pwm-img.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pwm.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. /* PWM registers */
  24. #define PWM_CTRL_CFG 0x0000
  25. #define PWM_CTRL_CFG_NO_SUB_DIV 0
  26. #define PWM_CTRL_CFG_SUB_DIV0 1
  27. #define PWM_CTRL_CFG_SUB_DIV1 2
  28. #define PWM_CTRL_CFG_SUB_DIV0_DIV1 3
  29. #define PWM_CTRL_CFG_DIV_SHIFT(ch) ((ch) * 2 + 4)
  30. #define PWM_CTRL_CFG_DIV_MASK 0x3
  31. #define PWM_CH_CFG(ch) (0x4 + (ch) * 4)
  32. #define PWM_CH_CFG_TMBASE_SHIFT 0
  33. #define PWM_CH_CFG_DUTY_SHIFT 16
  34. #define PERIP_PWM_PDM_CONTROL 0x0140
  35. #define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1
  36. #define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4)
  37. /*
  38. * PWM period is specified with a timebase register,
  39. * in number of step periods. The PWM duty cycle is also
  40. * specified in step periods, in the [0, $timebase] range.
  41. * In other words, the timebase imposes the duty cycle
  42. * resolution. Therefore, let's constraint the timebase to
  43. * a minimum value to allow a sane range of duty cycle values.
  44. * Imposing a minimum timebase, will impose a maximum PWM frequency.
  45. *
  46. * The value chosen is completely arbitrary.
  47. */
  48. #define MIN_TMBASE_STEPS 16
  49. struct img_pwm_soc_data {
  50. u32 max_timebase;
  51. };
  52. struct img_pwm_chip {
  53. struct device *dev;
  54. struct pwm_chip chip;
  55. struct clk *pwm_clk;
  56. struct clk *sys_clk;
  57. void __iomem *base;
  58. struct regmap *periph_regs;
  59. int max_period_ns;
  60. int min_period_ns;
  61. const struct img_pwm_soc_data *data;
  62. };
  63. static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
  64. {
  65. return container_of(chip, struct img_pwm_chip, chip);
  66. }
  67. static inline void img_pwm_writel(struct img_pwm_chip *chip,
  68. u32 reg, u32 val)
  69. {
  70. writel(val, chip->base + reg);
  71. }
  72. static inline u32 img_pwm_readl(struct img_pwm_chip *chip,
  73. u32 reg)
  74. {
  75. return readl(chip->base + reg);
  76. }
  77. static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  78. int duty_ns, int period_ns)
  79. {
  80. u32 val, div, duty, timebase;
  81. unsigned long mul, output_clk_hz, input_clk_hz;
  82. struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
  83. unsigned int max_timebase = pwm_chip->data->max_timebase;
  84. if (period_ns < pwm_chip->min_period_ns ||
  85. period_ns > pwm_chip->max_period_ns) {
  86. dev_err(chip->dev, "configured period not in range\n");
  87. return -ERANGE;
  88. }
  89. input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
  90. output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
  91. mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
  92. if (mul <= max_timebase) {
  93. div = PWM_CTRL_CFG_NO_SUB_DIV;
  94. timebase = DIV_ROUND_UP(mul, 1);
  95. } else if (mul <= max_timebase * 8) {
  96. div = PWM_CTRL_CFG_SUB_DIV0;
  97. timebase = DIV_ROUND_UP(mul, 8);
  98. } else if (mul <= max_timebase * 64) {
  99. div = PWM_CTRL_CFG_SUB_DIV1;
  100. timebase = DIV_ROUND_UP(mul, 64);
  101. } else if (mul <= max_timebase * 512) {
  102. div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
  103. timebase = DIV_ROUND_UP(mul, 512);
  104. } else if (mul > max_timebase * 512) {
  105. dev_err(chip->dev,
  106. "failed to configure timebase steps/divider value\n");
  107. return -EINVAL;
  108. }
  109. duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
  110. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  111. val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
  112. val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
  113. PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
  114. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  115. val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
  116. (timebase << PWM_CH_CFG_TMBASE_SHIFT);
  117. img_pwm_writel(pwm_chip, PWM_CH_CFG(pwm->hwpwm), val);
  118. return 0;
  119. }
  120. static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  121. {
  122. u32 val;
  123. struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
  124. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  125. val |= BIT(pwm->hwpwm);
  126. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  127. regmap_update_bits(pwm_chip->periph_regs, PERIP_PWM_PDM_CONTROL,
  128. PERIP_PWM_PDM_CONTROL_CH_MASK <<
  129. PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0);
  130. return 0;
  131. }
  132. static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  133. {
  134. u32 val;
  135. struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
  136. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  137. val &= ~BIT(pwm->hwpwm);
  138. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  139. }
  140. static const struct pwm_ops img_pwm_ops = {
  141. .config = img_pwm_config,
  142. .enable = img_pwm_enable,
  143. .disable = img_pwm_disable,
  144. .owner = THIS_MODULE,
  145. };
  146. static const struct img_pwm_soc_data pistachio_pwm = {
  147. .max_timebase = 255,
  148. };
  149. static const struct of_device_id img_pwm_of_match[] = {
  150. {
  151. .compatible = "img,pistachio-pwm",
  152. .data = &pistachio_pwm,
  153. },
  154. { }
  155. };
  156. MODULE_DEVICE_TABLE(of, img_pwm_of_match);
  157. static int img_pwm_probe(struct platform_device *pdev)
  158. {
  159. int ret;
  160. u64 val;
  161. unsigned long clk_rate;
  162. struct resource *res;
  163. struct img_pwm_chip *pwm;
  164. const struct of_device_id *of_dev_id;
  165. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  166. if (!pwm)
  167. return -ENOMEM;
  168. pwm->dev = &pdev->dev;
  169. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  170. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  171. if (IS_ERR(pwm->base))
  172. return PTR_ERR(pwm->base);
  173. of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev);
  174. if (!of_dev_id)
  175. return -ENODEV;
  176. pwm->data = of_dev_id->data;
  177. pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  178. "img,cr-periph");
  179. if (IS_ERR(pwm->periph_regs))
  180. return PTR_ERR(pwm->periph_regs);
  181. pwm->sys_clk = devm_clk_get(&pdev->dev, "sys");
  182. if (IS_ERR(pwm->sys_clk)) {
  183. dev_err(&pdev->dev, "failed to get system clock\n");
  184. return PTR_ERR(pwm->sys_clk);
  185. }
  186. pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
  187. if (IS_ERR(pwm->pwm_clk)) {
  188. dev_err(&pdev->dev, "failed to get pwm clock\n");
  189. return PTR_ERR(pwm->pwm_clk);
  190. }
  191. ret = clk_prepare_enable(pwm->sys_clk);
  192. if (ret < 0) {
  193. dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
  194. return ret;
  195. }
  196. ret = clk_prepare_enable(pwm->pwm_clk);
  197. if (ret < 0) {
  198. dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
  199. goto disable_sysclk;
  200. }
  201. clk_rate = clk_get_rate(pwm->pwm_clk);
  202. /* The maximum input clock divider is 512 */
  203. val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
  204. do_div(val, clk_rate);
  205. pwm->max_period_ns = val;
  206. val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
  207. do_div(val, clk_rate);
  208. pwm->min_period_ns = val;
  209. pwm->chip.dev = &pdev->dev;
  210. pwm->chip.ops = &img_pwm_ops;
  211. pwm->chip.base = -1;
  212. pwm->chip.npwm = 4;
  213. ret = pwmchip_add(&pwm->chip);
  214. if (ret < 0) {
  215. dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
  216. goto disable_pwmclk;
  217. }
  218. platform_set_drvdata(pdev, pwm);
  219. return 0;
  220. disable_pwmclk:
  221. clk_disable_unprepare(pwm->pwm_clk);
  222. disable_sysclk:
  223. clk_disable_unprepare(pwm->sys_clk);
  224. return ret;
  225. }
  226. static int img_pwm_remove(struct platform_device *pdev)
  227. {
  228. struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
  229. u32 val;
  230. unsigned int i;
  231. for (i = 0; i < pwm_chip->chip.npwm; i++) {
  232. val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
  233. val &= ~BIT(i);
  234. img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
  235. }
  236. clk_disable_unprepare(pwm_chip->pwm_clk);
  237. clk_disable_unprepare(pwm_chip->sys_clk);
  238. return pwmchip_remove(&pwm_chip->chip);
  239. }
  240. static struct platform_driver img_pwm_driver = {
  241. .driver = {
  242. .name = "img-pwm",
  243. .of_match_table = img_pwm_of_match,
  244. },
  245. .probe = img_pwm_probe,
  246. .remove = img_pwm_remove,
  247. };
  248. module_platform_driver(img_pwm_driver);
  249. MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>");
  250. MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
  251. MODULE_LICENSE("GPL v2");