Lines Matching +full:pwm +full:- +full:enable

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>
30 #define STI_INT_EN 0x54 /* Interrupt Enable/Disable register */
59 * Each capture input can be programmed to detect rising-edge, falling-edge,
100 struct mutex sti_pwm_lock; /* To sync between enable/disable calls */
125 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_get_prescale()
130 clk_rate = clk_get_rate(pc->pwm_clk); in sti_pwm_get_prescale()
132 dev_err(pc->dev, "failed to get clock rate\n"); in sti_pwm_get_prescale()
133 return -EINVAL; in sti_pwm_get_prescale()
137 * prescale = ((period_ns * clk_rate) / (10^9 * (max_pwm_cnt + 1)) - 1 in sti_pwm_get_prescale()
140 value *= cdata->max_pwm_cnt + 1; in sti_pwm_get_prescale()
143 return -EINVAL; in sti_pwm_get_prescale()
145 ps = period / value - 1; in sti_pwm_get_prescale()
146 if (ps > cdata->max_prescale) in sti_pwm_get_prescale()
147 return -EINVAL; in sti_pwm_get_prescale()
155 * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles. The
156 * only way to change the period (apart from changing the PWM input clock) is
157 * to change the PWM clock prescaler.
163 static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, in sti_pwm_config() argument
167 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_config()
169 struct pwm_device *cur = pc->cur; in sti_pwm_config()
170 struct device *dev = pc->dev; in sti_pwm_config()
174 ncfg = hweight_long(pc->configured); in sti_pwm_config()
191 ((ncfg == 1) && (pwm->hwpwm == cur->hwpwm)) || in sti_pwm_config()
192 ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) || in sti_pwm_config()
194 /* Enable clock before writing to PWM registers. */ in sti_pwm_config()
195 ret = clk_enable(pc->pwm_clk); in sti_pwm_config()
199 ret = clk_enable(pc->cpt_clk); in sti_pwm_config()
210 ret = regmap_field_write(pc->prescale_low, value); in sti_pwm_config()
216 ret = regmap_field_write(pc->prescale_high, value); in sti_pwm_config()
222 * When PWMVal == 0, PWM pulse = 1 local clock cycle. in sti_pwm_config()
224 * PWM pulse = (max_pwm_count + 1) local cycles, in sti_pwm_config()
227 value = cdata->max_pwm_cnt * duty_ns / period_ns; in sti_pwm_config()
229 ret = regmap_write(pc->regmap, PWM_OUT_VAL(pwm->hwpwm), value); in sti_pwm_config()
233 ret = regmap_field_write(pc->pwm_cpt_int_en, 0); in sti_pwm_config()
235 set_bit(pwm->hwpwm, &pc->configured); in sti_pwm_config()
236 pc->cur = pwm; in sti_pwm_config()
241 return -EINVAL; in sti_pwm_config()
245 clk_disable(pc->pwm_clk); in sti_pwm_config()
246 clk_disable(pc->cpt_clk); in sti_pwm_config()
250 static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) in sti_pwm_enable() argument
253 struct device *dev = pc->dev; in sti_pwm_enable()
257 * Since we have a common enable for all PWM devices, do not enable if in sti_pwm_enable()
260 mutex_lock(&pc->sti_pwm_lock); in sti_pwm_enable()
262 if (!pc->en_count) { in sti_pwm_enable()
263 ret = clk_enable(pc->pwm_clk); in sti_pwm_enable()
267 ret = clk_enable(pc->cpt_clk); in sti_pwm_enable()
271 ret = regmap_field_write(pc->pwm_out_en, 1); in sti_pwm_enable()
273 dev_err(dev, "failed to enable PWM device %u: %d\n", in sti_pwm_enable()
274 pwm->hwpwm, ret); in sti_pwm_enable()
279 pc->en_count++; in sti_pwm_enable()
282 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_enable()
286 static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) in sti_pwm_disable() argument
290 mutex_lock(&pc->sti_pwm_lock); in sti_pwm_disable()
292 if (--pc->en_count) { in sti_pwm_disable()
293 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_disable()
297 regmap_field_write(pc->pwm_out_en, 0); in sti_pwm_disable()
299 clk_disable(pc->pwm_clk); in sti_pwm_disable()
300 clk_disable(pc->cpt_clk); in sti_pwm_disable()
302 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_disable()
305 static void sti_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in sti_pwm_free() argument
309 clear_bit(pwm->hwpwm, &pc->configured); in sti_pwm_free()
312 static int sti_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, in sti_pwm_capture() argument
316 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_capture()
317 struct sti_cpt_ddata *ddata = pwm_get_chip_data(pwm); in sti_pwm_capture()
318 struct device *dev = pc->dev; in sti_pwm_capture()
323 if (pwm->hwpwm >= cdata->cpt_num_devs) { in sti_pwm_capture()
324 dev_err(dev, "device %u is not valid\n", pwm->hwpwm); in sti_pwm_capture()
325 return -EINVAL; in sti_pwm_capture()
328 mutex_lock(&ddata->lock); in sti_pwm_capture()
329 ddata->index = 0; in sti_pwm_capture()
332 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_RISING); in sti_pwm_capture()
333 regmap_field_write(pc->pwm_cpt_int_en, BIT(pwm->hwpwm)); in sti_pwm_capture()
335 /* Enable capture */ in sti_pwm_capture()
336 ret = regmap_field_write(pc->pwm_cpt_en, 1); in sti_pwm_capture()
338 dev_err(dev, "failed to enable PWM capture %u: %d\n", in sti_pwm_capture()
339 pwm->hwpwm, ret); in sti_pwm_capture()
343 ret = wait_event_interruptible_timeout(ddata->wait, ddata->index > 1, in sti_pwm_capture()
346 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_DISABLED); in sti_pwm_capture()
348 if (ret == -ERESTARTSYS) in sti_pwm_capture()
351 switch (ddata->index) { in sti_pwm_capture()
356 * - input signal is constant of less than 1 Hz in sti_pwm_capture()
357 * - there is no input signal at all in sti_pwm_capture()
361 result->period = 0; in sti_pwm_capture()
362 result->duty_cycle = 0; in sti_pwm_capture()
368 high = ddata->snapshot[1] - ddata->snapshot[0]; in sti_pwm_capture()
369 low = ddata->snapshot[2] - ddata->snapshot[1]; in sti_pwm_capture()
371 effective_ticks = clk_get_rate(pc->cpt_clk); in sti_pwm_capture()
373 result->period = (high + low) * NSEC_PER_SEC; in sti_pwm_capture()
374 result->period /= effective_ticks; in sti_pwm_capture()
376 result->duty_cycle = high * NSEC_PER_SEC; in sti_pwm_capture()
377 result->duty_cycle /= effective_ticks; in sti_pwm_capture()
388 regmap_field_write(pc->pwm_cpt_en, 0); in sti_pwm_capture()
390 mutex_unlock(&ddata->lock); in sti_pwm_capture()
397 .enable = sti_pwm_enable,
406 struct device *dev = pc->dev; in sti_pwm_interrupt()
413 ret = regmap_field_read(pc->pwm_cpt_int_stat, &cpt_int_stat); in sti_pwm_interrupt()
418 devicenum = ffs(cpt_int_stat) - 1; in sti_pwm_interrupt()
420 ddata = pwm_get_chip_data(&pc->chip.pwms[devicenum]); in sti_pwm_interrupt()
438 regmap_read(pc->regmap, in sti_pwm_interrupt()
440 &ddata->snapshot[ddata->index]); in sti_pwm_interrupt()
442 switch (ddata->index) { in sti_pwm_interrupt()
445 regmap_read(pc->regmap, PWM_CPT_EDGE(devicenum), &reg); in sti_pwm_interrupt()
447 regmap_write(pc->regmap, PWM_CPT_EDGE(devicenum), reg); in sti_pwm_interrupt()
449 ddata->index++; in sti_pwm_interrupt()
453 regmap_write(pc->regmap, in sti_pwm_interrupt()
456 wake_up(&ddata->wait); in sti_pwm_interrupt()
469 regmap_write(pc->regmap, PWM_INT_ACK, PWM_INT_ACK_MASK); in sti_pwm_interrupt()
476 struct device *dev = pc->dev; in sti_pwm_probe_dt()
478 struct device_node *np = dev->of_node; in sti_pwm_probe_dt()
479 struct sti_pwm_compat_data *cdata = pc->cdata; in sti_pwm_probe_dt()
483 ret = of_property_read_u32(np, "st,pwm-num-chan", &num_devs); in sti_pwm_probe_dt()
485 cdata->pwm_num_devs = num_devs; in sti_pwm_probe_dt()
487 ret = of_property_read_u32(np, "st,capture-num-chan", &num_devs); in sti_pwm_probe_dt()
489 cdata->cpt_num_devs = num_devs; in sti_pwm_probe_dt()
491 if (!cdata->pwm_num_devs && !cdata->cpt_num_devs) { in sti_pwm_probe_dt()
493 return -EINVAL; in sti_pwm_probe_dt()
496 reg_fields = cdata->reg_fields; in sti_pwm_probe_dt()
498 pc->prescale_low = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
500 if (IS_ERR(pc->prescale_low)) in sti_pwm_probe_dt()
501 return PTR_ERR(pc->prescale_low); in sti_pwm_probe_dt()
503 pc->prescale_high = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
505 if (IS_ERR(pc->prescale_high)) in sti_pwm_probe_dt()
506 return PTR_ERR(pc->prescale_high); in sti_pwm_probe_dt()
508 pc->pwm_out_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
510 if (IS_ERR(pc->pwm_out_en)) in sti_pwm_probe_dt()
511 return PTR_ERR(pc->pwm_out_en); in sti_pwm_probe_dt()
513 pc->pwm_cpt_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
515 if (IS_ERR(pc->pwm_cpt_en)) in sti_pwm_probe_dt()
516 return PTR_ERR(pc->pwm_cpt_en); in sti_pwm_probe_dt()
518 pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
520 if (IS_ERR(pc->pwm_cpt_int_en)) in sti_pwm_probe_dt()
521 return PTR_ERR(pc->pwm_cpt_int_en); in sti_pwm_probe_dt()
523 pc->pwm_cpt_int_stat = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_dt()
525 if (PTR_ERR_OR_ZERO(pc->pwm_cpt_int_stat)) in sti_pwm_probe_dt()
526 return PTR_ERR(pc->pwm_cpt_int_stat); in sti_pwm_probe_dt()
539 struct device *dev = &pdev->dev; in sti_pwm_probe()
547 return -ENOMEM; in sti_pwm_probe()
551 return -ENOMEM; in sti_pwm_probe()
553 pc->mmio = devm_platform_ioremap_resource(pdev, 0); in sti_pwm_probe()
554 if (IS_ERR(pc->mmio)) in sti_pwm_probe()
555 return PTR_ERR(pc->mmio); in sti_pwm_probe()
557 pc->regmap = devm_regmap_init_mmio(dev, pc->mmio, in sti_pwm_probe()
559 if (IS_ERR(pc->regmap)) in sti_pwm_probe()
560 return PTR_ERR(pc->regmap); in sti_pwm_probe()
566 ret = devm_request_irq(&pdev->dev, irq, sti_pwm_interrupt, 0, in sti_pwm_probe()
567 pdev->name, pc); in sti_pwm_probe()
569 dev_err(&pdev->dev, "Failed to request IRQ\n"); in sti_pwm_probe()
574 * Setup PWM data with default values: some values could be replaced in sti_pwm_probe()
577 cdata->reg_fields = sti_pwm_regfields; in sti_pwm_probe()
578 cdata->max_prescale = 0xff; in sti_pwm_probe()
579 cdata->max_pwm_cnt = 255; in sti_pwm_probe()
580 cdata->pwm_num_devs = 0; in sti_pwm_probe()
581 cdata->cpt_num_devs = 0; in sti_pwm_probe()
583 pc->cdata = cdata; in sti_pwm_probe()
584 pc->dev = dev; in sti_pwm_probe()
585 pc->en_count = 0; in sti_pwm_probe()
586 mutex_init(&pc->sti_pwm_lock); in sti_pwm_probe()
592 if (cdata->pwm_num_devs) { in sti_pwm_probe()
593 pc->pwm_clk = of_clk_get_by_name(dev->of_node, "pwm"); in sti_pwm_probe()
594 if (IS_ERR(pc->pwm_clk)) { in sti_pwm_probe()
595 dev_err(dev, "failed to get PWM clock\n"); in sti_pwm_probe()
596 return PTR_ERR(pc->pwm_clk); in sti_pwm_probe()
599 ret = clk_prepare(pc->pwm_clk); in sti_pwm_probe()
606 if (cdata->cpt_num_devs) { in sti_pwm_probe()
607 pc->cpt_clk = of_clk_get_by_name(dev->of_node, "capture"); in sti_pwm_probe()
608 if (IS_ERR(pc->cpt_clk)) { in sti_pwm_probe()
609 dev_err(dev, "failed to get PWM capture clock\n"); in sti_pwm_probe()
610 return PTR_ERR(pc->cpt_clk); in sti_pwm_probe()
613 ret = clk_prepare(pc->cpt_clk); in sti_pwm_probe()
620 pc->chip.dev = dev; in sti_pwm_probe()
621 pc->chip.ops = &sti_pwm_ops; in sti_pwm_probe()
622 pc->chip.npwm = pc->cdata->pwm_num_devs; in sti_pwm_probe()
624 ret = pwmchip_add(&pc->chip); in sti_pwm_probe()
626 clk_unprepare(pc->pwm_clk); in sti_pwm_probe()
627 clk_unprepare(pc->cpt_clk); in sti_pwm_probe()
631 for (i = 0; i < cdata->cpt_num_devs; i++) { in sti_pwm_probe()
636 return -ENOMEM; in sti_pwm_probe()
638 init_waitqueue_head(&ddata->wait); in sti_pwm_probe()
639 mutex_init(&ddata->lock); in sti_pwm_probe()
641 pwm_set_chip_data(&pc->chip.pwms[i], ddata); in sti_pwm_probe()
653 pwmchip_remove(&pc->chip); in sti_pwm_remove()
655 clk_unprepare(pc->pwm_clk); in sti_pwm_remove()
656 clk_unprepare(pc->cpt_clk); in sti_pwm_remove()
662 { .compatible = "st,sti-pwm", },
669 .name = "sti-pwm",
678 MODULE_DESCRIPTION("STMicroelectronics ST PWM driver");