pwm-img.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Imagination Technologies Pulse Width Modulator driver
  4. *
  5. * Copyright (c) 2014-2015, Imagination Technologies
  6. *
  7. * Based on drivers/pwm/pwm-tegra.c, Copyright (c) 2010, NVIDIA Corporation
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/property.h>
  18. #include <linux/pwm.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. /* PWM registers */
  22. #define PWM_CTRL_CFG 0x0000
  23. #define PWM_CTRL_CFG_NO_SUB_DIV 0
  24. #define PWM_CTRL_CFG_SUB_DIV0 1
  25. #define PWM_CTRL_CFG_SUB_DIV1 2
  26. #define PWM_CTRL_CFG_SUB_DIV0_DIV1 3
  27. #define PWM_CTRL_CFG_DIV_SHIFT(ch) ((ch) * 2 + 4)
  28. #define PWM_CTRL_CFG_DIV_MASK 0x3
  29. #define PWM_CH_CFG(ch) (0x4 + (ch) * 4)
  30. #define PWM_CH_CFG_TMBASE_SHIFT 0
  31. #define PWM_CH_CFG_DUTY_SHIFT 16
  32. #define PERIP_PWM_PDM_CONTROL 0x0140
  33. #define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1
  34. #define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4)
  35. #define IMG_PWM_PM_TIMEOUT 1000 /* ms */
  36. /*
  37. * PWM period is specified with a timebase register,
  38. * in number of step periods. The PWM duty cycle is also
  39. * specified in step periods, in the [0, $timebase] range.
  40. * In other words, the timebase imposes the duty cycle
  41. * resolution. Therefore, let's constraint the timebase to
  42. * a minimum value to allow a sane range of duty cycle values.
  43. * Imposing a minimum timebase, will impose a maximum PWM frequency.
  44. *
  45. * The value chosen is completely arbitrary.
  46. */
  47. #define MIN_TMBASE_STEPS 16
  48. #define IMG_PWM_NPWM 4
  49. struct img_pwm_soc_data {
  50. u32 max_timebase;
  51. };
  52. struct img_pwm_chip {
  53. struct clk *pwm_clk;
  54. struct clk *sys_clk;
  55. void __iomem *base;
  56. struct regmap *periph_regs;
  57. int max_period_ns;
  58. int min_period_ns;
  59. const struct img_pwm_soc_data *data;
  60. u32 suspend_ctrl_cfg;
  61. u32 suspend_ch_cfg[IMG_PWM_NPWM];
  62. };
  63. static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
  64. {
  65. return pwmchip_get_drvdata(chip);
  66. }
  67. static inline void img_pwm_writel(struct img_pwm_chip *imgchip,
  68. u32 reg, u32 val)
  69. {
  70. writel(val, imgchip->base + reg);
  71. }
  72. static inline u32 img_pwm_readl(struct img_pwm_chip *imgchip, u32 reg)
  73. {
  74. return readl(imgchip->base + reg);
  75. }
  76. static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  77. int duty_ns, int period_ns)
  78. {
  79. u32 val, div, duty, timebase;
  80. unsigned long mul, output_clk_hz, input_clk_hz;
  81. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  82. unsigned int max_timebase = imgchip->data->max_timebase;
  83. int ret;
  84. if (period_ns < imgchip->min_period_ns ||
  85. period_ns > imgchip->max_period_ns) {
  86. dev_err(pwmchip_parent(chip), "configured period not in range\n");
  87. return -ERANGE;
  88. }
  89. input_clk_hz = clk_get_rate(imgchip->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 {
  105. dev_err(pwmchip_parent(chip),
  106. "failed to configure timebase steps/divider value\n");
  107. return -EINVAL;
  108. }
  109. duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
  110. ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
  111. if (ret < 0)
  112. return ret;
  113. val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  114. val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
  115. val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
  116. PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
  117. img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
  118. val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
  119. (timebase << PWM_CH_CFG_TMBASE_SHIFT);
  120. img_pwm_writel(imgchip, PWM_CH_CFG(pwm->hwpwm), val);
  121. pm_runtime_mark_last_busy(pwmchip_parent(chip));
  122. pm_runtime_put_autosuspend(pwmchip_parent(chip));
  123. return 0;
  124. }
  125. static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  126. {
  127. u32 val;
  128. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  129. int ret;
  130. ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
  131. if (ret < 0)
  132. return ret;
  133. val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  134. val |= BIT(pwm->hwpwm);
  135. img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
  136. regmap_clear_bits(imgchip->periph_regs, PERIP_PWM_PDM_CONTROL,
  137. PERIP_PWM_PDM_CONTROL_CH_MASK <<
  138. PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm));
  139. return 0;
  140. }
  141. static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  142. {
  143. u32 val;
  144. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  145. val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  146. val &= ~BIT(pwm->hwpwm);
  147. img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
  148. pm_runtime_mark_last_busy(pwmchip_parent(chip));
  149. pm_runtime_put_autosuspend(pwmchip_parent(chip));
  150. }
  151. static int img_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  152. const struct pwm_state *state)
  153. {
  154. int err;
  155. if (state->polarity != PWM_POLARITY_NORMAL)
  156. return -EINVAL;
  157. if (!state->enabled) {
  158. if (pwm->state.enabled)
  159. img_pwm_disable(chip, pwm);
  160. return 0;
  161. }
  162. err = img_pwm_config(chip, pwm, state->duty_cycle, state->period);
  163. if (err)
  164. return err;
  165. if (!pwm->state.enabled)
  166. err = img_pwm_enable(chip, pwm);
  167. return err;
  168. }
  169. static const struct pwm_ops img_pwm_ops = {
  170. .apply = img_pwm_apply,
  171. };
  172. static const struct img_pwm_soc_data pistachio_pwm = {
  173. .max_timebase = 255,
  174. };
  175. static const struct of_device_id img_pwm_of_match[] = {
  176. {
  177. .compatible = "img,pistachio-pwm",
  178. .data = &pistachio_pwm,
  179. },
  180. { }
  181. };
  182. MODULE_DEVICE_TABLE(of, img_pwm_of_match);
  183. static int img_pwm_runtime_suspend(struct device *dev)
  184. {
  185. struct pwm_chip *chip = dev_get_drvdata(dev);
  186. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  187. clk_disable_unprepare(imgchip->pwm_clk);
  188. clk_disable_unprepare(imgchip->sys_clk);
  189. return 0;
  190. }
  191. static int img_pwm_runtime_resume(struct device *dev)
  192. {
  193. struct pwm_chip *chip = dev_get_drvdata(dev);
  194. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  195. int ret;
  196. ret = clk_prepare_enable(imgchip->sys_clk);
  197. if (ret < 0) {
  198. dev_err(dev, "could not prepare or enable sys clock\n");
  199. return ret;
  200. }
  201. ret = clk_prepare_enable(imgchip->pwm_clk);
  202. if (ret < 0) {
  203. dev_err(dev, "could not prepare or enable pwm clock\n");
  204. clk_disable_unprepare(imgchip->sys_clk);
  205. return ret;
  206. }
  207. return 0;
  208. }
  209. static int img_pwm_probe(struct platform_device *pdev)
  210. {
  211. int ret;
  212. u64 val;
  213. unsigned long clk_rate;
  214. struct pwm_chip *chip;
  215. struct img_pwm_chip *imgchip;
  216. chip = devm_pwmchip_alloc(&pdev->dev, IMG_PWM_NPWM, sizeof(*imgchip));
  217. if (IS_ERR(chip))
  218. return PTR_ERR(chip);
  219. imgchip = to_img_pwm_chip(chip);
  220. imgchip->base = devm_platform_ioremap_resource(pdev, 0);
  221. if (IS_ERR(imgchip->base))
  222. return PTR_ERR(imgchip->base);
  223. imgchip->data = device_get_match_data(&pdev->dev);
  224. imgchip->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  225. "img,cr-periph");
  226. if (IS_ERR(imgchip->periph_regs))
  227. return PTR_ERR(imgchip->periph_regs);
  228. imgchip->sys_clk = devm_clk_get(&pdev->dev, "sys");
  229. if (IS_ERR(imgchip->sys_clk)) {
  230. dev_err(&pdev->dev, "failed to get system clock\n");
  231. return PTR_ERR(imgchip->sys_clk);
  232. }
  233. imgchip->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
  234. if (IS_ERR(imgchip->pwm_clk)) {
  235. dev_err(&pdev->dev, "failed to get pwm clock\n");
  236. return PTR_ERR(imgchip->pwm_clk);
  237. }
  238. platform_set_drvdata(pdev, chip);
  239. pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT);
  240. pm_runtime_use_autosuspend(&pdev->dev);
  241. pm_runtime_enable(&pdev->dev);
  242. if (!pm_runtime_enabled(&pdev->dev)) {
  243. ret = img_pwm_runtime_resume(&pdev->dev);
  244. if (ret)
  245. goto err_pm_disable;
  246. }
  247. clk_rate = clk_get_rate(imgchip->pwm_clk);
  248. if (!clk_rate) {
  249. dev_err(&pdev->dev, "imgchip clock has no frequency\n");
  250. ret = -EINVAL;
  251. goto err_suspend;
  252. }
  253. /* The maximum input clock divider is 512 */
  254. val = (u64)NSEC_PER_SEC * 512 * imgchip->data->max_timebase;
  255. do_div(val, clk_rate);
  256. imgchip->max_period_ns = val;
  257. val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
  258. do_div(val, clk_rate);
  259. imgchip->min_period_ns = val;
  260. chip->ops = &img_pwm_ops;
  261. ret = pwmchip_add(chip);
  262. if (ret < 0) {
  263. dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
  264. goto err_suspend;
  265. }
  266. return 0;
  267. err_suspend:
  268. if (!pm_runtime_enabled(&pdev->dev))
  269. img_pwm_runtime_suspend(&pdev->dev);
  270. err_pm_disable:
  271. pm_runtime_disable(&pdev->dev);
  272. pm_runtime_dont_use_autosuspend(&pdev->dev);
  273. return ret;
  274. }
  275. static void img_pwm_remove(struct platform_device *pdev)
  276. {
  277. struct pwm_chip *chip = platform_get_drvdata(pdev);
  278. pm_runtime_disable(&pdev->dev);
  279. if (!pm_runtime_status_suspended(&pdev->dev))
  280. img_pwm_runtime_suspend(&pdev->dev);
  281. pwmchip_remove(chip);
  282. }
  283. #ifdef CONFIG_PM_SLEEP
  284. static int img_pwm_suspend(struct device *dev)
  285. {
  286. struct pwm_chip *chip = dev_get_drvdata(dev);
  287. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  288. int i, ret;
  289. if (pm_runtime_status_suspended(dev)) {
  290. ret = img_pwm_runtime_resume(dev);
  291. if (ret)
  292. return ret;
  293. }
  294. for (i = 0; i < chip->npwm; i++)
  295. imgchip->suspend_ch_cfg[i] = img_pwm_readl(imgchip,
  296. PWM_CH_CFG(i));
  297. imgchip->suspend_ctrl_cfg = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  298. img_pwm_runtime_suspend(dev);
  299. return 0;
  300. }
  301. static int img_pwm_resume(struct device *dev)
  302. {
  303. struct pwm_chip *chip = dev_get_drvdata(dev);
  304. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  305. int ret;
  306. int i;
  307. ret = img_pwm_runtime_resume(dev);
  308. if (ret)
  309. return ret;
  310. for (i = 0; i < chip->npwm; i++)
  311. img_pwm_writel(imgchip, PWM_CH_CFG(i),
  312. imgchip->suspend_ch_cfg[i]);
  313. img_pwm_writel(imgchip, PWM_CTRL_CFG, imgchip->suspend_ctrl_cfg);
  314. for (i = 0; i < chip->npwm; i++)
  315. if (imgchip->suspend_ctrl_cfg & BIT(i))
  316. regmap_clear_bits(imgchip->periph_regs,
  317. PERIP_PWM_PDM_CONTROL,
  318. PERIP_PWM_PDM_CONTROL_CH_MASK <<
  319. PERIP_PWM_PDM_CONTROL_CH_SHIFT(i));
  320. if (pm_runtime_status_suspended(dev))
  321. img_pwm_runtime_suspend(dev);
  322. return 0;
  323. }
  324. #endif /* CONFIG_PM */
  325. static const struct dev_pm_ops img_pwm_pm_ops = {
  326. SET_RUNTIME_PM_OPS(img_pwm_runtime_suspend,
  327. img_pwm_runtime_resume,
  328. NULL)
  329. SET_SYSTEM_SLEEP_PM_OPS(img_pwm_suspend, img_pwm_resume)
  330. };
  331. static struct platform_driver img_pwm_driver = {
  332. .driver = {
  333. .name = "img-pwm",
  334. .pm = &img_pwm_pm_ops,
  335. .of_match_table = img_pwm_of_match,
  336. },
  337. .probe = img_pwm_probe,
  338. .remove = img_pwm_remove,
  339. };
  340. module_platform_driver(img_pwm_driver);
  341. MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>");
  342. MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
  343. MODULE_LICENSE("GPL v2");