gpiopwm.c revision 1.5 1 1.5 msaitoh /* $NetBSD: gpiopwm.c,v 1.5 2016/07/14 04:00:45 msaitoh 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.1 mbalmer #define GPIOPWM_NPINS 1
40 1.1 mbalmer
41 1.1 mbalmer struct gpiopwm_softc {
42 1.1 mbalmer device_t sc_dev;
43 1.1 mbalmer void *sc_gpio;
44 1.1 mbalmer struct gpio_pinmap sc_map;
45 1.1 mbalmer int _map[GPIOPWM_NPINS];
46 1.1 mbalmer
47 1.1 mbalmer callout_t sc_pulse;
48 1.1 mbalmer int sc_ticks_on;
49 1.1 mbalmer int sc_ticks_off;
50 1.1 mbalmer
51 1.1 mbalmer struct sysctllog *sc_log;
52 1.1 mbalmer int sc_dying;
53 1.1 mbalmer };
54 1.1 mbalmer
55 1.1 mbalmer int gpiopwm_match(device_t, cfdata_t, void *);
56 1.1 mbalmer void gpiopwm_attach(device_t, device_t, void *);
57 1.1 mbalmer int gpiopwm_detach(device_t, int);
58 1.1 mbalmer int gpiopwm_activate(device_t, enum devact);
59 1.1 mbalmer static int gpiopwm_set_on(SYSCTLFN_ARGS);
60 1.1 mbalmer static int gpiopwm_set_off(SYSCTLFN_ARGS);
61 1.1 mbalmer static void gpiopwm_pulse(void *);
62 1.1 mbalmer
63 1.1 mbalmer CFATTACH_DECL_NEW(gpiopwm, sizeof(struct gpiopwm_softc),
64 1.1 mbalmer gpiopwm_match, gpiopwm_attach, gpiopwm_detach, gpiopwm_activate);
65 1.1 mbalmer
66 1.1 mbalmer extern struct cfdriver gpiopwm_cd;
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.1 mbalmer pmf_device_register(self, NULL, NULL);
108 1.1 mbalmer
109 1.1 mbalmer callout_init(&sc->sc_pulse, CALLOUT_MPSAFE);
110 1.1 mbalmer callout_setfunc(&sc->sc_pulse, gpiopwm_pulse, sc);
111 1.1 mbalmer
112 1.5 msaitoh sysctl_createv(&sc->sc_log, 0, NULL, &node,
113 1.5 msaitoh 0,
114 1.5 msaitoh CTLTYPE_NODE, device_xname(sc->sc_dev),
115 1.5 msaitoh SYSCTL_DESCR("GPIO software PWM"),
116 1.5 msaitoh NULL, 0, NULL, 0,
117 1.5 msaitoh CTL_HW, CTL_CREATE, CTL_EOL);
118 1.5 msaitoh
119 1.5 msaitoh if (node == NULL) {
120 1.5 msaitoh aprint_error(": can't create sysctl node\n");
121 1.5 msaitoh return;
122 1.5 msaitoh }
123 1.5 msaitoh
124 1.5 msaitoh sysctl_createv(&sc->sc_log, 0, &node, NULL,
125 1.5 msaitoh CTLFLAG_READWRITE,
126 1.5 msaitoh CTLTYPE_INT, "on",
127 1.5 msaitoh SYSCTL_DESCR("PWM 'on' period in ticks"),
128 1.5 msaitoh gpiopwm_set_on, 0, (void *)sc, 0,
129 1.1 mbalmer CTL_CREATE, CTL_EOL);
130 1.5 msaitoh sysctl_createv(&sc->sc_log, 0, &node, NULL,
131 1.5 msaitoh CTLFLAG_READWRITE,
132 1.5 msaitoh CTLTYPE_INT, "off",
133 1.5 msaitoh SYSCTL_DESCR("PWM 'off' period in ticks"),
134 1.5 msaitoh gpiopwm_set_off, 0, (void *)sc, 0,
135 1.1 mbalmer CTL_CREATE, CTL_EOL);
136 1.1 mbalmer
137 1.1 mbalmer aprint_normal("\n");
138 1.1 mbalmer return;
139 1.1 mbalmer }
140 1.1 mbalmer
141 1.1 mbalmer int
142 1.1 mbalmer gpiopwm_detach(device_t self, int flags)
143 1.1 mbalmer {
144 1.1 mbalmer struct gpiopwm_softc *sc = device_private(self);
145 1.1 mbalmer
146 1.1 mbalmer callout_halt(&sc->sc_pulse, NULL);
147 1.1 mbalmer callout_destroy(&sc->sc_pulse);
148 1.2 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW);
149 1.1 mbalmer
150 1.1 mbalmer pmf_device_deregister(self);
151 1.1 mbalmer gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
152 1.1 mbalmer
153 1.1 mbalmer if (sc->sc_log != NULL) {
154 1.1 mbalmer sysctl_teardown(&sc->sc_log);
155 1.1 mbalmer sc->sc_log = NULL;
156 1.1 mbalmer }
157 1.1 mbalmer return 0;
158 1.1 mbalmer }
159 1.1 mbalmer
160 1.1 mbalmer static int
161 1.1 mbalmer gpiopwm_set_on(SYSCTLFN_ARGS)
162 1.1 mbalmer {
163 1.1 mbalmer struct sysctlnode node;
164 1.1 mbalmer struct gpiopwm_softc *sc;
165 1.1 mbalmer int val, error;
166 1.1 mbalmer
167 1.1 mbalmer node = *rnode;
168 1.1 mbalmer sc = node.sysctl_data;
169 1.1 mbalmer
170 1.1 mbalmer callout_halt(&sc->sc_pulse, NULL);
171 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW);
172 1.1 mbalmer node.sysctl_data = &val;
173 1.1 mbalmer
174 1.1 mbalmer val = sc->sc_ticks_on;
175 1.1 mbalmer error = sysctl_lookup(SYSCTLFN_CALL(&node));
176 1.1 mbalmer if (error || newp == NULL)
177 1.1 mbalmer return error;
178 1.1 mbalmer
179 1.1 mbalmer sc->sc_ticks_on = val;
180 1.1 mbalmer if (sc->sc_ticks_on > 0 && sc->sc_ticks_off > 0) {
181 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH);
182 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_on);
183 1.1 mbalmer }
184 1.1 mbalmer return 0;
185 1.1 mbalmer }
186 1.1 mbalmer
187 1.1 mbalmer static int
188 1.1 mbalmer gpiopwm_set_off(SYSCTLFN_ARGS)
189 1.1 mbalmer {
190 1.1 mbalmer struct sysctlnode node;
191 1.1 mbalmer struct gpiopwm_softc *sc;
192 1.1 mbalmer int val, error;
193 1.1 mbalmer
194 1.1 mbalmer node = *rnode;
195 1.1 mbalmer sc = node.sysctl_data;
196 1.1 mbalmer
197 1.1 mbalmer callout_halt(&sc->sc_pulse, NULL);
198 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW);
199 1.1 mbalmer node.sysctl_data = &val;
200 1.1 mbalmer
201 1.1 mbalmer val = sc->sc_ticks_off;
202 1.1 mbalmer error = sysctl_lookup(SYSCTLFN_CALL(&node));
203 1.1 mbalmer if (error || newp == NULL)
204 1.1 mbalmer return error;
205 1.1 mbalmer
206 1.1 mbalmer sc->sc_ticks_off = val;
207 1.1 mbalmer if (sc->sc_ticks_on > 0 && sc->sc_ticks_off > 0) {
208 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH);
209 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_on);
210 1.1 mbalmer }
211 1.1 mbalmer return 0;
212 1.1 mbalmer }
213 1.1 mbalmer
214 1.1 mbalmer static void
215 1.1 mbalmer gpiopwm_pulse(void *arg)
216 1.1 mbalmer {
217 1.1 mbalmer struct gpiopwm_softc *sc;
218 1.1 mbalmer
219 1.1 mbalmer sc = arg;
220 1.1 mbalmer if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0) == GPIO_PIN_HIGH) {
221 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW);
222 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_off);
223 1.1 mbalmer } else {
224 1.1 mbalmer gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH);
225 1.1 mbalmer callout_schedule(&sc->sc_pulse, sc->sc_ticks_on);
226 1.1 mbalmer }
227 1.1 mbalmer }
228 1.1 mbalmer
229 1.1 mbalmer int
230 1.1 mbalmer gpiopwm_activate(device_t self, enum devact act)
231 1.1 mbalmer {
232 1.1 mbalmer struct gpiopwm_softc *sc = device_private(self);
233 1.1 mbalmer
234 1.1 mbalmer switch (act) {
235 1.1 mbalmer case DVACT_DEACTIVATE:
236 1.1 mbalmer sc->sc_dying = 1;
237 1.1 mbalmer return 0;
238 1.1 mbalmer default:
239 1.1 mbalmer return EOPNOTSUPP;
240 1.1 mbalmer }
241 1.1 mbalmer }
242