xref: /netbsd-src/sys/dev/gpio/gpiovar.h (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* $NetBSD: gpiovar.h,v 1.17 2017/07/06 10:43:06 jmcneill Exp $ */
2 /*	$OpenBSD: gpiovar.h,v 1.3 2006/01/14 12:33:49 grange Exp $	*/
3 
4 /*
5  * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _DEV_GPIO_GPIOVAR_H_
21 #define _DEV_GPIO_GPIOVAR_H_
22 
23 #include <sys/device.h>
24 
25 /* GPIO controller description */
26 typedef struct gpio_chipset_tag {
27 	void	*gp_cookie;
28 
29 	int	(*gp_gc_open)(void *, device_t);
30 	void    (*gp_gc_close)(void *, device_t);
31 	int	(*gp_pin_read)(void *, int);
32 	void	(*gp_pin_write)(void *, int, int);
33 	void	(*gp_pin_ctl)(void *, int, int);
34 	void	(*gp_pin_irqen)(void *, int, bool);
35 } *gpio_chipset_tag_t;
36 
37 /* GPIO pin description */
38 typedef struct gpio_pin {
39 	int			pin_num;	/* number */
40 	int			pin_caps;	/* capabilities */
41 	int			pin_flags;	/* current configuration */
42 	int			pin_state;	/* current state */
43 	int			pin_mapped;	/* is mapped */
44 	gpio_chipset_tag_t	pin_gc;		/* reference the controller */
45 	void			(*pin_callback)(void *); /* irq callback */
46 	void *			pin_callback_arg; /* callback arg */
47 	char			pin_defname[GPIOMAXNAME]; /* default name */
48 } gpio_pin_t;
49 
50 /* Attach GPIO framework to the controller */
51 struct gpiobus_attach_args {
52 	gpio_chipset_tag_t	 gba_gc;	/* underlying controller */
53 	gpio_pin_t		*gba_pins;	/* pins array */
54 	int			 gba_npins;	/* total number of pins */
55 };
56 
57 int gpiobus_print(void *, const char *);
58 
59 /* GPIO framework private methods */
60 #define gpiobus_open(gc, dev) \
61     ((gc)->gp_gc_open ? ((gc)->gp_gc_open((gc)->gp_cookie, dev)) : 0)
62 #define gpiobus_close(gc, dev) \
63     ((gc)->gp_gc_close ? ((gc)->gp_gc_close((gc)->gp_cookie, dev)), 1 : 0)
64 #define gpiobus_pin_read(gc, pin) \
65     ((gc)->gp_pin_read((gc)->gp_cookie, (pin)))
66 #define gpiobus_pin_write(gc, pin, value) \
67     ((gc)->gp_pin_write((gc)->gp_cookie, (pin), (value)))
68 #define gpiobus_pin_ctl(gc, pin, flags) \
69     ((gc)->gp_pin_ctl((gc)->gp_cookie, (pin), (flags)))
70 #define gpiobus_pin_irqen(gc, pin, en) \
71     ((gc)->gp_pin_irqen((gc)->gp_cookie, (pin), (en)))
72 
73 /* Attach devices connected to the GPIO pins */
74 struct gpio_attach_args {
75 	void		*ga_gpio;
76 	int		 ga_offset;
77 	uint32_t	 ga_mask;
78 	char		*ga_dvname;
79 	uint32_t	 ga_flags;
80 };
81 
82 /* GPIO pin map */
83 struct gpio_pinmap {
84 	int		*pm_map;		/* pin map */
85 	int		 pm_size;		/* map size */
86 };
87 
88 struct gpio_dev {
89 	device_t		sc_dev;	/* the gpio device */
90 	LIST_ENTRY(gpio_dev)	sc_next;
91 };
92 
93 struct gpio_name {
94 	char			gp_name[GPIOMAXNAME];
95 	int			gp_pin;
96 	LIST_ENTRY(gpio_name)	gp_next;
97 };
98 
99 void *	gpio_find_device(const char *);
100 const char * gpio_get_name(void *gpio);
101 int	gpio_pin_can_map(void *, int, uint32_t);
102 int	gpio_pin_map(void *, int, uint32_t, struct gpio_pinmap *);
103 void	gpio_pin_unmap(void *, struct gpio_pinmap *);
104 int	gpio_pin_read(void *, struct gpio_pinmap *, int);
105 void	gpio_pin_write(void *, struct gpio_pinmap *, int, int);
106 void	gpio_pin_ctl(void *, struct gpio_pinmap *, int, int);
107 int	gpio_pin_ctl_intr(void *, struct gpio_pinmap *, int, int,
108 			int, void (*)(void *), void *);
109 void	gpio_pin_irqen(void *, struct gpio_pinmap *, int, bool);
110 int	gpio_pin_caps(void *, struct gpio_pinmap *, int);
111 int	gpio_pin_wait(void *, int);
112 int	gpio_npins(uint32_t);
113 
114 int	gpio_lock(void *);
115 void	gpio_unlock(void *);
116 
117 void	gpio_intr(device_t, u_int32_t);
118 
119 #endif	/* !_DEV_GPIO_GPIOVAR_H_ */
120