summaryrefslogtreecommitdiff
path: root/drivers/media/radio/stfm1000
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/radio/stfm1000')
-rw-r--r--drivers/media/radio/stfm1000/Kconfig26
-rw-r--r--drivers/media/radio/stfm1000/Makefile14
-rw-r--r--drivers/media/radio/stfm1000/gen-precalc.c62
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-alsa.c660
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-core.c2459
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-filter.c860
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-filter.h185
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-i2c.c452
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-rds.c1529
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-rds.h364
-rw-r--r--drivers/media/radio/stfm1000/stfm1000-regs.h165
-rw-r--r--drivers/media/radio/stfm1000/stfm1000.h254
12 files changed, 7030 insertions, 0 deletions
diff --git a/drivers/media/radio/stfm1000/Kconfig b/drivers/media/radio/stfm1000/Kconfig
new file mode 100644
index 000000000000..ef30bf87de0b
--- /dev/null
+++ b/drivers/media/radio/stfm1000/Kconfig
@@ -0,0 +1,26 @@
+config RADIO_STFM1000
+ tristate "STFM1000 support"
+ depends on I2C && VIDEO_V4L2 && ARCH_STMP3XXX
+ select I2C_ALGOBIT
+ ---help---
+ Choose Y here if you have this FM radio card, and then fill in the
+ port address below.
+
+ In order to control your radio card, you will need to use programs
+ that are compatible with the Video For Linux API. Information on
+ this API and pointers to "v4l" programs may be found at
+ <file:Documentation/video4linux/API.html>.
+
+ To compile this driver as a module, choose M here: the
+ module will be called stfm1000.
+
+config RADIO_STFM1000_ALSA
+ tristate "STFM1000 audio support"
+ depends on RADIO_STFM1000 && SND
+ select SND_PCM
+ ---help---
+ This is a video4linux driver for direct (DMA) audio in
+ STFM1000 using ALSA
+
+ To compile this driver as a module, choose M here: the
+ module will be called stfm1000-alsa.
diff --git a/drivers/media/radio/stfm1000/Makefile b/drivers/media/radio/stfm1000/Makefile
new file mode 100644
index 000000000000..01f354001a64
--- /dev/null
+++ b/drivers/media/radio/stfm1000/Makefile
@@ -0,0 +1,14 @@
+stfm1000-objs := stfm1000-core.o stfm1000-i2c.o stfm1000-precalc.o stfm1000-filter.o stfm1000-rds.o
+
+clean-files += stfm1000-precalc.o
+
+obj-$(CONFIG_RADIO_STFM1000) += stfm1000.o
+obj-$(CONFIG_RADIO_STFM1000_ALSA) += stfm1000-alsa.o
+
+stfm1000-core.o: $(obj)/stfm1000-precalc.h
+
+hostprogs-$(CONFIG_RADIO_STFM1000) := gen-precalc
+$(obj)/stfm1000-precalc.c: $(obj)/gen-precalc $(src)/stfm1000-regs.h
+ $(obj)/gen-precalc >$@
+
+EXTRA_CFLAGS += -Idrivers/media/radio
diff --git a/drivers/media/radio/stfm1000/gen-precalc.c b/drivers/media/radio/stfm1000/gen-precalc.c
new file mode 100644
index 000000000000..d3797dbef815
--- /dev/null
+++ b/drivers/media/radio/stfm1000/gen-precalc.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+/* generate precalculated tables */
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "stfm1000-regs.h"
+
+static void generate_tune1(void)
+{
+ int start, end;
+ int ndiv; // N Divider in PLL
+ int incr; // Increment in PLL
+ int cicosr; // CIC oversampling ratio
+ int sdnominal; // value to serve pilot/interpolator loop in SD
+ int i, temp; // used in tuning table construction
+
+ start = STFM1000_FREQUENCY_100KHZ_MIN;
+ end = start + STFM1000_FREQUENCY_100KHZ_RANGE;
+
+ printf("const struct stfm1000_tune1\n"
+ "stfm1000_tune1_table[STFM1000_FREQUENCY_100KHZ_RANGE] = {\n");
+
+ for (i = start; i < end; i++) {
+
+ ndiv = (int)((i+14)/15) - 48;
+ incr = i - (int)(i/15)*15;
+ cicosr = (int)(i*2/3.0/16.0 + 0.5);
+ sdnominal = (int)(i*100.0e3/1.5/(double)cicosr/2.0/2.0*2.0*8.0*256.0/228.0e3*65536);
+
+ temp = 0x00000000; // clear
+ temp = temp | ((cicosr<<9) & STFM1000_TUNE1_CICOSR); // bits[14:9] 0x00007E00
+ temp = temp | ((ndiv<<4) & STFM1000_TUNE1_PLL_DIV); // bits[8:4] 0x000001F0
+ temp = temp | ((incr) & STFM1000_TUNE1_PLL_DIV); // bits[3:0] 0x0000000F
+
+ printf("\t[%d - STFM1000_FREQUENCY_100KHZ_MIN] = "
+ "{ .tune1 = 0x%08x, .sdnom = 0x%08x },\n",
+ i, temp, sdnominal);
+ }
+ printf("};\n");
+
+}
+
+int main(int argc, char *argv[])
+{
+ printf("#include \"stfm1000-regs.h\"\n\n");
+
+ generate_tune1();
+
+ return 0;
+}
diff --git a/drivers/media/radio/stfm1000/stfm1000-alsa.c b/drivers/media/radio/stfm1000/stfm1000-alsa.c
new file mode 100644
index 000000000000..d1da4475bc07
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-alsa.c
@@ -0,0 +1,660 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/uaccess.h>
+#include <linux/videodev2.h>
+#include <media/v4l2-common.h>
+#include <linux/i2c.h>
+#include <linux/irq.h>
+#include <linux/math64.h>
+
+#include <sound/control.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <linux/version.h> /* for KERNEL_VERSION MACRO */
+#include <linux/interrupt.h>
+#include <linux/dma-mapping.h>
+#include <mach/regs-dri.h>
+#include <mach/regs-apbx.h>
+#include <mach/regs-clkctrl.h>
+
+#include "stfm1000.h"
+
+#define STFM1000_PERIODS 16
+
+static int stfm1000_snd_volume_info(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_info *uinfo)
+{
+ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+ uinfo->count = 2; /* two channels */
+ uinfo->value.integer.min = 0;
+ uinfo->value.integer.max = 20;
+ return 0;
+}
+
+static int stfm1000_snd_volume_get(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct stfm1000 *stfm1000 = snd_kcontrol_chip(kcontrol);
+
+ (void)stfm1000;
+ ucontrol->value.integer.value[0] = 0; /* left */
+ ucontrol->value.integer.value[1] = 0; /* right */
+ return 0;
+}
+
+static int stfm1000_snd_volume_put(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct stfm1000 *stfm1000 = snd_kcontrol_chip(kcontrol);
+ int change;
+ int left, right;
+
+ (void)stfm1000;
+
+ left = ucontrol->value.integer.value[0];
+ if (left < 0)
+ left = 0;
+ if (left > 20)
+ left = 20;
+ right = ucontrol->value.integer.value[1];
+ if (right < 0)
+ right = 0;
+ if (right > 20)
+ right = 20;
+
+ change = 1;
+ return change;
+}
+
+static struct snd_kcontrol_new stfm1000_snd_controls[] = {
+ {
+ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+ .name = "Radio Volume",
+ .index = 0,
+ .info = stfm1000_snd_volume_info,
+ .get = stfm1000_snd_volume_get,
+ .put = stfm1000_snd_volume_put,
+ .private_value = 0,
+ },
+};
+
+static struct snd_pcm_hardware stfm1000_snd_capture = {
+
+ .info = SNDRV_PCM_INFO_MMAP |
+ SNDRV_PCM_INFO_MMAP_VALID |
+ SNDRV_PCM_INFO_INTERLEAVED |
+ SNDRV_PCM_INFO_BLOCK_TRANSFER,
+ .formats = SNDRV_PCM_FMTBIT_S16_LE,
+ .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
+ .rate_min = 44100,
+ .rate_max = 48000,
+ .channels_min = 2,
+ .channels_max = 2,
+ .buffer_bytes_max = SZ_256K,
+ .period_bytes_min = SZ_4K,
+ .period_bytes_max = SZ_4K,
+ .periods_min = STFM1000_PERIODS,
+ .periods_max = STFM1000_PERIODS,
+};
+
+static int stfm1000_snd_capture_open(struct snd_pcm_substream *substream)
+{
+ struct snd_pcm_runtime *runtime = substream->runtime;
+ struct stfm1000 *stfm1000 = snd_pcm_substream_chip(substream);
+ int err;
+
+ /* should never happen, just a sanity check */
+ BUG_ON(stfm1000 == NULL);
+
+ mutex_lock(&stfm1000->deffered_work_lock);
+ stfm1000->read_count = 0;
+ stfm1000->read_offset = 0;
+
+ stfm1000->substream = substream;
+ runtime->private_data = stfm1000;
+ runtime->hw = stfm1000_snd_capture;
+
+ mutex_unlock(&stfm1000->deffered_work_lock);
+
+ err = snd_pcm_hw_constraint_integer(runtime,
+ SNDRV_PCM_HW_PARAM_PERIODS);
+ if (err < 0) {
+ printk(KERN_ERR "%s: snd_pcm_hw_constraint_integer "
+ "SNDRV_PCM_HW_PARAM_PERIODS failed\n", __func__);
+ return err;
+ }
+
+ err = snd_pcm_hw_constraint_step(runtime, 0,
+ SNDRV_PCM_HW_PARAM_PERIODS, 2);
+ if (err < 0) {
+ printk(KERN_ERR "%s: snd_pcm_hw_constraint_integer "
+ "SNDRV_PCM_HW_PARAM_PERIODS failed\n", __func__);
+ return err;
+ }
+
+ return 0;
+}
+
+static int stfm1000_snd_capture_close(struct snd_pcm_substream *substream)
+{
+ struct stfm1000 *stfm1000 = snd_pcm_substream_chip(substream);
+
+ (void)stfm1000; /* nothing */
+ return 0;
+}
+
+static int stfm1000_snd_hw_params(struct snd_pcm_substream *substream,
+ struct snd_pcm_hw_params *hw_params)
+{
+ struct stfm1000 *stfm1000 = snd_pcm_substream_chip(substream);
+ unsigned int period_size, periods;
+ int ret;
+
+ periods = params_periods(hw_params);
+ period_size = params_period_bytes(hw_params);
+
+ if (period_size < 0x100 || period_size > 0x10000)
+ return -EINVAL;
+ if (periods < STFM1000_PERIODS)
+ return -EINVAL;
+ if (period_size * periods > 1024 * 1024)
+ return -EINVAL;
+
+ stfm1000->blocks = periods;
+ stfm1000->blksize = period_size;
+ stfm1000->bufsize = params_buffer_bytes(hw_params);
+
+ ret = snd_pcm_lib_malloc_pages(substream, stfm1000->bufsize);
+ if (ret < 0) { /* 0 & 1 are valid returns */
+ printk(KERN_ERR "%s: snd_pcm_lib_malloc_pages() failed\n",
+ __func__);
+ return ret;
+ }
+
+ /* the dri buffer is twice as large as the audio buffer */
+ stfm1000->dri_bufsz = (stfm1000->bufsize / 4) *
+ sizeof(struct stfm1000_dri_sample);
+ stfm1000->dri_buf = dma_alloc_coherent(&stfm1000->radio.dev,
+ stfm1000->dri_bufsz, &stfm1000->dri_phys, GFP_KERNEL);
+ if (stfm1000->dri_buf == NULL) {
+ printk(KERN_ERR "%s: dma_alloc_coherent() failed\n", __func__);
+ snd_pcm_lib_free_pages(substream);
+ return -ENOMEM;
+ }
+
+ return ret;
+}
+
+static int stfm1000_snd_hw_free(struct snd_pcm_substream *substream)
+{
+ struct stfm1000 *stfm1000 = snd_pcm_substream_chip(substream);
+
+ if (stfm1000->dri_buf) {
+ dma_free_coherent(&stfm1000->radio.dev,
+ (stfm1000->bufsize / 4) *
+ sizeof(struct stfm1000_dri_sample),
+ stfm1000->dri_buf, stfm1000->dri_phys);
+ stfm1000->dri_buf = NULL;
+ stfm1000->dri_phys = 0;
+ }
+ snd_pcm_lib_free_pages(substream);
+ return 0;
+}
+
+
+static int stfm1000_snd_capture_prepare(struct snd_pcm_substream *substream)
+{
+ struct snd_pcm_runtime *runtime = substream->runtime;
+ struct stfm1000 *stfm1000 = snd_pcm_substream_chip(substream);
+
+ stfm1000->substream = substream;
+
+ if (snd_pcm_format_width(runtime->format) != 16 ||
+ !snd_pcm_format_signed(runtime->format) ||
+ snd_pcm_format_big_endian(runtime->format)) {
+ printk(KERN_INFO "STFM1000: ALSA capture_prepare illegal format\n");
+ return -EINVAL;
+ }
+
+ /* really shouldn't happen */
+ BUG_ON(stfm1000->blocks > stfm1000->desc_num);
+
+ mutex_lock(&stfm1000->deffered_work_lock);
+
+ if (stfm1000->now_recording != 0) {
+ printk(KERN_INFO "STFM1000: ALSA capture_prepare still running\n");
+ mutex_unlock(&stfm1000->deffered_work_lock);
+ return -EBUSY;
+ }
+ stfm1000->now_recording = 1;
+
+ mutex_unlock(&stfm1000->deffered_work_lock);
+
+ return 0;
+
+}
+
+static void stfm1000_snd_capture_trigger_start(struct work_struct *work)
+{
+ struct stfm1000 *stfm1000;
+
+ stfm1000 = container_of(work, struct stfm1000,
+ snd_capture_start_work.work);
+
+ mutex_lock(&stfm1000->deffered_work_lock);
+
+ BUG_ON(stfm1000->now_recording != 1);
+
+ stfm1000_bring_up(stfm1000);
+
+ mutex_unlock(&stfm1000->deffered_work_lock);
+}
+
+static void stfm1000_snd_capture_trigger_stop(struct work_struct *work)
+{
+ struct stfm1000 *stfm1000;
+
+ stfm1000 = container_of(work, struct stfm1000,
+ snd_capture_stop_work.work);
+
+ mutex_lock(&stfm1000->deffered_work_lock);
+
+ stfm1000->stopping_recording = 1;
+
+ stfm1000_take_down(stfm1000);
+
+ BUG_ON(stfm1000->now_recording != 1);
+ stfm1000->now_recording = 0;
+
+ stfm1000->stopping_recording = 0;
+
+ mutex_unlock(&stfm1000->deffered_work_lock);
+}
+
+static int execute_non_atomic(work_func_t fn, struct execute_work *ew)
+{
+ if (!in_atomic() && !in_interrupt()) {
+ fn(&ew->work);
+ return 0;
+ }
+
+ INIT_WORK(&ew->work, fn);
+ schedule_work(&ew->work);
+
+ return 1;
+}
+
+static int stfm1000_snd_capture_trigger(struct snd_pcm_substream *substream,
+ int cmd)
+{
+ struct snd_pcm_runtime *runtime = substream->runtime;
+ struct stfm1000 *stfm1000 = runtime->private_data;
+ int err = 0;
+
+ (void)stfm1000;
+
+ switch (cmd) {
+
+ case SNDRV_PCM_TRIGGER_START:
+ execute_non_atomic(stfm1000_snd_capture_trigger_start,
+ &stfm1000->snd_capture_start_work);
+ break;
+
+ case SNDRV_PCM_TRIGGER_STOP:
+ execute_non_atomic(stfm1000_snd_capture_trigger_stop,
+ &stfm1000->snd_capture_stop_work);
+ break;
+
+ case SNDRV_PCM_TRIGGER_RESUME:
+ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+ stmp3xxx_dma_unfreeze(stfm1000->dma_ch);
+ break;
+
+ case SNDRV_PCM_TRIGGER_SUSPEND:
+ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+ stmp3xxx_dma_freeze(stfm1000->dma_ch);
+ break;
+
+ default:
+ err = -EINVAL;
+ break;
+ }
+
+ return err;
+}
+
+static snd_pcm_uframes_t
+stfm1000_snd_capture_pointer(struct snd_pcm_substream *substream)
+{
+ struct snd_pcm_runtime *runtime = substream->runtime;
+ struct stfm1000 *stfm1000 = runtime->private_data;
+
+ if (stfm1000->read_count) {
+ stfm1000->read_count -= snd_pcm_lib_period_bytes(substream);
+ stfm1000->read_offset += snd_pcm_lib_period_bytes(substream);
+ if (stfm1000->read_offset == substream->runtime->dma_bytes)
+ stfm1000->read_offset = 0;
+ }
+
+ return bytes_to_frames(runtime, stfm1000->read_offset);
+}
+
+static struct snd_pcm_ops stfm1000_snd_capture_ops = {
+ .open = stfm1000_snd_capture_open,
+ .close = stfm1000_snd_capture_close,
+ .ioctl = snd_pcm_lib_ioctl,
+ .hw_params = stfm1000_snd_hw_params,
+ .hw_free = stfm1000_snd_hw_free,
+ .prepare = stfm1000_snd_capture_prepare,
+ .trigger = stfm1000_snd_capture_trigger,
+ .pointer = stfm1000_snd_capture_pointer,
+};
+
+static void stfm1000_snd_free(struct snd_card *card)
+{
+ struct stfm1000 *stfm1000 = card->private_data;
+
+ free_irq(IRQ_DRI_ATTENTION, stfm1000);
+ free_irq(IRQ_DRI_DMA, stfm1000);
+}
+
+static int stfm1000_alsa_instance_init(struct stfm1000 *stfm1000)
+{
+ int ret, i;
+ struct snd_card *card;
+ struct snd_pcm *pcm;
+ struct snd_kcontrol *ctl;
+
+ mutex_init(&stfm1000->deffered_work_lock);
+
+ /* request dma channel */
+ stfm1000->desc_num = STFM1000_PERIODS;
+ stfm1000->dma_ch = STMP3xxx_DMA(5, STMP3XXX_BUS_APBX);
+ ret = stmp3xxx_dma_request(stfm1000->dma_ch, &stfm1000->radio.dev,
+ "stmp3xxx dri");
+ if (ret != 0) {
+ printk(KERN_ERR "%s: stmp3xxx_dma_request failed\n", __func__);
+ goto err;
+ }
+
+ stfm1000->dma = kzalloc(sizeof(*stfm1000->dma) * stfm1000->desc_num,
+ GFP_KERNEL);
+ if (stfm1000->dma == NULL) {
+ printk(KERN_ERR "%s: stmp3xxx_dma_request failed\n", __func__);
+ ret = -ENOMEM;
+ goto err_rel_dma;
+ }
+
+ for (i = 0; i < stfm1000->desc_num; i++) {
+ ret = stmp3xxx_dma_allocate_command(stfm1000->dma_ch,
+ &stfm1000->dma[i]);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: stmp3xxx_dma_allocate_command "
+ "failed\n", __func__);
+ goto err_free_dma;
+ }
+ }
+
+ /* allocate ALSA card structure (we only need an extra pointer
+ * back to stfm1000) */
+ card = snd_card_new(-1, NULL, THIS_MODULE, 0);
+ if (card == NULL) {
+ ret = -ENOMEM;
+ printk(KERN_ERR "%s: snd_card_new failed\n", __func__);
+ goto err_free_dma;
+ }
+ stfm1000->card = card;
+ card->private_data = stfm1000; /* point back */
+
+ /* mixer controls */
+ strcpy(card->driver, "stfm1000");
+ card->private_free = stfm1000_snd_free;
+
+ strcpy(card->mixername, "stfm1000 mixer");
+ for (i = 0; i < ARRAY_SIZE(stfm1000_snd_controls); i++) {
+ ctl = snd_ctl_new1(&stfm1000_snd_controls[i], stfm1000);
+ if (ctl == NULL) {
+ printk(KERN_ERR "%s: snd_ctl_new1 failed\n", __func__);
+ goto err_free_controls;
+ }
+ ret = snd_ctl_add(card, ctl);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: snd_ctl_add failed\n", __func__);
+ goto err_free_controls;
+ }
+ }
+
+ /* PCM */
+ ret = snd_pcm_new(card, "STFM1000 PCM", 0, 0, 1, &pcm);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: snd_ctl_add failed\n", __func__);
+ goto err_free_controls;
+ }
+ stfm1000->pcm = pcm;
+ pcm->private_data = stfm1000; /* point back */
+
+ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
+ &stfm1000_snd_capture_ops);
+ pcm->info_flags = 0;
+ strcpy(pcm->name, "STFM1000 PCM");
+
+ snd_card_set_dev(card, &stfm1000->radio.dev);
+ strcpy(card->shortname, "STFM1000");
+
+ ret = snd_pcm_lib_preallocate_pages_for_all(stfm1000->pcm,
+ SNDRV_DMA_TYPE_CONTINUOUS, card->dev, SZ_256K, SZ_256K);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: snd_pcm_lib_preallocate_pages_for_all "
+ "failed\n", __func__);
+ goto err_free_pcm;
+ }
+
+ ret = request_irq(IRQ_DRI_DMA, stfm1000_dri_dma_irq, 0, "stfm1000",
+ stfm1000);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: request_irq failed\n", __func__);
+ goto err_free_prealloc;
+ }
+
+ ret = request_irq(IRQ_DRI_ATTENTION, stfm1000_dri_attn_irq, 0,
+ "stfm1000", stfm1000);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: request_irq failed\n", __func__);
+ goto err_rel_irq;
+ }
+
+ ret = snd_card_register(stfm1000->card);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: snd_card_register failed\n", __func__);
+ goto err_rel_irq2;
+ }
+
+ /* Enable completion interrupt */
+ stmp3xxx_dma_clear_interrupt(stfm1000->dma_ch);
+ stmp3xxx_dma_enable_interrupt(stfm1000->dma_ch);
+
+ printk(KERN_INFO "%s/alsa: %s registered\n", "STFM1000",
+ card->longname);
+
+ return 0;
+
+err_rel_irq2:
+ free_irq(IRQ_DRI_ATTENTION, stfm1000);
+
+err_rel_irq:
+ free_irq(IRQ_DRI_DMA, stfm1000);
+
+err_free_prealloc:
+ snd_pcm_lib_preallocate_free_for_all(stfm1000->pcm);
+
+err_free_pcm:
+ /* XXX TODO */
+
+err_free_controls:
+ /* XXX TODO */
+
+/* err_free_card: */
+ snd_card_free(stfm1000->card);
+
+err_free_dma:
+ for (i = stfm1000->desc_num - 1; i >= 0; i--) {
+ if (stfm1000->dma[i].command != NULL)
+ stmp3xxx_dma_free_command(stfm1000->dma_ch,
+ &stfm1000->dma[i]);
+ }
+
+err_rel_dma:
+ stmp3xxx_dma_release(stfm1000->dma_ch);
+err:
+ return ret;
+}
+
+static void stfm1000_alsa_instance_release(struct stfm1000 *stfm1000)
+{
+ int i;
+
+ stmp3xxx_dma_clear_interrupt(stfm1000->dma_ch);
+ stmp3xxx_arch_dma_reset_channel(stfm1000->dma_ch);
+
+ snd_card_free(stfm1000->card);
+
+ for (i = stfm1000->desc_num - 1; i >= 0; i--)
+ stmp3xxx_dma_free_command(stfm1000->dma_ch, &stfm1000->dma[i]);
+
+ kfree(stfm1000->dma);
+
+ stmp3xxx_dma_release(stfm1000->dma_ch);
+}
+
+static void stfm1000_alsa_dma_irq(struct stfm1000 *stfm1000)
+{
+ struct snd_pcm_runtime *runtime;
+ int desc;
+ s16 *src, *dst;
+
+ if (stfm1000->stopping_recording)
+ return;
+
+ if (stfm1000->read_count >= stfm1000->blksize *
+ (stfm1000->blocks - 2)) {
+ printk(KERN_ERR "irq: overrun %d - Blocks in %d\n",
+ stfm1000->read_count, stfm1000->blocks);
+ return;
+ }
+
+ /* someone has brutally killed user-space */
+ if (stfm1000->substream == NULL ||
+ stfm1000->substream->runtime == NULL)
+ return;
+
+ BUG_ON(stfm1000->substream == NULL);
+ BUG_ON(stfm1000->substream->runtime == NULL);
+
+ desc = stfm1000->read_offset / stfm1000->blksize;
+ runtime = stfm1000->substream->runtime;
+
+ if (runtime->dma_area == NULL)
+ printk(KERN_INFO "runtime->dma_area = NULL\n");
+ BUG_ON(runtime->dma_area == NULL);
+ if (stfm1000->dri_buf == NULL)
+ printk(KERN_INFO "stfm1000->dri_buf = NULL\n");
+ BUG_ON(stfm1000->dri_buf == NULL);
+
+ if (desc >= stfm1000->blocks) {
+ printk(KERN_INFO "desc=%d ->blocks=%d\n",
+ desc, stfm1000->blocks);
+ printk(KERN_INFO "->read_offset=%x ->blksize=%x\n",
+ stfm1000->read_offset, stfm1000->blksize);
+ }
+ BUG_ON(desc >= stfm1000->blocks);
+
+ src = stfm1000->dri_buf + desc * (stfm1000->blksize * 2);
+ dst = (void *)runtime->dma_area + desc * stfm1000->blksize;
+
+ /* perform filtering */
+ stfm1000_decode_block(stfm1000, src, dst, stfm1000->blksize / 4);
+
+ stfm1000->read_count += stfm1000->blksize;
+
+ if (stfm1000->read_count >=
+ snd_pcm_lib_period_bytes(stfm1000->substream))
+ snd_pcm_period_elapsed(stfm1000->substream);
+}
+
+static void stfm1000_alsa_attn_irq(struct stfm1000 *stfm1000)
+{
+ /* nothing */
+}
+
+struct stfm1000_alsa_ops stfm1000_default_alsa_ops = {
+ .init = stfm1000_alsa_instance_init,
+ .release = stfm1000_alsa_instance_release,
+ .dma_irq = stfm1000_alsa_dma_irq,
+ .attn_irq = stfm1000_alsa_attn_irq,
+};
+
+static int stfm1000_alsa_init(void)
+{
+ struct stfm1000 *stfm1000 = NULL;
+ struct list_head *list;
+ int ret;
+
+ stfm1000_alsa_ops = &stfm1000_default_alsa_ops;
+
+ list_for_each(list, &stfm1000_devlist) {
+ stfm1000 = list_entry(list, struct stfm1000, devlist);
+ ret = (*stfm1000_alsa_ops->init)(stfm1000);
+ if (ret != 0) {
+ printk(KERN_ERR "stfm1000 ALSA driver for DMA sound "
+ "failed init.\n");
+ return ret;
+ }
+ stfm1000->alsa_initialized = 1;
+ }
+
+ printk(KERN_INFO "stfm1000 ALSA driver for DMA sound loaded\n");
+
+ return 0;
+}
+
+static void stfm1000_alsa_exit(void)
+{
+ struct stfm1000 *stfm1000 = NULL;
+ struct list_head *list;
+
+ list_for_each(list, &stfm1000_devlist) {
+ stfm1000 = list_entry(list, struct stfm1000, devlist);
+
+ if (!stfm1000->alsa_initialized)
+ continue;
+
+ stfm1000_take_down(stfm1000);
+ (*stfm1000_alsa_ops->release)(stfm1000);
+ stfm1000->alsa_initialized = 0;
+ }
+
+ printk(KERN_INFO "stfm1000 ALSA driver for DMA sound unloaded\n");
+}
+
+/* We initialize this late, to make sure the sound system is up and running */
+late_initcall(stfm1000_alsa_init);
+module_exit(stfm1000_alsa_exit);
+
+MODULE_AUTHOR("Pantelis Antoniou");
+MODULE_DESCRIPTION("An ALSA PCM driver for the STFM1000 chip.");
+MODULE_LICENSE("GPL");
diff --git a/drivers/media/radio/stfm1000/stfm1000-core.c b/drivers/media/radio/stfm1000/stfm1000-core.c
new file mode 100644
index 000000000000..5086100bb480
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-core.c
@@ -0,0 +1,2459 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/uaccess.h>
+#include <linux/videodev2.h>
+#include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
+#include <linux/i2c.h>
+#include <linux/irq.h>
+#include <linux/math64.h>
+
+#include <sound/control.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <linux/version.h> /* for KERNEL_VERSION MACRO */
+#include <linux/interrupt.h>
+#include <linux/dma-mapping.h>
+#include <linux/device.h>
+#include <linux/freezer.h>
+#include <linux/kthread.h>
+#include <mach/regs-dri.h>
+#include <mach/regs-apbx.h>
+#include <mach/regs-clkctrl.h>
+
+#include "stfm1000.h"
+
+static DEFINE_MUTEX(devlist_lock);
+static unsigned int stfm1000_devcount;
+
+LIST_HEAD(stfm1000_devlist);
+EXPORT_SYMBOL(stfm1000_devlist);
+
+/* alsa interface */
+struct stfm1000_alsa_ops *stfm1000_alsa_ops;
+EXPORT_SYMBOL(stfm1000_alsa_ops);
+
+/* region, 0=US, 1=europe */
+static int georegion = 1; /* default is europe */
+static int rds_enable = 1; /* default is enabled */
+
+static int sw_tune(struct stfm1000 *stfm1000, u32 freq);
+
+static const const char *stfm1000_get_rev_txt(u32 id)
+{
+ switch (id) {
+ case 0x01: return "TA1";
+ case 0x02: return "TA2";
+ case 0x11: return "TB1";
+ case 0x12: return "TB2";
+ }
+ return NULL;
+}
+
+static const struct stfm1000_reg stfm1000_tb2_powerup[] = {
+ STFM1000_REG(REF, 0x00200000),
+ STFM1000_DELAY(20),
+ STFM1000_REG(DATAPATH, 0x00010210),
+ STFM1000_REG(TUNE1, 0x0004CF01),
+ STFM1000_REG(SDNOMINAL, 0x1C5EBCF0),
+ STFM1000_REG(PILOTTRACKING, 0x000001B6),
+ STFM1000_REG(INITIALIZATION1, 0x9fb80008),
+ STFM1000_REG(INITIALIZATION2, 0x8516e444 | STFM1000_DEEMPH_50_75B),
+ STFM1000_REG(INITIALIZATION3, 0x1402190b),
+ STFM1000_REG(INITIALIZATION4, 0x525bf052),
+ STFM1000_REG(INITIALIZATION5, 0x1000d106),
+ STFM1000_REG(INITIALIZATION6, 0x000062cb),
+ STFM1000_REG(AGC_CONTROL1, 0x1BCB2202),
+ STFM1000_REG(AGC_CONTROL2, 0x000020F0),
+ STFM1000_REG(CLK1, 0x10000000),
+ STFM1000_REG(CLK1, 0x20000000),
+ STFM1000_REG(CLK1, 0x00000000),
+ STFM1000_REG(CLK2, 0x7f000000),
+ STFM1000_REG(REF, 0x00B8222D),
+ STFM1000_REG(CLK1, 0x30000000),
+ STFM1000_REG(CLK1, 0x30002000),
+ STFM1000_REG(CLK1, 0x10002000),
+ STFM1000_REG(LNA, 0x0D080009),
+ STFM1000_DELAY(10),
+ STFM1000_REG(MIXFILT, 0x00008000),
+ STFM1000_REG(MIXFILT, 0x00000000),
+ STFM1000_REG(MIXFILT, 0x00007205),
+ STFM1000_REG(ADC, 0x001B3282),
+ STFM1000_REG(ATTENTION, 0x0000003F),
+ STFM1000_END,
+};
+
+static const struct stfm1000_reg stfm1000_ta2_powerup[] = {
+ STFM1000_REG(REF, 0x00200000),
+ STFM1000_DELAY(20),
+ STFM1000_REG(DATAPATH, 0x00010210),
+ STFM1000_REG(TUNE1, 0x00044F01),
+ STFM1000_REG(SDNOMINAL, 0x1C5EBCF0),
+ STFM1000_REG(PILOTTRACKING, 0x000001B6),
+ STFM1000_REG(INITIALIZATION1, 0x9fb80008),
+ STFM1000_REG(INITIALIZATION2, 0x8506e444),
+ STFM1000_REG(INITIALIZATION3, 0x1402190b),
+ STFM1000_REG(INITIALIZATION4, 0x525bf052),
+ STFM1000_REG(INITIALIZATION5, 0x7000d106),
+ STFM1000_REG(INITIALIZATION6, 0x0000c2cb),
+ STFM1000_REG(AGC_CONTROL1, 0x002c8402),
+ STFM1000_REG(AGC_CONTROL2, 0x00140050),
+ STFM1000_REG(CLK1, 0x10000000),
+ STFM1000_REG(CLK1, 0x20000000),
+ STFM1000_REG(CLK1, 0x00000000),
+ STFM1000_REG(CLK2, 0x7f000000),
+ STFM1000_REG(REF, 0x0030222D),
+ STFM1000_REG(CLK1, 0x30000000),
+ STFM1000_REG(CLK1, 0x30002000),
+ STFM1000_REG(CLK1, 0x10002000),
+ STFM1000_REG(LNA, 0x05080009),
+ STFM1000_REG(MIXFILT, 0x00008000),
+ STFM1000_REG(MIXFILT, 0x00000000),
+ STFM1000_REG(MIXFILT, 0x00007200),
+ STFM1000_REG(ADC, 0x00033000),
+ STFM1000_REG(ATTENTION, 0x0000003F),
+ STFM1000_END,
+};
+
+static const struct stfm1000_reg stfm1000_powerdown[] = {
+ STFM1000_REG(DATAPATH, 0x00010210),
+ STFM1000_REG(REF, 0),
+ STFM1000_REG(LNA, 0),
+ STFM1000_REG(MIXFILT, 0),
+ STFM1000_REG(CLK1, 0x20000000),
+ STFM1000_REG(CLK1, 0),
+ STFM1000_REG(CLK2, 0),
+ STFM1000_REG(ADC, 0),
+ STFM1000_REG(TUNE1, 0),
+ STFM1000_REG(SDNOMINAL, 0),
+ STFM1000_REG(PILOTTRACKING, 0),
+ STFM1000_REG(INITIALIZATION1, 0),
+ STFM1000_REG(INITIALIZATION2, 0),
+ STFM1000_REG(INITIALIZATION3, 0),
+ STFM1000_REG(INITIALIZATION4, 0),
+ STFM1000_REG(INITIALIZATION5, 0),
+ STFM1000_REG(INITIALIZATION6, 0x00007E00),
+ STFM1000_REG(AGC_CONTROL1, 0),
+ STFM1000_REG(AGC_CONTROL2, 0),
+ STFM1000_REG(DATAPATH, 0x00000200),
+};
+
+struct stfm1000_tuner_pmi {
+ u32 min;
+ u32 max;
+ u32 freq;
+ u32 pll_xtal; /* 1 = pll, 0 = xtal */
+};
+
+#define PLL 1
+#define XTAL 0
+
+static const struct stfm1000_tuner_pmi stfm1000_pmi_lookup[] = {
+ { .min = 76100, .max = 76500, .freq = 19200, .pll_xtal = PLL },
+ { .min = 79700, .max = 79900, .freq = 19200, .pll_xtal = PLL },
+ { .min = 80800, .max = 81200, .freq = 19200, .pll_xtal = PLL },
+ { .min = 82100, .max = 82600, .freq = 19200, .pll_xtal = PLL },
+ { .min = 86800, .max = 87200, .freq = 19200, .pll_xtal = PLL },
+ { .min = 88100, .max = 88600, .freq = 19200, .pll_xtal = PLL },
+ { .min = 89800, .max = 90500, .freq = 19200, .pll_xtal = PLL },
+ { .min = 91400, .max = 91900, .freq = 19200, .pll_xtal = PLL },
+ { .min = 92800, .max = 93300, .freq = 19200, .pll_xtal = PLL },
+ { .min = 97400, .max = 97900, .freq = 19200, .pll_xtal = PLL },
+ { .min = 98800, .max = 99200, .freq = 19200, .pll_xtal = PLL },
+ { .min = 100200, .max = 100400, .freq = 19200, .pll_xtal = PLL },
+ { .min = 103500, .max = 103900, .freq = 19200, .pll_xtal = PLL },
+ { .min = 104800, .max = 105200, .freq = 19200, .pll_xtal = PLL },
+ { .min = 106100, .max = 106500, .freq = 19200, .pll_xtal = PLL },
+
+ { .min = 76600, .max = 77000, .freq = 20000, .pll_xtal = PLL },
+ { .min = 77800, .max = 78300, .freq = 20000, .pll_xtal = PLL },
+ { .min = 79200, .max = 79600, .freq = 20000, .pll_xtal = PLL },
+ { .min = 80600, .max = 80700, .freq = 20000, .pll_xtal = PLL },
+ { .min = 83900, .max = 84400, .freq = 20000, .pll_xtal = PLL },
+ { .min = 85300, .max = 85800, .freq = 20000, .pll_xtal = PLL },
+ { .min = 94200, .max = 94700, .freq = 20000, .pll_xtal = PLL },
+ { .min = 95600, .max = 96100, .freq = 20000, .pll_xtal = PLL },
+ { .min = 100500, .max = 100800, .freq = 20000, .pll_xtal = PLL },
+ { .min = 101800, .max = 102200, .freq = 20000, .pll_xtal = PLL },
+ { .min = 103100, .max = 103400, .freq = 20000, .pll_xtal = PLL },
+ { .min = 106600, .max = 106900, .freq = 20000, .pll_xtal = PLL },
+ { .min = 107800, .max = 108000, .freq = 20000, .pll_xtal = PLL },
+
+ { .min = 0, .max = 0, .freq = 24000, .pll_xtal = XTAL }
+};
+
+int stfm1000_power_up(struct stfm1000 *stfm1000)
+{
+ struct stfm1000_reg *reg, *pwrup_reg;
+ const struct stfm1000_reg *orig_reg, *treg;
+ int ret, size;
+
+ mutex_lock(&stfm1000->state_lock);
+
+ /* Enable DRI clock for 24Mhz. */
+ HW_CLKCTRL_XTAL_CLR(BM_CLKCTRL_XTAL_DRI_CLK24M_GATE);
+
+ orig_reg = stfm1000->revid == STFM1000_CHIP_REV_TA2 ?
+ stfm1000_ta2_powerup : stfm1000_tb2_powerup;
+
+ /* find size of the set */
+ for (treg = orig_reg; treg->regno != STFM1000_REG_END; treg++)
+ ;
+ size = (treg + 1 - orig_reg) * sizeof(*treg);
+
+ /* allocate copy */
+ pwrup_reg = kmalloc(size, GFP_KERNEL);
+ if (pwrup_reg == NULL) {
+ printk(KERN_ERR "%s: out of memory\n", __func__);
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ /* copy it */
+ memcpy(pwrup_reg, orig_reg, size);
+
+ /* fixup region of INITILIZATION2 */
+ for (reg = pwrup_reg; reg->regno != STFM1000_REG_END; reg++) {
+
+ /* we only care for INITIALIZATION2 register */
+ if (reg->regno != STFM1000_INITIALIZATION2)
+ continue;
+
+ /* geographic region select */
+ if (stfm1000->georegion == 0) /* USA */
+ reg->value &= ~STFM1000_DEEMPH_50_75B;
+ else /* Europe */
+ reg->value |= STFM1000_DEEMPH_50_75B;
+
+ /* RDS enabled */
+ if (stfm1000->revid == STFM1000_CHIP_REV_TB2) {
+ if (stfm1000->rds_enable)
+ reg->value |= STFM1000_RDS_ENABLE;
+ else
+ reg->value &= ~STFM1000_RDS_ENABLE;
+ }
+ }
+
+ ret = stfm1000_write_regs(stfm1000, pwrup_reg);
+
+ kfree(pwrup_reg);
+out:
+ mutex_unlock(&stfm1000->state_lock);
+
+ return ret;
+}
+
+int stfm1000_power_down(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ mutex_lock(&stfm1000->state_lock);
+
+ /* Disable DRI clock for 24Mhz. */
+ HW_CLKCTRL_XTAL_CLR(BM_CLKCTRL_XTAL_DRI_CLK24M_GATE);
+
+ ret = stfm1000_write_regs(stfm1000, stfm1000_powerdown);
+
+ /* Disable DRI clock for 24Mhz. */
+ /* XXX bug warning, disabling the DRI clock is bad news */
+ /* doing so causes noise to be received from the DRI */
+ /* interface. Leave it on for now */
+ /* HW_CLKCTRL_XTAL_CLR(BM_CLKCTRL_XTAL_DRI_CLK24M_GATE); */
+
+ mutex_unlock(&stfm1000->state_lock);
+
+ return ret;
+}
+
+int stfm1000_dcdc_update(struct stfm1000 *stfm1000, u32 freq)
+{
+ const struct stfm1000_tuner_pmi *pmi;
+ int i;
+
+ /* search for DCDC frequency */
+ pmi = stfm1000_pmi_lookup;
+ for (i = 0; i < ARRAY_SIZE(stfm1000_pmi_lookup); i++, pmi++) {
+ if (freq >= pmi->min && freq <= pmi->max)
+ break;
+ }
+ if (i >= ARRAY_SIZE(stfm1000_pmi_lookup))
+ return -1;
+
+ /* adjust DCDC frequency so that it is out of Tuner PLL range */
+ /* XXX there is no adjustment API (os_pmi_SetDcdcFreq)*/
+ return 0;
+}
+
+static void Mute_Audio(struct stfm1000 *stfm1000)
+{
+ stfm1000->mute = 1;
+}
+
+static void Unmute_Audio(struct stfm1000 *stfm1000)
+{
+ stfm1000->mute = 0;
+}
+
+static const struct stfm1000_reg sd_dp_on_regs[] = {
+ STFM1000_REG_SETBITS(DATAPATH, STFM1000_DP_EN),
+ STFM1000_DELAY(3),
+ STFM1000_REG_SETBITS(DATAPATH, STFM1000_DB_ACCEPT),
+ STFM1000_REG_CLRBITS(AGC_CONTROL1, STFM1000_B2_BYPASS_AGC_CTL),
+ STFM1000_REG_CLRBITS(DATAPATH, STFM1000_DB_ACCEPT),
+ STFM1000_END,
+};
+
+static int SD_DP_On(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ ret = stfm1000_write_regs(stfm1000, sd_dp_on_regs);
+ if (ret != 0)
+ return ret;
+
+ return 0;
+}
+
+static const struct stfm1000_reg sd_dp_off_regs[] = {
+ STFM1000_REG_SETBITS(DATAPATH, STFM1000_DB_ACCEPT),
+ STFM1000_REG_CLRBITS(DATAPATH, STFM1000_DP_EN),
+ STFM1000_REG_SETBITS(AGC_CONTROL1, STFM1000_B2_BYPASS_AGC_CTL),
+ STFM1000_REG_CLRBITS(PILOTTRACKING, STFM1000_B2_PILOTTRACKING_EN),
+ STFM1000_REG_CLRBITS(DATAPATH, STFM1000_DB_ACCEPT),
+ STFM1000_END,
+};
+
+static int SD_DP_Off(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ ret = stfm1000_write_regs(stfm1000, sd_dp_off_regs);
+ if (ret != 0)
+ return ret;
+
+ return 0;
+}
+
+static int DRI_Start_Stream(struct stfm1000 *stfm1000)
+{
+ dma_addr_t dma_buffer_phys;
+ int i, next;
+ u32 cmd;
+
+ /* we must not be gated */
+ BUG_ON(HW_CLKCTRL_XTAL_RD() & BM_CLKCTRL_XTAL_DRI_CLK24M_GATE);
+
+ /* hw_dri_SetReset */
+ HW_DRI_CTRL_CLR(BM_DRI_CTRL_SFTRST | BM_DRI_CTRL_CLKGATE);
+ HW_DRI_CTRL_SET(BM_DRI_CTRL_SFTRST);
+ while ((HW_DRI_CTRL_RD() & BM_DRI_CTRL_CLKGATE) == 0)
+ cpu_relax();
+ HW_DRI_CTRL_CLR(BM_DRI_CTRL_SFTRST | BM_DRI_CTRL_CLKGATE);
+
+ /* DRI enable/config */
+ HW_DRI_TIMING_WR(BF_DRI_TIMING_GAP_DETECTION_INTERVAL(0x10) |
+ BF_DRI_TIMING_PILOT_REP_RATE(0x08));
+
+ /* XXX SDK bug */
+ /* While the SDK enables the gate here, everytime the stream */
+ /* is started, doing so, causes the DRI to input audio noise */
+ /* at any subsequent starts */
+ /* Enable DRI clock for 24Mhz. */
+ /* HW_CLKCTRL_XTAL_CLR(BM_CLKCTRL_XTAL_DRI_CLK24M_GATE); */
+
+ stmp3xxx_arch_dma_reset_channel(stfm1000->dma_ch);
+
+ dma_buffer_phys = stfm1000->dri_phys;
+
+ for (i = 0; i < stfm1000->blocks; i++) {
+ next = (i + 1) % stfm1000->blocks;
+
+ /* link */
+ stfm1000->dma[i].command->next = stfm1000->dma[next].handle;
+ stfm1000->dma[i].next_descr = &stfm1000->dma[next];
+
+ /* receive DRI is 8 bytes per 4 samples */
+ cmd = BF_APBX_CHn_CMD_XFER_COUNT(stfm1000->blksize * 2) |
+ BM_APBX_CHn_CMD_IRQONCMPLT |
+ BM_APBX_CHn_CMD_CHAIN |
+ BF_APBX_CHn_CMD_COMMAND(
+ BV_APBX_CHn_CMD_COMMAND__DMA_WRITE);
+
+ stfm1000->dma[i].command->cmd = cmd;
+ stfm1000->dma[i].command->buf_ptr = dma_buffer_phys;
+ stfm1000->dma[i].command->pio_words[0] =
+ BM_DRI_CTRL_OVERFLOW_IRQ_EN |
+ BM_DRI_CTRL_PILOT_SYNC_LOSS_IRQ_EN |
+ BM_DRI_CTRL_ATTENTION_IRQ_EN |
+ /* BM_DRI_CTRL_STOP_ON_OFLOW_ERROR | */
+ /* BM_DRI_CTRL_STOP_ON_PILOT_ERROR | */
+ BM_DRI_CTRL_ENABLE_INPUTS;
+
+ dma_buffer_phys += stfm1000->blksize * 2;
+
+ }
+
+ /* Enable completion interrupt */
+ stmp3xxx_dma_clear_interrupt(stfm1000->dma_ch);
+ stmp3xxx_dma_enable_interrupt(stfm1000->dma_ch);
+
+ /* clear DRI interrupts pending */
+ HW_DRI_CTRL_CLR(BM_DRI_CTRL_OVERFLOW_IRQ |
+ BM_DRI_CTRL_PILOT_SYNC_LOSS_IRQ |
+ BM_DRI_CTRL_ATTENTION_IRQ);
+
+ /* Stop DRI on error */
+ HW_DRI_CTRL_CLR(BM_DRI_CTRL_STOP_ON_OFLOW_ERROR |
+ BM_DRI_CTRL_STOP_ON_PILOT_ERROR);
+
+ /* Reacquire data stream */
+ HW_DRI_CTRL_SET(BM_DRI_CTRL_REACQUIRE_PHASE |
+ BM_DRI_CTRL_OVERFLOW_IRQ_EN |
+ BM_DRI_CTRL_PILOT_SYNC_LOSS_IRQ_EN |
+ BM_DRI_CTRL_ATTENTION_IRQ_EN |
+ BM_DRI_CTRL_ENABLE_INPUTS);
+
+ stmp3xxx_dma_go(stfm1000->dma_ch, stfm1000->dma, 1);
+
+ /* Turn on DRI hardware (don't forget to leave RUN bit ON) */
+ HW_DRI_CTRL_SET(BM_DRI_CTRL_RUN);
+
+ return 0;
+}
+
+static int DRI_Stop_Stream(struct stfm1000 *stfm1000)
+{
+ int desc;
+
+ /* disable interrupts */
+ HW_DRI_CTRL_CLR(BM_DRI_CTRL_OVERFLOW_IRQ_EN |
+ BM_DRI_CTRL_PILOT_SYNC_LOSS_IRQ_EN |
+ BM_DRI_CTRL_ATTENTION_IRQ_EN);
+
+ /* Freeze DMA channel for a moment */
+ stmp3xxx_dma_freeze(stfm1000->dma_ch);
+
+ /* all descriptors, set sema bit */
+ for (desc = 0; desc < stfm1000->blocks; desc++)
+ stfm1000->dma[desc].command->cmd |= BM_APBX_CHn_CMD_SEMAPHORE;
+
+ /* Let the current DMA transaction finish */
+ stmp3xxx_dma_unfreeze(stfm1000->dma_ch);
+ msleep(5);
+
+ /* dma shutdown */
+ stmp3xxx_arch_dma_reset_channel(stfm1000->dma_ch);
+
+ /* Turn OFF data lines and stop controller */
+ HW_DRI_CTRL_CLR(BM_DRI_CTRL_ENABLE_INPUTS | BM_DRI_CTRL_RUN);
+
+ /* hw_dri_SetReset */
+ HW_DRI_CTRL_SET(BM_DRI_CTRL_SFTRST | BM_DRI_CTRL_CLKGATE);
+
+ /* XXX SDK bug */
+ /* While the SDK enables the gate here, everytime the stream */
+ /* is started, doing so, causes the DRI to input audio noise */
+ /* at any subsequent starts */
+ /* Enable DRI clock for 24Mhz. */
+ /* Disable DRI clock for 24Mhz. */
+ /* HW_CLKCTRL_XTAL_SET(BM_CLKCTRL_XTAL_DRI_CLK24M_GATE); */
+
+ return 0;
+}
+
+static int DRI_On(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ if (stfm1000->active)
+ DRI_Start_Stream(stfm1000);
+
+ ret = stfm1000_set_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_SAI_EN);
+ return ret;
+}
+
+static int DRI_Off(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ if (stfm1000->active)
+ DRI_Stop_Stream(stfm1000);
+
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_SAI_EN);
+
+ return 0;
+}
+
+static int SD_Set_Channel_Filter(struct stfm1000 *stfm1000)
+{
+ int bypass_setting;
+ int sig_qual;
+ u32 tmp;
+ int ret;
+
+ /*
+ * set channel filter
+ *
+ * B2_NEAR_CHAN_MIX_REG_MASK values from T-Spec
+ * 000 : 0 kHz mix.
+ * 001 : +100 kHz mix.
+ * 010 : +200 kHz mix.
+ * 011 : +300 kHz mix.
+ * 100 : -400 kHz mix.
+ * 101 : -300 kHz mix.
+ * 110 : -200 kHz mix.
+ * 111 : -100 kHz mix.
+ */
+
+ /* get near channel amplitude */
+ ret = stfm1000_write_masked(stfm1000, STFM1000_INITIALIZATION3,
+ STFM1000_B2_NEAR_CHAN_MIX(0x01),
+ STFM1000_B2_NEAR_CHAN_MIX_MASK);
+ if (ret != 0)
+ return ret;
+
+ msleep(10); /* wait for the signal quality to settle */
+
+ ret = stfm1000_read(stfm1000, STFM1000_SIGNALQUALITY, &tmp);
+ if (ret != 0)
+ return ret;
+
+ sig_qual = (tmp & STFM1000_NEAR_CHAN_AMPLITUDE_MASK) >>
+ STFM1000_NEAR_CHAN_AMPLITUDE_SHIFT;
+
+ bypass_setting = 0;
+
+ /* check near channel amplitude vs threshold */
+ if (sig_qual < stfm1000->adj_chan_th) {
+ /* get near channel amplitude again */
+ ret = stfm1000_write_masked(stfm1000, STFM1000_INITIALIZATION3,
+ STFM1000_B2_NEAR_CHAN_MIX(0x05),
+ STFM1000_B2_NEAR_CHAN_MIX_MASK);
+ if (ret != 0)
+ return ret;
+
+ msleep(10); /* wait for the signal quality to settle */
+
+ ret = stfm1000_read(stfm1000, STFM1000_SIGNALQUALITY, &tmp);
+ if (ret != 0)
+ return ret;
+
+ sig_qual = (tmp & STFM1000_NEAR_CHAN_AMPLITUDE_MASK) >>
+ STFM1000_NEAR_CHAN_AMPLITUDE_SHIFT;
+
+ if (sig_qual < stfm1000->adj_chan_th)
+ bypass_setting = 2;
+ }
+
+ /* set filter settings */
+ ret = stfm1000_write_masked(stfm1000, STFM1000_INITIALIZATION1,
+ STFM1000_B2_BYPASS_FILT(bypass_setting),
+ STFM1000_B2_BYPASS_FILT_MASK);
+ if (ret != 0)
+ return ret;
+
+ return 0;
+}
+
+static int SD_Look_For_Pilot_TA2(struct stfm1000 *stfm1000)
+{
+ int i;
+ u32 pilot;
+ int ret;
+
+ /* assume pilot */
+ stfm1000->pilot_present = 1;
+
+ for (i = 0; i < 3; i++) {
+
+ ret = stfm1000_read(stfm1000, STFM1000_PILOTCORRECTION,
+ &pilot);
+ if (ret != 0)
+ return ret;
+
+ pilot &= STFM1000_PILOTEST_TA2_MASK;
+ pilot >>= STFM1000_PILOTEST_TA2_SHIFT;
+
+ /* out of range? */
+ if (pilot < 0xe2 || pilot >= 0xb5) {
+ stfm1000->pilot_present = 0;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+
+static int SD_Look_For_Pilot_TB2(struct stfm1000 *stfm1000)
+{
+ int i;
+ u32 pilot;
+ int ret;
+
+ /* assume pilot */
+ stfm1000->pilot_present = 1;
+
+ for (i = 0; i < 3; i++) {
+
+ ret = stfm1000_read(stfm1000, STFM1000_PILOTCORRECTION,
+ &pilot);
+ if (ret != 0)
+ return ret;
+
+ pilot &= STFM1000_PILOTEST_TB2_MASK;
+ pilot >>= STFM1000_PILOTEST_TB2_SHIFT;
+
+ /* out of range? */
+ if (pilot < 0x1e || pilot >= 0x7f) {
+ stfm1000->pilot_present = 0;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int SD_Look_For_Pilot(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ if (stfm1000->revid == STFM1000_CHIP_REV_TA2)
+ ret = SD_Look_For_Pilot_TA2(stfm1000);
+ else
+ ret = SD_Look_For_Pilot_TB2(stfm1000);
+
+ if (ret != 0)
+ return ret;
+
+ if (!stfm1000->pilot_present) {
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_PILOTTRACKING,
+ STFM1000_B2_PILOTTRACKING_EN);
+ if (ret != 0)
+ return ret;
+
+ /* set force mono parameters for the filter */
+ stfm1000->filter_parms.pCoefForcedMono = 1;
+
+ /* yeah, I know, it's stupid */
+ stfm1000->rds_state.demod.pCoefForcedMono =
+ stfm1000->filter_parms.pCoefForcedMono;
+ }
+
+ return 0;
+}
+
+static int SD_Gear_Shift_Pilot_Tracking(struct stfm1000 *stfm1000)
+{
+ static const struct {
+ int delay;
+ u32 value;
+ } track_table[] = {
+ { .delay = 10, .value = 0x81b6 },
+ { .delay = 6, .value = 0x82a5 },
+ { .delay = 6, .value = 0x8395 },
+ { .delay = 8, .value = 0x8474 },
+ { .delay = 20, .value = 0x8535 },
+ { .delay = 50, .value = 0x8632 },
+ { .delay = 0, .value = 0x8810 },
+ };
+ int i;
+ int ret;
+
+ for (i = 0; i < ARRAY_SIZE(track_table); i++) {
+ ret = stfm1000_write(stfm1000, STFM1000_PILOTTRACKING,
+ track_table[i].value);
+ if (ret != 0)
+ return ret;
+
+ if (i < ARRAY_SIZE(track_table) - 1) /* last one no delay */
+ msleep(track_table[i].delay);
+ }
+
+ return 0;
+}
+
+static int SD_Optimize_Channel(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ ret = stfm1000_set_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_DB_ACCEPT);
+ if (ret != 0)
+ return ret;
+
+ ret = stfm1000_write(stfm1000, STFM1000_PILOTTRACKING,
+ STFM1000_B2_PILOTTRACKING_EN |
+ STFM1000_B2_PILOTLPF_TIMECONSTANT(0x01) |
+ STFM1000_B2_PFDSCALE(0x0B) |
+ STFM1000_B2_PFDFILTER_SPEEDUP(0x06)); /* 0x000081B6 */
+ if (ret != 0)
+ return ret;
+
+ ret = SD_Set_Channel_Filter(stfm1000);
+ if (ret != 0)
+ return ret;
+
+ ret = SD_Look_For_Pilot(stfm1000);
+ if (ret != 0)
+ return ret;
+
+ if (stfm1000->pilot_present) {
+ ret = SD_Gear_Shift_Pilot_Tracking(stfm1000);
+ if (ret != 0)
+ return ret;
+ }
+
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_DB_ACCEPT);
+ if (ret != 0)
+ return ret;
+
+ return 0;
+}
+
+static int Monitor_STFM_Quality(struct stfm1000 *stfm1000)
+{
+ u32 tmp, rssi_dc_est, tone_data;
+ u32 lna_rms, bias, agc_out, lna_th, lna, ref;
+ u16 rssi_mantissa, rssi_exponent, rssi_decoded;
+ u16 prssi;
+ s16 mpx_dc;
+ int rssi_log;
+ int bypass_filter;
+ int ret;
+
+ ret = stfm1000_set_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_DB_ACCEPT);
+ if (ret != 0)
+ return ret;
+
+ /* Get Rssi register readings from STFM1000 */
+ stfm1000_read(stfm1000, STFM1000_RSSI_TONE, &tmp);
+ rssi_dc_est = tmp & 0xffff;
+ tone_data = (tmp >> 16) & 0x0fff;
+
+ rssi_mantissa = (rssi_dc_est & 0xffe0) >> 5; /* 11Msb */
+ rssi_exponent = rssi_dc_est & 0x001f; /* 5 lsb */
+ rssi_decoded = (u32)rssi_mantissa << rssi_exponent;
+
+ /* Convert Rsst to 10log(Rssi) */
+ for (prssi = 20; prssi > 0; prssi--)
+ if (rssi_decoded >= (1 << prssi))
+ break;
+
+ rssi_log = (3 * rssi_decoded >> prssi) + (3 * prssi - 3);
+ /* clamp to positive */
+ if (rssi_log < 0)
+ rssi_log = 0;
+ /* Compensate for errors in truncation/approximation by adding 1 */
+ rssi_log++;
+
+ stfm1000->rssi_dc_est_log = rssi_log;
+ stfm1000->signal_strength = stfm1000->rssi_dc_est_log;
+
+ /* determine absolute value */
+ if (tmp & 0x0800)
+ mpx_dc = ((tmp >> 16) & 0x0fff) | 0xf000;
+ else
+ mpx_dc = (tmp >> 16) & 0x0fff;
+ stfm1000->mpx_dc = mpx_dc;
+ mpx_dc = mpx_dc < 0 ? -mpx_dc : mpx_dc;
+
+ if (stfm1000->tuning_grid_50KHz)
+ stfm1000->is_station = rssi_log > stfm1000->tune_rssi_th;
+ else
+ stfm1000->is_station = rssi_log > stfm1000->tune_rssi_th &&
+ mpx_dc > stfm1000->tune_mpx_dc_th;
+
+ /* weak signal? */
+ if (stfm1000->rssi_dc_est_log <
+ (stfm1000->filter_parms.pCoefLmrGaTh - 20)) {
+
+ if (stfm1000->pilot_present)
+ bypass_filter = 1; /* Filter settings #2 */
+ else
+ bypass_filter = 0;
+
+ /* configure filter for narrow band */
+ ret = stfm1000_write_masked(stfm1000, STFM1000_AGC_CONTROL1,
+ STFM1000_B2_BYPASS_FILT(bypass_filter),
+ STFM1000_B2_BYPASS_FILT_MASK);
+ if (ret != 0)
+ return ret;
+
+ /* Turn off pilot tracking */
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_PILOTTRACKING,
+ STFM1000_B2_PILOTTRACKING_EN);
+ if (ret != 0)
+ return ret;
+
+ /* enable "forced mono" in black box */
+ stfm1000->filter_parms.pCoefForcedMono = 1;
+
+ /* yeah, I know, it's stupid */
+ stfm1000->rds_state.demod.pCoefForcedMono =
+ stfm1000->filter_parms.pCoefForcedMono;
+
+ /* Set weak signal flag */
+ stfm1000->weak_signal = 1;
+
+ if (stfm1000->revid == STFM1000_CHIP_REV_TA2) {
+
+ /* read AGC_STAT register */
+ ret = stfm1000_read(stfm1000, STFM1000_AGC_STAT, &tmp);
+ if (ret != 0)
+ return ret;
+
+ lna_rms = (tmp & STFM1000_LNA_RMS_MASK) >>
+ STFM1000_LNA_RMS_SHIFT;
+
+ /* Check the energy level from LNA Power Meter A/D */
+ if (lna_rms == 0)
+ bias = STFM1000_IBIAS2_DN | STFM1000_IBIAS1_UP;
+ else
+ bias = STFM1000_IBIAS2_UP | STFM1000_IBIAS1_DN;
+
+ if (lna_rms == 0 || lna_rms > 2) {
+ ret = stfm1000_write_masked(stfm1000,
+ STFM1000_LNA, bias,
+ STFM1000_IBIAS2_UP |
+ STFM1000_IBIAS2_DN |
+ STFM1000_IBIAS1_UP |
+ STFM1000_IBIAS1_DN);
+ if (ret != 0)
+ return ret;
+ }
+
+ } else {
+
+ /* Set LNA bias */
+
+ /* read AGC_STAT register */
+ ret = stfm1000_read(stfm1000, STFM1000_AGC_STAT, &tmp);
+ if (ret != 0)
+ return ret;
+
+ agc_out = (tmp & STFM1000_AGCOUT_STAT_MASK) >>
+ STFM1000_AGCOUT_STAT_SHIFT;
+
+ /* read LNA register (this is a cached register) */
+ ret = stfm1000_read(stfm1000, STFM1000_LNA, &lna);
+ if (ret != 0)
+ return ret;
+
+ /* read REF register (this is a cached register) */
+ ret = stfm1000_read(stfm1000, STFM1000_REF, &ref);
+ if (ret != 0)
+ return ret;
+
+/* work around the 80 line width problem */
+#undef LNADEF
+#define LNADEF STFM1000_LNA_AMP1_IMPROVE_DISTORTION
+ if (agc_out == 31) {
+ if (rssi_log <= 16) {
+ if (lna & STFM1000_IBIAS1_DN)
+ lna &= ~STFM1000_IBIAS1_DN;
+ else {
+ lna |= STFM1000_IBIAS1_UP;
+ ref &= ~LNADEF;
+ }
+ }
+ if (rssi_log >= 26) {
+ if (lna & STFM1000_IBIAS1_UP) {
+ lna &= ~STFM1000_IBIAS1_UP;
+ ref |= LNADEF;
+ } else
+ lna |= STFM1000_IBIAS1_DN;
+ }
+ } else {
+ lna &= ~STFM1000_IBIAS1_UP;
+ lna |= STFM1000_IBIAS1_DN;
+ ref |= LNADEF;
+ }
+#undef LNADEF
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_LNA,
+ lna, STFM1000_IBIAS1_UP | STFM1000_IBIAS1_DN);
+ if (ret != 0)
+ return ret;
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_REF,
+ ref, STFM1000_LNA_AMP1_IMPROVE_DISTORTION);
+ if (ret != 0)
+ return ret;
+ }
+
+ } else if (stfm1000->rssi_dc_est_log >
+ (stfm1000->filter_parms.pCoefLmrGaTh - 17)) {
+
+ bias = STFM1000_IBIAS2_UP | STFM1000_IBIAS1_DN;
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_LNA,
+ bias, STFM1000_IBIAS2_UP | STFM1000_IBIAS2_DN |
+ STFM1000_IBIAS1_UP | STFM1000_IBIAS1_DN);
+ if (ret != 0)
+ return ret;
+
+ ret = SD_Set_Channel_Filter(stfm1000);
+ if (ret != 0)
+ return ret;
+
+ ret = SD_Look_For_Pilot(stfm1000);
+ if (ret != 0)
+ return ret;
+
+ if (stfm1000->pilot_present) {
+ if (stfm1000->prev_pilot_present ||
+ stfm1000->weak_signal) {
+
+ /* gear shift pilot tracking */
+ ret = SD_Gear_Shift_Pilot_Tracking(
+ stfm1000);
+ if (ret != 0)
+ return ret;
+
+ /* set force mono parameters for the
+ * filter */
+ stfm1000->filter_parms.
+ pCoefForcedMono = stfm1000->
+ force_mono;
+
+ /* yeah, I know, it's stupid */
+ stfm1000->rds_state.demod.
+ pCoefForcedMono = stfm1000->
+ filter_parms.
+ pCoefForcedMono;
+ }
+ } else {
+ ret = stfm1000_clear_bits(stfm1000,
+ STFM1000_PILOTTRACKING,
+ STFM1000_B2_PILOTTRACKING_EN);
+ if (ret != 0)
+ return ret;
+
+ /* set force mono parameters for the filter */
+ stfm1000->filter_parms.pCoefForcedMono = 1;
+
+ /* yeah, I know, it's stupid */
+ stfm1000->rds_state.demod.pCoefForcedMono =
+ stfm1000->filter_parms.pCoefForcedMono;
+ }
+
+ /* Reset weak signal flag */
+ stfm1000->weak_signal = 0;
+ stfm1000->prev_pilot_present = stfm1000->pilot_present;
+
+ } else {
+
+ ret = SD_Look_For_Pilot(stfm1000);
+ if (ret != 0)
+ return ret;
+
+ if (!stfm1000->pilot_present) {
+ ret = stfm1000_clear_bits(stfm1000,
+ STFM1000_PILOTTRACKING,
+ STFM1000_B2_PILOTTRACKING_EN);
+ if (ret != 0)
+ return ret;
+
+ /* set force mono parameters for the filter */
+ stfm1000->filter_parms.pCoefForcedMono = 1;
+
+ /* yeah, I know, it's stupid */
+ stfm1000->rds_state.demod.pCoefForcedMono =
+ stfm1000->filter_parms.pCoefForcedMono;
+
+ /* Reset weak signal flag */
+ stfm1000->weak_signal = 0;
+ stfm1000->prev_pilot_present = stfm1000->pilot_present;
+ }
+
+ }
+
+ if (stfm1000->revid == STFM1000_CHIP_REV_TA2) {
+
+ /* read AGC_STAT register */
+ ret = stfm1000_read(stfm1000, STFM1000_AGC_STAT, &tmp);
+ if (ret != 0)
+ return ret;
+
+ agc_out = (tmp & STFM1000_AGCOUT_STAT_MASK) >>
+ STFM1000_AGCOUT_STAT_SHIFT;
+ lna_rms = (tmp & STFM1000_LNA_RMS_MASK) >>
+ STFM1000_LNA_RMS_SHIFT;
+
+ ret = stfm1000_read(stfm1000, STFM1000_AGC_CONTROL1, &tmp);
+ if (ret != 0)
+ return ret;
+
+ /* extract LNATH */
+ lna_th = (tmp & STFM1000_B2_LNATH_MASK) >>
+ STFM1000_B2_LNATH_SHIFT;
+
+ if (lna_rms > lna_th && agc_out <= 1) {
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_LNA,
+ STFM1000_USEATTEN(1), STFM1000_USEATTEN_MASK);
+ if (ret != 0)
+ return ret;
+
+ } else if (agc_out > 15) {
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_LNA,
+ STFM1000_USEATTEN(0), STFM1000_USEATTEN_MASK);
+ if (ret != 0)
+ return ret;
+ }
+ }
+
+ /* disable buffered writes */
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_DB_ACCEPT);
+ if (ret != 0)
+ return ret;
+
+ return ret;
+}
+
+static int Is_Station(struct stfm1000 *stfm1000)
+{
+ u32 tmp, rssi_dc_est, tone_data;
+ u16 rssi_mantissa, rssi_exponent, rssi_decoded;
+ u16 prssi;
+ s16 mpx_dc;
+ int rssi_log;
+
+ /* Get Rssi register readings from STFM1000 */
+ stfm1000_read(stfm1000, STFM1000_RSSI_TONE, &tmp);
+ rssi_dc_est = tmp & 0xffff;
+ tone_data = (tmp >> 16) & 0x0fff;
+
+ rssi_mantissa = (rssi_dc_est & 0xffe0) >> 5; /* 11Msb */
+ rssi_exponent = rssi_dc_est & 0x001f; /* 5 lsb */
+ rssi_decoded = (u32)rssi_mantissa << rssi_exponent;
+
+ /* Convert Rsst to 10log(Rssi) */
+ for (prssi = 20; prssi > 0; prssi--)
+ if (rssi_decoded >= (1 << prssi))
+ break;
+
+ rssi_log = (3 * rssi_decoded >> prssi) + (3 * prssi - 3);
+ /* clamp to positive */
+ if (rssi_log < 0)
+ rssi_log = 0;
+ /* Compensate for errors in truncation/approximation by adding 1 */
+ rssi_log++;
+
+ stfm1000->rssi_dc_est_log = rssi_log;
+ stfm1000->signal_strength = stfm1000->rssi_dc_est_log;
+
+ /* determine absolute value */
+ if (tmp & 0x0800)
+ mpx_dc = ((tmp >> 16) & 0x0fff) | 0xf000;
+ else
+ mpx_dc = (tmp >> 16) & 0x0fff;
+ stfm1000->mpx_dc = mpx_dc;
+ mpx_dc = mpx_dc < 0 ? -mpx_dc : mpx_dc;
+
+ if (stfm1000->tuning_grid_50KHz)
+ stfm1000->is_station = rssi_log > stfm1000->tune_rssi_th;
+ else
+ stfm1000->is_station = rssi_log > stfm1000->tune_rssi_th &&
+ mpx_dc > stfm1000->tune_mpx_dc_th;
+
+ return 0;
+}
+
+int Monitor_STFM_AGC(struct stfm1000 *stfm1000)
+{
+ /* we don't do any AGC for now */
+ return 0;
+}
+
+static int Take_Down(struct stfm1000 *stfm1000)
+{
+ Mute_Audio(stfm1000);
+
+ DRI_Off(stfm1000);
+
+ SD_DP_Off(stfm1000);
+
+ return 0;
+}
+
+static int Bring_Up(struct stfm1000 *stfm1000)
+{
+ SD_DP_On(stfm1000);
+
+ SD_Optimize_Channel(stfm1000);
+
+ DRI_On(stfm1000);
+
+ Unmute_Audio(stfm1000);
+
+ if (stfm1000->rds_enable)
+ stfm1000_rds_reset(&stfm1000->rds_state);
+
+ stfm1000->rds_sync = stfm1000->rds_enable; /* force sync (if RDS) */
+ stfm1000->rds_demod_running = 0;
+ stfm1000->rssi_dc_est_log = 0;
+ stfm1000->signal_strength = 0;
+
+ stfm1000->next_quality_monitor = jiffies + msecs_to_jiffies(
+ stfm1000->quality_monitor_period);
+ stfm1000->next_agc_monitor = jiffies + msecs_to_jiffies(
+ stfm1000->agc_monitor_period);
+ stfm1000->rds_pkt_bad = 0;
+ stfm1000->rds_pkt_good = 0;
+ stfm1000->rds_pkt_recovered = 0;
+ stfm1000->rds_pkt_lost_sync = 0;
+ stfm1000->rds_bit_overruns = 0;
+
+ return 0;
+}
+
+/* These are not used yet */
+
+static int Lock_Station(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ ret = SD_Optimize_Channel(stfm1000);
+ if (ret != 0)
+ return ret;
+
+ /* AGC monitor start? */
+
+ return ret;
+}
+
+static const struct stfm1000_reg sd_unlock_regs[] = {
+ STFM1000_REG_SETBITS(DATAPATH, STFM1000_DB_ACCEPT),
+ STFM1000_REG_CLRBITS(PILOTTRACKING, STFM1000_B2_PILOTTRACKING_EN),
+ STFM1000_REG_CLRBITS(DATAPATH, STFM1000_DB_ACCEPT),
+ STFM1000_END,
+};
+
+static int Unlock_Station(struct stfm1000 *stfm1000)
+{
+ int ret;
+
+ ret = stfm1000_write_regs(stfm1000, sd_unlock_regs);
+ return ret;
+}
+
+irqreturn_t stfm1000_dri_dma_irq(int irq, void *dev_id)
+{
+ struct stfm1000 *stfm1000 = dev_id;
+ u32 err_mask, irq_mask;
+ u32 ctrl;
+ int handled = 0;
+
+#ifdef CONFIG_ARCH_STMP37XX
+ err_mask = 1 << (16 + stfm1000->dma_ch);
+#endif
+#ifdef CONFIG_ARCH_STMP378X
+ err_mask = 1 << stfm1000->dma_ch;
+#endif
+ irq_mask = 1 << stfm1000->dma_ch;
+
+#ifdef CONFIG_ARCH_STMP37XX
+ ctrl = HW_APBX_CTRL1_RD();
+#endif
+#ifdef CONFIG_ARCH_STMP378X
+ ctrl = HW_APBX_CTRL2_RD();
+#endif
+
+ if (ctrl & err_mask) {
+ handled = 1;
+ printk(KERN_WARNING "%s: DMA audio channel %d error\n",
+ __func__, stfm1000->dma_ch);
+#ifdef CONFIG_ARCH_STMP37XX
+ HW_APBX_CTRL1_CLR(err_mask);
+#endif
+#ifdef CONFIG_ARCH_STMP378X
+ HW_APBX_CTRL2_CLR(err_mask);
+#endif
+ }
+
+ if (HW_APBX_CTRL1_RD() & irq_mask) {
+ handled = 1;
+ stmp3xxx_dma_clear_interrupt(stfm1000->dma_ch);
+
+ if (stfm1000->alsa_initialized) {
+ BUG_ON(stfm1000_alsa_ops->dma_irq == NULL);
+ (*stfm1000_alsa_ops->dma_irq)(stfm1000);
+ }
+ }
+
+ return handled ? IRQ_HANDLED : IRQ_NONE;
+}
+EXPORT_SYMBOL(stfm1000_dri_dma_irq);
+
+irqreturn_t stfm1000_dri_attn_irq(int irq, void *dev_id)
+{
+ struct stfm1000 *stfm1000 = dev_id;
+ int handled = 1;
+ u32 mask;
+
+ (void)stfm1000;
+ mask = HW_DRI_CTRL_RD();
+ mask &= BM_DRI_CTRL_OVERFLOW_IRQ | BM_DRI_CTRL_PILOT_SYNC_LOSS_IRQ |
+ BM_DRI_CTRL_ATTENTION_IRQ;
+
+ HW_DRI_CTRL_CLR(mask);
+
+ printk(KERN_INFO "DRI_ATTN:%s%s%s\n",
+ (mask & BM_DRI_CTRL_OVERFLOW_IRQ) ? " OV" : "",
+ (mask & BM_DRI_CTRL_PILOT_SYNC_LOSS_IRQ) ? " SL" : "",
+ (mask & BM_DRI_CTRL_ATTENTION_IRQ) ? " AT" : "");
+
+ if (stfm1000->alsa_initialized) {
+ BUG_ON(stfm1000_alsa_ops->attn_irq == NULL);
+ (*stfm1000_alsa_ops->attn_irq)(stfm1000);
+ }
+
+ return handled ? IRQ_HANDLED : IRQ_NONE;
+}
+EXPORT_SYMBOL(stfm1000_dri_attn_irq);
+
+void stfm1000_decode_block(struct stfm1000 *stfm1000, const s16 *src, s16 *dst,
+ int count)
+{
+ int i;
+
+ if (stfm1000->mute) {
+ memset(dst, 0, count * sizeof(s16) * 2);
+ return;
+
+ }
+
+ for (i = 0; i < count; i++, dst += 2, src += 4) {
+
+ stfm1000_filter_decode(&stfm1000->filter_parms,
+ src[0], src[1], src[2]);
+
+ dst[0] = stfm1000_filter_value_left(&stfm1000->filter_parms);
+ dst[1] = stfm1000_filter_value_right(&stfm1000->filter_parms);
+ }
+
+ stfm1000->rssi = stfm1000->filter_parms.RssiDecoded;
+ stfm1000->stereo = stfm1000->pilot_present &&
+ !stfm1000->filter_parms.pCoefForcedMono;
+
+ /* RDS processing */
+ if (stfm1000->rds_demod_running) {
+ /* rewind */
+ src -= count * 4;
+ stfm1000_rds_demod(&stfm1000->rds_state, src, count);
+ }
+
+}
+EXPORT_SYMBOL(stfm1000_decode_block);
+
+void stfm1000_take_down(struct stfm1000 *stfm1000)
+{
+ mutex_lock(&stfm1000->state_lock);
+ stfm1000->active = 0;
+ Take_Down(stfm1000);
+ mutex_unlock(&stfm1000->state_lock);
+}
+EXPORT_SYMBOL(stfm1000_take_down);
+
+void stfm1000_bring_up(struct stfm1000 *stfm1000)
+{
+ mutex_lock(&stfm1000->state_lock);
+
+ stfm1000->active = 1;
+
+ stfm1000_filter_reset(&stfm1000->filter_parms);
+
+ Bring_Up(stfm1000);
+
+ mutex_unlock(&stfm1000->state_lock);
+}
+EXPORT_SYMBOL(stfm1000_bring_up);
+
+void stfm1000_tune_current(struct stfm1000 *stfm1000)
+{
+ mutex_lock(&stfm1000->state_lock);
+ sw_tune(stfm1000, stfm1000->freq);
+ mutex_unlock(&stfm1000->state_lock);
+}
+EXPORT_SYMBOL(stfm1000_tune_current);
+
+/* Alternate ZIF Tunings to avoid EMI */
+const struct stfm1000_tune1
+stfm1000_board_emi_tuneups[STFM1000_FREQUENCY_100KHZ_RANGE] = {
+#undef TUNE_ENTRY
+#define TUNE_ENTRY(f, t1, sd) \
+ [(f) - STFM1000_FREQUENCY_100KHZ_MIN] = \
+ { .tune1 = (t1), .sdnom = (sd) }
+ TUNE_ENTRY(765, 0x84030, 0x1BF5E50D), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(780, 0x84240, 0x1BA5162F), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(795, 0x84250, 0x1C2D2F39), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(810, 0x84460, 0x1BDD207E), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(825, 0x84470, 0x1C6138CD), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(839, 0xC4680, 0x1C11F704), /* 061215 Jon, IF +100kHz */
+ TUNE_ENTRY(840, 0x84680, 0x1c11f704),
+ TUNE_ENTRY(855, 0x84890, 0x1BC71C71), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(870, 0x848A0, 0x1C43DE10), /* 061215 Jon, IF +0kHz */
+ TUNE_ENTRY(885, 0x84AB0, 0x1BF9B021), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(899, 0xC4CC0, 0x1BB369A9), /* 061025 Arthur, IF +100kHz */
+ TUNE_ENTRY(900, 0x84CC0, 0x1BB369A9), /* 061025 Arthur, IF 0kHz */
+ TUNE_ENTRY(915, 0x84CD0, 0x1C299A5B), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(930, 0x84ee0, 0x1be3e6aa), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(945, 0x84ef0, 0x1c570f8b), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(959, 0xC5100, 0x1c11f704),
+ TUNE_ENTRY(960, 0x85100, 0x1c11f704),
+ TUNE_ENTRY(975, 0x85310, 0x1bd03d57), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(990, 0x85320, 0x1c3dc822), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(1005, 0x85530, 0x1bfc93ff), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(1019, 0xC5740, 0x1BBE683C), /* 061025 Arthur, IF +100kHz */
+ TUNE_ENTRY(1020, 0x85740, 0x1bbe683c), /* 061025 Arthur, IF +0kHz */
+ TUNE_ENTRY(1035, 0x85750, 0x1c26dab6), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(1050, 0x85960, 0x1be922b4), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(1065, 0x85970, 0x1c4f357c), /* 061101 Arthur, IF +0kHz */
+ TUNE_ENTRY(1079, 0xC5B80, 0x1c11f704),
+ TUNE_ENTRY(1080, 0x85B80, 0x1c11f704),
+#undef TUNE_ENTRY
+};
+
+static const struct stfm1000_tune1 *stfm1000_board_emi_tune(int freq100)
+{
+ const struct stfm1000_tune1 *tune1;
+
+ if ((unsigned int)(freq100 - STFM1000_FREQUENCY_100KHZ_MIN) >=
+ STFM1000_FREQUENCY_100KHZ_RANGE)
+ return NULL;
+
+ tune1 = &stfm1000_board_emi_tuneups[freq100 -
+ STFM1000_FREQUENCY_100KHZ_MIN];
+ if (tune1->tune1 == 0 && tune1->sdnom == 0)
+ return NULL;
+ return tune1;
+}
+
+/* freq in kHz */
+static int sw_tune(struct stfm1000 *stfm1000, u32 freq)
+{
+ u32 freq100 = freq / 100;
+ int tune_cap;
+ int i2s_clock;
+ int mix_reg;
+ int if_freq, fe_freq;
+ u32 tune1, sdnom, agc1;
+ const struct stfm1000_tune1 *tp;
+ int ret;
+
+ if_freq = 0;
+ mix_reg = 1;
+ switch (mix_reg) {
+ case 0: if_freq = -2; break;
+ case 1: if_freq = -1; break;
+ case 2: if_freq = 0; break;
+ case 3: if_freq = 1; break;
+ case 4: if_freq = 2; break;
+ }
+
+ /* handle board specific EMI tuning */
+ tp = stfm1000_board_emi_tune(freq100);
+ if (tp != NULL) {
+ tune1 = tp->tune1;
+ sdnom = tp->sdnom;
+ } else {
+ fe_freq = freq100 + if_freq;
+
+ /* clamp into range */
+ if (fe_freq < STFM1000_FREQUENCY_100KHZ_MIN)
+ fe_freq = STFM1000_FREQUENCY_100KHZ_MIN;
+ else if (fe_freq > STFM1000_FREQUENCY_100KHZ_MAX)
+ fe_freq = STFM1000_FREQUENCY_100KHZ_MAX;
+
+ tp = &stfm1000_tune1_table[fe_freq -
+ STFM1000_FREQUENCY_100KHZ_MIN];
+
+ /* bits [14:0], [20:18] */
+ tune1 = (tp->tune1 & 0x7fff) | (mix_reg << 18);
+ sdnom = tp->sdnom;
+ }
+
+ agc1 = stfm1000->revid == STFM1000_CHIP_REV_TA2 ? 0x0400 : 0x2200;
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_AGC_CONTROL1,
+ agc1, 0x3f00);
+ if (ret != 0)
+ goto err;
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_TUNE1, tune1,
+ 0xFFFF7FFF); /* do not set bit-15 */
+ if (ret != 0)
+ goto err;
+
+ /* keep this around */
+ stfm1000->sdnominal_pivot = sdnom;
+
+ ret = stfm1000_write(stfm1000, STFM1000_SDNOMINAL, sdnom);
+ if (ret != 0)
+ goto err;
+
+ /* fix for seek-not-stopping on alternate tunings */
+ ret = stfm1000_set_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_DB_ACCEPT);
+ if (ret != 0)
+ goto err;
+
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_DATAPATH,
+ STFM1000_DB_ACCEPT);
+ if (ret != 0)
+ goto err;
+
+ ret = stfm1000_set_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_DRI_CLK_EN);
+ if (ret != 0)
+ goto err;
+
+ /* 6MHz spur fix */
+ if ((freq100 >= 778 && freq100 <= 782) ||
+ (freq100 >= 838 && freq100 <= 842) ||
+ (freq100 >= 898 && freq100 <= 902) ||
+ (freq100 >= 958 && freq100 <= 962) ||
+ (freq100 >= 1018 && freq100 <= 1022) ||
+ (freq100 >= 1078 && freq100 <= 1080))
+ i2s_clock = 5; /* 4.8MHz */
+ else
+ i2s_clock = 4;
+
+ ret = stfm1000_write_masked(stfm1000, STFM1000_DATAPATH,
+ STFM1000_SAI_CLK_DIV(i2s_clock), STFM1000_SAI_CLK_DIV_MASK);
+ if (ret != 0)
+ goto err;
+
+ ret = stfm1000_set_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_DRI_CLK_EN);
+ if (ret != 0)
+ goto err;
+
+ if (tune1 & 0xf)
+ ret = stfm1000_set_bits(stfm1000, STFM1000_CLK1,
+ STFM1000_ENABLE_TAPDELAYFIX);
+ else
+ ret = stfm1000_clear_bits(stfm1000, STFM1000_CLK1,
+ STFM1000_ENABLE_TAPDELAYFIX);
+
+ if (ret != 0)
+ goto err;
+
+ tune_cap = (int)(stfm1000->tune_cap_a_f -
+ stfm1000->tune_cap_b_f * freq100);
+ if (tune_cap < 4)
+ tune_cap = 4;
+ ret = stfm1000_write_masked(stfm1000, STFM1000_LNA,
+ STFM1000_ANTENNA_TUNECAP(tune_cap),
+ STFM1000_ANTENNA_TUNECAP_MASK);
+ if (ret != 0)
+ goto err;
+
+ /* set signal strenth to 0 */
+ /* stfm1000_dcdc_update(); */
+
+ /* cmp_rds_setRdsStatus(0) */
+ /* cmp_rds_ResetGroupCallbacks(); */
+ stfm1000->freq = freq;
+
+ return 0;
+err:
+ return -1;
+}
+
+static const struct v4l2_queryctrl radio_qctrl[] = {
+ {
+ .id = V4L2_CID_AUDIO_MUTE,
+ .name = "Mute",
+ .minimum = 0,
+ .maximum = 1,
+ .default_value = 1,
+ .type = V4L2_CTRL_TYPE_BOOLEAN,
+ },
+};
+
+static int vidioc_querycap(struct file *file, void *priv,
+ struct v4l2_capability *v)
+{
+ strlcpy(v->driver, "radio-stfm1000", sizeof(v->driver));
+ strlcpy(v->card, "STFM1000 Radio", sizeof(v->card));
+ sprintf(v->bus_info, "i2c");
+ v->version = KERNEL_VERSION(0, 0, 1);
+ v->capabilities = V4L2_CAP_TUNER;
+ return 0;
+}
+
+static int vidioc_g_tuner(struct file *file, void *priv,
+ struct v4l2_tuner *v)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+ u32 tmp, rssi_dc_est, tone_data;
+ u16 rssi_mantissa, rssi_exponent, rssi_decoded;
+ u16 prssi;
+ s16 mpx_dc;
+ int rssi_log;
+ int ret;
+
+ if (v->index > 0)
+ return -EINVAL;
+
+ mutex_lock(&stfm1000->state_lock);
+
+ strcpy(v->name, "FM");
+ v->type = V4L2_TUNER_RADIO;
+ v->rangelow = (u32)(87.5 * 16000);
+ v->rangehigh = (u32)(108 * 16000);
+ v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
+ v->capability = V4L2_TUNER_CAP_LOW;
+ v->audmode = V4L2_TUNER_MODE_STEREO;
+ v->signal = 0; /* tr_getsigstr(); */
+
+ msleep(50);
+
+ ret = stfm1000_read(stfm1000, STFM1000_RSSI_TONE, &tmp);
+ if (ret != 0)
+ goto out;
+
+ rssi_dc_est = tmp & 0xffff;
+ tone_data = (tmp >> 16) & 0x0fff;
+
+ rssi_mantissa = (rssi_dc_est & 0xffe0) >> 5; /* 11Msb */
+ rssi_exponent = rssi_dc_est & 0x001f; /* 5 lsb */
+ rssi_decoded = (u32)rssi_mantissa << rssi_exponent;
+
+ /* Convert Rsst to 10log(Rssi) */
+ for (prssi = 20; prssi > 0; prssi--)
+ if (rssi_decoded >= (1 << prssi))
+ break;
+
+ rssi_log = (3 * rssi_decoded >> prssi) + (3 * prssi - 3);
+ /* clamp to positive */
+ if (rssi_log < 0)
+ rssi_log = 0;
+ /* Compensate for errors in truncation/approximation by adding 1 */
+ rssi_log++;
+
+ stfm1000->rssi_dc_est_log = rssi_log;
+ stfm1000->signal_strength = stfm1000->rssi_dc_est_log;
+
+ /* determine absolute value */
+ if (tmp & 0x0800)
+ mpx_dc = ((tmp >> 16) & 0x0fff) | 0xf000;
+ else
+ mpx_dc = (tmp >> 16) & 0x0fff;
+ stfm1000->mpx_dc = mpx_dc;
+ mpx_dc = mpx_dc < 0 ? -mpx_dc : mpx_dc;
+
+ v->signal = rssi_decoded & 0xffff;
+
+out:
+ mutex_unlock(&stfm1000->state_lock);
+
+ return ret;
+}
+
+static int vidioc_s_tuner(struct file *file, void *priv,
+ struct v4l2_tuner *v)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ (void)stfm1000;
+
+ if (v->index > 0)
+ return -EINVAL;
+
+ return 0;
+}
+
+static int vidioc_s_frequency(struct file *file, void *priv,
+ struct v4l2_frequency *f)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ mutex_lock(&stfm1000->state_lock);
+
+ /* convert from the crazy linux value to our decimal based values */
+ stfm1000->freq = (u32)div_u64((u64)(125 * (u64)f->frequency), 2000);
+
+ if (stfm1000->active)
+ Take_Down(stfm1000);
+
+ sw_tune(stfm1000, stfm1000->freq);
+
+ if (stfm1000->active)
+ Bring_Up(stfm1000);
+
+ mutex_unlock(&stfm1000->state_lock);
+
+ return 0;
+}
+
+static int vidioc_g_frequency(struct file *file, void *priv,
+ struct v4l2_frequency *f)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ f->type = V4L2_TUNER_RADIO;
+ f->frequency = stfm1000->freq * 16;
+
+ return 0;
+}
+
+static int vidioc_queryctrl(struct file *file, void *priv,
+ struct v4l2_queryctrl *qc)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+ int i;
+
+ (void)stfm1000;
+
+ for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
+ if (qc->id && qc->id == radio_qctrl[i].id) {
+ memcpy(qc, &radio_qctrl[i], sizeof(*qc));
+ return 0;
+ }
+ }
+ return -EINVAL;
+}
+
+static int vidioc_g_ctrl(struct file *file, void *priv,
+ struct v4l2_control *ctrl)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ switch (ctrl->id) {
+
+ case V4L2_CID_AUDIO_MUTE:
+ ctrl->value = stfm1000->mute;
+ return 0;
+
+ }
+ return -EINVAL;
+}
+
+static int vidioc_s_ctrl(struct file *file, void *priv,
+ struct v4l2_control *ctrl)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+ int ret;
+
+ mutex_lock(&stfm1000->state_lock);
+
+ ret = -EINVAL;
+
+ switch (ctrl->id) {
+
+ case V4L2_CID_AUDIO_MUTE:
+ stfm1000->mute = ctrl->value;
+ ret = 0;
+ break;
+ }
+
+ mutex_unlock(&stfm1000->state_lock);
+
+ return ret;
+}
+
+static int vidioc_g_audio(struct file *file, void *priv,
+ struct v4l2_audio *a)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ (void)stfm1000;
+
+ if (a->index > 1)
+ return -EINVAL;
+
+ strcpy(a->name, "Radio");
+ a->capability = V4L2_AUDCAP_STEREO;
+ return 0;
+}
+
+static int vidioc_s_audio(struct file *file, void *priv,
+ struct v4l2_audio *a)
+{
+ if (a->index > 1)
+ return -EINVAL;
+ return 0;
+}
+
+static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ (void)stfm1000;
+
+ *i = 0;
+
+ return 0;
+}
+
+static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ (void)stfm1000;
+
+ if (i != 0)
+ return -EINVAL;
+
+ return 0;
+}
+
+const struct v4l2_ioctl_ops stfm_ioctl_ops = {
+ .vidioc_querycap = vidioc_querycap,
+ .vidioc_g_tuner = vidioc_g_tuner,
+ .vidioc_s_tuner = vidioc_s_tuner,
+ .vidioc_g_frequency = vidioc_g_frequency,
+ .vidioc_s_frequency = vidioc_s_frequency,
+ .vidioc_queryctrl = vidioc_queryctrl,
+ .vidioc_g_ctrl = vidioc_g_ctrl,
+ .vidioc_s_ctrl = vidioc_s_ctrl,
+ .vidioc_g_audio = vidioc_g_audio,
+ .vidioc_s_audio = vidioc_s_audio,
+ .vidioc_g_input = vidioc_g_input,
+ .vidioc_s_input = vidioc_s_input,
+};
+
+static int stfm1000_open(struct inode *inode, struct file *file)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ mutex_lock(&stfm1000->state_lock);
+ stfm1000->users = 1;
+ mutex_unlock(&stfm1000->state_lock);
+
+ return 0;
+}
+static int stfm1000_close(struct inode *inode, struct file *file)
+{
+ struct stfm1000 *stfm1000 = stfm1000_from_file(file);
+
+ if (!stfm1000)
+ return -ENODEV;
+
+ stfm1000->users = 0;
+ if (stfm1000->removed)
+ kfree(stfm1000);
+ return 0;
+}
+
+static const struct file_operations stfm1000_fops = {
+ .owner = THIS_MODULE,
+ .open = stfm1000_open,
+ .release = stfm1000_close,
+ .ioctl = video_ioctl2,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = v4l_compat_ioctl32,
+#endif
+ .llseek = no_llseek,
+};
+
+/* sysfs */
+
+#define STFM1000_RO_ATTR(var) \
+static ssize_t stfm1000_show_ ## var(struct device *d, \
+ struct device_attribute *attr, char *buf) \
+{ \
+ struct i2c_client *client = to_i2c_client(d); \
+ struct stfm1000 *stfm1000 = i2c_get_clientdata(client); \
+ return sprintf(buf, "%d\n", stfm1000->var); \
+} \
+static DEVICE_ATTR(var, 0444, stfm1000_show_ ##var, NULL)
+
+#define STFM1000_RW_ATTR(var) \
+static ssize_t stfm1000_show_ ## var(struct device *d, \
+ struct device_attribute *attr, char *buf) \
+{ \
+ struct i2c_client *client = to_i2c_client(d); \
+ struct stfm1000 *stfm1000 = i2c_get_clientdata(client); \
+ return sprintf(buf, "%u\n", stfm1000->var); \
+} \
+static ssize_t stfm1000_store_ ## var(struct device *d, \
+ struct device_attribute *attr, const char *buf, size_t size) \
+{ \
+ struct i2c_client *client = to_i2c_client(d); \
+ struct stfm1000 *stfm1000 = i2c_get_clientdata(client); \
+ unsigned long v; \
+ \
+ strict_strtoul(buf, 0, &v); \
+ stfm1000_commit_ ## var(stfm1000, v); \
+ return size; \
+} \
+static DEVICE_ATTR(var, 0644, stfm1000_show_ ##var, stfm1000_store_ ##var)
+
+#define STFM1000_RW_ATTR_SIMPLE(var) \
+static void stfm1000_commit_ ## var(struct stfm1000 *stfm1000, \
+ unsigned long value) \
+{ \
+ stfm1000->var = value; \
+} \
+STFM1000_RW_ATTR(var)
+
+STFM1000_RO_ATTR(weak_signal);
+STFM1000_RO_ATTR(pilot_present);
+STFM1000_RO_ATTR(stereo);
+STFM1000_RO_ATTR(rssi);
+STFM1000_RO_ATTR(mpx_dc);
+STFM1000_RO_ATTR(signal_strength);
+STFM1000_RW_ATTR_SIMPLE(rds_signal_th);
+STFM1000_RO_ATTR(rds_present);
+STFM1000_RO_ATTR(is_station);
+
+static void stfm1000_commit_georegion(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ /* don't do anything for illegal region */
+ if (value != 0 && value != 1)
+ return;
+
+ mutex_lock(&stfm1000->state_lock);
+
+ stfm1000->georegion = value;
+ if (stfm1000->georegion == 0)
+ stfm1000_clear_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_DEEMPH_50_75B);
+ else
+ stfm1000_set_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_DEEMPH_50_75B);
+
+ mutex_unlock(&stfm1000->state_lock);
+}
+STFM1000_RW_ATTR(georegion);
+
+static void stfm1000_commit_freq(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ mutex_lock(&stfm1000->state_lock);
+
+ /* clamp */
+ if (value < STFM1000_FREQUENCY_100KHZ_MIN * 100)
+ value = STFM1000_FREQUENCY_100KHZ_MIN * 100;
+ else if (value > STFM1000_FREQUENCY_100KHZ_MAX * 100)
+ value = STFM1000_FREQUENCY_100KHZ_MAX * 100;
+
+ stfm1000->freq = value;
+
+ if (stfm1000->active)
+ Take_Down(stfm1000);
+
+ sw_tune(stfm1000, stfm1000->freq);
+
+ if (stfm1000->active)
+ Bring_Up(stfm1000);
+
+ mutex_unlock(&stfm1000->state_lock);
+}
+STFM1000_RW_ATTR(freq);
+
+static void stfm1000_commit_mute(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ stfm1000->mute = !!value;
+}
+STFM1000_RW_ATTR(mute);
+
+static void stfm1000_commit_force_mono(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ stfm1000->force_mono = !!value;
+ /* set force mono parameters for the filter */
+ stfm1000->filter_parms.pCoefForcedMono = stfm1000->force_mono;
+
+ /* yeah, I know, it's stupid */
+ stfm1000->rds_state.demod.pCoefForcedMono =
+ stfm1000->filter_parms.pCoefForcedMono;
+}
+STFM1000_RW_ATTR(force_mono);
+
+STFM1000_RW_ATTR_SIMPLE(monitor_period);
+STFM1000_RW_ATTR_SIMPLE(quality_monitor);
+STFM1000_RW_ATTR_SIMPLE(quality_monitor_period);
+STFM1000_RW_ATTR_SIMPLE(agc_monitor_period);
+STFM1000_RW_ATTR_SIMPLE(tune_rssi_th);
+STFM1000_RW_ATTR_SIMPLE(tune_mpx_dc_th);
+
+static void stfm1000_commit_rds_enable(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ /* don't do anything for illegal values (or for not TB2) */
+ if ((value != 0 && value != 1) ||
+ stfm1000->revid == STFM1000_CHIP_REV_TA2)
+ return;
+
+ mutex_lock(&stfm1000->state_lock);
+
+ stfm1000->rds_enable = value;
+ if (stfm1000->rds_enable == 0)
+ stfm1000_clear_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_RDS_ENABLE);
+ else
+ stfm1000_set_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_RDS_ENABLE);
+
+ mutex_unlock(&stfm1000->state_lock);
+}
+STFM1000_RW_ATTR(rds_enable);
+
+static void stfm1000_commit_rds_sync(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ stfm1000->rds_sync = stfm1000->rds_enable && !!value;
+}
+STFM1000_RW_ATTR(rds_sync);
+
+STFM1000_RW_ATTR_SIMPLE(rds_pkt_good);
+STFM1000_RW_ATTR_SIMPLE(rds_pkt_bad);
+STFM1000_RW_ATTR_SIMPLE(rds_pkt_recovered);
+STFM1000_RW_ATTR_SIMPLE(rds_pkt_lost_sync);
+STFM1000_RW_ATTR_SIMPLE(rds_bit_overruns);
+STFM1000_RW_ATTR_SIMPLE(rds_info);
+
+static void stfm1000_commit_rds_sdnominal_adapt(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ stfm1000->rds_sdnominal_adapt = !!value;
+ stfm1000->rds_state.demod.sdnom_adapt = stfm1000->rds_sdnominal_adapt;
+}
+STFM1000_RW_ATTR(rds_sdnominal_adapt);
+
+static void stfm1000_commit_rds_phase_pop(struct stfm1000 *stfm1000,
+ unsigned long value)
+{
+ stfm1000->rds_phase_pop = !!value;
+ stfm1000->rds_state.demod.PhasePoppingEnabled =
+ stfm1000->rds_phase_pop;
+}
+STFM1000_RW_ATTR(rds_phase_pop);
+
+STFM1000_RW_ATTR_SIMPLE(tuning_grid_50KHz);
+
+static ssize_t stfm1000_show_rds_ps(struct device *d,
+ struct device_attribute *attr, char *buf)
+{
+ struct i2c_client *client = to_i2c_client(d);
+ struct stfm1000 *stfm1000 = i2c_get_clientdata(client);
+ char ps[9];
+
+ if (stfm1000_rds_get_ps(&stfm1000->rds_state, ps, sizeof(ps)) <= 0)
+ ps[0] = '\0';
+
+ return sprintf(buf, "%s\n", ps);
+}
+static DEVICE_ATTR(rds_ps, 0444, stfm1000_show_rds_ps, NULL);
+
+static ssize_t stfm1000_show_rds_text(struct device *d,
+ struct device_attribute *attr, char *buf)
+{
+ struct i2c_client *client = to_i2c_client(d);
+ struct stfm1000 *stfm1000 = i2c_get_clientdata(client);
+ char text[65];
+
+ if (stfm1000_rds_get_text(&stfm1000->rds_state, text,
+ sizeof(text)) <= 0)
+ text[0] = '\0';
+
+ return sprintf(buf, "%s\n", text);
+}
+static DEVICE_ATTR(rds_text, 0444, stfm1000_show_rds_text, NULL);
+
+static struct device_attribute *stfm1000_attrs[] = {
+ &dev_attr_agc_monitor_period,
+ &dev_attr_force_mono,
+ &dev_attr_freq,
+ &dev_attr_georegion,
+ &dev_attr_is_station,
+ &dev_attr_monitor_period,
+ &dev_attr_mpx_dc,
+ &dev_attr_mute,
+ &dev_attr_pilot_present,
+ &dev_attr_quality_monitor,
+ &dev_attr_quality_monitor_period,
+ &dev_attr_rds_bit_overruns,
+ &dev_attr_rds_enable,
+ &dev_attr_rds_info,
+ &dev_attr_rds_phase_pop,
+ &dev_attr_rds_pkt_bad,
+ &dev_attr_rds_pkt_good,
+ &dev_attr_rds_pkt_lost_sync,
+ &dev_attr_rds_pkt_recovered,
+ &dev_attr_rds_present,
+ &dev_attr_rds_ps,
+ &dev_attr_rds_sdnominal_adapt,
+ &dev_attr_rds_signal_th,
+ &dev_attr_rds_sync,
+ &dev_attr_rds_text,
+ &dev_attr_rssi,
+ &dev_attr_signal_strength,
+ &dev_attr_stereo,
+ &dev_attr_tune_mpx_dc_th,
+ &dev_attr_tune_rssi_th,
+ &dev_attr_tuning_grid_50KHz,
+ &dev_attr_weak_signal,
+ NULL,
+};
+
+/* monitor thread */
+
+static void rds_process(struct stfm1000 *stfm1000)
+{
+ int count, bit;
+ int mix_reg, sdnominal_reg;
+ u32 sdnom, sdnom_new, limit;
+ u8 buf[8];
+
+ if (!stfm1000->rds_enable)
+ return;
+
+ if (stfm1000->rds_sync &&
+ stfm1000->rssi_dc_est_log > stfm1000->rds_signal_th) {
+ if (stfm1000->rds_info)
+ printk(KERN_INFO "RDS: sync\n");
+ stfm1000_rds_reset(&stfm1000->rds_state);
+ stfm1000->rds_demod_running = 1;
+ stfm1000->rds_sync = 0;
+ }
+
+ if (!stfm1000->rds_demod_running)
+ return;
+
+ /* process mix reg requests */
+ spin_lock_irq(&stfm1000->rds_lock);
+ mix_reg = stfm1000_rds_mix_msg_get(&stfm1000->rds_state);
+ spin_unlock_irq(&stfm1000->rds_lock);
+
+ if (mix_reg != -1) {
+
+ if (stfm1000->rds_info)
+ printk(KERN_INFO "RDS: new RDS_MIXOFFSET %d\n",
+ mix_reg & 1);
+
+ /* update register */
+ if (mix_reg & 1)
+ stfm1000_set_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_RDS_MIXOFFSET);
+ else
+ stfm1000_clear_bits(stfm1000, STFM1000_INITIALIZATION2,
+ STFM1000_RDS_MIXOFFSET);
+
+ /* signal it's processed */
+ spin_lock_irq(&stfm1000->rds_lock);
+ stfm1000_rds_mix_msg_processed(&stfm1000->rds_state, mix_reg);
+ spin_unlock_irq(&stfm1000->rds_lock);
+ }
+
+ /* process sdnominal reg requests */
+ spin_lock_irq(&stfm1000->rds_lock);
+ sdnominal_reg = stfm1000_rds_sdnominal_msg_get(&stfm1000->rds_state);
+ spin_unlock_irq(&stfm1000->rds_lock);
+
+ /* any change? */
+ if (sdnominal_reg != 0) {
+
+ stfm1000_read(stfm1000, STFM1000_SDNOMINAL, &sdnom);
+
+ sdnom_new = sdnom + sdnominal_reg;
+
+ /* Limit SDNOMINAL to within 244 ppm of its ideal value */
+ limit = stfm1000->sdnominal_pivot +
+ (stfm1000->sdnominal_pivot >> 12);
+ if (sdnom_new > limit)
+ sdnom_new = limit;
+
+ limit = stfm1000->sdnominal_pivot -
+ (stfm1000->sdnominal_pivot >> 12);
+ if (sdnom_new < limit)
+ sdnom_new = limit;
+
+ /* write the register */
+ stfm1000_write(stfm1000, STFM1000_SDNOMINAL, sdnom_new);
+
+ /* signal it's processed */
+ spin_lock_irq(&stfm1000->rds_lock);
+ stfm1000_rds_sdnominal_msg_processed(&stfm1000->rds_state,
+ sdnominal_reg);
+ spin_unlock_irq(&stfm1000->rds_lock);
+ }
+
+ /* pump bits out & pass them to the process function */
+ spin_lock_irq(&stfm1000->rds_lock);
+ while (stfm1000_rds_bits_available(&stfm1000->rds_state) > 128) {
+ count = 0;
+ while (count++ < 128 &&
+ (bit = stmf1000_rds_get_bit(
+ &stfm1000->rds_state)) >= 0) {
+ spin_unlock_irq(&stfm1000->rds_lock);
+
+ /* push bit for packet processing */
+ stfm1000_rds_packet_bit(&stfm1000->rds_state, bit);
+
+ spin_lock_irq(&stfm1000->rds_lock);
+ }
+ }
+ spin_unlock_irq(&stfm1000->rds_lock);
+
+ /* now we're free to process non-interrupt related work */
+ while (stfm1000_rds_packet_dequeue(&stfm1000->rds_state, buf) == 0) {
+
+ if (stfm1000->rds_info)
+ printk(KERN_INFO "RDS-PKT: %02x %02x %02x %02x "
+ "%02x %02x %02x %02x\n",
+ buf[0], buf[1], buf[2], buf[3],
+ buf[4], buf[5], buf[6], buf[7]);
+
+ stfm1000_rds_process_packet(&stfm1000->rds_state, buf);
+ }
+
+ /* update our own counters */
+ stfm1000->rds_pkt_good += stfm1000->rds_state.pkt.good_packets;
+ stfm1000->rds_pkt_bad += stfm1000->rds_state.pkt.bad_packets;
+ stfm1000->rds_pkt_recovered +=
+ stfm1000->rds_state.pkt.recovered_packets;
+ stfm1000->rds_pkt_lost_sync +=
+ stfm1000->rds_state.pkt.sync_lost_packets;
+ stfm1000->rds_bit_overruns +=
+ stfm1000->rds_state.demod.RdsDemodSkippedBitCnt;
+
+ /* zero them now */
+ stfm1000->rds_state.pkt.good_packets = 0;
+ stfm1000->rds_state.pkt.bad_packets = 0;
+ stfm1000->rds_state.pkt.recovered_packets = 0;
+ stfm1000->rds_state.pkt.sync_lost_packets = 0;
+ stfm1000->rds_state.demod.RdsDemodSkippedBitCnt = 0;
+
+ /* reset requested from RDS handler? */
+ if (stfm1000_rds_get_reset_req(&stfm1000->rds_state)) {
+ if (stfm1000->rds_info)
+ printk(KERN_INFO "RDS: reset requested\n");
+ stfm1000_rds_reset(&stfm1000->rds_state);
+
+ stfm1000->rds_sync = stfm1000->rds_enable; /* force sync (if RDS) */
+ stfm1000->rds_demod_running = 0;
+ stfm1000->rssi_dc_est_log = 0;
+ stfm1000->signal_strength = 0;
+ }
+}
+
+void stfm1000_monitor_signal(struct stfm1000 *stfm1000, int bit)
+{
+ set_bit(bit, &stfm1000->thread_events);
+ return wake_up_interruptible(&stfm1000->thread_wait);
+}
+
+static int stfm1000_monitor_thread(void *data)
+{
+ struct stfm1000 *stfm1000 = data;
+ int ret;
+
+ printk(KERN_INFO "stfm1000: monitor thread started\n");
+
+ set_freezable();
+
+ /* Hmm, linux becomes *very* unhappy without this ... */
+ while (!kthread_should_stop()) {
+
+ ret = wait_event_interruptible_timeout(stfm1000->thread_wait,
+ stfm1000->thread_events == 0,
+ msecs_to_jiffies(stfm1000->monitor_period));
+
+ stfm1000->thread_events = 0;
+
+ if (kthread_should_stop())
+ break;
+
+ try_to_freeze();
+
+ mutex_lock(&stfm1000->state_lock);
+
+ /* we must be active */
+ if (!stfm1000->active)
+ goto next;
+
+ if (stfm1000->rds_enable)
+ rds_process(stfm1000);
+
+ /* perform quality monitor */
+ if (time_after_eq(jiffies, stfm1000->next_quality_monitor)) {
+
+ /* full quality monitor? */
+ if (stfm1000->quality_monitor)
+ Monitor_STFM_Quality(stfm1000);
+ else /* simple */
+ Is_Station(stfm1000);
+
+ while (time_after_eq(jiffies,
+ stfm1000->next_quality_monitor))
+ stfm1000->next_quality_monitor +=
+ msecs_to_jiffies(
+ stfm1000->quality_monitor_period);
+ }
+
+ /* perform AGC monitor (if enabled) */
+ if (stfm1000->agc_monitor && time_after_eq(jiffies,
+ stfm1000->next_agc_monitor)) {
+ Monitor_STFM_AGC(stfm1000);
+ while (time_after_eq(jiffies,
+ stfm1000->next_agc_monitor))
+ stfm1000->next_agc_monitor +=
+ msecs_to_jiffies(
+ stfm1000->agc_monitor_period);
+ }
+next:
+ mutex_unlock(&stfm1000->state_lock);
+ }
+
+ printk(KERN_INFO "stfm1000: monitor thread stopped\n");
+
+ return 0;
+}
+
+static u64 stfm1000_dma_mask = DMA_32BIT_MASK;
+
+static int stfm1000_probe(struct i2c_client *client,
+ const struct i2c_device_id *did)
+{
+ struct device *dev;
+ struct stfm1000 *stfm1000;
+ struct video_device *vd;
+ struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
+ int ret;
+ u32 id;
+ const char *idtxt;
+ int i;
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
+ dev_warn(&adapter->dev,
+ "I2C doesn't support I2C_FUNC_SMBUS_BYTE_DATA\n");
+ return -EIO;
+ }
+
+ /* make sure the dma masks are set correctly */
+ dev = &client->dev;
+ if (!dev->dma_mask)
+ dev->dma_mask = &stfm1000_dma_mask;
+ if (!dev->coherent_dma_mask)
+ dev->coherent_dma_mask = DMA_32BIT_MASK;
+
+ stfm1000 = kzalloc(sizeof(*stfm1000), GFP_KERNEL);
+ if (!stfm1000)
+ return -ENOMEM;
+
+ stfm1000->client = client;
+ i2c_set_clientdata(client, stfm1000);
+
+ mutex_init(&stfm1000->xfer_lock);
+ mutex_init(&stfm1000->state_lock);
+
+ vd = &stfm1000->radio;
+
+ strcpy(vd->name, "stfm1000");
+ vd->vfl_type = VID_TYPE_TUNER;
+ vd->fops = &stfm1000_fops;
+ vd->ioctl_ops = &stfm_ioctl_ops;
+
+ /* vd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG; */
+
+ vd->parent = &client->dev;
+
+ ret = video_register_device(vd, VFL_TYPE_RADIO, -1);
+ if (ret != 0) {
+ dev_warn(&adapter->dev,
+ "Cannot register radio device\n");
+ goto out;
+ }
+
+ spin_lock_init(&stfm1000->rds_lock);
+
+ stfm1000_setup_reg_set(stfm1000);
+
+ /* stfm1000->dbgflg |= STFM1000_DBGFLG_I2C; */
+
+ ret = stfm1000_read(stfm1000, STFM1000_CHIPID, &id);
+ if (ret < 0) {
+ dev_warn(&adapter->dev,
+ "Cannot read ID register\n");
+ goto out;
+ }
+ stfm1000->revid = id & 0xff;
+
+ /* NOTE: the tables are precalculated */
+ stfm1000->tune_rssi_th = 28;
+ stfm1000->tune_mpx_dc_th = 300;
+ stfm1000->adj_chan_th = 100;
+ stfm1000->pilot_est_th = 25;
+ stfm1000->agc_monitor = 0; /* AGC monitor disabled */
+ stfm1000->quality_monitor = 1;
+ stfm1000->weak_signal = 0;
+ stfm1000->prev_pilot_present = 0;
+ stfm1000->tune_cap_a_f = (u32)(72.4 * 65536);
+ stfm1000->tune_cap_b_f = (u32)(0.07 * 65536);
+
+ /* only TB2 supports RDS */
+ stfm1000->rds_enable = stfm1000->revid == STFM1000_CHIP_REV_TB2 &&
+ rds_enable;
+ stfm1000->rds_present = 0;
+ stfm1000->rds_signal_th = 33;
+
+ stfm1000->freq = 92600;
+
+ stfm1000->georegion = georegion;
+ stfm1000->rssi = 0;
+ stfm1000->stereo = 0;
+ stfm1000->force_mono = 0;
+ stfm1000->monitor_period = 100;
+ stfm1000->quality_monitor_period = 1000;
+ stfm1000->agc_monitor_period = 200;
+
+ stfm1000->rds_sdnominal_adapt = 0;
+ stfm1000->rds_phase_pop = 1;
+
+ /* enable info about RDS */
+ stfm1000->rds_info = 0;
+
+ ret = stfm1000_power_up(stfm1000);
+ if (ret != 0) {
+ printk(KERN_ERR "%s: stfm1000_power_up failed\n",
+ __func__);
+ goto out;
+ }
+
+ if (stfm1000_alsa_ops && stfm1000_alsa_ops->init) {
+ ret = (*stfm1000_alsa_ops->init)(stfm1000);
+ if (ret != 0)
+ goto out;
+ stfm1000->alsa_initialized = 1;
+ }
+
+ ret = 0;
+ for (i = 0; stfm1000_attrs[i]; i++) {
+ ret = device_create_file(dev, stfm1000_attrs[i]);
+ if (ret)
+ break;
+ }
+ if (ret) {
+ while (--i >= 0)
+ device_remove_file(dev, stfm1000_attrs[i]);
+ goto out;
+ }
+
+ /* add it to the list */
+ mutex_lock(&devlist_lock);
+ stfm1000->idx = stfm1000_devcount++;
+ list_add_tail(&stfm1000->devlist, &stfm1000_devlist);
+ mutex_unlock(&devlist_lock);
+
+ init_waitqueue_head(&stfm1000->thread_wait);
+ stfm1000->thread = kthread_run(stfm1000_monitor_thread, stfm1000,
+ "stfm1000-%d", stfm1000->idx);
+ if (stfm1000->thread == NULL) {
+ printk(KERN_ERR "stfm1000: kthread_run failed\n");
+ goto out;
+ }
+
+ idtxt = stfm1000_get_rev_txt(stfm1000->revid);
+ if (idtxt == NULL)
+ printk(KERN_INFO "STFM1000: Loaded for unknown revision id "
+ "0x%02x\n", stfm1000->revid);
+ else
+ printk(KERN_INFO "STFM1000: Loaded for revision %s\n", idtxt);
+
+ return 0;
+
+out:
+ kfree(stfm1000);
+ return ret;
+}
+
+static int stfm1000_remove(struct i2c_client *client)
+{
+ struct stfm1000 *stfm1000 = i2c_get_clientdata(client);
+ struct device *dev = &client->dev;
+ int i;
+
+ kthread_stop(stfm1000->thread);
+
+ for (i = 0; stfm1000_attrs[i]; i++)
+ device_remove_file(dev, stfm1000_attrs[i]);
+
+ if (stfm1000->alsa_initialized) {
+ BUG_ON(stfm1000_alsa_ops->release == NULL);
+ (*stfm1000_alsa_ops->release)(stfm1000);
+ stfm1000->alsa_initialized = 0;
+ }
+
+ stfm1000_power_down(stfm1000);
+
+ video_unregister_device(&stfm1000->radio);
+
+ mutex_lock(&devlist_lock);
+ list_del(&stfm1000->devlist);
+ mutex_unlock(&devlist_lock);
+
+ kfree(stfm1000);
+ return 0;
+}
+
+static const struct i2c_device_id stfm1000_id[] = {
+ { "stfm1000", 0xC0 },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, stfm1000_id);
+
+static struct i2c_driver stfm1000_i2c_driver = {
+ .driver = {
+ .name = "stfm1000",
+ },
+ .probe = stfm1000_probe,
+ .remove = stfm1000_remove,
+ .id_table = stfm1000_id,
+};
+
+static int __init
+stfm1000_init(void)
+{
+ /* pull those in */
+ (void)Lock_Station;
+ (void)Unlock_Station;
+ return i2c_add_driver(&stfm1000_i2c_driver);
+}
+
+static void __exit
+stfm1000_exit(void)
+{
+ i2c_del_driver(&stfm1000_i2c_driver);
+
+ stfm1000_alsa_ops = NULL;
+}
+
+module_init(stfm1000_init);
+module_exit(stfm1000_exit);
+
+MODULE_AUTHOR("Pantelis Antoniou");
+MODULE_DESCRIPTION("A driver for the STFM1000 chip.");
+MODULE_LICENSE("GPL");
+
+module_param(georegion, int, 0400);
+module_param(rds_enable, int, 0400);
diff --git a/drivers/media/radio/stfm1000/stfm1000-filter.c b/drivers/media/radio/stfm1000/stfm1000-filter.c
new file mode 100644
index 000000000000..df42524a5da7
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-filter.c
@@ -0,0 +1,860 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#include <linux/init.h>
+
+#include "stfm1000.h"
+
+void stfm1000_filter_reset(struct stfm1000_filter_parms *sdf)
+{
+ sdf->Left = 0;
+ sdf->Right = 0;
+ sdf->RssiDecoded = 0;
+ sdf->RssiMant = 0;
+ sdf->RssiExp = 0;
+ sdf->RssiLb = 0;
+ sdf->TrueRssi = 0;
+ sdf->Prssi = 0;
+ sdf->RssiLog = 0;
+ sdf->ScaledTrueRssi = 0;
+ sdf->FilteredRssi = 0;
+ sdf->PrevFilteredRssi = 0;
+ sdf->DecRssi = 0;
+ sdf->ScaledRssiDecoded = 0;
+ sdf->ScaledRssiDecodedZ = 0;
+ sdf->ScaledRssiDecodedZz = 0;
+ sdf->Echo = 0;
+ sdf->EchoLb = 0;
+ sdf->TrueEcho = 0;
+ sdf->FilteredEchoLpr = 0;
+ sdf->PrevFilteredEchoLpr = 0;
+ sdf->FilteredEchoLmr = 0;
+ sdf->PrevFilteredEchoLmr = 0;
+ sdf->GatedEcho = 0;
+ sdf->ControlLpr = 0;
+ sdf->ControlLmr = 0;
+ sdf->LprBw = 0;
+ sdf->LmrBw = 0;
+
+ sdf->LprXz = 0;
+ sdf->LprXzz = 0;
+ sdf->LprYz = 0;
+ sdf->LprYzz = 0;
+ sdf->LmrXz = 0;
+ sdf->LmrXzz = 0;
+ sdf->LmrYz = 0;
+ sdf->LmrYzz = 0;
+ sdf->FilteredLpr = 0;
+ sdf->FilteredLmr = 0;
+
+ sdf->B0B = 0;
+ sdf->B0S = 0;
+ sdf->B0M = 0;
+ sdf->B1over2B = 0;
+ sdf->B1over2S = 0;
+ sdf->B1over2M = 0;
+ sdf->A1over2B = 0;
+ sdf->A1over2S = 0;
+ sdf->A1over2M = 0;
+ sdf->A2B = 0;
+ sdf->A2S = 0;
+ sdf->A2M = 0;
+
+ sdf->AdjBw = 0;
+
+ sdf->pCoefLprBwThLo = 20 << 8;
+ sdf->pCoefLprBwThHi = 30 << 8;
+ sdf->pCoefLmrBwThLo = 40 << 8;
+ sdf->pCoefLmrBwThHi = 50 << 8;
+ sdf->pCoefLprBwSlSc = 4800; /* SDK-2287 */
+ sdf->pCoefLprBwSlSh = 10; /* SDK-2287 */
+ sdf->pCoefLmrBwSlSc = 4800; /* SDK-2287 */
+ sdf->pCoefLmrBwSlSh = 10; /* SDK-2287 */
+ sdf->pCoefLprGaSlSc = 0;
+ sdf->pCoefLprGaSlSh = 0;
+
+ sdf->ScaledControlLmr = 0;
+
+ sdf->LprGa = 32767;
+ sdf->LmrGa = 32767;
+
+ sdf->pCoefLprGaTh = 20; /* 25 */
+ sdf->pCoefLmrGaTh = 55; /* 60 50 */
+
+ sdf->MuteAudio = 0;
+ sdf->PrevMuteAudio = 0;
+ sdf->MuteActionFlag = 0;
+ sdf->ScaleAudio = 0;
+
+ /* *** Programmable initial setup for stereo path filters */
+ sdf->LprB0 = 18806; /* -3dB cutoff = 17 kHz */
+ sdf->LprB1over2 = 18812; /* -3dB cutoff = 17 kHz */
+ sdf->LprA1over2 = -16079; /* -3dB cutoff = 17 kHz */
+ sdf->LprA2 = -11125; /* -3dB cutoff = 17 kHz */
+ sdf->LmrB0 = 18806; /* -3dB cutoff = 17 kHz */
+ sdf->LmrB1over2 = 18812; /* -3dB cutoff = 17 kHz */
+ sdf->LmrA1over2 = -16079; /* -3dB cutoff = 17 kHz */
+ sdf->LmrA2 = -11125; /* -3dB cutoff = 17 kHz */
+
+ sdf->pCoefForceLockLmrBw = 0; /* Force Lock LMR BW = LPR BW
+ * XXX BUG WARNING -
+ * This control doesn't work! */
+
+ sdf->pCoefForcedMono = 0; /* Do not set this =
+ * Quality Monitor will overwrite it */
+ sdf->pCoefBypassBlend = 0; /* BUG WARNING -
+ * This control doesn't work! */
+ sdf->pCoefBypassSoftmute = 0; /* BUG WARNING -
+ * This control doesn't work! */
+ sdf->pCoefBypassBwCtl = 0; /* BUG WARNING -
+ * This control doesn't work! */
+
+ /* There's a bug or something in the attack/decay section b/c
+ * setting these coef's to anything */
+ /* higher than 100ms or so causes the RSSI to be artificially low -
+ * Needs investigation! 15DEC06 */
+ sdf->pCoefRssiAttack = 65386; /* changed to 100ms to avoid
+ * stereo crackling
+ * 60764 corresponds to 3 */
+ sdf->pCoefRssiDecay = 65386; /* changed to 100ms to avoid
+ * stereo crackling
+ * 65530 corresponds to 10 */
+ sdf->pCoefEchoLprAttack = 52239; /* corresponds to 1 */
+ sdf->pCoefEchoLprDecay = 64796; /* corresponds to 20 */
+ sdf->pCoefEchoLmrAttack = 52239; /* corresponds to 1 */
+ sdf->pCoefEchoLmrDecay = 65520; /* corresponds to 20 */
+ sdf->pCoefEchoTh = 100;
+ sdf->pCoefEchoScLpr = (u16) (0.9999 * 32767.0);
+ sdf->pCoefEchoScLmr = (u16) (0.9999 * 32767.0);
+}
+
+void stfm1000_filter_decode(struct stfm1000_filter_parms *sdf, s16 Lpr,
+ s16 Lmr, u16 Rssi)
+{
+ s16 temp1_reg; /* mimics 16 bit register */
+ s16 temp2_reg; /* mimics 16 bit register */
+ s16 temp3_reg; /* mimics 16 bit register */
+ s16 temp4_reg; /* mimics 16 bit register */
+#ifndef _TUNER_STFM_MUTE
+ s16 temp5_reg; /* mimics 16 bit register */
+#endif
+ s32 temp2_reg_32; /*eI 108 27th Feb 06 temp variables. */
+
+ /* **************************************************************** */
+ /* *** Stereo Processing ****************************************** */
+ /* **************************************************************** */
+ /* *** This block operates at Fs = 44.1kHz */
+ /* ******** */
+ /* *** LPR path filter (2nd order IIR) */
+
+ sdf->Acc_signed = sdf->LprB0 * Lpr + 2 * (sdf->LprB1over2 * sdf->LprXz)
+ + sdf->LprB0 * sdf->LprXzz + 2 * (sdf->LprA1over2 * sdf->LprYz)
+ + sdf->LprA2 * sdf->LprYzz;
+
+ sdf->FilteredLpr = sdf->Acc_signed >> 15;
+
+ sdf->LprXzz = sdf->LprXz; /* update taps */
+ sdf->LprXz = Lpr;
+ sdf->LprYzz = sdf->LprYz;
+ sdf->LprYz = sdf->FilteredLpr;
+
+ /* *** LMR path filter (2nd order IIR) */
+ sdf->Acc_signed = sdf->LmrB0 * Lmr + 2 * (sdf->LmrB1over2 * sdf->LmrXz)
+ + sdf->LmrB0 * sdf->LmrXzz + 2 * (sdf->LmrA1over2 * sdf->LmrYz)
+ + sdf->LmrA2 * sdf->LmrYzz;
+
+ sdf->FilteredLmr = sdf->Acc_signed >> 15;
+
+ sdf->LmrXzz = sdf->LmrXz; /* update taps */
+ sdf->LmrXz = Lmr;
+ sdf->LmrYzz = sdf->LmrYz;
+ sdf->LmrYz = sdf->FilteredLmr;
+
+ /* *** Stereo Matrix */
+ if (0 == sdf->pCoefBypassBlend)
+ temp1_reg = sdf->LmrGa * sdf->FilteredLmr >> 15; /* Blend */
+ else
+ temp1_reg = sdf->FilteredLmr;
+
+ if (sdf->pCoefForcedMono) /* Forced Mono */
+ temp1_reg = 0;
+
+ if (0 == sdf->pCoefBypassSoftmute) {
+ temp2_reg = sdf->LprGa * sdf->FilteredLpr >> 15; /* LPR */
+ temp3_reg = sdf->LprGa * temp1_reg >> 15; /* LMR */
+ } else {
+ temp2_reg = sdf->FilteredLpr;
+ temp3_reg = temp1_reg;
+ }
+
+ temp4_reg = (temp2_reg + temp3_reg) / 2; /* Matrix */
+
+#ifndef _TUNER_STFM_MUTE
+ temp5_reg = (temp2_reg - temp3_reg) / 2;
+#endif
+
+#if 0
+ /* *** DC Cut Filter (leaky bucket estimate) */
+ if (0 == sdf->pCoefBypassDcCut) {
+ sdf->LeftLb_i32 =
+ sdf->LeftLb_i32 + temp4_reg - (sdf->LeftLb_i32 >> 8);
+ temp2_reg = temp4_reg - (sdf->LeftLb_i32 >> 8); /* signal -
+ dc_estimate */
+
+ sdf->RightLb_i32 =
+ sdf->RightLb_i32 + temp5_reg - (sdf->RightLb_i32 >> 8);
+ temp3_reg = temp5_reg - (sdf->RightLb_i32 >> 8); /* signal -
+ dc_estimate */
+ } else {
+ temp2_reg = temp4_reg;
+ temp3_reg = temp5_reg;
+ }
+#endif
+#ifdef _TUNER_STFM_MUTE
+ /* *** Mute Audio */
+ if (sdf->MuteAudio != sdf->PrevMuteAudio) /* Mute transition */
+ sdf->MuteActionFlag = 1; /* set flag */
+ sdf->PrevMuteAudio = sdf->MuteAudio; /* update history */
+
+ if (sdf->MuteActionFlag) {
+ if (0 == sdf->MuteAudio) { /* Mute to zero */
+ /* gradual mute down */
+ sdf->ScaleAudio = sdf->ScaleAudio - sdf->pCoefMuteStep;
+
+ /* eI-117:Oct28:as per C++ code */
+ /* if (0 < sdf->ScaleAudio) */
+ if (0 > sdf->ScaleAudio) {
+ sdf->ScaleAudio = 0; /* Minimum scale
+ * factor */
+ sdf->MuteActionFlag = 0; /* End Mute Action */
+ }
+ } else { /* Un-Mute to one */
+ /* gradual mute up */
+ sdf->ScaleAudio = sdf->ScaleAudio + sdf->pCoefMuteStep;
+ if (0 > sdf->ScaleAudio) { /* look for rollover
+ * beyong 32767 */
+ sdf->ScaleAudio = 32767; /* Maximum scale
+ * factor */
+ sdf->MuteActionFlag = 0; /* End Mute Action */
+ }
+ } /* end else */
+ } /* end if (sdf->MuteActionFlag) */
+
+/*! Output Processed Sample */
+
+ sdf->Left = (temp2_reg * sdf->ScaleAudio) >> 15; /* Scale */
+ sdf->Right = (temp3_reg * sdf->ScaleAudio) >> 15; /* Scale */
+
+#else /* !_TUNER_STFM_MUTE */
+
+ sdf->Left = temp4_reg;
+ sdf->Right = temp5_reg;
+
+#endif /* !_TUNER_STFM_MUTE */
+
+ /* *** End Stereo Processing ************************************** */
+ /* **************************************************************** */
+
+ /* **************************************************************** */
+ /* *** Signal Quality Indicators ********************************** */
+ /* **************************************************************** */
+ /* *** This block operates at Fs = 44.1kHz */
+ /* ******** */
+ /* *** RSSI */
+ /* ******** */
+ /* Decode Floating Point RSSI data */
+ /*! Input RSSI sample */
+ sdf->RssiMant = (Rssi & 0xFFE0) >> 5; /* 11 msb's */
+ sdf->RssiExp = Rssi & 0x001F; /* 5 lsb's */
+ sdf->RssiDecoded = sdf->RssiMant << sdf->RssiExp;
+
+ /* *** Convert RSSI to 10*Log10(RSSI) */
+ /* This is easily accomplished in DSP code using the CLZ instruction */
+ /* rather than using all these comparisons. */
+ /* The basic idea is this: */
+ /* if x >= 2^P */
+ /* f(x) = 3*x>>P + (3*P-3) */
+ /* Approx. is valid over the range of sdf->RssiDecoded in [0, 2^21] */
+ /* *** */
+ if (sdf->RssiDecoded >= 1048576)
+ sdf->Prssi = 20;
+ else if (sdf->RssiDecoded >= 524288)
+ sdf->Prssi = 19;
+ else if (sdf->RssiDecoded >= 262144)
+ sdf->Prssi = 18;
+ else if (sdf->RssiDecoded >= 131072)
+ sdf->Prssi = 17;
+ else if (sdf->RssiDecoded >= 65536)
+ sdf->Prssi = 16;
+ else if (sdf->RssiDecoded >= 32768)
+ sdf->Prssi = 15;
+ else if (sdf->RssiDecoded >= 16384)
+ sdf->Prssi = 14;
+ else if (sdf->RssiDecoded >= 8192)
+ sdf->Prssi = 13;
+ else if (sdf->RssiDecoded >= 4096)
+ sdf->Prssi = 12;
+ else if (sdf->RssiDecoded >= 2048)
+ sdf->Prssi = 11;
+ else if (sdf->RssiDecoded >= 1024)
+ sdf->Prssi = 10;
+ else if (sdf->RssiDecoded >= 512)
+ sdf->Prssi = 9;
+ else if (sdf->RssiDecoded >= 256)
+ sdf->Prssi = 8;
+ else if (sdf->RssiDecoded >= 128)
+ sdf->Prssi = 7;
+ else if (sdf->RssiDecoded >= 64)
+ sdf->Prssi = 6;
+ else if (sdf->RssiDecoded >= 32)
+ sdf->Prssi = 5;
+ else if (sdf->RssiDecoded >= 16)
+ sdf->Prssi = 4;
+ else if (sdf->RssiDecoded >= 8)
+ sdf->Prssi = 3;
+ else if (sdf->RssiDecoded >= 4)
+ sdf->Prssi = 2;
+ else if (sdf->RssiDecoded >= 2)
+ sdf->Prssi = 1;
+ else
+ sdf->Prssi = 0;
+ sdf->RssiLog =
+ (3 * sdf->RssiDecoded >> sdf->Prssi) + (3 * sdf->Prssi - 3);
+
+ if (0 > sdf->RssiLog) /* Clamp to positive */
+ sdf->RssiLog = 0;
+
+ /* Compensate for errors in truncation/approximation by adding 1 */
+ sdf->RssiLog = sdf->RssiLog + 1;
+
+ /* Leaky Bucket Filter DC estimate of RSSI */
+ sdf->RssiLb = sdf->RssiLb + sdf->RssiLog - (sdf->RssiLb >> 3);
+ sdf->TrueRssi = sdf->RssiLb >> 3;
+
+ /* Scale up so we have some room for precision */
+ sdf->ScaledTrueRssi = sdf->TrueRssi << 8;
+ /* ************ */
+ /* *** end RSSI */
+ /* ************ */
+
+ /* ******** */
+ /* *** Echo */
+ /* ******** */
+ /* *** Isolate Echo information as higher frequency info */
+ /* using [1 -2 1] highpass FIR */
+ sdf->ScaledRssiDecoded = sdf->RssiDecoded >> 4;
+ sdf->Echo =
+ (s16) ((sdf->ScaledRssiDecoded -
+ 2 * sdf->ScaledRssiDecodedZ + sdf->ScaledRssiDecodedZz));
+ sdf->ScaledRssiDecodedZz = sdf->ScaledRssiDecodedZ;
+ sdf->ScaledRssiDecodedZ = sdf->ScaledRssiDecoded;
+ /* ************ */
+ /* *** end Echo */
+ /* ************ */
+ /* *** End Signal Quality Indicators ******************************* */
+ /* ***************************************************************** */
+
+ /* ***************************************************************** */
+ /* *** Weak Signal Processing ************************************** */
+ /* ***************************************************************** */
+ /* *** This block operates at Fs = 44.1/16 = 2.75 Khz
+ * *eI 108 28th Feb 06 WSP and SM executes at 2.75Khz */
+ /* decimate by 16 STFM_FILTER_BLOCK_MULTIPLE is 16 */
+ if (0 == sdf->DecRssi) {
+ /* *** Filter RSSI via attack/decay structure */
+ if (sdf->ScaledTrueRssi > sdf->PrevFilteredRssi)
+ sdf->Acc =
+ sdf->pCoefRssiAttack *
+ sdf->PrevFilteredRssi + (65535 -
+ sdf->pCoefRssiAttack)
+ * sdf->ScaledTrueRssi;
+ else
+ sdf->Acc =
+ sdf->pCoefRssiDecay *
+ sdf->PrevFilteredRssi + (65535 -
+ sdf->pCoefRssiDecay)
+ * sdf->ScaledTrueRssi;
+
+ sdf->FilteredRssi = sdf->Acc >> 16;
+ sdf->PrevFilteredRssi = sdf->FilteredRssi;
+
+ /* *** Form Echo "energy" representation */
+ if (0 > sdf->Echo)
+ sdf->Echo = -sdf->Echo; /* ABS() */
+
+ /* Threshold compare */
+ sdf->GatedEcho = (s16) (sdf->Echo - sdf->pCoefEchoTh);
+ if (0 > sdf->GatedEcho) /* Clamp to (+)ve */
+ sdf->GatedEcho = 0;
+
+ /* *** Leaky bucket DC estimate of Echo energy */
+ sdf->EchoLb = sdf->EchoLb + sdf->GatedEcho -
+ (sdf->EchoLb >> 3);
+ sdf->TrueEcho = sdf->EchoLb >> 3;
+
+ /* *** Filter Echo via attack/decay structure for LPR */
+ if (sdf->TrueEcho > sdf->PrevFilteredEchoLpr)
+ sdf->Acc =
+ sdf->pCoefEchoLprAttack *
+ sdf->PrevFilteredEchoLpr +
+ (65535 - sdf->pCoefEchoLprAttack) *
+ sdf->TrueEcho;
+ else
+ sdf->Acc =
+ sdf->pCoefEchoLprDecay *
+ sdf->PrevFilteredEchoLpr +
+ (65535 - sdf->pCoefEchoLprDecay) *
+ sdf->TrueEcho;
+
+ sdf->FilteredEchoLpr = sdf->Acc >> 16;
+ sdf->PrevFilteredEchoLpr = sdf->FilteredEchoLpr;
+
+ /* *** Filter Echo via attack/decay structure for LMR */
+ if (sdf->TrueEcho > sdf->PrevFilteredEchoLmr)
+ sdf->Acc = sdf->pCoefEchoLmrAttack *
+ sdf->PrevFilteredEchoLmr +
+ (65535 - sdf->pCoefEchoLmrAttack)
+ * sdf->TrueEcho;
+ else
+ sdf->Acc =
+ sdf->pCoefEchoLmrDecay *
+ sdf->PrevFilteredEchoLmr + (65535 -
+ sdf->pCoefEchoLmrDecay)
+ * sdf->TrueEcho;
+
+ sdf->FilteredEchoLmr = sdf->Acc >> 16;
+ sdf->PrevFilteredEchoLmr = sdf->FilteredEchoLmr;
+
+ /* *** Form control variables */
+ /* Generically speaking, ctl = f(RSSI, Echo) =
+ * RSSI - (a*Echo)<<b, where a,b are programmable */
+ sdf->ControlLpr = sdf->FilteredRssi -
+ ((sdf->pCoefEchoScLpr *
+ sdf->FilteredEchoLpr << sdf->pCoefEchoShLpr) >> 15);
+ if (0 > sdf->ControlLpr)
+ sdf->ControlLpr = 0; /* Clamp to positive */
+
+ sdf->ControlLmr = sdf->FilteredRssi -
+ ((sdf->pCoefEchoScLmr *
+ sdf->FilteredEchoLmr << sdf->pCoefEchoShLmr) >> 15);
+ if (0 > sdf->ControlLmr)
+ sdf->ControlLmr = 0; /* Clamp to positive */
+
+ /* *** Define LPR_BW = f(control LPR) */
+ /* Assume that 5 kHz and 17 kHz are limits of LPR_BW control */
+ if (sdf->ControlLpr <= sdf->pCoefLprBwThLo)
+ sdf->LprBw = 5000; /* lower limit is 5 kHz */
+ else if (sdf->ControlLpr >= sdf->pCoefLprBwThHi)
+ sdf->LprBw = 17000; /* upper limit is 17 kHz */
+ else
+ sdf->LprBw = 17000 -
+ ((sdf->pCoefLprBwSlSc *
+ (sdf->pCoefLprBwThHi -
+ sdf->ControlLpr)) >> sdf->pCoefLprBwSlSh);
+
+ /* *** Define LMR_BW = f(control LMR) */
+ /* Assume that 5 kHz and 17 kHz are limits of LPR_BW control */
+ if (0 == sdf->pCoefForceLockLmrBw) { /* only do these calc's
+ * if LMR BW not
+ * ForceLocked */
+ if (sdf->ControlLmr <= sdf->pCoefLmrBwThLo)
+ sdf->LmrBw = 5000; /* lower limit is
+ * 5 kHz */
+ else if (sdf->ControlLmr >= sdf->pCoefLmrBwThHi)
+ sdf->LmrBw = 17000; /* upper limit is
+ * 17 kHz */
+ else
+ sdf->LmrBw = 17000 -
+ ((sdf->pCoefLmrBwSlSc *
+ (sdf->pCoefLmrBwThHi -
+ sdf->ControlLmr)) >>
+ sdf->pCoefLmrBwSlSh);
+ }
+ /* *** Define LMR_Gain = f(control LMR)
+ * Assume that Blending occurs across 20 dB range of
+ * control LMR. For sake of listenability, approximate
+ * antilog blending curve
+ * To simplify antilog approx, scale control LMR back into
+ * "RSSI in dB range" [0,60] */
+ sdf->ScaledControlLmr = sdf->ControlLmr >> 8;
+
+ /* how far below blend threshold are we? */
+ temp1_reg = sdf->pCoefLmrGaTh - sdf->ScaledControlLmr;
+ if (0 > temp1_reg) /* We're not below threshold,
+ * so no blending needed */
+ temp1_reg = 0;
+ temp2_reg = 20 - temp1_reg; /* Blend range = 20 dB */
+ if (0 > temp2_reg)
+ temp2_reg = 0; /* if beyond that range,
+ * then clamp to 0 */
+
+ /* We want stereo separation (n dB) to rolloff linearly over
+ * the 20 dB wide blend region.
+ * this necessitates a particular rolloff for the blend
+ * parameter, which is not obvious.
+ * See sw_audio/log_approx.m for calculation of this rolloff,
+ * implemented below...
+ * Note that stereo_separation (in dB) = 20*log10((1+a)/(1-a)),
+ * where a = blend scaler
+ * appropriately scaled for 2^15. This relationship sits at
+ * the heart of why this curve is needed. */
+ if (15 <= temp2_reg)
+ temp3_reg = 264 * temp2_reg + 27487;
+ else if (10 <= temp2_reg)
+ temp3_reg = 650 * temp2_reg + 21692;
+ else if (5 <= temp2_reg)
+ temp3_reg = 1903 * temp2_reg + 9166;
+ else
+ temp3_reg = 3736 * temp2_reg;
+
+ sdf->LmrGa = temp3_reg;
+
+ if (32767 < sdf->LmrGa)
+ sdf->LmrGa = 32767; /* Clamp to '1' */
+
+ /* *** Define LPR_Gain = f(control LPR)
+ * Assume that SoftMuting occurs across 20 dB range of
+ * control LPR
+ * For sake of listenability, approximate antilog softmute
+ * curve To simplify antilog approx, scale control LPR back
+ * into "RSSI in dB range" [0,60] */
+ sdf->ScaledControlLpr = sdf->ControlLpr >> 8;
+ /* how far below softmute threshold are we? */
+ temp1_reg = sdf->pCoefLprGaTh - sdf->ScaledControlLpr;
+ if (0 > temp1_reg) /* We're not below threshold,
+ * so no softmute needed */
+ temp1_reg = 0;
+ temp2_reg = 20 - temp1_reg; /* SoftmMute range = 20 dB */
+ if (0 > temp2_reg)
+ temp2_reg = 0; /* if beyond that range,
+ * then clamp to 0 */
+ /* Form 100*10^((temp2_reg-20)/20) approximation (antilog)
+ * over range [0,20] dB
+ * approximation in range [0,100], but we only want to
+ * softmute down to -20 dB, no further */
+ if (16 < temp2_reg)
+ temp3_reg = 9 * temp2_reg - 80;
+ else if (12 < temp2_reg)
+ temp3_reg = 6 * temp2_reg - 33;
+ else if (8 < temp2_reg)
+ temp3_reg = 4 * temp2_reg - 8;
+ else
+ temp3_reg = 2 * temp2_reg + 9;
+
+ sdf->LprGa = 328 * temp3_reg; /* close to 32767*(1/100) */
+
+ if (32767 < sdf->LprGa)
+ sdf->LprGa = 32767; /* Clamp to '1' */
+
+ if (3277 > sdf->LprGa)
+ sdf->LprGa = 3277; /* Clamp to 0.1*32767 =
+ * -20 dB min gain */
+
+ /* *************** Bandwidth Sweep Algorithm ************ */
+ /* *** Calculate 2nd order filter coefficients as function
+ * of desired BW. We do this by constructing piece-wise
+ * linear filter coef's as f(BW), which is why we break the
+ * calc's into different BW regions below.
+ * coef(BW) = S*(M*BW + B)
+ * For more info, see sw_audio/ws_filter.m checked into CVS */
+ if (0 == sdf->pCoefBypassBwCtl) { /* if ==1, then we just go
+ * with default coef set */
+ /* determine if we run thru loop once or twice... */
+ if (1 == sdf->pCoefForceLockLmrBw)
+ temp4_reg = 1; /* run thru once only to calc.
+ * LPR coef's */
+ else
+ temp4_reg = 2; /* run thru twice to calc.
+ * LPR and LMR coef's */
+
+ /* Here's the big coef. calc. loop */
+ for (temp1_reg = 0; temp1_reg < temp4_reg;
+ temp1_reg++) {
+
+ if (0 == temp1_reg)
+ temp2_reg = (s16) sdf->LprBw;
+ else
+ temp2_reg = (s16) sdf->LmrBw;
+
+
+ if (6000 > temp2_reg) {
+ /* interval = [4.4kHz, 6.0kHz) */
+ sdf->B0M = 22102;
+ sdf->B0B = -2209;
+ sdf->B0S = 1;
+
+ sdf->B1over2M = 22089;
+ sdf->B1over2B = -2205;
+ sdf->B1over2S = 1;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = -24664;
+ sdf->A2B = 11698;
+ sdf->A2S = 2;
+ } else if (8000 > temp2_reg) {
+ /* interval = [6.0kHz, 8.0kHz) */
+ sdf->B0M = 22102;
+ sdf->B0B = -2209;
+ sdf->B0S = 1;
+
+ sdf->B1over2M = 22089;
+ sdf->B1over2B = -2205;
+ sdf->B1over2S = 1;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = -31231;
+ sdf->A2B = 18468;
+ sdf->A2S = 1;
+ } else if (10000 > temp2_reg) {
+ /* interval = [8.0kHz, 10.0kHz) */
+ sdf->B0M = 28433;
+ sdf->B0B = -4506;
+ sdf->B0S = 1;
+
+ sdf->B1over2M = 28462;
+ sdf->B1over2B = -4584;
+ sdf->B1over2S = 1;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = -14811;
+ sdf->A2B = 12511;
+ sdf->A2S = 1;
+ } else if (12000 > temp2_reg) {
+ /* interval = [10.0kHz, 12.0kHz) */
+ sdf->B0M = 28433;
+ sdf->B0B = -4506;
+ sdf->B0S = 1;
+
+ sdf->B1over2M = 28462;
+ sdf->B1over2B = -4584;
+ sdf->B1over2S = 1;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = -181;
+ sdf->A2B = 5875;
+ sdf->A2S = 1;
+ } else if (14000 > temp2_reg) {
+ /* interval = [12.0kHz, 14.0kHz) */
+ sdf->B0M = 18291;
+ sdf->B0B = -4470;
+ sdf->B0S = 2;
+
+ sdf->B1over2M = 18461;
+ sdf->B1over2B = -4597;
+ sdf->B1over2S = 2;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = 14379;
+ sdf->A2B = -2068;
+ sdf->A2S = 1;
+ } else if (16000 > temp2_reg) {
+ /* interval = [14.0kHz, 16.0kHz) */
+ sdf->B0M = 18291;
+ sdf->B0B = -4470;
+ sdf->B0S = 2;
+
+ sdf->B1over2M = 18461;
+ sdf->B1over2B = -4597;
+ sdf->B1over2S = 2;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = 30815;
+ sdf->A2B = -12481;
+ sdf->A2S = 1;
+ } else if (18000 > temp2_reg) {
+ /* interval = [16.0kHz, 18.0kHz) */
+ sdf->B0M = 24740;
+ sdf->B0B = -9152;
+ sdf->B0S = 2;
+
+ sdf->B1over2M = 24730;
+ sdf->B1over2B = -9142;
+ sdf->B1over2S = 2;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = 25631;
+ sdf->A2B = -13661;
+ sdf->A2S = 2;
+ } else {
+ /* interval = [18.0kHz, 19.845kHz) */
+ sdf->B0M = 24740;
+ sdf->B0B = -9152;
+ sdf->B0S = 2;
+
+ sdf->B1over2M = 24730;
+ sdf->B1over2B = -9142;
+ sdf->B1over2S = 2;
+
+ sdf->A1over2M = 31646;
+ sdf->A1over2B = -15695;
+ sdf->A1over2S = 2;
+
+ sdf->A2M = 19382;
+ sdf->A2B = -12183;
+ sdf->A2S = 4;
+ }
+
+ if (0 == temp1_reg) {
+ /* The piece-wise linear eq's are
+ * based on a scaled version
+ * (32768/22050) of BW */
+
+ /* Note 32768/22050 <-> 2*(16384/22050)
+ * <-> 2*((16384/22050)*32768)>>15 */
+ sdf->AdjBw = ((temp2_reg << 1) *
+ 24348) >> 15;
+
+ /* temp = mx */
+ temp3_reg = (sdf->B0M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LprB0 = sdf->B0S *
+ (temp3_reg + sdf->B0B);
+
+ /* temp = mx */
+ temp3_reg = (sdf->B1over2M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LprB1over2 = sdf->B1over2S *
+ (temp3_reg + sdf->B1over2B);
+
+ /* temp = mx */
+ temp3_reg = (sdf->A1over2M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LprA1over2 = -sdf->A1over2S *
+ (temp3_reg + sdf->A1over2B);
+
+ /* temp = mx */
+ temp3_reg = (sdf->A2M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LprA2 = -sdf->A2S *
+ (temp3_reg + sdf->A2B);
+ /* *** end LPR channel --
+ * LPR coefficients now ready for
+ * Stereo Path next time */
+ } else {
+ /* The piece-wise linear eq's are
+ * based on a scaled version
+ * (32768/22050) of BW */
+
+ /* Note 32768/22050 <-> 2*(16384/22050)
+ * <-> 2*((16384/22050)*32768)>>15 */
+ sdf->AdjBw = ((temp2_reg << 1) *
+ 24348) >> 15;
+
+ /* temp = mx */
+ temp3_reg = (sdf->B0M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LmrB0 = sdf->B0S *
+ (temp3_reg + sdf->B0B);
+
+ /* temp = mx */
+ temp3_reg = (sdf->B1over2M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LmrB1over2 = sdf->B1over2S *
+ (temp3_reg + sdf->B1over2B);
+
+ /* temp = mx */
+ temp3_reg = (sdf->A1over2M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LmrA1over2 = -sdf->A1over2S *
+ (temp3_reg + sdf->A1over2B);
+
+ /* temp = mx */
+ temp3_reg = (sdf->A2M *
+ sdf->AdjBw) >> 15;
+
+ /* y = S*(mx + b) */
+ sdf->LmrA2 = -sdf->A2S *
+ (temp3_reg + sdf->A2B);
+ /* *** end LMR channel -- LMR
+ * coefficients now ready for Stereo
+ * Path next time */
+ }
+ } /* end for (temp1_reg=0... */
+ if (1 == sdf->pCoefForceLockLmrBw) {
+ /* if Force Lock LMR BW = LPR BW */
+ /* then set LMR coef's = LPR coef's */
+ sdf->LmrB0 = sdf->LprB0;
+ sdf->LmrB1over2 = sdf->LprB1over2;
+ sdf->LmrA1over2 = sdf->LprA1over2;
+ sdf->LmrA2 = sdf->LprA2;
+ }
+
+ } /* end if (0 == sdf->pCoef_BypassBwCtl) */
+ /* eI 108 24th Feb 06 Streo Matrix part moved after
+ * weak signal processing. */
+ if (0 == sdf->pCoefBypassBlend)
+ temp1_reg = sdf->LmrGa; /* Blend */
+ else
+ temp1_reg = 1;
+
+ if (sdf->pCoefForcedMono) /* Forced Mono */
+ temp1_reg = 0;
+
+ if (0 == sdf->pCoefBypassSoftmute) {
+
+ /* SoftMute applied to LPR */
+ sdf->temp2_reg_sm = sdf->LprGa;
+
+ temp2_reg_32 = sdf->LprGa * temp1_reg;
+
+ /* SoftMute applied to LMR */
+ sdf->temp3_reg_sm = (temp2_reg_32) >> 15;
+ } else {
+ sdf->temp2_reg_sm = 1; /* eI 108 24th Feb 06 update
+ * global variable for IIR
+ * filter. */
+ sdf->temp3_reg_sm = temp1_reg;
+ }
+
+ } /* end if (0 == sdf->DecRssi) */
+
+ sdf->DecRssi = ((sdf->DecRssi + 1) % 16); /* end decimation
+ * by 16 */
+
+ /* *** End Weak Signal Processing ********************************** */
+ /* ***************************************************************** */
+}
diff --git a/drivers/media/radio/stfm1000/stfm1000-filter.h b/drivers/media/radio/stfm1000/stfm1000-filter.h
new file mode 100644
index 000000000000..d24e3e9244b8
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-filter.h
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#ifndef STFM1000_FILTER_H
+#define STFM1000_FILTER_H
+
+/* STFM1000 Black Box Filter parameters */
+struct stfm1000_filter_parms {
+ s16 LprXzz; /* LPR x(n-2) stereo filter */
+ s16 LmrXzz; /* LMR x(n-2) stereo filter */
+ s16 LprYzz; /* LPR y(n-2) stereo filter */
+ s16 LmrYzz; /* LMR y(n-2) stereo filter */
+
+ s16 LprXz; /* LPR x(n-1) stereo filter */
+ s16 LmrXz; /* LMR x(n-1) stereo filter */
+ s16 FilteredLpr; /* LPR filter output */
+ s16 FilteredLmr; /* LMR filter output */
+
+ s16 LprB0; /* LPR stereo filter coef */
+ s16 LprB1over2; /* LPR stereo filter coef */
+ s16 LprA1over2; /* LPR stereo filter coef */
+ s16 LprA2; /* LPR stereo filter coef */
+
+ s16 LmrB0; /* LMR stereo filter coef */
+ s16 LmrB1over2; /* LMR stereo filter coef */
+ s16 LmrA1over2; /* LMR stereo filter coef */
+ s16 LmrA2; /* LMR stereo filter coef */
+
+ s16 LprYz; /* LPR y(n-1) stereo filter */
+ s16 LmrYz; /* LMR y(n-1) stereo filter */
+
+ s16 Left; /* left channel audio out */
+ s16 Right; /* right channel audio out */
+ s32 LeftLb; /* left channel dc estimate */
+ s32 RightLb; /* right channel dc estimate */
+
+ u32 RssiDecoded; /* integer decoded RSSI */
+
+ u16 RssiMant; /* mantissa of float-coded RSSI */
+ s16 RssiLog; /* 10log10(decoded RSSI) */
+
+ u16 RssiExp; /* exponent of float-coded RSSI */
+ u16 RssiLb; /* leaky bucket dc of rssi */
+
+ u16 Prssi; /* power of 2 for RSSI */
+ u16 TrueRssi; /* DC estimate of log RSSI */
+
+ u16 ScaledRssiDecoded; /* scaled log RSSI */
+ s16 Echo; /* Echo info from HiPass(RSSI) */
+ u16 ScaledRssiDecodedZ; /* history buffer for above */
+ u16 ScaledRssiDecodedZz;/* ditto */
+
+ u16 ScaledTrueRssi; /* scaled version for precision */
+ u16 FilteredRssi; /* Attack/Decay filtered RSSI */
+ u16 PrevFilteredRssi; /* previous version of above */
+
+ u16 EchoLb; /* DC estimate of Echo energy */
+ u16 TrueEcho; /* scaled version of above */
+ u16 FilteredEchoLpr; /* Attack/Decay filt. Echo */
+ u16 PrevFilteredEchoLpr;/* previous version of above */
+ u16 FilteredEchoLmr; /* Attack/Decay filt. Echo */
+ u16 PrevFilteredEchoLmr;/* previous version of above */
+ s16 GatedEcho; /* Echo gated by threshold */
+
+ s16 ControlLpr; /* master control for LPR */
+ s16 ControlLmr; /* master control for LMR */
+ u16 LprBw; /* LPR Bandwidth desired */
+ u16 LmrBw; /* LMR Bandwidth desired */
+ u16 LprGa; /* LPR Gain (SoftMute) desired */
+ u16 LmrGa; /* LMR Gain (Blend) desired */
+ u16 ScaledControlLmr; /* Scaled down version Ctl LMR */
+ u16 ScaledControlLpr; /* Scaled down version Ctl LPR */
+
+ s16 B0M; /* BW ctl B0 coef slope */
+ s16 B0B; /* BW ctl B0 coef y-intercept */
+
+ u16 B0S; /* BW ctl B0 coef scale */
+ s16 B1over2M; /* BW ctl B1/2 coef slope */
+
+ s16 B1over2B; /* BW ctl B1/2 coef y-intercept */
+ s16 A1over2B; /* BW ctl A1/2 coef y-intercept */
+
+ u16 B1over2S; /* BW ctl B1/2 coef scale */
+ u16 A1over2S; /* BW ctl A1/2 coef scale */
+
+ s16 A1over2M; /* BW ctl A1/2 coef slope */
+ u16 A2S; /* BW ctl A2 coef scale */
+
+ s16 A2M; /* BW ctl A2 coef slope */
+ s16 A2B; /* BW ctl A2 coef y-intercept */
+
+ u16 AdjBw; /* Desired Filter BW scaled into range */
+
+ u16 DecRssi; /*! Decimation modulo counter */
+
+ s16 ScaleAudio; /*! Scale factor for Audio Mute */
+ u8 MuteAudio; /*! Control for muting audio */
+ u8 PrevMuteAudio; /*! History of control for muting audio */
+ u8 MuteActionFlag; /*! Indicator of when mute ramping occurs */
+
+ u32 Acc; /* mimics H/W accumulator */
+ s32 Acc_signed;
+ s16 temp1_reg; /* mimics 16 bit register */
+ s16 temp2_reg; /* mimics 16 bit register */
+ s16 temp3_reg; /* mimics 16 bit register */
+ s16 temp4_reg; /* mimics 16 bit register */
+ s16 temp5_reg; /* mimics 16 bit register */
+
+ /* *** Programmable Coefficients */
+ u16 pCoefRssiAttack; /* prog coef RSSI attack */
+ u16 pCoefRssiDecay; /* prog coef RSSI decay */
+ u16 pCoefEchoLprAttack; /* prog coef Echo LPR attack */
+ u16 pCoefEchoLprDecay; /* prog coef Echo LPR decay */
+ u16 pCoefEchoLmrAttack; /* prog coef Echo LMR attack */
+ u16 pCoefEchoLmrDecay; /* prog coef Echo LMR decay */
+
+ u16 pCoefEchoTh; /* prog coef Echo threshold */
+
+ u16 pCoefEchoScLpr; /* prog coef scale Echo LPR infl. */
+ u16 pCoefEchoScLmr; /* prog coef scale Echo LMR infl. */
+ u16 pCoefEchoShLpr; /* prog coef shift Echo LPR infl. */
+ u16 pCoefEchoShLmr; /* prog coef shift Echo LMR infl. */
+
+ u16 pCoefLprBwThLo; /* prog coef Low Th LPR BW */
+ u16 pCoefLprBwThHi; /* prog coef High Th LPR BW */
+ u16 pCoefLmrBwThLo; /* prog coef Low Th LMR BW */
+ u16 pCoefLmrBwThHi; /* prog coef High Th LMR BW */
+
+ u16 pCoefLprGaTh; /* prog coef Th LPR Gain (SoftMute) */
+ u16 pCoefLmrGaTh; /* prog coef Th LMR Gain (Blend) */
+
+ u16 pCoefLprBwSlSc; /* prog coef Slope scale LPR BW */
+ u16 pCoefLprBwSlSh; /* prog coef Slope shift LPR BW */
+ u16 pCoefLmrBwSlSc; /* prog coef Slope scale LMR BW */
+ u16 pCoefLmrBwSlSh; /* prog coef Slope shift LMR BW */
+ u16 pCoefLprGaSlSc; /* prog coef Slope scale LPR Gain */
+ u16 pCoefLprGaSlSh; /* prog coef Slope shift LPR Gain */
+
+ u8 pCoefForcedMono; /* Forced Mono control bit */
+ u8 pCoefBypassBlend; /* Forced bypass of stereo blend */
+ u8 pCoefBypassSoftmute; /* Forced bypass of softmute */
+ u8 pCoefBypassDcCut; /* Forced bypass of audio DC Cut filter */
+
+ u8 pCoefBypassBwCtl; /* Forced bypass of bandwidth control */
+ u8 pCoefForceLockLmrBw; /* prog flag to force LMR BW=LPR BW */
+
+ /* XXX added here, they were global */
+ s16 temp2_reg_sm;
+ s16 temp3_reg_sm;
+
+};
+
+/* STFM1000 Black Box Filter Function prototypes */
+void stfm1000_filter_reset(struct stfm1000_filter_parms *sdf);
+void stfm1000_filter_decode(struct stfm1000_filter_parms *sdf, s16 Lpr,
+ s16 Lmr, u16 Rssi);
+
+static inline s16
+stfm1000_filter_value_left(struct stfm1000_filter_parms *sdf)
+{
+ return sdf->Left;
+}
+
+static inline s16
+stfm1000_filter_value_right(struct stfm1000_filter_parms *sdf)
+{
+ return sdf->Right;
+}
+
+static inline u32
+stfm1000_filter_value_rssi(struct stfm1000_filter_parms *sdf)
+{
+ return sdf->RssiDecoded;
+}
+
+#endif
diff --git a/drivers/media/radio/stfm1000/stfm1000-i2c.c b/drivers/media/radio/stfm1000/stfm1000-i2c.c
new file mode 100644
index 000000000000..5ddeb3a5fd3a
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-i2c.c
@@ -0,0 +1,452 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#include <linux/io.h>
+#include <linux/videodev2.h>
+#include <media/v4l2-common.h>
+#include <linux/i2c.h>
+#include <linux/delay.h>
+
+#include <linux/version.h> /* for KERNEL_VERSION MACRO */
+
+#include "stfm1000.h"
+
+#define stfm1000_i2c_debug(p, fmt, arg...) \
+ do { \
+ if ((p)->dbgflg & STFM1000_DBGFLG_I2C) \
+ printk(KERN_INFO "stfm1000: " fmt, ##arg); \
+ } while (0)
+
+static const char *reg_names[STFM1000_NUM_REGS] = {
+#undef REGNAME
+#define REGNAME(x) \
+ [STFM1000_ ## x / 4] = #x ""
+
+ REGNAME(TUNE1),
+ REGNAME(SDNOMINAL),
+ REGNAME(PILOTTRACKING),
+ REGNAME(INITIALIZATION1),
+ REGNAME(INITIALIZATION2),
+ REGNAME(INITIALIZATION3),
+ REGNAME(INITIALIZATION4),
+ REGNAME(INITIALIZATION5),
+ REGNAME(INITIALIZATION6),
+ REGNAME(REF),
+ REGNAME(LNA),
+ REGNAME(MIXFILT),
+ REGNAME(CLK1),
+ REGNAME(CLK2),
+ REGNAME(ADC),
+ REGNAME(AGC_CONTROL1),
+ REGNAME(AGC_CONTROL2),
+ REGNAME(DATAPATH),
+ REGNAME(RMS),
+ REGNAME(AGC_STAT),
+ REGNAME(SIGNALQUALITY),
+ REGNAME(DCEST),
+ REGNAME(RSSI_TONE),
+ REGNAME(PILOTCORRECTION),
+ REGNAME(ATTENTION),
+ REGNAME(CLK3),
+ REGNAME(CHIPID),
+#undef REGNAME
+};
+
+static const int stfm1000_rw_regs[] = {
+ STFM1000_TUNE1,
+ STFM1000_SDNOMINAL,
+ STFM1000_PILOTTRACKING,
+ STFM1000_INITIALIZATION1,
+ STFM1000_INITIALIZATION2,
+ STFM1000_INITIALIZATION3,
+ STFM1000_INITIALIZATION4,
+ STFM1000_INITIALIZATION5,
+ STFM1000_INITIALIZATION6,
+ STFM1000_REF,
+ STFM1000_LNA,
+ STFM1000_MIXFILT,
+ STFM1000_CLK1,
+ STFM1000_CLK2,
+ STFM1000_ADC,
+ STFM1000_AGC_CONTROL1,
+ STFM1000_AGC_CONTROL2,
+ STFM1000_DATAPATH,
+ STFM1000_ATTENTION, /* it's both WR/RD */
+};
+
+static const int stfm1000_ra_regs[] = {
+ STFM1000_RMS,
+ STFM1000_AGC_STAT,
+ STFM1000_SIGNALQUALITY,
+ STFM1000_DCEST,
+ STFM1000_RSSI_TONE,
+ STFM1000_PILOTCORRECTION,
+ STFM1000_ATTENTION, /* it's both WR/RD - always read */
+ STFM1000_CLK3,
+ STFM1000_CHIPID
+};
+
+static int verify_writes;
+
+void stfm1000_setup_reg_set(struct stfm1000 *stfm1000)
+{
+ int i, reg;
+
+ /* set up register sets (read/write) */
+ for (i = 0; i < ARRAY_SIZE(stfm1000_rw_regs); i++) {
+ reg = stfm1000_rw_regs[i] / 4;
+ stfm1000->reg_rw_set[reg / 32] |= 1U << (reg & 31);
+ /* printk(KERN_INFO "STFM1000: rw <= %d\n", reg); */
+ }
+
+ /* for (i = 0; i < ARRAY_SIZE(stfm1000->reg_rw_set); i++)
+ printk("RW[%d] = 0x%08x\n", i, stfm1000->reg_rw_set[i]); */
+
+ /* set up register sets (read only) */
+ for (i = 0; i < ARRAY_SIZE(stfm1000_ra_regs); i++) {
+ reg = stfm1000_ra_regs[i] / 4;
+ stfm1000->reg_ra_set[reg / 32] |= 1U << (reg & 31);
+ /* printk(KERN_INFO "STFM1000: rw <= %d\n", reg); */
+ }
+ /* for (i = 0; i < ARRAY_SIZE(stfm1000->reg_ra_set); i++)
+ printk("RO[%d] = 0x%08x\n", i, stfm1000->reg_ra_set[i]); */
+
+ /* clear dirty */
+ memset(stfm1000->reg_dirty_set, 0, sizeof(stfm1000->reg_dirty_set));
+}
+
+static int stfm1000_reg_is_rw(struct stfm1000 *stfm1000, int reg)
+{
+ reg >>= 2;
+ return !!(stfm1000->reg_rw_set[reg / 32] & (1 << (reg & 31)));
+}
+
+static int stfm1000_reg_is_ra(struct stfm1000 *stfm1000, int reg)
+{
+ reg >>= 2;
+ return !!(stfm1000->reg_ra_set[reg / 32] & (1 << (reg & 31)));
+}
+
+static int stfm1000_reg_is_dirty(struct stfm1000 *stfm1000, int reg)
+{
+ reg >>= 2;
+ return !!(stfm1000->reg_dirty_set[reg / 32] & (1 << (reg & 31)));
+}
+
+static void stfm1000_reg_set_dirty(struct stfm1000 *stfm1000, int reg)
+{
+ reg >>= 2;
+ stfm1000->reg_dirty_set[reg / 32] |= 1 << (reg & 31);
+}
+
+static inline int stfm1000_reg_is_writeable(struct stfm1000 *stfm1000, int reg)
+{
+ return stfm1000_reg_is_rw(stfm1000, reg);
+}
+
+static inline int stfm1000_reg_is_readable(struct stfm1000 *stfm1000, int reg)
+{
+ return stfm1000_reg_is_rw(stfm1000, reg) ||
+ stfm1000_reg_is_ra(stfm1000, reg);
+}
+
+/********************************************************/
+
+static int write_reg_internal(struct stfm1000 *stfm1000, int reg, u32 value)
+{
+ u8 values[5];
+ int ret;
+
+ stfm1000_i2c_debug(stfm1000, "%s(%s - 0x%02x, 0x%08x)\n", __func__,
+ reg_names[reg / 4], reg, value);
+
+ values[0] = (u8)reg;
+ values[1] = (u8)value;
+ values[2] = (u8)(value >> 8);
+ values[3] = (u8)(value >> 16);
+ values[4] = (u8)(value >> 24);
+ ret = i2c_master_send(stfm1000->client, values, 5);
+ if (ret < 0)
+ return ret;
+ return 0;
+}
+
+static int read_reg_internal(struct stfm1000 *stfm1000, int reg, u32 *value)
+{
+ u8 regb = reg;
+ u8 values[4];
+ int ret;
+
+ ret = i2c_master_send(stfm1000->client, &regb, 1);
+ if (ret < 0)
+ goto out;
+ ret = i2c_master_recv(stfm1000->client, values, 4);
+ if (ret < 0)
+ goto out;
+ *value = (u32)values[0] | ((u32)values[1] << 8) |
+ ((u32)values[2] << 16) | ((u32)values[3] << 24);
+ ret = 0;
+
+ stfm1000_i2c_debug(stfm1000, "%s(%s - 0x%02x, 0x%08x)\n", __func__,
+ reg_names[reg / 4], reg, *value);
+out:
+ return ret;
+}
+
+int stfm1000_raw_write(struct stfm1000 *stfm1000, int reg, u32 value)
+{
+ int ret;
+
+ mutex_lock(&stfm1000->xfer_lock);
+ ret = write_reg_internal(stfm1000, reg, value);
+ mutex_unlock(&stfm1000->xfer_lock);
+
+ if (ret < 0)
+ dev_err(&stfm1000->client->dev, "%s: failed", __func__);
+
+ return ret;
+}
+
+int stfm1000_raw_read(struct stfm1000 *stfm1000, int reg, u32 *value)
+{
+ int ret;
+
+ mutex_lock(&stfm1000->xfer_lock);
+ ret = read_reg_internal(stfm1000, reg, value);
+ mutex_unlock(&stfm1000->xfer_lock);
+
+ if (ret < 0)
+ dev_err(&stfm1000->client->dev, "%s: failed", __func__);
+
+ return ret;
+}
+
+static inline void stfm1000_set_shadow_reg(struct stfm1000 *stfm1000,
+ int reg, u32 val)
+{
+ stfm1000->shadow_regs[reg / 4] = val;
+}
+
+static inline u32 stfm1000_get_shadow_reg(struct stfm1000 *stfm1000, int reg)
+{
+ return stfm1000->shadow_regs[reg / 4];
+}
+
+int stfm1000_write(struct stfm1000 *stfm1000, int reg, u32 value)
+{
+ int ret;
+
+ if (!stfm1000_reg_is_writeable(stfm1000, reg)) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ mutex_lock(&stfm1000->xfer_lock);
+
+ /* same value as last one written? */
+ if (stfm1000_reg_is_dirty(stfm1000, reg) &&
+ stfm1000_get_shadow_reg(stfm1000, reg) == value) {
+ ret = 0;
+
+ stfm1000_i2c_debug(stfm1000, "%s - HIT "
+ "(%s - 0x%02x, 0x%08x)\n", __func__,
+ reg_names[reg / 4], reg, value);
+
+ goto out_unlock;
+ }
+
+ /* actually write the register */
+ ret = write_reg_internal(stfm1000, reg, value);
+ if (ret < 0)
+ goto out_unlock;
+
+ /* update shadow register & mark it as dirty */
+ /* only if register is not read always */
+ if (!stfm1000_reg_is_ra(stfm1000, reg)) {
+ stfm1000_set_shadow_reg(stfm1000, reg, value);
+ stfm1000_reg_set_dirty(stfm1000, reg);
+ }
+
+out_unlock:
+ mutex_unlock(&stfm1000->xfer_lock);
+
+out:
+ if (ret < 0)
+ dev_err(&stfm1000->client->dev, "%s: failed", __func__);
+
+ if (verify_writes) {
+ u32 value2 = ~0;
+
+ stfm1000_raw_read(stfm1000, reg, &value2);
+
+ stfm1000_i2c_debug(stfm1000, "%s - VER "
+ "(%s - 0x%02x, W=0x%08x V=0x%08x) %s\n", __func__,
+ reg_names[reg / 4], reg, value, value2,
+ value == value2 ? "OK" : "** differs **");
+ }
+
+ return ret;
+}
+
+int stfm1000_read(struct stfm1000 *stfm1000, int reg, u32 *value)
+{
+ int ret = 0;
+
+ if (!stfm1000_reg_is_readable(stfm1000, reg)) {
+ ret = -EINVAL;
+ printk(KERN_INFO "%s: !readable(%d)\n", __func__, reg);
+ goto out;
+ }
+
+ mutex_lock(&stfm1000->xfer_lock);
+
+ /* if the register can be written & is dirty, use the shadow */
+ if (stfm1000_reg_is_writeable(stfm1000, reg) &&
+ stfm1000_reg_is_dirty(stfm1000, reg)) {
+
+ *value = stfm1000_get_shadow_reg(stfm1000, reg);
+ ret = 0;
+
+ stfm1000_i2c_debug(stfm1000, "%s - HIT "
+ "(%s - 0x%02x, 0x%08x)\n", __func__,
+ reg_names[reg / 4], reg, *value);
+
+ goto out_unlock;
+ }
+
+ /* register must be read */
+ ret = read_reg_internal(stfm1000, reg, value);
+ if (ret < 0)
+ goto out;
+
+ /* if the register is writeable, update shadow */
+ if (stfm1000_reg_is_writeable(stfm1000, reg)) {
+ stfm1000_set_shadow_reg(stfm1000, reg, *value);
+ stfm1000_reg_set_dirty(stfm1000, reg);
+ }
+
+out_unlock:
+ mutex_unlock(&stfm1000->xfer_lock);
+
+out:
+ if (ret < 0)
+ dev_err(&stfm1000->client->dev, "%s: failed", __func__);
+
+ return ret;
+}
+
+int stfm1000_write_masked(struct stfm1000 *stfm1000, int reg, u32 value,
+ u32 mask)
+{
+ int ret = 0;
+ u32 old_value;
+
+ if (!stfm1000_reg_is_writeable(stfm1000, reg)) {
+ ret = -EINVAL;
+ printk(KERN_ERR "%s: !writeable(%d)\n", __func__, reg);
+ goto out;
+ }
+
+ mutex_lock(&stfm1000->xfer_lock);
+
+ /* if the register wasn't written before, read it */
+ if (!stfm1000_reg_is_dirty(stfm1000, reg)) {
+ ret = read_reg_internal(stfm1000, reg, &old_value);
+ if (ret != 0)
+ goto out_unlock;
+ } else /* register was written, use the last value */
+ old_value = stfm1000_get_shadow_reg(stfm1000, reg);
+
+ /* perform masking */
+ value = (old_value & ~mask) | (value & mask);
+
+ /* if we write the same value, don't bother */
+ if (stfm1000_reg_is_dirty(stfm1000, reg) && value == old_value) {
+ ret = 0;
+
+ stfm1000_i2c_debug(stfm1000, "%s - HIT "
+ "(%s - 0x%02x, 0x%08x)\n", __func__,
+ reg_names[reg / 4], reg, value);
+
+ goto out_unlock;
+ }
+
+ /* actually write the register to the chip */
+ ret = write_reg_internal(stfm1000, reg, value);
+ if (ret < 0)
+ goto out_unlock;
+
+ /* if no error, update the shadow register and mark it as dirty */
+ stfm1000_set_shadow_reg(stfm1000, reg, value);
+ stfm1000_reg_set_dirty(stfm1000, reg);
+
+out_unlock:
+ mutex_unlock(&stfm1000->xfer_lock);
+
+out:
+ if (ret < 0)
+ dev_err(&stfm1000->client->dev, "%s: failed", __func__);
+
+ if (verify_writes) {
+ u32 value2 = ~0;
+
+ stfm1000_raw_read(stfm1000, reg, &value2);
+
+ stfm1000_i2c_debug(stfm1000, "%s - VER "
+ "(%s - 0x%02x, W=0x%08x V=0x%08x) %s\n", __func__,
+ reg_names[reg / 4], reg, value, value2,
+ value == value2 ? "OK" : "** differs **");
+ }
+
+ return ret;
+}
+
+int stfm1000_set_bits(struct stfm1000 *stfm1000, int reg, u32 value)
+{
+ return stfm1000_write_masked(stfm1000, reg, value, value);
+}
+
+int stfm1000_clear_bits(struct stfm1000 *stfm1000, int reg, u32 value)
+{
+ return stfm1000_write_masked(stfm1000, reg, ~value, value);
+}
+
+int stfm1000_write_regs(struct stfm1000 *stfm1000,
+ const struct stfm1000_reg *reg)
+{
+ int ret;
+
+ for (; reg && reg->regno != STFM1000_REG_END; reg++) {
+
+ if (reg->regno == STFM1000_REG_DELAY) {
+ msleep(reg->value);
+ continue;
+ }
+
+ if (reg->regno & STFM1000_REG_SET_BITS_MASK)
+ ret = stfm1000_set_bits(stfm1000, reg->regno & 0xff,
+ reg->value);
+ else if (reg->regno & STFM1000_REG_CLEAR_BITS_MASK)
+ ret = stfm1000_clear_bits(stfm1000, reg->regno & 0xff,
+ reg->value);
+ else
+ ret = stfm1000_write(stfm1000, reg->regno, reg->value);
+
+ if (ret != 0) {
+ printk(KERN_ERR "%s: failed to write reg 0x%x "
+ "with 0x%08x\n",
+ __func__, reg->regno, reg->value);
+ return ret;
+ }
+ }
+ return 0;
+}
diff --git a/drivers/media/radio/stfm1000/stfm1000-rds.c b/drivers/media/radio/stfm1000/stfm1000-rds.c
new file mode 100644
index 000000000000..5f63052d00c2
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-rds.c
@@ -0,0 +1,1529 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#include <linux/init.h>
+
+#include "stfm1000.h"
+
+#include "stfm1000-rds.h"
+
+#define bitstream_to_rds_state(b) \
+ container_of(b, struct stfm1000_rds_state, bitstream)
+#define demod_to_rds_state(d) \
+ container_of(d, struct stfm1000_rds_state, demod)
+#define pkt_to_rds_state(p) \
+ container_of(p, struct stfm1000_rds_state, pkt)
+#define text_to_rds_state(t) \
+ container_of(t, struct stfm1000_rds_state, text)
+#define rds_state_to_stfm1000(r) \
+ container_of(r, struct stfm1000, rds_state)
+
+#define TADJSH 8 /*Shifts used in bitslice loop filter */
+
+/* Reverse of Matlab's Fquant (see MatchedFilterDecomposition.m), so that */
+/* mixandsum code is easy; Used by rds_bitstream_stfmdemod.arm */
+const s16 u16_rds_basis[2*RDS_BASISLENGTH+8] = {
+ 14, 24, 34, 43, 50, 56, 60, 62, 62,
+ 60, 55, 49, 41, 32, 22, 11, 14, 24,
+ 34, 43, 50, 56, 60, 62, 62, 60, 55,
+ 49, 41, 32, 22, 11, 14, 24, 34, 43,
+ 50, 56, 60, 62
+};
+
+static int bits_free(struct stfm1000_rds_bitstream *rdsb)
+{
+ /* Do not show the last one word free. */
+ int FreeSpace = rdsb->TailBitCount - rdsb->HeadBitCount - 32;
+
+ if (FreeSpace < 0)
+ FreeSpace = (RDS_BITBUFSIZE * 32) + FreeSpace;
+ return FreeSpace;
+}
+
+static void put1bit(struct stfm1000_rds_bitstream *rdsb, int bit)
+{
+ int index = (rdsb->HeadBitCount >> 5);
+ u32 CurBit = (rdsb->HeadBitCount & 0x1f);
+ u32 CurWord = rdsb->buf[index];
+
+ if (CurBit == 0)
+ CurWord = 0;
+
+ CurWord = CurWord | (((u32)bit & 1) << CurBit);
+ rdsb->buf[index] = CurWord;
+ rdsb->HeadBitCount++;
+ if (rdsb->HeadBitCount >= RDS_BITBUFSIZE * 32)
+ rdsb->HeadBitCount = 0;
+}
+
+static int get1bit(struct stfm1000_rds_bitstream *rdsb)
+{
+ int Bit = 0;
+ int index = (rdsb->TailBitCount >> 5);
+ int CurBit = (rdsb->TailBitCount & 0x1f);
+ u32 CurWord = rdsb->buf[index];
+
+ Bit = (CurWord >> CurBit) & 1;
+ rdsb->TailBitCount++;
+ if (rdsb->TailBitCount == RDS_BITBUFSIZE*32)
+ rdsb->TailBitCount = 0;
+
+ return Bit;
+}
+
+static int bits_filled(struct stfm1000_rds_bitstream *rdsb)
+{
+ int FilledSpace = rdsb->HeadBitCount - rdsb->TailBitCount;
+
+ if (FilledSpace < 0)
+ FilledSpace = (RDS_BITBUFSIZE * 32) + FilledSpace;
+ return FilledSpace;
+}
+
+static void rds_mix_msg(struct stfm1000_rds_demod *rdsd, u8 MixSetting)
+{
+ if (rdsd->mix_msg_pending)
+ rdsd->mix_msg_overrun++;
+ rdsd->mix_msg = MixSetting;
+ rdsd->mix_msg_pending = 1;
+
+ /* signal monitor thread */
+ stfm1000_monitor_signal(
+ rds_state_to_stfm1000(demod_to_rds_state(rdsd)),
+ EVENT_RDS_MIXFILT);
+}
+
+/* call with interrupts disabled please */
+int stfm1000_rds_mix_msg_get(struct stfm1000_rds_state *rds)
+{
+ struct stfm1000_rds_demod *rdsd = &rds->demod;
+
+ if (!rdsd->mix_msg_pending)
+ return -1;
+
+ return rdsd->mix_msg;
+}
+
+/* call with interrupts disabled please */
+int stfm1000_rds_mix_msg_processed(struct stfm1000_rds_state *rds, int mix_msg)
+{
+ struct stfm1000_rds_demod *rdsd = &rds->demod;
+
+ if (!rdsd->mix_msg_pending)
+ return -1;
+
+ rdsd->mix_msg_pending = 0;
+
+ /* update the completion indication bit */
+ if ((mix_msg & 0x8) == 0)
+ rdsd->MixPopDone = 1;
+
+ /* this is reflected off the hardware register */
+ rdsd->rds_mix_offset = mix_msg & 1;
+
+ if (rdsd->mix_msg != mix_msg) {
+ rdsd->mix_msg_processed_changed++;
+ return -1;
+ }
+ return 0;
+}
+
+static void rds_sdnominal_msg(struct stfm1000_rds_demod *rdsd, int sdnominal)
+{
+ if (rdsd->sdnominal_msg_pending)
+ rdsd->sdnominal_msg_overrun++;
+ rdsd->sdnominal_msg = sdnominal;
+ rdsd->sdnominal_msg_pending = 1;
+
+ /* signal monitor thread */
+ stfm1000_monitor_signal(
+ rds_state_to_stfm1000(demod_to_rds_state(rdsd)),
+ EVENT_RDS_SDNOMINAL);
+}
+
+/* call with interrupts disabled please */
+int stfm1000_rds_sdnominal_msg_get(struct stfm1000_rds_state *rds)
+{
+ struct stfm1000_rds_demod *rdsd = &rds->demod;
+
+ if (!rdsd->sdnominal_msg_pending)
+ return 0;
+
+ return rdsd->sdnominal_msg;
+}
+
+/* call with interrupts disabled please */
+int stfm1000_rds_sdnominal_msg_processed(struct stfm1000_rds_state *rds,
+ int sdnominal_msg)
+{
+ struct stfm1000_rds_demod *rdsd = &rds->demod;
+
+ if (!rdsd->sdnominal_msg_pending)
+ return -1;
+
+ rdsd->sdnominal_msg_pending = 0;
+ return 0;
+}
+
+void demod_loop(struct stfm1000_rds_bitstream *rdsb,
+ struct stfm1000_rds_demod *rdsd)
+{
+ s32 filter_out;
+ u32 freeSpace;
+ s32 decomp_hist_pp;
+ u8 phase;
+
+ /* Check if we're at a half-basis point */
+ if ((rdsd->i & (RDS_BASISLENGTH/2 - 1)) != 0)
+ return; /* Nope, return */
+
+ /* Yes, time to do our work */
+ /* Rotate the length 3 history buffer */
+ decomp_hist_pp = rdsd->decomp_hist_p;
+ rdsd->decomp_hist_p = rdsd->decomp_hist;
+ if ((rdsd->i & (RDS_BASISLENGTH-1)) == 0) {
+ rdsd->decomp_hist = rdsd->mixandsum1>>9; /* Grab output of
+ * mixandsum1/512 */
+ rdsd->mixandsum1 = 0; /* Reset mixandsum #1 */
+ } else {
+ rdsd->decomp_hist = rdsd->mixandsum2>>9; /*Grab output of
+ * mixandsum2/512 */
+ rdsd->mixandsum2 = 0; /* Reset mixandsum #2 */
+ }
+
+ /* Form correlator/decimator output by convolving with the
+ * decomposition coefficients, DecompQuant from Matlab work. */
+ filter_out = (-58*rdsd->decomp_hist + 59*decomp_hist_pp)>>7;
+
+ /*Figure out which half-basis we are in (out of a bit-length cycle) */
+ phase = rdsd->i*2/RDS_BASISLENGTH;
+ /*Now what we do depends on the phase variable */
+ /*Phase 0: Bitslice and do timing alignment */
+ /*others (1-3): Keep value for timing alignment */
+
+ if (phase == 0) { /*Main processing (bitslice) */
+ u32 Ph;
+ u8 OldBit = rdsd->sliced_data; /* Save the previous value */
+
+ rdsd->return_num = 1;
+ if (filter_out >= 0) { /*This bit is "1" */
+ /*return value is XOR of previous bit (still in
+ * sliced_data) w/ this */
+ /* bit (1), which equals (NOT of the previous bit) */
+ rdsd->return_rdsdemod = !OldBit;
+ rdsd->sliced_data = 1; /*Newest bit value is 1 */
+ } else { /*This bit is "0" */
+ /*return value is XOR of previous bit (still in
+ * sliced_data) w/ this */
+ /* bit (0), which equals the previous bit */
+ rdsd->return_rdsdemod = OldBit;
+ rdsd->sliced_data = 0; /*Newest bit value is 0 */
+ }
+
+ freeSpace = bits_free(rdsb);
+
+ if (freeSpace > 0)
+ put1bit(rdsb, rdsd->return_rdsdemod);
+ else
+ rdsd->RdsDemodSkippedBitCnt++;
+
+ /*Increment bits received counter */
+ rdsd->BitAlignmentCounter++;
+ /*If mixer phase determination hasn't been done, start it */
+ if ((rdsd->MixPhaseState == 0) && (!rdsd->MixPhaseDetInProg)) {
+ rdsd->MixPhaseDetInProg = 1;
+ /*Go to first mixer setting (0) */
+ rds_mix_msg(rdsd, 0);
+ }
+
+ /* Do bit-slicing time adaption after the mixer phase
+ * determination */
+ if (!(rdsd->MixPhaseDetInProg) && !(rdsd->Synchronous)) {
+
+ /* Bitslice Timing Adjust Code (runs after
+ * MixPhaseDetInProg and if RDS is not synchronous to
+ * the FM pilot. */
+
+ u8 BigPh2; /* Expecting a large value in
+ * PhaseValue[2] */
+ u32 MaxRMS = 0; /*Largest phase RMS */
+ s8 MaxPh = 0; /*Index of largest phase RMS */
+ s32 zerocross;
+
+ /* Locate the largest phase RMS
+ * (should be at phase zero) */
+ for (Ph = 0; Ph < 4; Ph++)
+ if (rdsd->Ph_RMS[Ph] > MaxRMS) {
+ MaxRMS = rdsd->Ph_RMS[Ph];
+ MaxPh = Ph;
+ }
+
+ /* During each bit time we expect the four phases to
+ * take one of the following patterns, where 1
+ * corresponds to maximum modulation:
+ * 1, 0, -1, 0 Case I
+ * -1, 0, 1, 0 Case II
+ * 1, 1/2, 0, -1/2 Case III
+ * -1, -1/2, 0, 1/2 Case IV
+ * We need to distinguish between cases in order to do
+ * the timing adjustment. Below we compare the
+ * correlation of the samples with Case I and Case III
+ * to see which has a bigger abs(correlation). Thus
+ * BigPh2, if set, means that we decided on Case I or
+ * Case II; if BigPh2 clear, we decided Case III or IV.
+ */
+ BigPh2 = abs(rdsd->PhaseValue[0]-rdsd->PhaseValue[2]) >
+ abs(rdsd->PhaseValue[0] +
+ ((rdsd->PhaseValue[1]-
+ rdsd->PhaseValue[3])>>1));
+ /* If BigPh2, use the difference between phase 1 value
+ * (downgoing for Case I, upgoing for Case II) and
+ * phase 3 value (upgoing for Case I, downgoing for
+ * Case II, thus the subtraction) to indicate timing
+ * error. If not BigPh2, use the sum of the phase 1
+ * value (downgoing for Case III, upgoing for Case IV)
+ * and phase 3 value (downgoing for Case III, upgoing
+ * for Case IV, thus the addition) to indicate timing
+ * error. If BigPh2, the slopes at phase 1 & phase 3
+ * are approximately double that if not BigPh2.
+ * Since we are trying to measure timing, scale
+ * by 1/2 in the BigPh2 case. */
+ if (BigPh2)
+ zerocross = (rdsd->PhaseValue[1]-
+ rdsd->PhaseValue[3])>>1;
+ else
+ zerocross = rdsd->PhaseValue[1]+
+ rdsd->PhaseValue[3];
+ /* Now if the prev bit was a "1", then the first zero
+ * crossing (phase 1 if BigPh2, phase 2 if !BigPh2)
+ * was a falling one, and if we were late then
+ * zerocross should be negative. If the prev bit was a
+ * "0", then the first zero crossing was a rising one,
+ * and if we were late then zerocross would be
+ * positive. If we are "late" it means that we need to
+ * do a shorter cycle of, say, 15 samples instead of
+ * 16, to "catch up" so that in the future we will be
+ * sampling earlier. We shorten the cycle by adding
+ * to i, so "late" is going to mean "increment i".
+ * Therefore "late" should be positive, which is done
+ * here by inverting zerocross if the previous bit was
+ * 1. You could say that this step reflects cases I
+ * and III into II and IV, respectively. */
+ if (OldBit)
+ zerocross = -zerocross;
+ if (!rdsd->DisablePushing) {
+ /*The algorithm so far has a stable operating
+ * point 17 phases away from the correct one.
+ * The following code is experimental and may
+ * be deleterious in low SNR conditions, but is
+ * an attempt to move off of the incorrect
+ * operating point. */
+
+ if (MaxPh != 0) {
+ /* If it isn't the same MaxPh as the
+ * last non-zero one, clear the counter
+ */
+ if (MaxPh != rdsd->PushLastMaxPh) {
+ /*Reset the counter */
+ rdsd->PushCounter = 0;
+ /*Record which phase we're now
+ * counting */
+ rdsd->PushLastMaxPh = MaxPh;
+ }
+ /* If the Max RMS is on the same
+ * non-zero phase, count up */
+ rdsd->PushCounter++;
+ }
+ /* Once every 128 bits, check and then reset
+ * PushCounter */
+ if (!(rdsd->BitAlignmentCounter & 0x0FF)) {
+ /*If 90% of the time the max phase has
+ * been from the same non-zero phase,
+ * decide that we are latched onto a 0
+ * lock point. Do a large push of the
+ * timing. */
+ if (rdsd->PushCounter > 230) {
+ s32 pshiph;
+ /*Convert from phase number to
+ * the number of filter
+ * output samples that we need
+ * to shift */
+ if (rdsd->PushLastMaxPh >= 2)
+ pshiph =
+ 4 - (s8)rdsd->
+ PushLastMaxPh;
+ else
+ pshiph =
+ -(s8)rdsd->
+ PushLastMaxPh;
+ /* Scale by the number of i-
+ * phases per output sample */
+ pshiph <<=
+ RDS_BASISSHIFTS-1;
+ /* Perform big pop to get near
+ * correct timing */
+ rdsd->i += (RDS_BASISLENGTH<<1)
+ + pshiph;
+ /* Set status indicating big
+ * pop was needed. Reset all
+ * leaky-bucket and summation
+ * variables because the big
+ * timing shift has invalidated
+ * them. Ph_RMS values don't
+ * need to be reset because
+ * they will shift over to
+ * reasonable values again
+ * before their erroneous
+ * values could have effect. */
+ rdsd->rds_big_timeshift = 1;
+ /*rdsd->Ph_RMS[0] = 0; */
+ /*rdsd->Ph_RMS[1] = 0; */
+ /*rdsd->Ph_RMS[2] = 0; */
+ /*rdsd->Ph_RMS[3] = 0; */
+ rdsd->mixandsum1 = 0;
+ rdsd->mixandsum2 = 0;
+ rdsd->SkipsAccum +=
+ pshiph;
+
+ /* Make adjustments in other
+ * values because of the push
+ * (they wouldn't otherwise be
+ * able to use the information
+ * that a push was needed in
+ * their future control
+ * decisions). */
+ if (rdsd->PushLastMaxPh != 2) {
+ /* If we weren't
+ * pushing from phase
+ * two, accumulate (for
+ * use in adapting
+ * SDNOMINAL) the
+ * phases moved by
+ * pushing. Phase two
+ * pushes are not used;
+ * the push direction
+ * is arbitrary since
+ * Phase 2 is 180
+ * degrees out. Also,
+ * phase 2 pushes don't
+ * result from
+ * reasonable slippage.
+ * */
+
+ if (rdsd->sdnom_adapt)
+ rdsd->SdnomSk
+ += pshiph;
+
+ /* Modify timing_adj to
+ * account for half of
+ * the DC response that
+ * would have occurred
+ * in timing_adj if
+ * that control loop
+ * had seen the push
+ * happen. (Why half?
+ * Because the loop has
+ * already seen a
+ * history of zerocross
+ * values that heads it
+ * in the same
+ * direction as this
+ * adjustment, but may
+ * have seen as few as
+ * half of what it
+ * should have.) */
+ rdsd->timing_adj +=
+ pshiph <<
+ (TADJSH+1);
+ }
+ /*Set countdown timer that will
+ * prevent any mixer popping
+ * until the Ph_RMS variables
+ * have had enough time to
+ * stabilize */
+
+ /* 2.5 time constants */
+ rdsd->PushSafetyZone = 5;
+ }
+ /*Reset the push counter */
+ rdsd->PushCounter = 0;
+ } /*end once every 128 bits */
+ } /*end if !DisablePushing */
+
+ /* Further possible additions:
+ *
+ * 1. Pushes modify timing_adj to decrease convergence
+ * time.
+ * 2. Separate timing_adj into pilottracking and non-pt
+ * cases (avoids convergence time after stereo/mono
+ * transitions)
+ *
+ * Old loop filter was a leaky bucket integrator, and
+ * it always lagged behind if the FM station had RDS
+ * asynchronous to the pilot, because the control loop
+ * needs another integrator to converge on a frequency
+ * error.
+ * New loop filter = 1/(1-1/z) * (a-1/z) * k,
+ * where a = 1+1/256 and k = 1/1024.
+ * You can narrow the loop bandwidth by making "a"
+ * twice as close to 1 and halving k, e.g. a = 1+1/512
+ * and k = 1/2048.
+ * (The way implemented, that narrowing loop BW by
+ * a factor of 2 can be done by incrementing TADJSH.)
+ *
+ * TGR 8/31/2007 */
+
+ /*Integrator, 1/(1-1/z) */
+ rdsd->timing_adj += zerocross;
+ /*Limit to 1 phase every 8 samples */
+ if (rdsd->SkipSafetyZone) {
+ rdsd->SkipSafetyZone--;
+ rdsd->sampskip = 0;
+ } else {
+ /*sampskip of non-zero is allowed,
+ * calculate what it really is */
+
+ /*Saturate timing_adj to 2's comp
+ * (2*TADJSH+4)-bit range. */
+ if (rdsd->timing_adj > (1<<(2*TADJSH+3))-1)
+ rdsd->timing_adj = (1<<(2*TADJSH+3))-1;
+ if (rdsd->timing_adj < -(1<<(2*TADJSH+3)))
+ rdsd->timing_adj = -(1<<(2*TADJSH+3));
+
+ /* Zero, implemented after the integrator
+ * output.
+ * (a-1/z) = (1+1/256) - 1/z = (1-1/z) + 1/256.
+ * But (1 - 1/z) is timing_adj-
+ * prev_timing_adj = zerocross. */
+ rdsd->sampskip = zerocross /* 1 - 1/z */
+ /* 1/256 (with rounding) */
+ + ((rdsd->timing_adj
+ + (1<<(TADJSH-1)))>>TADJSH);
+ /*Round and apply k */
+ rdsd->sampskip += (1<<(TADJSH+1));
+ rdsd->sampskip >>= (TADJSH+2);
+ /*Limit to [-1,+1] inclusive */
+ if (rdsd->sampskip > 1)
+ rdsd->sampskip = 1;
+ if (rdsd->sampskip < -1)
+ rdsd->sampskip = -1;
+ /* If non-zero, start the skip safety zone,
+ * which excludes more sample skipping for a
+ * while. Note that the safety zone only
+ * applies to the skips -- pushes can still
+ * happen inside a SkipSafetyZone. */
+ if (rdsd->sampskip)
+ rdsd->SkipSafetyZone = 8-1;
+ }
+ /**********************************************
+ * End Timing Adjust Code
+ **********************************************/
+
+ /**********************************************
+ * Begin Phase Popper Code
+ **********************************************/
+ /* If Phase Popping is enabled and 1/2 of a
+ * time constant has gone by... */
+ if (rdsd->PhasePoppingEnabled &&
+ !(rdsd->BitAlignmentCounter &
+ ((1<<(RMSALPHASHIFTS-1))-1))) {
+
+ u8 ForcePop = 0; /* Used to force a pop */
+
+ /*Record the maximum of the envelope */
+ if (MaxRMS > rdsd->PhasePopMaxRMS)
+ rdsd->PhasePopMaxRMS = MaxRMS;
+ /* Also track MaxRMS into MixPhase0/1Mag, so
+ * that we can see what the largest RMS on each
+ * of those phases is. On synchronous stations
+ * (meaning the RDS carrier and bit rate are
+ * synchronized with the pilot), the right mix
+ * phase will always be big and the wrong phase
+ * small. On asynchronous stations (and
+ * stations without RDS), both phases will at
+ * some time or other have about the
+ * same amplitude on each of the phases. */
+ if (rdsd->rds_mix_offset) {
+ if (MaxRMS > rdsd->MixPhase1Mag)
+ rdsd->MixPhase1Mag = MaxRMS;
+ } else {
+ if (MaxRMS > rdsd->MixPhase0Mag)
+ rdsd->MixPhase0Mag = MaxRMS;
+ }
+ /* Update PopSafetyZone and PushSafetyZone
+ * counters. With RMSALPHASHIFTS = 5, each
+ * tick is 16/1187.5 =~ 13.5 ms. */
+ if (rdsd->PopSafetyZone) {
+ rdsd->PopSafetyZone--;
+ /* If safety zone just ended and this
+ * mix phase is giving smaller RMS than
+ * before the pop, then the pop was a
+ * mistake. Go back to previous mixer
+ * phase */
+ if (!(rdsd->PopSafetyZone)
+ && (rdsd->PhasePopMaxRMS <
+ rdsd->PrePopRMS))
+ ForcePop = 1;
+ }
+ /* If there is no recent push, and Phase 0 has
+ * the maximum RMS, and at least 1/7th of a
+ * second has passed since the last phase pop,
+ * and ((the RMS is less than 1/2 of
+ * PhasePopMaxRMS) or (the RMS is less than
+ * 100)), then try a phase pop. */
+ if (/* (rdsd->Ph_RMS[0] == MaxRMS) &&
+ * Phase 0 has maximum RMS */
+ !(rdsd->PopSafetyZone)) {
+ /* and Long enough since last
+ * phase pop */
+
+ /* Eligible for a pop, see if one of
+ * the pop conditions is met */
+ if ((MaxRMS<<1) <
+ rdsd->PhasePopMaxRMS) {
+ /*RMS decline from its peak */
+ ForcePop = 1;
+ } else if ((MaxRMS>>RMSALPHASHIFTS)
+ < 50) {
+ /*RMS too small to receive,
+ * either there's no RDS or
+ * this is the wrong phase */
+ ForcePop = 1;
+ }
+ }
+ if (ForcePop) {
+
+ /*Pop to opposite setting */
+ rds_mix_msg(rdsd, 0x8 |
+ !rdsd->rds_mix_offset);
+
+ /*Save the pre-pop RMS so that later we
+ * can see if the pop was actually
+ * effective */
+ rdsd->PrePopRMS = MaxRMS;
+ /*Reset the PhasePopMaxRMS. We rely on
+ * the PopSafetyZone to give time to
+ * get a new valid max RMS before we're
+ * eligible for the next phase pop. If
+ * there were no reset we'd be forever
+ * incrementing PhasePopMaxRMS due
+ * to just happenstance large-noise
+ * samples and it might eventually get
+ * some freakish large value causing
+ * frequent erroneous pops. */
+ rdsd->PhasePopMaxRMS = 0;
+ /* Pop Safety zone length is decided by
+ * how much of an asynchronous
+ * frequency can be supported. Allowing
+ * 50 ppm of transmitter error (error
+ * between their own pilot, that we
+ * should be locked to, and their RDS
+ * carrier (which by RDS spec should be
+ * locked to their pilot, but we've
+ * recently found frequently isn't).
+ * 50ppm * 57kHz = 2.85Hz.
+ * (2.85 cycles/sec)(4 pops/cycle)
+ * = 11.4 pops/second.
+ * Safety zone = (1/11.4) seconds =~ 104
+ * bits, round down to 96 bits which
+ * yields 6 ticks if RMSALPHASHIFTS = 5.
+ * */
+ rdsd->PopSafetyZone = 96>>
+ (RMSALPHASHIFTS-1);
+ }
+ }
+ /******************************************************
+ * End Phase Popper Code
+ ******************************************************/
+
+ /* SDNOMINAL adaption */
+ if (rdsd->sdnom_adapt) {
+ rdsd->SdnomSk += rdsd->sampskip;
+ if (rdsd->pCoefForcedMono &&
+ (rdsd->BitAlignmentCounter & 0xFFF) ==
+ 0x800) {
+
+ rds_sdnominal_msg(rdsd,
+ -(rdsd->SdnomSk<<9));
+
+ /*Reset skips counter */
+ rdsd->SdnomSk = 0;
+ }
+ }
+
+ rdsd->SkipsAccum += rdsd->sampskip;
+ /* Once per 3.45 seconds, print out signal strength,
+ * skips and pops. Then reset the variables totalling
+ * those occurrences */
+ if (!(rdsd->BitAlignmentCounter & 0xFFF)) {
+ /* During very noisy input (or if no RDS, or no
+ * station present), timing_adj can go crazy,
+ * since it is the integral of noise. Although
+ * it is a saturated value (earlier, in the
+ * timing adjust code), the level at which we
+ * can saturate still leaves room for
+ * timing_adj to get too big. A large value of
+ * timing_adj is a persistent pathology because
+ * the phase is shifting so quickly that the
+ * push detector (which relies on stable
+ * phase-RMS values) never triggers, thus there
+ * is no implemented rescue besides this
+ * clearing that restores proper function. */
+ if (abs(rdsd->SkipsAccum) > 300)
+ rdsd->timing_adj = 0;
+ /*Reset the accumulations. */
+ rdsd->SkipsAccum = 0;
+ }
+ } /*End of bit timing adaption */
+
+ /* If mixer phase determination in progress,
+ * perform actions at certain times */
+ if (rdsd->MixPhaseDetInProg) {
+ /*~10ms settling time after mixer phase change */
+ #define MIXPHASE_STARTMEAS 12
+ /*~20ms measurement window */
+ #define MIXPHASE_ENDMEAS (MIXPHASE_STARTMEAS+24)
+ if (rdsd->BitAlignmentCounter == MIXPHASE_STARTMEAS) {
+ /*Reset the RMS variables */
+ rdsd->Ph_RMS[0] = 0;
+ rdsd->Ph_RMS[1] = 0;
+ rdsd->Ph_RMS[2] = 0;
+ rdsd->Ph_RMS[3] = 0;
+ /* Don't reset mixandsum values because at
+ * least they have filtered continuously. All
+ * we really need for the mixer phase decision
+ * is a constant measurement window. */
+ } else if (rdsd->BitAlignmentCounter ==
+ MIXPHASE_ENDMEAS) {
+ /*Measurement = mean of RMS values */
+ u32 Ndx, MeasVal = 0;
+ for (Ndx = 0; Ndx < 4;
+ MeasVal += rdsd->Ph_RMS[Ndx++]>>2);
+ /*Store measurement in correct place */
+ if (rdsd->MixPhaseState == 1) {
+ rdsd->MixPhase0Mag = MeasVal;
+ /*Go to next mixer setting */
+ rds_mix_msg(rdsd, 1);
+ } else if (rdsd->MixPhaseState == 2) {
+ u8 NextMixSetting;
+ rdsd->MixPhase1Mag = MeasVal;
+ /* Both measurements done now, see what
+ * mixer setting we need to use.
+ * 0 if MixPhase0Mag > MixPhase1Mag,
+ * 1 otherwise. */
+ NextMixSetting = (rdsd->MixPhase0Mag
+ <= rdsd->MixPhase1Mag);
+ /* If the mixer setting needed is 1,
+ * that is already the current setting.
+ * Terminate mixer phase determination.
+ * Otherwise send message to switch the
+ * mixer phase setting. */
+ if (NextMixSetting) {
+ rdsd->MixPhaseState = 3;
+ rdsd->MixPhaseDetInProg = 0;
+ } else
+ rds_mix_msg(rdsd, 0);
+ }
+ }
+ /* Reset BitAlignmentCounter if the Mixer just popped
+ * Change state, if required. States are:
+ * 0: Initial state, send msg causing RDS_MIXOFFSET=>0
+ * 1: Measure with RDS_MIXOFFSET = 0.
+ * Lasts just over 30 ms.
+ * 2: Measure with RDS_MIXOFFSET = 1.
+ * Lasts just over 30 ms.
+ * 3: At final RDS_MIXOFFSET value.
+ * Lasts as long as RDS continues. */
+ if (rdsd->MixPopDone) {
+ rdsd->MixPopDone = 0;
+ rdsd->BitAlignmentCounter = 0;
+ rdsd->MixPhaseState++; /*Go to next state */
+ /* If we got to state 3, turn off mixer phase
+ * determination code */
+ if (rdsd->MixPhaseState == 3)
+ rdsd->MixPhaseDetInProg = 0;
+ }
+ }
+
+ /* Update status variables */
+ rdsd->RDS_BIT_AMP_STAT_REG9 = rdsd->Ph_RMS[0]>>RMSALPHASHIFTS;
+ /*Saturate */
+ if (rdsd->RDS_BIT_AMP_STAT_REG9 > 511)
+ rdsd->RDS_BIT_AMP_STAT_REG9 = 511;
+ } /*End phase 0 code */
+
+ /***************************************************
+ * Actions common to all phases
+ ***************************************************/
+
+ /* Save the output of each phase for possible
+ * calculations during phase 0 */
+ rdsd->PhaseValue[phase] = filter_out;
+
+ /*So that we can measure signal amplitude and/or determine what (if */
+ /* any) big jump is needed, maintain the RMS of each phase. Phase */
+ /* 0 RMS is already in Ph_RMS[0] (see bitslicing code, earlier). */
+ rdsd->Ph_RMS[phase] += abs(filter_out) -
+ (rdsd->Ph_RMS[phase]>>RMSALPHASHIFTS);
+}
+
+#if defined(CONFIG_ARM)
+
+/* assembly version for ARM */
+#define RDS_MAC(_acc, _x, _y) \
+ __asm__ __volatile__ ( \
+ "smlabb %0, %1, %2, %0\n" \
+ : "=&r" (_acc) \
+ : "r" (_x), "r" (_y) \
+ : "cc")
+
+#else
+
+/* all others, use standard C */
+#define RDS_MAC(_acc, _x, _y) \
+ do { \
+ (_acc) += (s16)(_x) * (s16)(_y); \
+ } while (0)
+
+#endif
+
+static void rds_demod(const u16 *data, struct stfm1000_rds_demod *rdsd,
+ struct stfm1000_rds_bitstream *rbit, int total)
+{
+ register const s16 *basis0;
+ register const s16 *basis1;
+ register s16 val;
+ register int i;
+ register int sampskip;
+ register s32 acc1;
+ register s32 acc2;
+
+ /* point to the table */
+ basis0 = u16_rds_basis;
+ basis1 = basis0 + 8;
+
+ rdsd->return_num = 0;
+
+ /* restore state */
+ i = rdsd->i;
+ acc1 = rdsd->mixandsum1;
+ acc2 = rdsd->mixandsum2; /* 64 bit */
+ sampskip = rdsd->sampskip;
+
+ while (total-- > 0) {
+
+ val = data[3]; /* load RDS data */
+ data += 4;
+ if (val == 0x7fff) /* illegal RDS sample */
+ continue;
+
+ RDS_MAC(acc1, val, basis0[i]);
+ RDS_MAC(acc2, val, basis1[i]);
+
+ if (i == 4) {
+ i += sampskip;
+ sampskip = 0;
+ }
+
+ if ((i & (RDS_BASISLENGTH / 2 - 1)) == 0) {
+
+ /* save state */
+ rdsd->mixandsum1 = acc1;
+ rdsd->mixandsum2 = acc2;
+ rdsd->i = i;
+ rdsd->sampskip = sampskip;
+
+ demod_loop(rbit, rdsd);
+
+ /* restore state */
+ acc1 = rdsd->mixandsum1;
+ acc2 = rdsd->mixandsum2;
+ i = rdsd->i;
+ sampskip = rdsd->sampskip;
+ }
+ i = (i + 1) & 31;
+ }
+
+ /* save state */
+ rdsd->mixandsum1 = acc1;
+ rdsd->mixandsum2 = acc2;
+ rdsd->i = i;
+ rdsd->sampskip = sampskip;
+}
+
+void stfm1000_rds_demod(struct stfm1000_rds_state *rds, const u16 *dri_data,
+ int total)
+{
+ rds_demod(dri_data, &rds->demod, &rds->bitstream, total);
+
+ /* signal only when we have enough */
+ if (bits_filled(&rds->bitstream) > 128)
+ stfm1000_monitor_signal(rds_state_to_stfm1000(rds),
+ EVENT_RDS_BITS);
+}
+
+static void bitstream_reset(struct stfm1000_rds_bitstream *rdsb)
+{
+ memset(rdsb, 0, sizeof(*rdsb));
+}
+
+static void demod_reset(struct stfm1000_rds_demod *rdsd)
+{
+ memset(rdsd, 0, sizeof(*rdsd));
+ rdsd->sdnom_adapt = 0; /* XXX this doesn't really work right */
+ /* it causes underruns at ALSA */
+ rdsd->PhasePoppingEnabled = 1; /* does this? */
+}
+
+static void packet_reset(struct stfm1000_rds_pkt *rdsp)
+{
+ memset(rdsp, 0, sizeof(*rdsp));
+ rdsp->state = SYNC_OFFSET_A;
+}
+
+static void text_reset(struct stfm1000_rds_text *rdst)
+{
+ memset(rdst, 0, sizeof(*rdst));
+}
+
+void stfm1000_rds_reset(struct stfm1000_rds_state *rds)
+{
+ bitstream_reset(&rds->bitstream);
+ demod_reset(&rds->demod);
+ packet_reset(&rds->pkt);
+ text_reset(&rds->text);
+ rds->reset_req = 0;
+}
+
+int stfm1000_rds_bits_available(struct stfm1000_rds_state *rds)
+{
+ return bits_filled(&rds->bitstream);
+}
+
+int stmf1000_rds_get_bit(struct stfm1000_rds_state *rds)
+{
+ if (bits_filled(&rds->bitstream) == 0)
+ return -1;
+ return get1bit(&rds->bitstream);
+}
+
+int stmf1000_rds_avail_bits(struct stfm1000_rds_state *rds)
+{
+ return bits_filled(&rds->bitstream);
+}
+
+static const u32 rds_ParityCheck[] = {
+ 0x31B, 0x38F, 0x2A7, 0x0F7, 0x1EE,
+ 0x3DC, 0x201, 0x1BB, 0x376, 0x355,
+ 0x313, 0x39F, 0x287, 0x0B7, 0x16E,
+ 0x2DC, 0x001, 0x002, 0x004, 0x008,
+ 0x010, 0x020, 0x040, 0x080, 0x100,
+ 0x200
+};
+
+static int calc_syndrome(u32 rdscrc)
+{
+ int i;
+ u32 syndrome = 0;
+ int word = 0x1;
+
+ for (i = 0; i < 26; i++) {
+ if (rdscrc & word)
+ syndrome ^= rds_ParityCheck[i];
+ word <<= 1;
+ }
+ return syndrome;
+}
+
+static u32 ecc_table[1024];
+static int ecc_table_generated;
+
+static void generate_ecc_table(void)
+{
+ int i, j, size;
+ u32 syndrome, word;
+
+ for (i = 0; i < ECC_TBL_SIZE; i++)
+ ecc_table[i] = 0xFFFFFFFF;
+ ecc_table[0] = 0x0;
+
+ for (j = 0; j < 5; j++) {
+ word = (1 << (j + 1)) - 1; /* 0x01 0x03 0x07 0x0f 0x1f */
+ size = 26 - j; /* 26, 25, 24, 23, 22 */
+ syndrome = 0;
+ for (i = 0; i < size; i++) {
+ syndrome = calc_syndrome(word);
+ ecc_table[syndrome] = word;
+ word <<= 1;
+ }
+ }
+}
+
+static u32 ecc_correct(u32 rdsBits, int *recovered)
+{
+ u32 syndrome;
+ u32 errorBits;
+
+ if (recovered)
+ *recovered = 0;
+
+ /* Calculate Syndrome on Received Packet */
+ syndrome = calc_syndrome(rdsBits);
+
+ if (syndrome == 0)
+ return rdsBits; /* block is clean */
+
+ /* generate table first time we get here */
+ if (!ecc_table_generated) {
+ generate_ecc_table();
+ ecc_table_generated = 1;
+ }
+
+ /* Attempt to recover block */
+ errorBits = ecc_table[syndrome];
+ if (errorBits == UNRECOVERABLE_RDS_BLOCK)
+ return UNRECOVERABLE_RDS_BLOCK; /* Block can not be recovered.
+ * it is bad packet */
+
+ rdsBits = rdsBits ^ errorBits;
+ if (recovered)
+ (*recovered)++;
+ return rdsBits; /* ECC correct */
+}
+
+/* The following table lists the RDS and RBDS Program Type codes
+ * and their meanings:
+ * PTY code RDS Program type RBDS Program type */
+static const struct stfm1000_rds_pty stc_tss_pty_tab[] = {
+ { 0, "No program type", "No program type"},
+ { 1, "News", "News"},
+ { 2, "Current affairs", "Information"},
+ { 3, "Information", "Sports"},
+ { 4, "Sports", "Talk"},
+ { 5, "Education", "Rock"},
+ { 6, "Drama", "Classic Rock"},
+ { 7, "Culture", "Adult Hits"},
+ { 8, "Science", "Soft Rock"},
+ { 9, "Varied", "Top 40"},
+ { 10, "Pop", "Music Country"},
+ { 11, "Rock", "Music Oldies"},
+ { 12, "M.O.R.", "Music Soft"},
+ { 13, "Light classical", "Nostalgia"},
+ { 14, "Serious", "Classical Jazz"},
+ { 15, "Other Music", "Classical"},
+ { 16, "Weather", "Rhythm and Blues"},
+ { 17, "Finance", "Soft Rhythm and Blues"},
+ { 18, "Children's programs", "Language"},
+ { 19, "Social Affairs", "Religious Music"},
+ { 20, "Religion", "Religious Talk"},
+ { 21, "Phone In", "Personality"},
+ { 22, "Travel", "Public"},
+ { 23, "Leisure", "College"},
+ { 24, "Jazz Music", "Unassigned"},
+ { 25, "Country Music", "Unassigned"},
+ { 26, "National Music", "Unassigned"},
+ { 27, "Oldies Music", "Unassigned"},
+ { 28, "Folk Music", "Unassigned"},
+ { 29, "Documentary", "Weather"},
+ { 30, "Alarm Test", "Emergency Test"},
+ { 31, "Alarm", "Emergency"},
+};
+
+#if 0
+static const char *rds_group_txt[] = {
+ [RDS_GROUP_TYPE_0A] = "Basic tuning and switching information (0A)",
+ [RDS_GROUP_TYPE_0B] = "Basic tuning and switching information (0B)",
+ [RDS_GROUP_TYPE_1A] = "Program item number and slow labeling codes",
+ [RDS_GROUP_TYPE_1B] = "Program item number",
+ [RDS_GROUP_TYPE_2A] = "Radio Text (2A)",
+ [RDS_GROUP_TYPE_2B] = "Radio Text (2B)",
+ [RDS_GROUP_TYPE_3A] = "Application identification for ODA only",
+ [RDS_GROUP_TYPE_3B] = "Open data applications",
+ [RDS_GROUP_TYPE_4A] = "Clock-time and date",
+ [RDS_GROUP_TYPE_4B] = "Open data applications",
+ [RDS_GROUP_TYPE_5A] = "Transparent Data Channels (32 ch.) or ODA (5A)",
+ [RDS_GROUP_TYPE_5B] = "Transparent Data Channels (32 ch.) or ODA (5B)",
+ [RDS_GROUP_TYPE_6A] = "In House Applications or ODA (6A)",
+ [RDS_GROUP_TYPE_6B] = "In House Applications or ODA (6B)",
+ [RDS_GROUP_TYPE_7A] = "Radio Paging or ODA",
+ [RDS_GROUP_TYPE_7B] = "Open Data Applications",
+ [RDS_GROUP_TYPE_8A] = "Traffic Message Channel or ODA",
+ [RDS_GROUP_TYPE_8B] = "Open Data Applications",
+ [RDS_GROUP_TYPE_9A] = "Emergency warning system or ODA",
+ [RDS_GROUP_TYPE_9B] = "Open Data Applications",
+ [RDS_GROUP_TYPE_10A] = "Program Type Name",
+ [RDS_GROUP_TYPE_10B] = "Open Data Applications (10B)",
+ [RDS_GROUP_TYPE_11A] = "Open Data Applications (11A)",
+ [RDS_GROUP_TYPE_11B] = "Open Data Applications (11B)",
+ [RDS_GROUP_TYPE_12A] = "Open Data Applications (12A)",
+ [RDS_GROUP_TYPE_12B] = "Open Data Applications (12B)",
+ [RDS_GROUP_TYPE_13A] = "Enhanced Radio Paging or ODA",
+ [RDS_GROUP_TYPE_13B] = "Open Data Applications",
+ [RDS_GROUP_TYPE_14A] = "Enhanced Other Networks information (14A)",
+ [RDS_GROUP_TYPE_14B] = "Enhanced Other Networks information (14B)",
+ [RDS_GROUP_TYPE_15A] = "Defined in RBDS",
+ [RDS_GROUP_TYPE_15B] = "Fast switching information",
+};
+#endif
+
+static void dump_rds_packet(u8 *buf)
+{
+ u16 pi, offb;
+
+ pi = (u16)(buf[0] << 8) | buf[1];
+ offb = (u16)(buf[1] << 8) | buf[2];
+
+ printk(KERN_INFO "GRP: "
+ "PI=0x%04x "
+ "GT=%2d VER=%d TP=%d PTY=%2d "
+ "PS_SEG=%2d RT_AB=%2d RT_SEG=%2d\n", pi,
+ RDS_GROUP_TYPE(offb), RDS_VERSION(offb), RDS_TP(offb),
+ RDS_PTY(offb),
+ RDS_PS_SEG(offb), RDS_RT_AB(offb), RDS_RT_SEG(offb));
+}
+
+void stfm1000_rds_process_packet(struct stfm1000_rds_state *rds, u8 *buffer)
+{
+ struct stfm1000_rds_text *rdst = &rds->text;
+ /* char tempCallLetters[5] = {0}; */
+ struct rds_group_data grp;
+ int grpno;
+ u32 offset;
+ char tps[9];
+ int i, seg, idx;
+
+ grp.piCode = ((u16)buffer[0] << 8) | buffer[1];
+ grp.offsetB = ((u16)buffer[2] << 8) | buffer[3];
+ grp.offsetC = ((u16)buffer[4] << 8) | buffer[5];
+ grp.offsetD = ((u16)buffer[6] << 8) | buffer[7];
+
+ grpno = (grp.offsetB >> (8 + 3)) & 0x1f;
+
+ if (rds_state_to_stfm1000(rds)->rds_info)
+ dump_rds_packet(buffer);
+
+ /* Is this the first time through? */
+ if (!rdst->bRds_detected) {
+ rdst->pi = grp.piCode;
+ rdst->tp = RDS_TP(grp.offsetB);
+ rdst->version = RDS_VERSION(grp.offsetB);
+ rdst->pty.id = RDS_PTY(grp.offsetB);
+ rdst->pty.pRds = stc_tss_pty_tab[rdst->pty.id].pRds;
+ rdst->pty.pRdbs = stc_tss_pty_tab[rdst->pty.id].pRdbs;
+ rdst->bRds_detected = 1;
+ }
+
+ /* Have we process too many PI errors? */
+ if (grp.piCode != rdst->pi) {
+ if (rdst->mismatch++ > 10) {
+
+ /* requested reset of RDS */
+ rds->reset_req = 1;
+
+ /* signal monitor thread */
+ stfm1000_monitor_signal(rds_state_to_stfm1000(rds),
+ EVENT_RDS_RESET);
+
+ if (rds_state_to_stfm1000(rds)->rds_info)
+ printk(KERN_INFO "RDS: RESET!!!\n");
+
+ text_reset(rdst);
+ }
+ rdst->consecutiveGood = 0;
+ return;
+ }
+
+ if (rdst->consecutiveGood++ > 10)
+ rdst->mismatch = 0; /* reset bad count */
+
+ if (rdst->consecutiveGood > rdst->consecutiveGoodMax)
+ rdst->consecutiveGoodMax = rdst->consecutiveGood;
+
+ switch (grpno) {
+ case RDS_GROUP_TYPE_0A:
+ case RDS_GROUP_TYPE_0B:
+ /* Extract Service Name information */
+ offset = RDS_PS_SEG(grp.offsetB) * 2;
+ rdst->wk_ps[offset] = buffer[6]; /* better */
+ rdst->wk_ps[offset + 1] = buffer[7];
+ rdst->wk_ps_mask |= 1 << RDS_PS_SEG(grp.offsetB);
+
+ if (rds_state_to_stfm1000(rds)->rds_info) {
+ for (i = 0; i < 8; i++) {
+ if (rdst->wk_ps_mask & (1 << i)) {
+ tps[i * 2] =
+ rdst->wk_ps[i * 2];
+ tps[i * 2 + 1] =
+ rdst->wk_ps[i * 2 + 1];
+ } else {
+ tps[i * 2] = '_';
+ tps[i * 2 + 1] = '_';
+ }
+ }
+ tps[ARRAY_SIZE(tps) - 1] = '\0';
+ if (rds_state_to_stfm1000(rds)->rds_info)
+ printk(KERN_INFO "RDS-PS (curr): %s\n", tps);
+ }
+
+ if (rdst->wk_ps_mask != ALL_SEGMENT_BITS)
+ break;
+
+ if (rdst->ps_valid) {
+ if (memcmp(rdst->ps, rdst->wk_ps, 8) != 0) {
+ memset(rdst->cp_ps, 0, 8);
+ memset(rdst->wk_ps, 0, 8);
+ rdst->wk_ps_mask = 0;
+ }
+
+ memset(rdst->ps, 0, 8);
+ rdst->ps_valid = 0;
+ break;
+ }
+
+ /* does working buffer == compare buffer */
+ if (memcmp(rdst->cp_ps, rdst->wk_ps, 8) != 0) {
+ /* just copy from working to compare buffer */
+ memcpy(rdst->cp_ps, rdst->wk_ps, 8);
+ rdst->wk_ps_mask = 0;
+ break;
+ }
+
+ /* working buffer matches compare buffer, send to UI */
+ memcpy(rdst->ps, rdst->cp_ps, 8);
+ rdst->ps_valid = 1;
+
+ if (rds_state_to_stfm1000(rds)->rds_info)
+ printk(KERN_INFO "RDS: PS '%s'\n", rdst->ps);
+
+ /* clear working mask-only */
+ rdst->wk_ps_mask = 0;
+ break;
+
+ case RDS_GROUP_TYPE_2A:
+
+ /* Clear buffer */
+ if (rdst->textAB_flag != RDS_RT_AB(grp.offsetB)) {
+ memset(rdst->wk_text, 0, 64);
+ rdst->wk_text_mask = 0;
+ rdst->textAB_flag = RDS_RT_AB(grp.offsetB);
+ }
+
+ /* Extract Text */
+ seg = RDS_RT_SEG(grp.offsetB);
+ idx = seg * 4;
+
+ #define CNVT_EOT(x) ((x) != RDS_EOT ? (x) : 0)
+ rdst->wk_text[idx++] = CNVT_EOT(buffer[4]);
+ rdst->wk_text[idx++] = CNVT_EOT(buffer[5]);
+ rdst->wk_text[idx++] = CNVT_EOT(buffer[6]);
+ rdst->wk_text[idx++] = CNVT_EOT(buffer[7]);
+
+ rdst->wk_text_mask |= 1 << seg;
+ /* scan msg data for EOT. If found set all higher
+ * mask bits */
+ for (idx = 0; idx < 4; idx++) {
+ if (rdst->text[idx] == RDS_EOT)
+ break;
+ }
+ if (idx < 4) {
+ /* set current and all higher bits */
+ for (idx = RDS_RT_SEG(grp.offsetB); idx < 16;
+ idx++)
+ rdst->wk_text_mask |= 1 << idx;
+ }
+
+ /* Process buffer when filled */
+ if (rdst->wk_text_mask != ALL_TEXT_BITS)
+ break;
+
+ if (!rdst->text_valid)
+ rdst->text_valid = 1;
+ else if (memcmp(rdst->text, rdst->wk_text, 64) == 0)
+ break;
+
+ memcpy(rdst->text, rdst->wk_text, 64);
+
+ if (rds_state_to_stfm1000(rds)->rds_info)
+ printk(KERN_INFO "RDS: TEXT '%s'\n", rdst->text);
+
+ memset(rdst->wk_text, 0, 64);
+ rdst->wk_text_mask = 0;
+ break;
+
+ default:
+ break;
+ }
+}
+
+int stfm1000_rds_packet_dequeue(struct stfm1000_rds_state *rds, u8 *buf)
+{
+ struct stfm1000_rds_pkt *pkt = &rds->pkt;
+
+ if (pkt->buf_cnt == 0)
+ return -1;
+
+ memcpy(buf, &pkt->buf_queue[pkt->buf_tail][0], 8);
+ if (++pkt->buf_tail >= RDS_PKT_QUEUE)
+ pkt->buf_tail = 0;
+ pkt->buf_cnt--;
+
+ return 0;
+}
+
+void stfm1000_rds_packet_bit(struct stfm1000_rds_state *rds, int bit)
+{
+ struct stfm1000_rds_pkt *pkt = &rds->pkt;
+ u32 rdsdata, rdscrc, rdscrc_c, rdscrc_cp;
+ int correct, correct2, recovered, recovered2;
+ int RetVal;
+
+ /* Stick into shift register */
+ pkt->rdsstream = ((pkt->rdsstream << 1) | bit) & 0x03ffffff;
+ pkt->bitsinfifo++;
+ pkt->bitcount++;
+
+ /* wait for 26 bits of block */
+ if (pkt->bitsinfifo < 26)
+ return;
+
+ rdsdata = pkt->rdsstream & 0x03fffc00; /* 16 bits of Info. word */
+ rdscrc = pkt->rdsstream & 0x3ff; /* 10 bits of Checkword */
+
+ switch (pkt->state) {
+ case SYNC_OFFSET_A:
+
+ RetVal = calc_syndrome(pkt->rdsstream);
+
+ switch (RetVal) {
+ case RDS_SYNDROME_OFFSETA:
+ pkt->state = OFFSET_B;
+ break;
+ case RDS_SYNDROME_OFFSETB:
+ pkt->state = OFFSET_C_CP;
+ break;
+ case RDS_SYNDROME_OFFSETC:
+ pkt->state = OFFSET_D;
+ break;
+ case RDS_SYNDROME_OFFSETCP:
+ pkt->state = OFFSET_D;
+ break;
+ case RDS_SYNDROME_OFFSETD:
+ pkt->state = OFFSET_A;
+ break;
+ default:
+ pkt->state = SYNC_OFFSET_A;
+ break;
+ }
+ if (pkt->state == SYNC_OFFSET_A) {
+ pkt->sync_lost_packets++;
+ /* XXX send info? */
+ break;
+ }
+
+ pkt->good_packets++;
+
+ rdsdata = pkt->rdsstream & 0x03fffc00;
+
+ /* Save type A packet in buffer */
+ rdsdata >>= 10;
+ pkt->buffer[0] = (rdsdata >> 8);
+ pkt->buffer[1] = (rdsdata & 0xff);
+ pkt->bitsinfifo = 0;
+
+ /* We found a block with zero errors, but it is not at the
+ * start of the group. */
+ if (pkt->state == OFFSET_B)
+ pkt->discardpacket = 0;
+ else
+ pkt->discardpacket = 1;
+ break;
+
+ case OFFSET_A: /* Type A: we are in sync now */
+ rdscrc ^= RDS_OFFSETA;
+ correct = ecc_correct(rdsdata | rdscrc, &recovered);
+ if (correct == UNRECOVERABLE_RDS_BLOCK) {
+ pkt->bad_packets++;
+ pkt->discardpacket++;
+ pkt->state++;
+ pkt->bitsinfifo = 0;
+ break;
+ }
+
+ if (recovered)
+ pkt->recovered_packets++;
+ pkt->good_packets++;
+
+ /* Attempt to see, if we can get the entire group.
+ * Don't discard. */
+ pkt->discardpacket = 0;
+ rdsdata = correct & 0x03fffc00;
+
+ /* Save type A packet in buffer */
+ rdsdata >>= 10;
+ pkt->buffer[0] = (rdsdata >> 8);
+ pkt->buffer[1] = (rdsdata & 0xff);
+ pkt->bitsinfifo = 0;
+ pkt->state++;
+ break;
+
+ case OFFSET_B: /* Waiting for type B */
+ rdscrc ^= RDS_OFFSETB;
+ correct = ecc_correct(rdsdata | rdscrc, &recovered);
+ if (correct == UNRECOVERABLE_RDS_BLOCK) {
+ pkt->bad_packets++;
+ pkt->discardpacket++;
+ pkt->state++;
+ pkt->bitsinfifo = 0;
+ break;
+ }
+ if (recovered)
+ pkt->recovered_packets++;
+ pkt->good_packets++;
+
+ rdsdata = correct & 0x03fffc00;
+
+ /* Save type B packet in buffer */
+ rdsdata >>= 10;
+ pkt->buffer[2] = (rdsdata >> 8);
+ pkt->buffer[3] = (rdsdata & 0xff);
+ pkt->bitsinfifo = 0;
+ pkt->state++;
+ break;
+
+ case OFFSET_C_CP: /* Waiting for type C or C' */
+ rdscrc_c = rdscrc ^ RDS_OFFSETC;
+ rdscrc_cp = rdscrc ^ RDS_OFFSETCP;
+ correct = ecc_correct(rdsdata | rdscrc_c, &recovered);
+ correct2 = ecc_correct(rdsdata | rdscrc_cp, &recovered2);
+ if (correct == UNRECOVERABLE_RDS_BLOCK
+ && correct2 == UNRECOVERABLE_RDS_BLOCK) {
+ pkt->bad_packets++;
+ pkt->discardpacket++;
+ pkt->state++;
+ pkt->bitsinfifo = 0;
+ break;
+ }
+
+ if (recovered || recovered2)
+ pkt->recovered_packets++;
+ pkt->good_packets++;
+
+ if (correct == UNRECOVERABLE_RDS_BLOCK)
+ correct = correct2;
+
+ rdsdata = correct & 0x03fffc00;
+
+ /* Save type C packet in buffer */
+ rdsdata >>= 10;
+ pkt->buffer[4] = (rdsdata >> 8);
+ pkt->buffer[5] = (rdsdata & 0xff);
+ pkt->bitsinfifo = 0;
+ pkt->state++;
+ break;
+
+ case OFFSET_D: /* Waiting for type D */
+ rdscrc ^= RDS_OFFSETD;
+ correct = ecc_correct(rdsdata | rdscrc, &recovered);
+ if (correct == UNRECOVERABLE_RDS_BLOCK) {
+ pkt->bad_packets++;
+ pkt->discardpacket++;
+ pkt->state = OFFSET_A;
+ pkt->bitsinfifo = 0;
+ break;
+ }
+
+ if (recovered)
+ pkt->recovered_packets++;
+ pkt->good_packets++;
+
+ rdsdata = correct & 0x03fffc00;
+
+ /* Save type D packet in buffer */
+ rdsdata >>= 10;
+ pkt->buffer[6] = (rdsdata >> 8);
+ pkt->buffer[7] = (rdsdata & 0xff);
+
+ /* buffer it if all segments were ok */
+ if (pkt->discardpacket) {
+ /* We're still in sync, so back to state 1 */
+ pkt->state = OFFSET_A;
+ pkt->bitsinfifo = 0;
+ pkt->discardpacket = 0;
+ break;
+ }
+
+ pkt->state++;
+ /* fall-through */
+
+ case PACKET_OUT:
+ pkt->GroupDropOnce = 1;
+
+ /* queue packet */
+ if (pkt->buf_cnt < RDS_PKT_QUEUE) {
+ memcpy(&pkt->buf_queue[pkt->buf_head][0],
+ pkt->buffer, 8);
+ if (++pkt->buf_head >= RDS_PKT_QUEUE)
+ pkt->buf_head = 0;
+ pkt->buf_cnt++;
+ } else
+ pkt->buf_overruns++;
+
+ /* We're still in sync, so back to state 1 */
+ pkt->state = OFFSET_A;
+ pkt->bitsinfifo = 0;
+ pkt->discardpacket = 0;
+ break;
+
+ }
+
+ /* Lots of errors? If so, go back to resync mode */
+ if (pkt->discardpacket >= 10) {
+ pkt->state = SYNC_OFFSET_A; /* reset sync state */
+ pkt->bitsinfifo = 26; /* resync a bit faster */
+ }
+}
+
+/* GROUP_TYPE 0A-0B (buffer must have enough space for 9 bytes) */
+int stfm1000_rds_get_ps(struct stfm1000_rds_state *rds, u8 *buffer,
+ int bufsize)
+{
+ struct stfm1000_rds_text *rdst = &rds->text;
+
+ if (bufsize < 9)
+ return -1;
+
+ if (!rdst->ps_valid)
+ return -1;
+
+ memcpy(buffer, rdst->ps, 8);
+ buffer[8] = '\0';
+
+ return 8;
+}
+
+/* GROUP_TYPE 2A (buffer must have enough space for 65 bytes) */
+int stfm1000_rds_get_text(struct stfm1000_rds_state *rds, u8 *buffer,
+ int bufsize)
+{
+ struct stfm1000_rds_text *rdst = &rds->text;
+
+ if (bufsize < 9)
+ return -1;
+
+ if (!rdst->text_valid)
+ return -1;
+
+ memcpy(buffer, rdst->text, 64);
+ buffer[64] = '\0';
+
+ return 64;
+}
diff --git a/drivers/media/radio/stfm1000/stfm1000-rds.h b/drivers/media/radio/stfm1000/stfm1000-rds.h
new file mode 100644
index 000000000000..44b9a610f86e
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-rds.h
@@ -0,0 +1,364 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#ifndef STFM1000_RDS_H
+#define STFM1000_RDS_H
+
+#include <linux/types.h>
+
+/* log2(number of samples in a filter basis) */
+#define RDS_BASISSHIFTS 4
+
+/* number of samples in a filter basis */
+#define RDS_BASISLENGTH (1 << RDS_BASISSHIFTS)
+
+#define TIME_ADAPT_OVER 100
+
+/* 2^(-this) is the RMS leaky bucket time constant */
+#define RMSALPHASHIFTS 5
+
+#define PROCESS_RDS_BITS 128
+
+#define RDS_BITBUFSIZE 1024 /* was 128 */
+struct stfm1000_rds_bitstream {
+ u32 buf[RDS_BITBUFSIZE]; /* bit buffer */
+ int HeadBitCount; /* bit buffer head counter */
+ int TailBitCount; /* bit buffer tail counter */
+};
+
+struct stfm1000_rds_demod {
+ u32 mixandsum1; /* Accumulator for first
+ * basis filter */
+ u32 mixandsum2; /* Accumulator for 2nd
+ * basis filter */
+ u32 i; /* Phase Index, 32 phases per
+ * RDS bit */
+ u32 return_num; /* Set if there is a new RDS bit */
+ u32 BitAlignmentCounter; /* Counts bits for timing purposes */
+ int sampskip; /* Requested timing shift (on i) */
+
+ int DisablePushing; /* Disables phase push algorithm
+ * (phase push happens when Ph_RMS[x],
+ * x != 0, is consistently the maximum
+ * Ph_RMS) */
+ int MixPopDone; /* Last mixer phase set request is
+ * done */
+ u8 rds_big_timeshift; /* If set, indicates a push or large
+ * timing shift occurred */
+ int return_rdsdemod; /* Output, (most recent bit) XOR
+ * (prev bit) */
+ u32 RDS_BIT_AMP_STAT_REG9; /* Size of bit (RMS of RDS signal at
+ * bitslicing instant, typically 220
+ * to 270) */
+ s32 decomp_hist; /* Most recent basis filter output */
+ s32 decomp_hist_p; /* Previous basis filter output */
+ s32 PhaseValue[4]; /* Half-basis phase samples over the
+ * most recent bit */
+ u32 Ph_RMS[4]; /* RMS of the four half-basis phases */
+ s32 timing_adj; /* Timing loop leaky-bucket
+ * accumulator */
+ u32 MixPhase0Mag; /* Magnitude of RDS signal with RDS
+ * mixer phase 0 (from mixer phase
+ * determination) */
+ u32 MixPhase1Mag; /* Magnitude of RDS signal with RDS
+ * mixer phase 1 (from mixer phase
+ * determination) */
+ u32 PhasePopMaxRMS; /* Maximum RMS observed since last
+ * phase pop */
+ u32 PrePopRMS; /* Max of Ph_RMS array right before the
+ * most recent phase pop */
+ u8 MixPhaseState; /* State of RDS mixer phase
+ * determination state machine */
+ int MixPhaseDetInProg; /* Set if RDS mix phase determination
+ * is in progress */
+ int sliced_data; /* The most recent bit decision */
+ u8 PopSafetyZone; /* Countdown timer, holds off next
+ * phase pop after a recent one */
+ u8 PushSafetyZone; /* Countdown timer, holds off next
+ * phase pop after a timing push (b/c
+ * timing push resets Ph_RMS vars) */
+ u8 SkipSafetyZone; /* Countdown timer, holds off next
+ * phase skip (small timing adj) */
+ int Synchronous; /* RDS has been determined to be
+ * synchronous to pilot */
+ u8 PushLastMaxPh; /* The index at which Ph_RMS is
+ * maximum ("x" in the above two
+ * comments) */
+ s32 PushCounter; /* Counts instances of Ph_RMS[x], x!=0,
+ * being the maximum Ph_RMS */
+ s32 SkipsAccum; /* Accumulation of all timing skips
+ * since RDS demod started */
+ s32 SdnomSk; /* Skips counter used for SDNOMINAL
+ * adaption */
+
+ /* update this everytime it's changed & put it here */
+ unsigned int rds_mix_offset : 1;
+
+ unsigned int sdnom_adapt : 1;
+ unsigned int pCoefForcedMono : 1; /* copy of filter parameter */
+ unsigned int PhasePoppingEnabled : 1;
+
+ unsigned int mix_msg_pending : 1;
+ u8 mix_msg;
+ unsigned int mix_msg_overrun;
+ unsigned int mix_msg_processed_changed;
+
+ unsigned int sdnominal_msg_pending : 1;
+ int sdnominal_msg;
+ unsigned int sdnominal_msg_overrun;
+
+ u32 RdsDemodSkippedBitCnt; /* bit skipped by RDS demodulator due
+ * to unavailable space in buf[]
+ * (bit buffer) */
+};
+
+#define RDS_OFFSETA 0x0fc
+#define RDS_OFFSETB 0x198
+#define RDS_OFFSETC 0x168
+#define RDS_OFFSETCP 0x350
+#define RDS_OFFSETD 0x1b4
+
+#define RDS_SYNDROME_OFFSETA 0x3d8
+#define RDS_SYNDROME_OFFSETB 0x3d4
+#define RDS_SYNDROME_OFFSETC 0x25c
+#define RDS_SYNDROME_OFFSETCP 0x3cc
+#define RDS_SYNDROME_OFFSETD 0x258
+
+#define SYNC_OFFSET_A 0 /* default state */
+#define OFFSET_A 1
+#define OFFSET_B 2
+#define OFFSET_C_CP 3
+#define OFFSET_D 4
+#define PACKET_OUT 5
+
+#define ECC_TBL_SIZE 1024
+#define UNRECOVERABLE_RDS_BLOCK 0xffffffff
+
+#define RDS_PKT_QUEUE 16
+
+struct stfm1000_rds_pkt {
+ int state; /* Current state */
+ u32 rdsstream; /* Current RDS data */
+ u8 buffer[8]; /* temporary storage of RDS data */
+ int discardpacket; /* discard packet count */
+ int sync_lost_packets; /* sync lost */
+ int good_packets; /* good packet */
+ int bad_packets; /* bad packet */
+ int recovered_packets; /* recovered packet */
+ int bitsinfifo; /* bits count */
+ int GroupDropOnce; /* Send Group Drop Message once */
+ int bitcount; /* Counter for Number of Bits read */
+
+ /* queue the packets here */
+ int buf_overruns;
+ int buf_head;
+ int buf_tail;
+ int buf_cnt;
+ int buf_queue[RDS_PKT_QUEUE][8];
+};
+
+#define AUDIT 0
+#define ALL_SEGMENT_BITS 0xF
+#define ALL_TEXT_BITS 0xFFFF
+
+struct stfm1000_rds_pty {
+ u8 id; /* Program Type ID */
+ u8 *pRds; /* RDS description */
+ u8 *pRdbs; /* RDBS description */
+};
+
+struct stfm1000_rds_text {
+ u8 bRds_detected; /* Has the first packet come in yet? */
+ u16 pi; /* Program Identification Code (PI) */
+ struct stfm1000_rds_pty pty; /* Program Type (PTY)) */
+ u8 tp; /* Traffic Program (TP) identification
+ * code */
+ u8 ps[9]; /* Program Service Name Sent to UI */
+ u8 altFreq[2]; /* Alternate frequency (AF) */
+ u8 callLetters[5]; /* For US, stations call letters */
+
+ u8 text[65]; /* Radio Text A */
+
+ unsigned int version : 1; /* Is station broadcasting version
+ * A or B (B0) */
+ unsigned int ps_valid : 1; /* station name is valid */
+ unsigned int text_valid : 1; /* Text is valid */
+ unsigned int textAB_flag : 1; /* Current flag setting, reset if flag
+ * changes */
+
+ /*------------------Working area--------------------------- */
+ u8 cp_ps[8]; /* Compare buffer for PS */
+ u8 wk_ps[8]; /* Program Service buffer */
+ u8 wk_ps_mask; /* lower 4 bits must be set
+ * before copy */
+ u8 wk_text[64]; /* Radio Text buffer */
+ u16 wk_text_mask; /* all bits must be set before copy */
+
+ /*-------------------Counters------------------------------ */
+ u32 messages; /* total number of messages recieved */
+ u32 unsupported; /* call to unsupported group type */
+ u32 mismatch; /* Mismatched values */
+ u32 consecutiveGood; /* Consecutive good will clear bad */
+ u32 consecutiveGoodMax; /* Max counter for paramaters */
+};
+
+/* Maximum number of RDS groups described in the U.S. RBDS Standard. */
+#define MAX_RDS_GROUPS_SUPPORTED 32
+
+/* Common Constants */
+#define RDS_LINE_FEED 0xA
+#define RDS_EOT 0xD
+
+/* Offsets into OFFSETB */
+#define RDS_GROUP_TYPE(x) (((x) >> 12) & 0xF)
+#define RDS_VERSION(x) (((x) >> 11) & 0x1)
+#define RDS_TP(x) (((x) >> 10) & 0x1)
+#define RDS_PTY(x) (((x) >> 5) & 0x1F)
+#define RDS_PS_SEG(x) ((x) & 0x3)
+#define RDS_RT_AB(x) (((x) >> 4) & 0x1)
+#define RDS_RT_SEG(x) ((x) & 0xF)
+
+/* This values corresond to the Group Types defined */
+/* In the U.S. RBDS standard. */
+#define RDS_GROUP_TYPE_0A 0 /* Basic tuning and switching information */
+#define RDS_GROUP_TYPE_0B 1 /* Basic tuning and switching information */
+#define RDS_GROUP_TYPE_1A 2 /* Program item number and slow labeling
+ * codes */
+#define RDS_GROUP_TYPE_1B 3 /* Program item number */
+#define RDS_GROUP_TYPE_2A 4 /* Radio Text */
+#define RDS_GROUP_TYPE_2B 5 /* Radio Text */
+#define RDS_GROUP_TYPE_3A 6 /* Application identification for ODA
+ * only */
+#define RDS_GROUP_TYPE_3B 7 /* Open data applications */
+#define RDS_GROUP_TYPE_4A 8 /* Clock-time and date */
+#define RDS_GROUP_TYPE_4B 9 /* Open data applications */
+#define RDS_GROUP_TYPE_5A 10 /* Transparent Data Channels (32 channels)
+ * or ODA */
+#define RDS_GROUP_TYPE_5B 11 /* Transparent Data Channels (32 channels)
+ * or ODA */
+#define RDS_GROUP_TYPE_6A 12 /* In House Applications or ODA */
+#define RDS_GROUP_TYPE_6B 13 /* In House Applications or ODA */
+#define RDS_GROUP_TYPE_7A 14 /* Radio Paging or ODA */
+#define RDS_GROUP_TYPE_7B 15 /* Open Data Applications */
+#define RDS_GROUP_TYPE_8A 16 /* Traffic Message Channel or ODA */
+#define RDS_GROUP_TYPE_8B 17 /* Open Data Applications */
+#define RDS_GROUP_TYPE_9A 18 /* Emergency warning system or ODA */
+#define RDS_GROUP_TYPE_9B 19 /* Open Data Applications */
+#define RDS_GROUP_TYPE_10A 20 /* Program Type Name */
+#define RDS_GROUP_TYPE_10B 21 /* Open Data Applications */
+#define RDS_GROUP_TYPE_11A 22 /* Open Data Applications */
+#define RDS_GROUP_TYPE_11B 23 /* Open Data Applications */
+#define RDS_GROUP_TYPE_12A 24 /* Open Data Applications */
+#define RDS_GROUP_TYPE_12B 25 /* Open Data Applications */
+#define RDS_GROUP_TYPE_13A 26 /* Enhanced Radio Paging or ODA */
+#define RDS_GROUP_TYPE_13B 27 /* Open Data Applications */
+#define RDS_GROUP_TYPE_14A 28 /* Enhanced Other Networks information */
+#define RDS_GROUP_TYPE_14B 29 /* Enhanced Other Networks information */
+#define RDS_GROUP_TYPE_15A 30 /* Defined in RBDS */
+#define RDS_GROUP_TYPE_15B 31 /* Fast switching information */
+#define NUM_DEFINED_RDS_GROUPS 32 /* Number of groups defined in RBDS
+ * standard */
+
+/* Structure representing Generic packet of 64 bits. */
+struct rds_group_data {
+ u16 piCode; /* * Program ID */
+ u16 offsetB; /* subject to group type */
+ u16 offsetC; /* subject to group type */
+ u16 offsetD; /* subject to group type */
+};
+
+/* Structure representing Group 0A (Service Name) */
+struct rds_group0A {
+ u16 piCode; /* * Program ID */
+ u16 offsetB; /* subject to group type */
+ u8 freq[2]; /* alt frequency 0=1 */
+ u8 text[2]; /* Name segment */
+};
+
+/* Structure representing Group 0B (Service Name) */
+struct rds_group0B {
+ u16 piCode; /* * Program ID */
+ u16 offsetB; /* subject to group type */
+ u16 piCode_dup; /* Duplicate PI Code */
+ u8 text[2]; /* station text */
+};
+
+/* Structure representing Group 2A (Radio Text) (64 char) */
+struct rds_group2A {
+ u16 piCode; /* * Program ID */
+ u16 offsetB; /* subject to group type */
+ u8 text[4];
+};
+
+/* Structure representing Group 2B (Radio Text) (32 char) */
+struct rds_group2B {
+ u16 piCode; /* * Program ID */
+ u16 offsetB; /* subject to group type */
+ u16 piCode_dup; /* Duplicate PI Code */
+ u8 text[2];
+};
+
+/* Structure representing all groups */
+union rds_msg {
+ struct rds_group2B gt2B;
+ struct rds_group2A gt2A;
+ struct rds_group0B gt0B;
+ struct rds_group0A gt0A;
+ struct rds_group_data gt00;
+};
+
+struct stfm1000_rds_state {
+ struct stfm1000_rds_bitstream bitstream;
+ struct stfm1000_rds_demod demod;
+ struct stfm1000_rds_pkt pkt;
+ struct stfm1000_rds_text text;
+ unsigned int reset_req : 1;
+};
+
+/* callback from rds etc. */
+void stfm1000_rds_reset(struct stfm1000_rds_state *rds);
+void stfm1000_rds_start(struct stfm1000_rds_state *rds);
+void stfm1000_rds_stop(struct stfm1000_rds_state *rds);
+
+/* call these from the monitor thread, but with interrupts disabled */
+int stfm1000_rds_mix_msg_get(struct stfm1000_rds_state *rds);
+int stfm1000_rds_mix_msg_processed(struct stfm1000_rds_state *rds,
+ int mix_msg);
+int stfm1000_rds_sdnominal_msg_get(struct stfm1000_rds_state *rds);
+int stfm1000_rds_sdnominal_msg_processed(struct stfm1000_rds_state *rds,
+ int sdnominal_msg);
+int stfm1000_rds_bits_available(struct stfm1000_rds_state *rds);
+int stmf1000_rds_get_bit(struct stfm1000_rds_state *rds);
+
+/* called from audio handler (interrupt) */
+void stfm1000_rds_demod(struct stfm1000_rds_state *rds, const u16 *dri_data,
+ int total);
+
+/* call these from monitor thread, interrupts enabled */
+void stfm1000_rds_packet_bit(struct stfm1000_rds_state *rds, int bit);
+int stfm1000_rds_packet_dequeue(struct stfm1000_rds_state *rds, u8 *buf);
+void stfm1000_rds_process_packet(struct stfm1000_rds_state *rds, u8 *buffer);
+
+static inline int stfm1000_rds_get_reset_req(struct stfm1000_rds_state *rds)
+{
+ return rds->reset_req;
+}
+
+/* GROUP_TYPE 0A-0B */
+int stfm1000_rds_get_ps(struct stfm1000_rds_state *rds, u8 *buffer,
+ int bufsize);
+
+/* GROUP_TYPE 2A */
+int stfm1000_rds_get_text(struct stfm1000_rds_state *rds, u8 *buffer,
+ int bufsize);
+
+#endif
diff --git a/drivers/media/radio/stfm1000/stfm1000-regs.h b/drivers/media/radio/stfm1000/stfm1000-regs.h
new file mode 100644
index 000000000000..c9476b7d67e1
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000-regs.h
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#ifndef STFM1000_REGS_H
+#define STFM1000_REGS_H
+
+/* registers */
+#define STFM1000_TUNE1 0x00
+#define STFM1000_SDNOMINAL 0x04
+#define STFM1000_PILOTTRACKING 0x08
+#define STFM1000_INITIALIZATION1 0x10
+#define STFM1000_INITIALIZATION2 0x14
+#define STFM1000_INITIALIZATION3 0x18
+#define STFM1000_INITIALIZATION4 0x1C
+#define STFM1000_INITIALIZATION5 0x20
+#define STFM1000_INITIALIZATION6 0x24
+#define STFM1000_REF 0x28
+#define STFM1000_LNA 0x2C
+#define STFM1000_MIXFILT 0x30
+#define STFM1000_CLK1 0x34
+#define STFM1000_CLK2 0x38
+#define STFM1000_ADC 0x3C
+#define STFM1000_AGC_CONTROL1 0x44
+#define STFM1000_AGC_CONTROL2 0x48
+#define STFM1000_DATAPATH 0x5C
+#define STFM1000_RMS 0x60
+#define STFM1000_AGC_STAT 0x64
+#define STFM1000_SIGNALQUALITY 0x68
+#define STFM1000_DCEST 0x6C
+#define STFM1000_RSSI_TONE 0x70
+#define STFM1000_PILOTCORRECTION 0x74
+#define STFM1000_ATTENTION 0x78
+#define STFM1000_CLK3 0x7C
+#define STFM1000_CHIPID 0x80
+
+/* number of registers */
+#define STFM1000_NUM_REGS ((0x80 + 4) / 4)
+
+#define STFM1000_FREQUENCY_100KHZ_MIN 758
+#define STFM1000_FREQUENCY_100KHZ_RANGE 325
+#define STFM1000_FREQUENCY_100KHZ_MAX (STFM1000_FREQUENCY_100KHZ_MIN + \
+ STFM1000_FREQUENCY_100KHZ_RANGE)
+
+#define STFM1000_TUNE1_B2_MIX 0x001C0000
+#define STFM1000_TUNE1_CICOSR 0x00007E00
+#define STFM1000_TUNE1_PLL_DIV 0x000001FF
+
+#define STFM1000_CHIP_REV_TA1 0x00000001
+#define STFM1000_CHIP_REV_TA2 0x00000002
+#define STFM1000_CHIP_REV_TB1 0x00000011
+#define STFM1000_CHIP_REV_TB2 0x00000012
+
+/* DATAPATH bits we use */
+#define STFM1000_DP_EN 0x01000000
+#define STFM1000_DB_ACCEPT 0x00010000
+#define STFM1000_SAI_CLK_DIV_MASK 0x7c
+#define STFM1000_SAI_CLK_DIV_SHIFT 2
+#define STFM1000_SAI_CLK_DIV(x) \
+ (((x) << STFM1000_SAI_CLK_DIV_SHIFT) & STFM1000_SAI_CLK_DIV_MASK)
+#define STFM1000_SAI_EN 0x00000001
+
+/* AGC_CONTROL1 bits we use */
+#define STFM1000_B2_BYPASS_AGC_CTL 0x00004000
+#define STFM1000_B2_BYPASS_FILT_MASK 0x0000000C
+#define STFM1000_B2_BYPASS_FILT_SHIFT 2
+#define STFM1000_B2_BYPASS_FILT(x) \
+ (((x) << STFM1000_B2_BYPASS_FILT_SHIFT) & STFM1000_B2_BYPASS_FILT_MASK)
+#define STFM1000_B2_LNATH_MASK 0x001F0000
+#define STFM1000_B2_LNATH_SHIFT 16
+#define STFM1000_B2_LNATH(x) \
+ (((x) << STFM1000_B2_LNATH_SHIFT) & STFM1000_B2_LNATH_MASK)
+
+/* AGC_STAT bits we use */
+#define STFM1000_AGCOUT_STAT_MASK 0x1F000000
+#define STFM1000_AGCOUT_STAT_SHIFT 24
+#define STFM1000_LNA_RMS_MASK 0x00001F00
+#define STFM1000_LNA_RMS_SHIFT 8
+
+/* PILOTTRACKING bits we use */
+#define STFM1000_B2_PILOTTRACKING_EN 0x00008000
+#define STFM1000_B2_PILOTLPF_TIMECONSTANT_MASK 0x00000f00
+#define STFM1000_B2_PILOTLPF_TIMECONSTANT_SHIFT 8
+#define STFM1000_B2_PILOTLPF_TIMECONSTANT(x) \
+ (((x) << STFM1000_B2_PILOTLPF_TIMECONSTANT_SHIFT) & \
+ STFM1000_B2_PILOTLPF_TIMECONSTANT_MASK)
+#define STFM1000_B2_PFDSCALE_MASK 0x000000f0
+#define STFM1000_B2_PFDSCALE_SHIFT 4
+#define STFM1000_B2_PFDSCALE(x) \
+ (((x) << STFM1000_B2_PFDSCALE_SHIFT) & STFM1000_B2_PFDSCALE_MASK)
+#define STFM1000_B2_PFDFILTER_SPEEDUP_MASK 0x0000000f
+#define STFM1000_B2_PFDFILTER_SPEEDUP_SHIFT 0
+#define STFM1000_B2_PFDFILTER_SPEEDUP(x) \
+ (((x) << STFM1000_B2_PFDFILTER_SPEEDUP_SHIFT) & \
+ STFM1000_B2_PFDFILTER_SPEEDUP_MASK)
+
+/* PILOTCORRECTION bits we use */
+#define STFM1000_PILOTEST_TA2_MASK 0xff000000
+#define STFM1000_PILOTEST_TA2_SHIFT 24
+#define STFM1000_PILOTEST_TB2_MASK 0xfe000000
+#define STFM1000_PILOTEST_TB2_SHIFT 25
+
+/* INITIALIZATION1 bits we use */
+#define STFM1000_B2_BYPASS_FILT_MASK 0x0000000C
+#define STFM1000_B2_BYPASS_FILT_SHIFT 2
+#define STFM1000_B2_BYPASS_FILT(x) \
+ (((x) << STFM1000_B2_BYPASS_FILT_SHIFT) & STFM1000_B2_BYPASS_FILT_MASK)
+
+/* INITIALIZATION2 bits we use */
+#define STFM1000_DRI_CLK_EN 0x80000000
+#define STFM1000_DEEMPH_50_75B 0x00000100
+#define STFM1000_RDS_ENABLE 0x00100000
+#define STFM1000_RDS_MIXOFFSET 0x00200000
+
+/* INITIALIZATION3 bits we use */
+#define STFM1000_B2_NEAR_CHAN_MIX_MASK 0x1c000000
+#define STFM1000_B2_NEAR_CHAN_MIX_SHIFT 26
+#define STFM1000_B2_NEAR_CHAN_MIX(x) \
+ (((x) << STFM1000_B2_NEAR_CHAN_MIX_SHIFT) & \
+ STFM1000_B2_NEAR_CHAN_MIX_MASK)
+
+/* CLK1 bits we use */
+#define STFM1000_ENABLE_TAPDELAYFIX 0x00000020
+
+/* REF bits we use */
+#define STFM1000_LNA_AMP1_IMPROVE_DISTORTION 0x08000000
+
+/* LNA bits we use */
+#define STFM1000_AMP2_IMPROVE_DISTORTION 0x08000000
+#define STFM1000_ANTENNA_TUNECAP_MASK 0x001F0000
+#define STFM1000_ANTENNA_TUNECAP_SHIFT 16
+#define STFM1000_ANTENNA_TUNECAP(x) \
+ (((x) << STFM1000_ANTENNA_TUNECAP_SHIFT) & \
+ STFM1000_ANTENNA_TUNECAP_MASK)
+#define STFM1000_IBIAS2_UP 0x00000008
+#define STFM1000_IBIAS2_DN 0x00000004
+#define STFM1000_IBIAS1_UP 0x00000002
+#define STFM1000_IBIAS1_DN 0x00000001
+#define STFM1000_USEATTEN_MASK 0x00600000
+#define STFM1000_USEATTEN_SHIFT 21
+#define STFM1000_USEATTEN(x) \
+ (((x) << STFM1000_USEATTEN_SHIFT) & STFM1000_USEATTEN_MASK)
+
+/* SIGNALQUALITY bits we use */
+#define STFM1000_NEAR_CHAN_AMPLITUDE_MASK 0x0000007F
+#define STFM1000_NEAR_CHAN_AMPLITUDE_SHIFT 0
+#define STFM1000_NEAR_CHAN_AMPLITUDE(x) \
+ (((x) << STFM1000_NEAR_CHAN_AMPLITUDE_SHIFT) & \
+ STFM1000_NEAR_CHAN_AMPLITUDE_MASK)
+
+/* precalc tables elements */
+struct stfm1000_tune1 {
+ unsigned int tune1; /* at least 32 bit */
+ unsigned int sdnom;
+};
+
+#endif
diff --git a/drivers/media/radio/stfm1000/stfm1000.h b/drivers/media/radio/stfm1000/stfm1000.h
new file mode 100644
index 000000000000..5ae6f38db3b0
--- /dev/null
+++ b/drivers/media/radio/stfm1000/stfm1000.h
@@ -0,0 +1,254 @@
+/*
+ * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#ifndef STFM1000_H
+#define STFM1000_H
+
+#include <linux/videodev2.h>
+#include <media/v4l2-common.h>
+#include <linux/i2c.h>
+#include <linux/list.h>
+#include <linux/wait.h>
+#include <linux/irq.h>
+#include <media/videobuf-dma-sg.h>
+#include <sound/core.h>
+#include <sound/pcm.h>
+#include <mach/dma.h>
+
+#include "stfm1000-regs.h"
+
+#include "stfm1000-filter.h"
+#include "stfm1000-rds.h"
+
+struct stfm1000 {
+ struct list_head devlist;
+ int idx;
+
+ struct i2c_client *client;
+ struct video_device radio;
+
+ /* alsa */
+ struct snd_card *card;
+ struct snd_pcm *pcm;
+ struct snd_pcm_substream *substream;
+ struct stmp3xxx_dma_descriptor *dma;
+ int desc_num;
+ int dma_ch;
+ int dma_irq;
+ int attn_irq;
+
+ struct mutex state_lock;
+ int read_count;
+ int read_offset;
+ int blocks;
+ int blksize;
+ int bufsize;
+
+ struct mutex deffered_work_lock;
+ struct execute_work snd_capture_start_work;
+ struct execute_work snd_capture_stop_work;
+
+ int now_recording;
+ int alsa_initialized;
+ int stopping_recording;
+
+ /* actual DRI buffer */
+ dma_addr_t dri_phys;
+ void *dri_buf;
+ int dri_bufsz;
+
+ /* various */
+ u16 curvol;
+ int users;
+ int removed;
+ struct mutex xfer_lock;
+ u8 revid;
+
+ unsigned int dbgflg;
+
+ /* shadow registers */
+ u32 shadow_regs[STFM1000_NUM_REGS];
+ u32 reg_rw_set[(STFM1000_NUM_REGS + 31) / 32];
+ u32 reg_ra_set[(STFM1000_NUM_REGS + 31) / 32];
+ u32 reg_dirty_set[(STFM1000_NUM_REGS + 31) / 32];
+
+ /* tuning parameters (not everything is used for now) */
+ u16 tune_rssi_th; /* sd_ctl_TuneRssiTh_u16 */
+ u16 tune_mpx_dc_th; /* sd_ctl_TuneMpxDcTh_u16 */
+ u16 adj_chan_th; /* sd_ctl_AdjChanTh_u16 */
+ u16 pilot_est_th; /* sd_ctl_PilotEstTh_u16 */
+ u16 coef_lna_turn_off_th; /* sd_ctl_pCoefLnaTurnOffTh_u16 */
+ u16 coef_lna_turn_on_th; /* sd_ctl_pCoefLnaTurnOnTh_u16 */
+ u16 reg_agc_ref_lna_off; /* sd_ctl_pRegAgcRefLnaOff_u16 */
+ u16 reg_agc_ref_lna_on; /* sd_ctl_pRegAgcRefLnaOn_u16 */
+
+ u32 sdnominal_pivot; /* sd_ctl_SdnominalData_u32 */
+
+ /* jiffies of the next monitor cycle */
+ unsigned long next_quality_monitor;
+ unsigned long next_agc_monitor;
+
+ unsigned int mute : 1; /* XXX */
+ unsigned int lna_driving : 1; /* sd_ctl_LnaDriving_u1 */
+ unsigned int weak_signal : 1; /* sd_ctl_WeakSignal_u1 */
+ unsigned int is_station : 1; /* XXX */
+ unsigned int force_mono : 1; /* XXX */
+ unsigned int signal_indicator : 1; /* XXX */
+ unsigned int stereo_indicator : 1; /* XXX */
+ unsigned int agc_monitor : 1; /* XXX */
+ unsigned int quality_monitor : 1; /* XXX */
+ unsigned int pilot_present : 1; /* sd_ctl_PilotPresent_u1 */
+ unsigned int prev_pilot_present : 1; /* XXX */
+ unsigned int stereo : 1;
+ unsigned int active : 1; /* set when audio enabled */
+ unsigned int rds_enable : 1; /* set when rds is enabled */
+ unsigned int rds_present : 1; /* RDS info present */
+ unsigned int rds_sync : 1; /* RDS force sync */
+ unsigned int rds_demod_running : 1; /* RDS demod is running ATM */
+ unsigned int rds_sdnominal_adapt : 1; /* adapt for better recept. */
+ unsigned int rds_phase_pop : 1; /* enable phase pop */
+ unsigned int rds_info : 1; /* print debugging info RDS */
+ unsigned int tuning_grid_50KHz : 1; /* tuning grid of 50Khz */
+ u32 rssi; /* rssi last decoded frame */
+ u16 rssi_dc_est_log;
+ u16 signal_strength; /* is rssi_dc_est_log */
+ u16 rds_signal_th; /* RDS threshold */
+ s16 mpx_dc; /* sd_ctl_ShadowToneData_i16 */
+
+ u32 tune_cap_a_f; /* float! sd_ctl_TuneCapA_f */
+ u32 tune_cap_b_f; /* float! sd_ctl_TuneCapB_f */
+
+ int monitor_period; /* period of the monitor */
+ int quality_monitor_period; /* update period in ms */
+ int agc_monitor_period; /* update period in ms */
+
+ int georegion; /* current graphical region */
+
+ /* last tuned frequency */
+ int freq; /* 88.0 = 8800 */
+
+ /* weak signal processing filter state */
+ struct stfm1000_filter_parms filter_parms;
+
+ /* state of rds */
+ spinlock_t rds_lock;
+ struct stfm1000_rds_state rds_state;
+ unsigned int rds_pkt_bad;
+ unsigned int rds_pkt_good;
+ unsigned int rds_pkt_recovered;
+ unsigned int rds_pkt_lost_sync;
+ unsigned int rds_bit_overruns;
+
+ /* monitor thread */
+ wait_queue_head_t thread_wait;
+ unsigned long thread_events;
+ struct task_struct *thread;
+};
+
+#define EVENT_RDS_BITS 0
+#define EVENT_RDS_MIXFILT 1
+#define EVENT_RDS_SDNOMINAL 2
+#define EVENT_RDS_RESET 3
+
+#define STFM1000_DBGFLG_I2C (1 << 0)
+
+static inline struct stfm1000 *stfm1000_from_file(struct file *file)
+{
+ return container_of(video_devdata(file), struct stfm1000, radio);
+}
+
+/* in stfm1000-i2c.c */
+
+/* setup reg set */
+void stfm1000_setup_reg_set(struct stfm1000 *stfm1000);
+
+/* direct access to registers bypassing the shadow register set */
+int stfm1000_raw_read(struct stfm1000 *stfm1000, int reg, u32 *value);
+int stfm1000_raw_write(struct stfm1000 *stfm1000, int reg, u32 value);
+
+/* access using the shadow register set */
+int stfm1000_write(struct stfm1000 *stfm1000, int reg, u32 value);
+int stfm1000_read(struct stfm1000 *stfm1000, int reg, u32 *value);
+int stfm1000_write_masked(struct stfm1000 *stfm1000, int reg, u32 value,
+ u32 mask);
+int stfm1000_set_bits(struct stfm1000 *stfm1000, int reg, u32 value);
+int stfm1000_clear_bits(struct stfm1000 *stfm1000, int reg, u32 value);
+
+struct stfm1000_reg {
+ unsigned int regno;
+ u32 value;
+};
+
+#define STFM1000_REG_END -1
+#define STFM1000_REG_DELAY -2
+
+#define STFM1000_REG_SET_BITS_MASK 0x1000
+#define STFM1000_REG_CLEAR_BITS_MASK 0x2000
+
+#define STFM1000_REG(r, v) \
+ { .regno = STFM1000_ ## r , .value = (v) }
+
+#define STFM1000_END \
+ { .regno = STFM1000_REG_END }
+
+#define STFM1000_DELAY(x) \
+ { .regno = STFM1000_REG_DELAY, .value = (x) }
+
+#define STFM1000_REG_SETBITS(r, v) \
+ { .regno = STFM1000_ ## r | STFM1000_REG_SET_BITS_MASK, \
+ .value = (v) }
+
+#define STFM1000_REG_CLRBITS(r, v) \
+ { .regno = STFM1000_ ## r | STFM1000_REG_CLEAR_BITS_MASK, \
+ .value = (v) }
+
+int stfm1000_write_regs(struct stfm1000 *stfm1000,
+ const struct stfm1000_reg *reg);
+
+/* in stfm1000-precalc.c */
+extern const struct stfm1000_tune1
+stfm1000_tune1_table[STFM1000_FREQUENCY_100KHZ_RANGE];
+
+/* exported for use by alsa driver */
+
+struct stfm1000_dri_sample {
+ /* L+R */
+ u16 l_plus_r;
+ /* L-R */
+ u16 l_minus_r;
+ /* Rx signal strength channel */
+ u16 rssi;
+ /* Radio data service channel */
+ u16 rds;
+};
+
+struct stfm1000_alsa_ops {
+ int (*init)(struct stfm1000 *stfm1000);
+ void (*release)(struct stfm1000 *stfm1000);
+ void (*dma_irq)(struct stfm1000 *stfm1000);
+ void (*attn_irq)(struct stfm1000 *stfm1000);
+};
+
+extern struct list_head stfm1000_devlist;
+extern struct stfm1000_alsa_ops *stfm1000_alsa_ops;
+
+/* needed for setting the interrupt handlers from alsa */
+irqreturn_t stfm1000_dri_attn_irq(int irq, void *dev_id);
+irqreturn_t stfm1000_dri_dma_irq(int irq, void *dev_id);
+void stfm1000_decode_block(struct stfm1000 *stfm1000, const s16 *src, s16 *dst, int count);
+void stfm1000_take_down(struct stfm1000 *stfm1000);
+void stfm1000_bring_up(struct stfm1000 *stfm1000);
+void stfm1000_tune_current(struct stfm1000 *stfm1000);
+
+void stfm1000_monitor_signal(struct stfm1000 *stfm1000, int bit);
+
+#endif