linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] WIP: i2c slave support
@ 2014-09-02 18:02 Wolfram Sang
       [not found] ` <1409680961-12632-1-git-send-email-wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2014-09-02 18:02 UTC (permalink / raw)
  To: linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: linux-sh-u79uwXL29TY76Z2rM5mHXA, Wolfram Sang

From: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>

I am currently working on the long standing issue of slave support for the I2C
framework. Here is a very rough first draft, so you can see what path I chose.
Three big things are still missing: documentation, a driver supporting the
hooks created here, and testing. So, this is more a draft how the interface
could look like and will soon get the details when I implement the support for
the i2c-rcar driver. If you want to comment, that is appreciated. However,
please focus on high-level comments, not implementation details. There is a lot
of sloppiness in the details, I do know. If they are still present in the first
real patches, then you can complain ;)

Since docs are still missing, here is a rough description: A software slave
driver is a standard I2C driver which will be matched against a standard I2C
client. The exception is that it requests a slave instance from its adapter via
i2c_slave_request() in probe. It provides a callback which will be invoked in
case events happen for this slave.

And again: This is a draft, up to now compile tested only.

Wolfram Sang (3):
  WIP: i2c core changes for slave support
  WIP: sketch of an slave-eeprom simulator
  WIP: adapt dts for i2c slave use

 arch/arm/boot/dts/r8a7790-lager.dts |  15 ++--
 drivers/i2c/i2c-core.c              |  42 +++++++++++
 drivers/i2c/i2c-slave-eeprom.c      | 140 ++++++++++++++++++++++++++++++++++++
 include/linux/i2c-slave.h           |  28 ++++++++
 include/linux/i2c.h                 |   5 ++
 5 files changed, 225 insertions(+), 5 deletions(-)
 create mode 100644 drivers/i2c/i2c-slave-eeprom.c
 create mode 100644 include/linux/i2c-slave.h

-- 
2.0.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] WIP: i2c core changes for slave support
       [not found] ` <1409680961-12632-1-git-send-email-wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>
@ 2014-09-02 18:02   ` Wolfram Sang
  2014-09-02 18:02   ` [PATCH 2/3] WIP: sketch of an slave-eeprom simulator Wolfram Sang
  2014-09-02 18:02   ` [PATCH 3/3] WIP: adapt dts for i2c slave use Wolfram Sang
  2 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2014-09-02 18:02 UTC (permalink / raw)
  To: linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: linux-sh-u79uwXL29TY76Z2rM5mHXA, Wolfram Sang

From: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>

Not for upstream yet.

Signed-off-by: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>
---
 drivers/i2c/i2c-core.c    | 42 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c-slave.h | 28 ++++++++++++++++++++++++++++
 include/linux/i2c.h       |  5 +++++
 3 files changed, 75 insertions(+)
 create mode 100644 include/linux/i2c-slave.h

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 632057a44615..63ea70e5c274 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -2536,6 +2536,48 @@ trace:
 }
 EXPORT_SYMBOL(i2c_smbus_xfer);
 
+int i2c_slave_register(struct i2c_client *client,
+		       int (*slave_cb)(struct i2c_client *, enum i2c_slave_event, u8 *))
+{
+	u16 addr = client->addr;
+	int ret;
+
+	if (!slave_cb)
+		return -EINVAL;
+
+	ret = i2c_check_addr_validity(addr);
+	if (ret)
+		return ret;
+
+	if (!client->adapter->algo->reg_slave)
+		return -EOPNOTSUPP;
+
+	client->slave_cb = slave_cb;
+
+	ret = client->adapter->algo->reg_slave(client);
+	if (ret)
+		client->slave_cb = NULL;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(i2c_slave_register);
+
+int i2c_slave_unregister(struct i2c_client *client)
+{
+	int ret;
+
+	if (!client->adapter->algo->unreg_slave)
+		return -EOPNOTSUPP;
+
+	ret = client->adapter->algo->unreg_slave(client);
+
+	if (ret == 0)
+		client->slave_cb = NULL;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(i2c_slave_unregister);
+
 MODULE_AUTHOR("Simon G. Vogl <simon-nD9nYVNVf00W+b/DJNNodF6hYfS7NtTn@public.gmane.org>");
 MODULE_DESCRIPTION("I2C-Bus main module");
 MODULE_LICENSE("GPL");
diff --git a/include/linux/i2c-slave.h b/include/linux/i2c-slave.h
new file mode 100644
index 000000000000..ddc9493cb5e7
--- /dev/null
+++ b/include/linux/i2c-slave.h
@@ -0,0 +1,28 @@
+/*
+ * i2c-slave.h - Linux I2C slave support
+ *
+ * Copyright (C) 2014 by Wolfram Sang
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; version 2 of the License.
+ */
+
+#include <linux/i2c.h>
+
+#ifndef _LINUX_I2C_SLAVE_H
+#define _LINUX_I2C_SLAVE_H
+
+enum i2c_slave_event {
+	I2C_SLAVE_REQ_READ,
+	I2C_SLAVE_REQ_WRITE,
+	I2C_SLAVE_STOP,
+};
+
+extern int i2c_slave_register(struct i2c_client *client, int (*slave_cb)(struct i2c_client *, enum i2c_slave_event, u8 *));
+extern int i2c_slave_unregister(struct i2c_client *client);
+
+#define i2c_slave_event(__client, __event, __value) \
+	client->slave_cb(__client, __event, __value)
+
+#endif
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index a95efeb53a8b..8541aa6b0178 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -46,6 +46,7 @@ struct i2c_client;
 struct i2c_driver;
 union i2c_smbus_data;
 struct i2c_board_info;
+enum i2c_slave_event;
 
 struct module;
 
@@ -224,6 +225,7 @@ struct i2c_client {
 	struct device dev;		/* the device structure		*/
 	int irq;			/* irq issued by device		*/
 	struct list_head detected;
+	int (*slave_cb)(struct i2c_client *, enum i2c_slave_event, u8 *);
 };
 #define to_i2c_client(d) container_of(d, struct i2c_client, dev)
 
@@ -377,6 +379,9 @@ struct i2c_algorithm {
 
 	/* To determine what the adapter supports */
 	u32 (*functionality) (struct i2c_adapter *);
+
+	int (*reg_slave)(struct i2c_client *client);
+	int (*unreg_slave)(struct i2c_client *client);
 };
 
 /**
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] WIP: sketch of an slave-eeprom simulator
       [not found] ` <1409680961-12632-1-git-send-email-wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>
  2014-09-02 18:02   ` [PATCH 1/3] WIP: i2c core changes for " Wolfram Sang
@ 2014-09-02 18:02   ` Wolfram Sang
  2014-09-02 18:02   ` [PATCH 3/3] WIP: adapt dts for i2c slave use Wolfram Sang
  2 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2014-09-02 18:02 UTC (permalink / raw)
  To: linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: linux-sh-u79uwXL29TY76Z2rM5mHXA, Wolfram Sang

From: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>

Not for upstream yet.

Signed-off-by: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>
---
 drivers/i2c/i2c-slave-eeprom.c | 140 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 140 insertions(+)
 create mode 100644 drivers/i2c/i2c-slave-eeprom.c

diff --git a/drivers/i2c/i2c-slave-eeprom.c b/drivers/i2c/i2c-slave-eeprom.c
new file mode 100644
index 000000000000..f12edd316ad5
--- /dev/null
+++ b/drivers/i2c/i2c-slave-eeprom.c
@@ -0,0 +1,140 @@
+/*
+ * I2C Slave mode EEPROM simulator
+ *
+ * Copyright (C) 2014 by Wolfram Sang
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; version 2 of the License.
+ */
+
+#include <linux/i2c.h>
+#include <linux/i2c-slave.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+
+#define EEPROM_SIZE 256
+
+struct i2c_slave_eeprom_data {
+	struct mutex lock;
+	struct bin_attribute bin;
+	u8 buffer[EEPROM_SIZE];
+	u8 eeprom_ptr;
+	bool first_write_done;
+};
+
+static int at24s_slave_cb(struct i2c_client *client, enum i2c_slave_event event, u8 *val)
+{
+	struct i2c_slave_eeprom_data *at24s = i2c_get_clientdata(client);
+
+	switch (event) {
+	case I2C_SLAVE_REQ_WRITE:
+		if (!at24s->first_write_done)
+			at24s->eeprom_ptr = *val;
+		else
+			at24s->buffer[at24s->eeprom_ptr++] = *val;
+
+		at24s->first_write_done = true;
+		break;
+
+	case I2C_SLAVE_REQ_READ:
+		*val = at24s->buffer[at24s->eeprom_ptr++];
+		break;
+
+	case I2C_SLAVE_STOP:
+		at24s->first_write_done = false;
+		break;
+	}
+
+	return 0;
+}
+
+static ssize_t at24s_bin_read(struct file *filp, struct kobject *kobj,
+		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
+{
+	struct i2c_slave_eeprom_data *at24s;
+
+	at24s = dev_get_drvdata(container_of(kobj, struct device, kobj));
+
+	memcpy(buf, &at24s->buffer[off], count);
+
+	return count;
+}
+
+static ssize_t at24s_bin_write(struct file *filp, struct kobject *kobj,
+		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
+{
+	struct i2c_slave_eeprom_data *at24s;
+
+	if (unlikely(off >= attr->size))
+		return -EFBIG;
+
+	at24s = dev_get_drvdata(container_of(kobj, struct device, kobj));
+
+	memcpy(&at24s->buffer[off], buf, count);
+
+	return count;
+}
+
+static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
+{
+	struct i2c_slave_eeprom_data *at24s;
+	int ret;
+
+	at24s = devm_kzalloc(&client->dev, sizeof(struct i2c_slave_eeprom_data), GFP_KERNEL);
+	if (!at24s)
+		return -ENOMEM;
+
+	i2c_set_clientdata(client, at24s);
+
+	ret = i2c_slave_register(client, at24s_slave_cb);
+	if (ret)
+		return ret;
+
+	sysfs_bin_attr_init(&at24s->bin);
+	at24s->bin.attr.name = "slave-eeprom";
+	at24s->bin.attr.mode = S_IRUSR | S_IWUSR;
+	at24s->bin.read = at24s_bin_read;
+	at24s->bin.write = at24s_bin_write;
+	at24s->bin.size = EEPROM_SIZE;
+
+	ret = sysfs_create_bin_file(&client->dev.kobj, &at24s->bin);
+	if (ret)
+		return ret;
+
+	return 0;
+};
+
+static int i2c_slave_eeprom_remove(struct i2c_client *client)
+{
+	struct i2c_slave_eeprom_data *at24s;
+
+	at24s = i2c_get_clientdata(client);
+	sysfs_remove_bin_file(&client->dev.kobj, &at24s->bin);
+
+	return i2c_slave_unregister(client);
+}
+
+static const struct i2c_device_id i2c_slave_eeprom_id[] = {
+	{ "i2c-slave-eeprom", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
+
+static struct i2c_driver i2c_slave_eeprom_driver = {
+	.driver = {
+		.name = "i2c-slave-eeprom",
+		.owner = THIS_MODULE,
+	},
+	.probe = i2c_slave_eeprom_probe,
+	.remove = i2c_slave_eeprom_remove,
+	.id_table = i2c_slave_eeprom_id,
+};
+module_i2c_driver(i2c_slave_eeprom_driver);
+
+MODULE_AUTHOR("Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>");
+MODULE_DESCRIPTION("I2C Slave mode EEPROM simulator");
+MODULE_LICENSE("GPL v2");
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] WIP: adapt dts for i2c slave use
       [not found] ` <1409680961-12632-1-git-send-email-wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>
  2014-09-02 18:02   ` [PATCH 1/3] WIP: i2c core changes for " Wolfram Sang
  2014-09-02 18:02   ` [PATCH 2/3] WIP: sketch of an slave-eeprom simulator Wolfram Sang
@ 2014-09-02 18:02   ` Wolfram Sang
  2 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2014-09-02 18:02 UTC (permalink / raw)
  To: linux-i2c-u79uwXL29TY76Z2rM5mHXA
  Cc: linux-sh-u79uwXL29TY76Z2rM5mHXA, Wolfram Sang

From: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>

not for upstream yet.

Signed-off-by: Wolfram Sang <wsa+renesas-jBu1N2QxHDJrcw3mvpCnnVaTQe2KTcn/@public.gmane.org>
---
 arch/arm/boot/dts/r8a7790-lager.dts | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/arch/arm/boot/dts/r8a7790-lager.dts b/arch/arm/boot/dts/r8a7790-lager.dts
index 0118cbfd1cb3..93ff6ce357bb 100644
--- a/arch/arm/boot/dts/r8a7790-lager.dts
+++ b/arch/arm/boot/dts/r8a7790-lager.dts
@@ -205,9 +205,9 @@
 		renesas,function = "msiof1";
 	};
 
-	iic1_pins: iic1 {
-		renesas,groups = "iic1";
-		renesas,function = "iic1";
+	i2c1_pins: i2c1 {
+		renesas,groups = "i2c1";
+		renesas,function = "i2c1";
 	};
 
 	iic2_pins: iic2 {
@@ -365,10 +365,15 @@
 	status = "ok";
 };
 
-&iic1	{
+&i2c1	{
 	status = "ok";
-	pinctrl-0 = <&iic1_pins>;
+	pinctrl-0 = <&i2c1_pins>;
 	pinctrl-names = "default";
+
+	eeprom@64 {
+		compatible = "i2c-slave-eeprom";
+		reg = <0x64>;
+	};
 };
 
 &iic2	{
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-09-02 18:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-02 18:02 [PATCH 0/3] WIP: i2c slave support Wolfram Sang
     [not found] ` <1409680961-12632-1-git-send-email-wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>
2014-09-02 18:02   ` [PATCH 1/3] WIP: i2c core changes for " Wolfram Sang
2014-09-02 18:02   ` [PATCH 2/3] WIP: sketch of an slave-eeprom simulator Wolfram Sang
2014-09-02 18:02   ` [PATCH 3/3] WIP: adapt dts for i2c slave use Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).