From nobody Mon Sep 28 18:34:05 2026 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E456837882E; Tue, 18 Aug 2026 14:30:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.135.77 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787063435; cv=none; b=LD7X1WPhzp/Ex4bbW8Qmknqyqq71NxbNKa5MnqndpxPLXnKMmvmn3Hi9eUBENknhdOX1dyE/saihApnJoTrrdTI5CWu1QBUqMLlxm1+Mf3tqPKu/oh9bUNIFOQM2mpan0PmJynL/GGFpvDI6dccig5zMhxb5eLCUbK20838B8Zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787063435; c=relaxed/simple; bh=OEDTheWwHAMV5h6e8q1Io2U8jVFQMucC02eas7jS+bA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=N2u16eR5qmYFw3ToxwC38YmXr/1wzNxWyQD+ATm4bvnCjTpH1rM30qzk1NI0gJqJ8LM1k5Cbf6A2krhNtEDUQEBmavnnyJC2u/ZYMCebOeLP8vsKE6J9RTTCtqTSS4dlOqMm5f5YYsoHO48j2oizqdwYaF+4K/JNrCl7M+isVxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=analog.com; spf=pass smtp.mailfrom=analog.com; dkim=pass (2048-bit key) header.d=analog.com header.i=@analog.com header.b=zNGY89pC; arc=none smtp.client-ip=148.163.135.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=analog.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=analog.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=analog.com header.i=@analog.com header.b="zNGY89pC" Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67IDuGrU3121321; Tue, 18 Aug 2026 10:29:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=DKIM; bh=XvhJk JO5kA2wKTCrjbJC3vVu3eoMVx9zG/wFUBePGAo=; b=zNGY89pC4NWCCBEbke+fO gXGP9skXmtm3zwWnf9ZfSMv5CSTiaMlFPmfzbWJZ4PbGbzqTe2JHjRGf/dv34jWA MTTA9C6WdfTKlXw97im6lr6IVs87tMTHmzEZ8hXtvjHdHeGBWYOFwnWXxjfpCalX kEbg3WGhZB48bn2/ogOzregQewGd8XN+qVP7m8Ku0dOW/PqehbXpW1dskwUESbbO 7QdgOV6JQKvfKCogtyBEnDrOvJX5bbO68e8mnwWN1fKWxDeiKH9MpztPxncicIqB rIMYNO5/oz7PVP1tIAkU8uQ5+uWJCz3PArXMl3meVbTMHidEJ0PVl7xepnJYixA9 A== Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 4g4nr21aj5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Aug 2026 10:29:55 -0400 (EDT) Received: from ASHBMBX8.ad.analog.com (ASHBMBX8.ad.analog.com [10.64.17.5]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 67IETsYA035931 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 18 Aug 2026 10:29:54 -0400 Received: from ASHBCASHYB5.ad.analog.com (10.64.17.133) by ASHBMBX8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1748.37; Tue, 18 Aug 2026 10:29:54 -0400 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBCASHYB5.ad.analog.com (10.64.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1748.37; Tue, 18 Aug 2026 10:29:54 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.1748.37 via Frontend Transport; Tue, 18 Aug 2026 10:29:54 -0400 Received: from HYB-JRXo5UEs61B.ad.analog.com ([10.66.6.190]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 67IETZOv015349; Tue, 18 Aug 2026 10:29:47 -0400 From: Stefan Popa To: CC: , , , , , , , , , , , , , , , , Stefan Popa Subject: [PATCH v7 1/2] dt-bindings: iio: adc: add adi,max40080 Date: Tue, 18 Aug 2026 17:29:27 +0300 Message-ID: <20260818142928.8244-2-stefan.popa@analog.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260818142928.8244-1-stefan.popa@analog.com> References: <20260818142928.8244-1-stefan.popa@analog.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODE4MDEwNyBTYWx0ZWRfX0tnmJ60M9y1h PaY3OInEsQC8/VV9KO3OG0xDzflT0S6VMV3P/bnzElTIs2pUajiibSTD0Mq3uNol2Wdhljlw7bC m0iNnNbMbBd9RfahWVw7d4XoCu6AB10HLxo1p8YQ+oVo90OsxSc6C8bit+7BsOK0aQW4d3vzAH9 7AFyIda1sG5SpRsN9L/HftJGtf4C9pS01jOYeruMcgCZg9NAQPAQ+kV25C1/5b478n0HIRm9aw4 UKRmO+O0fu345hPQNeAtr89fq4t9yy/s/7qBOnyTS3rz2k6I1C1eUUcBnVxDFOkBBGhwvO5DG+C 5eRqLr40YkwvQ6yRqnPvxqvkm+k8vZsYrjlUKLQhtF/n6YdxZfEEiEPGPbnokfylunVJTE2k24T Gr6F+PFcgFc7uJ0OneKcDZZGOAhD/58Rvmf61PXA9naKTHjr3xKJt+CPzvUFTvSOlkaWGImN0BE N+j4vJXT5DBr3a17cPA== X-Proofpoint-GUID: XDTi_CGp_S5907oyfL_XqybH5H6-3Z57 X-Authority-Analysis: v=2.4 cv=B7CJFutM c=1 sm=1 tr=0 ts=6a846c63 cx=c_pps a=3WNzaoukacrqR9RwcOSAdA==:117 a=3WNzaoukacrqR9RwcOSAdA==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=0sLvza09kfJOxVLZPwjg:22 a=Z0pTeXoby7EwIRygza74:22 a=gEfo2CItAAAA:8 a=gAnH3GRIAAAA:8 a=VwQbUJbxAAAA:8 a=P-IC7800AAAA:8 a=Svqhk2N__qMwEp2CcvoA:9 a=sptkURWiP4Gy88Gu7hUp:22 a=d3PnA9EDa4IxuAV0gXij:22 X-Proofpoint-Spam-Info: AW1haW4tMjYwODE4MDEwNyBTYWx0ZWRfXwhaClbzwc6k7 UcV4dskGsK5Eo7+vdP6vkAYhSTc3HAQ6J9CHTl0tesGZ5CaVS13OVzty37/gWpVOpq4xHI2vRNR RP+pSpNLtP5oMgpUufwAXMiJiSc2esdYebYHL8eYZh19aic2E3ts X-Proofpoint-ORIG-GUID: XDTi_CGp_S5907oyfL_XqybH5H6-3Z57 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-18_02,2026-08-18_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 phishscore=0 adultscore=0 clxscore=1011 priorityscore=1501 impostorscore=0 suspectscore=0 malwarescore=0 bulkscore=0 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608180107 Content-Type: text/plain; charset="utf-8" Add device tree bindings for the Analog Devices MAX40080 bidirectional current-sense amplifier with a 12-bit ADC and an I2C/SMBus interface. Signed-off-by: Stefan Popa Reviewed-by: Krzysztof Kozlowski --- .../bindings/iio/adc/adi,max40080.yaml | 65 +++++++++++++++++++ MAINTAINERS | 8 +++ 2 files changed, 73 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,max40080.= yaml diff --git a/Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml b/= Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml new file mode 100644 index 0000000000000..b0ec9159a2939 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/adi,max40080.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Analog Devices MAX40080 bidirectional current-sense amplifier + +maintainers: + - Ciprian Hegbeli + - Stefan Popa + +description: | + The MAX40080 is a high-precision, bidirectional current-sense amplifier = with + an integrated 12-bit ADC and an I2C/SMBus interface. It measures the vol= tage + across an external shunt resistor and the input bus voltage, and stores = the + results in an internal FIFO. + + Datasheet: + https://www.analog.com/max40080 + +properties: + compatible: + const: adi,max40080 + + reg: + maxItems: 1 + + "#io-channel-cells": + const: 1 + + vdd-supply: + description: Power supply for the device. + + interrupts: + maxItems: 1 + description: ALERT_ output for overcurrent/undervoltage threshold even= ts. + + shunt-resistor-micro-ohms: + description: + Value of the current-sense shunt resistor connected between the RS+ = and + RS- inputs. Used to scale the reported current. + +required: + - compatible + - reg + - shunt-resistor-micro-ohms + - vdd-supply + +additionalProperties: false + +examples: + - | + i2c { + #address-cells =3D <1>; + #size-cells =3D <0>; + + adc@20 { + compatible =3D "adi,max40080"; + reg =3D <0x20>; + #io-channel-cells =3D <1>; + vdd-supply =3D <&vdd>; + shunt-resistor-micro-ohms =3D <100000>; + }; + }; diff --git a/MAINTAINERS b/MAINTAINERS index e087673237636..c1e5afa42de2e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15513,6 +15513,14 @@ L: linux-iio@vger.kernel.org S: Supported F: drivers/iio/temperature/max30208.c =20 +MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER +M: Ciprian Hegbeli +M: Stefan Popa +L: linux-iio@vger.kernel.org +S: Supported +W: https://ez.analog.com/linux-software-drivers +F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml + MAXIM MAX7360 KEYPAD LED MFD DRIVER M: Mathieu Dubois-Briand S: Maintained --=20 2.53.0 From nobody Mon Sep 28 18:34:05 2026 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93C9E393DDC; Tue, 18 Aug 2026 14:30:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.135.77 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787063439; cv=none; b=rIjc5tDQdRaboUOqbNd18tuCIjFpoj1QqgPpbzys8a3iV2e2wfJb1djLNjS4eas8cLQLLY6UCOi9jmrXVS2fzYN/aZYSQW3FHdxk/4XCR7/6r+VlGjqER6JB/M3h5xNMUyKp8TBU6Iq4TGNs4/8dBIAOR5CxRACvBV58OCE3x5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787063439; c=relaxed/simple; bh=NL6/fThds/E5MA5TFHOpyOd4woRiAJqeZiDs1iulRRQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Oy+pRWVN0eA3ZVDUqpTEzBEMmdv2LM88FX2iGX52jeAdpGUbMC3ArUzivs8b7iLUJofzij6Ko78jWRaFrpsouUXeHgBdtmVadrz2V0eeT8RyZ73pTDs8oIvrTfxULw4ZjL4a1PxaWdre5qCEDEANpO4KjEXgLJi4jK8Uc4KpUEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=analog.com; spf=pass smtp.mailfrom=analog.com; dkim=pass (2048-bit key) header.d=analog.com header.i=@analog.com header.b=zsSthmWX; arc=none smtp.client-ip=148.163.135.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=analog.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=analog.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=analog.com header.i=@analog.com header.b="zsSthmWX" Received: from pps.filterd (m0516787.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67IDuT0A3404590; Tue, 18 Aug 2026 10:29:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=DKIM; bh=VY0eP 0kinQCflLMVK9+DJbOMd8GNfyBEs2LjhMJ1Wh4=; b=zsSthmWXO5RmKkWaEvF+m WLie3wTxOPGVKBpOEf852COaOjn7xCYZRTTUgRoTg2j3rjcPIxLimKVRzTgCdC55 Lvxwsfdt6heZLk2VJf7D9PSsTZ1ekQmPpY80q1usnoIfWoZZAxOFeBqlNmfRB8gO hxEAioSqprPE7oKWoo3e6J+gV+VzwblHHJMXLHXXoyVbv3cJYb52bA3G+jnkusp7 NNQgv0LFKtv2V/oj8lt2xH+hnI2cBlqBReww6H1zEBD42I3HtGJcnMJD5iS6Wi4r GflK2gc8OTWoPkzfQOv9Gw8QQWyfKfoJyvm4/h7mlA0NzLsaXHqEC9SrHOWbsHuG A== Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 4g4evkkbww-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Aug 2026 10:29:59 -0400 (EDT) Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 67IETwaM035962 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 18 Aug 2026 10:29:58 -0400 Received: from ASHBCASHYB5.ad.analog.com (10.64.17.133) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1748.37; Tue, 18 Aug 2026 10:29:57 -0400 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBCASHYB5.ad.analog.com (10.64.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1748.37; Tue, 18 Aug 2026 10:29:57 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.1748.37 via Frontend Transport; Tue, 18 Aug 2026 10:29:57 -0400 Received: from HYB-JRXo5UEs61B.ad.analog.com ([10.66.6.190]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 67IETZOw015349; Tue, 18 Aug 2026 10:29:50 -0400 From: Stefan Popa To: CC: , , , , , , , , , , , , , , , , Stefan Popa Subject: [PATCH v7 2/2] iio: adc: add MAX40080 current-sense amplifier driver Date: Tue, 18 Aug 2026 17:29:28 +0300 Message-ID: <20260818142928.8244-3-stefan.popa@analog.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260818142928.8244-1-stefan.popa@analog.com> References: <20260818142928.8244-1-stefan.popa@analog.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Spam-Info: AW1haW4tMjYwODE4MDEwNyBTYWx0ZWRfX7SkE9qu5QxMS rL67ZwGG9KWlayoqs5hWmJn6C5yKZqlvLXnkImz16TeS6t76mRVtBpMmn/Qr4Q44jhm3O5k8/Gj yf9y96fQJ6NFBWpBiP6p1WjkcAFeRfX6Aqm7fZ1dHw3q+QeEV7Mb X-Proofpoint-GUID: g8s_g_pPakKPn8tomdgf4fPLMQtQrfjy X-Proofpoint-ORIG-GUID: g8s_g_pPakKPn8tomdgf4fPLMQtQrfjy X-Authority-Analysis: v=2.4 cv=AOdqhGMl c=1 sm=1 tr=0 ts=6a846c67 cx=c_pps a=3WNzaoukacrqR9RwcOSAdA==:117 a=3WNzaoukacrqR9RwcOSAdA==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=0sLvza09kfJOxVLZPwjg:22 a=OmVn7CZJonkx5R5zMQLL:22 a=gAnH3GRIAAAA:8 a=VwQbUJbxAAAA:8 a=P-IC7800AAAA:8 a=YIcQchwrqHIIhTqXiBUA:9 a=d3PnA9EDa4IxuAV0gXij:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODE4MDEwNyBTYWx0ZWRfX6SFtdeD3GNkE FlycavvCwbgFweLhowGwzKuzqo4u1ZWG2N5+NtKpFkVqQjeoBjaNN8WVxi8rIxsu6ITlsG7Rm12 HFxTZHeWQ3JFaBQsOqJRv9iHJOqmGSSTN8uZNOIYqbtkFH9SygJeDAjqQx8VwUnCH7bC9OfZkXL voL1kUKteFPHra2FhzOgfAqOK9lgFDU1cbEPRwst69q5xkepASuu5BBkoK/0+rqIYxunIOsrkze RwY68TRINuCm61ojC0s6y0v2lrLh/mtMnsyKvctBzBrY/z7+dTr3rXFf8DCc7GscC4Mwt+xgtMc L+NlRQPdiYzkUf7rinqlaIyEHVQmooa4n3utv4LOQPIsMK+8SM0FDqCyAjryQVMyZJACoHu/czT 66AC06YVuwdNWxYCyXMksFJTX13shZGe0POSMCiF306y01nWv7aJvNqaptGBG57y3c16j/me2lA XEM0k/wxtDqngKWcqSg== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-18_02,2026-08-18_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 suspectscore=0 impostorscore=0 bulkscore=0 priorityscore=1501 phishscore=0 spamscore=0 malwarescore=0 adultscore=0 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608180107 Content-Type: text/plain; charset="utf-8" The MAX40080 is a bidirectional current-sense amplifier with an integrated 12-bit ADC and an I2C/SMBus interface. It measures the voltage across an external shunt resistor and the input bus voltage, storing the results in an internal FIFO. Add a direct-mode IIO driver exposing the current and voltage channels with raw, scale and hardware-gain attributes, a configurable oversampling (digital averaging) ratio, and PEC-protected register access. The current scale is derived from the shunt resistor value described in the device tree. The driver operates in single-measurement mode: each raw read triggers an on-demand conversion via SMBus Quick Command and returns a matched current/voltage pair. This avoids the latency and complexity of the continuous FIFO mode while ensuring each read reflects the current state. The two selectable current-sense ranges are exposed through scale/scale_available. Continuous FIFO buffering, threshold events and the alert interrupt are intentionally left out of this initial submission and may be added later. Co-developed-by: Ciprian Hegbeli Signed-off-by: Ciprian Hegbeli Signed-off-by: Stefan Popa Reviewed-by: Siratul Islam --- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/max40080.c | 572 +++++++++++++++++++++++++++++++++++++ 4 files changed, 585 insertions(+) create mode 100644 drivers/iio/adc/max40080.c diff --git a/MAINTAINERS b/MAINTAINERS index c1e5afa42de2e..27f3be3ead889 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15520,6 +15520,7 @@ L: linux-iio@vger.kernel.org S: Supported W: https://ez.analog.com/linux-software-drivers F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml +F: drivers/iio/adc/max40080.c =20 MAXIM MAX7360 KEYPAD LED MFD DRIVER M: Mathieu Dubois-Briand diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 58da8255525e4..ebdbad81fbbd6 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -1907,3 +1907,14 @@ config XILINX_AMS xilinx-ams. =20 endmenu + +config MAX40080 + tristate "Analog Devices MAX40080 Current Sense Amplifier" + depends on I2C + help + Say yes here to build support for the Analog Devices MAX40080 + bidirectional current-sense amplifier with a 12-bit ADC and an I2C + interface. + + To compile this driver as a module, choose M here: the module will be + called max40080. diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index 7cc8f9a12f763..9245a337dd935 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -166,3 +166,4 @@ obj-$(CONFIG_VIPERBOARD_ADC) +=3D viperboard_adc.o obj-$(CONFIG_XILINX_AMS) +=3D xilinx-ams.o xilinx-xadc-y :=3D xilinx-xadc-core.o xilinx-xadc-events.o obj-$(CONFIG_XILINX_XADC) +=3D xilinx-xadc.o +obj-$(CONFIG_MAX40080) +=3D max40080.o diff --git a/drivers/iio/adc/max40080.c b/drivers/iio/adc/max40080.c new file mode 100644 index 0000000000000..4b397bcd145b2 --- /dev/null +++ b/drivers/iio/adc/max40080.c @@ -0,0 +1,572 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * MAX40080 Digital Current-Sense Amplifier driver + * + * Copyright 2026 Analog Devices, Inc. + * + * Datasheet: https://www.analog.com/media/en/technical-documentation/data= -sheets/MAX40080.pdf + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define MAX40080_REG_CFG 0x00 +#define MAX40080_CFG_MODE_MSK GENMASK(2, 0) +#define MAX40080_CFG_PEC_EN_MSK BIT(5) +#define MAX40080_CFG_RANGE_MSK BIT(6) +#define MAX40080_CFG_FILTER_MSK GENMASK(14, 12) + +#define MAX40080_REG_FIFO_CFG 0x0A +#define MAX40080_FIFO_CFG_STORE_IV_MSK GENMASK(1, 0) + +#define MAX40080_REG_IV 0x10 +/* Current is a 13-bit two's-complement value (magnitude + sign bit). */ +#define MAX40080_IV_I_MSK GENMASK(12, 0) +#define MAX40080_IV_I_SIGN_BIT 12 +#define MAX40080_IV_V_MAG_MSK GENMASK(27, 16) +#define MAX40080_IV_VALID_MSK BIT(31) + +/* CFG.mode field values. */ +#define MAX40080_CFG_MODE_STDBY 0x00 +#define MAX40080_CFG_MODE_SINGLE 0x02 + +/* CFG.range field values. */ +#define MAX40080_CFG_RANGE_50mV 0 +#define MAX40080_CFG_RANGE_10mV 1 + +/* FIFO_CFG.store_iv field values. */ +#define MAX40080_FIFO_CFG_STORE_IV 0x02 + +#define MAX40080_ADC_RES_BITS 12 +#define MAX40080_INTER_VREF_mV 1250 +#define MAX40080_V_BUFF_GAIN 30 +#define MAX40080_CSA_50mV_GAIN 25 +#define MAX40080_CSA_10mV_GAIN 125 + +/* + * The RANGE field (CFG bit 6) selects one of two current-sense full-scale + * ranges (the MAX40080 supports exactly two: +/-50 mV and +/-10 mV). Inde= xed + * by the CFG.range field value. + */ +static const int max40080_csa_gain[] =3D { + [MAX40080_CFG_RANGE_50mV] =3D MAX40080_CSA_50mV_GAIN, + [MAX40080_CFG_RANGE_10mV] =3D MAX40080_CSA_10mV_GAIN, +}; + +struct max40080_state { + struct i2c_client *client; + /* Serializes read-modify-write access to the CFG register. */ + struct mutex lock; + u32 shunt_resistor_uOhm; + /* Cached configuration: the selected RANGE index and oversampling ratio.= */ + unsigned int range; + int oversampling_ratio; + /* + * Precomputed current scale (mA per code) for each RANGE setting, as + * {integer, nano} pairs for IIO_VAL_INT_PLUS_NANO. The range is + * selected by writing the corresponding scale. + */ + int current_scale[2][2]; +}; + +static const int max40080_oversampling_avail[] =3D { 1, 8, 16, 32, 64, 128= }; + +static int max40080_update_bits(struct max40080_state *st, u8 reg, + u16 mask, u16 val) +{ + int tmp; + + tmp =3D i2c_smbus_read_word_data(st->client, reg); + if (tmp < 0) + return tmp; + + tmp =3D (tmp & ~mask) | (val & mask); + + return i2c_smbus_write_word_data(st->client, reg, tmp); +} + +/* + * In single-measurement mode the device sits idle until it receives an SM= Bus + * Quick Command, then performs exactly one current and one voltage conver= sion + * and returns to idle. Triggering on demand this way (rather than running= the + * FIFO continuously in active mode) means each read returns a fresh, cohe= rent + * current/voltage pair instead of the oldest queued FIFO entry. + */ +static int max40080_trigger_measurement(struct max40080_state *st) +{ + return i2c_smbus_xfer(st->client->adapter, st->client->addr, + st->client->flags, I2C_SMBUS_WRITE, 0, + I2C_SMBUS_QUICK, NULL); +} + +/* + * A single measurement holds the matched current/voltage pair in one 32-b= it + * word (MAX40080_REG_IV). Reading all four bytes in one transaction retur= ns + * both from the same conversion; reading the separate current (0x0C) and + * voltage (0x0E) registers would decorrelate the two channels. + * + * Unlike the word accesses used elsewhere, this is a plain I2C block read= : the + * SMBus layer does not append or verify a PEC byte for it even when PEC is + * otherwise enabled for the device, so this transfer is not PEC protected. + */ +static int max40080_read_iv_once(struct max40080_state *st, u32 *iv) +{ + u8 buf[4]; + int ret; + + ret =3D i2c_smbus_read_i2c_block_data(st->client, MAX40080_REG_IV, + sizeof(buf), buf); + if (ret < 0) + return ret; + + *iv =3D get_unaligned_le32(buf); + + return 0; +} + +static int max40080_read_iv(struct max40080_state *st, u32 *iv) +{ + u32 tmp =3D 0; + int ret, io_ret; + + guard(mutex)(&st->lock); + + ret =3D max40080_trigger_measurement(st); + if (ret < 0) + return ret; + + /* + * Wait for the conversion to complete by polling the FIFO valid bit + * (or bail out on an I2C error). Polling the device's own status makes + * this independent of the actual conversion time, which varies with the + * oversampling ratio and the bus speed. The timeout is only a safety + * ceiling: the worst case is the maximum 128x averaging on both the + * current and voltage channels at the slowest 15 ksps base rate plus the + * inter-channel switching time, i.e. roughly 20 ms; 50 ms leaves ample + * margin. + */ + ret =3D read_poll_timeout(max40080_read_iv_once, io_ret, + io_ret || (tmp & MAX40080_IV_VALID_MSK), + 500, 50000, false, st, &tmp); + /* + * Propagate the last-read value even on timeout so the caller can + * inspect it for debugging. + */ + *iv =3D tmp; + if (ret) + return ret; + + return io_ret; +} + +static int max40080_get_current(struct max40080_state *st, int *val) +{ + u32 iv; + int ret; + + ret =3D max40080_read_iv(st, &iv); + if (ret) + return ret; + + *val =3D sign_extend32(FIELD_GET(MAX40080_IV_I_MSK, iv), + MAX40080_IV_I_SIGN_BIT); + + return 0; +} + +static int max40080_get_voltage(struct max40080_state *st, int *val) +{ + u32 iv; + int ret; + + ret =3D max40080_read_iv(st, &iv); + if (ret) + return ret; + + *val =3D FIELD_GET(MAX40080_IV_V_MAG_MSK, iv); + + return 0; +} + +static int max40080_set_range(struct max40080_state *st, unsigned int rang= e) +{ + int ret; + + ret =3D max40080_update_bits(st, MAX40080_REG_CFG, MAX40080_CFG_RANGE_MSK, + FIELD_PREP(MAX40080_CFG_RANGE_MSK, range)); + if (ret) + return ret; + + st->range =3D range; + + return 0; +} + +/* + * Precompute the current scale (mA per code) for each RANGE setting as + * {integer, nano} pairs. The shunt drop for a full-scale code is + * Vref[mV] / (BIT(ADC_RES_BITS) * gain) + * and current =3D Vshunt / Rshunt, so with Rshunt in micro-ohms the scale= in + * mA/code is + * Vref[mV] * NANO * MICRO / (BIT(ADC_RES_BITS) * gain * Rshunt[uOhm]) + * expressed as an integer part plus a nano fractional part. + */ +static void max40080_calc_current_scale(struct max40080_state *st) +{ + u64 numerator, denominator; + u32 rem; + + for (unsigned int i =3D 0; i < ARRAY_SIZE(max40080_csa_gain); i++) { + numerator =3D 1ULL * MAX40080_INTER_VREF_mV * NANO * MICRO; + denominator =3D 1ULL * BIT(MAX40080_ADC_RES_BITS) * max40080_csa_gain[i]= * + st->shunt_resistor_uOhm; + numerator =3D div64_u64(numerator, denominator); + st->current_scale[i][0] =3D div_u64_rem(numerator, NANO, &rem); + st->current_scale[i][1] =3D rem; + } +} + +/* + * max40080_oversampling_avail[] is ordered so that its index is the FILTER + * field value (index 0 =3D no averaging, index 1 =3D 8x, ...). Return tha= t index + * for an exact match, or -EINVAL for a value that is not on the list. + */ +static int max40080_oversampling_to_filter(int val) +{ + for (unsigned int i =3D 0; i < ARRAY_SIZE(max40080_oversampling_avail); i= ++) { + if (max40080_oversampling_avail[i] =3D=3D val) + return i; + } + + return -EINVAL; +} + +static int max40080_set_oversampling_ratio(struct max40080_state *st, int = val) +{ + int filter; + int ret; + + filter =3D max40080_oversampling_to_filter(val); + if (filter < 0) + return filter; + + ret =3D max40080_update_bits(st, MAX40080_REG_CFG, MAX40080_CFG_FILTER_MS= K, + FIELD_PREP(MAX40080_CFG_FILTER_MSK, filter)); + if (ret) + return ret; + + st->oversampling_ratio =3D val; + + return 0; +} + +static int max40080_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct max40080_state *st =3D iio_priv(indio_dev); + unsigned int range; + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + if (chan->type =3D=3D IIO_CURRENT) { + ret =3D max40080_get_current(st, val); + if (ret) + return ret; + } else if (chan->type =3D=3D IIO_VOLTAGE) { + ret =3D max40080_get_voltage(st, val); + if (ret) + return ret; + } + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + if (chan->type =3D=3D IIO_CURRENT) { + /* + * The selectable current-sense range is exposed through + * scale: each RANGE setting has its own precomputed + * mA-per-code value. Userspace picks the range by writing + * the matching scale. + * + * Take a local copy of range to ensure val and val2 come + * from the same setting even if a concurrent write changes + * st->range between the two accesses. + */ + range =3D st->range; + *val =3D st->current_scale[range][0]; + *val2 =3D st->current_scale[range][1]; + return IIO_VAL_INT_PLUS_NANO; + } + /* voltage[mV] =3D raw * Vref[mV] * buffer_gain / BIT(ADC_RES_BITS) */ + *val =3D MAX40080_INTER_VREF_mV * MAX40080_V_BUFF_GAIN; + *val2 =3D MAX40080_ADC_RES_BITS; + return IIO_VAL_FRACTIONAL_LOG2; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + *val =3D st->oversampling_ratio; + return IIO_VAL_INT; + default: + return -EINVAL; + } +} + +static int max40080_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct max40080_state *st =3D iio_priv(indio_dev); + + guard(mutex)(&st->lock); + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + /* Only the current channel has a selectable range/scale. */ + if (chan->type !=3D IIO_CURRENT) + return -EINVAL; + + for (unsigned int i =3D 0; i < ARRAY_SIZE(max40080_csa_gain); i++) { + if (val =3D=3D st->current_scale[i][0] && + val2 =3D=3D st->current_scale[i][1]) + return max40080_set_range(st, i); + } + + return -EINVAL; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + return max40080_set_oversampling_ratio(st, val); + default: + return -EINVAL; + } +} + +static int max40080_write_raw_get_fmt(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_SCALE: + return IIO_VAL_INT_PLUS_NANO; + default: + return IIO_VAL_INT; + } +} + +static int max40080_read_avail(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int **vals, int *type, int *length, + long info) +{ + struct max40080_state *st =3D iio_priv(indio_dev); + + switch (info) { + case IIO_CHAN_INFO_SCALE: + if (chan->type !=3D IIO_CURRENT) + return -EINVAL; + + *vals =3D (int *)st->current_scale; + *length =3D ARRAY_SIZE(max40080_csa_gain) * 2; + *type =3D IIO_VAL_INT_PLUS_NANO; + return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: + *vals =3D max40080_oversampling_avail; + *length =3D ARRAY_SIZE(max40080_oversampling_avail); + *type =3D IIO_VAL_INT; + return IIO_AVAIL_LIST; + default: + return -EINVAL; + } +} + +static int max40080_reg_access(struct iio_dev *indio_dev, unsigned int reg, + unsigned int write_val, unsigned int *read_val) +{ + struct max40080_state *st =3D iio_priv(indio_dev); + int val; + + if (read_val) { + val =3D i2c_smbus_read_word_data(st->client, reg); + if (val < 0) + return val; + + *read_val =3D val; + + return 0; + } + + return i2c_smbus_write_word_data(st->client, reg, write_val); +} + +static const struct iio_info max40080_info =3D { + .read_raw =3D max40080_read_raw, + .write_raw =3D max40080_write_raw, + .write_raw_get_fmt =3D max40080_write_raw_get_fmt, + .read_avail =3D max40080_read_avail, + .debugfs_reg_access =3D &max40080_reg_access, +}; + +static const struct iio_chan_spec max40080_channels[] =3D { + { + .type =3D IIO_CURRENT, + .indexed =3D 1, + .channel =3D 0, + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available =3D BIT(IIO_CHAN_INFO_SCALE), + .info_mask_shared_by_all =3D BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .info_mask_shared_by_all_available =3D + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + }, + { + .type =3D IIO_VOLTAGE, + .indexed =3D 1, + .channel =3D 0, + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE), + .info_mask_shared_by_all =3D BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .info_mask_shared_by_all_available =3D + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + }, +}; + +/* + * Configure the device from the cached state. The device powers up in sta= ndby + * with PEC enabled (CFG POR =3D 0x0060), so PEC is kept enabled throughou= t. + */ +static int max40080_init(struct max40080_state *st) +{ + u16 fifo_cfg, cfg; + int ret, filter; + + filter =3D max40080_oversampling_to_filter(st->oversampling_ratio); + if (filter < 0) + return filter; + + /* + * Put the device in standby before (re)configuring the FIFO: the FIFO + * configuration register can only be written while the device is not + * converting. + */ + cfg =3D FIELD_PREP(MAX40080_CFG_MODE_MSK, MAX40080_CFG_MODE_STDBY) | + FIELD_PREP(MAX40080_CFG_PEC_EN_MSK, 1); + + ret =3D i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg); + if (ret) + return ret; + + /* Store a matched current+voltage pair per conversion. */ + fifo_cfg =3D FIELD_PREP(MAX40080_FIFO_CFG_STORE_IV_MSK, MAX40080_FIFO_CFG= _STORE_IV); + + ret =3D i2c_smbus_write_word_data(st->client, MAX40080_REG_FIFO_CFG, + fifo_cfg); + if (ret) + return ret; + + /* + * Use single-measurement mode: the device stays idle and converts once + * per SMBus Quick Command (see max40080_trigger_measurement()), so each + * read returns a fresh sample rather than a queued FIFO entry. + */ + cfg =3D FIELD_PREP(MAX40080_CFG_MODE_MSK, MAX40080_CFG_MODE_SINGLE) | + FIELD_PREP(MAX40080_CFG_PEC_EN_MSK, 1) | + FIELD_PREP(MAX40080_CFG_RANGE_MSK, st->range) | + FIELD_PREP(MAX40080_CFG_FILTER_MSK, filter); + + return i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg); +} + +static int max40080_probe(struct i2c_client *client) +{ + const char *propname =3D "shunt-resistor-micro-ohms"; + struct device *dev =3D &client->dev; + struct iio_dev *indio_dev; + struct max40080_state *st; + int ret; + + /* + * The device powers up with PEC enabled (CFG POR =3D 0x0060) and rejects + * unprotected transactions, so PEC support is mandatory, along with word + * access, the I2C block read used for the current/voltage pair, and the + * Quick Command used to trigger a conversion. + */ + if (!i2c_check_functionality(client->adapter, + I2C_FUNC_SMBUS_WORD_DATA | + I2C_FUNC_SMBUS_I2C_BLOCK | + I2C_FUNC_SMBUS_QUICK | + I2C_FUNC_SMBUS_PEC)) + return -EOPNOTSUPP; + + client->flags |=3D I2C_CLIENT_PEC; + + indio_dev =3D devm_iio_device_alloc(dev, sizeof(*st)); + if (!indio_dev) + return -ENOMEM; + + st =3D iio_priv(indio_dev); + st->client =3D client; + + ret =3D devm_mutex_init(dev, &st->lock); + if (ret) + return ret; + + ret =3D device_property_read_u32(dev, propname, &st->shunt_resistor_uOhm); + if (ret) + return dev_err_probe(dev, ret, "can't read %s\n", propname); + if (!st->shunt_resistor_uOhm) + return dev_err_probe(dev, -EINVAL, "%s must be non-zero\n", + propname); + + max40080_calc_current_scale(st); + + /* Defaults: 50 mV range, no averaging. */ + st->range =3D MAX40080_CFG_RANGE_50mV; + st->oversampling_ratio =3D 1; + + indio_dev->name =3D "max40080"; + indio_dev->info =3D &max40080_info; + indio_dev->modes =3D INDIO_DIRECT_MODE; + indio_dev->channels =3D max40080_channels; + indio_dev->num_channels =3D ARRAY_SIZE(max40080_channels); + + ret =3D max40080_init(st); + if (ret) + return ret; + + return devm_iio_device_register(dev, indio_dev); +} + +static const struct i2c_device_id max40080_i2c_ids[] =3D { + { .name =3D "max40080" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, max40080_i2c_ids); + +static const struct of_device_id max40080_of_match[] =3D { + { .compatible =3D "adi,max40080" }, + { } +}; +MODULE_DEVICE_TABLE(of, max40080_of_match); + +static struct i2c_driver max40080_driver =3D { + .driver =3D { + .name =3D "max40080", + .of_match_table =3D max40080_of_match, + }, + .probe =3D max40080_probe, + .id_table =3D max40080_i2c_ids, +}; +module_i2c_driver(max40080_driver); + +MODULE_AUTHOR("Ciprian Hegbeli "); +MODULE_AUTHOR("Stefan Popa "); +MODULE_DESCRIPTION("Analog Devices MAX40080 current-sense amplifier driver= "); +MODULE_LICENSE("GPL"); --=20 2.53.0