Lines Matching +full:fixed +full:- +full:mmio +full:- +full:clock

1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2013-2016 STMicroelectronics (R&D) Limited
59 * Each capture input can be programmed to detect rising-edge, falling-edge,
96 void __iomem *mmio; member
124 clk_rate = clk_get_rate(pc->pwm_clk); in sti_pwm_get_prescale()
126 dev_err(pc->dev, "failed to get clock rate\n"); in sti_pwm_get_prescale()
127 return -EINVAL; in sti_pwm_get_prescale()
131 * prescale = ((period_ns * clk_rate) / (10^9 * (max_pwm_cnt + 1)) - 1 in sti_pwm_get_prescale()
134 value *= pc->max_pwm_cnt + 1; in sti_pwm_get_prescale()
137 return -EINVAL; in sti_pwm_get_prescale()
139 ps = period / value - 1; in sti_pwm_get_prescale()
140 if (ps > pc->max_prescale) in sti_pwm_get_prescale()
141 return -EINVAL; in sti_pwm_get_prescale()
149 * For STiH4xx PWM IP, the PWM period is fixed to 256 local clock cycles. The
150 * only way to change the period (apart from changing the PWM input clock) is
151 * to change the PWM clock prescaler.
154 * period values are supported (for a particular clock rate). The requested
162 struct pwm_device *cur = pc->cur; in sti_pwm_config()
163 struct device *dev = pc->dev; in sti_pwm_config()
167 ncfg = hweight_long(pc->configured); in sti_pwm_config()
184 ((ncfg == 1) && (pwm->hwpwm == cur->hwpwm)) || in sti_pwm_config()
185 ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) || in sti_pwm_config()
187 /* Enable clock before writing to PWM registers. */ in sti_pwm_config()
188 ret = clk_enable(pc->pwm_clk); in sti_pwm_config()
192 ret = clk_enable(pc->cpt_clk); in sti_pwm_config()
203 ret = regmap_field_write(pc->prescale_low, value); in sti_pwm_config()
209 ret = regmap_field_write(pc->prescale_high, value); in sti_pwm_config()
215 * When PWMVal == 0, PWM pulse = 1 local clock cycle. in sti_pwm_config()
220 value = pc->max_pwm_cnt * duty_ns / period_ns; in sti_pwm_config()
222 ret = regmap_write(pc->regmap, PWM_OUT_VAL(pwm->hwpwm), value); in sti_pwm_config()
226 ret = regmap_field_write(pc->pwm_cpt_int_en, 0); in sti_pwm_config()
228 set_bit(pwm->hwpwm, &pc->configured); in sti_pwm_config()
229 pc->cur = pwm; in sti_pwm_config()
234 return -EINVAL; in sti_pwm_config()
238 clk_disable(pc->pwm_clk); in sti_pwm_config()
239 clk_disable(pc->cpt_clk); in sti_pwm_config()
246 struct device *dev = pc->dev; in sti_pwm_enable()
253 mutex_lock(&pc->sti_pwm_lock); in sti_pwm_enable()
255 if (!pc->en_count) { in sti_pwm_enable()
256 ret = clk_enable(pc->pwm_clk); in sti_pwm_enable()
260 ret = clk_enable(pc->cpt_clk); in sti_pwm_enable()
264 ret = regmap_field_write(pc->pwm_out_en, 1); in sti_pwm_enable()
267 pwm->hwpwm, ret); in sti_pwm_enable()
272 pc->en_count++; in sti_pwm_enable()
275 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_enable()
283 mutex_lock(&pc->sti_pwm_lock); in sti_pwm_disable()
285 if (--pc->en_count) { in sti_pwm_disable()
286 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_disable()
290 regmap_field_write(pc->pwm_out_en, 0); in sti_pwm_disable()
292 clk_disable(pc->pwm_clk); in sti_pwm_disable()
293 clk_disable(pc->cpt_clk); in sti_pwm_disable()
295 mutex_unlock(&pc->sti_pwm_lock); in sti_pwm_disable()
302 clear_bit(pwm->hwpwm, &pc->configured); in sti_pwm_free()
309 struct sti_cpt_ddata *ddata = &pc->ddata[pwm->hwpwm]; in sti_pwm_capture()
310 struct device *dev = pc->dev; in sti_pwm_capture()
315 if (pwm->hwpwm >= pc->cpt_num_devs) { in sti_pwm_capture()
316 dev_err(dev, "device %u is not valid\n", pwm->hwpwm); in sti_pwm_capture()
317 return -EINVAL; in sti_pwm_capture()
320 mutex_lock(&ddata->lock); in sti_pwm_capture()
321 ddata->index = 0; in sti_pwm_capture()
324 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_RISING); in sti_pwm_capture()
325 regmap_field_write(pc->pwm_cpt_int_en, BIT(pwm->hwpwm)); in sti_pwm_capture()
328 ret = regmap_field_write(pc->pwm_cpt_en, 1); in sti_pwm_capture()
331 pwm->hwpwm, ret); in sti_pwm_capture()
335 ret = wait_event_interruptible_timeout(ddata->wait, ddata->index > 1, in sti_pwm_capture()
338 regmap_write(pc->regmap, PWM_CPT_EDGE(pwm->hwpwm), CPT_EDGE_DISABLED); in sti_pwm_capture()
340 if (ret == -ERESTARTSYS) in sti_pwm_capture()
343 switch (ddata->index) { in sti_pwm_capture()
348 * - input signal is constant of less than 1 Hz in sti_pwm_capture()
349 * - there is no input signal at all in sti_pwm_capture()
353 result->period = 0; in sti_pwm_capture()
354 result->duty_cycle = 0; in sti_pwm_capture()
360 high = ddata->snapshot[1] - ddata->snapshot[0]; in sti_pwm_capture()
361 low = ddata->snapshot[2] - ddata->snapshot[1]; in sti_pwm_capture()
363 effective_ticks = clk_get_rate(pc->cpt_clk); in sti_pwm_capture()
365 result->period = (high + low) * NSEC_PER_SEC; in sti_pwm_capture()
366 result->period /= effective_ticks; in sti_pwm_capture()
368 result->duty_cycle = high * NSEC_PER_SEC; in sti_pwm_capture()
369 result->duty_cycle /= effective_ticks; in sti_pwm_capture()
380 regmap_field_write(pc->pwm_cpt_en, 0); in sti_pwm_capture()
382 mutex_unlock(&ddata->lock); in sti_pwm_capture()
390 struct device *dev = pc->dev; in sti_pwm_apply()
393 if (pwm->hwpwm >= pc->pwm_num_devs) { in sti_pwm_apply()
395 pwm->hwpwm); in sti_pwm_apply()
396 return -EINVAL; in sti_pwm_apply()
399 if (state->polarity != PWM_POLARITY_NORMAL) in sti_pwm_apply()
400 return -EINVAL; in sti_pwm_apply()
402 if (!state->enabled) { in sti_pwm_apply()
403 if (pwm->state.enabled) in sti_pwm_apply()
409 err = sti_pwm_config(chip, pwm, state->duty_cycle, state->period); in sti_pwm_apply()
413 if (!pwm->state.enabled) in sti_pwm_apply()
428 struct device *dev = pc->dev; in sti_pwm_interrupt()
435 ret = regmap_field_read(pc->pwm_cpt_int_stat, &cpt_int_stat); in sti_pwm_interrupt()
440 devicenum = ffs(cpt_int_stat) - 1; in sti_pwm_interrupt()
442 ddata = &pc->ddata[devicenum]; in sti_pwm_interrupt()
460 regmap_read(pc->regmap, in sti_pwm_interrupt()
462 &ddata->snapshot[ddata->index]); in sti_pwm_interrupt()
464 switch (ddata->index) { in sti_pwm_interrupt()
467 regmap_read(pc->regmap, PWM_CPT_EDGE(devicenum), &reg); in sti_pwm_interrupt()
469 regmap_write(pc->regmap, PWM_CPT_EDGE(devicenum), reg); in sti_pwm_interrupt()
471 ddata->index++; in sti_pwm_interrupt()
475 regmap_write(pc->regmap, in sti_pwm_interrupt()
478 wake_up(&ddata->wait); in sti_pwm_interrupt()
491 regmap_write(pc->regmap, PWM_INT_ACK, PWM_INT_ACK_MASK); in sti_pwm_interrupt()
498 struct device *dev = pc->dev; in sti_pwm_probe_regmap()
500 pc->prescale_low = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_regmap()
502 if (IS_ERR(pc->prescale_low)) in sti_pwm_probe_regmap()
503 return PTR_ERR(pc->prescale_low); in sti_pwm_probe_regmap()
505 pc->prescale_high = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_regmap()
507 if (IS_ERR(pc->prescale_high)) in sti_pwm_probe_regmap()
508 return PTR_ERR(pc->prescale_high); in sti_pwm_probe_regmap()
510 pc->pwm_out_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_regmap()
512 if (IS_ERR(pc->pwm_out_en)) in sti_pwm_probe_regmap()
513 return PTR_ERR(pc->pwm_out_en); in sti_pwm_probe_regmap()
515 pc->pwm_cpt_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_regmap()
517 if (IS_ERR(pc->pwm_cpt_en)) in sti_pwm_probe_regmap()
518 return PTR_ERR(pc->pwm_cpt_en); in sti_pwm_probe_regmap()
520 pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_regmap()
522 if (IS_ERR(pc->pwm_cpt_int_en)) in sti_pwm_probe_regmap()
523 return PTR_ERR(pc->pwm_cpt_int_en); in sti_pwm_probe_regmap()
525 pc->pwm_cpt_int_stat = devm_regmap_field_alloc(dev, pc->regmap, in sti_pwm_probe_regmap()
527 if (PTR_ERR_OR_ZERO(pc->pwm_cpt_int_stat)) in sti_pwm_probe_regmap()
528 return PTR_ERR(pc->pwm_cpt_int_stat); in sti_pwm_probe_regmap()
541 struct device *dev = &pdev->dev; in sti_pwm_probe()
542 struct device_node *np = dev->of_node; in sti_pwm_probe()
551 ret = of_property_read_u32(np, "st,pwm-num-chan", &num_devs); in sti_pwm_probe()
555 ret = of_property_read_u32(np, "st,capture-num-chan", &num_devs); in sti_pwm_probe()
560 return dev_err_probe(dev, -EINVAL, "No channels configured\n"); in sti_pwm_probe()
567 pc->mmio = devm_platform_ioremap_resource(pdev, 0); in sti_pwm_probe()
568 if (IS_ERR(pc->mmio)) in sti_pwm_probe()
569 return PTR_ERR(pc->mmio); in sti_pwm_probe()
571 pc->regmap = devm_regmap_init_mmio(dev, pc->mmio, in sti_pwm_probe()
573 if (IS_ERR(pc->regmap)) in sti_pwm_probe()
574 return dev_err_probe(dev, PTR_ERR(pc->regmap), in sti_pwm_probe()
581 ret = devm_request_irq(&pdev->dev, irq, sti_pwm_interrupt, 0, in sti_pwm_probe()
582 pdev->name, pc); in sti_pwm_probe()
584 dev_err_probe(&pdev->dev, ret, "Failed to request IRQ\n"); in sti_pwm_probe()
590 pc->max_prescale = 0xff; in sti_pwm_probe()
591 pc->max_pwm_cnt = 255; in sti_pwm_probe()
592 pc->pwm_num_devs = pwm_num_devs; in sti_pwm_probe()
593 pc->cpt_num_devs = cpt_num_devs; in sti_pwm_probe()
595 pc->dev = dev; in sti_pwm_probe()
596 pc->en_count = 0; in sti_pwm_probe()
597 mutex_init(&pc->sti_pwm_lock); in sti_pwm_probe()
604 pc->pwm_clk = devm_clk_get_prepared(dev, "pwm"); in sti_pwm_probe()
605 if (IS_ERR(pc->pwm_clk)) in sti_pwm_probe()
606 return dev_err_probe(dev, PTR_ERR(pc->pwm_clk), in sti_pwm_probe()
607 "failed to get PWM clock\n"); in sti_pwm_probe()
611 pc->cpt_clk = devm_clk_get_prepared(dev, "capture"); in sti_pwm_probe()
612 if (IS_ERR(pc->cpt_clk)) in sti_pwm_probe()
613 return dev_err_probe(dev, PTR_ERR(pc->cpt_clk), in sti_pwm_probe()
614 "failed to get PWM capture clock\n"); in sti_pwm_probe()
616 pc->ddata = devm_kcalloc(dev, cpt_num_devs, in sti_pwm_probe()
617 sizeof(*pc->ddata), GFP_KERNEL); in sti_pwm_probe()
618 if (!pc->ddata) in sti_pwm_probe()
619 return -ENOMEM; in sti_pwm_probe()
622 struct sti_cpt_ddata *ddata = &pc->ddata[i]; in sti_pwm_probe()
624 init_waitqueue_head(&ddata->wait); in sti_pwm_probe()
625 mutex_init(&ddata->lock); in sti_pwm_probe()
629 chip->ops = &sti_pwm_ops; in sti_pwm_probe()
639 { .compatible = "st,sti-pwm", },
646 .name = "sti-pwm",