mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Viral Patel <viral8854@gmail.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Dixit Parmar" <dixitparmar19@gmail.com>,
	"Victor Chernyakin" <chernyakin.victor.j@outlook.com>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] iio: magnetometer: add Infineon TLE493D-W2B6 driver
Date: Mon, 23 Nov 2026 10:45:22 +0200	[thread overview]
Message-ID: <awP9IkDVy4VM9Xum@ashevche-desk.local> (raw)
In-Reply-To: <20261121-tle493d-v2-2-92d7e57cec89@gmail.com>

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@gmail.com>

> + * 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



      parent reply	other threads:[~2026-11-23  8:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  5:55 [PATCH v2 0/2] iio: magnetometer: add Infineon TLE493D-W2B6 support Viral Patel
2026-11-21  5:55 ` [PATCH v2 1/2] dt-bindings: iio: magnetometer: infineon,tlv493d-a1b6: add TLE493D-W2B6 Viral Patel
2026-11-21 19:29   ` Jonathan Cameron
2026-11-21  5:55 ` [PATCH v2 2/2] iio: magnetometer: add Infineon TLE493D-W2B6 driver Viral Patel
2026-11-21 19:59   ` Jonathan Cameron
2026-11-23  8:45   ` Andy Shevchenko [this message]

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=awP9IkDVy4VM9Xum@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=chernyakin.victor.j@outlook.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dixitparmar19@gmail.com \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=viral8854@gmail.com \
    /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®