xref: /dragonfly/sys/dev/video/cxm/cxm_i2c.c (revision 0fe46dc6)
1 /*
2  * Copyright (c) 2003, 2004, 2005
3  *	John Wehle <john@feith.com>.  All rights reserved.
4  *
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.
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.
18  *
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.
30  */
31 
32 /*
33  * I2c routines for the Conexant MPEG-2 Codec driver.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/uio.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/poll.h>
44 #include <sys/select.h>
45 #include <sys/resource.h>
46 #include <sys/bus.h>
47 #include <sys/rman.h>
48 
49 #include <machine/clock.h>
50 
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pcivar.h>
53 
54 #include <dev/video/cxm/cxm.h>
55 
56 #include <bus/iicbus/iiconf.h>
57 
58 #include "iicbb_if.h"
59 
60 
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);
65 
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);
72 
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),
78 
79 	/* bus interface */
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),
83 
84 	/* iicbb interface */
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),
91 
92 	DEVMETHOD_END
93 };
94 
95 static driver_t cxm_iic_driver = {
96 	"cxm_iic",
97 	cxm_iic_methods,
98 	sizeof(struct cxm_iic_softc),
99 };
100 
101 static devclass_t cxm_iic_devclass;
102 
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);
106 
107 
108 /*
109  * the boot time probe routine.
110  *
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.
115  */
116 static int
117 cxm_iic_probe(device_t dev)
118 {
119 	device_set_desc(dev, "Conexant iTVC15 / iTVC16 I2C controller");
120 
121 	return 0;
122 }
123 
124 
125 /*
126  * the attach routine.
127  */
128 static int
129 cxm_iic_attach(device_t dev)
130 {
131 	device_t *kids;
132 	device_t iicbus;
133 	int error;
134 	int numkids;
135 	int i;
136 	bus_space_handle_t *bhandlep;
137 	bus_space_tag_t *btagp;
138 	struct cxm_iic_softc *sc;
139 	device_t child;
140 
141 	/* Get the device data */
142 	sc = device_get_softc(dev);
143 
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)) {
149 		device_printf(dev,
150 			      "could not retrieve bus space information\n");
151 		return ENXIO;
152 	}
153 
154 	sc->btag = *btagp;
155 	sc->bhandle = *bhandlep;
156 
157 	/* add bit-banging generic code onto cxm_iic interface */
158 	sc->iicbb = device_add_child(dev, "iicbb", -1);
159 
160 	if (!sc->iicbb) {
161 		device_printf(dev, "could not add iicbb\n");
162 		return ENXIO;
163 	}
164 
165 	/* probed and attached the bit-banging code */
166 	error = device_probe_and_attach(sc->iicbb);
167 
168 	if (error) {
169 		device_printf(dev, "could not attach iicbb\n");
170 		goto fail;
171 	}
172 
173 	/* locate iicbus which was attached by the bit-banging code */
174 	iicbus = NULL;
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) {
178 			iicbus = kids[i];
179 			break;
180 		}
181 	kfree(kids, M_TEMP);
182 
183 	if (!iicbus) {
184 		device_printf(dev, "could not find iicbus\n");
185 		error = ENXIO;
186 		goto fail;
187 	}
188 
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");
192 		error = ENXIO;
193 		goto fail;
194 	}
195 
196 	return 0;
197 
198 fail:
199 	/*
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.
203 	 *
204 	 * Remember the child before detaching so we can
205 	 * delete it (bus_generic_detach indirectly zeroes
206 	 * sc->child_dev).
207 	 */
208 	child = sc->iicbb;
209 	bus_generic_detach(dev);
210 	if (child)
211 		device_delete_child(dev, child);
212 
213 	return error;
214 }
215 
216 
217 /*
218  * the detach routine.
219  */
220 static int
221 cxm_iic_detach(device_t dev)
222 {
223 	struct cxm_iic_softc *sc;
224 	device_t child;
225 
226 	/* Get the device data */
227 	sc = device_get_softc(dev);
228 
229 	BUS_WRITE_IVAR(device_get_parent(dev), dev, CXM_IVAR_IICBUS, 0);
230 
231 	/*
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.
235 	 *
236 	 * Remember the child before detaching so we can
237 	 * delete it (bus_generic_detach indirectly zeroes
238 	 * sc->child_dev).
239 	 */
240 	child = sc->iicbb;
241 	bus_generic_detach(dev);
242 	if (child)
243 		device_delete_child(dev, child);
244 
245 	return 0;
246 }
247 
248 
249 /*
250  * the child detached routine.
251  */
252 static void
253 cxm_iic_child_detached(device_t dev, device_t child)
254 {
255 	struct cxm_iic_softc *sc;
256 
257 	/* Get the device data */
258 	sc = device_get_softc(dev);
259 
260 	if (child == sc->iicbb)
261 		sc->iicbb = NULL;
262 }
263 
264 
265 static int
266 cxm_iic_callback(device_t dev, int index, caddr_t *data)
267 {
268 	return 0;
269 }
270 
271 
272 static int
273 cxm_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
274 {
275 	struct cxm_iic_softc *sc;
276 
277 	/* Get the device data */
278 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
279 
280 	/* Set scl to 1 */
281 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)1);
282 
283 	/* Set sda to 1 */
284 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)1);
285 
286 	/*
287 	 * PCI writes may be buffered so force the
288 	 * write to complete by reading the last
289 	 * location written.
290 	 */
291 
292 	CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
293 
294 	/* Wait for 10 usec */
295 	DELAY(10);
296 
297 	return IIC_ENOADDR;
298 }
299 
300 
301 static int
302 cxm_iic_getscl(device_t dev)
303 {
304 	struct cxm_iic_softc *sc;
305 
306 	/* Get the device data */
307 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
308 
309 	/* Get sda */
310 	return CSR_READ_1(sc, CXM_REG_I2C_GETSCL);
311 }
312 
313 
314 static int
315 cxm_iic_getsda(device_t dev)
316 {
317 	struct cxm_iic_softc *sc;
318 
319 	/* Get the device data */
320 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
321 
322 	/* Get sda */
323 	return CSR_READ_1(sc, CXM_REG_I2C_GETSDA);
324 }
325 
326 
327 static void
328 cxm_iic_setscl(device_t dev, int val)
329 {
330 	struct cxm_iic_softc *sc;
331 
332 	/* Get the device data */
333 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
334 
335 	/* Set scl to the requested value */
336 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)(val ? 1 : 0));
337 
338 	/*
339 	 * PCI writes may be buffered so force the
340 	 * write to complete by reading the last
341 	 * location written.
342 	 */
343 
344 	CSR_READ_4(sc, CXM_REG_I2C_SETSCL);
345 }
346 
347 
348 static void
349 cxm_iic_setsda(device_t dev, int val)
350 {
351 	struct cxm_iic_softc *sc;
352 
353 	/* Get the device data */
354 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
355 
356 	/* Set sda to the requested value */
357 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)(val ? 1 : 0));
358 
359 	/*
360 	 * PCI writes may be buffered so force the
361 	 * write to complete by reading the last
362 	 * location written.
363 	 */
364 
365 	CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
366 }
367