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>
22 #include <linux/of_device.h>
23 #include <linux/of_irq.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <soc/at91/atmel_tcb.h>
30 #define ATMEL_TC_ACMR_MASK (ATMEL_TC_ACPA | ATMEL_TC_ACPC | \
31 ATMEL_TC_AEEVT | ATMEL_TC_ASWTRG)
33 #define ATMEL_TC_BCMR_MASK (ATMEL_TC_BCPB | ATMEL_TC_BCPC | \
34 ATMEL_TC_BEEVT | ATMEL_TC_BSWTRG)
36 struct atmel_tcb_pwm_device
{
37 enum pwm_polarity polarity
; /* PWM polarity */
38 unsigned div
; /* PWM clock divider */
39 unsigned duty
; /* PWM duty expressed in clk cycles */
40 unsigned period
; /* PWM period expressed in clk cycles */
43 struct atmel_tcb_channel
{
51 struct atmel_tcb_pwm_chip
{
56 struct regmap
*regmap
;
60 struct atmel_tcb_pwm_device
*pwms
[NPWM
];
61 struct atmel_tcb_channel bkup
;
64 const u8 atmel_tcb_divisors
[] = { 2, 8, 32, 128, 0, };
66 static inline struct atmel_tcb_pwm_chip
*to_tcb_chip(struct pwm_chip
*chip
)
68 return container_of(chip
, struct atmel_tcb_pwm_chip
, chip
);
71 static int atmel_tcb_pwm_set_polarity(struct pwm_chip
*chip
,
72 struct pwm_device
*pwm
,
73 enum pwm_polarity polarity
)
75 struct atmel_tcb_pwm_device
*tcbpwm
= pwm_get_chip_data(pwm
);
77 tcbpwm
->polarity
= polarity
;
82 static int atmel_tcb_pwm_request(struct pwm_chip
*chip
,
83 struct pwm_device
*pwm
)
85 struct atmel_tcb_pwm_chip
*tcbpwmc
= to_tcb_chip(chip
);
86 struct atmel_tcb_pwm_device
*tcbpwm
;
90 tcbpwm
= devm_kzalloc(chip
->dev
, sizeof(*tcbpwm
), GFP_KERNEL
);
94 ret
= clk_prepare_enable(tcbpwmc
->clk
);
96 devm_kfree(chip
->dev
, tcbpwm
);
100 pwm_set_chip_data(pwm
, tcbpwm
);
101 tcbpwm
->polarity
= PWM_POLARITY_NORMAL
;
106 spin_lock(&tcbpwmc
->lock
);
107 regmap_read(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CMR
), &cmr
);
109 * Get init config from Timer Counter registers if
110 * Timer Counter is already configured as a PWM generator.
112 if (cmr
& ATMEL_TC_WAVE
) {
114 regmap_read(tcbpwmc
->regmap
,
115 ATMEL_TC_REG(tcbpwmc
->channel
, RA
),
118 regmap_read(tcbpwmc
->regmap
,
119 ATMEL_TC_REG(tcbpwmc
->channel
, RB
),
122 tcbpwm
->div
= cmr
& ATMEL_TC_TCCLKS
;
123 regmap_read(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, RC
),
125 cmr
&= (ATMEL_TC_TCCLKS
| ATMEL_TC_ACMR_MASK
|
130 cmr
|= ATMEL_TC_WAVE
| ATMEL_TC_WAVESEL_UP_AUTO
| ATMEL_TC_EEVT_XC0
;
131 regmap_write(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CMR
), cmr
);
132 spin_unlock(&tcbpwmc
->lock
);
134 tcbpwmc
->pwms
[pwm
->hwpwm
] = tcbpwm
;
139 static void atmel_tcb_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
141 struct atmel_tcb_pwm_chip
*tcbpwmc
= to_tcb_chip(chip
);
142 struct atmel_tcb_pwm_device
*tcbpwm
= pwm_get_chip_data(pwm
);
144 clk_disable_unprepare(tcbpwmc
->clk
);
145 tcbpwmc
->pwms
[pwm
->hwpwm
] = NULL
;
146 devm_kfree(chip
->dev
, tcbpwm
);
149 static void atmel_tcb_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
151 struct atmel_tcb_pwm_chip
*tcbpwmc
= to_tcb_chip(chip
);
152 struct atmel_tcb_pwm_device
*tcbpwm
= pwm_get_chip_data(pwm
);
154 enum pwm_polarity polarity
= tcbpwm
->polarity
;
157 * If duty is 0 the timer will be stopped and we have to
158 * configure the output correctly on software trigger:
159 * - set output to high if PWM_POLARITY_INVERSED
160 * - set output to low if PWM_POLARITY_NORMAL
162 * This is why we're reverting polarity in this case.
164 if (tcbpwm
->duty
== 0)
165 polarity
= !polarity
;
167 spin_lock(&tcbpwmc
->lock
);
168 regmap_read(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CMR
), &cmr
);
170 /* flush old setting and set the new one */
171 if (pwm
->hwpwm
== 0) {
172 cmr
&= ~ATMEL_TC_ACMR_MASK
;
173 if (polarity
== PWM_POLARITY_INVERSED
)
174 cmr
|= ATMEL_TC_ASWTRG_CLEAR
;
176 cmr
|= ATMEL_TC_ASWTRG_SET
;
178 cmr
&= ~ATMEL_TC_BCMR_MASK
;
179 if (polarity
== PWM_POLARITY_INVERSED
)
180 cmr
|= ATMEL_TC_BSWTRG_CLEAR
;
182 cmr
|= ATMEL_TC_BSWTRG_SET
;
185 regmap_write(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CMR
), cmr
);
188 * Use software trigger to apply the new setting.
189 * If both PWM devices in this group are disabled we stop the clock.
191 if (!(cmr
& (ATMEL_TC_ACPC
| ATMEL_TC_BCPC
))) {
192 regmap_write(tcbpwmc
->regmap
,
193 ATMEL_TC_REG(tcbpwmc
->channel
, CCR
),
194 ATMEL_TC_SWTRG
| ATMEL_TC_CLKDIS
);
195 tcbpwmc
->bkup
.enabled
= 1;
197 regmap_write(tcbpwmc
->regmap
,
198 ATMEL_TC_REG(tcbpwmc
->channel
, CCR
),
200 tcbpwmc
->bkup
.enabled
= 0;
203 spin_unlock(&tcbpwmc
->lock
);
206 static int atmel_tcb_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
208 struct atmel_tcb_pwm_chip
*tcbpwmc
= to_tcb_chip(chip
);
209 struct atmel_tcb_pwm_device
*tcbpwm
= pwm_get_chip_data(pwm
);
211 enum pwm_polarity polarity
= tcbpwm
->polarity
;
214 * If duty is 0 the timer will be stopped and we have to
215 * configure the output correctly on software trigger:
216 * - set output to high if PWM_POLARITY_INVERSED
217 * - set output to low if PWM_POLARITY_NORMAL
219 * This is why we're reverting polarity in this case.
221 if (tcbpwm
->duty
== 0)
222 polarity
= !polarity
;
224 spin_lock(&tcbpwmc
->lock
);
225 regmap_read(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CMR
), &cmr
);
227 /* flush old setting and set the new one */
228 cmr
&= ~ATMEL_TC_TCCLKS
;
230 if (pwm
->hwpwm
== 0) {
231 cmr
&= ~ATMEL_TC_ACMR_MASK
;
233 /* Set CMR flags according to given polarity */
234 if (polarity
== PWM_POLARITY_INVERSED
)
235 cmr
|= ATMEL_TC_ASWTRG_CLEAR
;
237 cmr
|= ATMEL_TC_ASWTRG_SET
;
239 cmr
&= ~ATMEL_TC_BCMR_MASK
;
240 if (polarity
== PWM_POLARITY_INVERSED
)
241 cmr
|= ATMEL_TC_BSWTRG_CLEAR
;
243 cmr
|= ATMEL_TC_BSWTRG_SET
;
247 * If duty is 0 or equal to period there's no need to register
248 * a specific action on RA/RB and RC compare.
249 * The output will be configured on software trigger and keep
250 * this config till next config call.
252 if (tcbpwm
->duty
!= tcbpwm
->period
&& tcbpwm
->duty
> 0) {
253 if (pwm
->hwpwm
== 0) {
254 if (polarity
== PWM_POLARITY_INVERSED
)
255 cmr
|= ATMEL_TC_ACPA_SET
| ATMEL_TC_ACPC_CLEAR
;
257 cmr
|= ATMEL_TC_ACPA_CLEAR
| ATMEL_TC_ACPC_SET
;
259 if (polarity
== PWM_POLARITY_INVERSED
)
260 cmr
|= ATMEL_TC_BCPB_SET
| ATMEL_TC_BCPC_CLEAR
;
262 cmr
|= ATMEL_TC_BCPB_CLEAR
| ATMEL_TC_BCPC_SET
;
266 cmr
|= (tcbpwm
->div
& ATMEL_TC_TCCLKS
);
268 regmap_write(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CMR
), cmr
);
271 regmap_write(tcbpwmc
->regmap
,
272 ATMEL_TC_REG(tcbpwmc
->channel
, RA
),
275 regmap_write(tcbpwmc
->regmap
,
276 ATMEL_TC_REG(tcbpwmc
->channel
, RB
),
279 regmap_write(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, RC
),
282 /* Use software trigger to apply the new setting */
283 regmap_write(tcbpwmc
->regmap
, ATMEL_TC_REG(tcbpwmc
->channel
, CCR
),
284 ATMEL_TC_SWTRG
| ATMEL_TC_CLKEN
);
285 tcbpwmc
->bkup
.enabled
= 1;
286 spin_unlock(&tcbpwmc
->lock
);
290 static int atmel_tcb_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
291 int duty_ns
, int period_ns
)
293 struct atmel_tcb_pwm_chip
*tcbpwmc
= to_tcb_chip(chip
);
294 struct atmel_tcb_pwm_device
*tcbpwm
= pwm_get_chip_data(pwm
);
295 struct atmel_tcb_pwm_device
*atcbpwm
= NULL
;
300 unsigned rate
= clk_get_rate(tcbpwmc
->clk
);
301 unsigned long long min
;
302 unsigned long long max
;
305 * Find best clk divisor:
306 * the smallest divisor which can fulfill the period_ns requirements.
307 * If there is a gclk, the first divisor is actuallly the gclk selector
311 for (; i
< ARRAY_SIZE(atmel_tcb_divisors
); ++i
) {
312 if (atmel_tcb_divisors
[i
] == 0) {
316 min
= div_u64((u64
)NSEC_PER_SEC
* atmel_tcb_divisors
[i
], rate
);
317 max
= min
<< tcbpwmc
->width
;
318 if (max
>= period_ns
)
323 * If none of the divisor are small enough to represent period_ns
324 * take slow clock (32KHz).
326 if (i
== ARRAY_SIZE(atmel_tcb_divisors
)) {
328 rate
= clk_get_rate(tcbpwmc
->slow_clk
);
329 min
= div_u64(NSEC_PER_SEC
, rate
);
330 max
= min
<< tcbpwmc
->width
;
332 /* If period is too big return ERANGE error */
337 duty
= div_u64(duty_ns
, min
);
338 period
= div_u64(period_ns
, min
);
341 atcbpwm
= tcbpwmc
->pwms
[1];
343 atcbpwm
= tcbpwmc
->pwms
[0];
346 * PWM devices provided by the TCB driver are grouped by 2.
347 * PWM devices in a given group must be configured with the
350 * We're checking the period value of the second PWM device
351 * in this group before applying the new config.
353 if ((atcbpwm
&& atcbpwm
->duty
> 0 &&
354 atcbpwm
->duty
!= atcbpwm
->period
) &&
355 (atcbpwm
->div
!= i
|| atcbpwm
->period
!= period
)) {
357 "failed to configure period_ns: PWM group already configured with a different value\n");
361 tcbpwm
->period
= period
;
365 /* If the PWM is enabled, call enable to apply the new conf */
366 if (pwm_is_enabled(pwm
))
367 atmel_tcb_pwm_enable(chip
, pwm
);
372 static const struct pwm_ops atmel_tcb_pwm_ops
= {
373 .request
= atmel_tcb_pwm_request
,
374 .free
= atmel_tcb_pwm_free
,
375 .config
= atmel_tcb_pwm_config
,
376 .set_polarity
= atmel_tcb_pwm_set_polarity
,
377 .enable
= atmel_tcb_pwm_enable
,
378 .disable
= atmel_tcb_pwm_disable
,
379 .owner
= THIS_MODULE
,
382 static struct atmel_tcb_config tcb_rm9200_config
= {
386 static struct atmel_tcb_config tcb_sam9x5_config
= {
390 static struct atmel_tcb_config tcb_sama5d2_config
= {
395 static const struct of_device_id atmel_tcb_of_match
[] = {
396 { .compatible
= "atmel,at91rm9200-tcb", .data
= &tcb_rm9200_config
, },
397 { .compatible
= "atmel,at91sam9x5-tcb", .data
= &tcb_sam9x5_config
, },
398 { .compatible
= "atmel,sama5d2-tcb", .data
= &tcb_sama5d2_config
, },
402 static int atmel_tcb_pwm_probe(struct platform_device
*pdev
)
404 const struct of_device_id
*match
;
405 struct atmel_tcb_pwm_chip
*tcbpwm
;
406 const struct atmel_tcb_config
*config
;
407 struct device_node
*np
= pdev
->dev
.of_node
;
408 struct regmap
*regmap
;
409 struct clk
*clk
, *gclk
= NULL
;
410 struct clk
*slow_clk
;
411 char clk_name
[] = "t0_clk";
415 err
= of_property_read_u32(np
, "reg", &channel
);
418 "failed to get Timer Counter Block channel from device tree (error: %d)\n",
423 regmap
= syscon_node_to_regmap(np
->parent
);
425 return PTR_ERR(regmap
);
427 slow_clk
= of_clk_get_by_name(np
->parent
, "slow_clk");
428 if (IS_ERR(slow_clk
))
429 return PTR_ERR(slow_clk
);
431 clk_name
[1] += channel
;
432 clk
= of_clk_get_by_name(np
->parent
, clk_name
);
434 clk
= of_clk_get_by_name(np
->parent
, "t0_clk");
438 match
= of_match_node(atmel_tcb_of_match
, np
->parent
);
439 config
= match
->data
;
441 if (config
->has_gclk
) {
442 gclk
= of_clk_get_by_name(np
->parent
, "gclk");
444 return PTR_ERR(gclk
);
447 tcbpwm
= devm_kzalloc(&pdev
->dev
, sizeof(*tcbpwm
), GFP_KERNEL
);
448 if (tcbpwm
== NULL
) {
453 tcbpwm
->chip
.dev
= &pdev
->dev
;
454 tcbpwm
->chip
.ops
= &atmel_tcb_pwm_ops
;
455 tcbpwm
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
456 tcbpwm
->chip
.of_pwm_n_cells
= 3;
457 tcbpwm
->chip
.base
= -1;
458 tcbpwm
->chip
.npwm
= NPWM
;
459 tcbpwm
->channel
= channel
;
460 tcbpwm
->regmap
= regmap
;
463 tcbpwm
->slow_clk
= slow_clk
;
464 tcbpwm
->width
= config
->counter_width
;
466 err
= clk_prepare_enable(slow_clk
);
470 spin_lock_init(&tcbpwm
->lock
);
472 err
= pwmchip_add(&tcbpwm
->chip
);
474 goto err_disable_clk
;
476 platform_set_drvdata(pdev
, tcbpwm
);
481 clk_disable_unprepare(tcbpwm
->slow_clk
);
489 static int atmel_tcb_pwm_remove(struct platform_device
*pdev
)
491 struct atmel_tcb_pwm_chip
*tcbpwm
= platform_get_drvdata(pdev
);
494 clk_disable_unprepare(tcbpwm
->slow_clk
);
495 clk_put(tcbpwm
->slow_clk
);
496 clk_put(tcbpwm
->clk
);
498 err
= pwmchip_remove(&tcbpwm
->chip
);
505 static const struct of_device_id atmel_tcb_pwm_dt_ids
[] = {
506 { .compatible
= "atmel,tcb-pwm", },
509 MODULE_DEVICE_TABLE(of
, atmel_tcb_pwm_dt_ids
);
511 #ifdef CONFIG_PM_SLEEP
512 static int atmel_tcb_pwm_suspend(struct device
*dev
)
514 struct atmel_tcb_pwm_chip
*tcbpwm
= dev_get_drvdata(dev
);
515 struct atmel_tcb_channel
*chan
= &tcbpwm
->bkup
;
516 unsigned int channel
= tcbpwm
->channel
;
518 regmap_read(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, CMR
), &chan
->cmr
);
519 regmap_read(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, RA
), &chan
->ra
);
520 regmap_read(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, RB
), &chan
->rb
);
521 regmap_read(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, RC
), &chan
->rc
);
526 static int atmel_tcb_pwm_resume(struct device
*dev
)
528 struct atmel_tcb_pwm_chip
*tcbpwm
= dev_get_drvdata(dev
);
529 struct atmel_tcb_channel
*chan
= &tcbpwm
->bkup
;
530 unsigned int channel
= tcbpwm
->channel
;
532 regmap_write(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, CMR
), chan
->cmr
);
533 regmap_write(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, RA
), chan
->ra
);
534 regmap_write(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, RB
), chan
->rb
);
535 regmap_write(tcbpwm
->regmap
, ATMEL_TC_REG(channel
, RC
), chan
->rc
);
538 regmap_write(tcbpwm
->regmap
,
539 ATMEL_TC_CLKEN
| ATMEL_TC_SWTRG
,
540 ATMEL_TC_REG(channel
, CCR
));
546 static SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops
, atmel_tcb_pwm_suspend
,
547 atmel_tcb_pwm_resume
);
549 static struct platform_driver atmel_tcb_pwm_driver
= {
551 .name
= "atmel-tcb-pwm",
552 .of_match_table
= atmel_tcb_pwm_dt_ids
,
553 .pm
= &atmel_tcb_pwm_pm_ops
,
555 .probe
= atmel_tcb_pwm_probe
,
556 .remove
= atmel_tcb_pwm_remove
,
558 module_platform_driver(atmel_tcb_pwm_driver
);
560 MODULE_AUTHOR("Boris BREZILLON <b.brezillon@overkiz.com>");
561 MODULE_DESCRIPTION("Atmel Timer Counter Pulse Width Modulation Driver");
562 MODULE_LICENSE("GPL v2");