blob: f1a8be58255b010850e92f08b0defe30d05541c2 [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
30/* ad193x register cache & default register settings */
31static const u8 ad193x_reg[AD193X_NUM_REGS] = {
32 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0,
33};
34
Barry Songa1533d92010-03-19 14:48:33 +080035/*
36 * AD193X volume/mute/de-emphasis etc. controls
37 */
38static const char *ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
39
40static const struct soc_enum ad193x_deemp_enum =
41 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp);
42
43static const struct snd_kcontrol_new ad193x_snd_controls[] = {
44 /* DAC volume control */
Barry Songba0a24e2010-04-21 17:36:49 +080045 SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080046 AD193X_DAC_R1_VOL, 0, 0xFF, 1),
Barry Songba0a24e2010-04-21 17:36:49 +080047 SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080048 AD193X_DAC_R2_VOL, 0, 0xFF, 1),
Barry Songba0a24e2010-04-21 17:36:49 +080049 SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080050 AD193X_DAC_R3_VOL, 0, 0xFF, 1),
Barry Songba0a24e2010-04-21 17:36:49 +080051 SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL,
Barry Songa1533d92010-03-19 14:48:33 +080052 AD193X_DAC_R4_VOL, 0, 0xFF, 1),
53
54 /* ADC switch control */
55 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
56 AD193X_ADCR1_MUTE, 1, 1),
57 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
58 AD193X_ADCR2_MUTE, 1, 1),
59
60 /* DAC switch control */
61 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
62 AD193X_DACR1_MUTE, 1, 1),
63 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
64 AD193X_DACR2_MUTE, 1, 1),
65 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
66 AD193X_DACR3_MUTE, 1, 1),
67 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
68 AD193X_DACR4_MUTE, 1, 1),
69
70 /* ADC high-pass filter */
71 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
72 AD193X_ADC_HIGHPASS_FILTER, 1, 0),
73
74 /* DAC de-emphasis */
75 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
76};
77
78static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
79 SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0, 0, 1),
80 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
81 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
82 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
83 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
84 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
85 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
86 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
87 SND_SOC_DAPM_INPUT("ADC1IN"),
88 SND_SOC_DAPM_INPUT("ADC2IN"),
89};
90
91static const struct snd_soc_dapm_route audio_paths[] = {
92 { "DAC", NULL, "PLL_PWR" },
93 { "ADC", NULL, "PLL_PWR" },
94 { "DAC", NULL, "ADC_PWR" },
95 { "ADC", NULL, "ADC_PWR" },
96 { "DAC1OUT", "DAC1 Switch", "DAC" },
97 { "DAC2OUT", "DAC2 Switch", "DAC" },
98 { "DAC3OUT", "DAC3 Switch", "DAC" },
99 { "DAC4OUT", "DAC4 Switch", "DAC" },
100 { "ADC", "ADC1 Switch", "ADC1IN" },
101 { "ADC", "ADC2 Switch", "ADC2IN" },
102};
103
104/*
105 * DAI ops entries
106 */
107
108static int ad193x_mute(struct snd_soc_dai *dai, int mute)
109{
110 struct snd_soc_codec *codec = dai->codec;
111 int reg;
112
113 reg = snd_soc_read(codec, AD193X_DAC_CTRL2);
114 reg = (mute > 0) ? reg | AD193X_DAC_MASTER_MUTE : reg &
115 (~AD193X_DAC_MASTER_MUTE);
116 snd_soc_write(codec, AD193X_DAC_CTRL2, reg);
117
118 return 0;
119}
120
121static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
122 unsigned int rx_mask, int slots, int width)
123{
124 struct snd_soc_codec *codec = dai->codec;
125 int dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
126 int adc_reg = snd_soc_read(codec, AD193X_ADC_CTRL2);
127
128 dac_reg &= ~AD193X_DAC_CHAN_MASK;
129 adc_reg &= ~AD193X_ADC_CHAN_MASK;
130
131 switch (slots) {
132 case 2:
133 dac_reg |= AD193X_DAC_2_CHANNELS << AD193X_DAC_CHAN_SHFT;
134 adc_reg |= AD193X_ADC_2_CHANNELS << AD193X_ADC_CHAN_SHFT;
135 break;
136 case 4:
137 dac_reg |= AD193X_DAC_4_CHANNELS << AD193X_DAC_CHAN_SHFT;
138 adc_reg |= AD193X_ADC_4_CHANNELS << AD193X_ADC_CHAN_SHFT;
139 break;
140 case 8:
141 dac_reg |= AD193X_DAC_8_CHANNELS << AD193X_DAC_CHAN_SHFT;
142 adc_reg |= AD193X_ADC_8_CHANNELS << AD193X_ADC_CHAN_SHFT;
143 break;
144 case 16:
145 dac_reg |= AD193X_DAC_16_CHANNELS << AD193X_DAC_CHAN_SHFT;
146 adc_reg |= AD193X_ADC_16_CHANNELS << AD193X_ADC_CHAN_SHFT;
147 break;
148 default:
149 return -EINVAL;
150 }
151
152 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
153 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg);
154
155 return 0;
156}
157
158static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
159 unsigned int fmt)
160{
161 struct snd_soc_codec *codec = codec_dai->codec;
Barry Songd6bdc0f2010-04-21 17:36:48 +0800162 int adc_reg1, adc_reg2, dac_reg;
Barry Songa1533d92010-03-19 14:48:33 +0800163
Barry Songd6bdc0f2010-04-21 17:36:48 +0800164 adc_reg1 = snd_soc_read(codec, AD193X_ADC_CTRL1);
165 adc_reg2 = snd_soc_read(codec, AD193X_ADC_CTRL2);
Barry Songa1533d92010-03-19 14:48:33 +0800166 dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
167
168 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
169 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
170 */
171 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
172 case SND_SOC_DAIFMT_I2S:
Barry Songd6bdc0f2010-04-21 17:36:48 +0800173 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
174 adc_reg1 |= AD193X_ADC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800175 break;
176 case SND_SOC_DAIFMT_DSP_A:
Barry Songd6bdc0f2010-04-21 17:36:48 +0800177 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
178 adc_reg1 |= AD193X_ADC_SERFMT_AUX;
Barry Songa1533d92010-03-19 14:48:33 +0800179 break;
180 default:
181 return -EINVAL;
182 }
183
184 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
185 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800186 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
187 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800188 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
189 dac_reg &= ~AD193X_DAC_BCLK_INV;
190 break;
191 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800192 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
193 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800194 dac_reg |= AD193X_DAC_LEFT_HIGH;
195 dac_reg &= ~AD193X_DAC_BCLK_INV;
196 break;
197 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800198 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
199 adc_reg2 |= AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800200 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
201 dac_reg |= AD193X_DAC_BCLK_INV;
202 break;
203
204 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800205 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
206 adc_reg2 |= AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800207 dac_reg |= AD193X_DAC_LEFT_HIGH;
208 dac_reg |= AD193X_DAC_BCLK_INV;
209 break;
210 default:
211 return -EINVAL;
212 }
213
214 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
215 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800216 adc_reg2 |= AD193X_ADC_LCR_MASTER;
217 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800218 dac_reg |= AD193X_DAC_LCR_MASTER;
219 dac_reg |= AD193X_DAC_BCLK_MASTER;
220 break;
221 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800222 adc_reg2 |= AD193X_ADC_LCR_MASTER;
223 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800224 dac_reg |= AD193X_DAC_LCR_MASTER;
225 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
226 break;
227 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800228 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
229 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800230 dac_reg &= ~AD193X_DAC_LCR_MASTER;
231 dac_reg |= AD193X_DAC_BCLK_MASTER;
232 break;
233 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800234 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
235 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800236 dac_reg &= ~AD193X_DAC_LCR_MASTER;
237 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
238 break;
239 default:
240 return -EINVAL;
241 }
242
Barry Songd6bdc0f2010-04-21 17:36:48 +0800243 snd_soc_write(codec, AD193X_ADC_CTRL1, adc_reg1);
244 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg2);
Barry Songa1533d92010-03-19 14:48:33 +0800245 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
246
247 return 0;
248}
249
Barry Songfab90aa2010-05-21 11:57:01 +0800250static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
251 int clk_id, unsigned int freq, int dir)
252{
253 struct snd_soc_codec *codec = codec_dai->codec;
254 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
255 switch (freq) {
256 case 12288000:
257 case 18432000:
258 case 24576000:
259 case 36864000:
260 ad193x->sysclk = freq;
261 return 0;
262 }
263 return -EINVAL;
264}
265
Barry Songa1533d92010-03-19 14:48:33 +0800266static int ad193x_hw_params(struct snd_pcm_substream *substream,
267 struct snd_pcm_hw_params *params,
268 struct snd_soc_dai *dai)
269{
Barry Songfab90aa2010-05-21 11:57:01 +0800270 int word_len = 0, reg = 0, master_rate = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800271
272 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000273 struct snd_soc_codec *codec = rtd->codec;
Barry Songfab90aa2010-05-21 11:57:01 +0800274 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Barry Songa1533d92010-03-19 14:48:33 +0800275
276 /* bit size */
277 switch (params_format(params)) {
278 case SNDRV_PCM_FORMAT_S16_LE:
279 word_len = 3;
280 break;
281 case SNDRV_PCM_FORMAT_S20_3LE:
282 word_len = 1;
283 break;
284 case SNDRV_PCM_FORMAT_S24_LE:
285 case SNDRV_PCM_FORMAT_S32_LE:
286 word_len = 0;
287 break;
288 }
289
Barry Songfab90aa2010-05-21 11:57:01 +0800290 switch (ad193x->sysclk) {
291 case 12288000:
292 master_rate = AD193X_PLL_INPUT_256;
293 break;
294 case 18432000:
295 master_rate = AD193X_PLL_INPUT_384;
296 break;
297 case 24576000:
298 master_rate = AD193X_PLL_INPUT_512;
299 break;
300 case 36864000:
301 master_rate = AD193X_PLL_INPUT_768;
302 break;
303 }
304
305 reg = snd_soc_read(codec, AD193X_PLL_CLK_CTRL0);
306 reg = (reg & AD193X_PLL_INPUT_MASK) | master_rate;
307 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, reg);
308
Barry Songa1533d92010-03-19 14:48:33 +0800309 reg = snd_soc_read(codec, AD193X_DAC_CTRL2);
Scott Jiang95c93d82011-08-12 18:04:11 -0400310 reg = (reg & (~AD193X_DAC_WORD_LEN_MASK))
311 | (word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800312 snd_soc_write(codec, AD193X_DAC_CTRL2, reg);
313
314 reg = snd_soc_read(codec, AD193X_ADC_CTRL1);
315 reg = (reg & (~AD193X_ADC_WORD_LEN_MASK)) | word_len;
316 snd_soc_write(codec, AD193X_ADC_CTRL1, reg);
317
318 return 0;
319}
320
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000321static struct snd_soc_dai_ops ad193x_dai_ops = {
322 .hw_params = ad193x_hw_params,
323 .digital_mute = ad193x_mute,
324 .set_tdm_slot = ad193x_set_tdm_slot,
325 .set_sysclk = ad193x_set_dai_sysclk,
326 .set_fmt = ad193x_set_dai_fmt,
327};
328
329/* codec DAI instance */
330static struct snd_soc_dai_driver ad193x_dai = {
331 .name = "ad193x-hifi",
332 .playback = {
333 .stream_name = "Playback",
334 .channels_min = 2,
335 .channels_max = 8,
336 .rates = SNDRV_PCM_RATE_48000,
337 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
338 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
339 },
340 .capture = {
341 .stream_name = "Capture",
342 .channels_min = 2,
343 .channels_max = 4,
344 .rates = SNDRV_PCM_RATE_48000,
345 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
346 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
347 },
348 .ops = &ad193x_dai_ops,
349};
350
351static int ad193x_probe(struct snd_soc_codec *codec)
Barry Songa1533d92010-03-19 14:48:33 +0800352{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000353 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200354 struct snd_soc_dapm_context *dapm = &codec->dapm;
Barry Song9dd7b792010-03-29 11:16:00 +0800355 int ret;
356
Scott Jiang119bfef2011-03-26 03:52:20 -0400357 if (ad193x->control_type == SND_SOC_I2C)
358 ret = snd_soc_codec_set_cache_io(codec, 8, 8, ad193x->control_type);
Barry Songa1533d92010-03-19 14:48:33 +0800359 else
Scott Jiang119bfef2011-03-26 03:52:20 -0400360 ret = snd_soc_codec_set_cache_io(codec, 16, 8, ad193x->control_type);
Barry Songa1533d92010-03-19 14:48:33 +0800361 if (ret < 0) {
Scott Jiang119bfef2011-03-26 03:52:20 -0400362 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
Barry Songa1533d92010-03-19 14:48:33 +0800363 return ret;
364 }
365
366 /* default setting for ad193x */
367
368 /* unmute dac channels */
369 snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0);
370 /* de-emphasis: 48kHz, powedown dac */
371 snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A);
372 /* powerdown dac, dac in tdm mode */
373 snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41);
374 /* high-pass filter enable */
375 snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3);
376 /* sata delay=1, adc aux mode */
377 snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43);
378 /* pll input: mclki/xi */
379 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
380 snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800381
382 snd_soc_add_controls(codec, ad193x_snd_controls,
383 ARRAY_SIZE(ad193x_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200384 snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets,
Barry Songa1533d92010-03-19 14:48:33 +0800385 ARRAY_SIZE(ad193x_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200386 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
Barry Songa1533d92010-03-19 14:48:33 +0800387
Barry Songa1533d92010-03-19 14:48:33 +0800388 return ret;
389}
390
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
Barry Songa1533d92010-03-19 14:48:33 +0800392 .probe = ad193x_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000393 .reg_cache_default = ad193x_reg,
394 .reg_cache_size = AD193X_NUM_REGS,
395 .reg_word_size = sizeof(u16),
Barry Songa1533d92010-03-19 14:48:33 +0800396};
Barry Songa1533d92010-03-19 14:48:33 +0800397
398#if defined(CONFIG_SPI_MASTER)
399static int __devinit ad193x_spi_probe(struct spi_device *spi)
400{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000401 struct ad193x_priv *ad193x;
402 int ret;
403
404 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
405 if (ad193x == NULL)
406 return -ENOMEM;
407
408 spi_set_drvdata(spi, ad193x);
Scott Jiang119bfef2011-03-26 03:52:20 -0400409 ad193x->control_type = SND_SOC_SPI;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000410
411 ret = snd_soc_register_codec(&spi->dev,
412 &soc_codec_dev_ad193x, &ad193x_dai, 1);
413 if (ret < 0)
414 kfree(ad193x);
415 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800416}
417
418static int __devexit ad193x_spi_remove(struct spi_device *spi)
419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 snd_soc_unregister_codec(&spi->dev);
421 kfree(spi_get_drvdata(spi));
422 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800423}
424
425static struct spi_driver ad193x_spi_driver = {
426 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400427 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800428 .owner = THIS_MODULE,
429 },
430 .probe = ad193x_spi_probe,
431 .remove = __devexit_p(ad193x_spi_remove),
432};
433#endif
434
435#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
436static const struct i2c_device_id ad193x_id[] = {
437 { "ad1936", 0 },
438 { "ad1937", 0 },
439 { }
440};
441MODULE_DEVICE_TABLE(i2c, ad193x_id);
442
443static int __devinit ad193x_i2c_probe(struct i2c_client *client,
444 const struct i2c_device_id *id)
445{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000446 struct ad193x_priv *ad193x;
447 int ret;
448
449 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
450 if (ad193x == NULL)
451 return -ENOMEM;
452
453 i2c_set_clientdata(client, ad193x);
Scott Jiang119bfef2011-03-26 03:52:20 -0400454 ad193x->control_type = SND_SOC_I2C;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455
456 ret = snd_soc_register_codec(&client->dev,
457 &soc_codec_dev_ad193x, &ad193x_dai, 1);
458 if (ret < 0)
459 kfree(ad193x);
460 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800461}
462
463static int __devexit ad193x_i2c_remove(struct i2c_client *client)
464{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 snd_soc_unregister_codec(&client->dev);
466 kfree(i2c_get_clientdata(client));
467 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800468}
469
470static struct i2c_driver ad193x_i2c_driver = {
471 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400472 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800473 },
474 .probe = ad193x_i2c_probe,
475 .remove = __devexit_p(ad193x_i2c_remove),
476 .id_table = ad193x_id,
477};
478#endif
479
480static int __init ad193x_modinit(void)
481{
482 int ret;
483
484#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
485 ret = i2c_add_driver(&ad193x_i2c_driver);
486 if (ret != 0) {
487 printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
488 ret);
489 }
490#endif
491
492#if defined(CONFIG_SPI_MASTER)
493 ret = spi_register_driver(&ad193x_spi_driver);
494 if (ret != 0) {
495 printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
496 ret);
497 }
498#endif
499 return ret;
500}
501module_init(ad193x_modinit);
502
503static void __exit ad193x_modexit(void)
504{
505#if defined(CONFIG_SPI_MASTER)
506 spi_unregister_driver(&ad193x_spi_driver);
507#endif
508
509#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
510 i2c_del_driver(&ad193x_i2c_driver);
511#endif
512}
513module_exit(ad193x_modexit);
514
515MODULE_DESCRIPTION("ASoC ad193x driver");
516MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
517MODULE_LICENSE("GPL");