blob: eedb6f5e5823499919e14611db21a747a2a023b6 [file] [log] [blame]
Barry Songa1533d92010-03-19 14:48:33 +08001/*
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 Rothwell1b132ea2010-03-29 15:32:18 +110015#include <linux/slab.h>
Barry Songa1533d92010-03-19 14:48:33 +080016#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 Songa1533d92010-03-19 14:48:33 +080022#include "ad193x.h"
23
24/* codec private data */
25struct ad193x_priv {
Scott Jiang119bfef2011-03-26 03:52:20 -040026 enum snd_soc_control_type control_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000027 int sysclk;
Barry Songa1533d92010-03-19 14:48:33 +080028};
29
Barry Songa1533d92010-03-19 14:48:33 +080030/*
31 * AD193X volume/mute/de-emphasis etc. controls
32 */
33static const char *ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
34
35static const struct soc_enum ad193x_deemp_enum =
36 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp);
37
38static const struct snd_kcontrol_new ad193x_snd_controls[] = {
39 /* DAC volume control */
Barry Songba0a24e2010-04-21 17:36:49 +080040 SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080041 AD193X_DAC_R1_VOL, 0, 0xFF, 1),
Barry Songba0a24e2010-04-21 17:36:49 +080042 SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080043 AD193X_DAC_R2_VOL, 0, 0xFF, 1),
Barry Songba0a24e2010-04-21 17:36:49 +080044 SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080045 AD193X_DAC_R3_VOL, 0, 0xFF, 1),
Barry Songba0a24e2010-04-21 17:36:49 +080046 SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080047 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
73static 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
86static 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
103static int ad193x_mute(struct snd_soc_dai *dai, int mute)
104{
105 struct snd_soc_codec *codec = dai->codec;
106 int reg;
107
108 reg = snd_soc_read(codec, AD193X_DAC_CTRL2);
109 reg = (mute > 0) ? reg | AD193X_DAC_MASTER_MUTE : reg &
110 (~AD193X_DAC_MASTER_MUTE);
111 snd_soc_write(codec, AD193X_DAC_CTRL2, reg);
112
113 return 0;
114}
115
116static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
117 unsigned int rx_mask, int slots, int width)
118{
119 struct snd_soc_codec *codec = dai->codec;
120 int dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
121 int adc_reg = snd_soc_read(codec, AD193X_ADC_CTRL2);
122
123 dac_reg &= ~AD193X_DAC_CHAN_MASK;
124 adc_reg &= ~AD193X_ADC_CHAN_MASK;
125
126 switch (slots) {
127 case 2:
128 dac_reg |= AD193X_DAC_2_CHANNELS << AD193X_DAC_CHAN_SHFT;
129 adc_reg |= AD193X_ADC_2_CHANNELS << AD193X_ADC_CHAN_SHFT;
130 break;
131 case 4:
132 dac_reg |= AD193X_DAC_4_CHANNELS << AD193X_DAC_CHAN_SHFT;
133 adc_reg |= AD193X_ADC_4_CHANNELS << AD193X_ADC_CHAN_SHFT;
134 break;
135 case 8:
136 dac_reg |= AD193X_DAC_8_CHANNELS << AD193X_DAC_CHAN_SHFT;
137 adc_reg |= AD193X_ADC_8_CHANNELS << AD193X_ADC_CHAN_SHFT;
138 break;
139 case 16:
140 dac_reg |= AD193X_DAC_16_CHANNELS << AD193X_DAC_CHAN_SHFT;
141 adc_reg |= AD193X_ADC_16_CHANNELS << AD193X_ADC_CHAN_SHFT;
142 break;
143 default:
144 return -EINVAL;
145 }
146
147 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
148 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg);
149
150 return 0;
151}
152
153static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
154 unsigned int fmt)
155{
156 struct snd_soc_codec *codec = codec_dai->codec;
Barry Songd6bdc0f2010-04-21 17:36:48 +0800157 int adc_reg1, adc_reg2, dac_reg;
Barry Songa1533d92010-03-19 14:48:33 +0800158
Barry Songd6bdc0f2010-04-21 17:36:48 +0800159 adc_reg1 = snd_soc_read(codec, AD193X_ADC_CTRL1);
160 adc_reg2 = snd_soc_read(codec, AD193X_ADC_CTRL2);
Barry Songa1533d92010-03-19 14:48:33 +0800161 dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
162
163 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
164 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
165 */
166 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
167 case SND_SOC_DAIFMT_I2S:
Barry Songd6bdc0f2010-04-21 17:36:48 +0800168 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
169 adc_reg1 |= AD193X_ADC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800170 break;
171 case SND_SOC_DAIFMT_DSP_A:
Barry Songd6bdc0f2010-04-21 17:36:48 +0800172 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
173 adc_reg1 |= AD193X_ADC_SERFMT_AUX;
Barry Songa1533d92010-03-19 14:48:33 +0800174 break;
175 default:
176 return -EINVAL;
177 }
178
179 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
180 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800181 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
182 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800183 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
184 dac_reg &= ~AD193X_DAC_BCLK_INV;
185 break;
186 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800187 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
188 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800189 dac_reg |= AD193X_DAC_LEFT_HIGH;
190 dac_reg &= ~AD193X_DAC_BCLK_INV;
191 break;
192 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800193 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
194 adc_reg2 |= AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800195 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
196 dac_reg |= AD193X_DAC_BCLK_INV;
197 break;
198
199 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800200 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
201 adc_reg2 |= AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800202 dac_reg |= AD193X_DAC_LEFT_HIGH;
203 dac_reg |= AD193X_DAC_BCLK_INV;
204 break;
205 default:
206 return -EINVAL;
207 }
208
209 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
210 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800211 adc_reg2 |= AD193X_ADC_LCR_MASTER;
212 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800213 dac_reg |= AD193X_DAC_LCR_MASTER;
214 dac_reg |= AD193X_DAC_BCLK_MASTER;
215 break;
216 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800217 adc_reg2 |= AD193X_ADC_LCR_MASTER;
218 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800219 dac_reg |= AD193X_DAC_LCR_MASTER;
220 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
221 break;
222 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800223 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
224 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800225 dac_reg &= ~AD193X_DAC_LCR_MASTER;
226 dac_reg |= AD193X_DAC_BCLK_MASTER;
227 break;
228 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800229 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
230 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800231 dac_reg &= ~AD193X_DAC_LCR_MASTER;
232 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
233 break;
234 default:
235 return -EINVAL;
236 }
237
Barry Songd6bdc0f2010-04-21 17:36:48 +0800238 snd_soc_write(codec, AD193X_ADC_CTRL1, adc_reg1);
239 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg2);
Barry Songa1533d92010-03-19 14:48:33 +0800240 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
241
242 return 0;
243}
244
Barry Songfab90aa2010-05-21 11:57:01 +0800245static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
246 int clk_id, unsigned int freq, int dir)
247{
248 struct snd_soc_codec *codec = codec_dai->codec;
249 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
250 switch (freq) {
251 case 12288000:
252 case 18432000:
253 case 24576000:
254 case 36864000:
255 ad193x->sysclk = freq;
256 return 0;
257 }
258 return -EINVAL;
259}
260
Barry Songa1533d92010-03-19 14:48:33 +0800261static int ad193x_hw_params(struct snd_pcm_substream *substream,
262 struct snd_pcm_hw_params *params,
263 struct snd_soc_dai *dai)
264{
Barry Songfab90aa2010-05-21 11:57:01 +0800265 int word_len = 0, reg = 0, master_rate = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800266
267 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000268 struct snd_soc_codec *codec = rtd->codec;
Barry Songfab90aa2010-05-21 11:57:01 +0800269 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Barry Songa1533d92010-03-19 14:48:33 +0800270
271 /* bit size */
272 switch (params_format(params)) {
273 case SNDRV_PCM_FORMAT_S16_LE:
274 word_len = 3;
275 break;
276 case SNDRV_PCM_FORMAT_S20_3LE:
277 word_len = 1;
278 break;
279 case SNDRV_PCM_FORMAT_S24_LE:
280 case SNDRV_PCM_FORMAT_S32_LE:
281 word_len = 0;
282 break;
283 }
284
Barry Songfab90aa2010-05-21 11:57:01 +0800285 switch (ad193x->sysclk) {
286 case 12288000:
287 master_rate = AD193X_PLL_INPUT_256;
288 break;
289 case 18432000:
290 master_rate = AD193X_PLL_INPUT_384;
291 break;
292 case 24576000:
293 master_rate = AD193X_PLL_INPUT_512;
294 break;
295 case 36864000:
296 master_rate = AD193X_PLL_INPUT_768;
297 break;
298 }
299
300 reg = snd_soc_read(codec, AD193X_PLL_CLK_CTRL0);
301 reg = (reg & AD193X_PLL_INPUT_MASK) | master_rate;
302 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, reg);
303
Barry Songa1533d92010-03-19 14:48:33 +0800304 reg = snd_soc_read(codec, AD193X_DAC_CTRL2);
Scott Jiang95c93d82011-08-12 18:04:11 -0400305 reg = (reg & (~AD193X_DAC_WORD_LEN_MASK))
306 | (word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800307 snd_soc_write(codec, AD193X_DAC_CTRL2, reg);
308
309 reg = snd_soc_read(codec, AD193X_ADC_CTRL1);
310 reg = (reg & (~AD193X_ADC_WORD_LEN_MASK)) | word_len;
311 snd_soc_write(codec, AD193X_ADC_CTRL1, reg);
312
313 return 0;
314}
315
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000316static struct snd_soc_dai_ops ad193x_dai_ops = {
317 .hw_params = ad193x_hw_params,
318 .digital_mute = ad193x_mute,
319 .set_tdm_slot = ad193x_set_tdm_slot,
320 .set_sysclk = ad193x_set_dai_sysclk,
321 .set_fmt = ad193x_set_dai_fmt,
322};
323
324/* codec DAI instance */
325static struct snd_soc_dai_driver ad193x_dai = {
326 .name = "ad193x-hifi",
327 .playback = {
328 .stream_name = "Playback",
329 .channels_min = 2,
330 .channels_max = 8,
331 .rates = SNDRV_PCM_RATE_48000,
332 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
333 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
334 },
335 .capture = {
336 .stream_name = "Capture",
337 .channels_min = 2,
338 .channels_max = 4,
339 .rates = SNDRV_PCM_RATE_48000,
340 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
341 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
342 },
343 .ops = &ad193x_dai_ops,
344};
345
346static int ad193x_probe(struct snd_soc_codec *codec)
Barry Songa1533d92010-03-19 14:48:33 +0800347{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000348 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200349 struct snd_soc_dapm_context *dapm = &codec->dapm;
Barry Song9dd7b792010-03-29 11:16:00 +0800350 int ret;
351
Scott Jiang119bfef2011-03-26 03:52:20 -0400352 if (ad193x->control_type == SND_SOC_I2C)
353 ret = snd_soc_codec_set_cache_io(codec, 8, 8, ad193x->control_type);
Barry Songa1533d92010-03-19 14:48:33 +0800354 else
Scott Jiang119bfef2011-03-26 03:52:20 -0400355 ret = snd_soc_codec_set_cache_io(codec, 16, 8, ad193x->control_type);
Barry Songa1533d92010-03-19 14:48:33 +0800356 if (ret < 0) {
Scott Jiang119bfef2011-03-26 03:52:20 -0400357 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
Barry Songa1533d92010-03-19 14:48:33 +0800358 return ret;
359 }
360
361 /* default setting for ad193x */
362
363 /* unmute dac channels */
364 snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0);
365 /* de-emphasis: 48kHz, powedown dac */
366 snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A);
367 /* powerdown dac, dac in tdm mode */
368 snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41);
369 /* high-pass filter enable */
370 snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3);
371 /* sata delay=1, adc aux mode */
372 snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43);
373 /* pll input: mclki/xi */
374 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
375 snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800376
377 snd_soc_add_controls(codec, ad193x_snd_controls,
378 ARRAY_SIZE(ad193x_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200379 snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets,
Barry Songa1533d92010-03-19 14:48:33 +0800380 ARRAY_SIZE(ad193x_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200381 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
Barry Songa1533d92010-03-19 14:48:33 +0800382
Barry Songa1533d92010-03-19 14:48:33 +0800383 return ret;
384}
385
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000386static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
Barry Songa1533d92010-03-19 14:48:33 +0800387 .probe = ad193x_probe,
Barry Songa1533d92010-03-19 14:48:33 +0800388};
Barry Songa1533d92010-03-19 14:48:33 +0800389
390#if defined(CONFIG_SPI_MASTER)
391static int __devinit ad193x_spi_probe(struct spi_device *spi)
392{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000393 struct ad193x_priv *ad193x;
394 int ret;
395
396 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
397 if (ad193x == NULL)
398 return -ENOMEM;
399
400 spi_set_drvdata(spi, ad193x);
Scott Jiang119bfef2011-03-26 03:52:20 -0400401 ad193x->control_type = SND_SOC_SPI;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000402
403 ret = snd_soc_register_codec(&spi->dev,
404 &soc_codec_dev_ad193x, &ad193x_dai, 1);
405 if (ret < 0)
406 kfree(ad193x);
407 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800408}
409
410static int __devexit ad193x_spi_remove(struct spi_device *spi)
411{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000412 snd_soc_unregister_codec(&spi->dev);
413 kfree(spi_get_drvdata(spi));
414 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800415}
416
417static struct spi_driver ad193x_spi_driver = {
418 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400419 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800420 .owner = THIS_MODULE,
421 },
422 .probe = ad193x_spi_probe,
423 .remove = __devexit_p(ad193x_spi_remove),
424};
425#endif
426
427#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
428static const struct i2c_device_id ad193x_id[] = {
429 { "ad1936", 0 },
430 { "ad1937", 0 },
431 { }
432};
433MODULE_DEVICE_TABLE(i2c, ad193x_id);
434
435static int __devinit ad193x_i2c_probe(struct i2c_client *client,
436 const struct i2c_device_id *id)
437{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000438 struct ad193x_priv *ad193x;
439 int ret;
440
441 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
442 if (ad193x == NULL)
443 return -ENOMEM;
444
445 i2c_set_clientdata(client, ad193x);
Scott Jiang119bfef2011-03-26 03:52:20 -0400446 ad193x->control_type = SND_SOC_I2C;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447
448 ret = snd_soc_register_codec(&client->dev,
449 &soc_codec_dev_ad193x, &ad193x_dai, 1);
450 if (ret < 0)
451 kfree(ad193x);
452 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800453}
454
455static int __devexit ad193x_i2c_remove(struct i2c_client *client)
456{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000457 snd_soc_unregister_codec(&client->dev);
458 kfree(i2c_get_clientdata(client));
459 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800460}
461
462static struct i2c_driver ad193x_i2c_driver = {
463 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400464 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800465 },
466 .probe = ad193x_i2c_probe,
467 .remove = __devexit_p(ad193x_i2c_remove),
468 .id_table = ad193x_id,
469};
470#endif
471
472static int __init ad193x_modinit(void)
473{
474 int ret;
475
476#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
477 ret = i2c_add_driver(&ad193x_i2c_driver);
478 if (ret != 0) {
479 printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
480 ret);
481 }
482#endif
483
484#if defined(CONFIG_SPI_MASTER)
485 ret = spi_register_driver(&ad193x_spi_driver);
486 if (ret != 0) {
487 printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
488 ret);
489 }
490#endif
491 return ret;
492}
493module_init(ad193x_modinit);
494
495static void __exit ad193x_modexit(void)
496{
497#if defined(CONFIG_SPI_MASTER)
498 spi_unregister_driver(&ad193x_spi_driver);
499#endif
500
501#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
502 i2c_del_driver(&ad193x_i2c_driver);
503#endif
504}
505module_exit(ad193x_modexit);
506
507MODULE_DESCRIPTION("ASoC ad193x driver");
508MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
509MODULE_LICENSE("GPL");