xref: /openbmc/linux/drivers/pwm/pwm-sunplus.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b3c4af85SHammer Hsieh // SPDX-License-Identifier: GPL-2.0
2b3c4af85SHammer Hsieh /*
3b3c4af85SHammer Hsieh  * PWM device driver for SUNPLUS SP7021 SoC
4b3c4af85SHammer Hsieh  *
5b3c4af85SHammer Hsieh  * Links:
6b3c4af85SHammer Hsieh  *   Reference Manual:
7b3c4af85SHammer Hsieh  *   https://sunplus-tibbo.atlassian.net/wiki/spaces/doc/overview
8b3c4af85SHammer Hsieh  *
9b3c4af85SHammer Hsieh  *   Reference Manual(PWM module):
10b3c4af85SHammer Hsieh  *   https://sunplus.atlassian.net/wiki/spaces/doc/pages/461144198/12.+Pulse+Width+Modulation+PWM
11b3c4af85SHammer Hsieh  *
12b3c4af85SHammer Hsieh  * Limitations:
13b3c4af85SHammer Hsieh  * - Only supports normal polarity.
14b3c4af85SHammer Hsieh  * - It output low when PWM channel disabled.
15b3c4af85SHammer Hsieh  * - When the parameters change, current running period will not be completed
16b3c4af85SHammer Hsieh  *     and run new settings immediately.
17b3c4af85SHammer Hsieh  * - In .apply() PWM output need to write register FREQ and DUTY. When first write FREQ
18b3c4af85SHammer Hsieh  *     done and not yet write DUTY, it has short timing gap use new FREQ and old DUTY.
19b3c4af85SHammer Hsieh  *
20b3c4af85SHammer Hsieh  * Author: Hammer Hsieh <hammerh0314@gmail.com>
21b3c4af85SHammer Hsieh  */
22b3c4af85SHammer Hsieh #include <linux/bitfield.h>
23b3c4af85SHammer Hsieh #include <linux/clk.h>
24b3c4af85SHammer Hsieh #include <linux/io.h>
25b3c4af85SHammer Hsieh #include <linux/kernel.h>
26*0a41b0c5SRob Herring #include <linux/mod_devicetable.h>
27b3c4af85SHammer Hsieh #include <linux/module.h>
28b3c4af85SHammer Hsieh #include <linux/platform_device.h>
29b3c4af85SHammer Hsieh #include <linux/pwm.h>
30b3c4af85SHammer Hsieh 
31b3c4af85SHammer Hsieh #define SP7021_PWM_MODE0		0x000
32b3c4af85SHammer Hsieh #define SP7021_PWM_MODE0_PWMEN(ch)	BIT(ch)
33b3c4af85SHammer Hsieh #define SP7021_PWM_MODE0_BYPASS(ch)	BIT(8 + (ch))
34b3c4af85SHammer Hsieh #define SP7021_PWM_MODE1		0x004
35b3c4af85SHammer Hsieh #define SP7021_PWM_MODE1_CNT_EN(ch)	BIT(ch)
36b3c4af85SHammer Hsieh #define SP7021_PWM_FREQ(ch)		(0x008 + 4 * (ch))
37b3c4af85SHammer Hsieh #define SP7021_PWM_FREQ_MAX		GENMASK(15, 0)
38b3c4af85SHammer Hsieh #define SP7021_PWM_DUTY(ch)		(0x018 + 4 * (ch))
39b3c4af85SHammer Hsieh #define SP7021_PWM_DUTY_DD_SEL(ch)	FIELD_PREP(GENMASK(9, 8), ch)
40b3c4af85SHammer Hsieh #define SP7021_PWM_DUTY_MAX		GENMASK(7, 0)
41b3c4af85SHammer Hsieh #define SP7021_PWM_DUTY_MASK		SP7021_PWM_DUTY_MAX
42b3c4af85SHammer Hsieh #define SP7021_PWM_FREQ_SCALER		256
43b3c4af85SHammer Hsieh #define SP7021_PWM_NUM			4
44b3c4af85SHammer Hsieh 
45b3c4af85SHammer Hsieh struct sunplus_pwm {
46b3c4af85SHammer Hsieh 	struct pwm_chip chip;
47b3c4af85SHammer Hsieh 	void __iomem *base;
48b3c4af85SHammer Hsieh 	struct clk *clk;
49b3c4af85SHammer Hsieh };
50b3c4af85SHammer Hsieh 
to_sunplus_pwm(struct pwm_chip * chip)51b3c4af85SHammer Hsieh static inline struct sunplus_pwm *to_sunplus_pwm(struct pwm_chip *chip)
52b3c4af85SHammer Hsieh {
53b3c4af85SHammer Hsieh 	return container_of(chip, struct sunplus_pwm, chip);
54b3c4af85SHammer Hsieh }
55b3c4af85SHammer Hsieh 
sunplus_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)56b3c4af85SHammer Hsieh static int sunplus_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
57b3c4af85SHammer Hsieh 			     const struct pwm_state *state)
58b3c4af85SHammer Hsieh {
59b3c4af85SHammer Hsieh 	struct sunplus_pwm *priv = to_sunplus_pwm(chip);
60b3c4af85SHammer Hsieh 	u32 dd_freq, duty, mode0, mode1;
61b3c4af85SHammer Hsieh 	u64 clk_rate;
62b3c4af85SHammer Hsieh 
63b3c4af85SHammer Hsieh 	if (state->polarity != pwm->state.polarity)
64b3c4af85SHammer Hsieh 		return -EINVAL;
65b3c4af85SHammer Hsieh 
66b3c4af85SHammer Hsieh 	if (!state->enabled) {
67b3c4af85SHammer Hsieh 		/* disable pwm channel output */
68b3c4af85SHammer Hsieh 		mode0 = readl(priv->base + SP7021_PWM_MODE0);
69b3c4af85SHammer Hsieh 		mode0 &= ~SP7021_PWM_MODE0_PWMEN(pwm->hwpwm);
70b3c4af85SHammer Hsieh 		writel(mode0, priv->base + SP7021_PWM_MODE0);
71b3c4af85SHammer Hsieh 		/* disable pwm channel clk source */
72b3c4af85SHammer Hsieh 		mode1 = readl(priv->base + SP7021_PWM_MODE1);
73b3c4af85SHammer Hsieh 		mode1 &= ~SP7021_PWM_MODE1_CNT_EN(pwm->hwpwm);
74b3c4af85SHammer Hsieh 		writel(mode1, priv->base + SP7021_PWM_MODE1);
75b3c4af85SHammer Hsieh 		return 0;
76b3c4af85SHammer Hsieh 	}
77b3c4af85SHammer Hsieh 
78b3c4af85SHammer Hsieh 	clk_rate = clk_get_rate(priv->clk);
79b3c4af85SHammer Hsieh 
80b3c4af85SHammer Hsieh 	/*
81b3c4af85SHammer Hsieh 	 * The following calculations might overflow if clk is bigger
82b3c4af85SHammer Hsieh 	 * than 256 GHz. In practise it's 202.5MHz, so this limitation
83b3c4af85SHammer Hsieh 	 * is only theoretic.
84b3c4af85SHammer Hsieh 	 */
85b3c4af85SHammer Hsieh 	if (clk_rate > (u64)SP7021_PWM_FREQ_SCALER * NSEC_PER_SEC)
86b3c4af85SHammer Hsieh 		return -EINVAL;
87b3c4af85SHammer Hsieh 
88b3c4af85SHammer Hsieh 	/*
89b3c4af85SHammer Hsieh 	 * With clk_rate limited above we have dd_freq <= state->period,
90b3c4af85SHammer Hsieh 	 * so this cannot overflow.
91b3c4af85SHammer Hsieh 	 */
92b3c4af85SHammer Hsieh 	dd_freq = mul_u64_u64_div_u64(clk_rate, state->period, (u64)SP7021_PWM_FREQ_SCALER
93b3c4af85SHammer Hsieh 				* NSEC_PER_SEC);
94b3c4af85SHammer Hsieh 
95b3c4af85SHammer Hsieh 	if (dd_freq == 0)
96b3c4af85SHammer Hsieh 		return -EINVAL;
97b3c4af85SHammer Hsieh 
98b3c4af85SHammer Hsieh 	if (dd_freq > SP7021_PWM_FREQ_MAX)
99b3c4af85SHammer Hsieh 		dd_freq = SP7021_PWM_FREQ_MAX;
100b3c4af85SHammer Hsieh 
101b3c4af85SHammer Hsieh 	writel(dd_freq, priv->base + SP7021_PWM_FREQ(pwm->hwpwm));
102b3c4af85SHammer Hsieh 
103b3c4af85SHammer Hsieh 	/* cal and set pwm duty */
104b3c4af85SHammer Hsieh 	mode0 = readl(priv->base + SP7021_PWM_MODE0);
105b3c4af85SHammer Hsieh 	mode0 |= SP7021_PWM_MODE0_PWMEN(pwm->hwpwm);
106b3c4af85SHammer Hsieh 	mode1 = readl(priv->base + SP7021_PWM_MODE1);
107b3c4af85SHammer Hsieh 	mode1 |= SP7021_PWM_MODE1_CNT_EN(pwm->hwpwm);
108b3c4af85SHammer Hsieh 	if (state->duty_cycle == state->period) {
109b3c4af85SHammer Hsieh 		/* PWM channel output = high */
110b3c4af85SHammer Hsieh 		mode0 |= SP7021_PWM_MODE0_BYPASS(pwm->hwpwm);
111b3c4af85SHammer Hsieh 		duty = SP7021_PWM_DUTY_DD_SEL(pwm->hwpwm) | SP7021_PWM_DUTY_MAX;
112b3c4af85SHammer Hsieh 	} else {
113b3c4af85SHammer Hsieh 		mode0 &= ~SP7021_PWM_MODE0_BYPASS(pwm->hwpwm);
114b3c4af85SHammer Hsieh 		/*
115b3c4af85SHammer Hsieh 		 * duty_ns <= period_ns 27 bits, clk_rate 28 bits, won't overflow.
116b3c4af85SHammer Hsieh 		 */
117b3c4af85SHammer Hsieh 		duty = mul_u64_u64_div_u64(state->duty_cycle, clk_rate,
118b3c4af85SHammer Hsieh 					   (u64)dd_freq * NSEC_PER_SEC);
119b3c4af85SHammer Hsieh 		duty = SP7021_PWM_DUTY_DD_SEL(pwm->hwpwm) | duty;
120b3c4af85SHammer Hsieh 	}
121b3c4af85SHammer Hsieh 	writel(duty, priv->base + SP7021_PWM_DUTY(pwm->hwpwm));
122b3c4af85SHammer Hsieh 	writel(mode1, priv->base + SP7021_PWM_MODE1);
123b3c4af85SHammer Hsieh 	writel(mode0, priv->base + SP7021_PWM_MODE0);
124b3c4af85SHammer Hsieh 
125b3c4af85SHammer Hsieh 	return 0;
126b3c4af85SHammer Hsieh }
127b3c4af85SHammer Hsieh 
sunplus_pwm_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)1286c452cffSUwe Kleine-König static int sunplus_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
129b3c4af85SHammer Hsieh 				 struct pwm_state *state)
130b3c4af85SHammer Hsieh {
131b3c4af85SHammer Hsieh 	struct sunplus_pwm *priv = to_sunplus_pwm(chip);
132b3c4af85SHammer Hsieh 	u32 mode0, dd_freq, duty;
133b3c4af85SHammer Hsieh 	u64 clk_rate;
134b3c4af85SHammer Hsieh 
135b3c4af85SHammer Hsieh 	mode0 = readl(priv->base + SP7021_PWM_MODE0);
136b3c4af85SHammer Hsieh 
137b3c4af85SHammer Hsieh 	if (mode0 & BIT(pwm->hwpwm)) {
138b3c4af85SHammer Hsieh 		clk_rate = clk_get_rate(priv->clk);
139b3c4af85SHammer Hsieh 		dd_freq = readl(priv->base + SP7021_PWM_FREQ(pwm->hwpwm));
140b3c4af85SHammer Hsieh 		duty = readl(priv->base + SP7021_PWM_DUTY(pwm->hwpwm));
141b3c4af85SHammer Hsieh 		duty = FIELD_GET(SP7021_PWM_DUTY_MASK, duty);
142b3c4af85SHammer Hsieh 		/*
143b3c4af85SHammer Hsieh 		 * dd_freq 16 bits, SP7021_PWM_FREQ_SCALER 8 bits
144b3c4af85SHammer Hsieh 		 * NSEC_PER_SEC 30 bits, won't overflow.
145b3c4af85SHammer Hsieh 		 */
146b3c4af85SHammer Hsieh 		state->period = DIV64_U64_ROUND_UP((u64)dd_freq * (u64)SP7021_PWM_FREQ_SCALER
147b3c4af85SHammer Hsieh 						* NSEC_PER_SEC, clk_rate);
148b3c4af85SHammer Hsieh 		/*
149b3c4af85SHammer Hsieh 		 * dd_freq 16 bits, duty 8 bits, NSEC_PER_SEC 30 bits, won't overflow.
150b3c4af85SHammer Hsieh 		 */
151b3c4af85SHammer Hsieh 		state->duty_cycle = DIV64_U64_ROUND_UP((u64)dd_freq * (u64)duty * NSEC_PER_SEC,
152b3c4af85SHammer Hsieh 						       clk_rate);
153b3c4af85SHammer Hsieh 		state->enabled = true;
154b3c4af85SHammer Hsieh 	} else {
155b3c4af85SHammer Hsieh 		state->enabled = false;
156b3c4af85SHammer Hsieh 	}
157b3c4af85SHammer Hsieh 
158b3c4af85SHammer Hsieh 	state->polarity = PWM_POLARITY_NORMAL;
1596c452cffSUwe Kleine-König 
1606c452cffSUwe Kleine-König 	return 0;
161b3c4af85SHammer Hsieh }
162b3c4af85SHammer Hsieh 
163b3c4af85SHammer Hsieh static const struct pwm_ops sunplus_pwm_ops = {
164b3c4af85SHammer Hsieh 	.apply = sunplus_pwm_apply,
165b3c4af85SHammer Hsieh 	.get_state = sunplus_pwm_get_state,
166b3c4af85SHammer Hsieh 	.owner = THIS_MODULE,
167b3c4af85SHammer Hsieh };
168b3c4af85SHammer Hsieh 
sunplus_pwm_clk_release(void * data)169b3c4af85SHammer Hsieh static void sunplus_pwm_clk_release(void *data)
170b3c4af85SHammer Hsieh {
171b3c4af85SHammer Hsieh 	struct clk *clk = data;
172b3c4af85SHammer Hsieh 
173b3c4af85SHammer Hsieh 	clk_disable_unprepare(clk);
174b3c4af85SHammer Hsieh }
175b3c4af85SHammer Hsieh 
sunplus_pwm_probe(struct platform_device * pdev)176b3c4af85SHammer Hsieh static int sunplus_pwm_probe(struct platform_device *pdev)
177b3c4af85SHammer Hsieh {
178b3c4af85SHammer Hsieh 	struct device *dev = &pdev->dev;
179b3c4af85SHammer Hsieh 	struct sunplus_pwm *priv;
180b3c4af85SHammer Hsieh 	int ret;
181b3c4af85SHammer Hsieh 
182b3c4af85SHammer Hsieh 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
183b3c4af85SHammer Hsieh 	if (!priv)
184b3c4af85SHammer Hsieh 		return -ENOMEM;
185b3c4af85SHammer Hsieh 
186b3c4af85SHammer Hsieh 	priv->base = devm_platform_ioremap_resource(pdev, 0);
187b3c4af85SHammer Hsieh 	if (IS_ERR(priv->base))
188b3c4af85SHammer Hsieh 		return PTR_ERR(priv->base);
189b3c4af85SHammer Hsieh 
190b3c4af85SHammer Hsieh 	priv->clk = devm_clk_get(dev, NULL);
191b3c4af85SHammer Hsieh 	if (IS_ERR(priv->clk))
192b3c4af85SHammer Hsieh 		return dev_err_probe(dev, PTR_ERR(priv->clk),
193b3c4af85SHammer Hsieh 				     "get pwm clock failed\n");
194b3c4af85SHammer Hsieh 
195b3c4af85SHammer Hsieh 	ret = clk_prepare_enable(priv->clk);
196b3c4af85SHammer Hsieh 	if (ret < 0) {
197b3c4af85SHammer Hsieh 		dev_err(dev, "failed to enable clock: %d\n", ret);
198b3c4af85SHammer Hsieh 		return ret;
199b3c4af85SHammer Hsieh 	}
200b3c4af85SHammer Hsieh 
201b3c4af85SHammer Hsieh 	ret = devm_add_action_or_reset(dev, sunplus_pwm_clk_release, priv->clk);
202b3c4af85SHammer Hsieh 	if (ret < 0) {
203b3c4af85SHammer Hsieh 		dev_err(dev, "failed to release clock: %d\n", ret);
204b3c4af85SHammer Hsieh 		return ret;
205b3c4af85SHammer Hsieh 	}
206b3c4af85SHammer Hsieh 
207b3c4af85SHammer Hsieh 	priv->chip.dev = dev;
208b3c4af85SHammer Hsieh 	priv->chip.ops = &sunplus_pwm_ops;
209b3c4af85SHammer Hsieh 	priv->chip.npwm = SP7021_PWM_NUM;
210b3c4af85SHammer Hsieh 
211b3c4af85SHammer Hsieh 	ret = devm_pwmchip_add(dev, &priv->chip);
212b3c4af85SHammer Hsieh 	if (ret < 0)
213b3c4af85SHammer Hsieh 		return dev_err_probe(dev, ret, "Cannot register sunplus PWM\n");
214b3c4af85SHammer Hsieh 
215b3c4af85SHammer Hsieh 	return 0;
216b3c4af85SHammer Hsieh }
217b3c4af85SHammer Hsieh 
218b3c4af85SHammer Hsieh static const struct of_device_id sunplus_pwm_of_match[] = {
219b3c4af85SHammer Hsieh 	{ .compatible = "sunplus,sp7021-pwm", },
220b3c4af85SHammer Hsieh 	{}
221b3c4af85SHammer Hsieh };
222b3c4af85SHammer Hsieh MODULE_DEVICE_TABLE(of, sunplus_pwm_of_match);
223b3c4af85SHammer Hsieh 
224b3c4af85SHammer Hsieh static struct platform_driver sunplus_pwm_driver = {
225b3c4af85SHammer Hsieh 	.probe		= sunplus_pwm_probe,
226b3c4af85SHammer Hsieh 	.driver		= {
227b3c4af85SHammer Hsieh 		.name	= "sunplus-pwm",
228b3c4af85SHammer Hsieh 		.of_match_table = sunplus_pwm_of_match,
229b3c4af85SHammer Hsieh 	},
230b3c4af85SHammer Hsieh };
231b3c4af85SHammer Hsieh module_platform_driver(sunplus_pwm_driver);
232b3c4af85SHammer Hsieh 
233b3c4af85SHammer Hsieh MODULE_DESCRIPTION("Sunplus SoC PWM Driver");
234b3c4af85SHammer Hsieh MODULE_AUTHOR("Hammer Hsieh <hammerh0314@gmail.com>");
235b3c4af85SHammer Hsieh MODULE_LICENSE("GPL");
236