1248911Sian/*-
2248911Sian * Copyright (C) 2006 M. Warner Losh. All rights reserved.
3248911Sian * Copyright (C) 2012 Ian Lepore. All rights reserved.
4248911Sian *
5248911Sian * Redistribution and use in source and binary forms, with or without
6248911Sian * modification, are permitted provided that the following conditions
7248911Sian * are met:
8248911Sian * 1. Redistributions of source code must retain the above copyright
9248911Sian *    notice, this list of conditions and the following disclaimer.
10248911Sian * 2. Redistributions in binary form must reproduce the above copyright
11248911Sian *    notice, this list of conditions and the following disclaimer in the
12248911Sian *    documentation and/or other materials provided with the distribution.
13248911Sian *
14248911Sian * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15248911Sian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16248911Sian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17248911Sian * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18248911Sian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19248911Sian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20248911Sian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21248911Sian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22248911Sian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23248911Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24248911Sian * SUCH DAMAGE.
25248911Sian *
26248911Sian * $FreeBSD$
27248911Sian */
28248911Sian
29248911Sian#ifndef _ARM_AT91_GPIO_H
30248911Sian#define _ARM_AT91_GPIO_H
31248911Sian
32248911Sian#ifndef _KERNEL
33248911Sian#include <sys/types.h>
34248911Sian#endif
35248911Sian#include <sys/ioccom.h>
36248911Sian
37248911Sian/* Userland GPIO API for Atmel AT91 series SOC.
38248911Sian *
39248911Sian * Open /dev/pioN (where N is 0 for PIOA, 1 for PIOB, etc), and use ioctl(2)
40248911Sian * calls to configure the pin(s) as needed.
41248911Sian *
42248911Sian * The userland interrupt support allows you to use read(2) and/or select(2) to
43248911Sian * get notified of interrupts on PIO pins for which you enabled interrupt
44248911Sian * notifications.  Each time an interrupt occurs on a given pin, that pin number
45248911Sian * is written into a buffer as a uint8_t.  Thus, reading from /dev/pioN delivers
46248911Sian * info on which interrupt(s) have occurred since the last read.  You can also
47248911Sian * use select() to block until an interrupt occurs (you still need to read() to
48248911Sian * consume the interrupt number bytes from the buffer.)
49248911Sian */
50248911Sian
51248911Sianstruct at91_gpio_info
52248911Sian{
53248911Sian	uint32_t	output_status;	/* Current state of output pins */
54248911Sian	uint32_t	input_status;	/* 1->out 0->in bitmask */
55248911Sian	uint32_t	highz_status;	/* 1->highz 0->driven bitmask */
56248911Sian	uint32_t	pullup_status;	/* 1->floating 0->pullup engaged */
57248911Sian	uint32_t	glitch_status;	/* 0-> no glitch filter 1->gf */
58248911Sian	uint32_t	enabled_status;	/* 1->used for pio 0->other */
59248911Sian	uint32_t	periph_status;	/* 0->A periph 1->B periph */
60248911Sian	uint32_t	intr_status;	/* 1-> ISR enabled, 0->disabled */
61248911Sian	uint32_t	extra_status[8];/* Extra status info, device depend */
62248911Sian};
63248911Sian
64248911Sianstruct at91_gpio_cfg
65248911Sian{
66248911Sian	uint32_t	cfgmask;	/* which things change */
67248911Sian#define	AT91_GPIO_CFG_INPUT 	0x01	/* configure input/output pins */
68248911Sian#define	AT91_GPIO_CFG_HI_Z  	0x02	/* HiZ */
69248911Sian#define	AT91_GPIO_CFG_PULLUP	0x04	/* Enable/disable pullup resistors */
70248911Sian#define	AT91_GPIO_CFG_GLITCH	0x08	/* Glitch filtering */
71248911Sian#define	AT91_GPIO_CFG_GPIO  	0x10	/* Use pin for PIO or peripheral */
72248911Sian#define	AT91_GPIO_CFG_PERIPH	0x20	/* Select which peripheral to use */
73248911Sian#define	AT91_GPIO_CFG_INTR  	0x40	/* Select pin for interrupts */
74248911Sian	uint32_t	iomask;		/* Mask of bits to change */
75248911Sian	uint32_t	input;		/* or output */
76248911Sian	uint32_t	hi_z;		/* Disable output */
77248911Sian	uint32_t	pullup;		/* Enable pullup resistor */
78248911Sian	uint32_t	glitch;		/* Glitch filtering */
79248911Sian	uint32_t	gpio;		/* Enabled for PIO (1) or periph (0) */
80248911Sian	uint32_t	periph;		/* Select periph A (0) or periph B (1) */
81248911Sian	uint32_t	intr;		/* Enable interrupt (1), or not (0) */
82248911Sian};
83248911Sian
84248911Sianstruct at91_gpio_bang
85248911Sian{
86248911Sian	uint32_t	clockpin;	/* clock pin MASK */
87248911Sian	uint32_t	datapin; 	/* Data pin MASK */
88248911Sian	uint32_t	bits;		/* bits to clock out (all 32) */
89248911Sian};
90248911Sian
91248911Sianstruct at91_gpio_bang_many
92248911Sian{
93248911Sian	uint32_t	clockpin;	/* clock pin MASK */
94248911Sian	uint32_t	datapin;	/* Data pin MASK */
95248911Sian	void		*bits;		/* bits to clock out */
96248911Sian	uint32_t	numbits;	/* Number of bits to clock out */
97248911Sian};
98248911Sian
99248911Sian#define	AT91_GPIO_SET		_IOW('g', 0, uint32_t)			/* Turn bits on */
100248911Sian#define	AT91_GPIO_CLR		_IOW('g', 1, uint32_t)			/* Turn bits off */
101248911Sian#define	AT91_GPIO_READ		_IOR('g', 2, uint32_t)			/* Read input bit state */
102248911Sian#define	AT91_GPIO_INFO		_IOR('g', 3, struct at91_gpio_info)	/* State of pio cfg */
103248911Sian#define	AT91_GPIO_CFG		_IOW('g', 4, struct at91_gpio_cfg)	/* Configure pio */
104248911Sian#define	AT91_GPIO_BANG		_IOW('g', 5, struct at91_gpio_bang)	/* bit bang 32 bits */
105248911Sian#define	AT91_GPIO_BANG_MANY	_IOW('g', 6, struct at91_gpio_bang_many)/* bit bang >32 bits */
106248911Sian
107248911Sian#endif /* _ARM_AT91_GPIO_H */
108248911Sian
109