Re: [PATCH v2 2/2] iio: magnetometer: add Infineon TLE493D-W2B6 driver

From: Jonathan Cameron

Date: Sat Nov 21 2026 - 14:59:45 EST


On Sat, 21 Nov 2026 11:25:11 +0530
Viral Patel <viral8854@xxxxxxxxx> wrote:

> Add a driver for the Infineon TLE493D-W2B6, a 3D Hall effect magnetic
> sensor with 12-bit resolution per axis and an integrated temperature
> sensor, accessed over I2C.

This either suffers from copying LLM generated commit messages or
is pretty much what the LLM came up with. They are way way too verbose
as they generate something that hides the important details largely
defeating the point of a commit message. I'm only interested in a
couple of lines of 'what' followed by any 'why' stuff for less obvious
decisions. A starting point of telling the LLM it is being to verbose
and to keep to these aspects tends to help, but then it needs the human
to edit even that for the bits that actually matter!

>
> The sensor is run in master-controlled mode. Each measurement rewrites
> the Config register with the "trigger after write" bits set in the
> register address byte, which starts a single conversion, and then polls
> registers 0x00 to 0x06 until Diag reports a completed conversion with a
> new frame counter. Results with a fuse parity error, test mode flag or
> bad bus parity are rejected. FP and CP are recomputed on every write of
> MOD1 and Config, as a wrong FP makes the sensor release the bus until it
> is reset.

This is too much detail for a commit message. I'd just keep the first
sentence about master-controlled mode. The rest should be clear from
the code and if it isn't comments belong down there, not in a commit
message no one normally sees.

>
> The /INT output shares a pin with SCL, so the interrupt is disabled and
> so is clock stretching. The sensor's reset sequence is four SMBus Quick
> commands, so it is sent during probe only if the adapter supports
> I2C_FUNC_SMBUS_QUICK.

First sentence of this good. Second one see the code, no need to say.

>
> Raw I2C transfers are used rather than regmap: once PR is set a plain
> read always starts at register 0x00, and the register address byte of a
> write carries trigger bits.
>
> The X, Y and Z channels and the temperature channel are available
> through sysfs and through a triggered buffer with a software timestamp.
> Wake-up, self-test, low-power and fast modes, sampling frequency control
> and the short range are not supported yet.

Don't care about what you didn't support for a commit message. What
you do support can be summarised as something like.

Both sysfs and triggered buffer capture of channels is supported.

>
> This is not added to the tlv493d driver: apart from the X/Y/Z bit
> packing, the two parts differ in write format, read protocol after
> reset, register map and parity scheme.

That is cover letter detail. Not useful in here unless it was really
borderline so discussion likely.

>
> Based on the TLE493D-W2B6 User Manual v1.11 and Datasheet v1.2. The
> user manual is provided by Infineon after a free registration:
> https://www.infineon.com/gated/infineon-tle493d-w2b6-usermanual-en_dc6d4e62-cde1-4d4b-87bb-c3368e25e35c

Add this manual as a second datasheet tag and drop that paragraph.

>
> Datasheet: https://www.infineon.com/assets/row/public/documents/30/49/infineon-tle493d-w2b6-datasheet-en.pdf
> Signed-off-by: Viral Patel <viral8854@xxxxxxxxx>

A lot of the comments below are more things that I thought
about asking for and then why I didn't! :)

I want to give others time to comment anyway so wouldn't have picked it up
today, but if it wasn't for the commit message I might just have considere
picking it up later in the week (possibly tweaking a few trivial things
whilst applying).

Thanks,

Jonathan


> diff --git a/drivers/iio/magnetometer/tle493d.c b/drivers/iio/magnetometer/tle493d.c
> new file mode 100644
> index 000000000000..62b7e7ea7ec0
> --- /dev/null
> +++ b/drivers/iio/magnetometer/tle493d.c


> +#define TLE493D_RESET_DELAY_US 30
> +/* A full range conversion takes about 175 us */
> +#define TLE493D_POLL_US 100
> +#define TLE493D_TIMEOUT_US 1000

That's a generous margin. I suppose it's still only a
millisec so who cares!


> +/*
> + * Write one register, updating its parity bit first. Only MOD1 and Config
> + * are written by this driver. The sensor drops off the bus until reset if
> + * MOD1 is written with a wrong FP.
> + */
> +static int tle493d_write(struct tle493d_data *data, u8 reg, u8 val, u8 trig)
> +{
> + u8 buf[2];
> + int ret;
> +
> + switch (reg) {
> + case TLE493D_REG_MOD1:
> + val = tle493d_mod1_fp(val, data->regs[TLE493D_REG_MOD2]);

Why reuse val? I'd just write buf[1] directly here then no need to do
so below.

> + break;
> + case TLE493D_REG_CONFIG:
> + val = tle493d_config_cp(data->regs, val);
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + buf[0] = FIELD_PREP(TLE493D_ADDR_TRIG, trig) |
> + FIELD_PREP(TLE493D_ADDR_REG, reg);

Given I'm suggesting writing buf[1] in the switch, maybe move this
line ahead of that as seems weird to set buf[1] before buf[0].

> + buf[1] = val;
> +
> + ret = i2c_master_send(data->client, buf, sizeof(buf));
> + if (ret < 0)
> + return ret;
> + if (ret != sizeof(buf))
> + return -EIO;
> +
> + data->regs[reg] = val;
> +
> + return 0;
> +}


> +static irqreturn_t tle493d_trigger_handler(int irq, void *p)
> +{
> + struct iio_poll_func *pf = p;
> + struct iio_dev *indio_dev = pf->indio_dev;
> + struct tle493d_data *data = iio_priv(indio_dev);
> + u8 buf[TLE493D_DATA_LEN];
> + struct {
> + s16 magn[3];
> + u16 temp;
> + aligned_s64 timestamp;
> + } scan = { };

I messed around with a named structure type here then designated
initialiser below but it ends up messier, so this is good.
Just left a comment here for any other reviewers who wonder
the same!

> + int ret;
> +
> + mutex_lock(&data->lock);
> + ret = tle493d_measure(data, buf);
> + mutex_unlock(&data->lock);
> + if (ret)
> + goto out;
> +
> + scan.magn[0] = tle493d_get_channel_data(buf, TLE493D_AXIS_X);
> + scan.magn[1] = tle493d_get_channel_data(buf, TLE493D_AXIS_Y);
> + scan.magn[2] = tle493d_get_channel_data(buf, TLE493D_AXIS_Z);
> + scan.temp = tle493d_get_channel_data(buf, TLE493D_TEMPERATURE);
> + iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), pf->timestamp);
> +
> +out:
> + iio_trigger_notify_done(indio_dev->trig);
> +
> + return IRQ_HANDLED;
> +}



> +static int Andreas Kemnade <andreas@xxxxxxxxxxxx>(struct tle493d_data *data, unsigned int iicadr)
> +{
> + struct device *dev = &data->client->dev;
> + unsigned int hwv, id;
> + u8 mod1;
> + int ret;
> +
> + /*
> + * After reset the sensor uses a 2-byte read protocol which plain I2C
> + * reads cannot handle, so MOD1 has to be written before any read. MOD2
> + * is not known yet and FP is computed with its reset value, PRD = 0.
> + * Clock stretching and the interrupt are disabled because /INT shares
> + * the SCL pin.
> + *
> + * Every MOD1 write also sets IICadr, which selects the address the
> + * sensor responds on, so it must match the address the device is
> + * already on or the sensor moves to another address.
> + */

See below for where I'm requesting a cross reference to this comment.
I wrote a nice paragraph asking for basically this text before scrolling up
and seeing it here :)


> +
> +static int tle493d_probe(struct i2c_client *client)
> +{
> + struct device *dev = &client->dev;
> + struct tle493d_data *data;
> + struct iio_dev *indio_dev;
> + unsigned int iicadr;
> + int ret;
> +
> + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> + return dev_err_probe(dev, -EOPNOTSUPP, "plain I2C transfers not supported\n");
> +
> + /* MOD1 writes need the IICadr value matching the current address */
> + for (iicadr = 0; iicadr < ARRAY_SIZE(tle493d_i2c_addrs); iicadr++) {
> + if (client->addr == tle493d_i2c_addrs[iicadr])
> + break;
> + }
> + if (iicadr == ARRAY_SIZE(tle493d_i2c_addrs))
> + return dev_err_probe(dev, -EINVAL, "unsupported I2C address 0x%02x\n",
> + client->addr);
Add a reference here back to the more detailed comment above.
/* Unable to support other addresses, see tl2493d_init() */

Otherwise this smells to much like an over restrictive limitation
on devices to ones we know about. In this particular case it is
a fundamental constraint!



> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> + if (!indio_dev)
> + return -ENOMEM;

>