initcall: add postconsole_initcall
[barebox-mini2440.git] / commands / i2c.c
blobd38e779acb153aa9aeee2e13424c3605c0a23224
1 /*
2 * i2c.c - i2c commands
4 * Copyright (c) 2010 Eric Bénard <eric@eukrea.Com>, Eukréa Electromatique
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * MA 02111-1307 USA
22 #include <common.h>
23 #include <command.h>
24 #include <errno.h>
25 #include <malloc.h>
26 #include <getopt.h>
27 #include <i2c/i2c.h>
29 static int do_i2c_probe(struct command *cmdtp, int argc, char *argv[])
31 struct i2c_adapter *adapter;
32 struct i2c_client client;
33 int startaddr = -1, stopaddr = -1, addr, ret;
34 u8 reg;
36 if (argc < 4)
37 return COMMAND_ERROR_USAGE;
39 adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
40 if (!adapter)
41 return -ENODEV;
42 client.adapter = adapter;
44 startaddr = simple_strtol(argv[2], NULL, 0);
45 stopaddr = simple_strtol(argv[3], NULL, 0);
46 if ((startaddr == -1) || (stopaddr == -1) || (startaddr > stopaddr))
47 return COMMAND_ERROR_USAGE;
49 if (stopaddr > 0x7F)
50 stopaddr = 0x7F;
52 printf("probing i2c range 0X%02x - 0x%02x :\n", startaddr, stopaddr);
53 for (addr = startaddr; addr <= stopaddr; addr++) {
54 client.addr = addr;
55 ret = i2c_write_reg(&client, 0x00, &reg, 0);
56 if (ret == 0)
57 printf("0x%02x ", addr);
59 printf("\n");
60 return 0;
63 static const __maybe_unused char cmd_i2c_probe_help[] =
64 "Usage: i2c_probe bus 0xstartaddr 0xstopaddr\n"
65 "probe a range of i2c addresses.\n";
67 BAREBOX_CMD_START(i2c_probe)
68 .cmd = do_i2c_probe,
69 .usage = "probe for an i2c device",
70 BAREBOX_CMD_HELP(cmd_i2c_probe_help)
71 BAREBOX_CMD_END
73 static int do_i2c_write(struct command *cmdtp, int argc, char *argv[])
75 struct i2c_adapter *adapter = NULL;
76 struct i2c_client client;
77 int addr = -1, reg = -1, count = -1, verbose = 0, ret, opt, i, bus = 0;
78 u8 *buf;
80 while ((opt = getopt(argc, argv, "a:b:r:v")) > 0) {
81 switch (opt) {
82 case 'a':
83 addr = simple_strtol(optarg, NULL, 0);
84 break;
85 case 'r':
86 reg = simple_strtol(optarg, NULL, 0);
87 break;
88 case 'b':
89 bus = simple_strtoul(optarg, NULL, 0);
90 break;
91 case 'v':
92 verbose = 1;
93 break;
97 count = argc - optind;
99 if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
100 return COMMAND_ERROR_USAGE;
102 adapter = i2c_get_adapter(bus);
103 if (!adapter) {
104 printf("i2c bus %d not found\n", bus);
105 return -ENODEV;
108 client.adapter = adapter;
109 client.addr = addr;
111 buf = xmalloc(count);
112 for (i = 0; i < count; i++)
113 *(buf + i) = (char) simple_strtol(argv[optind+i], NULL, 16);
115 ret = i2c_write_reg(&client, reg, buf, count);
116 if (ret != count)
117 goto out;
118 ret = 0;
120 if (verbose) {
121 printf("wrote %i bytes starting at reg 0x%02x to i2cdev 0x%02x on bus %i\n",
122 count, reg, addr, adapter->nr);
123 for (i = 0; i < count; i++)
124 printf("0x%02x ", *(buf + i));
125 printf("\n");
128 out:
129 free(buf);
130 return ret;
133 static const __maybe_unused char cmd_i2c_write_help[] =
134 "Usage: i2c_write [OPTION] ... hexdatas\n"
135 "write to i2c device.\n"
136 " -a 0x<addr> i2c device address\n"
137 " -b <bus_num> i2c bus number (default = 0)\n"
138 " -r 0x<reg> start register\n";
140 BAREBOX_CMD_START(i2c_write)
141 .cmd = do_i2c_write,
142 .usage = "write to an i2c device",
143 BAREBOX_CMD_HELP(cmd_i2c_write_help)
144 BAREBOX_CMD_END
146 static int do_i2c_read(struct command *cmdtp, int argc, char *argv[])
148 struct i2c_adapter *adapter = NULL;
149 struct i2c_client client;
150 u8 *buf;
151 int count = -1, addr = -1, reg = -1, verbose = 0, ret, opt, bus = 0;
153 while ((opt = getopt(argc, argv, "a:b:c:r:v")) > 0) {
154 switch (opt) {
155 case 'a':
156 addr = simple_strtol(optarg, NULL, 0);
157 break;
158 case 'c':
159 count = simple_strtoul(optarg, NULL, 0);
160 break;
161 case 'b':
162 bus = simple_strtoul(optarg, NULL, 0);
163 break;
164 case 'r':
165 reg = simple_strtol(optarg, NULL, 0);
166 break;
167 case 'v':
168 verbose = 1;
169 break;
173 if ((addr < 0) || (reg < 0) || (count == 0) || (addr > 0x7F))
174 return COMMAND_ERROR_USAGE;
176 adapter = i2c_get_adapter(bus);
177 if (!adapter) {
178 printf("i2c bus %d not found\n", bus);
179 return -ENODEV;
182 client.adapter = adapter;
183 client.addr = addr;
185 buf = xmalloc(count);
186 ret = i2c_read_reg(&client, reg, buf, count);
187 if (ret == count) {
188 int i;
189 if (verbose)
190 printf("read %i bytes starting at reg 0x%02x from i2cdev 0x%02x on bus %i\n",
191 count, reg, addr, adapter->nr);
192 for (i = 0; i < count; i++)
193 printf("0x%02x ", *(buf + i));
194 printf("\n");
195 ret = 0;
198 free(buf);
199 return ret;
202 static const __maybe_unused char cmd_i2c_read_help[] =
203 "Usage: i2c_read [OPTION]\n"
204 "read i2c device.\n"
205 " -a 0x<addr> i2c device address\n"
206 " -b <bus_num> i2c bus number (default = 0)\n"
207 " -r 0x<reg> start register\n"
208 " -c <count> byte count\n";
210 BAREBOX_CMD_START(i2c_read)
211 .cmd = do_i2c_read,
212 .usage = "read from an i2c device",
213 BAREBOX_CMD_HELP(cmd_i2c_read_help)
214 BAREBOX_CMD_END