1 /* $NetBSD: sunxi_i2s.c,v 1.4 2019/05/08 13:40:14 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.4 2019/05/08 13:40:14 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-acodec-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 return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE| 300 AUDIO_PROP_MMAP|AUDIO_PROP_FULLDUPLEX|AUDIO_PROP_INDEPENDENT; 301 } 302 303 static int 304 sunxi_i2s_round_blocksize(void *priv, int bs, int mode, 305 const audio_params_t *params) 306 { 307 bs &= ~3; 308 if (bs == 0) 309 bs = 4; 310 return bs; 311 } 312 313 static int 314 sunxi_i2s_trigger_output(void *priv, void *start, void *end, int blksize, 315 void (*intr)(void *), void *intrarg, const audio_params_t *params) 316 { 317 struct sunxi_i2s_softc * const sc = priv; 318 struct sunxi_i2s_chan *ch = &sc->sc_pchan; 319 struct sunxi_i2s_dma *dma; 320 bus_addr_t pstart; 321 bus_size_t psize; 322 uint32_t val; 323 int error; 324 325 pstart = 0; 326 psize = (uintptr_t)end - (uintptr_t)start; 327 328 LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 329 if (dma->dma_addr == start) { 330 pstart = dma->dma_map->dm_segs[0].ds_addr; 331 break; 332 } 333 if (pstart == 0) { 334 device_printf(sc->sc_dev, "bad addr %p\n", start); 335 return EINVAL; 336 } 337 338 ch->ch_intr = intr; 339 ch->ch_intrarg = intrarg; 340 ch->ch_start_phys = ch->ch_cur_phys = pstart; 341 ch->ch_end_phys = pstart + psize; 342 ch->ch_blksize = blksize; 343 344 /* Flush FIFO */ 345 val = I2S_READ(sc, DA_FCTL); 346 I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FTX); 347 I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FTX); 348 349 /* Reset TX sample counter */ 350 I2S_WRITE(sc, DA_TXCNT, 0); 351 352 /* Enable transmitter block */ 353 val = I2S_READ(sc, DA_CTL); 354 I2S_WRITE(sc, DA_CTL, val | DA_CTL_TXEN); 355 356 /* Enable TX DRQ */ 357 val = I2S_READ(sc, DA_INT); 358 I2S_WRITE(sc, DA_INT, val | DA_INT_TX_DRQ); 359 360 /* Start DMA transfer */ 361 error = sunxi_i2s_transfer(ch); 362 if (error != 0) { 363 aprint_error_dev(sc->sc_dev, 364 "failed to start DMA transfer: %d\n", error); 365 return error; 366 } 367 368 return 0; 369 } 370 371 static int 372 sunxi_i2s_trigger_input(void *priv, void *start, void *end, int blksize, 373 void (*intr)(void *), void *intrarg, const audio_params_t *params) 374 { 375 struct sunxi_i2s_softc * const sc = priv; 376 struct sunxi_i2s_chan *ch = &sc->sc_rchan; 377 struct sunxi_i2s_dma *dma; 378 bus_addr_t pstart; 379 bus_size_t psize; 380 uint32_t val; 381 int error; 382 383 pstart = 0; 384 psize = (uintptr_t)end - (uintptr_t)start; 385 386 LIST_FOREACH(dma, &sc->sc_dmalist, dma_list) 387 if (dma->dma_addr == start) { 388 pstart = dma->dma_map->dm_segs[0].ds_addr; 389 break; 390 } 391 if (pstart == 0) { 392 device_printf(sc->sc_dev, "bad addr %p\n", start); 393 return EINVAL; 394 } 395 396 ch->ch_intr = intr; 397 ch->ch_intrarg = intrarg; 398 ch->ch_start_phys = ch->ch_cur_phys = pstart; 399 ch->ch_end_phys = pstart + psize; 400 ch->ch_blksize = blksize; 401 402 /* Flush FIFO */ 403 val = I2S_READ(sc, DA_FCTL); 404 I2S_WRITE(sc, DA_FCTL, val | DA_FCTL_FRX); 405 I2S_WRITE(sc, DA_FCTL, val & ~DA_FCTL_FRX); 406 407 /* Reset RX sample counter */ 408 I2S_WRITE(sc, DA_RXCNT, 0); 409 410 /* Enable receiver block */ 411 val = I2S_READ(sc, DA_CTL); 412 I2S_WRITE(sc, DA_CTL, val | DA_CTL_RXEN); 413 414 /* Enable RX DRQ */ 415 val = I2S_READ(sc, DA_INT); 416 I2S_WRITE(sc, DA_INT, val | DA_INT_RX_DRQ); 417 418 /* Start DMA transfer */ 419 error = sunxi_i2s_transfer(ch); 420 if (error != 0) { 421 aprint_error_dev(sc->sc_dev, 422 "failed to start DMA transfer: %d\n", error); 423 return error; 424 } 425 426 return 0; 427 } 428 429 static int 430 sunxi_i2s_halt_output(void *priv) 431 { 432 struct sunxi_i2s_softc * const sc = priv; 433 struct sunxi_i2s_chan *ch = &sc->sc_pchan; 434 uint32_t val; 435 436 /* Disable DMA channel */ 437 fdtbus_dma_halt(ch->ch_dma); 438 439 /* Disable transmitter block */ 440 val = I2S_READ(sc, DA_CTL); 441 I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_TXEN); 442 443 /* Disable TX DRQ */ 444 val = I2S_READ(sc, DA_INT); 445 I2S_WRITE(sc, DA_INT, val & ~DA_INT_TX_DRQ); 446 447 ch->ch_intr = NULL; 448 ch->ch_intrarg = NULL; 449 450 return 0; 451 } 452 453 static int 454 sunxi_i2s_halt_input(void *priv) 455 { 456 struct sunxi_i2s_softc * const sc = priv; 457 struct sunxi_i2s_chan *ch = &sc->sc_rchan; 458 uint32_t val; 459 460 /* Disable DMA channel */ 461 fdtbus_dma_halt(ch->ch_dma); 462 463 /* Disable receiver block */ 464 val = I2S_READ(sc, DA_CTL); 465 I2S_WRITE(sc, DA_CTL, val & ~DA_CTL_RXEN); 466 467 /* Disable RX DRQ */ 468 val = I2S_READ(sc, DA_INT); 469 I2S_WRITE(sc, DA_INT, val & ~DA_INT_RX_DRQ); 470 471 return 0; 472 } 473 474 static void 475 sunxi_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread) 476 { 477 struct sunxi_i2s_softc * const sc = priv; 478 479 *intr = &sc->sc_intr_lock; 480 *thread = &sc->sc_lock; 481 } 482 483 static const struct audio_hw_if sunxi_i2s_hw_if = { 484 .query_format = sunxi_i2s_query_format, 485 .set_format = sunxi_i2s_set_format, 486 .allocm = sunxi_i2s_allocm, 487 .freem = sunxi_i2s_freem, 488 .get_props = sunxi_i2s_get_props, 489 .round_blocksize = sunxi_i2s_round_blocksize, 490 .trigger_output = sunxi_i2s_trigger_output, 491 .trigger_input = sunxi_i2s_trigger_input, 492 .halt_output = sunxi_i2s_halt_output, 493 .halt_input = sunxi_i2s_halt_input, 494 .get_locks = sunxi_i2s_get_locks, 495 }; 496 497 static void 498 sunxi_i2s_dmaintr(void *priv) 499 { 500 struct sunxi_i2s_chan * const ch = priv; 501 struct sunxi_i2s_softc * const sc = ch->ch_sc; 502 503 mutex_enter(&sc->sc_intr_lock); 504 ch->ch_cur_phys += ch->ch_blksize; 505 if (ch->ch_cur_phys >= ch->ch_end_phys) 506 ch->ch_cur_phys = ch->ch_start_phys; 507 508 if (ch->ch_intr) { 509 ch->ch_intr(ch->ch_intrarg); 510 sunxi_i2s_transfer(ch); 511 } 512 mutex_exit(&sc->sc_intr_lock); 513 } 514 515 static int 516 sunxi_i2s_chan_init(struct sunxi_i2s_softc *sc, 517 struct sunxi_i2s_chan *ch, u_int mode, const char *dmaname) 518 { 519 ch->ch_sc = sc; 520 ch->ch_mode = mode; 521 ch->ch_dma = fdtbus_dma_get(sc->sc_phandle, dmaname, sunxi_i2s_dmaintr, ch); 522 if (ch->ch_dma == NULL) { 523 aprint_error(": couldn't get dma channel \"%s\"\n", dmaname); 524 return ENXIO; 525 } 526 527 if (mode == AUMODE_PLAY) { 528 ch->ch_req.dreq_dir = FDT_DMA_WRITE; 529 ch->ch_req.dreq_dev_phys = 530 sc->sc_baseaddr + DA_TXFIFO; 531 } else { 532 ch->ch_req.dreq_dir = FDT_DMA_READ; 533 ch->ch_req.dreq_dev_phys = 534 sc->sc_baseaddr + DA_RXFIFO; 535 } 536 ch->ch_req.dreq_mem_opt.opt_bus_width = 32; 537 ch->ch_req.dreq_mem_opt.opt_burst_len = 8; 538 ch->ch_req.dreq_dev_opt.opt_bus_width = 32; 539 ch->ch_req.dreq_dev_opt.opt_burst_len = 8; 540 541 return 0; 542 } 543 544 static int 545 sunxi_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir) 546 { 547 struct sunxi_i2s_softc * const sc = audio_dai_private(dai); 548 uint32_t val; 549 550 /* XXX */ 551 552 val = DA_CLKD_MCLKO_EN; 553 val |= __SHIFTIN(DA_CLKD_BCLKDIV_8, DA_CLKD_BCLKDIV); 554 val |= __SHIFTIN(DA_CLKD_MCLKDIV_1, DA_CLKD_MCLKDIV); 555 556 I2S_WRITE(sc, DA_CLKD, val); 557 558 return 0; 559 } 560 561 static int 562 sunxi_i2s_dai_set_format(audio_dai_tag_t dai, u_int format) 563 { 564 struct sunxi_i2s_softc * const sc = audio_dai_private(dai); 565 uint32_t ctl, fat0; 566 567 const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK); 568 const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK); 569 const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK); 570 571 ctl = I2S_READ(sc, DA_CTL); 572 fat0 = I2S_READ(sc, DA_FAT0); 573 574 fat0 &= ~DA_FAT0_FMT; 575 switch (fmt) { 576 case AUDIO_DAI_FORMAT_I2S: 577 fat0 |= __SHIFTIN(DA_FMT_I2S, DA_FAT0_FMT); 578 break; 579 case AUDIO_DAI_FORMAT_RJ: 580 fat0 |= __SHIFTIN(DA_FMT_RJ, DA_FAT0_FMT); 581 break; 582 case AUDIO_DAI_FORMAT_LJ: 583 fat0 |= __SHIFTIN(DA_FMT_LJ, DA_FAT0_FMT); 584 break; 585 default: 586 return EINVAL; 587 } 588 589 fat0 &= ~(DA_FAT0_LRCP|DA_FAT0_BCP); 590 if (AUDIO_DAI_POLARITY_B(pol)) 591 fat0 |= __SHIFTIN(DA_BCP_INVERTED, DA_FAT0_BCP); 592 if (AUDIO_DAI_POLARITY_F(pol)) 593 fat0 |= __SHIFTIN(DA_LRCP_INVERTED, DA_FAT0_LRCP); 594 595 switch (clk) { 596 case AUDIO_DAI_CLOCK_CBM_CFM: 597 ctl |= DA_CTL_MS; /* codec is master */ 598 break; 599 case AUDIO_DAI_CLOCK_CBS_CFS: 600 ctl &= ~DA_CTL_MS; /* codec is slave */ 601 break; 602 default: 603 return EINVAL; 604 } 605 606 ctl &= ~DA_CTL_PCM; 607 608 I2S_WRITE(sc, DA_CTL, ctl); 609 I2S_WRITE(sc, DA_FAT0, fat0); 610 611 return 0; 612 } 613 614 static audio_dai_tag_t 615 sunxi_i2s_dai_get_tag(device_t dev, const void *data, size_t len) 616 { 617 struct sunxi_i2s_softc * const sc = device_private(dev); 618 619 if (len != 4) 620 return NULL; 621 622 return &sc->sc_dai; 623 } 624 625 static struct fdtbus_dai_controller_func sunxi_i2s_dai_funcs = { 626 .get_tag = sunxi_i2s_dai_get_tag 627 }; 628 629 static int 630 sunxi_i2s_clock_init(int phandle) 631 { 632 struct fdtbus_reset *rst; 633 struct clk *clk; 634 int error; 635 636 /* Set module clock to 24.576MHz, suitable for 48 kHz sampling rates */ 637 clk = fdtbus_clock_get(phandle, "mod"); 638 if (clk == NULL) { 639 aprint_error(": couldn't find mod clock\n"); 640 return ENXIO; 641 } 642 error = clk_set_rate(clk, SUNXI_I2S_CLK_RATE); 643 if (error != 0) { 644 aprint_error(": couldn't set mod clock rate: %d\n", error); 645 return error; 646 } 647 error = clk_enable(clk); 648 if (error != 0) { 649 aprint_error(": couldn't enable mod clock: %d\n", error); 650 return error; 651 } 652 653 /* Enable APB clock */ 654 clk = fdtbus_clock_get(phandle, "apb"); 655 if (clk == NULL) { 656 aprint_error(": couldn't find apb clock\n"); 657 return ENXIO; 658 } 659 error = clk_enable(clk); 660 if (error != 0) { 661 aprint_error(": couldn't enable apb clock: %d\n", error); 662 return error; 663 } 664 665 /* De-assert reset */ 666 rst = fdtbus_reset_get(phandle, "rst"); 667 if (rst == NULL) { 668 aprint_error(": couldn't find reset\n"); 669 return ENXIO; 670 } 671 error = fdtbus_reset_deassert(rst); 672 if (error != 0) { 673 aprint_error(": couldn't de-assert reset: %d\n", error); 674 return error; 675 } 676 677 return 0; 678 } 679 680 static int 681 sunxi_i2s_match(device_t parent, cfdata_t cf, void *aux) 682 { 683 struct fdt_attach_args * const faa = aux; 684 685 return of_match_compat_data(faa->faa_phandle, compat_data); 686 } 687 688 static void 689 sunxi_i2s_attach(device_t parent, device_t self, void *aux) 690 { 691 struct sunxi_i2s_softc * const sc = device_private(self); 692 struct fdt_attach_args * const faa = aux; 693 const int phandle = faa->faa_phandle; 694 bus_addr_t addr; 695 bus_size_t size; 696 uint32_t val; 697 698 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 699 aprint_error(": couldn't get registers\n"); 700 return; 701 } 702 703 if (sunxi_i2s_clock_init(phandle) != 0) 704 return; 705 706 sc->sc_dev = self; 707 sc->sc_phandle = phandle; 708 sc->sc_baseaddr = addr; 709 sc->sc_bst = faa->faa_bst; 710 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 711 aprint_error(": couldn't map registers\n"); 712 return; 713 } 714 sc->sc_dmat = faa->faa_dmat; 715 LIST_INIT(&sc->sc_dmalist); 716 sc->sc_cfg = (void *)of_search_compatible(phandle, compat_data)->data; 717 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 718 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED); 719 720 if (sunxi_i2s_chan_init(sc, &sc->sc_pchan, AUMODE_PLAY, "tx") != 0 || 721 sunxi_i2s_chan_init(sc, &sc->sc_rchan, AUMODE_RECORD, "rx") != 0) { 722 aprint_error(": couldn't setup channels\n"); 723 return; 724 } 725 726 aprint_naive("\n"); 727 aprint_normal(": %s\n", sc->sc_cfg->name); 728 729 /* Reset */ 730 val = I2S_READ(sc, DA_CTL); 731 val &= ~(DA_CTL_TXEN|DA_CTL_RXEN|DA_CTL_GEN); 732 I2S_WRITE(sc, DA_CTL, val); 733 734 val = I2S_READ(sc, DA_FCTL); 735 val &= ~(DA_FCTL_FTX|DA_FCTL_FRX); 736 I2S_WRITE(sc, DA_FCTL, val); 737 738 I2S_WRITE(sc, DA_TXCNT, 0); 739 I2S_WRITE(sc, DA_RXCNT, 0); 740 741 /* Enable */ 742 I2S_WRITE(sc, DA_CTL, DA_CTL_GEN | DA_CTL_SDO_EN); 743 744 /* Setup channels */ 745 I2S_WRITE(sc, sc->sc_cfg->txchmap, 0x76543210); 746 I2S_WRITE(sc, sc->sc_cfg->txchsel, __SHIFTIN(1, DA_CHSEL_SEL) | 747 __SHIFTIN(3, DA_CHSEL_EN)); 748 I2S_WRITE(sc, sc->sc_cfg->rxchmap, 0x76543210); 749 I2S_WRITE(sc, sc->sc_cfg->rxchsel, __SHIFTIN(1, DA_CHSEL_SEL) | 750 __SHIFTIN(3, DA_CHSEL_EN)); 751 752 sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD; 753 sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE; 754 sc->sc_format.validbits = 32; 755 sc->sc_format.precision = 32; 756 sc->sc_format.channels = 2; 757 sc->sc_format.channel_mask = AUFMT_STEREO; 758 sc->sc_format.frequency_type = 1; 759 sc->sc_format.frequency[0] = 48000; 760 761 sc->sc_dai.dai_set_sysclk = sunxi_i2s_dai_set_sysclk; 762 sc->sc_dai.dai_set_format = sunxi_i2s_dai_set_format; 763 sc->sc_dai.dai_hw_if = &sunxi_i2s_hw_if; 764 sc->sc_dai.dai_dev = self; 765 sc->sc_dai.dai_priv = sc; 766 fdtbus_register_dai_controller(self, phandle, &sunxi_i2s_dai_funcs); 767 } 768 769 CFATTACH_DECL_NEW(sunxi_i2s, sizeof(struct sunxi_i2s_softc), 770 sunxi_i2s_match, sunxi_i2s_attach, NULL, NULL); 771