2 * Copyright (c) 2003, 2004, 2005
3 * John Wehle <john@feith.com>. All rights reserved.
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Wehle.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * I2c routines for the Conexant MPEG-2 Codec driver.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/select.h>
45 #include <sys/resource.h>
49 #include <machine/clock.h>
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pcivar.h>
54 #include <dev/video/cxm/cxm.h>
56 #include <bus/iicbus/iiconf.h>
61 static int cxm_iic_probe(device_t dev);
62 static int cxm_iic_attach(device_t dev);
63 static int cxm_iic_detach(device_t dev);
64 static void cxm_iic_child_detached(device_t dev, device_t child);
66 static int cxm_iic_callback(device_t, int, caddr_t *);
67 static int cxm_iic_reset(device_t, u_char, u_char, u_char *);
68 static int cxm_iic_getscl(device_t);
69 static int cxm_iic_getsda(device_t);
70 static void cxm_iic_setscl(device_t, int);
71 static void cxm_iic_setsda(device_t, int);
73 static device_method_t cxm_iic_methods[] = {
74 /* Device interface */
75 DEVMETHOD(device_probe, cxm_iic_probe),
76 DEVMETHOD(device_attach, cxm_iic_attach),
77 DEVMETHOD(device_detach, cxm_iic_detach),
80 DEVMETHOD(bus_child_detached, cxm_iic_child_detached),
81 DEVMETHOD(bus_print_child, bus_generic_print_child),
82 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
85 DEVMETHOD(iicbb_callback, cxm_iic_callback),
86 DEVMETHOD(iicbb_reset, cxm_iic_reset),
87 DEVMETHOD(iicbb_getscl, cxm_iic_getscl),
88 DEVMETHOD(iicbb_getsda, cxm_iic_getsda),
89 DEVMETHOD(iicbb_setscl, cxm_iic_setscl),
90 DEVMETHOD(iicbb_setsda, cxm_iic_setsda),
95 static driver_t cxm_iic_driver = {
98 sizeof(struct cxm_iic_softc),
101 static devclass_t cxm_iic_devclass;
103 DRIVER_MODULE(cxm_iic, cxm, cxm_iic_driver, cxm_iic_devclass, NULL, NULL);
104 MODULE_VERSION(cxm_iic, 1);
105 MODULE_DEPEND(cxm_iic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
109 * the boot time probe routine.
111 * The cxm_iic device is only probed after it has
112 * been established that the cxm device is present
113 * which means that the cxm_iic device * must *
114 * be present since it's built into the cxm hardware.
117 cxm_iic_probe(device_t dev)
119 device_set_desc(dev, "Conexant iTVC15 / iTVC16 I2C controller");
126 * the attach routine.
129 cxm_iic_attach(device_t dev)
136 bus_space_handle_t *bhandlep;
137 bus_space_tag_t *btagp;
138 struct cxm_iic_softc *sc;
141 /* Get the device data */
142 sc = device_get_softc(dev);
144 /* retrieve the cxm btag and bhandle */
145 if (BUS_READ_IVAR(device_get_parent(dev), dev,
146 CXM_IVAR_BTAG, (uintptr_t *)&btagp)
147 || BUS_READ_IVAR(device_get_parent(dev), dev,
148 CXM_IVAR_BHANDLE, (uintptr_t *)&bhandlep)) {
150 "could not retrieve bus space information\n");
155 sc->bhandle = *bhandlep;
157 /* add bit-banging generic code onto cxm_iic interface */
158 sc->iicbb = device_add_child(dev, "iicbb", -1);
161 device_printf(dev, "could not add iicbb\n");
165 /* probed and attached the bit-banging code */
166 error = device_probe_and_attach(sc->iicbb);
169 device_printf(dev, "could not attach iicbb\n");
173 /* locate iicbus which was attached by the bit-banging code */
175 device_get_children(sc->iicbb, &kids, &numkids);
176 for (i = 0; i < numkids; i++)
177 if (strcmp(device_get_name(kids[i]), "iicbus") == 0) {
184 device_printf(dev, "could not find iicbus\n");
189 if (BUS_WRITE_IVAR(device_get_parent(dev), dev,
190 CXM_IVAR_IICBUS, (uintptr_t)&iicbus)) {
191 device_printf(dev, "could not store iicbus information\n");
200 * Detach the children before recursively deleting
201 * in case a child has a pointer to a grandchild
202 * which is used by the child's detach routine.
204 * Remember the child before detaching so we can
205 * delete it (bus_generic_detach indirectly zeroes
209 bus_generic_detach(dev);
211 device_delete_child(dev, child);
218 * the detach routine.
221 cxm_iic_detach(device_t dev)
223 struct cxm_iic_softc *sc;
226 /* Get the device data */
227 sc = device_get_softc(dev);
229 BUS_WRITE_IVAR(device_get_parent(dev), dev, CXM_IVAR_IICBUS, 0);
232 * Detach the children before recursively deleting
233 * in case a child has a pointer to a grandchild
234 * which is used by the child's detach routine.
236 * Remember the child before detaching so we can
237 * delete it (bus_generic_detach indirectly zeroes
241 bus_generic_detach(dev);
243 device_delete_child(dev, child);
250 * the child detached routine.
253 cxm_iic_child_detached(device_t dev, device_t child)
255 struct cxm_iic_softc *sc;
257 /* Get the device data */
258 sc = device_get_softc(dev);
260 if (child == sc->iicbb)
266 cxm_iic_callback(device_t dev, int index, caddr_t *data)
273 cxm_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
275 struct cxm_iic_softc *sc;
277 /* Get the device data */
278 sc = (struct cxm_iic_softc *)device_get_softc(dev);
281 CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)1);
284 CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)1);
287 * PCI writes may be buffered so force the
288 * write to complete by reading the last
292 CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
294 /* Wait for 10 usec */
302 cxm_iic_getscl(device_t dev)
304 struct cxm_iic_softc *sc;
306 /* Get the device data */
307 sc = (struct cxm_iic_softc *)device_get_softc(dev);
310 return CSR_READ_1(sc, CXM_REG_I2C_GETSCL);
315 cxm_iic_getsda(device_t dev)
317 struct cxm_iic_softc *sc;
319 /* Get the device data */
320 sc = (struct cxm_iic_softc *)device_get_softc(dev);
323 return CSR_READ_1(sc, CXM_REG_I2C_GETSDA);
328 cxm_iic_setscl(device_t dev, int val)
330 struct cxm_iic_softc *sc;
332 /* Get the device data */
333 sc = (struct cxm_iic_softc *)device_get_softc(dev);
335 /* Set scl to the requested value */
336 CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)(val ? 1 : 0));
339 * PCI writes may be buffered so force the
340 * write to complete by reading the last
344 CSR_READ_4(sc, CXM_REG_I2C_SETSCL);
349 cxm_iic_setsda(device_t dev, int val)
351 struct cxm_iic_softc *sc;
353 /* Get the device data */
354 sc = (struct cxm_iic_softc *)device_get_softc(dev);
356 /* Set sda to the requested value */
357 CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)(val ? 1 : 0));
360 * PCI writes may be buffered so force the
361 * write to complete by reading the last
365 CSR_READ_4(sc, CXM_REG_I2C_SETSDA);