xref: /freebsd/sys/dev/iicbus/iic.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
1 /*-
2  * Copyright (c) 1998 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$
27  *
28  */
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/buf.h>
36 #include <sys/uio.h>
37 #include <sys/malloc.h>
38 #include <sys/fcntl.h>
39 
40 #include <machine/clock.h>
41 
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
44 
45 #include <machine/iic.h>
46 
47 #include "iicbus_if.h"
48 
49 #define BUFSIZE 1024
50 
51 struct iic_softc {
52 
53 	u_char sc_addr;			/* address on iicbus */
54 	int sc_count;			/* >0 if device opened */
55 
56 	char sc_buffer[BUFSIZE];	/* output buffer */
57 	char sc_inbuf[BUFSIZE];		/* input buffer */
58 };
59 
60 #define IIC_SOFTC(unit) \
61 	((struct iic_softc *)devclass_get_softc(iic_devclass, (unit)))
62 
63 #define IIC_DEVICE(unit) \
64 	(devclass_get_device(iic_devclass, (unit)))
65 
66 static int iic_probe(device_t);
67 static int iic_attach(device_t);
68 
69 static devclass_t iic_devclass;
70 
71 static device_method_t iic_methods[] = {
72 	/* device interface */
73 	DEVMETHOD(device_probe,		iic_probe),
74 	DEVMETHOD(device_attach,	iic_attach),
75 
76 	/* iicbus interface */
77 	DEVMETHOD(iicbus_intr,		iicbus_generic_intr),
78 
79 	{ 0, 0 }
80 };
81 
82 static driver_t iic_driver = {
83 	"iic",
84 	iic_methods,
85 	sizeof(struct iic_softc),
86 };
87 
88 static	d_open_t	iicopen;
89 static	d_close_t	iicclose;
90 static	d_write_t	iicwrite;
91 static	d_read_t	iicread;
92 static	d_ioctl_t	iicioctl;
93 
94 #define CDEV_MAJOR 105
95 static struct cdevsw iic_cdevsw = {
96 	/* open */	iicopen,
97 	/* close */	iicclose,
98 	/* read */	iicread,
99 	/* write */	iicwrite,
100 	/* ioctl */	iicioctl,
101 	/* poll */	nopoll,
102 	/* mmap */	nommap,
103 	/* strategy */	nostrategy,
104 	/* name */	"iic",
105 	/* maj */	CDEV_MAJOR,
106 	/* dump */	nodump,
107 	/* psize */	nopsize,
108 	/* flags */	0,
109 	/* bmaj */	-1
110 };
111 
112 /*
113  * iicprobe()
114  */
115 static int
116 iic_probe(device_t dev)
117 {
118 	struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
119 
120 	sc->sc_addr = iicbus_get_addr(dev);
121 
122 	/* XXX detect chip with start/stop conditions */
123 
124 	return (0);
125 }
126 
127 /*
128  * iicattach()
129  */
130 static int
131 iic_attach(device_t dev)
132 {
133 	make_dev(&iic_cdevsw, device_get_unit(dev),	/* XXX cleanup */
134 			UID_ROOT, GID_WHEEL,
135 			0600, "iic%d", device_get_unit(dev));
136 	return (0);
137 }
138 
139 static int
140 iicopen (dev_t dev, int flags, int fmt, struct proc *p)
141 {
142 	struct iic_softc *sc = IIC_SOFTC(minor(dev));
143 
144 	if (!sc)
145 		return (EINVAL);
146 
147 	if (sc->sc_count > 0)
148 		return (EBUSY);
149 
150 	sc->sc_count++;
151 
152 	return (0);
153 }
154 
155 static int
156 iicclose(dev_t dev, int flags, int fmt, struct proc *p)
157 {
158 	struct iic_softc *sc = IIC_SOFTC(minor(dev));
159 
160 	if (!sc)
161 		return (EINVAL);
162 
163 	if (!sc->sc_count)
164 		return (EINVAL);
165 
166 	sc->sc_count--;
167 
168 	if (sc->sc_count < 0)
169 		panic("%s: iic_count < 0!", __FUNCTION__);
170 
171 	return (0);
172 }
173 
174 static int
175 iicwrite(dev_t dev, struct uio * uio, int ioflag)
176 {
177 	device_t iicdev = IIC_DEVICE(minor(dev));
178 	struct iic_softc *sc = IIC_SOFTC(minor(dev));
179 	int sent, error, count;
180 
181 	if (!sc || !iicdev)
182 		return (EINVAL);
183 
184 	if (sc->sc_count == 0)
185 		return (EINVAL);
186 
187 	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
188 		return (error);
189 
190 	count = min(uio->uio_resid, BUFSIZE);
191 	uiomove(sc->sc_buffer, count, uio);
192 
193 	error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
194 					sc->sc_buffer, count, &sent);
195 
196 	iicbus_release_bus(device_get_parent(iicdev), iicdev);
197 
198 	return(error);
199 }
200 
201 static int
202 iicread(dev_t dev, struct uio * uio, int ioflag)
203 {
204 	device_t iicdev = IIC_DEVICE(minor(dev));
205 	struct iic_softc *sc = IIC_SOFTC(minor(dev));
206 	int len, error = 0;
207 	int bufsize;
208 
209 	if (!sc || !iicdev)
210 		return (EINVAL);
211 
212 	if (sc->sc_count == 0)
213 		return (EINVAL);
214 
215 	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
216 		return (error);
217 
218 	/* max amount of data to read */
219 	len = min(uio->uio_resid, BUFSIZE);
220 
221 	if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
222 					sc->sc_inbuf, len, &bufsize)))
223 		return (error);
224 
225 	if (bufsize > uio->uio_resid)
226 		panic("%s: too much data read!", __FUNCTION__);
227 
228 	iicbus_release_bus(device_get_parent(iicdev), iicdev);
229 
230 	return (uiomove(sc->sc_inbuf, bufsize, uio));
231 }
232 
233 static int
234 iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
235 {
236 	device_t iicdev = IIC_DEVICE(minor(dev));
237 	struct iic_softc *sc = IIC_SOFTC(minor(dev));
238 	device_t parent = device_get_parent(iicdev);
239 	struct iiccmd *s = (struct iiccmd *)data;
240 	int error, count;
241 
242 	if (!sc)
243 		return (EINVAL);
244 
245 	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
246 			(flags & O_NONBLOCK) ? IIC_DONTWAIT :
247 						(IIC_WAIT | IIC_INTR))))
248 		return (error);
249 
250 	switch (cmd) {
251 	case I2CSTART:
252 		error = iicbus_start(parent, s->slave, 0);
253 		break;
254 
255 	case I2CSTOP:
256 		error = iicbus_stop(parent);
257 		break;
258 
259 	case I2CRSTCARD:
260 		error = iicbus_reset(parent, 0, 0, NULL);
261 		break;
262 
263 	case I2CWRITE:
264 		error = iicbus_write(parent, s->buf, s->count, &count, 0);
265 		break;
266 
267 	case I2CREAD:
268 		error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0);
269 		break;
270 
271 	default:
272 		error = ENODEV;
273 	}
274 
275 	iicbus_release_bus(device_get_parent(iicdev), iicdev);
276 
277 	return (error);
278 }
279 
280 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
281