blob: 2374ca5ffe68bacc6c37db16be249858fb17fad2 [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);
310 reg = (reg & (~AD193X_DAC_WORD_LEN_MASK)) | word_len;
311 snd_soc_write(codec, AD193X_DAC_CTRL2, reg);
312
313 reg = snd_soc_read(codec, AD193X_ADC_CTRL1);
314 reg = (reg & (~AD193X_ADC_WORD_LEN_MASK)) | word_len;
315 snd_soc_write(codec, AD193X_ADC_CTRL1, reg);
316
317 return 0;
318}
319
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320static struct snd_soc_dai_ops ad193x_dai_ops = {
321 .hw_params = ad193x_hw_params,
322 .digital_mute = ad193x_mute,
323 .set_tdm_slot = ad193x_set_tdm_slot,
324 .set_sysclk = ad193x_set_dai_sysclk,
325 .set_fmt = ad193x_set_dai_fmt,
326};
327
328/* codec DAI instance */
329static struct snd_soc_dai_driver ad193x_dai = {
330 .name = "ad193x-hifi",
331 .playback = {
332 .stream_name = "Playback",
333 .channels_min = 2,
334 .channels_max = 8,
335 .rates = SNDRV_PCM_RATE_48000,
336 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
337 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
338 },
339 .capture = {
340 .stream_name = "Capture",
341 .channels_min = 2,
342 .channels_max = 4,
343 .rates = SNDRV_PCM_RATE_48000,
344 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
345 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
346 },
347 .ops = &ad193x_dai_ops,
348};
349
350static int ad193x_probe(struct snd_soc_codec *codec)
Barry Songa1533d92010-03-19 14:48:33 +0800351{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000352 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200353 struct snd_soc_dapm_context *dapm = &codec->dapm;
Barry Song9dd7b792010-03-29 11:16:00 +0800354 int ret;
355
Scott Jiang119bfef2011-03-26 03:52:20 -0400356 if (ad193x->control_type == SND_SOC_I2C)
357 ret = snd_soc_codec_set_cache_io(codec, 8, 8, ad193x->control_type);
Barry Songa1533d92010-03-19 14:48:33 +0800358 else
Scott Jiang119bfef2011-03-26 03:52:20 -0400359 ret = snd_soc_codec_set_cache_io(codec, 16, 8, ad193x->control_type);
Barry Songa1533d92010-03-19 14:48:33 +0800360 if (ret < 0) {
Scott Jiang119bfef2011-03-26 03:52:20 -0400361 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
Barry Songa1533d92010-03-19 14:48:33 +0800362 return ret;
363 }
364
365 /* default setting for ad193x */
366
367 /* unmute dac channels */
368 snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0);
369 /* de-emphasis: 48kHz, powedown dac */
370 snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A);
371 /* powerdown dac, dac in tdm mode */
372 snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41);
373 /* high-pass filter enable */
374 snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3);
375 /* sata delay=1, adc aux mode */
376 snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43);
377 /* pll input: mclki/xi */
378 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
379 snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800380
381 snd_soc_add_controls(codec, ad193x_snd_controls,
382 ARRAY_SIZE(ad193x_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200383 snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets,
Barry Songa1533d92010-03-19 14:48:33 +0800384 ARRAY_SIZE(ad193x_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200385 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
Barry Songa1533d92010-03-19 14:48:33 +0800386
Barry Songa1533d92010-03-19 14:48:33 +0800387 return ret;
388}
389
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000390static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
Barry Songa1533d92010-03-19 14:48:33 +0800391 .probe = ad193x_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000392 .reg_cache_default = ad193x_reg,
393 .reg_cache_size = AD193X_NUM_REGS,
394 .reg_word_size = sizeof(u16),
Barry Songa1533d92010-03-19 14:48:33 +0800395};
Barry Songa1533d92010-03-19 14:48:33 +0800396
397#if defined(CONFIG_SPI_MASTER)
398static int __devinit ad193x_spi_probe(struct spi_device *spi)
399{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000400 struct ad193x_priv *ad193x;
401 int ret;
402
403 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
404 if (ad193x == NULL)
405 return -ENOMEM;
406
407 spi_set_drvdata(spi, ad193x);
Scott Jiang119bfef2011-03-26 03:52:20 -0400408 ad193x->control_type = SND_SOC_SPI;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000409
410 ret = snd_soc_register_codec(&spi->dev,
411 &soc_codec_dev_ad193x, &ad193x_dai, 1);
412 if (ret < 0)
413 kfree(ad193x);
414 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800415}
416
417static int __devexit ad193x_spi_remove(struct spi_device *spi)
418{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000419 snd_soc_unregister_codec(&spi->dev);
420 kfree(spi_get_drvdata(spi));
421 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800422}
423
424static struct spi_driver ad193x_spi_driver = {
425 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400426 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800427 .owner = THIS_MODULE,
428 },
429 .probe = ad193x_spi_probe,
430 .remove = __devexit_p(ad193x_spi_remove),
431};
432#endif
433
434#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
435static const struct i2c_device_id ad193x_id[] = {
436 { "ad1936", 0 },
437 { "ad1937", 0 },
438 { }
439};
440MODULE_DEVICE_TABLE(i2c, ad193x_id);
441
442static int __devinit ad193x_i2c_probe(struct i2c_client *client,
443 const struct i2c_device_id *id)
444{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000445 struct ad193x_priv *ad193x;
446 int ret;
447
448 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
449 if (ad193x == NULL)
450 return -ENOMEM;
451
452 i2c_set_clientdata(client, ad193x);
Scott Jiang119bfef2011-03-26 03:52:20 -0400453 ad193x->control_type = SND_SOC_I2C;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000454
455 ret = snd_soc_register_codec(&client->dev,
456 &soc_codec_dev_ad193x, &ad193x_dai, 1);
457 if (ret < 0)
458 kfree(ad193x);
459 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800460}
461
462static int __devexit ad193x_i2c_remove(struct i2c_client *client)
463{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 snd_soc_unregister_codec(&client->dev);
465 kfree(i2c_get_clientdata(client));
466 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800467}
468
469static struct i2c_driver ad193x_i2c_driver = {
470 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400471 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800472 },
473 .probe = ad193x_i2c_probe,
474 .remove = __devexit_p(ad193x_i2c_remove),
475 .id_table = ad193x_id,
476};
477#endif
478
479static int __init ad193x_modinit(void)
480{
481 int ret;
482
483#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
484 ret = i2c_add_driver(&ad193x_i2c_driver);
485 if (ret != 0) {
486 printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
487 ret);
488 }
489#endif
490
491#if defined(CONFIG_SPI_MASTER)
492 ret = spi_register_driver(&ad193x_spi_driver);
493 if (ret != 0) {
494 printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
495 ret);
496 }
497#endif
498 return ret;
499}
500module_init(ad193x_modinit);
501
502static void __exit ad193x_modexit(void)
503{
504#if defined(CONFIG_SPI_MASTER)
505 spi_unregister_driver(&ad193x_spi_driver);
506#endif
507
508#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
509 i2c_del_driver(&ad193x_i2c_driver);
510#endif
511}
512module_exit(ad193x_modexit);
513
514MODULE_DESCRIPTION("ASoC ad193x driver");
515MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
516MODULE_LICENSE("GPL");