xref: /dragonfly/sys/dev/video/cxm/cxm_i2c.c (revision 235099c3)
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 #if 0
69 static int	cxm_iic_getscl(device_t);
70 #endif
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);
75 
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),
81 
82 	/* bus interface */
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),
86 
87 	/* iicbb interface */
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),
92 
93 	{ 0, 0 }
94 };
95 
96 static driver_t cxm_iic_driver = {
97 	"cxm_iic",
98 	cxm_iic_methods,
99 	sizeof(struct cxm_iic_softc),
100 };
101 
102 static devclass_t cxm_iic_devclass;
103 
104 MODULE_VERSION(cxm_iic, 1);
105 DRIVER_MODULE(cxm_iic, cxm, cxm_iic_driver, cxm_iic_devclass, 0, 0);
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 	int unit;
137 	bus_space_handle_t *bhandlep;
138 	bus_space_tag_t *btagp;
139 	struct cxm_iic_softc *sc;
140 	device_t child;
141 
142 	/* Get the device data */
143 	sc = device_get_softc(dev);
144 	unit = device_get_unit(dev);
145 
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)) {
151 		device_printf(dev,
152 			      "could not retrieve bus space information\n");
153 		return ENXIO;
154 	}
155 
156 	sc->btag = *btagp;
157 	sc->bhandle = *bhandlep;
158 
159 	/* add bit-banging generic code onto cxm_iic interface */
160 	sc->iicbb = device_add_child(dev, "iicbb", -1);
161 
162 	if (!sc->iicbb) {
163 		device_printf(dev, "could not add iicbb\n");
164 		return ENXIO;
165 	}
166 
167 	/* probed and attached the bit-banging code */
168 	error = device_probe_and_attach(sc->iicbb);
169 
170 	if (error) {
171 		device_printf(dev, "could not attach iicbb\n");
172 		goto fail;
173 	}
174 
175 	/* locate iicbus which was attached by the bit-banging code */
176 	iicbus = NULL;
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) {
180 			iicbus = kids[i];
181 			break;
182 		}
183 	kfree(kids, M_TEMP);
184 
185 	if (!iicbus) {
186 		device_printf(dev, "could not find iicbus\n");
187 		error = ENXIO;
188 		goto fail;
189 	}
190 
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");
194 		error = ENXIO;
195 		goto fail;
196 	}
197 
198 	return 0;
199 
200 fail:
201 	/*
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.
205 	 *
206 	 * Remember the child before detaching so we can
207 	 * delete it (bus_generic_detach indirectly zeroes
208 	 * sc->child_dev).
209 	 */
210 	child = sc->iicbb;
211 	bus_generic_detach(dev);
212 	if (child)
213 		device_delete_child(dev, child);
214 
215 	return error;
216 }
217 
218 
219 /*
220  * the detach routine.
221  */
222 static int
223 cxm_iic_detach(device_t dev)
224 {
225 	struct cxm_iic_softc *sc;
226 	device_t child;
227 
228 	/* Get the device data */
229 	sc = device_get_softc(dev);
230 
231 	BUS_WRITE_IVAR(device_get_parent(dev), dev, CXM_IVAR_IICBUS, 0);
232 
233 	/*
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.
237 	 *
238 	 * Remember the child before detaching so we can
239 	 * delete it (bus_generic_detach indirectly zeroes
240 	 * sc->child_dev).
241 	 */
242 	child = sc->iicbb;
243 	bus_generic_detach(dev);
244 	if (child)
245 		device_delete_child(dev, child);
246 
247 	return 0;
248 }
249 
250 
251 /*
252  * the child detached routine.
253  */
254 static void
255 cxm_iic_child_detached(device_t dev, device_t child)
256 {
257 	struct cxm_iic_softc *sc;
258 
259 	/* Get the device data */
260 	sc = device_get_softc(dev);
261 
262 	if (child == sc->iicbb)
263 		sc->iicbb = NULL;
264 }
265 
266 
267 static int
268 cxm_iic_callback(device_t dev, int index, caddr_t *data)
269 {
270 	return 0;
271 }
272 
273 
274 static int
275 cxm_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
276 {
277 	struct cxm_iic_softc *sc;
278 
279 	/* Get the device data */
280 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
281 
282 	/* Set scl to 1 */
283 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)1);
284 
285 	/* Set sda to 1 */
286 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)1);
287 
288 	/*
289 	 * PCI writes may be buffered so force the
290 	 * write to complete by reading the last
291 	 * location written.
292 	 */
293 
294 	CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
295 
296 	/* Wait for 10 usec */
297 	DELAY(10);
298 
299 	return IIC_ENOADDR;
300 }
301 
302 
303 #if 0
304 static int
305 cxm_iic_getscl(device_t dev)
306 {
307 	struct cxm_iic_softc *sc;
308 
309 	/* Get the device data */
310 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
311 
312 	/* Get sda */
313 	return CSR_READ_1(sc, CXM_REG_I2C_GETSCL);
314 }
315 #endif
316 
317 
318 static int
319 cxm_iic_getsda(device_t dev)
320 {
321 	struct cxm_iic_softc *sc;
322 
323 	/* Get the device data */
324 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
325 
326 	/* Get sda */
327 	return CSR_READ_1(sc, CXM_REG_I2C_GETSDA);
328 }
329 
330 
331 static void
332 cxm_iic_setscl(device_t dev, int val)
333 {
334 	struct cxm_iic_softc *sc;
335 
336 	/* Get the device data */
337 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
338 
339 	/* Set scl to the requested value */
340 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)(val ? 1 : 0));
341 
342 	/*
343 	 * PCI writes may be buffered so force the
344 	 * write to complete by reading the last
345 	 * location written.
346 	 */
347 
348 	CSR_READ_4(sc, CXM_REG_I2C_SETSCL);
349 }
350 
351 
352 static void
353 cxm_iic_setsda(device_t dev, int val)
354 {
355 	struct cxm_iic_softc *sc;
356 
357 	/* Get the device data */
358 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
359 
360 	/* Set sda to the requested value */
361 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)(val ? 1 : 0));
362 
363 	/*
364 	 * PCI writes may be buffered so force the
365 	 * write to complete by reading the last
366 	 * location written.
367 	 */
368 
369 	CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
370 }
371 
372 
373 static void
374 cxm_iic_setlines(device_t dev, int ctrl, int data)
375 {
376 
377 	cxm_iic_setscl(dev, ctrl);
378 	cxm_iic_setsda(dev, data);
379 
380 	/* Wait for 10 usec */
381 	DELAY(10);
382 }
383