2 * Copyright (C) 2008-2009 Semihalf, Michal Hajduk and Bartlomiej Sieka
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
39 #include <sys/ioctl.h>
41 #include <dev/iicbus/iic.h>
43 #define I2C_DEV "/dev/iic0"
44 #define I2C_MODE_NOTSET 0
45 #define I2C_MODE_NONE 1
46 #define I2C_MODE_STOP_START 2
47 #define I2C_MODE_REPEATED_START 3
73 fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
74 "[-w [0|8|16]] [-c count] [-m [ss|rs|no]] [-b] [-v]\n",
76 fprintf(stderr, " %s -s [-f device] [-n skip_addr] -v\n",
78 fprintf(stderr, " %s -r [-f device] -v\n", getprogname());
82 static struct skip_range
83 skip_get_range(char *skip_addr)
85 struct skip_range addr_range;
91 token = strsep(&skip_addr, "..");
93 addr_range.start = strtoul(token, 0, 16);
94 token = strsep(&skip_addr, "..");
95 if ((token != NULL) && !atoi(token)) {
96 token = strsep(&skip_addr, "..");
98 addr_range.end = strtoul(token, 0, 16);
105 /* Parse the string to get hex 7 bits addresses */
107 skip_get_tokens(char *skip_addr, int *sk_addr, int max_index)
112 for (i = 0; i < max_index; i++) {
113 token = strsep(&skip_addr, ":");
116 sk_addr[i] = strtoul(token, 0, 16);
122 scan_bus(struct iiccmd cmd, char *dev, int skip, char *skip_addr)
124 struct skip_range addr_range = { 0, 0 };
125 int *tokens, fd, error, i, index, j;
126 int len = 0, do_skip = 0, no_range = 1;
128 fd = open(dev, O_RDWR);
130 fprintf(stderr, "Error opening I2C controller (%s) for "
131 "scanning: %s\n", dev, strerror(errno));
136 len = strlen(skip_addr);
137 if (strstr(skip_addr, "..") != NULL) {
138 addr_range = skip_get_range(skip_addr);
141 tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
142 if (tokens == NULL) {
143 fprintf(stderr, "Error allocating tokens "
147 index = skip_get_tokens(skip_addr, tokens,
151 if (!no_range && (addr_range.start > addr_range.end)) {
152 fprintf(stderr, "Skip address out of range\n");
157 printf("Scanning I2C devices on %s: ", dev);
158 for (i = 1; i < 127; i++) {
160 if (skip && ( addr_range.start < addr_range.end)) {
161 if (i >= addr_range.start && i <= addr_range.end)
164 } else if (skip && no_range)
165 for (j = 0; j < index; j++) {
166 if (tokens[j] == i) {
180 error = ioctl(fd, I2CRSTCARD, &cmd);
186 error = ioctl(fd, I2CSTART, &cmd);
191 error = ioctl(fd, I2CSTOP, &cmd);
195 error = ioctl(fd, I2CRSTCARD, &cmd);
198 if (skip && no_range)
202 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
203 dev, strerror(errno));
210 reset_bus(struct iiccmd cmd, char *dev)
214 fd = open(dev, O_RDWR);
216 fprintf(stderr, "Error opening I2C controller (%s) for "
217 "resetting: %s\n", dev, strerror(errno));
221 printf("Resetting I2C controller on %s: ", dev);
222 error = ioctl(fd, I2CRSTCARD, &cmd);
226 printf("error: %s\n", strerror(errno));
235 prepare_buf(int size, uint32_t off)
245 else if (size == 2) {
246 buf[0] = (off >> 8) & 0xff;
254 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
257 int ch, i, error, fd, bufsize;
261 * Read data to be written to the chip from stdin
263 if (i2c_opt.verbose && !i2c_opt.binary)
264 fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count);
266 for (i = 0; i < i2c_opt.count; i++) {
270 err(1, "not enough data, exiting\n");
275 fd = open(dev, O_RDWR);
278 err(1, "open failed");
282 * Write offset where the data will go
284 cmd.slave = i2c_opt.addr;
285 error = ioctl(fd, I2CSTART, &cmd);
287 err_msg = "ioctl: error sending start condition";
292 bufsize = i2c_opt.width / 8;
293 buf = prepare_buf(bufsize, i2c_opt.off);
295 err_msg = "error: offset malloc";
301 error = ioctl(fd, I2CWRITE, &cmd);
304 err_msg = "ioctl: error when write offset";
309 /* Mode - stop start */
310 if (i2c_opt.mode == I2C_MODE_STOP_START) {
311 cmd.slave = i2c_opt.addr;
312 error = ioctl(fd, I2CSTOP, &cmd);
314 err_msg = "ioctl: error sending stop condition";
317 cmd.slave = i2c_opt.addr;
318 error = ioctl(fd, I2CSTART, &cmd);
320 err_msg = "ioctl: error sending start condition";
324 /* Mode - repeated start */
325 if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
326 cmd.slave = i2c_opt.addr;
327 error = ioctl(fd, I2CRPTSTART, &cmd);
329 err_msg = "ioctl: error sending repeated start "
338 cmd.count = i2c_opt.count;
341 error = ioctl(fd, I2CWRITE, &cmd);
343 err_msg = "ioctl: error when write";
346 cmd.slave = i2c_opt.addr;
347 error = ioctl(fd, I2CSTOP, &cmd);
349 err_msg = "ioctl: error sending stop condition";
357 cmd.slave = i2c_opt.addr;
358 error = ioctl(fd, I2CSTOP, &cmd);
360 fprintf(stderr, "error sending stop condtion\n");
363 fprintf(stderr, "%s", err_msg);
370 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
373 int i, fd, error, bufsize;
374 char *err_msg, data = 0, *buf;
376 fd = open(dev, O_RDWR);
378 err(1, "open failed");
380 bzero(&cmd, sizeof(cmd));
383 cmd.slave = i2c_opt.addr;
387 error = ioctl(fd, I2CSTART, &cmd);
389 err_msg = "ioctl: error sending start condition";
392 bufsize = i2c_opt.width / 8;
393 buf = prepare_buf(bufsize, i2c_opt.off);
395 err_msg = "error: offset malloc";
402 error = ioctl(fd, I2CWRITE, &cmd);
405 err_msg = "ioctl: error when write offset";
409 if (i2c_opt.mode == I2C_MODE_STOP_START) {
410 cmd.slave = i2c_opt.addr;
411 error = ioctl(fd, I2CSTOP, &cmd);
416 cmd.slave = i2c_opt.addr;
420 if (i2c_opt.mode == I2C_MODE_STOP_START) {
421 error = ioctl(fd, I2CSTART, &cmd);
423 err_msg = "ioctl: error sending start condition";
426 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
427 error = ioctl(fd, I2CRPTSTART, &cmd);
429 err_msg = "ioctl: error sending repeated start "
434 error = ioctl(fd, I2CSTOP, &cmd);
438 for (i = 0; i < i2c_opt.count; i++) {
439 error = read(fd, &i2c_buf[i], 1);
441 err_msg = "ioctl: error while reading";
450 cmd.slave = i2c_opt.addr;
451 error = ioctl(fd, I2CSTOP, &cmd);
453 fprintf(stderr, "error sending stop condtion\n");
456 fprintf(stderr, "%s", err_msg);
463 main(int argc, char** argv)
466 struct options i2c_opt;
467 char *dev, *skip_addr, *err_msg, *i2c_buf;
468 int error, chunk_size, i, j, ch;
473 /* Line-break the output every chunk_size bytes */
480 i2c_opt.addr_set = 0;
483 i2c_opt.dir = 'r'; /* direction = read */
486 i2c_opt.binary = 0; /* ASCII text output */
487 i2c_opt.scan = 0; /* no bus scan */
488 i2c_opt.skip = 0; /* scan all addresses */
489 i2c_opt.reset = 0; /* no bus reset */
490 i2c_opt.mode = I2C_MODE_NOTSET;
492 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
495 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
496 if (i2c_opt.addr == 0 && errno == EINVAL)
497 i2c_opt.addr_set = 0;
499 i2c_opt.addr_set = 1;
505 i2c_opt.dir = optarg[0];
508 i2c_opt.off = strtoul(optarg, 0, 16);
509 if (i2c_opt.off == 0 && errno == EINVAL)
513 i2c_opt.width = atoi(optarg);
516 i2c_opt.count = atoi(optarg);
519 if (!strcmp(optarg, "no"))
520 i2c_opt.mode = I2C_MODE_NONE;
521 else if (!strcmp(optarg, "ss"))
522 i2c_opt.mode = I2C_MODE_STOP_START;
523 else if (!strcmp(optarg, "rs"))
524 i2c_opt.mode = I2C_MODE_REPEATED_START;
552 /* Set default mode if option -m is not specified */
553 if (i2c_opt.mode == I2C_MODE_NOTSET) {
554 if (i2c_opt.dir == 'r')
555 i2c_opt.mode = I2C_MODE_STOP_START;
556 else if (i2c_opt.dir == 'w')
557 i2c_opt.mode = I2C_MODE_NONE;
560 /* Basic sanity check of command line arguments */
562 if (i2c_opt.addr_set)
564 } else if (i2c_opt.reset) {
565 if (i2c_opt.addr_set)
569 } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
570 if ((i2c_opt.addr_set == 0) ||
571 !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
572 i2c_opt.width == 16))
577 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
578 "offset: 0x%02x, width: %u, count: %u\n", dev,
579 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
580 i2c_opt.width, i2c_opt.count);
583 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
586 exit(reset_bus(cmd, dev));
588 i2c_buf = malloc(i2c_opt.count);
590 err(1, "data malloc");
592 if (i2c_opt.dir == 'w') {
593 error = i2c_write(dev, i2c_opt, i2c_buf);
599 if (i2c_opt.dir == 'r') {
600 error = i2c_read(dev, i2c_opt, i2c_buf);
608 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
613 while (i < i2c_opt.count) {
614 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
616 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
618 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
619 fprintf(stdout, "%c", i2c_buf[j++]);
623 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
625 if ((i % chunk_size) == 0)
626 fprintf(stderr, "\n");
628 if ((i % chunk_size) != 0)
629 fprintf(stderr, "\n");