2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2008-2009 Semihalf, Michal Hajduk and Bartlomiej Sieka
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
41 #include <sys/ioctl.h>
43 #include <dev/iicbus/iic.h>
45 #define I2C_DEV "/dev/iic0"
46 #define I2C_MODE_NOTSET 0
47 #define I2C_MODE_NONE 1
48 #define I2C_MODE_STOP_START 2
49 #define I2C_MODE_REPEATED_START 3
50 #define I2C_MODE_TRANSFER 4
76 fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
77 "[-w [0|8|16]] [-c count] [-m [tr|ss|rs|no]] [-b] [-v]\n",
79 fprintf(stderr, " %s -s [-f device] [-n skip_addr] -v\n",
81 fprintf(stderr, " %s -r [-f device] -v\n", getprogname());
85 static struct skip_range
86 skip_get_range(char *skip_addr)
88 struct skip_range addr_range;
94 token = strsep(&skip_addr, "..");
96 addr_range.start = strtoul(token, 0, 16);
97 token = strsep(&skip_addr, "..");
98 if ((token != NULL) && !atoi(token)) {
99 token = strsep(&skip_addr, "..");
101 addr_range.end = strtoul(token, 0, 16);
108 /* Parse the string to get hex 7 bits addresses */
110 skip_get_tokens(char *skip_addr, int *sk_addr, int max_index)
115 for (i = 0; i < max_index; i++) {
116 token = strsep(&skip_addr, ":");
119 sk_addr[i] = strtoul(token, 0, 16);
125 scan_bus(struct iiccmd cmd, char *dev, int skip, char *skip_addr)
127 struct iic_msg rdmsg;
128 struct iic_rdwr_data rdwrdata;
129 struct skip_range addr_range = { 0, 0 };
130 int *tokens, fd, error, i, index, j;
131 int len = 0, do_skip = 0, no_range = 1, num_found = 0, use_read_xfer = 0;
134 fd = open(dev, O_RDWR);
136 fprintf(stderr, "Error opening I2C controller (%s) for "
137 "scanning: %s\n", dev, strerror(errno));
142 len = strlen(skip_addr);
143 if (strstr(skip_addr, "..") != NULL) {
144 addr_range = skip_get_range(skip_addr);
147 tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
148 if (tokens == NULL) {
149 fprintf(stderr, "Error allocating tokens "
154 index = skip_get_tokens(skip_addr, tokens,
158 if (!no_range && (addr_range.start > addr_range.end)) {
159 fprintf(stderr, "Skip address out of range\n");
165 printf("Scanning I2C devices on %s: ", dev);
170 "Hardware may not support START/STOP scanning; "
171 "trying less-reliable read method.\n");
174 for (i = 1; i < 127; i++) {
176 if (skip && ( addr_range.start < addr_range.end)) {
177 if (i >= addr_range.start && i <= addr_range.end)
180 } else if (skip && no_range)
181 for (j = 0; j < index; j++) {
182 if (tokens[j] == i) {
196 error = ioctl(fd, I2CRSTCARD, &cmd);
198 fprintf(stderr, "Controller reset failed\n");
204 rdmsg.flags = IIC_M_RD;
205 rdmsg.slave = i << 1;
206 rdwrdata.msgs = &rdmsg;
208 error = ioctl(fd, I2CRDWR, &rdwrdata);
212 error = ioctl(fd, I2CSTART, &cmd);
213 if (errno == ENODEV || errno == EOPNOTSUPP) {
214 /* If START not supported try reading. */
227 * If we found nothing, maybe START is not supported and returns a
228 * generic error code such as EIO or ENXIO, so try again using reads.
230 if (num_found == 0) {
231 if (!use_read_xfer) {
235 printf("<none found>");
239 error = ioctl(fd, I2CRSTCARD, &cmd);
242 if (skip && no_range)
246 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
247 dev, strerror(errno));
254 reset_bus(struct iiccmd cmd, char *dev)
258 fd = open(dev, O_RDWR);
260 fprintf(stderr, "Error opening I2C controller (%s) for "
261 "resetting: %s\n", dev, strerror(errno));
265 printf("Resetting I2C controller on %s: ", dev);
266 error = ioctl(fd, I2CRSTCARD, &cmd);
270 printf("error: %s\n", strerror(errno));
279 prepare_buf(int size, uint32_t off)
289 else if (size == 2) {
290 buf[0] = (off >> 8) & 0xff;
298 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
301 int error, fd, bufsize;
304 fd = open(dev, O_RDWR);
307 err(1, "open failed");
310 cmd.slave = i2c_opt.addr;
311 error = ioctl(fd, I2CSTART, &cmd);
313 err_msg = "ioctl: error sending start condition";
318 bufsize = i2c_opt.width / 8;
319 buf = prepare_buf(bufsize, i2c_opt.off);
321 err_msg = "error: offset malloc";
329 switch(i2c_opt.mode) {
330 case I2C_MODE_STOP_START:
332 * Write offset where the data will go
337 error = ioctl(fd, I2CWRITE, &cmd);
340 err_msg = "ioctl: error writing offset";
345 error = ioctl(fd, I2CSTOP);
347 err_msg = "ioctl: error sending stop condition";
350 cmd.slave = i2c_opt.addr;
351 error = ioctl(fd, I2CSTART, &cmd);
353 err_msg = "ioctl: error sending start condition";
360 cmd.count = i2c_opt.count;
363 error = ioctl(fd, I2CWRITE, &cmd);
365 err_msg = "ioctl: error writing";
370 case I2C_MODE_REPEATED_START:
372 * Write offset where the data will go
377 error = ioctl(fd, I2CWRITE, &cmd);
380 err_msg = "ioctl: error writing offset";
385 cmd.slave = i2c_opt.addr;
386 error = ioctl(fd, I2CRPTSTART, &cmd);
388 err_msg = "ioctl: error sending repeated start "
396 cmd.count = i2c_opt.count;
399 error = ioctl(fd, I2CWRITE, &cmd);
401 err_msg = "ioctl: error writing";
406 case I2C_MODE_NONE: /* fall through */
408 buf = realloc(buf, bufsize + i2c_opt.count);
410 err_msg = "error: data malloc";
414 memcpy(buf + bufsize, i2c_buf, i2c_opt.count);
416 * Write offset and data
418 cmd.count = bufsize + i2c_opt.count;
421 error = ioctl(fd, I2CWRITE, &cmd);
424 err_msg = "ioctl: error writing";
429 error = ioctl(fd, I2CSTOP);
431 err_msg = "ioctl: error sending stop condition";
439 error = ioctl(fd, I2CSTOP);
441 fprintf(stderr, "error sending stop condition\n");
444 fprintf(stderr, "%s\n", err_msg);
451 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
454 int fd, error, bufsize;
455 char *err_msg, data = 0, *buf;
457 fd = open(dev, O_RDWR);
459 err(1, "open failed");
461 bzero(&cmd, sizeof(cmd));
464 cmd.slave = i2c_opt.addr;
468 error = ioctl(fd, I2CSTART, &cmd);
470 err_msg = "ioctl: error sending start condition";
473 bufsize = i2c_opt.width / 8;
474 buf = prepare_buf(bufsize, i2c_opt.off);
476 err_msg = "error: offset malloc";
483 error = ioctl(fd, I2CWRITE, &cmd);
486 err_msg = "ioctl: error writing offset";
490 if (i2c_opt.mode == I2C_MODE_STOP_START) {
491 error = ioctl(fd, I2CSTOP);
493 err_msg = "error sending stop condition";
498 cmd.slave = i2c_opt.addr | 1;
502 if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.width == 0) {
503 error = ioctl(fd, I2CSTART, &cmd);
505 err_msg = "ioctl: error sending start condition";
508 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
509 error = ioctl(fd, I2CRPTSTART, &cmd);
511 err_msg = "ioctl: error sending repeated start "
517 cmd.count = i2c_opt.count;
520 error = ioctl(fd, I2CREAD, &cmd);
522 err_msg = "ioctl: error while reading";
526 error = ioctl(fd, I2CSTOP);
528 err_msg = "error sending stop condtion\n";
536 error = ioctl(fd, I2CSTOP);
538 fprintf(stderr, "error sending stop condition\n");
541 fprintf(stderr, "%s\n", err_msg);
548 * i2c_rdwr_transfer() - use I2CRDWR to conduct a complete i2c transfer.
550 * Some i2c hardware is unable to provide direct control over START, REPEAT-
551 * START, and STOP operations. Such hardware can only perform a complete
552 * START-<data>-STOP or START-<data>-REPEAT-START-<data>-STOP sequence as a
553 * single operation. The driver framework refers to this sequence as a
554 * "transfer" so we call it "transfer mode". We assemble either one or two
555 * iic_msg structures to describe the IO operations, and hand them off to the
556 * driver to be handled as a single transfer.
559 i2c_rdwr_transfer(char *dev, struct options i2c_opt, char *i2c_buf)
561 struct iic_msg msgs[2];
562 struct iic_rdwr_data xfer;
571 if (i2c_opt.width > 0) {
572 msgs[i].flags = IIC_M_WR | IIC_M_NOSTOP;
573 msgs[i].slave = i2c_opt.addr;
574 msgs[i].buf = off.buf;
575 if (i2c_opt.width == 8) {
576 off.off8 = (uint8_t)i2c_opt.off;
579 off.off16 = (uint16_t)i2c_opt.off;
586 * If the transfer direction is write and we did a write of the offset
587 * above, then we need to elide the start; this transfer is just more
588 * writing that follows the one started above. For a read, we always do
589 * a start; if we did an offset write above it'll be a repeat-start
590 * because of the NOSTOP flag used above.
592 if (i2c_opt.dir == 'w')
593 msgs[i].flags = IIC_M_WR | ((i > 0) ? IIC_M_NOSTART : 0);
595 msgs[i].flags = IIC_M_RD;
596 msgs[i].slave = i2c_opt.addr;
597 msgs[i].len = i2c_opt.count;
598 msgs[i].buf = i2c_buf;
604 if ((fd = open(dev, O_RDWR)) == -1)
605 err(1, "open(%s) failed", dev);
606 if (ioctl(fd, I2CRDWR, &xfer) == -1 )
607 err(1, "ioctl(I2CRDWR) failed");
614 main(int argc, char** argv)
617 struct options i2c_opt;
618 char *dev, *skip_addr, *i2c_buf;
619 int error, chunk_size, i, j, ch;
624 /* Line-break the output every chunk_size bytes */
630 i2c_opt.addr_set = 0;
633 i2c_opt.dir = 'r'; /* direction = read */
636 i2c_opt.binary = 0; /* ASCII text output */
637 i2c_opt.scan = 0; /* no bus scan */
638 i2c_opt.skip = 0; /* scan all addresses */
639 i2c_opt.reset = 0; /* no bus reset */
640 i2c_opt.mode = I2C_MODE_NOTSET;
642 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
645 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
646 if (i2c_opt.addr == 0 && errno == EINVAL)
647 i2c_opt.addr_set = 0;
649 i2c_opt.addr_set = 1;
655 i2c_opt.dir = optarg[0];
658 i2c_opt.off = strtoul(optarg, 0, 16);
659 if (i2c_opt.off == 0 && errno == EINVAL)
663 i2c_opt.width = atoi(optarg);
666 i2c_opt.count = atoi(optarg);
669 if (!strcmp(optarg, "no"))
670 i2c_opt.mode = I2C_MODE_NONE;
671 else if (!strcmp(optarg, "ss"))
672 i2c_opt.mode = I2C_MODE_STOP_START;
673 else if (!strcmp(optarg, "rs"))
674 i2c_opt.mode = I2C_MODE_REPEATED_START;
675 else if (!strcmp(optarg, "tr"))
676 i2c_opt.mode = I2C_MODE_TRANSFER;
704 /* Set default mode if option -m is not specified */
705 if (i2c_opt.mode == I2C_MODE_NOTSET) {
706 if (i2c_opt.dir == 'r')
707 i2c_opt.mode = I2C_MODE_STOP_START;
708 else if (i2c_opt.dir == 'w')
709 i2c_opt.mode = I2C_MODE_NONE;
712 /* Basic sanity check of command line arguments */
714 if (i2c_opt.addr_set)
716 } else if (i2c_opt.reset) {
717 if (i2c_opt.addr_set)
721 } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
722 if ((i2c_opt.addr_set == 0) ||
723 !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
724 i2c_opt.width == 16))
729 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
730 "offset: 0x%02x, width: %u, count: %u\n", dev,
731 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
732 i2c_opt.width, i2c_opt.count);
735 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
738 exit(reset_bus(cmd, dev));
740 i2c_buf = malloc(i2c_opt.count);
742 err(1, "data malloc");
745 * For a write, read the data to be written to the chip from stdin.
747 if (i2c_opt.dir == 'w') {
748 if (i2c_opt.verbose && !i2c_opt.binary)
749 fprintf(stderr, "Enter %u bytes of data: ",
751 for (i = 0; i < i2c_opt.count; i++) {
755 err(1, "not enough data, exiting\n");
761 if (i2c_opt.mode == I2C_MODE_TRANSFER)
762 error = i2c_rdwr_transfer(dev, i2c_opt, i2c_buf);
763 else if (i2c_opt.dir == 'w')
764 error = i2c_write(dev, i2c_opt, i2c_buf);
766 error = i2c_read(dev, i2c_opt, i2c_buf);
774 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
779 while (i < i2c_opt.count) {
780 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
782 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
784 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
785 fprintf(stdout, "%c", i2c_buf[j++]);
789 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
791 if ((i % chunk_size) == 0)
792 fprintf(stderr, "\n");
794 if ((i % chunk_size) != 0)
795 fprintf(stderr, "\n");