Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: sound/soc/codecs/ad1938.c |
| 3 | * Author: Barry Song <Barry.Song@analog.com> |
| 4 | * |
| 5 | * Created: June 04 2009 |
| 6 | * Description: Driver for AD1938 sound chip |
| 7 | * |
| 8 | * Modified: |
| 9 | * Copyright 2009 Analog Devices Inc. |
| 10 | * |
| 11 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, see the file COPYING, or write |
| 25 | * to the Free Software Foundation, Inc., |
| 26 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | */ |
| 28 | |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/module.h> |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 31 | #include <linux/kernel.h> |
| 32 | #include <linux/device.h> |
| 33 | #include <sound/core.h> |
| 34 | #include <sound/pcm.h> |
| 35 | #include <sound/pcm_params.h> |
| 36 | #include <sound/initval.h> |
| 37 | #include <sound/soc.h> |
| 38 | #include <sound/tlv.h> |
| 39 | #include <sound/soc-dapm.h> |
| 40 | #include <linux/spi/spi.h> |
| 41 | #include "ad1938.h" |
| 42 | |
| 43 | /* codec private data */ |
| 44 | struct ad1938_priv { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 45 | struct snd_soc_codec codec; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 46 | u8 reg_cache[AD1938_NUM_REGS]; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 47 | }; |
| 48 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 49 | /* ad1938 register cache & default register settings */ |
| 50 | static const u8 ad1938_reg[AD1938_NUM_REGS] = { |
| 51 | 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, |
| 52 | }; |
| 53 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 54 | static struct snd_soc_codec *ad1938_codec; |
| 55 | struct snd_soc_codec_device soc_codec_dev_ad1938; |
| 56 | static int ad1938_register(struct ad1938_priv *ad1938); |
| 57 | static void ad1938_unregister(struct ad1938_priv *ad1938); |
| 58 | |
| 59 | /* |
| 60 | * AD1938 volume/mute/de-emphasis etc. controls |
| 61 | */ |
| 62 | static const char *ad1938_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"}; |
| 63 | |
| 64 | static const struct soc_enum ad1938_deemp_enum = |
| 65 | SOC_ENUM_SINGLE(AD1938_DAC_CTRL2, 1, 4, ad1938_deemp); |
| 66 | |
| 67 | static const struct snd_kcontrol_new ad1938_snd_controls[] = { |
| 68 | /* DAC volume control */ |
| 69 | SOC_DOUBLE_R("DAC1 Volume", AD1938_DAC_L1_VOL, |
| 70 | AD1938_DAC_R1_VOL, 0, 0xFF, 1), |
| 71 | SOC_DOUBLE_R("DAC2 Volume", AD1938_DAC_L2_VOL, |
| 72 | AD1938_DAC_R2_VOL, 0, 0xFF, 1), |
| 73 | SOC_DOUBLE_R("DAC3 Volume", AD1938_DAC_L3_VOL, |
| 74 | AD1938_DAC_R3_VOL, 0, 0xFF, 1), |
| 75 | SOC_DOUBLE_R("DAC4 Volume", AD1938_DAC_L4_VOL, |
| 76 | AD1938_DAC_R4_VOL, 0, 0xFF, 1), |
| 77 | |
| 78 | /* ADC switch control */ |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 79 | SOC_DOUBLE("ADC1 Switch", AD1938_ADC_CTRL0, AD1938_ADCL1_MUTE, |
| 80 | AD1938_ADCR1_MUTE, 1, 1), |
| 81 | SOC_DOUBLE("ADC2 Switch", AD1938_ADC_CTRL0, AD1938_ADCL2_MUTE, |
| 82 | AD1938_ADCR2_MUTE, 1, 1), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 83 | |
| 84 | /* DAC switch control */ |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 85 | SOC_DOUBLE("DAC1 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL1_MUTE, |
| 86 | AD1938_DACR1_MUTE, 1, 1), |
| 87 | SOC_DOUBLE("DAC2 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL2_MUTE, |
| 88 | AD1938_DACR2_MUTE, 1, 1), |
| 89 | SOC_DOUBLE("DAC3 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL3_MUTE, |
| 90 | AD1938_DACR3_MUTE, 1, 1), |
| 91 | SOC_DOUBLE("DAC4 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL4_MUTE, |
| 92 | AD1938_DACR4_MUTE, 1, 1), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 93 | |
| 94 | /* ADC high-pass filter */ |
| 95 | SOC_SINGLE("ADC High Pass Filter Switch", AD1938_ADC_CTRL0, |
| 96 | AD1938_ADC_HIGHPASS_FILTER, 1, 0), |
| 97 | |
| 98 | /* DAC de-emphasis */ |
| 99 | SOC_ENUM("Playback Deemphasis", ad1938_deemp_enum), |
| 100 | }; |
| 101 | |
| 102 | static const struct snd_soc_dapm_widget ad1938_dapm_widgets[] = { |
| 103 | SND_SOC_DAPM_DAC("DAC", "Playback", AD1938_DAC_CTRL0, 0, 1), |
| 104 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), |
Barry Song | 5b61735 | 2009-12-25 14:10:08 +0800 | [diff] [blame] | 105 | SND_SOC_DAPM_SUPPLY("PLL_PWR", AD1938_PLL_CLK_CTRL0, 0, 1, NULL, 0), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 106 | SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1938_ADC_CTRL0, 0, 1, NULL, 0), |
Barry Song | c4ff357ad | 2009-08-12 11:34:25 +0800 | [diff] [blame] | 107 | SND_SOC_DAPM_OUTPUT("DAC1OUT"), |
| 108 | SND_SOC_DAPM_OUTPUT("DAC2OUT"), |
| 109 | SND_SOC_DAPM_OUTPUT("DAC3OUT"), |
| 110 | SND_SOC_DAPM_OUTPUT("DAC4OUT"), |
| 111 | SND_SOC_DAPM_INPUT("ADC1IN"), |
| 112 | SND_SOC_DAPM_INPUT("ADC2IN"), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static const struct snd_soc_dapm_route audio_paths[] = { |
Barry Song | 5b61735 | 2009-12-25 14:10:08 +0800 | [diff] [blame] | 116 | { "DAC", NULL, "PLL_PWR" }, |
| 117 | { "ADC", NULL, "PLL_PWR" }, |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 118 | { "DAC", NULL, "ADC_PWR" }, |
| 119 | { "ADC", NULL, "ADC_PWR" }, |
Barry Song | c4ff357ad | 2009-08-12 11:34:25 +0800 | [diff] [blame] | 120 | { "DAC1OUT", "DAC1 Switch", "DAC" }, |
| 121 | { "DAC2OUT", "DAC2 Switch", "DAC" }, |
| 122 | { "DAC3OUT", "DAC3 Switch", "DAC" }, |
| 123 | { "DAC4OUT", "DAC4 Switch", "DAC" }, |
| 124 | { "ADC", "ADC1 Switch", "ADC1IN" }, |
| 125 | { "ADC", "ADC2 Switch", "ADC2IN" }, |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * DAI ops entries |
| 130 | */ |
| 131 | |
| 132 | static int ad1938_mute(struct snd_soc_dai *dai, int mute) |
| 133 | { |
| 134 | struct snd_soc_codec *codec = dai->codec; |
| 135 | int reg; |
| 136 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 137 | reg = snd_soc_read(codec, AD1938_DAC_CTRL2); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 138 | reg = (mute > 0) ? reg | AD1938_DAC_MASTER_MUTE : reg & |
| 139 | (~AD1938_DAC_MASTER_MUTE); |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 140 | snd_soc_write(codec, AD1938_DAC_CTRL2, reg); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Mark Brown | d2a3821 | 2009-08-12 14:30:33 +0100 | [diff] [blame] | 145 | static int ad1938_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, |
Barry Song | 08ba864 | 2009-12-25 14:10:07 +0800 | [diff] [blame] | 146 | unsigned int rx_mask, int slots, int width) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 147 | { |
| 148 | struct snd_soc_codec *codec = dai->codec; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 149 | int dac_reg = snd_soc_read(codec, AD1938_DAC_CTRL1); |
| 150 | int adc_reg = snd_soc_read(codec, AD1938_ADC_CTRL2); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 151 | |
| 152 | dac_reg &= ~AD1938_DAC_CHAN_MASK; |
| 153 | adc_reg &= ~AD1938_ADC_CHAN_MASK; |
| 154 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 155 | switch (slots) { |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 156 | case 2: |
| 157 | dac_reg |= AD1938_DAC_2_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 158 | adc_reg |= AD1938_ADC_2_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 159 | break; |
| 160 | case 4: |
| 161 | dac_reg |= AD1938_DAC_4_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 162 | adc_reg |= AD1938_ADC_4_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 163 | break; |
| 164 | case 8: |
| 165 | dac_reg |= AD1938_DAC_8_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 166 | adc_reg |= AD1938_ADC_8_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 167 | break; |
| 168 | case 16: |
| 169 | dac_reg |= AD1938_DAC_16_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 170 | adc_reg |= AD1938_ADC_16_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 171 | break; |
| 172 | default: |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 176 | snd_soc_write(codec, AD1938_DAC_CTRL1, dac_reg); |
| 177 | snd_soc_write(codec, AD1938_ADC_CTRL2, adc_reg); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int ad1938_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 183 | unsigned int fmt) |
| 184 | { |
| 185 | struct snd_soc_codec *codec = codec_dai->codec; |
| 186 | int adc_reg, dac_reg; |
| 187 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 188 | adc_reg = snd_soc_read(codec, AD1938_ADC_CTRL2); |
| 189 | dac_reg = snd_soc_read(codec, AD1938_DAC_CTRL1); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 190 | |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 191 | /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S |
| 192 | * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 193 | */ |
| 194 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 195 | case SND_SOC_DAIFMT_I2S: |
| 196 | adc_reg &= ~AD1938_ADC_SERFMT_MASK; |
| 197 | adc_reg |= AD1938_ADC_SERFMT_TDM; |
| 198 | break; |
| 199 | case SND_SOC_DAIFMT_DSP_A: |
| 200 | adc_reg &= ~AD1938_ADC_SERFMT_MASK; |
| 201 | adc_reg |= AD1938_ADC_SERFMT_AUX; |
| 202 | break; |
| 203 | default: |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 208 | case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ |
| 209 | adc_reg &= ~AD1938_ADC_LEFT_HIGH; |
| 210 | adc_reg &= ~AD1938_ADC_BCLK_INV; |
| 211 | dac_reg &= ~AD1938_DAC_LEFT_HIGH; |
| 212 | dac_reg &= ~AD1938_DAC_BCLK_INV; |
| 213 | break; |
| 214 | case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ |
| 215 | adc_reg |= AD1938_ADC_LEFT_HIGH; |
| 216 | adc_reg &= ~AD1938_ADC_BCLK_INV; |
| 217 | dac_reg |= AD1938_DAC_LEFT_HIGH; |
| 218 | dac_reg &= ~AD1938_DAC_BCLK_INV; |
| 219 | break; |
| 220 | case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ |
| 221 | adc_reg &= ~AD1938_ADC_LEFT_HIGH; |
| 222 | adc_reg |= AD1938_ADC_BCLK_INV; |
| 223 | dac_reg &= ~AD1938_DAC_LEFT_HIGH; |
| 224 | dac_reg |= AD1938_DAC_BCLK_INV; |
| 225 | break; |
| 226 | |
| 227 | case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ |
| 228 | adc_reg |= AD1938_ADC_LEFT_HIGH; |
| 229 | adc_reg |= AD1938_ADC_BCLK_INV; |
| 230 | dac_reg |= AD1938_DAC_LEFT_HIGH; |
| 231 | dac_reg |= AD1938_DAC_BCLK_INV; |
| 232 | break; |
| 233 | default: |
| 234 | return -EINVAL; |
| 235 | } |
| 236 | |
| 237 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 238 | case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ |
| 239 | adc_reg |= AD1938_ADC_LCR_MASTER; |
| 240 | adc_reg |= AD1938_ADC_BCLK_MASTER; |
| 241 | dac_reg |= AD1938_DAC_LCR_MASTER; |
| 242 | dac_reg |= AD1938_DAC_BCLK_MASTER; |
| 243 | break; |
| 244 | case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ |
| 245 | adc_reg |= AD1938_ADC_LCR_MASTER; |
| 246 | adc_reg &= ~AD1938_ADC_BCLK_MASTER; |
| 247 | dac_reg |= AD1938_DAC_LCR_MASTER; |
| 248 | dac_reg &= ~AD1938_DAC_BCLK_MASTER; |
| 249 | break; |
| 250 | case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ |
| 251 | adc_reg &= ~AD1938_ADC_LCR_MASTER; |
| 252 | adc_reg |= AD1938_ADC_BCLK_MASTER; |
| 253 | dac_reg &= ~AD1938_DAC_LCR_MASTER; |
| 254 | dac_reg |= AD1938_DAC_BCLK_MASTER; |
| 255 | break; |
| 256 | case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ |
| 257 | adc_reg &= ~AD1938_ADC_LCR_MASTER; |
| 258 | adc_reg &= ~AD1938_ADC_BCLK_MASTER; |
| 259 | dac_reg &= ~AD1938_DAC_LCR_MASTER; |
| 260 | dac_reg &= ~AD1938_DAC_BCLK_MASTER; |
| 261 | break; |
| 262 | default: |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 266 | snd_soc_write(codec, AD1938_ADC_CTRL2, adc_reg); |
| 267 | snd_soc_write(codec, AD1938_DAC_CTRL1, dac_reg); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int ad1938_hw_params(struct snd_pcm_substream *substream, |
| 273 | struct snd_pcm_hw_params *params, |
| 274 | struct snd_soc_dai *dai) |
| 275 | { |
| 276 | int word_len = 0, reg = 0; |
| 277 | |
| 278 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 279 | struct snd_soc_device *socdev = rtd->socdev; |
| 280 | struct snd_soc_codec *codec = socdev->card->codec; |
| 281 | |
| 282 | /* bit size */ |
| 283 | switch (params_format(params)) { |
| 284 | case SNDRV_PCM_FORMAT_S16_LE: |
| 285 | word_len = 3; |
| 286 | break; |
| 287 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 288 | word_len = 1; |
| 289 | break; |
| 290 | case SNDRV_PCM_FORMAT_S24_LE: |
| 291 | case SNDRV_PCM_FORMAT_S32_LE: |
| 292 | word_len = 0; |
| 293 | break; |
| 294 | } |
| 295 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 296 | reg = snd_soc_read(codec, AD1938_DAC_CTRL2); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 297 | reg = (reg & (~AD1938_DAC_WORD_LEN_MASK)) | word_len; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 298 | snd_soc_write(codec, AD1938_DAC_CTRL2, reg); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 299 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 300 | reg = snd_soc_read(codec, AD1938_ADC_CTRL1); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 301 | reg = (reg & (~AD1938_ADC_WORD_LEN_MASK)) | word_len; |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 302 | snd_soc_write(codec, AD1938_ADC_CTRL1, reg); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 303 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 304 | return 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int __devinit ad1938_spi_probe(struct spi_device *spi) |
| 308 | { |
| 309 | struct snd_soc_codec *codec; |
| 310 | struct ad1938_priv *ad1938; |
| 311 | |
| 312 | ad1938 = kzalloc(sizeof(struct ad1938_priv), GFP_KERNEL); |
| 313 | if (ad1938 == NULL) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | codec = &ad1938->codec; |
| 317 | codec->control_data = spi; |
| 318 | codec->dev = &spi->dev; |
| 319 | |
Takashi Iwai | 416c8fe | 2009-07-17 07:48:03 +0200 | [diff] [blame] | 320 | dev_set_drvdata(&spi->dev, ad1938); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 321 | |
| 322 | return ad1938_register(ad1938); |
| 323 | } |
| 324 | |
| 325 | static int __devexit ad1938_spi_remove(struct spi_device *spi) |
| 326 | { |
Takashi Iwai | 416c8fe | 2009-07-17 07:48:03 +0200 | [diff] [blame] | 327 | struct ad1938_priv *ad1938 = dev_get_drvdata(&spi->dev); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 328 | |
| 329 | ad1938_unregister(ad1938); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static struct spi_driver ad1938_spi_driver = { |
| 334 | .driver = { |
Barry Song | 2a70813 | 2009-08-14 12:06:38 +0800 | [diff] [blame] | 335 | .name = "ad1938", |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 336 | .owner = THIS_MODULE, |
| 337 | }, |
| 338 | .probe = ad1938_spi_probe, |
| 339 | .remove = __devexit_p(ad1938_spi_remove), |
| 340 | }; |
| 341 | |
| 342 | static struct snd_soc_dai_ops ad1938_dai_ops = { |
| 343 | .hw_params = ad1938_hw_params, |
| 344 | .digital_mute = ad1938_mute, |
| 345 | .set_tdm_slot = ad1938_set_tdm_slot, |
| 346 | .set_fmt = ad1938_set_dai_fmt, |
| 347 | }; |
| 348 | |
| 349 | /* codec DAI instance */ |
| 350 | struct snd_soc_dai ad1938_dai = { |
| 351 | .name = "AD1938", |
| 352 | .playback = { |
| 353 | .stream_name = "Playback", |
| 354 | .channels_min = 2, |
| 355 | .channels_max = 8, |
| 356 | .rates = SNDRV_PCM_RATE_48000, |
| 357 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 358 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 359 | }, |
| 360 | .capture = { |
| 361 | .stream_name = "Capture", |
| 362 | .channels_min = 2, |
| 363 | .channels_max = 4, |
| 364 | .rates = SNDRV_PCM_RATE_48000, |
| 365 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 366 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 367 | }, |
| 368 | .ops = &ad1938_dai_ops, |
| 369 | }; |
| 370 | EXPORT_SYMBOL_GPL(ad1938_dai); |
| 371 | |
| 372 | static int ad1938_register(struct ad1938_priv *ad1938) |
| 373 | { |
| 374 | int ret; |
| 375 | struct snd_soc_codec *codec = &ad1938->codec; |
| 376 | |
| 377 | if (ad1938_codec) { |
| 378 | dev_err(codec->dev, "Another ad1938 is registered\n"); |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | mutex_init(&codec->mutex); |
| 383 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 384 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 385 | codec->private_data = ad1938; |
| 386 | codec->reg_cache = ad1938->reg_cache; |
| 387 | codec->reg_cache_size = AD1938_NUM_REGS; |
| 388 | codec->name = "AD1938"; |
| 389 | codec->owner = THIS_MODULE; |
| 390 | codec->dai = &ad1938_dai; |
| 391 | codec->num_dai = 1; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 392 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 393 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 394 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 395 | ad1938_dai.dev = codec->dev; |
| 396 | ad1938_codec = codec; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 397 | |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 398 | memcpy(codec->reg_cache, ad1938_reg, AD1938_NUM_REGS); |
| 399 | |
| 400 | ret = snd_soc_codec_set_cache_io(codec, 16, 8, SND_SOC_SPI); |
| 401 | if (ret < 0) { |
| 402 | dev_err(codec->dev, "failed to set cache I/O: %d\n", |
| 403 | ret); |
| 404 | kfree(ad1938); |
| 405 | return ret; |
| 406 | } |
| 407 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 408 | /* default setting for ad1938 */ |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 409 | |
| 410 | /* unmute dac channels */ |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 411 | snd_soc_write(codec, AD1938_DAC_CHNL_MUTE, 0x0); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 412 | /* de-emphasis: 48kHz, powedown dac */ |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 413 | snd_soc_write(codec, AD1938_DAC_CTRL2, 0x1A); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 414 | /* powerdown dac, dac in tdm mode */ |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 415 | snd_soc_write(codec, AD1938_DAC_CTRL0, 0x41); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 416 | /* high-pass filter enable */ |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 417 | snd_soc_write(codec, AD1938_ADC_CTRL0, 0x3); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 418 | /* sata delay=1, adc aux mode */ |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 419 | snd_soc_write(codec, AD1938_ADC_CTRL1, 0x43); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 420 | /* pll input: mclki/xi */ |
Barry Song | 994dc42 | 2010-01-27 11:46:18 +0800 | [diff] [blame] | 421 | snd_soc_write(codec, AD1938_PLL_CLK_CTRL0, 0x9D); |
| 422 | snd_soc_write(codec, AD1938_PLL_CLK_CTRL1, 0x04); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 423 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 424 | ret = snd_soc_register_codec(codec); |
| 425 | if (ret != 0) { |
| 426 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
Barry Song | 2a70813 | 2009-08-14 12:06:38 +0800 | [diff] [blame] | 427 | kfree(ad1938); |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 428 | return ret; |
| 429 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 430 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 431 | ret = snd_soc_register_dai(&ad1938_dai); |
| 432 | if (ret != 0) { |
| 433 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); |
| 434 | snd_soc_unregister_codec(codec); |
Barry Song | 2a70813 | 2009-08-14 12:06:38 +0800 | [diff] [blame] | 435 | kfree(ad1938); |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 436 | return ret; |
| 437 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 438 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 439 | return 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void ad1938_unregister(struct ad1938_priv *ad1938) |
| 443 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 444 | snd_soc_unregister_dai(&ad1938_dai); |
| 445 | snd_soc_unregister_codec(&ad1938->codec); |
| 446 | kfree(ad1938); |
| 447 | ad1938_codec = NULL; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static int ad1938_probe(struct platform_device *pdev) |
| 451 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 452 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 453 | struct snd_soc_codec *codec; |
| 454 | int ret = 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 455 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 456 | if (ad1938_codec == NULL) { |
| 457 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 458 | return -ENODEV; |
| 459 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 460 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 461 | socdev->card->codec = ad1938_codec; |
| 462 | codec = ad1938_codec; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 463 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 464 | /* register pcms */ |
| 465 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 466 | if (ret < 0) { |
| 467 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
| 468 | goto pcm_err; |
| 469 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 470 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 471 | snd_soc_add_controls(codec, ad1938_snd_controls, |
| 472 | ARRAY_SIZE(ad1938_snd_controls)); |
| 473 | snd_soc_dapm_new_controls(codec, ad1938_dapm_widgets, |
| 474 | ARRAY_SIZE(ad1938_dapm_widgets)); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 475 | snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths)); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 476 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 477 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 478 | pcm_err: |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 479 | return ret; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* power down chip */ |
| 483 | static int ad1938_remove(struct platform_device *pdev) |
| 484 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 485 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 486 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 487 | snd_soc_free_pcms(socdev); |
| 488 | snd_soc_dapm_free(socdev); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 489 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 490 | return 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 491 | } |
| 492 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 493 | struct snd_soc_codec_device soc_codec_dev_ad1938 = { |
| 494 | .probe = ad1938_probe, |
| 495 | .remove = ad1938_remove, |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 496 | }; |
| 497 | EXPORT_SYMBOL_GPL(soc_codec_dev_ad1938); |
| 498 | |
| 499 | static int __init ad1938_init(void) |
| 500 | { |
| 501 | int ret; |
| 502 | |
| 503 | ret = spi_register_driver(&ad1938_spi_driver); |
| 504 | if (ret != 0) { |
| 505 | printk(KERN_ERR "Failed to register ad1938 SPI driver: %d\n", |
| 506 | ret); |
| 507 | } |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | module_init(ad1938_init); |
| 512 | |
| 513 | static void __exit ad1938_exit(void) |
| 514 | { |
| 515 | spi_unregister_driver(&ad1938_spi_driver); |
| 516 | } |
| 517 | module_exit(ad1938_exit); |
| 518 | |
| 519 | MODULE_DESCRIPTION("ASoC ad1938 driver"); |
| 520 | MODULE_AUTHOR("Barry Song "); |
| 521 | MODULE_LICENSE("GPL"); |