Skip to contentSkip to conversation

i2c-imx: change log level for No ACK

PATCH In review Eric Bénard 3 participants 8 messages May 24, 2010 – May 25, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 i2c-imx: change log level for No ACK

Without this, i2c_probe triggers No ACK log at each address probed without a slave's answer

---
 drivers/i2c/i2c-imx.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/i2c/i2c-imx.c +1 -1
index 03e3785..cc64d94 100644
@@ -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;
 		}
 	}
Signature · 7 lines
1.6.3.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/4 i2c: implement i2c_get_adapter()

From: Sascha Hauer <[email protected]>

---
v2 : add i2c_get_adapter to include/i2c/i2c.h

 drivers/i2c/i2c.c |   24 +++++++++++++++++++++++-
 include/i2c/i2c.h |    2 ++
 2 files changed, 25 insertions(+), 1 deletions(-)
drivers/i2c/i2c.c +23 -1
index 32fd026..1fa114a 100644
@@ -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);
 
include/i2c/i2c.h +2 -0
index 0760fdc..c031bbb 100644
@@ -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;
 };
 
 
@@ -120,6 +121,7 @@ static inline int i2c_register_board_info(int busnum,
 }
 #endif
 extern int i2c_add_numbered_adapter(struct i2c_adapter *adapter);
+struct i2c_adapter *i2c_get_adapter(int busnum);
 
 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
 extern int i2c_master_send(struct i2c_client *client, const char *buf, int count);
Signature · 7 lines
1.6.3.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 3/4 commands: add i2c commands

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.

---
v3 :
enable brain and fix v2
tested on iMX27
v2 :
updated as per Sascha's comments

 commands/Kconfig  |    8 ++
 commands/Makefile |    1 +
 commands/i2c.c    |  218 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100644 commands/i2c.c
commands/Kconfig +8 -0
index 0c09f91..0ea32d9 100644
@@ -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
commands/Makefile +1 -0
index 74b0994..3eef5de 100644
@@ -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
commands/i2c.c new file +218 -0

218 lines not on this page · raw message

Signature · 7 lines
1.6.3.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 4/4 eukrea_cpuimx27: add fb support

---
 board/eukrea_cpuimx27/eukrea_cpuimx27.c |   67 ++++++++++++++++++++++++++++++-
 1 files changed, 66 insertions(+), 1 deletions(-)
board/eukrea_cpuimx27/eukrea_cpuimx27.c +66 -1
index ef57a96..e40c260 100644
@@ -40,6 +40,7 @@
 #include <asm/io.h>
 #include <mach/imx-nand.h>
 #include <mach/imx-pll.h>
+#include <mach/imxfb.h>
 #include <ns16550.h>
 #include <asm/mmu.h>
 #include <i2c/i2c.h>
@@ -176,6 +177,39 @@ static void eukrea_cpuimx27_mmu_init(void)
 }
 #endif
 
+#ifdef CONFIG_DRIVER_VIDEO_IMX
+static struct imx_fb_videomode imxfb_mode = {
+	.mode = {
+		.name		= "CMO-QVGA",
+		.refresh	= 60,
+		.xres		= 320,
+		.yres		= 240,
+		.pixclock	= 156000,
+		.hsync_len	= 30,
+		.left_margin	= 38,
+		.right_margin	= 20,
+		.vsync_len	= 3,
+		.upper_margin	= 15,
+		.lower_margin	= 4,
+	},
+	.pcr		= 0xFAD08B80,
+	.bpp		= 16,};
+
+static struct imx_fb_platform_data eukrea_cpuimx27_fb_data = {
+	.mode	= &imxfb_mode,
+	.pwmr	= 0x00A903FF,
+	.lscr1	= 0x00120300,
+	.dmacr	= 0x00020010,
+};
+
+static struct device_d imxfb_dev = {
+	.name		= "imxfb",
+	.map_base	= 0x10021000,
+	.size		= 0x1000,
+	.platform_data	= &eukrea_cpuimx27_fb_data,
+};
+#endif
+
 static int eukrea_cpuimx27_devices_init(void)
 {
 	char *envdev = "no";
@@ -208,6 +242,31 @@ static int eukrea_cpuimx27_devices_init(void)
 		PE14_PF_UART1_CTS,
 		PE15_PF_UART1_RTS,
 #endif
+#ifdef CONFIG_DRIVER_VIDEO_IMX
+		PA5_PF_LSCLK,
+		PA6_PF_LD0,
+		PA7_PF_LD1,
+		PA8_PF_LD2,
+		PA9_PF_LD3,
+		PA10_PF_LD4,
+		PA11_PF_LD5,
+		PA12_PF_LD6,
+		PA13_PF_LD7,
+		PA14_PF_LD8,
+		PA15_PF_LD9,
+		PA16_PF_LD10,
+		PA17_PF_LD11,
+		PA18_PF_LD12,
+		PA19_PF_LD13,
+		PA20_PF_LD14,
+		PA21_PF_LD15,
+		PA22_PF_LD16,
+		PA23_PF_LD17,
+		PA28_PF_HSYNC,
+		PA29_PF_VSYNC,
+		PA31_PF_OE_ACD,
+		GPIO_PORTE | 5 | GPIO_GPIO | GPIO_OUT,
+#endif
 	};
 
 	eukrea_cpuimx27_mmu_init();
@@ -217,7 +276,7 @@ static int eukrea_cpuimx27_devices_init(void)
 	CS0L = 0xA0330D01;
 	CS0A = 0x002208C0;
 
-	/* initizalize gpios */
+	/* initialize gpios */
 	for (i = 0; i < ARRAY_SIZE(mode); i++)
 		imx_gpio_mode(mode[i]);
 
@@ -239,6 +298,12 @@ static int eukrea_cpuimx27_devices_init(void)
 
 	printf("Using environment in %s Flash\n", envdev);
 
+#ifdef CONFIG_DRIVER_VIDEO_IMX
+	register_device(&imxfb_dev);
+	gpio_direction_output(GPIO_PORTE | 5, 0);
+	gpio_set_value(GPIO_PORTE | 5, 1);
+#endif
+
 	armlinux_add_dram(&sdram_dev);
 	armlinux_set_bootparams((void *)0xa0000100);
 	armlinux_set_architecture(MACH_TYPE_CPUIMX27);
Signature · 7 lines
1.6.3.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

Re: [PATCH 3/4] commands: add i2c commands

In reply to Eric Bénard
Quoted 69 lines · Eric Bénard

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.

---
v3 :
enable brain and fix v2
tested on iMX27
v2 :
updated as per Sascha's comments

 commands/Kconfig  |    8 ++
 commands/Makefile |    1 +
 commands/i2c.c    |  218 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100644 commands/i2c.c
commands/Kconfig +8 -0
index 0c09f91..0ea32d9 100644
@@ -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
commands/Makefile +1 -0
index 74b0994..3eef5de 100644
@@ -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
commands/i2c.c new file +19 -0
new file mode 100644
index 0000000..4bd7d85
@@ -0,0 +1,218 @@
+/*
+ * i2c.c - i2c commands
+ *
+ * Copyright (c) 2010 Eric Bénard <[email protected]>, 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

commands/i2c.c line 29

Quoted 10 lines
commands/i2c.c new file +10 -0
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <i2c/i2c.h>
+
+struct i2c_client	*client;

is this global still used?

Quoted 71 lines
commands/i2c.c new file +71 -0
+
+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 = NULL;
+	struct i2c_client client;
+	int addr = -1, reg = -1, count = -1, verbose = 0, ret, opt;
+	u8 *buf;
+
+	while ((opt = getopt(argc, argv, "a:b:r: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(optarg, NULL, 0));
+			if (!adapter)
+				return -ENODEV;
+			client.adapter = adapter;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}

commands/i2c.c line 110

Quoted 10 lines
commands/i2c.c new file +10 -0
+
+	count = argc - optind;
+
+	if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
+		return COMMAND_ERROR_USAGE;
+
+	client.addr = addr;
+
+	buf = malloc(count);
+	if (buf) {

I think barebox call hang() or something similar if it runs out of memory, so IIRC no need for this test.

commands/i2c.c line 117

Quoted 7 lines
commands/i2c.c new file +7 -0
+		int i;
+		u8 tmp;
+		for (i = 0; i < count; i++) {
+			tmp = (char) simple_strtol(argv[optind+i], NULL, 16);
+			if (tmp < 0) {
+				free(buf);
+				return COMMAND_ERROR_USAGE;

here...

commands/i2c.c line 124

Quoted 7 lines
commands/i2c.c new file +7 -0
+			}
+			*(buf + i) = tmp;
+		}
+		ret = i2c_write_reg(&client, reg, buf, count);
+		if (ret != count) {
+			free(buf);
+			return ret;

and here might be a "goto out_free" usefull.

Quoted 53 lines
commands/i2c.c new file +53 -0
+		}
+		if (verbose) {
+			printf("wrote %i bytes starting at reg 0x%02x to i2cdev 0x%02x on bus %i\n",
+				count, reg, addr, adapter->nr);
+			for (i = 0; i < count; i++)
+				printf("0x%02x ", *(buf + i));
+			printf("\n");
+		}
+		free(buf);
+		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 = NULL;
+	struct i2c_client client;
+	u8 *buf;
+	int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt;
+
+	while ((opt = getopt(argc, argv, "a:b:c:r:v")) > 0) {
+		switch (opt) {
+		case 'a':
+			addr = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'c':
+			count = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'b':
+			adapter = i2c_get_adapter(simple_strtoul(optarg, NULL, 0));
+			if (!adapter)
+				return -ENODEV;
+			client.adapter = adapter;
+			break;
+		case 'r':
+			reg = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'v':
+			verbose = 1;

commands/i2c.c line 187

Quoted 10 lines
commands/i2c.c new file +10 -0
+			break;
+		}
+	}
+
+	if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
+		return COMMAND_ERROR_USAGE;
+
+	client.addr = addr;
+
+	buf = malloc(count);

see above

Quoted 21 lines
commands/i2c.c new file +21 -0
+	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 on bus %i\n",
+					count, reg, addr, adapter->nr);
+			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"

commands/i2c.c line 218

Quoted 10 lines
commands/i2c.c new file +10 -0
+"  -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

Is the "-b" an optional or required argument. It would be nice if it defaults to the first bus.

cheers, Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

maintainer
#5 lore raw

Re: [PATCH 3/4] commands: add i2c commands

Quoted 159 lines · Marc Kleine-Budde
Quoted 69 lines · Eric Bénard

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.

---
v3 :
enable brain and fix v2
tested on iMX27
v2 :
updated as per Sascha's comments

 commands/Kconfig  |    8 ++
 commands/Makefile |    1 +
 commands/i2c.c    |  218 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100644 commands/i2c.c
commands/Kconfig +8 -0
index 0c09f91..0ea32d9 100644
@@ -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
commands/Makefile +1 -0
index 74b0994..3eef5de 100644
@@ -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
commands/i2c.c new file +19 -0
new file mode 100644
index 0000000..4bd7d85
@@ -0,0 +1,218 @@
+/*
+ * i2c.c - i2c commands
+ *
+ * Copyright (c) 2010 Eric Bénard <[email protected]>, 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

commands/i2c.c line 29

Quoted 10 lines
commands/i2c.c new file +10 -0
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <i2c/i2c.h>
+
+struct i2c_client	*client;

is this global still used?

Quoted 76 lines
commands/i2c.c new file +76 -0
+
+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 = NULL;
+	struct i2c_client client;
+	int addr = -1, reg = -1, count = -1, verbose = 0, ret, opt;
+	u8 *buf;
+
+	while ((opt = getopt(argc, argv, "a:b:r: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(optarg, 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;
Quoted 8 lines

commands/i2c.c line 110

Quoted 5 lines
commands/i2c.c new file +5 -0
+
+	client.addr = addr;
+
+	buf = malloc(count);
+	if (buf) {

I think barebox call hang() or something similar if it runs out of memory, so IIRC no need for this test.

This is only true for the 'x' prefixed versions of this function (xmalloc, xzalloc). malloc will return NULL as usual. You can use xmalloc here.

BTW I really prefer bailing out in case of an error instead of putting the whole rest of the function into an if clause. This leads to deep indention levels and badly readable code.

Quoted 130 lines

commands/i2c.c line 117

Quoted 8 lines
commands/i2c.c new file +7 -0
+		int i;
+		u8 tmp;
+		for (i = 0; i < count; i++) {
+			tmp = (char) simple_strtol(argv[optind+i], NULL, 16);
+			if (tmp < 0) {
+				free(buf);
+				return COMMAND_ERROR_USAGE;

here...

commands/i2c.c line 124

Quoted 7 lines
commands/i2c.c new file +7 -0
+			}
+			*(buf + i) = tmp;
+		}
+		ret = i2c_write_reg(&client, reg, buf, count);
+		if (ret != count) {
+			free(buf);
+			return ret;

and here might be a "goto out_free" usefull.

Quoted 53 lines
commands/i2c.c new file +53 -0
+		}
+		if (verbose) {
+			printf("wrote %i bytes starting at reg 0x%02x to i2cdev 0x%02x on bus %i\n",
+				count, reg, addr, adapter->nr);
+			for (i = 0; i < count; i++)
+				printf("0x%02x ", *(buf + i));
+			printf("\n");
+		}
+		free(buf);
+		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 = NULL;
+	struct i2c_client client;
+	u8 *buf;
+	int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt;
+
+	while ((opt = getopt(argc, argv, "a:b:c:r:v")) > 0) {
+		switch (opt) {
+		case 'a':
+			addr = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'c':
+			count = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'b':
+			adapter = i2c_get_adapter(simple_strtoul(optarg, NULL, 0));
+			if (!adapter)
+				return -ENODEV;
+			client.adapter = adapter;
+			break;
+		case 'r':
+			reg = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'v':
+			verbose = 1;

commands/i2c.c line 187

Quoted 10 lines
commands/i2c.c new file +10 -0
+			break;
+		}
+	}
+
+	if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
+		return COMMAND_ERROR_USAGE;
+
+	client.addr = addr;
+
+	buf = malloc(count);

see above

Quoted 21 lines
commands/i2c.c new file +21 -0
+	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 on bus %i\n",
+					count, reg, addr, adapter->nr);
+			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"

commands/i2c.c line 218

Quoted 10 lines
commands/i2c.c new file +10 -0
+"  -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

Is the "-b" an optional or required argument. It would be nice if it defaults to the first bus.

cheers, Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#6 lore raw

Re: [PATCH 3/4] commands: add i2c commands

In reply to Eric Bénard
Quoted 128 lines · Eric Bénard

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.

---
v3 :
enable brain and fix v2
tested on iMX27
v2 :
updated as per Sascha's comments

 commands/Kconfig  |    8 ++
 commands/Makefile |    1 +
 commands/i2c.c    |  218 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100644 commands/i2c.c
commands/Kconfig +8 -0
index 0c09f91..0ea32d9 100644
@@ -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
commands/Makefile +1 -0
index 74b0994..3eef5de 100644
@@ -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
commands/i2c.c new file +78 -0
new file mode 100644
index 0000000..4bd7d85
@@ -0,0 +1,218 @@
+/*
+ * i2c.c - i2c commands
+ *
+ * Copyright (c) 2010 Eric Bénard <[email protected]>, 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 = NULL;
+	struct i2c_client client;

commands/i2c.c line 88

Quoted 10 lines
commands/i2c.c new file +10 -0
+	int addr = -1, reg = -1, count = -1, verbose = 0, ret, opt;
+	u8 *buf;
+
+	while ((opt = getopt(argc, argv, "a:b:r:v")) > 0) {
+		switch (opt) {
+		case 'a':
+			addr = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'r':
+			reg = simple_strtol(optarg, NULL, 16);

Normally barebox interpretes all numbers as decimal unless prefixed with 0x. You should keep this except for the hexdata below.

Quoted 17 lines
commands/i2c.c new file +17 -0
+			break;
+		case 'b':
+			adapter = i2c_get_adapter(simple_strtoul(optarg, 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;

commands/i2c.c line 115

Quoted 10 lines
commands/i2c.c new file +10 -0
+
+	client.addr = addr;
+
+	buf = malloc(count);
+	if (buf) {
+		int i;
+		u8 tmp;
+		for (i = 0; i < count; i++) {
+			tmp = (char) simple_strtol(argv[optind+i], NULL, 16);
+			if (tmp < 0) {

simple_strtol does not return an error and an u8 is never < 0 anyway. You can remove this check.

Quoted 5 lines
commands/i2c.c new file +5 -0
+				free(buf);
+				return COMMAND_ERROR_USAGE;
+			}
+			*(buf + i) = tmp;
+		}

commands/i2c.c line 130

Quoted 10 lines
commands/i2c.c new file +10 -0
+		ret = i2c_write_reg(&client, reg, buf, count);
+		if (ret != count) {
+			free(buf);
+			return ret;
+		}
+		if (verbose) {
+			printf("wrote %i bytes starting at reg 0x%02x to i2cdev 0x%02x on bus %i\n",
+				count, reg, addr, adapter->nr);
+			for (i = 0; i < count; i++)
+				printf("0x%02x ", *(buf + i));

the memory_display display function seems useful here. I should add a prototype for it.

Quoted 92 lines
commands/i2c.c new file +88 -0
+			printf("\n");
+		}
+		free(buf);
+		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 = NULL;
+	struct i2c_client client;
+	u8 *buf;
+	int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt;
+
+	while ((opt = getopt(argc, argv, "a:b:c:r:v")) > 0) {
+		switch (opt) {
+		case 'a':
+			addr = simple_strtol(optarg, NULL, 16);
+			break;
+		case 'c':
+			count = simple_strtoul(optarg, NULL, 0);
+			break;
+		case 'b':
+			adapter = i2c_get_adapter(simple_strtoul(optarg, NULL, 0));
+			if (!adapter)
+				return -ENODEV;
+			client.adapter = adapter;
+			break;
+		case 'r':
+			reg = simple_strtol(optarg, NULL, 16);
+			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 on bus %i\n",
+					count, reg, addr, adapter->nr);
+			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
Signature · 1 line
1.6.3.3
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

Re: [PATCH 2/4] i2c: implement i2c_get_adapter()

In reply to Eric Bénard

Hello,

Quoted 3 lines · Eric Bénard

From: Sascha Hauer <[email protected]>

You have to add you S-o-b, if the patch goes through your hands.

Cheers, Marc

Quoted 76 lines

---
v2 : add i2c_get_adapter to include/i2c/i2c.h

 drivers/i2c/i2c.c |   24 +++++++++++++++++++++++-
 include/i2c/i2c.h |    2 ++
 2 files changed, 25 insertions(+), 1 deletions(-)
drivers/i2c/i2c.c +23 -1
index 32fd026..1fa114a 100644
@@ -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);
 
include/i2c/i2c.h +2 -0
index 0760fdc..c031bbb 100644
@@ -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;
 };
 
 
@@ -120,6 +121,7 @@ static inline int i2c_register_board_info(int busnum,
 }
 #endif
 extern int i2c_add_numbered_adapter(struct i2c_adapter *adapter);
+struct i2c_adapter *i2c_get_adapter(int busnum);
 
 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
 extern int i2c_master_send(struct i2c_client *client, const char *buf, int count);
Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox