From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.218.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796441753; b=VZ2NGDUAw1EVDL3ouME094MwNubpHGBi1f9StzlZz33b4Losffuhj7aCChljg/bBpsFk zcJGH5IjUpJaCgygf3ThjpITR/HJNTTJvKo25fqxU+S22zF77UEF9X6plpTGlO0HbPzX+ uMg+QmGkshM52v+A76pmrIrN32SUyILk1TzlcxPJS+Ev+1Of31WN/oaTyi4kxV6wxGdA/ XtTe/tUVyhecgDgG+ISGJOVRD5cWk8/QMMY64QQ3+SCMqy9GHPRcOJir6hB9PbrX5Ura5 +j3+JDsRfI73MlX8yNClhynvmRqwZOdVN5zv5PLwSzfgzZLzoAwLmICkb1vf8mZhbkA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796441753; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=cs2ADaKSe/icIVxWKKhEj+kA3jIueRbOdwIbyXn/P9E=; b=0hMzhtQ+dc9/TFlX/bjpKt0YYU+6kEOhd0oVs1xNln5FQFtUmORkVDjjjFgYY5+7GeDa a+VI0ktifxM+bZDG8Hzw76gxSJI5qD7TakWVKcPjfWUI6qkuRnsW/SQ6t4nXn6mYVM8Zo Dvj4nBvN957daoW9ASMZjAqIUTmTabIwxUhXXhng+GBXcQxQ8vwpu9Q4zjKQ1eKlsofnC CGq8EL7IV/Glp2Plkh21RUUDNca4yVFUsAIPhPE2iCmfF7PbuGSzr6oiNK5JF5gLqBqHu XujcOscPPAf/R6VCHIS0swibUo5BglfC5fYoFjtrFohnilcVX+eULjui+Kpi66meLUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=eS7O1CtD; arc=none smtp.remote-ip=209.85.218.42 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AF04606A334 for ; Sat, 5 Dec 2026 03:35:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8AF04606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=eS7O1CtD Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-c5126505cb3so265223666b.0 for ; Fri, 04 Dec 2026 19:35:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796441751; x=1797046551; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cs2ADaKSe/icIVxWKKhEj+kA3jIueRbOdwIbyXn/P9E=; b=eS7O1CtDqEOlr/ln5Nx7aQRrAVuktbVdFYeai3c7P8IucHUXe6xqRwvzVZVaK9YJAK z0qAyimRc+XMPB1DwkuLbCo6IdeqrtcMVmRie5kwnFWc0Yq13EQqHfzb3H+717LCG1Df 8cnzFmj86q2Bgx2l7J/+8bVU96EFSUbj6qfV2jlUMLoebpJxJebq2nmOKhhphXUkQh/T gaNFDMuD8WojDcZdGEcQ+St4L+EWSH1So6VZ3pT8TvwcunTxCTXyKQfsXpwBTOQkz+8f /xrQ4jHoJX3lFKDlsPjfs2flh9UA7EkWqmNF2DkyJrZJqot2xPFmE1NJxN6O32nvJZaK 3prg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796441751; x=1797046551; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cs2ADaKSe/icIVxWKKhEj+kA3jIueRbOdwIbyXn/P9E=; b=dgJlwdiIuaRaBNEfwtGn5GMggi/SC+1zRfcB6gnri+LE64n8MRR/gCX6pZyS+QD3/m YzHDobiyS9t3PJGzS8n4+wbER2cbNnQLgKsWLXu+vGSRxH3Zi54M50V2e8tCbq8uLOCa FpSA1D5aLxE/wsg72u8Hh2WTvkoSs31rPgfbfdpaEUwf3j4b57MrZHp2JanOZ72+HCPx 9nbLHegjHtdxliylLAwoU1RNv5Kokm9iycYuotP/t9kvv5hVVEu+d/oUQCDxf+pKLVnu 98Rnzhwfno4ccKgOmItTy2mIpWd0Vk7yU1D1vTCciUO+V+jnveI0CUYfJIyFnsnIb2AK 9oCA== X-Forwarded-Encrypted: i=1; AL+hErF2IzZE4e9vPtwF4zFDpiOlrcBYxZySKaN9BTN0yYZMAqmCKTYffBjhnLcC8Z7NVzIKTk06S5JSs+42@vger.kernel.org X-Gm-Message-State: APuU4Z/C+dPjnxlzV0MLzlnnoqWCj5dBhxGJ5J8HDC9rDD12A7D0qAQa lLFlm5T9Vgvr+sDRSJ6KC5TOhOIxGf2ncMvyWFmE+BPXIkQhjC9aE4Mg X-Gm-Gg: AZFdTZeEVQeKCHkUEXhMd8QRce8cfvZpzylCBVdwdBMiuAO3fZJF14gzA9mazp+rY9F 9RnRI2dipZrNox67mXjqk0q4bEmp36JQdzOEfeMCBj014+Y3fP+BQKJDl/QksSUB6unZrdRlEDK ZaaKh0689TiIvD9N9c6+6QvOXX76s8mvESf3gXzh90yTUpOwcN3NNU4oT65Weg+Cxk5gpWgFKe4 2Nd5zv53QJlcuR+9hoZV55u0ztcktNqQTxfO6bCoYHpC2nulC4GU0DV7/3wViV5O5MeZ81wJFZT LlSUWik7H3s0aihvR1CKrM3Kj1Lyt88HbX0wZyZzplUkjUqvm+FBHqrGzW7TjigUc3Zki/G1DtQ iZIPl+FvV6fLGBaUYUMLgaXCHxmOcCBbDkGvJsePJdY5M8lW6PrlJekar4U4CNlYg+HweJV61Dh scvKqkT32BK1QBCW/jlR0m1zgGlWFmVxW1WVQjWawYMEan55LCo/WOOWbQ4s8eL7geWoyRfcM+a axZcVVn4LgIZl82e+YlxGSOyiTv2E/O0vhrlKA9dp5JSBrl1Hlm8hs8frPW4i2FbvS98nrVx3iR Vv9jK5Yl2+8maqaVaeS5MFbai3Lvgh0RVg== X-Received: by 2002:a17:907:3c82:b0:c52:58d6:d69d with SMTP id a640c23a62f3a-c52624bf96fmr269868166b.39.1796441750694; Fri, 04 Dec 2026 19:35:50 -0800 (PST) Received: from [127.0.1.1] ([195.155.168.196]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c525b2b7568sm199708566b.2.2026.12.04.19.35.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 19:35:48 -0800 (PST) From: =?utf-8?q?Adil_H=C3=BCsrev_Tepe?= Date: Sat, 05 Dec 2026 06:35:41 +0300 Subject: [PATCH v4 2/2] iio: proximity: add Imagis ISG5320A SAR sensor driver X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20261205-isg5320a-v4-2-a9bb8573c064@gmail.com> References: <20261205-isg5320a-v4-0-a9bb8573c064@gmail.com> In-Reply-To: <20261205-isg5320a-v4-0-a9bb8573c064@gmail.com> To: Jonathan Cameron , David Lechner , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, phone-devel@vger.kernel.org, Joshua Crofts , =?utf-8?q?Adil_H=C3=BCsrev_Tepe?= X-Mailer: b4 0.16.0 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 --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#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 "); +MODULE_DESCRIPTION("Imagis ISG5320A capacitive SAR/grip sensor"); +MODULE_LICENSE("GPL"); -- 2.53.0