1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Atmel Pulse Width Modulation Controller
5 * Copyright (C) 2013 Atmel Corporation
8 * Links to reference manuals for the supported PWM chips can be found in
9 * Documentation/arm/microchip.rst.
12 * - Periods start with the inactive level.
13 * - Hardware has to be stopped in general to update settings.
15 * Software bugs/possible improvements:
16 * - When atmel_pwm_apply() is called with state->enabled=false a change in
17 * state->polarity isn't honored.
18 * - Instead of sleeping to wait for a completed period, the interrupt
19 * functionality could be used.
22 #include <linux/clk.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
29 #include <linux/of_device.h>
30 #include <linux/platform_device.h>
31 #include <linux/pwm.h>
32 #include <linux/slab.h>
34 /* The following is global registers for PWM controller */
40 #define PWM_SR_ALL_CH_ON 0x0F
42 /* The following register is PWM channel related registers */
43 #define PWM_CH_REG_OFFSET 0x200
44 #define PWM_CH_REG_SIZE 0x20
47 /* Bit field in CMR */
48 #define PWM_CMR_CPOL (1 << 9)
49 #define PWM_CMR_UPD_CDTY (1 << 10)
50 #define PWM_CMR_CPRE_MSK 0xF
52 /* The following registers for PWM v1 */
53 #define PWMV1_CDTY 0x04
54 #define PWMV1_CPRD 0x08
55 #define PWMV1_CUPD 0x10
57 /* The following registers for PWM v2 */
58 #define PWMV2_CDTY 0x04
59 #define PWMV2_CDTYUPD 0x08
60 #define PWMV2_CPRD 0x0C
61 #define PWMV2_CPRDUPD 0x10
63 #define PWM_MAX_PRES 10
65 struct atmel_pwm_registers {
72 struct atmel_pwm_config {
76 struct atmel_pwm_data {
77 struct atmel_pwm_registers regs;
78 struct atmel_pwm_config cfg;
81 struct atmel_pwm_chip {
85 const struct atmel_pwm_data *data;
87 unsigned int updated_pwms;
88 /* ISR is cleared when read, ensure only one thread does that */
89 struct mutex isr_lock;
92 static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
94 return container_of(chip, struct atmel_pwm_chip, chip);
97 static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
100 return readl_relaxed(chip->base + offset);
103 static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
104 unsigned long offset, unsigned long val)
106 writel_relaxed(val, chip->base + offset);
109 static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
110 unsigned int ch, unsigned long offset)
112 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
114 return atmel_pwm_readl(chip, base + offset);
117 static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
118 unsigned int ch, unsigned long offset,
121 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
123 atmel_pwm_writel(chip, base + offset, val);
126 static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
127 unsigned long clkrate,
128 const struct pwm_state *state,
129 unsigned long *cprd, u32 *pres)
131 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
132 unsigned long long cycles = state->period;
135 /* Calculate the period cycles and prescale value */
137 do_div(cycles, NSEC_PER_SEC);
140 * The register for the period length is cfg.period_bits bits wide.
141 * So for each bit the number of clock cycles is wider divide the input
142 * clock frequency by two using pres and shift cprd accordingly.
144 shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
146 if (shift > PWM_MAX_PRES) {
147 dev_err(chip->dev, "pres exceeds the maximum value\n");
149 } else if (shift > 0) {
161 static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
162 unsigned long clkrate, unsigned long cprd,
163 u32 pres, unsigned long *cdty)
165 unsigned long long cycles = state->duty_cycle;
168 do_div(cycles, NSEC_PER_SEC);
170 *cdty = cprd - cycles;
173 static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
176 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
179 if (atmel_pwm->data->regs.duty_upd ==
180 atmel_pwm->data->regs.period_upd) {
181 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
182 val &= ~PWM_CMR_UPD_CDTY;
183 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
186 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
187 atmel_pwm->data->regs.duty_upd, cdty);
190 static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
191 struct pwm_device *pwm,
192 unsigned long cprd, unsigned long cdty)
194 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
196 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
197 atmel_pwm->data->regs.duty, cdty);
198 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
199 atmel_pwm->data->regs.period, cprd);
202 static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
205 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
206 unsigned long timeout = jiffies + 2 * HZ;
209 * Wait for at least a complete period to have passed before disabling a
210 * channel to be sure that CDTY has been updated
212 mutex_lock(&atmel_pwm->isr_lock);
213 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
215 while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) &&
216 time_before(jiffies, timeout)) {
217 usleep_range(10, 100);
218 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
221 mutex_unlock(&atmel_pwm->isr_lock);
222 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
225 * Wait for the PWM channel disable operation to be effective before
226 * stopping the clock.
228 timeout = jiffies + 2 * HZ;
230 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
231 time_before(jiffies, timeout))
232 usleep_range(10, 100);
235 clk_disable(atmel_pwm->clk);
238 static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
239 const struct pwm_state *state)
241 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
242 struct pwm_state cstate;
243 unsigned long cprd, cdty;
247 pwm_get_state(pwm, &cstate);
249 if (state->enabled) {
250 unsigned long clkrate = clk_get_rate(atmel_pwm->clk);
252 if (cstate.enabled &&
253 cstate.polarity == state->polarity &&
254 cstate.period == state->period) {
255 u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
257 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
258 atmel_pwm->data->regs.period);
259 pres = cmr & PWM_CMR_CPRE_MSK;
261 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
262 atmel_pwm_update_cdty(chip, pwm, cdty);
266 ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd,
270 "failed to calculate cprd and prescaler\n");
274 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
276 if (cstate.enabled) {
277 atmel_pwm_disable(chip, pwm, false);
279 ret = clk_enable(atmel_pwm->clk);
281 dev_err(chip->dev, "failed to enable clock\n");
286 /* It is necessary to preserve CPOL, inside CMR */
287 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
288 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
289 if (state->polarity == PWM_POLARITY_NORMAL)
290 val &= ~PWM_CMR_CPOL;
293 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
294 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
295 mutex_lock(&atmel_pwm->isr_lock);
296 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
297 atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm);
298 mutex_unlock(&atmel_pwm->isr_lock);
299 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
300 } else if (cstate.enabled) {
301 atmel_pwm_disable(chip, pwm, true);
307 static void atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
308 struct pwm_state *state)
310 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
313 sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
314 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
316 if (sr & (1 << pwm->hwpwm)) {
317 unsigned long rate = clk_get_rate(atmel_pwm->clk);
318 u32 cdty, cprd, pres;
321 pres = cmr & PWM_CMR_CPRE_MSK;
323 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
324 atmel_pwm->data->regs.period);
325 tmp = (u64)cprd * NSEC_PER_SEC;
327 state->period = DIV64_U64_ROUND_UP(tmp, rate);
329 cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
330 atmel_pwm->data->regs.duty);
331 tmp = (u64)(cprd - cdty) * NSEC_PER_SEC;
333 state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
335 state->enabled = true;
337 state->enabled = false;
340 if (cmr & PWM_CMR_CPOL)
341 state->polarity = PWM_POLARITY_INVERSED;
343 state->polarity = PWM_POLARITY_NORMAL;
346 static const struct pwm_ops atmel_pwm_ops = {
347 .apply = atmel_pwm_apply,
348 .get_state = atmel_pwm_get_state,
349 .owner = THIS_MODULE,
352 static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
354 .period = PWMV1_CPRD,
355 .period_upd = PWMV1_CUPD,
357 .duty_upd = PWMV1_CUPD,
360 /* 16 bits to keep period and duty. */
365 static const struct atmel_pwm_data atmel_sama5_pwm_data = {
367 .period = PWMV2_CPRD,
368 .period_upd = PWMV2_CPRDUPD,
370 .duty_upd = PWMV2_CDTYUPD,
373 /* 16 bits to keep period and duty. */
378 static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
380 .period = PWMV1_CPRD,
381 .period_upd = PWMV1_CUPD,
383 .duty_upd = PWMV1_CUPD,
386 /* 32 bits to keep period and duty. */
391 static const struct of_device_id atmel_pwm_dt_ids[] = {
393 .compatible = "atmel,at91sam9rl-pwm",
394 .data = &atmel_sam9rl_pwm_data,
396 .compatible = "atmel,sama5d3-pwm",
397 .data = &atmel_sama5_pwm_data,
399 .compatible = "atmel,sama5d2-pwm",
400 .data = &atmel_sama5_pwm_data,
402 .compatible = "microchip,sam9x60-pwm",
403 .data = &mchp_sam9x60_pwm_data,
408 MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
410 static int atmel_pwm_probe(struct platform_device *pdev)
412 struct atmel_pwm_chip *atmel_pwm;
415 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
419 mutex_init(&atmel_pwm->isr_lock);
420 atmel_pwm->data = of_device_get_match_data(&pdev->dev);
421 atmel_pwm->updated_pwms = 0;
423 atmel_pwm->base = devm_platform_ioremap_resource(pdev, 0);
424 if (IS_ERR(atmel_pwm->base))
425 return PTR_ERR(atmel_pwm->base);
427 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
428 if (IS_ERR(atmel_pwm->clk))
429 return PTR_ERR(atmel_pwm->clk);
431 ret = clk_prepare(atmel_pwm->clk);
433 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
437 atmel_pwm->chip.dev = &pdev->dev;
438 atmel_pwm->chip.ops = &atmel_pwm_ops;
439 atmel_pwm->chip.npwm = 4;
441 ret = pwmchip_add(&atmel_pwm->chip);
443 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
447 platform_set_drvdata(pdev, atmel_pwm);
452 clk_unprepare(atmel_pwm->clk);
456 static int atmel_pwm_remove(struct platform_device *pdev)
458 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
460 pwmchip_remove(&atmel_pwm->chip);
462 clk_unprepare(atmel_pwm->clk);
463 mutex_destroy(&atmel_pwm->isr_lock);
468 static struct platform_driver atmel_pwm_driver = {
471 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
473 .probe = atmel_pwm_probe,
474 .remove = atmel_pwm_remove,
476 module_platform_driver(atmel_pwm_driver);
478 MODULE_ALIAS("platform:atmel-pwm");
480 MODULE_DESCRIPTION("Atmel PWM driver");
481 MODULE_LICENSE("GPL v2");