cx88-dsp.c 8.6 KB

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