12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 25bf4fa7dSWolfram Sang /* 35bf4fa7dSWolfram Sang * Linux I2C core OF support code 45bf4fa7dSWolfram Sang * 55bf4fa7dSWolfram Sang * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de> 65bf4fa7dSWolfram Sang * based on a previous patch from Jon Smirl <jonsmirl@gmail.com> 75bf4fa7dSWolfram Sang * 82f5a55c5SWolfram Sang * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org> 95bf4fa7dSWolfram Sang */ 105bf4fa7dSWolfram Sang 115bf4fa7dSWolfram Sang #include <dt-bindings/i2c/i2c.h> 125bf4fa7dSWolfram Sang #include <linux/device.h> 135bf4fa7dSWolfram Sang #include <linux/err.h> 145bf4fa7dSWolfram Sang #include <linux/i2c.h> 155bf4fa7dSWolfram Sang #include <linux/module.h> 165bf4fa7dSWolfram Sang #include <linux/of.h> 175bf4fa7dSWolfram Sang #include <linux/of_device.h> 18a8023e66SWolfram Sang #include <linux/sysfs.h> 195bf4fa7dSWolfram Sang 205bf4fa7dSWolfram Sang #include "i2c-core.h" 215bf4fa7dSWolfram Sang 22da0086d0SBoris Brezillon int of_i2c_get_board_info(struct device *dev, struct device_node *node, 23da0086d0SBoris Brezillon struct i2c_board_info *info) 245bf4fa7dSWolfram Sang { 255bf4fa7dSWolfram Sang u32 addr; 26a1671af2SWolfram Sang int ret; 275bf4fa7dSWolfram Sang 28da0086d0SBoris Brezillon memset(info, 0, sizeof(*info)); 295bf4fa7dSWolfram Sang 30*673aa1edSMiquel Raynal if (of_alias_from_compatible(node, info->type, sizeof(info->type)) < 0) { 31da0086d0SBoris Brezillon dev_err(dev, "of_i2c: modalias failure on %pOF\n", node); 32da0086d0SBoris Brezillon return -EINVAL; 335bf4fa7dSWolfram Sang } 345bf4fa7dSWolfram Sang 35a1671af2SWolfram Sang ret = of_property_read_u32(node, "reg", &addr); 36a1671af2SWolfram Sang if (ret) { 37da0086d0SBoris Brezillon dev_err(dev, "of_i2c: invalid reg on %pOF\n", node); 38da0086d0SBoris Brezillon return ret; 395bf4fa7dSWolfram Sang } 405bf4fa7dSWolfram Sang 415bf4fa7dSWolfram Sang if (addr & I2C_TEN_BIT_ADDRESS) { 425bf4fa7dSWolfram Sang addr &= ~I2C_TEN_BIT_ADDRESS; 43da0086d0SBoris Brezillon info->flags |= I2C_CLIENT_TEN; 445bf4fa7dSWolfram Sang } 455bf4fa7dSWolfram Sang 465bf4fa7dSWolfram Sang if (addr & I2C_OWN_SLAVE_ADDRESS) { 475bf4fa7dSWolfram Sang addr &= ~I2C_OWN_SLAVE_ADDRESS; 48da0086d0SBoris Brezillon info->flags |= I2C_CLIENT_SLAVE; 495bf4fa7dSWolfram Sang } 505bf4fa7dSWolfram Sang 51da0086d0SBoris Brezillon info->addr = addr; 52da0086d0SBoris Brezillon info->of_node = node; 5360774d2aSSaravana Kannan info->fwnode = of_fwnode_handle(node); 545bf4fa7dSWolfram Sang 555bf4fa7dSWolfram Sang if (of_property_read_bool(node, "host-notify")) 56da0086d0SBoris Brezillon info->flags |= I2C_CLIENT_HOST_NOTIFY; 575bf4fa7dSWolfram Sang 585bf4fa7dSWolfram Sang if (of_get_property(node, "wakeup-source", NULL)) 59da0086d0SBoris Brezillon info->flags |= I2C_CLIENT_WAKE; 60da0086d0SBoris Brezillon 61da0086d0SBoris Brezillon return 0; 62da0086d0SBoris Brezillon } 63da0086d0SBoris Brezillon EXPORT_SYMBOL_GPL(of_i2c_get_board_info); 64da0086d0SBoris Brezillon 65da0086d0SBoris Brezillon static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, 66da0086d0SBoris Brezillon struct device_node *node) 67da0086d0SBoris Brezillon { 68da0086d0SBoris Brezillon struct i2c_client *client; 69da0086d0SBoris Brezillon struct i2c_board_info info; 70da0086d0SBoris Brezillon int ret; 71da0086d0SBoris Brezillon 72da0086d0SBoris Brezillon dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node); 73da0086d0SBoris Brezillon 74da0086d0SBoris Brezillon ret = of_i2c_get_board_info(&adap->dev, node, &info); 75da0086d0SBoris Brezillon if (ret) 76da0086d0SBoris Brezillon return ERR_PTR(ret); 775bf4fa7dSWolfram Sang 785f0155b4SWolfram Sang client = i2c_new_client_device(adap, &info); 795f0155b4SWolfram Sang if (IS_ERR(client)) 80453a237cSRob Herring dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node); 815f0155b4SWolfram Sang 82c49b0e07SWolfram Sang return client; 835bf4fa7dSWolfram Sang } 845bf4fa7dSWolfram Sang 855bf4fa7dSWolfram Sang void of_i2c_register_devices(struct i2c_adapter *adap) 865bf4fa7dSWolfram Sang { 875bf4fa7dSWolfram Sang struct device_node *bus, *node; 885bf4fa7dSWolfram Sang struct i2c_client *client; 895bf4fa7dSWolfram Sang 905bf4fa7dSWolfram Sang /* Only register child devices if the adapter has a node pointer set */ 915bf4fa7dSWolfram Sang if (!adap->dev.of_node) 925bf4fa7dSWolfram Sang return; 935bf4fa7dSWolfram Sang 945bf4fa7dSWolfram Sang dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); 955bf4fa7dSWolfram Sang 965bf4fa7dSWolfram Sang bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus"); 975bf4fa7dSWolfram Sang if (!bus) 985bf4fa7dSWolfram Sang bus = of_node_get(adap->dev.of_node); 995bf4fa7dSWolfram Sang 1005bf4fa7dSWolfram Sang for_each_available_child_of_node(bus, node) { 1015bf4fa7dSWolfram Sang if (of_node_test_and_set_flag(node, OF_POPULATED)) 1025bf4fa7dSWolfram Sang continue; 1035bf4fa7dSWolfram Sang 1045bf4fa7dSWolfram Sang client = of_i2c_register_device(adap, node); 1055bf4fa7dSWolfram Sang if (IS_ERR(client)) { 106f1c87cebSWolfram Sang dev_err(&adap->dev, 107453a237cSRob Herring "Failed to create I2C device for %pOF\n", 108453a237cSRob Herring node); 1095bf4fa7dSWolfram Sang of_node_clear_flag(node, OF_POPULATED); 1105bf4fa7dSWolfram Sang } 1115bf4fa7dSWolfram Sang } 1125bf4fa7dSWolfram Sang 1135bf4fa7dSWolfram Sang of_node_put(bus); 1145bf4fa7dSWolfram Sang } 1155bf4fa7dSWolfram Sang 1165bf4fa7dSWolfram Sang static const struct of_device_id* 1175bf4fa7dSWolfram Sang i2c_of_match_device_sysfs(const struct of_device_id *matches, 1185bf4fa7dSWolfram Sang struct i2c_client *client) 1195bf4fa7dSWolfram Sang { 1205bf4fa7dSWolfram Sang const char *name; 1215bf4fa7dSWolfram Sang 1225bf4fa7dSWolfram Sang for (; matches->compatible[0]; matches++) { 1235bf4fa7dSWolfram Sang /* 1245bf4fa7dSWolfram Sang * Adding devices through the i2c sysfs interface provides us 1255bf4fa7dSWolfram Sang * a string to match which may be compatible with the device 1265bf4fa7dSWolfram Sang * tree compatible strings, however with no actual of_node the 1275bf4fa7dSWolfram Sang * of_match_device() will not match 1285bf4fa7dSWolfram Sang */ 1295bf4fa7dSWolfram Sang if (sysfs_streq(client->name, matches->compatible)) 1305bf4fa7dSWolfram Sang return matches; 1315bf4fa7dSWolfram Sang 1325bf4fa7dSWolfram Sang name = strchr(matches->compatible, ','); 1335bf4fa7dSWolfram Sang if (!name) 1345bf4fa7dSWolfram Sang name = matches->compatible; 1355bf4fa7dSWolfram Sang else 1365bf4fa7dSWolfram Sang name++; 1375bf4fa7dSWolfram Sang 1385bf4fa7dSWolfram Sang if (sysfs_streq(client->name, name)) 1395bf4fa7dSWolfram Sang return matches; 1405bf4fa7dSWolfram Sang } 1415bf4fa7dSWolfram Sang 1425bf4fa7dSWolfram Sang return NULL; 1435bf4fa7dSWolfram Sang } 1445bf4fa7dSWolfram Sang 1455bf4fa7dSWolfram Sang const struct of_device_id 1465bf4fa7dSWolfram Sang *i2c_of_match_device(const struct of_device_id *matches, 1475bf4fa7dSWolfram Sang struct i2c_client *client) 1485bf4fa7dSWolfram Sang { 1495bf4fa7dSWolfram Sang const struct of_device_id *match; 1505bf4fa7dSWolfram Sang 1515bf4fa7dSWolfram Sang if (!(client && matches)) 1525bf4fa7dSWolfram Sang return NULL; 1535bf4fa7dSWolfram Sang 1545bf4fa7dSWolfram Sang match = of_match_device(matches, &client->dev); 1555bf4fa7dSWolfram Sang if (match) 1565bf4fa7dSWolfram Sang return match; 1575bf4fa7dSWolfram Sang 1585bf4fa7dSWolfram Sang return i2c_of_match_device_sysfs(matches, client); 1595bf4fa7dSWolfram Sang } 1605bf4fa7dSWolfram Sang EXPORT_SYMBOL_GPL(i2c_of_match_device); 1615bf4fa7dSWolfram Sang 1625bf4fa7dSWolfram Sang #if IS_ENABLED(CONFIG_OF_DYNAMIC) 1635bf4fa7dSWolfram Sang static int of_i2c_notify(struct notifier_block *nb, unsigned long action, 1645bf4fa7dSWolfram Sang void *arg) 1655bf4fa7dSWolfram Sang { 1665bf4fa7dSWolfram Sang struct of_reconfig_data *rd = arg; 1675bf4fa7dSWolfram Sang struct i2c_adapter *adap; 1685bf4fa7dSWolfram Sang struct i2c_client *client; 1695bf4fa7dSWolfram Sang 1705bf4fa7dSWolfram Sang switch (of_reconfig_get_state_change(action, rd)) { 1715bf4fa7dSWolfram Sang case OF_RECONFIG_CHANGE_ADD: 1725bf4fa7dSWolfram Sang adap = of_find_i2c_adapter_by_node(rd->dn->parent); 1735bf4fa7dSWolfram Sang if (adap == NULL) 1745bf4fa7dSWolfram Sang return NOTIFY_OK; /* not for us */ 1755bf4fa7dSWolfram Sang 1765bf4fa7dSWolfram Sang if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 1775bf4fa7dSWolfram Sang put_device(&adap->dev); 1785bf4fa7dSWolfram Sang return NOTIFY_OK; 1795bf4fa7dSWolfram Sang } 1805bf4fa7dSWolfram Sang 1815bf4fa7dSWolfram Sang client = of_i2c_register_device(adap, rd->dn); 1825bf4fa7dSWolfram Sang if (IS_ERR(client)) { 183453a237cSRob Herring dev_err(&adap->dev, "failed to create client for '%pOF'\n", 184453a237cSRob Herring rd->dn); 185a4c2fec1SWen Yang put_device(&adap->dev); 1865bf4fa7dSWolfram Sang of_node_clear_flag(rd->dn, OF_POPULATED); 1875bf4fa7dSWolfram Sang return notifier_from_errno(PTR_ERR(client)); 1885bf4fa7dSWolfram Sang } 189a4c2fec1SWen Yang put_device(&adap->dev); 1905bf4fa7dSWolfram Sang break; 1915bf4fa7dSWolfram Sang case OF_RECONFIG_CHANGE_REMOVE: 1925bf4fa7dSWolfram Sang /* already depopulated? */ 1935bf4fa7dSWolfram Sang if (!of_node_check_flag(rd->dn, OF_POPULATED)) 1945bf4fa7dSWolfram Sang return NOTIFY_OK; 1955bf4fa7dSWolfram Sang 1965bf4fa7dSWolfram Sang /* find our device by node */ 1975bf4fa7dSWolfram Sang client = of_find_i2c_device_by_node(rd->dn); 1985bf4fa7dSWolfram Sang if (client == NULL) 1995bf4fa7dSWolfram Sang return NOTIFY_OK; /* no? not meant for us */ 2005bf4fa7dSWolfram Sang 2015bf4fa7dSWolfram Sang /* unregister takes one ref away */ 2025bf4fa7dSWolfram Sang i2c_unregister_device(client); 2035bf4fa7dSWolfram Sang 2045bf4fa7dSWolfram Sang /* and put the reference of the find */ 2055bf4fa7dSWolfram Sang put_device(&client->dev); 2065bf4fa7dSWolfram Sang break; 2075bf4fa7dSWolfram Sang } 2085bf4fa7dSWolfram Sang 2095bf4fa7dSWolfram Sang return NOTIFY_OK; 2105bf4fa7dSWolfram Sang } 2115bf4fa7dSWolfram Sang 2125bf4fa7dSWolfram Sang struct notifier_block i2c_of_notifier = { 2135bf4fa7dSWolfram Sang .notifier_call = of_i2c_notify, 2145bf4fa7dSWolfram Sang }; 2155bf4fa7dSWolfram Sang #endif /* CONFIG_OF_DYNAMIC */ 216