Lines Matching +full:neg +full:- +full:edge
1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2021-2023 Microchip Corporation. All rights reserved.
8 * https://www.microsemi.com/document-portal/doc_download/1245275-corepwm-hb
11 * - If the IP block is configured without "shadow registers", all register
19 * - The IP block has no concept of a duty cycle, only rising/falling edges of
22 * or a falling edge is possible. I.E. a 50% waveform at twice the requested
28 * - The PWM period is set for the whole IP block not per channel. The driver
78 * 0-7 and the upper reg 8-15. Check if the pwm is in the upper reg in mchp_core_pwm_enable()
81 reg_offset = MCHPCOREPWM_EN(pwm->hwpwm >> 3); in mchp_core_pwm_enable()
82 shift = pwm->hwpwm & 7; in mchp_core_pwm_enable()
84 channel_enable = readb_relaxed(mchp_core_pwm->base + reg_offset); in mchp_core_pwm_enable()
88 writel_relaxed(channel_enable, mchp_core_pwm->base + reg_offset); in mchp_core_pwm_enable()
89 mchp_core_pwm->channel_enabled &= ~BIT(pwm->hwpwm); in mchp_core_pwm_enable()
90 mchp_core_pwm->channel_enabled |= enable << pwm->hwpwm; in mchp_core_pwm_enable()
95 * This is a NO-OP if the channel does not have shadow registers. in mchp_core_pwm_enable()
97 if (mchp_core_pwm->sync_update_mask & (1 << pwm->hwpwm)) in mchp_core_pwm_enable()
98 mchp_core_pwm->update_timestamp = ktime_add_ns(ktime_get(), period); in mchp_core_pwm_enable()
112 if (mchp_core_pwm->sync_update_mask & (1 << channel)) { in mchp_core_pwm_wait_for_sync_update()
117 remaining_ns = ktime_to_ns(ktime_sub(mchp_core_pwm->update_timestamp, in mchp_core_pwm_wait_for_sync_update()
145 duty_steps = mul_u64_u64_div_u64(state->duty_cycle, clk_rate, tmp); in mchp_core_pwm_calc_duty()
161 * In that case set the unwanted edge to a value that never in mchp_core_pwm_apply_duty()
167 if (state->polarity == PWM_POLARITY_INVERSED) { in mchp_core_pwm_apply_duty()
180 writel_relaxed(posedge, mchp_core_pwm->base + MCHPCOREPWM_POSEDGE(pwm->hwpwm)); in mchp_core_pwm_apply_duty()
181 writel_relaxed(negedge, mchp_core_pwm->base + MCHPCOREPWM_NEGEDGE(pwm->hwpwm)); in mchp_core_pwm_apply_duty()
194 * period = ------------------------------------- in mchp_core_pwm_calc_period()
209 * neg-/pos-edge issue described in the limitations. in mchp_core_pwm_calc_period()
211 tmp = mul_u64_u64_div_u64(state->period, clk_rate, NSEC_PER_SEC); in mchp_core_pwm_calc_period()
242 return -EINVAL; in mchp_core_pwm_calc_period()
249 * prescale = ------------------------- - 1 in mchp_core_pwm_calc_period()
254 * ------------------- was precomputed as `tmp` in mchp_core_pwm_calc_period()
257 *prescale = ((u16)tmp) / (MCHPCOREPWM_PERIOD_STEPS_MAX + 1) - 1; in mchp_core_pwm_calc_period()
262 * period_steps = ----------------------------- - 1 in mchp_core_pwm_calc_period()
283 if (!state->enabled) { in mchp_core_pwm_apply_locked()
284 mchp_core_pwm_enable(chip, pwm, false, pwm->state.period); in mchp_core_pwm_apply_locked()
293 clk_rate = clk_get_rate(mchp_core_pwm->clk); in mchp_core_pwm_apply_locked()
295 return -EINVAL; in mchp_core_pwm_apply_locked()
304 * cycle pos & neg edges in mchp_core_pwm_apply_locked()
310 period_locked = mchp_core_pwm->channel_enabled & ~(1 << pwm->hwpwm); in mchp_core_pwm_apply_locked()
316 hw_prescale = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_PRESCALE); in mchp_core_pwm_apply_locked()
317 hw_period_steps = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_PERIOD); in mchp_core_pwm_apply_locked()
321 return -EINVAL; in mchp_core_pwm_apply_locked()
331 return -EINVAL; in mchp_core_pwm_apply_locked()
348 writel_relaxed(prescale, mchp_core_pwm->base + MCHPCOREPWM_PRESCALE); in mchp_core_pwm_apply_locked()
349 writel_relaxed(period_steps, mchp_core_pwm->base + MCHPCOREPWM_PERIOD); in mchp_core_pwm_apply_locked()
354 mchp_core_pwm_enable(chip, pwm, true, pwm->state.period); in mchp_core_pwm_apply_locked()
365 mutex_lock(&mchp_core_pwm->lock); in mchp_core_pwm_apply()
367 mchp_core_pwm_wait_for_sync_update(mchp_core_pwm, pwm->hwpwm); in mchp_core_pwm_apply()
371 mutex_unlock(&mchp_core_pwm->lock); in mchp_core_pwm_apply()
384 mutex_lock(&mchp_core_pwm->lock); in mchp_core_pwm_get_state()
386 mchp_core_pwm_wait_for_sync_update(mchp_core_pwm, pwm->hwpwm); in mchp_core_pwm_get_state()
388 if (mchp_core_pwm->channel_enabled & (1 << pwm->hwpwm)) in mchp_core_pwm_get_state()
389 state->enabled = true; in mchp_core_pwm_get_state()
391 state->enabled = false; in mchp_core_pwm_get_state()
393 rate = clk_get_rate(mchp_core_pwm->clk); in mchp_core_pwm_get_state()
400 * period = ------------------------------------- in mchp_core_pwm_get_state()
408 prescale = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_PRESCALE); in mchp_core_pwm_get_state()
409 period_steps = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_PERIOD); in mchp_core_pwm_get_state()
411 state->period = (period_steps + 1) * (prescale + 1); in mchp_core_pwm_get_state()
412 state->period *= NSEC_PER_SEC; in mchp_core_pwm_get_state()
413 state->period = DIV64_U64_ROUND_UP(state->period, rate); in mchp_core_pwm_get_state()
415 posedge = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_POSEDGE(pwm->hwpwm)); in mchp_core_pwm_get_state()
416 negedge = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_NEGEDGE(pwm->hwpwm)); in mchp_core_pwm_get_state()
418 mutex_unlock(&mchp_core_pwm->lock); in mchp_core_pwm_get_state()
421 state->duty_cycle = state->period; in mchp_core_pwm_get_state()
422 state->period *= 2; in mchp_core_pwm_get_state()
424 duty_steps = abs((s16)posedge - (s16)negedge); in mchp_core_pwm_get_state()
425 state->duty_cycle = duty_steps * (prescale + 1) * NSEC_PER_SEC; in mchp_core_pwm_get_state()
426 state->duty_cycle = DIV64_U64_ROUND_UP(state->duty_cycle, rate); in mchp_core_pwm_get_state()
429 state->polarity = negedge < posedge ? PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL; in mchp_core_pwm_get_state()
441 .compatible = "microchip,corepwm-rtl-v4",
454 chip = devm_pwmchip_alloc(&pdev->dev, 16, sizeof(*mchp_core_pwm)); in mchp_core_pwm_probe()
459 mchp_core_pwm->base = devm_platform_get_and_ioremap_resource(pdev, 0, ®s); in mchp_core_pwm_probe()
460 if (IS_ERR(mchp_core_pwm->base)) in mchp_core_pwm_probe()
461 return PTR_ERR(mchp_core_pwm->base); in mchp_core_pwm_probe()
463 mchp_core_pwm->clk = devm_clk_get_enabled(&pdev->dev, NULL); in mchp_core_pwm_probe()
464 if (IS_ERR(mchp_core_pwm->clk)) in mchp_core_pwm_probe()
465 return dev_err_probe(&pdev->dev, PTR_ERR(mchp_core_pwm->clk), in mchp_core_pwm_probe()
468 if (of_property_read_u32(pdev->dev.of_node, "microchip,sync-update-mask", in mchp_core_pwm_probe()
469 &mchp_core_pwm->sync_update_mask)) in mchp_core_pwm_probe()
470 mchp_core_pwm->sync_update_mask = 0; in mchp_core_pwm_probe()
472 mutex_init(&mchp_core_pwm->lock); in mchp_core_pwm_probe()
474 chip->ops = &mchp_core_pwm_ops; in mchp_core_pwm_probe()
476 mchp_core_pwm->channel_enabled = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_EN(0)); in mchp_core_pwm_probe()
477 mchp_core_pwm->channel_enabled |= in mchp_core_pwm_probe()
478 readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_EN(1)) << 8; in mchp_core_pwm_probe()
484 writel_relaxed(1U, mchp_core_pwm->base + MCHPCOREPWM_SYNC_UPD); in mchp_core_pwm_probe()
485 mchp_core_pwm->update_timestamp = ktime_get(); in mchp_core_pwm_probe()
487 ret = devm_pwmchip_add(&pdev->dev, chip); in mchp_core_pwm_probe()
489 return dev_err_probe(&pdev->dev, ret, "Failed to add pwmchip\n"); in mchp_core_pwm_probe()
496 .name = "mchp-core-pwm",