blob: bcd849496f8df6ba2573ab034ad9aac5c0974b6e [file] [log] [blame]
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +01001// 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
8#include <linux/bitfield.h>
9#include <linux/bitops.h>
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/err.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/pwm.h>
20#include <linux/slab.h>
21
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
26#define MX1_PWMC_EN BIT(4)
27
28struct pwm_imx1_chip {
29 struct clk *clk_ipg;
30 struct clk *clk_per;
31 void __iomem *mmio_base;
32 struct pwm_chip chip;
33};
34
35#define to_pwm_imx1_chip(chip) container_of(chip, struct pwm_imx1_chip, chip)
36
37static int pwm_imx1_clk_prepare_enable(struct pwm_chip *chip)
38{
39 struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
40 int ret;
41
42 ret = clk_prepare_enable(imx->clk_ipg);
43 if (ret)
44 return ret;
45
46 ret = clk_prepare_enable(imx->clk_per);
47 if (ret) {
48 clk_disable_unprepare(imx->clk_ipg);
49 return ret;
50 }
51
52 return 0;
53}
54
55static void pwm_imx1_clk_disable_unprepare(struct pwm_chip *chip)
56{
57 struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
58
59 clk_disable_unprepare(imx->clk_per);
60 clk_disable_unprepare(imx->clk_ipg);
61}
62
63static int pwm_imx1_config(struct pwm_chip *chip,
64 struct pwm_device *pwm, int duty_ns, int period_ns)
65{
66 struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
67 u32 max, p;
68
69 /*
70 * The PWM subsystem allows for exact frequencies. However,
71 * I cannot connect a scope on my device to the PWM line and
72 * thus cannot provide the program the PWM controller
73 * exactly. Instead, I'm relying on the fact that the
74 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
75 * function group already. So I'll just modify the PWM sample
76 * register to follow the ratio of duty_ns vs. period_ns
77 * accordingly.
78 *
79 * This is good enough for programming the brightness of
80 * the LCD backlight.
81 *
82 * The real implementation would divide PERCLK[0] first by
83 * both the prescaler (/1 .. /128) and then by CLKSEL
84 * (/2 .. /16).
85 */
86 max = readl(imx->mmio_base + MX1_PWMP);
87 p = max * duty_ns / period_ns;
88
89 writel(max - p, imx->mmio_base + MX1_PWMS);
90
91 return 0;
92}
93
94static int pwm_imx1_enable(struct pwm_chip *chip, struct pwm_device *pwm)
95{
96 struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
97 u32 value;
98 int ret;
99
100 ret = pwm_imx1_clk_prepare_enable(chip);
101 if (ret < 0)
102 return ret;
103
104 value = readl(imx->mmio_base + MX1_PWMC);
105 value |= MX1_PWMC_EN;
106 writel(value, imx->mmio_base + MX1_PWMC);
107
108 return 0;
109}
110
111static void pwm_imx1_disable(struct pwm_chip *chip, struct pwm_device *pwm)
112{
113 struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
114 u32 value;
115
116 value = readl(imx->mmio_base + MX1_PWMC);
117 value &= ~MX1_PWMC_EN;
118 writel(value, imx->mmio_base + MX1_PWMC);
119
120 pwm_imx1_clk_disable_unprepare(chip);
121}
122
123static const struct pwm_ops pwm_imx1_ops = {
124 .enable = pwm_imx1_enable,
125 .disable = pwm_imx1_disable,
126 .config = pwm_imx1_config,
127 .owner = THIS_MODULE,
128};
129
130static const struct of_device_id pwm_imx1_dt_ids[] = {
131 { .compatible = "fsl,imx1-pwm", },
132 { /* sentinel */ }
133};
134MODULE_DEVICE_TABLE(of, pwm_imx1_dt_ids);
135
136static int pwm_imx1_probe(struct platform_device *pdev)
137{
138 struct pwm_imx1_chip *imx;
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100139
140 imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
141 if (!imx)
142 return -ENOMEM;
143
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100144 imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
Anson Huangf1332e92020-08-11 14:24:32 +0800145 if (IS_ERR(imx->clk_ipg))
146 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_ipg),
147 "getting ipg clock failed\n");
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100148
149 imx->clk_per = devm_clk_get(&pdev->dev, "per");
Anson Huangf1332e92020-08-11 14:24:32 +0800150 if (IS_ERR(imx->clk_per))
151 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per),
152 "failed to get peripheral clock\n");
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100153
154 imx->chip.ops = &pwm_imx1_ops;
155 imx->chip.dev = &pdev->dev;
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100156 imx->chip.npwm = 1;
157
Yangtao Li860b1ca2019-12-29 08:05:58 +0000158 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100159 if (IS_ERR(imx->mmio_base))
160 return PTR_ERR(imx->mmio_base);
161
Uwe Kleine-Königf7edeb42021-05-25 08:35:28 +0200162 return devm_pwmchip_add(&pdev->dev, &imx->chip);
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100163}
164
165static struct platform_driver pwm_imx1_driver = {
166 .driver = {
167 .name = "pwm-imx1",
168 .of_match_table = pwm_imx1_dt_ids,
169 },
170 .probe = pwm_imx1_probe,
Uwe Kleine-Königd80f8202019-01-07 20:53:52 +0100171};
172module_platform_driver(pwm_imx1_driver);
173
174MODULE_LICENSE("GPL v2");
175MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");