I2C: Add helper macro for i2c_driver boilerplate
[zen-stable.git] / sound / soc / codecs / ad193x.c
blob120602130b5c0a667cc612ab97877d016bb5a667
1 /*
2 * AD193X Audio Codec driver supporting AD1936/7/8/9
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
15 #include <linux/slab.h>
16 #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>
22 #include "ad193x.h"
24 /* codec private data */
25 struct ad193x_priv {
26 struct regmap *regmap;
27 int sysclk;
31 * AD193X volume/mute/de-emphasis etc. controls
33 static const char *ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
35 static const struct soc_enum ad193x_deemp_enum =
36 SOC_ENUM_SINGLE(AD193X_DAC_CTRL2, 1, 4, ad193x_deemp);
38 static const struct snd_kcontrol_new ad193x_snd_controls[] = {
39 /* DAC volume control */
40 SOC_DOUBLE_R("DAC1 Volume", AD193X_DAC_L1_VOL,
41 AD193X_DAC_R1_VOL, 0, 0xFF, 1),
42 SOC_DOUBLE_R("DAC2 Volume", AD193X_DAC_L2_VOL,
43 AD193X_DAC_R2_VOL, 0, 0xFF, 1),
44 SOC_DOUBLE_R("DAC3 Volume", AD193X_DAC_L3_VOL,
45 AD193X_DAC_R3_VOL, 0, 0xFF, 1),
46 SOC_DOUBLE_R("DAC4 Volume", AD193X_DAC_L4_VOL,
47 AD193X_DAC_R4_VOL, 0, 0xFF, 1),
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),
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),
65 /* ADC high-pass filter */
66 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
67 AD193X_ADC_HIGHPASS_FILTER, 1, 0),
69 /* DAC de-emphasis */
70 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
73 static 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"),
86 static 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" },
100 * DAI ops entries
103 static int ad193x_mute(struct snd_soc_dai *dai, int mute)
105 struct snd_soc_codec *codec = dai->codec;
107 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);
115 return 0;
118 static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
119 unsigned int rx_mask, int slots, int width)
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);
125 dac_reg &= ~AD193X_DAC_CHAN_MASK;
126 adc_reg &= ~AD193X_ADC_CHAN_MASK;
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;
149 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
150 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg);
152 return 0;
155 static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
156 unsigned int fmt)
158 struct snd_soc_codec *codec = codec_dai->codec;
159 int adc_reg1, adc_reg2, dac_reg;
161 adc_reg1 = snd_soc_read(codec, AD193X_ADC_CTRL1);
162 adc_reg2 = snd_soc_read(codec, AD193X_ADC_CTRL2);
163 dac_reg = snd_soc_read(codec, AD193X_DAC_CTRL1);
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)
168 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
169 case SND_SOC_DAIFMT_I2S:
170 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
171 adc_reg1 |= AD193X_ADC_SERFMT_TDM;
172 break;
173 case SND_SOC_DAIFMT_DSP_A:
174 adc_reg1 &= ~AD193X_ADC_SERFMT_MASK;
175 adc_reg1 |= AD193X_ADC_SERFMT_AUX;
176 break;
177 default:
178 return -EINVAL;
181 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
182 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
183 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
184 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
185 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 */
189 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
190 adc_reg2 &= ~AD193X_ADC_BCLK_INV;
191 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 */
195 adc_reg2 &= ~AD193X_ADC_LEFT_HIGH;
196 adc_reg2 |= AD193X_ADC_BCLK_INV;
197 dac_reg &= ~AD193X_DAC_LEFT_HIGH;
198 dac_reg |= AD193X_DAC_BCLK_INV;
199 break;
201 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
202 adc_reg2 |= AD193X_ADC_LEFT_HIGH;
203 adc_reg2 |= AD193X_ADC_BCLK_INV;
204 dac_reg |= AD193X_DAC_LEFT_HIGH;
205 dac_reg |= AD193X_DAC_BCLK_INV;
206 break;
207 default:
208 return -EINVAL;
211 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
212 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
213 adc_reg2 |= AD193X_ADC_LCR_MASTER;
214 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
215 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 */
219 adc_reg2 |= AD193X_ADC_LCR_MASTER;
220 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
221 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 */
225 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
226 adc_reg2 |= AD193X_ADC_BCLK_MASTER;
227 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 */
231 adc_reg2 &= ~AD193X_ADC_LCR_MASTER;
232 adc_reg2 &= ~AD193X_ADC_BCLK_MASTER;
233 dac_reg &= ~AD193X_DAC_LCR_MASTER;
234 dac_reg &= ~AD193X_DAC_BCLK_MASTER;
235 break;
236 default:
237 return -EINVAL;
240 snd_soc_write(codec, AD193X_ADC_CTRL1, adc_reg1);
241 snd_soc_write(codec, AD193X_ADC_CTRL2, adc_reg2);
242 snd_soc_write(codec, AD193X_DAC_CTRL1, dac_reg);
244 return 0;
247 static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
248 int clk_id, unsigned int freq, int dir)
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;
260 return -EINVAL;
263 static int ad193x_hw_params(struct snd_pcm_substream *substream,
264 struct snd_pcm_hw_params *params,
265 struct snd_soc_dai *dai)
267 int word_len = 0, master_rate = 0;
269 struct snd_soc_pcm_runtime *rtd = substream->private_data;
270 struct snd_soc_codec *codec = rtd->codec;
271 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
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;
287 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;
302 snd_soc_update_bits(codec, AD193X_PLL_CLK_CTRL0,
303 AD193X_PLL_INPUT_MASK, master_rate);
305 snd_soc_update_bits(codec, AD193X_DAC_CTRL2,
306 AD193X_DAC_WORD_LEN_MASK,
307 word_len << AD193X_DAC_WORD_LEN_SHFT);
309 snd_soc_update_bits(codec, AD193X_ADC_CTRL1,
310 AD193X_ADC_WORD_LEN_MASK, word_len);
312 return 0;
315 static 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,
323 /* codec DAI instance */
324 static 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,
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,
342 .ops = &ad193x_dai_ops,
345 static int ad193x_probe(struct snd_soc_codec *codec)
347 struct ad193x_priv *ad193x = snd_soc_codec_get_drvdata(codec);
348 struct snd_soc_dapm_context *dapm = &codec->dapm;
349 int ret;
351 codec->control_data = ad193x->regmap;
352 ret = snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
353 if (ret < 0) {
354 dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
355 return ret;
358 /* default setting for ad193x */
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);
374 snd_soc_add_controls(codec, ad193x_snd_controls,
375 ARRAY_SIZE(ad193x_snd_controls));
376 snd_soc_dapm_new_controls(dapm, ad193x_dapm_widgets,
377 ARRAY_SIZE(ad193x_dapm_widgets));
378 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
380 return ret;
383 static struct snd_soc_codec_driver soc_codec_dev_ad193x = {
384 .probe = ad193x_probe,
387 #if defined(CONFIG_SPI_MASTER)
389 static 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,
396 static int __devinit ad193x_spi_probe(struct spi_device *spi)
398 struct ad193x_priv *ad193x;
399 int ret;
401 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
402 if (ad193x == NULL)
403 return -ENOMEM;
405 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;
411 spi_set_drvdata(spi, ad193x);
413 ret = snd_soc_register_codec(&spi->dev,
414 &soc_codec_dev_ad193x, &ad193x_dai, 1);
415 if (ret < 0)
416 goto err_regmap_exit;
418 return 0;
420 err_regmap_exit:
421 regmap_exit(ad193x->regmap);
422 err_free:
423 kfree(ad193x);
425 return ret;
428 static int __devexit ad193x_spi_remove(struct spi_device *spi)
430 struct ad193x_priv *ad193x = spi_get_drvdata(spi);
432 snd_soc_unregister_codec(&spi->dev);
433 regmap_exit(ad193x->regmap);
434 kfree(ad193x);
435 return 0;
438 static struct spi_driver ad193x_spi_driver = {
439 .driver = {
440 .name = "ad193x",
441 .owner = THIS_MODULE,
443 .probe = ad193x_spi_probe,
444 .remove = __devexit_p(ad193x_spi_remove),
446 #endif
448 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
450 static const struct regmap_config ad193x_i2c_regmap_config = {
451 .val_bits = 8,
452 .reg_bits = 8,
455 static const struct i2c_device_id ad193x_id[] = {
456 { "ad1936", 0 },
457 { "ad1937", 0 },
460 MODULE_DEVICE_TABLE(i2c, ad193x_id);
462 static int __devinit ad193x_i2c_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
465 struct ad193x_priv *ad193x;
466 int ret;
468 ad193x = kzalloc(sizeof(struct ad193x_priv), GFP_KERNEL);
469 if (ad193x == NULL)
470 return -ENOMEM;
472 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;
478 i2c_set_clientdata(client, ad193x);
480 ret = snd_soc_register_codec(&client->dev,
481 &soc_codec_dev_ad193x, &ad193x_dai, 1);
482 if (ret < 0)
483 goto err_regmap_exit;
485 return 0;
487 err_regmap_exit:
488 regmap_exit(ad193x->regmap);
489 err_free:
490 kfree(ad193x);
491 return ret;
494 static int __devexit ad193x_i2c_remove(struct i2c_client *client)
496 struct ad193x_priv *ad193x = i2c_get_clientdata(client);
498 snd_soc_unregister_codec(&client->dev);
499 regmap_exit(ad193x->regmap);
500 kfree(ad193x);
501 return 0;
504 static struct i2c_driver ad193x_i2c_driver = {
505 .driver = {
506 .name = "ad193x",
508 .probe = ad193x_i2c_probe,
509 .remove = __devexit_p(ad193x_i2c_remove),
510 .id_table = ad193x_id,
512 #endif
514 static int __init ad193x_modinit(void)
516 int ret;
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);
524 #endif
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);
532 #endif
533 return ret;
535 module_init(ad193x_modinit);
537 static void __exit ad193x_modexit(void)
539 #if defined(CONFIG_SPI_MASTER)
540 spi_unregister_driver(&ad193x_spi_driver);
541 #endif
543 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
544 i2c_del_driver(&ad193x_i2c_driver);
545 #endif
547 module_exit(ad193x_modexit);
549 MODULE_DESCRIPTION("ASoC ad193x driver");
550 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
551 MODULE_LICENSE("GPL");