162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * sl28cpld PWM driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2020 Michael Walle <michael@walle.cc>
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * There is no public datasheet available for this PWM core. But it is easy
862306a36Sopenharmony_ci * enough to be briefly explained. It consists of one 8-bit counter. The PWM
962306a36Sopenharmony_ci * supports four distinct frequencies by selecting when to reset the counter.
1062306a36Sopenharmony_ci * With the prescaler setting you can select which bit of the counter is used
1162306a36Sopenharmony_ci * to reset it. This implies that the higher the frequency the less remaining
1262306a36Sopenharmony_ci * bits are available for the actual counter.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * Let cnt[7:0] be the counter, clocked at 32kHz:
1562306a36Sopenharmony_ci * +-----------+--------+--------------+-----------+---------------+
1662306a36Sopenharmony_ci * | prescaler |  reset | counter bits | frequency | period length |
1762306a36Sopenharmony_ci * +-----------+--------+--------------+-----------+---------------+
1862306a36Sopenharmony_ci * |         0 | cnt[7] |     cnt[6:0] |    250 Hz |    4000000 ns |
1962306a36Sopenharmony_ci * |         1 | cnt[6] |     cnt[5:0] |    500 Hz |    2000000 ns |
2062306a36Sopenharmony_ci * |         2 | cnt[5] |     cnt[4:0] |     1 kHz |    1000000 ns |
2162306a36Sopenharmony_ci * |         3 | cnt[4] |     cnt[3:0] |     2 kHz |     500000 ns |
2262306a36Sopenharmony_ci * +-----------+--------+--------------+-----------+---------------+
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * Limitations:
2562306a36Sopenharmony_ci * - The hardware cannot generate a 100% duty cycle if the prescaler is 0.
2662306a36Sopenharmony_ci * - The hardware cannot atomically set the prescaler and the counter value,
2762306a36Sopenharmony_ci *   which might lead to glitches and inconsistent states if a write fails.
2862306a36Sopenharmony_ci * - The counter is not reset if you switch the prescaler which leads
2962306a36Sopenharmony_ci *   to glitches, too.
3062306a36Sopenharmony_ci * - The duty cycle will switch immediately and not after a complete cycle.
3162306a36Sopenharmony_ci * - Depending on the actual implementation, disabling the PWM might have
3262306a36Sopenharmony_ci *   side effects. For example, if the output pin is shared with a GPIO pin
3362306a36Sopenharmony_ci *   it will automatically switch back to GPIO mode.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#include <linux/bitfield.h>
3762306a36Sopenharmony_ci#include <linux/kernel.h>
3862306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
3962306a36Sopenharmony_ci#include <linux/module.h>
4062306a36Sopenharmony_ci#include <linux/platform_device.h>
4162306a36Sopenharmony_ci#include <linux/property.h>
4262306a36Sopenharmony_ci#include <linux/pwm.h>
4362306a36Sopenharmony_ci#include <linux/regmap.h>
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * PWM timer block registers.
4762306a36Sopenharmony_ci */
4862306a36Sopenharmony_ci#define SL28CPLD_PWM_CTRL			0x00
4962306a36Sopenharmony_ci#define   SL28CPLD_PWM_CTRL_ENABLE		BIT(7)
5062306a36Sopenharmony_ci#define   SL28CPLD_PWM_CTRL_PRESCALER_MASK	GENMASK(1, 0)
5162306a36Sopenharmony_ci#define SL28CPLD_PWM_CYCLE			0x01
5262306a36Sopenharmony_ci#define   SL28CPLD_PWM_CYCLE_MAX		GENMASK(6, 0)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define SL28CPLD_PWM_CLK			32000 /* 32 kHz */
5562306a36Sopenharmony_ci#define SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler)	(1 << (7 - (prescaler)))
5662306a36Sopenharmony_ci#define SL28CPLD_PWM_PERIOD(prescaler) \
5762306a36Sopenharmony_ci	(NSEC_PER_SEC / SL28CPLD_PWM_CLK * SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler))
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * We calculate the duty cycle like this:
6162306a36Sopenharmony_ci *   duty_cycle_ns = pwm_cycle_reg * max_period_ns / max_duty_cycle
6262306a36Sopenharmony_ci *
6362306a36Sopenharmony_ci * With
6462306a36Sopenharmony_ci *   max_period_ns = 1 << (7 - prescaler) / SL28CPLD_PWM_CLK * NSEC_PER_SEC
6562306a36Sopenharmony_ci *   max_duty_cycle = 1 << (7 - prescaler)
6662306a36Sopenharmony_ci * this then simplifies to:
6762306a36Sopenharmony_ci *   duty_cycle_ns = pwm_cycle_reg / SL28CPLD_PWM_CLK * NSEC_PER_SEC
6862306a36Sopenharmony_ci *                 = NSEC_PER_SEC / SL28CPLD_PWM_CLK * pwm_cycle_reg
6962306a36Sopenharmony_ci *
7062306a36Sopenharmony_ci * NSEC_PER_SEC is a multiple of SL28CPLD_PWM_CLK, therefore we're not losing
7162306a36Sopenharmony_ci * precision by doing the divison first.
7262306a36Sopenharmony_ci */
7362306a36Sopenharmony_ci#define SL28CPLD_PWM_TO_DUTY_CYCLE(reg) \
7462306a36Sopenharmony_ci	(NSEC_PER_SEC / SL28CPLD_PWM_CLK * (reg))
7562306a36Sopenharmony_ci#define SL28CPLD_PWM_FROM_DUTY_CYCLE(duty_cycle) \
7662306a36Sopenharmony_ci	(DIV_ROUND_DOWN_ULL((duty_cycle), NSEC_PER_SEC / SL28CPLD_PWM_CLK))
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define sl28cpld_pwm_read(priv, reg, val) \
7962306a36Sopenharmony_ci	regmap_read((priv)->regmap, (priv)->offset + (reg), (val))
8062306a36Sopenharmony_ci#define sl28cpld_pwm_write(priv, reg, val) \
8162306a36Sopenharmony_ci	regmap_write((priv)->regmap, (priv)->offset + (reg), (val))
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistruct sl28cpld_pwm {
8462306a36Sopenharmony_ci	struct pwm_chip chip;
8562306a36Sopenharmony_ci	struct regmap *regmap;
8662306a36Sopenharmony_ci	u32 offset;
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic inline struct sl28cpld_pwm *sl28cpld_pwm_from_chip(struct pwm_chip *chip)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	return container_of(chip, struct sl28cpld_pwm, chip);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic int sl28cpld_pwm_get_state(struct pwm_chip *chip,
9562306a36Sopenharmony_ci				  struct pwm_device *pwm,
9662306a36Sopenharmony_ci				  struct pwm_state *state)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
9962306a36Sopenharmony_ci	unsigned int reg;
10062306a36Sopenharmony_ci	int prescaler;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	sl28cpld_pwm_read(priv, SL28CPLD_PWM_CTRL, &reg);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	state->enabled = reg & SL28CPLD_PWM_CTRL_ENABLE;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	prescaler = FIELD_GET(SL28CPLD_PWM_CTRL_PRESCALER_MASK, reg);
10762306a36Sopenharmony_ci	state->period = SL28CPLD_PWM_PERIOD(prescaler);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	sl28cpld_pwm_read(priv, SL28CPLD_PWM_CYCLE, &reg);
11062306a36Sopenharmony_ci	state->duty_cycle = SL28CPLD_PWM_TO_DUTY_CYCLE(reg);
11162306a36Sopenharmony_ci	state->polarity = PWM_POLARITY_NORMAL;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	/*
11462306a36Sopenharmony_ci	 * Sanitize values for the PWM core. Depending on the prescaler it
11562306a36Sopenharmony_ci	 * might happen that we calculate a duty_cycle greater than the actual
11662306a36Sopenharmony_ci	 * period. This might happen if someone (e.g. the bootloader) sets an
11762306a36Sopenharmony_ci	 * invalid combination of values. The behavior of the hardware is
11862306a36Sopenharmony_ci	 * undefined in this case. But we need to report sane values back to
11962306a36Sopenharmony_ci	 * the PWM core.
12062306a36Sopenharmony_ci	 */
12162306a36Sopenharmony_ci	state->duty_cycle = min(state->duty_cycle, state->period);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	return 0;
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic int sl28cpld_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
12762306a36Sopenharmony_ci			      const struct pwm_state *state)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
13062306a36Sopenharmony_ci	unsigned int cycle, prescaler;
13162306a36Sopenharmony_ci	bool write_duty_cycle_first;
13262306a36Sopenharmony_ci	int ret;
13362306a36Sopenharmony_ci	u8 ctrl;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	/* Polarity inversion is not supported */
13662306a36Sopenharmony_ci	if (state->polarity != PWM_POLARITY_NORMAL)
13762306a36Sopenharmony_ci		return -EINVAL;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/*
14062306a36Sopenharmony_ci	 * Calculate the prescaler. Pick the biggest period that isn't
14162306a36Sopenharmony_ci	 * bigger than the requested period.
14262306a36Sopenharmony_ci	 */
14362306a36Sopenharmony_ci	prescaler = DIV_ROUND_UP_ULL(SL28CPLD_PWM_PERIOD(0), state->period);
14462306a36Sopenharmony_ci	prescaler = order_base_2(prescaler);
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	if (prescaler > field_max(SL28CPLD_PWM_CTRL_PRESCALER_MASK))
14762306a36Sopenharmony_ci		return -ERANGE;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	ctrl = FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, prescaler);
15062306a36Sopenharmony_ci	if (state->enabled)
15162306a36Sopenharmony_ci		ctrl |= SL28CPLD_PWM_CTRL_ENABLE;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	cycle = SL28CPLD_PWM_FROM_DUTY_CYCLE(state->duty_cycle);
15462306a36Sopenharmony_ci	cycle = min_t(unsigned int, cycle, SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler));
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/*
15762306a36Sopenharmony_ci	 * Work around the hardware limitation. See also above. Trap 100% duty
15862306a36Sopenharmony_ci	 * cycle if the prescaler is 0. Set prescaler to 1 instead. We don't
15962306a36Sopenharmony_ci	 * care about the frequency because its "all-one" in either case.
16062306a36Sopenharmony_ci	 *
16162306a36Sopenharmony_ci	 * We don't need to check the actual prescaler setting, because only
16262306a36Sopenharmony_ci	 * if the prescaler is 0 we can have this particular value.
16362306a36Sopenharmony_ci	 */
16462306a36Sopenharmony_ci	if (cycle == SL28CPLD_PWM_MAX_DUTY_CYCLE(0)) {
16562306a36Sopenharmony_ci		ctrl &= ~SL28CPLD_PWM_CTRL_PRESCALER_MASK;
16662306a36Sopenharmony_ci		ctrl |= FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, 1);
16762306a36Sopenharmony_ci		cycle = SL28CPLD_PWM_MAX_DUTY_CYCLE(1);
16862306a36Sopenharmony_ci	}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	/*
17162306a36Sopenharmony_ci	 * To avoid glitches when we switch the prescaler, we have to make sure
17262306a36Sopenharmony_ci	 * we have a valid duty cycle for the new mode.
17362306a36Sopenharmony_ci	 *
17462306a36Sopenharmony_ci	 * Take the current prescaler (or the current period length) into
17562306a36Sopenharmony_ci	 * account to decide whether we have to write the duty cycle or the new
17662306a36Sopenharmony_ci	 * prescaler first. If the period length is decreasing we have to
17762306a36Sopenharmony_ci	 * write the duty cycle first.
17862306a36Sopenharmony_ci	 */
17962306a36Sopenharmony_ci	write_duty_cycle_first = pwm->state.period > state->period;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	if (write_duty_cycle_first) {
18262306a36Sopenharmony_ci		ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
18362306a36Sopenharmony_ci		if (ret)
18462306a36Sopenharmony_ci			return ret;
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CTRL, ctrl);
18862306a36Sopenharmony_ci	if (ret)
18962306a36Sopenharmony_ci		return ret;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	if (!write_duty_cycle_first) {
19262306a36Sopenharmony_ci		ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
19362306a36Sopenharmony_ci		if (ret)
19462306a36Sopenharmony_ci			return ret;
19562306a36Sopenharmony_ci	}
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	return 0;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic const struct pwm_ops sl28cpld_pwm_ops = {
20162306a36Sopenharmony_ci	.apply = sl28cpld_pwm_apply,
20262306a36Sopenharmony_ci	.get_state = sl28cpld_pwm_get_state,
20362306a36Sopenharmony_ci	.owner = THIS_MODULE,
20462306a36Sopenharmony_ci};
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic int sl28cpld_pwm_probe(struct platform_device *pdev)
20762306a36Sopenharmony_ci{
20862306a36Sopenharmony_ci	struct sl28cpld_pwm *priv;
20962306a36Sopenharmony_ci	struct pwm_chip *chip;
21062306a36Sopenharmony_ci	int ret;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	if (!pdev->dev.parent) {
21362306a36Sopenharmony_ci		dev_err(&pdev->dev, "no parent device\n");
21462306a36Sopenharmony_ci		return -ENODEV;
21562306a36Sopenharmony_ci	}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
21862306a36Sopenharmony_ci	if (!priv)
21962306a36Sopenharmony_ci		return -ENOMEM;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
22262306a36Sopenharmony_ci	if (!priv->regmap) {
22362306a36Sopenharmony_ci		dev_err(&pdev->dev, "could not get parent regmap\n");
22462306a36Sopenharmony_ci		return -ENODEV;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	ret = device_property_read_u32(&pdev->dev, "reg", &priv->offset);
22862306a36Sopenharmony_ci	if (ret) {
22962306a36Sopenharmony_ci		dev_err(&pdev->dev, "no 'reg' property found (%pe)\n",
23062306a36Sopenharmony_ci			ERR_PTR(ret));
23162306a36Sopenharmony_ci		return -EINVAL;
23262306a36Sopenharmony_ci	}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	/* Initialize the pwm_chip structure */
23562306a36Sopenharmony_ci	chip = &priv->chip;
23662306a36Sopenharmony_ci	chip->dev = &pdev->dev;
23762306a36Sopenharmony_ci	chip->ops = &sl28cpld_pwm_ops;
23862306a36Sopenharmony_ci	chip->npwm = 1;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	ret = devm_pwmchip_add(&pdev->dev, chip);
24162306a36Sopenharmony_ci	if (ret) {
24262306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to add PWM chip (%pe)",
24362306a36Sopenharmony_ci			ERR_PTR(ret));
24462306a36Sopenharmony_ci		return ret;
24562306a36Sopenharmony_ci	}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	return 0;
24862306a36Sopenharmony_ci}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistatic const struct of_device_id sl28cpld_pwm_of_match[] = {
25162306a36Sopenharmony_ci	{ .compatible = "kontron,sl28cpld-pwm" },
25262306a36Sopenharmony_ci	{}
25362306a36Sopenharmony_ci};
25462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sl28cpld_pwm_of_match);
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cistatic struct platform_driver sl28cpld_pwm_driver = {
25762306a36Sopenharmony_ci	.probe = sl28cpld_pwm_probe,
25862306a36Sopenharmony_ci	.driver = {
25962306a36Sopenharmony_ci		.name = "sl28cpld-pwm",
26062306a36Sopenharmony_ci		.of_match_table = sl28cpld_pwm_of_match,
26162306a36Sopenharmony_ci	},
26262306a36Sopenharmony_ci};
26362306a36Sopenharmony_cimodule_platform_driver(sl28cpld_pwm_driver);
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ciMODULE_DESCRIPTION("sl28cpld PWM Driver");
26662306a36Sopenharmony_ciMODULE_AUTHOR("Michael Walle <michael@walle.cc>");
26762306a36Sopenharmony_ciMODULE_LICENSE("GPL");
268