1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Overkiz SAS 2012
5 * Author: Boris BREZILLON <b.brezillon@overkiz.com>
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 enum pwm_polarity polarity; /* PWM polarity */
37 unsigned div; /* PWM clock divider */
38 unsigned duty; /* PWM duty expressed in clk cycles */
39 unsigned period; /* PWM period expressed in clk cycles */
42 struct atmel_tcb_channel {
50 struct atmel_tcb_pwm_chip {
55 struct regmap *regmap;
59 struct atmel_tcb_pwm_device pwms[NPWM];
60 struct atmel_tcb_channel bkup;
63 static const u8 atmel_tcb_divisors[] = { 2, 8, 32, 128, 0, };
65 static inline struct atmel_tcb_pwm_chip *to_tcb_chip(struct pwm_chip *chip)
67 return container_of(chip, struct atmel_tcb_pwm_chip, chip);
70 static int atmel_tcb_pwm_request(struct pwm_chip *chip,
71 struct pwm_device *pwm)
73 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
74 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
78 ret = clk_prepare_enable(tcbpwmc->clk);
82 tcbpwm->polarity = PWM_POLARITY_NORMAL;
87 spin_lock(&tcbpwmc->lock);
88 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
90 * Get init config from Timer Counter registers if
91 * Timer Counter is already configured as a PWM generator.
93 if (cmr & ATMEL_TC_WAVE) {
95 regmap_read(tcbpwmc->regmap,
96 ATMEL_TC_REG(tcbpwmc->channel, RA),
99 regmap_read(tcbpwmc->regmap,
100 ATMEL_TC_REG(tcbpwmc->channel, RB),
103 tcbpwm->div = cmr & ATMEL_TC_TCCLKS;
104 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC),
106 cmr &= (ATMEL_TC_TCCLKS | ATMEL_TC_ACMR_MASK |
111 cmr |= ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO | ATMEL_TC_EEVT_XC0;
112 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
113 spin_unlock(&tcbpwmc->lock);
118 static void atmel_tcb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
120 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
122 clk_disable_unprepare(tcbpwmc->clk);
125 static void atmel_tcb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
127 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
128 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
130 enum pwm_polarity polarity = tcbpwm->polarity;
133 * If duty is 0 the timer will be stopped and we have to
134 * configure the output correctly on software trigger:
135 * - set output to high if PWM_POLARITY_INVERSED
136 * - set output to low if PWM_POLARITY_NORMAL
138 * This is why we're reverting polarity in this case.
140 if (tcbpwm->duty == 0)
141 polarity = !polarity;
143 spin_lock(&tcbpwmc->lock);
144 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
146 /* flush old setting and set the new one */
147 if (pwm->hwpwm == 0) {
148 cmr &= ~ATMEL_TC_ACMR_MASK;
149 if (polarity == PWM_POLARITY_INVERSED)
150 cmr |= ATMEL_TC_ASWTRG_CLEAR;
152 cmr |= ATMEL_TC_ASWTRG_SET;
154 cmr &= ~ATMEL_TC_BCMR_MASK;
155 if (polarity == PWM_POLARITY_INVERSED)
156 cmr |= ATMEL_TC_BSWTRG_CLEAR;
158 cmr |= ATMEL_TC_BSWTRG_SET;
161 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
164 * Use software trigger to apply the new setting.
165 * If both PWM devices in this group are disabled we stop the clock.
167 if (!(cmr & (ATMEL_TC_ACPC | ATMEL_TC_BCPC))) {
168 regmap_write(tcbpwmc->regmap,
169 ATMEL_TC_REG(tcbpwmc->channel, CCR),
170 ATMEL_TC_SWTRG | ATMEL_TC_CLKDIS);
171 tcbpwmc->bkup.enabled = 1;
173 regmap_write(tcbpwmc->regmap,
174 ATMEL_TC_REG(tcbpwmc->channel, CCR),
176 tcbpwmc->bkup.enabled = 0;
179 spin_unlock(&tcbpwmc->lock);
182 static int atmel_tcb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
184 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
185 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
187 enum pwm_polarity polarity = tcbpwm->polarity;
190 * If duty is 0 the timer will be stopped and we have to
191 * configure the output correctly on software trigger:
192 * - set output to high if PWM_POLARITY_INVERSED
193 * - set output to low if PWM_POLARITY_NORMAL
195 * This is why we're reverting polarity in this case.
197 if (tcbpwm->duty == 0)
198 polarity = !polarity;
200 spin_lock(&tcbpwmc->lock);
201 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
203 /* flush old setting and set the new one */
204 cmr &= ~ATMEL_TC_TCCLKS;
206 if (pwm->hwpwm == 0) {
207 cmr &= ~ATMEL_TC_ACMR_MASK;
209 /* Set CMR flags according to given polarity */
210 if (polarity == PWM_POLARITY_INVERSED)
211 cmr |= ATMEL_TC_ASWTRG_CLEAR;
213 cmr |= ATMEL_TC_ASWTRG_SET;
215 cmr &= ~ATMEL_TC_BCMR_MASK;
216 if (polarity == PWM_POLARITY_INVERSED)
217 cmr |= ATMEL_TC_BSWTRG_CLEAR;
219 cmr |= ATMEL_TC_BSWTRG_SET;
223 * If duty is 0 or equal to period there's no need to register
224 * a specific action on RA/RB and RC compare.
225 * The output will be configured on software trigger and keep
226 * this config till next config call.
228 if (tcbpwm->duty != tcbpwm->period && tcbpwm->duty > 0) {
229 if (pwm->hwpwm == 0) {
230 if (polarity == PWM_POLARITY_INVERSED)
231 cmr |= ATMEL_TC_ACPA_SET | ATMEL_TC_ACPC_CLEAR;
233 cmr |= ATMEL_TC_ACPA_CLEAR | ATMEL_TC_ACPC_SET;
235 if (polarity == PWM_POLARITY_INVERSED)
236 cmr |= ATMEL_TC_BCPB_SET | ATMEL_TC_BCPC_CLEAR;
238 cmr |= ATMEL_TC_BCPB_CLEAR | ATMEL_TC_BCPC_SET;
242 cmr |= (tcbpwm->div & ATMEL_TC_TCCLKS);
244 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
247 regmap_write(tcbpwmc->regmap,
248 ATMEL_TC_REG(tcbpwmc->channel, RA),
251 regmap_write(tcbpwmc->regmap,
252 ATMEL_TC_REG(tcbpwmc->channel, RB),
255 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC),
258 /* Use software trigger to apply the new setting */
259 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CCR),
260 ATMEL_TC_SWTRG | ATMEL_TC_CLKEN);
261 tcbpwmc->bkup.enabled = 1;
262 spin_unlock(&tcbpwmc->lock);
266 static int atmel_tcb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
267 int duty_ns, int period_ns)
269 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
270 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
271 struct atmel_tcb_pwm_device *atcbpwm = NULL;
276 unsigned rate = clk_get_rate(tcbpwmc->clk);
277 unsigned long long min;
278 unsigned long long max;
281 * Find best clk divisor:
282 * the smallest divisor which can fulfill the period_ns requirements.
283 * If there is a gclk, the first divisor is actually the gclk selector
287 for (; i < ARRAY_SIZE(atmel_tcb_divisors); ++i) {
288 if (atmel_tcb_divisors[i] == 0) {
292 min = div_u64((u64)NSEC_PER_SEC * atmel_tcb_divisors[i], rate);
293 max = min << tcbpwmc->width;
294 if (max >= period_ns)
299 * If none of the divisor are small enough to represent period_ns
300 * take slow clock (32KHz).
302 if (i == ARRAY_SIZE(atmel_tcb_divisors)) {
304 rate = clk_get_rate(tcbpwmc->slow_clk);
305 min = div_u64(NSEC_PER_SEC, rate);
306 max = min << tcbpwmc->width;
308 /* If period is too big return ERANGE error */
313 duty = div_u64(duty_ns, min);
314 period = div_u64(period_ns, min);
317 atcbpwm = &tcbpwmc->pwms[1];
319 atcbpwm = &tcbpwmc->pwms[0];
322 * PWM devices provided by the TCB driver are grouped by 2.
323 * PWM devices in a given group must be configured with the
326 * We're checking the period value of the second PWM device
327 * in this group before applying the new config.
329 if ((atcbpwm && atcbpwm->duty > 0 &&
330 atcbpwm->duty != atcbpwm->period) &&
331 (atcbpwm->div != i || atcbpwm->period != period)) {
333 "failed to configure period_ns: PWM group already configured with a different value\n");
337 tcbpwm->period = period;
344 static int atmel_tcb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
345 const struct pwm_state *state)
347 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
348 struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm];
349 int duty_cycle, period;
352 tcbpwm->polarity = state->polarity;
354 if (!state->enabled) {
355 atmel_tcb_pwm_disable(chip, pwm);
359 period = state->period < INT_MAX ? state->period : INT_MAX;
360 duty_cycle = state->duty_cycle < INT_MAX ? state->duty_cycle : INT_MAX;
362 ret = atmel_tcb_pwm_config(chip, pwm, duty_cycle, period);
366 return atmel_tcb_pwm_enable(chip, pwm);
369 static const struct pwm_ops atmel_tcb_pwm_ops = {
370 .request = atmel_tcb_pwm_request,
371 .free = atmel_tcb_pwm_free,
372 .apply = atmel_tcb_pwm_apply,
373 .owner = THIS_MODULE,
376 static struct atmel_tcb_config tcb_rm9200_config = {
380 static struct atmel_tcb_config tcb_sam9x5_config = {
384 static struct atmel_tcb_config tcb_sama5d2_config = {
389 static const struct of_device_id atmel_tcb_of_match[] = {
390 { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
391 { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
392 { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
396 static int atmel_tcb_pwm_probe(struct platform_device *pdev)
398 const struct of_device_id *match;
399 struct atmel_tcb_pwm_chip *tcbpwm;
400 const struct atmel_tcb_config *config;
401 struct device_node *np = pdev->dev.of_node;
402 char clk_name[] = "t0_clk";
406 tcbpwm = devm_kzalloc(&pdev->dev, sizeof(*tcbpwm), GFP_KERNEL);
410 err = of_property_read_u32(np, "reg", &channel);
413 "failed to get Timer Counter Block channel from device tree (error: %d)\n",
418 tcbpwm->regmap = syscon_node_to_regmap(np->parent);
419 if (IS_ERR(tcbpwm->regmap))
420 return PTR_ERR(tcbpwm->regmap);
422 tcbpwm->slow_clk = of_clk_get_by_name(np->parent, "slow_clk");
423 if (IS_ERR(tcbpwm->slow_clk))
424 return PTR_ERR(tcbpwm->slow_clk);
426 clk_name[1] += channel;
427 tcbpwm->clk = of_clk_get_by_name(np->parent, clk_name);
428 if (IS_ERR(tcbpwm->clk))
429 tcbpwm->clk = of_clk_get_by_name(np->parent, "t0_clk");
430 if (IS_ERR(tcbpwm->clk)) {
431 err = PTR_ERR(tcbpwm->clk);
435 match = of_match_node(atmel_tcb_of_match, np->parent);
436 config = match->data;
438 if (config->has_gclk) {
439 tcbpwm->gclk = of_clk_get_by_name(np->parent, "gclk");
440 if (IS_ERR(tcbpwm->gclk)) {
441 err = PTR_ERR(tcbpwm->gclk);
446 tcbpwm->chip.dev = &pdev->dev;
447 tcbpwm->chip.ops = &atmel_tcb_pwm_ops;
448 tcbpwm->chip.npwm = NPWM;
449 tcbpwm->channel = channel;
450 tcbpwm->width = config->counter_width;
452 err = clk_prepare_enable(tcbpwm->slow_clk);
456 spin_lock_init(&tcbpwm->lock);
458 err = pwmchip_add(&tcbpwm->chip);
460 goto err_disable_clk;
462 platform_set_drvdata(pdev, tcbpwm);
467 clk_disable_unprepare(tcbpwm->slow_clk);
470 clk_put(tcbpwm->gclk);
473 clk_put(tcbpwm->clk);
476 clk_put(tcbpwm->slow_clk);
481 static void atmel_tcb_pwm_remove(struct platform_device *pdev)
483 struct atmel_tcb_pwm_chip *tcbpwm = platform_get_drvdata(pdev);
485 pwmchip_remove(&tcbpwm->chip);
487 clk_disable_unprepare(tcbpwm->slow_clk);
488 clk_put(tcbpwm->gclk);
489 clk_put(tcbpwm->clk);
490 clk_put(tcbpwm->slow_clk);
493 static const struct of_device_id atmel_tcb_pwm_dt_ids[] = {
494 { .compatible = "atmel,tcb-pwm", },
497 MODULE_DEVICE_TABLE(of, atmel_tcb_pwm_dt_ids);
499 #ifdef CONFIG_PM_SLEEP
500 static int atmel_tcb_pwm_suspend(struct device *dev)
502 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev);
503 struct atmel_tcb_channel *chan = &tcbpwm->bkup;
504 unsigned int channel = tcbpwm->channel;
506 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, CMR), &chan->cmr);
507 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RA), &chan->ra);
508 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RB), &chan->rb);
509 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RC), &chan->rc);
514 static int atmel_tcb_pwm_resume(struct device *dev)
516 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev);
517 struct atmel_tcb_channel *chan = &tcbpwm->bkup;
518 unsigned int channel = tcbpwm->channel;
520 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, CMR), chan->cmr);
521 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RA), chan->ra);
522 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RB), chan->rb);
523 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RC), chan->rc);
526 regmap_write(tcbpwm->regmap,
527 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
528 ATMEL_TC_REG(channel, CCR));
534 static SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops, atmel_tcb_pwm_suspend,
535 atmel_tcb_pwm_resume);
537 static struct platform_driver atmel_tcb_pwm_driver = {
539 .name = "atmel-tcb-pwm",
540 .of_match_table = atmel_tcb_pwm_dt_ids,
541 .pm = &atmel_tcb_pwm_pm_ops,
543 .probe = atmel_tcb_pwm_probe,
544 .remove_new = atmel_tcb_pwm_remove,
546 module_platform_driver(atmel_tcb_pwm_driver);
548 MODULE_AUTHOR("Boris BREZILLON <b.brezillon@overkiz.com>");
549 MODULE_DESCRIPTION("Atmel Timer Counter Pulse Width Modulation Driver");
550 MODULE_LICENSE("GPL v2");