iic.c revision 130585
1211485Simp/*-
2211485Simp * Copyright (c) 1998, 2001 Nicolas Souchu
3211485Simp * All rights reserved.
4211485Simp *
5211485Simp * Redistribution and use in source and binary forms, with or without
6211485Simp * modification, are permitted provided that the following conditions
7211485Simp * are met:
8211485Simp * 1. Redistributions of source code must retain the above copyright
9211485Simp *    notice, this list of conditions and the following disclaimer.
10211485Simp * 2. Redistributions in binary form must reproduce the above copyright
11211485Simp *    notice, this list of conditions and the following disclaimer in the
12211485Simp *    documentation and/or other materials provided with the distribution.
13211485Simp *
14211485Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15211485Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16211485Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17211485Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18211485Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19211485Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20211485Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21211485Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22211485Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23211485Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24211485Simp * SUCH DAMAGE.
25211485Simp *
26211485Simp * $FreeBSD: head/sys/dev/iicbus/iic.c 130585 2004-06-16 09:47:26Z phk $
27211485Simp *
28211485Simp */
29211485Simp#include <sys/param.h>
30211485Simp#include <sys/kernel.h>
31211485Simp#include <sys/systm.h>
32211485Simp#include <sys/malloc.h>
33211485Simp#include <sys/module.h>
34211485Simp#include <sys/bus.h>
35211485Simp#include <sys/conf.h>
36211730Simp#include <sys/uio.h>
37211730Simp#include <sys/fcntl.h>
38211485Simp
39220059Sjpaetzel#include <dev/iicbus/iiconf.h>
40220059Sjpaetzel#include <dev/iicbus/iicbus.h>
41211730Simp#include <dev/iicbus/iic.h>
42211730Simp
43211730Simp#include "iicbus_if.h"
44211485Simp
45211730Simp#define BUFSIZE 1024
46211730Simp
47211730Simpstruct iic_softc {
48211730Simp
49211730Simp	u_char sc_addr;			/* 7 bit address on iicbus */
50211730Simp	int sc_count;			/* >0 if device opened */
51211485Simp
52211485Simp	char sc_buffer[BUFSIZE];	/* output buffer */
53211485Simp	char sc_inbuf[BUFSIZE];		/* input buffer */
54211485Simp
55211485Simp	struct cdev *sc_devnode;
56211730Simp};
57211730Simp
58211730Simp#define IIC_SOFTC(unit) \
59211485Simp	((struct iic_softc *)devclass_get_softc(iic_devclass, (unit)))
60211730Simp
61211730Simp#define IIC_DEVICE(unit) \
62211730Simp	(devclass_get_device(iic_devclass, (unit)))
63211485Simp
64211730Simpstatic int iic_probe(device_t);
65211730Simpstatic int iic_attach(device_t);
66211730Simpstatic int iic_detach(device_t);
67211730Simpstatic void iic_identify(driver_t *driver, device_t parent);
68211485Simp
69211730Simpstatic devclass_t iic_devclass;
70211730Simp
71211485Simpstatic device_method_t iic_methods[] = {
72211730Simp	/* device interface */
73211730Simp	DEVMETHOD(device_identify,	iic_identify),
74211485Simp	DEVMETHOD(device_probe,		iic_probe),
75211485Simp	DEVMETHOD(device_attach,	iic_attach),
76211485Simp	DEVMETHOD(device_detach,	iic_detach),
77211485Simp
78211485Simp	/* iicbus interface */
79247734Sjpaetzel	DEVMETHOD(iicbus_intr,		iicbus_generic_intr),
80247734Sjpaetzel
81247734Sjpaetzel	{ 0, 0 }
82211730Simp};
83211730Simp
84211485Simpstatic driver_t iic_driver = {
85247734Sjpaetzel	"iic",
86247734Sjpaetzel	iic_methods,
87211486Simp	sizeof(struct iic_softc),
88247734Sjpaetzel};
89247734Sjpaetzel
90247734Sjpaetzelstatic	d_open_t	iicopen;
91247734Sjpaetzelstatic	d_close_t	iicclose;
92247734Sjpaetzelstatic	d_write_t	iicwrite;
93247734Sjpaetzelstatic	d_read_t	iicread;
94247734Sjpaetzelstatic	d_ioctl_t	iicioctl;
95247734Sjpaetzel
96247734Sjpaetzelstatic struct cdevsw iic_cdevsw = {
97247734Sjpaetzel	.d_version =	D_VERSION,
98247734Sjpaetzel	.d_flags =	D_NEEDGIANT,
99247734Sjpaetzel	.d_open =	iicopen,
100247734Sjpaetzel	.d_close =	iicclose,
101247734Sjpaetzel	.d_read =	iicread,
102247734Sjpaetzel	.d_write =	iicwrite,
103247734Sjpaetzel	.d_ioctl =	iicioctl,
104247734Sjpaetzel	.d_name =	"iic",
105247734Sjpaetzel};
106247734Sjpaetzel
107247734Sjpaetzelstatic void
108247734Sjpaetzeliic_identify(driver_t *driver, device_t parent)
109247734Sjpaetzel{
110247734Sjpaetzel	BUS_ADD_CHILD(parent, 0, "iic", -1);
111247734Sjpaetzel}
112247734Sjpaetzel
113247734Sjpaetzelstatic int
114247734Sjpaetzeliic_probe(device_t dev)
115247734Sjpaetzel{
116247734Sjpaetzel	device_set_desc(dev, "I2C generic I/O");
117247734Sjpaetzel	return (0);
118247734Sjpaetzel}
119247734Sjpaetzel
120247734Sjpaetzelstatic int
121247734Sjpaetzeliic_attach(device_t dev)
122247734Sjpaetzel{
123247734Sjpaetzel	struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
124247734Sjpaetzel
125247734Sjpaetzel	if (!sc)
126247734Sjpaetzel		return (ENOMEM);
127247734Sjpaetzel
128247734Sjpaetzel	bzero(sc, sizeof(struct iic_softc));
129247734Sjpaetzel
130247734Sjpaetzel	sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev),
131211730Simp			UID_ROOT, GID_WHEEL,
132247734Sjpaetzel			0600, "iic%d", device_get_unit(dev));
133247734Sjpaetzel
134211730Simp	return (0);
135211486Simp}
136247734Sjpaetzel
137211485Simpstatic int
138247734Sjpaetzeliic_detach(device_t dev)
139247734Sjpaetzel{
140247734Sjpaetzel	struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
141247734Sjpaetzel
142247734Sjpaetzel	if (sc->sc_devnode)
143247734Sjpaetzel		destroy_dev(sc->sc_devnode);
144247734Sjpaetzel
145211485Simp	return (0);
146247734Sjpaetzel}
147247734Sjpaetzel
148247734Sjpaetzelstatic int
149247734Sjpaetzeliicopen (struct cdev *dev, int flags, int fmt, struct thread *td)
150247734Sjpaetzel{
151211485Simp	struct iic_softc *sc = IIC_SOFTC(minor(dev));
152247734Sjpaetzel
153247734Sjpaetzel	if (!sc)
154247734Sjpaetzel		return (EINVAL);
155247734Sjpaetzel
156247734Sjpaetzel	if (sc->sc_count > 0)
157247734Sjpaetzel		return (EBUSY);
158247734Sjpaetzel
159247734Sjpaetzel	sc->sc_count++;
160247734Sjpaetzel
161247734Sjpaetzel	return (0);
162211730Simp}
163247734Sjpaetzel
164211486Simpstatic int
165247734Sjpaetzeliicclose(struct cdev *dev, int flags, int fmt, struct thread *td)
166247734Sjpaetzel{
167247734Sjpaetzel	struct iic_softc *sc = IIC_SOFTC(minor(dev));
168247734Sjpaetzel
169247734Sjpaetzel	if (!sc)
170247734Sjpaetzel		return (EINVAL);
171211485Simp
172247734Sjpaetzel	if (!sc->sc_count)
173247734Sjpaetzel		return (EINVAL);
174247734Sjpaetzel
175247734Sjpaetzel	sc->sc_count--;
176247734Sjpaetzel
177247734Sjpaetzel	if (sc->sc_count < 0)
178247734Sjpaetzel		panic("%s: iic_count < 0!", __func__);
179247734Sjpaetzel
180247734Sjpaetzel	return (0);
181247734Sjpaetzel}
182247734Sjpaetzel
183247734Sjpaetzelstatic int
184247734Sjpaetzeliicwrite(struct cdev *dev, struct uio * uio, int ioflag)
185247734Sjpaetzel{
186247734Sjpaetzel	device_t iicdev = IIC_DEVICE(minor(dev));
187211730Simp	struct iic_softc *sc = IIC_SOFTC(minor(dev));
188211485Simp	int sent, error, count;
189
190	if (!sc || !iicdev || !sc->sc_addr)
191		return (EINVAL);
192
193	if (sc->sc_count == 0)
194		return (EINVAL);
195
196	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
197		return (error);
198
199	count = min(uio->uio_resid, BUFSIZE);
200	uiomove(sc->sc_buffer, count, uio);
201
202	error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
203					sc->sc_buffer, count, &sent);
204
205	iicbus_release_bus(device_get_parent(iicdev), iicdev);
206
207	return(error);
208}
209
210static int
211iicread(struct cdev *dev, struct uio * uio, int ioflag)
212{
213	device_t iicdev = IIC_DEVICE(minor(dev));
214	struct iic_softc *sc = IIC_SOFTC(minor(dev));
215	int len, error = 0;
216	int bufsize;
217
218	if (!sc || !iicdev || !sc->sc_addr)
219		return (EINVAL);
220
221	if (sc->sc_count == 0)
222		return (EINVAL);
223
224	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
225		return (error);
226
227	/* max amount of data to read */
228	len = min(uio->uio_resid, BUFSIZE);
229
230	if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
231					sc->sc_inbuf, len, &bufsize)))
232		return (error);
233
234	if (bufsize > uio->uio_resid)
235		panic("%s: too much data read!", __func__);
236
237	iicbus_release_bus(device_get_parent(iicdev), iicdev);
238
239	return (uiomove(sc->sc_inbuf, bufsize, uio));
240}
241
242static int
243iicioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
244{
245	device_t iicdev = IIC_DEVICE(minor(dev));
246	struct iic_softc *sc = IIC_SOFTC(minor(dev));
247	device_t parent = device_get_parent(iicdev);
248	struct iiccmd *s = (struct iiccmd *)data;
249	int error, count;
250	char *buf = NULL;
251
252	if (!sc)
253		return (EINVAL);
254
255	if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
256			(flags & O_NONBLOCK) ? IIC_DONTWAIT :
257						(IIC_WAIT | IIC_INTR))))
258		return (error);
259
260	switch (cmd) {
261	case I2CSTART:
262		error = iicbus_start(parent, s->slave, 0);
263
264		/*
265		 * Implicitly set the chip addr to the slave addr passed as
266		 * parameter. Consequently, start/stop shall be called before
267		 * the read or the write of a block.
268		 */
269		if (!error)
270			sc->sc_addr = s->slave;
271
272		break;
273
274	case I2CSTOP:
275		error = iicbus_stop(parent);
276		break;
277
278	case I2CRSTCARD:
279		error = iicbus_reset(parent, 0, 0, NULL);
280		break;
281
282	case I2CWRITE:
283		if (s->count <= 0) {
284			error = EINVAL;
285			break;
286		}
287		buf = malloc((unsigned long)s->count, M_TEMP, M_WAITOK);
288		error = copyin(s->buf, buf, s->count);
289		if (error)
290			break;
291		error = iicbus_write(parent, buf, s->count, &count, 10);
292		break;
293
294	case I2CREAD:
295		if (s->count <= 0) {
296			error = EINVAL;
297			break;
298		}
299		buf = malloc((unsigned long)s->count, M_TEMP, M_WAITOK);
300		error = iicbus_read(parent, buf, s->count, &count, s->last, 10);
301		if (error)
302			break;
303		error = copyout(buf, s->buf, s->count);
304		break;
305
306	default:
307		error = ENOTTY;
308	}
309
310	iicbus_release_bus(device_get_parent(iicdev), iicdev);
311
312	if (buf != NULL)
313		free(buf, M_TEMP);
314	return (error);
315}
316
317DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
318MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
319MODULE_VERSION(iic, 1);
320