Re: [PATCH 4/4] mfd: ocelot: Read LAN9645x registers in a single SPI transfer
From: Jens Emil Schulz Østergaard
Date: Thu Dec 03 2026 - 05:54:37 EST
On Wed Dec 2, 2026 at 2:35 PM CET, Lee Jones wrote:
> On Mon, 23 Nov 2026, Jens Emil Schulz Østergaard wrote:
[...]
>> +static int ocelot_spi_regmap_xfer_read(void *context, const void *reg,
>> + size_t reg_size, void *val,
>> + size_t val_size)
>> +{
>> + struct ocelot_spi_regmap_ctx *ctx = context;
>> + struct spi_transfer xfer = {};
>> + struct ocelot_ddata *ddata;
>> + const u8 *regu8 = reg;
>> + u8 *buf = ctx->buf;
>> + u8 *valu8 = val;
>> + size_t off;
>> + int err;
>> +
>> + ddata = dev_get_drvdata(ctx->dev);
>
> Define and initialise this at the top.
Will do.
>> +
>> + if (reg_size != OCELOT_SPI_ADDR_SIZE || val_size != OCELOT_SPI_VAL_SIZE ||
>> + ddata->spi_padding_bytes > OCELOT_SPI_MAX_PADDING_BYTES)
>> + return -EINVAL;
>> +
>> + off = OCELOT_SPI_ADDR_SIZE + ddata->spi_padding_bytes;
>> +
>> + buf[0] = regu8[0];
>> + buf[1] = regu8[1];
>> + buf[2] = regu8[2];
>> + memset(&buf[OCELOT_SPI_ADDR_SIZE], 0,
>> + ddata->spi_padding_bytes + OCELOT_SPI_VAL_SIZE);
>
> Why not use memcpy()? Or start from a pre-initialised space?
The address will be copied with memcpy() in v2.
The memset() is there because the buffer is both tx_buf and rx_buf, so
every read overwrites the padding bytes with the received data. In v2
the transmit and receive buffers are separate. The transmit buffer is
zeroed once when it is allocated, and a read only copies the address
into it.
[...]
>> + valu8[0] = buf[off + 0];
>> + valu8[1] = buf[off + 1];
>> + valu8[2] = buf[off + 2];
>> + valu8[3] = buf[off + 3];
>
> memcpy()?
Will do.
[...]
>> - return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, ®map_config);
>> + if (!ocelot_spi_single_xfer_read(spi, spi_get_device_match_data(spi)))
>> + return devm_regmap_init(dev, &ocelot_spi_regmap_bus, dev, ®map_config);
>
> Needs a comment.
I will add one.
>
>> + ctx = devm_kzalloc_obj(dev, *ctx);
>> + if (!ctx)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + ctx->dev = dev;
>> +
>> + return devm_regmap_init(dev, &ocelot_spi_regmap_xfer_bus, ctx, ®map_config);
>> }
>> EXPORT_SYMBOL_NS(ocelot_spi_init_regmap, "MFD_OCELOT_SPI");
>>
>> @@ -296,7 +384,7 @@ static int ocelot_spi_probe(struct spi_device *spi)
>> spi->max_speed_hz, OCELOT_SPI_MAX_PADDING_BYTES);
>>
>> dummy_len = max(padding, data->reset_padding_bytes);
>> - if (dummy_len) {
>> + if (!ocelot_spi_single_xfer_read(spi, data) && dummy_len) {
>
> Comment.
I will add one.