4 * Copyright (c) 2010 Eric Bénard <eric@eukrea.Com>, Eukréa Electromatique
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 static int do_i2c_probe(struct command
*cmdtp
, int argc
, char *argv
[])
31 struct i2c_adapter
*adapter
;
32 struct i2c_client client
;
33 int startaddr
= -1, stopaddr
= -1, addr
, ret
;
37 return COMMAND_ERROR_USAGE
;
39 adapter
= i2c_get_adapter(simple_strtoul(argv
[1], NULL
, 0));
42 client
.adapter
= adapter
;
44 startaddr
= simple_strtol(argv
[2], NULL
, 0);
45 stopaddr
= simple_strtol(argv
[3], NULL
, 0);
46 if ((startaddr
== -1) || (stopaddr
== -1) || (startaddr
> stopaddr
))
47 return COMMAND_ERROR_USAGE
;
52 printf("probing i2c range 0X%02x - 0x%02x :\n", startaddr
, stopaddr
);
53 for (addr
= startaddr
; addr
<= stopaddr
; addr
++) {
55 ret
= i2c_write_reg(&client
, 0x00, ®
, 0);
57 printf("0x%02x ", addr
);
63 static const __maybe_unused
char cmd_i2c_probe_help
[] =
64 "Usage: i2c_probe bus 0xstartaddr 0xstopaddr\n"
65 "probe a range of i2c addresses.\n";
67 BAREBOX_CMD_START(i2c_probe
)
69 .usage
= "probe for an i2c device",
70 BAREBOX_CMD_HELP(cmd_i2c_probe_help
)
73 static int do_i2c_write(struct command
*cmdtp
, int argc
, char *argv
[])
75 struct i2c_adapter
*adapter
= NULL
;
76 struct i2c_client client
;
77 int addr
= -1, reg
= -1, count
= -1, verbose
= 0, ret
, opt
, i
, bus
= 0;
80 while ((opt
= getopt(argc
, argv
, "a:b:r:v")) > 0) {
83 addr
= simple_strtol(optarg
, NULL
, 0);
86 reg
= simple_strtol(optarg
, NULL
, 0);
89 bus
= simple_strtoul(optarg
, NULL
, 0);
97 count
= argc
- optind
;
99 if ((addr
< 0) || (reg
< 0) || (count
== 0) || (addr
> 0x7F))
100 return COMMAND_ERROR_USAGE
;
102 adapter
= i2c_get_adapter(bus
);
104 printf("i2c bus %d not found\n", bus
);
108 client
.adapter
= adapter
;
111 buf
= xmalloc(count
);
112 for (i
= 0; i
< count
; i
++)
113 *(buf
+ i
) = (char) simple_strtol(argv
[optind
+i
], NULL
, 16);
115 ret
= i2c_write_reg(&client
, reg
, buf
, count
);
121 printf("wrote %i bytes starting at reg 0x%02x to i2cdev 0x%02x on bus %i\n",
122 count
, reg
, addr
, adapter
->nr
);
123 for (i
= 0; i
< count
; i
++)
124 printf("0x%02x ", *(buf
+ i
));
133 static const __maybe_unused
char cmd_i2c_write_help
[] =
134 "Usage: i2c_write [OPTION] ... hexdatas\n"
135 "write to i2c device.\n"
136 " -a 0x<addr> i2c device address\n"
137 " -b <bus_num> i2c bus number (default = 0)\n"
138 " -r 0x<reg> start register\n";
140 BAREBOX_CMD_START(i2c_write
)
142 .usage
= "write to an i2c device",
143 BAREBOX_CMD_HELP(cmd_i2c_write_help
)
146 static int do_i2c_read(struct command
*cmdtp
, int argc
, char *argv
[])
148 struct i2c_adapter
*adapter
= NULL
;
149 struct i2c_client client
;
151 int count
= -1, addr
= -1, reg
= -1, verbose
= 0, ret
, opt
, bus
= 0;
153 while ((opt
= getopt(argc
, argv
, "a:b:c:r:v")) > 0) {
156 addr
= simple_strtol(optarg
, NULL
, 0);
159 count
= simple_strtoul(optarg
, NULL
, 0);
162 bus
= simple_strtoul(optarg
, NULL
, 0);
165 reg
= simple_strtol(optarg
, NULL
, 0);
173 if ((addr
< 0) || (reg
< 0) || (count
== 0) || (addr
> 0x7F))
174 return COMMAND_ERROR_USAGE
;
176 adapter
= i2c_get_adapter(bus
);
178 printf("i2c bus %d not found\n", bus
);
182 client
.adapter
= adapter
;
185 buf
= xmalloc(count
);
186 ret
= i2c_read_reg(&client
, reg
, buf
, count
);
190 printf("read %i bytes starting at reg 0x%02x from i2cdev 0x%02x on bus %i\n",
191 count
, reg
, addr
, adapter
->nr
);
192 for (i
= 0; i
< count
; i
++)
193 printf("0x%02x ", *(buf
+ i
));
202 static const __maybe_unused
char cmd_i2c_read_help
[] =
203 "Usage: i2c_read [OPTION]\n"
205 " -a 0x<addr> i2c device address\n"
206 " -b <bus_num> i2c bus number (default = 0)\n"
207 " -r 0x<reg> start register\n"
208 " -c <count> byte count\n";
210 BAREBOX_CMD_START(i2c_read
)
212 .usage
= "read from an i2c device",
213 BAREBOX_CMD_HELP(cmd_i2c_read_help
)