diff mbox

[1/2] i2c: core: Allow the driver to override the default i2c_bus match behavior

Message ID 20170722185537.12696-1-hdegoede@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Hans de Goede July 22, 2017, 6:55 p.m. UTC
Some ACPI devices report multiple ids for a single i2c_client, while not
really implementing the hw-interface asociated with some of these ids.

For some of these devices calling probe and having probe fail with
-ENODEV is a problem in itself as this causes the device to be
powered-up and down again (causes its PS0 and PS3 ACPI methods to be
executed) which puts some devices in an unusable state.

This commit adds a match callback to i2c_driver, allowing drivers to
override the default i2c_bus match behavior and tell the core they
are not the right driver for the device, avoiding i2c_bus_type.probe
getting called, avoiding the undesirable power up / down cycle.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/i2c/i2c-core-base.c | 12 +++++++++---
 include/linux/i2c.h         |  6 ++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

Comments

Hans de Goede Aug. 14, 2017, 8:13 p.m. UTC | #1
Hi,

On 22-07-17 20:55, Hans de Goede wrote:
> Some ACPI devices report multiple ids for a single i2c_client, while not
> really implementing the hw-interface asociated with some of these ids.
> 
> For some of these devices calling probe and having probe fail with
> -ENODEV is a problem in itself as this causes the device to be
> powered-up and down again (causes its PS0 and PS3 ACPI methods to be
> executed) which puts some devices in an unusable state.
> 
> This commit adds a match callback to i2c_driver, allowing drivers to
> override the default i2c_bus match behavior and tell the core they
> are not the right driver for the device, avoiding i2c_bus_type.probe
> getting called, avoiding the undesirable power up / down cycle.
> 
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>

What is the status of this series ? I thought there was agreement
on merging this through the i2c (wsa/linux.git) tree ?

Regards,

Hans


> ---
>   drivers/i2c/i2c-core-base.c | 12 +++++++++---
>   include/linux/i2c.h         |  6 ++++++
>   2 files changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
> index c89dac7fd2e7..2c6702f2347b 100644
> --- a/drivers/i2c/i2c-core-base.c
> +++ b/drivers/i2c/i2c-core-base.c
> @@ -98,8 +98,16 @@ EXPORT_SYMBOL_GPL(i2c_match_id);
>   static int i2c_device_match(struct device *dev, struct device_driver *drv)
>   {
>   	struct i2c_client	*client = i2c_verify_client(dev);
> -	struct i2c_driver	*driver;
> +	struct i2c_driver	*driver = to_i2c_driver(drv);
> +	int ret;
>   
> +	if (driver->match && client) {
> +		ret = driver->match(client);
> +		if (ret < 0)
> +			return 0;
> +		if (ret > 0)
> +			return 1;
> +	}
>   
>   	/* Attempt an OF style match */
>   	if (i2c_of_match_device(drv->of_match_table, client))
> @@ -109,8 +117,6 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv)
>   	if (acpi_driver_match_device(dev, drv))
>   		return 1;
>   
> -	driver = to_i2c_driver(drv);
> -
>   	/* Finally an I2C match */
>   	if (i2c_match_id(driver->id_table, client))
>   		return 1;
> diff --git a/include/linux/i2c.h b/include/linux/i2c.h
> index 00ca5b86a753..670577c82bc3 100644
> --- a/include/linux/i2c.h
> +++ b/include/linux/i2c.h
> @@ -139,6 +139,9 @@ enum i2c_alert_protocol {
>    * struct i2c_driver - represent an I2C device driver
>    * @class: What kind of i2c device we instantiate (for detect)
>    * @attach_adapter: Callback for bus addition (deprecated)
> + * @match: Allows the driver to override the default i2c_bus match behavior
> + *         return < 0 to fail the match, > 0 to force a match, 0 to fallback
> + *         to default id matching
>    * @probe: Callback for device binding - soon to be deprecated
>    * @probe_new: New callback for device binding
>    * @remove: Callback for device unbinding
> @@ -180,6 +183,9 @@ struct i2c_driver {
>   	 */
>   	int (*attach_adapter)(struct i2c_adapter *) __deprecated;
>   
> +	/* Set this to override standard i2c_bus match behavior */
> +	int (*match)(struct i2c_client *);
> +
>   	/* Standard driver model interfaces */
>   	int (*probe)(struct i2c_client *, const struct i2c_device_id *);
>   	int (*remove)(struct i2c_client *);
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wolfram Sang Aug. 14, 2017, 9:21 p.m. UTC | #2
On Mon, Aug 14, 2017 at 10:13:23PM +0200, Hans de Goede wrote:
> Hi,
> 
> On 22-07-17 20:55, Hans de Goede wrote:
> >Some ACPI devices report multiple ids for a single i2c_client, while not
> >really implementing the hw-interface asociated with some of these ids.
> >
> >For some of these devices calling probe and having probe fail with
> >-ENODEV is a problem in itself as this causes the device to be
> >powered-up and down again (causes its PS0 and PS3 ACPI methods to be
> >executed) which puts some devices in an unusable state.
> >
> >This commit adds a match callback to i2c_driver, allowing drivers to
> >override the default i2c_bus match behavior and tell the core they
> >are not the right driver for the device, avoiding i2c_bus_type.probe
> >getting called, avoiding the undesirable power up / down cycle.
> >
> >Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> 
> What is the status of this series ? I thought there was agreement
> on merging this through the i2c (wsa/linux.git) tree ?

Yes, but I need to review the I2C core changes first.
diff mbox

Patch

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index c89dac7fd2e7..2c6702f2347b 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -98,8 +98,16 @@  EXPORT_SYMBOL_GPL(i2c_match_id);
 static int i2c_device_match(struct device *dev, struct device_driver *drv)
 {
 	struct i2c_client	*client = i2c_verify_client(dev);
-	struct i2c_driver	*driver;
+	struct i2c_driver	*driver = to_i2c_driver(drv);
+	int ret;
 
+	if (driver->match && client) {
+		ret = driver->match(client);
+		if (ret < 0)
+			return 0;
+		if (ret > 0)
+			return 1;
+	}
 
 	/* Attempt an OF style match */
 	if (i2c_of_match_device(drv->of_match_table, client))
@@ -109,8 +117,6 @@  static int i2c_device_match(struct device *dev, struct device_driver *drv)
 	if (acpi_driver_match_device(dev, drv))
 		return 1;
 
-	driver = to_i2c_driver(drv);
-
 	/* Finally an I2C match */
 	if (i2c_match_id(driver->id_table, client))
 		return 1;
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 00ca5b86a753..670577c82bc3 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -139,6 +139,9 @@  enum i2c_alert_protocol {
  * struct i2c_driver - represent an I2C device driver
  * @class: What kind of i2c device we instantiate (for detect)
  * @attach_adapter: Callback for bus addition (deprecated)
+ * @match: Allows the driver to override the default i2c_bus match behavior
+ *         return < 0 to fail the match, > 0 to force a match, 0 to fallback
+ *         to default id matching
  * @probe: Callback for device binding - soon to be deprecated
  * @probe_new: New callback for device binding
  * @remove: Callback for device unbinding
@@ -180,6 +183,9 @@  struct i2c_driver {
 	 */
 	int (*attach_adapter)(struct i2c_adapter *) __deprecated;
 
+	/* Set this to override standard i2c_bus match behavior */
+	int (*match)(struct i2c_client *);
+
 	/* Standard driver model interfaces */
 	int (*probe)(struct i2c_client *, const struct i2c_device_id *);
 	int (*remove)(struct i2c_client *);