Skip to content

Commit cabcf4f

Browse files
Lee JonesWolfram Sang
authored andcommitted
i2c: Add the ability to match device to compatible string without an of_node
A great deal of I2C devices are currently matched via DT node name, and as such the compatible naming convention of '<vendor>,<device>' has gone somewhat awry - some nodes don't supply one, some supply an arbitrary string and others the correct device name with an arbitrary vendor prefix. In an effort to correct this problem we have to supply a mechanism to match a device by compatible string AND by simple device name. This function strips off the '<vendor>,' part of a supplied compatible string and attempts to match without it. It is also used for sysfs, where a user can choose to instantiate a device on an i2c bus using the sysfs interface by providing a string and address to match and communicate with the device on the bus. Presently this string is only matched against the old i2c device id style strings, even in the presence of full device tree compatible strings with vendor prefixes. Providing a vendor-prefixed string to the sysfs interface will not match against the device tree of_match_device() calls as there is no device tree node to parse from the sysfs interface. This function can match both vendor prefixed and stripped compatible strings on the sysfs interface. Acked-by: Grant Likely <[email protected]> Signed-off-by: Lee Jones <[email protected]> Reviewed-by: Javier Martinez Canillas <[email protected]> Tested-by: Javier Martinez Canillas <[email protected]> Signed-off-by: Kieran Bingham <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 811073b commit cabcf4f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

drivers/i2c/i2c-core.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,36 @@ struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
17691769
return adapter;
17701770
}
17711771
EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
1772+
1773+
static const struct of_device_id*
1774+
i2c_of_match_device_sysfs(const struct of_device_id *matches,
1775+
struct i2c_client *client)
1776+
{
1777+
const char *name;
1778+
1779+
for (; matches->compatible[0]; matches++) {
1780+
/*
1781+
* Adding devices through the i2c sysfs interface provides us
1782+
* a string to match which may be compatible with the device
1783+
* tree compatible strings, however with no actual of_node the
1784+
* of_match_device() will not match
1785+
*/
1786+
if (sysfs_streq(client->name, matches->compatible))
1787+
return matches;
1788+
1789+
name = strchr(matches->compatible, ',');
1790+
if (!name)
1791+
name = matches->compatible;
1792+
else
1793+
name++;
1794+
1795+
if (sysfs_streq(client->name, name))
1796+
return matches;
1797+
}
1798+
1799+
return NULL;
1800+
}
1801+
17721802
#else
17731803
static void of_i2c_register_devices(struct i2c_adapter *adap) { }
17741804
#endif /* CONFIG_OF */

0 commit comments

Comments
 (0)