Skip to content

Commit ef1e9b6

Browse files
Russell King (Oracle)gregkh
authored andcommitted
i2c: add fwnode APIs
[ Upstream commit 373c612 ] 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 <[email protected]> Signed-off-by: Russell King (Oracle) <[email protected]> Signed-off-by: Wolfram Sang <[email protected]> Stable-dep-of: 3f858bb ("i2c: acpi: Unbind mux adapters before delete") Signed-off-by: Sasha Levin <[email protected]>
1 parent c0cd2d8 commit ef1e9b6

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
@@ -1017,6 +1017,35 @@ void i2c_unregister_device(struct i2c_client *client)
10171017
}
10181018
EXPORT_SYMBOL_GPL(i2c_unregister_device);
10191019

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

10211050
static const struct i2c_device_id dummy_id[] = {
10221051
{ "dummy", 0 },
@@ -1767,6 +1796,75 @@ int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter)
17671796
}
17681797
EXPORT_SYMBOL_GPL(devm_i2c_add_adapter);
17691798

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

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
@@ -964,15 +964,33 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
964964

965965
#endif /* I2C */
966966

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

971983
/* must call put_device() when done with returned i2c_adapter device */
972-
struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node);
984+
static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
985+
{
986+
return i2c_find_adapter_by_fwnode(of_fwnode_handle(node));
987+
}
973988

974989
/* must call i2c_put_adapter() when done with returned i2c_adapter device */
975-
struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node);
990+
static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
991+
{
992+
return i2c_get_adapter_by_fwnode(of_fwnode_handle(node));
993+
}
976994

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

0 commit comments

Comments
 (0)