Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+
0002  *
0003  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0004  *  Universal interface for Audio Codec '97
0005  *
0006  *  For more details look to AC '97 component specification revision 2.1
0007  *  by Intel Corporation (http://developer.intel.com).
0008  */
0009 
0010 #ifndef __SOUND_AC97_CODEC_H
0011 #define __SOUND_AC97_CODEC_H
0012 
0013 #include <linux/bitops.h>
0014 #include <linux/device.h>
0015 #include <linux/workqueue.h>
0016 #include <sound/ac97/regs.h>
0017 #include <sound/pcm.h>
0018 #include <sound/control.h>
0019 #include <sound/info.h>
0020 
0021 /* maximum number of devices on the AC97 bus */
0022 #define AC97_BUS_MAX_DEVICES    4
0023 
0024 /* specific - SigmaTel */
0025 #define AC97_SIGMATEL_OUTSEL    0x64    /* Output Select, STAC9758 */
0026 #define AC97_SIGMATEL_INSEL 0x66    /* Input Select, STAC9758 */
0027 #define AC97_SIGMATEL_IOMISC    0x68    /* STAC9758 */
0028 #define AC97_SIGMATEL_ANALOG    0x6c    /* Analog Special */
0029 #define AC97_SIGMATEL_DAC2INVERT 0x6e
0030 #define AC97_SIGMATEL_BIAS1 0x70
0031 #define AC97_SIGMATEL_BIAS2 0x72
0032 #define AC97_SIGMATEL_VARIOUS   0x72    /* STAC9758 */
0033 #define AC97_SIGMATEL_MULTICHN  0x74    /* Multi-Channel programming */
0034 #define AC97_SIGMATEL_CIC1  0x76
0035 #define AC97_SIGMATEL_CIC2  0x78
0036 
0037 /* specific - Analog Devices */
0038 #define AC97_AD_TEST        0x5a    /* test register */
0039 #define AC97_AD_TEST2       0x5c    /* undocumented test register 2 */
0040 #define AC97_AD_HPFD_SHIFT  12  /* High Pass Filter Disable */
0041 #define AC97_AD_CODEC_CFG   0x70    /* codec configuration */
0042 #define AC97_AD_JACK_SPDIF  0x72    /* Jack Sense & S/PDIF */
0043 #define AC97_AD_SERIAL_CFG  0x74    /* Serial Configuration */
0044 #define AC97_AD_MISC        0x76    /* Misc Control Bits */
0045 #define AC97_AD_VREFD_SHIFT 2   /* V_REFOUT Disable (AD1888) */
0046 
0047 /* specific - Cirrus Logic */
0048 #define AC97_CSR_ACMODE     0x5e    /* AC Mode Register */
0049 #define AC97_CSR_MISC_CRYSTAL   0x60    /* Misc Crystal Control */
0050 #define AC97_CSR_SPDIF      0x68    /* S/PDIF Register */
0051 #define AC97_CSR_SERIAL     0x6a    /* Serial Port Control */
0052 #define AC97_CSR_SPECF_ADDR 0x6c    /* Special Feature Address */
0053 #define AC97_CSR_SPECF_DATA 0x6e    /* Special Feature Data */
0054 #define AC97_CSR_BDI_STATUS 0x7a    /* BDI Status */
0055 
0056 /* specific - Conexant */
0057 #define AC97_CXR_AUDIO_MISC 0x5c
0058 #define AC97_CXR_SPDIFEN    (1<<3)
0059 #define AC97_CXR_COPYRGT    (1<<2)
0060 #define AC97_CXR_SPDIF_MASK (3<<0)
0061 #define AC97_CXR_SPDIF_PCM  0x0
0062 #define AC97_CXR_SPDIF_AC3  0x2
0063 
0064 /* specific - ALC */
0065 #define AC97_ALC650_SPDIF_INPUT_STATUS1 0x60
0066 /* S/PDIF input status 1 bit defines */
0067 #define AC97_ALC650_PRO             0x0001  /* Professional status */
0068 #define AC97_ALC650_NAUDIO          0x0002  /* Non audio stream */
0069 #define AC97_ALC650_COPY            0x0004  /* Copyright status */
0070 #define AC97_ALC650_PRE             0x0038  /* Preemphasis status */
0071 #define AC97_ALC650_PRE_SHIFT       3
0072 #define AC97_ALC650_MODE            0x00C0  /* Preemphasis status */
0073 #define AC97_ALC650_MODE_SHIFT      6
0074 #define AC97_ALC650_CC_MASK         0x7f00  /* Category Code mask */
0075 #define AC97_ALC650_CC_SHIFT        8
0076 #define AC97_ALC650_L               0x8000  /* Generation Level status */
0077 
0078 #define AC97_ALC650_SPDIF_INPUT_STATUS2 0x62
0079 /* S/PDIF input status 2 bit defines */
0080 #define AC97_ALC650_SOUCE_MASK      0x000f  /* Source number */
0081 #define AC97_ALC650_CHANNEL_MASK    0x00f0  /* Channel number */
0082 #define AC97_ALC650_CHANNEL_SHIFT   4 
0083 #define AC97_ALC650_SPSR_MASK       0x0f00  /* S/PDIF Sample Rate bits */
0084 #define AC97_ALC650_SPSR_SHIFT      8
0085 #define AC97_ALC650_SPSR_44K        0x0000  /* Use 44.1kHz Sample rate */
0086 #define AC97_ALC650_SPSR_48K        0x0200  /* Use 48kHz Sample rate */
0087 #define AC97_ALC650_SPSR_32K        0x0300  /* Use 32kHz Sample rate */
0088 #define AC97_ALC650_CLOCK_ACCURACY  0x3000  /* Clock accuracy */
0089 #define AC97_ALC650_CLOCK_SHIFT     12
0090 #define AC97_ALC650_CLOCK_LOCK      0x4000  /* Clock locked status */
0091 #define AC97_ALC650_V               0x8000  /* Validity status */
0092 
0093 #define AC97_ALC650_SURR_DAC_VOL    0x64
0094 #define AC97_ALC650_LFE_DAC_VOL     0x66
0095 #define AC97_ALC650_UNKNOWN1        0x68
0096 #define AC97_ALC650_MULTICH     0x6a
0097 #define AC97_ALC650_UNKNOWN2        0x6c
0098 #define AC97_ALC650_REVISION        0x6e
0099 #define AC97_ALC650_UNKNOWN3        0x70
0100 #define AC97_ALC650_UNKNOWN4        0x72
0101 #define AC97_ALC650_MISC        0x74
0102 #define AC97_ALC650_GPIO_SETUP      0x76
0103 #define AC97_ALC650_GPIO_STATUS     0x78
0104 #define AC97_ALC650_CLOCK       0x7a
0105 
0106 /* specific - Yamaha YMF7x3 */
0107 #define AC97_YMF7X3_DIT_CTRL    0x66    /* DIT Control (YMF743) / 2 (YMF753) */
0108 #define AC97_YMF7X3_3D_MODE_SEL 0x68    /* 3D Mode Select */
0109 
0110 /* specific - C-Media */
0111 #define AC97_CM9738_VENDOR_CTRL 0x5a
0112 #define AC97_CM9739_MULTI_CHAN  0x64
0113 #define AC97_CM9739_SPDIF_IN_STATUS 0x68 /* 32bit */
0114 #define AC97_CM9739_SPDIF_CTRL  0x6c
0115 
0116 /* specific - wolfson */
0117 #define AC97_WM97XX_FMIXER_VOL  0x72
0118 #define AC97_WM9704_RMIXER_VOL  0x74
0119 #define AC97_WM9704_TEST        0x5a
0120 #define AC97_WM9704_RPCM_VOL    0x70
0121 #define AC97_WM9711_OUT3VOL     0x16
0122 
0123 
0124 /* ac97->scaps */
0125 #define AC97_SCAP_AUDIO     (1<<0)  /* audio codec 97 */
0126 #define AC97_SCAP_MODEM     (1<<1)  /* modem codec 97 */
0127 #define AC97_SCAP_SURROUND_DAC  (1<<2)  /* surround L&R DACs are present */
0128 #define AC97_SCAP_CENTER_LFE_DAC (1<<3) /* center and LFE DACs are present */
0129 #define AC97_SCAP_SKIP_AUDIO    (1<<4)  /* skip audio part of codec */
0130 #define AC97_SCAP_SKIP_MODEM    (1<<5)  /* skip modem part of codec */
0131 #define AC97_SCAP_INDEP_SDIN    (1<<6)  /* independent SDIN */
0132 #define AC97_SCAP_INV_EAPD  (1<<7)  /* inverted EAPD */
0133 #define AC97_SCAP_DETECT_BY_VENDOR (1<<8) /* use vendor registers for read tests */
0134 #define AC97_SCAP_NO_SPDIF  (1<<9)  /* don't build SPDIF controls */
0135 #define AC97_SCAP_EAPD_LED  (1<<10) /* EAPD as mute LED */
0136 #define AC97_SCAP_POWER_SAVE    (1<<11) /* capable for aggressive power-saving */
0137 
0138 /* ac97->flags */
0139 #define AC97_HAS_PC_BEEP    (1<<0)  /* force PC Speaker usage */
0140 #define AC97_AD_MULTI       (1<<1)  /* Analog Devices - multi codecs */
0141 #define AC97_CS_SPDIF       (1<<2)  /* Cirrus Logic uses funky SPDIF */
0142 #define AC97_CX_SPDIF       (1<<3)  /* Conexant's spdif interface */
0143 #define AC97_STEREO_MUTES   (1<<4)  /* has stereo mute bits */
0144 #define AC97_DOUBLE_RATE    (1<<5)  /* supports double rate playback */
0145 #define AC97_HAS_NO_MASTER_VOL  (1<<6)  /* no Master volume */
0146 #define AC97_HAS_NO_PCM_VOL (1<<7)  /* no PCM volume */
0147 #define AC97_DEFAULT_POWER_OFF  (1<<8)  /* no RESET write */
0148 #define AC97_MODEM_PATCH    (1<<9)  /* modem patch */
0149 #define AC97_HAS_NO_REC_GAIN    (1<<10) /* no Record gain */
0150 #define AC97_HAS_NO_PHONE   (1<<11) /* no PHONE volume */
0151 #define AC97_HAS_NO_PC_BEEP (1<<12) /* no PC Beep volume */
0152 #define AC97_HAS_NO_VIDEO   (1<<13) /* no Video volume */
0153 #define AC97_HAS_NO_CD      (1<<14) /* no CD volume */
0154 #define AC97_HAS_NO_MIC (1<<15) /* no MIC volume */
0155 #define AC97_HAS_NO_TONE    (1<<16) /* no Tone volume */
0156 #define AC97_HAS_NO_STD_PCM (1<<17) /* no standard AC97 PCM volume and mute */
0157 #define AC97_HAS_NO_AUX     (1<<18) /* no standard AC97 AUX volume and mute */
0158 #define AC97_HAS_8CH        (1<<19) /* supports 8-channel output */
0159 
0160 /* rates indexes */
0161 #define AC97_RATES_FRONT_DAC    0
0162 #define AC97_RATES_SURR_DAC 1
0163 #define AC97_RATES_LFE_DAC  2
0164 #define AC97_RATES_ADC      3
0165 #define AC97_RATES_MIC_ADC  4
0166 #define AC97_RATES_SPDIF    5
0167 
0168 #define AC97_NUM_GPIOS      16
0169 /*
0170  *
0171  */
0172 
0173 struct snd_ac97;
0174 struct snd_ac97_gpio_priv;
0175 struct snd_pcm_chmap;
0176 
0177 struct snd_ac97_build_ops {
0178     int (*build_3d) (struct snd_ac97 *ac97);
0179     int (*build_specific) (struct snd_ac97 *ac97);
0180     int (*build_spdif) (struct snd_ac97 *ac97);
0181     int (*build_post_spdif) (struct snd_ac97 *ac97);
0182 #ifdef CONFIG_PM
0183     void (*suspend) (struct snd_ac97 *ac97);
0184     void (*resume) (struct snd_ac97 *ac97);
0185 #endif
0186     void (*update_jacks) (struct snd_ac97 *ac97);   /* for jack-sharing */
0187 };
0188 
0189 struct snd_ac97_bus_ops {
0190     void (*reset) (struct snd_ac97 *ac97);
0191     void (*warm_reset)(struct snd_ac97 *ac97);
0192     void (*write) (struct snd_ac97 *ac97, unsigned short reg, unsigned short val);
0193     unsigned short (*read) (struct snd_ac97 *ac97, unsigned short reg);
0194     void (*wait) (struct snd_ac97 *ac97);
0195     void (*init) (struct snd_ac97 *ac97);
0196 };
0197 
0198 struct snd_ac97_bus {
0199     /* -- lowlevel (hardware) driver specific -- */
0200     const struct snd_ac97_bus_ops *ops;
0201     void *private_data;
0202     void (*private_free) (struct snd_ac97_bus *bus);
0203     /* --- */
0204     struct snd_card *card;
0205     unsigned short num; /* bus number */
0206     unsigned short no_vra: 1, /* bridge doesn't support VRA */
0207                dra: 1,  /* bridge supports double rate */
0208                isdin: 1;/* independent SDIN */
0209     unsigned int clock; /* AC'97 base clock (usually 48000Hz) */
0210     spinlock_t bus_lock;    /* used mainly for slot allocation */
0211     unsigned short used_slots[2][4]; /* actually used PCM slots */
0212     unsigned short pcms_count; /* count of PCMs */
0213     struct ac97_pcm *pcms;
0214     struct snd_ac97 *codec[4];
0215     struct snd_info_entry *proc;
0216 };
0217 
0218 /* static resolution table */
0219 struct snd_ac97_res_table {
0220     unsigned short reg; /* register */
0221     unsigned short bits;    /* resolution bitmask */
0222 };
0223 
0224 struct snd_ac97_template {
0225     void *private_data;
0226     void (*private_free) (struct snd_ac97 *ac97);
0227     struct pci_dev *pci;    /* assigned PCI device - used for quirks */
0228     unsigned short num; /* number of codec: 0 = primary, 1 = secondary */
0229     unsigned short addr;    /* physical address of codec [0-3] */
0230     unsigned int scaps; /* driver capabilities */
0231     const struct snd_ac97_res_table *res_table; /* static resolution */
0232 };
0233 
0234 struct snd_ac97 {
0235     /* -- lowlevel (hardware) driver specific -- */
0236     const struct snd_ac97_build_ops *build_ops;
0237     void *private_data;
0238     void (*private_free) (struct snd_ac97 *ac97);
0239     /* --- */
0240     struct snd_ac97_bus *bus;
0241     struct pci_dev *pci;    /* assigned PCI device - used for quirks */
0242     struct snd_info_entry *proc;
0243     struct snd_info_entry *proc_regs;
0244     unsigned short subsystem_vendor;
0245     unsigned short subsystem_device;
0246     struct mutex reg_mutex;
0247     struct mutex page_mutex;    /* mutex for AD18xx multi-codecs and paging (2.3) */
0248     unsigned short num; /* number of codec: 0 = primary, 1 = secondary */
0249     unsigned short addr;    /* physical address of codec [0-3] */
0250     unsigned int id;    /* identification of codec */
0251     unsigned short caps;    /* capabilities (register 0) */
0252     unsigned short ext_id;  /* extended feature identification (register 28) */
0253     unsigned short ext_mid; /* extended modem ID (register 3C) */
0254     const struct snd_ac97_res_table *res_table; /* static resolution */
0255     unsigned int scaps; /* driver capabilities */
0256     unsigned int flags; /* specific code */
0257     unsigned int rates[6];  /* see AC97_RATES_* defines */
0258     unsigned int spdif_status;
0259     unsigned short regs[0x80]; /* register cache */
0260     DECLARE_BITMAP(reg_accessed, 0x80); /* bit flags */
0261     union {         /* vendor specific code */
0262         struct {
0263             unsigned short unchained[3];    // 0 = C34, 1 = C79, 2 = C69
0264             unsigned short chained[3];  // 0 = C34, 1 = C79, 2 = C69
0265             unsigned short id[3];       // codec IDs (lower 16-bit word)
0266             unsigned short pcmreg[3];   // PCM registers
0267             unsigned short codec_cfg[3];    // CODEC_CFG bits
0268             unsigned char swap_mic_linein;  // AD1986/AD1986A only
0269             unsigned char lo_as_master; /* LO as master */
0270         } ad18xx;
0271         unsigned int dev_flags;     /* device specific */
0272     } spec;
0273     /* jack-sharing info */
0274     unsigned char indep_surround;
0275     unsigned char channel_mode;
0276 
0277 #ifdef CONFIG_SND_AC97_POWER_SAVE
0278     unsigned int power_up;  /* power states */
0279     struct delayed_work power_work;
0280 #endif
0281     struct device dev;
0282     struct snd_ac97_gpio_priv *gpio_priv;
0283 
0284     struct snd_pcm_chmap *chmaps[2]; /* channel-maps (optional) */
0285 };
0286 
0287 #define to_ac97_t(d) container_of(d, struct snd_ac97, dev)
0288 
0289 /* conditions */
0290 static inline int ac97_is_audio(struct snd_ac97 * ac97)
0291 {
0292     return (ac97->scaps & AC97_SCAP_AUDIO);
0293 }
0294 static inline int ac97_is_modem(struct snd_ac97 * ac97)
0295 {
0296     return (ac97->scaps & AC97_SCAP_MODEM);
0297 }
0298 static inline int ac97_is_rev22(struct snd_ac97 * ac97)
0299 {
0300     return (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_22;
0301 }
0302 static inline int ac97_can_amap(struct snd_ac97 * ac97)
0303 {
0304     return (ac97->ext_id & AC97_EI_AMAP) != 0;
0305 }
0306 static inline int ac97_can_spdif(struct snd_ac97 * ac97)
0307 {
0308     return (ac97->ext_id & AC97_EI_SPDIF) != 0;
0309 }
0310 
0311 /* functions */
0312 /* create new AC97 bus */
0313 int snd_ac97_bus(struct snd_card *card, int num,
0314          const struct snd_ac97_bus_ops *ops,
0315          void *private_data, struct snd_ac97_bus **rbus);
0316 /* create mixer controls */
0317 int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template,
0318            struct snd_ac97 **rac97);
0319 const char *snd_ac97_get_short_name(struct snd_ac97 *ac97);
0320 
0321 void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value);
0322 unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg);
0323 void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value);
0324 int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value);
0325 int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value);
0326 #ifdef CONFIG_SND_AC97_POWER_SAVE
0327 int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup);
0328 #else
0329 static inline int snd_ac97_update_power(struct snd_ac97 *ac97, int reg,
0330                     int powerup)
0331 {
0332     return 0;
0333 }
0334 #endif
0335 #ifdef CONFIG_PM
0336 void snd_ac97_suspend(struct snd_ac97 *ac97);
0337 void snd_ac97_resume(struct snd_ac97 *ac97);
0338 #endif
0339 int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
0340     unsigned int id_mask);
0341 
0342 /* quirk types */
0343 enum {
0344     AC97_TUNE_DEFAULT = -1, /* use default from quirk list (not valid in list) */
0345     AC97_TUNE_NONE = 0, /* nothing extra to do */
0346     AC97_TUNE_HP_ONLY,  /* headphone (true line-out) control as master only */
0347     AC97_TUNE_SWAP_HP,  /* swap headphone and master controls */
0348     AC97_TUNE_SWAP_SURROUND, /* swap master and surround controls */
0349     AC97_TUNE_AD_SHARING,   /* for AD1985, turn on OMS bit and use headphone */
0350     AC97_TUNE_ALC_JACK, /* for Realtek, enable JACK detection */
0351     AC97_TUNE_INV_EAPD, /* inverted EAPD implementation */
0352     AC97_TUNE_MUTE_LED, /* EAPD bit works as mute LED */
0353     AC97_TUNE_HP_MUTE_LED,  /* EAPD bit works as mute LED, use headphone control as master */
0354 };
0355 
0356 struct ac97_quirk {
0357     unsigned short subvendor; /* PCI subsystem vendor id */
0358     unsigned short subdevice; /* PCI subsystem device id */
0359     unsigned short mask;    /* device id bit mask, 0 = accept all */
0360     unsigned int codec_id;  /* codec id (if any), 0 = accept all */
0361     const char *name;   /* name shown as info */
0362     int type;       /* quirk type above */
0363 };
0364 
0365 int snd_ac97_tune_hardware(struct snd_ac97 *ac97,
0366                const struct ac97_quirk *quirk,
0367                const char *override);
0368 int snd_ac97_set_rate(struct snd_ac97 *ac97, int reg, unsigned int rate);
0369 
0370 /*
0371  * PCM allocation
0372  */
0373 
0374 enum ac97_pcm_cfg {
0375     AC97_PCM_CFG_FRONT = 2,
0376     AC97_PCM_CFG_REAR = 10,     /* alias surround */
0377     AC97_PCM_CFG_LFE = 11,      /* center + lfe */
0378     AC97_PCM_CFG_40 = 4,        /* front + rear */
0379     AC97_PCM_CFG_51 = 6,        /* front + rear + center/lfe */
0380     AC97_PCM_CFG_SPDIF = 20
0381 };
0382 
0383 struct ac97_pcm {
0384     struct snd_ac97_bus *bus;
0385     unsigned int stream: 1,        /* stream type: 1 = capture */
0386              exclusive: 1,     /* exclusive mode, don't override with other pcms */
0387              copy_flag: 1,     /* lowlevel driver must fill all entries */
0388              spdif: 1;         /* spdif pcm */
0389     unsigned short aslots;         /* active slots */
0390     unsigned short cur_dbl;        /* current double-rate state */
0391     unsigned int rates;        /* available rates */
0392     struct {
0393         unsigned short slots;      /* driver input: requested AC97 slot numbers */
0394         unsigned short rslots[4];  /* allocated slots per codecs */
0395         unsigned char rate_table[4];
0396         struct snd_ac97 *codec[4];     /* allocated codecs */
0397     } r[2];                /* 0 = standard rates, 1 = double rates */
0398     unsigned long private_value;       /* used by the hardware driver */
0399 };
0400 
0401 int snd_ac97_pcm_assign(struct snd_ac97_bus *ac97,
0402             unsigned short pcms_count,
0403             const struct ac97_pcm *pcms);
0404 int snd_ac97_pcm_open(struct ac97_pcm *pcm, unsigned int rate,
0405               enum ac97_pcm_cfg cfg, unsigned short slots);
0406 int snd_ac97_pcm_close(struct ac97_pcm *pcm);
0407 int snd_ac97_pcm_double_rate_rules(struct snd_pcm_runtime *runtime);
0408 
0409 /* ad hoc AC97 device driver access */
0410 extern struct bus_type ac97_bus_type;
0411 
0412 /* AC97 platform_data adding function */
0413 static inline void snd_ac97_dev_add_pdata(struct snd_ac97 *ac97, void *data)
0414 {
0415     ac97->dev.platform_data = data;
0416 }
0417 
0418 #endif /* __SOUND_AC97_CODEC_H */