pwm-imx.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * simple driver for PWM (Pulse Width Modulator) controller
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/io.h>
  18. #include <linux/pwm.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. /* i.MX1 and i.MX21 share the same PWM function block: */
  22. #define MX1_PWMC 0x00 /* PWM Control Register */
  23. #define MX1_PWMS 0x04 /* PWM Sample Register */
  24. #define MX1_PWMP 0x08 /* PWM Period Register */
  25. #define MX1_PWMC_EN (1 << 4)
  26. /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
  27. #define MX3_PWMCR 0x00 /* PWM Control Register */
  28. #define MX3_PWMSR 0x04 /* PWM Status Register */
  29. #define MX3_PWMSAR 0x0C /* PWM Sample Register */
  30. #define MX3_PWMPR 0x10 /* PWM Period Register */
  31. #define MX3_PWMCR_PRESCALER(x) ((((x) - 1) & 0xFFF) << 4)
  32. #define MX3_PWMCR_DOZEEN (1 << 24)
  33. #define MX3_PWMCR_WAITEN (1 << 23)
  34. #define MX3_PWMCR_DBGEN (1 << 22)
  35. #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
  36. #define MX3_PWMCR_CLKSRC_IPG (1 << 16)
  37. #define MX3_PWMCR_SWR (1 << 3)
  38. #define MX3_PWMCR_EN (1 << 0)
  39. #define MX3_PWMSR_FIFOAV_4WORDS 0x4
  40. #define MX3_PWMSR_FIFOAV_MASK 0x7
  41. #define MX3_PWM_SWR_LOOP 5
  42. struct imx_chip {
  43. struct clk *clk_per;
  44. struct clk *clk_ipg;
  45. void __iomem *mmio_base;
  46. struct pwm_chip chip;
  47. int (*config)(struct pwm_chip *chip,
  48. struct pwm_device *pwm, int duty_ns, int period_ns);
  49. void (*set_enable)(struct pwm_chip *chip, bool enable);
  50. };
  51. #define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
  52. static int imx_pwm_config_v1(struct pwm_chip *chip,
  53. struct pwm_device *pwm, int duty_ns, int period_ns)
  54. {
  55. struct imx_chip *imx = to_imx_chip(chip);
  56. /*
  57. * The PWM subsystem allows for exact frequencies. However,
  58. * I cannot connect a scope on my device to the PWM line and
  59. * thus cannot provide the program the PWM controller
  60. * exactly. Instead, I'm relying on the fact that the
  61. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  62. * function group already. So I'll just modify the PWM sample
  63. * register to follow the ratio of duty_ns vs. period_ns
  64. * accordingly.
  65. *
  66. * This is good enough for programming the brightness of
  67. * the LCD backlight.
  68. *
  69. * The real implementation would divide PERCLK[0] first by
  70. * both the prescaler (/1 .. /128) and then by CLKSEL
  71. * (/2 .. /16).
  72. */
  73. u32 max = readl(imx->mmio_base + MX1_PWMP);
  74. u32 p = max * duty_ns / period_ns;
  75. writel(max - p, imx->mmio_base + MX1_PWMS);
  76. return 0;
  77. }
  78. static void imx_pwm_set_enable_v1(struct pwm_chip *chip, bool enable)
  79. {
  80. struct imx_chip *imx = to_imx_chip(chip);
  81. u32 val;
  82. val = readl(imx->mmio_base + MX1_PWMC);
  83. if (enable)
  84. val |= MX1_PWMC_EN;
  85. else
  86. val &= ~MX1_PWMC_EN;
  87. writel(val, imx->mmio_base + MX1_PWMC);
  88. }
  89. static int imx_pwm_config_v2(struct pwm_chip *chip,
  90. struct pwm_device *pwm, int duty_ns, int period_ns)
  91. {
  92. struct imx_chip *imx = to_imx_chip(chip);
  93. struct device *dev = chip->dev;
  94. unsigned long long c;
  95. unsigned long period_cycles, duty_cycles, prescale;
  96. unsigned int period_ms;
  97. bool enable = pwm_is_enabled(pwm);
  98. int wait_count = 0, fifoav;
  99. u32 cr, sr;
  100. /*
  101. * i.MX PWMv2 has a 4-word sample FIFO.
  102. * In order to avoid FIFO overflow issue, we do software reset
  103. * to clear all sample FIFO if the controller is disabled or
  104. * wait for a full PWM cycle to get a relinquished FIFO slot
  105. * when the controller is enabled and the FIFO is fully loaded.
  106. */
  107. if (enable) {
  108. sr = readl(imx->mmio_base + MX3_PWMSR);
  109. fifoav = sr & MX3_PWMSR_FIFOAV_MASK;
  110. if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
  111. period_ms = DIV_ROUND_UP(pwm_get_period(pwm),
  112. NSEC_PER_MSEC);
  113. msleep(period_ms);
  114. sr = readl(imx->mmio_base + MX3_PWMSR);
  115. if (fifoav == (sr & MX3_PWMSR_FIFOAV_MASK))
  116. dev_warn(dev, "there is no free FIFO slot\n");
  117. }
  118. } else {
  119. writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
  120. do {
  121. usleep_range(200, 1000);
  122. cr = readl(imx->mmio_base + MX3_PWMCR);
  123. } while ((cr & MX3_PWMCR_SWR) &&
  124. (wait_count++ < MX3_PWM_SWR_LOOP));
  125. if (cr & MX3_PWMCR_SWR)
  126. dev_warn(dev, "software reset timeout\n");
  127. }
  128. c = clk_get_rate(imx->clk_per);
  129. c = c * period_ns;
  130. do_div(c, 1000000000);
  131. period_cycles = c;
  132. prescale = period_cycles / 0x10000 + 1;
  133. period_cycles /= prescale;
  134. c = (unsigned long long)period_cycles * duty_ns;
  135. do_div(c, period_ns);
  136. duty_cycles = c;
  137. /*
  138. * according to imx pwm RM, the real period value should be
  139. * PERIOD value in PWMPR plus 2.
  140. */
  141. if (period_cycles > 2)
  142. period_cycles -= 2;
  143. else
  144. period_cycles = 0;
  145. writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
  146. writel(period_cycles, imx->mmio_base + MX3_PWMPR);
  147. cr = MX3_PWMCR_PRESCALER(prescale) |
  148. MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
  149. MX3_PWMCR_DBGEN | MX3_PWMCR_CLKSRC_IPG_HIGH;
  150. if (enable)
  151. cr |= MX3_PWMCR_EN;
  152. writel(cr, imx->mmio_base + MX3_PWMCR);
  153. return 0;
  154. }
  155. static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
  156. {
  157. struct imx_chip *imx = to_imx_chip(chip);
  158. u32 val;
  159. val = readl(imx->mmio_base + MX3_PWMCR);
  160. if (enable)
  161. val |= MX3_PWMCR_EN;
  162. else
  163. val &= ~MX3_PWMCR_EN;
  164. writel(val, imx->mmio_base + MX3_PWMCR);
  165. }
  166. static int imx_pwm_config(struct pwm_chip *chip,
  167. struct pwm_device *pwm, int duty_ns, int period_ns)
  168. {
  169. struct imx_chip *imx = to_imx_chip(chip);
  170. int ret;
  171. ret = clk_prepare_enable(imx->clk_ipg);
  172. if (ret)
  173. return ret;
  174. ret = imx->config(chip, pwm, duty_ns, period_ns);
  175. clk_disable_unprepare(imx->clk_ipg);
  176. return ret;
  177. }
  178. static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  179. {
  180. struct imx_chip *imx = to_imx_chip(chip);
  181. int ret;
  182. ret = clk_prepare_enable(imx->clk_per);
  183. if (ret)
  184. return ret;
  185. imx->set_enable(chip, true);
  186. return 0;
  187. }
  188. static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  189. {
  190. struct imx_chip *imx = to_imx_chip(chip);
  191. imx->set_enable(chip, false);
  192. clk_disable_unprepare(imx->clk_per);
  193. }
  194. static struct pwm_ops imx_pwm_ops = {
  195. .enable = imx_pwm_enable,
  196. .disable = imx_pwm_disable,
  197. .config = imx_pwm_config,
  198. .owner = THIS_MODULE,
  199. };
  200. struct imx_pwm_data {
  201. int (*config)(struct pwm_chip *chip,
  202. struct pwm_device *pwm, int duty_ns, int period_ns);
  203. void (*set_enable)(struct pwm_chip *chip, bool enable);
  204. };
  205. static struct imx_pwm_data imx_pwm_data_v1 = {
  206. .config = imx_pwm_config_v1,
  207. .set_enable = imx_pwm_set_enable_v1,
  208. };
  209. static struct imx_pwm_data imx_pwm_data_v2 = {
  210. .config = imx_pwm_config_v2,
  211. .set_enable = imx_pwm_set_enable_v2,
  212. };
  213. static const struct of_device_id imx_pwm_dt_ids[] = {
  214. { .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
  215. { .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
  216. { /* sentinel */ }
  217. };
  218. MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
  219. static int imx_pwm_probe(struct platform_device *pdev)
  220. {
  221. const struct of_device_id *of_id =
  222. of_match_device(imx_pwm_dt_ids, &pdev->dev);
  223. const struct imx_pwm_data *data;
  224. struct imx_chip *imx;
  225. struct resource *r;
  226. int ret = 0;
  227. if (!of_id)
  228. return -ENODEV;
  229. imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
  230. if (imx == NULL)
  231. return -ENOMEM;
  232. imx->clk_per = devm_clk_get(&pdev->dev, "per");
  233. if (IS_ERR(imx->clk_per)) {
  234. dev_err(&pdev->dev, "getting per clock failed with %ld\n",
  235. PTR_ERR(imx->clk_per));
  236. return PTR_ERR(imx->clk_per);
  237. }
  238. imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  239. if (IS_ERR(imx->clk_ipg)) {
  240. dev_err(&pdev->dev, "getting ipg clock failed with %ld\n",
  241. PTR_ERR(imx->clk_ipg));
  242. return PTR_ERR(imx->clk_ipg);
  243. }
  244. imx->chip.ops = &imx_pwm_ops;
  245. imx->chip.dev = &pdev->dev;
  246. imx->chip.base = -1;
  247. imx->chip.npwm = 1;
  248. imx->chip.can_sleep = true;
  249. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  251. if (IS_ERR(imx->mmio_base))
  252. return PTR_ERR(imx->mmio_base);
  253. data = of_id->data;
  254. imx->config = data->config;
  255. imx->set_enable = data->set_enable;
  256. ret = pwmchip_add(&imx->chip);
  257. if (ret < 0)
  258. return ret;
  259. platform_set_drvdata(pdev, imx);
  260. return 0;
  261. }
  262. static int imx_pwm_remove(struct platform_device *pdev)
  263. {
  264. struct imx_chip *imx;
  265. imx = platform_get_drvdata(pdev);
  266. if (imx == NULL)
  267. return -ENODEV;
  268. return pwmchip_remove(&imx->chip);
  269. }
  270. static struct platform_driver imx_pwm_driver = {
  271. .driver = {
  272. .name = "imx-pwm",
  273. .of_match_table = imx_pwm_dt_ids,
  274. },
  275. .probe = imx_pwm_probe,
  276. .remove = imx_pwm_remove,
  277. };
  278. module_platform_driver(imx_pwm_driver);
  279. MODULE_LICENSE("GPL v2");
  280. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");