1 // SPDX-License-Identifier: GPL-2.0-only
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/pwm.h>
15 #include <linux/slab.h>
17 struct lpc32xx_pwm_chip {
22 #define PWM_ENABLE BIT(31)
23 #define PWM_PIN_LEVEL BIT(30)
25 static inline struct lpc32xx_pwm_chip *to_lpc32xx_pwm_chip(struct pwm_chip *chip)
27 return pwmchip_get_drvdata(chip);
30 static int lpc32xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
31 int duty_ns, int period_ns)
33 struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
35 int period_cycles, duty_cycles;
37 c = clk_get_rate(lpc32xx->clk);
39 /* The highest acceptable divisor is 256, which is represented by 0 */
40 period_cycles = div64_u64(c * period_ns,
41 (unsigned long long)NSEC_PER_SEC * 256);
42 if (!period_cycles || period_cycles > 256)
44 if (period_cycles == 256)
47 /* Compute 256 x #duty/period value and care for corner cases */
48 duty_cycles = div64_u64((unsigned long long)(period_ns - duty_ns) * 256,
52 if (duty_cycles > 255)
55 val = readl(lpc32xx->base);
57 val |= (period_cycles << 8) | duty_cycles;
58 writel(val, lpc32xx->base);
63 static int lpc32xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
65 struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
69 ret = clk_prepare_enable(lpc32xx->clk);
73 val = readl(lpc32xx->base);
75 writel(val, lpc32xx->base);
80 static void lpc32xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
82 struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
85 val = readl(lpc32xx->base);
87 writel(val, lpc32xx->base);
89 clk_disable_unprepare(lpc32xx->clk);
92 static int lpc32xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
93 const struct pwm_state *state)
97 if (state->polarity != PWM_POLARITY_NORMAL)
100 if (!state->enabled) {
101 if (pwm->state.enabled)
102 lpc32xx_pwm_disable(chip, pwm);
107 err = lpc32xx_pwm_config(chip, pwm, state->duty_cycle, state->period);
111 if (!pwm->state.enabled)
112 err = lpc32xx_pwm_enable(chip, pwm);
117 static const struct pwm_ops lpc32xx_pwm_ops = {
118 .apply = lpc32xx_pwm_apply,
121 static int lpc32xx_pwm_probe(struct platform_device *pdev)
123 struct pwm_chip *chip;
124 struct lpc32xx_pwm_chip *lpc32xx;
128 chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*lpc32xx));
130 return PTR_ERR(chip);
131 lpc32xx = to_lpc32xx_pwm_chip(chip);
133 lpc32xx->base = devm_platform_ioremap_resource(pdev, 0);
134 if (IS_ERR(lpc32xx->base))
135 return PTR_ERR(lpc32xx->base);
137 lpc32xx->clk = devm_clk_get(&pdev->dev, NULL);
138 if (IS_ERR(lpc32xx->clk))
139 return PTR_ERR(lpc32xx->clk);
141 chip->ops = &lpc32xx_pwm_ops;
143 /* If PWM is disabled, configure the output to the default value */
144 val = readl(lpc32xx->base);
145 val &= ~PWM_PIN_LEVEL;
146 writel(val, lpc32xx->base);
148 ret = devm_pwmchip_add(&pdev->dev, chip);
150 dev_err(&pdev->dev, "failed to add PWM chip, error %d\n", ret);
157 static const struct of_device_id lpc32xx_pwm_dt_ids[] = {
158 { .compatible = "nxp,lpc3220-pwm", },
161 MODULE_DEVICE_TABLE(of, lpc32xx_pwm_dt_ids);
163 static struct platform_driver lpc32xx_pwm_driver = {
165 .name = "lpc32xx-pwm",
166 .of_match_table = lpc32xx_pwm_dt_ids,
168 .probe = lpc32xx_pwm_probe,
170 module_platform_driver(lpc32xx_pwm_driver);
172 MODULE_ALIAS("platform:lpc32xx-pwm");
174 MODULE_DESCRIPTION("LPC32XX PWM Driver");
175 MODULE_LICENSE("GPL v2");