1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pwm.h>
14 #define PWM_CONTROL 0x000
15 #define PWM_CONTROL_SHIFT(x) ((x) * 8)
16 #define PWM_CONTROL_MASK 0xff
17 #define PWM_MODE 0x80 /* set timer in PWM mode */
18 #define PWM_ENABLE (1 << 0)
19 #define PWM_POLARITY (1 << 4)
21 #define PERIOD(x) (((x) * 0x10) + 0x10)
22 #define DUTY(x) (((x) * 0x10) + 0x14)
24 #define PERIOD_MIN 0x2
34 static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip)
36 return container_of(chip, struct bcm2835_pwm, chip);
39 static int bcm2835_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
41 struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
44 value = readl(pc->base + PWM_CONTROL);
45 value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
46 value |= (PWM_MODE << PWM_CONTROL_SHIFT(pwm->hwpwm));
47 writel(value, pc->base + PWM_CONTROL);
52 static void bcm2835_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
54 struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
57 value = readl(pc->base + PWM_CONTROL);
58 value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
59 writel(value, pc->base + PWM_CONTROL);
62 static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
63 const struct pwm_state *state)
66 struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
67 unsigned long long period_cycles;
73 * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC
74 * must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the
75 * multiplication period * rate doesn't overflow.
76 * To calculate the maximal possible period that guarantees the
79 * round(period * rate / NSEC_PER_SEC) <= U32_MAX
80 * <=> period * rate / NSEC_PER_SEC < U32_MAX + 0.5
81 * <=> period * rate < (U32_MAX + 0.5) * NSEC_PER_SEC
82 * <=> period < ((U32_MAX + 0.5) * NSEC_PER_SEC) / rate
83 * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate
84 * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1
86 max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1;
88 if (state->period > max_period)
92 period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC);
94 /* don't accept a period that is too small */
95 if (period_cycles < PERIOD_MIN)
98 writel(period_cycles, pc->base + PERIOD(pwm->hwpwm));
101 val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC);
102 writel(val, pc->base + DUTY(pwm->hwpwm));
105 val = readl(pc->base + PWM_CONTROL);
107 if (state->polarity == PWM_POLARITY_NORMAL)
108 val &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm));
110 val |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm);
114 val |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm);
116 val &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm));
118 writel(val, pc->base + PWM_CONTROL);
123 static const struct pwm_ops bcm2835_pwm_ops = {
124 .request = bcm2835_pwm_request,
125 .free = bcm2835_pwm_free,
126 .apply = bcm2835_pwm_apply,
129 static void devm_clk_rate_exclusive_put(void *data)
131 struct clk *clk = data;
133 clk_rate_exclusive_put(clk);
136 static int bcm2835_pwm_probe(struct platform_device *pdev)
138 struct bcm2835_pwm *pc;
141 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
145 pc->dev = &pdev->dev;
147 pc->base = devm_platform_ioremap_resource(pdev, 0);
148 if (IS_ERR(pc->base))
149 return PTR_ERR(pc->base);
151 pc->clk = devm_clk_get_enabled(&pdev->dev, NULL);
153 return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
154 "clock not found\n");
156 ret = clk_rate_exclusive_get(pc->clk);
158 return dev_err_probe(&pdev->dev, ret,
159 "fail to get exclusive rate\n");
161 ret = devm_add_action_or_reset(&pdev->dev, devm_clk_rate_exclusive_put,
164 clk_rate_exclusive_put(pc->clk);
168 pc->rate = clk_get_rate(pc->clk);
170 return dev_err_probe(&pdev->dev, -EINVAL,
171 "failed to get clock rate\n");
173 pc->chip.dev = &pdev->dev;
174 pc->chip.ops = &bcm2835_pwm_ops;
175 pc->chip.atomic = true;
178 platform_set_drvdata(pdev, pc);
180 ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
182 return dev_err_probe(&pdev->dev, ret,
183 "failed to add pwmchip\n");
188 static int bcm2835_pwm_suspend(struct device *dev)
190 struct bcm2835_pwm *pc = dev_get_drvdata(dev);
192 clk_disable_unprepare(pc->clk);
197 static int bcm2835_pwm_resume(struct device *dev)
199 struct bcm2835_pwm *pc = dev_get_drvdata(dev);
201 return clk_prepare_enable(pc->clk);
204 static DEFINE_SIMPLE_DEV_PM_OPS(bcm2835_pwm_pm_ops, bcm2835_pwm_suspend,
207 static const struct of_device_id bcm2835_pwm_of_match[] = {
208 { .compatible = "brcm,bcm2835-pwm", },
211 MODULE_DEVICE_TABLE(of, bcm2835_pwm_of_match);
213 static struct platform_driver bcm2835_pwm_driver = {
215 .name = "bcm2835-pwm",
216 .of_match_table = bcm2835_pwm_of_match,
217 .pm = pm_ptr(&bcm2835_pwm_pm_ops),
219 .probe = bcm2835_pwm_probe,
221 module_platform_driver(bcm2835_pwm_driver);
224 MODULE_DESCRIPTION("Broadcom BCM2835 PWM driver");
225 MODULE_LICENSE("GPL v2");