1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux I2C core slave support code
5 * Copyright (C) 2014 by Wolfram Sang <wsa@sang-engineering.com>
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>
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/i2c_slave.h>
20 int i2c_slave_register(struct i2c_client
*client
, i2c_slave_cb_t slave_cb
)
24 if (WARN(IS_ERR_OR_NULL(client
) || !slave_cb
, "insufficient data\n"))
27 if (!(client
->flags
& I2C_CLIENT_SLAVE
))
28 dev_warn(&client
->dev
, "%s: client slave flag not set. You might see address collisions\n",
31 if (!(client
->flags
& I2C_CLIENT_TEN
)) {
32 /* Enforce stricter address checking */
33 ret
= i2c_check_7bit_addr_validity_strict(client
->addr
);
35 dev_err(&client
->dev
, "%s: invalid address\n", __func__
);
40 if (!client
->adapter
->algo
->reg_slave
) {
41 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
45 client
->slave_cb
= slave_cb
;
47 i2c_lock_bus(client
->adapter
, I2C_LOCK_ROOT_ADAPTER
);
48 ret
= client
->adapter
->algo
->reg_slave(client
);
49 i2c_unlock_bus(client
->adapter
, I2C_LOCK_ROOT_ADAPTER
);
52 client
->slave_cb
= NULL
;
53 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
58 EXPORT_SYMBOL_GPL(i2c_slave_register
);
60 int i2c_slave_unregister(struct i2c_client
*client
)
64 if (IS_ERR_OR_NULL(client
))
67 if (!client
->adapter
->algo
->unreg_slave
) {
68 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
72 i2c_lock_bus(client
->adapter
, I2C_LOCK_ROOT_ADAPTER
);
73 ret
= client
->adapter
->algo
->unreg_slave(client
);
74 i2c_unlock_bus(client
->adapter
, I2C_LOCK_ROOT_ADAPTER
);
77 client
->slave_cb
= NULL
;
79 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
83 EXPORT_SYMBOL_GPL(i2c_slave_unregister
);
85 int i2c_slave_event(struct i2c_client
*client
,
86 enum i2c_slave_event event
, u8
*val
)
88 int ret
= client
->slave_cb(client
, event
, val
);
90 if (trace_i2c_slave_enabled())
91 trace_i2c_slave(client
, event
, val
, ret
);
95 EXPORT_SYMBOL_GPL(i2c_slave_event
);
98 * i2c_detect_slave_mode - detect operation mode
99 * @dev: The device owning the bus
101 * This checks the device nodes for an I2C slave by checking the address
102 * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
103 * flag this means the device is configured to act as a I2C slave and it will
104 * be listening at that address.
106 * Returns true if an I2C own slave address is detected, otherwise returns
109 bool i2c_detect_slave_mode(struct device
*dev
)
111 if (IS_BUILTIN(CONFIG_OF
) && dev
->of_node
) {
114 for_each_child_of_node_scoped(dev
->of_node
, child
) {
115 of_property_read_u32(child
, "reg", ®
);
116 if (reg
& I2C_OWN_SLAVE_ADDRESS
)
119 } else if (IS_BUILTIN(CONFIG_ACPI
) && ACPI_HANDLE(dev
)) {
120 dev_dbg(dev
, "ACPI slave is not supported yet\n");
124 EXPORT_SYMBOL_GPL(i2c_detect_slave_mode
);