Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / misc / lpbb / lpbb.c
1 /*-
2  * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
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/ppbus/lpbb.c,v 1.11.2.1 2000/05/24 00:20:57 n_hibma Exp $
27  *
28  */
29
30 /*
31  * I2C Bit-Banging over parallel port
32  *
33  * See the Official Philips interface description in lpbb(4)
34  */
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/uio.h>
42
43 #include <machine/clock.h>
44
45 #include <dev/ppbus/ppbconf.h>
46 #include "ppbus_if.h"
47 #include <dev/ppbus/ppbio.h>
48
49 #include <dev/iicbus/iiconf.h>
50 #include <dev/iicbus/iicbus.h>
51
52 #include "iicbb_if.h"
53
54 static int lpbb_detect(device_t dev);
55
56 static void
57 lpbb_identify(driver_t *driver, device_t parent)
58 {
59
60         BUS_ADD_CHILD(parent, 0, "lpbb", 0);
61 }
62
63 static int
64 lpbb_probe(device_t dev)
65 {
66
67         /* Perhaps call this during identify instead? */
68         if (!lpbb_detect(dev))
69                 return (ENXIO);
70
71         device_set_desc(dev, "Parallel I2C bit-banging interface");
72
73         return (0);
74 }
75
76 static int
77 lpbb_attach(device_t dev)
78 {
79         device_t bitbang, iicbus;
80         
81         /* add generic bit-banging code */
82         bitbang = device_add_child(dev, "iicbb", -1);
83
84         /* add the iicbus to the tree */
85         iicbus = iicbus_alloc_bus(bitbang);
86
87         device_probe_and_attach(bitbang);
88
89         /* XXX should be in iicbb_attach! */
90         device_probe_and_attach(iicbus);
91
92         return (0);
93 }
94
95 static int
96 lpbb_callback(device_t dev, int index, caddr_t *data)
97 {
98         device_t ppbus = device_get_parent(dev);
99         int error = 0;
100         int how;
101
102         switch (index) {
103         case IIC_REQUEST_BUS:
104                 /* request the ppbus */
105                 how = *(int *)data;
106                 error = ppb_request_bus(ppbus, dev, how);
107                 break;
108
109         case IIC_RELEASE_BUS:
110                 /* release the ppbus */
111                 error = ppb_release_bus(ppbus, dev);
112                 break;
113
114         default:
115                 error = EINVAL;
116         }
117
118         return (error);
119 }
120
121 #define SDA_out 0x80
122 #define SCL_out 0x08
123 #define SDA_in  0x80
124 #define SCL_in  0x08
125 #define ALIM    0x20
126 #define I2CKEY  0x50
127
128 static int getSDA(device_t ppbus)
129 {
130         if((ppb_rstr(ppbus)&SDA_in)==SDA_in)
131                 return 1;                   
132         else                                
133                 return 0;                   
134 }
135
136 static void setSDA(device_t ppbus, char val)
137 {
138         if(val==0)
139                 ppb_wdtr(ppbus, (u_char)SDA_out);
140         else                            
141                 ppb_wdtr(ppbus, (u_char)~SDA_out);
142 }
143
144 static void setSCL(device_t ppbus, unsigned char val)
145 {
146         if(val==0)
147                 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
148         else                                               
149                 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out)); 
150 }
151
152 static int lpbb_detect(device_t dev)
153 {
154         device_t ppbus = device_get_parent(dev);
155
156         if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
157                 device_printf(dev, "can't allocate ppbus\n");
158                 return (0);
159         }
160
161         /* reset bus */
162         setSDA(ppbus, 1);
163         setSCL(ppbus, 1);
164
165         if ((ppb_rstr(ppbus) & I2CKEY) ||
166                 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
167
168                 ppb_release_bus(ppbus, dev);
169                 return (0);
170         }
171
172         ppb_release_bus(ppbus, dev);
173
174         return (1);
175 }
176
177 static int
178 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
179 {
180         device_t ppbus = device_get_parent(dev);
181
182         /* reset bus */
183         setSDA(ppbus, 1);
184         setSCL(ppbus, 1);
185
186         return (IIC_ENOADDR);
187 }
188
189 static void
190 lpbb_setlines(device_t dev, int ctrl, int data)
191 {
192         device_t ppbus = device_get_parent(dev);
193
194         setSCL(ppbus, ctrl);
195         setSDA(ppbus, data);
196 }
197
198 static int
199 lpbb_getdataline(device_t dev)
200 {
201         device_t ppbus = device_get_parent(dev);
202
203         return (getSDA(ppbus));
204 }
205
206 static devclass_t lpbb_devclass;
207
208 static device_method_t lpbb_methods[] = {
209         /* device interface */
210         DEVMETHOD(device_identify,      lpbb_identify),
211         DEVMETHOD(device_probe,         lpbb_probe),
212         DEVMETHOD(device_attach,        lpbb_attach),
213
214         /* bus interface */
215         DEVMETHOD(bus_print_child,      bus_generic_print_child),
216
217         /* iicbb interface */
218         DEVMETHOD(iicbb_callback,       lpbb_callback),
219         DEVMETHOD(iicbb_setlines,       lpbb_setlines),
220         DEVMETHOD(iicbb_getdataline,    lpbb_getdataline),
221         DEVMETHOD(iicbb_reset,          lpbb_reset),
222
223         { 0, 0 }
224 };
225
226 static driver_t lpbb_driver = {
227         "lpbb",
228         lpbb_methods,
229         1,
230 };
231
232 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);