Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * AD193X Audio Codec driver supporting AD1936/7/8/9 |
| 3 | * |
| 4 | * Copyright 2010 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/spi/spi.h> |
Stephen Rothwell | 1b132ea | 2010-03-29 15:32:18 +1100 | [diff] [blame] | 15 | #include <linux/slab.h> |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 16 | #include <sound/core.h> |
| 17 | #include <sound/pcm.h> |
| 18 | #include <sound/pcm_params.h> |
| 19 | #include <sound/initval.h> |
| 20 | #include <sound/soc.h> |
| 21 | #include <sound/tlv.h> |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 22 | #include "ad193x.h" |
| 23 | |
| 24 | /* codec private data */ |
| 25 | struct ad193x_priv { |
Scott Jiang | 119bfef | 2011-03-26 03:52:20 -0400 | [diff] [blame] | 26 | enum snd_soc_control_type control_type; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 27 | int sysclk; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /* ad193x register cache & default register settings */ |
| 31 | static const u8 ad193x_reg[AD193X_NUM_REGS] = { |
| 32 | 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, |
| 33 | }; |
| 34 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 35 | /* |
| 36 | * AD193X volume/mute/de-emphasis etc. controls |
| 37 | */ |
| 38 | static const char *ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"}; |
| 39 | |
| 40 | static const struct soc_enum ad193x_deemp_enum = |
| 41 | SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp); |
| 42 | |
| 43 | static const struct snd_kcontrol_new ad193x_snd_controls[] = { |
| 44 | /* DAC volume control */ |
Barry Song | ba0a24e | 2010-04-21 17:36:49 +0800 | [diff] [blame] | 45 | SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 46 | AD193X_DAC_R1_VOL, 0, 0xFF, 1), |
Barry Song | ba0a24e | 2010-04-21 17:36:49 +0800 | [diff] [blame] | 47 | SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 48 | AD193X_DAC_R2_VOL, 0, 0xFF, 1), |
Barry Song | ba0a24e | 2010-04-21 17:36:49 +0800 | [diff] [blame] | 49 | SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 50 | AD193X_DAC_R3_VOL, 0, 0xFF, 1), |
Barry Song | ba0a24e | 2010-04-21 17:36:49 +0800 | [diff] [blame] | 51 | SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 52 | AD193X_DAC_R4_VOL, 0, 0xFF, 1), |
| 53 | |
| 54 | /* ADC switch control */ |
| 55 | SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE, |
| 56 | AD193X_ADCR1_MUTE, 1, 1), |
| 57 | SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE, |
| 58 | AD193X_ADCR2_MUTE, 1, 1), |
| 59 | |
| 60 | /* DAC switch control */ |
| 61 | SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE, |
| 62 | AD193X_DACR1_MUTE, 1, 1), |
| 63 | SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE, |
| 64 | AD193X_DACR2_MUTE, 1, 1), |
| 65 | SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE, |
| 66 | AD193X_DACR3_MUTE, 1, 1), |
| 67 | SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE, |
| 68 | AD193X_DACR4_MUTE, 1, 1), |
| 69 | |
| 70 | /* ADC high-pass filter */ |
| 71 | SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0, |
| 72 | AD193X_ADC_HIGHPASS_FILTER, 1, 0), |
| 73 | |
| 74 | /* DAC de-emphasis */ |
| 75 | SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum), |
| 76 | }; |
| 77 | |
| 78 | static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = { |
| 79 | SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0, 0, 1), |
| 80 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), |
| 81 | SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0), |
| 82 | SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0), |
| 83 | SND_SOC_DAPM_OUTPUT("DAC1OUT"), |
| 84 | SND_SOC_DAPM_OUTPUT("DAC2OUT"), |
| 85 | SND_SOC_DAPM_OUTPUT("DAC3OUT"), |
| 86 | SND_SOC_DAPM_OUTPUT("DAC4OUT"), |
| 87 | SND_SOC_DAPM_INPUT("ADC1IN"), |
| 88 | SND_SOC_DAPM_INPUT("ADC2IN"), |
| 89 | }; |
| 90 | |
| 91 | static const struct snd_soc_dapm_route audio_paths[] = { |
| 92 | { "DAC", NULL, "PLL_PWR" }, |
| 93 | { "ADC", NULL, "PLL_PWR" }, |
| 94 | { "DAC", NULL, "ADC_PWR" }, |
| 95 | { "ADC", NULL, "ADC_PWR" }, |
| 96 | { "DAC1OUT", "DAC1 Switch", "DAC" }, |
| 97 | { "DAC2OUT", "DAC2 Switch", "DAC" }, |
| 98 | { "DAC3OUT", "DAC3 Switch", "DAC" }, |
| 99 | { "DAC4OUT", "DAC4 Switch", "DAC" }, |
| 100 | { "ADC", "ADC1 Switch", "ADC1IN" }, |
| 101 | { "ADC", "ADC2 Switch", "ADC2IN" }, |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * DAI ops entries |
| 106 | */ |
| 107 | |
| 108 | static int ad193x_mute(struct snd_soc_dai *dai, int mute) |
| 109 | { |
| 110 | struct snd_soc_codec *codec = dai->codec; |
| 111 | int reg; |
| 112 | |
| 113 | reg = snd_soc_read(codec, AD193X_DAC_CTRL2); |
| 114 | reg = (mute > 0) ? reg | AD193X_DAC_MASTER_MUTE : reg & |
| 115 | (~AD193X_DAC_MASTER_MUTE); |
| 116 | snd_soc_write(codec, AD193X_DAC_CTRL2, reg); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, |
| 122 | unsigned int rx_mask, int slots, int width) |
| 123 | { |
| 124 | struct snd_soc_codec *codec = dai->codec; |
| 125 | int dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1); |
| 126 | int adc_reg = snd_soc_read(codec, AD193X_ADC_CTRL2); |
| 127 | |
| 128 | dac_reg &= ~AD193X_DAC_CHAN_MASK; |
| 129 | adc_reg &= ~AD193X_ADC_CHAN_MASK; |
| 130 | |
| 131 | switch (slots) { |
| 132 | case 2: |
| 133 | dac_reg |= AD193X_DAC_2_CHANNELS << AD193X_DAC_CHAN_SHFT; |
| 134 | adc_reg |= AD193X_ADC_2_CHANNELS << AD193X_ADC_CHAN_SHFT; |
| 135 | break; |
| 136 | case 4: |
| 137 | dac_reg |= AD193X_DAC_4_CHANNELS << AD193X_DAC_CHAN_SHFT; |
| 138 | adc_reg |= AD193X_ADC_4_CHANNELS << AD193X_ADC_CHAN_SHFT; |
| 139 | break; |
| 140 | case 8: |
| 141 | dac_reg |= AD193X_DAC_8_CHANNELS << AD193X_DAC_CHAN_SHFT; |
| 142 | adc_reg |= AD193X_ADC_8_CHANNELS << AD193X_ADC_CHAN_SHFT; |
| 143 | break; |
| 144 | case 16: |
| 145 | dac_reg |= AD193X_DAC_16_CHANNELS << AD193X_DAC_CHAN_SHFT; |
| 146 | adc_reg |= AD193X_ADC_16_CHANNELS << AD193X_ADC_CHAN_SHFT; |
| 147 | break; |
| 148 | default: |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg); |
| 153 | snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 159 | unsigned int fmt) |
| 160 | { |
| 161 | struct snd_soc_codec *codec = codec_dai->codec; |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 162 | int adc_reg1, adc_reg2, dac_reg; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 163 | |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 164 | adc_reg1 = snd_soc_read(codec, AD193X_ADC_CTRL1); |
| 165 | adc_reg2 = snd_soc_read(codec, AD193X_ADC_CTRL2); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 166 | dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1); |
| 167 | |
| 168 | /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S |
| 169 | * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) |
| 170 | */ |
| 171 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 172 | case SND_SOC_DAIFMT_I2S: |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 173 | adc_reg1 &= ~AD193X_ADC_SERFMT_MASK; |
| 174 | adc_reg1 |= AD193X_ADC_SERFMT_TDM; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 175 | break; |
| 176 | case SND_SOC_DAIFMT_DSP_A: |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 177 | adc_reg1 &= ~AD193X_ADC_SERFMT_MASK; |
| 178 | adc_reg1 |= AD193X_ADC_SERFMT_AUX; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 179 | break; |
| 180 | default: |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 185 | case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 186 | adc_reg2 &= ~AD193X_ADC_LEFT_HIGH; |
| 187 | adc_reg2 &= ~AD193X_ADC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 188 | dac_reg &= ~AD193X_DAC_LEFT_HIGH; |
| 189 | dac_reg &= ~AD193X_DAC_BCLK_INV; |
| 190 | break; |
| 191 | case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 192 | adc_reg2 |= AD193X_ADC_LEFT_HIGH; |
| 193 | adc_reg2 &= ~AD193X_ADC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 194 | dac_reg |= AD193X_DAC_LEFT_HIGH; |
| 195 | dac_reg &= ~AD193X_DAC_BCLK_INV; |
| 196 | break; |
| 197 | case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 198 | adc_reg2 &= ~AD193X_ADC_LEFT_HIGH; |
| 199 | adc_reg2 |= AD193X_ADC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 200 | dac_reg &= ~AD193X_DAC_LEFT_HIGH; |
| 201 | dac_reg |= AD193X_DAC_BCLK_INV; |
| 202 | break; |
| 203 | |
| 204 | case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 205 | adc_reg2 |= AD193X_ADC_LEFT_HIGH; |
| 206 | adc_reg2 |= AD193X_ADC_BCLK_INV; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 207 | dac_reg |= AD193X_DAC_LEFT_HIGH; |
| 208 | dac_reg |= AD193X_DAC_BCLK_INV; |
| 209 | break; |
| 210 | default: |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 215 | case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 216 | adc_reg2 |= AD193X_ADC_LCR_MASTER; |
| 217 | adc_reg2 |= AD193X_ADC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 218 | dac_reg |= AD193X_DAC_LCR_MASTER; |
| 219 | dac_reg |= AD193X_DAC_BCLK_MASTER; |
| 220 | break; |
| 221 | case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 222 | adc_reg2 |= AD193X_ADC_LCR_MASTER; |
| 223 | adc_reg2 &= ~AD193X_ADC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 224 | dac_reg |= AD193X_DAC_LCR_MASTER; |
| 225 | dac_reg &= ~AD193X_DAC_BCLK_MASTER; |
| 226 | break; |
| 227 | case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 228 | adc_reg2 &= ~AD193X_ADC_LCR_MASTER; |
| 229 | adc_reg2 |= AD193X_ADC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 230 | dac_reg &= ~AD193X_DAC_LCR_MASTER; |
| 231 | dac_reg |= AD193X_DAC_BCLK_MASTER; |
| 232 | break; |
| 233 | case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 234 | adc_reg2 &= ~AD193X_ADC_LCR_MASTER; |
| 235 | adc_reg2 &= ~AD193X_ADC_BCLK_MASTER; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 236 | dac_reg &= ~AD193X_DAC_LCR_MASTER; |
| 237 | dac_reg &= ~AD193X_DAC_BCLK_MASTER; |
| 238 | break; |
| 239 | default: |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
Barry Song | d6bdc0f | 2010-04-21 17:36:48 +0800 | [diff] [blame] | 243 | snd_soc_write(codec, AD193X_ADC_CTRL1, adc_reg1); |
| 244 | snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg2); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 245 | snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 250 | static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 251 | int clk_id, unsigned int freq, int dir) |
| 252 | { |
| 253 | struct snd_soc_codec *codec = codec_dai->codec; |
| 254 | struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec); |
| 255 | switch (freq) { |
| 256 | case 12288000: |
| 257 | case 18432000: |
| 258 | case 24576000: |
| 259 | case 36864000: |
| 260 | ad193x->sysclk = freq; |
| 261 | return 0; |
| 262 | } |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 266 | static int ad193x_hw_params(struct snd_pcm_substream *substream, |
| 267 | struct snd_pcm_hw_params *params, |
| 268 | struct snd_soc_dai *dai) |
| 269 | { |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 270 | int word_len = 0, reg = 0, master_rate = 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 271 | |
| 272 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 273 | struct snd_soc_codec *codec = rtd->codec; |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 274 | struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 275 | |
| 276 | /* bit size */ |
| 277 | switch (params_format(params)) { |
| 278 | case SNDRV_PCM_FORMAT_S16_LE: |
| 279 | word_len = 3; |
| 280 | break; |
| 281 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 282 | word_len = 1; |
| 283 | break; |
| 284 | case SNDRV_PCM_FORMAT_S24_LE: |
| 285 | case SNDRV_PCM_FORMAT_S32_LE: |
| 286 | word_len = 0; |
| 287 | break; |
| 288 | } |
| 289 | |
Barry Song | fab90aa | 2010-05-21 11:57:01 +0800 | [diff] [blame] | 290 | switch (ad193x->sysclk) { |
| 291 | case 12288000: |
| 292 | master_rate = AD193X_PLL_INPUT_256; |
| 293 | break; |
| 294 | case 18432000: |
| 295 | master_rate = AD193X_PLL_INPUT_384; |
| 296 | break; |
| 297 | case 24576000: |
| 298 | master_rate = AD193X_PLL_INPUT_512; |
| 299 | break; |
| 300 | case 36864000: |
| 301 | master_rate = AD193X_PLL_INPUT_768; |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | reg = snd_soc_read(codec, AD193X_PLL_CLK_CTRL0); |
| 306 | reg = (reg & AD193X_PLL_INPUT_MASK) | master_rate; |
| 307 | snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, reg); |
| 308 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 309 | reg = snd_soc_read(codec, AD193X_DAC_CTRL2); |
| 310 | reg = (reg & (~AD193X_DAC_WORD_LEN_MASK)) | word_len; |
| 311 | snd_soc_write(codec, AD193X_DAC_CTRL2, reg); |
| 312 | |
| 313 | reg = snd_soc_read(codec, AD193X_ADC_CTRL1); |
| 314 | reg = (reg & (~AD193X_ADC_WORD_LEN_MASK)) | word_len; |
| 315 | snd_soc_write(codec, AD193X_ADC_CTRL1, reg); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 320 | static struct snd_soc_dai_ops ad193x_dai_ops = { |
| 321 | .hw_params = ad193x_hw_params, |
| 322 | .digital_mute = ad193x_mute, |
| 323 | .set_tdm_slot = ad193x_set_tdm_slot, |
| 324 | .set_sysclk = ad193x_set_dai_sysclk, |
| 325 | .set_fmt = ad193x_set_dai_fmt, |
| 326 | }; |
| 327 | |
| 328 | /* codec DAI instance */ |
| 329 | static struct snd_soc_dai_driver ad193x_dai = { |
| 330 | .name = "ad193x-hifi", |
| 331 | .playback = { |
| 332 | .stream_name = "Playback", |
| 333 | .channels_min = 2, |
| 334 | .channels_max = 8, |
| 335 | .rates = SNDRV_PCM_RATE_48000, |
| 336 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 337 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 338 | }, |
| 339 | .capture = { |
| 340 | .stream_name = "Capture", |
| 341 | .channels_min = 2, |
| 342 | .channels_max = 4, |
| 343 | .rates = SNDRV_PCM_RATE_48000, |
| 344 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 345 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 346 | }, |
| 347 | .ops = &ad193x_dai_ops, |
| 348 | }; |
| 349 | |
| 350 | static int ad193x_probe(struct snd_soc_codec *codec) |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 351 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 352 | struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 353 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Barry Song | 9dd7b79 | 2010-03-29 11:16:00 +0800 | [diff] [blame] | 354 | int ret; |
| 355 | |
Scott Jiang | 119bfef | 2011-03-26 03:52:20 -0400 | [diff] [blame] | 356 | if (ad193x->control_type == SND_SOC_I2C) |
| 357 | ret = snd_soc_codec_set_cache_io(codec, 8, 8, ad193x->control_type); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 358 | else |
Scott Jiang | 119bfef | 2011-03-26 03:52:20 -0400 | [diff] [blame] | 359 | ret = snd_soc_codec_set_cache_io(codec, 16, 8, ad193x->control_type); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 360 | if (ret < 0) { |
Scott Jiang | 119bfef | 2011-03-26 03:52:20 -0400 | [diff] [blame] | 361 | dev_err(codec->dev, "failed to set cache I/O: %d\n", ret); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /* default setting for ad193x */ |
| 366 | |
| 367 | /* unmute dac channels */ |
| 368 | snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0); |
| 369 | /* de-emphasis: 48kHz, powedown dac */ |
| 370 | snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A); |
| 371 | /* powerdown dac, dac in tdm mode */ |
| 372 | snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41); |
| 373 | /* high-pass filter enable */ |
| 374 | snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3); |
| 375 | /* sata delay=1, adc aux mode */ |
| 376 | snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43); |
| 377 | /* pll input: mclki/xi */ |
| 378 | snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */ |
| 379 | snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 380 | |
| 381 | snd_soc_add_controls(codec, ad193x_snd_controls, |
| 382 | ARRAY_SIZE(ad193x_snd_controls)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 383 | snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets, |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 384 | ARRAY_SIZE(ad193x_dapm_widgets)); |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 385 | snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths)); |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 386 | |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 387 | return ret; |
| 388 | } |
| 389 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 390 | static struct snd_soc_codec_driver soc_codec_dev_ad193x = { |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 391 | .probe = ad193x_probe, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 392 | .reg_cache_default = ad193x_reg, |
| 393 | .reg_cache_size = AD193X_NUM_REGS, |
| 394 | .reg_word_size = sizeof(u16), |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 395 | }; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 396 | |
| 397 | #if defined(CONFIG_SPI_MASTER) |
| 398 | static int __devinit ad193x_spi_probe(struct spi_device *spi) |
| 399 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 400 | struct ad193x_priv *ad193x; |
| 401 | int ret; |
| 402 | |
| 403 | ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL); |
| 404 | if (ad193x == NULL) |
| 405 | return -ENOMEM; |
| 406 | |
| 407 | spi_set_drvdata(spi, ad193x); |
Scott Jiang | 119bfef | 2011-03-26 03:52:20 -0400 | [diff] [blame] | 408 | ad193x->control_type = SND_SOC_SPI; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 409 | |
| 410 | ret = snd_soc_register_codec(&spi->dev, |
| 411 | &soc_codec_dev_ad193x, &ad193x_dai, 1); |
| 412 | if (ret < 0) |
| 413 | kfree(ad193x); |
| 414 | return ret; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static int __devexit ad193x_spi_remove(struct spi_device *spi) |
| 418 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 419 | snd_soc_unregister_codec(&spi->dev); |
| 420 | kfree(spi_get_drvdata(spi)); |
| 421 | return 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static struct spi_driver ad193x_spi_driver = { |
| 425 | .driver = { |
Mike Frysinger | e43a7d4 | 2011-03-27 00:44:12 -0400 | [diff] [blame] | 426 | .name = "ad193x", |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 427 | .owner = THIS_MODULE, |
| 428 | }, |
| 429 | .probe = ad193x_spi_probe, |
| 430 | .remove = __devexit_p(ad193x_spi_remove), |
| 431 | }; |
| 432 | #endif |
| 433 | |
| 434 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 435 | static const struct i2c_device_id ad193x_id[] = { |
| 436 | { "ad1936", 0 }, |
| 437 | { "ad1937", 0 }, |
| 438 | { } |
| 439 | }; |
| 440 | MODULE_DEVICE_TABLE(i2c, ad193x_id); |
| 441 | |
| 442 | static int __devinit ad193x_i2c_probe(struct i2c_client *client, |
| 443 | const struct i2c_device_id *id) |
| 444 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 445 | struct ad193x_priv *ad193x; |
| 446 | int ret; |
| 447 | |
| 448 | ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL); |
| 449 | if (ad193x == NULL) |
| 450 | return -ENOMEM; |
| 451 | |
| 452 | i2c_set_clientdata(client, ad193x); |
Scott Jiang | 119bfef | 2011-03-26 03:52:20 -0400 | [diff] [blame] | 453 | ad193x->control_type = SND_SOC_I2C; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 454 | |
| 455 | ret = snd_soc_register_codec(&client->dev, |
| 456 | &soc_codec_dev_ad193x, &ad193x_dai, 1); |
| 457 | if (ret < 0) |
| 458 | kfree(ad193x); |
| 459 | return ret; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int __devexit ad193x_i2c_remove(struct i2c_client *client) |
| 463 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 464 | snd_soc_unregister_codec(&client->dev); |
| 465 | kfree(i2c_get_clientdata(client)); |
| 466 | return 0; |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static struct i2c_driver ad193x_i2c_driver = { |
| 470 | .driver = { |
Mike Frysinger | e43a7d4 | 2011-03-27 00:44:12 -0400 | [diff] [blame] | 471 | .name = "ad193x", |
Barry Song | a1533d9 | 2010-03-19 14:48:33 +0800 | [diff] [blame] | 472 | }, |
| 473 | .probe = ad193x_i2c_probe, |
| 474 | .remove = __devexit_p(ad193x_i2c_remove), |
| 475 | .id_table = ad193x_id, |
| 476 | }; |
| 477 | #endif |
| 478 | |
| 479 | static int __init ad193x_modinit(void) |
| 480 | { |
| 481 | int ret; |
| 482 | |
| 483 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 484 | ret = i2c_add_driver(&ad193x_i2c_driver); |
| 485 | if (ret != 0) { |
| 486 | printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n", |
| 487 | ret); |
| 488 | } |
| 489 | #endif |
| 490 | |
| 491 | #if defined(CONFIG_SPI_MASTER) |
| 492 | ret = spi_register_driver(&ad193x_spi_driver); |
| 493 | if (ret != 0) { |
| 494 | printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n", |
| 495 | ret); |
| 496 | } |
| 497 | #endif |
| 498 | return ret; |
| 499 | } |
| 500 | module_init(ad193x_modinit); |
| 501 | |
| 502 | static void __exit ad193x_modexit(void) |
| 503 | { |
| 504 | #if defined(CONFIG_SPI_MASTER) |
| 505 | spi_unregister_driver(&ad193x_spi_driver); |
| 506 | #endif |
| 507 | |
| 508 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 509 | i2c_del_driver(&ad193x_i2c_driver); |
| 510 | #endif |
| 511 | } |
| 512 | module_exit(ad193x_modexit); |
| 513 | |
| 514 | MODULE_DESCRIPTION("ASoC ad193x driver"); |
| 515 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 516 | MODULE_LICENSE("GPL"); |