Linux Audio

Check our new training course

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