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