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 iic_msg rdmsg;
125 struct iic_rdwr_data rdwrdata;
126 struct skip_range addr_range = { 0, 0 };
127 int *tokens, fd, error, i, index, j;
128 int len = 0, do_skip = 0, no_range = 1, num_found = 0, use_read_xfer = 0;
131 fd = open(dev, O_RDWR);
133 fprintf(stderr, "Error opening I2C controller (%s) for "
134 "scanning: %s\n", dev, strerror(errno));
139 len = strlen(skip_addr);
140 if (strstr(skip_addr, "..") != NULL) {
141 addr_range = skip_get_range(skip_addr);
144 tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
145 if (tokens == NULL) {
146 fprintf(stderr, "Error allocating tokens "
151 index = skip_get_tokens(skip_addr, tokens,
155 if (!no_range && (addr_range.start > addr_range.end)) {
156 fprintf(stderr, "Skip address out of range\n");
162 printf("Scanning I2C devices on %s: ", dev);
167 "Hardware may not support START/STOP scanning; "
168 "trying less-reliable read method.\n");
171 for (i = 1; i < 127; i++) {
173 if (skip && ( addr_range.start < addr_range.end)) {
174 if (i >= addr_range.start && i <= addr_range.end)
177 } else if (skip && no_range)
178 for (j = 0; j < index; j++) {
179 if (tokens[j] == i) {
193 error = ioctl(fd, I2CRSTCARD, &cmd);
195 fprintf(stderr, "Controller reset failed\n");
201 rdmsg.flags = IIC_M_RD;
202 rdmsg.slave = i << 1;
203 rdwrdata.msgs = &rdmsg;
205 error = ioctl(fd, I2CRDWR, &rdwrdata);
209 error = ioctl(fd, I2CSTART, &cmd);
210 if (errno == ENODEV || errno == EOPNOTSUPP) {
211 /* If START not supported try reading. */
217 ioctl(fd, I2CSTOP, &cmd);
225 * If we found nothing, maybe START is not supported and returns a
226 * generic error code such as EIO or ENXIO, so try again using reads.
228 if (num_found == 0) {
229 if (!use_read_xfer) {
233 printf("<none found>");
237 error = ioctl(fd, I2CRSTCARD, &cmd);
240 if (skip && no_range)
244 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
245 dev, strerror(errno));
252 reset_bus(struct iiccmd cmd, char *dev)
256 fd = open(dev, O_RDWR);
258 fprintf(stderr, "Error opening I2C controller (%s) for "
259 "resetting: %s\n", dev, strerror(errno));
263 printf("Resetting I2C controller on %s: ", dev);
264 error = ioctl(fd, I2CRSTCARD, &cmd);
268 printf("error: %s\n", strerror(errno));
277 prepare_buf(int size, uint32_t off)
287 else if (size == 2) {
288 buf[0] = (off >> 8) & 0xff;
296 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
299 int ch, i, error, fd, bufsize;
303 * Read data to be written to the chip from stdin
305 if (i2c_opt.verbose && !i2c_opt.binary)
306 fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count);
308 for (i = 0; i < i2c_opt.count; i++) {
312 err(1, "not enough data, exiting\n");
317 fd = open(dev, O_RDWR);
320 err(1, "open failed");
323 cmd.slave = i2c_opt.addr;
324 error = ioctl(fd, I2CSTART, &cmd);
326 err_msg = "ioctl: error sending start condition";
331 bufsize = i2c_opt.width / 8;
332 buf = prepare_buf(bufsize, i2c_opt.off);
334 err_msg = "error: offset malloc";
342 switch(i2c_opt.mode) {
343 case I2C_MODE_STOP_START:
345 * Write offset where the data will go
350 error = ioctl(fd, I2CWRITE, &cmd);
353 err_msg = "ioctl: error writing offset";
358 error = ioctl(fd, I2CSTOP, &cmd);
360 err_msg = "ioctl: error sending stop condition";
363 cmd.slave = i2c_opt.addr;
364 error = ioctl(fd, I2CSTART, &cmd);
366 err_msg = "ioctl: error sending start condition";
373 cmd.count = i2c_opt.count;
376 error = ioctl(fd, I2CWRITE, &cmd);
378 err_msg = "ioctl: error writing";
383 case I2C_MODE_REPEATED_START:
385 * Write offset where the data will go
390 error = ioctl(fd, I2CWRITE, &cmd);
393 err_msg = "ioctl: error writing offset";
398 cmd.slave = i2c_opt.addr;
399 error = ioctl(fd, I2CRPTSTART, &cmd);
401 err_msg = "ioctl: error sending repeated start "
409 cmd.count = i2c_opt.count;
412 error = ioctl(fd, I2CWRITE, &cmd);
414 err_msg = "ioctl: error writing";
419 case I2C_MODE_NONE: /* fall through */
421 buf = realloc(buf, bufsize + i2c_opt.count);
423 err_msg = "error: data malloc";
427 memcpy(buf + bufsize, i2c_buf, i2c_opt.count);
429 * Write offset and data
431 cmd.count = bufsize + i2c_opt.count;
434 error = ioctl(fd, I2CWRITE, &cmd);
437 err_msg = "ioctl: error writing";
442 cmd.slave = i2c_opt.addr;
443 error = ioctl(fd, I2CSTOP, &cmd);
445 err_msg = "ioctl: error sending stop condition";
453 cmd.slave = i2c_opt.addr;
454 error = ioctl(fd, I2CSTOP, &cmd);
456 fprintf(stderr, "error sending stop condition\n");
459 fprintf(stderr, "%s\n", err_msg);
466 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
469 int i, fd, error, bufsize;
470 char *err_msg, data = 0, *buf;
472 fd = open(dev, O_RDWR);
474 err(1, "open failed");
476 bzero(&cmd, sizeof(cmd));
479 cmd.slave = i2c_opt.addr;
483 error = ioctl(fd, I2CSTART, &cmd);
485 err_msg = "ioctl: error sending start condition";
488 bufsize = i2c_opt.width / 8;
489 buf = prepare_buf(bufsize, i2c_opt.off);
491 err_msg = "error: offset malloc";
498 error = ioctl(fd, I2CWRITE, &cmd);
501 err_msg = "ioctl: error writing offset";
505 if (i2c_opt.mode == I2C_MODE_STOP_START) {
506 cmd.slave = i2c_opt.addr;
507 error = ioctl(fd, I2CSTOP, &cmd);
509 err_msg = "error sending stop condition";
514 cmd.slave = i2c_opt.addr;
518 if (i2c_opt.mode == I2C_MODE_STOP_START) {
519 error = ioctl(fd, I2CSTART, &cmd);
521 err_msg = "ioctl: error sending start condition";
524 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
525 error = ioctl(fd, I2CRPTSTART, &cmd);
527 err_msg = "ioctl: error sending repeated start "
532 error = ioctl(fd, I2CSTOP, &cmd);
534 err_msg = "error sending stop condition";
538 for (i = 0; i < i2c_opt.count; i++) {
539 error = read(fd, &i2c_buf[i], 1);
541 err_msg = "ioctl: error while reading";
550 cmd.slave = i2c_opt.addr;
551 error = ioctl(fd, I2CSTOP, &cmd);
553 fprintf(stderr, "error sending stop condition\n");
556 fprintf(stderr, "%s\n", err_msg);
563 main(int argc, char** argv)
566 struct options i2c_opt;
567 char *dev, *skip_addr, *i2c_buf;
568 int error, chunk_size, i, j, ch;
573 /* Line-break the output every chunk_size bytes */
579 i2c_opt.addr_set = 0;
582 i2c_opt.dir = 'r'; /* direction = read */
585 i2c_opt.binary = 0; /* ASCII text output */
586 i2c_opt.scan = 0; /* no bus scan */
587 i2c_opt.skip = 0; /* scan all addresses */
588 i2c_opt.reset = 0; /* no bus reset */
589 i2c_opt.mode = I2C_MODE_NOTSET;
591 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
594 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
595 if (i2c_opt.addr == 0 && errno == EINVAL)
596 i2c_opt.addr_set = 0;
598 i2c_opt.addr_set = 1;
604 i2c_opt.dir = optarg[0];
607 i2c_opt.off = strtoul(optarg, 0, 16);
608 if (i2c_opt.off == 0 && errno == EINVAL)
612 i2c_opt.width = atoi(optarg);
615 i2c_opt.count = atoi(optarg);
618 if (!strcmp(optarg, "no"))
619 i2c_opt.mode = I2C_MODE_NONE;
620 else if (!strcmp(optarg, "ss"))
621 i2c_opt.mode = I2C_MODE_STOP_START;
622 else if (!strcmp(optarg, "rs"))
623 i2c_opt.mode = I2C_MODE_REPEATED_START;
651 /* Set default mode if option -m is not specified */
652 if (i2c_opt.mode == I2C_MODE_NOTSET) {
653 if (i2c_opt.dir == 'r')
654 i2c_opt.mode = I2C_MODE_STOP_START;
655 else if (i2c_opt.dir == 'w')
656 i2c_opt.mode = I2C_MODE_NONE;
659 /* Basic sanity check of command line arguments */
661 if (i2c_opt.addr_set)
663 } else if (i2c_opt.reset) {
664 if (i2c_opt.addr_set)
668 } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
669 if ((i2c_opt.addr_set == 0) ||
670 !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
671 i2c_opt.width == 16))
676 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
677 "offset: 0x%02x, width: %u, count: %u\n", dev,
678 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
679 i2c_opt.width, i2c_opt.count);
682 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
685 exit(reset_bus(cmd, dev));
687 i2c_buf = malloc(i2c_opt.count);
689 err(1, "data malloc");
691 if (i2c_opt.dir == 'w') {
692 error = i2c_write(dev, i2c_opt, i2c_buf);
698 if (i2c_opt.dir == 'r') {
699 error = i2c_read(dev, i2c_opt, i2c_buf);
707 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
712 while (i < i2c_opt.count) {
713 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
715 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
717 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
718 fprintf(stdout, "%c", i2c_buf[j++]);
722 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
724 if ((i % chunk_size) == 0)
725 fprintf(stderr, "\n");
727 if ((i % chunk_size) != 0)
728 fprintf(stderr, "\n");