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 *);
69 static int cxm_iic_getscl(device_t);
71 static int cxm_iic_getsda(device_t);
72 static void cxm_iic_setscl(device_t, int);
73 static void cxm_iic_setsda(device_t, int);
74 static void cxm_iic_setlines(device_t, int, int);
76 static device_method_t cxm_iic_methods[] = {
77 /* Device interface */
78 DEVMETHOD(device_probe, cxm_iic_probe),
79 DEVMETHOD(device_attach, cxm_iic_attach),
80 DEVMETHOD(device_detach, cxm_iic_detach),
83 DEVMETHOD(bus_child_detached, cxm_iic_child_detached),
84 DEVMETHOD(bus_print_child, bus_generic_print_child),
85 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
88 DEVMETHOD(iicbb_callback, cxm_iic_callback),
89 DEVMETHOD(iicbb_reset, cxm_iic_reset),
90 DEVMETHOD(iicbb_getdataline, cxm_iic_getsda),
91 DEVMETHOD(iicbb_setlines, cxm_iic_setlines),
96 static driver_t cxm_iic_driver = {
99 sizeof(struct cxm_iic_softc),
102 static devclass_t cxm_iic_devclass;
104 MODULE_VERSION(cxm_iic, 1);
105 DRIVER_MODULE(cxm_iic, cxm, cxm_iic_driver, cxm_iic_devclass, 0, 0);
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)
137 bus_space_handle_t *bhandlep;
138 bus_space_tag_t *btagp;
139 struct cxm_iic_softc *sc;
142 /* Get the device data */
143 sc = device_get_softc(dev);
144 unit = device_get_unit(dev);
146 /* retrieve the cxm btag and bhandle */
147 if (BUS_READ_IVAR(device_get_parent(dev), dev,
148 CXM_IVAR_BTAG, (uintptr_t *)&btagp)
149 || BUS_READ_IVAR(device_get_parent(dev), dev,
150 CXM_IVAR_BHANDLE, (uintptr_t *)&bhandlep)) {
152 "could not retrieve bus space information\n");
157 sc->bhandle = *bhandlep;
159 /* add bit-banging generic code onto cxm_iic interface */
160 sc->iicbb = device_add_child(dev, "iicbb", -1);
163 device_printf(dev, "could not add iicbb\n");
167 /* probed and attached the bit-banging code */
168 error = device_probe_and_attach(sc->iicbb);
171 device_printf(dev, "could not attach iicbb\n");
175 /* locate iicbus which was attached by the bit-banging code */
177 device_get_children(sc->iicbb, &kids, &numkids);
178 for (i = 0; i < numkids; i++)
179 if (strcmp(device_get_name(kids[i]), "iicbus") == 0) {
186 device_printf(dev, "could not find iicbus\n");
191 if (BUS_WRITE_IVAR(device_get_parent(dev), dev,
192 CXM_IVAR_IICBUS, (uintptr_t)&iicbus)) {
193 device_printf(dev, "could not store iicbus information\n");
202 * Detach the children before recursively deleting
203 * in case a child has a pointer to a grandchild
204 * which is used by the child's detach routine.
206 * Remember the child before detaching so we can
207 * delete it (bus_generic_detach indirectly zeroes
211 bus_generic_detach(dev);
213 device_delete_child(dev, child);
220 * the detach routine.
223 cxm_iic_detach(device_t dev)
225 struct cxm_iic_softc *sc;
228 /* Get the device data */
229 sc = device_get_softc(dev);
231 BUS_WRITE_IVAR(device_get_parent(dev), dev, CXM_IVAR_IICBUS, 0);
234 * Detach the children before recursively deleting
235 * in case a child has a pointer to a grandchild
236 * which is used by the child's detach routine.
238 * Remember the child before detaching so we can
239 * delete it (bus_generic_detach indirectly zeroes
243 bus_generic_detach(dev);
245 device_delete_child(dev, child);
252 * the child detached routine.
255 cxm_iic_child_detached(device_t dev, device_t child)
257 struct cxm_iic_softc *sc;
259 /* Get the device data */
260 sc = device_get_softc(dev);
262 if (child == sc->iicbb)
268 cxm_iic_callback(device_t dev, int index, caddr_t *data)
275 cxm_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
277 struct cxm_iic_softc *sc;
279 /* Get the device data */
280 sc = (struct cxm_iic_softc *)device_get_softc(dev);
283 CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)1);
286 CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)1);
289 * PCI writes may be buffered so force the
290 * write to complete by reading the last
294 CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
296 /* Wait for 10 usec */
305 cxm_iic_getscl(device_t dev)
307 struct cxm_iic_softc *sc;
309 /* Get the device data */
310 sc = (struct cxm_iic_softc *)device_get_softc(dev);
313 return CSR_READ_1(sc, CXM_REG_I2C_GETSCL);
319 cxm_iic_getsda(device_t dev)
321 struct cxm_iic_softc *sc;
323 /* Get the device data */
324 sc = (struct cxm_iic_softc *)device_get_softc(dev);
327 return CSR_READ_1(sc, CXM_REG_I2C_GETSDA);
332 cxm_iic_setscl(device_t dev, int val)
334 struct cxm_iic_softc *sc;
336 /* Get the device data */
337 sc = (struct cxm_iic_softc *)device_get_softc(dev);
339 /* Set scl to the requested value */
340 CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)(val ? 1 : 0));
343 * PCI writes may be buffered so force the
344 * write to complete by reading the last
348 CSR_READ_4(sc, CXM_REG_I2C_SETSCL);
353 cxm_iic_setsda(device_t dev, int val)
355 struct cxm_iic_softc *sc;
357 /* Get the device data */
358 sc = (struct cxm_iic_softc *)device_get_softc(dev);
360 /* Set sda to the requested value */
361 CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)(val ? 1 : 0));
364 * PCI writes may be buffered so force the
365 * write to complete by reading the last
369 CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
374 cxm_iic_setlines(device_t dev, int ctrl, int data)
377 cxm_iic_setscl(dev, ctrl);
378 cxm_iic_setsda(dev, data);
380 /* Wait for 10 usec */