summaryrefslogtreecommitdiff
path: root/drivers/i2c
diff options
context:
space:
mode:
authorJae Hyun Yoo <jae.hyun.yoo@intel.com>2019-02-16 03:05:09 +0300
committerJae Hyun Yoo <jae.hyun.yoo@linux.intel.com>2021-11-05 10:22:07 +0300
commit5652a04f2654d8b0c6883a254d01fbf22260e701 (patch)
tree2755fbf9e90e806abd421f9ee4b64f3795676471 /drivers/i2c
parentf5b63d083feb8183c81a9db6ef5152401797a60a (diff)
downloadlinux-5652a04f2654d8b0c6883a254d01fbf22260e701.tar.xz
i2c: Add mux hold/unhold msg types
This commit adds mux hold/unhold message types to support extended mux control for IPMB and MCTP devices. A hold or an unhold message can be added at the end of I2C message stream wrapped by repeated-start, also can be used as a single message independantly. This mux hold/unhold message will be delivered throughout all mux levels in the path. Means that if it goes to multi-level mux path, all muxes will be held/unheld by this message. 1. Hold message struct i2c_msg msg; uint16_t timeout = 5000; // timeout in ms. 5 secs in this example. msg.addr = 0x0; // any value can be used. addr will be ignored in this packet. msg.flags = I2C_M_HOLD; // set this flag to indicate it's a hold message. msg.len = sizeof(uint16_t); // timeout value will be delivered using two bytes buffer. msg.buf = (uint8_t *)&timeout; // set timeout value. 2. Unhold message struct i2c_msg msg; uint16_t timeout = 0; // set 0 for an unhold message. msg.addr = 0x0; // any value can be used. addr will be ignored in this packet. msg.flags = I2C_M_HOLD; // set this flag to indicate it's an unhold message. msg.len = sizeof(uint16_t); // timeout value will be delivered using two bytes buffer. msg.buf = (uint8_t *)&timeout; // set timeout value. This unhold message can be delivered to a mux adapter even when a bus is locked so that any holding state can be unheld immediately by invoking this unhold message. This patch would not be welcomed from upstream so it should be kept in downstream only. Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/i2c-core-base.c76
-rw-r--r--drivers/i2c/i2c-core-smbus.c22
-rw-r--r--drivers/i2c/i2c-mux.c112
3 files changed, 194 insertions, 16 deletions
diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 54964fbe3f03..734834779d41 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1523,6 +1523,27 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
}
EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
+static void i2c_adapter_hold(struct i2c_adapter *adapter, unsigned long timeout)
+{
+ mutex_lock(&adapter->hold_lock);
+ schedule_delayed_work(&adapter->unhold_work, timeout);
+}
+
+static void i2c_adapter_unhold(struct i2c_adapter *adapter)
+{
+ cancel_delayed_work_sync(&adapter->unhold_work);
+ mutex_unlock(&adapter->hold_lock);
+}
+
+static void i2c_adapter_unhold_work(struct work_struct *work)
+{
+ struct delayed_work *dwork = to_delayed_work(work);
+ struct i2c_adapter *adapter = container_of(dwork, struct i2c_adapter,
+ unhold_work);
+
+ mutex_unlock(&adapter->hold_lock);
+}
+
static int i2c_register_adapter(struct i2c_adapter *adap)
{
int res = -EINVAL;
@@ -1607,6 +1628,9 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
mutex_unlock(&core_lock);
+ mutex_init(&adap->hold_lock);
+ INIT_DELAYED_WORK(&adap->unhold_work, i2c_adapter_unhold_work);
+
return 0;
out_reg:
@@ -1827,6 +1851,8 @@ void i2c_del_adapter(struct i2c_adapter *adap)
idr_remove(&i2c_adapter_idr, adap->nr);
mutex_unlock(&core_lock);
+ i2c_adapter_unhold(adap);
+
/* Clear the device structure in case this adapter is ever going to be
added again */
memset(&adap->dev, 0, sizeof(adap->dev));
@@ -2171,7 +2197,9 @@ static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs,
*/
int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
+ enum i2c_hold_msg_type hold_msg = I2C_HOLD_MSG_NONE;
unsigned long orig_jiffies;
+ unsigned long timeout;
int ret, try;
if (WARN_ON(!msgs || num < 1))
@@ -2184,6 +2212,25 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
return -EOPNOTSUPP;
+ /* Do not deliver a mux hold msg to root bus adapter */
+ if (!i2c_parent_is_i2c_adapter(adap)) {
+ hold_msg = i2c_check_hold_msg(msgs[num - 1].flags,
+ msgs[num - 1].len,
+ (u16 *)msgs[num - 1].buf);
+ if (hold_msg == I2C_HOLD_MSG_SET) {
+ timeout = msecs_to_jiffies(*(u16 *)msgs[num - 1].buf);
+ i2c_adapter_hold(adap, timeout);
+
+ if (--num == 0)
+ return 0;
+ } else if (hold_msg == I2C_HOLD_MSG_RESET) {
+ i2c_adapter_unhold(adap);
+ return 0;
+ } else if (hold_msg == I2C_HOLD_MSG_NONE) {
+ mutex_lock(&adap->hold_lock);
+ }
+ }
+
/*
* i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
* enabled. This is an efficient way of keeping the for-loop from
@@ -2220,6 +2267,13 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
trace_i2c_result(adap, num, ret);
}
+ if (!i2c_parent_is_i2c_adapter(adap)) {
+ if (hold_msg == I2C_HOLD_MSG_SET && ret < 0)
+ i2c_adapter_unhold(adap);
+ else if (hold_msg == I2C_HOLD_MSG_NONE)
+ mutex_unlock(&adap->hold_lock);
+ }
+
return ret;
}
EXPORT_SYMBOL(__i2c_transfer);
@@ -2238,6 +2292,7 @@ EXPORT_SYMBOL(__i2c_transfer);
*/
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
+ bool do_bus_lock = true;
int ret;
if (!adap->algo->master_xfer) {
@@ -2261,12 +2316,25 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
* one (discarding status on the second message) or errno
* (discarding status on the first one).
*/
- ret = __i2c_lock_bus_helper(adap);
- if (ret)
- return ret;
+ /*
+ * Do not lock a bus for delivering an unhold msg to a mux
+ * adpater. This is just for a single length unhold msg case.
+ */
+ if (num == 1 && i2c_parent_is_i2c_adapter(adap) &&
+ i2c_check_hold_msg(msgs[0].flags, msgs[0].len,
+ (u16 *)msgs[0].buf) ==
+ I2C_HOLD_MSG_RESET)
+ do_bus_lock = false;
+
+ if (do_bus_lock) {
+ ret = __i2c_lock_bus_helper(adap);
+ if (ret)
+ return ret;
+ }
ret = __i2c_transfer(adap, msgs, num);
- i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
+ if (do_bus_lock)
+ i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
return ret;
}
diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
index e5b2d1465e7e..e4ec158d38f1 100644
--- a/drivers/i2c/i2c-core-smbus.c
+++ b/drivers/i2c/i2c-core-smbus.c
@@ -535,15 +535,29 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
unsigned short flags, char read_write,
u8 command, int protocol, union i2c_smbus_data *data)
{
+ bool do_bus_lock = true;
s32 res;
- res = __i2c_lock_bus_helper(adapter);
- if (res)
- return res;
+ /*
+ * Do not lock a bus for delivering an unhold msg to a mux adpater.
+ * This is just for a single length unhold msg case.
+ */
+ if (i2c_parent_is_i2c_adapter(adapter) &&
+ i2c_check_hold_msg(flags,
+ protocol == I2C_SMBUS_WORD_DATA ? 2 : 0,
+ &data->word) == I2C_HOLD_MSG_RESET)
+ do_bus_lock = false;
+
+ if (do_bus_lock) {
+ res = __i2c_lock_bus_helper(adapter);
+ if (res)
+ return res;
+ }
res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
command, protocol, data);
- i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
+ if (do_bus_lock)
+ i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
return res;
}
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index 774507b54b57..ce3c3ad3f129 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -36,21 +36,61 @@ struct i2c_mux_priv {
u32 chan_id;
};
+static void i2c_mux_hold(struct i2c_mux_core *muxc, unsigned long timeout)
+{
+ mutex_lock(&muxc->hold_lock);
+ schedule_delayed_work(&muxc->unhold_work, timeout);
+}
+
+static void i2c_mux_unhold(struct i2c_mux_core *muxc)
+{
+ cancel_delayed_work_sync(&muxc->unhold_work);
+ mutex_unlock(&muxc->hold_lock);
+}
+
+static void i2c_mux_unhold_work(struct work_struct *work)
+{
+ struct delayed_work *dwork = to_delayed_work(work);
+ struct i2c_mux_core *muxc = container_of(dwork, struct i2c_mux_core,
+ unhold_work);
+
+ mutex_unlock(&muxc->hold_lock);
+}
+
static int __i2c_mux_master_xfer(struct i2c_adapter *adap,
struct i2c_msg msgs[], int num)
{
struct i2c_mux_priv *priv = adap->algo_data;
struct i2c_mux_core *muxc = priv->muxc;
struct i2c_adapter *parent = muxc->parent;
+ enum i2c_hold_msg_type hold_msg;
+ unsigned long timeout;
int ret;
/* Switch to the right mux port and perform the transfer. */
+ hold_msg = i2c_check_hold_msg(msgs[num - 1].flags,
+ msgs[num - 1].len,
+ (u16 *)msgs[num - 1].buf);
+ if (hold_msg == I2C_HOLD_MSG_SET) {
+ timeout = msecs_to_jiffies(*(u16 *)msgs[num - 1].buf);
+ i2c_mux_hold(muxc, timeout);
+ } else if (hold_msg == I2C_HOLD_MSG_NONE) {
+ mutex_lock(&muxc->hold_lock);
+ }
ret = muxc->select(muxc, priv->chan_id);
if (ret >= 0)
ret = __i2c_transfer(parent, msgs, num);
- if (muxc->deselect)
- muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg != I2C_HOLD_MSG_SET) {
+ if (muxc->deselect)
+ muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg == I2C_HOLD_MSG_RESET)
+ i2c_mux_unhold(muxc);
+ else
+ mutex_unlock(&muxc->hold_lock);
+ } else if (hold_msg == I2C_HOLD_MSG_SET && ret < 0) {
+ i2c_mux_unhold(muxc);
+ }
return ret;
}
@@ -61,15 +101,32 @@ static int i2c_mux_master_xfer(struct i2c_adapter *adap,
struct i2c_mux_priv *priv = adap->algo_data;
struct i2c_mux_core *muxc = priv->muxc;
struct i2c_adapter *parent = muxc->parent;
+ enum i2c_hold_msg_type hold_msg;
+ unsigned long timeout;
int ret;
/* Switch to the right mux port and perform the transfer. */
+ hold_msg = i2c_check_hold_msg(msgs[num - 1].flags,
+ msgs[num - 1].len,
+ (u16 *)msgs[num - 1].buf);
+ if (hold_msg == I2C_HOLD_MSG_SET) {
+ timeout = msecs_to_jiffies(*(u16 *)msgs[num - 1].buf);
+ i2c_mux_hold(muxc, timeout);
+ } else if (hold_msg == I2C_HOLD_MSG_NONE) {
+ mutex_lock(&muxc->hold_lock);
+ }
ret = muxc->select(muxc, priv->chan_id);
if (ret >= 0)
ret = i2c_transfer(parent, msgs, num);
- if (muxc->deselect)
- muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg != I2C_HOLD_MSG_SET) {
+ if (muxc->deselect)
+ muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg == I2C_HOLD_MSG_RESET)
+ i2c_mux_unhold(muxc);
+ else
+ mutex_unlock(&muxc->hold_lock);
+ }
return ret;
}
@@ -82,16 +139,33 @@ static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
struct i2c_mux_priv *priv = adap->algo_data;
struct i2c_mux_core *muxc = priv->muxc;
struct i2c_adapter *parent = muxc->parent;
+ enum i2c_hold_msg_type hold_msg;
+ unsigned long timeout;
int ret;
/* Select the right mux port and perform the transfer. */
+ hold_msg = i2c_check_hold_msg(flags,
+ size == I2C_SMBUS_WORD_DATA ? 2 : 0,
+ &data->word);
+ if (hold_msg == I2C_HOLD_MSG_SET) {
+ timeout = msecs_to_jiffies(data->word);
+ i2c_mux_hold(muxc, timeout);
+ } else if (hold_msg == I2C_HOLD_MSG_NONE) {
+ mutex_lock(&muxc->hold_lock);
+ }
ret = muxc->select(muxc, priv->chan_id);
if (ret >= 0)
ret = __i2c_smbus_xfer(parent, addr, flags,
read_write, command, size, data);
- if (muxc->deselect)
- muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg != I2C_HOLD_MSG_SET) {
+ if (muxc->deselect)
+ muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg == I2C_HOLD_MSG_RESET)
+ i2c_mux_unhold(muxc);
+ else
+ mutex_unlock(&muxc->hold_lock);
+ }
return ret;
}
@@ -104,16 +178,33 @@ static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
struct i2c_mux_priv *priv = adap->algo_data;
struct i2c_mux_core *muxc = priv->muxc;
struct i2c_adapter *parent = muxc->parent;
+ enum i2c_hold_msg_type hold_msg;
+ unsigned long timeout;
int ret;
/* Select the right mux port and perform the transfer. */
+ hold_msg = i2c_check_hold_msg(flags,
+ size == I2C_SMBUS_WORD_DATA ? 2 : 0,
+ &data->word);
+ if (hold_msg == I2C_HOLD_MSG_SET) {
+ timeout = msecs_to_jiffies(data->word);
+ i2c_mux_hold(muxc, timeout);
+ } else if (hold_msg == I2C_HOLD_MSG_NONE) {
+ mutex_lock(&muxc->hold_lock);
+ }
ret = muxc->select(muxc, priv->chan_id);
if (ret >= 0)
ret = i2c_smbus_xfer(parent, addr, flags,
read_write, command, size, data);
- if (muxc->deselect)
- muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg != I2C_HOLD_MSG_SET) {
+ if (muxc->deselect)
+ muxc->deselect(muxc, priv->chan_id);
+ if (hold_msg == I2C_HOLD_MSG_RESET)
+ i2c_mux_unhold(muxc);
+ else
+ mutex_unlock(&muxc->hold_lock);
+ }
return ret;
}
@@ -263,6 +354,9 @@ struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
muxc->deselect = deselect;
muxc->max_adapters = max_adapters;
+ mutex_init(&muxc->hold_lock);
+ INIT_DELAYED_WORK(&muxc->unhold_work, i2c_mux_unhold_work);
+
return muxc;
}
EXPORT_SYMBOL_GPL(i2c_mux_alloc);
@@ -441,6 +535,8 @@ void i2c_mux_del_adapters(struct i2c_mux_core *muxc)
{
char symlink_name[20];
+ i2c_mux_unhold(muxc);
+
while (muxc->num_adapters) {
struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters];
struct i2c_mux_priv *priv = adap->algo_data;