1 --- a/drivers/pwm/pwm-sun4i.c
2 +++ b/drivers/pwm/pwm-sun4i.c
4 * Driver for Allwinner sun4i Pulse Width Modulation Controller
6 * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com>
9 + * - When outputing the source clock directly, the PWM logic will be bypassed
10 + * and the currently running period is not guaranteed to be completed
13 #include <linux/bitops.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/pwm.h>
18 +#include <linux/reset.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/time.h>
22 @@ -72,12 +77,15 @@ static const u32 prescaler_table[] = {
24 struct sun4i_pwm_data {
25 bool has_prescaler_bypass;
26 + bool has_direct_mod_clk_output;
30 struct sun4i_pwm_chip {
32 + struct clk *bus_clk;
34 + struct reset_control *rst;
37 const struct sun4i_pwm_data *data;
38 @@ -115,6 +123,20 @@ static void sun4i_pwm_get_state(struct p
40 val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
43 + * PWM chapter in H6 manual has a diagram which explains that if bypass
44 + * bit is set, no other setting has any meaning. Even more, experiment
45 + * proved that also enable bit is ignored in this case.
47 + if ((val & BIT_CH(PWM_BYPASS, pwm->hwpwm)) &&
48 + sun4i_pwm->data->has_direct_mod_clk_output) {
49 + state->period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, clk_rate);
50 + state->duty_cycle = DIV_ROUND_UP_ULL(state->period, 2);
51 + state->polarity = PWM_POLARITY_NORMAL;
52 + state->enabled = true;
56 if ((PWM_REG_PRESCAL(val, pwm->hwpwm) == PWM_PRESCAL_MASK) &&
57 sun4i_pwm->data->has_prescaler_bypass)
59 @@ -146,13 +168,24 @@ static void sun4i_pwm_get_state(struct p
61 static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
62 const struct pwm_state *state,
63 - u32 *dty, u32 *prd, unsigned int *prsclr)
64 + u32 *dty, u32 *prd, unsigned int *prsclr,
67 u64 clk_rate, div = 0;
68 unsigned int pval, prescaler = 0;
70 clk_rate = clk_get_rate(sun4i_pwm->clk);
72 + *bypass = sun4i_pwm->data->has_direct_mod_clk_output &&
74 + (state->period * clk_rate >= NSEC_PER_SEC) &&
75 + (state->period * clk_rate < 2 * NSEC_PER_SEC) &&
76 + (state->duty_cycle * clk_rate * 2 >= NSEC_PER_SEC);
78 + /* Skip calculation of other parameters if we bypass them */
82 if (sun4i_pwm->data->has_prescaler_bypass) {
83 /* First, test without any prescaler when available */
84 prescaler = PWM_PRESCAL_MASK;
85 @@ -200,10 +233,11 @@ static int sun4i_pwm_apply(struct pwm_ch
87 struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
88 struct pwm_state cstate;
90 + u32 ctrl, duty, period, val;
92 - unsigned int delay_us;
93 + unsigned int delay_us, prescaler;
97 pwm_get_state(pwm, &cstate);
99 @@ -218,43 +252,50 @@ static int sun4i_pwm_apply(struct pwm_ch
100 spin_lock(&sun4i_pwm->ctrl_lock);
101 ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
103 - if ((cstate.period != state->period) ||
104 - (cstate.duty_cycle != state->duty_cycle)) {
105 - u32 period, duty, val;
106 - unsigned int prescaler;
107 + ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
110 + dev_err(chip->dev, "period exceeds the maximum value\n");
111 + spin_unlock(&sun4i_pwm->ctrl_lock);
112 + if (!cstate.enabled)
113 + clk_disable_unprepare(sun4i_pwm->clk);
117 - ret = sun4i_pwm_calculate(sun4i_pwm, state,
118 - &duty, &period, &prescaler);
120 - dev_err(chip->dev, "period exceeds the maximum value\n");
121 + if (sun4i_pwm->data->has_direct_mod_clk_output) {
123 + ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);
124 + /* We can skip other parameter */
125 + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
126 spin_unlock(&sun4i_pwm->ctrl_lock);
127 - if (!cstate.enabled)
128 - clk_disable_unprepare(sun4i_pwm->clk);
132 + ctrl &= ~BIT_CH(PWM_BYPASS, pwm->hwpwm);
136 - if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
137 - /* Prescaler changed, the clock has to be gated */
138 - ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
139 - sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
141 - ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
142 - ctrl |= BIT_CH(prescaler, pwm->hwpwm);
144 + if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
145 + /* Prescaler changed, the clock has to be gated */
146 + ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
147 + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
149 - val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
150 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
151 - sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
152 - usecs_to_jiffies(cstate.period / 1000 + 1);
153 - sun4i_pwm->needs_delay[pwm->hwpwm] = true;
154 + ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
155 + ctrl |= BIT_CH(prescaler, pwm->hwpwm);
158 + val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
159 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
160 + sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
161 + usecs_to_jiffies(cstate.period / 1000 + 1);
162 + sun4i_pwm->needs_delay[pwm->hwpwm] = true;
164 if (state->polarity != PWM_POLARITY_NORMAL)
165 ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
167 ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
169 ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
171 if (state->enabled) {
172 ctrl |= BIT_CH(PWM_EN, pwm->hwpwm);
173 } else if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
174 @@ -320,6 +361,12 @@ static const struct sun4i_pwm_data sun4i
178 +static const struct sun4i_pwm_data sun50i_h6_pwm_data = {
179 + .has_prescaler_bypass = true,
180 + .has_direct_mod_clk_output = true,
184 static const struct of_device_id sun4i_pwm_dt_ids[] = {
186 .compatible = "allwinner,sun4i-a10-pwm",
187 @@ -337,6 +384,9 @@ static const struct of_device_id sun4i_p
188 .compatible = "allwinner,sun8i-h3-pwm",
189 .data = &sun4i_pwm_single_bypass,
191 + .compatible = "allwinner,sun50i-h6-pwm",
192 + .data = &sun50i_h6_pwm_data,
197 @@ -361,9 +411,69 @@ static int sun4i_pwm_probe(struct platfo
198 if (IS_ERR(pwm->base))
199 return PTR_ERR(pwm->base);
201 - pwm->clk = devm_clk_get(&pdev->dev, NULL);
202 - if (IS_ERR(pwm->clk))
204 + * All hardware variants need a source clock that is divided and
205 + * then feeds the counter that defines the output wave form. In the
206 + * device tree this clock is either unnamed or called "mod".
207 + * Some variants (e.g. H6) need another clock to access the
208 + * hardware registers; this is called "bus".
209 + * So we request "mod" first (and ignore the corner case that a
210 + * parent provides a "mod" clock while the right one would be the
211 + * unnamed one of the PWM device) and if this is not found we fall
212 + * back to the first clock of the PWM.
214 + pwm->clk = devm_clk_get_optional(&pdev->dev, "mod");
215 + if (IS_ERR(pwm->clk)) {
216 + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
217 + dev_err(&pdev->dev, "get mod clock failed %pe\n",
219 return PTR_ERR(pwm->clk);
223 + pwm->clk = devm_clk_get(&pdev->dev, NULL);
224 + if (IS_ERR(pwm->clk)) {
225 + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
226 + dev_err(&pdev->dev, "get unnamed clock failed %pe\n",
228 + return PTR_ERR(pwm->clk);
232 + pwm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus");
233 + if (IS_ERR(pwm->bus_clk)) {
234 + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
235 + dev_err(&pdev->dev, "get bus clock failed %pe\n",
237 + return PTR_ERR(pwm->bus_clk);
240 + pwm->rst = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
241 + if (IS_ERR(pwm->rst)) {
242 + if (PTR_ERR(pwm->rst) != -EPROBE_DEFER)
243 + dev_err(&pdev->dev, "get reset failed %pe\n",
245 + return PTR_ERR(pwm->rst);
248 + /* Deassert reset */
249 + ret = reset_control_deassert(pwm->rst);
251 + dev_err(&pdev->dev, "cannot deassert reset control: %pe\n",
257 + * We're keeping the bus clock on for the sake of simplicity.
258 + * Actually it only needs to be on for hardware register accesses.
260 + ret = clk_prepare_enable(pwm->bus_clk);
262 + dev_err(&pdev->dev, "cannot prepare and enable bus_clk %pe\n",
267 pwm->chip.dev = &pdev->dev;
268 pwm->chip.ops = &sun4i_pwm_ops;
269 @@ -377,19 +487,34 @@ static int sun4i_pwm_probe(struct platfo
270 ret = pwmchip_add(&pwm->chip);
272 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
277 platform_set_drvdata(pdev, pwm);
282 + clk_disable_unprepare(pwm->bus_clk);
284 + reset_control_assert(pwm->rst);
289 static int sun4i_pwm_remove(struct platform_device *pdev)
291 struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
294 + ret = pwmchip_remove(&pwm->chip);
298 - return pwmchip_remove(&pwm->chip);
299 + clk_disable_unprepare(pwm->bus_clk);
300 + reset_control_assert(pwm->rst);
305 static struct platform_driver sun4i_pwm_driver = {