lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aJAmgX0876tu5Ss0@dixit>
Date: Mon, 4 Aug 2025 08:48:25 +0530
From: Dixit Parmar <dixitparmar19@...il.com>
To: Jonathan Cameron <jic23@...nel.org>
Cc: David Lechner <dlechner@...libre.com>,
	Nuno Sá <nuno.sa@...log.com>,
	Andy Shevchenko <andy@...nel.org>, Rob Herring <robh@...nel.org>,
	Krzysztof Kozlowski <krzk+dt@...nel.org>,
	Conor Dooley <conor+dt@...nel.org>, linux-kernel@...r.kernel.org,
	linux-iio@...r.kernel.org, devicetree@...r.kernel.org
Subject: Re: [PATCH v2 1/2] iio: magnetometer: add support for Infineon
 TLV493D 3D Magentic sensor

On Sat, Aug 02, 2025 at 12:43:33PM +0100, Jonathan Cameron wrote:
> On Sat, 02 Aug 2025 12:14:27 +0530
> Dixit Parmar <dixitparmar19@...il.com> wrote:
> 
> > The Infineon TLV493D is a Low-Power 3D Magnetic Sensor. The Sensor
> > applications includes joysticks, control elements (white goods,
> > multifunction knops), or electric meters (anti tampering) and any
> > other application that requires accurate angular measurements at
> > low power consumptions.
> > 
> > The Sensor is configured over I2C, and as part of Sensor measurement
> > data it provides 3-Axis magnetic fields and temperature core measurement.
> > 
> > The driver supports raw value read and buffered input via external trigger
> > to allow streaming values with the same sensing timestamp.
> > 
> > While sensor has interrupt pin multiplexed with I2C SCL pin. But for bus
> > configurations interrupt(INT) is not recommended, unless timing constraints
> > between I2C data transfers and interrupt pulses are monitored and aligned.
> > 
> > The Sensor's I2C register map and mode information is described in product
> > User Manual[Link].
> > 
> > Datasheet: https://www.infineon.com/assets/row/public/documents/24/49/infineon-tlv493d-a1b6-datasheet-en.pdf
> > Link: https://www.mouser.com/pdfDocs/Infineon-TLV493D-A1B6_3DMagnetic-UserManual-v01_03-EN.pdf
> > Signed-off-by: Dixit Parmar <dixitparmar19@...il.com>
> 
> Hi Dixit,
> 
> Some additional comments inline.
> 
> > diff --git a/drivers/iio/magnetometer/tlv493d.c b/drivers/iio/magnetometer/tlv493d.c
> > new file mode 100644
> > index 000000000000..da1569ae97bf
> > --- /dev/null
> > +++ b/drivers/iio/magnetometer/tlv493d.c
> 
> > +enum tlv493d_op_mode {
> > +	TLV493D_OP_MODE_POWERDOWN = 0,
> > +	TLV493D_OP_MODE_FAST,
> > +	TLV493D_OP_MODE_LOWPOWER,
> > +	TLV493D_OP_MODE_ULTRA_LOWPOWER,
> > +	TLV493D_OP_MODE_MASTERCONTROLLED,
> > +	TLV493D_OP_MODE_MAX
> In order to be able to use this as the type for a parameter as suggested
> below, you'll need to drop MODE_MAX.  Comments on why you shouldn't
> need that anyway below.
> 
> > +};
> > +
> > +struct tlv493d_mode {
> > +	u8 m;
> You have an enum type.  Much better to use it.
> 
> > +	u32 sleep_us;
> > +};
> > +
> > +struct tlv493d_data {
> > +	struct device *dev;
> > +	struct i2c_client *client;
> > +	/* protects from simultaneous sensor access and register readings */
> > +	struct mutex lock;
> > +	u8 mode;
> > +	u8 wr_regs[TLV493D_WR_REG_MAX];
> > +};
> > +
> > +/*
> > + * Different mode has different measurement cycle time, this time is
> > + * used in deriving the sleep and timemout while reading the data from
> > + * sensor in polling.
> > + * Power-down mode: No measurement.
> > + * Fast mode: Freq:3.3 KHz. Measurement time:305 usec.
> > + * Low-power mode: Freq:100 Hz. Measurement time:10 msec.
> > + * Ultra low-power mode: Freq:10 Hz. Measurement time:100 msec.
> > + * Master controlled mode: Freq:3.3 Khz. Measurement time:305 usec.
> > + */
> > +static struct tlv493d_mode tlv493d_mode_info[TLV493D_OP_MODE_MAX] = {
> If you want to size it, do it using the enum values. [] is fine here
> 	[TLV493D_OP_MODE_POWERDOWN] = { }
> 
> I'm not sure why this should embed the index.  Can you just drop .m?
>
Indeed, In V2 I wanted to change this to a u32 array containing the
timing values for all mode and drop struct, but was skeptical.
IMO that would also be a clean way rather than having it in struct. Makes sense?
> > +	{ .m = TLV493D_OP_MODE_POWERDOWN, .sleep_us = 0 },
> > +	{ .m = TLV493D_OP_MODE_FAST, .sleep_us = 305 },
> > +	{ .m = TLV493D_OP_MODE_LOWPOWER, .sleep_us = 10 * USEC_PER_MSEC	},
> > +	{ .m = TLV493D_OP_MODE_ULTRA_LOWPOWER, .sleep_us = 100 * USEC_PER_MSEC },
> > +	{ .m = TLV493D_OP_MODE_MASTERCONTROLLED, .sleep_us = 305 }
> > +};
> > +
> > +/*
> > + * The datasheet mentions the sensor supports only direct byte-stream write
> > + * starting from register address 0x0. So for any modification to be made to
> > + * any write registers, it must be written starting from the register address
> > + * 0x0. I2C write operation should not contain register address in the I2C
> > + * frame, it should contains only raw byte stream for the write registers.
> > + * I2C Frame: |S|SlaveAddr Wr|Ack|Byte[0]|Ack|Byte[1]|Ack|.....|Sp|
> > + */
> > +static int tlv493d_write_all_regs(struct tlv493d_data *data)
> > +{
> > +	int ret;
> > +
> > +	if (!data || !data->client)
> If either of these happen, something went very very wrong.
> No need for the checks.  Remove all similar ones.
> 
Interesting, Is the idea behind is that this API is used within the
driver and the inputs are known? Ofcourse it saves some CPU cycles.
> > +		return -EINVAL;
> > +
> > +	ret = i2c_master_send(data->client, data->wr_regs, ARRAY_SIZE(data->wr_regs));
> > +	if (ret < 0) {
> > +		dev_err(data->dev, "i2c write registers failed, error: %d\n", ret);
> > +		return ret;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int tlv493d_set_operating_mode(struct tlv493d_data *data, u8 mode)
> As below. Use the enum type.
> > +{
> > +	if (!data)
> > +		return -EINVAL;
> 
> As above. Data is never going to be NULL, so don't check it.
> 
> > +
> > +	u8 mode1_cfg, mode2_cfg;
> > +
> > +	switch (mode) {
> > +	case TLV493D_OP_MODE_POWERDOWN:
> > +		mode1_cfg = FIELD_PREP(TLV493D_MODE1_MOD_LOWFAST, 0);
> > +		mode2_cfg = FIELD_PREP(TLV493D_MODE2_LP_PERIOD, 0);
> > +		break;
> > +
> > +	case TLV493D_OP_MODE_FAST:
> > +		mode1_cfg = FIELD_PREP(TLV493D_MODE1_MOD_LOWFAST, 1);
> > +		mode2_cfg = FIELD_PREP(TLV493D_MODE2_LP_PERIOD, 0);
> > +		break;
> > +
> > +	case TLV493D_OP_MODE_LOWPOWER:
> > +		mode1_cfg = FIELD_PREP(TLV493D_MODE1_MOD_LOWFAST, 2);
> > +		mode2_cfg = FIELD_PREP(TLV493D_MODE2_LP_PERIOD, 1);
> > +		break;
> > +
> > +	case TLV493D_OP_MODE_ULTRA_LOWPOWER:
> > +		mode1_cfg = FIELD_PREP(TLV493D_MODE1_MOD_LOWFAST, 2);
> > +		mode2_cfg = FIELD_PREP(TLV493D_MODE2_LP_PERIOD, 0);
> > +		break;
> > +
> > +	case TLV493D_OP_MODE_MASTERCONTROLLED:
> > +		mode1_cfg = FIELD_PREP(TLV493D_MODE1_MOD_LOWFAST, 3);
> > +		mode2_cfg = FIELD_PREP(TLV493D_MODE2_LP_PERIOD, 0);
> > +		break;
> > +
> > +	default:
> > +		dev_err(data->dev, "invalid mode configuration\n");
> > +		return -EINVAL;
> And with the enum type you shouldn't need a default.
> 
Can you please help me understand this better? So far my learning was,
wherever there is switch case, it must have default case handled
appropriatly.
> > +	}
> > +
> > +	data->wr_regs[TLV493D_WR_REG_MODE1] |= mode1_cfg;
> > +	data->wr_regs[TLV493D_WR_REG_MODE2] |= mode2_cfg;
> > +
> > +	return tlv493d_write_all_regs(data);
> > +}
> > +
> > +static s16 tlv493d_get_channel_data(u8 *b, u8 ch)
> Use the enum. 
> > +{
> > +	if (!b)
> > +		return -EINVAL;
> Unnecessary check
> > +
> > +	u16 val = 0;
> Variable declarations at the top of scope unless strong reason to do otherwise
> (only normally done for auto cleanup)
> > +
> > +	switch (ch) {
> > +	case TLV493D_AXIS_X:
> > +		val = FIELD_GET(TLV493D_BX_MAG_X_AXIS_MSB, b[TLV493D_RD_REG_BX]) << 4 |
> > +			FIELD_GET(TLV493D_BX2_MAG_X_AXIS_LSB, b[TLV493D_RD_REG_BX2]) >> 4;
> > +		break;
> > +	case TLV493D_AXIS_Y:
> > +		val = FIELD_GET(TLV493D_BY_MAG_Y_AXIS_MSB, b[TLV493D_RD_REG_BY]) << 4 |
> > +			FIELD_GET(TLV493D_BX2_MAG_Y_AXIS_LSB, b[TLV493D_RD_REG_BX2]);
> > +		break;
> > +	case TLV493D_AXIS_Z:
> > +		val = FIELD_GET(TLV493D_BZ_MAG_Z_AXIS_MSB, b[TLV493D_RD_REG_BZ]) << 4 |
> > +			FIELD_GET(TLV493D_BZ2_MAG_Z_AXIS_LSB, b[TLV493D_RD_REG_BZ2]);
> > +		break;
> > +	case TLV493D_TEMPERATURE:
> > +		val = FIELD_GET(TLV493D_TEMP_TEMP_MSB, b[TLV493D_RD_REG_TEMP]) << 8 |
> > +			FIELD_GET(TLV493D_TEMP2_TEMP_LSB, b[TLV493D_RD_REG_TEMP2]);
> > +		break;
> > +	default:
> > +		return -EINVAL;
> And with the enum, there will be no need to have a default.
> Not to mention passing -EINVAL through an s16 is unusual and I doubt wise.
> 
> > +	}
> > +
> > +	return sign_extend32(val, 11);
> > +}
> > +
> > +static int tlv493d_get_measurements(struct tlv493d_data *data, s16 *x, s16 *y,
> > +				s16 *z, s16 *t)
> > +{
> > +	u8 buff[7] = {};
> > +	int err, ret;
> > +	struct tlv493d_mode *mode;
> > +
> > +	if (!data)
> > +		return -EINVAL;
> > +
> > +	guard(mutex)(&data->lock);
> > +
> > +	ret = pm_runtime_resume_and_get(data->dev);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	mode = &tlv493d_mode_info[data->mode];
> > +
> > +	/*
> > +	 * Poll until data is valid,
> > +	 * For a valid data TLV493D_TEMP_CHANNEL bit of TLV493D_RD_REG_TEMP should be set to 0.
> > +	 * The sampling time depends on the sensor mode. poll 3x the time of the sampling time.
> > +	 */
> > +	ret = read_poll_timeout(i2c_master_recv, err, err ||
> > +			FIELD_GET(TLV493D_TEMP_CHANNEL, buff[TLV493D_RD_REG_TEMP]) == 0,
> > +			mode->sleep_us, (3 * mode->sleep_us), false, data->client, buff,
> > +			ARRAY_SIZE(buff));
> > +	if (ret) {
> > +		dev_err(data->dev, "i2c read poll timeout, error:%d\n", ret);
> > +		goto out;
> > +	}
> > +	if (err < 0) {
> > +		dev_err(data->dev, "i2c read data failed, error:%d\n", err);
> > +		ret = err;
> > +		goto out;
> > +	}
> > +
> > +	*x = tlv493d_get_channel_data(buff, TLV493D_AXIS_X);
> > +	*y = tlv493d_get_channel_data(buff, TLV493D_AXIS_Y);
> > +	*z = tlv493d_get_channel_data(buff, TLV493D_AXIS_Z);
> > +	*t = tlv493d_get_channel_data(buff, TLV493D_TEMPERATURE);
> > +
> > +out:
> > +	pm_runtime_mark_last_busy(data->dev);
> I've just rebased (mid merge window so i'll do it again on rc1) and now
> have the version of pm_runtime_put_autosuspend() that internally calls
> pm_runtime_mark_last_busy().
> 
> So please drop the line above if you need to do a v3.
>  
Okay.
> > +	pm_runtime_put_autosuspend(data->dev);
> > +	return ret;
> > +}
> 
> > +static int tlv493d_read_raw(struct iio_dev *indio_dev,
> > +			const struct iio_chan_spec *chan, int *val,
> > +			int *val2, long mask)
> > +{
> > +	struct tlv493d_data *data = iio_priv(indio_dev);
> > +	s16 x, y, z, t;
> > +	int ret;
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_RAW:
> > +		ret = tlv493d_get_measurements(data, &x, &y, &z, &t);
> > +		if (ret)
> > +			return ret;
> > +
> > +		/* Return raw values for requested channel */
> > +		switch (chan->address) {
> > +		case TLV493D_AXIS_X:
> > +			*val = x;
> > +			return IIO_VAL_INT;
> > +		case TLV493D_AXIS_Y:
> > +			*val = y;
> > +			return IIO_VAL_INT;
> > +		case TLV493D_AXIS_Z:
> > +			*val = z;
> > +			return IIO_VAL_INT;
> > +		case TLV493D_TEMPERATURE:
> > +			*val = t;
> > +			return IIO_VAL_INT;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	case IIO_CHAN_INFO_SCALE:
> > +		switch (chan->type) {
> > +		case IIO_MAGN:
> > +			/*
> > +			 * Magnetic field scale: 0.0098 mTesla (i.e. 9.8 µT)
> > +			 * Magnetic filed in Guass: mT * 10 = 0.098.
> > +			 */
> > +			*val = 98;
> > +			*val2 = 1000;
> > +			return IIO_VAL_FRACTIONAL;
> > +		case IIO_TEMP:
> > +			/*
> > +			 * Temperature scale: 1.1 °C per LSB, expressed as 1100 m°C
> > +			 * Returned as integer for IIO core to apply:
> > +			 * temp = (raw + offset) * scale
> > +			 */
> > +			*val = 1100;
> > +			return IIO_VAL_INT;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +	case IIO_CHAN_INFO_OFFSET:
> > +		switch (chan->type) {
> > +		case IIO_TEMP:
> > +			/*
> > +			 * Temperature offset includes sensor-specific raw offset
> > +			 * plus compensation for +25°C bias in formula.
> > +			 * offset = -raw_offset + (25000 / 1100)
> > +			 * -340 + 22.72 = -317.28
> > +			 */
> > +			*val = -31728;
> > +			*val2 = 100;
> > +			return IIO_VAL_FRACTIONAL;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +
> > +	return 0;
> 
> Can you get here? If not drop this.  Compilers are very good at complaining if
> code changes later such that we should add this back.
> 
> > +}
> 
> > +static const struct iio_info tlv493d_info = {
> > +	.read_raw = tlv493d_read_raw,
> > +};
> > +
> > +static const struct iio_buffer_setup_ops tlv493d_setup_ops = { NULL };
> 
> No need specify that NULL. Due to some odd quirks of compiler specific
> handling and C spec evolution (none of which apply to the kernel because
> we carefully choose build options) that is actually less likely to do what
> you want than = { };
> 
Originally it was { } in V1. Got review comment that it must have NULL
if no ops is being passed, so I added (May be I would have asked and
understand their point). Nevertheless will remove NULL.
> 
> 
> > +
> > +static const unsigned long tlv493d_scan_masks[] = { GENMASK(3, 0), 0 };
> > +
> > +static int tlv493d_probe(struct i2c_client *client)
> > +{
> > +	struct device *dev = &client->dev;
> > +	struct iio_dev *indio_dev;
> > +	struct tlv493d_data *data;
> > +	int ret;
> > +
> > +	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> > +	if (!indio_dev)
> > +		return -ENOMEM;
> > +
> > +	data = iio_priv(indio_dev);
> > +	data->dev = dev;
> > +	data->client = client;
> > +	i2c_set_clientdata(client, indio_dev);
> > +
> > +	ret = devm_mutex_init(dev, &data->lock);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = devm_regulator_get_enable(dev, "vdd");
> > +	if (ret)
> > +		return dev_err_probe(dev, ret, "failed to enable regulator\n");
> > +
> > +	/*
> > +	 * Setting Sensor default operating mode as Master Controlled mode as
> > +	 * it performs measurement cycle on-request only and stays in Power-Down
> > +	 * mode until next cycle is initiated.
> > +	 */
> > +	data->mode = TLV493D_OP_MODE_MASTERCONTROLLED;
> > +	ret = tlv493d_init(data);
> > +	if (ret)
> > +		return dev_err_probe(dev, ret, "failed to initialize\n");
> > +
> > +	indio_dev->info = &tlv493d_info;
> > +	indio_dev->modes = INDIO_DIRECT_MODE;
> > +	indio_dev->name = client->name;
> > +	indio_dev->channels = tlv493d_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(tlv493d_channels);
> > +	indio_dev->available_scan_masks = tlv493d_scan_masks;
> > +
> > +	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> > +			iio_pollfunc_store_time, tlv493d_trigger_handler,
> > +			&tlv493d_setup_ops);
> Similar to below no alignment. Something like;
> 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> 					      iio_pollfunc_store_time,
> 					      tlv493d_trigger_handler,
> 					      &tlv493d_setup_ops);
> 
> If you do have a case where the line ends up very long with this style, then
> indent only one tab more than line above. If that applied here it would be.
> 
> 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> 		iio_pollfunc_store_time, tlv493d_trigger_handler,
> 		&tlv493d_setup_ops);
> 
> 
> 
> > +	if (ret < 0)
> > +		return dev_err_probe(dev, ret, "iio triggered buffer setup failed\n");
> > +
> > +	ret = pm_runtime_set_active(dev);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	ret = devm_pm_runtime_enable(dev);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	pm_runtime_get_noresume(dev);
> > +	pm_runtime_set_autosuspend_delay(dev, 500);
> > +	pm_runtime_use_autosuspend(dev);
> > +
> > +	pm_runtime_mark_last_busy(dev);
> > +	pm_runtime_put_autosuspend(dev);
> > +
> > +	ret = devm_iio_device_register(dev, indio_dev);
> > +	if (ret)
> > +		return dev_err_probe(dev, ret, "iio device register failed\n");
> > +
> > +	return 0;
> > +}
> > +
> > +static int tlv493d_runtime_suspend(struct device *dev)
> > +{
> > +	struct iio_dev *indio_dev = dev_get_drvdata(dev);
> > +	struct tlv493d_data *data = iio_priv(indio_dev);
> > +
> > +	return tlv493d_set_operating_mode(data, TLV493D_OP_MODE_POWERDOWN);
> > +}
> > +
> > +static int tlv493d_runtime_resume(struct device *dev)
> > +{
> > +	struct iio_dev *indio_dev = dev_get_drvdata(dev);
> > +	struct tlv493d_data *data = iio_priv(indio_dev);
> 
> Trivial but you could do
> 	struct tlv493d_data *data = iio_priv(dev_get_drvdata(dev));
> with no real loss of readability.
> 
> > +
> > +	return tlv493d_set_operating_mode(data, data->mode);
> > +}
> > +
> > +static DEFINE_RUNTIME_DEV_PM_OPS(tlv493d_pm_ops,
> > +			tlv493d_runtime_suspend, tlv493d_runtime_resume, NULL);
> Preferred wrapping here is
> static DEFINE_RUNTIME_DEV_PM_OPS(tlv493d_pm_ops, tlv493d_runtime_suspend,
> 				 tlv493d_runtime_resume, NULL);
> 
> So go nearer 80 chars on first line and align second line below the parameters
> on the first line.
> 
> Jonathan
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ