From patchwork Sun Oct 10 03:31:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oskar Senft X-Patchwork-Id: 12548243 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23443C433F5 for ; Sun, 10 Oct 2021 03:31:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF1F660EFE for ; Sun, 10 Oct 2021 03:31:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230017AbhJJDdT (ORCPT ); Sat, 9 Oct 2021 23:33:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229685AbhJJDdP (ORCPT ); Sat, 9 Oct 2021 23:33:15 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 225CCC061570 for ; Sat, 9 Oct 2021 20:31:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id i83-20020a252256000000b005b67a878f56so18461856ybi.17 for ; Sat, 09 Oct 2021 20:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=g7EJveLFxokNEPlL/tBARdXLFCbt2zr2sbuE+aH5gDg=; b=KvmF4L7GCUkYpLZnh/RhRG8DOvChmVQLOZS3IRfJqUg2JS7re7EKuMcJUtSZvo2Ke9 DyZ1/eDFu1lbQG7j3JimTxNQbDpf83AdUlxOKq2E2H40NZVfnhHgj7EbnQ3/TvDBIwiq /Nzv841D1Zwl7uRs4KXke+l2kwUjcd5XK3dqYg+C808QhtLeDxunFo4yzDF3H80Et9lN LSkO+762F8e7UIzwrmGgjjlfEksmPBpDrpOHWDd4HInGbADJymkutMU75dGa7DFYVCY5 u8VbdEWnOUe6CPKXujrLzEQ5QidtJRAjumcmX6RdIdemT11A+MgDRsuaBFrnlXhf6iae rjdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=g7EJveLFxokNEPlL/tBARdXLFCbt2zr2sbuE+aH5gDg=; b=48aYSMWNLRngB6apO2eSJYyZhzsJsuxeHOZxhtTb7Lkx7q13qVfIJvhnoKvfQh62Vb Dg3nzUaCHvfPbXlB4/wzkf9vfMauAz4Ai8l45m8dFWERHX0pxF/cVIX+QhTu26gWjWTU bifULfc26XNVABVnr5Q5cVTazZkjYOfNJHx2cbjoeCKbmzKLX2uE1o95jmOaCFqbz0FO uKXMcnkDk2ifG8Bn6C6AhqIi6ojz26S6rt1lPC9zM+hVX5cL5Fbi0xD3he1MavstdVHW j8cMlqkneDLWlGZpxWa/RZTn20hUvOOvr8HwhSC1vknEwBMwf6HFtg1dbmswgw+f7yz4 KKyg== X-Gm-Message-State: AOAM530xQXlK4pIMJmd2K0bP4eZ9z6Xbc+rvnoxH7deMraRY/dK2vcPb 3OvA8+rhpd/wDm55If5i4M/mZdg= X-Google-Smtp-Source: ABdhPJwvsVg8yIJ5folfecqAnSh3yK/B7CLK30Awqsr4W6RZZDipbs8xSZDsUUSAPGv+qJjE4QiociE= X-Received: from osk.cam.corp.google.com ([2620:0:1004:1a:cf37:dd81:3340:f4a7]) (user=osk job=sendgmr) by 2002:a25:2286:: with SMTP id i128mr12286454ybi.76.1633836677363; Sat, 09 Oct 2021 20:31:17 -0700 (PDT) Date: Sat, 9 Oct 2021 23:31:11 -0400 Message-Id: <20211010033112.3621816-1-osk@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.882.g93a45727a2-goog Subject: [PATCH v6 1/2] dt-bindings: hwmon: Add nct7802 bindings From: Oskar Senft To: Jean Delvare , Guenter Roeck , Rob Herring , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: Oskar Senft Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org This change documents the device tree bindings for the Nuvoton NCT7802Y driver. Signed-off-by: Oskar Senft --- Changes from PATCH v5: - Refactored to use patternProperties. - Added validation for sensor-type and temperature-mode. --- .../bindings/hwmon/nuvoton,nct7802.yaml | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/nuvoton,nct7802.yaml diff --git a/Documentation/devicetree/bindings/hwmon/nuvoton,nct7802.yaml b/Documentation/devicetree/bindings/hwmon/nuvoton,nct7802.yaml new file mode 100644 index 000000000000..73fffd661f0a --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/nuvoton,nct7802.yaml @@ -0,0 +1,144 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- + +$id: http://devicetree.org/schemas/hwmon/nuvoton,nct7802.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Nuvoton NCT7802Y Hardware Monitoring IC + +maintainers: + - Guenter Roeck + +description: | + The NCT7802Y is a hardware monitor IC which supports one on-die and up to + 5 remote temperature sensors with SMBus interface. + + Datasheets: + https://www.nuvoton.com/export/resource-files/Nuvoton_NCT7802Y_Datasheet_V12.pdf + +properties: + compatible: + enum: + - nuvoton,nct7802 + + reg: + maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + +patternProperties: + "^channel@[0-3]$": + type: object + properties: + reg: + items: + - enum: + - 0 # Local Temperature Sensor ("LTD") + - 1 # Remote Temperature Sensor or Voltage Sensor 1 ("RTD1") + - 2 # Remote Temperature Sensor or Voltage Sensor 2 ("RTD2") + - 3 # Remote Temperature Sensor or Voltage Sensor 3 ("RTD3") + sensor-type: + items: + - enum: + - temperature + - voltage + temperature-mode: + items: + - enum: + - thermistor + - thermal-diode + required: + - reg + allOf: + # For channels RTD1, RTD2 and RTD3, require sensor-type to be set. + # Otherwise (for all other channels), do not allow temperature-mode to be + # set. + - if: + properties: + reg: + items: + - enum: + - 1 + - 2 + - 3 + then: + required: + - sensor-type + else: + not: + required: + - sensor-type + + # For channels RTD1 and RTD2 and if sensor-type is "temperature", require + # temperature-mode to be set. Otherwise (for all other channels or + # sensor-type settings), do not allow temperature-mode to be set + - if: + properties: + reg: + items: + - enum: + - 1 + - 2 + sensor-type: + items: + - enum: + - temperature + then: + required: + - temperature-mode + else: + not: + required: + - temperature-mode + + additionalProperties: false + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + nct7802@28 { + compatible = "nuvoton,nct7802"; + reg = <0x28>; + + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { /* LTD */ + reg = <0>; + status = "okay"; + }; + + channel@1 { /* RTD1 */ + reg = <1>; + status = "okay"; + sensor-type = "voltage"; + }; + + channel@2 { /* RTD2 */ + reg = <2>; + status = "okay"; + sensor-type = "temperature"; + temperature-mode = "thermal-diode"; + }; + + channel@3 { /* RTD3 */ + reg = <3>; + status = "okay"; + sensor-type = "temperature"; + }; + }; + }; From patchwork Sun Oct 10 03:31:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oskar Senft X-Patchwork-Id: 12548245 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A78CC4332F for ; Sun, 10 Oct 2021 03:31:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DF1860F6B for ; Sun, 10 Oct 2021 03:31:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230105AbhJJDdU (ORCPT ); Sat, 9 Oct 2021 23:33:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229895AbhJJDdT (ORCPT ); Sat, 9 Oct 2021 23:33:19 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2406C061762 for ; Sat, 9 Oct 2021 20:31:20 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id y16-20020a2586d0000000b005b752db8f97so18333650ybm.18 for ; Sat, 09 Oct 2021 20:31:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=GTFBSlmBEa7odVv9MwgpMymtH3E72sgHzXsh/md0g9k=; b=fztNZ+tuHSBVQ+F0Re1E3H950emA6aSsoX/k5Nk4JiMboTGOHsOTsyk///xHjIN7nY cQpk1oIoSpkXE6TGeKBYR+JHkRmdPoS2KYx9ukc12niGB2SMSe0pPY4tEBdUNlNDusM0 kbTDnsrroXqk9u7JriuGQB8cs2C/mv8QY81TbOXmAotdkb1UzVYipyHSG6795yopD5dL CxiGtsrvCFRHscNB7/DgpzUV35ciNo10uKI4sVljvK4x/K633y55kYgC37mYzgvrESVY KZqRKIA3p3oHnqBpF9G7McCdNxgqdenUdPeab7r0dGkuz9l0SqgB5onesqCxdf2tQz1d 31Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=GTFBSlmBEa7odVv9MwgpMymtH3E72sgHzXsh/md0g9k=; b=ODNC1DVX+L9RTS2nOfihAHJFGuf5PawiATwArK69YSq3xTJzAA+reZTn0nBgFKAdDE DuDJ1/puZ+fl/7ctvOEyRrSGl08nP1OvAy3v69wv5d5G17car2lumJcei8FfkGZcn9fH KZDYG/0XwHWn6u7yjyYkpeoGLsNkJ6GJx8Ew3a4k3wYmxGVrK1sOK2jqKnbOYCAZnN9y 1C4Eh5EUjGw5z/SsGKrP1GQCSeAof8UXfmU66PhVJynKCcBrRINvfzzXTZUZPIx6j8ru dXYwiXOSxyF3Jjh4aw4pG5SYbOcGGX23oZN/RPsLnIugjvey1lP8bMbPTjJcLRS2nveO gLrg== X-Gm-Message-State: AOAM530UUxvmyAzu8URf7/YOT+PD8sETCdtIGIXJVarRy5K9fRhrcvvo 7VqfYg6LvDljoKGWwDNCgeAaQDQ= X-Google-Smtp-Source: ABdhPJxfJfZbJWJMRGv30fvKYBtkmUp+/KyFDdRza+LRta/JvYZSuIIYGTVjBoVoCtl1wGhcD7wagF4= X-Received: from osk.cam.corp.google.com ([2620:0:1004:1a:cf37:dd81:3340:f4a7]) (user=osk job=sendgmr) by 2002:a25:5646:: with SMTP id k67mr13586251ybb.127.1633836679955; Sat, 09 Oct 2021 20:31:19 -0700 (PDT) Date: Sat, 9 Oct 2021 23:31:12 -0400 In-Reply-To: <20211010033112.3621816-1-osk@google.com> Message-Id: <20211010033112.3621816-2-osk@google.com> Mime-Version: 1.0 References: <20211010033112.3621816-1-osk@google.com> X-Mailer: git-send-email 2.33.0.882.g93a45727a2-goog Subject: [PATCH v6 2/2] hwmon: (nct7802) Make temperature/voltage sensors configurable From: Oskar Senft To: Jean Delvare , Guenter Roeck , Rob Herring , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Cc: Oskar Senft Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org This change allows LTD and RTD inputs to be configured via device tree bindings. If the DT bindings are not present or invalid, the input configuration is not modified and left at HW defaults. Signed-off-by: Oskar Senft Reviewed-by: Guenter Roeck --- Changes from PATCH v5: - Removed unused "found_channel_config" variable. - Initialize mode_mask and mode_val to defaults. --- drivers/hwmon/nct7802.c | 129 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/nct7802.c b/drivers/hwmon/nct7802.c index 604af2f6103a..d56f78327619 100644 --- a/drivers/hwmon/nct7802.c +++ b/drivers/hwmon/nct7802.c @@ -51,6 +51,23 @@ static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = { #define REG_CHIP_ID 0xfe #define REG_VERSION_ID 0xff +/* + * Resistance temperature detector (RTD) modes according to 7.2.32 Mode + * Selection Register + */ +#define RTD_MODE_CURRENT 0x1 +#define RTD_MODE_THERMISTOR 0x2 +#define RTD_MODE_VOLTAGE 0x3 + +#define MODE_RTD_MASK 0x3 +#define MODE_LTD_EN 0x40 + +/* + * Bit offset for sensors modes in REG_MODE. + * Valid for index 0..2, indicating RTD1..3. + */ +#define MODE_BIT_OFFSET_RTD(index) ((index) * 2) + /* * Data structures and manipulation thereof */ @@ -1038,7 +1055,112 @@ static const struct regmap_config nct7802_regmap_config = { .volatile_reg = nct7802_regmap_is_volatile, }; -static int nct7802_init_chip(struct nct7802_data *data) +static int nct7802_get_channel_config(struct device *dev, + struct device_node *node, u8 *mode_mask, + u8 *mode_val) +{ + u32 reg; + const char *type_str, *md_str; + u8 md; + + if (!node->name || of_node_cmp(node->name, "channel")) + return 0; + + if (of_property_read_u32(node, "reg", ®)) { + dev_err(dev, "Could not read reg value for '%s'\n", + node->full_name); + return -EINVAL; + } + + if (reg > 3) { + dev_err(dev, "Invalid reg (%u) in '%s'\n", reg, + node->full_name); + return -EINVAL; + } + + if (reg == 0) { + if (!of_device_is_available(node)) + *mode_val &= ~MODE_LTD_EN; + else + *mode_val |= MODE_LTD_EN; + *mode_mask |= MODE_LTD_EN; + return 0; + } + + /* At this point we have reg >= 1 && reg <= 3 */ + + if (!of_device_is_available(node)) { + *mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1)); + *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); + return 0; + } + + if (of_property_read_string(node, "sensor-type", &type_str)) { + dev_err(dev, "No type for '%s'\n", node->full_name); + return -EINVAL; + } + + if (!strcmp(type_str, "voltage")) { + *mode_val |= (RTD_MODE_VOLTAGE & MODE_RTD_MASK) + << MODE_BIT_OFFSET_RTD(reg - 1); + *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); + return 0; + } + + if (strcmp(type_str, "temperature")) { + dev_err(dev, "Invalid type '%s' for '%s'\n", type_str, + node->full_name); + return -EINVAL; + } + + if (reg == 3) { + /* RTD3 only supports thermistor mode */ + md = RTD_MODE_THERMISTOR; + } else { + if (of_property_read_string(node, "temperature-mode", + &md_str)) { + dev_err(dev, "No mode for '%s'\n", node->full_name); + return -EINVAL; + } + + if (!strcmp(md_str, "thermal-diode")) + md = RTD_MODE_CURRENT; + else if (!strcmp(md_str, "thermistor")) + md = RTD_MODE_THERMISTOR; + else { + dev_err(dev, "Invalid mode '%s' for '%s'\n", md_str, + node->full_name); + return -EINVAL; + } + } + + *mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1); + *mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1); + + return 0; +} + +static int nct7802_configure_channels(struct device *dev, + struct nct7802_data *data) +{ + /* Enable local temperature sensor by default */ + u8 mode_mask = MODE_LTD_EN, mode_val = MODE_LTD_EN; + struct device_node *node; + int err; + + if (dev->of_node) { + for_each_child_of_node(dev->of_node, node) { + err = nct7802_get_channel_config(dev, node, &mode_mask, + &mode_val); + if (err) + return err; + } + } + + return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val); +} + +static int nct7802_init_chip(struct device *dev, struct nct7802_data *data) { int err; @@ -1047,8 +1169,7 @@ static int nct7802_init_chip(struct nct7802_data *data) if (err) return err; - /* Enable local temperature sensor */ - err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40); + err = nct7802_configure_channels(dev, data); if (err) return err; @@ -1074,7 +1195,7 @@ static int nct7802_probe(struct i2c_client *client) mutex_init(&data->access_lock); mutex_init(&data->in_alarm_lock); - ret = nct7802_init_chip(data); + ret = nct7802_init_chip(dev, data); if (ret < 0) return ret;