Linux IIO development
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: puranjay12@gmail.com, jic23@kernel.org, lars@metafoo.de,
	robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org
Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	kernel@pengutronix.de
Subject: [PATCH v4 2/5] iio: temperature: tmp117: improve fallback capabilities
Date: Mon, 20 Feb 2023 13:25:49 +0100	[thread overview]
Message-ID: <20230220122552.925216-3-m.felsch@pengutronix.de> (raw)
In-Reply-To: <20230220122552.925216-1-m.felsch@pengutronix.de>

Don't error if the device-id found don't match the device-id for the
TMP117 sensor since other TMPxxx might be compatible to the TMP117. The
fallback mechanism tries to gather the required information from the
of_device_id or from the i2c_client information.

The commit also prepares the driver for adding new devices more easily
by making use of switch-case at the relevant parts.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v4:
- new patch to implement possible fallback (Jonathan)

 drivers/iio/temperature/tmp117.c | 67 +++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 23 deletions(-)

diff --git a/drivers/iio/temperature/tmp117.c b/drivers/iio/temperature/tmp117.c
index f9b8f2b570f6b..4ddb8cf9a29ab 100644
--- a/drivers/iio/temperature/tmp117.c
+++ b/drivers/iio/temperature/tmp117.c
@@ -16,6 +16,7 @@
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/limits.h>
+#include <linux/property.h>
 
 #include <linux/iio/iio.h>
 
@@ -113,32 +114,60 @@ static const struct iio_info tmp117_info = {
 	.write_raw = tmp117_write_raw,
 };
 
+static const struct of_device_id tmp117_of_match[] = {
+	{ .compatible = "ti,tmp117", .data = (void *)TMP117_DEVICE_ID },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tmp117_of_match);
+
+static const struct i2c_device_id tmp117_id[] = {
+	{ "tmp117", TMP117_DEVICE_ID },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, tmp117_id);
+
 static int tmp117_identify(struct i2c_client *client)
 {
+	unsigned long match_data;
 	int dev_id;
 
 	dev_id = i2c_smbus_read_word_swapped(client, TMP117_REG_DEVICE_ID);
 	if (dev_id < 0)
 		return dev_id;
-	if (dev_id != TMP117_DEVICE_ID) {
-		dev_err(&client->dev, "TMP117 not found\n");
-		return -ENODEV;
+
+	switch (dev_id) {
+	case TMP117_DEVICE_ID:
+		return dev_id;
 	}
-	return 0;
+
+	dev_info(&client->dev, "Unknown device id (0x%x), use fallback compatible\n",
+		 dev_id);
+
+	match_data = (uintptr_t)device_get_match_data(&client->dev);
+	if (match_data)
+		return match_data;
+
+	match_data = i2c_match_id(tmp117_id, client)->driver_data;
+	if (match_data)
+		return match_data;
+
+	dev_err(&client->dev, "error: No valid fallback found\n");
+
+	return -ENODEV;
 }
 
 static int tmp117_probe(struct i2c_client *client)
 {
 	struct tmp117_data *data;
 	struct iio_dev *indio_dev;
-	int ret;
+	int dev_id;
 
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
 		return -EOPNOTSUPP;
 
-	ret = tmp117_identify(client);
-	if (ret < 0)
-		return ret;
+	dev_id = tmp117_identify(client);
+	if (dev_id < 0)
+		return dev_id;
 
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
@@ -148,28 +177,20 @@ static int tmp117_probe(struct i2c_client *client)
 	data->client = client;
 	data->calibbias = 0;
 
-	indio_dev->name = "tmp117";
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->info = &tmp117_info;
 
-	indio_dev->channels = tmp117_channels;
-	indio_dev->num_channels = ARRAY_SIZE(tmp117_channels);
+	switch (dev_id) {
+	case TMP117_DEVICE_ID:
+		indio_dev->channels = tmp117_channels;
+		indio_dev->num_channels = ARRAY_SIZE(tmp117_channels);
+		indio_dev->name = "tmp117";
+		break;
+	}
 
 	return devm_iio_device_register(&client->dev, indio_dev);
 }
 
-static const struct of_device_id tmp117_of_match[] = {
-	{ .compatible = "ti,tmp117", },
-	{ }
-};
-MODULE_DEVICE_TABLE(of, tmp117_of_match);
-
-static const struct i2c_device_id tmp117_id[] = {
-	{ "tmp117", 0 },
-	{ }
-};
-MODULE_DEVICE_TABLE(i2c, tmp117_id);
-
 static struct i2c_driver tmp117_driver = {
 	.driver = {
 		.name	= "tmp117",
-- 
2.30.2


  parent reply	other threads:[~2023-02-20 12:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-20 12:25 [PATCH v4 0/5] Add TI TMP116 Support Marco Felsch
2023-02-20 12:25 ` [PATCH v4 1/5] dt-bindings: iio: ti,tmp117: fix documentation link Marco Felsch
2023-02-20 12:25 ` Marco Felsch [this message]
2023-02-26 13:07   ` [PATCH v4 2/5] iio: temperature: tmp117: improve fallback capabilities Jonathan Cameron
2023-02-27 18:44     ` Marco Felsch
2023-03-04 13:13       ` Jonathan Cameron
2023-02-20 12:25 ` [PATCH v4 3/5] dt-bindings: iio: ti,tmp117: add binding for the TMP116 Marco Felsch
2023-02-20 12:25 ` [PATCH v4 4/5] iio: temperature: tmp117: add TI TMP116 support Marco Felsch
2023-02-20 12:25 ` [PATCH v4 5/5] iio: temperature: tmp117: cosmetic alignment cleanup Marco Felsch
2023-02-26 13:09   ` Jonathan Cameron

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=20230220122552.925216-3-m.felsch@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=kernel@pengutronix.de \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=puranjay12@gmail.com \
    --cc=robh+dt@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