Lines Matching +full:chip +full:- +full:relative
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
5 * Reference Manual : https://www.nxp.com/docs/en/data-sheet/MC33XS2410.pdf
8 * - Supports frequencies between 0.5Hz and 2048Hz with following steps:
9 * - 0.5 Hz steps from 0.5 Hz to 32 Hz
10 * - 2 Hz steps from 2 Hz to 128 Hz
11 * - 8 Hz steps from 8 Hz to 512 Hz
12 * - 32 Hz steps from 32 Hz to 2048 Hz
13 * - Cannot generate a 0 % duty cycle.
14 * - Always produces low output if disabled.
15 * - Configuration isn't atomic. When changing polarity, duty cycle or period
45 #define MC33XS2410_PWM_CTRL3_EN(chan) BIT(4 + (chan) - 1)
48 #define MC33XS2410_PWM_FREQ(chan) (0x08 + (chan) - 1)
53 #define MC33XS2410_PWM_DC(chan) (0x0c + (chan) - 1)
76 return -EINVAL; in mc33xs2410_write_regs()
99 return -EINVAL; in mc33xs2410_read_regs()
102 for (i = 0; i < len - 1; i++) in mc33xs2410_read_regs()
111 val[i - 1] = FIELD_GET(MC33XS2410_FRAME_OUT_DATA, rx[i]); in mc33xs2410_read_regs()
186 FIELD_PREP(MC33XS2410_PWM_FREQ_COUNT, count - 1); in mc33xs2410_pwm_get_freq()
195 * - 0 = 0.5Hz in mc33xs2410_pwm_get_period()
196 * - 1 = 2Hz in mc33xs2410_pwm_get_period()
197 * - 2 = 8Hz in mc33xs2410_pwm_get_period()
198 * - 3 = 32Hz in mc33xs2410_pwm_get_period()
214 * The hardware cannot generate a 0% relative duty cycle for normal and inversed
218 * to normal and the relative duty cylce must be set to 100%. The device then
221 static int mc33xs2410_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in mc33xs2410_pwm_apply() argument
224 struct spi_device *spi = pwmchip_get_drvdata(chip); in mc33xs2410_pwm_apply()
226 MC33XS2410_PWM_FREQ(pwm->hwpwm + 1), in mc33xs2410_pwm_apply()
227 MC33XS2410_PWM_DC(pwm->hwpwm + 1), in mc33xs2410_pwm_apply()
237 period = min(state->period, MC33XS2410_PWM_MAX_PERIOD(0)); in mc33xs2410_pwm_apply()
239 return -EINVAL; in mc33xs2410_pwm_apply()
251 duty_cycle = min(period, state->duty_cycle); in mc33xs2410_pwm_apply()
252 rel_dc = div64_u64(duty_cycle * 256, period) - 1; in mc33xs2410_pwm_apply()
255 else if (state->polarity == PWM_POLARITY_NORMAL) in mc33xs2410_pwm_apply()
261 mask = MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm + 1); in mc33xs2410_pwm_apply()
262 if (state->polarity == PWM_POLARITY_INVERSED && rel_dc >= 0) in mc33xs2410_pwm_apply()
268 mask = MC33XS2410_PWM_CTRL3_EN(pwm->hwpwm + 1); in mc33xs2410_pwm_apply()
269 if (state->enabled && in mc33xs2410_pwm_apply()
270 !(state->polarity == PWM_POLARITY_NORMAL && rel_dc < 0)) in mc33xs2410_pwm_apply()
278 static int mc33xs2410_pwm_get_state(struct pwm_chip *chip, in mc33xs2410_pwm_get_state() argument
282 struct spi_device *spi = pwmchip_get_drvdata(chip); in mc33xs2410_pwm_get_state()
284 MC33XS2410_PWM_FREQ(pwm->hwpwm + 1), in mc33xs2410_pwm_get_state()
285 MC33XS2410_PWM_DC(pwm->hwpwm + 1), in mc33xs2410_pwm_get_state()
297 state->period = mc33xs2410_pwm_get_period(val[0]); in mc33xs2410_pwm_get_state()
298 state->polarity = (val[2] & MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm + 1)) ? in mc33xs2410_pwm_get_state()
300 state->enabled = !!(val[3] & MC33XS2410_PWM_CTRL3_EN(pwm->hwpwm + 1)); in mc33xs2410_pwm_get_state()
301 state->duty_cycle = DIV_ROUND_UP_ULL((val[1] + 1) * state->period, 256); in mc33xs2410_pwm_get_state()
319 /* Wake-up time */ in mc33xs2410_reset()
327 struct device *dev = &spi->dev; in mc33xs2410_probe()
329 struct pwm_chip *chip; in mc33xs2410_probe() local
332 chip = devm_pwmchip_alloc(dev, 4, 0); in mc33xs2410_probe()
333 if (IS_ERR(chip)) in mc33xs2410_probe()
334 return PTR_ERR(chip); in mc33xs2410_probe()
336 spi->bits_per_word = 16; in mc33xs2410_probe()
337 spi->mode |= SPI_CS_WORD; in mc33xs2410_probe()
342 pwmchip_set_drvdata(chip, spi); in mc33xs2410_probe()
343 chip->ops = &mc33xs2410_pwm_ops; in mc33xs2410_probe()
372 ret = devm_pwmchip_add(dev, chip); in mc33xs2410_probe()
374 return dev_err_probe(dev, ret, "Failed to add pwm chip\n"); in mc33xs2410_probe()
378 return dev_err_probe(dev, -ENODEV, "Failed to register hwmon device\n"); in mc33xs2410_probe()
397 .name = "mc33xs2410-pwm",
405 MODULE_DESCRIPTION("NXP MC33XS2410 high-side switch driver");