Path: blob/master/drivers/media/video/cx88/cx88-dsp.c
17768 views
/*1*2* Stereo and SAP detection for cx883*4* Copyright (c) 2009 Marton Balint <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (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 of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.19*/2021#include <linux/slab.h>22#include <linux/kernel.h>23#include <linux/module.h>24#include <linux/jiffies.h>25#include <asm/div64.h>2627#include "cx88.h"28#include "cx88-reg.h"2930#define INT_PI ((s32)(3.141592653589 * 32768.0))3132#define compat_remainder(a, b) \33((float)(((s32)((a)*100))%((s32)((b)*100)))/100.0)3435#define baseband_freq(carrier, srate, tone) ((s32)( \36(compat_remainder(carrier + tone, srate)) / srate * 2 * INT_PI))3738/* We calculate the baseband frequencies of the carrier and the pilot tones39* based on the the sampling rate of the audio rds fifo. */4041#define FREQ_A2_CARRIER baseband_freq(54687.5, 2689.36, 0.0)42#define FREQ_A2_DUAL baseband_freq(54687.5, 2689.36, 274.1)43#define FREQ_A2_STEREO baseband_freq(54687.5, 2689.36, 117.5)4445/* The frequencies below are from the reference driver. They probably need46* further adjustments, because they are not tested at all. You may even need47* to play a bit with the registers of the chip to select the proper signal48* for the input of the audio rds fifo, and measure it's sampling rate to49* calculate the proper baseband frequencies... */5051#define FREQ_A2M_CARRIER ((s32)(2.114516 * 32768.0))52#define FREQ_A2M_DUAL ((s32)(2.754916 * 32768.0))53#define FREQ_A2M_STEREO ((s32)(2.462326 * 32768.0))5455#define FREQ_EIAJ_CARRIER ((s32)(1.963495 * 32768.0)) /* 5pi/8 */56#define FREQ_EIAJ_DUAL ((s32)(2.562118 * 32768.0))57#define FREQ_EIAJ_STEREO ((s32)(2.601053 * 32768.0))5859#define FREQ_BTSC_DUAL ((s32)(1.963495 * 32768.0)) /* 5pi/8 */60#define FREQ_BTSC_DUAL_REF ((s32)(1.374446 * 32768.0)) /* 7pi/16 */6162#define FREQ_BTSC_SAP ((s32)(2.471532 * 32768.0))63#define FREQ_BTSC_SAP_REF ((s32)(1.730072 * 32768.0))6465/* The spectrum of the signal should be empty between these frequencies. */66#define FREQ_NOISE_START ((s32)(0.100000 * 32768.0))67#define FREQ_NOISE_END ((s32)(1.200000 * 32768.0))6869static unsigned int dsp_debug;70module_param(dsp_debug, int, 0644);71MODULE_PARM_DESC(dsp_debug, "enable audio dsp debug messages");7273#define dprintk(level, fmt, arg...) if (dsp_debug >= level) \74printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)7576static s32 int_cos(u32 x)77{78u32 t2, t4, t6, t8;79s32 ret;80u16 period = x / INT_PI;81if (period % 2)82return -int_cos(x - INT_PI);83x = x % INT_PI;84if (x > INT_PI/2)85return -int_cos(INT_PI/2 - (x % (INT_PI/2)));86/* Now x is between 0 and INT_PI/2.87* To calculate cos(x) we use it's Taylor polinom. */88t2 = x*x/32768/2;89t4 = t2*x/32768*x/32768/3/4;90t6 = t4*x/32768*x/32768/5/6;91t8 = t6*x/32768*x/32768/7/8;92ret = 32768-t2+t4-t6+t8;93return ret;94}9596static u32 int_goertzel(s16 x[], u32 N, u32 freq)97{98/* We use the Goertzel algorithm to determine the power of the99* given frequency in the signal */100s32 s_prev = 0;101s32 s_prev2 = 0;102s32 coeff = 2*int_cos(freq);103u32 i;104105u64 tmp;106u32 divisor;107108for (i = 0; i < N; i++) {109s32 s = x[i] + ((s64)coeff*s_prev/32768) - s_prev2;110s_prev2 = s_prev;111s_prev = s;112}113114tmp = (s64)s_prev2 * s_prev2 + (s64)s_prev * s_prev -115(s64)coeff * s_prev2 * s_prev / 32768;116117/* XXX: N must be low enough so that N*N fits in s32.118* Else we need two divisions. */119divisor = N * N;120do_div(tmp, divisor);121122return (u32) tmp;123}124125static u32 freq_magnitude(s16 x[], u32 N, u32 freq)126{127u32 sum = int_goertzel(x, N, freq);128return (u32)int_sqrt(sum);129}130131static u32 noise_magnitude(s16 x[], u32 N, u32 freq_start, u32 freq_end)132{133int i;134u32 sum = 0;135u32 freq_step;136int samples = 5;137138if (N > 192) {139/* The last 192 samples are enough for noise detection */140x += (N-192);141N = 192;142}143144freq_step = (freq_end - freq_start) / (samples - 1);145146for (i = 0; i < samples; i++) {147sum += int_goertzel(x, N, freq_start);148freq_start += freq_step;149}150151return (u32)int_sqrt(sum / samples);152}153154static s32 detect_a2_a2m_eiaj(struct cx88_core *core, s16 x[], u32 N)155{156s32 carrier, stereo, dual, noise;157s32 carrier_freq, stereo_freq, dual_freq;158s32 ret;159160switch (core->tvaudio) {161case WW_BG:162case WW_DK:163carrier_freq = FREQ_A2_CARRIER;164stereo_freq = FREQ_A2_STEREO;165dual_freq = FREQ_A2_DUAL;166break;167case WW_M:168carrier_freq = FREQ_A2M_CARRIER;169stereo_freq = FREQ_A2M_STEREO;170dual_freq = FREQ_A2M_DUAL;171break;172case WW_EIAJ:173carrier_freq = FREQ_EIAJ_CARRIER;174stereo_freq = FREQ_EIAJ_STEREO;175dual_freq = FREQ_EIAJ_DUAL;176break;177default:178printk(KERN_WARNING "%s/0: unsupported audio mode %d for %s\n",179core->name, core->tvaudio, __func__);180return UNSET;181}182183carrier = freq_magnitude(x, N, carrier_freq);184stereo = freq_magnitude(x, N, stereo_freq);185dual = freq_magnitude(x, N, dual_freq);186noise = noise_magnitude(x, N, FREQ_NOISE_START, FREQ_NOISE_END);187188dprintk(1, "detect a2/a2m/eiaj: carrier=%d, stereo=%d, dual=%d, "189"noise=%d\n", carrier, stereo, dual, noise);190191if (stereo > dual)192ret = V4L2_TUNER_SUB_STEREO;193else194ret = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;195196if (core->tvaudio == WW_EIAJ) {197/* EIAJ checks may need adjustments */198if ((carrier > max(stereo, dual)*2) &&199(carrier < max(stereo, dual)*6) &&200(carrier > 20 && carrier < 200) &&201(max(stereo, dual) > min(stereo, dual))) {202/* For EIAJ the carrier is always present,203so we probably don't need noise detection */204return ret;205}206} else {207if ((carrier > max(stereo, dual)*2) &&208(carrier < max(stereo, dual)*8) &&209(carrier > 20 && carrier < 200) &&210(noise < 10) &&211(max(stereo, dual) > min(stereo, dual)*2)) {212return ret;213}214}215return V4L2_TUNER_SUB_MONO;216}217218static s32 detect_btsc(struct cx88_core *core, s16 x[], u32 N)219{220s32 sap_ref = freq_magnitude(x, N, FREQ_BTSC_SAP_REF);221s32 sap = freq_magnitude(x, N, FREQ_BTSC_SAP);222s32 dual_ref = freq_magnitude(x, N, FREQ_BTSC_DUAL_REF);223s32 dual = freq_magnitude(x, N, FREQ_BTSC_DUAL);224dprintk(1, "detect btsc: dual_ref=%d, dual=%d, sap_ref=%d, sap=%d"225"\n", dual_ref, dual, sap_ref, sap);226/* FIXME: Currently not supported */227return UNSET;228}229230static s16 *read_rds_samples(struct cx88_core *core, u32 *N)231{232const struct sram_channel *srch = &cx88_sram_channels[SRAM_CH27];233s16 *samples;234235unsigned int i;236unsigned int bpl = srch->fifo_size/AUD_RDS_LINES;237unsigned int spl = bpl/4;238unsigned int sample_count = spl*(AUD_RDS_LINES-1);239240u32 current_address = cx_read(srch->ptr1_reg);241u32 offset = (current_address - srch->fifo_start + bpl);242243dprintk(1, "read RDS samples: current_address=%08x (offset=%08x), "244"sample_count=%d, aud_intstat=%08x\n", current_address,245current_address - srch->fifo_start, sample_count,246cx_read(MO_AUD_INTSTAT));247248samples = kmalloc(sizeof(s16)*sample_count, GFP_KERNEL);249if (!samples)250return NULL;251252*N = sample_count;253254for (i = 0; i < sample_count; i++) {255offset = offset % (AUD_RDS_LINES*bpl);256samples[i] = cx_read(srch->fifo_start + offset);257offset += 4;258}259260if (dsp_debug >= 2) {261dprintk(2, "RDS samples dump: ");262for (i = 0; i < sample_count; i++)263printk("%hd ", samples[i]);264printk(".\n");265}266267return samples;268}269270s32 cx88_dsp_detect_stereo_sap(struct cx88_core *core)271{272s16 *samples;273u32 N = 0;274s32 ret = UNSET;275276/* If audio RDS fifo is disabled, we can't read the samples */277if (!(cx_read(MO_AUD_DMACNTRL) & 0x04))278return ret;279if (!(cx_read(AUD_CTL) & EN_FMRADIO_EN_RDS))280return ret;281282/* Wait at least 500 ms after an audio standard change */283if (time_before(jiffies, core->last_change + msecs_to_jiffies(500)))284return ret;285286samples = read_rds_samples(core, &N);287288if (!samples)289return ret;290291switch (core->tvaudio) {292case WW_BG:293case WW_DK:294case WW_EIAJ:295case WW_M:296ret = detect_a2_a2m_eiaj(core, samples, N);297break;298case WW_BTSC:299ret = detect_btsc(core, samples, N);300break;301case WW_NONE:302case WW_I:303case WW_L:304case WW_I2SPT:305case WW_FM:306case WW_I2SADC:307break;308}309310kfree(samples);311312if (UNSET != ret)313dprintk(1, "stereo/sap detection result:%s%s%s\n",314(ret & V4L2_TUNER_SUB_MONO) ? " mono" : "",315(ret & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",316(ret & V4L2_TUNER_SUB_LANG2) ? " dual" : "");317318return ret;319}320EXPORT_SYMBOL(cx88_dsp_detect_stereo_sap);321322323324