bcm2835_pwm.c revision 1.3 1 1.3 skrll /* $NetBSD: bcm2835_pwm.c,v 1.3 2017/12/10 21:38:26 skrll Exp $ */
2 1.1 mlelstv
3 1.1 mlelstv /*-
4 1.1 mlelstv * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 mlelstv * All rights reserved.
6 1.1 mlelstv *
7 1.1 mlelstv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 mlelstv * by Michael van Elst
9 1.1 mlelstv *
10 1.1 mlelstv * Redistribution and use in source and binary forms, with or without
11 1.1 mlelstv * modification, are permitted provided that the following conditions
12 1.1 mlelstv * are met:
13 1.1 mlelstv * 1. Redistributions of source code must retain the above copyright
14 1.1 mlelstv * notice, this list of conditions and the following disclaimer.
15 1.1 mlelstv * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mlelstv * notice, this list of conditions and the following disclaimer in the
17 1.1 mlelstv * documentation and/or other materials provided with the distribution.
18 1.1 mlelstv *
19 1.1 mlelstv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 mlelstv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 mlelstv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 mlelstv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 mlelstv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 mlelstv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 mlelstv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 mlelstv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 mlelstv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 mlelstv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 mlelstv * POSSIBILITY OF SUCH DAMAGE.
30 1.1 mlelstv */
31 1.1 mlelstv
32 1.1 mlelstv /*
33 1.1 mlelstv * Driver for BCM2835 Pulse Width Modulator
34 1.1 mlelstv *
35 1.1 mlelstv * Each channel can be allocated and used individually, but
36 1.1 mlelstv * for FIFO usage, both channels must to be requested.
37 1.1 mlelstv */
38 1.1 mlelstv
39 1.1 mlelstv #include <sys/cdefs.h>
40 1.3 skrll __KERNEL_RCSID(0, "$NetBSD: bcm2835_pwm.c,v 1.3 2017/12/10 21:38:26 skrll Exp $");
41 1.1 mlelstv
42 1.1 mlelstv #include "bcmdmac.h"
43 1.1 mlelstv
44 1.1 mlelstv #include <sys/param.h>
45 1.1 mlelstv #include <sys/systm.h>
46 1.1 mlelstv #include <sys/device.h>
47 1.1 mlelstv #include <sys/kernel.h>
48 1.1 mlelstv #include <sys/bus.h>
49 1.1 mlelstv #include <sys/atomic.h>
50 1.1 mlelstv #include <sys/intr.h>
51 1.1 mlelstv
52 1.1 mlelstv #include <arm/broadcom/bcm2835reg.h>
53 1.1 mlelstv
54 1.1 mlelstv #include <arm/broadcom/bcm2835_pwm.h>
55 1.1 mlelstv
56 1.3 skrll #include <dev/fdt/fdtvar.h>
57 1.3 skrll
58 1.1 mlelstv struct bcm_pwm_channel {
59 1.1 mlelstv struct bcm2835pwm_softc *sc;
60 1.1 mlelstv uint32_t ctlmask, stamask, gapomask;
61 1.1 mlelstv int rng, dat;
62 1.1 mlelstv bool inuse;
63 1.1 mlelstv uint32_t ctlsave, rngsave, datsave;
64 1.1 mlelstv };
65 1.1 mlelstv
66 1.1 mlelstv struct bcm2835pwm_softc {
67 1.1 mlelstv device_t sc_dev;
68 1.1 mlelstv
69 1.1 mlelstv bus_space_tag_t sc_iot;
70 1.1 mlelstv bus_space_handle_t sc_ioh;
71 1.2 skrll bus_addr_t sc_iob;
72 1.1 mlelstv
73 1.3 skrll struct clk *sc_clk;
74 1.1 mlelstv int sc_clockrate;
75 1.1 mlelstv struct bcm_pwm_channel sc_channels[2];
76 1.1 mlelstv kmutex_t sc_lock;
77 1.1 mlelstv };
78 1.1 mlelstv
79 1.1 mlelstv #define PWM_WRITE(sc, reg, val) \
80 1.1 mlelstv bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
81 1.1 mlelstv #define PWM_READ(sc, reg) \
82 1.1 mlelstv bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
83 1.1 mlelstv
84 1.1 mlelstv static int bcmpwm_match(device_t, cfdata_t, void *);
85 1.1 mlelstv static void bcmpwm_attach(device_t, device_t, void *);
86 1.1 mlelstv static int bcmpwm_wait(struct bcm2835pwm_softc *);
87 1.1 mlelstv
88 1.3 skrll CFATTACH_DECL_NEW(bcmpwm, sizeof(struct bcm2835pwm_softc),
89 1.1 mlelstv bcmpwm_match, bcmpwm_attach, NULL, NULL);
90 1.1 mlelstv
91 1.1 mlelstv /* ARGSUSED */
92 1.1 mlelstv static int
93 1.1 mlelstv bcmpwm_match(device_t parent, cfdata_t match, void *aux)
94 1.1 mlelstv {
95 1.3 skrll const char * const compatible[] = { "brcm,bcm2835-pwm", NULL };
96 1.3 skrll struct fdt_attach_args * const faa = aux;
97 1.1 mlelstv
98 1.3 skrll return of_match_compatible(faa->faa_phandle, compatible);
99 1.1 mlelstv }
100 1.1 mlelstv
101 1.1 mlelstv static void
102 1.1 mlelstv bcmpwm_attach(device_t parent, device_t self, void *aux)
103 1.1 mlelstv {
104 1.1 mlelstv struct bcm2835pwm_softc *sc = device_private(self);
105 1.3 skrll struct fdt_attach_args * const faa = aux;
106 1.3 skrll const int phandle = faa->faa_phandle;
107 1.3 skrll bus_size_t size;
108 1.1 mlelstv
109 1.3 skrll sc->sc_dev = self;
110 1.3 skrll sc->sc_iot = faa->faa_bst;
111 1.1 mlelstv
112 1.3 skrll int error = fdtbus_get_reg(phandle, 0, &sc->sc_iob, &size);
113 1.3 skrll if (error) {
114 1.3 skrll aprint_error(": failed to get registers\n");
115 1.3 skrll return;
116 1.3 skrll }
117 1.1 mlelstv
118 1.3 skrll if (bus_space_map(sc->sc_iot, sc->sc_iob, size, 0,
119 1.1 mlelstv &sc->sc_ioh)) {
120 1.1 mlelstv aprint_error_dev(sc->sc_dev, "unable to map device\n");
121 1.3 skrll return;
122 1.1 mlelstv }
123 1.1 mlelstv
124 1.3 skrll sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
125 1.3 skrll if (sc->sc_clk == NULL) {
126 1.3 skrll aprint_error(": couldn't get clk\n");
127 1.3 skrll return;
128 1.3 skrll }
129 1.3 skrll
130 1.3 skrll error = clk_enable(sc->sc_clk);
131 1.3 skrll if (error != 0) {
132 1.3 skrll aprint_error(": couldn't enable clk\n");
133 1.3 skrll return;
134 1.3 skrll }
135 1.3 skrll
136 1.3 skrll aprint_naive("\n");
137 1.3 skrll aprint_normal(": Pulse Width Modulator\n");
138 1.3 skrll
139 1.3 skrll sc->sc_clockrate = clk_get_rate(sc->sc_clk);
140 1.1 mlelstv
141 1.1 mlelstv sc->sc_channels[0].sc = sc;
142 1.1 mlelstv sc->sc_channels[0].ctlmask = PWM_CTL_MSEN1 | PWM_CTL_USEF1 |
143 1.1 mlelstv PWM_CTL_POLA1 | PWM_CTL_SBIT1 |
144 1.1 mlelstv PWM_CTL_RPTL1 | PWM_CTL_MODE1 |
145 1.1 mlelstv PWM_CTL_PWEN1;
146 1.1 mlelstv sc->sc_channels[0].stamask = PWM_STA_STA1;
147 1.1 mlelstv sc->sc_channels[0].gapomask = PWM_STA_GAPO1;
148 1.1 mlelstv sc->sc_channels[0].rng = PWM_RNG1;
149 1.1 mlelstv sc->sc_channels[0].dat = PWM_DAT1;
150 1.1 mlelstv
151 1.1 mlelstv sc->sc_channels[1].sc = sc;
152 1.1 mlelstv sc->sc_channels[1].ctlmask = PWM_CTL_MSEN2 | PWM_CTL_USEF2 |
153 1.1 mlelstv PWM_CTL_POLA2 | PWM_CTL_SBIT2 |
154 1.1 mlelstv PWM_CTL_RPTL2 | PWM_CTL_MODE2 |
155 1.1 mlelstv PWM_CTL_PWEN2;
156 1.1 mlelstv sc->sc_channels[1].stamask = PWM_STA_STA2;
157 1.1 mlelstv sc->sc_channels[1].gapomask = PWM_STA_GAPO2;
158 1.1 mlelstv sc->sc_channels[1].rng = PWM_RNG2;
159 1.1 mlelstv sc->sc_channels[1].dat = PWM_DAT2;
160 1.1 mlelstv
161 1.1 mlelstv /* The PWM hardware can be used by vcaudio if the
162 1.1 mlelstv * analog output is selected
163 1.1 mlelstv */
164 1.1 mlelstv sc->sc_channels[0].inuse = false;
165 1.1 mlelstv sc->sc_channels[1].inuse = false;
166 1.1 mlelstv
167 1.1 mlelstv mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
168 1.1 mlelstv
169 1.1 mlelstv /* Success! */
170 1.1 mlelstv
171 1.3 skrll return;
172 1.1 mlelstv }
173 1.1 mlelstv
174 1.1 mlelstv struct bcm_pwm_channel *
175 1.1 mlelstv bcm_pwm_alloc(int num)
176 1.1 mlelstv {
177 1.1 mlelstv struct bcm2835pwm_softc *sc;
178 1.1 mlelstv device_t dev;
179 1.1 mlelstv struct bcm_pwm_channel *pwm;
180 1.1 mlelstv
181 1.1 mlelstv dev = device_find_by_driver_unit("bcmpwm", 0);
182 1.1 mlelstv if (dev == NULL)
183 1.1 mlelstv return NULL;
184 1.1 mlelstv sc = device_private(dev);
185 1.1 mlelstv
186 1.1 mlelstv if (num < 0 || num >= __arraycount(sc->sc_channels))
187 1.1 mlelstv return NULL;
188 1.1 mlelstv
189 1.1 mlelstv pwm = &sc->sc_channels[num];
190 1.1 mlelstv
191 1.1 mlelstv mutex_enter(&sc->sc_lock);
192 1.1 mlelstv if (pwm->inuse)
193 1.1 mlelstv pwm = NULL;
194 1.1 mlelstv else
195 1.1 mlelstv pwm->inuse = true;
196 1.1 mlelstv mutex_exit(&sc->sc_lock);
197 1.1 mlelstv
198 1.1 mlelstv if (pwm) {
199 1.1 mlelstv pwm->datsave = PWM_READ(pwm->sc, pwm->dat);
200 1.1 mlelstv pwm->ctlsave = PWM_READ(pwm->sc, PWM_CTL);
201 1.1 mlelstv pwm->rngsave = PWM_READ(pwm->sc, pwm->rng);
202 1.1 mlelstv }
203 1.1 mlelstv
204 1.1 mlelstv return pwm;
205 1.1 mlelstv }
206 1.1 mlelstv
207 1.1 mlelstv void
208 1.1 mlelstv bcm_pwm_free(struct bcm_pwm_channel *pwm)
209 1.1 mlelstv {
210 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
211 1.1 mlelstv
212 1.1 mlelstv KASSERT(pwm->inuse);
213 1.1 mlelstv
214 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->rng, pwm->rngsave);
215 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_CTL, pwm->ctlsave & ~PWM_CTL_WRITEZERO);
216 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->dat, pwm->datsave);
217 1.1 mlelstv
218 1.1 mlelstv mutex_enter(&sc->sc_lock);
219 1.1 mlelstv pwm->inuse = false;
220 1.1 mlelstv mutex_exit(&sc->sc_lock);
221 1.1 mlelstv }
222 1.1 mlelstv
223 1.1 mlelstv void
224 1.1 mlelstv bcm_pwm_control(struct bcm_pwm_channel *pwm, uint32_t ctl, uint32_t rng)
225 1.1 mlelstv {
226 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
227 1.1 mlelstv uint32_t w;
228 1.1 mlelstv
229 1.1 mlelstv KASSERT(pwm->inuse);
230 1.1 mlelstv
231 1.1 mlelstv /* set control bits like for channel 0
232 1.1 mlelstv * there are generic bit definitions that the caller can use.
233 1.1 mlelstv */
234 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_CTL);
235 1.1 mlelstv ctl = (w & ~pwm->ctlmask) | __SHIFTIN(ctl, pwm->ctlmask);
236 1.1 mlelstv
237 1.1 mlelstv /* when FIFO usage gets enabled but wasn't clear the FIFO */
238 1.1 mlelstv if ((w & (PWM_CTL_USEF1|PWM_CTL_USEF2)) == 0 &&
239 1.1 mlelstv (ctl & (PWM_CTL_USEF1|PWM_CTL_USEF2)) != 0)
240 1.1 mlelstv ctl |= PWM_CTL_CLRF1;
241 1.1 mlelstv
242 1.1 mlelstv PWM_WRITE(sc, pwm->rng, rng);
243 1.1 mlelstv PWM_WRITE(sc, PWM_CTL, ctl & ~PWM_CTL_WRITEZERO);
244 1.1 mlelstv }
245 1.1 mlelstv
246 1.1 mlelstv uint32_t
247 1.1 mlelstv bcm_pwm_status(struct bcm_pwm_channel *pwm)
248 1.1 mlelstv {
249 1.1 mlelstv uint32_t w;
250 1.1 mlelstv uint32_t common = PWM_STA_BERR | PWM_STA_RERR1 |
251 1.1 mlelstv PWM_STA_WERR1 | PWM_STA_EMPT1 | PWM_STA_FULL1;
252 1.1 mlelstv
253 1.1 mlelstv /* return status bits like for channel 0
254 1.1 mlelstv * there are generic bit definitions that the caller can use.
255 1.1 mlelstv *
256 1.1 mlelstv * The BERR bit is returned for both channels.
257 1.1 mlelstv */
258 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_STA);
259 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_STA, w &
260 1.1 mlelstv (pwm->stamask | pwm->gapomask | common));
261 1.1 mlelstv
262 1.1 mlelstv w = __SHIFTIN(__SHIFTOUT(w, pwm->stamask), PWM_STA_STA)
263 1.1 mlelstv | __SHIFTIN(__SHIFTOUT(w, pwm->gapomask), PWM_STA_GAPO)
264 1.1 mlelstv | (w & common);
265 1.1 mlelstv
266 1.1 mlelstv return w;
267 1.1 mlelstv }
268 1.1 mlelstv
269 1.1 mlelstv static int
270 1.1 mlelstv bcmpwm_wait(struct bcm2835pwm_softc *sc)
271 1.1 mlelstv {
272 1.1 mlelstv int i;
273 1.1 mlelstv uint32_t s;
274 1.1 mlelstv
275 1.1 mlelstv for (i=0; i<1000; ++i) {
276 1.1 mlelstv s = PWM_READ(sc, PWM_STA);
277 1.1 mlelstv if ((s & PWM_STA_FULL1) == 0)
278 1.1 mlelstv break;
279 1.1 mlelstv delay(1);
280 1.1 mlelstv }
281 1.1 mlelstv if (i >= 1000)
282 1.1 mlelstv return -1;
283 1.1 mlelstv
284 1.1 mlelstv return 0;
285 1.1 mlelstv }
286 1.1 mlelstv
287 1.1 mlelstv int
288 1.1 mlelstv bcm_pwm_write(struct bcm_pwm_channel *pwm, uint32_t *data1, uint32_t *data2,
289 1.1 mlelstv int len)
290 1.1 mlelstv {
291 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
292 1.1 mlelstv int n;
293 1.1 mlelstv uint32_t r;
294 1.1 mlelstv bool even = false;
295 1.1 mlelstv
296 1.1 mlelstv KASSERT(pwm->inuse);
297 1.1 mlelstv
298 1.1 mlelstv n = len;
299 1.1 mlelstv while (n > 0) {
300 1.1 mlelstv if (bcmpwm_wait(sc))
301 1.1 mlelstv break;
302 1.1 mlelstv r = even ? *data2++ : *data1++;
303 1.1 mlelstv PWM_WRITE(sc, PWM_FIFO, r);
304 1.1 mlelstv if (data2 != NULL)
305 1.1 mlelstv even = !even;
306 1.1 mlelstv --n;
307 1.1 mlelstv }
308 1.1 mlelstv
309 1.1 mlelstv return len - n;
310 1.1 mlelstv }
311 1.1 mlelstv
312 1.1 mlelstv void
313 1.1 mlelstv bcm_pwm_set(struct bcm_pwm_channel *pwm, uint32_t w)
314 1.1 mlelstv {
315 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
316 1.1 mlelstv
317 1.1 mlelstv PWM_WRITE(sc, pwm->dat, w);
318 1.1 mlelstv }
319 1.1 mlelstv
320 1.1 mlelstv int
321 1.1 mlelstv bcm_pwm_flush(struct bcm_pwm_channel *pwm)
322 1.1 mlelstv {
323 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
324 1.1 mlelstv
325 1.1 mlelstv return bcmpwm_wait(sc) ? EIO : 0;
326 1.1 mlelstv }
327 1.1 mlelstv
328 1.1 mlelstv void
329 1.1 mlelstv bcm_pwm_dma_enable(struct bcm_pwm_channel *pwm, bool enable)
330 1.1 mlelstv {
331 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
332 1.1 mlelstv uint32_t w;
333 1.1 mlelstv
334 1.1 mlelstv #if 0
335 1.1 mlelstv w = PWM_READ(sc, PWM_DMAC);
336 1.1 mlelstv if (enable)
337 1.1 mlelstv w |= PWM_DMAC_ENAB;
338 1.1 mlelstv else
339 1.1 mlelstv w &= ~PWM_DMAC_ENAB;
340 1.1 mlelstv #else
341 1.1 mlelstv w = (enable ? PWM_DMAC_ENAB : 0)
342 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_PANIC)
343 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_DREQ);
344 1.1 mlelstv #endif
345 1.1 mlelstv PWM_WRITE(sc, PWM_DMAC, w & ~PWM_DMAC_WRITEZERO);
346 1.1 mlelstv }
347 1.1 mlelstv
348 1.1 mlelstv uint32_t
349 1.1 mlelstv bcm_pwm_dma_address(struct bcm_pwm_channel *pwm)
350 1.1 mlelstv {
351 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
352 1.1 mlelstv
353 1.2 skrll return sc->sc_iob + PWM_FIFO;
354 1.1 mlelstv }
355 1.1 mlelstv
356