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