1 // SPDX-License-Identifier: GPL-2.0
3 * simple driver for PWM (Pulse Width Modulator) controller
8 * - When disabled the output is driven to 0 independent of the configured
12 #include <linux/bitfield.h>
13 #include <linux/bitops.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/pwm.h>
23 #include <linux/slab.h>
25 #define MX3_PWMCR 0x00 /* PWM Control Register */
26 #define MX3_PWMSR 0x04 /* PWM Status Register */
27 #define MX3_PWMSAR 0x0C /* PWM Sample Register */
28 #define MX3_PWMPR 0x10 /* PWM Period Register */
30 #define MX3_PWMCR_FWM GENMASK(27, 26)
31 #define MX3_PWMCR_STOPEN BIT(25)
32 #define MX3_PWMCR_DOZEN BIT(24)
33 #define MX3_PWMCR_WAITEN BIT(23)
34 #define MX3_PWMCR_DBGEN BIT(22)
35 #define MX3_PWMCR_BCTR BIT(21)
36 #define MX3_PWMCR_HCTR BIT(20)
38 #define MX3_PWMCR_POUTC GENMASK(19, 18)
39 #define MX3_PWMCR_POUTC_NORMAL 0
40 #define MX3_PWMCR_POUTC_INVERTED 1
41 #define MX3_PWMCR_POUTC_OFF 2
43 #define MX3_PWMCR_CLKSRC GENMASK(17, 16)
44 #define MX3_PWMCR_CLKSRC_OFF 0
45 #define MX3_PWMCR_CLKSRC_IPG 1
46 #define MX3_PWMCR_CLKSRC_IPG_HIGH 2
47 #define MX3_PWMCR_CLKSRC_IPG_32K 3
49 #define MX3_PWMCR_PRESCALER GENMASK(15, 4)
51 #define MX3_PWMCR_SWR BIT(3)
53 #define MX3_PWMCR_REPEAT GENMASK(2, 1)
54 #define MX3_PWMCR_REPEAT_1X 0
55 #define MX3_PWMCR_REPEAT_2X 1
56 #define MX3_PWMCR_REPEAT_4X 2
57 #define MX3_PWMCR_REPEAT_8X 3
59 #define MX3_PWMCR_EN BIT(0)
61 #define MX3_PWMSR_FWE BIT(6)
62 #define MX3_PWMSR_CMP BIT(5)
63 #define MX3_PWMSR_ROV BIT(4)
64 #define MX3_PWMSR_FE BIT(3)
66 #define MX3_PWMSR_FIFOAV GENMASK(2, 0)
67 #define MX3_PWMSR_FIFOAV_EMPTY 0
68 #define MX3_PWMSR_FIFOAV_1WORD 1
69 #define MX3_PWMSR_FIFOAV_2WORDS 2
70 #define MX3_PWMSR_FIFOAV_3WORDS 3
71 #define MX3_PWMSR_FIFOAV_4WORDS 4
73 #define MX3_PWMCR_PRESCALER_SET(x) FIELD_PREP(MX3_PWMCR_PRESCALER, (x) - 1)
74 #define MX3_PWMCR_PRESCALER_GET(x) (FIELD_GET(MX3_PWMCR_PRESCALER, \
77 #define MX3_PWM_SWR_LOOP 5
79 /* PWMPR register value of 0xffff has the same effect as 0xfffe */
80 #define MX3_PWMPR_MAX 0xfffe
82 struct pwm_imx27_chip {
85 void __iomem *mmio_base;
88 * The driver cannot read the current duty cycle from the hardware if
89 * the hardware is disabled. Cache the last programmed duty cycle
90 * value to return in that case.
92 unsigned int duty_cycle;
95 static inline struct pwm_imx27_chip *to_pwm_imx27_chip(struct pwm_chip *chip)
97 return pwmchip_get_drvdata(chip);
100 static int pwm_imx27_clk_prepare_enable(struct pwm_imx27_chip *imx)
104 ret = clk_prepare_enable(imx->clk_ipg);
108 ret = clk_prepare_enable(imx->clk_per);
110 clk_disable_unprepare(imx->clk_ipg);
117 static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx)
119 clk_disable_unprepare(imx->clk_per);
120 clk_disable_unprepare(imx->clk_ipg);
123 static int pwm_imx27_get_state(struct pwm_chip *chip,
124 struct pwm_device *pwm, struct pwm_state *state)
126 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
127 u32 period, prescaler, pwm_clk, val;
131 ret = pwm_imx27_clk_prepare_enable(imx);
135 val = readl(imx->mmio_base + MX3_PWMCR);
137 if (val & MX3_PWMCR_EN)
138 state->enabled = true;
140 state->enabled = false;
142 switch (FIELD_GET(MX3_PWMCR_POUTC, val)) {
143 case MX3_PWMCR_POUTC_NORMAL:
144 state->polarity = PWM_POLARITY_NORMAL;
146 case MX3_PWMCR_POUTC_INVERTED:
147 state->polarity = PWM_POLARITY_INVERSED;
150 dev_warn(pwmchip_parent(chip), "can't set polarity, output disconnected");
153 prescaler = MX3_PWMCR_PRESCALER_GET(val);
154 pwm_clk = clk_get_rate(imx->clk_per);
155 val = readl(imx->mmio_base + MX3_PWMPR);
156 period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val;
158 /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */
159 tmp = NSEC_PER_SEC * (u64)(period + 2) * prescaler;
160 state->period = DIV_ROUND_UP_ULL(tmp, pwm_clk);
163 * PWMSAR can be read only if PWM is enabled. If the PWM is disabled,
164 * use the cached value.
167 val = readl(imx->mmio_base + MX3_PWMSAR);
169 val = imx->duty_cycle;
171 tmp = NSEC_PER_SEC * (u64)(val) * prescaler;
172 state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk);
174 pwm_imx27_clk_disable_unprepare(imx);
179 static void pwm_imx27_sw_reset(struct pwm_chip *chip)
181 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
182 struct device *dev = pwmchip_parent(chip);
186 writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR);
188 usleep_range(200, 1000);
189 cr = readl(imx->mmio_base + MX3_PWMCR);
190 } while ((cr & MX3_PWMCR_SWR) &&
191 (wait_count++ < MX3_PWM_SWR_LOOP));
193 if (cr & MX3_PWMCR_SWR)
194 dev_warn(dev, "software reset timeout\n");
197 static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip,
198 struct pwm_device *pwm)
200 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
201 struct device *dev = pwmchip_parent(chip);
202 unsigned int period_ms;
206 sr = readl(imx->mmio_base + MX3_PWMSR);
207 fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr);
208 if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) {
209 period_ms = DIV_ROUND_UP_ULL(pwm->state.period,
213 sr = readl(imx->mmio_base + MX3_PWMSR);
214 if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr))
215 dev_warn(dev, "there is no free FIFO slot\n");
219 static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm,
220 const struct pwm_state *state)
222 unsigned long period_cycles, duty_cycles, prescale;
223 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip);
224 unsigned long long c;
225 unsigned long long clkrate;
229 clkrate = clk_get_rate(imx->clk_per);
230 c = clkrate * state->period;
232 do_div(c, NSEC_PER_SEC);
235 prescale = period_cycles / 0x10000 + 1;
237 period_cycles /= prescale;
238 c = clkrate * state->duty_cycle;
239 do_div(c, NSEC_PER_SEC);
241 duty_cycles /= prescale;
244 * according to imx pwm RM, the real period value should be PERIOD
245 * value in PWMPR plus 2.
247 if (period_cycles > 2)
253 * Wait for a free FIFO slot if the PWM is already enabled, and flush
254 * the FIFO if the PWM was disabled and is about to be enabled.
256 if (pwm->state.enabled) {
257 pwm_imx27_wait_fifo_slot(chip, pwm);
259 ret = pwm_imx27_clk_prepare_enable(imx);
263 pwm_imx27_sw_reset(chip);
266 writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
267 writel(period_cycles, imx->mmio_base + MX3_PWMPR);
270 * Store the duty cycle for future reference in cases where the
271 * MX3_PWMSAR register can't be read (i.e. when the PWM is disabled).
273 imx->duty_cycle = duty_cycles;
275 cr = MX3_PWMCR_PRESCALER_SET(prescale) |
276 MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN |
277 FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) |
280 if (state->polarity == PWM_POLARITY_INVERSED)
281 cr |= FIELD_PREP(MX3_PWMCR_POUTC,
282 MX3_PWMCR_POUTC_INVERTED);
287 writel(cr, imx->mmio_base + MX3_PWMCR);
290 pwm_imx27_clk_disable_unprepare(imx);
295 static const struct pwm_ops pwm_imx27_ops = {
296 .apply = pwm_imx27_apply,
297 .get_state = pwm_imx27_get_state,
300 static const struct of_device_id pwm_imx27_dt_ids[] = {
301 { .compatible = "fsl,imx27-pwm", },
304 MODULE_DEVICE_TABLE(of, pwm_imx27_dt_ids);
306 static int pwm_imx27_probe(struct platform_device *pdev)
308 struct pwm_chip *chip;
309 struct pwm_imx27_chip *imx;
313 chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*imx));
315 return PTR_ERR(chip);
316 imx = to_pwm_imx27_chip(chip);
318 imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
319 if (IS_ERR(imx->clk_ipg))
320 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_ipg),
321 "getting ipg clock failed\n");
323 imx->clk_per = devm_clk_get(&pdev->dev, "per");
324 if (IS_ERR(imx->clk_per))
325 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per),
326 "failed to get peripheral clock\n");
328 chip->ops = &pwm_imx27_ops;
330 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
331 if (IS_ERR(imx->mmio_base))
332 return PTR_ERR(imx->mmio_base);
334 ret = pwm_imx27_clk_prepare_enable(imx);
338 /* keep clks on if pwm is running */
339 pwmcr = readl(imx->mmio_base + MX3_PWMCR);
340 if (!(pwmcr & MX3_PWMCR_EN))
341 pwm_imx27_clk_disable_unprepare(imx);
343 return devm_pwmchip_add(&pdev->dev, chip);
346 static struct platform_driver imx_pwm_driver = {
349 .of_match_table = pwm_imx27_dt_ids,
351 .probe = pwm_imx27_probe,
353 module_platform_driver(imx_pwm_driver);
355 MODULE_DESCRIPTION("i.MX27 and later i.MX SoCs Pulse Width Modulator driver");
356 MODULE_LICENSE("GPL v2");