cx88-dsp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. *
  3. * Stereo and SAP detection for cx88
  4. *
  5. * Copyright (c) 2009 Marton Balint <cus@fazekas.hu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/jiffies.h>
  25. #include <asm/div64.h>
  26. #include "cx88.h"
  27. #include "cx88-reg.h"
  28. #define INT_PI ((s32)(3.141592653589 * 32768.0))
  29. #define compat_remainder(a, b) \
  30. ((float)(((s32)((a)*100))%((s32)((b)*100)))/100.0)
  31. #define baseband_freq(carrier, srate, tone) ((s32)( \
  32. (compat_remainder(carrier + tone, srate)) / srate * 2 * INT_PI))
  33. /* We calculate the baseband frequencies of the carrier and the pilot tones
  34. * based on the the sampling rate of the audio rds fifo. */
  35. #define FREQ_A2_CARRIER baseband_freq(54687.5, 2689.36, 0.0)
  36. #define FREQ_A2_DUAL baseband_freq(54687.5, 2689.36, 274.1)
  37. #define FREQ_A2_STEREO baseband_freq(54687.5, 2689.36, 117.5)
  38. /* The frequencies below are from the reference driver. They probably need
  39. * further adjustments, because they are not tested at all. You may even need
  40. * to play a bit with the registers of the chip to select the proper signal
  41. * for the input of the audio rds fifo, and measure it's sampling rate to
  42. * calculate the proper baseband frequencies... */
  43. #define FREQ_A2M_CARRIER ((s32)(2.114516 * 32768.0))
  44. #define FREQ_A2M_DUAL ((s32)(2.754916 * 32768.0))
  45. #define FREQ_A2M_STEREO ((s32)(2.462326 * 32768.0))
  46. #define FREQ_EIAJ_CARRIER ((s32)(1.963495 * 32768.0)) /* 5pi/8 */
  47. #define FREQ_EIAJ_DUAL ((s32)(2.562118 * 32768.0))
  48. #define FREQ_EIAJ_STEREO ((s32)(2.601053 * 32768.0))
  49. #define FREQ_BTSC_DUAL ((s32)(1.963495 * 32768.0)) /* 5pi/8 */
  50. #define FREQ_BTSC_DUAL_REF ((s32)(1.374446 * 32768.0)) /* 7pi/16 */
  51. #define FREQ_BTSC_SAP ((s32)(2.471532 * 32768.0))
  52. #define FREQ_BTSC_SAP_REF ((s32)(1.730072 * 32768.0))
  53. /* The spectrum of the signal should be empty between these frequencies. */
  54. #define FREQ_NOISE_START ((s32)(0.100000 * 32768.0))
  55. #define FREQ_NOISE_END ((s32)(1.200000 * 32768.0))
  56. static unsigned int dsp_debug;
  57. module_param(dsp_debug, int, 0644);
  58. MODULE_PARM_DESC(dsp_debug, "enable audio dsp debug messages");
  59. #define dprintk(level, fmt, arg...) if (dsp_debug >= level) \
  60. printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
  61. static s32 int_cos(u32 x)
  62. {
  63. u32 t2, t4, t6, t8;
  64. s32 ret;
  65. u16 period = x / INT_PI;
  66. if (period % 2)
  67. return -int_cos(x - INT_PI);
  68. x = x % INT_PI;
  69. if (x > INT_PI/2)
  70. return -int_cos(INT_PI/2 - (x % (INT_PI/2)));
  71. /* Now x is between 0 and INT_PI/2.
  72. * To calculate cos(x) we use it's Taylor polinom. */
  73. t2 = x*x/32768/2;
  74. t4 = t2*x/32768*x/32768/3/4;
  75. t6 = t4*x/32768*x/32768/5/6;
  76. t8 = t6*x/32768*x/32768/7/8;
  77. ret = 32768-t2+t4-t6+t8;
  78. return ret;
  79. }
  80. static u32 int_goertzel(s16 x[], u32 N, u32 freq)
  81. {
  82. /* We use the Goertzel algorithm to determine the power of the
  83. * given frequency in the signal */
  84. s32 s_prev = 0;
  85. s32 s_prev2 = 0;
  86. s32 coeff = 2*int_cos(freq);
  87. u32 i;
  88. u64 tmp;
  89. u32 divisor;
  90. for (i = 0; i < N; i++) {
  91. s32 s = x[i] + ((s64)coeff*s_prev/32768) - s_prev2;
  92. s_prev2 = s_prev;
  93. s_prev = s;
  94. }
  95. tmp = (s64)s_prev2 * s_prev2 + (s64)s_prev * s_prev -
  96. (s64)coeff * s_prev2 * s_prev / 32768;
  97. /* XXX: N must be low enough so that N*N fits in s32.
  98. * Else we need two divisions. */
  99. divisor = N * N;
  100. do_div(tmp, divisor);
  101. return (u32) tmp;
  102. }
  103. static u32 freq_magnitude(s16 x[], u32 N, u32 freq)
  104. {
  105. u32 sum = int_goertzel(x, N, freq);
  106. return (u32)int_sqrt(sum);
  107. }
  108. static u32 noise_magnitude(s16 x[], u32 N, u32 freq_start, u32 freq_end)
  109. {
  110. int i;
  111. u32 sum = 0;
  112. u32 freq_step;
  113. int samples = 5;
  114. if (N > 192) {
  115. /* The last 192 samples are enough for noise detection */
  116. x += (N-192);
  117. N = 192;
  118. }
  119. freq_step = (freq_end - freq_start) / (samples - 1);
  120. for (i = 0; i < samples; i++) {
  121. sum += int_goertzel(x, N, freq_start);
  122. freq_start += freq_step;
  123. }
  124. return (u32)int_sqrt(sum / samples);
  125. }
  126. static s32 detect_a2_a2m_eiaj(struct cx88_core *core, s16 x[], u32 N)
  127. {
  128. s32 carrier, stereo, dual, noise;
  129. s32 carrier_freq, stereo_freq, dual_freq;
  130. s32 ret;
  131. switch (core->tvaudio) {
  132. case WW_BG:
  133. case WW_DK:
  134. carrier_freq = FREQ_A2_CARRIER;
  135. stereo_freq = FREQ_A2_STEREO;
  136. dual_freq = FREQ_A2_DUAL;
  137. break;
  138. case WW_M:
  139. carrier_freq = FREQ_A2M_CARRIER;
  140. stereo_freq = FREQ_A2M_STEREO;
  141. dual_freq = FREQ_A2M_DUAL;
  142. break;
  143. case WW_EIAJ:
  144. carrier_freq = FREQ_EIAJ_CARRIER;
  145. stereo_freq = FREQ_EIAJ_STEREO;
  146. dual_freq = FREQ_EIAJ_DUAL;
  147. break;
  148. default:
  149. printk(KERN_WARNING "%s/0: unsupported audio mode %d for %s\n",
  150. core->name, core->tvaudio, __func__);
  151. return UNSET;
  152. }
  153. carrier = freq_magnitude(x, N, carrier_freq);
  154. stereo = freq_magnitude(x, N, stereo_freq);
  155. dual = freq_magnitude(x, N, dual_freq);
  156. noise = noise_magnitude(x, N, FREQ_NOISE_START, FREQ_NOISE_END);
  157. dprintk(1, "detect a2/a2m/eiaj: carrier=%d, stereo=%d, dual=%d, "
  158. "noise=%d\n", carrier, stereo, dual, noise);
  159. if (stereo > dual)
  160. ret = V4L2_TUNER_SUB_STEREO;
  161. else
  162. ret = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  163. if (core->tvaudio == WW_EIAJ) {
  164. /* EIAJ checks may need adjustments */
  165. if ((carrier > max(stereo, dual)*2) &&
  166. (carrier < max(stereo, dual)*6) &&
  167. (carrier > 20 && carrier < 200) &&
  168. (max(stereo, dual) > min(stereo, dual))) {
  169. /* For EIAJ the carrier is always present,
  170. so we probably don't need noise detection */
  171. return ret;
  172. }
  173. } else {
  174. if ((carrier > max(stereo, dual)*2) &&
  175. (carrier < max(stereo, dual)*8) &&
  176. (carrier > 20 && carrier < 200) &&
  177. (noise < 10) &&
  178. (max(stereo, dual) > min(stereo, dual)*2)) {
  179. return ret;
  180. }
  181. }
  182. return V4L2_TUNER_SUB_MONO;
  183. }
  184. static s32 detect_btsc(struct cx88_core *core, s16 x[], u32 N)
  185. {
  186. s32 sap_ref = freq_magnitude(x, N, FREQ_BTSC_SAP_REF);
  187. s32 sap = freq_magnitude(x, N, FREQ_BTSC_SAP);
  188. s32 dual_ref = freq_magnitude(x, N, FREQ_BTSC_DUAL_REF);
  189. s32 dual = freq_magnitude(x, N, FREQ_BTSC_DUAL);
  190. dprintk(1, "detect btsc: dual_ref=%d, dual=%d, sap_ref=%d, sap=%d"
  191. "\n", dual_ref, dual, sap_ref, sap);
  192. /* FIXME: Currently not supported */
  193. return UNSET;
  194. }
  195. static s16 *read_rds_samples(struct cx88_core *core, u32 *N)
  196. {
  197. const struct sram_channel *srch = &cx88_sram_channels[SRAM_CH27];
  198. s16 *samples;
  199. unsigned int i;
  200. unsigned int bpl = srch->fifo_size/AUD_RDS_LINES;
  201. unsigned int spl = bpl/4;
  202. unsigned int sample_count = spl*(AUD_RDS_LINES-1);
  203. u32 current_address = cx_read(srch->ptr1_reg);
  204. u32 offset = (current_address - srch->fifo_start + bpl);
  205. dprintk(1, "read RDS samples: current_address=%08x (offset=%08x), "
  206. "sample_count=%d, aud_intstat=%08x\n", current_address,
  207. current_address - srch->fifo_start, sample_count,
  208. cx_read(MO_AUD_INTSTAT));
  209. samples = kmalloc(sizeof(s16)*sample_count, GFP_KERNEL);
  210. if (!samples)
  211. return NULL;
  212. *N = sample_count;
  213. for (i = 0; i < sample_count; i++) {
  214. offset = offset % (AUD_RDS_LINES*bpl);
  215. samples[i] = cx_read(srch->fifo_start + offset);
  216. offset += 4;
  217. }
  218. if (dsp_debug >= 2) {
  219. dprintk(2, "RDS samples dump: ");
  220. for (i = 0; i < sample_count; i++)
  221. printk("%hd ", samples[i]);
  222. printk(".\n");
  223. }
  224. return samples;
  225. }
  226. s32 cx88_dsp_detect_stereo_sap(struct cx88_core *core)
  227. {
  228. s16 *samples;
  229. u32 N = 0;
  230. s32 ret = UNSET;
  231. /* If audio RDS fifo is disabled, we can't read the samples */
  232. if (!(cx_read(MO_AUD_DMACNTRL) & 0x04))
  233. return ret;
  234. if (!(cx_read(AUD_CTL) & EN_FMRADIO_EN_RDS))
  235. return ret;
  236. /* Wait at least 500 ms after an audio standard change */
  237. if (time_before(jiffies, core->last_change + msecs_to_jiffies(500)))
  238. return ret;
  239. samples = read_rds_samples(core, &N);
  240. if (!samples)
  241. return ret;
  242. switch (core->tvaudio) {
  243. case WW_BG:
  244. case WW_DK:
  245. case WW_EIAJ:
  246. case WW_M:
  247. ret = detect_a2_a2m_eiaj(core, samples, N);
  248. break;
  249. case WW_BTSC:
  250. ret = detect_btsc(core, samples, N);
  251. break;
  252. case WW_NONE:
  253. case WW_I:
  254. case WW_L:
  255. case WW_I2SPT:
  256. case WW_FM:
  257. case WW_I2SADC:
  258. break;
  259. }
  260. kfree(samples);
  261. if (UNSET != ret)
  262. dprintk(1, "stereo/sap detection result:%s%s%s\n",
  263. (ret & V4L2_TUNER_SUB_MONO) ? " mono" : "",
  264. (ret & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
  265. (ret & V4L2_TUNER_SUB_LANG2) ? " dual" : "");
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(cx88_dsp_detect_stereo_sap);