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