Skip to contentSkip to conversation

iio: mma8452: Fix probe failing when an i2c_device_id is used

PATCH Applied Hans de Goede 2 participants 4 messages Jan 6, 2022 – Jan 30, 2022 IIO

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: mma8452: Fix probe failing when an i2c_device_id is used

The mma8452_driver declares both of_match_table and i2c_driver.id_table match-tables, but its probe() function only checked for of matches.

Add support for i2c_device_id matches. This fixes the driver not loading on some x86 tablets (e.g. the Nextbook Ares 8) where the i2c_client is instantiated by platform code using an i2c_device_id.

---
 drivers/iio/accel/mma8452.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/iio/accel/mma8452.c +15 -8
index 09c7f10fefb6..c82841c0a7b3 100644
@@ -1523,12 +1523,7 @@ static int mma8452_probe(struct i2c_client *client,
 	struct iio_dev *indio_dev;
 	int ret;
 	const struct of_device_id *match;
-
-	match = of_match_device(mma8452_dt_ids, &client->dev);
-	if (!match) {
-		dev_err(&client->dev, "unknown device model\n");
-		return -ENODEV;
-	}
+	const char *compatible;
 
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
@@ -1537,7 +1532,19 @@ static int mma8452_probe(struct i2c_client *client,
 	data = iio_priv(indio_dev);
 	data->client = client;
 	mutex_init(&data->lock);
-	data->chip_info = match->data;
+
+	if (id) {
+		compatible = id->name;
+		data->chip_info = &mma_chip_info_table[id->driver_data];
+	} else {
+		match = of_match_device(mma8452_dt_ids, &client->dev);
+		if (!match) {
+			dev_err(&client->dev, "unknown device model\n");
+			return -ENODEV;
+		}
+		compatible = match->compatible;
+		data->chip_info = match->data;
+	}
 
 	data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
 	if (IS_ERR(data->vdd_reg))
@@ -1581,7 +1588,7 @@ static int mma8452_probe(struct i2c_client *client,
 	}
 
 	dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
-		 match->compatible, data->chip_info->chip_id);
+		 compatible, data->chip_info->chip_id);
 
 	i2c_set_clientdata(client, indio_dev);
 	indio_dev->info = &mma8452_info;
Signature · 1 line
2.33.1
maintainer
#1 lore raw
Quoted 8 lines · On Thu, 6 Jan 2022 12:14:14 +0100 Hans de Goede <[email protected]> wrote:

The mma8452_driver declares both of_match_table and i2c_driver.id_table match-tables, but its probe() function only checked for of matches.

Add support for i2c_device_id matches. This fixes the driver not loading on some x86 tablets (e.g. the Nextbook Ares 8) where the i2c_client is instantiated by platform code using an i2c_device_id.

Hi Hans,

At some point we'll want to get rid of the of_ specific stuff in here in favour of generic firmware properties and I suspect at that time we'll move the device name into the chip_info_table[] entries so that we can just use device_get_match_data()

In the meantime this fix looks good to me. Is there an appropriate
Fixes: tag?

Thanks,

Jonathan

Quoted 52 lines
---
 drivers/iio/accel/mma8452.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/iio/accel/mma8452.c +15 -8
index 09c7f10fefb6..c82841c0a7b3 100644
@@ -1523,12 +1523,7 @@ static int mma8452_probe(struct i2c_client *client,
 	struct iio_dev *indio_dev;
 	int ret;
 	const struct of_device_id *match;
-
-	match = of_match_device(mma8452_dt_ids, &client->dev);
-	if (!match) {
-		dev_err(&client->dev, "unknown device model\n");
-		return -ENODEV;
-	}
+	const char *compatible;
 
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
@@ -1537,7 +1532,19 @@ static int mma8452_probe(struct i2c_client *client,
 	data = iio_priv(indio_dev);
 	data->client = client;
 	mutex_init(&data->lock);
-	data->chip_info = match->data;
+
+	if (id) {
+		compatible = id->name;
+		data->chip_info = &mma_chip_info_table[id->driver_data];
+	} else {
+		match = of_match_device(mma8452_dt_ids, &client->dev);
+		if (!match) {
+			dev_err(&client->dev, "unknown device model\n");
+			return -ENODEV;
+		}
+		compatible = match->compatible;
+		data->chip_info = match->data;
+	}
 
 	data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
 	if (IS_ERR(data->vdd_reg))
@@ -1581,7 +1588,7 @@ static int mma8452_probe(struct i2c_client *client,
 	}
 
 	dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
-		 match->compatible, data->chip_info->chip_id);
+		 compatible, data->chip_info->chip_id);
 
 	i2c_set_clientdata(client, indio_dev);
 	indio_dev->info = &mma8452_info;
#2 lore raw

Hi,

Quoted 13 lines · Jonathan Cameron
Quoted 8 lines · On Thu, 6 Jan 2022 12:14:14 +0100 Hans de Goede <[email protected]> wrote:

The mma8452_driver declares both of_match_table and i2c_driver.id_table match-tables, but its probe() function only checked for of matches.

Add support for i2c_device_id matches. This fixes the driver not loading on some x86 tablets (e.g. the Nextbook Ares 8) where the i2c_client is instantiated by platform code using an i2c_device_id.

Hi Hans,

Quoted 7 lines

At some point we'll want to get rid of the of_ specific stuff in here in favour of generic firmware properties and I suspect at that time we'll move the device name into the chip_info_table[] entries so that we can just use device_get_match_data()

In the meantime this fix looks good to me. Is there an appropriate
Fixes: tag?

I did a quick dive in the git history and the of_match_device() || return -ENODEV behavior was introduced in:

c3cdd6e48e35 ("iio: mma8452: refactor for seperating chip specific data")

Regards,

Hans

Quoted 53 lines
Quoted 52 lines
---
 drivers/iio/accel/mma8452.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/iio/accel/mma8452.c +15 -8
index 09c7f10fefb6..c82841c0a7b3 100644
@@ -1523,12 +1523,7 @@ static int mma8452_probe(struct i2c_client *client,
 	struct iio_dev *indio_dev;
 	int ret;
 	const struct of_device_id *match;
-
-	match = of_match_device(mma8452_dt_ids, &client->dev);
-	if (!match) {
-		dev_err(&client->dev, "unknown device model\n");
-		return -ENODEV;
-	}
+	const char *compatible;
 
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
@@ -1537,7 +1532,19 @@ static int mma8452_probe(struct i2c_client *client,
 	data = iio_priv(indio_dev);
 	data->client = client;
 	mutex_init(&data->lock);
-	data->chip_info = match->data;
+
+	if (id) {
+		compatible = id->name;
+		data->chip_info = &mma_chip_info_table[id->driver_data];
+	} else {
+		match = of_match_device(mma8452_dt_ids, &client->dev);
+		if (!match) {
+			dev_err(&client->dev, "unknown device model\n");
+			return -ENODEV;
+		}
+		compatible = match->compatible;
+		data->chip_info = match->data;
+	}
 
 	data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
 	if (IS_ERR(data->vdd_reg))
@@ -1581,7 +1588,7 @@ static int mma8452_probe(struct i2c_client *client,
 	}
 
 	dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
-		 match->compatible, data->chip_info->chip_id);
+		 compatible, data->chip_info->chip_id);
 
 	i2c_set_clientdata(client, indio_dev);
 	indio_dev->info = &mma8452_info;
maintainer Applied
#3 lore raw
Quoted 21 lines · On Sun, 9 Jan 2022 16:35:46 +0100 Hans de Goede <[email protected]> wrote:

Hi,

Quoted 18 lines · Jonathan Cameron
Quoted 8 lines · On Thu, 6 Jan 2022 12:14:14 +0100 Hans de Goede <[email protected]> wrote:

The mma8452_driver declares both of_match_table and i2c_driver.id_table match-tables, but its probe() function only checked for of matches.

Add support for i2c_device_id matches. This fixes the driver not loading on some x86 tablets (e.g. the Nextbook Ares 8) where the i2c_client is instantiated by platform code using an i2c_device_id.

Hi Hans,

At some point we'll want to get rid of the of_ specific stuff in here in favour of generic firmware properties and I suspect at that time we'll move the device name into the chip_info_table[] entries so that we can just use device_get_match_data()

Quoted 7 lines
Quoted 2 lines

In the meantime this fix looks good to me. Is there an appropriate
Fixes: tag?

I did a quick dive in the git history and the of_match_device() || return -ENODEV behavior was introduced in:

c3cdd6e48e35 ("iio: mma8452: refactor for seperating chip specific data")

Applied to the fixes-togreg branch of iio.git with that as the fixes tag.

Hmm. Sending "Fixes: tag?" was a bad idea on my part as b4 picked it up as a fixes tag. Good think I was editing it anyway or I might not have noticed.

Thanks,

Jonathan

Quoted 62 lines

Regards,

Hans

Quoted 53 lines
Quoted 52 lines
---
 drivers/iio/accel/mma8452.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/iio/accel/mma8452.c +15 -8
index 09c7f10fefb6..c82841c0a7b3 100644
@@ -1523,12 +1523,7 @@ static int mma8452_probe(struct i2c_client *client,
 	struct iio_dev *indio_dev;
 	int ret;
 	const struct of_device_id *match;
-
-	match = of_match_device(mma8452_dt_ids, &client->dev);
-	if (!match) {
-		dev_err(&client->dev, "unknown device model\n");
-		return -ENODEV;
-	}
+	const char *compatible;
 
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
@@ -1537,7 +1532,19 @@ static int mma8452_probe(struct i2c_client *client,
 	data = iio_priv(indio_dev);
 	data->client = client;
 	mutex_init(&data->lock);
-	data->chip_info = match->data;
+
+	if (id) {
+		compatible = id->name;
+		data->chip_info = &mma_chip_info_table[id->driver_data];
+	} else {
+		match = of_match_device(mma8452_dt_ids, &client->dev);
+		if (!match) {
+			dev_err(&client->dev, "unknown device model\n");
+			return -ENODEV;
+		}
+		compatible = match->compatible;
+		data->chip_info = match->data;
+	}
 
 	data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
 	if (IS_ERR(data->vdd_reg))
@@ -1581,7 +1588,7 @@ static int mma8452_probe(struct i2c_client *client,
 	}
 
 	dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
-		 match->compatible, data->chip_info->chip_id);
+		 compatible, data->chip_info->chip_id);
 
 	i2c_set_clientdata(client, indio_dev);
 	indio_dev->info = &mma8452_info;