1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Overkiz SAS 2012
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/clocksource.h>
11 #include <linux/clockchips.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/ioport.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <soc/at91/atmel_tcb.h>
29 #define ATMEL_TC_ACMR_MASK (ATMEL_TC_ACPA | ATMEL_TC_ACPC | \
30 ATMEL_TC_AEEVT | ATMEL_TC_ASWTRG)
32 #define ATMEL_TC_BCMR_MASK (ATMEL_TC_BCPB | ATMEL_TC_BCPC | \
33 ATMEL_TC_BEEVT | ATMEL_TC_BSWTRG)
35 struct atmel_tcb_pwm_device {
36 unsigned div; /* PWM clock divider */
37 unsigned duty; /* PWM duty expressed in clk cycles */
38 unsigned period; /* PWM period expressed in clk cycles */
41 struct atmel_tcb_channel {
49 struct atmel_tcb_pwm_chip {
54 struct regmap *regmap;
58 struct atmel_tcb_pwm_device pwms[NPWM];
59 struct atmel_tcb_channel bkup;
62 static const u8 atmel_tcb_divisors[] = { 2, 8, 32, 128, 0, };
64 static inline struct atmel_tcb_pwm_chip *to_tcb_chip(struct pwm_chip *chip)
66 return container_of(chip, struct atmel_tcb_pwm_chip, chip);
69 static int atmel_tcb_pwm_request(struct pwm_chip *chip,
70 struct pwm_device *pwm)
72 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
73 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
77 ret = clk_prepare_enable(tcbpwmc->clk);
85 spin_lock(&tcbpwmc->lock);
86 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
88 * Get init config from Timer Counter registers if
89 * Timer Counter is already configured as a PWM generator.
91 if (cmr & ATMEL_TC_WAVE) {
93 regmap_read(tcbpwmc->regmap,
94 ATMEL_TC_REG(tcbpwmc->channel, RA),
97 regmap_read(tcbpwmc->regmap,
98 ATMEL_TC_REG(tcbpwmc->channel, RB),
101 tcbpwm->div = cmr & ATMEL_TC_TCCLKS;
102 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC),
104 cmr &= (ATMEL_TC_TCCLKS | ATMEL_TC_ACMR_MASK |
109 cmr |= ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO | ATMEL_TC_EEVT_XC0;
110 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
111 spin_unlock(&tcbpwmc->lock);
116 static void atmel_tcb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
118 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
120 clk_disable_unprepare(tcbpwmc->clk);
123 static void atmel_tcb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
124 enum pwm_polarity polarity)
126 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
127 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
131 * If duty is 0 the timer will be stopped and we have to
132 * configure the output correctly on software trigger:
133 * - set output to high if PWM_POLARITY_INVERSED
134 * - set output to low if PWM_POLARITY_NORMAL
136 * This is why we're reverting polarity in this case.
138 if (tcbpwm->duty == 0)
139 polarity = !polarity;
141 spin_lock(&tcbpwmc->lock);
142 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
144 /* flush old setting and set the new one */
145 if (pwm->hwpwm == 0) {
146 cmr &= ~ATMEL_TC_ACMR_MASK;
147 if (polarity == PWM_POLARITY_INVERSED)
148 cmr |= ATMEL_TC_ASWTRG_CLEAR;
150 cmr |= ATMEL_TC_ASWTRG_SET;
152 cmr &= ~ATMEL_TC_BCMR_MASK;
153 if (polarity == PWM_POLARITY_INVERSED)
154 cmr |= ATMEL_TC_BSWTRG_CLEAR;
156 cmr |= ATMEL_TC_BSWTRG_SET;
159 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
162 * Use software trigger to apply the new setting.
163 * If both PWM devices in this group are disabled we stop the clock.
165 if (!(cmr & (ATMEL_TC_ACPC | ATMEL_TC_BCPC))) {
166 regmap_write(tcbpwmc->regmap,
167 ATMEL_TC_REG(tcbpwmc->channel, CCR),
168 ATMEL_TC_SWTRG | ATMEL_TC_CLKDIS);
169 tcbpwmc->bkup.enabled = 1;
171 regmap_write(tcbpwmc->regmap,
172 ATMEL_TC_REG(tcbpwmc->channel, CCR),
174 tcbpwmc->bkup.enabled = 0;
177 spin_unlock(&tcbpwmc->lock);
180 static int atmel_tcb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm,
181 enum pwm_polarity polarity)
183 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
184 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
188 * If duty is 0 the timer will be stopped and we have to
189 * configure the output correctly on software trigger:
190 * - set output to high if PWM_POLARITY_INVERSED
191 * - set output to low if PWM_POLARITY_NORMAL
193 * This is why we're reverting polarity in this case.
195 if (tcbpwm->duty == 0)
196 polarity = !polarity;
198 spin_lock(&tcbpwmc->lock);
199 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
201 /* flush old setting and set the new one */
202 cmr &= ~ATMEL_TC_TCCLKS;
204 if (pwm->hwpwm == 0) {
205 cmr &= ~ATMEL_TC_ACMR_MASK;
207 /* Set CMR flags according to given polarity */
208 if (polarity == PWM_POLARITY_INVERSED)
209 cmr |= ATMEL_TC_ASWTRG_CLEAR;
211 cmr |= ATMEL_TC_ASWTRG_SET;
213 cmr &= ~ATMEL_TC_BCMR_MASK;
214 if (polarity == PWM_POLARITY_INVERSED)
215 cmr |= ATMEL_TC_BSWTRG_CLEAR;
217 cmr |= ATMEL_TC_BSWTRG_SET;
221 * If duty is 0 or equal to period there's no need to register
222 * a specific action on RA/RB and RC compare.
223 * The output will be configured on software trigger and keep
224 * this config till next config call.
226 if (tcbpwm->duty != tcbpwm->period && tcbpwm->duty > 0) {
227 if (pwm->hwpwm == 0) {
228 if (polarity == PWM_POLARITY_INVERSED)
229 cmr |= ATMEL_TC_ACPA_SET | ATMEL_TC_ACPC_CLEAR;
231 cmr |= ATMEL_TC_ACPA_CLEAR | ATMEL_TC_ACPC_SET;
233 if (polarity == PWM_POLARITY_INVERSED)
234 cmr |= ATMEL_TC_BCPB_SET | ATMEL_TC_BCPC_CLEAR;
236 cmr |= ATMEL_TC_BCPB_CLEAR | ATMEL_TC_BCPC_SET;
240 cmr |= (tcbpwm->div & ATMEL_TC_TCCLKS);
242 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
245 regmap_write(tcbpwmc->regmap,
246 ATMEL_TC_REG(tcbpwmc->channel, RA),
249 regmap_write(tcbpwmc->regmap,
250 ATMEL_TC_REG(tcbpwmc->channel, RB),
253 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC),
256 /* Use software trigger to apply the new setting */
257 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CCR),
258 ATMEL_TC_SWTRG | ATMEL_TC_CLKEN);
259 tcbpwmc->bkup.enabled = 1;
260 spin_unlock(&tcbpwmc->lock);
264 static int atmel_tcb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
265 int duty_ns, int period_ns)
267 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
268 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
269 struct atmel_tcb_pwm_device *atcbpwm = NULL;
274 unsigned rate = clk_get_rate(tcbpwmc->clk);
275 unsigned long long min;
276 unsigned long long max;
279 * Find best clk divisor:
280 * the smallest divisor which can fulfill the period_ns requirements.
281 * If there is a gclk, the first divisor is actually the gclk selector
285 for (; i < ARRAY_SIZE(atmel_tcb_divisors); ++i) {
286 if (atmel_tcb_divisors[i] == 0) {
290 min = div_u64((u64)NSEC_PER_SEC * atmel_tcb_divisors[i], rate);
291 max = min << tcbpwmc->width;
292 if (max >= period_ns)
297 * If none of the divisor are small enough to represent period_ns
298 * take slow clock (32KHz).
300 if (i == ARRAY_SIZE(atmel_tcb_divisors)) {
302 rate = clk_get_rate(tcbpwmc->slow_clk);
303 min = div_u64(NSEC_PER_SEC, rate);
304 max = min << tcbpwmc->width;
306 /* If period is too big return ERANGE error */
311 duty = div_u64(duty_ns, min);
312 period = div_u64(period_ns, min);
315 atcbpwm = &tcbpwmc->pwms[1];
317 atcbpwm = &tcbpwmc->pwms[0];
320 * PWM devices provided by the TCB driver are grouped by 2.
321 * PWM devices in a given group must be configured with the
324 * We're checking the period value of the second PWM device
325 * in this group before applying the new config.
327 if ((atcbpwm && atcbpwm->duty > 0 &&
328 atcbpwm->duty != atcbpwm->period) &&
329 (atcbpwm->div != i || atcbpwm->period != period)) {
331 "failed to configure period_ns: PWM group already configured with a different value\n");
335 tcbpwm->period = period;
342 static int atmel_tcb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
343 const struct pwm_state *state)
345 int duty_cycle, period;
348 if (!state->enabled) {
349 atmel_tcb_pwm_disable(chip, pwm, state->polarity);
353 period = state->period < INT_MAX ? state->period : INT_MAX;
354 duty_cycle = state->duty_cycle < INT_MAX ? state->duty_cycle : INT_MAX;
356 ret = atmel_tcb_pwm_config(chip, pwm, duty_cycle, period);
360 return atmel_tcb_pwm_enable(chip, pwm, state->polarity);
363 static const struct pwm_ops atmel_tcb_pwm_ops = {
364 .request = atmel_tcb_pwm_request,
365 .free = atmel_tcb_pwm_free,
366 .apply = atmel_tcb_pwm_apply,
369 static struct atmel_tcb_config tcb_rm9200_config = {
373 static struct atmel_tcb_config tcb_sam9x5_config = {
377 static struct atmel_tcb_config tcb_sama5d2_config = {
382 static const struct of_device_id atmel_tcb_of_match[] = {
383 { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
384 { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
385 { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
389 static int atmel_tcb_pwm_probe(struct platform_device *pdev)
391 const struct of_device_id *match;
392 struct atmel_tcb_pwm_chip *tcbpwm;
393 const struct atmel_tcb_config *config;
394 struct device_node *np = pdev->dev.of_node;
395 char clk_name[] = "t0_clk";
399 tcbpwm = devm_kzalloc(&pdev->dev, sizeof(*tcbpwm), GFP_KERNEL);
403 err = of_property_read_u32(np, "reg", &channel);
406 "failed to get Timer Counter Block channel from device tree (error: %d)\n",
411 tcbpwm->regmap = syscon_node_to_regmap(np->parent);
412 if (IS_ERR(tcbpwm->regmap))
413 return PTR_ERR(tcbpwm->regmap);
415 tcbpwm->slow_clk = of_clk_get_by_name(np->parent, "slow_clk");
416 if (IS_ERR(tcbpwm->slow_clk))
417 return PTR_ERR(tcbpwm->slow_clk);
419 clk_name[1] += channel;
420 tcbpwm->clk = of_clk_get_by_name(np->parent, clk_name);
421 if (IS_ERR(tcbpwm->clk))
422 tcbpwm->clk = of_clk_get_by_name(np->parent, "t0_clk");
423 if (IS_ERR(tcbpwm->clk)) {
424 err = PTR_ERR(tcbpwm->clk);
428 match = of_match_node(atmel_tcb_of_match, np->parent);
429 config = match->data;
431 if (config->has_gclk) {
432 tcbpwm->gclk = of_clk_get_by_name(np->parent, "gclk");
433 if (IS_ERR(tcbpwm->gclk)) {
434 err = PTR_ERR(tcbpwm->gclk);
439 tcbpwm->chip.dev = &pdev->dev;
440 tcbpwm->chip.ops = &atmel_tcb_pwm_ops;
441 tcbpwm->chip.npwm = NPWM;
442 tcbpwm->channel = channel;
443 tcbpwm->width = config->counter_width;
445 err = clk_prepare_enable(tcbpwm->slow_clk);
449 spin_lock_init(&tcbpwm->lock);
451 err = pwmchip_add(&tcbpwm->chip);
453 goto err_disable_clk;
455 platform_set_drvdata(pdev, tcbpwm);
460 clk_disable_unprepare(tcbpwm->slow_clk);
463 clk_put(tcbpwm->gclk);
466 clk_put(tcbpwm->clk);
469 clk_put(tcbpwm->slow_clk);
474 static void atmel_tcb_pwm_remove(struct platform_device *pdev)
476 struct atmel_tcb_pwm_chip *tcbpwm = platform_get_drvdata(pdev);
478 pwmchip_remove(&tcbpwm->chip);
480 clk_disable_unprepare(tcbpwm->slow_clk);
481 clk_put(tcbpwm->gclk);
482 clk_put(tcbpwm->clk);
483 clk_put(tcbpwm->slow_clk);
486 static const struct of_device_id atmel_tcb_pwm_dt_ids[] = {
487 { .compatible = "atmel,tcb-pwm", },
490 MODULE_DEVICE_TABLE(of, atmel_tcb_pwm_dt_ids);
492 static int atmel_tcb_pwm_suspend(struct device *dev)
494 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev);
495 struct atmel_tcb_channel *chan = &tcbpwm->bkup;
496 unsigned int channel = tcbpwm->channel;
498 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, CMR), &chan->cmr);
499 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RA), &chan->ra);
500 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RB), &chan->rb);
501 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RC), &chan->rc);
506 static int atmel_tcb_pwm_resume(struct device *dev)
508 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev);
509 struct atmel_tcb_channel *chan = &tcbpwm->bkup;
510 unsigned int channel = tcbpwm->channel;
512 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, CMR), chan->cmr);
513 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RA), chan->ra);
514 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RB), chan->rb);
515 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RC), chan->rc);
518 regmap_write(tcbpwm->regmap,
519 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
520 ATMEL_TC_REG(channel, CCR));
525 static DEFINE_SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops, atmel_tcb_pwm_suspend,
526 atmel_tcb_pwm_resume);
528 static struct platform_driver atmel_tcb_pwm_driver = {
530 .name = "atmel-tcb-pwm",
531 .of_match_table = atmel_tcb_pwm_dt_ids,
532 .pm = pm_ptr(&atmel_tcb_pwm_pm_ops),
534 .probe = atmel_tcb_pwm_probe,
535 .remove_new = atmel_tcb_pwm_remove,
537 module_platform_driver(atmel_tcb_pwm_driver);
540 MODULE_DESCRIPTION("Atmel Timer Counter Pulse Width Modulation Driver");
541 MODULE_LICENSE("GPL v2");