xref: /freebsd/sys/dev/gpio/gpio_if.m (revision 4fd32b1e7f54811bd3a6f362493d256db40bb686)
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	static int
67	gpio_default_get_pin_list(device_t dev, uint32_t *pin_list)
68	{
69		uint32_t maxpin;
70		int err;
71
72		err = GPIO_PIN_MAX(dev, &maxpin);
73		if (err != 0)
74			return (ENXIO);
75
76		for (int i = 0; i <= maxpin; i++)
77			pin_list[i] = i;
78
79		return (0);
80	}
81};
82
83HEADER {
84	#include <dev/ofw/openfirm.h>
85};
86
87#
88# Return the gpiobus device reference
89#
90METHOD device_t get_bus {
91	device_t dev;
92} DEFAULT gpio_default_get_bus;
93
94#
95# Get maximum pin number
96#
97METHOD int pin_max {
98	device_t dev;
99	int *maxpin;
100};
101
102#
103# Set value of pin specified by pin_num
104#
105METHOD int pin_set {
106	device_t dev;
107	uint32_t pin_num;
108	uint32_t pin_value;
109};
110
111#
112# Get value of pin specified by pin_num
113#
114METHOD int pin_get {
115	device_t dev;
116	uint32_t pin_num;
117	uint32_t *pin_value;
118};
119
120#
121# Toggle value of pin specified by pin_num
122#
123METHOD int pin_toggle {
124	device_t dev;
125	uint32_t pin_num;
126};
127
128#
129# Get pin capabilities
130#
131METHOD int pin_getcaps {
132	device_t dev;
133	uint32_t pin_num;
134	uint32_t *caps;
135};
136
137#
138# Get pin flags
139#
140METHOD int pin_getflags {
141	device_t dev;
142	uint32_t pin_num;
143	uint32_t *flags;
144};
145
146#
147# Get pin name
148#
149METHOD int pin_getname {
150	device_t dev;
151	uint32_t pin_num;
152	char *name;
153};
154
155#
156# Set current configuration and capabilities
157#
158METHOD int pin_setflags {
159	device_t dev;
160	uint32_t pin_num;
161	uint32_t flags;
162};
163
164#
165# Allow the GPIO controller to map the gpio-specifier on its own.
166#
167METHOD int map_gpios {
168        device_t bus;
169        phandle_t dev;
170        phandle_t gparent;
171        int gcells;
172        pcell_t *gpios;
173        uint32_t *pin;
174        uint32_t *flags;
175} DEFAULT gpio_default_map_gpios;
176
177#
178# Simultaneously read and/or change up to 32 adjacent pins.
179# If the device cannot change the pins simultaneously, returns EOPNOTSUPP.
180#
181# More details about using this interface can be found in sys/gpio.h
182#
183METHOD int pin_access_32 {
184	device_t dev;
185	uint32_t first_pin;
186	uint32_t clear_pins;
187	uint32_t change_pins;
188	uint32_t *orig_pins;
189} DEFAULT gpio_default_nosupport;
190
191#
192# Simultaneously configure up to 32 adjacent pins.
193# This is intended to change the configuration of all the pins simultaneously,
194# but unlike pin_access_32, this will not fail if the hardware can't do so.
195#
196# More details about using this interface can be found in sys/gpio.h
197#
198METHOD int pin_config_32 {
199	device_t dev;
200	uint32_t first_pin;
201	uint32_t num_pins;
202	uint32_t *pin_flags;
203} DEFAULT gpio_default_nosupport;
204
205#
206# Get the controller's pin numbers. pin_list is expected to be an array with at
207# least GPIO_PIN_MAX() elements. Populates pin_list from 0 to GPIO_PIN_MAX() by
208# default.
209#
210METHOD int get_pin_list {
211	device_t dev;
212	uint32_t *pin_list;
213} DEFAULT gpio_default_get_pin_list;
214