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/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pwm.h>
20 #include <linux/slab.h>
22 #define BERLIN_PWM_EN 0x0
23 #define BERLIN_PWM_ENABLE BIT(0)
24 #define BERLIN_PWM_CONTROL 0x4
26 * The prescaler claims to support 8 different moduli, configured using the
27 * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
28 * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be
29 * implemented by internally shifting TCNT left without adding additional
30 * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
31 * for 8, 0x1fff; and so on. This means that those moduli are entirely
32 * useless, as we could just do the shift ourselves. The 4096 modulus is
33 * implemented with a real prescaler, so we do use that, but we treat it
34 * as a flag instead of pretending the modulus is actually configurable.
36 #define BERLIN_PWM_PRESCALE_4096 0x7
37 #define BERLIN_PWM_INVERT_POLARITY BIT(3)
38 #define BERLIN_PWM_DUTY 0x8
39 #define BERLIN_PWM_TCNT 0xc
40 #define BERLIN_PWM_MAX_TCNT 65535
42 struct berlin_pwm_channel {
49 struct berlin_pwm_chip {
55 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
57 return container_of(chip, struct berlin_pwm_chip, chip);
60 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
61 unsigned int channel, unsigned long offset)
63 return readl_relaxed(bpc->base + channel * 0x10 + offset);
66 static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
67 unsigned int channel, u32 value,
70 writel_relaxed(value, bpc->base + channel * 0x10 + offset);
73 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
75 struct berlin_pwm_channel *channel;
77 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
81 return pwm_set_chip_data(pwm, channel);
84 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
86 struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
91 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
92 u64 duty_ns, u64 period_ns)
94 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
95 bool prescale_4096 = false;
96 u32 value, duty, period;
99 cycles = clk_get_rate(bpc->clk);
101 do_div(cycles, NSEC_PER_SEC);
103 if (cycles > BERLIN_PWM_MAX_TCNT) {
104 prescale_4096 = true;
105 cycles >>= 12; // Prescaled by 4096
107 if (cycles > BERLIN_PWM_MAX_TCNT)
113 do_div(cycles, period_ns);
116 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
118 value |= BERLIN_PWM_PRESCALE_4096;
120 value &= ~BERLIN_PWM_PRESCALE_4096;
121 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
123 berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
124 berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
129 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
130 struct pwm_device *pwm,
131 enum pwm_polarity polarity)
133 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
136 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
138 if (polarity == PWM_POLARITY_NORMAL)
139 value &= ~BERLIN_PWM_INVERT_POLARITY;
141 value |= BERLIN_PWM_INVERT_POLARITY;
143 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
148 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
150 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
153 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
154 value |= BERLIN_PWM_ENABLE;
155 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
160 static void berlin_pwm_disable(struct pwm_chip *chip,
161 struct pwm_device *pwm)
163 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
166 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
167 value &= ~BERLIN_PWM_ENABLE;
168 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
171 static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
172 const struct pwm_state *state)
175 bool enabled = pwm->state.enabled;
177 if (state->polarity != pwm->state.polarity) {
179 berlin_pwm_disable(chip, pwm);
183 err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
188 if (!state->enabled) {
190 berlin_pwm_disable(chip, pwm);
194 err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
199 return berlin_pwm_enable(chip, pwm);
204 static const struct pwm_ops berlin_pwm_ops = {
205 .request = berlin_pwm_request,
206 .free = berlin_pwm_free,
207 .apply = berlin_pwm_apply,
208 .owner = THIS_MODULE,
211 static const struct of_device_id berlin_pwm_match[] = {
212 { .compatible = "marvell,berlin-pwm" },
215 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
217 static int berlin_pwm_probe(struct platform_device *pdev)
219 struct berlin_pwm_chip *bpc;
222 bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
226 bpc->base = devm_platform_ioremap_resource(pdev, 0);
227 if (IS_ERR(bpc->base))
228 return PTR_ERR(bpc->base);
230 bpc->clk = devm_clk_get(&pdev->dev, NULL);
231 if (IS_ERR(bpc->clk))
232 return PTR_ERR(bpc->clk);
234 ret = clk_prepare_enable(bpc->clk);
238 bpc->chip.dev = &pdev->dev;
239 bpc->chip.ops = &berlin_pwm_ops;
242 ret = pwmchip_add(&bpc->chip);
244 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
245 clk_disable_unprepare(bpc->clk);
249 platform_set_drvdata(pdev, bpc);
254 static void berlin_pwm_remove(struct platform_device *pdev)
256 struct berlin_pwm_chip *bpc = platform_get_drvdata(pdev);
258 pwmchip_remove(&bpc->chip);
260 clk_disable_unprepare(bpc->clk);
263 #ifdef CONFIG_PM_SLEEP
264 static int berlin_pwm_suspend(struct device *dev)
266 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
269 for (i = 0; i < bpc->chip.npwm; i++) {
270 struct berlin_pwm_channel *channel;
272 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
276 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
277 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
278 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
279 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
282 clk_disable_unprepare(bpc->clk);
287 static int berlin_pwm_resume(struct device *dev)
289 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
293 ret = clk_prepare_enable(bpc->clk);
297 for (i = 0; i < bpc->chip.npwm; i++) {
298 struct berlin_pwm_channel *channel;
300 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
304 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
305 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
306 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
307 berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
314 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
317 static struct platform_driver berlin_pwm_driver = {
318 .probe = berlin_pwm_probe,
319 .remove_new = berlin_pwm_remove,
321 .name = "berlin-pwm",
322 .of_match_table = berlin_pwm_match,
323 .pm = &berlin_pwm_pm_ops,
326 module_platform_driver(berlin_pwm_driver);
328 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
329 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
330 MODULE_LICENSE("GPL v2");