11.1Sjmcneill/* $NetBSD: gpiorfkill.c,v 1.1 2015/05/29 23:17:13 jmcneill Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 51.1Sjmcneill * All rights reserved. 61.1Sjmcneill * 71.1Sjmcneill * Redistribution and use in source and binary forms, with or without 81.1Sjmcneill * modification, are permitted provided that the following conditions 91.1Sjmcneill * are met: 101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer. 121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 131.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 141.1Sjmcneill * documentation and/or other materials provided with the distribution. 151.1Sjmcneill * 161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261.1Sjmcneill * SUCH DAMAGE. 271.1Sjmcneill */ 281.1Sjmcneill 291.1Sjmcneill#include "locators.h" 301.1Sjmcneill 311.1Sjmcneill#include <sys/cdefs.h> 321.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: gpiorfkill.c,v 1.1 2015/05/29 23:17:13 jmcneill Exp $"); 331.1Sjmcneill 341.1Sjmcneill#include <sys/param.h> 351.1Sjmcneill#include <sys/bus.h> 361.1Sjmcneill#include <sys/device.h> 371.1Sjmcneill#include <sys/intr.h> 381.1Sjmcneill#include <sys/systm.h> 391.1Sjmcneill#include <sys/kernel.h> 401.1Sjmcneill#include <sys/sysctl.h> 411.1Sjmcneill#include <sys/gpio.h> 421.1Sjmcneill 431.1Sjmcneill#include <dev/gpio/gpiovar.h> 441.1Sjmcneill 451.1Sjmcneillstatic int gpiorfkill_match(device_t, cfdata_t, void *); 461.1Sjmcneillstatic void gpiorfkill_attach(device_t, device_t, void *); 471.1Sjmcneill 481.1Sjmcneillstruct gpiorfkill_softc { 491.1Sjmcneill device_t sc_dev; 501.1Sjmcneill void *sc_gpio; 511.1Sjmcneill struct gpio_pinmap sc_map; 521.1Sjmcneill int sc_pinmap[1]; 531.1Sjmcneill 541.1Sjmcneill int sc_state; 551.1Sjmcneill 561.1Sjmcneill struct sysctllog *sc_sysctllog; 571.1Sjmcneill int sc_sysctlnode; 581.1Sjmcneill}; 591.1Sjmcneill 601.1Sjmcneillstatic void gpiorfkill_enable(struct gpiorfkill_softc *, int); 611.1Sjmcneillstatic void gpiorfkill_sysctl_init(struct gpiorfkill_softc *); 621.1Sjmcneillstatic int gpiorfkill_enable_helper(SYSCTLFN_PROTO); 631.1Sjmcneill 641.1SjmcneillCFATTACH_DECL_NEW(gpiorfkill, sizeof(struct gpiorfkill_softc), 651.1Sjmcneill gpiorfkill_match, gpiorfkill_attach, NULL, NULL); 661.1Sjmcneill 671.1Sjmcneillstatic int 681.1Sjmcneillgpiorfkill_match(device_t parent, cfdata_t cf, void *aux) 691.1Sjmcneill{ 701.1Sjmcneill struct gpio_attach_args * const ga = aux; 711.1Sjmcneill 721.1Sjmcneill if (strcmp(ga->ga_dvname, cf->cf_name) != 0) 731.1Sjmcneill return 0; 741.1Sjmcneill 751.1Sjmcneill if (ga->ga_offset == -1 || gpio_npins(ga->ga_mask) != 1) 761.1Sjmcneill return 0; 771.1Sjmcneill 781.1Sjmcneill return 1; 791.1Sjmcneill} 801.1Sjmcneill 811.1Sjmcneillstatic void 821.1Sjmcneillgpiorfkill_attach(device_t parent, device_t self, void *aux) 831.1Sjmcneill{ 841.1Sjmcneill struct gpiorfkill_softc * const sc = device_private(self); 851.1Sjmcneill struct gpio_attach_args * const ga = aux; 861.1Sjmcneill int caps; 871.1Sjmcneill 881.1Sjmcneill sc->sc_dev = self; 891.1Sjmcneill sc->sc_gpio = ga->ga_gpio; 901.1Sjmcneill sc->sc_map.pm_map = sc->sc_pinmap; 911.1Sjmcneill if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask, 921.1Sjmcneill &sc->sc_map)) { 931.1Sjmcneill aprint_error(": couldn't map pins\n"); 941.1Sjmcneill return; 951.1Sjmcneill } 961.1Sjmcneill 971.1Sjmcneill caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, 0); 981.1Sjmcneill if ((caps & GPIO_PIN_OUTPUT) == 0) { 991.1Sjmcneill aprint_error(": pin is not an output pin\n"); 1001.1Sjmcneill return; 1011.1Sjmcneill } 1021.1Sjmcneill 1031.1Sjmcneill gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_OUTPUT); 1041.1Sjmcneill 1051.1Sjmcneill aprint_naive("\n"); 1061.1Sjmcneill aprint_normal("\n"); 1071.1Sjmcneill 1081.1Sjmcneill gpiorfkill_enable(sc, 1); 1091.1Sjmcneill gpiorfkill_sysctl_init(sc); 1101.1Sjmcneill} 1111.1Sjmcneill 1121.1Sjmcneillstatic void 1131.1Sjmcneillgpiorfkill_enable(struct gpiorfkill_softc *sc, int enable) 1141.1Sjmcneill{ 1151.1Sjmcneill sc->sc_state = enable; 1161.1Sjmcneill gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, sc->sc_state); 1171.1Sjmcneill} 1181.1Sjmcneill 1191.1Sjmcneillstatic void 1201.1Sjmcneillgpiorfkill_sysctl_init(struct gpiorfkill_softc *sc) 1211.1Sjmcneill{ 1221.1Sjmcneill const struct sysctlnode *node, *devnode; 1231.1Sjmcneill int error; 1241.1Sjmcneill 1251.1Sjmcneill error = sysctl_createv(&sc->sc_sysctllog, 0, NULL, &devnode, 1261.1Sjmcneill 0, CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, 1271.1Sjmcneill NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 1281.1Sjmcneill if (error) 1291.1Sjmcneill goto sysctl_failed; 1301.1Sjmcneill 1311.1Sjmcneill error = sysctl_createv(&sc->sc_sysctllog, 0, &devnode, &node, 1321.1Sjmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "enable", NULL, 1331.1Sjmcneill gpiorfkill_enable_helper, 0, (void *)sc, 0, 1341.1Sjmcneill CTL_CREATE, CTL_EOL); 1351.1Sjmcneill if (error) 1361.1Sjmcneill goto sysctl_failed; 1371.1Sjmcneill sc->sc_sysctlnode = node->sysctl_num; 1381.1Sjmcneill 1391.1Sjmcneill return; 1401.1Sjmcneill 1411.1Sjmcneillsysctl_failed: 1421.1Sjmcneill aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes (%d)\n", 1431.1Sjmcneill error); 1441.1Sjmcneill sysctl_teardown(&sc->sc_sysctllog); 1451.1Sjmcneill} 1461.1Sjmcneill 1471.1Sjmcneillstatic int 1481.1Sjmcneillgpiorfkill_enable_helper(SYSCTLFN_ARGS) 1491.1Sjmcneill{ 1501.1Sjmcneill struct gpiorfkill_softc *sc; 1511.1Sjmcneill struct sysctlnode node; 1521.1Sjmcneill int error, enable; 1531.1Sjmcneill 1541.1Sjmcneill node = *rnode; 1551.1Sjmcneill sc = node.sysctl_data; 1561.1Sjmcneill enable = sc->sc_state; 1571.1Sjmcneill node.sysctl_data = &enable; 1581.1Sjmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1591.1Sjmcneill if (error || newp == NULL) 1601.1Sjmcneill return error; 1611.1Sjmcneill 1621.1Sjmcneill enable = !!enable; 1631.1Sjmcneill gpiorfkill_enable(sc, enable); 1641.1Sjmcneill 1651.1Sjmcneill return 0; 1661.1Sjmcneill} 167