diff mbox

[3/3] i2c: expose adapter probe and remove probed functions

Message ID 1473108014-30787-4-git-send-email-david@lechnology.com
State New
Headers show

Commit Message

David Lechner Sept. 5, 2016, 8:40 p.m. UTC
Publicly expose functions for detecting devices and removing detected
devices.

Currently an i2c adapter only probes/detects new devices when the adapter
is added or when a new i2c driver is added. This adds a new function,
i2c_adapter_probe(), that allows detecting sensors at any time.

Furthermore, the function i2c_adapter_remove_probed() is added to remove
the devices added by i2c_adapter_probe() at any time.

The intended use of these functions is for LEGO MINDSTORMS sensors. These
are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe()
is called to automatically configure the sensor. When the sensor is
disconnected, i2c_adapter_remove_probed() is called to remove the
automatically configured device.

Signed-off-by: David Lechner <david@lechnology.com>
---
 drivers/i2c/i2c-core.c | 45 ++++++++++++++++++++++++++++++++++++++-------
 include/linux/i2c.h    |  3 +++
 2 files changed, 41 insertions(+), 7 deletions(-)

Comments

Wolfram Sang Sept. 16, 2016, 9:03 p.m. UTC | #1
> The intended use of these functions is for LEGO MINDSTORMS sensors. These
> are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe()
> is called to automatically configure the sensor. When the sensor is
> disconnected, i2c_adapter_remove_probed() is called to remove the
> automatically configured device.

How do you detect the hot-plug events?
David Lechner Sept. 16, 2016, 9:09 p.m. UTC | #2
On 09/16/2016 04:03 PM, Wolfram Sang wrote:
>
>> The intended use of these functions is for LEGO MINDSTORMS sensors. These
>> are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe()
>> is called to automatically configure the sensor. When the sensor is
>> disconnected, i2c_adapter_remove_probed() is called to remove the
>> automatically configured device.
>
> How do you detect the hot-plug events?
>

There are "port" drivers that do this. An I2C adapter is assigned to 
each port via device-tree or platform data struct.

Port driver code if you are interested:
- 
https://github.com/ev3dev/lego-linux-drivers/blob/master/ev3/legoev3_ports_in.c
- 
https://github.com/ev3dev/lego-linux-drivers/blob/master/evb/evb_ports_in.c

In particular, here is where we call the new i2c_adapter_probe() 
function: 
https://github.com/ev3dev/lego-linux-drivers/blob/master/evb/evb_ports_in.c#L543



--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 28436d9..2781a88 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1691,6 +1691,23 @@  static int __process_new_adapter(struct device_driver *d, void *data)
 	return i2c_do_add_adapter(to_i2c_driver(d), data);
 }
 
+/**
+ * i2c_adapter_probe - probe adapter for clients
+ * @adap: The i2c adapter.
+ *
+ * This calls the detect function of each driver that matches the class of the
+ * adapter. This function is called when an adapter is added, so there is no
+ * need to call this manually, unless you have hot-plugable i2c devices that
+ * are connected after the adapter is created.
+ */
+void i2c_adapter_probe(struct i2c_adapter *adap)
+{
+	mutex_lock(&core_lock);
+	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
+	mutex_unlock(&core_lock);
+}
+EXPORT_SYMBOL(i2c_adapter_probe);
+
 static int i2c_register_adapter(struct i2c_adapter *adap)
 {
 	int res = -EINVAL;
@@ -1759,9 +1776,7 @@  static int i2c_register_adapter(struct i2c_adapter *adap)
 		i2c_scan_static_board_info(adap);
 
 	/* Notify drivers */
-	mutex_lock(&core_lock);
-	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
-	mutex_unlock(&core_lock);
+	i2c_adapter_probe(adap);
 
 	return 0;
 
@@ -1904,6 +1919,25 @@  static int __process_removed_adapter(struct device_driver *d, void *data)
 }
 
 /**
+ * i2c_adapter_remove_probed - Remove clients that were automatically detected.
+ * @adap: The i2c adapter.
+ *
+ * This removes all i2c clients from this adapter that were added via driver
+ * detect() functions. This function is called when an adapter is removed, so
+ * there is no need to call this manually, unless you have hot-plugable i2c
+ * devices that are disconnected before the adapter is destroyed.
+ *
+ * Any clients that were added manually (e.g. via sysfs) are not removed.
+ */
+void i2c_adapter_remove_probed(struct i2c_adapter *adap)
+{
+	mutex_lock(&core_lock);
+	bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_removed_adapter);
+	mutex_unlock(&core_lock);
+}
+EXPORT_SYMBOL(i2c_adapter_remove_probed);
+
+/**
  * i2c_del_adapter - unregister I2C adapter
  * @adap: the adapter being unregistered
  * Context: can sleep
@@ -1927,10 +1961,7 @@  void i2c_del_adapter(struct i2c_adapter *adap)
 
 	acpi_i2c_remove_space_handler(adap);
 	/* Tell drivers about this removal */
-	mutex_lock(&core_lock);
-	bus_for_each_drv(&i2c_bus_type, NULL, adap,
-			       __process_removed_adapter);
-	mutex_unlock(&core_lock);
+	i2c_adapter_remove_probed(adap);
 
 	/* Remove devices instantiated from sysfs */
 	mutex_lock_nested(&adap->userspace_clients_lock,
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 3eab858..0016623 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -657,6 +657,9 @@  extern int i2c_add_adapter(struct i2c_adapter *);
 extern void i2c_del_adapter(struct i2c_adapter *);
 extern int i2c_add_numbered_adapter(struct i2c_adapter *);
 
+extern void i2c_adapter_probe(struct i2c_adapter *adap);
+extern void i2c_adapter_remove_probed(struct i2c_adapter *adap);
+
 extern int i2c_register_driver(struct module *, struct i2c_driver *);
 extern void i2c_del_driver(struct i2c_driver *);