iicbus.c revision 227843
138774Snsouch/*-
293023Snsouch * Copyright (c) 1998, 2001 Nicolas Souchu
338774Snsouch * All rights reserved.
438774Snsouch *
538774Snsouch * Redistribution and use in source and binary forms, with or without
638774Snsouch * modification, are permitted provided that the following conditions
738774Snsouch * are met:
838774Snsouch * 1. Redistributions of source code must retain the above copyright
938774Snsouch *    notice, this list of conditions and the following disclaimer.
1038774Snsouch * 2. Redistributions in binary form must reproduce the above copyright
1138774Snsouch *    notice, this list of conditions and the following disclaimer in the
1238774Snsouch *    documentation and/or other materials provided with the distribution.
1338774Snsouch *
1438774Snsouch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1538774Snsouch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1638774Snsouch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1738774Snsouch * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838774Snsouch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938774Snsouch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2038774Snsouch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2138774Snsouch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2238774Snsouch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2338774Snsouch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2438774Snsouch * SUCH DAMAGE.
2538774Snsouch */
2638774Snsouch
27119418Sobrien#include <sys/cdefs.h>
28119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/iicbus/iicbus.c 227843 2011-11-22 21:28:20Z marius $");
29119418Sobrien
3038774Snsouch/*
3138774Snsouch * Autoconfiguration and support routines for the Philips serial I2C bus
3238774Snsouch */
3338774Snsouch
3438774Snsouch#include <sys/param.h>
3538774Snsouch#include <sys/systm.h>
3638774Snsouch#include <sys/kernel.h>
37181304Sjhb#include <sys/lock.h>
38167856Simp#include <sys/malloc.h>
3938774Snsouch#include <sys/module.h>
40181304Sjhb#include <sys/mutex.h>
4138774Snsouch#include <sys/bus.h>
4238774Snsouch
4338774Snsouch#include <dev/iicbus/iiconf.h>
4438774Snsouch#include <dev/iicbus/iicbus.h>
4538774Snsouch
4638774Snsouch#include "iicbus_if.h"
4738774Snsouch
48129152Sjoerg/* See comments below for why auto-scanning is a bad idea. */
49129152Sjoerg#define SCAN_IICBUS 0
50129152Sjoerg
5140782Snsouchstatic int
5240782Snsouchiicbus_probe(device_t dev)
5340782Snsouch{
54160372Simp
5542442Snsouch	device_set_desc(dev, "Philips I2C bus");
56186833Snwhitehorn
57186833Snwhitehorn	/* Allow other subclasses to override this driver. */
58187457Snwhitehorn	return (BUS_PROBE_GENERIC);
5940782Snsouch}
6040782Snsouch
61129152Sjoerg#if SCAN_IICBUS
6240782Snsouchstatic int
6340782Snsouchiic_probe_device(device_t dev, u_char addr)
6440782Snsouch{
6540782Snsouch	int count;
6640782Snsouch	char byte;
6740782Snsouch
6840782Snsouch	if ((addr & 1) == 0) {
6940782Snsouch		/* is device writable? */
7040782Snsouch		if (!iicbus_start(dev, (u_char)addr, 0)) {
7140782Snsouch			iicbus_stop(dev);
7240782Snsouch			return (1);
7340782Snsouch		}
7440782Snsouch	} else {
7540782Snsouch		/* is device readable? */
7640782Snsouch		if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
7740782Snsouch			return (1);
7840782Snsouch	}
7940782Snsouch
8040782Snsouch	return (0);
8140782Snsouch}
8242442Snsouch#endif
8340782Snsouch
8438774Snsouch/*
8540782Snsouch * We add all the devices which we know about.
8640782Snsouch * The generic attach routine will attach them if they are alive.
8738774Snsouch */
8838774Snsouchstatic int
8940782Snsouchiicbus_attach(device_t dev)
9038774Snsouch{
91129152Sjoerg#if SCAN_IICBUS
92129152Sjoerg	unsigned char addr;
93129152Sjoerg#endif
94167856Simp	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
95129152Sjoerg
96167856Simp	sc->dev = dev;
97181304Sjhb	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
9840782Snsouch	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
9938774Snsouch
10042442Snsouch	/* device probing is meaningless since the bus is supposed to be
10142442Snsouch	 * hot-plug. Moreover, some I2C chips do not appreciate random
10242442Snsouch	 * accesses like stop after start to fast, reads for less than
10342442Snsouch	 * x bytes...
10442442Snsouch	 */
105129152Sjoerg#if SCAN_IICBUS
10640782Snsouch	printf("Probing for devices on iicbus%d:", device_get_unit(dev));
10738774Snsouch
10840782Snsouch	/* probe any devices */
109129152Sjoerg	for (addr = 16; addr < 240; addr++) {
11040782Snsouch		if (iic_probe_device(dev, (u_char)addr)) {
11140782Snsouch			printf(" <%x>", addr);
11240782Snsouch		}
11340782Snsouch	}
11440782Snsouch	printf("\n");
11542442Snsouch#endif
116181304Sjhb	bus_generic_probe(dev);
117167856Simp	bus_enumerate_hinted_children(dev);
11838774Snsouch	bus_generic_attach(dev);
11938774Snsouch        return (0);
12038774Snsouch}
12193023Snsouch
12293023Snsouchstatic int
12393023Snsouchiicbus_detach(device_t dev)
12493023Snsouch{
125181304Sjhb	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
126160372Simp
12793023Snsouch	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
12893023Snsouch	bus_generic_detach(dev);
129181304Sjhb	mtx_destroy(&sc->lock);
13093023Snsouch	return (0);
13193023Snsouch}
13293023Snsouch
13393023Snsouchstatic int
134167856Simpiicbus_print_child(device_t dev, device_t child)
13593023Snsouch{
136167856Simp	struct iicbus_ivar *devi = IICBUS_IVAR(child);
137167856Simp	int retval = 0;
138160372Simp
139167856Simp	retval += bus_print_child_header(dev, child);
140167856Simp	if (devi->addr != 0)
141167856Simp		retval += printf(" at addr %#x", devi->addr);
142167856Simp	retval += bus_print_child_footer(dev, child);
143167856Simp
144167856Simp	return (retval);
145167856Simp}
146167856Simp
147167856Simpstatic void
148167856Simpiicbus_probe_nomatch(device_t bus, device_t child)
149167856Simp{
150167856Simp	struct iicbus_ivar *devi = IICBUS_IVAR(child);
151167856Simp
152167856Simp	device_printf(bus, "<unknown card>");
153167856Simp	printf(" at addr %#x\n", devi->addr);
154167856Simp	return;
155167856Simp}
156167856Simp
157167856Simpstatic int
158167856Simpiicbus_child_location_str(device_t bus, device_t child, char *buf,
159167856Simp    size_t buflen)
160167856Simp{
161167856Simp	struct iicbus_ivar *devi = IICBUS_IVAR(child);
162167856Simp
163167856Simp	snprintf(buf, buflen, "addr=%#x", devi->addr);
16493023Snsouch	return (0);
16593023Snsouch}
16693023Snsouch
167167856Simpstatic int
168167856Simpiicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
169167856Simp    size_t buflen)
170167856Simp{
171167856Simp	*buf = '\0';
172167856Simp	return (0);
173167856Simp}
174167856Simp
175167856Simpstatic int
176188461Simpiicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
177167856Simp{
178167856Simp	struct iicbus_ivar *devi = IICBUS_IVAR(child);
179167856Simp
180167856Simp	switch (which) {
181167856Simp	default:
182167856Simp		return (EINVAL);
183167856Simp	case IICBUS_IVAR_ADDR:
184209800Snwhitehorn		*result = devi->addr;
185167856Simp		break;
186167856Simp	}
187167856Simp	return (0);
188167856Simp}
189167856Simp
190167856Simpstatic device_t
191212413Savgiicbus_add_child(device_t dev, u_int order, const char *name, int unit)
192167856Simp{
193167856Simp	device_t child;
194167856Simp	struct iicbus_ivar *devi;
195167856Simp
196167856Simp	child = device_add_child_ordered(dev, order, name, unit);
197167856Simp	if (child == NULL)
198167856Simp		return (child);
199167856Simp	devi = malloc(sizeof(struct iicbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
200167856Simp	if (devi == NULL) {
201167856Simp		device_delete_child(dev, child);
202167856Simp		return (0);
203167856Simp	}
204167856Simp	device_set_ivars(child, devi);
205167856Simp	return (child);
206167856Simp}
207167856Simp
208167856Simpstatic void
209167856Simpiicbus_hinted_child(device_t bus, const char *dname, int dunit)
210167856Simp{
211167856Simp	device_t child;
212167856Simp	struct iicbus_ivar *devi;
213167856Simp
214167856Simp	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
215167856Simp	devi = IICBUS_IVAR(child);
216167856Simp	resource_int_value(dname, dunit, "addr", &devi->addr);
217167856Simp}
218167856Simp
21938774Snsouchint
22038774Snsouchiicbus_generic_intr(device_t dev, int event, char *buf)
22138774Snsouch{
222160372Simp
22338774Snsouch	return (0);
22438774Snsouch}
22538774Snsouch
22640782Snsouchint
22740782Snsouchiicbus_null_callback(device_t dev, int index, caddr_t data)
22840782Snsouch{
229160372Simp
23040782Snsouch	return (0);
23140782Snsouch}
23240782Snsouch
23340782Snsouchint
23440782Snsouchiicbus_null_repeated_start(device_t dev, u_char addr)
23540782Snsouch{
236160372Simp
23740782Snsouch	return (IIC_ENOTSUPP);
23840782Snsouch}
23940782Snsouch
240167856Simpstatic device_method_t iicbus_methods[] = {
241167856Simp        /* device interface */
242167856Simp        DEVMETHOD(device_probe,         iicbus_probe),
243167856Simp        DEVMETHOD(device_attach,        iicbus_attach),
244167856Simp        DEVMETHOD(device_detach,        iicbus_detach),
245167856Simp
246167856Simp        /* bus interface */
247167856Simp        DEVMETHOD(bus_add_child,	iicbus_add_child),
248167856Simp        DEVMETHOD(bus_print_child,      iicbus_print_child),
249167856Simp	DEVMETHOD(bus_probe_nomatch,	iicbus_probe_nomatch),
250167856Simp	DEVMETHOD(bus_read_ivar,	iicbus_read_ivar),
251167856Simp	DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
252167856Simp	DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
253167856Simp	DEVMETHOD(bus_hinted_child,	iicbus_hinted_child),
254167856Simp
255167856Simp	/* iicbus interface */
256167856Simp	DEVMETHOD(iicbus_transfer,	iicbus_transfer),
257167856Simp
258227843Smarius	DEVMETHOD_END
259167856Simp};
260167856Simp
261167856Simpdriver_t iicbus_driver = {
262167856Simp        "iicbus",
263167856Simp        iicbus_methods,
264167856Simp        sizeof(struct iicbus_softc),
265167856Simp};
266167856Simp
267167856Simpdevclass_t iicbus_devclass;
268167856Simp
26993023SnsouchMODULE_VERSION(iicbus, IICBUS_MODVER);
270187261SnwhitehornDRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);
271187261Snwhitehorn
272