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