mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: "Jens Emil Schulz Østergaard" <jensemil.schulzostergaard@microchip.com>
Cc: Colin Foster <colin.foster@in-advantage.com>,
	UNGLinuxDriver@microchip.com, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Richard Cochran <richardcochran@gmail.com>,
	mfd@lists.linux.dev, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH 1/4] mfd: ocelot: Select the chip through match data
Date: Wed, 2 Dec 2026 12:34:38 +0000	[thread overview]
Message-ID: <179568560270.32537.14032655101111129070@kernel.org> (raw)
In-Reply-To: <20261123-mfd-ocelot-add-lan9645x-support-v1-1-567d811fe1de@microchip.com>

On Mon, 23 Nov 2026, Jens Emil Schulz Østergaard wrote:

> The SPI interface bootstrap, the chip reset and the child devices are
> hardcoded for the VSC7512. Describe the serial interface configuration
> target, its registers, the IF_CFGSTAT readback check and the padding
> byte calculation in SPI match data, and select the soft reset register
> and the child devices in the core by the chip it names. This prepares
> for adding the LAN9645x switch family.

I support the idea, just not the implementation.

> The VSC7512 soft reset defines get a VSC7512 prefix.
>
> The probe now fails for a device bound without match data, otherwise
> there is no functional change.
>
> Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
> ---
>  drivers/mfd/ocelot-core.c | 48 +++++++++++++++++++++++------
>  drivers/mfd/ocelot-spi.c  | 77 +++++++++++++++++++++++++++++++++--------------
>  drivers/mfd/ocelot.h      |  6 ++++
>  3 files changed, 100 insertions(+), 31 deletions(-)
>
> diff --git a/drivers/mfd/ocelot-core.c b/drivers/mfd/ocelot-core.c
> index 41aff2708854..2f53122fb964 100644
> --- a/drivers/mfd/ocelot-core.c
> +++ b/drivers/mfd/ocelot-core.c
> @@ -28,9 +28,9 @@
>
>  #include "ocelot.h"
>
> -#define REG_GCB_SOFT_RST		0x0008
> +#define VSC7512_GCB_SOFT_RST		0x0008
>
> -#define BIT_SOFT_CHIP_RST		BIT(0)
> +#define VSC7512_SOFT_CHIP_RST		BIT(0)
>
>  #define VSC7512_MIIM0_RES_START		0x7107009c
>  #define VSC7512_MIIM1_RES_START		0x710700c0
> @@ -84,11 +84,23 @@
>  #define VSC7512_GCB_RST_SLEEP_US	100
>  #define VSC7512_GCB_RST_TIMEOUT_US	100000
>
> +struct ocelot_soft_rst {
> +	unsigned int reg;
> +	u32 mask;
> +};
> +
> +static const struct ocelot_soft_rst ocelot_soft_rst[] = {
> +	[OCELOT_CHIP_VSC7512] = {
> +		.reg = VSC7512_GCB_SOFT_RST,
> +		.mask = VSC7512_SOFT_CHIP_RST,
> +	},
> +};
> +
>  static int ocelot_gcb_chip_rst_status(struct ocelot_ddata *ddata)
>  {
>  	int val, err;
>
> -	err = regmap_read(ddata->gcb_regmap, REG_GCB_SOFT_RST, &val);
> +	err = regmap_read(ddata->gcb_regmap, ocelot_soft_rst[ddata->chip].reg, &val);
>  	if (err)
>  		return err;
>
> @@ -98,15 +110,18 @@ static int ocelot_gcb_chip_rst_status(struct ocelot_ddata *ddata)
>  int ocelot_chip_reset(struct device *dev)
>  {
>  	struct ocelot_ddata *ddata = dev_get_drvdata(dev);
> +	const struct ocelot_soft_rst *rst;
>  	int ret, val;
>
> +	rst = &ocelot_soft_rst[ddata->chip];
> +
>  	/*
>  	 * Reset the entire chip here to put it into a completely known state.
>  	 * Other drivers may want to reset their own subsystems. The register
>  	 * self-clears, so one write is all that is needed and wait for it to
>  	 * clear.
>  	 */
> -	ret = regmap_write(ddata->gcb_regmap, REG_GCB_SOFT_RST, BIT_SOFT_CHIP_RST);
> +	ret = regmap_write(ddata->gcb_regmap, rst->reg, rst->mask);
>  	if (ret)
>  		return ret;
>
> @@ -197,6 +212,18 @@ static const struct mfd_cell vsc7512_devs[] = {
>  	},
>  };
>
> +struct ocelot_cells {
> +	const struct mfd_cell *cells;
> +	unsigned int num_cells;
> +};
> +
> +static const struct ocelot_cells ocelot_cells[] = {
> +	[OCELOT_CHIP_VSC7512] = {
> +		.cells = vsc7512_devs,
> +		.num_cells = ARRAY_SIZE(vsc7512_devs),
> +	},
> +};

Please select the 'mfd_cell' array and its size using a switch() statement on
'ddata->chip' inside ocelot_core_init() rather than introducing a wrapper struct
and an array of arrays.

Also, we already have helpers for you in:

  include/linux/mfd/core.h

>  static void ocelot_core_try_add_regmap(struct device *dev,
>  				       const struct resource *res)
>  {
> @@ -217,14 +244,17 @@ static void ocelot_core_try_add_regmaps(struct device *dev,
>
>  int ocelot_core_init(struct device *dev)
>  {
> -	int i, ndevs;
> +	struct ocelot_ddata *ddata = dev_get_drvdata(dev);
> +	const struct ocelot_cells *c;
> +	int i;
>
> -	ndevs = ARRAY_SIZE(vsc7512_devs);
> +	c = &ocelot_cells[ddata->chip];
>
> -	for (i = 0; i < ndevs; i++)
> -		ocelot_core_try_add_regmaps(dev, &vsc7512_devs[i]);
> +	for (i = 0; i < c->num_cells; i++)

for (int i = 0; ...

> +		ocelot_core_try_add_regmaps(dev, &c->cells[i]);
>
> -	return devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, vsc7512_devs, ndevs, NULL, 0, NULL);
> +	return devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, c->cells, c->num_cells,
> +				    NULL, 0, NULL);
>  }
>  EXPORT_SYMBOL_NS(ocelot_core_init, "MFD_OCELOT");
>
> diff --git a/drivers/mfd/ocelot-spi.c b/drivers/mfd/ocelot-spi.c
> index 52bde8d4daf3..e803d0363e62 100644
> --- a/drivers/mfd/ocelot-spi.c
> +++ b/drivers/mfd/ocelot-spi.c
> @@ -50,7 +50,42 @@ static const struct resource vsc7512_gcb_resource =
>  			     VSC7512_CHIP_REGS_RES_SIZE,
>  			     "devcpu_gcb_chip_regs");
>
> -static int ocelot_spi_initialize(struct device *dev)
> +struct ocelot_spi_data {

May I suggest that we ditch this pdata strategy and assign these based
off of an integer that you pass through .data?

> +	enum ocelot_chip chip;
> +	const struct resource *cpuorg_res;
> +	unsigned int if_ctrl;
> +	unsigned int if_cfgstat;
> +	u32 if_cfgstat_si;
> +	const struct resource *gcb_res;
> +	int (*padding_bytes)(u32 max_speed_hz);

Function pointers at driver-level give me the ick.

Again, if there is no other way to do this other than in a function, you
can call it from within the correct case of the switch() suggested
above.  This way you can keep all of the chip specific logic together in
a coherent space.

> +};
> +
> +static int vsc7512_spi_padding_bytes(u32 max_speed_hz)
> +{
> +	if (max_speed_hz <= 500000)
> +		return 0;
> +
> +	/*
> +	 * Calculation taken from the manual for IF_CFGSTAT:IF_CFG. Register
> +	 * access time is 1us, so we need to configure and send out enough
> +	 * padding bytes between the read request and data transmission that
> +	 * lasts at least 1 microsecond.
> +	 */
> +	return 1 + (max_speed_hz / HZ_PER_MHZ + 2) / 8;
> +}
> +
> +static const struct ocelot_spi_data vsc7512_spi_data = {
> +	.chip = OCELOT_CHIP_VSC7512,
> +	.cpuorg_res = &vsc7512_dev_cpuorg_resource,
> +	.if_ctrl = REG_DEV_CPUORG_IF_CTRL,
> +	.if_cfgstat = REG_DEV_CPUORG_IF_CFGSTAT,
> +	.if_cfgstat_si = CFGSTAT_IF_NUM_SI,
> +	.gcb_res = &vsc7512_gcb_resource,
> +	.padding_bytes = vsc7512_spi_padding_bytes,
> +};
> +
> +static int ocelot_spi_initialize(struct device *dev,
> +				 const struct ocelot_spi_data *data)
>  {
>  	struct ocelot_ddata *ddata = dev_get_drvdata(dev);
>  	u32 val, check;
> @@ -80,7 +115,7 @@ static int ocelot_spi_initialize(struct device *dev)
>  	 * | 22222233 | 11112222 |   111111 |          |
>  	 * | 45678901 | 67890123 | 89012345 | 01234567 |
>  	 */
> -	err = regmap_write(ddata->cpuorg_regmap, REG_DEV_CPUORG_IF_CTRL, val);
> +	err = regmap_write(ddata->cpuorg_regmap, data->if_ctrl, val);
>  	if (err)
>  		return err;
>
> @@ -90,7 +125,7 @@ static int ocelot_spi_initialize(struct device *dev)
>  	 * first payload bit is shifted out too quickly, the read will fail.
>  	 */
>  	val = ddata->spi_padding_bytes;
> -	err = regmap_write(ddata->cpuorg_regmap, REG_DEV_CPUORG_IF_CFGSTAT, val);
> +	err = regmap_write(ddata->cpuorg_regmap, data->if_cfgstat, val);
>  	if (err)
>  		return err;
>
> @@ -109,9 +144,9 @@ static int ocelot_spi_initialize(struct device *dev)
>  	 * the registers are being accessed. Since we're accessing them via the
>  	 * serial interface, it must return IF_NUM_SI.
>  	 */
> -	check = val | CFGSTAT_IF_NUM_SI;
> +	check = val | data->if_cfgstat_si;
>
> -	err = regmap_read(ddata->cpuorg_regmap, REG_DEV_CPUORG_IF_CFGSTAT, &val);
> +	err = regmap_read(ddata->cpuorg_regmap, data->if_cfgstat, &val);
>  	if (err)
>  		return err;
>
> @@ -196,28 +231,26 @@ EXPORT_SYMBOL_NS(ocelot_spi_init_regmap, "MFD_OCELOT_SPI");
>
>  static int ocelot_spi_probe(struct spi_device *spi)
>  {
> +	const struct ocelot_spi_data *data;
>  	struct device *dev = &spi->dev;
>  	struct ocelot_ddata *ddata;
>  	struct regmap *r;
>  	int err;
>
> +	data = spi_get_device_match_data(spi);
> +	if (!data)
> +		return -ENODEV;
> +
>  	ddata = devm_kzalloc_obj(dev, *ddata);
>  	if (!ddata)
>  		return -ENOMEM;
>
> +	ddata->chip = data->chip;
> +
>  	spi_set_drvdata(spi, ddata);
>
> -	if (spi->max_speed_hz <= 500000) {
> -		ddata->spi_padding_bytes = 0;
> -	} else {
> -		/*
> -		 * Calculation taken from the manual for IF_CFGSTAT:IF_CFG.
> -		 * Register access time is 1us, so we need to configure and send
> -		 * out enough padding bytes between the read request and data
> -		 * transmission that lasts at least 1 microsecond.
> -		 */
> -		ddata->spi_padding_bytes = 1 + (spi->max_speed_hz / HZ_PER_MHZ + 2) / 8;
> -
> +	ddata->spi_padding_bytes = data->padding_bytes(spi->max_speed_hz);
> +	if (ddata->spi_padding_bytes) {
>  		ddata->dummy_buf = devm_kzalloc(dev, ddata->spi_padding_bytes, GFP_KERNEL);
>  		if (!ddata->dummy_buf)
>  			return -ENOMEM;
> @@ -229,13 +262,13 @@ static int ocelot_spi_probe(struct spi_device *spi)
>  	if (err)
>  		return dev_err_probe(&spi->dev, err, "Error performing SPI setup\n");
>
> -	r = ocelot_spi_init_regmap(dev, &vsc7512_dev_cpuorg_resource);
> +	r = ocelot_spi_init_regmap(dev, data->cpuorg_res);
>  	if (IS_ERR(r))
>  		return PTR_ERR(r);
>
>  	ddata->cpuorg_regmap = r;
>
> -	r = ocelot_spi_init_regmap(dev, &vsc7512_gcb_resource);
> +	r = ocelot_spi_init_regmap(dev, data->gcb_res);
>  	if (IS_ERR(r))
>  		return PTR_ERR(r);
>
> @@ -246,7 +279,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
>  	 * This must be done before calling init, and after a chip reset is
>  	 * performed.
>  	 */
> -	err = ocelot_spi_initialize(dev);
> +	err = ocelot_spi_initialize(dev, data);
>  	if (err)
>  		return dev_err_probe(dev, err, "Error initializing SPI bus\n");
>
> @@ -258,7 +291,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
>  	 * A chip reset will clear the SPI configuration, so it needs to be done
>  	 * again before we can access any registers.
>  	 */
> -	err = ocelot_spi_initialize(dev);
> +	err = ocelot_spi_initialize(dev, data);
>  	if (err)
>  		return dev_err_probe(dev, err, "Error initializing SPI bus after reset\n");
>
> @@ -270,13 +303,13 @@ static int ocelot_spi_probe(struct spi_device *spi)
>  }
>
>  static const struct spi_device_id ocelot_spi_ids[] = {
> -	{ .name = "vsc7512" },
> +	{ .name = "vsc7512", .driver_data = (kernel_ulong_t)&vsc7512_spi_data },
>  	{ }
>  };
>  MODULE_DEVICE_TABLE(spi, ocelot_spi_ids);
>
>  static const struct of_device_id ocelot_spi_of_match[] = {
> -	{ .compatible = "mscc,vsc7512" },
> +	{ .compatible = "mscc,vsc7512", .data = &vsc7512_spi_data },
>  	{ }
>  };
>  MODULE_DEVICE_TABLE(of, ocelot_spi_of_match);
> diff --git a/drivers/mfd/ocelot.h b/drivers/mfd/ocelot.h
> index b8bc2f1486e2..92b5b893ff9b 100644
> --- a/drivers/mfd/ocelot.h
> +++ b/drivers/mfd/ocelot.h
> @@ -10,8 +10,13 @@ struct device;
>  struct regmap;
>  struct resource;
>
> +enum ocelot_chip {
> +	OCELOT_CHIP_VSC7512,
> +};
> +
>  /**
>   * struct ocelot_ddata - Private data for an external Ocelot chip
> + * @chip:		Chip family member.
>   * @gcb_regmap:		General Configuration Block regmap. Used for
>   *			operations like chip reset.
>   * @cpuorg_regmap:	CPU Device Origin Block regmap. Used for operations
> @@ -24,6 +29,7 @@ struct resource;
>   *			data of a SPI read operation.
>   */
>  struct ocelot_ddata {
> +	enum ocelot_chip chip;
>  	struct regmap *gcb_regmap;
>  	struct regmap *cpuorg_regmap;
>  	int spi_padding_bytes;
>
> -- 
> 2.52.0
>

-- 
Lee Jones

  reply	other threads:[~2026-12-02 12:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:25 [PATCH 0/4] mfd: ocelot: Support the LAN9645x switch family Jens Emil Schulz Østergaard
2026-11-23 13:25 ` [PATCH 1/4] mfd: ocelot: Select the chip through match data Jens Emil Schulz Østergaard
2026-12-02 12:34   ` Lee Jones [this message]
2026-11-23 13:25 ` [PATCH 2/4] dt-bindings: mfd: Add the Microchip LAN9645x switch family Jens Emil Schulz Østergaard
2026-11-23 13:25 ` [PATCH 3/4] mfd: ocelot: Add support for the " Jens Emil Schulz Østergaard
2026-12-02 13:28   ` Lee Jones
2026-11-23 13:25 ` [PATCH 4/4] mfd: ocelot: Read LAN9645x registers in a single SPI transfer Jens Emil Schulz Østergaard
2026-12-02 13:35   ` Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179568560270.32537.14032655101111129070@kernel.org \
    --to=lee@kernel.org \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=colin.foster@in-advantage.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jensemil.schulzostergaard@microchip.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®