blob: 7d64f2021b062f5d920903f3f349a74227fdc57f [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 {
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +020026 struct regmap *regmap;
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 */
Lars-Peter Clausenc4e7a4a2011-11-28 17:28:08 +010033static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
Barry Songa1533d92010-03-19 14:48:33 +080034
35static const struct soc_enum ad193x_deemp_enum =
36 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp);
37
Lars-Peter Clausen591c0342011-11-28 17:28:07 +010038static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0);
39
Barry Songa1533d92010-03-19 14:48:33 +080040static const struct snd_kcontrol_new ad193x_snd_controls[] = {
41 /* DAC volume control */
Lars-Peter Clausen591c0342011-11-28 17:28:07 +010042 SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL,
43 AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv),
44 SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL,
45 AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv),
46 SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL,
47 AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv),
48 SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL,
49 AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv),
Barry Songa1533d92010-03-19 14:48:33 +080050
51 /* ADC switch control */
52 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
53 AD193X_ADCR1_MUTE, 1, 1),
54 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
55 AD193X_ADCR2_MUTE, 1, 1),
56
57 /* DAC switch control */
58 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
59 AD193X_DACR1_MUTE, 1, 1),
60 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
61 AD193X_DACR2_MUTE, 1, 1),
62 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
63 AD193X_DACR3_MUTE, 1, 1),
64 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
65 AD193X_DACR4_MUTE, 1, 1),
66
67 /* ADC high-pass filter */
68 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
69 AD193X_ADC_HIGHPASS_FILTER, 1, 0),
70
71 /* DAC de-emphasis */
72 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
73};
74
75static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
76 SND_SOC_DAPM_DAC("DAC", "Playback", AD193X_DAC_CTRL0, 0, 1),
77 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
78 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
79 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +010080 SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0),
Barry Songa1533d92010-03-19 14:48:33 +080081 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
82 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
83 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
84 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
85 SND_SOC_DAPM_INPUT("ADC1IN"),
86 SND_SOC_DAPM_INPUT("ADC2IN"),
87};
88
89static const struct snd_soc_dapm_route audio_paths[] = {
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +010090 { "DAC", NULL, "SYSCLK" },
91 { "ADC", NULL, "SYSCLK" },
Barry Songa1533d92010-03-19 14:48:33 +080092 { "DAC", NULL, "ADC_PWR" },
93 { "ADC", NULL, "ADC_PWR" },
Lars-Peter Clausenb21990b2011-11-28 17:28:09 +010094 { "DAC1OUT", NULL, "DAC" },
95 { "DAC2OUT", NULL, "DAC" },
96 { "DAC3OUT", NULL, "DAC" },
97 { "DAC4OUT", NULL, "DAC" },
98 { "ADC", NULL, "ADC1IN" },
99 { "ADC", NULL, "ADC2IN" },
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +0100100 { "SYSCLK", NULL, "PLL_PWR" },
Barry Songa1533d92010-03-19 14:48:33 +0800101};
102
103/*
104 * DAI ops entries
105 */
106
107static int ad193x_mute(struct snd_soc_dai *dai, int mute)
108{
109 struct snd_soc_codec *codec = dai->codec;
Barry Songa1533d92010-03-19 14:48:33 +0800110
Axel Lin54c96cf2011-10-18 06:25:08 +0800111 if (mute)
112 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
113 AD193X_DAC_MASTER_MUTE,
114 AD193X_DAC_MASTER_MUTE);
115 else
116 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
117 AD193X_DAC_MASTER_MUTE, 0);
Barry Songa1533d92010-03-19 14:48:33 +0800118
119 return 0;
120}
121
122static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
123 unsigned int rx_mask, int slots, int width)
124{
125 struct snd_soc_codec *codec = dai->codec;
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100126 unsigned int channels;
Barry Songa1533d92010-03-19 14:48:33 +0800127
128 switch (slots) {
129 case 2:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100130 channels = AD193X_2_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800131 break;
132 case 4:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100133 channels = AD193X_4_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800134 break;
135 case 8:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100136 channels = AD193X_8_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800137 break;
138 case 16:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100139 channels = AD193X_16_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800140 break;
141 default:
142 return -EINVAL;
143 }
144
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100145 snd_soc_update_bits(codec, AD193X_DAC_CTRL1, AD193X_DAC_CHAN_MASK,
146 channels << AD193X_DAC_CHAN_SHFT);
147 snd_soc_update_bits(codec, AD193X_ADC_CTRL2, AD193X_ADC_CHAN_MASK,
148 channels << AD193X_ADC_CHAN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800149
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;
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100157 unsigned int adc_serfmt = 0;
158 unsigned int adc_fmt = 0;
159 unsigned int dac_fmt = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800160
161 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
162 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
163 */
164 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
165 case SND_SOC_DAIFMT_I2S:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100166 adc_serfmt |= AD193X_ADC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800167 break;
168 case SND_SOC_DAIFMT_DSP_A:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100169 adc_serfmt |= AD193X_ADC_SERFMT_AUX;
Barry Songa1533d92010-03-19 14:48:33 +0800170 break;
171 default:
172 return -EINVAL;
173 }
174
175 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
176 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songa1533d92010-03-19 14:48:33 +0800177 break;
178 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100179 adc_fmt |= AD193X_ADC_LEFT_HIGH;
180 dac_fmt |= AD193X_DAC_LEFT_HIGH;
Barry Songa1533d92010-03-19 14:48:33 +0800181 break;
182 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100183 adc_fmt |= AD193X_ADC_BCLK_INV;
184 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800185 break;
Barry Songa1533d92010-03-19 14:48:33 +0800186 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100187 adc_fmt |= AD193X_ADC_LEFT_HIGH;
188 adc_fmt |= AD193X_ADC_BCLK_INV;
189 dac_fmt |= AD193X_DAC_LEFT_HIGH;
190 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800191 break;
192 default:
193 return -EINVAL;
194 }
195
196 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
197 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100198 adc_fmt |= AD193X_ADC_LCR_MASTER;
199 adc_fmt |= AD193X_ADC_BCLK_MASTER;
200 dac_fmt |= AD193X_DAC_LCR_MASTER;
201 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800202 break;
203 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100204 adc_fmt |= AD193X_ADC_LCR_MASTER;
205 dac_fmt |= AD193X_DAC_LCR_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800206 break;
207 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100208 adc_fmt |= AD193X_ADC_BCLK_MASTER;
209 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800210 break;
211 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songa1533d92010-03-19 14:48:33 +0800212 break;
213 default:
214 return -EINVAL;
215 }
216
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100217 snd_soc_update_bits(codec, AD193X_ADC_CTRL1, AD193X_ADC_SERFMT_MASK,
218 adc_serfmt);
219 snd_soc_update_bits(codec, AD193X_ADC_CTRL2, AD193X_ADC_FMT_MASK,
220 adc_fmt);
221 snd_soc_update_bits(codec, AD193X_DAC_CTRL1, AD193X_DAC_FMT_MASK,
222 dac_fmt);
Barry Songa1533d92010-03-19 14:48:33 +0800223
224 return 0;
225}
226
Barry Songfab90aa2010-05-21 11:57:01 +0800227static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
228 int clk_id, unsigned int freq, int dir)
229{
230 struct snd_soc_codec *codec = codec_dai->codec;
231 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
232 switch (freq) {
233 case 12288000:
234 case 18432000:
235 case 24576000:
236 case 36864000:
237 ad193x->sysclk = freq;
238 return 0;
239 }
240 return -EINVAL;
241}
242
Barry Songa1533d92010-03-19 14:48:33 +0800243static int ad193x_hw_params(struct snd_pcm_substream *substream,
244 struct snd_pcm_hw_params *params,
245 struct snd_soc_dai *dai)
246{
Axel Lin54c96cf2011-10-18 06:25:08 +0800247 int word_len = 0, master_rate = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800248
249 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000250 struct snd_soc_codec *codec = rtd->codec;
Barry Songfab90aa2010-05-21 11:57:01 +0800251 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Barry Songa1533d92010-03-19 14:48:33 +0800252
253 /* bit size */
254 switch (params_format(params)) {
255 case SNDRV_PCM_FORMAT_S16_LE:
256 word_len = 3;
257 break;
258 case SNDRV_PCM_FORMAT_S20_3LE:
259 word_len = 1;
260 break;
261 case SNDRV_PCM_FORMAT_S24_LE:
262 case SNDRV_PCM_FORMAT_S32_LE:
263 word_len = 0;
264 break;
265 }
266
Barry Songfab90aa2010-05-21 11:57:01 +0800267 switch (ad193x->sysclk) {
268 case 12288000:
269 master_rate = AD193X_PLL_INPUT_256;
270 break;
271 case 18432000:
272 master_rate = AD193X_PLL_INPUT_384;
273 break;
274 case 24576000:
275 master_rate = AD193X_PLL_INPUT_512;
276 break;
277 case 36864000:
278 master_rate = AD193X_PLL_INPUT_768;
279 break;
280 }
281
Axel Lin54c96cf2011-10-18 06:25:08 +0800282 snd_soc_update_bits(codec, AD193X_PLL_CLK_CTRL0,
283 AD193X_PLL_INPUT_MASK, master_rate);
Barry Songfab90aa2010-05-21 11:57:01 +0800284
Axel Lin54c96cf2011-10-18 06:25:08 +0800285 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
286 AD193X_DAC_WORD_LEN_MASK,
287 word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800288
Axel Lin54c96cf2011-10-18 06:25:08 +0800289 snd_soc_update_bits(codec, AD193X_ADC_CTRL1,
290 AD193X_ADC_WORD_LEN_MASK, word_len);
Barry Songa1533d92010-03-19 14:48:33 +0800291
292 return 0;
293}
294
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100295static const struct snd_soc_dai_ops ad193x_dai_ops = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000296 .hw_params = ad193x_hw_params,
297 .digital_mute = ad193x_mute,
298 .set_tdm_slot = ad193x_set_tdm_slot,
299 .set_sysclk = ad193x_set_dai_sysclk,
300 .set_fmt = ad193x_set_dai_fmt,
301};
302
303/* codec DAI instance */
304static struct snd_soc_dai_driver ad193x_dai = {
305 .name = "ad193x-hifi",
306 .playback = {
307 .stream_name = "Playback",
308 .channels_min = 2,
309 .channels_max = 8,
310 .rates = SNDRV_PCM_RATE_48000,
311 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
312 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
313 },
314 .capture = {
315 .stream_name = "Capture",
316 .channels_min = 2,
317 .channels_max = 4,
318 .rates = SNDRV_PCM_RATE_48000,
319 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
320 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
321 },
322 .ops = &ad193x_dai_ops,
323};
324
325static int ad193x_probe(struct snd_soc_codec *codec)
Barry Songa1533d92010-03-19 14:48:33 +0800326{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000327 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Barry Song9dd7b792010-03-29 11:16:00 +0800328 int ret;
329
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200330 codec->control_data = ad193x->regmap;
331 ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
Barry Songa1533d92010-03-19 14:48:33 +0800332 if (ret < 0) {
Scott Jiang119bfef2011-03-26 03:52:20 -0400333 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
Barry Songa1533d92010-03-19 14:48:33 +0800334 return ret;
335 }
336
337 /* default setting for ad193x */
338
339 /* unmute dac channels */
340 snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0);
341 /* de-emphasis: 48kHz, powedown dac */
342 snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A);
343 /* powerdown dac, dac in tdm mode */
344 snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41);
345 /* high-pass filter enable */
346 snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3);
347 /* sata delay=1, adc aux mode */
348 snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43);
349 /* pll input: mclki/xi */
350 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
351 snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800352
Barry Songa1533d92010-03-19 14:48:33 +0800353 return ret;
354}
355
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000356static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
Barry Songa1533d92010-03-19 14:48:33 +0800357 .probe = ad193x_probe,
Lars-Peter Clausenb90d4182011-11-28 17:28:06 +0100358 .controls = ad193x_snd_controls,
359 .num_controls = ARRAY_SIZE(ad193x_snd_controls),
360 .dapm_widgets = ad193x_dapm_widgets,
361 .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets),
362 .dapm_routes = audio_paths,
363 .num_dapm_routes = ARRAY_SIZE(audio_paths),
Barry Songa1533d92010-03-19 14:48:33 +0800364};
Barry Songa1533d92010-03-19 14:48:33 +0800365
366#if defined(CONFIG_SPI_MASTER)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200367
368static const struct regmap_config ad193x_spi_regmap_config = {
369 .val_bits = 8,
370 .reg_bits = 16,
371 .read_flag_mask = 0x09,
372 .write_flag_mask = 0x08,
373};
374
Barry Songa1533d92010-03-19 14:48:33 +0800375static int __devinit ad193x_spi_probe(struct spi_device *spi)
376{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377 struct ad193x_priv *ad193x;
378 int ret;
379
380 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
381 if (ad193x == NULL)
382 return -ENOMEM;
383
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200384 ad193x->regmap = regmap_init_spi(spi, &ad193x_spi_regmap_config);
385 if (IS_ERR(ad193x->regmap)) {
386 ret = PTR_ERR(ad193x->regmap);
387 goto err_free;
388 }
389
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000390 spi_set_drvdata(spi, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391
392 ret = snd_soc_register_codec(&spi->dev,
393 &soc_codec_dev_ad193x, &ad193x_dai, 1);
394 if (ret < 0)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200395 goto err_regmap_exit;
396
397 return 0;
398
399err_regmap_exit:
400 regmap_exit(ad193x->regmap);
401err_free:
402 kfree(ad193x);
403
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000404 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800405}
406
407static int __devexit ad193x_spi_remove(struct spi_device *spi)
408{
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200409 struct ad193x_priv *ad193x = spi_get_drvdata(spi);
410
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000411 snd_soc_unregister_codec(&spi->dev);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200412 regmap_exit(ad193x->regmap);
413 kfree(ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000414 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)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200428
429static const struct regmap_config ad193x_i2c_regmap_config = {
430 .val_bits = 8,
431 .reg_bits = 8,
432};
433
Barry Songa1533d92010-03-19 14:48:33 +0800434static const struct i2c_device_id ad193x_id[] = {
435 { "ad1936", 0 },
436 { "ad1937", 0 },
437 { }
438};
439MODULE_DEVICE_TABLE(i2c, ad193x_id);
440
441static int __devinit ad193x_i2c_probe(struct i2c_client *client,
442 const struct i2c_device_id *id)
443{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000444 struct ad193x_priv *ad193x;
445 int ret;
446
447 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
448 if (ad193x == NULL)
449 return -ENOMEM;
450
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200451 ad193x->regmap = regmap_init_i2c(client, &ad193x_i2c_regmap_config);
452 if (IS_ERR(ad193x->regmap)) {
453 ret = PTR_ERR(ad193x->regmap);
454 goto err_free;
455 }
456
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000457 i2c_set_clientdata(client, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458
459 ret = snd_soc_register_codec(&client->dev,
460 &soc_codec_dev_ad193x, &ad193x_dai, 1);
461 if (ret < 0)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200462 goto err_regmap_exit;
463
464 return 0;
465
466err_regmap_exit:
467 regmap_exit(ad193x->regmap);
468err_free:
469 kfree(ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000470 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800471}
472
473static int __devexit ad193x_i2c_remove(struct i2c_client *client)
474{
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200475 struct ad193x_priv *ad193x = i2c_get_clientdata(client);
476
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000477 snd_soc_unregister_codec(&client->dev);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200478 regmap_exit(ad193x->regmap);
479 kfree(ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800481}
482
483static struct i2c_driver ad193x_i2c_driver = {
484 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400485 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800486 },
487 .probe = ad193x_i2c_probe,
488 .remove = __devexit_p(ad193x_i2c_remove),
489 .id_table = ad193x_id,
490};
491#endif
492
493static int __init ad193x_modinit(void)
494{
495 int ret;
496
497#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
498 ret = i2c_add_driver(&ad193x_i2c_driver);
499 if (ret != 0) {
500 printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
501 ret);
502 }
503#endif
504
505#if defined(CONFIG_SPI_MASTER)
506 ret = spi_register_driver(&ad193x_spi_driver);
507 if (ret != 0) {
508 printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
509 ret);
510 }
511#endif
512 return ret;
513}
514module_init(ad193x_modinit);
515
516static void __exit ad193x_modexit(void)
517{
518#if defined(CONFIG_SPI_MASTER)
519 spi_unregister_driver(&ad193x_spi_driver);
520#endif
521
522#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
523 i2c_del_driver(&ad193x_i2c_driver);
524#endif
525}
526module_exit(ad193x_modexit);
527
528MODULE_DESCRIPTION("ASoC ad193x driver");
529MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
530MODULE_LICENSE("GPL");