* [PATCH v4 2/2] iio: proximity: add Imagis ISG5320A SAR sensor driver
2026-12-05 3:35 [PATCH v4 0/2] iio: proximity: add Imagis ISG5320A (Samsung Galaxy XCover5) Adil Hüsrev Tepe
2026-12-05 3:35 ` [PATCH v4 1/2] dt-bindings: iio: proximity: add Imagis ISG5320A Adil Hüsrev Tepe
@ 2026-12-05 3:35 ` Adil Hüsrev Tepe
2026-12-05 3:58 ` sashiko-bot
2026-12-05 11:08 ` [PATCH v4 0/2] iio: proximity: add Imagis ISG5320A (Samsung Galaxy XCover5) Andy Shevchenko
2 siblings, 1 reply; 6+ messages in thread
From: Adil Hüsrev Tepe @ 2026-12-05 3:35 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley
Cc: linux-iio, devicetree, linux-kernel, phone-devel, Joshua Crofts,
Adil Hüsrev Tepe
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.
There is no public datasheet; the register map, init table and the
reset and calibration sequences follow Samsung's vendor driver.
Signed-off-by: Adil Hüsrev Tepe <tadilhusrev@gmail.com>
---
v4 (reviews from Jonathan Cameron, Joshua Crofts, Andy Shevchenko and
Sashiko AI):
- Drop the per-line references to the vendor driver; one general
reference in the header comment.
- Write the init table and the sleep/normal sequences with
regmap_multi_reg_write(); the delays live in the reg_sequence.
- Init table: 29 of the 150 entries equal the value read back after a
soft reset (measured on one unit). The register layout is not
documented and 27 of them are next to a register that the table
changes, so they may be part of a multi-byte value and stay. The
other two (0x41 and 0xC5) are dropped.
- Drop the init-table read-back check, the CHIPID read and the
dev_info()/dev_dbg() output.
- No register cache; a comment says why.
- fsleep() for all delays, with comments saying where the reset and
scan-stop waits come from. The 5 ms before the first access is folded
into the power-on delay.
- get_unaligned_be16() for the 16-bit big-endian registers.
- Get the struct device from the regmap instead of storing it.
- guard() held to the end of the function instead of scoped_guard().
No break or goto next to guard(): the IRQ thread reads the status in
a helper and returns.
- Events are gated at the source (IRQFUNC) only: no disable_irq(), no
IRQF_NO_AUTOEN; the IRQ thread no longer checks ev_enabled.
- No synchronize_irq() either: on unbind the IRQ is freed before the
chip is put to sleep.
- PM_RUNTIME_ACQUIRE_AUTOSUSPEND() in read_raw() and events_on().
events_on() takes one more reference with pm_runtime_get_noresume()
that enabled events keep and events_off() drops, so the get and the
put are both inside these two functions.
- No wakeup support for now: drop enable_irq_wake() and
device_init_wakeup(); use DEFINE_RUNTIME_DEV_PM_OPS() so the chip is
also put to sleep on system suspend.
- devm_pm_runtime_set_active_enabled() and autosuspend without the
get_noresume/put dance.
- The teardown is split into two devm actions, each registered right
after what it undoes. The sleep action follows the chip start, so a
failed IRQ request no longer leaves the chip scanning (Sashiko,
Medium); it puts the chip to sleep unconditionally. The events-off
action follows the IRQ request; on unbind it turns enabled events off
and drops their runtime PM reference.
- Drop shutdown() (Sashiko, Low).
- Drop the IRQF_TRIGGER_FALLING fallback; the trigger comes from DT.
- The interrupt is required, as in the binding; drop the code path for
a device without one.
- Use devm_regulator_get_enable() instead of the _optional variant and
always apply the power-on delay. On the XCover5 the supply is a PMIC
LDO referenced by vdd-supply, not a fixed regulator as I wrote in my
reply to the v3 review.
- Clamp the reported falling threshold at 0; what the chip does with a
far hysteresis above the threshold is not known. With the current
table the value is 300 - 150.
- Open-code the channel specs instead of a macro.
- Drop the cfcal_th warning.
- Reorder struct isg5320a_data (pahole), add err.h, drop
mod_devicetable.h, blank lines between statement groups, no
duplicate error message after devm_request_threaded_irq().
Andy Shevchenko's review of v2, which v3 did not address:
- Headers: mod_devicetable.h and pm_wakeup.h are gone; time.h,
minmax.h, lockdep.h and dev_printk.h are added for what the file
uses.
- ISG5320A_AUTOSUSPEND_MS is (5 * MSEC_PER_SEC).
- ISG5320A_RESET_CONDITION and its comment are gone with the cfcal_th
code.
- The redundant dev pointer in the private struct is gone.
- bool members: ev_enabled and close are protected by different locks,
so they stay two plain bools instead of bit-fields; they are at the
end of the struct.
- REGCACHE_NONE is justified in a comment.
- The sleeps are explained. The INT line is no longer read as a GPIO,
so readx_poll_timeout() does not apply.
- isg5320a_initialize() no longer takes the mutex: it only runs from
probe, before the IRQ is requested, runtime PM is enabled and the IIO
device is registered.
- The init table holds settings to write, not register defaults, and
there is no cache to seed, so it is a struct reg_sequence for
regmap_multi_reg_write() rather than reg_defaults.
- get_unaligned_be16(), the devm runtime PM helper, no trigger-type
fallback and no extra message after devm_request_threaded_irq(), as
listed above.
---
drivers/iio/proximity/Kconfig | 11 +
drivers/iio/proximity/Makefile | 1 +
drivers/iio/proximity/isg5320a.c | 745 +++++++++++++++++++++++++++++++++++++++
3 files changed, 757 insertions(+)
diff --git a/drivers/iio/proximity/Kconfig b/drivers/iio/proximity/Kconfig
index bb77fad2a1b..311a7b9e42a 100644
--- a/drivers/iio/proximity/Kconfig
+++ b/drivers/iio/proximity/Kconfig
@@ -67,6 +67,17 @@ config IRSD200
To compile this driver as a module, choose M here: the module will be
called irsd200.
+config ISG5320A
+ tristate "Imagis ISG5320A capacitive SAR/grip sensor"
+ depends on I2C
+ select REGMAP_I2C
+ help
+ Say Y here to build a driver for the Imagis ISG5320A capacitive
+ SAR/grip sensor.
+
+ To compile this driver as a module, choose M here: the module will
+ be called isg5320a.
+
config ISL29501
tristate "Intersil ISL29501 Time Of Flight sensor"
depends on I2C
diff --git a/drivers/iio/proximity/Makefile b/drivers/iio/proximity/Makefile
index 4352833dd8a..6aa606fa5cb 100644
--- a/drivers/iio/proximity/Makefile
+++ b/drivers/iio/proximity/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_CROS_EC_MKBP_PROXIMITY) += cros_ec_mkbp_proximity.o
obj-$(CONFIG_D3323AA) += d3323aa.o
obj-$(CONFIG_HX9023S) += hx9023s.o
obj-$(CONFIG_IRSD200) += irsd200.o
+obj-$(CONFIG_ISG5320A) += isg5320a.o
obj-$(CONFIG_ISL29501) += isl29501.o
obj-$(CONFIG_LIDAR_LITE_V2) += pulsedlight-lidar-lite-v2.o
obj-$(CONFIG_MB1232) += mb1232.o
diff --git a/drivers/iio/proximity/isg5320a.c b/drivers/iio/proximity/isg5320a.c
new file mode 100644
index 00000000000..407f2c7e2d8
--- /dev/null
+++ b/drivers/iio/proximity/isg5320a.c
@@ -0,0 +1,745 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Imagis ISG5320A capacitive SAR/grip sensor.
+ *
+ * The register map, init table and the reset/calibration sequences and
+ * their timings follow Samsung's vendor driver for the Galaxy XCover5
+ * (drivers/sensors/isg5320a.c, Copyright (C) Imagis Technology); there is
+ * no public datasheet.
+ *
+ * The chip is used as one proximity channel: 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/dev_printk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/lockdep.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+
+#include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+
+#define ISG5320A_REG_IRQSRC 0x00 /* read releases the INT line */
+#define ISG5320A_REG_IRQSTS 0x01
+#define ISG5320A_IRQSTS_PROX BIT(4)
+#define ISG5320A_REG_OSCCON 0x06
+#define ISG5320A_OSC_SLEEP 0xB0
+#define ISG5320A_OSC_NORMAL 0xF0
+#define ISG5320A_REG_IRQFUNC 0x07
+#define ISG5320A_IRQFUNC_ENABLE 0x0D
+#define ISG5320A_IRQFUNC_DISABLE 0x13
+#define ISG5320A_REG_BS_ON_WD 0x19
+#define ISG5320A_BS_WD_OFF 0x0A
+#define ISG5320A_BS_WD_ON 0x8A
+#define ISG5320A_REG_SCANCTRL1 0x38
+#define ISG5320A_SCAN_STOP 0x00
+#define ISG5320A_CFCAL_START 0xDD
+#define ISG5320A_CFCAL_BUSY BIT(4)
+#define ISG5320A_REG_SCANCTRL2 0x39
+#define ISG5320A_SCAN2_RESET 0x00
+#define ISG5320A_SCAN2_CLEAR 0x60
+#define ISG5320A_BFCAL_START 0x07
+/* CDC16_T_H/L then BSL16_B_H/L, all big endian */
+#define ISG5320A_REG_CDC16_T_H 0x5C
+/* B_PROXCTL3..8: threshold, close and far hysteresis, 16 bit big endian */
+#define ISG5320A_REG_B_PROXCTL3 0x9B
+#define ISG5320A_REG_PROTECT 0xCD
+#define ISG5320A_PROTECT_UNLOCK 0xDE
+#define ISG5320A_REG_SOFTRESET 0xFD
+#define ISG5320A_SOFTRESET_VAL 0xDE
+#define ISG5320A_MAX_REG 0xFE
+
+#define ISG5320A_AUTOSUSPEND_MS (5 * MSEC_PER_SEC)
+
+/*
+ * Vendor setup table for the XCover5. The register layout is not documented.
+ * Two entries that only repeat the value the register has after the soft
+ * reset are left out (0x41, 0xC5). Other such entries are kept, because an
+ * adjacent register changes and the two may be parts of one value.
+ */
+static const struct reg_sequence isg5320a_setup_reg[] = {
+ { 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 }, { 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 }, { 0x13, 0x3A },
+ { 0x18, 0xF1 }, /* A channel on CS0, B channel on 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 },
+};
+
+static const struct reg_sequence isg5320a_sleep_seq[] = {
+ { ISG5320A_REG_SCANCTRL1, ISG5320A_SCAN_STOP },
+ { ISG5320A_REG_OSCCON, ISG5320A_OSC_SLEEP },
+ { ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_OFF },
+};
+
+/*
+ * Restart scanning and force a calibration. The delays (in us) match the
+ * vendor driver.
+ */
+static const struct reg_sequence isg5320a_normal_seq[] = {
+ { ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_ON },
+ { ISG5320A_REG_SCANCTRL1, ISG5320A_SCAN_STOP, 30 * USEC_PER_MSEC },
+ { ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_OFF },
+ { ISG5320A_REG_SCANCTRL2, ISG5320A_SCAN2_CLEAR, 100 * USEC_PER_MSEC },
+ { ISG5320A_REG_OSCCON, ISG5320A_OSC_NORMAL },
+ { ISG5320A_REG_BS_ON_WD, ISG5320A_BS_WD_ON },
+ { ISG5320A_REG_SCANCTRL2, ISG5320A_SCAN2_RESET },
+ { ISG5320A_REG_SCANCTRL1, ISG5320A_CFCAL_START, 500 * USEC_PER_MSEC },
+ /* 300 ms for the calibration to settle, to match the vendor driver */
+ { ISG5320A_REG_SCANCTRL2, ISG5320A_BFCAL_START, 300 * USEC_PER_MSEC },
+};
+
+struct isg5320a_data {
+ struct regmap *regmap;
+ /* Serialises multi-register chip sequences and the cached sample. */
+ struct mutex lock;
+ /* Serialises event enable/disable and protects ev_enabled. */
+ struct mutex ev_lock;
+ u16 cdc;
+ u16 base;
+ u16 thresh;
+ u16 close_hyst;
+ u16 far_hyst;
+ bool ev_enabled;
+ bool close;
+};
+
+static bool isg5320a_writeable_reg(struct device *dev, unsigned int reg)
+{
+ 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 (unsigned int i = 0; i < ARRAY_SIZE(isg5320a_setup_reg); i++)
+ if (isg5320a_setup_reg[i].reg == 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;
+}
+
+/*
+ * No register cache: configuration registers are only written as whole bytes
+ * from fixed sequences, never read-modify-written. The only reads are the
+ * threshold registers, once at probe, and status and data registers that the
+ * chip updates itself.
+ */
+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,
+};
+
+/*
+ * Soft reset. The two waits follow the vendor driver, which
+ * - reads IRQSRC to release the INT line and then polls the line for up
+ * to 50 ms until it is high;
+ * - after the reset command waits 1 ms and then polls the line for up to
+ * 50 ms until the chip pulls it low.
+ * The INT line is not polled here, so the full time is waited in both
+ * places.
+ */
+static int isg5320a_reset(struct isg5320a_data *data)
+{
+ unsigned int val;
+ int ret;
+
+ /* Also checks that the chip answers at all. */
+ ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val);
+ if (ret)
+ return ret;
+
+ fsleep(50 * USEC_PER_MSEC);
+
+ ret = regmap_write(data->regmap, ISG5320A_REG_PROTECT,
+ ISG5320A_PROTECT_UNLOCK);
+ if (ret)
+ return ret;
+
+ ret = regmap_write(data->regmap, ISG5320A_REG_SOFTRESET,
+ ISG5320A_SOFTRESET_VAL);
+ if (ret)
+ return ret;
+
+ fsleep(51 * USEC_PER_MSEC);
+
+ return 0;
+}
+
+static int isg5320a_initialize(struct isg5320a_data *data)
+{
+ unsigned int val;
+ u8 buf[6];
+ int ret;
+
+ 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;
+
+ /* The vendor driver waits 30 ms after stopping the scan. */
+ fsleep(30 * USEC_PER_MSEC);
+
+ ret = regmap_multi_reg_write(data->regmap, isg5320a_setup_reg,
+ ARRAY_SIZE(isg5320a_setup_reg));
+ if (ret)
+ return ret;
+
+ ret = regmap_bulk_read(data->regmap, ISG5320A_REG_B_PROXCTL3, buf,
+ sizeof(buf));
+ if (ret)
+ return ret;
+
+ data->thresh = get_unaligned_be16(&buf[0]);
+ data->close_hyst = get_unaligned_be16(&buf[2]);
+ data->far_hyst = get_unaligned_be16(&buf[4]);
+
+ return 0;
+}
+
+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;
+
+ return regmap_multi_reg_write(data->regmap, isg5320a_sleep_seq,
+ ARRAY_SIZE(isg5320a_sleep_seq));
+}
+
+static int isg5320a_mode_normal(struct isg5320a_data *data)
+{
+ unsigned int tries, val;
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val);
+ if (ret)
+ return ret;
+
+ /*
+ * The chip clears ISG5320A_CFCAL_BUSY once the calibration has run.
+ * Now and then the bit stays set and nothing is measured. Running
+ * the sequence again on the running chip does not help; it has to be
+ * put to sleep first.
+ */
+ for (tries = 3; tries; tries--) {
+ ret = regmap_multi_reg_write(data->regmap, isg5320a_normal_seq,
+ ARRAY_SIZE(isg5320a_normal_seq));
+ if (ret)
+ return ret;
+
+ ret = regmap_read(data->regmap, ISG5320A_REG_SCANCTRL1, &val);
+ if (ret)
+ return ret;
+
+ if (!(val & ISG5320A_CFCAL_BUSY))
+ return 0;
+
+ ret = regmap_multi_reg_write(data->regmap, isg5320a_sleep_seq,
+ ARRAY_SIZE(isg5320a_sleep_seq));
+ if (ret)
+ return ret;
+ }
+
+ return -ETIMEDOUT;
+}
+
+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 not valid samples; keep the previous value. */
+ v = get_unaligned_be16(&buf[0]);
+ if (v != 0 && v != 0xFFFF)
+ data->cdc = v;
+
+ v = get_unaligned_be16(&buf[2]);
+ if (v != 0 && v != 0xFFFF)
+ data->base = v;
+
+ return 0;
+}
+
+static int isg5320a_read_irq_status(struct isg5320a_data *data, bool *close)
+{
+ unsigned int val;
+ int ret;
+
+ lockdep_assert_held(&data->lock);
+
+ ret = isg5320a_read_sample(data);
+ if (ret)
+ return ret;
+
+ ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val);
+ if (ret)
+ return ret;
+
+ ret = regmap_read(data->regmap, ISG5320A_REG_IRQSTS, &val);
+ if (ret)
+ return ret;
+
+ *close = val & ISG5320A_IRQSTS_PROX;
+
+ 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);
+ enum iio_event_direction dir;
+ bool close;
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ ret = isg5320a_read_irq_status(data, &close);
+ if (ret) {
+ dev_err_ratelimited(regmap_get_device(data->regmap),
+ "irq status read failed: %d\n", ret);
+ return IRQ_HANDLED;
+ }
+
+ if (close == data->close)
+ return IRQ_HANDLED;
+
+ data->close = close;
+
+ 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_raw(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ int *val, int *val2, long mask)
+{
+ struct isg5320a_data *data = iio_priv(indio_dev);
+ struct device *dev = regmap_get_device(data->regmap);
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW: {
+ PM_RUNTIME_ACQUIRE_AUTOSUSPEND(dev, pm);
+ ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+ if (ret)
+ return ret;
+
+ guard(mutex)(&data->lock);
+
+ ret = isg5320a_read_sample(data);
+ if (ret)
+ return ret;
+
+ *val = (int)data->cdc - (int)data->base;
+
+ return IIO_VAL_INT;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+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;
+
+ guard(mutex)(&data->ev_lock);
+
+ return data->ev_enabled;
+}
+
+static int isg5320a_events_on(struct isg5320a_data *data)
+{
+ struct device *dev = regmap_get_device(data->regmap);
+ unsigned int val;
+ int ret;
+
+ PM_RUNTIME_ACQUIRE_AUTOSUSPEND(dev, pm);
+ ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+ if (ret)
+ return ret;
+
+ guard(mutex)(&data->lock);
+
+ ret = regmap_read(data->regmap, ISG5320A_REG_IRQSTS, &val);
+ if (ret)
+ return ret;
+
+ data->close = val & ISG5320A_IRQSTS_PROX;
+
+ /* Release the INT line before unmasking the interrupt. */
+ ret = regmap_read(data->regmap, ISG5320A_REG_IRQSRC, &val);
+ if (ret)
+ return ret;
+
+ ret = regmap_write(data->regmap, ISG5320A_REG_IRQFUNC,
+ ISG5320A_IRQFUNC_ENABLE);
+ if (ret)
+ return ret;
+
+ /* Enabled events keep the chip awake until isg5320a_events_off(). */
+ pm_runtime_get_noresume(dev);
+ data->ev_enabled = true;
+
+ return 0;
+}
+
+static int isg5320a_events_off(struct isg5320a_data *data)
+{
+ struct device *dev = regmap_get_device(data->regmap);
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ ret = regmap_write(data->regmap, ISG5320A_REG_IRQFUNC,
+ ISG5320A_IRQFUNC_DISABLE);
+
+ data->ev_enabled = false;
+
+ /* Drop the reference kept by isg5320a_events_on(). */
+ pm_runtime_put_autosuspend(dev);
+
+ return ret;
+}
+
+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);
+
+ return isg5320a_events_off(data);
+}
+
+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;
+
+ switch (dir) {
+ case IIO_EV_DIR_RISING:
+ *val = data->thresh + data->close_hyst;
+ return IIO_VAL_INT;
+ case IIO_EV_DIR_FALLING:
+ /*
+ * What the chip does with a far hysteresis above the
+ * threshold is not known; do not report a negative value.
+ */
+ *val = max((int)data->thresh - (int)data->far_hyst, 0);
+ return IIO_VAL_INT;
+ default:
+ return -EINVAL;
+ }
+}
+
+static const struct iio_event_spec isg5320a_events[] = {
+ {
+ .type = IIO_EV_TYPE_THRESH,
+ .dir = IIO_EV_DIR_RISING,
+ .mask_separate = BIT(IIO_EV_INFO_VALUE),
+ },
+ {
+ .type = IIO_EV_TYPE_THRESH,
+ .dir = IIO_EV_DIR_FALLING,
+ .mask_separate = BIT(IIO_EV_INFO_VALUE),
+ },
+ {
+ /* One chip-level IRQ enable (IRQFUNC) covers both directions. */
+ .type = IIO_EV_TYPE_THRESH,
+ .dir = IIO_EV_DIR_EITHER,
+ .mask_separate = BIT(IIO_EV_INFO_ENABLE),
+ },
+};
+
+static const struct iio_chan_spec isg5320a_channels[] = {
+ {
+ .type = IIO_PROXIMITY,
+ .indexed = 1,
+ .channel = 0,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+ .event_spec = isg5320a_events,
+ .num_event_specs = ARRAY_SIZE(isg5320a_events),
+ },
+};
+
+static const struct iio_info isg5320a_info = {
+ .read_raw = isg5320a_read_raw,
+ .read_event_config = isg5320a_read_event_config,
+ .write_event_config = isg5320a_write_event_config,
+ .read_event_value = isg5320a_read_event_value,
+};
+
+static void isg5320a_sleep_action(void *arg)
+{
+ struct isg5320a_data *data = arg;
+
+ /*
+ * Undo isg5320a_mode_normal(). Runtime PM is already disabled when
+ * this runs and the chip may or may not be asleep, so the sleep
+ * sequence is written unconditionally; it only sets registers to
+ * fixed values.
+ */
+ isg5320a_mode_sleep(data);
+}
+
+static void isg5320a_events_off_action(void *arg)
+{
+ struct isg5320a_data *data = arg;
+
+ guard(mutex)(&data->ev_lock);
+
+ /* Events left enabled still hold their runtime PM reference. */
+ if (data->ev_enabled)
+ isg5320a_events_off(data);
+}
+
+static int isg5320a_probe(struct i2c_client *client)
+{
+ struct device *dev = &client->dev;
+ struct isg5320a_data *data;
+ struct iio_dev *indio_dev;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ data = iio_priv(indio_dev);
+ 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;
+
+ ret = devm_regulator_get_enable(dev, "vdd");
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to enable vdd\n");
+
+ /*
+ * The chip's start-up time is not documented. The vendor driver waits
+ * 5 ms before the first access with the supply already on; 15 ms is
+ * used here and works on the XCover5.
+ */
+ fsleep(15 * USEC_PER_MSEC);
+
+ 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);
+ if (ret)
+ return dev_err_probe(dev, ret, "reset failed\n");
+
+ /* 300 ms between reset and init, to match the vendor driver */
+ fsleep(300 * USEC_PER_MSEC);
+
+ ret = isg5320a_initialize(data);
+ if (ret)
+ return dev_err_probe(dev, ret, "init failed\n");
+
+ ret = isg5320a_mode_normal(data);
+ if (ret)
+ return dev_err_probe(dev, ret, "start failed\n");
+
+ ret = devm_add_action_or_reset(dev, isg5320a_sleep_action, data);
+ if (ret)
+ return ret;
+
+ pm_runtime_set_autosuspend_delay(dev, ISG5320A_AUTOSUSPEND_MS);
+ pm_runtime_use_autosuspend(dev);
+
+ ret = devm_pm_runtime_set_active_enabled(dev);
+ if (ret)
+ return ret;
+
+ indio_dev->name = "isg5320a";
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->info = &isg5320a_info;
+ indio_dev->channels = isg5320a_channels;
+ indio_dev->num_channels = ARRAY_SIZE(isg5320a_channels);
+
+ ret = devm_request_threaded_irq(dev, client->irq, NULL,
+ isg5320a_irq_thread, IRQF_ONESHOT,
+ "isg5320a", indio_dev);
+ if (ret)
+ return ret;
+
+ /* On unbind: after the IIO device is gone, before the IRQ is freed. */
+ ret = devm_add_action_or_reset(dev, isg5320a_events_off_action, data);
+ if (ret)
+ return ret;
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static int isg5320a_runtime_suspend(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct isg5320a_data *data = iio_priv(indio_dev);
+
+ return isg5320a_mode_sleep(data);
+}
+
+static int isg5320a_runtime_resume(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct isg5320a_data *data = iio_priv(indio_dev);
+
+ return isg5320a_mode_normal(data);
+}
+
+static DEFINE_RUNTIME_DEV_PM_OPS(isg5320a_pm_ops, isg5320a_runtime_suspend,
+ isg5320a_runtime_resume, NULL);
+
+static const struct of_device_id isg5320a_of_match[] = {
+ { .compatible = "imagis,isg5320a" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, isg5320a_of_match);
+
+static const struct i2c_device_id isg5320a_id[] = {
+ { .name = "isg5320a" },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, isg5320a_id);
+
+static struct i2c_driver isg5320a_driver = {
+ .driver = {
+ .name = "isg5320a",
+ .of_match_table = isg5320a_of_match,
+ .pm = pm_ptr(&isg5320a_pm_ops),
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+ },
+ .probe = isg5320a_probe,
+ .id_table = isg5320a_id,
+};
+module_i2c_driver(isg5320a_driver);
+
+MODULE_AUTHOR("Adil Hüsrev Tepe <tadilhusrev@gmail.com>");
+MODULE_DESCRIPTION("Imagis ISG5320A capacitive SAR/grip sensor");
+MODULE_LICENSE("GPL");
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread