From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796304775; b=Iu5aGkV+BsarQXnDAETEMPH6SYJBtsljSTpDajfrgx3X44bfvTQJu4fj7T9xEcLRYicD SKZWM7/1UGwmBM82CoLC9E1DwIP1L8jNH/D0rVNZe5pM+2laaL4CmsXG985yEjl46+vbi 3d9bqfG/rwFBB/dGDilmJ4lgzC/BHIX4j5NTRAnmNTYamYqhrAWD61eadPyqGmebfo8ZR jIvJEnyhAAFO2eJFIQYYExsWeGm7s5fV3c4jX/KJvHprd+dI76R1KYrM+C4OyqrmoTuNk JRh3Ka65uDqcEMpMEkg7jKf22KqhY1+AsNbGG5unr7DNtnqWvZNY8SQb23FFekIl2Tw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796304775; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=VqexzPnoqulfHq3we72Y0pSPltc4LLxzj2wPCg9HUp0=; b=wVt/6E9JA3TiCC4JrN/c4kZA3Ni+N+TlJhbh+kHzvI0bqa3u4mIMYx83C2tNX7NyyNIg fNnM+RYQxXvHNRJP6YrikH4LCMv0Btn6ctcVgXrUo7vFDXQfIAXYrCJ964WAhnfF6s5MP Tm9iD/kalq5y+0EdILRD9DQ2IP9yaNx+xLbDa659e81T+4A8QrqelhWA1Fx9eVOLVoj+L ox0fKdOCXl1iipMYBvh5Z78obXCzeqVnoRaHoNS7MQT8ioB7Dgw5HNjEEhc/itZiJOE6/ tKXcYiPTJJ3W+ABkYnDDqPO0/gTb282i8NR9qPL422J0ewTXupGG07pw+QwNlFpwmIQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Bjd+F9bm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 5B171606A330; Thu, 3 Dec 2026 13:32:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Bjd+F9bm Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 437911F000C1; Thu, 3 Dec 2026 13:32:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796304775; bh=VqexzPnoqulfHq3we72Y0pSPltc4LLxzj2wPCg9HUp0=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Bjd+F9bmN6Xxa4wgyak9jrLEcvgvuebkkOU96weogOmZbeYH5PtC/TCkpfTLHi1hA QMwQWOzoiZChNcC/rwArIkud4e3gIgUgzgpzYcedtSRRwUGl2ZI6/zT/U3XhfNvY94 SXRhI+Q4A4F1eZEssmyNTAh/S+H89vYtKPOavQVLZhwRSyif4iONN1nuTHUWhPzYJ3 DSM93tq2T36JD8foKPNL+L05+Ih+vNUEYdO9O5Ddvrv1JBYf7hhCMmNBuPtIXvK8ZG NcTsm/VTxHIC6wF+cB9MMbgm51GXH2alPlfCdiK8XlygvKfL1d9UyUgotedER1W/Px SMOwy2PTDQQlg== Date: Thu, 3 Dec 2026 14:32:51 +0100 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Angelo Dureghello Cc: Geert Uytterhoeven , linux-m68k@lists.linux-m68k.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org Subject: Re: [PATCH 2/2] drivers: pwm: add mcf54415 mcPWM support Message-ID: References: <20261005-wip-bl-stmark2-pwm-v1-0-168b9dc653f2@baylibre.com> <20261005-wip-bl-stmark2-pwm-v1-2-168b9dc653f2@baylibre.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="festxwceb6xoebyw" Content-Disposition: inline In-Reply-To: <20261005-wip-bl-stmark2-pwm-v1-2-168b9dc653f2@baylibre.com> --festxwceb6xoebyw Content-Type: text/plain; protected-headers=v1; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Subject: Re: [PATCH 2/2] drivers: pwm: add mcf54415 mcPWM support MIME-Version: 1.0 Hey Angelo, On Mon, Oct 05, 2026 at 12:06:24PM +0200, Angelo Dureghello wrote: > From: Angelo Dureghello >=20 > Add initial basic mcf54415 mcPWM (motor control PWM) support, to be > used with mcf54415/6/7/8 NXP SoCs. >=20 > Signed-off-by: Angelo Dureghello > --- > drivers/pwm/Kconfig | 11 + > drivers/pwm/Makefile | 1 + > drivers/pwm/pwm-mcf.c | 646 ++++++++++++++++++++++++++++++++++++++++++++= ++++++ > 3 files changed, 658 insertions(+) >=20 > diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig > index 729776086879..ddb98bec33d3 100644 > --- a/drivers/pwm/Kconfig > +++ b/drivers/pwm/Kconfig > @@ -486,6 +486,17 @@ config PWM_MC33XS2410 > To compile this driver as a module, choose M here: the module > will be called pwm-mc33xs2410. > =20 > +config PWM_MCF > + tristate "NXP ColdFire mcPWM support" > + depends on M5441x || COMPILE_TEST > + select REGMAP_MMIO > + help > + NXP ColdFire mcPWM (motor control PWM) support. This module is > + available in the mcf5441x CPU family, > + > + To compile this driver as a module, choose M here: the module > + will be called pwm-mcf. > + > config PWM_MEDIATEK > tristate "MediaTek PWM support" > depends on ARCH_MEDIATEK || RALINK || COMPILE_TEST > diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile > index 5630a521a7cf..471c29e25255 100644 > --- a/drivers/pwm/Makefile > +++ b/drivers/pwm/Makefile > @@ -42,6 +42,7 @@ obj-$(CONFIG_PWM_LPSS_PCI) +=3D pwm-lpss-pci.o > obj-$(CONFIG_PWM_LPSS_PLATFORM) +=3D pwm-lpss-platform.o > obj-$(CONFIG_PWM_MAX7360) +=3D pwm-max7360.o > obj-$(CONFIG_PWM_MC33XS2410) +=3D pwm-mc33xs2410.o > +obj-$(CONFIG_PWM_MCF) +=3D pwm-mcf.o > obj-$(CONFIG_PWM_MEDIATEK) +=3D pwm-mediatek.o > obj-$(CONFIG_PWM_MESON) +=3D pwm-meson.o > obj-$(CONFIG_PWM_MICROCHIP_CORE) +=3D pwm-microchip-core.o > diff --git a/drivers/pwm/pwm-mcf.c b/drivers/pwm/pwm-mcf.c > new file mode 100644 > index 000000000000..b1dec215ef64 > --- /dev/null > +++ b/drivers/pwm/pwm-mcf.c > @@ -0,0 +1,646 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// SPDX-FileCopyrightText: 2026 BayLibre, SAS. > +/* > + * Simple driver for mcPWM (Motor Control Pulse Width Modulation) > + * controller, available on ColdFire SoCs mcf54415/6/7/8. > + * > + * Reference Manual: > + * https://www.nxp.com/docs/en/reference-manual/MCF54418RM.pdf > + * > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define MCF_PWM_SUBMODS 0x04 > +#define MCF_PWM_SUB_OFFS(n) ((n) * MCFPWM_SUB_SIZE) > + > +#define MCF_PWM_SM_CNT 0x00 > +#define MCF_PWM_SM_INIT 0x02 > +#define MCF_PWM_SM_CR2 0x04 > +#define MCF_PWM_SM_CR2_IND BIT(13) > +#define MCF_PWM_SM_CR1 0x06 > +#define MCF_PWM_SM_CR1_FULL BIT(10) > +#define MCF_PWM_SM_CR1_PRSC GENMASK(6, 4) > +#define MCF_PWM_SM_VAL0 0x08 > +#define MCF_PWM_SM_VAL1 0x0A > +#define MCF_PWM_SM_VAL2 0x0C > +#define MCF_PWM_SM_VAL3 0x0E > +#define MCF_PWM_SM_VAL4 0x10 > +#define MCF_PWM_SM_VAL5 0x12 > +#define MCF_PWM_SM_OCR 0x18 > +#define MCF_PWM_SM_OCR_POL_A BIT(10) > +#define MCF_PWM_SM_OCR_POL_B BIT(9) > +#define MCF_PWM_SM_OCR_POL_X BIT(8) > +#define MCF_PWM_SM_SR 0x1a > +#define MCF_PWM_SM_DISMAP 0x22 > + > +#define MCF_PWM_OUTEN 0x140 > +#define MCF_PWM_OUTEN_PWMA_EN GENMASK(11, 8) > +#define MCF_PWM_MCR 0x148 > +#define MCF_PWM_MCR_LDOK GENMASK(3, 0) > +#define MCF_PWM_MCR_CLDOK GENMASK(7, 4) > +#define MCF_PWM_MCR_RUN GENMASK(11, 8) > +#define MCF_PWM_FSR 0x14e > + > +struct pwm_mcf { > + struct regmap *map; > + struct clk *clk; > + unsigned long rate; > + u64 period_ns_min; > + u64 period_ns_max; > +}; > + > +struct pwm_mcf_waveform { > + u8 enable; > + u8 prescaler; > + s16 init; > + s16 val0; > + s16 val1; > + s16 val2; > + s16 val3; > + s16 val4; > + s16 val5; > +}; > + > +static const struct regmap_config pwm_mcf_regmap_config =3D { > + .reg_bits =3D 16, > + .val_bits =3D 16, > + .reg_stride =3D 2, > + .max_register =3D MCFPWM_SIZE, /* R.M. Table 34-4, PWM Memory Map */ > + .val_format_endian =3D REGMAP_ENDIAN_BIG, > + .reg_format_endian =3D REGMAP_ENDIAN_BIG, > +}; > + > +static inline struct pwm_mcf *pwm_mcf_from_chip(struct pwm_chip *chip) > +{ > + return pwmchip_get_drvdata(chip); > +} > + > +static int pwm_mcf_regmap_read(struct regmap *map, int submod, > + unsigned int reg, unsigned int *val) > +{ > + /* Avoid any global configuration register overwrite. */ > + if (submod >=3D MCF_PWM_SUBMODS) > + return -EINVAL; > + > + return regmap_read(map, MCF_PWM_SUB_OFFS(submod) + reg, val); > +} > + > +static int pwm_mcf_regmap_write(struct regmap *map, int submod, > + unsigned int reg, unsigned int val) > +{ > + if (submod >=3D MCF_PWM_SUBMODS) > + return -EINVAL; > + > + return regmap_write(map, MCF_PWM_SUB_OFFS(submod) + reg, val); > +} > + > +static int pwm_mcf_regmap_set_bits(struct regmap *map, int submod, > + unsigned int reg, unsigned int bits) > +{ > + if (submod >=3D MCF_PWM_SUBMODS) > + return -EINVAL; > + > + return regmap_set_bits(map, MCF_PWM_SUB_OFFS(submod) + reg, bits); > +} > + > +#ifdef DEBUG > +static void pwm_mcf_dump_regs(struct pwm_chip *chip, struct pwm_device *= pwm) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + struct device *dev =3D pwmchip_parent(chip); > + unsigned int val, ch; > + > + ch =3D pwm->hwpwm; > + > + dev_dbg(dev, "***** mcPWM register set *****\n"); > + regmap_read(priv->map, MCF_PWM_MCR, &val); > + dev_dbg(dev, "***** MCF_PWM_MCR: %04x *****\n", val); > + regmap_read(priv->map, MCF_PWM_FSR, &val); > + dev_dbg(dev, "***** MCF_PWM_FSR: %04x *****\n", val); > + regmap_read(priv->map, MCF_PWM_OUTEN, &val); > + dev_dbg(dev, "***** MCF_PWM_OUTEN: %04x *****\n", val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_CR1, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_CR1(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_CR2, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_CR2(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_CNT, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_CNT(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_SR, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_SR(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_OCR, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_OCR(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_INIT, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_INIT(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL0, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_VAL0(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL1, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_VAL1(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL2, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_VAL2(%d): %04x *****\n", ch, val); > + pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL3, &val); > + dev_dbg(dev, "***** MCF_PWM_SM_VAL3(%d): %04x *****\n", ch, val); > +} > +#endif > + > +/* > + * From R.M. 34.3.21. > + * Considering a submodule n, setting the n-th LDOK bit, PWM_SMnCR1[PRSC= ], > + * PWM_SMnINIT, and PWM_SMnVAL are loaded into a set of buffers. > + * The pulse width take effect at the next PWM reload if LDMOD is cleare= d, or > + * immediately if LDMOD is set. > + * As per R.M., any LDOK bit must be set only by reading it, checking if= it is > + * zero, and then writing a logic one to it. > + */ > +static int pwm_mcf_set_mcr_ldok(struct pwm_chip *chip, struct pwm_device= *pwm) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + unsigned int val, bit; > + int ch, ret; > + > + ch =3D pwm->hwpwm; > + > + bit =3D FIELD_PREP(MCF_PWM_MCR_LDOK, BIT(ch)); > + ret =3D regmap_read_poll_timeout(priv->map, MCF_PWM_MCR, val, > + !(val & bit), 10, 1000); > + if (ret) > + return ret; > + > + return regmap_set_bits(priv->map, MCF_PWM_MCR, > + FIELD_PREP(MCF_PWM_MCR_LDOK, BIT(ch))); > +} > + > +/* > + * Any LDOK PWM_MCF bit can be cleared manually setting respective CLDOK= bit. > + */ > +static int pwm_mcf_clear_mcr_ldok(struct pwm_chip *chip, struct pwm_devi= ce *pwm) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + int val; > + > + val =3D FIELD_PREP(MCF_PWM_MCR_CLDOK, BIT(pwm->hwpwm)); > + > + return regmap_set_bits(priv->map, MCF_PWM_MCR, val); > +} > + > +static int pwm_mcf_calc_prescaler(u64 period_ns, unsigned long *rate, u8= *presc) > +{ > + u64 ticks =3D mul_u64_u32_div(period_ns, *rate, NSEC_PER_SEC); > + u32 pmin =3D max(1U, DIV_ROUND_UP_ULL(ticks, 65535ULL)); > + u32 ps =3D roundup_pow_of_two(pmin); > + > + *rate /=3D ps; > + /* Register asks a 3 bit bitmask to reach 128 max. */ > + *presc =3D ilog2(ps); > + > + return 0; > +} > + > +static int pwm_mcf_enable(struct pwm_chip *chip, struct pwm_device *pwm) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + unsigned int ch; > + int ret; > + > + dev_dbg(pwmchip_parent(chip), "%s() enabling ch %d\n", > + __func__, pwm->hwpwm); > + > + ch =3D pwm->hwpwm; > + > + /* > + * RUN specific submodule. > + * RM 34.5: initialize all registers and set the PWM_MCR[LDOK] > + * bit before setting the PWM_MCR[RUN] bit. We are here always after > + * apply, so LDOK bits has been always cleared before. > + */ > + ret =3D pwm_mcf_set_mcr_ldok(chip, pwm); > + if (ret) > + return ret; > + > + /* Remove high impedance now. */ > + ret =3D regmap_set_bits(priv->map, MCF_PWM_OUTEN, > + FIELD_PREP(MCF_PWM_OUTEN_PWMA_EN, BIT(ch))); > + if (ret) > + return ret; > + > + return regmap_set_bits(priv->map, MCF_PWM_MCR, > + FIELD_PREP(MCF_PWM_MCR_RUN, BIT(ch))); > +} > + > +static int pwm_mcf_disable(struct pwm_chip *chip, struct pwm_device *pwm) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + int ch, val, ret; > + > + ch =3D pwm->hwpwm; > + > + /* STOP specific submodule. */ > + ret =3D regmap_clear_bits(priv->map, MCF_PWM_MCR, > + FIELD_PREP(MCF_PWM_MCR_RUN, BIT(ch))); > + if (ret) > + return ret; > + > + /* Set high impedance. */ > + val =3D FIELD_PREP(MCF_PWM_OUTEN_PWMA_EN, BIT(ch)); > + ret =3D regmap_clear_bits(priv->map, MCF_PWM_OUTEN, val); > + if (ret) > + return ret; > + > + clk_disable_unprepare(priv->clk); > + > + return ret; > +} > + > +static int pwm_mcf_round_waveform_tohw(struct pwm_chip *chip, > + struct pwm_device *pwm, > + const struct pwm_waveform *wf, > + void *_wfhw) > +{ > + struct device *dev =3D pwmchip_parent(chip); > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + struct pwm_mcf_waveform *wfhw =3D _wfhw; > + u16 half_period, duty; > + unsigned long rate; > + u64 period_ns; > + u8 prescaler; > + int ret, val; > + > + /* > + * The MCF mcPWM (motor control) module allows to set up and down edges > + * separately, for each submodule (channel) and for each output line > + * (A and B). Each channel has his own counter. > + * > + * INIT and VAL0 defines the whole counter range (max steps 2^16) for > + * both A and B channels, > + * VAL0 must be the "half" of the counter range, > + * VAL2/3, often referred in the R.M. as PWM23, are respectively > + * turn-on and turn-off edges for channel A, > + * VAL4/5, often referred in the R.M. as PWM45 are the same for > + * channel B and not used/set. > + * > + * Timer: time -> > + * > + * VAL1 0x0100 _/| /. /. > + * VAL3 _/| | / . / . > + * VAL5 _/| | | / . / . > + * VAL0 0x0000 -----_+- |-|-|------+------.------+------. half range > + * VAL4 _/ | | | | / . / . / > + * VAL2 _/ | | | | | / . / . / > + * INIT 0xff00 / | | | | | |/ ./ ./ Why does the first raising timer look different to the 2nd and 3rd? > + * | |________| .________. .________, .__ > + * A out ._| .____. |_.__| .____. |_.__| .____. |_.__| > + * B out .___| |____.____| |____.____| |___.____| > + * > + * Fron R.M. 34.4.1: center aligning the signals is not restricted > + * to symmetry around the zero count value, as any other number > + * also works. However, centering on zero provides the greatest range > + * in signed mode and simplifies the calculations. I don't understand that. I also tried with the reference manual, but failed. Isn't that hardware just a free-running counter between INIT and VAL1 and that for output A asserts the output at VAL2 and deasserts at VAL3? With that it would be more natural for the Linux waveform representation to use INIT =3D 0. I don't understand VAL0 though, maybe the counter reaching that values makes the hardware resample the registers? And there is a third output that is controlled by VAL0? > + * > + * Signed values are used to have the prescaler working, using unsigned > + * values no PWM signal get generated (hw bug ?) ? > + */ > + > + period_ns =3D wf->period_length_ns; > + > + if (period_ns =3D=3D 0) { > + *wfhw =3D (typeof(*wfhw)){ > + .enable =3D 0, > + }; > + return 0; > + } > + > + /* Check min and max nanoseconds limits now. */ > + > + if (period_ns < priv->period_ns_min + 1) { > + period_ns =3D priv->period_ns_min + 1; If period_ns_min cannot get configured, the name is wrong. > + return 1; You still need to setup *wfhw before returning 1. > + } > + > + if (period_ns > priv->period_ns_max) { > + dev_dbg(dev, "%s() period_ns %llu\n", __func__, period_ns); > + return -EINVAL; -EINVAL is wrong here. A too big request is supposed to be capped to period_ns_max. > + } > + > + /* > + * Setting prescaler here, same function returns correct divided rate > + * to calculate period and duty_cycle counts later on. > + */ > + rate =3D priv->rate; > + ret =3D pwm_mcf_calc_prescaler(period_ns, &rate, &prescaler); > + if (ret) > + return ret; > + > + /* > + * Minimal resolution in ns would be a number.reminder, so using a > + * mul_u64_u32_div operation, as period_ns * rate / NSEC_PER_SEC. I don't understand that comment, even when considering s/reminder/remainder/ > + */ > + val =3D mul_u64_u32_div(period_ns, rate, NSEC_PER_SEC); > + dev_dbg(dev, "%s() period val (count) %u\n", __func__, val); > + > + half_period =3D val >> 1; > + > + duty =3D mul_u64_u32_div(wf->duty_length_ns, rate, NSEC_PER_SEC); > + dev_dbg(dev, "%s() duty val (count) %u\n", __func__, duty); > + > + *wfhw =3D (typeof(*wfhw)){ > + .enable =3D 1, > + .prescaler =3D prescaler, > + .init =3D -half_period, > + .val0 =3D 0, > + .val1 =3D half_period - 1, > + .val2 =3D -half_period, > + .val3 =3D -half_period + duty, > + }; > + > + return 0; > +} > + > +static int pwm_mcf_round_waveform_fromhw(struct pwm_chip *chip, > + struct pwm_device *pwm, > + const void *_wfhw, > + struct pwm_waveform *wf) > +{ > + struct device *dev =3D pwmchip_parent(chip); > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + const struct pwm_mcf_waveform *wfhw =3D _wfhw; > + unsigned long rate; > + > + if (wfhw->enable) { > + rate =3D priv->rate >> wfhw->prescaler; > + > + *wf =3D (typeof(*wf)){ > + .period_length_ns =3D > + DIV_ROUND_UP_ULL((u64)(-wfhw->init << 1) > + * NSEC_PER_SEC, rate), > + .duty_length_ns =3D > + DIV_ROUND_UP_ULL((u64)(wfhw->val3 - wfhw->init) > + * NSEC_PER_SEC, rate), That is only true with val1 =3D -wfhw->init - 1 > + }; > + } else { > + *wf =3D (typeof(*wf)){ > + .period_length_ns =3D 0, > + .duty_length_ns =3D 0, No need to specify .duty_length_ns here. > + }; > + } > + > + dev_dbg(dev, "pwm#%u: INIT: %04x, VAL0: %04x, VAL1: %04x, VAL2: %04x\n", > + pwm->hwpwm, wfhw->init, wfhw->val0, wfhw->val1, wfhw->val2); > + dev_dbg(dev, "VAL3 %04x @%lu -> %lld/%lld [+%lld]\n", wfhw->val3, rate, > + wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); =46rom the nice ASCII art in pwm_mcf_round_waveform_tohw() returning duty_offset_ns =3D 0 always looks wrong. > + > + return 0; > +} > + > +static int pwm_mcf_read_waveform(struct pwm_chip *chip, struct pwm_devic= e *pwm, > + void *_wfhw) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + struct pwm_mcf_waveform *wfhw =3D _wfhw; > + unsigned int val, ch; > + int ret; > + > + ch =3D pwm->hwpwm; > + > + ret =3D clk_prepare_enable(priv->clk); > + if (ret) > + return ret; > + > + ret =3D regmap_read(priv->map, MCF_PWM_MCR, &val); > + if (ret) > + goto read_rw_exit; > + > + wfhw->enable =3D FIELD_GET(MCF_PWM_MCR_RUN, val) & BIT(ch); > + > + if (wfhw->enable) { > + ret =3D pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_INIT, &val); > + if (ret) > + goto read_rw_exit; > + wfhw->init =3D (s16)val; > + ret =3D pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL0, &val); > + if (ret) > + goto read_rw_exit; > + wfhw->val0 =3D (s16)val; > + ret =3D pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL1, &val); > + if (ret) > + goto read_rw_exit; > + wfhw->val1 =3D (s16)val; > + ret =3D pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL2, &val); > + if (ret) > + goto read_rw_exit; > + wfhw->val2 =3D (s16)val; > + ret =3D pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_VAL3, &val); > + if (ret) > + goto read_rw_exit; > + wfhw->val3 =3D (s16)val; > + ret =3D pwm_mcf_regmap_read(priv->map, ch, MCF_PWM_SM_CR1, &val); > + if (ret) > + goto read_rw_exit; > + wfhw->prescaler =3D FIELD_GET(MCF_PWM_SM_CR1_PRSC, val); > + } > + > +read_rw_exit: > + clk_disable_unprepare(priv->clk); > + > + return ret; > +} > + > +static int pwm_mcf_write_waveform(struct pwm_chip *chip, struct pwm_devi= ce *pwm, > + const void *_wfhw) > +{ > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + const struct pwm_mcf_waveform *wfhw =3D _wfhw; > + unsigned int ch, val; > + int ret; > + > + ch =3D pwm->hwpwm; > + > + if (!wfhw->enable) { > + if (pwm->state.enabled) > + pwm_mcf_disable(chip, pwm); > + > + return 0; > + } > + > + ret =3D clk_prepare_enable(priv->clk); > + if (ret) > + return ret; > + > + /* Be sure MCF LDOK is cleared before finally load any value. */ > + ret =3D pwm_mcf_clear_mcr_ldok(chip, pwm); > + if (ret) > + goto out; > + > + ret =3D pwm_mcf_regmap_write(priv->map, ch, MCF_PWM_SM_INIT, wfhw->init= ); > + if (ret) > + goto out; > + ret =3D pwm_mcf_regmap_write(priv->map, ch, MCF_PWM_SM_VAL0, wfhw->val0= ); > + if (ret) > + goto out; > + ret =3D pwm_mcf_regmap_write(priv->map, ch, MCF_PWM_SM_VAL1, wfhw->val1= ); > + if (ret) > + goto out; > + ret =3D pwm_mcf_regmap_write(priv->map, ch, MCF_PWM_SM_VAL2, wfhw->val2= ); > + if (ret) > + goto out; > + ret =3D pwm_mcf_regmap_write(priv->map, ch, MCF_PWM_SM_VAL3, wfhw->val3= ); > + if (ret) > + goto out; > + > + /* Force independent mode (using sub-ch A only). */ > + ret =3D pwm_mcf_regmap_set_bits(priv->map, ch, MCF_PWM_SM_CR2, > + MCF_PWM_SM_CR2_IND); > + if (ret) > + goto out; > + > + /* Set reload fixed at full cycle as more accurate mode. */ > + ret =3D pwm_mcf_regmap_set_bits(priv->map, ch, MCF_PWM_SM_CR1, > + MCF_PWM_SM_CR1_FULL); > + if (ret) > + goto out; > + > + val =3D FIELD_PREP(MCF_PWM_SM_CR1_PRSC, wfhw->prescaler); > + ret =3D pwm_mcf_regmap_set_bits(priv->map, ch, MCF_PWM_SM_CR1, val); > + if (ret) > + goto out; > + > + ret =3D pwm_mcf_enable(chip, pwm); > + > +#ifdef DEBUG > + pwm_mcf_dump_regs(chip, pwm); > +#endif > + return ret; > + > +out: > + clk_disable_unprepare(priv->clk); > + > + return ret; > +} > + > +static int pwm_mcf_init(struct pwm_chip *chip) > +{ > + struct device *dev =3D pwmchip_parent(chip); > + struct pwm_mcf *priv =3D pwm_mcf_from_chip(chip); > + int i, ret; > + > + ret =3D clk_prepare_enable(priv->clk); > + if (ret) > + return ret; > + > + /* > + * To fast-up operations, calculating here all the values that cannot > + * change after hw power on. > + */ > + ret =3D clk_rate_exclusive_get(priv->clk); > + if (ret) > + goto exit_init; > + > + priv->rate =3D clk_get_rate(priv->clk); > + if (priv->rate =3D=3D 0) { > + ret =3D -EINVAL; > + goto exit_init; > + } > + > + /* > + * Calculating fixed minimum/maximum nanosecs period possible, > + * considering the possible maximum prescaler value as 7 (fclk/128). > + */ > + priv->period_ns_min =3D div_u64(NSEC_PER_SEC, priv->rate) + 1; > + priv->period_ns_max =3D mul_u64_u32_div(65535ULL, NSEC_PER_SEC, > + (priv->rate >> 7)); > + > + dev_dbg(dev, "%s() period limits: min %llu, max %llu\n", > + __func__, priv->period_ns_min, priv->period_ns_max); > + > + /* > + * Set independent mode for all submodules: > + * default is "complementary", and on apply only PWM "A" is used, > + * so in complementary mode, for safety reason (motor control with > + * a wrong complementary drive) the PWM is not started at all on RUN. > + */ > + for (i =3D 0, ret =3D 0; i < MCF_PWM_SUBMODS && ret =3D=3D 0; i++) { > + ret =3D pwm_mcf_regmap_set_bits(priv->map, i, MCF_PWM_SM_CR2, > + MCF_PWM_SM_CR2_IND); > + } > + > +exit_init: > + clk_disable_unprepare(priv->clk); > + > + return ret; > +} > + > +static const struct pwm_ops pwm_mcf_ops =3D { > + .sizeof_wfhw =3D sizeof(struct pwm_mcf_waveform), > + .round_waveform_tohw =3D pwm_mcf_round_waveform_tohw, > + .round_waveform_fromhw =3D pwm_mcf_round_waveform_fromhw, > + .read_waveform =3D pwm_mcf_read_waveform, > + .write_waveform =3D pwm_mcf_write_waveform, > +}; > + > +static int pwm_mcf_probe(struct platform_device *pdev) > +{ > + struct device *dev =3D &pdev->dev; > + struct pwm_chip *chip; > + struct pwm_mcf *priv; > + void __iomem *regs; > + int ret; > + > + chip =3D devm_pwmchip_alloc(dev, MCF_PWM_SUBMODS, sizeof(*priv)); > + if (IS_ERR(chip)) > + return dev_err_probe(dev, PTR_ERR(chip), > + "Failed to allocate priv memory.\n"); > + > + priv =3D pwm_mcf_from_chip(chip); > + > + regs =3D devm_platform_ioremap_resource(pdev, 0); > + if (IS_ERR(regs)) > + return dev_err_probe(dev, PTR_ERR(regs), > + "Failed to get io regs\n"); > + > + priv->map =3D devm_regmap_init_mmio(dev, regs, &pwm_mcf_regmap_config); > + if (IS_ERR(priv->map)) > + return dev_err_probe(dev, PTR_ERR(priv->map), > + "Failed mmio regmap init\n"); > + > + priv->clk =3D devm_clk_get(dev, "mcfpwm"); > + if (IS_ERR(priv->clk)) > + return dev_err_probe(dev, PTR_ERR(priv->clk), > + "Failed getting clock\n"); > + > + chip->ops =3D &pwm_mcf_ops; > + > + ret =3D pwm_mcf_init(chip); > + if (ret) > + return dev_err_probe(dev, ret, "Failed to initialize PWM\n"); > + > + ret =3D devm_pwmchip_add(dev, chip); > + if (ret < 0) > + return dev_err_probe(dev, ret, "Failed to register pwmchip\n"); > + > + return 0; > +} > + > +static const struct platform_device_id pwm_mcf_ids[] =3D { > + { .name =3D "mcfpwm" }, > + { } > +}; > + > +static struct platform_driver pwm_mcf_driver =3D { > + .driver =3D { > + .name =3D "pwm-mcf", > + }, > + .probe =3D pwm_mcf_probe, > + .id_table =3D pwm_mcf_ids, > +}; > +module_platform_driver(pwm_mcf_driver); > + > +MODULE_DESCRIPTION("mcf54415 Motor Control Pulse Width Modulation driver= "); > +MODULE_LICENSE("GPL"); > +MODULE_AUTHOR("Angelo Dureghello "); Best regards Uwe --festxwceb6xoebyw Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEP4GsaTp6HlmJrf7Tj4D7WH0S/k4FAmsRb3gACgkQj4D7WH0S /k768Qf/TtlhLFsb4NshWZsHfT1byRB3wtQdHC34I2KHI2GBNHB6Tb0ZanCTHaVX +Pk4yncjgIDF8RcgqN3XGER7aWb3dxgEMveJ80bw/RYjeiw60J7Bf+7t5rZo045P QzSnBaEEFAWENLIiUM08KlP+IX3tJMLUIr18tiVA5FdvgZHDXVQPVCGKU6i4myTz kfzjzP+gYLlnH0tQufC0sFx9Zbr4dB0yKbJl+cnP4CX22A83Iz3Fw4qrAgpwTT2N 5BRLqjPWt/R/nKtXBiUU3LBAh/jqHl2maYNnK4lCNtBlRU3Gdd/bNWER5JaXqb1a PSwPP0CbjZI0fBL0RPe8ua+rQOxt2w== =CrAl -----END PGP SIGNATURE----- --festxwceb6xoebyw--