iic.c revision 51658
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: head/sys/dev/iicbus/iic.c 51658 1999-09-25 18:24:47Z phk $
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	/* 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 */
115static int
116iic_probe(device_t dev)
117{
118	struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
119	static int once;
120
121	if (!once++)
122                cdevsw_add(&iic_cdevsw);
123
124	sc->sc_addr = iicbus_get_addr(dev);
125
126	/* XXX detect chip with start/stop conditions */
127
128	return (0);
129}
130
131/*
132 * iicattach()
133 */
134static int
135iic_attach(device_t dev)
136{
137	return (0);
138}
139
140static int
141iicopen (dev_t dev, int flags, int fmt, struct proc *p)
142{
143	struct iic_softc *sc = IIC_SOFTC(minor(dev));
144
145	if (!sc)
146		return (EINVAL);
147
148	if (sc->sc_count > 0)
149		return (EBUSY);
150
151	sc->sc_count++;
152
153	return (0);
154}
155
156static int
157iicclose(dev_t dev, int flags, int fmt, struct proc *p)
158{
159	struct iic_softc *sc = IIC_SOFTC(minor(dev));
160
161	if (!sc)
162		return (EINVAL);
163
164	if (!sc->sc_count)
165		return (EINVAL);
166
167	sc->sc_count--;
168
169	if (sc->sc_count < 0)
170		panic("%s: iic_count < 0!", __FUNCTION__);
171
172	return (0);
173}
174
175static int
176iicwrite(dev_t dev, struct uio * uio, int ioflag)
177{
178	device_t iicdev = IIC_DEVICE(minor(dev));
179	struct iic_softc *sc = IIC_SOFTC(minor(dev));
180	int sent, error, count;
181
182	if (!sc || !iicdev)
183		return (EINVAL);
184
185	if (sc->sc_count == 0)
186		return (EINVAL);
187
188	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
189		return (error);
190
191	count = min(uio->uio_resid, BUFSIZE);
192	uiomove(sc->sc_buffer, count, uio);
193
194	error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
195					sc->sc_buffer, count, &sent);
196
197	iicbus_release_bus(device_get_parent(iicdev), iicdev);
198
199	return(error);
200}
201
202static int
203iicread(dev_t dev, struct uio * uio, int ioflag)
204{
205	device_t iicdev = IIC_DEVICE(minor(dev));
206	struct iic_softc *sc = IIC_SOFTC(minor(dev));
207	int len, error = 0;
208	int bufsize;
209
210	if (!sc || !iicdev)
211		return (EINVAL);
212
213	if (sc->sc_count == 0)
214		return (EINVAL);
215
216	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
217		return (error);
218
219	/* max amount of data to read */
220	len = min(uio->uio_resid, BUFSIZE);
221
222	if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
223					sc->sc_inbuf, len, &bufsize)))
224		return (error);
225
226	if (bufsize > uio->uio_resid)
227		panic("%s: too much data read!", __FUNCTION__);
228
229	iicbus_release_bus(device_get_parent(iicdev), iicdev);
230
231	return (uiomove(sc->sc_inbuf, bufsize, uio));
232}
233
234static int
235iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
236{
237	device_t iicdev = IIC_DEVICE(minor(dev));
238	struct iic_softc *sc = IIC_SOFTC(minor(dev));
239	device_t parent = device_get_parent(iicdev);
240	struct iiccmd *s = (struct iiccmd *)data;
241	int error, count;
242
243	if (!sc)
244		return (EINVAL);
245
246	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
247			(flags & O_NONBLOCK) ? IIC_DONTWAIT :
248						(IIC_WAIT | IIC_INTR))))
249		return (error);
250
251	switch (cmd) {
252	case I2CSTART:
253		error = iicbus_start(parent, s->slave, 0);
254		break;
255
256	case I2CSTOP:
257		error = iicbus_stop(parent);
258		break;
259
260	case I2CRSTCARD:
261		error = iicbus_reset(parent, 0, 0, NULL);
262		break;
263
264	case I2CWRITE:
265		error = iicbus_write(parent, s->buf, s->count, &count, 0);
266		break;
267
268	case I2CREAD:
269		error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0);
270		break;
271
272	default:
273		error = ENODEV;
274	}
275
276	iicbus_release_bus(device_get_parent(iicdev), iicdev);
277
278	return (error);
279}
280
281DEV_DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, iic_cdevsw, 0, 0);
282