xref: /openbmc/linux/drivers/pwm/pwm-rockchip.c (revision 84ea61f6)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2101353c8SBeniamino Galvani /*
3101353c8SBeniamino Galvani  * PWM driver for Rockchip SoCs
4101353c8SBeniamino Galvani  *
5101353c8SBeniamino Galvani  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
6f6306299SCaesar Wang  * Copyright (C) 2014 ROCKCHIP, Inc.
7101353c8SBeniamino Galvani  */
8101353c8SBeniamino Galvani 
9101353c8SBeniamino Galvani #include <linux/clk.h>
10101353c8SBeniamino Galvani #include <linux/io.h>
11101353c8SBeniamino Galvani #include <linux/module.h>
12101353c8SBeniamino Galvani #include <linux/of.h>
13f6306299SCaesar Wang #include <linux/of_device.h>
14101353c8SBeniamino Galvani #include <linux/platform_device.h>
15101353c8SBeniamino Galvani #include <linux/pwm.h>
16101353c8SBeniamino Galvani #include <linux/time.h>
17101353c8SBeniamino Galvani 
18101353c8SBeniamino Galvani #define PWM_CTRL_TIMER_EN	(1 << 0)
19101353c8SBeniamino Galvani #define PWM_CTRL_OUTPUT_EN	(1 << 3)
20101353c8SBeniamino Galvani 
21f6306299SCaesar Wang #define PWM_ENABLE		(1 << 0)
22f6306299SCaesar Wang #define PWM_CONTINUOUS		(1 << 1)
23f6306299SCaesar Wang #define PWM_DUTY_POSITIVE	(1 << 3)
247264354cSDoug Anderson #define PWM_DUTY_NEGATIVE	(0 << 3)
25f6306299SCaesar Wang #define PWM_INACTIVE_NEGATIVE	(0 << 4)
267264354cSDoug Anderson #define PWM_INACTIVE_POSITIVE	(1 << 4)
27bc834d7bSDavid Wu #define PWM_POLARITY_MASK	(PWM_DUTY_POSITIVE | PWM_INACTIVE_POSITIVE)
28f6306299SCaesar Wang #define PWM_OUTPUT_LEFT		(0 << 5)
293f9a3631SDavid Wu #define PWM_LOCK_EN		(1 << 6)
30f6306299SCaesar Wang #define PWM_LP_DISABLE		(0 << 8)
31101353c8SBeniamino Galvani 
32101353c8SBeniamino Galvani struct rockchip_pwm_chip {
33101353c8SBeniamino Galvani 	struct pwm_chip chip;
34101353c8SBeniamino Galvani 	struct clk *clk;
3527922ff5SDavid Wu 	struct clk *pclk;
36f6306299SCaesar Wang 	const struct rockchip_pwm_data *data;
37101353c8SBeniamino Galvani 	void __iomem *base;
38101353c8SBeniamino Galvani };
39101353c8SBeniamino Galvani 
40f6306299SCaesar Wang struct rockchip_pwm_regs {
41f6306299SCaesar Wang 	unsigned long duty;
42f6306299SCaesar Wang 	unsigned long period;
43f6306299SCaesar Wang 	unsigned long cntr;
44f6306299SCaesar Wang 	unsigned long ctrl;
45f6306299SCaesar Wang };
46f6306299SCaesar Wang 
47f6306299SCaesar Wang struct rockchip_pwm_data {
48f6306299SCaesar Wang 	struct rockchip_pwm_regs regs;
49f6306299SCaesar Wang 	unsigned int prescaler;
502bf1c98aSBoris Brezillon 	bool supports_polarity;
513f9a3631SDavid Wu 	bool supports_lock;
52831b2790SDavid Wu 	u32 enable_conf;
53f6306299SCaesar Wang };
54f6306299SCaesar Wang 
55101353c8SBeniamino Galvani static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
56101353c8SBeniamino Galvani {
57101353c8SBeniamino Galvani 	return container_of(c, struct rockchip_pwm_chip, chip);
58101353c8SBeniamino Galvani }
59101353c8SBeniamino Galvani 
601ebb74cfSBoris Brezillon static void rockchip_pwm_get_state(struct pwm_chip *chip,
611ebb74cfSBoris Brezillon 				   struct pwm_device *pwm,
621ebb74cfSBoris Brezillon 				   struct pwm_state *state)
631ebb74cfSBoris Brezillon {
641ebb74cfSBoris Brezillon 	struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
65831b2790SDavid Wu 	u32 enable_conf = pc->data->enable_conf;
661ebb74cfSBoris Brezillon 	unsigned long clk_rate;
671ebb74cfSBoris Brezillon 	u64 tmp;
68831b2790SDavid Wu 	u32 val;
691ebb74cfSBoris Brezillon 	int ret;
701ebb74cfSBoris Brezillon 
7127922ff5SDavid Wu 	ret = clk_enable(pc->pclk);
721ebb74cfSBoris Brezillon 	if (ret)
731ebb74cfSBoris Brezillon 		return;
741ebb74cfSBoris Brezillon 
7511be938aSSimon South 	ret = clk_enable(pc->clk);
7611be938aSSimon South 	if (ret)
7711be938aSSimon South 		return;
7811be938aSSimon South 
791ebb74cfSBoris Brezillon 	clk_rate = clk_get_rate(pc->clk);
801ebb74cfSBoris Brezillon 
811ebb74cfSBoris Brezillon 	tmp = readl_relaxed(pc->base + pc->data->regs.period);
821ebb74cfSBoris Brezillon 	tmp *= pc->data->prescaler * NSEC_PER_SEC;
831ebb74cfSBoris Brezillon 	state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
841ebb74cfSBoris Brezillon 
851ebb74cfSBoris Brezillon 	tmp = readl_relaxed(pc->base + pc->data->regs.duty);
861ebb74cfSBoris Brezillon 	tmp *= pc->data->prescaler * NSEC_PER_SEC;
871ebb74cfSBoris Brezillon 	state->duty_cycle =  DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
881ebb74cfSBoris Brezillon 
89831b2790SDavid Wu 	val = readl_relaxed(pc->base + pc->data->regs.ctrl);
90cad0f296SRasmus Villemoes 	state->enabled = (val & enable_conf) == enable_conf;
91831b2790SDavid Wu 
92ba73deb1SUwe Kleine-König 	if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE))
93831b2790SDavid Wu 		state->polarity = PWM_POLARITY_INVERSED;
94ba73deb1SUwe Kleine-König 	else
95ba73deb1SUwe Kleine-König 		state->polarity = PWM_POLARITY_NORMAL;
961ebb74cfSBoris Brezillon 
9711be938aSSimon South 	clk_disable(pc->clk);
9827922ff5SDavid Wu 	clk_disable(pc->pclk);
991ebb74cfSBoris Brezillon }
1001ebb74cfSBoris Brezillon 
101f90df9cdSDavid Wu static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
10271523d18SUwe Kleine-König 			       const struct pwm_state *state)
103101353c8SBeniamino Galvani {
104101353c8SBeniamino Galvani 	struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
105101353c8SBeniamino Galvani 	unsigned long period, duty;
106101353c8SBeniamino Galvani 	u64 clk_rate, div;
107bc834d7bSDavid Wu 	u32 ctrl;
108101353c8SBeniamino Galvani 
109101353c8SBeniamino Galvani 	clk_rate = clk_get_rate(pc->clk);
110101353c8SBeniamino Galvani 
111101353c8SBeniamino Galvani 	/*
112101353c8SBeniamino Galvani 	 * Since period and duty cycle registers have a width of 32
113101353c8SBeniamino Galvani 	 * bits, every possible input period can be obtained using the
114101353c8SBeniamino Galvani 	 * default prescaler value for all practical clock rate values.
115101353c8SBeniamino Galvani 	 */
116bc834d7bSDavid Wu 	div = clk_rate * state->period;
11712f9ce4aSBoris Brezillon 	period = DIV_ROUND_CLOSEST_ULL(div,
11812f9ce4aSBoris Brezillon 				       pc->data->prescaler * NSEC_PER_SEC);
119101353c8SBeniamino Galvani 
120bc834d7bSDavid Wu 	div = clk_rate * state->duty_cycle;
12112f9ce4aSBoris Brezillon 	duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
122101353c8SBeniamino Galvani 
1233f9a3631SDavid Wu 	/*
1243f9a3631SDavid Wu 	 * Lock the period and duty of previous configuration, then
1253f9a3631SDavid Wu 	 * change the duty and period, that would not be effective.
1263f9a3631SDavid Wu 	 */
1273f9a3631SDavid Wu 	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
1283f9a3631SDavid Wu 	if (pc->data->supports_lock) {
1293f9a3631SDavid Wu 		ctrl |= PWM_LOCK_EN;
1303f9a3631SDavid Wu 		writel_relaxed(ctrl, pc->base + pc->data->regs.ctrl);
1313f9a3631SDavid Wu 	}
1323f9a3631SDavid Wu 
133f6306299SCaesar Wang 	writel(period, pc->base + pc->data->regs.period);
134f6306299SCaesar Wang 	writel(duty, pc->base + pc->data->regs.duty);
135bc834d7bSDavid Wu 
136bc834d7bSDavid Wu 	if (pc->data->supports_polarity) {
137bc834d7bSDavid Wu 		ctrl &= ~PWM_POLARITY_MASK;
138bc834d7bSDavid Wu 		if (state->polarity == PWM_POLARITY_INVERSED)
139bc834d7bSDavid Wu 			ctrl |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
140bc834d7bSDavid Wu 		else
141bc834d7bSDavid Wu 			ctrl |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
142bc834d7bSDavid Wu 	}
1433f9a3631SDavid Wu 
1443f9a3631SDavid Wu 	/*
1453f9a3631SDavid Wu 	 * Unlock and set polarity at the same time,
1463f9a3631SDavid Wu 	 * the configuration of duty, period and polarity
1473f9a3631SDavid Wu 	 * would be effective together at next period.
1483f9a3631SDavid Wu 	 */
1493f9a3631SDavid Wu 	if (pc->data->supports_lock)
1503f9a3631SDavid Wu 		ctrl &= ~PWM_LOCK_EN;
1513f9a3631SDavid Wu 
152bc834d7bSDavid Wu 	writel(ctrl, pc->base + pc->data->regs.ctrl);
153101353c8SBeniamino Galvani }
154101353c8SBeniamino Galvani 
155a900152bSDavid Wu static int rockchip_pwm_enable(struct pwm_chip *chip,
156a900152bSDavid Wu 			       struct pwm_device *pwm,
157831b2790SDavid Wu 			       bool enable)
158a900152bSDavid Wu {
159a900152bSDavid Wu 	struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
160831b2790SDavid Wu 	u32 enable_conf = pc->data->enable_conf;
161a900152bSDavid Wu 	int ret;
162ed054693SDavid Wu 	u32 val;
163a900152bSDavid Wu 
164a900152bSDavid Wu 	if (enable) {
165a900152bSDavid Wu 		ret = clk_enable(pc->clk);
166a900152bSDavid Wu 		if (ret)
167a900152bSDavid Wu 			return ret;
168a900152bSDavid Wu 	}
169a900152bSDavid Wu 
170ed054693SDavid Wu 	val = readl_relaxed(pc->base + pc->data->regs.ctrl);
171ed054693SDavid Wu 
172ed054693SDavid Wu 	if (enable)
173ed054693SDavid Wu 		val |= enable_conf;
174ed054693SDavid Wu 	else
175ed054693SDavid Wu 		val &= ~enable_conf;
176ed054693SDavid Wu 
177ed054693SDavid Wu 	writel_relaxed(val, pc->base + pc->data->regs.ctrl);
178a900152bSDavid Wu 
179a900152bSDavid Wu 	if (!enable)
180a900152bSDavid Wu 		clk_disable(pc->clk);
181a900152bSDavid Wu 
182a900152bSDavid Wu 	return 0;
183a900152bSDavid Wu }
184a900152bSDavid Wu 
185ed054693SDavid Wu static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
18671523d18SUwe Kleine-König 			      const struct pwm_state *state)
187ed054693SDavid Wu {
188ed054693SDavid Wu 	struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
189831b2790SDavid Wu 	struct pwm_state curstate;
190831b2790SDavid Wu 	bool enabled;
191831b2790SDavid Wu 	int ret = 0;
192ed054693SDavid Wu 
193ed054693SDavid Wu 	ret = clk_enable(pc->pclk);
194ed054693SDavid Wu 	if (ret)
195ed054693SDavid Wu 		return ret;
196ed054693SDavid Wu 
19711be938aSSimon South 	ret = clk_enable(pc->clk);
19811be938aSSimon South 	if (ret)
19911be938aSSimon South 		return ret;
20011be938aSSimon South 
201831b2790SDavid Wu 	pwm_get_state(pwm, &curstate);
202831b2790SDavid Wu 	enabled = curstate.enabled;
203831b2790SDavid Wu 
2043f9a3631SDavid Wu 	if (state->polarity != curstate.polarity && enabled &&
2053f9a3631SDavid Wu 	    !pc->data->supports_lock) {
206831b2790SDavid Wu 		ret = rockchip_pwm_enable(chip, pwm, false);
207a900152bSDavid Wu 		if (ret)
208a900152bSDavid Wu 			goto out;
209831b2790SDavid Wu 		enabled = false;
210831b2790SDavid Wu 	}
211831b2790SDavid Wu 
212831b2790SDavid Wu 	rockchip_pwm_config(chip, pwm, state);
213831b2790SDavid Wu 	if (state->enabled != enabled) {
214831b2790SDavid Wu 		ret = rockchip_pwm_enable(chip, pwm, state->enabled);
215831b2790SDavid Wu 		if (ret)
216831b2790SDavid Wu 			goto out;
217831b2790SDavid Wu 	}
2182bf1c98aSBoris Brezillon 
2192bf1c98aSBoris Brezillon out:
22011be938aSSimon South 	clk_disable(pc->clk);
22127922ff5SDavid Wu 	clk_disable(pc->pclk);
2222bf1c98aSBoris Brezillon 
2232bf1c98aSBoris Brezillon 	return ret;
224101353c8SBeniamino Galvani }
225101353c8SBeniamino Galvani 
226831b2790SDavid Wu static const struct pwm_ops rockchip_pwm_ops = {
2271ebb74cfSBoris Brezillon 	.get_state = rockchip_pwm_get_state,
2282bf1c98aSBoris Brezillon 	.apply = rockchip_pwm_apply,
2297264354cSDoug Anderson 	.owner = THIS_MODULE,
2307264354cSDoug Anderson };
2317264354cSDoug Anderson 
232f6306299SCaesar Wang static const struct rockchip_pwm_data pwm_data_v1 = {
233f6306299SCaesar Wang 	.regs = {
234f6306299SCaesar Wang 		.duty = 0x04,
235f6306299SCaesar Wang 		.period = 0x08,
236f6306299SCaesar Wang 		.cntr = 0x00,
237f6306299SCaesar Wang 		.ctrl = 0x0c,
238f6306299SCaesar Wang 	},
239f6306299SCaesar Wang 	.prescaler = 2,
240831b2790SDavid Wu 	.supports_polarity = false,
2413f9a3631SDavid Wu 	.supports_lock = false,
242831b2790SDavid Wu 	.enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN,
243f6306299SCaesar Wang };
244f6306299SCaesar Wang 
245f6306299SCaesar Wang static const struct rockchip_pwm_data pwm_data_v2 = {
246f6306299SCaesar Wang 	.regs = {
247f6306299SCaesar Wang 		.duty = 0x08,
248f6306299SCaesar Wang 		.period = 0x04,
249f6306299SCaesar Wang 		.cntr = 0x00,
250f6306299SCaesar Wang 		.ctrl = 0x0c,
251f6306299SCaesar Wang 	},
252f6306299SCaesar Wang 	.prescaler = 1,
2532bf1c98aSBoris Brezillon 	.supports_polarity = true,
2543f9a3631SDavid Wu 	.supports_lock = false,
255831b2790SDavid Wu 	.enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
256831b2790SDavid Wu 		       PWM_CONTINUOUS,
257f6306299SCaesar Wang };
258f6306299SCaesar Wang 
259f6306299SCaesar Wang static const struct rockchip_pwm_data pwm_data_vop = {
260f6306299SCaesar Wang 	.regs = {
261f6306299SCaesar Wang 		.duty = 0x08,
262f6306299SCaesar Wang 		.period = 0x04,
263f6306299SCaesar Wang 		.cntr = 0x0c,
264f6306299SCaesar Wang 		.ctrl = 0x00,
265f6306299SCaesar Wang 	},
266f6306299SCaesar Wang 	.prescaler = 1,
2672bf1c98aSBoris Brezillon 	.supports_polarity = true,
2683f9a3631SDavid Wu 	.supports_lock = false,
2693f9a3631SDavid Wu 	.enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
2703f9a3631SDavid Wu 		       PWM_CONTINUOUS,
2713f9a3631SDavid Wu };
2723f9a3631SDavid Wu 
2733f9a3631SDavid Wu static const struct rockchip_pwm_data pwm_data_v3 = {
2743f9a3631SDavid Wu 	.regs = {
2753f9a3631SDavid Wu 		.duty = 0x08,
2763f9a3631SDavid Wu 		.period = 0x04,
2773f9a3631SDavid Wu 		.cntr = 0x00,
2783f9a3631SDavid Wu 		.ctrl = 0x0c,
2793f9a3631SDavid Wu 	},
2803f9a3631SDavid Wu 	.prescaler = 1,
2813f9a3631SDavid Wu 	.supports_polarity = true,
2823f9a3631SDavid Wu 	.supports_lock = true,
283831b2790SDavid Wu 	.enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
284831b2790SDavid Wu 		       PWM_CONTINUOUS,
285f6306299SCaesar Wang };
286f6306299SCaesar Wang 
287f6306299SCaesar Wang static const struct of_device_id rockchip_pwm_dt_ids[] = {
288f6306299SCaesar Wang 	{ .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
289f6306299SCaesar Wang 	{ .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
290f6306299SCaesar Wang 	{ .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
2913f9a3631SDavid Wu 	{ .compatible = "rockchip,rk3328-pwm", .data = &pwm_data_v3},
292f6306299SCaesar Wang 	{ /* sentinel */ }
293f6306299SCaesar Wang };
294f6306299SCaesar Wang MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
295f6306299SCaesar Wang 
296101353c8SBeniamino Galvani static int rockchip_pwm_probe(struct platform_device *pdev)
297101353c8SBeniamino Galvani {
298f6306299SCaesar Wang 	const struct of_device_id *id;
299101353c8SBeniamino Galvani 	struct rockchip_pwm_chip *pc;
300457f74abSSimon South 	u32 enable_conf, ctrl;
301d21ba5d6SSimon South 	bool enabled;
30227922ff5SDavid Wu 	int ret, count;
303101353c8SBeniamino Galvani 
304f6306299SCaesar Wang 	id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
305f6306299SCaesar Wang 	if (!id)
306f6306299SCaesar Wang 		return -EINVAL;
307f6306299SCaesar Wang 
308101353c8SBeniamino Galvani 	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
309101353c8SBeniamino Galvani 	if (!pc)
310101353c8SBeniamino Galvani 		return -ENOMEM;
311101353c8SBeniamino Galvani 
3125119ee9eSYangtao Li 	pc->base = devm_platform_ioremap_resource(pdev, 0);
313101353c8SBeniamino Galvani 	if (IS_ERR(pc->base))
314101353c8SBeniamino Galvani 		return PTR_ERR(pc->base);
315101353c8SBeniamino Galvani 
31627922ff5SDavid Wu 	pc->clk = devm_clk_get(&pdev->dev, "pwm");
31727922ff5SDavid Wu 	if (IS_ERR(pc->clk)) {
318101353c8SBeniamino Galvani 		pc->clk = devm_clk_get(&pdev->dev, NULL);
319836719f8SKrzysztof Kozlowski 		if (IS_ERR(pc->clk))
320836719f8SKrzysztof Kozlowski 			return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
321c9f809d0SSimon South 					     "Can't get PWM clk\n");
32227922ff5SDavid Wu 	}
32327922ff5SDavid Wu 
32427922ff5SDavid Wu 	count = of_count_phandle_with_args(pdev->dev.of_node,
32527922ff5SDavid Wu 					   "clocks", "#clock-cells");
32627922ff5SDavid Wu 	if (count == 2)
32727922ff5SDavid Wu 		pc->pclk = devm_clk_get(&pdev->dev, "pclk");
32827922ff5SDavid Wu 	else
32927922ff5SDavid Wu 		pc->pclk = pc->clk;
33027922ff5SDavid Wu 
33127922ff5SDavid Wu 	if (IS_ERR(pc->pclk)) {
33227922ff5SDavid Wu 		ret = PTR_ERR(pc->pclk);
33327922ff5SDavid Wu 		if (ret != -EPROBE_DEFER)
33427922ff5SDavid Wu 			dev_err(&pdev->dev, "Can't get APB clk: %d\n", ret);
33527922ff5SDavid Wu 		return ret;
33627922ff5SDavid Wu 	}
337101353c8SBeniamino Galvani 
33848cf973cSBoris Brezillon 	ret = clk_prepare_enable(pc->clk);
33927922ff5SDavid Wu 	if (ret) {
340c9f809d0SSimon South 		dev_err(&pdev->dev, "Can't prepare enable PWM clk: %d\n", ret);
341101353c8SBeniamino Galvani 		return ret;
34227922ff5SDavid Wu 	}
34327922ff5SDavid Wu 
344d9b657a5SSimon South 	ret = clk_prepare_enable(pc->pclk);
34527922ff5SDavid Wu 	if (ret) {
346d9b657a5SSimon South 		dev_err(&pdev->dev, "Can't prepare enable APB clk: %d\n", ret);
34727922ff5SDavid Wu 		goto err_clk;
34827922ff5SDavid Wu 	}
349101353c8SBeniamino Galvani 
350101353c8SBeniamino Galvani 	platform_set_drvdata(pdev, pc);
351101353c8SBeniamino Galvani 
352f6306299SCaesar Wang 	pc->data = id->data;
353101353c8SBeniamino Galvani 	pc->chip.dev = &pdev->dev;
354831b2790SDavid Wu 	pc->chip.ops = &rockchip_pwm_ops;
355101353c8SBeniamino Galvani 	pc->chip.npwm = 1;
356101353c8SBeniamino Galvani 
357d21ba5d6SSimon South 	enable_conf = pc->data->enable_conf;
358d21ba5d6SSimon South 	ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
359d21ba5d6SSimon South 	enabled = (ctrl & enable_conf) == enable_conf;
360d21ba5d6SSimon South 
361101353c8SBeniamino Galvani 	ret = pwmchip_add(&pc->chip);
362101353c8SBeniamino Galvani 	if (ret < 0) {
363101353c8SBeniamino Galvani 		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
36427922ff5SDavid Wu 		goto err_pclk;
365101353c8SBeniamino Galvani 	}
366101353c8SBeniamino Galvani 
36748cf973cSBoris Brezillon 	/* Keep the PWM clk enabled if the PWM appears to be up and running. */
368d21ba5d6SSimon South 	if (!enabled)
36948cf973cSBoris Brezillon 		clk_disable(pc->clk);
37048cf973cSBoris Brezillon 
371d9b657a5SSimon South 	clk_disable(pc->pclk);
372d9b657a5SSimon South 
37327922ff5SDavid Wu 	return 0;
37427922ff5SDavid Wu 
37527922ff5SDavid Wu err_pclk:
376d9b657a5SSimon South 	clk_disable_unprepare(pc->pclk);
37727922ff5SDavid Wu err_clk:
37827922ff5SDavid Wu 	clk_disable_unprepare(pc->clk);
37927922ff5SDavid Wu 
380101353c8SBeniamino Galvani 	return ret;
381101353c8SBeniamino Galvani }
382101353c8SBeniamino Galvani 
383101353c8SBeniamino Galvani static int rockchip_pwm_remove(struct platform_device *pdev)
384101353c8SBeniamino Galvani {
385101353c8SBeniamino Galvani 	struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
386101353c8SBeniamino Galvani 
387*84ea61f6SUwe Kleine-König 	pwmchip_remove(&pc->chip);
388*84ea61f6SUwe Kleine-König 
38927922ff5SDavid Wu 	clk_unprepare(pc->pclk);
390101353c8SBeniamino Galvani 	clk_unprepare(pc->clk);
391101353c8SBeniamino Galvani 
392*84ea61f6SUwe Kleine-König 	return 0;
393101353c8SBeniamino Galvani }
394101353c8SBeniamino Galvani 
395101353c8SBeniamino Galvani static struct platform_driver rockchip_pwm_driver = {
396101353c8SBeniamino Galvani 	.driver = {
397101353c8SBeniamino Galvani 		.name = "rockchip-pwm",
398101353c8SBeniamino Galvani 		.of_match_table = rockchip_pwm_dt_ids,
399101353c8SBeniamino Galvani 	},
400101353c8SBeniamino Galvani 	.probe = rockchip_pwm_probe,
401101353c8SBeniamino Galvani 	.remove = rockchip_pwm_remove,
402101353c8SBeniamino Galvani };
403101353c8SBeniamino Galvani module_platform_driver(rockchip_pwm_driver);
404101353c8SBeniamino Galvani 
405101353c8SBeniamino Galvani MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
406101353c8SBeniamino Galvani MODULE_DESCRIPTION("Rockchip SoC PWM driver");
407101353c8SBeniamino Galvani MODULE_LICENSE("GPL v2");
408