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
75 fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
76 "[-w [0|8|16]] [-c count] [-m [ss|rs|no]] [-b] [-v]\n",
78 fprintf(stderr, " %s -s [-f device] [-n skip_addr] -v\n",
80 fprintf(stderr, " %s -r [-f device] -v\n", getprogname());
84 static struct skip_range
85 skip_get_range(char *skip_addr)
87 struct skip_range addr_range;
93 token = strsep(&skip_addr, "..");
95 addr_range.start = strtoul(token, 0, 16);
96 token = strsep(&skip_addr, "..");
97 if ((token != NULL) && !atoi(token)) {
98 token = strsep(&skip_addr, "..");
100 addr_range.end = strtoul(token, 0, 16);
107 /* Parse the string to get hex 7 bits addresses */
109 skip_get_tokens(char *skip_addr, int *sk_addr, int max_index)
114 for (i = 0; i < max_index; i++) {
115 token = strsep(&skip_addr, ":");
118 sk_addr[i] = strtoul(token, 0, 16);
124 scan_bus(struct iiccmd cmd, char *dev, int skip, char *skip_addr)
126 struct iic_msg rdmsg;
127 struct iic_rdwr_data rdwrdata;
128 struct skip_range addr_range = { 0, 0 };
129 int *tokens, fd, error, i, index, j;
130 int len = 0, do_skip = 0, no_range = 1, num_found = 0, use_read_xfer = 0;
133 fd = open(dev, O_RDWR);
135 fprintf(stderr, "Error opening I2C controller (%s) for "
136 "scanning: %s\n", dev, strerror(errno));
141 len = strlen(skip_addr);
142 if (strstr(skip_addr, "..") != NULL) {
143 addr_range = skip_get_range(skip_addr);
146 tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
147 if (tokens == NULL) {
148 fprintf(stderr, "Error allocating tokens "
153 index = skip_get_tokens(skip_addr, tokens,
157 if (!no_range && (addr_range.start > addr_range.end)) {
158 fprintf(stderr, "Skip address out of range\n");
164 printf("Scanning I2C devices on %s: ", dev);
169 "Hardware may not support START/STOP scanning; "
170 "trying less-reliable read method.\n");
173 for (i = 1; i < 127; i++) {
175 if (skip && ( addr_range.start < addr_range.end)) {
176 if (i >= addr_range.start && i <= addr_range.end)
179 } else if (skip && no_range)
180 for (j = 0; j < index; j++) {
181 if (tokens[j] == i) {
195 error = ioctl(fd, I2CRSTCARD, &cmd);
197 fprintf(stderr, "Controller reset failed\n");
203 rdmsg.flags = IIC_M_RD;
204 rdmsg.slave = i << 1;
205 rdwrdata.msgs = &rdmsg;
207 error = ioctl(fd, I2CRDWR, &rdwrdata);
211 error = ioctl(fd, I2CSTART, &cmd);
212 if (errno == ENODEV || errno == EOPNOTSUPP) {
213 /* If START not supported try reading. */
226 * If we found nothing, maybe START is not supported and returns a
227 * generic error code such as EIO or ENXIO, so try again using reads.
229 if (num_found == 0) {
230 if (!use_read_xfer) {
234 printf("<none found>");
238 error = ioctl(fd, I2CRSTCARD, &cmd);
241 if (skip && no_range)
245 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
246 dev, strerror(errno));
253 reset_bus(struct iiccmd cmd, char *dev)
257 fd = open(dev, O_RDWR);
259 fprintf(stderr, "Error opening I2C controller (%s) for "
260 "resetting: %s\n", dev, strerror(errno));
264 printf("Resetting I2C controller on %s: ", dev);
265 error = ioctl(fd, I2CRSTCARD, &cmd);
269 printf("error: %s\n", strerror(errno));
278 prepare_buf(int size, uint32_t off)
288 else if (size == 2) {
289 buf[0] = (off >> 8) & 0xff;
297 i2c_write(char *dev, struct options i2c_opt, char *i2c_buf)
300 int ch, i, error, fd, bufsize;
304 * Read data to be written to the chip from stdin
306 if (i2c_opt.verbose && !i2c_opt.binary)
307 fprintf(stderr, "Enter %u bytes of data: ", i2c_opt.count);
309 for (i = 0; i < i2c_opt.count; i++) {
313 err(1, "not enough data, exiting\n");
318 fd = open(dev, O_RDWR);
321 err(1, "open failed");
324 cmd.slave = i2c_opt.addr;
325 error = ioctl(fd, I2CSTART, &cmd);
327 err_msg = "ioctl: error sending start condition";
332 bufsize = i2c_opt.width / 8;
333 buf = prepare_buf(bufsize, i2c_opt.off);
335 err_msg = "error: offset malloc";
343 switch(i2c_opt.mode) {
344 case I2C_MODE_STOP_START:
346 * Write offset where the data will go
351 error = ioctl(fd, I2CWRITE, &cmd);
354 err_msg = "ioctl: error writing offset";
359 error = ioctl(fd, I2CSTOP);
361 err_msg = "ioctl: error sending stop condition";
364 cmd.slave = i2c_opt.addr;
365 error = ioctl(fd, I2CSTART, &cmd);
367 err_msg = "ioctl: error sending start condition";
374 cmd.count = i2c_opt.count;
377 error = ioctl(fd, I2CWRITE, &cmd);
379 err_msg = "ioctl: error writing";
384 case I2C_MODE_REPEATED_START:
386 * Write offset where the data will go
391 error = ioctl(fd, I2CWRITE, &cmd);
394 err_msg = "ioctl: error writing offset";
399 cmd.slave = i2c_opt.addr;
400 error = ioctl(fd, I2CRPTSTART, &cmd);
402 err_msg = "ioctl: error sending repeated start "
410 cmd.count = i2c_opt.count;
413 error = ioctl(fd, I2CWRITE, &cmd);
415 err_msg = "ioctl: error writing";
420 case I2C_MODE_NONE: /* fall through */
422 buf = realloc(buf, bufsize + i2c_opt.count);
424 err_msg = "error: data malloc";
428 memcpy(buf + bufsize, i2c_buf, i2c_opt.count);
430 * Write offset and data
432 cmd.count = bufsize + i2c_opt.count;
435 error = ioctl(fd, I2CWRITE, &cmd);
438 err_msg = "ioctl: error writing";
443 error = ioctl(fd, I2CSTOP);
445 err_msg = "ioctl: error sending stop condition";
453 error = ioctl(fd, I2CSTOP);
455 fprintf(stderr, "error sending stop condition\n");
458 fprintf(stderr, "%s\n", err_msg);
465 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
468 int fd, error, bufsize;
469 char *err_msg, data = 0, *buf;
471 fd = open(dev, O_RDWR);
473 err(1, "open failed");
475 bzero(&cmd, sizeof(cmd));
478 cmd.slave = i2c_opt.addr;
482 error = ioctl(fd, I2CSTART, &cmd);
484 err_msg = "ioctl: error sending start condition";
487 bufsize = i2c_opt.width / 8;
488 buf = prepare_buf(bufsize, i2c_opt.off);
490 err_msg = "error: offset malloc";
497 error = ioctl(fd, I2CWRITE, &cmd);
500 err_msg = "ioctl: error writing offset";
504 if (i2c_opt.mode == I2C_MODE_STOP_START) {
505 error = ioctl(fd, I2CSTOP);
507 err_msg = "error sending stop condition";
512 cmd.slave = i2c_opt.addr | 1;
516 if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.width == 0) {
517 error = ioctl(fd, I2CSTART, &cmd);
519 err_msg = "ioctl: error sending start condition";
522 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
523 error = ioctl(fd, I2CRPTSTART, &cmd);
525 err_msg = "ioctl: error sending repeated start "
531 cmd.count = i2c_opt.count;
534 error = ioctl(fd, I2CREAD, &cmd);
536 err_msg = "ioctl: error while reading";
540 error = ioctl(fd, I2CSTOP);
542 err_msg = "error sending stop condtion\n";
550 error = ioctl(fd, I2CSTOP);
552 fprintf(stderr, "error sending stop condition\n");
555 fprintf(stderr, "%s\n", err_msg);
562 main(int argc, char** argv)
565 struct options i2c_opt;
566 char *dev, *skip_addr, *i2c_buf;
567 int error, chunk_size, i, j, ch;
572 /* Line-break the output every chunk_size bytes */
578 i2c_opt.addr_set = 0;
581 i2c_opt.dir = 'r'; /* direction = read */
584 i2c_opt.binary = 0; /* ASCII text output */
585 i2c_opt.scan = 0; /* no bus scan */
586 i2c_opt.skip = 0; /* scan all addresses */
587 i2c_opt.reset = 0; /* no bus reset */
588 i2c_opt.mode = I2C_MODE_NOTSET;
590 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
593 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
594 if (i2c_opt.addr == 0 && errno == EINVAL)
595 i2c_opt.addr_set = 0;
597 i2c_opt.addr_set = 1;
603 i2c_opt.dir = optarg[0];
606 i2c_opt.off = strtoul(optarg, 0, 16);
607 if (i2c_opt.off == 0 && errno == EINVAL)
611 i2c_opt.width = atoi(optarg);
614 i2c_opt.count = atoi(optarg);
617 if (!strcmp(optarg, "no"))
618 i2c_opt.mode = I2C_MODE_NONE;
619 else if (!strcmp(optarg, "ss"))
620 i2c_opt.mode = I2C_MODE_STOP_START;
621 else if (!strcmp(optarg, "rs"))
622 i2c_opt.mode = I2C_MODE_REPEATED_START;
650 /* Set default mode if option -m is not specified */
651 if (i2c_opt.mode == I2C_MODE_NOTSET) {
652 if (i2c_opt.dir == 'r')
653 i2c_opt.mode = I2C_MODE_STOP_START;
654 else if (i2c_opt.dir == 'w')
655 i2c_opt.mode = I2C_MODE_NONE;
658 /* Basic sanity check of command line arguments */
660 if (i2c_opt.addr_set)
662 } else if (i2c_opt.reset) {
663 if (i2c_opt.addr_set)
667 } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
668 if ((i2c_opt.addr_set == 0) ||
669 !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
670 i2c_opt.width == 16))
675 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
676 "offset: 0x%02x, width: %u, count: %u\n", dev,
677 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
678 i2c_opt.width, i2c_opt.count);
681 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
684 exit(reset_bus(cmd, dev));
686 i2c_buf = malloc(i2c_opt.count);
688 err(1, "data malloc");
690 if (i2c_opt.dir == 'w') {
691 error = i2c_write(dev, i2c_opt, i2c_buf);
697 if (i2c_opt.dir == 'r') {
698 error = i2c_read(dev, i2c_opt, i2c_buf);
706 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
711 while (i < i2c_opt.count) {
712 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
714 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
716 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
717 fprintf(stdout, "%c", i2c_buf[j++]);
721 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
723 if ((i % chunk_size) == 0)
724 fprintf(stderr, "\n");
726 if ((i % chunk_size) != 0)
727 fprintf(stderr, "\n");