1 1.7 riastrad /* $NetBSD: gpiopwm.c,v 1.7 2017/10/28 04:53:56 riastradh Exp $ */ 2 1.1 mbalmer 3 1.1 mbalmer /* 4 1.1 mbalmer * Copyright (c) 2011 Marc Balmer <marc (at) msys.ch> 5 1.1 mbalmer * All rights reserved. 6 1.1 mbalmer * 7 1.1 mbalmer * Redistribution and use in source and binary forms, with or without 8 1.1 mbalmer * modification, are permitted provided that the following conditions 9 1.1 mbalmer * are met: 10 1.1 mbalmer * 1. Redistributions of source code must retain the above copyright 11 1.1 mbalmer * notice, this list of conditions and the following disclaimer. 12 1.1 mbalmer * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 mbalmer * notice, this list of conditions and the following disclaimer in the 14 1.1 mbalmer * documentation and/or other materials provided with the distribution. 15 1.1 mbalmer * 16 1.1 mbalmer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 mbalmer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 mbalmer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 mbalmer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 mbalmer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 mbalmer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 mbalmer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 mbalmer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 mbalmer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 mbalmer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 mbalmer */ 27 1.1 mbalmer 28 1.1 mbalmer /* 29 1.1 mbalmer * Driver for pulsing GPIO pins in software 30 1.1 mbalmer */ 31 1.1 mbalmer 32 1.1 mbalmer #include <sys/param.h> 33 1.1 mbalmer #include <sys/device.h> 34 1.1 mbalmer #include <sys/gpio.h> 35 1.1 mbalmer #include <sys/sysctl.h> 36 1.1 mbalmer 37 1.1 mbalmer #include <dev/gpio/gpiovar.h> 38 1.1 mbalmer 39 1.7 riastrad #include "ioconf.h" 40 1.7 riastrad 41 1.1 mbalmer #define GPIOPWM_NPINS 1 42 1.1 mbalmer 43 1.1 mbalmer struct gpiopwm_softc { 44 1.1 mbalmer device_t sc_dev; 45 1.1 mbalmer void *sc_gpio; 46 1.1 mbalmer struct gpio_pinmap sc_map; 47 1.1 mbalmer int _map[GPIOPWM_NPINS]; 48 1.1 mbalmer 49 1.1 mbalmer callout_t sc_pulse; 50 1.1 mbalmer int sc_ticks_on; 51 1.1 mbalmer int sc_ticks_off; 52 1.1 mbalmer 53 1.1 mbalmer struct sysctllog *sc_log; 54 1.1 mbalmer int sc_dying; 55 1.1 mbalmer }; 56 1.1 mbalmer 57 1.1 mbalmer int gpiopwm_match(device_t, cfdata_t, void *); 58 1.1 mbalmer void gpiopwm_attach(device_t, device_t, void *); 59 1.1 mbalmer int gpiopwm_detach(device_t, int); 60 1.1 mbalmer int gpiopwm_activate(device_t, enum devact); 61 1.1 mbalmer static int gpiopwm_set_on(SYSCTLFN_ARGS); 62 1.1 mbalmer static int gpiopwm_set_off(SYSCTLFN_ARGS); 63 1.1 mbalmer static void gpiopwm_pulse(void *); 64 1.1 mbalmer 65 1.1 mbalmer CFATTACH_DECL_NEW(gpiopwm, sizeof(struct gpiopwm_softc), 66 1.1 mbalmer gpiopwm_match, gpiopwm_attach, gpiopwm_detach, gpiopwm_activate); 67 1.1 mbalmer 68 1.1 mbalmer int 69 1.1 mbalmer gpiopwm_match(device_t parent, cfdata_t cf, 70 1.1 mbalmer void *aux) 71 1.1 mbalmer { 72 1.1 mbalmer struct gpio_attach_args *ga = aux; 73 1.1 mbalmer 74 1.1 mbalmer if (strcmp(ga->ga_dvname, cf->cf_name)) 75 1.1 mbalmer return 0; 76 1.1 mbalmer 77 1.1 mbalmer if (ga->ga_offset == -1) 78 1.1 mbalmer return 0; 79 1.1 mbalmer 80 1.1 mbalmer /* Check number of pins, must be 1 */ 81 1.1 mbalmer if (gpio_npins(ga->ga_mask) != GPIOPWM_NPINS) { 82 1.1 mbalmer aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name, 83 1.1 mbalmer ga->ga_mask); 84 1.1 mbalmer return 0; 85 1.1 mbalmer } 86 1.1 mbalmer return 1; 87 1.1 mbalmer } 88 1.1 mbalmer 89 1.1 mbalmer void 90 1.1 mbalmer gpiopwm_attach(device_t parent, device_t self, void *aux) 91 1.1 mbalmer { 92 1.1 mbalmer struct gpiopwm_softc *sc = device_private(self); 93 1.1 mbalmer struct gpio_attach_args *ga = aux; 94 1.1 mbalmer const struct sysctlnode *node; 95 1.1 mbalmer 96 1.1 mbalmer sc->sc_dev = self; 97 1.1 mbalmer 98 1.1 mbalmer /* Map pin */ 99 1.1 mbalmer sc->sc_gpio = ga->ga_gpio; 100 1.1 mbalmer sc->sc_map.pm_map = sc->_map; 101 1.1 mbalmer if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 102 1.1 mbalmer &sc->sc_map)) { 103 1.1 mbalmer aprint_error(": can't map pin\n"); 104 1.1 mbalmer return; 105 1.1 mbalmer } 106 1.1 mbalmer aprint_normal(" [%d]", sc->sc_map.pm_map[0]); 107 1.6 maya if (!pmf_device_register(self, NULL, NULL)) 108 1.6 maya aprint_error_dev(self, "couldn't establish power handler\n"); 109 1.1 mbalmer 110 1.1 mbalmer callout_init(&sc->sc_pulse, CALLOUT_MPSAFE); 111 1.1 mbalmer callout_setfunc(&sc->sc_pulse, gpiopwm_pulse, sc); 112 1.1 mbalmer 113 1.5 msaitoh sysctl_createv(&sc->sc_log, 0, NULL, &node, 114 1.5 msaitoh 0, 115 1.5 msaitoh CTLTYPE_NODE, device_xname(sc->sc_dev), 116 1.5 msaitoh SYSCTL_DESCR("GPIO software PWM"), 117 1.5 msaitoh NULL, 0, NULL, 0, 118 1.5 msaitoh CTL_HW, CTL_CREATE, CTL_EOL); 119 1.5 msaitoh 120 1.5 msaitoh if (node == NULL) { 121 1.5 msaitoh aprint_error(": can't create sysctl node\n"); 122 1.5 msaitoh return; 123 1.5 msaitoh } 124 1.5 msaitoh 125 1.5 msaitoh sysctl_createv(&sc->sc_log, 0, &node, NULL, 126 1.5 msaitoh CTLFLAG_READWRITE, 127 1.5 msaitoh CTLTYPE_INT, "on", 128 1.5 msaitoh SYSCTL_DESCR("PWM 'on' period in ticks"), 129 1.5 msaitoh gpiopwm_set_on, 0, (void *)sc, 0, 130 1.1 mbalmer CTL_CREATE, CTL_EOL); 131 1.5 msaitoh sysctl_createv(&sc->sc_log, 0, &node, NULL, 132 1.5 msaitoh CTLFLAG_READWRITE, 133 1.5 msaitoh CTLTYPE_INT, "off", 134 1.5 msaitoh SYSCTL_DESCR("PWM 'off' period in ticks"), 135 1.5 msaitoh gpiopwm_set_off, 0, (void *)sc, 0, 136 1.1 mbalmer CTL_CREATE, CTL_EOL); 137 1.1 mbalmer 138 1.1 mbalmer aprint_normal("\n"); 139 1.1 mbalmer return; 140 1.1 mbalmer } 141 1.1 mbalmer 142 1.1 mbalmer int 143 1.1 mbalmer gpiopwm_detach(device_t self, int flags) 144 1.1 mbalmer { 145 1.1 mbalmer struct gpiopwm_softc *sc = device_private(self); 146 1.1 mbalmer 147 1.1 mbalmer callout_halt(&sc->sc_pulse, NULL); 148 1.1 mbalmer callout_destroy(&sc->sc_pulse); 149 1.2 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); 150 1.1 mbalmer 151 1.1 mbalmer pmf_device_deregister(self); 152 1.1 mbalmer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); 153 1.1 mbalmer 154 1.1 mbalmer if (sc->sc_log != NULL) { 155 1.1 mbalmer sysctl_teardown(&sc->sc_log); 156 1.1 mbalmer sc->sc_log = NULL; 157 1.1 mbalmer } 158 1.1 mbalmer return 0; 159 1.1 mbalmer } 160 1.1 mbalmer 161 1.1 mbalmer static int 162 1.1 mbalmer gpiopwm_set_on(SYSCTLFN_ARGS) 163 1.1 mbalmer { 164 1.1 mbalmer struct sysctlnode node; 165 1.1 mbalmer struct gpiopwm_softc *sc; 166 1.1 mbalmer int val, error; 167 1.1 mbalmer 168 1.1 mbalmer node = *rnode; 169 1.1 mbalmer sc = node.sysctl_data; 170 1.1 mbalmer 171 1.1 mbalmer callout_halt(&sc->sc_pulse, NULL); 172 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); 173 1.1 mbalmer node.sysctl_data = &val; 174 1.1 mbalmer 175 1.1 mbalmer val = sc->sc_ticks_on; 176 1.1 mbalmer error = sysctl_lookup(SYSCTLFN_CALL(&node)); 177 1.1 mbalmer if (error || newp == NULL) 178 1.1 mbalmer return error; 179 1.1 mbalmer 180 1.1 mbalmer sc->sc_ticks_on = val; 181 1.1 mbalmer if (sc->sc_ticks_on > 0 && sc->sc_ticks_off > 0) { 182 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH); 183 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_on); 184 1.1 mbalmer } 185 1.1 mbalmer return 0; 186 1.1 mbalmer } 187 1.1 mbalmer 188 1.1 mbalmer static int 189 1.1 mbalmer gpiopwm_set_off(SYSCTLFN_ARGS) 190 1.1 mbalmer { 191 1.1 mbalmer struct sysctlnode node; 192 1.1 mbalmer struct gpiopwm_softc *sc; 193 1.1 mbalmer int val, error; 194 1.1 mbalmer 195 1.1 mbalmer node = *rnode; 196 1.1 mbalmer sc = node.sysctl_data; 197 1.1 mbalmer 198 1.1 mbalmer callout_halt(&sc->sc_pulse, NULL); 199 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); 200 1.1 mbalmer node.sysctl_data = &val; 201 1.1 mbalmer 202 1.1 mbalmer val = sc->sc_ticks_off; 203 1.1 mbalmer error = sysctl_lookup(SYSCTLFN_CALL(&node)); 204 1.1 mbalmer if (error || newp == NULL) 205 1.1 mbalmer return error; 206 1.1 mbalmer 207 1.1 mbalmer sc->sc_ticks_off = val; 208 1.1 mbalmer if (sc->sc_ticks_on > 0 && sc->sc_ticks_off > 0) { 209 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH); 210 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_on); 211 1.1 mbalmer } 212 1.1 mbalmer return 0; 213 1.1 mbalmer } 214 1.1 mbalmer 215 1.1 mbalmer static void 216 1.1 mbalmer gpiopwm_pulse(void *arg) 217 1.1 mbalmer { 218 1.1 mbalmer struct gpiopwm_softc *sc; 219 1.1 mbalmer 220 1.1 mbalmer sc = arg; 221 1.1 mbalmer if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0) == GPIO_PIN_HIGH) { 222 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); 223 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_off); 224 1.1 mbalmer } else { 225 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH); 226 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_on); 227 1.1 mbalmer } 228 1.1 mbalmer } 229 1.1 mbalmer 230 1.1 mbalmer int 231 1.1 mbalmer gpiopwm_activate(device_t self, enum devact act) 232 1.1 mbalmer { 233 1.1 mbalmer struct gpiopwm_softc *sc = device_private(self); 234 1.1 mbalmer 235 1.1 mbalmer switch (act) { 236 1.1 mbalmer case DVACT_DEACTIVATE: 237 1.1 mbalmer sc->sc_dying = 1; 238 1.1 mbalmer return 0; 239 1.1 mbalmer default: 240 1.1 mbalmer return EOPNOTSUPP; 241 1.1 mbalmer } 242 1.1 mbalmer } 243