Skip to content

Commit 373c612

Browse files
Russell King (Oracle)wsakernel
Russell King (Oracle)
authored andcommitted
i2c: add fwnode APIs
Add fwnode APIs for finding and getting I2C adapters, which will be used by the SFP code. These are passed the fwnode corresponding to the adapter, and return the I2C adapter. It is the responsibility of the caller to find the appropriate fwnode. We keep the DT and ACPI interfaces, but where appropriate, recode them to use the fwnode interfaces internally. Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com> Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> Signed-off-by: Wolfram Sang <wsa@kernel.org>
1 parent 1b929c0 commit 373c612

File tree

4 files changed

+120
-81
lines changed

4 files changed

+120
-81
lines changed

drivers/i2c/i2c-core-acpi.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,7 @@ EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle);
442442

443443
static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
444444
{
445-
struct device *dev;
446-
struct i2c_client *client;
447-
448-
dev = bus_find_device_by_acpi_dev(&i2c_bus_type, adev);
449-
if (!dev)
450-
return NULL;
451-
452-
client = i2c_verify_client(dev);
453-
if (!client)
454-
put_device(dev);
455-
456-
return client;
445+
return i2c_find_device_by_fwnode(acpi_fwnode_handle(adev));
457446
}
458447

459448
static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,

drivers/i2c/i2c-core-base.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,35 @@ void i2c_unregister_device(struct i2c_client *client)
10121012
}
10131013
EXPORT_SYMBOL_GPL(i2c_unregister_device);
10141014

1015+
/**
1016+
* i2c_find_device_by_fwnode() - find an i2c_client for the fwnode
1017+
* @fwnode: &struct fwnode_handle corresponding to the &struct i2c_client
1018+
*
1019+
* Look up and return the &struct i2c_client corresponding to the @fwnode.
1020+
* If no client can be found, or @fwnode is NULL, this returns NULL.
1021+
*
1022+
* The user must call put_device(&client->dev) once done with the i2c client.
1023+
*/
1024+
struct i2c_client *i2c_find_device_by_fwnode(struct fwnode_handle *fwnode)
1025+
{
1026+
struct i2c_client *client;
1027+
struct device *dev;
1028+
1029+
if (!fwnode)
1030+
return NULL;
1031+
1032+
dev = bus_find_device_by_fwnode(&i2c_bus_type, fwnode);
1033+
if (!dev)
1034+
return NULL;
1035+
1036+
client = i2c_verify_client(dev);
1037+
if (!client)
1038+
put_device(dev);
1039+
1040+
return client;
1041+
}
1042+
EXPORT_SYMBOL(i2c_find_device_by_fwnode);
1043+
10151044

10161045
static const struct i2c_device_id dummy_id[] = {
10171046
{ "dummy", 0 },
@@ -1761,6 +1790,75 @@ int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter)
17611790
}
17621791
EXPORT_SYMBOL_GPL(devm_i2c_add_adapter);
17631792

1793+
static int i2c_dev_or_parent_fwnode_match(struct device *dev, const void *data)
1794+
{
1795+
if (dev_fwnode(dev) == data)
1796+
return 1;
1797+
1798+
if (dev->parent && dev_fwnode(dev->parent) == data)
1799+
return 1;
1800+
1801+
return 0;
1802+
}
1803+
1804+
/**
1805+
* i2c_find_adapter_by_fwnode() - find an i2c_adapter for the fwnode
1806+
* @fwnode: &struct fwnode_handle corresponding to the &struct i2c_adapter
1807+
*
1808+
* Look up and return the &struct i2c_adapter corresponding to the @fwnode.
1809+
* If no adapter can be found, or @fwnode is NULL, this returns NULL.
1810+
*
1811+
* The user must call put_device(&adapter->dev) once done with the i2c adapter.
1812+
*/
1813+
struct i2c_adapter *i2c_find_adapter_by_fwnode(struct fwnode_handle *fwnode)
1814+
{
1815+
struct i2c_adapter *adapter;
1816+
struct device *dev;
1817+
1818+
if (!fwnode)
1819+
return NULL;
1820+
1821+
dev = bus_find_device(&i2c_bus_type, NULL, fwnode,
1822+
i2c_dev_or_parent_fwnode_match);
1823+
if (!dev)
1824+
return NULL;
1825+
1826+
adapter = i2c_verify_adapter(dev);
1827+
if (!adapter)
1828+
put_device(dev);
1829+
1830+
return adapter;
1831+
}
1832+
EXPORT_SYMBOL(i2c_find_adapter_by_fwnode);
1833+
1834+
/**
1835+
* i2c_get_adapter_by_fwnode() - find an i2c_adapter for the fwnode
1836+
* @fwnode: &struct fwnode_handle corresponding to the &struct i2c_adapter
1837+
*
1838+
* Look up and return the &struct i2c_adapter corresponding to the @fwnode,
1839+
* and increment the adapter module's use count. If no adapter can be found,
1840+
* or @fwnode is NULL, this returns NULL.
1841+
*
1842+
* The user must call i2c_put_adapter(adapter) once done with the i2c adapter.
1843+
* Note that this is different from i2c_find_adapter_by_node().
1844+
*/
1845+
struct i2c_adapter *i2c_get_adapter_by_fwnode(struct fwnode_handle *fwnode)
1846+
{
1847+
struct i2c_adapter *adapter;
1848+
1849+
adapter = i2c_find_adapter_by_fwnode(fwnode);
1850+
if (!adapter)
1851+
return NULL;
1852+
1853+
if (!try_module_get(adapter->owner)) {
1854+
put_device(&adapter->dev);
1855+
adapter = NULL;
1856+
}
1857+
1858+
return adapter;
1859+
}
1860+
EXPORT_SYMBOL(i2c_get_adapter_by_fwnode);
1861+
17641862
static void i2c_parse_timing(struct device *dev, char *prop_name, u32 *cur_val_p,
17651863
u32 def_val, bool use_def)
17661864
{

drivers/i2c/i2c-core-of.c

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -113,72 +113,6 @@ void of_i2c_register_devices(struct i2c_adapter *adap)
113113
of_node_put(bus);
114114
}
115115

116-
static int of_dev_or_parent_node_match(struct device *dev, const void *data)
117-
{
118-
if (dev->of_node == data)
119-
return 1;
120-
121-
if (dev->parent)
122-
return dev->parent->of_node == data;
123-
124-
return 0;
125-
}
126-
127-
/* must call put_device() when done with returned i2c_client device */
128-
struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
129-
{
130-
struct device *dev;
131-
struct i2c_client *client;
132-
133-
dev = bus_find_device_by_of_node(&i2c_bus_type, node);
134-
if (!dev)
135-
return NULL;
136-
137-
client = i2c_verify_client(dev);
138-
if (!client)
139-
put_device(dev);
140-
141-
return client;
142-
}
143-
EXPORT_SYMBOL(of_find_i2c_device_by_node);
144-
145-
/* must call put_device() when done with returned i2c_adapter device */
146-
struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
147-
{
148-
struct device *dev;
149-
struct i2c_adapter *adapter;
150-
151-
dev = bus_find_device(&i2c_bus_type, NULL, node,
152-
of_dev_or_parent_node_match);
153-
if (!dev)
154-
return NULL;
155-
156-
adapter = i2c_verify_adapter(dev);
157-
if (!adapter)
158-
put_device(dev);
159-
160-
return adapter;
161-
}
162-
EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
163-
164-
/* must call i2c_put_adapter() when done with returned i2c_adapter device */
165-
struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
166-
{
167-
struct i2c_adapter *adapter;
168-
169-
adapter = of_find_i2c_adapter_by_node(node);
170-
if (!adapter)
171-
return NULL;
172-
173-
if (!try_module_get(adapter->owner)) {
174-
put_device(&adapter->dev);
175-
adapter = NULL;
176-
}
177-
178-
return adapter;
179-
}
180-
EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
181-
182116
static const struct of_device_id*
183117
i2c_of_match_device_sysfs(const struct of_device_id *matches,
184118
struct i2c_client *client)

include/linux/i2c.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,15 +965,33 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
965965

966966
#endif /* I2C */
967967

968+
/* must call put_device() when done with returned i2c_client device */
969+
struct i2c_client *i2c_find_device_by_fwnode(struct fwnode_handle *fwnode);
970+
971+
/* must call put_device() when done with returned i2c_adapter device */
972+
struct i2c_adapter *i2c_find_adapter_by_fwnode(struct fwnode_handle *fwnode);
973+
974+
/* must call i2c_put_adapter() when done with returned i2c_adapter device */
975+
struct i2c_adapter *i2c_get_adapter_by_fwnode(struct fwnode_handle *fwnode);
976+
968977
#if IS_ENABLED(CONFIG_OF)
969978
/* must call put_device() when done with returned i2c_client device */
970-
struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);
979+
static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
980+
{
981+
return i2c_find_device_by_fwnode(of_fwnode_handle(node));
982+
}
971983

972984
/* must call put_device() when done with returned i2c_adapter device */
973-
struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node);
985+
static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
986+
{
987+
return i2c_find_adapter_by_fwnode(of_fwnode_handle(node));
988+
}
974989

975990
/* must call i2c_put_adapter() when done with returned i2c_adapter device */
976-
struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node);
991+
static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
992+
{
993+
return i2c_get_adapter_by_fwnode(of_fwnode_handle(node));
994+
}
977995

978996
const struct of_device_id
979997
*i2c_of_match_device(const struct of_device_id *matches,

0 commit comments

Comments
 (0)