xref: /OK3568_Linux_fs/kernel/drivers/pwm/pwm-sl28cpld.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * sl28cpld PWM driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2020 Michael Walle <michael@walle.cc>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * There is no public datasheet available for this PWM core. But it is easy
8*4882a593Smuzhiyun  * enough to be briefly explained. It consists of one 8-bit counter. The PWM
9*4882a593Smuzhiyun  * supports four distinct frequencies by selecting when to reset the counter.
10*4882a593Smuzhiyun  * With the prescaler setting you can select which bit of the counter is used
11*4882a593Smuzhiyun  * to reset it. This implies that the higher the frequency the less remaining
12*4882a593Smuzhiyun  * bits are available for the actual counter.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Let cnt[7:0] be the counter, clocked at 32kHz:
15*4882a593Smuzhiyun  * +-----------+--------+--------------+-----------+---------------+
16*4882a593Smuzhiyun  * | prescaler |  reset | counter bits | frequency | period length |
17*4882a593Smuzhiyun  * +-----------+--------+--------------+-----------+---------------+
18*4882a593Smuzhiyun  * |         0 | cnt[7] |     cnt[6:0] |    250 Hz |    4000000 ns |
19*4882a593Smuzhiyun  * |         1 | cnt[6] |     cnt[5:0] |    500 Hz |    2000000 ns |
20*4882a593Smuzhiyun  * |         2 | cnt[5] |     cnt[4:0] |     1 kHz |    1000000 ns |
21*4882a593Smuzhiyun  * |         3 | cnt[4] |     cnt[3:0] |     2 kHz |     500000 ns |
22*4882a593Smuzhiyun  * +-----------+--------+--------------+-----------+---------------+
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * Limitations:
25*4882a593Smuzhiyun  * - The hardware cannot generate a 100% duty cycle if the prescaler is 0.
26*4882a593Smuzhiyun  * - The hardware cannot atomically set the prescaler and the counter value,
27*4882a593Smuzhiyun  *   which might lead to glitches and inconsistent states if a write fails.
28*4882a593Smuzhiyun  * - The counter is not reset if you switch the prescaler which leads
29*4882a593Smuzhiyun  *   to glitches, too.
30*4882a593Smuzhiyun  * - The duty cycle will switch immediately and not after a complete cycle.
31*4882a593Smuzhiyun  * - Depending on the actual implementation, disabling the PWM might have
32*4882a593Smuzhiyun  *   side effects. For example, if the output pin is shared with a GPIO pin
33*4882a593Smuzhiyun  *   it will automatically switch back to GPIO mode.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <linux/bitfield.h>
37*4882a593Smuzhiyun #include <linux/kernel.h>
38*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
39*4882a593Smuzhiyun #include <linux/module.h>
40*4882a593Smuzhiyun #include <linux/platform_device.h>
41*4882a593Smuzhiyun #include <linux/pwm.h>
42*4882a593Smuzhiyun #include <linux/regmap.h>
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * PWM timer block registers.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun #define SL28CPLD_PWM_CTRL			0x00
48*4882a593Smuzhiyun #define   SL28CPLD_PWM_CTRL_ENABLE		BIT(7)
49*4882a593Smuzhiyun #define   SL28CPLD_PWM_CTRL_PRESCALER_MASK	GENMASK(1, 0)
50*4882a593Smuzhiyun #define SL28CPLD_PWM_CYCLE			0x01
51*4882a593Smuzhiyun #define   SL28CPLD_PWM_CYCLE_MAX		GENMASK(6, 0)
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define SL28CPLD_PWM_CLK			32000 /* 32 kHz */
54*4882a593Smuzhiyun #define SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler)	(1 << (7 - (prescaler)))
55*4882a593Smuzhiyun #define SL28CPLD_PWM_PERIOD(prescaler) \
56*4882a593Smuzhiyun 	(NSEC_PER_SEC / SL28CPLD_PWM_CLK * SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler))
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun  * We calculate the duty cycle like this:
60*4882a593Smuzhiyun  *   duty_cycle_ns = pwm_cycle_reg * max_period_ns / max_duty_cycle
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * With
63*4882a593Smuzhiyun  *   max_period_ns = 1 << (7 - prescaler) / SL28CPLD_PWM_CLK * NSEC_PER_SEC
64*4882a593Smuzhiyun  *   max_duty_cycle = 1 << (7 - prescaler)
65*4882a593Smuzhiyun  * this then simplifies to:
66*4882a593Smuzhiyun  *   duty_cycle_ns = pwm_cycle_reg / SL28CPLD_PWM_CLK * NSEC_PER_SEC
67*4882a593Smuzhiyun  *                 = NSEC_PER_SEC / SL28CPLD_PWM_CLK * pwm_cycle_reg
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * NSEC_PER_SEC is a multiple of SL28CPLD_PWM_CLK, therefore we're not losing
70*4882a593Smuzhiyun  * precision by doing the divison first.
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun #define SL28CPLD_PWM_TO_DUTY_CYCLE(reg) \
73*4882a593Smuzhiyun 	(NSEC_PER_SEC / SL28CPLD_PWM_CLK * (reg))
74*4882a593Smuzhiyun #define SL28CPLD_PWM_FROM_DUTY_CYCLE(duty_cycle) \
75*4882a593Smuzhiyun 	(DIV_ROUND_DOWN_ULL((duty_cycle), NSEC_PER_SEC / SL28CPLD_PWM_CLK))
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #define sl28cpld_pwm_read(priv, reg, val) \
78*4882a593Smuzhiyun 	regmap_read((priv)->regmap, (priv)->offset + (reg), (val))
79*4882a593Smuzhiyun #define sl28cpld_pwm_write(priv, reg, val) \
80*4882a593Smuzhiyun 	regmap_write((priv)->regmap, (priv)->offset + (reg), (val))
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct sl28cpld_pwm {
83*4882a593Smuzhiyun 	struct pwm_chip pwm_chip;
84*4882a593Smuzhiyun 	struct regmap *regmap;
85*4882a593Smuzhiyun 	u32 offset;
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun #define sl28cpld_pwm_from_chip(_chip) \
88*4882a593Smuzhiyun 	container_of(_chip, struct sl28cpld_pwm, pwm_chip)
89*4882a593Smuzhiyun 
sl28cpld_pwm_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)90*4882a593Smuzhiyun static void sl28cpld_pwm_get_state(struct pwm_chip *chip,
91*4882a593Smuzhiyun 				   struct pwm_device *pwm,
92*4882a593Smuzhiyun 				   struct pwm_state *state)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
95*4882a593Smuzhiyun 	unsigned int reg;
96*4882a593Smuzhiyun 	int prescaler;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	sl28cpld_pwm_read(priv, SL28CPLD_PWM_CTRL, &reg);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	state->enabled = reg & SL28CPLD_PWM_CTRL_ENABLE;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	prescaler = FIELD_GET(SL28CPLD_PWM_CTRL_PRESCALER_MASK, reg);
103*4882a593Smuzhiyun 	state->period = SL28CPLD_PWM_PERIOD(prescaler);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	sl28cpld_pwm_read(priv, SL28CPLD_PWM_CYCLE, &reg);
106*4882a593Smuzhiyun 	state->duty_cycle = SL28CPLD_PWM_TO_DUTY_CYCLE(reg);
107*4882a593Smuzhiyun 	state->polarity = PWM_POLARITY_NORMAL;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/*
110*4882a593Smuzhiyun 	 * Sanitize values for the PWM core. Depending on the prescaler it
111*4882a593Smuzhiyun 	 * might happen that we calculate a duty_cycle greater than the actual
112*4882a593Smuzhiyun 	 * period. This might happen if someone (e.g. the bootloader) sets an
113*4882a593Smuzhiyun 	 * invalid combination of values. The behavior of the hardware is
114*4882a593Smuzhiyun 	 * undefined in this case. But we need to report sane values back to
115*4882a593Smuzhiyun 	 * the PWM core.
116*4882a593Smuzhiyun 	 */
117*4882a593Smuzhiyun 	state->duty_cycle = min(state->duty_cycle, state->period);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
sl28cpld_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)120*4882a593Smuzhiyun static int sl28cpld_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
121*4882a593Smuzhiyun 			      const struct pwm_state *state)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
124*4882a593Smuzhiyun 	unsigned int cycle, prescaler;
125*4882a593Smuzhiyun 	bool write_duty_cycle_first;
126*4882a593Smuzhiyun 	int ret;
127*4882a593Smuzhiyun 	u8 ctrl;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	/* Polarity inversion is not supported */
130*4882a593Smuzhiyun 	if (state->polarity != PWM_POLARITY_NORMAL)
131*4882a593Smuzhiyun 		return -EINVAL;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	/*
134*4882a593Smuzhiyun 	 * Calculate the prescaler. Pick the biggest period that isn't
135*4882a593Smuzhiyun 	 * bigger than the requested period.
136*4882a593Smuzhiyun 	 */
137*4882a593Smuzhiyun 	prescaler = DIV_ROUND_UP_ULL(SL28CPLD_PWM_PERIOD(0), state->period);
138*4882a593Smuzhiyun 	prescaler = order_base_2(prescaler);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (prescaler > field_max(SL28CPLD_PWM_CTRL_PRESCALER_MASK))
141*4882a593Smuzhiyun 		return -ERANGE;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	ctrl = FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, prescaler);
144*4882a593Smuzhiyun 	if (state->enabled)
145*4882a593Smuzhiyun 		ctrl |= SL28CPLD_PWM_CTRL_ENABLE;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	cycle = SL28CPLD_PWM_FROM_DUTY_CYCLE(state->duty_cycle);
148*4882a593Smuzhiyun 	cycle = min_t(unsigned int, cycle, SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler));
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/*
151*4882a593Smuzhiyun 	 * Work around the hardware limitation. See also above. Trap 100% duty
152*4882a593Smuzhiyun 	 * cycle if the prescaler is 0. Set prescaler to 1 instead. We don't
153*4882a593Smuzhiyun 	 * care about the frequency because its "all-one" in either case.
154*4882a593Smuzhiyun 	 *
155*4882a593Smuzhiyun 	 * We don't need to check the actual prescaler setting, because only
156*4882a593Smuzhiyun 	 * if the prescaler is 0 we can have this particular value.
157*4882a593Smuzhiyun 	 */
158*4882a593Smuzhiyun 	if (cycle == SL28CPLD_PWM_MAX_DUTY_CYCLE(0)) {
159*4882a593Smuzhiyun 		ctrl &= ~SL28CPLD_PWM_CTRL_PRESCALER_MASK;
160*4882a593Smuzhiyun 		ctrl |= FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, 1);
161*4882a593Smuzhiyun 		cycle = SL28CPLD_PWM_MAX_DUTY_CYCLE(1);
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/*
165*4882a593Smuzhiyun 	 * To avoid glitches when we switch the prescaler, we have to make sure
166*4882a593Smuzhiyun 	 * we have a valid duty cycle for the new mode.
167*4882a593Smuzhiyun 	 *
168*4882a593Smuzhiyun 	 * Take the current prescaler (or the current period length) into
169*4882a593Smuzhiyun 	 * account to decide whether we have to write the duty cycle or the new
170*4882a593Smuzhiyun 	 * prescaler first. If the period length is decreasing we have to
171*4882a593Smuzhiyun 	 * write the duty cycle first.
172*4882a593Smuzhiyun 	 */
173*4882a593Smuzhiyun 	write_duty_cycle_first = pwm->state.period > state->period;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (write_duty_cycle_first) {
176*4882a593Smuzhiyun 		ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
177*4882a593Smuzhiyun 		if (ret)
178*4882a593Smuzhiyun 			return ret;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CTRL, ctrl);
182*4882a593Smuzhiyun 	if (ret)
183*4882a593Smuzhiyun 		return ret;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (!write_duty_cycle_first) {
186*4882a593Smuzhiyun 		ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
187*4882a593Smuzhiyun 		if (ret)
188*4882a593Smuzhiyun 			return ret;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	return 0;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun static const struct pwm_ops sl28cpld_pwm_ops = {
195*4882a593Smuzhiyun 	.apply = sl28cpld_pwm_apply,
196*4882a593Smuzhiyun 	.get_state = sl28cpld_pwm_get_state,
197*4882a593Smuzhiyun 	.owner = THIS_MODULE,
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun 
sl28cpld_pwm_probe(struct platform_device * pdev)200*4882a593Smuzhiyun static int sl28cpld_pwm_probe(struct platform_device *pdev)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct sl28cpld_pwm *priv;
203*4882a593Smuzhiyun 	struct pwm_chip *chip;
204*4882a593Smuzhiyun 	int ret;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	if (!pdev->dev.parent) {
207*4882a593Smuzhiyun 		dev_err(&pdev->dev, "no parent device\n");
208*4882a593Smuzhiyun 		return -ENODEV;
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
212*4882a593Smuzhiyun 	if (!priv)
213*4882a593Smuzhiyun 		return -ENOMEM;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
216*4882a593Smuzhiyun 	if (!priv->regmap) {
217*4882a593Smuzhiyun 		dev_err(&pdev->dev, "could not get parent regmap\n");
218*4882a593Smuzhiyun 		return -ENODEV;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	ret = device_property_read_u32(&pdev->dev, "reg", &priv->offset);
222*4882a593Smuzhiyun 	if (ret) {
223*4882a593Smuzhiyun 		dev_err(&pdev->dev, "no 'reg' property found (%pe)\n",
224*4882a593Smuzhiyun 			ERR_PTR(ret));
225*4882a593Smuzhiyun 		return -EINVAL;
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	/* Initialize the pwm_chip structure */
229*4882a593Smuzhiyun 	chip = &priv->pwm_chip;
230*4882a593Smuzhiyun 	chip->dev = &pdev->dev;
231*4882a593Smuzhiyun 	chip->ops = &sl28cpld_pwm_ops;
232*4882a593Smuzhiyun 	chip->base = -1;
233*4882a593Smuzhiyun 	chip->npwm = 1;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	ret = pwmchip_add(&priv->pwm_chip);
236*4882a593Smuzhiyun 	if (ret) {
237*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to add PWM chip (%pe)",
238*4882a593Smuzhiyun 			ERR_PTR(ret));
239*4882a593Smuzhiyun 		return ret;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	platform_set_drvdata(pdev, priv);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
sl28cpld_pwm_remove(struct platform_device * pdev)247*4882a593Smuzhiyun static int sl28cpld_pwm_remove(struct platform_device *pdev)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	struct sl28cpld_pwm *priv = platform_get_drvdata(pdev);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return pwmchip_remove(&priv->pwm_chip);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun static const struct of_device_id sl28cpld_pwm_of_match[] = {
255*4882a593Smuzhiyun 	{ .compatible = "kontron,sl28cpld-pwm" },
256*4882a593Smuzhiyun 	{}
257*4882a593Smuzhiyun };
258*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, sl28cpld_pwm_of_match);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun static struct platform_driver sl28cpld_pwm_driver = {
261*4882a593Smuzhiyun 	.probe = sl28cpld_pwm_probe,
262*4882a593Smuzhiyun 	.remove	= sl28cpld_pwm_remove,
263*4882a593Smuzhiyun 	.driver = {
264*4882a593Smuzhiyun 		.name = "sl28cpld-pwm",
265*4882a593Smuzhiyun 		.of_match_table = sl28cpld_pwm_of_match,
266*4882a593Smuzhiyun 	},
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun module_platform_driver(sl28cpld_pwm_driver);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun MODULE_DESCRIPTION("sl28cpld PWM Driver");
271*4882a593Smuzhiyun MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
272*4882a593Smuzhiyun MODULE_LICENSE("GPL");
273