From nobody Sun Dec 14 08:08:30 2025 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49AD846BF for ; Mon, 27 Jan 2025 14:04:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737986695; cv=none; b=uXSmXtbLuxfukfzA7BPvTAiho1B7VnGc9JiawTri3J97Kfe051NYpGb9JkxKkhThRm2YaCctC//Agx2wk7OIOHqWPjKpMaK4Ng16qolbsG+QlE9ILzm3U/D+TdxcyFF58r0+znLrqUtEpewEMbwiA8iNE/9gz5d7XOuffOSasow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737986695; c=relaxed/simple; bh=UpHNkBI+znuAR7QFzUQVnchxDjBRHfzjxtWP5dwdcCA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DrA00iHKe73FPynWIoNHNZhc0AhwxinCV0ruzEf4UnxzbnNy6H3H8vGv7sK3Wuno/A7tilFbrgecHTPHLvwJQgg/RnnB4oFwlFmLkQ9WCIDiHNjuMr/o119ST2XZu0PTTyXz9wnEwKoH9++Q0ovbaqWMNUOSPgs/j92E7W4vtA0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=xBkPRinv; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="xBkPRinv" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-438a3216fc2so46339125e9.1 for ; Mon, 27 Jan 2025 06:04:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1737986691; x=1738591491; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=iNC2vjMhon2bW9VLQSkA9jwR9CGK+U2zRjC3qRDo0Bc=; b=xBkPRinv3Bi4yu9UlbFpnqn5Ud2YsOs7RltIrKzwW0yRX/wKh4sYtSCFCoVlAg9kGi UpuMRyOqt5JVB/VG10OCGCWeLs0hFNrcipkEnRC1qxA3dp5bKY6tLBPPOUK7UmYQ+V1m EM2u6pl33GzVR6DVaAZ9Fkt/pqT2ZCyG2jUKW8Rz/XJhazUfdSXtHYe/eVtXuFJQ5UJa t4N+L716ioPjcXnAjJSHlCDUWJ+H0II8F9p9Ycjw2xQ7VGFjnkWFd+Li22pYamLN5ASl 4XoBDa1cR6VoGcniUB0DAbuI9P/nPjztiQg/RP8MN7T4fNrWx8m3S+XQoPwNSYo8Q6++ emLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737986691; x=1738591491; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iNC2vjMhon2bW9VLQSkA9jwR9CGK+U2zRjC3qRDo0Bc=; b=jnXF+u3HuoXD7kcVMTYqY9xgGIv9dY7eYdXPgPNg7lK64Iiqr31DamQ70b9YUiBClu 0jBWbkyQKMPO29oETFXEaQVW5aUTMJY1SIuQZHG0P+YpU2bTNa7L0TWQ9eMtMZHpcKh4 iQWm22EZMgC+TCpVRJTVsXfgUszRl91zQD8saDNG74thGuQpCJSzOmQ1MwKMvadSmZZA yChS8r+IX/bV8ug29TZJaYGVDB3rDYTcwuyBna7/rAY9mR4KZNAzHU5JdJSBQF/PyeJz n2wonpa7ZROLu+yKTwuq1eB+HpzgARGKxmi69FgYr7XgrTEC8SM8anL7Gj0oIEyCeJpT WOmQ== X-Forwarded-Encrypted: i=1; AJvYcCW1uQZzIuWTa1IOcqLklsTfnmRFfKeLBTa+L5OgvnyQ/aPyXzC5j7V2KNGy96EIAr+4gviM1NMJWOlzrZo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/RdUJL5yAXhzm7o8WCzZMyk3DaJyyNVmGCPeCk4q8T0D4NXI6 Cxi7NWIct+aXy1tJXvahsG7/ksWqaoTq6mFydjYAaSpxJiYHIn/yo4uVJ1/tLtU= X-Gm-Gg: ASbGncv63o0wrjKs4U0bucZ6p/oH/wEcrC9zlzuoRbCe0qeLbjnNnxUFpyta2i7E1I+ oAxZO/er8kiHiFiuyTgQPbMIDHmjnnh+Km7Rq7LXlZiPcYhBP1CvOsO1mLkDmSmaYCnUcLILjvx G0FJ3k772heSXHn+Zv+OgQeYgMJoLe3vxv6nJD0DRrCJjvCLjBpWPD0ZNcznuTQSW42/jOQXVi8 mTQ1kXToW/c3EwcJWr83cluP26I5TiQEZNY4GbXp0uh/gTxrYn9df6yAaGIGqoXJfAMuMdyJn6z hmga+n7b5/un0V+HZa8YCfFk3VKSnt63GvHwfkemmbDDJQBmzFM= X-Google-Smtp-Source: AGHT+IGGBWanpFTeP02UHs/S3DLKjDqcOFx6yfwE/ZiViqDo1Y06YNnMhn9DsKK+yDK9DAhTq8qC9w== X-Received: by 2002:adf:edcc:0:b0:385:f44a:a3b with SMTP id ffacd0b85a97d-38bf58e872dmr30759883f8f.41.1737986691499; Mon, 27 Jan 2025 06:04:51 -0800 (PST) Received: from [127.0.0.1] (alille-653-1-300-114.w90-1.abo.wanadoo.fr. [90.1.180.114]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a313383sm11480639f8f.36.2025.01.27.06.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 06:04:50 -0800 (PST) From: Guillaume Ranquet Date: Mon, 27 Jan 2025 14:59:32 +0100 Subject: [PATCH v5 1/2] iio: introduce the FAULT event type Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250127-ad4111_openwire-v5-1-ef2db05c384f@baylibre.com> References: <20250127-ad4111_openwire-v5-0-ef2db05c384f@baylibre.com> In-Reply-To: <20250127-ad4111_openwire-v5-0-ef2db05c384f@baylibre.com> To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Guillaume Ranquet , Nuno Sa X-Mailer: b4 0.15-dev Add a new event type to describe an hardware failure. Reviewed-by: Nuno Sa Signed-off-by: Guillaume Ranquet Reviewed-by: David Lechner --- drivers/iio/industrialio-event.c | 2 ++ include/uapi/linux/iio/types.h | 2 ++ tools/iio/iio_event_monitor.c | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-ev= ent.c index db06501b0e61..06295cfc2da8 100644 --- a/drivers/iio/industrialio-event.c +++ b/drivers/iio/industrialio-event.c @@ -232,6 +232,7 @@ static const char * const iio_ev_type_text[] =3D { [IIO_EV_TYPE_CHANGE] =3D "change", [IIO_EV_TYPE_MAG_REFERENCED] =3D "mag_referenced", [IIO_EV_TYPE_GESTURE] =3D "gesture", + [IIO_EV_TYPE_FAULT] =3D "fault", }; =20 static const char * const iio_ev_dir_text[] =3D { @@ -240,6 +241,7 @@ static const char * const iio_ev_dir_text[] =3D { [IIO_EV_DIR_FALLING] =3D "falling", [IIO_EV_DIR_SINGLETAP] =3D "singletap", [IIO_EV_DIR_DOUBLETAP] =3D "doubletap", + [IIO_EV_DIR_FAULT_OPENWIRE] =3D "openwire", }; =20 static const char * const iio_ev_info_text[] =3D { diff --git a/include/uapi/linux/iio/types.h b/include/uapi/linux/iio/types.h index 12886d4465e4..3eb0821af7a4 100644 --- a/include/uapi/linux/iio/types.h +++ b/include/uapi/linux/iio/types.h @@ -119,6 +119,7 @@ enum iio_event_type { IIO_EV_TYPE_CHANGE, IIO_EV_TYPE_MAG_REFERENCED, IIO_EV_TYPE_GESTURE, + IIO_EV_TYPE_FAULT, }; =20 enum iio_event_direction { @@ -128,6 +129,7 @@ enum iio_event_direction { IIO_EV_DIR_NONE, IIO_EV_DIR_SINGLETAP, IIO_EV_DIR_DOUBLETAP, + IIO_EV_DIR_FAULT_OPENWIRE, }; =20 #endif /* _UAPI_IIO_TYPES_H_ */ diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index cccf62ea2b8f..eab7b082f19d 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -75,6 +75,7 @@ static const char * const iio_ev_type_text[] =3D { [IIO_EV_TYPE_CHANGE] =3D "change", [IIO_EV_TYPE_MAG_REFERENCED] =3D "mag_referenced", [IIO_EV_TYPE_GESTURE] =3D "gesture", + [IIO_EV_TYPE_FAULT] =3D "fault", }; =20 static const char * const iio_ev_dir_text[] =3D { @@ -83,6 +84,7 @@ static const char * const iio_ev_dir_text[] =3D { [IIO_EV_DIR_FALLING] =3D "falling", [IIO_EV_DIR_SINGLETAP] =3D "singletap", [IIO_EV_DIR_DOUBLETAP] =3D "doubletap", + [IIO_EV_DIR_FAULT_OPENWIRE] =3D "openwire", }; =20 static const char * const iio_modifier_names[] =3D { @@ -249,6 +251,7 @@ static bool event_is_known(struct iio_event_data *event) case IIO_EV_TYPE_MAG_ADAPTIVE: case IIO_EV_TYPE_CHANGE: case IIO_EV_TYPE_GESTURE: + case IIO_EV_TYPE_FAULT: break; default: return false; @@ -260,6 +263,7 @@ static bool event_is_known(struct iio_event_data *event) case IIO_EV_DIR_FALLING: case IIO_EV_DIR_SINGLETAP: case IIO_EV_DIR_DOUBLETAP: + case IIO_EV_DIR_FAULT_OPENWIRE: case IIO_EV_DIR_NONE: break; default: --=20 2.47.1 From nobody Sun Dec 14 08:08:30 2025 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C7905672 for ; Mon, 27 Jan 2025 14:04:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737986696; cv=none; b=NRKGk9zFEzZNDO8GnQ/P0GFSDWuxTomnb6qTW2xBXqCKMoFw8laxzT6nVDmZvUTsG5jzZ0qH0+ZFcql2RlDTciQ7XiDmLkPhb3TARv6+HjLHis8Nir9Mkot8tSlzsLreXNb/wttW6JIUA4WZA4U1BjSzjawgul9t2cnt5QrkTLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737986696; c=relaxed/simple; bh=tv8ufJNX2FveIIM0Ullgh4WcbgMO2rH4hcs0LieA+jQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eoCJQcnzGWdM5mB3l7EukTeAp/uyOIkkbhcWkGojG4opI0nJdv2QKkQbxkgcybxcBeb2QLR2OywNJpHOIPSdqlJDQBygiNDSFQKJNngcQDOoBEQtlPYGECX3UbcHNXdBTdpO2X0xsphKXSsXezWOcyhendDsGc6kPJOHBnPO5qk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=iv7wyLvG; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="iv7wyLvG" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-436345cc17bso31682965e9.0 for ; Mon, 27 Jan 2025 06:04:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1737986693; x=1738591493; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=qY0uFd4cFOzopA1OWUs9gFHikn9daeVfoWLrhNqrq8o=; b=iv7wyLvGyEzX4P8aXy9usbIXueAUhsXbtiNEnWVhIL83/FuGYucz0hnatAjcKJhvue 3d5aGnqz+6OSOYbiM42fhnJ8Y/hUov8PnBwcCTFujjnq2Y49uxtTJunhHxZ62fPgnzPz 6A9yn3v5WT6j5OI9OiJXySlYo2BA3bcX/o3IQOiAWFUrnbzZxLtcPR+gwt5FX3xZilnI IG+nAS0onO4h56e1HIl0z1XrYv+HiufPSPn2Dzg4vaIHqSQtqrsPtRgNSZCEwsUPTB2T gZSF6y6ZvPBYnCbQj9JeOQbFs3aTQdHLtYpAxi8VE6rPKn49CR+a0sA482ic/MO55NOu b9NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737986693; x=1738591493; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qY0uFd4cFOzopA1OWUs9gFHikn9daeVfoWLrhNqrq8o=; b=HgVmvw8UyWdYqg5wp3wlKppTaNb84gO39pzcqHdGEpktqrnyO1agmEfNn/JfNJh4IL z9rj1ijQiGIpSwa5LF7i6Po3QaSgnpENPsPtX8ouiJgk/aElkpvnjquxO88vpNDq6hf9 LrProK/b54xQyNv+Sy9PEzmL+YDsuRxwINTK7ApZ+bGu0ypmKwM5nM6QMf59uuvZqG1R DKBJF8iuw6trX744XzhWWb4sx2zMDdYAFBa5ce4GYY52BrjajC5Qn3NPF7d8Eeso94MD IML4080Oo0lFAQpOqxHnVZqnFBbw+zz8wEI++0hjcub2gemjhtpw2Pjkr6imptB/5mpM K1EQ== X-Forwarded-Encrypted: i=1; AJvYcCWvNg1bmfLJ+tEewYp68giLFe8BFGWiwGFMa+oJtBDyjpm7WU974VjiIuqH63uY5WTZSXNbIBqRXVKc20Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yzi3LdRdiLI8mwTOfakjSY05upsvpvqEz14GJwvCeODlj/GwR+2 JsM7GdnCuDEaHcz3Je7JIYy8zTqKW8tkALZPIf/3yvVv44EhGLC5Xq6eJiemVe8= X-Gm-Gg: ASbGnctd8pmx7UZ8NPXF52Lt/lpFLmH00fNEkgea3/BtCmrpaSEtpUjaDvXLHGtnbTP qmJByBBCu2cgNqt0UOfpa0Na77Q2YebCdlLnOgL01YdZJ+I372fLXNahQV8ZS88FXfPwXwIiB53 GGpBhQU1DXTt+BaRcdFtzxs48AYTqA2FMfqWZpa25l567y5rKySu6LiP4kj2NkN2o9UTKIesCAf 3gsOjwct2iF5KO9XFUpVCzJR6doQQouJH6j6CKDbPCxG7pjBGKHl4GNucjylI76iCG7D681yO4b 6wBOARTSU1Y53+jmk0yJnsLEgxlNpjtumhIAX/gg0uRpZFaQOrg= X-Google-Smtp-Source: AGHT+IGLm1hJ1mW9caGceHze6AjMdQ/MxMJ2NJ1GV9e265lvP/Q/g7qOxf8xZbW2WZh7iYDigEbsHg== X-Received: by 2002:a05:6000:1b82:b0:385:e3b8:f331 with SMTP id ffacd0b85a97d-38bf5663bd7mr29768565f8f.14.1737986692513; Mon, 27 Jan 2025 06:04:52 -0800 (PST) Received: from [127.0.0.1] (alille-653-1-300-114.w90-1.abo.wanadoo.fr. [90.1.180.114]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a313383sm11480639f8f.36.2025.01.27.06.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 06:04:51 -0800 (PST) From: Guillaume Ranquet Date: Mon, 27 Jan 2025 14:59:33 +0100 Subject: [PATCH v5 2/2] iio: adc: ad7173: add openwire detection support for single conversions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250127-ad4111_openwire-v5-2-ef2db05c384f@baylibre.com> References: <20250127-ad4111_openwire-v5-0-ef2db05c384f@baylibre.com> In-Reply-To: <20250127-ad4111_openwire-v5-0-ef2db05c384f@baylibre.com> To: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Guillaume Ranquet , Nuno Sa X-Mailer: b4 0.15-dev Some chips of the ad7173 family supports open wire detection. Generate a level fault event whenever an external source is disconnected from the system input on single conversions. Reviewed-by: Nuno Sa Signed-off-by: Guillaume Ranquet Reviewed-by: David Lechner --- drivers/iio/adc/ad7173.c | 179 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 179 insertions(+) diff --git a/drivers/iio/adc/ad7173.c b/drivers/iio/adc/ad7173.c index 8b438c689594..1855929b23d0 100644 --- a/drivers/iio/adc/ad7173.c +++ b/drivers/iio/adc/ad7173.c @@ -35,6 +35,7 @@ #include =20 #include +#include #include #include #include @@ -102,6 +103,7 @@ =20 #define AD7173_GPIO_PDSW BIT(14) #define AD7173_GPIO_OP_EN2_3 BIT(13) +#define AD4111_GPIO_GP_OW_EN BIT(12) #define AD7173_GPIO_MUX_IO BIT(12) #define AD7173_GPIO_SYNC_EN BIT(11) #define AD7173_GPIO_ERR_EN BIT(10) @@ -149,6 +151,7 @@ =20 #define AD7173_FILTER_ODR0_MASK GENMASK(5, 0) #define AD7173_MAX_CONFIGS 8 +#define AD4111_OW_DET_THRSH_MV 300 =20 #define AD7173_MODE_CAL_INT_ZERO 0x4 /* Internal Zero-Scale Calibration */ #define AD7173_MODE_CAL_INT_FULL 0x5 /* Internal Full-Scale Calibration */ @@ -182,11 +185,15 @@ struct ad7173_device_info { bool has_int_ref; bool has_ref2; bool has_internal_fs_calibration; + bool has_openwire_det; bool higher_gpio_bits; u8 num_gpios; }; =20 struct ad7173_channel_config { + /* Openwire detection threshold */ + unsigned int openwire_thrsh_raw; + int openwire_comp_chan; u8 cfg_slot; bool live; =20 @@ -203,6 +210,7 @@ struct ad7173_channel { unsigned int ain; struct ad7173_channel_config cfg; u8 syscalib_mode; + bool openwire_det_en; }; =20 struct ad7173_state { @@ -394,6 +402,76 @@ static int ad7173_calibrate_all(struct ad7173_state *s= t, struct iio_dev *indio_d return 0; } =20 +/* + * Associative array of channel pairs for open wire detection + * The array is indexed by ain and gives the associated channel pair + * to perform the open wire detection with + * the channel pair [0] is for non differential and pair [1] + * is for differential inputs + */ +static int openwire_ain_to_channel_pair[][2][2] =3D { +/* AIN Single Differential */ + [0] =3D { {0, 15}, {1, 2} }, + [1] =3D { {1, 2}, {2, 1} }, + [2] =3D { {3, 4}, {5, 6} }, + [3] =3D { {5, 6}, {6, 5} }, + [4] =3D { {7, 8}, {9, 10} }, + [5] =3D { {9, 10}, {10, 9} }, + [6] =3D { {11, 12}, {13, 14} }, + [7] =3D { {13, 14}, {14, 13} }, +}; + +/* + * Openwire detection on ad4111 works by running the same input measurement + * on two different channels and compare if the difference between the two + * measurements exceeds a certain value (typical 300mV) + */ +static int ad4111_openwire_event(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan) +{ + struct ad7173_state *st =3D iio_priv(indio_dev); + struct ad7173_channel *adchan =3D &st->channels[chan->address]; + struct ad7173_channel_config *cfg =3D &adchan->cfg; + int ret, val1, val2; + + ret =3D regmap_set_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, + AD4111_GPIO_GP_OW_EN); + if (ret) + return ret; + + adchan->cfg.openwire_comp_chan =3D + openwire_ain_to_channel_pair[chan->channel][chan->differential][0]; + + ret =3D ad_sigma_delta_single_conversion(indio_dev, chan, &val1); + if (ret < 0) { + dev_err(&indio_dev->dev, + "Error running ad_sigma_delta single conversion: %d", ret); + goto out; + } + + adchan->cfg.openwire_comp_chan =3D + openwire_ain_to_channel_pair[chan->channel][chan->differential][1]; + + ret =3D ad_sigma_delta_single_conversion(indio_dev, chan, &val2); + if (ret < 0) { + dev_err(&indio_dev->dev, + "Error running ad_sigma_delta single conversion: %d", ret); + goto out; + } + + if (abs(val1 - val2) > cfg->openwire_thrsh_raw) + iio_push_event(indio_dev, + IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, chan->address, + IIO_EV_TYPE_FAULT, IIO_EV_DIR_FAULT_OPENWIRE), + iio_get_time_ns(indio_dev)); + +out: + adchan->cfg.openwire_comp_chan =3D -1; + regmap_clear_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, + AD4111_GPIO_GP_OW_EN); + return ret; +} + static int ad7173_mask_xlate(struct gpio_regmap *gpio, unsigned int base, unsigned int offset, unsigned int *reg, unsigned int *mask) @@ -591,6 +669,9 @@ static int ad7173_set_channel(struct ad_sigma_delta *sd= , unsigned int channel) FIELD_PREP(AD7173_CH_SETUP_SEL_MASK, st->channels[channel].cfg.cfg_= slot) | st->channels[channel].ain; =20 + if (st->channels[channel].cfg.openwire_comp_chan >=3D 0) + channel =3D st->channels[channel].cfg.openwire_comp_chan; + return ad_sd_write_reg(&st->sd, AD7173_REG_CH(channel), 2, val); } =20 @@ -639,6 +720,11 @@ static int ad7173_disable_all(struct ad_sigma_delta *s= d) =20 static int ad7173_disable_one(struct ad_sigma_delta *sd, unsigned int chan) { + struct ad7173_state *st =3D ad_sigma_delta_to_ad7173(sd); + + if (st->channels[chan].cfg.openwire_comp_chan >=3D 0) + chan =3D st->channels[chan].cfg.openwire_comp_chan; + return ad_sd_write_reg(sd, AD7173_REG_CH(chan), 2, 0); } =20 @@ -690,6 +776,7 @@ static const struct ad7173_device_info ad4111_device_in= fo =3D { .has_current_inputs =3D true, .has_int_ref =3D true, .has_internal_fs_calibration =3D true, + .has_openwire_det =3D true, .clock =3D 2 * HZ_PER_MHZ, .sinc5_data_rates =3D ad7173_sinc5_data_rates, .num_sinc5_data_rates =3D ARRAY_SIZE(ad7173_sinc5_data_rates), @@ -1000,6 +1087,12 @@ static int ad7173_read_raw(struct iio_dev *indio_dev, if (ret < 0) return ret; =20 + if (ch->openwire_det_en) { + ret =3D ad4111_openwire_event(indio_dev, chan); + if (ret < 0) + return ret; + } + return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: =20 @@ -1144,12 +1237,57 @@ static int ad7173_debug_reg_access(struct iio_dev *= indio_dev, unsigned int reg, return ad_sd_write_reg(&st->sd, reg, reg_size, writeval); } =20 +static int ad7173_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 ad7173_state *st =3D iio_priv(indio_dev); + struct ad7173_channel *adchan =3D &st->channels[chan->address]; + + switch (type) { + case IIO_EV_TYPE_FAULT: + adchan->openwire_det_en =3D state; + return 0; + default: + return -EINVAL; + } +} + +static int ad7173_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 ad7173_state *st =3D iio_priv(indio_dev); + struct ad7173_channel *adchan =3D &st->channels[chan->address]; + + switch (type) { + case IIO_EV_TYPE_FAULT: + return adchan->openwire_det_en; + default: + return -EINVAL; + } +} + +static const struct iio_event_spec ad4111_events[] =3D { + { + .type =3D IIO_EV_TYPE_FAULT, + .dir =3D IIO_EV_DIR_FAULT_OPENWIRE, + .mask_separate =3D BIT(IIO_EV_INFO_VALUE), + .mask_shared_by_all =3D BIT(IIO_EV_INFO_ENABLE), + }, +}; + static const struct iio_info ad7173_info =3D { .read_raw =3D &ad7173_read_raw, .write_raw =3D &ad7173_write_raw, .debugfs_reg_access =3D &ad7173_debug_reg_access, .validate_trigger =3D ad_sd_validate_trigger, .update_scan_mode =3D ad7173_update_scan_mode, + .write_event_config =3D ad7173_write_event_config, + .read_event_config =3D ad7173_read_event_config, }; =20 static const struct iio_scan_type ad4113_scan_type =3D { @@ -1353,6 +1491,37 @@ static int ad7173_validate_reference(struct ad7173_s= tate *st, int ref_sel) return 0; } =20 +static int ad7173_validate_openwire_ain_inputs(struct ad7173_state *st, + bool differential, + unsigned int ain0, + unsigned int ain1) +{ + /* + * If the channel is configured as differential, + * the ad4111 requires specific ains to be used together + */ + if (differential) + return (ain0 % 2) ? (ain0 - 1) =3D=3D ain1 : (ain0 + 1) =3D=3D ain1; + + return ain1 =3D=3D AD4111_VINCOM_INPUT; +} + +static unsigned int ad7173_calc_openwire_thrsh_raw(struct ad7173_state *st, + struct iio_chan_spec *chan, + struct ad7173_channel *chan_st_priv, + unsigned int thrsh_mv) { + unsigned int thrsh_raw; + + thrsh_raw =3D + BIT(chan->scan_type.realbits - !!(chan_st_priv->cfg.bipolar)) + * thrsh_mv + / ad7173_get_ref_voltage_milli(st, chan_st_priv->cfg.ref_sel); + if (chan->channel < st->info->num_voltage_in_div) + thrsh_raw /=3D AD4111_DIVIDER_RATIO; + + return thrsh_raw; +} + static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev) { struct ad7173_channel *chans_st_arr, *chan_st_priv; @@ -1400,6 +1569,7 @@ static int ad7173_fw_parse_channel_config(struct iio_= dev *indio_dev) chan_st_priv->cfg.bipolar =3D false; chan_st_priv->cfg.input_buf =3D st->info->has_input_buf; chan_st_priv->cfg.ref_sel =3D AD7173_SETUP_REF_SEL_INT_REF; + chan_st_priv->cfg.openwire_comp_chan =3D -1; st->adc_mode |=3D AD7173_ADC_MODE_REF_EN; if (st->info->data_reg_only_16bit) chan_arr[chan_index].scan_type =3D ad4113_scan_type; @@ -1466,6 +1636,7 @@ static int ad7173_fw_parse_channel_config(struct iio_= dev *indio_dev) chan->channel =3D ain[0]; chan_st_priv->cfg.input_buf =3D st->info->has_input_buf; chan_st_priv->cfg.odr =3D 0; + chan_st_priv->cfg.openwire_comp_chan =3D -1; =20 chan_st_priv->cfg.bipolar =3D fwnode_property_read_bool(child, "bipolar"= ); if (chan_st_priv->cfg.bipolar) @@ -1480,6 +1651,14 @@ static int ad7173_fw_parse_channel_config(struct iio= _dev *indio_dev) chan_st_priv->cfg.input_buf =3D st->info->has_input_buf; chan->channel2 =3D ain[1]; chan_st_priv->ain =3D AD7173_CH_ADDRESS(ain[0], ain[1]); + if (st->info->has_openwire_det && + ad7173_validate_openwire_ain_inputs(st, chan->differential, ain[0],= ain[1])) { + chan->event_spec =3D ad4111_events; + chan->num_event_specs =3D ARRAY_SIZE(ad4111_events); + chan_st_priv->cfg.openwire_thrsh_raw =3D + ad7173_calc_openwire_thrsh_raw(st, chan, chan_st_priv, + AD4111_OW_DET_THRSH_MV); + } } =20 if (st->info->data_reg_only_16bit) --=20 2.47.1