xref: /freebsd/sys/dev/gpio/gpio_if.m (revision 1e66f787c838b5af7de716e266caf4e5d190d54b)
1#-
2# Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
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$
27#
28
29#include <sys/bus.h>
30#include <sys/gpio.h>
31
32INTERFACE gpio;
33
34CODE {
35	static device_t
36	gpio_default_get_bus(void)
37	{
38
39		return (NULL);
40	}
41
42	static int
43	gpio_default_nosupport(void)
44	{
45
46		return (EOPNOTSUPP);
47	}
48
49	static int
50	gpio_default_map_gpios(device_t bus, phandle_t dev,
51	    phandle_t gparent, int gcells, pcell_t *gpios, uint32_t *pin,
52	    uint32_t *flags)
53	{
54		/* Propagate up the bus hierarchy until someone handles it. */
55		if (device_get_parent(bus) != NULL)
56			return (GPIO_MAP_GPIOS(device_get_parent(bus), dev,
57			    gparent, gcells, gpios, pin, flags));
58
59		/* If that fails, then assume the FreeBSD defaults. */
60		*pin = gpios[0];
61		if (gcells == 2 || gcells == 3)
62			*flags = gpios[gcells - 1];
63
64		return (0);
65	}
66};
67
68HEADER {
69	#include <dev/ofw/openfirm.h>
70};
71
72#
73# Return the gpiobus device reference
74#
75METHOD device_t get_bus {
76	device_t dev;
77} DEFAULT gpio_default_get_bus;
78
79#
80# Get maximum pin number
81#
82METHOD int pin_max {
83	device_t dev;
84	int *maxpin;
85};
86
87#
88# Set value of pin specifed by pin_num
89#
90METHOD int pin_set {
91	device_t dev;
92	uint32_t pin_num;
93	uint32_t pin_value;
94};
95
96#
97# Get value of pin specifed by pin_num
98#
99METHOD int pin_get {
100	device_t dev;
101	uint32_t pin_num;
102	uint32_t *pin_value;
103};
104
105#
106# Toggle value of pin specifed by pin_num
107#
108METHOD int pin_toggle {
109	device_t dev;
110	uint32_t pin_num;
111};
112
113#
114# Get pin capabilities
115#
116METHOD int pin_getcaps {
117	device_t dev;
118	uint32_t pin_num;
119	uint32_t *caps;
120};
121
122#
123# Get pin flags
124#
125METHOD int pin_getflags {
126	device_t dev;
127	uint32_t pin_num;
128	uint32_t *flags;
129};
130
131#
132# Get pin name
133#
134METHOD int pin_getname {
135	device_t dev;
136	uint32_t pin_num;
137	char *name;
138};
139
140#
141# Set current configuration and capabilities
142#
143METHOD int pin_setflags {
144	device_t dev;
145	uint32_t pin_num;
146	uint32_t flags;
147};
148
149#
150# Allow the GPIO controller to map the gpio-specifier on its own.
151#
152METHOD int map_gpios {
153        device_t bus;
154        phandle_t dev;
155        phandle_t gparent;
156        int gcells;
157        pcell_t *gpios;
158        uint32_t *pin;
159        uint32_t *flags;
160} DEFAULT gpio_default_map_gpios;
161
162#
163# Simultaneously read and/or change up to 32 adjacent pins.
164# If the device cannot change the pins simultaneously, returns EOPNOTSUPP.
165#
166# More details about using this interface can be found in sys/gpio.h
167#
168METHOD int pin_access_32 {
169	device_t dev;
170	uint32_t first_pin;
171	uint32_t clear_pins;
172	uint32_t change_pins;
173	uint32_t *orig_pins;
174} DEFAULT gpio_default_nosupport;
175
176#
177# Simultaneously configure up to 32 adjacent pins.
178# This is intended to change the configuration of all the pins simultaneously,
179# but unlike pin_access_32, this will not fail if the hardware can't do so.
180#
181# More details about using this interface can be found in sys/gpio.h
182#
183METHOD int pin_config_32 {
184	device_t dev;
185	uint32_t first_pin;
186	uint32_t num_pins;
187	uint32_t *pin_flags;
188} DEFAULT gpio_default_nosupport;
189