Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: John Erasmus Mari Geronimo <johnerasmusmari.geronimo@analog.com>
To: <devicetree@vger.kernel.org>, <linux-hwmon@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-doc@vger.kernel.org>
Cc: Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>
Subject: [PATCH 1/3] hwmon: (max31827) refactor enum chips to chip info
Date: Wed, 8 Jan 2025 16:25:29 +0800	[thread overview]
Message-ID: <20250108082531.15467-2-johnerasmusmari.geronimo@analog.com> (raw)
In-Reply-To: <20250108082531.15467-1-johnerasmusmari.geronimo@analog.com>

Utilized chip info to replace enum chips to cater similar devices with
different configurations.

Signed-off-by: John Erasmus Mari Geronimo <johnerasmusmari.geronimo@analog.com>
---
 drivers/hwmon/max31827.c | 86 ++++++++++++++++++++--------------------
 1 file changed, 43 insertions(+), 43 deletions(-)

diff --git a/drivers/hwmon/max31827.c b/drivers/hwmon/max31827.c
index 48e8f8ba4..5d319d401 100644
--- a/drivers/hwmon/max31827.c
+++ b/drivers/hwmon/max31827.c
@@ -47,12 +47,6 @@
 #define MAX31827_M_DGR_TO_16_BIT(x)	(((x) << 4) / 1000)
 #define MAX31827_DEVICE_ENABLE(x)	((x) ? 0xA : 0x0)
 
-/*
- * The enum passed in the .data pointer of struct of_device_id must
- * start with a value != 0 since that is a requirement for using
- * device_get_match_data().
- */
-enum chips { max31827 = 1, max31828, max31829 };
 
 enum max31827_cnv {
 	MAX31827_CNV_1_DIV_64_HZ = 1,
@@ -95,6 +89,17 @@ static const u16 max31827_conv_times[] = {
 	[MAX31827_RES_12_BIT] = MAX31827_12_BIT_CNV_TIME,
 };
 
+struct max31827_state;
+static const struct max31827_chip_info max31827;
+static const struct max31827_chip_info max31828;
+static const struct max31827_chip_info max31829;
+static const struct max31827_chip_info max31875;
+
+struct max31827_chip_info {
+	u8 alarm_pol_default;
+	u32 fault_q_default;
+};
+
 struct max31827_state {
 	/*
 	 * Prevent simultaneous access to the i2c client.
@@ -104,6 +109,7 @@ struct max31827_state {
 	bool enable;
 	unsigned int resolution;
 	unsigned int update_interval;
+	const struct max31827_chip_info *chip_info;
 };
 
 static const struct regmap_config max31827_regmap = {
@@ -493,9 +499,9 @@ static struct attribute *max31827_attrs[] = {
 ATTRIBUTE_GROUPS(max31827);
 
 static const struct i2c_device_id max31827_i2c_ids[] = {
-	{ "max31827", max31827 },
-	{ "max31828", max31828 },
-	{ "max31829", max31829 },
+	{ "max31827", (kernel_ulong_t)&max31827 },
+	{ "max31828", (kernel_ulong_t)&max31828 },
+	{ "max31829", (kernel_ulong_t)&max31829 },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, max31827_i2c_ids);
@@ -506,7 +512,6 @@ static int max31827_init_client(struct max31827_state *st,
 	struct fwnode_handle *fwnode;
 	unsigned int res = 0;
 	u32 data, lsb_idx;
-	enum chips type;
 	bool prop;
 	int ret;
 
@@ -523,8 +528,6 @@ static int max31827_init_client(struct max31827_state *st,
 	prop = fwnode_property_read_bool(fwnode, "adi,timeout-enable");
 	res |= FIELD_PREP(MAX31827_CONFIGURATION_TIMEOUT_MASK, !prop);
 
-	type = (enum chips)(uintptr_t)device_get_match_data(dev);
-
 	if (fwnode_property_present(fwnode, "adi,alarm-pol")) {
 		ret = fwnode_property_read_u32(fwnode, "adi,alarm-pol", &data);
 		if (ret)
@@ -535,19 +538,8 @@ static int max31827_init_client(struct max31827_state *st,
 		/*
 		 * Set default value.
 		 */
-		switch (type) {
-		case max31827:
-		case max31828:
-			res |= FIELD_PREP(MAX31827_CONFIGURATION_ALRM_POL_MASK,
-					  MAX31827_ALRM_POL_LOW);
-			break;
-		case max31829:
-			res |= FIELD_PREP(MAX31827_CONFIGURATION_ALRM_POL_MASK,
-					  MAX31827_ALRM_POL_HIGH);
-			break;
-		default:
-			return -EOPNOTSUPP;
-		}
+		res |= FIELD_PREP(MAX31827_CONFIGURATION_ALRM_POL_MASK,
+				  st->chip_info->alarm_pol_default);
 	}
 
 	if (fwnode_property_present(fwnode, "adi,fault-q")) {
@@ -571,19 +563,8 @@ static int max31827_init_client(struct max31827_state *st,
 		/*
 		 * Set default value.
 		 */
-		switch (type) {
-		case max31827:
-			res |= FIELD_PREP(MAX31827_CONFIGURATION_FLT_Q_MASK,
-					  MAX31827_FLT_Q_1);
-			break;
-		case max31828:
-		case max31829:
-			res |= FIELD_PREP(MAX31827_CONFIGURATION_FLT_Q_MASK,
-					  MAX31827_FLT_Q_4);
-			break;
-		default:
-			return -EOPNOTSUPP;
-		}
+		res |= FIELD_PREP(MAX31827_CONFIGURATION_FLT_Q_MASK,
+				  st->chip_info->fault_q_default);
 	}
 
 	return regmap_write(st->regmap, MAX31827_CONFIGURATION_REG, res);
@@ -604,11 +585,25 @@ static const struct hwmon_ops max31827_hwmon_ops = {
 	.write = max31827_write,
 };
 
-static const struct hwmon_chip_info max31827_chip_info = {
+static const struct hwmon_chip_info max31827_hwmon_chip_info = {
 	.ops = &max31827_hwmon_ops,
 	.info = max31827_info,
 };
 
+static const struct max31827_chip_info max31827 = {
+	.alarm_pol_default = MAX31827_ALRM_POL_LOW,
+	.fault_q_default = MAX31827_FLT_Q_1,
+};
+
+static const struct max31827_chip_info max31828 = {
+	.alarm_pol_default = MAX31827_ALRM_POL_LOW,
+	.fault_q_default = MAX31827_FLT_Q_4,
+
+static const struct max31827_chip_info max31829 = {
+	.alarm_pol_default = MAX31827_ALRM_POL_HIGH,
+	.fault_q_default = MAX31827_FLT_Q_4,
+};
+
 static int max31827_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -623,6 +618,10 @@ static int max31827_probe(struct i2c_client *client)
 	if (!st)
 		return -ENOMEM;
 
+	st->chip_info = device_get_match_data(dev);
+	if (!st->chip_info)
+		return -ENODEV;
+
 	mutex_init(&st->lock);
 
 	st->regmap = devm_regmap_init_i2c(client, &max31827_regmap);
@@ -639,7 +638,7 @@ static int max31827_probe(struct i2c_client *client)
 		return err;
 
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, st,
-							 &max31827_chip_info,
+							 &max31827_hwmon_chip_info,
 							 max31827_groups);
 
 	return PTR_ERR_OR_ZERO(hwmon_dev);
@@ -648,15 +647,15 @@ static int max31827_probe(struct i2c_client *client)
 static const struct of_device_id max31827_of_match[] = {
 	{
 		.compatible = "adi,max31827",
-		.data = (void *)max31827
+		.data = &max31827
 	},
 	{
 		.compatible = "adi,max31828",
-		.data = (void *)max31828
+		.data = &max31828
 	},
 	{
 		.compatible = "adi,max31829",
-		.data = (void *)max31829
+		.data = &max31829
 	},
 	{ }
 };
@@ -672,6 +671,7 @@ static struct i2c_driver max31827_driver = {
 };
 module_i2c_driver(max31827_driver);
 
+MODULE_AUTHOR("John Erasmus Mari Geronimo <johnerasmusmari.geronimo@analog.com>");
 MODULE_AUTHOR("Daniel Matyas <daniel.matyas@analog.com>");
 MODULE_DESCRIPTION("Maxim MAX31827 low-power temperature switch driver");
 MODULE_LICENSE("GPL");
-- 
2.34.1


  reply	other threads:[~2025-01-08  8:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-08  8:25 [PATCH 0/3] Add max31875 support John Erasmus Mari Geronimo
2025-01-08  8:25 ` John Erasmus Mari Geronimo [this message]
2025-01-08 14:22   ` [PATCH 1/3] hwmon: (max31827) refactor enum chips to chip info Guenter Roeck
2025-01-09  5:05   ` kernel test robot
2025-01-08  8:25 ` [PATCH 2/3] dt-bindings: hwmon: adi,max31827: add MAX31875 John Erasmus Mari Geronimo
2025-01-08  9:42   ` Krzysztof Kozlowski
2025-01-08  8:25 ` [PATCH 3/3] hwmon: (max31827) add max31875 support John Erasmus Mari Geronimo
2025-03-02 18:41   ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250108082531.15467-2-johnerasmusmari.geronimo@analog.com \
    --to=johnerasmusmari.geronimo@analog.com \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=jdelvare@suse.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox