Re: [PATCH v3 2/2] iio: proximity: add Imagis ISG5320A SAR sensor driver

From: Jonathan Cameron

Date: Sat Nov 21 2026 - 21:45:13 EST


On Tue, 17 Nov 2026 20:33:33 +0300
Adil Hüsrev Tepe <tadilhusrev@xxxxxxxxx> wrote:

> Add an IIO driver for the Imagis ISG5320A capacitive SAR/grip sensor
> (I2C), as fitted on the Samsung Galaxy XCover5. It exposes one
> IIO_PROXIMITY channel (the B-channel signal minus its baseline) and a
> rising/falling threshold proximity event.
>
> Tested on a Samsung Galaxy XCover5 (re-confirmed 2026-11-17, kernel
> 7.4.0-rc1-00023-gdcb8b91fb779+ #30): the driver binds, registers as
> iio:device4 "isg5320a", and in_proximity0_raw tracked a live
> hand-proximity test (baseline ~0, swinging to -12817/9748/10759 as a
> hand approached/withdrew the sensor).
>
> Signed-off-by: Adil Hüsrev Tepe <tadilhusrev@xxxxxxxxx>
Hi Adil,

Welcome to IIO.

Unfortunately this looks a bit too much like an aged vendor driver.
I've called out a few things but Andy's earlier comment about
making sure you read other recent drivers and threads will get you
a long way.

+ as others have mentioned, for a new driver at least 1 week is
appropriate between versions whether or sashiko is making noise.
Feel free to reply to the patches if you disagree with anything
sashiko says, otherwise just fix it for v2.

Various comments inline.

> diff --git a/drivers/iio/proximity/isg5320a.c b/drivers/iio/proximity/isg5320a.c
> new file mode 100644
> index 000000000..4df0e09d3
> --- /dev/null
> +++ b/drivers/iio/proximity/isg5320a.c
> @@ -0,0 +1,848 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Imagis ISG5320A capacitive SAR/grip sensor.
> + *
> + * Register map, init table and every write sequence below come from the
> + * Samsung vendor driver (drivers/sensors/isg5320a.c + isg5320a_reg.h,
> + * Galaxy XCover5 5.10 tree, Copyright (C) Imagis Technology). Citations of
> + * the form "vendor :NNN" refer to lines in that isg5320a.c. This driver
> + * writes no register the vendor driver does not write, and only with the
> + * values the vendor writes; regmap enforces that through writeable_reg.

Don't do this referencing to specific places. Just a general reference is
enough. Current style makes a mess of this driver to gain info most
readers don't need. If there are non obvious things that are drawn from
the vendor driver then add a comment just for those.

> + *
> + * The chip is used as one proximity channel: the vendor's "diff" value
> + * (CDC16_T - BSL16_B, i.e. the B-channel signal minus its baseline) is
> + * exposed as in_proximity0_raw, and the chip's CLOSE/FAR interrupt is
> + * exposed as IIO threshold events (rising = grip, falling = release).
> + */


> +struct isg5320a_reg_data {
> + u8 addr;
> + u8 val;
> +};
> +
> +/*
> + * Vendor setup_reg[] for "XCOVER5 ALL" (isg5320a_reg.h :283-435). The other
> + * table (:130-282) is under CONFIG_SENSORS_ISG5320A_XCOVER5_KOR, which the
> + * stock exynos850-xcover5xx_defconfig leaves unset; it differs only in
> + * 0x9C (threshold low byte), 0xA0 (far hysteresis low byte) and 0xBA.
> + */
> +static const struct isg5320a_reg_data isg5320a_setup_reg[] = {
Ah what fun. Might be worth checking if any of these are the reset
defaults and hence we can rely on reset and cut down on what is written.


> + { 0x05, 0xFC }, { 0x06, 0xF1 }, { 0x07, 0x13 }, { 0x08, 0x01 },
> + { 0x09, 0x80 }, { 0x0A, 0x20 }, { 0x0B, 0x00 }, { 0x0C, 0x00 },
> + { 0x12, 0x89 }, { 0x19, 0x8A }, { 0x1B, 0x28 }, { 0x1D, 0x20 },
> + { 0x1E, 0x00 }, { 0x1F, 0x18 }, { 0x20, 0x05 }, { 0x21, 0x10 },
> + { 0x22, 0x10 }, { 0x23, 0x00 }, { 0x24, 0x00 }, { 0x25, 0x05 },
> + { 0x26, 0x05 }, { 0x27, 0x10 }, { 0x28, 0x20 }, { 0x29, 0x00 },
> + { 0x2A, 0x20 }, { 0x2B, 0x00 }, { 0x2C, 0x00 }, { 0x2D, 0x20 },
> + { 0x2F, 0x18 }, { 0x30, 0xC0 }, { 0x41, 0x11 }, { 0x60, 0xF0 },
> + { 0x61, 0x8F }, { 0x62, 0x0C }, { 0x63, 0x00 }, { 0x64, 0x00 },
> + { 0x65, 0x44 }, { 0x66, 0x3C }, { 0x67, 0x01 }, { 0x68, 0x00 },
> + { 0x69, 0x00 }, { 0x6A, 0x10 }, { 0x6B, 0x00 }, { 0x6C, 0x10 },
> + { 0x6D, 0x40 }, { 0x6E, 0x22 }, { 0x6F, 0x00 }, { 0x70, 0x01 },
> + { 0x71, 0x60 }, { 0x72, 0xFF }, { 0x73, 0xFF }, { 0x74, 0xFF },
> + { 0x7B, 0x08 }, { 0x7C, 0x00 }, { 0x7D, 0x0C }, { 0x7E, 0x00 },
> + { 0x7F, 0xF8 }, { 0x80, 0xF8 }, { 0x81, 0x00 }, { 0x82, 0xF0 },
> + { 0x83, 0xF0 }, { 0x84, 0xF0 }, { 0x85, 0xF0 }, { 0x86, 0xF0 },
> + { 0x87, 0xF0 }, { 0x88, 0x00 }, { 0x89, 0x00 }, { 0x8A, 0x08 },
> + { 0x8B, 0x00 }, { 0x8C, 0x02 }, { 0x8D, 0x00 }, { 0x99, 0x21 },
> + { 0x9A, 0x3C }, { 0x9B, 0x01 }, { 0x9C, 0x2C }, { 0x9D, 0x00 },
> + { 0x9E, 0x00 }, { 0x9F, 0x00 }, { 0xA0, 0x96 }, { 0xA1, 0xC0 },
> + { 0xA2, 0x22 }, { 0xA3, 0x00 }, { 0xA4, 0x08 }, { 0xA5, 0x60 },
> + { 0xA6, 0xFF }, { 0xA7, 0xFF }, { 0xA8, 0xFF }, { 0xA9, 0x00 },
> + { 0xAF, 0x40 }, { 0xB0, 0x00 }, { 0xB1, 0x60 }, { 0xB2, 0x00 },
> + { 0xB3, 0xD0 }, { 0xB4, 0x10 }, { 0xB5, 0x28 }, { 0xB6, 0xF0 },
> + { 0xB7, 0xF6 }, { 0xB8, 0xFD }, { 0xB9, 0xDE }, { 0xBA, 0xFD },
> + { 0xBB, 0xF0 }, { 0xBC, 0x00 }, { 0xBD, 0xFF }, { 0xBE, 0x01 },
> + { 0xBF, 0x5E }, { 0xC0, 0x00 }, { 0xC1, 0x69 }, { 0xC2, 0xF0 },
> + { 0xC3, 0x00 }, { 0xC4, 0x00 }, { 0xC5, 0x00 }, { 0x13, 0x3A },
> + { 0x18, 0xF1 }, /* A ch : CS0, B ch : CS1 */
> + { 0x1C, 0x18 }, { 0x35, 0x00 }, { 0x36, 0x00 }, { 0x3A, 0x0A },
> + { 0x3B, 0x50 }, { 0x3C, 0x0A }, { 0x3D, 0x50 }, { 0x3E, 0x60 },
> + { 0x94, 0x10 }, { 0x95, 0xA9 }, { 0x96, 0x11 }, { 0x2E, 0x14 },
> + { 0x0E, 0xE0 }, { 0x0F, 0x1E }, { 0x10, 0xC0 }, { 0x11, 0xC1 },
> + { 0xD0, 0xE0 }, { 0xD1, 0x08 }, { 0xD2, 0x00 }, { 0xD3, 0xFF },
> + { 0xD4, 0xFF }, { 0xD5, 0x1E }, { 0xD6, 0x32 }, { 0xD7, 0xFF },
> + { 0xD8, 0x20 }, { 0xD9, 0x70 }, { 0xDA, 0x7F }, { 0xDB, 0xE0 },
> + { 0xDC, 0x00 }, { 0x76, 0x08 }, { 0x77, 0x00 }, { 0x79, 0x02 },
> + { 0x7A, 0x00 }, { 0xAA, 0x36 }, { 0xAB, 0xB0 }, { 0xAD, 0x04 },
> + { 0xAE, 0x00 },
> +};
> +
> +struct isg5320a_data {
> + struct device *dev;
> + struct regmap *regmap;

You can trivially retrieve the dev from regmap and they are always the
same one.

> + int irq;
> + /* Serialises multi-register chip sequences and the cached sample. */
> + struct mutex lock;
> + /* Serialises event enable/disable (sleeps in disable_irq()). */
> + struct mutex ev_lock;
> + bool ev_enabled;
> + bool irq_wake_enabled;
> + bool close;
> + u16 cdc;
> + u16 base;
> + u16 thresh;
> + u16 close_hyst;
> + u16 far_hyst;
> + u32 cfcal_th;
> +};
> +
> +static bool isg5320a_writeable_reg(struct device *dev, unsigned int reg)
> +{
> + unsigned int i;
> +
> + switch (reg) {
> + case ISG5320A_REG_OSCCON:
> + case ISG5320A_REG_IRQFUNC:
> + case ISG5320A_REG_BS_ON_WD:
> + case ISG5320A_REG_SCANCTRL1:
> + case ISG5320A_REG_SCANCTRL2:
> + case ISG5320A_REG_PROTECT:
> + case ISG5320A_REG_SOFTRESET:
> + return true;
> + }
> +
> + for (i = 0; i < ARRAY_SIZE(isg5320a_setup_reg); i++)

for (unsigned int i = 0;...

> + if (isg5320a_setup_reg[i].addr == reg)
> + return true;
> +
> + return false;
> +}
> +
> +static bool isg5320a_precious_reg(struct device *dev, unsigned int reg)
> +{
> + /* Reading IRQSRC acknowledges the interrupt; keep debugfs off it. */
> + return reg == ISG5320A_REG_IRQSRC;
> +}
> +
> +static const struct regmap_config isg5320a_regmap_config = {
> + .reg_bits = 8,
> + .val_bits = 8,
> + .max_register = ISG5320A_MAX_REG,
> + .writeable_reg = isg5320a_writeable_reg,
> + .precious_reg = isg5320a_precious_reg,
> + .cache_type = REGCACHE_NONE,

Why? I'd expect a brief note if caching can't be used for some reason.

> +};
> +
> +/* vendor isg5320a_reset(), :247-292, plus a CHIPID read the vendor never does */
> +static int isg5320a_reset(struct isg5320a_data *data)
> +{
> + struct device *dev = data->dev;
> + unsigned int val;
> + int ret;
> +
> + usleep_range(5000, 5100); /* :255-256 */

fsleep()

> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val); /* :258 */
> + if (ret)
> + return dev_err_probe(dev, ret, "no response at IRQSRC\n");
> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_CHIPID, &val);
> + if (ret)
> + return dev_err_probe(dev, ret, "CHIPID read failed\n");
> + if (val == ISG5320A_VENDOR_CHIP_ID)
> + dev_info(dev, "CHIPID 0x%02x\n", val);

Too much. Check other drivers, we never print this.

> + else
> + dev_warn(dev, "CHIPID 0x%02x (vendor header says 0x%02x; not enforced)\n",
> + val, ISG5320A_VENDOR_CHIP_ID);
> +
> + msleep(50); /* :262-266 */
> +
> + ret = regmap_write(data->regmap, ISG5320A_REG_PROTECT, /* :268-271 */
> + ISG5320A_PROTECT_UNLOCK);
> + if (ret)
> + return dev_err_probe(dev, ret, "unlock protect failed\n");
> +
> + ret = regmap_write(data->regmap, ISG5320A_REG_SOFTRESET, /* :273-276 */
> + ISG5320A_SOFTRESET_VAL);
> + if (ret)
> + return dev_err_probe(dev, ret, "soft reset write failed\n");
> +
> + usleep_range(1000, 1100); /* :278 */

fsleep() though following it with an msleep is odd.

> +
> + msleep(50); /* :280-289 */
> + dev_dbg(dev, "reset done\n");

That is too much even for debug in a driver to be upstreamed.
We have a rich set of tracing tools for when we need info on this sort
of thing.

> +
> + return 0;
> +}
> +
> +/* vendor isg5320a_initialize(), :614-660, without the /sdcard override */
> +static int isg5320a_initialize(struct isg5320a_data *data)
> +{
> + struct device *dev = data->dev;
> + unsigned int i, val, mismatch = 0;
> + u8 buf[6];
> + int ret;
> +
> + guard(mutex)(&data->lock);
> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val); /* :625 */
> + if (ret)
> + return ret;
> + ret = regmap_write(data->regmap, ISG5320A_REG_SCANCTRL1, /* :626 */
> + ISG5320A_SCAN_STOP);
> + if (ret)
> + return ret;
> + msleep(30); /* :627 */
> +
> + for (i = 0; i < ARRAY_SIZE(isg5320a_setup_reg); i++) { /* :631-637 */

for (unsigned int i = 0; i < ...

> + const struct isg5320a_reg_data *r = &isg5320a_setup_reg[i];
> +
> + ret = regmap_write(data->regmap, r->addr, r->val);
> + if (ret)
> + return dev_err_probe(dev, ret, "init write 0x%02x failed\n",
> + r->addr);
> + ret = regmap_read(data->regmap, r->addr, &val);
> + if (ret)
> + return ret;
> + if (val != r->val) {
> + mismatch++;
> + dev_dbg(dev, "reg 0x%02x wrote 0x%02x read 0x%02x\n",
> + r->addr, r->val, val);
Given that's dead hardware or something else that we should know about (volatile
bits in a register) I'd argue we don't need to check it. With that in mind
regmap_multi_reg_write() is your friend. You may even be able to use
it for other sequences of constant writes.


> + }
> + }
> + dev_info(dev, "init table: %zu writes, %u read back different\n",
> + ARRAY_SIZE(isg5320a_setup_reg), mismatch);

Clean this debug stuff up. dev_dbg() at most.

> +
> + /* :646-650 reads the threshold; :890-897 reads all six bytes */
> + ret = regmap_bulk_read(data->regmap, ISG5320A_REG_B_PROXCTL3, buf,
> + sizeof(buf));
> + if (ret)
> + return ret;
> + data->thresh = (buf[0] << 8) | buf[1];

See below on handling big endian values.

> + data->close_hyst = (buf[2] << 8) | buf[3];
> + data->far_hyst = (buf[4] << 8) | buf[5];
> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_DIGITAL_ACC, &val); /* :653-657 */
> + if (ret)
> + return ret;
> + data->cfcal_th = ISG5320A_RESET_CONDITION * val / 8;
> +
> + dev_info(dev, "threshold %u, close hyst %u, far hyst %u, cfcal_th %u\n",
> + data->thresh, data->close_hyst, data->far_hyst, data->cfcal_th);

Isn't all that readable from sysfs? Feels like too much noise to be more than
dev_dbg() maybe.

> +
> + return 0;
> +}
> +
> +/* vendor isg5320a_set_mode(SLEEP), :749-755 */
> +static int isg5320a_mode_sleep(struct isg5320a_data *data)
> +{
> + unsigned int val;
> + int ret;
> +
> + guard(mutex)(&data->lock);
> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val);
> + if (ret)
> + return ret;
> + ret = regmap_write(data->regmap, ISG5320A_REG_SCANCTRL1, ISG5320A_SCAN_STOP);
> + if (ret)
> + return ret;
> + ret = regmap_write(data->regmap, ISG5320A_REG_OSCCON, ISG5320A_OSC_SLEEP);
> + if (ret)
> + return ret;
> + return regmap_write(data->regmap, ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_OFF);
> +}
> +
> +/*
> + * vendor isg5320a_set_mode(NORMAL), :749 + :757-758, which runs
> + * isg5320a_force_calibration(data, false), :302-321.
> + */
> +static int isg5320a_mode_normal(struct isg5320a_data *data)
> +{
> + struct regmap *map = data->regmap;
> + unsigned int val;
> + int ret;
> +
> + guard(mutex)(&data->lock);
> +
> + ret = regmap_read(map, ISG5320A_REG_IRQSRC, &val); /* :749 */
> + if (ret)
> + return ret;

Unless related clump of registers, add a blank line between them.
Same for other functions.

> + ret = regmap_write(map, ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_ON); /* :757 */
> + if (ret)
> + return ret;
> +
> + ret = regmap_write(map, ISG5320A_REG_SCANCTRL1, ISG5320A_SCAN_STOP); /* :303 */
> + if (ret)
> + return ret;
> + msleep(30);

Definitely a blank line after the sleep.

> + ret = regmap_write(map, ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_OFF); /* :306 */
> + if (ret)
> + return ret;
> + ret = regmap_write(map, ISG5320A_REG_SCANCTRL2, ISG5320A_SCAN2_CLEAR); /* :307 */
> + if (ret)
> + return ret;
> + msleep(100);
And here
/* :309 */
> + ret = regmap_write(map, ISG5320A_REG_OSCCON, ISG5320A_OSC_NORMAL); /* :310 */
> + if (ret)
> + return ret;
> + ret = regmap_write(map, ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_ON); /* :312 */
> + if (ret)
> + return ret;
> + ret = regmap_write(map, ISG5320A_REG_SCANCTRL2, ISG5320A_SCAN2_RESET); /* :313 */
> + if (ret)
> + return ret;
> + ret = regmap_write(map, ISG5320A_REG_SCANCTRL1, ISG5320A_CFCAL_START); /* :316 */
> + if (ret)
> + return ret;
> + msleep(500); /* :318 */
> + ret = regmap_write(map, ISG5320A_REG_SCANCTRL2, ISG5320A_BFCAL_START); /* :321 */
> + if (ret)
> + return ret;
> +
> + /* The vendor sw_reset path waits 300 ms after BFCAL before reading (:850-852). */
> + msleep(300);
> +
> + return 0;
> +}
> +
> +/* vendor isg5320a_get_raw_data(), :445-466; caller holds data->lock */
> +static int isg5320a_read_sample(struct isg5320a_data *data)
> +{
> + u8 buf[4];
> + u16 v;
> + int ret;
> +
> + lockdep_assert_held(&data->lock);
> +
> + ret = regmap_bulk_read(data->regmap, ISG5320A_REG_CDC16_T_H, buf,
> + sizeof(buf));
> + if (ret)
> + return ret;
> +
> + /* 0x0000 and 0xFFFF are discarded and the last value kept (:459-464) */
> + v = (buf[0] << 8) | buf[1];

Those are endian reads so either read into the
write datatype __be16 I think and use be16_to_cpu()
or read into a buffer of u8[] and use get_unaligned_be16().

> + if (v != 0 && v != 0xFFFF)
> + data->cdc = v;
> + v = (buf[2] << 8) | buf[3];
> + if (v != 0 && v != 0xFFFF)
> + data->base = v;
> +
> + return 0;
> +}
> +
> +static int isg5320a_get_sample(struct isg5320a_data *data, u16 *cdc, u16 *base)
> +{
> + struct device *dev = data->dev;
> + int ret;
> +
> + ACQUIRE(pm_runtime_active_auto_try_enabled, pm)(dev);
> + ret = ACQUIRE_ERR(pm_runtime_active_auto_try_enabled, &pm);
> + if (ret < 0)
> + return ret;
> +
> + scoped_guard(mutex, &data->lock) {
Wherever there is not much else going to happen, just hold the guard()
to the end of the function.

guard(mutex)(&data->lock);

ret = isg5320a_read_sample(data);
if (ret)
return ret;

// don't set anything on error!

> + ret = isg5320a_read_sample(data);
> + *cdc = data->cdc;
> + *base = data->base;
> + }
> + if (ret)
> + return ret;
> +
> + /*
> + * The vendor cal_work (:1640-1647) force-recalibrates when cdc drops
> + * below cfcal_th. That loop is not ported; only report the condition.
> + */
> + if (*cdc < data->cfcal_th)
> + dev_warn_ratelimited(dev, "cdc %u below cfcal_th %u (vendor would recalibrate)\n",
> + *cdc, data->cfcal_th);
> +
> + return 0;
> +}
> +
> +static irqreturn_t isg5320a_irq_thread(int irq, void *private)
> +{
> + struct iio_dev *indio_dev = private;
> + struct isg5320a_data *data = iio_priv(indio_dev);
> + s64 timestamp = iio_get_time_ns(indio_dev);
> + unsigned int src, sts;
> + bool changed = false;
> + bool close = false;
> + int ret;
> +
> + scoped_guard(mutex, &data->lock) {

I'd hold it for whole function. The stuff outside the lock if fairly light.
guard(mutex)(&data->lock);
should be fine.


> + isg5320a_read_sample(data); /* :581 */
> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &src); /* :583 */
> + if (ret)
> + break;

That relies on a the implementation of scoped_guard() as a hidden for loop.
I'm not a fan as consider these too hard to spot. Just print an error message
if you want to and return from in here.


> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSTS, &sts); /* :598 */
> + if (ret)
> + break;
> +
> + close = sts & ISG5320A_IRQSTS_PROX; /* :518 */
> + changed = close != data->close;
> + data->close = close;
> + }
> +
> + if (ret) {
> + dev_err_ratelimited(data->dev, "irq status read failed: %d\n", ret);
> + return IRQ_HANDLED;
> + }
> +
> + dev_dbg(data->dev, "irq src 0x%02x sts 0x%02x cdc %u base %u\n",
> + src, sts, data->cdc, data->base);
> +
> + if (changed && READ_ONCE(data->ev_enabled)) {

So what is the enabled protecting against? Sending a stray event in a race
with them being disabled is allowed behavior. Synchronising the irq and
then check on whether we actually saw an irq should deal with other
cases. This rather wrecks my suggestion below of always taking the ev_lock
+ the guard() one above as they will be in reverse order of elsewhere.

> + enum iio_event_direction dir;
> +
> + dir = close ? IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING;
> + iio_push_event(indio_dev,
> + IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
> + IIO_EV_TYPE_THRESH, dir),
> + timestamp);
> + }
> +
> + return IRQ_HANDLED;
> +}

> +
> +static int isg5320a_read_event_config(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir)
> +{
> + struct isg5320a_data *data = iio_priv(indio_dev);
> +
> + if (type != IIO_EV_TYPE_THRESH || dir != IIO_EV_DIR_EITHER)
> + return -EINVAL;
> +
> + return READ_ONCE(data->ev_enabled);

As below - I'd take the ev_lock if this is to avoid theoretical
tearing or other compiler silliness (that almost certainly don't
apply here in any real compiler.)

> +}

> +
> +static void isg5320a_events_off(struct isg5320a_data *data)
> +{
> + struct device *dev = data->dev;
> + int ret = 0;
> +
> + scoped_guard(mutex, &data->lock)
> + ret = regmap_write(data->regmap, ISG5320A_REG_IRQFUNC, /* :726 */
> + ISG5320A_IRQFUNC_DISABLE);
> + if (ret)
> + dev_warn(dev, "IRQFUNC disable failed: %d\n", ret);
> +
> + WRITE_ONCE(data->ev_enabled, false);

I'd just take the lock where you do the READ_ONCE.

> + disable_irq(data->irq);

You've just written a register that sounds like it is preventing them at source.
Why do you need to disable the irq? I haven't checked, but the most I'd expect
to see here is synchronize_irq() if you need to be very sure one doesn't turn up
late. That tends to only matter on remove() path were we are about to free resources
the irq uses.

> + if (data->irq_wake_enabled) {
> + disable_irq_wake(data->irq); /* :729 */
> + data->irq_wake_enabled = false;
> + }
> +}
> +
> +static int isg5320a_write_event_config(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir,
> + bool state)
> +{
> + struct isg5320a_data *data = iio_priv(indio_dev);
> +
> + if (type != IIO_EV_TYPE_THRESH || dir != IIO_EV_DIR_EITHER)
> + return -EINVAL;
> +
> + guard(mutex)(&data->ev_lock);
> +
> + if (state == data->ev_enabled)
> + return 0;
> +
> + if (state)
> + return isg5320a_events_on(data);
> +
> + isg5320a_events_off(data);
> + pm_runtime_put_autosuspend(data->dev);

Why is the get inside events_on, but the put outside events_off?
Aim for consistency for things like this. Either is fine, but
I'm not going to like a mixture!

> +
> + return 0;
> +}
> +
> +static int isg5320a_read_event_value(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir,
> + enum iio_event_info info,
> + int *val, int *val2)
> +{
> + struct isg5320a_data *data = iio_priv(indio_dev);
> +
> + if (type != IIO_EV_TYPE_THRESH || info != IIO_EV_INFO_VALUE)
> + return -EINVAL;
> +
> + /* vendor normal_threshold_show, :896-897 */
> + switch (dir) {
> + case IIO_EV_DIR_RISING:
> + *val = data->thresh + data->close_hyst;
> + return IIO_VAL_INT;
> + case IIO_EV_DIR_FALLING:
> + *val = (int)data->thresh - (int)data->far_hyst;

Does negative make sense? Any idea what the device does if we
get to that?

> + return IIO_VAL_INT;
> + default:
> + return -EINVAL;
> + }
> +}

> +
> +#define ISG5320A_CHANNEL(_events, _nevents) \
> + { \
> + .type = IIO_PROXIMITY, \
> + .indexed = 1, \
> + .channel = 0, \
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
> + .event_spec = _events, \
> + .num_event_specs = _nevents, \
> + }
> +
> +static const struct iio_chan_spec isg5320a_channels[] = {
> + ISG5320A_CHANNEL(isg5320a_events, ARRAY_SIZE(isg5320a_events)),

to me that macro looks a bit pointless. I'd just open code
setting everything as will be more readable and only 4 shared lines.

> +};
> +
> +static const struct iio_chan_spec isg5320a_channels_noirq[] = {
> + ISG5320A_CHANNEL(NULL, 0),
> +};

> +/* vendor isg5320a_remove(), :2030-2033 */
> +static void isg5320a_teardown(void *arg)
> +{
> + struct isg5320a_data *data = arg;
> + struct device *dev = data->dev;
Mentioned below. Split this up. It is also much more complex
than I'd expect to see.
> +
> + pm_runtime_disable(dev);
That should be handled via devm_pm_runtime_enable()

> +
> + if (data->ev_enabled) {
> + isg5320a_events_off(data);
> + pm_runtime_put_noidle(dev);
This bit is correct as you do need to turn off events.
> + }
> +
> + if (!pm_runtime_status_suspended(dev))
> + isg5320a_mode_sleep(data);

Basing that on the runtime state tends to hit corner
cases. What happens if you do it unconditionally?

> +
> + pm_runtime_set_suspended(dev);
You might want one of the variants of devm_pm_runtime_enable()
to sort that for you.
> + pm_runtime_dont_use_autosuspend(dev);
Check upstream for that. It actually happened when you
did pm_runtime_disable(). That is a recent change.



> +}
> +
> +static int isg5320a_probe(struct i2c_client *client)
> +{
> + struct device *dev = &client->dev;
> + struct isg5320a_data *data;
> + struct iio_dev *indio_dev;
> + unsigned long irqflags;
> + 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->irq = client->irq;
> + i2c_set_clientdata(client, indio_dev);
> +
> + ret = devm_mutex_init(dev, &data->lock);
> + if (ret)
> + return ret;
> + ret = devm_mutex_init(dev, &data->ev_lock);
> + if (ret)
> + return ret;
> +
> + /* The vendor node has no supply; the board relies on an always-on LDO. */

No value in that comment.

> + ret = devm_regulator_get_enable_optional(dev, "vdd");

vdd isn't optional. Devices tend not to work great without power..
Look at what happens if it isn't there in device tree.

> + if (ret && ret != -ENODEV)
> + return dev_err_probe(dev, ret, "failed to enable vdd\n");
> + /*
> + * If the rail was just switched on, give the chip time to come out
> + * of power-on reset. The vendor never switches it, so this value
> + * (10 ms) is a guess, not from the vendor source.
> + */
> + if (!ret)

ah. I guess this is why you are doing the optional. Don't - it complicates
code and you should make not assumptions. If a sleep is needed for power
on, always do it even if you didn't control the power on.

> + usleep_range(10000, 11000);

fsleep and do it unconditionally. Though should only reflect the device
time not the power rail (which the regulator driver should have dealt with
at least near enough that it doesn't matter).


> +
> + data->regmap = devm_regmap_init_i2c(client, &isg5320a_regmap_config);
> + if (IS_ERR(data->regmap))
> + return dev_err_probe(dev, PTR_ERR(data->regmap), "regmap init failed\n");
> +
> + ret = isg5320a_reset(data); /* :1915 */
> + if (ret)
> + return ret;
> +
> + msleep(300); /* vendor runs init_work 300 ms later, :1981 */
This may be worth capturing. 300 msec to match vendor driver. or something like that.
> +
> + ret = isg5320a_initialize(data); /* :1620 */
> + if (ret)
> + return dev_err_probe(dev, ret, "init failed\n");
> +
> + ret = isg5320a_mode_normal(data); /* :1621 */

Get rid of all these comments You are dealing with upstream not the vendor driver.

> + if (ret)
> + return dev_err_probe(dev, ret, "start failed\n");
> +
> + /*
> + * The chip is already active, so a later failure (e.g. the IRQ
> + * request below) must still put it back to sleep instead of leaving
> + * it scanning with nothing bound to it.
> + */
As you manually enabled the power (which is fine) one solution is
to add devm_add_action_or_reset() that disables it again. I think
that's idempotent here which makes it simple.

> + pm_runtime_set_autosuspend_delay(dev, ISG5320A_AUTOSUSPEND_MS);
> + pm_runtime_use_autosuspend(dev);
> + pm_runtime_set_active(dev);
> + pm_runtime_get_noresume(dev);
> + pm_runtime_enable(dev);
> + pm_runtime_put_autosuspend(dev);

What do you think this dance is doing? Look at what happens if you
take no references at all and enable autosuspend. There is stuff
in the core that sashiko has recently finally found and stopped putting
out incorrect feedback on this. Either that or it found the many
emails saying ignore sashiko as it is wrong on this one thing.

Probably also want the devm variant to reduce some other stuff you have
later.

> +
> + if (data->irq > 0) {
> + irqflags = irq_get_trigger_type(data->irq);
> + if (!irqflags)
> + irqflags = IRQF_TRIGGER_FALLING; /* :1932 */

No to doing that in new drivers. That is papering over a DT bug.
We have a few instances of ancient drivers doing this in tree because
we want got it wrong. Not in new code.

> +
> + ret = devm_request_threaded_irq(dev, data->irq, NULL,
> + isg5320a_irq_thread,
> + irqflags | IRQF_ONESHOT |
> + IRQF_NO_AUTOEN,
> + "isg5320a", indio_dev);
> + if (ret)
> + return dev_err_probe(dev, ret, "irq request failed\n");
> +
> + if (device_property_read_bool(dev, "wakeup-source"))
> + device_init_wakeup(dev, true);

Good. So you know if is a wakeup source. Make your power saving code aware
of that. See below.

> + }
> +
> + /*
> + * Registered after the IRQ request, so devm's LIFO unwind runs this
> + * teardown (which may disable_irq()/disable_irq_wake() the IRQ in
> + * isg5320a_events_off()) before the IRQ itself is freed, not after.
> + */

I'll come back to those disable_irq() (above, I review upwards as code
makes more sense that way around!) That's normally a bad smell in a
driver unless we have a very stupid device where we can't do a more
elegant and controlled disable at source. Also split this up.
I wouldn't mind even tear down at this point with a similar comment.
There are some corners around that which I'm planning to rework, but
not the stuff changing the mode etc or messing with runtime pm.
For those register stuff immediately after what is being undone.

> + ret = devm_add_action_or_reset(dev, isg5320a_teardown, data);
> + if (ret)
> + return ret;
> +
> + indio_dev->name = "isg5320a";
> + indio_dev->modes = INDIO_DIRECT_MODE;
> + if (data->irq > 0) {
> + indio_dev->info = &isg5320a_info;
> + indio_dev->channels = isg5320a_channels;
> + indio_dev->num_channels = ARRAY_SIZE(isg5320a_channels);
> + } else {
> + indio_dev->info = &isg5320a_info_noirq;
> + indio_dev->channels = isg5320a_channels_noirq;
> + indio_dev->num_channels = ARRAY_SIZE(isg5320a_channels_noirq);
> + }
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +/* vendor isg5320a_shutdown(), :2071-2080, synchronized like isg5320a_teardown() */

Given vendor drivers change too so by all means have a reference
somewhere to it, but I wouldn't call out each path.

> +static void isg5320a_shutdown(struct i2c_client *client)

Why do you care about shutdown? In vast majority of cases it is
not seen in a IIO driver because it doesn't matter if we turn
off a device when the power is promptly cut. It might matter
for you of course, but I want to know more.

> +{
> + struct isg5320a_data *data = iio_priv(i2c_get_clientdata(client));
> + struct device *dev = data->dev;
> +
> + guard(mutex)(&data->ev_lock);
> +
> + if (data->ev_enabled) {
> + isg5320a_events_off(data);
> + pm_runtime_put_noidle(dev);
> + }
> +
> + if (!pm_runtime_status_suspended(dev))
> + isg5320a_mode_sleep(data);
> +}

> +
> +/*
> + * Runtime PM only. System sleep leaves the chip as it is, as the vendor
> + * does (its suspend/resume only stop and restart the work items), so an
> + * enabled grip event can still wake the system.

Ignore the vendor driver (which is doing it wrong) and research how
wakeup sources work. They need to be deliberately registered rather
than just relying on leaving stuff on and hoping right thing happens.
Until that is in place, I would at least use the DEFINE_RUNTIME_DEV_PM_OPS
so we get some power saving on suspend. It is fine to upstream a driver
without wakeup support.


> + */
> +static const struct dev_pm_ops isg5320a_pm_ops = {
> + RUNTIME_PM_OPS(isg5320a_runtime_suspend, isg5320a_runtime_resume, NULL)
> +};

> +