1 1.4 thorpej /* $NetBSD: bcm2835_pwm.c,v 1.4 2021/01/27 03:10:19 thorpej 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.4 thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_pwm.c,v 1.4 2021/01/27 03:10:19 thorpej 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.4 thorpej static const struct device_compatible_entry compat_data[] = { 92 1.4 thorpej { .compat = "brcm,bcm2835-pwm" }, 93 1.4 thorpej DEVICE_COMPAT_EOL 94 1.4 thorpej }; 95 1.4 thorpej 96 1.1 mlelstv /* ARGSUSED */ 97 1.1 mlelstv static int 98 1.1 mlelstv bcmpwm_match(device_t parent, cfdata_t match, void *aux) 99 1.1 mlelstv { 100 1.3 skrll struct fdt_attach_args * const faa = aux; 101 1.1 mlelstv 102 1.4 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 103 1.1 mlelstv } 104 1.1 mlelstv 105 1.1 mlelstv static void 106 1.1 mlelstv bcmpwm_attach(device_t parent, device_t self, void *aux) 107 1.1 mlelstv { 108 1.1 mlelstv struct bcm2835pwm_softc *sc = device_private(self); 109 1.3 skrll struct fdt_attach_args * const faa = aux; 110 1.3 skrll const int phandle = faa->faa_phandle; 111 1.3 skrll bus_size_t size; 112 1.1 mlelstv 113 1.3 skrll sc->sc_dev = self; 114 1.3 skrll sc->sc_iot = faa->faa_bst; 115 1.1 mlelstv 116 1.3 skrll int error = fdtbus_get_reg(phandle, 0, &sc->sc_iob, &size); 117 1.3 skrll if (error) { 118 1.3 skrll aprint_error(": failed to get registers\n"); 119 1.3 skrll return; 120 1.3 skrll } 121 1.1 mlelstv 122 1.3 skrll if (bus_space_map(sc->sc_iot, sc->sc_iob, size, 0, 123 1.1 mlelstv &sc->sc_ioh)) { 124 1.1 mlelstv aprint_error_dev(sc->sc_dev, "unable to map device\n"); 125 1.3 skrll return; 126 1.1 mlelstv } 127 1.1 mlelstv 128 1.3 skrll sc->sc_clk = fdtbus_clock_get_index(phandle, 0); 129 1.3 skrll if (sc->sc_clk == NULL) { 130 1.3 skrll aprint_error(": couldn't get clk\n"); 131 1.3 skrll return; 132 1.3 skrll } 133 1.3 skrll 134 1.3 skrll error = clk_enable(sc->sc_clk); 135 1.3 skrll if (error != 0) { 136 1.3 skrll aprint_error(": couldn't enable clk\n"); 137 1.3 skrll return; 138 1.3 skrll } 139 1.3 skrll 140 1.3 skrll aprint_naive("\n"); 141 1.3 skrll aprint_normal(": Pulse Width Modulator\n"); 142 1.3 skrll 143 1.3 skrll sc->sc_clockrate = clk_get_rate(sc->sc_clk); 144 1.1 mlelstv 145 1.1 mlelstv sc->sc_channels[0].sc = sc; 146 1.1 mlelstv sc->sc_channels[0].ctlmask = PWM_CTL_MSEN1 | PWM_CTL_USEF1 | 147 1.1 mlelstv PWM_CTL_POLA1 | PWM_CTL_SBIT1 | 148 1.1 mlelstv PWM_CTL_RPTL1 | PWM_CTL_MODE1 | 149 1.1 mlelstv PWM_CTL_PWEN1; 150 1.1 mlelstv sc->sc_channels[0].stamask = PWM_STA_STA1; 151 1.1 mlelstv sc->sc_channels[0].gapomask = PWM_STA_GAPO1; 152 1.1 mlelstv sc->sc_channels[0].rng = PWM_RNG1; 153 1.1 mlelstv sc->sc_channels[0].dat = PWM_DAT1; 154 1.1 mlelstv 155 1.1 mlelstv sc->sc_channels[1].sc = sc; 156 1.1 mlelstv sc->sc_channels[1].ctlmask = PWM_CTL_MSEN2 | PWM_CTL_USEF2 | 157 1.1 mlelstv PWM_CTL_POLA2 | PWM_CTL_SBIT2 | 158 1.1 mlelstv PWM_CTL_RPTL2 | PWM_CTL_MODE2 | 159 1.1 mlelstv PWM_CTL_PWEN2; 160 1.1 mlelstv sc->sc_channels[1].stamask = PWM_STA_STA2; 161 1.1 mlelstv sc->sc_channels[1].gapomask = PWM_STA_GAPO2; 162 1.1 mlelstv sc->sc_channels[1].rng = PWM_RNG2; 163 1.1 mlelstv sc->sc_channels[1].dat = PWM_DAT2; 164 1.1 mlelstv 165 1.1 mlelstv /* The PWM hardware can be used by vcaudio if the 166 1.1 mlelstv * analog output is selected 167 1.1 mlelstv */ 168 1.1 mlelstv sc->sc_channels[0].inuse = false; 169 1.1 mlelstv sc->sc_channels[1].inuse = false; 170 1.1 mlelstv 171 1.1 mlelstv mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 172 1.1 mlelstv 173 1.1 mlelstv /* Success! */ 174 1.1 mlelstv 175 1.3 skrll return; 176 1.1 mlelstv } 177 1.1 mlelstv 178 1.1 mlelstv struct bcm_pwm_channel * 179 1.1 mlelstv bcm_pwm_alloc(int num) 180 1.1 mlelstv { 181 1.1 mlelstv struct bcm2835pwm_softc *sc; 182 1.1 mlelstv device_t dev; 183 1.1 mlelstv struct bcm_pwm_channel *pwm; 184 1.1 mlelstv 185 1.1 mlelstv dev = device_find_by_driver_unit("bcmpwm", 0); 186 1.1 mlelstv if (dev == NULL) 187 1.1 mlelstv return NULL; 188 1.1 mlelstv sc = device_private(dev); 189 1.1 mlelstv 190 1.1 mlelstv if (num < 0 || num >= __arraycount(sc->sc_channels)) 191 1.1 mlelstv return NULL; 192 1.1 mlelstv 193 1.1 mlelstv pwm = &sc->sc_channels[num]; 194 1.1 mlelstv 195 1.1 mlelstv mutex_enter(&sc->sc_lock); 196 1.1 mlelstv if (pwm->inuse) 197 1.1 mlelstv pwm = NULL; 198 1.1 mlelstv else 199 1.1 mlelstv pwm->inuse = true; 200 1.1 mlelstv mutex_exit(&sc->sc_lock); 201 1.1 mlelstv 202 1.1 mlelstv if (pwm) { 203 1.1 mlelstv pwm->datsave = PWM_READ(pwm->sc, pwm->dat); 204 1.1 mlelstv pwm->ctlsave = PWM_READ(pwm->sc, PWM_CTL); 205 1.1 mlelstv pwm->rngsave = PWM_READ(pwm->sc, pwm->rng); 206 1.1 mlelstv } 207 1.1 mlelstv 208 1.1 mlelstv return pwm; 209 1.1 mlelstv } 210 1.1 mlelstv 211 1.1 mlelstv void 212 1.1 mlelstv bcm_pwm_free(struct bcm_pwm_channel *pwm) 213 1.1 mlelstv { 214 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 215 1.1 mlelstv 216 1.1 mlelstv KASSERT(pwm->inuse); 217 1.1 mlelstv 218 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->rng, pwm->rngsave); 219 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_CTL, pwm->ctlsave & ~PWM_CTL_WRITEZERO); 220 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->dat, pwm->datsave); 221 1.1 mlelstv 222 1.1 mlelstv mutex_enter(&sc->sc_lock); 223 1.1 mlelstv pwm->inuse = false; 224 1.1 mlelstv mutex_exit(&sc->sc_lock); 225 1.1 mlelstv } 226 1.1 mlelstv 227 1.1 mlelstv void 228 1.1 mlelstv bcm_pwm_control(struct bcm_pwm_channel *pwm, uint32_t ctl, uint32_t rng) 229 1.1 mlelstv { 230 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 231 1.1 mlelstv uint32_t w; 232 1.1 mlelstv 233 1.1 mlelstv KASSERT(pwm->inuse); 234 1.1 mlelstv 235 1.1 mlelstv /* set control bits like for channel 0 236 1.1 mlelstv * there are generic bit definitions that the caller can use. 237 1.1 mlelstv */ 238 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_CTL); 239 1.1 mlelstv ctl = (w & ~pwm->ctlmask) | __SHIFTIN(ctl, pwm->ctlmask); 240 1.1 mlelstv 241 1.1 mlelstv /* when FIFO usage gets enabled but wasn't clear the FIFO */ 242 1.1 mlelstv if ((w & (PWM_CTL_USEF1|PWM_CTL_USEF2)) == 0 && 243 1.1 mlelstv (ctl & (PWM_CTL_USEF1|PWM_CTL_USEF2)) != 0) 244 1.1 mlelstv ctl |= PWM_CTL_CLRF1; 245 1.1 mlelstv 246 1.1 mlelstv PWM_WRITE(sc, pwm->rng, rng); 247 1.1 mlelstv PWM_WRITE(sc, PWM_CTL, ctl & ~PWM_CTL_WRITEZERO); 248 1.1 mlelstv } 249 1.1 mlelstv 250 1.1 mlelstv uint32_t 251 1.1 mlelstv bcm_pwm_status(struct bcm_pwm_channel *pwm) 252 1.1 mlelstv { 253 1.1 mlelstv uint32_t w; 254 1.1 mlelstv uint32_t common = PWM_STA_BERR | PWM_STA_RERR1 | 255 1.1 mlelstv PWM_STA_WERR1 | PWM_STA_EMPT1 | PWM_STA_FULL1; 256 1.1 mlelstv 257 1.1 mlelstv /* return status bits like for channel 0 258 1.1 mlelstv * there are generic bit definitions that the caller can use. 259 1.1 mlelstv * 260 1.1 mlelstv * The BERR bit is returned for both channels. 261 1.1 mlelstv */ 262 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_STA); 263 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_STA, w & 264 1.1 mlelstv (pwm->stamask | pwm->gapomask | common)); 265 1.1 mlelstv 266 1.1 mlelstv w = __SHIFTIN(__SHIFTOUT(w, pwm->stamask), PWM_STA_STA) 267 1.1 mlelstv | __SHIFTIN(__SHIFTOUT(w, pwm->gapomask), PWM_STA_GAPO) 268 1.1 mlelstv | (w & common); 269 1.1 mlelstv 270 1.1 mlelstv return w; 271 1.1 mlelstv } 272 1.1 mlelstv 273 1.1 mlelstv static int 274 1.1 mlelstv bcmpwm_wait(struct bcm2835pwm_softc *sc) 275 1.1 mlelstv { 276 1.1 mlelstv int i; 277 1.1 mlelstv uint32_t s; 278 1.1 mlelstv 279 1.1 mlelstv for (i=0; i<1000; ++i) { 280 1.1 mlelstv s = PWM_READ(sc, PWM_STA); 281 1.1 mlelstv if ((s & PWM_STA_FULL1) == 0) 282 1.1 mlelstv break; 283 1.1 mlelstv delay(1); 284 1.1 mlelstv } 285 1.1 mlelstv if (i >= 1000) 286 1.1 mlelstv return -1; 287 1.1 mlelstv 288 1.1 mlelstv return 0; 289 1.1 mlelstv } 290 1.1 mlelstv 291 1.1 mlelstv int 292 1.1 mlelstv bcm_pwm_write(struct bcm_pwm_channel *pwm, uint32_t *data1, uint32_t *data2, 293 1.1 mlelstv int len) 294 1.1 mlelstv { 295 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 296 1.1 mlelstv int n; 297 1.1 mlelstv uint32_t r; 298 1.1 mlelstv bool even = false; 299 1.1 mlelstv 300 1.1 mlelstv KASSERT(pwm->inuse); 301 1.1 mlelstv 302 1.1 mlelstv n = len; 303 1.1 mlelstv while (n > 0) { 304 1.1 mlelstv if (bcmpwm_wait(sc)) 305 1.1 mlelstv break; 306 1.1 mlelstv r = even ? *data2++ : *data1++; 307 1.1 mlelstv PWM_WRITE(sc, PWM_FIFO, r); 308 1.1 mlelstv if (data2 != NULL) 309 1.1 mlelstv even = !even; 310 1.1 mlelstv --n; 311 1.1 mlelstv } 312 1.1 mlelstv 313 1.1 mlelstv return len - n; 314 1.1 mlelstv } 315 1.1 mlelstv 316 1.1 mlelstv void 317 1.1 mlelstv bcm_pwm_set(struct bcm_pwm_channel *pwm, uint32_t w) 318 1.1 mlelstv { 319 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 320 1.1 mlelstv 321 1.1 mlelstv PWM_WRITE(sc, pwm->dat, w); 322 1.1 mlelstv } 323 1.1 mlelstv 324 1.1 mlelstv int 325 1.1 mlelstv bcm_pwm_flush(struct bcm_pwm_channel *pwm) 326 1.1 mlelstv { 327 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 328 1.1 mlelstv 329 1.1 mlelstv return bcmpwm_wait(sc) ? EIO : 0; 330 1.1 mlelstv } 331 1.1 mlelstv 332 1.1 mlelstv void 333 1.1 mlelstv bcm_pwm_dma_enable(struct bcm_pwm_channel *pwm, bool enable) 334 1.1 mlelstv { 335 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 336 1.1 mlelstv uint32_t w; 337 1.1 mlelstv 338 1.1 mlelstv #if 0 339 1.1 mlelstv w = PWM_READ(sc, PWM_DMAC); 340 1.1 mlelstv if (enable) 341 1.1 mlelstv w |= PWM_DMAC_ENAB; 342 1.1 mlelstv else 343 1.1 mlelstv w &= ~PWM_DMAC_ENAB; 344 1.1 mlelstv #else 345 1.1 mlelstv w = (enable ? PWM_DMAC_ENAB : 0) 346 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_PANIC) 347 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_DREQ); 348 1.1 mlelstv #endif 349 1.1 mlelstv PWM_WRITE(sc, PWM_DMAC, w & ~PWM_DMAC_WRITEZERO); 350 1.1 mlelstv } 351 1.1 mlelstv 352 1.1 mlelstv uint32_t 353 1.1 mlelstv bcm_pwm_dma_address(struct bcm_pwm_channel *pwm) 354 1.1 mlelstv { 355 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc; 356 1.1 mlelstv 357 1.2 skrll return sc->sc_iob + PWM_FIFO; 358 1.1 mlelstv } 359 1.1 mlelstv 360