Re: [PATCH v2 2/2] iio: magnetometer: add Infineon TLE493D-W2B6 driver
From: Andy Shevchenko
Date: Mon Nov 23 2026 - 03:45:32 EST
On Sat, Nov 21, 2026 at 11:25:11AM +0530, Viral Patel 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.
>
> 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.
>
> 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.
>
> 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.
>
> 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.
>
> 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
Make this a formal Link tag in a form of: Link: $URL [1], and use [1] in
the text above.
> Datasheet: https://www.infineon.com/assets/row/public/documents/30/49/infineon-tle493d-w2b6-datasheet-en.pdf
> Signed-off-by: Viral Patel <viral8854@xxxxxxxxx>
> + * Driver for the Infineon TLE493D-W2B6 3D Magnetic Sensor
...
> +#include <linux/array_size.h>
> +#include <linux/bitfield.h>
> +#include <linux/bitops.h>
> +#include <linux/bits.h>
It's guaranteed by bitops.h.
> +#include <linux/cleanup.h>
> +#include <linux/delay.h>
> +#include <linux/dev_printk.h>
> +#include <linux/errno.h>
> +#include <linux/i2c.h>
> +#include <linux/iopoll.h>
> +#include <linux/irqreturn.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/types.h>
...
> +/* A full range conversion takes about 175 us */
Then why not starting with it?
Either elaborate in the comment why smaller is chosen, or use the mentioned one.
> +#define TLE493D_POLL_US 100
> +#define TLE493D_TIMEOUT_US 1000
(1 * USEC_PER_MSEC)
This will require to include time.h.
...
> +/* FP makes MOD1 together with MOD2.PRD odd parity */
> +static u8 tle493d_mod1_fp(u8 mod1, u8 mod2)
> +{
> + unsigned int ones;
> +
> + mod1 &= ~TLE493D_MOD1_FP;
> + ones = hweight8(mod1) + hweight8(mod2 & TLE493D_MOD2_PRD);
> + if (!(ones & 1))
When I see ' & 1' I want to immediately have it as ' & BIT(0)' but it seems
wrong semantically here, so, why not use ' % 2' instead? The compiler will make
it optimal in the generated code if not the same.
> + mod1 |= TLE493D_MOD1_FP;
> +
> + return mod1;
> +}
...
> +/* CP makes registers 0x07 to 0x10 odd parity, ignoring WA, TST and PH */
> +static u8 tle493d_config_cp(const u8 *regs, u8 config)
> +{
> + unsigned int ones = 0;
> + unsigned int i;
> +
> + for (i = TLE493D_REG_XL; i < TLE493D_REG_WU; i++)
for (unsigned int i = TLE493D_REG_XL; i < TLE493D_REG_WU; i++)
> + ones += hweight8(regs[i]);
> + ones += hweight8(regs[TLE493D_REG_WU] & ~TLE493D_WU_WA);
> + ones += hweight8(regs[TLE493D_REG_TMODE] & ~TLE493D_TMODE_TST);
> + ones += hweight8(regs[TLE493D_REG_TPHASE] & ~TLE493D_TPHASE_PH);
> +
> + config &= ~TLE493D_CONFIG_CP;
> + ones += hweight8(config);
> + if (!(ones & 1))
As per above.
> + config |= TLE493D_CONFIG_CP;
> +
> + return config;
> +}
Similar comments apply elsewhere in the code.
...
> +static bool tle493d_conversion_done(const u8 *buf, u8 last_frm)
> +{
> + u8 diag = buf[TLE493D_REG_DIAG];
> +
> + return FIELD_GET(TLE493D_DIAG_PD0, diag) &&
> + FIELD_GET(TLE493D_DIAG_PD3, diag) &&
> + FIELD_GET(TLE493D_DIAG_FRM, diag) != last_frm;
vvv (see below)
> +}
> +
> +static int tle493d_measure(struct tle493d_data *data, u8 *buf)
> +{
> + u8 last_frm = FIELD_GET(TLE493D_DIAG_FRM, data->regs[TLE493D_REG_DIAG]);
This is the same as per above, perhaps split a helper for this field?
> + u8 diag;
> + int ret, err;
> +
> + /* Rewriting Config with the write trigger starts a single conversion */
> + ret = tle493d_write(data, TLE493D_REG_CONFIG,
> + data->regs[TLE493D_REG_CONFIG], TLE493D_ADDR_TRIG_WRITE);
> + if (ret)
> + return ret;
> +
> + ret = read_poll_timeout(tle493d_read, err,
> + err || tle493d_conversion_done(buf, last_frm),
> + TLE493D_POLL_US, TLE493D_TIMEOUT_US, true,
This 'true' is better to be explained at the top of the read_*() call.
Something like
/* We need to delay before checking the conversion state */
> + data, buf, TLE493D_DATA_LEN);
> + if (ret)
> + return ret;
> + if (err)
> + return err;
> +
> + /*
> + * Cache Diag before checking for errors: its frame counter is what the
> + * next measurement compares against, so a rejected frame must not be
> + * seen as new again.
> + */
> + diag = buf[TLE493D_REG_DIAG];
> + data->regs[TLE493D_REG_DIAG] = diag;
> +
> + if (!FIELD_GET(TLE493D_DIAG_FF, diag) || FIELD_GET(TLE493D_DIAG_T, diag) ||
> + !tle493d_data_parity_ok(buf))
> + return -EIO;
> +
> + return 0;
> +}
...
> +static int tle493d_get_channel_data(const u8 *buf, unsigned int ch)
> +{
> + u32 val;
> +
> + switch (ch) {
> + case TLE493D_AXIS_X:
> + val = buf[TLE493D_REG_BX] << 4 |
> + FIELD_GET(TLE493D_BX2_X_LSB, buf[TLE493D_REG_BX2]);
> + return sign_extend32(val, 11);
> + case TLE493D_AXIS_Y:
> + val = buf[TLE493D_REG_BY] << 4 |
> + FIELD_GET(TLE493D_BX2_Y_LSB, buf[TLE493D_REG_BX2]);
> + return sign_extend32(val, 11);
> + case TLE493D_AXIS_Z:
> + val = buf[TLE493D_REG_BZ] << 4 |
> + FIELD_GET(TLE493D_TEMP2_Z_LSB, buf[TLE493D_REG_TEMP2]);
> + return sign_extend32(val, 11);
> + case TLE493D_TEMPERATURE:
> + /* 12-bit value whose two LSBs are always zero */
Hmm... Never ever can be negative?
> + return buf[TLE493D_REG_TEMP] << 4 |
> + FIELD_GET(TLE493D_TEMP2_TEMP_LSB, buf[TLE493D_REG_TEMP2]) << 2;
> + default:
> + 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 = { };
> + int ret;
> + mutex_lock(&data->lock);
> + ret = tle493d_measure(data, buf);
> + mutex_unlock(&data->lock);
> + if (ret)
> + goto out;
Hmm... It will be mix with cleanup.h (by style), the easiest way is to split
the inner function. Some driver(s) use scoped_guard() with 'break' statement,
but that requires a comment.
> + 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 const unsigned long tle493d_scan_masks[] = { GENMASK(3, 0), 0 };
IIRC. Jonathan prefers to see BIT() by BIT() ORed instead of semantic-less
GENMASK().
...
> + ret = tle493d_init(data, iicadr);
> + if (ret)
> + return ret;
Does _init() do anything that we want to undone on failure or at driver's
remove stage? If so, we need a wrapped rollback function.
--
With Best Regards,
Andy Shevchenko