i2c: designware: Enable mode swapping
Subsystems (4)
v2 cover i2c: designware: Enable mode swapping
Hi,
Changed since v1:
- The init cb is replaced with i2c_dw_init() also in i2c-designware-amdisp.c
- Checking I2C_FUNC_SLAVE in i2c_dw_reg_slave()
The original cover letter:
It's currently not possible to support MCTP or any other protocol that requires support for both modes with the DesignWare I2C because the driver can only be used in one mode. I'm assuming that the driver was designed this way because the DesignWare I2C can not be operated as I2C master and I2C slave simultaneously, however, that does not actually mean both modes could not be supported at the same time. See the patch 5/6 for more detailed explanation.
This series will enable support for both modes in the driver by utilising a simple mode swap method, and that way make it possible to support MCTP, IPMI, SMBus Host Notification Protocol, and any other protocol requires the support for both modes with the DesignWare I2C.
thanks,
Heikki Krogerus (6):
i2c: designware: Remove useless driver specific option for I2C target
i2c: designware: Remove unnecessary function exports
i2c: designware: Combine some of the common functions
i2c: designware: Combine the init functions
i2c: designware: Enable mode swapping
i2c: designware: Remove an unnecessary condition
drivers/i2c/busses/Kconfig | 10 +- drivers/i2c/busses/Makefile | 2 +- drivers/i2c/busses/i2c-designware-amdisp.c | 4 +- drivers/i2c/busses/i2c-designware-common.c | 211 +++++++++++++++++++-- drivers/i2c/busses/i2c-designware-core.h | 25 +-- drivers/i2c/busses/i2c-designware-master.c | 174 ++--------------- drivers/i2c/busses/i2c-designware-slave.c | 127 ++----------- 7 files changed, 254 insertions(+), 299 deletions(-)
Signature · 1 line
2.50.1
PATCH v2 1/6 i2c: designware: Remove useless driver specific option for I2C target
The generic option for I2C target is already user selectable, which makes the DesignWare specific option completely unnecessary. The DesignWare option also silently selected I2C_SLAVE instead of depending on it without any real need for it.
--- drivers/i2c/busses/Kconfig | 10 ++-------- drivers/i2c/busses/Makefile | 2 +- drivers/i2c/busses/i2c-designware-core.h | 2 +- 3 files changed, 4 insertions(+), 10 deletions(-)
drivers/i2c/busses/Kconfig +2 -8
@@ -569,20 +569,14 @@ config I2C_DESIGNWARE_CORE help This option enables support for the Synopsys DesignWare I2C adapter. This driver includes support for the I2C host on the Synopsys - Designware I2C adapter. + Designware I2C adapter, and the I2C slave when enabled (select + I2C_SLAVE). To compile the driver as a module, choose M here: the module will be called i2c-designware-core. if I2C_DESIGNWARE_CORE -config I2C_DESIGNWARE_SLAVE - bool "Synopsys DesignWare Slave" - select I2C_SLAVE - help - If you say yes to this option, support will be included for the - Synopsys DesignWare I2C slave adapter. - config I2C_DESIGNWARE_PLATFORM tristate "Synopsys DesignWare Platform driver" depends on (ACPI && COMMON_CLK) || !ACPI
drivers/i2c/busses/Makefile +1 -1
@@ -53,7 +53,7 @@ obj-$(CONFIG_I2C_DAVINCI) += i2c-davinci.o obj-$(CONFIG_I2C_DESIGNWARE_CORE) += i2c-designware-core.o i2c-designware-core-y := i2c-designware-common.o i2c-designware-core-y += i2c-designware-master.o -i2c-designware-core-$(CONFIG_I2C_DESIGNWARE_SLAVE) += i2c-designware-slave.o +i2c-designware-core-$(CONFIG_I2C_SLAVE) += i2c-designware-slave.o obj-$(CONFIG_I2C_DESIGNWARE_PLATFORM) += i2c-designware-platform.o i2c-designware-platform-y := i2c-designware-platdrv.o i2c-designware-platform-$(CONFIG_I2C_DESIGNWARE_AMDPSP) += i2c-designware-amdpsp.o
drivers/i2c/busses/i2c-designware-core.h +1 -1
@@ -386,7 +386,7 @@ void i2c_dw_disable(struct dw_i2c_dev *dev);extern void i2c_dw_configure_master (struct dw_i2c_dev *dev);extern int i2c_dw_probe_master (struct dw_i2c_dev *dev); -#if IS_ENABLED (CONFIG_I2C_DESIGNWARE_SLAVE ) +#if IS_ENABLED (CONFIG_I2C_SLAVE )extern void i2c_dw_configure_slave (struct dw_i2c_dev *dev);extern int i2c_dw_probe_slave (struct dw_i2c_dev *dev);#else
Signature · 1 line
2.50.1
PATCH v2 2/6 i2c: designware: Remove unnecessary function exports
In reply to Heikki KrogerusThe master and slave probe functions are only called from the core.
--- drivers/i2c/busses/i2c-designware-master.c | 1 - drivers/i2c/busses/i2c-designware-slave.c | 1 - 2 files changed, 2 deletions(-)
drivers/i2c/busses/i2c-designware-master.c +0 -1
@@ -1101,7 +1101,6 @@ int i2c_dw_probe_master(struct dw_i2c_dev *dev)return ret; } -EXPORT_SYMBOL_GPL (i2c_dw_probe_master);MODULE_DESCRIPTION ("Synopsys DesignWare I2C bus master adapter" );MODULE_LICENSE ("GPL" );
drivers/i2c/busses/i2c-designware-slave.c +0 -1
@@ -277,7 +277,6 @@ int i2c_dw_probe_slave(struct dw_i2c_dev *dev)return ret; } -EXPORT_SYMBOL_GPL (i2c_dw_probe_slave);MODULE_AUTHOR ("Luis Oliveira <[email protected]>" );MODULE_DESCRIPTION ("Synopsys DesignWare I2C bus slave adapter" );
Signature · 1 line
2.50.1
PATCH v2 3/6 i2c: designware: Combine some of the common functions
In reply to Heikki KrogerusThe adapter can be registered just in the core instead of separately in the master and slave drivers. The same applies to the interrupt.
--- drivers/i2c/busses/i2c-designware-common.c | 108 +++++++++++++++++++-- drivers/i2c/busses/i2c-designware-core.h | 11 ++- drivers/i2c/busses/i2c-designware-master.c | 97 +++--------------- drivers/i2c/busses/i2c-designware-slave.c | 53 ++-------- 4 files changed, 126 insertions(+), 143 deletions(-)
drivers/i2c/busses/i2c-designware-common.c +101 -7
@@ -131,7 +131,7 @@ static int dw_reg_write_word(void *context, unsigned int reg, unsigned int val)* * Return: 0 on success, or negative errno otherwise. */ -int i2c_dw_init_regmap (struct dw_i2c_dev *dev) +static int i2c_dw_init_regmap (struct dw_i2c_dev *dev) {struct regmap_config map_cfg = { .reg_bits =32 ,
@@ -457,7 +457,7 @@ u32 i2c_dw_scl_lcnt(struct dw_i2c_dev *dev, unsigned int reg, u32 ic_clk,return DIV_ROUND_CLOSEST_ULL ((u64 )ic_clk * (tLOW + tf),MICRO ) -1 + offset; } -int i2c_dw_set_sda_hold (struct dw_i2c_dev *dev) +static int i2c_dw_set_sda_hold (struct dw_i2c_dev *dev) {unsigned int reg;int ret;
@@ -672,7 +672,7 @@ int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)return -EIO ; } -int i2c_dw_set_fifo_size (struct dw_i2c_dev *dev) +static int i2c_dw_set_fifo_size (struct dw_i2c_dev *dev) {u32 tx_fifo_depth, rx_fifo_depth;unsigned int param;
@@ -741,19 +741,113 @@ void i2c_dw_disable(struct dw_i2c_dev *dev) }EXPORT_SYMBOL_GPL (i2c_dw_disable); +static irqreturn_t i2c_dw_isr (int this_irq,void *dev_id) +{ +struct dw_i2c_dev *dev = dev_id; + +if (dev->mode ==DW_IC_SLAVE ) +return i2c_dw_isr_slave (dev); + +return i2c_dw_isr_master (dev); +} + +static const struct i2c_algorithm i2c_dw_algo = { + .xfer = i2c_dw_xfer, + .functionality = i2c_dw_func, +#if IS_ENABLED(CONFIG_I2C_SLAVE) + .reg_slave = i2c_dw_reg_slave, + .unreg_slave = i2c_dw_unreg_slave, +#endif +}; + +static const struct i2c_adapter_quirks i2c_dw_quirks = { + .flags =I2C_AQ_NO_ZERO_LEN , +}; +int i2c_dw_probe (struct dw_i2c_dev *dev) { +struct i2c_adapter *adap = &dev->adapter; +unsigned long irq_flags; +int ret; +device_set_node (&dev->adapter.dev,dev_fwnode (dev->dev)); + ret =i2c_dw_init_regmap (dev); +if (ret) +return ret; + + ret =i2c_dw_set_sda_hold (dev); +if (ret) +return ret; + + ret =i2c_dw_set_fifo_size (dev); +if (ret) +return ret; +switch (dev->mode) {case DW_IC_SLAVE : -return i2c_dw_probe_slave (dev); + ret =i2c_dw_probe_slave (dev); +break ;case DW_IC_MASTER : -return i2c_dw_probe_master (dev); + ret =i2c_dw_probe_master (dev); +break ;default : -dev_err (dev->dev,"Wrong operation mode: %d\n" , dev->mode); -return -EINVAL ; + ret = -EINVAL ; +break ; } +if (ret) +return ret; + + ret = dev->init (dev); +if (ret) +return ret; + +if (!adap->name[0 ]) +strscpy (adap->name,"Synopsys DesignWare I2C adapter" ); + + adap->retries =3 ; + adap->algo = &i2c_dw_algo; + adap->quirks = &i2c_dw_quirks; + adap->dev.parent = dev->dev; +i2c_set_adapdata (adap, dev); + +/* +* REVISIT: The mode check may not be necessary. +* For now keeping the flags as they were originally. +*/ +if (dev->mode ==DW_IC_SLAVE ) + irq_flags =IRQF_SHARED ; +else if (dev->flags &ACCESS_NO_IRQ_SUSPEND ) + irq_flags =IRQF_NO_SUSPEND ; +else + irq_flags =IRQF_SHARED |IRQF_COND_SUSPEND ; + + ret =i2c_dw_acquire_lock (dev); +if (ret) +return ret; + +__i2c_dw_write_intr_mask (dev,0 ); +i2c_dw_release_lock (dev); + +if (!(dev->flags &ACCESS_POLLING )) { + ret =devm_request_irq (dev->dev, dev->irq, i2c_dw_isr, + irq_flags,dev_name (dev->dev), dev); +if (ret) +return ret; + } + +/* +* Increment PM usage count during adapter registration in order to +* avoid possible spurious runtime suspend when adapter device is +* registered to the device core and immediate resume in case bus has +* registered I2C slaves that do I2C transfers in their probe. +*/ +ACQUIRE (pm_runtime_noresume, pm)(dev->dev); + ret =ACQUIRE_ERR (pm_runtime_noresume, &pm); +if (ret) +return ret; + +return i2c_add_numbered_adapter (adap); }EXPORT_SYMBOL_GPL (i2c_dw_probe);
drivers/i2c/busses/i2c-designware-core.h +8 -3
@@ -13,6 +13,7 @@#include <linux/completion.h> #include <linux/errno.h> #include <linux/i2c.h> +#include <linux/irqreturn.h> #include <linux/pm.h> #include <linux/regmap.h> #include <linux/types.h>
@@ -333,20 +334,18 @@ struct i2c_dw_semaphore_callbacks {int (*probe)(struct dw_i2c_dev *dev); }; -int i2c_dw_init_regmap (struct dw_i2c_dev *dev);u32 i2c_dw_scl_hcnt (struct dw_i2c_dev *dev,unsigned int reg,u32 ic_clk,u32 tSYMBOL,u32 tf,int offset);u32 i2c_dw_scl_lcnt (struct dw_i2c_dev *dev,unsigned int reg,u32 ic_clk,u32 tLOW,u32 tf,int offset); -int i2c_dw_set_sda_hold (struct dw_i2c_dev *dev);u32 i2c_dw_clk_rate (struct dw_i2c_dev *dev);int i2c_dw_prepare_clk (struct dw_i2c_dev *dev,bool prepare);int i2c_dw_acquire_lock (struct dw_i2c_dev *dev);void i2c_dw_release_lock (struct dw_i2c_dev *dev);int i2c_dw_wait_bus_not_busy (struct dw_i2c_dev *dev);int i2c_dw_handle_tx_abort (struct dw_i2c_dev *dev); -int i2c_dw_set_fifo_size (struct dw_i2c_dev *dev);u32 i2c_dw_func (struct i2c_adapter *adap); +irqreturn_t i2c_dw_isr_master (struct dw_i2c_dev *dev);extern const struct dev_pm_ops i2c_dw_dev_pm_ops;
@@ -386,12 +385,18 @@ void i2c_dw_disable(struct dw_i2c_dev *dev);extern void i2c_dw_configure_master (struct dw_i2c_dev *dev);extern int i2c_dw_probe_master (struct dw_i2c_dev *dev); +int i2c_dw_xfer (struct i2c_adapter *adap,struct i2c_msg *msgs,int num); +#if IS_ENABLED (CONFIG_I2C_SLAVE )extern void i2c_dw_configure_slave (struct dw_i2c_dev *dev);extern int i2c_dw_probe_slave (struct dw_i2c_dev *dev); +irqreturn_t i2c_dw_isr_slave (struct dw_i2c_dev *dev); +int i2c_dw_reg_slave (struct i2c_client *client); +int i2c_dw_unreg_slave (struct i2c_client *client);#else static inline void i2c_dw_configure_slave (struct dw_i2c_dev *dev) { }static inline int i2c_dw_probe_slave (struct dw_i2c_dev *dev) {return -EINVAL ; } +static inline irqreturn_t i2c_dw_isr_slave (struct dw_i2c_dev *dev) {return IRQ_NONE ; }#endif static inline void i2c_dw_configure (struct dw_i2c_dev *dev)
drivers/i2c/busses/i2c-designware-master.c +11 -86
154 lines not on this page · raw message
drivers/i2c/busses/i2c-designware-slave.c +6 -47
87 lines not on this page · raw message
Signature · 1 line
2.50.1
Andy Shevchenko
reviewerRe: [PATCH v2 3/6] i2c: designware: Combine some of the common functions
Quoted 3 lines · Heikki Krogerus
The adapter can be registered just in the core instead of separately in the master and slave drivers. The same applies to the interrupt.
...
Quoted 34 lines
int
i2c_dw_probe(struct dw_i2c_dev *dev)
{drivers/i2c/busses/i2c-designware-common.c +22
-4+struct i2c_adapter *adap = &dev->adapter; +unsigned long irq_flags; +int ret; +device_set_node (&dev->adapter.dev,dev_fwnode (dev->dev)); + ret =i2c_dw_init_regmap (dev); +if (ret) +return ret; + + ret =i2c_dw_set_sda_hold (dev); +if (ret) +return ret; + + ret =i2c_dw_set_fifo_size (dev); +if (ret) +return ret; +switch (dev->mode) {case DW_IC_SLAVE : -return i2c_dw_probe_slave (dev); + ret =i2c_dw_probe_slave (dev); +break ;case DW_IC_MASTER : -return i2c_dw_probe_master (dev); + ret =i2c_dw_probe_master (dev); +break ;default : -dev_err (dev->dev,"Wrong operation mode: %d\n" , dev->mode); -return -EINVAL ; + ret = -EINVAL ; +break ;
drivers/i2c/busses/i2c-designware-common.c void i2c_dw_disable(struct dw_i2c_dev *dev)
Quoted 10 lines
drivers/i2c/busses/i2c-designware-common.c +8
-0} +if (ret) +return ret; + + ret = dev->init (dev); +if (ret) +return ret; +if (!adap->name[0 ]) +strscpy (adap->name,"Synopsys DesignWare I2C adapter" );
Hmm... See below.
Quoted 44 lines
drivers/i2c/busses/i2c-designware-common.c +43
-0+ adap->retries =3 ; + adap->algo = &i2c_dw_algo; + adap->quirks = &i2c_dw_quirks; + adap->dev.parent = dev->dev; +i2c_set_adapdata (adap, dev); + +/* +* REVISIT: The mode check may not be necessary. +* For now keeping the flags as they were originally. +*/ +if (dev->mode ==DW_IC_SLAVE ) + irq_flags =IRQF_SHARED ; +else if (dev->flags &ACCESS_NO_IRQ_SUSPEND ) + irq_flags =IRQF_NO_SUSPEND ; +else + irq_flags =IRQF_SHARED |IRQF_COND_SUSPEND ; + + ret =i2c_dw_acquire_lock (dev); +if (ret) +return ret; + +__i2c_dw_write_intr_mask (dev,0 ); +i2c_dw_release_lock (dev); + +if (!(dev->flags &ACCESS_POLLING )) { + ret =devm_request_irq (dev->dev, dev->irq, i2c_dw_isr, + irq_flags,dev_name (dev->dev), dev); +if (ret) +return ret; + } + +/* +* Increment PM usage count during adapter registration in order to +* avoid possible spurious runtime suspend when adapter device is +* registered to the device core and immediate resume in case bus has +* registered I2C slaves that do I2C transfers in their probe. +*/ +ACQUIRE (pm_runtime_noresume, pm)(dev->dev); + ret =ACQUIRE_ERR (pm_runtime_noresume, &pm); +if (ret) +return ret; + +return i2c_add_numbered_adapter (adap); }
...
drivers/i2c/busses/i2c-designware-slave.c EXPORT_SYMBOL_GPL(i2c_dw_configure_slave); · line 261
Quoted 2 lines
drivers/i2c/busses/i2c-designware-slave.c +0
-2-snprintf (adap->name,sizeof (adap->name), -"Synopsys DesignWare I2C Slave adapter" );
This patch changes the user visible strings (via i2cdetect) or module names in case we want to find it by name (note, we already have such precedents for other adapters). Currently we have three variants if I not miss anything:
Generic for master (as in this change), Generic for slave, and AMD platform driver case. If you think this is okay change, then just drop the AMD case as well, and hence remove the no more needed conditional. Otherwise I would somehow group this naming in one place, if possible.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/6] i2c: designware: Combine some of the common functions
Hi Andy,
Quoted 10 lines
drivers/i2c/busses/i2c-designware-slave.c EXPORT_SYMBOL_GPL(i2c_dw_configure_slave); · line 261
Quoted 2 lines
drivers/i2c/busses/i2c-designware-slave.c +0
-2-snprintf (adap->name,sizeof (adap->name), -"Synopsys DesignWare I2C Slave adapter" );This patch changes the user visible strings (via
i2cdetect) or module names in case we want to find it by name (note, we already have such precedents for other adapters). Currently we have three variants if I not miss anything:
Generic for master (as in this change), Generic for slave, and AMD platform driver case. If you think this is okay change, then just drop the AMD case as well, and hence remove the no more needed conditional. Otherwise I would somehow group this naming in one place, if possible.
The only thing that this will change is, it removes the common slave/target only description, because after this that setup is no longer possible - master mode is now always supported. So this is the correct thing to do.
I don't think the user space should ever rely on a description like this except possibly with some customised/non-common systems that the user space really has to handle in some specific way, but if something really did rely on this common "target only" description, it could have only used it to determine that it basically can't use the device for anything as it's slave/target only - so basically to use it to check the functionality (same as i2cdetect -F). But as said, this is no longer a problem.
As for the AMD case, if I understood what you are proposing, I disagree with you. The glue drivers should always be allowed to assign the name (these would be the "non-common" systems that the user space may actually need to know about). I'm also against grouping the naming. The glue drivers must handle the platform specifics including the naming if needed, not the core.
thanks,
Signature · 1 line
heikki
Re: [PATCH v2 3/6] i2c: designware: Combine some of the common functions
Tue, Dec 30, 2025 at 04:14:32PM +0200, Heikki Krogerus kirjoitti:
Quoted 27 lines
Hi Andy,
Quoted 10 lines
drivers/i2c/busses/i2c-designware-slave.c EXPORT_SYMBOL_GPL(i2c_dw_configure_slave); · line 261
Quoted 2 lines
drivers/i2c/busses/i2c-designware-slave.c +0
-2-snprintf (adap->name,sizeof (adap->name), -"Synopsys DesignWare I2C Slave adapter" );This patch changes the user visible strings (via
i2cdetect) or module names in case we want to find it by name (note, we already have such precedents for other adapters). Currently we have three variants if I not miss anything:
Generic for master (as in this change), Generic for slave, and AMD platform driver case. If you think this is okay change, then just drop the AMD case as well, and hence remove the no more needed conditional. Otherwise I would somehow group this naming in one place, if possible.The only thing that this will change is, it removes the common slave/target only description, because after this that setup is no longer possible - master mode is now always supported. So this is the correct thing to do.
I don't think the user space should ever rely on a description like this except possibly with some customised/non-common systems that the user space really has to handle in some specific way, but if something really did rely on this common "target only" description, it could have only used it to determine that it basically can't use the device for anything as it's slave/target only - so basically to use it to check the functionality (same as i2cdetect -F). But as said, this is no longer a problem.
Quoted 6 lines
As for the AMD case, if I understood what you are proposing, I disagree with you. The glue drivers should always be allowed to assign the name (these would be the "non-common" systems that the user space may actually need to know about). I'm also against grouping the naming. The glue drivers must handle the platform specifics including the naming if needed, not the core.
Ping.
Signature · 1 line
heikki
Andy Shevchenko
reviewerRe: [PATCH v2 3/6] i2c: designware: Combine some of the common functions
Quoted 1 line · Heikki Krogerus
Tue, Dec 30, 2025 at 04:14:32PM +0200, Heikki Krogerus kirjoitti:
...
Quoted 25 lines
Quoted 25 lines
Quoted 10 lines
drivers/i2c/busses/i2c-designware-slave.c EXPORT_SYMBOL_GPL(i2c_dw_configure_slave); · line 261
Quoted 2 lines
drivers/i2c/busses/i2c-designware-slave.c +0
-2-snprintf (adap->name,sizeof (adap->name), -"Synopsys DesignWare I2C Slave adapter" );This patch changes the user visible strings (via
i2cdetect) or module names in case we want to find it by name (note, we already have such precedents for other adapters). Currently we have three variants if I not miss anything:
Generic for master (as in this change), Generic for slave, and AMD platform driver case. If you think this is okay change, then just drop the AMD case as well, and hence remove the no more needed conditional. Otherwise I would somehow group this naming in one place, if possible.The only thing that this will change is, it removes the common slave/target only description, because after this that setup is no longer possible - master mode is now always supported. So this is the correct thing to do.
I don't think the user space should ever rely on a description like this except possibly with some customised/non-common systems that the user space really has to handle in some specific way, but if something really did rely on this common "target only" description, it could have only used it to determine that it basically can't use the device for anything as it's slave/target only - so basically to use it to check the functionality (same as i2cdetect -F). But as said, this is no longer a problem.
Quoted 8 lines
Quoted 6 lines
As for the AMD case, if I understood what you are proposing, I disagree with you. The glue drivers should always be allowed to assign the name (these would be the "non-common" systems that the user space may actually need to know about). I'm also against grouping the naming. The glue drivers must handle the platform specifics including the naming if needed, not the core.
Ping.
Do you need my input on this? If you think this is good change, make sure the summary of the above goes to the commit message.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/6] i2c: designware: Combine some of the common functions
Mon, Jan 19, 2026 at 03:44:22PM +0200, Andy Shevchenko kirjoitti:
Quoted 37 lines · Heikki Krogerus
Quoted 1 line
Tue, Dec 30, 2025 at 04:14:32PM +0200, Heikki Krogerus kirjoitti:
...
Quoted 32 lines
Quoted 31 lines
Quoted 10 lines
drivers/i2c/busses/i2c-designware-slave.c EXPORT_SYMBOL_GPL(i2c_dw_configure_slave); · line 261
Quoted 2 lines
drivers/i2c/busses/i2c-designware-slave.c +0
-2-snprintf (adap->name,sizeof (adap->name), -"Synopsys DesignWare I2C Slave adapter" );This patch changes the user visible strings (via
i2cdetect) or module names in case we want to find it by name (note, we already have such precedents for other adapters). Currently we have three variants if I not miss anything:
Generic for master (as in this change), Generic for slave, and AMD platform driver case. If you think this is okay change, then just drop the AMD case as well, and hence remove the no more needed conditional. Otherwise I would somehow group this naming in one place, if possible.The only thing that this will change is, it removes the common slave/target only description, because after this that setup is no longer possible - master mode is now always supported. So this is the correct thing to do.
I don't think the user space should ever rely on a description like this except possibly with some customised/non-common systems that the user space really has to handle in some specific way, but if something really did rely on this common "target only" description, it could have only used it to determine that it basically can't use the device for anything as it's slave/target only - so basically to use it to check the functionality (same as i2cdetect -F). But as said, this is no longer a problem.
As for the AMD case, if I understood what you are proposing, I disagree with you. The glue drivers should always be allowed to assign the name (these would be the "non-common" systems that the user space may actually need to know about). I'm also against grouping the naming. The glue drivers must handle the platform specifics including the naming if needed, not the core.
Quoted 4 lines
Quoted 1 line
Ping.
Do you need my input on this? If you think this is good change, make sure the summary of the above goes to the commit message.
Got it.
Thanks Andy,
Signature · 1 line
heikki
PATCH v2 4/6 i2c: designware: Combine the init functions
In reply to Heikki KrogerusProviding a single function for controller initialisation. The controller initialisation has the same steps for master and slave modes, except the timing parameters are only needed in master mode.
--- drivers/i2c/busses/i2c-designware-amdisp.c | 4 +- drivers/i2c/busses/i2c-designware-common.c | 81 +++++++++++++++++++++- drivers/i2c/busses/i2c-designware-core.h | 3 +- drivers/i2c/busses/i2c-designware-master.c | 70 +------------------ drivers/i2c/busses/i2c-designware-slave.c | 44 ------------ 5 files changed, 85 insertions(+), 117 deletions(-)
drivers/i2c/busses/i2c-designware-amdisp.c +2 -2
@@ -163,8 +163,8 @@ static int amd_isp_dw_i2c_plat_runtime_resume(struct device *dev)if (!i_dev->shared_with_punit)i2c_dw_prepare_clk (i_dev, true); -if (i_dev->init) - i_dev->init (i_dev); + +i2c_dw_init (i_dev);return 0 ; }
drivers/i2c/busses/i2c-designware-common.c +79 -2
99 lines not on this page · raw message
drivers/i2c/busses/i2c-designware-core.h +1 -2
@@ -240,7 +240,6 @@ struct reset_control;* @semaphore_idx: Index of table with semaphore type attached to the bus. It's * -1 if there is no semaphore. * @shared_with_punit: true if this bus is shared with the SoC's PUNIT -* @init: function to initialize the I2C hardware * @set_sda_hold_time: callback to retrieve IP specific SDA hold timing * @mode: operation mode - DW_IC_MASTER or DW_IC_SLAVE * @rinfo: I²C GPIO recovery information
@@ -301,7 +300,6 @@ struct dw_i2c_dev {void (*release_lock)(void );int semaphore_idx;bool shared_with_punit; -int (*init)(struct dw_i2c_dev *dev);int (*set_sda_hold_time)(struct dw_i2c_dev *dev);int mode;struct i2c_bus_recovery_info rinfo;
@@ -408,6 +406,7 @@ static inline void i2c_dw_configure(struct dw_i2c_dev *dev) }int i2c_dw_probe (struct dw_i2c_dev *dev); +int i2c_dw_init (struct dw_i2c_dev *dev);#if IS_ENABLED (CONFIG_I2C_DESIGNWARE_BAYTRAIL )int i2c_dw_baytrail_probe_lock_support (struct dw_i2c_dev *dev);
drivers/i2c/busses/i2c-designware-master.c +3 -67
101 lines not on this page · raw message
drivers/i2c/busses/i2c-designware-slave.c +0 -44
56 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [PATCH v2 4/6] i2c: designware: Combine the init functions
Quoted 4 lines · Heikki Krogerus
Providing a single function for controller initialisation. The controller initialisation has the same steps for master and slave modes, except the timing parameters are only needed in master mode.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 5/6 i2c: designware: Enable mode swapping
In reply to Heikki KrogerusThe DesignWare I2C can not be operated as I2C master and I2C slave simultaneously, but that does not actually mean master and slave modes can not be supported at the same time. It just means an explicit mode swap needs to be executed when the mode is changed. The DesignWare I2C documentation actually describes a couple of cases where the mode is excepted to be changed.
The I2C master will now always be supported. Both modes are now always configured in i2c_dw_configure(), but the slave mode will continue to be available only when the Kconfig option I2C_SLAVE is enabled.
The driver will now start in master mode and then swap to slave mode when a slave device is registered. After a slave device is registered, the controller is swapped to master mode when a transfer in master mode is started and then back to slave mode again after the transfer is completed.
The DesignWare I2C can now be used with protocols such as MCTP (drivers/net/mctp/mctp-i2c.c) and IPMI (drivers/char/ipmi/) that require support for both I2C master and I2C slave. It is now also possible to support the SMBus Host Notification Protocol as I2C master if needed.
--- drivers/i2c/busses/i2c-designware-common.c | 50 +++++++++++++++------- drivers/i2c/busses/i2c-designware-core.h | 9 ++-- drivers/i2c/busses/i2c-designware-master.c | 6 ++- drivers/i2c/busses/i2c-designware-slave.c | 35 +++++++-------- 4 files changed, 57 insertions(+), 43 deletions(-)
drivers/i2c/busses/i2c-designware-common.c +34 -16
@@ -358,21 +358,25 @@ static inline u32 i2c_dw_acpi_round_bus_speed(struct device *device) { return 0;#endif /* CONFIG_ACPI */ -static void i2c_dw_configure_mode (struct dw_i2c_dev *dev) +static void i2c_dw_configure_mode (struct dw_i2c_dev *dev,int mode) { -switch (dev->mode) { +switch (mode) {case DW_IC_MASTER :regmap_write (dev->map,DW_IC_TX_TL , dev->tx_fifo_depth /2 );regmap_write (dev->map,DW_IC_RX_TL ,0 );regmap_write (dev->map,DW_IC_CON , dev->master_cfg);break ;case DW_IC_SLAVE : + dev->status =0 ;regmap_write (dev->map,DW_IC_TX_TL ,0 );regmap_write (dev->map,DW_IC_RX_TL ,0 );regmap_write (dev->map,DW_IC_CON , dev->slave_cfg); +regmap_write (dev->map,DW_IC_SAR , dev->slave->addr);regmap_write (dev->map,DW_IC_INTR_MASK ,DW_IC_INTR_SLAVE_MASK ); +__i2c_dw_enable (dev);break ;default : +WARN (1 ,"Invalid mode %d\n" , mode);return ; } }
@@ -394,6 +398,31 @@ static void i2c_dw_write_timings(struct dw_i2c_dev *dev) } } +/** +* i2c_dw_set_mode() - Select the controller mode of operation - master or slave +* @dev: device private data +* @mode: I2C mode of operation +* +* Configures the controller to operate in @mode. This function needs to be +* called when ever a mode swap is required. +* +* Setting the slave mode does not have an effect before a slave device is +* registered. So before the slave device is registered, the controller is kept +* in master mode regardless of @mode. +* +* The controller must be disabled before this function is called. +*/ +void i2c_dw_set_mode (struct dw_i2c_dev *dev,int mode) +{ +if (mode ==DW_IC_SLAVE && !dev->slave) + mode =DW_IC_MASTER ; +if (dev->mode == mode) +return ; + +i2c_dw_configure_mode (dev, mode); + dev->mode = mode; +} +/** * i2c_dw_init() - Initialize the DesignWare I2C hardware * @dev: device private data
@@ -420,14 +449,13 @@ int i2c_dw_init(struct dw_i2c_dev *dev)*/ regmap_write (dev->map,DW_IC_SMBUS_INTR_MASK ,0 ); -if (dev->mode ==DW_IC_MASTER ) -i2c_dw_write_timings (dev); +i2c_dw_write_timings (dev);/* Write SDA hold time if supported */ if (dev->sda_hold_time)regmap_write (dev->map,DW_IC_SDA_HOLD , dev->sda_hold_time); -i2c_dw_configure_mode (dev); +i2c_dw_configure_mode (dev, dev->mode);i2c_dw_release_lock (dev);
@@ -861,17 +889,7 @@ int i2c_dw_probe(struct dw_i2c_dev *dev)if (ret)return ret; -switch (dev->mode) { -case DW_IC_SLAVE : - ret =i2c_dw_probe_slave (dev); -break ; -case DW_IC_MASTER : - ret =i2c_dw_probe_master (dev); -break ; -default : - ret = -EINVAL ; -break ; - } + ret =i2c_dw_probe_master (dev);if (ret)return ret;
drivers/i2c/busses/i2c-designware-core.h +3 -6
@@ -387,26 +387,23 @@ int i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);#if IS_ENABLED (CONFIG_I2C_SLAVE )extern void i2c_dw_configure_slave (struct dw_i2c_dev *dev); -extern int i2c_dw_probe_slave (struct dw_i2c_dev *dev);irqreturn_t i2c_dw_isr_slave (struct dw_i2c_dev *dev);int i2c_dw_reg_slave (struct i2c_client *client);int i2c_dw_unreg_slave (struct i2c_client *client);#else static inline void i2c_dw_configure_slave (struct dw_i2c_dev *dev) { } -static inline int i2c_dw_probe_slave (struct dw_i2c_dev *dev) {return -EINVAL ; }static inline irqreturn_t i2c_dw_isr_slave (struct dw_i2c_dev *dev) {return IRQ_NONE ; }#endif static inline void i2c_dw_configure (struct dw_i2c_dev *dev) { -if (i2c_detect_slave_mode (dev->dev)) -i2c_dw_configure_slave (dev); -else -i2c_dw_configure_master (dev); +i2c_dw_configure_slave (dev); +i2c_dw_configure_master (dev); }int i2c_dw_probe (struct dw_i2c_dev *dev);int i2c_dw_init (struct dw_i2c_dev *dev); +void i2c_dw_set_mode (struct dw_i2c_dev *dev,int mode);#if IS_ENABLED (CONFIG_I2C_DESIGNWARE_BAYTRAIL )int i2c_dw_baytrail_probe_lock_support (struct dw_i2c_dev *dev);
drivers/i2c/busses/i2c-designware-master.c +5 -1
@@ -194,6 +194,8 @@ static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)/* Disable the adapter */ __i2c_dw_disable (dev); +i2c_dw_set_mode (dev,DW_IC_MASTER ); +/* If the slave address is ten bit address, enable 10BITADDR */ if (msgs[dev->msg_write_idx].flags &I2C_M_TEN ) { ic_con =DW_IC_CON_10BITADDR_MASTER ;
@@ -831,6 +833,8 @@ i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num) ret = -EIO ; done: +i2c_dw_set_mode (dev,DW_IC_SLAVE ); +i2c_dw_release_lock (dev); done_nolock:
@@ -853,7 +857,7 @@ void i2c_dw_configure_master(struct dw_i2c_dev *dev) {struct i2c_timings *t = &dev->timings; - dev->functionality =I2C_FUNC_10BIT_ADDR |DW_IC_DEFAULT_FUNCTIONALITY ; + dev->functionality |=I2C_FUNC_10BIT_ADDR |DW_IC_DEFAULT_FUNCTIONALITY ; dev->master_cfg =DW_IC_CON_MASTER |DW_IC_CON_SLAVE_DISABLE |DW_IC_CON_RESTART_EN ;
drivers/i2c/busses/i2c-designware-slave.c +15 -20
@@ -24,24 +24,25 @@int i2c_dw_reg_slave (struct i2c_client *slave) {struct dw_i2c_dev *dev =i2c_get_adapdata (slave->adapter); +int ret; +if (!i2c_check_functionality (slave->adapter,I2C_FUNC_SLAVE )) +return -EOPNOTSUPP ;if (dev->slave)return -EBUSY ;if (slave->flags &I2C_CLIENT_TEN )return -EAFNOSUPPORT ; -pm_runtime_get_sync (dev->dev); -/* -* Set slave address in the IC_SAR register, -* the address to which the DW_apb_i2c responds. -*/ + ret =i2c_dw_acquire_lock (dev); +if (ret) +return ret; + +pm_runtime_get_sync (dev->dev);__i2c_dw_disable_nowait (dev); -regmap_write (dev->map,DW_IC_SAR , slave->addr); dev->slave = slave; +i2c_dw_set_mode (dev,DW_IC_SLAVE ); -__i2c_dw_enable (dev); - - dev->status =0 ; +i2c_dw_release_lock (dev);return 0 ; }
@@ -54,6 +55,7 @@ int i2c_dw_unreg_slave(struct i2c_client *slave)i2c_dw_disable (dev);synchronize_irq (dev->irq); dev->slave =NULL ; +i2c_dw_set_mode (dev,DW_IC_MASTER );pm_runtime_put_sync_suspend (dev->dev);return 0 ;
@@ -176,23 +178,16 @@ irqreturn_t i2c_dw_isr_slave(struct dw_i2c_dev *dev)void i2c_dw_configure_slave (struct dw_i2c_dev *dev) { - dev->functionality =I2C_FUNC_SLAVE ; +if (dev->flags &ACCESS_POLLING ) +return ; + + dev->functionality |=I2C_FUNC_SLAVE ; dev->slave_cfg =DW_IC_CON_RX_FIFO_FULL_HLD_CTRL |DW_IC_CON_RESTART_EN |DW_IC_CON_STOP_DET_IFADDRESSED ; - - dev->mode =DW_IC_SLAVE ; }EXPORT_SYMBOL_GPL (i2c_dw_configure_slave); -int i2c_dw_probe_slave (struct dw_i2c_dev *dev) -{ -if (dev->flags &ACCESS_POLLING ) -return -EOPNOTSUPP ; - -return 0 ; -} -MODULE_AUTHOR ("Luis Oliveira <[email protected]>" );MODULE_DESCRIPTION ("Synopsys DesignWare I2C bus slave adapter" );MODULE_LICENSE ("GPL v2" );
Signature · 1 line
2.50.1
PATCH v2 6/6 i2c: designware: Remove an unnecessary condition
In reply to Heikki KrogerusWriting also the high speed timing registers unconditionally. The reset value for these registers is 0, so this should always be safe.
--- drivers/i2c/busses/i2c-designware-common.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-designware-common.c +3 -5
@@ -391,11 +391,9 @@ static void i2c_dw_write_timings(struct dw_i2c_dev *dev)regmap_write (dev->map,DW_IC_FS_SCL_HCNT , dev->fs_hcnt);regmap_write (dev->map,DW_IC_FS_SCL_LCNT , dev->fs_lcnt); -/* Write high speed timing parameters if supported */ -if (dev->hs_hcnt && dev->hs_lcnt) { -regmap_write (dev->map,DW_IC_HS_SCL_HCNT , dev->hs_hcnt); -regmap_write (dev->map,DW_IC_HS_SCL_LCNT , dev->hs_lcnt); - } +/* Write high speed timing parameters */ +regmap_write (dev->map,DW_IC_HS_SCL_HCNT , dev->hs_hcnt); +regmap_write (dev->map,DW_IC_HS_SCL_LCNT , dev->hs_lcnt); }/**
Signature · 1 line
2.50.1
Re: [PATCH v2 6/6] i2c: designware: Remove an unnecessary condition
Quoted 3 lines · Heikki Krogerus
Writing also the high speed timing registers unconditionally. The reset value for these registers is 0, so this should always be safe.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 23 lines · Heikki Krogerus
Hi,
Changed since v1:
- The init cb is replaced withi2c_dw_init()also ini2c-designware-amdisp.c
- CheckingI2C_FUNC_SLAVEini2c_dw_reg_slave()The original cover letter:
It's currently not possible to support MCTP or any other protocol that requires support for both modes with the DesignWare I2C because the driver can only be used in one mode. I'm assuming that the driver was designed this way because the DesignWare I2C can not be operated as I2C master and I2C slave simultaneously, however, that does not actually mean both modes could not be supported at the same time. See the patch 5/6 for more detailed explanation.
This series will enable support for both modes in the driver by utilising a simple mode swap method, and that way make it possible to support MCTP, IPMI, SMBus Host Notification Protocol, and any other protocol requires the support for both modes with the DesignWare I2C.
thanks,
Quoted 7 lines
Heikki Krogerus (6):
i2c: designware: Remove useless driver specific option for I2C target
i2c: designware: Remove unnecessary function exports
i2c: designware: Combine some of the common functions
i2c: designware: Combine the init functions
i2c: designware: Enable mode swapping
i2c: designware: Remove an unnecessary condition
For the whole series,
Andi Shyti
maintainerHi Heikki,
Quoted 3 lines
Heikki Krogerus (6):
i2c: designware: Remove useless driver specific option for I2C target
i2c: designware: Remove unnecessary function exports
to unburden you a little, I applied the first two patches.
Andi