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

From: Andy Shevchenko

Date: Sat Dec 05 2026 - 06:56:43 EST


On Sat, Dec 05, 2026 at 12:51:25PM +0530, Viral Patel wrote:
> Add a driver for a 3-axis Hall effect magnetometer with 12-bit
> resolution and a built-in temperature sensor. Communication is via I2C.
>
> The driver runs the sensor in master-controlled mode. As /INT is
> combined with the SCL pin, the interrupt and clock stretching are
> turned off.
>
> Communication uses raw I2C transfers rather than regmap. Once the PR
> bit has been set, a plain read always begins at register 0x00, and the
> register address byte of a write also carries trigger bits.
>
> Channel data can be read through sysfs and through a triggered buffer.

...

> +static int tle493d_read_raw(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan, int *val,
> + int *val2, long mask)

Wrap logically

static int tle493d_read_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
int *val, int *val2, long mask)

...

> +static void tle493d_reset(struct tle493d_data *data)
> +{
> + struct i2c_adapter *adap = data->client->adapter;
> + static const struct {
> + u16 addr;
> + char read_write;
> + } frames[] = {
> + { 0x7f, I2C_SMBUS_READ },
> + { 0x7f, I2C_SMBUS_READ },
> + { 0x00, I2C_SMBUS_WRITE },
> + { 0x00, I2C_SMBUS_WRITE },
> + };

No need, see below how.

> + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
> + dev_dbg(&data->client->dev, "adapter cannot send the reset sequence\n");
> + return;
> + }
> +
> + /* The reset frames are not acknowledged, so ignore the return values */
> + for (unsigned int i = 0; i < ARRAY_SIZE(frames); i++)
> + i2c_smbus_xfer(adap, frames[i].addr, 0, frames[i].read_write, 0,
> + I2C_SMBUS_QUICK, NULL);

You don't need the above table, just do two loops:

for (unsigned int i = 0; i < 2; i++)
i2c_smbus_xfer(adap, 0x7f, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_QUICK, NULL);
for (unsigned int i = 0; i < 2; i++)
i2c_smbus_xfer(adap, 0x00, 0, I2C_SMBUS_WRITE, 0, I2C_SMBUS_QUICK, NULL);

The comment has to explain double reads and writes

> + fsleep(TLE493D_RESET_DELAY_US);
> +}

...

> +static int tle493d_init(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.
> + */
> + mod1 = FIELD_PREP(TLE493D_MOD1_IICADR, iicadr) | TLE493D_MOD1_PR |
> + TLE493D_MOD1_CA | TLE493D_MOD1_INT |
> + FIELD_PREP(TLE493D_MOD1_MODE, TLE493D_MOD1_MODE_MASTER);

Rewrap to improve readability:

mod1 = FIELD_PREP(TLE493D_MOD1_MODE, TLE493D_MOD1_MODE_MASTER) |
FIELD_PREP(TLE493D_MOD1_IICADR, iicadr) |
TLE493D_MOD1_PR | TLE493D_MOD1_CA | TLE493D_MOD1_INT;

> + ret = tle493d_write(data, TLE493D_REG_MOD1, mod1, 0);
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to write MOD1\n");
> +
> + ret = tle493d_read(data, data->regs, TLE493D_NUM_REGS);
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to read registers\n");
> +
> + hwv = FIELD_GET(TLE493D_VER_HWV, data->regs[TLE493D_REG_VER]);
> + if (hwv != TLE493D_VER_HWV_B21)
> + dev_info(dev, "unknown hardware version %u\n", hwv);
> +
> + id = FIELD_GET(TLE493D_TEMP2_ID, data->regs[TLE493D_REG_TEMP2]);
> + if (id != iicadr)
> + dev_info(dev, "unexpected ID %u, expected %u\n", id, iicadr);
> +
> + /*
> + * Full range, temperature and Bz enabled, no trigger via Config. This
> + * also sets a valid CP, which is not the case after reset.
> + */
> + ret = tle493d_write(data, TLE493D_REG_CONFIG, 0, 0);
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to write Config\n");
> +
> + return 0;
> +}

...

With the above being addressed, feel free to add
Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>

--
With Best Regards,
Andy Shevchenko