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