[PATCH v3 05/24] iio: acpi: Add iio_get_acpi_device_name_and_data() helper function

Andy Shevchenko posted 24 patches 1 month ago
[PATCH v3 05/24] iio: acpi: Add iio_get_acpi_device_name_and_data() helper function
Posted by Andy Shevchenko 1 month ago
A few drivers duplicate the code to retrieve ACPI device instance name.
Some of them want an associated driver data as well.

In order of deduplication introduce the common helper functions.

Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/iio/industrialio-acpi.c | 38 ++++++++++++++++++++++++++++++++-
 include/linux/iio/iio.h         | 10 +++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/industrialio-acpi.c b/drivers/iio/industrialio-acpi.c
index 1e46908f9534..d67a43843799 100644
--- a/drivers/iio/industrialio-acpi.c
+++ b/drivers/iio/industrialio-acpi.c
@@ -2,7 +2,8 @@
 /* IIO ACPI helper functions */
 
 #include <linux/acpi.h>
-#include <linux/dev_printk.h>
+#include <linux/device.h>
+#include <linux/export.h>
 #include <linux/iio/iio.h>
 #include <linux/sprintf.h>
 
@@ -87,3 +88,38 @@ bool iio_read_acpi_mount_matrix(struct device *dev,
 	return ret;
 }
 EXPORT_SYMBOL_GPL(iio_read_acpi_mount_matrix);
+
+/**
+ * iio_get_acpi_device_name_and_data() - Return ACPI device instance name and driver data
+ * @dev:		Device structure
+ * @data:		Optional pointer to return driver data
+ *
+ * When device was enumerated by ACPI ID matching, the user might
+ * want to set description for the physical chip. In such cases
+ * the ACPI device instance name might be used. This call may be
+ * performed to retrieve this information.
+ *
+ * NOTE: This helper function exists only for backward compatibility,
+ * do not use in a new code!
+ *
+ * Returns: ACPI device instance name or %NULL.
+ */
+const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data)
+{
+	const struct acpi_device_id *id;
+	acpi_handle handle;
+
+	handle = ACPI_HANDLE(dev);
+	if (!handle)
+		return NULL;
+
+	id = acpi_match_device(dev->driver->acpi_match_table, dev);
+	if (!id)
+		return NULL;
+
+	if (data)
+		*data = (const void *)id->driver_data;
+
+	return dev_name(dev);
+}
+EXPORT_SYMBOL_GPL(iio_get_acpi_device_name_and_data);
diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index 3a9b57187a95..445d6666a291 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -831,6 +831,7 @@ int iio_device_resume_triggering(struct iio_dev *indio_dev);
 bool iio_read_acpi_mount_matrix(struct device *dev,
 				struct iio_mount_matrix *orientation,
 				char *acpi_method);
+const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data);
 #else
 static inline bool iio_read_acpi_mount_matrix(struct device *dev,
 					      struct iio_mount_matrix *orientation,
@@ -838,7 +839,16 @@ static inline bool iio_read_acpi_mount_matrix(struct device *dev,
 {
 	return false;
 }
+static inline const char *
+iio_get_acpi_device_name_and_data(struct device *dev, const void **data)
+{
+	return NULL;
+}
 #endif
+static inline const char *iio_get_acpi_device_name(struct device *dev)
+{
+	return iio_get_acpi_device_name_and_data(dev, NULL);
+}
 
 /**
  * iio_get_current_scan_type - Get the current scan type for a channel
-- 
2.43.0.rc1.1336.g36b5255a03ac
Re: [PATCH v3 05/24] iio: acpi: Add iio_get_acpi_device_name_and_data() helper function
Posted by Jonathan Cameron 1 month ago
On Thu, 24 Oct 2024 22:04:54 +0300
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> A few drivers duplicate the code to retrieve ACPI device instance name.
> Some of them want an associated driver data as well.
> 
> In order of deduplication introduce the common helper functions.
It's a weird function, but clearly useful to reduce duplication of
weird code.  So fair enough.  Thanks for adding the do not use message.
Applied.

> 
> Reviewed-by: Hans de Goede <hdegoede@redhat.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/iio/industrialio-acpi.c | 38 ++++++++++++++++++++++++++++++++-
>  include/linux/iio/iio.h         | 10 +++++++++
>  2 files changed, 47 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/industrialio-acpi.c b/drivers/iio/industrialio-acpi.c
> index 1e46908f9534..d67a43843799 100644
> --- a/drivers/iio/industrialio-acpi.c
> +++ b/drivers/iio/industrialio-acpi.c
> @@ -2,7 +2,8 @@
>  /* IIO ACPI helper functions */
>  
>  #include <linux/acpi.h>
> -#include <linux/dev_printk.h>
> +#include <linux/device.h>
> +#include <linux/export.h>
>  #include <linux/iio/iio.h>
>  #include <linux/sprintf.h>
>  
> @@ -87,3 +88,38 @@ bool iio_read_acpi_mount_matrix(struct device *dev,
>  	return ret;
>  }
>  EXPORT_SYMBOL_GPL(iio_read_acpi_mount_matrix);
> +
> +/**
> + * iio_get_acpi_device_name_and_data() - Return ACPI device instance name and driver data
> + * @dev:		Device structure
> + * @data:		Optional pointer to return driver data
> + *
> + * When device was enumerated by ACPI ID matching, the user might
> + * want to set description for the physical chip. In such cases
> + * the ACPI device instance name might be used. This call may be
> + * performed to retrieve this information.
> + *
> + * NOTE: This helper function exists only for backward compatibility,
> + * do not use in a new code!
> + *
> + * Returns: ACPI device instance name or %NULL.
> + */
> +const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data)
> +{
> +	const struct acpi_device_id *id;
> +	acpi_handle handle;
> +
> +	handle = ACPI_HANDLE(dev);
> +	if (!handle)
> +		return NULL;
> +
> +	id = acpi_match_device(dev->driver->acpi_match_table, dev);
> +	if (!id)
> +		return NULL;
> +
> +	if (data)
> +		*data = (const void *)id->driver_data;
> +
> +	return dev_name(dev);
> +}
> +EXPORT_SYMBOL_GPL(iio_get_acpi_device_name_and_data);
> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
> index 3a9b57187a95..445d6666a291 100644
> --- a/include/linux/iio/iio.h
> +++ b/include/linux/iio/iio.h
> @@ -831,6 +831,7 @@ int iio_device_resume_triggering(struct iio_dev *indio_dev);
>  bool iio_read_acpi_mount_matrix(struct device *dev,
>  				struct iio_mount_matrix *orientation,
>  				char *acpi_method);
> +const char *iio_get_acpi_device_name_and_data(struct device *dev, const void **data);
>  #else
>  static inline bool iio_read_acpi_mount_matrix(struct device *dev,
>  					      struct iio_mount_matrix *orientation,
> @@ -838,7 +839,16 @@ static inline bool iio_read_acpi_mount_matrix(struct device *dev,
>  {
>  	return false;
>  }
> +static inline const char *
> +iio_get_acpi_device_name_and_data(struct device *dev, const void **data)
> +{
> +	return NULL;
> +}
>  #endif
> +static inline const char *iio_get_acpi_device_name(struct device *dev)
> +{
> +	return iio_get_acpi_device_name_and_data(dev, NULL);
> +}
>  
>  /**
>   * iio_get_current_scan_type - Get the current scan type for a channel