Skip to contentSkip to conversation

Input: adxl34x - use device core to create driver-specific device attributes

PATCH Superseded Dmitry Torokhov 2 participants 6 messages Jun 9, 2024 – Jun 10, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dmitry Torokhov

author maintainer
#0 lore raw

PATCH 1/3 Input: adxl34x - use device core to create driver-specific device attributes

Instead of creating driver-specific device attributes with sysfs_create_group() have device core do this by setting up dev_groups pointer in the driver structure.

---
 drivers/input/misc/adxl34x-i2c.c |  1 +
 drivers/input/misc/adxl34x-spi.c |  1 +
 drivers/input/misc/adxl34x.c     | 15 +++++++--------
 drivers/input/misc/adxl34x.h     |  1 +
 4 files changed, 10 insertions(+), 8 deletions(-)
drivers/input/misc/adxl34x-i2c.c +1 -0
index d4014e367c77..7531c7b2d657 100644
@@ -132,6 +132,7 @@ MODULE_DEVICE_TABLE(of, adxl34x_of_id);
 static struct i2c_driver adxl34x_driver = {
 	.driver = {
 		.name = "adxl34x",
+		.dev_groups = adxl34x_groups,
 		.pm = pm_sleep_ptr(&adxl34x_pm),
 		.of_match_table = adxl34x_of_id,
 	},
drivers/input/misc/adxl34x-spi.c +1 -0
index f1094a8ccdd5..2befcc4df0be 100644
@@ -97,6 +97,7 @@ static void adxl34x_spi_remove(struct spi_device *spi)
 static struct spi_driver adxl34x_driver = {
 	.driver = {
 		.name = "adxl34x",
+		.dev_groups = adxl34x_groups,
 		.pm = pm_sleep_ptr(&adxl34x_pm),
 	},
 	.probe   = adxl34x_spi_probe,
drivers/input/misc/adxl34x.c +7 -8
index a3f45e0ee0c7..fbe5a56c19d1 100644
@@ -664,6 +664,12 @@ static const struct attribute_group adxl34x_attr_group = {
 	.attrs = adxl34x_attributes,
 };
 
+const struct attribute_group *adxl34x_groups[] = {
+	&adxl34x_attr_group,
+	NULL
+};
+EXPORT_SYMBOL_GPL(adxl34x_groups);
+
 static int adxl34x_input_open(struct input_dev *input)
 {
 	struct adxl34x *ac = input_get_drvdata(input);
@@ -823,13 +829,9 @@ struct adxl34x *adxl34x_probe(struct device *dev, int irq,
 		goto err_free_mem;
 	}
 
-	err = sysfs_create_group(&dev->kobj, &adxl34x_attr_group);
-	if (err)
-		goto err_free_irq;
-
 	err = input_register_device(input_dev);
 	if (err)
-		goto err_remove_attr;
+		goto err_free_irq;
 
 	AC_WRITE(ac, OFSX, pdata->x_axis_offset);
 	ac->hwcal.x = pdata->x_axis_offset;
@@ -889,8 +891,6 @@ struct adxl34x *adxl34x_probe(struct device *dev, int irq,
 
 	return ac;
 
- err_remove_attr:
-	sysfs_remove_group(&dev->kobj, &adxl34x_attr_group);
  err_free_irq:
 	free_irq(ac->irq, ac);
  err_free_mem:
@@ -903,7 +903,6 @@ EXPORT_SYMBOL_GPL(adxl34x_probe);
 
 void adxl34x_remove(struct adxl34x *ac)
 {
-	sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
 	free_irq(ac->irq, ac);
 	input_unregister_device(ac->input);
 	dev_dbg(ac->dev, "unregistered accelerometer\n");
drivers/input/misc/adxl34x.h +1 -0
index f9272a2e7a96..67e0ddc5c3eb 100644
@@ -26,5 +26,6 @@ struct adxl34x *adxl34x_probe(struct device *dev, int irq,
 void adxl34x_remove(struct adxl34x *ac);
 
 extern const struct dev_pm_ops adxl34x_pm;
+extern const struct attribute_group *adxl34x_groups[];
 
 #endif
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
author maintainer
#1 lore raw

PATCH 2/3 Input: adxl34x - switch to using managed resources

Switch the driver to use managed resources to simplify error handling.

---
 drivers/input/misc/adxl34x-i2c.c |  8 ---
 drivers/input/misc/adxl34x-spi.c |  8 ---
 drivers/input/misc/adxl34x.c     | 85 +++++++++++---------------------
 drivers/input/misc/adxl34x.h     |  1 -
 4 files changed, 30 insertions(+), 72 deletions(-)
drivers/input/misc/adxl34x-i2c.c +0 -8
index 7531c7b2d657..c05d898898e8 100644
@@ -98,13 +98,6 @@ static int adxl34x_i2c_probe(struct i2c_client *client)
 	return 0;
 }
 
-static void adxl34x_i2c_remove(struct i2c_client *client)
-{
-	struct adxl34x *ac = i2c_get_clientdata(client);
-
-	adxl34x_remove(ac);
-}
-
 static const struct i2c_device_id adxl34x_id[] = {
 	{ "adxl34x" },
 	{ }
@@ -137,7 +130,6 @@ static struct i2c_driver adxl34x_driver = {
 		.of_match_table = adxl34x_of_id,
 	},
 	.probe    = adxl34x_i2c_probe,
-	.remove   = adxl34x_i2c_remove,
 	.id_table = adxl34x_id,
 };
 
drivers/input/misc/adxl34x-spi.c +0 -8
index 2befcc4df0be..fd716d861832 100644
@@ -87,13 +87,6 @@ static int adxl34x_spi_probe(struct spi_device *spi)
 	return 0;
 }
 
-static void adxl34x_spi_remove(struct spi_device *spi)
-{
-	struct adxl34x *ac = spi_get_drvdata(spi);
-
-	adxl34x_remove(ac);
-}
-
 static struct spi_driver adxl34x_driver = {
 	.driver = {
 		.name = "adxl34x",
@@ -101,7 +94,6 @@ static struct spi_driver adxl34x_driver = {
 		.pm = pm_sleep_ptr(&adxl34x_pm),
 	},
 	.probe   = adxl34x_spi_probe,
-	.remove  = adxl34x_spi_remove,
 };
 
 module_spi_driver(adxl34x_driver);
drivers/input/misc/adxl34x.c +30 -55

156 lines not on this page · raw message

drivers/input/misc/adxl34x.h +0 -1
index 67e0ddc5c3eb..718e90c2046d 100644
@@ -23,7 +23,6 @@ struct adxl34x_bus_ops {
 struct adxl34x *adxl34x_probe(struct device *dev, int irq,
 			      bool fifo_delay_default,
 			      const struct adxl34x_bus_ops *bops);
-void adxl34x_remove(struct adxl34x *ac);
 
 extern const struct dev_pm_ops adxl34x_pm;
 extern const struct attribute_group *adxl34x_groups[];
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
#2 lore raw

Re: [PATCH 2/3] Input: adxl34x - switch to using managed resources

Quoted 133 lines · Dmitry Torokhov

Switch the driver to use managed resources to simplify error handling.

---
 drivers/input/misc/adxl34x-i2c.c |  8 --- drivers/input/misc/adxl34x-spi.c |  8 --- drivers/input/misc/adxl34x.c     | 85 +++++++++++--------------------- drivers/input/misc/adxl34x.h     |  1 -

 4 files changed, 30 insertions(+), 72 deletions(-)
drivers/input/misc/adxl34x- renamed from drivers/input/misc/adxl34x-i2c.c +0 -0

i2c.c
index 7531c7b2d657..c05d898898e8 100644

drivers/input/misc/adxl34x-i2c.c +0 -0
@@ -98,13 +98,6 @@ static int adxl34x_i2c_probe(struct i2c_client *client)
  	return 0;
  }

drivers/input/misc/adxl34x-i2c.c +0 -7
-static void adxl34x_i2c_remove(struct i2c_client *client)
-{
-	struct adxl34x *ac = i2c_get_clientdata(client);
-
-	adxl34x_remove(ac);
-}
-

 static const struct i2c_device_id adxl34x_id[] = { { "adxl34x" },
  { }

drivers/input/misc/adxl34x-i2c.c +0 -1
@@ -137,7 +130,6 @@ static struct i2c_driver adxl34x_driver = {
  		.of_match_table = adxl34x_of_id,
  	},
  	.probe    = adxl34x_i2c_probe,
-	.remove   = adxl34x_i2c_remove,
  	.id_table = adxl34x_id,
  };

drivers/input/misc/adxl34x- renamed from drivers/input/misc/adxl34x-spi.c +0 -0

spi.c
index 2befcc4df0be..fd716d861832 100644

drivers/input/misc/adxl34x-spi.c +0 -0
@@ -87,13 +87,6 @@ static int adxl34x_spi_probe(struct spi_device *spi)
  	return 0;
  }

drivers/input/misc/adxl34x-spi.c +0 -7
-static void adxl34x_spi_remove(struct spi_device *spi)
-{
-	struct adxl34x *ac = spi_get_drvdata(spi);
-
-	adxl34x_remove(ac);
-}
-

 static struct spi_driver adxl34x_driver = {
  .driver = {
  .name = "adxl34x",

drivers/input/misc/adxl34x-spi.c +0 -1
@@ -101,7 +94,6 @@ static struct spi_driver adxl34x_driver = {
  		.pm = pm_sleep_ptr(&adxl34x_pm),
  	},
  	.probe   = adxl34x_spi_probe,
-	.remove  = adxl34x_spi_remove,
  };


 module_spi_driver(adxl34x_driver);

drivers/input/misc/adxl34x.c +1 -1
index fbe5a56c19d1..c6c34005f5d2 100644
@@ -707,21 +707,21 @@ struct adxl34x *adxl34x_probe(struct device *dev, int
 irq,
  	struct adxl34x *ac;
  	struct input_dev *input_dev;
  	const struct adxl34x_platform_data *pdata;
-	int err, range, i;
+	int error, range, i;
  	int revid;


  if (!irq) {
  dev_err(dev, "no IRQ?\n");

drivers/input/misc/adxl34x.c +1 -2
-		err = -ENODEV;
-		goto err_out;
+		return ERR_PTR(-ENODEV);

  }

drivers/input/misc/adxl34x.c +7 -6
-	ac = kzalloc(sizeof(*ac), GFP_KERNEL);
-	input_dev = input_allocate_device();
-	if (!ac || !input_dev) {
-		err = -ENOMEM;
-		goto err_free_mem;
-	}
+	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
+	if (!ac)
+		return ERR_PTR(-ENOMEM);
+
+	input_dev = devm_input_allocate_device(dev);
+	if (!input_dev)
+		return ERR_PTR(-ENOMEM);


  ac->fifo_delay = fifo_delay_default;

drivers/input/misc/adxl34x.c +0 -0
@@ -754,14 +754,12 @@ struct adxl34x *adxl34x_probe(struct device *dev, int
 irq,
  		break;

  default:
  dev_err(dev, "Failed to probe %s\n", input_dev->name);

drivers/input/misc/adxl34x.c +1 -2
-		err = -ENODEV;
-		goto err_free_mem;
+		return ERR_PTR(-ENODEV);

  }

  snprintf(ac->phys, sizeof(ac->phys), "%s/input0", dev_name(dev));
  input_dev->phys = ac->phys;

drivers/input/misc/adxl34x.c +0 -1
-	input_dev->dev.parent = dev;

  input_dev->id.product = ac->model;
  input_dev->id.bustype = bops->bustype;
  input_dev->open = adxl34x_input_open;

drivers/input/misc/adxl34x.c +0 -0
@@ -769,18 +767,12 @@ struct adxl34x *adxl34x_probe(struct device *dev, int
 irq,


  input_set_drvdata(input_dev, ac);

drivers/input/misc/adxl34x.c +0 -2
-	__set_bit(ac->pdata.ev_type, input_dev->evbit);
-

  if (ac->pdata.ev_type == EV_REL) {

drivers/input/misc/adxl34x.c +3 -3
-		__set_bit(REL_X, input_dev->relbit);
-		__set_bit(REL_Y, input_dev->relbit);
-		__set_bit(REL_Z, input_dev->relbit);
+		input_set_capability(input_dev, EV_REL, REL_X);
+		input_set_capability(input_dev, EV_REL, REL_Y);
+		input_set_capability(input_dev, EV_REL, REL_Z);

  } else {
  /* EV_ABS */

drivers/input/misc/adxl34x.c struct adxl34x *adxl34x_probe(struct device *dev, int irq, · line 782

Quoted 3 lines
drivers/input/misc/adxl34x.c +0 -3
-		__set_bit(ABS_X, input_dev->absbit);
-		__set_bit(ABS_Y, input_dev->absbit);
-		__set_bit(ABS_Z, input_dev->absbit);

Are these somehow default or already set? I guess in input_set_abs_params()... Anyways, the move from "raw" __set_bits() to input APIs should maybe be done in a separate patch? They are unrelated with the current one.

- Nuno Sá

author maintainer
#3 lore raw

PATCH 3/3 Input: adxl34x- switch to using "guard" notation

In reply to Dmitry Torokhov

Switch to using guard(mutex)() notation to acquire and automatically release mutexes.

---
 drivers/input/misc/adxl34x.c | 61 ++++++++++++------------------------
 1 file changed, 20 insertions(+), 41 deletions(-)
drivers/input/misc/adxl34x.c +20 -41
index c6c34005f5d2..7cafbf8d5f1a 100644
@@ -241,7 +241,8 @@ static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
 
 	ac->bops->read_block(ac->dev, DATAX0, DATAZ1 - DATAX0 + 1, buf);
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
+
 	ac->saved.x = (s16) le16_to_cpu(buf[0]);
 	axis->x = ac->saved.x;
 
@@ -250,7 +251,6 @@ static void adxl34x_get_triple(struct adxl34x *ac, struct axis_triple *axis)
 
 	ac->saved.z = (s16) le16_to_cpu(buf[2]);
 	axis->z = ac->saved.z;
-	mutex_unlock(&ac->mutex);
 }
 
 static void adxl34x_service_ev_fifo(struct adxl34x *ac)
@@ -416,15 +416,13 @@ static int adxl34x_suspend(struct device *dev)
 {
 	struct adxl34x *ac = dev_get_drvdata(dev);
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	if (!ac->suspended && !ac->disabled && ac->opened)
 		__adxl34x_disable(ac);
 
 	ac->suspended = true;
 
-	mutex_unlock(&ac->mutex);
-
 	return 0;
 }
 
@@ -432,15 +430,13 @@ static int adxl34x_resume(struct device *dev)
 {
 	struct adxl34x *ac = dev_get_drvdata(dev);
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	if (ac->suspended && !ac->disabled && ac->opened)
 		__adxl34x_enable(ac);
 
 	ac->suspended = false;
 
-	mutex_unlock(&ac->mutex);
-
 	return 0;
 }
 
@@ -464,7 +460,7 @@ static ssize_t adxl34x_disable_store(struct device *dev,
 	if (error)
 		return error;
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	if (!ac->suspended && ac->opened) {
 		if (val) {
@@ -478,8 +474,6 @@ static ssize_t adxl34x_disable_store(struct device *dev,
 
 	ac->disabled = !!val;
 
-	mutex_unlock(&ac->mutex);
-
 	return count;
 }
 
@@ -489,16 +483,13 @@ static ssize_t adxl34x_calibrate_show(struct device *dev,
 				      struct device_attribute *attr, char *buf)
 {
 	struct adxl34x *ac = dev_get_drvdata(dev);
-	ssize_t count;
 
-	mutex_lock(&ac->mutex);
-	count = sprintf(buf, "%d,%d,%d\n",
-			ac->hwcal.x * 4 + ac->swcal.x,
-			ac->hwcal.y * 4 + ac->swcal.y,
-			ac->hwcal.z * 4 + ac->swcal.z);
-	mutex_unlock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
-	return count;
+	return sprintf(buf, "%d,%d,%d\n",
+		       ac->hwcal.x * 4 + ac->swcal.x,
+		       ac->hwcal.y * 4 + ac->swcal.y,
+		       ac->hwcal.z * 4 + ac->swcal.z);
 }
 
 static ssize_t adxl34x_calibrate_store(struct device *dev,
@@ -512,7 +503,8 @@ static ssize_t adxl34x_calibrate_store(struct device *dev,
 	 * We use HW calibration and handle the remaining bits in SW. (4mg/LSB)
 	 */
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
+
 	ac->hwcal.x -= (ac->saved.x / 4);
 	ac->swcal.x = ac->saved.x % 4;
 
@@ -525,7 +517,6 @@ static ssize_t adxl34x_calibrate_store(struct device *dev,
 	AC_WRITE(ac, OFSX, (s8) ac->hwcal.x);
 	AC_WRITE(ac, OFSY, (s8) ac->hwcal.y);
 	AC_WRITE(ac, OFSZ, (s8) ac->hwcal.z);
-	mutex_unlock(&ac->mutex);
 
 	return count;
 }
@@ -553,15 +544,13 @@ static ssize_t adxl34x_rate_store(struct device *dev,
 	if (error)
 		return error;
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	ac->pdata.data_rate = RATE(val);
 	AC_WRITE(ac, BW_RATE,
 		 ac->pdata.data_rate |
 			(ac->pdata.low_power_mode ? LOW_POWER : 0));
 
-	mutex_unlock(&ac->mutex);
-
 	return count;
 }
 
@@ -588,7 +577,7 @@ static ssize_t adxl34x_autosleep_store(struct device *dev,
 	if (error)
 		return error;
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	if (val)
 		ac->pdata.power_mode |= (PCTL_AUTO_SLEEP | PCTL_LINK);
@@ -598,8 +587,6 @@ static ssize_t adxl34x_autosleep_store(struct device *dev,
 	if (!ac->disabled && !ac->suspended && ac->opened)
 		AC_WRITE(ac, POWER_CTL, ac->pdata.power_mode | PCTL_MEASURE);
 
-	mutex_unlock(&ac->mutex);
-
 	return count;
 }
 
@@ -610,14 +597,11 @@ static ssize_t adxl34x_position_show(struct device *dev,
 				 struct device_attribute *attr, char *buf)
 {
 	struct adxl34x *ac = dev_get_drvdata(dev);
-	ssize_t count;
 
-	mutex_lock(&ac->mutex);
-	count = sprintf(buf, "(%d, %d, %d)\n",
-			ac->saved.x, ac->saved.y, ac->saved.z);
-	mutex_unlock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
-	return count;
+	return sprintf(buf, "(%d, %d, %d)\n",
+		       ac->saved.x, ac->saved.y, ac->saved.z);
 }
 
 static DEVICE_ATTR(position, S_IRUGO, adxl34x_position_show, NULL);
@@ -638,9 +622,8 @@ static ssize_t adxl34x_write_store(struct device *dev,
 	if (error)
 		return error;
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 	AC_WRITE(ac, val >> 8, val & 0xFF);
-	mutex_unlock(&ac->mutex);
 
 	return count;
 }
@@ -674,15 +657,13 @@ static int adxl34x_input_open(struct input_dev *input)
 {
 	struct adxl34x *ac = input_get_drvdata(input);
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	if (!ac->suspended && !ac->disabled)
 		__adxl34x_enable(ac);
 
 	ac->opened = true;
 
-	mutex_unlock(&ac->mutex);
-
 	return 0;
 }
 
@@ -690,14 +671,12 @@ static void adxl34x_input_close(struct input_dev *input)
 {
 	struct adxl34x *ac = input_get_drvdata(input);
 
-	mutex_lock(&ac->mutex);
+	guard(mutex)(&ac->mutex);
 
 	if (!ac->suspended && !ac->disabled)
 		__adxl34x_disable(ac);
 
 	ac->opened = false;
-
-	mutex_unlock(&ac->mutex);
 }
 
 struct adxl34x *adxl34x_probe(struct device *dev, int irq,
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog