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/endian.h>
42 #include <sys/ioctl.h>
44 #include <dev/iicbus/iic.h>
46 #define I2C_DEV "/dev/iic0"
47 #define I2C_MODE_NOTSET 0
48 #define I2C_MODE_NONE 1
49 #define I2C_MODE_STOP_START 2
50 #define I2C_MODE_REPEATED_START 3
51 #define I2C_MODE_TRANSFER 4
79 fprintf(stderr, "usage: %s -a addr [-f device] [-d [r|w]] [-o offset] "
80 "[-w [0|8|16]] [-c count] [-m [tr|ss|rs|no]] [-b] [-v]\n",
82 fprintf(stderr, " %s -s [-f device] [-n skip_addr] -v\n",
84 fprintf(stderr, " %s -r [-f device] -v\n", getprogname());
88 static struct skip_range
89 skip_get_range(char *skip_addr)
91 struct skip_range addr_range;
97 token = strsep(&skip_addr, "..");
99 addr_range.start = strtoul(token, 0, 16);
100 token = strsep(&skip_addr, "..");
101 if ((token != NULL) && !atoi(token)) {
102 token = strsep(&skip_addr, "..");
104 addr_range.end = strtoul(token, 0, 16);
111 /* Parse the string to get hex 7 bits addresses */
113 skip_get_tokens(char *skip_addr, int *sk_addr, int max_index)
118 for (i = 0; i < max_index; i++) {
119 token = strsep(&skip_addr, ":");
122 sk_addr[i] = strtoul(token, 0, 16);
128 scan_bus(const char *dev, int fd, int skip, char *skip_addr)
131 struct iic_msg rdmsg;
132 struct iic_rdwr_data rdwrdata;
133 struct skip_range addr_range = { 0, 0 };
134 int *tokens = NULL, error, i, idx = 0, j;
135 int len = 0, do_skip = 0, no_range = 1, num_found = 0, use_read_xfer = 0;
139 assert(skip_addr != NULL);
140 len = strlen(skip_addr);
141 if (strstr(skip_addr, "..") != NULL) {
142 addr_range = skip_get_range(skip_addr);
145 tokens = (int *)malloc((len / 2 + 1) * sizeof(int));
146 if (tokens == NULL) {
147 fprintf(stderr, "Error allocating tokens "
152 idx = skip_get_tokens(skip_addr, tokens,
156 if (!no_range && (addr_range.start > addr_range.end)) {
157 fprintf(stderr, "Skip address out of range\n");
163 printf("Scanning I2C devices on %s: ", dev);
168 "Hardware may not support START/STOP scanning; "
169 "trying less-reliable read method.\n");
172 for (i = 1; i < 127; i++) {
174 if (skip && ( addr_range.start < addr_range.end)) {
175 if (i >= addr_range.start && i <= addr_range.end)
178 } else if (skip && no_range) {
179 assert (tokens != NULL);
180 for (j = 0; j < idx; j++) {
181 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. */
218 (void)ioctl(fd, I2CSTOP);
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);
241 if (skip && no_range)
244 assert(tokens == NULL);
247 fprintf(stderr, "Error scanning I2C controller (%s): %s\n",
248 dev, strerror(errno));
255 reset_bus(const char *dev, int fd)
260 printf("Resetting I2C controller on %s: ", dev);
261 error = ioctl(fd, I2CRSTCARD, &cmd);
264 printf("error: %s\n", strerror(errno));
273 encode_offset(const char *width, unsigned address, uint8_t *dst, size_t *len)
276 if (!strcmp(width, "0")) {
280 if (!strcmp(width, "8")) {
282 return ("Invalid 8-bit address\n");
287 if (address > 0xffff)
288 return ("Invalid 16-bit address\n");
289 if (!strcmp(width, "16LE") || !strcmp(width, "16")) {
290 le16enc(dst, address);
294 if (!strcmp(width, "16BE")) {
295 be16enc(dst, address);
299 return ("Invalid offset width, must be: 0|8|16|16LE|16BE\n");
303 write_offset(int fd, struct options i2c_opt, struct iiccmd *cmd)
307 if (i2c_opt.off_len > 0) {
308 cmd->count = i2c_opt.off_len;
309 cmd->buf = i2c_opt.off_buf;
310 error = ioctl(fd, I2CWRITE, cmd);
312 return ("ioctl: error writing offset\n");
318 i2c_write(int fd, struct options i2c_opt, char *i2c_buf)
325 cmd.slave = i2c_opt.addr;
326 error = ioctl(fd, I2CSTART, &cmd);
328 err_msg = "ioctl: error sending start condition";
332 switch(i2c_opt.mode) {
333 case I2C_MODE_STOP_START:
334 err_msg = write_offset(fd, i2c_opt, &cmd);
338 error = ioctl(fd, I2CSTOP);
340 err_msg = "ioctl: error sending stop condition";
343 cmd.slave = i2c_opt.addr;
344 error = ioctl(fd, I2CSTART, &cmd);
346 err_msg = "ioctl: error sending start condition";
353 cmd.count = i2c_opt.count;
356 error = ioctl(fd, I2CWRITE, &cmd);
358 err_msg = "ioctl: error writing";
363 case I2C_MODE_REPEATED_START:
364 err_msg = write_offset(fd, i2c_opt, &cmd);
368 cmd.slave = i2c_opt.addr;
369 error = ioctl(fd, I2CRPTSTART, &cmd);
371 err_msg = "ioctl: error sending repeated start "
379 cmd.count = i2c_opt.count;
382 error = ioctl(fd, I2CWRITE, &cmd);
384 err_msg = "ioctl: error writing";
389 case I2C_MODE_NONE: /* fall through */
391 buf = malloc(i2c_opt.off_len + i2c_opt.count);
393 err_msg = "error: data malloc";
396 memcpy(buf, i2c_opt.off_buf, i2c_opt.off_len);
398 memcpy(buf + i2c_opt.off_len, i2c_buf, i2c_opt.count);
400 * Write offset and data
402 cmd.count = i2c_opt.off_len + i2c_opt.count;
405 error = ioctl(fd, I2CWRITE, &cmd);
408 err_msg = "ioctl: error writing";
413 error = ioctl(fd, I2CSTOP);
415 err_msg = "ioctl: error sending stop condition";
422 error = ioctl(fd, I2CSTOP);
424 fprintf(stderr, "error sending stop condition\n");
427 fprintf(stderr, "%s\n", err_msg);
433 i2c_read(int fd, struct options i2c_opt, char *i2c_buf)
440 bzero(&cmd, sizeof(cmd));
442 if (i2c_opt.off_len) {
443 cmd.slave = i2c_opt.addr;
447 error = ioctl(fd, I2CSTART, &cmd);
449 err_msg = "ioctl: error sending start condition";
453 err_msg = write_offset(fd, i2c_opt, &cmd);
457 if (i2c_opt.mode == I2C_MODE_STOP_START) {
458 error = ioctl(fd, I2CSTOP);
460 err_msg = "error sending stop condition";
465 cmd.slave = i2c_opt.addr | 1;
469 if (i2c_opt.mode == I2C_MODE_STOP_START || i2c_opt.off_len == 0) {
470 error = ioctl(fd, I2CSTART, &cmd);
472 err_msg = "ioctl: error sending start condition";
475 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
476 error = ioctl(fd, I2CRPTSTART, &cmd);
478 err_msg = "ioctl: error sending repeated start "
484 cmd.count = i2c_opt.count;
487 error = ioctl(fd, I2CREAD, &cmd);
489 err_msg = "ioctl: error while reading";
493 error = ioctl(fd, I2CSTOP);
495 err_msg = "error sending stop condtion\n";
502 error = ioctl(fd, I2CSTOP);
504 fprintf(stderr, "error sending stop condition\n");
507 fprintf(stderr, "%s\n", err_msg);
513 * i2c_rdwr_transfer() - use I2CRDWR to conduct a complete i2c transfer.
515 * Some i2c hardware is unable to provide direct control over START, REPEAT-
516 * START, and STOP operations. Such hardware can only perform a complete
517 * START-<data>-STOP or START-<data>-REPEAT-START-<data>-STOP sequence as a
518 * single operation. The driver framework refers to this sequence as a
519 * "transfer" so we call it "transfer mode". We assemble either one or two
520 * iic_msg structures to describe the IO operations, and hand them off to the
521 * driver to be handled as a single transfer.
524 i2c_rdwr_transfer(int fd, struct options i2c_opt, char *i2c_buf)
526 struct iic_msg msgs[2], *msgp = msgs;
527 struct iic_rdwr_data xfer;
530 if (i2c_opt.off_len) {
531 msgp->flags = IIC_M_WR | IIC_M_NOSTOP;
532 msgp->slave = i2c_opt.addr;
533 msgp->buf = i2c_opt.off_buf;
534 msgp->len = i2c_opt.off_len;
536 flag = IIC_M_NOSTART;
540 * If the transfer direction is write and we did a write of the offset
541 * above, then we need to elide the start; this transfer is just more
542 * writing that follows the one started above. For a read, we always do
543 * a start; if we did an offset write above it'll be a repeat-start
544 * because of the NOSTOP flag used above.
546 if (i2c_opt.dir == 'w')
547 msgp->flags = IIC_M_WR | flag;
549 msgp->flags = IIC_M_RD;
550 msgp->slave = i2c_opt.addr;
551 msgp->len = i2c_opt.count;
556 xfer.nmsgs = msgp - msgs;
558 if (ioctl(fd, I2CRDWR, &xfer) == -1 )
559 err(1, "ioctl(I2CRDWR) failed");
565 main(int argc, char** argv)
567 struct options i2c_opt;
568 char *skip_addr = NULL, *i2c_buf;
569 const char *dev, *err_msg;
570 int fd, error, chunk_size, i, j, ch;
575 /* Line-break the output every chunk_size bytes */
581 i2c_opt.addr_set = 0;
584 i2c_opt.dir = 'r'; /* direction = read */
587 i2c_opt.binary = 0; /* ASCII text output */
588 i2c_opt.scan = 0; /* no bus scan */
589 i2c_opt.skip = 0; /* scan all addresses */
590 i2c_opt.reset = 0; /* no bus reset */
591 i2c_opt.mode = I2C_MODE_NOTSET;
593 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
596 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
597 if (i2c_opt.addr == 0 && errno == EINVAL)
598 i2c_opt.addr_set = 0;
600 i2c_opt.addr_set = 1;
606 i2c_opt.dir = optarg[0];
609 i2c_opt.off = strtoul(optarg, 0, 16);
610 if (i2c_opt.off == 0 && errno == EINVAL)
614 i2c_opt.width = optarg;
617 i2c_opt.count = atoi(optarg);
620 if (!strcmp(optarg, "no"))
621 i2c_opt.mode = I2C_MODE_NONE;
622 else if (!strcmp(optarg, "ss"))
623 i2c_opt.mode = I2C_MODE_STOP_START;
624 else if (!strcmp(optarg, "rs"))
625 i2c_opt.mode = I2C_MODE_REPEATED_START;
626 else if (!strcmp(optarg, "tr"))
627 i2c_opt.mode = I2C_MODE_TRANSFER;
655 fprintf(stderr, "Too many arguments\n");
659 /* Set default mode if option -m is not specified */
660 if (i2c_opt.mode == I2C_MODE_NOTSET) {
661 if (i2c_opt.dir == 'r')
662 i2c_opt.mode = I2C_MODE_STOP_START;
663 else if (i2c_opt.dir == 'w')
664 i2c_opt.mode = I2C_MODE_NONE;
667 if (i2c_opt.addr_set) {
668 err_msg = encode_offset(i2c_opt.width, i2c_opt.off,
669 i2c_opt.off_buf, &i2c_opt.off_len);
670 if (err_msg != NULL) {
671 fprintf(stderr, "%s", err_msg);
676 /* Basic sanity check of command line arguments */
678 if (i2c_opt.addr_set)
680 } else if (i2c_opt.reset) {
681 if (i2c_opt.addr_set)
688 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
689 "offset: 0x%02x, width: %s, count: %d\n", dev,
690 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
691 i2c_opt.width, i2c_opt.count);
693 fd = open(dev, O_RDWR);
695 fprintf(stderr, "Error opening I2C controller (%s): %s\n",
696 dev, strerror(errno));
701 exit(scan_bus(dev, fd, i2c_opt.skip, skip_addr));
704 exit(reset_bus(dev, fd));
706 i2c_buf = malloc(i2c_opt.count);
708 err(1, "data malloc");
711 * For a write, read the data to be written to the chip from stdin.
713 if (i2c_opt.dir == 'w') {
714 if (i2c_opt.verbose && !i2c_opt.binary)
715 fprintf(stderr, "Enter %d bytes of data: ",
717 for (i = 0; i < i2c_opt.count; i++) {
721 err(1, "not enough data, exiting\n");
727 if (i2c_opt.mode == I2C_MODE_TRANSFER)
728 error = i2c_rdwr_transfer(fd, i2c_opt, i2c_buf);
729 else if (i2c_opt.dir == 'w')
730 error = i2c_write(fd, i2c_opt, i2c_buf);
732 error = i2c_read(fd, i2c_opt, i2c_buf);
743 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
748 while (i < i2c_opt.count) {
749 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
751 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
753 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
754 fprintf(stdout, "%c", i2c_buf[j++]);
758 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
760 if ((i % chunk_size) == 0)
761 fprintf(stderr, "\n");
763 if ((i % chunk_size) != 0)
764 fprintf(stderr, "\n");