1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux I2C core slave support code
4 *
5 * Copyright (C) 2014 by Wolfram Sang <wsa@sang-engineering.com>
6 */
7
8 #include <dt-bindings/i2c/i2c.h>
9 #include <linux/acpi.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/of.h>
14 #include <linux/property.h>
15
16 #include "i2c-core.h"
17
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/i2c_slave.h>
20
i2c_slave_register(struct i2c_client * client,i2c_slave_cb_t slave_cb)21 int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
22 {
23 int ret;
24
25 if (WARN(IS_ERR_OR_NULL(client) || !slave_cb, "insufficient data\n"))
26 return -EINVAL;
27
28 if (!(client->flags & I2C_CLIENT_SLAVE))
29 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
30 __func__);
31
32 if (!(client->flags & I2C_CLIENT_TEN)) {
33 /* Enforce stricter address checking */
34 ret = i2c_check_7bit_addr_validity_strict(client->addr);
35 if (ret) {
36 dev_err(&client->dev, "%s: invalid address\n", __func__);
37 return ret;
38 }
39 }
40
41 if (!client->adapter->algo->reg_slave) {
42 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
43 return -EOPNOTSUPP;
44 }
45
46 client->slave_cb = slave_cb;
47
48 i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
49 ret = client->adapter->algo->reg_slave(client);
50 i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
51
52 if (ret) {
53 client->slave_cb = NULL;
54 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
55 }
56
57 return ret;
58 }
59 EXPORT_SYMBOL_GPL(i2c_slave_register);
60
i2c_slave_unregister(struct i2c_client * client)61 int i2c_slave_unregister(struct i2c_client *client)
62 {
63 int ret;
64
65 if (IS_ERR_OR_NULL(client))
66 return -EINVAL;
67
68 if (!client->adapter->algo->unreg_slave) {
69 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
70 return -EOPNOTSUPP;
71 }
72
73 i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
74 ret = client->adapter->algo->unreg_slave(client);
75 i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
76
77 if (ret == 0)
78 client->slave_cb = NULL;
79 else
80 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
81
82 return ret;
83 }
84 EXPORT_SYMBOL_GPL(i2c_slave_unregister);
85
i2c_slave_event(struct i2c_client * client,enum i2c_slave_event event,u8 * val)86 int i2c_slave_event(struct i2c_client *client,
87 enum i2c_slave_event event, u8 *val)
88 {
89 int ret = client->slave_cb(client, event, val);
90
91 if (trace_i2c_slave_enabled())
92 trace_i2c_slave(client, event, val, ret);
93
94 return ret;
95 }
96 EXPORT_SYMBOL_GPL(i2c_slave_event);
97
98 /**
99 * i2c_detect_slave_mode - detect operation mode
100 * @dev: The device owning the bus
101 *
102 * This checks the device nodes for an I2C slave by checking the address
103 * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
104 * flag this means the device is configured to act as a I2C slave and it will
105 * be listening at that address.
106 *
107 * Returns true if an I2C own slave address is detected, otherwise returns
108 * false.
109 */
i2c_detect_slave_mode(struct device * dev)110 bool i2c_detect_slave_mode(struct device *dev)
111 {
112 struct fwnode_handle *fwnode = dev_fwnode(dev);
113
114 if (is_of_node(fwnode)) {
115 struct fwnode_handle *child __free(fwnode_handle) = NULL;
116 u32 reg;
117
118 fwnode_for_each_child_node(fwnode, child) {
119 fwnode_property_read_u32(child, "reg", ®);
120 if (reg & I2C_OWN_SLAVE_ADDRESS)
121 return true;
122 }
123 } else if (is_acpi_device_node(fwnode)) {
124 dev_dbg(dev, "ACPI slave is not supported yet\n");
125 }
126 return false;
127 }
128 EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);
129