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