1 --- a/drivers/pwm/Kconfig
2 +++ b/drivers/pwm/Kconfig
3 @@ -263,6 +263,15 @@ config PWM_STI
4 To compile this driver as a module, choose M here: the module
5 will be called pwm-sti.
8 + tristate "Allwinner sun4i PWM support"
9 + depends on ARCH_SUNXI || COMPILE_TEST
11 + Generic PWM framework driver for Allwinner sun4i and sun7i SoCs.
13 + To compile this driver as a module, choose M here: the module
14 + will be called pwm-sun4i.
17 tristate "NVIDIA Tegra PWM support"
19 --- a/drivers/pwm/Makefile
20 +++ b/drivers/pwm/Makefile
21 @@ -24,6 +24,7 @@ obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockch
22 obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
23 obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
24 obj-$(CONFIG_PWM_STI) += pwm-sti.o
25 +obj-$(CONFIG_PWM_SUN4I) += pwm-sun4i.o
26 obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
27 obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o
28 obj-$(CONFIG_PWM_TIEHRPWM) += pwm-tiehrpwm.o
30 +++ b/drivers/pwm/pwm-sun4i.c
33 + * Driver for Allwinner sun4i Pulse Width Modulation Controller
35 + * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com>
37 + * Licensed under GPLv2.
40 +#include <linux/bitops.h>
41 +#include <linux/clk.h>
42 +#include <linux/err.h>
43 +#include <linux/io.h>
44 +#include <linux/module.h>
45 +#include <linux/mutex.h>
46 +#include <linux/of.h>
47 +#include <linux/of_device.h>
48 +#include <linux/platform_device.h>
49 +#include <linux/pwm.h>
50 +#include <linux/slab.h>
51 +#include <linux/time.h>
53 +#define PWM_CTRL_REG 0x0
55 +#define PWM_CH_PRD_BASE 0x4
56 +#define PWM_CH_PRD_OFFSET 0x4
57 +#define PWM_CH_PRD(ch) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFFSET * (ch))
59 +#define PWMCH_OFFSET 15
60 +#define PWM_PRESCAL_MASK GENMASK(3, 0)
61 +#define PWM_PRESCAL_OFF 0
62 +#define PWM_EN BIT(4)
63 +#define PWM_ACT_STATE BIT(5)
64 +#define PWM_CLK_GATING BIT(6)
65 +#define PWM_MODE BIT(7)
66 +#define PWM_PULSE BIT(8)
67 +#define PWM_BYPASS BIT(9)
69 +#define PWM_RDY_BASE 28
70 +#define PWM_RDY_OFFSET 1
71 +#define PWM_RDY(ch) BIT(PWM_RDY_BASE + PWM_RDY_OFFSET * (ch))
73 +#define PWM_PRD(prd) (((prd) - 1) << 16)
74 +#define PWM_PRD_MASK GENMASK(15, 0)
76 +#define PWM_DTY_MASK GENMASK(15, 0)
78 +#define BIT_CH(bit, chan) ((bit) << ((chan) * PWMCH_OFFSET))
80 +static const u32 prescaler_table[] = {
96 + 0, /* Actually 1 but tested separately */
99 +struct sun4i_pwm_data {
100 + bool has_prescaler_bypass;
104 +struct sun4i_pwm_chip {
105 + struct pwm_chip chip;
107 + void __iomem *base;
108 + struct mutex ctrl_lock;
109 + const struct sun4i_pwm_data *data;
112 +static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
114 + return container_of(chip, struct sun4i_pwm_chip, chip);
117 +static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip,
118 + unsigned long offset)
120 + return readl(chip->base + offset);
123 +static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip,
124 + u32 val, unsigned long offset)
126 + writel(val, chip->base + offset);
129 +static int sun4i_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
130 + int duty_ns, int period_ns)
132 + struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
133 + u32 clk_rate, prd, dty, val, clk_gate;
135 + unsigned int prescaler = 0;
138 + clk_rate = clk_get_rate(sun4i_pwm->clk);
140 + if (sun4i_pwm->data->has_prescaler_bypass) {
141 + /* First, test without any prescaler when available */
142 + prescaler = PWM_PRESCAL_MASK;
144 + * When not using any prescaler, the clock period in nanoseconds
145 + * is not an integer so round it half up instead of
146 + * truncating to get less surprising values.
148 + div = clk_rate * (u64)period_ns + NSEC_PER_SEC/2;
149 + do_div(div, NSEC_PER_SEC);
150 + if (div - 1 > PWM_PRD_MASK)
154 + if (prescaler == 0) {
155 + /* Go up from the first divider */
156 + for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) {
157 + if (!prescaler_table[prescaler])
159 + div = clk_rate / prescaler_table[prescaler];
160 + div = div * (u64)period_ns;
161 + do_div(div, NSEC_PER_SEC);
162 + if (div - 1 <= PWM_PRD_MASK)
166 + if (div - 1 > PWM_PRD_MASK) {
167 + dev_err(chip->dev, "period exceeds the maximum value\n");
174 + do_div(div, period_ns);
177 + err = clk_prepare_enable(sun4i_pwm->clk);
179 + dev_err(chip->dev, "failed to enable PWM clock\n");
183 + mutex_lock(&sun4i_pwm->ctrl_lock);
184 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
186 + if (sun4i_pwm->data->has_rdy && (val & PWM_RDY(pwm->hwpwm))) {
187 + mutex_unlock(&sun4i_pwm->ctrl_lock);
188 + clk_disable_unprepare(sun4i_pwm->clk);
192 + clk_gate = val & BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
194 + val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
195 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
198 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
199 + val &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
200 + val |= BIT_CH(prescaler, pwm->hwpwm);
201 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
203 + val = (dty & PWM_DTY_MASK) | PWM_PRD(prd);
204 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
207 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
209 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
212 + mutex_unlock(&sun4i_pwm->ctrl_lock);
213 + clk_disable_unprepare(sun4i_pwm->clk);
218 +static int sun4i_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
219 + enum pwm_polarity polarity)
221 + struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
225 + ret = clk_prepare_enable(sun4i_pwm->clk);
227 + dev_err(chip->dev, "failed to enable PWM clock\n");
231 + mutex_lock(&sun4i_pwm->ctrl_lock);
232 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
234 + if (polarity != PWM_POLARITY_NORMAL)
235 + val &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
237 + val |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
239 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
241 + mutex_unlock(&sun4i_pwm->ctrl_lock);
242 + clk_disable_unprepare(sun4i_pwm->clk);
247 +static int sun4i_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
249 + struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
253 + ret = clk_prepare_enable(sun4i_pwm->clk);
255 + dev_err(chip->dev, "failed to enable PWM clock\n");
259 + mutex_lock(&sun4i_pwm->ctrl_lock);
260 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
261 + val |= BIT_CH(PWM_EN, pwm->hwpwm);
262 + val |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
263 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
264 + mutex_unlock(&sun4i_pwm->ctrl_lock);
269 +static void sun4i_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
271 + struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
274 + mutex_lock(&sun4i_pwm->ctrl_lock);
275 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
276 + val &= ~BIT_CH(PWM_EN, pwm->hwpwm);
277 + val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
278 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG);
279 + mutex_unlock(&sun4i_pwm->ctrl_lock);
281 + clk_disable_unprepare(sun4i_pwm->clk);
284 +static const struct pwm_ops sun4i_pwm_ops = {
285 + .config = sun4i_pwm_config,
286 + .set_polarity = sun4i_pwm_set_polarity,
287 + .enable = sun4i_pwm_enable,
288 + .disable = sun4i_pwm_disable,
289 + .owner = THIS_MODULE,
292 +static const struct sun4i_pwm_data sun4i_pwm_data_a10 = {
293 + .has_prescaler_bypass = false,
297 +static const struct sun4i_pwm_data sun4i_pwm_data_a20 = {
298 + .has_prescaler_bypass = true,
302 +static const struct of_device_id sun4i_pwm_dt_ids[] = {
304 + .compatible = "allwinner,sun4i-a10-pwm",
305 + .data = &sun4i_pwm_data_a10,
307 + .compatible = "allwinner,sun7i-a20-pwm",
308 + .data = &sun4i_pwm_data_a20,
313 +MODULE_DEVICE_TABLE(of, sun4i_pwm_dt_ids);
315 +static int sun4i_pwm_probe(struct platform_device *pdev)
317 + struct sun4i_pwm_chip *pwm;
318 + struct resource *res;
321 + const struct of_device_id *match;
323 + match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);
325 + pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
329 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
330 + pwm->base = devm_ioremap_resource(&pdev->dev, res);
331 + if (IS_ERR(pwm->base))
332 + return PTR_ERR(pwm->base);
334 + pwm->clk = devm_clk_get(&pdev->dev, NULL);
335 + if (IS_ERR(pwm->clk))
336 + return PTR_ERR(pwm->clk);
338 + pwm->chip.dev = &pdev->dev;
339 + pwm->chip.ops = &sun4i_pwm_ops;
340 + pwm->chip.base = -1;
341 + pwm->chip.npwm = 2;
342 + pwm->chip.can_sleep = true;
343 + pwm->chip.of_xlate = of_pwm_xlate_with_flags;
344 + pwm->chip.of_pwm_n_cells = 3;
345 + pwm->data = match->data;
347 + mutex_init(&pwm->ctrl_lock);
349 + ret = pwmchip_add(&pwm->chip);
351 + dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
355 + platform_set_drvdata(pdev, pwm);
357 + ret = clk_prepare_enable(pwm->clk);
359 + dev_err(&pdev->dev, "failed to enable PWM clock\n");
363 + val = sun4i_pwm_readl(pwm, PWM_CTRL_REG);
364 + for (i = 0; i < pwm->chip.npwm; i++) {
365 + if (!(val & BIT_CH(PWM_ACT_STATE, i)))
366 + pwm->chip.pwms[i].polarity = PWM_POLARITY_INVERSED;
368 + clk_disable_unprepare(pwm->clk);
373 + pwmchip_remove(&pwm->chip);
376 + mutex_destroy(&pwm->ctrl_lock);
380 +static int sun4i_pwm_remove(struct platform_device *pdev)
382 + struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
384 + mutex_destroy(&pwm->ctrl_lock);
386 + return pwmchip_remove(&pwm->chip);
389 +static struct platform_driver sun4i_pwm_driver = {
391 + .name = "sun4i-pwm",
392 + .of_match_table = sun4i_pwm_dt_ids,
394 + .probe = sun4i_pwm_probe,
395 + .remove = sun4i_pwm_remove,
397 +module_platform_driver(sun4i_pwm_driver);
399 +MODULE_ALIAS("platform:sun4i-pwm");
400 +MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
401 +MODULE_DESCRIPTION("Allwinner sun4i PWM driver");
402 +MODULE_LICENSE("GPL v2");