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