iic.c revision 48557
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 *	$Id: iic.c,v 1.12 1999/05/31 11:24:43 phk Exp $
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
51struct 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
66static int iic_probe(device_t);
67static int iic_attach(device_t);
68
69static devclass_t iic_devclass;
70
71static 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
82static driver_t iic_driver = {
83	"iic",
84	iic_methods,
85	sizeof(struct iic_softc),
86};
87
88static	d_open_t	iicopen;
89static	d_close_t	iicclose;
90static	d_write_t	iicwrite;
91static	d_read_t	iicread;
92static	d_ioctl_t	iicioctl;
93
94#define CDEV_MAJOR 105
95static struct cdevsw iic_cdevsw = {
96	/* open */	iicopen,
97	/* close */	iicclose,
98	/* read */	iicread,
99	/* write */	iicwrite,
100	/* ioctl */	iicioctl,
101	/* stop */	nostop,
102	/* reset */	noreset,
103	/* devtotty */	nodevtotty,
104	/* poll */	nopoll,
105	/* mmap */	nommap,
106	/* strategy */	nostrategy,
107	/* name */	"iic",
108	/* parms */	noparms,
109	/* maj */	CDEV_MAJOR,
110	/* dump */	nodump,
111	/* psize */	nopsize,
112	/* flags */	0,
113	/* maxio */	0,
114	/* bmaj */	-1
115};
116
117/*
118 * iicprobe()
119 */
120static int
121iic_probe(device_t dev)
122{
123	struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
124	static int once;
125
126	if (!once++)
127                cdevsw_add(&iic_cdevsw);
128
129	sc->sc_addr = iicbus_get_addr(dev);
130
131	/* XXX detect chip with start/stop conditions */
132
133	return (0);
134}
135
136/*
137 * iicattach()
138 */
139static int
140iic_attach(device_t dev)
141{
142	return (0);
143}
144
145static int
146iicopen (dev_t dev, int flags, int fmt, struct proc *p)
147{
148	struct iic_softc *sc = IIC_SOFTC(minor(dev));
149
150	if (!sc)
151		return (EINVAL);
152
153	if (sc->sc_count > 0)
154		return (EBUSY);
155
156	sc->sc_count++;
157
158	return (0);
159}
160
161static int
162iicclose(dev_t dev, int flags, int fmt, struct proc *p)
163{
164	struct iic_softc *sc = IIC_SOFTC(minor(dev));
165
166	if (!sc)
167		return (EINVAL);
168
169	if (!sc->sc_count)
170		return (EINVAL);
171
172	sc->sc_count--;
173
174	if (sc->sc_count < 0)
175		panic("%s: iic_count < 0!", __FUNCTION__);
176
177	return (0);
178}
179
180static int
181iicwrite(dev_t dev, struct uio * uio, int ioflag)
182{
183	device_t iicdev = IIC_DEVICE(minor(dev));
184	struct iic_softc *sc = IIC_SOFTC(minor(dev));
185	int sent, error, count;
186
187	if (!sc || !iicdev)
188		return (EINVAL);
189
190	if (sc->sc_count == 0)
191		return (EINVAL);
192
193	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
194		return (error);
195
196	count = min(uio->uio_resid, BUFSIZE);
197	uiomove(sc->sc_buffer, count, uio);
198
199	error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
200					sc->sc_buffer, count, &sent);
201
202	iicbus_release_bus(device_get_parent(iicdev), iicdev);
203
204	return(error);
205}
206
207static int
208iicread(dev_t dev, struct uio * uio, int ioflag)
209{
210	device_t iicdev = IIC_DEVICE(minor(dev));
211	struct iic_softc *sc = IIC_SOFTC(minor(dev));
212	int len, error = 0;
213	int bufsize;
214
215	if (!sc || !iicdev)
216		return (EINVAL);
217
218	if (sc->sc_count == 0)
219		return (EINVAL);
220
221	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
222		return (error);
223
224	/* max amount of data to read */
225	len = min(uio->uio_resid, BUFSIZE);
226
227	if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
228					sc->sc_inbuf, len, &bufsize)))
229		return (error);
230
231	if (bufsize > uio->uio_resid)
232		panic("%s: too much data read!", __FUNCTION__);
233
234	iicbus_release_bus(device_get_parent(iicdev), iicdev);
235
236	return (uiomove(sc->sc_inbuf, bufsize, uio));
237}
238
239static int
240iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
241{
242	device_t iicdev = IIC_DEVICE(minor(dev));
243	struct iic_softc *sc = IIC_SOFTC(minor(dev));
244	device_t parent = device_get_parent(iicdev);
245	struct iiccmd *s = (struct iiccmd *)data;
246	int error, count;
247
248	if (!sc)
249		return (EINVAL);
250
251	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
252			(flags & O_NONBLOCK) ? IIC_DONTWAIT :
253						(IIC_WAIT | IIC_INTR))))
254		return (error);
255
256	switch (cmd) {
257	case I2CSTART:
258		error = iicbus_start(parent, s->slave, 0);
259		break;
260
261	case I2CSTOP:
262		error = iicbus_stop(parent);
263		break;
264
265	case I2CRSTCARD:
266		error = iicbus_reset(parent, 0, 0, NULL);
267		break;
268
269	case I2CWRITE:
270		error = iicbus_write(parent, s->buf, s->count, &count, 0);
271		break;
272
273	case I2CREAD:
274		error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0);
275		break;
276
277	default:
278		error = ENODEV;
279	}
280
281	iicbus_release_bus(device_get_parent(iicdev), iicdev);
282
283	return (error);
284}
285
286DEV_DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, iic_cdevsw, 0, 0);
287