diff options
Diffstat (limited to 'sound/soc/codecs/tlv320dac33.c')
-rw-r--r-- | sound/soc/codecs/tlv320dac33.c | 180 |
1 files changed, 127 insertions, 53 deletions
diff --git a/sound/soc/codecs/tlv320dac33.c b/sound/soc/codecs/tlv320dac33.c index 65adc77eada1..8651b01ed223 100644 --- a/sound/soc/codecs/tlv320dac33.c +++ b/sound/soc/codecs/tlv320dac33.c | |||
@@ -49,8 +49,6 @@ | |||
49 | 49 | ||
50 | #define NSAMPLE_MAX 5700 | 50 | #define NSAMPLE_MAX 5700 |
51 | 51 | ||
52 | #define LATENCY_TIME_MS 20 | ||
53 | |||
54 | #define MODE7_LTHR 10 | 52 | #define MODE7_LTHR 10 |
55 | #define MODE7_UTHR (DAC33_BUFFER_SIZE_SAMPLES - 10) | 53 | #define MODE7_UTHR (DAC33_BUFFER_SIZE_SAMPLES - 10) |
56 | 54 | ||
@@ -62,6 +60,9 @@ | |||
62 | #define US_TO_SAMPLES(rate, us) \ | 60 | #define US_TO_SAMPLES(rate, us) \ |
63 | (rate / (1000000 / us)) | 61 | (rate / (1000000 / us)) |
64 | 62 | ||
63 | #define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \ | ||
64 | ((samples * 5000) / ((burstrate * 5000) / (burstrate - playrate))) | ||
65 | |||
65 | static void dac33_calculate_times(struct snd_pcm_substream *substream); | 66 | static void dac33_calculate_times(struct snd_pcm_substream *substream); |
66 | static int dac33_prepare_chip(struct snd_pcm_substream *substream); | 67 | static int dac33_prepare_chip(struct snd_pcm_substream *substream); |
67 | 68 | ||
@@ -107,6 +108,10 @@ struct tlv320dac33_priv { | |||
107 | * this */ | 108 | * this */ |
108 | enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */ | 109 | enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */ |
109 | unsigned int nsample; /* burst read amount from host */ | 110 | unsigned int nsample; /* burst read amount from host */ |
111 | int mode1_latency; /* latency caused by the i2c writes in | ||
112 | * us */ | ||
113 | int auto_fifo_config; /* Configure the FIFO based on the | ||
114 | * period size */ | ||
110 | u8 burst_bclkdiv; /* BCLK divider value in burst mode */ | 115 | u8 burst_bclkdiv; /* BCLK divider value in burst mode */ |
111 | unsigned int burst_rate; /* Interface speed in Burst modes */ | 116 | unsigned int burst_rate; /* Interface speed in Burst modes */ |
112 | 117 | ||
@@ -120,6 +125,8 @@ struct tlv320dac33_priv { | |||
120 | * samples */ | 125 | * samples */ |
121 | unsigned int mode7_us_to_lthr; /* Time to reach lthr from uthr */ | 126 | unsigned int mode7_us_to_lthr; /* Time to reach lthr from uthr */ |
122 | 127 | ||
128 | unsigned int uthr; | ||
129 | |||
123 | enum dac33_state state; | 130 | enum dac33_state state; |
124 | }; | 131 | }; |
125 | 132 | ||
@@ -442,6 +449,39 @@ static int dac33_set_nsample(struct snd_kcontrol *kcontrol, | |||
442 | return ret; | 449 | return ret; |
443 | } | 450 | } |
444 | 451 | ||
452 | static int dac33_get_uthr(struct snd_kcontrol *kcontrol, | ||
453 | struct snd_ctl_elem_value *ucontrol) | ||
454 | { | ||
455 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | ||
456 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); | ||
457 | |||
458 | ucontrol->value.integer.value[0] = dac33->uthr; | ||
459 | |||
460 | return 0; | ||
461 | } | ||
462 | |||
463 | static int dac33_set_uthr(struct snd_kcontrol *kcontrol, | ||
464 | struct snd_ctl_elem_value *ucontrol) | ||
465 | { | ||
466 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | ||
467 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); | ||
468 | int ret = 0; | ||
469 | |||
470 | if (dac33->substream) | ||
471 | return -EBUSY; | ||
472 | |||
473 | if (dac33->uthr == ucontrol->value.integer.value[0]) | ||
474 | return 0; | ||
475 | |||
476 | if (ucontrol->value.integer.value[0] < (MODE7_LTHR + 10) || | ||
477 | ucontrol->value.integer.value[0] > MODE7_UTHR) | ||
478 | ret = -EINVAL; | ||
479 | else | ||
480 | dac33->uthr = ucontrol->value.integer.value[0]; | ||
481 | |||
482 | return ret; | ||
483 | } | ||
484 | |||
445 | static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol, | 485 | static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol, |
446 | struct snd_ctl_elem_value *ucontrol) | 486 | struct snd_ctl_elem_value *ucontrol) |
447 | { | 487 | { |
@@ -503,13 +543,18 @@ static const struct snd_kcontrol_new dac33_snd_controls[] = { | |||
503 | DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1), | 543 | DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1), |
504 | }; | 544 | }; |
505 | 545 | ||
506 | static const struct snd_kcontrol_new dac33_nsample_snd_controls[] = { | 546 | static const struct snd_kcontrol_new dac33_mode_snd_controls[] = { |
507 | SOC_SINGLE_EXT("nSample", 0, 0, 5900, 0, | ||
508 | dac33_get_nsample, dac33_set_nsample), | ||
509 | SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum, | 547 | SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum, |
510 | dac33_get_fifo_mode, dac33_set_fifo_mode), | 548 | dac33_get_fifo_mode, dac33_set_fifo_mode), |
511 | }; | 549 | }; |
512 | 550 | ||
551 | static const struct snd_kcontrol_new dac33_fifo_snd_controls[] = { | ||
552 | SOC_SINGLE_EXT("nSample", 0, 0, 5900, 0, | ||
553 | dac33_get_nsample, dac33_set_nsample), | ||
554 | SOC_SINGLE_EXT("UTHR", 0, 0, MODE7_UTHR, 0, | ||
555 | dac33_get_uthr, dac33_set_uthr), | ||
556 | }; | ||
557 | |||
513 | /* Analog bypass */ | 558 | /* Analog bypass */ |
514 | static const struct snd_kcontrol_new dac33_dapm_abypassl_control = | 559 | static const struct snd_kcontrol_new dac33_dapm_abypassl_control = |
515 | SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1); | 560 | SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1); |
@@ -612,7 +657,7 @@ static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33) | |||
612 | switch (dac33->fifo_mode) { | 657 | switch (dac33->fifo_mode) { |
613 | case DAC33_FIFO_MODE1: | 658 | case DAC33_FIFO_MODE1: |
614 | dac33_write16(codec, DAC33_NSAMPLE_MSB, | 659 | dac33_write16(codec, DAC33_NSAMPLE_MSB, |
615 | DAC33_THRREG(dac33->nsample + dac33->alarm_threshold)); | 660 | DAC33_THRREG(dac33->nsample)); |
616 | 661 | ||
617 | /* Take the timestamps */ | 662 | /* Take the timestamps */ |
618 | spin_lock_irq(&dac33->lock); | 663 | spin_lock_irq(&dac33->lock); |
@@ -761,6 +806,10 @@ static void dac33_shutdown(struct snd_pcm_substream *substream, | |||
761 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); | 806 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
762 | 807 | ||
763 | dac33->substream = NULL; | 808 | dac33->substream = NULL; |
809 | |||
810 | /* Reset the nSample restrictions */ | ||
811 | dac33->nsample_min = 0; | ||
812 | dac33->nsample_max = NSAMPLE_MAX; | ||
764 | } | 813 | } |
765 | 814 | ||
766 | static int dac33_hw_params(struct snd_pcm_substream *substream, | 815 | static int dac33_hw_params(struct snd_pcm_substream *substream, |
@@ -985,7 +1034,7 @@ static int dac33_prepare_chip(struct snd_pcm_substream *substream) | |||
985 | * Configure the threshold levels, and leave 10 sample space | 1034 | * Configure the threshold levels, and leave 10 sample space |
986 | * at the bottom, and also at the top of the FIFO | 1035 | * at the bottom, and also at the top of the FIFO |
987 | */ | 1036 | */ |
988 | dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(MODE7_UTHR)); | 1037 | dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(dac33->uthr)); |
989 | dac33_write16(codec, DAC33_LTHR_MSB, DAC33_THRREG(MODE7_LTHR)); | 1038 | dac33_write16(codec, DAC33_LTHR_MSB, DAC33_THRREG(MODE7_LTHR)); |
990 | break; | 1039 | break; |
991 | default: | 1040 | default: |
@@ -1003,57 +1052,71 @@ static void dac33_calculate_times(struct snd_pcm_substream *substream) | |||
1003 | struct snd_soc_device *socdev = rtd->socdev; | 1052 | struct snd_soc_device *socdev = rtd->socdev; |
1004 | struct snd_soc_codec *codec = socdev->card->codec; | 1053 | struct snd_soc_codec *codec = socdev->card->codec; |
1005 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); | 1054 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
1055 | unsigned int period_size = substream->runtime->period_size; | ||
1056 | unsigned int rate = substream->runtime->rate; | ||
1006 | unsigned int nsample_limit; | 1057 | unsigned int nsample_limit; |
1007 | 1058 | ||
1008 | /* In bypass mode we don't need to calculate */ | 1059 | /* In bypass mode we don't need to calculate */ |
1009 | if (!dac33->fifo_mode) | 1060 | if (!dac33->fifo_mode) |
1010 | return; | 1061 | return; |
1011 | 1062 | ||
1012 | /* Number of samples (16bit, stereo) in one period */ | ||
1013 | dac33->nsample_min = snd_pcm_lib_period_bytes(substream) / 4; | ||
1014 | |||
1015 | /* Number of samples (16bit, stereo) in ALSA buffer */ | ||
1016 | dac33->nsample_max = snd_pcm_lib_buffer_bytes(substream) / 4; | ||
1017 | /* Subtract one period from the total */ | ||
1018 | dac33->nsample_max -= dac33->nsample_min; | ||
1019 | |||
1020 | /* Number of samples for LATENCY_TIME_MS / 2 */ | ||
1021 | dac33->alarm_threshold = substream->runtime->rate / | ||
1022 | (1000 / (LATENCY_TIME_MS / 2)); | ||
1023 | |||
1024 | /* Find and fix up the lowest nsmaple limit */ | ||
1025 | nsample_limit = substream->runtime->rate / (1000 / LATENCY_TIME_MS); | ||
1026 | |||
1027 | if (dac33->nsample_min < nsample_limit) | ||
1028 | dac33->nsample_min = nsample_limit; | ||
1029 | |||
1030 | if (dac33->nsample < dac33->nsample_min) | ||
1031 | dac33->nsample = dac33->nsample_min; | ||
1032 | |||
1033 | /* | ||
1034 | * Find and fix up the highest nsmaple limit | ||
1035 | * In order to not overflow the DAC33 buffer substract the | ||
1036 | * alarm_threshold value from the size of the DAC33 buffer | ||
1037 | */ | ||
1038 | nsample_limit = DAC33_BUFFER_SIZE_SAMPLES - dac33->alarm_threshold; | ||
1039 | |||
1040 | if (dac33->nsample_max > nsample_limit) | ||
1041 | dac33->nsample_max = nsample_limit; | ||
1042 | |||
1043 | if (dac33->nsample > dac33->nsample_max) | ||
1044 | dac33->nsample = dac33->nsample_max; | ||
1045 | |||
1046 | switch (dac33->fifo_mode) { | 1063 | switch (dac33->fifo_mode) { |
1047 | case DAC33_FIFO_MODE1: | 1064 | case DAC33_FIFO_MODE1: |
1065 | /* Number of samples under i2c latency */ | ||
1066 | dac33->alarm_threshold = US_TO_SAMPLES(rate, | ||
1067 | dac33->mode1_latency); | ||
1068 | if (dac33->auto_fifo_config) { | ||
1069 | if (period_size <= dac33->alarm_threshold) | ||
1070 | /* | ||
1071 | * Configure nSamaple to number of periods, | ||
1072 | * which covers the latency requironment. | ||
1073 | */ | ||
1074 | dac33->nsample = period_size * | ||
1075 | ((dac33->alarm_threshold / period_size) + | ||
1076 | (dac33->alarm_threshold % period_size ? | ||
1077 | 1 : 0)); | ||
1078 | else | ||
1079 | dac33->nsample = period_size; | ||
1080 | } else { | ||
1081 | /* nSample time shall not be shorter than i2c latency */ | ||
1082 | dac33->nsample_min = dac33->alarm_threshold; | ||
1083 | /* | ||
1084 | * nSample should not be bigger than alsa buffer minus | ||
1085 | * size of one period to avoid overruns | ||
1086 | */ | ||
1087 | dac33->nsample_max = substream->runtime->buffer_size - | ||
1088 | period_size; | ||
1089 | nsample_limit = DAC33_BUFFER_SIZE_SAMPLES - | ||
1090 | dac33->alarm_threshold; | ||
1091 | if (dac33->nsample_max > nsample_limit) | ||
1092 | dac33->nsample_max = nsample_limit; | ||
1093 | |||
1094 | /* Correct the nSample if it is outside of the ranges */ | ||
1095 | if (dac33->nsample < dac33->nsample_min) | ||
1096 | dac33->nsample = dac33->nsample_min; | ||
1097 | if (dac33->nsample > dac33->nsample_max) | ||
1098 | dac33->nsample = dac33->nsample_max; | ||
1099 | } | ||
1100 | |||
1048 | dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate, | 1101 | dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate, |
1049 | dac33->nsample); | 1102 | dac33->nsample); |
1050 | dac33->t_stamp1 = 0; | 1103 | dac33->t_stamp1 = 0; |
1051 | dac33->t_stamp2 = 0; | 1104 | dac33->t_stamp2 = 0; |
1052 | break; | 1105 | break; |
1053 | case DAC33_FIFO_MODE7: | 1106 | case DAC33_FIFO_MODE7: |
1107 | if (dac33->auto_fifo_config) { | ||
1108 | dac33->uthr = UTHR_FROM_PERIOD_SIZE( | ||
1109 | period_size, | ||
1110 | rate, | ||
1111 | dac33->burst_rate) + 9; | ||
1112 | if (dac33->uthr > MODE7_UTHR) | ||
1113 | dac33->uthr = MODE7_UTHR; | ||
1114 | if (dac33->uthr < (MODE7_LTHR + 10)) | ||
1115 | dac33->uthr = (MODE7_LTHR + 10); | ||
1116 | } | ||
1054 | dac33->mode7_us_to_lthr = | 1117 | dac33->mode7_us_to_lthr = |
1055 | SAMPLES_TO_US(substream->runtime->rate, | 1118 | SAMPLES_TO_US(substream->runtime->rate, |
1056 | MODE7_UTHR - MODE7_LTHR + 1); | 1119 | dac33->uthr - MODE7_LTHR + 1); |
1057 | dac33->t_stamp1 = 0; | 1120 | dac33->t_stamp1 = 0; |
1058 | break; | 1121 | break; |
1059 | default: | 1122 | default: |
@@ -1104,7 +1167,7 @@ static snd_pcm_sframes_t dac33_dai_delay( | |||
1104 | struct snd_soc_codec *codec = socdev->card->codec; | 1167 | struct snd_soc_codec *codec = socdev->card->codec; |
1105 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); | 1168 | struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec); |
1106 | unsigned long long t0, t1, t_now; | 1169 | unsigned long long t0, t1, t_now; |
1107 | unsigned int time_delta; | 1170 | unsigned int time_delta, uthr; |
1108 | int samples_out, samples_in, samples; | 1171 | int samples_out, samples_in, samples; |
1109 | snd_pcm_sframes_t delay = 0; | 1172 | snd_pcm_sframes_t delay = 0; |
1110 | 1173 | ||
@@ -1182,6 +1245,7 @@ static snd_pcm_sframes_t dac33_dai_delay( | |||
1182 | case DAC33_FIFO_MODE7: | 1245 | case DAC33_FIFO_MODE7: |
1183 | spin_lock(&dac33->lock); | 1246 | spin_lock(&dac33->lock); |
1184 | t0 = dac33->t_stamp1; | 1247 | t0 = dac33->t_stamp1; |
1248 | uthr = dac33->uthr; | ||
1185 | spin_unlock(&dac33->lock); | 1249 | spin_unlock(&dac33->lock); |
1186 | t_now = ktime_to_us(ktime_get()); | 1250 | t_now = ktime_to_us(ktime_get()); |
1187 | 1251 | ||
@@ -1194,7 +1258,7 @@ static snd_pcm_sframes_t dac33_dai_delay( | |||
1194 | * Either the timestamps are messed or equal. Report | 1258 | * Either the timestamps are messed or equal. Report |
1195 | * maximum delay | 1259 | * maximum delay |
1196 | */ | 1260 | */ |
1197 | delay = MODE7_UTHR; | 1261 | delay = uthr; |
1198 | goto out; | 1262 | goto out; |
1199 | } | 1263 | } |
1200 | 1264 | ||
@@ -1208,8 +1272,8 @@ static snd_pcm_sframes_t dac33_dai_delay( | |||
1208 | substream->runtime->rate, | 1272 | substream->runtime->rate, |
1209 | time_delta); | 1273 | time_delta); |
1210 | 1274 | ||
1211 | if (likely(MODE7_UTHR > samples_out)) | 1275 | if (likely(uthr > samples_out)) |
1212 | delay = MODE7_UTHR - samples_out; | 1276 | delay = uthr - samples_out; |
1213 | else | 1277 | else |
1214 | delay = 0; | 1278 | delay = 0; |
1215 | } else { | 1279 | } else { |
@@ -1227,8 +1291,8 @@ static snd_pcm_sframes_t dac33_dai_delay( | |||
1227 | time_delta); | 1291 | time_delta); |
1228 | delay = MODE7_LTHR + samples_in - samples_out; | 1292 | delay = MODE7_LTHR + samples_in - samples_out; |
1229 | 1293 | ||
1230 | if (unlikely(delay > MODE7_UTHR)) | 1294 | if (unlikely(delay > uthr)) |
1231 | delay = MODE7_UTHR; | 1295 | delay = uthr; |
1232 | } | 1296 | } |
1233 | break; | 1297 | break; |
1234 | default: | 1298 | default: |
@@ -1347,10 +1411,15 @@ static int dac33_soc_probe(struct platform_device *pdev) | |||
1347 | 1411 | ||
1348 | snd_soc_add_controls(codec, dac33_snd_controls, | 1412 | snd_soc_add_controls(codec, dac33_snd_controls, |
1349 | ARRAY_SIZE(dac33_snd_controls)); | 1413 | ARRAY_SIZE(dac33_snd_controls)); |
1350 | /* Only add the nSample controls, if we have valid IRQ number */ | 1414 | /* Only add the FIFO controls, if we have valid IRQ number */ |
1351 | if (dac33->irq >= 0) | 1415 | if (dac33->irq >= 0) { |
1352 | snd_soc_add_controls(codec, dac33_nsample_snd_controls, | 1416 | snd_soc_add_controls(codec, dac33_mode_snd_controls, |
1353 | ARRAY_SIZE(dac33_nsample_snd_controls)); | 1417 | ARRAY_SIZE(dac33_mode_snd_controls)); |
1418 | /* FIFO usage controls only, if autoio config is not selected */ | ||
1419 | if (!dac33->auto_fifo_config) | ||
1420 | snd_soc_add_controls(codec, dac33_fifo_snd_controls, | ||
1421 | ARRAY_SIZE(dac33_fifo_snd_controls)); | ||
1422 | } | ||
1354 | 1423 | ||
1355 | dac33_add_widgets(codec); | 1424 | dac33_add_widgets(codec); |
1356 | 1425 | ||
@@ -1481,9 +1550,14 @@ static int __devinit dac33_i2c_probe(struct i2c_client *client, | |||
1481 | /* Pre calculate the burst rate */ | 1550 | /* Pre calculate the burst rate */ |
1482 | dac33->burst_rate = BURST_BASEFREQ_HZ / dac33->burst_bclkdiv / 32; | 1551 | dac33->burst_rate = BURST_BASEFREQ_HZ / dac33->burst_bclkdiv / 32; |
1483 | dac33->keep_bclk = pdata->keep_bclk; | 1552 | dac33->keep_bclk = pdata->keep_bclk; |
1553 | dac33->auto_fifo_config = pdata->auto_fifo_config; | ||
1554 | dac33->mode1_latency = pdata->mode1_latency; | ||
1555 | if (!dac33->mode1_latency) | ||
1556 | dac33->mode1_latency = 10000; /* 10ms */ | ||
1484 | dac33->irq = client->irq; | 1557 | dac33->irq = client->irq; |
1485 | dac33->nsample = NSAMPLE_MAX; | 1558 | dac33->nsample = NSAMPLE_MAX; |
1486 | dac33->nsample_max = NSAMPLE_MAX; | 1559 | dac33->nsample_max = NSAMPLE_MAX; |
1560 | dac33->uthr = MODE7_UTHR; | ||
1487 | /* Disable FIFO use by default */ | 1561 | /* Disable FIFO use by default */ |
1488 | dac33->fifo_mode = DAC33_FIFO_BYPASS; | 1562 | dac33->fifo_mode = DAC33_FIFO_BYPASS; |
1489 | 1563 | ||