2 * PWM device driver for ST SoCs
4 * Copyright (C) 2013-2016 STMicroelectronics (R&D) Limited
6 * Author: Ajit Pal Singh <ajitpal.singh@st.com>
7 * Lee Jones <lee.jones@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/clk.h>
16 #include <linux/interrupt.h>
17 #include <linux/math64.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/pwm.h>
23 #include <linux/regmap.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/wait.h>
29 #define PWM_OUT_VAL(x) (0x00 + (4 * (x))) /* Device's Duty Cycle register */
30 #define PWM_CPT_VAL(x) (0x10 + (4 * (x))) /* Capture value */
31 #define PWM_CPT_EDGE(x) (0x30 + (4 * (x))) /* Edge to capture on */
33 #define STI_PWM_CTRL 0x50 /* Control/Config register */
34 #define STI_INT_EN 0x54 /* Interrupt Enable/Disable register */
35 #define STI_INT_STA 0x58 /* Interrupt Status register */
36 #define PWM_INT_ACK 0x5c
37 #define PWM_PRESCALE_LOW_MASK 0x0f
38 #define PWM_PRESCALE_HIGH_MASK 0xf0
39 #define PWM_CPT_EDGE_MASK 0x03
40 #define PWM_INT_ACK_MASK 0x1ff
42 #define STI_MAX_CPT_DEVS 4
43 #define CPT_DC_MAX 0xff
63 * Each capture input can be programmed to detect rising-edge, falling-edge,
64 * either edge or neither egde.
73 struct sti_cpt_ddata {
77 wait_queue_head_t wait;
80 struct sti_pwm_compat_data {
81 const struct reg_field *reg_fields;
82 unsigned int pwm_num_devs;
83 unsigned int cpt_num_devs;
84 unsigned int max_pwm_cnt;
85 unsigned int max_prescale;
86 struct sti_cpt_ddata *ddata;
93 struct regmap *regmap;
94 struct sti_pwm_compat_data *cdata;
95 struct regmap_field *prescale_low;
96 struct regmap_field *prescale_high;
97 struct regmap_field *pwm_out_en;
98 struct regmap_field *pwm_cpt_en;
99 struct regmap_field *pwm_cpt_int_en;
100 struct regmap_field *pwm_cpt_int_stat;
101 struct pwm_chip chip;
102 struct pwm_device *cur;
103 unsigned long configured;
104 unsigned int en_count;
105 struct mutex sti_pwm_lock; /* To sync between enable/disable calls */
109 static const struct reg_field sti_pwm_regfields[MAX_REGFIELDS] = {
110 [PWMCLK_PRESCALE_LOW] = REG_FIELD(STI_PWM_CTRL, 0, 3),
111 [PWMCLK_PRESCALE_HIGH] = REG_FIELD(STI_PWM_CTRL, 11, 14),
112 [CPTCLK_PRESCALE] = REG_FIELD(STI_PWM_CTRL, 4, 8),
113 [PWM_OUT_EN] = REG_FIELD(STI_PWM_CTRL, 9, 9),
114 [PWM_CPT_EN] = REG_FIELD(STI_PWM_CTRL, 10, 10),
115 [PWM_CPT_INT_EN] = REG_FIELD(STI_INT_EN, 1, 4),
116 [PWM_CPT_INT_STAT] = REG_FIELD(STI_INT_STA, 1, 4),
119 static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip)
121 return container_of(chip, struct sti_pwm_chip, chip);
125 * Calculate the prescaler value corresponding to the period.
127 static int sti_pwm_get_prescale(struct sti_pwm_chip *pc, unsigned long period,
128 unsigned int *prescale)
130 struct sti_pwm_compat_data *cdata = pc->cdata;
131 unsigned long clk_rate;
135 clk_rate = clk_get_rate(pc->pwm_clk);
137 dev_err(pc->dev, "failed to get clock rate\n");
142 * prescale = ((period_ns * clk_rate) / (10^9 * (max_pwm_cnt + 1)) - 1
144 value = NSEC_PER_SEC / clk_rate;
145 value *= cdata->max_pwm_cnt + 1;
150 ps = period / value - 1;
151 if (ps > cdata->max_prescale)
160 * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles. The
161 * only way to change the period (apart from changing the PWM input clock) is
162 * to change the PWM clock prescaler.
164 * The prescaler is of 8 bits, so 256 prescaler values and hence 256 possible
165 * period values are supported (for a particular clock rate). The requested
166 * period will be applied only if it matches one of these 256 values.
168 static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
169 int duty_ns, int period_ns)
171 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
172 struct sti_pwm_compat_data *cdata = pc->cdata;
173 unsigned int ncfg, value, prescale = 0;
174 struct pwm_device *cur = pc->cur;
175 struct device *dev = pc->dev;
176 bool period_same = false;
179 ncfg = hweight_long(pc->configured);
181 period_same = (period_ns == pwm_get_period(cur));
184 * Allow configuration changes if one of the following conditions
186 * 1. No devices have been configured.
187 * 2. Only one device has been configured and the new request is for
189 * 3. Only one device has been configured and the new request is for
190 * a new device and period of the new device is same as the current
192 * 4. More than one devices are configured and period of the new
193 * requestis the same as the current period.
196 ((ncfg == 1) && (pwm->hwpwm == cur->hwpwm)) ||
197 ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) ||
198 ((ncfg > 1) && period_same)) {
199 /* Enable clock before writing to PWM registers. */
200 ret = clk_enable(pc->pwm_clk);
204 ret = clk_enable(pc->cpt_clk);
209 ret = sti_pwm_get_prescale(pc, period_ns, &prescale);
213 value = prescale & PWM_PRESCALE_LOW_MASK;
215 ret = regmap_field_write(pc->prescale_low, value);
219 value = (prescale & PWM_PRESCALE_HIGH_MASK) >> 4;
221 ret = regmap_field_write(pc->prescale_high, value);
227 * When PWMVal == 0, PWM pulse = 1 local clock cycle.
228 * When PWMVal == max_pwm_count,
229 * PWM pulse = (max_pwm_count + 1) local cycles,
230 * that is continuous pulse: signal never goes low.
232 value = cdata->max_pwm_cnt * duty_ns / period_ns;
234 ret = regmap_write(pc->regmap, PWM_OUT_VAL(pwm->hwpwm), value);
238 ret = regmap_field_write(pc->pwm_cpt_int_en, 0);
240 set_bit(pwm->hwpwm, &pc->configured);
243 dev_dbg(dev, "prescale:%u, period:%i, duty:%i, value:%u\n",
244 prescale, period_ns, duty_ns, value);
250 clk_disable(pc->pwm_clk);
251 clk_disable(pc->cpt_clk);
255 static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
257 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
258 struct device *dev = pc->dev;
262 * Since we have a common enable for all PWM devices, do not enable if
265 mutex_lock(&pc->sti_pwm_lock);
268 ret = clk_enable(pc->pwm_clk);
272 ret = clk_enable(pc->cpt_clk);
276 ret = regmap_field_write(pc->pwm_out_en, 1);
278 dev_err(dev, "failed to enable PWM device %u: %d\n",
287 mutex_unlock(&pc->sti_pwm_lock);
291 static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
293 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
295 mutex_lock(&pc->sti_pwm_lock);
297 if (--pc->en_count) {
298 mutex_unlock(&pc->sti_pwm_lock);
302 regmap_field_write(pc->pwm_out_en, 0);
304 clk_disable(pc->pwm_clk);
305 clk_disable(pc->cpt_clk);
307 mutex_unlock(&pc->sti_pwm_lock);
310 static void sti_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
312 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
314 clear_bit(pwm->hwpwm, &pc->configured);
317 static int sti_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm,
318 struct pwm_capture *result, unsigned long timeout)
320 struct sti_pwm_chip *pc = to_sti_pwmchip(chip);
321 struct sti_pwm_compat_data *cdata = pc->cdata;
322 struct sti_cpt_ddata *ddata = &cdata->ddata[pwm->hwpwm];
323 struct device *dev = pc->dev;
324 unsigned int effective_ticks;
325 unsigned long long high, low;
328 if (pwm->hwpwm >= cdata->cpt_num_devs) {
329 dev_err(dev, "device %u is not valid\n", pwm->hwpwm);
333 mutex_lock(&ddata->lock);
336 /* Prepare capture measurement */
337 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_RISING);
338 regmap_field_write(pc->pwm_cpt_int_en, BIT(pwm->hwpwm));
341 ret = regmap_field_write(pc->pwm_cpt_en, 1);
343 dev_err(dev, "failed to enable PWM capture %u: %d\n",
348 ret = wait_event_interruptible_timeout(ddata->wait, ddata->index > 1,
349 msecs_to_jiffies(timeout));
351 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_DISABLED);
353 if (ret == -ERESTARTSYS)
356 switch (ddata->index) {
360 * Getting here could mean:
361 * - input signal is constant of less than 1 Hz
362 * - there is no input signal at all
364 * In such case the frequency is rounded down to 0
367 result->duty_cycle = 0;
372 /* We have everying we need */
373 high = ddata->snapshot[1] - ddata->snapshot[0];
374 low = ddata->snapshot[2] - ddata->snapshot[1];
376 effective_ticks = clk_get_rate(pc->cpt_clk);
378 result->period = (high + low) * NSEC_PER_SEC;
379 result->period /= effective_ticks;
381 result->duty_cycle = high * NSEC_PER_SEC;
382 result->duty_cycle /= effective_ticks;
387 dev_err(dev, "internal error\n");
392 /* Disable capture */
393 regmap_field_write(pc->pwm_cpt_en, 0);
395 mutex_unlock(&ddata->lock);
399 static const struct pwm_ops sti_pwm_ops = {
400 .capture = sti_pwm_capture,
401 .config = sti_pwm_config,
402 .enable = sti_pwm_enable,
403 .disable = sti_pwm_disable,
404 .free = sti_pwm_free,
405 .owner = THIS_MODULE,
408 static irqreturn_t sti_pwm_interrupt(int irq, void *data)
410 struct sti_pwm_chip *pc = data;
411 struct device *dev = pc->dev;
412 struct sti_cpt_ddata *ddata;
414 unsigned int cpt_int_stat;
418 ret = regmap_field_read(pc->pwm_cpt_int_stat, &cpt_int_stat);
422 while (cpt_int_stat) {
423 devicenum = ffs(cpt_int_stat) - 1;
425 ddata = &pc->cdata->ddata[devicenum];
431 * __| |_________________| |________
434 * Capture start by the first available rising edge. When a
435 * capture event occurs, capture value (CPT_VALx) is stored,
436 * index incremented, capture edge changed.
438 * After the capture, if the index > 1, we have collected the
439 * necessary data so we signal the thread waiting for it and
440 * disable the capture by setting capture edge to none
443 regmap_read(pc->regmap,
444 PWM_CPT_VAL(devicenum),
445 &ddata->snapshot[ddata->index]);
447 switch (ddata->index) {
450 regmap_read(pc->regmap, PWM_CPT_EDGE(devicenum), ®);
451 reg ^= PWM_CPT_EDGE_MASK;
452 regmap_write(pc->regmap, PWM_CPT_EDGE(devicenum), reg);
458 regmap_write(pc->regmap,
459 PWM_CPT_EDGE(devicenum),
461 wake_up(&ddata->wait);
465 dev_err(dev, "Internal error\n");
468 cpt_int_stat &= ~BIT_MASK(devicenum);
473 /* Just ACK everything */
474 regmap_write(pc->regmap, PWM_INT_ACK, PWM_INT_ACK_MASK);
479 static int sti_pwm_probe_dt(struct sti_pwm_chip *pc)
481 struct device *dev = pc->dev;
482 const struct reg_field *reg_fields;
483 struct device_node *np = dev->of_node;
484 struct sti_pwm_compat_data *cdata = pc->cdata;
488 ret = of_property_read_u32(np, "st,pwm-num-chan", &num_devs);
490 cdata->pwm_num_devs = num_devs;
492 ret = of_property_read_u32(np, "st,capture-num-chan", &num_devs);
494 cdata->cpt_num_devs = num_devs;
496 if (!cdata->pwm_num_devs && !cdata->cpt_num_devs) {
497 dev_err(dev, "No channels configured\n");
501 reg_fields = cdata->reg_fields;
503 pc->prescale_low = devm_regmap_field_alloc(dev, pc->regmap,
504 reg_fields[PWMCLK_PRESCALE_LOW]);
505 if (IS_ERR(pc->prescale_low))
506 return PTR_ERR(pc->prescale_low);
508 pc->prescale_high = devm_regmap_field_alloc(dev, pc->regmap,
509 reg_fields[PWMCLK_PRESCALE_HIGH]);
510 if (IS_ERR(pc->prescale_high))
511 return PTR_ERR(pc->prescale_high);
514 pc->pwm_out_en = devm_regmap_field_alloc(dev, pc->regmap,
515 reg_fields[PWM_OUT_EN]);
516 if (IS_ERR(pc->pwm_out_en))
517 return PTR_ERR(pc->pwm_out_en);
519 pc->pwm_cpt_en = devm_regmap_field_alloc(dev, pc->regmap,
520 reg_fields[PWM_CPT_EN]);
521 if (IS_ERR(pc->pwm_cpt_en))
522 return PTR_ERR(pc->pwm_cpt_en);
524 pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap,
525 reg_fields[PWM_CPT_INT_EN]);
526 if (IS_ERR(pc->pwm_cpt_int_en))
527 return PTR_ERR(pc->pwm_cpt_int_en);
529 pc->pwm_cpt_int_stat = devm_regmap_field_alloc(dev, pc->regmap,
530 reg_fields[PWM_CPT_INT_STAT]);
531 if (PTR_ERR_OR_ZERO(pc->pwm_cpt_int_stat))
532 return PTR_ERR(pc->pwm_cpt_int_stat);
537 static const struct regmap_config sti_pwm_regmap_config = {
543 static int sti_pwm_probe(struct platform_device *pdev)
545 struct device *dev = &pdev->dev;
546 struct sti_pwm_compat_data *cdata;
547 struct sti_pwm_chip *pc;
548 struct resource *res;
552 pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
556 cdata = devm_kzalloc(dev, sizeof(*cdata), GFP_KERNEL);
560 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
562 pc->mmio = devm_ioremap_resource(dev, res);
563 if (IS_ERR(pc->mmio))
564 return PTR_ERR(pc->mmio);
566 pc->regmap = devm_regmap_init_mmio(dev, pc->mmio,
567 &sti_pwm_regmap_config);
568 if (IS_ERR(pc->regmap))
569 return PTR_ERR(pc->regmap);
571 irq = platform_get_irq(pdev, 0);
573 dev_err(&pdev->dev, "Failed to obtain IRQ\n");
577 ret = devm_request_irq(&pdev->dev, irq, sti_pwm_interrupt, 0,
580 dev_err(&pdev->dev, "Failed to request IRQ\n");
585 * Setup PWM data with default values: some values could be replaced
586 * with specific ones provided from Device Tree.
588 cdata->reg_fields = sti_pwm_regfields;
589 cdata->max_prescale = 0xff;
590 cdata->max_pwm_cnt = 255;
591 cdata->pwm_num_devs = 0;
592 cdata->cpt_num_devs = 0;
597 mutex_init(&pc->sti_pwm_lock);
599 ret = sti_pwm_probe_dt(pc);
603 if (cdata->pwm_num_devs) {
604 pc->pwm_clk = of_clk_get_by_name(dev->of_node, "pwm");
605 if (IS_ERR(pc->pwm_clk)) {
606 dev_err(dev, "failed to get PWM clock\n");
607 return PTR_ERR(pc->pwm_clk);
610 ret = clk_prepare(pc->pwm_clk);
612 dev_err(dev, "failed to prepare clock\n");
617 if (cdata->cpt_num_devs) {
618 pc->cpt_clk = of_clk_get_by_name(dev->of_node, "capture");
619 if (IS_ERR(pc->cpt_clk)) {
620 dev_err(dev, "failed to get PWM capture clock\n");
621 return PTR_ERR(pc->cpt_clk);
624 ret = clk_prepare(pc->cpt_clk);
626 dev_err(dev, "failed to prepare clock\n");
630 cdata->ddata = devm_kzalloc(dev, cdata->cpt_num_devs * sizeof(*cdata->ddata), GFP_KERNEL);
636 pc->chip.ops = &sti_pwm_ops;
638 pc->chip.npwm = pc->cdata->pwm_num_devs;
640 for (i = 0; i < cdata->cpt_num_devs; i++) {
641 struct sti_cpt_ddata *ddata = &cdata->ddata[i];
643 init_waitqueue_head(&ddata->wait);
644 mutex_init(&ddata->lock);
647 ret = pwmchip_add(&pc->chip);
649 clk_unprepare(pc->pwm_clk);
650 clk_unprepare(pc->cpt_clk);
654 platform_set_drvdata(pdev, pc);
659 static int sti_pwm_remove(struct platform_device *pdev)
661 struct sti_pwm_chip *pc = platform_get_drvdata(pdev);
664 for (i = 0; i < pc->cdata->pwm_num_devs; i++)
665 pwm_disable(&pc->chip.pwms[i]);
667 clk_unprepare(pc->pwm_clk);
668 clk_unprepare(pc->cpt_clk);
670 return pwmchip_remove(&pc->chip);
673 static const struct of_device_id sti_pwm_of_match[] = {
674 { .compatible = "st,sti-pwm", },
677 MODULE_DEVICE_TABLE(of, sti_pwm_of_match);
679 static struct platform_driver sti_pwm_driver = {
682 .of_match_table = sti_pwm_of_match,
684 .probe = sti_pwm_probe,
685 .remove = sti_pwm_remove,
687 module_platform_driver(sti_pwm_driver);
689 MODULE_AUTHOR("Ajit Pal Singh <ajitpal.singh@st.com>");
690 MODULE_DESCRIPTION("STMicroelectronics ST PWM driver");
691 MODULE_LICENSE("GPL");