Lines Matching +full:fu540 +full:- +full:c000 +full:- +full:v1

1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2018 SiFive
5 * Reference Manual : https://static.dev.sifive.com/FU540-C000-v1.0.pdf
8 * - When changing both duty cycle and period, we cannot prevent in
11 * - The hardware cannot generate a 100% duty cycle.
12 * - The hardware generates only inverted output.
64 mutex_lock(&ddata->lock); in pwm_sifive_request()
65 ddata->user_count++; in pwm_sifive_request()
66 mutex_unlock(&ddata->lock); in pwm_sifive_request()
75 mutex_lock(&ddata->lock); in pwm_sifive_free()
76 ddata->user_count--; in pwm_sifive_free()
77 mutex_unlock(&ddata->lock); in pwm_sifive_free()
80 /* Called holding ddata->lock */
95 scale_pow = div64_ul(ddata->approx_period * (u64)rate, NSEC_PER_SEC); in pwm_sifive_update_clock()
96 scale = clamp(ilog2(scale_pow) - PWM_SIFIVE_CMPWIDTH, 0, 0xf); in pwm_sifive_update_clock()
100 writel(val, ddata->regs + PWM_SIFIVE_PWMCFG); in pwm_sifive_update_clock()
104 ddata->real_period = div64_ul(num, rate); in pwm_sifive_update_clock()
105 dev_dbg(ddata->parent, in pwm_sifive_update_clock()
106 "New real_period = %u ns\n", ddata->real_period); in pwm_sifive_update_clock()
115 duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); in pwm_sifive_get_state()
117 state->enabled = duty > 0; in pwm_sifive_get_state()
119 val = readl(ddata->regs + PWM_SIFIVE_PWMCFG); in pwm_sifive_get_state()
121 state->enabled = false; in pwm_sifive_get_state()
123 state->period = ddata->real_period; in pwm_sifive_get_state()
124 state->duty_cycle = in pwm_sifive_get_state()
125 (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH; in pwm_sifive_get_state()
126 state->polarity = PWM_POLARITY_INVERSED; in pwm_sifive_get_state()
142 if (state->polarity != PWM_POLARITY_INVERSED) in pwm_sifive_apply()
143 return -EINVAL; in pwm_sifive_apply()
145 cur_state = pwm->state; in pwm_sifive_apply()
148 duty_cycle = state->duty_cycle; in pwm_sifive_apply()
149 if (!state->enabled) in pwm_sifive_apply()
159 frac = DIV64_U64_ROUND_CLOSEST(num, state->period); in pwm_sifive_apply()
161 frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1); in pwm_sifive_apply()
163 mutex_lock(&ddata->lock); in pwm_sifive_apply()
164 if (state->period != ddata->approx_period) { in pwm_sifive_apply()
167 * However if ddate->approx_period == 0 this is the first time we set in pwm_sifive_apply()
171 if (ddata->user_count != 1 && ddata->approx_period) { in pwm_sifive_apply()
172 mutex_unlock(&ddata->lock); in pwm_sifive_apply()
173 return -EBUSY; in pwm_sifive_apply()
175 ddata->approx_period = state->period; in pwm_sifive_apply()
176 pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk)); in pwm_sifive_apply()
178 mutex_unlock(&ddata->lock); in pwm_sifive_apply()
186 ret = clk_enable(ddata->clk); in pwm_sifive_apply()
193 writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); in pwm_sifive_apply()
195 if (!state->enabled) in pwm_sifive_apply()
196 clk_disable(ddata->clk); in pwm_sifive_apply()
216 mutex_lock(&ddata->lock); in pwm_sifive_clock_notifier()
217 pwm_sifive_update_clock(ddata, ndata->new_rate); in pwm_sifive_clock_notifier()
218 mutex_unlock(&ddata->lock); in pwm_sifive_clock_notifier()
226 struct device *dev = &pdev->dev; in pwm_sifive_probe()
238 ddata->parent = dev; in pwm_sifive_probe()
239 mutex_init(&ddata->lock); in pwm_sifive_probe()
240 chip->ops = &pwm_sifive_ops; in pwm_sifive_probe()
242 ddata->regs = devm_platform_ioremap_resource(pdev, 0); in pwm_sifive_probe()
243 if (IS_ERR(ddata->regs)) in pwm_sifive_probe()
244 return PTR_ERR(ddata->regs); in pwm_sifive_probe()
246 ddata->clk = devm_clk_get_prepared(dev, NULL); in pwm_sifive_probe()
247 if (IS_ERR(ddata->clk)) in pwm_sifive_probe()
248 return dev_err_probe(dev, PTR_ERR(ddata->clk), in pwm_sifive_probe()
251 ret = clk_enable(ddata->clk); in pwm_sifive_probe()
257 val = readl(ddata->regs + PWM_SIFIVE_PWMCFG); in pwm_sifive_probe()
261 for (i = 0; i < chip->npwm; ++i) { in pwm_sifive_probe()
262 val = readl(ddata->regs + PWM_SIFIVE_PWMCMP(i)); in pwm_sifive_probe()
272 ret = clk_enable(ddata->clk); in pwm_sifive_probe()
280 clk_disable(ddata->clk); in pwm_sifive_probe()
285 ddata->notifier.notifier_call = pwm_sifive_clock_notifier; in pwm_sifive_probe()
286 ret = clk_notifier_register(ddata->clk, &ddata->notifier); in pwm_sifive_probe()
299 dev_dbg(dev, "SiFive PWM chip registered %d PWMs\n", chip->npwm); in pwm_sifive_probe()
304 clk_notifier_unregister(ddata->clk, &ddata->notifier); in pwm_sifive_probe()
307 clk_disable(ddata->clk); in pwm_sifive_probe()
308 --enabled_clks; in pwm_sifive_probe()
322 clk_notifier_unregister(ddata->clk, &ddata->notifier); in pwm_sifive_remove()
324 for (ch = 0; ch < chip->npwm; ch++) { in pwm_sifive_remove()
325 pwm = &chip->pwms[ch]; in pwm_sifive_remove()
326 if (pwm->state.enabled) in pwm_sifive_remove()
327 clk_disable(ddata->clk); in pwm_sifive_remove()
341 .name = "pwm-sifive",