linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] iio: humidity: hdc100x: add manufacturer and device id cehck
@ 2022-07-22 17:20 Potin Lai
  2022-07-25 21:01 ` Andy Shevchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Potin Lai @ 2022-07-22 17:20 UTC (permalink / raw)
  To: Jonathan Cameron, Lars-Peter Clausen
  Cc: Patrick Williams, Potin Lai, Potin Lai, linux-iio, linux-kernel

Add manufacturer and device id checking during probe, and Skip the
checking if chip model not supported.

Supported:
- HDC1000
- HDC1010
- HDC1050
- HDC1080

Not supported:
- HDC1008

Signed-off-by: Potin Lai <potin.lai.pt@gmail.com>
---
 drivers/iio/humidity/hdc100x.c | 87 +++++++++++++++++++++++++++++-----
 1 file changed, 76 insertions(+), 11 deletions(-)

diff --git a/drivers/iio/humidity/hdc100x.c b/drivers/iio/humidity/hdc100x.c
index 9e0fce917ce4..b8ae8d2ae7f0 100644
--- a/drivers/iio/humidity/hdc100x.c
+++ b/drivers/iio/humidity/hdc100x.c
@@ -34,6 +34,32 @@
 #define HDC100X_REG_CONFIG_ACQ_MODE		BIT(12)
 #define HDC100X_REG_CONFIG_HEATER_EN		BIT(13)
 
+#define HDC100X_REG_MFR_ID	0xFE
+#define HDC100X_REG_DEV_ID	0xFF
+
+#define HDC100X_MFR_ID	0x5449
+
+enum {
+	HDC100X,
+	HDC1000,
+	HDC1008,
+	HDC1010,
+	HDC1050,
+	HDC1080
+};
+
+struct hdc100x_of_data {
+	bool support_mfr_check;
+};
+
+static const struct hdc100x_of_data hdc100x_of_data = {
+	.support_mfr_check	= true,
+};
+
+static const struct hdc100x_of_data hdc1008_of_data = {
+	.support_mfr_check	= false,
+};
+
 struct hdc100x_data {
 	struct i2c_client *client;
 	struct mutex lock;
@@ -351,6 +377,42 @@ static const struct iio_info hdc100x_info = {
 	.attrs = &hdc100x_attribute_group,
 };
 
+static int hdc100x_read_mfr_id(struct i2c_client *client)
+{
+	return i2c_smbus_read_word_swapped(client, HDC100X_REG_MFR_ID);
+}
+
+static int hdc100x_read_dev_id(struct i2c_client *client)
+{
+	return i2c_smbus_read_word_swapped(client, HDC100X_REG_DEV_ID);
+}
+
+static const struct of_device_id hdc100x_dt_ids[];
+static bool is_valid_hdc100x(struct i2c_client *client,
+			     const struct i2c_device_id *id)
+{
+	const struct of_device_id *match;
+	struct hdc100x_of_data *of_data;
+	int mfr_id, dev_id;
+
+	match = i2c_of_match_device(hdc100x_dt_ids, client);
+
+	if (match) {
+		of_data = (struct hdc100x_of_data *)match->data;
+		if (!of_data->support_mfr_check)
+			return true;
+	} else if (id->driver_data == HDC1008)
+		return true;
+
+	mfr_id = hdc100x_read_mfr_id(client);
+	dev_id = hdc100x_read_dev_id(client);
+	if (mfr_id == HDC100X_MFR_ID &&
+	   (dev_id == 0x1000 || dev_id == 0x1050))
+		return true;
+
+	return false;
+}
+
 static int hdc100x_probe(struct i2c_client *client,
 			 const struct i2c_device_id *id)
 {
@@ -362,6 +424,9 @@ static int hdc100x_probe(struct i2c_client *client,
 				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
 		return -EOPNOTSUPP;
 
+	if (!is_valid_hdc100x(client, id))
+		return -EOPNOTSUPP;
+
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
 		return -ENOMEM;
@@ -397,22 +462,22 @@ static int hdc100x_probe(struct i2c_client *client,
 }
 
 static const struct i2c_device_id hdc100x_id[] = {
-	{ "hdc100x", 0 },
-	{ "hdc1000", 0 },
-	{ "hdc1008", 0 },
-	{ "hdc1010", 0 },
-	{ "hdc1050", 0 },
-	{ "hdc1080", 0 },
+	{ "hdc100X", HDC100X },
+	{ "hdc1000", HDC1000 },
+	{ "hdc1008", HDC1008 },
+	{ "hdc1010", HDC1010 },
+	{ "hdc1050", HDC1050 },
+	{ "hdc1080", HDC1080 },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
 
 static const struct of_device_id hdc100x_dt_ids[] = {
-	{ .compatible = "ti,hdc1000" },
-	{ .compatible = "ti,hdc1008" },
-	{ .compatible = "ti,hdc1010" },
-	{ .compatible = "ti,hdc1050" },
-	{ .compatible = "ti,hdc1080" },
+	{ .compatible = "ti,hdc1000", .data = &hdc100x_of_data },
+	{ .compatible = "ti,hdc1008", .data = &hdc1008_of_data },
+	{ .compatible = "ti,hdc1010", .data = &hdc100x_of_data },
+	{ .compatible = "ti,hdc1050", .data = &hdc100x_of_data },
+	{ .compatible = "ti,hdc1080", .data = &hdc100x_of_data },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] iio: humidity: hdc100x: add manufacturer and device id cehck
  2022-07-22 17:20 [PATCH 1/1] iio: humidity: hdc100x: add manufacturer and device id cehck Potin Lai
@ 2022-07-25 21:01 ` Andy Shevchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2022-07-25 21:01 UTC (permalink / raw)
  To: Potin Lai
  Cc: Jonathan Cameron, Lars-Peter Clausen, Patrick Williams,
	Potin Lai, linux-iio, Linux Kernel Mailing List

On Fri, Jul 22, 2022 at 7:25 PM Potin Lai <potin.lai.pt@gmail.com> wrote:
>
> Add manufacturer and device id checking during probe, and Skip the

ID
skip

> checking if chip model not supported.
>
> Supported:
> - HDC1000
> - HDC1010
> - HDC1050
> - HDC1080
>
> Not supported:
> - HDC1008

...

> +enum {
> +       HDC100X,
> +       HDC1000,
> +       HDC1008,
> +       HDC1010,
> +       HDC1050,
> +       HDC1080

+ Comma here.

> +};

...

> +static const struct of_device_id hdc100x_dt_ids[];

No, drop it.

...

> +       match = i2c_of_match_device(hdc100x_dt_ids, client);
> +
> +       if (match) {
> +               of_data = (struct hdc100x_of_data *)match->data;
> +               if (!of_data->support_mfr_check)
> +                       return true;

Besides the redundant blank line this call to i2c_of_match_device() is not good.

of_data is a misleading name. What about ACPI?

What you are looking for is:

  data = device_get_match_data(&client->dev);

> +       } else if (id->driver_data == HDC1008)

Don't use I2C id field, switch to i2c ->probe_new() if it's not done yet.

> +               return true;
> +
> +       mfr_id = hdc100x_read_mfr_id(client);
> +       dev_id = hdc100x_read_dev_id(client);
> +       if (mfr_id == HDC100X_MFR_ID &&
> +          (dev_id == 0x1000 || dev_id == 0x1050))
> +               return true;
> +
> +       return false;
> +}

...

>  static const struct i2c_device_id hdc100x_id[] = {
> -       { "hdc100x", 0 },
> -       { "hdc1000", 0 },
> -       { "hdc1008", 0 },
> -       { "hdc1010", 0 },
> -       { "hdc1050", 0 },
> -       { "hdc1080", 0 },
> +       { "hdc100X", HDC100X },
> +       { "hdc1000", HDC1000 },
> +       { "hdc1008", HDC1008 },
> +       { "hdc1010", HDC1010 },
> +       { "hdc1050", HDC1050 },
> +       { "hdc1080", HDC1080 },

Please, use pointers as in of_device_id table.

>         { }
>  };

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-25 21:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-22 17:20 [PATCH 1/1] iio: humidity: hdc100x: add manufacturer and device id cehck Potin Lai
2022-07-25 21:01 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).