pwm-img.c 11 KB

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