00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #undef VWSND_DEBUG
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 #include <linux/config.h>
00142 #include <linux/module.h>
00143 #include <linux/stddef.h>
00144 #include <asm/fixmap.h>
00145 #include <asm/sgi-cobalt.h>
00146 #include <asm/spinlock.h>
00147
00148 #include "sound_config.h"
00149
00150
00151
00152
00153 #ifdef VWSND_DEBUG
00154
00155 #include <linux/interrupt.h>
00156
00157 static int shut_up = 1;
00158
00159
00160
00161
00162
00163 static void dbgassert(const char *fcn, int line, const char *expr)
00164 {
00165 if (in_interrupt())
00166 panic("ASSERTION FAILED IN INTERRUPT, %s:%s:%d %s\n",
00167 __FILE__, fcn, line, expr);
00168 else {
00169 int x;
00170 printk(KERN_ERR "ASSERTION FAILED, %s:%s:%d %s\n",
00171 __FILE__, fcn, line, expr);
00172 x = * (volatile int *) 0;
00173 }
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 #define ASSERT(e) ((e) ? (void) 0 : dbgassert(__FUNCTION__, __LINE__, #e))
00193 #define DBGDO(x) x
00194 #define DBGX(fmt, args...) (in_interrupt() ? 0 : printk(KERN_ERR fmt, ##args))
00195 #define DBGP(fmt, args...) (DBGX(__FUNCTION__ ": " fmt, ##args))
00196 #define DBGE(fmt, args...) (DBGX(__FUNCTION__ fmt, ##args))
00197 #define DBGC(rtn) (DBGP("calling %s\n", rtn))
00198 #define DBGR() (DBGP("returning\n"))
00199 #define DBGXV(fmt, args...) (shut_up ? 0 : DBGX(fmt, ##args))
00200 #define DBGPV(fmt, args...) (shut_up ? 0 : DBGP(fmt, ##args))
00201 #define DBGEV(fmt, args...) (shut_up ? 0 : DBGE(fmt, ##args))
00202 #define DBGCV(rtn) (shut_up ? 0 : DBGC(rtn))
00203 #define DBGRV() (shut_up ? 0 : DBGR())
00204
00205 #else
00206
00207 #define ASSERT(e) ((void) 0)
00208 #define DBGDO(x)
00209 #define DBGX(fmt, args...) ((void) 0)
00210 #define DBGP(fmt, args...) ((void) 0)
00211 #define DBGE(fmt, args...) ((void) 0)
00212 #define DBGC(rtn) ((void) 0)
00213 #define DBGR() ((void) 0)
00214 #define DBGPV(fmt, args...) ((void) 0)
00215 #define DBGXV(fmt, args...) ((void) 0)
00216 #define DBGEV(fmt, args...) ((void) 0)
00217 #define DBGCV(rtn) ((void) 0)
00218 #define DBGRV() ((void) 0)
00219
00220 #endif
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 enum {
00231 LI_PAGE0_OFFSET = 0x01000 - 0x1000,
00232 LI_PAGE1_OFFSET = 0x0F000 - 0x1000,
00233 LI_PAGE2_OFFSET = 0x10000 - 0x1000,
00234 };
00235
00236
00237
00238 typedef struct lithium {
00239 caddr_t page0;
00240 caddr_t page1;
00241 caddr_t page2;
00242 spinlock_t lock;
00243 } lithium_t;
00244
00245
00246
00247
00248
00249
00250
00251 static int li_create(lithium_t *lith, unsigned long baseaddr)
00252 {
00253 static void li_destroy(lithium_t *);
00254
00255 lith->lock = SPIN_LOCK_UNLOCKED;
00256 lith->page0 = ioremap_nocache(baseaddr + LI_PAGE0_OFFSET, PAGE_SIZE);
00257 lith->page1 = ioremap_nocache(baseaddr + LI_PAGE1_OFFSET, PAGE_SIZE);
00258 lith->page2 = ioremap_nocache(baseaddr + LI_PAGE2_OFFSET, PAGE_SIZE);
00259 if (!lith->page0 || !lith->page1 || !lith->page2) {
00260 li_destroy(lith);
00261 return -ENOMEM;
00262 }
00263 return 0;
00264 }
00265
00266
00267
00268
00269
00270 static void li_destroy(lithium_t *lith)
00271 {
00272 if (lith->page0) {
00273 iounmap(lith->page0);
00274 lith->page0 = NULL;
00275 }
00276 if (lith->page1) {
00277 iounmap(lith->page1);
00278 lith->page1 = NULL;
00279 }
00280 if (lith->page2) {
00281 iounmap(lith->page2);
00282 lith->page2 = NULL;
00283 }
00284 }
00285
00286
00287
00288
00289
00290 static __inline__ unsigned long li_readl(lithium_t *lith, int off)
00291 {
00292 return * (volatile unsigned long *) (lith->page0 + off);
00293 }
00294
00295 static __inline__ unsigned char li_readb(lithium_t *lith, int off)
00296 {
00297 return * (volatile unsigned char *) (lith->page0 + off);
00298 }
00299
00300 static __inline__ void li_writel(lithium_t *lith, int off, unsigned long val)
00301 {
00302 * (volatile unsigned long *) (lith->page0 + off) = val;
00303 }
00304
00305 static __inline__ void li_writeb(lithium_t *lith, int off, unsigned char val)
00306 {
00307 * (volatile unsigned char *) (lith->page0 + off) = val;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 #define LI_HOST_CONTROLLER 0x000
00364 # define LI_HC_RESET 0x00008000
00365 # define LI_HC_LINK_ENABLE 0x00004000
00366 # define LI_HC_LINK_FAILURE 0x00000004
00367 # define LI_HC_LINK_CODEC 0x00000002
00368 # define LI_HC_LINK_READY 0x00000001
00369
00370 #define LI_INTR_STATUS 0x010
00371 #define LI_INTR_MASK 0x014
00372 # define LI_INTR_LINK_ERR 0x00008000
00373 # define LI_INTR_COMM2_TRIG 0x00000008
00374 # define LI_INTR_COMM2_UNDERFLOW 0x00000004
00375 # define LI_INTR_COMM1_TRIG 0x00000002
00376 # define LI_INTR_COMM1_OVERFLOW 0x00000001
00377
00378 #define LI_CODEC_COMMAND 0x018
00379 # define LI_CC_BUSY 0x00008000
00380 # define LI_CC_DIR 0x00000080
00381 # define LI_CC_DIR_RD LI_CC_DIR
00382 # define LI_CC_DIR_WR (!LI_CC_DIR)
00383 # define LI_CC_ADDR_MASK 0x0000007F
00384
00385 #define LI_CODEC_DATA 0x01C
00386
00387 #define LI_COMM1_BASE 0x100
00388 #define LI_COMM1_CTL 0x104
00389 # define LI_CCTL_RESET 0x80000000
00390 # define LI_CCTL_SIZE 0x70000000
00391 # define LI_CCTL_DMA_ENABLE 0x08000000
00392 # define LI_CCTL_TMASK 0x07000000
00393 # define LI_CCTL_TPTR 0x00FF0000
00394 # define LI_CCTL_RPTR 0x0000FF00
00395 # define LI_CCTL_WPTR 0x000000FF
00396 #define LI_COMM1_CFG 0x108
00397 # define LI_CCFG_LOCK 0x00008000
00398 # define LI_CCFG_SLOT 0x00000070
00399 # define LI_CCFG_DIRECTION 0x00000008
00400 # define LI_CCFG_DIR_IN (!LI_CCFG_DIRECTION)
00401 # define LI_CCFG_DIR_OUT LI_CCFG_DIRECTION
00402 # define LI_CCFG_MODE 0x00000004
00403 # define LI_CCFG_MODE_MONO (!LI_CCFG_MODE)
00404 # define LI_CCFG_MODE_STEREO LI_CCFG_MODE
00405 # define LI_CCFG_FORMAT 0x00000003
00406 # define LI_CCFG_FMT_8BIT 0x00000000
00407 # define LI_CCFG_FMT_16BIT 0x00000001
00408 #define LI_COMM2_BASE 0x10C
00409 #define LI_COMM2_CTL 0x110
00410
00411 #define LI_COMM2_CFG 0x114
00412
00413
00414 #define LI_UST_LOW 0x200
00415 #define LI_UST_HIGH 0x204
00416
00417 #define LI_AUDIO1_UST 0x300
00418 #define LI_AUDIO1_MSC 0x304
00419 #define LI_AUDIO2_UST 0x308
00420 #define LI_AUDIO2_MSC 0x30C
00421
00422
00423
00424
00425
00426
00427 #define DMACHUNK_SHIFT 5
00428 #define DMACHUNK_SIZE (1 << DMACHUNK_SHIFT)
00429 #define BYTES_TO_CHUNKS(bytes) ((bytes) >> DMACHUNK_SHIFT)
00430 #define CHUNKS_TO_BYTES(chunks) ((chunks) << DMACHUNK_SHIFT)
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 #define SHIFT_FIELD(val, mask) (((val) * ((mask) & -(mask))) & (mask))
00441 #define UNSHIFT_FIELD(val, mask) (((val) & (mask)) / ((mask) & -(mask)))
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452 typedef struct dma_chan_desc {
00453 int basereg;
00454 int cfgreg;
00455 int ctlreg;
00456 int hwptrreg;
00457 int swptrreg;
00458 int ustreg;
00459 int mscreg;
00460 unsigned long swptrmask;
00461 int ad1843_slot;
00462 int direction;
00463 } dma_chan_desc_t;
00464
00465 static const dma_chan_desc_t li_comm1 = {
00466 LI_COMM1_BASE,
00467 LI_COMM1_CFG,
00468 LI_COMM1_CTL,
00469 LI_COMM1_CTL + 0,
00470 LI_COMM1_CTL + 1,
00471 LI_AUDIO1_UST,
00472 LI_AUDIO1_MSC,
00473 LI_CCTL_RPTR,
00474 2,
00475 LI_CCFG_DIR_IN
00476 };
00477
00478 static const dma_chan_desc_t li_comm2 = {
00479 LI_COMM2_BASE,
00480 LI_COMM2_CFG,
00481 LI_COMM2_CTL,
00482 LI_COMM2_CTL + 1,
00483 LI_COMM2_CTL + 0,
00484 LI_AUDIO2_UST,
00485 LI_AUDIO2_MSC,
00486 LI_CCTL_WPTR,
00487 2,
00488 LI_CCFG_DIR_OUT
00489 };
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 typedef struct dma_chan {
00501 const dma_chan_desc_t *desc;
00502 lithium_t *lith;
00503 unsigned long baseval;
00504 unsigned long cfgval;
00505 unsigned long ctlval;
00506 } dma_chan_t;
00507
00508
00509
00510
00511
00512
00513
00514 typedef struct ustmsc {
00515 unsigned long long ust;
00516 unsigned long msc;
00517 } ustmsc_t;
00518
00519
00520
00521
00522
00523
00524
00525
00526 static int li_ad1843_wait(lithium_t *lith)
00527 {
00528 unsigned long later = jiffies + 2;
00529 while (li_readl(lith, LI_CODEC_COMMAND) & LI_CC_BUSY)
00530 if (jiffies >= later)
00531 return -EBUSY;
00532 return 0;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541 static int li_read_ad1843_reg(lithium_t *lith, int reg)
00542 {
00543 int val;
00544
00545 ASSERT(!in_interrupt());
00546 spin_lock(&lith->lock);
00547 {
00548 val = li_ad1843_wait(lith);
00549 if (val == 0) {
00550 li_writel(lith, LI_CODEC_COMMAND, LI_CC_DIR_RD | reg);
00551 val = li_ad1843_wait(lith);
00552 }
00553 if (val == 0)
00554 val = li_readl(lith, LI_CODEC_DATA);
00555 }
00556 spin_unlock(&lith->lock);
00557
00558 DBGXV("li_read_ad1843_reg(lith=0x%p, reg=%d) returns 0x%04x\n",
00559 lith, reg, val);
00560
00561 return val;
00562 }
00563
00564
00565
00566
00567
00568 static void li_write_ad1843_reg(lithium_t *lith, int reg, int newval)
00569 {
00570 spin_lock(&lith->lock);
00571 {
00572 if (li_ad1843_wait(lith) == 0) {
00573 li_writel(lith, LI_CODEC_DATA, newval);
00574 li_writel(lith, LI_CODEC_COMMAND, LI_CC_DIR_WR | reg);
00575 }
00576 }
00577 spin_unlock(&lith->lock);
00578 }
00579
00580
00581
00582
00583
00584
00585 static void li_setup_dma(dma_chan_t *chan,
00586 const dma_chan_desc_t *desc,
00587 lithium_t *lith,
00588 unsigned long buffer_paddr,
00589 int bufshift,
00590 int fragshift,
00591 int channels,
00592 int sampsize)
00593 {
00594 unsigned long mode, format;
00595 unsigned long size, tmask;
00596
00597 DBGEV("(chan=0x%p, desc=0x%p, lith=0x%p, buffer_paddr=0x%lx, "
00598 "bufshift=%d, fragshift=%d, channels=%d, sampsize=%d)\n",
00599 chan, desc, lith, buffer_paddr,
00600 bufshift, fragshift, channels, sampsize);
00601
00602
00603
00604 li_writel(lith, desc->ctlreg, LI_CCTL_RESET);
00605
00606 ASSERT(channels == 1 || channels == 2);
00607 if (channels == 2)
00608 mode = LI_CCFG_MODE_STEREO;
00609 else
00610 mode = LI_CCFG_MODE_MONO;
00611 ASSERT(sampsize == 1 || sampsize == 2);
00612 if (sampsize == 2)
00613 format = LI_CCFG_FMT_16BIT;
00614 else
00615 format = LI_CCFG_FMT_8BIT;
00616 chan->desc = desc;
00617 chan->lith = lith;
00618
00619
00620
00621
00622
00623
00624
00625 ASSERT(!(buffer_paddr & 0xFF));
00626 chan->baseval = (buffer_paddr >> 8) | 1 << (37 - 8);
00627
00628 chan->cfgval = (!LI_CCFG_LOCK |
00629 SHIFT_FIELD(desc->ad1843_slot, LI_CCFG_SLOT) |
00630 desc->direction |
00631 mode |
00632 format);
00633
00634 size = bufshift - 6;
00635 tmask = 13 - fragshift;
00636 ASSERT(size >= 2 && size <= 7);
00637 ASSERT(tmask >= 1 && tmask <= 7);
00638 chan->ctlval = (!LI_CCTL_RESET |
00639 SHIFT_FIELD(size, LI_CCTL_SIZE) |
00640 !LI_CCTL_DMA_ENABLE |
00641 SHIFT_FIELD(tmask, LI_CCTL_TMASK) |
00642 SHIFT_FIELD(0, LI_CCTL_TPTR));
00643
00644 DBGPV("basereg 0x%x = 0x%lx\n", desc->basereg, chan->baseval);
00645 DBGPV("cfgreg 0x%x = 0x%lx\n", desc->cfgreg, chan->cfgval);
00646 DBGPV("ctlreg 0x%x = 0x%lx\n", desc->ctlreg, chan->ctlval);
00647
00648 li_writel(lith, desc->basereg, chan->baseval);
00649 li_writel(lith, desc->cfgreg, chan->cfgval);
00650 li_writel(lith, desc->ctlreg, chan->ctlval);
00651
00652 DBGRV();
00653 }
00654
00655 static void li_shutdown_dma(dma_chan_t *chan)
00656 {
00657 lithium_t *lith = chan->lith;
00658 caddr_t lith1 = lith->page1;
00659
00660 DBGEV("(chan=0x%p)\n", chan);
00661
00662 chan->ctlval &= ~LI_CCTL_DMA_ENABLE;
00663 DBGPV("ctlreg 0x%x = 0x%lx\n", chan->desc->ctlreg, chan->ctlval);
00664 li_writel(lith, chan->desc->ctlreg, chan->ctlval);
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 if (lith1 && chan->desc->direction == LI_CCFG_DIR_OUT)
00678 * (volatile unsigned long *) (lith1 + 0x500) = 0;
00679 }
00680
00681
00682
00683
00684
00685
00686
00687 static __inline__ void li_activate_dma(dma_chan_t *chan)
00688 {
00689 chan->ctlval |= LI_CCTL_DMA_ENABLE;
00690 DBGPV("ctlval = 0x%lx\n", chan->ctlval);
00691 li_writel(chan->lith, chan->desc->ctlreg, chan->ctlval);
00692 }
00693
00694 static void li_deactivate_dma(dma_chan_t *chan)
00695 {
00696 lithium_t *lith = chan->lith;
00697 caddr_t lith2 = lith->page2;
00698
00699 chan->ctlval &= ~(LI_CCTL_DMA_ENABLE | LI_CCTL_RPTR | LI_CCTL_WPTR);
00700 DBGPV("ctlval = 0x%lx\n", chan->ctlval);
00701 DBGPV("ctlreg 0x%x = 0x%lx\n", chan->desc->ctlreg, chan->ctlval);
00702 li_writel(lith, chan->desc->ctlreg, chan->ctlval);
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714 if (lith2 && chan->desc->direction == LI_CCFG_DIR_OUT) {
00715 * (volatile unsigned long *) (lith2 + 0x98) = 0;
00716 * (volatile unsigned long *) (lith2 + 0x9C) = 0;
00717 }
00718 }
00719
00720
00721
00722
00723
00724
00725 static __inline__ int li_read_swptr(dma_chan_t *chan)
00726 {
00727 const unsigned long mask = chan->desc->swptrmask;
00728
00729 return CHUNKS_TO_BYTES(UNSHIFT_FIELD(chan->ctlval, mask));
00730 }
00731
00732 static __inline__ int li_read_hwptr(dma_chan_t *chan)
00733 {
00734 return CHUNKS_TO_BYTES(li_readb(chan->lith, chan->desc->hwptrreg));
00735 }
00736
00737 static __inline__ void li_write_swptr(dma_chan_t *chan, int val)
00738 {
00739 const unsigned long mask = chan->desc->swptrmask;
00740
00741 ASSERT(!(val & ~CHUNKS_TO_BYTES(0xFF)));
00742 val = BYTES_TO_CHUNKS(val);
00743 chan->ctlval = (chan->ctlval & ~mask) | SHIFT_FIELD(val, mask);
00744 li_writeb(chan->lith, chan->desc->swptrreg, val);
00745 }
00746
00747
00748
00749 static void li_read_USTMSC(dma_chan_t *chan, ustmsc_t *ustmsc)
00750 {
00751 lithium_t *lith = chan->lith;
00752 const dma_chan_desc_t *desc = chan->desc;
00753 unsigned long now_low, now_high0, now_high1, chan_ust;
00754
00755 spin_lock(&lith->lock);
00756 {
00757
00758
00759
00760
00761
00762 do {
00763 now_high0 = li_readl(lith, LI_UST_HIGH);
00764 now_low = li_readl(lith, LI_UST_LOW);
00765
00766
00767
00768
00769
00770
00771
00772 ustmsc->msc = li_readl(lith, desc->mscreg);
00773 chan_ust = li_readl(lith, desc->ustreg);
00774
00775 now_high1 = li_readl(lith, LI_UST_HIGH);
00776 } while (now_high0 != now_high1);
00777 }
00778 spin_unlock(&lith->lock);
00779 ustmsc->ust = ((unsigned long long) now_high0 << 32 | chan_ust);
00780 }
00781
00782 static void li_enable_interrupts(lithium_t *lith, unsigned int mask)
00783 {
00784 DBGEV("(lith=0x%p, mask=0x%x)\n", lith, mask);
00785
00786
00787
00788 li_writel(lith, LI_INTR_STATUS, mask);
00789
00790
00791
00792 mask |= li_readl(lith, LI_INTR_MASK);
00793 li_writel(lith, LI_INTR_MASK, mask);
00794 }
00795
00796 static void li_disable_interrupts(lithium_t *lith, unsigned int mask)
00797 {
00798 unsigned int keepmask;
00799
00800 DBGEV("(lith=0x%p, mask=0x%x)\n", lith, mask);
00801
00802
00803
00804 keepmask = li_readl(lith, LI_INTR_MASK) & ~mask;
00805 li_writel(lith, LI_INTR_MASK, keepmask);
00806
00807
00808
00809 li_writel(lith, LI_INTR_STATUS, mask);
00810 }
00811
00812
00813
00814 static unsigned int li_get_clear_intr_status(lithium_t *lith)
00815 {
00816 unsigned int status;
00817
00818 status = li_readl(lith, LI_INTR_STATUS);
00819 li_writel(lith, LI_INTR_STATUS, ~0);
00820 return status & li_readl(lith, LI_INTR_MASK);
00821 }
00822
00823 static int li_init(lithium_t *lith)
00824 {
00825
00826
00827
00828
00829 li_writel(lith, LI_HOST_CONTROLLER, LI_HC_RESET);
00830 udelay(1);
00831
00832
00833
00834
00835
00836 li_writel(lith, LI_HOST_CONTROLLER, LI_HC_LINK_ENABLE);
00837 udelay(1);
00838
00839 return 0;
00840 }
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854 typedef struct ad1843_bitfield {
00855 char reg;
00856 char lo_bit;
00857 char nbits;
00858 } ad1843_bitfield_t;
00859
00860 static const ad1843_bitfield_t
00861 ad1843_PDNO = { 0, 14, 1 },
00862 ad1843_INIT = { 0, 15, 1 },
00863 ad1843_RIG = { 2, 0, 4 },
00864 ad1843_RMGE = { 2, 4, 1 },
00865 ad1843_RSS = { 2, 5, 3 },
00866 ad1843_LIG = { 2, 8, 4 },
00867 ad1843_LMGE = { 2, 12, 1 },
00868 ad1843_LSS = { 2, 13, 3 },
00869 ad1843_RX1M = { 4, 0, 5 },
00870 ad1843_RX1MM = { 4, 7, 1 },
00871 ad1843_LX1M = { 4, 8, 5 },
00872 ad1843_LX1MM = { 4, 15, 1 },
00873 ad1843_RX2M = { 5, 0, 5 },
00874 ad1843_RX2MM = { 5, 7, 1 },
00875 ad1843_LX2M = { 5, 8, 5 },
00876 ad1843_LX2MM = { 5, 15, 1 },
00877 ad1843_RMCM = { 7, 0, 5 },
00878 ad1843_RMCMM = { 7, 7, 1 },
00879 ad1843_LMCM = { 7, 8, 5 },
00880 ad1843_LMCMM = { 7, 15, 1 },
00881 ad1843_HPOS = { 8, 4, 1 },
00882 ad1843_HPOM = { 8, 5, 1 },
00883 ad1843_RDA1G = { 9, 0, 6 },
00884 ad1843_RDA1GM = { 9, 7, 1 },
00885 ad1843_LDA1G = { 9, 8, 6 },
00886 ad1843_LDA1GM = { 9, 15, 1 },
00887 ad1843_RDA1AM = { 11, 7, 1 },
00888 ad1843_LDA1AM = { 11, 15, 1 },
00889 ad1843_ADLC = { 15, 0, 2 },
00890 ad1843_ADRC = { 15, 2, 2 },
00891 ad1843_DA1C = { 15, 8, 2 },
00892 ad1843_C1C = { 17, 0, 16 },
00893 ad1843_C2C = { 20, 0, 16 },
00894 ad1843_DAADL = { 25, 4, 2 },
00895 ad1843_DAADR = { 25, 6, 2 },
00896 ad1843_DRSFLT = { 25, 15, 1 },
00897 ad1843_ADLF = { 26, 0, 2 },
00898 ad1843_ADRF = { 26, 2, 2 },
00899 ad1843_ADTLK = { 26, 4, 1 },
00900 ad1843_SCF = { 26, 7, 1 },
00901 ad1843_DA1F = { 26, 8, 2 },
00902 ad1843_DA1SM = { 26, 14, 1 },
00903 ad1843_ADLEN = { 27, 0, 1 },
00904 ad1843_ADREN = { 27, 1, 1 },
00905 ad1843_AAMEN = { 27, 4, 1 },
00906 ad1843_ANAEN = { 27, 7, 1 },
00907 ad1843_DA1EN = { 27, 8, 1 },
00908 ad1843_DA2EN = { 27, 9, 1 },
00909 ad1843_C1EN = { 28, 11, 1 },
00910 ad1843_C2EN = { 28, 12, 1 },
00911 ad1843_PDNI = { 28, 15, 1 };
00912
00913
00914
00915
00916
00917
00918
00919 typedef struct ad1843_gain {
00920
00921 int negative;
00922 const ad1843_bitfield_t *lfield;
00923 const ad1843_bitfield_t *rfield;
00924
00925 } ad1843_gain_t;
00926
00927 static const ad1843_gain_t ad1843_gain_RECLEV
00928 = { 0, &ad1843_LIG, &ad1843_RIG };
00929 static const ad1843_gain_t ad1843_gain_LINE
00930 = { 1, &ad1843_LX1M, &ad1843_RX1M };
00931 static const ad1843_gain_t ad1843_gain_CD
00932 = { 1, &ad1843_LX2M, &ad1843_RX2M };
00933 static const ad1843_gain_t ad1843_gain_MIC
00934 = { 1, &ad1843_LMCM, &ad1843_RMCM };
00935 static const ad1843_gain_t ad1843_gain_PCM
00936 = { 1, &ad1843_LDA1G, &ad1843_RDA1G };
00937
00938
00939
00940 static int ad1843_read_bits(lithium_t *lith, const ad1843_bitfield_t *field)
00941 {
00942 int w = li_read_ad1843_reg(lith, field->reg);
00943 int val = w >> field->lo_bit & ((1 << field->nbits) - 1);
00944
00945 DBGXV("ad1843_read_bits(lith=0x%p, field->{%d %d %d}) returns 0x%x\n",
00946 lith, field->reg, field->lo_bit, field->nbits, val);
00947
00948 return val;
00949 }
00950
00951
00952
00953
00954
00955 static int ad1843_write_bits(lithium_t *lith,
00956 const ad1843_bitfield_t *field,
00957 int newval)
00958 {
00959 int w = li_read_ad1843_reg(lith, field->reg);
00960 int mask = ((1 << field->nbits) - 1) << field->lo_bit;
00961 int oldval = (w & mask) >> field->lo_bit;
00962 int newbits = (newval << field->lo_bit) & mask;
00963 w = (w & ~mask) | newbits;
00964 (void) li_write_ad1843_reg(lith, field->reg, w);
00965
00966 DBGXV("ad1843_write_bits(lith=0x%p, field->{%d %d %d}, val=0x%x) "
00967 "returns 0x%x\n",
00968 lith, field->reg, field->lo_bit, field->nbits, newval,
00969 oldval);
00970
00971 return oldval;
00972 }
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987 static void ad1843_read_multi(lithium_t *lith, int argcount, ...)
00988 {
00989 va_list ap;
00990 const ad1843_bitfield_t *fp;
00991 int w = 0, mask, *value, reg = -1;
00992
00993 va_start(ap, argcount);
00994 while (--argcount >= 0) {
00995 fp = va_arg(ap, const ad1843_bitfield_t *);
00996 value = va_arg(ap, int *);
00997 if (reg == -1) {
00998 reg = fp->reg;
00999 w = li_read_ad1843_reg(lith, reg);
01000 }
01001 ASSERT(reg == fp->reg);
01002 mask = (1 << fp->nbits) - 1;
01003 *value = w >> fp->lo_bit & mask;
01004 }
01005 va_end(ap);
01006 }
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019 static void ad1843_write_multi(lithium_t *lith, int argcount, ...)
01020 {
01021 va_list ap;
01022 int reg;
01023 const ad1843_bitfield_t *fp;
01024 int value;
01025 int w, m, mask, bits;
01026
01027 mask = 0;
01028 bits = 0;
01029 reg = -1;
01030
01031 va_start(ap, argcount);
01032 while (--argcount >= 0) {
01033 fp = va_arg(ap, const ad1843_bitfield_t *);
01034 value = va_arg(ap, int);
01035 if (reg == -1)
01036 reg = fp->reg;
01037 ASSERT(fp->reg == reg);
01038 m = ((1 << fp->nbits) - 1) << fp->lo_bit;
01039 mask |= m;
01040 bits |= (value << fp->lo_bit) & m;
01041 }
01042 va_end(ap);
01043 ASSERT(!(bits & ~mask));
01044 if (~mask & 0xFFFF)
01045 w = li_read_ad1843_reg(lith, reg);
01046 else
01047 w = 0;
01048 w = (w & ~mask) | bits;
01049 (void) li_write_ad1843_reg(lith, reg, w);
01050 }
01051
01052
01053
01054
01055
01056
01057 static int ad1843_get_gain(lithium_t *lith, const ad1843_gain_t *gp)
01058 {
01059 int lg, rg;
01060 unsigned short mask = (1 << gp->lfield->nbits) - 1;
01061
01062 ad1843_read_multi(lith, 2, gp->lfield, &lg, gp->rfield, &rg);
01063 if (gp->negative) {
01064 lg = mask - lg;
01065 rg = mask - rg;
01066 }
01067 lg = (lg * 100 + (mask >> 1)) / mask;
01068 rg = (rg * 100 + (mask >> 1)) / mask;
01069 return lg << 0 | rg << 8;
01070 }
01071
01072
01073
01074
01075
01076
01077
01078
01079 static int ad1843_set_gain(lithium_t *lith,
01080 const ad1843_gain_t *gp,
01081 int newval)
01082 {
01083 unsigned short mask = (1 << gp->lfield->nbits) - 1;
01084
01085 int lg = newval >> 0 & 0xFF;
01086 int rg = newval >> 8;
01087 if (lg < 0 || lg > 100 || rg < 0 || rg > 100)
01088 return -EINVAL;
01089 lg = (lg * mask + (mask >> 1)) / 100;
01090 rg = (rg * mask + (mask >> 1)) / 100;
01091 if (gp->negative) {
01092 lg = mask - lg;
01093 rg = mask - rg;
01094 }
01095 ad1843_write_multi(lith, 2, gp->lfield, lg, gp->rfield, rg);
01096 return ad1843_get_gain(lith, gp);
01097 }
01098
01099
01100
01101 static int ad1843_get_recsrc(lithium_t *lith)
01102 {
01103 int ls = ad1843_read_bits(lith, &ad1843_LSS);
01104
01105 switch (ls) {
01106 case 1:
01107 return SOUND_MASK_MIC;
01108 case 2:
01109 return SOUND_MASK_LINE;
01110 case 3:
01111 return SOUND_MASK_CD;
01112 case 6:
01113 return SOUND_MASK_PCM;
01114 default:
01115 ASSERT(0);
01116 return -1;
01117 }
01118 }
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135 static void ad1843_set_resample_mode(lithium_t *lith, int onoff)
01136 {
01137
01138 int save_da1 = li_read_ad1843_reg(lith, 9);
01139
01140
01141 ad1843_write_multi(lith, 4,
01142 &ad1843_DA1EN, 0,
01143 &ad1843_DA2EN, 0,
01144 &ad1843_ADLEN, 0,
01145 &ad1843_ADREN, 0);
01146
01147
01148 ASSERT(onoff == 0 || onoff == 1);
01149 ad1843_write_bits(lith, &ad1843_DRSFLT, onoff);
01150
01151
01152 ad1843_write_multi(lith, 3,
01153 &ad1843_DA1EN, 1,
01154 &ad1843_ADLEN, 1,
01155 &ad1843_ADREN, 1);
01156
01157
01158 li_write_ad1843_reg(lith, 9, save_da1);
01159 }
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171 static int ad1843_set_recsrc(lithium_t *lith, int newsrc)
01172 {
01173 int bits;
01174 int oldbits;
01175
01176 switch (newsrc) {
01177 case SOUND_MASK_PCM:
01178 bits = 6;
01179 break;
01180
01181 case SOUND_MASK_MIC:
01182 bits = 1;
01183 break;
01184
01185 case SOUND_MASK_LINE:
01186 bits = 2;
01187 break;
01188
01189 case SOUND_MASK_CD:
01190 bits = 3;
01191 break;
01192
01193 default:
01194 return -EINVAL;
01195 }
01196 oldbits = ad1843_read_bits(lith, &ad1843_LSS);
01197 if (newsrc == SOUND_MASK_PCM && oldbits != 6) {
01198 DBGP("enabling digital resample mode\n");
01199 ad1843_set_resample_mode(lith, 1);
01200 ad1843_write_multi(lith, 2,
01201 &ad1843_DAADL, 2,
01202 &ad1843_DAADR, 2);
01203 } else if (newsrc != SOUND_MASK_PCM && oldbits == 6) {
01204 DBGP("disabling digital resample mode\n");
01205 ad1843_set_resample_mode(lith, 0);
01206 ad1843_write_multi(lith, 2,
01207 &ad1843_DAADL, 0,
01208 &ad1843_DAADR, 0);
01209 }
01210 ad1843_write_multi(lith, 2, &ad1843_LSS, bits, &ad1843_RSS, bits);
01211 return newsrc;
01212 }
01213
01214
01215
01216
01217
01218 static int ad1843_get_outsrc(lithium_t *lith)
01219 {
01220 int pcm, line, mic, cd;
01221
01222 pcm = ad1843_read_bits(lith, &ad1843_LDA1GM) ? 0 : SOUND_MASK_PCM;
01223 line = ad1843_read_bits(lith, &ad1843_LX1MM) ? 0 : SOUND_MASK_LINE;
01224 cd = ad1843_read_bits(lith, &ad1843_LX2MM) ? 0 : SOUND_MASK_CD;
01225 mic = ad1843_read_bits(lith, &ad1843_LMCMM) ? 0 : SOUND_MASK_MIC;
01226
01227 return pcm | line | cd | mic;
01228 }
01229
01230
01231
01232
01233
01234
01235
01236 static int ad1843_set_outsrc(lithium_t *lith, int mask)
01237 {
01238 int pcm, line, mic, cd;
01239
01240 if (mask & ~(SOUND_MASK_PCM | SOUND_MASK_LINE |
01241 SOUND_MASK_CD | SOUND_MASK_MIC))
01242 return -EINVAL;
01243 pcm = (mask & SOUND_MASK_PCM) ? 0 : 1;
01244 line = (mask & SOUND_MASK_LINE) ? 0 : 1;
01245 mic = (mask & SOUND_MASK_MIC) ? 0 : 1;
01246 cd = (mask & SOUND_MASK_CD) ? 0 : 1;
01247
01248 ad1843_write_multi(lith, 2, &ad1843_LDA1GM, pcm, &ad1843_RDA1GM, pcm);
01249 ad1843_write_multi(lith, 2, &ad1843_LX1MM, line, &ad1843_RX1MM, line);
01250 ad1843_write_multi(lith, 2, &