[PATCH 3/3] i2c: Export i2c_init_adapter() for use by drivers in early initialisation

From: Chris Wilson
Date: Thu Jun 09 2016 - 04:55:11 EST


Some drivers require use of their i2c adapter long before they can add
the adapter to userspace (i.e. before they establish and expose their
objects in the sysfs kobject tree). Currently i2c_add_adapter()
registers the adapter with userspace, which causes a conflict in the
ordering of the driver's initialisation. Exporting i2c_init_adapter()
allows for the i2c adapter to be used early in the initialisation in
order to detect whether the device the adapter is attached to is even
present. The drivers still need to call i2c_add_adapter() (or
i2c_add_number_adapter()) as per normal to complete their registration.

Signed-off-by: Chris Wilson <chris@xxxxxxxxxxxxxxxxxx>
Cc: Ville SyrjÃlà <ville.syrjala@xxxxxxxxxxxxxxx>
Cc: Wolfram Sang <wsa@xxxxxxxxxxxxx>
Cc: linux-i2c@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx
---
drivers/i2c/i2c-core.c | 16 +++++++++++++++-
include/linux/i2c.h | 1 +
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 91ff70d31ec8..0a3bdb048d65 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -1511,7 +1511,20 @@ static int __process_new_adapter(struct device_driver *d, void *data)
return i2c_do_add_adapter(to_i2c_driver(d), data);
}

-static int i2c_init_adapter(struct i2c_adapter *adap)
+/**
+ * i2c_init_adapter - initialise i2c adapter for internal use
+ * @adapter: the adapter to initialise
+ * Context: any
+ *
+ * This routine is used to initialise an I2C adapter for internal use
+ * prior to registering it with third parties (including userspace). The
+ * driver should still call i2c_add_adapter() or i2c_add_numbered_adapter()
+ * when it is ready to expose the adapter to userspace.
+ *
+ * Returns: 0 on success, or a negative error value if the adapter is not
+ * fully specified for use.
+ */
+int i2c_init_adapter(struct i2c_adapter *adap)
{
/* Only initialise the adapter once. */
if (list_empty(&adap->userspace_clients))
@@ -1546,6 +1559,7 @@ static int i2c_init_adapter(struct i2c_adapter *adap)

return 0;
}
+EXPORT_SYMBOL(i2c_init_adapter);

static int i2c_register_adapter(struct i2c_adapter *adap)
{
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 96a25ae14494..72c76c5efba5 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -639,6 +639,7 @@ i2c_unlock_adapter(struct i2c_adapter *adapter)
/* administration...
*/
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
+extern int i2c_init_adapter(struct i2c_adapter *);
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 *);
--
2.8.1