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#
14213237Sgonzo# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15213237Sgonzo# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16213237Sgonzo# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17213237Sgonzo# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18213237Sgonzo# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19213237Sgonzo# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20213237Sgonzo# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21213237Sgonzo# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22213237Sgonzo# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23213237Sgonzo# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24213237Sgonzo# SUCH DAMAGE.
25213237Sgonzo#
26213237Sgonzo# $FreeBSD$
27213237Sgonzo#
28213237Sgonzo
29213237Sgonzo#include <sys/bus.h>
30213237Sgonzo#include <sys/gpio.h>
31213237Sgonzo
32213237SgonzoINTERFACE gpio;
33213237Sgonzo
34266135SloosCODE {
35266135Sloos	static gpio_map_gpios_t gpio_default_map_gpios;
36266135Sloos
37266135Sloos	int
38266135Sloos	gpio_default_map_gpios(device_t bus, phandle_t dev,
39266135Sloos	    phandle_t gparent, int gcells, pcell_t *gpios, uint32_t *pin,
40266135Sloos	    uint32_t *flags)
41266135Sloos	{
42266135Sloos		/* Propagate up the bus hierarchy until someone handles it. */  
43266135Sloos		if (device_get_parent(bus) != NULL)
44266135Sloos			return (GPIO_MAP_GPIOS(device_get_parent(bus), dev,
45266135Sloos			    gparent, gcells, gpios, pin, flags));
46266135Sloos
47266135Sloos		/* If that fails, then assume the FreeBSD defaults. */
48266135Sloos		*pin = gpios[0];
49266135Sloos		if (gcells == 2 || gcells == 3)
50266135Sloos			*flags = gpios[gcells - 1];
51266135Sloos
52266135Sloos		return (0);
53266135Sloos	}
54266135Sloos};
55266135Sloos
56266135SloosHEADER {
57266135Sloos	#include <dev/ofw/openfirm.h>
58266135Sloos};
59266135Sloos
60213237Sgonzo#
61213237Sgonzo# Get total number of pins
62213237Sgonzo#
63213237SgonzoMETHOD int pin_max {
64213237Sgonzo	device_t dev;
65213237Sgonzo	int *npins;
66213237Sgonzo};
67213237Sgonzo
68213237Sgonzo#
69213237Sgonzo# Set value of pin specifed by pin_num 
70213237Sgonzo#
71213237SgonzoMETHOD int pin_set {
72213237Sgonzo	device_t dev;
73213237Sgonzo	uint32_t pin_num;
74213237Sgonzo	uint32_t pin_value;
75213237Sgonzo};
76213237Sgonzo
77213237Sgonzo#
78213237Sgonzo# Get value of pin specifed by pin_num 
79213237Sgonzo#
80213237SgonzoMETHOD int pin_get {
81213237Sgonzo	device_t dev;
82213237Sgonzo	uint32_t pin_num;
83213237Sgonzo	uint32_t *pin_value;
84213237Sgonzo};
85213237Sgonzo
86213237Sgonzo#
87213237Sgonzo# Toggle value of pin specifed by pin_num 
88213237Sgonzo#
89213237SgonzoMETHOD int pin_toggle {
90213237Sgonzo	device_t dev;
91213237Sgonzo	uint32_t pin_num;
92213237Sgonzo};
93213237Sgonzo
94213237Sgonzo#
95213237Sgonzo# Get pin capabilities
96213237Sgonzo#
97213237SgonzoMETHOD int pin_getcaps {
98213237Sgonzo	device_t dev;
99213237Sgonzo	uint32_t pin_num;
100213237Sgonzo	uint32_t *caps;
101213237Sgonzo};
102213237Sgonzo
103213237Sgonzo#
104213237Sgonzo# Get pin flags
105213237Sgonzo#
106213237SgonzoMETHOD int pin_getflags {
107213237Sgonzo	device_t dev;
108213237Sgonzo	uint32_t pin_num;
109213237Sgonzo	uint32_t *flags;
110213237Sgonzo};
111213237Sgonzo
112213237Sgonzo#
113213237Sgonzo# Get pin name
114213237Sgonzo#
115213237SgonzoMETHOD int pin_getname {
116213237Sgonzo	device_t dev;
117213237Sgonzo	uint32_t pin_num;
118213237Sgonzo	char *name;
119213237Sgonzo};
120213237Sgonzo
121213237Sgonzo#
122213237Sgonzo# Set current configuration and capabilities
123213237Sgonzo#
124213237SgonzoMETHOD int pin_setflags {
125213237Sgonzo	device_t dev;
126213237Sgonzo	uint32_t pin_num;
127213237Sgonzo	uint32_t flags;
128213237Sgonzo};
129266135Sloos
130266135Sloos#
131266135Sloos# Allow the GPIO controller to map the gpio-specifier on its own.
132266135Sloos#
133266135SloosMETHOD int map_gpios {
134266135Sloos        device_t bus;
135266135Sloos        phandle_t dev;
136266135Sloos        phandle_t gparent;
137266135Sloos        int gcells;
138266135Sloos        pcell_t *gpios;
139266135Sloos        uint32_t *pin;
140266135Sloos        uint32_t *flags;
141266135Sloos} DEFAULT gpio_default_map_gpios;
142