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 "
148 index = skip_get_tokens(skip_addr, tokens,
152 if (!no_range && (addr_range.start > addr_range.end)) {
153 fprintf(stderr, "Skip address out of range\n");
159 printf("Scanning I2C devices on %s: ", dev);
160 for (i = 1; i < 127; i++) {
162 if (skip && ( addr_range.start < addr_range.end)) {
163 if (i >= addr_range.start && i <= addr_range.end)
166 } else if (skip && no_range)
167 for (j = 0; j < index; j++) {
168 if (tokens[j] == i) {
182 error = ioctl(fd, I2CRSTCARD, &cmd);
188 error = ioctl(fd, I2CSTART, &cmd);
193 error = ioctl(fd, I2CSTOP, &cmd);
197 error = ioctl(fd, I2CRSTCARD, &cmd);
200 if (skip && no_range)
204 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
205 dev, strerror(errno));
212 reset_bus(struct iiccmd cmd, char *dev)
216 fd = open(dev, O_RDWR);
218 fprintf(stderr, "Error opening I2C controller (%s) for "
219 "resetting: %s\n", dev, strerror(errno));
223 printf("Resetting I2C controller on %s: ", dev);
224 error = ioctl(fd, I2CRSTCARD, &cmd);
228 printf("error: %s\n", strerror(errno));
237 prepare_buf(int size, uint32_t off)
247 else if (size == 2) {
248 buf[0] = (off >> 8) & 0xff;
256 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
259 int ch, i, error, fd, bufsize;
263 * Read data to be written to the chip from stdin
265 if (i2c_opt.verbose && !i2c_opt.binary)
266 fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count);
268 for (i = 0; i < i2c_opt.count; i++) {
272 err(1, "not enough data, exiting\n");
277 fd = open(dev, O_RDWR);
280 err(1, "open failed");
283 cmd.slave = i2c_opt.addr;
284 error = ioctl(fd, I2CSTART, &cmd);
286 err_msg = "ioctl: error sending start condition";
291 bufsize = i2c_opt.width / 8;
292 buf = prepare_buf(bufsize, i2c_opt.off);
294 err_msg = "error: offset malloc";
302 switch(i2c_opt.mode) {
303 case I2C_MODE_STOP_START:
305 * Write offset where the data will go
310 error = ioctl(fd, I2CWRITE, &cmd);
313 err_msg = "ioctl: error when write offset";
318 error = ioctl(fd, I2CSTOP, &cmd);
320 err_msg = "ioctl: error sending stop condition";
323 cmd.slave = i2c_opt.addr;
324 error = ioctl(fd, I2CSTART, &cmd);
326 err_msg = "ioctl: error sending start condition";
333 cmd.count = i2c_opt.count;
336 error = ioctl(fd, I2CWRITE, &cmd);
338 err_msg = "ioctl: error when write";
343 case I2C_MODE_REPEATED_START:
345 * Write offset where the data will go
350 error = ioctl(fd, I2CWRITE, &cmd);
353 err_msg = "ioctl: error when write offset";
358 cmd.slave = i2c_opt.addr;
359 error = ioctl(fd, I2CRPTSTART, &cmd);
361 err_msg = "ioctl: error sending repeated start "
369 cmd.count = i2c_opt.count;
372 error = ioctl(fd, I2CWRITE, &cmd);
374 err_msg = "ioctl: error when write";
379 case I2C_MODE_NONE: /* fall through */
381 buf = realloc(buf, bufsize + i2c_opt.count);
383 err_msg = "error: data malloc";
387 memcpy(buf + bufsize, i2c_buf, i2c_opt.count);
389 * Write offset and data
391 cmd.count = bufsize + i2c_opt.count;
394 error = ioctl(fd, I2CWRITE, &cmd);
397 err_msg = "ioctl: error when write";
402 cmd.slave = i2c_opt.addr;
403 error = ioctl(fd, I2CSTOP, &cmd);
405 err_msg = "ioctl: error sending stop condition";
413 cmd.slave = i2c_opt.addr;
414 error = ioctl(fd, I2CSTOP, &cmd);
416 fprintf(stderr, "error sending stop condtion\n");
419 fprintf(stderr, "%s", err_msg);
426 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
429 int i, fd, error, bufsize;
430 char *err_msg, data = 0, *buf;
432 fd = open(dev, O_RDWR);
434 err(1, "open failed");
436 bzero(&cmd, sizeof(cmd));
439 cmd.slave = i2c_opt.addr;
443 error = ioctl(fd, I2CSTART, &cmd);
445 err_msg = "ioctl: error sending start condition";
448 bufsize = i2c_opt.width / 8;
449 buf = prepare_buf(bufsize, i2c_opt.off);
451 err_msg = "error: offset malloc";
458 error = ioctl(fd, I2CWRITE, &cmd);
461 err_msg = "ioctl: error when write offset";
465 if (i2c_opt.mode == I2C_MODE_STOP_START) {
466 cmd.slave = i2c_opt.addr;
467 error = ioctl(fd, I2CSTOP, &cmd);
469 err_msg = "error sending stop condtion\n";
474 cmd.slave = i2c_opt.addr;
478 if (i2c_opt.mode == I2C_MODE_STOP_START) {
479 error = ioctl(fd, I2CSTART, &cmd);
481 err_msg = "ioctl: error sending start condition";
484 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
485 error = ioctl(fd, I2CRPTSTART, &cmd);
487 err_msg = "ioctl: error sending repeated start "
492 error = ioctl(fd, I2CSTOP, &cmd);
494 err_msg = "error sending stop condtion\n";
498 for (i = 0; i < i2c_opt.count; i++) {
499 error = read(fd, &i2c_buf[i], 1);
501 err_msg = "ioctl: error while reading";
510 cmd.slave = i2c_opt.addr;
511 error = ioctl(fd, I2CSTOP, &cmd);
513 fprintf(stderr, "error sending stop condtion\n");
516 fprintf(stderr, "%s", err_msg);
523 main(int argc, char** argv)
526 struct options i2c_opt;
527 char *dev, *skip_addr, *i2c_buf;
528 int error, chunk_size, i, j, ch;
533 /* Line-break the output every chunk_size bytes */
539 i2c_opt.addr_set = 0;
542 i2c_opt.dir = 'r'; /* direction = read */
545 i2c_opt.binary = 0; /* ASCII text output */
546 i2c_opt.scan = 0; /* no bus scan */
547 i2c_opt.skip = 0; /* scan all addresses */
548 i2c_opt.reset = 0; /* no bus reset */
549 i2c_opt.mode = I2C_MODE_NOTSET;
551 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
554 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
555 if (i2c_opt.addr == 0 && errno == EINVAL)
556 i2c_opt.addr_set = 0;
558 i2c_opt.addr_set = 1;
564 i2c_opt.dir = optarg[0];
567 i2c_opt.off = strtoul(optarg, 0, 16);
568 if (i2c_opt.off == 0 && errno == EINVAL)
572 i2c_opt.width = atoi(optarg);
575 i2c_opt.count = atoi(optarg);
578 if (!strcmp(optarg, "no"))
579 i2c_opt.mode = I2C_MODE_NONE;
580 else if (!strcmp(optarg, "ss"))
581 i2c_opt.mode = I2C_MODE_STOP_START;
582 else if (!strcmp(optarg, "rs"))
583 i2c_opt.mode = I2C_MODE_REPEATED_START;
611 /* Set default mode if option -m is not specified */
612 if (i2c_opt.mode == I2C_MODE_NOTSET) {
613 if (i2c_opt.dir == 'r')
614 i2c_opt.mode = I2C_MODE_STOP_START;
615 else if (i2c_opt.dir == 'w')
616 i2c_opt.mode = I2C_MODE_NONE;
619 /* Basic sanity check of command line arguments */
621 if (i2c_opt.addr_set)
623 } else if (i2c_opt.reset) {
624 if (i2c_opt.addr_set)
628 } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
629 if ((i2c_opt.addr_set == 0) ||
630 !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
631 i2c_opt.width == 16))
636 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
637 "offset: 0x%02x, width: %u, count: %u\n", dev,
638 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
639 i2c_opt.width, i2c_opt.count);
642 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
645 exit(reset_bus(cmd, dev));
647 i2c_buf = malloc(i2c_opt.count);
649 err(1, "data malloc");
651 if (i2c_opt.dir == 'w') {
652 error = i2c_write(dev, i2c_opt, i2c_buf);
658 if (i2c_opt.dir == 'r') {
659 error = i2c_read(dev, i2c_opt, i2c_buf);
667 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
672 while (i < i2c_opt.count) {
673 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
675 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
677 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
678 fprintf(stdout, "%c", i2c_buf[j++]);
682 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
684 if ((i % chunk_size) == 0)
685 fprintf(stderr, "\n");
687 if ((i % chunk_size) != 0)
688 fprintf(stderr, "\n");