From patchwork Thu Dec 7 19:44:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13483930 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VzUETL61" Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 004DE10F7; Thu, 7 Dec 2023 11:44:59 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a1e83adfe72so131606166b.1; Thu, 07 Dec 2023 11:44:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701978298; x=1702583098; 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=1EnS+pRSTO4R91TDbHHpG803uRr+ViRWAjg2k0SmyRQ=; b=VzUETL61rh/E1scygTuCbd8UETn+cDNFSoG7On5AbJyuDmi8r7WjC8Qbv/5KuyJM4Y Q5GKAkJahooM4dZqu11Um4n6jFmWzNNZHwlxB4unU9RtECJ1eOY8jkLSfmifGAvNccTT h8jdYgQttQ56ZqBaNEtNk3lXih2wEIy/ZyDoNMsQ0BNG7QV3vUwdbp7CPVqMgD63Pb26 8xGoSuD7ZrjhntJeIxZRcMkrRd+mOnDkxqEPcyAQvdaAi7x7XHcceNiCzjnTUOhv/TwB 8MFS9XbKJ9N1MwpFjL8EbUmHulDa7NBImqaE5x6Okr6jaeDHsAXpXP5E0vzTIScW6eWz Harw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701978298; x=1702583098; 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=1EnS+pRSTO4R91TDbHHpG803uRr+ViRWAjg2k0SmyRQ=; b=LPzQh5sfngUWhBWR/TAcSRSY9GFSBKNom1J6bl8Uls2VLBO6m9zQHnJJK5GpBIzV5k 3owaaWWicwwfhdz3Qdvjzmcm054vn22i6Rmz8qQ5pOm9F7Y5STXHuQi1el6uNFyApnJJ TUFa56IDHVS5eUyreKAltC4F+amqIByvVsGMFNZ47+0VQaMEcW6rJ5NWUwiMXVSfOkFK uRdvXN7yeMako9PwjoMg3NiRFMgr5NmddWNu5iKNnxsAbdsCDqwI00tLYHCVMWElzrjO vKIG6onkKDLEEYS5s+YTVQ/rQxSx9bSvXuHEpxoNRrrvwE74faYPPMiO2G8xqmQBPXP/ 8eZQ== X-Gm-Message-State: AOJu0YyMo8UaIUqEqonuXTuoTk8mGJMjEzOUgidLpHy60dDUzVXlOgza AlzfFoFMjooqsG2hwlbwTpUs/jLoaeW58g== X-Google-Smtp-Source: AGHT+IED03EnQ1EsA7gayURbpr7Ndp7aS+552NA4iDyL0Kh2YUGMD4fhTyP5uW3YdYFVmWXM0j2Vcg== X-Received: by 2002:a17:907:cb06:b0:a18:3001:6003 with SMTP id um6-20020a170907cb0600b00a1830016003mr1745883ejc.71.1701978297794; Thu, 07 Dec 2023 11:44:57 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1de9-f6cf-eef8-174f.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1de9:f6cf:eef8:174f]) by smtp.gmail.com with ESMTPSA id rr17-20020a170907899100b00a1d457954d6sm127364ejc.75.2023.12.07.11.44.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 11:44:57 -0800 (PST) From: Javier Carrasco Date: Thu, 07 Dec 2023 20:44:51 +0100 Subject: [PATCH v3 1/5] dt-bindings: vendor-prefixes: add Amphenol Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231020-topic-chipcap2-v3-1-5b3bb50a5f0b@gmail.com> References: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> In-Reply-To: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jean Delvare , Guenter Roeck , Jonathan Corbet , Liam Girdwood , Mark Brown Cc: Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Javier Carrasco , Krzysztof Kozlowski X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1701978294; l=874; i=javier.carrasco.cruz@gmail.com; s=20230509; h=from:subject:message-id; bh=PCidHchbaiTdlYu+9md1vtBsSrbu0tUQ7foLUj3NqoI=; b=7VBrQr3aL0lWdVkCZ996XknBABearyTwa/Etg4qigiFfnFLpHHFGKz9RO+9+Vj7lMgZOyR/WQ NQZSGgEx3rnDddXloOeQfozrgJDyeb1VBp4Crr+AemoQQN46A3OGG/d X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= Add vendor prefix for Amphenol (https://www.amphenol-sensors.com) Acked-by: Krzysztof Kozlowski Signed-off-by: Javier Carrasco --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index 309b94c328c8..4a49f24b906b 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -105,6 +105,8 @@ patternProperties: description: Amlogic, Inc. "^ampere,.*": description: Ampere Computing LLC + "^amphenol,.*": + description: Amphenol Advanced Sensors "^ampire,.*": description: Ampire Co., Ltd. "^ams,.*": From patchwork Thu Dec 7 19:44:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13483931 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c8Iq9OGS" Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13D8A1709; Thu, 7 Dec 2023 11:45:01 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-54cb4fa667bso1864259a12.3; Thu, 07 Dec 2023 11:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701978299; x=1702583099; 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=iR96KjiNOLwJsrZW9YjWEMZZO1SB3q3Gyovy8VvF6lw=; b=c8Iq9OGSDTXULqsFMiUWJ1ixM+dCdkmMijuC3xTRiggdwXJooBb0ETWsh5GdGQe95M CLkj6rZCuoz8fN21B5STx3nJiQ9Iu7En6aRF8OhffOvK4NbBxzf6JHEaCaJ4vWNeCt8+ 3FNhXsvbfbE1EPqSzm0mtKKUfMJKtIPyIg6rFtmql4dh9VcGBgt5ZoMn/e5X++uANZtK NDo3C0TLMNOrwKjDfmuVMsMlxvoQtURLJYlyDJN+xgS5hOkBpPO/1rsRNdA5NOkEipQ1 g7v+/TJWiSFY8gjp2M7uksCgLzJu1cm2ypz5j2vrujcxF5mQ/Tcq0IpC9GYAmNGKvPK/ wpxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701978299; x=1702583099; 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=iR96KjiNOLwJsrZW9YjWEMZZO1SB3q3Gyovy8VvF6lw=; b=c5BhCDDABSHLxxUZ84wGfE+MJJYwR6ArCnD8pIkN+mQoSd48MS/5ByVtVEN+VVRUmF X5U2cv6LJR+LyVg5B8nFC4lsyZmQwltvjNgFd6Z7VR7VZbYnfoOHm9OehOm+WKZk4Pe1 iwhiNwiBQ6UBfQ2+bi93u2o67p3xFSF4CXnhjmOXsFtLB6HaScIcumPM1rcCozDmIU9p zQ4RFCa0vXvLmv7+kyOeuJRfSuVniKzzW6dX2GY9GCUki0eaDJgLkK7bOxtgiJNK+nzm k/7/TgRdVUdUueghz6kQTAlwnJgzVqKA42T9y5F11QjlqmhYao1FUxNzjfTvKoMvD0r5 vsZA== X-Gm-Message-State: AOJu0YyvQsJdSbynHNWZwROKiv0+SL4pf8ieCEAc22V1KBarHbX4JMpv PhPhWwGsBBhOaKNJvGhqypXcUTBaALKUSA== X-Google-Smtp-Source: AGHT+IGGfg6FQkDeXgFVOZAAwsGykP8saS+dQgbd6tzsdn7cTkB8gmpMf11d4ZrPl2zNZU3SJZ39zg== X-Received: by 2002:a17:906:3f5b:b0:a10:8db4:bcf3 with SMTP id f27-20020a1709063f5b00b00a108db4bcf3mr2164697ejj.9.1701978299216; Thu, 07 Dec 2023 11:44:59 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1de9-f6cf-eef8-174f.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1de9:f6cf:eef8:174f]) by smtp.gmail.com with ESMTPSA id rr17-20020a170907899100b00a1d457954d6sm127364ejc.75.2023.12.07.11.44.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 11:44:58 -0800 (PST) From: Javier Carrasco Date: Thu, 07 Dec 2023 20:44:52 +0100 Subject: [PATCH v3 2/5] hwmon: (core) Add support for humidity min/max alarm Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231020-topic-chipcap2-v3-2-5b3bb50a5f0b@gmail.com> References: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> In-Reply-To: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jean Delvare , Guenter Roeck , Jonathan Corbet , Liam Girdwood , Mark Brown Cc: Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Javier Carrasco X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1701978294; l=2225; i=javier.carrasco.cruz@gmail.com; s=20230509; h=from:subject:message-id; bh=re14X9c0E+VBxIxbVskjRDUjU7zOxmCOyQzwnK834Ds=; b=6+7E7/19cnzN0IzDd0a0MN1feBqT8ruX2D3qeCJ1uUNbYkWz1JYXsq2M+Ky3CGJcDcyc1c2K3 dc1V6ZyWHOXBBg/Mw+a5UKLAbMVyel7oYNKn8OyNw0w6E3RqLJyiKSE X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= Add min_alarm and max_alarm attributes for humidityX to support devices that can generate these alarms. Such attributes already exist for other magnitudes such as tempX. Tested with a ChipCap 2 temperature-humidity sensor. Signed-off-by: Javier Carrasco --- drivers/hwmon/hwmon.c | 2 ++ include/linux/hwmon.h | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index c7dd3f5b2bd5..7f92984c37d9 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -579,8 +579,10 @@ static const char * const hwmon_humidity_attr_templates[] = { [hwmon_humidity_input] = "humidity%d_input", [hwmon_humidity_label] = "humidity%d_label", [hwmon_humidity_min] = "humidity%d_min", + [hwmon_humidity_min_alarm] = "humidity%d_min_alarm", [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", [hwmon_humidity_max] = "humidity%d_max", + [hwmon_humidity_max_alarm] = "humidity%d_max_alarm", [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", [hwmon_humidity_alarm] = "humidity%d_alarm", [hwmon_humidity_fault] = "humidity%d_fault", diff --git a/include/linux/hwmon.h b/include/linux/hwmon.h index 8cd6a6b33593..154de35e34ac 100644 --- a/include/linux/hwmon.h +++ b/include/linux/hwmon.h @@ -286,8 +286,10 @@ enum hwmon_humidity_attributes { hwmon_humidity_input, hwmon_humidity_label, hwmon_humidity_min, + hwmon_humidity_min_alarm, hwmon_humidity_min_hyst, hwmon_humidity_max, + hwmon_humidity_max_alarm, hwmon_humidity_max_hyst, hwmon_humidity_alarm, hwmon_humidity_fault, @@ -299,8 +301,10 @@ enum hwmon_humidity_attributes { #define HWMON_H_INPUT BIT(hwmon_humidity_input) #define HWMON_H_LABEL BIT(hwmon_humidity_label) #define HWMON_H_MIN BIT(hwmon_humidity_min) +#define HWMON_H_MIN_ALARM BIT(hwmon_humidity_min_alarm) #define HWMON_H_MIN_HYST BIT(hwmon_humidity_min_hyst) #define HWMON_H_MAX BIT(hwmon_humidity_max) +#define HWMON_H_MAX_ALARM BIT(hwmon_humidity_max_alarm) #define HWMON_H_MAX_HYST BIT(hwmon_humidity_max_hyst) #define HWMON_H_ALARM BIT(hwmon_humidity_alarm) #define HWMON_H_FAULT BIT(hwmon_humidity_fault) From patchwork Thu Dec 7 19:44:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13483932 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EqkdsZNo" Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 780CA1708; Thu, 7 Dec 2023 11:45:02 -0800 (PST) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-548ce39b101so1864740a12.2; Thu, 07 Dec 2023 11:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701978300; x=1702583100; 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=h+VcwsO3OlfK5Hp3uhRyspjZBcwCEAsB1Mu1l8CtFcY=; b=EqkdsZNolLBkcAgU+ENIEAqz7BomVsZDEp4iChIx4OffrT7YZPotlhfdFws65owU+k LVYjuG6kQVIuVP8P2RG+SSilX98J/htvMn1msMF+Vi4x4S4TAuqwGhorLBVEShZsLxkG NmRUwWFoZGsa1JtK6O3412YQ/g2X0txv4fWFjnKQHPLSRrJ//yCcIL75OxDymtP9rfle zRn8R7VPHBjstVK3gn63DtSyhvWSeIrDGr4SjjSMER8s+ODLfSJX0joqbOT2nOcMrP/+ pPlik843n295Bj6ke+OvInToTcKipTWIz/phgcxlpdkyUjVhCVI/cls8Cy9va3yCvag1 NbGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701978300; x=1702583100; 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=h+VcwsO3OlfK5Hp3uhRyspjZBcwCEAsB1Mu1l8CtFcY=; b=kWw2EPT4NENHnvW+xuXZWYiCxetKj2y4jJRFX8ByPoLCO30mtYFTyPeygFDdtCaV/O fhVDZg0bXKW2+dvoqqV0LCKo/I+ZP5GnBCrj1omD0nGSIf1DxqV41gDEVgDNCuJBIxfi yLdY6x3quscIMvbRYk/lXIVs05tvXixU+fdH4Or/CjY3re2wQBszcWUEZ7+7p8e3KiNr qpaA18xOMfK7TKZr7hvvATk8prGzSq6gmcAJT+XOJFMUjh0RAxrru5COxlloMrHH0Qwo YOFOdQ9VtVojWIBgeQ1udNSQgBoZj6l/UfTD+1VH712WManLnQ14/1/J+jln6Q6CM8md +Uwg== X-Gm-Message-State: AOJu0YzrfSt5AqXL4GT4yZ8kxHpolQvnzwnUGpGEz5x8sSDt4twEkyf/ CUpo4EoLwwuZqYohCzckdaym8JpVSTeOUw== X-Google-Smtp-Source: AGHT+IGKqzKpbIsnCksG/kahaPc7nbc7oh4bA1CZPfeqWcQmKywyzdjLJZlOpqSMbA2xBTvpWim2FA== X-Received: by 2002:a17:906:260c:b0:a1a:c370:2218 with SMTP id h12-20020a170906260c00b00a1ac3702218mr1344265ejc.83.1701978300554; Thu, 07 Dec 2023 11:45:00 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1de9-f6cf-eef8-174f.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1de9:f6cf:eef8:174f]) by smtp.gmail.com with ESMTPSA id rr17-20020a170907899100b00a1d457954d6sm127364ejc.75.2023.12.07.11.44.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 11:45:00 -0800 (PST) From: Javier Carrasco Date: Thu, 07 Dec 2023 20:44:53 +0100 Subject: [PATCH v3 3/5] ABI: sysfs-class-hwmon: add descriptions for humidity min/max alarms Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231020-topic-chipcap2-v3-3-5b3bb50a5f0b@gmail.com> References: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> In-Reply-To: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jean Delvare , Guenter Roeck , Jonathan Corbet , Liam Girdwood , Mark Brown Cc: Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Javier Carrasco X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1701978294; l=1006; i=javier.carrasco.cruz@gmail.com; s=20230509; h=from:subject:message-id; bh=THjML2ZdEMhAgD4H3dSikGyikzlsdusSvlgcKoBaOeo=; b=mEOH75y2ZLTNzWqqh+yZ+o74f3Zs3fe6EWnMyfEZKbLj572fs8BukBH8sYdOTYzvswIs5SMpi +HMkiDQIrgxDwykgp6SM6FxejNGGJqCvSS+FILOhYajOOvRc8bgqv9r X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= This attributes have been recently introduced and require the corresponding ABI documentation. Signed-off-by: Javier Carrasco --- Documentation/ABI/testing/sysfs-class-hwmon | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-class-hwmon b/Documentation/ABI/testing/sysfs-class-hwmon index 638f4c6d4ec7..af94c1f373de 100644 --- a/Documentation/ABI/testing/sysfs-class-hwmon +++ b/Documentation/ABI/testing/sysfs-class-hwmon @@ -908,6 +908,24 @@ Description: RW +What: /sys/class/hwmon/hwmonX/humidityY_min_alarm +Description: + Minimum humidity detection + + - 0: OK + - 1: Minimum humidity detected + + RO + +What: /sys/class/hwmon/hwmonX/humidityY_max_alarm +Description: + Maximum humidity detection + + - 0: OK + - 1: Maximum humidity detected + + RO + What: /sys/class/hwmon/hwmonX/humidityY_rated_min Description: Minimum rated humidity. From patchwork Thu Dec 7 19:44:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13483933 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F56+RECo" Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17A5C170C; Thu, 7 Dec 2023 11:45:04 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-a00cbb83c80so151848966b.0; Thu, 07 Dec 2023 11:45:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701978302; x=1702583102; 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=BQIFVhJJWVMErSy2LBehuPtfd+ssOO9PI0vGzyJlWu4=; b=F56+RECoKagw/r+XWvLKY0gaKf7BdWL9C4dtKbVePboZaiUGYsMRFVfjAz8gsSCXPy uz/ymucwMOIGLYJuF3AoE0p/Z7vO1opR8hOgTFnzPQumZCi7fKbHQ1pP2CP69V1XOJcj y0iAFHEBxb5vsMhgdgbOkhHer1fY1NciGxlj6l1c+kyUCwR8iMWaRsBF2GdakJHOlEMS d2WYwfspLKGBjwhuIsoDnyzDJyGvLHNRePhEn7JNOPjfmel4YJTVagNqFvPXrm7fmLNJ UVWgi/w3rw8WHSLDUaK+QxsC+1l+uZ91Exf4ocA1SqWPbQMBXODXHD5i9Uro4N0YSry+ uoIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701978302; x=1702583102; 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=BQIFVhJJWVMErSy2LBehuPtfd+ssOO9PI0vGzyJlWu4=; b=ak56pwpShsgobR4RD5OxMZa5cWT6djjyasNzyFQD7+B02SgI9m+PPoUngeAtiCZtB9 LvYZnoRQb08zD4FC0/zDG1JhXBrUMoWaKe0vHvOQtPdrJU1t4YP6nsFq5g2N9D/pZlnS FH3SzNIF9pK66QLjzMQjoT7ZXBsOoG516Wh9Yt3NhfD0n3No9gVO4XcQxQrRu4Z7byM9 saozDEZGhIiFTP6LKVBfxoIuCw75E5Gn9vL55mHMgObJ/X98QDyTESRrqni513MuCceX S2kH9zgvKtvjnWE2SrXZXzaFUf4aZTHU4ADrVdCKPOz+byzcMSArR/qXEpth5pwqXsK0 1hBQ== X-Gm-Message-State: AOJu0YyCLFjkjaaRwxwMRiGcdkDoIRszsoe5CEZazCZfRpPfD4PmQYJ+ o41lS4UAHHYyJVbu448sbI83t+z4R6ZSBA== X-Google-Smtp-Source: AGHT+IFbkE9v9gKKM3CEmpySv54v5+3W+2J8h90C0bMqbBxR6BDm9S8HT5gAj2+nIcnH5NjTdkm+2g== X-Received: by 2002:a17:906:9e06:b0:a19:85ae:e0c3 with SMTP id fp6-20020a1709069e0600b00a1985aee0c3mr1579412ejc.35.1701978302066; Thu, 07 Dec 2023 11:45:02 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1de9-f6cf-eef8-174f.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1de9:f6cf:eef8:174f]) by smtp.gmail.com with ESMTPSA id rr17-20020a170907899100b00a1d457954d6sm127364ejc.75.2023.12.07.11.45.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 11:45:01 -0800 (PST) From: Javier Carrasco Date: Thu, 07 Dec 2023 20:44:54 +0100 Subject: [PATCH v3 4/5] dt-bindings: hwmon: Add Amphenol ChipCap 2 Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231020-topic-chipcap2-v3-4-5b3bb50a5f0b@gmail.com> References: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> In-Reply-To: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jean Delvare , Guenter Roeck , Jonathan Corbet , Liam Girdwood , Mark Brown Cc: Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Javier Carrasco X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1701978294; l=2523; i=javier.carrasco.cruz@gmail.com; s=20230509; h=from:subject:message-id; bh=C++NOYO1W92ZUhE0C/w4m3kdvpKV1mYW6ndjBCwmz50=; b=dQ7l6Yyz3XJiqpb2y/gNyCRYElUSUMIC8aA+B3mLEa3xk2fem9Je6PzFq4IW7eI1qePr4ywWW vq8ex8bzDseCe5CQ7CE8ftWbpPD9LyJ0Pl1nErJgteODoBPcA+9Khgo X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= Add device tree bindings and an example for the ChipCap 2 humidity and temperature sensor. Signed-off-by: Javier Carrasco Reviewed-by: Conor Dooley --- .../bindings/hwmon/amphenol,chipcap2.yaml | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Documentation/devicetree/bindings/hwmon/amphenol,chipcap2.yaml b/Documentation/devicetree/bindings/hwmon/amphenol,chipcap2.yaml new file mode 100644 index 000000000000..b83bbc2719ff --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/amphenol,chipcap2.yaml @@ -0,0 +1,74 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/hwmon/amphenol,chipcap2.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ChipCap 2 humidity and temperature iio sensor + +maintainers: + - Javier Carrasco + +description: | + Relative humidity and temperature sensor on I2C bus. + + Datasheets: + https://www.amphenol-sensors.com/en/telaire/humidity/527-humidity-sensors/3095-chipcap-2 + +properties: + compatible: + enum: + - amphenol,cc2d23 + - amphenol,cc2d23s + - amphenol,cc2d25 + - amphenol,cc2d25s + - amphenol,cc2d33 + - amphenol,cc2d33s + - amphenol,cc2d35 + - amphenol,cc2d35s + + reg: + maxItems: 1 + + interrupts: + items: + - description: measurement ready indicator + - description: low humidity alarm + - description: high humidity alarm + + interrupt-names: + items: + - const: ready + - const: low + - const: high + + vdd-supply: + description: + Dedicated, controllable supply-regulator to reset the device and + enter in command mode. + If not defined, no alarms will be available. + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + #include + i2c { + #address-cells = <1>; + #size-cells = <0>; + + humidity@28 { + compatible = "amphenol,cc2d23s"; + reg = <0x28>; + interrupt-parent = <&gpio>; + interrupts = <4 IRQ_TYPE_EDGE_RISING>, + <5 IRQ_TYPE_EDGE_RISING>, + <6 IRQ_TYPE_EDGE_RISING>; + interrupt-names = "ready", "low", "high"; + vdd-supply = <®_vdd>; + }; + }; From patchwork Thu Dec 7 19:44:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 13483934 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CxoHHOm1" Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D15731709; Thu, 7 Dec 2023 11:45:05 -0800 (PST) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-9e1021dbd28so175746566b.3; Thu, 07 Dec 2023 11:45:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701978304; x=1702583104; 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=YG7VsL3J9QFVr4dF5/pC07IDiEA7ZKy8wIX2cT6cZkA=; b=CxoHHOm1y4SyYkNPc+pZGTKSTSihRvGNTedi9jgKtCukVt/gCU86ymP8bzxxm0H/b9 pw3dyOFQU2tbNHYlnCyYCyJM/jDrpkSysc1YJ0beBItTdEBHoAtW90nCsTazNhOX65Us gtwpor+CSrs0g05XkTj2k8mcWlxt2XK6Fqif9ZTGrSXIc9BghbSXSLRJU+aUKxVmUiWY doQh8Iq+iutPJeCp2OwcQkFUgxWg3k33YLKbPAH9EqkQ+NYgM21WLUKC8fJo1gmLlyZ5 /qmprEvZ+nSiUZAjJ6xQUAeu38y6DGBg5iskyNKgPxpx5TezBitXZ76gb8w+ldk2cVl4 ZYhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701978304; x=1702583104; 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=YG7VsL3J9QFVr4dF5/pC07IDiEA7ZKy8wIX2cT6cZkA=; b=XE2W45Nsu4wryPE9eq/hDsFLNdVCJ/IiXI6sS6OhyBr6eL/mWV12IDnKmjXmBHG7Tf Lw8rVgIjtUrIyJOv9bGXKuuFr3J0T0LaQAL7KgG4gjI3JXhp/UMltlpTGZiTWpAbqFh3 6zpFIPHM7ODEhfEmUFAwc/sEwtlx7yCFF3bI0MsLHr0dE+Yr3+jOD8h2EYd4zrs5dRY4 bU2NHwUbSf8SFADe/cHDxD36ZOZbdh8fwBRQQjDviGFrNtpxcvWHEk/4KkzcSevLIo1S L80gWzQTpGHmpqkfN9DgYAcKafHpEziDqdzjM8baD/FwsxkHNGPe9/TNhQXT04EWut6r SmKQ== X-Gm-Message-State: AOJu0YzGp+eY8BSHlcEO7uXU4F7DybBoOFqlDmG6WaPZ240Siv+igGQn FDaqfW4wG+vPtULQJDlDZA/QM8rZH9QeWw== X-Google-Smtp-Source: AGHT+IHlTNdpnfVBGtNYEZQwewPdqqqe7QuyfF02bLnD5sOaM0Feix6P0G0uP9jXKj8pN50OuNdxeQ== X-Received: by 2002:a17:907:ca1d:b0:a1d:67c:6bd6 with SMTP id uk29-20020a170907ca1d00b00a1d067c6bd6mr1668133ejc.115.1701978303594; Thu, 07 Dec 2023 11:45:03 -0800 (PST) Received: from [127.0.1.1] (2a02-8389-41cf-e200-1de9-f6cf-eef8-174f.cable.dynamic.v6.surfer.at. [2a02:8389:41cf:e200:1de9:f6cf:eef8:174f]) by smtp.gmail.com with ESMTPSA id rr17-20020a170907899100b00a1d457954d6sm127364ejc.75.2023.12.07.11.45.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 11:45:03 -0800 (PST) From: Javier Carrasco Date: Thu, 07 Dec 2023 20:44:55 +0100 Subject: [PATCH v3 5/5] hwmon: Add support for Amphenol ChipCap 2 Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231020-topic-chipcap2-v3-5-5b3bb50a5f0b@gmail.com> References: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> In-Reply-To: <20231020-topic-chipcap2-v3-0-5b3bb50a5f0b@gmail.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jean Delvare , Guenter Roeck , Jonathan Corbet , Liam Girdwood , Mark Brown Cc: Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org, Javier Carrasco X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1701978294; l=36561; i=javier.carrasco.cruz@gmail.com; s=20230509; h=from:subject:message-id; bh=rD61Med5KtdfpnlLHIHQn9jDX7Vmx+ZnsGuqT/sgxfA=; b=vhMLkJ9+UAJ6ijW52cqzOFLCgEMnr0P2Mt6h7Ttm5XMlkXqgoMOIRIzvOjUGqV7ktMsZdOa9l t0l61KY+YrFCb63Why72FMUzq8f6Ktye8lnjqniYkJAhpi83qpGfYPZ X-Developer-Key: i=javier.carrasco.cruz@gmail.com; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= The Amphenol ChipCap 2 is a capacitive polymer humidity and temperature sensor with an integrated EEPROM and minimum/maximum humidity alarms. All device variants offer an I2C interface and depending on the part number, two different output modes: - CC2D: digital output - CC2A: analog (PDM) output This driver adds support for the digital variant (CC2D part numbers), which is also divided into two subfamilies [1]: - non-sleep measurement mode (CC2D23, CC2D25, CC2D33, CC2D35) - sleep measurement mode (CC2D23S, CC2D25S, CC2D33S, CC2D35S) The only difference between them is that the non-sleep parts carry out measurements periodically, whereas the sleep counterparts require the user to trigger each measurement, staying in sleep mode otherwise to increase power savings. The Chipcap 2 EEPROM can be accessed to configure a series of parameters like the minimum/maximum humidity alarm threshold and hysteresis. The EEPROM is only accessible in the command window after a power-on reset. The default window lasts 10 ms if no Start_CM command is sent. After the command window is finished (either after the mentioned timeout of after a Start_NOM command is sent), the device enters the normal operation mode and makes a first measurement automatically. Unfortunately, the device does not provide any hardware or software reset and therefore the driver must trigger power cycles to enter the command mode. An external regulator is required for that and if it is not available, the driver cannot configure the alarms and it must limit the device operation to humidity and temperature measurements. Given the high cost of accessing the command mode, an EEPROM read is made in the probe function and the alarm parameters are stored for further readings, only accessing the command mode again to overwrite those parameters. The measurement process makes use of the ready signal if available, using maximum delay values from the datasheet otherwise. In non-sleep mode, a single "data fetch" command is required to read the measurements and no trigger is required. Sleep devices require an additional "measurement request" command to trigger the measurement, followed by a data fetch to retrieve the measured values. In both cases the ready signal goes high when valid data can be fetched. The ready signal stays high until the data is retrieved even if further measurements are made. The minimum and maximum humidity alarms can be configured with two registers per alarm: one stores the alarm threshold and the other one keeps the value that turns off the alarm. Note that the second register stores an absolute value and not the hysteresis. The hysteresis is therefore obtained as a subtraction of the two registers. The alarm signals are only updated when a measurement is carried out. Sleep mode devices will not trigger their alarms until a measurement is requested. This driver has been tested with a sleep device and the non-sleep mechanism has been inferred from the common behavior after power-on reset explained above, where a first measurement is made without user intervention. Therefore there is no guarantee that no adjustments in the measurement function will be required if the driver ever controls a non-sleep device. [1] Note that the measurement mode is programmed in the manufacturing process. The datasheet mentions the name of the register and exact bit to set the measurement mode, but no addresses are provided and according to the manufacturer, it is not modifiable by the user. Signed-off-by: Javier Carrasco --- Documentation/hwmon/chipcap2.rst | 73 +++ Documentation/hwmon/index.rst | 1 + MAINTAINERS | 8 + drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/chipcap2.c | 1040 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 1133 insertions(+) diff --git a/Documentation/hwmon/chipcap2.rst b/Documentation/hwmon/chipcap2.rst new file mode 100644 index 000000000000..35a968aaac4e --- /dev/null +++ b/Documentation/hwmon/chipcap2.rst @@ -0,0 +1,73 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Kernel driver ChipCap2 +====================== + +Supported chips: + + * Amphenol CC2D23, CC2D23S, CC2D25, CC2D25S, CC2D33, CC2D33S, CC2D35, CC2D35S + + Prefix: 'chipcap2' + + Addresses scanned: - + + Datasheet: https://www.amphenol-sensors.com/en/telaire/humidity/527-humidity-sensors/3095-chipcap-2 + +Author: + + - Javier Carrasco + +Description +----------- + +This driver implements support for the Amphenol ChipCap 2 chips, a humidity +and temperature family. Temperature is measured in milli degrees celsius, +relative humidity is expressed as a per cent mille. The measurement ranges +are the following: + + - Relative humidity: 0 to 100000 pcm (14-bit resolution) + - Temperature: -40000 to +125000 m°C (14-bit resolution) + +The device communicates with the I2C protocol and uses the I2C address 0x28 +by default. + +Depending on the hardware configuration, up to two humidity alarms to control +minimum and maximum values are provided. Their thresholds and hystersis can be +configured via sysfs. + +Thresholds and hysteris must be provided as a per cent mille. These values +might be truncated to match the 14-bit device resolution (6.1 pcm/LSB) + +Known Issues +------------ + +The driver does not support I2C address and command window length modification. + +sysfs-Interface +--------------- + +The following list includes the sysfs attributes that the driver always provides, +their permissions and a short description: + +=============================== ======= ======================================== +Name Perm Description +=============================== ======= ======================================== +temp1_input: RO temperature input +humidity1_input: RO humidity input +=============================== ======= ======================================== + +The following list includes the sysfs attributes that the driver may provide +depending on the hardware configuration: + +=============================== ======= ======================================== +Name Perm Description +=============================== ======= ======================================== +humidity1_min: RW humidity low limit. Measurements under + this limit trigger a humidity low alarm +humidity1_max: RW humidity high limit. Measurements above + this limit trigger a humidity high alarm +humidity1_min_hyst: RW humidity low hystersis +humidity1_max_hyst: RW humidity high hystersis +humidity1_min_alarm: RO humidity low alarm indicator +humidity1_max_alarm: RO humidity high alarm indicator +=============================== ======= ======================================== diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst index 72f4e6065bae..35f7573b4822 100644 --- a/Documentation/hwmon/index.rst +++ b/Documentation/hwmon/index.rst @@ -51,6 +51,7 @@ Hardware Monitoring Kernel Drivers bel-pfe bpa-rs600 bt1-pvt + chipcap2 coretemp corsair-cpro corsair-psu diff --git a/MAINTAINERS b/MAINTAINERS index 788be9ab5b73..88eec0adec49 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1086,6 +1086,14 @@ F: Documentation/devicetree/bindings/perf/amlogic,g12-ddr-pmu.yaml F: drivers/perf/amlogic/ F: include/soc/amlogic/ +AMPHENOL CHIPCAP 2 HUMIDITY-TEMPERATURE IIO DRIVER +M: Javier Carrasco +L: linux-hwmon@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/hwmon/amphenol,chipcap2.yaml +F: Documentation/hwmon/chipcap2.rst +F: drivers/hwmon/chipcap2.c + AMPHION VPU CODEC V4L2 DRIVER M: Ming Qian M: Zhou Peng diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index cf27523eed5a..11b133ef2384 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -452,6 +452,16 @@ config SENSORS_BT1_PVT_ALARMS the data conversion will be periodically performed and the data will be saved in the internal driver cache. +config SENSORS_CHIPCAP2 + tristate "Amphenol ChipCap 2 relative humidity and temperature sensor" + depends on I2C + help + Say yes here to build support for the Amphenol ChipCap 2 + relative humidity and temperature sensor. + + To compile this driver as a module, choose M here: the module + will be called chipcap2. + config SENSORS_CORSAIR_CPRO tristate "Corsair Commander Pro controller" depends on HID diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index e84bd9685b5c..daf0acef1482 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -58,6 +58,7 @@ obj-$(CONFIG_SENSORS_ASPEED) += aspeed-pwm-tacho.o obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o obj-$(CONFIG_SENSORS_AXI_FAN_CONTROL) += axi-fan-control.o obj-$(CONFIG_SENSORS_BT1_PVT) += bt1-pvt.o +obj-$(CONFIG_SENSORS_CHIPCAP2) += chipcap2.o obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o obj-$(CONFIG_SENSORS_CORSAIR_CPRO) += corsair-cpro.o obj-$(CONFIG_SENSORS_CORSAIR_PSU) += corsair-psu.o diff --git a/drivers/hwmon/chipcap2.c b/drivers/hwmon/chipcap2.c new file mode 100644 index 000000000000..d5f8bce2b921 --- /dev/null +++ b/drivers/hwmon/chipcap2.c @@ -0,0 +1,1040 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * cc2.c - Support for the Amphenol ChipCap 2 relative humidity, temperature sensor + * + * Part numbers supported: + * CC2D23, CC2D23S, CC2D25, CC2D25S, CC2D33, CC2D33S, CC2D35, CC2D35S + * + * Author: Javier Carrasco + * + * Datasheet and application notes: + * https://www.amphenol-sensors.com/en/telaire/humidity/527-humidity-sensors/3095-chipcap-2 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CC2_DEV_NAME "chipcap2" + +#define CC2_START_CM 0xA0 +#define CC2_START_NOM 0x80 +#define CC2_R_ALARM_HIGH_ON 0x18 +#define CC2_R_ALARM_HIGH_OFF 0x19 +#define CC2_R_ALARM_LOW_ON 0x1A +#define CC2_R_ALARM_LOW_OFF 0x1B +#define CC2_RW_OFFSET 0x40 +#define CC2_W_ALARM_HIGH_ON (CC2_R_ALARM_HIGH_ON + CC2_RW_OFFSET) +#define CC2_W_ALARM_HIGH_OFF (CC2_R_ALARM_HIGH_OFF + CC2_RW_OFFSET) +#define CC2_W_ALARM_LOW_ON (CC2_R_ALARM_LOW_ON + CC2_RW_OFFSET) +#define CC2_W_ALARM_LOW_OFF (CC2_R_ALARM_LOW_OFF + CC2_RW_OFFSET) + +#define CC2_STATUS_FIELD GENMASK(7, 6) +#define CC2_STATUS_VALID_DATA 0x00 +#define CC2_STATUS_STALE_DATA 0x01 +#define CC2_STATUS_CMD_MODE 0x02 + +#define CC2_RESPONSE_FIELD GENMASK(1, 0) +#define CC2_RESPONSE_BUSY 0x00 +#define CC2_RESPONSE_ACK 0x01 +#define CC2_RESPONSE_NACK 0x02 + +#define CC2_ERR_CORR_EEPROM BIT(2) +#define CC2_ERR_UNCORR_EEPROM BIT(3) +#define CC2_ERR_RAM_PARITY BIT(4) +#define CC2_ERR_CONFIG_LOAD BIT(5) + +#define CC2_EEPROM_SIZE 10 +#define CC2_EEPROM_DATA_LEN 3 +#define CC2_MEASUREMENT_DATA_LEN 4 + +#define CC2_RH_DATA_FIELD GENMASK(13, 0) + +#define CC2_POWER_CYCLE_MS 150 +#define CC2_RESP_SLEEP_MS 45 +#define CC2_RESP_START_CM_US 100 +#define CC2_RESP_EEPROM_R_US 100 +#define CC2_RESP_EEPROM_W_MS 12 +#define CC2_STARTUP_TIME_US 1250 +#define CC2_UPDATE_RATE_MS 165 + +#define CC2_RH_MIN 0 +#define CC2_RH_MAX (100 * 1000U) + +struct cc2_data; + +enum cc2_ids { cc2d23, cc2d23s }; + +struct cc2_config { + int (*measurement)(struct cc2_data *data, + enum hwmon_sensor_types type, + long *val); +}; + +struct cc2_rh_alarm_info { + long min; + long min_hyst; + long max; + long max_hyst; + bool low_alarm; + bool high_alarm; + bool low_alarm_visible; + bool high_alarm_visible; +}; + +struct cc2_data { + const struct cc2_config *config; + struct cc2_rh_alarm_info rh_alarm; + struct mutex alarm_lock; /* alarm status lock */ + struct completion complete; + struct device *hwmon; + struct i2c_client *client; + struct mutex i2c_lock; /* i2c communication lock */ + struct regulator *regulator; + const char *name; + int irq_ready; + int irq_low; + int irq_high; + bool ignore_irqs; +}; + +enum cc2_chan_addr { + CC2_CHAN_TEMP = 0, + CC2_CHAN_HUMIDITY, +}; + +/* %RH as a per cent mille from a register value */ +static long cc2_rh_convert(u16 data) +{ + unsigned long tmp = (data & CC2_RH_DATA_FIELD) * CC2_RH_MAX; + + return tmp / ((1 << 14) - 1); +} + +/* convert %RH to a register value */ +static u16 cc2_rh_to_reg(long data) +{ + return data * ((1 << 14) - 1) / CC2_RH_MAX; +} + +/* round humidity values to the device resolution (14 bits) */ +static long cc2_rh_to_device_resolution(long data) +{ + u16 reg_val; + + reg_val = cc2_rh_to_reg(data); + + return cc2_rh_convert(reg_val); +} + +/* temperature in milli degrees celsius from a register value */ +static long cc2_temp_convert(u16 data) +{ + unsigned long tmp = ((data >> 2) * 165 * 1000U) / ((1 << 14) - 1); + + return tmp - 40 * 1000U; +} + +static int cc2_cmd_response_diagnostic(struct device *dev, u8 status) +{ + int resp; + + if (FIELD_GET(CC2_STATUS_FIELD, status) != CC2_STATUS_CMD_MODE) { + dev_dbg(dev, "Command sent out of command window\n"); + return -ETIMEDOUT; + } + + resp = FIELD_GET(CC2_RESPONSE_FIELD, status); + switch (resp) { + case CC2_RESPONSE_ACK: + return 0; + case CC2_RESPONSE_BUSY: + return -EBUSY; + case CC2_RESPONSE_NACK: + if (resp & CC2_ERR_CORR_EEPROM) + dev_dbg(dev, "Command failed: corrected EEPROM\n"); + if (resp & CC2_ERR_UNCORR_EEPROM) + dev_dbg(dev, "Command failed: uncorrected EEPROM\n"); + if (resp & CC2_ERR_RAM_PARITY) + dev_dbg(dev, "Command failed: RAM parity\n"); + if (resp & CC2_ERR_RAM_PARITY) + dev_dbg(dev, "Command failed: configuration error\n"); + return -ENODATA; + default: + dev_dbg(dev, "Unknown command reply\n"); + return -EINVAL; + } +} + +static int cc2_read_command_status(struct i2c_client *client) +{ + u8 status; + int ret; + + ret = i2c_master_recv(client, &status, 1); + if (ret != 1) { + ret = ret < 0 ? ret : -EIO; + return ret; + } + + return cc2_cmd_response_diagnostic(&client->dev, status); +} + +static int cc2_clear_data_ready(struct i2c_client *client) +{ + u8 data[CC2_MEASUREMENT_DATA_LEN]; + u8 status; + int ret; + + ret = i2c_master_recv(client, data, CC2_MEASUREMENT_DATA_LEN); + if (ret != CC2_MEASUREMENT_DATA_LEN) { + ret = ret < 0 ? ret : -EIO; + return ret; + } + status = FIELD_GET(CC2_STATUS_FIELD, data[0]); + if (status == CC2_STATUS_STALE_DATA) + return -EBUSY; + + if (status != CC2_STATUS_VALID_DATA) + return -EIO; + + return 0; +} + +/* + * The command mode is only accessible after sending the START_CM command in the + * first 10 ms after power-up. + */ +static int cc2_command_mode_start(struct cc2_data *data) +{ + unsigned long timeout; + int ret; + + ret = i2c_smbus_write_word_data(data->client, CC2_START_CM, 0); + if (ret < 0) + return ret; + + if (data->irq_ready > 0) { + timeout = usecs_to_jiffies(2 * CC2_RESP_START_CM_US); + ret = wait_for_completion_timeout(&data->complete, timeout); + if (!ret) + return -ETIMEDOUT; + } else { + usleep_range(CC2_RESP_START_CM_US, 2 * CC2_RESP_START_CM_US); + } + + return cc2_read_command_status(data->client); +} + +/* Sending a Start_NOM command finishes the command mode immediately with no + * reply and the device enters normal operation mode + */ +static int cc2_command_mode_finish(struct cc2_data *data) +{ + int ret; + + ret = i2c_smbus_write_word_data(data->client, CC2_START_NOM, 0); + if (ret < 0) + return ret; + + return 0; +} + +/* EEPROM registers can only be read in command mode */ +static int cc2_read_eeprom_reg(struct cc2_data *data, u8 cmd, u16 *val) +{ + u8 buf[CC2_EEPROM_DATA_LEN]; + unsigned long timeout; + int ret; + + ret = i2c_smbus_write_word_data(data->client, cmd, 0); + if (ret < 0) + return ret; + + if (data->irq_ready > 0) { + timeout = usecs_to_jiffies(2 * CC2_RESP_EEPROM_R_US); + ret = wait_for_completion_timeout(&data->complete, timeout); + if (!ret) + return -ETIMEDOUT; + } else { + usleep_range(CC2_RESP_EEPROM_R_US, CC2_RESP_EEPROM_R_US + 10); + } + ret = i2c_master_recv(data->client, buf, CC2_EEPROM_DATA_LEN); + if (ret != CC2_EEPROM_DATA_LEN) { + ret = ret < 0 ? ret : -EIO; + return ret; + } + + *val = be16_to_cpup((__be16 *)&buf[1]); + + return cc2_read_command_status(data->client); +} + +static int cc2_write_eeprom_reg(struct cc2_data *data, u8 cmd, u16 val) +{ + unsigned long timeout; + int ret; + + cpu_to_be16s(&val); + ret = i2c_smbus_write_word_data(data->client, cmd, val); + if (ret < 0) + return ret; + + if (data->irq_ready > 0) { + timeout = msecs_to_jiffies(2 * CC2_RESP_EEPROM_W_MS); + ret = wait_for_completion_timeout(&data->complete, timeout); + if (!ret) + return -ETIMEDOUT; + } else { + msleep(CC2_RESP_EEPROM_W_MS); + } + + return cc2_read_command_status(data->client); +} + +/* Command mode is only accessible in the first 10 ms after power-up, but the + * device does not provide any kind of reset. In order to access the command + * mode during normal operation, a power cycle must be triggered. + */ +static int cc2_start_command_window(struct cc2_data *data) +{ + int ret; + + /* ignore alarms triggered by voltage toggling during the power cycle */ + mutex_lock(&data->alarm_lock); + data->ignore_irqs = true; + mutex_unlock(&data->alarm_lock); + + /* clear any pending completion */ + try_wait_for_completion(&data->complete); + + if (regulator_is_enabled(data->regulator)) { + ret = regulator_disable(data->regulator); + if (ret < 0) + return ret; + + msleep(CC2_POWER_CYCLE_MS); /* ensure a clean power cycle */ + } + + ret = regulator_enable(data->regulator); + if (ret < 0) + return ret; + + /* + * TODO: the startup-delay-us property of the regulator might be + * added to the delay (if provided). + * Currently there is no interface to read its value apart from + * a direct access to regulator->rdev->constraints->enable_time, + * which is discouraged like any direct access to the regulator_dev + * structure. This would be relevant in cases where the startup delay + * is in the range of milliseconds. + */ + usleep_range(CC2_STARTUP_TIME_US, CC2_STARTUP_TIME_US + 125); + + mutex_lock(&data->alarm_lock); + data->ignore_irqs = false; + mutex_unlock(&data->alarm_lock); + + return 0; +} + +/* + * An EEPROM update requires the following steps: + * 1. Power cycle to accept the START_CM command. + * 2. STARTM_CM command to enter command mode. + * 3. Command(s) to update register(s). + * 4. START_NOM command to exit command mode. + * After that, the device switches to normal mode and a measurement is + * triggered automatically. In order to obtain a recent measurement on demand, + * the automatic measurement is dropped. + */ +static int cc2_update_eeprom(struct cc2_data *data, u8 *cmd, u16 *val, int len) +{ + int i, ret; + + if (!cmd || !val || len > CC2_EEPROM_SIZE) + return -EINVAL; + + ret = cc2_start_command_window(data); + if (ret < 0) + return ret; + + mutex_lock(&data->i2c_lock); + ret = cc2_command_mode_start(data); + if (ret < 0) + goto unlock; + + for (i = 0; i < len; i++) { + ret = cc2_write_eeprom_reg(data, *cmd, *val); + if (ret < 0) { + dev_dbg(&data->client->dev, + "Failed to write register %#02x\n", *cmd); + goto cmd_finish; + } + cmd++; + val++; + } + +cmd_finish: + ret = cc2_command_mode_finish(data); +unlock: + mutex_unlock(&data->i2c_lock); + + return ret; +} + +/* + * EEPROM access is a highly time-consuming task that involves a power cycle. + * The alarm-related registers are read once and their values are stored and + * updated without further EEPROM accesses. + * Probe deferring is used to minimize the risk of missing the command window + * if the task is waiting to be executed for too long. + */ +static int cc2_read_eeprom_alarm_registers(struct cc2_data *data) +{ + u16 val; + int ret; + + mutex_lock(&data->i2c_lock); + ret = cc2_command_mode_start(data); + if (ret < 0) { + ret = (ret == -ETIMEDOUT) ? -EPROBE_DEFER : ret; + goto unlock; + } + + ret = cc2_read_eeprom_reg(data, CC2_R_ALARM_LOW_ON, &val); + if (ret < 0) + goto unlock; + + data->rh_alarm.min = cc2_rh_convert(val); + + ret = cc2_read_eeprom_reg(data, CC2_R_ALARM_LOW_OFF, &val); + if (ret < 0) + goto unlock; + + data->rh_alarm.min_hyst = cc2_rh_convert(val); + + ret = cc2_read_eeprom_reg(data, CC2_R_ALARM_HIGH_ON, &val); + if (ret < 0) + goto unlock; + + data->rh_alarm.max = cc2_rh_convert(val); + + ret = cc2_read_eeprom_reg(data, CC2_R_ALARM_HIGH_OFF, &val); + if (ret < 0) + goto unlock; + + data->rh_alarm.max_hyst = cc2_rh_convert(val); + + ret = cc2_command_mode_finish(data); + +unlock: + mutex_unlock(&data->i2c_lock); + + return ret; +} + +static int cc2_retrieve_alarm_config(struct cc2_data *data) +{ + int ret; + + ret = cc2_start_command_window(data); + if (ret) + return ret; + + return cc2_read_eeprom_alarm_registers(data); +} + +/* + * A measurement request must be sent in sleep mode to wake up the device and + * start measuring. This request only contains the slave address and the write + * bit. + */ +static int cc2_measurement_request(struct i2c_client *client) +{ + int ret; + + ret = i2c_master_send(client, NULL, 0); + if (ret < 0) + return ret; + + return 0; +}; + +static int cc2_data_fetch(struct i2c_client *client, + enum hwmon_sensor_types type, long *val) +{ + u8 data[CC2_MEASUREMENT_DATA_LEN]; + u8 status; + int ret; + + ret = i2c_master_recv(client, data, CC2_MEASUREMENT_DATA_LEN); + if (ret != CC2_MEASUREMENT_DATA_LEN) { + ret = ret < 0 ? ret : -EIO; + return ret; + } + status = FIELD_GET(CC2_STATUS_FIELD, data[0]); + if (status == CC2_STATUS_STALE_DATA) + return -EBUSY; + + if (status != CC2_STATUS_VALID_DATA) + return -EIO; + + switch (type) { + case hwmon_humidity: + *val = cc2_rh_convert(be16_to_cpup((__be16 *)&data[0])); + break; + case hwmon_temp: + *val = cc2_temp_convert(be16_to_cpup((__be16 *)&data[2])); + break; + default: + return -EINVAL; + } + + return 0; +} + +/* + * non sleep-mode devies make measurements periodically and * indicate + * valid data with the ready signal, whose level stays high until data + * is fetched even if new measurements are made. + */ +static int cc2d23_measurement(struct cc2_data *data, + enum hwmon_sensor_types type, long *val) +{ + unsigned long timeout; + int ret; + + if (data->irq_ready > 0) { + if (try_wait_for_completion(&data->complete)) + goto data_fetch; + + timeout = msecs_to_jiffies(CC2_UPDATE_RATE_MS); + ret = wait_for_completion_timeout(&data->complete, timeout); + if (!ret) + return -ETIMEDOUT; + } else { + /* check if data is ready and retry for stale data */ + ret = cc2_data_fetch(data->client, type, val); + if (ret != -EBUSY) + return ret; + + msleep(CC2_UPDATE_RATE_MS); + } + +data_fetch: + return cc2_data_fetch(data->client, type, val); +} + +/* + * sleep-mode devies require a measurement request command to wake up + * the device and trigger a measurement. Then a data fetch command is + * sent to retrieve the measured values. + */ +static int cc2d23s_measurement(struct cc2_data *data, + enum hwmon_sensor_types type, long *val) +{ + unsigned long timeout; + int ret; + + if (data->irq_ready > 0) { + /* + * the device carries out a first measurement after entering + * measurement mode automatically. To avoid outdated values, + * a new request is made + */ + if (try_wait_for_completion(&data->complete)) { + ret = cc2_clear_data_ready(data->client); + if (ret < 0) + return ret; + } + + ret = cc2_measurement_request(data->client); + if (ret < 0) + return ret; + + timeout = msecs_to_jiffies(2 * CC2_RESP_SLEEP_MS); + ret = wait_for_completion_timeout(&data->complete, timeout); + if (!ret) + return -ETIMEDOUT; + } else { + ret = cc2_measurement_request(data->client); + if (ret < 0) + return ret; + + msleep(CC2_RESP_SLEEP_MS); + } + + return cc2_data_fetch(data->client, type, val); +} + +static umode_t cc2_is_visible(const void *data, enum hwmon_sensor_types type, + u32 attr, int channel) +{ + const struct cc2_data *cc2 = data; + + switch (type) { + case hwmon_humidity: + switch (attr) { + case hwmon_humidity_input: + return 0444; + case hwmon_humidity_min_alarm: + return cc2->rh_alarm.low_alarm_visible ? 0444 : 0; + case hwmon_humidity_max_alarm: + return cc2->rh_alarm.high_alarm_visible ? 0444 : 0; + case hwmon_humidity_min: + case hwmon_humidity_min_hyst: + return cc2->rh_alarm.low_alarm_visible ? 0644 : 0; + case hwmon_humidity_max: + case hwmon_humidity_max_hyst: + return cc2->rh_alarm.high_alarm_visible ? 0644 : 0; + default: + return 0; + } + case hwmon_temp: + switch (attr) { + case hwmon_temp_input: + return 0444; + default: + return 0; + } + default: + break; + } + + return 0; +} + +static irqreturn_t cc2_ready_interrupt(int irq, void *data) +{ + struct cc2_data *cc2 = data; + + mutex_lock(&cc2->alarm_lock); + if (!cc2->ignore_irqs) + complete(&cc2->complete); + mutex_unlock(&cc2->alarm_lock); + + return IRQ_HANDLED; +} + +static irqreturn_t cc2_low_interrupt(int irq, void *data) +{ + struct cc2_data *cc2 = data; + + mutex_lock(&cc2->alarm_lock); + if (!cc2->ignore_irqs) { + hwmon_notify_event(cc2->hwmon, hwmon_humidity, + hwmon_humidity_min_alarm, CC2_CHAN_HUMIDITY); + cc2->rh_alarm.low_alarm = true; + } + mutex_unlock(&cc2->alarm_lock); + + return IRQ_HANDLED; +} + +static irqreturn_t cc2_high_interrupt(int irq, void *data) +{ + struct cc2_data *cc2 = data; + + mutex_lock(&cc2->alarm_lock); + if (!cc2->ignore_irqs) { + hwmon_notify_event(cc2->hwmon, hwmon_humidity, + hwmon_humidity_max_alarm, CC2_CHAN_HUMIDITY); + cc2->rh_alarm.high_alarm = true; + } + mutex_unlock(&cc2->alarm_lock); + + return IRQ_HANDLED; +} + +static int cc2_humidity_min_alarm_status(struct cc2_data *data, long *val) +{ + long meas; + int ret; + + mutex_lock(&data->i2c_lock); + ret = data->config->measurement(data, hwmon_humidity, &meas); + mutex_unlock(&data->i2c_lock); + if (ret < 0) + return ret; + + mutex_lock(&data->alarm_lock); + if (data->rh_alarm.low_alarm) { + *val = (meas < data->rh_alarm.min_hyst) ? 1 : 0; + data->rh_alarm.low_alarm = *val; + } else { + *val = 0; + } + mutex_unlock(&data->alarm_lock); + + return 0; +} + +static int cc2_humidity_max_alarm_status(struct cc2_data *data, long *val) +{ + long meas; + int ret; + + mutex_lock(&data->i2c_lock); + ret = data->config->measurement(data, hwmon_humidity, &meas); + mutex_unlock(&data->i2c_lock); + if (ret < 0) + return ret; + + mutex_lock(&data->alarm_lock); + if (data->rh_alarm.high_alarm) { + *val = (meas > data->rh_alarm.max_hyst) ? 1 : 0; + data->rh_alarm.high_alarm = *val; + } else { + *val = 0; + } + mutex_unlock(&data->alarm_lock); + + return 0; +} + +static int cc2_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, + int channel, long *val) +{ + struct cc2_data *data = dev_get_drvdata(dev); + int ret = 0; + + switch (type) { + case hwmon_temp: + mutex_lock(&data->i2c_lock); + ret = data->config->measurement(data, type, val); + mutex_unlock(&data->i2c_lock); + break; + case hwmon_humidity: + switch (attr) { + case hwmon_humidity_input: + mutex_lock(&data->i2c_lock); + ret = data->config->measurement(data, type, val); + mutex_unlock(&data->i2c_lock); + break; + case hwmon_humidity_min: + *val = data->rh_alarm.min; + break; + case hwmon_humidity_min_hyst: + *val = data->rh_alarm.min_hyst; + break; + case hwmon_humidity_max: + *val = data->rh_alarm.max; + break; + case hwmon_humidity_max_hyst: + *val = data->rh_alarm.max_hyst; + break; + case hwmon_humidity_min_alarm: + ret = cc2_humidity_min_alarm_status(data, val); + if (ret < 0) + return -EIO; + break; + case hwmon_humidity_max_alarm: + ret = cc2_humidity_max_alarm_status(data, val); + if (ret < 0) + return -EIO; + break; + default: + return -EOPNOTSUPP; + } + break; + default: + return -EOPNOTSUPP; + } + + return ret; +} + +static int cc2_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, + int channel, long val) +{ + struct cc2_data *data = dev_get_drvdata(dev); + u8 cmd[CC2_EEPROM_SIZE]; + u16 arg[CC2_EEPROM_SIZE]; + int ret; + + if (type != hwmon_humidity) + return -EOPNOTSUPP; + + if (val < CC2_RH_MIN || val > CC2_RH_MAX) + return -EINVAL; + + switch (attr) { + case hwmon_humidity_min: + cmd[0] = CC2_W_ALARM_LOW_ON; + arg[0] = cc2_rh_to_reg(val); + ret = cc2_update_eeprom(data, cmd, arg, 1); + if (ret < 0) + return ret; + + data->rh_alarm.min = cc2_rh_to_device_resolution(val); + break; + case hwmon_humidity_min_hyst: + cmd[0] = CC2_W_ALARM_LOW_OFF; + arg[0] = cc2_rh_to_reg(val); + ret = cc2_update_eeprom(data, cmd, arg, 1); + if (ret < 0) + return ret; + + data->rh_alarm.min_hyst = cc2_rh_to_device_resolution(val); + break; + case hwmon_humidity_max: + cmd[0] = CC2_W_ALARM_HIGH_ON; + arg[0] = cc2_rh_to_reg(val); + ret = cc2_update_eeprom(data, cmd, arg, 1); + if (ret < 0) + return ret; + + data->rh_alarm.max = cc2_rh_to_device_resolution(val); + break; + case hwmon_humidity_max_hyst: + cmd[0] = CC2_W_ALARM_HIGH_OFF; + arg[0] = cc2_rh_to_reg(val); + ret = cc2_update_eeprom(data, cmd, arg, 1); + if (ret < 0) + return ret; + + data->rh_alarm.max_hyst = cc2_rh_to_device_resolution(val); + break; + default: + return -EOPNOTSUPP; + } + + return 0; +} + +static int cc2_disable(struct cc2_data *data) +{ + int ret = 0; + + if (data->regulator && regulator_is_enabled(data->regulator)) + ret = regulator_disable(data->regulator); + + return ret; +} + +static int cc2_request_ready_irq(struct cc2_data *data, struct device *dev) +{ + int ret = 0; + + data->irq_ready = fwnode_irq_get_byname(dev_fwnode(dev), "ready"); + if (data->irq_ready > 0) { + init_completion(&data->complete); + ret = devm_request_threaded_irq(dev, data->irq_ready, NULL, + cc2_ready_interrupt, + IRQF_ONESHOT | + IRQF_TRIGGER_RISING, + dev_name(dev), data); + } + + return ret; +} + +static int cc2_request_alarm_irqs(struct cc2_data *data, struct device *dev) +{ + int ret; + + data->irq_low = fwnode_irq_get_byname(dev_fwnode(dev), "low"); + if (data->irq_low > 0) { + ret = devm_request_threaded_irq(dev, data->irq_low, NULL, + cc2_low_interrupt, + IRQF_ONESHOT | + IRQF_TRIGGER_RISING, + dev_name(dev), data); + if (!ret) + data->rh_alarm.low_alarm_visible = true; + } + + data->irq_high = fwnode_irq_get_byname(dev_fwnode(dev), "high"); + if (data->irq_high > 0) { + ret = devm_request_threaded_irq(dev, data->irq_high, NULL, + cc2_high_interrupt, + IRQF_ONESHOT | + IRQF_TRIGGER_RISING, + dev_name(dev), data); + if (!ret) + data->rh_alarm.high_alarm_visible = true; + } + + return ret; +} + +static const struct hwmon_channel_info *cc2_info[] = { + HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT), + HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT | HWMON_H_MIN | HWMON_H_MAX | + HWMON_H_MIN_HYST | HWMON_H_MAX_HYST | + HWMON_H_MIN_ALARM | HWMON_H_MAX_ALARM), + NULL +}; + +static const struct hwmon_ops cc2_hwmon_ops = { + .is_visible = cc2_is_visible, + .read = cc2_read, + .write = cc2_write, +}; + +static const struct hwmon_chip_info cc2_chip_info = { + .ops = &cc2_hwmon_ops, + .info = cc2_info, +}; + +static const struct cc2_config cc2_config[] = { + [cc2d23] = { .measurement = cc2d23_measurement, }, + [cc2d23s] = { .measurement = cc2d23s_measurement, }, +}; + +static const struct i2c_device_id cc2_id[] = { + { "cc2d23", cc2d23 }, + { "cc2d23s", cc2d23s }, + { "cc2d25", cc2d23 }, + { "cc2d25s", cc2d23s }, + { "cc2d33", cc2d23 }, + { "cc2d33s", cc2d23s }, + { "cc2d35", cc2d23 }, + { "cc2d35s", cc2d23s }, + { } +}; +MODULE_DEVICE_TABLE(i2c, cc2_id); + +static const struct of_device_id cc2_of_match[] = { + { + .compatible = "amphenol,cc2d23", + .data = (void *)cc2d23, + }, + { + .compatible = "amphenol,cc2d23s", + .data = (void *)cc2d23s, + }, + { + .compatible = "amphenol,cc2d25", + .data = (void *)cc2d23, + }, + { + .compatible = "amphenol,cc2d25s", + .data = (void *)cc2d23s, + }, + { + .compatible = "amphenol,cc2d33", + .data = (void *)cc2d23, + }, + { + .compatible = "amphenol,cc2d33s", + .data = (void *)cc2d23s, + }, + { + .compatible = "amphenol,cc2d35", + .data = (void *)cc2d23, + }, + { + .compatible = "amphenol,cc2d35s", + .data = (void *)cc2d23s, + }, + { }, +}; +MODULE_DEVICE_TABLE(of, cc2_of_match); + +static int cc2_probe(struct i2c_client *client) +{ + struct cc2_data *data; + struct device *dev = &client->dev; + enum cc2_ids chip; + int ret; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) + return -EOPNOTSUPP; + + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + i2c_set_clientdata(client, data); + + mutex_init(&data->i2c_lock); + mutex_init(&data->alarm_lock); + + data->client = client; + + if (client->dev.of_node) + chip = (uintptr_t)of_device_get_match_data(&client->dev); + else + chip = i2c_match_id(cc2_id, client)->driver_data; + + data->config = &cc2_config[chip]; + + ret = cc2_request_ready_irq(data, dev); + if (ret) + return ret; + + data->regulator = devm_regulator_get_optional(dev, "vdd"); + if (IS_ERR(data->regulator)) { + if (PTR_ERR(data->regulator) != -ENODEV) { + dev_err_probe(dev, PTR_ERR(data->regulator), + "Failed to get regulator\n"); + return PTR_ERR(data->regulator); + } + /* No access to EEPROM without regulator: no alarm control */ + data->regulator = NULL; + } else { + ret = cc2_retrieve_alarm_config(data); + if (ret) { + dev_err_probe(dev, PTR_ERR(data->regulator), + "Failed to retrieve alarm config\n"); + goto cleanup; + } + + ret = cc2_request_alarm_irqs(data, dev); + if (ret) { + dev_err_probe(dev, PTR_ERR(data->regulator), + "Failed to request alarm irqs\n"); + goto cleanup; + } + } + + data->hwmon = devm_hwmon_device_register_with_info(dev, client->name, + data, &cc2_chip_info, + NULL); + if (IS_ERR(data->hwmon)) { + dev_err_probe(dev, PTR_ERR(data->regulator), + "Failed to register hwmon device\n"); + ret = PTR_ERR(data->hwmon); + goto cleanup; + } + + return 0; + +cleanup: + if (cc2_disable(data)) + dev_dbg(dev, "Failed to disable device"); + + return ret; +} + +static void cc2_remove(struct i2c_client *client) +{ + struct cc2_data *data = i2c_get_clientdata(client); + int ret = cc2_disable(data); + + if (ret) + dev_dbg(&client->dev, "Failed to disable device"); +} + +static struct i2c_driver cc2_driver = { + .driver = { + .name = CC2_DEV_NAME, + .of_match_table = cc2_of_match, + }, + .probe = cc2_probe, + .remove = cc2_remove, +}; +module_i2c_driver(cc2_driver); + +MODULE_AUTHOR("Javier Carrasco "); +MODULE_DESCRIPTION("Amphenol ChipCap 2 humidity and temperature sensor driver"); +MODULE_LICENSE("GPL");