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