1 /* $NetBSD: sunxi_i2s.c,v 1.6 2019/06/08 08:02:37 isaki Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: sunxi_i2s.c,v 1.6 2019/06/08 08:02:37 isaki Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/cpu.h> 35 #include <sys/device.h> 36 #include <sys/kmem.h> 37 #include <sys/gpio.h> 38 39 #include <sys/audioio.h> 40 #include <dev/audio/audio_if.h> 41 #include <dev/audio/linear.h> 42 43 #include <dev/fdt/fdtvar.h> 44 45 #define SUNXI_I2S_CLK_RATE 24576000 46 47 #define DA_CTL 0x00 48 #define DA_CTL_SDO_EN __BIT(8) 49 #define DA_CTL_MS __BIT(5) 50 #define DA_CTL_PCM __BIT(4) 51 #define DA_CTL_TXEN __BIT(2) 52 #define DA_CTL_RXEN __BIT(1) 53 #define DA_CTL_GEN __BIT(0) 54 #define DA_FAT0 0x04 55 #define DA_FAT0_LRCP __BIT(7) 56 #define DA_LRCP_NORMAL 0 57 #define DA_LRCP_INVERTED 1 58 #define DA_FAT0_BCP __BIT(6) 59 #define DA_BCP_NORMAL 0 60 #define DA_BCP_INVERTED 1 61 #define DA_FAT0_SR __BITS(5,4) 62 #define DA_FAT0_WSS __BITS(3,2) 63 #define DA_FAT0_FMT __BITS(1,0) 64 #define DA_FMT_I2S 0 65 #define DA_FMT_LJ 1 66 #define DA_FMT_RJ 2 67 #define DA_FAT1 0x08 68 #define DA_ISTA 0x0c 69 #define DA_RXFIFO 0x10 70 #define DA_FCTL 0x14 71 #define DA_FCTL_HUB_EN __BIT(31) 72 #define DA_FCTL_FTX __BIT(25) 73 #define DA_FCTL_FRX __BIT(24) 74 #define DA_FCTL_TXIM __BIT(2) 75 #define DA_FCTL_RXIM __BITS(1,0) 76 #define DA_FSTA 0x18 77 #define DA_INT 0x1c 78 #define DA_INT_TX_DRQ __BIT(7) 79 #define DA_INT_RX_DRQ __BIT(3) 80 #define DA_TXFIFO 0x20 81 #define DA_CLKD 0x24 82 #define DA_CLKD_MCLKO_EN __BIT(7) 83 #define DA_CLKD_BCLKDIV __BITS(6,4) 84 #define DA_CLKD_BCLKDIV_8 3 85 #define DA_CLKD_BCLKDIV_16 5 86 #define DA_CLKD_MCLKDIV __BITS(3,0) 87 #define DA_CLKD_MCLKDIV_1 0 88 #define DA_TXCNT 0x28 89 #define DA_RXCNT 0x2c 90 91 #define DA_CHSEL_EN __BITS(11,4) 92 #define DA_CHSEL_SEL __BITS(2,0) 93 94 struct sunxi_i2s_config { 95 const char *name; 96 bus_size_t txchsel; 97 bus_size_t txchmap; 98 bus_size_t rxchsel; 99 bus_size_t rxchmap; 100 }; 101 102 static const struct sunxi_i2s_config sun50i_a64_codec_config = { 103 .name = "Audio Codec (digital part)", 104 .txchsel = 0x30, 105 .txchmap = 0x34, 106 .rxchsel = 0x38, 107 .rxchmap = 0x3c, 108 }; 109 110 static const struct of_compat_data compat_data[] = { 111 { "allwinner,sun50i-a64-codec-i2s", 112 (uintptr_t)&sun50i_a64_codec_config }, 113 114 { NULL } 115 }; 116 117 struct sunxi_i2s_softc; 118 119 struct sunxi_i2s_chan { 120 struct sunxi_i2s_softc *ch_sc; 121 u_int ch_mode; 122 123 struct fdtbus_dma *ch_dma; 124 struct fdtbus_dma_req ch_req; 125 126 audio_params_t ch_params; 127 128 bus_addr_t ch_start_phys; 129 bus_addr_t ch_end_phys; 130 bus_addr_t ch_cur_phys; 131 int ch_blksize; 132 133 void (*ch_intr)(void *); 134 void *ch_intrarg; 135 }; 136 137 struct sunxi_i2s_dma { 138 LIST_ENTRY(sunxi_i2s_dma) dma_list; 139 bus_dmamap_t dma_map; 140 void *dma_addr; 141 size_t dma_size; 142 bus_dma_segment_t dma_segs[1]; 143 int dma_nsegs; 144 }; 145 146 struct sunxi_i2s_softc { 147 device_t sc_dev; 148 bus_space_tag_t sc_bst; 149 bus_space_handle_t sc_bsh; 150 bus_dma_tag_t sc_dmat; 151 int sc_phandle; 152 bus_addr_t sc_baseaddr; 153 154 struct sunxi_i2s_config *sc_cfg; 155 156 LIST_HEAD(, sunxi_i2s_dma) sc_dmalist; 157 158 kmutex_t sc_lock; 159 kmutex_t sc_intr_lock; 160 161 struct audio_format sc_format; 162 163 struct sunxi_i2s_chan sc_pchan; 164 struct sunxi_i2s_chan sc_rchan; 165 166 struct audio_dai_device sc_dai; 167 }; 168 169 #define I2S_READ(sc, reg) \ 170 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 171 #define I2S_WRITE(sc, reg, val) \ 172 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 173 174 static int 175 sunxi_i2s_allocdma(struct sunxi_i2s_softc *sc, size_t size, 176 size_t align, struct sunxi_i2s_dma *dma) 177 { 178 int error; 179 180 dma->dma_size = size; 181 error = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, align, 0, 182 dma->dma_segs, 1, &dma->dma_nsegs, BUS_DMA_WAITOK); 183 if (error) 184 return error; 185 186 error = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs, 187 dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT); 188 if (error) 189 goto free; 190 191 error = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs, 192 dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map); 193 if (error) 194 goto unmap; 195 196 error = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr, 197 dma->dma_size, NULL, BUS_DMA_WAITOK); 198 if (error) 199 goto destroy; 200 201 return 0; 202 203 destroy: 204 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 205 unmap: 206 bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 207 free: 208 bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 209 210 return error; 211 } 212 213 static void 214 sunxi_i2s_freedma(struct sunxi_i2s_softc *sc, struct sunxi_i2s_dma *dma) 215 { 216 bus_dmamap_unload(sc->sc_dmat, dma->dma_map); 217 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map); 218 bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size); 219 bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs); 220 } 221 222 static int 223 sunxi_i2s_transfer(struct sunxi_i2s_chan *ch) 224 { 225 bus_dma_segment_t seg; 226 227 seg.ds_addr = ch->ch_cur_phys; 228 seg.ds_len = ch->ch_blksize; 229 ch->ch_req.dreq_segs = &seg; 230 ch->ch_req.dreq_nsegs = 1; 231 232 return fdtbus_dma_transfer(ch->ch_dma, &ch->ch_req); 233 } 234 235 static int 236 sunxi_i2s_query_format(void *priv, audio_format_query_t *afp) 237 { 238 struct sunxi_i2s_softc * const sc = priv; 239 240 return audio_query_format(&sc->sc_format, 1, afp); 241 } 242 243 static int 244 sunxi_i2s_set_format(void *priv, int setmode, 245 const audio_params_t *play, const audio_params_t *rec, 246 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil) 247 { 248 249 if ((setmode & AUMODE_PLAY)) { 250 pfil->codec = audio_internal_to_linear32; 251 } 252 if ((setmode & AUMODE_RECORD)) { 253 rfil->codec = audio_linear32_to_internal; 254 } 255 256 return 0; 257 } 258 259 static void * 260 sunxi_i2s_allocm(void *priv, int dir, size_t size) 261 { 262 struct sunxi_i2s_softc * const sc = priv; 263 struct sunxi_i2s_dma *dma; 264 int error; 265 266 dma = kmem_alloc(sizeof(*dma), KM_SLEEP); 267 268 error = sunxi_i2s_allocdma(sc, size, 16, dma); 269 if (error) { 270 kmem_free(dma, sizeof(*dma)); 271 device_printf(sc->sc_dev, "couldn't allocate DMA memory (%d)\n", 272 error); 273 return NULL; 274 } 275 276 LIST_INSERT_HEAD(&sc->sc_dmalist, dma, dma_list); 277 278 return dma->dma_addr; 279 } 280 281 static void 282 sunxi_i2s_freem(void *priv, void *addr, size_t size) 283 { 284 struct sunxi_i2s_softc * const sc = priv; 285 struct sunxi_i2s_dma *dma; 286 287 LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 288 if (dma->dma_addr == addr) { 289 sunxi_i2s_freedma(sc, dma); 290 LIST_REMOVE(dma, dma_list); 291 kmem_free(dma, sizeof(*dma)); 292 break; 293 } 294 } 295 296 static int 297 sunxi_i2s_get_props(void *priv) 298 { 299 300 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE | 301 AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT; 302 } 303 304 static int 305 sunxi_i2s_round_blocksize(void *priv, int bs, int mode, 306 const audio_params_t *params) 307 { 308 bs &= ~3; 309 if (bs == 0) 310 bs = 4; 311 return bs; 312 } 313 314 static int 315 sunxi_i2s_trigger_output(void *priv, void *start, void *end, int blksize, 316 void (*intr)(void *), void *intrarg, const audio_params_t *params) 317 { 318 struct sunxi_i2s_softc * const sc = priv; 319 struct sunxi_i2s_chan *ch = &sc->sc_pchan; 320 struct sunxi_i2s_dma *dma; 321 bus_addr_t pstart; 322 bus_size_t psize; 323 uint32_t val; 324 int error; 325 326 pstart = 0; 327 psize = (uintptr_t)end - (uintptr_t)start; 328 329 LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 330 if (dma->dma_addr == start) { 331 pstart = dma->dma_map->dm_segs[0].ds_addr; 332 break; 333 } 334 if (pstart == 0) { 335 device_printf(sc->sc_dev, "bad addr %p\n", start); 336 return EINVAL; 337 } 338 339 ch->ch_intr = intr; 340 ch->ch_intrarg = intrarg; 341 ch->ch_start_phys = ch->ch_cur_phys = pstart; 342 ch->ch_end_phys = pstart + psize; 343 ch->ch_blksize = blksize; 344 345 /* Flush FIFO */ 346 val = I2S_READ(sc, DA_FCTL); 347 I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX); 348 I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX); 349 350 /* Reset TX sample counter */ 351 I2S_WRITE(sc, DA_TXCNT, 0); 352 353 /* Enable transmitter block */ 354 val = I2S_READ(sc, DA_CTL); 355 I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN); 356 357 /* Enable TX DRQ */ 358 val = I2S_READ(sc, DA_INT); 359 I2S_WRITE(sc, DA_INT, val | DA_INT_TX_DRQ); 360 361 /* Start DMA transfer */ 362 error = sunxi_i2s_transfer(ch); 363 if (error != 0) { 364 aprint_error_dev(sc->sc_dev, 365 "failed to start DMA transfer: %d\n", error); 366 return error; 367 } 368 369 return 0; 370 } 371 372 static int 373 sunxi_i2s_trigger_input(void *priv, void *start, void *end, int blksize, 374 void (*intr)(void *), void *intrarg, const audio_params_t *params) 375 { 376 struct sunxi_i2s_softc * const sc = priv; 377 struct sunxi_i2s_chan *ch = &sc->sc_rchan; 378 struct sunxi_i2s_dma *dma; 379 bus_addr_t pstart; 380 bus_size_t psize; 381 uint32_t val; 382 int error; 383 384 pstart = 0; 385 psize = (uintptr_t)end - (uintptr_t)start; 386 387 LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 388 if (dma->dma_addr == start) { 389 pstart = dma->dma_map->dm_segs[0].ds_addr; 390 break; 391 } 392 if (pstart == 0) { 393 device_printf(sc->sc_dev, "bad addr %p\n", start); 394 return EINVAL; 395 } 396 397 ch->ch_intr = intr; 398 ch->ch_intrarg = intrarg; 399 ch->ch_start_phys = ch->ch_cur_phys = pstart; 400 ch->ch_end_phys = pstart + psize; 401 ch->ch_blksize = blksize; 402 403 /* Flush FIFO */ 404 val = I2S_READ(sc, DA_FCTL); 405 I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX); 406 I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX); 407 408 /* Reset RX sample counter */ 409 I2S_WRITE(sc, DA_RXCNT, 0); 410 411 /* Enable receiver block */ 412 val = I2S_READ(sc, DA_CTL); 413 I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN); 414 415 /* Enable RX DRQ */ 416 val = I2S_READ(sc, DA_INT); 417 I2S_WRITE(sc, DA_INT, val | DA_INT_RX_DRQ); 418 419 /* Start DMA transfer */ 420 error = sunxi_i2s_transfer(ch); 421 if (error != 0) { 422 aprint_error_dev(sc->sc_dev, 423 "failed to start DMA transfer: %d\n", error); 424 return error; 425 } 426 427 return 0; 428 } 429 430 static int 431 sunxi_i2s_halt_output(void *priv) 432 { 433 struct sunxi_i2s_softc * const sc = priv; 434 struct sunxi_i2s_chan *ch = &sc->sc_pchan; 435 uint32_t val; 436 437 /* Disable DMA channel */ 438 fdtbus_dma_halt(ch->ch_dma); 439 440 /* Disable transmitter block */ 441 val = I2S_READ(sc, DA_CTL); 442 I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN); 443 444 /* Disable TX DRQ */ 445 val = I2S_READ(sc, DA_INT); 446 I2S_WRITE(sc, DA_INT, val & ~DA_INT_TX_DRQ); 447 448 ch->ch_intr = NULL; 449 ch->ch_intrarg = NULL; 450 451 return 0; 452 } 453 454 static int 455 sunxi_i2s_halt_input(void *priv) 456 { 457 struct sunxi_i2s_softc * const sc = priv; 458 struct sunxi_i2s_chan *ch = &sc->sc_rchan; 459 uint32_t val; 460 461 /* Disable DMA channel */ 462 fdtbus_dma_halt(ch->ch_dma); 463 464 /* Disable receiver block */ 465 val = I2S_READ(sc, DA_CTL); 466 I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN); 467 468 /* Disable RX DRQ */ 469 val = I2S_READ(sc, DA_INT); 470 I2S_WRITE(sc, DA_INT, val & ~DA_INT_RX_DRQ); 471 472 return 0; 473 } 474 475 static void 476 sunxi_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread) 477 { 478 struct sunxi_i2s_softc * const sc = priv; 479 480 *intr = &sc->sc_intr_lock; 481 *thread = &sc->sc_lock; 482 } 483 484 static const struct audio_hw_if sunxi_i2s_hw_if = { 485 .query_format = sunxi_i2s_query_format, 486 .set_format = sunxi_i2s_set_format, 487 .allocm = sunxi_i2s_allocm, 488 .freem = sunxi_i2s_freem, 489 .get_props = sunxi_i2s_get_props, 490 .round_blocksize = sunxi_i2s_round_blocksize, 491 .trigger_output = sunxi_i2s_trigger_output, 492 .trigger_input = sunxi_i2s_trigger_input, 493 .halt_output = sunxi_i2s_halt_output, 494 .halt_input = sunxi_i2s_halt_input, 495 .get_locks = sunxi_i2s_get_locks, 496 }; 497 498 static void 499 sunxi_i2s_dmaintr(void *priv) 500 { 501 struct sunxi_i2s_chan * const ch = priv; 502 struct sunxi_i2s_softc * const sc = ch->ch_sc; 503 504 mutex_enter(&sc->sc_intr_lock); 505 ch->ch_cur_phys += ch->ch_blksize; 506 if (ch->ch_cur_phys >= ch->ch_end_phys) 507 ch->ch_cur_phys = ch->ch_start_phys; 508 509 if (ch->ch_intr) { 510 ch->ch_intr(ch->ch_intrarg); 511 sunxi_i2s_transfer(ch); 512 } 513 mutex_exit(&sc->sc_intr_lock); 514 } 515 516 static int 517 sunxi_i2s_chan_init(struct sunxi_i2s_softc *sc, 518 struct sunxi_i2s_chan *ch, u_int mode, const char *dmaname) 519 { 520 ch->ch_sc = sc; 521 ch->ch_mode = mode; 522 ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_i2s_dmaintr, ch); 523 if (ch->ch_dma == NULL) { 524 aprint_error(": couldn't get dma channel \"%s\"\n", dmaname); 525 return ENXIO; 526 } 527 528 if (mode == AUMODE_PLAY) { 529 ch->ch_req.dreq_dir = FDT_DMA_WRITE; 530 ch->ch_req.dreq_dev_phys = 531 sc->sc_baseaddr + DA_TXFIFO; 532 } else { 533 ch->ch_req.dreq_dir = FDT_DMA_READ; 534 ch->ch_req.dreq_dev_phys = 535 sc->sc_baseaddr + DA_RXFIFO; 536 } 537 ch->ch_req.dreq_mem_opt.opt_bus_width = 32; 538 ch->ch_req.dreq_mem_opt.opt_burst_len = 8; 539 ch->ch_req.dreq_dev_opt.opt_bus_width = 32; 540 ch->ch_req.dreq_dev_opt.opt_burst_len = 8; 541 542 return 0; 543 } 544 545 static int 546 sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir) 547 { 548 struct sunxi_i2s_softc * const sc = audio_dai_private(dai); 549 uint32_t val; 550 551 /* XXX */ 552 553 val = DA_CLKD_MCLKO_EN; 554 val |= __SHIFTIN(DA_CLKD_BCLKDIV_8, DA_CLKD_BCLKDIV); 555 val |= __SHIFTIN(DA_CLKD_MCLKDIV_1, DA_CLKD_MCLKDIV); 556 557 I2S_WRITE(sc, DA_CLKD, val); 558 559 return 0; 560 } 561 562 static int 563 sunxi_i2s_dai_set_format(audio_dai_tag_t dai, u_int format) 564 { 565 struct sunxi_i2s_softc * const sc = audio_dai_private(dai); 566 uint32_t ctl, fat0; 567 568 const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK); 569 const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK); 570 const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK); 571 572 ctl = I2S_READ(sc, DA_CTL); 573 fat0 = I2S_READ(sc, DA_FAT0); 574 575 fat0 &= ~DA_FAT0_FMT; 576 switch (fmt) { 577 case AUDIO_DAI_FORMAT_I2S: 578 fat0 |= __SHIFTIN(DA_FMT_I2S, DA_FAT0_FMT); 579 break; 580 case AUDIO_DAI_FORMAT_RJ: 581 fat0 |= __SHIFTIN(DA_FMT_RJ, DA_FAT0_FMT); 582 break; 583 case AUDIO_DAI_FORMAT_LJ: 584 fat0 |= __SHIFTIN(DA_FMT_LJ, DA_FAT0_FMT); 585 break; 586 default: 587 return EINVAL; 588 } 589 590 fat0 &= ~(DA_FAT0_LRCP|DA_FAT0_BCP); 591 if (AUDIO_DAI_POLARITY_B(pol)) 592 fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP); 593 if (AUDIO_DAI_POLARITY_F(pol)) 594 fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP); 595 596 switch (clk) { 597 case AUDIO_DAI_CLOCK_CBM_CFM: 598 ctl |= DA_CTL_MS; /* codec is master */ 599 break; 600 case AUDIO_DAI_CLOCK_CBS_CFS: 601 ctl &= ~DA_CTL_MS; /* codec is slave */ 602 break; 603 default: 604 return EINVAL; 605 } 606 607 ctl &= ~DA_CTL_PCM; 608 609 I2S_WRITE(sc, DA_CTL, ctl); 610 I2S_WRITE(sc, DA_FAT0, fat0); 611 612 return 0; 613 } 614 615 static audio_dai_tag_t 616 sunxi_i2s_dai_get_tag(device_t dev, const void *data, size_t len) 617 { 618 struct sunxi_i2s_softc * const sc = device_private(dev); 619 620 if (len != 4) 621 return NULL; 622 623 return &sc->sc_dai; 624 } 625 626 static struct fdtbus_dai_controller_func sunxi_i2s_dai_funcs = { 627 .get_tag = sunxi_i2s_dai_get_tag 628 }; 629 630 static int 631 sunxi_i2s_clock_init(int phandle) 632 { 633 struct fdtbus_reset *rst; 634 struct clk *clk; 635 int error; 636 637 /* Set module clock to 24.576MHz, suitable for 48 kHz sampling rates */ 638 clk = fdtbus_clock_get(phandle, "mod"); 639 if (clk == NULL) { 640 aprint_error(": couldn't find mod clock\n"); 641 return ENXIO; 642 } 643 error = clk_set_rate(clk, SUNXI_I2S_CLK_RATE); 644 if (error != 0) { 645 aprint_error(": couldn't set mod clock rate: %d\n", error); 646 return error; 647 } 648 error = clk_enable(clk); 649 if (error != 0) { 650 aprint_error(": couldn't enable mod clock: %d\n", error); 651 return error; 652 } 653 654 /* Enable APB clock */ 655 clk = fdtbus_clock_get(phandle, "apb"); 656 if (clk == NULL) { 657 aprint_error(": couldn't find apb clock\n"); 658 return ENXIO; 659 } 660 error = clk_enable(clk); 661 if (error != 0) { 662 aprint_error(": couldn't enable apb clock: %d\n", error); 663 return error; 664 } 665 666 /* De-assert reset */ 667 rst = fdtbus_reset_get(phandle, "rst"); 668 if (rst == NULL) { 669 aprint_error(": couldn't find reset\n"); 670 return ENXIO; 671 } 672 error = fdtbus_reset_deassert(rst); 673 if (error != 0) { 674 aprint_error(": couldn't de-assert reset: %d\n", error); 675 return error; 676 } 677 678 return 0; 679 } 680 681 static int 682 sunxi_i2s_match(device_t parent, cfdata_t cf, void *aux) 683 { 684 struct fdt_attach_args * const faa = aux; 685 686 return of_match_compat_data(faa->faa_phandle, compat_data); 687 } 688 689 static void 690 sunxi_i2s_attach(device_t parent, device_t self, void *aux) 691 { 692 struct sunxi_i2s_softc * const sc = device_private(self); 693 struct fdt_attach_args * const faa = aux; 694 const int phandle = faa->faa_phandle; 695 bus_addr_t addr; 696 bus_size_t size; 697 uint32_t val; 698 699 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 700 aprint_error(": couldn't get registers\n"); 701 return; 702 } 703 704 if (sunxi_i2s_clock_init(phandle) != 0) 705 return; 706 707 sc->sc_dev = self; 708 sc->sc_phandle = phandle; 709 sc->sc_baseaddr = addr; 710 sc->sc_bst = faa->faa_bst; 711 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 712 aprint_error(": couldn't map registers\n"); 713 return; 714 } 715 sc->sc_dmat = faa->faa_dmat; 716 LIST_INIT(&sc->sc_dmalist); 717 sc->sc_cfg = (void *)of_search_compatible(phandle, compat_data)->data; 718 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 719 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED); 720 721 if (sunxi_i2s_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 || 722 sunxi_i2s_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) { 723 aprint_error(": couldn't setup channels\n"); 724 return; 725 } 726 727 aprint_naive("\n"); 728 aprint_normal(": %s\n", sc->sc_cfg->name); 729 730 /* Reset */ 731 val = I2S_READ(sc, DA_CTL); 732 val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN); 733 I2S_WRITE(sc, DA_CTL, val); 734 735 val = I2S_READ(sc, DA_FCTL); 736 val &= ~(DA_FCTL_FTX|DA_FCTL_FRX); 737 I2S_WRITE(sc, DA_FCTL, val); 738 739 I2S_WRITE(sc, DA_TXCNT, 0); 740 I2S_WRITE(sc, DA_RXCNT, 0); 741 742 /* Enable */ 743 I2S_WRITE(sc, DA_CTL, DA_CTL_GEN | DA_CTL_SDO_EN); 744 745 /* Setup channels */ 746 I2S_WRITE(sc, sc->sc_cfg->txchmap, 0x76543210); 747 I2S_WRITE(sc, sc->sc_cfg->txchsel, __SHIFTIN(1, DA_CHSEL_SEL) | 748 __SHIFTIN(3, DA_CHSEL_EN)); 749 I2S_WRITE(sc, sc->sc_cfg->rxchmap, 0x76543210); 750 I2S_WRITE(sc, sc->sc_cfg->rxchsel, __SHIFTIN(1, DA_CHSEL_SEL) | 751 __SHIFTIN(3, DA_CHSEL_EN)); 752 753 sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD; 754 sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE; 755 sc->sc_format.validbits = 32; 756 sc->sc_format.precision = 32; 757 sc->sc_format.channels = 2; 758 sc->sc_format.channel_mask = AUFMT_STEREO; 759 sc->sc_format.frequency_type = 1; 760 sc->sc_format.frequency[0] = 48000; 761 762 sc->sc_dai.dai_set_sysclk = sunxi_i2s_dai_set_sysclk; 763 sc->sc_dai.dai_set_format = sunxi_i2s_dai_set_format; 764 sc->sc_dai.dai_hw_if = &sunxi_i2s_hw_if; 765 sc->sc_dai.dai_dev = self; 766 sc->sc_dai.dai_priv = sc; 767 fdtbus_register_dai_controller(self, phandle, &sunxi_i2s_dai_funcs); 768 } 769 770 CFATTACH_DECL_NEW(sunxi_i2s, sizeof(struct sunxi_i2s_softc), 771 sunxi_i2s_match, sunxi_i2s_attach, NULL, NULL); 772