1 /* $NetBSD: gpiovar.h,v 1.20 2024/12/08 20:40:38 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 35 void * (*gp_intr_establish)(void *, int, int, int, 36 int (*)(void *), void *); 37 void (*gp_intr_disestablish)(void *, void *); 38 bool (*gp_intr_str)(void *, int, int, char *, size_t); 39 void (*gp_intr_mask)(void *, void *); 40 void (*gp_intr_unmask)(void *, void *); 41 } *gpio_chipset_tag_t; 42 43 /* GPIO pin description */ 44 typedef struct gpio_pin { 45 int pin_num; /* number */ 46 int pin_caps; /* capabilities */ 47 int pin_flags; /* current configuration */ 48 int pin_state; /* current state */ 49 int pin_mapped; /* is mapped */ 50 gpio_chipset_tag_t pin_gc; /* reference the controller */ 51 char pin_defname[GPIOMAXNAME]; /* default name */ 52 int pin_intrcaps; /* interrupt capabilities */ 53 } gpio_pin_t; 54 55 /* Attach GPIO framework to the controller */ 56 struct gpiobus_attach_args { 57 gpio_chipset_tag_t gba_gc; /* underlying controller */ 58 gpio_pin_t *gba_pins; /* pins array */ 59 int gba_npins; /* total number of pins */ 60 }; 61 62 int gpiobus_print(void *, const char *); 63 64 /* GPIO framework private methods */ 65 #define gpiobus_open(gc, dev) \ 66 ((gc)->gp_gc_open ? ((gc)->gp_gc_open((gc)->gp_cookie, dev)) : 0) 67 #define gpiobus_close(gc, dev) \ 68 ((gc)->gp_gc_close ? ((gc)->gp_gc_close((gc)->gp_cookie, dev)), 1 : 0) 69 #define gpiobus_pin_read(gc, pin) \ 70 ((gc)->gp_pin_read((gc)->gp_cookie, (pin))) 71 #define gpiobus_pin_write(gc, pin, value) \ 72 ((gc)->gp_pin_write((gc)->gp_cookie, (pin), (value))) 73 #define gpiobus_pin_ctl(gc, pin, flags) \ 74 ((gc)->gp_pin_ctl((gc)->gp_cookie, (pin), (flags))) 75 76 /* Attach devices connected to the GPIO pins */ 77 struct gpio_attach_args { 78 void *ga_gpio; 79 int ga_offset; 80 uint32_t ga_mask; 81 char *ga_dvname; 82 uint32_t ga_flags; /* driver-specific flags */ 83 }; 84 85 /* GPIO pin map */ 86 struct gpio_pinmap { 87 int *pm_map; /* pin map */ 88 int pm_size; /* map size */ 89 }; 90 91 struct gpio_dev { 92 device_t sc_dev; /* the gpio device */ 93 LIST_ENTRY(gpio_dev) sc_next; 94 }; 95 96 struct gpio_name { 97 char gp_name[GPIOMAXNAME]; 98 int gp_pin; 99 LIST_ENTRY(gpio_name) gp_next; 100 }; 101 102 void * gpio_find_device(const char *); 103 const char * gpio_get_name(void *gpio); 104 int gpio_pin_can_map(void *, int, uint32_t); 105 int gpio_pin_map(void *, int, uint32_t, struct gpio_pinmap *); 106 void gpio_pin_unmap(void *, struct gpio_pinmap *); 107 int gpio_pin_read(void *, struct gpio_pinmap *, int); 108 void gpio_pin_write(void *, struct gpio_pinmap *, int, int); 109 int gpio_pin_caps(void *, struct gpio_pinmap *, int); 110 int gpio_pin_get_conf(void *, struct gpio_pinmap *, int); 111 bool gpio_pin_set_conf(void *, struct gpio_pinmap *, int, int); 112 void gpio_pin_ctl(void *, struct gpio_pinmap *, int, int); 113 int gpio_pin_intrcaps(void *, struct gpio_pinmap *, int); 114 bool gpio_pin_irqmode_issupported(void *, struct gpio_pinmap *, int, int); 115 int gpio_pin_wait(void *, int); 116 int gpio_npins(uint32_t); 117 118 void * gpio_intr_establish(void *, struct gpio_pinmap *, int, int, int, 119 int (*)(void *), void *); 120 void gpio_intr_disestablish(void *, void *); 121 bool gpio_intr_str(void *, struct gpio_pinmap *, int, int, 122 char *, size_t); 123 void gpio_intr_mask(void *, void *); 124 void gpio_intr_unmask(void *, void *); 125 int gpio_pin_to_pin_num(void *, struct gpio_pinmap *, int); 126 127 int gpio_lock(void *); 128 void gpio_unlock(void *); 129 130 #endif /* !_DEV_GPIO_GPIOVAR_H_ */ 131