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

From: Joshua Crofts

Date: Wed Nov 18 2026 - 04:54:28 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, some 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.
> + *
> + * 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).
> + */
> +
> +#include <linux/array_size.h>
> +#include <linux/bits.h>
> +#include <linux/cleanup.h>
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/errno.h>

+ err.h

> +#include <linux/i2c.h>
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/mod_devicetable.h>

Remove this, i2c.h includes the correct device tables.

> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/pm_wakeup.h>
> +#include <linux/property.h>
> +#include <linux/regmap.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/types.h>
> +

Otherwise good to see that IWYU is slowly catching on in new
submissions :)

> +#include <linux/iio/events.h>
> +#include <linux/iio/iio.h>
> +

...

> +
> +struct isg5320a_data {
> + struct device *dev;
> + struct regmap *regmap;
> + 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;
> +};

I ran pahole and found this layout to be optimal without any holes:

struct device * dev; /* 0 8 */
struct regmap * regmap; /* 8 8 */
struct mutex lock __attribute__((__aligned__(8))); /* 16 24 */
struct mutex ev_lock __attribute__((__aligned__(8))); /* 40 24 */
/* --- cacheline 1 boundary (64 bytes) --- */
int irq; /* 64 4 */
u32 cfcal_th; /* 68 4 */
u16 cdc; /* 72 2 */
u16 base; /* 74 2 */
u16 thresh; /* 76 2 */
u16 close_hyst; /* 78 2 */
u16 far_hyst; /* 80 2 */
bool ev_enabled; /* 82 1 */
bool irq_wake_enabled; /* 83 1 */
bool close; /* 84 1 */

> +
> +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,
> +};
> +
> +/* 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() would be better. Same goes for other usleep_range() calls.

> +
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val); /* :258 */
> + if (ret)
> + return dev_err_probe(dev, ret, "no response at IRQSRC\n");

Do we need these dev_err_probe() calls at each regmap operation? return ret is good
enough.

> +
> + 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);
> + 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 */
> +
> + msleep(50); /* :280-289 */
> + dev_dbg(dev, "reset done\n");
> +
> + 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;

Blank lines between each function call + if statement group, it improves readability.

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

unsigned int i = 0 as well.

> + 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);
> + }
> + }
> + dev_info(dev, "init table: %zu writes, %u read back different\n",
> + ARRAY_SIZE(isg5320a_setup_reg), mismatch);
> +
> + /* :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];
> + 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);
> +
> + return 0;
> +}
> +

...

> +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);
> +}
> +
> +/* vendor isg5320a_set_enable(), :681-742 (minus the work items) */
> +static int isg5320a_events_on(struct isg5320a_data *data)
> +{
> + struct device *dev = data->dev;
> + unsigned int val;
> + int ret;
> +
> + ret = pm_runtime_resume_and_get(dev);

You could use PM_RUNTIME_ACQUIRE_AUTOSUSPEND() to ensure the device is suspended
on scope exit. It works on the same cleanup.h magic as guard().

> + if (ret)
> + return ret;
> +
> + scoped_guard(mutex, &data->lock) {
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSTS, &val); /* :700 */
> + if (ret)
> + break;

With the macro mentioned above you could just return here and don't need the if (ret)
check below.

> + data->close = val & ISG5320A_IRQSTS_PROX; /* :708-713 */
> + isg5320a_read_sample(data); /* :704 */
> + ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val); /* :718 */
> + if (ret)
> + break;
> + ret = regmap_write(data->regmap, ISG5320A_REG_IRQFUNC, /* :719 */
> + ISG5320A_IRQFUNC_ENABLE);
> + }
> + if (ret) {
> + pm_runtime_put_autosuspend(dev);
> + return ret;
> + }
> +
> + WRITE_ONCE(data->ev_enabled, true);
> + enable_irq(data->irq); /* :721 */
> + /*
> + * Cache the decision: device_may_wakeup(dev) may change before
> + * events_off(), and we must only clear a wake enable we actually
> + * set, so only mark it enabled if enable_irq_wake() succeeded.
> + */
> + if (device_may_wakeup(dev))
> + data->irq_wake_enabled = !enable_irq_wake(data->irq); /* :722 */
> +
> + dev_dbg(dev, "events on, initial state %s\n", data->close ? "close" : "far");
> +
> + return 0;
> +}
> +

...

> +/* vendor isg5320a_remove(), :2030-2033 */
> +static void isg5320a_teardown(void *arg)
> +{
> + struct isg5320a_data *data = arg;
> + struct device *dev = data->dev;
> +
> + pm_runtime_disable(dev);
> +
> + if (data->ev_enabled) {
> + isg5320a_events_off(data);
> + pm_runtime_put_noidle(dev);
> + }
> +
> + if (!pm_runtime_status_suspended(dev))
> + isg5320a_mode_sleep(data);
> +
> + pm_runtime_set_suspended(dev);

> + pm_runtime_dont_use_autosuspend(dev);

No need for this, I've changed the PM runtime core to call this automatically
on driver exit :)

> +}
> +
> +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;

+ blank line

> + 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. */
> + ret = devm_regulator_get_enable_optional(dev, "vdd");
> + 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)
> + usleep_range(10000, 11000);

fsleep()

> +
> + 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 */
> +
> + ret = isg5320a_initialize(data); /* :1620 */
> + if (ret)
> + return dev_err_probe(dev, ret, "init failed\n");
> +
> + ret = isg5320a_mode_normal(data); /* :1621 */
> + 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.
> + */
> + 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);
> +
> + if (data->irq > 0) {
> + irqflags = irq_get_trigger_type(data->irq);
> + if (!irqflags)
> + irqflags = IRQF_TRIGGER_FALLING; /* :1932 */
> +
> + 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");

Duplicate error call, devm_request_threaded_irq() already does this on failure.

> +
> + if (device_property_read_bool(dev, "wakeup-source"))
> + device_init_wakeup(dev, true);
> + }
> +
> + /*
> + * 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.
> + */
> + 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);
> +}

--
Kind regards,
Joshua Crofts