pwm-imx.c 8.1 KB

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