From patchwork Tue Jul 23 00:51:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13739253 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C9CD53376 for ; Tue, 23 Jul 2024 00:52:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695933; cv=none; b=WBo0xo5MnojdDr5zQkA6/jKE2RFcYdRe+xnrtAVJGJ5zKohW5yLFBNvhVYM5VadedXXWheC2/o9v6b0oY4MShR5mH/V2KqTh1Lgk48lGQmnIyfXsDlTHbv7hQauAgObA8OhgGUBb/sCuV02jC3VQ//7slehck8+VNecjPfcd6xE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695933; c=relaxed/simple; bh=dngi2elumui10v1Wh156QB89NnKrnohxds5RfYG3Xic=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JZsFt4SfoxpQi3B6RoiC+OvEsPIA27CgbPzrBqshOKrcFWkwK3UlGdzbpNxZOy7CZF9jMOEmri3Zzv/mXsLwtdj0zqVGUZFLWJfxH94zIV6OuxW3azv7tPzg6HggZmvRI1q42mW0JRYbgbWauJvf5MACV2j7VSsvYvTtnwTFlL8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VmhhjGbG; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VmhhjGbG" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1fc65329979so853055ad.0 for ; Mon, 22 Jul 2024 17:52:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721695930; x=1722300730; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=5JfRQGxlDbMP9iZuZVNYBfc9gi38N9bubZDW+KY7goQ=; b=VmhhjGbG1tAnTj5/hs2M6SWu4dYjqD7opues/pPJJdAJS/6K+v4PAkWnBu7mIxEAvX SJ1BLKQ4G3MW8n4v5PhXTzyKgyNH/qoafkD4BFkovmrpPwAgWpqSxP/y29e6qC1hLL3x kSvHFIq5A34e84M9Zhawo7acmf4UOxbB3Rel4yqTQp4lkIB0v+/7cKZqzNY3DQUvEzrr MKs/mdYSyyiArs5nJRQwkkufD+PQsHngQlBMALFaTU8kxOxDj0OjKWrxwMAjQLyrRBoq 5BqBHxdgJ4vX1nJnIEefAjkzRdQeUT46nTDpD1P4O/XLW4HFhHJka9BN2IEqNPUjI/qF bo0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721695930; x=1722300730; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5JfRQGxlDbMP9iZuZVNYBfc9gi38N9bubZDW+KY7goQ=; b=r2PF8RteL+nitDK7+kzPyJLYV0/RwcvaTV1cr32nqthN/eXAHsL0m4UrnhX9fvi032 W7JvR2y3YJjPN1YoSRPoumzOHq7OFDqB8ng7IddyCubzunhjCBe2YKnC9yQBD9OjrV+6 qCcR3PoE1rDA5Tmqh4P8IoYdIGFFABFVdPGnFr3uCJwes3rdLeLfpkGt/OpoiBY43fLP 2UcTxcN4q3m1aFX+xYxAu85PBBxTFF0PBJ8Tb9C458k7rtgaE0GrwcKVNEP0IEC/RYJt sL5Hw04fL+d5Ds8i/nhye6Y8xwjBM79I0SFyndiSuE809epnhI5oBzw3MP5B1OnuJiNF Fikw== X-Gm-Message-State: AOJu0YyW2iRGB1OoxU7Lsp53/vbD3Jzke3KUfGjn+9AgxBnZduyAiCz/ 1ZBu1DYqTZ25Iz38bDaWdXvdb5Jgldfsd4r0u2QXmkZcVAXJXeoYXJnFSQ== X-Google-Smtp-Source: AGHT+IHMzceHVYTvAgity1KaxUQApMTtN7GDvtytSol/pQDg5ZXCUTfhBqez8V6WnI39OvzhAPgJ6A== X-Received: by 2002:a17:902:e746:b0:1fb:701b:7298 with SMTP id d9443c01a7336-1fd74573442mr90970085ad.32.1721695930104; Mon, 22 Jul 2024 17:52:10 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fd6f2aaee0sm61489505ad.117.2024.07.22.17.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 17:52:09 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 1/6] hwmon: (max6697) Reorder include files Date: Mon, 22 Jul 2024 17:51:59 -0700 Message-Id: <20240723005204.1356501-2-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723005204.1356501-1-linux@roeck-us.net> References: <20240723005204.1356501-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reorder include files to alphabetic order to improve maintainability. Signed-off-by: Guenter Roeck --- drivers/hwmon/max6697.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index 20981f9443dd..6745d311dcf2 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c @@ -6,16 +6,16 @@ * Copyright (c) 2011 David George */ -#include -#include -#include -#include -#include +#include #include #include -#include +#include +#include +#include +#include #include #include +#include #include From patchwork Tue Jul 23 00:52:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13739254 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BE8212E1DB for ; Tue, 23 Jul 2024 00:52:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695934; cv=none; b=B+jfGUCWqkf3I0CUnXovdgWJzT7pt6+cuzMxLya2/1aDJqT3lKqyjUgNe+aw3lD0zd6Nit9qR5+s8N44C/Qp+mw7XR549qDUffieKnwpNpQdB8Q7CCJyjuINb+eFKexWWW72wDIB1RLkBjUNfX58NjpYM2gVuqub1BxxH9TkFqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695934; c=relaxed/simple; bh=OtHozKEMCclI4OMD7Aj0ng8H5SH48LA4zDdUM1zmvmg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XN4/MDvatLfoP+Baaeb8q75dXht/Qfbe4L6E6Gbc7OOvBGlPvbjqN44Zd6BlFmR7R5K7CQV5/QLIM7CxswfOjcmT0mJgIs4wp7HT0tv7N8uoZxGOnWk8dV9pqo3z3iOEnt6eEq0uYUEvCXD68IG+rvQCsWFx0Sn38NIqGAXT99c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ILo5yZA6; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ILo5yZA6" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70d150e8153so1457443b3a.0 for ; Mon, 22 Jul 2024 17:52:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721695932; x=1722300732; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=t6FbNFg7iRyxt1CLYcV0xNCTSJVjupxhzhwwshD/KRA=; b=ILo5yZA60BJICAAZeBGDDF4b0mljEJTST/r+P8iIjF8Xss2IXiSUDgdJ3uIBqpwIsQ pZCL3a1C8qHhFVM6bpaNQKR7ieaMZs1XG25V4r78BEBFDAwUFTYtUORTt/K9L54e8aEW gd06P3JYSrlFc6RxLzIIcalvLjq1PrnbMasXjZdrH4IEE/fcF3Dx7vOYqe041J1ICvOz U7/SgNlHtdDQcEMMn6CPmuWWaR9imCPGBGx3+fZLClrH9o8qVnfjvFunLuFCo/p17mW4 cx/CMmCdZtBeMMLCgnvUgi5lY+AHBsPeIf13LWjfhSnrILeugWJSaBF0Byfe3AlWtAEA 4bPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721695932; x=1722300732; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=t6FbNFg7iRyxt1CLYcV0xNCTSJVjupxhzhwwshD/KRA=; b=PHl/kfWWNCekg9t6U4Yfhw0EebcjQ9C/UGq10TdcTf7s26ign9drcwDA8cDfgIKQZ5 k3MZaMwQC0ZYarHW2AjdADzYxdW9uVDdqzRnMnoM7M4+zXpxm+IvfsDRPYSk9U/Tqt2I Ec9nr5ZKT8SBxuMTTC/OLTw7vk5wAtcXFBUMp7F5TL6Eo8zHDdXrTM14UA4GGWog3Xeg FsFWQ6rqrAEAMi4R1TQPhZHr0slOkkHo3vKWfDfbgKCybR6VEN1Ak3ZiTnA/TysmuuHr y+1mxr//IX/f9WH8ke9gSv8FBFFOdT5jtXfsfFI1byAq9jQCcBNW5H8bnZqYDd/U3AYe E9dA== X-Gm-Message-State: AOJu0YwEa4Mu/wEt/uVaFZoK30hGuZXlvBL1gVUhUaQj3jWlMVTlSIkW psEObbxH+wAVa3PfIUikVqPwBA2l5afzYltDq1FalIXnX9yXXy/ppoB+MA== X-Google-Smtp-Source: AGHT+IHWomAke+Bsj0aWisyrrjh3ou9KISIfeaHMfPwtItEe/C+7Rc170qFynAmQbwYyvN9yzcd6Uw== X-Received: by 2002:a05:6a00:3e1c:b0:70d:2208:c98e with SMTP id d2e1a72fcca58-70e80602780mr989443b3a.0.1721695931856; Mon, 22 Jul 2024 17:52:11 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70d1e4ac7a4sm3007836b3a.136.2024.07.22.17.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 17:52:11 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 2/6] hwmon: (max6697) Drop platform data support Date: Mon, 22 Jul 2024 17:52:00 -0700 Message-Id: <20240723005204.1356501-3-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723005204.1356501-1-linux@roeck-us.net> References: <20240723005204.1356501-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Platform data is not used anywhere in the upstram kernel. Drop support for it to simplify code maintenance. Signed-off-by: Guenter Roeck --- drivers/hwmon/max6697.c | 169 ++++++++++++-------------- include/linux/platform_data/max6697.h | 33 ----- 2 files changed, 76 insertions(+), 126 deletions(-) delete mode 100644 include/linux/platform_data/max6697.h diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index 6745d311dcf2..1ad4bf31cd24 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c @@ -17,8 +17,6 @@ #include #include -#include - enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694, max6697, max6698, max6699 }; @@ -558,54 +556,92 @@ static const struct attribute_group max6697_group = { }; __ATTRIBUTE_GROUPS(max6697); -static void max6697_get_config_of(struct device_node *node, - struct max6697_platform_data *pdata) +static int max6697_config_of(struct max6697_data *data, struct i2c_client *client) { - int len; - const __be32 *prop; + const struct max6697_chip_data *chip = data->chip; + struct device_node *node = client->dev.of_node; + int ret, confreg; + int factor = 0; + u32 vals[2]; - pdata->smbus_timeout_disable = - of_property_read_bool(node, "smbus-timeout-disable"); - pdata->extended_range_enable = - of_property_read_bool(node, "extended-range-enable"); - pdata->beta_compensation = - of_property_read_bool(node, "beta-compensation-enable"); + confreg = 0; + if (of_property_read_bool(node, "smbus-timeout-disable") && + (data->chip->valid_conf & MAX6697_CONF_TIMEOUT)) { + confreg |= MAX6697_CONF_TIMEOUT; + } + if (of_property_read_bool(node, "extended-range-enable") && + (chip->valid_conf & MAX6581_CONF_EXTENDED)) { + confreg |= MAX6581_CONF_EXTENDED; + data->temp_offset = 64; + } + if (of_property_read_bool(node, "beta-compensation-enable") && + (chip->valid_conf & MAX6693_CONF_BETA)) { + confreg |= MAX6693_CONF_BETA; + } - prop = of_get_property(node, "alert-mask", &len); - if (prop && len == sizeof(u32)) - pdata->alert_mask = be32_to_cpu(prop[0]); - prop = of_get_property(node, "over-temperature-mask", &len); - if (prop && len == sizeof(u32)) - pdata->over_temperature_mask = be32_to_cpu(prop[0]); - prop = of_get_property(node, "resistance-cancellation", &len); - if (prop) { - if (len == sizeof(u32)) - pdata->resistance_cancellation = be32_to_cpu(prop[0]); - else - pdata->resistance_cancellation = 0xfe; - } - prop = of_get_property(node, "transistor-ideality", &len); - if (prop && len == 2 * sizeof(u32)) { - pdata->ideality_mask = be32_to_cpu(prop[0]); - pdata->ideality_value = be32_to_cpu(prop[1]); + if (of_property_read_u32(node, "alert-mask", vals)) + vals[0] = 0; + ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK, + MAX6697_ALERT_MAP_BITS(vals[0])); + if (ret) + return ret; + + if (of_property_read_u32(node, "over-temperature-mask", vals)) + vals[0] = 0; + ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK, + MAX6697_OVERT_MAP_BITS(vals[0])); + if (ret) + return ret; + + if (data->type == max6581) { + if (of_property_read_bool(node, "resistance-cancellation") && + chip->valid_conf & MAX6697_CONF_RESISTANCE) { + confreg |= MAX6697_CONF_RESISTANCE; + factor = 1; + } + } else { + if (of_property_read_u32(node, "resistance-cancellation", &vals[0])) + vals[0] = 0xfe; + + factor = hweight8(vals[0] & 0xfe); + ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE, + vals[0] >> 1); + if (ret < 0) + return ret; + + if (of_property_read_u32_array(node, "transistor-ideality", vals, 2)) { + vals[0] = 0; + vals[1] = 0; + } + + ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY, + vals[1]); + if (ret < 0) + return ret; + ret = i2c_smbus_write_byte_data(client, + MAX6581_REG_IDEALITY_SELECT, + vals[0] >> 1); + if (ret < 0) + return ret; } + ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, confreg); + if (ret < 0) + return ret; + return factor; } static int max6697_init_chip(struct max6697_data *data, struct i2c_client *client) { - struct max6697_platform_data *pdata = dev_get_platdata(&client->dev); - struct max6697_platform_data p; const struct max6697_chip_data *chip = data->chip; int factor = chip->channels; int ret, reg; /* - * Don't touch configuration if neither platform data nor OF - * configuration was specified. If that is the case, use the - * current chip configuration. + * Don't touch configuration if there is no devicetree configuration. + * If that is the case, use the current chip configuration. */ - if (!pdata && !client->dev.of_node) { + if (!client->dev.of_node) { reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG); if (reg < 0) return reg; @@ -621,67 +657,14 @@ static int max6697_init_chip(struct max6697_data *data, if (reg & MAX6697_CONF_RESISTANCE) factor++; } - goto done; - } - - if (client->dev.of_node) { - memset(&p, 0, sizeof(p)); - max6697_get_config_of(client->dev.of_node, &p); - pdata = &p; - } - - reg = 0; - if (pdata->smbus_timeout_disable && - (chip->valid_conf & MAX6697_CONF_TIMEOUT)) { - reg |= MAX6697_CONF_TIMEOUT; - } - if (pdata->extended_range_enable && - (chip->valid_conf & MAX6581_CONF_EXTENDED)) { - reg |= MAX6581_CONF_EXTENDED; - data->temp_offset = 64; - } - if (pdata->resistance_cancellation && - (chip->valid_conf & MAX6697_CONF_RESISTANCE)) { - reg |= MAX6697_CONF_RESISTANCE; - factor++; - } - if (pdata->beta_compensation && - (chip->valid_conf & MAX6693_CONF_BETA)) { - reg |= MAX6693_CONF_BETA; - } - - ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, reg); - if (ret < 0) - return ret; - - ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK, - MAX6697_ALERT_MAP_BITS(pdata->alert_mask)); - if (ret < 0) - return ret; - - ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK, - MAX6697_OVERT_MAP_BITS(pdata->over_temperature_mask)); - if (ret < 0) - return ret; - - if (data->type == max6581) { - factor += hweight8(pdata->resistance_cancellation >> 1); - ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE, - pdata->resistance_cancellation >> 1); - if (ret < 0) - return ret; - ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY, - pdata->ideality_value); - if (ret < 0) - return ret; - ret = i2c_smbus_write_byte_data(client, - MAX6581_REG_IDEALITY_SELECT, - pdata->ideality_mask >> 1); + data->update_interval = factor * MAX6697_CONV_TIME; + } else { + ret = max6697_config_of(data, client); if (ret < 0) return ret; + data->update_interval = (factor + ret) * MAX6697_CONV_TIME; } -done: - data->update_interval = factor * MAX6697_CONV_TIME; + return 0; } diff --git a/include/linux/platform_data/max6697.h b/include/linux/platform_data/max6697.h deleted file mode 100644 index 6fbb70005541..000000000000 --- a/include/linux/platform_data/max6697.h +++ /dev/null @@ -1,33 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * max6697.h - * Copyright (c) 2012 Guenter Roeck - */ - -#ifndef MAX6697_H -#define MAX6697_H - -#include - -/* - * For all bit masks: - * bit 0: local temperature - * bit 1..7: remote temperatures - */ -struct max6697_platform_data { - bool smbus_timeout_disable; /* set to disable SMBus timeouts */ - bool extended_range_enable; /* set to enable extended temp range */ - bool beta_compensation; /* set to enable beta compensation */ - u8 alert_mask; /* set bit to 1 to disable alert */ - u8 over_temperature_mask; /* set bit to 1 to disable */ - u8 resistance_cancellation; /* set bit to 0 to disable - * bit mask for MAX6581, - * boolean for other chips - */ - u8 ideality_mask; /* set bit to 0 to disable */ - u8 ideality_value; /* transistor ideality as per - * MAX6581 datasheet - */ -}; - -#endif /* MAX6697_H */ From patchwork Tue Jul 23 00:52:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13739255 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD9AC131BAF for ; Tue, 23 Jul 2024 00:52:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695936; cv=none; b=hOwSjJKyKOM4Jr6V+MtXTgUQnMKbqd/o1DImmwArIsprQ+uHYWwq+UrqxoU3qLzHqshh0AZGWLvyUAITaNMdrGdfK4MKp9jV2rSEzSRwV0gL2xomUjqpsOiR/fp0OVDF0DzS5fQZVmy/K+36qjmn01VP+4g8D3Sxfm4bDqtuEIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695936; c=relaxed/simple; bh=BHZB4L5lNzrboAlkDVv/pos9dKkQxocz+wUb+KT8Pto=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ewh+Z+WjMTJJZblWxlGDm7QQvJUhzUDQwI3fk6MsjrXJbjN7KdvzPgIA6YIzVkA7EotabBepjod+LuSjITDB73MF40MnRNTX40td90dYLtPXxBhRyGApB7Esa6sp6zvMtk3GlRJ1Cuk+Qa3KN80NsEw4YBJfPsdrFVlx69966vg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ANIW2Ib5; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ANIW2Ib5" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1fc56fd4de1so2113985ad.0 for ; Mon, 22 Jul 2024 17:52:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721695934; x=1722300734; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=KHQEhT1ez21e4J803kL8dmYORJYup/GJSKEllK+ooms=; b=ANIW2Ib5dW6QSZ9eisQDRSkKcX1bBYC4qrphC2+YXIJnI2EwqN38AmcDDytw9ChHcJ zoyBLiSoOEglXZkqAmzBZWtjFrLnmViinGRghCPCYS6QyDM5c8ZPAKkzKMsaHAxWbvuZ aGsaz4j9loJvZW9fc0psA8SF1LRDkMoTJG/ohAhRkEhLqVfP6j1m7vNkKdysQtWnKHNN 0ok6QsqMia7gFcA3GWqhJ5xdu4GR8xSyFYQe1907I6Xql1Nh1Ms8Byc75JC5xHqP4i+/ iAEYtnbGT3IsexI30lW6aCUPs0J5Y8+Ty0rcYZ8MJ87K8UmC7fZhGUazF/MIHBF9pqsZ dSJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721695934; x=1722300734; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=KHQEhT1ez21e4J803kL8dmYORJYup/GJSKEllK+ooms=; b=ZZQyfHn5gz0hulihkFJXftvsJ0j4WI/liKZx1+kjrte/ke0KF/T2p9fxQPBLOixpIG nMG2tlMzZ+kUliNEHETxZQdJgmyhSaTnqaUtP038GgKD0BqVyRcGBDYrjojiFadhH14z kprtmu0hhVyHhVMtKoRx6OfJcA8FrU9vo7bXbMQ3NnvzOu1v4mTt13qeeo6d/h7eMYX9 axZmwwbmMZMH+Rym8fhLwTs3micC5+UJsjmlgTsIPeRZsbQXBAGr+ssAVZHd5kjqhMFo lOZ2FhnxgZv8VHn0EicPNk8Zt7apx9EgfsuCuCJn8zUaT45z05egWWt8RQ2vmlA6VMSK /TzQ== X-Gm-Message-State: AOJu0YzVdqoe54BIR2cj9o7eyeZC10Cef/vskh6PeV84SBOvZJt7CDjt +zhYA0Rhfz4wwMJ5RzeGcJbFdxAj2zJ+E7sAeNdWIYZpz4ZGXqS7cE1TOQ== X-Google-Smtp-Source: AGHT+IF7pca+cse6X2CzenFwWiiS9YVtc3CBOSxC58E2GOsl06+sipzJMNCuV0LCkU46+AqZo7FA7A== X-Received: by 2002:a17:903:41cd:b0:1fb:a38b:c5b7 with SMTP id d9443c01a7336-1fdb94d1959mr9012245ad.1.1721695933643; Mon, 22 Jul 2024 17:52:13 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fd6f4479a1sm61371105ad.207.2024.07.22.17.52.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 17:52:13 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 3/6] hwmon: (max6697) Use bit operations where possible Date: Mon, 22 Jul 2024 17:52:01 -0700 Message-Id: <20240723005204.1356501-4-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723005204.1356501-1-linux@roeck-us.net> References: <20240723005204.1356501-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use bit operations to improve code maintainability. Signed-off-by: Guenter Roeck --- drivers/hwmon/max6697.c | 43 +++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index 1ad4bf31cd24..d03881ea90b8 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c @@ -6,6 +6,8 @@ * Copyright (c) 2011 David George */ +#include +#include #include #include #include @@ -32,20 +34,31 @@ static const u8 MAX6697_REG_CRIT[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 }; /* - * Map device tree / platform data register bit map to chip bit map. + * Map device tree / internal register bit map to chip bit map. * Applies to alert register and over-temperature register. */ + +#define MAX6697_EXTERNAL_MASK_DT GENMASK(7, 1) +#define MAX6697_LOCAL_MASK_DT BIT(0) +#define MAX6697_EXTERNAL_MASK_CHIP GENMASK(6, 0) +#define MAX6697_LOCAL_MASK_CHIP BIT(7) + +/* alert - local channel is in bit 6 */ #define MAX6697_ALERT_MAP_BITS(reg) ((((reg) & 0x7e) >> 1) | \ (((reg) & 0x01) << 6) | ((reg) & 0x80)) -#define MAX6697_OVERT_MAP_BITS(reg) (((reg) >> 1) | (((reg) & 0x01) << 7)) + +/* over-temperature - local channel is in bit 7 */ +#define MAX6697_OVERT_MAP_BITS(reg) \ + (FIELD_PREP(MAX6697_EXTERNAL_MASK_CHIP, FIELD_GET(MAX6697_EXTERNAL_MASK_DT, reg)) | \ + FIELD_PREP(MAX6697_LOCAL_MASK_CHIP, FIELD_GET(MAX6697_LOCAL_MASK_DT, reg))) #define MAX6697_REG_STAT(n) (0x44 + (n)) #define MAX6697_REG_CONFIG 0x41 -#define MAX6581_CONF_EXTENDED (1 << 1) -#define MAX6693_CONF_BETA (1 << 2) -#define MAX6697_CONF_RESISTANCE (1 << 3) -#define MAX6697_CONF_TIMEOUT (1 << 5) +#define MAX6581_CONF_EXTENDED BIT(1) +#define MAX6693_CONF_BETA BIT(2) +#define MAX6697_CONF_RESISTANCE BIT(3) +#define MAX6697_CONF_TIMEOUT BIT(5) #define MAX6697_REG_ALERT_MASK 0x42 #define MAX6697_REG_OVERT_MASK 0x43 @@ -193,7 +206,7 @@ static struct max6697_data *max6697_update_device(struct device *dev) goto abort; for (i = 0; i < data->chip->channels; i++) { - if (data->chip->have_ext & (1 << i)) { + if (data->chip->have_ext & BIT(i)) { val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP_EXT[i]); if (unlikely(val < 0)) { @@ -217,7 +230,7 @@ static struct max6697_data *max6697_update_device(struct device *dev) } data->temp[i][MAX6697_TEMP_MAX] = val; - if (data->chip->have_crit & (1 << i)) { + if (data->chip->have_crit & BIT(i)) { val = i2c_smbus_read_byte_data(client, MAX6697_REG_CRIT[i]); if (unlikely(val < 0)) { @@ -291,7 +304,7 @@ static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, if (data->chip->alarm_map) index = data->chip->alarm_map[index]; - return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1); + return sprintf(buf, "%u\n", !!(data->alarms & BIT(index))); } static ssize_t temp_store(struct device *dev, @@ -342,20 +355,20 @@ static ssize_t offset_store(struct device *dev, struct device_attribute *devattr ret = select; goto abort; } - channel_enabled = (select & (1 << (index - 1))); + channel_enabled = (select & BIT(index - 1)); temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX); val = DIV_ROUND_CLOSEST(temp, 250); /* disable the offset for channel if the new offset is 0 */ if (val == 0) { if (channel_enabled) ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT, - select & ~(1 << (index - 1))); + select & ~BIT(index - 1)); ret = ret < 0 ? ret : count; goto abort; } if (!channel_enabled) { ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT, - select | (1 << (index - 1))); + select | BIT(index - 1)); if (ret < 0) goto abort; } @@ -378,7 +391,7 @@ static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT); if (select < 0) ret = select; - else if (select & (1 << (index - 1))) + else if (select & BIT(index - 1)) ret = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET); else ret = 0; @@ -467,9 +480,9 @@ static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr, if (channel >= chip->channels) return 0; - if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel))) + if ((nr == 3 || nr == 4) && !(chip->have_crit & BIT(channel))) return 0; - if (nr == 5 && !(chip->have_fault & (1 << channel))) + if (nr == 5 && !(chip->have_fault & BIT(channel))) return 0; /* offset reg is only supported on max6581 remote channels */ if (nr == 6) From patchwork Tue Jul 23 00:52:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13739256 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB5C41311A7 for ; Tue, 23 Jul 2024 00:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695938; cv=none; b=MyfYBpL45reCCrircSEWt/fdDOQLF6y0nXr1Y82UmFTVDF9ju0/cew9NxgMfjDTuIfW646Xkd2nypKxhuH1Ls0eADT6cYM/8zukZ0vOwabMQDBUtS7XveqQHX8R/d33Ie2j3usVkUovioEgs+K/yydIML3WnstU6WXrEJBRPIPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695938; c=relaxed/simple; bh=sV/SoI+XPMxPxxN9lWPHM5UFQtC7cQ9qVYYNtLNooz4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HKuzP0YGMUFub/pvDUmdNi/89bWQh0kqQWxxQchDZlmuqBObaPstMoFQeAar8lfkh4gyGvyKWiyklPOLgc9c/KnK2ccELqk1PBzwjgU+6Ex4vrcA7iRxvUht+VfBC6nDiBrtX73K6es0iBZBAxQLZz0jozYuB7kERxb5/uNaMlg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QiBY3zmv; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QiBY3zmv" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-70d399da0b5so539829b3a.3 for ; Mon, 22 Jul 2024 17:52:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721695935; x=1722300735; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=xrhvC33Nu8GyfIMBH4OXC5V6/21dHQNBbsPca9CHbIM=; b=QiBY3zmvDaOey/mVSQCOQMiNMacfy7EHV3NY+nDmGDys5TDLGUIOOzAUMqBAWDSs72 ik0VjARrT61jNpliuUXk3jrfaDZd8W0K2z71Fg+WyENZN9FCa062dTk4ugQrFlB3Ue+W KqAzP6/xQr/Aj925dh35KiboUE/ko4/QXPm6YMJtb40Yb5Vt0gHlB/alYClxTVEaL/fe rZ9R9kVFZ2RvqMcM1YZOz7RQzOZVzTmr0CzGKU2mVRCfnE42Vl7wH/UuWBHXbB/++SsG eEr0YYWQibHxHXOeDWLXssQjV2oQbnJmARhUJ3QNG2I8RSq08X5Al3eFwqCRJI5uuHG+ HGbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721695935; x=1722300735; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xrhvC33Nu8GyfIMBH4OXC5V6/21dHQNBbsPca9CHbIM=; b=iNzK7OMwy9I11tvyJslyLuy5HbUdopP6ZA+merPXshouSi+hk6m0715t3Y1124dACp +zR6Di76zVA6e3UXs4wi9Qn2iXqkM+cn75PAItohvHmBqt+lHmzsHulyRHVYw4q5fd7C SZxZJvQlBMoYz6SOVO/7Nh0Nrz+nzELfk+lU9WEVnhN9T7hw/3AVCG61GPM8WWOKXmcA KjshmSTi+CE64dSVzEBbINpuHqKGWUmHl9QixiwPbvJIT6X9JRmY77bELdfKg/mOc4li e6/ezCD2OMXX38KUQeWtv+bQ7dwH6Udt/EuLlFmt8+71mMvo143APB/C4iBne1Q89nts abtw== X-Gm-Message-State: AOJu0YzG7EPncE9CwIO/bBe4GVgK57emSrBL6xNrd368a2qVFaKwhI5c d2f44mK2Juho4j68mH2/fcEZ1mU+5c6L40De7SbHvNEVwj2IqOA+yeACPQ== X-Google-Smtp-Source: AGHT+IElZvUyHi6DhPI/yhe/G4de47Gck7JDdLdc/vEVVMWmrfrGzMmnUBM3f6wB+dEuyXY9+YquNQ== X-Received: by 2002:a05:6a00:1acf:b0:70d:21d9:e2ae with SMTP id d2e1a72fcca58-70d21d9e8b2mr5688840b3a.6.1721695935321; Mon, 22 Jul 2024 17:52:15 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70d14d41bc1sm3881606b3a.105.2024.07.22.17.52.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 17:52:14 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 4/6] hwmon: (max6697) Convert to use regmap Date: Mon, 22 Jul 2024 17:52:02 -0700 Message-Id: <20240723005204.1356501-5-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723005204.1356501-1-linux@roeck-us.net> References: <20240723005204.1356501-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use regmap for register caching, and use regmap API for bit operations to simplify the code. This patch reduces object file size by approximately 10%. Signed-off-by: Guenter Roeck Reviewed-by: Tzung-Bi Shih --- drivers/hwmon/max6697.c | 317 +++++++++++++++------------------------- 1 file changed, 121 insertions(+), 196 deletions(-) diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index d03881ea90b8..865ac36e3629 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c @@ -12,11 +12,11 @@ #include #include #include -#include #include #include #include #include +#include #include enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694, @@ -82,20 +82,15 @@ struct max6697_chip_data { }; struct max6697_data { - struct i2c_client *client; + struct regmap *regmap; enum chips type; const struct max6697_chip_data *chip; - int update_interval; /* in milli-seconds */ int temp_offset; /* in degrees C */ struct mutex update_lock; - unsigned long last_updated; /* In jiffies */ - bool valid; /* true if following fields are valid */ - /* 1x local and up to 7x remote */ - u8 temp[8][4]; /* [nr][0]=temp [1]=ext [2]=max [3]=crit */ #define MAX6697_TEMP_INPUT 0 #define MAX6697_TEMP_EXT 1 #define MAX6697_TEMP_MAX 2 @@ -189,88 +184,22 @@ static inline int max6581_offset_to_millic(int val) return sign_extend32(val, 7) * 250; } -static struct max6697_data *max6697_update_device(struct device *dev) -{ - struct max6697_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; - struct max6697_data *ret = data; - int val; - int i; - u32 alarms; - - mutex_lock(&data->update_lock); - - if (data->valid && - !time_after(jiffies, data->last_updated - + msecs_to_jiffies(data->update_interval))) - goto abort; - - for (i = 0; i < data->chip->channels; i++) { - if (data->chip->have_ext & BIT(i)) { - val = i2c_smbus_read_byte_data(client, - MAX6697_REG_TEMP_EXT[i]); - if (unlikely(val < 0)) { - ret = ERR_PTR(val); - goto abort; - } - data->temp[i][MAX6697_TEMP_EXT] = val; - } - - val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP[i]); - if (unlikely(val < 0)) { - ret = ERR_PTR(val); - goto abort; - } - data->temp[i][MAX6697_TEMP_INPUT] = val; - - val = i2c_smbus_read_byte_data(client, MAX6697_REG_MAX[i]); - if (unlikely(val < 0)) { - ret = ERR_PTR(val); - goto abort; - } - data->temp[i][MAX6697_TEMP_MAX] = val; - - if (data->chip->have_crit & BIT(i)) { - val = i2c_smbus_read_byte_data(client, - MAX6697_REG_CRIT[i]); - if (unlikely(val < 0)) { - ret = ERR_PTR(val); - goto abort; - } - data->temp[i][MAX6697_TEMP_CRIT] = val; - } - } - - alarms = 0; - for (i = 0; i < 3; i++) { - val = i2c_smbus_read_byte_data(client, MAX6697_REG_STAT(i)); - if (unlikely(val < 0)) { - ret = ERR_PTR(val); - goto abort; - } - alarms = (alarms << 8) | val; - } - data->alarms = alarms; - data->last_updated = jiffies; - data->valid = true; -abort: - mutex_unlock(&data->update_lock); - - return ret; -} - static ssize_t temp_input_show(struct device *dev, struct device_attribute *devattr, char *buf) { + struct max6697_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr(devattr)->index; - struct max6697_data *data = max6697_update_device(dev); - int temp; + unsigned int regs[2] = { MAX6697_REG_TEMP[index], + MAX6697_REG_TEMP_EXT[index] }; + u8 regdata[2] = { }; + int temp, ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_multi_reg_read(data->regmap, regs, regdata, + data->chip->have_ext & BIT(index) ? 2 : 1); + if (ret) + return ret; - temp = (data->temp[index][MAX6697_TEMP_INPUT] - data->temp_offset) << 3; - temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5; + temp = ((regdata[0] - data->temp_offset) << 3) | (regdata[1] >> 5); return sprintf(buf, "%d\n", temp * 125); } @@ -278,33 +207,41 @@ static ssize_t temp_input_show(struct device *dev, static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, char *buf) { - int nr = to_sensor_dev_attr_2(devattr)->nr; + struct max6697_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr_2(devattr)->index; - struct max6697_data *data = max6697_update_device(dev); - int temp; + int nr = to_sensor_dev_attr_2(devattr)->nr; + unsigned int temp; + int reg, ret; - if (IS_ERR(data)) - return PTR_ERR(data); + if (index == MAX6697_TEMP_MAX) + reg = MAX6697_REG_MAX[nr]; + else + reg = MAX6697_REG_CRIT[nr]; - temp = data->temp[nr][index]; - temp -= data->temp_offset; + ret = regmap_read(data->regmap, reg, &temp); + if (ret) + return ret; - return sprintf(buf, "%d\n", temp * 1000); + return sprintf(buf, "%d\n", ((int)temp - data->temp_offset) * 1000); } static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, char *buf) { + struct max6697_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr(attr)->index; - struct max6697_data *data = max6697_update_device(dev); - - if (IS_ERR(data)) - return PTR_ERR(data); + unsigned int alarms; + int reg, ret; if (data->chip->alarm_map) index = data->chip->alarm_map[index]; - return sprintf(buf, "%u\n", !!(data->alarms & BIT(index))); + reg = MAX6697_REG_STAT(2 - (index / 8)); + ret = regmap_read(data->regmap, reg, &alarms); + if (ret) + return ret; + + return sprintf(buf, "%u\n", !!(alarms & BIT(index & 7))); } static ssize_t temp_store(struct device *dev, @@ -321,82 +258,63 @@ static ssize_t temp_store(struct device *dev, if (ret < 0) return ret; - mutex_lock(&data->update_lock); temp = clamp_val(temp, -1000000, 1000000); /* prevent underflow */ temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset; temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127); - data->temp[nr][index] = temp; - ret = i2c_smbus_write_byte_data(data->client, - index == 2 ? MAX6697_REG_MAX[nr] - : MAX6697_REG_CRIT[nr], - temp); - mutex_unlock(&data->update_lock); + ret = regmap_write(data->regmap, + index == 2 ? MAX6697_REG_MAX[nr] + : MAX6697_REG_CRIT[nr], + temp); - return ret < 0 ? ret : count; + return ret ? : count; } static ssize_t offset_store(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { - int val, ret, index, select; - struct max6697_data *data; - bool channel_enabled; + struct max6697_data *data = dev_get_drvdata(dev); + int index = to_sensor_dev_attr(devattr)->index; + struct regmap *regmap = data->regmap; long temp; + int ret; - index = to_sensor_dev_attr(devattr)->index; - data = dev_get_drvdata(dev); ret = kstrtol(buf, 10, &temp); if (ret < 0) return ret; mutex_lock(&data->update_lock); - select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT); - if (select < 0) { - ret = select; - goto abort; - } - channel_enabled = (select & BIT(index - 1)); temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX); - val = DIV_ROUND_CLOSEST(temp, 250); - /* disable the offset for channel if the new offset is 0 */ - if (val == 0) { - if (channel_enabled) - ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT, - select & ~BIT(index - 1)); - ret = ret < 0 ? ret : count; - goto abort; + temp = DIV_ROUND_CLOSEST(temp, 250); + if (!temp) { /* disable this (and only this) channel */ + ret = regmap_clear_bits(regmap, MAX6581_REG_OFFSET_SELECT, BIT(index - 1)); + goto unlock; } - if (!channel_enabled) { - ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT, - select | BIT(index - 1)); - if (ret < 0) - goto abort; - } - ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET, val); - ret = ret < 0 ? ret : count; - -abort: + /* enable channel, and update offset */ + ret = regmap_set_bits(regmap, MAX6581_REG_OFFSET_SELECT, BIT(index - 1)); + if (ret) + goto unlock; + ret = regmap_write(regmap, MAX6581_REG_OFFSET, temp); +unlock: mutex_unlock(&data->update_lock); - return ret; + return ret ? : count; } static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, char *buf) { - struct max6697_data *data; - int select, ret, index; + unsigned int regs[2] = { MAX6581_REG_OFFSET_SELECT, MAX6581_REG_OFFSET }; + struct max6697_data *data = dev_get_drvdata(dev); + int index = to_sensor_dev_attr(devattr)->index; + u8 regdata[2]; + int ret; - index = to_sensor_dev_attr(devattr)->index; - data = dev_get_drvdata(dev); - mutex_lock(&data->update_lock); - select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT); - if (select < 0) - ret = select; - else if (select & BIT(index - 1)) - ret = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET); - else - ret = 0; - mutex_unlock(&data->update_lock); - return ret < 0 ? ret : sprintf(buf, "%d\n", max6581_offset_to_millic(ret)); + ret = regmap_multi_reg_read(data->regmap, regs, regdata, 2); + if (ret) + return ret; + + if (!(regdata[0] & BIT(index - 1))) + regdata[1] = 0; + + return sprintf(buf, "%d\n", max6581_offset_to_millic(regdata[1])); } static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); @@ -569,12 +487,11 @@ static const struct attribute_group max6697_group = { }; __ATTRIBUTE_GROUPS(max6697); -static int max6697_config_of(struct max6697_data *data, struct i2c_client *client) +static int max6697_config_of(struct device_node *node, struct max6697_data *data) { const struct max6697_chip_data *chip = data->chip; - struct device_node *node = client->dev.of_node; + struct regmap *regmap = data->regmap; int ret, confreg; - int factor = 0; u32 vals[2]; confreg = 0; @@ -594,15 +511,15 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien if (of_property_read_u32(node, "alert-mask", vals)) vals[0] = 0; - ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK, - MAX6697_ALERT_MAP_BITS(vals[0])); + ret = regmap_write(regmap, MAX6697_REG_ALERT_MASK, + MAX6697_ALERT_MAP_BITS(vals[0])); if (ret) return ret; if (of_property_read_u32(node, "over-temperature-mask", vals)) vals[0] = 0; - ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK, - MAX6697_OVERT_MAP_BITS(vals[0])); + ret = regmap_write(regmap, MAX6697_REG_OVERT_MASK, + MAX6697_OVERT_MAP_BITS(vals[0])); if (ret) return ret; @@ -610,15 +527,12 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien if (of_property_read_bool(node, "resistance-cancellation") && chip->valid_conf & MAX6697_CONF_RESISTANCE) { confreg |= MAX6697_CONF_RESISTANCE; - factor = 1; } } else { if (of_property_read_u32(node, "resistance-cancellation", &vals[0])) vals[0] = 0xfe; - factor = hweight8(vals[0] & 0xfe); - ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE, - vals[0] >> 1); + ret = regmap_write(regmap, MAX6581_REG_RESISTANCE, vals[0] >> 1); if (ret < 0) return ret; @@ -627,81 +541,92 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien vals[1] = 0; } - ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY, - vals[1]); + ret = regmap_write(regmap, MAX6581_REG_IDEALITY, vals[1]); if (ret < 0) return ret; - ret = i2c_smbus_write_byte_data(client, - MAX6581_REG_IDEALITY_SELECT, - vals[0] >> 1); + ret = regmap_write(regmap, MAX6581_REG_IDEALITY_SELECT, + vals[0] >> 1); if (ret < 0) return ret; } - ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, confreg); - if (ret < 0) - return ret; - return factor; + return regmap_write(regmap, MAX6697_REG_CONFIG, confreg); } -static int max6697_init_chip(struct max6697_data *data, - struct i2c_client *client) +static int max6697_init_chip(struct device_node *np, struct max6697_data *data) { - const struct max6697_chip_data *chip = data->chip; - int factor = chip->channels; - int ret, reg; + unsigned int reg; + int ret; /* * Don't touch configuration if there is no devicetree configuration. * If that is the case, use the current chip configuration. */ - if (!client->dev.of_node) { - reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG); - if (reg < 0) - return reg; + if (!np) { + struct regmap *regmap = data->regmap; + + ret = regmap_read(regmap, MAX6697_REG_CONFIG, ®); + if (ret < 0) + return ret; if (data->type == max6581) { if (reg & MAX6581_CONF_EXTENDED) data->temp_offset = 64; - reg = i2c_smbus_read_byte_data(client, - MAX6581_REG_RESISTANCE); - if (reg < 0) - return reg; - factor += hweight8(reg); - } else { - if (reg & MAX6697_CONF_RESISTANCE) - factor++; + ret = regmap_read(regmap, MAX6581_REG_RESISTANCE, ®); } - data->update_interval = factor * MAX6697_CONV_TIME; } else { - ret = max6697_config_of(data, client); - if (ret < 0) - return ret; - data->update_interval = (factor + ret) * MAX6697_CONV_TIME; + ret = max6697_config_of(np, data); } - return 0; + return ret; } +static bool max6697_volatile_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case 0x00 ... 0x09: /* temperature high bytes */ + case 0x44 ... 0x47: /* status */ + case 0x51 ... 0x58: /* temperature low bytes */ + return true; + default: + return false; + } +} + +static bool max6697_writeable_reg(struct device *dev, unsigned int reg) +{ + return reg != 0x0a && reg != 0x0f && !max6697_volatile_reg(dev, reg); +} + +static const struct regmap_config max6697_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0x58, + .writeable_reg = max6697_writeable_reg, + .volatile_reg = max6697_volatile_reg, + .cache_type = REGCACHE_MAPLE, +}; + static int max6697_probe(struct i2c_client *client) { - struct i2c_adapter *adapter = client->adapter; struct device *dev = &client->dev; struct max6697_data *data; struct device *hwmon_dev; + struct regmap *regmap; int err; - if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) - return -ENODEV; + regmap = regmap_init_i2c(client, &max6697_regmap_config); + if (IS_ERR(regmap)) + return PTR_ERR(regmap); data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL); if (!data) return -ENOMEM; + data->regmap = regmap; data->type = (uintptr_t)i2c_get_match_data(client); data->chip = &max6697_chip_data[data->type]; - data->client = client; mutex_init(&data->update_lock); - err = max6697_init_chip(data, client); + err = max6697_init_chip(client->dev.of_node, data); if (err) return err; From patchwork Tue Jul 23 00:52:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13739257 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 798A212E1DB for ; Tue, 23 Jul 2024 00:52:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695940; cv=none; b=A1uoi8xJWZXx2YJXtwW7ZmqY0OcZXHqM6+g3HeUSliqVttXx/jhdExr4tLYTP3ZDVVT4Do1tmI47jEWZOewFjVbhmVkayjbaB2PqrOvQBLd5/rYkTq6mTkUuODVBx9HLnPZbzGnKawPhDZZadGbMCHWUOaHWanP3vrruqHXngA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695940; c=relaxed/simple; bh=Mig8QcwFWoDroYu+Xdf2YQJZ5H4d3iPODRiQQWKTqho=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eJQpYM+FELdULPnbKND8wKbn4rkEvivooJz36aYBy8dW76Dkd/0QhbGhyMyT3GYbYHwdtsgHnphx8T5r9F7KC+r9/2U9wKmPh+VcPmz0F6/5bcmvXo/jo/IUV1Lb6v/fmp5kBO6Z5M6GdGnVTwqEUNf3LNqgRFiuthroHIh40Hk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=clhad67q; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="clhad67q" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fb53bfb6easo900645ad.2 for ; Mon, 22 Jul 2024 17:52:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721695937; x=1722300737; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=6ZxEa/J2i/6ZygvYSqgyQt1fiMm6Jz5W1CfdBL1xp7o=; b=clhad67qXR0j1Nk7fvpsQX9h9Ybfm57BwShwCyGO7S0TOfN7/cYZTv5yJf1ya3xayP zJd+Cglsk1Zog56REiG7JFZPtWSO/xg5DBxNZ4aZwm8iN3QH0L4agHgxshup+UTrAoCr eLuwhZ7dmw3adoujBDXcosD33XzOOnPbyiLG6T2Q4K1YnGk192SrXUa2ulOGd7HER53/ 9X+yOGlCG/rRdm7O22L2qmJf7nT+TBhOqcaQX+SZlxqLM7xnk4cZfTyiUbqzl1YXz76m 0nOq+Kj2DT8rYxXwii/57xblkGu0uM9QSyQv7aMCjtgnpx2aJ5CoSbTeMmAlxnAu5mWo 2zBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721695937; x=1722300737; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=6ZxEa/J2i/6ZygvYSqgyQt1fiMm6Jz5W1CfdBL1xp7o=; b=p7uCMw0Gep/1fjG9+vuPTBUwKvpy5o5mYcdqPuk+33gJuezIvpOrFjaqQY/f4ft/L4 kBFQH9EyoYOFI7DoIcTH6t/gcDlc9Ya28iC7HzDK+xZt60DY+xnHQWwCIm0ua5zwWSla UtSJUz3JxzHDAUI3nl4EnR/s8xt4kKbDWg9ra063TWA9V0q3qVVNBkMUoFhrVN1yf4uE 67kua4im30JUH9vzXufirRB1kJOcxiEwGxCP0HIU90vyvOJ7Eo7H/0xBbo2+LawPpCrD HtJ7tY/1wmox8jda3Qb4unJyEibRv3odi/SiFJNf5Rw1igLHYwNWPvzkze/d0WcFfTxT ruWw== X-Gm-Message-State: AOJu0YyEbCAtyztSlfRQH6MT09tuWbiUjFpnhHmJMZwlUp/pXLtN+8eH 2TGnxceJIyyPGxQIz6lblBnDhrzInKV1LuvSrHPFjKsqcvZ//4vMhol1xQ== X-Google-Smtp-Source: AGHT+IFLwMJzoVrCg+Mmi1/wvr6QHW7k/jTpvnNYNYocm21grzaB0HS9/6vpZ/5kop7jZbY0zYZe1Q== X-Received: by 2002:a17:902:d488:b0:1fb:58b8:2fbc with SMTP id d9443c01a7336-1fd74563df5mr54335585ad.29.1721695937074; Mon, 22 Jul 2024 17:52:17 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fd6f2a3809sm61550435ad.106.2024.07.22.17.52.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 17:52:16 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 5/6] hwmon: (max6697) Convert to with_info hwmon API Date: Mon, 22 Jul 2024 17:52:03 -0700 Message-Id: <20240723005204.1356501-6-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723005204.1356501-1-linux@roeck-us.net> References: <20240723005204.1356501-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Convert to with_info hwmon API to simplify the code and reduce its size. This patch reduces object file size by approximately 25%. Signed-off-by: Guenter Roeck Reviewed-by: Tzung-Bi Shih --- drivers/hwmon/max6697.c | 464 +++++++++++++++------------------------- 1 file changed, 173 insertions(+), 291 deletions(-) diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index 865ac36e3629..fbe90ab18b10 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -52,7 +51,9 @@ static const u8 MAX6697_REG_CRIT[] = { (FIELD_PREP(MAX6697_EXTERNAL_MASK_CHIP, FIELD_GET(MAX6697_EXTERNAL_MASK_DT, reg)) | \ FIELD_PREP(MAX6697_LOCAL_MASK_CHIP, FIELD_GET(MAX6697_LOCAL_MASK_DT, reg))) -#define MAX6697_REG_STAT(n) (0x44 + (n)) +#define MAX6697_REG_STAT_ALARM 0x44 +#define MAX6697_REG_STAT_CRIT 0x45 +#define MAX6697_REG_STAT_FAULT 0x46 #define MAX6697_REG_CONFIG 0x41 #define MAX6581_CONF_EXTENDED BIT(1) @@ -78,7 +79,6 @@ struct max6697_chip_data { u32 have_crit; u32 have_fault; u8 valid_conf; - const u8 *alarm_map; }; struct max6697_data { @@ -98,11 +98,6 @@ struct max6697_data { u32 alarms; }; -/* Diode fault status bits on MAX6581 are right shifted by one bit */ -static const u8 max6581_alarm_map[] = { - 0, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23 }; - static const struct max6697_chip_data max6697_chip_data[] = { [max6581] = { .channels = 8, @@ -110,7 +105,6 @@ static const struct max6697_chip_data max6697_chip_data[] = { .have_ext = 0x7f, .have_fault = 0xfe, .valid_conf = MAX6581_CONF_EXTENDED | MAX6697_CONF_TIMEOUT, - .alarm_map = max6581_alarm_map, }, [max6602] = { .channels = 5, @@ -179,313 +173,202 @@ static const struct max6697_chip_data max6697_chip_data[] = { }, }; -static inline int max6581_offset_to_millic(int val) -{ - return sign_extend32(val, 7) * 250; -} - -static ssize_t temp_input_show(struct device *dev, - struct device_attribute *devattr, char *buf) +static int max6697_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *val) { + unsigned int offset_regs[2] = { MAX6581_REG_OFFSET_SELECT, MAX6581_REG_OFFSET }; + unsigned int temp_regs[2] = { MAX6697_REG_TEMP[channel], + MAX6697_REG_TEMP_EXT[channel] }; struct max6697_data *data = dev_get_drvdata(dev); - int index = to_sensor_dev_attr(devattr)->index; - unsigned int regs[2] = { MAX6697_REG_TEMP[index], - MAX6697_REG_TEMP_EXT[index] }; - u8 regdata[2] = { }; - int temp, ret; - - ret = regmap_multi_reg_read(data->regmap, regs, regdata, - data->chip->have_ext & BIT(index) ? 2 : 1); - if (ret) - return ret; - - temp = ((regdata[0] - data->temp_offset) << 3) | (regdata[1] >> 5); - - return sprintf(buf, "%d\n", temp * 125); -} - -static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, - char *buf) -{ - struct max6697_data *data = dev_get_drvdata(dev); - int index = to_sensor_dev_attr_2(devattr)->index; - int nr = to_sensor_dev_attr_2(devattr)->nr; - unsigned int temp; - int reg, ret; - - if (index == MAX6697_TEMP_MAX) - reg = MAX6697_REG_MAX[nr]; - else - reg = MAX6697_REG_CRIT[nr]; - - ret = regmap_read(data->regmap, reg, &temp); - if (ret) - return ret; - - return sprintf(buf, "%d\n", ((int)temp - data->temp_offset) * 1000); -} - -static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, - char *buf) -{ - struct max6697_data *data = dev_get_drvdata(dev); - int index = to_sensor_dev_attr(attr)->index; - unsigned int alarms; - int reg, ret; - - if (data->chip->alarm_map) - index = data->chip->alarm_map[index]; - - reg = MAX6697_REG_STAT(2 - (index / 8)); - ret = regmap_read(data->regmap, reg, &alarms); - if (ret) - return ret; - - return sprintf(buf, "%u\n", !!(alarms & BIT(index & 7))); -} - -static ssize_t temp_store(struct device *dev, - struct device_attribute *devattr, const char *buf, - size_t count) -{ - int nr = to_sensor_dev_attr_2(devattr)->nr; - int index = to_sensor_dev_attr_2(devattr)->index; - struct max6697_data *data = dev_get_drvdata(dev); - long temp; - int ret; - - ret = kstrtol(buf, 10, &temp); - if (ret < 0) - return ret; - - temp = clamp_val(temp, -1000000, 1000000); /* prevent underflow */ - temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset; - temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127); - ret = regmap_write(data->regmap, - index == 2 ? MAX6697_REG_MAX[nr] - : MAX6697_REG_CRIT[nr], - temp); - - return ret ? : count; -} - -static ssize_t offset_store(struct device *dev, struct device_attribute *devattr, const char *buf, - size_t count) -{ - struct max6697_data *data = dev_get_drvdata(dev); - int index = to_sensor_dev_attr(devattr)->index; struct regmap *regmap = data->regmap; - long temp; + u8 regdata[2] = { }; + u32 regval; int ret; - ret = kstrtol(buf, 10, &temp); - if (ret < 0) - return ret; + switch (attr) { + case hwmon_temp_input: + ret = regmap_multi_reg_read(regmap, temp_regs, regdata, + data->chip->have_ext & BIT(channel) ? 2 : 1); + if (ret) + return ret; + *val = (((regdata[0] - data->temp_offset) << 3) | (regdata[1] >> 5)) * 125; + break; + case hwmon_temp_max: + ret = regmap_read(regmap, MAX6697_REG_MAX[channel], ®val); + if (ret) + return ret; + *val = ((int)regval - data->temp_offset) * 1000; + break; + case hwmon_temp_crit: + ret = regmap_read(regmap, MAX6697_REG_CRIT[channel], ®val); + if (ret) + return ret; + *val = ((int)regval - data->temp_offset) * 1000; + break; + case hwmon_temp_offset: + ret = regmap_multi_reg_read(regmap, offset_regs, regdata, 2); + if (ret) + return ret; - mutex_lock(&data->update_lock); - temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX); - temp = DIV_ROUND_CLOSEST(temp, 250); - if (!temp) { /* disable this (and only this) channel */ - ret = regmap_clear_bits(regmap, MAX6581_REG_OFFSET_SELECT, BIT(index - 1)); - goto unlock; + if (!(regdata[0] & BIT(channel - 1))) + regdata[1] = 0; + + *val = sign_extend32(regdata[1], 7) * 250; + break; + case hwmon_temp_fault: + ret = regmap_read(regmap, MAX6697_REG_STAT_FAULT, ®val); + if (ret) + return ret; + if (data->type == max6581) + *val = !!(regval & BIT(channel - 1)); + else + *val = !!(regval & BIT(channel)); + break; + case hwmon_temp_crit_alarm: + ret = regmap_read(regmap, MAX6697_REG_STAT_CRIT, ®val); + if (ret) + return ret; + *val = !!(regval & BIT(channel ? channel - 1 : 7)); + break; + case hwmon_temp_max_alarm: + ret = regmap_read(regmap, MAX6697_REG_STAT_ALARM, ®val); + if (ret) + return ret; + switch (channel) { + case 0: + *val = !!(regval & BIT(6)); + break; + case 7: + *val = !!(regval & BIT(7)); + break; + default: + *val = !!(regval & BIT(channel - 1)); + break; + } + break; + default: + return -EOPNOTSUPP; } - /* enable channel, and update offset */ - ret = regmap_set_bits(regmap, MAX6581_REG_OFFSET_SELECT, BIT(index - 1)); - if (ret) - goto unlock; - ret = regmap_write(regmap, MAX6581_REG_OFFSET, temp); -unlock: - mutex_unlock(&data->update_lock); - return ret ? : count; + return 0; } -static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, char *buf) +static int max6697_write(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long val) { - unsigned int regs[2] = { MAX6581_REG_OFFSET_SELECT, MAX6581_REG_OFFSET }; struct max6697_data *data = dev_get_drvdata(dev); - int index = to_sensor_dev_attr(devattr)->index; - u8 regdata[2]; + struct regmap *regmap = data->regmap; int ret; - ret = regmap_multi_reg_read(data->regmap, regs, regdata, 2); - if (ret) + switch (attr) { + case hwmon_temp_max: + val = clamp_val(val, -1000000, 1000000); /* prevent underflow */ + val = DIV_ROUND_CLOSEST(val, 1000) + data->temp_offset; + val = clamp_val(val, 0, data->type == max6581 ? 255 : 127); + return regmap_write(regmap, MAX6697_REG_MAX[channel], val); + case hwmon_temp_crit: + val = clamp_val(val, -1000000, 1000000); /* prevent underflow */ + val = DIV_ROUND_CLOSEST(val, 1000) + data->temp_offset; + val = clamp_val(val, 0, data->type == max6581 ? 255 : 127); + return regmap_write(regmap, MAX6697_REG_CRIT[channel], val); + case hwmon_temp_offset: + mutex_lock(&data->update_lock); + val = clamp_val(val, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX); + val = DIV_ROUND_CLOSEST(val, 250); + if (!val) { /* disable this (and only this) channel */ + ret = regmap_clear_bits(regmap, MAX6581_REG_OFFSET_SELECT, + BIT(channel - 1)); + } else { + /* enable channel and update offset */ + ret = regmap_set_bits(regmap, MAX6581_REG_OFFSET_SELECT, + BIT(channel - 1)); + if (ret) + goto unlock; + ret = regmap_write(regmap, MAX6581_REG_OFFSET, val); + } +unlock: + mutex_unlock(&data->update_lock); return ret; - - if (!(regdata[0] & BIT(index - 1))) - regdata[1] = 0; - - return sprintf(buf, "%d\n", max6581_offset_to_millic(regdata[1])); + default: + return -EOPNOTSUPP; + } } -static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); -static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); -static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 1, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2); -static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 2, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp4_input, temp_input, 3); -static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 3, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp5_input, temp_input, 4); -static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 4, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp6_input, temp_input, 5); -static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp6_crit, temp, 5, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp7_input, temp_input, 6); -static SENSOR_DEVICE_ATTR_2_RW(temp7_max, temp, 6, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp7_crit, temp, 6, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp8_input, temp_input, 7); -static SENSOR_DEVICE_ATTR_2_RW(temp8_max, temp, 7, MAX6697_TEMP_MAX); -static SENSOR_DEVICE_ATTR_2_RW(temp8_crit, temp, 7, MAX6697_TEMP_CRIT); - -static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 22); -static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 16); -static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 17); -static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, 18); -static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, 19); -static SENSOR_DEVICE_ATTR_RO(temp6_max_alarm, alarm, 20); -static SENSOR_DEVICE_ATTR_RO(temp7_max_alarm, alarm, 21); -static SENSOR_DEVICE_ATTR_RO(temp8_max_alarm, alarm, 23); - -static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 15); -static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 8); -static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 9); -static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, alarm, 10); -static SENSOR_DEVICE_ATTR_RO(temp5_crit_alarm, alarm, 11); -static SENSOR_DEVICE_ATTR_RO(temp6_crit_alarm, alarm, 12); -static SENSOR_DEVICE_ATTR_RO(temp7_crit_alarm, alarm, 13); -static SENSOR_DEVICE_ATTR_RO(temp8_crit_alarm, alarm, 14); - -static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 1); -static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2); -static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, 3); -static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, 4); -static SENSOR_DEVICE_ATTR_RO(temp6_fault, alarm, 5); -static SENSOR_DEVICE_ATTR_RO(temp7_fault, alarm, 6); -static SENSOR_DEVICE_ATTR_RO(temp8_fault, alarm, 7); - -/* There is no offset for local temperature so starting from temp2 */ -static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 1); -static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 2); -static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 3); -static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 4); -static SENSOR_DEVICE_ATTR_RW(temp6_offset, offset, 5); -static SENSOR_DEVICE_ATTR_RW(temp7_offset, offset, 6); -static SENSOR_DEVICE_ATTR_RW(temp8_offset, offset, 7); - -static DEVICE_ATTR(dummy, 0, NULL, NULL); - -static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr, - int index) +static umode_t max6697_is_visible(const void *_data, enum hwmon_sensor_types type, + u32 attr, int channel) { - struct device *dev = kobj_to_dev(kobj); - struct max6697_data *data = dev_get_drvdata(dev); + const struct max6697_data *data = _data; const struct max6697_chip_data *chip = data->chip; - int channel = index / 7; /* channel number */ - int nr = index % 7; /* attribute index within channel */ if (channel >= chip->channels) return 0; - if ((nr == 3 || nr == 4) && !(chip->have_crit & BIT(channel))) - return 0; - if (nr == 5 && !(chip->have_fault & BIT(channel))) - return 0; - /* offset reg is only supported on max6581 remote channels */ - if (nr == 6) - if (data->type != max6581 || channel == 0) - return 0; - - return attr->mode; + switch (attr) { + case hwmon_temp_max: + return 0644; + case hwmon_temp_input: + case hwmon_temp_max_alarm: + return 0444; + case hwmon_temp_crit: + if (chip->have_crit & BIT(channel)) + return 0644; + break; + case hwmon_temp_crit_alarm: + if (chip->have_crit & BIT(channel)) + return 0444; + break; + case hwmon_temp_fault: + if (chip->have_fault & BIT(channel)) + return 0444; + break; + case hwmon_temp_offset: + if (data->type == max6581 && channel) + return 0644; + break; + default: + break; + } + return 0; } -/* - * max6697_is_visible uses the index into the following array to determine - * if attributes should be created or not. Any change in order or content - * must be matched in max6697_is_visible. - */ -static struct attribute *max6697_attributes[] = { - &sensor_dev_attr_temp1_input.dev_attr.attr, - &sensor_dev_attr_temp1_max.dev_attr.attr, - &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp1_crit.dev_attr.attr, - &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, - &dev_attr_dummy.attr, - &dev_attr_dummy.attr, - - &sensor_dev_attr_temp2_input.dev_attr.attr, - &sensor_dev_attr_temp2_max.dev_attr.attr, - &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp2_crit.dev_attr.attr, - &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp2_fault.dev_attr.attr, - &sensor_dev_attr_temp2_offset.dev_attr.attr, - - &sensor_dev_attr_temp3_input.dev_attr.attr, - &sensor_dev_attr_temp3_max.dev_attr.attr, - &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp3_crit.dev_attr.attr, - &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp3_fault.dev_attr.attr, - &sensor_dev_attr_temp3_offset.dev_attr.attr, - - &sensor_dev_attr_temp4_input.dev_attr.attr, - &sensor_dev_attr_temp4_max.dev_attr.attr, - &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp4_crit.dev_attr.attr, - &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp4_fault.dev_attr.attr, - &sensor_dev_attr_temp4_offset.dev_attr.attr, - - &sensor_dev_attr_temp5_input.dev_attr.attr, - &sensor_dev_attr_temp5_max.dev_attr.attr, - &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp5_crit.dev_attr.attr, - &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp5_fault.dev_attr.attr, - &sensor_dev_attr_temp5_offset.dev_attr.attr, - - &sensor_dev_attr_temp6_input.dev_attr.attr, - &sensor_dev_attr_temp6_max.dev_attr.attr, - &sensor_dev_attr_temp6_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp6_crit.dev_attr.attr, - &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp6_fault.dev_attr.attr, - &sensor_dev_attr_temp6_offset.dev_attr.attr, - - &sensor_dev_attr_temp7_input.dev_attr.attr, - &sensor_dev_attr_temp7_max.dev_attr.attr, - &sensor_dev_attr_temp7_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp7_crit.dev_attr.attr, - &sensor_dev_attr_temp7_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp7_fault.dev_attr.attr, - &sensor_dev_attr_temp7_offset.dev_attr.attr, - - &sensor_dev_attr_temp8_input.dev_attr.attr, - &sensor_dev_attr_temp8_max.dev_attr.attr, - &sensor_dev_attr_temp8_max_alarm.dev_attr.attr, - &sensor_dev_attr_temp8_crit.dev_attr.attr, - &sensor_dev_attr_temp8_crit_alarm.dev_attr.attr, - &sensor_dev_attr_temp8_fault.dev_attr.attr, - &sensor_dev_attr_temp8_offset.dev_attr.attr, +/* Return 0 if detection is successful, -ENODEV otherwise */ +static const struct hwmon_channel_info * const max6697_info[] = { + HWMON_CHANNEL_INFO(temp, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET, + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_FAULT | HWMON_T_OFFSET), NULL }; -static const struct attribute_group max6697_group = { - .attrs = max6697_attributes, .is_visible = max6697_is_visible, +static const struct hwmon_ops max6697_hwmon_ops = { + .is_visible = max6697_is_visible, + .read = max6697_read, + .write = max6697_write, +}; + +static const struct hwmon_chip_info max6697_chip_info = { + .ops = &max6697_hwmon_ops, + .info = max6697_info, }; -__ATTRIBUTE_GROUPS(max6697); static int max6697_config_of(struct device_node *node, struct max6697_data *data) { @@ -630,9 +513,8 @@ static int max6697_probe(struct i2c_client *client) if (err) return err; - hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - data, - max6697_groups); + hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, + &max6697_chip_info, NULL); return PTR_ERR_OR_ZERO(hwmon_dev); } From patchwork Tue Jul 23 00:52:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13739258 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24E831311A7 for ; Tue, 23 Jul 2024 00:52:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695941; cv=none; b=hYhFY/O2WWQ3ttD/kAiPrY1IeSId1Ecas+RYhM9vIBGftNKZg+9pHAOoj4pLdTgPBN1UhaJBH5E+HgGpTniWlGeJFuudgSfPsimav0KJeAS2JHlxBfQceABfCrEO0ZwzqxubdM3UnNmiycYeHRC/tGdWSfJZYRCKIOIPEXA9gzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721695941; c=relaxed/simple; bh=S5QZA80lhF4eaLBtFtxU3tRR7M2V+EChdPstNO83d3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JWDsNZBaBxzvqN9y6Kq4WMeE9w0mAIl6UgNEqOP/ylbiJRRPYObBkAgi7cVtM1V9GBm3cCxd4UV5vD5qP4ccm9aWedaSouD5gIWaKHaEdi1elBggwIgpFIuDXxe0swD3W6piPEAbvsMVnDORs8T1lupoDXzKBYCFUD4mykAKEvA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eHX53S4a; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eHX53S4a" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-70d1cbbeeaeso1031334b3a.0 for ; Mon, 22 Jul 2024 17:52:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721695939; x=1722300739; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=3nB5+q1cLh9iZ99GCUGsuV9I+wkqVY2epG/v5xeZS9o=; b=eHX53S4adNoPdkXQRKbkqdYYpWnRZRs6V4T/sjrGr30LFMecKh60tocXiPXTnLDMY/ AOuN789238qT+dY0hG1+IE6DkTns+bABUA5ZnK33AENKRdjlfkKZerocgHqLO7DjfzyQ Fnr37tq4HIuhE8m4KQn2EHczaj/SPHHYCHrSRDHPN8CWdk5qULClGLGU8lvAIbw0E9co hDEcqunWDJCfgnODn53sHy11CaGtiPqypXSGf3x78PhHMa5owl9fLMF6YfbQ8WMn/kMO 8iBSSxGE3LLuB1CvjStd/DdT4Fu3jmMK4b3hhZeIQfJ+ofeYP6Gov/Aj7G2DCyshQdvU jo7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721695939; x=1722300739; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=3nB5+q1cLh9iZ99GCUGsuV9I+wkqVY2epG/v5xeZS9o=; b=cOxzCMKkQioUp9l9aBAqkB5jFAa09MB42QefQb9JgfwZzkJd91ksErWqeuSH/n83l0 Migv+8BrkV1nIaofWzNRYef46Yq3iTFMM8AH9hc/ZbgLbhiYDjESh1PNhhTncBS0G2AI vM/NScK7cRZz7q3HqJscQAi8bdqiseT4jGMXBtys42Rjkih9aNPCgUc9VsVJ4dcHSbex gT65NWj5iD/uDh/z3/GjCKbwx9FCCi4oKNrdT1JLZcOUCsWM8MoDl38SQzsFCmm5nf+2 lRkYP8b9ocqKC75LZk2CA40NDh8t/Q94Ie/s/zZQb34CMMt/zodoiVF8aaRhKyRoUfyU frlQ== X-Gm-Message-State: AOJu0YwH660UnY5duzUhmZfpgI6r7SF9QuOAqbNq1kPf8j3JdSu+9rtx 6VDOgM10f3q/vZ6aP1BqHzaDj1KV72TK05U4ZXjDSQ2wpAu/cR0Lm9CSiw== X-Google-Smtp-Source: AGHT+IGl1qX0ORX6P3pwtFFkDTh0xJox+vY+mi3Ht+u/hnOmuG9ht8H6mGxOPh+b9H1RBfFGiV08NQ== X-Received: by 2002:a05:6a00:3989:b0:70a:f57c:fa27 with SMTP id d2e1a72fcca58-70d0862a20emr8994114b3a.19.1721695938913; Mon, 22 Jul 2024 17:52:18 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70cff5ab918sm5912872b3a.183.2024.07.22.17.52.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 17:52:18 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 6/6] hwmon: (max6697) Add support for tempX_min and tempX_min_alarm Date: Mon, 22 Jul 2024 17:52:04 -0700 Message-Id: <20240723005204.1356501-7-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723005204.1356501-1-linux@roeck-us.net> References: <20240723005204.1356501-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 MAX6581 supports setting the minimum temperature as well as minimum temperature alarms. Add support for it. Signed-off-by: Guenter Roeck Reviewed-by: Tzung-Bi Shih --- drivers/hwmon/max6697.c | 59 +++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index fbe90ab18b10..a00d38ed4d52 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c @@ -32,6 +32,7 @@ static const u8 MAX6697_REG_MAX[] = { static const u8 MAX6697_REG_CRIT[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 }; +#define MAX6697_REG_MIN 0x30 /* * Map device tree / internal register bit map to chip bit map. * Applies to alert register and over-temperature register. @@ -54,6 +55,7 @@ static const u8 MAX6697_REG_CRIT[] = { #define MAX6697_REG_STAT_ALARM 0x44 #define MAX6697_REG_STAT_CRIT 0x45 #define MAX6697_REG_STAT_FAULT 0x46 +#define MAX6697_REG_STAT_MIN_ALARM 0x47 #define MAX6697_REG_CONFIG 0x41 #define MAX6581_CONF_EXTENDED BIT(1) @@ -173,6 +175,18 @@ static const struct max6697_chip_data max6697_chip_data[] = { }, }; +static int max6697_alarm_channel_map(int channel) +{ + switch (channel) { + case 0: + return 6; + case 7: + return 7; + default: + return channel - 1; + } +} + static int max6697_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { @@ -205,6 +219,12 @@ static int max6697_read(struct device *dev, enum hwmon_sensor_types type, return ret; *val = ((int)regval - data->temp_offset) * 1000; break; + case hwmon_temp_min: + ret = regmap_read(regmap, MAX6697_REG_MIN, ®val); + if (ret) + return ret; + *val = ((int)regval - data->temp_offset) * 1000; + break; case hwmon_temp_offset: ret = regmap_multi_reg_read(regmap, offset_regs, regdata, 2); if (ret) @@ -234,17 +254,13 @@ static int max6697_read(struct device *dev, enum hwmon_sensor_types type, ret = regmap_read(regmap, MAX6697_REG_STAT_ALARM, ®val); if (ret) return ret; - switch (channel) { - case 0: - *val = !!(regval & BIT(6)); - break; - case 7: - *val = !!(regval & BIT(7)); - break; - default: - *val = !!(regval & BIT(channel - 1)); - break; - } + *val = !!(regval & BIT(max6697_alarm_channel_map(channel))); + break; + case hwmon_temp_min_alarm: + ret = regmap_read(regmap, MAX6697_REG_STAT_MIN_ALARM, ®val); + if (ret) + return ret; + *val = !!(regval & BIT(max6697_alarm_channel_map(channel))); break; default: return -EOPNOTSUPP; @@ -270,6 +286,11 @@ static int max6697_write(struct device *dev, enum hwmon_sensor_types type, val = DIV_ROUND_CLOSEST(val, 1000) + data->temp_offset; val = clamp_val(val, 0, data->type == max6581 ? 255 : 127); return regmap_write(regmap, MAX6697_REG_CRIT[channel], val); + case hwmon_temp_min: + val = clamp_val(val, -1000000, 1000000); /* prevent underflow */ + val = DIV_ROUND_CLOSEST(val, 1000) + data->temp_offset; + val = clamp_val(val, 0, 255); + return regmap_write(regmap, MAX6697_REG_MIN, val); case hwmon_temp_offset: mutex_lock(&data->update_lock); val = clamp_val(val, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX); @@ -308,6 +329,14 @@ static umode_t max6697_is_visible(const void *_data, enum hwmon_sensor_types typ case hwmon_temp_input: case hwmon_temp_max_alarm: return 0444; + case hwmon_temp_min: + if (data->type == max6581) + return channel ? 0444 : 0644; + break; + case hwmon_temp_min_alarm: + if (data->type == max6581) + return 0444; + break; case hwmon_temp_crit: if (chip->have_crit & BIT(channel)) return 0644; @@ -334,27 +363,35 @@ static umode_t max6697_is_visible(const void *_data, enum hwmon_sensor_types typ static const struct hwmon_channel_info * const max6697_info[] = { HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | + HWMON_T_MIN | HWMON_T_MIN_ALARM | HWMON_T_FAULT | HWMON_T_OFFSET), NULL };