xref: /netbsd-src/sys/dev/gpio/gpiorfkill.c (revision 252766dfd0c20cf16ea8358e2ce3510b073ef3fa)
1*252766dfSjmcneill /* $NetBSD: gpiorfkill.c,v 1.1 2015/05/29 23:17:13 jmcneill Exp $ */
2*252766dfSjmcneill 
3*252766dfSjmcneill /*-
4*252766dfSjmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5*252766dfSjmcneill  * All rights reserved.
6*252766dfSjmcneill  *
7*252766dfSjmcneill  * Redistribution and use in source and binary forms, with or without
8*252766dfSjmcneill  * modification, are permitted provided that the following conditions
9*252766dfSjmcneill  * are met:
10*252766dfSjmcneill  * 1. Redistributions of source code must retain the above copyright
11*252766dfSjmcneill  *    notice, this list of conditions and the following disclaimer.
12*252766dfSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13*252766dfSjmcneill  *    notice, this list of conditions and the following disclaimer in the
14*252766dfSjmcneill  *    documentation and/or other materials provided with the distribution.
15*252766dfSjmcneill  *
16*252766dfSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*252766dfSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*252766dfSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*252766dfSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*252766dfSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*252766dfSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*252766dfSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*252766dfSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*252766dfSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*252766dfSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*252766dfSjmcneill  * SUCH DAMAGE.
27*252766dfSjmcneill  */
28*252766dfSjmcneill 
29*252766dfSjmcneill #include "locators.h"
30*252766dfSjmcneill 
31*252766dfSjmcneill #include <sys/cdefs.h>
32*252766dfSjmcneill __KERNEL_RCSID(0, "$NetBSD: gpiorfkill.c,v 1.1 2015/05/29 23:17:13 jmcneill Exp $");
33*252766dfSjmcneill 
34*252766dfSjmcneill #include <sys/param.h>
35*252766dfSjmcneill #include <sys/bus.h>
36*252766dfSjmcneill #include <sys/device.h>
37*252766dfSjmcneill #include <sys/intr.h>
38*252766dfSjmcneill #include <sys/systm.h>
39*252766dfSjmcneill #include <sys/kernel.h>
40*252766dfSjmcneill #include <sys/sysctl.h>
41*252766dfSjmcneill #include <sys/gpio.h>
42*252766dfSjmcneill 
43*252766dfSjmcneill #include <dev/gpio/gpiovar.h>
44*252766dfSjmcneill 
45*252766dfSjmcneill static int	gpiorfkill_match(device_t, cfdata_t, void *);
46*252766dfSjmcneill static void	gpiorfkill_attach(device_t, device_t, void *);
47*252766dfSjmcneill 
48*252766dfSjmcneill struct gpiorfkill_softc {
49*252766dfSjmcneill 	device_t		sc_dev;
50*252766dfSjmcneill 	void			*sc_gpio;
51*252766dfSjmcneill 	struct gpio_pinmap	sc_map;
52*252766dfSjmcneill 	int			sc_pinmap[1];
53*252766dfSjmcneill 
54*252766dfSjmcneill 	int			sc_state;
55*252766dfSjmcneill 
56*252766dfSjmcneill 	struct sysctllog	*sc_sysctllog;
57*252766dfSjmcneill 	int			sc_sysctlnode;
58*252766dfSjmcneill };
59*252766dfSjmcneill 
60*252766dfSjmcneill static void	gpiorfkill_enable(struct gpiorfkill_softc *, int);
61*252766dfSjmcneill static void	gpiorfkill_sysctl_init(struct gpiorfkill_softc *);
62*252766dfSjmcneill static int	gpiorfkill_enable_helper(SYSCTLFN_PROTO);
63*252766dfSjmcneill 
64*252766dfSjmcneill CFATTACH_DECL_NEW(gpiorfkill, sizeof(struct gpiorfkill_softc),
65*252766dfSjmcneill 	gpiorfkill_match, gpiorfkill_attach, NULL, NULL);
66*252766dfSjmcneill 
67*252766dfSjmcneill static int
gpiorfkill_match(device_t parent,cfdata_t cf,void * aux)68*252766dfSjmcneill gpiorfkill_match(device_t parent, cfdata_t cf, void *aux)
69*252766dfSjmcneill {
70*252766dfSjmcneill 	struct gpio_attach_args * const ga = aux;
71*252766dfSjmcneill 
72*252766dfSjmcneill 	if (strcmp(ga->ga_dvname, cf->cf_name) != 0)
73*252766dfSjmcneill 		return 0;
74*252766dfSjmcneill 
75*252766dfSjmcneill 	if (ga->ga_offset == -1 || gpio_npins(ga->ga_mask) != 1)
76*252766dfSjmcneill 		return 0;
77*252766dfSjmcneill 
78*252766dfSjmcneill 	return 1;
79*252766dfSjmcneill }
80*252766dfSjmcneill 
81*252766dfSjmcneill static void
gpiorfkill_attach(device_t parent,device_t self,void * aux)82*252766dfSjmcneill gpiorfkill_attach(device_t parent, device_t self, void *aux)
83*252766dfSjmcneill {
84*252766dfSjmcneill 	struct gpiorfkill_softc * const sc = device_private(self);
85*252766dfSjmcneill 	struct gpio_attach_args * const ga = aux;
86*252766dfSjmcneill 	int caps;
87*252766dfSjmcneill 
88*252766dfSjmcneill 	sc->sc_dev = self;
89*252766dfSjmcneill 	sc->sc_gpio = ga->ga_gpio;
90*252766dfSjmcneill 	sc->sc_map.pm_map = sc->sc_pinmap;
91*252766dfSjmcneill 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
92*252766dfSjmcneill 	    &sc->sc_map)) {
93*252766dfSjmcneill 		aprint_error(": couldn't map pins\n");
94*252766dfSjmcneill 		return;
95*252766dfSjmcneill 	}
96*252766dfSjmcneill 
97*252766dfSjmcneill 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, 0);
98*252766dfSjmcneill 	if ((caps & GPIO_PIN_OUTPUT) == 0) {
99*252766dfSjmcneill 		aprint_error(": pin is not an output pin\n");
100*252766dfSjmcneill 		return;
101*252766dfSjmcneill 	}
102*252766dfSjmcneill 
103*252766dfSjmcneill 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_OUTPUT);
104*252766dfSjmcneill 
105*252766dfSjmcneill 	aprint_naive("\n");
106*252766dfSjmcneill 	aprint_normal("\n");
107*252766dfSjmcneill 
108*252766dfSjmcneill 	gpiorfkill_enable(sc, 1);
109*252766dfSjmcneill 	gpiorfkill_sysctl_init(sc);
110*252766dfSjmcneill }
111*252766dfSjmcneill 
112*252766dfSjmcneill static void
gpiorfkill_enable(struct gpiorfkill_softc * sc,int enable)113*252766dfSjmcneill gpiorfkill_enable(struct gpiorfkill_softc *sc, int enable)
114*252766dfSjmcneill {
115*252766dfSjmcneill 	sc->sc_state = enable;
116*252766dfSjmcneill 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, sc->sc_state);
117*252766dfSjmcneill }
118*252766dfSjmcneill 
119*252766dfSjmcneill static void
gpiorfkill_sysctl_init(struct gpiorfkill_softc * sc)120*252766dfSjmcneill gpiorfkill_sysctl_init(struct gpiorfkill_softc *sc)
121*252766dfSjmcneill {
122*252766dfSjmcneill 	const struct sysctlnode *node, *devnode;
123*252766dfSjmcneill 	int error;
124*252766dfSjmcneill 
125*252766dfSjmcneill 	error = sysctl_createv(&sc->sc_sysctllog, 0, NULL, &devnode,
126*252766dfSjmcneill 	    0, CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
127*252766dfSjmcneill 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
128*252766dfSjmcneill 	if (error)
129*252766dfSjmcneill 		goto sysctl_failed;
130*252766dfSjmcneill 
131*252766dfSjmcneill 	error = sysctl_createv(&sc->sc_sysctllog, 0, &devnode, &node,
132*252766dfSjmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "enable", NULL,
133*252766dfSjmcneill 	    gpiorfkill_enable_helper, 0, (void *)sc, 0,
134*252766dfSjmcneill 	    CTL_CREATE, CTL_EOL);
135*252766dfSjmcneill 	if (error)
136*252766dfSjmcneill 		goto sysctl_failed;
137*252766dfSjmcneill 	sc->sc_sysctlnode = node->sysctl_num;
138*252766dfSjmcneill 
139*252766dfSjmcneill 	return;
140*252766dfSjmcneill 
141*252766dfSjmcneill sysctl_failed:
142*252766dfSjmcneill 	aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes (%d)\n",
143*252766dfSjmcneill 	    error);
144*252766dfSjmcneill 	sysctl_teardown(&sc->sc_sysctllog);
145*252766dfSjmcneill }
146*252766dfSjmcneill 
147*252766dfSjmcneill static int
gpiorfkill_enable_helper(SYSCTLFN_ARGS)148*252766dfSjmcneill gpiorfkill_enable_helper(SYSCTLFN_ARGS)
149*252766dfSjmcneill {
150*252766dfSjmcneill 	struct gpiorfkill_softc *sc;
151*252766dfSjmcneill 	struct sysctlnode node;
152*252766dfSjmcneill 	int error, enable;
153*252766dfSjmcneill 
154*252766dfSjmcneill 	node = *rnode;
155*252766dfSjmcneill 	sc = node.sysctl_data;
156*252766dfSjmcneill 	enable = sc->sc_state;
157*252766dfSjmcneill 	node.sysctl_data = &enable;
158*252766dfSjmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
159*252766dfSjmcneill 	if (error || newp == NULL)
160*252766dfSjmcneill 		return error;
161*252766dfSjmcneill 
162*252766dfSjmcneill 	enable = !!enable;
163*252766dfSjmcneill 	gpiorfkill_enable(sc, enable);
164*252766dfSjmcneill 
165*252766dfSjmcneill 	return 0;
166*252766dfSjmcneill }
167