a117aa6d63852ecf892480293ab9c8ac8efd3f63
[dragonfly.git] / sys / dev / video / bktr / bktr_i2c.c
1 /*
2  * Copyright (c) 1998, 2001 Nicolas Souchu
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/bktr/bktr_i2c.c,v 1.25 2003/12/08 07:59:18 obrien Exp $
27  * $DragonFly: src/sys/dev/video/bktr/bktr_i2c.c,v 1.5 2004/05/15 17:54:12 joerg Exp $
28  */
29
30 /*
31  * I2C support for the bti2c chipset.
32  *
33  * From brooktree848.c <fsmp@freefall.org>
34  */
35
36 #include "opt_bktr.h"
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 #include <sys/uio.h>
45
46 #include <sys/select.h>
47
48 #include <bus/pci/pcivar.h>
49 #include <bus/pci/pcireg.h>
50
51 #include <machine/bus_memio.h>          /* for bus space */
52 #include <machine/bus.h>
53 #include <sys/bus.h>
54
55 #include <dev/video/meteor/ioctl_meteor.h>
56 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
57 #include <dev/video/bktr/bktr_reg.h>
58 #include <dev/video/bktr/bktr_i2c.h>
59
60 #include <bus/smbus/smbconf.h>
61 #include <bus/iicbus/iiconf.h>
62
63 #define I2C_DELAY       40
64
65 /* Compilation is void if BKTR_USE_FREEBSD_SMBUS is not
66  * defined. This allows bktr owners to have smbus active for there
67  * motherboard and still use their bktr without smbus.
68  */
69 #if defined(BKTR_USE_FREEBSD_SMBUS)
70
71 #define BTI2C_DEBUG(x)  if (bti2c_debug) (x)
72 static int bti2c_debug = 0;
73
74 /*
75  * Call this to pass the address of the bktr device to the
76  * bti2c_i2c layer and initialize all the I2C bus architecture
77  */
78 int bt848_i2c_attach(device_t dev)
79 {
80         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
81         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
82
83         sc->smbus = device_add_child(dev, "smbus", -1);
84         sc->iicbb = device_add_child(dev, "iicbb", -1);
85
86         if (!sc->iicbb || !sc->smbus)
87                 return ENXIO;
88
89         bus_generic_attach(dev);
90
91         return (0);
92 };
93
94 int bt848_i2c_detach(device_t dev)
95 {
96         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
97         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
98         int error = 0;
99
100         if ((error = bus_generic_detach(dev)))
101                 goto error;
102
103         if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb)))
104                 goto error;
105
106         if (sc->smbus && (error = device_delete_child(dev, sc->smbus)))
107                 goto error;
108
109 error:
110         return (error);
111 }
112
113 int bti2c_smb_callback(device_t dev, int index, caddr_t *data)
114 {
115         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
116         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
117         int error = 0;
118
119         /* test each time if we already have/haven't the iicbus
120          * to avoid deadlocks
121          */
122         switch (index) {
123         case SMB_REQUEST_BUS:
124                 /* XXX test & set */
125                 if (!sc->bus_owned) {
126                         sc->bus_owned = 1;
127                 } else
128                         error = EWOULDBLOCK;
129                 break;
130
131         case SMB_RELEASE_BUS:
132                 /* XXX test & set */
133                 if (sc->bus_owned) {
134                         sc->bus_owned = 0;
135                 } else
136                         error = EINVAL;
137                 break;
138
139         default:
140                 error = EINVAL;
141         }
142
143         return (error);
144 }
145
146 int bti2c_iic_callback(device_t dev, int index, caddr_t *data)
147 {
148         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
149         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
150         int error = 0;
151
152         /* test each time if we already have/haven't the smbus
153          * to avoid deadlocks
154          */
155         switch (index) {
156         case IIC_REQUEST_BUS:
157                 /* XXX test & set */
158                 if (!sc->bus_owned) {
159                         sc->bus_owned = 1;
160                 } else
161                         error = EWOULDBLOCK;
162                 break;
163
164         case IIC_RELEASE_BUS:
165                 /* XXX test & set */
166                 if (sc->bus_owned) {
167                         sc->bus_owned = 0;
168                 } else
169                         error = EINVAL;
170                 break;
171
172         default:
173                 error = EINVAL;
174         }
175
176         return (error);
177 }
178
179 int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
180 {
181         if (oldaddr)
182                 *oldaddr = 0;                   /* XXX */
183
184         return (IIC_ENOADDR);
185 }
186
187 void bti2c_iic_setsda(device_t dev, int val)
188 {
189         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
190         int clock;
191
192         clock = INL(sc, BKTR_I2C_DATA_CTL) & 0x2;
193
194         if (val)
195                 OUTL(sc, BKTR_I2C_DATA_CTL, clock | 1);
196         else
197                 OUTL(sc, BKTR_I2C_DATA_CTL, clock);
198
199         DELAY(I2C_DELAY);
200
201         return;
202 }
203
204 void bti2c_iic_setscl(device_t dev, int val)
205 {
206         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
207         int data;
208
209         data = INL(sc, BKTR_I2C_DATA_CTL) & 0x1;
210
211         if (val)
212                 OUTL(sc, BKTR_I2C_DATA_CTL, 0x2 | data);
213         else
214                 OUTL(sc, BKTR_I2C_DATA_CTL, data);
215
216         DELAY(I2C_DELAY);
217
218         return;
219 }
220
221 int
222 bti2c_iic_getsda(device_t dev)
223 {
224         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
225
226         return (INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
227 }
228
229 int
230 bti2c_iic_getscl(device_t dev)
231 {
232         return (0);
233 }
234
235 static int
236 bti2c_write(struct bktr_softc *sc, u_long data)
237 {
238         u_long          x;
239
240         /* clear status bits */
241         OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
242
243         BTI2C_DEBUG(printf("w%lx", data));
244
245         /* write the address and data */
246         OUTL(sc, BKTR_I2C_DATA_CTL, data);
247
248         /* wait for completion */
249         for ( x = 0x7fffffff; x; --x ) {        /* safety valve */
250                 if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
251                         break;
252         }
253
254         /* check for ACK */
255         if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
256                 BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
257                         (!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
258                 return (SMB_ENOACK);
259         }
260         BTI2C_DEBUG(printf("+"));
261
262         /* return OK */
263         return( 0 );
264 }
265
266 int
267 bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
268 {
269         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
270         u_long data;
271
272         data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
273
274         return (bti2c_write(sc, data));
275 }
276
277 /*
278  * byte1 becomes low byte of word
279  * byte2 becomes high byte of word
280  */
281 int
282 bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
283 {
284         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
285         u_long data;
286         char low, high;
287
288         low = (char)(word & 0xff);
289         high = (char)((word & 0xff00) >> 8);
290
291         data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
292                 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
293
294         return (bti2c_write(sc, data));
295 }
296
297 /*
298  * The Bt878 and Bt879 differed on the treatment of i2c commands
299  */
300 int
301 bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
302 {
303         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
304         u_long          x;
305
306         /* clear status bits */
307         OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
308
309         OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
310
311         BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
312
313         /* wait for completion */
314         for ( x = 0x7fffffff; x; --x ) {        /* safety valve */
315                 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
316                         break;
317         }
318
319         /* check for ACK */
320         if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
321                 BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
322                         (!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
323                 return (SMB_ENOACK);
324         }
325
326         *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
327         BTI2C_DEBUG(printf("r%x+", *byte));
328
329         return (0);
330 }
331
332 #endif /* defined(BKTR_USE_FREEBSD_SMBUS) */