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