2 * Marvell Berlin PWM driver
4 * Copyright (C) 2015 Marvell Technology Group Ltd.
6 * Author: Antoine Tenart <antoine.tenart@free-electrons.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #include <linux/clk.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
21 #define BERLIN_PWM_EN 0x0
22 #define BERLIN_PWM_ENABLE BIT(0)
23 #define BERLIN_PWM_CONTROL 0x4
25 * The prescaler claims to support 8 different moduli, configured using the
26 * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
27 * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be
28 * implemented by internally shifting TCNT left without adding additional
29 * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
30 * for 8, 0x1fff; and so on. This means that those moduli are entirely
31 * useless, as we could just do the shift ourselves. The 4096 modulus is
32 * implemented with a real prescaler, so we do use that, but we treat it
33 * as a flag instead of pretending the modulus is actually configurable.
35 #define BERLIN_PWM_PRESCALE_4096 0x7
36 #define BERLIN_PWM_INVERT_POLARITY BIT(3)
37 #define BERLIN_PWM_DUTY 0x8
38 #define BERLIN_PWM_TCNT 0xc
39 #define BERLIN_PWM_MAX_TCNT 65535
41 struct berlin_pwm_channel {
48 struct berlin_pwm_chip {
54 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
56 return container_of(chip, struct berlin_pwm_chip, chip);
59 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
60 unsigned int channel, unsigned long offset)
62 return readl_relaxed(bpc->base + channel * 0x10 + offset);
65 static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
66 unsigned int channel, u32 value,
69 writel_relaxed(value, bpc->base + channel * 0x10 + offset);
72 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
74 struct berlin_pwm_channel *channel;
76 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
80 return pwm_set_chip_data(pwm, channel);
83 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
85 struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
90 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
91 u64 duty_ns, u64 period_ns)
93 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
94 bool prescale_4096 = false;
95 u32 value, duty, period;
98 cycles = clk_get_rate(bpc->clk);
100 do_div(cycles, NSEC_PER_SEC);
102 if (cycles > BERLIN_PWM_MAX_TCNT) {
103 prescale_4096 = true;
104 cycles >>= 12; // Prescaled by 4096
106 if (cycles > BERLIN_PWM_MAX_TCNT)
112 do_div(cycles, period_ns);
115 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
117 value |= BERLIN_PWM_PRESCALE_4096;
119 value &= ~BERLIN_PWM_PRESCALE_4096;
120 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
122 berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
123 berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
128 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
129 struct pwm_device *pwm,
130 enum pwm_polarity polarity)
132 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
135 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
137 if (polarity == PWM_POLARITY_NORMAL)
138 value &= ~BERLIN_PWM_INVERT_POLARITY;
140 value |= BERLIN_PWM_INVERT_POLARITY;
142 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
147 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
149 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
152 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
153 value |= BERLIN_PWM_ENABLE;
154 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
159 static void berlin_pwm_disable(struct pwm_chip *chip,
160 struct pwm_device *pwm)
162 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
165 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
166 value &= ~BERLIN_PWM_ENABLE;
167 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
170 static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
171 const struct pwm_state *state)
174 bool enabled = pwm->state.enabled;
176 if (state->polarity != pwm->state.polarity) {
178 berlin_pwm_disable(chip, pwm);
182 err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
187 if (!state->enabled) {
189 berlin_pwm_disable(chip, pwm);
193 err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
198 return berlin_pwm_enable(chip, pwm);
203 static const struct pwm_ops berlin_pwm_ops = {
204 .request = berlin_pwm_request,
205 .free = berlin_pwm_free,
206 .apply = berlin_pwm_apply,
207 .owner = THIS_MODULE,
210 static const struct of_device_id berlin_pwm_match[] = {
211 { .compatible = "marvell,berlin-pwm" },
214 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
216 static int berlin_pwm_probe(struct platform_device *pdev)
218 struct berlin_pwm_chip *bpc;
221 bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
225 bpc->base = devm_platform_ioremap_resource(pdev, 0);
226 if (IS_ERR(bpc->base))
227 return PTR_ERR(bpc->base);
229 bpc->clk = devm_clk_get(&pdev->dev, NULL);
230 if (IS_ERR(bpc->clk))
231 return PTR_ERR(bpc->clk);
233 ret = clk_prepare_enable(bpc->clk);
237 bpc->chip.dev = &pdev->dev;
238 bpc->chip.ops = &berlin_pwm_ops;
241 ret = pwmchip_add(&bpc->chip);
243 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
244 clk_disable_unprepare(bpc->clk);
248 platform_set_drvdata(pdev, bpc);
253 static int berlin_pwm_remove(struct platform_device *pdev)
255 struct berlin_pwm_chip *bpc = platform_get_drvdata(pdev);
257 pwmchip_remove(&bpc->chip);
259 clk_disable_unprepare(bpc->clk);
264 #ifdef CONFIG_PM_SLEEP
265 static int berlin_pwm_suspend(struct device *dev)
267 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
270 for (i = 0; i < bpc->chip.npwm; i++) {
271 struct berlin_pwm_channel *channel;
273 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
277 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
278 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
279 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
280 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
283 clk_disable_unprepare(bpc->clk);
288 static int berlin_pwm_resume(struct device *dev)
290 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
294 ret = clk_prepare_enable(bpc->clk);
298 for (i = 0; i < bpc->chip.npwm; i++) {
299 struct berlin_pwm_channel *channel;
301 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
305 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
306 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
307 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
308 berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
315 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
318 static struct platform_driver berlin_pwm_driver = {
319 .probe = berlin_pwm_probe,
320 .remove = berlin_pwm_remove,
322 .name = "berlin-pwm",
323 .of_match_table = berlin_pwm_match,
324 .pm = &berlin_pwm_pm_ops,
327 module_platform_driver(berlin_pwm_driver);
329 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
330 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
331 MODULE_LICENSE("GPL v2");