xref: /dragonfly/sys/dev/video/cxm/cxm_i2c.c (revision 8accc937)
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 	{ 0, 0 }
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 	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 static int
304 cxm_iic_getscl(device_t dev)
305 {
306 	struct cxm_iic_softc *sc;
307 
308 	/* Get the device data */
309 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
310 
311 	/* Get sda */
312 	return CSR_READ_1(sc, CXM_REG_I2C_GETSCL);
313 }
314 
315 
316 static int
317 cxm_iic_getsda(device_t dev)
318 {
319 	struct cxm_iic_softc *sc;
320 
321 	/* Get the device data */
322 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
323 
324 	/* Get sda */
325 	return CSR_READ_1(sc, CXM_REG_I2C_GETSDA);
326 }
327 
328 
329 static void
330 cxm_iic_setscl(device_t dev, int val)
331 {
332 	struct cxm_iic_softc *sc;
333 
334 	/* Get the device data */
335 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
336 
337 	/* Set scl to the requested value */
338 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)(val ? 1 : 0));
339 
340 	/*
341 	 * PCI writes may be buffered so force the
342 	 * write to complete by reading the last
343 	 * location written.
344 	 */
345 
346 	CSR_READ_4(sc, CXM_REG_I2C_SETSCL);
347 }
348 
349 
350 static void
351 cxm_iic_setsda(device_t dev, int val)
352 {
353 	struct cxm_iic_softc *sc;
354 
355 	/* Get the device data */
356 	sc = (struct cxm_iic_softc *)device_get_softc(dev);
357 
358 	/* Set sda to the requested value */
359 	CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)(val ? 1 : 0));
360 
361 	/*
362 	 * PCI writes may be buffered so force the
363 	 * write to complete by reading the last
364 	 * location written.
365 	 */
366 
367 	CSR_READ_4(sc, CXM_REG_I2C_SETSDA);
368 }
369