[U-Boot-Users] [Patch 5/9 Try2]U-boot-V2:cmd: add I2C commands

Menon, Nishanth x0nishan at ti.com
Sat Jun 21 04:34:51 CEST 2008


i2cdev command is introduced here. This allows
for device creation for doing i2c ops.

This is based on the discussion here:
http://www.nabble.com/-Patch-0-9--U-boot-V2%3A-
Introduce-I2C-support-for-SDP3430-to17981717.html
 #a18029740

Signed-off-by: Nishanth Menon <x0nishan at ti.com>

---
 Documentation/commands.dox |    1
 commands/Kconfig           |    4
 commands/i2c.c             |  211 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 216 insertions(+)

Index: u-boot-v2.git/Documentation/commands.dox
===================================================================
--- u-boot-v2.git.orig/Documentation/commands.dox       2008-06-20 21:09:53.000000000 -0500
+++ u-boot-v2.git/Documentation/commands.dox    2008-06-20 21:09:59.000000000 -0500
@@ -20,5 +20,6 @@
  - @subpage setenv_command
  - @subpage sh_command
  - @subpage unprotect_command
+ - @subpage page_do_cmd_i2cdev

 */
Index: u-boot-v2.git/commands/Kconfig
===================================================================
--- u-boot-v2.git.orig/commands/Kconfig 2008-06-20 21:09:53.000000000 -0500
+++ u-boot-v2.git/commands/Kconfig      2008-06-20 21:11:16.000000000 -0500
@@ -180,6 +180,10 @@
        prompt "nand"
 endmenu

+config CMD_I2C
+       depends on I2C_CHARDEV
+       tristate
+       prompt "i2cdev"

 menu "booting                       "

Index: u-boot-v2.git/commands/i2c.c
===================================================================
--- /dev/null   1970-01-01 00:00:00.000000000 +0000
+++ u-boot-v2.git/commands/i2c.c        2008-06-20 21:09:59.000000000 -0500
@@ -0,0 +1,211 @@
+/**
+ * @file
+ * @brief I2C related applications
+ *
+ * FileName: commands/i2c.c
+ * This provides i2cdump, i2cdetect, i2cset and i2cget appplications
+ * The code originates from the i2c-tools located here:
+ * http://www.lm-sensors.org/wiki/I2CTools
+ *
+ * This file merges the userspace headers and applications to a single
+ * entity as we do not maintain a userspace/kernel space isolation
+ *
+ * NOTE: 10-bit addresses are NOT supported!
+ */
+/*
+ * (C) Copyright 2008
+ * Texas Instruments, <www.ti.com>
+ * Nishanth Menon <x0nishan at ti.com>
+ *
+ * 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 <types.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <fs.h>
+#include <getopt.h>
+#include <linux/i2c-dev.h>
+
+static const __maybe_unused char cmd_i2cdev_s[] =
+    "Usage:\n"
+    "  i2cdev [-a|-r] -b bus_dev -d dev_addr \n"
+    "Where:\n"
+    "-a                Add a new device node\n"
+    "-r                Remove existing device node\n"
+    "-b bus_dev        i2c bus device to operate on\n"
+    "-d dev_addr       Device address for which to create\n";
+
+static int do_cmd_i2cdev(cmd_tbl_t *cmdtp, int argc, char *argv[])
+{
+       char *bus_dev = NULL;
+       int dev_addr = -1;
+       int opt, file, ret;
+       char add = 0, rem = 0;
+       getopt_reset();
+
+       while ((opt = getopt(argc, argv, "arb:d:")) > 0) {
+               switch (opt) {
+               case 'a':
+                       if (rem) {
+                               fprintf(stderr,
+                                       "you cannot add when you selected "
+                                       "delete!\n");
+                               goto fail_ret;
+                       }
+                       add = 1;
+                       break;
+               case 'r':
+                       if (add) {
+                               fprintf(stderr,
+                                       "you cannot delete when you selected "
+                                       "add!\n");
+                               goto fail_ret;
+                       }
+                       rem = 1;
+                       break;
+               case 'b':
+                       bus_dev = optarg;
+                       break;
+               case 'd':
+                       dev_addr = (int)simple_strtoul(optarg, NULL, 10);
+                       if (dev_addr > 0xFF) {
+                               fprintf(stderr, "I2C address are b/w 0 and "
+                                       "0x7f\n");
+                               goto fail_ret;
+                       }
+                       break;
+               default:
+                       fprintf(stderr, "unknown arg %s\n", opt);
+                       goto fail_ret;
+               }
+       }
+
+       /* check args */
+       if (!(add + rem) || (bus_dev == NULL) || (dev_addr < 0)) {
+               fprintf(stderr, "not enough arguments\n");
+               goto fail_ret;
+       }
+       file = open(bus_dev, O_RDONLY);
+       if (file < 0) {
+               perror(argv[0]);
+               goto fail_ret;
+       }
+       if (add)
+               ret = ioctl(file, I2C_SLAVE_ADD, (void *)dev_addr);
+       else
+               ret = ioctl(file, I2C_SLAVE_REM, (void *)dev_addr);
+       if (ret)
+               fprintf(stderr, "Operation failed-%d!\n", ret);
+       close(file);
+       return (ret) ? 1 : 0;
+fail_ret:
+       u_boot_cmd_usage(cmdtp);
+       return 1;
+}
+
+U_BOOT_CMD_START(i2cdev)
+       .maxargs = 6,
+       .cmd = do_cmd_i2cdev,
+       .usage = "program to add a new i2cdevice",
+       U_BOOT_CMD_HELP(cmd_i2cdev_s)
+U_BOOT_CMD_END
+/** @page page_do_cmd_i2cdev i2cdev creates a I2C device node
+SYNOPSIS:
+ at li i2cdev [-a|-r] -b bus_dev -d dev_addr
+
+DESCRIPTION:
+ at verbatim
+       This program allows creation of device nodes corresponding
+       to a I2C device address on an I2C bus. This allows users
+       to do read or write access to a specific I2C device using
+       standard commands.
+       NOTE: i2cbusses are created by default once CHARDEV is
+       enabled during compilation.
+ at endverbatim
+OPTIONS:
+ at verbatim
+    -a         Add a new device node
+    -r         Remove existing device node
+    -b bus_dev i2c bus device to operate on
+    -d dev_addr        Device address for which to create
+ at endverbatim
+
+ at warning Doing a blind probe on i2c bus is NOT RECOMMENDED unless
+you really know what you are doing!! These programs can confuse your
+I2C bus, cause data loss and worse!! Even Read/Write is to be used with
+absolute care and knowledge. If you need to know I2C specification, refer:
+http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf
+
+EXAMPLE USAGE:
+ at li Scan the i2cbus for all devices present:
+ at verbatim
+$ md -b -s /dev/i2cbus0
+ 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+ 00000010: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+ 00000020: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+ 00000030: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+ 00000040: ff ff ff ff ff ff ff ff 48 49 4a 4b ff ff ff ff    ........HIJK....
+ 00000050: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+ 00000060: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+ 00000070: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff    ................
+
+NOTE: 0xff means there are no devices present there. This example shows that
+there are devices on 0x48-0x4B. The md operation on a bus device does a 0 byte
+read (See warning).
+ at endverbatim
+
+ at li Create a new device node(if the device is present) on /dev/i2cbus0_49:
+ at verbatim
+       i2cdev -a -b /dev/i2cbus0 -d 0x49
+ at endverbatim
+
+ at li Remove a existing device node (/dev/i2cbus0_49):
+ at verbatim
+       i2cdev -r -b /dev/i2cbus0 -d 0x49
+ at endverbatim
+
+ at li Dump all registers of device 0n bus 0 device address 0x49:
+ at verbatim
+$ md -b -s /dev/i2cbus0_49
+00000000: 00 00 00 00 00 00 00 00 00 00 0f 0f 0f 0f 00 00    ................
+00000010: 3f 3f 3f 3f 25 00 00 00 00 32 32 32 32 00 00 55    ????%....2222..U
+00000020: 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 00    ................
+00000030: 13 00 00 00 00 79 11 00 00 00 06 00 44 69 00 00    .....y......Di..
+00000040: 00 00 00 00 32 00 00 00 00 00 00 00 00 00 00 1b    ....2...........
+00000050: 00 00 03 f0 05 00 00 00 1a 1a 00 00 00 00 00 00    ................
+00000060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
+00000070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
+00000080: 00 20 00 00 00 2f 00 09 50 ff ff ff ff ff ff ff    . .../..P.......
+00000090: ff 90 00 00 55 00 00 00 03 00 00 00 00 00 00 00    ....U...........
+000000a0: 00 00 00 00 00 00 00 00 00 00 04 15 50 01 44 05    ............P.D.
+000000b0: 00 00 00 00 ff ff 03 00 00 00 ff ff 03 00 00 00    ................
+000000c0: 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00    ................
+000000d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
+000000e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
+000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
+ at endverbatim
+
+ at li Set a value 0xaa to register 1 on device 0x49, bus 0 and check if set:
+ at verbatim
+$ mw -b -d /dev/i2cbus0_49 0x1 0xaa
+$ md -b -s /dev/i2cbus0_49 0x1+1
+00000001: aa                                            .
+ at endverbatim
+
+ */




More information about the U-Boot mailing list