1213237Sgonzo/*-
2213237Sgonzo * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3213237Sgonzo * All rights reserved.
4213237Sgonzo *
5213237Sgonzo * Redistribution and use in source and binary forms, with or without
6213237Sgonzo * modification, are permitted provided that the following conditions
7213237Sgonzo * are met:
8213237Sgonzo * 1. Redistributions of source code must retain the above copyright
9213237Sgonzo *    notice, this list of conditions and the following disclaimer.
10213237Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
11213237Sgonzo *    notice, this list of conditions and the following disclaimer in the
12213237Sgonzo *    documentation and/or other materials provided with the distribution.
13213237Sgonzo *
14213277Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15213277Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16213277Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17213277Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18213277Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19213277Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20213277Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21213277Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22213277Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23213277Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24213277Sgonzo * SUCH DAMAGE.
25213237Sgonzo *
26213237Sgonzo * $FreeBSD$
27213237Sgonzo *
28213237Sgonzo */
29213237Sgonzo
30213237Sgonzo#ifndef	__GPIOBUS_H__
31213237Sgonzo#define	__GPIOBUS_H__
32213237Sgonzo
33266105Sloos#include "opt_platform.h"
34266105Sloos
35213237Sgonzo#include <sys/lock.h>
36213237Sgonzo#include <sys/mutex.h>
37278784Sloos#include <sys/rman.h>
38213237Sgonzo
39266105Sloos#ifdef FDT
40266105Sloos#include <dev/ofw/ofw_bus_subr.h>
41266105Sloos#endif
42213237Sgonzo
43266135Sloos#include "gpio_if.h"
44266135Sloos
45278781Sloos#ifdef FDT
46278781Sloos#define	GPIOBUS_IVAR(d) (struct gpiobus_ivar *)				\
47278781Sloos	&((struct ofw_gpiobus_devinfo *)device_get_ivars(d))->opd_dinfo
48278781Sloos#else
49266105Sloos#define	GPIOBUS_IVAR(d) (struct gpiobus_ivar *) device_get_ivars(d)
50278781Sloos#endif
51266105Sloos#define	GPIOBUS_SOFTC(d) (struct gpiobus_softc *) device_get_softc(d)
52266105Sloos#define	GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
53266105Sloos#define	GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
54266105Sloos#define	GPIOBUS_LOCK_INIT(_sc) mtx_init(&_sc->sc_mtx,			\
55266105Sloos	    device_get_nameunit(_sc->sc_dev), "gpiobus", MTX_DEF)
56266105Sloos#define	GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx)
57266105Sloos#define	GPIOBUS_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
58266105Sloos#define	GPIOBUS_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)
59266105Sloos
60278783Sloos#define	GPIOBUS_WAIT		1
61278783Sloos#define	GPIOBUS_DONTWAIT	2
62278783Sloos
63213237Sgonzostruct gpiobus_softc
64213237Sgonzo{
65213237Sgonzo	struct mtx	sc_mtx;		/* bus mutex */
66278784Sloos	struct rman	sc_intr_rman;	/* isr resources */
67213237Sgonzo	device_t	sc_busdev;	/* bus device */
68213237Sgonzo	device_t	sc_owner;	/* bus owner */
69213237Sgonzo	device_t	sc_dev;		/* driver device */
70213237Sgonzo	int		sc_npins;	/* total pins on bus */
71213237Sgonzo	int		*sc_pins_mapped; /* mark mapped pins */
72213237Sgonzo};
73213237Sgonzo
74213237Sgonzostruct gpiobus_ivar
75213237Sgonzo{
76278784Sloos	struct resource_list	rl;	/* isr resource list */
77213237Sgonzo	uint32_t	npins;	/* pins total */
78266135Sloos	uint32_t	*flags;	/* pins flags */
79213237Sgonzo	uint32_t	*pins;	/* pins map */
80213237Sgonzo};
81213237Sgonzo
82266105Sloos#ifdef FDT
83266135Sloosstruct ofw_gpiobus_devinfo {
84266135Sloos	struct gpiobus_ivar	opd_dinfo;
85266135Sloos	struct ofw_bus_devinfo	opd_obdinfo;
86266135Sloos};
87266135Sloos
88266135Sloosstatic __inline int
89266135Sloosgpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells,
90266135Sloos    pcell_t *gpios, uint32_t *pin, uint32_t *flags)
91266135Sloos{
92266135Sloos	return (GPIO_MAP_GPIOS(bus, dev, gparent, gcells, gpios, pin, flags));
93266135Sloos}
94266135Sloos
95266105Sloosdevice_t ofw_gpiobus_add_fdt_child(device_t, phandle_t);
96266105Sloos#endif
97278786Sloosint gpio_check_flags(uint32_t, uint32_t);
98278781Sloosint gpiobus_init_softc(device_t);
99266105Sloos
100266105Sloosextern driver_t gpiobus_driver;
101266105Sloos
102213237Sgonzo#endif	/* __GPIOBUS_H__ */
103