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 | |
| 49 | static struct snd_soc_codec *ad1938_codec; |
| 50 | struct snd_soc_codec_device soc_codec_dev_ad1938; |
| 51 | static int ad1938_register(struct ad1938_priv *ad1938); |
| 52 | static void ad1938_unregister(struct ad1938_priv *ad1938); |
| 53 | |
| 54 | /* |
| 55 | * AD1938 volume/mute/de-emphasis etc. controls |
| 56 | */ |
| 57 | static const char *ad1938_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"}; |
| 58 | |
| 59 | static const struct soc_enum ad1938_deemp_enum = |
| 60 | SOC_ENUM_SINGLE(AD1938_DAC_CTRL2, 1, 4, ad1938_deemp); |
| 61 | |
| 62 | static const struct snd_kcontrol_new ad1938_snd_controls[] = { |
| 63 | /* DAC volume control */ |
| 64 | SOC_DOUBLE_R("DAC1 Volume", AD1938_DAC_L1_VOL, |
| 65 | AD1938_DAC_R1_VOL, 0, 0xFF, 1), |
| 66 | SOC_DOUBLE_R("DAC2 Volume", AD1938_DAC_L2_VOL, |
| 67 | AD1938_DAC_R2_VOL, 0, 0xFF, 1), |
| 68 | SOC_DOUBLE_R("DAC3 Volume", AD1938_DAC_L3_VOL, |
| 69 | AD1938_DAC_R3_VOL, 0, 0xFF, 1), |
| 70 | SOC_DOUBLE_R("DAC4 Volume", AD1938_DAC_L4_VOL, |
| 71 | AD1938_DAC_R4_VOL, 0, 0xFF, 1), |
| 72 | |
| 73 | /* ADC switch control */ |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 74 | SOC_DOUBLE("ADC1 Switch", AD1938_ADC_CTRL0, AD1938_ADCL1_MUTE, |
| 75 | AD1938_ADCR1_MUTE, 1, 1), |
| 76 | SOC_DOUBLE("ADC2 Switch", AD1938_ADC_CTRL0, AD1938_ADCL2_MUTE, |
| 77 | AD1938_ADCR2_MUTE, 1, 1), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 78 | |
| 79 | /* DAC switch control */ |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 80 | SOC_DOUBLE("DAC1 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL1_MUTE, |
| 81 | AD1938_DACR1_MUTE, 1, 1), |
| 82 | SOC_DOUBLE("DAC2 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL2_MUTE, |
| 83 | AD1938_DACR2_MUTE, 1, 1), |
| 84 | SOC_DOUBLE("DAC3 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL3_MUTE, |
| 85 | AD1938_DACR3_MUTE, 1, 1), |
| 86 | SOC_DOUBLE("DAC4 Switch", AD1938_DAC_CHNL_MUTE, AD1938_DACL4_MUTE, |
| 87 | AD1938_DACR4_MUTE, 1, 1), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 88 | |
| 89 | /* ADC high-pass filter */ |
| 90 | SOC_SINGLE("ADC High Pass Filter Switch", AD1938_ADC_CTRL0, |
| 91 | AD1938_ADC_HIGHPASS_FILTER, 1, 0), |
| 92 | |
| 93 | /* DAC de-emphasis */ |
| 94 | SOC_ENUM("Playback Deemphasis", ad1938_deemp_enum), |
| 95 | }; |
| 96 | |
| 97 | static const struct snd_soc_dapm_widget ad1938_dapm_widgets[] = { |
| 98 | SND_SOC_DAPM_DAC("DAC", "Playback", AD1938_DAC_CTRL0, 0, 1), |
| 99 | SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0), |
Barry Song | 5b61735 | 2009-12-25 14:10:08 +0800 | [diff] [blame^] | 100 | 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] | 101 | 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] | 102 | SND_SOC_DAPM_OUTPUT("DAC1OUT"), |
| 103 | SND_SOC_DAPM_OUTPUT("DAC2OUT"), |
| 104 | SND_SOC_DAPM_OUTPUT("DAC3OUT"), |
| 105 | SND_SOC_DAPM_OUTPUT("DAC4OUT"), |
| 106 | SND_SOC_DAPM_INPUT("ADC1IN"), |
| 107 | SND_SOC_DAPM_INPUT("ADC2IN"), |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static const struct snd_soc_dapm_route audio_paths[] = { |
Barry Song | 5b61735 | 2009-12-25 14:10:08 +0800 | [diff] [blame^] | 111 | { "DAC", NULL, "PLL_PWR" }, |
| 112 | { "ADC", NULL, "PLL_PWR" }, |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 113 | { "DAC", NULL, "ADC_PWR" }, |
| 114 | { "ADC", NULL, "ADC_PWR" }, |
Barry Song | c4ff357ad | 2009-08-12 11:34:25 +0800 | [diff] [blame] | 115 | { "DAC1OUT", "DAC1 Switch", "DAC" }, |
| 116 | { "DAC2OUT", "DAC2 Switch", "DAC" }, |
| 117 | { "DAC3OUT", "DAC3 Switch", "DAC" }, |
| 118 | { "DAC4OUT", "DAC4 Switch", "DAC" }, |
| 119 | { "ADC", "ADC1 Switch", "ADC1IN" }, |
| 120 | { "ADC", "ADC2 Switch", "ADC2IN" }, |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /* |
| 124 | * DAI ops entries |
| 125 | */ |
| 126 | |
| 127 | static int ad1938_mute(struct snd_soc_dai *dai, int mute) |
| 128 | { |
| 129 | struct snd_soc_codec *codec = dai->codec; |
| 130 | int reg; |
| 131 | |
| 132 | reg = codec->read(codec, AD1938_DAC_CTRL2); |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 133 | reg = (mute > 0) ? reg | AD1938_DAC_MASTER_MUTE : reg & |
| 134 | (~AD1938_DAC_MASTER_MUTE); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 135 | codec->write(codec, AD1938_DAC_CTRL2, reg); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Mark Brown | d2a3821 | 2009-08-12 14:30:33 +0100 | [diff] [blame] | 140 | 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] | 141 | unsigned int rx_mask, int slots, int width) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 142 | { |
| 143 | struct snd_soc_codec *codec = dai->codec; |
| 144 | int dac_reg = codec->read(codec, AD1938_DAC_CTRL1); |
| 145 | int adc_reg = codec->read(codec, AD1938_ADC_CTRL2); |
| 146 | |
| 147 | dac_reg &= ~AD1938_DAC_CHAN_MASK; |
| 148 | adc_reg &= ~AD1938_ADC_CHAN_MASK; |
| 149 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 150 | switch (slots) { |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 151 | case 2: |
| 152 | dac_reg |= AD1938_DAC_2_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 153 | adc_reg |= AD1938_ADC_2_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 154 | break; |
| 155 | case 4: |
| 156 | dac_reg |= AD1938_DAC_4_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 157 | adc_reg |= AD1938_ADC_4_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 158 | break; |
| 159 | case 8: |
| 160 | dac_reg |= AD1938_DAC_8_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 161 | adc_reg |= AD1938_ADC_8_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 162 | break; |
| 163 | case 16: |
| 164 | dac_reg |= AD1938_DAC_16_CHANNELS << AD1938_DAC_CHAN_SHFT; |
| 165 | adc_reg |= AD1938_ADC_16_CHANNELS << AD1938_ADC_CHAN_SHFT; |
| 166 | break; |
| 167 | default: |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
| 171 | codec->write(codec, AD1938_DAC_CTRL1, dac_reg); |
| 172 | codec->write(codec, AD1938_ADC_CTRL2, adc_reg); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int ad1938_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 178 | unsigned int fmt) |
| 179 | { |
| 180 | struct snd_soc_codec *codec = codec_dai->codec; |
| 181 | int adc_reg, dac_reg; |
| 182 | |
| 183 | adc_reg = codec->read(codec, AD1938_ADC_CTRL2); |
| 184 | dac_reg = codec->read(codec, AD1938_DAC_CTRL1); |
| 185 | |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 186 | /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S |
| 187 | * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 188 | */ |
| 189 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 190 | case SND_SOC_DAIFMT_I2S: |
| 191 | adc_reg &= ~AD1938_ADC_SERFMT_MASK; |
| 192 | adc_reg |= AD1938_ADC_SERFMT_TDM; |
| 193 | break; |
| 194 | case SND_SOC_DAIFMT_DSP_A: |
| 195 | adc_reg &= ~AD1938_ADC_SERFMT_MASK; |
| 196 | adc_reg |= AD1938_ADC_SERFMT_AUX; |
| 197 | break; |
| 198 | default: |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
| 202 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 203 | case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */ |
| 204 | adc_reg &= ~AD1938_ADC_LEFT_HIGH; |
| 205 | adc_reg &= ~AD1938_ADC_BCLK_INV; |
| 206 | dac_reg &= ~AD1938_DAC_LEFT_HIGH; |
| 207 | dac_reg &= ~AD1938_DAC_BCLK_INV; |
| 208 | break; |
| 209 | case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */ |
| 210 | adc_reg |= AD1938_ADC_LEFT_HIGH; |
| 211 | adc_reg &= ~AD1938_ADC_BCLK_INV; |
| 212 | dac_reg |= AD1938_DAC_LEFT_HIGH; |
| 213 | dac_reg &= ~AD1938_DAC_BCLK_INV; |
| 214 | break; |
| 215 | case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */ |
| 216 | adc_reg &= ~AD1938_ADC_LEFT_HIGH; |
| 217 | adc_reg |= AD1938_ADC_BCLK_INV; |
| 218 | dac_reg &= ~AD1938_DAC_LEFT_HIGH; |
| 219 | dac_reg |= AD1938_DAC_BCLK_INV; |
| 220 | break; |
| 221 | |
| 222 | case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */ |
| 223 | adc_reg |= AD1938_ADC_LEFT_HIGH; |
| 224 | adc_reg |= AD1938_ADC_BCLK_INV; |
| 225 | dac_reg |= AD1938_DAC_LEFT_HIGH; |
| 226 | dac_reg |= AD1938_DAC_BCLK_INV; |
| 227 | break; |
| 228 | default: |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 233 | case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */ |
| 234 | adc_reg |= AD1938_ADC_LCR_MASTER; |
| 235 | adc_reg |= AD1938_ADC_BCLK_MASTER; |
| 236 | dac_reg |= AD1938_DAC_LCR_MASTER; |
| 237 | dac_reg |= AD1938_DAC_BCLK_MASTER; |
| 238 | break; |
| 239 | case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */ |
| 240 | adc_reg |= AD1938_ADC_LCR_MASTER; |
| 241 | adc_reg &= ~AD1938_ADC_BCLK_MASTER; |
| 242 | dac_reg |= AD1938_DAC_LCR_MASTER; |
| 243 | dac_reg &= ~AD1938_DAC_BCLK_MASTER; |
| 244 | break; |
| 245 | case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */ |
| 246 | adc_reg &= ~AD1938_ADC_LCR_MASTER; |
| 247 | adc_reg |= AD1938_ADC_BCLK_MASTER; |
| 248 | dac_reg &= ~AD1938_DAC_LCR_MASTER; |
| 249 | dac_reg |= AD1938_DAC_BCLK_MASTER; |
| 250 | break; |
| 251 | case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */ |
| 252 | adc_reg &= ~AD1938_ADC_LCR_MASTER; |
| 253 | adc_reg &= ~AD1938_ADC_BCLK_MASTER; |
| 254 | dac_reg &= ~AD1938_DAC_LCR_MASTER; |
| 255 | dac_reg &= ~AD1938_DAC_BCLK_MASTER; |
| 256 | break; |
| 257 | default: |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | codec->write(codec, AD1938_ADC_CTRL2, adc_reg); |
| 262 | codec->write(codec, AD1938_DAC_CTRL1, dac_reg); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int ad1938_hw_params(struct snd_pcm_substream *substream, |
| 268 | struct snd_pcm_hw_params *params, |
| 269 | struct snd_soc_dai *dai) |
| 270 | { |
| 271 | int word_len = 0, reg = 0; |
| 272 | |
| 273 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 274 | struct snd_soc_device *socdev = rtd->socdev; |
| 275 | struct snd_soc_codec *codec = socdev->card->codec; |
| 276 | |
| 277 | /* bit size */ |
| 278 | switch (params_format(params)) { |
| 279 | case SNDRV_PCM_FORMAT_S16_LE: |
| 280 | word_len = 3; |
| 281 | break; |
| 282 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 283 | word_len = 1; |
| 284 | break; |
| 285 | case SNDRV_PCM_FORMAT_S24_LE: |
| 286 | case SNDRV_PCM_FORMAT_S32_LE: |
| 287 | word_len = 0; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | reg = codec->read(codec, AD1938_DAC_CTRL2); |
| 292 | reg = (reg & (~AD1938_DAC_WORD_LEN_MASK)) | word_len; |
| 293 | codec->write(codec, AD1938_DAC_CTRL2, reg); |
| 294 | |
| 295 | reg = codec->read(codec, AD1938_ADC_CTRL1); |
| 296 | reg = (reg & (~AD1938_ADC_WORD_LEN_MASK)) | word_len; |
| 297 | codec->write(codec, AD1938_ADC_CTRL1, reg); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 302 | /* |
| 303 | * interface to read/write ad1938 register |
| 304 | */ |
| 305 | |
| 306 | #define AD1938_SPI_ADDR 0x4 |
| 307 | #define AD1938_SPI_READ 0x1 |
| 308 | #define AD1938_SPI_BUFLEN 3 |
| 309 | |
| 310 | /* |
| 311 | * write to the ad1938 register space |
| 312 | */ |
| 313 | |
| 314 | static int ad1938_write_reg(struct snd_soc_codec *codec, unsigned int reg, |
| 315 | unsigned int value) |
| 316 | { |
| 317 | u8 *reg_cache = codec->reg_cache; |
| 318 | int ret = 0; |
| 319 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 320 | if (value != reg_cache[reg]) { |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 321 | uint8_t buf[AD1938_SPI_BUFLEN]; |
| 322 | struct spi_transfer t = { |
| 323 | .tx_buf = buf, |
| 324 | .len = AD1938_SPI_BUFLEN, |
| 325 | }; |
| 326 | struct spi_message m; |
| 327 | |
| 328 | buf[0] = AD1938_SPI_ADDR << 1; |
| 329 | buf[1] = reg; |
| 330 | buf[2] = value; |
| 331 | spi_message_init(&m); |
| 332 | spi_message_add_tail(&t, &m); |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 333 | ret = spi_sync(codec->control_data, &m); |
| 334 | if (ret == 0) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 335 | reg_cache[reg] = value; |
| 336 | } |
| 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * read from the ad1938 register space cache |
| 343 | */ |
| 344 | |
| 345 | static unsigned int ad1938_read_reg_cache(struct snd_soc_codec *codec, |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 346 | unsigned int reg) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 347 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 348 | u8 *reg_cache = codec->reg_cache; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 349 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 350 | if (reg >= codec->reg_cache_size) |
| 351 | return -EINVAL; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 352 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 353 | return reg_cache[reg]; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
| 357 | * read from the ad1938 register space |
| 358 | */ |
| 359 | |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 360 | static unsigned int ad1938_read_reg(struct snd_soc_codec *codec, |
| 361 | unsigned int reg) |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 362 | { |
| 363 | char w_buf[AD1938_SPI_BUFLEN]; |
| 364 | char r_buf[AD1938_SPI_BUFLEN]; |
| 365 | int ret; |
| 366 | |
| 367 | struct spi_transfer t = { |
| 368 | .tx_buf = w_buf, |
| 369 | .rx_buf = r_buf, |
| 370 | .len = AD1938_SPI_BUFLEN, |
| 371 | }; |
| 372 | struct spi_message m; |
| 373 | |
| 374 | w_buf[0] = (AD1938_SPI_ADDR << 1) | AD1938_SPI_READ; |
| 375 | w_buf[1] = reg; |
| 376 | w_buf[2] = 0; |
| 377 | |
| 378 | spi_message_init(&m); |
| 379 | spi_message_add_tail(&t, &m); |
| 380 | ret = spi_sync(codec->control_data, &m); |
| 381 | if (ret == 0) |
| 382 | return r_buf[2]; |
| 383 | else |
| 384 | return -EIO; |
| 385 | } |
| 386 | |
| 387 | static int ad1938_fill_cache(struct snd_soc_codec *codec) |
| 388 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 389 | int i; |
| 390 | u8 *reg_cache = codec->reg_cache; |
| 391 | struct spi_device *spi = codec->control_data; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 392 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 393 | for (i = 0; i < codec->reg_cache_size; i++) { |
| 394 | int ret = ad1938_read_reg(codec, i); |
| 395 | if (ret == -EIO) { |
| 396 | dev_err(&spi->dev, "AD1938 SPI read failure\n"); |
| 397 | return ret; |
| 398 | } |
| 399 | reg_cache[i] = ret; |
| 400 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 401 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 402 | return 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static int __devinit ad1938_spi_probe(struct spi_device *spi) |
| 406 | { |
| 407 | struct snd_soc_codec *codec; |
| 408 | struct ad1938_priv *ad1938; |
| 409 | |
| 410 | ad1938 = kzalloc(sizeof(struct ad1938_priv), GFP_KERNEL); |
| 411 | if (ad1938 == NULL) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | codec = &ad1938->codec; |
| 415 | codec->control_data = spi; |
| 416 | codec->dev = &spi->dev; |
| 417 | |
Takashi Iwai | 416c8fe | 2009-07-17 07:48:03 +0200 | [diff] [blame] | 418 | dev_set_drvdata(&spi->dev, ad1938); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 419 | |
| 420 | return ad1938_register(ad1938); |
| 421 | } |
| 422 | |
| 423 | static int __devexit ad1938_spi_remove(struct spi_device *spi) |
| 424 | { |
Takashi Iwai | 416c8fe | 2009-07-17 07:48:03 +0200 | [diff] [blame] | 425 | struct ad1938_priv *ad1938 = dev_get_drvdata(&spi->dev); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 426 | |
| 427 | ad1938_unregister(ad1938); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static struct spi_driver ad1938_spi_driver = { |
| 432 | .driver = { |
Barry Song | 2a70813 | 2009-08-14 12:06:38 +0800 | [diff] [blame] | 433 | .name = "ad1938", |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 434 | .owner = THIS_MODULE, |
| 435 | }, |
| 436 | .probe = ad1938_spi_probe, |
| 437 | .remove = __devexit_p(ad1938_spi_remove), |
| 438 | }; |
| 439 | |
| 440 | static struct snd_soc_dai_ops ad1938_dai_ops = { |
| 441 | .hw_params = ad1938_hw_params, |
| 442 | .digital_mute = ad1938_mute, |
| 443 | .set_tdm_slot = ad1938_set_tdm_slot, |
| 444 | .set_fmt = ad1938_set_dai_fmt, |
| 445 | }; |
| 446 | |
| 447 | /* codec DAI instance */ |
| 448 | struct snd_soc_dai ad1938_dai = { |
| 449 | .name = "AD1938", |
| 450 | .playback = { |
| 451 | .stream_name = "Playback", |
| 452 | .channels_min = 2, |
| 453 | .channels_max = 8, |
| 454 | .rates = SNDRV_PCM_RATE_48000, |
| 455 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 456 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 457 | }, |
| 458 | .capture = { |
| 459 | .stream_name = "Capture", |
| 460 | .channels_min = 2, |
| 461 | .channels_max = 4, |
| 462 | .rates = SNDRV_PCM_RATE_48000, |
| 463 | .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 464 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, |
| 465 | }, |
| 466 | .ops = &ad1938_dai_ops, |
| 467 | }; |
| 468 | EXPORT_SYMBOL_GPL(ad1938_dai); |
| 469 | |
| 470 | static int ad1938_register(struct ad1938_priv *ad1938) |
| 471 | { |
| 472 | int ret; |
| 473 | struct snd_soc_codec *codec = &ad1938->codec; |
| 474 | |
| 475 | if (ad1938_codec) { |
| 476 | dev_err(codec->dev, "Another ad1938 is registered\n"); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | |
| 480 | mutex_init(&codec->mutex); |
| 481 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 482 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 483 | codec->private_data = ad1938; |
| 484 | codec->reg_cache = ad1938->reg_cache; |
| 485 | codec->reg_cache_size = AD1938_NUM_REGS; |
| 486 | codec->name = "AD1938"; |
| 487 | codec->owner = THIS_MODULE; |
| 488 | codec->dai = &ad1938_dai; |
| 489 | codec->num_dai = 1; |
| 490 | codec->write = ad1938_write_reg; |
| 491 | codec->read = ad1938_read_reg_cache; |
| 492 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 493 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 494 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 495 | ad1938_dai.dev = codec->dev; |
| 496 | ad1938_codec = codec; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 497 | |
| 498 | /* default setting for ad1938 */ |
Barry Song | 3a39f83 | 2009-07-29 15:03:16 +0800 | [diff] [blame] | 499 | |
| 500 | /* unmute dac channels */ |
| 501 | codec->write(codec, AD1938_DAC_CHNL_MUTE, 0x0); |
| 502 | /* de-emphasis: 48kHz, powedown dac */ |
| 503 | codec->write(codec, AD1938_DAC_CTRL2, 0x1A); |
| 504 | /* powerdown dac, dac in tdm mode */ |
| 505 | codec->write(codec, AD1938_DAC_CTRL0, 0x41); |
| 506 | /* high-pass filter enable */ |
| 507 | codec->write(codec, AD1938_ADC_CTRL0, 0x3); |
| 508 | /* sata delay=1, adc aux mode */ |
| 509 | codec->write(codec, AD1938_ADC_CTRL1, 0x43); |
| 510 | /* pll input: mclki/xi */ |
| 511 | codec->write(codec, AD1938_PLL_CLK_CTRL0, 0x9D); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 512 | codec->write(codec, AD1938_PLL_CLK_CTRL1, 0x04); |
| 513 | |
| 514 | ad1938_fill_cache(codec); |
| 515 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 516 | ret = snd_soc_register_codec(codec); |
| 517 | if (ret != 0) { |
| 518 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
Barry Song | 2a70813 | 2009-08-14 12:06:38 +0800 | [diff] [blame] | 519 | kfree(ad1938); |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 520 | return ret; |
| 521 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 522 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 523 | ret = snd_soc_register_dai(&ad1938_dai); |
| 524 | if (ret != 0) { |
| 525 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); |
| 526 | snd_soc_unregister_codec(codec); |
Barry Song | 2a70813 | 2009-08-14 12:06:38 +0800 | [diff] [blame] | 527 | kfree(ad1938); |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 528 | return ret; |
| 529 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 530 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 531 | return 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static void ad1938_unregister(struct ad1938_priv *ad1938) |
| 535 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 536 | snd_soc_unregister_dai(&ad1938_dai); |
| 537 | snd_soc_unregister_codec(&ad1938->codec); |
| 538 | kfree(ad1938); |
| 539 | ad1938_codec = NULL; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static int ad1938_probe(struct platform_device *pdev) |
| 543 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 544 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 545 | struct snd_soc_codec *codec; |
| 546 | int ret = 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 547 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 548 | if (ad1938_codec == NULL) { |
| 549 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 550 | return -ENODEV; |
| 551 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 552 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 553 | socdev->card->codec = ad1938_codec; |
| 554 | codec = ad1938_codec; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 555 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 556 | /* register pcms */ |
| 557 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 558 | if (ret < 0) { |
| 559 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
| 560 | goto pcm_err; |
| 561 | } |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 562 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 563 | snd_soc_add_controls(codec, ad1938_snd_controls, |
| 564 | ARRAY_SIZE(ad1938_snd_controls)); |
| 565 | snd_soc_dapm_new_controls(codec, ad1938_dapm_widgets, |
| 566 | ARRAY_SIZE(ad1938_dapm_widgets)); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 567 | snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths)); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 568 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 569 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 570 | pcm_err: |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 571 | return ret; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* power down chip */ |
| 575 | static int ad1938_remove(struct platform_device *pdev) |
| 576 | { |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 577 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 578 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 579 | snd_soc_free_pcms(socdev); |
| 580 | snd_soc_dapm_free(socdev); |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 581 | |
Mark Brown | bca1465 | 2009-07-18 11:09:42 +0100 | [diff] [blame] | 582 | return 0; |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 583 | } |
| 584 | |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 585 | struct snd_soc_codec_device soc_codec_dev_ad1938 = { |
| 586 | .probe = ad1938_probe, |
| 587 | .remove = ad1938_remove, |
Barry Song | 1274738 | 2009-07-16 16:00:05 +0800 | [diff] [blame] | 588 | }; |
| 589 | EXPORT_SYMBOL_GPL(soc_codec_dev_ad1938); |
| 590 | |
| 591 | static int __init ad1938_init(void) |
| 592 | { |
| 593 | int ret; |
| 594 | |
| 595 | ret = spi_register_driver(&ad1938_spi_driver); |
| 596 | if (ret != 0) { |
| 597 | printk(KERN_ERR "Failed to register ad1938 SPI driver: %d\n", |
| 598 | ret); |
| 599 | } |
| 600 | |
| 601 | return ret; |
| 602 | } |
| 603 | module_init(ad1938_init); |
| 604 | |
| 605 | static void __exit ad1938_exit(void) |
| 606 | { |
| 607 | spi_unregister_driver(&ad1938_spi_driver); |
| 608 | } |
| 609 | module_exit(ad1938_exit); |
| 610 | |
| 611 | MODULE_DESCRIPTION("ASoC ad1938 driver"); |
| 612 | MODULE_AUTHOR("Barry Song "); |
| 613 | MODULE_LICENSE("GPL"); |