From nobody Fri Apr 3 04:52:59 2026 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) (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 6F6BF36165D; Tue, 17 Feb 2026 14:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=68.232.154.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771337264; cv=none; b=UkNfcS0UTMyh4iETps0j3I56vFTyQDnQODkLsVizWV0npYB5DIVlcVVP8f2wRNA/ISrbpVTmxszlhjie2ntQHPrxkNXiMEOTB3ImwiKtttEty0VTDWibtBaqdQXHZzV54TTtWwtr8455Y1ZxzWl7zGvnuiNMpZTJLx9dTBb9OHY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771337264; c=relaxed/simple; bh=S9OUibKHbi69i0rxDsGt3stdu3AkJ8WpagkUyVkNc1s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=cJgUpLyypUe4n07s8VMF1zyf5IX046qmIvgSLhxZWkI5X1Xv9qBydu6MBcJdD2Hd7+tS186WasOAb9nNdXs9P9uaIsBargHsglLYVAaHWXZ9GX6eFr4J5RuQbu7LzfhIeukMLOros1RkbFVI/YldczYBcgtUcz5qEK3NSd+GhJk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com; spf=pass smtp.mailfrom=microchip.com; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b=LeMAF7AH; arc=none smtp.client-ip=68.232.154.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=microchip.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b="LeMAF7AH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1771337262; x=1802873262; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=S9OUibKHbi69i0rxDsGt3stdu3AkJ8WpagkUyVkNc1s=; b=LeMAF7AHFRh0qi7WVn4l0ALXnGqj2GqWkztHfI3q6xSmrpHYBrIVYtAl BadmpF5g8ntxHwtCoggNk0a0feME7QF8YdtsX8/spq3ueC99FakpOQdiU J81E/1JD3MxJ+vjKZxCj9Cvh0ZV5WG2sRqOCys6wWkw3w9A1j/GHW3ebe e+y+LEv+hvZ9aeEuX+vv4lae+/b7Knj/3nuA+sQdqhGh8VxcKGIYRjg1C UekuKmptRyUs19KvuShH5n3qk/BzHOr/K5FqxjpdhzqyYQ+9tFZSAYNe+ TE06/EkoZKEMpJHOqYIb/o4KnuVQ9J20sOEIRVqCEjYWmnnmWhfuGnEng Q==; X-CSE-ConnectionGUID: NsNq/mm8SHKghX7/+9wqEA== X-CSE-MsgGUID: c7/PFTOSRGe9Meo9MH2Stw== X-IronPort-AV: E=Sophos;i="6.21,296,1763449200"; d="scan'208";a="53912587" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa2.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 17 Feb 2026 07:07:34 -0700 Received: from chn-vm-ex01.mchp-main.com (10.10.85.143) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.58; Tue, 17 Feb 2026 07:07:24 -0700 Received: from [127.0.1.1] (10.10.85.11) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server id 15.1.2507.58 via Frontend Transport; Tue, 17 Feb 2026 07:07:22 -0700 From: Victor Duicu Date: Tue, 17 Feb 2026 16:06:13 +0200 Subject: [PATCH v10 1/2] dt-bindings: hwmon: add support for MCP998X 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: <20260217-add-mcp9982-hwmon-v10-1-5e0aaae6f289@microchip.com> References: <20260217-add-mcp9982-hwmon-v10-0-5e0aaae6f289@microchip.com> In-Reply-To: <20260217-add-mcp9982-hwmon-v10-0-5e0aaae6f289@microchip.com> To: Guenter Roeck , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet CC: , , , , , X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1771337236; l=7280; i=victor.duicu@microchip.com; s=20260213; h=from:subject:message-id; bh=S9OUibKHbi69i0rxDsGt3stdu3AkJ8WpagkUyVkNc1s=; b=pzkwLtbuG8xgwLuNu7KX4mnVRyE5tuCCfwP10t6N+yH8pJb5Xl3ZvVpx/DHXmDYqVXD1rdJp3 nRoBC8LKGM+BoNyGbEgnIDIUSy+RURg2fJntaYFIfe+ec/Oc8pwqFwc X-Developer-Key: i=victor.duicu@microchip.com; a=ed25519; pk=/o+aE26HN3Piv9T5t+efqb0aeJw9ErwMPeSC8lYXQsA= This is the devicetree schema for Microchip MCP998X/33 and MCP998XD/33D Multichannel Automotive Temperature Monitor Family. Signed-off-by: Victor Duicu --- .../bindings/hwmon/microchip,mcp9982.yaml | 223 +++++++++++++++++= ++++ MAINTAINERS | 6 + 2 files changed, 229 insertions(+) diff --git a/Documentation/devicetree/bindings/hwmon/microchip,mcp9982.yaml= b/Documentation/devicetree/bindings/hwmon/microchip,mcp9982.yaml new file mode 100644 index 000000000000..b59c64e9886d --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/microchip,mcp9982.yaml @@ -0,0 +1,223 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/hwmon/microchip,mcp9982.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip MCP998X/33 and MCP998XD/33D Temperature Monitor + +maintainers: + - Victor Duicu + +description: | + The MCP998X/33 and MCP998XD/33D family is a high-accuracy 2-wire + multichannel automotive temperature monitor. + The datasheet can be found here: + https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/Produc= tDocuments/DataSheets/MCP998X-Family-Data-Sheet-DS20006827.pdf + +properties: + compatible: + enum: + - microchip,mcp9933 + - microchip,mcp9933d + - microchip,mcp9982 + - microchip,mcp9982d + - microchip,mcp9983 + - microchip,mcp9983d + - microchip,mcp9984 + - microchip,mcp9984d + - microchip,mcp9985 + - microchip,mcp9985d + + reg: + maxItems: 1 + + interrupts: + maxItems: 2 + + interrupt-names: + description: + The chip family has three different interrupt pins divided among the= m. + The chips without "D" have alert-therm and therm-addr. + The chips with "D" have alert-therm and sys-shtdwn. + minItems: 1 + maxItems: 2 + items: + enum: [alert-therm, therm-addr, sys-shtdwn] + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + microchip,enable-anti-parallel: + description: + Enable anti-parallel diode mode operation. + MCP9984/84D/85/85D and MCP9933/33D support reading two external diod= es + in anti-parallel connection on the same set of pins. + type: boolean + + microchip,parasitic-res-on-channel1-2: + description: + Indicates that the chip and the diodes/transistors are sufficiently = far + apart that a parasitic resistance is added to the wires, which can a= ffect + the measurements. Due to the anti-parallel diode connections, channe= ls + 1 and 2 are affected together. + type: boolean + + microchip,parasitic-res-on-channel3-4: + description: + Indicates that the chip and the diodes/transistors are sufficiently = far + apart that a parasitic resistance is added to the wires, which can a= ffect + the measurements. Due to the anti-parallel diode connections, channe= ls + 3 and 4 are affected together. + type: boolean + + microchip,power-state: + description: + The chip can be set in Run state or Standby state. In Run state the = ADC + is converting on all channels at the programmed conversion rate. + In Standby state the host must initiate a conversion cycle by writing + to the One-Shot register. + True value sets Run state. + Chips with "D" in the name can only be set in Run mode. + type: boolean + + vdd-supply: true + +patternProperties: + "^channel@[1-4]$": + description: + Represents the external temperature channels to which + a remote diode is connected. + type: object + + properties: + reg: + items: + maxItems: 1 + + label: + description: Unique name to identify which channel this is. + + required: + - reg + + additionalProperties: false + +required: + - compatible + - reg + - vdd-supply + +allOf: + - if: + properties: + compatible: + contains: + enum: + - microchip,mcp9982d + - microchip,mcp9983d + - microchip,mcp9984d + - microchip,mcp9985d + - microchip,mcp9933d + then: + properties: + interrupt-names: + not: + contains: + const: therm-addr + required: + - microchip,power-state + - microchip,parasitic-res-on-channel1-2 + - microchip,parasitic-res-on-channel3-4 + else: + properties: + microchip,power-state: true + interrupt-names: + not: + contains: + const: sys-shtdwn + + - if: + properties: + compatible: + contains: + enum: + - microchip,mcp9982 + - microchip,mcp9982d + then: + properties: + microchip,enable-anti-parallel: false + patternProperties: + "^channel@[2-4]$": false + + - if: + properties: + compatible: + contains: + enum: + - microchip,mcp9983 + - microchip,mcp9983d + then: + properties: + microchip,enable-anti-parallel: false + patternProperties: + "^channel@[3-4]$": false + + - if: + properties: + compatible: + contains: + enum: + - microchip,mcp9933 + - microchip,mcp9933d + then: + patternProperties: + "^channel@[3-4]$": false + + - if: + properties: + compatible: + contains: + enum: + - microchip,mcp9984 + - microchip,mcp9984d + then: + properties: + channel@4: false + +additionalProperties: false + +examples: + - | + i2c { + #address-cells =3D <1>; + #size-cells =3D <0>; + + temperature-sensor@4c { + compatible =3D "microchip,mcp9985"; + reg =3D <0x4c>; + + #address-cells =3D <1>; + #size-cells =3D <0>; + + microchip,enable-anti-parallel; + microchip,parasitic-res-on-channel1-2; + microchip,parasitic-res-on-channel3-4; + vdd-supply =3D <&vdd>; + + channel@1 { + reg =3D <1>; + label =3D "Room Temperature"; + }; + + channel@2 { + reg =3D <2>; + label =3D "GPU Temperature"; + }; + }; + }; + +... diff --git a/MAINTAINERS b/MAINTAINERS index e08767323763..026510a4129c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17160,6 +17160,12 @@ S: Maintained F: Documentation/devicetree/bindings/iio/adc/microchip,mcp3911.yaml F: drivers/iio/adc/mcp3911.c =20 +MICROCHIP MCP9982 TEMPERATURE DRIVER +M: Victor Duicu +L: linux-hwmon@vger.kernel.org +S: Supported +F: Documentation/devicetree/bindings/hwmon/microchip,mcp9982.yaml + MICROCHIP MMC/SD/SDIO MCI DRIVER M: Aubin Constans S: Maintained --=20 2.51.0 From nobody Fri Apr 3 04:52:59 2026 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) (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 D36D036BCC3; Tue, 17 Feb 2026 14:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=68.232.154.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771337265; cv=none; b=K0hjN2kk3GHBv25++s75RZnfMwNpf5bwoFCFKOd+LUWS5vgDskPaKmJEO5mbIcMSMIcimtrw7eGCqoZSQZO3SJSK8/u7Zn5UkzHooTjo0eYsM2Wu0tz2BM/eF4q9fZE2cF4A38kHBJ3C3R4sOSan66SIZPKy8dg3fPjgksBcsGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771337265; c=relaxed/simple; bh=sh+9klWG0uTPdaqRF2gWCjNyT/6opk54MCSvEC6JVEU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=cKS8OKHrwUAzunrQrJEKs4yTW8ts4/LeTfxefNbzJkTkblfhSNNbE7N8okUyLKVv+HpTtUQMc33Vjnu+bkToI8NgTASRVmJuoF6MLWF4WJPEkmkd2hO+oBOvjahF9BCJ+vyr0sSZklHWNO79x0IgG/pJPRVZVFYWzrod00iW9KQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com; spf=pass smtp.mailfrom=microchip.com; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b=vPGRgsM7; arc=none smtp.client-ip=68.232.154.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=microchip.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b="vPGRgsM7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1771337262; x=1802873262; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=sh+9klWG0uTPdaqRF2gWCjNyT/6opk54MCSvEC6JVEU=; b=vPGRgsM7grZgDlz8YNYc5peH3boGtnFZCHbWYYN54jIs7dFK0+ojMHZC 24PW3xj5l4U2Zdxt6DPGvzjfCMSz4m77P6k44jijoKaVm5SDukvjCx5xx Kz/yZvPqhG1cIpR+eQXHDBCK2ZtcZ7fS7IUM8I7grIVGRHtlkjqxzNv7m mPk+tG4B0Gv6eiW8GNuYpNyOh/JOOJas+ZgC4DRSUQKM5Rx+DlTr3ckHU dRAdvGaqw7lKA1p7saEJbvOvM5F8kc12Q80FzDWXQdCdFxbyekyfQ2TF2 srRrpYq9t9J3WidRYRxx7lY41UadujboiIj6SaFxNLwTS0/Vk6qBUaXMJ A==; X-CSE-ConnectionGUID: NsNq/mm8SHKghX7/+9wqEA== X-CSE-MsgGUID: n5ZyPxcrSraK8mAeKqTdNQ== X-IronPort-AV: E=Sophos;i="6.21,296,1763449200"; d="scan'208";a="53912588" X-Amp-Result: SKIPPED(no attachment in message) Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa2.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 17 Feb 2026 07:07:34 -0700 Received: from chn-vm-ex01.mchp-main.com (10.10.85.143) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.58; Tue, 17 Feb 2026 07:07:27 -0700 Received: from [127.0.1.1] (10.10.85.11) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server id 15.1.2507.58 via Frontend Transport; Tue, 17 Feb 2026 07:07:24 -0700 From: Victor Duicu Date: Tue, 17 Feb 2026 16:06:14 +0200 Subject: [PATCH v10 2/2] hwmon: add support for MCP998X 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: <20260217-add-mcp9982-hwmon-v10-2-5e0aaae6f289@microchip.com> References: <20260217-add-mcp9982-hwmon-v10-0-5e0aaae6f289@microchip.com> In-Reply-To: <20260217-add-mcp9982-hwmon-v10-0-5e0aaae6f289@microchip.com> To: Guenter Roeck , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet CC: , , , , , X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1771337236; l=36135; i=victor.duicu@microchip.com; s=20260213; h=from:subject:message-id; bh=sh+9klWG0uTPdaqRF2gWCjNyT/6opk54MCSvEC6JVEU=; b=3sLDjVYHeO3g1XhBG1PsbTIWIsl+S4Rz0FzY8HQrmBervin1HU2jE4+AoZ1P6cgzxZym+VCjL 21Orb+XRklfBXkSuvyUqsO7rUgF3bQAngb7HqNp4Gudgf21C6Vu/aPR X-Developer-Key: i=victor.duicu@microchip.com; a=ed25519; pk=/o+aE26HN3Piv9T5t+efqb0aeJw9ErwMPeSC8lYXQsA= This is the driver for Microchip MCP998X/33 and MCP998XD/33D Multichannel Automotive Temperature Monitor Family. Signed-off-by: Victor Duicu --- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/mcp9982.rst | 111 +++++ MAINTAINERS | 2 + drivers/hwmon/Kconfig | 11 + drivers/hwmon/Makefile | 1 + drivers/hwmon/mcp9982.c | 954 ++++++++++++++++++++++++++++++++++++= ++++ 6 files changed, 1080 insertions(+) diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst index 85d7a686883e..b02709fc216e 100644 --- a/Documentation/hwmon/index.rst +++ b/Documentation/hwmon/index.rst @@ -173,6 +173,7 @@ Hardware Monitoring Kernel Drivers mc33xs2410_hwmon mc34vr500 mcp3021 + mcp9982 menf21bmc mlxreg-fan mp2856 diff --git a/Documentation/hwmon/mcp9982.rst b/Documentation/hwmon/mcp9982.= rst new file mode 100644 index 000000000000..ceff3e69ee78 --- /dev/null +++ b/Documentation/hwmon/mcp9982.rst @@ -0,0 +1,111 @@ +.. SPDX-License-Identifier: GPL-2.0+ + +Kernel driver MCP998X +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Supported chips: + + * Microchip Technology MCP998X/MCP9933 and MCP998XD/MCP9933D + + Prefix: 'mcp9982' + + Datasheet: + https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/Produc= tDocuments/DataSheets/MCP998X-Family-Data-Sheet-DS20006827.pdf + +Authors: + + - Victor Duicu + +Description +----------- + +This driver implements support for the MCP998X family containing: MCP9982, +MCP9982D, MCP9983, MCP9983D, MCP9984, MCP9984D, MCP9985, MCP9985D, +MCP9933 and MCP9933D. + +The MCP998X Family is a high accuracy 2-wire multichannel automotive +temperature monitor. + +The chips in the family have different numbers of external channels, +ranging from 1 (MCP9982) to 4 channels (MCP9985). Reading diodes in +anti-parallel connection is supported by MCP9984/85/33 and +MCP9984D/85D/33D. Dedicated hardware shutdown circuitry is present +only in MCP998XD and MCP9933D. + +Temperatures are read in millidegrees Celsius, ranging from -64 to +191.875 with 0.125 precision. + +Each channel has a minimum, maximum, and critical limit alongside associat= ed alarms. +The chips also implement a hysteresis mechanism which applies only to the = maximum +and critical limits. The relative difference between a limit and its hyste= resis +is the same for both and the value is kept in a single register. + +The chips measure temperatures with a variable conversion rate. +Update_interval =3D Conversion/Second, so the available options are: +- 16000 (ms) =3D 1 conv/16 sec +- 8000 (ms) =3D 1 conv/8 sec +- 4000 (ms) =3D 1 conv/4 sec +- 2000 (ms) =3D 1 conv/2 sec +- 1000 (ms) =3D 1 conv/sec +- 500 (ms) =3D 2 conv/sec +- 250 (ms) =3D 4 conv/sec +- 125 (ms) =3D 8 conv/sec +- 64 (ms) =3D 16 conv/sec +- 32 (ms) =3D 32 conv/sec +- 16 (ms) =3D 64 conv/sec + +Usage Notes +----------- + +Parameters that can be configured in devicetree: +- anti-parallel diode mode operation +- resistance error correction on channels 1 and 2 +- resistance error correction on channels 3 and 4 +- power state + +Chips 82/83 and 82D/83D do not support anti-parallel diode mode. +For chips with "D" in the name resistance error correction must be on. +Please see Documentation/devicetree/bindings/hwmon/microchip,mcp9982.yaml +for details. + +There are two power states: +- Active state: in which the chip is converting on all channels at the +programmed rate. + +- Standby state: in which the host must initiate a conversion cycle. + +Chips with "D" in the name work in Active state only and those without +can work in either state. + +Chips with "D" in the name can't set update interval slower than 1 second. + +Among the hysteresis attributes, only the tempX_crit_hyst ones are writeab= le +while the others are read only. Setting tempX_crit_hyst writes the differe= nce +between tempX_crit and tempX_crit_hyst in the hysteresis register. The new= value +applies automatically to the other limits. At power up the device starts = with +the value 10 in the hysteresis register. + +Sysfs entries +------------- + +The following attributes are supported. The temperature limits and +update_interval are read-write. The attribute tempX_crit_hyst is read-writ= e, +while tempX_max_hyst is read only. All other attributes are read only. + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +temp[1-5]_label User name for channel. +temp[1-5]_input Measured temperature for channel. + +temp[1-5]_crit Critical temperature limit. +temp[1-5]_crit_alarm Critical temperature limit alarm. +temp[1-5]_crit_hyst Critical temperature limit hysteresis. + +temp[1-5]_max High temperature limit. +temp[1-5]_max_alarm High temperature limit alarm. +temp[1-5]_max_hyst High temperature limit hysteresis. + +temp[1-5]_min Low temperature limit. +temp[1-5]_min_alarm Low temperature limit alarm. + +update_interval The interval at which the chip will update readings. +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/MAINTAINERS b/MAINTAINERS index 026510a4129c..5c6662e10b04 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17165,6 +17165,8 @@ M: Victor Duicu L: linux-hwmon@vger.kernel.org S: Supported F: Documentation/devicetree/bindings/hwmon/microchip,mcp9982.yaml +F: Documentation/hwmon/mcp9982.rst +F: drivers/hwmon/mcp9982.c =20 MICROCHIP MMC/SD/SDIO MCI DRIVER M: Aubin Constans diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 157678b821fc..c758ab2d5fdf 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1388,6 +1388,17 @@ config SENSORS_MCP3021 This driver can also be built as a module. If so, the module will be called mcp3021. =20 +config SENSORS_MCP9982 + tristate "Microchip Technology MCP9982 driver" + depends on I2C + select REGMAP_I2C + help + Say yes here to include support for Microchip Technology's MCP998= X/33 + and MCP998XD/33D Multichannel Automotive Temperature Monitor Fami= ly. + + This driver can also be built as a module. If so, the module + will be called mcp9982. + config SENSORS_MLXREG_FAN tristate "Mellanox FAN driver" depends on MELLANOX_PLATFORM diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index eade8e3b1bde..cec33da29a68 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -170,6 +170,7 @@ obj-$(CONFIG_SENSORS_MC13783_ADC)+=3D mc13783-adc.o obj-$(CONFIG_SENSORS_MC33XS2410) +=3D mc33xs2410_hwmon.o obj-$(CONFIG_SENSORS_MC34VR500) +=3D mc34vr500.o obj-$(CONFIG_SENSORS_MCP3021) +=3D mcp3021.o +obj-$(CONFIG_SENSORS_MCP9982) +=3D mcp9982.o obj-$(CONFIG_SENSORS_TC654) +=3D tc654.o obj-$(CONFIG_SENSORS_TPS23861) +=3D tps23861.o obj-$(CONFIG_SENSORS_MLXREG_FAN) +=3D mlxreg-fan.o diff --git a/drivers/hwmon/mcp9982.c b/drivers/hwmon/mcp9982.c new file mode 100644 index 000000000000..f13e80283404 --- /dev/null +++ b/drivers/hwmon/mcp9982.c @@ -0,0 +1,954 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * HWMON driver for MCP998X/33 and MCP998XD/33D Multichannel Automotive + * Temperature Monitor Family + * + * Copyright (C) 2026 Microchip Technology Inc. and its subsidiaries + * + * Author: Victor Duicu + * + * Datasheet can be found here: + * https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/Product= Documents/DataSheets/MCP998X-Family-Data-Sheet-DS20006827.pdf + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* MCP9982 Registers */ +#define MCP9982_HIGH_BYTE_ADDR(index) (2 * (index)) +#define MCP9982_ONE_SHOT_ADDR 0x0A +#define MCP9982_INTERNAL_HIGH_LIMIT_ADDR 0x0B +#define MCP9982_INTERNAL_LOW_LIMIT_ADDR 0x0C +#define MCP9982_EXT_HIGH_LIMIT_ADDR(index) (4 * ((index) - 1) + 0x0D) +#define MCP9982_EXT_LOW_LIMIT_ADDR(index) (4 * ((index) - 1) + 0x0F) +#define MCP9982_THERM_LIMIT_ADDR(index) ((index) + 0x1D) +#define MCP9982_CFG_ADDR 0x22 +#define MCP9982_CONV_ADDR 0x24 +#define MCP9982_HYS_ADDR 0x25 +#define MCP9982_CONSEC_ALRT_ADDR 0x26 +#define MCP9982_ALRT_CFG_ADDR 0x27 +#define MCP9982_RUNNING_AVG_ADDR 0x28 +#define MCP9982_HOTTEST_CFG_ADDR 0x29 +#define MCP9982_STATUS_ADDR 0x2A +#define MCP9982_EXT_FAULT_STATUS_ADDR 0x2B +#define MCP9982_HIGH_LIMIT_STATUS_ADDR 0x2C +#define MCP9982_LOW_LIMIT_STATUS_ADDR 0x2D +#define MCP9982_THERM_LIMIT_STATUS_ADDR 0x2E +#define MCP9982_HOTTEST_HIGH_BYTE_ADDR 0x2F +#define MCP9982_HOTTEST_LOW_BYTE_ADDR 0x30 +#define MCP9982_HOTTEST_STATUS_ADDR 0x31 +#define MCP9982_THERM_SHTDWN_CFG_ADDR 0x32 +#define MCP9982_HRDW_THERM_SHTDWN_LIMIT_ADDR 0x33 +#define MCP9982_EXT_BETA_CFG_ADDR(index) ((index) + 0x33) +#define MCP9982_EXT_IDEAL_ADDR(index) ((index) + 0x35) + +/* MCP9982 Bits */ +#define MCP9982_CFG_MSKAL BIT(7) +#define MCP9982_CFG_RS BIT(6) +#define MCP9982_CFG_ATTHM BIT(5) +#define MCP9982_CFG_RECD12 BIT(4) +#define MCP9982_CFG_RECD34 BIT(3) +#define MCP9982_CFG_RANGE BIT(2) +#define MCP9982_CFG_DA_ENA BIT(1) +#define MCP9982_CFG_APDD BIT(0) + +#define MCP9982_STATUS_BUSY BIT(5) + +/* Constants and default values */ +#define MCP9982_MAX_NUM_CHANNELS 5 +#define MCP9982_BETA_AUTODETECT 16 +#define MCP9982_IDEALITY_DEFAULT 18 +#define MCP9982_OFFSET 64 +#define MCP9982_DEFAULT_CONSEC_ALRT_VAL 112 +#define MCP9982_DEFAULT_HYS_VAL 10 +#define MCP9982_DEFAULT_CONV_VAL 6 +#define MCP9982_WAKE_UP_TIME_US 125000 +#define MCP9982_WAKE_UP_TIME_MAX_US 130000 +#define MCP9982_HIGH_LIMIT_DEFAULT 85000 +#define MCP9982_LOW_LIMIT_DEFAULT 0 + +static const struct hwmon_channel_info * const mcp9985_info[] =3D { + HWMON_CHANNEL_INFO(temp, + HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MIN | + HWMON_T_MIN_ALARM | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_MAX_HYST | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | + HWMON_T_CRIT_HYST, + HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MIN | + HWMON_T_MIN_ALARM | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_MAX_HYST | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | + HWMON_T_CRIT_HYST, + HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MIN | + HWMON_T_MIN_ALARM | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_MAX_HYST | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | + HWMON_T_CRIT_HYST, + HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MIN | + HWMON_T_MIN_ALARM | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_MAX_HYST | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | + HWMON_T_CRIT_HYST, + HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_MIN | + HWMON_T_MIN_ALARM | HWMON_T_MAX | HWMON_T_MAX_ALARM | + HWMON_T_MAX_HYST | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | + HWMON_T_CRIT_HYST), + HWMON_CHANNEL_INFO(chip, + HWMON_C_UPDATE_INTERVAL), + NULL +}; + +/** + * struct mcp9982_features - features of a mcp9982 instance + * @name: chip's name + * @phys_channels: number of physical channels supported by the chip + * @hw_thermal_shutdown: presence of hardware thermal shutdown circuitry + * @allow_apdd: whether the chip supports enabling APDD + */ +struct mcp9982_features { + const char *name; + u8 phys_channels; + bool hw_thermal_shutdown; + bool allow_apdd; +}; + +static const struct mcp9982_features mcp9933_chip_config =3D { + .name =3D "mcp9933", + .phys_channels =3D 3, + .hw_thermal_shutdown =3D false, + .allow_apdd =3D true, +}; + +static const struct mcp9982_features mcp9933d_chip_config =3D { + .name =3D "mcp9933d", + .phys_channels =3D 3, + .hw_thermal_shutdown =3D true, + .allow_apdd =3D true, +}; + +static const struct mcp9982_features mcp9982_chip_config =3D { + .name =3D "mcp9982", + .phys_channels =3D 2, + .hw_thermal_shutdown =3D false, + .allow_apdd =3D false, +}; + +static const struct mcp9982_features mcp9982d_chip_config =3D { + .name =3D "mcp9982d", + .phys_channels =3D 2, + .hw_thermal_shutdown =3D true, + .allow_apdd =3D false, +}; + +static const struct mcp9982_features mcp9983_chip_config =3D { + .name =3D "mcp9983", + .phys_channels =3D 3, + .hw_thermal_shutdown =3D false, + .allow_apdd =3D false, +}; + +static const struct mcp9982_features mcp9983d_chip_config =3D { + .name =3D "mcp9983d", + .phys_channels =3D 3, + .hw_thermal_shutdown =3D true, + .allow_apdd =3D false, +}; + +static const struct mcp9982_features mcp9984_chip_config =3D { + .name =3D "mcp9984", + .phys_channels =3D 4, + .hw_thermal_shutdown =3D false, + .allow_apdd =3D true, +}; + +static const struct mcp9982_features mcp9984d_chip_config =3D { + .name =3D "mcp9984d", + .phys_channels =3D 4, + .hw_thermal_shutdown =3D true, + .allow_apdd =3D true, +}; + +static const struct mcp9982_features mcp9985_chip_config =3D { + .name =3D "mcp9985", + .phys_channels =3D 5, + .hw_thermal_shutdown =3D false, + .allow_apdd =3D true, +}; + +static const struct mcp9982_features mcp9985d_chip_config =3D { + .name =3D "mcp9985d", + .phys_channels =3D 5, + .hw_thermal_shutdown =3D true, + .allow_apdd =3D true, +}; + +static const unsigned int mcp9982_update_interval[11] =3D { + 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 64, 32, 16 +}; + +/* MCP9982 regmap configuration */ +static const struct regmap_range mcp9982_regmap_wr_ranges[] =3D { + regmap_reg_range(MCP9982_ONE_SHOT_ADDR, MCP9982_CFG_ADDR), + regmap_reg_range(MCP9982_CONV_ADDR, MCP9982_HOTTEST_CFG_ADDR), + regmap_reg_range(MCP9982_THERM_SHTDWN_CFG_ADDR, MCP9982_THERM_SHTDWN_CFG_= ADDR), + regmap_reg_range(MCP9982_EXT_BETA_CFG_ADDR(1), MCP9982_EXT_IDEAL_ADDR(4)), +}; + +static const struct regmap_access_table mcp9982_regmap_wr_table =3D { + .yes_ranges =3D mcp9982_regmap_wr_ranges, + .n_yes_ranges =3D ARRAY_SIZE(mcp9982_regmap_wr_ranges), +}; + +static const struct regmap_range mcp9982_regmap_rd_ranges[] =3D { + regmap_reg_range(MCP9982_HIGH_BYTE_ADDR(0), MCP9982_CFG_ADDR), + regmap_reg_range(MCP9982_CONV_ADDR, MCP9982_EXT_IDEAL_ADDR(4)), +}; + +static const struct regmap_access_table mcp9982_regmap_rd_table =3D { + .yes_ranges =3D mcp9982_regmap_rd_ranges, + .n_yes_ranges =3D ARRAY_SIZE(mcp9982_regmap_rd_ranges), +}; + +static bool mcp9982_is_volatile_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case MCP9982_ONE_SHOT_ADDR: + case MCP9982_INTERNAL_HIGH_LIMIT_ADDR: + case MCP9982_INTERNAL_LOW_LIMIT_ADDR: + case MCP9982_EXT_LOW_LIMIT_ADDR(1): + case MCP9982_EXT_LOW_LIMIT_ADDR(1) + 1: + case MCP9982_EXT_LOW_LIMIT_ADDR(2): + case MCP9982_EXT_LOW_LIMIT_ADDR(2) + 1: + case MCP9982_EXT_LOW_LIMIT_ADDR(3): + case MCP9982_EXT_LOW_LIMIT_ADDR(3) + 1: + case MCP9982_EXT_LOW_LIMIT_ADDR(4): + case MCP9982_EXT_LOW_LIMIT_ADDR(4) + 1: + case MCP9982_EXT_HIGH_LIMIT_ADDR(1): + case MCP9982_EXT_HIGH_LIMIT_ADDR(1) + 1: + case MCP9982_EXT_HIGH_LIMIT_ADDR(2): + case MCP9982_EXT_HIGH_LIMIT_ADDR(2) + 1: + case MCP9982_EXT_HIGH_LIMIT_ADDR(3): + case MCP9982_EXT_HIGH_LIMIT_ADDR(3) + 1: + case MCP9982_EXT_HIGH_LIMIT_ADDR(4): + case MCP9982_EXT_HIGH_LIMIT_ADDR(4) + 1: + case MCP9982_THERM_LIMIT_ADDR(0): + case MCP9982_THERM_LIMIT_ADDR(1): + case MCP9982_THERM_LIMIT_ADDR(2): + case MCP9982_THERM_LIMIT_ADDR(3): + case MCP9982_THERM_LIMIT_ADDR(4): + case MCP9982_CFG_ADDR: + case MCP9982_CONV_ADDR: + case MCP9982_HYS_ADDR: + case MCP9982_CONSEC_ALRT_ADDR: + case MCP9982_ALRT_CFG_ADDR: + case MCP9982_RUNNING_AVG_ADDR: + case MCP9982_HOTTEST_CFG_ADDR: + case MCP9982_THERM_SHTDWN_CFG_ADDR: + return false; + default: + return true; + } +} + +static const struct regmap_config mcp9982_regmap_config =3D { + .reg_bits =3D 8, + .val_bits =3D 8, + .rd_table =3D &mcp9982_regmap_rd_table, + .wr_table =3D &mcp9982_regmap_wr_table, + .volatile_reg =3D mcp9982_is_volatile_reg, + .max_register =3D MCP9982_EXT_IDEAL_ADDR(4), + .cache_type =3D REGCACHE_MAPLE, +}; + +/** + * struct mcp9982_priv - information about chip parameters + * @regmap: device register map + * @chip: pointer to structure holding chip features + * @labels: labels of the channels + * @interval_idx: index representing the current update interval + * @enabled_channel_mask: mask containing which channels should be enabled + * @num_channels: number of active physical channels + * @recd34_enable: state of Resistance Error Correction(REC) on channels = 3 and 4 + * @recd12_enable: state of Resistance Error Correction(REC) on channels = 1 and 2 + * @apdd_enable: state of anti-parallel diode mode + * @run_state: chip is in Run state, otherwise is in Standby state + */ +struct mcp9982_priv { + struct regmap *regmap; + const struct mcp9982_features *chip; + const char *labels[MCP9982_MAX_NUM_CHANNELS]; + unsigned int interval_idx; + unsigned long enabled_channel_mask; + u8 num_channels; + bool recd34_enable; + bool recd12_enable; + bool apdd_enable; + bool run_state; +}; + +static int mcp9982_read_limit(struct mcp9982_priv *priv, u8 address, long = *val) +{ + unsigned int limit, reg_high, reg_low; + int ret; + + switch (address) { + case MCP9982_INTERNAL_HIGH_LIMIT_ADDR: + case MCP9982_INTERNAL_LOW_LIMIT_ADDR: + case MCP9982_THERM_LIMIT_ADDR(0): + case MCP9982_THERM_LIMIT_ADDR(1): + case MCP9982_THERM_LIMIT_ADDR(2): + case MCP9982_THERM_LIMIT_ADDR(3): + case MCP9982_THERM_LIMIT_ADDR(4): + ret =3D regmap_read(priv->regmap, address, &limit); + if (ret) + return ret; + + *val =3D ((int)limit - MCP9982_OFFSET) * 1000; + + return 0; + case MCP9982_EXT_HIGH_LIMIT_ADDR(1): + case MCP9982_EXT_HIGH_LIMIT_ADDR(2): + case MCP9982_EXT_HIGH_LIMIT_ADDR(3): + case MCP9982_EXT_HIGH_LIMIT_ADDR(4): + case MCP9982_EXT_LOW_LIMIT_ADDR(1): + case MCP9982_EXT_LOW_LIMIT_ADDR(2): + case MCP9982_EXT_LOW_LIMIT_ADDR(3): + case MCP9982_EXT_LOW_LIMIT_ADDR(4): + /* + * The MCP998X family is designed so that block reading is allowed + * only on the dedicated temperature and status memory blocks. + * Reading from those memory areas uses SMbus, while from any other + * region I2C is used and only one byte readings are allowed. + * + * This behavior is described in the documentation at page 26. + * + * When reading the temperature limits only single byte reads + * are allowed. + */ + ret =3D regmap_read(priv->regmap, address, ®_high); + if (ret) + return ret; + + ret =3D regmap_read(priv->regmap, address + 1, ®_low); + if (ret) + return ret; + + *val =3D ((reg_high << 8) + reg_low) >> 5; + *val =3D (*val - (MCP9982_OFFSET << 3)) * 125; + + return 0; + default: + return -EINVAL; + } +} + +static int mcp9982_read(struct device *dev, enum hwmon_sensor_types type, = u32 attr, int channel, + long *val) +{ + struct mcp9982_priv *priv =3D dev_get_drvdata(dev); + unsigned int reg_high, reg_low; + int ret, hyst; + u8 addr; + + /* In Standby State the conversion cycle must be initiated manually. */ + if (!priv->run_state) { + ret =3D regmap_write(priv->regmap, MCP9982_ONE_SHOT_ADDR, 1); + if (ret) + return ret; + usleep_range(MCP9982_WAKE_UP_TIME_US, MCP9982_WAKE_UP_TIME_MAX_US); + } + + switch (type) { + case hwmon_temp: + switch (attr) { + case hwmon_temp_input: + /* Block reading from addresses 0x00->0x09 is not allowed. */ + ret =3D regmap_read(priv->regmap, MCP9982_HIGH_BYTE_ADDR(channel), ®= _high); + if (ret) + return ret; + + ret =3D regmap_read(priv->regmap, MCP9982_HIGH_BYTE_ADDR(channel) + 1, + ®_low); + if (ret) + return ret; + + *val =3D ((reg_high << 8) + reg_low) >> 5; + *val =3D (*val - (MCP9982_OFFSET << 3)) * 125; + + return 0; + case hwmon_temp_max: + if (channel) + addr =3D MCP9982_EXT_HIGH_LIMIT_ADDR(channel); + else + addr =3D MCP9982_INTERNAL_HIGH_LIMIT_ADDR; + + return mcp9982_read_limit(priv, addr, val); + case hwmon_temp_max_alarm: + *val =3D regmap_test_bits(priv->regmap, MCP9982_HIGH_LIMIT_STATUS_ADDR, + BIT(channel)); + if (*val < 0) + return *val; + + return 0; + case hwmon_temp_max_hyst: + if (channel) + addr =3D MCP9982_EXT_HIGH_LIMIT_ADDR(channel); + else + addr =3D MCP9982_INTERNAL_HIGH_LIMIT_ADDR; + ret =3D mcp9982_read_limit(priv, addr, val); + if (ret) + return ret; + + ret =3D regmap_read(priv->regmap, MCP9982_HYS_ADDR, &hyst); + if (ret) + return ret; + + *val -=3D hyst * 1000; + + return 0; + case hwmon_temp_min: + if (channel) + addr =3D MCP9982_EXT_LOW_LIMIT_ADDR(channel); + else + addr =3D MCP9982_INTERNAL_LOW_LIMIT_ADDR; + + return mcp9982_read_limit(priv, addr, val); + case hwmon_temp_min_alarm: + *val =3D regmap_test_bits(priv->regmap, MCP9982_LOW_LIMIT_STATUS_ADDR, + BIT(channel)); + if (*val < 0) + return *val; + + return 0; + case hwmon_temp_crit: + return mcp9982_read_limit(priv, MCP9982_THERM_LIMIT_ADDR(channel), val); + case hwmon_temp_crit_alarm: + *val =3D regmap_test_bits(priv->regmap, MCP9982_THERM_LIMIT_STATUS_ADDR, + BIT(channel)); + if (*val < 0) + return *val; + + return 0; + case hwmon_temp_crit_hyst: + ret =3D mcp9982_read_limit(priv, MCP9982_THERM_LIMIT_ADDR(channel), val= ); + if (ret) + return ret; + + ret =3D regmap_read(priv->regmap, MCP9982_HYS_ADDR, &hyst); + if (ret) + return ret; + + *val -=3D hyst * 1000; + + return 0; + default: + return -EINVAL; + } + case hwmon_chip: + switch (attr) { + case hwmon_chip_update_interval: + *val =3D mcp9982_update_interval[priv->interval_idx]; + return 0; + default: + return -EINVAL; + } + default: + return -EINVAL; + } +} + +static int mcp9982_read_label(struct device *dev, enum hwmon_sensor_types = type, u32 attr, + int channel, const char **str) +{ + struct mcp9982_priv *priv =3D dev_get_drvdata(dev); + + switch (type) { + case hwmon_temp: + switch (attr) { + case hwmon_temp_label: + *str =3D priv->labels[channel]; + return 0; + default: + return -EOPNOTSUPP; + } + default: + return -EOPNOTSUPP; + } +} + +static int mcp9982_write_limit(struct mcp9982_priv *priv, u8 address, long= val) +{ + int ret; + unsigned int regh, regl; + + /* Range is always -64 to 191.875=C2=B0C. */ + val =3D clamp_val(val, -64000, 191875); + val =3D (val + MCP9982_OFFSET * 1000) << 5; + val =3D DIV_ROUND_CLOSEST(val, 125); + + switch (address) { + case MCP9982_INTERNAL_HIGH_LIMIT_ADDR: + case MCP9982_INTERNAL_LOW_LIMIT_ADDR: + case MCP9982_THERM_LIMIT_ADDR(0): + case MCP9982_THERM_LIMIT_ADDR(1): + case MCP9982_THERM_LIMIT_ADDR(2): + case MCP9982_THERM_LIMIT_ADDR(3): + case MCP9982_THERM_LIMIT_ADDR(4): + val =3D val >> 8; + ret =3D regmap_write(priv->regmap, address, val); + if (ret) + return ret; + + return 0; + case MCP9982_EXT_HIGH_LIMIT_ADDR(1): + case MCP9982_EXT_HIGH_LIMIT_ADDR(2): + case MCP9982_EXT_HIGH_LIMIT_ADDR(3): + case MCP9982_EXT_HIGH_LIMIT_ADDR(4): + case MCP9982_EXT_LOW_LIMIT_ADDR(1): + case MCP9982_EXT_LOW_LIMIT_ADDR(2): + case MCP9982_EXT_LOW_LIMIT_ADDR(3): + case MCP9982_EXT_LOW_LIMIT_ADDR(4): + regl =3D val & 0xFF; + regh =3D val >> 8; + + /* Block write to addresses 0x0D->0x1C is not allowed. */ + ret =3D regmap_write(priv->regmap, address, regh); + if (ret) + return ret; + + ret =3D regmap_write(priv->regmap, address + 1, regl); + if (ret) + return ret; + + return 0; + default: + return -EINVAL; + } +} + +static int mcp9982_write_hyst(struct mcp9982_priv *priv, int channel, long= val) +{ + int hyst, ret; + int limit; + + val =3D clamp_val(val, -64000, 191875); + val =3D (val + MCP9982_OFFSET * 1000) << 5; + val =3D DIV_ROUND_CLOSEST(val, 125); + val =3D val >> 8; + + /* Therm register is 8 bits and so it keeps only the integer part of the = temperature. */ + ret =3D regmap_read(priv->regmap, MCP9982_THERM_LIMIT_ADDR(channel), &lim= it); + if (ret) + return ret; + + hyst =3D clamp_val(limit - val, 0, 255); + + ret =3D regmap_write(priv->regmap, MCP9982_HYS_ADDR, hyst); + + return ret; +} + +static int mcp9982_write(struct device *dev, enum hwmon_sensor_types type,= u32 attr, int channel, + long val) +{ + struct mcp9982_priv *priv =3D dev_get_drvdata(dev); + unsigned int idx; + u8 addr; + int ret; + + switch (type) { + case hwmon_chip: + switch (attr) { + case hwmon_chip_update_interval: + + /* + * For MCP998XD and MCP9933D update interval + * can't be slower than 1 second. + */ + if (priv->chip->hw_thermal_shutdown) + val =3D clamp(val, 0, 1000); + + idx =3D find_closest_descending(val, mcp9982_update_interval, + ARRAY_SIZE(mcp9982_update_interval)); + + ret =3D regmap_write(priv->regmap, MCP9982_CONV_ADDR, idx); + if (ret) + return ret; + + priv->interval_idx =3D idx; + + return 0; + default: + return -EINVAL; + } + case hwmon_temp: + switch (attr) { + case hwmon_temp_max: + if (channel) + addr =3D MCP9982_EXT_HIGH_LIMIT_ADDR(channel); + else + addr =3D MCP9982_INTERNAL_HIGH_LIMIT_ADDR; + + return mcp9982_write_limit(priv, addr, val); + case hwmon_temp_min: + if (channel) + addr =3D MCP9982_EXT_LOW_LIMIT_ADDR(channel); + else + addr =3D MCP9982_INTERNAL_LOW_LIMIT_ADDR; + + return mcp9982_write_limit(priv, addr, val); + case hwmon_temp_crit: + return mcp9982_write_limit(priv, MCP9982_THERM_LIMIT_ADDR(channel), val= ); + case hwmon_temp_crit_hyst: + return mcp9982_write_hyst(priv, channel, val); + default: + return -EINVAL; + } + default: + return -EINVAL; + } +} + +static umode_t mcp9982_is_visible(const void *_data, enum hwmon_sensor_typ= es type, u32 attr, + int channel) +{ + const struct mcp9982_priv *priv =3D _data; + + if (!test_bit(channel, &priv->enabled_channel_mask)) + return 0; + + switch (type) { + case hwmon_temp: + switch (attr) { + case hwmon_temp_label: + if (priv->labels[channel]) + return 0444; + else + return 0; + case hwmon_temp_input: + case hwmon_temp_min_alarm: + case hwmon_temp_max_alarm: + case hwmon_temp_max_hyst: + case hwmon_temp_crit_alarm: + return 0444; + case hwmon_temp_min: + case hwmon_temp_max: + case hwmon_temp_crit: + case hwmon_temp_crit_hyst: + return 0644; + default: + return 0; + } + case hwmon_chip: + switch (attr) { + case hwmon_chip_update_interval: + return 0644; + default: + return 0; + } + default: + return 0; + } +} + +static const struct hwmon_ops mcp9982_hwmon_ops =3D { + .is_visible =3D mcp9982_is_visible, + .read =3D mcp9982_read, + .read_string =3D mcp9982_read_label, + .write =3D mcp9982_write, +}; + +static int mcp9982_init(struct device *dev, struct mcp9982_priv *priv) +{ + unsigned int i; + int ret; + u8 val; + + /* Chips 82/83 and 82D/83D do not support anti-parallel diode mode. */ + if (!priv->chip->allow_apdd && priv->apdd_enable =3D=3D 1) + return dev_err_probe(dev, -EINVAL, "Incorrect setting of APDD.\n"); + + /* Chips with "D" work only in Run state. */ + if (priv->chip->hw_thermal_shutdown && !priv->run_state) + return dev_err_probe(dev, -EINVAL, "Incorrect setting of Power State.\n"= ); + + /* + * For chips with "D" in the name, resistance error correction must be + * on so that hardware shutdown feature can't be overridden. + */ + if (priv->chip->hw_thermal_shutdown) + if (!priv->recd34_enable || !priv->recd12_enable) + return dev_err_probe(dev, -EINVAL, "Incorrect setting of RECD.\n"); + + /* + * Set default values in registers. + * APDD, RECD12 and RECD34 are active on 0. + */ + val =3D FIELD_PREP(MCP9982_CFG_MSKAL, 1) | + FIELD_PREP(MCP9982_CFG_RS, !priv->run_state) | + FIELD_PREP(MCP9982_CFG_ATTHM, 1) | + FIELD_PREP(MCP9982_CFG_RECD12, !priv->recd12_enable) | + FIELD_PREP(MCP9982_CFG_RECD34, !priv->recd34_enable) | + FIELD_PREP(MCP9982_CFG_RANGE, 1) | FIELD_PREP(MCP9982_CFG_DA_ENA, 0= ) | + FIELD_PREP(MCP9982_CFG_APDD, !priv->apdd_enable); + + ret =3D regmap_write(priv->regmap, MCP9982_CFG_ADDR, val); + if (ret) + return ret; + + /* Read initial value from register */ + ret =3D regmap_read(priv->regmap, MCP9982_CONV_ADDR, &priv->interval_idx); + if (ret) + return ret; + + ret =3D regmap_write(priv->regmap, MCP9982_HYS_ADDR, MCP9982_DEFAULT_HYS_= VAL); + if (ret) + return ret; + + ret =3D regmap_write(priv->regmap, MCP9982_CONSEC_ALRT_ADDR, MCP9982_DEFA= ULT_CONSEC_ALRT_VAL); + if (ret) + return ret; + + ret =3D regmap_write(priv->regmap, MCP9982_ALRT_CFG_ADDR, 0); + if (ret) + return ret; + + ret =3D regmap_write(priv->regmap, MCP9982_RUNNING_AVG_ADDR, 0); + if (ret) + return ret; + + ret =3D regmap_write(priv->regmap, MCP9982_HOTTEST_CFG_ADDR, 0); + if (ret) + return ret; + + /* + * Only external channels 1 and 2 support beta compensation. + * Set beta auto-detection. + */ + for (i =3D 1; i < 3; i++) + if (test_bit(i, &priv->enabled_channel_mask)) { + ret =3D regmap_write(priv->regmap, MCP9982_EXT_BETA_CFG_ADDR(i), + MCP9982_BETA_AUTODETECT); + if (ret) + return ret; + } + + /* Set default values for internal channel limits. */ + if (test_bit(0, &priv->enabled_channel_mask)) { + ret =3D mcp9982_write_limit(priv, MCP9982_INTERNAL_HIGH_LIMIT_ADDR, + MCP9982_HIGH_LIMIT_DEFAULT); + if (ret) + return ret; + + ret =3D mcp9982_write_limit(priv, MCP9982_INTERNAL_LOW_LIMIT_ADDR, + MCP9982_LOW_LIMIT_DEFAULT); + if (ret) + return ret; + + ret =3D mcp9982_write_limit(priv, MCP9982_THERM_LIMIT_ADDR(0), + MCP9982_HIGH_LIMIT_DEFAULT); + if (ret) + return ret; + } + + /* Set ideality factor and limits to default for external channels. */ + for (i =3D 1; i < MCP9982_MAX_NUM_CHANNELS; i++) + if (test_bit(i, &priv->enabled_channel_mask)) { + ret =3D regmap_write(priv->regmap, MCP9982_EXT_IDEAL_ADDR(i), + MCP9982_IDEALITY_DEFAULT); + if (ret) + return ret; + + ret =3D mcp9982_write_limit(priv, MCP9982_EXT_HIGH_LIMIT_ADDR(i), + MCP9982_HIGH_LIMIT_DEFAULT); + if (ret) + return ret; + + ret =3D mcp9982_write_limit(priv, MCP9982_EXT_LOW_LIMIT_ADDR(i), + MCP9982_LOW_LIMIT_DEFAULT); + if (ret) + return ret; + + ret =3D mcp9982_write_limit(priv, MCP9982_THERM_LIMIT_ADDR(i), + MCP9982_HIGH_LIMIT_DEFAULT); + if (ret) + return ret; + } + + return 0; +} + +static int mcp9982_parse_fw_config(struct device *dev, int device_nr_chann= els) +{ + struct mcp9982_priv *priv =3D dev_get_drvdata(dev); + unsigned int reg_nr; + int ret; + + /* Initialise internal channel( which is always present ). */ + priv->labels[0] =3D "internal diode"; + priv->enabled_channel_mask =3D 1; + + /* Default values to work on systems without devicetree or firmware nodes= . */ + if (!dev_fwnode(dev)) { + priv->num_channels =3D device_nr_channels; + priv->enabled_channel_mask =3D BIT(priv->num_channels) - 1; + priv->apdd_enable =3D false; + priv->recd12_enable =3D true; + priv->recd34_enable =3D true; + priv->run_state =3D true; + return 0; + } + + priv->apdd_enable =3D + device_property_read_bool(dev, "microchip,enable-anti-parallel"); + + priv->recd12_enable =3D + device_property_read_bool(dev, "microchip,parasitic-res-on-channel1-2"); + + priv->recd34_enable =3D + device_property_read_bool(dev, "microchip,parasitic-res-on-channel3-4"); + + priv->run_state =3D + device_property_read_bool(dev, "microchip,power-state"); + + priv->num_channels =3D device_get_child_node_count(dev) + 1; + + if (priv->num_channels > device_nr_channels) + return dev_err_probe(dev, -EINVAL, + "More channels than the chip supports.\n"); + + /* Read information about the external channels. */ + device_for_each_child_node_scoped(dev, child) { + reg_nr =3D 0; + ret =3D fwnode_property_read_u32(child, "reg", ®_nr); + if (ret || !reg_nr || reg_nr >=3D device_nr_channels) + return dev_err_probe(dev, -EINVAL, + "Channel reg is incorrectly set.\n"); + + fwnode_property_read_string(child, "label", &priv->labels[reg_nr]); + set_bit(reg_nr, &priv->enabled_channel_mask); + } + + return 0; +} + +static const struct hwmon_chip_info mcp998x_chip_info =3D { + .ops =3D &mcp9982_hwmon_ops, + .info =3D mcp9985_info, +}; + +static int mcp9982_probe(struct i2c_client *client) +{ + const struct mcp9982_features *chip; + struct device *dev =3D &client->dev; + struct mcp9982_priv *priv; + struct device *hwmon_dev; + int ret; + + priv =3D devm_kzalloc(dev, sizeof(struct mcp9982_priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->regmap =3D devm_regmap_init_i2c(client, &mcp9982_regmap_config); + + if (IS_ERR(priv->regmap)) + return dev_err_probe(dev, PTR_ERR(priv->regmap), + "Cannot initialize register map.\n"); + + dev_set_drvdata(dev, priv); + + chip =3D i2c_get_match_data(client); + if (!chip) + return -EINVAL; + priv->chip =3D chip; + + ret =3D mcp9982_parse_fw_config(dev, chip->phys_channels); + if (ret) + return ret; + + ret =3D mcp9982_init(dev, priv); + if (ret) + return ret; + + hwmon_dev =3D devm_hwmon_device_register_with_info(dev, chip->name, priv, + &mcp998x_chip_info, NULL); + + return PTR_ERR_OR_ZERO(hwmon_dev); +} + +static const struct i2c_device_id mcp9982_id[] =3D { + { .name =3D "mcp9933", .driver_data =3D (kernel_ulong_t)&mcp9933_chip_con= fig }, + { .name =3D "mcp9933d", .driver_data =3D (kernel_ulong_t)&mcp9933d_chip_c= onfig }, + { .name =3D "mcp9982", .driver_data =3D (kernel_ulong_t)&mcp9982_chip_con= fig }, + { .name =3D "mcp9982d", .driver_data =3D (kernel_ulong_t)&mcp9982d_chip_c= onfig }, + { .name =3D "mcp9983", .driver_data =3D (kernel_ulong_t)&mcp9983_chip_con= fig }, + { .name =3D "mcp9983d", .driver_data =3D (kernel_ulong_t)&mcp9983d_chip_c= onfig }, + { .name =3D "mcp9984", .driver_data =3D (kernel_ulong_t)&mcp9984_chip_con= fig }, + { .name =3D "mcp9984d", .driver_data =3D (kernel_ulong_t)&mcp9984d_chip_c= onfig }, + { .name =3D "mcp9985", .driver_data =3D (kernel_ulong_t)&mcp9985_chip_con= fig }, + { .name =3D "mcp9985d", .driver_data =3D (kernel_ulong_t)&mcp9985d_chip_c= onfig }, + { } +}; +MODULE_DEVICE_TABLE(i2c, mcp9982_id); + +static const struct of_device_id mcp9982_of_match[] =3D { + { + .compatible =3D "microchip,mcp9933", + .data =3D &mcp9933_chip_config, + }, { + .compatible =3D "microchip,mcp9933d", + .data =3D &mcp9933d_chip_config, + }, { + .compatible =3D "microchip,mcp9982", + .data =3D &mcp9982_chip_config, + }, { + .compatible =3D "microchip,mcp9982d", + .data =3D &mcp9982d_chip_config, + }, { + .compatible =3D "microchip,mcp9983", + .data =3D &mcp9983_chip_config, + }, { + .compatible =3D "microchip,mcp9983d", + .data =3D &mcp9983d_chip_config, + }, { + .compatible =3D "microchip,mcp9984", + .data =3D &mcp9984_chip_config, + }, { + .compatible =3D "microchip,mcp9984d", + .data =3D &mcp9984d_chip_config, + }, { + .compatible =3D "microchip,mcp9985", + .data =3D &mcp9985_chip_config, + }, { + .compatible =3D "microchip,mcp9985d", + .data =3D &mcp9985d_chip_config, + }, + { } +}; +MODULE_DEVICE_TABLE(of, mcp9982_of_match); + +static struct i2c_driver mcp9982_driver =3D { + .driver =3D { + .name =3D "mcp9982", + .of_match_table =3D mcp9982_of_match, + }, + .probe =3D mcp9982_probe, + .id_table =3D mcp9982_id, +}; +module_i2c_driver(mcp9982_driver); + +MODULE_AUTHOR("Victor Duicu "); +MODULE_DESCRIPTION("MCP998X/33 and MCP998XD/33D Multichannel Automotive Te= mperature Monitor Driver"); +MODULE_LICENSE("GPL"); --=20 2.51.0