Lines Matching +full:rk2928 +full:- +full:pwm

1 // SPDX-License-Identifier: GPL-2.0-only
3 * PWM driver for Rockchip SoCs
15 #include <linux/pwm.h>
60 struct pwm_device *pwm, in rockchip_pwm_get_state() argument
64 u32 enable_conf = pc->data->enable_conf; in rockchip_pwm_get_state()
70 ret = clk_enable(pc->pclk); in rockchip_pwm_get_state()
74 ret = clk_enable(pc->clk); in rockchip_pwm_get_state()
78 clk_rate = clk_get_rate(pc->clk); in rockchip_pwm_get_state()
80 tmp = readl_relaxed(pc->base + pc->data->regs.period); in rockchip_pwm_get_state()
81 tmp *= pc->data->prescaler * NSEC_PER_SEC; in rockchip_pwm_get_state()
82 state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); in rockchip_pwm_get_state()
84 tmp = readl_relaxed(pc->base + pc->data->regs.duty); in rockchip_pwm_get_state()
85 tmp *= pc->data->prescaler * NSEC_PER_SEC; in rockchip_pwm_get_state()
86 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); in rockchip_pwm_get_state()
88 val = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_get_state()
89 state->enabled = (val & enable_conf) == enable_conf; in rockchip_pwm_get_state()
91 if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE)) in rockchip_pwm_get_state()
92 state->polarity = PWM_POLARITY_INVERSED; in rockchip_pwm_get_state()
94 state->polarity = PWM_POLARITY_NORMAL; in rockchip_pwm_get_state()
96 clk_disable(pc->clk); in rockchip_pwm_get_state()
97 clk_disable(pc->pclk); in rockchip_pwm_get_state()
102 static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, in rockchip_pwm_config() argument
110 clk_rate = clk_get_rate(pc->clk); in rockchip_pwm_config()
117 div = clk_rate * state->period; in rockchip_pwm_config()
119 pc->data->prescaler * NSEC_PER_SEC); in rockchip_pwm_config()
121 div = clk_rate * state->duty_cycle; in rockchip_pwm_config()
122 duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC); in rockchip_pwm_config()
128 ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_config()
129 if (pc->data->supports_lock) { in rockchip_pwm_config()
131 writel_relaxed(ctrl, pc->base + pc->data->regs.ctrl); in rockchip_pwm_config()
134 writel(period, pc->base + pc->data->regs.period); in rockchip_pwm_config()
135 writel(duty, pc->base + pc->data->regs.duty); in rockchip_pwm_config()
137 if (pc->data->supports_polarity) { in rockchip_pwm_config()
139 if (state->polarity == PWM_POLARITY_INVERSED) in rockchip_pwm_config()
150 if (pc->data->supports_lock) in rockchip_pwm_config()
153 writel(ctrl, pc->base + pc->data->regs.ctrl); in rockchip_pwm_config()
157 struct pwm_device *pwm, in rockchip_pwm_enable() argument
161 u32 enable_conf = pc->data->enable_conf; in rockchip_pwm_enable()
166 ret = clk_enable(pc->clk); in rockchip_pwm_enable()
171 val = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_enable()
178 writel_relaxed(val, pc->base + pc->data->regs.ctrl); in rockchip_pwm_enable()
181 clk_disable(pc->clk); in rockchip_pwm_enable()
186 static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in rockchip_pwm_apply() argument
194 ret = clk_enable(pc->pclk); in rockchip_pwm_apply()
198 ret = clk_enable(pc->clk); in rockchip_pwm_apply()
202 pwm_get_state(pwm, &curstate); in rockchip_pwm_apply()
205 if (state->polarity != curstate.polarity && enabled && in rockchip_pwm_apply()
206 !pc->data->supports_lock) { in rockchip_pwm_apply()
207 ret = rockchip_pwm_enable(chip, pwm, false); in rockchip_pwm_apply()
213 rockchip_pwm_config(chip, pwm, state); in rockchip_pwm_apply()
214 if (state->enabled != enabled) { in rockchip_pwm_apply()
215 ret = rockchip_pwm_enable(chip, pwm, state->enabled); in rockchip_pwm_apply()
221 clk_disable(pc->clk); in rockchip_pwm_apply()
222 clk_disable(pc->pclk); in rockchip_pwm_apply()
288 { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
289 { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
290 { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
291 { .compatible = "rockchip,rk3328-pwm", .data = &pwm_data_v3},
304 chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pc)); in rockchip_pwm_probe()
309 pc->base = devm_platform_ioremap_resource(pdev, 0); in rockchip_pwm_probe()
310 if (IS_ERR(pc->base)) in rockchip_pwm_probe()
311 return PTR_ERR(pc->base); in rockchip_pwm_probe()
313 pc->clk = devm_clk_get(&pdev->dev, "pwm"); in rockchip_pwm_probe()
314 if (IS_ERR(pc->clk)) { in rockchip_pwm_probe()
315 pc->clk = devm_clk_get(&pdev->dev, NULL); in rockchip_pwm_probe()
316 if (IS_ERR(pc->clk)) in rockchip_pwm_probe()
317 return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk), in rockchip_pwm_probe()
318 "Can't get PWM clk\n"); in rockchip_pwm_probe()
321 count = of_count_phandle_with_args(pdev->dev.of_node, in rockchip_pwm_probe()
322 "clocks", "#clock-cells"); in rockchip_pwm_probe()
324 pc->pclk = devm_clk_get(&pdev->dev, "pclk"); in rockchip_pwm_probe()
326 pc->pclk = pc->clk; in rockchip_pwm_probe()
328 if (IS_ERR(pc->pclk)) in rockchip_pwm_probe()
329 return dev_err_probe(&pdev->dev, PTR_ERR(pc->pclk), "Can't get APB clk\n"); in rockchip_pwm_probe()
331 ret = clk_prepare_enable(pc->clk); in rockchip_pwm_probe()
333 return dev_err_probe(&pdev->dev, ret, "Can't prepare enable PWM clk\n"); in rockchip_pwm_probe()
335 ret = clk_prepare_enable(pc->pclk); in rockchip_pwm_probe()
337 dev_err_probe(&pdev->dev, ret, "Can't prepare enable APB clk\n"); in rockchip_pwm_probe()
343 pc->data = device_get_match_data(&pdev->dev); in rockchip_pwm_probe()
344 chip->ops = &rockchip_pwm_ops; in rockchip_pwm_probe()
346 enable_conf = pc->data->enable_conf; in rockchip_pwm_probe()
347 ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_probe()
352 dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n"); in rockchip_pwm_probe()
356 /* Keep the PWM clk enabled if the PWM appears to be up and running. */ in rockchip_pwm_probe()
358 clk_disable(pc->clk); in rockchip_pwm_probe()
360 clk_disable(pc->pclk); in rockchip_pwm_probe()
365 clk_disable_unprepare(pc->pclk); in rockchip_pwm_probe()
367 clk_disable_unprepare(pc->clk); in rockchip_pwm_probe()
379 clk_unprepare(pc->pclk); in rockchip_pwm_remove()
380 clk_unprepare(pc->clk); in rockchip_pwm_remove()
385 .name = "rockchip-pwm",
394 MODULE_DESCRIPTION("Rockchip SoC PWM driver");