Skip to contentSkip to conversation

iio: accel: adxl345: Convert to use dev_err_probe()

PATCH In review Andy Shevchenko 3 participants 13 messages Feb 22, 2022 – Feb 27, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/8 iio: accel: adxl345: Convert to use dev_err_probe()

It's fine to call dev_err_probe() in ->probe() when error code is known. Convert the driver to use dev_err_probe().

---
v2: fixed typo (LKP), shorten one line to satisfy checkpatch

 drivers/iio/accel/adxl345_core.c | 26 +++++++++-----------------
 drivers/iio/accel/adxl345_i2c.c  |  7 ++-----
 drivers/iio/accel/adxl345_spi.c  | 15 +++++----------
 3 files changed, 16 insertions(+), 32 deletions(-)
drivers/iio/accel/adxl345_core.c +9 -17
index ef2240e356e0..078e1029e49d 100644
@@ -222,16 +222,12 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 	int ret;
 
 	ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
-	if (ret < 0) {
-		dev_err(dev, "Error reading device ID: %d\n", ret);
-		return ret;
-	}
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Error reading device ID\n");
 
-	if (regval != ADXL345_DEVID) {
-		dev_err(dev, "Invalid device ID: %x, expected %x\n",
-			regval, ADXL345_DEVID);
-		return -ENODEV;
-	}
+	if (regval != ADXL345_DEVID)
+		return dev_err_probe(dev, -ENODEV, "Invalid device ID: %x, expected %x\n",
+				     regval, ADXL345_DEVID);
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
 	if (!indio_dev)
@@ -245,10 +241,8 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 
 	ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT,
 			   data->data_range);
-	if (ret < 0) {
-		dev_err(dev, "Failed to set data range: %d\n", ret);
-		return ret;
-	}
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Failed to set data range\n");
 
 	indio_dev->name = name;
 	indio_dev->info = &adxl345_info;
@@ -259,10 +253,8 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 	/* Enable measurement mode */
 	ret = regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
 			   ADXL345_POWER_CTL_MEASURE);
-	if (ret < 0) {
-		dev_err(dev, "Failed to enable measurement mode: %d\n", ret);
-		return ret;
-	}
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Failed to enable measurement mode\n");
 
 	ret = devm_add_action_or_reset(dev, adxl345_powerdown, data->regmap);
 	if (ret < 0)
drivers/iio/accel/adxl345_i2c.c +2 -5
index 7bc8324c4f07..e3205dce91b8 100644
@@ -28,11 +28,8 @@ static int adxl345_i2c_probe(struct i2c_client *client,
 		return -ENODEV;
 
 	regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
-	if (IS_ERR(regmap)) {
-		dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
-			PTR_ERR(regmap));
-		return PTR_ERR(regmap);
-	}
+	if (IS_ERR(regmap))
+		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
 	return adxl345_core_probe(&client->dev, regmap, id->driver_data,
 				  id->name);
drivers/iio/accel/adxl345_spi.c +5 -10
index c752562c5d3b..9223302fdd46 100644
@@ -26,18 +26,13 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	struct regmap *regmap;
 
 	/* Bail out if max_speed_hz exceeds 5 MHz */
-	if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) {
-		dev_err(&spi->dev, "SPI CLK, %d Hz exceeds 5 MHz\n",
-			spi->max_speed_hz);
-		return -EINVAL;
-	}
+	if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
+		return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n",
+				     spi->max_speed_hz);
 
 	regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
-	if (IS_ERR(regmap)) {
-		dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
-			PTR_ERR(regmap));
-		return PTR_ERR(regmap);
-	}
+	if (IS_ERR(regmap))
+		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
 	return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
 }
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/8 iio: accel: adxl345: Set driver_data for OF enumeration

In order to enable this driver on mode platforms, assign driver_data field in the OF device ID table.

While at it, make sure that device type is not 0 which may be wrongly interpreted by device property APIs in the future.

---
v2: used proper casting for the supplied data (LKP)

 drivers/iio/accel/adxl345.h     | 4 ++--
 drivers/iio/accel/adxl345_i2c.c | 4 ++--
 drivers/iio/accel/adxl345_spi.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/iio/accel/adxl345.h +2 -2
index af0fdd02c4f2..5a68d4dac717 100644
@@ -9,8 +9,8 @@
 #define _ADXL345_H_
 
 enum adxl345_device_type {
-	ADXL345,
-	ADXL375,
+	ADXL345	= 1,
+	ADXL375 = 2,
 };
 
 int adxl345_core_probe(struct device *dev, struct regmap *regmap,
drivers/iio/accel/adxl345_i2c.c +2 -2
index e3205dce91b8..4c6efe2eefc1 100644
@@ -44,8 +44,8 @@ static const struct i2c_device_id adxl345_i2c_id[] = {
 MODULE_DEVICE_TABLE(i2c, adxl345_i2c_id);
 
 static const struct of_device_id adxl345_of_match[] = {
-	{ .compatible = "adi,adxl345" },
-	{ .compatible = "adi,adxl375" },
+	{ .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
+	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
 	{ },
 };
 
drivers/iio/accel/adxl345_spi.c +2 -2
index 9223302fdd46..72550132b1bb 100644
@@ -46,8 +46,8 @@ static const struct spi_device_id adxl345_spi_id[] = {
 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
 
 static const struct of_device_id adxl345_of_match[] = {
-	{ .compatible = "adi,adxl345" },
-	{ .compatible = "adi,adxl375" },
+	{ .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
+	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
 	{ },
 };
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 3/8 iio: accel: adxl345: Get rid of name parameter in adxl345_core_probe()

In reply to Andy Shevchenko

As a preparation to switch to use device properties, get rid of name parameter in adxl345_core_probe(). Instead, choose it based on the type.

---
v2: no changes

 drivers/iio/accel/adxl345.h      |  3 +--
 drivers/iio/accel/adxl345_core.c | 15 +++++++++++++--
 drivers/iio/accel/adxl345_i2c.c  |  3 +--
 drivers/iio/accel/adxl345_spi.c  |  2 +-
 4 files changed, 16 insertions(+), 7 deletions(-)
drivers/iio/accel/adxl345.h +1 -2
index 5a68d4dac717..9b0d4f487c43 100644
@@ -13,7 +13,6 @@ enum adxl345_device_type {
 	ADXL375 = 2,
 };
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap,
-		       enum adxl345_device_type type, const char *name);
+int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type);
 
 #endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +13 -2
index 078e1029e49d..0f34c349aa1e 100644
@@ -213,14 +213,25 @@ static void adxl345_powerdown(void *regmap)
 	regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY);
 }
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap,
-		       enum adxl345_device_type type, const char *name)
+int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type)
 {
 	struct adxl345_data *data;
 	struct iio_dev *indio_dev;
+	const char *name;
 	u32 regval;
 	int ret;
 
+	switch (type) {
+	case ADXL345:
+		name = "adxl345";
+		break;
+	case ADXL375:
+		name = "adxl375";
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "Error reading device ID\n");
drivers/iio/accel/adxl345_i2c.c +1 -2
index 4c6efe2eefc1..1e42cf3a2991 100644
@@ -31,8 +31,7 @@ static int adxl345_i2c_probe(struct i2c_client *client,
 	if (IS_ERR(regmap))
 		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&client->dev, regmap, id->driver_data,
-				  id->name);
+	return adxl345_core_probe(&client->dev, regmap, id->driver_data);
 }
 
 static const struct i2c_device_id adxl345_i2c_id[] = {
drivers/iio/accel/adxl345_spi.c +1 -1
index 72550132b1bb..34b7001d519f 100644
@@ -34,7 +34,7 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
+	return adxl345_core_probe(&spi->dev, regmap, id->driver_data);
 }
 
 static const struct spi_device_id adxl345_spi_id[] = {
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v2 3/8] iio: accel: adxl345: Get rid of name parameter in adxl345_core_probe()

Quoted 17 lines · On Tue, 22 Feb 2022 11:00:04 +0200 Andy Shevchenko…

As a preparation to switch to use device properties, get rid of name parameter in adxl345_core_probe(). Instead, choose it based on the type.

---
v2: no changes

 drivers/iio/accel/adxl345.h      |  3 +--
 drivers/iio/accel/adxl345_core.c | 15 +++++++++++++--
 drivers/iio/accel/adxl345_i2c.c  |  3 +--
 drivers/iio/accel/adxl345_spi.c  |  2 +-
 4 files changed, 16 insertions(+), 7 deletions(-)
drivers/iio/accel/adxl345.h +0 -0
index 5a68d4dac717..9b0d4f487c43 100644

drivers/iio/accel/adxl345.h enum adxl345_device_type { · line 16

Quoted 6 lines
drivers/iio/accel/adxl345.h +1 -2
@@ -13,7 +13,6 @@ enum adxl345_device_type {
 	ADXL375 = 2,
 };
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap,
-		       enum adxl345_device_type type, const char *name);
+int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type);

Nit picking but I'm not that keen on these lines being so long as it doesn't really help readability so I'd rather keep them sub 80 chars.

If I'm happy with everything else I might tidy that up whilst applying.

Thanks,

Jonathan

Quoted 61 lines
drivers/iio/accel/adxl345.h +0 -0
 #endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +13 -2
index 078e1029e49d..0f34c349aa1e 100644
@@ -213,14 +213,25 @@ static void adxl345_powerdown(void *regmap)
 	regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY);
 }
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap,
-		       enum adxl345_device_type type, const char *name)
+int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type)
 {
 	struct adxl345_data *data;
 	struct iio_dev *indio_dev;
+	const char *name;
 	u32 regval;
 	int ret;
 
+	switch (type) {
+	case ADXL345:
+		name = "adxl345";
+		break;
+	case ADXL375:
+		name = "adxl375";
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "Error reading device ID\n");
drivers/iio/accel/adxl345_i2c.c +1 -2
index 4c6efe2eefc1..1e42cf3a2991 100644
@@ -31,8 +31,7 @@ static int adxl345_i2c_probe(struct i2c_client *client,
 	if (IS_ERR(regmap))
 		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&client->dev, regmap, id->driver_data,
-				  id->name);
+	return adxl345_core_probe(&client->dev, regmap, id->driver_data);
 }
 
 static const struct i2c_device_id adxl345_i2c_id[] = {
drivers/iio/accel/adxl345_spi.c +1 -1
index 72550132b1bb..34b7001d519f 100644
@@ -34,7 +34,7 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
+	return adxl345_core_probe(&spi->dev, regmap, id->driver_data);
 }
 
 static const struct spi_device_id adxl345_spi_id[] = {
#4 lore raw

PATCH v2 4/8 iio: accel: adxl345: Make use of device properties

In reply to Andy Shevchenko

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

---
v2: no changes

 drivers/iio/accel/adxl345.h      |  2 +-
 drivers/iio/accel/adxl345_core.c |  5 ++++-
 drivers/iio/accel/adxl345_i2c.c  | 11 ++++-------
 drivers/iio/accel/adxl345_spi.c  |  3 +--
 4 files changed, 10 insertions(+), 11 deletions(-)
drivers/iio/accel/adxl345.h +1 -1
index 9b0d4f487c43..d7e67cb08538 100644
@@ -13,6 +13,6 @@ enum adxl345_device_type {
 	ADXL375 = 2,
 };
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type);
+int adxl345_core_probe(struct device *dev, struct regmap *regmap);
 
 #endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +4 -1
index 0f34c349aa1e..315a408115b3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/module.h>
+#include <linux/property.h>
 #include <linux/regmap.h>
 
 #include <linux/iio/iio.h>
@@ -213,14 +214,16 @@ static void adxl345_powerdown(void *regmap)
 	regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY);
 }
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type)
+int adxl345_core_probe(struct device *dev, struct regmap *regmap)
 {
+	enum adxl345_device_type type;
 	struct adxl345_data *data;
 	struct iio_dev *indio_dev;
 	const char *name;
 	u32 regval;
 	int ret;
 
+	type = (uintptr_t)device_get_match_data(dev);
 	switch (type) {
 	case ADXL345:
 		name = "adxl345";
drivers/iio/accel/adxl345_i2c.c +4 -7
index 1e42cf3a2991..861d8477d799 100644
@@ -19,19 +19,16 @@ static const struct regmap_config adxl345_i2c_regmap_config = {
 	.val_bits = 8,
 };
 
-static int adxl345_i2c_probe(struct i2c_client *client,
-			     const struct i2c_device_id *id)
+static int adxl345_i2c_probe(struct i2c_client *client)
 {
+	enum adxl345_device_type type;
 	struct regmap *regmap;
 
-	if (!id)
-		return -ENODEV;
-
 	regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
 	if (IS_ERR(regmap))
 		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&client->dev, regmap, id->driver_data);
+	return adxl345_core_probe(&client->dev, regmap);
 }
 
 static const struct i2c_device_id adxl345_i2c_id[] = {
@@ -55,7 +52,7 @@ static struct i2c_driver adxl345_i2c_driver = {
 		.name	= "adxl345_i2c",
 		.of_match_table = adxl345_of_match,
 	},
-	.probe		= adxl345_i2c_probe,
+	.probe_new	= adxl345_i2c_probe,
 	.id_table	= adxl345_i2c_id,
 };
 
drivers/iio/accel/adxl345_spi.c +1 -2
index 34b7001d519f..ee4c50c8a95b 100644
@@ -22,7 +22,6 @@ static const struct regmap_config adxl345_spi_regmap_config = {
 
 static int adxl345_spi_probe(struct spi_device *spi)
 {
-	const struct spi_device_id *id = spi_get_device_id(spi);
 	struct regmap *regmap;
 
 	/* Bail out if max_speed_hz exceeds 5 MHz */
@@ -34,7 +33,7 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&spi->dev, regmap, id->driver_data);
+	return adxl345_core_probe(&spi->dev, regmap);
 }
 
 static const struct spi_device_id adxl345_spi_id[] = {
Signature · 1 line
2.34.1
maintainer
#5 lore raw

Re: [PATCH v2 4/8] iio: accel: adxl345: Make use of device properties

Quoted 17 lines · On Tue, 22 Feb 2022 11:00:05 +0200 Andy Shevchenko…

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

---
v2: no changes

 drivers/iio/accel/adxl345.h      |  2 +-
 drivers/iio/accel/adxl345_core.c |  5 ++++-
 drivers/iio/accel/adxl345_i2c.c  | 11 ++++-------
 drivers/iio/accel/adxl345_spi.c  |  3 +--
 4 files changed, 10 insertions(+), 11 deletions(-)
drivers/iio/accel/adxl345.h +0 -0
index 9b0d4f487c43..d7e67cb08538 100644

drivers/iio/accel/adxl345.h enum adxl345_device_type { · line 16

Quoted 5 lines
drivers/iio/accel/adxl345.h +1 -1
@@ -13,6 +13,6 @@ enum adxl345_device_type {
 	ADXL375 = 2,
 };
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type);
+int adxl345_core_probe(struct device *dev, struct regmap *regmap);

Ah! They shorten again :)

Temporary long ugly lines is of course fine so I'll leave them be.

Jonathan

Quoted 89 lines
drivers/iio/accel/adxl345.h +0 -0
 #endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +4 -1
index 0f34c349aa1e..315a408115b3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/module.h>
+#include <linux/property.h>
 #include <linux/regmap.h>
 
 #include <linux/iio/iio.h>
@@ -213,14 +214,16 @@ static void adxl345_powerdown(void *regmap)
 	regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY);
 }
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type)
+int adxl345_core_probe(struct device *dev, struct regmap *regmap)
 {
+	enum adxl345_device_type type;
 	struct adxl345_data *data;
 	struct iio_dev *indio_dev;
 	const char *name;
 	u32 regval;
 	int ret;
 
+	type = (uintptr_t)device_get_match_data(dev);
 	switch (type) {
 	case ADXL345:
 		name = "adxl345";
drivers/iio/accel/adxl345_i2c.c +4 -7
index 1e42cf3a2991..861d8477d799 100644
@@ -19,19 +19,16 @@ static const struct regmap_config adxl345_i2c_regmap_config = {
 	.val_bits = 8,
 };
 
-static int adxl345_i2c_probe(struct i2c_client *client,
-			     const struct i2c_device_id *id)
+static int adxl345_i2c_probe(struct i2c_client *client)
 {
+	enum adxl345_device_type type;
 	struct regmap *regmap;
 
-	if (!id)
-		return -ENODEV;
-
 	regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
 	if (IS_ERR(regmap))
 		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&client->dev, regmap, id->driver_data);
+	return adxl345_core_probe(&client->dev, regmap);
 }
 
 static const struct i2c_device_id adxl345_i2c_id[] = {
@@ -55,7 +52,7 @@ static struct i2c_driver adxl345_i2c_driver = {
 		.name	= "adxl345_i2c",
 		.of_match_table = adxl345_of_match,
 	},
-	.probe		= adxl345_i2c_probe,
+	.probe_new	= adxl345_i2c_probe,
 	.id_table	= adxl345_i2c_id,
 };
 
drivers/iio/accel/adxl345_spi.c +1 -2
index 34b7001d519f..ee4c50c8a95b 100644
@@ -22,7 +22,6 @@ static const struct regmap_config adxl345_spi_regmap_config = {
 
 static int adxl345_spi_probe(struct spi_device *spi)
 {
-	const struct spi_device_id *id = spi_get_device_id(spi);
 	struct regmap *regmap;
 
 	/* Bail out if max_speed_hz exceeds 5 MHz */
@@ -34,7 +33,7 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&spi->dev, regmap, id->driver_data);
+	return adxl345_core_probe(&spi->dev, regmap);
 }
 
 static const struct spi_device_id adxl345_spi_id[] = {
maintainer
#6 lore raw

Re: [PATCH v2 4/8] iio: accel: adxl345: Make use of device properties

In reply to Andy Shevchenko
Quoted 59 lines · On Tue, 22 Feb 2022 11:00:05 +0200 Andy Shevchenko…

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

---
v2: no changes

 drivers/iio/accel/adxl345.h      |  2 +-
 drivers/iio/accel/adxl345_core.c |  5 ++++-
 drivers/iio/accel/adxl345_i2c.c  | 11 ++++-------
 drivers/iio/accel/adxl345_spi.c  |  3 +--
 4 files changed, 10 insertions(+), 11 deletions(-)
drivers/iio/accel/adxl345.h +1 -1
index 9b0d4f487c43..d7e67cb08538 100644
@@ -13,6 +13,6 @@ enum adxl345_device_type {
 	ADXL375 = 2,
 };
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type);
+int adxl345_core_probe(struct device *dev, struct regmap *regmap);
 
 #endif /* _ADXL345_H_ */
drivers/iio/accel/adxl345_core.c +4 -1
index 0f34c349aa1e..315a408115b3 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/module.h>
+#include <linux/property.h>
 #include <linux/regmap.h>
 
 #include <linux/iio/iio.h>
@@ -213,14 +214,16 @@ static void adxl345_powerdown(void *regmap)
 	regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY);
 }
 
-int adxl345_core_probe(struct device *dev, struct regmap *regmap, enum adxl345_device_type type)
+int adxl345_core_probe(struct device *dev, struct regmap *regmap)
 {
+	enum adxl345_device_type type;
 	struct adxl345_data *data;
 	struct iio_dev *indio_dev;
 	const char *name;
 	u32 regval;
 	int ret;
 
+	type = (uintptr_t)device_get_match_data(dev);
 	switch (type) {
 	case ADXL345:
 		name = "adxl345";
drivers/iio/accel/adxl345_i2c.c +0 -0
index 1e42cf3a2991..861d8477d799 100644

drivers/iio/accel/adxl345_i2c.c static const struct regmap_config adxl345_i2c_regmap_config = { · line 24

Quoted 8 lines
drivers/iio/accel/adxl345_i2c.c +2 -2
@@ -19,19 +19,16 @@ static const struct regmap_config adxl345_i2c_regmap_config = {
 	.val_bits = 8,
 };
 
-static int adxl345_i2c_probe(struct i2c_client *client,
-			     const struct i2c_device_id *id)
+static int adxl345_i2c_probe(struct i2c_client *client)
 {
+	enum adxl345_device_type type;

Not used - (via a build warning)

Jonathan

Quoted 44 lines
drivers/iio/accel/adxl345_i2c.c +2 -5
 	struct regmap *regmap;
 
-	if (!id)
-		return -ENODEV;
-
 	regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
 	if (IS_ERR(regmap))
 		return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&client->dev, regmap, id->driver_data);
+	return adxl345_core_probe(&client->dev, regmap);
 }
 
 static const struct i2c_device_id adxl345_i2c_id[] = {
@@ -55,7 +52,7 @@ static struct i2c_driver adxl345_i2c_driver = {
 		.name	= "adxl345_i2c",
 		.of_match_table = adxl345_of_match,
 	},
-	.probe		= adxl345_i2c_probe,
+	.probe_new	= adxl345_i2c_probe,
 	.id_table	= adxl345_i2c_id,
 };
 
drivers/iio/accel/adxl345_spi.c +1 -2
index 34b7001d519f..ee4c50c8a95b 100644
@@ -22,7 +22,6 @@ static const struct regmap_config adxl345_spi_regmap_config = {
 
 static int adxl345_spi_probe(struct spi_device *spi)
 {
-	const struct spi_device_id *id = spi_get_device_id(spi);
 	struct regmap *regmap;
 
 	/* Bail out if max_speed_hz exceeds 5 MHz */
@@ -34,7 +33,7 @@ static int adxl345_spi_probe(struct spi_device *spi)
 	if (IS_ERR(regmap))
 		return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
-	return adxl345_core_probe(&spi->dev, regmap, id->driver_data);
+	return adxl345_core_probe(&spi->dev, regmap);
 }
 
 static const struct spi_device_id adxl345_spi_id[] = {
#7 lore raw

PATCH v2 5/8 iio: accel: adxl345: Add ACPI HID table

In reply to Andy Shevchenko

From: Kai-Heng Feng <[email protected]>

x86 boards may use ACPI HID "ADS0345" for adxl345 device.

Analog replied:
"ADS034X is not a valid PNP ID. ADS0345 would be.
I'm not aware that this ID is already taken.
Feel free to submit a mainline Linux input mailing list patch."

So add an ACPI match table for that accordingly.

Since ACPI device may not match to any I2C ID, use the name and type directly from ACPI ID table in absence of I2C ID.

---
v2: no changes

 drivers/iio/accel/adxl345_i2c.c | 7 +++++++
 drivers/iio/accel/adxl345_spi.c | 7 +++++++
 2 files changed, 14 insertions(+)
drivers/iio/accel/adxl345_i2c.c +7 -0
index 861d8477d799..11ecff0e0405 100644
@@ -47,10 +47,17 @@ static const struct of_device_id adxl345_of_match[] = {
 
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
 
+static const struct acpi_device_id adxl345_acpi_match[] = {
+	{ "ADS0345", ADXL345 },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
+
 static struct i2c_driver adxl345_i2c_driver = {
 	.driver = {
 		.name	= "adxl345_i2c",
 		.of_match_table = adxl345_of_match,
+		.acpi_match_table = adxl345_acpi_match,
 	},
 	.probe_new	= adxl345_i2c_probe,
 	.id_table	= adxl345_i2c_id,
drivers/iio/accel/adxl345_spi.c +7 -0
index ee4c50c8a95b..850ac616d65e 100644
@@ -52,10 +52,17 @@ static const struct of_device_id adxl345_of_match[] = {
 
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
 
+static const struct acpi_device_id adxl345_acpi_match[] = {
+	{ "ADS0345", ADXL345 },
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
+
 static struct spi_driver adxl345_spi_driver = {
 	.driver = {
 		.name	= "adxl345_spi",
 		.of_match_table = adxl345_of_match,
+		.acpi_match_table = adxl345_acpi_match,
 	},
 	.probe		= adxl345_spi_probe,
 	.id_table	= adxl345_spi_id,
Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 6/8 iio: accel: adxl345: Extract adxl345_powerup() helper

In reply to Andy Shevchenko

For the sake of symmetry and possible reuse in the future extract adxl435_powerup() helper.

---
v2: no changes

 drivers/iio/accel/adxl345_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/iio/accel/adxl345_core.c +6 -2
index 315a408115b3..4e4562fc35c9 100644
@@ -209,6 +209,11 @@ static const struct iio_info adxl345_info = {
 	.write_raw_get_fmt	= adxl345_write_raw_get_fmt,
 };
 
+static int adxl345_powerup(void *regmap)
+{
+	return regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_MEASURE);
+}
+
 static void adxl345_powerdown(void *regmap)
 {
 	regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY);
@@ -265,8 +270,7 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap)
 	indio_dev->num_channels = ARRAY_SIZE(adxl345_channels);
 
 	/* Enable measurement mode */
-	ret = regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
-			   ADXL345_POWER_CTL_MEASURE);
+	ret = adxl345_powerup(data->regmap);
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "Failed to enable measurement mode\n");
 
Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 7/8 iio: accel: adxl345: Drop comma in terminator entries

In reply to Andy Shevchenko

Terminator entries are by definition should terminate the array. Dropping comma make this enforced at compile time.

---
v2: no changes

 drivers/iio/accel/adxl345_core.c | 2 +-
 drivers/iio/accel/adxl345_i2c.c  | 2 +-
 drivers/iio/accel/adxl345_spi.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/iio/accel/adxl345_core.c +1 -1
index 4e4562fc35c9..370bfec1275a 100644
@@ -195,7 +195,7 @@ static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
 
 static struct attribute *adxl345_attrs[] = {
 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
-	NULL,
+	NULL
 };
 
 static const struct attribute_group adxl345_attrs_group = {
drivers/iio/accel/adxl345_i2c.c +1 -1
index 11ecff0e0405..1ceb9be13cdf 100644
@@ -42,7 +42,7 @@ MODULE_DEVICE_TABLE(i2c, adxl345_i2c_id);
 static const struct of_device_id adxl345_of_match[] = {
 	{ .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
 	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
-	{ },
+	{ }
 };
 
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
drivers/iio/accel/adxl345_spi.c +1 -1
index 850ac616d65e..2f5fc565f1f4 100644
@@ -47,7 +47,7 @@ MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
 static const struct of_device_id adxl345_of_match[] = {
 	{ .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
 	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
-	{ },
+	{ }
 };
 
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 8/8 iio: accel: adxl345: Remove unneeded blank lines

In reply to Andy Shevchenko

Remove unneeded blank lines where they separate the data type definitions and the macros which are using them.

---
v2: no changes

 drivers/iio/accel/adxl345_i2c.c | 3 ---
 drivers/iio/accel/adxl345_spi.c | 3 ---
 2 files changed, 6 deletions(-)
drivers/iio/accel/adxl345_i2c.c +0 -3
index 1ceb9be13cdf..0d0b34cf2b69 100644
@@ -36,7 +36,6 @@ static const struct i2c_device_id adxl345_i2c_id[] = {
 	{ "adxl375", ADXL375 },
 	{ }
 };
-
 MODULE_DEVICE_TABLE(i2c, adxl345_i2c_id);
 
 static const struct of_device_id adxl345_of_match[] = {
@@ -44,7 +43,6 @@ static const struct of_device_id adxl345_of_match[] = {
 	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
 	{ }
 };
-
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
 
 static const struct acpi_device_id adxl345_acpi_match[] = {
@@ -62,7 +60,6 @@ static struct i2c_driver adxl345_i2c_driver = {
 	.probe_new	= adxl345_i2c_probe,
 	.id_table	= adxl345_i2c_id,
 };
-
 module_i2c_driver(adxl345_i2c_driver);
 
 MODULE_AUTHOR("Eva Rachel Retuya <[email protected]>");
drivers/iio/accel/adxl345_spi.c +0 -3
index 2f5fc565f1f4..aaade5808657 100644
@@ -41,7 +41,6 @@ static const struct spi_device_id adxl345_spi_id[] = {
 	{ "adxl375", ADXL375 },
 	{ }
 };
-
 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
 
 static const struct of_device_id adxl345_of_match[] = {
@@ -49,7 +48,6 @@ static const struct of_device_id adxl345_of_match[] = {
 	{ .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
 	{ }
 };
-
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
 
 static const struct acpi_device_id adxl345_acpi_match[] = {
@@ -67,7 +65,6 @@ static struct spi_driver adxl345_spi_driver = {
 	.probe		= adxl345_spi_probe,
 	.id_table	= adxl345_spi_id,
 };
-
 module_spi_driver(adxl345_spi_driver);
 
 MODULE_AUTHOR("Eva Rachel Retuya <[email protected]>");
Signature · 1 line
2.34.1
Tested-by on v2 1/8
#11 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

It's fine to call dev_err_probe() in ->probe() when error code is known. Convert the driver to use dev_err_probe().

Tested on ACPI based platform. Hence, for the whole series,

Quoted 108 lines

---
v2: fixed typo (LKP), shorten one line to satisfy checkpatch

 drivers/iio/accel/adxl345_core.c | 26 +++++++++-----------------
 drivers/iio/accel/adxl345_i2c.c  |  7 ++-----
 drivers/iio/accel/adxl345_spi.c  | 15 +++++----------
 3 files changed, 16 insertions(+), 32 deletions(-)
drivers/iio/accel/adxl345_core.c +9 -17
index ef2240e356e0..078e1029e49d 100644
@@ -222,16 +222,12 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
        int ret;
 
        ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
-       if (ret < 0) {
-               dev_err(dev, "Error reading device ID: %d\n", ret);
-               return ret;
-       }
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Error reading device ID\n");
 
-       if (regval != ADXL345_DEVID) {
-               dev_err(dev, "Invalid device ID: %x, expected %x\n",
-                       regval, ADXL345_DEVID);
-               return -ENODEV;
-       }
+       if (regval != ADXL345_DEVID)
+               return dev_err_probe(dev, -ENODEV, "Invalid device ID: %x, expected %x\n",
+                                    regval, ADXL345_DEVID);
 
        indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
        if (!indio_dev)
@@ -245,10 +241,8 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 
        ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT,
                           data->data_range);
-       if (ret < 0) {
-               dev_err(dev, "Failed to set data range: %d\n", ret);
-               return ret;
-       }
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Failed to set data range\n");
 
        indio_dev->name = name;
        indio_dev->info = &adxl345_info;
@@ -259,10 +253,8 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
        /* Enable measurement mode */
        ret = regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
                           ADXL345_POWER_CTL_MEASURE);
-       if (ret < 0) {
-               dev_err(dev, "Failed to enable measurement mode: %d\n", ret);
-               return ret;
-       }
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Failed to enable measurement mode\n");
 
        ret = devm_add_action_or_reset(dev, adxl345_powerdown, data->regmap);
        if (ret < 0)
drivers/iio/accel/adxl345_i2c.c +2 -5
index 7bc8324c4f07..e3205dce91b8 100644
@@ -28,11 +28,8 @@ static int adxl345_i2c_probe(struct i2c_client *client,
                return -ENODEV;
 
        regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
-       if (IS_ERR(regmap)) {
-               dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
-                       PTR_ERR(regmap));
-               return PTR_ERR(regmap);
-       }
+       if (IS_ERR(regmap))
+               return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
        return adxl345_core_probe(&client->dev, regmap, id->driver_data,
                                  id->name);
drivers/iio/accel/adxl345_spi.c +5 -11
index c752562c5d3b..9223302fdd46 100644
@@ -26,18 +26,13 @@ static int adxl345_spi_probe(struct spi_device *spi)
        struct regmap *regmap;
 
        /* Bail out if max_speed_hz exceeds 5 MHz */
-       if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) {
-               dev_err(&spi->dev, "SPI CLK, %d Hz exceeds 5 MHz\n",
-                       spi->max_speed_hz);
-               return -EINVAL;
-       }
+       if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
+               return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n",
+                                    spi->max_speed_hz);
 
        regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
-       if (IS_ERR(regmap)) {
-               dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
-                       PTR_ERR(regmap));
-               return PTR_ERR(regmap);
-       }
+       if (IS_ERR(regmap))
+               return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
        return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
 }
--

2.34.1

maintainer
#12 lore raw
Quoted 11 lines · On Wed, 23 Feb 2022 11:10:47 +0800 Kai-Heng Feng <[email protected]>…
Quoted 5 lines · Andy Shevchenko

It's fine to call dev_err_probe() in ->probe() when error code is known. Convert the driver to use dev_err_probe().

Tested on ACPI based platform. Hence, for the whole series,

Series applied to the togreg branch of iio.git and pushed out as testing to let 0-day see if it can find anything we missed.

Thanks,

Jonathan

Quoted 109 lines
Quoted 109 lines

---
v2: fixed typo (LKP), shorten one line to satisfy checkpatch

 drivers/iio/accel/adxl345_core.c | 26 +++++++++-----------------
 drivers/iio/accel/adxl345_i2c.c  |  7 ++-----
 drivers/iio/accel/adxl345_spi.c  | 15 +++++----------
 3 files changed, 16 insertions(+), 32 deletions(-)
drivers/iio/accel/adxl345_core.c +9 -17
index ef2240e356e0..078e1029e49d 100644
@@ -222,16 +222,12 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
        int ret;
 
        ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
-       if (ret < 0) {
-               dev_err(dev, "Error reading device ID: %d\n", ret);
-               return ret;
-       }
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Error reading device ID\n");
 
-       if (regval != ADXL345_DEVID) {
-               dev_err(dev, "Invalid device ID: %x, expected %x\n",
-                       regval, ADXL345_DEVID);
-               return -ENODEV;
-       }
+       if (regval != ADXL345_DEVID)
+               return dev_err_probe(dev, -ENODEV, "Invalid device ID: %x, expected %x\n",
+                                    regval, ADXL345_DEVID);
 
        indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
        if (!indio_dev)
@@ -245,10 +241,8 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
 
        ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT,
                           data->data_range);
-       if (ret < 0) {
-               dev_err(dev, "Failed to set data range: %d\n", ret);
-               return ret;
-       }
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Failed to set data range\n");
 
        indio_dev->name = name;
        indio_dev->info = &adxl345_info;
@@ -259,10 +253,8 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap,
        /* Enable measurement mode */
        ret = regmap_write(data->regmap, ADXL345_REG_POWER_CTL,
                           ADXL345_POWER_CTL_MEASURE);
-       if (ret < 0) {
-               dev_err(dev, "Failed to enable measurement mode: %d\n", ret);
-               return ret;
-       }
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Failed to enable measurement mode\n");
 
        ret = devm_add_action_or_reset(dev, adxl345_powerdown, data->regmap);
        if (ret < 0)
drivers/iio/accel/adxl345_i2c.c +2 -5
index 7bc8324c4f07..e3205dce91b8 100644
@@ -28,11 +28,8 @@ static int adxl345_i2c_probe(struct i2c_client *client,
                return -ENODEV;
 
        regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
-       if (IS_ERR(regmap)) {
-               dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
-                       PTR_ERR(regmap));
-               return PTR_ERR(regmap);
-       }
+       if (IS_ERR(regmap))
+               return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
        return adxl345_core_probe(&client->dev, regmap, id->driver_data,
                                  id->name);
drivers/iio/accel/adxl345_spi.c +5 -11
index c752562c5d3b..9223302fdd46 100644
@@ -26,18 +26,13 @@ static int adxl345_spi_probe(struct spi_device *spi)
        struct regmap *regmap;
 
        /* Bail out if max_speed_hz exceeds 5 MHz */
-       if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ) {
-               dev_err(&spi->dev, "SPI CLK, %d Hz exceeds 5 MHz\n",
-                       spi->max_speed_hz);
-               return -EINVAL;
-       }
+       if (spi->max_speed_hz > ADXL345_MAX_SPI_FREQ_HZ)
+               return dev_err_probe(&spi->dev, -EINVAL, "SPI CLK, %d Hz exceeds 5 MHz\n",
+                                    spi->max_speed_hz);
 
        regmap = devm_regmap_init_spi(spi, &adxl345_spi_regmap_config);
-       if (IS_ERR(regmap)) {
-               dev_err(&spi->dev, "Error initializing spi regmap: %ld\n",
-                       PTR_ERR(regmap));
-               return PTR_ERR(regmap);
-       }
+       if (IS_ERR(regmap))
+               return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n");
 
        return adxl345_core_probe(&spi->dev, regmap, id->driver_data, id->name);
 }
--

2.34.1