usr.sbin/makefs/ffs: Remove m_buf::b_is_hammer2
[dragonfly.git] / sys / bus / iicbus / iicsmb.c
blob8d2d5dc902b59ec8004fdc1b419357a8655e7b6f
1 /*-
2 * Copyright (c) 1998, 2001 Nicolas Souchu
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 THE 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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/iicbus/iicsmb.c,v 1.18 2009/02/10 22:50:23 imp Exp $
30 * I2C to SMB bridge
32 * Example:
34 * smb bttv
35 * \ /
36 * smbus
37 * / \
38 * iicsmb bti2c
39 * |
40 * iicbus
41 * / | \
42 * iicbb pcf ...
43 * |
44 * lpbb
47 #include <sys/param.h>
48 #include <sys/bus.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
54 #include <bus/iicbus/iiconf.h>
55 #include <bus/iicbus/iicbus.h>
57 #include <bus/smbus/smbconf.h>
59 #include "iicbus_if.h"
60 #include "smbus_if.h"
62 struct iicsmb_softc {
64 #define SMB_WAITING_ADDR 0x0
65 #define SMB_WAITING_LOW 0x1
66 #define SMB_WAITING_HIGH 0x2
67 #define SMB_DONE 0x3
68 int state;
70 u_char devaddr; /* slave device address */
72 char low; /* low byte received first */
73 char high; /* high byte */
75 device_t smbus;
78 static int iicsmb_probe(device_t);
79 static int iicsmb_attach(device_t);
80 static int iicsmb_detach(device_t);
81 static void iicsmb_identify(driver_t *driver, device_t parent);
83 static int iicsmb_intr(device_t dev, int event, char *buf);
84 static int iicsmb_callback(device_t dev, int index, void *data);
85 static int iicsmb_quick(device_t dev, u_char slave, int how);
86 static int iicsmb_sendb(device_t dev, u_char slave, char byte);
87 static int iicsmb_recvb(device_t dev, u_char slave, char *byte);
88 static int iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte);
89 static int iicsmb_writew(device_t dev, u_char slave, char cmd, short word);
90 static int iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte);
91 static int iicsmb_readw(device_t dev, u_char slave, char cmd, short *word);
92 static int iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata);
93 static int iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
94 static int iicsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf);
96 static devclass_t iicsmb_devclass;
98 static device_method_t iicsmb_methods[] = {
99 /* device interface */
100 DEVMETHOD(device_identify, iicsmb_identify),
101 DEVMETHOD(device_probe, iicsmb_probe),
102 DEVMETHOD(device_attach, iicsmb_attach),
103 DEVMETHOD(device_detach, iicsmb_detach),
105 /* bus interface */
106 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
107 DEVMETHOD(bus_print_child, bus_generic_print_child),
109 /* iicbus interface */
110 DEVMETHOD(iicbus_intr, iicsmb_intr),
112 /* smbus interface */
113 DEVMETHOD(smbus_callback, iicsmb_callback),
114 DEVMETHOD(smbus_quick, iicsmb_quick),
115 DEVMETHOD(smbus_sendb, iicsmb_sendb),
116 DEVMETHOD(smbus_recvb, iicsmb_recvb),
117 DEVMETHOD(smbus_writeb, iicsmb_writeb),
118 DEVMETHOD(smbus_writew, iicsmb_writew),
119 DEVMETHOD(smbus_readb, iicsmb_readb),
120 DEVMETHOD(smbus_readw, iicsmb_readw),
121 DEVMETHOD(smbus_pcall, iicsmb_pcall),
122 DEVMETHOD(smbus_bwrite, iicsmb_bwrite),
123 DEVMETHOD(smbus_bread, iicsmb_bread),
125 DEVMETHOD_END
128 static driver_t iicsmb_driver = {
129 "iicsmb",
130 iicsmb_methods,
131 sizeof(struct iicsmb_softc),
134 #define IICBUS_TIMEOUT 100 /* us */
136 static void
137 iicsmb_identify(driver_t *driver, device_t parent)
140 if (device_find_child(parent, "iicsmb", -1) == NULL)
141 BUS_ADD_CHILD(parent, parent, 0, "iicsmb", -1);
144 static int
145 iicsmb_probe(device_t dev)
147 device_set_desc(dev, "SMBus over I2C bridge");
148 return (0);
151 static int
152 iicsmb_attach(device_t dev)
154 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
156 sc->smbus = device_add_child(dev, "smbus", -1);
158 /* probe and attach the smbus */
159 bus_generic_attach(dev);
161 return (0);
164 static int
165 iicsmb_detach(device_t dev)
167 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
169 bus_generic_detach(dev);
170 if (sc->smbus) {
171 device_delete_child(dev, sc->smbus);
174 return (0);
178 * iicsmb_intr()
180 * iicbus interrupt handler
182 static int
183 iicsmb_intr(device_t dev, int event, char *buf)
185 struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
187 switch (event) {
188 case INTR_GENERAL:
189 case INTR_START:
190 sc->state = SMB_WAITING_ADDR;
191 break;
193 case INTR_STOP:
194 /* call smbus intr handler */
195 smbus_intr(sc->smbus, sc->devaddr,
196 sc->low, sc->high, SMB_ENOERR);
197 break;
199 case INTR_RECEIVE:
200 switch (sc->state) {
201 case SMB_DONE:
202 /* XXX too much data, discard */
203 kprintf("%s: too much data from 0x%x\n", __func__,
204 sc->devaddr & 0xff);
205 goto end;
207 case SMB_WAITING_ADDR:
208 sc->devaddr = (u_char)*buf;
209 sc->state = SMB_WAITING_LOW;
210 break;
212 case SMB_WAITING_LOW:
213 sc->low = *buf;
214 sc->state = SMB_WAITING_HIGH;
215 break;
217 case SMB_WAITING_HIGH:
218 sc->high = *buf;
219 sc->state = SMB_DONE;
220 break;
222 end:
223 break;
225 case INTR_TRANSMIT:
226 case INTR_NOACK:
227 break;
229 case INTR_ERROR:
230 switch (*buf) {
231 case IIC_EBUSERR:
232 smbus_intr(sc->smbus, sc->devaddr, 0, 0, SMB_EBUSERR);
233 break;
235 default:
236 kprintf("%s unknown error 0x%x!\n", __func__,
237 (int)*buf);
238 break;
240 break;
242 default:
243 panic("%s: unknown event (%d)!", __func__, event);
246 return (0);
249 static int
250 iicsmb_callback(device_t dev, int index, void *data)
252 device_t parent = device_get_parent(dev);
253 int error = 0;
254 int how;
256 switch (index) {
257 case SMB_REQUEST_BUS:
258 /* request underlying iicbus */
259 how = *(int *)data;
260 error = iicbus_request_bus(parent, dev, how);
261 break;
263 case SMB_RELEASE_BUS:
264 /* release underlying iicbus */
265 error = iicbus_release_bus(parent, dev);
266 break;
268 default:
269 error = EINVAL;
272 return (error);
275 static int
276 iicsmb_quick(device_t dev, u_char slave, int how)
278 device_t parent = device_get_parent(dev);
279 int error;
281 switch (how) {
282 case SMB_QWRITE:
283 error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT);
284 break;
286 case SMB_QREAD:
287 error = iicbus_start(parent, slave | LSB, IICBUS_TIMEOUT);
288 break;
290 default:
291 error = EINVAL;
292 break;
295 if (!error)
296 error = iicbus_stop(parent);
298 return (error);
301 static int
302 iicsmb_sendb(device_t dev, u_char slave, char byte)
304 device_t parent = device_get_parent(dev);
305 int error, sent;
307 error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT);
309 if (!error) {
310 error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT);
312 iicbus_stop(parent);
315 return (error);
318 static int
319 iicsmb_recvb(device_t dev, u_char slave, char *byte)
321 device_t parent = device_get_parent(dev);
322 int error, read;
324 error = iicbus_start(parent, slave | LSB, 0);
326 if (!error) {
327 error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT);
329 iicbus_stop(parent);
332 return (error);
335 static int
336 iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
338 device_t parent = device_get_parent(dev);
339 int error, sent;
341 error = iicbus_start(parent, slave & ~LSB, 0);
343 if (!error) {
344 if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
345 error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT);
347 iicbus_stop(parent);
350 return (error);
353 static int
354 iicsmb_writew(device_t dev, u_char slave, char cmd, short word)
356 device_t parent = device_get_parent(dev);
357 int error, sent;
359 char low = (char)(word & 0xff);
360 char high = (char)((word & 0xff00) >> 8);
362 error = iicbus_start(parent, slave & ~LSB, 0);
364 if (!error) {
365 if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
366 if (!(error = iicbus_write(parent, &low, 1, &sent, IICBUS_TIMEOUT)))
367 error = iicbus_write(parent, &high, 1, &sent, IICBUS_TIMEOUT);
369 iicbus_stop(parent);
372 return (error);
375 static int
376 iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
378 device_t parent = device_get_parent(dev);
379 int error, sent, read;
381 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
382 return (error);
384 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
385 goto error;
387 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
388 goto error;
390 if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
391 goto error;
393 error:
394 iicbus_stop(parent);
395 return (error);
398 #define BUF2SHORT(low,high) \
399 ((short)(((high) & 0xff) << 8) | (short)((low) & 0xff))
401 static int
402 iicsmb_readw(device_t dev, u_char slave, char cmd, short *word)
404 device_t parent = device_get_parent(dev);
405 int error, sent, read;
406 char buf[2];
408 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
409 return (error);
411 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
412 goto error;
414 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
415 goto error;
417 if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
418 goto error;
420 /* first, receive low, then high byte */
421 *word = BUF2SHORT(buf[0], buf[1]);
423 error:
424 iicbus_stop(parent);
425 return (error);
428 static int
429 iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
431 device_t parent = device_get_parent(dev);
432 int error, sent, read;
433 char buf[2];
435 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
436 return (error);
438 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
439 goto error;
441 /* first, send low, then high byte */
442 buf[0] = (char)(sdata & 0xff);
443 buf[1] = (char)((sdata & 0xff00) >> 8);
445 if ((error = iicbus_write(parent, buf, 2, &sent, IICBUS_TIMEOUT)))
446 goto error;
448 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
449 goto error;
451 if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
452 goto error;
454 /* first, receive low, then high byte */
455 *rdata = BUF2SHORT(buf[0], buf[1]);
457 error:
458 iicbus_stop(parent);
459 return (error);
462 static int
463 iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
465 device_t parent = device_get_parent(dev);
466 int error, sent;
468 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
469 goto error;
471 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
472 goto error;
474 if ((error = iicbus_write(parent, buf, (int)count, &sent, IICBUS_TIMEOUT)))
475 goto error;
477 if ((error = iicbus_stop(parent)))
478 goto error;
480 error:
481 return (error);
484 static int
485 iicsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
487 device_t parent = device_get_parent(dev);
488 int error, sent, read;
490 if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
491 return (error);
493 if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
494 goto error;
496 if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
497 goto error;
499 if ((error = iicbus_read(parent, buf, (int)*count, &read,
500 IIC_LAST_READ, IICBUS_TIMEOUT)))
501 goto error;
502 *count = read;
504 error:
505 iicbus_stop(parent);
506 return (error);
509 DRIVER_MODULE(iicsmb, iicbus, iicsmb_driver, iicsmb_devclass, NULL, NULL);
510 DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, NULL, NULL);
511 MODULE_DEPEND(iicsmb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
512 MODULE_DEPEND(iicsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
513 MODULE_VERSION(iicsmb, 1);