Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Linux I2C core OF support code
0004  *
0005  * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
0006  * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
0007  *
0008  * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org>
0009  */
0010 
0011 #include <dt-bindings/i2c/i2c.h>
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/i2c.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/sysfs.h>
0019 
0020 #include "i2c-core.h"
0021 
0022 int of_i2c_get_board_info(struct device *dev, struct device_node *node,
0023               struct i2c_board_info *info)
0024 {
0025     u32 addr;
0026     int ret;
0027 
0028     memset(info, 0, sizeof(*info));
0029 
0030     if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
0031         dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
0032         return -EINVAL;
0033     }
0034 
0035     ret = of_property_read_u32(node, "reg", &addr);
0036     if (ret) {
0037         dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
0038         return ret;
0039     }
0040 
0041     if (addr & I2C_TEN_BIT_ADDRESS) {
0042         addr &= ~I2C_TEN_BIT_ADDRESS;
0043         info->flags |= I2C_CLIENT_TEN;
0044     }
0045 
0046     if (addr & I2C_OWN_SLAVE_ADDRESS) {
0047         addr &= ~I2C_OWN_SLAVE_ADDRESS;
0048         info->flags |= I2C_CLIENT_SLAVE;
0049     }
0050 
0051     info->addr = addr;
0052     info->of_node = node;
0053     info->fwnode = of_fwnode_handle(node);
0054 
0055     if (of_property_read_bool(node, "host-notify"))
0056         info->flags |= I2C_CLIENT_HOST_NOTIFY;
0057 
0058     if (of_get_property(node, "wakeup-source", NULL))
0059         info->flags |= I2C_CLIENT_WAKE;
0060 
0061     return 0;
0062 }
0063 EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
0064 
0065 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
0066                          struct device_node *node)
0067 {
0068     struct i2c_client *client;
0069     struct i2c_board_info info;
0070     int ret;
0071 
0072     dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
0073 
0074     ret = of_i2c_get_board_info(&adap->dev, node, &info);
0075     if (ret)
0076         return ERR_PTR(ret);
0077 
0078     client = i2c_new_client_device(adap, &info);
0079     if (IS_ERR(client))
0080         dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
0081 
0082     return client;
0083 }
0084 
0085 void of_i2c_register_devices(struct i2c_adapter *adap)
0086 {
0087     struct device_node *bus, *node;
0088     struct i2c_client *client;
0089 
0090     /* Only register child devices if the adapter has a node pointer set */
0091     if (!adap->dev.of_node)
0092         return;
0093 
0094     dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
0095 
0096     bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
0097     if (!bus)
0098         bus = of_node_get(adap->dev.of_node);
0099 
0100     for_each_available_child_of_node(bus, node) {
0101         if (of_node_test_and_set_flag(node, OF_POPULATED))
0102             continue;
0103 
0104         client = of_i2c_register_device(adap, node);
0105         if (IS_ERR(client)) {
0106             dev_err(&adap->dev,
0107                  "Failed to create I2C device for %pOF\n",
0108                  node);
0109             of_node_clear_flag(node, OF_POPULATED);
0110         }
0111     }
0112 
0113     of_node_put(bus);
0114 }
0115 
0116 static int of_dev_or_parent_node_match(struct device *dev, const void *data)
0117 {
0118     if (dev->of_node == data)
0119         return 1;
0120 
0121     if (dev->parent)
0122         return dev->parent->of_node == data;
0123 
0124     return 0;
0125 }
0126 
0127 /* must call put_device() when done with returned i2c_client device */
0128 struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
0129 {
0130     struct device *dev;
0131     struct i2c_client *client;
0132 
0133     dev = bus_find_device_by_of_node(&i2c_bus_type, node);
0134     if (!dev)
0135         return NULL;
0136 
0137     client = i2c_verify_client(dev);
0138     if (!client)
0139         put_device(dev);
0140 
0141     return client;
0142 }
0143 EXPORT_SYMBOL(of_find_i2c_device_by_node);
0144 
0145 /* must call put_device() when done with returned i2c_adapter device */
0146 struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
0147 {
0148     struct device *dev;
0149     struct i2c_adapter *adapter;
0150 
0151     dev = bus_find_device(&i2c_bus_type, NULL, node,
0152                   of_dev_or_parent_node_match);
0153     if (!dev)
0154         return NULL;
0155 
0156     adapter = i2c_verify_adapter(dev);
0157     if (!adapter)
0158         put_device(dev);
0159 
0160     return adapter;
0161 }
0162 EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
0163 
0164 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
0165 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
0166 {
0167     struct i2c_adapter *adapter;
0168 
0169     adapter = of_find_i2c_adapter_by_node(node);
0170     if (!adapter)
0171         return NULL;
0172 
0173     if (!try_module_get(adapter->owner)) {
0174         put_device(&adapter->dev);
0175         adapter = NULL;
0176     }
0177 
0178     return adapter;
0179 }
0180 EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
0181 
0182 static const struct of_device_id*
0183 i2c_of_match_device_sysfs(const struct of_device_id *matches,
0184                   struct i2c_client *client)
0185 {
0186     const char *name;
0187 
0188     for (; matches->compatible[0]; matches++) {
0189         /*
0190          * Adding devices through the i2c sysfs interface provides us
0191          * a string to match which may be compatible with the device
0192          * tree compatible strings, however with no actual of_node the
0193          * of_match_device() will not match
0194          */
0195         if (sysfs_streq(client->name, matches->compatible))
0196             return matches;
0197 
0198         name = strchr(matches->compatible, ',');
0199         if (!name)
0200             name = matches->compatible;
0201         else
0202             name++;
0203 
0204         if (sysfs_streq(client->name, name))
0205             return matches;
0206     }
0207 
0208     return NULL;
0209 }
0210 
0211 const struct of_device_id
0212 *i2c_of_match_device(const struct of_device_id *matches,
0213              struct i2c_client *client)
0214 {
0215     const struct of_device_id *match;
0216 
0217     if (!(client && matches))
0218         return NULL;
0219 
0220     match = of_match_device(matches, &client->dev);
0221     if (match)
0222         return match;
0223 
0224     return i2c_of_match_device_sysfs(matches, client);
0225 }
0226 EXPORT_SYMBOL_GPL(i2c_of_match_device);
0227 
0228 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
0229 static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
0230              void *arg)
0231 {
0232     struct of_reconfig_data *rd = arg;
0233     struct i2c_adapter *adap;
0234     struct i2c_client *client;
0235 
0236     switch (of_reconfig_get_state_change(action, rd)) {
0237     case OF_RECONFIG_CHANGE_ADD:
0238         adap = of_find_i2c_adapter_by_node(rd->dn->parent);
0239         if (adap == NULL)
0240             return NOTIFY_OK;   /* not for us */
0241 
0242         if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
0243             put_device(&adap->dev);
0244             return NOTIFY_OK;
0245         }
0246 
0247         client = of_i2c_register_device(adap, rd->dn);
0248         if (IS_ERR(client)) {
0249             dev_err(&adap->dev, "failed to create client for '%pOF'\n",
0250                  rd->dn);
0251             put_device(&adap->dev);
0252             of_node_clear_flag(rd->dn, OF_POPULATED);
0253             return notifier_from_errno(PTR_ERR(client));
0254         }
0255         put_device(&adap->dev);
0256         break;
0257     case OF_RECONFIG_CHANGE_REMOVE:
0258         /* already depopulated? */
0259         if (!of_node_check_flag(rd->dn, OF_POPULATED))
0260             return NOTIFY_OK;
0261 
0262         /* find our device by node */
0263         client = of_find_i2c_device_by_node(rd->dn);
0264         if (client == NULL)
0265             return NOTIFY_OK;   /* no? not meant for us */
0266 
0267         /* unregister takes one ref away */
0268         i2c_unregister_device(client);
0269 
0270         /* and put the reference of the find */
0271         put_device(&client->dev);
0272         break;
0273     }
0274 
0275     return NOTIFY_OK;
0276 }
0277 
0278 struct notifier_block i2c_of_notifier = {
0279     .notifier_call = of_i2c_notify,
0280 };
0281 #endif /* CONFIG_OF_DYNAMIC */