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 Comments from sacha incorporated. Signed-off-by: Nishanth Menon <[EMAIL PROTECTED]> --- Documentation/commands.dox | 1 commands/Kconfig | 4 commands/i2c.c | 204 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) Index: u-boot-v2.git/Documentation/commands.dox =================================================================== --- u-boot-v2.git.orig/Documentation/commands.dox 2008-06-24 16:59:06.000000000 -0500 +++ u-boot-v2.git/Documentation/commands.dox 2008-06-24 17:00:03.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-24 16:59:05.000000000 -0500 +++ u-boot-v2.git/commands/Kconfig 2008-06-24 19:27:43.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-24 18:07:17.000000000 -0500 @@ -0,0 +1,204 @@ +/** + * @file + * @brief I2C related applications + * + * FileName: commands/i2c.c + * + * This provides for creation of i2c device specific node + * for a given bus. + * + */ +/* + * This implementation is heavily influenced by the valuable inputs from + * Sascha Hauer <[EMAIL PROTECTED]> + * + * (C) Copyright 2008 + * Texas Instruments, <www.ti.com> + * Nishanth Menon <[EMAIL PROTECTED]> + * + * 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': + add = 1; + break; + case 'r': + 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) { + fprintf(stderr, "You cannot add and remove at the same time\n"); + goto fail_ret; + } + 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: [EMAIL PROTECTED] i2cdev [-a|-r] -b bus_dev -d dev_addr + +DESCRIPTION: [EMAIL PROTECTED] + 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. [EMAIL PROTECTED] +OPTIONS: [EMAIL PROTECTED] + -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 [EMAIL PROTECTED] + [EMAIL PROTECTED] 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: [EMAIL PROTECTED] Scan the i2cbus for all devices present: [EMAIL PROTECTED] +$ 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). [EMAIL PROTECTED] + [EMAIL PROTECTED] Create a new device node(if the device is present) on /dev/i2cbus0_49: [EMAIL PROTECTED] + i2cdev -a -b /dev/i2cbus0 -d 0x49 [EMAIL PROTECTED] + [EMAIL PROTECTED] Remove a existing device node (/dev/i2cbus0_49): [EMAIL PROTECTED] + i2cdev -r -b /dev/i2cbus0 -d 0x49 [EMAIL PROTECTED] + [EMAIL PROTECTED] Dump all registers of device 0n bus 0 device address 0x49: [EMAIL PROTECTED] +$ 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 ................ [EMAIL PROTECTED] + [EMAIL PROTECTED] Set a value 0xaa to register 1 on device 0x49, bus 0 and check if set: [EMAIL PROTECTED] +$ mw -b -d /dev/i2cbus0_49 0x1 0xaa +$ md -b -s /dev/i2cbus0_49 0x1+1 +00000001: aa . [EMAIL PROTECTED] + */ ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ U-Boot-Users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/u-boot-users
