blob: 120602130b5c0a667cc612ab97877d016bb5a667 [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 */
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;
Barry Songa1533d92010-03-19 14:48:33 +0800106
Axel Lin54c96cf2011-10-18 06:25:08 +0800107 if (mute)
108 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
109 AD193X_DAC_MASTER_MUTE,
110 AD193X_DAC_MASTER_MUTE);
111 else
112 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
113 AD193X_DAC_MASTER_MUTE, 0);
Barry Songa1533d92010-03-19 14:48:33 +0800114
115 return 0;
116}
117
118static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
119 unsigned int rx_mask, int slots, int width)
120{
121 struct snd_soc_codec *codec = dai->codec;
122 int dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
123 int adc_reg = snd_soc_read(codec, AD193X_ADC_CTRL2);
124
125 dac_reg &= ~AD193X_DAC_CHAN_MASK;
126 adc_reg &= ~AD193X_ADC_CHAN_MASK;
127
128 switch (slots) {
129 case 2:
130 dac_reg |= AD193X_DAC_2_CHANNELS << AD193X_DAC_CHAN_SHFT;
131 adc_reg |= AD193X_ADC_2_CHANNELS << AD193X_ADC_CHAN_SHFT;
132 break;
133 case 4:
134 dac_reg |= AD193X_DAC_4_CHANNELS << AD193X_DAC_CHAN_SHFT;
135 adc_reg |= AD193X_ADC_4_CHANNELS << AD193X_ADC_CHAN_SHFT;
136 break;
137 case 8:
138 dac_reg |= AD193X_DAC_8_CHANNELS << AD193X_DAC_CHAN_SHFT;
139 adc_reg |= AD193X_ADC_8_CHANNELS << AD193X_ADC_CHAN_SHFT;
140 break;
141 case 16:
142 dac_reg |= AD193X_DAC_16_CHANNELS << AD193X_DAC_CHAN_SHFT;
143 adc_reg |= AD193X_ADC_16_CHANNELS << AD193X_ADC_CHAN_SHFT;
144 break;
145 default:
146 return -EINVAL;
147 }
148
149 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
150 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg);
151
152 return 0;
153}
154
155static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
156 unsigned int fmt)
157{
158 struct snd_soc_codec *codec = codec_dai->codec;
Barry Songd6bdc0f2010-04-21 17:36:48 +0800159 int adc_reg1, adc_reg2, dac_reg;
Barry Songa1533d92010-03-19 14:48:33 +0800160
Barry Songd6bdc0f2010-04-21 17:36:48 +0800161 adc_reg1 = snd_soc_read(codec, AD193X_ADC_CTRL1);
162 adc_reg2 = snd_soc_read(codec, AD193X_ADC_CTRL2);
Barry Songa1533d92010-03-19 14:48:33 +0800163 dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
164
165 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
166 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
167 */
168 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
169 case SND_SOC_DAIFMT_I2S:
Barry Songd6bdc0f2010-04-21 17:36:48 +0800170 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
171 adc_reg1 |= AD193X_ADC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800172 break;
173 case SND_SOC_DAIFMT_DSP_A:
Barry Songd6bdc0f2010-04-21 17:36:48 +0800174 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
175 adc_reg1 |= AD193X_ADC_SERFMT_AUX;
Barry Songa1533d92010-03-19 14:48:33 +0800176 break;
177 default:
178 return -EINVAL;
179 }
180
181 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
182 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800183 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
184 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800185 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
186 dac_reg &= ~AD193X_DAC_BCLK_INV;
187 break;
188 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800189 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
190 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800191 dac_reg |= AD193X_DAC_LEFT_HIGH;
192 dac_reg &= ~AD193X_DAC_BCLK_INV;
193 break;
194 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800195 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
196 adc_reg2 |= AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800197 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
198 dac_reg |= AD193X_DAC_BCLK_INV;
199 break;
200
201 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800202 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
203 adc_reg2 |= AD193X_ADC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800204 dac_reg |= AD193X_DAC_LEFT_HIGH;
205 dac_reg |= AD193X_DAC_BCLK_INV;
206 break;
207 default:
208 return -EINVAL;
209 }
210
211 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
212 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800213 adc_reg2 |= AD193X_ADC_LCR_MASTER;
214 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800215 dac_reg |= AD193X_DAC_LCR_MASTER;
216 dac_reg |= AD193X_DAC_BCLK_MASTER;
217 break;
218 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800219 adc_reg2 |= AD193X_ADC_LCR_MASTER;
220 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800221 dac_reg |= AD193X_DAC_LCR_MASTER;
222 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
223 break;
224 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800225 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
226 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800227 dac_reg &= ~AD193X_DAC_LCR_MASTER;
228 dac_reg |= AD193X_DAC_BCLK_MASTER;
229 break;
230 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songd6bdc0f2010-04-21 17:36:48 +0800231 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
232 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800233 dac_reg &= ~AD193X_DAC_LCR_MASTER;
234 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
235 break;
236 default:
237 return -EINVAL;
238 }
239
Barry Songd6bdc0f2010-04-21 17:36:48 +0800240 snd_soc_write(codec, AD193X_ADC_CTRL1, adc_reg1);
241 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg2);
Barry Songa1533d92010-03-19 14:48:33 +0800242 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
243
244 return 0;
245}
246
Barry Songfab90aa2010-05-21 11:57:01 +0800247static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
248 int clk_id, unsigned int freq, int dir)
249{
250 struct snd_soc_codec *codec = codec_dai->codec;
251 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
252 switch (freq) {
253 case 12288000:
254 case 18432000:
255 case 24576000:
256 case 36864000:
257 ad193x->sysclk = freq;
258 return 0;
259 }
260 return -EINVAL;
261}
262
Barry Songa1533d92010-03-19 14:48:33 +0800263static int ad193x_hw_params(struct snd_pcm_substream *substream,
264 struct snd_pcm_hw_params *params,
265 struct snd_soc_dai *dai)
266{
Axel Lin54c96cf2011-10-18 06:25:08 +0800267 int word_len = 0, master_rate = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800268
269 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000270 struct snd_soc_codec *codec = rtd->codec;
Barry Songfab90aa2010-05-21 11:57:01 +0800271 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Barry Songa1533d92010-03-19 14:48:33 +0800272
273 /* bit size */
274 switch (params_format(params)) {
275 case SNDRV_PCM_FORMAT_S16_LE:
276 word_len = 3;
277 break;
278 case SNDRV_PCM_FORMAT_S20_3LE:
279 word_len = 1;
280 break;
281 case SNDRV_PCM_FORMAT_S24_LE:
282 case SNDRV_PCM_FORMAT_S32_LE:
283 word_len = 0;
284 break;
285 }
286
Barry Songfab90aa2010-05-21 11:57:01 +0800287 switch (ad193x->sysclk) {
288 case 12288000:
289 master_rate = AD193X_PLL_INPUT_256;
290 break;
291 case 18432000:
292 master_rate = AD193X_PLL_INPUT_384;
293 break;
294 case 24576000:
295 master_rate = AD193X_PLL_INPUT_512;
296 break;
297 case 36864000:
298 master_rate = AD193X_PLL_INPUT_768;
299 break;
300 }
301
Axel Lin54c96cf2011-10-18 06:25:08 +0800302 snd_soc_update_bits(codec, AD193X_PLL_CLK_CTRL0,
303 AD193X_PLL_INPUT_MASK, master_rate);
Barry Songfab90aa2010-05-21 11:57:01 +0800304
Axel Lin54c96cf2011-10-18 06:25:08 +0800305 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
306 AD193X_DAC_WORD_LEN_MASK,
307 word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800308
Axel Lin54c96cf2011-10-18 06:25:08 +0800309 snd_soc_update_bits(codec, AD193X_ADC_CTRL1,
310 AD193X_ADC_WORD_LEN_MASK, word_len);
Barry Songa1533d92010-03-19 14:48:33 +0800311
312 return 0;
313}
314
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000315static struct snd_soc_dai_ops ad193x_dai_ops = {
316 .hw_params = ad193x_hw_params,
317 .digital_mute = ad193x_mute,
318 .set_tdm_slot = ad193x_set_tdm_slot,
319 .set_sysclk = ad193x_set_dai_sysclk,
320 .set_fmt = ad193x_set_dai_fmt,
321};
322
323/* codec DAI instance */
324static struct snd_soc_dai_driver ad193x_dai = {
325 .name = "ad193x-hifi",
326 .playback = {
327 .stream_name = "Playback",
328 .channels_min = 2,
329 .channels_max = 8,
330 .rates = SNDRV_PCM_RATE_48000,
331 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
332 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
333 },
334 .capture = {
335 .stream_name = "Capture",
336 .channels_min = 2,
337 .channels_max = 4,
338 .rates = SNDRV_PCM_RATE_48000,
339 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
340 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
341 },
342 .ops = &ad193x_dai_ops,
343};
344
345static int ad193x_probe(struct snd_soc_codec *codec)
Barry Songa1533d92010-03-19 14:48:33 +0800346{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000347 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200348 struct snd_soc_dapm_context *dapm = &codec->dapm;
Barry Song9dd7b792010-03-29 11:16:00 +0800349 int ret;
350
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200351 codec->control_data = ad193x->regmap;
352 ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
Barry Songa1533d92010-03-19 14:48:33 +0800353 if (ret < 0) {
Scott Jiang119bfef2011-03-26 03:52:20 -0400354 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
Barry Songa1533d92010-03-19 14:48:33 +0800355 return ret;
356 }
357
358 /* default setting for ad193x */
359
360 /* unmute dac channels */
361 snd_soc_write(codec, AD193X_DAC_CHNL_MUTE, 0x0);
362 /* de-emphasis: 48kHz, powedown dac */
363 snd_soc_write(codec, AD193X_DAC_CTRL2, 0x1A);
364 /* powerdown dac, dac in tdm mode */
365 snd_soc_write(codec, AD193X_DAC_CTRL0, 0x41);
366 /* high-pass filter enable */
367 snd_soc_write(codec, AD193X_ADC_CTRL0, 0x3);
368 /* sata delay=1, adc aux mode */
369 snd_soc_write(codec, AD193X_ADC_CTRL1, 0x43);
370 /* pll input: mclki/xi */
371 snd_soc_write(codec, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
372 snd_soc_write(codec, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800373
374 snd_soc_add_controls(codec, ad193x_snd_controls,
375 ARRAY_SIZE(ad193x_snd_controls));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200376 snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets,
Barry Songa1533d92010-03-19 14:48:33 +0800377 ARRAY_SIZE(ad193x_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200378 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
Barry Songa1533d92010-03-19 14:48:33 +0800379
Barry Songa1533d92010-03-19 14:48:33 +0800380 return ret;
381}
382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
Barry Songa1533d92010-03-19 14:48:33 +0800384 .probe = ad193x_probe,
Barry Songa1533d92010-03-19 14:48:33 +0800385};
Barry Songa1533d92010-03-19 14:48:33 +0800386
387#if defined(CONFIG_SPI_MASTER)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200388
389static const struct regmap_config ad193x_spi_regmap_config = {
390 .val_bits = 8,
391 .reg_bits = 16,
392 .read_flag_mask = 0x09,
393 .write_flag_mask = 0x08,
394};
395
Barry Songa1533d92010-03-19 14:48:33 +0800396static int __devinit ad193x_spi_probe(struct spi_device *spi)
397{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000398 struct ad193x_priv *ad193x;
399 int ret;
400
401 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
402 if (ad193x == NULL)
403 return -ENOMEM;
404
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200405 ad193x->regmap = regmap_init_spi(spi, &ad193x_spi_regmap_config);
406 if (IS_ERR(ad193x->regmap)) {
407 ret = PTR_ERR(ad193x->regmap);
408 goto err_free;
409 }
410
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000411 spi_set_drvdata(spi, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000412
413 ret = snd_soc_register_codec(&spi->dev,
414 &soc_codec_dev_ad193x, &ad193x_dai, 1);
415 if (ret < 0)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200416 goto err_regmap_exit;
417
418 return 0;
419
420err_regmap_exit:
421 regmap_exit(ad193x->regmap);
422err_free:
423 kfree(ad193x);
424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000425 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800426}
427
428static int __devexit ad193x_spi_remove(struct spi_device *spi)
429{
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200430 struct ad193x_priv *ad193x = spi_get_drvdata(spi);
431
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000432 snd_soc_unregister_codec(&spi->dev);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200433 regmap_exit(ad193x->regmap);
434 kfree(ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000435 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800436}
437
438static struct spi_driver ad193x_spi_driver = {
439 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400440 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800441 .owner = THIS_MODULE,
442 },
443 .probe = ad193x_spi_probe,
444 .remove = __devexit_p(ad193x_spi_remove),
445};
446#endif
447
448#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200449
450static const struct regmap_config ad193x_i2c_regmap_config = {
451 .val_bits = 8,
452 .reg_bits = 8,
453};
454
Barry Songa1533d92010-03-19 14:48:33 +0800455static const struct i2c_device_id ad193x_id[] = {
456 { "ad1936", 0 },
457 { "ad1937", 0 },
458 { }
459};
460MODULE_DEVICE_TABLE(i2c, ad193x_id);
461
462static int __devinit ad193x_i2c_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
464{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000465 struct ad193x_priv *ad193x;
466 int ret;
467
468 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
469 if (ad193x == NULL)
470 return -ENOMEM;
471
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200472 ad193x->regmap = regmap_init_i2c(client, &ad193x_i2c_regmap_config);
473 if (IS_ERR(ad193x->regmap)) {
474 ret = PTR_ERR(ad193x->regmap);
475 goto err_free;
476 }
477
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 i2c_set_clientdata(client, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000479
480 ret = snd_soc_register_codec(&client->dev,
481 &soc_codec_dev_ad193x, &ad193x_dai, 1);
482 if (ret < 0)
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200483 goto err_regmap_exit;
484
485 return 0;
486
487err_regmap_exit:
488 regmap_exit(ad193x->regmap);
489err_free:
490 kfree(ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 return ret;
Barry Songa1533d92010-03-19 14:48:33 +0800492}
493
494static int __devexit ad193x_i2c_remove(struct i2c_client *client)
495{
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200496 struct ad193x_priv *ad193x = i2c_get_clientdata(client);
497
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000498 snd_soc_unregister_codec(&client->dev);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200499 regmap_exit(ad193x->regmap);
500 kfree(ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800502}
503
504static struct i2c_driver ad193x_i2c_driver = {
505 .driver = {
Mike Frysingere43a7d42011-03-27 00:44:12 -0400506 .name = "ad193x",
Barry Songa1533d92010-03-19 14:48:33 +0800507 },
508 .probe = ad193x_i2c_probe,
509 .remove = __devexit_p(ad193x_i2c_remove),
510 .id_table = ad193x_id,
511};
512#endif
513
514static int __init ad193x_modinit(void)
515{
516 int ret;
517
518#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
519 ret = i2c_add_driver(&ad193x_i2c_driver);
520 if (ret != 0) {
521 printk(KERN_ERR "Failed to register AD193X I2C driver: %d\n",
522 ret);
523 }
524#endif
525
526#if defined(CONFIG_SPI_MASTER)
527 ret = spi_register_driver(&ad193x_spi_driver);
528 if (ret != 0) {
529 printk(KERN_ERR "Failed to register AD193X SPI driver: %d\n",
530 ret);
531 }
532#endif
533 return ret;
534}
535module_init(ad193x_modinit);
536
537static void __exit ad193x_modexit(void)
538{
539#if defined(CONFIG_SPI_MASTER)
540 spi_unregister_driver(&ad193x_spi_driver);
541#endif
542
543#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
544 i2c_del_driver(&ad193x_i2c_driver);
545#endif
546}
547module_exit(ad193x_modexit);
548
549MODULE_DESCRIPTION("ASoC ad193x driver");
550MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
551MODULE_LICENSE("GPL");