The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/gpio/gpioled_fdt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_platform.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/bus.h>
   35 #include <sys/gpio.h>
   36 #include <sys/kernel.h>
   37 #include <sys/lock.h>
   38 #include <sys/malloc.h>
   39 #include <sys/module.h>
   40 #include <sys/mutex.h>
   41 
   42 #include <dev/fdt/fdt_common.h>
   43 #include <dev/ofw/ofw_bus.h>
   44 
   45 #include <dev/gpio/gpiobusvar.h>
   46 #include <dev/led/led.h>
   47 
   48 #include "gpiobus_if.h"
   49 
   50 struct gpioled
   51 {
   52         struct gpioleds_softc   *parent_sc;
   53         gpio_pin_t              pin;
   54         struct cdev             *leddev;
   55 };
   56 
   57 struct gpioleds_softc
   58 {
   59         device_t        sc_dev;
   60         device_t        sc_busdev;
   61         struct gpioled  *sc_leds;
   62         int             sc_total_leds;
   63 };
   64 
   65 static void gpioled_control(void *, int);
   66 static int gpioled_probe(device_t);
   67 static int gpioled_attach(device_t);
   68 static int gpioled_detach(device_t);
   69 
   70 static void
   71 gpioled_control(void *priv, int onoff)
   72 {
   73         struct gpioled *led;
   74 
   75         led = (struct gpioled *)priv;
   76         if (led->pin)
   77                 gpio_pin_set_active(led->pin, onoff);
   78 }
   79 
   80 static void
   81 gpioleds_attach_led(struct gpioleds_softc *sc, phandle_t node,
   82     struct gpioled *led)
   83 {
   84         char *name;
   85         int state, err;
   86         char *default_state;
   87 
   88         led->parent_sc = sc;
   89 
   90         state = 0;
   91         if (OF_getprop_alloc(node, "default-state",
   92             (void **)&default_state) != -1) {
   93                 if (strcasecmp(default_state, "on") == 0)
   94                         state = 1;
   95                 else if (strcasecmp(default_state, "off") == 0)
   96                         state = 0;
   97                 else if (strcasecmp(default_state, "keep") == 0)
   98                         state = -1;
   99                 else {
  100                         state = -1;
  101                         device_printf(sc->sc_dev,
  102                             "unknown value for default-state in FDT\n");
  103                 }
  104                 OF_prop_free(default_state);
  105         }
  106 
  107         name = NULL;
  108         if (OF_getprop_alloc(node, "label", (void **)&name) == -1)
  109                 OF_getprop_alloc(node, "name", (void **)&name);
  110 
  111         if (name == NULL) {
  112                 device_printf(sc->sc_dev,
  113                     "no name provided for gpio LED, skipping\n");
  114                 return;
  115         }
  116 
  117         err = gpio_pin_get_by_ofw_idx(sc->sc_dev, node, 0, &led->pin);
  118         if (err) {
  119                 device_printf(sc->sc_dev, "<%s> failed to map pin\n", name);
  120                 if (name)
  121                         OF_prop_free(name);
  122                 return;
  123         }
  124         gpio_pin_setflags(led->pin, GPIO_PIN_OUTPUT);
  125 
  126         led->leddev = led_create_state(gpioled_control, led, name,
  127             state);
  128 
  129         if (name != NULL)
  130                 OF_prop_free(name);
  131 }
  132 
  133 static void
  134 gpioleds_detach_led(struct gpioled *led)
  135 {
  136 
  137         if (led->leddev != NULL)
  138                 led_destroy(led->leddev);
  139 
  140         if (led->pin)
  141                 gpio_pin_release(led->pin);
  142 }
  143 
  144 static int
  145 gpioled_probe(device_t dev)
  146 {
  147         if (!ofw_bus_is_compatible(dev, "gpio-leds"))
  148                 return (ENXIO);
  149 
  150         device_set_desc(dev, "GPIO LEDs");
  151 
  152         return (BUS_PROBE_DEFAULT);
  153 }
  154 
  155 static int
  156 gpioled_attach(device_t dev)
  157 {
  158         struct gpioleds_softc *sc;
  159         phandle_t child, leds;
  160         int total_leds;
  161 
  162         if ((leds = ofw_bus_get_node(dev)) == -1)
  163                 return (ENXIO);
  164 
  165         sc = device_get_softc(dev);
  166         sc->sc_dev = dev;
  167         sc->sc_busdev = device_get_parent(dev);
  168 
  169         /* Traverse the 'gpio-leds' node and count leds */
  170         total_leds = 0;
  171         for (child = OF_child(leds); child != 0; child = OF_peer(child)) {
  172                 if (!OF_hasprop(child, "gpios"))
  173                         continue;
  174                 total_leds++;
  175         }
  176 
  177         if (total_leds) {
  178                 sc->sc_leds =  malloc(sizeof(struct gpioled) * total_leds,
  179                     M_DEVBUF, M_WAITOK | M_ZERO);
  180 
  181                 sc->sc_total_leds = 0;
  182                 /* Traverse the 'gpio-leds' node and count leds */
  183                 for (child = OF_child(leds); child != 0; child = OF_peer(child)) {
  184                         if (!OF_hasprop(child, "gpios"))
  185                                 continue;
  186                         gpioleds_attach_led(sc, child, &sc->sc_leds[sc->sc_total_leds]);
  187                         sc->sc_total_leds++;
  188                 }
  189         }
  190 
  191         return (0);
  192 }
  193 
  194 static int
  195 gpioled_detach(device_t dev)
  196 {
  197         struct gpioleds_softc *sc;
  198         int i;
  199 
  200         sc = device_get_softc(dev);
  201 
  202         for (i = 0; i < sc->sc_total_leds; i++)
  203                 gpioleds_detach_led(&sc->sc_leds[i]);
  204 
  205         if (sc->sc_leds)
  206                 free(sc->sc_leds, M_DEVBUF);
  207 
  208         return (0);
  209 }
  210 
  211 static devclass_t gpioled_devclass;
  212 
  213 static device_method_t gpioled_methods[] = {
  214         /* Device interface */
  215         DEVMETHOD(device_probe,         gpioled_probe),
  216         DEVMETHOD(device_attach,        gpioled_attach),
  217         DEVMETHOD(device_detach,        gpioled_detach),
  218 
  219         DEVMETHOD_END
  220 };
  221 
  222 static driver_t gpioled_driver = {
  223         "gpioled",
  224         gpioled_methods,
  225         sizeof(struct gpioleds_softc),
  226 };
  227 
  228 DRIVER_MODULE(gpioled, ofwbus, gpioled_driver, gpioled_devclass, 0, 0);
  229 DRIVER_MODULE(gpioled, simplebus, gpioled_driver, gpioled_devclass, 0, 0);
  230 MODULE_DEPEND(gpioled, gpiobus, 1, 1, 1);

Cache object: f393f749c5c086d2072258140d7a48c5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.