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> 419db33d22SMichael Walle #include <linux/pwm.h> 429db33d22SMichael Walle #include <linux/regmap.h> 439db33d22SMichael Walle 449db33d22SMichael Walle /* 459db33d22SMichael Walle * PWM timer block registers. 469db33d22SMichael Walle */ 479db33d22SMichael Walle #define SL28CPLD_PWM_CTRL 0x00 489db33d22SMichael Walle #define SL28CPLD_PWM_CTRL_ENABLE BIT(7) 499db33d22SMichael Walle #define SL28CPLD_PWM_CTRL_PRESCALER_MASK GENMASK(1, 0) 509db33d22SMichael Walle #define SL28CPLD_PWM_CYCLE 0x01 519db33d22SMichael Walle #define SL28CPLD_PWM_CYCLE_MAX GENMASK(6, 0) 529db33d22SMichael Walle 539db33d22SMichael Walle #define SL28CPLD_PWM_CLK 32000 /* 32 kHz */ 549db33d22SMichael Walle #define SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler) (1 << (7 - (prescaler))) 559db33d22SMichael Walle #define SL28CPLD_PWM_PERIOD(prescaler) \ 569db33d22SMichael Walle (NSEC_PER_SEC / SL28CPLD_PWM_CLK * SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler)) 579db33d22SMichael Walle 589db33d22SMichael Walle /* 599db33d22SMichael Walle * We calculate the duty cycle like this: 609db33d22SMichael Walle * duty_cycle_ns = pwm_cycle_reg * max_period_ns / max_duty_cycle 619db33d22SMichael Walle * 629db33d22SMichael Walle * With 639db33d22SMichael Walle * max_period_ns = 1 << (7 - prescaler) / SL28CPLD_PWM_CLK * NSEC_PER_SEC 649db33d22SMichael Walle * max_duty_cycle = 1 << (7 - prescaler) 659db33d22SMichael Walle * this then simplifies to: 669db33d22SMichael Walle * duty_cycle_ns = pwm_cycle_reg / SL28CPLD_PWM_CLK * NSEC_PER_SEC 679db33d22SMichael Walle * = NSEC_PER_SEC / SL28CPLD_PWM_CLK * pwm_cycle_reg 689db33d22SMichael Walle * 699db33d22SMichael Walle * NSEC_PER_SEC is a multiple of SL28CPLD_PWM_CLK, therefore we're not losing 709db33d22SMichael Walle * precision by doing the divison first. 719db33d22SMichael Walle */ 729db33d22SMichael Walle #define SL28CPLD_PWM_TO_DUTY_CYCLE(reg) \ 739db33d22SMichael Walle (NSEC_PER_SEC / SL28CPLD_PWM_CLK * (reg)) 749db33d22SMichael Walle #define SL28CPLD_PWM_FROM_DUTY_CYCLE(duty_cycle) \ 759db33d22SMichael Walle (DIV_ROUND_DOWN_ULL((duty_cycle), NSEC_PER_SEC / SL28CPLD_PWM_CLK)) 769db33d22SMichael Walle 779db33d22SMichael Walle #define sl28cpld_pwm_read(priv, reg, val) \ 789db33d22SMichael Walle regmap_read((priv)->regmap, (priv)->offset + (reg), (val)) 799db33d22SMichael Walle #define sl28cpld_pwm_write(priv, reg, val) \ 809db33d22SMichael Walle regmap_write((priv)->regmap, (priv)->offset + (reg), (val)) 819db33d22SMichael Walle 829db33d22SMichael Walle struct sl28cpld_pwm { 839db33d22SMichael Walle struct pwm_chip pwm_chip; 849db33d22SMichael Walle struct regmap *regmap; 859db33d22SMichael Walle u32 offset; 869db33d22SMichael Walle }; 87*062c9cdfSUwe Kleine-König #define sl28cpld_pwm_from_chip(_chip) \ 88*062c9cdfSUwe Kleine-König container_of(_chip, struct sl28cpld_pwm, pwm_chip) 899db33d22SMichael Walle 909db33d22SMichael Walle static void sl28cpld_pwm_get_state(struct pwm_chip *chip, 919db33d22SMichael Walle struct pwm_device *pwm, 929db33d22SMichael Walle struct pwm_state *state) 939db33d22SMichael Walle { 94*062c9cdfSUwe Kleine-König struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip); 959db33d22SMichael Walle unsigned int reg; 969db33d22SMichael Walle int prescaler; 979db33d22SMichael Walle 989db33d22SMichael Walle sl28cpld_pwm_read(priv, SL28CPLD_PWM_CTRL, ®); 999db33d22SMichael Walle 1009db33d22SMichael Walle state->enabled = reg & SL28CPLD_PWM_CTRL_ENABLE; 1019db33d22SMichael Walle 1029db33d22SMichael Walle prescaler = FIELD_GET(SL28CPLD_PWM_CTRL_PRESCALER_MASK, reg); 1039db33d22SMichael Walle state->period = SL28CPLD_PWM_PERIOD(prescaler); 1049db33d22SMichael Walle 1059db33d22SMichael Walle sl28cpld_pwm_read(priv, SL28CPLD_PWM_CYCLE, ®); 1069db33d22SMichael Walle state->duty_cycle = SL28CPLD_PWM_TO_DUTY_CYCLE(reg); 1079db33d22SMichael Walle state->polarity = PWM_POLARITY_NORMAL; 1089db33d22SMichael Walle 1099db33d22SMichael Walle /* 1109db33d22SMichael Walle * Sanitize values for the PWM core. Depending on the prescaler it 1119db33d22SMichael Walle * might happen that we calculate a duty_cycle greater than the actual 1129db33d22SMichael Walle * period. This might happen if someone (e.g. the bootloader) sets an 1139db33d22SMichael Walle * invalid combination of values. The behavior of the hardware is 1149db33d22SMichael Walle * undefined in this case. But we need to report sane values back to 1159db33d22SMichael Walle * the PWM core. 1169db33d22SMichael Walle */ 1179db33d22SMichael Walle state->duty_cycle = min(state->duty_cycle, state->period); 1189db33d22SMichael Walle } 1199db33d22SMichael Walle 1209db33d22SMichael Walle static int sl28cpld_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 1219db33d22SMichael Walle const struct pwm_state *state) 1229db33d22SMichael Walle { 123*062c9cdfSUwe Kleine-König struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip); 1249db33d22SMichael Walle unsigned int cycle, prescaler; 1259db33d22SMichael Walle bool write_duty_cycle_first; 1269db33d22SMichael Walle int ret; 1279db33d22SMichael Walle u8 ctrl; 1289db33d22SMichael Walle 1299db33d22SMichael Walle /* Polarity inversion is not supported */ 1309db33d22SMichael Walle if (state->polarity != PWM_POLARITY_NORMAL) 1319db33d22SMichael Walle return -EINVAL; 1329db33d22SMichael Walle 1339db33d22SMichael Walle /* 1349db33d22SMichael Walle * Calculate the prescaler. Pick the biggest period that isn't 1359db33d22SMichael Walle * bigger than the requested period. 1369db33d22SMichael Walle */ 1379db33d22SMichael Walle prescaler = DIV_ROUND_UP_ULL(SL28CPLD_PWM_PERIOD(0), state->period); 1389db33d22SMichael Walle prescaler = order_base_2(prescaler); 1399db33d22SMichael Walle 1409db33d22SMichael Walle if (prescaler > field_max(SL28CPLD_PWM_CTRL_PRESCALER_MASK)) 1419db33d22SMichael Walle return -ERANGE; 1429db33d22SMichael Walle 1439db33d22SMichael Walle ctrl = FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, prescaler); 1449db33d22SMichael Walle if (state->enabled) 1459db33d22SMichael Walle ctrl |= SL28CPLD_PWM_CTRL_ENABLE; 1469db33d22SMichael Walle 1479db33d22SMichael Walle cycle = SL28CPLD_PWM_FROM_DUTY_CYCLE(state->duty_cycle); 1489db33d22SMichael Walle cycle = min_t(unsigned int, cycle, SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler)); 1499db33d22SMichael Walle 1509db33d22SMichael Walle /* 1519db33d22SMichael Walle * Work around the hardware limitation. See also above. Trap 100% duty 1529db33d22SMichael Walle * cycle if the prescaler is 0. Set prescaler to 1 instead. We don't 1539db33d22SMichael Walle * care about the frequency because its "all-one" in either case. 1549db33d22SMichael Walle * 1559db33d22SMichael Walle * We don't need to check the actual prescaler setting, because only 1569db33d22SMichael Walle * if the prescaler is 0 we can have this particular value. 1579db33d22SMichael Walle */ 1589db33d22SMichael Walle if (cycle == SL28CPLD_PWM_MAX_DUTY_CYCLE(0)) { 1599db33d22SMichael Walle ctrl &= ~SL28CPLD_PWM_CTRL_PRESCALER_MASK; 1609db33d22SMichael Walle ctrl |= FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, 1); 1619db33d22SMichael Walle cycle = SL28CPLD_PWM_MAX_DUTY_CYCLE(1); 1629db33d22SMichael Walle } 1639db33d22SMichael Walle 1649db33d22SMichael Walle /* 1659db33d22SMichael Walle * To avoid glitches when we switch the prescaler, we have to make sure 1669db33d22SMichael Walle * we have a valid duty cycle for the new mode. 1679db33d22SMichael Walle * 1689db33d22SMichael Walle * Take the current prescaler (or the current period length) into 1699db33d22SMichael Walle * account to decide whether we have to write the duty cycle or the new 1709db33d22SMichael Walle * prescaler first. If the period length is decreasing we have to 1719db33d22SMichael Walle * write the duty cycle first. 1729db33d22SMichael Walle */ 1739db33d22SMichael Walle write_duty_cycle_first = pwm->state.period > state->period; 1749db33d22SMichael Walle 1759db33d22SMichael Walle if (write_duty_cycle_first) { 1769db33d22SMichael Walle ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle); 1779db33d22SMichael Walle if (ret) 1789db33d22SMichael Walle return ret; 1799db33d22SMichael Walle } 1809db33d22SMichael Walle 1819db33d22SMichael Walle ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CTRL, ctrl); 1829db33d22SMichael Walle if (ret) 1839db33d22SMichael Walle return ret; 1849db33d22SMichael Walle 1859db33d22SMichael Walle if (!write_duty_cycle_first) { 1869db33d22SMichael Walle ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle); 1879db33d22SMichael Walle if (ret) 1889db33d22SMichael Walle return ret; 1899db33d22SMichael Walle } 1909db33d22SMichael Walle 1919db33d22SMichael Walle return 0; 1929db33d22SMichael Walle } 1939db33d22SMichael Walle 1949db33d22SMichael Walle static const struct pwm_ops sl28cpld_pwm_ops = { 1959db33d22SMichael Walle .apply = sl28cpld_pwm_apply, 1969db33d22SMichael Walle .get_state = sl28cpld_pwm_get_state, 1979db33d22SMichael Walle .owner = THIS_MODULE, 1989db33d22SMichael Walle }; 1999db33d22SMichael Walle 2009db33d22SMichael Walle static int sl28cpld_pwm_probe(struct platform_device *pdev) 2019db33d22SMichael Walle { 2029db33d22SMichael Walle struct sl28cpld_pwm *priv; 2039db33d22SMichael Walle struct pwm_chip *chip; 2049db33d22SMichael Walle int ret; 2059db33d22SMichael Walle 2069db33d22SMichael Walle if (!pdev->dev.parent) { 2079db33d22SMichael Walle dev_err(&pdev->dev, "no parent device\n"); 2089db33d22SMichael Walle return -ENODEV; 2099db33d22SMichael Walle } 2109db33d22SMichael Walle 2119db33d22SMichael Walle priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 2129db33d22SMichael Walle if (!priv) 2139db33d22SMichael Walle return -ENOMEM; 2149db33d22SMichael Walle 2159db33d22SMichael Walle priv->regmap = dev_get_regmap(pdev->dev.parent, NULL); 2169db33d22SMichael Walle if (!priv->regmap) { 2179db33d22SMichael Walle dev_err(&pdev->dev, "could not get parent regmap\n"); 2189db33d22SMichael Walle return -ENODEV; 2199db33d22SMichael Walle } 2209db33d22SMichael Walle 2219db33d22SMichael Walle ret = device_property_read_u32(&pdev->dev, "reg", &priv->offset); 2229db33d22SMichael Walle if (ret) { 2239db33d22SMichael Walle dev_err(&pdev->dev, "no 'reg' property found (%pe)\n", 2249db33d22SMichael Walle ERR_PTR(ret)); 2259db33d22SMichael Walle return -EINVAL; 2269db33d22SMichael Walle } 2279db33d22SMichael Walle 2289db33d22SMichael Walle /* Initialize the pwm_chip structure */ 2299db33d22SMichael Walle chip = &priv->pwm_chip; 2309db33d22SMichael Walle chip->dev = &pdev->dev; 2319db33d22SMichael Walle chip->ops = &sl28cpld_pwm_ops; 2329db33d22SMichael Walle chip->base = -1; 2339db33d22SMichael Walle chip->npwm = 1; 2349db33d22SMichael Walle 2359db33d22SMichael Walle ret = pwmchip_add(&priv->pwm_chip); 2369db33d22SMichael Walle if (ret) { 2379db33d22SMichael Walle dev_err(&pdev->dev, "failed to add PWM chip (%pe)", 2389db33d22SMichael Walle ERR_PTR(ret)); 2399db33d22SMichael Walle return ret; 2409db33d22SMichael Walle } 2419db33d22SMichael Walle 2429db33d22SMichael Walle platform_set_drvdata(pdev, priv); 2439db33d22SMichael Walle 2449db33d22SMichael Walle return 0; 2459db33d22SMichael Walle } 2469db33d22SMichael Walle 2479db33d22SMichael Walle static int sl28cpld_pwm_remove(struct platform_device *pdev) 2489db33d22SMichael Walle { 2499db33d22SMichael Walle struct sl28cpld_pwm *priv = platform_get_drvdata(pdev); 2509db33d22SMichael Walle 2519db33d22SMichael Walle return pwmchip_remove(&priv->pwm_chip); 2529db33d22SMichael Walle } 2539db33d22SMichael Walle 2549db33d22SMichael Walle static const struct of_device_id sl28cpld_pwm_of_match[] = { 2559db33d22SMichael Walle { .compatible = "kontron,sl28cpld-pwm" }, 2569db33d22SMichael Walle {} 2579db33d22SMichael Walle }; 2589db33d22SMichael Walle MODULE_DEVICE_TABLE(of, sl28cpld_pwm_of_match); 2599db33d22SMichael Walle 2609db33d22SMichael Walle static struct platform_driver sl28cpld_pwm_driver = { 2619db33d22SMichael Walle .probe = sl28cpld_pwm_probe, 2629db33d22SMichael Walle .remove = sl28cpld_pwm_remove, 2639db33d22SMichael Walle .driver = { 2649db33d22SMichael Walle .name = "sl28cpld-pwm", 2659db33d22SMichael Walle .of_match_table = sl28cpld_pwm_of_match, 2669db33d22SMichael Walle }, 2679db33d22SMichael Walle }; 2689db33d22SMichael Walle module_platform_driver(sl28cpld_pwm_driver); 2699db33d22SMichael Walle 2709db33d22SMichael Walle MODULE_DESCRIPTION("sl28cpld PWM Driver"); 2719db33d22SMichael Walle MODULE_AUTHOR("Michael Walle <michael@walle.cc>"); 2729db33d22SMichael Walle MODULE_LICENSE("GPL"); 273