15bf4fa7dSWolfram Sang /* 25bf4fa7dSWolfram Sang * Linux I2C core OF support code 35bf4fa7dSWolfram Sang * 45bf4fa7dSWolfram Sang * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de> 55bf4fa7dSWolfram Sang * based on a previous patch from Jon Smirl <jonsmirl@gmail.com> 65bf4fa7dSWolfram Sang * 7*a1671af2SWolfram Sang * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de> 85bf4fa7dSWolfram Sang * 95bf4fa7dSWolfram Sang * This program is free software; you can redistribute it and/or modify it 105bf4fa7dSWolfram Sang * under the terms of the GNU General Public License as published by the Free 115bf4fa7dSWolfram Sang * Software Foundation; either version 2 of the License, or (at your option) 125bf4fa7dSWolfram Sang * any later version. 135bf4fa7dSWolfram Sang */ 145bf4fa7dSWolfram Sang 155bf4fa7dSWolfram Sang #include <dt-bindings/i2c/i2c.h> 165bf4fa7dSWolfram Sang #include <linux/device.h> 175bf4fa7dSWolfram Sang #include <linux/err.h> 185bf4fa7dSWolfram Sang #include <linux/i2c.h> 195bf4fa7dSWolfram Sang #include <linux/module.h> 205bf4fa7dSWolfram Sang #include <linux/of.h> 215bf4fa7dSWolfram Sang #include <linux/of_device.h> 225bf4fa7dSWolfram Sang 235bf4fa7dSWolfram Sang #include "i2c-core.h" 245bf4fa7dSWolfram Sang 255bf4fa7dSWolfram Sang static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, 265bf4fa7dSWolfram Sang struct device_node *node) 275bf4fa7dSWolfram Sang { 28c49b0e07SWolfram Sang struct i2c_client *client; 295bf4fa7dSWolfram Sang struct i2c_board_info info = {}; 305bf4fa7dSWolfram Sang struct dev_archdata dev_ad = {}; 315bf4fa7dSWolfram Sang u32 addr; 32*a1671af2SWolfram Sang int ret; 335bf4fa7dSWolfram Sang 34453a237cSRob Herring dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node); 355bf4fa7dSWolfram Sang 365bf4fa7dSWolfram Sang if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) { 37453a237cSRob Herring dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n", 38453a237cSRob Herring node); 395bf4fa7dSWolfram Sang return ERR_PTR(-EINVAL); 405bf4fa7dSWolfram Sang } 415bf4fa7dSWolfram Sang 42*a1671af2SWolfram Sang ret = of_property_read_u32(node, "reg", &addr); 43*a1671af2SWolfram Sang if (ret) { 44453a237cSRob Herring dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node); 45*a1671af2SWolfram Sang return ERR_PTR(ret); 465bf4fa7dSWolfram Sang } 475bf4fa7dSWolfram Sang 485bf4fa7dSWolfram Sang if (addr & I2C_TEN_BIT_ADDRESS) { 495bf4fa7dSWolfram Sang addr &= ~I2C_TEN_BIT_ADDRESS; 505bf4fa7dSWolfram Sang info.flags |= I2C_CLIENT_TEN; 515bf4fa7dSWolfram Sang } 525bf4fa7dSWolfram Sang 535bf4fa7dSWolfram Sang if (addr & I2C_OWN_SLAVE_ADDRESS) { 545bf4fa7dSWolfram Sang addr &= ~I2C_OWN_SLAVE_ADDRESS; 555bf4fa7dSWolfram Sang info.flags |= I2C_CLIENT_SLAVE; 565bf4fa7dSWolfram Sang } 575bf4fa7dSWolfram Sang 585bf4fa7dSWolfram Sang info.addr = addr; 595bf4fa7dSWolfram Sang info.archdata = &dev_ad; 60e6db2d32SWolfram Sang info.of_node = of_node_get(node); 615bf4fa7dSWolfram Sang 625bf4fa7dSWolfram Sang if (of_property_read_bool(node, "host-notify")) 635bf4fa7dSWolfram Sang info.flags |= I2C_CLIENT_HOST_NOTIFY; 645bf4fa7dSWolfram Sang 655bf4fa7dSWolfram Sang if (of_get_property(node, "wakeup-source", NULL)) 665bf4fa7dSWolfram Sang info.flags |= I2C_CLIENT_WAKE; 675bf4fa7dSWolfram Sang 68c49b0e07SWolfram Sang client = i2c_new_device(adap, &info); 69c49b0e07SWolfram Sang if (!client) { 70453a237cSRob Herring dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node); 715bf4fa7dSWolfram Sang of_node_put(node); 725bf4fa7dSWolfram Sang return ERR_PTR(-EINVAL); 735bf4fa7dSWolfram Sang } 74c49b0e07SWolfram Sang return client; 755bf4fa7dSWolfram Sang } 765bf4fa7dSWolfram Sang 775bf4fa7dSWolfram Sang void of_i2c_register_devices(struct i2c_adapter *adap) 785bf4fa7dSWolfram Sang { 795bf4fa7dSWolfram Sang struct device_node *bus, *node; 805bf4fa7dSWolfram Sang struct i2c_client *client; 815bf4fa7dSWolfram Sang 825bf4fa7dSWolfram Sang /* Only register child devices if the adapter has a node pointer set */ 835bf4fa7dSWolfram Sang if (!adap->dev.of_node) 845bf4fa7dSWolfram Sang return; 855bf4fa7dSWolfram Sang 865bf4fa7dSWolfram Sang dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); 875bf4fa7dSWolfram Sang 885bf4fa7dSWolfram Sang bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus"); 895bf4fa7dSWolfram Sang if (!bus) 905bf4fa7dSWolfram Sang bus = of_node_get(adap->dev.of_node); 915bf4fa7dSWolfram Sang 925bf4fa7dSWolfram Sang for_each_available_child_of_node(bus, node) { 935bf4fa7dSWolfram Sang if (of_node_test_and_set_flag(node, OF_POPULATED)) 945bf4fa7dSWolfram Sang continue; 955bf4fa7dSWolfram Sang 965bf4fa7dSWolfram Sang client = of_i2c_register_device(adap, node); 975bf4fa7dSWolfram Sang if (IS_ERR(client)) { 98f1c87cebSWolfram Sang dev_err(&adap->dev, 99453a237cSRob Herring "Failed to create I2C device for %pOF\n", 100453a237cSRob Herring node); 1015bf4fa7dSWolfram Sang of_node_clear_flag(node, OF_POPULATED); 1025bf4fa7dSWolfram Sang } 1035bf4fa7dSWolfram Sang } 1045bf4fa7dSWolfram Sang 1055bf4fa7dSWolfram Sang of_node_put(bus); 1065bf4fa7dSWolfram Sang } 1075bf4fa7dSWolfram Sang 1085bf4fa7dSWolfram Sang static int of_dev_node_match(struct device *dev, void *data) 1095bf4fa7dSWolfram Sang { 1105bf4fa7dSWolfram Sang return dev->of_node == data; 1115bf4fa7dSWolfram Sang } 1125bf4fa7dSWolfram Sang 1135bf4fa7dSWolfram Sang /* must call put_device() when done with returned i2c_client device */ 1145bf4fa7dSWolfram Sang struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) 1155bf4fa7dSWolfram Sang { 1165bf4fa7dSWolfram Sang struct device *dev; 1175bf4fa7dSWolfram Sang struct i2c_client *client; 1185bf4fa7dSWolfram Sang 1195bf4fa7dSWolfram Sang dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match); 1205bf4fa7dSWolfram Sang if (!dev) 1215bf4fa7dSWolfram Sang return NULL; 1225bf4fa7dSWolfram Sang 1235bf4fa7dSWolfram Sang client = i2c_verify_client(dev); 1245bf4fa7dSWolfram Sang if (!client) 1255bf4fa7dSWolfram Sang put_device(dev); 1265bf4fa7dSWolfram Sang 1275bf4fa7dSWolfram Sang return client; 1285bf4fa7dSWolfram Sang } 1295bf4fa7dSWolfram Sang EXPORT_SYMBOL(of_find_i2c_device_by_node); 1305bf4fa7dSWolfram Sang 1315bf4fa7dSWolfram Sang /* must call put_device() when done with returned i2c_adapter device */ 1325bf4fa7dSWolfram Sang struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node) 1335bf4fa7dSWolfram Sang { 1345bf4fa7dSWolfram Sang struct device *dev; 1355bf4fa7dSWolfram Sang struct i2c_adapter *adapter; 1365bf4fa7dSWolfram Sang 1375bf4fa7dSWolfram Sang dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match); 1385bf4fa7dSWolfram Sang if (!dev) 1395bf4fa7dSWolfram Sang return NULL; 1405bf4fa7dSWolfram Sang 1415bf4fa7dSWolfram Sang adapter = i2c_verify_adapter(dev); 1425bf4fa7dSWolfram Sang if (!adapter) 1435bf4fa7dSWolfram Sang put_device(dev); 1445bf4fa7dSWolfram Sang 1455bf4fa7dSWolfram Sang return adapter; 1465bf4fa7dSWolfram Sang } 1475bf4fa7dSWolfram Sang EXPORT_SYMBOL(of_find_i2c_adapter_by_node); 1485bf4fa7dSWolfram Sang 1495bf4fa7dSWolfram Sang /* must call i2c_put_adapter() when done with returned i2c_adapter device */ 1505bf4fa7dSWolfram Sang struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node) 1515bf4fa7dSWolfram Sang { 1525bf4fa7dSWolfram Sang struct i2c_adapter *adapter; 1535bf4fa7dSWolfram Sang 1545bf4fa7dSWolfram Sang adapter = of_find_i2c_adapter_by_node(node); 1555bf4fa7dSWolfram Sang if (!adapter) 1565bf4fa7dSWolfram Sang return NULL; 1575bf4fa7dSWolfram Sang 1585bf4fa7dSWolfram Sang if (!try_module_get(adapter->owner)) { 1595bf4fa7dSWolfram Sang put_device(&adapter->dev); 1605bf4fa7dSWolfram Sang adapter = NULL; 1615bf4fa7dSWolfram Sang } 1625bf4fa7dSWolfram Sang 1635bf4fa7dSWolfram Sang return adapter; 1645bf4fa7dSWolfram Sang } 1655bf4fa7dSWolfram Sang EXPORT_SYMBOL(of_get_i2c_adapter_by_node); 1665bf4fa7dSWolfram Sang 1675bf4fa7dSWolfram Sang static const struct of_device_id* 1685bf4fa7dSWolfram Sang i2c_of_match_device_sysfs(const struct of_device_id *matches, 1695bf4fa7dSWolfram Sang struct i2c_client *client) 1705bf4fa7dSWolfram Sang { 1715bf4fa7dSWolfram Sang const char *name; 1725bf4fa7dSWolfram Sang 1735bf4fa7dSWolfram Sang for (; matches->compatible[0]; matches++) { 1745bf4fa7dSWolfram Sang /* 1755bf4fa7dSWolfram Sang * Adding devices through the i2c sysfs interface provides us 1765bf4fa7dSWolfram Sang * a string to match which may be compatible with the device 1775bf4fa7dSWolfram Sang * tree compatible strings, however with no actual of_node the 1785bf4fa7dSWolfram Sang * of_match_device() will not match 1795bf4fa7dSWolfram Sang */ 1805bf4fa7dSWolfram Sang if (sysfs_streq(client->name, matches->compatible)) 1815bf4fa7dSWolfram Sang return matches; 1825bf4fa7dSWolfram Sang 1835bf4fa7dSWolfram Sang name = strchr(matches->compatible, ','); 1845bf4fa7dSWolfram Sang if (!name) 1855bf4fa7dSWolfram Sang name = matches->compatible; 1865bf4fa7dSWolfram Sang else 1875bf4fa7dSWolfram Sang name++; 1885bf4fa7dSWolfram Sang 1895bf4fa7dSWolfram Sang if (sysfs_streq(client->name, name)) 1905bf4fa7dSWolfram Sang return matches; 1915bf4fa7dSWolfram Sang } 1925bf4fa7dSWolfram Sang 1935bf4fa7dSWolfram Sang return NULL; 1945bf4fa7dSWolfram Sang } 1955bf4fa7dSWolfram Sang 1965bf4fa7dSWolfram Sang const struct of_device_id 1975bf4fa7dSWolfram Sang *i2c_of_match_device(const struct of_device_id *matches, 1985bf4fa7dSWolfram Sang struct i2c_client *client) 1995bf4fa7dSWolfram Sang { 2005bf4fa7dSWolfram Sang const struct of_device_id *match; 2015bf4fa7dSWolfram Sang 2025bf4fa7dSWolfram Sang if (!(client && matches)) 2035bf4fa7dSWolfram Sang return NULL; 2045bf4fa7dSWolfram Sang 2055bf4fa7dSWolfram Sang match = of_match_device(matches, &client->dev); 2065bf4fa7dSWolfram Sang if (match) 2075bf4fa7dSWolfram Sang return match; 2085bf4fa7dSWolfram Sang 2095bf4fa7dSWolfram Sang return i2c_of_match_device_sysfs(matches, client); 2105bf4fa7dSWolfram Sang } 2115bf4fa7dSWolfram Sang EXPORT_SYMBOL_GPL(i2c_of_match_device); 2125bf4fa7dSWolfram Sang 2135bf4fa7dSWolfram Sang #if IS_ENABLED(CONFIG_OF_DYNAMIC) 2145bf4fa7dSWolfram Sang static int of_i2c_notify(struct notifier_block *nb, unsigned long action, 2155bf4fa7dSWolfram Sang void *arg) 2165bf4fa7dSWolfram Sang { 2175bf4fa7dSWolfram Sang struct of_reconfig_data *rd = arg; 2185bf4fa7dSWolfram Sang struct i2c_adapter *adap; 2195bf4fa7dSWolfram Sang struct i2c_client *client; 2205bf4fa7dSWolfram Sang 2215bf4fa7dSWolfram Sang switch (of_reconfig_get_state_change(action, rd)) { 2225bf4fa7dSWolfram Sang case OF_RECONFIG_CHANGE_ADD: 2235bf4fa7dSWolfram Sang adap = of_find_i2c_adapter_by_node(rd->dn->parent); 2245bf4fa7dSWolfram Sang if (adap == NULL) 2255bf4fa7dSWolfram Sang return NOTIFY_OK; /* not for us */ 2265bf4fa7dSWolfram Sang 2275bf4fa7dSWolfram Sang if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 2285bf4fa7dSWolfram Sang put_device(&adap->dev); 2295bf4fa7dSWolfram Sang return NOTIFY_OK; 2305bf4fa7dSWolfram Sang } 2315bf4fa7dSWolfram Sang 2325bf4fa7dSWolfram Sang client = of_i2c_register_device(adap, rd->dn); 2335bf4fa7dSWolfram Sang put_device(&adap->dev); 2345bf4fa7dSWolfram Sang 2355bf4fa7dSWolfram Sang if (IS_ERR(client)) { 236453a237cSRob Herring dev_err(&adap->dev, "failed to create client for '%pOF'\n", 237453a237cSRob Herring rd->dn); 2385bf4fa7dSWolfram Sang of_node_clear_flag(rd->dn, OF_POPULATED); 2395bf4fa7dSWolfram Sang return notifier_from_errno(PTR_ERR(client)); 2405bf4fa7dSWolfram Sang } 2415bf4fa7dSWolfram Sang break; 2425bf4fa7dSWolfram Sang case OF_RECONFIG_CHANGE_REMOVE: 2435bf4fa7dSWolfram Sang /* already depopulated? */ 2445bf4fa7dSWolfram Sang if (!of_node_check_flag(rd->dn, OF_POPULATED)) 2455bf4fa7dSWolfram Sang return NOTIFY_OK; 2465bf4fa7dSWolfram Sang 2475bf4fa7dSWolfram Sang /* find our device by node */ 2485bf4fa7dSWolfram Sang client = of_find_i2c_device_by_node(rd->dn); 2495bf4fa7dSWolfram Sang if (client == NULL) 2505bf4fa7dSWolfram Sang return NOTIFY_OK; /* no? not meant for us */ 2515bf4fa7dSWolfram Sang 2525bf4fa7dSWolfram Sang /* unregister takes one ref away */ 2535bf4fa7dSWolfram Sang i2c_unregister_device(client); 2545bf4fa7dSWolfram Sang 2555bf4fa7dSWolfram Sang /* and put the reference of the find */ 2565bf4fa7dSWolfram Sang put_device(&client->dev); 2575bf4fa7dSWolfram Sang break; 2585bf4fa7dSWolfram Sang } 2595bf4fa7dSWolfram Sang 2605bf4fa7dSWolfram Sang return NOTIFY_OK; 2615bf4fa7dSWolfram Sang } 2625bf4fa7dSWolfram Sang 2635bf4fa7dSWolfram Sang struct notifier_block i2c_of_notifier = { 2645bf4fa7dSWolfram Sang .notifier_call = of_i2c_notify, 2655bf4fa7dSWolfram Sang }; 2665bf4fa7dSWolfram Sang #endif /* CONFIG_OF_DYNAMIC */ 267