Lines Matching +full:- +full:- +full:disable +full:- +full:tpm
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018-2019 NXP.
6 * - The TPM counter and period counter are shared between
9 * - Changes to polarity cannot be latched at the time of the
11 * - Changing period and duty cycle together isn't atomic,
49 * together as a 2-bit field here.
57 #define PWM_IMX_TPM_MOD_MOD GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
92 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip); in pwm_imx_tpm_round_state() local
96 rate = clk_get_rate(tpm->clk); in pwm_imx_tpm_round_state()
97 tmp = (u64)state->period * rate; in pwm_imx_tpm_round_state()
102 prescale = ilog2(clock_unit) + 1 - PWM_IMX_TPM_MOD_WIDTH; in pwm_imx_tpm_round_state()
105 return -ERANGE; in pwm_imx_tpm_round_state()
106 p->prescale = prescale; in pwm_imx_tpm_round_state()
110 return -EINVAL; in pwm_imx_tpm_round_state()
111 p->mod = period_count - 1; in pwm_imx_tpm_round_state()
116 real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate); in pwm_imx_tpm_round_state()
123 if (!state->enabled) in pwm_imx_tpm_round_state()
124 real_state->duty_cycle = 0; in pwm_imx_tpm_round_state()
126 real_state->duty_cycle = state->duty_cycle; in pwm_imx_tpm_round_state()
128 tmp = (u64)p->mod * real_state->duty_cycle; in pwm_imx_tpm_round_state()
129 p->val = DIV64_U64_ROUND_CLOSEST(tmp, real_state->period); in pwm_imx_tpm_round_state()
131 real_state->polarity = state->polarity; in pwm_imx_tpm_round_state()
132 real_state->enabled = state->enabled; in pwm_imx_tpm_round_state()
141 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip); in pwm_imx_tpm_get_state() local
146 state->period = tpm->real_period; in pwm_imx_tpm_get_state()
149 rate = clk_get_rate(tpm->clk); in pwm_imx_tpm_get_state()
150 val = readl(tpm->base + PWM_IMX_TPM_SC); in pwm_imx_tpm_get_state()
152 tmp = readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm)); in pwm_imx_tpm_get_state()
154 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate); in pwm_imx_tpm_get_state()
157 val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm)); in pwm_imx_tpm_get_state()
159 state->polarity = PWM_POLARITY_INVERSED; in pwm_imx_tpm_get_state()
165 state->polarity = PWM_POLARITY_NORMAL; in pwm_imx_tpm_get_state()
168 state->enabled = FIELD_GET(PWM_IMX_TPM_CnSC_ELS, val) ? true : false; in pwm_imx_tpm_get_state()
179 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip); in pwm_imx_tpm_apply_hw() local
186 if (state->period != tpm->real_period) { in pwm_imx_tpm_apply_hw()
188 * TPM counter is shared by multiple channels, so in pwm_imx_tpm_apply_hw()
193 if (tpm->user_count > 1) in pwm_imx_tpm_apply_hw()
194 return -EBUSY; in pwm_imx_tpm_apply_hw()
196 val = readl(tpm->base + PWM_IMX_TPM_SC); in pwm_imx_tpm_apply_hw()
199 if (cmod && cur_prescale != p->prescale) in pwm_imx_tpm_apply_hw()
200 return -EBUSY; in pwm_imx_tpm_apply_hw()
202 /* set TPM counter prescale */ in pwm_imx_tpm_apply_hw()
204 val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale); in pwm_imx_tpm_apply_hw()
205 writel(val, tpm->base + PWM_IMX_TPM_SC); in pwm_imx_tpm_apply_hw()
215 writel(p->mod, tpm->base + PWM_IMX_TPM_MOD); in pwm_imx_tpm_apply_hw()
216 tpm->real_period = state->period; in pwm_imx_tpm_apply_hw()
223 if (c.enabled && c.polarity != state->polarity) in pwm_imx_tpm_apply_hw()
224 return -EBUSY; in pwm_imx_tpm_apply_hw()
226 if (state->duty_cycle != c.duty_cycle) { in pwm_imx_tpm_apply_hw()
235 writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm)); in pwm_imx_tpm_apply_hw()
241 timeout = jiffies + msecs_to_jiffies(tpm->real_period / in pwm_imx_tpm_apply_hw()
243 while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod in pwm_imx_tpm_apply_hw()
244 || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm)) in pwm_imx_tpm_apply_hw()
245 != p->val) { in pwm_imx_tpm_apply_hw()
247 return -ETIME; in pwm_imx_tpm_apply_hw()
253 * polarity settings will enabled/disable output status in pwm_imx_tpm_apply_hw()
258 val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm)); in pwm_imx_tpm_apply_hw()
261 if (state->enabled) { in pwm_imx_tpm_apply_hw()
263 * set polarity (for edge-aligned PWM modes) in pwm_imx_tpm_apply_hw()
270 val |= (state->polarity == PWM_POLARITY_NORMAL) ? in pwm_imx_tpm_apply_hw()
274 writel(val, tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm)); in pwm_imx_tpm_apply_hw()
277 if (state->enabled != c.enabled) { in pwm_imx_tpm_apply_hw()
278 val = readl(tpm->base + PWM_IMX_TPM_SC); in pwm_imx_tpm_apply_hw()
279 if (state->enabled) { in pwm_imx_tpm_apply_hw()
280 if (++tpm->enable_count == 1) in pwm_imx_tpm_apply_hw()
283 if (--tpm->enable_count == 0) in pwm_imx_tpm_apply_hw()
286 writel(val, tpm->base + PWM_IMX_TPM_SC); in pwm_imx_tpm_apply_hw()
296 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip); in pwm_imx_tpm_apply() local
305 mutex_lock(&tpm->lock); in pwm_imx_tpm_apply()
307 mutex_unlock(&tpm->lock); in pwm_imx_tpm_apply()
314 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip); in pwm_imx_tpm_request() local
316 mutex_lock(&tpm->lock); in pwm_imx_tpm_request()
317 tpm->user_count++; in pwm_imx_tpm_request()
318 mutex_unlock(&tpm->lock); in pwm_imx_tpm_request()
325 struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip); in pwm_imx_tpm_free() local
327 mutex_lock(&tpm->lock); in pwm_imx_tpm_free()
328 tpm->user_count--; in pwm_imx_tpm_free()
329 mutex_unlock(&tpm->lock); in pwm_imx_tpm_free()
342 struct imx_tpm_pwm_chip *tpm; in pwm_imx_tpm_probe() local
346 tpm = devm_kzalloc(&pdev->dev, sizeof(*tpm), GFP_KERNEL); in pwm_imx_tpm_probe()
347 if (!tpm) in pwm_imx_tpm_probe()
348 return -ENOMEM; in pwm_imx_tpm_probe()
350 platform_set_drvdata(pdev, tpm); in pwm_imx_tpm_probe()
352 tpm->base = devm_platform_ioremap_resource(pdev, 0); in pwm_imx_tpm_probe()
353 if (IS_ERR(tpm->base)) in pwm_imx_tpm_probe()
354 return PTR_ERR(tpm->base); in pwm_imx_tpm_probe()
356 tpm->clk = devm_clk_get(&pdev->dev, NULL); in pwm_imx_tpm_probe()
357 if (IS_ERR(tpm->clk)) in pwm_imx_tpm_probe()
358 return dev_err_probe(&pdev->dev, PTR_ERR(tpm->clk), in pwm_imx_tpm_probe()
361 ret = clk_prepare_enable(tpm->clk); in pwm_imx_tpm_probe()
363 dev_err(&pdev->dev, in pwm_imx_tpm_probe()
368 tpm->chip.dev = &pdev->dev; in pwm_imx_tpm_probe()
369 tpm->chip.ops = &imx_tpm_pwm_ops; in pwm_imx_tpm_probe()
372 val = readl(tpm->base + PWM_IMX_TPM_PARAM); in pwm_imx_tpm_probe()
373 tpm->chip.npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val); in pwm_imx_tpm_probe()
375 mutex_init(&tpm->lock); in pwm_imx_tpm_probe()
377 ret = pwmchip_add(&tpm->chip); in pwm_imx_tpm_probe()
379 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); in pwm_imx_tpm_probe()
380 clk_disable_unprepare(tpm->clk); in pwm_imx_tpm_probe()
388 struct imx_tpm_pwm_chip *tpm = platform_get_drvdata(pdev); in pwm_imx_tpm_remove() local
390 pwmchip_remove(&tpm->chip); in pwm_imx_tpm_remove()
392 clk_disable_unprepare(tpm->clk); in pwm_imx_tpm_remove()
397 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev); in pwm_imx_tpm_suspend() local
399 if (tpm->enable_count > 0) in pwm_imx_tpm_suspend()
400 return -EBUSY; in pwm_imx_tpm_suspend()
407 tpm->real_period = 0; in pwm_imx_tpm_suspend()
409 clk_disable_unprepare(tpm->clk); in pwm_imx_tpm_suspend()
416 struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev); in pwm_imx_tpm_resume() local
419 ret = clk_prepare_enable(tpm->clk); in pwm_imx_tpm_resume()
430 { .compatible = "fsl,imx7ulp-pwm", },
437 .name = "imx7ulp-tpm-pwm",
447 MODULE_DESCRIPTION("i.MX TPM PWM Driver");