Lines Matching +full:pwm +full:- +full:period

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PWM device driver for ST SoCs
5 * Copyright (C) 2013-2016 STMicroelectronics (R&D) Limited
18 #include <linux/pwm.h>
59 * Each capture input can be programmed to detect rising-edge, falling-edge,
121 * Calculate the prescaler value corresponding to the period.
123 static int sti_pwm_get_prescale(struct sti_pwm_chip *pc, unsigned long period, in sti_pwm_get_prescale() argument
126 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_get_prescale()
131 clk_rate = clk_get_rate(pc->pwm_clk); in sti_pwm_get_prescale()
133 dev_err(pc->dev, "failed to get clock rate\n"); in sti_pwm_get_prescale()
134 return -EINVAL; in sti_pwm_get_prescale()
138 * prescale = ((period_ns * clk_rate) / (10^9 * (max_pwm_cnt + 1)) - 1 in sti_pwm_get_prescale()
141 value *= cdata->max_pwm_cnt + 1; in sti_pwm_get_prescale()
143 if (period % value) in sti_pwm_get_prescale()
144 return -EINVAL; in sti_pwm_get_prescale()
146 ps = period / value - 1; in sti_pwm_get_prescale()
147 if (ps > cdata->max_prescale) in sti_pwm_get_prescale()
148 return -EINVAL; in sti_pwm_get_prescale()
156 * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles. The
157 * only way to change the period (apart from changing the PWM input clock) is
158 * to change the PWM clock prescaler.
161 * period values are supported (for a particular clock rate). The requested
162 * period will be applied only if it matches one of these 256 values.
164 static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, in sti_pwm_config() argument
168 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_config()
170 struct pwm_device *cur = pc->cur; in sti_pwm_config()
171 struct device *dev = pc->dev; in sti_pwm_config()
175 ncfg = hweight_long(pc->configured); in sti_pwm_config()
186 * a new device and period of the new device is same as the current in sti_pwm_config()
187 * configured period. in sti_pwm_config()
188 * 4. More than one devices are configured and period of the new in sti_pwm_config()
189 * requestis the same as the current period. in sti_pwm_config()
192 ((ncfg == 1) && (pwm->hwpwm == cur->hwpwm)) || in sti_pwm_config()
193 ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) || in sti_pwm_config()
195 /* Enable clock before writing to PWM registers. */ in sti_pwm_config()
196 ret = clk_enable(pc->pwm_clk); in sti_pwm_config()
200 ret = clk_enable(pc->cpt_clk); in sti_pwm_config()
211 ret = regmap_field_write(pc->prescale_low, value); in sti_pwm_config()
217 ret = regmap_field_write(pc->prescale_high, value); in sti_pwm_config()
223 * When PWMVal == 0, PWM pulse = 1 local clock cycle. in sti_pwm_config()
225 * PWM pulse = (max_pwm_count + 1) local cycles, in sti_pwm_config()
228 value = cdata->max_pwm_cnt * duty_ns / period_ns; in sti_pwm_config()
230 ret = regmap_write(pc->regmap, PWM_OUT_VAL(pwm->hwpwm), value); in sti_pwm_config()
234 ret = regmap_field_write(pc->pwm_cpt_int_en, 0); in sti_pwm_config()
236 set_bit(pwm->hwpwm, &pc->configured); in sti_pwm_config()
237 pc->cur = pwm; in sti_pwm_config()
239 dev_dbg(dev, "prescale:%u, period:%i, duty:%i, value:%u\n", in sti_pwm_config()
242 return -EINVAL; in sti_pwm_config()
246 clk_disable(pc->pwm_clk); in sti_pwm_config()
247 clk_disable(pc->cpt_clk); in sti_pwm_config()
251 static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) in sti_pwm_enable() argument
254 struct device *dev = pc->dev; in sti_pwm_enable()
258 * Since we have a common enable for all PWM devices, do not enable if in sti_pwm_enable()
261 mutex_lock(&pc->sti_pwm_lock); in sti_pwm_enable()
263 if (!pc->en_count) { in sti_pwm_enable()
264 ret = clk_enable(pc->pwm_clk); in sti_pwm_enable()
268 ret = clk_enable(pc->cpt_clk); in sti_pwm_enable()
272 ret = regmap_field_write(pc->pwm_out_en, 1); in sti_pwm_enable()
274 dev_err(dev, "failed to enable PWM device %u: %d\n", in sti_pwm_enable()
275 pwm->hwpwm, ret); in sti_pwm_enable()
280 pc->en_count++; in sti_pwm_enable()
283 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_enable()
287 static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) in sti_pwm_disable() argument
291 mutex_lock(&pc->sti_pwm_lock); in sti_pwm_disable()
293 if (--pc->en_count) { in sti_pwm_disable()
294 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_disable()
298 regmap_field_write(pc->pwm_out_en, 0); in sti_pwm_disable()
300 clk_disable(pc->pwm_clk); in sti_pwm_disable()
301 clk_disable(pc->cpt_clk); in sti_pwm_disable()
303 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_disable()
306 static void sti_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in sti_pwm_free() argument
310 clear_bit(pwm->hwpwm, &pc->configured); in sti_pwm_free()
313 static int sti_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, in sti_pwm_capture() argument
317 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_capture()
318 struct sti_cpt_ddata *ddata = &cdata->ddata[pwm->hwpwm]; in sti_pwm_capture()
319 struct device *dev = pc->dev; in sti_pwm_capture()
324 if (pwm->hwpwm >= cdata->cpt_num_devs) { in sti_pwm_capture()
325 dev_err(dev, "device %u is not valid\n", pwm->hwpwm); in sti_pwm_capture()
326 return -EINVAL; in sti_pwm_capture()
329 mutex_lock(&ddata->lock); in sti_pwm_capture()
330 ddata->index = 0; in sti_pwm_capture()
333 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_RISING); in sti_pwm_capture()
334 regmap_field_write(pc->pwm_cpt_int_en, BIT(pwm->hwpwm)); in sti_pwm_capture()
337 ret = regmap_field_write(pc->pwm_cpt_en, 1); in sti_pwm_capture()
339 dev_err(dev, "failed to enable PWM capture %u: %d\n", in sti_pwm_capture()
340 pwm->hwpwm, ret); in sti_pwm_capture()
344 ret = wait_event_interruptible_timeout(ddata->wait, ddata->index > 1, in sti_pwm_capture()
347 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_DISABLED); in sti_pwm_capture()
349 if (ret == -ERESTARTSYS) in sti_pwm_capture()
352 switch (ddata->index) { in sti_pwm_capture()
357 * - input signal is constant of less than 1 Hz in sti_pwm_capture()
358 * - there is no input signal at all in sti_pwm_capture()
362 result->period = 0; in sti_pwm_capture()
363 result->duty_cycle = 0; in sti_pwm_capture()
369 high = ddata->snapshot[1] - ddata->snapshot[0]; in sti_pwm_capture()
370 low = ddata->snapshot[2] - ddata->snapshot[1]; in sti_pwm_capture()
372 effective_ticks = clk_get_rate(pc->cpt_clk); in sti_pwm_capture()
374 result->period = (high + low) * NSEC_PER_SEC; in sti_pwm_capture()
375 result->period /= effective_ticks; in sti_pwm_capture()
377 result->duty_cycle = high * NSEC_PER_SEC; in sti_pwm_capture()
378 result->duty_cycle /= effective_ticks; in sti_pwm_capture()
389 regmap_field_write(pc->pwm_cpt_en, 0); in sti_pwm_capture()
391 mutex_unlock(&ddata->lock); in sti_pwm_capture()
395 static int sti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in sti_pwm_apply() argument
400 if (state->polarity != PWM_POLARITY_NORMAL) in sti_pwm_apply()
401 return -EINVAL; in sti_pwm_apply()
403 if (!state->enabled) { in sti_pwm_apply()
404 if (pwm->state.enabled) in sti_pwm_apply()
405 sti_pwm_disable(chip, pwm); in sti_pwm_apply()
410 err = sti_pwm_config(chip, pwm, state->duty_cycle, state->period); in sti_pwm_apply()
414 if (!pwm->state.enabled) in sti_pwm_apply()
415 err = sti_pwm_enable(chip, pwm); in sti_pwm_apply()
429 struct device *dev = pc->dev; in sti_pwm_interrupt()
436 ret = regmap_field_read(pc->pwm_cpt_int_stat, &cpt_int_stat); in sti_pwm_interrupt()
441 devicenum = ffs(cpt_int_stat) - 1; in sti_pwm_interrupt()
443 ddata = &pc->cdata->ddata[devicenum]; in sti_pwm_interrupt()
461 regmap_read(pc->regmap, in sti_pwm_interrupt()
463 &ddata->snapshot[ddata->index]); in sti_pwm_interrupt()
465 switch (ddata->index) { in sti_pwm_interrupt()
468 regmap_read(pc->regmap, PWM_CPT_EDGE(devicenum), &reg); in sti_pwm_interrupt()
470 regmap_write(pc->regmap, PWM_CPT_EDGE(devicenum), reg); in sti_pwm_interrupt()
472 ddata->index++; in sti_pwm_interrupt()
476 regmap_write(pc->regmap, in sti_pwm_interrupt()
479 wake_up(&ddata->wait); in sti_pwm_interrupt()
492 regmap_write(pc->regmap, PWM_INT_ACK, PWM_INT_ACK_MASK); in sti_pwm_interrupt()
499 struct device *dev = pc->dev; in sti_pwm_probe_dt()
501 struct device_node *np = dev->of_node; in sti_pwm_probe_dt()
502 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_probe_dt()
506 ret = of_property_read_u32(np, "st,pwm-num-chan", &num_devs); in sti_pwm_probe_dt()
508 cdata->pwm_num_devs = num_devs; in sti_pwm_probe_dt()
510 ret = of_property_read_u32(np, "st,capture-num-chan", &num_devs); in sti_pwm_probe_dt()
512 cdata->cpt_num_devs = num_devs; in sti_pwm_probe_dt()
514 if (!cdata->pwm_num_devs && !cdata->cpt_num_devs) { in sti_pwm_probe_dt()
516 return -EINVAL; in sti_pwm_probe_dt()
519 reg_fields = cdata->reg_fields; in sti_pwm_probe_dt()
521 pc->prescale_low = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
523 if (IS_ERR(pc->prescale_low)) in sti_pwm_probe_dt()
524 return PTR_ERR(pc->prescale_low); in sti_pwm_probe_dt()
526 pc->prescale_high = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
528 if (IS_ERR(pc->prescale_high)) in sti_pwm_probe_dt()
529 return PTR_ERR(pc->prescale_high); in sti_pwm_probe_dt()
531 pc->pwm_out_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
533 if (IS_ERR(pc->pwm_out_en)) in sti_pwm_probe_dt()
534 return PTR_ERR(pc->pwm_out_en); in sti_pwm_probe_dt()
536 pc->pwm_cpt_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
538 if (IS_ERR(pc->pwm_cpt_en)) in sti_pwm_probe_dt()
539 return PTR_ERR(pc->pwm_cpt_en); in sti_pwm_probe_dt()
541 pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
543 if (IS_ERR(pc->pwm_cpt_int_en)) in sti_pwm_probe_dt()
544 return PTR_ERR(pc->pwm_cpt_int_en); in sti_pwm_probe_dt()
546 pc->pwm_cpt_int_stat = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
548 if (PTR_ERR_OR_ZERO(pc->pwm_cpt_int_stat)) in sti_pwm_probe_dt()
549 return PTR_ERR(pc->pwm_cpt_int_stat); in sti_pwm_probe_dt()
562 struct device *dev = &pdev->dev; in sti_pwm_probe()
570 return -ENOMEM; in sti_pwm_probe()
574 return -ENOMEM; in sti_pwm_probe()
576 pc->mmio = devm_platform_ioremap_resource(pdev, 0); in sti_pwm_probe()
577 if (IS_ERR(pc->mmio)) in sti_pwm_probe()
578 return PTR_ERR(pc->mmio); in sti_pwm_probe()
580 pc->regmap = devm_regmap_init_mmio(dev, pc->mmio, in sti_pwm_probe()
582 if (IS_ERR(pc->regmap)) in sti_pwm_probe()
583 return PTR_ERR(pc->regmap); in sti_pwm_probe()
589 ret = devm_request_irq(&pdev->dev, irq, sti_pwm_interrupt, 0, in sti_pwm_probe()
590 pdev->name, pc); in sti_pwm_probe()
592 dev_err(&pdev->dev, "Failed to request IRQ\n"); in sti_pwm_probe()
597 * Setup PWM data with default values: some values could be replaced in sti_pwm_probe()
600 cdata->reg_fields = sti_pwm_regfields; in sti_pwm_probe()
601 cdata->max_prescale = 0xff; in sti_pwm_probe()
602 cdata->max_pwm_cnt = 255; in sti_pwm_probe()
603 cdata->pwm_num_devs = 0; in sti_pwm_probe()
604 cdata->cpt_num_devs = 0; in sti_pwm_probe()
606 pc->cdata = cdata; in sti_pwm_probe()
607 pc->dev = dev; in sti_pwm_probe()
608 pc->en_count = 0; in sti_pwm_probe()
609 mutex_init(&pc->sti_pwm_lock); in sti_pwm_probe()
615 if (cdata->pwm_num_devs) { in sti_pwm_probe()
616 pc->pwm_clk = of_clk_get_by_name(dev->of_node, "pwm"); in sti_pwm_probe()
617 if (IS_ERR(pc->pwm_clk)) { in sti_pwm_probe()
618 dev_err(dev, "failed to get PWM clock\n"); in sti_pwm_probe()
619 return PTR_ERR(pc->pwm_clk); in sti_pwm_probe()
622 ret = clk_prepare(pc->pwm_clk); in sti_pwm_probe()
629 if (cdata->cpt_num_devs) { in sti_pwm_probe()
630 pc->cpt_clk = of_clk_get_by_name(dev->of_node, "capture"); in sti_pwm_probe()
631 if (IS_ERR(pc->cpt_clk)) { in sti_pwm_probe()
632 dev_err(dev, "failed to get PWM capture clock\n"); in sti_pwm_probe()
633 return PTR_ERR(pc->cpt_clk); in sti_pwm_probe()
636 ret = clk_prepare(pc->cpt_clk); in sti_pwm_probe()
642 cdata->ddata = devm_kzalloc(dev, cdata->cpt_num_devs * sizeof(*cdata->ddata), GFP_KERNEL); in sti_pwm_probe()
643 if (!cdata->ddata) in sti_pwm_probe()
644 return -ENOMEM; in sti_pwm_probe()
647 pc->chip.dev = dev; in sti_pwm_probe()
648 pc->chip.ops = &sti_pwm_ops; in sti_pwm_probe()
649 pc->chip.npwm = pc->cdata->pwm_num_devs; in sti_pwm_probe()
651 for (i = 0; i < cdata->cpt_num_devs; i++) { in sti_pwm_probe()
652 struct sti_cpt_ddata *ddata = &cdata->ddata[i]; in sti_pwm_probe()
654 init_waitqueue_head(&ddata->wait); in sti_pwm_probe()
655 mutex_init(&ddata->lock); in sti_pwm_probe()
658 ret = pwmchip_add(&pc->chip); in sti_pwm_probe()
660 clk_unprepare(pc->pwm_clk); in sti_pwm_probe()
661 clk_unprepare(pc->cpt_clk); in sti_pwm_probe()
674 pwmchip_remove(&pc->chip); in sti_pwm_remove()
676 clk_unprepare(pc->pwm_clk); in sti_pwm_remove()
677 clk_unprepare(pc->cpt_clk); in sti_pwm_remove()
681 { .compatible = "st,sti-pwm", },
688 .name = "sti-pwm",
697 MODULE_DESCRIPTION("STMicroelectronics ST PWM driver");