pwm-img.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.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 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. u32 suspend_ctrl_cfg;
  63. u32 suspend_ch_cfg[IMG_PWM_NPWM];
  64. };
  65. static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
  66. {
  67. return container_of(chip, struct img_pwm_chip, chip);
  68. }
  69. static inline void img_pwm_writel(struct img_pwm_chip *imgchip,
  70. u32 reg, u32 val)
  71. {
  72. writel(val, imgchip->base + reg);
  73. }
  74. static inline u32 img_pwm_readl(struct img_pwm_chip *imgchip, u32 reg)
  75. {
  76. return readl(imgchip->base + reg);
  77. }
  78. static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  79. int duty_ns, int period_ns)
  80. {
  81. u32 val, div, duty, timebase;
  82. unsigned long mul, output_clk_hz, input_clk_hz;
  83. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  84. unsigned int max_timebase = imgchip->data->max_timebase;
  85. int ret;
  86. if (period_ns < imgchip->min_period_ns ||
  87. period_ns > imgchip->max_period_ns) {
  88. dev_err(chip->dev, "configured period not in range\n");
  89. return -ERANGE;
  90. }
  91. input_clk_hz = clk_get_rate(imgchip->pwm_clk);
  92. output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
  93. mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
  94. if (mul <= max_timebase) {
  95. div = PWM_CTRL_CFG_NO_SUB_DIV;
  96. timebase = DIV_ROUND_UP(mul, 1);
  97. } else if (mul <= max_timebase * 8) {
  98. div = PWM_CTRL_CFG_SUB_DIV0;
  99. timebase = DIV_ROUND_UP(mul, 8);
  100. } else if (mul <= max_timebase * 64) {
  101. div = PWM_CTRL_CFG_SUB_DIV1;
  102. timebase = DIV_ROUND_UP(mul, 64);
  103. } else if (mul <= max_timebase * 512) {
  104. div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
  105. timebase = DIV_ROUND_UP(mul, 512);
  106. } else {
  107. dev_err(chip->dev,
  108. "failed to configure timebase steps/divider value\n");
  109. return -EINVAL;
  110. }
  111. duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
  112. ret = pm_runtime_resume_and_get(chip->dev);
  113. if (ret < 0)
  114. return ret;
  115. val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  116. val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
  117. val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
  118. PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
  119. img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
  120. val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
  121. (timebase << PWM_CH_CFG_TMBASE_SHIFT);
  122. img_pwm_writel(imgchip, PWM_CH_CFG(pwm->hwpwm), val);
  123. pm_runtime_mark_last_busy(chip->dev);
  124. pm_runtime_put_autosuspend(chip->dev);
  125. return 0;
  126. }
  127. static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  128. {
  129. u32 val;
  130. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  131. int ret;
  132. ret = pm_runtime_resume_and_get(chip->dev);
  133. if (ret < 0)
  134. return ret;
  135. val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  136. val |= BIT(pwm->hwpwm);
  137. img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
  138. regmap_update_bits(imgchip->periph_regs, PERIP_PWM_PDM_CONTROL,
  139. PERIP_PWM_PDM_CONTROL_CH_MASK <<
  140. PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0);
  141. return 0;
  142. }
  143. static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  144. {
  145. u32 val;
  146. struct img_pwm_chip *imgchip = to_img_pwm_chip(chip);
  147. val = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  148. val &= ~BIT(pwm->hwpwm);
  149. img_pwm_writel(imgchip, PWM_CTRL_CFG, val);
  150. pm_runtime_mark_last_busy(chip->dev);
  151. pm_runtime_put_autosuspend(chip->dev);
  152. }
  153. static int img_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  154. const struct pwm_state *state)
  155. {
  156. int err;
  157. if (state->polarity != PWM_POLARITY_NORMAL)
  158. return -EINVAL;
  159. if (!state->enabled) {
  160. if (pwm->state.enabled)
  161. img_pwm_disable(chip, pwm);
  162. return 0;
  163. }
  164. err = img_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
  165. if (err)
  166. return err;
  167. if (!pwm->state.enabled)
  168. err = img_pwm_enable(chip, pwm);
  169. return err;
  170. }
  171. static const struct pwm_ops img_pwm_ops = {
  172. .apply = img_pwm_apply,
  173. .owner = THIS_MODULE,
  174. };
  175. static const struct img_pwm_soc_data pistachio_pwm = {
  176. .max_timebase = 255,
  177. };
  178. static const struct of_device_id img_pwm_of_match[] = {
  179. {
  180. .compatible = "img,pistachio-pwm",
  181. .data = &pistachio_pwm,
  182. },
  183. { }
  184. };
  185. MODULE_DEVICE_TABLE(of, img_pwm_of_match);
  186. static int img_pwm_runtime_suspend(struct device *dev)
  187. {
  188. struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
  189. clk_disable_unprepare(imgchip->pwm_clk);
  190. clk_disable_unprepare(imgchip->sys_clk);
  191. return 0;
  192. }
  193. static int img_pwm_runtime_resume(struct device *dev)
  194. {
  195. struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
  196. int ret;
  197. ret = clk_prepare_enable(imgchip->sys_clk);
  198. if (ret < 0) {
  199. dev_err(dev, "could not prepare or enable sys clock\n");
  200. return ret;
  201. }
  202. ret = clk_prepare_enable(imgchip->pwm_clk);
  203. if (ret < 0) {
  204. dev_err(dev, "could not prepare or enable pwm clock\n");
  205. clk_disable_unprepare(imgchip->sys_clk);
  206. return ret;
  207. }
  208. return 0;
  209. }
  210. static int img_pwm_probe(struct platform_device *pdev)
  211. {
  212. int ret;
  213. u64 val;
  214. unsigned long clk_rate;
  215. struct img_pwm_chip *imgchip;
  216. const struct of_device_id *of_dev_id;
  217. imgchip = devm_kzalloc(&pdev->dev, sizeof(*imgchip), GFP_KERNEL);
  218. if (!imgchip)
  219. return -ENOMEM;
  220. imgchip->dev = &pdev->dev;
  221. imgchip->base = devm_platform_ioremap_resource(pdev, 0);
  222. if (IS_ERR(imgchip->base))
  223. return PTR_ERR(imgchip->base);
  224. of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev);
  225. if (!of_dev_id)
  226. return -ENODEV;
  227. imgchip->data = of_dev_id->data;
  228. imgchip->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  229. "img,cr-periph");
  230. if (IS_ERR(imgchip->periph_regs))
  231. return PTR_ERR(imgchip->periph_regs);
  232. imgchip->sys_clk = devm_clk_get(&pdev->dev, "sys");
  233. if (IS_ERR(imgchip->sys_clk)) {
  234. dev_err(&pdev->dev, "failed to get system clock\n");
  235. return PTR_ERR(imgchip->sys_clk);
  236. }
  237. imgchip->pwm_clk = devm_clk_get(&pdev->dev, "imgchip");
  238. if (IS_ERR(imgchip->pwm_clk)) {
  239. dev_err(&pdev->dev, "failed to get imgchip clock\n");
  240. return PTR_ERR(imgchip->pwm_clk);
  241. }
  242. platform_set_drvdata(pdev, imgchip);
  243. pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT);
  244. pm_runtime_use_autosuspend(&pdev->dev);
  245. pm_runtime_enable(&pdev->dev);
  246. if (!pm_runtime_enabled(&pdev->dev)) {
  247. ret = img_pwm_runtime_resume(&pdev->dev);
  248. if (ret)
  249. goto err_pm_disable;
  250. }
  251. clk_rate = clk_get_rate(imgchip->pwm_clk);
  252. if (!clk_rate) {
  253. dev_err(&pdev->dev, "imgchip clock has no frequency\n");
  254. ret = -EINVAL;
  255. goto err_suspend;
  256. }
  257. /* The maximum input clock divider is 512 */
  258. val = (u64)NSEC_PER_SEC * 512 * imgchip->data->max_timebase;
  259. do_div(val, clk_rate);
  260. imgchip->max_period_ns = val;
  261. val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
  262. do_div(val, clk_rate);
  263. imgchip->min_period_ns = val;
  264. imgchip->chip.dev = &pdev->dev;
  265. imgchip->chip.ops = &img_pwm_ops;
  266. imgchip->chip.npwm = IMG_PWM_NPWM;
  267. ret = pwmchip_add(&imgchip->chip);
  268. if (ret < 0) {
  269. dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
  270. goto err_suspend;
  271. }
  272. return 0;
  273. err_suspend:
  274. if (!pm_runtime_enabled(&pdev->dev))
  275. img_pwm_runtime_suspend(&pdev->dev);
  276. err_pm_disable:
  277. pm_runtime_disable(&pdev->dev);
  278. pm_runtime_dont_use_autosuspend(&pdev->dev);
  279. return ret;
  280. }
  281. static int img_pwm_remove(struct platform_device *pdev)
  282. {
  283. struct img_pwm_chip *imgchip = platform_get_drvdata(pdev);
  284. pm_runtime_disable(&pdev->dev);
  285. if (!pm_runtime_status_suspended(&pdev->dev))
  286. img_pwm_runtime_suspend(&pdev->dev);
  287. pwmchip_remove(&imgchip->chip);
  288. return 0;
  289. }
  290. #ifdef CONFIG_PM_SLEEP
  291. static int img_pwm_suspend(struct device *dev)
  292. {
  293. struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
  294. int i, ret;
  295. if (pm_runtime_status_suspended(dev)) {
  296. ret = img_pwm_runtime_resume(dev);
  297. if (ret)
  298. return ret;
  299. }
  300. for (i = 0; i < imgchip->chip.npwm; i++)
  301. imgchip->suspend_ch_cfg[i] = img_pwm_readl(imgchip,
  302. PWM_CH_CFG(i));
  303. imgchip->suspend_ctrl_cfg = img_pwm_readl(imgchip, PWM_CTRL_CFG);
  304. img_pwm_runtime_suspend(dev);
  305. return 0;
  306. }
  307. static int img_pwm_resume(struct device *dev)
  308. {
  309. struct img_pwm_chip *imgchip = dev_get_drvdata(dev);
  310. int ret;
  311. int i;
  312. ret = img_pwm_runtime_resume(dev);
  313. if (ret)
  314. return ret;
  315. for (i = 0; i < imgchip->chip.npwm; i++)
  316. img_pwm_writel(imgchip, PWM_CH_CFG(i),
  317. imgchip->suspend_ch_cfg[i]);
  318. img_pwm_writel(imgchip, PWM_CTRL_CFG, imgchip->suspend_ctrl_cfg);
  319. for (i = 0; i < imgchip->chip.npwm; i++)
  320. if (imgchip->suspend_ctrl_cfg & BIT(i))
  321. regmap_update_bits(imgchip->periph_regs,
  322. PERIP_PWM_PDM_CONTROL,
  323. PERIP_PWM_PDM_CONTROL_CH_MASK <<
  324. PERIP_PWM_PDM_CONTROL_CH_SHIFT(i),
  325. 0);
  326. if (pm_runtime_status_suspended(dev))
  327. img_pwm_runtime_suspend(dev);
  328. return 0;
  329. }
  330. #endif /* CONFIG_PM */
  331. static const struct dev_pm_ops img_pwm_pm_ops = {
  332. SET_RUNTIME_PM_OPS(img_pwm_runtime_suspend,
  333. img_pwm_runtime_resume,
  334. NULL)
  335. SET_SYSTEM_SLEEP_PM_OPS(img_pwm_suspend, img_pwm_resume)
  336. };
  337. static struct platform_driver img_pwm_driver = {
  338. .driver = {
  339. .name = "img-pwm",
  340. .pm = &img_pwm_pm_ops,
  341. .of_match_table = img_pwm_of_match,
  342. },
  343. .probe = img_pwm_probe,
  344. .remove = img_pwm_remove,
  345. };
  346. module_platform_driver(img_pwm_driver);
  347. MODULE_AUTHOR("Sai Masarapu <[email protected]>");
  348. MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
  349. MODULE_LICENSE("GPL v2");