aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/tlv320aic23.c
diff options
context:
space:
mode:
authorTroy Kisky <troy.kisky@boundarydevices.com>2008-11-05 13:53:28 -0500
committerMark Brown <broonie@opensource.wolfsonmicro.com>2008-11-07 08:27:47 -0500
commit26df91c36fb976af9d08c20028b5cb1317eedcb3 (patch)
treebe668d98080f9a7f239d7b8c5e405a771dee36d3 /sound/soc/codecs/tlv320aic23.c
parente18c94d20224f3df584531a48d944d8cccfda46d (diff)
ASoC: TLV320AIC23B Support more sample rates
Add support for more sample rates, different crystals and split playback/capture rates. Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com> Acked-by: Arun KS <arunks@mistralsolutions.com> Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Diffstat (limited to 'sound/soc/codecs/tlv320aic23.c')
-rw-r--r--sound/soc/codecs/tlv320aic23.c222
1 files changed, 177 insertions, 45 deletions
diff --git a/sound/soc/codecs/tlv320aic23.c b/sound/soc/codecs/tlv320aic23.c
index 44308dac9e18..a95b538b8fe7 100644
--- a/sound/soc/codecs/tlv320aic23.c
+++ b/sound/soc/codecs/tlv320aic23.c
@@ -37,12 +37,6 @@
37 37
38#define AIC23_VERSION "0.1" 38#define AIC23_VERSION "0.1"
39 39
40struct tlv320aic23_srate_reg_info {
41 u32 sample_rate;
42 u8 control; /* SR3, SR2, SR1, SR0 and BOSR */
43 u8 divider; /* if 0 CLKIN = MCLK, if 1 CLKIN = MCLK/2 */
44};
45
46/* 40/*
47 * AIC23 register cache 41 * AIC23 register cache
48 */ 42 */
@@ -261,20 +255,151 @@ static const struct snd_soc_dapm_route intercon[] = {
261 255
262}; 256};
263 257
264/* tlv320aic23 related */ 258/* AIC23 driver data */
265static const struct tlv320aic23_srate_reg_info srate_reg_info[] = { 259struct aic23 {
266 {4000, 0x06, 1}, /* 4000 */ 260 struct snd_soc_codec codec;
267 {8000, 0x06, 0}, /* 8000 */ 261 int mclk;
268 {16000, 0x0C, 1}, /* 16000 */ 262 int requested_adc;
269 {22050, 0x11, 1}, /* 22050 */ 263 int requested_dac;
270 {24000, 0x00, 1}, /* 24000 */ 264};
271 {32000, 0x0C, 0}, /* 32000 */ 265
272 {44100, 0x11, 0}, /* 44100 */ 266/*
273 {48000, 0x00, 0}, /* 48000 */ 267 * Common Crystals used
274 {88200, 0x1F, 0}, /* 88200 */ 268 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
275 {96000, 0x0E, 0}, /* 96000 */ 269 * 12.0000 Mhz /125 = *96k /136 = 88.235K
270 * 12.2880 Mhz /128 = *96k /192 = 64k
271 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
272 * 18.4320 Mhz /128 = 144k /192 = *96k
273 */
274
275/*
276 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
277 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
278 */
279static const int bosr_usb_divisor_table[] = {
280 128, 125, 192, 136
281};
282#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
283#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
284static const unsigned short sr_valid_mask[] = {
285 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
286 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
287 LOWER_GROUP, /* Usb, bosr - 0*/
288 UPPER_GROUP, /* Usb, bosr - 1*/
289};
290/*
291 * Every divisor is a factor of 11*12
292 */
293#define SR_MULT (11*12)
294#define A(x) (x) ? (SR_MULT/x) : 0
295static const unsigned char sr_adc_mult_table[] = {
296 A(2), A(2), A(12), A(12), A(0), A(0), A(3), A(1),
297 A(2), A(2), A(11), A(11), A(0), A(0), A(0), A(1)
298};
299static const unsigned char sr_dac_mult_table[] = {
300 A(2), A(12), A(2), A(12), A(0), A(0), A(3), A(1),
301 A(2), A(11), A(2), A(11), A(0), A(0), A(0), A(1)
276}; 302};
277 303
304static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
305 int dac, int dac_l, int dac_h, int need_dac)
306{
307 if ((adc >= adc_l) && (adc <= adc_h) &&
308 (dac >= dac_l) && (dac <= dac_h)) {
309 int diff_adc = need_adc - adc;
310 int diff_dac = need_dac - dac;
311 return abs(diff_adc) + abs(diff_dac);
312 }
313 return UINT_MAX;
314}
315
316static int find_rate(int mclk, u32 need_adc, u32 need_dac)
317{
318 int i, j;
319 int best_i = -1;
320 int best_j = -1;
321 int best_div = 0;
322 unsigned best_score = UINT_MAX;
323 int adc_l, adc_h, dac_l, dac_h;
324
325 need_adc *= SR_MULT;
326 need_dac *= SR_MULT;
327 /*
328 * rates given are +/- 1/32
329 */
330 adc_l = need_adc - (need_adc >> 5);
331 adc_h = need_adc + (need_adc >> 5);
332 dac_l = need_dac - (need_dac >> 5);
333 dac_h = need_dac + (need_dac >> 5);
334 for (i = 0; i < 4; i++) {
335 int base = mclk / bosr_usb_divisor_table[i];
336 int mask = sr_valid_mask[i];
337 for (j = 0; j < 16; j++, mask >>= 1) {
338 int adc;
339 int dac;
340 int score;
341 if ((mask & 1) == 0)
342 continue;
343 adc = base * sr_adc_mult_table[j];
344 dac = base * sr_dac_mult_table[j];
345 score = get_score(adc, adc_l, adc_h, need_adc,
346 dac, dac_l, dac_h, need_dac);
347 if (best_score > score) {
348 best_score = score;
349 best_i = i;
350 best_j = j;
351 best_div = 0;
352 }
353 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
354 (dac >> 1), dac_l, dac_h, need_dac);
355 /* prefer to have a /2 */
356 if ((score != UINT_MAX) && (best_score >= score)) {
357 best_score = score;
358 best_i = i;
359 best_j = j;
360 best_div = 1;
361 }
362 }
363 }
364 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
365}
366
367static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
368 u32 *sample_rate_adc, u32 *sample_rate_dac)
369{
370 int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE);
371 int sr = (src >> 2) & 0x0f;
372 int val = (mclk / bosr_usb_divisor_table[src & 3]);
373 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
374 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
375 if (src & TLV320AIC23_CLKIN_HALF) {
376 adc >>= 1;
377 dac >>= 1;
378 }
379 *sample_rate_adc = adc;
380 *sample_rate_dac = dac;
381}
382
383static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
384 u32 sample_rate_adc, u32 sample_rate_dac)
385{
386 /* Search for the right sample rate */
387 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
388 if (data < 0) {
389 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
390 __func__, sample_rate_adc, sample_rate_dac);
391 return -EINVAL;
392 }
393 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
394 if (1) {
395 int adc, dac;
396 get_current_sample_rates(codec, mclk, &adc, &dac);
397 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
398 adc, dac, data);
399 }
400 return 0;
401}
402
278static int tlv320aic23_add_widgets(struct snd_soc_codec *codec) 403static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
279{ 404{
280 snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets, 405 snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets,
@@ -293,27 +418,30 @@ static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
293 struct snd_soc_pcm_runtime *rtd = substream->private_data; 418 struct snd_soc_pcm_runtime *rtd = substream->private_data;
294 struct snd_soc_device *socdev = rtd->socdev; 419 struct snd_soc_device *socdev = rtd->socdev;
295 struct snd_soc_codec *codec = socdev->codec; 420 struct snd_soc_codec *codec = socdev->codec;
296 u16 iface_reg, data; 421 u16 iface_reg;
297 u8 count = 0; 422 int ret;
423 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
424 u32 sample_rate_adc = aic23->requested_adc;
425 u32 sample_rate_dac = aic23->requested_dac;
426 u32 sample_rate = params_rate(params);
427
428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
429 aic23->requested_dac = sample_rate_dac = sample_rate;
430 if (!sample_rate_adc)
431 sample_rate_adc = sample_rate;
432 } else {
433 aic23->requested_adc = sample_rate_adc = sample_rate;
434 if (!sample_rate_dac)
435 sample_rate_dac = sample_rate;
436 }
437 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
438 sample_rate_dac);
439 if (ret < 0)
440 return ret;
298 441
299 iface_reg = 442 iface_reg =
300 tlv320aic23_read_reg_cache(codec, 443 tlv320aic23_read_reg_cache(codec,
301 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2); 444 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
302
303 /* Search for the right sample rate */
304 /* Verify what happens if the rate is not supported
305 * now it goes to 96Khz */
306 while ((srate_reg_info[count].sample_rate != params_rate(params)) &&
307 (count < ARRAY_SIZE(srate_reg_info))) {
308 count++;
309 }
310
311 data = (srate_reg_info[count].divider << TLV320AIC23_CLKIN_SHIFT) |
312 (srate_reg_info[count]. control << TLV320AIC23_BOSR_SHIFT) |
313 TLV320AIC23_USB_CLK_ON;
314
315 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
316
317 switch (params_format(params)) { 445 switch (params_format(params)) {
318 case SNDRV_PCM_FORMAT_S16_LE: 446 case SNDRV_PCM_FORMAT_S16_LE:
319 break; 447 break;
@@ -349,12 +477,17 @@ static void tlv320aic23_shutdown(struct snd_pcm_substream *substream)
349 struct snd_soc_pcm_runtime *rtd = substream->private_data; 477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
350 struct snd_soc_device *socdev = rtd->socdev; 478 struct snd_soc_device *socdev = rtd->socdev;
351 struct snd_soc_codec *codec = socdev->codec; 479 struct snd_soc_codec *codec = socdev->codec;
480 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
352 481
353 /* deactivate */ 482 /* deactivate */
354 if (!codec->active) { 483 if (!codec->active) {
355 udelay(50); 484 udelay(50);
356 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0); 485 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
357 } 486 }
487 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
488 aic23->requested_dac = 0;
489 else
490 aic23->requested_adc = 0;
358} 491}
359 492
360static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute) 493static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
@@ -422,12 +555,9 @@ static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
422 int clk_id, unsigned int freq, int dir) 555 int clk_id, unsigned int freq, int dir)
423{ 556{
424 struct snd_soc_codec *codec = codec_dai->codec; 557 struct snd_soc_codec *codec = codec_dai->codec;
425 558 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
426 switch (freq) { 559 aic23->mclk = freq;
427 case 12000000: 560 return 0;
428 return 0;
429 }
430 return -EINVAL;
431} 561}
432 562
433static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec, 563static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
@@ -659,14 +789,15 @@ static int tlv320aic23_probe(struct platform_device *pdev)
659{ 789{
660 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 790 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
661 struct snd_soc_codec *codec; 791 struct snd_soc_codec *codec;
792 struct aic23 *aic23;
662 int ret = 0; 793 int ret = 0;
663 794
664 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION); 795 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
665 796
666 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 797 aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
667 if (codec == NULL) 798 if (aic23 == NULL)
668 return -ENOMEM; 799 return -ENOMEM;
669 800 codec = &aic23->codec;
670 socdev->codec = codec; 801 socdev->codec = codec;
671 mutex_init(&codec->mutex); 802 mutex_init(&codec->mutex);
672 INIT_LIST_HEAD(&codec->dapm_widgets); 803 INIT_LIST_HEAD(&codec->dapm_widgets);
@@ -687,6 +818,7 @@ static int tlv320aic23_remove(struct platform_device *pdev)
687{ 818{
688 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 819 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
689 struct snd_soc_codec *codec = socdev->codec; 820 struct snd_soc_codec *codec = socdev->codec;
821 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
690 822
691 if (codec->control_data) 823 if (codec->control_data)
692 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); 824 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
@@ -697,7 +829,7 @@ static int tlv320aic23_remove(struct platform_device *pdev)
697 i2c_del_driver(&tlv320aic23_i2c_driver); 829 i2c_del_driver(&tlv320aic23_i2c_driver);
698#endif 830#endif
699 kfree(codec->reg_cache); 831 kfree(codec->reg_cache);
700 kfree(codec); 832 kfree(aic23);
701 833
702 return 0; 834 return 0;
703} 835}