2 * Linux I2C core slave support code
4 * Copyright (C) 2014 by Wolfram Sang <wsa@sang-engineering.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
12 #include <dt-bindings/i2c/i2c.h>
13 #include <linux/acpi.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
21 int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
25 if (WARN(IS_ERR_OR_NULL(client) || !slave_cb, "insufficient data\n"))
28 if (!(client->flags & I2C_CLIENT_SLAVE))
29 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
32 if (!(client->flags & I2C_CLIENT_TEN)) {
33 /* Enforce stricter address checking */
34 ret = i2c_check_7bit_addr_validity_strict(client->addr);
36 dev_err(&client->dev, "%s: invalid address\n", __func__);
41 if (!client->adapter->algo->reg_slave) {
42 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
46 client->slave_cb = slave_cb;
48 i2c_lock_adapter(client->adapter);
49 ret = client->adapter->algo->reg_slave(client);
50 i2c_unlock_adapter(client->adapter);
53 client->slave_cb = NULL;
54 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
59 EXPORT_SYMBOL_GPL(i2c_slave_register);
61 int i2c_slave_unregister(struct i2c_client *client)
65 if (IS_ERR_OR_NULL(client))
68 if (!client->adapter->algo->unreg_slave) {
69 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
73 i2c_lock_adapter(client->adapter);
74 ret = client->adapter->algo->unreg_slave(client);
75 i2c_unlock_adapter(client->adapter);
78 client->slave_cb = NULL;
80 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
84 EXPORT_SYMBOL_GPL(i2c_slave_unregister);
87 * i2c_detect_slave_mode - detect operation mode
88 * @dev: The device owning the bus
90 * This checks the device nodes for an I2C slave by checking the address
91 * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
92 * flag this means the device is configured to act as a I2C slave and it will
93 * be listening at that address.
95 * Returns true if an I2C own slave address is detected, otherwise returns
98 bool i2c_detect_slave_mode(struct device *dev)
100 if (IS_BUILTIN(CONFIG_OF) && dev->of_node) {
101 struct device_node *child;
104 for_each_child_of_node(dev->of_node, child) {
105 of_property_read_u32(child, "reg", ®);
106 if (reg & I2C_OWN_SLAVE_ADDRESS) {
111 } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) {
112 dev_dbg(dev, "ACPI slave is not supported yet\n");
116 EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);