2 * Marvell Berlin PWM driver
4 * Copyright (C) 2015 Marvell Technology Group Ltd.
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 #define BERLIN_PWM_NUMPWMS 4
44 struct berlin_pwm_channel {
51 struct berlin_pwm_chip {
55 struct berlin_pwm_channel channel[BERLIN_PWM_NUMPWMS];
58 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
60 return container_of(chip, struct berlin_pwm_chip, chip);
63 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
64 unsigned int channel, unsigned long offset)
66 return readl_relaxed(bpc->base + channel * 0x10 + offset);
69 static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
70 unsigned int channel, u32 value,
73 writel_relaxed(value, bpc->base + channel * 0x10 + offset);
76 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
77 u64 duty_ns, u64 period_ns)
79 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
80 bool prescale_4096 = false;
81 u32 value, duty, period;
84 cycles = clk_get_rate(bpc->clk);
86 do_div(cycles, NSEC_PER_SEC);
88 if (cycles > BERLIN_PWM_MAX_TCNT) {
90 cycles >>= 12; // Prescaled by 4096
92 if (cycles > BERLIN_PWM_MAX_TCNT)
98 do_div(cycles, period_ns);
101 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
103 value |= BERLIN_PWM_PRESCALE_4096;
105 value &= ~BERLIN_PWM_PRESCALE_4096;
106 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
108 berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
109 berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
114 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
115 struct pwm_device *pwm,
116 enum pwm_polarity polarity)
118 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
121 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
123 if (polarity == PWM_POLARITY_NORMAL)
124 value &= ~BERLIN_PWM_INVERT_POLARITY;
126 value |= BERLIN_PWM_INVERT_POLARITY;
128 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
133 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
135 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
138 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
139 value |= BERLIN_PWM_ENABLE;
140 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
145 static void berlin_pwm_disable(struct pwm_chip *chip,
146 struct pwm_device *pwm)
148 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
151 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
152 value &= ~BERLIN_PWM_ENABLE;
153 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
156 static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
157 const struct pwm_state *state)
160 bool enabled = pwm->state.enabled;
162 if (state->polarity != pwm->state.polarity) {
164 berlin_pwm_disable(chip, pwm);
168 err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
173 if (!state->enabled) {
175 berlin_pwm_disable(chip, pwm);
179 err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
184 return berlin_pwm_enable(chip, pwm);
189 static const struct pwm_ops berlin_pwm_ops = {
190 .apply = berlin_pwm_apply,
193 static const struct of_device_id berlin_pwm_match[] = {
194 { .compatible = "marvell,berlin-pwm" },
197 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
199 static int berlin_pwm_probe(struct platform_device *pdev)
201 struct berlin_pwm_chip *bpc;
204 bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
208 bpc->base = devm_platform_ioremap_resource(pdev, 0);
209 if (IS_ERR(bpc->base))
210 return PTR_ERR(bpc->base);
212 bpc->clk = devm_clk_get_enabled(&pdev->dev, NULL);
213 if (IS_ERR(bpc->clk))
214 return PTR_ERR(bpc->clk);
216 bpc->chip.dev = &pdev->dev;
217 bpc->chip.ops = &berlin_pwm_ops;
218 bpc->chip.npwm = BERLIN_PWM_NUMPWMS;
220 ret = devm_pwmchip_add(&pdev->dev, &bpc->chip);
222 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
224 platform_set_drvdata(pdev, bpc);
229 static int berlin_pwm_suspend(struct device *dev)
231 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
234 for (i = 0; i < bpc->chip.npwm; i++) {
235 struct berlin_pwm_channel *channel = &bpc->channel[i];
237 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
238 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
239 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
240 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
243 clk_disable_unprepare(bpc->clk);
248 static int berlin_pwm_resume(struct device *dev)
250 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
254 ret = clk_prepare_enable(bpc->clk);
258 for (i = 0; i < bpc->chip.npwm; i++) {
259 struct berlin_pwm_channel *channel = &bpc->channel[i];
261 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
262 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
263 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
264 berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
270 static DEFINE_SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
273 static struct platform_driver berlin_pwm_driver = {
274 .probe = berlin_pwm_probe,
276 .name = "berlin-pwm",
277 .of_match_table = berlin_pwm_match,
278 .pm = pm_ptr(&berlin_pwm_pm_ops),
281 module_platform_driver(berlin_pwm_driver);
284 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
285 MODULE_LICENSE("GPL v2");