Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Linux I2C core slave support code
0004  *
0005  * Copyright (C) 2014 by Wolfram Sang <wsa@sang-engineering.com>
0006  */
0007 
0008 #include <dt-bindings/i2c/i2c.h>
0009 #include <linux/acpi.h>
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/i2c.h>
0013 #include <linux/of.h>
0014 
0015 #include "i2c-core.h"
0016 
0017 #define CREATE_TRACE_POINTS
0018 #include <trace/events/i2c_slave.h>
0019 
0020 int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
0021 {
0022     int ret;
0023 
0024     if (WARN(IS_ERR_OR_NULL(client) || !slave_cb, "insufficient data\n"))
0025         return -EINVAL;
0026 
0027     if (!(client->flags & I2C_CLIENT_SLAVE))
0028         dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
0029              __func__);
0030 
0031     if (!(client->flags & I2C_CLIENT_TEN)) {
0032         /* Enforce stricter address checking */
0033         ret = i2c_check_7bit_addr_validity_strict(client->addr);
0034         if (ret) {
0035             dev_err(&client->dev, "%s: invalid address\n", __func__);
0036             return ret;
0037         }
0038     }
0039 
0040     if (!client->adapter->algo->reg_slave) {
0041         dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
0042         return -EOPNOTSUPP;
0043     }
0044 
0045     client->slave_cb = slave_cb;
0046 
0047     i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
0048     ret = client->adapter->algo->reg_slave(client);
0049     i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
0050 
0051     if (ret) {
0052         client->slave_cb = NULL;
0053         dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
0054     }
0055 
0056     return ret;
0057 }
0058 EXPORT_SYMBOL_GPL(i2c_slave_register);
0059 
0060 int i2c_slave_unregister(struct i2c_client *client)
0061 {
0062     int ret;
0063 
0064     if (IS_ERR_OR_NULL(client))
0065         return -EINVAL;
0066 
0067     if (!client->adapter->algo->unreg_slave) {
0068         dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
0069         return -EOPNOTSUPP;
0070     }
0071 
0072     i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
0073     ret = client->adapter->algo->unreg_slave(client);
0074     i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
0075 
0076     if (ret == 0)
0077         client->slave_cb = NULL;
0078     else
0079         dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
0080 
0081     return ret;
0082 }
0083 EXPORT_SYMBOL_GPL(i2c_slave_unregister);
0084 
0085 int i2c_slave_event(struct i2c_client *client,
0086             enum i2c_slave_event event, u8 *val)
0087 {
0088     int ret = client->slave_cb(client, event, val);
0089 
0090     if (trace_i2c_slave_enabled())
0091         trace_i2c_slave(client, event, val, ret);
0092 
0093     return ret;
0094 }
0095 EXPORT_SYMBOL_GPL(i2c_slave_event);
0096 
0097 /**
0098  * i2c_detect_slave_mode - detect operation mode
0099  * @dev: The device owning the bus
0100  *
0101  * This checks the device nodes for an I2C slave by checking the address
0102  * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
0103  * flag this means the device is configured to act as a I2C slave and it will
0104  * be listening at that address.
0105  *
0106  * Returns true if an I2C own slave address is detected, otherwise returns
0107  * false.
0108  */
0109 bool i2c_detect_slave_mode(struct device *dev)
0110 {
0111     if (IS_BUILTIN(CONFIG_OF) && dev->of_node) {
0112         struct device_node *child;
0113         u32 reg;
0114 
0115         for_each_child_of_node(dev->of_node, child) {
0116             of_property_read_u32(child, "reg", &reg);
0117             if (reg & I2C_OWN_SLAVE_ADDRESS) {
0118                 of_node_put(child);
0119                 return true;
0120             }
0121         }
0122     } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) {
0123         dev_dbg(dev, "ACPI slave is not supported yet\n");
0124     }
0125     return false;
0126 }
0127 EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);