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