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>
61 struct pwm_device *pwm, in rockchip_pwm_get_state() argument
65 u32 enable_conf = pc->data->enable_conf; in rockchip_pwm_get_state()
71 ret = clk_enable(pc->pclk); in rockchip_pwm_get_state()
75 ret = clk_enable(pc->clk); in rockchip_pwm_get_state()
79 clk_rate = clk_get_rate(pc->clk); in rockchip_pwm_get_state()
81 tmp = readl_relaxed(pc->base + pc->data->regs.period); in rockchip_pwm_get_state()
82 tmp *= pc->data->prescaler * NSEC_PER_SEC; in rockchip_pwm_get_state()
83 state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); in rockchip_pwm_get_state()
85 tmp = readl_relaxed(pc->base + pc->data->regs.duty); in rockchip_pwm_get_state()
86 tmp *= pc->data->prescaler * NSEC_PER_SEC; in rockchip_pwm_get_state()
87 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); in rockchip_pwm_get_state()
89 val = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_get_state()
90 state->enabled = (val & enable_conf) == enable_conf; in rockchip_pwm_get_state()
92 if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE)) in rockchip_pwm_get_state()
93 state->polarity = PWM_POLARITY_INVERSED; in rockchip_pwm_get_state()
95 state->polarity = PWM_POLARITY_NORMAL; in rockchip_pwm_get_state()
97 clk_disable(pc->clk); in rockchip_pwm_get_state()
98 clk_disable(pc->pclk); in rockchip_pwm_get_state()
103 static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, in rockchip_pwm_config() argument
111 clk_rate = clk_get_rate(pc->clk); in rockchip_pwm_config()
118 div = clk_rate * state->period; in rockchip_pwm_config()
120 pc->data->prescaler * NSEC_PER_SEC); in rockchip_pwm_config()
122 div = clk_rate * state->duty_cycle; in rockchip_pwm_config()
123 duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC); in rockchip_pwm_config()
129 ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_config()
130 if (pc->data->supports_lock) { in rockchip_pwm_config()
132 writel_relaxed(ctrl, pc->base + pc->data->regs.ctrl); in rockchip_pwm_config()
135 writel(period, pc->base + pc->data->regs.period); in rockchip_pwm_config()
136 writel(duty, pc->base + pc->data->regs.duty); in rockchip_pwm_config()
138 if (pc->data->supports_polarity) { in rockchip_pwm_config()
140 if (state->polarity == PWM_POLARITY_INVERSED) in rockchip_pwm_config()
151 if (pc->data->supports_lock) in rockchip_pwm_config()
154 writel(ctrl, pc->base + pc->data->regs.ctrl); in rockchip_pwm_config()
158 struct pwm_device *pwm, in rockchip_pwm_enable() argument
162 u32 enable_conf = pc->data->enable_conf; in rockchip_pwm_enable()
167 ret = clk_enable(pc->clk); in rockchip_pwm_enable()
172 val = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_enable()
179 writel_relaxed(val, pc->base + pc->data->regs.ctrl); in rockchip_pwm_enable()
182 clk_disable(pc->clk); in rockchip_pwm_enable()
187 static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in rockchip_pwm_apply() argument
195 ret = clk_enable(pc->pclk); in rockchip_pwm_apply()
199 ret = clk_enable(pc->clk); in rockchip_pwm_apply()
203 pwm_get_state(pwm, &curstate); in rockchip_pwm_apply()
206 if (state->polarity != curstate.polarity && enabled && in rockchip_pwm_apply()
207 !pc->data->supports_lock) { in rockchip_pwm_apply()
208 ret = rockchip_pwm_enable(chip, pwm, false); in rockchip_pwm_apply()
214 rockchip_pwm_config(chip, pwm, state); in rockchip_pwm_apply()
215 if (state->enabled != enabled) { in rockchip_pwm_apply()
216 ret = rockchip_pwm_enable(chip, pwm, state->enabled); in rockchip_pwm_apply()
222 clk_disable(pc->clk); in rockchip_pwm_apply()
223 clk_disable(pc->pclk); in rockchip_pwm_apply()
290 { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
291 { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
292 { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
293 { .compatible = "rockchip,rk3328-pwm", .data = &pwm_data_v3},
306 id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev); in rockchip_pwm_probe()
308 return -EINVAL; in rockchip_pwm_probe()
310 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); in rockchip_pwm_probe()
312 return -ENOMEM; in rockchip_pwm_probe()
314 pc->base = devm_platform_ioremap_resource(pdev, 0); in rockchip_pwm_probe()
315 if (IS_ERR(pc->base)) in rockchip_pwm_probe()
316 return PTR_ERR(pc->base); in rockchip_pwm_probe()
318 pc->clk = devm_clk_get(&pdev->dev, "pwm"); in rockchip_pwm_probe()
319 if (IS_ERR(pc->clk)) { in rockchip_pwm_probe()
320 pc->clk = devm_clk_get(&pdev->dev, NULL); in rockchip_pwm_probe()
321 if (IS_ERR(pc->clk)) in rockchip_pwm_probe()
322 return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk), in rockchip_pwm_probe()
323 "Can't get PWM clk\n"); in rockchip_pwm_probe()
326 count = of_count_phandle_with_args(pdev->dev.of_node, in rockchip_pwm_probe()
327 "clocks", "#clock-cells"); in rockchip_pwm_probe()
329 pc->pclk = devm_clk_get(&pdev->dev, "pclk"); in rockchip_pwm_probe()
331 pc->pclk = pc->clk; in rockchip_pwm_probe()
333 if (IS_ERR(pc->pclk)) in rockchip_pwm_probe()
334 return dev_err_probe(&pdev->dev, PTR_ERR(pc->pclk), "Can't get APB clk\n"); in rockchip_pwm_probe()
336 ret = clk_prepare_enable(pc->clk); in rockchip_pwm_probe()
338 return dev_err_probe(&pdev->dev, ret, "Can't prepare enable PWM clk\n"); in rockchip_pwm_probe()
340 ret = clk_prepare_enable(pc->pclk); in rockchip_pwm_probe()
342 dev_err_probe(&pdev->dev, ret, "Can't prepare enable APB clk\n"); in rockchip_pwm_probe()
348 pc->data = id->data; in rockchip_pwm_probe()
349 pc->chip.dev = &pdev->dev; in rockchip_pwm_probe()
350 pc->chip.ops = &rockchip_pwm_ops; in rockchip_pwm_probe()
351 pc->chip.npwm = 1; in rockchip_pwm_probe()
353 enable_conf = pc->data->enable_conf; in rockchip_pwm_probe()
354 ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl); in rockchip_pwm_probe()
357 ret = pwmchip_add(&pc->chip); in rockchip_pwm_probe()
359 dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n"); in rockchip_pwm_probe()
363 /* Keep the PWM clk enabled if the PWM appears to be up and running. */ in rockchip_pwm_probe()
365 clk_disable(pc->clk); in rockchip_pwm_probe()
367 clk_disable(pc->pclk); in rockchip_pwm_probe()
372 clk_disable_unprepare(pc->pclk); in rockchip_pwm_probe()
374 clk_disable_unprepare(pc->clk); in rockchip_pwm_probe()
383 pwmchip_remove(&pc->chip); in rockchip_pwm_remove()
385 clk_unprepare(pc->pclk); in rockchip_pwm_remove()
386 clk_unprepare(pc->clk); in rockchip_pwm_remove()
391 .name = "rockchip-pwm",
400 MODULE_DESCRIPTION("Rockchip SoC PWM driver");