xref: /openbmc/linux/drivers/pwm/pwm-sl28cpld.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
19db33d22SMichael Walle // SPDX-License-Identifier: GPL-2.0-only
29db33d22SMichael Walle /*
39db33d22SMichael Walle  * sl28cpld PWM driver
49db33d22SMichael Walle  *
59db33d22SMichael Walle  * Copyright (c) 2020 Michael Walle <michael@walle.cc>
69db33d22SMichael Walle  *
79db33d22SMichael Walle  * There is no public datasheet available for this PWM core. But it is easy
89db33d22SMichael Walle  * enough to be briefly explained. It consists of one 8-bit counter. The PWM
99db33d22SMichael Walle  * supports four distinct frequencies by selecting when to reset the counter.
109db33d22SMichael Walle  * With the prescaler setting you can select which bit of the counter is used
119db33d22SMichael Walle  * to reset it. This implies that the higher the frequency the less remaining
129db33d22SMichael Walle  * bits are available for the actual counter.
139db33d22SMichael Walle  *
149db33d22SMichael Walle  * Let cnt[7:0] be the counter, clocked at 32kHz:
159db33d22SMichael Walle  * +-----------+--------+--------------+-----------+---------------+
169db33d22SMichael Walle  * | prescaler |  reset | counter bits | frequency | period length |
179db33d22SMichael Walle  * +-----------+--------+--------------+-----------+---------------+
189db33d22SMichael Walle  * |         0 | cnt[7] |     cnt[6:0] |    250 Hz |    4000000 ns |
199db33d22SMichael Walle  * |         1 | cnt[6] |     cnt[5:0] |    500 Hz |    2000000 ns |
209db33d22SMichael Walle  * |         2 | cnt[5] |     cnt[4:0] |     1 kHz |    1000000 ns |
219db33d22SMichael Walle  * |         3 | cnt[4] |     cnt[3:0] |     2 kHz |     500000 ns |
229db33d22SMichael Walle  * +-----------+--------+--------------+-----------+---------------+
239db33d22SMichael Walle  *
249db33d22SMichael Walle  * Limitations:
259db33d22SMichael Walle  * - The hardware cannot generate a 100% duty cycle if the prescaler is 0.
269db33d22SMichael Walle  * - The hardware cannot atomically set the prescaler and the counter value,
279db33d22SMichael Walle  *   which might lead to glitches and inconsistent states if a write fails.
289db33d22SMichael Walle  * - The counter is not reset if you switch the prescaler which leads
299db33d22SMichael Walle  *   to glitches, too.
309db33d22SMichael Walle  * - The duty cycle will switch immediately and not after a complete cycle.
319db33d22SMichael Walle  * - Depending on the actual implementation, disabling the PWM might have
329db33d22SMichael Walle  *   side effects. For example, if the output pin is shared with a GPIO pin
339db33d22SMichael Walle  *   it will automatically switch back to GPIO mode.
349db33d22SMichael Walle  */
359db33d22SMichael Walle 
369db33d22SMichael Walle #include <linux/bitfield.h>
379db33d22SMichael Walle #include <linux/kernel.h>
389db33d22SMichael Walle #include <linux/mod_devicetable.h>
399db33d22SMichael Walle #include <linux/module.h>
409db33d22SMichael Walle #include <linux/platform_device.h>
41*0a41b0c5SRob Herring #include <linux/property.h>
429db33d22SMichael Walle #include <linux/pwm.h>
439db33d22SMichael Walle #include <linux/regmap.h>
449db33d22SMichael Walle 
459db33d22SMichael Walle /*
469db33d22SMichael Walle  * PWM timer block registers.
479db33d22SMichael Walle  */
489db33d22SMichael Walle #define SL28CPLD_PWM_CTRL			0x00
499db33d22SMichael Walle #define   SL28CPLD_PWM_CTRL_ENABLE		BIT(7)
509db33d22SMichael Walle #define   SL28CPLD_PWM_CTRL_PRESCALER_MASK	GENMASK(1, 0)
519db33d22SMichael Walle #define SL28CPLD_PWM_CYCLE			0x01
529db33d22SMichael Walle #define   SL28CPLD_PWM_CYCLE_MAX		GENMASK(6, 0)
539db33d22SMichael Walle 
549db33d22SMichael Walle #define SL28CPLD_PWM_CLK			32000 /* 32 kHz */
559db33d22SMichael Walle #define SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler)	(1 << (7 - (prescaler)))
569db33d22SMichael Walle #define SL28CPLD_PWM_PERIOD(prescaler) \
579db33d22SMichael Walle 	(NSEC_PER_SEC / SL28CPLD_PWM_CLK * SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler))
589db33d22SMichael Walle 
599db33d22SMichael Walle /*
609db33d22SMichael Walle  * We calculate the duty cycle like this:
619db33d22SMichael Walle  *   duty_cycle_ns = pwm_cycle_reg * max_period_ns / max_duty_cycle
629db33d22SMichael Walle  *
639db33d22SMichael Walle  * With
649db33d22SMichael Walle  *   max_period_ns = 1 << (7 - prescaler) / SL28CPLD_PWM_CLK * NSEC_PER_SEC
659db33d22SMichael Walle  *   max_duty_cycle = 1 << (7 - prescaler)
669db33d22SMichael Walle  * this then simplifies to:
679db33d22SMichael Walle  *   duty_cycle_ns = pwm_cycle_reg / SL28CPLD_PWM_CLK * NSEC_PER_SEC
689db33d22SMichael Walle  *                 = NSEC_PER_SEC / SL28CPLD_PWM_CLK * pwm_cycle_reg
699db33d22SMichael Walle  *
709db33d22SMichael Walle  * NSEC_PER_SEC is a multiple of SL28CPLD_PWM_CLK, therefore we're not losing
719db33d22SMichael Walle  * precision by doing the divison first.
729db33d22SMichael Walle  */
739db33d22SMichael Walle #define SL28CPLD_PWM_TO_DUTY_CYCLE(reg) \
749db33d22SMichael Walle 	(NSEC_PER_SEC / SL28CPLD_PWM_CLK * (reg))
759db33d22SMichael Walle #define SL28CPLD_PWM_FROM_DUTY_CYCLE(duty_cycle) \
769db33d22SMichael Walle 	(DIV_ROUND_DOWN_ULL((duty_cycle), NSEC_PER_SEC / SL28CPLD_PWM_CLK))
779db33d22SMichael Walle 
789db33d22SMichael Walle #define sl28cpld_pwm_read(priv, reg, val) \
799db33d22SMichael Walle 	regmap_read((priv)->regmap, (priv)->offset + (reg), (val))
809db33d22SMichael Walle #define sl28cpld_pwm_write(priv, reg, val) \
819db33d22SMichael Walle 	regmap_write((priv)->regmap, (priv)->offset + (reg), (val))
829db33d22SMichael Walle 
839db33d22SMichael Walle struct sl28cpld_pwm {
84bc83fe5cSUwe Kleine-König 	struct pwm_chip chip;
859db33d22SMichael Walle 	struct regmap *regmap;
869db33d22SMichael Walle 	u32 offset;
879db33d22SMichael Walle };
88bc83fe5cSUwe Kleine-König 
sl28cpld_pwm_from_chip(struct pwm_chip * chip)89bc83fe5cSUwe Kleine-König static inline struct sl28cpld_pwm *sl28cpld_pwm_from_chip(struct pwm_chip *chip)
90bc83fe5cSUwe Kleine-König {
91bc83fe5cSUwe Kleine-König 	return container_of(chip, struct sl28cpld_pwm, chip);
92bc83fe5cSUwe Kleine-König }
939db33d22SMichael Walle 
sl28cpld_pwm_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)946c452cffSUwe Kleine-König static int sl28cpld_pwm_get_state(struct pwm_chip *chip,
959db33d22SMichael Walle 				  struct pwm_device *pwm,
969db33d22SMichael Walle 				  struct pwm_state *state)
979db33d22SMichael Walle {
98062c9cdfSUwe Kleine-König 	struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
999db33d22SMichael Walle 	unsigned int reg;
1009db33d22SMichael Walle 	int prescaler;
1019db33d22SMichael Walle 
1029db33d22SMichael Walle 	sl28cpld_pwm_read(priv, SL28CPLD_PWM_CTRL, &reg);
1039db33d22SMichael Walle 
1049db33d22SMichael Walle 	state->enabled = reg & SL28CPLD_PWM_CTRL_ENABLE;
1059db33d22SMichael Walle 
1069db33d22SMichael Walle 	prescaler = FIELD_GET(SL28CPLD_PWM_CTRL_PRESCALER_MASK, reg);
1079db33d22SMichael Walle 	state->period = SL28CPLD_PWM_PERIOD(prescaler);
1089db33d22SMichael Walle 
1099db33d22SMichael Walle 	sl28cpld_pwm_read(priv, SL28CPLD_PWM_CYCLE, &reg);
1109db33d22SMichael Walle 	state->duty_cycle = SL28CPLD_PWM_TO_DUTY_CYCLE(reg);
1119db33d22SMichael Walle 	state->polarity = PWM_POLARITY_NORMAL;
1129db33d22SMichael Walle 
1139db33d22SMichael Walle 	/*
1149db33d22SMichael Walle 	 * Sanitize values for the PWM core. Depending on the prescaler it
1159db33d22SMichael Walle 	 * might happen that we calculate a duty_cycle greater than the actual
1169db33d22SMichael Walle 	 * period. This might happen if someone (e.g. the bootloader) sets an
1179db33d22SMichael Walle 	 * invalid combination of values. The behavior of the hardware is
1189db33d22SMichael Walle 	 * undefined in this case. But we need to report sane values back to
1199db33d22SMichael Walle 	 * the PWM core.
1209db33d22SMichael Walle 	 */
1219db33d22SMichael Walle 	state->duty_cycle = min(state->duty_cycle, state->period);
1226c452cffSUwe Kleine-König 
1236c452cffSUwe Kleine-König 	return 0;
1249db33d22SMichael Walle }
1259db33d22SMichael Walle 
sl28cpld_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)1269db33d22SMichael Walle static int sl28cpld_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
1279db33d22SMichael Walle 			      const struct pwm_state *state)
1289db33d22SMichael Walle {
129062c9cdfSUwe Kleine-König 	struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
1309db33d22SMichael Walle 	unsigned int cycle, prescaler;
1319db33d22SMichael Walle 	bool write_duty_cycle_first;
1329db33d22SMichael Walle 	int ret;
1339db33d22SMichael Walle 	u8 ctrl;
1349db33d22SMichael Walle 
1359db33d22SMichael Walle 	/* Polarity inversion is not supported */
1369db33d22SMichael Walle 	if (state->polarity != PWM_POLARITY_NORMAL)
1379db33d22SMichael Walle 		return -EINVAL;
1389db33d22SMichael Walle 
1399db33d22SMichael Walle 	/*
1409db33d22SMichael Walle 	 * Calculate the prescaler. Pick the biggest period that isn't
1419db33d22SMichael Walle 	 * bigger than the requested period.
1429db33d22SMichael Walle 	 */
1439db33d22SMichael Walle 	prescaler = DIV_ROUND_UP_ULL(SL28CPLD_PWM_PERIOD(0), state->period);
1449db33d22SMichael Walle 	prescaler = order_base_2(prescaler);
1459db33d22SMichael Walle 
1469db33d22SMichael Walle 	if (prescaler > field_max(SL28CPLD_PWM_CTRL_PRESCALER_MASK))
1479db33d22SMichael Walle 		return -ERANGE;
1489db33d22SMichael Walle 
1499db33d22SMichael Walle 	ctrl = FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, prescaler);
1509db33d22SMichael Walle 	if (state->enabled)
1519db33d22SMichael Walle 		ctrl |= SL28CPLD_PWM_CTRL_ENABLE;
1529db33d22SMichael Walle 
1539db33d22SMichael Walle 	cycle = SL28CPLD_PWM_FROM_DUTY_CYCLE(state->duty_cycle);
1549db33d22SMichael Walle 	cycle = min_t(unsigned int, cycle, SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler));
1559db33d22SMichael Walle 
1569db33d22SMichael Walle 	/*
1579db33d22SMichael Walle 	 * Work around the hardware limitation. See also above. Trap 100% duty
1589db33d22SMichael Walle 	 * cycle if the prescaler is 0. Set prescaler to 1 instead. We don't
1599db33d22SMichael Walle 	 * care about the frequency because its "all-one" in either case.
1609db33d22SMichael Walle 	 *
1619db33d22SMichael Walle 	 * We don't need to check the actual prescaler setting, because only
1629db33d22SMichael Walle 	 * if the prescaler is 0 we can have this particular value.
1639db33d22SMichael Walle 	 */
1649db33d22SMichael Walle 	if (cycle == SL28CPLD_PWM_MAX_DUTY_CYCLE(0)) {
1659db33d22SMichael Walle 		ctrl &= ~SL28CPLD_PWM_CTRL_PRESCALER_MASK;
1669db33d22SMichael Walle 		ctrl |= FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, 1);
1679db33d22SMichael Walle 		cycle = SL28CPLD_PWM_MAX_DUTY_CYCLE(1);
1689db33d22SMichael Walle 	}
1699db33d22SMichael Walle 
1709db33d22SMichael Walle 	/*
1719db33d22SMichael Walle 	 * To avoid glitches when we switch the prescaler, we have to make sure
1729db33d22SMichael Walle 	 * we have a valid duty cycle for the new mode.
1739db33d22SMichael Walle 	 *
1749db33d22SMichael Walle 	 * Take the current prescaler (or the current period length) into
1759db33d22SMichael Walle 	 * account to decide whether we have to write the duty cycle or the new
1769db33d22SMichael Walle 	 * prescaler first. If the period length is decreasing we have to
1779db33d22SMichael Walle 	 * write the duty cycle first.
1789db33d22SMichael Walle 	 */
1799db33d22SMichael Walle 	write_duty_cycle_first = pwm->state.period > state->period;
1809db33d22SMichael Walle 
1819db33d22SMichael Walle 	if (write_duty_cycle_first) {
1829db33d22SMichael Walle 		ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
1839db33d22SMichael Walle 		if (ret)
1849db33d22SMichael Walle 			return ret;
1859db33d22SMichael Walle 	}
1869db33d22SMichael Walle 
1879db33d22SMichael Walle 	ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CTRL, ctrl);
1889db33d22SMichael Walle 	if (ret)
1899db33d22SMichael Walle 		return ret;
1909db33d22SMichael Walle 
1919db33d22SMichael Walle 	if (!write_duty_cycle_first) {
1929db33d22SMichael Walle 		ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
1939db33d22SMichael Walle 		if (ret)
1949db33d22SMichael Walle 			return ret;
1959db33d22SMichael Walle 	}
1969db33d22SMichael Walle 
1979db33d22SMichael Walle 	return 0;
1989db33d22SMichael Walle }
1999db33d22SMichael Walle 
2009db33d22SMichael Walle static const struct pwm_ops sl28cpld_pwm_ops = {
2019db33d22SMichael Walle 	.apply = sl28cpld_pwm_apply,
2029db33d22SMichael Walle 	.get_state = sl28cpld_pwm_get_state,
2039db33d22SMichael Walle 	.owner = THIS_MODULE,
2049db33d22SMichael Walle };
2059db33d22SMichael Walle 
sl28cpld_pwm_probe(struct platform_device * pdev)2069db33d22SMichael Walle static int sl28cpld_pwm_probe(struct platform_device *pdev)
2079db33d22SMichael Walle {
2089db33d22SMichael Walle 	struct sl28cpld_pwm *priv;
2099db33d22SMichael Walle 	struct pwm_chip *chip;
2109db33d22SMichael Walle 	int ret;
2119db33d22SMichael Walle 
2129db33d22SMichael Walle 	if (!pdev->dev.parent) {
2139db33d22SMichael Walle 		dev_err(&pdev->dev, "no parent device\n");
2149db33d22SMichael Walle 		return -ENODEV;
2159db33d22SMichael Walle 	}
2169db33d22SMichael Walle 
2179db33d22SMichael Walle 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2189db33d22SMichael Walle 	if (!priv)
2199db33d22SMichael Walle 		return -ENOMEM;
2209db33d22SMichael Walle 
2219db33d22SMichael Walle 	priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
2229db33d22SMichael Walle 	if (!priv->regmap) {
2239db33d22SMichael Walle 		dev_err(&pdev->dev, "could not get parent regmap\n");
2249db33d22SMichael Walle 		return -ENODEV;
2259db33d22SMichael Walle 	}
2269db33d22SMichael Walle 
2279db33d22SMichael Walle 	ret = device_property_read_u32(&pdev->dev, "reg", &priv->offset);
2289db33d22SMichael Walle 	if (ret) {
2299db33d22SMichael Walle 		dev_err(&pdev->dev, "no 'reg' property found (%pe)\n",
2309db33d22SMichael Walle 			ERR_PTR(ret));
2319db33d22SMichael Walle 		return -EINVAL;
2329db33d22SMichael Walle 	}
2339db33d22SMichael Walle 
2349db33d22SMichael Walle 	/* Initialize the pwm_chip structure */
235bc83fe5cSUwe Kleine-König 	chip = &priv->chip;
2369db33d22SMichael Walle 	chip->dev = &pdev->dev;
2379db33d22SMichael Walle 	chip->ops = &sl28cpld_pwm_ops;
2389db33d22SMichael Walle 	chip->npwm = 1;
2399db33d22SMichael Walle 
240bc83fe5cSUwe Kleine-König 	ret = devm_pwmchip_add(&pdev->dev, chip);
2419db33d22SMichael Walle 	if (ret) {
2429db33d22SMichael Walle 		dev_err(&pdev->dev, "failed to add PWM chip (%pe)",
2439db33d22SMichael Walle 			ERR_PTR(ret));
2449db33d22SMichael Walle 		return ret;
2459db33d22SMichael Walle 	}
2469db33d22SMichael Walle 
2479db33d22SMichael Walle 	return 0;
2489db33d22SMichael Walle }
2499db33d22SMichael Walle 
2509db33d22SMichael Walle static const struct of_device_id sl28cpld_pwm_of_match[] = {
2519db33d22SMichael Walle 	{ .compatible = "kontron,sl28cpld-pwm" },
2529db33d22SMichael Walle 	{}
2539db33d22SMichael Walle };
2549db33d22SMichael Walle MODULE_DEVICE_TABLE(of, sl28cpld_pwm_of_match);
2559db33d22SMichael Walle 
2569db33d22SMichael Walle static struct platform_driver sl28cpld_pwm_driver = {
2579db33d22SMichael Walle 	.probe = sl28cpld_pwm_probe,
2589db33d22SMichael Walle 	.driver = {
2599db33d22SMichael Walle 		.name = "sl28cpld-pwm",
2609db33d22SMichael Walle 		.of_match_table = sl28cpld_pwm_of_match,
2619db33d22SMichael Walle 	},
2629db33d22SMichael Walle };
2639db33d22SMichael Walle module_platform_driver(sl28cpld_pwm_driver);
2649db33d22SMichael Walle 
2659db33d22SMichael Walle MODULE_DESCRIPTION("sl28cpld PWM Driver");
2669db33d22SMichael Walle MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
2679db33d22SMichael Walle MODULE_LICENSE("GPL");
268