mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] i2c-imx: change log level for No ACK
@ 2010-05-24 11:55 Eric Bénard
  2010-05-24 11:55 ` [PATCH 2/3] commands: add i2c commands Eric Bénard
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Bénard @ 2010-05-24 11:55 UTC (permalink / raw)
  To: s.hauer; +Cc: barebox

Signed-off-by: Eric Bénard <eric@eukrea.com>
---
 drivers/i2c/i2c-imx.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/drivers/i2c/i2c-imx.c b/drivers/i2c/i2c-imx.c
index 03e3785..cc64d94 100644
--- a/drivers/i2c/i2c-imx.c
+++ b/drivers/i2c/i2c-imx.c
@@ -214,7 +214,7 @@ static int i2c_imx_acked(struct i2c_adapter *adapter)
 			break;
 
 		if (is_timeout(start, MSECOND)) {
-			dev_err(adapter->dev, "<%s> No ACK\n", __func__);
+			dev_dbg(adapter->dev, "<%s> No ACK\n", __func__);
 			return -EIO;
 		}
 	}
-- 
1.6.3.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* [PATCH 2/3] commands: add i2c commands
  2010-05-24 11:55 [PATCH 1/3] i2c-imx: change log level for No ACK Eric Bénard
@ 2010-05-24 11:55 ` Eric Bénard
  2010-05-24 11:55   ` [PATCH 3/3] i2c: implement i2c_get_adapter() Eric Bénard
  2010-05-24 12:10   ` [PATCH 2/3] commands: add i2c commands Eric Bénard
  0 siblings, 2 replies; 4+ messages in thread
From: Eric Bénard @ 2010-05-24 11:55 UTC (permalink / raw)
  To: s.hauer; +Cc: barebox

theses commands allow low level access to i2c bus and can be useful
to setup an i2c device without having to add code, compile and flash
barebox.

Signed-off-by: Eric Bénard <eric@eukrea.com>
---
 commands/Kconfig  |    8 ++
 commands/Makefile |    1 +
 commands/i2c.c    |  218 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100644 commands/i2c.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 0c09f91..0ea32d9 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -314,4 +314,12 @@ config CMD_UNLZO
 	  Say yes here to get the unlzo command. lzo is a fast compression
 	  algorithm by Markus Franz Xaver Johannes Oberhumer.
 
+config CMD_I2C
+	bool
+	depends on I2C
+	prompt "i2c commands"
+	help
+	  include i2c_probe, i2c_read and i2c_write commands to communicate
+	  on i2c bus.
+
 endmenu
diff --git a/commands/Makefile b/commands/Makefile
index 74b0994..3eef5de 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -48,3 +48,4 @@ obj-$(CONFIG_CMD_BMP)		+= bmp.o
 obj-$(CONFIG_USB_GADGET_DFU)	+= dfu.o
 obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNLZO)		+= unlzo.o
+obj-$(CONFIG_CMD_I2C)		+= i2c.o
diff --git a/commands/i2c.c b/commands/i2c.c
new file mode 100644
index 0000000..cd9ee53
--- /dev/null
+++ b/commands/i2c.c
@@ -0,0 +1,218 @@
+/*
+ * i2c.c - i2c commands
+ *
+ * Copyright (c) 2010 Eric Bénard <eric@eukrea.Com>, Eukréa Electromatique
+ *
+ * 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; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <i2c/i2c.h>
+
+struct i2c_client	*client;
+
+static int do_i2c_probe(struct command *cmdtp, int argc, char *argv[])
+{
+	struct i2c_adapter *adapter;
+	struct i2c_client client;
+	int startaddr = -1, stopaddr = -1, addr, ret;
+	u8 reg;
+
+	if (argc < 4)
+		return COMMAND_ERROR_USAGE;
+
+	adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
+	if (!adapter)
+		return -ENODEV;
+	client.adapter = adapter;
+
+	startaddr = simple_strtol(argv[2], NULL, 16);
+	stopaddr = simple_strtol(argv[3], NULL, 16);
+	if ((startaddr == -1) || (stopaddr == -1) || (startaddr > stopaddr))
+		return COMMAND_ERROR_USAGE;
+
+	if (stopaddr > 0x7F)
+		stopaddr = 0x7F;
+
+	printf("probing i2c range 0X%02x - 0x%02x :\n", startaddr, stopaddr);
+	for (addr = startaddr; addr <= stopaddr; addr++) {
+		client.addr = addr;
+		ret = i2c_write_reg(&client, 0x00, &reg, 0);
+		if (ret == 0)
+			printf("0x%02x ", addr);
+	}
+	printf("\n");
+	return 0;
+}
+
+static const __maybe_unused char cmd_i2c_probe_help[] =
+"Usage: i2c_probe bus 0xstartaddr 0xstopaddr\n"
+"probe a range of i2c addresses.\n";
+
+BAREBOX_CMD_START(i2c_probe)
+	.cmd		= do_i2c_probe,
+	.usage		= "probe for an i2c device",
+	BAREBOX_CMD_HELP(cmd_i2c_probe_help)
+BAREBOX_CMD_END
+
+static int do_i2c_write(struct command *cmdtp, int argc, char *argv[])
+{
+	struct i2c_adapter *adapter;
+	struct i2c_client client;
+	int addr = -1, reg = -1, count = -1, verbose = 0, ret, opt;
+	u8 *buf;
+
+	while ((opt = getopt(argc, argv, "a:br:v")) > 0) {
+		switch (opt) {
+		case 'a':
+			addr = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'r':
+			reg = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'b':
+			adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
+			if (!adapter)
+				return -ENODEV;
+			client.adapter = adapter;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	count = argc - optind;
+
+	if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
+		return COMMAND_ERROR_USAGE;
+
+	client.addr = addr;
+
+	buf = malloc(count);
+	if (buf) {
+		int i;
+		u8 tmp;
+		for (i = 0; i < count; i++) {
+			tmp = simple_strtol((char *) (optind+i), NULL, 16);
+			if (tmp < 0) {
+				free(buf);
+				return COMMAND_ERROR_USAGE;
+			}
+			*(buf + i) = tmp;
+		}
+		ret = i2c_write_reg(&client, reg, buf, count);
+		if (ret != count) {
+			free(buf);
+			return ret;
+		}
+		free(buf);
+		if (verbose)
+			printf("wrote %i bytes starting at reg 0x%02x to i2cdev 0x%02x\n",
+				count, reg, addr);
+		return 0;
+	}
+	return 1;
+}
+
+static const __maybe_unused char cmd_i2c_write_help[] =
+"Usage: i2c_write [OPTION] ... hexdatas\n"
+"write to i2c device.\n"
+"  -a 0x<addr>   i2c device address\n"
+"  -b <bus_num>  i2c bus number\n"
+"  -r 0x<reg>    start register\n";
+
+BAREBOX_CMD_START(i2c_write)
+	.cmd		= do_i2c_write,
+	.usage		= "write to an i2c device",
+	BAREBOX_CMD_HELP(cmd_i2c_write_help)
+BAREBOX_CMD_END
+
+static int do_i2c_read(struct command *cmdtp, int argc, char *argv[])
+{
+	struct i2c_adapter *adapter;
+	struct i2c_client client;
+	u8 *buf;
+	int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt;
+
+	while ((opt = getopt(argc, argv, "a:c:r:")) > 0) {
+		switch (opt) {
+		case 'a':
+			addr = simple_strtol(optarg, NULL, 16);
+			if (addr < 0)
+				return COMMAND_ERROR_USAGE;
+			break;
+		case 'c':
+			count = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'b':
+			adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
+			if (!adapter)
+				return -ENODEV;
+			client.adapter = adapter;
+			break;
+		case 'r':
+			reg = simple_strtol(optarg, NULL, 16);
+			if (reg < 0)
+				return COMMAND_ERROR_USAGE;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
+		return COMMAND_ERROR_USAGE;
+
+	client.addr = addr;
+
+	buf = malloc(count);
+	if (buf) {
+		ret = i2c_read_reg(&client, reg, buf, count);
+		if (ret == count) {
+			int i;
+			if (verbose)
+				printf("read %i bytes starting at reg 0x%02x from i2cdev 0x%02x\n",
+					count, reg, addr);
+			for (i = 0; i < count; i++)
+				printf("0x%02x ", *(buf + i));
+			printf("\n");
+			free(buf);
+			return 0;
+		}
+		free(buf);
+	}
+	return 1;
+}
+
+static const __maybe_unused char cmd_i2c_read_help[] =
+"Usage: i2c_read [OPTION]\n"
+"read i2c device.\n"
+"  -a 0x<addr>   i2c device address\n"
+"  -b <bus_num>  i2c bus number\n"
+"  -r 0x<reg>    start register\n"
+"  -c <count>    byte count\n";
+
+BAREBOX_CMD_START(i2c_read)
+	.cmd		= do_i2c_read,
+	.usage		= "read from an i2c device",
+	BAREBOX_CMD_HELP(cmd_i2c_read_help)
+BAREBOX_CMD_END
-- 
1.6.3.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* [PATCH 3/3] i2c: implement i2c_get_adapter()
  2010-05-24 11:55 ` [PATCH 2/3] commands: add i2c commands Eric Bénard
@ 2010-05-24 11:55   ` Eric Bénard
  2010-05-24 12:10   ` [PATCH 2/3] commands: add i2c commands Eric Bénard
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Bénard @ 2010-05-24 11:55 UTC (permalink / raw)
  To: s.hauer; +Cc: barebox

From: Sascha Hauer <s.hauer@pengutronix.de>

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/i2c/i2c.c |   24 +++++++++++++++++++++++-
 include/i2c/i2c.h |    1 +
 2 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index 32fd026..1fa114a 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -39,7 +39,7 @@ struct boardinfo {
 };
 
 static LIST_HEAD(board_list);
-
+static LIST_HEAD(adapter_list);
 
 /**
  * i2c_transfer - execute a single or combined I2C message
@@ -327,6 +327,23 @@ static void scan_boardinfo(struct i2c_adapter *adapter)
 }
 
 /**
+ *
+ * i2c_get_adapter - get an i2c adapter from its busnum
+ *
+ * @param	busnum	the desired bus number
+ *
+ */
+struct i2c_adapter *i2c_get_adapter(int busnum)
+{
+	struct i2c_adapter *adap;
+
+	list_for_each_entry(adap, &adapter_list, list)
+		if (adap->nr == busnum)
+			return adap;
+	return NULL;
+}
+
+/**
  * i2c_register_master - register I2C master controller
  *
  * @param	master	initialized master, originally from i2c_alloc_master()
@@ -345,6 +362,11 @@ static void scan_boardinfo(struct i2c_adapter *adapter)
  */
 int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
 {
+	if (i2c_get_adapter(adapter->nr))
+		return -EBUSY;
+
+	list_add_tail(&adapter_list, &adapter->list);
+
 	/* populate children from any i2c device tables */
 	scan_boardinfo(adapter);
 
diff --git a/include/i2c/i2c.h b/include/i2c/i2c.h
index 0760fdc..802f553 100644
--- a/include/i2c/i2c.h
+++ b/include/i2c/i2c.h
@@ -66,6 +66,7 @@ struct i2c_adapter {
 	struct device_d		*dev;	/* ptr to device */
 	int			nr;	/* bus number */
 	int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
+	struct list_head	list;
 };
 
 
-- 
1.6.3.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* Re: [PATCH 2/3] commands: add i2c commands
  2010-05-24 11:55 ` [PATCH 2/3] commands: add i2c commands Eric Bénard
  2010-05-24 11:55   ` [PATCH 3/3] i2c: implement i2c_get_adapter() Eric Bénard
@ 2010-05-24 12:10   ` Eric Bénard
  1 sibling, 0 replies; 4+ messages in thread
From: Eric Bénard @ 2010-05-24 12:10 UTC (permalink / raw)
  To: barebox

Le 24/05/2010 13:55, Eric Bénard a écrit :
> theses commands allow low level access to i2c bus and can be useful
> to setup an i2c device without having to add code, compile and flash
> barebox.
>
> Signed-off-by: Eric Bénard<eric@eukrea.com>

forget it, v3 will be the right one !

Eric

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

end of thread, other threads:[~2010-05-24 12:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-24 11:55 [PATCH 1/3] i2c-imx: change log level for No ACK Eric Bénard
2010-05-24 11:55 ` [PATCH 2/3] commands: add i2c commands Eric Bénard
2010-05-24 11:55   ` [PATCH 3/3] i2c: implement i2c_get_adapter() Eric Bénard
2010-05-24 12:10   ` [PATCH 2/3] commands: add i2c commands Eric Bénard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox