[U-Boot,v2,1/6] i2c: add slave mode support
diff mbox

Message ID 1398561270-25091-2-git-send-email-danindrey@mail.ru
State Changes Requested
Headers show

Commit Message

Andrey Danin April 27, 2014, 1:14 a.m. UTC
Signed-off-by: Andrey Danin <danindrey@mail.ru>
CC: Stephen Warren <swarren@nvidia.com>
CC: Marc Dietrich <marvin24@gmx.de>
CC: Julian Andres Klode <jak@jak-linux.org>
CC: ac100@lists.launchpad.net
---
 drivers/i2c/i2c_core.c |   13 +++++++++++++
 include/i2c.h          |   30 +++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

Comments

Stephen Warren April 28, 2014, 11:06 p.m. UTC | #1
On 04/26/2014 07:14 PM, Andrey Danin wrote:
> Signed-off-by: Andrey Danin <danindrey@mail.ru>
> CC: Stephen Warren <swarren@nvidia.com>
> CC: Marc Dietrich <marvin24@gmx.de>
> CC: Julian Andres Klode <jak@jak-linux.org>
> CC: ac100@lists.launchpad.net

Don't you want to CC the I2C maintainer, Heiko Schocher <hs@denx.de>, on
this series?
Heiko Schocher April 29, 2014, 5:30 a.m. UTC | #2
Hello Andrey,

Am 27.04.2014 03:14, schrieb Andrey Danin:
> Signed-off-by: Andrey Danin<danindrey@mail.ru>
> CC: Stephen Warren<swarren@nvidia.com>
> CC: Marc Dietrich<marvin24@gmx.de>
> CC: Julian Andres Klode<jak@jak-linux.org>
> CC: ac100@lists.launchpad.net
> ---
>   drivers/i2c/i2c_core.c |   13 +++++++++++++
>   include/i2c.h          |   30 +++++++++++++++++++++++++++++-
>   2 files changed, 42 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
> index 18d6736..105aa0a 100644
> --- a/drivers/i2c/i2c_core.c
> +++ b/drivers/i2c/i2c_core.c
> @@ -395,6 +395,19 @@ void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
>   	i2c_write(addr, reg, 1,&val, 1);
>   }
>
> +int i2c_slave_io(struct i2c_transaction *trans)
> +{
> +	struct i2c_adapter *cur = I2C_ADAP;
> +
> +	if (!cur->slave_io) {
> +		printf("Error: slave IO is not supported on adap %d\n",
> +		       cur->hwadapnr);
> +		return -1;
> +	}
> +
> +	return cur->slave_io(cur, trans);
> +}
> +
>   void __i2c_init(int speed, int slaveaddr)
>   {
>   	i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
> diff --git a/include/i2c.h b/include/i2c.h
> index f93a183..165b919 100644
> --- a/include/i2c.h
> +++ b/include/i2c.h
> @@ -55,6 +55,20 @@
>   #define CONFIG_SYS_SPD_BUS_NUM		0
>   #endif
>
> +struct i2c_transaction {
> +	char rx_buf[34];
> +	int rx_pos;
> +
> +	char tx_buf[34];
> +	int tx_pos;
> +	int tx_size;
> +
> +	unsigned int start_timeout;
> +	unsigned int timeout;
> +
> +	int res;
> +};
> +
>   struct i2c_adapter {
>   	void		(*init)(struct i2c_adapter *adap, int speed,
>   				int slaveaddr);
> @@ -65,6 +79,8 @@ struct i2c_adapter {
>   	int		(*write)(struct i2c_adapter *adap, uint8_t chip,
>   				uint addr, int alen, uint8_t *buffer,
>   				int len);
> +	int		(*slave_io)(struct i2c_adapter *adap,
> +				struct i2c_transaction *trans);
>   	uint		(*set_bus_speed)(struct i2c_adapter *adap,
>   				uint speed);
>   	int		speed;
> @@ -81,12 +97,13 @@ struct i2c_adapter {
>   		.probe		=	_probe, \
>   		.read		=	_read, \
>   		.write		=	_write, \
> +		.slave_io	=	0, \
>   		.set_bus_speed	=	_set_speed, \
>   		.speed		=	_speed, \
>   		.slaveaddr	=	_slaveaddr, \
>   		.init_done	=	0, \
>   		.hwadapnr	=	_hwadapnr, \
> -		.name		=	#_name \
> +		.name		=	#_name, \
>   };
>
>   #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
> @@ -450,4 +467,15 @@ int i2c_get_bus_num_fdt(int node);
>    * @return 0 if port was reset, -1 if not found
>    */
>   int i2c_reset_port_fdt(const void *blob, int node);
> +
> +/**
> + * Perform I2C transaction with master device.
> + *
> + * @param trans  I2C transaction object
> + * @return 0 if succeeded, -1 if not supported,
> + *         1 if not ready, 2 if operation timed out,
> + *         3 if not our packet, other - unknown error.
> + */
> +int i2c_slave_io(struct i2c_transaction *trans);
> +
>   #endif	/* _I2C_H_ */

Hmm, why you use positiv error codes? Can we use negativ codes here
please? And maybe define somewhere this values as defines? Or can
you use:

include/asm-generic/errno.h

Beside of that, it looks Ok to me.

bye,
Heiko
Andrey Danin April 30, 2014, 5:31 a.m. UTC | #3
On 29.04.2014 3:06, Stephen Warren wrote:
> On 04/26/2014 07:14 PM, Andrey Danin wrote:
>> Signed-off-by: Andrey Danin <danindrey@mail.ru>
>> CC: Stephen Warren <swarren@nvidia.com>
>> CC: Marc Dietrich <marvin24@gmx.de>
>> CC: Julian Andres Klode <jak@jak-linux.org>
>> CC: ac100@lists.launchpad.net
>
> Don't you want to CC the I2C maintainer, Heiko Schocher <hs@denx.de>, on
> this series?
>
Sorry, I forgot to add him. I will add in v3.

Patch
diff mbox

diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
index 18d6736..105aa0a 100644
--- a/drivers/i2c/i2c_core.c
+++ b/drivers/i2c/i2c_core.c
@@ -395,6 +395,19 @@  void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val)
 	i2c_write(addr, reg, 1, &val, 1);
 }
 
+int i2c_slave_io(struct i2c_transaction *trans)
+{
+	struct i2c_adapter *cur = I2C_ADAP;
+
+	if (!cur->slave_io) {
+		printf("Error: slave IO is not supported on adap %d\n",
+		       cur->hwadapnr);
+		return -1;
+	}
+
+	return cur->slave_io(cur, trans);
+}
+
 void __i2c_init(int speed, int slaveaddr)
 {
 	i2c_init_bus(i2c_get_bus_num(), speed, slaveaddr);
diff --git a/include/i2c.h b/include/i2c.h
index f93a183..165b919 100644
--- a/include/i2c.h
+++ b/include/i2c.h
@@ -55,6 +55,20 @@ 
 #define CONFIG_SYS_SPD_BUS_NUM		0
 #endif
 
+struct i2c_transaction {
+	char rx_buf[34];
+	int rx_pos;
+
+	char tx_buf[34];
+	int tx_pos;
+	int tx_size;
+
+	unsigned int start_timeout;
+	unsigned int timeout;
+
+	int res;
+};
+
 struct i2c_adapter {
 	void		(*init)(struct i2c_adapter *adap, int speed,
 				int slaveaddr);
@@ -65,6 +79,8 @@  struct i2c_adapter {
 	int		(*write)(struct i2c_adapter *adap, uint8_t chip,
 				uint addr, int alen, uint8_t *buffer,
 				int len);
+	int		(*slave_io)(struct i2c_adapter *adap,
+				struct i2c_transaction *trans);
 	uint		(*set_bus_speed)(struct i2c_adapter *adap,
 				uint speed);
 	int		speed;
@@ -81,12 +97,13 @@  struct i2c_adapter {
 		.probe		=	_probe, \
 		.read		=	_read, \
 		.write		=	_write, \
+		.slave_io	=	0, \
 		.set_bus_speed	=	_set_speed, \
 		.speed		=	_speed, \
 		.slaveaddr	=	_slaveaddr, \
 		.init_done	=	0, \
 		.hwadapnr	=	_hwadapnr, \
-		.name		=	#_name \
+		.name		=	#_name, \
 };
 
 #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
@@ -450,4 +467,15 @@  int i2c_get_bus_num_fdt(int node);
  * @return 0 if port was reset, -1 if not found
  */
 int i2c_reset_port_fdt(const void *blob, int node);
+
+/**
+ * Perform I2C transaction with master device.
+ *
+ * @param trans  I2C transaction object
+ * @return 0 if succeeded, -1 if not supported,
+ *         1 if not ready, 2 if operation timed out,
+ *         3 if not our packet, other - unknown error.
+ */
+int i2c_slave_io(struct i2c_transaction *trans);
+
 #endif	/* _I2C_H_ */