Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
0004  *
0005  * @File    cthw20k2.c
0006  *
0007  * @Brief
0008  * This file contains the implementation of hardware access method for 20k2.
0009  *
0010  * @Author  Liu Chun
0011  * @Date    May 14 2008
0012  */
0013 
0014 #include <linux/types.h>
0015 #include <linux/slab.h>
0016 #include <linux/pci.h>
0017 #include <linux/io.h>
0018 #include <linux/string.h>
0019 #include <linux/kernel.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/delay.h>
0022 #include "cthw20k2.h"
0023 #include "ct20k2reg.h"
0024 
0025 struct hw20k2 {
0026     struct hw hw;
0027     /* for i2c */
0028     unsigned char dev_id;
0029     unsigned char addr_size;
0030     unsigned char data_size;
0031 
0032     int mic_source;
0033 };
0034 
0035 static u32 hw_read_20kx(struct hw *hw, u32 reg);
0036 static void hw_write_20kx(struct hw *hw, u32 reg, u32 data);
0037 
0038 /*
0039  * Type definition block.
0040  * The layout of control structures can be directly applied on 20k2 chip.
0041  */
0042 
0043 /*
0044  * SRC control block definitions.
0045  */
0046 
0047 /* SRC resource control block */
0048 #define SRCCTL_STATE    0x00000007
0049 #define SRCCTL_BM   0x00000008
0050 #define SRCCTL_RSR  0x00000030
0051 #define SRCCTL_SF   0x000001C0
0052 #define SRCCTL_WR   0x00000200
0053 #define SRCCTL_PM   0x00000400
0054 #define SRCCTL_ROM  0x00001800
0055 #define SRCCTL_VO   0x00002000
0056 #define SRCCTL_ST   0x00004000
0057 #define SRCCTL_IE   0x00008000
0058 #define SRCCTL_ILSZ 0x000F0000
0059 #define SRCCTL_BP   0x00100000
0060 
0061 #define SRCCCR_CISZ 0x000007FF
0062 #define SRCCCR_CWA  0x001FF800
0063 #define SRCCCR_D    0x00200000
0064 #define SRCCCR_RS   0x01C00000
0065 #define SRCCCR_NAL  0x3E000000
0066 #define SRCCCR_RA   0xC0000000
0067 
0068 #define SRCCA_CA    0x0FFFFFFF
0069 #define SRCCA_RS    0xE0000000
0070 
0071 #define SRCSA_SA    0x0FFFFFFF
0072 
0073 #define SRCLA_LA    0x0FFFFFFF
0074 
0075 /* Mixer Parameter Ring ram Low and Hight register.
0076  * Fixed-point value in 8.24 format for parameter channel */
0077 #define MPRLH_PITCH 0xFFFFFFFF
0078 
0079 /* SRC resource register dirty flags */
0080 union src_dirty {
0081     struct {
0082         u16 ctl:1;
0083         u16 ccr:1;
0084         u16 sa:1;
0085         u16 la:1;
0086         u16 ca:1;
0087         u16 mpr:1;
0088         u16 czbfs:1;    /* Clear Z-Buffers */
0089         u16 rsv:9;
0090     } bf;
0091     u16 data;
0092 };
0093 
0094 struct src_rsc_ctrl_blk {
0095     unsigned int    ctl;
0096     unsigned int    ccr;
0097     unsigned int    ca;
0098     unsigned int    sa;
0099     unsigned int    la;
0100     unsigned int    mpr;
0101     union src_dirty dirty;
0102 };
0103 
0104 /* SRC manager control block */
0105 union src_mgr_dirty {
0106     struct {
0107         u16 enb0:1;
0108         u16 enb1:1;
0109         u16 enb2:1;
0110         u16 enb3:1;
0111         u16 enb4:1;
0112         u16 enb5:1;
0113         u16 enb6:1;
0114         u16 enb7:1;
0115         u16 enbsa:1;
0116         u16 rsv:7;
0117     } bf;
0118     u16 data;
0119 };
0120 
0121 struct src_mgr_ctrl_blk {
0122     unsigned int        enbsa;
0123     unsigned int        enb[8];
0124     union src_mgr_dirty dirty;
0125 };
0126 
0127 /* SRCIMP manager control block */
0128 #define SRCAIM_ARC  0x00000FFF
0129 #define SRCAIM_NXT  0x00FF0000
0130 #define SRCAIM_SRC  0xFF000000
0131 
0132 struct srcimap {
0133     unsigned int srcaim;
0134     unsigned int idx;
0135 };
0136 
0137 /* SRCIMP manager register dirty flags */
0138 union srcimp_mgr_dirty {
0139     struct {
0140         u16 srcimap:1;
0141         u16 rsv:15;
0142     } bf;
0143     u16 data;
0144 };
0145 
0146 struct srcimp_mgr_ctrl_blk {
0147     struct srcimap      srcimap;
0148     union srcimp_mgr_dirty  dirty;
0149 };
0150 
0151 /*
0152  * Function implementation block.
0153  */
0154 
0155 static int src_get_rsc_ctrl_blk(void **rblk)
0156 {
0157     struct src_rsc_ctrl_blk *blk;
0158 
0159     *rblk = NULL;
0160     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
0161     if (!blk)
0162         return -ENOMEM;
0163 
0164     *rblk = blk;
0165 
0166     return 0;
0167 }
0168 
0169 static int src_put_rsc_ctrl_blk(void *blk)
0170 {
0171     kfree(blk);
0172 
0173     return 0;
0174 }
0175 
0176 static int src_set_state(void *blk, unsigned int state)
0177 {
0178     struct src_rsc_ctrl_blk *ctl = blk;
0179 
0180     set_field(&ctl->ctl, SRCCTL_STATE, state);
0181     ctl->dirty.bf.ctl = 1;
0182     return 0;
0183 }
0184 
0185 static int src_set_bm(void *blk, unsigned int bm)
0186 {
0187     struct src_rsc_ctrl_blk *ctl = blk;
0188 
0189     set_field(&ctl->ctl, SRCCTL_BM, bm);
0190     ctl->dirty.bf.ctl = 1;
0191     return 0;
0192 }
0193 
0194 static int src_set_rsr(void *blk, unsigned int rsr)
0195 {
0196     struct src_rsc_ctrl_blk *ctl = blk;
0197 
0198     set_field(&ctl->ctl, SRCCTL_RSR, rsr);
0199     ctl->dirty.bf.ctl = 1;
0200     return 0;
0201 }
0202 
0203 static int src_set_sf(void *blk, unsigned int sf)
0204 {
0205     struct src_rsc_ctrl_blk *ctl = blk;
0206 
0207     set_field(&ctl->ctl, SRCCTL_SF, sf);
0208     ctl->dirty.bf.ctl = 1;
0209     return 0;
0210 }
0211 
0212 static int src_set_wr(void *blk, unsigned int wr)
0213 {
0214     struct src_rsc_ctrl_blk *ctl = blk;
0215 
0216     set_field(&ctl->ctl, SRCCTL_WR, wr);
0217     ctl->dirty.bf.ctl = 1;
0218     return 0;
0219 }
0220 
0221 static int src_set_pm(void *blk, unsigned int pm)
0222 {
0223     struct src_rsc_ctrl_blk *ctl = blk;
0224 
0225     set_field(&ctl->ctl, SRCCTL_PM, pm);
0226     ctl->dirty.bf.ctl = 1;
0227     return 0;
0228 }
0229 
0230 static int src_set_rom(void *blk, unsigned int rom)
0231 {
0232     struct src_rsc_ctrl_blk *ctl = blk;
0233 
0234     set_field(&ctl->ctl, SRCCTL_ROM, rom);
0235     ctl->dirty.bf.ctl = 1;
0236     return 0;
0237 }
0238 
0239 static int src_set_vo(void *blk, unsigned int vo)
0240 {
0241     struct src_rsc_ctrl_blk *ctl = blk;
0242 
0243     set_field(&ctl->ctl, SRCCTL_VO, vo);
0244     ctl->dirty.bf.ctl = 1;
0245     return 0;
0246 }
0247 
0248 static int src_set_st(void *blk, unsigned int st)
0249 {
0250     struct src_rsc_ctrl_blk *ctl = blk;
0251 
0252     set_field(&ctl->ctl, SRCCTL_ST, st);
0253     ctl->dirty.bf.ctl = 1;
0254     return 0;
0255 }
0256 
0257 static int src_set_ie(void *blk, unsigned int ie)
0258 {
0259     struct src_rsc_ctrl_blk *ctl = blk;
0260 
0261     set_field(&ctl->ctl, SRCCTL_IE, ie);
0262     ctl->dirty.bf.ctl = 1;
0263     return 0;
0264 }
0265 
0266 static int src_set_ilsz(void *blk, unsigned int ilsz)
0267 {
0268     struct src_rsc_ctrl_blk *ctl = blk;
0269 
0270     set_field(&ctl->ctl, SRCCTL_ILSZ, ilsz);
0271     ctl->dirty.bf.ctl = 1;
0272     return 0;
0273 }
0274 
0275 static int src_set_bp(void *blk, unsigned int bp)
0276 {
0277     struct src_rsc_ctrl_blk *ctl = blk;
0278 
0279     set_field(&ctl->ctl, SRCCTL_BP, bp);
0280     ctl->dirty.bf.ctl = 1;
0281     return 0;
0282 }
0283 
0284 static int src_set_cisz(void *blk, unsigned int cisz)
0285 {
0286     struct src_rsc_ctrl_blk *ctl = blk;
0287 
0288     set_field(&ctl->ccr, SRCCCR_CISZ, cisz);
0289     ctl->dirty.bf.ccr = 1;
0290     return 0;
0291 }
0292 
0293 static int src_set_ca(void *blk, unsigned int ca)
0294 {
0295     struct src_rsc_ctrl_blk *ctl = blk;
0296 
0297     set_field(&ctl->ca, SRCCA_CA, ca);
0298     ctl->dirty.bf.ca = 1;
0299     return 0;
0300 }
0301 
0302 static int src_set_sa(void *blk, unsigned int sa)
0303 {
0304     struct src_rsc_ctrl_blk *ctl = blk;
0305 
0306     set_field(&ctl->sa, SRCSA_SA, sa);
0307     ctl->dirty.bf.sa = 1;
0308     return 0;
0309 }
0310 
0311 static int src_set_la(void *blk, unsigned int la)
0312 {
0313     struct src_rsc_ctrl_blk *ctl = blk;
0314 
0315     set_field(&ctl->la, SRCLA_LA, la);
0316     ctl->dirty.bf.la = 1;
0317     return 0;
0318 }
0319 
0320 static int src_set_pitch(void *blk, unsigned int pitch)
0321 {
0322     struct src_rsc_ctrl_blk *ctl = blk;
0323 
0324     set_field(&ctl->mpr, MPRLH_PITCH, pitch);
0325     ctl->dirty.bf.mpr = 1;
0326     return 0;
0327 }
0328 
0329 static int src_set_clear_zbufs(void *blk, unsigned int clear)
0330 {
0331     ((struct src_rsc_ctrl_blk *)blk)->dirty.bf.czbfs = (clear ? 1 : 0);
0332     return 0;
0333 }
0334 
0335 static int src_set_dirty(void *blk, unsigned int flags)
0336 {
0337     ((struct src_rsc_ctrl_blk *)blk)->dirty.data = (flags & 0xffff);
0338     return 0;
0339 }
0340 
0341 static int src_set_dirty_all(void *blk)
0342 {
0343     ((struct src_rsc_ctrl_blk *)blk)->dirty.data = ~(0x0);
0344     return 0;
0345 }
0346 
0347 #define AR_SLOT_SIZE        4096
0348 #define AR_SLOT_BLOCK_SIZE  16
0349 #define AR_PTS_PITCH        6
0350 #define AR_PARAM_SRC_OFFSET 0x60
0351 
0352 static unsigned int src_param_pitch_mixer(unsigned int src_idx)
0353 {
0354     return ((src_idx << 4) + AR_PTS_PITCH + AR_SLOT_SIZE
0355             - AR_PARAM_SRC_OFFSET) % AR_SLOT_SIZE;
0356 
0357 }
0358 
0359 static int src_commit_write(struct hw *hw, unsigned int idx, void *blk)
0360 {
0361     struct src_rsc_ctrl_blk *ctl = blk;
0362     int i;
0363 
0364     if (ctl->dirty.bf.czbfs) {
0365         /* Clear Z-Buffer registers */
0366         for (i = 0; i < 8; i++)
0367             hw_write_20kx(hw, SRC_UPZ+idx*0x100+i*0x4, 0);
0368 
0369         for (i = 0; i < 4; i++)
0370             hw_write_20kx(hw, SRC_DN0Z+idx*0x100+i*0x4, 0);
0371 
0372         for (i = 0; i < 8; i++)
0373             hw_write_20kx(hw, SRC_DN1Z+idx*0x100+i*0x4, 0);
0374 
0375         ctl->dirty.bf.czbfs = 0;
0376     }
0377     if (ctl->dirty.bf.mpr) {
0378         /* Take the parameter mixer resource in the same group as that
0379          * the idx src is in for simplicity. Unlike src, all conjugate
0380          * parameter mixer resources must be programmed for
0381          * corresponding conjugate src resources. */
0382         unsigned int pm_idx = src_param_pitch_mixer(idx);
0383         hw_write_20kx(hw, MIXER_PRING_LO_HI+4*pm_idx, ctl->mpr);
0384         hw_write_20kx(hw, MIXER_PMOPLO+8*pm_idx, 0x3);
0385         hw_write_20kx(hw, MIXER_PMOPHI+8*pm_idx, 0x0);
0386         ctl->dirty.bf.mpr = 0;
0387     }
0388     if (ctl->dirty.bf.sa) {
0389         hw_write_20kx(hw, SRC_SA+idx*0x100, ctl->sa);
0390         ctl->dirty.bf.sa = 0;
0391     }
0392     if (ctl->dirty.bf.la) {
0393         hw_write_20kx(hw, SRC_LA+idx*0x100, ctl->la);
0394         ctl->dirty.bf.la = 0;
0395     }
0396     if (ctl->dirty.bf.ca) {
0397         hw_write_20kx(hw, SRC_CA+idx*0x100, ctl->ca);
0398         ctl->dirty.bf.ca = 0;
0399     }
0400 
0401     /* Write srccf register */
0402     hw_write_20kx(hw, SRC_CF+idx*0x100, 0x0);
0403 
0404     if (ctl->dirty.bf.ccr) {
0405         hw_write_20kx(hw, SRC_CCR+idx*0x100, ctl->ccr);
0406         ctl->dirty.bf.ccr = 0;
0407     }
0408     if (ctl->dirty.bf.ctl) {
0409         hw_write_20kx(hw, SRC_CTL+idx*0x100, ctl->ctl);
0410         ctl->dirty.bf.ctl = 0;
0411     }
0412 
0413     return 0;
0414 }
0415 
0416 static int src_get_ca(struct hw *hw, unsigned int idx, void *blk)
0417 {
0418     struct src_rsc_ctrl_blk *ctl = blk;
0419 
0420     ctl->ca = hw_read_20kx(hw, SRC_CA+idx*0x100);
0421     ctl->dirty.bf.ca = 0;
0422 
0423     return get_field(ctl->ca, SRCCA_CA);
0424 }
0425 
0426 static unsigned int src_get_dirty(void *blk)
0427 {
0428     return ((struct src_rsc_ctrl_blk *)blk)->dirty.data;
0429 }
0430 
0431 static unsigned int src_dirty_conj_mask(void)
0432 {
0433     return 0x20;
0434 }
0435 
0436 static int src_mgr_enbs_src(void *blk, unsigned int idx)
0437 {
0438     ((struct src_mgr_ctrl_blk *)blk)->enbsa |= (0x1 << ((idx%128)/4));
0439     ((struct src_mgr_ctrl_blk *)blk)->dirty.bf.enbsa = 1;
0440     ((struct src_mgr_ctrl_blk *)blk)->enb[idx/32] |= (0x1 << (idx%32));
0441     return 0;
0442 }
0443 
0444 static int src_mgr_enb_src(void *blk, unsigned int idx)
0445 {
0446     ((struct src_mgr_ctrl_blk *)blk)->enb[idx/32] |= (0x1 << (idx%32));
0447     ((struct src_mgr_ctrl_blk *)blk)->dirty.data |= (0x1 << (idx/32));
0448     return 0;
0449 }
0450 
0451 static int src_mgr_dsb_src(void *blk, unsigned int idx)
0452 {
0453     ((struct src_mgr_ctrl_blk *)blk)->enb[idx/32] &= ~(0x1 << (idx%32));
0454     ((struct src_mgr_ctrl_blk *)blk)->dirty.data |= (0x1 << (idx/32));
0455     return 0;
0456 }
0457 
0458 static int src_mgr_commit_write(struct hw *hw, void *blk)
0459 {
0460     struct src_mgr_ctrl_blk *ctl = blk;
0461     int i;
0462     unsigned int ret;
0463 
0464     if (ctl->dirty.bf.enbsa) {
0465         do {
0466             ret = hw_read_20kx(hw, SRC_ENBSTAT);
0467         } while (ret & 0x1);
0468         hw_write_20kx(hw, SRC_ENBSA, ctl->enbsa);
0469         ctl->dirty.bf.enbsa = 0;
0470     }
0471     for (i = 0; i < 8; i++) {
0472         if ((ctl->dirty.data & (0x1 << i))) {
0473             hw_write_20kx(hw, SRC_ENB+(i*0x100), ctl->enb[i]);
0474             ctl->dirty.data &= ~(0x1 << i);
0475         }
0476     }
0477 
0478     return 0;
0479 }
0480 
0481 static int src_mgr_get_ctrl_blk(void **rblk)
0482 {
0483     struct src_mgr_ctrl_blk *blk;
0484 
0485     *rblk = NULL;
0486     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
0487     if (!blk)
0488         return -ENOMEM;
0489 
0490     *rblk = blk;
0491 
0492     return 0;
0493 }
0494 
0495 static int src_mgr_put_ctrl_blk(void *blk)
0496 {
0497     kfree(blk);
0498 
0499     return 0;
0500 }
0501 
0502 static int srcimp_mgr_get_ctrl_blk(void **rblk)
0503 {
0504     struct srcimp_mgr_ctrl_blk *blk;
0505 
0506     *rblk = NULL;
0507     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
0508     if (!blk)
0509         return -ENOMEM;
0510 
0511     *rblk = blk;
0512 
0513     return 0;
0514 }
0515 
0516 static int srcimp_mgr_put_ctrl_blk(void *blk)
0517 {
0518     kfree(blk);
0519 
0520     return 0;
0521 }
0522 
0523 static int srcimp_mgr_set_imaparc(void *blk, unsigned int slot)
0524 {
0525     struct srcimp_mgr_ctrl_blk *ctl = blk;
0526 
0527     set_field(&ctl->srcimap.srcaim, SRCAIM_ARC, slot);
0528     ctl->dirty.bf.srcimap = 1;
0529     return 0;
0530 }
0531 
0532 static int srcimp_mgr_set_imapuser(void *blk, unsigned int user)
0533 {
0534     struct srcimp_mgr_ctrl_blk *ctl = blk;
0535 
0536     set_field(&ctl->srcimap.srcaim, SRCAIM_SRC, user);
0537     ctl->dirty.bf.srcimap = 1;
0538     return 0;
0539 }
0540 
0541 static int srcimp_mgr_set_imapnxt(void *blk, unsigned int next)
0542 {
0543     struct srcimp_mgr_ctrl_blk *ctl = blk;
0544 
0545     set_field(&ctl->srcimap.srcaim, SRCAIM_NXT, next);
0546     ctl->dirty.bf.srcimap = 1;
0547     return 0;
0548 }
0549 
0550 static int srcimp_mgr_set_imapaddr(void *blk, unsigned int addr)
0551 {
0552     ((struct srcimp_mgr_ctrl_blk *)blk)->srcimap.idx = addr;
0553     ((struct srcimp_mgr_ctrl_blk *)blk)->dirty.bf.srcimap = 1;
0554     return 0;
0555 }
0556 
0557 static int srcimp_mgr_commit_write(struct hw *hw, void *blk)
0558 {
0559     struct srcimp_mgr_ctrl_blk *ctl = blk;
0560 
0561     if (ctl->dirty.bf.srcimap) {
0562         hw_write_20kx(hw, SRC_IMAP+ctl->srcimap.idx*0x100,
0563                         ctl->srcimap.srcaim);
0564         ctl->dirty.bf.srcimap = 0;
0565     }
0566 
0567     return 0;
0568 }
0569 
0570 /*
0571  * AMIXER control block definitions.
0572  */
0573 
0574 #define AMOPLO_M    0x00000003
0575 #define AMOPLO_IV   0x00000004
0576 #define AMOPLO_X    0x0003FFF0
0577 #define AMOPLO_Y    0xFFFC0000
0578 
0579 #define AMOPHI_SADR 0x000000FF
0580 #define AMOPHI_SE   0x80000000
0581 
0582 /* AMIXER resource register dirty flags */
0583 union amixer_dirty {
0584     struct {
0585         u16 amoplo:1;
0586         u16 amophi:1;
0587         u16 rsv:14;
0588     } bf;
0589     u16 data;
0590 };
0591 
0592 /* AMIXER resource control block */
0593 struct amixer_rsc_ctrl_blk {
0594     unsigned int        amoplo;
0595     unsigned int        amophi;
0596     union amixer_dirty  dirty;
0597 };
0598 
0599 static int amixer_set_mode(void *blk, unsigned int mode)
0600 {
0601     struct amixer_rsc_ctrl_blk *ctl = blk;
0602 
0603     set_field(&ctl->amoplo, AMOPLO_M, mode);
0604     ctl->dirty.bf.amoplo = 1;
0605     return 0;
0606 }
0607 
0608 static int amixer_set_iv(void *blk, unsigned int iv)
0609 {
0610     struct amixer_rsc_ctrl_blk *ctl = blk;
0611 
0612     set_field(&ctl->amoplo, AMOPLO_IV, iv);
0613     ctl->dirty.bf.amoplo = 1;
0614     return 0;
0615 }
0616 
0617 static int amixer_set_x(void *blk, unsigned int x)
0618 {
0619     struct amixer_rsc_ctrl_blk *ctl = blk;
0620 
0621     set_field(&ctl->amoplo, AMOPLO_X, x);
0622     ctl->dirty.bf.amoplo = 1;
0623     return 0;
0624 }
0625 
0626 static int amixer_set_y(void *blk, unsigned int y)
0627 {
0628     struct amixer_rsc_ctrl_blk *ctl = blk;
0629 
0630     set_field(&ctl->amoplo, AMOPLO_Y, y);
0631     ctl->dirty.bf.amoplo = 1;
0632     return 0;
0633 }
0634 
0635 static int amixer_set_sadr(void *blk, unsigned int sadr)
0636 {
0637     struct amixer_rsc_ctrl_blk *ctl = blk;
0638 
0639     set_field(&ctl->amophi, AMOPHI_SADR, sadr);
0640     ctl->dirty.bf.amophi = 1;
0641     return 0;
0642 }
0643 
0644 static int amixer_set_se(void *blk, unsigned int se)
0645 {
0646     struct amixer_rsc_ctrl_blk *ctl = blk;
0647 
0648     set_field(&ctl->amophi, AMOPHI_SE, se);
0649     ctl->dirty.bf.amophi = 1;
0650     return 0;
0651 }
0652 
0653 static int amixer_set_dirty(void *blk, unsigned int flags)
0654 {
0655     ((struct amixer_rsc_ctrl_blk *)blk)->dirty.data = (flags & 0xffff);
0656     return 0;
0657 }
0658 
0659 static int amixer_set_dirty_all(void *blk)
0660 {
0661     ((struct amixer_rsc_ctrl_blk *)blk)->dirty.data = ~(0x0);
0662     return 0;
0663 }
0664 
0665 static int amixer_commit_write(struct hw *hw, unsigned int idx, void *blk)
0666 {
0667     struct amixer_rsc_ctrl_blk *ctl = blk;
0668 
0669     if (ctl->dirty.bf.amoplo || ctl->dirty.bf.amophi) {
0670         hw_write_20kx(hw, MIXER_AMOPLO+idx*8, ctl->amoplo);
0671         ctl->dirty.bf.amoplo = 0;
0672         hw_write_20kx(hw, MIXER_AMOPHI+idx*8, ctl->amophi);
0673         ctl->dirty.bf.amophi = 0;
0674     }
0675 
0676     return 0;
0677 }
0678 
0679 static int amixer_get_y(void *blk)
0680 {
0681     struct amixer_rsc_ctrl_blk *ctl = blk;
0682 
0683     return get_field(ctl->amoplo, AMOPLO_Y);
0684 }
0685 
0686 static unsigned int amixer_get_dirty(void *blk)
0687 {
0688     return ((struct amixer_rsc_ctrl_blk *)blk)->dirty.data;
0689 }
0690 
0691 static int amixer_rsc_get_ctrl_blk(void **rblk)
0692 {
0693     struct amixer_rsc_ctrl_blk *blk;
0694 
0695     *rblk = NULL;
0696     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
0697     if (!blk)
0698         return -ENOMEM;
0699 
0700     *rblk = blk;
0701 
0702     return 0;
0703 }
0704 
0705 static int amixer_rsc_put_ctrl_blk(void *blk)
0706 {
0707     kfree(blk);
0708 
0709     return 0;
0710 }
0711 
0712 static int amixer_mgr_get_ctrl_blk(void **rblk)
0713 {
0714     *rblk = NULL;
0715 
0716     return 0;
0717 }
0718 
0719 static int amixer_mgr_put_ctrl_blk(void *blk)
0720 {
0721     return 0;
0722 }
0723 
0724 /*
0725  * DAIO control block definitions.
0726  */
0727 
0728 /* Receiver Sample Rate Tracker Control register */
0729 #define SRTCTL_SRCO 0x000000FF
0730 #define SRTCTL_SRCM 0x0000FF00
0731 #define SRTCTL_RSR  0x00030000
0732 #define SRTCTL_DRAT 0x00300000
0733 #define SRTCTL_EC   0x01000000
0734 #define SRTCTL_ET   0x10000000
0735 
0736 /* DAIO Receiver register dirty flags */
0737 union dai_dirty {
0738     struct {
0739         u16 srt:1;
0740         u16 rsv:15;
0741     } bf;
0742     u16 data;
0743 };
0744 
0745 /* DAIO Receiver control block */
0746 struct dai_ctrl_blk {
0747     unsigned int    srt;
0748     union dai_dirty dirty;
0749 };
0750 
0751 /* Audio Input Mapper RAM */
0752 #define AIM_ARC     0x00000FFF
0753 #define AIM_NXT     0x007F0000
0754 
0755 struct daoimap {
0756     unsigned int aim;
0757     unsigned int idx;
0758 };
0759 
0760 /* Audio Transmitter Control and Status register */
0761 #define ATXCTL_EN   0x00000001
0762 #define ATXCTL_MODE 0x00000010
0763 #define ATXCTL_CD   0x00000020
0764 #define ATXCTL_RAW  0x00000100
0765 #define ATXCTL_MT   0x00000200
0766 #define ATXCTL_NUC  0x00003000
0767 #define ATXCTL_BEN  0x00010000
0768 #define ATXCTL_BMUX 0x00700000
0769 #define ATXCTL_B24  0x01000000
0770 #define ATXCTL_CPF  0x02000000
0771 #define ATXCTL_RIV  0x10000000
0772 #define ATXCTL_LIV  0x20000000
0773 #define ATXCTL_RSAT 0x40000000
0774 #define ATXCTL_LSAT 0x80000000
0775 
0776 /* XDIF Transmitter register dirty flags */
0777 union dao_dirty {
0778     struct {
0779         u16 atxcsl:1;
0780         u16 rsv:15;
0781     } bf;
0782     u16 data;
0783 };
0784 
0785 /* XDIF Transmitter control block */
0786 struct dao_ctrl_blk {
0787     /* XDIF Transmitter Channel Status Low Register */
0788     unsigned int    atxcsl;
0789     union dao_dirty dirty;
0790 };
0791 
0792 /* Audio Receiver Control register */
0793 #define ARXCTL_EN   0x00000001
0794 
0795 /* DAIO manager register dirty flags */
0796 union daio_mgr_dirty {
0797     struct {
0798         u32 atxctl:8;
0799         u32 arxctl:8;
0800         u32 daoimap:1;
0801         u32 rsv:15;
0802     } bf;
0803     u32 data;
0804 };
0805 
0806 /* DAIO manager control block */
0807 struct daio_mgr_ctrl_blk {
0808     struct daoimap      daoimap;
0809     unsigned int        txctl[8];
0810     unsigned int        rxctl[8];
0811     union daio_mgr_dirty    dirty;
0812 };
0813 
0814 static int dai_srt_set_srco(void *blk, unsigned int src)
0815 {
0816     struct dai_ctrl_blk *ctl = blk;
0817 
0818     set_field(&ctl->srt, SRTCTL_SRCO, src);
0819     ctl->dirty.bf.srt = 1;
0820     return 0;
0821 }
0822 
0823 static int dai_srt_set_srcm(void *blk, unsigned int src)
0824 {
0825     struct dai_ctrl_blk *ctl = blk;
0826 
0827     set_field(&ctl->srt, SRTCTL_SRCM, src);
0828     ctl->dirty.bf.srt = 1;
0829     return 0;
0830 }
0831 
0832 static int dai_srt_set_rsr(void *blk, unsigned int rsr)
0833 {
0834     struct dai_ctrl_blk *ctl = blk;
0835 
0836     set_field(&ctl->srt, SRTCTL_RSR, rsr);
0837     ctl->dirty.bf.srt = 1;
0838     return 0;
0839 }
0840 
0841 static int dai_srt_set_drat(void *blk, unsigned int drat)
0842 {
0843     struct dai_ctrl_blk *ctl = blk;
0844 
0845     set_field(&ctl->srt, SRTCTL_DRAT, drat);
0846     ctl->dirty.bf.srt = 1;
0847     return 0;
0848 }
0849 
0850 static int dai_srt_set_ec(void *blk, unsigned int ec)
0851 {
0852     struct dai_ctrl_blk *ctl = blk;
0853 
0854     set_field(&ctl->srt, SRTCTL_EC, ec ? 1 : 0);
0855     ctl->dirty.bf.srt = 1;
0856     return 0;
0857 }
0858 
0859 static int dai_srt_set_et(void *blk, unsigned int et)
0860 {
0861     struct dai_ctrl_blk *ctl = blk;
0862 
0863     set_field(&ctl->srt, SRTCTL_ET, et ? 1 : 0);
0864     ctl->dirty.bf.srt = 1;
0865     return 0;
0866 }
0867 
0868 static int dai_commit_write(struct hw *hw, unsigned int idx, void *blk)
0869 {
0870     struct dai_ctrl_blk *ctl = blk;
0871 
0872     if (ctl->dirty.bf.srt) {
0873         hw_write_20kx(hw, AUDIO_IO_RX_SRT_CTL+0x40*idx, ctl->srt);
0874         ctl->dirty.bf.srt = 0;
0875     }
0876 
0877     return 0;
0878 }
0879 
0880 static int dai_get_ctrl_blk(void **rblk)
0881 {
0882     struct dai_ctrl_blk *blk;
0883 
0884     *rblk = NULL;
0885     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
0886     if (!blk)
0887         return -ENOMEM;
0888 
0889     *rblk = blk;
0890 
0891     return 0;
0892 }
0893 
0894 static int dai_put_ctrl_blk(void *blk)
0895 {
0896     kfree(blk);
0897 
0898     return 0;
0899 }
0900 
0901 static int dao_set_spos(void *blk, unsigned int spos)
0902 {
0903     ((struct dao_ctrl_blk *)blk)->atxcsl = spos;
0904     ((struct dao_ctrl_blk *)blk)->dirty.bf.atxcsl = 1;
0905     return 0;
0906 }
0907 
0908 static int dao_commit_write(struct hw *hw, unsigned int idx, void *blk)
0909 {
0910     struct dao_ctrl_blk *ctl = blk;
0911 
0912     if (ctl->dirty.bf.atxcsl) {
0913         if (idx < 4) {
0914             /* S/PDIF SPOSx */
0915             hw_write_20kx(hw, AUDIO_IO_TX_CSTAT_L+0x40*idx,
0916                             ctl->atxcsl);
0917         }
0918         ctl->dirty.bf.atxcsl = 0;
0919     }
0920 
0921     return 0;
0922 }
0923 
0924 static int dao_get_spos(void *blk, unsigned int *spos)
0925 {
0926     *spos = ((struct dao_ctrl_blk *)blk)->atxcsl;
0927     return 0;
0928 }
0929 
0930 static int dao_get_ctrl_blk(void **rblk)
0931 {
0932     struct dao_ctrl_blk *blk;
0933 
0934     *rblk = NULL;
0935     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
0936     if (!blk)
0937         return -ENOMEM;
0938 
0939     *rblk = blk;
0940 
0941     return 0;
0942 }
0943 
0944 static int dao_put_ctrl_blk(void *blk)
0945 {
0946     kfree(blk);
0947 
0948     return 0;
0949 }
0950 
0951 static int daio_mgr_enb_dai(void *blk, unsigned int idx)
0952 {
0953     struct daio_mgr_ctrl_blk *ctl = blk;
0954 
0955     set_field(&ctl->rxctl[idx], ARXCTL_EN, 1);
0956     ctl->dirty.bf.arxctl |= (0x1 << idx);
0957     return 0;
0958 }
0959 
0960 static int daio_mgr_dsb_dai(void *blk, unsigned int idx)
0961 {
0962     struct daio_mgr_ctrl_blk *ctl = blk;
0963 
0964     set_field(&ctl->rxctl[idx], ARXCTL_EN, 0);
0965 
0966     ctl->dirty.bf.arxctl |= (0x1 << idx);
0967     return 0;
0968 }
0969 
0970 static int daio_mgr_enb_dao(void *blk, unsigned int idx)
0971 {
0972     struct daio_mgr_ctrl_blk *ctl = blk;
0973 
0974     set_field(&ctl->txctl[idx], ATXCTL_EN, 1);
0975     ctl->dirty.bf.atxctl |= (0x1 << idx);
0976     return 0;
0977 }
0978 
0979 static int daio_mgr_dsb_dao(void *blk, unsigned int idx)
0980 {
0981     struct daio_mgr_ctrl_blk *ctl = blk;
0982 
0983     set_field(&ctl->txctl[idx], ATXCTL_EN, 0);
0984     ctl->dirty.bf.atxctl |= (0x1 << idx);
0985     return 0;
0986 }
0987 
0988 static int daio_mgr_dao_init(void *blk, unsigned int idx, unsigned int conf)
0989 {
0990     struct daio_mgr_ctrl_blk *ctl = blk;
0991 
0992     if (idx < 4) {
0993         /* S/PDIF output */
0994         switch ((conf & 0xf)) {
0995         case 1:
0996             set_field(&ctl->txctl[idx], ATXCTL_NUC, 0);
0997             break;
0998         case 2:
0999             set_field(&ctl->txctl[idx], ATXCTL_NUC, 1);
1000             break;
1001         case 4:
1002             set_field(&ctl->txctl[idx], ATXCTL_NUC, 2);
1003             break;
1004         case 8:
1005             set_field(&ctl->txctl[idx], ATXCTL_NUC, 3);
1006             break;
1007         default:
1008             break;
1009         }
1010         /* CDIF */
1011         set_field(&ctl->txctl[idx], ATXCTL_CD, (!(conf & 0x7)));
1012         /* Non-audio */
1013         set_field(&ctl->txctl[idx], ATXCTL_LIV, (conf >> 4) & 0x1);
1014         /* Non-audio */
1015         set_field(&ctl->txctl[idx], ATXCTL_RIV, (conf >> 4) & 0x1);
1016         set_field(&ctl->txctl[idx], ATXCTL_RAW,
1017               ((conf >> 3) & 0x1) ? 0 : 0);
1018         ctl->dirty.bf.atxctl |= (0x1 << idx);
1019     } else {
1020         /* I2S output */
1021         /*idx %= 4; */
1022     }
1023     return 0;
1024 }
1025 
1026 static int daio_mgr_set_imaparc(void *blk, unsigned int slot)
1027 {
1028     struct daio_mgr_ctrl_blk *ctl = blk;
1029 
1030     set_field(&ctl->daoimap.aim, AIM_ARC, slot);
1031     ctl->dirty.bf.daoimap = 1;
1032     return 0;
1033 }
1034 
1035 static int daio_mgr_set_imapnxt(void *blk, unsigned int next)
1036 {
1037     struct daio_mgr_ctrl_blk *ctl = blk;
1038 
1039     set_field(&ctl->daoimap.aim, AIM_NXT, next);
1040     ctl->dirty.bf.daoimap = 1;
1041     return 0;
1042 }
1043 
1044 static int daio_mgr_set_imapaddr(void *blk, unsigned int addr)
1045 {
1046     ((struct daio_mgr_ctrl_blk *)blk)->daoimap.idx = addr;
1047     ((struct daio_mgr_ctrl_blk *)blk)->dirty.bf.daoimap = 1;
1048     return 0;
1049 }
1050 
1051 static int daio_mgr_commit_write(struct hw *hw, void *blk)
1052 {
1053     struct daio_mgr_ctrl_blk *ctl = blk;
1054     unsigned int data;
1055     int i;
1056 
1057     for (i = 0; i < 8; i++) {
1058         if ((ctl->dirty.bf.atxctl & (0x1 << i))) {
1059             data = ctl->txctl[i];
1060             hw_write_20kx(hw, (AUDIO_IO_TX_CTL+(0x40*i)), data);
1061             ctl->dirty.bf.atxctl &= ~(0x1 << i);
1062             mdelay(1);
1063         }
1064         if ((ctl->dirty.bf.arxctl & (0x1 << i))) {
1065             data = ctl->rxctl[i];
1066             hw_write_20kx(hw, (AUDIO_IO_RX_CTL+(0x40*i)), data);
1067             ctl->dirty.bf.arxctl &= ~(0x1 << i);
1068             mdelay(1);
1069         }
1070     }
1071     if (ctl->dirty.bf.daoimap) {
1072         hw_write_20kx(hw, AUDIO_IO_AIM+ctl->daoimap.idx*4,
1073                         ctl->daoimap.aim);
1074         ctl->dirty.bf.daoimap = 0;
1075     }
1076 
1077     return 0;
1078 }
1079 
1080 static int daio_mgr_get_ctrl_blk(struct hw *hw, void **rblk)
1081 {
1082     struct daio_mgr_ctrl_blk *blk;
1083     int i;
1084 
1085     *rblk = NULL;
1086     blk = kzalloc(sizeof(*blk), GFP_KERNEL);
1087     if (!blk)
1088         return -ENOMEM;
1089 
1090     for (i = 0; i < 8; i++) {
1091         blk->txctl[i] = hw_read_20kx(hw, AUDIO_IO_TX_CTL+(0x40*i));
1092         blk->rxctl[i] = hw_read_20kx(hw, AUDIO_IO_RX_CTL+(0x40*i));
1093     }
1094 
1095     *rblk = blk;
1096 
1097     return 0;
1098 }
1099 
1100 static int daio_mgr_put_ctrl_blk(void *blk)
1101 {
1102     kfree(blk);
1103 
1104     return 0;
1105 }
1106 
1107 /* Timer interrupt */
1108 static int set_timer_irq(struct hw *hw, int enable)
1109 {
1110     hw_write_20kx(hw, GIE, enable ? IT_INT : 0);
1111     return 0;
1112 }
1113 
1114 static int set_timer_tick(struct hw *hw, unsigned int ticks)
1115 {
1116     if (ticks)
1117         ticks |= TIMR_IE | TIMR_IP;
1118     hw_write_20kx(hw, TIMR, ticks);
1119     return 0;
1120 }
1121 
1122 static unsigned int get_wc(struct hw *hw)
1123 {
1124     return hw_read_20kx(hw, WC);
1125 }
1126 
1127 /* Card hardware initialization block */
1128 struct dac_conf {
1129     unsigned int msr; /* master sample rate in rsrs */
1130 };
1131 
1132 struct adc_conf {
1133     unsigned int msr;   /* master sample rate in rsrs */
1134     unsigned char input;    /* the input source of ADC */
1135     unsigned char mic20db;  /* boost mic by 20db if input is microphone */
1136 };
1137 
1138 struct daio_conf {
1139     unsigned int msr; /* master sample rate in rsrs */
1140 };
1141 
1142 struct trn_conf {
1143     unsigned long vm_pgt_phys;
1144 };
1145 
1146 static int hw_daio_init(struct hw *hw, const struct daio_conf *info)
1147 {
1148     u32 data;
1149     int i;
1150 
1151     /* Program I2S with proper sample rate and enable the correct I2S
1152      * channel. ED(0/8/16/24): Enable all I2S/I2X master clock output */
1153     if (1 == info->msr) {
1154         hw_write_20kx(hw, AUDIO_IO_MCLK, 0x01010101);
1155         hw_write_20kx(hw, AUDIO_IO_TX_BLRCLK, 0x01010101);
1156         hw_write_20kx(hw, AUDIO_IO_RX_BLRCLK, 0);
1157     } else if (2 == info->msr) {
1158         if (hw->model != CTSB1270) {
1159             hw_write_20kx(hw, AUDIO_IO_MCLK, 0x11111111);
1160         } else {
1161             /* PCM4220 on Titanium HD is different. */
1162             hw_write_20kx(hw, AUDIO_IO_MCLK, 0x11011111);
1163         }
1164         /* Specify all playing 96khz
1165          * EA [0]   - Enabled
1166          * RTA [4:5]    - 96kHz
1167          * EB [8]   - Enabled
1168          * RTB [12:13]  - 96kHz
1169          * EC [16]  - Enabled
1170          * RTC [20:21]  - 96kHz
1171          * ED [24]  - Enabled
1172          * RTD [28:29]  - 96kHz */
1173         hw_write_20kx(hw, AUDIO_IO_TX_BLRCLK, 0x11111111);
1174         hw_write_20kx(hw, AUDIO_IO_RX_BLRCLK, 0);
1175     } else if ((4 == info->msr) && (hw->model == CTSB1270)) {
1176         hw_write_20kx(hw, AUDIO_IO_MCLK, 0x21011111);
1177         hw_write_20kx(hw, AUDIO_IO_TX_BLRCLK, 0x21212121);
1178         hw_write_20kx(hw, AUDIO_IO_RX_BLRCLK, 0);
1179     } else {
1180         dev_alert(hw->card->dev,
1181               "ERROR!!! Invalid sampling rate!!!\n");
1182         return -EINVAL;
1183     }
1184 
1185     for (i = 0; i < 8; i++) {
1186         if (i <= 3) {
1187             /* This comment looks wrong since loop is over 4  */
1188             /* channels and emu20k2 supports 4 spdif IOs.     */
1189             /* 1st 3 channels are SPDIFs (SB0960) */
1190             if (i == 3)
1191                 data = 0x1001001;
1192             else
1193                 data = 0x1000001;
1194 
1195             hw_write_20kx(hw, (AUDIO_IO_TX_CTL+(0x40*i)), data);
1196             hw_write_20kx(hw, (AUDIO_IO_RX_CTL+(0x40*i)), data);
1197 
1198             /* Initialize the SPDIF Out Channel status registers.
1199              * The value specified here is based on the typical
1200              * values provided in the specification, namely: Clock
1201              * Accuracy of 1000ppm, Sample Rate of 48KHz,
1202              * unspecified source number, Generation status = 1,
1203              * Category code = 0x12 (Digital Signal Mixer),
1204              * Mode = 0, Emph = 0, Copy Permitted, AN = 0
1205              * (indicating that we're transmitting digital audio,
1206              * and the Professional Use bit is 0. */
1207 
1208             hw_write_20kx(hw, AUDIO_IO_TX_CSTAT_L+(0x40*i),
1209                     0x02109204); /* Default to 48kHz */
1210 
1211             hw_write_20kx(hw, AUDIO_IO_TX_CSTAT_H+(0x40*i), 0x0B);
1212         } else {
1213             /* Again, loop is over 4 channels not 5. */
1214             /* Next 5 channels are I2S (SB0960) */
1215             data = 0x11;
1216             hw_write_20kx(hw, AUDIO_IO_RX_CTL+(0x40*i), data);
1217             if (2 == info->msr) {
1218                 /* Four channels per sample period */
1219                 data |= 0x1000;
1220             } else if (4 == info->msr) {
1221                 /* FIXME: check this against the chip spec */
1222                 data |= 0x2000;
1223             }
1224             hw_write_20kx(hw, AUDIO_IO_TX_CTL+(0x40*i), data);
1225         }
1226     }
1227 
1228     return 0;
1229 }
1230 
1231 /* TRANSPORT operations */
1232 static int hw_trn_init(struct hw *hw, const struct trn_conf *info)
1233 {
1234     u32 vmctl, data;
1235     u32 ptp_phys_low, ptp_phys_high;
1236     int i;
1237 
1238     /* Set up device page table */
1239     if ((~0UL) == info->vm_pgt_phys) {
1240         dev_alert(hw->card->dev,
1241               "Wrong device page table page address!!!\n");
1242         return -1;
1243     }
1244 
1245     vmctl = 0x80000C0F;  /* 32-bit, 4k-size page */
1246     ptp_phys_low = (u32)info->vm_pgt_phys;
1247     ptp_phys_high = upper_32_bits(info->vm_pgt_phys);
1248     if (sizeof(void *) == 8) /* 64bit address */
1249         vmctl |= (3 << 8);
1250     /* Write page table physical address to all PTPAL registers */
1251     for (i = 0; i < 64; i++) {
1252         hw_write_20kx(hw, VMEM_PTPAL+(16*i), ptp_phys_low);
1253         hw_write_20kx(hw, VMEM_PTPAH+(16*i), ptp_phys_high);
1254     }
1255     /* Enable virtual memory transfer */
1256     hw_write_20kx(hw, VMEM_CTL, vmctl);
1257     /* Enable transport bus master and queueing of request */
1258     hw_write_20kx(hw, TRANSPORT_CTL, 0x03);
1259     hw_write_20kx(hw, TRANSPORT_INT, 0x200c01);
1260     /* Enable transport ring */
1261     data = hw_read_20kx(hw, TRANSPORT_ENB);
1262     hw_write_20kx(hw, TRANSPORT_ENB, (data | 0x03));
1263 
1264     return 0;
1265 }
1266 
1267 /* Card initialization */
1268 #define GCTL_AIE    0x00000001
1269 #define GCTL_UAA    0x00000002
1270 #define GCTL_DPC    0x00000004
1271 #define GCTL_DBP    0x00000008
1272 #define GCTL_ABP    0x00000010
1273 #define GCTL_TBP    0x00000020
1274 #define GCTL_SBP    0x00000040
1275 #define GCTL_FBP    0x00000080
1276 #define GCTL_ME     0x00000100
1277 #define GCTL_AID    0x00001000
1278 
1279 #define PLLCTL_SRC  0x00000007
1280 #define PLLCTL_SPE  0x00000008
1281 #define PLLCTL_RD   0x000000F0
1282 #define PLLCTL_FD   0x0001FF00
1283 #define PLLCTL_OD   0x00060000
1284 #define PLLCTL_B    0x00080000
1285 #define PLLCTL_AS   0x00100000
1286 #define PLLCTL_LF   0x03E00000
1287 #define PLLCTL_SPS  0x1C000000
1288 #define PLLCTL_AD   0x60000000
1289 
1290 #define PLLSTAT_CCS 0x00000007
1291 #define PLLSTAT_SPL 0x00000008
1292 #define PLLSTAT_CRD 0x000000F0
1293 #define PLLSTAT_CFD 0x0001FF00
1294 #define PLLSTAT_SL  0x00020000
1295 #define PLLSTAT_FAS 0x00040000
1296 #define PLLSTAT_B   0x00080000
1297 #define PLLSTAT_PD  0x00100000
1298 #define PLLSTAT_OCA 0x00200000
1299 #define PLLSTAT_NCA 0x00400000
1300 
1301 static int hw_pll_init(struct hw *hw, unsigned int rsr)
1302 {
1303     unsigned int pllenb;
1304     unsigned int pllctl;
1305     unsigned int pllstat;
1306     int i;
1307 
1308     pllenb = 0xB;
1309     hw_write_20kx(hw, PLL_ENB, pllenb);
1310     pllctl = 0x20C00000;
1311     set_field(&pllctl, PLLCTL_B, 0);
1312     set_field(&pllctl, PLLCTL_FD, 48000 == rsr ? 16 - 4 : 147 - 4);
1313     set_field(&pllctl, PLLCTL_RD, 48000 == rsr ? 1 - 1 : 10 - 1);
1314     hw_write_20kx(hw, PLL_CTL, pllctl);
1315     msleep(40);
1316 
1317     pllctl = hw_read_20kx(hw, PLL_CTL);
1318     set_field(&pllctl, PLLCTL_FD, 48000 == rsr ? 16 - 2 : 147 - 2);
1319     hw_write_20kx(hw, PLL_CTL, pllctl);
1320     msleep(40);
1321 
1322     for (i = 0; i < 1000; i++) {
1323         pllstat = hw_read_20kx(hw, PLL_STAT);
1324         if (get_field(pllstat, PLLSTAT_PD))
1325             continue;
1326 
1327         if (get_field(pllstat, PLLSTAT_B) !=
1328                     get_field(pllctl, PLLCTL_B))
1329             continue;
1330 
1331         if (get_field(pllstat, PLLSTAT_CCS) !=
1332                     get_field(pllctl, PLLCTL_SRC))
1333             continue;
1334 
1335         if (get_field(pllstat, PLLSTAT_CRD) !=
1336                     get_field(pllctl, PLLCTL_RD))
1337             continue;
1338 
1339         if (get_field(pllstat, PLLSTAT_CFD) !=
1340                     get_field(pllctl, PLLCTL_FD))
1341             continue;
1342 
1343         break;
1344     }
1345     if (i >= 1000) {
1346         dev_alert(hw->card->dev,
1347               "PLL initialization failed!!!\n");
1348         return -EBUSY;
1349     }
1350 
1351     return 0;
1352 }
1353 
1354 static int hw_auto_init(struct hw *hw)
1355 {
1356     unsigned int gctl;
1357     int i;
1358 
1359     gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
1360     set_field(&gctl, GCTL_AIE, 0);
1361     hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
1362     set_field(&gctl, GCTL_AIE, 1);
1363     hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
1364     mdelay(10);
1365     for (i = 0; i < 400000; i++) {
1366         gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
1367         if (get_field(gctl, GCTL_AID))
1368             break;
1369     }
1370     if (!get_field(gctl, GCTL_AID)) {
1371         dev_alert(hw->card->dev, "Card Auto-init failed!!!\n");
1372         return -EBUSY;
1373     }
1374 
1375     return 0;
1376 }
1377 
1378 /* DAC operations */
1379 
1380 #define CS4382_MC1      0x1
1381 #define CS4382_MC2      0x2
1382 #define CS4382_MC3      0x3
1383 #define CS4382_FC       0x4
1384 #define CS4382_IC       0x5
1385 #define CS4382_XC1      0x6
1386 #define CS4382_VCA1         0x7
1387 #define CS4382_VCB1         0x8
1388 #define CS4382_XC2      0x9
1389 #define CS4382_VCA2         0xA
1390 #define CS4382_VCB2         0xB
1391 #define CS4382_XC3      0xC
1392 #define CS4382_VCA3     0xD
1393 #define CS4382_VCB3     0xE
1394 #define CS4382_XC4      0xF
1395 #define CS4382_VCA4         0x10
1396 #define CS4382_VCB4         0x11
1397 #define CS4382_CREV         0x12
1398 
1399 /* I2C status */
1400 #define STATE_LOCKED        0x00
1401 #define STATE_UNLOCKED      0xAA
1402 #define DATA_READY      0x800000    /* Used with I2C_IF_STATUS */
1403 #define DATA_ABORT      0x10000     /* Used with I2C_IF_STATUS */
1404 
1405 #define I2C_STATUS_DCM  0x00000001
1406 #define I2C_STATUS_BC   0x00000006
1407 #define I2C_STATUS_APD  0x00000008
1408 #define I2C_STATUS_AB   0x00010000
1409 #define I2C_STATUS_DR   0x00800000
1410 
1411 #define I2C_ADDRESS_PTAD    0x0000FFFF
1412 #define I2C_ADDRESS_SLAD    0x007F0000
1413 
1414 struct regs_cs4382 {
1415     u32 mode_control_1;
1416     u32 mode_control_2;
1417     u32 mode_control_3;
1418 
1419     u32 filter_control;
1420     u32 invert_control;
1421 
1422     u32 mix_control_P1;
1423     u32 vol_control_A1;
1424     u32 vol_control_B1;
1425 
1426     u32 mix_control_P2;
1427     u32 vol_control_A2;
1428     u32 vol_control_B2;
1429 
1430     u32 mix_control_P3;
1431     u32 vol_control_A3;
1432     u32 vol_control_B3;
1433 
1434     u32 mix_control_P4;
1435     u32 vol_control_A4;
1436     u32 vol_control_B4;
1437 };
1438 
1439 static int hw20k2_i2c_unlock_full_access(struct hw *hw)
1440 {
1441     u8 UnlockKeySequence_FLASH_FULLACCESS_MODE[2] =  {0xB3, 0xD4};
1442 
1443     /* Send keys for forced BIOS mode */
1444     hw_write_20kx(hw, I2C_IF_WLOCK,
1445             UnlockKeySequence_FLASH_FULLACCESS_MODE[0]);
1446     hw_write_20kx(hw, I2C_IF_WLOCK,
1447             UnlockKeySequence_FLASH_FULLACCESS_MODE[1]);
1448     /* Check whether the chip is unlocked */
1449     if (hw_read_20kx(hw, I2C_IF_WLOCK) == STATE_UNLOCKED)
1450         return 0;
1451 
1452     return -1;
1453 }
1454 
1455 static int hw20k2_i2c_lock_chip(struct hw *hw)
1456 {
1457     /* Write twice */
1458     hw_write_20kx(hw, I2C_IF_WLOCK, STATE_LOCKED);
1459     hw_write_20kx(hw, I2C_IF_WLOCK, STATE_LOCKED);
1460     if (hw_read_20kx(hw, I2C_IF_WLOCK) == STATE_LOCKED)
1461         return 0;
1462 
1463     return -1;
1464 }
1465 
1466 static int hw20k2_i2c_init(struct hw *hw, u8 dev_id, u8 addr_size, u8 data_size)
1467 {
1468     struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
1469     int err;
1470     unsigned int i2c_status;
1471     unsigned int i2c_addr;
1472 
1473     err = hw20k2_i2c_unlock_full_access(hw);
1474     if (err < 0)
1475         return err;
1476 
1477     hw20k2->addr_size = addr_size;
1478     hw20k2->data_size = data_size;
1479     hw20k2->dev_id = dev_id;
1480 
1481     i2c_addr = 0;
1482     set_field(&i2c_addr, I2C_ADDRESS_SLAD, dev_id);
1483 
1484     hw_write_20kx(hw, I2C_IF_ADDRESS, i2c_addr);
1485 
1486     i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
1487 
1488     set_field(&i2c_status, I2C_STATUS_DCM, 1); /* Direct control mode */
1489 
1490     hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
1491 
1492     return 0;
1493 }
1494 
1495 static int hw20k2_i2c_uninit(struct hw *hw)
1496 {
1497     unsigned int i2c_status;
1498     unsigned int i2c_addr;
1499 
1500     i2c_addr = 0;
1501     set_field(&i2c_addr, I2C_ADDRESS_SLAD, 0x57); /* I2C id */
1502 
1503     hw_write_20kx(hw, I2C_IF_ADDRESS, i2c_addr);
1504 
1505     i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
1506 
1507     set_field(&i2c_status, I2C_STATUS_DCM, 0); /* I2C mode */
1508 
1509     hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
1510 
1511     return hw20k2_i2c_lock_chip(hw);
1512 }
1513 
1514 static int hw20k2_i2c_wait_data_ready(struct hw *hw)
1515 {
1516     int i = 0x400000;
1517     unsigned int ret;
1518 
1519     do {
1520         ret = hw_read_20kx(hw, I2C_IF_STATUS);
1521     } while ((!(ret & DATA_READY)) && --i);
1522 
1523     return i;
1524 }
1525 
1526 static int hw20k2_i2c_read(struct hw *hw, u16 addr, u32 *datap)
1527 {
1528     struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
1529     unsigned int i2c_status;
1530 
1531     i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
1532     set_field(&i2c_status, I2C_STATUS_BC,
1533           (4 == hw20k2->addr_size) ? 0 : hw20k2->addr_size);
1534     hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
1535     if (!hw20k2_i2c_wait_data_ready(hw))
1536         return -1;
1537 
1538     hw_write_20kx(hw, I2C_IF_WDATA, addr);
1539     if (!hw20k2_i2c_wait_data_ready(hw))
1540         return -1;
1541 
1542     /* Force a read operation */
1543     hw_write_20kx(hw, I2C_IF_RDATA, 0);
1544     if (!hw20k2_i2c_wait_data_ready(hw))
1545         return -1;
1546 
1547     *datap = hw_read_20kx(hw, I2C_IF_RDATA);
1548 
1549     return 0;
1550 }
1551 
1552 static int hw20k2_i2c_write(struct hw *hw, u16 addr, u32 data)
1553 {
1554     struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
1555     unsigned int i2c_data = (data << (hw20k2->addr_size * 8)) | addr;
1556     unsigned int i2c_status;
1557 
1558     i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
1559 
1560     set_field(&i2c_status, I2C_STATUS_BC,
1561           (4 == (hw20k2->addr_size + hw20k2->data_size)) ?
1562           0 : (hw20k2->addr_size + hw20k2->data_size));
1563 
1564     hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
1565     hw20k2_i2c_wait_data_ready(hw);
1566     /* Dummy write to trigger the write operation */
1567     hw_write_20kx(hw, I2C_IF_WDATA, 0);
1568     hw20k2_i2c_wait_data_ready(hw);
1569 
1570     /* This is the real data */
1571     hw_write_20kx(hw, I2C_IF_WDATA, i2c_data);
1572     hw20k2_i2c_wait_data_ready(hw);
1573 
1574     return 0;
1575 }
1576 
1577 static void hw_dac_stop(struct hw *hw)
1578 {
1579     u32 data;
1580     data = hw_read_20kx(hw, GPIO_DATA);
1581     data &= 0xFFFFFFFD;
1582     hw_write_20kx(hw, GPIO_DATA, data);
1583     usleep_range(10000, 11000);
1584 }
1585 
1586 static void hw_dac_start(struct hw *hw)
1587 {
1588     u32 data;
1589     data = hw_read_20kx(hw, GPIO_DATA);
1590     data |= 0x2;
1591     hw_write_20kx(hw, GPIO_DATA, data);
1592     msleep(50);
1593 }
1594 
1595 static void hw_dac_reset(struct hw *hw)
1596 {
1597     hw_dac_stop(hw);
1598     hw_dac_start(hw);
1599 }
1600 
1601 static int hw_dac_init(struct hw *hw, const struct dac_conf *info)
1602 {
1603     int err;
1604     u32 data;
1605     int i;
1606     struct regs_cs4382 cs_read = {0};
1607     struct regs_cs4382 cs_def = {
1608         .mode_control_1 = 0x00000001, /* Mode Control 1 */
1609         .mode_control_2 = 0x00000000, /* Mode Control 2 */
1610         .mode_control_3 = 0x00000084, /* Mode Control 3 */
1611         .filter_control = 0x00000000, /* Filter Control */
1612         .invert_control = 0x00000000, /* Invert Control */
1613         .mix_control_P1 = 0x00000024, /* Mixing Control Pair 1 */
1614         .vol_control_A1 = 0x00000000, /* Vol Control A1 */
1615         .vol_control_B1 = 0x00000000, /* Vol Control B1 */
1616         .mix_control_P2 = 0x00000024, /* Mixing Control Pair 2 */
1617         .vol_control_A2 = 0x00000000, /* Vol Control A2 */
1618         .vol_control_B2 = 0x00000000, /* Vol Control B2 */
1619         .mix_control_P3 = 0x00000024, /* Mixing Control Pair 3 */
1620         .vol_control_A3 = 0x00000000, /* Vol Control A3 */
1621         .vol_control_B3 = 0x00000000, /* Vol Control B3 */
1622         .mix_control_P4 = 0x00000024, /* Mixing Control Pair 4 */
1623         .vol_control_A4 = 0x00000000, /* Vol Control A4 */
1624         .vol_control_B4 = 0x00000000  /* Vol Control B4 */
1625                  };
1626 
1627     if (hw->model == CTSB1270) {
1628         hw_dac_stop(hw);
1629         data = hw_read_20kx(hw, GPIO_DATA);
1630         data &= ~0x0600;
1631         if (1 == info->msr)
1632             data |= 0x0000; /* Single Speed Mode 0-50kHz */
1633         else if (2 == info->msr)
1634             data |= 0x0200; /* Double Speed Mode 50-100kHz */
1635         else
1636             data |= 0x0600; /* Quad Speed Mode 100-200kHz */
1637         hw_write_20kx(hw, GPIO_DATA, data);
1638         hw_dac_start(hw);
1639         return 0;
1640     }
1641 
1642     /* Set DAC reset bit as output */
1643     data = hw_read_20kx(hw, GPIO_CTRL);
1644     data |= 0x02;
1645     hw_write_20kx(hw, GPIO_CTRL, data);
1646 
1647     err = hw20k2_i2c_init(hw, 0x18, 1, 1);
1648     if (err < 0)
1649         goto End;
1650 
1651     for (i = 0; i < 2; i++) {
1652         /* Reset DAC twice just in-case the chip
1653          * didn't initialized properly */
1654         hw_dac_reset(hw);
1655         hw_dac_reset(hw);
1656 
1657         if (hw20k2_i2c_read(hw, CS4382_MC1,  &cs_read.mode_control_1))
1658             continue;
1659 
1660         if (hw20k2_i2c_read(hw, CS4382_MC2,  &cs_read.mode_control_2))
1661             continue;
1662 
1663         if (hw20k2_i2c_read(hw, CS4382_MC3,  &cs_read.mode_control_3))
1664             continue;
1665 
1666         if (hw20k2_i2c_read(hw, CS4382_FC,   &cs_read.filter_control))
1667             continue;
1668 
1669         if (hw20k2_i2c_read(hw, CS4382_IC,   &cs_read.invert_control))
1670             continue;
1671 
1672         if (hw20k2_i2c_read(hw, CS4382_XC1,  &cs_read.mix_control_P1))
1673             continue;
1674 
1675         if (hw20k2_i2c_read(hw, CS4382_VCA1, &cs_read.vol_control_A1))
1676             continue;
1677 
1678         if (hw20k2_i2c_read(hw, CS4382_VCB1, &cs_read.vol_control_B1))
1679             continue;
1680 
1681         if (hw20k2_i2c_read(hw, CS4382_XC2,  &cs_read.mix_control_P2))
1682             continue;
1683 
1684         if (hw20k2_i2c_read(hw, CS4382_VCA2, &cs_read.vol_control_A2))
1685             continue;
1686 
1687         if (hw20k2_i2c_read(hw, CS4382_VCB2, &cs_read.vol_control_B2))
1688             continue;
1689 
1690         if (hw20k2_i2c_read(hw, CS4382_XC3,  &cs_read.mix_control_P3))
1691             continue;
1692 
1693         if (hw20k2_i2c_read(hw, CS4382_VCA3, &cs_read.vol_control_A3))
1694             continue;
1695 
1696         if (hw20k2_i2c_read(hw, CS4382_VCB3, &cs_read.vol_control_B3))
1697             continue;
1698 
1699         if (hw20k2_i2c_read(hw, CS4382_XC4,  &cs_read.mix_control_P4))
1700             continue;
1701 
1702         if (hw20k2_i2c_read(hw, CS4382_VCA4, &cs_read.vol_control_A4))
1703             continue;
1704 
1705         if (hw20k2_i2c_read(hw, CS4382_VCB4, &cs_read.vol_control_B4))
1706             continue;
1707 
1708         if (memcmp(&cs_read, &cs_def, sizeof(cs_read)))
1709             continue;
1710         else
1711             break;
1712     }
1713 
1714     if (i >= 2)
1715         goto End;
1716 
1717     /* Note: Every I2C write must have some delay.
1718      * This is not a requirement but the delay works here... */
1719     hw20k2_i2c_write(hw, CS4382_MC1, 0x80);
1720     hw20k2_i2c_write(hw, CS4382_MC2, 0x10);
1721     if (1 == info->msr) {
1722         hw20k2_i2c_write(hw, CS4382_XC1, 0x24);
1723         hw20k2_i2c_write(hw, CS4382_XC2, 0x24);
1724         hw20k2_i2c_write(hw, CS4382_XC3, 0x24);
1725         hw20k2_i2c_write(hw, CS4382_XC4, 0x24);
1726     } else if (2 == info->msr) {
1727         hw20k2_i2c_write(hw, CS4382_XC1, 0x25);
1728         hw20k2_i2c_write(hw, CS4382_XC2, 0x25);
1729         hw20k2_i2c_write(hw, CS4382_XC3, 0x25);
1730         hw20k2_i2c_write(hw, CS4382_XC4, 0x25);
1731     } else {
1732         hw20k2_i2c_write(hw, CS4382_XC1, 0x26);
1733         hw20k2_i2c_write(hw, CS4382_XC2, 0x26);
1734         hw20k2_i2c_write(hw, CS4382_XC3, 0x26);
1735         hw20k2_i2c_write(hw, CS4382_XC4, 0x26);
1736     }
1737 
1738     return 0;
1739 End:
1740 
1741     hw20k2_i2c_uninit(hw);
1742     return -1;
1743 }
1744 
1745 /* ADC operations */
1746 #define MAKE_WM8775_ADDR(addr, data)    (u32)(((addr<<1)&0xFE)|((data>>8)&0x1))
1747 #define MAKE_WM8775_DATA(data)  (u32)(data&0xFF)
1748 
1749 #define WM8775_IC       0x0B
1750 #define WM8775_MMC      0x0C
1751 #define WM8775_AADCL    0x0E
1752 #define WM8775_AADCR    0x0F
1753 #define WM8775_ADCMC    0x15
1754 #define WM8775_RESET    0x17
1755 
1756 static int hw_is_adc_input_selected(struct hw *hw, enum ADCSRC type)
1757 {
1758     u32 data;
1759     if (hw->model == CTSB1270) {
1760         /* Titanium HD has two ADC chips, one for line in and one */
1761         /* for MIC. We don't need to switch the ADC input. */
1762         return 1;
1763     }
1764     data = hw_read_20kx(hw, GPIO_DATA);
1765     switch (type) {
1766     case ADC_MICIN:
1767         data = (data & (0x1 << 14)) ? 1 : 0;
1768         break;
1769     case ADC_LINEIN:
1770         data = (data & (0x1 << 14)) ? 0 : 1;
1771         break;
1772     default:
1773         data = 0;
1774     }
1775     return data;
1776 }
1777 
1778 #define MIC_BOOST_0DB 0xCF
1779 #define MIC_BOOST_STEPS_PER_DB 2
1780 
1781 static void hw_wm8775_input_select(struct hw *hw, u8 input, s8 gain_in_db)
1782 {
1783     u32 adcmc, gain;
1784 
1785     if (input > 3)
1786         input = 3;
1787 
1788     adcmc = ((u32)1 << input) | 0x100; /* Link L+R gain... */
1789 
1790     hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_ADCMC, adcmc),
1791                 MAKE_WM8775_DATA(adcmc));
1792 
1793     if (gain_in_db < -103)
1794         gain_in_db = -103;
1795     if (gain_in_db > 24)
1796         gain_in_db = 24;
1797 
1798     gain = gain_in_db * MIC_BOOST_STEPS_PER_DB + MIC_BOOST_0DB;
1799 
1800     hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_AADCL, gain),
1801                 MAKE_WM8775_DATA(gain));
1802     /* ...so there should be no need for the following. */
1803     hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_AADCR, gain),
1804                 MAKE_WM8775_DATA(gain));
1805 }
1806 
1807 static int hw_adc_input_select(struct hw *hw, enum ADCSRC type)
1808 {
1809     u32 data;
1810     data = hw_read_20kx(hw, GPIO_DATA);
1811     switch (type) {
1812     case ADC_MICIN:
1813         data |= (0x1 << 14);
1814         hw_write_20kx(hw, GPIO_DATA, data);
1815         hw_wm8775_input_select(hw, 0, 20); /* Mic, 20dB */
1816         break;
1817     case ADC_LINEIN:
1818         data &= ~(0x1 << 14);
1819         hw_write_20kx(hw, GPIO_DATA, data);
1820         hw_wm8775_input_select(hw, 1, 0); /* Line-in, 0dB */
1821         break;
1822     default:
1823         break;
1824     }
1825 
1826     return 0;
1827 }
1828 
1829 static int hw_adc_init(struct hw *hw, const struct adc_conf *info)
1830 {
1831     int err;
1832     u32 data, ctl;
1833 
1834     /*  Set ADC reset bit as output */
1835     data = hw_read_20kx(hw, GPIO_CTRL);
1836     data |= (0x1 << 15);
1837     hw_write_20kx(hw, GPIO_CTRL, data);
1838 
1839     /* Initialize I2C */
1840     err = hw20k2_i2c_init(hw, 0x1A, 1, 1);
1841     if (err < 0) {
1842         dev_alert(hw->card->dev, "Failure to acquire I2C!!!\n");
1843         goto error;
1844     }
1845 
1846     /* Reset the ADC (reset is active low). */
1847     data = hw_read_20kx(hw, GPIO_DATA);
1848     data &= ~(0x1 << 15);
1849     hw_write_20kx(hw, GPIO_DATA, data);
1850 
1851     if (hw->model == CTSB1270) {
1852         /* Set up the PCM4220 ADC on Titanium HD */
1853         data &= ~0x0C;
1854         if (1 == info->msr)
1855             data |= 0x00; /* Single Speed Mode 32-50kHz */
1856         else if (2 == info->msr)
1857             data |= 0x08; /* Double Speed Mode 50-108kHz */
1858         else
1859             data |= 0x04; /* Quad Speed Mode 108kHz-216kHz */
1860         hw_write_20kx(hw, GPIO_DATA, data);
1861     }
1862 
1863     usleep_range(10000, 11000);
1864     /* Return the ADC to normal operation. */
1865     data |= (0x1 << 15);
1866     hw_write_20kx(hw, GPIO_DATA, data);
1867     msleep(50);
1868 
1869     /* I2C write to register offset 0x0B to set ADC LRCLK polarity */
1870     /* invert bit, interface format to I2S, word length to 24-bit, */
1871     /* enable ADC high pass filter. Fixes bug 5323?     */
1872     hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_IC, 0x26),
1873              MAKE_WM8775_DATA(0x26));
1874 
1875     /* Set the master mode (256fs) */
1876     if (1 == info->msr) {
1877         /* slave mode, 128x oversampling 256fs */
1878         hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_MMC, 0x02),
1879                         MAKE_WM8775_DATA(0x02));
1880     } else if ((2 == info->msr) || (4 == info->msr)) {
1881         /* slave mode, 64x oversampling, 256fs */
1882         hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_MMC, 0x0A),
1883                         MAKE_WM8775_DATA(0x0A));
1884     } else {
1885         dev_alert(hw->card->dev,
1886               "Invalid master sampling rate (msr %d)!!!\n",
1887               info->msr);
1888         err = -EINVAL;
1889         goto error;
1890     }
1891 
1892     if (hw->model != CTSB1270) {
1893         /* Configure GPIO bit 14 change to line-in/mic-in */
1894         ctl = hw_read_20kx(hw, GPIO_CTRL);
1895         ctl |= 0x1 << 14;
1896         hw_write_20kx(hw, GPIO_CTRL, ctl);
1897         hw_adc_input_select(hw, ADC_LINEIN);
1898     } else {
1899         hw_wm8775_input_select(hw, 0, 0);
1900     }
1901 
1902     return 0;
1903 error:
1904     hw20k2_i2c_uninit(hw);
1905     return err;
1906 }
1907 
1908 static struct capabilities hw_capabilities(struct hw *hw)
1909 {
1910     struct capabilities cap;
1911 
1912     cap.digit_io_switch = 0;
1913     cap.dedicated_mic = hw->model == CTSB1270;
1914     cap.output_switch = hw->model == CTSB1270;
1915     cap.mic_source_switch = hw->model == CTSB1270;
1916 
1917     return cap;
1918 }
1919 
1920 static int hw_output_switch_get(struct hw *hw)
1921 {
1922     u32 data = hw_read_20kx(hw, GPIO_EXT_DATA);
1923 
1924     switch (data & 0x30) {
1925     case 0x00:
1926          return 0;
1927     case 0x10:
1928          return 1;
1929     case 0x20:
1930          return 2;
1931     default:
1932          return 3;
1933     }
1934 }
1935 
1936 static int hw_output_switch_put(struct hw *hw, int position)
1937 {
1938     u32 data;
1939 
1940     if (position == hw_output_switch_get(hw))
1941         return 0;
1942 
1943     /* Mute line and headphones (intended for anti-pop). */
1944     data = hw_read_20kx(hw, GPIO_DATA);
1945     data |= (0x03 << 11);
1946     hw_write_20kx(hw, GPIO_DATA, data);
1947 
1948     data = hw_read_20kx(hw, GPIO_EXT_DATA) & ~0x30;
1949     switch (position) {
1950     case 0:
1951         break;
1952     case 1:
1953         data |= 0x10;
1954         break;
1955     default:
1956         data |= 0x20;
1957     }
1958     hw_write_20kx(hw, GPIO_EXT_DATA, data);
1959 
1960     /* Unmute line and headphones. */
1961     data = hw_read_20kx(hw, GPIO_DATA);
1962     data &= ~(0x03 << 11);
1963     hw_write_20kx(hw, GPIO_DATA, data);
1964 
1965     return 1;
1966 }
1967 
1968 static int hw_mic_source_switch_get(struct hw *hw)
1969 {
1970     struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
1971 
1972     return hw20k2->mic_source;
1973 }
1974 
1975 static int hw_mic_source_switch_put(struct hw *hw, int position)
1976 {
1977     struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
1978 
1979     if (position == hw20k2->mic_source)
1980         return 0;
1981 
1982     switch (position) {
1983     case 0:
1984         hw_wm8775_input_select(hw, 0, 0); /* Mic, 0dB */
1985         break;
1986     case 1:
1987         hw_wm8775_input_select(hw, 1, 0); /* FP Mic, 0dB */
1988         break;
1989     case 2:
1990         hw_wm8775_input_select(hw, 3, 0); /* Aux Ext, 0dB */
1991         break;
1992     default:
1993         return 0;
1994     }
1995 
1996     hw20k2->mic_source = position;
1997 
1998     return 1;
1999 }
2000 
2001 static irqreturn_t ct_20k2_interrupt(int irq, void *dev_id)
2002 {
2003     struct hw *hw = dev_id;
2004     unsigned int status;
2005 
2006     status = hw_read_20kx(hw, GIP);
2007     if (!status)
2008         return IRQ_NONE;
2009 
2010     if (hw->irq_callback)
2011         hw->irq_callback(hw->irq_callback_data, status);
2012 
2013     hw_write_20kx(hw, GIP, status);
2014     return IRQ_HANDLED;
2015 }
2016 
2017 static int hw_card_start(struct hw *hw)
2018 {
2019     int err = 0;
2020     struct pci_dev *pci = hw->pci;
2021     unsigned int gctl;
2022     const unsigned int dma_bits = BITS_PER_LONG;
2023 
2024     err = pci_enable_device(pci);
2025     if (err < 0)
2026         return err;
2027 
2028     /* Set DMA transfer mask */
2029     if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(dma_bits)))
2030         dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
2031 
2032     if (!hw->io_base) {
2033         err = pci_request_regions(pci, "XFi");
2034         if (err < 0)
2035             goto error1;
2036 
2037         hw->io_base = pci_resource_start(hw->pci, 2);
2038         hw->mem_base = ioremap(hw->io_base,
2039                        pci_resource_len(hw->pci, 2));
2040         if (!hw->mem_base) {
2041             err = -ENOENT;
2042             goto error2;
2043         }
2044     }
2045 
2046     /* Switch to 20k2 mode from UAA mode. */
2047     gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
2048     set_field(&gctl, GCTL_UAA, 0);
2049     hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
2050 
2051     if (hw->irq < 0) {
2052         err = request_irq(pci->irq, ct_20k2_interrupt, IRQF_SHARED,
2053                   KBUILD_MODNAME, hw);
2054         if (err < 0) {
2055             dev_err(hw->card->dev,
2056                 "XFi: Cannot get irq %d\n", pci->irq);
2057             goto error2;
2058         }
2059         hw->irq = pci->irq;
2060         hw->card->sync_irq = hw->irq;
2061     }
2062 
2063     pci_set_master(pci);
2064 
2065     return 0;
2066 
2067 /*error3:
2068     iounmap((void *)hw->mem_base);
2069     hw->mem_base = (unsigned long)NULL;*/
2070 error2:
2071     pci_release_regions(pci);
2072     hw->io_base = 0;
2073 error1:
2074     pci_disable_device(pci);
2075     return err;
2076 }
2077 
2078 static int hw_card_stop(struct hw *hw)
2079 {
2080     unsigned int data;
2081 
2082     /* disable transport bus master and queueing of request */
2083     hw_write_20kx(hw, TRANSPORT_CTL, 0x00);
2084 
2085     /* disable pll */
2086     data = hw_read_20kx(hw, PLL_ENB);
2087     hw_write_20kx(hw, PLL_ENB, (data & (~0x07)));
2088 
2089     /* TODO: Disable interrupt and so on... */
2090     return 0;
2091 }
2092 
2093 static int hw_card_shutdown(struct hw *hw)
2094 {
2095     if (hw->irq >= 0)
2096         free_irq(hw->irq, hw);
2097 
2098     hw->irq = -1;
2099     iounmap(hw->mem_base);
2100     hw->mem_base = NULL;
2101 
2102     if (hw->io_base)
2103         pci_release_regions(hw->pci);
2104 
2105     hw->io_base = 0;
2106 
2107     pci_disable_device(hw->pci);
2108 
2109     return 0;
2110 }
2111 
2112 static int hw_card_init(struct hw *hw, struct card_conf *info)
2113 {
2114     int err;
2115     unsigned int gctl;
2116     u32 data = 0;
2117     struct dac_conf dac_info = {0};
2118     struct adc_conf adc_info = {0};
2119     struct daio_conf daio_info = {0};
2120     struct trn_conf trn_info = {0};
2121 
2122     /* Get PCI io port/memory base address and
2123      * do 20kx core switch if needed. */
2124     err = hw_card_start(hw);
2125     if (err)
2126         return err;
2127 
2128     /* PLL init */
2129     err = hw_pll_init(hw, info->rsr);
2130     if (err < 0)
2131         return err;
2132 
2133     /* kick off auto-init */
2134     err = hw_auto_init(hw);
2135     if (err < 0)
2136         return err;
2137 
2138     gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
2139     set_field(&gctl, GCTL_DBP, 1);
2140     set_field(&gctl, GCTL_TBP, 1);
2141     set_field(&gctl, GCTL_FBP, 1);
2142     set_field(&gctl, GCTL_DPC, 0);
2143     hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
2144 
2145     /* Reset all global pending interrupts */
2146     hw_write_20kx(hw, GIE, 0);
2147     /* Reset all SRC pending interrupts */
2148     hw_write_20kx(hw, SRC_IP, 0);
2149 
2150     if (hw->model != CTSB1270) {
2151         /* TODO: detect the card ID and configure GPIO accordingly. */
2152         /* Configures GPIO (0xD802 0x98028) */
2153         /*hw_write_20kx(hw, GPIO_CTRL, 0x7F07);*/
2154         /* Configures GPIO (SB0880) */
2155         /*hw_write_20kx(hw, GPIO_CTRL, 0xFF07);*/
2156         hw_write_20kx(hw, GPIO_CTRL, 0xD802);
2157     } else {
2158         hw_write_20kx(hw, GPIO_CTRL, 0x9E5F);
2159     }
2160     /* Enable audio ring */
2161     hw_write_20kx(hw, MIXER_AR_ENABLE, 0x01);
2162 
2163     trn_info.vm_pgt_phys = info->vm_pgt_phys;
2164     err = hw_trn_init(hw, &trn_info);
2165     if (err < 0)
2166         return err;
2167 
2168     daio_info.msr = info->msr;
2169     err = hw_daio_init(hw, &daio_info);
2170     if (err < 0)
2171         return err;
2172 
2173     dac_info.msr = info->msr;
2174     err = hw_dac_init(hw, &dac_info);
2175     if (err < 0)
2176         return err;
2177 
2178     adc_info.msr = info->msr;
2179     adc_info.input = ADC_LINEIN;
2180     adc_info.mic20db = 0;
2181     err = hw_adc_init(hw, &adc_info);
2182     if (err < 0)
2183         return err;
2184 
2185     data = hw_read_20kx(hw, SRC_MCTL);
2186     data |= 0x1; /* Enables input from the audio ring */
2187     hw_write_20kx(hw, SRC_MCTL, data);
2188 
2189     return 0;
2190 }
2191 
2192 #ifdef CONFIG_PM_SLEEP
2193 static int hw_suspend(struct hw *hw)
2194 {
2195     hw_card_stop(hw);
2196     return 0;
2197 }
2198 
2199 static int hw_resume(struct hw *hw, struct card_conf *info)
2200 {
2201     /* Re-initialize card hardware. */
2202     return hw_card_init(hw, info);
2203 }
2204 #endif
2205 
2206 static u32 hw_read_20kx(struct hw *hw, u32 reg)
2207 {
2208     return readl(hw->mem_base + reg);
2209 }
2210 
2211 static void hw_write_20kx(struct hw *hw, u32 reg, u32 data)
2212 {
2213     writel(data, hw->mem_base + reg);
2214 }
2215 
2216 static const struct hw ct20k2_preset = {
2217     .irq = -1,
2218 
2219     .card_init = hw_card_init,
2220     .card_stop = hw_card_stop,
2221     .pll_init = hw_pll_init,
2222     .is_adc_source_selected = hw_is_adc_input_selected,
2223     .select_adc_source = hw_adc_input_select,
2224     .capabilities = hw_capabilities,
2225     .output_switch_get = hw_output_switch_get,
2226     .output_switch_put = hw_output_switch_put,
2227     .mic_source_switch_get = hw_mic_source_switch_get,
2228     .mic_source_switch_put = hw_mic_source_switch_put,
2229 #ifdef CONFIG_PM_SLEEP
2230     .suspend = hw_suspend,
2231     .resume = hw_resume,
2232 #endif
2233 
2234     .src_rsc_get_ctrl_blk = src_get_rsc_ctrl_blk,
2235     .src_rsc_put_ctrl_blk = src_put_rsc_ctrl_blk,
2236     .src_mgr_get_ctrl_blk = src_mgr_get_ctrl_blk,
2237     .src_mgr_put_ctrl_blk = src_mgr_put_ctrl_blk,
2238     .src_set_state = src_set_state,
2239     .src_set_bm = src_set_bm,
2240     .src_set_rsr = src_set_rsr,
2241     .src_set_sf = src_set_sf,
2242     .src_set_wr = src_set_wr,
2243     .src_set_pm = src_set_pm,
2244     .src_set_rom = src_set_rom,
2245     .src_set_vo = src_set_vo,
2246     .src_set_st = src_set_st,
2247     .src_set_ie = src_set_ie,
2248     .src_set_ilsz = src_set_ilsz,
2249     .src_set_bp = src_set_bp,
2250     .src_set_cisz = src_set_cisz,
2251     .src_set_ca = src_set_ca,
2252     .src_set_sa = src_set_sa,
2253     .src_set_la = src_set_la,
2254     .src_set_pitch = src_set_pitch,
2255     .src_set_dirty = src_set_dirty,
2256     .src_set_clear_zbufs = src_set_clear_zbufs,
2257     .src_set_dirty_all = src_set_dirty_all,
2258     .src_commit_write = src_commit_write,
2259     .src_get_ca = src_get_ca,
2260     .src_get_dirty = src_get_dirty,
2261     .src_dirty_conj_mask = src_dirty_conj_mask,
2262     .src_mgr_enbs_src = src_mgr_enbs_src,
2263     .src_mgr_enb_src = src_mgr_enb_src,
2264     .src_mgr_dsb_src = src_mgr_dsb_src,
2265     .src_mgr_commit_write = src_mgr_commit_write,
2266 
2267     .srcimp_mgr_get_ctrl_blk = srcimp_mgr_get_ctrl_blk,
2268     .srcimp_mgr_put_ctrl_blk = srcimp_mgr_put_ctrl_blk,
2269     .srcimp_mgr_set_imaparc = srcimp_mgr_set_imaparc,
2270     .srcimp_mgr_set_imapuser = srcimp_mgr_set_imapuser,
2271     .srcimp_mgr_set_imapnxt = srcimp_mgr_set_imapnxt,
2272     .srcimp_mgr_set_imapaddr = srcimp_mgr_set_imapaddr,
2273     .srcimp_mgr_commit_write = srcimp_mgr_commit_write,
2274 
2275     .amixer_rsc_get_ctrl_blk = amixer_rsc_get_ctrl_blk,
2276     .amixer_rsc_put_ctrl_blk = amixer_rsc_put_ctrl_blk,
2277     .amixer_mgr_get_ctrl_blk = amixer_mgr_get_ctrl_blk,
2278     .amixer_mgr_put_ctrl_blk = amixer_mgr_put_ctrl_blk,
2279     .amixer_set_mode = amixer_set_mode,
2280     .amixer_set_iv = amixer_set_iv,
2281     .amixer_set_x = amixer_set_x,
2282     .amixer_set_y = amixer_set_y,
2283     .amixer_set_sadr = amixer_set_sadr,
2284     .amixer_set_se = amixer_set_se,
2285     .amixer_set_dirty = amixer_set_dirty,
2286     .amixer_set_dirty_all = amixer_set_dirty_all,
2287     .amixer_commit_write = amixer_commit_write,
2288     .amixer_get_y = amixer_get_y,
2289     .amixer_get_dirty = amixer_get_dirty,
2290 
2291     .dai_get_ctrl_blk = dai_get_ctrl_blk,
2292     .dai_put_ctrl_blk = dai_put_ctrl_blk,
2293     .dai_srt_set_srco = dai_srt_set_srco,
2294     .dai_srt_set_srcm = dai_srt_set_srcm,
2295     .dai_srt_set_rsr = dai_srt_set_rsr,
2296     .dai_srt_set_drat = dai_srt_set_drat,
2297     .dai_srt_set_ec = dai_srt_set_ec,
2298     .dai_srt_set_et = dai_srt_set_et,
2299     .dai_commit_write = dai_commit_write,
2300 
2301     .dao_get_ctrl_blk = dao_get_ctrl_blk,
2302     .dao_put_ctrl_blk = dao_put_ctrl_blk,
2303     .dao_set_spos = dao_set_spos,
2304     .dao_commit_write = dao_commit_write,
2305     .dao_get_spos = dao_get_spos,
2306 
2307     .daio_mgr_get_ctrl_blk = daio_mgr_get_ctrl_blk,
2308     .daio_mgr_put_ctrl_blk = daio_mgr_put_ctrl_blk,
2309     .daio_mgr_enb_dai = daio_mgr_enb_dai,
2310     .daio_mgr_dsb_dai = daio_mgr_dsb_dai,
2311     .daio_mgr_enb_dao = daio_mgr_enb_dao,
2312     .daio_mgr_dsb_dao = daio_mgr_dsb_dao,
2313     .daio_mgr_dao_init = daio_mgr_dao_init,
2314     .daio_mgr_set_imaparc = daio_mgr_set_imaparc,
2315     .daio_mgr_set_imapnxt = daio_mgr_set_imapnxt,
2316     .daio_mgr_set_imapaddr = daio_mgr_set_imapaddr,
2317     .daio_mgr_commit_write = daio_mgr_commit_write,
2318 
2319     .set_timer_irq = set_timer_irq,
2320     .set_timer_tick = set_timer_tick,
2321     .get_wc = get_wc,
2322 };
2323 
2324 int create_20k2_hw_obj(struct hw **rhw)
2325 {
2326     struct hw20k2 *hw20k2;
2327 
2328     *rhw = NULL;
2329     hw20k2 = kzalloc(sizeof(*hw20k2), GFP_KERNEL);
2330     if (!hw20k2)
2331         return -ENOMEM;
2332 
2333     hw20k2->hw = ct20k2_preset;
2334     *rhw = &hw20k2->hw;
2335 
2336     return 0;
2337 }
2338 
2339 int destroy_20k2_hw_obj(struct hw *hw)
2340 {
2341     if (hw->io_base)
2342         hw_card_shutdown(hw);
2343 
2344     kfree(hw);
2345     return 0;
2346 }