1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2016 Broadcom
5 #include <linux/delay.h>
8 #include <linux/math64.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pwm.h>
14 #define IPROC_PWM_CTRL_OFFSET 0x00
15 #define IPROC_PWM_CTRL_TYPE_SHIFT(ch) (15 + (ch))
16 #define IPROC_PWM_CTRL_POLARITY_SHIFT(ch) (8 + (ch))
17 #define IPROC_PWM_CTRL_EN_SHIFT(ch) (ch)
19 #define IPROC_PWM_PERIOD_OFFSET(ch) (0x04 + ((ch) << 3))
20 #define IPROC_PWM_PERIOD_MIN 0x02
21 #define IPROC_PWM_PERIOD_MAX 0xffff
23 #define IPROC_PWM_DUTY_CYCLE_OFFSET(ch) (0x08 + ((ch) << 3))
24 #define IPROC_PWM_DUTY_CYCLE_MIN 0x00
25 #define IPROC_PWM_DUTY_CYCLE_MAX 0xffff
27 #define IPROC_PWM_PRESCALE_OFFSET 0x24
28 #define IPROC_PWM_PRESCALE_BITS 0x06
29 #define IPROC_PWM_PRESCALE_SHIFT(ch) ((3 - (ch)) * \
30 IPROC_PWM_PRESCALE_BITS)
31 #define IPROC_PWM_PRESCALE_MASK(ch) (IPROC_PWM_PRESCALE_MAX << \
32 IPROC_PWM_PRESCALE_SHIFT(ch))
33 #define IPROC_PWM_PRESCALE_MIN 0x00
34 #define IPROC_PWM_PRESCALE_MAX 0x3f
42 static inline struct iproc_pwmc *to_iproc_pwmc(struct pwm_chip *chip)
44 return container_of(chip, struct iproc_pwmc, chip);
47 static void iproc_pwmc_enable(struct iproc_pwmc *ip, unsigned int channel)
51 value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
52 value |= 1 << IPROC_PWM_CTRL_EN_SHIFT(channel);
53 writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
55 /* must be a 400 ns delay between clearing and setting enable bit */
59 static void iproc_pwmc_disable(struct iproc_pwmc *ip, unsigned int channel)
63 value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
64 value &= ~(1 << IPROC_PWM_CTRL_EN_SHIFT(channel));
65 writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
67 /* must be a 400 ns delay between clearing and setting enable bit */
71 static int iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
72 struct pwm_state *state)
74 struct iproc_pwmc *ip = to_iproc_pwmc(chip);
78 value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
80 if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
81 state->enabled = true;
83 state->enabled = false;
85 if (value & BIT(IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm)))
86 state->polarity = PWM_POLARITY_NORMAL;
88 state->polarity = PWM_POLARITY_INVERSED;
90 rate = clk_get_rate(ip->clk);
93 state->duty_cycle = 0;
97 value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
98 prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
99 prescale &= IPROC_PWM_PRESCALE_MAX;
101 multi = NSEC_PER_SEC * (prescale + 1);
103 value = readl(ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
104 tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
105 state->period = div64_u64(tmp, rate);
107 value = readl(ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
108 tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
109 state->duty_cycle = div64_u64(tmp, rate);
114 static int iproc_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
115 const struct pwm_state *state)
117 unsigned long prescale = IPROC_PWM_PRESCALE_MIN;
118 struct iproc_pwmc *ip = to_iproc_pwmc(chip);
119 u32 value, period, duty;
122 rate = clk_get_rate(ip->clk);
125 * Find period count, duty count and prescale to suit duty_cycle and
126 * period. This is done according to formulas described below:
128 * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
129 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
131 * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
132 * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
137 div = NSEC_PER_SEC * (prescale + 1);
138 value = rate * state->period;
139 period = div64_u64(value, div);
140 value = rate * state->duty_cycle;
141 duty = div64_u64(value, div);
143 if (period < IPROC_PWM_PERIOD_MIN)
146 if (period <= IPROC_PWM_PERIOD_MAX &&
147 duty <= IPROC_PWM_DUTY_CYCLE_MAX)
150 /* Otherwise, increase prescale and recalculate counts */
151 if (++prescale > IPROC_PWM_PRESCALE_MAX)
155 iproc_pwmc_disable(ip, pwm->hwpwm);
158 value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
159 value &= ~IPROC_PWM_PRESCALE_MASK(pwm->hwpwm);
160 value |= prescale << IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
161 writel(value, ip->base + IPROC_PWM_PRESCALE_OFFSET);
163 /* set period and duty cycle */
164 writel(period, ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
165 writel(duty, ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
168 value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
170 if (state->polarity == PWM_POLARITY_NORMAL)
171 value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm);
173 value &= ~(1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm));
175 writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
178 iproc_pwmc_enable(ip, pwm->hwpwm);
183 static const struct pwm_ops iproc_pwm_ops = {
184 .apply = iproc_pwmc_apply,
185 .get_state = iproc_pwmc_get_state,
186 .owner = THIS_MODULE,
189 static int iproc_pwmc_probe(struct platform_device *pdev)
191 struct iproc_pwmc *ip;
196 ip = devm_kzalloc(&pdev->dev, sizeof(*ip), GFP_KERNEL);
200 platform_set_drvdata(pdev, ip);
202 ip->chip.dev = &pdev->dev;
203 ip->chip.ops = &iproc_pwm_ops;
206 ip->base = devm_platform_ioremap_resource(pdev, 0);
207 if (IS_ERR(ip->base))
208 return PTR_ERR(ip->base);
210 ip->clk = devm_clk_get(&pdev->dev, NULL);
211 if (IS_ERR(ip->clk)) {
212 dev_err(&pdev->dev, "failed to get clock: %ld\n",
214 return PTR_ERR(ip->clk);
217 ret = clk_prepare_enable(ip->clk);
219 dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
223 /* Set full drive and normal polarity for all channels */
224 value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
226 for (i = 0; i < ip->chip.npwm; i++) {
227 value &= ~(1 << IPROC_PWM_CTRL_TYPE_SHIFT(i));
228 value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(i);
231 writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
233 ret = pwmchip_add(&ip->chip);
235 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
236 clk_disable_unprepare(ip->clk);
242 static void iproc_pwmc_remove(struct platform_device *pdev)
244 struct iproc_pwmc *ip = platform_get_drvdata(pdev);
246 pwmchip_remove(&ip->chip);
248 clk_disable_unprepare(ip->clk);
251 static const struct of_device_id bcm_iproc_pwmc_dt[] = {
252 { .compatible = "brcm,iproc-pwm" },
255 MODULE_DEVICE_TABLE(of, bcm_iproc_pwmc_dt);
257 static struct platform_driver iproc_pwmc_driver = {
259 .name = "bcm-iproc-pwm",
260 .of_match_table = bcm_iproc_pwmc_dt,
262 .probe = iproc_pwmc_probe,
263 .remove_new = iproc_pwmc_remove,
265 module_platform_driver(iproc_pwmc_driver);
267 MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <yendapally.reddy@broadcom.com>");
268 MODULE_DESCRIPTION("Broadcom iProc PWM driver");
269 MODULE_LICENSE("GPL v2");