1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Broadcom BCM7038 PWM driver
4 * Author: Florian Fainelli
6 * Copyright (C) 2015 Broadcom Corporation
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/clk.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pwm.h>
20 #include <linux/spinlock.h>
23 #define CTRL_START BIT(0)
24 #define CTRL_OEB BIT(1)
25 #define CTRL_FORCE_HIGH BIT(2)
26 #define CTRL_OPENDRAIN BIT(3)
27 #define CTRL_CHAN_OFFS 4
29 #define PWM_CTRL2 0x04
30 #define CTRL2_OUT_SELECT BIT(0)
32 #define PWM_CH_SIZE 0x8
34 #define PWM_CWORD_MSB(ch) (0x08 + ((ch) * PWM_CH_SIZE))
35 #define PWM_CWORD_LSB(ch) (0x0c + ((ch) * PWM_CH_SIZE))
37 /* Number of bits for the CWORD value */
38 #define CWORD_BIT_SIZE 16
41 * Maximum control word value allowed when variable-frequency PWM is used as a
42 * clock for the constant-frequency PMW.
44 #define CONST_VAR_F_MAX 32768
45 #define CONST_VAR_F_MIN 1
47 #define PWM_ON(ch) (0x18 + ((ch) * PWM_CH_SIZE))
49 #define PWM_PERIOD(ch) (0x1c + ((ch) * PWM_CH_SIZE))
50 #define PWM_PERIOD_MIN 0
52 #define PWM_ON_PERIOD_MAX 0xff
60 static inline u32 brcmstb_pwm_readl(struct brcmstb_pwm *p,
63 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
64 return __raw_readl(p->base + offset);
66 return readl_relaxed(p->base + offset);
69 static inline void brcmstb_pwm_writel(struct brcmstb_pwm *p, u32 value,
72 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
73 __raw_writel(value, p->base + offset);
75 writel_relaxed(value, p->base + offset);
78 static inline struct brcmstb_pwm *to_brcmstb_pwm(struct pwm_chip *chip)
80 return container_of(chip, struct brcmstb_pwm, chip);
84 * Fv is derived from the variable frequency output. The variable frequency
85 * output is configured using this formula:
87 * W = cword, if cword < 2 ^ 15 else 16-bit 2's complement of cword
89 * Fv = W x 2 ^ -16 x 27Mhz (reference clock)
91 * The period is: (period + 1) / Fv and "on" time is on / (period + 1)
93 * The PWM core framework specifies that the "duty_ns" parameter is in fact the
94 * "on" time, so this translates directly into our HW programming here.
96 static int brcmstb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
97 u64 duty_ns, u64 period_ns)
99 struct brcmstb_pwm *p = to_brcmstb_pwm(chip);
100 unsigned long pc, dc, cword = CONST_VAR_F_MAX;
101 unsigned int channel = pwm->hwpwm;
105 * If asking for a duty_ns equal to period_ns, we need to substract
106 * the period value by 1 to make it shorter than the "on" time and
107 * produce a flat 100% duty cycle signal, and max out the "on" time
109 if (duty_ns == period_ns) {
110 dc = PWM_ON_PERIOD_MAX;
111 pc = PWM_ON_PERIOD_MAX - 1;
119 * Calculate the base rate from base frequency and current
122 rate = (u64)clk_get_rate(p->clk) * (u64)cword;
123 rate >>= CWORD_BIT_SIZE;
125 pc = mul_u64_u64_div_u64(period_ns, rate, NSEC_PER_SEC);
126 dc = mul_u64_u64_div_u64(duty_ns + 1, rate, NSEC_PER_SEC);
129 * We can be called with separate duty and period updates,
130 * so do not reject dc == 0 right away
132 if (pc == PWM_PERIOD_MIN || (dc < PWM_ON_MIN && duty_ns))
135 /* We converged on a calculation */
136 if (pc <= PWM_ON_PERIOD_MAX && dc <= PWM_ON_PERIOD_MAX)
140 * The cword needs to be a power of 2 for the variable
141 * frequency generator to output a 50% duty cycle variable
142 * frequency which is used as input clock to the fixed
143 * frequency generator.
148 * Desired periods are too large, we do not have a divider
151 if (cword < CONST_VAR_F_MIN)
157 * Configure the defined "cword" value to have the variable frequency
158 * generator output a base frequency for the constant frequency
159 * generator to derive from.
161 brcmstb_pwm_writel(p, cword >> 8, PWM_CWORD_MSB(channel));
162 brcmstb_pwm_writel(p, cword & 0xff, PWM_CWORD_LSB(channel));
164 /* Select constant frequency signal output */
165 value = brcmstb_pwm_readl(p, PWM_CTRL2);
166 value |= CTRL2_OUT_SELECT << (channel * CTRL_CHAN_OFFS);
167 brcmstb_pwm_writel(p, value, PWM_CTRL2);
169 /* Configure on and period value */
170 brcmstb_pwm_writel(p, pc, PWM_PERIOD(channel));
171 brcmstb_pwm_writel(p, dc, PWM_ON(channel));
176 static inline void brcmstb_pwm_enable_set(struct brcmstb_pwm *p,
177 unsigned int channel, bool enable)
179 unsigned int shift = channel * CTRL_CHAN_OFFS;
182 value = brcmstb_pwm_readl(p, PWM_CTRL);
185 value &= ~(CTRL_OEB << shift);
186 value |= (CTRL_START | CTRL_OPENDRAIN) << shift;
188 value &= ~((CTRL_START | CTRL_OPENDRAIN) << shift);
189 value |= CTRL_OEB << shift;
192 brcmstb_pwm_writel(p, value, PWM_CTRL);
195 static int brcmstb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
196 const struct pwm_state *state)
198 struct brcmstb_pwm *p = to_brcmstb_pwm(chip);
201 if (state->polarity != PWM_POLARITY_NORMAL)
204 if (!state->enabled) {
205 if (pwm->state.enabled)
206 brcmstb_pwm_enable_set(p, pwm->hwpwm, false);
211 err = brcmstb_pwm_config(chip, pwm, state->duty_cycle, state->period);
215 if (!pwm->state.enabled)
216 brcmstb_pwm_enable_set(p, pwm->hwpwm, true);
221 static const struct pwm_ops brcmstb_pwm_ops = {
222 .apply = brcmstb_pwm_apply,
223 .owner = THIS_MODULE,
226 static const struct of_device_id brcmstb_pwm_of_match[] = {
227 { .compatible = "brcm,bcm7038-pwm", },
230 MODULE_DEVICE_TABLE(of, brcmstb_pwm_of_match);
232 static int brcmstb_pwm_probe(struct platform_device *pdev)
234 struct brcmstb_pwm *p;
237 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
241 p->clk = devm_clk_get(&pdev->dev, NULL);
242 if (IS_ERR(p->clk)) {
243 dev_err(&pdev->dev, "failed to obtain clock\n");
244 return PTR_ERR(p->clk);
247 ret = clk_prepare_enable(p->clk);
249 dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
253 platform_set_drvdata(pdev, p);
255 p->chip.dev = &pdev->dev;
256 p->chip.ops = &brcmstb_pwm_ops;
259 p->base = devm_platform_ioremap_resource(pdev, 0);
260 if (IS_ERR(p->base)) {
261 ret = PTR_ERR(p->base);
265 ret = pwmchip_add(&p->chip);
267 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
274 clk_disable_unprepare(p->clk);
278 static int brcmstb_pwm_remove(struct platform_device *pdev)
280 struct brcmstb_pwm *p = platform_get_drvdata(pdev);
282 pwmchip_remove(&p->chip);
283 clk_disable_unprepare(p->clk);
288 #ifdef CONFIG_PM_SLEEP
289 static int brcmstb_pwm_suspend(struct device *dev)
291 struct brcmstb_pwm *p = dev_get_drvdata(dev);
298 static int brcmstb_pwm_resume(struct device *dev)
300 struct brcmstb_pwm *p = dev_get_drvdata(dev);
308 static SIMPLE_DEV_PM_OPS(brcmstb_pwm_pm_ops, brcmstb_pwm_suspend,
311 static struct platform_driver brcmstb_pwm_driver = {
312 .probe = brcmstb_pwm_probe,
313 .remove = brcmstb_pwm_remove,
315 .name = "pwm-brcmstb",
316 .of_match_table = brcmstb_pwm_of_match,
317 .pm = &brcmstb_pwm_pm_ops,
320 module_platform_driver(brcmstb_pwm_driver);
322 MODULE_AUTHOR("Florian Fainelli <f.fainelli@gmail.com>");
323 MODULE_DESCRIPTION("Broadcom STB PWM driver");
324 MODULE_ALIAS("platform:pwm-brcmstb");
325 MODULE_LICENSE("GPL");