i2c_exec.c revision 1.3.18.2 1 1.3.18.2 yamt /* $NetBSD: i2c_exec.c,v 1.3.18.2 2008/01/21 09:42:50 yamt Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2003 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.3.18.2 yamt #include <sys/cdefs.h>
39 1.3.18.2 yamt __KERNEL_RCSID(0, "$NetBSD: i2c_exec.c,v 1.3.18.2 2008/01/21 09:42:50 yamt Exp $");
40 1.3.18.2 yamt
41 1.1 thorpej #include <sys/param.h>
42 1.1 thorpej #include <sys/systm.h>
43 1.1 thorpej #include <sys/device.h>
44 1.1 thorpej #include <sys/event.h>
45 1.1 thorpej #include <sys/conf.h>
46 1.1 thorpej
47 1.1 thorpej #define _I2C_PRIVATE
48 1.1 thorpej #include <dev/i2c/i2cvar.h>
49 1.1 thorpej
50 1.3.18.1 yamt static uint8_t iic_smbus_crc8(uint16_t);
51 1.3.18.1 yamt static uint8_t iic_smbus_pec(int, uint8_t *, uint8_t *);
52 1.3.18.1 yamt
53 1.1 thorpej /*
54 1.1 thorpej * iic_exec:
55 1.1 thorpej *
56 1.1 thorpej * Simplified I2C client interface engine.
57 1.1 thorpej *
58 1.1 thorpej * This and the SMBus routines are the preferred interface for
59 1.1 thorpej * client access to I2C/SMBus, since many automated controllers
60 1.1 thorpej * do not provide access to the low-level primitives of the I2C
61 1.1 thorpej * bus protocol.
62 1.1 thorpej */
63 1.1 thorpej int
64 1.1 thorpej iic_exec(i2c_tag_t tag, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
65 1.1 thorpej size_t cmdlen, void *vbuf, size_t buflen, int flags)
66 1.1 thorpej {
67 1.1 thorpej const uint8_t *cmd = vcmd;
68 1.1 thorpej uint8_t *buf = vbuf;
69 1.3 mycroft int error;
70 1.1 thorpej size_t len;
71 1.1 thorpej
72 1.3.18.1 yamt if ((flags & I2C_F_PEC) && cmdlen > 0 && tag->ic_exec != NULL) {
73 1.3.18.1 yamt uint8_t data[33]; /* XXX */
74 1.3.18.1 yamt uint8_t b[3];
75 1.3.18.1 yamt
76 1.3.18.1 yamt b[0] = addr << 1;
77 1.3.18.1 yamt b[1] = cmd[0];
78 1.3.18.1 yamt
79 1.3.18.1 yamt switch (buflen) {
80 1.3.18.1 yamt case 0:
81 1.3.18.1 yamt data[0] = iic_smbus_pec(2, b, NULL);
82 1.3.18.1 yamt buflen++;
83 1.3.18.1 yamt break;
84 1.3.18.1 yamt case 1:
85 1.3.18.1 yamt b[2] = buf[0];
86 1.3.18.1 yamt data[0] = iic_smbus_pec(3, b, NULL);
87 1.3.18.1 yamt data[1] = b[2];
88 1.3.18.1 yamt buflen++;
89 1.3.18.1 yamt break;
90 1.3.18.1 yamt case 2:
91 1.3.18.1 yamt break;
92 1.3.18.1 yamt default:
93 1.3.18.1 yamt memcpy(data, vbuf, sizeof(vbuf));
94 1.3.18.1 yamt data[sizeof(vbuf)] = iic_smbus_pec(2, b, data);
95 1.3.18.1 yamt buflen++;
96 1.3.18.1 yamt break;
97 1.3.18.1 yamt }
98 1.3.18.1 yamt
99 1.3.18.1 yamt return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
100 1.3.18.1 yamt cmdlen, data, buflen, flags));
101 1.3.18.1 yamt }
102 1.3.18.1 yamt
103 1.1 thorpej /*
104 1.1 thorpej * Defer to the controller if it provides an exec function. Use
105 1.1 thorpej * it if it does.
106 1.1 thorpej */
107 1.1 thorpej if (tag->ic_exec != NULL)
108 1.1 thorpej return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
109 1.1 thorpej cmdlen, buf, buflen, flags));
110 1.1 thorpej
111 1.1 thorpej if ((len = cmdlen) != 0) {
112 1.1 thorpej if ((error = iic_initiate_xfer(tag, addr, flags)) != 0)
113 1.1 thorpej goto bad;
114 1.1 thorpej while (len--) {
115 1.1 thorpej if ((error = iic_write_byte(tag, *cmd++, flags)) != 0)
116 1.1 thorpej goto bad;
117 1.1 thorpej }
118 1.1 thorpej }
119 1.1 thorpej
120 1.1 thorpej if (I2C_OP_READ_P(op))
121 1.1 thorpej flags |= I2C_F_READ;
122 1.1 thorpej
123 1.1 thorpej len = buflen;
124 1.1 thorpej while (len--) {
125 1.1 thorpej if (len == 0 && I2C_OP_STOP_P(op))
126 1.1 thorpej flags |= I2C_F_STOP;
127 1.1 thorpej if (I2C_OP_READ_P(op)) {
128 1.1 thorpej /* Send REPEATED START. */
129 1.1 thorpej if ((len + 1) == buflen &&
130 1.1 thorpej (error = iic_initiate_xfer(tag, addr, flags)) != 0)
131 1.1 thorpej goto bad;
132 1.1 thorpej /* NACK on last byte. */
133 1.1 thorpej if (len == 0)
134 1.1 thorpej flags |= I2C_F_LAST;
135 1.1 thorpej if ((error = iic_read_byte(tag, buf++, flags)) != 0)
136 1.1 thorpej goto bad;
137 1.1 thorpej } else {
138 1.1 thorpej /* Maybe send START. */
139 1.1 thorpej if ((len + 1) == buflen && cmdlen == 0 &&
140 1.1 thorpej (error = iic_initiate_xfer(tag, addr, flags)) != 0)
141 1.1 thorpej goto bad;
142 1.3 mycroft if ((error = iic_write_byte(tag, *buf++, flags)) != 0)
143 1.1 thorpej goto bad;
144 1.1 thorpej }
145 1.1 thorpej }
146 1.1 thorpej
147 1.1 thorpej return (0);
148 1.1 thorpej bad:
149 1.1 thorpej iic_send_stop(tag, flags);
150 1.1 thorpej return (error);
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej /*
154 1.1 thorpej * iic_smbus_write_byte:
155 1.1 thorpej *
156 1.1 thorpej * Perform an SMBus "write byte" operation.
157 1.1 thorpej */
158 1.1 thorpej int
159 1.1 thorpej iic_smbus_write_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
160 1.1 thorpej uint8_t val, int flags)
161 1.1 thorpej {
162 1.1 thorpej
163 1.1 thorpej return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1,
164 1.1 thorpej &val, 1, flags));
165 1.1 thorpej }
166 1.1 thorpej
167 1.1 thorpej /*
168 1.3.18.1 yamt * iic_smbus_write_word:
169 1.3.18.1 yamt *
170 1.3.18.1 yamt * Perform an SMBus "write word" operation.
171 1.3.18.1 yamt */
172 1.3.18.1 yamt int
173 1.3.18.1 yamt iic_smbus_write_word(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
174 1.3.18.1 yamt uint16_t val, int flags)
175 1.3.18.1 yamt {
176 1.3.18.1 yamt uint8_t vbuf[2];
177 1.3.18.1 yamt
178 1.3.18.1 yamt vbuf[0] = val & 0xff;
179 1.3.18.1 yamt vbuf[1] = (val >> 8) & 0xff;
180 1.3.18.1 yamt
181 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1,
182 1.3.18.1 yamt vbuf, 2, flags));
183 1.3.18.1 yamt }
184 1.3.18.1 yamt
185 1.3.18.1 yamt /*
186 1.1 thorpej * iic_smbus_read_byte:
187 1.1 thorpej *
188 1.1 thorpej * Perform an SMBus "read byte" operation.
189 1.1 thorpej */
190 1.1 thorpej int
191 1.1 thorpej iic_smbus_read_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
192 1.1 thorpej uint8_t *valp, int flags)
193 1.1 thorpej {
194 1.1 thorpej
195 1.1 thorpej return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1,
196 1.1 thorpej valp, 1, flags));
197 1.1 thorpej }
198 1.1 thorpej
199 1.1 thorpej /*
200 1.3.18.1 yamt * iic_smbus_read_word:
201 1.3.18.1 yamt *
202 1.3.18.1 yamt * Perform an SMBus "read word" operation.
203 1.3.18.1 yamt */
204 1.3.18.1 yamt int
205 1.3.18.1 yamt iic_smbus_read_word(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
206 1.3.18.1 yamt uint16_t *valp, int flags)
207 1.3.18.1 yamt {
208 1.3.18.1 yamt
209 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1,
210 1.3.18.1 yamt (uint8_t *)valp, 2, flags));
211 1.3.18.1 yamt }
212 1.3.18.1 yamt
213 1.3.18.1 yamt /*
214 1.1 thorpej * iic_smbus_receive_byte:
215 1.1 thorpej *
216 1.1 thorpej * Perform an SMBus "receive byte" operation.
217 1.1 thorpej */
218 1.1 thorpej int
219 1.1 thorpej iic_smbus_receive_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t *valp,
220 1.1 thorpej int flags)
221 1.1 thorpej {
222 1.1 thorpej
223 1.1 thorpej return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
224 1.1 thorpej valp, 1, flags));
225 1.1 thorpej }
226 1.3.18.1 yamt
227 1.3.18.1 yamt /*
228 1.3.18.1 yamt * iic_smbus_send_byte:
229 1.3.18.1 yamt *
230 1.3.18.1 yamt * Perform an SMBus "send byte" operation.
231 1.3.18.1 yamt */
232 1.3.18.1 yamt int
233 1.3.18.1 yamt iic_smbus_send_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t val, int flags)
234 1.3.18.1 yamt {
235 1.3.18.1 yamt
236 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, NULL, 0,
237 1.3.18.1 yamt &val, 1, flags));
238 1.3.18.1 yamt }
239 1.3.18.1 yamt
240 1.3.18.1 yamt /*
241 1.3.18.1 yamt * iic_smbus_quick_read:
242 1.3.18.1 yamt *
243 1.3.18.1 yamt * Perform an SMBus "quick read" operation.
244 1.3.18.1 yamt */
245 1.3.18.1 yamt int
246 1.3.18.1 yamt iic_smbus_quick_read(i2c_tag_t tag, i2c_addr_t addr, int flags)
247 1.3.18.1 yamt {
248 1.3.18.1 yamt
249 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
250 1.3.18.1 yamt NULL, 0, flags));
251 1.3.18.1 yamt }
252 1.3.18.1 yamt
253 1.3.18.1 yamt /*
254 1.3.18.1 yamt * iic_smbus_quick_write:
255 1.3.18.1 yamt *
256 1.3.18.1 yamt * Perform an SMBus "quick write" operation.
257 1.3.18.1 yamt */
258 1.3.18.1 yamt int
259 1.3.18.1 yamt iic_smbus_quick_write(i2c_tag_t tag, i2c_addr_t addr, int flags)
260 1.3.18.1 yamt {
261 1.3.18.1 yamt
262 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, NULL, 0,
263 1.3.18.1 yamt NULL, 0, flags));
264 1.3.18.1 yamt }
265 1.3.18.1 yamt
266 1.3.18.1 yamt /*
267 1.3.18.1 yamt * iic_smbus_block_read:
268 1.3.18.1 yamt *
269 1.3.18.1 yamt * Perform an SMBus "block read" operation.
270 1.3.18.1 yamt */
271 1.3.18.1 yamt int
272 1.3.18.1 yamt iic_smbus_block_read(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
273 1.3.18.1 yamt uint8_t *vbuf, size_t buflen, int flags)
274 1.3.18.1 yamt {
275 1.3.18.1 yamt
276 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1,
277 1.3.18.1 yamt vbuf, buflen, flags));
278 1.3.18.1 yamt }
279 1.3.18.1 yamt
280 1.3.18.1 yamt /*
281 1.3.18.1 yamt * iic_smbus_block_write:
282 1.3.18.1 yamt *
283 1.3.18.1 yamt * Perform an SMBus "block write" operation.
284 1.3.18.1 yamt */
285 1.3.18.1 yamt int
286 1.3.18.1 yamt iic_smbus_block_write(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
287 1.3.18.1 yamt uint8_t *vbuf, size_t buflen, int flags)
288 1.3.18.1 yamt {
289 1.3.18.1 yamt
290 1.3.18.1 yamt return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1,
291 1.3.18.1 yamt vbuf, buflen, flags));
292 1.3.18.1 yamt }
293 1.3.18.1 yamt
294 1.3.18.1 yamt /*
295 1.3.18.1 yamt * iic_smbus_crc8
296 1.3.18.1 yamt *
297 1.3.18.1 yamt * Private helper for calculating packet error checksum
298 1.3.18.1 yamt */
299 1.3.18.1 yamt static uint8_t
300 1.3.18.1 yamt iic_smbus_crc8(uint16_t data)
301 1.3.18.1 yamt {
302 1.3.18.1 yamt int i;
303 1.3.18.1 yamt
304 1.3.18.1 yamt for (i = 0; i < 8; i++) {
305 1.3.18.1 yamt if (data & 0x8000)
306 1.3.18.1 yamt data = data ^ (0x1070U << 3);
307 1.3.18.1 yamt data = data << 1;
308 1.3.18.1 yamt }
309 1.3.18.1 yamt
310 1.3.18.1 yamt return (uint8_t)(data >> 8);
311 1.3.18.1 yamt }
312 1.3.18.1 yamt
313 1.3.18.1 yamt /*
314 1.3.18.1 yamt * iic_smbus_pec
315 1.3.18.1 yamt *
316 1.3.18.1 yamt * Private function for calculating packet error checking on SMBus
317 1.3.18.1 yamt * packets.
318 1.3.18.1 yamt */
319 1.3.18.1 yamt static uint8_t
320 1.3.18.1 yamt iic_smbus_pec(int count, uint8_t *s, uint8_t *r)
321 1.3.18.1 yamt {
322 1.3.18.1 yamt int i;
323 1.3.18.1 yamt uint8_t crc = 0;
324 1.3.18.1 yamt
325 1.3.18.1 yamt for (i = 0; i < count; i++)
326 1.3.18.1 yamt crc = iic_smbus_crc8((crc ^ s[i]) << 8);
327 1.3.18.1 yamt if (r != NULL)
328 1.3.18.1 yamt for (i = 0; i <= r[0]; i++)
329 1.3.18.1 yamt crc = iic_smbus_crc8((crc ^ r[i]) << 8);
330 1.3.18.1 yamt
331 1.3.18.1 yamt return crc;
332 1.3.18.1 yamt }
333