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 = test_bit(PWMF_ENABLED, &pwm->flags);
  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->period, NSEC_PER_MSEC);
  112. msleep(period_ms);
  113. sr = readl(imx->mmio_base + MX3_PWMSR);
  114. if (fifoav == (sr & MX3_PWMSR_FIFOAV_MASK))
  115. dev_warn(dev, "there is no free FIFO slot\n");
  116. }
  117. } else {
  118. writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
  119. do {
  120. usleep_range(200, 1000);
  121. cr = readl(imx->mmio_base + MX3_PWMCR);
  122. } while ((cr & MX3_PWMCR_SWR) &&
  123. (wait_count++ < MX3_PWM_SWR_LOOP));
  124. if (cr & MX3_PWMCR_SWR)
  125. dev_warn(dev, "software reset timeout\n");
  126. }
  127. c = clk_get_rate(imx->clk_per);
  128. c = c * period_ns;
  129. do_div(c, 1000000000);
  130. period_cycles = c;
  131. prescale = period_cycles / 0x10000 + 1;
  132. period_cycles /= prescale;
  133. c = (unsigned long long)period_cycles * duty_ns;
  134. do_div(c, period_ns);
  135. duty_cycles = c;
  136. /*
  137. * according to imx pwm RM, the real period value should be
  138. * PERIOD value in PWMPR plus 2.
  139. */
  140. if (period_cycles > 2)
  141. period_cycles -= 2;
  142. else
  143. period_cycles = 0;
  144. writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
  145. writel(period_cycles, imx->mmio_base + MX3_PWMPR);
  146. cr = MX3_PWMCR_PRESCALER(prescale) |
  147. MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
  148. MX3_PWMCR_DBGEN | MX3_PWMCR_CLKSRC_IPG_HIGH;
  149. if (enable)
  150. cr |= MX3_PWMCR_EN;
  151. writel(cr, imx->mmio_base + MX3_PWMCR);
  152. return 0;
  153. }
  154. static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
  155. {
  156. struct imx_chip *imx = to_imx_chip(chip);
  157. u32 val;
  158. val = readl(imx->mmio_base + MX3_PWMCR);
  159. if (enable)
  160. val |= MX3_PWMCR_EN;
  161. else
  162. val &= ~MX3_PWMCR_EN;
  163. writel(val, imx->mmio_base + MX3_PWMCR);
  164. }
  165. static int imx_pwm_config(struct pwm_chip *chip,
  166. struct pwm_device *pwm, int duty_ns, int period_ns)
  167. {
  168. struct imx_chip *imx = to_imx_chip(chip);
  169. int ret;
  170. ret = clk_prepare_enable(imx->clk_ipg);
  171. if (ret)
  172. return ret;
  173. ret = imx->config(chip, pwm, duty_ns, period_ns);
  174. clk_disable_unprepare(imx->clk_ipg);
  175. return ret;
  176. }
  177. static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  178. {
  179. struct imx_chip *imx = to_imx_chip(chip);
  180. int ret;
  181. ret = clk_prepare_enable(imx->clk_per);
  182. if (ret)
  183. return ret;
  184. imx->set_enable(chip, true);
  185. return 0;
  186. }
  187. static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  188. {
  189. struct imx_chip *imx = to_imx_chip(chip);
  190. imx->set_enable(chip, false);
  191. clk_disable_unprepare(imx->clk_per);
  192. }
  193. static struct pwm_ops imx_pwm_ops = {
  194. .enable = imx_pwm_enable,
  195. .disable = imx_pwm_disable,
  196. .config = imx_pwm_config,
  197. .owner = THIS_MODULE,
  198. };
  199. struct imx_pwm_data {
  200. int (*config)(struct pwm_chip *chip,
  201. struct pwm_device *pwm, int duty_ns, int period_ns);
  202. void (*set_enable)(struct pwm_chip *chip, bool enable);
  203. };
  204. static struct imx_pwm_data imx_pwm_data_v1 = {
  205. .config = imx_pwm_config_v1,
  206. .set_enable = imx_pwm_set_enable_v1,
  207. };
  208. static struct imx_pwm_data imx_pwm_data_v2 = {
  209. .config = imx_pwm_config_v2,
  210. .set_enable = imx_pwm_set_enable_v2,
  211. };
  212. static const struct of_device_id imx_pwm_dt_ids[] = {
  213. { .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
  214. { .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
  215. { /* sentinel */ }
  216. };
  217. MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
  218. static int imx_pwm_probe(struct platform_device *pdev)
  219. {
  220. const struct of_device_id *of_id =
  221. of_match_device(imx_pwm_dt_ids, &pdev->dev);
  222. const struct imx_pwm_data *data;
  223. struct imx_chip *imx;
  224. struct resource *r;
  225. int ret = 0;
  226. if (!of_id)
  227. return -ENODEV;
  228. imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
  229. if (imx == NULL)
  230. return -ENOMEM;
  231. imx->clk_per = devm_clk_get(&pdev->dev, "per");
  232. if (IS_ERR(imx->clk_per)) {
  233. dev_err(&pdev->dev, "getting per clock failed with %ld\n",
  234. PTR_ERR(imx->clk_per));
  235. return PTR_ERR(imx->clk_per);
  236. }
  237. imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  238. if (IS_ERR(imx->clk_ipg)) {
  239. dev_err(&pdev->dev, "getting ipg clock failed with %ld\n",
  240. PTR_ERR(imx->clk_ipg));
  241. return PTR_ERR(imx->clk_ipg);
  242. }
  243. imx->chip.ops = &imx_pwm_ops;
  244. imx->chip.dev = &pdev->dev;
  245. imx->chip.base = -1;
  246. imx->chip.npwm = 1;
  247. imx->chip.can_sleep = true;
  248. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  249. imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  250. if (IS_ERR(imx->mmio_base))
  251. return PTR_ERR(imx->mmio_base);
  252. data = of_id->data;
  253. imx->config = data->config;
  254. imx->set_enable = data->set_enable;
  255. ret = pwmchip_add(&imx->chip);
  256. if (ret < 0)
  257. return ret;
  258. platform_set_drvdata(pdev, imx);
  259. return 0;
  260. }
  261. static int imx_pwm_remove(struct platform_device *pdev)
  262. {
  263. struct imx_chip *imx;
  264. imx = platform_get_drvdata(pdev);
  265. if (imx == NULL)
  266. return -ENODEV;
  267. return pwmchip_remove(&imx->chip);
  268. }
  269. static struct platform_driver imx_pwm_driver = {
  270. .driver = {
  271. .name = "imx-pwm",
  272. .of_match_table = imx_pwm_dt_ids,
  273. },
  274. .probe = imx_pwm_probe,
  275. .remove = imx_pwm_remove,
  276. };
  277. module_platform_driver(imx_pwm_driver);
  278. MODULE_LICENSE("GPL v2");
  279. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");