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");
284 * Write offset where the data will go
286 cmd.slave = i2c_opt.addr;
287 error = ioctl(fd, I2CSTART, &cmd);
289 err_msg = "ioctl: error sending start condition";
294 bufsize = i2c_opt.width / 8;
295 buf = prepare_buf(bufsize, i2c_opt.off);
297 err_msg = "error: offset malloc";
303 error = ioctl(fd, I2CWRITE, &cmd);
306 err_msg = "ioctl: error when write offset";
311 /* Mode - stop start */
312 if (i2c_opt.mode == I2C_MODE_STOP_START) {
313 cmd.slave = i2c_opt.addr;
314 error = ioctl(fd, I2CSTOP, &cmd);
316 err_msg = "ioctl: error sending stop condition";
319 cmd.slave = i2c_opt.addr;
320 error = ioctl(fd, I2CSTART, &cmd);
322 err_msg = "ioctl: error sending start condition";
326 /* Mode - repeated start */
327 if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
328 cmd.slave = i2c_opt.addr;
329 error = ioctl(fd, I2CRPTSTART, &cmd);
331 err_msg = "ioctl: error sending repeated start "
340 cmd.count = i2c_opt.count;
343 error = ioctl(fd, I2CWRITE, &cmd);
345 err_msg = "ioctl: error when write";
348 cmd.slave = i2c_opt.addr;
349 error = ioctl(fd, I2CSTOP, &cmd);
351 err_msg = "ioctl: error sending stop condition";
359 cmd.slave = i2c_opt.addr;
360 error = ioctl(fd, I2CSTOP, &cmd);
362 fprintf(stderr, "error sending stop condtion\n");
365 fprintf(stderr, "%s", err_msg);
372 i2c_read(char *dev, struct options i2c_opt, char *i2c_buf)
375 int i, fd, error, bufsize;
376 char *err_msg, data = 0, *buf;
378 fd = open(dev, O_RDWR);
380 err(1, "open failed");
382 bzero(&cmd, sizeof(cmd));
385 cmd.slave = i2c_opt.addr;
389 error = ioctl(fd, I2CSTART, &cmd);
391 err_msg = "ioctl: error sending start condition";
394 bufsize = i2c_opt.width / 8;
395 buf = prepare_buf(bufsize, i2c_opt.off);
397 err_msg = "error: offset malloc";
404 error = ioctl(fd, I2CWRITE, &cmd);
407 err_msg = "ioctl: error when write offset";
411 if (i2c_opt.mode == I2C_MODE_STOP_START) {
412 cmd.slave = i2c_opt.addr;
413 error = ioctl(fd, I2CSTOP, &cmd);
415 err_msg = "error sending stop condtion\n";
420 cmd.slave = i2c_opt.addr;
424 if (i2c_opt.mode == I2C_MODE_STOP_START) {
425 error = ioctl(fd, I2CSTART, &cmd);
427 err_msg = "ioctl: error sending start condition";
430 } else if (i2c_opt.mode == I2C_MODE_REPEATED_START) {
431 error = ioctl(fd, I2CRPTSTART, &cmd);
433 err_msg = "ioctl: error sending repeated start "
438 error = ioctl(fd, I2CSTOP, &cmd);
440 err_msg = "error sending stop condtion\n";
444 for (i = 0; i < i2c_opt.count; i++) {
445 error = read(fd, &i2c_buf[i], 1);
447 err_msg = "ioctl: error while reading";
456 cmd.slave = i2c_opt.addr;
457 error = ioctl(fd, I2CSTOP, &cmd);
459 fprintf(stderr, "error sending stop condtion\n");
462 fprintf(stderr, "%s", err_msg);
469 main(int argc, char** argv)
472 struct options i2c_opt;
473 char *dev, *skip_addr, *i2c_buf;
474 int error, chunk_size, i, j, ch;
479 /* Line-break the output every chunk_size bytes */
485 i2c_opt.addr_set = 0;
488 i2c_opt.dir = 'r'; /* direction = read */
491 i2c_opt.binary = 0; /* ASCII text output */
492 i2c_opt.scan = 0; /* no bus scan */
493 i2c_opt.skip = 0; /* scan all addresses */
494 i2c_opt.reset = 0; /* no bus reset */
495 i2c_opt.mode = I2C_MODE_NOTSET;
497 while ((ch = getopt(argc, argv, "a:f:d:o:w:c:m:n:sbvrh")) != -1) {
500 i2c_opt.addr = (strtoul(optarg, 0, 16) << 1);
501 if (i2c_opt.addr == 0 && errno == EINVAL)
502 i2c_opt.addr_set = 0;
504 i2c_opt.addr_set = 1;
510 i2c_opt.dir = optarg[0];
513 i2c_opt.off = strtoul(optarg, 0, 16);
514 if (i2c_opt.off == 0 && errno == EINVAL)
518 i2c_opt.width = atoi(optarg);
521 i2c_opt.count = atoi(optarg);
524 if (!strcmp(optarg, "no"))
525 i2c_opt.mode = I2C_MODE_NONE;
526 else if (!strcmp(optarg, "ss"))
527 i2c_opt.mode = I2C_MODE_STOP_START;
528 else if (!strcmp(optarg, "rs"))
529 i2c_opt.mode = I2C_MODE_REPEATED_START;
557 /* Set default mode if option -m is not specified */
558 if (i2c_opt.mode == I2C_MODE_NOTSET) {
559 if (i2c_opt.dir == 'r')
560 i2c_opt.mode = I2C_MODE_STOP_START;
561 else if (i2c_opt.dir == 'w')
562 i2c_opt.mode = I2C_MODE_NONE;
565 /* Basic sanity check of command line arguments */
567 if (i2c_opt.addr_set)
569 } else if (i2c_opt.reset) {
570 if (i2c_opt.addr_set)
574 } else if ((i2c_opt.dir == 'r' || i2c_opt.dir == 'w')) {
575 if ((i2c_opt.addr_set == 0) ||
576 !(i2c_opt.width == 0 || i2c_opt.width == 8 ||
577 i2c_opt.width == 16))
582 fprintf(stderr, "dev: %s, addr: 0x%x, r/w: %c, "
583 "offset: 0x%02x, width: %u, count: %u\n", dev,
584 i2c_opt.addr >> 1, i2c_opt.dir, i2c_opt.off,
585 i2c_opt.width, i2c_opt.count);
588 exit(scan_bus(cmd, dev, i2c_opt.skip, skip_addr));
591 exit(reset_bus(cmd, dev));
593 i2c_buf = malloc(i2c_opt.count);
595 err(1, "data malloc");
597 if (i2c_opt.dir == 'w') {
598 error = i2c_write(dev, i2c_opt, i2c_buf);
604 if (i2c_opt.dir == 'r') {
605 error = i2c_read(dev, i2c_opt, i2c_buf);
613 fprintf(stderr, "\nData %s (hex):\n", i2c_opt.dir == 'r' ?
618 while (i < i2c_opt.count) {
619 if (i2c_opt.verbose || (i2c_opt.dir == 'r' &&
621 fprintf (stderr, "%02hhx ", i2c_buf[i++]);
623 if (i2c_opt.dir == 'r' && i2c_opt.binary) {
624 fprintf(stdout, "%c", i2c_buf[j++]);
628 if (!i2c_opt.verbose && (i2c_opt.dir == 'w'))
630 if ((i % chunk_size) == 0)
631 fprintf(stderr, "\n");
633 if ((i % chunk_size) != 0)
634 fprintf(stderr, "\n");