python_code
stringlengths 0
1.8M
| repo_name
stringclasses 7
values | file_path
stringlengths 5
99
|
---|---|---|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Cobalt NOR flash functions
*
* Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/cfi.h>
#include <linux/time.h>
#include "cobalt-flash.h"
#define ADRS(offset) (COBALT_BUS_FLASH_BASE + offset)
static struct map_info cobalt_flash_map = {
.name = "cobalt-flash",
.bankwidth = 2, /* 16 bits */
.size = 0x4000000, /* 64MB */
.phys = 0, /* offset */
};
static map_word flash_read16(struct map_info *map, unsigned long offset)
{
map_word r;
r.x[0] = cobalt_bus_read32(map->virt, ADRS(offset));
if (offset & 0x2)
r.x[0] >>= 16;
else
r.x[0] &= 0x0000ffff;
return r;
}
static void flash_write16(struct map_info *map, const map_word datum,
unsigned long offset)
{
u16 data = (u16)datum.x[0];
cobalt_bus_write16(map->virt, ADRS(offset), data);
}
static void flash_copy_from(struct map_info *map, void *to,
unsigned long from, ssize_t len)
{
u32 src = from;
u8 *dest = to;
u32 data;
while (len) {
data = cobalt_bus_read32(map->virt, ADRS(src));
do {
*dest = data >> (8 * (src & 3));
src++;
dest++;
len--;
} while (len && (src % 4));
}
}
static void flash_copy_to(struct map_info *map, unsigned long to,
const void *from, ssize_t len)
{
const u8 *src = from;
u32 dest = to;
pr_info("%s: offset 0x%x: length %zu\n", __func__, dest, len);
while (len) {
u16 data;
do {
data = *src << (8 * (dest & 1));
src++;
dest++;
len--;
} while (len && (dest % 2));
cobalt_bus_write16(map->virt, ADRS(dest - 2), data);
}
}
int cobalt_flash_probe(struct cobalt *cobalt)
{
struct map_info *map = &cobalt_flash_map;
struct mtd_info *mtd;
BUG_ON(!map_bankwidth_supported(map->bankwidth));
map->virt = cobalt->bar1;
map->read = flash_read16;
map->write = flash_write16;
map->copy_from = flash_copy_from;
map->copy_to = flash_copy_to;
mtd = do_map_probe("cfi_probe", map);
cobalt->mtd = mtd;
if (!mtd) {
cobalt_err("Probe CFI flash failed!\n");
return -1;
}
mtd->owner = THIS_MODULE;
mtd->dev.parent = &cobalt->pci_dev->dev;
mtd_device_register(mtd, NULL, 0);
return 0;
}
void cobalt_flash_remove(struct cobalt *cobalt)
{
if (cobalt->mtd) {
mtd_device_unregister(cobalt->mtd);
map_destroy(cobalt->mtd);
}
}
| linux-master | drivers/media/pci/cobalt/cobalt-flash.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* cobalt I2C functions
*
* Derived from cx18-i2c.c
*
* Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include "cobalt-driver.h"
#include "cobalt-i2c.h"
struct cobalt_i2c_regs {
/* Clock prescaler register lo-byte */
u8 prerlo;
u8 dummy0[3];
/* Clock prescaler register high-byte */
u8 prerhi;
u8 dummy1[3];
/* Control register */
u8 ctr;
u8 dummy2[3];
/* Transmit/Receive register */
u8 txr_rxr;
u8 dummy3[3];
/* Command and Status register */
u8 cr_sr;
u8 dummy4[3];
};
/* CTR[7:0] - Control register */
/* I2C Core enable bit */
#define M00018_CTR_BITMAP_EN_MSK (1 << 7)
/* I2C Core interrupt enable bit */
#define M00018_CTR_BITMAP_IEN_MSK (1 << 6)
/* CR[7:0] - Command register */
/* I2C start condition */
#define M00018_CR_BITMAP_STA_MSK (1 << 7)
/* I2C stop condition */
#define M00018_CR_BITMAP_STO_MSK (1 << 6)
/* I2C read from slave */
#define M00018_CR_BITMAP_RD_MSK (1 << 5)
/* I2C write to slave */
#define M00018_CR_BITMAP_WR_MSK (1 << 4)
/* I2C ack */
#define M00018_CR_BITMAP_ACK_MSK (1 << 3)
/* I2C Interrupt ack */
#define M00018_CR_BITMAP_IACK_MSK (1 << 0)
/* SR[7:0] - Status register */
/* Receive acknowledge from slave */
#define M00018_SR_BITMAP_RXACK_MSK (1 << 7)
/* Busy, I2C bus busy (as defined by start / stop bits) */
#define M00018_SR_BITMAP_BUSY_MSK (1 << 6)
/* Arbitration lost - core lost arbitration */
#define M00018_SR_BITMAP_AL_MSK (1 << 5)
/* Transfer in progress */
#define M00018_SR_BITMAP_TIP_MSK (1 << 1)
/* Interrupt flag */
#define M00018_SR_BITMAP_IF_MSK (1 << 0)
/* Frequency, in Hz */
#define I2C_FREQUENCY 400000
#define ALT_CPU_FREQ 83333333
static struct cobalt_i2c_regs __iomem *
cobalt_i2c_regs(struct cobalt *cobalt, unsigned idx)
{
switch (idx) {
case 0:
default:
return (struct cobalt_i2c_regs __iomem *)
(cobalt->bar1 + COBALT_I2C_0_BASE);
case 1:
return (struct cobalt_i2c_regs __iomem *)
(cobalt->bar1 + COBALT_I2C_1_BASE);
case 2:
return (struct cobalt_i2c_regs __iomem *)
(cobalt->bar1 + COBALT_I2C_2_BASE);
case 3:
return (struct cobalt_i2c_regs __iomem *)
(cobalt->bar1 + COBALT_I2C_3_BASE);
case 4:
return (struct cobalt_i2c_regs __iomem *)
(cobalt->bar1 + COBALT_I2C_HSMA_BASE);
}
}
/* Do low-level i2c byte transfer.
* Returns -1 in case of an error or 0 otherwise.
*/
static int cobalt_tx_bytes(struct cobalt_i2c_regs __iomem *regs,
struct i2c_adapter *adap, bool start, bool stop,
u8 *data, u16 len)
{
unsigned long start_time;
int status;
int cmd;
int i;
for (i = 0; i < len; i++) {
/* Setup data */
iowrite8(data[i], ®s->txr_rxr);
/* Setup command */
if (i == 0 && start) {
/* Write + Start */
cmd = M00018_CR_BITMAP_WR_MSK |
M00018_CR_BITMAP_STA_MSK;
} else if (i == len - 1 && stop) {
/* Write + Stop */
cmd = M00018_CR_BITMAP_WR_MSK |
M00018_CR_BITMAP_STO_MSK;
} else {
/* Write only */
cmd = M00018_CR_BITMAP_WR_MSK;
}
/* Execute command */
iowrite8(cmd, ®s->cr_sr);
/* Wait for transfer to complete (TIP = 0) */
start_time = jiffies;
status = ioread8(®s->cr_sr);
while (status & M00018_SR_BITMAP_TIP_MSK) {
if (time_after(jiffies, start_time + adap->timeout))
return -ETIMEDOUT;
cond_resched();
status = ioread8(®s->cr_sr);
}
/* Verify ACK */
if (status & M00018_SR_BITMAP_RXACK_MSK) {
/* NO ACK! */
return -EIO;
}
/* Verify arbitration */
if (status & M00018_SR_BITMAP_AL_MSK) {
/* Arbitration lost! */
return -EIO;
}
}
return 0;
}
/* Do low-level i2c byte read.
* Returns -1 in case of an error or 0 otherwise.
*/
static int cobalt_rx_bytes(struct cobalt_i2c_regs __iomem *regs,
struct i2c_adapter *adap, bool start, bool stop,
u8 *data, u16 len)
{
unsigned long start_time;
int status;
int cmd;
int i;
for (i = 0; i < len; i++) {
/* Setup command */
if (i == 0 && start) {
/* Read + Start */
cmd = M00018_CR_BITMAP_RD_MSK |
M00018_CR_BITMAP_STA_MSK;
} else if (i == len - 1 && stop) {
/* Read + Stop */
cmd = M00018_CR_BITMAP_RD_MSK |
M00018_CR_BITMAP_STO_MSK;
} else {
/* Read only */
cmd = M00018_CR_BITMAP_RD_MSK;
}
/* Last byte to read, no ACK */
if (i == len - 1)
cmd |= M00018_CR_BITMAP_ACK_MSK;
/* Execute command */
iowrite8(cmd, ®s->cr_sr);
/* Wait for transfer to complete (TIP = 0) */
start_time = jiffies;
status = ioread8(®s->cr_sr);
while (status & M00018_SR_BITMAP_TIP_MSK) {
if (time_after(jiffies, start_time + adap->timeout))
return -ETIMEDOUT;
cond_resched();
status = ioread8(®s->cr_sr);
}
/* Verify arbitration */
if (status & M00018_SR_BITMAP_AL_MSK) {
/* Arbitration lost! */
return -EIO;
}
/* Store data */
data[i] = ioread8(®s->txr_rxr);
}
return 0;
}
/* Generate stop condition on i2c bus.
* The m00018 stop isn't doing the right thing (wrong timing).
* So instead send a start condition, 8 zeroes and a stop condition.
*/
static int cobalt_stop(struct cobalt_i2c_regs __iomem *regs,
struct i2c_adapter *adap)
{
u8 data = 0;
return cobalt_tx_bytes(regs, adap, true, true, &data, 1);
}
static int cobalt_xfer(struct i2c_adapter *adap,
struct i2c_msg msgs[], int num)
{
struct cobalt_i2c_data *data = adap->algo_data;
struct cobalt_i2c_regs __iomem *regs = data->regs;
struct i2c_msg *pmsg;
unsigned short flags;
int ret = 0;
int i, j;
for (i = 0; i < num; i++) {
int stop = (i == num - 1);
pmsg = &msgs[i];
flags = pmsg->flags;
if (!(pmsg->flags & I2C_M_NOSTART)) {
u8 addr = pmsg->addr << 1;
if (flags & I2C_M_RD)
addr |= 1;
if (flags & I2C_M_REV_DIR_ADDR)
addr ^= 1;
for (j = 0; j < adap->retries; j++) {
ret = cobalt_tx_bytes(regs, adap, true, false,
&addr, 1);
if (!ret)
break;
cobalt_stop(regs, adap);
}
if (ret < 0)
return ret;
ret = 0;
}
if (pmsg->flags & I2C_M_RD) {
/* read bytes into buffer */
ret = cobalt_rx_bytes(regs, adap, false, stop,
pmsg->buf, pmsg->len);
if (ret < 0)
goto bailout;
} else {
/* write bytes from buffer */
ret = cobalt_tx_bytes(regs, adap, false, stop,
pmsg->buf, pmsg->len);
if (ret < 0)
goto bailout;
}
}
ret = i;
bailout:
if (ret < 0)
cobalt_stop(regs, adap);
return ret;
}
static u32 cobalt_func(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}
/* template for i2c-bit-algo */
static const struct i2c_adapter cobalt_i2c_adap_template = {
.name = "cobalt i2c driver",
.algo = NULL, /* set by i2c-algo-bit */
.algo_data = NULL, /* filled from template */
.owner = THIS_MODULE,
};
static const struct i2c_algorithm cobalt_algo = {
.master_xfer = cobalt_xfer,
.functionality = cobalt_func,
};
/* init + register i2c algo-bit adapter */
int cobalt_i2c_init(struct cobalt *cobalt)
{
int i, err;
int status;
int prescale;
unsigned long start_time;
cobalt_dbg(1, "i2c init\n");
/* Define I2C clock prescaler */
prescale = ((ALT_CPU_FREQ) / (5 * I2C_FREQUENCY)) - 1;
for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
struct cobalt_i2c_regs __iomem *regs =
cobalt_i2c_regs(cobalt, i);
struct i2c_adapter *adap = &cobalt->i2c_adap[i];
/* Disable I2C */
iowrite8(M00018_CTR_BITMAP_EN_MSK, ®s->cr_sr);
iowrite8(0, ®s->ctr);
iowrite8(0, ®s->cr_sr);
start_time = jiffies;
do {
if (time_after(jiffies, start_time + HZ)) {
if (cobalt_ignore_err) {
adap->dev.parent = NULL;
return 0;
}
return -ETIMEDOUT;
}
status = ioread8(®s->cr_sr);
} while (status & M00018_SR_BITMAP_TIP_MSK);
/* Disable I2C */
iowrite8(0, ®s->ctr);
iowrite8(0, ®s->cr_sr);
/* Calculate i2c prescaler */
iowrite8(prescale & 0xff, ®s->prerlo);
iowrite8((prescale >> 8) & 0xff, ®s->prerhi);
/* Enable I2C, interrupts disabled */
iowrite8(M00018_CTR_BITMAP_EN_MSK, ®s->ctr);
/* Setup algorithm for adapter */
cobalt->i2c_data[i].cobalt = cobalt;
cobalt->i2c_data[i].regs = regs;
*adap = cobalt_i2c_adap_template;
adap->algo = &cobalt_algo;
adap->algo_data = &cobalt->i2c_data[i];
adap->retries = 3;
sprintf(adap->name + strlen(adap->name),
" #%d-%d", cobalt->instance, i);
i2c_set_adapdata(adap, &cobalt->v4l2_dev);
adap->dev.parent = &cobalt->pci_dev->dev;
err = i2c_add_adapter(adap);
if (err) {
if (cobalt_ignore_err) {
adap->dev.parent = NULL;
return 0;
}
while (i--)
i2c_del_adapter(&cobalt->i2c_adap[i]);
return err;
}
cobalt_info("registered bus %s\n", adap->name);
}
return 0;
}
void cobalt_i2c_exit(struct cobalt *cobalt)
{
int i;
cobalt_dbg(1, "i2c exit\n");
for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
cobalt_err("unregistered bus %s\n", cobalt->i2c_adap[i].name);
i2c_del_adapter(&cobalt->i2c_adap[i]);
}
}
| linux-master | drivers/media/pci/cobalt/cobalt-i2c.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* ALSA PCM device for the
* ALSA interface to cobalt PCM capture streams
*
* Copyright 2014-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <media/v4l2-device.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include "cobalt-driver.h"
#include "cobalt-alsa.h"
#include "cobalt-alsa-pcm.h"
static unsigned int pcm_debug;
module_param(pcm_debug, int, 0644);
MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
#define dprintk(fmt, arg...) \
do { \
if (pcm_debug) \
pr_info("cobalt-alsa-pcm %s: " fmt, __func__, ##arg); \
} while (0)
static const struct snd_pcm_hardware snd_cobalt_hdmi_capture = {
.info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_MMAP_VALID,
.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
.rates = SNDRV_PCM_RATE_48000,
.rate_min = 48000,
.rate_max = 48000,
.channels_min = 1,
.channels_max = 8,
.buffer_bytes_max = 4 * 240 * 8 * 4, /* 5 ms of data */
.period_bytes_min = 1920, /* 1 sample = 8 * 4 bytes */
.period_bytes_max = 240 * 8 * 4, /* 5 ms of 8 channel data */
.periods_min = 1,
.periods_max = 4,
};
static const struct snd_pcm_hardware snd_cobalt_playback = {
.info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_MMAP_VALID,
.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
.rates = SNDRV_PCM_RATE_48000,
.rate_min = 48000,
.rate_max = 48000,
.channels_min = 1,
.channels_max = 8,
.buffer_bytes_max = 4 * 240 * 8 * 4, /* 5 ms of data */
.period_bytes_min = 1920, /* 1 sample = 8 * 4 bytes */
.period_bytes_max = 240 * 8 * 4, /* 5 ms of 8 channel data */
.periods_min = 1,
.periods_max = 4,
};
static void sample_cpy(u8 *dst, const u8 *src, u32 len, bool is_s32)
{
static const unsigned map[8] = { 0, 1, 5, 4, 2, 3, 6, 7 };
unsigned idx = 0;
while (len >= (is_s32 ? 4 : 2)) {
unsigned offset = map[idx] * 4;
u32 val = src[offset + 1] + (src[offset + 2] << 8) +
(src[offset + 3] << 16);
if (is_s32) {
*dst++ = 0;
*dst++ = val & 0xff;
}
*dst++ = (val >> 8) & 0xff;
*dst++ = (val >> 16) & 0xff;
len -= is_s32 ? 4 : 2;
idx++;
}
}
static void cobalt_alsa_announce_pcm_data(struct snd_cobalt_card *cobsc,
u8 *pcm_data,
size_t skip,
size_t samples)
{
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime;
unsigned long flags;
unsigned int oldptr;
unsigned int stride;
int length = samples;
int period_elapsed = 0;
bool is_s32;
dprintk("cobalt alsa announce ptr=%p data=%p num_bytes=%zd\n", cobsc,
pcm_data, samples);
substream = cobsc->capture_pcm_substream;
if (substream == NULL) {
dprintk("substream was NULL\n");
return;
}
runtime = substream->runtime;
if (runtime == NULL) {
dprintk("runtime was NULL\n");
return;
}
is_s32 = runtime->format == SNDRV_PCM_FORMAT_S32_LE;
stride = runtime->frame_bits >> 3;
if (stride == 0) {
dprintk("stride is zero\n");
return;
}
if (length == 0) {
dprintk("%s: length was zero\n", __func__);
return;
}
if (runtime->dma_area == NULL) {
dprintk("dma area was NULL - ignoring\n");
return;
}
oldptr = cobsc->hwptr_done_capture;
if (oldptr + length >= runtime->buffer_size) {
unsigned int cnt = runtime->buffer_size - oldptr;
unsigned i;
for (i = 0; i < cnt; i++)
sample_cpy(runtime->dma_area + (oldptr + i) * stride,
pcm_data + i * skip,
stride, is_s32);
for (i = cnt; i < length; i++)
sample_cpy(runtime->dma_area + (i - cnt) * stride,
pcm_data + i * skip, stride, is_s32);
} else {
unsigned i;
for (i = 0; i < length; i++)
sample_cpy(runtime->dma_area + (oldptr + i) * stride,
pcm_data + i * skip,
stride, is_s32);
}
snd_pcm_stream_lock_irqsave(substream, flags);
cobsc->hwptr_done_capture += length;
if (cobsc->hwptr_done_capture >=
runtime->buffer_size)
cobsc->hwptr_done_capture -=
runtime->buffer_size;
cobsc->capture_transfer_done += length;
if (cobsc->capture_transfer_done >=
runtime->period_size) {
cobsc->capture_transfer_done -=
runtime->period_size;
period_elapsed = 1;
}
snd_pcm_stream_unlock_irqrestore(substream, flags);
if (period_elapsed)
snd_pcm_period_elapsed(substream);
}
static int alsa_fnc(struct vb2_buffer *vb, void *priv)
{
struct cobalt_stream *s = priv;
unsigned char *p = vb2_plane_vaddr(vb, 0);
int i;
if (pcm_debug) {
pr_info("alsa: ");
for (i = 0; i < 8 * 4; i++) {
if (!(i & 3))
pr_cont(" ");
pr_cont("%02x", p[i]);
}
pr_cont("\n");
}
cobalt_alsa_announce_pcm_data(s->alsa,
vb2_plane_vaddr(vb, 0),
8 * 4,
vb2_get_plane_payload(vb, 0) / (8 * 4));
return 0;
}
static int snd_cobalt_pcm_capture_open(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
struct cobalt_stream *s = cobsc->s;
runtime->hw = snd_cobalt_hdmi_capture;
snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
cobsc->capture_pcm_substream = substream;
runtime->private_data = s;
cobsc->alsa_record_cnt++;
if (cobsc->alsa_record_cnt == 1) {
int rc;
rc = vb2_thread_start(&s->q, alsa_fnc, s, s->vdev.name);
if (rc) {
cobsc->alsa_record_cnt--;
return rc;
}
}
return 0;
}
static int snd_cobalt_pcm_capture_close(struct snd_pcm_substream *substream)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
struct cobalt_stream *s = cobsc->s;
cobsc->alsa_record_cnt--;
if (cobsc->alsa_record_cnt == 0)
vb2_thread_stop(&s->q);
return 0;
}
static int snd_cobalt_pcm_prepare(struct snd_pcm_substream *substream)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
cobsc->hwptr_done_capture = 0;
cobsc->capture_transfer_done = 0;
return 0;
}
static int snd_cobalt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_STOP:
return 0;
default:
return -EINVAL;
}
return 0;
}
static
snd_pcm_uframes_t snd_cobalt_pcm_pointer(struct snd_pcm_substream *substream)
{
snd_pcm_uframes_t hwptr_done;
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
hwptr_done = cobsc->hwptr_done_capture;
return hwptr_done;
}
static void pb_sample_cpy(u8 *dst, const u8 *src, u32 len, bool is_s32)
{
static const unsigned map[8] = { 0, 1, 5, 4, 2, 3, 6, 7 };
unsigned idx = 0;
while (len >= (is_s32 ? 4 : 2)) {
unsigned offset = map[idx] * 4;
u8 *out = dst + offset;
*out++ = 0;
if (is_s32) {
src++;
*out++ = *src++;
} else {
*out++ = 0;
}
*out++ = *src++;
*out = *src++;
len -= is_s32 ? 4 : 2;
idx++;
}
}
static void cobalt_alsa_pb_pcm_data(struct snd_cobalt_card *cobsc,
u8 *pcm_data,
size_t skip,
size_t samples)
{
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime;
unsigned long flags;
unsigned int pos;
unsigned int stride;
bool is_s32;
unsigned i;
dprintk("cobalt alsa pb ptr=%p data=%p samples=%zd\n", cobsc,
pcm_data, samples);
substream = cobsc->playback_pcm_substream;
if (substream == NULL) {
dprintk("substream was NULL\n");
return;
}
runtime = substream->runtime;
if (runtime == NULL) {
dprintk("runtime was NULL\n");
return;
}
is_s32 = runtime->format == SNDRV_PCM_FORMAT_S32_LE;
stride = runtime->frame_bits >> 3;
if (stride == 0) {
dprintk("stride is zero\n");
return;
}
if (samples == 0) {
dprintk("%s: samples was zero\n", __func__);
return;
}
if (runtime->dma_area == NULL) {
dprintk("dma area was NULL - ignoring\n");
return;
}
pos = cobsc->pb_pos % cobsc->pb_size;
for (i = 0; i < cobsc->pb_count / (8 * 4); i++)
pb_sample_cpy(pcm_data + i * skip,
runtime->dma_area + pos + i * stride,
stride, is_s32);
snd_pcm_stream_lock_irqsave(substream, flags);
cobsc->pb_pos += i * stride;
snd_pcm_stream_unlock_irqrestore(substream, flags);
if (cobsc->pb_pos % cobsc->pb_count == 0)
snd_pcm_period_elapsed(substream);
}
static int alsa_pb_fnc(struct vb2_buffer *vb, void *priv)
{
struct cobalt_stream *s = priv;
if (s->alsa->alsa_pb_channel)
cobalt_alsa_pb_pcm_data(s->alsa,
vb2_plane_vaddr(vb, 0),
8 * 4,
vb2_get_plane_payload(vb, 0) / (8 * 4));
return 0;
}
static int snd_cobalt_pcm_playback_open(struct snd_pcm_substream *substream)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
struct snd_pcm_runtime *runtime = substream->runtime;
struct cobalt_stream *s = cobsc->s;
runtime->hw = snd_cobalt_playback;
snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
cobsc->playback_pcm_substream = substream;
runtime->private_data = s;
cobsc->alsa_playback_cnt++;
if (cobsc->alsa_playback_cnt == 1) {
int rc;
rc = vb2_thread_start(&s->q, alsa_pb_fnc, s, s->vdev.name);
if (rc) {
cobsc->alsa_playback_cnt--;
return rc;
}
}
return 0;
}
static int snd_cobalt_pcm_playback_close(struct snd_pcm_substream *substream)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
struct cobalt_stream *s = cobsc->s;
cobsc->alsa_playback_cnt--;
if (cobsc->alsa_playback_cnt == 0)
vb2_thread_stop(&s->q);
return 0;
}
static int snd_cobalt_pcm_pb_prepare(struct snd_pcm_substream *substream)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
cobsc->pb_size = snd_pcm_lib_buffer_bytes(substream);
cobsc->pb_count = snd_pcm_lib_period_bytes(substream);
cobsc->pb_pos = 0;
return 0;
}
static int snd_cobalt_pcm_pb_trigger(struct snd_pcm_substream *substream,
int cmd)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
if (cobsc->alsa_pb_channel)
return -EBUSY;
cobsc->alsa_pb_channel = true;
return 0;
case SNDRV_PCM_TRIGGER_STOP:
cobsc->alsa_pb_channel = false;
return 0;
default:
return -EINVAL;
}
}
static
snd_pcm_uframes_t snd_cobalt_pcm_pb_pointer(struct snd_pcm_substream *substream)
{
struct snd_cobalt_card *cobsc = snd_pcm_substream_chip(substream);
size_t ptr;
ptr = cobsc->pb_pos;
return bytes_to_frames(substream->runtime, ptr) %
substream->runtime->buffer_size;
}
static const struct snd_pcm_ops snd_cobalt_pcm_capture_ops = {
.open = snd_cobalt_pcm_capture_open,
.close = snd_cobalt_pcm_capture_close,
.prepare = snd_cobalt_pcm_prepare,
.trigger = snd_cobalt_pcm_trigger,
.pointer = snd_cobalt_pcm_pointer,
};
static const struct snd_pcm_ops snd_cobalt_pcm_playback_ops = {
.open = snd_cobalt_pcm_playback_open,
.close = snd_cobalt_pcm_playback_close,
.prepare = snd_cobalt_pcm_pb_prepare,
.trigger = snd_cobalt_pcm_pb_trigger,
.pointer = snd_cobalt_pcm_pb_pointer,
};
int snd_cobalt_pcm_create(struct snd_cobalt_card *cobsc)
{
struct snd_pcm *sp;
struct snd_card *sc = cobsc->sc;
struct cobalt_stream *s = cobsc->s;
struct cobalt *cobalt = s->cobalt;
int ret;
s->q.gfp_flags |= __GFP_ZERO;
if (!s->is_output) {
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_AUDIO_IPP_RESETN_BIT(s->video_channel),
0);
mdelay(2);
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_AUDIO_IPP_RESETN_BIT(s->video_channel),
1);
mdelay(1);
ret = snd_pcm_new(sc, "Cobalt PCM-In HDMI",
0, /* PCM device 0, the only one for this card */
0, /* 0 playback substreams */
1, /* 1 capture substream */
&sp);
if (ret) {
cobalt_err("snd_cobalt_pcm_create() failed for input with err %d\n",
ret);
goto err_exit;
}
snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
&snd_cobalt_pcm_capture_ops);
snd_pcm_set_managed_buffer_all(sp, SNDRV_DMA_TYPE_VMALLOC,
NULL, 0, 0);
sp->info_flags = 0;
sp->private_data = cobsc;
strscpy(sp->name, "cobalt", sizeof(sp->name));
} else {
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_AUDIO_OPP_RESETN_BIT, 0);
mdelay(2);
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_AUDIO_OPP_RESETN_BIT, 1);
mdelay(1);
ret = snd_pcm_new(sc, "Cobalt PCM-Out HDMI",
0, /* PCM device 0, the only one for this card */
1, /* 0 playback substreams */
0, /* 1 capture substream */
&sp);
if (ret) {
cobalt_err("snd_cobalt_pcm_create() failed for output with err %d\n",
ret);
goto err_exit;
}
snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_PLAYBACK,
&snd_cobalt_pcm_playback_ops);
snd_pcm_set_managed_buffer_all(sp, SNDRV_DMA_TYPE_VMALLOC,
NULL, 0, 0);
sp->info_flags = 0;
sp->private_data = cobsc;
strscpy(sp->name, "cobalt", sizeof(sp->name));
}
return 0;
err_exit:
return ret;
}
| linux-master | drivers/media/pci/cobalt/cobalt-alsa-pcm.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* cobalt interrupt handling
*
* Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include <media/i2c/adv7604.h>
#include "cobalt-driver.h"
#include "cobalt-irq.h"
#include "cobalt-omnitek.h"
static void cobalt_dma_stream_queue_handler(struct cobalt_stream *s)
{
struct cobalt *cobalt = s->cobalt;
int rx = s->video_channel;
struct m00473_freewheel_regmap __iomem *fw =
COBALT_CVI_FREEWHEEL(s->cobalt, rx);
struct m00233_video_measure_regmap __iomem *vmr =
COBALT_CVI_VMR(s->cobalt, rx);
struct m00389_cvi_regmap __iomem *cvi =
COBALT_CVI(s->cobalt, rx);
struct m00479_clk_loss_detector_regmap __iomem *clkloss =
COBALT_CVI_CLK_LOSS(s->cobalt, rx);
struct cobalt_buffer *cb;
bool skip = false;
spin_lock(&s->irqlock);
if (list_empty(&s->bufs)) {
pr_err("no buffers!\n");
spin_unlock(&s->irqlock);
return;
}
/* Give the fresh filled up buffer to the user.
* Note that the interrupt is only sent if the DMA can continue
* with a new buffer, so it is always safe to return this buffer
* to userspace. */
cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
list_del(&cb->list);
spin_unlock(&s->irqlock);
if (s->is_audio || s->is_output)
goto done;
if (s->unstable_frame) {
uint32_t stat = ioread32(&vmr->irq_status);
iowrite32(stat, &vmr->irq_status);
if (!(ioread32(&vmr->status) &
M00233_STATUS_BITMAP_INIT_DONE_MSK)) {
cobalt_dbg(1, "!init_done\n");
if (s->enable_freewheel)
goto restart_fw;
goto done;
}
if (ioread32(&clkloss->status) &
M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) {
iowrite32(0, &clkloss->ctrl);
iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
cobalt_dbg(1, "no clock\n");
if (s->enable_freewheel)
goto restart_fw;
goto done;
}
if ((stat & (M00233_IRQ_STATUS_BITMAP_VACTIVE_AREA_MSK |
M00233_IRQ_STATUS_BITMAP_HACTIVE_AREA_MSK)) ||
ioread32(&vmr->vactive_area) != s->timings.bt.height ||
ioread32(&vmr->hactive_area) != s->timings.bt.width) {
cobalt_dbg(1, "unstable\n");
if (s->enable_freewheel)
goto restart_fw;
goto done;
}
if (!s->enable_cvi) {
s->enable_cvi = true;
iowrite32(M00389_CONTROL_BITMAP_ENABLE_MSK, &cvi->control);
goto done;
}
if (!(ioread32(&cvi->status) & M00389_STATUS_BITMAP_LOCK_MSK)) {
cobalt_dbg(1, "cvi no lock\n");
if (s->enable_freewheel)
goto restart_fw;
goto done;
}
if (!s->enable_freewheel) {
cobalt_dbg(1, "stable\n");
s->enable_freewheel = true;
iowrite32(0, &fw->ctrl);
goto done;
}
cobalt_dbg(1, "enabled fw\n");
iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK |
M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK,
&vmr->control);
iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK, &fw->ctrl);
s->enable_freewheel = false;
s->unstable_frame = false;
s->skip_first_frames = 2;
skip = true;
goto done;
}
if (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) {
restart_fw:
cobalt_dbg(1, "lost lock\n");
iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK,
&vmr->control);
iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK,
&fw->ctrl);
iowrite32(0, &cvi->control);
s->unstable_frame = true;
s->enable_freewheel = false;
s->enable_cvi = false;
}
done:
if (s->skip_first_frames) {
skip = true;
s->skip_first_frames--;
}
cb->vb.vb2_buf.timestamp = ktime_get_ns();
/* TODO: the sequence number should be read from the FPGA so we
also know about dropped frames. */
cb->vb.sequence = s->sequence++;
vb2_buffer_done(&cb->vb.vb2_buf,
(skip || s->unstable_frame) ?
VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
}
irqreturn_t cobalt_irq_handler(int irq, void *dev_id)
{
struct cobalt *cobalt = (struct cobalt *)dev_id;
u32 dma_interrupt =
cobalt_read_bar0(cobalt, DMA_INTERRUPT_STATUS_REG) & 0xffff;
u32 mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
u32 edge = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_EDGE);
int i;
/* Clear DMA interrupt */
cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG, dma_interrupt);
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, mask & ~edge);
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_EDGE, edge);
for (i = 0; i < COBALT_NUM_STREAMS; i++) {
struct cobalt_stream *s = &cobalt->streams[i];
unsigned dma_fifo_mask = s->dma_fifo_mask;
if (dma_interrupt & (1 << s->dma_channel)) {
cobalt->irq_dma[i]++;
/* Give fresh buffer to user and chain newly
* queued buffers */
cobalt_dma_stream_queue_handler(s);
if (!s->is_audio) {
edge &= ~dma_fifo_mask;
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
mask & ~edge);
}
}
if (s->is_audio)
continue;
if (edge & s->adv_irq_mask)
set_bit(COBALT_STREAM_FL_ADV_IRQ, &s->flags);
if ((edge & mask & dma_fifo_mask) && vb2_is_streaming(&s->q)) {
cobalt_info("full rx FIFO %d\n", i);
cobalt->irq_full_fifo++;
}
}
queue_work(cobalt->irq_work_queues, &cobalt->irq_work_queue);
if (edge & mask & (COBALT_SYSSTAT_VI0_INT1_MSK |
COBALT_SYSSTAT_VI1_INT1_MSK |
COBALT_SYSSTAT_VI2_INT1_MSK |
COBALT_SYSSTAT_VI3_INT1_MSK |
COBALT_SYSSTAT_VIHSMA_INT1_MSK |
COBALT_SYSSTAT_VOHSMA_INT1_MSK))
cobalt->irq_adv1++;
if (edge & mask & (COBALT_SYSSTAT_VI0_INT2_MSK |
COBALT_SYSSTAT_VI1_INT2_MSK |
COBALT_SYSSTAT_VI2_INT2_MSK |
COBALT_SYSSTAT_VI3_INT2_MSK |
COBALT_SYSSTAT_VIHSMA_INT2_MSK))
cobalt->irq_adv2++;
if (edge & mask & COBALT_SYSSTAT_VOHSMA_INT1_MSK)
cobalt->irq_advout++;
if (dma_interrupt)
cobalt->irq_dma_tot++;
if (!(edge & mask) && !dma_interrupt)
cobalt->irq_none++;
dma_interrupt = cobalt_read_bar0(cobalt, DMA_INTERRUPT_STATUS_REG);
return IRQ_HANDLED;
}
void cobalt_irq_work_handler(struct work_struct *work)
{
struct cobalt *cobalt =
container_of(work, struct cobalt, irq_work_queue);
int i;
for (i = 0; i < COBALT_NUM_NODES; i++) {
struct cobalt_stream *s = &cobalt->streams[i];
if (test_and_clear_bit(COBALT_STREAM_FL_ADV_IRQ, &s->flags)) {
u32 mask;
v4l2_subdev_call(cobalt->streams[i].sd, core,
interrupt_service_routine, 0, NULL);
mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
mask | s->adv_irq_mask);
}
}
}
void cobalt_irq_log_status(struct cobalt *cobalt)
{
u32 mask;
int i;
cobalt_info("irq: adv1=%u adv2=%u advout=%u none=%u full=%u\n",
cobalt->irq_adv1, cobalt->irq_adv2, cobalt->irq_advout,
cobalt->irq_none, cobalt->irq_full_fifo);
cobalt_info("irq: dma_tot=%u (", cobalt->irq_dma_tot);
for (i = 0; i < COBALT_NUM_STREAMS; i++)
pr_cont("%s%u", i ? "/" : "", cobalt->irq_dma[i]);
pr_cont(")\n");
cobalt->irq_dma_tot = cobalt->irq_adv1 = cobalt->irq_adv2 = 0;
cobalt->irq_advout = cobalt->irq_none = cobalt->irq_full_fifo = 0;
memset(cobalt->irq_dma, 0, sizeof(cobalt->irq_dma));
mask = cobalt_read_bar1(cobalt, COBALT_SYS_STAT_MASK);
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK,
mask |
COBALT_SYSSTAT_VI0_LOST_DATA_MSK |
COBALT_SYSSTAT_VI1_LOST_DATA_MSK |
COBALT_SYSSTAT_VI2_LOST_DATA_MSK |
COBALT_SYSSTAT_VI3_LOST_DATA_MSK |
COBALT_SYSSTAT_VIHSMA_LOST_DATA_MSK |
COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK |
COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK |
COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK);
}
| linux-master | drivers/media/pci/cobalt/cobalt-irq.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Omnitek Scatter-Gather DMA Controller
*
* Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include <linux/string.h>
#include <linux/io.h>
#include <linux/pci_regs.h>
#include <linux/spinlock.h>
#include "cobalt-driver.h"
#include "cobalt-omnitek.h"
/* descriptor */
#define END_OF_CHAIN (1 << 1)
#define INTERRUPT_ENABLE (1 << 2)
#define WRITE_TO_PCI (1 << 3)
#define READ_FROM_PCI (0 << 3)
#define DESCRIPTOR_FLAG_MSK (END_OF_CHAIN | INTERRUPT_ENABLE | WRITE_TO_PCI)
#define NEXT_ADRS_MSK 0xffffffe0
/* control/status register */
#define ENABLE (1 << 0)
#define START (1 << 1)
#define ABORT (1 << 2)
#define DONE (1 << 4)
#define SG_INTERRUPT (1 << 5)
#define EVENT_INTERRUPT (1 << 6)
#define SCATTER_GATHER_MODE (1 << 8)
#define DISABLE_VIDEO_RESYNC (1 << 9)
#define EVENT_INTERRUPT_ENABLE (1 << 10)
#define DIRECTIONAL_MSK (3 << 16)
#define INPUT_ONLY (0 << 16)
#define OUTPUT_ONLY (1 << 16)
#define BIDIRECTIONAL (2 << 16)
#define DMA_TYPE_MEMORY (0 << 18)
#define DMA_TYPE_FIFO (1 << 18)
#define BASE (cobalt->bar0)
#define CAPABILITY_HEADER (BASE)
#define CAPABILITY_REGISTER (BASE + 0x04)
#define PCI_64BIT (1 << 8)
#define LOCAL_64BIT (1 << 9)
#define INTERRUPT_STATUS (BASE + 0x08)
#define PCI(c) (BASE + 0x40 + ((c) * 0x40))
#define SIZE(c) (BASE + 0x58 + ((c) * 0x40))
#define DESCRIPTOR(c) (BASE + 0x50 + ((c) * 0x40))
#define CS_REG(c) (BASE + 0x60 + ((c) * 0x40))
#define BYTES_TRANSFERRED(c) (BASE + 0x64 + ((c) * 0x40))
static char *get_dma_direction(u32 status)
{
switch (status & DIRECTIONAL_MSK) {
case INPUT_ONLY: return "Input";
case OUTPUT_ONLY: return "Output";
case BIDIRECTIONAL: return "Bidirectional";
}
return "";
}
static void show_dma_capability(struct cobalt *cobalt)
{
u32 header = ioread32(CAPABILITY_HEADER);
u32 capa = ioread32(CAPABILITY_REGISTER);
u32 i;
cobalt_info("Omnitek DMA capability: ID 0x%02x Version 0x%02x Next 0x%x Size 0x%x\n",
header & 0xff, (header >> 8) & 0xff,
(header >> 16) & 0xffff, (capa >> 24) & 0xff);
switch ((capa >> 8) & 0x3) {
case 0:
cobalt_info("Omnitek DMA: 32 bits PCIe and Local\n");
break;
case 1:
cobalt_info("Omnitek DMA: 64 bits PCIe, 32 bits Local\n");
break;
case 3:
cobalt_info("Omnitek DMA: 64 bits PCIe and Local\n");
break;
}
for (i = 0; i < (capa & 0xf); i++) {
u32 status = ioread32(CS_REG(i));
cobalt_info("Omnitek DMA channel #%d: %s %s\n", i,
status & DMA_TYPE_FIFO ? "FIFO" : "MEMORY",
get_dma_direction(status));
}
}
void omni_sg_dma_start(struct cobalt_stream *s, struct sg_dma_desc_info *desc)
{
struct cobalt *cobalt = s->cobalt;
iowrite32((u32)((u64)desc->bus >> 32), DESCRIPTOR(s->dma_channel) + 4);
iowrite32((u32)desc->bus & NEXT_ADRS_MSK, DESCRIPTOR(s->dma_channel));
iowrite32(ENABLE | SCATTER_GATHER_MODE | START, CS_REG(s->dma_channel));
}
bool is_dma_done(struct cobalt_stream *s)
{
struct cobalt *cobalt = s->cobalt;
if (ioread32(CS_REG(s->dma_channel)) & DONE)
return true;
return false;
}
void omni_sg_dma_abort_channel(struct cobalt_stream *s)
{
struct cobalt *cobalt = s->cobalt;
if (!is_dma_done(s))
iowrite32(ABORT, CS_REG(s->dma_channel));
}
int omni_sg_dma_init(struct cobalt *cobalt)
{
u32 capa = ioread32(CAPABILITY_REGISTER);
int i;
cobalt->first_fifo_channel = 0;
cobalt->dma_channels = capa & 0xf;
if (capa & PCI_64BIT)
cobalt->pci_32_bit = false;
else
cobalt->pci_32_bit = true;
for (i = 0; i < cobalt->dma_channels; i++) {
u32 status = ioread32(CS_REG(i));
u32 ctrl = ioread32(CS_REG(i));
if (!(ctrl & DONE))
iowrite32(ABORT, CS_REG(i));
if (!(status & DMA_TYPE_FIFO))
cobalt->first_fifo_channel++;
}
show_dma_capability(cobalt);
return 0;
}
int descriptor_list_create(struct cobalt *cobalt,
struct scatterlist *scatter_list, bool to_pci, unsigned sglen,
unsigned size, unsigned width, unsigned stride,
struct sg_dma_desc_info *desc)
{
struct sg_dma_descriptor *d = (struct sg_dma_descriptor *)desc->virt;
dma_addr_t next = desc->bus;
unsigned offset = 0;
unsigned copy_bytes = width;
unsigned copied = 0;
bool first = true;
/* Must be 4-byte aligned */
WARN_ON(sg_dma_address(scatter_list) & 3);
WARN_ON(size & 3);
WARN_ON(next & 3);
WARN_ON(stride & 3);
WARN_ON(stride < width);
if (width >= stride)
copy_bytes = stride = size;
while (size) {
dma_addr_t addr = sg_dma_address(scatter_list) + offset;
unsigned bytes;
if (addr == 0)
return -EFAULT;
if (cobalt->pci_32_bit) {
WARN_ON((u64)addr >> 32);
if ((u64)addr >> 32)
return -EFAULT;
}
/* PCIe address */
d->pci_l = addr & 0xffffffff;
/* If dma_addr_t is 32 bits, then addr >> 32 is actually the
equivalent of addr >> 0 in gcc. So must cast to u64. */
d->pci_h = (u64)addr >> 32;
/* Sync to start of streaming frame */
d->local = 0;
d->reserved0 = 0;
/* Transfer bytes */
bytes = min(sg_dma_len(scatter_list) - offset,
copy_bytes - copied);
if (first) {
if (to_pci)
d->local = 0x11111111;
first = false;
if (sglen == 1) {
/* Make sure there are always at least two
* descriptors */
d->bytes = (bytes / 2) & ~3;
d->reserved1 = 0;
size -= d->bytes;
copied += d->bytes;
offset += d->bytes;
addr += d->bytes;
next += sizeof(struct sg_dma_descriptor);
d->next_h = (u32)((u64)next >> 32);
d->next_l = (u32)next |
(to_pci ? WRITE_TO_PCI : 0);
bytes -= d->bytes;
d++;
/* PCIe address */
d->pci_l = addr & 0xffffffff;
/* If dma_addr_t is 32 bits, then addr >> 32
* is actually the equivalent of addr >> 0 in
* gcc. So must cast to u64. */
d->pci_h = (u64)addr >> 32;
/* Sync to start of streaming frame */
d->local = 0;
d->reserved0 = 0;
}
}
d->bytes = bytes;
d->reserved1 = 0;
size -= bytes;
copied += bytes;
offset += bytes;
if (copied == copy_bytes) {
while (copied < stride) {
bytes = min(sg_dma_len(scatter_list) - offset,
stride - copied);
copied += bytes;
offset += bytes;
size -= bytes;
if (sg_dma_len(scatter_list) == offset) {
offset = 0;
scatter_list = sg_next(scatter_list);
}
}
copied = 0;
} else {
offset = 0;
scatter_list = sg_next(scatter_list);
}
/* Next descriptor + control bits */
next += sizeof(struct sg_dma_descriptor);
if (size == 0) {
/* Loopback to the first descriptor */
d->next_h = (u32)((u64)desc->bus >> 32);
d->next_l = (u32)desc->bus |
(to_pci ? WRITE_TO_PCI : 0) | INTERRUPT_ENABLE;
if (!to_pci)
d->local = 0x22222222;
desc->last_desc_virt = d;
} else {
d->next_h = (u32)((u64)next >> 32);
d->next_l = (u32)next | (to_pci ? WRITE_TO_PCI : 0);
}
d++;
}
return 0;
}
void descriptor_list_chain(struct sg_dma_desc_info *this,
struct sg_dma_desc_info *next)
{
struct sg_dma_descriptor *d = this->last_desc_virt;
u32 direction = d->next_l & WRITE_TO_PCI;
if (next == NULL) {
d->next_h = 0;
d->next_l = direction | INTERRUPT_ENABLE | END_OF_CHAIN;
} else {
d->next_h = (u32)((u64)next->bus >> 32);
d->next_l = (u32)next->bus | direction | INTERRUPT_ENABLE;
}
}
void *descriptor_list_allocate(struct sg_dma_desc_info *desc, size_t bytes)
{
desc->size = bytes;
desc->virt = dma_alloc_coherent(desc->dev, bytes,
&desc->bus, GFP_KERNEL);
return desc->virt;
}
void descriptor_list_free(struct sg_dma_desc_info *desc)
{
if (desc->virt)
dma_free_coherent(desc->dev, desc->size,
desc->virt, desc->bus);
desc->virt = NULL;
}
void descriptor_list_interrupt_enable(struct sg_dma_desc_info *desc)
{
struct sg_dma_descriptor *d = desc->last_desc_virt;
d->next_l |= INTERRUPT_ENABLE;
}
void descriptor_list_interrupt_disable(struct sg_dma_desc_info *desc)
{
struct sg_dma_descriptor *d = desc->last_desc_virt;
d->next_l &= ~INTERRUPT_ENABLE;
}
void descriptor_list_loopback(struct sg_dma_desc_info *desc)
{
struct sg_dma_descriptor *d = desc->last_desc_virt;
d->next_h = (u32)((u64)desc->bus >> 32);
d->next_l = (u32)desc->bus | (d->next_l & DESCRIPTOR_FLAG_MSK);
}
void descriptor_list_end_of_chain(struct sg_dma_desc_info *desc)
{
struct sg_dma_descriptor *d = desc->last_desc_virt;
d->next_l |= END_OF_CHAIN;
}
| linux-master | drivers/media/pci/cobalt/cobalt-omnitek.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* cobalt driver initialization and card probing
*
* Derived from cx18-driver.c
*
* Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include <linux/delay.h>
#include <media/i2c/adv7604.h>
#include <media/i2c/adv7842.h>
#include <media/i2c/adv7511.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ctrls.h>
#include "cobalt-driver.h"
#include "cobalt-irq.h"
#include "cobalt-i2c.h"
#include "cobalt-v4l2.h"
#include "cobalt-flash.h"
#include "cobalt-alsa.h"
#include "cobalt-omnitek.h"
/* add your revision and whatnot here */
static const struct pci_device_id cobalt_pci_tbl[] = {
{PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_COBALT,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
{0,}
};
MODULE_DEVICE_TABLE(pci, cobalt_pci_tbl);
static atomic_t cobalt_instance = ATOMIC_INIT(0);
int cobalt_debug;
module_param_named(debug, cobalt_debug, int, 0644);
MODULE_PARM_DESC(debug, "Debug level. Default: 0\n");
int cobalt_ignore_err;
module_param_named(ignore_err, cobalt_ignore_err, int, 0644);
MODULE_PARM_DESC(ignore_err,
"If set then ignore missing i2c adapters/receivers. Default: 0\n");
MODULE_AUTHOR("Hans Verkuil <[email protected]> & Morten Hestnes");
MODULE_DESCRIPTION("cobalt driver");
MODULE_LICENSE("GPL");
static u8 edid[256] = {
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
0x50, 0x21, 0x32, 0x27, 0x00, 0x00, 0x00, 0x00,
0x22, 0x1a, 0x01, 0x03, 0x80, 0x30, 0x1b, 0x78,
0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
0x0f, 0x50, 0x54, 0x2f, 0xcf, 0x00, 0x31, 0x59,
0x45, 0x59, 0x61, 0x59, 0x81, 0x99, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
0x45, 0x00, 0xe0, 0x0e, 0x11, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
0x5e, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x63,
0x6f, 0x62, 0x61, 0x6c, 0x74, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9d,
0x02, 0x03, 0x1f, 0xf1, 0x4a, 0x10, 0x1f, 0x04,
0x13, 0x22, 0x21, 0x20, 0x02, 0x11, 0x01, 0x23,
0x09, 0x07, 0x07, 0x68, 0x03, 0x0c, 0x00, 0x10,
0x00, 0x00, 0x22, 0x0f, 0xe2, 0x00, 0xca, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
};
static void cobalt_set_interrupt(struct cobalt *cobalt, bool enable)
{
if (enable) {
unsigned irqs = COBALT_SYSSTAT_VI0_INT1_MSK |
COBALT_SYSSTAT_VI1_INT1_MSK |
COBALT_SYSSTAT_VI2_INT1_MSK |
COBALT_SYSSTAT_VI3_INT1_MSK |
COBALT_SYSSTAT_VI0_INT2_MSK |
COBALT_SYSSTAT_VI1_INT2_MSK |
COBALT_SYSSTAT_VI2_INT2_MSK |
COBALT_SYSSTAT_VI3_INT2_MSK |
COBALT_SYSSTAT_VI0_LOST_DATA_MSK |
COBALT_SYSSTAT_VI1_LOST_DATA_MSK |
COBALT_SYSSTAT_VI2_LOST_DATA_MSK |
COBALT_SYSSTAT_VI3_LOST_DATA_MSK |
COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK;
if (cobalt->have_hsma_rx)
irqs |= COBALT_SYSSTAT_VIHSMA_INT1_MSK |
COBALT_SYSSTAT_VIHSMA_INT2_MSK |
COBALT_SYSSTAT_VIHSMA_LOST_DATA_MSK;
if (cobalt->have_hsma_tx)
irqs |= COBALT_SYSSTAT_VOHSMA_INT1_MSK |
COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK |
COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK;
/* Clear any existing interrupts */
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_EDGE, 0xffffffff);
/* PIO Core interrupt mask register.
Enable ADV7604 INT1 interrupts */
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, irqs);
} else {
/* Disable all ADV7604 interrupts */
cobalt_write_bar1(cobalt, COBALT_SYS_STAT_MASK, 0);
}
}
static unsigned cobalt_get_sd_nr(struct v4l2_subdev *sd)
{
struct cobalt *cobalt = to_cobalt(sd->v4l2_dev);
unsigned i;
for (i = 0; i < COBALT_NUM_NODES; i++)
if (sd == cobalt->streams[i].sd)
return i;
cobalt_err("Invalid adv7604 subdev pointer!\n");
return 0;
}
static void cobalt_notify(struct v4l2_subdev *sd,
unsigned int notification, void *arg)
{
struct cobalt *cobalt = to_cobalt(sd->v4l2_dev);
unsigned sd_nr = cobalt_get_sd_nr(sd);
struct cobalt_stream *s = &cobalt->streams[sd_nr];
bool hotplug = arg ? *((int *)arg) : false;
if (s->is_output)
return;
switch (notification) {
case ADV76XX_HOTPLUG:
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_HPD_TO_CONNECTOR_BIT(sd_nr), hotplug);
cobalt_dbg(1, "Set hotplug for adv %d to %d\n", sd_nr, hotplug);
break;
case V4L2_DEVICE_NOTIFY_EVENT:
cobalt_dbg(1, "Format changed for adv %d\n", sd_nr);
v4l2_event_queue(&s->vdev, arg);
break;
default:
break;
}
}
static int get_payload_size(u16 code)
{
switch (code) {
case 0: return 128;
case 1: return 256;
case 2: return 512;
case 3: return 1024;
case 4: return 2048;
case 5: return 4096;
default: return 0;
}
return 0;
}
static const char *get_link_speed(u16 stat)
{
switch (stat & PCI_EXP_LNKSTA_CLS) {
case 1: return "2.5 Gbit/s";
case 2: return "5 Gbit/s";
case 3: return "10 Gbit/s";
}
return "Unknown speed";
}
void cobalt_pcie_status_show(struct cobalt *cobalt)
{
struct pci_dev *pci_dev = cobalt->pci_dev;
struct pci_dev *pci_bus_dev = cobalt->pci_dev->bus->self;
u32 capa;
u16 stat, ctrl;
if (!pci_is_pcie(pci_dev) || !pci_is_pcie(pci_bus_dev))
return;
/* Device */
pcie_capability_read_dword(pci_dev, PCI_EXP_DEVCAP, &capa);
pcie_capability_read_word(pci_dev, PCI_EXP_DEVCTL, &ctrl);
pcie_capability_read_word(pci_dev, PCI_EXP_DEVSTA, &stat);
cobalt_info("PCIe device capability 0x%08x: Max payload %d\n",
capa, get_payload_size(capa & PCI_EXP_DEVCAP_PAYLOAD));
cobalt_info("PCIe device control 0x%04x: Max payload %d. Max read request %d\n",
ctrl,
get_payload_size((ctrl & PCI_EXP_DEVCTL_PAYLOAD) >> 5),
get_payload_size((ctrl & PCI_EXP_DEVCTL_READRQ) >> 12));
cobalt_info("PCIe device status 0x%04x\n", stat);
/* Link */
pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &capa);
pcie_capability_read_word(pci_dev, PCI_EXP_LNKCTL, &ctrl);
pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &stat);
cobalt_info("PCIe link capability 0x%08x: %s per lane and %u lanes\n",
capa, get_link_speed(capa),
(capa & PCI_EXP_LNKCAP_MLW) >> 4);
cobalt_info("PCIe link control 0x%04x\n", ctrl);
cobalt_info("PCIe link status 0x%04x: %s per lane and %u lanes\n",
stat, get_link_speed(stat),
(stat & PCI_EXP_LNKSTA_NLW) >> 4);
/* Bus */
pcie_capability_read_dword(pci_bus_dev, PCI_EXP_LNKCAP, &capa);
cobalt_info("PCIe bus link capability 0x%08x: %s per lane and %u lanes\n",
capa, get_link_speed(capa),
(capa & PCI_EXP_LNKCAP_MLW) >> 4);
/* Slot */
pcie_capability_read_dword(pci_dev, PCI_EXP_SLTCAP, &capa);
pcie_capability_read_word(pci_dev, PCI_EXP_SLTCTL, &ctrl);
pcie_capability_read_word(pci_dev, PCI_EXP_SLTSTA, &stat);
cobalt_info("PCIe slot capability 0x%08x\n", capa);
cobalt_info("PCIe slot control 0x%04x\n", ctrl);
cobalt_info("PCIe slot status 0x%04x\n", stat);
}
static unsigned pcie_link_get_lanes(struct cobalt *cobalt)
{
struct pci_dev *pci_dev = cobalt->pci_dev;
u16 link;
if (!pci_is_pcie(pci_dev))
return 0;
pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &link);
return (link & PCI_EXP_LNKSTA_NLW) >> 4;
}
static unsigned pcie_bus_link_get_lanes(struct cobalt *cobalt)
{
struct pci_dev *pci_dev = cobalt->pci_dev->bus->self;
u32 link;
if (!pci_is_pcie(pci_dev))
return 0;
pcie_capability_read_dword(pci_dev, PCI_EXP_LNKCAP, &link);
return (link & PCI_EXP_LNKCAP_MLW) >> 4;
}
static void msi_config_show(struct cobalt *cobalt, struct pci_dev *pci_dev)
{
u16 ctrl, data;
u32 adrs_l, adrs_h;
pci_read_config_word(pci_dev, 0x52, &ctrl);
cobalt_info("MSI %s\n", ctrl & 1 ? "enable" : "disable");
cobalt_info("MSI multiple message: Capable %u. Enable %u\n",
(1 << ((ctrl >> 1) & 7)), (1 << ((ctrl >> 4) & 7)));
if (ctrl & 0x80)
cobalt_info("MSI: 64-bit address capable\n");
pci_read_config_dword(pci_dev, 0x54, &adrs_l);
pci_read_config_dword(pci_dev, 0x58, &adrs_h);
pci_read_config_word(pci_dev, 0x5c, &data);
if (ctrl & 0x80)
cobalt_info("MSI: Address 0x%08x%08x. Data 0x%04x\n",
adrs_h, adrs_l, data);
else
cobalt_info("MSI: Address 0x%08x. Data 0x%04x\n",
adrs_l, data);
}
static void cobalt_pci_iounmap(struct cobalt *cobalt, struct pci_dev *pci_dev)
{
if (cobalt->bar0) {
pci_iounmap(pci_dev, cobalt->bar0);
cobalt->bar0 = NULL;
}
if (cobalt->bar1) {
pci_iounmap(pci_dev, cobalt->bar1);
cobalt->bar1 = NULL;
}
}
static void cobalt_free_msi(struct cobalt *cobalt, struct pci_dev *pci_dev)
{
free_irq(pci_dev->irq, (void *)cobalt);
pci_free_irq_vectors(pci_dev);
}
static int cobalt_setup_pci(struct cobalt *cobalt, struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
u32 ctrl;
int ret;
cobalt_dbg(1, "enabling pci device\n");
ret = pci_enable_device(pci_dev);
if (ret) {
cobalt_err("can't enable device\n");
return ret;
}
pci_set_master(pci_dev);
pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cobalt->card_rev);
pci_read_config_word(pci_dev, PCI_DEVICE_ID, &cobalt->device_id);
switch (cobalt->device_id) {
case PCI_DEVICE_ID_COBALT:
cobalt_info("PCI Express interface from Omnitek\n");
break;
default:
cobalt_info("PCI Express interface provider is unknown!\n");
break;
}
if (pcie_link_get_lanes(cobalt) != 8) {
cobalt_warn("PCI Express link width is %d lanes.\n",
pcie_link_get_lanes(cobalt));
if (pcie_bus_link_get_lanes(cobalt) < 8)
cobalt_warn("The current slot only supports %d lanes, for best performance 8 are needed\n",
pcie_bus_link_get_lanes(cobalt));
if (pcie_link_get_lanes(cobalt) != pcie_bus_link_get_lanes(cobalt)) {
cobalt_err("The card is most likely not seated correctly in the PCIe slot\n");
ret = -EIO;
goto err_disable;
}
}
if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(64))) {
ret = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
if (ret) {
cobalt_err("no suitable DMA available\n");
goto err_disable;
}
}
ret = pci_request_regions(pci_dev, "cobalt");
if (ret) {
cobalt_err("error requesting regions\n");
goto err_disable;
}
cobalt_pcie_status_show(cobalt);
cobalt->bar0 = pci_iomap(pci_dev, 0, 0);
cobalt->bar1 = pci_iomap(pci_dev, 1, 0);
if (cobalt->bar1 == NULL) {
cobalt->bar1 = pci_iomap(pci_dev, 2, 0);
cobalt_info("64-bit BAR\n");
}
if (!cobalt->bar0 || !cobalt->bar1) {
ret = -EIO;
goto err_release;
}
/* Reset the video inputs before enabling any interrupts */
ctrl = cobalt_read_bar1(cobalt, COBALT_SYS_CTRL_BASE);
cobalt_write_bar1(cobalt, COBALT_SYS_CTRL_BASE, ctrl & ~0xf00);
/* Disable interrupts to prevent any spurious interrupts
from being generated. */
cobalt_set_interrupt(cobalt, false);
if (pci_alloc_irq_vectors(pci_dev, 1, 1, PCI_IRQ_MSI) < 1) {
cobalt_err("Could not enable MSI\n");
ret = -EIO;
goto err_release;
}
msi_config_show(cobalt, pci_dev);
/* Register IRQ */
if (request_irq(pci_dev->irq, cobalt_irq_handler, IRQF_SHARED,
cobalt->v4l2_dev.name, (void *)cobalt)) {
cobalt_err("Failed to register irq %d\n", pci_dev->irq);
ret = -EIO;
goto err_msi;
}
omni_sg_dma_init(cobalt);
return 0;
err_msi:
pci_disable_msi(pci_dev);
err_release:
cobalt_pci_iounmap(cobalt, pci_dev);
pci_release_regions(pci_dev);
err_disable:
pci_disable_device(cobalt->pci_dev);
return ret;
}
static int cobalt_hdl_info_get(struct cobalt *cobalt)
{
int i;
for (i = 0; i < COBALT_HDL_INFO_SIZE; i++)
cobalt->hdl_info[i] =
ioread8(cobalt->bar1 + COBALT_HDL_INFO_BASE + i);
cobalt->hdl_info[COBALT_HDL_INFO_SIZE - 1] = '\0';
if (strstr(cobalt->hdl_info, COBALT_HDL_SEARCH_STR))
return 0;
return 1;
}
static void cobalt_stream_struct_init(struct cobalt *cobalt)
{
int i;
for (i = 0; i < COBALT_NUM_STREAMS; i++) {
struct cobalt_stream *s = &cobalt->streams[i];
s->cobalt = cobalt;
s->flags = 0;
s->is_audio = false;
s->is_output = false;
s->is_dummy = true;
/* The Memory DMA channels will always get a lower channel
* number than the FIFO DMA. Video input should map to the
* stream 0-3. The other can use stream struct from 4 and
* higher */
if (i <= COBALT_HSMA_IN_NODE) {
s->dma_channel = i + cobalt->first_fifo_channel;
s->video_channel = i;
s->dma_fifo_mask =
COBALT_SYSSTAT_VI0_LOST_DATA_MSK << (4 * i);
s->adv_irq_mask =
COBALT_SYSSTAT_VI0_INT1_MSK << (4 * i);
} else if (i >= COBALT_AUDIO_IN_STREAM &&
i <= COBALT_AUDIO_IN_STREAM + 4) {
unsigned idx = i - COBALT_AUDIO_IN_STREAM;
s->dma_channel = 6 + idx;
s->is_audio = true;
s->video_channel = idx;
s->dma_fifo_mask = COBALT_SYSSTAT_AUD_IN_LOST_DATA_MSK;
} else if (i == COBALT_HSMA_OUT_NODE) {
s->dma_channel = 11;
s->is_output = true;
s->video_channel = 5;
s->dma_fifo_mask = COBALT_SYSSTAT_VOHSMA_LOST_DATA_MSK;
s->adv_irq_mask = COBALT_SYSSTAT_VOHSMA_INT1_MSK;
} else if (i == COBALT_AUDIO_OUT_STREAM) {
s->dma_channel = 12;
s->is_audio = true;
s->is_output = true;
s->video_channel = 5;
s->dma_fifo_mask = COBALT_SYSSTAT_AUD_OUT_LOST_DATA_MSK;
} else {
/* FIXME: Memory DMA for debug purpose */
s->dma_channel = i - COBALT_NUM_NODES;
}
cobalt_info("stream #%d -> dma channel #%d <- video channel %d\n",
i, s->dma_channel, s->video_channel);
}
}
static int cobalt_subdevs_init(struct cobalt *cobalt)
{
static struct adv76xx_platform_data adv7604_pdata = {
.disable_pwrdnb = 1,
.ain_sel = ADV7604_AIN7_8_9_NC_SYNC_3_1,
.bus_order = ADV7604_BUS_ORDER_BRG,
.blank_data = 1,
.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0,
.int1_config = ADV76XX_INT1_CONFIG_ACTIVE_HIGH,
.dr_str_data = ADV76XX_DR_STR_HIGH,
.dr_str_clk = ADV76XX_DR_STR_HIGH,
.dr_str_sync = ADV76XX_DR_STR_HIGH,
.hdmi_free_run_mode = 1,
.inv_vs_pol = 1,
.inv_hs_pol = 1,
};
static struct i2c_board_info adv7604_info = {
.type = "adv7604",
.addr = 0x20,
.platform_data = &adv7604_pdata,
};
struct cobalt_stream *s = cobalt->streams;
int i;
for (i = 0; i < COBALT_NUM_INPUTS; i++) {
struct v4l2_subdev_format sd_fmt = {
.pad = ADV7604_PAD_SOURCE,
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.format.code = MEDIA_BUS_FMT_YUYV8_1X16,
};
struct v4l2_subdev_edid cobalt_edid = {
.pad = ADV76XX_PAD_HDMI_PORT_A,
.start_block = 0,
.blocks = 2,
.edid = edid,
};
int err;
s[i].pad_source = ADV7604_PAD_SOURCE;
s[i].i2c_adap = &cobalt->i2c_adap[i];
if (s[i].i2c_adap->dev.parent == NULL)
continue;
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(i), 1);
s[i].sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
s[i].i2c_adap, &adv7604_info, NULL);
if (!s[i].sd) {
if (cobalt_ignore_err)
continue;
return -ENODEV;
}
err = v4l2_subdev_call(s[i].sd, video, s_routing,
ADV76XX_PAD_HDMI_PORT_A, 0, 0);
if (err)
return err;
err = v4l2_subdev_call(s[i].sd, pad, set_edid,
&cobalt_edid);
if (err)
return err;
err = v4l2_subdev_call(s[i].sd, pad, set_fmt, NULL,
&sd_fmt);
if (err)
return err;
/* Reset channel video module */
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(i), 0);
mdelay(2);
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(i), 1);
mdelay(1);
s[i].is_dummy = false;
cobalt->streams[i + COBALT_AUDIO_IN_STREAM].is_dummy = false;
}
return 0;
}
static int cobalt_subdevs_hsma_init(struct cobalt *cobalt)
{
static struct adv7842_platform_data adv7842_pdata = {
.disable_pwrdnb = 1,
.ain_sel = ADV7842_AIN1_2_3_NC_SYNC_1_2,
.bus_order = ADV7842_BUS_ORDER_RBG,
.op_format_mode_sel = ADV7842_OP_FORMAT_MODE0,
.blank_data = 1,
.dr_str_data = 3,
.dr_str_clk = 3,
.dr_str_sync = 3,
.mode = ADV7842_MODE_HDMI,
.hdmi_free_run_enable = 1,
.vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P,
.i2c_sdp_io = 0x4a,
.i2c_sdp = 0x48,
.i2c_cp = 0x22,
.i2c_vdp = 0x24,
.i2c_afe = 0x26,
.i2c_hdmi = 0x34,
.i2c_repeater = 0x32,
.i2c_edid = 0x36,
.i2c_infoframe = 0x3e,
.i2c_cec = 0x40,
.i2c_avlink = 0x42,
};
static struct i2c_board_info adv7842_info = {
.type = "adv7842",
.addr = 0x20,
.platform_data = &adv7842_pdata,
};
struct v4l2_subdev_format sd_fmt = {
.pad = ADV7842_PAD_SOURCE,
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.format.code = MEDIA_BUS_FMT_YUYV8_1X16,
};
static struct adv7511_platform_data adv7511_pdata = {
.i2c_edid = 0x7e >> 1,
.i2c_cec = 0x7c >> 1,
.i2c_pktmem = 0x70 >> 1,
.cec_clk = 12000000,
};
static struct i2c_board_info adv7511_info = {
.type = "adv7511-v4l2",
.addr = 0x39, /* 0x39 or 0x3d */
.platform_data = &adv7511_pdata,
};
struct v4l2_subdev_edid cobalt_edid = {
.pad = ADV7842_EDID_PORT_A,
.start_block = 0,
.blocks = 2,
.edid = edid,
};
struct cobalt_stream *s = &cobalt->streams[COBALT_HSMA_IN_NODE];
s->i2c_adap = &cobalt->i2c_adap[COBALT_NUM_ADAPTERS - 1];
if (s->i2c_adap->dev.parent == NULL)
return 0;
cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(4), 1);
s->sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
s->i2c_adap, &adv7842_info, NULL);
if (s->sd) {
int err = v4l2_subdev_call(s->sd, pad, set_edid, &cobalt_edid);
if (err)
return err;
err = v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
&sd_fmt);
if (err)
return err;
cobalt->have_hsma_rx = true;
s->pad_source = ADV7842_PAD_SOURCE;
s->is_dummy = false;
cobalt->streams[4 + COBALT_AUDIO_IN_STREAM].is_dummy = false;
/* Reset channel video module */
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 0);
mdelay(2);
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 1);
mdelay(1);
return err;
}
cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_NRESET_TO_HDMI_BIT(4), 0);
cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_PWRDN0_TO_HSMA_TX_BIT, 0);
s++;
s->i2c_adap = &cobalt->i2c_adap[COBALT_NUM_ADAPTERS - 1];
s->sd = v4l2_i2c_new_subdev_board(&cobalt->v4l2_dev,
s->i2c_adap, &adv7511_info, NULL);
if (s->sd) {
/* A transmitter is hooked up, so we can set this bit */
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 1);
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_VIDEO_RX_RESETN_BIT(4), 0);
cobalt_s_bit_sysctrl(cobalt,
COBALT_SYS_CTRL_VIDEO_TX_RESETN_BIT, 1);
cobalt->have_hsma_tx = true;
v4l2_subdev_call(s->sd, core, s_power, 1);
v4l2_subdev_call(s->sd, video, s_stream, 1);
v4l2_subdev_call(s->sd, audio, s_stream, 1);
v4l2_ctrl_s_ctrl(v4l2_ctrl_find(s->sd->ctrl_handler,
V4L2_CID_DV_TX_MODE), V4L2_DV_TX_MODE_HDMI);
s->is_dummy = false;
cobalt->streams[COBALT_AUDIO_OUT_STREAM].is_dummy = false;
return 0;
}
return -ENODEV;
}
static int cobalt_probe(struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
struct cobalt *cobalt;
int retval = 0;
int i;
/* FIXME - module parameter arrays constrain max instances */
i = atomic_inc_return(&cobalt_instance) - 1;
cobalt = kzalloc(sizeof(struct cobalt), GFP_KERNEL);
if (cobalt == NULL)
return -ENOMEM;
cobalt->pci_dev = pci_dev;
cobalt->instance = i;
mutex_init(&cobalt->pci_lock);
retval = v4l2_device_register(&pci_dev->dev, &cobalt->v4l2_dev);
if (retval) {
pr_err("cobalt: v4l2_device_register of card %d failed\n",
cobalt->instance);
kfree(cobalt);
return retval;
}
snprintf(cobalt->v4l2_dev.name, sizeof(cobalt->v4l2_dev.name),
"cobalt-%d", cobalt->instance);
cobalt->v4l2_dev.notify = cobalt_notify;
cobalt_info("Initializing card %d\n", cobalt->instance);
cobalt->irq_work_queues =
create_singlethread_workqueue(cobalt->v4l2_dev.name);
if (cobalt->irq_work_queues == NULL) {
cobalt_err("Could not create workqueue\n");
retval = -ENOMEM;
goto err;
}
INIT_WORK(&cobalt->irq_work_queue, cobalt_irq_work_handler);
/* PCI Device Setup */
retval = cobalt_setup_pci(cobalt, pci_dev, pci_id);
if (retval != 0)
goto err_wq;
/* Show HDL version info */
if (cobalt_hdl_info_get(cobalt))
cobalt_info("Not able to read the HDL info\n");
else
cobalt_info("%s", cobalt->hdl_info);
retval = cobalt_i2c_init(cobalt);
if (retval)
goto err_pci;
cobalt_stream_struct_init(cobalt);
retval = cobalt_subdevs_init(cobalt);
if (retval)
goto err_i2c;
if (!(cobalt_read_bar1(cobalt, COBALT_SYS_STAT_BASE) &
COBALT_SYSSTAT_HSMA_PRSNTN_MSK)) {
retval = cobalt_subdevs_hsma_init(cobalt);
if (retval)
goto err_i2c;
}
retval = cobalt_nodes_register(cobalt);
if (retval) {
cobalt_err("Error %d registering device nodes\n", retval);
goto err_i2c;
}
cobalt_set_interrupt(cobalt, true);
v4l2_device_call_all(&cobalt->v4l2_dev, 0, core,
interrupt_service_routine, 0, NULL);
cobalt_info("Initialized cobalt card\n");
cobalt_flash_probe(cobalt);
return 0;
err_i2c:
cobalt_i2c_exit(cobalt);
cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 0);
err_pci:
cobalt_free_msi(cobalt, pci_dev);
cobalt_pci_iounmap(cobalt, pci_dev);
pci_release_regions(cobalt->pci_dev);
pci_disable_device(cobalt->pci_dev);
err_wq:
destroy_workqueue(cobalt->irq_work_queues);
err:
cobalt_err("error %d on initialization\n", retval);
v4l2_device_unregister(&cobalt->v4l2_dev);
kfree(cobalt);
return retval;
}
static void cobalt_remove(struct pci_dev *pci_dev)
{
struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
struct cobalt *cobalt = to_cobalt(v4l2_dev);
int i;
cobalt_flash_remove(cobalt);
cobalt_set_interrupt(cobalt, false);
flush_workqueue(cobalt->irq_work_queues);
cobalt_nodes_unregister(cobalt);
for (i = 0; i < COBALT_NUM_ADAPTERS; i++) {
struct v4l2_subdev *sd = cobalt->streams[i].sd;
struct i2c_client *client;
if (sd == NULL)
continue;
client = v4l2_get_subdevdata(sd);
v4l2_device_unregister_subdev(sd);
i2c_unregister_device(client);
}
cobalt_i2c_exit(cobalt);
cobalt_free_msi(cobalt, pci_dev);
cobalt_s_bit_sysctrl(cobalt, COBALT_SYS_CTRL_HSMA_TX_ENABLE_BIT, 0);
cobalt_pci_iounmap(cobalt, pci_dev);
pci_release_regions(cobalt->pci_dev);
pci_disable_device(cobalt->pci_dev);
destroy_workqueue(cobalt->irq_work_queues);
cobalt_info("removed cobalt card\n");
v4l2_device_unregister(v4l2_dev);
kfree(cobalt);
}
/* define a pci_driver for card detection */
static struct pci_driver cobalt_pci_driver = {
.name = "cobalt",
.id_table = cobalt_pci_tbl,
.probe = cobalt_probe,
.remove = cobalt_remove,
};
module_pci_driver(cobalt_pci_driver);
| linux-master | drivers/media/pci/cobalt/cobalt-driver.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Cobalt CPLD functions
*
* Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
* All rights reserved.
*/
#include <linux/delay.h>
#include "cobalt-cpld.h"
#define ADRS(offset) (COBALT_BUS_CPLD_BASE + offset)
static u16 cpld_read(struct cobalt *cobalt, u32 offset)
{
return cobalt_bus_read32(cobalt->bar1, ADRS(offset));
}
static void cpld_write(struct cobalt *cobalt, u32 offset, u16 val)
{
return cobalt_bus_write32(cobalt->bar1, ADRS(offset), val);
}
static void cpld_info_ver3(struct cobalt *cobalt)
{
u32 rd;
u32 tmp;
cobalt_info("CPLD System control register (read/write)\n");
cobalt_info("\t\tSystem control: 0x%04x (0x0f00)\n",
cpld_read(cobalt, 0));
cobalt_info("CPLD Clock control register (read/write)\n");
cobalt_info("\t\tClock control: 0x%04x (0x0000)\n",
cpld_read(cobalt, 0x04));
cobalt_info("CPLD HSMA Clk Osc register (read/write) - Must set wr trigger to load default values\n");
cobalt_info("\t\tRegister #7:\t0x%04x (0x0022)\n",
cpld_read(cobalt, 0x08));
cobalt_info("\t\tRegister #8:\t0x%04x (0x0047)\n",
cpld_read(cobalt, 0x0c));
cobalt_info("\t\tRegister #9:\t0x%04x (0x00fa)\n",
cpld_read(cobalt, 0x10));
cobalt_info("\t\tRegister #10:\t0x%04x (0x0061)\n",
cpld_read(cobalt, 0x14));
cobalt_info("\t\tRegister #11:\t0x%04x (0x001e)\n",
cpld_read(cobalt, 0x18));
cobalt_info("\t\tRegister #12:\t0x%04x (0x0045)\n",
cpld_read(cobalt, 0x1c));
cobalt_info("\t\tRegister #135:\t0x%04x\n",
cpld_read(cobalt, 0x20));
cobalt_info("\t\tRegister #137:\t0x%04x\n",
cpld_read(cobalt, 0x24));
cobalt_info("CPLD System status register (read only)\n");
cobalt_info("\t\tSystem status: 0x%04x\n",
cpld_read(cobalt, 0x28));
cobalt_info("CPLD MAXII info register (read only)\n");
cobalt_info("\t\tBoard serial number: 0x%04x\n",
cpld_read(cobalt, 0x2c));
cobalt_info("\t\tMAXII program revision: 0x%04x\n",
cpld_read(cobalt, 0x30));
cobalt_info("CPLD temp and voltage ADT7411 registers (read only)\n");
cobalt_info("\t\tBoard temperature: %u Celsius\n",
cpld_read(cobalt, 0x34) / 4);
cobalt_info("\t\tFPGA temperature: %u Celsius\n",
cpld_read(cobalt, 0x38) / 4);
rd = cpld_read(cobalt, 0x3c);
tmp = (rd * 33 * 1000) / (483 * 10);
cobalt_info("\t\tVDD 3V3: %u,%03uV\n", tmp / 1000, tmp % 1000);
rd = cpld_read(cobalt, 0x40);
tmp = (rd * 74 * 2197) / (27 * 1000);
cobalt_info("\t\tADC ch3 5V: %u,%03uV\n", tmp / 1000, tmp % 1000);
rd = cpld_read(cobalt, 0x44);
tmp = (rd * 74 * 2197) / (47 * 1000);
cobalt_info("\t\tADC ch4 3V: %u,%03uV\n", tmp / 1000, tmp % 1000);
rd = cpld_read(cobalt, 0x48);
tmp = (rd * 57 * 2197) / (47 * 1000);
cobalt_info("\t\tADC ch5 2V5: %u,%03uV\n", tmp / 1000, tmp % 1000);
rd = cpld_read(cobalt, 0x4c);
tmp = (rd * 2197) / 1000;
cobalt_info("\t\tADC ch6 1V8: %u,%03uV\n", tmp / 1000, tmp % 1000);
rd = cpld_read(cobalt, 0x50);
tmp = (rd * 2197) / 1000;
cobalt_info("\t\tADC ch7 1V5: %u,%03uV\n", tmp / 1000, tmp % 1000);
rd = cpld_read(cobalt, 0x54);
tmp = (rd * 2197) / 1000;
cobalt_info("\t\tADC ch8 0V9: %u,%03uV\n", tmp / 1000, tmp % 1000);
}
void cobalt_cpld_status(struct cobalt *cobalt)
{
u32 rev = cpld_read(cobalt, 0x30);
switch (rev) {
case 3:
case 4:
case 5:
cpld_info_ver3(cobalt);
break;
default:
cobalt_info("CPLD revision %u is not supported!\n", rev);
break;
}
}
#define DCO_MIN 4850000000ULL
#define DCO_MAX 5670000000ULL
#define SI570_CLOCK_CTRL 0x04
#define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_WR_TRIGGER 0x200
#define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_RST_TRIGGER 0x100
#define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL 0x80
#define S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN 0x40
#define SI570_REG7 0x08
#define SI570_REG8 0x0c
#define SI570_REG9 0x10
#define SI570_REG10 0x14
#define SI570_REG11 0x18
#define SI570_REG12 0x1c
#define SI570_REG135 0x20
#define SI570_REG137 0x24
struct multiplier {
unsigned mult, hsdiv, n1;
};
/* List all possible multipliers (= hsdiv * n1). There are lots of duplicates,
which are all removed in this list to keep the list as short as possible.
The values for hsdiv and n1 are the actual values, not the register values.
*/
static const struct multiplier multipliers[] = {
{ 4, 4, 1 }, { 5, 5, 1 }, { 6, 6, 1 },
{ 7, 7, 1 }, { 8, 4, 2 }, { 9, 9, 1 },
{ 10, 5, 2 }, { 11, 11, 1 }, { 12, 6, 2 },
{ 14, 7, 2 }, { 16, 4, 4 }, { 18, 9, 2 },
{ 20, 5, 4 }, { 22, 11, 2 }, { 24, 4, 6 },
{ 28, 7, 4 }, { 30, 5, 6 }, { 32, 4, 8 },
{ 36, 6, 6 }, { 40, 4, 10 }, { 42, 7, 6 },
{ 44, 11, 4 }, { 48, 4, 12 }, { 50, 5, 10 },
{ 54, 9, 6 }, { 56, 4, 14 }, { 60, 5, 12 },
{ 64, 4, 16 }, { 66, 11, 6 }, { 70, 5, 14 },
{ 72, 4, 18 }, { 80, 4, 20 }, { 84, 6, 14 },
{ 88, 11, 8 }, { 90, 5, 18 }, { 96, 4, 24 },
{ 98, 7, 14 }, { 100, 5, 20 }, { 104, 4, 26 },
{ 108, 6, 18 }, { 110, 11, 10 }, { 112, 4, 28 },
{ 120, 4, 30 }, { 126, 7, 18 }, { 128, 4, 32 },
{ 130, 5, 26 }, { 132, 11, 12 }, { 136, 4, 34 },
{ 140, 5, 28 }, { 144, 4, 36 }, { 150, 5, 30 },
{ 152, 4, 38 }, { 154, 11, 14 }, { 156, 6, 26 },
{ 160, 4, 40 }, { 162, 9, 18 }, { 168, 4, 42 },
{ 170, 5, 34 }, { 176, 11, 16 }, { 180, 5, 36 },
{ 182, 7, 26 }, { 184, 4, 46 }, { 190, 5, 38 },
{ 192, 4, 48 }, { 196, 7, 28 }, { 198, 11, 18 },
{ 198, 9, 22 }, { 200, 4, 50 }, { 204, 6, 34 },
{ 208, 4, 52 }, { 210, 5, 42 }, { 216, 4, 54 },
{ 220, 11, 20 }, { 224, 4, 56 }, { 228, 6, 38 },
{ 230, 5, 46 }, { 232, 4, 58 }, { 234, 9, 26 },
{ 238, 7, 34 }, { 240, 4, 60 }, { 242, 11, 22 },
{ 248, 4, 62 }, { 250, 5, 50 }, { 252, 6, 42 },
{ 256, 4, 64 }, { 260, 5, 52 }, { 264, 11, 24 },
{ 266, 7, 38 }, { 270, 5, 54 }, { 272, 4, 68 },
{ 276, 6, 46 }, { 280, 4, 70 }, { 286, 11, 26 },
{ 288, 4, 72 }, { 290, 5, 58 }, { 294, 7, 42 },
{ 296, 4, 74 }, { 300, 5, 60 }, { 304, 4, 76 },
{ 306, 9, 34 }, { 308, 11, 28 }, { 310, 5, 62 },
{ 312, 4, 78 }, { 320, 4, 80 }, { 322, 7, 46 },
{ 324, 6, 54 }, { 328, 4, 82 }, { 330, 11, 30 },
{ 336, 4, 84 }, { 340, 5, 68 }, { 342, 9, 38 },
{ 344, 4, 86 }, { 348, 6, 58 }, { 350, 5, 70 },
{ 352, 11, 32 }, { 360, 4, 90 }, { 364, 7, 52 },
{ 368, 4, 92 }, { 370, 5, 74 }, { 372, 6, 62 },
{ 374, 11, 34 }, { 376, 4, 94 }, { 378, 7, 54 },
{ 380, 5, 76 }, { 384, 4, 96 }, { 390, 5, 78 },
{ 392, 4, 98 }, { 396, 11, 36 }, { 400, 4, 100 },
{ 406, 7, 58 }, { 408, 4, 102 }, { 410, 5, 82 },
{ 414, 9, 46 }, { 416, 4, 104 }, { 418, 11, 38 },
{ 420, 5, 84 }, { 424, 4, 106 }, { 430, 5, 86 },
{ 432, 4, 108 }, { 434, 7, 62 }, { 440, 11, 40 },
{ 444, 6, 74 }, { 448, 4, 112 }, { 450, 5, 90 },
{ 456, 4, 114 }, { 460, 5, 92 }, { 462, 11, 42 },
{ 464, 4, 116 }, { 468, 6, 78 }, { 470, 5, 94 },
{ 472, 4, 118 }, { 476, 7, 68 }, { 480, 4, 120 },
{ 484, 11, 44 }, { 486, 9, 54 }, { 488, 4, 122 },
{ 490, 5, 98 }, { 492, 6, 82 }, { 496, 4, 124 },
{ 500, 5, 100 }, { 504, 4, 126 }, { 506, 11, 46 },
{ 510, 5, 102 }, { 512, 4, 128 }, { 516, 6, 86 },
{ 518, 7, 74 }, { 520, 5, 104 }, { 522, 9, 58 },
{ 528, 11, 48 }, { 530, 5, 106 }, { 532, 7, 76 },
{ 540, 5, 108 }, { 546, 7, 78 }, { 550, 11, 50 },
{ 552, 6, 92 }, { 558, 9, 62 }, { 560, 5, 112 },
{ 564, 6, 94 }, { 570, 5, 114 }, { 572, 11, 52 },
{ 574, 7, 82 }, { 576, 6, 96 }, { 580, 5, 116 },
{ 588, 6, 98 }, { 590, 5, 118 }, { 594, 11, 54 },
{ 600, 5, 120 }, { 602, 7, 86 }, { 610, 5, 122 },
{ 612, 6, 102 }, { 616, 11, 56 }, { 620, 5, 124 },
{ 624, 6, 104 }, { 630, 5, 126 }, { 636, 6, 106 },
{ 638, 11, 58 }, { 640, 5, 128 }, { 644, 7, 92 },
{ 648, 6, 108 }, { 658, 7, 94 }, { 660, 11, 60 },
{ 666, 9, 74 }, { 672, 6, 112 }, { 682, 11, 62 },
{ 684, 6, 114 }, { 686, 7, 98 }, { 696, 6, 116 },
{ 700, 7, 100 }, { 702, 9, 78 }, { 704, 11, 64 },
{ 708, 6, 118 }, { 714, 7, 102 }, { 720, 6, 120 },
{ 726, 11, 66 }, { 728, 7, 104 }, { 732, 6, 122 },
{ 738, 9, 82 }, { 742, 7, 106 }, { 744, 6, 124 },
{ 748, 11, 68 }, { 756, 6, 126 }, { 768, 6, 128 },
{ 770, 11, 70 }, { 774, 9, 86 }, { 784, 7, 112 },
{ 792, 11, 72 }, { 798, 7, 114 }, { 810, 9, 90 },
{ 812, 7, 116 }, { 814, 11, 74 }, { 826, 7, 118 },
{ 828, 9, 92 }, { 836, 11, 76 }, { 840, 7, 120 },
{ 846, 9, 94 }, { 854, 7, 122 }, { 858, 11, 78 },
{ 864, 9, 96 }, { 868, 7, 124 }, { 880, 11, 80 },
{ 882, 7, 126 }, { 896, 7, 128 }, { 900, 9, 100 },
{ 902, 11, 82 }, { 918, 9, 102 }, { 924, 11, 84 },
{ 936, 9, 104 }, { 946, 11, 86 }, { 954, 9, 106 },
{ 968, 11, 88 }, { 972, 9, 108 }, { 990, 11, 90 },
{ 1008, 9, 112 }, { 1012, 11, 92 }, { 1026, 9, 114 },
{ 1034, 11, 94 }, { 1044, 9, 116 }, { 1056, 11, 96 },
{ 1062, 9, 118 }, { 1078, 11, 98 }, { 1080, 9, 120 },
{ 1098, 9, 122 }, { 1100, 11, 100 }, { 1116, 9, 124 },
{ 1122, 11, 102 }, { 1134, 9, 126 }, { 1144, 11, 104 },
{ 1152, 9, 128 }, { 1166, 11, 106 }, { 1188, 11, 108 },
{ 1210, 11, 110 }, { 1232, 11, 112 }, { 1254, 11, 114 },
{ 1276, 11, 116 }, { 1298, 11, 118 }, { 1320, 11, 120 },
{ 1342, 11, 122 }, { 1364, 11, 124 }, { 1386, 11, 126 },
{ 1408, 11, 128 },
};
bool cobalt_cpld_set_freq(struct cobalt *cobalt, unsigned f_out)
{
const unsigned f_xtal = 39170000; /* xtal for si598 */
u64 dco;
u64 rfreq;
unsigned delta = 0xffffffff;
unsigned i_best = 0;
unsigned i;
u8 n1, hsdiv;
u8 regs[6];
int found = 0;
int retries = 3;
for (i = 0; i < ARRAY_SIZE(multipliers); i++) {
unsigned mult = multipliers[i].mult;
u32 d;
dco = (u64)f_out * mult;
if (dco < DCO_MIN || dco > DCO_MAX)
continue;
div_u64_rem((dco << 28) + f_xtal / 2, f_xtal, &d);
if (d < delta) {
found = 1;
i_best = i;
delta = d;
}
}
if (!found)
return false;
dco = (u64)f_out * multipliers[i_best].mult;
n1 = multipliers[i_best].n1 - 1;
hsdiv = multipliers[i_best].hsdiv - 4;
rfreq = div_u64(dco << 28, f_xtal);
cpld_read(cobalt, SI570_CLOCK_CTRL);
regs[0] = (hsdiv << 5) | (n1 >> 2);
regs[1] = ((n1 & 0x3) << 6) | (rfreq >> 32);
regs[2] = (rfreq >> 24) & 0xff;
regs[3] = (rfreq >> 16) & 0xff;
regs[4] = (rfreq >> 8) & 0xff;
regs[5] = rfreq & 0xff;
/* The sequence of clock_ctrl flags to set is very weird. It looks
like I have to reset it, then set the new frequency and reset it
again. It shouldn't be necessary to do a reset, but if I don't,
then a strange frequency is set (156.412034 MHz, or register values
0x01, 0xc7, 0xfc, 0x7f, 0x53, 0x62).
*/
cobalt_dbg(1, "%u: %6ph\n", f_out, regs);
while (retries--) {
u8 read_regs[6];
cpld_write(cobalt, SI570_CLOCK_CTRL,
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL);
usleep_range(10000, 15000);
cpld_write(cobalt, SI570_REG7, regs[0]);
cpld_write(cobalt, SI570_REG8, regs[1]);
cpld_write(cobalt, SI570_REG9, regs[2]);
cpld_write(cobalt, SI570_REG10, regs[3]);
cpld_write(cobalt, SI570_REG11, regs[4]);
cpld_write(cobalt, SI570_REG12, regs[5]);
cpld_write(cobalt, SI570_CLOCK_CTRL,
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_WR_TRIGGER);
usleep_range(10000, 15000);
cpld_write(cobalt, SI570_CLOCK_CTRL,
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL);
usleep_range(10000, 15000);
read_regs[0] = cpld_read(cobalt, SI570_REG7);
read_regs[1] = cpld_read(cobalt, SI570_REG8);
read_regs[2] = cpld_read(cobalt, SI570_REG9);
read_regs[3] = cpld_read(cobalt, SI570_REG10);
read_regs[4] = cpld_read(cobalt, SI570_REG11);
read_regs[5] = cpld_read(cobalt, SI570_REG12);
cpld_write(cobalt, SI570_CLOCK_CTRL,
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN |
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_FPGA_CTRL |
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_RST_TRIGGER);
usleep_range(10000, 15000);
cpld_write(cobalt, SI570_CLOCK_CTRL,
S01755_REG_CLOCK_CTRL_BITMAP_CLKHSMA_EN);
usleep_range(10000, 15000);
if (!memcmp(read_regs, regs, sizeof(read_regs)))
break;
cobalt_dbg(1, "retry: %6ph\n", read_regs);
}
if (2 - retries)
cobalt_info("Needed %d retries\n", 2 - retries);
return true;
}
| linux-master | drivers/media/pci/cobalt/cobalt-cpld.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
*
* Based on the audio support from the tw6869 driver:
* Copyright 2015 www.starterkit.ru <[email protected]>
*
* Based on:
* Driver for Intersil|Techwell TW6869 based DVR cards
* (c) 2011-12 liran <[email protected]> [Intersil|Techwell China]
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/control.h>
#include "tw686x.h"
#include "tw686x-regs.h"
#define AUDIO_CHANNEL_OFFSET 8
void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
unsigned int pb_status)
{
unsigned long flags;
unsigned int ch, pb;
for_each_set_bit(ch, &requests, max_channels(dev)) {
struct tw686x_audio_channel *ac = &dev->audio_channels[ch];
struct tw686x_audio_buf *done = NULL;
struct tw686x_audio_buf *next = NULL;
struct tw686x_dma_desc *desc;
pb = !!(pb_status & BIT(AUDIO_CHANNEL_OFFSET + ch));
spin_lock_irqsave(&ac->lock, flags);
/* Sanity check */
if (!ac->ss || !ac->curr_bufs[0] || !ac->curr_bufs[1]) {
spin_unlock_irqrestore(&ac->lock, flags);
continue;
}
if (!list_empty(&ac->buf_list)) {
next = list_first_entry(&ac->buf_list,
struct tw686x_audio_buf, list);
list_move_tail(&next->list, &ac->buf_list);
done = ac->curr_bufs[!pb];
ac->curr_bufs[pb] = next;
}
spin_unlock_irqrestore(&ac->lock, flags);
if (!done)
continue;
/*
* Checking for a non-nil dma_desc[pb]->virt buffer is
* the same as checking for memcpy DMA mode.
*/
desc = &ac->dma_descs[pb];
if (desc->virt) {
memcpy(done->virt, desc->virt,
dev->period_size);
} else {
u32 reg = pb ? ADMA_B_ADDR[ch] : ADMA_P_ADDR[ch];
reg_write(dev, reg, next->dma);
}
ac->ptr = done->dma - ac->buf[0].dma;
snd_pcm_period_elapsed(ac->ss);
}
}
/*
* Audio parameters are global and shared among all
* capture channels. The driver prevents changes to
* the parameters if any audio channel is capturing.
*/
static const struct snd_pcm_hardware tw686x_capture_hw = {
.info = (SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP_VALID),
.formats = SNDRV_PCM_FMTBIT_S16_LE,
.rates = SNDRV_PCM_RATE_8000_48000,
.rate_min = 8000,
.rate_max = 48000,
.channels_min = 1,
.channels_max = 1,
.buffer_bytes_max = TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
.period_bytes_min = AUDIO_DMA_SIZE_MIN,
.period_bytes_max = AUDIO_DMA_SIZE_MAX,
.periods_min = TW686X_AUDIO_PERIODS_MIN,
.periods_max = TW686X_AUDIO_PERIODS_MAX,
};
static int tw686x_pcm_open(struct snd_pcm_substream *ss)
{
struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
struct snd_pcm_runtime *rt = ss->runtime;
int err;
ac->ss = ss;
rt->hw = tw686x_capture_hw;
err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
if (err < 0)
return err;
return 0;
}
static int tw686x_pcm_close(struct snd_pcm_substream *ss)
{
struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
ac->ss = NULL;
return 0;
}
static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
{
struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
struct snd_pcm_runtime *rt = ss->runtime;
unsigned int period_size = snd_pcm_lib_period_bytes(ss);
struct tw686x_audio_buf *p_buf, *b_buf;
unsigned long flags;
int i;
spin_lock_irqsave(&dev->lock, flags);
/*
* Given the audio parameters are global (i.e. shared across
* DMA channels), we need to check new params are allowed.
*/
if (((dev->audio_rate != rt->rate) ||
(dev->period_size != period_size)) && dev->audio_enabled)
goto err_audio_busy;
tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
spin_unlock_irqrestore(&dev->lock, flags);
if (dev->audio_rate != rt->rate) {
u32 reg;
dev->audio_rate = rt->rate;
reg = ((125000000 / rt->rate) << 16) +
((125000000 % rt->rate) << 16) / rt->rate;
reg_write(dev, AUDIO_CONTROL2, reg);
}
if (dev->period_size != period_size) {
u32 reg;
dev->period_size = period_size;
reg = reg_read(dev, AUDIO_CONTROL1);
reg &= ~(AUDIO_DMA_SIZE_MASK << AUDIO_DMA_SIZE_SHIFT);
reg |= period_size << AUDIO_DMA_SIZE_SHIFT;
reg_write(dev, AUDIO_CONTROL1, reg);
}
if (rt->periods < TW686X_AUDIO_PERIODS_MIN ||
rt->periods > TW686X_AUDIO_PERIODS_MAX)
return -EINVAL;
spin_lock_irqsave(&ac->lock, flags);
INIT_LIST_HEAD(&ac->buf_list);
for (i = 0; i < rt->periods; i++) {
ac->buf[i].dma = rt->dma_addr + period_size * i;
ac->buf[i].virt = rt->dma_area + period_size * i;
INIT_LIST_HEAD(&ac->buf[i].list);
list_add_tail(&ac->buf[i].list, &ac->buf_list);
}
p_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
list_move_tail(&p_buf->list, &ac->buf_list);
b_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
list_move_tail(&b_buf->list, &ac->buf_list);
ac->curr_bufs[0] = p_buf;
ac->curr_bufs[1] = b_buf;
ac->ptr = 0;
if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY) {
reg_write(dev, ADMA_P_ADDR[ac->ch], p_buf->dma);
reg_write(dev, ADMA_B_ADDR[ac->ch], b_buf->dma);
}
spin_unlock_irqrestore(&ac->lock, flags);
return 0;
err_audio_busy:
spin_unlock_irqrestore(&dev->lock, flags);
return -EBUSY;
}
static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
{
struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
unsigned long flags;
int err = 0;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
if (ac->curr_bufs[0] && ac->curr_bufs[1]) {
spin_lock_irqsave(&dev->lock, flags);
dev->audio_enabled = 1;
tw686x_enable_channel(dev,
AUDIO_CHANNEL_OFFSET + ac->ch);
spin_unlock_irqrestore(&dev->lock, flags);
mod_timer(&dev->dma_delay_timer,
jiffies + msecs_to_jiffies(100));
} else {
err = -EIO;
}
break;
case SNDRV_PCM_TRIGGER_STOP:
spin_lock_irqsave(&dev->lock, flags);
dev->audio_enabled = 0;
tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
spin_unlock_irqrestore(&dev->lock, flags);
spin_lock_irqsave(&ac->lock, flags);
ac->curr_bufs[0] = NULL;
ac->curr_bufs[1] = NULL;
spin_unlock_irqrestore(&ac->lock, flags);
break;
default:
err = -EINVAL;
}
return err;
}
static snd_pcm_uframes_t tw686x_pcm_pointer(struct snd_pcm_substream *ss)
{
struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
return bytes_to_frames(ss->runtime, ac->ptr);
}
static const struct snd_pcm_ops tw686x_pcm_ops = {
.open = tw686x_pcm_open,
.close = tw686x_pcm_close,
.prepare = tw686x_pcm_prepare,
.trigger = tw686x_pcm_trigger,
.pointer = tw686x_pcm_pointer,
};
static int tw686x_snd_pcm_init(struct tw686x_dev *dev)
{
struct snd_card *card = dev->snd_card;
struct snd_pcm *pcm;
struct snd_pcm_substream *ss;
unsigned int i;
int err;
err = snd_pcm_new(card, card->driver, 0, 0, max_channels(dev), &pcm);
if (err < 0)
return err;
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &tw686x_pcm_ops);
snd_pcm_chip(pcm) = dev;
pcm->info_flags = 0;
strscpy(pcm->name, "tw686x PCM", sizeof(pcm->name));
for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
ss; ss = ss->next, i++)
snprintf(ss->name, sizeof(ss->name), "vch%u audio", i);
snd_pcm_set_managed_buffer_all(pcm,
SNDRV_DMA_TYPE_DEV,
&dev->pci_dev->dev,
TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX);
return 0;
}
static void tw686x_audio_dma_free(struct tw686x_dev *dev,
struct tw686x_audio_channel *ac)
{
int pb;
for (pb = 0; pb < 2; pb++) {
if (!ac->dma_descs[pb].virt)
continue;
dma_free_coherent(&dev->pci_dev->dev, ac->dma_descs[pb].size,
ac->dma_descs[pb].virt,
ac->dma_descs[pb].phys);
ac->dma_descs[pb].virt = NULL;
}
}
static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
struct tw686x_audio_channel *ac)
{
int pb;
/*
* In the memcpy DMA mode we allocate a coherent buffer
* and use it for the DMA capture. Otherwise, DMA
* acts on the ALSA buffers as received in pcm_prepare.
*/
if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY)
return 0;
for (pb = 0; pb < 2; pb++) {
u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch];
void *virt;
virt = dma_alloc_coherent(&dev->pci_dev->dev,
AUDIO_DMA_SIZE_MAX,
&ac->dma_descs[pb].phys, GFP_KERNEL);
if (!virt) {
dev_err(&dev->pci_dev->dev,
"dma%d: unable to allocate audio DMA %s-buffer\n",
ac->ch, pb ? "B" : "P");
return -ENOMEM;
}
ac->dma_descs[pb].virt = virt;
ac->dma_descs[pb].size = AUDIO_DMA_SIZE_MAX;
reg_write(dev, reg, ac->dma_descs[pb].phys);
}
return 0;
}
void tw686x_audio_free(struct tw686x_dev *dev)
{
unsigned long flags;
u32 dma_ch_mask;
u32 dma_cmd;
spin_lock_irqsave(&dev->lock, flags);
dma_cmd = reg_read(dev, DMA_CMD);
dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE);
reg_write(dev, DMA_CMD, dma_cmd & ~0xff00);
reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask & ~0xff00);
spin_unlock_irqrestore(&dev->lock, flags);
if (!dev->snd_card)
return;
snd_card_free(dev->snd_card);
dev->snd_card = NULL;
}
int tw686x_audio_init(struct tw686x_dev *dev)
{
struct pci_dev *pci_dev = dev->pci_dev;
struct snd_card *card;
int err, ch;
/* Enable external audio */
reg_write(dev, AUDIO_CONTROL1, BIT(0));
err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1,
SNDRV_DEFAULT_STR1,
THIS_MODULE, 0, &card);
if (err < 0)
return err;
dev->snd_card = card;
strscpy(card->driver, "tw686x", sizeof(card->driver));
strscpy(card->shortname, "tw686x", sizeof(card->shortname));
strscpy(card->longname, pci_name(pci_dev), sizeof(card->longname));
snd_card_set_dev(card, &pci_dev->dev);
for (ch = 0; ch < max_channels(dev); ch++) {
struct tw686x_audio_channel *ac;
ac = &dev->audio_channels[ch];
spin_lock_init(&ac->lock);
ac->dev = dev;
ac->ch = ch;
err = tw686x_audio_dma_alloc(dev, ac);
if (err < 0)
goto err_cleanup;
}
err = tw686x_snd_pcm_init(dev);
if (err < 0)
goto err_cleanup;
err = snd_card_register(card);
if (!err)
return 0;
err_cleanup:
for (ch = 0; ch < max_channels(dev); ch++) {
if (!dev->audio_channels[ch].dev)
continue;
tw686x_audio_dma_free(dev, &dev->audio_channels[ch]);
}
snd_card_free(card);
dev->snd_card = NULL;
return err;
}
| linux-master | drivers/media/pci/tw686x/tw686x-audio.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
*
* Based on original driver by Krzysztof Ha?asa:
* Copyright (C) 2015 Industrial Research Institute for Automation
* and Measurements PIAP
*
* Notes
* -----
*
* 1. Under stress-testing, it has been observed that the PCIe link
* goes down, without reason. Therefore, the driver takes special care
* to allow device hot-unplugging.
*
* 2. TW686X devices are capable of setting a few different DMA modes,
* including: scatter-gather, field and frame modes. However,
* under stress testings it has been found that the machine can
* freeze completely if DMA registers are programmed while streaming
* is active.
*
* Therefore, driver implements a dma_mode called 'memcpy' which
* avoids cycling the DMA buffers, and insteads allocates extra DMA buffers
* and then copies into vmalloc'ed user buffers.
*
* In addition to this, when streaming is on, the driver tries to access
* hardware registers as infrequently as possible. This is done by using
* a timer to limit the rate at which DMA is reset on DMA channels error.
*/
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci_ids.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include "tw686x.h"
#include "tw686x-regs.h"
/*
* This module parameter allows to control the DMA_TIMER_INTERVAL value.
* The DMA_TIMER_INTERVAL register controls the minimum DMA interrupt
* time span (iow, the maximum DMA interrupt rate) thus allowing for
* IRQ coalescing.
*
* The chip datasheet does not mention a time unit for this value, so
* users wanting fine-grain control over the interrupt rate should
* determine the desired value through testing.
*/
static u32 dma_interval = 0x00098968;
module_param(dma_interval, int, 0444);
MODULE_PARM_DESC(dma_interval, "Minimum time span for DMA interrupting host");
static unsigned int dma_mode = TW686X_DMA_MODE_MEMCPY;
static const char *dma_mode_name(unsigned int mode)
{
switch (mode) {
case TW686X_DMA_MODE_MEMCPY:
return "memcpy";
case TW686X_DMA_MODE_CONTIG:
return "contig";
case TW686X_DMA_MODE_SG:
return "sg";
default:
return "unknown";
}
}
static int tw686x_dma_mode_get(char *buffer, const struct kernel_param *kp)
{
return sprintf(buffer, "%s", dma_mode_name(dma_mode));
}
static int tw686x_dma_mode_set(const char *val, const struct kernel_param *kp)
{
if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_MEMCPY)))
dma_mode = TW686X_DMA_MODE_MEMCPY;
else if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_CONTIG)))
dma_mode = TW686X_DMA_MODE_CONTIG;
else if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_SG)))
dma_mode = TW686X_DMA_MODE_SG;
else
return -EINVAL;
return 0;
}
module_param_call(dma_mode, tw686x_dma_mode_set, tw686x_dma_mode_get,
&dma_mode, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(dma_mode, "DMA operation mode (memcpy/contig/sg, default=memcpy)");
void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel)
{
u32 dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
u32 dma_cmd = reg_read(dev, DMA_CMD);
dma_en &= ~BIT(channel);
dma_cmd &= ~BIT(channel);
/* Must remove it from pending too */
dev->pending_dma_en &= ~BIT(channel);
dev->pending_dma_cmd &= ~BIT(channel);
/* Stop DMA if no channels are enabled */
if (!dma_en)
dma_cmd = 0;
reg_write(dev, DMA_CHANNEL_ENABLE, dma_en);
reg_write(dev, DMA_CMD, dma_cmd);
}
void tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel)
{
u32 dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
u32 dma_cmd = reg_read(dev, DMA_CMD);
dev->pending_dma_en |= dma_en | BIT(channel);
dev->pending_dma_cmd |= dma_cmd | DMA_CMD_ENABLE | BIT(channel);
}
/*
* The purpose of this awful hack is to avoid enabling the DMA
* channels "too fast" which makes some TW686x devices very
* angry and freeze the CPU (see note 1).
*/
static void tw686x_dma_delay(struct timer_list *t)
{
struct tw686x_dev *dev = from_timer(dev, t, dma_delay_timer);
unsigned long flags;
spin_lock_irqsave(&dev->lock, flags);
reg_write(dev, DMA_CHANNEL_ENABLE, dev->pending_dma_en);
reg_write(dev, DMA_CMD, dev->pending_dma_cmd);
dev->pending_dma_en = 0;
dev->pending_dma_cmd = 0;
spin_unlock_irqrestore(&dev->lock, flags);
}
static void tw686x_reset_channels(struct tw686x_dev *dev, unsigned int ch_mask)
{
u32 dma_en, dma_cmd;
dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
dma_cmd = reg_read(dev, DMA_CMD);
/*
* Save pending register status, the timer will
* restore them.
*/
dev->pending_dma_en |= dma_en;
dev->pending_dma_cmd |= dma_cmd;
/* Disable the reset channels */
reg_write(dev, DMA_CHANNEL_ENABLE, dma_en & ~ch_mask);
if ((dma_en & ~ch_mask) == 0) {
dev_dbg(&dev->pci_dev->dev, "reset: stopping DMA\n");
dma_cmd &= ~DMA_CMD_ENABLE;
}
reg_write(dev, DMA_CMD, dma_cmd & ~ch_mask);
}
static irqreturn_t tw686x_irq(int irq, void *dev_id)
{
struct tw686x_dev *dev = (struct tw686x_dev *)dev_id;
unsigned int video_requests, audio_requests, reset_ch;
u32 fifo_status, fifo_signal, fifo_ov, fifo_bad, fifo_errors;
u32 int_status, dma_en, video_en, pb_status;
unsigned long flags;
int_status = reg_read(dev, INT_STATUS); /* cleared on read */
fifo_status = reg_read(dev, VIDEO_FIFO_STATUS);
/* INT_STATUS does not include FIFO_STATUS errors! */
if (!int_status && !TW686X_FIFO_ERROR(fifo_status))
return IRQ_NONE;
if (int_status & INT_STATUS_DMA_TOUT) {
dev_dbg(&dev->pci_dev->dev,
"DMA timeout. Resetting DMA for all channels\n");
reset_ch = ~0;
goto reset_channels;
}
spin_lock_irqsave(&dev->lock, flags);
dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
spin_unlock_irqrestore(&dev->lock, flags);
video_en = dma_en & 0xff;
fifo_signal = ~(fifo_status & 0xff) & video_en;
fifo_ov = fifo_status >> 24;
fifo_bad = fifo_status >> 16;
/* Mask of channels with signal and FIFO errors */
fifo_errors = fifo_signal & (fifo_ov | fifo_bad);
reset_ch = 0;
pb_status = reg_read(dev, PB_STATUS);
/* Coalesce video frame/error events */
video_requests = (int_status & video_en) | fifo_errors;
audio_requests = (int_status & dma_en) >> 8;
if (video_requests)
tw686x_video_irq(dev, video_requests, pb_status,
fifo_status, &reset_ch);
if (audio_requests)
tw686x_audio_irq(dev, audio_requests, pb_status);
reset_channels:
if (reset_ch) {
spin_lock_irqsave(&dev->lock, flags);
tw686x_reset_channels(dev, reset_ch);
spin_unlock_irqrestore(&dev->lock, flags);
mod_timer(&dev->dma_delay_timer,
jiffies + msecs_to_jiffies(100));
}
return IRQ_HANDLED;
}
static void tw686x_dev_release(struct v4l2_device *v4l2_dev)
{
struct tw686x_dev *dev = container_of(v4l2_dev, struct tw686x_dev,
v4l2_dev);
unsigned int ch;
for (ch = 0; ch < max_channels(dev); ch++)
v4l2_ctrl_handler_free(&dev->video_channels[ch].ctrl_handler);
v4l2_device_unregister(&dev->v4l2_dev);
kfree(dev->audio_channels);
kfree(dev->video_channels);
kfree(dev);
}
static int tw686x_probe(struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
struct tw686x_dev *dev;
int err;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
dev->type = pci_id->driver_data;
dev->dma_mode = dma_mode;
sprintf(dev->name, "tw%04X", pci_dev->device);
dev->video_channels = kcalloc(max_channels(dev),
sizeof(*dev->video_channels), GFP_KERNEL);
if (!dev->video_channels) {
err = -ENOMEM;
goto free_dev;
}
dev->audio_channels = kcalloc(max_channels(dev),
sizeof(*dev->audio_channels), GFP_KERNEL);
if (!dev->audio_channels) {
err = -ENOMEM;
goto free_video;
}
pr_info("%s: PCI %s, IRQ %d, MMIO 0x%lx (%s mode)\n", dev->name,
pci_name(pci_dev), pci_dev->irq,
(unsigned long)pci_resource_start(pci_dev, 0),
dma_mode_name(dma_mode));
dev->pci_dev = pci_dev;
if (pci_enable_device(pci_dev)) {
err = -EIO;
goto free_audio;
}
pci_set_master(pci_dev);
err = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
if (err) {
dev_err(&pci_dev->dev, "32-bit PCI DMA not supported\n");
err = -EIO;
goto disable_pci;
}
err = pci_request_regions(pci_dev, dev->name);
if (err) {
dev_err(&pci_dev->dev, "unable to request PCI region\n");
goto disable_pci;
}
dev->mmio = pci_ioremap_bar(pci_dev, 0);
if (!dev->mmio) {
dev_err(&pci_dev->dev, "unable to remap PCI region\n");
err = -ENOMEM;
goto free_region;
}
/* Reset all subsystems */
reg_write(dev, SYS_SOFT_RST, 0x0f);
mdelay(1);
reg_write(dev, SRST[0], 0x3f);
if (max_channels(dev) > 4)
reg_write(dev, SRST[1], 0x3f);
/* Disable the DMA engine */
reg_write(dev, DMA_CMD, 0);
reg_write(dev, DMA_CHANNEL_ENABLE, 0);
/* Enable DMA FIFO overflow and pointer check */
reg_write(dev, DMA_CONFIG, 0xffffff04);
reg_write(dev, DMA_CHANNEL_TIMEOUT, 0x140c8584);
reg_write(dev, DMA_TIMER_INTERVAL, dma_interval);
spin_lock_init(&dev->lock);
timer_setup(&dev->dma_delay_timer, tw686x_dma_delay, 0);
/*
* This must be set right before initializing v4l2_dev.
* It's used to release resources after the last handle
* held is released.
*/
dev->v4l2_dev.release = tw686x_dev_release;
err = tw686x_video_init(dev);
if (err) {
dev_err(&pci_dev->dev, "can't register video\n");
goto iounmap;
}
err = tw686x_audio_init(dev);
if (err)
dev_warn(&pci_dev->dev, "can't register audio\n");
err = request_irq(pci_dev->irq, tw686x_irq, IRQF_SHARED,
dev->name, dev);
if (err < 0) {
dev_err(&pci_dev->dev, "unable to request interrupt\n");
goto tw686x_free;
}
pci_set_drvdata(pci_dev, dev);
return 0;
tw686x_free:
tw686x_video_free(dev);
tw686x_audio_free(dev);
iounmap:
pci_iounmap(pci_dev, dev->mmio);
free_region:
pci_release_regions(pci_dev);
disable_pci:
pci_disable_device(pci_dev);
free_audio:
kfree(dev->audio_channels);
free_video:
kfree(dev->video_channels);
free_dev:
kfree(dev);
return err;
}
static void tw686x_remove(struct pci_dev *pci_dev)
{
struct tw686x_dev *dev = pci_get_drvdata(pci_dev);
unsigned long flags;
/* This guarantees the IRQ handler is no longer running,
* which means we can kiss good-bye some resources.
*/
free_irq(pci_dev->irq, dev);
tw686x_video_free(dev);
tw686x_audio_free(dev);
del_timer_sync(&dev->dma_delay_timer);
pci_iounmap(pci_dev, dev->mmio);
pci_release_regions(pci_dev);
pci_disable_device(pci_dev);
/*
* Setting pci_dev to NULL allows to detect hardware is no longer
* available and will be used by vb2_ops. This is required because
* the device sometimes hot-unplugs itself as the result of a PCIe
* link down.
* The lock is really important here.
*/
spin_lock_irqsave(&dev->lock, flags);
dev->pci_dev = NULL;
spin_unlock_irqrestore(&dev->lock, flags);
/*
* This calls tw686x_dev_release if it's the last reference.
* Otherwise, release is postponed until there are no users left.
*/
v4l2_device_put(&dev->v4l2_dev);
}
/*
* On TW6864 and TW6868, all channels share the pair of video DMA SG tables,
* with 10-bit start_idx and end_idx determining start and end of frame buffer
* for particular channel.
* TW6868 with all its 8 channels would be problematic (only 127 SG entries per
* channel) but we support only 4 channels on this chip anyway (the first
* 4 channels are driven with internal video decoder, the other 4 would require
* an external TW286x part).
*
* On TW6865 and TW6869, each channel has its own DMA SG table, with indexes
* starting with 0. Both chips have complete sets of internal video decoders
* (respectively 4 or 8-channel).
*
* All chips have separate SG tables for two video frames.
*/
/* driver_data is number of A/V channels */
static const struct pci_device_id tw686x_pci_tbl[] = {
{
PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6864),
.driver_data = 4
},
{
PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6865), /* not tested */
.driver_data = 4 | TYPE_SECOND_GEN
},
/*
* TW6868 supports 8 A/V channels with an external TW2865 chip;
* not supported by the driver.
*/
{
PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6868), /* not tested */
.driver_data = 4
},
{
PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6869),
.driver_data = 8 | TYPE_SECOND_GEN},
{}
};
MODULE_DEVICE_TABLE(pci, tw686x_pci_tbl);
static struct pci_driver tw686x_pci_driver = {
.name = "tw686x",
.id_table = tw686x_pci_tbl,
.probe = tw686x_probe,
.remove = tw686x_remove,
};
module_pci_driver(tw686x_pci_driver);
MODULE_DESCRIPTION("Driver for video frame grabber cards based on Intersil/Techwell TW686[4589]");
MODULE_AUTHOR("Ezequiel Garcia <[email protected]>");
MODULE_AUTHOR("Krzysztof Ha?asa <[email protected]>");
MODULE_LICENSE("GPL v2");
| linux-master | drivers/media/pci/tw686x/tw686x-core.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
*
* Based on original driver by Krzysztof Ha?asa:
* Copyright (C) 2015 Industrial Research Institute for Automation
* and Measurements PIAP
*/
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <media/v4l2-common.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-dma-contig.h>
#include <media/videobuf2-dma-sg.h>
#include <media/videobuf2-vmalloc.h>
#include "tw686x.h"
#include "tw686x-regs.h"
#define TW686X_INPUTS_PER_CH 4
#define TW686X_VIDEO_WIDTH 720
#define TW686X_VIDEO_HEIGHT(id) ((id & V4L2_STD_525_60) ? 480 : 576)
#define TW686X_MAX_FPS(id) ((id & V4L2_STD_525_60) ? 30 : 25)
#define TW686X_MAX_SG_ENTRY_SIZE 4096
#define TW686X_MAX_SG_DESC_COUNT 256 /* PAL 720x576 needs 203 4-KB pages */
#define TW686X_SG_TABLE_SIZE (TW686X_MAX_SG_DESC_COUNT * sizeof(struct tw686x_sg_desc))
static const struct tw686x_format formats[] = {
{
.fourcc = V4L2_PIX_FMT_UYVY,
.mode = 0,
.depth = 16,
}, {
.fourcc = V4L2_PIX_FMT_RGB565,
.mode = 5,
.depth = 16,
}, {
.fourcc = V4L2_PIX_FMT_YUYV,
.mode = 6,
.depth = 16,
}
};
static void tw686x_buf_done(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
struct tw686x_dev *dev = vc->dev;
struct vb2_v4l2_buffer *vb;
struct vb2_buffer *vb2_buf;
if (vc->curr_bufs[pb]) {
vb = &vc->curr_bufs[pb]->vb;
vb->field = dev->dma_ops->field;
vb->sequence = vc->sequence++;
vb2_buf = &vb->vb2_buf;
if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY)
memcpy(vb2_plane_vaddr(vb2_buf, 0), desc->virt,
desc->size);
vb2_buf->timestamp = ktime_get_ns();
vb2_buffer_done(vb2_buf, VB2_BUF_STATE_DONE);
}
vc->pb = !pb;
}
/*
* We can call this even when alloc_dma failed for the given channel
*/
static void tw686x_memcpy_dma_free(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
struct tw686x_dev *dev = vc->dev;
struct pci_dev *pci_dev;
unsigned long flags;
/* Check device presence. Shouldn't really happen! */
spin_lock_irqsave(&dev->lock, flags);
pci_dev = dev->pci_dev;
spin_unlock_irqrestore(&dev->lock, flags);
if (!pci_dev) {
WARN(1, "trying to deallocate on missing device\n");
return;
}
if (desc->virt) {
dma_free_coherent(&dev->pci_dev->dev, desc->size, desc->virt,
desc->phys);
desc->virt = NULL;
}
}
static int tw686x_memcpy_dma_alloc(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_dev *dev = vc->dev;
u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
unsigned int len;
void *virt;
WARN(vc->dma_descs[pb].virt,
"Allocating buffer but previous still here\n");
len = (vc->width * vc->height * vc->format->depth) >> 3;
virt = dma_alloc_coherent(&dev->pci_dev->dev, len,
&vc->dma_descs[pb].phys, GFP_KERNEL);
if (!virt) {
v4l2_err(&dev->v4l2_dev,
"dma%d: unable to allocate %s-buffer\n",
vc->ch, pb ? "B" : "P");
return -ENOMEM;
}
vc->dma_descs[pb].size = len;
vc->dma_descs[pb].virt = virt;
reg_write(dev, reg, vc->dma_descs[pb].phys);
return 0;
}
static void tw686x_memcpy_buf_refill(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_v4l2_buf *buf;
while (!list_empty(&vc->vidq_queued)) {
buf = list_first_entry(&vc->vidq_queued,
struct tw686x_v4l2_buf, list);
list_del(&buf->list);
vc->curr_bufs[pb] = buf;
return;
}
vc->curr_bufs[pb] = NULL;
}
static const struct tw686x_dma_ops memcpy_dma_ops = {
.alloc = tw686x_memcpy_dma_alloc,
.free = tw686x_memcpy_dma_free,
.buf_refill = tw686x_memcpy_buf_refill,
.mem_ops = &vb2_vmalloc_memops,
.hw_dma_mode = TW686X_FRAME_MODE,
.field = V4L2_FIELD_INTERLACED,
};
static void tw686x_contig_buf_refill(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_v4l2_buf *buf;
while (!list_empty(&vc->vidq_queued)) {
u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
dma_addr_t phys;
buf = list_first_entry(&vc->vidq_queued,
struct tw686x_v4l2_buf, list);
list_del(&buf->list);
phys = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
reg_write(vc->dev, reg, phys);
buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
vc->curr_bufs[pb] = buf;
return;
}
vc->curr_bufs[pb] = NULL;
}
static const struct tw686x_dma_ops contig_dma_ops = {
.buf_refill = tw686x_contig_buf_refill,
.mem_ops = &vb2_dma_contig_memops,
.hw_dma_mode = TW686X_FRAME_MODE,
.field = V4L2_FIELD_INTERLACED,
};
static int tw686x_sg_desc_fill(struct tw686x_sg_desc *descs,
struct tw686x_v4l2_buf *buf,
unsigned int buf_len)
{
struct sg_table *vbuf = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
unsigned int len, entry_len;
struct scatterlist *sg;
int i, count;
/* Clear the scatter-gather table */
memset(descs, 0, TW686X_SG_TABLE_SIZE);
count = 0;
for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
dma_addr_t phys = sg_dma_address(sg);
len = sg_dma_len(sg);
while (len && buf_len) {
if (count == TW686X_MAX_SG_DESC_COUNT)
return -ENOMEM;
entry_len = min_t(unsigned int, len,
TW686X_MAX_SG_ENTRY_SIZE);
entry_len = min_t(unsigned int, entry_len, buf_len);
descs[count].phys = cpu_to_le32(phys);
descs[count++].flags_length =
cpu_to_le32(BIT(30) | entry_len);
phys += entry_len;
len -= entry_len;
buf_len -= entry_len;
}
if (!buf_len)
return 0;
}
return -ENOMEM;
}
static void tw686x_sg_buf_refill(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_dev *dev = vc->dev;
struct tw686x_v4l2_buf *buf;
while (!list_empty(&vc->vidq_queued)) {
unsigned int buf_len;
buf = list_first_entry(&vc->vidq_queued,
struct tw686x_v4l2_buf, list);
list_del(&buf->list);
buf_len = (vc->width * vc->height * vc->format->depth) >> 3;
if (tw686x_sg_desc_fill(vc->sg_descs[pb], buf, buf_len)) {
v4l2_err(&dev->v4l2_dev,
"dma%d: unable to fill %s-buffer\n",
vc->ch, pb ? "B" : "P");
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
continue;
}
buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
vc->curr_bufs[pb] = buf;
return;
}
vc->curr_bufs[pb] = NULL;
}
static void tw686x_sg_dma_free(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
struct tw686x_dev *dev = vc->dev;
if (desc->size) {
dma_free_coherent(&dev->pci_dev->dev, desc->size, desc->virt,
desc->phys);
desc->virt = NULL;
}
vc->sg_descs[pb] = NULL;
}
static int tw686x_sg_dma_alloc(struct tw686x_video_channel *vc,
unsigned int pb)
{
struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
struct tw686x_dev *dev = vc->dev;
u32 reg = pb ? DMA_PAGE_TABLE1_ADDR[vc->ch] :
DMA_PAGE_TABLE0_ADDR[vc->ch];
void *virt;
if (desc->size) {
virt = dma_alloc_coherent(&dev->pci_dev->dev, desc->size,
&desc->phys, GFP_KERNEL);
if (!virt) {
v4l2_err(&dev->v4l2_dev,
"dma%d: unable to allocate %s-buffer\n",
vc->ch, pb ? "B" : "P");
return -ENOMEM;
}
desc->virt = virt;
reg_write(dev, reg, desc->phys);
} else {
virt = dev->video_channels[0].dma_descs[pb].virt +
vc->ch * TW686X_SG_TABLE_SIZE;
}
vc->sg_descs[pb] = virt;
return 0;
}
static int tw686x_sg_setup(struct tw686x_dev *dev)
{
unsigned int sg_table_size, pb, ch, channels;
if (is_second_gen(dev)) {
/*
* TW6865/TW6869: each channel needs a pair of
* P-B descriptor tables.
*/
channels = max_channels(dev);
sg_table_size = TW686X_SG_TABLE_SIZE;
} else {
/*
* TW6864/TW6868: we need to allocate a pair of
* P-B descriptor tables, common for all channels.
* Each table will be bigger than 4 KB.
*/
channels = 1;
sg_table_size = max_channels(dev) * TW686X_SG_TABLE_SIZE;
}
for (ch = 0; ch < channels; ch++) {
struct tw686x_video_channel *vc = &dev->video_channels[ch];
for (pb = 0; pb < 2; pb++)
vc->dma_descs[pb].size = sg_table_size;
}
return 0;
}
static const struct tw686x_dma_ops sg_dma_ops = {
.setup = tw686x_sg_setup,
.alloc = tw686x_sg_dma_alloc,
.free = tw686x_sg_dma_free,
.buf_refill = tw686x_sg_buf_refill,
.mem_ops = &vb2_dma_sg_memops,
.hw_dma_mode = TW686X_SG_MODE,
.field = V4L2_FIELD_SEQ_TB,
};
static const unsigned int fps_map[15] = {
/*
* bit 31 enables selecting the field control register
* bits 0-29 are a bitmask with fields that will be output.
* For NTSC (and PAL-M, PAL-60), all 30 bits are used.
* For other PAL standards, only the first 25 bits are used.
*/
0x00000000, /* output all fields */
0x80000006, /* 2 fps (60Hz), 2 fps (50Hz) */
0x80018006, /* 4 fps (60Hz), 4 fps (50Hz) */
0x80618006, /* 6 fps (60Hz), 6 fps (50Hz) */
0x81818186, /* 8 fps (60Hz), 8 fps (50Hz) */
0x86186186, /* 10 fps (60Hz), 8 fps (50Hz) */
0x86619866, /* 12 fps (60Hz), 10 fps (50Hz) */
0x86666666, /* 14 fps (60Hz), 12 fps (50Hz) */
0x9999999e, /* 16 fps (60Hz), 14 fps (50Hz) */
0x99e6799e, /* 18 fps (60Hz), 16 fps (50Hz) */
0x9e79e79e, /* 20 fps (60Hz), 16 fps (50Hz) */
0x9e7e7e7e, /* 22 fps (60Hz), 18 fps (50Hz) */
0x9fe7f9fe, /* 24 fps (60Hz), 20 fps (50Hz) */
0x9ffe7ffe, /* 26 fps (60Hz), 22 fps (50Hz) */
0x9ffffffe, /* 28 fps (60Hz), 24 fps (50Hz) */
};
static unsigned int tw686x_real_fps(unsigned int index, unsigned int max_fps)
{
unsigned long mask;
if (!index || index >= ARRAY_SIZE(fps_map))
return max_fps;
mask = GENMASK(max_fps - 1, 0);
return hweight_long(fps_map[index] & mask);
}
static unsigned int tw686x_fps_idx(unsigned int fps, unsigned int max_fps)
{
unsigned int idx, real_fps;
int delta;
/* First guess */
idx = (12 + 15 * fps) / max_fps;
/* Minimal possible framerate is 2 frames per second */
if (!idx)
return 1;
/* Check if the difference is bigger than abs(1) and adjust */
real_fps = tw686x_real_fps(idx, max_fps);
delta = real_fps - fps;
if (delta < -1)
idx++;
else if (delta > 1)
idx--;
/* Max framerate */
if (idx >= 15)
return 0;
return idx;
}
static void tw686x_set_framerate(struct tw686x_video_channel *vc,
unsigned int fps)
{
unsigned int i;
i = tw686x_fps_idx(fps, TW686X_MAX_FPS(vc->video_standard));
reg_write(vc->dev, VIDEO_FIELD_CTRL[vc->ch], fps_map[i]);
vc->fps = tw686x_real_fps(i, TW686X_MAX_FPS(vc->video_standard));
}
static const struct tw686x_format *format_by_fourcc(unsigned int fourcc)
{
unsigned int cnt;
for (cnt = 0; cnt < ARRAY_SIZE(formats); cnt++)
if (formats[cnt].fourcc == fourcc)
return &formats[cnt];
return NULL;
}
static int tw686x_queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers, unsigned int *nplanes,
unsigned int sizes[], struct device *alloc_devs[])
{
struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
unsigned int szimage =
(vc->width * vc->height * vc->format->depth) >> 3;
/*
* Let's request at least three buffers: two for the
* DMA engine and one for userspace.
*/
if (vq->num_buffers + *nbuffers < 3)
*nbuffers = 3 - vq->num_buffers;
if (*nplanes) {
if (*nplanes != 1 || sizes[0] < szimage)
return -EINVAL;
return 0;
}
sizes[0] = szimage;
*nplanes = 1;
return 0;
}
static void tw686x_buf_queue(struct vb2_buffer *vb)
{
struct tw686x_video_channel *vc = vb2_get_drv_priv(vb->vb2_queue);
struct tw686x_dev *dev = vc->dev;
struct pci_dev *pci_dev;
unsigned long flags;
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct tw686x_v4l2_buf *buf =
container_of(vbuf, struct tw686x_v4l2_buf, vb);
/* Check device presence */
spin_lock_irqsave(&dev->lock, flags);
pci_dev = dev->pci_dev;
spin_unlock_irqrestore(&dev->lock, flags);
if (!pci_dev) {
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
return;
}
spin_lock_irqsave(&vc->qlock, flags);
list_add_tail(&buf->list, &vc->vidq_queued);
spin_unlock_irqrestore(&vc->qlock, flags);
}
static void tw686x_clear_queue(struct tw686x_video_channel *vc,
enum vb2_buffer_state state)
{
unsigned int pb;
while (!list_empty(&vc->vidq_queued)) {
struct tw686x_v4l2_buf *buf;
buf = list_first_entry(&vc->vidq_queued,
struct tw686x_v4l2_buf, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, state);
}
for (pb = 0; pb < 2; pb++) {
if (vc->curr_bufs[pb])
vb2_buffer_done(&vc->curr_bufs[pb]->vb.vb2_buf, state);
vc->curr_bufs[pb] = NULL;
}
}
static int tw686x_start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
struct tw686x_dev *dev = vc->dev;
struct pci_dev *pci_dev;
unsigned long flags;
int pb, err;
/* Check device presence */
spin_lock_irqsave(&dev->lock, flags);
pci_dev = dev->pci_dev;
spin_unlock_irqrestore(&dev->lock, flags);
if (!pci_dev) {
err = -ENODEV;
goto err_clear_queue;
}
spin_lock_irqsave(&vc->qlock, flags);
/* Sanity check */
if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY &&
(!vc->dma_descs[0].virt || !vc->dma_descs[1].virt)) {
spin_unlock_irqrestore(&vc->qlock, flags);
v4l2_err(&dev->v4l2_dev,
"video%d: refusing to start without DMA buffers\n",
vc->num);
err = -ENOMEM;
goto err_clear_queue;
}
for (pb = 0; pb < 2; pb++)
dev->dma_ops->buf_refill(vc, pb);
spin_unlock_irqrestore(&vc->qlock, flags);
vc->sequence = 0;
vc->pb = 0;
spin_lock_irqsave(&dev->lock, flags);
tw686x_enable_channel(dev, vc->ch);
spin_unlock_irqrestore(&dev->lock, flags);
mod_timer(&dev->dma_delay_timer, jiffies + msecs_to_jiffies(100));
return 0;
err_clear_queue:
spin_lock_irqsave(&vc->qlock, flags);
tw686x_clear_queue(vc, VB2_BUF_STATE_QUEUED);
spin_unlock_irqrestore(&vc->qlock, flags);
return err;
}
static void tw686x_stop_streaming(struct vb2_queue *vq)
{
struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
struct tw686x_dev *dev = vc->dev;
struct pci_dev *pci_dev;
unsigned long flags;
/* Check device presence */
spin_lock_irqsave(&dev->lock, flags);
pci_dev = dev->pci_dev;
spin_unlock_irqrestore(&dev->lock, flags);
if (pci_dev)
tw686x_disable_channel(dev, vc->ch);
spin_lock_irqsave(&vc->qlock, flags);
tw686x_clear_queue(vc, VB2_BUF_STATE_ERROR);
spin_unlock_irqrestore(&vc->qlock, flags);
}
static int tw686x_buf_prepare(struct vb2_buffer *vb)
{
struct tw686x_video_channel *vc = vb2_get_drv_priv(vb->vb2_queue);
unsigned int size =
(vc->width * vc->height * vc->format->depth) >> 3;
if (vb2_plane_size(vb, 0) < size)
return -EINVAL;
vb2_set_plane_payload(vb, 0, size);
return 0;
}
static const struct vb2_ops tw686x_video_qops = {
.queue_setup = tw686x_queue_setup,
.buf_queue = tw686x_buf_queue,
.buf_prepare = tw686x_buf_prepare,
.start_streaming = tw686x_start_streaming,
.stop_streaming = tw686x_stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
static int tw686x_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct tw686x_video_channel *vc;
struct tw686x_dev *dev;
unsigned int ch;
vc = container_of(ctrl->handler, struct tw686x_video_channel,
ctrl_handler);
dev = vc->dev;
ch = vc->ch;
switch (ctrl->id) {
case V4L2_CID_BRIGHTNESS:
reg_write(dev, BRIGHT[ch], ctrl->val & 0xff);
return 0;
case V4L2_CID_CONTRAST:
reg_write(dev, CONTRAST[ch], ctrl->val);
return 0;
case V4L2_CID_SATURATION:
reg_write(dev, SAT_U[ch], ctrl->val);
reg_write(dev, SAT_V[ch], ctrl->val);
return 0;
case V4L2_CID_HUE:
reg_write(dev, HUE[ch], ctrl->val & 0xff);
return 0;
}
return -EINVAL;
}
static const struct v4l2_ctrl_ops ctrl_ops = {
.s_ctrl = tw686x_s_ctrl,
};
static int tw686x_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct tw686x_dev *dev = vc->dev;
f->fmt.pix.width = vc->width;
f->fmt.pix.height = vc->height;
f->fmt.pix.field = dev->dma_ops->field;
f->fmt.pix.pixelformat = vc->format->fourcc;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
f->fmt.pix.bytesperline = (f->fmt.pix.width * vc->format->depth) / 8;
f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
return 0;
}
static int tw686x_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct tw686x_dev *dev = vc->dev;
unsigned int video_height = TW686X_VIDEO_HEIGHT(vc->video_standard);
const struct tw686x_format *format;
format = format_by_fourcc(f->fmt.pix.pixelformat);
if (!format) {
format = &formats[0];
f->fmt.pix.pixelformat = format->fourcc;
}
if (f->fmt.pix.width <= TW686X_VIDEO_WIDTH / 2)
f->fmt.pix.width = TW686X_VIDEO_WIDTH / 2;
else
f->fmt.pix.width = TW686X_VIDEO_WIDTH;
if (f->fmt.pix.height <= video_height / 2)
f->fmt.pix.height = video_height / 2;
else
f->fmt.pix.height = video_height;
f->fmt.pix.bytesperline = (f->fmt.pix.width * format->depth) / 8;
f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
f->fmt.pix.field = dev->dma_ops->field;
return 0;
}
static int tw686x_set_format(struct tw686x_video_channel *vc,
unsigned int pixelformat, unsigned int width,
unsigned int height, bool realloc)
{
struct tw686x_dev *dev = vc->dev;
u32 val, dma_width, dma_height, dma_line_width;
int err, pb;
vc->format = format_by_fourcc(pixelformat);
vc->width = width;
vc->height = height;
/* We need new DMA buffers if the framesize has changed */
if (dev->dma_ops->alloc && realloc) {
for (pb = 0; pb < 2; pb++)
dev->dma_ops->free(vc, pb);
for (pb = 0; pb < 2; pb++) {
err = dev->dma_ops->alloc(vc, pb);
if (err) {
if (pb > 0)
dev->dma_ops->free(vc, 0);
return err;
}
}
}
val = reg_read(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch]);
if (vc->width <= TW686X_VIDEO_WIDTH / 2)
val |= BIT(23);
else
val &= ~BIT(23);
if (vc->height <= TW686X_VIDEO_HEIGHT(vc->video_standard) / 2)
val |= BIT(24);
else
val &= ~BIT(24);
val &= ~0x7ffff;
/* Program the DMA scatter-gather */
if (dev->dma_mode == TW686X_DMA_MODE_SG) {
u32 start_idx, end_idx;
start_idx = is_second_gen(dev) ?
0 : vc->ch * TW686X_MAX_SG_DESC_COUNT;
end_idx = start_idx + TW686X_MAX_SG_DESC_COUNT - 1;
val |= (end_idx << 10) | start_idx;
}
val &= ~(0x7 << 20);
val |= vc->format->mode << 20;
reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], val);
/* Program the DMA frame size */
dma_width = (vc->width * 2) & 0x7ff;
dma_height = vc->height / 2;
dma_line_width = (vc->width * 2) & 0x7ff;
val = (dma_height << 22) | (dma_line_width << 11) | dma_width;
reg_write(vc->dev, VDMA_WHP[vc->ch], val);
return 0;
}
static int tw686x_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct tw686x_video_channel *vc = video_drvdata(file);
unsigned long area;
bool realloc;
int err;
if (vb2_is_busy(&vc->vidq))
return -EBUSY;
area = vc->width * vc->height;
err = tw686x_try_fmt_vid_cap(file, priv, f);
if (err)
return err;
realloc = area != (f->fmt.pix.width * f->fmt.pix.height);
return tw686x_set_format(vc, f->fmt.pix.pixelformat,
f->fmt.pix.width, f->fmt.pix.height,
realloc);
}
static int tw686x_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct tw686x_dev *dev = vc->dev;
strscpy(cap->driver, "tw686x", sizeof(cap->driver));
strscpy(cap->card, dev->name, sizeof(cap->card));
return 0;
}
static int tw686x_set_standard(struct tw686x_video_channel *vc, v4l2_std_id id)
{
u32 val;
if (id & V4L2_STD_NTSC)
val = 0;
else if (id & V4L2_STD_PAL)
val = 1;
else if (id & V4L2_STD_SECAM)
val = 2;
else if (id & V4L2_STD_NTSC_443)
val = 3;
else if (id & V4L2_STD_PAL_M)
val = 4;
else if (id & V4L2_STD_PAL_Nc)
val = 5;
else if (id & V4L2_STD_PAL_60)
val = 6;
else
return -EINVAL;
vc->video_standard = id;
reg_write(vc->dev, SDT[vc->ch], val);
val = reg_read(vc->dev, VIDEO_CONTROL1);
if (id & V4L2_STD_525_60)
val &= ~(1 << (SYS_MODE_DMA_SHIFT + vc->ch));
else
val |= (1 << (SYS_MODE_DMA_SHIFT + vc->ch));
reg_write(vc->dev, VIDEO_CONTROL1, val);
return 0;
}
static int tw686x_s_std(struct file *file, void *priv, v4l2_std_id id)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct v4l2_format f;
int ret;
if (vc->video_standard == id)
return 0;
if (vb2_is_busy(&vc->vidq))
return -EBUSY;
ret = tw686x_set_standard(vc, id);
if (ret)
return ret;
/*
* Adjust format after V4L2_STD_525_60/V4L2_STD_625_50 change,
* calling g_fmt and s_fmt will sanitize the height
* according to the standard.
*/
tw686x_g_fmt_vid_cap(file, priv, &f);
tw686x_s_fmt_vid_cap(file, priv, &f);
/*
* Frame decimation depends on the chosen standard,
* so reset it to the current value.
*/
tw686x_set_framerate(vc, vc->fps);
return 0;
}
static int tw686x_querystd(struct file *file, void *priv, v4l2_std_id *std)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct tw686x_dev *dev = vc->dev;
unsigned int old_std, detected_std = 0;
unsigned long end;
if (vb2_is_streaming(&vc->vidq))
return -EBUSY;
/* Enable and start standard detection */
old_std = reg_read(dev, SDT[vc->ch]);
reg_write(dev, SDT[vc->ch], 0x7);
reg_write(dev, SDT_EN[vc->ch], 0xff);
end = jiffies + msecs_to_jiffies(500);
while (time_is_after_jiffies(end)) {
detected_std = reg_read(dev, SDT[vc->ch]);
if (!(detected_std & BIT(7)))
break;
msleep(100);
}
reg_write(dev, SDT[vc->ch], old_std);
/* Exit if still busy */
if (detected_std & BIT(7))
return 0;
detected_std = (detected_std >> 4) & 0x7;
switch (detected_std) {
case TW686X_STD_NTSC_M:
*std &= V4L2_STD_NTSC;
break;
case TW686X_STD_NTSC_443:
*std &= V4L2_STD_NTSC_443;
break;
case TW686X_STD_PAL_M:
*std &= V4L2_STD_PAL_M;
break;
case TW686X_STD_PAL_60:
*std &= V4L2_STD_PAL_60;
break;
case TW686X_STD_PAL:
*std &= V4L2_STD_PAL;
break;
case TW686X_STD_PAL_CN:
*std &= V4L2_STD_PAL_Nc;
break;
case TW686X_STD_SECAM:
*std &= V4L2_STD_SECAM;
break;
default:
*std = 0;
}
return 0;
}
static int tw686x_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
struct tw686x_video_channel *vc = video_drvdata(file);
*id = vc->video_standard;
return 0;
}
static int tw686x_enum_framesizes(struct file *file, void *priv,
struct v4l2_frmsizeenum *fsize)
{
struct tw686x_video_channel *vc = video_drvdata(file);
if (fsize->index)
return -EINVAL;
fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
fsize->stepwise.max_width = TW686X_VIDEO_WIDTH;
fsize->stepwise.min_width = fsize->stepwise.max_width / 2;
fsize->stepwise.step_width = fsize->stepwise.min_width;
fsize->stepwise.max_height = TW686X_VIDEO_HEIGHT(vc->video_standard);
fsize->stepwise.min_height = fsize->stepwise.max_height / 2;
fsize->stepwise.step_height = fsize->stepwise.min_height;
return 0;
}
static int tw686x_enum_frameintervals(struct file *file, void *priv,
struct v4l2_frmivalenum *ival)
{
struct tw686x_video_channel *vc = video_drvdata(file);
int max_fps = TW686X_MAX_FPS(vc->video_standard);
int max_rates = DIV_ROUND_UP(max_fps, 2);
if (ival->index >= max_rates)
return -EINVAL;
ival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
ival->discrete.numerator = 1;
if (ival->index < (max_rates - 1))
ival->discrete.denominator = (ival->index + 1) * 2;
else
ival->discrete.denominator = max_fps;
return 0;
}
static int tw686x_g_parm(struct file *file, void *priv,
struct v4l2_streamparm *sp)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct v4l2_captureparm *cp = &sp->parm.capture;
if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
sp->parm.capture.readbuffers = 3;
cp->capability = V4L2_CAP_TIMEPERFRAME;
cp->timeperframe.numerator = 1;
cp->timeperframe.denominator = vc->fps;
return 0;
}
static int tw686x_s_parm(struct file *file, void *priv,
struct v4l2_streamparm *sp)
{
struct tw686x_video_channel *vc = video_drvdata(file);
struct v4l2_captureparm *cp = &sp->parm.capture;
unsigned int denominator = cp->timeperframe.denominator;
unsigned int numerator = cp->timeperframe.numerator;
unsigned int fps;
if (vb2_is_busy(&vc->vidq))
return -EBUSY;
fps = (!numerator || !denominator) ? 0 : denominator / numerator;
if (vc->fps != fps)
tw686x_set_framerate(vc, fps);
return tw686x_g_parm(file, priv, sp);
}
static int tw686x_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index >= ARRAY_SIZE(formats))
return -EINVAL;
f->pixelformat = formats[f->index].fourcc;
return 0;
}
static void tw686x_set_input(struct tw686x_video_channel *vc, unsigned int i)
{
u32 val;
vc->input = i;
val = reg_read(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch]);
val &= ~(0x3 << 30);
val |= i << 30;
reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], val);
}
static int tw686x_s_input(struct file *file, void *priv, unsigned int i)
{
struct tw686x_video_channel *vc = video_drvdata(file);
if (i >= TW686X_INPUTS_PER_CH)
return -EINVAL;
if (i == vc->input)
return 0;
/*
* Not sure we are able to support on the fly input change
*/
if (vb2_is_busy(&vc->vidq))
return -EBUSY;
tw686x_set_input(vc, i);
return 0;
}
static int tw686x_g_input(struct file *file, void *priv, unsigned int *i)
{
struct tw686x_video_channel *vc = video_drvdata(file);
*i = vc->input;
return 0;
}
static int tw686x_enum_input(struct file *file, void *priv,
struct v4l2_input *i)
{
struct tw686x_video_channel *vc = video_drvdata(file);
unsigned int vidstat;
if (i->index >= TW686X_INPUTS_PER_CH)
return -EINVAL;
snprintf(i->name, sizeof(i->name), "Composite%d", i->index);
i->type = V4L2_INPUT_TYPE_CAMERA;
i->std = vc->device->tvnorms;
i->capabilities = V4L2_IN_CAP_STD;
vidstat = reg_read(vc->dev, VIDSTAT[vc->ch]);
i->status = 0;
if (vidstat & TW686X_VIDSTAT_VDLOSS)
i->status |= V4L2_IN_ST_NO_SIGNAL;
if (!(vidstat & TW686X_VIDSTAT_HLOCK))
i->status |= V4L2_IN_ST_NO_H_LOCK;
return 0;
}
static const struct v4l2_file_operations tw686x_video_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.unlocked_ioctl = video_ioctl2,
.release = vb2_fop_release,
.poll = vb2_fop_poll,
.read = vb2_fop_read,
.mmap = vb2_fop_mmap,
};
static const struct v4l2_ioctl_ops tw686x_video_ioctl_ops = {
.vidioc_querycap = tw686x_querycap,
.vidioc_g_fmt_vid_cap = tw686x_g_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = tw686x_s_fmt_vid_cap,
.vidioc_enum_fmt_vid_cap = tw686x_enum_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = tw686x_try_fmt_vid_cap,
.vidioc_querystd = tw686x_querystd,
.vidioc_g_std = tw686x_g_std,
.vidioc_s_std = tw686x_s_std,
.vidioc_g_parm = tw686x_g_parm,
.vidioc_s_parm = tw686x_s_parm,
.vidioc_enum_framesizes = tw686x_enum_framesizes,
.vidioc_enum_frameintervals = tw686x_enum_frameintervals,
.vidioc_enum_input = tw686x_enum_input,
.vidioc_g_input = tw686x_g_input,
.vidioc_s_input = tw686x_s_input,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests,
unsigned int pb_status, unsigned int fifo_status,
unsigned int *reset_ch)
{
struct tw686x_video_channel *vc;
unsigned long flags;
unsigned int ch, pb;
for_each_set_bit(ch, &requests, max_channels(dev)) {
vc = &dev->video_channels[ch];
/*
* This can either be a blue frame (with signal-lost bit set)
* or a good frame (with signal-lost bit clear). If we have just
* got signal, then this channel needs resetting.
*/
if (vc->no_signal && !(fifo_status & BIT(ch))) {
v4l2_printk(KERN_DEBUG, &dev->v4l2_dev,
"video%d: signal recovered\n", vc->num);
vc->no_signal = false;
*reset_ch |= BIT(ch);
vc->pb = 0;
continue;
}
vc->no_signal = !!(fifo_status & BIT(ch));
/* Check FIFO errors only if there's signal */
if (!vc->no_signal) {
u32 fifo_ov, fifo_bad;
fifo_ov = (fifo_status >> 24) & BIT(ch);
fifo_bad = (fifo_status >> 16) & BIT(ch);
if (fifo_ov || fifo_bad) {
/* Mark this channel for reset */
v4l2_printk(KERN_DEBUG, &dev->v4l2_dev,
"video%d: FIFO error\n", vc->num);
*reset_ch |= BIT(ch);
vc->pb = 0;
continue;
}
}
pb = !!(pb_status & BIT(ch));
if (vc->pb != pb) {
/* Mark this channel for reset */
v4l2_printk(KERN_DEBUG, &dev->v4l2_dev,
"video%d: unexpected p-b buffer!\n",
vc->num);
*reset_ch |= BIT(ch);
vc->pb = 0;
continue;
}
spin_lock_irqsave(&vc->qlock, flags);
tw686x_buf_done(vc, pb);
dev->dma_ops->buf_refill(vc, pb);
spin_unlock_irqrestore(&vc->qlock, flags);
}
}
void tw686x_video_free(struct tw686x_dev *dev)
{
unsigned int ch, pb;
for (ch = 0; ch < max_channels(dev); ch++) {
struct tw686x_video_channel *vc = &dev->video_channels[ch];
video_unregister_device(vc->device);
if (dev->dma_ops->free)
for (pb = 0; pb < 2; pb++)
dev->dma_ops->free(vc, pb);
}
}
int tw686x_video_init(struct tw686x_dev *dev)
{
unsigned int ch, val;
int err;
if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY)
dev->dma_ops = &memcpy_dma_ops;
else if (dev->dma_mode == TW686X_DMA_MODE_CONTIG)
dev->dma_ops = &contig_dma_ops;
else if (dev->dma_mode == TW686X_DMA_MODE_SG)
dev->dma_ops = &sg_dma_ops;
else
return -EINVAL;
err = v4l2_device_register(&dev->pci_dev->dev, &dev->v4l2_dev);
if (err)
return err;
if (dev->dma_ops->setup) {
err = dev->dma_ops->setup(dev);
if (err)
return err;
}
/* Initialize vc->dev and vc->ch for the error path */
for (ch = 0; ch < max_channels(dev); ch++) {
struct tw686x_video_channel *vc = &dev->video_channels[ch];
vc->dev = dev;
vc->ch = ch;
}
for (ch = 0; ch < max_channels(dev); ch++) {
struct tw686x_video_channel *vc = &dev->video_channels[ch];
struct video_device *vdev;
mutex_init(&vc->vb_mutex);
spin_lock_init(&vc->qlock);
INIT_LIST_HEAD(&vc->vidq_queued);
/* default settings */
err = tw686x_set_standard(vc, V4L2_STD_NTSC);
if (err)
goto error;
err = tw686x_set_format(vc, formats[0].fourcc,
TW686X_VIDEO_WIDTH,
TW686X_VIDEO_HEIGHT(vc->video_standard),
true);
if (err)
goto error;
tw686x_set_input(vc, 0);
tw686x_set_framerate(vc, 30);
reg_write(dev, VDELAY_LO[ch], 0x14);
reg_write(dev, HACTIVE_LO[ch], 0xd0);
reg_write(dev, VIDEO_SIZE[ch], 0);
vc->vidq.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
vc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vc->vidq.drv_priv = vc;
vc->vidq.buf_struct_size = sizeof(struct tw686x_v4l2_buf);
vc->vidq.ops = &tw686x_video_qops;
vc->vidq.mem_ops = dev->dma_ops->mem_ops;
vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
vc->vidq.min_buffers_needed = 2;
vc->vidq.lock = &vc->vb_mutex;
vc->vidq.gfp_flags = dev->dma_mode != TW686X_DMA_MODE_MEMCPY ?
GFP_DMA32 : 0;
vc->vidq.dev = &dev->pci_dev->dev;
err = vb2_queue_init(&vc->vidq);
if (err) {
v4l2_err(&dev->v4l2_dev,
"dma%d: cannot init vb2 queue\n", ch);
goto error;
}
err = v4l2_ctrl_handler_init(&vc->ctrl_handler, 4);
if (err) {
v4l2_err(&dev->v4l2_dev,
"dma%d: cannot init ctrl handler\n", ch);
goto error;
}
v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
V4L2_CID_CONTRAST, 0, 255, 1, 100);
v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
V4L2_CID_SATURATION, 0, 255, 1, 128);
v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
V4L2_CID_HUE, -128, 127, 1, 0);
err = vc->ctrl_handler.error;
if (err)
goto error;
err = v4l2_ctrl_handler_setup(&vc->ctrl_handler);
if (err)
goto error;
vdev = video_device_alloc();
if (!vdev) {
v4l2_err(&dev->v4l2_dev,
"dma%d: unable to allocate device\n", ch);
err = -ENOMEM;
goto error;
}
snprintf(vdev->name, sizeof(vdev->name), "%s video", dev->name);
vdev->fops = &tw686x_video_fops;
vdev->ioctl_ops = &tw686x_video_ioctl_ops;
vdev->release = video_device_release;
vdev->v4l2_dev = &dev->v4l2_dev;
vdev->queue = &vc->vidq;
vdev->tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50;
vdev->minor = -1;
vdev->lock = &vc->vb_mutex;
vdev->ctrl_handler = &vc->ctrl_handler;
vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
vc->device = vdev;
video_set_drvdata(vdev, vc);
err = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
if (err < 0) {
video_device_release(vdev);
goto error;
}
vc->num = vdev->num;
}
val = TW686X_DEF_PHASE_REF;
for (ch = 0; ch < max_channels(dev); ch++)
val |= dev->dma_ops->hw_dma_mode << (16 + ch * 2);
reg_write(dev, PHASE_REF, val);
reg_write(dev, MISC2[0], 0xe7);
reg_write(dev, VCTRL1[0], 0xcc);
reg_write(dev, LOOP[0], 0xa5);
if (max_channels(dev) > 4) {
reg_write(dev, VCTRL1[1], 0xcc);
reg_write(dev, LOOP[1], 0xa5);
reg_write(dev, MISC2[1], 0xe7);
}
return 0;
error:
tw686x_video_free(dev);
return err;
}
| linux-master | drivers/media/pci/tw686x/tw686x-video.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* netup_unidvb_core.c
*
* Main module for NetUP Universal Dual DVB-CI
*
* Copyright (C) 2014 NetUP Inc.
* Copyright (C) 2014 Sergey Kozlov <[email protected]>
* Copyright (C) 2014 Abylay Ospan <[email protected]>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kmod.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-vmalloc.h>
#include "netup_unidvb.h"
#include "cxd2841er.h"
#include "horus3a.h"
#include "ascot2e.h"
#include "helene.h"
#include "lnbh25.h"
static int spi_enable;
module_param(spi_enable, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_DESCRIPTION("Driver for NetUP Dual Universal DVB CI PCIe card");
MODULE_AUTHOR("[email protected]");
MODULE_VERSION(NETUP_UNIDVB_VERSION);
MODULE_LICENSE("GPL");
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
/* Avalon-MM PCI-E registers */
#define AVL_PCIE_IENR 0x50
#define AVL_PCIE_ISR 0x40
#define AVL_IRQ_ENABLE 0x80
#define AVL_IRQ_ASSERTED 0x80
/* GPIO registers */
#define GPIO_REG_IO 0x4880
#define GPIO_REG_IO_TOGGLE 0x4882
#define GPIO_REG_IO_SET 0x4884
#define GPIO_REG_IO_CLEAR 0x4886
/* GPIO bits */
#define GPIO_FEA_RESET (1 << 0)
#define GPIO_FEB_RESET (1 << 1)
#define GPIO_RFA_CTL (1 << 2)
#define GPIO_RFB_CTL (1 << 3)
#define GPIO_FEA_TU_RESET (1 << 4)
#define GPIO_FEB_TU_RESET (1 << 5)
/* DMA base address */
#define NETUP_DMA0_ADDR 0x4900
#define NETUP_DMA1_ADDR 0x4940
/* 8 DMA blocks * 128 packets * 188 bytes*/
#define NETUP_DMA_BLOCKS_COUNT 8
#define NETUP_DMA_PACKETS_COUNT 128
/* DMA status bits */
#define BIT_DMA_RUN 1
#define BIT_DMA_ERROR 2
#define BIT_DMA_IRQ 0x200
/**
* struct netup_dma_regs - the map of DMA module registers
* @ctrlstat_set: Control register, write to set control bits
* @ctrlstat_clear: Control register, write to clear control bits
* @start_addr_lo: DMA ring buffer start address, lower part
* @start_addr_hi: DMA ring buffer start address, higher part
* @size: DMA ring buffer size register
* * Bits [0-7]: DMA packet size, 188 bytes
* * Bits [16-23]: packets count in block, 128 packets
* * Bits [24-31]: blocks count, 8 blocks
* @timeout: DMA timeout in units of 8ns
* For example, value of 375000000 equals to 3 sec
* @curr_addr_lo: Current ring buffer head address, lower part
* @curr_addr_hi: Current ring buffer head address, higher part
* @stat_pkt_received: Statistic register, not tested
* @stat_pkt_accepted: Statistic register, not tested
* @stat_pkt_overruns: Statistic register, not tested
* @stat_pkt_underruns: Statistic register, not tested
* @stat_fifo_overruns: Statistic register, not tested
*/
struct netup_dma_regs {
__le32 ctrlstat_set;
__le32 ctrlstat_clear;
__le32 start_addr_lo;
__le32 start_addr_hi;
__le32 size;
__le32 timeout;
__le32 curr_addr_lo;
__le32 curr_addr_hi;
__le32 stat_pkt_received;
__le32 stat_pkt_accepted;
__le32 stat_pkt_overruns;
__le32 stat_pkt_underruns;
__le32 stat_fifo_overruns;
} __packed __aligned(1);
struct netup_unidvb_buffer {
struct vb2_v4l2_buffer vb;
struct list_head list;
u32 size;
};
static int netup_unidvb_tuner_ctrl(void *priv, int is_dvb_tc);
static void netup_unidvb_queue_cleanup(struct netup_dma *dma);
static struct cxd2841er_config demod_config = {
.i2c_addr = 0xc8,
.xtal = SONY_XTAL_24000,
.flags = CXD2841ER_USE_GATECTRL | CXD2841ER_ASCOT
};
static struct horus3a_config horus3a_conf = {
.i2c_address = 0xc0,
.xtal_freq_mhz = 16,
.set_tuner_callback = netup_unidvb_tuner_ctrl
};
static struct ascot2e_config ascot2e_conf = {
.i2c_address = 0xc2,
.set_tuner_callback = netup_unidvb_tuner_ctrl
};
static struct helene_config helene_conf = {
.i2c_address = 0xc0,
.xtal = SONY_HELENE_XTAL_24000,
.set_tuner_callback = netup_unidvb_tuner_ctrl
};
static struct lnbh25_config lnbh25_conf = {
.i2c_address = 0x10,
.data2_config = LNBH25_TEN | LNBH25_EXTM
};
static int netup_unidvb_tuner_ctrl(void *priv, int is_dvb_tc)
{
u8 reg, mask;
struct netup_dma *dma = priv;
struct netup_unidvb_dev *ndev;
if (!priv)
return -EINVAL;
ndev = dma->ndev;
dev_dbg(&ndev->pci_dev->dev, "%s(): num %d is_dvb_tc %d\n",
__func__, dma->num, is_dvb_tc);
reg = readb(ndev->bmmio0 + GPIO_REG_IO);
mask = (dma->num == 0) ? GPIO_RFA_CTL : GPIO_RFB_CTL;
/* inverted tuner control in hw rev. 1.4 */
if (ndev->rev == NETUP_HW_REV_1_4)
is_dvb_tc = !is_dvb_tc;
if (!is_dvb_tc)
reg |= mask;
else
reg &= ~mask;
writeb(reg, ndev->bmmio0 + GPIO_REG_IO);
return 0;
}
static void netup_unidvb_dev_enable(struct netup_unidvb_dev *ndev)
{
u16 gpio_reg;
/* enable PCI-E interrupts */
writel(AVL_IRQ_ENABLE, ndev->bmmio0 + AVL_PCIE_IENR);
/* unreset frontends bits[0:1] */
writeb(0x00, ndev->bmmio0 + GPIO_REG_IO);
msleep(100);
gpio_reg =
GPIO_FEA_RESET | GPIO_FEB_RESET |
GPIO_FEA_TU_RESET | GPIO_FEB_TU_RESET |
GPIO_RFA_CTL | GPIO_RFB_CTL;
writeb(gpio_reg, ndev->bmmio0 + GPIO_REG_IO);
dev_dbg(&ndev->pci_dev->dev,
"%s(): AVL_PCIE_IENR 0x%x GPIO_REG_IO 0x%x\n",
__func__, readl(ndev->bmmio0 + AVL_PCIE_IENR),
(int)readb(ndev->bmmio0 + GPIO_REG_IO));
}
static void netup_unidvb_dma_enable(struct netup_dma *dma, int enable)
{
u32 irq_mask = (dma->num == 0 ?
NETUP_UNIDVB_IRQ_DMA1 : NETUP_UNIDVB_IRQ_DMA2);
dev_dbg(&dma->ndev->pci_dev->dev,
"%s(): DMA%d enable %d\n", __func__, dma->num, enable);
if (enable) {
writel(BIT_DMA_RUN, &dma->regs->ctrlstat_set);
writew(irq_mask, dma->ndev->bmmio0 + REG_IMASK_SET);
} else {
writel(BIT_DMA_RUN, &dma->regs->ctrlstat_clear);
writew(irq_mask, dma->ndev->bmmio0 + REG_IMASK_CLEAR);
}
}
static irqreturn_t netup_dma_interrupt(struct netup_dma *dma)
{
u64 addr_curr;
u32 size;
unsigned long flags;
struct device *dev = &dma->ndev->pci_dev->dev;
spin_lock_irqsave(&dma->lock, flags);
addr_curr = ((u64)readl(&dma->regs->curr_addr_hi) << 32) |
(u64)readl(&dma->regs->curr_addr_lo) | dma->high_addr;
/* clear IRQ */
writel(BIT_DMA_IRQ, &dma->regs->ctrlstat_clear);
/* sanity check */
if (addr_curr < dma->addr_phys ||
addr_curr > dma->addr_phys + dma->ring_buffer_size) {
if (addr_curr != 0) {
dev_err(dev,
"%s(): addr 0x%llx not from 0x%llx:0x%llx\n",
__func__, addr_curr, (u64)dma->addr_phys,
(u64)(dma->addr_phys + dma->ring_buffer_size));
}
goto irq_handled;
}
size = (addr_curr >= dma->addr_last) ?
(u32)(addr_curr - dma->addr_last) :
(u32)(dma->ring_buffer_size - (dma->addr_last - addr_curr));
if (dma->data_size != 0) {
printk_ratelimited("%s(): lost interrupt, data size %d\n",
__func__, dma->data_size);
dma->data_size += size;
}
if (dma->data_size == 0 || dma->data_size > dma->ring_buffer_size) {
dma->data_size = size;
dma->data_offset = (u32)(dma->addr_last - dma->addr_phys);
}
dma->addr_last = addr_curr;
queue_work(dma->ndev->wq, &dma->work);
irq_handled:
spin_unlock_irqrestore(&dma->lock, flags);
return IRQ_HANDLED;
}
static irqreturn_t netup_unidvb_isr(int irq, void *dev_id)
{
struct pci_dev *pci_dev = (struct pci_dev *)dev_id;
struct netup_unidvb_dev *ndev = pci_get_drvdata(pci_dev);
u32 reg40, reg_isr;
irqreturn_t iret = IRQ_NONE;
/* disable interrupts */
writel(0, ndev->bmmio0 + AVL_PCIE_IENR);
/* check IRQ source */
reg40 = readl(ndev->bmmio0 + AVL_PCIE_ISR);
if ((reg40 & AVL_IRQ_ASSERTED) != 0) {
/* IRQ is being signaled */
reg_isr = readw(ndev->bmmio0 + REG_ISR);
if (reg_isr & NETUP_UNIDVB_IRQ_SPI)
iret = netup_spi_interrupt(ndev->spi);
else if (!ndev->old_fw) {
if (reg_isr & NETUP_UNIDVB_IRQ_I2C0) {
iret = netup_i2c_interrupt(&ndev->i2c[0]);
} else if (reg_isr & NETUP_UNIDVB_IRQ_I2C1) {
iret = netup_i2c_interrupt(&ndev->i2c[1]);
} else if (reg_isr & NETUP_UNIDVB_IRQ_DMA1) {
iret = netup_dma_interrupt(&ndev->dma[0]);
} else if (reg_isr & NETUP_UNIDVB_IRQ_DMA2) {
iret = netup_dma_interrupt(&ndev->dma[1]);
} else if (reg_isr & NETUP_UNIDVB_IRQ_CI) {
iret = netup_ci_interrupt(ndev);
} else {
goto err;
}
} else {
err:
dev_err(&pci_dev->dev,
"%s(): unknown interrupt 0x%x\n",
__func__, reg_isr);
}
}
/* re-enable interrupts */
writel(AVL_IRQ_ENABLE, ndev->bmmio0 + AVL_PCIE_IENR);
return iret;
}
static int netup_unidvb_queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers,
unsigned int *nplanes,
unsigned int sizes[],
struct device *alloc_devs[])
{
struct netup_dma *dma = vb2_get_drv_priv(vq);
dev_dbg(&dma->ndev->pci_dev->dev, "%s()\n", __func__);
*nplanes = 1;
if (vq->num_buffers + *nbuffers < VIDEO_MAX_FRAME)
*nbuffers = VIDEO_MAX_FRAME - vq->num_buffers;
sizes[0] = PAGE_ALIGN(NETUP_DMA_PACKETS_COUNT * 188);
dev_dbg(&dma->ndev->pci_dev->dev, "%s() nbuffers=%d sizes[0]=%d\n",
__func__, *nbuffers, sizes[0]);
return 0;
}
static int netup_unidvb_buf_prepare(struct vb2_buffer *vb)
{
struct netup_dma *dma = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct netup_unidvb_buffer *buf = container_of(vbuf,
struct netup_unidvb_buffer, vb);
dev_dbg(&dma->ndev->pci_dev->dev, "%s(): buf 0x%p\n", __func__, buf);
buf->size = 0;
return 0;
}
static void netup_unidvb_buf_queue(struct vb2_buffer *vb)
{
unsigned long flags;
struct netup_dma *dma = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct netup_unidvb_buffer *buf = container_of(vbuf,
struct netup_unidvb_buffer, vb);
dev_dbg(&dma->ndev->pci_dev->dev, "%s(): %p\n", __func__, buf);
spin_lock_irqsave(&dma->lock, flags);
list_add_tail(&buf->list, &dma->free_buffers);
spin_unlock_irqrestore(&dma->lock, flags);
mod_timer(&dma->timeout, jiffies + msecs_to_jiffies(1000));
}
static int netup_unidvb_start_streaming(struct vb2_queue *q, unsigned int count)
{
struct netup_dma *dma = vb2_get_drv_priv(q);
dev_dbg(&dma->ndev->pci_dev->dev, "%s()\n", __func__);
netup_unidvb_dma_enable(dma, 1);
return 0;
}
static void netup_unidvb_stop_streaming(struct vb2_queue *q)
{
struct netup_dma *dma = vb2_get_drv_priv(q);
dev_dbg(&dma->ndev->pci_dev->dev, "%s()\n", __func__);
netup_unidvb_dma_enable(dma, 0);
netup_unidvb_queue_cleanup(dma);
}
static const struct vb2_ops dvb_qops = {
.queue_setup = netup_unidvb_queue_setup,
.buf_prepare = netup_unidvb_buf_prepare,
.buf_queue = netup_unidvb_buf_queue,
.start_streaming = netup_unidvb_start_streaming,
.stop_streaming = netup_unidvb_stop_streaming,
};
static int netup_unidvb_queue_init(struct netup_dma *dma,
struct vb2_queue *vb_queue)
{
int res;
/* Init videobuf2 queue structure */
vb_queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vb_queue->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
vb_queue->drv_priv = dma;
vb_queue->buf_struct_size = sizeof(struct netup_unidvb_buffer);
vb_queue->ops = &dvb_qops;
vb_queue->mem_ops = &vb2_vmalloc_memops;
vb_queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
res = vb2_queue_init(vb_queue);
if (res != 0) {
dev_err(&dma->ndev->pci_dev->dev,
"%s(): vb2_queue_init failed (%d)\n", __func__, res);
}
return res;
}
static int netup_unidvb_dvb_init(struct netup_unidvb_dev *ndev,
int num)
{
int fe_count = 2;
int i = 0;
struct vb2_dvb_frontend *fes[2];
u8 fe_name[32];
if (ndev->rev == NETUP_HW_REV_1_3)
demod_config.xtal = SONY_XTAL_20500;
else
demod_config.xtal = SONY_XTAL_24000;
if (num < 0 || num > 1) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to init DVB bus %d\n", __func__, num);
return -ENODEV;
}
mutex_init(&ndev->frontends[num].lock);
INIT_LIST_HEAD(&ndev->frontends[num].felist);
for (i = 0; i < fe_count; i++) {
if (vb2_dvb_alloc_frontend(&ndev->frontends[num], i+1)
== NULL) {
dev_err(&ndev->pci_dev->dev,
"%s(): unable to allocate vb2_dvb_frontend\n",
__func__);
return -ENOMEM;
}
}
for (i = 0; i < fe_count; i++) {
fes[i] = vb2_dvb_get_frontend(&ndev->frontends[num], i+1);
if (fes[i] == NULL) {
dev_err(&ndev->pci_dev->dev,
"%s(): frontends has not been allocated\n",
__func__);
return -EINVAL;
}
}
for (i = 0; i < fe_count; i++) {
netup_unidvb_queue_init(&ndev->dma[num], &fes[i]->dvb.dvbq);
snprintf(fe_name, sizeof(fe_name), "netup_fe%d", i);
fes[i]->dvb.name = fe_name;
}
fes[0]->dvb.frontend = dvb_attach(cxd2841er_attach_s,
&demod_config, &ndev->i2c[num].adap);
if (fes[0]->dvb.frontend == NULL) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to attach DVB-S/S2 frontend\n",
__func__);
goto frontend_detach;
}
if (ndev->rev == NETUP_HW_REV_1_3) {
horus3a_conf.set_tuner_priv = &ndev->dma[num];
if (!dvb_attach(horus3a_attach, fes[0]->dvb.frontend,
&horus3a_conf, &ndev->i2c[num].adap)) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to attach HORUS3A DVB-S/S2 tuner frontend\n",
__func__);
goto frontend_detach;
}
} else {
helene_conf.set_tuner_priv = &ndev->dma[num];
if (!dvb_attach(helene_attach_s, fes[0]->dvb.frontend,
&helene_conf, &ndev->i2c[num].adap)) {
dev_err(&ndev->pci_dev->dev,
"%s(): unable to attach HELENE DVB-S/S2 tuner frontend\n",
__func__);
goto frontend_detach;
}
}
if (!dvb_attach(lnbh25_attach, fes[0]->dvb.frontend,
&lnbh25_conf, &ndev->i2c[num].adap)) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to attach SEC frontend\n", __func__);
goto frontend_detach;
}
/* DVB-T/T2 frontend */
fes[1]->dvb.frontend = dvb_attach(cxd2841er_attach_t_c,
&demod_config, &ndev->i2c[num].adap);
if (fes[1]->dvb.frontend == NULL) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to attach Ter frontend\n", __func__);
goto frontend_detach;
}
fes[1]->dvb.frontend->id = 1;
if (ndev->rev == NETUP_HW_REV_1_3) {
ascot2e_conf.set_tuner_priv = &ndev->dma[num];
if (!dvb_attach(ascot2e_attach, fes[1]->dvb.frontend,
&ascot2e_conf, &ndev->i2c[num].adap)) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to attach Ter tuner frontend\n",
__func__);
goto frontend_detach;
}
} else {
helene_conf.set_tuner_priv = &ndev->dma[num];
if (!dvb_attach(helene_attach, fes[1]->dvb.frontend,
&helene_conf, &ndev->i2c[num].adap)) {
dev_err(&ndev->pci_dev->dev,
"%s(): unable to attach HELENE Ter tuner frontend\n",
__func__);
goto frontend_detach;
}
}
if (vb2_dvb_register_bus(&ndev->frontends[num],
THIS_MODULE, NULL,
&ndev->pci_dev->dev, NULL, adapter_nr, 1)) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): unable to register DVB bus %d\n",
__func__, num);
goto frontend_detach;
}
dev_info(&ndev->pci_dev->dev, "DVB init done, num=%d\n", num);
return 0;
frontend_detach:
vb2_dvb_dealloc_frontends(&ndev->frontends[num]);
return -EINVAL;
}
static void netup_unidvb_dvb_fini(struct netup_unidvb_dev *ndev, int num)
{
if (num < 0 || num > 1) {
dev_err(&ndev->pci_dev->dev,
"%s(): unable to unregister DVB bus %d\n",
__func__, num);
return;
}
vb2_dvb_unregister_bus(&ndev->frontends[num]);
dev_info(&ndev->pci_dev->dev,
"%s(): DVB bus %d unregistered\n", __func__, num);
}
static int netup_unidvb_dvb_setup(struct netup_unidvb_dev *ndev)
{
int res;
res = netup_unidvb_dvb_init(ndev, 0);
if (res)
return res;
res = netup_unidvb_dvb_init(ndev, 1);
if (res) {
netup_unidvb_dvb_fini(ndev, 0);
return res;
}
return 0;
}
static int netup_unidvb_ring_copy(struct netup_dma *dma,
struct netup_unidvb_buffer *buf)
{
u32 copy_bytes, ring_bytes;
u32 buff_bytes = NETUP_DMA_PACKETS_COUNT * 188 - buf->size;
u8 *p = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
struct netup_unidvb_dev *ndev = dma->ndev;
if (p == NULL) {
dev_err(&ndev->pci_dev->dev,
"%s(): buffer is NULL\n", __func__);
return -EINVAL;
}
p += buf->size;
if (dma->data_offset + dma->data_size > dma->ring_buffer_size) {
ring_bytes = dma->ring_buffer_size - dma->data_offset;
copy_bytes = (ring_bytes > buff_bytes) ?
buff_bytes : ring_bytes;
memcpy_fromio(p, (u8 __iomem *)(dma->addr_virt + dma->data_offset), copy_bytes);
p += copy_bytes;
buf->size += copy_bytes;
buff_bytes -= copy_bytes;
dma->data_size -= copy_bytes;
dma->data_offset += copy_bytes;
if (dma->data_offset == dma->ring_buffer_size)
dma->data_offset = 0;
}
if (buff_bytes > 0) {
ring_bytes = dma->data_size;
copy_bytes = (ring_bytes > buff_bytes) ?
buff_bytes : ring_bytes;
memcpy_fromio(p, (u8 __iomem *)(dma->addr_virt + dma->data_offset), copy_bytes);
buf->size += copy_bytes;
dma->data_size -= copy_bytes;
dma->data_offset += copy_bytes;
if (dma->data_offset == dma->ring_buffer_size)
dma->data_offset = 0;
}
return 0;
}
static void netup_unidvb_dma_worker(struct work_struct *work)
{
struct netup_dma *dma = container_of(work, struct netup_dma, work);
struct netup_unidvb_dev *ndev = dma->ndev;
struct netup_unidvb_buffer *buf;
unsigned long flags;
spin_lock_irqsave(&dma->lock, flags);
if (dma->data_size == 0) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): data_size == 0\n", __func__);
goto work_done;
}
while (dma->data_size > 0) {
if (list_empty(&dma->free_buffers)) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): no free buffers\n", __func__);
goto work_done;
}
buf = list_first_entry(&dma->free_buffers,
struct netup_unidvb_buffer, list);
if (buf->size >= NETUP_DMA_PACKETS_COUNT * 188) {
dev_dbg(&ndev->pci_dev->dev,
"%s(): buffer overflow, size %d\n",
__func__, buf->size);
goto work_done;
}
if (netup_unidvb_ring_copy(dma, buf))
goto work_done;
if (buf->size == NETUP_DMA_PACKETS_COUNT * 188) {
list_del(&buf->list);
dev_dbg(&ndev->pci_dev->dev,
"%s(): buffer %p done, size %d\n",
__func__, buf, buf->size);
buf->vb.vb2_buf.timestamp = ktime_get_ns();
vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
}
}
work_done:
dma->data_size = 0;
spin_unlock_irqrestore(&dma->lock, flags);
}
static void netup_unidvb_queue_cleanup(struct netup_dma *dma)
{
struct netup_unidvb_buffer *buf;
unsigned long flags;
spin_lock_irqsave(&dma->lock, flags);
while (!list_empty(&dma->free_buffers)) {
buf = list_first_entry(&dma->free_buffers,
struct netup_unidvb_buffer, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
spin_unlock_irqrestore(&dma->lock, flags);
}
static void netup_unidvb_dma_timeout(struct timer_list *t)
{
struct netup_dma *dma = from_timer(dma, t, timeout);
struct netup_unidvb_dev *ndev = dma->ndev;
dev_dbg(&ndev->pci_dev->dev, "%s()\n", __func__);
netup_unidvb_queue_cleanup(dma);
}
static int netup_unidvb_dma_init(struct netup_unidvb_dev *ndev, int num)
{
struct netup_dma *dma;
struct device *dev = &ndev->pci_dev->dev;
if (num < 0 || num > 1) {
dev_err(dev, "%s(): unable to register DMA%d\n",
__func__, num);
return -ENODEV;
}
dma = &ndev->dma[num];
dev_info(dev, "%s(): starting DMA%d\n", __func__, num);
dma->num = num;
dma->ndev = ndev;
spin_lock_init(&dma->lock);
INIT_WORK(&dma->work, netup_unidvb_dma_worker);
INIT_LIST_HEAD(&dma->free_buffers);
timer_setup(&dma->timeout, netup_unidvb_dma_timeout, 0);
dma->ring_buffer_size = ndev->dma_size / 2;
dma->addr_virt = ndev->dma_virt + dma->ring_buffer_size * num;
dma->addr_phys = (dma_addr_t)((u64)ndev->dma_phys +
dma->ring_buffer_size * num);
dev_info(dev, "%s(): DMA%d buffer virt/phys 0x%p/0x%llx size %d\n",
__func__, num, dma->addr_virt,
(unsigned long long)dma->addr_phys,
dma->ring_buffer_size);
memset_io((u8 __iomem *)dma->addr_virt, 0, dma->ring_buffer_size);
dma->addr_last = dma->addr_phys;
dma->high_addr = (u32)(dma->addr_phys & 0xC0000000);
dma->regs = (struct netup_dma_regs __iomem *)(num == 0 ?
ndev->bmmio0 + NETUP_DMA0_ADDR :
ndev->bmmio0 + NETUP_DMA1_ADDR);
writel((NETUP_DMA_BLOCKS_COUNT << 24) |
(NETUP_DMA_PACKETS_COUNT << 8) | 188, &dma->regs->size);
writel((u32)(dma->addr_phys & 0x3FFFFFFF), &dma->regs->start_addr_lo);
writel(0, &dma->regs->start_addr_hi);
writel(dma->high_addr, ndev->bmmio0 + 0x1000);
writel(375000000, &dma->regs->timeout);
msleep(1000);
writel(BIT_DMA_IRQ, &dma->regs->ctrlstat_clear);
return 0;
}
static void netup_unidvb_dma_fini(struct netup_unidvb_dev *ndev, int num)
{
struct netup_dma *dma;
if (num < 0 || num > 1)
return;
dev_dbg(&ndev->pci_dev->dev, "%s(): num %d\n", __func__, num);
dma = &ndev->dma[num];
netup_unidvb_dma_enable(dma, 0);
msleep(50);
cancel_work_sync(&dma->work);
del_timer_sync(&dma->timeout);
}
static int netup_unidvb_dma_setup(struct netup_unidvb_dev *ndev)
{
int res;
res = netup_unidvb_dma_init(ndev, 0);
if (res)
return res;
res = netup_unidvb_dma_init(ndev, 1);
if (res) {
netup_unidvb_dma_fini(ndev, 0);
return res;
}
netup_unidvb_dma_enable(&ndev->dma[0], 0);
netup_unidvb_dma_enable(&ndev->dma[1], 0);
return 0;
}
static int netup_unidvb_ci_setup(struct netup_unidvb_dev *ndev,
struct pci_dev *pci_dev)
{
int res;
writew(NETUP_UNIDVB_IRQ_CI, ndev->bmmio0 + REG_IMASK_SET);
res = netup_unidvb_ci_register(ndev, 0, pci_dev);
if (res)
return res;
res = netup_unidvb_ci_register(ndev, 1, pci_dev);
if (res)
netup_unidvb_ci_unregister(ndev, 0);
return res;
}
static int netup_unidvb_request_mmio(struct pci_dev *pci_dev)
{
if (!request_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0), NETUP_UNIDVB_NAME)) {
dev_err(&pci_dev->dev,
"%s(): unable to request MMIO bar 0 at 0x%llx\n",
__func__,
(unsigned long long)pci_resource_start(pci_dev, 0));
return -EBUSY;
}
if (!request_mem_region(pci_resource_start(pci_dev, 1),
pci_resource_len(pci_dev, 1), NETUP_UNIDVB_NAME)) {
dev_err(&pci_dev->dev,
"%s(): unable to request MMIO bar 1 at 0x%llx\n",
__func__,
(unsigned long long)pci_resource_start(pci_dev, 1));
release_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
return -EBUSY;
}
return 0;
}
static int netup_unidvb_request_modules(struct device *dev)
{
static const char * const modules[] = {
"lnbh25", "ascot2e", "horus3a", "cxd2841er", "helene", NULL
};
const char * const *curr_mod = modules;
int err;
while (*curr_mod != NULL) {
err = request_module(*curr_mod);
if (err) {
dev_warn(dev, "request_module(%s) failed: %d\n",
*curr_mod, err);
}
++curr_mod;
}
return 0;
}
static int netup_unidvb_initdev(struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
u8 board_revision;
u16 board_vendor;
struct netup_unidvb_dev *ndev;
int old_firmware = 0;
netup_unidvb_request_modules(&pci_dev->dev);
/* Check card revision */
if (pci_dev->revision != NETUP_PCI_DEV_REVISION) {
dev_err(&pci_dev->dev,
"netup_unidvb: expected card revision %d, got %d\n",
NETUP_PCI_DEV_REVISION, pci_dev->revision);
dev_err(&pci_dev->dev,
"Please upgrade firmware!\n");
dev_err(&pci_dev->dev,
"Instructions on http://www.netup.tv\n");
old_firmware = 1;
spi_enable = 1;
}
/* allocate device context */
ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
if (!ndev)
goto dev_alloc_err;
/* detect hardware revision */
if (pci_dev->device == NETUP_HW_REV_1_3)
ndev->rev = NETUP_HW_REV_1_3;
else
ndev->rev = NETUP_HW_REV_1_4;
dev_info(&pci_dev->dev,
"%s(): board (0x%x) hardware revision 0x%x\n",
__func__, pci_dev->device, ndev->rev);
ndev->old_fw = old_firmware;
ndev->wq = create_singlethread_workqueue(NETUP_UNIDVB_NAME);
if (!ndev->wq) {
dev_err(&pci_dev->dev,
"%s(): unable to create workqueue\n", __func__);
goto wq_create_err;
}
ndev->pci_dev = pci_dev;
ndev->pci_bus = pci_dev->bus->number;
ndev->pci_slot = PCI_SLOT(pci_dev->devfn);
ndev->pci_func = PCI_FUNC(pci_dev->devfn);
ndev->board_num = ndev->pci_bus*10 + ndev->pci_slot;
pci_set_drvdata(pci_dev, ndev);
/* PCI init */
dev_info(&pci_dev->dev, "%s(): PCI device (%d). Bus:0x%x Slot:0x%x\n",
__func__, ndev->board_num, ndev->pci_bus, ndev->pci_slot);
if (pci_enable_device(pci_dev)) {
dev_err(&pci_dev->dev, "%s(): pci_enable_device failed\n",
__func__);
goto pci_enable_err;
}
/* read PCI info */
pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &board_revision);
pci_read_config_word(pci_dev, PCI_VENDOR_ID, &board_vendor);
if (board_vendor != NETUP_VENDOR_ID) {
dev_err(&pci_dev->dev, "%s(): unknown board vendor 0x%x",
__func__, board_vendor);
goto pci_detect_err;
}
dev_info(&pci_dev->dev,
"%s(): board vendor 0x%x, revision 0x%x\n",
__func__, board_vendor, board_revision);
pci_set_master(pci_dev);
if (dma_set_mask(&pci_dev->dev, 0xffffffff) < 0) {
dev_err(&pci_dev->dev,
"%s(): 32bit PCI DMA is not supported\n", __func__);
goto pci_detect_err;
}
dev_info(&pci_dev->dev, "%s(): using 32bit PCI DMA\n", __func__);
/* Clear "no snoop" and "relaxed ordering" bits, use default MRRS. */
pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL,
PCI_EXP_DEVCTL_READRQ | PCI_EXP_DEVCTL_RELAX_EN |
PCI_EXP_DEVCTL_NOSNOOP_EN, 0);
/* Adjust PCIe completion timeout. */
pcie_capability_clear_and_set_word(pci_dev,
PCI_EXP_DEVCTL2, PCI_EXP_DEVCTL2_COMP_TIMEOUT, 0x2);
if (netup_unidvb_request_mmio(pci_dev)) {
dev_err(&pci_dev->dev,
"%s(): unable to request MMIO regions\n", __func__);
goto pci_detect_err;
}
ndev->lmmio0 = ioremap(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
if (!ndev->lmmio0) {
dev_err(&pci_dev->dev,
"%s(): unable to remap MMIO bar 0\n", __func__);
goto pci_bar0_error;
}
ndev->lmmio1 = ioremap(pci_resource_start(pci_dev, 1),
pci_resource_len(pci_dev, 1));
if (!ndev->lmmio1) {
dev_err(&pci_dev->dev,
"%s(): unable to remap MMIO bar 1\n", __func__);
goto pci_bar1_error;
}
ndev->bmmio0 = (u8 __iomem *)ndev->lmmio0;
ndev->bmmio1 = (u8 __iomem *)ndev->lmmio1;
dev_info(&pci_dev->dev,
"%s(): PCI MMIO at 0x%p (%d); 0x%p (%d); IRQ %d",
__func__,
ndev->lmmio0, (u32)pci_resource_len(pci_dev, 0),
ndev->lmmio1, (u32)pci_resource_len(pci_dev, 1),
pci_dev->irq);
ndev->dma_size = 2 * 188 *
NETUP_DMA_BLOCKS_COUNT * NETUP_DMA_PACKETS_COUNT;
ndev->dma_virt = dma_alloc_coherent(&pci_dev->dev,
ndev->dma_size, &ndev->dma_phys, GFP_KERNEL);
if (!ndev->dma_virt) {
dev_err(&pci_dev->dev, "%s(): unable to allocate DMA buffer\n",
__func__);
goto dma_alloc_err;
}
netup_unidvb_dev_enable(ndev);
if (spi_enable && netup_spi_init(ndev)) {
dev_warn(&pci_dev->dev,
"netup_unidvb: SPI flash setup failed\n");
goto spi_setup_err;
}
if (old_firmware) {
dev_err(&pci_dev->dev,
"netup_unidvb: card initialization was incomplete\n");
return 0;
}
if (netup_i2c_register(ndev)) {
dev_err(&pci_dev->dev, "netup_unidvb: I2C setup failed\n");
goto i2c_setup_err;
}
/* enable I2C IRQs */
writew(NETUP_UNIDVB_IRQ_I2C0 | NETUP_UNIDVB_IRQ_I2C1,
ndev->bmmio0 + REG_IMASK_SET);
usleep_range(5000, 10000);
if (netup_unidvb_dvb_setup(ndev)) {
dev_err(&pci_dev->dev, "netup_unidvb: DVB setup failed\n");
goto dvb_setup_err;
}
if (netup_unidvb_ci_setup(ndev, pci_dev)) {
dev_err(&pci_dev->dev, "netup_unidvb: CI setup failed\n");
goto ci_setup_err;
}
if (netup_unidvb_dma_setup(ndev)) {
dev_err(&pci_dev->dev, "netup_unidvb: DMA setup failed\n");
goto dma_setup_err;
}
if (request_irq(pci_dev->irq, netup_unidvb_isr, IRQF_SHARED,
"netup_unidvb", pci_dev) < 0) {
dev_err(&pci_dev->dev,
"%s(): can't get IRQ %d\n", __func__, pci_dev->irq);
goto dma_setup_err;
}
dev_info(&pci_dev->dev,
"netup_unidvb: device has been initialized\n");
return 0;
dma_setup_err:
netup_unidvb_ci_unregister(ndev, 0);
netup_unidvb_ci_unregister(ndev, 1);
ci_setup_err:
netup_unidvb_dvb_fini(ndev, 0);
netup_unidvb_dvb_fini(ndev, 1);
dvb_setup_err:
netup_i2c_unregister(ndev);
i2c_setup_err:
if (ndev->spi)
netup_spi_release(ndev);
spi_setup_err:
dma_free_coherent(&pci_dev->dev, ndev->dma_size,
ndev->dma_virt, ndev->dma_phys);
dma_alloc_err:
iounmap(ndev->lmmio1);
pci_bar1_error:
iounmap(ndev->lmmio0);
pci_bar0_error:
release_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
release_mem_region(pci_resource_start(pci_dev, 1),
pci_resource_len(pci_dev, 1));
pci_detect_err:
pci_disable_device(pci_dev);
pci_enable_err:
pci_set_drvdata(pci_dev, NULL);
destroy_workqueue(ndev->wq);
wq_create_err:
kfree(ndev);
dev_alloc_err:
dev_err(&pci_dev->dev,
"%s(): failed to initialize device\n", __func__);
return -EIO;
}
static void netup_unidvb_finidev(struct pci_dev *pci_dev)
{
struct netup_unidvb_dev *ndev = pci_get_drvdata(pci_dev);
dev_info(&pci_dev->dev, "%s(): trying to stop device\n", __func__);
if (!ndev->old_fw) {
netup_unidvb_dma_fini(ndev, 0);
netup_unidvb_dma_fini(ndev, 1);
netup_unidvb_ci_unregister(ndev, 0);
netup_unidvb_ci_unregister(ndev, 1);
netup_unidvb_dvb_fini(ndev, 0);
netup_unidvb_dvb_fini(ndev, 1);
netup_i2c_unregister(ndev);
}
if (ndev->spi)
netup_spi_release(ndev);
writew(0xffff, ndev->bmmio0 + REG_IMASK_CLEAR);
dma_free_coherent(&ndev->pci_dev->dev, ndev->dma_size,
ndev->dma_virt, ndev->dma_phys);
free_irq(pci_dev->irq, pci_dev);
iounmap(ndev->lmmio0);
iounmap(ndev->lmmio1);
release_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
release_mem_region(pci_resource_start(pci_dev, 1),
pci_resource_len(pci_dev, 1));
pci_disable_device(pci_dev);
pci_set_drvdata(pci_dev, NULL);
destroy_workqueue(ndev->wq);
kfree(ndev);
dev_info(&pci_dev->dev,
"%s(): device has been successfully stopped\n", __func__);
}
static const struct pci_device_id netup_unidvb_pci_tbl[] = {
{ PCI_DEVICE(0x1b55, 0x18f6) }, /* hw rev. 1.3 */
{ PCI_DEVICE(0x1b55, 0x18f7) }, /* hw rev. 1.4 */
{ 0, }
};
MODULE_DEVICE_TABLE(pci, netup_unidvb_pci_tbl);
static struct pci_driver netup_unidvb_pci_driver = {
.name = "netup_unidvb",
.id_table = netup_unidvb_pci_tbl,
.probe = netup_unidvb_initdev,
.remove = netup_unidvb_finidev,
};
module_pci_driver(netup_unidvb_pci_driver);
| linux-master | drivers/media/pci/netup_unidvb/netup_unidvb_core.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* netup_unidvb_spi.c
*
* Internal SPI driver for NetUP Universal Dual DVB-CI
*
* Copyright (C) 2014 NetUP Inc.
* Copyright (C) 2014 Sergey Kozlov <[email protected]>
* Copyright (C) 2014 Abylay Ospan <[email protected]>
*/
#include "netup_unidvb.h"
#include <linux/spi/spi.h>
#include <linux/spi/flash.h>
#include <linux/mtd/partitions.h>
#include <mtd/mtd-abi.h>
#define NETUP_SPI_CTRL_IRQ 0x1000
#define NETUP_SPI_CTRL_IMASK 0x2000
#define NETUP_SPI_CTRL_START 0x8000
#define NETUP_SPI_CTRL_LAST_CS 0x4000
#define NETUP_SPI_TIMEOUT 6000
enum netup_spi_state {
SPI_STATE_START,
SPI_STATE_DONE,
};
struct netup_spi_regs {
__u8 data[1024];
__le16 control_stat;
__le16 clock_divider;
} __packed __aligned(1);
struct netup_spi {
struct device *dev;
struct spi_master *master;
struct netup_spi_regs __iomem *regs;
u8 __iomem *mmio;
spinlock_t lock;
wait_queue_head_t waitq;
enum netup_spi_state state;
};
static char netup_spi_name[64] = "fpga";
static struct mtd_partition netup_spi_flash_partitions = {
.name = netup_spi_name,
.size = 0x1000000, /* 16MB */
.offset = 0,
.mask_flags = MTD_CAP_ROM
};
static struct flash_platform_data spi_flash_data = {
.name = "netup0_m25p128",
.parts = &netup_spi_flash_partitions,
.nr_parts = 1,
};
static struct spi_board_info netup_spi_board = {
.modalias = "m25p128",
.max_speed_hz = 11000000,
.chip_select = 0,
.mode = SPI_MODE_0,
.platform_data = &spi_flash_data,
};
irqreturn_t netup_spi_interrupt(struct netup_spi *spi)
{
u16 reg;
unsigned long flags;
if (!spi)
return IRQ_NONE;
spin_lock_irqsave(&spi->lock, flags);
reg = readw(&spi->regs->control_stat);
if (!(reg & NETUP_SPI_CTRL_IRQ)) {
spin_unlock_irqrestore(&spi->lock, flags);
dev_dbg(&spi->master->dev,
"%s(): not mine interrupt\n", __func__);
return IRQ_NONE;
}
writew(reg | NETUP_SPI_CTRL_IRQ, &spi->regs->control_stat);
reg = readw(&spi->regs->control_stat);
writew(reg & ~NETUP_SPI_CTRL_IMASK, &spi->regs->control_stat);
spi->state = SPI_STATE_DONE;
wake_up(&spi->waitq);
spin_unlock_irqrestore(&spi->lock, flags);
dev_dbg(&spi->master->dev,
"%s(): SPI interrupt handled\n", __func__);
return IRQ_HANDLED;
}
static int netup_spi_transfer(struct spi_master *master,
struct spi_message *msg)
{
struct netup_spi *spi = spi_master_get_devdata(master);
struct spi_transfer *t;
int result = 0;
u32 tr_size;
/* reset CS */
writew(NETUP_SPI_CTRL_LAST_CS, &spi->regs->control_stat);
writew(0, &spi->regs->control_stat);
list_for_each_entry(t, &msg->transfers, transfer_list) {
tr_size = t->len;
while (tr_size) {
u32 frag_offset = t->len - tr_size;
u32 frag_size = (tr_size > sizeof(spi->regs->data)) ?
sizeof(spi->regs->data) : tr_size;
int frag_last = 0;
if (list_is_last(&t->transfer_list,
&msg->transfers) &&
frag_offset + frag_size == t->len) {
frag_last = 1;
}
if (t->tx_buf) {
memcpy_toio(spi->regs->data,
t->tx_buf + frag_offset,
frag_size);
} else {
memset_io(spi->regs->data,
0, frag_size);
}
spi->state = SPI_STATE_START;
writew((frag_size & 0x3ff) |
NETUP_SPI_CTRL_IMASK |
NETUP_SPI_CTRL_START |
(frag_last ? NETUP_SPI_CTRL_LAST_CS : 0),
&spi->regs->control_stat);
dev_dbg(&spi->master->dev,
"%s(): control_stat 0x%04x\n",
__func__, readw(&spi->regs->control_stat));
wait_event_timeout(spi->waitq,
spi->state != SPI_STATE_START,
msecs_to_jiffies(NETUP_SPI_TIMEOUT));
if (spi->state == SPI_STATE_DONE) {
if (t->rx_buf) {
memcpy_fromio(t->rx_buf + frag_offset,
spi->regs->data, frag_size);
}
} else {
if (spi->state == SPI_STATE_START) {
dev_dbg(&spi->master->dev,
"%s(): transfer timeout\n",
__func__);
} else {
dev_dbg(&spi->master->dev,
"%s(): invalid state %d\n",
__func__, spi->state);
}
result = -EIO;
goto done;
}
tr_size -= frag_size;
msg->actual_length += frag_size;
}
}
done:
msg->status = result;
spi_finalize_current_message(master);
return result;
}
static int netup_spi_setup(struct spi_device *spi)
{
return 0;
}
int netup_spi_init(struct netup_unidvb_dev *ndev)
{
struct spi_master *master;
struct netup_spi *nspi;
master = devm_spi_alloc_master(&ndev->pci_dev->dev,
sizeof(struct netup_spi));
if (!master) {
dev_err(&ndev->pci_dev->dev,
"%s(): unable to alloc SPI master\n", __func__);
return -EINVAL;
}
nspi = spi_master_get_devdata(master);
master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
master->bus_num = -1;
master->num_chipselect = 1;
master->transfer_one_message = netup_spi_transfer;
master->setup = netup_spi_setup;
spin_lock_init(&nspi->lock);
init_waitqueue_head(&nspi->waitq);
nspi->master = master;
nspi->regs = (struct netup_spi_regs __iomem *)(ndev->bmmio0 + 0x4000);
writew(2, &nspi->regs->clock_divider);
writew(NETUP_UNIDVB_IRQ_SPI, ndev->bmmio0 + REG_IMASK_SET);
ndev->spi = nspi;
if (spi_register_master(master)) {
ndev->spi = NULL;
dev_err(&ndev->pci_dev->dev,
"%s(): unable to register SPI bus\n", __func__);
return -EINVAL;
}
snprintf(netup_spi_name,
sizeof(netup_spi_name),
"fpga_%02x:%02x.%01x",
ndev->pci_bus,
ndev->pci_slot,
ndev->pci_func);
if (!spi_new_device(master, &netup_spi_board)) {
spi_unregister_master(master);
ndev->spi = NULL;
dev_err(&ndev->pci_dev->dev,
"%s(): unable to create SPI device\n", __func__);
return -EINVAL;
}
dev_dbg(&ndev->pci_dev->dev, "%s(): SPI init OK\n", __func__);
return 0;
}
void netup_spi_release(struct netup_unidvb_dev *ndev)
{
u16 reg;
unsigned long flags;
struct netup_spi *spi = ndev->spi;
if (!spi)
return;
spi_unregister_master(spi->master);
spin_lock_irqsave(&spi->lock, flags);
reg = readw(&spi->regs->control_stat);
writew(reg | NETUP_SPI_CTRL_IRQ, &spi->regs->control_stat);
reg = readw(&spi->regs->control_stat);
writew(reg & ~NETUP_SPI_CTRL_IMASK, &spi->regs->control_stat);
spin_unlock_irqrestore(&spi->lock, flags);
ndev->spi = NULL;
}
| linux-master | drivers/media/pci/netup_unidvb/netup_unidvb_spi.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* netup_unidvb_ci.c
*
* DVB CAM support for NetUP Universal Dual DVB-CI
*
* Copyright (C) 2014 NetUP Inc.
* Copyright (C) 2014 Sergey Kozlov <[email protected]>
* Copyright (C) 2014 Abylay Ospan <[email protected]>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kmod.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include "netup_unidvb.h"
/* CI slot 0 base address */
#define CAM0_CONFIG 0x0
#define CAM0_IO 0x8000
#define CAM0_MEM 0x10000
#define CAM0_SZ 32
/* CI slot 1 base address */
#define CAM1_CONFIG 0x20000
#define CAM1_IO 0x28000
#define CAM1_MEM 0x30000
#define CAM1_SZ 32
/* ctrlstat registers */
#define CAM_CTRLSTAT_READ_SET 0x4980
#define CAM_CTRLSTAT_CLR 0x4982
/* register bits */
#define BIT_CAM_STCHG (1<<0)
#define BIT_CAM_PRESENT (1<<1)
#define BIT_CAM_RESET (1<<2)
#define BIT_CAM_BYPASS (1<<3)
#define BIT_CAM_READY (1<<4)
#define BIT_CAM_ERROR (1<<5)
#define BIT_CAM_OVERCURR (1<<6)
/* BIT_CAM_BYPASS bit shift for SLOT 1 */
#define CAM1_SHIFT 8
irqreturn_t netup_ci_interrupt(struct netup_unidvb_dev *ndev)
{
writew(0x101, ndev->bmmio0 + CAM_CTRLSTAT_CLR);
return IRQ_HANDLED;
}
static int netup_unidvb_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221,
int slot)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
u16 shift = (state->nr == 1) ? CAM1_SHIFT : 0;
dev_dbg(&dev->pci_dev->dev, "%s(): CAM_CTRLSTAT=0x%x\n",
__func__, readw(dev->bmmio0 + CAM_CTRLSTAT_READ_SET));
if (slot != 0)
return -EINVAL;
/* pass data to CAM module */
writew(BIT_CAM_BYPASS << shift, dev->bmmio0 + CAM_CTRLSTAT_CLR);
dev_dbg(&dev->pci_dev->dev, "%s(): CAM_CTRLSTAT=0x%x done\n",
__func__, readw(dev->bmmio0 + CAM_CTRLSTAT_READ_SET));
return 0;
}
static int netup_unidvb_ci_slot_shutdown(struct dvb_ca_en50221 *en50221,
int slot)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
dev_dbg(&dev->pci_dev->dev, "%s()\n", __func__);
return 0;
}
static int netup_unidvb_ci_slot_reset(struct dvb_ca_en50221 *en50221,
int slot)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
unsigned long timeout = 0;
u16 shift = (state->nr == 1) ? CAM1_SHIFT : 0;
u16 ci_stat = 0;
int reset_counter = 3;
dev_dbg(&dev->pci_dev->dev, "%s(): CAM_CTRLSTAT_READ_SET=0x%x\n",
__func__, readw(dev->bmmio0 + CAM_CTRLSTAT_READ_SET));
reset:
timeout = jiffies + msecs_to_jiffies(5000);
/* start reset */
writew(BIT_CAM_RESET << shift, dev->bmmio0 + CAM_CTRLSTAT_READ_SET);
dev_dbg(&dev->pci_dev->dev, "%s(): waiting for reset\n", __func__);
/* wait until reset done */
while (time_before(jiffies, timeout)) {
ci_stat = readw(dev->bmmio0 + CAM_CTRLSTAT_READ_SET);
if (ci_stat & (BIT_CAM_READY << shift))
break;
udelay(1000);
}
if (!(ci_stat & (BIT_CAM_READY << shift)) && reset_counter > 0) {
dev_dbg(&dev->pci_dev->dev,
"%s(): CAMP reset timeout! Will try again..\n",
__func__);
reset_counter--;
goto reset;
}
return 0;
}
static int netup_unidvb_poll_ci_slot_status(struct dvb_ca_en50221 *en50221,
int slot, int open)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
u16 shift = (state->nr == 1) ? CAM1_SHIFT : 0;
u16 ci_stat = 0;
dev_dbg(&dev->pci_dev->dev, "%s(): CAM_CTRLSTAT_READ_SET=0x%x\n",
__func__, readw(dev->bmmio0 + CAM_CTRLSTAT_READ_SET));
ci_stat = readw(dev->bmmio0 + CAM_CTRLSTAT_READ_SET);
if (ci_stat & (BIT_CAM_READY << shift)) {
state->status = DVB_CA_EN50221_POLL_CAM_PRESENT |
DVB_CA_EN50221_POLL_CAM_READY;
} else if (ci_stat & (BIT_CAM_PRESENT << shift)) {
state->status = DVB_CA_EN50221_POLL_CAM_PRESENT;
} else {
state->status = 0;
}
return state->status;
}
static int netup_unidvb_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
int slot, int addr)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
u8 val = *((u8 __force *)state->membase8_config + addr);
dev_dbg(&dev->pci_dev->dev,
"%s(): addr=0x%x val=0x%x\n", __func__, addr, val);
return val;
}
static int netup_unidvb_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
int slot, int addr, u8 data)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
dev_dbg(&dev->pci_dev->dev,
"%s(): addr=0x%x data=0x%x\n", __func__, addr, data);
*((u8 __force *)state->membase8_config + addr) = data;
return 0;
}
static int netup_unidvb_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221,
int slot, u8 addr)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
u8 val = *((u8 __force *)state->membase8_io + addr);
dev_dbg(&dev->pci_dev->dev,
"%s(): addr=0x%x val=0x%x\n", __func__, addr, val);
return val;
}
static int netup_unidvb_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221,
int slot, u8 addr, u8 data)
{
struct netup_ci_state *state = en50221->data;
struct netup_unidvb_dev *dev = state->dev;
dev_dbg(&dev->pci_dev->dev,
"%s(): addr=0x%x data=0x%x\n", __func__, addr, data);
*((u8 __force *)state->membase8_io + addr) = data;
return 0;
}
int netup_unidvb_ci_register(struct netup_unidvb_dev *dev,
int num, struct pci_dev *pci_dev)
{
int result;
struct netup_ci_state *state;
if (num < 0 || num > 1) {
dev_err(&pci_dev->dev, "%s(): invalid CI adapter %d\n",
__func__, num);
return -EINVAL;
}
state = &dev->ci[num];
state->nr = num;
state->membase8_config = dev->bmmio1 +
((num == 0) ? CAM0_CONFIG : CAM1_CONFIG);
state->membase8_io = dev->bmmio1 +
((num == 0) ? CAM0_IO : CAM1_IO);
state->dev = dev;
state->ca.owner = THIS_MODULE;
state->ca.read_attribute_mem = netup_unidvb_ci_read_attribute_mem;
state->ca.write_attribute_mem = netup_unidvb_ci_write_attribute_mem;
state->ca.read_cam_control = netup_unidvb_ci_read_cam_ctl;
state->ca.write_cam_control = netup_unidvb_ci_write_cam_ctl;
state->ca.slot_reset = netup_unidvb_ci_slot_reset;
state->ca.slot_shutdown = netup_unidvb_ci_slot_shutdown;
state->ca.slot_ts_enable = netup_unidvb_ci_slot_ts_ctl;
state->ca.poll_slot_status = netup_unidvb_poll_ci_slot_status;
state->ca.data = state;
result = dvb_ca_en50221_init(&dev->frontends[num].adapter,
&state->ca, 0, 1);
if (result < 0) {
dev_err(&pci_dev->dev,
"%s(): dvb_ca_en50221_init result %d\n",
__func__, result);
return result;
}
writew(NETUP_UNIDVB_IRQ_CI, dev->bmmio0 + REG_IMASK_SET);
dev_info(&pci_dev->dev,
"%s(): CI adapter %d init done\n", __func__, num);
return 0;
}
void netup_unidvb_ci_unregister(struct netup_unidvb_dev *dev, int num)
{
struct netup_ci_state *state;
dev_dbg(&dev->pci_dev->dev, "%s()\n", __func__);
if (num < 0 || num > 1) {
dev_err(&dev->pci_dev->dev, "%s(): invalid CI adapter %d\n",
__func__, num);
return;
}
state = &dev->ci[num];
dvb_ca_en50221_release(&state->ca);
}
| linux-master | drivers/media/pci/netup_unidvb/netup_unidvb_ci.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* netup_unidvb_i2c.c
*
* Internal I2C bus driver for NetUP Universal Dual DVB-CI
*
* Copyright (C) 2014 NetUP Inc.
* Copyright (C) 2014 Sergey Kozlov <[email protected]>
* Copyright (C) 2014 Abylay Ospan <[email protected]>
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include "netup_unidvb.h"
#define NETUP_I2C_BUS0_ADDR 0x4800
#define NETUP_I2C_BUS1_ADDR 0x4840
#define NETUP_I2C_TIMEOUT 1000
/* twi_ctrl0_stat reg bits */
#define TWI_IRQEN_COMPL 0x1
#define TWI_IRQEN_ANACK 0x2
#define TWI_IRQEN_DNACK 0x4
#define TWI_IRQ_COMPL (TWI_IRQEN_COMPL << 8)
#define TWI_IRQ_ANACK (TWI_IRQEN_ANACK << 8)
#define TWI_IRQ_DNACK (TWI_IRQEN_DNACK << 8)
#define TWI_IRQ_TX 0x800
#define TWI_IRQ_RX 0x1000
#define TWI_IRQEN (TWI_IRQEN_COMPL | TWI_IRQEN_ANACK | TWI_IRQEN_DNACK)
/* twi_addr_ctrl1 reg bits*/
#define TWI_TRANSFER 0x100
#define TWI_NOSTOP 0x200
#define TWI_SOFT_RESET 0x2000
/* twi_clkdiv reg value */
#define TWI_CLKDIV 156
/* fifo_stat_ctrl reg bits */
#define FIFO_IRQEN 0x8000
#define FIFO_RESET 0x4000
/* FIFO size */
#define FIFO_SIZE 16
struct netup_i2c_fifo_regs {
union {
__u8 data8;
__le16 data16;
__le32 data32;
};
__u8 padding[4];
__le16 stat_ctrl;
} __packed __aligned(1);
struct netup_i2c_regs {
__le16 clkdiv;
__le16 twi_ctrl0_stat;
__le16 twi_addr_ctrl1;
__le16 length;
__u8 padding1[8];
struct netup_i2c_fifo_regs tx_fifo;
__u8 padding2[6];
struct netup_i2c_fifo_regs rx_fifo;
} __packed __aligned(1);
irqreturn_t netup_i2c_interrupt(struct netup_i2c *i2c)
{
u16 reg, tmp;
unsigned long flags;
irqreturn_t iret = IRQ_HANDLED;
spin_lock_irqsave(&i2c->lock, flags);
reg = readw(&i2c->regs->twi_ctrl0_stat);
writew(reg & ~TWI_IRQEN, &i2c->regs->twi_ctrl0_stat);
dev_dbg(i2c->adap.dev.parent,
"%s(): twi_ctrl0_state 0x%x\n", __func__, reg);
if ((reg & TWI_IRQEN_COMPL) != 0 && (reg & TWI_IRQ_COMPL)) {
dev_dbg(i2c->adap.dev.parent,
"%s(): TWI_IRQEN_COMPL\n", __func__);
i2c->state = STATE_DONE;
goto irq_ok;
}
if ((reg & TWI_IRQEN_ANACK) != 0 && (reg & TWI_IRQ_ANACK)) {
dev_dbg(i2c->adap.dev.parent,
"%s(): TWI_IRQEN_ANACK\n", __func__);
i2c->state = STATE_ERROR;
goto irq_ok;
}
if ((reg & TWI_IRQEN_DNACK) != 0 && (reg & TWI_IRQ_DNACK)) {
dev_dbg(i2c->adap.dev.parent,
"%s(): TWI_IRQEN_DNACK\n", __func__);
i2c->state = STATE_ERROR;
goto irq_ok;
}
if ((reg & TWI_IRQ_RX) != 0) {
tmp = readw(&i2c->regs->rx_fifo.stat_ctrl);
writew(tmp & ~FIFO_IRQEN, &i2c->regs->rx_fifo.stat_ctrl);
i2c->state = STATE_WANT_READ;
dev_dbg(i2c->adap.dev.parent,
"%s(): want read\n", __func__);
goto irq_ok;
}
if ((reg & TWI_IRQ_TX) != 0) {
tmp = readw(&i2c->regs->tx_fifo.stat_ctrl);
writew(tmp & ~FIFO_IRQEN, &i2c->regs->tx_fifo.stat_ctrl);
i2c->state = STATE_WANT_WRITE;
dev_dbg(i2c->adap.dev.parent,
"%s(): want write\n", __func__);
goto irq_ok;
}
dev_warn(&i2c->adap.dev, "%s(): not mine interrupt\n", __func__);
iret = IRQ_NONE;
irq_ok:
spin_unlock_irqrestore(&i2c->lock, flags);
if (iret == IRQ_HANDLED)
wake_up(&i2c->wq);
return iret;
}
static void netup_i2c_reset(struct netup_i2c *i2c)
{
dev_dbg(i2c->adap.dev.parent, "%s()\n", __func__);
i2c->state = STATE_DONE;
writew(TWI_SOFT_RESET, &i2c->regs->twi_addr_ctrl1);
writew(TWI_CLKDIV, &i2c->regs->clkdiv);
writew(FIFO_RESET, &i2c->regs->tx_fifo.stat_ctrl);
writew(FIFO_RESET, &i2c->regs->rx_fifo.stat_ctrl);
writew(0x800, &i2c->regs->tx_fifo.stat_ctrl);
writew(0x800, &i2c->regs->rx_fifo.stat_ctrl);
}
static void netup_i2c_fifo_tx(struct netup_i2c *i2c)
{
u8 data;
u32 fifo_space = FIFO_SIZE -
(readw(&i2c->regs->tx_fifo.stat_ctrl) & 0x3f);
u32 msg_length = i2c->msg->len - i2c->xmit_size;
msg_length = (msg_length < fifo_space ? msg_length : fifo_space);
while (msg_length--) {
data = i2c->msg->buf[i2c->xmit_size++];
writeb(data, &i2c->regs->tx_fifo.data8);
dev_dbg(i2c->adap.dev.parent,
"%s(): write 0x%02x\n", __func__, data);
}
if (i2c->xmit_size < i2c->msg->len) {
dev_dbg(i2c->adap.dev.parent,
"%s(): TX IRQ enabled\n", __func__);
writew(readw(&i2c->regs->tx_fifo.stat_ctrl) | FIFO_IRQEN,
&i2c->regs->tx_fifo.stat_ctrl);
}
}
static void netup_i2c_fifo_rx(struct netup_i2c *i2c)
{
u8 data;
u32 fifo_size = readw(&i2c->regs->rx_fifo.stat_ctrl) & 0x3f;
dev_dbg(i2c->adap.dev.parent,
"%s(): RX fifo size %d\n", __func__, fifo_size);
while (fifo_size--) {
data = readb(&i2c->regs->rx_fifo.data8);
if ((i2c->msg->flags & I2C_M_RD) != 0 &&
i2c->xmit_size < i2c->msg->len) {
i2c->msg->buf[i2c->xmit_size++] = data;
dev_dbg(i2c->adap.dev.parent,
"%s(): read 0x%02x\n", __func__, data);
}
}
if (i2c->xmit_size < i2c->msg->len) {
dev_dbg(i2c->adap.dev.parent,
"%s(): RX IRQ enabled\n", __func__);
writew(readw(&i2c->regs->rx_fifo.stat_ctrl) | FIFO_IRQEN,
&i2c->regs->rx_fifo.stat_ctrl);
}
}
static void netup_i2c_start_xfer(struct netup_i2c *i2c)
{
u16 rdflag = ((i2c->msg->flags & I2C_M_RD) ? 1 : 0);
u16 reg = readw(&i2c->regs->twi_ctrl0_stat);
writew(TWI_IRQEN | reg, &i2c->regs->twi_ctrl0_stat);
writew(i2c->msg->len, &i2c->regs->length);
writew(TWI_TRANSFER | (i2c->msg->addr << 1) | rdflag,
&i2c->regs->twi_addr_ctrl1);
dev_dbg(i2c->adap.dev.parent,
"%s(): length %d twi_addr_ctrl1 0x%x twi_ctrl0_stat 0x%x\n",
__func__, readw(&i2c->regs->length),
readw(&i2c->regs->twi_addr_ctrl1),
readw(&i2c->regs->twi_ctrl0_stat));
i2c->state = STATE_WAIT;
i2c->xmit_size = 0;
if (!rdflag)
netup_i2c_fifo_tx(i2c);
else
writew(FIFO_IRQEN | readw(&i2c->regs->rx_fifo.stat_ctrl),
&i2c->regs->rx_fifo.stat_ctrl);
}
static int netup_i2c_xfer(struct i2c_adapter *adap,
struct i2c_msg *msgs, int num)
{
unsigned long flags;
int i, trans_done, res = num;
struct netup_i2c *i2c = i2c_get_adapdata(adap);
u16 reg;
spin_lock_irqsave(&i2c->lock, flags);
if (i2c->state != STATE_DONE) {
dev_dbg(i2c->adap.dev.parent,
"%s(): i2c->state == %d, resetting I2C\n",
__func__, i2c->state);
netup_i2c_reset(i2c);
}
dev_dbg(i2c->adap.dev.parent, "%s() num %d\n", __func__, num);
for (i = 0; i < num; i++) {
i2c->msg = &msgs[i];
netup_i2c_start_xfer(i2c);
trans_done = 0;
while (!trans_done) {
spin_unlock_irqrestore(&i2c->lock, flags);
if (wait_event_timeout(i2c->wq,
i2c->state != STATE_WAIT,
msecs_to_jiffies(NETUP_I2C_TIMEOUT))) {
spin_lock_irqsave(&i2c->lock, flags);
switch (i2c->state) {
case STATE_WANT_READ:
netup_i2c_fifo_rx(i2c);
break;
case STATE_WANT_WRITE:
netup_i2c_fifo_tx(i2c);
break;
case STATE_DONE:
if ((i2c->msg->flags & I2C_M_RD) != 0 &&
i2c->xmit_size != i2c->msg->len)
netup_i2c_fifo_rx(i2c);
dev_dbg(i2c->adap.dev.parent,
"%s(): msg %d OK\n",
__func__, i);
trans_done = 1;
break;
case STATE_ERROR:
res = -EIO;
dev_dbg(i2c->adap.dev.parent,
"%s(): error state\n",
__func__);
goto done;
default:
dev_dbg(i2c->adap.dev.parent,
"%s(): invalid state %d\n",
__func__, i2c->state);
res = -EINVAL;
goto done;
}
if (!trans_done) {
i2c->state = STATE_WAIT;
reg = readw(
&i2c->regs->twi_ctrl0_stat);
writew(TWI_IRQEN | reg,
&i2c->regs->twi_ctrl0_stat);
}
spin_unlock_irqrestore(&i2c->lock, flags);
} else {
spin_lock_irqsave(&i2c->lock, flags);
dev_dbg(i2c->adap.dev.parent,
"%s(): wait timeout\n", __func__);
res = -ETIMEDOUT;
goto done;
}
spin_lock_irqsave(&i2c->lock, flags);
}
}
done:
spin_unlock_irqrestore(&i2c->lock, flags);
dev_dbg(i2c->adap.dev.parent, "%s(): result %d\n", __func__, res);
return res;
}
static u32 netup_i2c_func(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}
static const struct i2c_algorithm netup_i2c_algorithm = {
.master_xfer = netup_i2c_xfer,
.functionality = netup_i2c_func,
};
static const struct i2c_adapter netup_i2c_adapter = {
.owner = THIS_MODULE,
.name = NETUP_UNIDVB_NAME,
.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
.algo = &netup_i2c_algorithm,
};
static int netup_i2c_init(struct netup_unidvb_dev *ndev, int bus_num)
{
int ret;
struct netup_i2c *i2c;
if (bus_num < 0 || bus_num > 1) {
dev_err(&ndev->pci_dev->dev,
"%s(): invalid bus_num %d\n", __func__, bus_num);
return -EINVAL;
}
i2c = &ndev->i2c[bus_num];
spin_lock_init(&i2c->lock);
init_waitqueue_head(&i2c->wq);
i2c->regs = (struct netup_i2c_regs __iomem *)(ndev->bmmio0 +
(bus_num == 0 ? NETUP_I2C_BUS0_ADDR : NETUP_I2C_BUS1_ADDR));
netup_i2c_reset(i2c);
i2c->adap = netup_i2c_adapter;
i2c->adap.dev.parent = &ndev->pci_dev->dev;
i2c_set_adapdata(&i2c->adap, i2c);
ret = i2c_add_adapter(&i2c->adap);
if (ret)
return ret;
dev_info(&ndev->pci_dev->dev,
"%s(): registered I2C bus %d at 0x%x\n",
__func__,
bus_num, (bus_num == 0 ?
NETUP_I2C_BUS0_ADDR :
NETUP_I2C_BUS1_ADDR));
return 0;
}
static void netup_i2c_remove(struct netup_unidvb_dev *ndev, int bus_num)
{
struct netup_i2c *i2c;
if (bus_num < 0 || bus_num > 1) {
dev_err(&ndev->pci_dev->dev,
"%s(): invalid bus number %d\n", __func__, bus_num);
return;
}
i2c = &ndev->i2c[bus_num];
netup_i2c_reset(i2c);
/* remove adapter */
i2c_del_adapter(&i2c->adap);
dev_info(&ndev->pci_dev->dev,
"netup_i2c_remove: unregistered I2C bus %d\n", bus_num);
}
int netup_i2c_register(struct netup_unidvb_dev *ndev)
{
int ret;
ret = netup_i2c_init(ndev, 0);
if (ret)
return ret;
ret = netup_i2c_init(ndev, 1);
if (ret) {
netup_i2c_remove(ndev, 0);
return ret;
}
return 0;
}
void netup_i2c_unregister(struct netup_unidvb_dev *ndev)
{
netup_i2c_remove(ndev, 0);
netup_i2c_remove(ndev, 1);
}
| linux-master | drivers/media/pci/netup_unidvb/netup_unidvb_i2c.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* tw68_risc.c
* Part of the device driver for Techwell 68xx based cards
*
* Much of this code is derived from the cx88 and sa7134 drivers, which
* were in turn derived from the bt87x driver. The original work was by
* Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
* Hans Verkuil, Andy Walls and many others. Their work is gratefully
* acknowledged. Full credit goes to them - any problems within this code
* are mine.
*
* Copyright (C) 2009 William M. Brack
*
* Refactored and updated to the latest v4l core frameworks:
*
* Copyright (C) 2014 Hans Verkuil <[email protected]>
*/
#include "tw68.h"
/**
* tw68_risc_field
* @rp: pointer to current risc program position
* @sglist: pointer to "scatter-gather list" of buffer pointers
* @offset: offset to target memory buffer
* @sync_line: 0 -> no sync, 1 -> odd sync, 2 -> even sync
* @bpl: number of bytes per scan line
* @padding: number of bytes of padding to add
* @lines: number of lines in field
* @jump: insert a jump at the start
*/
static __le32 *tw68_risc_field(__le32 *rp, struct scatterlist *sglist,
unsigned int offset, u32 sync_line,
unsigned int bpl, unsigned int padding,
unsigned int lines, bool jump)
{
struct scatterlist *sg;
unsigned int line, todo, done;
if (jump) {
*(rp++) = cpu_to_le32(RISC_JUMP);
*(rp++) = 0;
}
/* sync instruction */
if (sync_line == 1)
*(rp++) = cpu_to_le32(RISC_SYNCO);
else
*(rp++) = cpu_to_le32(RISC_SYNCE);
*(rp++) = 0;
/* scan lines */
sg = sglist;
for (line = 0; line < lines; line++) {
/* calculate next starting position */
while (offset && offset >= sg_dma_len(sg)) {
offset -= sg_dma_len(sg);
sg = sg_next(sg);
}
if (bpl <= sg_dma_len(sg) - offset) {
/* fits into current chunk */
*(rp++) = cpu_to_le32(RISC_LINESTART |
/* (offset<<12) |*/ bpl);
*(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
offset += bpl;
} else {
/*
* scanline needs to be split. Put the start in
* whatever memory remains using RISC_LINESTART,
* then the remainder into following addresses
* given by the scatter-gather list.
*/
todo = bpl; /* one full line to be done */
/* first fragment */
done = (sg_dma_len(sg) - offset);
*(rp++) = cpu_to_le32(RISC_LINESTART |
(7 << 24) |
done);
*(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
todo -= done;
sg = sg_next(sg);
/* succeeding fragments have no offset */
while (todo > sg_dma_len(sg)) {
*(rp++) = cpu_to_le32(RISC_INLINE |
(done << 12) |
sg_dma_len(sg));
*(rp++) = cpu_to_le32(sg_dma_address(sg));
todo -= sg_dma_len(sg);
sg = sg_next(sg);
done += sg_dma_len(sg);
}
if (todo) {
/* final chunk - offset 0, count 'todo' */
*(rp++) = cpu_to_le32(RISC_INLINE |
(done << 12) |
todo);
*(rp++) = cpu_to_le32(sg_dma_address(sg));
}
offset = todo;
}
offset += padding;
}
return rp;
}
/**
* tw68_risc_buffer
*
* This routine is called by tw68-video. It allocates
* memory for the dma controller "program" and then fills in that
* memory with the appropriate "instructions".
*
* @pci: structure with info about the pci
* slot which our device is in.
* @buf: structure with info about the memory
* used for our controller program.
* @sglist: scatter-gather list entry
* @top_offset: offset within the risc program area for the
* first odd frame line
* @bottom_offset: offset within the risc program area for the
* first even frame line
* @bpl: number of data bytes per scan line
* @padding: number of extra bytes to add at end of line
* @lines: number of scan lines
*/
int tw68_risc_buffer(struct pci_dev *pci,
struct tw68_buf *buf,
struct scatterlist *sglist,
unsigned int top_offset,
unsigned int bottom_offset,
unsigned int bpl,
unsigned int padding,
unsigned int lines)
{
u32 instructions, fields;
__le32 *rp;
fields = 0;
if (UNSET != top_offset)
fields++;
if (UNSET != bottom_offset)
fields++;
/*
* estimate risc mem: worst case is one write per page border +
* one write per scan line + syncs + 2 jumps (all 2 dwords).
* Padding can cause next bpl to start close to a page border.
* First DMA region may be smaller than PAGE_SIZE
*/
instructions = fields * (1 + (((bpl + padding) * lines) /
PAGE_SIZE) + lines) + 4;
buf->size = instructions * 8;
buf->cpu = dma_alloc_coherent(&pci->dev, buf->size, &buf->dma,
GFP_KERNEL);
if (buf->cpu == NULL)
return -ENOMEM;
/* write risc instructions */
rp = buf->cpu;
if (UNSET != top_offset) /* generates SYNCO */
rp = tw68_risc_field(rp, sglist, top_offset, 1,
bpl, padding, lines, true);
if (UNSET != bottom_offset) /* generates SYNCE */
rp = tw68_risc_field(rp, sglist, bottom_offset, 2,
bpl, padding, lines, top_offset == UNSET);
/* save pointer to jmp instruction address */
buf->jmp = rp;
buf->cpu[1] = cpu_to_le32(buf->dma + 8);
/* assure risc buffer hasn't overflowed */
BUG_ON((buf->jmp - buf->cpu + 2) * sizeof(buf->cpu[0]) > buf->size);
return 0;
}
#if 0
/* ------------------------------------------------------------------ */
/* debug helper code */
static void tw68_risc_decode(u32 risc, u32 addr)
{
#define RISC_OP(reg) (((reg) >> 28) & 7)
static struct instr_details {
char *name;
u8 has_data_type;
u8 has_byte_info;
u8 has_addr;
} instr[8] = {
[RISC_OP(RISC_SYNCO)] = {"syncOdd", 0, 0, 0},
[RISC_OP(RISC_SYNCE)] = {"syncEven", 0, 0, 0},
[RISC_OP(RISC_JUMP)] = {"jump", 0, 0, 1},
[RISC_OP(RISC_LINESTART)] = {"lineStart", 1, 1, 1},
[RISC_OP(RISC_INLINE)] = {"inline", 1, 1, 1},
};
u32 p;
p = RISC_OP(risc);
if (!(risc & 0x80000000) || !instr[p].name) {
pr_debug("0x%08x [ INVALID ]\n", risc);
return;
}
pr_debug("0x%08x %-9s IRQ=%d",
risc, instr[p].name, (risc >> 27) & 1);
if (instr[p].has_data_type)
pr_debug(" Type=%d", (risc >> 24) & 7);
if (instr[p].has_byte_info)
pr_debug(" Start=0x%03x Count=%03u",
(risc >> 12) & 0xfff, risc & 0xfff);
if (instr[p].has_addr)
pr_debug(" StartAddr=0x%08x", addr);
pr_debug("\n");
}
void tw68_risc_program_dump(struct tw68_core *core, struct tw68_buf *buf)
{
const __le32 *addr;
pr_debug("%s: risc_program_dump: risc=%p, buf->cpu=0x%p, buf->jmp=0x%p\n",
core->name, buf, buf->cpu, buf->jmp);
for (addr = buf->cpu; addr <= buf->jmp; addr += 2)
tw68_risc_decode(*addr, *(addr+1));
}
#endif
| linux-master | drivers/media/pci/tw68/tw68-risc.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* tw68-core.c
* Core functions for the Techwell 68xx driver
*
* Much of this code is derived from the cx88 and sa7134 drivers, which
* were in turn derived from the bt87x driver. The original work was by
* Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
* Hans Verkuil, Andy Walls and many others. Their work is gratefully
* acknowledged. Full credit goes to them - any problems within this code
* are mine.
*
* Copyright (C) 2009 William M. Brack
*
* Refactored and updated to the latest v4l core frameworks:
*
* Copyright (C) 2014 Hans Verkuil <[email protected]>
*/
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/kmod.h>
#include <linux/sound.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/dma-mapping.h>
#include <linux/pci_ids.h>
#include <linux/pm.h>
#include <media/v4l2-dev.h>
#include "tw68.h"
#include "tw68-reg.h"
MODULE_DESCRIPTION("v4l2 driver module for tw6800 based video capture cards");
MODULE_AUTHOR("William M. Brack");
MODULE_AUTHOR("Hans Verkuil <[email protected]>");
MODULE_LICENSE("GPL");
static unsigned int latency = UNSET;
module_param(latency, int, 0444);
MODULE_PARM_DESC(latency, "pci latency timer");
static unsigned int video_nr[] = {[0 ... (TW68_MAXBOARDS - 1)] = UNSET };
module_param_array(video_nr, int, NULL, 0444);
MODULE_PARM_DESC(video_nr, "video device number");
static unsigned int card[] = {[0 ... (TW68_MAXBOARDS - 1)] = UNSET };
module_param_array(card, int, NULL, 0444);
MODULE_PARM_DESC(card, "card type");
static atomic_t tw68_instance = ATOMIC_INIT(0);
/* ------------------------------------------------------------------ */
/*
* Please add any new PCI IDs to: https://pci-ids.ucw.cz. This keeps
* the PCI ID database up to date. Note that the entries must be
* added under vendor 0x1797 (Techwell Inc.) as subsystem IDs.
*/
static const struct pci_device_id tw68_pci_tbl[] = {
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6800)},
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6801)},
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6804)},
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6816_1)},
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6816_2)},
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6816_3)},
{PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_6816_4)},
{0,}
};
/* ------------------------------------------------------------------ */
/*
* The device is given a "soft reset". According to the specifications,
* after this "all register content remain unchanged", so we also write
* to all specified registers manually as well (mostly to manufacturer's
* specified reset values)
*/
static int tw68_hw_init1(struct tw68_dev *dev)
{
/* Assure all interrupts are disabled */
tw_writel(TW68_INTMASK, 0); /* 020 */
/* Clear any pending interrupts */
tw_writel(TW68_INTSTAT, 0xffffffff); /* 01C */
/* Stop risc processor, set default buffer level */
tw_writel(TW68_DMAC, 0x1600);
tw_writeb(TW68_ACNTL, 0x80); /* 218 soft reset */
msleep(100);
tw_writeb(TW68_INFORM, 0x40); /* 208 mux0, 27mhz xtal */
tw_writeb(TW68_OPFORM, 0x04); /* 20C analog line-lock */
tw_writeb(TW68_HSYNC, 0); /* 210 color-killer high sens */
tw_writeb(TW68_ACNTL, 0x42); /* 218 int vref #2, chroma adc off */
tw_writeb(TW68_CROP_HI, 0x02); /* 21C Hactive m.s. bits */
tw_writeb(TW68_VDELAY_LO, 0x12);/* 220 Mfg specified reset value */
tw_writeb(TW68_VACTIVE_LO, 0xf0);
tw_writeb(TW68_HDELAY_LO, 0x0f);
tw_writeb(TW68_HACTIVE_LO, 0xd0);
tw_writeb(TW68_CNTRL1, 0xcd); /* 230 Wide Chroma BPF B/W
* Secam reduction, Adap comb for
* NTSC, Op Mode 1 */
tw_writeb(TW68_VSCALE_LO, 0); /* 234 */
tw_writeb(TW68_SCALE_HI, 0x11); /* 238 */
tw_writeb(TW68_HSCALE_LO, 0); /* 23c */
tw_writeb(TW68_BRIGHT, 0); /* 240 */
tw_writeb(TW68_CONTRAST, 0x5c); /* 244 */
tw_writeb(TW68_SHARPNESS, 0x51);/* 248 */
tw_writeb(TW68_SAT_U, 0x80); /* 24C */
tw_writeb(TW68_SAT_V, 0x80); /* 250 */
tw_writeb(TW68_HUE, 0x00); /* 254 */
/* TODO - Check that none of these are set by control defaults */
tw_writeb(TW68_SHARP2, 0x53); /* 258 Mfg specified reset val */
tw_writeb(TW68_VSHARP, 0x80); /* 25C Sharpness Coring val 8 */
tw_writeb(TW68_CORING, 0x44); /* 260 CTI and Vert Peak coring */
tw_writeb(TW68_CNTRL2, 0x00); /* 268 No power saving enabled */
tw_writeb(TW68_SDT, 0x07); /* 270 Enable shadow reg, auto-det */
tw_writeb(TW68_SDTR, 0x7f); /* 274 All stds recog, don't start */
tw_writeb(TW68_CLMPG, 0x50); /* 280 Clamp end at 40 sys clocks */
tw_writeb(TW68_IAGC, 0x22); /* 284 Mfg specified reset val */
tw_writeb(TW68_AGCGAIN, 0xf0); /* 288 AGC gain when loop disabled */
tw_writeb(TW68_PEAKWT, 0xd8); /* 28C White peak threshold */
tw_writeb(TW68_CLMPL, 0x3c); /* 290 Y channel clamp level */
/* tw_writeb(TW68_SYNCT, 0x38);*/ /* 294 Sync amplitude */
tw_writeb(TW68_SYNCT, 0x30); /* 294 Sync amplitude */
tw_writeb(TW68_MISSCNT, 0x44); /* 298 Horiz sync, VCR detect sens */
tw_writeb(TW68_PCLAMP, 0x28); /* 29C Clamp pos from PLL sync */
/* Bit DETV of VCNTL1 helps sync multi cams/chip board */
tw_writeb(TW68_VCNTL1, 0x04); /* 2A0 */
tw_writeb(TW68_VCNTL2, 0); /* 2A4 */
tw_writeb(TW68_CKILL, 0x68); /* 2A8 Mfg specified reset val */
tw_writeb(TW68_COMB, 0x44); /* 2AC Mfg specified reset val */
tw_writeb(TW68_LDLY, 0x30); /* 2B0 Max positive luma delay */
tw_writeb(TW68_MISC1, 0x14); /* 2B4 Mfg specified reset val */
tw_writeb(TW68_LOOP, 0xa5); /* 2B8 Mfg specified reset val */
tw_writeb(TW68_MISC2, 0xe0); /* 2BC Enable colour killer */
tw_writeb(TW68_MVSN, 0); /* 2C0 */
tw_writeb(TW68_CLMD, 0x05); /* 2CC slice level auto, clamp med. */
tw_writeb(TW68_IDCNTL, 0); /* 2D0 Writing zero to this register
* selects NTSC ID detection,
* but doesn't change the
* sensitivity (which has a reset
* value of 1E). Since we are
* not doing auto-detection, it
* has no real effect */
tw_writeb(TW68_CLCNTL1, 0); /* 2D4 */
tw_writel(TW68_VBIC, 0x03); /* 010 */
tw_writel(TW68_CAP_CTL, 0x03); /* 040 Enable both even & odd flds */
tw_writel(TW68_DMAC, 0x2000); /* patch set had 0x2080 */
tw_writel(TW68_TESTREG, 0); /* 02C */
/*
* Some common boards, especially inexpensive single-chip models,
* use the GPIO bits 0-3 to control an on-board video-output mux.
* For these boards, we need to set up the GPIO register into
* "normal" mode, set bits 0-3 as output, and then set those bits
* zero.
*
* Eventually, it would be nice if we could identify these boards
* uniquely, and only do this initialisation if the board has been
* identify. For the moment, however, it shouldn't hurt anything
* to do these steps.
*/
tw_writel(TW68_GPIOC, 0); /* Set the GPIO to "normal", no ints */
tw_writel(TW68_GPOE, 0x0f); /* Set bits 0-3 to "output" */
tw_writel(TW68_GPDATA, 0); /* Set all bits to low state */
/* Initialize the device control structures */
mutex_init(&dev->lock);
spin_lock_init(&dev->slock);
/* Initialize any subsystems */
tw68_video_init1(dev);
return 0;
}
static irqreturn_t tw68_irq(int irq, void *dev_id)
{
struct tw68_dev *dev = dev_id;
u32 status, orig;
int loop;
status = orig = tw_readl(TW68_INTSTAT) & dev->pci_irqmask;
/* Check if anything to do */
if (0 == status)
return IRQ_NONE; /* Nope - return */
for (loop = 0; loop < 10; loop++) {
if (status & dev->board_virqmask) /* video interrupt */
tw68_irq_video_done(dev, status);
status = tw_readl(TW68_INTSTAT) & dev->pci_irqmask;
if (0 == status)
return IRQ_HANDLED;
}
dev_dbg(&dev->pci->dev, "%s: **** INTERRUPT NOT HANDLED - clearing mask (orig 0x%08x, cur 0x%08x)",
dev->name, orig, tw_readl(TW68_INTSTAT));
dev_dbg(&dev->pci->dev, "%s: pci_irqmask 0x%08x; board_virqmask 0x%08x ****\n",
dev->name, dev->pci_irqmask, dev->board_virqmask);
tw_clearl(TW68_INTMASK, dev->pci_irqmask);
return IRQ_HANDLED;
}
static int tw68_initdev(struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
struct tw68_dev *dev;
int vidnr = -1;
int err;
dev = devm_kzalloc(&pci_dev->dev, sizeof(*dev), GFP_KERNEL);
if (NULL == dev)
return -ENOMEM;
dev->instance = v4l2_device_set_name(&dev->v4l2_dev, "tw68",
&tw68_instance);
err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
if (err)
return err;
/* pci init */
dev->pci = pci_dev;
if (pci_enable_device(pci_dev)) {
err = -EIO;
goto fail1;
}
dev->name = dev->v4l2_dev.name;
if (UNSET != latency) {
pr_info("%s: setting pci latency timer to %d\n",
dev->name, latency);
pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
}
/* print pci info */
pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
pr_info("%s: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
dev->pci_lat, (u64)pci_resource_start(pci_dev, 0));
pci_set_master(pci_dev);
err = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
if (err) {
pr_info("%s: Oops: no 32bit PCI DMA ???\n", dev->name);
goto fail1;
}
switch (pci_id->device) {
case PCI_DEVICE_ID_TECHWELL_6800: /* TW6800 */
dev->vdecoder = TW6800;
dev->board_virqmask = TW68_VID_INTS;
break;
case PCI_DEVICE_ID_TECHWELL_6801: /* Video decoder for TW6802 */
dev->vdecoder = TW6801;
dev->board_virqmask = TW68_VID_INTS | TW68_VID_INTSX;
break;
case PCI_DEVICE_ID_TECHWELL_6804: /* Video decoder for TW6804 */
dev->vdecoder = TW6804;
dev->board_virqmask = TW68_VID_INTS | TW68_VID_INTSX;
break;
default:
dev->vdecoder = TWXXXX; /* To be announced */
dev->board_virqmask = TW68_VID_INTS | TW68_VID_INTSX;
break;
}
/* get mmio */
if (!request_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0),
dev->name)) {
err = -EBUSY;
pr_err("%s: can't get MMIO memory @ 0x%llx\n",
dev->name,
(unsigned long long)pci_resource_start(pci_dev, 0));
goto fail1;
}
dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
dev->bmmio = (__u8 __iomem *)dev->lmmio;
if (NULL == dev->lmmio) {
err = -EIO;
pr_err("%s: can't ioremap() MMIO memory\n",
dev->name);
goto fail2;
}
/* initialize hardware #1 */
/* Then do any initialisation wanted before interrupts are on */
tw68_hw_init1(dev);
/* get irq */
err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw68_irq,
IRQF_SHARED, dev->name, dev);
if (err < 0) {
pr_err("%s: can't get IRQ %d\n",
dev->name, pci_dev->irq);
goto fail3;
}
/*
* Now do remainder of initialisation, first for
* things unique for this card, then for general board
*/
if (dev->instance < TW68_MAXBOARDS)
vidnr = video_nr[dev->instance];
/* initialise video function first */
err = tw68_video_init2(dev, vidnr);
if (err < 0) {
pr_err("%s: can't register video device\n",
dev->name);
goto fail4;
}
tw_setl(TW68_INTMASK, dev->pci_irqmask);
pr_info("%s: registered device %s\n",
dev->name, video_device_node_name(&dev->vdev));
return 0;
fail4:
video_unregister_device(&dev->vdev);
fail3:
iounmap(dev->lmmio);
fail2:
release_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
fail1:
v4l2_device_unregister(&dev->v4l2_dev);
return err;
}
static void tw68_finidev(struct pci_dev *pci_dev)
{
struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
struct tw68_dev *dev =
container_of(v4l2_dev, struct tw68_dev, v4l2_dev);
/* shutdown subsystems */
tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
tw_writel(TW68_INTMASK, 0);
/* unregister */
video_unregister_device(&dev->vdev);
v4l2_ctrl_handler_free(&dev->hdl);
/* release resources */
iounmap(dev->lmmio);
release_mem_region(pci_resource_start(pci_dev, 0),
pci_resource_len(pci_dev, 0));
v4l2_device_unregister(&dev->v4l2_dev);
}
static int __maybe_unused tw68_suspend(struct device *dev_d)
{
struct pci_dev *pci_dev = to_pci_dev(dev_d);
struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
struct tw68_dev *dev = container_of(v4l2_dev,
struct tw68_dev, v4l2_dev);
tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
dev->pci_irqmask &= ~TW68_VID_INTS;
tw_writel(TW68_INTMASK, 0);
synchronize_irq(pci_dev->irq);
vb2_discard_done(&dev->vidq);
return 0;
}
static int __maybe_unused tw68_resume(struct device *dev_d)
{
struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
struct tw68_dev *dev = container_of(v4l2_dev,
struct tw68_dev, v4l2_dev);
struct tw68_buf *buf;
unsigned long flags;
/* Do things that are done in tw68_initdev ,
except of initializing memory structures.*/
msleep(100);
tw68_set_tvnorm_hw(dev);
/*resume unfinished buffer(s)*/
spin_lock_irqsave(&dev->slock, flags);
buf = container_of(dev->active.next, struct tw68_buf, list);
tw68_video_start_dma(dev, buf);
spin_unlock_irqrestore(&dev->slock, flags);
return 0;
}
/* ----------------------------------------------------------- */
static SIMPLE_DEV_PM_OPS(tw68_pm_ops, tw68_suspend, tw68_resume);
static struct pci_driver tw68_pci_driver = {
.name = "tw68",
.id_table = tw68_pci_tbl,
.probe = tw68_initdev,
.remove = tw68_finidev,
.driver.pm = &tw68_pm_ops,
};
module_pci_driver(tw68_pci_driver);
| linux-master | drivers/media/pci/tw68/tw68-core.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* tw68 functions to handle video data
*
* Much of this code is derived from the cx88 and sa7134 drivers, which
* were in turn derived from the bt87x driver. The original work was by
* Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
* Hans Verkuil, Andy Walls and many others. Their work is gratefully
* acknowledged. Full credit goes to them - any problems within this code
* are mine.
*
* Copyright (C) 2009 William M. Brack
*
* Refactored and updated to the latest v4l core frameworks:
*
* Copyright (C) 2014 Hans Verkuil <[email protected]>
*/
#include <linux/module.h>
#include <media/v4l2-common.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-dma-sg.h>
#include "tw68.h"
#include "tw68-reg.h"
/* ------------------------------------------------------------------ */
/* data structs for video */
/*
* FIXME -
* Note that the saa7134 has formats, e.g. YUV420, which are classified
* as "planar". These affect overlay mode, and are flagged with a field
* ".planar" in the format. Do we need to implement this in this driver?
*/
static const struct tw68_format formats[] = {
{
.fourcc = V4L2_PIX_FMT_RGB555,
.depth = 16,
.twformat = ColorFormatRGB15,
}, {
.fourcc = V4L2_PIX_FMT_RGB555X,
.depth = 16,
.twformat = ColorFormatRGB15 | ColorFormatBSWAP,
}, {
.fourcc = V4L2_PIX_FMT_RGB565,
.depth = 16,
.twformat = ColorFormatRGB16,
}, {
.fourcc = V4L2_PIX_FMT_RGB565X,
.depth = 16,
.twformat = ColorFormatRGB16 | ColorFormatBSWAP,
}, {
.fourcc = V4L2_PIX_FMT_BGR24,
.depth = 24,
.twformat = ColorFormatRGB24,
}, {
.fourcc = V4L2_PIX_FMT_RGB24,
.depth = 24,
.twformat = ColorFormatRGB24 | ColorFormatBSWAP,
}, {
.fourcc = V4L2_PIX_FMT_BGR32,
.depth = 32,
.twformat = ColorFormatRGB32,
}, {
.fourcc = V4L2_PIX_FMT_RGB32,
.depth = 32,
.twformat = ColorFormatRGB32 | ColorFormatBSWAP |
ColorFormatWSWAP,
}, {
.fourcc = V4L2_PIX_FMT_YUYV,
.depth = 16,
.twformat = ColorFormatYUY2,
}, {
.fourcc = V4L2_PIX_FMT_UYVY,
.depth = 16,
.twformat = ColorFormatYUY2 | ColorFormatBSWAP,
}
};
#define FORMATS ARRAY_SIZE(formats)
#define NORM_625_50 \
.h_delay = 3, \
.h_delay0 = 133, \
.h_start = 0, \
.h_stop = 719, \
.v_delay = 24, \
.vbi_v_start_0 = 7, \
.vbi_v_stop_0 = 22, \
.video_v_start = 24, \
.video_v_stop = 311, \
.vbi_v_start_1 = 319
#define NORM_525_60 \
.h_delay = 8, \
.h_delay0 = 138, \
.h_start = 0, \
.h_stop = 719, \
.v_delay = 22, \
.vbi_v_start_0 = 10, \
.vbi_v_stop_0 = 21, \
.video_v_start = 22, \
.video_v_stop = 262, \
.vbi_v_start_1 = 273
/*
* The following table is searched by tw68_s_std, first for a specific
* match, then for an entry which contains the desired id. The table
* entries should therefore be ordered in ascending order of specificity.
*/
static const struct tw68_tvnorm tvnorms[] = {
{
.name = "PAL", /* autodetect */
.id = V4L2_STD_PAL,
NORM_625_50,
.sync_control = 0x18,
.luma_control = 0x40,
.chroma_ctrl1 = 0x81,
.chroma_gain = 0x2a,
.chroma_ctrl2 = 0x06,
.vgate_misc = 0x1c,
.format = VideoFormatPALBDGHI,
}, {
.name = "NTSC",
.id = V4L2_STD_NTSC,
NORM_525_60,
.sync_control = 0x59,
.luma_control = 0x40,
.chroma_ctrl1 = 0x89,
.chroma_gain = 0x2a,
.chroma_ctrl2 = 0x0e,
.vgate_misc = 0x18,
.format = VideoFormatNTSC,
}, {
.name = "SECAM",
.id = V4L2_STD_SECAM,
NORM_625_50,
.sync_control = 0x18,
.luma_control = 0x1b,
.chroma_ctrl1 = 0xd1,
.chroma_gain = 0x80,
.chroma_ctrl2 = 0x00,
.vgate_misc = 0x1c,
.format = VideoFormatSECAM,
}, {
.name = "PAL-M",
.id = V4L2_STD_PAL_M,
NORM_525_60,
.sync_control = 0x59,
.luma_control = 0x40,
.chroma_ctrl1 = 0xb9,
.chroma_gain = 0x2a,
.chroma_ctrl2 = 0x0e,
.vgate_misc = 0x18,
.format = VideoFormatPALM,
}, {
.name = "PAL-Nc",
.id = V4L2_STD_PAL_Nc,
NORM_625_50,
.sync_control = 0x18,
.luma_control = 0x40,
.chroma_ctrl1 = 0xa1,
.chroma_gain = 0x2a,
.chroma_ctrl2 = 0x06,
.vgate_misc = 0x1c,
.format = VideoFormatPALNC,
}, {
.name = "PAL-60",
.id = V4L2_STD_PAL_60,
.h_delay = 186,
.h_start = 0,
.h_stop = 719,
.v_delay = 26,
.video_v_start = 23,
.video_v_stop = 262,
.vbi_v_start_0 = 10,
.vbi_v_stop_0 = 21,
.vbi_v_start_1 = 273,
.sync_control = 0x18,
.luma_control = 0x40,
.chroma_ctrl1 = 0x81,
.chroma_gain = 0x2a,
.chroma_ctrl2 = 0x06,
.vgate_misc = 0x1c,
.format = VideoFormatPAL60,
}
};
#define TVNORMS ARRAY_SIZE(tvnorms)
static const struct tw68_format *format_by_fourcc(unsigned int fourcc)
{
unsigned int i;
for (i = 0; i < FORMATS; i++)
if (formats[i].fourcc == fourcc)
return formats+i;
return NULL;
}
/* ------------------------------------------------------------------ */
/*
* Note that the cropping rectangles are described in terms of a single
* frame, i.e. line positions are only 1/2 the interlaced equivalent
*/
static void set_tvnorm(struct tw68_dev *dev, const struct tw68_tvnorm *norm)
{
if (norm != dev->tvnorm) {
dev->width = 720;
dev->height = (norm->id & V4L2_STD_525_60) ? 480 : 576;
dev->tvnorm = norm;
tw68_set_tvnorm_hw(dev);
}
}
/*
* tw68_set_scale
*
* Scaling and Cropping for video decoding
*
* We are working with 3 values for horizontal and vertical - scale,
* delay and active.
*
* HACTIVE represent the actual number of pixels in the "usable" image,
* before scaling. HDELAY represents the number of pixels skipped
* between the start of the horizontal sync and the start of the image.
* HSCALE is calculated using the formula
* HSCALE = (HACTIVE / (#pixels desired)) * 256
*
* The vertical registers are similar, except based upon the total number
* of lines in the image, and the first line of the image (i.e. ignoring
* vertical sync and VBI).
*
* Note that the number of bytes reaching the FIFO (and hence needing
* to be processed by the DMAP program) is completely dependent upon
* these values, especially HSCALE.
*
* Parameters:
* @dev pointer to the device structure, needed for
* getting current norm (as well as debug print)
* @width actual image width (from user buffer)
* @height actual image height
* @field indicates Top, Bottom or Interlaced
*/
static int tw68_set_scale(struct tw68_dev *dev, unsigned int width,
unsigned int height, enum v4l2_field field)
{
const struct tw68_tvnorm *norm = dev->tvnorm;
/* set individually for debugging clarity */
int hactive, hdelay, hscale;
int vactive, vdelay, vscale;
int comb;
if (V4L2_FIELD_HAS_BOTH(field)) /* if field is interlaced */
height /= 2; /* we must set for 1-frame */
pr_debug("%s: width=%d, height=%d, both=%d\n"
" tvnorm h_delay=%d, h_start=%d, h_stop=%d, v_delay=%d, v_start=%d, v_stop=%d\n",
__func__, width, height, V4L2_FIELD_HAS_BOTH(field),
norm->h_delay, norm->h_start, norm->h_stop,
norm->v_delay, norm->video_v_start,
norm->video_v_stop);
switch (dev->vdecoder) {
case TW6800:
hdelay = norm->h_delay0;
break;
default:
hdelay = norm->h_delay;
break;
}
hdelay += norm->h_start;
hactive = norm->h_stop - norm->h_start + 1;
hscale = (hactive * 256) / (width);
vdelay = norm->v_delay;
vactive = ((norm->id & V4L2_STD_525_60) ? 524 : 624) / 2 - norm->video_v_start;
vscale = (vactive * 256) / height;
pr_debug("%s: %dx%d [%s%s,%s]\n", __func__,
width, height,
V4L2_FIELD_HAS_TOP(field) ? "T" : "",
V4L2_FIELD_HAS_BOTTOM(field) ? "B" : "",
v4l2_norm_to_name(dev->tvnorm->id));
pr_debug("%s: hactive=%d, hdelay=%d, hscale=%d; vactive=%d, vdelay=%d, vscale=%d\n",
__func__,
hactive, hdelay, hscale, vactive, vdelay, vscale);
comb = ((vdelay & 0x300) >> 2) |
((vactive & 0x300) >> 4) |
((hdelay & 0x300) >> 6) |
((hactive & 0x300) >> 8);
pr_debug("%s: setting CROP_HI=%02x, VDELAY_LO=%02x, VACTIVE_LO=%02x, HDELAY_LO=%02x, HACTIVE_LO=%02x\n",
__func__, comb, vdelay, vactive, hdelay, hactive);
tw_writeb(TW68_CROP_HI, comb);
tw_writeb(TW68_VDELAY_LO, vdelay & 0xff);
tw_writeb(TW68_VACTIVE_LO, vactive & 0xff);
tw_writeb(TW68_HDELAY_LO, hdelay & 0xff);
tw_writeb(TW68_HACTIVE_LO, hactive & 0xff);
comb = ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8);
pr_debug("%s: setting SCALE_HI=%02x, VSCALE_LO=%02x, HSCALE_LO=%02x\n",
__func__, comb, vscale, hscale);
tw_writeb(TW68_SCALE_HI, comb);
tw_writeb(TW68_VSCALE_LO, vscale);
tw_writeb(TW68_HSCALE_LO, hscale);
return 0;
}
/* ------------------------------------------------------------------ */
int tw68_video_start_dma(struct tw68_dev *dev, struct tw68_buf *buf)
{
/* Set cropping and scaling */
tw68_set_scale(dev, dev->width, dev->height, dev->field);
/*
* Set start address for RISC program. Note that if the DMAP
* processor is currently running, it must be stopped before
* a new address can be set.
*/
tw_clearl(TW68_DMAC, TW68_DMAP_EN);
tw_writel(TW68_DMAP_SA, buf->dma);
/* Clear any pending interrupts */
tw_writel(TW68_INTSTAT, dev->board_virqmask);
/* Enable the risc engine and the fifo */
tw_andorl(TW68_DMAC, 0xff, dev->fmt->twformat |
ColorFormatGamma | TW68_DMAP_EN | TW68_FIFO_EN);
dev->pci_irqmask |= dev->board_virqmask;
tw_setl(TW68_INTMASK, dev->pci_irqmask);
return 0;
}
/* ------------------------------------------------------------------ */
/* calc max # of buffers from size (must not exceed the 4MB virtual
* address space per DMA channel) */
static int tw68_buffer_count(unsigned int size, unsigned int count)
{
unsigned int maxcount;
maxcount = (4 * 1024 * 1024) / roundup(size, PAGE_SIZE);
if (count > maxcount)
count = maxcount;
return count;
}
/* ------------------------------------------------------------- */
/* vb2 queue operations */
static int tw68_queue_setup(struct vb2_queue *q,
unsigned int *num_buffers, unsigned int *num_planes,
unsigned int sizes[], struct device *alloc_devs[])
{
struct tw68_dev *dev = vb2_get_drv_priv(q);
unsigned tot_bufs = q->num_buffers + *num_buffers;
unsigned size = (dev->fmt->depth * dev->width * dev->height) >> 3;
if (tot_bufs < 2)
tot_bufs = 2;
tot_bufs = tw68_buffer_count(size, tot_bufs);
*num_buffers = tot_bufs - q->num_buffers;
/*
* We allow create_bufs, but only if the sizeimage is >= as the
* current sizeimage. The tw68_buffer_count calculation becomes quite
* difficult otherwise.
*/
if (*num_planes)
return sizes[0] < size ? -EINVAL : 0;
*num_planes = 1;
sizes[0] = size;
return 0;
}
/*
* The risc program for each buffers works as follows: it starts with a simple
* 'JUMP to addr + 8', which is effectively a NOP. Then the program to DMA the
* buffer follows and at the end we have a JUMP back to the start + 8 (skipping
* the initial JUMP).
*
* This is the program of the first buffer to be queued if the active list is
* empty and it just keeps DMAing this buffer without generating any interrupts.
*
* If a new buffer is added then the initial JUMP in the program generates an
* interrupt as well which signals that the previous buffer has been DMAed
* successfully and that it can be returned to userspace.
*
* It also sets the final jump of the previous buffer to the start of the new
* buffer, thus chaining the new buffer into the DMA chain. This is a single
* atomic u32 write, so there is no race condition.
*
* The end-result of all this that you only get an interrupt when a buffer
* is ready, so the control flow is very easy.
*/
static void tw68_buf_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct tw68_dev *dev = vb2_get_drv_priv(vq);
struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
struct tw68_buf *prev;
unsigned long flags;
spin_lock_irqsave(&dev->slock, flags);
/* append a 'JUMP to start of buffer' to the buffer risc program */
buf->jmp[0] = cpu_to_le32(RISC_JUMP);
buf->jmp[1] = cpu_to_le32(buf->dma + 8);
if (!list_empty(&dev->active)) {
prev = list_entry(dev->active.prev, struct tw68_buf, list);
buf->cpu[0] |= cpu_to_le32(RISC_INT_BIT);
prev->jmp[1] = cpu_to_le32(buf->dma);
}
list_add_tail(&buf->list, &dev->active);
spin_unlock_irqrestore(&dev->slock, flags);
}
/*
* buffer_prepare
*
* Set the ancillary information into the buffer structure. This
* includes generating the necessary risc program if it hasn't already
* been done for the current buffer format.
* The structure fh contains the details of the format requested by the
* user - type, width, height and #fields. This is compared with the
* last format set for the current buffer. If they differ, the risc
* code (which controls the filling of the buffer) is (re-)generated.
*/
static int tw68_buf_prepare(struct vb2_buffer *vb)
{
int ret;
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct tw68_dev *dev = vb2_get_drv_priv(vq);
struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
struct sg_table *dma = vb2_dma_sg_plane_desc(vb, 0);
unsigned size, bpl;
size = (dev->width * dev->height * dev->fmt->depth) >> 3;
if (vb2_plane_size(vb, 0) < size)
return -EINVAL;
vb2_set_plane_payload(vb, 0, size);
bpl = (dev->width * dev->fmt->depth) >> 3;
switch (dev->field) {
case V4L2_FIELD_TOP:
ret = tw68_risc_buffer(dev->pci, buf, dma->sgl,
0, UNSET, bpl, 0, dev->height);
break;
case V4L2_FIELD_BOTTOM:
ret = tw68_risc_buffer(dev->pci, buf, dma->sgl,
UNSET, 0, bpl, 0, dev->height);
break;
case V4L2_FIELD_SEQ_TB:
ret = tw68_risc_buffer(dev->pci, buf, dma->sgl,
0, bpl * (dev->height >> 1),
bpl, 0, dev->height >> 1);
break;
case V4L2_FIELD_SEQ_BT:
ret = tw68_risc_buffer(dev->pci, buf, dma->sgl,
bpl * (dev->height >> 1), 0,
bpl, 0, dev->height >> 1);
break;
case V4L2_FIELD_INTERLACED:
default:
ret = tw68_risc_buffer(dev->pci, buf, dma->sgl,
0, bpl, bpl, bpl, dev->height >> 1);
break;
}
return ret;
}
static void tw68_buf_finish(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct tw68_dev *dev = vb2_get_drv_priv(vq);
struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
if (buf->cpu)
dma_free_coherent(&dev->pci->dev, buf->size, buf->cpu, buf->dma);
}
static int tw68_start_streaming(struct vb2_queue *q, unsigned int count)
{
struct tw68_dev *dev = vb2_get_drv_priv(q);
struct tw68_buf *buf =
container_of(dev->active.next, struct tw68_buf, list);
dev->seqnr = 0;
tw68_video_start_dma(dev, buf);
return 0;
}
static void tw68_stop_streaming(struct vb2_queue *q)
{
struct tw68_dev *dev = vb2_get_drv_priv(q);
/* Stop risc & fifo */
tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
while (!list_empty(&dev->active)) {
struct tw68_buf *buf =
container_of(dev->active.next, struct tw68_buf, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
}
static const struct vb2_ops tw68_video_qops = {
.queue_setup = tw68_queue_setup,
.buf_queue = tw68_buf_queue,
.buf_prepare = tw68_buf_prepare,
.buf_finish = tw68_buf_finish,
.start_streaming = tw68_start_streaming,
.stop_streaming = tw68_stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
/* ------------------------------------------------------------------ */
static int tw68_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct tw68_dev *dev =
container_of(ctrl->handler, struct tw68_dev, hdl);
switch (ctrl->id) {
case V4L2_CID_BRIGHTNESS:
tw_writeb(TW68_BRIGHT, ctrl->val);
break;
case V4L2_CID_HUE:
tw_writeb(TW68_HUE, ctrl->val);
break;
case V4L2_CID_CONTRAST:
tw_writeb(TW68_CONTRAST, ctrl->val);
break;
case V4L2_CID_SATURATION:
tw_writeb(TW68_SAT_U, ctrl->val);
tw_writeb(TW68_SAT_V, ctrl->val);
break;
case V4L2_CID_COLOR_KILLER:
if (ctrl->val)
tw_andorb(TW68_MISC2, 0xe0, 0xe0);
else
tw_andorb(TW68_MISC2, 0xe0, 0x00);
break;
case V4L2_CID_CHROMA_AGC:
if (ctrl->val)
tw_andorb(TW68_LOOP, 0x30, 0x20);
else
tw_andorb(TW68_LOOP, 0x30, 0x00);
break;
}
return 0;
}
/* ------------------------------------------------------------------ */
/*
* Note that this routine returns what is stored in the fh structure, and
* does not interrogate any of the device registers.
*/
static int tw68_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct tw68_dev *dev = video_drvdata(file);
f->fmt.pix.width = dev->width;
f->fmt.pix.height = dev->height;
f->fmt.pix.field = dev->field;
f->fmt.pix.pixelformat = dev->fmt->fourcc;
f->fmt.pix.bytesperline =
(f->fmt.pix.width * (dev->fmt->depth)) >> 3;
f->fmt.pix.sizeimage =
f->fmt.pix.height * f->fmt.pix.bytesperline;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
static int tw68_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct tw68_dev *dev = video_drvdata(file);
const struct tw68_format *fmt;
enum v4l2_field field;
unsigned int maxh;
fmt = format_by_fourcc(f->fmt.pix.pixelformat);
if (NULL == fmt)
return -EINVAL;
field = f->fmt.pix.field;
maxh = (dev->tvnorm->id & V4L2_STD_525_60) ? 480 : 576;
switch (field) {
case V4L2_FIELD_TOP:
case V4L2_FIELD_BOTTOM:
break;
case V4L2_FIELD_INTERLACED:
case V4L2_FIELD_SEQ_BT:
case V4L2_FIELD_SEQ_TB:
maxh = maxh * 2;
break;
default:
field = (f->fmt.pix.height > maxh / 2)
? V4L2_FIELD_INTERLACED
: V4L2_FIELD_BOTTOM;
break;
}
f->fmt.pix.field = field;
if (f->fmt.pix.width < 48)
f->fmt.pix.width = 48;
if (f->fmt.pix.height < 32)
f->fmt.pix.height = 32;
if (f->fmt.pix.width > 720)
f->fmt.pix.width = 720;
if (f->fmt.pix.height > maxh)
f->fmt.pix.height = maxh;
f->fmt.pix.width &= ~0x03;
f->fmt.pix.bytesperline =
(f->fmt.pix.width * (fmt->depth)) >> 3;
f->fmt.pix.sizeimage =
f->fmt.pix.height * f->fmt.pix.bytesperline;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
/*
* Note that tw68_s_fmt_vid_cap sets the information into the fh structure,
* and it will be used for all future new buffers. However, there could be
* some number of buffers on the "active" chain which will be filled before
* the change takes place.
*/
static int tw68_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct tw68_dev *dev = video_drvdata(file);
int err;
err = tw68_try_fmt_vid_cap(file, priv, f);
if (0 != err)
return err;
dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
dev->width = f->fmt.pix.width;
dev->height = f->fmt.pix.height;
dev->field = f->fmt.pix.field;
return 0;
}
static int tw68_enum_input(struct file *file, void *priv,
struct v4l2_input *i)
{
struct tw68_dev *dev = video_drvdata(file);
unsigned int n;
n = i->index;
if (n >= TW68_INPUT_MAX)
return -EINVAL;
i->index = n;
i->type = V4L2_INPUT_TYPE_CAMERA;
snprintf(i->name, sizeof(i->name), "Composite %d", n);
/* If the query is for the current input, get live data */
if (n == dev->input) {
int v1 = tw_readb(TW68_STATUS1);
int v2 = tw_readb(TW68_MVSN);
if (0 != (v1 & (1 << 7)))
i->status |= V4L2_IN_ST_NO_SYNC;
if (0 != (v1 & (1 << 6)))
i->status |= V4L2_IN_ST_NO_H_LOCK;
if (0 != (v1 & (1 << 2)))
i->status |= V4L2_IN_ST_NO_SIGNAL;
if (0 != (v1 & 1 << 1))
i->status |= V4L2_IN_ST_NO_COLOR;
if (0 != (v2 & (1 << 2)))
i->status |= V4L2_IN_ST_MACROVISION;
}
i->std = video_devdata(file)->tvnorms;
return 0;
}
static int tw68_g_input(struct file *file, void *priv, unsigned int *i)
{
struct tw68_dev *dev = video_drvdata(file);
*i = dev->input;
return 0;
}
static int tw68_s_input(struct file *file, void *priv, unsigned int i)
{
struct tw68_dev *dev = video_drvdata(file);
if (i >= TW68_INPUT_MAX)
return -EINVAL;
dev->input = i;
tw_andorb(TW68_INFORM, 0x03 << 2, dev->input << 2);
return 0;
}
static int tw68_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strscpy(cap->driver, "tw68", sizeof(cap->driver));
strscpy(cap->card, "Techwell Capture Card",
sizeof(cap->card));
return 0;
}
static int tw68_s_std(struct file *file, void *priv, v4l2_std_id id)
{
struct tw68_dev *dev = video_drvdata(file);
unsigned int i;
if (vb2_is_busy(&dev->vidq))
return -EBUSY;
/* Look for match on complete norm id (may have mult bits) */
for (i = 0; i < TVNORMS; i++) {
if (id == tvnorms[i].id)
break;
}
/* If no exact match, look for norm which contains this one */
if (i == TVNORMS) {
for (i = 0; i < TVNORMS; i++)
if (id & tvnorms[i].id)
break;
}
/* If still not matched, give up */
if (i == TVNORMS)
return -EINVAL;
set_tvnorm(dev, &tvnorms[i]); /* do the actual setting */
return 0;
}
static int tw68_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
struct tw68_dev *dev = video_drvdata(file);
*id = dev->tvnorm->id;
return 0;
}
static int tw68_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index >= FORMATS)
return -EINVAL;
f->pixelformat = formats[f->index].fourcc;
return 0;
}
/*
* Used strictly for internal development and debugging, this routine
* prints out the current register contents for the tw68xx device.
*/
static void tw68_dump_regs(struct tw68_dev *dev)
{
unsigned char line[80];
int i, j, k;
unsigned char *cptr;
pr_info("Full dump of TW68 registers:\n");
/* First we do the PCI regs, 8 4-byte regs per line */
for (i = 0; i < 0x100; i += 32) {
cptr = line;
cptr += sprintf(cptr, "%03x ", i);
/* j steps through the next 4 words */
for (j = i; j < i + 16; j += 4)
cptr += sprintf(cptr, "%08x ", tw_readl(j));
*cptr++ = ' ';
for (; j < i + 32; j += 4)
cptr += sprintf(cptr, "%08x ", tw_readl(j));
*cptr++ = '\n';
*cptr = 0;
pr_info("%s", line);
}
/* Next the control regs, which are single-byte, address mod 4 */
while (i < 0x400) {
cptr = line;
cptr += sprintf(cptr, "%03x ", i);
/* Print out 4 groups of 4 bytes */
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
cptr += sprintf(cptr, "%02x ",
tw_readb(i));
i += 4;
}
*cptr++ = ' ';
}
*cptr++ = '\n';
*cptr = 0;
pr_info("%s", line);
}
}
static int vidioc_log_status(struct file *file, void *priv)
{
struct tw68_dev *dev = video_drvdata(file);
tw68_dump_regs(dev);
return v4l2_ctrl_log_status(file, priv);
}
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int vidioc_g_register(struct file *file, void *priv,
struct v4l2_dbg_register *reg)
{
struct tw68_dev *dev = video_drvdata(file);
if (reg->size == 1)
reg->val = tw_readb(reg->reg);
else
reg->val = tw_readl(reg->reg);
return 0;
}
static int vidioc_s_register(struct file *file, void *priv,
const struct v4l2_dbg_register *reg)
{
struct tw68_dev *dev = video_drvdata(file);
if (reg->size == 1)
tw_writeb(reg->reg, reg->val);
else
tw_writel(reg->reg & 0xffff, reg->val);
return 0;
}
#endif
static const struct v4l2_ctrl_ops tw68_ctrl_ops = {
.s_ctrl = tw68_s_ctrl,
};
static const struct v4l2_file_operations video_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = vb2_fop_release,
.read = vb2_fop_read,
.poll = vb2_fop_poll,
.mmap = vb2_fop_mmap,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops video_ioctl_ops = {
.vidioc_querycap = tw68_querycap,
.vidioc_enum_fmt_vid_cap = tw68_enum_fmt_vid_cap,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_s_std = tw68_s_std,
.vidioc_g_std = tw68_g_std,
.vidioc_enum_input = tw68_enum_input,
.vidioc_g_input = tw68_g_input,
.vidioc_s_input = tw68_s_input,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
.vidioc_g_fmt_vid_cap = tw68_g_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = tw68_try_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = tw68_s_fmt_vid_cap,
.vidioc_log_status = vidioc_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
#ifdef CONFIG_VIDEO_ADV_DEBUG
.vidioc_g_register = vidioc_g_register,
.vidioc_s_register = vidioc_s_register,
#endif
};
static const struct video_device tw68_video_template = {
.name = "tw68_video",
.fops = &video_fops,
.ioctl_ops = &video_ioctl_ops,
.release = video_device_release_empty,
.tvnorms = TW68_NORMS,
.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING,
};
/* ------------------------------------------------------------------ */
/* exported stuff */
void tw68_set_tvnorm_hw(struct tw68_dev *dev)
{
tw_andorb(TW68_SDT, 0x07, dev->tvnorm->format);
}
int tw68_video_init1(struct tw68_dev *dev)
{
struct v4l2_ctrl_handler *hdl = &dev->hdl;
v4l2_ctrl_handler_init(hdl, 6);
v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
V4L2_CID_BRIGHTNESS, -128, 127, 1, 20);
v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
V4L2_CID_CONTRAST, 0, 255, 1, 100);
v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
V4L2_CID_SATURATION, 0, 255, 1, 128);
/* NTSC only */
v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
V4L2_CID_HUE, -128, 127, 1, 0);
v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
V4L2_CID_CHROMA_AGC, 0, 1, 1, 1);
if (hdl->error) {
v4l2_ctrl_handler_free(hdl);
return hdl->error;
}
dev->v4l2_dev.ctrl_handler = hdl;
v4l2_ctrl_handler_setup(hdl);
return 0;
}
int tw68_video_init2(struct tw68_dev *dev, int video_nr)
{
int ret;
set_tvnorm(dev, &tvnorms[0]);
dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
dev->width = 720;
dev->height = 576;
dev->field = V4L2_FIELD_INTERLACED;
INIT_LIST_HEAD(&dev->active);
dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
dev->vidq.ops = &tw68_video_qops;
dev->vidq.mem_ops = &vb2_dma_sg_memops;
dev->vidq.drv_priv = dev;
dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
dev->vidq.buf_struct_size = sizeof(struct tw68_buf);
dev->vidq.lock = &dev->lock;
dev->vidq.min_buffers_needed = 2;
dev->vidq.dev = &dev->pci->dev;
ret = vb2_queue_init(&dev->vidq);
if (ret)
return ret;
dev->vdev = tw68_video_template;
dev->vdev.v4l2_dev = &dev->v4l2_dev;
dev->vdev.lock = &dev->lock;
dev->vdev.queue = &dev->vidq;
video_set_drvdata(&dev->vdev, dev);
return video_register_device(&dev->vdev, VFL_TYPE_VIDEO, video_nr);
}
/*
* tw68_irq_video_done
*/
void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status)
{
__u32 reg;
/* reset interrupts handled by this routine */
tw_writel(TW68_INTSTAT, status);
/*
* Check most likely first
*
* DMAPI shows we have reached the end of the risc code
* for the current buffer.
*/
if (status & TW68_DMAPI) {
struct tw68_buf *buf;
spin_lock(&dev->slock);
buf = list_entry(dev->active.next, struct tw68_buf, list);
list_del(&buf->list);
spin_unlock(&dev->slock);
buf->vb.vb2_buf.timestamp = ktime_get_ns();
buf->vb.field = dev->field;
buf->vb.sequence = dev->seqnr++;
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
status &= ~(TW68_DMAPI);
if (0 == status)
return;
}
if (status & (TW68_VLOCK | TW68_HLOCK))
dev_dbg(&dev->pci->dev, "Lost sync\n");
if (status & TW68_PABORT)
dev_err(&dev->pci->dev, "PABORT interrupt\n");
if (status & TW68_DMAPERR)
dev_err(&dev->pci->dev, "DMAPERR interrupt\n");
/*
* On TW6800, FDMIS is apparently generated if video input is switched
* during operation. Therefore, it is not enabled for that chip.
*/
if (status & TW68_FDMIS)
dev_dbg(&dev->pci->dev, "FDMIS interrupt\n");
if (status & TW68_FFOF) {
/* probably a logic error */
reg = tw_readl(TW68_DMAC) & TW68_FIFO_EN;
tw_clearl(TW68_DMAC, TW68_FIFO_EN);
dev_dbg(&dev->pci->dev, "FFOF interrupt\n");
tw_setl(TW68_DMAC, reg);
}
if (status & TW68_FFERR)
dev_dbg(&dev->pci->dev, "FFERR interrupt\n");
}
| linux-master | drivers/media/pci/tw68/tw68-video.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-max.c: Digital Devices bridge MAX card support
*
* Copyright (C) 2010-2017 Digital Devices GmbH
* Ralph Metzler <[email protected]>
* Marcus Metzler <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/io.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/swab.h>
#include <linux/vmalloc.h>
#include "ddbridge.h"
#include "ddbridge-regs.h"
#include "ddbridge-io.h"
#include "ddbridge-mci.h"
#include "ddbridge-max.h"
#include "mxl5xx.h"
/******************************************************************************/
/* MaxS4/8 related modparams */
static int fmode;
module_param(fmode, int, 0444);
MODULE_PARM_DESC(fmode, "frontend emulation mode");
static int fmode_sat = -1;
module_param(fmode_sat, int, 0444);
MODULE_PARM_DESC(fmode_sat, "set frontend emulation mode sat");
static int old_quattro;
module_param(old_quattro, int, 0444);
MODULE_PARM_DESC(old_quattro, "old quattro LNB input order ");
/******************************************************************************/
static int lnb_command(struct ddb *dev, u32 link, u32 lnb, u32 cmd)
{
u32 c, v = 0, tag = DDB_LINK_TAG(link);
v = LNB_TONE & (dev->link[link].lnb.tone << (15 - lnb));
ddbwritel(dev, cmd | v, tag | LNB_CONTROL(lnb));
for (c = 0; c < 10; c++) {
v = ddbreadl(dev, tag | LNB_CONTROL(lnb));
if ((v & LNB_BUSY) == 0)
break;
msleep(20);
}
if (c == 10)
dev_info(dev->dev, "%s lnb = %08x cmd = %08x\n",
__func__, lnb, cmd);
return 0;
}
static int max_send_master_cmd(struct dvb_frontend *fe,
struct dvb_diseqc_master_cmd *cmd)
{
struct ddb_input *input = fe->sec_priv;
struct ddb_port *port = input->port;
struct ddb *dev = port->dev;
struct ddb_dvb *dvb = &port->dvb[input->nr & 1];
u32 tag = DDB_LINK_TAG(port->lnr);
int i;
u32 fmode = dev->link[port->lnr].lnb.fmode;
if (fmode == 2 || fmode == 1)
return 0;
if (dvb->diseqc_send_master_cmd)
dvb->diseqc_send_master_cmd(fe, cmd);
mutex_lock(&dev->link[port->lnr].lnb.lock);
ddbwritel(dev, 0, tag | LNB_BUF_LEVEL(dvb->input));
for (i = 0; i < cmd->msg_len; i++)
ddbwritel(dev, cmd->msg[i], tag | LNB_BUF_WRITE(dvb->input));
lnb_command(dev, port->lnr, dvb->input, LNB_CMD_DISEQC);
mutex_unlock(&dev->link[port->lnr].lnb.lock);
return 0;
}
static int lnb_send_diseqc(struct ddb *dev, u32 link, u32 input,
struct dvb_diseqc_master_cmd *cmd)
{
u32 tag = DDB_LINK_TAG(link);
int i;
ddbwritel(dev, 0, tag | LNB_BUF_LEVEL(input));
for (i = 0; i < cmd->msg_len; i++)
ddbwritel(dev, cmd->msg[i], tag | LNB_BUF_WRITE(input));
lnb_command(dev, link, input, LNB_CMD_DISEQC);
return 0;
}
static int lnb_set_sat(struct ddb *dev, u32 link, u32 input, u32 sat, u32 band,
u32 hor)
{
struct dvb_diseqc_master_cmd cmd = {
.msg = {0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00},
.msg_len = 4
};
cmd.msg[3] = 0xf0 | (((sat << 2) & 0x0c) | (band ? 1 : 0) |
(hor ? 2 : 0));
return lnb_send_diseqc(dev, link, input, &cmd);
}
static int lnb_set_tone(struct ddb *dev, u32 link, u32 input,
enum fe_sec_tone_mode tone)
{
int s = 0;
u32 mask = (1ULL << input);
switch (tone) {
case SEC_TONE_OFF:
if (!(dev->link[link].lnb.tone & mask))
return 0;
dev->link[link].lnb.tone &= ~(1ULL << input);
break;
case SEC_TONE_ON:
if (dev->link[link].lnb.tone & mask)
return 0;
dev->link[link].lnb.tone |= (1ULL << input);
break;
default:
s = -EINVAL;
break;
}
if (!s)
s = lnb_command(dev, link, input, LNB_CMD_NOP);
return s;
}
static int lnb_set_voltage(struct ddb *dev, u32 link, u32 input,
enum fe_sec_voltage voltage)
{
int s = 0;
if (dev->link[link].lnb.oldvoltage[input] == voltage)
return 0;
switch (voltage) {
case SEC_VOLTAGE_OFF:
if (dev->link[link].lnb.voltage[input])
return 0;
lnb_command(dev, link, input, LNB_CMD_OFF);
break;
case SEC_VOLTAGE_13:
lnb_command(dev, link, input, LNB_CMD_LOW);
break;
case SEC_VOLTAGE_18:
lnb_command(dev, link, input, LNB_CMD_HIGH);
break;
default:
s = -EINVAL;
break;
}
dev->link[link].lnb.oldvoltage[input] = voltage;
return s;
}
static int max_set_input_unlocked(struct dvb_frontend *fe, int in)
{
struct ddb_input *input = fe->sec_priv;
struct ddb_port *port = input->port;
struct ddb *dev = port->dev;
struct ddb_dvb *dvb = &port->dvb[input->nr & 1];
int res = 0;
if (in > 3)
return -EINVAL;
if (dvb->input != in) {
u32 bit = (1ULL << input->nr);
u32 obit =
dev->link[port->lnr].lnb.voltage[dvb->input & 3] & bit;
dev->link[port->lnr].lnb.voltage[dvb->input & 3] &= ~bit;
dvb->input = in;
dev->link[port->lnr].lnb.voltage[dvb->input & 3] |= obit;
}
res = dvb->set_input(fe, in);
return res;
}
static int max_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
{
struct ddb_input *input = fe->sec_priv;
struct ddb_port *port = input->port;
struct ddb *dev = port->dev;
struct ddb_dvb *dvb = &port->dvb[input->nr & 1];
int tuner = 0;
int res = 0;
u32 fmode = dev->link[port->lnr].lnb.fmode;
mutex_lock(&dev->link[port->lnr].lnb.lock);
dvb->tone = tone;
switch (fmode) {
default:
case 0:
case 3:
res = lnb_set_tone(dev, port->lnr, dvb->input, tone);
break;
case 1:
case 2:
if (old_quattro) {
if (dvb->tone == SEC_TONE_ON)
tuner |= 2;
if (dvb->voltage == SEC_VOLTAGE_18)
tuner |= 1;
} else {
if (dvb->tone == SEC_TONE_ON)
tuner |= 1;
if (dvb->voltage == SEC_VOLTAGE_18)
tuner |= 2;
}
res = max_set_input_unlocked(fe, tuner);
break;
}
mutex_unlock(&dev->link[port->lnr].lnb.lock);
return res;
}
static int max_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage voltage)
{
struct ddb_input *input = fe->sec_priv;
struct ddb_port *port = input->port;
struct ddb *dev = port->dev;
struct ddb_dvb *dvb = &port->dvb[input->nr & 1];
int tuner = 0;
u32 nv, ov = dev->link[port->lnr].lnb.voltages;
int res = 0;
u32 fmode = dev->link[port->lnr].lnb.fmode;
mutex_lock(&dev->link[port->lnr].lnb.lock);
dvb->voltage = voltage;
switch (fmode) {
case 3:
default:
case 0:
if (fmode == 3)
max_set_input_unlocked(fe, 0);
if (voltage == SEC_VOLTAGE_OFF)
dev->link[port->lnr].lnb.voltage[dvb->input] &=
~(1ULL << input->nr);
else
dev->link[port->lnr].lnb.voltage[dvb->input] |=
(1ULL << input->nr);
res = lnb_set_voltage(dev, port->lnr, dvb->input, voltage);
break;
case 1:
case 2:
if (voltage == SEC_VOLTAGE_OFF)
dev->link[port->lnr].lnb.voltages &=
~(1ULL << input->nr);
else
dev->link[port->lnr].lnb.voltages |=
(1ULL << input->nr);
nv = dev->link[port->lnr].lnb.voltages;
if (old_quattro) {
if (dvb->tone == SEC_TONE_ON)
tuner |= 2;
if (dvb->voltage == SEC_VOLTAGE_18)
tuner |= 1;
} else {
if (dvb->tone == SEC_TONE_ON)
tuner |= 1;
if (dvb->voltage == SEC_VOLTAGE_18)
tuner |= 2;
}
res = max_set_input_unlocked(fe, tuner);
if (nv != ov) {
if (nv) {
lnb_set_voltage(
dev, port->lnr,
0, SEC_VOLTAGE_13);
if (fmode == 1) {
lnb_set_voltage(
dev, port->lnr,
0, SEC_VOLTAGE_13);
if (old_quattro) {
lnb_set_voltage(
dev, port->lnr,
1, SEC_VOLTAGE_18);
lnb_set_voltage(
dev, port->lnr,
2, SEC_VOLTAGE_13);
} else {
lnb_set_voltage(
dev, port->lnr,
1, SEC_VOLTAGE_13);
lnb_set_voltage(
dev, port->lnr,
2, SEC_VOLTAGE_18);
}
lnb_set_voltage(
dev, port->lnr,
3, SEC_VOLTAGE_18);
}
} else {
lnb_set_voltage(
dev, port->lnr,
0, SEC_VOLTAGE_OFF);
if (fmode == 1) {
lnb_set_voltage(
dev, port->lnr,
1, SEC_VOLTAGE_OFF);
lnb_set_voltage(
dev, port->lnr,
2, SEC_VOLTAGE_OFF);
lnb_set_voltage(
dev, port->lnr,
3, SEC_VOLTAGE_OFF);
}
}
}
break;
}
mutex_unlock(&dev->link[port->lnr].lnb.lock);
return res;
}
static int max_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
{
return 0;
}
static int max_send_burst(struct dvb_frontend *fe, enum fe_sec_mini_cmd burst)
{
return 0;
}
static int mxl_fw_read(void *priv, u8 *buf, u32 len)
{
struct ddb_link *link = priv;
struct ddb *dev = link->dev;
dev_info(dev->dev, "Read mxl_fw from link %u\n", link->nr);
return ddbridge_flashread(dev, link->nr, buf, 0xc0000, len);
}
int ddb_lnb_init_fmode(struct ddb *dev, struct ddb_link *link, u32 fm)
{
u32 l = link->nr;
if (link->lnb.fmode == fm)
return 0;
dev_info(dev->dev, "Set fmode link %u = %u\n", l, fm);
mutex_lock(&link->lnb.lock);
if (fm == 2 || fm == 1) {
if (fmode_sat >= 0) {
lnb_set_sat(dev, l, 0, fmode_sat, 0, 0);
if (old_quattro) {
lnb_set_sat(dev, l, 1, fmode_sat, 0, 1);
lnb_set_sat(dev, l, 2, fmode_sat, 1, 0);
} else {
lnb_set_sat(dev, l, 1, fmode_sat, 1, 0);
lnb_set_sat(dev, l, 2, fmode_sat, 0, 1);
}
lnb_set_sat(dev, l, 3, fmode_sat, 1, 1);
}
lnb_set_tone(dev, l, 0, SEC_TONE_OFF);
if (old_quattro) {
lnb_set_tone(dev, l, 1, SEC_TONE_OFF);
lnb_set_tone(dev, l, 2, SEC_TONE_ON);
} else {
lnb_set_tone(dev, l, 1, SEC_TONE_ON);
lnb_set_tone(dev, l, 2, SEC_TONE_OFF);
}
lnb_set_tone(dev, l, 3, SEC_TONE_ON);
}
link->lnb.fmode = fm;
mutex_unlock(&link->lnb.lock);
return 0;
}
static struct mxl5xx_cfg mxl5xx = {
.adr = 0x60,
.type = 0x01,
.clk = 27000000,
.ts_clk = 139,
.cap = 12,
.fw_read = mxl_fw_read,
};
int ddb_fe_attach_mxl5xx(struct ddb_input *input)
{
struct ddb *dev = input->port->dev;
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct ddb_port *port = input->port;
struct ddb_link *link = &dev->link[port->lnr];
struct mxl5xx_cfg cfg;
int demod, tuner;
cfg = mxl5xx;
cfg.fw_priv = link;
dvb->set_input = NULL;
demod = input->nr;
tuner = demod & 3;
if (fmode == 3)
tuner = 0;
dvb->fe = dvb_attach(mxl5xx_attach, i2c, &cfg,
demod, tuner, &dvb->set_input);
if (!dvb->fe) {
dev_err(dev->dev, "No MXL5XX found!\n");
return -ENODEV;
}
if (!dvb->set_input) {
dev_err(dev->dev, "No mxl5xx_set_input function pointer!\n");
return -ENODEV;
}
if (input->nr < 4) {
lnb_command(dev, port->lnr, input->nr, LNB_CMD_INIT);
lnb_set_voltage(dev, port->lnr, input->nr, SEC_VOLTAGE_OFF);
}
ddb_lnb_init_fmode(dev, link, fmode);
dvb->fe->ops.set_voltage = max_set_voltage;
dvb->fe->ops.enable_high_lnb_voltage = max_enable_high_lnb_voltage;
dvb->fe->ops.set_tone = max_set_tone;
dvb->diseqc_send_master_cmd = dvb->fe->ops.diseqc_send_master_cmd;
dvb->fe->ops.diseqc_send_master_cmd = max_send_master_cmd;
dvb->fe->ops.diseqc_send_burst = max_send_burst;
dvb->fe->sec_priv = input;
dvb->input = tuner;
return 0;
}
/******************************************************************************/
/* MAX MCI related functions */
int ddb_fe_attach_mci(struct ddb_input *input, u32 type)
{
struct ddb *dev = input->port->dev;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct ddb_port *port = input->port;
struct ddb_link *link = &dev->link[port->lnr];
int demod, tuner;
struct mci_cfg cfg;
demod = input->nr;
tuner = demod & 3;
switch (type) {
case DDB_TUNER_MCI_SX8:
cfg = ddb_max_sx8_cfg;
if (fmode == 3)
tuner = 0;
break;
default:
return -EINVAL;
}
dvb->fe = ddb_mci_attach(input, &cfg, demod, &dvb->set_input);
if (!dvb->fe) {
dev_err(dev->dev, "No MCI card found!\n");
return -ENODEV;
}
if (!dvb->set_input) {
dev_err(dev->dev, "No MCI set_input function pointer!\n");
return -ENODEV;
}
if (input->nr < 4) {
lnb_command(dev, port->lnr, input->nr, LNB_CMD_INIT);
lnb_set_voltage(dev, port->lnr, input->nr, SEC_VOLTAGE_OFF);
}
ddb_lnb_init_fmode(dev, link, fmode);
dvb->fe->ops.set_voltage = max_set_voltage;
dvb->fe->ops.enable_high_lnb_voltage = max_enable_high_lnb_voltage;
dvb->fe->ops.set_tone = max_set_tone;
dvb->diseqc_send_master_cmd = dvb->fe->ops.diseqc_send_master_cmd;
dvb->fe->ops.diseqc_send_master_cmd = max_send_master_cmd;
dvb->fe->ops.diseqc_send_burst = max_send_burst;
dvb->fe->sec_priv = input;
dvb->input = tuner;
return 0;
}
| linux-master | drivers/media/pci/ddbridge/ddbridge-max.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-ci.c: Digital Devices bridge CI (DuoFlex, CI Bridge) support
*
* Copyright (C) 2010-2017 Digital Devices GmbH
* Marcus Metzler <[email protected]>
* Ralph Metzler <[email protected]>
*/
#include "ddbridge.h"
#include "ddbridge-regs.h"
#include "ddbridge-ci.h"
#include "ddbridge-io.h"
#include "ddbridge-i2c.h"
#include "cxd2099.h"
/* Octopus CI internal CI interface */
static int wait_ci_ready(struct ddb_ci *ci)
{
u32 count = 10;
ndelay(500);
do {
if (ddbreadl(ci->port->dev,
CI_CONTROL(ci->nr)) & CI_READY)
break;
usleep_range(1, 2);
if ((--count) == 0)
return -1;
} while (1);
return 0;
}
static int read_attribute_mem(struct dvb_ca_en50221 *ca,
int slot, int address)
{
struct ddb_ci *ci = ca->data;
u32 val, off = (address >> 1) & (CI_BUFFER_SIZE - 1);
if (address > CI_BUFFER_SIZE)
return -1;
ddbwritel(ci->port->dev, CI_READ_CMD | (1 << 16) | address,
CI_DO_READ_ATTRIBUTES(ci->nr));
wait_ci_ready(ci);
val = 0xff & ddbreadl(ci->port->dev, CI_BUFFER(ci->nr) + off);
return val;
}
static int write_attribute_mem(struct dvb_ca_en50221 *ca, int slot,
int address, u8 value)
{
struct ddb_ci *ci = ca->data;
ddbwritel(ci->port->dev, CI_WRITE_CMD | (value << 16) | address,
CI_DO_ATTRIBUTE_RW(ci->nr));
wait_ci_ready(ci);
return 0;
}
static int read_cam_control(struct dvb_ca_en50221 *ca,
int slot, u8 address)
{
u32 count = 100;
struct ddb_ci *ci = ca->data;
u32 res;
ddbwritel(ci->port->dev, CI_READ_CMD | address,
CI_DO_IO_RW(ci->nr));
ndelay(500);
do {
res = ddbreadl(ci->port->dev, CI_READDATA(ci->nr));
if (res & CI_READY)
break;
usleep_range(1, 2);
if ((--count) == 0)
return -1;
} while (1);
return 0xff & res;
}
static int write_cam_control(struct dvb_ca_en50221 *ca, int slot,
u8 address, u8 value)
{
struct ddb_ci *ci = ca->data;
ddbwritel(ci->port->dev, CI_WRITE_CMD | (value << 16) | address,
CI_DO_IO_RW(ci->nr));
wait_ci_ready(ci);
return 0;
}
static int slot_reset(struct dvb_ca_en50221 *ca, int slot)
{
struct ddb_ci *ci = ca->data;
ddbwritel(ci->port->dev, CI_POWER_ON,
CI_CONTROL(ci->nr));
msleep(100);
ddbwritel(ci->port->dev, CI_POWER_ON | CI_RESET_CAM,
CI_CONTROL(ci->nr));
ddbwritel(ci->port->dev, CI_ENABLE | CI_POWER_ON | CI_RESET_CAM,
CI_CONTROL(ci->nr));
usleep_range(20, 25);
ddbwritel(ci->port->dev, CI_ENABLE | CI_POWER_ON,
CI_CONTROL(ci->nr));
return 0;
}
static int slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
{
struct ddb_ci *ci = ca->data;
ddbwritel(ci->port->dev, 0, CI_CONTROL(ci->nr));
msleep(300);
return 0;
}
static int slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
{
struct ddb_ci *ci = ca->data;
u32 val = ddbreadl(ci->port->dev, CI_CONTROL(ci->nr));
ddbwritel(ci->port->dev, val | CI_BYPASS_DISABLE,
CI_CONTROL(ci->nr));
return 0;
}
static int poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
{
struct ddb_ci *ci = ca->data;
u32 val = ddbreadl(ci->port->dev, CI_CONTROL(ci->nr));
int stat = 0;
if (val & CI_CAM_DETECT)
stat |= DVB_CA_EN50221_POLL_CAM_PRESENT;
if (val & CI_CAM_READY)
stat |= DVB_CA_EN50221_POLL_CAM_READY;
return stat;
}
static struct dvb_ca_en50221 en_templ = {
.read_attribute_mem = read_attribute_mem,
.write_attribute_mem = write_attribute_mem,
.read_cam_control = read_cam_control,
.write_cam_control = write_cam_control,
.slot_reset = slot_reset,
.slot_shutdown = slot_shutdown,
.slot_ts_enable = slot_ts_enable,
.poll_slot_status = poll_slot_status,
};
static void ci_attach(struct ddb_port *port)
{
struct ddb_ci *ci;
ci = kzalloc(sizeof(*ci), GFP_KERNEL);
if (!ci)
return;
memcpy(&ci->en, &en_templ, sizeof(en_templ));
ci->en.data = ci;
port->en = &ci->en;
port->en_freedata = 1;
ci->port = port;
ci->nr = port->nr - 2;
}
/* DuoFlex Dual CI support */
static int write_creg(struct ddb_ci *ci, u8 data, u8 mask)
{
struct i2c_adapter *i2c = &ci->port->i2c->adap;
u8 adr = (ci->port->type == DDB_CI_EXTERNAL_XO2) ? 0x12 : 0x13;
ci->port->creg = (ci->port->creg & ~mask) | data;
return i2c_write_reg(i2c, adr, 0x02, ci->port->creg);
}
static int read_attribute_mem_xo2(struct dvb_ca_en50221 *ca,
int slot, int address)
{
struct ddb_ci *ci = ca->data;
struct i2c_adapter *i2c = &ci->port->i2c->adap;
u8 adr = (ci->port->type == DDB_CI_EXTERNAL_XO2) ? 0x12 : 0x13;
int res;
u8 val;
res = i2c_read_reg16(i2c, adr, 0x8000 | address, &val);
return res ? res : val;
}
static int write_attribute_mem_xo2(struct dvb_ca_en50221 *ca, int slot,
int address, u8 value)
{
struct ddb_ci *ci = ca->data;
struct i2c_adapter *i2c = &ci->port->i2c->adap;
u8 adr = (ci->port->type == DDB_CI_EXTERNAL_XO2) ? 0x12 : 0x13;
return i2c_write_reg16(i2c, adr, 0x8000 | address, value);
}
static int read_cam_control_xo2(struct dvb_ca_en50221 *ca,
int slot, u8 address)
{
struct ddb_ci *ci = ca->data;
struct i2c_adapter *i2c = &ci->port->i2c->adap;
u8 adr = (ci->port->type == DDB_CI_EXTERNAL_XO2) ? 0x12 : 0x13;
u8 val;
int res;
res = i2c_read_reg(i2c, adr, 0x20 | (address & 3), &val);
return res ? res : val;
}
static int write_cam_control_xo2(struct dvb_ca_en50221 *ca, int slot,
u8 address, u8 value)
{
struct ddb_ci *ci = ca->data;
struct i2c_adapter *i2c = &ci->port->i2c->adap;
u8 adr = (ci->port->type == DDB_CI_EXTERNAL_XO2) ? 0x12 : 0x13;
return i2c_write_reg(i2c, adr, 0x20 | (address & 3), value);
}
static int slot_reset_xo2(struct dvb_ca_en50221 *ca, int slot)
{
struct ddb_ci *ci = ca->data;
dev_dbg(ci->port->dev->dev, "%s\n", __func__);
write_creg(ci, 0x01, 0x01);
write_creg(ci, 0x04, 0x04);
msleep(20);
write_creg(ci, 0x02, 0x02);
write_creg(ci, 0x00, 0x04);
write_creg(ci, 0x18, 0x18);
return 0;
}
static int slot_shutdown_xo2(struct dvb_ca_en50221 *ca, int slot)
{
struct ddb_ci *ci = ca->data;
dev_dbg(ci->port->dev->dev, "%s\n", __func__);
write_creg(ci, 0x10, 0xff);
write_creg(ci, 0x08, 0x08);
return 0;
}
static int slot_ts_enable_xo2(struct dvb_ca_en50221 *ca, int slot)
{
struct ddb_ci *ci = ca->data;
dev_dbg(ci->port->dev->dev, "%s\n", __func__);
write_creg(ci, 0x00, 0x10);
return 0;
}
static int poll_slot_status_xo2(struct dvb_ca_en50221 *ca, int slot, int open)
{
struct ddb_ci *ci = ca->data;
struct i2c_adapter *i2c = &ci->port->i2c->adap;
u8 adr = (ci->port->type == DDB_CI_EXTERNAL_XO2) ? 0x12 : 0x13;
u8 val = 0;
int stat = 0;
i2c_read_reg(i2c, adr, 0x01, &val);
if (val & 2)
stat |= DVB_CA_EN50221_POLL_CAM_PRESENT;
if (val & 1)
stat |= DVB_CA_EN50221_POLL_CAM_READY;
return stat;
}
static struct dvb_ca_en50221 en_xo2_templ = {
.read_attribute_mem = read_attribute_mem_xo2,
.write_attribute_mem = write_attribute_mem_xo2,
.read_cam_control = read_cam_control_xo2,
.write_cam_control = write_cam_control_xo2,
.slot_reset = slot_reset_xo2,
.slot_shutdown = slot_shutdown_xo2,
.slot_ts_enable = slot_ts_enable_xo2,
.poll_slot_status = poll_slot_status_xo2,
};
static void ci_xo2_attach(struct ddb_port *port)
{
struct ddb_ci *ci;
ci = kzalloc(sizeof(*ci), GFP_KERNEL);
if (!ci)
return;
memcpy(&ci->en, &en_xo2_templ, sizeof(en_xo2_templ));
ci->en.data = ci;
port->en = &ci->en;
port->en_freedata = 1;
ci->port = port;
ci->nr = port->nr - 2;
ci->port->creg = 0;
write_creg(ci, 0x10, 0xff);
write_creg(ci, 0x08, 0x08);
}
static const struct cxd2099_cfg cxd_cfgtmpl = {
.bitrate = 72000,
.polarity = 1,
.clock_mode = 1,
.max_i2c = 512,
};
static int ci_cxd2099_attach(struct ddb_port *port, u32 bitrate)
{
struct cxd2099_cfg cxd_cfg = cxd_cfgtmpl;
struct i2c_client *client;
cxd_cfg.bitrate = bitrate;
cxd_cfg.en = &port->en;
client = dvb_module_probe("cxd2099", NULL, &port->i2c->adap,
0x40, &cxd_cfg);
if (!client)
goto err;
port->dvb[0].i2c_client[0] = client;
port->en_freedata = 0;
return 0;
err:
dev_err(port->dev->dev, "CXD2099AR attach failed\n");
return -ENODEV;
}
int ddb_ci_attach(struct ddb_port *port, u32 bitrate)
{
int ret;
switch (port->type) {
case DDB_CI_EXTERNAL_SONY:
ret = ci_cxd2099_attach(port, bitrate);
if (ret)
return -ENODEV;
break;
case DDB_CI_EXTERNAL_XO2:
case DDB_CI_EXTERNAL_XO2_B:
ci_xo2_attach(port);
break;
case DDB_CI_INTERNAL:
ci_attach(port);
break;
default:
return -ENODEV;
}
if (!port->en)
return -ENODEV;
dvb_ca_en50221_init(port->dvb[0].adap, port->en, 0, 1);
return 0;
}
void ddb_ci_detach(struct ddb_port *port)
{
if (port->dvb[0].dev)
dvb_unregister_device(port->dvb[0].dev);
if (port->en) {
dvb_ca_en50221_release(port->en);
dvb_module_release(port->dvb[0].i2c_client[0]);
port->dvb[0].i2c_client[0] = NULL;
/* free alloc'ed memory if needed */
if (port->en_freedata)
kfree(port->en->data);
port->en = NULL;
}
}
| linux-master | drivers/media/pci/ddbridge/ddbridge-ci.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-i2c.c: Digital Devices bridge i2c driver
*
* Copyright (C) 2010-2017 Digital Devices GmbH
* Ralph Metzler <[email protected]>
* Marcus Metzler <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/io.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/swab.h>
#include <linux/vmalloc.h>
#include "ddbridge.h"
#include "ddbridge-i2c.h"
#include "ddbridge-regs.h"
#include "ddbridge-io.h"
/******************************************************************************/
static int ddb_i2c_cmd(struct ddb_i2c *i2c, u32 adr, u32 cmd)
{
struct ddb *dev = i2c->dev;
unsigned long stat;
u32 val;
ddbwritel(dev, (adr << 9) | cmd, i2c->regs + I2C_COMMAND);
stat = wait_for_completion_timeout(&i2c->completion, HZ);
val = ddbreadl(dev, i2c->regs + I2C_COMMAND);
if (stat == 0) {
dev_err(dev->dev, "I2C timeout, card %d, port %d, link %u\n",
dev->nr, i2c->nr, i2c->link);
{
u32 istat = ddbreadl(dev, INTERRUPT_STATUS);
dev_err(dev->dev, "DDBridge IRS %08x\n", istat);
if (i2c->link) {
u32 listat = ddbreadl(dev,
DDB_LINK_TAG(i2c->link) |
INTERRUPT_STATUS);
dev_err(dev->dev, "DDBridge link %u IRS %08x\n",
i2c->link, listat);
}
if (istat & 1) {
ddbwritel(dev, istat & 1, INTERRUPT_ACK);
} else {
u32 mon = ddbreadl(dev,
i2c->regs + I2C_MONITOR);
dev_err(dev->dev, "I2C cmd=%08x mon=%08x\n",
val, mon);
}
}
return -EIO;
}
val &= 0x70000;
if (val == 0x20000)
dev_err(dev->dev, "I2C bus error\n");
if (val)
return -EIO;
return 0;
}
static int ddb_i2c_master_xfer(struct i2c_adapter *adapter,
struct i2c_msg msg[], int num)
{
struct ddb_i2c *i2c = (struct ddb_i2c *)i2c_get_adapdata(adapter);
struct ddb *dev = i2c->dev;
u8 addr = 0;
addr = msg[0].addr;
if (msg[0].len > i2c->bsize)
return -EIO;
switch (num) {
case 1:
if (msg[0].flags & I2C_M_RD) {
ddbwritel(dev, msg[0].len << 16,
i2c->regs + I2C_TASKLENGTH);
if (ddb_i2c_cmd(i2c, addr, 3))
break;
ddbcpyfrom(dev, msg[0].buf,
i2c->rbuf, msg[0].len);
return num;
}
ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
ddbwritel(dev, msg[0].len, i2c->regs + I2C_TASKLENGTH);
if (ddb_i2c_cmd(i2c, addr, 2))
break;
return num;
case 2:
if ((msg[0].flags & I2C_M_RD) == I2C_M_RD)
break;
if ((msg[1].flags & I2C_M_RD) != I2C_M_RD)
break;
if (msg[1].len > i2c->bsize)
break;
ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
ddbwritel(dev, msg[0].len | (msg[1].len << 16),
i2c->regs + I2C_TASKLENGTH);
if (ddb_i2c_cmd(i2c, addr, 1))
break;
ddbcpyfrom(dev, msg[1].buf,
i2c->rbuf,
msg[1].len);
return num;
default:
break;
}
return -EIO;
}
static u32 ddb_i2c_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}
static const struct i2c_algorithm ddb_i2c_algo = {
.master_xfer = ddb_i2c_master_xfer,
.functionality = ddb_i2c_functionality,
};
void ddb_i2c_release(struct ddb *dev)
{
int i;
struct ddb_i2c *i2c;
for (i = 0; i < dev->i2c_num; i++) {
i2c = &dev->i2c[i];
i2c_del_adapter(&i2c->adap);
}
}
static void i2c_handler(void *priv)
{
struct ddb_i2c *i2c = (struct ddb_i2c *)priv;
complete(&i2c->completion);
}
static int ddb_i2c_add(struct ddb *dev, struct ddb_i2c *i2c,
const struct ddb_regmap *regmap, int link,
int i, int num)
{
struct i2c_adapter *adap;
i2c->nr = i;
i2c->dev = dev;
i2c->link = link;
i2c->bsize = regmap->i2c_buf->size;
i2c->wbuf = DDB_LINK_TAG(link) |
(regmap->i2c_buf->base + i2c->bsize * i);
i2c->rbuf = i2c->wbuf; /* + i2c->bsize / 2 */
i2c->regs = DDB_LINK_TAG(link) |
(regmap->i2c->base + regmap->i2c->size * i);
ddbwritel(dev, I2C_SPEED_100, i2c->regs + I2C_TIMING);
ddbwritel(dev, ((i2c->rbuf & 0xffff) << 16) | (i2c->wbuf & 0xffff),
i2c->regs + I2C_TASKADDRESS);
init_completion(&i2c->completion);
adap = &i2c->adap;
i2c_set_adapdata(adap, i2c);
#ifdef I2C_ADAP_CLASS_TV_DIGITAL
adap->class = I2C_ADAP_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
#else
#ifdef I2C_CLASS_TV_ANALOG
adap->class = I2C_CLASS_TV_ANALOG;
#endif
#endif
snprintf(adap->name, I2C_NAME_SIZE, "ddbridge_%02x.%x.%x",
dev->nr, i2c->link, i);
adap->algo = &ddb_i2c_algo;
adap->algo_data = (void *)i2c;
adap->dev.parent = dev->dev;
return i2c_add_adapter(adap);
}
int ddb_i2c_init(struct ddb *dev)
{
int stat = 0;
u32 i, j, num = 0, l, base;
struct ddb_i2c *i2c;
struct i2c_adapter *adap;
const struct ddb_regmap *regmap;
for (l = 0; l < DDB_MAX_LINK; l++) {
if (!dev->link[l].info)
continue;
regmap = dev->link[l].info->regmap;
if (!regmap || !regmap->i2c)
continue;
base = regmap->irq_base_i2c;
for (i = 0; i < regmap->i2c->num; i++) {
if (!(dev->link[l].info->i2c_mask & (1 << i)))
continue;
i2c = &dev->i2c[num];
ddb_irq_set(dev, l, i + base, i2c_handler, i2c);
stat = ddb_i2c_add(dev, i2c, regmap, l, i, num);
if (stat)
break;
num++;
}
}
if (stat) {
for (j = 0; j < num; j++) {
i2c = &dev->i2c[j];
adap = &i2c->adap;
i2c_del_adapter(adap);
}
} else {
dev->i2c_num = num;
}
return stat;
}
| linux-master | drivers/media/pci/ddbridge/ddbridge-i2c.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-sx8.c: Digital Devices MAX SX8 driver
*
* Copyright (C) 2018 Digital Devices GmbH
* Marcus Metzler <[email protected]>
* Ralph Metzler <[email protected]>
*/
#include "ddbridge.h"
#include "ddbridge-io.h"
#include "ddbridge-mci.h"
static const u32 MCLK = (1550000000 / 12);
static const u32 MAX_LDPC_BITRATE = (720000000);
static const u32 MAX_DEMOD_LDPC_BITRATE = (1550000000 / 6);
#define SX8_TUNER_NUM 4
#define SX8_DEMOD_NUM 8
#define SX8_DEMOD_NONE 0xff
struct sx8_base {
struct mci_base mci_base;
u8 tuner_use_count[SX8_TUNER_NUM];
u32 gain_mode[SX8_TUNER_NUM];
u32 used_ldpc_bitrate[SX8_DEMOD_NUM];
u8 demod_in_use[SX8_DEMOD_NUM];
u32 iq_mode;
u32 burst_size;
u32 direct_mode;
};
struct sx8 {
struct mci mci;
int first_time_lock;
int started;
struct mci_result signal_info;
u32 bb_mode;
u32 local_frequency;
};
static void release(struct dvb_frontend *fe)
{
struct sx8 *state = fe->demodulator_priv;
struct mci_base *mci_base = state->mci.base;
mci_base->count--;
if (mci_base->count == 0) {
list_del(&mci_base->mci_list);
kfree(mci_base);
}
kfree(state);
}
static int get_info(struct dvb_frontend *fe)
{
int stat;
struct sx8 *state = fe->demodulator_priv;
struct mci_command cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.command = MCI_CMD_GETSIGNALINFO;
cmd.demod = state->mci.demod;
stat = ddb_mci_cmd(&state->mci, &cmd, &state->signal_info);
return stat;
}
static int get_snr(struct dvb_frontend *fe)
{
struct sx8 *state = fe->demodulator_priv;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
p->cnr.len = 1;
p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
p->cnr.stat[0].svalue =
(s64)state->signal_info.dvbs2_signal_info.signal_to_noise
* 10;
return 0;
}
static int get_strength(struct dvb_frontend *fe)
{
struct sx8 *state = fe->demodulator_priv;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
s32 str;
str = 100000 -
(state->signal_info.dvbs2_signal_info.channel_power
* 10 + 108750);
p->strength.len = 1;
p->strength.stat[0].scale = FE_SCALE_DECIBEL;
p->strength.stat[0].svalue = str;
return 0;
}
static int read_status(struct dvb_frontend *fe, enum fe_status *status)
{
int stat;
struct sx8 *state = fe->demodulator_priv;
struct mci_command cmd;
struct mci_result res;
cmd.command = MCI_CMD_GETSTATUS;
cmd.demod = state->mci.demod;
stat = ddb_mci_cmd(&state->mci, &cmd, &res);
if (stat)
return stat;
*status = 0x00;
get_info(fe);
get_strength(fe);
if (res.status == SX8_DEMOD_WAIT_MATYPE)
*status = 0x0f;
if (res.status == SX8_DEMOD_LOCKED) {
*status = 0x1f;
get_snr(fe);
}
return stat;
}
static int mci_set_tuner(struct dvb_frontend *fe, u32 tuner, u32 on)
{
struct sx8 *state = fe->demodulator_priv;
struct mci_base *mci_base = state->mci.base;
struct sx8_base *sx8_base = (struct sx8_base *)mci_base;
struct mci_command cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.tuner = state->mci.tuner;
cmd.command = on ? SX8_CMD_INPUT_ENABLE : SX8_CMD_INPUT_DISABLE;
cmd.sx8_input_enable.flags = sx8_base->gain_mode[state->mci.tuner];
return ddb_mci_cmd(&state->mci, &cmd, NULL);
}
static int stop(struct dvb_frontend *fe)
{
struct sx8 *state = fe->demodulator_priv;
struct mci_base *mci_base = state->mci.base;
struct sx8_base *sx8_base = (struct sx8_base *)mci_base;
struct mci_command cmd;
u32 input = state->mci.tuner;
memset(&cmd, 0, sizeof(cmd));
if (state->mci.demod != SX8_DEMOD_NONE) {
cmd.command = MCI_CMD_STOP;
cmd.demod = state->mci.demod;
ddb_mci_cmd(&state->mci, &cmd, NULL);
if (sx8_base->iq_mode) {
cmd.command = SX8_CMD_DISABLE_IQOUTPUT;
cmd.demod = state->mci.demod;
cmd.output = 0;
ddb_mci_cmd(&state->mci, &cmd, NULL);
ddb_mci_config(&state->mci, SX8_TSCONFIG_MODE_NORMAL);
}
}
mutex_lock(&mci_base->tuner_lock);
sx8_base->tuner_use_count[input]--;
if (!sx8_base->tuner_use_count[input])
mci_set_tuner(fe, input, 0);
if (state->mci.demod < SX8_DEMOD_NUM) {
sx8_base->demod_in_use[state->mci.demod] = 0;
state->mci.demod = SX8_DEMOD_NONE;
}
sx8_base->used_ldpc_bitrate[state->mci.nr] = 0;
sx8_base->iq_mode = 0;
mutex_unlock(&mci_base->tuner_lock);
state->started = 0;
return 0;
}
static int start(struct dvb_frontend *fe, u32 flags, u32 modmask, u32 ts_config)
{
struct sx8 *state = fe->demodulator_priv;
struct mci_base *mci_base = state->mci.base;
struct sx8_base *sx8_base = (struct sx8_base *)mci_base;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
u32 used_ldpc_bitrate = 0, free_ldpc_bitrate;
u32 used_demods = 0;
struct mci_command cmd;
u32 input = state->mci.tuner;
u32 bits_per_symbol = 0;
int i = -1, stat = 0;
if (p->symbol_rate >= (MCLK / 2))
flags &= ~1;
if ((flags & 3) == 0)
return -EINVAL;
if (flags & 2) {
u32 tmp = modmask;
bits_per_symbol = 1;
while (tmp & 1) {
tmp >>= 1;
bits_per_symbol++;
}
}
mutex_lock(&mci_base->tuner_lock);
if (sx8_base->iq_mode) {
stat = -EBUSY;
goto unlock;
}
if (sx8_base->direct_mode) {
if (p->symbol_rate >= MCLK / 2) {
if (state->mci.nr < 4)
i = state->mci.nr;
} else {
i = state->mci.nr;
}
} else {
for (i = 0; i < SX8_DEMOD_NUM; i++) {
used_ldpc_bitrate += sx8_base->used_ldpc_bitrate[i];
if (sx8_base->demod_in_use[i])
used_demods++;
}
if (used_ldpc_bitrate >= MAX_LDPC_BITRATE ||
((ts_config & SX8_TSCONFIG_MODE_MASK) >
SX8_TSCONFIG_MODE_NORMAL && used_demods > 0)) {
stat = -EBUSY;
goto unlock;
}
free_ldpc_bitrate = MAX_LDPC_BITRATE - used_ldpc_bitrate;
if (free_ldpc_bitrate > MAX_DEMOD_LDPC_BITRATE)
free_ldpc_bitrate = MAX_DEMOD_LDPC_BITRATE;
while (p->symbol_rate * bits_per_symbol > free_ldpc_bitrate)
bits_per_symbol--;
if (bits_per_symbol < 2) {
stat = -EBUSY;
goto unlock;
}
modmask &= ((1 << (bits_per_symbol - 1)) - 1);
if (((flags & 0x02) != 0) && modmask == 0) {
stat = -EBUSY;
goto unlock;
}
i = (p->symbol_rate > (MCLK / 2)) ? 3 : 7;
while (i >= 0 && sx8_base->demod_in_use[i])
i--;
}
if (i < 0) {
stat = -EBUSY;
goto unlock;
}
sx8_base->demod_in_use[i] = 1;
sx8_base->used_ldpc_bitrate[state->mci.nr] = p->symbol_rate
* bits_per_symbol;
state->mci.demod = i;
if (!sx8_base->tuner_use_count[input])
mci_set_tuner(fe, input, 1);
sx8_base->tuner_use_count[input]++;
sx8_base->iq_mode = (ts_config > 1);
unlock:
mutex_unlock(&mci_base->tuner_lock);
if (stat)
return stat;
memset(&cmd, 0, sizeof(cmd));
if (sx8_base->iq_mode) {
cmd.command = SX8_CMD_ENABLE_IQOUTPUT;
cmd.demod = state->mci.demod;
cmd.output = 0;
ddb_mci_cmd(&state->mci, &cmd, NULL);
ddb_mci_config(&state->mci, ts_config);
}
if (p->stream_id != NO_STREAM_ID_FILTER && p->stream_id != 0x80000000)
flags |= 0x80;
dev_dbg(mci_base->dev, "MCI-%d: tuner=%d demod=%d\n",
state->mci.nr, state->mci.tuner, state->mci.demod);
cmd.command = MCI_CMD_SEARCH_DVBS;
cmd.dvbs2_search.flags = flags;
cmd.dvbs2_search.s2_modulation_mask = modmask;
cmd.dvbs2_search.retry = 2;
cmd.dvbs2_search.frequency = p->frequency * 1000;
cmd.dvbs2_search.symbol_rate = p->symbol_rate;
cmd.dvbs2_search.scrambling_sequence_index =
p->scrambling_sequence_index | 0x80000000;
cmd.dvbs2_search.input_stream_id =
(p->stream_id != NO_STREAM_ID_FILTER) ? p->stream_id : 0;
cmd.tuner = state->mci.tuner;
cmd.demod = state->mci.demod;
cmd.output = state->mci.nr;
if (p->stream_id == 0x80000000)
cmd.output |= 0x80;
stat = ddb_mci_cmd(&state->mci, &cmd, NULL);
if (stat)
stop(fe);
return stat;
}
static int start_iq(struct dvb_frontend *fe, u32 flags, u32 roll_off,
u32 ts_config)
{
struct sx8 *state = fe->demodulator_priv;
struct mci_base *mci_base = state->mci.base;
struct sx8_base *sx8_base = (struct sx8_base *)mci_base;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
u32 used_demods = 0;
struct mci_command cmd;
u32 input = state->mci.tuner;
int i, stat = 0;
mutex_lock(&mci_base->tuner_lock);
if (sx8_base->iq_mode) {
stat = -EBUSY;
goto unlock;
}
for (i = 0; i < SX8_DEMOD_NUM; i++)
if (sx8_base->demod_in_use[i])
used_demods++;
if (used_demods > 0) {
stat = -EBUSY;
goto unlock;
}
state->mci.demod = 0;
if (!sx8_base->tuner_use_count[input])
mci_set_tuner(fe, input, 1);
sx8_base->tuner_use_count[input]++;
sx8_base->iq_mode = (ts_config > 1);
unlock:
mutex_unlock(&mci_base->tuner_lock);
if (stat)
return stat;
memset(&cmd, 0, sizeof(cmd));
cmd.command = SX8_CMD_START_IQ;
cmd.sx8_start_iq.flags = flags;
cmd.sx8_start_iq.roll_off = roll_off;
cmd.sx8_start_iq.frequency = p->frequency * 1000;
cmd.sx8_start_iq.symbol_rate = p->symbol_rate;
cmd.tuner = state->mci.tuner;
cmd.demod = state->mci.demod;
stat = ddb_mci_cmd(&state->mci, &cmd, NULL);
if (stat)
stop(fe);
ddb_mci_config(&state->mci, ts_config);
return stat;
}
static int set_parameters(struct dvb_frontend *fe)
{
int stat = 0;
struct sx8 *state = fe->demodulator_priv;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
u32 ts_config = SX8_TSCONFIG_MODE_NORMAL, iq_mode = 0, isi;
if (state->started)
stop(fe);
isi = p->stream_id;
if (isi != NO_STREAM_ID_FILTER)
iq_mode = (isi & 0x30000000) >> 28;
if (iq_mode)
ts_config = (SX8_TSCONFIG_TSHEADER | SX8_TSCONFIG_MODE_IQ);
if (iq_mode < 3) {
u32 mask;
switch (p->modulation) {
/* uncomment whenever these modulations hit the DVB API
* case APSK_256:
* mask = 0x7f;
* break;
* case APSK_128:
* mask = 0x3f;
* break;
* case APSK_64:
* mask = 0x1f;
* break;
*/
case APSK_32:
mask = 0x0f;
break;
case APSK_16:
mask = 0x07;
break;
default:
mask = 0x03;
break;
}
stat = start(fe, 3, mask, ts_config);
} else {
stat = start_iq(fe, 0, 4, ts_config);
}
if (!stat) {
state->started = 1;
state->first_time_lock = 1;
state->signal_info.status = SX8_DEMOD_WAIT_SIGNAL;
}
return stat;
}
static int tune(struct dvb_frontend *fe, bool re_tune,
unsigned int mode_flags,
unsigned int *delay, enum fe_status *status)
{
int r;
if (re_tune) {
r = set_parameters(fe);
if (r)
return r;
}
r = read_status(fe, status);
if (r)
return r;
if (*status & FE_HAS_LOCK)
return 0;
*delay = HZ / 10;
return 0;
}
static enum dvbfe_algo get_algo(struct dvb_frontend *fe)
{
return DVBFE_ALGO_HW;
}
static int set_input(struct dvb_frontend *fe, int input)
{
struct sx8 *state = fe->demodulator_priv;
struct mci_base *mci_base = state->mci.base;
if (input >= SX8_TUNER_NUM)
return -EINVAL;
state->mci.tuner = input;
dev_dbg(mci_base->dev, "MCI-%d: input=%d\n", state->mci.nr, input);
return 0;
}
static struct dvb_frontend_ops sx8_ops = {
.delsys = { SYS_DVBS, SYS_DVBS2 },
.info = {
.name = "Digital Devices MaxSX8 MCI DVB-S/S2/S2X",
.frequency_min_hz = 950 * MHz,
.frequency_max_hz = 2150 * MHz,
.symbol_rate_min = 100000,
.symbol_rate_max = 100000000,
.caps = FE_CAN_INVERSION_AUTO |
FE_CAN_FEC_AUTO |
FE_CAN_QPSK |
FE_CAN_2G_MODULATION |
FE_CAN_MULTISTREAM,
},
.get_frontend_algo = get_algo,
.tune = tune,
.release = release,
.read_status = read_status,
};
static int init(struct mci *mci)
{
struct sx8 *state = (struct sx8 *)mci;
state->mci.demod = SX8_DEMOD_NONE;
return 0;
}
const struct mci_cfg ddb_max_sx8_cfg = {
.type = 0,
.fe_ops = &sx8_ops,
.base_size = sizeof(struct sx8_base),
.state_size = sizeof(struct sx8),
.init = init,
.set_input = set_input,
};
| linux-master | drivers/media/pci/ddbridge/ddbridge-sx8.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge.c: Digital Devices PCIe bridge driver
*
* Copyright (C) 2010-2017 Digital Devices GmbH
* Ralph Metzler <[email protected]>
* Marcus Metzler <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/io.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/swab.h>
#include <linux/vmalloc.h>
#include "ddbridge.h"
#include "ddbridge-i2c.h"
#include "ddbridge-regs.h"
#include "ddbridge-hw.h"
#include "ddbridge-io.h"
/****************************************************************************/
/* module parameters */
#ifdef CONFIG_PCI_MSI
#ifdef CONFIG_DVB_DDBRIDGE_MSIENABLE
static int msi = 1;
#else
static int msi;
#endif
module_param(msi, int, 0444);
#ifdef CONFIG_DVB_DDBRIDGE_MSIENABLE
MODULE_PARM_DESC(msi, "Control MSI interrupts: 0-disable, 1-enable (default)");
#else
MODULE_PARM_DESC(msi, "Control MSI interrupts: 0-disable (default), 1-enable");
#endif
#endif
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static void ddb_irq_disable(struct ddb *dev)
{
ddbwritel(dev, 0, INTERRUPT_ENABLE);
ddbwritel(dev, 0, MSI1_ENABLE);
}
static void ddb_msi_exit(struct ddb *dev)
{
#ifdef CONFIG_PCI_MSI
if (dev->msi)
pci_free_irq_vectors(dev->pdev);
#endif
}
static void ddb_irq_exit(struct ddb *dev)
{
ddb_irq_disable(dev);
if (dev->msi == 2)
free_irq(pci_irq_vector(dev->pdev, 1), dev);
free_irq(pci_irq_vector(dev->pdev, 0), dev);
}
static void ddb_remove(struct pci_dev *pdev)
{
struct ddb *dev = (struct ddb *)pci_get_drvdata(pdev);
ddb_device_destroy(dev);
ddb_ports_detach(dev);
ddb_i2c_release(dev);
ddb_irq_exit(dev);
ddb_msi_exit(dev);
ddb_ports_release(dev);
ddb_buffers_free(dev);
ddb_unmap(dev);
pci_set_drvdata(pdev, NULL);
pci_disable_device(pdev);
}
#ifdef CONFIG_PCI_MSI
static void ddb_irq_msi(struct ddb *dev, int nr)
{
int stat;
if (msi && pci_msi_enabled()) {
stat = pci_alloc_irq_vectors(dev->pdev, 1, nr,
PCI_IRQ_MSI | PCI_IRQ_MSIX);
if (stat >= 1) {
dev->msi = stat;
dev_info(dev->dev, "using %d MSI interrupt(s)\n",
dev->msi);
} else {
dev_info(dev->dev, "MSI not available.\n");
}
}
}
#endif
static int ddb_irq_init(struct ddb *dev)
{
int stat;
int irq_flag = IRQF_SHARED;
ddbwritel(dev, 0x00000000, INTERRUPT_ENABLE);
ddbwritel(dev, 0x00000000, MSI1_ENABLE);
ddbwritel(dev, 0x00000000, MSI2_ENABLE);
ddbwritel(dev, 0x00000000, MSI3_ENABLE);
ddbwritel(dev, 0x00000000, MSI4_ENABLE);
ddbwritel(dev, 0x00000000, MSI5_ENABLE);
ddbwritel(dev, 0x00000000, MSI6_ENABLE);
ddbwritel(dev, 0x00000000, MSI7_ENABLE);
#ifdef CONFIG_PCI_MSI
ddb_irq_msi(dev, 2);
if (dev->msi)
irq_flag = 0;
if (dev->msi == 2) {
stat = request_irq(pci_irq_vector(dev->pdev, 0),
ddb_irq_handler0, irq_flag, "ddbridge",
(void *)dev);
if (stat < 0)
return stat;
stat = request_irq(pci_irq_vector(dev->pdev, 1),
ddb_irq_handler1, irq_flag, "ddbridge",
(void *)dev);
if (stat < 0) {
free_irq(pci_irq_vector(dev->pdev, 0), dev);
return stat;
}
} else
#endif
{
stat = request_irq(pci_irq_vector(dev->pdev, 0),
ddb_irq_handler, irq_flag, "ddbridge",
(void *)dev);
if (stat < 0)
return stat;
}
if (dev->msi == 2) {
ddbwritel(dev, 0x0fffff00, INTERRUPT_ENABLE);
ddbwritel(dev, 0x0000000f, MSI1_ENABLE);
} else {
ddbwritel(dev, 0x0fffff0f, INTERRUPT_ENABLE);
ddbwritel(dev, 0x00000000, MSI1_ENABLE);
}
return stat;
}
static int ddb_probe(struct pci_dev *pdev,
const struct pci_device_id *id)
{
struct ddb *dev;
int stat = 0;
if (pci_enable_device(pdev) < 0)
return -ENODEV;
pci_set_master(pdev);
if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)))
if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)))
return -ENODEV;
dev = vzalloc(sizeof(*dev));
if (!dev)
return -ENOMEM;
mutex_init(&dev->mutex);
dev->has_dma = 1;
dev->pdev = pdev;
dev->dev = &pdev->dev;
pci_set_drvdata(pdev, dev);
dev->link[0].ids.vendor = id->vendor;
dev->link[0].ids.device = id->device;
dev->link[0].ids.subvendor = id->subvendor;
dev->link[0].ids.subdevice = pdev->subsystem_device;
dev->link[0].ids.devid = (id->device << 16) | id->vendor;
dev->link[0].dev = dev;
dev->link[0].info = get_ddb_info(id->vendor, id->device,
id->subvendor, pdev->subsystem_device);
dev_info(&pdev->dev, "detected %s\n", dev->link[0].info->name);
dev->regs_len = pci_resource_len(dev->pdev, 0);
dev->regs = ioremap(pci_resource_start(dev->pdev, 0),
pci_resource_len(dev->pdev, 0));
if (!dev->regs) {
dev_err(&pdev->dev, "not enough memory for register map\n");
stat = -ENOMEM;
goto fail;
}
if (ddbreadl(dev, 0) == 0xffffffff) {
dev_err(&pdev->dev, "cannot read registers\n");
stat = -ENODEV;
goto fail;
}
dev->link[0].ids.hwid = ddbreadl(dev, 0);
dev->link[0].ids.regmapid = ddbreadl(dev, 4);
dev_info(&pdev->dev, "HW %08x REGMAP %08x\n",
dev->link[0].ids.hwid, dev->link[0].ids.regmapid);
ddbwritel(dev, 0, DMA_BASE_READ);
ddbwritel(dev, 0, DMA_BASE_WRITE);
stat = ddb_irq_init(dev);
if (stat < 0)
goto fail0;
if (ddb_init(dev) == 0)
return 0;
ddb_irq_exit(dev);
fail0:
dev_err(&pdev->dev, "fail0\n");
ddb_msi_exit(dev);
fail:
dev_err(&pdev->dev, "fail\n");
ddb_unmap(dev);
pci_set_drvdata(pdev, NULL);
pci_disable_device(pdev);
return -1;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
#define DDB_DEVICE_ANY(_device) \
{ PCI_DEVICE_SUB(DDVID, _device, DDVID, PCI_ANY_ID) }
static const struct pci_device_id ddb_id_table[] = {
DDB_DEVICE_ANY(0x0002),
DDB_DEVICE_ANY(0x0003),
DDB_DEVICE_ANY(0x0005),
DDB_DEVICE_ANY(0x0006),
DDB_DEVICE_ANY(0x0007),
DDB_DEVICE_ANY(0x0008),
DDB_DEVICE_ANY(0x0009),
DDB_DEVICE_ANY(0x0011),
DDB_DEVICE_ANY(0x0012),
DDB_DEVICE_ANY(0x0013),
DDB_DEVICE_ANY(0x0201),
DDB_DEVICE_ANY(0x0203),
DDB_DEVICE_ANY(0x0210),
DDB_DEVICE_ANY(0x0220),
DDB_DEVICE_ANY(0x0320),
DDB_DEVICE_ANY(0x0321),
DDB_DEVICE_ANY(0x0322),
DDB_DEVICE_ANY(0x0323),
DDB_DEVICE_ANY(0x0328),
DDB_DEVICE_ANY(0x0329),
{0}
};
MODULE_DEVICE_TABLE(pci, ddb_id_table);
static struct pci_driver ddb_pci_driver = {
.name = "ddbridge",
.id_table = ddb_id_table,
.probe = ddb_probe,
.remove = ddb_remove,
};
static __init int module_init_ddbridge(void)
{
int stat;
pr_info("Digital Devices PCIE bridge driver "
DDBRIDGE_VERSION
", Copyright (C) 2010-17 Digital Devices GmbH\n");
stat = ddb_init_ddbridge();
if (stat < 0)
return stat;
stat = pci_register_driver(&ddb_pci_driver);
if (stat < 0)
ddb_exit_ddbridge(0, stat);
return stat;
}
static __exit void module_exit_ddbridge(void)
{
pci_unregister_driver(&ddb_pci_driver);
ddb_exit_ddbridge(0, 0);
}
module_init(module_init_ddbridge);
module_exit(module_exit_ddbridge);
MODULE_DESCRIPTION("Digital Devices PCIe Bridge");
MODULE_AUTHOR("Ralph and Marcus Metzler, Metzler Brothers Systementwicklung GbR");
MODULE_LICENSE("GPL v2");
MODULE_VERSION(DDBRIDGE_VERSION);
| linux-master | drivers/media/pci/ddbridge/ddbridge-main.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-hw.c: Digital Devices bridge hardware maps
*
* Copyright (C) 2010-2017 Digital Devices GmbH
* Ralph Metzler <[email protected]>
* Marcus Metzler <[email protected]>
*/
#include "ddbridge.h"
#include "ddbridge-hw.h"
/******************************************************************************/
static const struct ddb_regset octopus_input = {
.base = 0x200,
.num = 0x08,
.size = 0x10,
};
static const struct ddb_regset octopus_output = {
.base = 0x280,
.num = 0x08,
.size = 0x10,
};
static const struct ddb_regset octopus_idma = {
.base = 0x300,
.num = 0x08,
.size = 0x10,
};
static const struct ddb_regset octopus_idma_buf = {
.base = 0x2000,
.num = 0x08,
.size = 0x100,
};
static const struct ddb_regset octopus_odma = {
.base = 0x380,
.num = 0x04,
.size = 0x10,
};
static const struct ddb_regset octopus_odma_buf = {
.base = 0x2800,
.num = 0x04,
.size = 0x100,
};
static const struct ddb_regset octopus_i2c = {
.base = 0x80,
.num = 0x04,
.size = 0x20,
};
static const struct ddb_regset octopus_i2c_buf = {
.base = 0x1000,
.num = 0x04,
.size = 0x200,
};
/****************************************************************************/
static const struct ddb_regmap octopus_map = {
.irq_base_i2c = 0,
.irq_base_idma = 8,
.irq_base_odma = 16,
.i2c = &octopus_i2c,
.i2c_buf = &octopus_i2c_buf,
.idma = &octopus_idma,
.idma_buf = &octopus_idma_buf,
.odma = &octopus_odma,
.odma_buf = &octopus_odma_buf,
.input = &octopus_input,
.output = &octopus_output,
};
/****************************************************************************/
static const struct ddb_info ddb_none = {
.type = DDB_NONE,
.name = "unknown Digital Devices PCIe card, install newer driver",
.regmap = &octopus_map,
};
static const struct ddb_info ddb_octopus = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Octopus DVB adapter",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
};
static const struct ddb_info ddb_octopusv3 = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Octopus V3 DVB adapter",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
};
static const struct ddb_info ddb_octopus_le = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Octopus LE DVB adapter",
.regmap = &octopus_map,
.port_num = 2,
.i2c_mask = 0x03,
};
static const struct ddb_info ddb_octopus_oem = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Octopus OEM",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.led_num = 1,
.fan_num = 1,
.temp_num = 1,
.temp_bus = 0,
};
static const struct ddb_info ddb_octopus_mini = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Octopus Mini",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
};
static const struct ddb_info ddb_v6 = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Cine S2 V6 DVB adapter",
.regmap = &octopus_map,
.port_num = 3,
.i2c_mask = 0x07,
};
static const struct ddb_info ddb_v6_5 = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Cine S2 V6.5 DVB adapter",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
};
static const struct ddb_info ddb_v7 = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Cine S2 V7 DVB adapter",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 2,
.board_control_2 = 4,
.ts_quirks = TS_QUIRK_REVERSED,
};
static const struct ddb_info ddb_v7a = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Cine S2 V7 Advanced DVB adapter",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 2,
.board_control_2 = 4,
.ts_quirks = TS_QUIRK_REVERSED,
};
static const struct ddb_info ddb_ctv7 = {
.type = DDB_OCTOPUS,
.name = "Digital Devices Cine CT V7 DVB adapter",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 3,
.board_control_2 = 4,
};
static const struct ddb_info ddb_satixs2v3 = {
.type = DDB_OCTOPUS,
.name = "Mystique SaTiX-S2 V3 DVB adapter",
.regmap = &octopus_map,
.port_num = 3,
.i2c_mask = 0x07,
};
static const struct ddb_info ddb_ci = {
.type = DDB_OCTOPUS_CI,
.name = "Digital Devices Octopus CI",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x03,
};
static const struct ddb_info ddb_cis = {
.type = DDB_OCTOPUS_CI,
.name = "Digital Devices Octopus CI single",
.regmap = &octopus_map,
.port_num = 3,
.i2c_mask = 0x03,
};
static const struct ddb_info ddb_ci_s2_pro = {
.type = DDB_OCTOPUS_CI,
.name = "Digital Devices Octopus CI S2 Pro",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x01,
.board_control = 2,
.board_control_2 = 4,
};
static const struct ddb_info ddb_ci_s2_pro_a = {
.type = DDB_OCTOPUS_CI,
.name = "Digital Devices Octopus CI S2 Pro Advanced",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x01,
.board_control = 2,
.board_control_2 = 4,
};
static const struct ddb_info ddb_dvbct = {
.type = DDB_OCTOPUS,
.name = "Digital Devices DVBCT V6.1 DVB adapter",
.regmap = &octopus_map,
.port_num = 3,
.i2c_mask = 0x07,
};
/****************************************************************************/
static const struct ddb_info ddb_ct2_8 = {
.type = DDB_OCTOPUS_MAX_CT,
.name = "Digital Devices MAX A8 CT2",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 0x0ff,
.board_control_2 = 0xf00,
.ts_quirks = TS_QUIRK_SERIAL,
.tempmon_irq = 24,
};
static const struct ddb_info ddb_c2t2_8 = {
.type = DDB_OCTOPUS_MAX_CT,
.name = "Digital Devices MAX A8 C2T2",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 0x0ff,
.board_control_2 = 0xf00,
.ts_quirks = TS_QUIRK_SERIAL,
.tempmon_irq = 24,
};
static const struct ddb_info ddb_isdbt_8 = {
.type = DDB_OCTOPUS_MAX_CT,
.name = "Digital Devices MAX A8 ISDBT",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 0x0ff,
.board_control_2 = 0xf00,
.ts_quirks = TS_QUIRK_SERIAL,
.tempmon_irq = 24,
};
static const struct ddb_info ddb_c2t2i_v0_8 = {
.type = DDB_OCTOPUS_MAX_CT,
.name = "Digital Devices MAX A8 C2T2I V0",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 0x0ff,
.board_control_2 = 0xf00,
.ts_quirks = TS_QUIRK_SERIAL | TS_QUIRK_ALT_OSC,
.tempmon_irq = 24,
};
static const struct ddb_info ddb_c2t2i_8 = {
.type = DDB_OCTOPUS_MAX_CT,
.name = "Digital Devices MAX A8 C2T2I",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x0f,
.board_control = 0x0ff,
.board_control_2 = 0xf00,
.ts_quirks = TS_QUIRK_SERIAL,
.tempmon_irq = 24,
};
/****************************************************************************/
static const struct ddb_info ddb_s2_48 = {
.type = DDB_OCTOPUS_MAX,
.name = "Digital Devices MAX S8 4/8",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x01,
.board_control = 1,
.tempmon_irq = 24,
};
static const struct ddb_info ddb_s2x_48 = {
.type = DDB_OCTOPUS_MCI,
.name = "Digital Devices MAX SX8",
.regmap = &octopus_map,
.port_num = 4,
.i2c_mask = 0x00,
.tempmon_irq = 24,
.mci_ports = 4,
.mci_type = 0,
};
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
#define DDB_DEVID(_device, _subdevice, _info) { \
.vendor = DDVID, \
.device = _device, \
.subvendor = DDVID, \
.subdevice = _subdevice, \
.info = &_info }
static const struct ddb_device_id ddb_device_ids[] = {
/* PCIe devices */
DDB_DEVID(0x0002, 0x0001, ddb_octopus),
DDB_DEVID(0x0003, 0x0001, ddb_octopus),
DDB_DEVID(0x0005, 0x0004, ddb_octopusv3),
DDB_DEVID(0x0003, 0x0002, ddb_octopus_le),
DDB_DEVID(0x0003, 0x0003, ddb_octopus_oem),
DDB_DEVID(0x0003, 0x0010, ddb_octopus_mini),
DDB_DEVID(0x0005, 0x0011, ddb_octopus_mini),
DDB_DEVID(0x0003, 0x0020, ddb_v6),
DDB_DEVID(0x0003, 0x0021, ddb_v6_5),
DDB_DEVID(0x0006, 0x0022, ddb_v7),
DDB_DEVID(0x0006, 0x0024, ddb_v7a),
DDB_DEVID(0x0003, 0x0030, ddb_dvbct),
DDB_DEVID(0x0003, 0xdb03, ddb_satixs2v3),
DDB_DEVID(0x0006, 0x0031, ddb_ctv7),
DDB_DEVID(0x0006, 0x0032, ddb_ctv7),
DDB_DEVID(0x0006, 0x0033, ddb_ctv7),
DDB_DEVID(0x0007, 0x0023, ddb_s2_48),
DDB_DEVID(0x0008, 0x0034, ddb_ct2_8),
DDB_DEVID(0x0008, 0x0035, ddb_c2t2_8),
DDB_DEVID(0x0008, 0x0036, ddb_isdbt_8),
DDB_DEVID(0x0008, 0x0037, ddb_c2t2i_v0_8),
DDB_DEVID(0x0008, 0x0038, ddb_c2t2i_8),
DDB_DEVID(0x0009, 0x0025, ddb_s2x_48),
DDB_DEVID(0x0006, 0x0039, ddb_ctv7),
DDB_DEVID(0x0011, 0x0040, ddb_ci),
DDB_DEVID(0x0011, 0x0041, ddb_cis),
DDB_DEVID(0x0012, 0x0042, ddb_ci),
DDB_DEVID(0x0013, 0x0043, ddb_ci_s2_pro),
DDB_DEVID(0x0013, 0x0044, ddb_ci_s2_pro_a),
};
/****************************************************************************/
const struct ddb_info *get_ddb_info(u16 vendor, u16 device,
u16 subvendor, u16 subdevice)
{
int i;
for (i = 0; i < ARRAY_SIZE(ddb_device_ids); i++) {
const struct ddb_device_id *id = &ddb_device_ids[i];
if (vendor == id->vendor &&
device == id->device &&
subvendor == id->subvendor &&
(subdevice == id->subdevice ||
id->subdevice == 0xffff))
return id->info;
}
return &ddb_none;
}
| linux-master | drivers/media/pci/ddbridge/ddbridge-hw.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-core.c: Digital Devices bridge core functions
*
* Copyright (C) 2010-2017 Digital Devices GmbH
* Marcus Metzler <[email protected]>
* Ralph Metzler <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/io.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <linux/swab.h>
#include <linux/vmalloc.h>
#include "ddbridge.h"
#include "ddbridge-i2c.h"
#include "ddbridge-regs.h"
#include "ddbridge-max.h"
#include "ddbridge-ci.h"
#include "ddbridge-io.h"
#include "tda18271c2dd.h"
#include "stv6110x.h"
#include "stv090x.h"
#include "lnbh24.h"
#include "drxk.h"
#include "stv0367.h"
#include "stv0367_priv.h"
#include "cxd2841er.h"
#include "tda18212.h"
#include "stv0910.h"
#include "stv6111.h"
#include "lnbh25.h"
#include "cxd2099.h"
#include "ddbridge-dummy-fe.h"
/****************************************************************************/
#define DDB_MAX_ADAPTER 64
/****************************************************************************/
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
static int adapter_alloc;
module_param(adapter_alloc, int, 0444);
MODULE_PARM_DESC(adapter_alloc,
"0-one adapter per io, 1-one per tab with io, 2-one per tab, 3-one for all");
static int ci_bitrate = 70000;
module_param(ci_bitrate, int, 0444);
MODULE_PARM_DESC(ci_bitrate, " Bitrate in KHz for output to CI.");
static int ts_loop = -1;
module_param(ts_loop, int, 0444);
MODULE_PARM_DESC(ts_loop, "TS in/out test loop on port ts_loop");
static int xo2_speed = 2;
module_param(xo2_speed, int, 0444);
MODULE_PARM_DESC(xo2_speed, "default transfer speed for xo2 based duoflex, 0=55,1=75,2=90,3=104 MBit/s, default=2, use attribute to change for individual cards");
#ifdef __arm__
static int alt_dma = 1;
#else
static int alt_dma;
#endif
module_param(alt_dma, int, 0444);
MODULE_PARM_DESC(alt_dma, "use alternative DMA buffer handling");
static int no_init;
module_param(no_init, int, 0444);
MODULE_PARM_DESC(no_init, "do not initialize most devices");
static int stv0910_single;
module_param(stv0910_single, int, 0444);
MODULE_PARM_DESC(stv0910_single, "use stv0910 cards as single demods");
static int dma_buf_num = 8;
module_param(dma_buf_num, int, 0444);
MODULE_PARM_DESC(dma_buf_num, "Number of DMA buffers, possible values: 8-32");
static int dma_buf_size = 21;
module_param(dma_buf_size, int, 0444);
MODULE_PARM_DESC(dma_buf_size,
"DMA buffer size as multiple of 128*47, possible values: 1-43");
static int dummy_tuner;
module_param(dummy_tuner, int, 0444);
MODULE_PARM_DESC(dummy_tuner,
"attach dummy tuner to port 0 on Octopus V3 or Octopus Mini cards");
/****************************************************************************/
static DEFINE_MUTEX(redirect_lock);
static struct workqueue_struct *ddb_wq;
static struct ddb *ddbs[DDB_MAX_ADAPTER];
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
struct ddb_irq *ddb_irq_set(struct ddb *dev, u32 link, u32 nr,
void (*handler)(void *), void *data)
{
struct ddb_irq *irq = &dev->link[link].irq[nr];
irq->handler = handler;
irq->data = data;
return irq;
}
static void ddb_set_dma_table(struct ddb_io *io)
{
struct ddb *dev = io->port->dev;
struct ddb_dma *dma = io->dma;
u32 i;
u64 mem;
if (!dma)
return;
for (i = 0; i < dma->num; i++) {
mem = dma->pbuf[i];
ddbwritel(dev, mem & 0xffffffff, dma->bufregs + i * 8);
ddbwritel(dev, mem >> 32, dma->bufregs + i * 8 + 4);
}
dma->bufval = ((dma->div & 0x0f) << 16) |
((dma->num & 0x1f) << 11) |
((dma->size >> 7) & 0x7ff);
}
static void ddb_set_dma_tables(struct ddb *dev)
{
u32 i;
for (i = 0; i < DDB_MAX_PORT; i++) {
if (dev->port[i].input[0])
ddb_set_dma_table(dev->port[i].input[0]);
if (dev->port[i].input[1])
ddb_set_dma_table(dev->port[i].input[1]);
if (dev->port[i].output)
ddb_set_dma_table(dev->port[i].output);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static void ddb_redirect_dma(struct ddb *dev,
struct ddb_dma *sdma,
struct ddb_dma *ddma)
{
u32 i, base;
u64 mem;
sdma->bufval = ddma->bufval;
base = sdma->bufregs;
for (i = 0; i < ddma->num; i++) {
mem = ddma->pbuf[i];
ddbwritel(dev, mem & 0xffffffff, base + i * 8);
ddbwritel(dev, mem >> 32, base + i * 8 + 4);
}
}
static int ddb_unredirect(struct ddb_port *port)
{
struct ddb_input *oredi, *iredi = NULL;
struct ddb_output *iredo = NULL;
/* dev_info(port->dev->dev,
* "unredirect %d.%d\n", port->dev->nr, port->nr);
*/
mutex_lock(&redirect_lock);
if (port->output->dma->running) {
mutex_unlock(&redirect_lock);
return -EBUSY;
}
oredi = port->output->redi;
if (!oredi)
goto done;
if (port->input[0]) {
iredi = port->input[0]->redi;
iredo = port->input[0]->redo;
if (iredo) {
iredo->port->output->redi = oredi;
if (iredo->port->input[0]) {
iredo->port->input[0]->redi = iredi;
ddb_redirect_dma(oredi->port->dev,
oredi->dma, iredo->dma);
}
port->input[0]->redo = NULL;
ddb_set_dma_table(port->input[0]);
}
oredi->redi = iredi;
port->input[0]->redi = NULL;
}
oredi->redo = NULL;
port->output->redi = NULL;
ddb_set_dma_table(oredi);
done:
mutex_unlock(&redirect_lock);
return 0;
}
static int ddb_redirect(u32 i, u32 p)
{
struct ddb *idev = ddbs[(i >> 4) & 0x3f];
struct ddb_input *input, *input2;
struct ddb *pdev = ddbs[(p >> 4) & 0x3f];
struct ddb_port *port;
if (!idev || !pdev)
return -EINVAL;
if (!idev->has_dma || !pdev->has_dma)
return -EINVAL;
port = &pdev->port[p & 0x0f];
if (!port->output)
return -EINVAL;
if (ddb_unredirect(port))
return -EBUSY;
if (i == 8)
return 0;
input = &idev->input[i & 7];
if (!input)
return -EINVAL;
mutex_lock(&redirect_lock);
if (port->output->dma->running || input->dma->running) {
mutex_unlock(&redirect_lock);
return -EBUSY;
}
input2 = port->input[0];
if (input2) {
if (input->redi) {
input2->redi = input->redi;
input->redi = NULL;
} else {
input2->redi = input;
}
}
input->redo = port->output;
port->output->redi = input;
ddb_redirect_dma(input->port->dev, input->dma, port->output->dma);
mutex_unlock(&redirect_lock);
return 0;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static void dma_free(struct pci_dev *pdev, struct ddb_dma *dma, int dir)
{
int i;
if (!dma)
return;
for (i = 0; i < dma->num; i++) {
if (dma->vbuf[i]) {
if (alt_dma) {
dma_unmap_single(&pdev->dev, dma->pbuf[i],
dma->size,
dir ? DMA_TO_DEVICE :
DMA_FROM_DEVICE);
kfree(dma->vbuf[i]);
dma->vbuf[i] = NULL;
} else {
dma_free_coherent(&pdev->dev, dma->size,
dma->vbuf[i], dma->pbuf[i]);
}
dma->vbuf[i] = NULL;
}
}
}
static int dma_alloc(struct pci_dev *pdev, struct ddb_dma *dma, int dir)
{
int i;
if (!dma)
return 0;
for (i = 0; i < dma->num; i++) {
if (alt_dma) {
dma->vbuf[i] = kmalloc(dma->size, __GFP_RETRY_MAYFAIL);
if (!dma->vbuf[i])
return -ENOMEM;
dma->pbuf[i] = dma_map_single(&pdev->dev,
dma->vbuf[i],
dma->size,
dir ? DMA_TO_DEVICE :
DMA_FROM_DEVICE);
if (dma_mapping_error(&pdev->dev, dma->pbuf[i])) {
kfree(dma->vbuf[i]);
dma->vbuf[i] = NULL;
return -ENOMEM;
}
} else {
dma->vbuf[i] = dma_alloc_coherent(&pdev->dev,
dma->size,
&dma->pbuf[i],
GFP_KERNEL);
if (!dma->vbuf[i])
return -ENOMEM;
}
}
return 0;
}
int ddb_buffers_alloc(struct ddb *dev)
{
int i;
struct ddb_port *port;
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
switch (port->class) {
case DDB_PORT_TUNER:
if (port->input[0]->dma)
if (dma_alloc(dev->pdev, port->input[0]->dma, 0)
< 0)
return -1;
if (port->input[1]->dma)
if (dma_alloc(dev->pdev, port->input[1]->dma, 0)
< 0)
return -1;
break;
case DDB_PORT_CI:
case DDB_PORT_LOOP:
if (port->input[0]->dma)
if (dma_alloc(dev->pdev, port->input[0]->dma, 0)
< 0)
return -1;
if (port->output->dma)
if (dma_alloc(dev->pdev, port->output->dma, 1)
< 0)
return -1;
break;
default:
break;
}
}
ddb_set_dma_tables(dev);
return 0;
}
void ddb_buffers_free(struct ddb *dev)
{
int i;
struct ddb_port *port;
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
if (port->input[0] && port->input[0]->dma)
dma_free(dev->pdev, port->input[0]->dma, 0);
if (port->input[1] && port->input[1]->dma)
dma_free(dev->pdev, port->input[1]->dma, 0);
if (port->output && port->output->dma)
dma_free(dev->pdev, port->output->dma, 1);
}
}
static void calc_con(struct ddb_output *output, u32 *con, u32 *con2, u32 flags)
{
struct ddb *dev = output->port->dev;
u32 bitrate = output->port->obr, max_bitrate = 72000;
u32 gap = 4, nco = 0;
*con = 0x1c;
if (output->port->gap != 0xffffffff) {
flags |= 1;
gap = output->port->gap;
max_bitrate = 0;
}
if (dev->link[0].info->type == DDB_OCTOPUS_CI && output->port->nr > 1) {
*con = 0x10c;
if (dev->link[0].ids.regmapid >= 0x10003 && !(flags & 1)) {
if (!(flags & 2)) {
/* NCO */
max_bitrate = 0;
gap = 0;
if (bitrate != 72000) {
if (bitrate >= 96000) {
*con |= 0x800;
} else {
*con |= 0x1000;
nco = (bitrate * 8192 + 71999)
/ 72000;
}
}
} else {
/* Divider and gap */
*con |= 0x1810;
if (bitrate <= 64000) {
max_bitrate = 64000;
nco = 8;
} else if (bitrate <= 72000) {
max_bitrate = 72000;
nco = 7;
} else {
max_bitrate = 96000;
nco = 5;
}
}
} else {
if (bitrate > 72000) {
*con |= 0x810; /* 96 MBit/s and gap */
max_bitrate = 96000;
}
*con |= 0x10; /* enable gap */
}
}
if (max_bitrate > 0) {
if (bitrate > max_bitrate)
bitrate = max_bitrate;
if (bitrate < 31000)
bitrate = 31000;
gap = ((max_bitrate - bitrate) * 94) / bitrate;
if (gap < 2)
*con &= ~0x10; /* Disable gap */
else
gap -= 2;
if (gap > 127)
gap = 127;
}
*con2 = (nco << 16) | gap;
}
static void ddb_output_start(struct ddb_output *output)
{
struct ddb *dev = output->port->dev;
u32 con = 0x11c, con2 = 0;
spin_lock_irq(&output->dma->lock);
output->dma->cbuf = 0;
output->dma->coff = 0;
output->dma->stat = 0;
ddbwritel(dev, 0, DMA_BUFFER_CONTROL(output->dma));
if (output->port->input[0]->port->class == DDB_PORT_LOOP)
con = (1UL << 13) | 0x14;
else
calc_con(output, &con, &con2, 0);
ddbwritel(dev, 0, TS_CONTROL(output));
ddbwritel(dev, 2, TS_CONTROL(output));
ddbwritel(dev, 0, TS_CONTROL(output));
ddbwritel(dev, con, TS_CONTROL(output));
ddbwritel(dev, con2, TS_CONTROL2(output));
ddbwritel(dev, output->dma->bufval,
DMA_BUFFER_SIZE(output->dma));
ddbwritel(dev, 0, DMA_BUFFER_ACK(output->dma));
ddbwritel(dev, 1, DMA_BASE_READ);
ddbwritel(dev, 7, DMA_BUFFER_CONTROL(output->dma));
ddbwritel(dev, con | 1, TS_CONTROL(output));
output->dma->running = 1;
spin_unlock_irq(&output->dma->lock);
}
static void ddb_output_stop(struct ddb_output *output)
{
struct ddb *dev = output->port->dev;
spin_lock_irq(&output->dma->lock);
ddbwritel(dev, 0, TS_CONTROL(output));
ddbwritel(dev, 0, DMA_BUFFER_CONTROL(output->dma));
output->dma->running = 0;
spin_unlock_irq(&output->dma->lock);
}
static void ddb_input_stop(struct ddb_input *input)
{
struct ddb *dev = input->port->dev;
u32 tag = DDB_LINK_TAG(input->port->lnr);
spin_lock_irq(&input->dma->lock);
ddbwritel(dev, 0, tag | TS_CONTROL(input));
ddbwritel(dev, 0, DMA_BUFFER_CONTROL(input->dma));
input->dma->running = 0;
spin_unlock_irq(&input->dma->lock);
}
static void ddb_input_start(struct ddb_input *input)
{
struct ddb *dev = input->port->dev;
spin_lock_irq(&input->dma->lock);
input->dma->cbuf = 0;
input->dma->coff = 0;
input->dma->stat = 0;
ddbwritel(dev, 0, DMA_BUFFER_CONTROL(input->dma));
ddbwritel(dev, 0, TS_CONTROL(input));
ddbwritel(dev, 2, TS_CONTROL(input));
ddbwritel(dev, 0, TS_CONTROL(input));
ddbwritel(dev, input->dma->bufval,
DMA_BUFFER_SIZE(input->dma));
ddbwritel(dev, 0, DMA_BUFFER_ACK(input->dma));
ddbwritel(dev, 1, DMA_BASE_WRITE);
ddbwritel(dev, 3, DMA_BUFFER_CONTROL(input->dma));
ddbwritel(dev, 0x09, TS_CONTROL(input));
if (input->port->type == DDB_TUNER_DUMMY)
ddbwritel(dev, 0x000fff01, TS_CONTROL2(input));
input->dma->running = 1;
spin_unlock_irq(&input->dma->lock);
}
static void ddb_input_start_all(struct ddb_input *input)
{
struct ddb_input *i = input;
struct ddb_output *o;
mutex_lock(&redirect_lock);
while (i && (o = i->redo)) {
ddb_output_start(o);
i = o->port->input[0];
if (i)
ddb_input_start(i);
}
ddb_input_start(input);
mutex_unlock(&redirect_lock);
}
static void ddb_input_stop_all(struct ddb_input *input)
{
struct ddb_input *i = input;
struct ddb_output *o;
mutex_lock(&redirect_lock);
ddb_input_stop(input);
while (i && (o = i->redo)) {
ddb_output_stop(o);
i = o->port->input[0];
if (i)
ddb_input_stop(i);
}
mutex_unlock(&redirect_lock);
}
static u32 ddb_output_free(struct ddb_output *output)
{
u32 idx, off, stat = output->dma->stat;
s32 diff;
idx = (stat >> 11) & 0x1f;
off = (stat & 0x7ff) << 7;
if (output->dma->cbuf != idx) {
if ((((output->dma->cbuf + 1) % output->dma->num) == idx) &&
(output->dma->size - output->dma->coff <= (2 * 188)))
return 0;
return 188;
}
diff = off - output->dma->coff;
if (diff <= 0 || diff > (2 * 188))
return 188;
return 0;
}
static ssize_t ddb_output_write(struct ddb_output *output,
const __user u8 *buf, size_t count)
{
struct ddb *dev = output->port->dev;
u32 idx, off, stat = output->dma->stat;
u32 left = count, len;
idx = (stat >> 11) & 0x1f;
off = (stat & 0x7ff) << 7;
while (left) {
len = output->dma->size - output->dma->coff;
if ((((output->dma->cbuf + 1) % output->dma->num) == idx) &&
off == 0) {
if (len <= 188)
break;
len -= 188;
}
if (output->dma->cbuf == idx) {
if (off > output->dma->coff) {
len = off - output->dma->coff;
len -= (len % 188);
if (len <= 188)
break;
len -= 188;
}
}
if (len > left)
len = left;
if (copy_from_user(output->dma->vbuf[output->dma->cbuf] +
output->dma->coff,
buf, len))
return -EIO;
if (alt_dma)
dma_sync_single_for_device(
dev->dev,
output->dma->pbuf[output->dma->cbuf],
output->dma->size, DMA_TO_DEVICE);
left -= len;
buf += len;
output->dma->coff += len;
if (output->dma->coff == output->dma->size) {
output->dma->coff = 0;
output->dma->cbuf = ((output->dma->cbuf + 1) %
output->dma->num);
}
ddbwritel(dev,
(output->dma->cbuf << 11) |
(output->dma->coff >> 7),
DMA_BUFFER_ACK(output->dma));
}
return count - left;
}
static u32 ddb_input_avail(struct ddb_input *input)
{
struct ddb *dev = input->port->dev;
u32 idx, off, stat = input->dma->stat;
u32 ctrl = ddbreadl(dev, DMA_BUFFER_CONTROL(input->dma));
idx = (stat >> 11) & 0x1f;
off = (stat & 0x7ff) << 7;
if (ctrl & 4) {
dev_err(dev->dev, "IA %d %d %08x\n", idx, off, ctrl);
ddbwritel(dev, stat, DMA_BUFFER_ACK(input->dma));
return 0;
}
if (input->dma->cbuf != idx)
return 188;
return 0;
}
static ssize_t ddb_input_read(struct ddb_input *input,
__user u8 *buf, size_t count)
{
struct ddb *dev = input->port->dev;
u32 left = count;
u32 idx, free, stat = input->dma->stat;
int ret;
idx = (stat >> 11) & 0x1f;
while (left) {
if (input->dma->cbuf == idx)
return count - left;
free = input->dma->size - input->dma->coff;
if (free > left)
free = left;
if (alt_dma)
dma_sync_single_for_cpu(
dev->dev,
input->dma->pbuf[input->dma->cbuf],
input->dma->size, DMA_FROM_DEVICE);
ret = copy_to_user(buf, input->dma->vbuf[input->dma->cbuf] +
input->dma->coff, free);
if (ret)
return -EFAULT;
input->dma->coff += free;
if (input->dma->coff == input->dma->size) {
input->dma->coff = 0;
input->dma->cbuf = (input->dma->cbuf + 1) %
input->dma->num;
}
left -= free;
buf += free;
ddbwritel(dev,
(input->dma->cbuf << 11) | (input->dma->coff >> 7),
DMA_BUFFER_ACK(input->dma));
}
return count;
}
/****************************************************************************/
/****************************************************************************/
static ssize_t ts_write(struct file *file, const __user char *buf,
size_t count, loff_t *ppos)
{
struct dvb_device *dvbdev = file->private_data;
struct ddb_output *output = dvbdev->priv;
struct ddb *dev = output->port->dev;
size_t left = count;
int stat;
if (!dev->has_dma)
return -EINVAL;
while (left) {
if (ddb_output_free(output) < 188) {
if (file->f_flags & O_NONBLOCK)
break;
if (wait_event_interruptible(
output->dma->wq,
ddb_output_free(output) >= 188) < 0)
break;
}
stat = ddb_output_write(output, buf, left);
if (stat < 0)
return stat;
buf += stat;
left -= stat;
}
return (left == count) ? -EAGAIN : (count - left);
}
static ssize_t ts_read(struct file *file, __user char *buf,
size_t count, loff_t *ppos)
{
struct dvb_device *dvbdev = file->private_data;
struct ddb_output *output = dvbdev->priv;
struct ddb_input *input = output->port->input[0];
struct ddb *dev = output->port->dev;
size_t left = count;
int stat;
if (!dev->has_dma)
return -EINVAL;
while (left) {
if (ddb_input_avail(input) < 188) {
if (file->f_flags & O_NONBLOCK)
break;
if (wait_event_interruptible(
input->dma->wq,
ddb_input_avail(input) >= 188) < 0)
break;
}
stat = ddb_input_read(input, buf, left);
if (stat < 0)
return stat;
left -= stat;
buf += stat;
}
return (count && (left == count)) ? -EAGAIN : (count - left);
}
static __poll_t ts_poll(struct file *file, poll_table *wait)
{
struct dvb_device *dvbdev = file->private_data;
struct ddb_output *output = dvbdev->priv;
struct ddb_input *input = output->port->input[0];
__poll_t mask = 0;
poll_wait(file, &input->dma->wq, wait);
poll_wait(file, &output->dma->wq, wait);
if (ddb_input_avail(input) >= 188)
mask |= EPOLLIN | EPOLLRDNORM;
if (ddb_output_free(output) >= 188)
mask |= EPOLLOUT | EPOLLWRNORM;
return mask;
}
static int ts_release(struct inode *inode, struct file *file)
{
struct dvb_device *dvbdev = file->private_data;
struct ddb_output *output = NULL;
struct ddb_input *input = NULL;
if (dvbdev) {
output = dvbdev->priv;
input = output->port->input[0];
}
if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
if (!input)
return -EINVAL;
ddb_input_stop(input);
} else if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
if (!output)
return -EINVAL;
ddb_output_stop(output);
}
return dvb_generic_release(inode, file);
}
static int ts_open(struct inode *inode, struct file *file)
{
int err;
struct dvb_device *dvbdev = file->private_data;
struct ddb_output *output = NULL;
struct ddb_input *input = NULL;
if (dvbdev) {
output = dvbdev->priv;
input = output->port->input[0];
}
if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
if (!input)
return -EINVAL;
if (input->redo || input->redi)
return -EBUSY;
} else if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
if (!output)
return -EINVAL;
} else {
return -EINVAL;
}
err = dvb_generic_open(inode, file);
if (err < 0)
return err;
if ((file->f_flags & O_ACCMODE) == O_RDONLY)
ddb_input_start(input);
else if ((file->f_flags & O_ACCMODE) == O_WRONLY)
ddb_output_start(output);
return err;
}
static const struct file_operations ci_fops = {
.owner = THIS_MODULE,
.read = ts_read,
.write = ts_write,
.open = ts_open,
.release = ts_release,
.poll = ts_poll,
.mmap = NULL,
};
static struct dvb_device dvbdev_ci = {
.priv = NULL,
.readers = 1,
.writers = 1,
.users = 2,
.fops = &ci_fops,
};
/****************************************************************************/
/****************************************************************************/
static int locked_gate_ctrl(struct dvb_frontend *fe, int enable)
{
struct ddb_input *input = fe->sec_priv;
struct ddb_port *port = input->port;
struct ddb_dvb *dvb = &port->dvb[input->nr & 1];
int status;
if (enable) {
mutex_lock(&port->i2c_gate_lock);
status = dvb->i2c_gate_ctrl(fe, 1);
} else {
status = dvb->i2c_gate_ctrl(fe, 0);
mutex_unlock(&port->i2c_gate_lock);
}
return status;
}
static int demod_attach_drxk(struct ddb_input *input)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct drxk_config config;
memset(&config, 0, sizeof(config));
config.adr = 0x29 + (input->nr & 1);
config.microcode_name = "drxk_a3.mc";
dvb->fe = dvb_attach(drxk_attach, &config, i2c);
if (!dvb->fe) {
dev_err(dev, "No DRXK found!\n");
return -ENODEV;
}
dvb->fe->sec_priv = input;
dvb->i2c_gate_ctrl = dvb->fe->ops.i2c_gate_ctrl;
dvb->fe->ops.i2c_gate_ctrl = locked_gate_ctrl;
return 0;
}
static int tuner_attach_tda18271(struct ddb_input *input)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct dvb_frontend *fe;
if (dvb->fe->ops.i2c_gate_ctrl)
dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 1);
fe = dvb_attach(tda18271c2dd_attach, dvb->fe, i2c, 0x60);
if (dvb->fe->ops.i2c_gate_ctrl)
dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 0);
if (!fe) {
dev_err(dev, "No TDA18271 found!\n");
return -ENODEV;
}
return 0;
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
static struct stv0367_config ddb_stv0367_config[] = {
{
.demod_address = 0x1f,
.xtal = 27000000,
.if_khz = 0,
.if_iq_mode = FE_TER_NORMAL_IF_TUNER,
.ts_mode = STV0367_SERIAL_PUNCT_CLOCK,
.clk_pol = STV0367_CLOCKPOLARITY_DEFAULT,
}, {
.demod_address = 0x1e,
.xtal = 27000000,
.if_khz = 0,
.if_iq_mode = FE_TER_NORMAL_IF_TUNER,
.ts_mode = STV0367_SERIAL_PUNCT_CLOCK,
.clk_pol = STV0367_CLOCKPOLARITY_DEFAULT,
},
};
static int demod_attach_stv0367(struct ddb_input *input)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
/* attach frontend */
dvb->fe = dvb_attach(stv0367ddb_attach,
&ddb_stv0367_config[(input->nr & 1)], i2c);
if (!dvb->fe) {
dev_err(dev, "No stv0367 found!\n");
return -ENODEV;
}
dvb->fe->sec_priv = input;
dvb->i2c_gate_ctrl = dvb->fe->ops.i2c_gate_ctrl;
dvb->fe->ops.i2c_gate_ctrl = locked_gate_ctrl;
return 0;
}
static int tuner_tda18212_ping(struct ddb_input *input, unsigned short adr)
{
struct i2c_adapter *adapter = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
u8 tda_id[2];
u8 subaddr = 0x00;
dev_dbg(dev, "stv0367-tda18212 tuner ping\n");
if (dvb->fe->ops.i2c_gate_ctrl)
dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 1);
if (i2c_read_regs(adapter, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
dev_dbg(dev, "tda18212 ping 1 fail\n");
if (i2c_read_regs(adapter, adr, subaddr, tda_id, sizeof(tda_id)) < 0)
dev_warn(dev, "tda18212 ping failed, expect problems\n");
if (dvb->fe->ops.i2c_gate_ctrl)
dvb->fe->ops.i2c_gate_ctrl(dvb->fe, 0);
return 0;
}
static int demod_attach_cxd28xx(struct ddb_input *input, int par, int osc24)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct cxd2841er_config cfg;
/* the cxd2841er driver expects 8bit/shifted I2C addresses */
cfg.i2c_addr = ((input->nr & 1) ? 0x6d : 0x6c) << 1;
cfg.xtal = osc24 ? SONY_XTAL_24000 : SONY_XTAL_20500;
cfg.flags = CXD2841ER_AUTO_IFHZ | CXD2841ER_EARLY_TUNE |
CXD2841ER_NO_WAIT_LOCK | CXD2841ER_NO_AGCNEG |
CXD2841ER_TSBITS;
if (!par)
cfg.flags |= CXD2841ER_TS_SERIAL;
/* attach frontend */
dvb->fe = dvb_attach(cxd2841er_attach_t_c, &cfg, i2c);
if (!dvb->fe) {
dev_err(dev, "No cxd2837/38/43/54 found!\n");
return -ENODEV;
}
dvb->fe->sec_priv = input;
dvb->i2c_gate_ctrl = dvb->fe->ops.i2c_gate_ctrl;
dvb->fe->ops.i2c_gate_ctrl = locked_gate_ctrl;
return 0;
}
static int tuner_attach_tda18212(struct ddb_input *input, u32 porttype)
{
struct i2c_adapter *adapter = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct i2c_client *client;
struct tda18212_config config = {
.fe = dvb->fe,
.if_dvbt_6 = 3550,
.if_dvbt_7 = 3700,
.if_dvbt_8 = 4150,
.if_dvbt2_6 = 3250,
.if_dvbt2_7 = 4000,
.if_dvbt2_8 = 4000,
.if_dvbc = 5000,
};
u8 addr = (input->nr & 1) ? 0x63 : 0x60;
/* due to a hardware quirk with the I2C gate on the stv0367+tda18212
* combo, the tda18212 must be probed by reading it's id _twice_ when
* cold started, or it very likely will fail.
*/
if (porttype == DDB_TUNER_DVBCT_ST)
tuner_tda18212_ping(input, addr);
/* perform tuner probe/init/attach */
client = dvb_module_probe("tda18212", NULL, adapter, addr, &config);
if (!client)
goto err;
dvb->i2c_client[0] = client;
return 0;
err:
dev_err(dev, "TDA18212 tuner not found. Device is not fully operational.\n");
return -ENODEV;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static struct stv090x_config stv0900 = {
.device = STV0900,
.demod_mode = STV090x_DUAL,
.clk_mode = STV090x_CLK_EXT,
.xtal = 27000000,
.address = 0x69,
.ts1_mode = STV090x_TSMODE_SERIAL_PUNCTURED,
.ts2_mode = STV090x_TSMODE_SERIAL_PUNCTURED,
.ts1_tei = 1,
.ts2_tei = 1,
.repeater_level = STV090x_RPTLEVEL_16,
.adc1_range = STV090x_ADC_1Vpp,
.adc2_range = STV090x_ADC_1Vpp,
.diseqc_envelope_mode = true,
};
static struct stv090x_config stv0900_aa = {
.device = STV0900,
.demod_mode = STV090x_DUAL,
.clk_mode = STV090x_CLK_EXT,
.xtal = 27000000,
.address = 0x68,
.ts1_mode = STV090x_TSMODE_SERIAL_PUNCTURED,
.ts2_mode = STV090x_TSMODE_SERIAL_PUNCTURED,
.ts1_tei = 1,
.ts2_tei = 1,
.repeater_level = STV090x_RPTLEVEL_16,
.adc1_range = STV090x_ADC_1Vpp,
.adc2_range = STV090x_ADC_1Vpp,
.diseqc_envelope_mode = true,
};
static struct stv6110x_config stv6110a = {
.addr = 0x60,
.refclk = 27000000,
.clk_div = 1,
};
static struct stv6110x_config stv6110b = {
.addr = 0x63,
.refclk = 27000000,
.clk_div = 1,
};
static int demod_attach_stv0900(struct ddb_input *input, int type)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct stv090x_config *feconf = type ? &stv0900_aa : &stv0900;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
dvb->fe = dvb_attach(stv090x_attach, feconf, i2c,
(input->nr & 1) ? STV090x_DEMODULATOR_1
: STV090x_DEMODULATOR_0);
if (!dvb->fe) {
dev_err(dev, "No STV0900 found!\n");
return -ENODEV;
}
if (!dvb_attach(lnbh24_attach, dvb->fe, i2c, 0,
0, (input->nr & 1) ?
(0x09 - type) : (0x0b - type))) {
dev_err(dev, "No LNBH24 found!\n");
dvb_frontend_detach(dvb->fe);
return -ENODEV;
}
return 0;
}
static int tuner_attach_stv6110(struct ddb_input *input, int type)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct stv090x_config *feconf = type ? &stv0900_aa : &stv0900;
struct stv6110x_config *tunerconf = (input->nr & 1) ?
&stv6110b : &stv6110a;
const struct stv6110x_devctl *ctl;
ctl = dvb_attach(stv6110x_attach, dvb->fe, tunerconf, i2c);
if (!ctl) {
dev_err(dev, "No STV6110X found!\n");
return -ENODEV;
}
dev_info(dev, "attach tuner input %d adr %02x\n",
input->nr, tunerconf->addr);
feconf->tuner_init = ctl->tuner_init;
feconf->tuner_sleep = ctl->tuner_sleep;
feconf->tuner_set_mode = ctl->tuner_set_mode;
feconf->tuner_set_frequency = ctl->tuner_set_frequency;
feconf->tuner_get_frequency = ctl->tuner_get_frequency;
feconf->tuner_set_bandwidth = ctl->tuner_set_bandwidth;
feconf->tuner_get_bandwidth = ctl->tuner_get_bandwidth;
feconf->tuner_set_bbgain = ctl->tuner_set_bbgain;
feconf->tuner_get_bbgain = ctl->tuner_get_bbgain;
feconf->tuner_set_refclk = ctl->tuner_set_refclk;
feconf->tuner_get_status = ctl->tuner_get_status;
return 0;
}
static const struct stv0910_cfg stv0910_p = {
.adr = 0x68,
.parallel = 1,
.rptlvl = 4,
.clk = 30000000,
.tsspeed = 0x28,
};
static const struct lnbh25_config lnbh25_cfg = {
.i2c_address = 0x0c << 1,
.data2_config = LNBH25_TEN
};
static int has_lnbh25(struct i2c_adapter *i2c, u8 adr)
{
u8 val;
return i2c_read_reg(i2c, adr, 0, &val) ? 0 : 1;
}
static int demod_attach_stv0910(struct ddb_input *input, int type, int tsfast)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct stv0910_cfg cfg = stv0910_p;
struct lnbh25_config lnbcfg = lnbh25_cfg;
if (stv0910_single)
cfg.single = 1;
if (type)
cfg.parallel = 2;
if (tsfast) {
dev_info(dev, "Enabling stv0910 higher speed TS\n");
cfg.tsspeed = 0x10;
}
dvb->fe = dvb_attach(stv0910_attach, i2c, &cfg, (input->nr & 1));
if (!dvb->fe) {
cfg.adr = 0x6c;
dvb->fe = dvb_attach(stv0910_attach, i2c,
&cfg, (input->nr & 1));
}
if (!dvb->fe) {
dev_err(dev, "No STV0910 found!\n");
return -ENODEV;
}
/* attach lnbh25 - leftshift by one as the lnbh25 driver expects 8bit
* i2c addresses
*/
if (has_lnbh25(i2c, 0x0d))
lnbcfg.i2c_address = (((input->nr & 1) ? 0x0d : 0x0c) << 1);
else
lnbcfg.i2c_address = (((input->nr & 1) ? 0x09 : 0x08) << 1);
if (!dvb_attach(lnbh25_attach, dvb->fe, &lnbcfg, i2c)) {
dev_err(dev, "No LNBH25 found!\n");
dvb_frontend_detach(dvb->fe);
return -ENODEV;
}
return 0;
}
static int tuner_attach_stv6111(struct ddb_input *input, int type)
{
struct i2c_adapter *i2c = &input->port->i2c->adap;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
struct dvb_frontend *fe;
u8 adr = (type ? 0 : 4) + ((input->nr & 1) ? 0x63 : 0x60);
fe = dvb_attach(stv6111_attach, dvb->fe, i2c, adr);
if (!fe) {
fe = dvb_attach(stv6111_attach, dvb->fe, i2c, adr & ~4);
if (!fe) {
dev_err(dev, "No STV6111 found at 0x%02x!\n", adr);
return -ENODEV;
}
}
return 0;
}
static int demod_attach_dummy(struct ddb_input *input)
{
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct device *dev = input->port->dev->dev;
dvb->fe = dvb_attach(ddbridge_dummy_fe_qam_attach);
if (!dvb->fe) {
dev_err(dev, "QAM dummy attach failed!\n");
return -ENODEV;
}
return 0;
}
static int start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
struct ddb_input *input = dvbdmx->priv;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
if (!dvb->users)
ddb_input_start_all(input);
return ++dvb->users;
}
static int stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
struct ddb_input *input = dvbdmx->priv;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
if (--dvb->users)
return dvb->users;
ddb_input_stop_all(input);
return 0;
}
static void dvb_input_detach(struct ddb_input *input)
{
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct dvb_demux *dvbdemux = &dvb->demux;
switch (dvb->attached) {
case 0x31:
if (dvb->fe2)
dvb_unregister_frontend(dvb->fe2);
if (dvb->fe)
dvb_unregister_frontend(dvb->fe);
fallthrough;
case 0x30:
dvb_module_release(dvb->i2c_client[0]);
dvb->i2c_client[0] = NULL;
if (dvb->fe2)
dvb_frontend_detach(dvb->fe2);
if (dvb->fe)
dvb_frontend_detach(dvb->fe);
dvb->fe = NULL;
dvb->fe2 = NULL;
fallthrough;
case 0x20:
dvb_net_release(&dvb->dvbnet);
fallthrough;
case 0x12:
dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
&dvb->hw_frontend);
dvbdemux->dmx.remove_frontend(&dvbdemux->dmx,
&dvb->mem_frontend);
fallthrough;
case 0x11:
dvb_dmxdev_release(&dvb->dmxdev);
fallthrough;
case 0x10:
dvb_dmx_release(&dvb->demux);
fallthrough;
case 0x01:
break;
}
dvb->attached = 0x00;
}
static int dvb_register_adapters(struct ddb *dev)
{
int i, ret = 0;
struct ddb_port *port;
struct dvb_adapter *adap;
if (adapter_alloc == 3) {
port = &dev->port[0];
adap = port->dvb[0].adap;
ret = dvb_register_adapter(adap, "DDBridge", THIS_MODULE,
port->dev->dev,
adapter_nr);
if (ret < 0)
return ret;
port->dvb[0].adap_registered = 1;
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
port->dvb[0].adap = adap;
port->dvb[1].adap = adap;
}
return 0;
}
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
switch (port->class) {
case DDB_PORT_TUNER:
adap = port->dvb[0].adap;
ret = dvb_register_adapter(adap, "DDBridge",
THIS_MODULE,
port->dev->dev,
adapter_nr);
if (ret < 0)
return ret;
port->dvb[0].adap_registered = 1;
if (adapter_alloc > 0) {
port->dvb[1].adap = port->dvb[0].adap;
break;
}
adap = port->dvb[1].adap;
ret = dvb_register_adapter(adap, "DDBridge",
THIS_MODULE,
port->dev->dev,
adapter_nr);
if (ret < 0)
return ret;
port->dvb[1].adap_registered = 1;
break;
case DDB_PORT_CI:
case DDB_PORT_LOOP:
adap = port->dvb[0].adap;
ret = dvb_register_adapter(adap, "DDBridge",
THIS_MODULE,
port->dev->dev,
adapter_nr);
if (ret < 0)
return ret;
port->dvb[0].adap_registered = 1;
break;
default:
if (adapter_alloc < 2)
break;
adap = port->dvb[0].adap;
ret = dvb_register_adapter(adap, "DDBridge",
THIS_MODULE,
port->dev->dev,
adapter_nr);
if (ret < 0)
return ret;
port->dvb[0].adap_registered = 1;
break;
}
}
return ret;
}
static void dvb_unregister_adapters(struct ddb *dev)
{
int i;
struct ddb_port *port;
struct ddb_dvb *dvb;
for (i = 0; i < dev->link[0].info->port_num; i++) {
port = &dev->port[i];
dvb = &port->dvb[0];
if (dvb->adap_registered)
dvb_unregister_adapter(dvb->adap);
dvb->adap_registered = 0;
dvb = &port->dvb[1];
if (dvb->adap_registered)
dvb_unregister_adapter(dvb->adap);
dvb->adap_registered = 0;
}
}
static int dvb_input_attach(struct ddb_input *input)
{
int ret = 0;
struct ddb_dvb *dvb = &input->port->dvb[input->nr & 1];
struct ddb_port *port = input->port;
struct dvb_adapter *adap = dvb->adap;
struct dvb_demux *dvbdemux = &dvb->demux;
struct ddb_ids *devids = &input->port->dev->link[input->port->lnr].ids;
int par = 0, osc24 = 0, tsfast = 0;
/*
* Determine if bridges with stv0910 demods can run with fast TS and
* thus support high bandwidth transponders.
* STV0910_PR and STV0910_P tuner types covers all relevant bridges,
* namely the CineS2 V7(A) and the Octopus CI S2 Pro/Advanced. All
* DuoFlex S2 V4(A) have type=DDB_TUNER_DVBS_STV0910 without any suffix
* and are limited by the serial link to the bridge, thus won't work
* in fast TS mode.
*/
if (port->nr == 0 &&
(port->type == DDB_TUNER_DVBS_STV0910_PR ||
port->type == DDB_TUNER_DVBS_STV0910_P)) {
/* fast TS on port 0 requires FPGA version >= 1.7 */
if ((devids->hwid & 0x00ffffff) >= 0x00010007)
tsfast = 1;
}
dvb->attached = 0x01;
dvbdemux->priv = input;
dvbdemux->dmx.capabilities = DMX_TS_FILTERING |
DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
dvbdemux->start_feed = start_feed;
dvbdemux->stop_feed = stop_feed;
dvbdemux->filternum = 256;
dvbdemux->feednum = 256;
ret = dvb_dmx_init(dvbdemux);
if (ret < 0)
return ret;
dvb->attached = 0x10;
dvb->dmxdev.filternum = 256;
dvb->dmxdev.demux = &dvbdemux->dmx;
ret = dvb_dmxdev_init(&dvb->dmxdev, adap);
if (ret < 0)
goto err_detach;
dvb->attached = 0x11;
dvb->mem_frontend.source = DMX_MEMORY_FE;
dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->mem_frontend);
dvb->hw_frontend.source = DMX_FRONTEND_0;
dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->hw_frontend);
ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &dvb->hw_frontend);
if (ret < 0)
goto err_detach;
dvb->attached = 0x12;
ret = dvb_net_init(adap, &dvb->dvbnet, dvb->dmxdev.demux);
if (ret < 0)
goto err_detach;
dvb->attached = 0x20;
dvb->fe = NULL;
dvb->fe2 = NULL;
switch (port->type) {
case DDB_TUNER_MXL5XX:
if (ddb_fe_attach_mxl5xx(input) < 0)
goto err_detach;
break;
case DDB_TUNER_DVBS_ST:
if (demod_attach_stv0900(input, 0) < 0)
goto err_detach;
if (tuner_attach_stv6110(input, 0) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBS_ST_AA:
if (demod_attach_stv0900(input, 1) < 0)
goto err_detach;
if (tuner_attach_stv6110(input, 1) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBS_STV0910:
if (demod_attach_stv0910(input, 0, tsfast) < 0)
goto err_detach;
if (tuner_attach_stv6111(input, 0) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBS_STV0910_PR:
if (demod_attach_stv0910(input, 1, tsfast) < 0)
goto err_detach;
if (tuner_attach_stv6111(input, 1) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBS_STV0910_P:
if (demod_attach_stv0910(input, 0, tsfast) < 0)
goto err_detach;
if (tuner_attach_stv6111(input, 1) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBCT_TR:
if (demod_attach_drxk(input) < 0)
goto err_detach;
if (tuner_attach_tda18271(input) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBCT_ST:
if (demod_attach_stv0367(input) < 0)
goto err_detach;
if (tuner_attach_tda18212(input, port->type) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBC2T2I_SONY_P:
if (input->port->dev->link[input->port->lnr].info->ts_quirks &
TS_QUIRK_ALT_OSC)
osc24 = 0;
else
osc24 = 1;
fallthrough;
case DDB_TUNER_DVBCT2_SONY_P:
case DDB_TUNER_DVBC2T2_SONY_P:
case DDB_TUNER_ISDBT_SONY_P:
if (input->port->dev->link[input->port->lnr].info->ts_quirks
& TS_QUIRK_SERIAL)
par = 0;
else
par = 1;
if (demod_attach_cxd28xx(input, par, osc24) < 0)
goto err_detach;
if (tuner_attach_tda18212(input, port->type) < 0)
goto err_tuner;
break;
case DDB_TUNER_DVBC2T2I_SONY:
osc24 = 1;
fallthrough;
case DDB_TUNER_DVBCT2_SONY:
case DDB_TUNER_DVBC2T2_SONY:
case DDB_TUNER_ISDBT_SONY:
if (demod_attach_cxd28xx(input, 0, osc24) < 0)
goto err_detach;
if (tuner_attach_tda18212(input, port->type) < 0)
goto err_tuner;
break;
case DDB_TUNER_DUMMY:
if (demod_attach_dummy(input) < 0)
goto err_detach;
break;
case DDB_TUNER_MCI_SX8:
if (ddb_fe_attach_mci(input, port->type) < 0)
goto err_detach;
break;
default:
return 0;
}
dvb->attached = 0x30;
if (dvb->fe) {
if (dvb_register_frontend(adap, dvb->fe) < 0)
goto err_detach;
if (dvb->fe2) {
if (dvb_register_frontend(adap, dvb->fe2) < 0) {
dvb_unregister_frontend(dvb->fe);
goto err_detach;
}
dvb->fe2->tuner_priv = dvb->fe->tuner_priv;
memcpy(&dvb->fe2->ops.tuner_ops,
&dvb->fe->ops.tuner_ops,
sizeof(struct dvb_tuner_ops));
}
}
dvb->attached = 0x31;
return 0;
err_tuner:
dev_err(port->dev->dev, "tuner attach failed!\n");
if (dvb->fe2)
dvb_frontend_detach(dvb->fe2);
if (dvb->fe)
dvb_frontend_detach(dvb->fe);
err_detach:
dvb_input_detach(input);
/* return error from ret if set */
if (ret < 0)
return ret;
return -ENODEV;
}
static int port_has_encti(struct ddb_port *port)
{
struct device *dev = port->dev->dev;
u8 val;
int ret = i2c_read_reg(&port->i2c->adap, 0x20, 0, &val);
if (!ret)
dev_info(dev, "[0x20]=0x%02x\n", val);
return ret ? 0 : 1;
}
static int port_has_cxd(struct ddb_port *port, u8 *type)
{
u8 val;
u8 probe[4] = { 0xe0, 0x00, 0x00, 0x00 }, data[4];
struct i2c_msg msgs[2] = {{ .addr = 0x40, .flags = 0,
.buf = probe, .len = 4 },
{ .addr = 0x40, .flags = I2C_M_RD,
.buf = data, .len = 4 } };
val = i2c_transfer(&port->i2c->adap, msgs, 2);
if (val != 2)
return 0;
if (data[0] == 0x02 && data[1] == 0x2b && data[3] == 0x43)
*type = 2;
else
*type = 1;
return 1;
}
static int port_has_xo2(struct ddb_port *port, u8 *type, u8 *id)
{
u8 probe[1] = { 0x00 }, data[4];
if (i2c_io(&port->i2c->adap, 0x10, probe, 1, data, 4))
return 0;
if (data[0] == 'D' && data[1] == 'F') {
*id = data[2];
*type = 1;
return 1;
}
if (data[0] == 'C' && data[1] == 'I') {
*id = data[2];
*type = 2;
return 1;
}
return 0;
}
static int port_has_stv0900(struct ddb_port *port)
{
u8 val;
if (i2c_read_reg16(&port->i2c->adap, 0x69, 0xf100, &val) < 0)
return 0;
return 1;
}
static int port_has_stv0900_aa(struct ddb_port *port, u8 *id)
{
if (i2c_read_reg16(&port->i2c->adap, 0x68, 0xf100, id) < 0)
return 0;
return 1;
}
static int port_has_drxks(struct ddb_port *port)
{
u8 val;
if (i2c_read(&port->i2c->adap, 0x29, &val) < 0)
return 0;
if (i2c_read(&port->i2c->adap, 0x2a, &val) < 0)
return 0;
return 1;
}
static int port_has_stv0367(struct ddb_port *port)
{
u8 val;
if (i2c_read_reg16(&port->i2c->adap, 0x1e, 0xf000, &val) < 0)
return 0;
if (val != 0x60)
return 0;
if (i2c_read_reg16(&port->i2c->adap, 0x1f, 0xf000, &val) < 0)
return 0;
if (val != 0x60)
return 0;
return 1;
}
static int init_xo2(struct ddb_port *port)
{
struct i2c_adapter *i2c = &port->i2c->adap;
struct ddb *dev = port->dev;
u8 val, data[2];
int res;
res = i2c_read_regs(i2c, 0x10, 0x04, data, 2);
if (res < 0)
return res;
if (data[0] != 0x01) {
dev_info(dev->dev, "Port %d: invalid XO2\n", port->nr);
return -1;
}
i2c_read_reg(i2c, 0x10, 0x08, &val);
if (val != 0) {
i2c_write_reg(i2c, 0x10, 0x08, 0x00);
msleep(100);
}
/* Enable tuner power, disable pll, reset demods */
i2c_write_reg(i2c, 0x10, 0x08, 0x04);
usleep_range(2000, 3000);
/* Release demod resets */
i2c_write_reg(i2c, 0x10, 0x08, 0x07);
/* speed: 0=55,1=75,2=90,3=104 MBit/s */
i2c_write_reg(i2c, 0x10, 0x09, xo2_speed);
if (dev->link[port->lnr].info->con_clock) {
dev_info(dev->dev, "Setting continuous clock for XO2\n");
i2c_write_reg(i2c, 0x10, 0x0a, 0x03);
i2c_write_reg(i2c, 0x10, 0x0b, 0x03);
} else {
i2c_write_reg(i2c, 0x10, 0x0a, 0x01);
i2c_write_reg(i2c, 0x10, 0x0b, 0x01);
}
usleep_range(2000, 3000);
/* Start XO2 PLL */
i2c_write_reg(i2c, 0x10, 0x08, 0x87);
return 0;
}
static int init_xo2_ci(struct ddb_port *port)
{
struct i2c_adapter *i2c = &port->i2c->adap;
struct ddb *dev = port->dev;
u8 val, data[2];
int res;
res = i2c_read_regs(i2c, 0x10, 0x04, data, 2);
if (res < 0)
return res;
if (data[0] > 1) {
dev_info(dev->dev, "Port %d: invalid XO2 CI %02x\n",
port->nr, data[0]);
return -1;
}
dev_info(dev->dev, "Port %d: DuoFlex CI %u.%u\n",
port->nr, data[0], data[1]);
i2c_read_reg(i2c, 0x10, 0x08, &val);
if (val != 0) {
i2c_write_reg(i2c, 0x10, 0x08, 0x00);
msleep(100);
}
/* Enable both CI */
i2c_write_reg(i2c, 0x10, 0x08, 3);
usleep_range(2000, 3000);
/* speed: 0=55,1=75,2=90,3=104 MBit/s */
i2c_write_reg(i2c, 0x10, 0x09, 1);
i2c_write_reg(i2c, 0x10, 0x08, 0x83);
usleep_range(2000, 3000);
if (dev->link[port->lnr].info->con_clock) {
dev_info(dev->dev, "Setting continuous clock for DuoFlex CI\n");
i2c_write_reg(i2c, 0x10, 0x0a, 0x03);
i2c_write_reg(i2c, 0x10, 0x0b, 0x03);
} else {
i2c_write_reg(i2c, 0x10, 0x0a, 0x01);
i2c_write_reg(i2c, 0x10, 0x0b, 0x01);
}
return 0;
}
static int port_has_cxd28xx(struct ddb_port *port, u8 *id)
{
struct i2c_adapter *i2c = &port->i2c->adap;
int status;
status = i2c_write_reg(&port->i2c->adap, 0x6e, 0, 0);
if (status)
return 0;
status = i2c_read_reg(i2c, 0x6e, 0xfd, id);
if (status)
return 0;
return 1;
}
static char *xo2names[] = {
"DUAL DVB-S2", "DUAL DVB-C/T/T2",
"DUAL DVB-ISDBT", "DUAL DVB-C/C2/T/T2",
"DUAL ATSC", "DUAL DVB-C/C2/T/T2,ISDB-T",
"", ""
};
static char *xo2types[] = {
"DVBS_ST", "DVBCT2_SONY",
"ISDBT_SONY", "DVBC2T2_SONY",
"ATSC_ST", "DVBC2T2I_SONY"
};
static void ddb_port_probe(struct ddb_port *port)
{
struct ddb *dev = port->dev;
u32 l = port->lnr;
struct ddb_link *link = &dev->link[l];
u8 id, type;
port->name = "NO MODULE";
port->type_name = "NONE";
port->class = DDB_PORT_NONE;
/* Handle missing ports and ports without I2C */
if (dummy_tuner && !port->nr &&
link->ids.device == 0x0005) {
port->name = "DUMMY";
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_DUMMY;
port->type_name = "DUMMY";
return;
}
if (port->nr == ts_loop) {
port->name = "TS LOOP";
port->class = DDB_PORT_LOOP;
return;
}
if (port->nr == 1 && link->info->type == DDB_OCTOPUS_CI &&
link->info->i2c_mask == 1) {
port->name = "NO TAB";
port->class = DDB_PORT_NONE;
return;
}
if (link->info->type == DDB_OCTOPUS_MAX) {
port->name = "DUAL DVB-S2 MAX";
port->type_name = "MXL5XX";
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_MXL5XX;
if (port->i2c)
ddbwritel(dev, I2C_SPEED_400,
port->i2c->regs + I2C_TIMING);
return;
}
if (link->info->type == DDB_OCTOPUS_MCI) {
if (port->nr >= link->info->mci_ports)
return;
port->name = "DUAL MCI";
port->type_name = "MCI";
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_MCI + link->info->mci_type;
return;
}
if (port->nr > 1 && link->info->type == DDB_OCTOPUS_CI) {
port->name = "CI internal";
port->type_name = "INTERNAL";
port->class = DDB_PORT_CI;
port->type = DDB_CI_INTERNAL;
}
if (!port->i2c)
return;
/* Probe ports with I2C */
if (port_has_cxd(port, &id)) {
if (id == 1) {
port->name = "CI";
port->type_name = "CXD2099";
port->class = DDB_PORT_CI;
port->type = DDB_CI_EXTERNAL_SONY;
ddbwritel(dev, I2C_SPEED_400,
port->i2c->regs + I2C_TIMING);
} else {
dev_info(dev->dev, "Port %d: Uninitialized DuoFlex\n",
port->nr);
return;
}
} else if (port_has_xo2(port, &type, &id)) {
ddbwritel(dev, I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
/*dev_info(dev->dev, "XO2 ID %02x\n", id);*/
if (type == 2) {
port->name = "DuoFlex CI";
port->class = DDB_PORT_CI;
port->type = DDB_CI_EXTERNAL_XO2;
port->type_name = "CI_XO2";
init_xo2_ci(port);
return;
}
id >>= 2;
if (id > 5) {
port->name = "unknown XO2 DuoFlex";
port->type_name = "UNKNOWN";
} else {
port->name = xo2names[id];
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_XO2 + id;
port->type_name = xo2types[id];
init_xo2(port);
}
} else if (port_has_cxd28xx(port, &id)) {
switch (id) {
case 0xa4:
port->name = "DUAL DVB-C2T2 CXD2843";
port->type = DDB_TUNER_DVBC2T2_SONY_P;
port->type_name = "DVBC2T2_SONY";
break;
case 0xb1:
port->name = "DUAL DVB-CT2 CXD2837";
port->type = DDB_TUNER_DVBCT2_SONY_P;
port->type_name = "DVBCT2_SONY";
break;
case 0xb0:
port->name = "DUAL ISDB-T CXD2838";
port->type = DDB_TUNER_ISDBT_SONY_P;
port->type_name = "ISDBT_SONY";
break;
case 0xc1:
port->name = "DUAL DVB-C2T2 ISDB-T CXD2854";
port->type = DDB_TUNER_DVBC2T2I_SONY_P;
port->type_name = "DVBC2T2I_ISDBT_SONY";
break;
default:
return;
}
port->class = DDB_PORT_TUNER;
ddbwritel(dev, I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
} else if (port_has_stv0900(port)) {
port->name = "DUAL DVB-S2";
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_DVBS_ST;
port->type_name = "DVBS_ST";
ddbwritel(dev, I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
} else if (port_has_stv0900_aa(port, &id)) {
port->name = "DUAL DVB-S2";
port->class = DDB_PORT_TUNER;
if (id == 0x51) {
if (port->nr == 0 &&
link->info->ts_quirks & TS_QUIRK_REVERSED)
port->type = DDB_TUNER_DVBS_STV0910_PR;
else
port->type = DDB_TUNER_DVBS_STV0910_P;
port->type_name = "DVBS_ST_0910";
} else {
port->type = DDB_TUNER_DVBS_ST_AA;
port->type_name = "DVBS_ST_AA";
}
ddbwritel(dev, I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
} else if (port_has_drxks(port)) {
port->name = "DUAL DVB-C/T";
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_DVBCT_TR;
port->type_name = "DVBCT_TR";
ddbwritel(dev, I2C_SPEED_400, port->i2c->regs + I2C_TIMING);
} else if (port_has_stv0367(port)) {
port->name = "DUAL DVB-C/T";
port->class = DDB_PORT_TUNER;
port->type = DDB_TUNER_DVBCT_ST;
port->type_name = "DVBCT_ST";
ddbwritel(dev, I2C_SPEED_100, port->i2c->regs + I2C_TIMING);
} else if (port_has_encti(port)) {
port->name = "ENCTI";
port->class = DDB_PORT_LOOP;
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static int ddb_port_attach(struct ddb_port *port)
{
int ret = 0;
switch (port->class) {
case DDB_PORT_TUNER:
ret = dvb_input_attach(port->input[0]);
if (ret < 0)
break;
ret = dvb_input_attach(port->input[1]);
if (ret < 0) {
dvb_input_detach(port->input[0]);
break;
}
port->input[0]->redi = port->input[0];
port->input[1]->redi = port->input[1];
break;
case DDB_PORT_CI:
ret = ddb_ci_attach(port, ci_bitrate);
if (ret < 0)
break;
fallthrough;
case DDB_PORT_LOOP:
ret = dvb_register_device(port->dvb[0].adap,
&port->dvb[0].dev,
&dvbdev_ci, (void *)port->output,
DVB_DEVICE_SEC, 0);
break;
default:
break;
}
if (ret < 0)
dev_err(port->dev->dev, "port_attach on port %d failed\n",
port->nr);
return ret;
}
int ddb_ports_attach(struct ddb *dev)
{
int i, numports, err_ports = 0, ret = 0;
struct ddb_port *port;
if (dev->port_num) {
ret = dvb_register_adapters(dev);
if (ret < 0) {
dev_err(dev->dev, "Registering adapters failed. Check DVB_MAX_ADAPTERS in config.\n");
return ret;
}
}
numports = dev->port_num;
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
if (port->class != DDB_PORT_NONE) {
ret = ddb_port_attach(port);
if (ret)
err_ports++;
} else {
numports--;
}
}
if (err_ports) {
if (err_ports == numports) {
dev_err(dev->dev, "All connected ports failed to initialise!\n");
return -ENODEV;
}
dev_warn(dev->dev, "%d of %d connected ports failed to initialise!\n",
err_ports, numports);
}
return 0;
}
void ddb_ports_detach(struct ddb *dev)
{
int i;
struct ddb_port *port;
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
switch (port->class) {
case DDB_PORT_TUNER:
dvb_input_detach(port->input[1]);
dvb_input_detach(port->input[0]);
break;
case DDB_PORT_CI:
case DDB_PORT_LOOP:
ddb_ci_detach(port);
break;
}
}
dvb_unregister_adapters(dev);
}
/* Copy input DMA pointers to output DMA and ACK. */
static void input_write_output(struct ddb_input *input,
struct ddb_output *output)
{
ddbwritel(output->port->dev,
input->dma->stat, DMA_BUFFER_ACK(output->dma));
output->dma->cbuf = (input->dma->stat >> 11) & 0x1f;
output->dma->coff = (input->dma->stat & 0x7ff) << 7;
}
static void output_ack_input(struct ddb_output *output,
struct ddb_input *input)
{
ddbwritel(input->port->dev,
output->dma->stat, DMA_BUFFER_ACK(input->dma));
}
static void input_write_dvb(struct ddb_input *input,
struct ddb_input *input2)
{
struct ddb_dvb *dvb = &input2->port->dvb[input2->nr & 1];
struct ddb_dma *dma, *dma2;
struct ddb *dev = input->port->dev;
int ack = 1;
dma = input->dma;
dma2 = input->dma;
/*
* if there also is an output connected, do not ACK.
* input_write_output will ACK.
*/
if (input->redo) {
dma2 = input->redo->dma;
ack = 0;
}
while (dma->cbuf != ((dma->stat >> 11) & 0x1f) ||
(4 & dma->ctrl)) {
if (4 & dma->ctrl) {
/* dev_err(dev->dev, "Overflow dma %d\n", dma->nr); */
ack = 1;
}
if (alt_dma)
dma_sync_single_for_cpu(dev->dev, dma2->pbuf[dma->cbuf],
dma2->size, DMA_FROM_DEVICE);
dvb_dmx_swfilter_packets(&dvb->demux,
dma2->vbuf[dma->cbuf],
dma2->size / 188);
dma->cbuf = (dma->cbuf + 1) % dma2->num;
if (ack)
ddbwritel(dev, (dma->cbuf << 11),
DMA_BUFFER_ACK(dma));
dma->stat = safe_ddbreadl(dev, DMA_BUFFER_CURRENT(dma));
dma->ctrl = safe_ddbreadl(dev, DMA_BUFFER_CONTROL(dma));
}
}
static void input_work(struct work_struct *work)
{
struct ddb_dma *dma = container_of(work, struct ddb_dma, work);
struct ddb_input *input = (struct ddb_input *)dma->io;
struct ddb *dev = input->port->dev;
unsigned long flags;
spin_lock_irqsave(&dma->lock, flags);
if (!dma->running) {
spin_unlock_irqrestore(&dma->lock, flags);
return;
}
dma->stat = ddbreadl(dev, DMA_BUFFER_CURRENT(dma));
dma->ctrl = ddbreadl(dev, DMA_BUFFER_CONTROL(dma));
if (input->redi)
input_write_dvb(input, input->redi);
if (input->redo)
input_write_output(input, input->redo);
wake_up(&dma->wq);
spin_unlock_irqrestore(&dma->lock, flags);
}
static void input_handler(void *data)
{
struct ddb_input *input = (struct ddb_input *)data;
struct ddb_dma *dma = input->dma;
queue_work(ddb_wq, &dma->work);
}
static void output_work(struct work_struct *work)
{
struct ddb_dma *dma = container_of(work, struct ddb_dma, work);
struct ddb_output *output = (struct ddb_output *)dma->io;
struct ddb *dev = output->port->dev;
unsigned long flags;
spin_lock_irqsave(&dma->lock, flags);
if (!dma->running)
goto unlock_exit;
dma->stat = ddbreadl(dev, DMA_BUFFER_CURRENT(dma));
dma->ctrl = ddbreadl(dev, DMA_BUFFER_CONTROL(dma));
if (output->redi)
output_ack_input(output, output->redi);
wake_up(&dma->wq);
unlock_exit:
spin_unlock_irqrestore(&dma->lock, flags);
}
static void output_handler(void *data)
{
struct ddb_output *output = (struct ddb_output *)data;
struct ddb_dma *dma = output->dma;
queue_work(ddb_wq, &dma->work);
}
/****************************************************************************/
/****************************************************************************/
static const struct ddb_regmap *io_regmap(struct ddb_io *io, int link)
{
const struct ddb_info *info;
if (link)
info = io->port->dev->link[io->port->lnr].info;
else
info = io->port->dev->link[0].info;
if (!info)
return NULL;
return info->regmap;
}
static void ddb_dma_init(struct ddb_io *io, int nr, int out)
{
struct ddb_dma *dma;
const struct ddb_regmap *rm = io_regmap(io, 0);
dma = out ? &io->port->dev->odma[nr] : &io->port->dev->idma[nr];
io->dma = dma;
dma->io = io;
spin_lock_init(&dma->lock);
init_waitqueue_head(&dma->wq);
if (out) {
INIT_WORK(&dma->work, output_work);
dma->regs = rm->odma->base + rm->odma->size * nr;
dma->bufregs = rm->odma_buf->base + rm->odma_buf->size * nr;
dma->num = dma_buf_num;
dma->size = dma_buf_size * 128 * 47;
dma->div = 1;
} else {
INIT_WORK(&dma->work, input_work);
dma->regs = rm->idma->base + rm->idma->size * nr;
dma->bufregs = rm->idma_buf->base + rm->idma_buf->size * nr;
dma->num = dma_buf_num;
dma->size = dma_buf_size * 128 * 47;
dma->div = 1;
}
ddbwritel(io->port->dev, 0, DMA_BUFFER_ACK(dma));
dev_dbg(io->port->dev->dev, "init link %u, io %u, dma %u, dmaregs %08x bufregs %08x\n",
io->port->lnr, io->nr, nr, dma->regs, dma->bufregs);
}
static void ddb_input_init(struct ddb_port *port, int nr, int pnr, int anr)
{
struct ddb *dev = port->dev;
struct ddb_input *input = &dev->input[anr];
const struct ddb_regmap *rm;
port->input[pnr] = input;
input->nr = nr;
input->port = port;
rm = io_regmap(input, 1);
input->regs = DDB_LINK_TAG(port->lnr) |
(rm->input->base + rm->input->size * nr);
dev_dbg(dev->dev, "init link %u, input %u, regs %08x\n",
port->lnr, nr, input->regs);
if (dev->has_dma) {
const struct ddb_regmap *rm0 = io_regmap(input, 0);
u32 base = rm0->irq_base_idma;
u32 dma_nr = nr;
if (port->lnr)
dma_nr += 32 + (port->lnr - 1) * 8;
dev_dbg(dev->dev, "init link %u, input %u, handler %u\n",
port->lnr, nr, dma_nr + base);
ddb_irq_set(dev, 0, dma_nr + base, &input_handler, input);
ddb_dma_init(input, dma_nr, 0);
}
}
static void ddb_output_init(struct ddb_port *port, int nr)
{
struct ddb *dev = port->dev;
struct ddb_output *output = &dev->output[nr];
const struct ddb_regmap *rm;
port->output = output;
output->nr = nr;
output->port = port;
rm = io_regmap(output, 1);
output->regs = DDB_LINK_TAG(port->lnr) |
(rm->output->base + rm->output->size * nr);
dev_dbg(dev->dev, "init link %u, output %u, regs %08x\n",
port->lnr, nr, output->regs);
if (dev->has_dma) {
const struct ddb_regmap *rm0 = io_regmap(output, 0);
u32 base = rm0->irq_base_odma;
ddb_irq_set(dev, 0, nr + base, &output_handler, output);
ddb_dma_init(output, nr, 1);
}
}
static int ddb_port_match_i2c(struct ddb_port *port)
{
struct ddb *dev = port->dev;
u32 i;
for (i = 0; i < dev->i2c_num; i++) {
if (dev->i2c[i].link == port->lnr &&
dev->i2c[i].nr == port->nr) {
port->i2c = &dev->i2c[i];
return 1;
}
}
return 0;
}
static int ddb_port_match_link_i2c(struct ddb_port *port)
{
struct ddb *dev = port->dev;
u32 i;
for (i = 0; i < dev->i2c_num; i++) {
if (dev->i2c[i].link == port->lnr) {
port->i2c = &dev->i2c[i];
return 1;
}
}
return 0;
}
void ddb_ports_init(struct ddb *dev)
{
u32 i, l, p;
struct ddb_port *port;
const struct ddb_info *info;
const struct ddb_regmap *rm;
for (p = l = 0; l < DDB_MAX_LINK; l++) {
info = dev->link[l].info;
if (!info)
continue;
rm = info->regmap;
if (!rm)
continue;
for (i = 0; i < info->port_num; i++, p++) {
port = &dev->port[p];
port->dev = dev;
port->nr = i;
port->lnr = l;
port->pnr = p;
port->gap = 0xffffffff;
port->obr = ci_bitrate;
mutex_init(&port->i2c_gate_lock);
if (!ddb_port_match_i2c(port)) {
if (info->type == DDB_OCTOPUS_MAX)
ddb_port_match_link_i2c(port);
}
ddb_port_probe(port);
port->dvb[0].adap = &dev->adap[2 * p];
port->dvb[1].adap = &dev->adap[2 * p + 1];
if (port->class == DDB_PORT_NONE && i && p &&
dev->port[p - 1].type == DDB_CI_EXTERNAL_XO2) {
port->class = DDB_PORT_CI;
port->type = DDB_CI_EXTERNAL_XO2_B;
port->name = "DuoFlex CI_B";
port->i2c = dev->port[p - 1].i2c;
}
dev_info(dev->dev, "Port %u: Link %u, Link Port %u (TAB %u): %s\n",
port->pnr, port->lnr, port->nr, port->nr + 1,
port->name);
if (port->class == DDB_PORT_CI &&
port->type == DDB_CI_EXTERNAL_XO2) {
ddb_input_init(port, 2 * i, 0, 2 * i);
ddb_output_init(port, i);
continue;
}
if (port->class == DDB_PORT_CI &&
port->type == DDB_CI_EXTERNAL_XO2_B) {
ddb_input_init(port, 2 * i - 1, 0, 2 * i - 1);
ddb_output_init(port, i);
continue;
}
if (port->class == DDB_PORT_NONE)
continue;
switch (dev->link[l].info->type) {
case DDB_OCTOPUS_CI:
if (i >= 2) {
ddb_input_init(port, 2 + i, 0, 2 + i);
ddb_input_init(port, 4 + i, 1, 4 + i);
ddb_output_init(port, i);
break;
}
fallthrough;
case DDB_OCTOPUS:
ddb_input_init(port, 2 * i, 0, 2 * i);
ddb_input_init(port, 2 * i + 1, 1, 2 * i + 1);
ddb_output_init(port, i);
break;
case DDB_OCTOPUS_MAX:
case DDB_OCTOPUS_MAX_CT:
case DDB_OCTOPUS_MCI:
ddb_input_init(port, 2 * i, 0, 2 * p);
ddb_input_init(port, 2 * i + 1, 1, 2 * p + 1);
break;
default:
break;
}
}
}
dev->port_num = p;
}
void ddb_ports_release(struct ddb *dev)
{
int i;
struct ddb_port *port;
for (i = 0; i < dev->port_num; i++) {
port = &dev->port[i];
if (port->input[0] && port->input[0]->dma)
cancel_work_sync(&port->input[0]->dma->work);
if (port->input[1] && port->input[1]->dma)
cancel_work_sync(&port->input[1]->dma->work);
if (port->output && port->output->dma)
cancel_work_sync(&port->output->dma->work);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
#define IRQ_HANDLE(_nr) \
do { if ((s & (1UL << ((_nr) & 0x1f))) && \
dev->link[0].irq[_nr].handler) \
dev->link[0].irq[_nr].handler(dev->link[0].irq[_nr].data); } \
while (0)
#define IRQ_HANDLE_NIBBLE(_shift) { \
if (s & (0x0000000f << ((_shift) & 0x1f))) { \
IRQ_HANDLE(0 + (_shift)); \
IRQ_HANDLE(1 + (_shift)); \
IRQ_HANDLE(2 + (_shift)); \
IRQ_HANDLE(3 + (_shift)); \
} \
}
#define IRQ_HANDLE_BYTE(_shift) { \
if (s & (0x000000ff << ((_shift) & 0x1f))) { \
IRQ_HANDLE(0 + (_shift)); \
IRQ_HANDLE(1 + (_shift)); \
IRQ_HANDLE(2 + (_shift)); \
IRQ_HANDLE(3 + (_shift)); \
IRQ_HANDLE(4 + (_shift)); \
IRQ_HANDLE(5 + (_shift)); \
IRQ_HANDLE(6 + (_shift)); \
IRQ_HANDLE(7 + (_shift)); \
} \
}
static void irq_handle_msg(struct ddb *dev, u32 s)
{
dev->i2c_irq++;
IRQ_HANDLE_NIBBLE(0);
}
static void irq_handle_io(struct ddb *dev, u32 s)
{
dev->ts_irq++;
IRQ_HANDLE_NIBBLE(4);
IRQ_HANDLE_BYTE(8);
IRQ_HANDLE_BYTE(16);
IRQ_HANDLE_BYTE(24);
}
irqreturn_t ddb_irq_handler0(int irq, void *dev_id)
{
struct ddb *dev = (struct ddb *)dev_id;
u32 mask = 0x8fffff00;
u32 s = mask & ddbreadl(dev, INTERRUPT_STATUS);
if (!s)
return IRQ_NONE;
do {
if (s & 0x80000000)
return IRQ_NONE;
ddbwritel(dev, s, INTERRUPT_ACK);
irq_handle_io(dev, s);
} while ((s = mask & ddbreadl(dev, INTERRUPT_STATUS)));
return IRQ_HANDLED;
}
irqreturn_t ddb_irq_handler1(int irq, void *dev_id)
{
struct ddb *dev = (struct ddb *)dev_id;
u32 mask = 0x8000000f;
u32 s = mask & ddbreadl(dev, INTERRUPT_STATUS);
if (!s)
return IRQ_NONE;
do {
if (s & 0x80000000)
return IRQ_NONE;
ddbwritel(dev, s, INTERRUPT_ACK);
irq_handle_msg(dev, s);
} while ((s = mask & ddbreadl(dev, INTERRUPT_STATUS)));
return IRQ_HANDLED;
}
irqreturn_t ddb_irq_handler(int irq, void *dev_id)
{
struct ddb *dev = (struct ddb *)dev_id;
u32 s = ddbreadl(dev, INTERRUPT_STATUS);
int ret = IRQ_HANDLED;
if (!s)
return IRQ_NONE;
do {
if (s & 0x80000000)
return IRQ_NONE;
ddbwritel(dev, s, INTERRUPT_ACK);
if (s & 0x0000000f)
irq_handle_msg(dev, s);
if (s & 0x0fffff00)
irq_handle_io(dev, s);
} while ((s = ddbreadl(dev, INTERRUPT_STATUS)));
return ret;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static int reg_wait(struct ddb *dev, u32 reg, u32 bit)
{
u32 count = 0;
while (safe_ddbreadl(dev, reg) & bit) {
ndelay(10);
if (++count == 100)
return -1;
}
return 0;
}
static int flashio(struct ddb *dev, u32 lnr, u8 *wbuf, u32 wlen, u8 *rbuf,
u32 rlen)
{
u32 data, shift;
u32 tag = DDB_LINK_TAG(lnr);
struct ddb_link *link = &dev->link[lnr];
mutex_lock(&link->flash_mutex);
if (wlen > 4)
ddbwritel(dev, 1, tag | SPI_CONTROL);
while (wlen > 4) {
/* FIXME: check for big-endian */
data = swab32(*(u32 *)wbuf);
wbuf += 4;
wlen -= 4;
ddbwritel(dev, data, tag | SPI_DATA);
if (reg_wait(dev, tag | SPI_CONTROL, 4))
goto fail;
}
if (rlen)
ddbwritel(dev, 0x0001 | ((wlen << (8 + 3)) & 0x1f00),
tag | SPI_CONTROL);
else
ddbwritel(dev, 0x0003 | ((wlen << (8 + 3)) & 0x1f00),
tag | SPI_CONTROL);
data = 0;
shift = ((4 - wlen) * 8);
while (wlen) {
data <<= 8;
data |= *wbuf;
wlen--;
wbuf++;
}
if (shift)
data <<= shift;
ddbwritel(dev, data, tag | SPI_DATA);
if (reg_wait(dev, tag | SPI_CONTROL, 4))
goto fail;
if (!rlen) {
ddbwritel(dev, 0, tag | SPI_CONTROL);
goto exit;
}
if (rlen > 4)
ddbwritel(dev, 1, tag | SPI_CONTROL);
while (rlen > 4) {
ddbwritel(dev, 0xffffffff, tag | SPI_DATA);
if (reg_wait(dev, tag | SPI_CONTROL, 4))
goto fail;
data = ddbreadl(dev, tag | SPI_DATA);
*(u32 *)rbuf = swab32(data);
rbuf += 4;
rlen -= 4;
}
ddbwritel(dev, 0x0003 | ((rlen << (8 + 3)) & 0x1F00),
tag | SPI_CONTROL);
ddbwritel(dev, 0xffffffff, tag | SPI_DATA);
if (reg_wait(dev, tag | SPI_CONTROL, 4))
goto fail;
data = ddbreadl(dev, tag | SPI_DATA);
ddbwritel(dev, 0, tag | SPI_CONTROL);
if (rlen < 4)
data <<= ((4 - rlen) * 8);
while (rlen > 0) {
*rbuf = ((data >> 24) & 0xff);
data <<= 8;
rbuf++;
rlen--;
}
exit:
mutex_unlock(&link->flash_mutex);
return 0;
fail:
mutex_unlock(&link->flash_mutex);
return -1;
}
int ddbridge_flashread(struct ddb *dev, u32 link, u8 *buf, u32 addr, u32 len)
{
u8 cmd[4] = {0x03, (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff};
return flashio(dev, link, cmd, 4, buf, len);
}
/*
* TODO/FIXME: add/implement IOCTLs from upstream driver
*/
#define DDB_NAME "ddbridge"
static u32 ddb_num;
static int ddb_major;
static DEFINE_MUTEX(ddb_mutex);
static int ddb_release(struct inode *inode, struct file *file)
{
struct ddb *dev = file->private_data;
dev->ddb_dev_users--;
return 0;
}
static int ddb_open(struct inode *inode, struct file *file)
{
struct ddb *dev = ddbs[iminor(inode)];
if (dev->ddb_dev_users)
return -EBUSY;
dev->ddb_dev_users++;
file->private_data = dev;
return 0;
}
static long ddb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct ddb *dev = file->private_data;
dev_warn(dev->dev, "DDB IOCTLs unsupported (cmd: %d, arg: %lu)\n",
cmd, arg);
return -ENOTTY;
}
static const struct file_operations ddb_fops = {
.unlocked_ioctl = ddb_ioctl,
.open = ddb_open,
.release = ddb_release,
};
static char *ddb_devnode(const struct device *device, umode_t *mode)
{
const struct ddb *dev = dev_get_drvdata(device);
return kasprintf(GFP_KERNEL, "ddbridge/card%d", dev->nr);
}
#define __ATTR_MRO(_name, _show) { \
.attr = { .name = __stringify(_name), .mode = 0444 }, \
.show = _show, \
}
#define __ATTR_MWO(_name, _store) { \
.attr = { .name = __stringify(_name), .mode = 0222 }, \
.store = _store, \
}
static ssize_t ports_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "%d\n", dev->port_num);
}
static ssize_t ts_irq_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "%d\n", dev->ts_irq);
}
static ssize_t i2c_irq_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "%d\n", dev->i2c_irq);
}
static ssize_t fan_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
u32 val;
val = ddbreadl(dev, GPIO_OUTPUT) & 1;
return sprintf(buf, "%d\n", val);
}
static ssize_t fan_store(struct device *device, struct device_attribute *d,
const char *buf, size_t count)
{
struct ddb *dev = dev_get_drvdata(device);
u32 val;
if (sscanf(buf, "%u\n", &val) != 1)
return -EINVAL;
ddbwritel(dev, 1, GPIO_DIRECTION);
ddbwritel(dev, val & 1, GPIO_OUTPUT);
return count;
}
static ssize_t fanspeed_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
int num = attr->attr.name[8] - 0x30;
struct ddb_link *link = &dev->link[num];
u32 spd;
spd = ddblreadl(link, TEMPMON_FANCONTROL) & 0xff;
return sprintf(buf, "%u\n", spd * 100);
}
static ssize_t temp_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
struct ddb_link *link = &dev->link[0];
struct i2c_adapter *adap;
int temp, temp2;
u8 tmp[2];
if (!link->info->temp_num)
return sprintf(buf, "no sensor\n");
adap = &dev->i2c[link->info->temp_bus].adap;
if (i2c_read_regs(adap, 0x48, 0, tmp, 2) < 0)
return sprintf(buf, "read_error\n");
temp = (tmp[0] << 3) | (tmp[1] >> 5);
temp *= 125;
if (link->info->temp_num == 2) {
if (i2c_read_regs(adap, 0x49, 0, tmp, 2) < 0)
return sprintf(buf, "read_error\n");
temp2 = (tmp[0] << 3) | (tmp[1] >> 5);
temp2 *= 125;
return sprintf(buf, "%d %d\n", temp, temp2);
}
return sprintf(buf, "%d\n", temp);
}
static ssize_t ctemp_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
struct i2c_adapter *adap;
int temp;
u8 tmp[2];
int num = attr->attr.name[4] - 0x30;
adap = &dev->i2c[num].adap;
if (!adap)
return 0;
if (i2c_read_regs(adap, 0x49, 0, tmp, 2) < 0)
if (i2c_read_regs(adap, 0x4d, 0, tmp, 2) < 0)
return sprintf(buf, "no sensor\n");
temp = tmp[0] * 1000;
return sprintf(buf, "%d\n", temp);
}
static ssize_t led_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
int num = attr->attr.name[3] - 0x30;
return sprintf(buf, "%d\n", dev->leds & (1 << num) ? 1 : 0);
}
static void ddb_set_led(struct ddb *dev, int num, int val)
{
if (!dev->link[0].info->led_num)
return;
switch (dev->port[num].class) {
case DDB_PORT_TUNER:
switch (dev->port[num].type) {
case DDB_TUNER_DVBS_ST:
i2c_write_reg16(&dev->i2c[num].adap,
0x69, 0xf14c, val ? 2 : 0);
break;
case DDB_TUNER_DVBCT_ST:
i2c_write_reg16(&dev->i2c[num].adap,
0x1f, 0xf00e, 0);
i2c_write_reg16(&dev->i2c[num].adap,
0x1f, 0xf00f, val ? 1 : 0);
break;
case DDB_TUNER_XO2 ... DDB_TUNER_DVBC2T2I_SONY:
{
u8 v;
i2c_read_reg(&dev->i2c[num].adap, 0x10, 0x08, &v);
v = (v & ~0x10) | (val ? 0x10 : 0);
i2c_write_reg(&dev->i2c[num].adap, 0x10, 0x08, v);
break;
}
default:
break;
}
break;
}
}
static ssize_t led_store(struct device *device,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ddb *dev = dev_get_drvdata(device);
int num = attr->attr.name[3] - 0x30;
u32 val;
if (sscanf(buf, "%u\n", &val) != 1)
return -EINVAL;
if (val)
dev->leds |= (1 << num);
else
dev->leds &= ~(1 << num);
ddb_set_led(dev, num, val);
return count;
}
static ssize_t snr_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
char snr[32];
int num = attr->attr.name[3] - 0x30;
if (dev->port[num].type >= DDB_TUNER_XO2) {
if (i2c_read_regs(&dev->i2c[num].adap, 0x10, 0x10, snr, 16) < 0)
return sprintf(buf, "NO SNR\n");
snr[16] = 0;
} else {
/* serial number at 0x100-0x11f */
if (i2c_read_regs16(&dev->i2c[num].adap,
0x57, 0x100, snr, 32) < 0)
if (i2c_read_regs16(&dev->i2c[num].adap,
0x50, 0x100, snr, 32) < 0)
return sprintf(buf, "NO SNR\n");
snr[31] = 0; /* in case it is not terminated on EEPROM */
}
return sprintf(buf, "%s\n", snr);
}
static ssize_t bsnr_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
char snr[16];
ddbridge_flashread(dev, 0, snr, 0x10, 15);
snr[15] = 0; /* in case it is not terminated on EEPROM */
return sprintf(buf, "%s\n", snr);
}
static ssize_t bpsnr_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
unsigned char snr[32];
if (!dev->i2c_num)
return 0;
if (i2c_read_regs16(&dev->i2c[0].adap,
0x50, 0x0000, snr, 32) < 0 ||
snr[0] == 0xff)
return sprintf(buf, "NO SNR\n");
snr[31] = 0; /* in case it is not terminated on EEPROM */
return sprintf(buf, "%s\n", snr);
}
static ssize_t redirect_show(struct device *device,
struct device_attribute *attr, char *buf)
{
return 0;
}
static ssize_t redirect_store(struct device *device,
struct device_attribute *attr,
const char *buf, size_t count)
{
unsigned int i, p;
int res;
if (sscanf(buf, "%x %x\n", &i, &p) != 2)
return -EINVAL;
res = ddb_redirect(i, p);
if (res < 0)
return res;
dev_info(device, "redirect: %02x, %02x\n", i, p);
return count;
}
static ssize_t gap_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
int num = attr->attr.name[3] - 0x30;
return sprintf(buf, "%d\n", dev->port[num].gap);
}
static ssize_t gap_store(struct device *device, struct device_attribute *attr,
const char *buf, size_t count)
{
struct ddb *dev = dev_get_drvdata(device);
int num = attr->attr.name[3] - 0x30;
unsigned int val;
if (sscanf(buf, "%u\n", &val) != 1)
return -EINVAL;
if (val > 128)
return -EINVAL;
if (val == 128)
val = 0xffffffff;
dev->port[num].gap = val;
return count;
}
static ssize_t version_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "%08x %08x\n",
dev->link[0].ids.hwid, dev->link[0].ids.regmapid);
}
static ssize_t hwid_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "0x%08X\n", dev->link[0].ids.hwid);
}
static ssize_t regmap_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "0x%08X\n", dev->link[0].ids.regmapid);
}
static ssize_t fmode_show(struct device *device,
struct device_attribute *attr, char *buf)
{
int num = attr->attr.name[5] - 0x30;
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "%u\n", dev->link[num].lnb.fmode);
}
static ssize_t devid_show(struct device *device,
struct device_attribute *attr, char *buf)
{
int num = attr->attr.name[5] - 0x30;
struct ddb *dev = dev_get_drvdata(device);
return sprintf(buf, "%08x\n", dev->link[num].ids.devid);
}
static ssize_t fmode_store(struct device *device, struct device_attribute *attr,
const char *buf, size_t count)
{
struct ddb *dev = dev_get_drvdata(device);
int num = attr->attr.name[5] - 0x30;
unsigned int val;
if (sscanf(buf, "%u\n", &val) != 1)
return -EINVAL;
if (val > 3)
return -EINVAL;
ddb_lnb_init_fmode(dev, &dev->link[num], val);
return count;
}
static struct device_attribute ddb_attrs[] = {
__ATTR_RO(version),
__ATTR_RO(ports),
__ATTR_RO(ts_irq),
__ATTR_RO(i2c_irq),
__ATTR(gap0, 0664, gap_show, gap_store),
__ATTR(gap1, 0664, gap_show, gap_store),
__ATTR(gap2, 0664, gap_show, gap_store),
__ATTR(gap3, 0664, gap_show, gap_store),
__ATTR(fmode0, 0664, fmode_show, fmode_store),
__ATTR(fmode1, 0664, fmode_show, fmode_store),
__ATTR(fmode2, 0664, fmode_show, fmode_store),
__ATTR(fmode3, 0664, fmode_show, fmode_store),
__ATTR_MRO(devid0, devid_show),
__ATTR_MRO(devid1, devid_show),
__ATTR_MRO(devid2, devid_show),
__ATTR_MRO(devid3, devid_show),
__ATTR_RO(hwid),
__ATTR_RO(regmap),
__ATTR(redirect, 0664, redirect_show, redirect_store),
__ATTR_MRO(snr, bsnr_show),
__ATTR_RO(bpsnr),
__ATTR_NULL,
};
static struct device_attribute ddb_attrs_temp[] = {
__ATTR_RO(temp),
};
static struct device_attribute ddb_attrs_fan[] = {
__ATTR(fan, 0664, fan_show, fan_store),
};
static struct device_attribute ddb_attrs_snr[] = {
__ATTR_MRO(snr0, snr_show),
__ATTR_MRO(snr1, snr_show),
__ATTR_MRO(snr2, snr_show),
__ATTR_MRO(snr3, snr_show),
};
static struct device_attribute ddb_attrs_ctemp[] = {
__ATTR_MRO(temp0, ctemp_show),
__ATTR_MRO(temp1, ctemp_show),
__ATTR_MRO(temp2, ctemp_show),
__ATTR_MRO(temp3, ctemp_show),
};
static struct device_attribute ddb_attrs_led[] = {
__ATTR(led0, 0664, led_show, led_store),
__ATTR(led1, 0664, led_show, led_store),
__ATTR(led2, 0664, led_show, led_store),
__ATTR(led3, 0664, led_show, led_store),
};
static struct device_attribute ddb_attrs_fanspeed[] = {
__ATTR_MRO(fanspeed0, fanspeed_show),
__ATTR_MRO(fanspeed1, fanspeed_show),
__ATTR_MRO(fanspeed2, fanspeed_show),
__ATTR_MRO(fanspeed3, fanspeed_show),
};
static struct class ddb_class = {
.name = "ddbridge",
.devnode = ddb_devnode,
};
static int ddb_class_create(void)
{
ddb_major = register_chrdev(0, DDB_NAME, &ddb_fops);
if (ddb_major < 0)
return ddb_major;
if (class_register(&ddb_class) < 0)
return -1;
return 0;
}
static void ddb_class_destroy(void)
{
class_unregister(&ddb_class);
unregister_chrdev(ddb_major, DDB_NAME);
}
static void ddb_device_attrs_del(struct ddb *dev)
{
int i;
for (i = 0; i < 4; i++)
if (dev->link[i].info && dev->link[i].info->tempmon_irq)
device_remove_file(dev->ddb_dev,
&ddb_attrs_fanspeed[i]);
for (i = 0; i < dev->link[0].info->temp_num; i++)
device_remove_file(dev->ddb_dev, &ddb_attrs_temp[i]);
for (i = 0; i < dev->link[0].info->fan_num; i++)
device_remove_file(dev->ddb_dev, &ddb_attrs_fan[i]);
for (i = 0; i < dev->i2c_num && i < 4; i++) {
if (dev->link[0].info->led_num)
device_remove_file(dev->ddb_dev, &ddb_attrs_led[i]);
device_remove_file(dev->ddb_dev, &ddb_attrs_snr[i]);
device_remove_file(dev->ddb_dev, &ddb_attrs_ctemp[i]);
}
for (i = 0; ddb_attrs[i].attr.name; i++)
device_remove_file(dev->ddb_dev, &ddb_attrs[i]);
}
static int ddb_device_attrs_add(struct ddb *dev)
{
int i;
for (i = 0; ddb_attrs[i].attr.name; i++)
if (device_create_file(dev->ddb_dev, &ddb_attrs[i]))
goto fail;
for (i = 0; i < dev->link[0].info->temp_num; i++)
if (device_create_file(dev->ddb_dev, &ddb_attrs_temp[i]))
goto fail;
for (i = 0; i < dev->link[0].info->fan_num; i++)
if (device_create_file(dev->ddb_dev, &ddb_attrs_fan[i]))
goto fail;
for (i = 0; (i < dev->i2c_num) && (i < 4); i++) {
if (device_create_file(dev->ddb_dev, &ddb_attrs_snr[i]))
goto fail;
if (device_create_file(dev->ddb_dev, &ddb_attrs_ctemp[i]))
goto fail;
if (dev->link[0].info->led_num)
if (device_create_file(dev->ddb_dev,
&ddb_attrs_led[i]))
goto fail;
}
for (i = 0; i < 4; i++)
if (dev->link[i].info && dev->link[i].info->tempmon_irq)
if (device_create_file(dev->ddb_dev,
&ddb_attrs_fanspeed[i]))
goto fail;
return 0;
fail:
return -1;
}
int ddb_device_create(struct ddb *dev)
{
int res = 0;
if (ddb_num == DDB_MAX_ADAPTER)
return -ENOMEM;
mutex_lock(&ddb_mutex);
dev->nr = ddb_num;
ddbs[dev->nr] = dev;
dev->ddb_dev = device_create(&ddb_class, dev->dev,
MKDEV(ddb_major, dev->nr),
dev, "ddbridge%d", dev->nr);
if (IS_ERR(dev->ddb_dev)) {
res = PTR_ERR(dev->ddb_dev);
dev_info(dev->dev, "Could not create ddbridge%d\n", dev->nr);
goto fail;
}
res = ddb_device_attrs_add(dev);
if (res) {
ddb_device_attrs_del(dev);
device_destroy(&ddb_class, MKDEV(ddb_major, dev->nr));
ddbs[dev->nr] = NULL;
dev->ddb_dev = ERR_PTR(-ENODEV);
} else {
ddb_num++;
}
fail:
mutex_unlock(&ddb_mutex);
return res;
}
void ddb_device_destroy(struct ddb *dev)
{
if (IS_ERR(dev->ddb_dev))
return;
ddb_device_attrs_del(dev);
device_destroy(&ddb_class, MKDEV(ddb_major, dev->nr));
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static void tempmon_setfan(struct ddb_link *link)
{
u32 temp, temp2, pwm;
if ((ddblreadl(link, TEMPMON_CONTROL) &
TEMPMON_CONTROL_OVERTEMP) != 0) {
dev_info(link->dev->dev, "Over temperature condition\n");
link->overtemperature_error = 1;
}
temp = (ddblreadl(link, TEMPMON_SENSOR0) >> 8) & 0xFF;
if (temp & 0x80)
temp = 0;
temp2 = (ddblreadl(link, TEMPMON_SENSOR1) >> 8) & 0xFF;
if (temp2 & 0x80)
temp2 = 0;
if (temp2 > temp)
temp = temp2;
pwm = (ddblreadl(link, TEMPMON_FANCONTROL) >> 8) & 0x0F;
if (pwm > 10)
pwm = 10;
if (temp >= link->temp_tab[pwm]) {
while (pwm < 10 && temp >= link->temp_tab[pwm + 1])
pwm += 1;
} else {
while (pwm > 1 && temp < link->temp_tab[pwm - 2])
pwm -= 1;
}
ddblwritel(link, (pwm << 8), TEMPMON_FANCONTROL);
}
static void temp_handler(void *data)
{
struct ddb_link *link = (struct ddb_link *)data;
spin_lock(&link->temp_lock);
tempmon_setfan(link);
spin_unlock(&link->temp_lock);
}
static int tempmon_init(struct ddb_link *link, int first_time)
{
struct ddb *dev = link->dev;
int status = 0;
u32 l = link->nr;
spin_lock_irq(&link->temp_lock);
if (first_time) {
static u8 temperature_table[11] = {
30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80 };
memcpy(link->temp_tab, temperature_table,
sizeof(temperature_table));
}
ddb_irq_set(dev, l, link->info->tempmon_irq, temp_handler, link);
ddblwritel(link, (TEMPMON_CONTROL_OVERTEMP | TEMPMON_CONTROL_AUTOSCAN |
TEMPMON_CONTROL_INTENABLE),
TEMPMON_CONTROL);
ddblwritel(link, (3 << 8), TEMPMON_FANCONTROL);
link->overtemperature_error =
((ddblreadl(link, TEMPMON_CONTROL) &
TEMPMON_CONTROL_OVERTEMP) != 0);
if (link->overtemperature_error) {
dev_info(link->dev->dev, "Over temperature condition\n");
status = -1;
}
tempmon_setfan(link);
spin_unlock_irq(&link->temp_lock);
return status;
}
static int ddb_init_tempmon(struct ddb_link *link)
{
const struct ddb_info *info = link->info;
if (!info->tempmon_irq)
return 0;
if (info->type == DDB_OCTOPUS_MAX_CT)
if (link->ids.regmapid < 0x00010002)
return 0;
spin_lock_init(&link->temp_lock);
dev_dbg(link->dev->dev, "init_tempmon\n");
return tempmon_init(link, 1);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
static int ddb_init_boards(struct ddb *dev)
{
const struct ddb_info *info;
struct ddb_link *link;
u32 l;
for (l = 0; l < DDB_MAX_LINK; l++) {
link = &dev->link[l];
info = link->info;
if (!info)
continue;
if (info->board_control) {
ddbwritel(dev, 0, DDB_LINK_TAG(l) | BOARD_CONTROL);
msleep(100);
ddbwritel(dev, info->board_control_2,
DDB_LINK_TAG(l) | BOARD_CONTROL);
usleep_range(2000, 3000);
ddbwritel(dev,
info->board_control_2 | info->board_control,
DDB_LINK_TAG(l) | BOARD_CONTROL);
usleep_range(2000, 3000);
}
ddb_init_tempmon(link);
}
return 0;
}
int ddb_init(struct ddb *dev)
{
mutex_init(&dev->link[0].lnb.lock);
mutex_init(&dev->link[0].flash_mutex);
if (no_init) {
ddb_device_create(dev);
return 0;
}
ddb_init_boards(dev);
if (ddb_i2c_init(dev) < 0)
goto fail1;
ddb_ports_init(dev);
if (ddb_buffers_alloc(dev) < 0) {
dev_info(dev->dev, "Could not allocate buffer memory\n");
goto fail2;
}
if (ddb_ports_attach(dev) < 0)
goto fail3;
ddb_device_create(dev);
if (dev->link[0].info->fan_num) {
ddbwritel(dev, 1, GPIO_DIRECTION);
ddbwritel(dev, 1, GPIO_OUTPUT);
}
return 0;
fail3:
dev_err(dev->dev, "fail3\n");
ddb_ports_detach(dev);
ddb_buffers_free(dev);
fail2:
dev_err(dev->dev, "fail2\n");
ddb_ports_release(dev);
ddb_i2c_release(dev);
fail1:
dev_err(dev->dev, "fail1\n");
return -1;
}
void ddb_unmap(struct ddb *dev)
{
if (dev->regs)
iounmap(dev->regs);
vfree(dev);
}
int ddb_exit_ddbridge(int stage, int error)
{
switch (stage) {
default:
case 2:
destroy_workqueue(ddb_wq);
fallthrough;
case 1:
ddb_class_destroy();
break;
}
return error;
}
int ddb_init_ddbridge(void)
{
if (dma_buf_num < 8)
dma_buf_num = 8;
if (dma_buf_num > 32)
dma_buf_num = 32;
if (dma_buf_size < 1)
dma_buf_size = 1;
if (dma_buf_size > 43)
dma_buf_size = 43;
if (ddb_class_create() < 0)
return -1;
ddb_wq = alloc_workqueue("ddbridge", 0, 0);
if (!ddb_wq)
return ddb_exit_ddbridge(1, -1);
return 0;
}
| linux-master | drivers/media/pci/ddbridge/ddbridge-core.c |
// SPDX-License-Identifier: GPL-2.0
/*
* ddbridge-mci.c: Digital Devices microcode interface
*
* Copyright (C) 2017-2018 Digital Devices GmbH
* Ralph Metzler <[email protected]>
* Marcus Metzler <[email protected]>
*/
#include "ddbridge.h"
#include "ddbridge-io.h"
#include "ddbridge-mci.h"
static LIST_HEAD(mci_list);
static int mci_reset(struct mci *state)
{
struct ddb_link *link = state->base->link;
u32 status = 0;
u32 timeout = 40;
ddblwritel(link, MCI_CONTROL_RESET, MCI_CONTROL);
ddblwritel(link, 0, MCI_CONTROL + 4); /* 1= no internal init */
msleep(300);
ddblwritel(link, 0, MCI_CONTROL);
while (1) {
status = ddblreadl(link, MCI_CONTROL);
if ((status & MCI_CONTROL_READY) == MCI_CONTROL_READY)
break;
if (--timeout == 0)
break;
msleep(50);
}
if ((status & MCI_CONTROL_READY) == 0)
return -1;
if (link->ids.device == 0x0009)
ddblwritel(link, SX8_TSCONFIG_MODE_NORMAL, SX8_TSCONFIG);
return 0;
}
int ddb_mci_config(struct mci *state, u32 config)
{
struct ddb_link *link = state->base->link;
if (link->ids.device != 0x0009)
return -EINVAL;
ddblwritel(link, config, SX8_TSCONFIG);
return 0;
}
static int _mci_cmd_unlocked(struct mci *state,
u32 *cmd, u32 cmd_len,
u32 *res, u32 res_len)
{
struct ddb_link *link = state->base->link;
u32 i, val;
unsigned long stat;
val = ddblreadl(link, MCI_CONTROL);
if (val & (MCI_CONTROL_RESET | MCI_CONTROL_START_COMMAND))
return -EIO;
if (cmd && cmd_len)
for (i = 0; i < cmd_len; i++)
ddblwritel(link, cmd[i], MCI_COMMAND + i * 4);
val |= (MCI_CONTROL_START_COMMAND | MCI_CONTROL_ENABLE_DONE_INTERRUPT);
ddblwritel(link, val, MCI_CONTROL);
stat = wait_for_completion_timeout(&state->base->completion, HZ);
if (stat == 0) {
dev_warn(state->base->dev, "MCI-%d: MCI timeout\n", state->nr);
return -EIO;
}
if (res && res_len)
for (i = 0; i < res_len; i++)
res[i] = ddblreadl(link, MCI_RESULT + i * 4);
return 0;
}
int ddb_mci_cmd(struct mci *state,
struct mci_command *command,
struct mci_result *result)
{
int stat;
mutex_lock(&state->base->mci_lock);
stat = _mci_cmd_unlocked(state,
(u32 *)command, sizeof(*command) / sizeof(u32),
(u32 *)result, sizeof(*result) / sizeof(u32));
mutex_unlock(&state->base->mci_lock);
return stat;
}
static void mci_handler(void *priv)
{
struct mci_base *base = (struct mci_base *)priv;
complete(&base->completion);
}
static struct mci_base *match_base(void *key)
{
struct mci_base *p;
list_for_each_entry(p, &mci_list, mci_list)
if (p->key == key)
return p;
return NULL;
}
static int probe(struct mci *state)
{
mci_reset(state);
return 0;
}
struct dvb_frontend
*ddb_mci_attach(struct ddb_input *input, struct mci_cfg *cfg, int nr,
int (**fn_set_input)(struct dvb_frontend *fe, int input))
{
struct ddb_port *port = input->port;
struct ddb *dev = port->dev;
struct ddb_link *link = &dev->link[port->lnr];
struct mci_base *base;
struct mci *state;
void *key = cfg->type ? (void *)port : (void *)link;
state = kzalloc(cfg->state_size, GFP_KERNEL);
if (!state)
return NULL;
base = match_base(key);
if (base) {
base->count++;
state->base = base;
} else {
base = kzalloc(cfg->base_size, GFP_KERNEL);
if (!base)
goto fail;
base->key = key;
base->count = 1;
base->link = link;
base->dev = dev->dev;
mutex_init(&base->mci_lock);
mutex_init(&base->tuner_lock);
ddb_irq_set(dev, link->nr, 0, mci_handler, base);
init_completion(&base->completion);
state->base = base;
if (probe(state) < 0) {
kfree(base);
goto fail;
}
list_add(&base->mci_list, &mci_list);
if (cfg->base_init)
cfg->base_init(base);
}
memcpy(&state->fe.ops, cfg->fe_ops, sizeof(struct dvb_frontend_ops));
state->fe.demodulator_priv = state;
state->nr = nr;
*fn_set_input = cfg->set_input;
state->tuner = nr;
state->demod = nr;
if (cfg->init)
cfg->init(state);
return &state->fe;
fail:
kfree(state);
return NULL;
}
| linux-master | drivers/media/pci/ddbridge/ddbridge-mci.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for Dummy Frontend
*
* Written by Emard <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <media/dvb_frontend.h>
#include "ddbridge-dummy-fe.h"
struct ddbridge_dummy_fe_state {
struct dvb_frontend frontend;
};
static int ddbridge_dummy_fe_read_status(struct dvb_frontend *fe,
enum fe_status *status)
{
*status = FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK;
return 0;
}
static int ddbridge_dummy_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
{
*ber = 0;
return 0;
}
static int ddbridge_dummy_fe_read_signal_strength(struct dvb_frontend *fe,
u16 *strength)
{
*strength = 0;
return 0;
}
static int ddbridge_dummy_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
{
*snr = 0;
return 0;
}
static int ddbridge_dummy_fe_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{
*ucblocks = 0;
return 0;
}
/*
* Should only be implemented if it actually reads something from the hardware.
* Also, it should check for the locks, in order to avoid report wrong data
* to userspace.
*/
static int ddbridge_dummy_fe_get_frontend(struct dvb_frontend *fe,
struct dtv_frontend_properties *p)
{
return 0;
}
static int ddbridge_dummy_fe_set_frontend(struct dvb_frontend *fe)
{
if (fe->ops.tuner_ops.set_params) {
fe->ops.tuner_ops.set_params(fe);
if (fe->ops.i2c_gate_ctrl)
fe->ops.i2c_gate_ctrl(fe, 0);
}
return 0;
}
static int ddbridge_dummy_fe_sleep(struct dvb_frontend *fe)
{
return 0;
}
static int ddbridge_dummy_fe_init(struct dvb_frontend *fe)
{
return 0;
}
static void ddbridge_dummy_fe_release(struct dvb_frontend *fe)
{
struct ddbridge_dummy_fe_state *state = fe->demodulator_priv;
kfree(state);
}
static const struct dvb_frontend_ops ddbridge_dummy_fe_qam_ops;
struct dvb_frontend *ddbridge_dummy_fe_qam_attach(void)
{
struct ddbridge_dummy_fe_state *state = NULL;
/* allocate memory for the internal state */
state = kzalloc(sizeof(struct ddbridge_dummy_fe_state), GFP_KERNEL);
if (!state)
return NULL;
/* create dvb_frontend */
memcpy(&state->frontend.ops,
&ddbridge_dummy_fe_qam_ops,
sizeof(struct dvb_frontend_ops));
state->frontend.demodulator_priv = state;
return &state->frontend;
}
EXPORT_SYMBOL_GPL(ddbridge_dummy_fe_qam_attach);
static const struct dvb_frontend_ops ddbridge_dummy_fe_qam_ops = {
.delsys = { SYS_DVBC_ANNEX_A },
.info = {
.name = "ddbridge dummy DVB-C",
.frequency_min_hz = 51 * MHz,
.frequency_max_hz = 858 * MHz,
.frequency_stepsize_hz = 62500,
/* symbol_rate_min: SACLK/64 == (XIN/2)/64 */
.symbol_rate_min = (57840000 / 2) / 64,
.symbol_rate_max = (57840000 / 2) / 4, /* SACLK/4 */
.caps = FE_CAN_QAM_16 |
FE_CAN_QAM_32 |
FE_CAN_QAM_64 |
FE_CAN_QAM_128 |
FE_CAN_QAM_256 |
FE_CAN_FEC_AUTO |
FE_CAN_INVERSION_AUTO
},
.release = ddbridge_dummy_fe_release,
.init = ddbridge_dummy_fe_init,
.sleep = ddbridge_dummy_fe_sleep,
.set_frontend = ddbridge_dummy_fe_set_frontend,
.get_frontend = ddbridge_dummy_fe_get_frontend,
.read_status = ddbridge_dummy_fe_read_status,
.read_ber = ddbridge_dummy_fe_read_ber,
.read_signal_strength = ddbridge_dummy_fe_read_signal_strength,
.read_snr = ddbridge_dummy_fe_read_snr,
.read_ucblocks = ddbridge_dummy_fe_read_ucblocks,
};
MODULE_DESCRIPTION("ddbridge dummy Frontend");
MODULE_AUTHOR("Emard");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/ddbridge/ddbridge-dummy-fe.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Bt8xx based DVB adapter driver
*
* Copyright (C) 2002,2003 Florian Schirmer <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/bitops.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <media/dmxdev.h>
#include <media/dvbdev.h>
#include <media/dvb_demux.h>
#include <media/dvb_frontend.h>
#include "dvb-bt8xx.h"
#include "bt878.h"
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
#define dprintk(fmt, arg...) do { \
if (debug) \
printk(KERN_DEBUG pr_fmt("%s: " fmt), \
__func__, ##arg); \
} while (0)
#define IF_FREQUENCYx6 217 /* 6 * 36.16666666667MHz */
static void dvb_bt8xx_task(struct tasklet_struct *t)
{
struct bt878 *bt = from_tasklet(bt, t, tasklet);
struct dvb_bt8xx_card *card = dev_get_drvdata(&bt->adapter->dev);
dprintk("%d\n", card->bt->finished_block);
while (card->bt->last_block != card->bt->finished_block) {
(card->bt->TS_Size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
(&card->demux,
&card->bt->buf_cpu[card->bt->last_block *
card->bt->block_bytes],
card->bt->block_bytes);
card->bt->last_block = (card->bt->last_block + 1) %
card->bt->block_count;
}
}
static int dvb_bt8xx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
struct dvb_demux*dvbdmx = dvbdmxfeed->demux;
struct dvb_bt8xx_card *card = dvbdmx->priv;
int rc;
dprintk("dvb_bt8xx: start_feed\n");
if (!dvbdmx->dmx.frontend)
return -EINVAL;
mutex_lock(&card->lock);
card->nfeeds++;
rc = card->nfeeds;
if (card->nfeeds == 1)
bt878_start(card->bt, card->gpio_mode,
card->op_sync_orin, card->irq_err_ignore);
mutex_unlock(&card->lock);
return rc;
}
static int dvb_bt8xx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
struct dvb_bt8xx_card *card = dvbdmx->priv;
dprintk("dvb_bt8xx: stop_feed\n");
if (!dvbdmx->dmx.frontend)
return -EINVAL;
mutex_lock(&card->lock);
card->nfeeds--;
if (card->nfeeds == 0)
bt878_stop(card->bt);
mutex_unlock(&card->lock);
return 0;
}
static int is_pci_slot_eq(struct pci_dev* adev, struct pci_dev* bdev)
{
if ((adev->subsystem_vendor == bdev->subsystem_vendor) &&
(adev->subsystem_device == bdev->subsystem_device) &&
(adev->bus->number == bdev->bus->number) &&
(PCI_SLOT(adev->devfn) == PCI_SLOT(bdev->devfn)))
return 1;
return 0;
}
static struct bt878 *dvb_bt8xx_878_match(unsigned int bttv_nr,
struct pci_dev* bttv_pci_dev)
{
unsigned int card_nr;
/* Hmm, n squared. Hope n is small */
for (card_nr = 0; card_nr < bt878_num; card_nr++)
if (is_pci_slot_eq(bt878[card_nr].dev, bttv_pci_dev))
return &bt878[card_nr];
return NULL;
}
static int thomson_dtt7579_demod_init(struct dvb_frontend* fe)
{
static u8 mt352_clock_config [] = { 0x89, 0x38, 0x38 };
static u8 mt352_reset [] = { 0x50, 0x80 };
static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
static u8 mt352_agc_cfg [] = { 0x67, 0x28, 0x20 };
static u8 mt352_gpp_ctl_cfg [] = { 0x8C, 0x33 };
static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
udelay(2000);
mt352_write(fe, mt352_reset, sizeof(mt352_reset));
mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
mt352_write(fe, mt352_agc_cfg, sizeof(mt352_agc_cfg));
mt352_write(fe, mt352_gpp_ctl_cfg, sizeof(mt352_gpp_ctl_cfg));
mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
return 0;
}
static int thomson_dtt7579_tuner_calc_regs(struct dvb_frontend *fe, u8* pllbuf, int buf_len)
{
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
u32 div;
unsigned char bs = 0;
unsigned char cp = 0;
if (buf_len < 5)
return -EINVAL;
div = (((c->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
if (c->frequency < 542000000)
cp = 0xb4;
else if (c->frequency < 771000000)
cp = 0xbc;
else
cp = 0xf4;
if (c->frequency == 0)
bs = 0x03;
else if (c->frequency < 443250000)
bs = 0x02;
else
bs = 0x08;
pllbuf[0] = 0x60;
pllbuf[1] = div >> 8;
pllbuf[2] = div & 0xff;
pllbuf[3] = cp;
pllbuf[4] = bs;
return 5;
}
static struct mt352_config thomson_dtt7579_config = {
.demod_address = 0x0f,
.demod_init = thomson_dtt7579_demod_init,
};
static struct zl10353_config thomson_dtt7579_zl10353_config = {
.demod_address = 0x0f,
};
static int cx24108_tuner_set_params(struct dvb_frontend *fe)
{
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
u32 freq = c->frequency;
int i, a, n, pump;
u32 band, pll;
u32 osci[]={950000,1019000,1075000,1178000,1296000,1432000,
1576000,1718000,1856000,2036000,2150000};
u32 bandsel[]={0,0x00020000,0x00040000,0x00100800,0x00101000,
0x00102000,0x00104000,0x00108000,0x00110000,
0x00120000,0x00140000};
#define XTAL 1011100 /* Hz, really 1.0111 MHz and a /10 prescaler */
dprintk("cx24108 debug: entering SetTunerFreq, freq=%d\n", freq);
/* This is really the bit driving the tuner chip cx24108 */
if (freq<950000)
freq = 950000; /* kHz */
else if (freq>2150000)
freq = 2150000; /* satellite IF is 950..2150MHz */
/* decide which VCO to use for the input frequency */
for(i = 1; (i < ARRAY_SIZE(osci) - 1) && (osci[i] < freq); i++);
dprintk("cx24108 debug: select vco #%d (f=%d)\n", i, freq);
band=bandsel[i];
/* the gain values must be set by SetSymbolrate */
/* compute the pll divider needed, from Conexant data sheet,
resolved for (n*32+a), remember f(vco) is f(receive) *2 or *4,
depending on the divider bit. It is set to /4 on the 2 lowest
bands */
n=((i<=2?2:1)*freq*10L)/(XTAL/100);
a=n%32; n/=32; if(a==0) n--;
pump=(freq<(osci[i-1]+osci[i])/2);
pll=0xf8000000|
((pump?1:2)<<(14+11))|
((n&0x1ff)<<(5+11))|
((a&0x1f)<<11);
/* everything is shifted left 11 bits to left-align the bits in the
32bit word. Output to the tuner goes MSB-aligned, after all */
dprintk("cx24108 debug: pump=%d, n=%d, a=%d\n", pump, n, a);
cx24110_pll_write(fe,band);
/* set vga and vca to their widest-band settings, as a precaution.
SetSymbolrate might not be called to set this up */
cx24110_pll_write(fe,0x500c0000);
cx24110_pll_write(fe,0x83f1f800);
cx24110_pll_write(fe,pll);
//writereg(client,0x56,0x7f);
return 0;
}
static int pinnsat_tuner_init(struct dvb_frontend* fe)
{
struct dvb_bt8xx_card *card = fe->dvb->priv;
bttv_gpio_enable(card->bttv_nr, 1, 1); /* output */
bttv_write_gpio(card->bttv_nr, 1, 1); /* relay on */
return 0;
}
static int pinnsat_tuner_sleep(struct dvb_frontend* fe)
{
struct dvb_bt8xx_card *card = fe->dvb->priv;
bttv_write_gpio(card->bttv_nr, 1, 0); /* relay off */
return 0;
}
static struct cx24110_config pctvsat_config = {
.demod_address = 0x55,
};
static int microtune_mt7202dtf_tuner_set_params(struct dvb_frontend *fe)
{
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
u8 cfg, cpump, band_select;
u8 data[4];
u32 div;
struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
div = (36000000 + c->frequency + 83333) / 166666;
cfg = 0x88;
if (c->frequency < 175000000)
cpump = 2;
else if (c->frequency < 390000000)
cpump = 1;
else if (c->frequency < 470000000)
cpump = 2;
else if (c->frequency < 750000000)
cpump = 2;
else
cpump = 3;
if (c->frequency < 175000000)
band_select = 0x0e;
else if (c->frequency < 470000000)
band_select = 0x05;
else
band_select = 0x03;
data[0] = (div >> 8) & 0x7f;
data[1] = div & 0xff;
data[2] = ((div >> 10) & 0x60) | cfg;
data[3] = (cpump << 6) | band_select;
if (fe->ops.i2c_gate_ctrl)
fe->ops.i2c_gate_ctrl(fe, 1);
i2c_transfer(card->i2c_adapter, &msg, 1);
return (div * 166666 - 36000000);
}
static int microtune_mt7202dtf_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
{
struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
return request_firmware(fw, name, &bt->bt->dev->dev);
}
static const struct sp887x_config microtune_mt7202dtf_config = {
.demod_address = 0x70,
.request_firmware = microtune_mt7202dtf_request_firmware,
};
static int advbt771_samsung_tdtc9251dh0_demod_init(struct dvb_frontend* fe)
{
static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
static u8 mt352_reset [] = { 0x50, 0x80 };
static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
static u8 mt352_agc_cfg [] = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
0x00, 0xFF, 0x00, 0x40, 0x40 };
static u8 mt352_av771_extra[] = { 0xB5, 0x7A };
static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
udelay(2000);
mt352_write(fe, mt352_reset, sizeof(mt352_reset));
mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
udelay(2000);
mt352_write(fe, mt352_av771_extra,sizeof(mt352_av771_extra));
mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
return 0;
}
static int advbt771_samsung_tdtc9251dh0_tuner_calc_regs(struct dvb_frontend *fe, u8 *pllbuf, int buf_len)
{
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
u32 div;
unsigned char bs = 0;
unsigned char cp = 0;
if (buf_len < 5) return -EINVAL;
div = (((c->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
if (c->frequency < 150000000)
cp = 0xB4;
else if (c->frequency < 173000000)
cp = 0xBC;
else if (c->frequency < 250000000)
cp = 0xB4;
else if (c->frequency < 400000000)
cp = 0xBC;
else if (c->frequency < 420000000)
cp = 0xF4;
else if (c->frequency < 470000000)
cp = 0xFC;
else if (c->frequency < 600000000)
cp = 0xBC;
else if (c->frequency < 730000000)
cp = 0xF4;
else
cp = 0xFC;
if (c->frequency < 150000000)
bs = 0x01;
else if (c->frequency < 173000000)
bs = 0x01;
else if (c->frequency < 250000000)
bs = 0x02;
else if (c->frequency < 400000000)
bs = 0x02;
else if (c->frequency < 420000000)
bs = 0x02;
else if (c->frequency < 470000000)
bs = 0x02;
else
bs = 0x08;
pllbuf[0] = 0x61;
pllbuf[1] = div >> 8;
pllbuf[2] = div & 0xff;
pllbuf[3] = cp;
pllbuf[4] = bs;
return 5;
}
static struct mt352_config advbt771_samsung_tdtc9251dh0_config = {
.demod_address = 0x0f,
.demod_init = advbt771_samsung_tdtc9251dh0_demod_init,
};
static const struct dst_config dst_config = {
.demod_address = 0x55,
};
static int or51211_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
{
struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
return request_firmware(fw, name, &bt->bt->dev->dev);
}
static void or51211_setmode(struct dvb_frontend * fe, int mode)
{
struct dvb_bt8xx_card *bt = fe->dvb->priv;
bttv_write_gpio(bt->bttv_nr, 0x0002, mode); /* Reset */
msleep(20);
}
static void or51211_reset(struct dvb_frontend * fe)
{
struct dvb_bt8xx_card *bt = fe->dvb->priv;
/* RESET DEVICE
* reset is controlled by GPIO-0
* when set to 0 causes reset and when to 1 for normal op
* must remain reset for 128 clock cycles on a 50Mhz clock
* also PRM1 PRM2 & PRM4 are controlled by GPIO-1,GPIO-2 & GPIO-4
* We assume that the reset has be held low long enough or we
* have been reset by a power on. When the driver is unloaded
* reset set to 0 so if reloaded we have been reset.
*/
/* reset & PRM1,2&4 are outputs */
int ret = bttv_gpio_enable(bt->bttv_nr, 0x001F, 0x001F);
if (ret != 0)
pr_warn("or51211: Init Error - Can't Reset DVR (%i)\n", ret);
bttv_write_gpio(bt->bttv_nr, 0x001F, 0x0000); /* Reset */
msleep(20);
/* Now set for normal operation */
bttv_write_gpio(bt->bttv_nr, 0x0001F, 0x0001);
/* wait for operation to begin */
msleep(500);
}
static void or51211_sleep(struct dvb_frontend * fe)
{
struct dvb_bt8xx_card *bt = fe->dvb->priv;
bttv_write_gpio(bt->bttv_nr, 0x0001, 0x0000);
}
static const struct or51211_config or51211_config = {
.demod_address = 0x15,
.request_firmware = or51211_request_firmware,
.setmode = or51211_setmode,
.reset = or51211_reset,
.sleep = or51211_sleep,
};
static int vp3021_alps_tded4_tuner_set_params(struct dvb_frontend *fe)
{
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
u8 buf[4];
u32 div;
struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf) };
div = (c->frequency + 36166667) / 166667;
buf[0] = (div >> 8) & 0x7F;
buf[1] = div & 0xFF;
buf[2] = 0x85;
if ((c->frequency >= 47000000) && (c->frequency < 153000000))
buf[3] = 0x01;
else if ((c->frequency >= 153000000) && (c->frequency < 430000000))
buf[3] = 0x02;
else if ((c->frequency >= 430000000) && (c->frequency < 824000000))
buf[3] = 0x0C;
else if ((c->frequency >= 824000000) && (c->frequency < 863000000))
buf[3] = 0x8C;
else
return -EINVAL;
if (fe->ops.i2c_gate_ctrl)
fe->ops.i2c_gate_ctrl(fe, 1);
i2c_transfer(card->i2c_adapter, &msg, 1);
return 0;
}
static struct nxt6000_config vp3021_alps_tded4_config = {
.demod_address = 0x0a,
.clock_inversion = 1,
};
static int digitv_alps_tded4_demod_init(struct dvb_frontend* fe)
{
static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
static u8 mt352_reset [] = { 0x50, 0x80 };
static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
static u8 mt352_agc_cfg [] = { 0x67, 0x20, 0xa0 };
static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
udelay(2000);
mt352_write(fe, mt352_reset, sizeof(mt352_reset));
mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
return 0;
}
static int digitv_alps_tded4_tuner_calc_regs(struct dvb_frontend *fe, u8 *pllbuf, int buf_len)
{
u32 div;
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
if (buf_len < 5)
return -EINVAL;
div = (((c->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
pllbuf[0] = 0x61;
pllbuf[1] = (div >> 8) & 0x7F;
pllbuf[2] = div & 0xFF;
pllbuf[3] = 0x85;
dprintk("frequency %u, div %u\n", c->frequency, div);
if (c->frequency < 470000000)
pllbuf[4] = 0x02;
else if (c->frequency > 823000000)
pllbuf[4] = 0x88;
else
pllbuf[4] = 0x08;
if (c->bandwidth_hz == 8000000)
pllbuf[4] |= 0x04;
return 5;
}
static void digitv_alps_tded4_reset(struct dvb_bt8xx_card *bt)
{
/*
* Reset the frontend, must be called before trying
* to initialise the MT352 or mt352_attach
* will fail. Same goes for the nxt6000 frontend.
*
*/
int ret = bttv_gpio_enable(bt->bttv_nr, 0x08, 0x08);
if (ret != 0)
pr_warn("digitv_alps_tded4: Init Error - Can't Reset DVR (%i)\n",
ret);
/* Pulse the reset line */
bttv_write_gpio(bt->bttv_nr, 0x08, 0x08); /* High */
bttv_write_gpio(bt->bttv_nr, 0x08, 0x00); /* Low */
msleep(100);
bttv_write_gpio(bt->bttv_nr, 0x08, 0x08); /* High */
}
static struct mt352_config digitv_alps_tded4_config = {
.demod_address = 0x0a,
.demod_init = digitv_alps_tded4_demod_init,
};
static struct lgdt330x_config tdvs_tua6034_config = {
.demod_chip = LGDT3303,
.serial_mpeg = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
};
static void lgdt330x_reset(struct dvb_bt8xx_card *bt)
{
/* Set pin 27 of the lgdt3303 chip high to reset the frontend */
/* Pulse the reset line */
bttv_write_gpio(bt->bttv_nr, 0x00e00007, 0x00000001); /* High */
bttv_write_gpio(bt->bttv_nr, 0x00e00007, 0x00000000); /* Low */
msleep(100);
bttv_write_gpio(bt->bttv_nr, 0x00e00007, 0x00000001); /* High */
msleep(100);
}
static void frontend_init(struct dvb_bt8xx_card *card, u32 type)
{
struct dst_state* state = NULL;
switch(type) {
case BTTV_BOARD_DVICO_DVBT_LITE:
card->fe = dvb_attach(mt352_attach, &thomson_dtt7579_config, card->i2c_adapter);
if (card->fe == NULL)
card->fe = dvb_attach(zl10353_attach, &thomson_dtt7579_zl10353_config,
card->i2c_adapter);
if (card->fe != NULL) {
card->fe->ops.tuner_ops.calc_regs = thomson_dtt7579_tuner_calc_regs;
card->fe->ops.info.frequency_min_hz = 174 * MHz;
card->fe->ops.info.frequency_max_hz = 862 * MHz;
}
break;
case BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE:
lgdt330x_reset(card);
card->fe = dvb_attach(lgdt330x_attach, &tdvs_tua6034_config,
0x0e, card->i2c_adapter);
if (card->fe != NULL) {
dvb_attach(simple_tuner_attach, card->fe,
card->i2c_adapter, 0x61,
TUNER_LG_TDVS_H06XF);
dprintk("dvb_bt8xx: lgdt330x detected\n");
}
break;
case BTTV_BOARD_NEBULA_DIGITV:
/*
* It is possible to determine the correct frontend using the I2C bus (see the Nebula SDK);
* this would be a cleaner solution than trying each frontend in turn.
*/
/* Old Nebula (marked (c)2003 on high profile pci card) has nxt6000 demod */
digitv_alps_tded4_reset(card);
card->fe = dvb_attach(nxt6000_attach, &vp3021_alps_tded4_config, card->i2c_adapter);
if (card->fe != NULL) {
card->fe->ops.tuner_ops.set_params = vp3021_alps_tded4_tuner_set_params;
dprintk("dvb_bt8xx: an nxt6000 was detected on your digitv card\n");
break;
}
/* New Nebula (marked (c)2005 on low profile pci card) has mt352 demod */
digitv_alps_tded4_reset(card);
card->fe = dvb_attach(mt352_attach, &digitv_alps_tded4_config, card->i2c_adapter);
if (card->fe != NULL) {
card->fe->ops.tuner_ops.calc_regs = digitv_alps_tded4_tuner_calc_regs;
dprintk("dvb_bt8xx: an mt352 was detected on your digitv card\n");
}
break;
case BTTV_BOARD_AVDVBT_761:
card->fe = dvb_attach(sp887x_attach, µtune_mt7202dtf_config, card->i2c_adapter);
if (card->fe) {
card->fe->ops.tuner_ops.set_params = microtune_mt7202dtf_tuner_set_params;
}
break;
case BTTV_BOARD_AVDVBT_771:
card->fe = dvb_attach(mt352_attach, &advbt771_samsung_tdtc9251dh0_config, card->i2c_adapter);
if (card->fe != NULL) {
card->fe->ops.tuner_ops.calc_regs = advbt771_samsung_tdtc9251dh0_tuner_calc_regs;
card->fe->ops.info.frequency_min_hz = 174 * MHz;
card->fe->ops.info.frequency_max_hz = 862 * MHz;
}
break;
case BTTV_BOARD_TWINHAN_DST:
/* DST is not a frontend driver !!! */
state = kmalloc(sizeof (struct dst_state), GFP_KERNEL);
if (!state) {
pr_err("No memory\n");
break;
}
/* Setup the Card */
state->config = &dst_config;
state->i2c = card->i2c_adapter;
state->bt = card->bt;
state->dst_ca = NULL;
/* DST is not a frontend, attaching the ASIC */
if (dvb_attach(dst_attach, state, &card->dvb_adapter) == NULL) {
pr_err("%s: Could not find a Twinhan DST\n", __func__);
kfree(state);
break;
}
/* Attach other DST peripherals if any */
/* Conditional Access device */
card->fe = &state->frontend;
if (state->dst_hw_cap & DST_TYPE_HAS_CA)
dvb_attach(dst_ca_attach, state, &card->dvb_adapter);
break;
case BTTV_BOARD_PINNACLESAT:
card->fe = dvb_attach(cx24110_attach, &pctvsat_config, card->i2c_adapter);
if (card->fe) {
card->fe->ops.tuner_ops.init = pinnsat_tuner_init;
card->fe->ops.tuner_ops.sleep = pinnsat_tuner_sleep;
card->fe->ops.tuner_ops.set_params = cx24108_tuner_set_params;
}
break;
case BTTV_BOARD_PC_HDTV:
card->fe = dvb_attach(or51211_attach, &or51211_config, card->i2c_adapter);
if (card->fe != NULL)
dvb_attach(simple_tuner_attach, card->fe,
card->i2c_adapter, 0x61,
TUNER_PHILIPS_FCV1236D);
break;
}
if (card->fe == NULL)
pr_err("A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
card->bt->dev->vendor,
card->bt->dev->device,
card->bt->dev->subsystem_vendor,
card->bt->dev->subsystem_device);
else
if (dvb_register_frontend(&card->dvb_adapter, card->fe)) {
pr_err("Frontend registration failed!\n");
dvb_frontend_detach(card->fe);
card->fe = NULL;
}
}
static int dvb_bt8xx_load_card(struct dvb_bt8xx_card *card, u32 type)
{
int result;
result = dvb_register_adapter(&card->dvb_adapter, card->card_name,
THIS_MODULE, &card->bt->dev->dev,
adapter_nr);
if (result < 0) {
pr_err("dvb_register_adapter failed (errno = %d)\n", result);
return result;
}
card->dvb_adapter.priv = card;
card->bt->adapter = card->i2c_adapter;
memset(&card->demux, 0, sizeof(struct dvb_demux));
card->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
card->demux.priv = card;
card->demux.filternum = 256;
card->demux.feednum = 256;
card->demux.start_feed = dvb_bt8xx_start_feed;
card->demux.stop_feed = dvb_bt8xx_stop_feed;
card->demux.write_to_decoder = NULL;
result = dvb_dmx_init(&card->demux);
if (result < 0) {
pr_err("dvb_dmx_init failed (errno = %d)\n", result);
goto err_unregister_adaptor;
}
card->dmxdev.filternum = 256;
card->dmxdev.demux = &card->demux.dmx;
card->dmxdev.capabilities = 0;
result = dvb_dmxdev_init(&card->dmxdev, &card->dvb_adapter);
if (result < 0) {
pr_err("dvb_dmxdev_init failed (errno = %d)\n", result);
goto err_dmx_release;
}
card->fe_hw.source = DMX_FRONTEND_0;
result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_hw);
if (result < 0) {
pr_err("dvb_dmx_init failed (errno = %d)\n", result);
goto err_dmxdev_release;
}
card->fe_mem.source = DMX_MEMORY_FE;
result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_mem);
if (result < 0) {
pr_err("dvb_dmx_init failed (errno = %d)\n", result);
goto err_remove_hw_frontend;
}
result = card->demux.dmx.connect_frontend(&card->demux.dmx, &card->fe_hw);
if (result < 0) {
pr_err("dvb_dmx_init failed (errno = %d)\n", result);
goto err_remove_mem_frontend;
}
result = dvb_net_init(&card->dvb_adapter, &card->dvbnet, &card->demux.dmx);
if (result < 0) {
pr_err("dvb_net_init failed (errno = %d)\n", result);
goto err_disconnect_frontend;
}
tasklet_setup(&card->bt->tasklet, dvb_bt8xx_task);
frontend_init(card, type);
return 0;
err_disconnect_frontend:
card->demux.dmx.disconnect_frontend(&card->demux.dmx);
err_remove_mem_frontend:
card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
err_remove_hw_frontend:
card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
err_dmxdev_release:
dvb_dmxdev_release(&card->dmxdev);
err_dmx_release:
dvb_dmx_release(&card->demux);
err_unregister_adaptor:
dvb_unregister_adapter(&card->dvb_adapter);
return result;
}
static int dvb_bt8xx_probe(struct bttv_sub_device *sub)
{
struct dvb_bt8xx_card *card;
struct pci_dev* bttv_pci_dev;
int ret;
if (!(card = kzalloc(sizeof(struct dvb_bt8xx_card), GFP_KERNEL)))
return -ENOMEM;
mutex_init(&card->lock);
card->bttv_nr = sub->core->nr;
strscpy(card->card_name, sub->core->v4l2_dev.name,
sizeof(card->card_name));
card->i2c_adapter = &sub->core->i2c_adap;
switch(sub->core->type) {
case BTTV_BOARD_PINNACLESAT:
card->gpio_mode = 0x0400c060;
/* should be: BT878_A_GAIN=0,BT878_A_PWRDN,BT878_DA_DPM,BT878_DA_SBR,
BT878_DA_IOM=1,BT878_DA_APP to enable serial highspeed mode. */
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
break;
case BTTV_BOARD_DVICO_DVBT_LITE:
card->gpio_mode = 0x0400C060;
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
/* 26, 15, 14, 6, 5
* A_PWRDN DA_DPM DA_SBR DA_IOM_DA
* DA_APP(parallel) */
break;
case BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE:
card->gpio_mode = 0x0400c060;
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
break;
case BTTV_BOARD_NEBULA_DIGITV:
case BTTV_BOARD_AVDVBT_761:
card->gpio_mode = (1 << 26) | (1 << 14) | (1 << 5);
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
/* A_PWRDN DA_SBR DA_APP (high speed serial) */
break;
case BTTV_BOARD_AVDVBT_771: //case 0x07711461:
card->gpio_mode = 0x0400402B;
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
/* A_PWRDN DA_SBR DA_APP[0] PKTP=10 RISC_ENABLE FIFO_ENABLE*/
break;
case BTTV_BOARD_TWINHAN_DST:
card->gpio_mode = 0x2204f2c;
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_APABORT | BT878_ARIPERR |
BT878_APPERR | BT878_AFBUS;
/* 25,21,14,11,10,9,8,3,2 then
* 0x33 = 5,4,1,0
* A_SEL=SML, DA_MLB, DA_SBR,
* DA_SDR=f, fifo trigger = 32 DWORDS
* IOM = 0 == audio A/D
* DPM = 0 == digital audio mode
* == async data parallel port
* then 0x33 (13 is set by start_capture)
* DA_APP = async data parallel port,
* ACAP_EN = 1,
* RISC+FIFO ENABLE */
break;
case BTTV_BOARD_PC_HDTV:
card->gpio_mode = 0x0100EC7B;
card->op_sync_orin = BT878_RISC_SYNC_MASK;
card->irq_err_ignore = BT878_AFBUS | BT878_AFDSR;
break;
default:
pr_err("Unknown bttv card type: %d\n", sub->core->type);
kfree(card);
return -ENODEV;
}
dprintk("dvb_bt8xx: identified card%d as %s\n", card->bttv_nr, card->card_name);
if (!(bttv_pci_dev = bttv_get_pcidev(card->bttv_nr))) {
pr_err("no pci device for card %d\n", card->bttv_nr);
kfree(card);
return -ENODEV;
}
if (!(card->bt = dvb_bt8xx_878_match(card->bttv_nr, bttv_pci_dev))) {
pr_err("unable to determine DMA core of card %d,\n", card->bttv_nr);
pr_err("if you have the ALSA bt87x audio driver installed, try removing it.\n");
kfree(card);
return -ENODEV;
}
mutex_init(&card->bt->gpio_lock);
card->bt->bttv_nr = sub->core->nr;
if ( (ret = dvb_bt8xx_load_card(card, sub->core->type)) ) {
kfree(card);
return ret;
}
dev_set_drvdata(&sub->dev, card);
return 0;
}
static void dvb_bt8xx_remove(struct bttv_sub_device *sub)
{
struct dvb_bt8xx_card *card = dev_get_drvdata(&sub->dev);
dprintk("dvb_bt8xx: unloading card%d\n", card->bttv_nr);
bt878_stop(card->bt);
tasklet_kill(&card->bt->tasklet);
dvb_net_release(&card->dvbnet);
card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
dvb_dmxdev_release(&card->dmxdev);
dvb_dmx_release(&card->demux);
if (card->fe) {
dvb_unregister_frontend(card->fe);
dvb_frontend_detach(card->fe);
}
dvb_unregister_adapter(&card->dvb_adapter);
kfree(card);
}
static struct bttv_sub_driver driver = {
.drv = {
.name = "dvb-bt8xx",
},
.probe = dvb_bt8xx_probe,
.remove = dvb_bt8xx_remove,
/* FIXME:
* .shutdown = dvb_bt8xx_shutdown,
* .suspend = dvb_bt8xx_suspend,
* .resume = dvb_bt8xx_resume,
*/
};
static int __init dvb_bt8xx_init(void)
{
return bttv_sub_register(&driver, "dvb");
}
static void __exit dvb_bt8xx_exit(void)
{
bttv_sub_unregister(&driver);
}
module_init(dvb_bt8xx_init);
module_exit(dvb_bt8xx_exit);
MODULE_DESCRIPTION("Bt8xx based DVB adapter driver");
MODULE_AUTHOR("Florian Schirmer <[email protected]>");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/bt8xx/dvb-bt8xx.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
Frontend/Card driver for TwinHan DST Frontend
Copyright (C) 2003 Jamie Honan
Copyright (C) 2004, 2005 Manu Abraham ([email protected])
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <asm/div64.h>
#include <media/dvb_frontend.h>
#include "dst_priv.h"
#include "dst_common.h"
static unsigned int verbose;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbosity level (0 to 3)");
static unsigned int dst_addons;
module_param(dst_addons, int, 0644);
MODULE_PARM_DESC(dst_addons, "CA daughterboard, default is 0 (No addons)");
static unsigned int dst_algo;
module_param(dst_algo, int, 0644);
MODULE_PARM_DESC(dst_algo, "tuning algo: default is 0=(SW), 1=(HW)");
#define HAS_LOCK 1
#define ATTEMPT_TUNE 2
#define HAS_POWER 4
#define dprintk(level, fmt, arg...) do { \
if (level >= verbose) \
printk(KERN_DEBUG pr_fmt("%s: " fmt), \
__func__, ##arg); \
} while(0)
static int dst_command(struct dst_state *state, u8 *data, u8 len);
static void dst_packsize(struct dst_state *state, int psize)
{
union dst_gpio_packet bits;
bits.psize = psize;
bt878_device_control(state->bt, DST_IG_TS, &bits);
}
static int dst_gpio_outb(struct dst_state *state, u32 mask, u32 enbb,
u32 outhigh, int delay)
{
union dst_gpio_packet enb;
union dst_gpio_packet bits;
int err;
enb.enb.mask = mask;
enb.enb.enable = enbb;
dprintk(2, "mask=[%04x], enbb=[%04x], outhigh=[%04x]\n",
mask, enbb, outhigh);
if ((err = bt878_device_control(state->bt, DST_IG_ENABLE, &enb)) < 0) {
dprintk(2, "dst_gpio_enb error (err == %i, mask == %02x, enb == %02x)\n",
err, mask, enbb);
return -EREMOTEIO;
}
udelay(1000);
/* because complete disabling means no output, no need to do output packet */
if (enbb == 0)
return 0;
if (delay)
msleep(10);
bits.outp.mask = enbb;
bits.outp.highvals = outhigh;
if ((err = bt878_device_control(state->bt, DST_IG_WRITE, &bits)) < 0) {
dprintk(2, "dst_gpio_outb error (err == %i, enbb == %02x, outhigh == %02x)\n",
err, enbb, outhigh);
return -EREMOTEIO;
}
return 0;
}
static int dst_gpio_inb(struct dst_state *state, u8 *result)
{
union dst_gpio_packet rd_packet;
int err;
*result = 0;
if ((err = bt878_device_control(state->bt, DST_IG_READ, &rd_packet)) < 0) {
pr_err("dst_gpio_inb error (err == %i)\n", err);
return -EREMOTEIO;
}
*result = (u8) rd_packet.rd.value;
return 0;
}
int rdc_reset_state(struct dst_state *state)
{
dprintk(2, "Resetting state machine\n");
if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, 0, NO_DELAY) < 0) {
pr_err("dst_gpio_outb ERROR !\n");
return -1;
}
msleep(10);
if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, RDC_8820_INT, NO_DELAY) < 0) {
pr_err("dst_gpio_outb ERROR !\n");
msleep(10);
return -1;
}
return 0;
}
EXPORT_SYMBOL(rdc_reset_state);
static int rdc_8820_reset(struct dst_state *state)
{
dprintk(3, "Resetting DST\n");
if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, 0, NO_DELAY) < 0) {
pr_err("dst_gpio_outb ERROR !\n");
return -1;
}
udelay(1000);
if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, RDC_8820_RESET, DELAY) < 0) {
pr_err("dst_gpio_outb ERROR !\n");
return -1;
}
return 0;
}
static int dst_pio_enable(struct dst_state *state)
{
if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_ENABLE, 0, NO_DELAY) < 0) {
pr_err("dst_gpio_outb ERROR !\n");
return -1;
}
udelay(1000);
return 0;
}
int dst_pio_disable(struct dst_state *state)
{
if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_DISABLE, RDC_8820_PIO_0_DISABLE, NO_DELAY) < 0) {
pr_err("dst_gpio_outb ERROR !\n");
return -1;
}
if (state->type_flags & DST_TYPE_HAS_FW_1)
udelay(1000);
return 0;
}
EXPORT_SYMBOL(dst_pio_disable);
int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode)
{
u8 reply;
int i;
for (i = 0; i < 200; i++) {
if (dst_gpio_inb(state, &reply) < 0) {
pr_err("dst_gpio_inb ERROR !\n");
return -1;
}
if ((reply & RDC_8820_PIO_0_ENABLE) == 0) {
dprintk(2, "dst wait ready after %d\n", i);
return 1;
}
msleep(10);
}
dprintk(1, "dst wait NOT ready after %d\n", i);
return 0;
}
EXPORT_SYMBOL(dst_wait_dst_ready);
int dst_error_recovery(struct dst_state *state)
{
dprintk(1, "Trying to return from previous errors.\n");
dst_pio_disable(state);
msleep(10);
dst_pio_enable(state);
msleep(10);
return 0;
}
EXPORT_SYMBOL(dst_error_recovery);
int dst_error_bailout(struct dst_state *state)
{
dprintk(2, "Trying to bailout from previous error.\n");
rdc_8820_reset(state);
dst_pio_disable(state);
msleep(10);
return 0;
}
EXPORT_SYMBOL(dst_error_bailout);
int dst_comm_init(struct dst_state *state)
{
dprintk(2, "Initializing DST.\n");
if ((dst_pio_enable(state)) < 0) {
pr_err("PIO Enable Failed\n");
return -1;
}
if ((rdc_reset_state(state)) < 0) {
pr_err("RDC 8820 State RESET Failed.\n");
return -1;
}
if (state->type_flags & DST_TYPE_HAS_FW_1)
msleep(100);
else
msleep(5);
return 0;
}
EXPORT_SYMBOL(dst_comm_init);
int write_dst(struct dst_state *state, u8 *data, u8 len)
{
struct i2c_msg msg = {
.addr = state->config->demod_address,
.flags = 0,
.buf = data,
.len = len
};
int err;
u8 cnt;
dprintk(1, "writing [ %*ph ]\n", len, data);
for (cnt = 0; cnt < 2; cnt++) {
if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
dprintk(2, "_write_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)\n",
err, len, data[0]);
dst_error_recovery(state);
continue;
} else
break;
}
if (cnt >= 2) {
dprintk(2, "RDC 8820 RESET\n");
dst_error_bailout(state);
return -1;
}
return 0;
}
EXPORT_SYMBOL(write_dst);
int read_dst(struct dst_state *state, u8 *ret, u8 len)
{
struct i2c_msg msg = {
.addr = state->config->demod_address,
.flags = I2C_M_RD,
.buf = ret,
.len = len
};
int err;
int cnt;
for (cnt = 0; cnt < 2; cnt++) {
if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
dprintk(2, "read_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)\n",
err, len, ret[0]);
dst_error_recovery(state);
continue;
} else
break;
}
if (cnt >= 2) {
dprintk(2, "RDC 8820 RESET\n");
dst_error_bailout(state);
return -1;
}
dprintk(3, "reply is %*ph\n", len, ret);
return 0;
}
EXPORT_SYMBOL(read_dst);
static int dst_set_polarization(struct dst_state *state)
{
switch (state->voltage) {
case SEC_VOLTAGE_13: /* Vertical */
dprintk(2, "Polarization=[Vertical]\n");
state->tx_tuna[8] &= ~0x40;
break;
case SEC_VOLTAGE_18: /* Horizontal */
dprintk(2, "Polarization=[Horizontal]\n");
state->tx_tuna[8] |= 0x40;
break;
case SEC_VOLTAGE_OFF:
break;
}
return 0;
}
static int dst_set_freq(struct dst_state *state, u32 freq)
{
state->frequency = freq;
dprintk(2, "set Frequency %u\n", freq);
if (state->dst_type == DST_TYPE_IS_SAT) {
freq = freq / 1000;
if (freq < 950 || freq > 2150)
return -EINVAL;
state->tx_tuna[2] = (freq >> 8);
state->tx_tuna[3] = (u8) freq;
state->tx_tuna[4] = 0x01;
state->tx_tuna[8] &= ~0x04;
if (state->type_flags & DST_TYPE_HAS_OBS_REGS) {
if (freq < 1531)
state->tx_tuna[8] |= 0x04;
}
} else if (state->dst_type == DST_TYPE_IS_TERR) {
freq = freq / 1000;
if (freq < 137000 || freq > 858000)
return -EINVAL;
state->tx_tuna[2] = (freq >> 16) & 0xff;
state->tx_tuna[3] = (freq >> 8) & 0xff;
state->tx_tuna[4] = (u8) freq;
} else if (state->dst_type == DST_TYPE_IS_CABLE) {
freq = freq / 1000;
state->tx_tuna[2] = (freq >> 16) & 0xff;
state->tx_tuna[3] = (freq >> 8) & 0xff;
state->tx_tuna[4] = (u8) freq;
} else if (state->dst_type == DST_TYPE_IS_ATSC) {
freq = freq / 1000;
if (freq < 51000 || freq > 858000)
return -EINVAL;
state->tx_tuna[2] = (freq >> 16) & 0xff;
state->tx_tuna[3] = (freq >> 8) & 0xff;
state->tx_tuna[4] = (u8) freq;
state->tx_tuna[5] = 0x00; /* ATSC */
state->tx_tuna[6] = 0x00;
if (state->dst_hw_cap & DST_TYPE_HAS_ANALOG)
state->tx_tuna[7] = 0x00; /* Digital */
} else
return -EINVAL;
return 0;
}
static int dst_set_bandwidth(struct dst_state *state, u32 bandwidth)
{
state->bandwidth = bandwidth;
if (state->dst_type != DST_TYPE_IS_TERR)
return -EOPNOTSUPP;
switch (bandwidth) {
case 6000000:
if (state->dst_hw_cap & DST_TYPE_HAS_CA)
state->tx_tuna[7] = 0x06;
else {
state->tx_tuna[6] = 0x06;
state->tx_tuna[7] = 0x00;
}
break;
case 7000000:
if (state->dst_hw_cap & DST_TYPE_HAS_CA)
state->tx_tuna[7] = 0x07;
else {
state->tx_tuna[6] = 0x07;
state->tx_tuna[7] = 0x00;
}
break;
case 8000000:
if (state->dst_hw_cap & DST_TYPE_HAS_CA)
state->tx_tuna[7] = 0x08;
else {
state->tx_tuna[6] = 0x08;
state->tx_tuna[7] = 0x00;
}
break;
default:
return -EINVAL;
}
return 0;
}
static int dst_set_inversion(struct dst_state *state,
enum fe_spectral_inversion inversion)
{
state->inversion = inversion;
switch (inversion) {
case INVERSION_OFF: /* Inversion = Normal */
state->tx_tuna[8] &= ~0x80;
break;
case INVERSION_ON:
state->tx_tuna[8] |= 0x80;
break;
default:
return -EINVAL;
}
return 0;
}
static int dst_set_fec(struct dst_state *state, enum fe_code_rate fec)
{
state->fec = fec;
return 0;
}
static enum fe_code_rate dst_get_fec(struct dst_state *state)
{
return state->fec;
}
static int dst_set_symbolrate(struct dst_state *state, u32 srate)
{
u32 symcalc;
u64 sval;
state->symbol_rate = srate;
if (state->dst_type == DST_TYPE_IS_TERR) {
return -EOPNOTSUPP;
}
dprintk(2, "set symrate %u\n", srate);
srate /= 1000;
if (state->dst_type == DST_TYPE_IS_SAT) {
if (state->type_flags & DST_TYPE_HAS_SYMDIV) {
sval = srate;
sval <<= 20;
do_div(sval, 88000);
symcalc = (u32) sval;
dprintk(2, "set symcalc %u\n", symcalc);
state->tx_tuna[5] = (u8) (symcalc >> 12);
state->tx_tuna[6] = (u8) (symcalc >> 4);
state->tx_tuna[7] = (u8) (symcalc << 4);
} else {
state->tx_tuna[5] = (u8) (srate >> 16) & 0x7f;
state->tx_tuna[6] = (u8) (srate >> 8);
state->tx_tuna[7] = (u8) srate;
}
state->tx_tuna[8] &= ~0x20;
if (state->type_flags & DST_TYPE_HAS_OBS_REGS) {
if (srate > 8000)
state->tx_tuna[8] |= 0x20;
}
} else if (state->dst_type == DST_TYPE_IS_CABLE) {
dprintk(3, "%s\n", state->fw_name);
if (!strncmp(state->fw_name, "DCTNEW", 6)) {
state->tx_tuna[5] = (u8) (srate >> 8);
state->tx_tuna[6] = (u8) srate;
state->tx_tuna[7] = 0x00;
} else if (!strncmp(state->fw_name, "DCT-CI", 6)) {
state->tx_tuna[5] = 0x00;
state->tx_tuna[6] = (u8) (srate >> 8);
state->tx_tuna[7] = (u8) srate;
}
}
return 0;
}
static int dst_set_modulation(struct dst_state *state,
enum fe_modulation modulation)
{
if (state->dst_type != DST_TYPE_IS_CABLE)
return -EOPNOTSUPP;
state->modulation = modulation;
switch (modulation) {
case QAM_16:
state->tx_tuna[8] = 0x10;
break;
case QAM_32:
state->tx_tuna[8] = 0x20;
break;
case QAM_64:
state->tx_tuna[8] = 0x40;
break;
case QAM_128:
state->tx_tuna[8] = 0x80;
break;
case QAM_256:
if (!strncmp(state->fw_name, "DCTNEW", 6))
state->tx_tuna[8] = 0xff;
else if (!strncmp(state->fw_name, "DCT-CI", 6))
state->tx_tuna[8] = 0x00;
break;
case QPSK:
case QAM_AUTO:
case VSB_8:
case VSB_16:
default:
return -EINVAL;
}
return 0;
}
static enum fe_modulation dst_get_modulation(struct dst_state *state)
{
return state->modulation;
}
u8 dst_check_sum(u8 *buf, u32 len)
{
u32 i;
u8 val = 0;
if (!len)
return 0;
for (i = 0; i < len; i++) {
val += buf[i];
}
return ((~val) + 1);
}
EXPORT_SYMBOL(dst_check_sum);
static void dst_type_flags_print(struct dst_state *state)
{
u32 type_flags = state->type_flags;
pr_err("DST type flags :\n");
if (type_flags & DST_TYPE_HAS_TS188)
pr_err(" 0x%x newtuner\n", DST_TYPE_HAS_TS188);
if (type_flags & DST_TYPE_HAS_NEWTUNE_2)
pr_err(" 0x%x newtuner 2\n", DST_TYPE_HAS_NEWTUNE_2);
if (type_flags & DST_TYPE_HAS_TS204)
pr_err(" 0x%x ts204\n", DST_TYPE_HAS_TS204);
if (type_flags & DST_TYPE_HAS_VLF)
pr_err(" 0x%x VLF\n", DST_TYPE_HAS_VLF);
if (type_flags & DST_TYPE_HAS_SYMDIV)
pr_err(" 0x%x symdiv\n", DST_TYPE_HAS_SYMDIV);
if (type_flags & DST_TYPE_HAS_FW_1)
pr_err(" 0x%x firmware version = 1\n", DST_TYPE_HAS_FW_1);
if (type_flags & DST_TYPE_HAS_FW_2)
pr_err(" 0x%x firmware version = 2\n", DST_TYPE_HAS_FW_2);
if (type_flags & DST_TYPE_HAS_FW_3)
pr_err(" 0x%x firmware version = 3\n", DST_TYPE_HAS_FW_3);
pr_err("\n");
}
static int dst_type_print(struct dst_state *state, u8 type)
{
char *otype;
switch (type) {
case DST_TYPE_IS_SAT:
otype = "satellite";
break;
case DST_TYPE_IS_TERR:
otype = "terrestrial";
break;
case DST_TYPE_IS_CABLE:
otype = "cable";
break;
case DST_TYPE_IS_ATSC:
otype = "atsc";
break;
default:
dprintk(2, "invalid dst type %d\n", type);
return -EINVAL;
}
dprintk(2, "DST type: %s\n", otype);
return 0;
}
static struct tuner_types tuner_list[] = {
{
.tuner_type = TUNER_TYPE_L64724,
.tuner_name = "L 64724",
.board_name = "UNKNOWN",
.fw_name = "UNKNOWN"
},
{
.tuner_type = TUNER_TYPE_STV0299,
.tuner_name = "STV 0299",
.board_name = "VP1020",
.fw_name = "DST-MOT"
},
{
.tuner_type = TUNER_TYPE_STV0299,
.tuner_name = "STV 0299",
.board_name = "VP1020",
.fw_name = "DST-03T"
},
{
.tuner_type = TUNER_TYPE_MB86A15,
.tuner_name = "MB 86A15",
.board_name = "VP1022",
.fw_name = "DST-03T"
},
{
.tuner_type = TUNER_TYPE_MB86A15,
.tuner_name = "MB 86A15",
.board_name = "VP1025",
.fw_name = "DST-03T"
},
{
.tuner_type = TUNER_TYPE_STV0299,
.tuner_name = "STV 0299",
.board_name = "VP1030",
.fw_name = "DST-CI"
},
{
.tuner_type = TUNER_TYPE_STV0299,
.tuner_name = "STV 0299",
.board_name = "VP1030",
.fw_name = "DSTMCI"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP2021",
.fw_name = "DCTNEW"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP2030",
.fw_name = "DCT-CI"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP2031",
.fw_name = "DCT-CI"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP2040",
.fw_name = "DCT-CI"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP3020",
.fw_name = "DTTFTA"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP3021",
.fw_name = "DTTFTA"
},
{
.tuner_type = TUNER_TYPE_TDA10046,
.tuner_name = "TDA10046",
.board_name = "VP3040",
.fw_name = "DTT-CI"
},
{
.tuner_type = TUNER_TYPE_UNKNOWN,
.tuner_name = "UNKNOWN",
.board_name = "VP3051",
.fw_name = "DTTNXT"
},
{
.tuner_type = TUNER_TYPE_NXT200x,
.tuner_name = "NXT200x",
.board_name = "VP3220",
.fw_name = "ATSCDI"
},
{
.tuner_type = TUNER_TYPE_NXT200x,
.tuner_name = "NXT200x",
.board_name = "VP3250",
.fw_name = "ATSCAD"
},
};
/*
Known cards list
Satellite
-------------------
200103A
VP-1020 DST-MOT LG(old), TS=188
VP-1020 DST-03T LG(new), TS=204
VP-1022 DST-03T LG(new), TS=204
VP-1025 DST-03T LG(new), TS=204
VP-1030 DSTMCI, LG(new), TS=188
VP-1032 DSTMCI, LG(new), TS=188
Cable
-------------------
VP-2030 DCT-CI, Samsung, TS=204
VP-2021 DCT-CI, Unknown, TS=204
VP-2031 DCT-CI, Philips, TS=188
VP-2040 DCT-CI, Philips, TS=188, with CA daughter board
VP-2040 DCT-CI, Philips, TS=204, without CA daughter board
Terrestrial
-------------------
VP-3050 DTTNXT TS=188
VP-3040 DTT-CI, Philips, TS=188
VP-3040 DTT-CI, Philips, TS=204
ATSC
-------------------
VP-3220 ATSCDI, TS=188
VP-3250 ATSCAD, TS=188
*/
static struct dst_types dst_tlist[] = {
{
.device_id = "200103A",
.offset = 0,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1 | DST_TYPE_HAS_OBS_REGS,
.dst_feature = 0,
.tuner_type = 0
}, /* obsolete */
{
.device_id = "DST-020",
.offset = 0,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
.dst_feature = 0,
.tuner_type = 0
}, /* obsolete */
{
.device_id = "DST-030",
.offset = 0,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_1,
.dst_feature = 0,
.tuner_type = 0
}, /* obsolete */
{
.device_id = "DST-03T",
.offset = 0,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_2,
.dst_feature = DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4 | DST_TYPE_HAS_DISEQC5
| DST_TYPE_HAS_MAC | DST_TYPE_HAS_MOTO,
.tuner_type = TUNER_TYPE_MULTI
},
{
.device_id = "DST-MOT",
.offset = 0,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
.dst_feature = 0,
.tuner_type = 0
}, /* obsolete */
{
.device_id = "DST-CI",
.offset = 1,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_1,
.dst_feature = DST_TYPE_HAS_CA,
.tuner_type = 0
}, /* An OEM board */
{
.device_id = "DSTMCI",
.offset = 1,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD | DST_TYPE_HAS_INC_COUNT | DST_TYPE_HAS_VLF,
.dst_feature = DST_TYPE_HAS_CA | DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4
| DST_TYPE_HAS_MOTO | DST_TYPE_HAS_MAC,
.tuner_type = TUNER_TYPE_MULTI
},
{
.device_id = "DSTFCI",
.offset = 1,
.dst_type = DST_TYPE_IS_SAT,
.type_flags = DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_1,
.dst_feature = 0,
.tuner_type = 0
}, /* unknown to vendor */
{
.device_id = "DCT-CI",
.offset = 1,
.dst_type = DST_TYPE_IS_CABLE,
.type_flags = DST_TYPE_HAS_MULTI_FE | DST_TYPE_HAS_FW_1 | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_VLF,
.dst_feature = DST_TYPE_HAS_CA,
.tuner_type = 0
},
{
.device_id = "DCTNEW",
.offset = 1,
.dst_type = DST_TYPE_IS_CABLE,
.type_flags = DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_3 | DST_TYPE_HAS_FW_BUILD | DST_TYPE_HAS_MULTI_FE,
.dst_feature = 0,
.tuner_type = 0
},
{
.device_id = "DTT-CI",
.offset = 1,
.dst_type = DST_TYPE_IS_TERR,
.type_flags = DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_MULTI_FE | DST_TYPE_HAS_VLF,
.dst_feature = DST_TYPE_HAS_CA,
.tuner_type = 0
},
{
.device_id = "DTTDIG",
.offset = 1,
.dst_type = DST_TYPE_IS_TERR,
.type_flags = DST_TYPE_HAS_FW_2,
.dst_feature = 0,
.tuner_type = 0
},
{
.device_id = "DTTNXT",
.offset = 1,
.dst_type = DST_TYPE_IS_TERR,
.type_flags = DST_TYPE_HAS_FW_2,
.dst_feature = DST_TYPE_HAS_ANALOG,
.tuner_type = 0
},
{
.device_id = "ATSCDI",
.offset = 1,
.dst_type = DST_TYPE_IS_ATSC,
.type_flags = DST_TYPE_HAS_FW_2,
.dst_feature = 0,
.tuner_type = 0
},
{
.device_id = "ATSCAD",
.offset = 1,
.dst_type = DST_TYPE_IS_ATSC,
.type_flags = DST_TYPE_HAS_MULTI_FE | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
.dst_feature = DST_TYPE_HAS_MAC | DST_TYPE_HAS_ANALOG,
.tuner_type = 0
},
{ }
};
static int dst_get_mac(struct dst_state *state)
{
u8 get_mac[] = { 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
get_mac[7] = dst_check_sum(get_mac, 7);
if (dst_command(state, get_mac, 8) < 0) {
dprintk(2, "Unsupported Command\n");
return -1;
}
memset(&state->mac_address, '\0', 8);
memcpy(&state->mac_address, &state->rxbuffer, 6);
pr_err("MAC Address=[%pM]\n", state->mac_address);
return 0;
}
static int dst_fw_ver(struct dst_state *state)
{
u8 get_ver[] = { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
get_ver[7] = dst_check_sum(get_ver, 7);
if (dst_command(state, get_ver, 8) < 0) {
dprintk(2, "Unsupported Command\n");
return -1;
}
memcpy(&state->fw_version, &state->rxbuffer, 8);
pr_err("Firmware Ver = %x.%x Build = %02x, on %x:%x, %x-%x-20%02x\n",
state->fw_version[0] >> 4, state->fw_version[0] & 0x0f,
state->fw_version[1],
state->fw_version[5], state->fw_version[6],
state->fw_version[4], state->fw_version[3], state->fw_version[2]);
return 0;
}
static int dst_card_type(struct dst_state *state)
{
int j;
struct tuner_types *p_tuner_list = NULL;
u8 get_type[] = { 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
get_type[7] = dst_check_sum(get_type, 7);
if (dst_command(state, get_type, 8) < 0) {
dprintk(2, "Unsupported Command\n");
return -1;
}
memset(&state->card_info, '\0', 8);
memcpy(&state->card_info, &state->rxbuffer, 7);
pr_err("Device Model=[%s]\n", &state->card_info[0]);
for (j = 0, p_tuner_list = tuner_list; j < ARRAY_SIZE(tuner_list); j++, p_tuner_list++) {
if (!strcmp(&state->card_info[0], p_tuner_list->board_name)) {
state->tuner_type = p_tuner_list->tuner_type;
pr_err("DST has [%s] tuner, tuner type=[%d]\n",
p_tuner_list->tuner_name, p_tuner_list->tuner_type);
}
}
return 0;
}
static int dst_get_vendor(struct dst_state *state)
{
u8 get_vendor[] = { 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
get_vendor[7] = dst_check_sum(get_vendor, 7);
if (dst_command(state, get_vendor, 8) < 0) {
dprintk(2, "Unsupported Command\n");
return -1;
}
memset(&state->vendor, '\0', 8);
memcpy(&state->vendor, &state->rxbuffer, 7);
pr_err("Vendor=[%s]\n", &state->vendor[0]);
return 0;
}
static void debug_dst_buffer(struct dst_state *state)
{
dprintk(3, "%s: [ %*ph ]\n", __func__, 8, state->rxbuffer);
}
static int dst_check_stv0299(struct dst_state *state)
{
u8 check_stv0299[] = { 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
check_stv0299[7] = dst_check_sum(check_stv0299, 7);
if (dst_command(state, check_stv0299, 8) < 0) {
pr_err("Cmd=[0x04] failed\n");
return -1;
}
debug_dst_buffer(state);
if (memcmp(&check_stv0299, &state->rxbuffer, 8)) {
pr_err("Found a STV0299 NIM\n");
state->tuner_type = TUNER_TYPE_STV0299;
return 0;
}
return -1;
}
static int dst_check_mb86a15(struct dst_state *state)
{
u8 check_mb86a15[] = { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
check_mb86a15[7] = dst_check_sum(check_mb86a15, 7);
if (dst_command(state, check_mb86a15, 8) < 0) {
pr_err("Cmd=[0x10], failed\n");
return -1;
}
debug_dst_buffer(state);
if (memcmp(&check_mb86a15, &state->rxbuffer, 8) < 0) {
pr_err("Found a MB86A15 NIM\n");
state->tuner_type = TUNER_TYPE_MB86A15;
return 0;
}
return -1;
}
static int dst_get_tuner_info(struct dst_state *state)
{
u8 get_tuner_1[] = { 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
u8 get_tuner_2[] = { 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
get_tuner_1[7] = dst_check_sum(get_tuner_1, 7);
get_tuner_2[7] = dst_check_sum(get_tuner_2, 7);
pr_err("DST TYpe = MULTI FE\n");
if (state->type_flags & DST_TYPE_HAS_MULTI_FE) {
if (dst_command(state, get_tuner_1, 8) < 0) {
dprintk(2, "Cmd=[0x13], Unsupported\n");
goto force;
}
} else {
if (dst_command(state, get_tuner_2, 8) < 0) {
dprintk(2, "Cmd=[0xb], Unsupported\n");
goto force;
}
}
memcpy(&state->board_info, &state->rxbuffer, 8);
if (state->type_flags & DST_TYPE_HAS_MULTI_FE) {
pr_err("DST type has TS=188\n");
}
if (state->board_info[0] == 0xbc) {
if (state->dst_type != DST_TYPE_IS_ATSC)
state->type_flags |= DST_TYPE_HAS_TS188;
else
state->type_flags |= DST_TYPE_HAS_NEWTUNE_2;
if (state->board_info[1] == 0x01) {
state->dst_hw_cap |= DST_TYPE_HAS_DBOARD;
pr_err("DST has Daughterboard\n");
}
}
return 0;
force:
if (!strncmp(state->fw_name, "DCT-CI", 6)) {
state->type_flags |= DST_TYPE_HAS_TS204;
pr_err("Forcing [%s] to TS188\n", state->fw_name);
}
return -1;
}
static int dst_get_device_id(struct dst_state *state)
{
u8 reply;
int i, j;
struct dst_types *p_dst_type = NULL;
struct tuner_types *p_tuner_list = NULL;
u8 use_dst_type = 0;
u32 use_type_flags = 0;
static u8 device_type[8] = {0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
state->tuner_type = 0;
device_type[7] = dst_check_sum(device_type, 7);
if (write_dst(state, device_type, FIXED_COMM))
return -1; /* Write failed */
if ((dst_pio_disable(state)) < 0)
return -1;
if (read_dst(state, &reply, GET_ACK))
return -1; /* Read failure */
if (reply != ACK) {
dprintk(2, "Write not Acknowledged! [Reply=0x%02x]\n", reply);
return -1; /* Unack'd write */
}
if (!dst_wait_dst_ready(state, DEVICE_INIT))
return -1; /* DST not ready yet */
if (read_dst(state, state->rxbuffer, FIXED_COMM))
return -1;
dst_pio_disable(state);
if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
dprintk(2, "Checksum failure!\n");
return -1; /* Checksum failure */
}
state->rxbuffer[7] = '\0';
for (i = 0, p_dst_type = dst_tlist; i < ARRAY_SIZE(dst_tlist); i++, p_dst_type++) {
if (!strncmp (&state->rxbuffer[p_dst_type->offset], p_dst_type->device_id, strlen (p_dst_type->device_id))) {
use_type_flags = p_dst_type->type_flags;
use_dst_type = p_dst_type->dst_type;
/* Card capabilities */
state->dst_hw_cap = p_dst_type->dst_feature;
pr_err("Recognise [%s]\n", p_dst_type->device_id);
strscpy(state->fw_name, p_dst_type->device_id,
sizeof(state->fw_name));
/* Multiple tuners */
if (p_dst_type->tuner_type & TUNER_TYPE_MULTI) {
switch (use_dst_type) {
case DST_TYPE_IS_SAT:
/* STV0299 check */
if (dst_check_stv0299(state) < 0) {
pr_err("Unsupported\n");
state->tuner_type = TUNER_TYPE_MB86A15;
}
break;
default:
break;
}
if (dst_check_mb86a15(state) < 0)
pr_err("Unsupported\n");
/* Single tuner */
} else {
state->tuner_type = p_dst_type->tuner_type;
}
for (j = 0, p_tuner_list = tuner_list; j < ARRAY_SIZE(tuner_list); j++, p_tuner_list++) {
if (!(strncmp(p_dst_type->device_id, p_tuner_list->fw_name, 7)) &&
p_tuner_list->tuner_type == state->tuner_type) {
pr_err("[%s] has a [%s]\n",
p_dst_type->device_id, p_tuner_list->tuner_name);
}
}
break;
}
}
if (i >= ARRAY_SIZE(dst_tlist)) {
pr_err("Unable to recognize %s or %s\n", &state->rxbuffer[0], &state->rxbuffer[1]);
pr_err("please email [email protected] with this type in");
use_dst_type = DST_TYPE_IS_SAT;
use_type_flags = DST_TYPE_HAS_SYMDIV;
}
dst_type_print(state, use_dst_type);
state->type_flags = use_type_flags;
state->dst_type = use_dst_type;
dst_type_flags_print(state);
return 0;
}
static int dst_probe(struct dst_state *state)
{
mutex_init(&state->dst_mutex);
if (dst_addons & DST_TYPE_HAS_CA) {
if ((rdc_8820_reset(state)) < 0) {
pr_err("RDC 8820 RESET Failed.\n");
return -1;
}
msleep(4000);
} else {
msleep(100);
}
if ((dst_comm_init(state)) < 0) {
pr_err("DST Initialization Failed.\n");
return -1;
}
msleep(100);
if (dst_get_device_id(state) < 0) {
pr_err("unknown device.\n");
return -1;
}
if (dst_get_mac(state) < 0) {
dprintk(2, "MAC: Unsupported command\n");
}
if ((state->type_flags & DST_TYPE_HAS_MULTI_FE) || (state->type_flags & DST_TYPE_HAS_FW_BUILD)) {
if (dst_get_tuner_info(state) < 0)
dprintk(2, "Tuner: Unsupported command\n");
}
if (state->type_flags & DST_TYPE_HAS_TS204) {
dst_packsize(state, 204);
}
if (state->type_flags & DST_TYPE_HAS_FW_BUILD) {
if (dst_fw_ver(state) < 0) {
dprintk(2, "FW: Unsupported command\n");
return 0;
}
if (dst_card_type(state) < 0) {
dprintk(2, "Card: Unsupported command\n");
return 0;
}
if (dst_get_vendor(state) < 0) {
dprintk(2, "Vendor: Unsupported command\n");
return 0;
}
}
return 0;
}
static int dst_command(struct dst_state *state, u8 *data, u8 len)
{
u8 reply;
mutex_lock(&state->dst_mutex);
if ((dst_comm_init(state)) < 0) {
dprintk(1, "DST Communication Initialization Failed.\n");
goto error;
}
if (write_dst(state, data, len)) {
dprintk(2, "Trying to recover..\n");
if ((dst_error_recovery(state)) < 0) {
pr_err("Recovery Failed.\n");
goto error;
}
goto error;
}
if ((dst_pio_disable(state)) < 0) {
pr_err("PIO Disable Failed.\n");
goto error;
}
if (state->type_flags & DST_TYPE_HAS_FW_1)
mdelay(3);
if (read_dst(state, &reply, GET_ACK)) {
dprintk(3, "Trying to recover..\n");
if ((dst_error_recovery(state)) < 0) {
dprintk(2, "Recovery Failed.\n");
goto error;
}
goto error;
}
if (reply != ACK) {
dprintk(2, "write not acknowledged 0x%02x\n", reply);
goto error;
}
if (len >= 2 && data[0] == 0 && (data[1] == 1 || data[1] == 3))
goto error;
if (state->type_flags & DST_TYPE_HAS_FW_1)
mdelay(3);
else
udelay(2000);
if (!dst_wait_dst_ready(state, NO_DELAY))
goto error;
if (read_dst(state, state->rxbuffer, FIXED_COMM)) {
dprintk(3, "Trying to recover..\n");
if ((dst_error_recovery(state)) < 0) {
dprintk(2, "Recovery failed.\n");
goto error;
}
goto error;
}
if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
dprintk(2, "checksum failure\n");
goto error;
}
mutex_unlock(&state->dst_mutex);
return 0;
error:
mutex_unlock(&state->dst_mutex);
return -EIO;
}
static int dst_get_signal(struct dst_state *state)
{
int retval;
u8 get_signal[] = { 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb };
//dprintk("%s: Getting Signal strength and other parameters\n", __func__);
if ((state->diseq_flags & ATTEMPT_TUNE) == 0) {
state->decode_lock = state->decode_strength = state->decode_snr = 0;
return 0;
}
if (0 == (state->diseq_flags & HAS_LOCK)) {
state->decode_lock = state->decode_strength = state->decode_snr = 0;
return 0;
}
if (time_after_eq(jiffies, state->cur_jiff + (HZ / 5))) {
retval = dst_command(state, get_signal, 8);
if (retval < 0)
return retval;
if (state->dst_type == DST_TYPE_IS_SAT) {
state->decode_lock = ((state->rxbuffer[6] & 0x10) == 0) ? 1 : 0;
state->decode_strength = state->rxbuffer[5] << 8;
state->decode_snr = state->rxbuffer[2] << 8 | state->rxbuffer[3];
} else if ((state->dst_type == DST_TYPE_IS_TERR) || (state->dst_type == DST_TYPE_IS_CABLE)) {
state->decode_lock = (state->rxbuffer[1]) ? 1 : 0;
state->decode_strength = state->rxbuffer[4] << 8;
state->decode_snr = state->rxbuffer[3] << 8;
} else if (state->dst_type == DST_TYPE_IS_ATSC) {
state->decode_lock = (state->rxbuffer[6] == 0x00) ? 1 : 0;
state->decode_strength = state->rxbuffer[4] << 8;
state->decode_snr = state->rxbuffer[2] << 8 | state->rxbuffer[3];
}
state->cur_jiff = jiffies;
}
return 0;
}
static int dst_tone_power_cmd(struct dst_state *state)
{
u8 packet[8] = { 0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00 };
if (state->dst_type != DST_TYPE_IS_SAT)
return -EOPNOTSUPP;
packet[4] = state->tx_tuna[4];
packet[2] = state->tx_tuna[2];
packet[3] = state->tx_tuna[3];
packet[7] = dst_check_sum (packet, 7);
return dst_command(state, packet, 8);
}
static int dst_get_tuna(struct dst_state *state)
{
int retval;
if ((state->diseq_flags & ATTEMPT_TUNE) == 0)
return 0;
state->diseq_flags &= ~(HAS_LOCK);
if (!dst_wait_dst_ready(state, NO_DELAY))
return -EIO;
if ((state->type_flags & DST_TYPE_HAS_VLF) &&
!(state->dst_type == DST_TYPE_IS_ATSC))
retval = read_dst(state, state->rx_tuna, 10);
else
retval = read_dst(state, &state->rx_tuna[2], FIXED_COMM);
if (retval < 0) {
dprintk(3, "read not successful\n");
return retval;
}
if ((state->type_flags & DST_TYPE_HAS_VLF) &&
!(state->dst_type == DST_TYPE_IS_ATSC)) {
if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[0], 9)) {
dprintk(2, "checksum failure ?\n");
return -EIO;
}
} else {
if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[2], 7)) {
dprintk(2, "checksum failure?\n");
return -EIO;
}
}
if (state->rx_tuna[2] == 0 && state->rx_tuna[3] == 0)
return 0;
if (state->dst_type == DST_TYPE_IS_SAT) {
state->decode_freq = ((state->rx_tuna[2] & 0x7f) << 8) + state->rx_tuna[3];
} else {
state->decode_freq = ((state->rx_tuna[2] & 0x7f) << 16) + (state->rx_tuna[3] << 8) + state->rx_tuna[4];
}
state->decode_freq = state->decode_freq * 1000;
state->decode_lock = 1;
state->diseq_flags |= HAS_LOCK;
return 1;
}
static int dst_set_voltage(struct dvb_frontend *fe,
enum fe_sec_voltage voltage);
static int dst_write_tuna(struct dvb_frontend *fe)
{
struct dst_state *state = fe->demodulator_priv;
int retval;
u8 reply;
dprintk(2, "type_flags 0x%x\n", state->type_flags);
state->decode_freq = 0;
state->decode_lock = state->decode_strength = state->decode_snr = 0;
if (state->dst_type == DST_TYPE_IS_SAT) {
if (!(state->diseq_flags & HAS_POWER))
dst_set_voltage(fe, SEC_VOLTAGE_13);
}
state->diseq_flags &= ~(HAS_LOCK | ATTEMPT_TUNE);
mutex_lock(&state->dst_mutex);
if ((dst_comm_init(state)) < 0) {
dprintk(3, "DST Communication initialization failed.\n");
goto error;
}
// if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
if ((state->type_flags & DST_TYPE_HAS_VLF) &&
(!(state->dst_type == DST_TYPE_IS_ATSC))) {
state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[0], 9);
retval = write_dst(state, &state->tx_tuna[0], 10);
} else {
state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[2], 7);
retval = write_dst(state, &state->tx_tuna[2], FIXED_COMM);
}
if (retval < 0) {
dst_pio_disable(state);
dprintk(3, "write not successful\n");
goto werr;
}
if ((dst_pio_disable(state)) < 0) {
dprintk(3, "DST PIO disable failed !\n");
goto error;
}
if ((read_dst(state, &reply, GET_ACK) < 0)) {
dprintk(3, "read verify not successful.\n");
goto error;
}
if (reply != ACK) {
dprintk(3, "write not acknowledged 0x%02x\n", reply);
goto error;
}
state->diseq_flags |= ATTEMPT_TUNE;
retval = dst_get_tuna(state);
werr:
mutex_unlock(&state->dst_mutex);
return retval;
error:
mutex_unlock(&state->dst_mutex);
return -EIO;
}
/*
* line22k0 0x00, 0x09, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00
* line22k1 0x00, 0x09, 0x01, 0xff, 0x01, 0x00, 0x00, 0x00
* line22k2 0x00, 0x09, 0x02, 0xff, 0x01, 0x00, 0x00, 0x00
* tone 0x00, 0x09, 0xff, 0x00, 0x01, 0x00, 0x00, 0x00
* data 0x00, 0x09, 0xff, 0x01, 0x01, 0x00, 0x00, 0x00
* power_off 0x00, 0x09, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00
* power_on 0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00
* Diseqc 1 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec
* Diseqc 2 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf4, 0xe8
* Diseqc 3 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf8, 0xe4
* Diseqc 4 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xfc, 0xe0
*/
static int dst_set_diseqc(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
{
struct dst_state *state = fe->demodulator_priv;
u8 packet[8] = { 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec };
if (state->dst_type != DST_TYPE_IS_SAT)
return -EOPNOTSUPP;
if (cmd->msg_len > 0 && cmd->msg_len < 5)
memcpy(&packet[3], cmd->msg, cmd->msg_len);
else if (cmd->msg_len == 5 && state->dst_hw_cap & DST_TYPE_HAS_DISEQC5)
memcpy(&packet[2], cmd->msg, cmd->msg_len);
else
return -EINVAL;
packet[7] = dst_check_sum(&packet[0], 7);
return dst_command(state, packet, 8);
}
static int dst_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage voltage)
{
int need_cmd, retval = 0;
struct dst_state *state = fe->demodulator_priv;
state->voltage = voltage;
if (state->dst_type != DST_TYPE_IS_SAT)
return -EOPNOTSUPP;
need_cmd = 0;
switch (voltage) {
case SEC_VOLTAGE_13:
case SEC_VOLTAGE_18:
if ((state->diseq_flags & HAS_POWER) == 0)
need_cmd = 1;
state->diseq_flags |= HAS_POWER;
state->tx_tuna[4] = 0x01;
break;
case SEC_VOLTAGE_OFF:
need_cmd = 1;
state->diseq_flags &= ~(HAS_POWER | HAS_LOCK | ATTEMPT_TUNE);
state->tx_tuna[4] = 0x00;
break;
default:
return -EINVAL;
}
if (need_cmd)
retval = dst_tone_power_cmd(state);
return retval;
}
static int dst_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
{
struct dst_state *state = fe->demodulator_priv;
state->tone = tone;
if (state->dst_type != DST_TYPE_IS_SAT)
return -EOPNOTSUPP;
switch (tone) {
case SEC_TONE_OFF:
if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
state->tx_tuna[2] = 0x00;
else
state->tx_tuna[2] = 0xff;
break;
case SEC_TONE_ON:
state->tx_tuna[2] = 0x02;
break;
default:
return -EINVAL;
}
return dst_tone_power_cmd(state);
}
static int dst_send_burst(struct dvb_frontend *fe, enum fe_sec_mini_cmd minicmd)
{
struct dst_state *state = fe->demodulator_priv;
if (state->dst_type != DST_TYPE_IS_SAT)
return -EOPNOTSUPP;
state->minicmd = minicmd;
switch (minicmd) {
case SEC_MINI_A:
state->tx_tuna[3] = 0x02;
break;
case SEC_MINI_B:
state->tx_tuna[3] = 0xff;
break;
}
return dst_tone_power_cmd(state);
}
static int bt8xx_dst_init(struct dvb_frontend *fe)
{
struct dst_state *state = fe->demodulator_priv;
static u8 sat_tuna_188[] = { 0x09, 0x00, 0x03, 0xb6, 0x01, 0x00, 0x73, 0x21, 0x00, 0x00 };
static u8 sat_tuna_204[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x55, 0xbd, 0x50, 0x00, 0x00 };
static u8 ter_tuna_188[] = { 0x09, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
static u8 ter_tuna_204[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
static u8 cab_tuna_188[] = { 0x09, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
static u8 cab_tuna_204[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
static u8 atsc_tuner[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
state->inversion = INVERSION_OFF;
state->voltage = SEC_VOLTAGE_13;
state->tone = SEC_TONE_OFF;
state->diseq_flags = 0;
state->k22 = 0x02;
state->bandwidth = 7000000;
state->cur_jiff = jiffies;
if (state->dst_type == DST_TYPE_IS_SAT)
memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_VLF) ? sat_tuna_188 : sat_tuna_204), sizeof (sat_tuna_204));
else if (state->dst_type == DST_TYPE_IS_TERR)
memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_VLF) ? ter_tuna_188 : ter_tuna_204), sizeof (ter_tuna_204));
else if (state->dst_type == DST_TYPE_IS_CABLE)
memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_VLF) ? cab_tuna_188 : cab_tuna_204), sizeof (cab_tuna_204));
else if (state->dst_type == DST_TYPE_IS_ATSC)
memcpy(state->tx_tuna, atsc_tuner, sizeof (atsc_tuner));
return 0;
}
static int dst_read_status(struct dvb_frontend *fe, enum fe_status *status)
{
struct dst_state *state = fe->demodulator_priv;
*status = 0;
if (state->diseq_flags & HAS_LOCK) {
// dst_get_signal(state); // don't require(?) to ask MCU
if (state->decode_lock)
*status |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_SYNC | FE_HAS_VITERBI;
}
return 0;
}
static int dst_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{
struct dst_state *state = fe->demodulator_priv;
int retval = dst_get_signal(state);
*strength = state->decode_strength;
return retval;
}
static int dst_read_snr(struct dvb_frontend *fe, u16 *snr)
{
struct dst_state *state = fe->demodulator_priv;
int retval = dst_get_signal(state);
*snr = state->decode_snr;
return retval;
}
static int dst_set_frontend(struct dvb_frontend *fe)
{
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
int retval = -EINVAL;
struct dst_state *state = fe->demodulator_priv;
if (p != NULL) {
retval = dst_set_freq(state, p->frequency);
if(retval != 0)
return retval;
dprintk(3, "Set Frequency=[%d]\n", p->frequency);
if (state->dst_type == DST_TYPE_IS_SAT) {
if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
dst_set_inversion(state, p->inversion);
dst_set_fec(state, p->fec_inner);
dst_set_symbolrate(state, p->symbol_rate);
dst_set_polarization(state);
dprintk(3, "Set Symbolrate=[%d]\n", p->symbol_rate);
} else if (state->dst_type == DST_TYPE_IS_TERR)
dst_set_bandwidth(state, p->bandwidth_hz);
else if (state->dst_type == DST_TYPE_IS_CABLE) {
dst_set_fec(state, p->fec_inner);
dst_set_symbolrate(state, p->symbol_rate);
dst_set_modulation(state, p->modulation);
}
retval = dst_write_tuna(fe);
}
return retval;
}
static int dst_tune_frontend(struct dvb_frontend* fe,
bool re_tune,
unsigned int mode_flags,
unsigned int *delay,
enum fe_status *status)
{
struct dst_state *state = fe->demodulator_priv;
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
if (re_tune) {
dst_set_freq(state, p->frequency);
dprintk(3, "Set Frequency=[%d]\n", p->frequency);
if (state->dst_type == DST_TYPE_IS_SAT) {
if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
dst_set_inversion(state, p->inversion);
dst_set_fec(state, p->fec_inner);
dst_set_symbolrate(state, p->symbol_rate);
dst_set_polarization(state);
dprintk(3, "Set Symbolrate=[%d]\n", p->symbol_rate);
} else if (state->dst_type == DST_TYPE_IS_TERR)
dst_set_bandwidth(state, p->bandwidth_hz);
else if (state->dst_type == DST_TYPE_IS_CABLE) {
dst_set_fec(state, p->fec_inner);
dst_set_symbolrate(state, p->symbol_rate);
dst_set_modulation(state, p->modulation);
}
dst_write_tuna(fe);
}
if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
dst_read_status(fe, status);
*delay = HZ/10;
return 0;
}
static enum dvbfe_algo dst_get_tuning_algo(struct dvb_frontend *fe)
{
return dst_algo ? DVBFE_ALGO_HW : DVBFE_ALGO_SW;
}
static int dst_get_frontend(struct dvb_frontend *fe,
struct dtv_frontend_properties *p)
{
struct dst_state *state = fe->demodulator_priv;
p->frequency = state->decode_freq;
if (state->dst_type == DST_TYPE_IS_SAT) {
if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
p->inversion = state->inversion;
p->symbol_rate = state->symbol_rate;
p->fec_inner = dst_get_fec(state);
} else if (state->dst_type == DST_TYPE_IS_TERR) {
p->bandwidth_hz = state->bandwidth;
} else if (state->dst_type == DST_TYPE_IS_CABLE) {
p->symbol_rate = state->symbol_rate;
p->fec_inner = dst_get_fec(state);
p->modulation = dst_get_modulation(state);
}
return 0;
}
static void bt8xx_dst_release(struct dvb_frontend *fe)
{
struct dst_state *state = fe->demodulator_priv;
if (state->dst_ca) {
dvb_unregister_device(state->dst_ca);
#ifdef CONFIG_MEDIA_ATTACH
symbol_put(dst_ca_attach);
#endif
}
kfree(state);
}
static const struct dvb_frontend_ops dst_dvbt_ops;
static const struct dvb_frontend_ops dst_dvbs_ops;
static const struct dvb_frontend_ops dst_dvbc_ops;
static const struct dvb_frontend_ops dst_atsc_ops;
struct dst_state *dst_attach(struct dst_state *state, struct dvb_adapter *dvb_adapter)
{
/* check if the ASIC is there */
if (dst_probe(state) < 0) {
kfree(state);
return NULL;
}
/* determine settings based on type */
/* create dvb_frontend */
switch (state->dst_type) {
case DST_TYPE_IS_TERR:
memcpy(&state->frontend.ops, &dst_dvbt_ops, sizeof(struct dvb_frontend_ops));
break;
case DST_TYPE_IS_CABLE:
memcpy(&state->frontend.ops, &dst_dvbc_ops, sizeof(struct dvb_frontend_ops));
break;
case DST_TYPE_IS_SAT:
memcpy(&state->frontend.ops, &dst_dvbs_ops, sizeof(struct dvb_frontend_ops));
break;
case DST_TYPE_IS_ATSC:
memcpy(&state->frontend.ops, &dst_atsc_ops, sizeof(struct dvb_frontend_ops));
break;
default:
pr_err("unknown DST type. please report to the LinuxTV.org DVB mailinglist.\n");
kfree(state);
return NULL;
}
state->frontend.demodulator_priv = state;
return state; /* Manu (DST is a card not a frontend) */
}
EXPORT_SYMBOL_GPL(dst_attach);
static const struct dvb_frontend_ops dst_dvbt_ops = {
.delsys = { SYS_DVBT },
.info = {
.name = "DST DVB-T",
.frequency_min_hz = 137 * MHz,
.frequency_max_hz = 858 * MHz,
.frequency_stepsize_hz = 166667,
.caps = FE_CAN_FEC_AUTO |
FE_CAN_QAM_AUTO |
FE_CAN_QAM_16 |
FE_CAN_QAM_32 |
FE_CAN_QAM_64 |
FE_CAN_QAM_128 |
FE_CAN_QAM_256 |
FE_CAN_TRANSMISSION_MODE_AUTO |
FE_CAN_GUARD_INTERVAL_AUTO
},
.release = bt8xx_dst_release,
.init = bt8xx_dst_init,
.tune = dst_tune_frontend,
.set_frontend = dst_set_frontend,
.get_frontend = dst_get_frontend,
.get_frontend_algo = dst_get_tuning_algo,
.read_status = dst_read_status,
.read_signal_strength = dst_read_signal_strength,
.read_snr = dst_read_snr,
};
static const struct dvb_frontend_ops dst_dvbs_ops = {
.delsys = { SYS_DVBS },
.info = {
.name = "DST DVB-S",
.frequency_min_hz = 950 * MHz,
.frequency_max_hz = 2150 * MHz,
.frequency_stepsize_hz = 1 * MHz,
.frequency_tolerance_hz = 29500 * kHz,
.symbol_rate_min = 1000000,
.symbol_rate_max = 45000000,
/* . symbol_rate_tolerance = ???,*/
.caps = FE_CAN_FEC_AUTO | FE_CAN_QPSK
},
.release = bt8xx_dst_release,
.init = bt8xx_dst_init,
.tune = dst_tune_frontend,
.set_frontend = dst_set_frontend,
.get_frontend = dst_get_frontend,
.get_frontend_algo = dst_get_tuning_algo,
.read_status = dst_read_status,
.read_signal_strength = dst_read_signal_strength,
.read_snr = dst_read_snr,
.diseqc_send_burst = dst_send_burst,
.diseqc_send_master_cmd = dst_set_diseqc,
.set_voltage = dst_set_voltage,
.set_tone = dst_set_tone,
};
static const struct dvb_frontend_ops dst_dvbc_ops = {
.delsys = { SYS_DVBC_ANNEX_A },
.info = {
.name = "DST DVB-C",
.frequency_min_hz = 51 * MHz,
.frequency_max_hz = 858 * MHz,
.frequency_stepsize_hz = 62500,
.symbol_rate_min = 1000000,
.symbol_rate_max = 45000000,
.caps = FE_CAN_FEC_AUTO |
FE_CAN_QAM_AUTO |
FE_CAN_QAM_16 |
FE_CAN_QAM_32 |
FE_CAN_QAM_64 |
FE_CAN_QAM_128 |
FE_CAN_QAM_256
},
.release = bt8xx_dst_release,
.init = bt8xx_dst_init,
.tune = dst_tune_frontend,
.set_frontend = dst_set_frontend,
.get_frontend = dst_get_frontend,
.get_frontend_algo = dst_get_tuning_algo,
.read_status = dst_read_status,
.read_signal_strength = dst_read_signal_strength,
.read_snr = dst_read_snr,
};
static const struct dvb_frontend_ops dst_atsc_ops = {
.delsys = { SYS_ATSC },
.info = {
.name = "DST ATSC",
.frequency_min_hz = 510 * MHz,
.frequency_max_hz = 858 * MHz,
.frequency_stepsize_hz = 62500,
.symbol_rate_min = 1000000,
.symbol_rate_max = 45000000,
.caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO | FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
},
.release = bt8xx_dst_release,
.init = bt8xx_dst_init,
.tune = dst_tune_frontend,
.set_frontend = dst_set_frontend,
.get_frontend = dst_get_frontend,
.get_frontend_algo = dst_get_tuning_algo,
.read_status = dst_read_status,
.read_signal_strength = dst_read_signal_strength,
.read_snr = dst_read_snr,
};
MODULE_DESCRIPTION("DST DVB-S/T/C/ATSC Combo Frontend driver");
MODULE_AUTHOR("Jamie Honan, Manu Abraham");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/bt8xx/dst.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv-gpio.c -- gpio sub drivers
sysfs-based sub driver interface for bttv
mainly intended for gpio access
Copyright (C) 1996,97,98 Ralph Metzler ([email protected])
& Marcus Metzler ([email protected])
(c) 1999-2003 Gerd Knorr <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <asm/io.h>
#include "bttvp.h"
/* ----------------------------------------------------------------------- */
/* internal: the bttv "bus" */
static int bttv_sub_bus_match(struct device *dev, struct device_driver *drv)
{
struct bttv_sub_driver *sub = to_bttv_sub_drv(drv);
int len = strlen(sub->wanted);
if (0 == strncmp(dev_name(dev), sub->wanted, len))
return 1;
return 0;
}
static int bttv_sub_probe(struct device *dev)
{
struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
return sub->probe ? sub->probe(sdev) : -ENODEV;
}
static void bttv_sub_remove(struct device *dev)
{
struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
if (sub->remove)
sub->remove(sdev);
}
struct bus_type bttv_sub_bus_type = {
.name = "bttv-sub",
.match = &bttv_sub_bus_match,
.probe = bttv_sub_probe,
.remove = bttv_sub_remove,
};
static void release_sub_device(struct device *dev)
{
struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
kfree(sub);
}
int bttv_sub_add_device(struct bttv_core *core, char *name)
{
struct bttv_sub_device *sub;
int err;
sub = kzalloc(sizeof(*sub),GFP_KERNEL);
if (NULL == sub)
return -ENOMEM;
sub->core = core;
sub->dev.parent = &core->pci->dev;
sub->dev.bus = &bttv_sub_bus_type;
sub->dev.release = release_sub_device;
dev_set_name(&sub->dev, "%s%d", name, core->nr);
err = device_register(&sub->dev);
if (0 != err) {
put_device(&sub->dev);
return err;
}
pr_info("%d: add subdevice \"%s\"\n", core->nr, dev_name(&sub->dev));
list_add_tail(&sub->list,&core->subs);
return 0;
}
int bttv_sub_del_devices(struct bttv_core *core)
{
struct bttv_sub_device *sub, *save;
list_for_each_entry_safe(sub, save, &core->subs, list) {
list_del(&sub->list);
device_unregister(&sub->dev);
}
return 0;
}
/* ----------------------------------------------------------------------- */
/* external: sub-driver register/unregister */
int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
{
sub->drv.bus = &bttv_sub_bus_type;
snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
return driver_register(&sub->drv);
}
EXPORT_SYMBOL(bttv_sub_register);
int bttv_sub_unregister(struct bttv_sub_driver *sub)
{
driver_unregister(&sub->drv);
return 0;
}
EXPORT_SYMBOL(bttv_sub_unregister);
/* ----------------------------------------------------------------------- */
/* external: gpio access functions */
void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
{
struct bttv *btv = container_of(core, struct bttv, c);
unsigned long flags;
u32 data;
spin_lock_irqsave(&btv->gpio_lock,flags);
data = btread(BT848_GPIO_OUT_EN);
data = data & ~mask;
data = data | (mask & outbits);
btwrite(data,BT848_GPIO_OUT_EN);
spin_unlock_irqrestore(&btv->gpio_lock,flags);
}
u32 bttv_gpio_read(struct bttv_core *core)
{
struct bttv *btv = container_of(core, struct bttv, c);
u32 value;
value = btread(BT848_GPIO_DATA);
return value;
}
void bttv_gpio_write(struct bttv_core *core, u32 value)
{
struct bttv *btv = container_of(core, struct bttv, c);
btwrite(value,BT848_GPIO_DATA);
}
void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
{
struct bttv *btv = container_of(core, struct bttv, c);
unsigned long flags;
u32 data;
spin_lock_irqsave(&btv->gpio_lock,flags);
data = btread(BT848_GPIO_DATA);
data = data & ~mask;
data = data | (mask & bits);
btwrite(data,BT848_GPIO_DATA);
spin_unlock_irqrestore(&btv->gpio_lock,flags);
}
| linux-master | drivers/media/pci/bt8xx/bttv-gpio.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* Copyright (c) 2003 Gerd Knorr
* Copyright (c) 2003 Pavel Machek
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/slab.h>
#include "bttv.h"
#include "bttvp.h"
static int ir_debug;
module_param(ir_debug, int, 0644);
static int ir_rc5_remote_gap = 885;
module_param(ir_rc5_remote_gap, int, 0644);
#undef dprintk
#define dprintk(fmt, ...) \
do { \
if (ir_debug >= 1) \
pr_info(fmt, ##__VA_ARGS__); \
} while (0)
#define DEVNAME "bttv-input"
#define MODULE_NAME "bttv"
/* ---------------------------------------------------------------------- */
static void ir_handle_key(struct bttv *btv)
{
struct bttv_ir *ir = btv->remote;
u32 gpio,data;
/* read gpio value */
gpio = bttv_gpio_read(&btv->c);
if (ir->polling) {
if (ir->last_gpio == gpio)
return;
ir->last_gpio = gpio;
}
/* extract data */
data = ir_extract_bits(gpio, ir->mask_keycode);
dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
gpio, data,
ir->polling ? "poll" : "irq",
(gpio & ir->mask_keydown) ? " down" : "",
(gpio & ir->mask_keyup) ? " up" : "");
if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
(ir->mask_keyup && !(gpio & ir->mask_keyup))) {
rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
} else {
/* HACK: Probably, ir->mask_keydown is missing
for this board */
if (btv->c.type == BTTV_BOARD_WINFAST2000)
rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0);
rc_keyup(ir->dev);
}
}
static void ir_enltv_handle_key(struct bttv *btv)
{
struct bttv_ir *ir = btv->remote;
u32 gpio, data, keyup;
/* read gpio value */
gpio = bttv_gpio_read(&btv->c);
/* extract data */
data = ir_extract_bits(gpio, ir->mask_keycode);
/* Check if it is keyup */
keyup = (gpio & ir->mask_keyup) ? 1UL << 31 : 0;
if ((ir->last_gpio & 0x7f) != data) {
dprintk("gpio=0x%x code=%d | %s\n",
gpio, data,
(gpio & ir->mask_keyup) ? " up" : "up/down");
rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
if (keyup)
rc_keyup(ir->dev);
} else {
if ((ir->last_gpio & 1UL << 31) == keyup)
return;
dprintk("(cnt) gpio=0x%x code=%d | %s\n",
gpio, data,
(gpio & ir->mask_keyup) ? " up" : "down");
if (keyup)
rc_keyup(ir->dev);
else
rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
0);
}
ir->last_gpio = data | keyup;
}
static int bttv_rc5_irq(struct bttv *btv);
void bttv_input_irq(struct bttv *btv)
{
struct bttv_ir *ir = btv->remote;
if (ir->rc5_gpio)
bttv_rc5_irq(btv);
else if (!ir->polling)
ir_handle_key(btv);
}
static void bttv_input_timer(struct timer_list *t)
{
struct bttv_ir *ir = from_timer(ir, t, timer);
struct bttv *btv = ir->btv;
if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
ir_enltv_handle_key(btv);
else
ir_handle_key(btv);
mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
}
/*
* FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
* on the rc-core way. As we need to be sure that both IRQ transitions are
* properly triggered, Better to touch it only with this hardware for
* testing.
*/
#define RC5_START(x) (((x) >> 12) & 0x03)
#define RC5_TOGGLE(x) (((x) >> 11) & 0x01)
#define RC5_ADDR(x) (((x) >> 6) & 0x1f)
#define RC5_INSTR(x) (((x) >> 0) & 0x3f)
/* decode raw bit pattern to RC5 code */
static u32 bttv_rc5_decode(unsigned int code)
{
unsigned int org_code = code;
unsigned int pair;
unsigned int rc5 = 0;
int i;
for (i = 0; i < 14; ++i) {
pair = code & 0x3;
code >>= 2;
rc5 <<= 1;
switch (pair) {
case 0:
case 2:
break;
case 1:
rc5 |= 1;
break;
case 3:
dprintk("rc5_decode(%x) bad code\n",
org_code);
return 0;
}
}
dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, instr=%x\n",
rc5, org_code, RC5_START(rc5),
RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
return rc5;
}
static void bttv_rc5_timer_end(struct timer_list *t)
{
struct bttv_ir *ir = from_timer(ir, t, timer);
ktime_t tv;
u32 gap, rc5, scancode;
u8 toggle, command, system;
/* get time */
tv = ktime_get();
gap = ktime_to_us(ktime_sub(tv, ir->base_time));
/* avoid overflow with gap >1s */
if (gap > USEC_PER_SEC) {
gap = 200000;
}
/* signal we're ready to start a new code */
ir->active = false;
/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
if (gap < 28000) {
dprintk("spurious timer_end\n");
return;
}
if (ir->last_bit < 20) {
/* ignore spurious codes (caused by light/other remotes) */
dprintk("short code: %x\n", ir->code);
return;
}
ir->code = (ir->code << ir->shift_by) | 1;
rc5 = bttv_rc5_decode(ir->code);
toggle = RC5_TOGGLE(rc5);
system = RC5_ADDR(rc5);
command = RC5_INSTR(rc5);
switch (RC5_START(rc5)) {
case 0x3:
break;
case 0x2:
command += 0x40;
break;
default:
return;
}
scancode = RC_SCANCODE_RC5(system, command);
rc_keydown(ir->dev, RC_PROTO_RC5, scancode, toggle);
dprintk("scancode %x, toggle %x\n", scancode, toggle);
}
static int bttv_rc5_irq(struct bttv *btv)
{
struct bttv_ir *ir = btv->remote;
ktime_t tv;
u32 gpio;
u32 gap;
unsigned long current_jiffies;
/* read gpio port */
gpio = bttv_gpio_read(&btv->c);
/* get time of bit */
current_jiffies = jiffies;
tv = ktime_get();
gap = ktime_to_us(ktime_sub(tv, ir->base_time));
/* avoid overflow with gap >1s */
if (gap > USEC_PER_SEC) {
gap = 200000;
}
dprintk("RC5 IRQ: gap %d us for %s\n",
gap, (gpio & 0x20) ? "mark" : "space");
/* remote IRQ? */
if (!(gpio & 0x20))
return 0;
/* active code => add bit */
if (ir->active) {
/* only if in the code (otherwise spurious IRQ or timer
late) */
if (ir->last_bit < 28) {
ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
ir_rc5_remote_gap;
ir->code |= 1 << ir->last_bit;
}
/* starting new code */
} else {
ir->active = true;
ir->code = 0;
ir->base_time = tv;
ir->last_bit = 0;
mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
}
/* toggle GPIO pin 4 to reset the irq */
bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
bttv_gpio_write(&btv->c, gpio | (1 << 4));
return 1;
}
/* ---------------------------------------------------------------------- */
static void bttv_ir_start(struct bttv_ir *ir)
{
if (ir->polling) {
timer_setup(&ir->timer, bttv_input_timer, 0);
ir->timer.expires = jiffies + msecs_to_jiffies(1000);
add_timer(&ir->timer);
} else if (ir->rc5_gpio) {
/* set timer_end for code completion */
timer_setup(&ir->timer, bttv_rc5_timer_end, 0);
ir->shift_by = 1;
ir->rc5_remote_gap = ir_rc5_remote_gap;
}
}
static void bttv_ir_stop(struct bttv *btv)
{
if (btv->remote->polling)
del_timer_sync(&btv->remote->timer);
if (btv->remote->rc5_gpio) {
u32 gpio;
del_timer_sync(&btv->remote->timer);
gpio = bttv_gpio_read(&btv->c);
bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
}
}
/*
* Get_key functions used by I2C remotes
*/
static int get_key_pv951(struct IR_i2c *ir, enum rc_proto *protocol,
u32 *scancode, u8 *toggle)
{
int rc;
unsigned char b;
/* poll IR chip */
rc = i2c_master_recv(ir->c, &b, 1);
if (rc != 1) {
dprintk("read error\n");
if (rc < 0)
return rc;
return -EIO;
}
/* ignore 0xaa */
if (b==0xaa)
return 0;
dprintk("key %02x\n", b);
/*
* NOTE:
* lirc_i2c maps the pv951 code as:
* addr = 0x61D6
* cmd = bit_reverse (b)
* So, it seems that this device uses NEC extended
* I decided to not fix the table, due to two reasons:
* 1) Without the actual device, this is only a guess;
* 2) As the addr is not reported via I2C, nor can be changed,
* the device is bound to the vendor-provided RC.
*/
*protocol = RC_PROTO_UNKNOWN;
*scancode = b;
*toggle = 0;
return 1;
}
/* Instantiate the I2C IR receiver device, if present */
void init_bttv_i2c_ir(struct bttv *btv)
{
static const unsigned short addr_list[] = {
0x1a, 0x18, 0x64, 0x30, 0x71,
I2C_CLIENT_END
};
struct i2c_board_info info;
struct i2c_client *i2c_dev;
if (0 != btv->i2c_rc)
return;
memset(&info, 0, sizeof(struct i2c_board_info));
memset(&btv->init_data, 0, sizeof(btv->init_data));
strscpy(info.type, "ir_video", I2C_NAME_SIZE);
switch (btv->c.type) {
case BTTV_BOARD_PV951:
btv->init_data.name = "PV951";
btv->init_data.get_key = get_key_pv951;
btv->init_data.ir_codes = RC_MAP_PV951;
info.addr = 0x4b;
break;
}
if (btv->init_data.name) {
info.platform_data = &btv->init_data;
i2c_dev = i2c_new_client_device(&btv->c.i2c_adap, &info);
} else {
/*
* The external IR receiver is at i2c address 0x34 (0x35 for
* reads). Future Hauppauge cards will have an internal
* receiver at 0x30 (0x31 for reads). In theory, both can be
* fitted, and Hauppauge suggest an external overrides an
* internal.
* That's why we probe 0x1a (~0x34) first. CB
*/
i2c_dev = i2c_new_scanned_device(&btv->c.i2c_adap, &info, addr_list, NULL);
}
if (IS_ERR(i2c_dev))
return;
#if defined(CONFIG_MODULES) && defined(MODULE)
request_module("ir-kbd-i2c");
#endif
}
int bttv_input_init(struct bttv *btv)
{
struct bttv_ir *ir;
char *ir_codes = NULL;
struct rc_dev *rc;
int err = -ENOMEM;
if (!btv->has_remote)
return -ENODEV;
ir = kzalloc(sizeof(*ir),GFP_KERNEL);
rc = rc_allocate_device(RC_DRIVER_SCANCODE);
if (!ir || !rc)
goto err_out_free;
/* detect & configure */
switch (btv->c.type) {
case BTTV_BOARD_AVERMEDIA:
case BTTV_BOARD_AVPHONE98:
case BTTV_BOARD_AVERMEDIA98:
ir_codes = RC_MAP_AVERMEDIA;
ir->mask_keycode = 0xf88000;
ir->mask_keydown = 0x010000;
ir->polling = 50; // ms
break;
case BTTV_BOARD_AVDVBT_761:
case BTTV_BOARD_AVDVBT_771:
ir_codes = RC_MAP_AVERMEDIA_DVBT;
ir->mask_keycode = 0x0f00c0;
ir->mask_keydown = 0x000020;
ir->polling = 50; // ms
break;
case BTTV_BOARD_PXELVWPLTVPAK:
ir_codes = RC_MAP_PIXELVIEW;
ir->mask_keycode = 0x003e00;
ir->mask_keyup = 0x010000;
ir->polling = 50; // ms
break;
case BTTV_BOARD_PV_M4900:
case BTTV_BOARD_PV_BT878P_9B:
case BTTV_BOARD_PV_BT878P_PLUS:
ir_codes = RC_MAP_PIXELVIEW;
ir->mask_keycode = 0x001f00;
ir->mask_keyup = 0x008000;
ir->polling = 50; // ms
break;
case BTTV_BOARD_WINFAST2000:
ir_codes = RC_MAP_WINFAST;
ir->mask_keycode = 0x1f8;
break;
case BTTV_BOARD_MAGICTVIEW061:
case BTTV_BOARD_MAGICTVIEW063:
ir_codes = RC_MAP_WINFAST;
ir->mask_keycode = 0x0008e000;
ir->mask_keydown = 0x00200000;
break;
case BTTV_BOARD_APAC_VIEWCOMP:
ir_codes = RC_MAP_APAC_VIEWCOMP;
ir->mask_keycode = 0x001f00;
ir->mask_keyup = 0x008000;
ir->polling = 50; // ms
break;
case BTTV_BOARD_ASKEY_CPH03X:
case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
case BTTV_BOARD_CONTVFMI:
case BTTV_BOARD_KWORLD_VSTREAM_XPERT:
ir_codes = RC_MAP_PIXELVIEW;
ir->mask_keycode = 0x001F00;
ir->mask_keyup = 0x006000;
ir->polling = 50; // ms
break;
case BTTV_BOARD_NEBULA_DIGITV:
ir_codes = RC_MAP_NEBULA;
ir->rc5_gpio = true;
break;
case BTTV_BOARD_MACHTV_MAGICTV:
ir_codes = RC_MAP_APAC_VIEWCOMP;
ir->mask_keycode = 0x001F00;
ir->mask_keyup = 0x004000;
ir->polling = 50; /* ms */
break;
case BTTV_BOARD_KOZUMI_KTV_01C:
ir_codes = RC_MAP_PCTV_SEDNA;
ir->mask_keycode = 0x001f00;
ir->mask_keyup = 0x006000;
ir->polling = 50; /* ms */
break;
case BTTV_BOARD_ENLTV_FM_2:
ir_codes = RC_MAP_ENCORE_ENLTV2;
ir->mask_keycode = 0x00fd00;
ir->mask_keyup = 0x000080;
ir->polling = 1; /* ms */
ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
ir->mask_keycode);
break;
}
if (!ir_codes) {
dprintk("Ooops: IR config error [card=%d]\n", btv->c.type);
err = -ENODEV;
goto err_out_free;
}
if (ir->rc5_gpio) {
u32 gpio;
/* enable remote irq */
bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
gpio = bttv_gpio_read(&btv->c);
bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
bttv_gpio_write(&btv->c, gpio | (1 << 4));
} else {
/* init hardware-specific stuff */
bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
}
/* init input device */
ir->dev = rc;
ir->btv = btv;
snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
btv->c.type);
snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
pci_name(btv->c.pci));
rc->device_name = ir->name;
rc->input_phys = ir->phys;
rc->input_id.bustype = BUS_PCI;
rc->input_id.version = 1;
if (btv->c.pci->subsystem_vendor) {
rc->input_id.vendor = btv->c.pci->subsystem_vendor;
rc->input_id.product = btv->c.pci->subsystem_device;
} else {
rc->input_id.vendor = btv->c.pci->vendor;
rc->input_id.product = btv->c.pci->device;
}
rc->dev.parent = &btv->c.pci->dev;
rc->map_name = ir_codes;
rc->driver_name = MODULE_NAME;
btv->remote = ir;
bttv_ir_start(ir);
/* all done */
err = rc_register_device(rc);
if (err)
goto err_out_stop;
return 0;
err_out_stop:
bttv_ir_stop(btv);
btv->remote = NULL;
err_out_free:
rc_free_device(rc);
kfree(ir);
return err;
}
void bttv_input_fini(struct bttv *btv)
{
if (btv->remote == NULL)
return;
bttv_ir_stop(btv);
rc_unregister_device(btv->remote->dev);
kfree(btv->remote);
btv->remote = NULL;
}
| linux-master | drivers/media/pci/bt8xx/bttv-input.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv - Bt848 frame grabber driver
Copyright (C) 1996,97,98 Ralph Metzler <[email protected]>
& Marcus Metzler <[email protected]>
(c) 1999-2002 Gerd Knorr <[email protected]>
some v4l2 code lines are taken from Justin's bttv2 driver which is
(c) 2000 Justin Schoeman <[email protected]>
V4L1 removal from:
(c) 2005-2006 Nickolay V. Shmyrev <[email protected]>
Fixes to be fully V4L2 compliant by
(c) 2006 Mauro Carvalho Chehab <[email protected]>
Cropping and overscan support
Copyright (C) 2005, 2006 Michael H. Schimek <[email protected]>
Sponsored by OPQ Systems AB
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/kdev_t.h>
#include "bttvp.h"
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
#include <media/i2c/tvaudio.h>
#include <media/drv-intf/msp3400.h>
#include <linux/dma-mapping.h>
#include <asm/io.h>
#include <asm/byteorder.h>
#include <media/i2c/saa6588.h>
#define BTTV_VERSION "0.9.19"
unsigned int bttv_num; /* number of Bt848s in use */
struct bttv *bttvs[BTTV_MAX];
unsigned int bttv_debug;
unsigned int bttv_verbose = 1;
unsigned int bttv_gpio;
/* config variables */
#ifdef __BIG_ENDIAN
static unsigned int bigendian=1;
#else
static unsigned int bigendian;
#endif
static unsigned int radio[BTTV_MAX];
static unsigned int irq_debug;
static unsigned int gbuffers = 8;
static unsigned int gbufsize = 0x208000;
static unsigned int reset_crop = 1;
static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
static int debug_latency;
static int disable_ir;
static unsigned int fdsr;
/* options */
static unsigned int combfilter;
static unsigned int lumafilter;
static unsigned int automute = 1;
static unsigned int chroma_agc;
static unsigned int agc_crush = 1;
static unsigned int whitecrush_upper = 0xCF;
static unsigned int whitecrush_lower = 0x7F;
static unsigned int vcr_hack;
static unsigned int irq_iswitch;
static unsigned int uv_ratio = 50;
static unsigned int full_luma_range;
static unsigned int coring;
/* API features (turn on/off stuff for testing) */
static unsigned int v4l2 = 1;
/* insmod args */
module_param(bttv_verbose, int, 0644);
module_param(bttv_gpio, int, 0644);
module_param(bttv_debug, int, 0644);
module_param(irq_debug, int, 0644);
module_param(debug_latency, int, 0644);
module_param(disable_ir, int, 0444);
module_param(fdsr, int, 0444);
module_param(gbuffers, int, 0444);
module_param(gbufsize, int, 0444);
module_param(reset_crop, int, 0444);
module_param(v4l2, int, 0644);
module_param(bigendian, int, 0644);
module_param(irq_iswitch, int, 0644);
module_param(combfilter, int, 0444);
module_param(lumafilter, int, 0444);
module_param(automute, int, 0444);
module_param(chroma_agc, int, 0444);
module_param(agc_crush, int, 0444);
module_param(whitecrush_upper, int, 0444);
module_param(whitecrush_lower, int, 0444);
module_param(vcr_hack, int, 0444);
module_param(uv_ratio, int, 0444);
module_param(full_luma_range, int, 0444);
module_param(coring, int, 0444);
module_param_array(radio, int, NULL, 0444);
module_param_array(video_nr, int, NULL, 0444);
module_param_array(radio_nr, int, NULL, 0444);
module_param_array(vbi_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
MODULE_PARM_DESC(video_nr, "video device numbers");
MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
MODULE_PARM_DESC(radio_nr, "radio device numbers");
MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
MODULE_LICENSE("GPL");
MODULE_VERSION(BTTV_VERSION);
#define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
#define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
#define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
#define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
#define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
#define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
#define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
#define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
#define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
/* ----------------------------------------------------------------------- */
/* sysfs */
static ssize_t card_show(struct device *cd,
struct device_attribute *attr, char *buf)
{
struct video_device *vfd = to_video_device(cd);
struct bttv *btv = video_get_drvdata(vfd);
return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
}
static DEVICE_ATTR_RO(card);
/* ----------------------------------------------------------------------- */
/* dvb auto-load setup */
#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
request_module("dvb-bt8xx");
}
static void request_modules(struct bttv *dev)
{
INIT_WORK(&dev->request_module_wk, request_module_async);
schedule_work(&dev->request_module_wk);
}
static void flush_request_modules(struct bttv *dev)
{
flush_work(&dev->request_module_wk);
}
#else
#define request_modules(dev)
#define flush_request_modules(dev) do {} while(0)
#endif /* CONFIG_MODULES */
/* ----------------------------------------------------------------------- */
/* static data */
/* special timing tables from conexant... */
static u8 SRAM_Table[][60] =
{
/* PAL digital input over GPIO[7:0] */
{
45, // 45 bytes following
0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
0x37,0x00,0xAF,0x21,0x00
},
/* NTSC digital input over GPIO[7:0] */
{
51, // 51 bytes following
0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
0x00,
},
// TGB_NTSC392 // quartzsight
// This table has been modified to be used for Fusion Rev D
{
0x2A, // size of table = 42
0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
0x20, 0x00
}
};
/* minhdelayx1 first video pixel we can capture on a line and
hdelayx1 start of active video, both relative to rising edge of
/HRESET pulse (0H) in 1 / fCLKx1.
swidth width of active video and
totalwidth total line width, both in 1 / fCLKx1.
sqwidth total line width in square pixels.
vdelay start of active video in 2 * field lines relative to
trailing edge of /VRESET pulse (VDELAY register).
sheight height of active video in 2 * field lines.
extraheight Added to sheight for cropcap.bounds.height only
videostart0 ITU-R frame line number of the line corresponding
to vdelay in the first field. */
#define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
vdelay, sheight, extraheight, videostart0) \
.cropcap.bounds.left = minhdelayx1, \
/* * 2 because vertically we count field lines times two, */ \
/* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
.cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
/* 4 is a safety margin at the end of the line. */ \
.cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
.cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
MIN_VDELAY, \
.cropcap.defrect.left = hdelayx1, \
.cropcap.defrect.top = (videostart0) * 2, \
.cropcap.defrect.width = swidth, \
.cropcap.defrect.height = sheight, \
.cropcap.pixelaspect.numerator = totalwidth, \
.cropcap.pixelaspect.denominator = sqwidth,
const struct bttv_tvnorm bttv_tvnorms[] = {
/* PAL-BDGHI */
/* max. active video is actually 922, but 924 is divisible by 4 and 3! */
/* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
{
.v4l2_id = V4L2_STD_PAL,
.name = "PAL",
.Fsc = 35468950,
.swidth = 924,
.sheight = 576,
.totalwidth = 1135,
.adelay = 0x7f,
.bdelay = 0x72,
.iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
.scaledtwidth = 1135,
.hdelayx1 = 186,
.hactivex1 = 924,
.vdelay = 0x20,
.vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
.sram = 0,
/* ITU-R frame line number of the first VBI line
we can capture, of the first and second field.
The last line is determined by cropcap.bounds. */
.vbistart = { 7, 320 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 186,
/* Should be (768 * 1135 + 944 / 2) / 944.
cropcap.defrect is used for image width
checks, so we keep the old value 924. */
/* swidth */ 924,
/* totalwidth */ 1135,
/* sqwidth */ 944,
/* vdelay */ 0x20,
/* sheight */ 576,
/* bt878 (and bt848?) can capture another
line below active video. */
/* extraheight */ 2,
/* videostart0 */ 23)
},{
.v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
.name = "NTSC",
.Fsc = 28636363,
.swidth = 768,
.sheight = 480,
.totalwidth = 910,
.adelay = 0x68,
.bdelay = 0x5d,
.iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
.scaledtwidth = 910,
.hdelayx1 = 128,
.hactivex1 = 910,
.vdelay = 0x1a,
.vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
.sram = 1,
.vbistart = { 10, 273 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 128,
/* Should be (640 * 910 + 780 / 2) / 780? */
/* swidth */ 768,
/* totalwidth */ 910,
/* sqwidth */ 780,
/* vdelay */ 0x1a,
/* sheight */ 480,
/* extraheight */ 0,
/* videostart0 */ 23)
},{
.v4l2_id = V4L2_STD_SECAM,
.name = "SECAM",
.Fsc = 35468950,
.swidth = 924,
.sheight = 576,
.totalwidth = 1135,
.adelay = 0x7f,
.bdelay = 0xb0,
.iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
.scaledtwidth = 1135,
.hdelayx1 = 186,
.hactivex1 = 922,
.vdelay = 0x20,
.vbipack = 255,
.sram = 0, /* like PAL, correct? */
.vbistart = { 7, 320 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 186,
/* swidth */ 924,
/* totalwidth */ 1135,
/* sqwidth */ 944,
/* vdelay */ 0x20,
/* sheight */ 576,
/* extraheight */ 0,
/* videostart0 */ 23)
},{
.v4l2_id = V4L2_STD_PAL_Nc,
.name = "PAL-Nc",
.Fsc = 28636363,
.swidth = 640,
.sheight = 576,
.totalwidth = 910,
.adelay = 0x68,
.bdelay = 0x5d,
.iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
.scaledtwidth = 780,
.hdelayx1 = 130,
.hactivex1 = 734,
.vdelay = 0x1a,
.vbipack = 144,
.sram = -1,
.vbistart = { 7, 320 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 130,
/* swidth */ (640 * 910 + 780 / 2) / 780,
/* totalwidth */ 910,
/* sqwidth */ 780,
/* vdelay */ 0x1a,
/* sheight */ 576,
/* extraheight */ 0,
/* videostart0 */ 23)
},{
.v4l2_id = V4L2_STD_PAL_M,
.name = "PAL-M",
.Fsc = 28636363,
.swidth = 640,
.sheight = 480,
.totalwidth = 910,
.adelay = 0x68,
.bdelay = 0x5d,
.iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
.scaledtwidth = 780,
.hdelayx1 = 135,
.hactivex1 = 754,
.vdelay = 0x1a,
.vbipack = 144,
.sram = -1,
.vbistart = { 10, 273 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 135,
/* swidth */ (640 * 910 + 780 / 2) / 780,
/* totalwidth */ 910,
/* sqwidth */ 780,
/* vdelay */ 0x1a,
/* sheight */ 480,
/* extraheight */ 0,
/* videostart0 */ 23)
},{
.v4l2_id = V4L2_STD_PAL_N,
.name = "PAL-N",
.Fsc = 35468950,
.swidth = 768,
.sheight = 576,
.totalwidth = 1135,
.adelay = 0x7f,
.bdelay = 0x72,
.iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
.scaledtwidth = 944,
.hdelayx1 = 186,
.hactivex1 = 922,
.vdelay = 0x20,
.vbipack = 144,
.sram = -1,
.vbistart = { 7, 320 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 186,
/* swidth */ (768 * 1135 + 944 / 2) / 944,
/* totalwidth */ 1135,
/* sqwidth */ 944,
/* vdelay */ 0x20,
/* sheight */ 576,
/* extraheight */ 0,
/* videostart0 */ 23)
},{
.v4l2_id = V4L2_STD_NTSC_M_JP,
.name = "NTSC-JP",
.Fsc = 28636363,
.swidth = 640,
.sheight = 480,
.totalwidth = 910,
.adelay = 0x68,
.bdelay = 0x5d,
.iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
.scaledtwidth = 780,
.hdelayx1 = 135,
.hactivex1 = 754,
.vdelay = 0x16,
.vbipack = 144,
.sram = -1,
.vbistart = { 10, 273 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 135,
/* swidth */ (640 * 910 + 780 / 2) / 780,
/* totalwidth */ 910,
/* sqwidth */ 780,
/* vdelay */ 0x16,
/* sheight */ 480,
/* extraheight */ 0,
/* videostart0 */ 23)
},{
/* that one hopefully works with the strange timing
* which video recorders produce when playing a NTSC
* tape on a PAL TV ... */
.v4l2_id = V4L2_STD_PAL_60,
.name = "PAL-60",
.Fsc = 35468950,
.swidth = 924,
.sheight = 480,
.totalwidth = 1135,
.adelay = 0x7f,
.bdelay = 0x72,
.iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
.scaledtwidth = 1135,
.hdelayx1 = 186,
.hactivex1 = 924,
.vdelay = 0x1a,
.vbipack = 255,
.vtotal = 524,
.sram = -1,
.vbistart = { 10, 273 },
CROPCAP(/* minhdelayx1 */ 68,
/* hdelayx1 */ 186,
/* swidth */ 924,
/* totalwidth */ 1135,
/* sqwidth */ 944,
/* vdelay */ 0x1a,
/* sheight */ 480,
/* extraheight */ 0,
/* videostart0 */ 23)
}
};
static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
/* ----------------------------------------------------------------------- */
/* bttv format list
packed pixel formats must come first */
static const struct bttv_format formats[] = {
{
.fourcc = V4L2_PIX_FMT_GREY,
.btformat = BT848_COLOR_FMT_Y8,
.depth = 8,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_HI240,
.btformat = BT848_COLOR_FMT_RGB8,
.depth = 8,
.flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
},{
.fourcc = V4L2_PIX_FMT_RGB555,
.btformat = BT848_COLOR_FMT_RGB15,
.depth = 16,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_RGB555X,
.btformat = BT848_COLOR_FMT_RGB15,
.btswap = 0x03, /* byteswap */
.depth = 16,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_RGB565,
.btformat = BT848_COLOR_FMT_RGB16,
.depth = 16,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_RGB565X,
.btformat = BT848_COLOR_FMT_RGB16,
.btswap = 0x03, /* byteswap */
.depth = 16,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_BGR24,
.btformat = BT848_COLOR_FMT_RGB24,
.depth = 24,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_BGR32,
.btformat = BT848_COLOR_FMT_RGB32,
.depth = 32,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_RGB32,
.btformat = BT848_COLOR_FMT_RGB32,
.btswap = 0x0f, /* byte+word swap */
.depth = 32,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_YUYV,
.btformat = BT848_COLOR_FMT_YUY2,
.depth = 16,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_UYVY,
.btformat = BT848_COLOR_FMT_YUY2,
.btswap = 0x03, /* byteswap */
.depth = 16,
.flags = FORMAT_FLAGS_PACKED,
},{
.fourcc = V4L2_PIX_FMT_YUV422P,
.btformat = BT848_COLOR_FMT_YCrCb422,
.depth = 16,
.flags = FORMAT_FLAGS_PLANAR,
.hshift = 1,
.vshift = 0,
},{
.fourcc = V4L2_PIX_FMT_YUV420,
.btformat = BT848_COLOR_FMT_YCrCb422,
.depth = 12,
.flags = FORMAT_FLAGS_PLANAR,
.hshift = 1,
.vshift = 1,
},{
.fourcc = V4L2_PIX_FMT_YVU420,
.btformat = BT848_COLOR_FMT_YCrCb422,
.depth = 12,
.flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
.hshift = 1,
.vshift = 1,
},{
.fourcc = V4L2_PIX_FMT_YUV411P,
.btformat = BT848_COLOR_FMT_YCrCb411,
.depth = 12,
.flags = FORMAT_FLAGS_PLANAR,
.hshift = 2,
.vshift = 0,
},{
.fourcc = V4L2_PIX_FMT_YUV410,
.btformat = BT848_COLOR_FMT_YCrCb411,
.depth = 9,
.flags = FORMAT_FLAGS_PLANAR,
.hshift = 2,
.vshift = 2,
},{
.fourcc = V4L2_PIX_FMT_YVU410,
.btformat = BT848_COLOR_FMT_YCrCb411,
.depth = 9,
.flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
.hshift = 2,
.vshift = 2,
},{
.fourcc = -1,
.btformat = BT848_COLOR_FMT_RAW,
.depth = 8,
.flags = FORMAT_FLAGS_RAW,
}
};
static const unsigned int FORMATS = ARRAY_SIZE(formats);
/* ----------------------------------------------------------------------- */
/* resource management */
/*
RESOURCE_ allocated by freed by
VIDEO_READ bttv_read 1) bttv_read 2)
VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
VIDIOC_QBUF 1) bttv_release
VIDIOCMCAPTURE 1)
VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
VIDIOC_QBUF 1) bttv_release
bttv_read, bttv_poll 1) 3)
1) The resource must be allocated when we enter buffer prepare functions
and remain allocated while buffers are in the DMA queue.
2) This is a single frame read.
3) This is a continuous read, implies VIDIOC_STREAMON.
Note this driver permits video input and standard changes regardless if
resources are allocated.
*/
#define VBI_RESOURCES (RESOURCE_VBI)
#define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
RESOURCE_VIDEO_STREAM)
int check_alloc_btres_lock(struct bttv *btv, int bit)
{
int xbits; /* mutual exclusive resources */
xbits = bit;
if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
/* is it free? */
if (btv->resources & xbits) {
/* no, someone else uses it */
goto fail;
}
if ((bit & VIDEO_RESOURCES)
&& 0 == (btv->resources & VIDEO_RESOURCES)) {
/* Do crop - use current, don't - use default parameters. */
__s32 top = btv->crop[!!btv->do_crop].rect.top;
if (btv->vbi_end > top)
goto fail;
/* We cannot capture the same line as video and VBI data.
Claim scan lines crop[].rect.top to bottom. */
btv->crop_start = top;
} else if (bit & VBI_RESOURCES) {
__s32 end = btv->vbi_fmt.end;
if (end > btv->crop_start)
goto fail;
/* Claim scan lines above btv->vbi_fmt.end. */
btv->vbi_end = end;
}
/* it's free, grab it */
btv->resources |= bit;
return 1;
fail:
return 0;
}
static
int check_btres(struct bttv *btv, int bit)
{
return (btv->resources & bit);
}
static
int locked_btres(struct bttv *btv, int bit)
{
return (btv->resources & bit);
}
/* Call with btv->lock down. */
static void
disclaim_vbi_lines(struct bttv *btv)
{
btv->vbi_end = 0;
}
/* Call with btv->lock down. */
static void
disclaim_video_lines(struct bttv *btv)
{
const struct bttv_tvnorm *tvnorm;
u8 crop;
tvnorm = &bttv_tvnorms[btv->tvnorm];
btv->crop_start = tvnorm->cropcap.bounds.top
+ tvnorm->cropcap.bounds.height;
/* VBI capturing ends at VDELAY, start of video capturing, no
matter how many lines the VBI RISC program expects. When video
capturing is off, it shall no longer "preempt" VBI capturing,
so we set VDELAY to maximum. */
crop = btread(BT848_E_CROP) | 0xc0;
btwrite(crop, BT848_E_CROP);
btwrite(0xfe, BT848_E_VDELAY_LO);
btwrite(crop, BT848_O_CROP);
btwrite(0xfe, BT848_O_VDELAY_LO);
}
void free_btres_lock(struct bttv *btv, int bits)
{
if ((btv->resources & bits) != bits) {
/* trying to free resources not allocated by us ... */
pr_err("BUG! (btres)\n");
}
btv->resources &= ~bits;
bits = btv->resources;
if (0 == (bits & VIDEO_RESOURCES))
disclaim_video_lines(btv);
if (0 == (bits & VBI_RESOURCES))
disclaim_vbi_lines(btv);
}
/* ----------------------------------------------------------------------- */
/* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
/* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
PLL_X = Reference pre-divider (0=1, 1=2)
PLL_C = Post divider (0=6, 1=4)
PLL_I = Integer input
PLL_F = Fractional input
F_input = 28.636363 MHz:
PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
*/
static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
{
unsigned char fl, fh, fi;
/* prevent overflows */
fin/=4;
fout/=4;
fout*=12;
fi=fout/fin;
fout=(fout%fin)*256;
fh=fout/fin;
fout=(fout%fin)*256;
fl=fout/fin;
btwrite(fl, BT848_PLL_F_LO);
btwrite(fh, BT848_PLL_F_HI);
btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
}
static void set_pll(struct bttv *btv)
{
int i;
if (!btv->pll.pll_crystal)
return;
if (btv->pll.pll_ofreq == btv->pll.pll_current) {
dprintk("%d: PLL: no change required\n", btv->c.nr);
return;
}
if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
/* no PLL needed */
if (btv->pll.pll_current == 0)
return;
if (bttv_verbose)
pr_info("%d: PLL can sleep, using XTAL (%d)\n",
btv->c.nr, btv->pll.pll_ifreq);
btwrite(0x00,BT848_TGCTRL);
btwrite(0x00,BT848_PLL_XCI);
btv->pll.pll_current = 0;
return;
}
if (bttv_verbose)
pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
btv->c.nr,
btv->pll.pll_ifreq, btv->pll.pll_ofreq);
set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
for (i=0; i<10; i++) {
/* Let other people run while the PLL stabilizes */
msleep(10);
if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
btwrite(0,BT848_DSTATUS);
} else {
btwrite(0x08,BT848_TGCTRL);
btv->pll.pll_current = btv->pll.pll_ofreq;
if (bttv_verbose)
pr_info("PLL set ok\n");
return;
}
}
btv->pll.pll_current = -1;
if (bttv_verbose)
pr_info("Setting PLL failed\n");
return;
}
/* used to switch between the bt848's analog/digital video capture modes */
static void bt848A_set_timing(struct bttv *btv)
{
int i, len;
int table_idx = bttv_tvnorms[btv->tvnorm].sram;
int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
if (btv->input == btv->dig) {
dprintk("%d: load digital timing table (table_idx=%d)\n",
btv->c.nr,table_idx);
/* timing change...reset timing generator address */
btwrite(0x00, BT848_TGCTRL);
btwrite(0x02, BT848_TGCTRL);
btwrite(0x00, BT848_TGCTRL);
len=SRAM_Table[table_idx][0];
for(i = 1; i <= len; i++)
btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
btv->pll.pll_ofreq = 27000000;
set_pll(btv);
btwrite(0x11, BT848_TGCTRL);
btwrite(0x41, BT848_DVSIF);
} else {
btv->pll.pll_ofreq = fsc;
set_pll(btv);
btwrite(0x0, BT848_DVSIF);
}
}
/* ----------------------------------------------------------------------- */
static void bt848_bright(struct bttv *btv, int bright)
{
int value;
// printk("set bright: %d\n", bright); // DEBUG
btv->bright = bright;
/* We want -128 to 127 we get 0-65535 */
value = (bright >> 8) - 128;
btwrite(value & 0xff, BT848_BRIGHT);
}
static void bt848_hue(struct bttv *btv, int hue)
{
int value;
btv->hue = hue;
/* -128 to 127 */
value = (hue >> 8) - 128;
btwrite(value & 0xff, BT848_HUE);
}
static void bt848_contrast(struct bttv *btv, int cont)
{
int value,hibit;
btv->contrast = cont;
/* 0-511 */
value = (cont >> 7);
hibit = (value >> 6) & 4;
btwrite(value & 0xff, BT848_CONTRAST_LO);
btaor(hibit, ~4, BT848_E_CONTROL);
btaor(hibit, ~4, BT848_O_CONTROL);
}
static void bt848_sat(struct bttv *btv, int color)
{
int val_u,val_v,hibits;
btv->saturation = color;
/* 0-511 for the color */
val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
hibits = (val_u >> 7) & 2;
hibits |= (val_v >> 8) & 1;
btwrite(val_u & 0xff, BT848_SAT_U_LO);
btwrite(val_v & 0xff, BT848_SAT_V_LO);
btaor(hibits, ~3, BT848_E_CONTROL);
btaor(hibits, ~3, BT848_O_CONTROL);
}
/* ----------------------------------------------------------------------- */
static int
video_mux(struct bttv *btv, unsigned int input)
{
int mux,mask2;
if (input >= bttv_tvcards[btv->c.type].video_inputs)
return -EINVAL;
/* needed by RemoteVideo MX */
mask2 = bttv_tvcards[btv->c.type].gpiomask2;
if (mask2)
gpio_inout(mask2,mask2);
if (input == btv->svhs) {
btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
} else {
btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
}
mux = bttv_muxsel(btv, input);
btaor(mux<<5, ~(3<<5), BT848_IFORM);
dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
/* card specific hook */
if(bttv_tvcards[btv->c.type].muxsel_hook)
bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
return 0;
}
static char *audio_modes[] = {
"audio: tuner", "audio: radio", "audio: extern",
"audio: intern", "audio: mute"
};
static void
audio_mux_gpio(struct bttv *btv, int input, int mute)
{
int gpio_val, signal, mute_gpio;
gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
bttv_tvcards[btv->c.type].gpiomask);
signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
/* automute */
mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
&& !btv->has_radio_tuner);
if (mute_gpio)
gpio_val = bttv_tvcards[btv->c.type].gpiomute;
else
gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
switch (btv->c.type) {
case BTTV_BOARD_VOODOOTV_FM:
case BTTV_BOARD_VOODOOTV_200:
gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
break;
default:
gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
}
if (bttv_gpio)
bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
}
static int
audio_mute(struct bttv *btv, int mute)
{
struct v4l2_ctrl *ctrl;
audio_mux_gpio(btv, btv->audio_input, mute);
if (btv->sd_msp34xx) {
ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
if (ctrl)
v4l2_ctrl_s_ctrl(ctrl, mute);
}
if (btv->sd_tvaudio) {
ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
if (ctrl)
v4l2_ctrl_s_ctrl(ctrl, mute);
}
if (btv->sd_tda7432) {
ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
if (ctrl)
v4l2_ctrl_s_ctrl(ctrl, mute);
}
return 0;
}
static int
audio_input(struct bttv *btv, int input)
{
audio_mux_gpio(btv, input, btv->mute);
if (btv->sd_msp34xx) {
u32 in;
/* Note: the inputs tuner/radio/extern/intern are translated
to msp routings. This assumes common behavior for all msp3400
based TV cards. When this assumption fails, then the
specific MSP routing must be added to the card table.
For now this is sufficient. */
switch (input) {
case TVAUDIO_INPUT_RADIO:
/* Some boards need the msp do to the radio demod */
if (btv->radio_uses_msp_demodulator) {
in = MSP_INPUT_DEFAULT;
break;
}
in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
break;
case TVAUDIO_INPUT_EXTERN:
in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
break;
case TVAUDIO_INPUT_INTERN:
/* Yes, this is the same input as for RADIO. I doubt
if this is ever used. The only board with an INTERN
input is the BTTV_BOARD_AVERMEDIA98. I wonder how
that was tested. My guess is that the whole INTERN
input does not work. */
in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
break;
case TVAUDIO_INPUT_TUNER:
default:
/* This is the only card that uses TUNER2, and afaik,
is the only difference between the VOODOOTV_FM
and VOODOOTV_200 */
if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
else
in = MSP_INPUT_DEFAULT;
break;
}
v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
in, MSP_OUTPUT_DEFAULT, 0);
}
if (btv->sd_tvaudio) {
v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
input, 0, 0);
}
return 0;
}
static void
bttv_crop_calc_limits(struct bttv_crop *c)
{
/* Scale factor min. 1:1, max. 16:1. Min. image size
48 x 32. Scaled width must be a multiple of 4. */
if (1) {
/* For bug compatibility with VIDIOCGCAP and image
size checks in earlier driver versions. */
c->min_scaled_width = 48;
c->min_scaled_height = 32;
} else {
c->min_scaled_width =
(max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
c->min_scaled_height =
max_t(unsigned int, 32, c->rect.height >> 4);
}
c->max_scaled_width = c->rect.width & ~3;
c->max_scaled_height = c->rect.height;
}
static void
bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
{
c->rect = bttv_tvnorms[norm].cropcap.defrect;
bttv_crop_calc_limits(c);
}
/* Call with btv->lock down. */
static int
set_tvnorm(struct bttv *btv, unsigned int norm)
{
const struct bttv_tvnorm *tvnorm;
v4l2_std_id id;
WARN_ON(norm >= BTTV_TVNORMS);
WARN_ON(btv->tvnorm >= BTTV_TVNORMS);
tvnorm = &bttv_tvnorms[norm];
if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
sizeof (tvnorm->cropcap))) {
bttv_crop_reset(&btv->crop[0], norm);
btv->crop[1] = btv->crop[0]; /* current = default */
if (0 == (btv->resources & VIDEO_RESOURCES)) {
btv->crop_start = tvnorm->cropcap.bounds.top
+ tvnorm->cropcap.bounds.height;
}
}
btv->tvnorm = norm;
btwrite(tvnorm->adelay, BT848_ADELAY);
btwrite(tvnorm->bdelay, BT848_BDELAY);
btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
BT848_IFORM);
btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
btwrite(1, BT848_VBI_PACK_DEL);
bt848A_set_timing(btv);
switch (btv->c.type) {
case BTTV_BOARD_VOODOOTV_FM:
case BTTV_BOARD_VOODOOTV_200:
bttv_tda9880_setnorm(btv, gpio_read());
break;
}
id = tvnorm->v4l2_id;
bttv_call_all(btv, video, s_std, id);
return 0;
}
/* Call with btv->lock down. */
static void
set_input(struct bttv *btv, unsigned int input, unsigned int norm)
{
unsigned long flags;
btv->input = input;
if (irq_iswitch) {
spin_lock_irqsave(&btv->s_lock,flags);
if (btv->curr.frame_irq) {
/* active capture -> delayed input switch */
btv->new_input = input;
} else {
video_mux(btv,input);
}
spin_unlock_irqrestore(&btv->s_lock,flags);
} else {
video_mux(btv,input);
}
btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
audio_input(btv, btv->audio_input);
set_tvnorm(btv, norm);
}
void init_irqreg(struct bttv *btv)
{
/* clear status */
btwrite(0xfffffUL, BT848_INT_STAT);
if (bttv_tvcards[btv->c.type].no_video) {
/* i2c only */
btwrite(BT848_INT_I2CDONE,
BT848_INT_MASK);
} else {
/* full video */
btwrite((btv->triton1) |
(btv->gpioirq ? BT848_INT_GPINT : 0) |
BT848_INT_SCERR |
(fdsr ? BT848_INT_FDSR : 0) |
BT848_INT_RISCI | BT848_INT_OCERR |
BT848_INT_FMTCHG|BT848_INT_HLOCK|
BT848_INT_I2CDONE,
BT848_INT_MASK);
}
}
static void init_bt848(struct bttv *btv)
{
if (bttv_tvcards[btv->c.type].no_video) {
/* very basic init only */
init_irqreg(btv);
return;
}
btwrite(0x00, BT848_CAP_CTL);
btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
/* set planar and packed mode trigger points and */
/* set rising edge of inverted GPINTR pin as irq trigger */
btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
BT848_GPIO_DMA_CTL_PLTP1_16|
BT848_GPIO_DMA_CTL_PLTP23_16|
BT848_GPIO_DMA_CTL_GPINTC|
BT848_GPIO_DMA_CTL_GPINTI,
BT848_GPIO_DMA_CTL);
btwrite(0x20, BT848_E_VSCALE_HI);
btwrite(0x20, BT848_O_VSCALE_HI);
v4l2_ctrl_handler_setup(&btv->ctrl_handler);
/* interrupt */
init_irqreg(btv);
}
static void bttv_reinit_bt848(struct bttv *btv)
{
unsigned long flags;
if (bttv_verbose)
pr_info("%d: reset, reinitialize\n", btv->c.nr);
spin_lock_irqsave(&btv->s_lock,flags);
btv->errors=0;
bttv_set_dma(btv,0);
spin_unlock_irqrestore(&btv->s_lock,flags);
init_bt848(btv);
btv->pll.pll_current = -1;
set_input(btv, btv->input, btv->tvnorm);
}
static int bttv_s_ctrl(struct v4l2_ctrl *c)
{
struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
int val;
switch (c->id) {
case V4L2_CID_BRIGHTNESS:
bt848_bright(btv, c->val);
break;
case V4L2_CID_HUE:
bt848_hue(btv, c->val);
break;
case V4L2_CID_CONTRAST:
bt848_contrast(btv, c->val);
break;
case V4L2_CID_SATURATION:
bt848_sat(btv, c->val);
break;
case V4L2_CID_COLOR_KILLER:
if (c->val) {
btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
} else {
btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
}
break;
case V4L2_CID_AUDIO_MUTE:
audio_mute(btv, c->val);
btv->mute = c->val;
break;
case V4L2_CID_AUDIO_VOLUME:
btv->volume_gpio(btv, c->val);
break;
case V4L2_CID_CHROMA_AGC:
val = c->val ? BT848_SCLOOP_CAGC : 0;
btwrite(val, BT848_E_SCLOOP);
btwrite(val, BT848_O_SCLOOP);
break;
case V4L2_CID_PRIVATE_COMBFILTER:
btv->opt_combfilter = c->val;
break;
case V4L2_CID_PRIVATE_LUMAFILTER:
if (c->val) {
btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
} else {
btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
}
break;
case V4L2_CID_PRIVATE_AUTOMUTE:
btv->opt_automute = c->val;
break;
case V4L2_CID_PRIVATE_AGC_CRUSH:
btwrite(BT848_ADC_RESERVED |
(c->val ? BT848_ADC_CRUSH : 0),
BT848_ADC);
break;
case V4L2_CID_PRIVATE_VCR_HACK:
btv->opt_vcr_hack = c->val;
break;
case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
btwrite(c->val, BT848_WC_UP);
break;
case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
btwrite(c->val, BT848_WC_DOWN);
break;
case V4L2_CID_PRIVATE_UV_RATIO:
btv->opt_uv_ratio = c->val;
bt848_sat(btv, btv->saturation);
break;
case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
break;
case V4L2_CID_PRIVATE_CORING:
btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
break;
default:
return -EINVAL;
}
return 0;
}
/* ----------------------------------------------------------------------- */
static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
.s_ctrl = bttv_s_ctrl,
};
static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_COMBFILTER,
.name = "Comb Filter",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 1,
};
static struct v4l2_ctrl_config bttv_ctrl_automute = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_AUTOMUTE,
.name = "Auto Mute",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 1,
};
static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_LUMAFILTER,
.name = "Luma Decimation Filter",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 1,
};
static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_AGC_CRUSH,
.name = "AGC Crush",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 1,
};
static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_VCR_HACK,
.name = "VCR Hack",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
.def = 1,
};
static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
.name = "Whitecrush Lower",
.type = V4L2_CTRL_TYPE_INTEGER,
.min = 0,
.max = 255,
.step = 1,
.def = 0x7f,
};
static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
.name = "Whitecrush Upper",
.type = V4L2_CTRL_TYPE_INTEGER,
.min = 0,
.max = 255,
.step = 1,
.def = 0xcf,
};
static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_UV_RATIO,
.name = "UV Ratio",
.type = V4L2_CTRL_TYPE_INTEGER,
.min = 0,
.max = 100,
.step = 1,
.def = 50,
};
static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
.name = "Full Luma Range",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.min = 0,
.max = 1,
.step = 1,
};
static struct v4l2_ctrl_config bttv_ctrl_coring = {
.ops = &bttv_ctrl_ops,
.id = V4L2_CID_PRIVATE_CORING,
.name = "Coring",
.type = V4L2_CTRL_TYPE_INTEGER,
.min = 0,
.max = 3,
.step = 1,
};
/* ----------------------------------------------------------------------- */
void bttv_gpio_tracking(struct bttv *btv, char *comment)
{
unsigned int outbits, data;
outbits = btread(BT848_GPIO_OUT_EN);
data = btread(BT848_GPIO_DATA);
pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
}
static const struct bttv_format*
format_by_fourcc(int fourcc)
{
unsigned int i;
for (i = 0; i < FORMATS; i++) {
if (-1 == formats[i].fourcc)
continue;
if (formats[i].fourcc == fourcc)
return formats+i;
}
return NULL;
}
/* ----------------------------------------------------------------------- */
/* video4linux (1) interface */
static int queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
unsigned int *num_planes, unsigned int sizes[],
struct device *alloc_devs[])
{
struct bttv *btv = vb2_get_drv_priv(q);
unsigned int size = btv->fmt->depth * btv->width * btv->height >> 3;
if (*num_planes)
return sizes[0] < size ? -EINVAL : 0;
*num_planes = 1;
sizes[0] = size;
return 0;
}
static void buf_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct bttv *btv = vb2_get_drv_priv(vq);
struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
unsigned long flags;
spin_lock_irqsave(&btv->s_lock, flags);
if (list_empty(&btv->capture)) {
btv->loop_irq = BT848_RISC_VIDEO;
if (vb2_is_streaming(&btv->vbiq))
btv->loop_irq |= BT848_RISC_VBI;
bttv_set_dma(btv, BT848_CAP_CTL_CAPTURE_ODD |
BT848_CAP_CTL_CAPTURE_EVEN);
}
list_add_tail(&buf->list, &btv->capture);
spin_unlock_irqrestore(&btv->s_lock, flags);
}
static int buf_prepare(struct vb2_buffer *vb)
{
struct vb2_queue *vq = vb->vb2_queue;
struct bttv *btv = vb2_get_drv_priv(vq);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
unsigned int size = (btv->fmt->depth * btv->width * btv->height) >> 3;
if (vb2_plane_size(vb, 0) < size)
return -EINVAL;
vb2_set_plane_payload(vb, 0, size);
if (btv->field != V4L2_FIELD_ALTERNATE) {
buf->vbuf.field = btv->field;
} else if (btv->field_last == V4L2_FIELD_TOP) {
buf->vbuf.field = V4L2_FIELD_BOTTOM;
btv->field_last = V4L2_FIELD_BOTTOM;
} else {
buf->vbuf.field = V4L2_FIELD_TOP;
btv->field_last = V4L2_FIELD_TOP;
}
/* Allocate memory for risc struct and create the risc program. */
return bttv_buffer_risc(btv, buf);
}
static void buf_cleanup(struct vb2_buffer *vb)
{
struct vb2_queue *vq = vb->vb2_queue;
struct bttv *btv = vb2_get_drv_priv(vq);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
btcx_riscmem_free(btv->c.pci, &buf->top);
btcx_riscmem_free(btv->c.pci, &buf->bottom);
}
static int start_streaming(struct vb2_queue *q, unsigned int count)
{
int ret = 1;
int seqnr = 0;
struct bttv_buffer *buf;
struct bttv *btv = vb2_get_drv_priv(q);
ret = check_alloc_btres_lock(btv, RESOURCE_VIDEO_STREAM);
if (ret == 0) {
if (btv->field_count)
seqnr++;
while (!list_empty(&btv->capture)) {
buf = list_entry(btv->capture.next,
struct bttv_buffer, list);
list_del(&buf->list);
buf->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
vb2_buffer_done(&buf->vbuf.vb2_buf,
VB2_BUF_STATE_QUEUED);
}
return !ret;
}
if (!vb2_is_streaming(&btv->vbiq)) {
init_irqreg(btv);
btv->field_count = 0;
}
btv->framedrop = 0;
return 0;
}
static void stop_streaming(struct vb2_queue *q)
{
unsigned long flags;
struct bttv *btv = vb2_get_drv_priv(q);
vb2_wait_for_all_buffers(q);
spin_lock_irqsave(&btv->s_lock, flags);
free_btres_lock(btv, RESOURCE_VIDEO_STREAM);
if (!vb2_is_streaming(&btv->vbiq)) {
/* stop field counter */
btand(~BT848_INT_VSYNC, BT848_INT_MASK);
}
spin_unlock_irqrestore(&btv->s_lock, flags);
}
static const struct vb2_ops bttv_video_qops = {
.queue_setup = queue_setup,
.buf_queue = buf_queue,
.buf_prepare = buf_prepare,
.buf_cleanup = buf_cleanup,
.start_streaming = start_streaming,
.stop_streaming = stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
static void radio_enable(struct bttv *btv)
{
/* Switch to the radio tuner */
if (!btv->has_radio_tuner) {
btv->has_radio_tuner = 1;
bttv_call_all(btv, tuner, s_radio);
btv->audio_input = TVAUDIO_INPUT_RADIO;
audio_input(btv, btv->audio_input);
}
}
static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
{
struct bttv *btv = video_drvdata(file);
unsigned int i;
for (i = 0; i < BTTV_TVNORMS; i++)
if (id & bttv_tvnorms[i].v4l2_id)
break;
if (i == BTTV_TVNORMS)
return -EINVAL;
btv->std = id;
set_tvnorm(btv, i);
return 0;
}
static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
struct bttv *btv = video_drvdata(file);
*id = btv->std;
return 0;
}
static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
{
struct bttv *btv = video_drvdata(file);
if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
*id &= V4L2_STD_625_50;
else
*id &= V4L2_STD_525_60;
return 0;
}
static int bttv_enum_input(struct file *file, void *priv,
struct v4l2_input *i)
{
struct bttv *btv = video_drvdata(file);
if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
return -EINVAL;
i->type = V4L2_INPUT_TYPE_CAMERA;
i->audioset = 0;
if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
sprintf(i->name, "Television");
i->type = V4L2_INPUT_TYPE_TUNER;
i->tuner = 0;
} else if (i->index == btv->svhs) {
sprintf(i->name, "S-Video");
} else {
sprintf(i->name, "Composite%d", i->index);
}
if (i->index == btv->input) {
__u32 dstatus = btread(BT848_DSTATUS);
if (0 == (dstatus & BT848_DSTATUS_PRES))
i->status |= V4L2_IN_ST_NO_SIGNAL;
if (0 == (dstatus & BT848_DSTATUS_HLOC))
i->status |= V4L2_IN_ST_NO_H_LOCK;
}
i->std = BTTV_NORMS;
return 0;
}
static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
{
struct bttv *btv = video_drvdata(file);
*i = btv->input;
return 0;
}
static int bttv_s_input(struct file *file, void *priv, unsigned int i)
{
struct bttv *btv = video_drvdata(file);
if (i >= bttv_tvcards[btv->c.type].video_inputs)
return -EINVAL;
set_input(btv, i, btv->tvnorm);
return 0;
}
static int bttv_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *t)
{
struct bttv *btv = video_drvdata(file);
if (t->index)
return -EINVAL;
bttv_call_all(btv, tuner, s_tuner, t);
if (btv->audio_mode_gpio) {
struct v4l2_tuner copy = *t;
btv->audio_mode_gpio(btv, ©, 1);
}
return 0;
}
static int bttv_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct bttv *btv = video_drvdata(file);
if (f->tuner)
return -EINVAL;
if (f->type == V4L2_TUNER_RADIO)
radio_enable(btv);
f->frequency = f->type == V4L2_TUNER_RADIO ?
btv->radio_freq : btv->tv_freq;
return 0;
}
static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
{
struct v4l2_frequency new_freq = *f;
bttv_call_all(btv, tuner, s_frequency, f);
/* s_frequency may clamp the frequency, so get the actual
frequency before assigning radio/tv_freq. */
bttv_call_all(btv, tuner, g_frequency, &new_freq);
if (new_freq.type == V4L2_TUNER_RADIO) {
radio_enable(btv);
btv->radio_freq = new_freq.frequency;
if (btv->has_tea575x) {
btv->tea.freq = btv->radio_freq;
snd_tea575x_set_freq(&btv->tea);
}
} else {
btv->tv_freq = new_freq.frequency;
}
}
static int bttv_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct bttv *btv = video_drvdata(file);
if (f->tuner)
return -EINVAL;
bttv_set_frequency(btv, f);
return 0;
}
static int bttv_log_status(struct file *file, void *f)
{
struct video_device *vdev = video_devdata(file);
struct bttv *btv = video_drvdata(file);
v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
bttv_call_all(btv, core, log_status);
return 0;
}
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int bttv_g_register(struct file *file, void *f,
struct v4l2_dbg_register *reg)
{
struct bttv *btv = video_drvdata(file);
/* bt848 has a 12-bit register space */
reg->reg &= 0xfff;
reg->val = btread(reg->reg);
reg->size = 1;
return 0;
}
static int bttv_s_register(struct file *file, void *f,
const struct v4l2_dbg_register *reg)
{
struct bttv *btv = video_drvdata(file);
/* bt848 has a 12-bit register space */
btwrite(reg->val, reg->reg & 0xfff);
return 0;
}
#endif
/* Given cropping boundaries b and the scaled width and height of a
single field or frame, which must not exceed hardware limits, this
function adjusts the cropping parameters c. */
static void
bttv_crop_adjust (struct bttv_crop * c,
const struct v4l2_rect * b,
__s32 width,
__s32 height,
enum v4l2_field field)
{
__s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
__s32 max_left;
__s32 max_top;
if (width < c->min_scaled_width) {
/* Max. hor. scale factor 16:1. */
c->rect.width = width * 16;
} else if (width > c->max_scaled_width) {
/* Min. hor. scale factor 1:1. */
c->rect.width = width;
max_left = b->left + b->width - width;
max_left = min(max_left, (__s32) MAX_HDELAY);
if (c->rect.left > max_left)
c->rect.left = max_left;
}
if (height < c->min_scaled_height) {
/* Max. vert. scale factor 16:1, single fields 8:1. */
c->rect.height = height * 16;
} else if (frame_height > c->max_scaled_height) {
/* Min. vert. scale factor 1:1.
Top and height count field lines times two. */
c->rect.height = (frame_height + 1) & ~1;
max_top = b->top + b->height - c->rect.height;
if (c->rect.top > max_top)
c->rect.top = max_top;
}
bttv_crop_calc_limits(c);
}
/* Returns an error if scaling to a frame or single field with the given
width and height is not possible with the current cropping parameters
and width aligned according to width_mask. If adjust_size is TRUE the
function may adjust the width and/or height instead, rounding width
to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
also adjust the current cropping parameters to get closer to the
desired image size. */
static int
limit_scaled_size_lock(struct bttv *btv, __s32 *width, __s32 *height,
enum v4l2_field field, unsigned int width_mask,
unsigned int width_bias, int adjust_size,
int adjust_crop)
{
const struct v4l2_rect *b;
struct bttv_crop *c;
__s32 min_width;
__s32 min_height;
__s32 max_width;
__s32 max_height;
int rc;
WARN_ON((int)width_mask >= 0 ||
width_bias >= (unsigned int)(-width_mask));
/* Make sure tvnorm, vbi_end and the current cropping parameters
remain consistent until we're done. */
b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
/* Do crop - use current, don't - use default parameters. */
c = &btv->crop[!!btv->do_crop];
if (btv->do_crop
&& adjust_size
&& adjust_crop
&& !locked_btres(btv, VIDEO_RESOURCES)) {
min_width = 48;
min_height = 32;
/* We cannot scale up. When the scaled image is larger
than crop.rect we adjust the crop.rect as required
by the V4L2 spec, hence cropcap.bounds are our limit. */
max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
max_height = b->height;
/* We cannot capture the same line as video and VBI data.
Note btv->vbi_end is really a minimum, see
bttv_vbi_try_fmt(). */
if (btv->vbi_end > b->top) {
max_height -= btv->vbi_end - b->top;
rc = -EBUSY;
if (min_height > max_height)
goto fail;
}
} else {
rc = -EBUSY;
if (btv->vbi_end > c->rect.top)
goto fail;
min_width = c->min_scaled_width;
min_height = c->min_scaled_height;
max_width = c->max_scaled_width;
max_height = c->max_scaled_height;
adjust_crop = 0;
}
min_width = (min_width - width_mask - 1) & width_mask;
max_width = max_width & width_mask;
/* Max. scale factor is 16:1 for frames, 8:1 for fields. */
/* Min. scale factor is 1:1. */
max_height >>= !V4L2_FIELD_HAS_BOTH(field);
if (adjust_size) {
*width = clamp(*width, min_width, max_width);
*height = clamp(*height, min_height, max_height);
/* Round after clamping to avoid overflow. */
*width = (*width + width_bias) & width_mask;
if (adjust_crop) {
bttv_crop_adjust(c, b, *width, *height, field);
if (btv->vbi_end > c->rect.top) {
/* Move the crop window out of the way. */
c->rect.top = btv->vbi_end;
}
}
} else {
rc = -EINVAL;
if (*width < min_width ||
*height < min_height ||
*width > max_width ||
*height > max_height ||
0 != (*width & ~width_mask))
goto fail;
}
rc = 0; /* success */
fail:
return rc;
}
static int bttv_switch_type(struct bttv *btv, enum v4l2_buf_type type)
{
int res;
struct vb2_queue *q;
switch (type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
q = &btv->capq;
res = RESOURCE_VIDEO_STREAM;
break;
case V4L2_BUF_TYPE_VBI_CAPTURE:
q = &btv->vbiq;
res = RESOURCE_VBI;
break;
default:
WARN_ON(1);
return -EINVAL;
}
if (check_btres(btv, res))
return -EBUSY;
if (vb2_is_busy(q))
return -EBUSY;
btv->type = type;
return 0;
}
static void
pix_format_set_size (struct v4l2_pix_format * f,
const struct bttv_format * fmt,
unsigned int width,
unsigned int height)
{
f->width = width;
f->height = height;
if (fmt->flags & FORMAT_FLAGS_PLANAR) {
f->bytesperline = width; /* Y plane */
f->sizeimage = (width * height * fmt->depth) >> 3;
} else {
f->bytesperline = (width * fmt->depth) >> 3;
f->sizeimage = height * f->bytesperline;
}
}
static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct bttv *btv = video_drvdata(file);
pix_format_set_size(&f->fmt.pix, btv->fmt, btv->width, btv->height);
f->fmt.pix.field = btv->field;
f->fmt.pix.pixelformat = btv->fmt->fourcc;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
unsigned int *width_mask,
unsigned int *width_bias)
{
if (fmt->flags & FORMAT_FLAGS_PLANAR) {
*width_mask = ~15; /* width must be a multiple of 16 pixels */
*width_bias = 8; /* nearest */
} else {
*width_mask = ~3; /* width must be a multiple of 4 pixels */
*width_bias = 2; /* nearest */
}
}
static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
const struct bttv_format *fmt;
struct bttv *btv = video_drvdata(file);
enum v4l2_field field;
__s32 width, height;
__s32 height2;
unsigned int width_mask, width_bias;
int rc;
fmt = format_by_fourcc(f->fmt.pix.pixelformat);
if (NULL == fmt)
return -EINVAL;
field = f->fmt.pix.field;
switch (field) {
case V4L2_FIELD_TOP:
case V4L2_FIELD_BOTTOM:
case V4L2_FIELD_ALTERNATE:
case V4L2_FIELD_INTERLACED:
break;
case V4L2_FIELD_SEQ_BT:
case V4L2_FIELD_SEQ_TB:
if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
field = V4L2_FIELD_SEQ_TB;
break;
}
fallthrough;
default: /* FIELD_ANY case */
height2 = btv->crop[!!btv->do_crop].rect.height >> 1;
field = (f->fmt.pix.height > height2)
? V4L2_FIELD_INTERLACED
: V4L2_FIELD_BOTTOM;
break;
}
width = f->fmt.pix.width;
height = f->fmt.pix.height;
bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
rc = limit_scaled_size_lock(btv, &width, &height, field, width_mask,
width_bias, 1, 0);
if (0 != rc)
return rc;
/* update data for the application */
f->fmt.pix.field = field;
pix_format_set_size(&f->fmt.pix, fmt, width, height);
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
int retval;
const struct bttv_format *fmt;
struct bttv *btv = video_drvdata(file);
__s32 width, height;
unsigned int width_mask, width_bias;
enum v4l2_field field;
retval = bttv_switch_type(btv, f->type);
if (0 != retval)
return retval;
retval = bttv_try_fmt_vid_cap(file, priv, f);
if (0 != retval)
return retval;
width = f->fmt.pix.width;
height = f->fmt.pix.height;
field = f->fmt.pix.field;
fmt = format_by_fourcc(f->fmt.pix.pixelformat);
bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
retval = limit_scaled_size_lock(btv, &width, &height, f->fmt.pix.field,
width_mask, width_bias, 1, 1);
if (0 != retval)
return retval;
f->fmt.pix.field = field;
/* update our state information */
btv->fmt = fmt;
btv->width = f->fmt.pix.width;
btv->height = f->fmt.pix.height;
btv->field = f->fmt.pix.field;
/*
* When field is V4L2_FIELD_ALTERNATE, buffers will be either
* V4L2_FIELD_TOP or V4L2_FIELD_BOTTOM depending on the value of
* field_last. Initialize field_last to V4L2_FIELD_BOTTOM so that
* streaming starts with a V4L2_FIELD_TOP buffer.
*/
btv->field_last = V4L2_FIELD_BOTTOM;
return 0;
}
static int bttv_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct bttv *btv = video_drvdata(file);
if (0 == v4l2)
return -EINVAL;
strscpy(cap->driver, "bttv", sizeof(cap->driver));
strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
if (video_is_registered(&btv->vbi_dev))
cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
if (video_is_registered(&btv->radio_dev)) {
cap->capabilities |= V4L2_CAP_RADIO;
if (btv->has_tea575x)
cap->capabilities |= V4L2_CAP_HW_FREQ_SEEK;
}
/*
* No need to lock here: those vars are initialized during board
* probe and remains untouched during the rest of the driver lifecycle
*/
if (btv->has_saa6588)
cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
if (btv->tuner_type != TUNER_ABSENT)
cap->capabilities |= V4L2_CAP_TUNER;
return 0;
}
static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
int index = -1, i;
for (i = 0; i < FORMATS; i++) {
if (formats[i].fourcc != -1)
index++;
if ((unsigned int)index == f->index)
break;
}
if (FORMATS == i)
return -EINVAL;
f->pixelformat = formats[i].fourcc;
return 0;
}
static int bttv_g_parm(struct file *file, void *f,
struct v4l2_streamparm *parm)
{
struct bttv *btv = video_drvdata(file);
if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
parm->parm.capture.readbuffers = gbuffers;
v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
&parm->parm.capture.timeperframe);
return 0;
}
static int bttv_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *t)
{
struct bttv *btv = video_drvdata(file);
if (0 != t->index)
return -EINVAL;
t->rxsubchans = V4L2_TUNER_SUB_MONO;
t->capability = V4L2_TUNER_CAP_NORM;
bttv_call_all(btv, tuner, g_tuner, t);
strscpy(t->name, "Television", sizeof(t->name));
t->type = V4L2_TUNER_ANALOG_TV;
if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
t->signal = 0xffff;
if (btv->audio_mode_gpio)
btv->audio_mode_gpio(btv, t, 0);
return 0;
}
static int bttv_g_pixelaspect(struct file *file, void *priv,
int type, struct v4l2_fract *f)
{
struct bttv *btv = video_drvdata(file);
if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
/* defrect and bounds are set via g_selection */
*f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
return 0;
}
static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
{
struct bttv *btv = video_drvdata(file);
if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
switch (sel->target) {
case V4L2_SEL_TGT_CROP:
sel->r = btv->crop[!!btv->do_crop].rect;
break;
case V4L2_SEL_TGT_CROP_DEFAULT:
sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
break;
case V4L2_SEL_TGT_CROP_BOUNDS:
sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
break;
default:
return -EINVAL;
}
return 0;
}
static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
{
struct bttv *btv = video_drvdata(file);
const struct v4l2_rect *b;
int retval;
struct bttv_crop c;
__s32 b_left;
__s32 b_top;
__s32 b_right;
__s32 b_bottom;
if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
if (sel->target != V4L2_SEL_TGT_CROP)
return -EINVAL;
/* Make sure tvnorm, vbi_end and the current cropping
parameters remain consistent until we're done. Note
read() may change vbi_end in check_alloc_btres_lock(). */
retval = -EBUSY;
if (locked_btres(btv, VIDEO_RESOURCES))
return retval;
b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
b_left = b->left;
b_right = b_left + b->width;
b_bottom = b->top + b->height;
b_top = max(b->top, btv->vbi_end);
if (b_top + 32 >= b_bottom) {
return retval;
}
/* Min. scaled size 48 x 32. */
c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
c.rect.width = clamp_t(s32, sel->r.width,
48, b_right - c.rect.left);
c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
/* Top and height must be a multiple of two. */
c.rect.top = (c.rect.top + 1) & ~1;
c.rect.height = clamp_t(s32, sel->r.height,
32, b_bottom - c.rect.top);
c.rect.height = (c.rect.height + 1) & ~1;
bttv_crop_calc_limits(&c);
sel->r = c.rect;
btv->crop[1] = c;
btv->do_crop = 1;
if (btv->width < c.min_scaled_width)
btv->width = c.min_scaled_width;
else if (btv->width > c.max_scaled_width)
btv->width = c.max_scaled_width;
if (btv->height < c.min_scaled_height)
btv->height = c.min_scaled_height;
else if (btv->height > c.max_scaled_height)
btv->height = c.max_scaled_height;
return 0;
}
static const struct v4l2_file_operations bttv_fops =
{
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = vb2_fop_release,
.unlocked_ioctl = video_ioctl2,
.read = vb2_fop_read,
.mmap = vb2_fop_mmap,
.poll = vb2_fop_poll,
};
static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
.vidioc_querycap = bttv_querycap,
.vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
.vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
.vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
.vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
.vidioc_g_pixelaspect = bttv_g_pixelaspect,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
.vidioc_s_std = bttv_s_std,
.vidioc_g_std = bttv_g_std,
.vidioc_enum_input = bttv_enum_input,
.vidioc_g_input = bttv_g_input,
.vidioc_s_input = bttv_s_input,
.vidioc_g_tuner = bttv_g_tuner,
.vidioc_s_tuner = bttv_s_tuner,
.vidioc_g_selection = bttv_g_selection,
.vidioc_s_selection = bttv_s_selection,
.vidioc_g_parm = bttv_g_parm,
.vidioc_g_frequency = bttv_g_frequency,
.vidioc_s_frequency = bttv_s_frequency,
.vidioc_log_status = bttv_log_status,
.vidioc_querystd = bttv_querystd,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
#ifdef CONFIG_VIDEO_ADV_DEBUG
.vidioc_g_register = bttv_g_register,
.vidioc_s_register = bttv_s_register,
#endif
};
static struct video_device bttv_video_template = {
.fops = &bttv_fops,
.ioctl_ops = &bttv_ioctl_ops,
.tvnorms = BTTV_NORMS,
};
/* ----------------------------------------------------------------------- */
/* radio interface */
static int radio_open(struct file *file)
{
struct video_device *vdev = video_devdata(file);
struct bttv *btv = video_drvdata(file);
int ret = v4l2_fh_open(file);
if (ret)
return ret;
dprintk("open dev=%s\n", video_device_node_name(vdev));
dprintk("%d: open called (radio)\n", btv->c.nr);
btv->radio_user++;
audio_mute(btv, btv->mute);
return 0;
}
static int radio_release(struct file *file)
{
struct bttv *btv = video_drvdata(file);
struct saa6588_command cmd;
btv->radio_user--;
bttv_call_all(btv, core, command, SAA6588_CMD_CLOSE, &cmd);
if (btv->radio_user == 0)
btv->has_radio_tuner = 0;
v4l2_fh_release(file);
return 0;
}
static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
{
struct bttv *btv = video_drvdata(file);
if (0 != t->index)
return -EINVAL;
strscpy(t->name, "Radio", sizeof(t->name));
t->type = V4L2_TUNER_RADIO;
radio_enable(btv);
bttv_call_all(btv, tuner, g_tuner, t);
if (btv->audio_mode_gpio)
btv->audio_mode_gpio(btv, t, 0);
if (btv->has_tea575x)
return snd_tea575x_g_tuner(&btv->tea, t);
return 0;
}
static int radio_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *t)
{
struct bttv *btv = video_drvdata(file);
if (0 != t->index)
return -EINVAL;
radio_enable(btv);
bttv_call_all(btv, tuner, s_tuner, t);
return 0;
}
static int radio_s_hw_freq_seek(struct file *file, void *priv,
const struct v4l2_hw_freq_seek *a)
{
struct bttv *btv = video_drvdata(file);
if (btv->has_tea575x)
return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
return -ENOTTY;
}
static int radio_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
struct bttv *btv = video_drvdata(file);
if (btv->has_tea575x)
return snd_tea575x_enum_freq_bands(&btv->tea, band);
return -ENOTTY;
}
static ssize_t radio_read(struct file *file, char __user *data,
size_t count, loff_t *ppos)
{
struct bttv *btv = video_drvdata(file);
struct saa6588_command cmd;
cmd.block_count = count / 3;
cmd.nonblocking = file->f_flags & O_NONBLOCK;
cmd.buffer = data;
cmd.instance = file;
cmd.result = -ENODEV;
radio_enable(btv);
bttv_call_all(btv, core, command, SAA6588_CMD_READ, &cmd);
return cmd.result;
}
static __poll_t radio_poll(struct file *file, poll_table *wait)
{
struct bttv *btv = video_drvdata(file);
struct saa6588_command cmd;
__poll_t rc = v4l2_ctrl_poll(file, wait);
radio_enable(btv);
cmd.instance = file;
cmd.event_list = wait;
cmd.poll_mask = 0;
bttv_call_all(btv, core, command, SAA6588_CMD_POLL, &cmd);
return rc | cmd.poll_mask;
}
static const struct v4l2_file_operations radio_fops =
{
.owner = THIS_MODULE,
.open = radio_open,
.read = radio_read,
.release = radio_release,
.unlocked_ioctl = video_ioctl2,
.poll = radio_poll,
};
static const struct v4l2_ioctl_ops radio_ioctl_ops = {
.vidioc_querycap = bttv_querycap,
.vidioc_log_status = bttv_log_status,
.vidioc_g_tuner = radio_g_tuner,
.vidioc_s_tuner = radio_s_tuner,
.vidioc_g_frequency = bttv_g_frequency,
.vidioc_s_frequency = bttv_s_frequency,
.vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
.vidioc_enum_freq_bands = radio_enum_freq_bands,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static struct video_device radio_template = {
.fops = &radio_fops,
.ioctl_ops = &radio_ioctl_ops,
};
/* ----------------------------------------------------------------------- */
/* some debug code */
static int bttv_risc_decode(u32 risc)
{
static char *instr[16] = {
[ BT848_RISC_WRITE >> 28 ] = "write",
[ BT848_RISC_SKIP >> 28 ] = "skip",
[ BT848_RISC_WRITEC >> 28 ] = "writec",
[ BT848_RISC_JUMP >> 28 ] = "jump",
[ BT848_RISC_SYNC >> 28 ] = "sync",
[ BT848_RISC_WRITE123 >> 28 ] = "write123",
[ BT848_RISC_SKIP123 >> 28 ] = "skip123",
[ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
};
static int incr[16] = {
[ BT848_RISC_WRITE >> 28 ] = 2,
[ BT848_RISC_JUMP >> 28 ] = 2,
[ BT848_RISC_SYNC >> 28 ] = 2,
[ BT848_RISC_WRITE123 >> 28 ] = 5,
[ BT848_RISC_SKIP123 >> 28 ] = 2,
[ BT848_RISC_WRITE1S23 >> 28 ] = 3,
};
static char *bits[] = {
"be0", "be1", "be2", "be3/resync",
"set0", "set1", "set2", "set3",
"clr0", "clr1", "clr2", "clr3",
"irq", "res", "eol", "sol",
};
int i;
pr_cont("0x%08x [ %s", risc,
instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
if (risc & (1 << (i + 12)))
pr_cont(" %s", bits[i]);
pr_cont(" count=%d ]\n", risc & 0xfff);
return incr[risc >> 28] ? incr[risc >> 28] : 1;
}
static void bttv_risc_disasm(struct bttv *btv,
struct btcx_riscmem *risc)
{
unsigned int i,j,n;
pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
for (i = 0; i < (risc->size >> 2); i += n) {
pr_info("%s: 0x%lx: ",
btv->c.v4l2_dev.name,
(unsigned long)(risc->dma + (i<<2)));
n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
for (j = 1; j < n; j++)
pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
btv->c.v4l2_dev.name,
(unsigned long)(risc->dma + ((i+j)<<2)),
risc->cpu[i+j], j);
if (0 == risc->cpu[i])
break;
}
}
static void bttv_print_riscaddr(struct bttv *btv)
{
pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
pr_info(" vbi : o=%08llx e=%08llx\n",
btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
pr_info(" cap : o=%08llx e=%08llx\n",
btv->curr.top
? (unsigned long long)btv->curr.top->top.dma : 0,
btv->curr.bottom
? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
bttv_risc_disasm(btv, &btv->main);
}
/* ----------------------------------------------------------------------- */
/* irq handler */
static char *irq_name[] = {
"FMTCHG", // format change detected (525 vs. 625)
"VSYNC", // vertical sync (new field)
"HSYNC", // horizontal sync
"OFLOW", // chroma/luma AGC overflow
"HLOCK", // horizontal lock changed
"VPRES", // video presence changed
"6", "7",
"I2CDONE", // hw irc operation finished
"GPINT", // gpio port triggered irq
"10",
"RISCI", // risc instruction triggered irq
"FBUS", // pixel data fifo dropped data (high pci bus latencies)
"FTRGT", // pixel data fifo overrun
"FDSR", // fifo data stream resyncronisation
"PPERR", // parity error (data transfer)
"RIPERR", // parity error (read risc instructions)
"PABORT", // pci abort
"OCERR", // risc instruction error
"SCERR", // syncronisation error
};
static void bttv_print_irqbits(u32 print, u32 mark)
{
unsigned int i;
pr_cont("bits:");
for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
if (print & (1 << i))
pr_cont(" %s", irq_name[i]);
if (mark & (1 << i))
pr_cont("*");
}
}
static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
{
pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
btv->c.nr,
(unsigned long)btv->main.dma,
(unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
(unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
(unsigned long)rc);
if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
btv->c.nr);
return;
}
pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
btv->c.nr);
pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
btv->c.nr);
dump_stack();
}
static int
bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
{
struct bttv_buffer *item;
memset(set,0,sizeof(*set));
/* capture request ? */
if (!list_empty(&btv->capture)) {
set->frame_irq = BT848_RISC_VIDEO;
item = list_entry(btv->capture.next, struct bttv_buffer, list);
if (V4L2_FIELD_HAS_TOP(item->vbuf.field))
set->top = item;
if (V4L2_FIELD_HAS_BOTTOM(item->vbuf.field))
set->bottom = item;
/* capture request for other field ? */
if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field) &&
item->list.next != &btv->capture) {
item = list_entry(item->list.next,
struct bttv_buffer, list);
/* Mike Isely <[email protected]> - Only check
* and set up the bottom field in the logic
* below. Don't ever do the top field. This
* of course means that if we set up the
* bottom field in the above code that we'll
* actually skip a field. But that's OK.
* Having processed only a single buffer this
* time, then the next time around the first
* available buffer should be for a top field.
* That will then cause us here to set up a
* top then a bottom field in the normal way.
* The alternative to this understanding is
* that we set up the second available buffer
* as a top field, but that's out of order
* since this driver always processes the top
* field first - the effect will be the two
* buffers being returned in the wrong order,
* with the second buffer also being delayed
* by one field time (owing to the fifo nature
* of videobuf). Worse still, we'll be stuck
* doing fields out of order now every time
* until something else causes a field to be
* dropped. By effectively forcing a field to
* drop this way then we always get back into
* sync within a single frame time. (Out of
* order fields can screw up deinterlacing
* algorithms.) */
if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field)) {
if (!set->bottom &&
item->vbuf.field == V4L2_FIELD_BOTTOM)
set->bottom = item;
if (set->top && set->bottom) {
/*
* The buffer set has a top buffer and
* a bottom buffer and they are not
* copies of each other.
*/
set->top_irq = BT848_RISC_TOP;
}
}
}
}
dprintk("%d: next set: top=%p bottom=%p [irq=%d,%d]\n",
btv->c.nr, set->top, set->bottom,
set->frame_irq, set->top_irq);
return 0;
}
static void
bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
struct bttv_buffer_set *curr, unsigned int state)
{
u64 ts = ktime_get_ns();
if (wakeup->top == wakeup->bottom) {
if (NULL != wakeup->top && curr->top != wakeup->top) {
if (irq_debug > 1)
pr_debug("%d: wakeup: both=%p\n",
btv->c.nr, wakeup->top);
wakeup->top->vbuf.vb2_buf.timestamp = ts;
wakeup->top->vbuf.sequence = btv->field_count >> 1;
vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
if (btv->field_count == 0)
btor(BT848_INT_VSYNC, BT848_INT_MASK);
}
} else {
if (NULL != wakeup->top && curr->top != wakeup->top) {
if (irq_debug > 1)
pr_debug("%d: wakeup: top=%p\n",
btv->c.nr, wakeup->top);
wakeup->top->vbuf.vb2_buf.timestamp = ts;
wakeup->top->vbuf.sequence = btv->field_count >> 1;
vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
if (btv->field_count == 0)
btor(BT848_INT_VSYNC, BT848_INT_MASK);
}
if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
if (irq_debug > 1)
pr_debug("%d: wakeup: bottom=%p\n",
btv->c.nr, wakeup->bottom);
wakeup->bottom->vbuf.vb2_buf.timestamp = ts;
wakeup->bottom->vbuf.sequence = btv->field_count >> 1;
vb2_buffer_done(&wakeup->bottom->vbuf.vb2_buf, state);
if (btv->field_count == 0)
btor(BT848_INT_VSYNC, BT848_INT_MASK);
}
}
}
static void
bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
unsigned int state)
{
if (NULL == wakeup)
return;
wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
wakeup->vbuf.sequence = btv->field_count >> 1;
vb2_buffer_done(&wakeup->vbuf.vb2_buf, state);
if (btv->field_count == 0)
btor(BT848_INT_VSYNC, BT848_INT_MASK);
}
static void bttv_irq_timeout(struct timer_list *t)
{
struct bttv *btv = from_timer(btv, t, timeout);
struct bttv_buffer_set old,new;
struct bttv_buffer *ovbi;
struct bttv_buffer *item;
unsigned long flags;
int seqnr = 0;
if (bttv_verbose) {
pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
btread(BT848_RISC_COUNT));
bttv_print_irqbits(btread(BT848_INT_STAT),0);
pr_cont("\n");
}
spin_lock_irqsave(&btv->s_lock,flags);
/* deactivate stuff */
memset(&new,0,sizeof(new));
old = btv->curr;
ovbi = btv->cvbi;
btv->curr = new;
btv->cvbi = NULL;
btv->loop_irq = 0;
bttv_buffer_activate_video(btv, &new);
bttv_buffer_activate_vbi(btv, NULL);
bttv_set_dma(btv, 0);
/* wake up */
bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
bttv_irq_wakeup_vbi(btv, ovbi, VB2_BUF_STATE_DONE);
/* cancel all outstanding capture / vbi requests */
if (btv->field_count)
seqnr++;
while (!list_empty(&btv->capture)) {
item = list_entry(btv->capture.next, struct bttv_buffer, list);
list_del(&item->list);
item->vbuf.vb2_buf.timestamp = ktime_get_ns();
item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
}
while (!list_empty(&btv->vcapture)) {
item = list_entry(btv->vcapture.next, struct bttv_buffer, list);
list_del(&item->list);
item->vbuf.vb2_buf.timestamp = ktime_get_ns();
item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
}
btv->errors++;
spin_unlock_irqrestore(&btv->s_lock,flags);
}
static void
bttv_irq_wakeup_top(struct bttv *btv)
{
struct bttv_buffer *wakeup = btv->curr.top;
if (NULL == wakeup)
return;
spin_lock(&btv->s_lock);
btv->curr.top_irq = 0;
btv->curr.top = NULL;
bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
wakeup->vbuf.sequence = btv->field_count >> 1;
vb2_buffer_done(&wakeup->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
if (btv->field_count == 0)
btor(BT848_INT_VSYNC, BT848_INT_MASK);
spin_unlock(&btv->s_lock);
}
static inline int is_active(struct btcx_riscmem *risc, u32 rc)
{
if (rc < risc->dma)
return 0;
if (rc > risc->dma + risc->size)
return 0;
return 1;
}
static void
bttv_irq_switch_video(struct bttv *btv)
{
struct bttv_buffer_set new;
struct bttv_buffer_set old;
dma_addr_t rc;
spin_lock(&btv->s_lock);
/* new buffer set */
bttv_irq_next_video(btv, &new);
rc = btread(BT848_RISC_COUNT);
if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
(btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
btv->framedrop++;
if (debug_latency)
bttv_irq_debug_low_latency(btv, rc);
spin_unlock(&btv->s_lock);
return;
}
/* switch over */
old = btv->curr;
btv->curr = new;
btv->loop_irq &= ~BT848_RISC_VIDEO;
bttv_buffer_activate_video(btv, &new);
bttv_set_dma(btv, 0);
/* switch input */
if (UNSET != btv->new_input) {
video_mux(btv,btv->new_input);
btv->new_input = UNSET;
}
/* wake up finished buffers */
bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
spin_unlock(&btv->s_lock);
}
static void
bttv_irq_switch_vbi(struct bttv *btv)
{
struct bttv_buffer *new = NULL;
struct bttv_buffer *old;
u32 rc;
spin_lock(&btv->s_lock);
if (!list_empty(&btv->vcapture))
new = list_entry(btv->vcapture.next, struct bttv_buffer, list);
old = btv->cvbi;
rc = btread(BT848_RISC_COUNT);
if (NULL != old && (is_active(&old->top, rc) ||
is_active(&old->bottom, rc))) {
btv->framedrop++;
if (debug_latency)
bttv_irq_debug_low_latency(btv, rc);
spin_unlock(&btv->s_lock);
return;
}
/* switch */
btv->cvbi = new;
btv->loop_irq &= ~BT848_RISC_VBI;
bttv_buffer_activate_vbi(btv, new);
bttv_set_dma(btv, 0);
bttv_irq_wakeup_vbi(btv, old, VB2_BUF_STATE_DONE);
spin_unlock(&btv->s_lock);
}
static irqreturn_t bttv_irq(int irq, void *dev_id)
{
u32 stat,astat;
u32 dstat;
int count;
struct bttv *btv;
int handled = 0;
btv=(struct bttv *)dev_id;
count=0;
while (1) {
/* get/clear interrupt status bits */
stat=btread(BT848_INT_STAT);
astat=stat&btread(BT848_INT_MASK);
if (!astat)
break;
handled = 1;
btwrite(stat,BT848_INT_STAT);
/* get device status bits */
dstat=btread(BT848_DSTATUS);
if (irq_debug) {
pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
btv->c.nr, count, btv->field_count,
stat>>28, btread(BT848_RISC_COUNT));
bttv_print_irqbits(stat,astat);
if (stat & BT848_INT_HLOCK)
pr_cont(" HLOC => %s",
dstat & BT848_DSTATUS_HLOC
? "yes" : "no");
if (stat & BT848_INT_VPRES)
pr_cont(" PRES => %s",
dstat & BT848_DSTATUS_PRES
? "yes" : "no");
if (stat & BT848_INT_FMTCHG)
pr_cont(" NUML => %s",
dstat & BT848_DSTATUS_NUML
? "625" : "525");
pr_cont("\n");
}
if (astat&BT848_INT_VSYNC)
btv->field_count++;
if ((astat & BT848_INT_GPINT) && btv->remote) {
bttv_input_irq(btv);
}
if (astat & BT848_INT_I2CDONE) {
btv->i2c_done = stat;
wake_up(&btv->i2c_queue);
}
if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VBI))
bttv_irq_switch_vbi(btv);
if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_TOP))
bttv_irq_wakeup_top(btv);
if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VIDEO))
bttv_irq_switch_video(btv);
if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
/* trigger automute */
audio_mux_gpio(btv, btv->audio_input, btv->mute);
if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
pr_info("%d: %s%s @ %08x,",
btv->c.nr,
(astat & BT848_INT_SCERR) ? "SCERR" : "",
(astat & BT848_INT_OCERR) ? "OCERR" : "",
btread(BT848_RISC_COUNT));
bttv_print_irqbits(stat,astat);
pr_cont("\n");
if (bttv_debug)
bttv_print_riscaddr(btv);
}
if (fdsr && astat & BT848_INT_FDSR) {
pr_info("%d: FDSR @ %08x\n",
btv->c.nr, btread(BT848_RISC_COUNT));
if (bttv_debug)
bttv_print_riscaddr(btv);
}
count++;
if (count > 4) {
if (count > 8 || !(astat & BT848_INT_GPINT)) {
btwrite(0, BT848_INT_MASK);
pr_err("%d: IRQ lockup, cleared int mask [",
btv->c.nr);
} else {
pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
btv->c.nr);
btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
BT848_INT_MASK);
}
bttv_print_irqbits(stat,astat);
pr_cont("]\n");
}
}
btv->irq_total++;
if (handled)
btv->irq_me++;
return IRQ_RETVAL(handled);
}
/* ----------------------------------------------------------------------- */
/* initialization */
static int vdev_init(struct bttv *btv, struct video_device *vfd,
const struct video_device *template,
const char *type_name)
{
int err;
struct vb2_queue *q;
*vfd = *template;
vfd->v4l2_dev = &btv->c.v4l2_dev;
vfd->release = video_device_release_empty;
video_set_drvdata(vfd, btv);
snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
type_name, bttv_tvcards[btv->c.type].name);
if (btv->tuner_type == TUNER_ABSENT) {
v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
}
if (strcmp(type_name, "radio") == 0)
return 0;
if (strcmp(type_name, "video") == 0) {
q = &btv->capq;
q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
q->ops = &bttv_video_qops;
} else if (strcmp(type_name, "vbi") == 0) {
q = &btv->vbiq;
q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
q->ops = &bttv_vbi_qops;
} else {
return -EINVAL;
}
q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
q->mem_ops = &vb2_dma_sg_memops;
q->drv_priv = btv;
q->gfp_flags = __GFP_DMA32;
q->buf_struct_size = sizeof(struct bttv_buffer);
q->lock = &btv->lock;
q->min_buffers_needed = 2;
q->dev = &btv->c.pci->dev;
err = vb2_queue_init(q);
if (err)
return err;
vfd->queue = q;
return 0;
}
static void bttv_unregister_video(struct bttv *btv)
{
video_unregister_device(&btv->video_dev);
video_unregister_device(&btv->vbi_dev);
video_unregister_device(&btv->radio_dev);
}
/* register video4linux devices */
static int bttv_register_video(struct bttv *btv)
{
/* video */
vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
btv->video_dev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
if (btv->tuner_type != TUNER_ABSENT)
btv->video_dev.device_caps |= V4L2_CAP_TUNER;
if (video_register_device(&btv->video_dev, VFL_TYPE_VIDEO,
video_nr[btv->c.nr]) < 0)
goto err;
pr_info("%d: registered device %s\n",
btv->c.nr, video_device_node_name(&btv->video_dev));
if (device_create_file(&btv->video_dev.dev,
&dev_attr_card)<0) {
pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
goto err;
}
/* vbi */
vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
btv->vbi_dev.device_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING;
if (btv->tuner_type != TUNER_ABSENT)
btv->vbi_dev.device_caps |= V4L2_CAP_TUNER;
if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
vbi_nr[btv->c.nr]) < 0)
goto err;
pr_info("%d: registered device %s\n",
btv->c.nr, video_device_node_name(&btv->vbi_dev));
if (!btv->has_radio)
return 0;
/* radio */
vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
btv->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
if (btv->has_saa6588)
btv->radio_dev.device_caps |= V4L2_CAP_READWRITE |
V4L2_CAP_RDS_CAPTURE;
if (btv->has_tea575x)
btv->radio_dev.device_caps |= V4L2_CAP_HW_FREQ_SEEK;
btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
radio_nr[btv->c.nr]) < 0)
goto err;
pr_info("%d: registered device %s\n",
btv->c.nr, video_device_node_name(&btv->radio_dev));
/* all done */
return 0;
err:
bttv_unregister_video(btv);
return -1;
}
/* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
/* response on cards with no firmware is not enabled by OF */
static void pci_set_command(struct pci_dev *dev)
{
#if defined(__powerpc__)
unsigned int cmd;
pci_read_config_dword(dev, PCI_COMMAND, &cmd);
cmd = (cmd | PCI_COMMAND_MEMORY );
pci_write_config_dword(dev, PCI_COMMAND, cmd);
#endif
}
static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
{
struct v4l2_frequency init_freq = {
.tuner = 0,
.type = V4L2_TUNER_ANALOG_TV,
.frequency = 980,
};
int result;
unsigned char lat;
struct bttv *btv;
struct v4l2_ctrl_handler *hdl;
if (bttv_num == BTTV_MAX)
return -ENOMEM;
pr_info("Bt8xx card found (%d)\n", bttv_num);
bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
if (btv == NULL) {
pr_err("out of memory\n");
return -ENOMEM;
}
btv->c.nr = bttv_num;
snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
"bttv%d", btv->c.nr);
/* initialize structs / fill in defaults */
mutex_init(&btv->lock);
spin_lock_init(&btv->s_lock);
spin_lock_init(&btv->gpio_lock);
init_waitqueue_head(&btv->i2c_queue);
INIT_LIST_HEAD(&btv->c.subs);
INIT_LIST_HEAD(&btv->capture);
INIT_LIST_HEAD(&btv->vcapture);
timer_setup(&btv->timeout, bttv_irq_timeout, 0);
btv->i2c_rc = -1;
btv->tuner_type = UNSET;
btv->new_input = UNSET;
btv->has_radio=radio[btv->c.nr];
/* pci stuff (init, get irq/mmio, ... */
btv->c.pci = dev;
btv->id = dev->device;
if (pci_enable_device(dev)) {
pr_warn("%d: Can't enable device\n", btv->c.nr);
result = -EIO;
goto free_mem;
}
if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
pr_warn("%d: No suitable DMA available\n", btv->c.nr);
result = -EIO;
goto free_mem;
}
if (!request_mem_region(pci_resource_start(dev,0),
pci_resource_len(dev,0),
btv->c.v4l2_dev.name)) {
pr_warn("%d: can't request iomem (0x%llx)\n",
btv->c.nr,
(unsigned long long)pci_resource_start(dev, 0));
result = -EBUSY;
goto free_mem;
}
pci_set_master(dev);
pci_set_command(dev);
result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
if (result < 0) {
pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
goto fail0;
}
hdl = &btv->ctrl_handler;
v4l2_ctrl_handler_init(hdl, 20);
btv->c.v4l2_dev.ctrl_handler = hdl;
v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
btv->revision = dev->revision;
pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
bttv_num, btv->id, btv->revision, pci_name(dev),
btv->c.pci->irq, lat,
(unsigned long long)pci_resource_start(dev, 0));
schedule();
btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
if (NULL == btv->bt848_mmio) {
pr_err("%d: ioremap() failed\n", btv->c.nr);
result = -EIO;
goto fail1;
}
/* identify card */
bttv_idcard(btv);
/* disable irqs, register irq handler */
btwrite(0, BT848_INT_MASK);
result = request_irq(btv->c.pci->irq, bttv_irq,
IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
if (result < 0) {
pr_err("%d: can't get IRQ %d\n",
bttv_num, btv->c.pci->irq);
goto fail1;
}
if (0 != bttv_handle_chipset(btv)) {
result = -EIO;
goto fail2;
}
/* init options from insmod args */
btv->opt_combfilter = combfilter;
bttv_ctrl_combfilter.def = combfilter;
bttv_ctrl_lumafilter.def = lumafilter;
btv->opt_automute = automute;
bttv_ctrl_automute.def = automute;
bttv_ctrl_agc_crush.def = agc_crush;
btv->opt_vcr_hack = vcr_hack;
bttv_ctrl_vcr_hack.def = vcr_hack;
bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
btv->opt_uv_ratio = uv_ratio;
bttv_ctrl_uv_ratio.def = uv_ratio;
bttv_ctrl_full_luma.def = full_luma_range;
bttv_ctrl_coring.def = coring;
/* fill struct bttv with some useful defaults */
btv->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
btv->width = 320;
btv->height = 240;
btv->field = V4L2_FIELD_INTERLACED;
btv->input = 0;
btv->tvnorm = 0; /* Index into bttv_tvnorms[] i.e. PAL. */
bttv_vbi_fmt_reset(&btv->vbi_fmt, btv->tvnorm);
btv->vbi_count[0] = VBI_DEFLINES;
btv->vbi_count[1] = VBI_DEFLINES;
btv->do_crop = 0;
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
if (btv->volume_gpio)
v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
/* initialize hardware */
if (bttv_gpio)
bttv_gpio_tracking(btv,"pre-init");
bttv_risc_init_main(btv);
init_bt848(btv);
/* gpio */
btwrite(0x00, BT848_GPIO_REG_INP);
btwrite(0x00, BT848_GPIO_OUT_EN);
if (bttv_verbose)
bttv_gpio_tracking(btv,"init");
/* needs to be done before i2c is registered */
bttv_init_card1(btv);
/* register i2c + gpio */
init_bttv_i2c(btv);
/* some card-specific stuff (needs working i2c) */
bttv_init_card2(btv);
bttv_init_tuner(btv);
if (btv->tuner_type != TUNER_ABSENT) {
bttv_set_frequency(btv, &init_freq);
btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
}
btv->std = V4L2_STD_PAL;
init_irqreg(btv);
if (!bttv_tvcards[btv->c.type].no_video)
v4l2_ctrl_handler_setup(hdl);
if (hdl->error) {
result = hdl->error;
goto fail2;
}
/* mute device */
audio_mute(btv, 1);
/* register video4linux + input */
if (!bttv_tvcards[btv->c.type].no_video) {
v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
v4l2_ctrl_radio_filter, false);
if (btv->radio_ctrl_handler.error) {
result = btv->radio_ctrl_handler.error;
goto fail2;
}
set_input(btv, btv->input, btv->tvnorm);
bttv_crop_reset(&btv->crop[0], btv->tvnorm);
btv->crop[1] = btv->crop[0]; /* current = default */
disclaim_vbi_lines(btv);
disclaim_video_lines(btv);
bttv_register_video(btv);
}
/* add subdevices and autoload dvb-bt8xx if needed */
if (bttv_tvcards[btv->c.type].has_dvb) {
bttv_sub_add_device(&btv->c, "dvb");
request_modules(btv);
}
if (!disable_ir) {
init_bttv_i2c_ir(btv);
bttv_input_init(btv);
}
/* everything is fine */
bttv_num++;
return 0;
fail2:
free_irq(btv->c.pci->irq,btv);
fail1:
v4l2_ctrl_handler_free(&btv->ctrl_handler);
v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
v4l2_device_unregister(&btv->c.v4l2_dev);
fail0:
if (btv->bt848_mmio)
iounmap(btv->bt848_mmio);
release_mem_region(pci_resource_start(btv->c.pci,0),
pci_resource_len(btv->c.pci,0));
pci_disable_device(btv->c.pci);
free_mem:
bttvs[btv->c.nr] = NULL;
kfree(btv);
return result;
}
static void bttv_remove(struct pci_dev *pci_dev)
{
struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
struct bttv *btv = to_bttv(v4l2_dev);
if (bttv_verbose)
pr_info("%d: unloading\n", btv->c.nr);
if (bttv_tvcards[btv->c.type].has_dvb)
flush_request_modules(btv);
/* shutdown everything (DMA+IRQs) */
btand(~15, BT848_GPIO_DMA_CTL);
btwrite(0, BT848_INT_MASK);
btwrite(~0x0, BT848_INT_STAT);
btwrite(0x0, BT848_GPIO_OUT_EN);
if (bttv_gpio)
bttv_gpio_tracking(btv,"cleanup");
/* tell gpio modules we are leaving ... */
btv->shutdown=1;
bttv_input_fini(btv);
bttv_sub_del_devices(&btv->c);
/* unregister i2c_bus + input */
fini_bttv_i2c(btv);
/* unregister video4linux */
bttv_unregister_video(btv);
/* free allocated memory */
v4l2_ctrl_handler_free(&btv->ctrl_handler);
v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
btcx_riscmem_free(btv->c.pci,&btv->main);
/* free resources */
free_irq(btv->c.pci->irq,btv);
iounmap(btv->bt848_mmio);
release_mem_region(pci_resource_start(btv->c.pci,0),
pci_resource_len(btv->c.pci,0));
pci_disable_device(btv->c.pci);
v4l2_device_unregister(&btv->c.v4l2_dev);
bttvs[btv->c.nr] = NULL;
kfree(btv);
return;
}
static int __maybe_unused bttv_suspend(struct device *dev)
{
struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
struct bttv *btv = to_bttv(v4l2_dev);
struct bttv_buffer_set idle;
unsigned long flags;
dprintk("%d: suspend\n", btv->c.nr);
/* stop dma + irqs */
spin_lock_irqsave(&btv->s_lock,flags);
memset(&idle, 0, sizeof(idle));
btv->state.video = btv->curr;
btv->state.vbi = btv->cvbi;
btv->state.loop_irq = btv->loop_irq;
btv->curr = idle;
btv->loop_irq = 0;
bttv_buffer_activate_video(btv, &idle);
bttv_buffer_activate_vbi(btv, NULL);
bttv_set_dma(btv, 0);
btwrite(0, BT848_INT_MASK);
spin_unlock_irqrestore(&btv->s_lock,flags);
/* save bt878 state */
btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
btv->state.gpio_data = gpio_read();
btv->state.disabled = 1;
return 0;
}
static int __maybe_unused bttv_resume(struct device *dev)
{
struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
struct bttv *btv = to_bttv(v4l2_dev);
unsigned long flags;
dprintk("%d: resume\n", btv->c.nr);
btv->state.disabled = 0;
/* restore bt878 state */
bttv_reinit_bt848(btv);
gpio_inout(0xffffff, btv->state.gpio_enable);
gpio_write(btv->state.gpio_data);
/* restart dma */
spin_lock_irqsave(&btv->s_lock,flags);
btv->curr = btv->state.video;
btv->cvbi = btv->state.vbi;
btv->loop_irq = btv->state.loop_irq;
bttv_buffer_activate_video(btv, &btv->curr);
bttv_buffer_activate_vbi(btv, btv->cvbi);
bttv_set_dma(btv, 0);
spin_unlock_irqrestore(&btv->s_lock,flags);
return 0;
}
static const struct pci_device_id bttv_pci_tbl[] = {
{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
{PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
{0,}
};
MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
static SIMPLE_DEV_PM_OPS(bttv_pm_ops,
bttv_suspend,
bttv_resume);
static struct pci_driver bttv_pci_driver = {
.name = "bttv",
.id_table = bttv_pci_tbl,
.probe = bttv_probe,
.remove = bttv_remove,
.driver.pm = &bttv_pm_ops,
};
static int __init bttv_init_module(void)
{
int ret;
bttv_num = 0;
pr_info("driver version %s loaded\n", BTTV_VERSION);
if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
gbuffers = 2;
if (gbufsize > BTTV_MAX_FBUF)
gbufsize = BTTV_MAX_FBUF;
gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
if (bttv_verbose)
pr_info("using %d buffers with %dk (%d pages) each for capture\n",
gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
bttv_check_chipset();
ret = bus_register(&bttv_sub_bus_type);
if (ret < 0) {
pr_warn("bus_register error: %d\n", ret);
return ret;
}
ret = pci_register_driver(&bttv_pci_driver);
if (ret < 0)
bus_unregister(&bttv_sub_bus_type);
return ret;
}
static void __exit bttv_cleanup_module(void)
{
pci_unregister_driver(&bttv_pci_driver);
bus_unregister(&bttv_sub_bus_type);
}
module_init(bttv_init_module);
module_exit(bttv_cleanup_module);
| linux-master | drivers/media/pci/bt8xx/bttv-driver.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv-cards.c
this file has configuration information - card-specific stuff
like the big tvcards array for the most part
Copyright (C) 1996,97,98 Ralph Metzler ([email protected])
& Marcus Metzler ([email protected])
(c) 1999-2001 Gerd Knorr <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/vmalloc.h>
#include <linux/firmware.h>
#include <net/checksum.h>
#include <asm/unaligned.h>
#include <asm/io.h>
#include "bttvp.h"
#include <media/v4l2-common.h>
#include <media/i2c/tvaudio.h>
#include "bttv-audio-hook.h"
/* fwd decl */
static void boot_msp34xx(struct bttv *btv, int pin);
static void hauppauge_eeprom(struct bttv *btv);
static void avermedia_eeprom(struct bttv *btv);
static void osprey_eeprom(struct bttv *btv, const u8 ee[256]);
static void modtec_eeprom(struct bttv *btv);
static void init_PXC200(struct bttv *btv);
static void init_RTV24(struct bttv *btv);
static void init_PCI8604PW(struct bttv *btv);
static void rv605_muxsel(struct bttv *btv, unsigned int input);
static void eagle_muxsel(struct bttv *btv, unsigned int input);
static void xguard_muxsel(struct bttv *btv, unsigned int input);
static void ivc120_muxsel(struct bttv *btv, unsigned int input);
static void gvc1100_muxsel(struct bttv *btv, unsigned int input);
static void PXC200_muxsel(struct bttv *btv, unsigned int input);
static void picolo_tetra_muxsel(struct bttv *btv, unsigned int input);
static void picolo_tetra_init(struct bttv *btv);
static void tibetCS16_muxsel(struct bttv *btv, unsigned int input);
static void tibetCS16_init(struct bttv *btv);
static void kodicom4400r_muxsel(struct bttv *btv, unsigned int input);
static void kodicom4400r_init(struct bttv *btv);
static void sigmaSLC_muxsel(struct bttv *btv, unsigned int input);
static void sigmaSQ_muxsel(struct bttv *btv, unsigned int input);
static void geovision_muxsel(struct bttv *btv, unsigned int input);
static void phytec_muxsel(struct bttv *btv, unsigned int input);
static void gv800s_muxsel(struct bttv *btv, unsigned int input);
static void gv800s_init(struct bttv *btv);
static void td3116_muxsel(struct bttv *btv, unsigned int input);
static int terratec_active_radio_upgrade(struct bttv *btv);
static int tea575x_init(struct bttv *btv);
static void identify_by_eeprom(struct bttv *btv,
unsigned char eeprom_data[256]);
static int pvr_boot(struct bttv *btv);
/* config variables */
static unsigned int triton1;
static unsigned int vsfx;
static unsigned int latency = UNSET;
static unsigned int card[BTTV_MAX] = { [ 0 ... (BTTV_MAX-1) ] = UNSET };
static unsigned int pll[BTTV_MAX] = { [ 0 ... (BTTV_MAX-1) ] = UNSET };
static unsigned int tuner[BTTV_MAX] = { [ 0 ... (BTTV_MAX-1) ] = UNSET };
static unsigned int svhs[BTTV_MAX] = { [ 0 ... (BTTV_MAX-1) ] = UNSET };
static unsigned int remote[BTTV_MAX] = { [ 0 ... (BTTV_MAX-1) ] = UNSET };
static unsigned int audiodev[BTTV_MAX];
static unsigned int saa6588[BTTV_MAX];
static struct bttv *master[BTTV_MAX] = { [ 0 ... (BTTV_MAX-1) ] = NULL };
static unsigned int autoload = UNSET;
static unsigned int gpiomask = UNSET;
static unsigned int audioall = UNSET;
static unsigned int audiomux[5] = { [ 0 ... 4 ] = UNSET };
/* insmod options */
module_param(triton1, int, 0444);
module_param(vsfx, int, 0444);
module_param(latency, int, 0444);
module_param(gpiomask, int, 0444);
module_param(audioall, int, 0444);
module_param(autoload, int, 0444);
module_param_array(card, int, NULL, 0444);
module_param_array(pll, int, NULL, 0444);
module_param_array(tuner, int, NULL, 0444);
module_param_array(svhs, int, NULL, 0444);
module_param_array(remote, int, NULL, 0444);
module_param_array(audiodev, int, NULL, 0444);
module_param_array(audiomux, int, NULL, 0444);
MODULE_PARM_DESC(triton1, "set ETBF pci config bit [enable bug compatibility for triton1 + others]");
MODULE_PARM_DESC(vsfx, "set VSFX pci config bit [yet another chipset flaw workaround]");
MODULE_PARM_DESC(latency,"pci latency timer");
MODULE_PARM_DESC(card,"specify TV/grabber card model, see CARDLIST file for a list");
MODULE_PARM_DESC(pll, "specify installed crystal (0=none, 28=28 MHz, 35=35 MHz, 14=14 MHz)");
MODULE_PARM_DESC(tuner,"specify installed tuner type");
MODULE_PARM_DESC(autoload, "obsolete option, please do not use anymore");
MODULE_PARM_DESC(audiodev, "specify audio device:\n"
"\t\t-1 = no audio\n"
"\t\t 0 = autodetect (default)\n"
"\t\t 1 = msp3400\n"
"\t\t 2 = tda7432\n"
"\t\t 3 = tvaudio");
MODULE_PARM_DESC(saa6588, "if 1, then load the saa6588 RDS module, default (0) is to use the card definition.");
/* I2C addresses list */
#define I2C_ADDR_TDA7432 0x8a
#define I2C_ADDR_MSP3400 0x80
#define I2C_ADDR_MSP3400_ALT 0x88
/* ----------------------------------------------------------------------- */
/* list of card IDs for bt878+ cards */
static struct CARD {
unsigned id;
int cardnr;
char *name;
} cards[] = {
{ 0x13eb0070, BTTV_BOARD_HAUPPAUGE878, "Hauppauge WinTV" },
{ 0x39000070, BTTV_BOARD_HAUPPAUGE878, "Hauppauge WinTV-D" },
{ 0x45000070, BTTV_BOARD_HAUPPAUGEPVR, "Hauppauge WinTV/PVR" },
{ 0xff000070, BTTV_BOARD_OSPREY1x0, "Osprey-100" },
{ 0xff010070, BTTV_BOARD_OSPREY2x0_SVID,"Osprey-200" },
{ 0xff020070, BTTV_BOARD_OSPREY500, "Osprey-500" },
{ 0xff030070, BTTV_BOARD_OSPREY2000, "Osprey-2000" },
{ 0xff040070, BTTV_BOARD_OSPREY540, "Osprey-540" },
{ 0xff070070, BTTV_BOARD_OSPREY440, "Osprey-440" },
{ 0x00011002, BTTV_BOARD_ATI_TVWONDER, "ATI TV Wonder" },
{ 0x00031002, BTTV_BOARD_ATI_TVWONDERVE,"ATI TV Wonder/VE" },
{ 0x6606107d, BTTV_BOARD_WINFAST2000, "Leadtek WinFast TV 2000" },
{ 0x6607107d, BTTV_BOARD_WINFASTVC100, "Leadtek WinFast VC 100" },
{ 0x6609107d, BTTV_BOARD_WINFAST2000, "Leadtek TV 2000 XP" },
{ 0x263610b4, BTTV_BOARD_STB2, "STB TV PCI FM, Gateway P/N 6000704" },
{ 0x264510b4, BTTV_BOARD_STB2, "STB TV PCI FM, Gateway P/N 6000704" },
{ 0x402010fc, BTTV_BOARD_GVBCTV3PCI, "I-O Data Co. GV-BCTV3/PCI" },
{ 0x405010fc, BTTV_BOARD_GVBCTV4PCI, "I-O Data Co. GV-BCTV4/PCI" },
{ 0x407010fc, BTTV_BOARD_GVBCTV5PCI, "I-O Data Co. GV-BCTV5/PCI" },
{ 0xd01810fc, BTTV_BOARD_GVBCTV5PCI, "I-O Data Co. GV-BCTV5/PCI" },
{ 0x001211bd, BTTV_BOARD_PINNACLE, "Pinnacle PCTV" },
/* some cards ship with byteswapped IDs ... */
{ 0x1200bd11, BTTV_BOARD_PINNACLE, "Pinnacle PCTV [bswap]" },
{ 0xff00bd11, BTTV_BOARD_PINNACLE, "Pinnacle PCTV [bswap]" },
/* this seems to happen as well ... */
{ 0xff1211bd, BTTV_BOARD_PINNACLE, "Pinnacle PCTV" },
{ 0x3000121a, BTTV_BOARD_VOODOOTV_200, "3Dfx VoodooTV 200" },
{ 0x263710b4, BTTV_BOARD_VOODOOTV_FM, "3Dfx VoodooTV FM" },
{ 0x3060121a, BTTV_BOARD_STB2, "3Dfx VoodooTV 100/ STB OEM" },
{ 0x3000144f, BTTV_BOARD_MAGICTVIEW063, "(Askey Magic/others) TView99 CPH06x" },
{ 0xa005144f, BTTV_BOARD_MAGICTVIEW063, "CPH06X TView99-Card" },
{ 0x3002144f, BTTV_BOARD_MAGICTVIEW061, "(Askey Magic/others) TView99 CPH05x" },
{ 0x3005144f, BTTV_BOARD_MAGICTVIEW061, "(Askey Magic/others) TView99 CPH061/06L (T1/LC)" },
{ 0x5000144f, BTTV_BOARD_MAGICTVIEW061, "Askey CPH050" },
{ 0x300014ff, BTTV_BOARD_MAGICTVIEW061, "TView 99 (CPH061)" },
{ 0x300214ff, BTTV_BOARD_PHOEBE_TVMAS, "Phoebe TV Master (CPH060)" },
{ 0x00011461, BTTV_BOARD_AVPHONE98, "AVerMedia TVPhone98" },
{ 0x00021461, BTTV_BOARD_AVERMEDIA98, "AVermedia TVCapture 98" },
{ 0x00031461, BTTV_BOARD_AVPHONE98, "AVerMedia TVPhone98" },
{ 0x00041461, BTTV_BOARD_AVERMEDIA98, "AVerMedia TVCapture 98" },
{ 0x03001461, BTTV_BOARD_AVERMEDIA98, "VDOMATE TV TUNER CARD" },
{ 0x1117153b, BTTV_BOARD_TERRATVALUE, "Terratec TValue (Philips PAL B/G)" },
{ 0x1118153b, BTTV_BOARD_TERRATVALUE, "Terratec TValue (Temic PAL B/G)" },
{ 0x1119153b, BTTV_BOARD_TERRATVALUE, "Terratec TValue (Philips PAL I)" },
{ 0x111a153b, BTTV_BOARD_TERRATVALUE, "Terratec TValue (Temic PAL I)" },
{ 0x1123153b, BTTV_BOARD_TERRATVRADIO, "Terratec TV Radio+" },
{ 0x1127153b, BTTV_BOARD_TERRATV, "Terratec TV+ (V1.05)" },
/* clashes with FlyVideo
*{ 0x18521852, BTTV_BOARD_TERRATV, "Terratec TV+ (V1.10)" }, */
{ 0x1134153b, BTTV_BOARD_TERRATVALUE, "Terratec TValue (LR102)" },
{ 0x1135153b, BTTV_BOARD_TERRATVALUER, "Terratec TValue Radio" }, /* LR102 */
{ 0x5018153b, BTTV_BOARD_TERRATVALUE, "Terratec TValue" }, /* ?? */
{ 0xff3b153b, BTTV_BOARD_TERRATVALUER, "Terratec TValue Radio" }, /* ?? */
{ 0x400015b0, BTTV_BOARD_ZOLTRIX_GENIE, "Zoltrix Genie TV" },
{ 0x400a15b0, BTTV_BOARD_ZOLTRIX_GENIE, "Zoltrix Genie TV" },
{ 0x400d15b0, BTTV_BOARD_ZOLTRIX_GENIE, "Zoltrix Genie TV / Radio" },
{ 0x401015b0, BTTV_BOARD_ZOLTRIX_GENIE, "Zoltrix Genie TV / Radio" },
{ 0x401615b0, BTTV_BOARD_ZOLTRIX_GENIE, "Zoltrix Genie TV / Radio" },
{ 0x1430aa00, BTTV_BOARD_PV143, "Provideo PV143A" },
{ 0x1431aa00, BTTV_BOARD_PV143, "Provideo PV143B" },
{ 0x1432aa00, BTTV_BOARD_PV143, "Provideo PV143C" },
{ 0x1433aa00, BTTV_BOARD_PV143, "Provideo PV143D" },
{ 0x1433aa03, BTTV_BOARD_PV143, "Security Eyes" },
{ 0x1460aa00, BTTV_BOARD_PV150, "Provideo PV150A-1" },
{ 0x1461aa01, BTTV_BOARD_PV150, "Provideo PV150A-2" },
{ 0x1462aa02, BTTV_BOARD_PV150, "Provideo PV150A-3" },
{ 0x1463aa03, BTTV_BOARD_PV150, "Provideo PV150A-4" },
{ 0x1464aa04, BTTV_BOARD_PV150, "Provideo PV150B-1" },
{ 0x1465aa05, BTTV_BOARD_PV150, "Provideo PV150B-2" },
{ 0x1466aa06, BTTV_BOARD_PV150, "Provideo PV150B-3" },
{ 0x1467aa07, BTTV_BOARD_PV150, "Provideo PV150B-4" },
{ 0xa132ff00, BTTV_BOARD_IVC100, "IVC-100" },
{ 0xa1550000, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550001, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550002, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550003, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550100, BTTV_BOARD_IVC200, "IVC-200G" },
{ 0xa1550101, BTTV_BOARD_IVC200, "IVC-200G" },
{ 0xa1550102, BTTV_BOARD_IVC200, "IVC-200G" },
{ 0xa1550103, BTTV_BOARD_IVC200, "IVC-200G" },
{ 0xa1550800, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550801, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550802, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa1550803, BTTV_BOARD_IVC200, "IVC-200" },
{ 0xa182ff00, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff01, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff02, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff03, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff04, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff05, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff06, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff07, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff08, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff09, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff0a, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff0b, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff0c, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff0d, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff0e, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xa182ff0f, BTTV_BOARD_IVC120, "IVC-120G" },
{ 0xf0500000, BTTV_BOARD_IVCE8784, "IVCE-8784" },
{ 0xf0500001, BTTV_BOARD_IVCE8784, "IVCE-8784" },
{ 0xf0500002, BTTV_BOARD_IVCE8784, "IVCE-8784" },
{ 0xf0500003, BTTV_BOARD_IVCE8784, "IVCE-8784" },
{ 0x41424344, BTTV_BOARD_GRANDTEC, "GrandTec Multi Capture" },
{ 0x01020304, BTTV_BOARD_XGUARD, "Grandtec Grand X-Guard" },
{ 0x18501851, BTTV_BOARD_CHRONOS_VS2, "FlyVideo 98 (LR50)/ Chronos Video Shuttle II" },
{ 0xa0501851, BTTV_BOARD_CHRONOS_VS2, "FlyVideo 98 (LR50)/ Chronos Video Shuttle II" },
{ 0x18511851, BTTV_BOARD_FLYVIDEO98EZ, "FlyVideo 98EZ (LR51)/ CyberMail AV" },
{ 0x18521852, BTTV_BOARD_TYPHOON_TVIEW, "FlyVideo 98FM (LR50)/ Typhoon TView TV/FM Tuner" },
{ 0x41a0a051, BTTV_BOARD_FLYVIDEO_98FM, "Lifeview FlyVideo 98 LR50 Rev Q" },
{ 0x18501f7f, BTTV_BOARD_FLYVIDEO_98, "Lifeview Flyvideo 98" },
{ 0x010115cb, BTTV_BOARD_GMV1, "AG GMV1" },
{ 0x010114c7, BTTV_BOARD_MODTEC_205, "Modular Technology MM201/MM202/MM205/MM210/MM215 PCTV" },
{ 0x10b42636, BTTV_BOARD_HAUPPAUGE878, "STB ???" },
{ 0x217d6606, BTTV_BOARD_WINFAST2000, "Leadtek WinFast TV 2000" },
{ 0xfff6f6ff, BTTV_BOARD_WINFAST2000, "Leadtek WinFast TV 2000" },
{ 0x03116000, BTTV_BOARD_SENSORAY311_611, "Sensoray 311" },
{ 0x06116000, BTTV_BOARD_SENSORAY311_611, "Sensoray 611" },
{ 0x00790e11, BTTV_BOARD_WINDVR, "Canopus WinDVR PCI" },
{ 0xa0fca1a0, BTTV_BOARD_ZOLTRIX, "Face to Face Tvmax" },
{ 0x82b2aa6a, BTTV_BOARD_SIMUS_GVC1100, "SIMUS GVC1100" },
{ 0x146caa0c, BTTV_BOARD_PV951, "ituner spectra8" },
{ 0x200a1295, BTTV_BOARD_PXC200, "ImageNation PXC200A" },
{ 0x40111554, BTTV_BOARD_PV_BT878P_9B, "Prolink Pixelview PV-BT" },
{ 0x17de0a01, BTTV_BOARD_KWORLD, "Mecer TV/FM/Video Tuner" },
{ 0x01051805, BTTV_BOARD_PICOLO_TETRA_CHIP, "Picolo Tetra Chip #1" },
{ 0x01061805, BTTV_BOARD_PICOLO_TETRA_CHIP, "Picolo Tetra Chip #2" },
{ 0x01071805, BTTV_BOARD_PICOLO_TETRA_CHIP, "Picolo Tetra Chip #3" },
{ 0x01081805, BTTV_BOARD_PICOLO_TETRA_CHIP, "Picolo Tetra Chip #4" },
{ 0x15409511, BTTV_BOARD_ACORP_Y878F, "Acorp Y878F" },
{ 0x53534149, BTTV_BOARD_SSAI_SECURITY, "SSAI Security Video Interface" },
{ 0x5353414a, BTTV_BOARD_SSAI_ULTRASOUND, "SSAI Ultrasound Video Interface" },
/* likely broken, vendor id doesn't match the other magic views ...
* { 0xa0fca04f, BTTV_BOARD_MAGICTVIEW063, "Guillemot Maxi TV Video 3" }, */
/* Duplicate PCI ID, reconfigure for this board during the eeprom read.
* { 0x13eb0070, BTTV_BOARD_HAUPPAUGE_IMPACTVCB, "Hauppauge ImpactVCB" }, */
{ 0x109e036e, BTTV_BOARD_CONCEPTRONIC_CTVFMI2, "Conceptronic CTVFMi v2"},
/* DVB cards (using pci function .1 for mpeg data xfer) */
{ 0x001c11bd, BTTV_BOARD_PINNACLESAT, "Pinnacle PCTV Sat" },
{ 0x01010071, BTTV_BOARD_NEBULA_DIGITV, "Nebula Electronics DigiTV" },
{ 0x20007063, BTTV_BOARD_PC_HDTV, "pcHDTV HD-2000 TV"},
{ 0x002611bd, BTTV_BOARD_TWINHAN_DST, "Pinnacle PCTV SAT CI" },
{ 0x00011822, BTTV_BOARD_TWINHAN_DST, "Twinhan VisionPlus DVB" },
{ 0xfc00270f, BTTV_BOARD_TWINHAN_DST, "ChainTech digitop DST-1000 DVB-S" },
{ 0x07711461, BTTV_BOARD_AVDVBT_771, "AVermedia AverTV DVB-T 771" },
{ 0x07611461, BTTV_BOARD_AVDVBT_761, "AverMedia AverTV DVB-T 761" },
{ 0xdb1018ac, BTTV_BOARD_DVICO_DVBT_LITE, "DViCO FusionHDTV DVB-T Lite" },
{ 0xdb1118ac, BTTV_BOARD_DVICO_DVBT_LITE, "Ultraview DVB-T Lite" },
{ 0xd50018ac, BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE, "DViCO FusionHDTV 5 Lite" },
{ 0x00261822, BTTV_BOARD_TWINHAN_DST, "DNTV Live! Mini "},
{ 0xd200dbc0, BTTV_BOARD_DVICO_FUSIONHDTV_2, "DViCO FusionHDTV 2" },
{ 0x763c008a, BTTV_BOARD_GEOVISION_GV600, "GeoVision GV-600" },
{ 0x18011000, BTTV_BOARD_ENLTV_FM_2, "Encore ENL TV-FM-2" },
{ 0x763d800a, BTTV_BOARD_GEOVISION_GV800S, "GeoVision GV-800(S) (master)" },
{ 0x763d800b, BTTV_BOARD_GEOVISION_GV800S_SL, "GeoVision GV-800(S) (slave)" },
{ 0x763d800c, BTTV_BOARD_GEOVISION_GV800S_SL, "GeoVision GV-800(S) (slave)" },
{ 0x763d800d, BTTV_BOARD_GEOVISION_GV800S_SL, "GeoVision GV-800(S) (slave)" },
{ 0x15401830, BTTV_BOARD_PV183, "Provideo PV183-1" },
{ 0x15401831, BTTV_BOARD_PV183, "Provideo PV183-2" },
{ 0x15401832, BTTV_BOARD_PV183, "Provideo PV183-3" },
{ 0x15401833, BTTV_BOARD_PV183, "Provideo PV183-4" },
{ 0x15401834, BTTV_BOARD_PV183, "Provideo PV183-5" },
{ 0x15401835, BTTV_BOARD_PV183, "Provideo PV183-6" },
{ 0x15401836, BTTV_BOARD_PV183, "Provideo PV183-7" },
{ 0x15401837, BTTV_BOARD_PV183, "Provideo PV183-8" },
{ 0x3116f200, BTTV_BOARD_TVT_TD3116, "Tongwei Video Technology TD-3116" },
{ 0x02280279, BTTV_BOARD_APOSONIC_WDVR, "Aposonic W-DVR" },
{ 0, -1, NULL }
};
/* ----------------------------------------------------------------------- */
/* array with description for bt848 / bt878 tv/grabber cards */
struct tvcard bttv_tvcards[] = {
/* ---- card 0x00 ---------------------------------- */
[BTTV_BOARD_UNKNOWN] = {
.name = " *** UNKNOWN/GENERIC *** ",
.video_inputs = 4,
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1, 0),
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MIRO] = {
.name = "MIRO PCTV",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 2, 0, 0, 0 },
.gpiomute = 10,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_HAUPPAUGE] = {
.name = "Hauppauge (bt848)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 3 },
.gpiomute = 4,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_STB] = {
.name = "STB, Gateway P/N 6000699 (bt848)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 4, 0, 2, 3 },
.gpiomute = 1,
.no_msp34xx = 1,
.tuner_type = TUNER_PHILIPS_NTSC,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
},
/* ---- card 0x04 ---------------------------------- */
[BTTV_BOARD_INTEL] = {
.name = "Intel Create and Share PCI/ Smart Video Recorder III",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 2,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0 },
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_DIAMOND] = {
.name = "Diamond DTV2000",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 3,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 0, 1, 0, 1 },
.gpiomute = 3,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_AVERMEDIA] = {
.name = "AVerMedia TVPhone",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 3,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomask = 0x0f,
.gpiomux = { 0x0c, 0x04, 0x08, 0x04 },
/* 0x04 for some cards ?? */
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= avermedia_tvphone_audio,
.has_remote = 1,
},
[BTTV_BOARD_MATRIX_VISION] = {
.name = "MATRIX-Vision MV-Delta",
.video_inputs = 5,
/* .audio_inputs= 1, */
.svhs = 3,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 0, 0),
.gpiomux = { 0 },
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x08 ---------------------------------- */
[BTTV_BOARD_FLYVIDEO] = {
.name = "Lifeview FlyVideo II (Bt848) LR26 / MAXI TV Video PCI2 LR26",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xc00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0xc00, 0x800, 0x400 },
.gpiomute = 0xc00,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_TURBOTV] = {
.name = "IMS/IXmicro TurboTV",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 3,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 1, 1, 2, 3 },
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_PAL,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_HAUPPAUGE878] = {
.name = "Hauppauge (bt878)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x0f, /* old: 7 */
.muxsel = MUXSEL(2, 0, 1, 1),
.gpiomux = { 0, 1, 2, 3 },
.gpiomute = 4,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MIROPRO] = {
.name = "MIRO PCTV pro",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x3014f,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x20001,0x10001, 0, 0 },
.gpiomute = 10,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x0c ---------------------------------- */
[BTTV_BOARD_ADSTECH_TV] = {
.name = "ADS Technologies Channel Surfer TV (bt848)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 13, 14, 11, 7 },
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_AVERMEDIA98] = {
.name = "AVerMedia TVCapture 98",
.video_inputs = 3,
/* .audio_inputs= 4, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 13, 14, 11, 7 },
.msp34xx_alt = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= avermedia_tv_stereo_audio,
.no_gpioirq = 1,
},
[BTTV_BOARD_VHX] = {
.name = "Aimslab Video Highway Xtreme (VHX)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 2, 1, 3 }, /* old: {0, 1, 2, 3, 4} */
.gpiomute = 4,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_ZOLTRIX] = {
.name = "Zoltrix TV-Max",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0, 1, 0 },
.gpiomute = 10,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x10 ---------------------------------- */
[BTTV_BOARD_PIXVIEWPLAYTV] = {
.name = "Prolink Pixelview PlayTV (bt878)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x01fe00,
.muxsel = MUXSEL(2, 3, 1, 1),
/* 2003-10-20 by "Anton A. Arapov" <[email protected]> */
.gpiomux = { 0x001e00, 0, 0x018000, 0x014000 },
.gpiomute = 0x002000,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_WINVIEW_601] = {
.name = "Leadtek WinView 601",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x8300f8,
.muxsel = MUXSEL(2, 3, 1, 1, 0),
.gpiomux = { 0x4fa007,0xcfa007,0xcfa007,0xcfa007 },
.gpiomute = 0xcfa007,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.volume_gpio = winview_volume,
.has_radio = 1,
},
[BTTV_BOARD_AVEC_INTERCAP] = {
.name = "AVEC Intercapture",
.video_inputs = 3,
/* .audio_inputs= 2, */
.svhs = 2,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 1, 0, 0, 0 },
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_LIFE_FLYKIT] = {
.name = "Lifeview FlyVideo II EZ /FlyKit LR38 Bt848 (capture only)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 0x8dff00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0 },
.no_msp34xx = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x14 ---------------------------------- */
[BTTV_BOARD_CEI_RAFFLES] = {
.name = "CEI Raffles Card",
.video_inputs = 3,
/* .audio_inputs= 3, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1, 1),
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_CONFERENCETV] = {
.name = "Lifeview FlyVideo 98/ Lucky Star Image World ConferenceTV LR50",
.video_inputs = 4,
/* .audio_inputs= 2, tuner, line in */
.svhs = 2,
.gpiomask = 0x1800,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0x800, 0x1000, 0x1000 },
.gpiomute = 0x1800,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_PHOEBE_TVMAS] = {
.name = "Askey CPH050/ Phoebe Tv Master + FM",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xc00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 0x800, 0x400 },
.gpiomute = 0xc00,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MODTEC_205] = {
.name = "Modular Technology MM201/MM202/MM205/MM210/MM215 PCTV, bt878",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.has_dig_in = 1,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 0), /* input 2 is digital */
/* .digital_mode= DIGITAL_MODE_CAMERA, */
.gpiomux = { 0, 0, 0, 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ALPS_TSBB5_PAL_I,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x18 ---------------------------------- */
[BTTV_BOARD_MAGICTVIEW061] = {
.name = "Askey CPH05X/06X (bt878) [many vendors]",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xe00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = {0x400, 0x400, 0x400, 0x400 },
.gpiomute = 0xc00,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
.has_radio = 1, /* not every card has radio */
},
[BTTV_BOARD_VOBIS_BOOSTAR] = {
.name = "Terratec TerraTV+ Version 1.0 (Bt848)/ Terra TValue Version 1.0/ Vobis TV-Boostar",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x1f0fff,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x20000, 0x30000, 0x10000, 0 },
.gpiomute = 0x40000,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= terratv_audio,
},
[BTTV_BOARD_HAUPPAUG_WCAM] = {
.name = "Hauppauge WinCam newer (bt878)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 3,
.gpiomask = 7,
.muxsel = MUXSEL(2, 0, 1, 1),
.gpiomux = { 0, 1, 2, 3 },
.gpiomute = 4,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MAXI] = {
.name = "Lifeview FlyVideo 98/ MAXI TV Video PCI2 LR50",
.video_inputs = 4,
/* .audio_inputs= 2, */
.svhs = 2,
.gpiomask = 0x1800,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0x800, 0x1000, 0x1000 },
.gpiomute = 0x1800,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_SECAM,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x1c ---------------------------------- */
[BTTV_BOARD_TERRATV] = {
.name = "Terratec TerraTV+ Version 1.1 (bt878)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x1f0fff,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x20000, 0x30000, 0x10000, 0x00000 },
.gpiomute = 0x40000,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= terratv_audio,
/* GPIO wiring:
External 20 pin connector (for Active Radio Upgrade board)
gpio00: i2c-sda
gpio01: i2c-scl
gpio02: om5610-data
gpio03: om5610-clk
gpio04: om5610-wre
gpio05: om5610-stereo
gpio06: rds6588-davn
gpio07: Pin 7 n.c.
gpio08: nIOW
gpio09+10: nIOR, nSEL ?? (bt878)
gpio09: nIOR (bt848)
gpio10: nSEL (bt848)
Sound Routing:
gpio16: u2-A0 (1st 4052bt)
gpio17: u2-A1
gpio18: u2-nEN
gpio19: u4-A0 (2nd 4052)
gpio20: u4-A1
u4-nEN - GND
Btspy:
00000 : Cdrom (internal audio input)
10000 : ext. Video audio input
20000 : TV Mono
a0000 : TV Mono/2
1a0000 : TV Stereo
30000 : Radio
40000 : Mute
*/
},
[BTTV_BOARD_PXC200] = {
/* Jannik Fritsch <[email protected]> */
.name = "Imagenation PXC200",
.video_inputs = 5,
/* .audio_inputs= 1, */
.svhs = 1, /* was: 4 */
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 0, 0),
.gpiomux = { 0 },
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.muxsel_hook = PXC200_muxsel,
},
[BTTV_BOARD_FLYVIDEO_98] = {
.name = "Lifeview FlyVideo 98 LR50",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x1800, /* 0x8dfe00 */
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0x0800, 0x1000, 0x1000 },
.gpiomute = 0x1800,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_IPROTV] = {
.name = "Formac iProTV, Formac ProTV I (bt848)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 3,
.gpiomask = 1,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 1, 0, 0, 0 },
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x20 ---------------------------------- */
[BTTV_BOARD_INTEL_C_S_PCI] = {
.name = "Intel Create and Share PCI/ Smart Video Recorder III",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 2,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0 },
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_TERRATVALUE] = {
.name = "Terratec TerraTValue Version Bt878",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xffff00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x500, 0, 0x300, 0x900 },
.gpiomute = 0x900,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_WINFAST2000] = {
.name = "Leadtek WinFast 2000/ WinFast 2000 XP",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
/* TV, CVid, SVid, CVid over SVid connector */
.muxsel = MUXSEL(2, 3, 1, 1, 0),
/* Alexander Varakin <[email protected]> [stereo version] */
.gpiomask = 0xb33000,
.gpiomux = { 0x122000,0x1000,0x0000,0x620000 },
.gpiomute = 0x800000,
/* Audio Routing for "WinFast 2000 XP" (no tv stereo !)
gpio23 -- hef4052:nEnable (0x800000)
gpio12 -- hef4052:A1
gpio13 -- hef4052:A0
0x0000: external audio
0x1000: FM
0x2000: TV
0x3000: n.c.
Note: There exists another variant "Winfast 2000" with tv stereo !?
Note: eeprom only contains FF and pci subsystem id 107d:6606
*/
.pll = PLL_28,
.has_radio = 1,
.tuner_type = TUNER_PHILIPS_PAL, /* default for now, gpio reads BFFF06 for Pal bg+dk */
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= winfast2000_audio,
.has_remote = 1,
},
[BTTV_BOARD_CHRONOS_VS2] = {
.name = "Lifeview FlyVideo 98 LR50 / Chronos Video Shuttle II",
.video_inputs = 4,
/* .audio_inputs= 3, */
.svhs = 2,
.gpiomask = 0x1800,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0x800, 0x1000, 0x1000 },
.gpiomute = 0x1800,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x24 ---------------------------------- */
[BTTV_BOARD_TYPHOON_TVIEW] = {
.name = "Lifeview FlyVideo 98FM LR50 / Typhoon TView TV/FM Tuner",
.video_inputs = 4,
/* .audio_inputs= 3, */
.svhs = 2,
.gpiomask = 0x1800,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0x800, 0x1000, 0x1000 },
.gpiomute = 0x1800,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
},
[BTTV_BOARD_PXELVWPLTVPRO] = {
.name = "Prolink PixelView PlayTV pro",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xff,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x21, 0x20, 0x24, 0x2c },
.gpiomute = 0x29,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MAGICTVIEW063] = {
.name = "Askey CPH06X TView99",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x551e00,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 0x551400, 0x551200, 0, 0 },
.gpiomute = 0x551c00,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
},
[BTTV_BOARD_PINNACLE] = {
.name = "Pinnacle PCTV Studio/Rave",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x03000F,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 2, 0xd0001, 0, 0 },
.gpiomute = 1,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x28 ---------------------------------- */
[BTTV_BOARD_STB2] = {
.name = "STB TV PCI FM, Gateway P/N 6000704 (bt878), 3Dfx VoodooTV 100",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 4, 0, 2, 3 },
.gpiomute = 1,
.no_msp34xx = 1,
.tuner_type = TUNER_PHILIPS_NTSC,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
},
[BTTV_BOARD_AVPHONE98] = {
.name = "AVerMedia TVPhone 98",
.video_inputs = 3,
/* .audio_inputs= 4, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 13, 4, 11, 7 },
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
.audio_mode_gpio= avermedia_tvphone_audio,
},
[BTTV_BOARD_PV951] = {
.name = "ProVideo PV951", /* pic16c54 */
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0, 0, 0},
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_ONAIR_TV] = {
.name = "Little OnAir TV",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xe00b,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0xff9ff6, 0xff9ff6, 0xff1ff7, 0 },
.gpiomute = 0xff3ffc,
.no_msp34xx = 1,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x2c ---------------------------------- */
[BTTV_BOARD_SIGMA_TVII_FM] = {
.name = "Sigma TVII-FM",
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 3,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 1, 1, 0, 2 },
.gpiomute = 3,
.no_msp34xx = 1,
.pll = PLL_NONE,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MATRIX_VISION2] = {
.name = "MATRIX-Vision MV-Delta 2",
.video_inputs = 5,
/* .audio_inputs= 1, */
.svhs = 3,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 0, 0),
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_ZOLTRIX_GENIE] = {
.name = "Zoltrix Genie TV/FM",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xbcf03f,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0xbc803f, 0xbc903f, 0xbcb03f, 0 },
.gpiomute = 0xbcb03f,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_4039FR5_NTSC,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_TERRATVRADIO] = {
.name = "Terratec TV/Radio+",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x70000,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x20000, 0x30000, 0x10000, 0 },
.gpiomute = 0x40000,
.no_msp34xx = 1,
.pll = PLL_35,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
},
/* ---- card 0x30 ---------------------------------- */
[BTTV_BOARD_DYNALINK] = {
.name = "Askey CPH03x/ Dynalink Magic TView",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = {2,0,0,0 },
.gpiomute = 1,
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_GVBCTV3PCI] = {
.name = "IODATA GV-BCTV3/PCI",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x010f00,
.muxsel = MUXSEL(2, 3, 0, 0),
.gpiomux = {0x10000, 0, 0x10000, 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ALPS_TSHC6_NTSC,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= gvbctv3pci_audio,
},
[BTTV_BOARD_PXELVWPLTVPAK] = {
.name = "Prolink PV-BT878P+4E / PixelView PlayTV PAK / Lenco MXTV-9578 CP",
.video_inputs = 5,
/* .audio_inputs= 1, */
.svhs = 3,
.has_dig_in = 1,
.gpiomask = 0xAA0000,
.muxsel = MUXSEL(2, 3, 1, 1, 0), /* in 4 is digital */
/* .digital_mode= DIGITAL_MODE_CAMERA, */
.gpiomux = { 0x20000, 0, 0x80000, 0x80000 },
.gpiomute = 0xa8000,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
/* GPIO wiring: (different from Rev.4C !)
GPIO17: U4.A0 (first hef4052bt)
GPIO19: U4.A1
GPIO20: U5.A1 (second hef4052bt)
GPIO21: U4.nEN
GPIO22: BT832 Reset Line
GPIO23: A5,A0, U5,nEN
Note: At i2c=0x8a is a Bt832 chip, which changes to 0x88 after being reset via GPIO22
*/
},
[BTTV_BOARD_EAGLE] = {
.name = "Eagle Wireless Capricorn2 (bt878A)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 7,
.muxsel = MUXSEL(2, 0, 1, 1),
.gpiomux = { 0, 1, 2, 3 },
.gpiomute = 4,
.pll = PLL_28,
.tuner_type = UNSET /* TUNER_ALPS_TMDH2_NTSC */,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x34 ---------------------------------- */
[BTTV_BOARD_PINNACLEPRO] = {
/* David Härdeman <[email protected]> */
.name = "Pinnacle PCTV Studio Pro",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 3,
.gpiomask = 0x03000F,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 1, 0xd0001, 0, 0 },
.gpiomute = 10,
/* sound path (5 sources):
MUX1 (mask 0x03), Enable Pin 0x08 (0=enable, 1=disable)
0= ext. Audio IN
1= from MUX2
2= Mono TV sound from Tuner
3= not connected
MUX2 (mask 0x30000):
0,2,3= from MSP34xx
1= FM stereo Radio from Tuner */
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_TVIEW_RDS_FM] = {
/* Claas Langbehn <[email protected]>,
Sven Grothklags <[email protected]> */
.name = "Typhoon TView RDS + FM Stereo / KNC1 TV Station RDS",
.video_inputs = 4,
/* .audio_inputs= 3, */
.svhs = 2,
.gpiomask = 0x1c,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0, 0x10, 8 },
.gpiomute = 4,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
},
[BTTV_BOARD_LIFETEC_9415] = {
/* Tim Röstermundt <[email protected]>
in de.comp.os.unix.linux.hardware:
options bttv card=0 pll=1 radio=1 gpiomask=0x18e0
gpiomux =0x44c71f,0x44d71f,0,0x44d71f,0x44dfff
options tuner type=5 */
.name = "Lifeview FlyVideo 2000 /FlyVideo A2/ Lifetec LT 9415 TV [LR90]",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x18e0,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x0000,0x0800,0x1000,0x1000 },
.gpiomute = 0x18e0,
/* For cards with tda9820/tda9821:
0x0000: Tuner normal stereo
0x0080: Tuner A2 SAP (second audio program = Zweikanalton)
0x0880: Tuner A2 stereo */
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_BESTBUY_EASYTV] = {
/* Miguel Angel Alvarez <[email protected]>
old Easy TV BT848 version (model CPH031) */
.name = "Askey CPH031/ BESTBUY Easy TV",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xF,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 2, 0, 0, 0 },
.gpiomute = 10,
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_PAL,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x38 ---------------------------------- */
[BTTV_BOARD_FLYVIDEO_98FM] = {
/* Gordon Heydon <[email protected] ('98) */
.name = "Lifeview FlyVideo 98FM LR50",
.video_inputs = 4,
/* .audio_inputs= 3, */
.svhs = 2,
.gpiomask = 0x1800,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0x800, 0x1000, 0x1000 },
.gpiomute = 0x1800,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
},
/* This is the ultimate cheapo capture card
* just a BT848A on a small PCB!
* Steve Hosgood <[email protected]> */
[BTTV_BOARD_GRANDTEC] = {
.name = "GrandTec 'Grand Video Capture' (Bt848)",
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = 1,
.gpiomask = 0,
.muxsel = MUXSEL(3, 1),
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_35,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_ASKEY_CPH060] = {
/* Daniel Herrington <[email protected]> */
.name = "Askey CPH060/ Phoebe TV Master Only (No FM)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xe00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x400, 0x400, 0x400, 0x400 },
.gpiomute = 0x800,
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_4036FY5_NTSC,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_ASKEY_CPH03X] = {
/* Matti Mottus <[email protected]> */
.name = "Askey CPH03x TV Capturer",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x03000F,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 2, 0, 0, 0 },
.gpiomute = 1,
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_PAL,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
},
/* ---- card 0x3c ---------------------------------- */
[BTTV_BOARD_MM100PCTV] = {
/* Philip Blundell <[email protected]> */
.name = "Modular Technology MM100PCTV",
.video_inputs = 2,
/* .audio_inputs= 2, */
.svhs = NO_SVHS,
.gpiomask = 11,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 2, 0, 0, 1 },
.gpiomute = 8,
.pll = PLL_35,
.tuner_type = TUNER_TEMIC_PAL,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_GMV1] = {
/* Adrian Cox <[email protected] */
.name = "AG Electronics GMV1",
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = 1,
.gpiomask = 0xF,
.muxsel = MUXSEL(2, 2),
.gpiomux = { },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_BESTBUY_EASYTV2] = {
/* Miguel Angel Alvarez <[email protected]>
new Easy TV BT878 version (model CPH061)
special thanks to Informatica Mieres for providing the card */
.name = "Askey CPH061/ BESTBUY Easy TV (bt878)",
.video_inputs = 3,
/* .audio_inputs= 2, */
.svhs = 2,
.gpiomask = 0xFF,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 1, 0, 4, 4 },
.gpiomute = 9,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_ATI_TVWONDER] = {
/* Lukas Gebauer <[email protected]> */
.name = "ATI TV-Wonder",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xf03f,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 0xbffe, 0, 0xbfff, 0 },
.gpiomute = 0xbffe,
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_4006FN5_MULTI_PAL,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x40 ---------------------------------- */
[BTTV_BOARD_ATI_TVWONDERVE] = {
/* Lukas Gebauer <[email protected]> */
.name = "ATI TV-Wonder VE",
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 1,
.muxsel = MUXSEL(2, 3, 0, 1),
.gpiomux = { 0, 0, 1, 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_TEMIC_4006FN5_MULTI_PAL,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_FLYVIDEO2000] = {
/* DeeJay <[email protected] (2000S) */
.name = "Lifeview FlyVideo 2000S LR90",
.video_inputs = 3,
/* .audio_inputs= 3, */
.svhs = 2,
.gpiomask = 0x18e0,
.muxsel = MUXSEL(2, 3, 0, 1),
/* Radio changed from 1e80 to 0x800 to make
FlyVideo2000S in .hu happy (gm)*/
/* -dk-???: set mute=0x1800 for tda9874h daughterboard */
.gpiomux = { 0x0000,0x0800,0x1000,0x1000 },
.gpiomute = 0x1800,
.audio_mode_gpio= fv2000s_audio,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_TERRATVALUER] = {
.name = "Terratec TValueRadio",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0xffff00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x500, 0x500, 0x300, 0x900 },
.gpiomute = 0x900,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
},
[BTTV_BOARD_GVBCTV4PCI] = {
/* TANAKA Kei <[email protected]> */
.name = "IODATA GV-BCTV4/PCI",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x010f00,
.muxsel = MUXSEL(2, 3, 0, 0),
.gpiomux = {0x10000, 0, 0x10000, 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_SHARP_2U5JF5540_NTSC,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= gvbctv3pci_audio,
},
/* ---- card 0x44 ---------------------------------- */
[BTTV_BOARD_VOODOOTV_FM] = {
.name = "3Dfx VoodooTV FM (Euro)",
/* try "insmod msp3400 simple=0" if you have
* sound problems with this card. */
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 0x4f8a00,
/* 0x100000: 1=MSP enabled (0=disable again)
* 0x010000: Connected to "S0" on tda9880 (0=Pal/BG, 1=NTSC) */
.gpiomux = {0x947fff, 0x987fff,0x947fff,0x947fff },
.gpiomute = 0x947fff,
/* tvtuner, radio, external,internal, mute, stereo
* tuner, Composite, SVid, Composite-on-Svid-adapter */
.muxsel = MUXSEL(2, 3, 0, 1),
.tuner_type = TUNER_MT2032,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
},
[BTTV_BOARD_VOODOOTV_200] = {
.name = "VoodooTV 200 (USA)",
/* try "insmod msp3400 simple=0" if you have
* sound problems with this card. */
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 0x4f8a00,
/* 0x100000: 1=MSP enabled (0=disable again)
* 0x010000: Connected to "S0" on tda9880 (0=Pal/BG, 1=NTSC) */
.gpiomux = {0x947fff, 0x987fff,0x947fff,0x947fff },
.gpiomute = 0x947fff,
/* tvtuner, radio, external,internal, mute, stereo
* tuner, Composite, SVid, Composite-on-Svid-adapter */
.muxsel = MUXSEL(2, 3, 0, 1),
.tuner_type = TUNER_MT2032,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
},
[BTTV_BOARD_AIMMS] = {
/* Philip Blundell <[email protected]> */
.name = "Active Imaging AIMMS",
.video_inputs = 1,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.muxsel = MUXSEL(2),
.gpiomask = 0
},
[BTTV_BOARD_PV_BT878P_PLUS] = {
/* Tomasz Pyra <[email protected]> */
.name = "Prolink Pixelview PV-BT878P+ (Rev.4C,8E)",
.video_inputs = 3,
/* .audio_inputs= 4, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0, 11, 7 }, /* TV and Radio with same GPIO ! */
.gpiomute = 13,
.pll = PLL_28,
.tuner_type = TUNER_LG_PAL_I_FM,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
/* GPIO wiring:
GPIO0: U4.A0 (hef4052bt)
GPIO1: U4.A1
GPIO2: U4.A1 (second hef4052bt)
GPIO3: U4.nEN, U5.A0, A5.nEN
GPIO8-15: vrd866b ?
*/
},
[BTTV_BOARD_FLYVIDEO98EZ] = {
.name = "Lifeview FlyVideo 98EZ (capture only) LR51",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 2,
/* AV1, AV2, SVHS, CVid adapter on SVHS */
.muxsel = MUXSEL(2, 3, 1, 1),
.pll = PLL_28,
.no_msp34xx = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x48 ---------------------------------- */
[BTTV_BOARD_PV_BT878P_9B] = {
/* Dariusz Kowalewski <[email protected]> */
.name = "Prolink Pixelview PV-BT878P+9B (PlayTV Pro rev.9B FM+NICAM)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x3f,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x01, 0x00, 0x03, 0x03 },
.gpiomute = 0x09,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= pvbt878p9b_audio, /* Note: not all cards have stereo */
.has_radio = 1, /* Note: not all cards have radio */
.has_remote = 1,
/* GPIO wiring:
GPIO0: A0 hef4052
GPIO1: A1 hef4052
GPIO3: nEN hef4052
GPIO8-15: vrd866b
GPIO20,22,23: R30,R29,R28
*/
},
[BTTV_BOARD_SENSORAY311_611] = {
/* Clay Kunz <[email protected]> */
/* you must jumper JP5 for the 311 card (PC/104+) to work */
.name = "Sensoray 311/611",
.video_inputs = 5,
/* .audio_inputs= 0, */
.svhs = 4,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 0, 0),
.gpiomux = { 0 },
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_RV605] = {
/* Miguel Freitas <[email protected]> */
.name = "RemoteVision MX (RV605)",
.video_inputs = 16,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0x00,
.gpiomask2 = 0x07ff,
.muxsel = MUXSEL(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
.no_msp34xx = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.muxsel_hook = rv605_muxsel,
},
[BTTV_BOARD_POWERCLR_MTV878] = {
.name = "Powercolor MTV878/ MTV878R/ MTV878F",
.video_inputs = 3,
/* .audio_inputs= 2, */
.svhs = 2,
.gpiomask = 0x1C800F, /* Bit0-2: Audio select, 8-12:remote control 14:remote valid 15:remote reset */
.muxsel = MUXSEL(2, 1, 1),
.gpiomux = { 0, 1, 2, 2 },
.gpiomute = 4,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
},
/* ---- card 0x4c ---------------------------------- */
[BTTV_BOARD_WINDVR] = {
/* Masaki Suzuki <[email protected]> */
.name = "Canopus WinDVR PCI (COMPAQ Presario 3524JP, 5112JP)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x140007,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 3 },
.gpiomute = 4,
.tuner_type = TUNER_PHILIPS_NTSC,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= windvr_audio,
},
[BTTV_BOARD_GRANDTEC_MULTI] = {
.name = "GrandTec Multi Capture Card (Bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_KWORLD] = {
.name = "Jetway TV/Capture JW-TV878-FBK, Kworld KW-TV878RF",
.video_inputs = 4,
/* .audio_inputs= 3, */
.svhs = 2,
.gpiomask = 7,
/* Tuner, SVid, SVHS, SVid to SVHS connector */
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0, 4, 4 },/* Yes, this tuner uses the same audio output for TV and FM radio!
* This card lacks external Audio In, so we mute it on Ext. & Int.
* The PCB can take a sbx1637/sbx1673, wiring unknown.
* This card lacks PCI subsystem ID, sigh.
* gpiomux =1: lower volume, 2+3: mute
* btwincap uses 0x80000/0x80003
*/
.gpiomute = 4,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
/* Samsung TCPA9095PC27A (BG+DK), philips compatible, w/FM, stereo and
radio signal strength indicators work fine. */
.has_radio = 1,
/* GPIO Info:
GPIO0,1: HEF4052 A0,A1
GPIO2: HEF4052 nENABLE
GPIO3-7: n.c.
GPIO8-13: IRDC357 data0-5 (data6 n.c. ?) [chip not present on my card]
GPIO14,15: ??
GPIO16-21: n.c.
GPIO22,23: ??
?? : mtu8b56ep microcontroller for IR (GPIO wiring unknown)*/
},
[BTTV_BOARD_DSP_TCVIDEO] = {
/* Arthur Tetzlaff-Deas, DSP Design Ltd <[email protected]> */
.name = "DSP Design TCVIDEO",
.video_inputs = 4,
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 3, 1, 0),
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x50 ---------------------------------- */
[BTTV_BOARD_HAUPPAUGEPVR] = {
.name = "Hauppauge WinTV PVR",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.muxsel = MUXSEL(2, 0, 1, 1),
.pll = PLL_28,
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.gpiomask = 7,
.gpiomux = {7},
},
[BTTV_BOARD_GVBCTV5PCI] = {
.name = "IODATA GV-BCTV5/PCI",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x0f0f80,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = {0x030000, 0x010000, 0, 0 },
.gpiomute = 0x020000,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_NTSC_M,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= gvbctv5pci_audio,
.has_radio = 1,
},
[BTTV_BOARD_OSPREY1x0] = {
.name = "Osprey 100/150 (878)", /* 0x1(2|3)-45C6-C1 */
.video_inputs = 4, /* id-inputs-clock */
/* .audio_inputs= 0, */
.svhs = 3,
.muxsel = MUXSEL(3, 2, 0, 1),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY1x0_848] = {
.name = "Osprey 100/150 (848)", /* 0x04-54C0-C1 & older boards */
.video_inputs = 3,
/* .audio_inputs= 0, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
/* ---- card 0x54 ---------------------------------- */
[BTTV_BOARD_OSPREY101_848] = {
.name = "Osprey 101 (848)", /* 0x05-40C0-C1 */
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = 1,
.muxsel = MUXSEL(3, 1),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY1x1] = {
.name = "Osprey 101/151", /* 0x1(4|5)-0004-C4 */
.video_inputs = 1,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(0),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY1x1_SVID] = {
.name = "Osprey 101/151 w/ svid", /* 0x(16|17|20)-00C4-C1 */
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = 1,
.muxsel = MUXSEL(0, 1),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY2xx] = {
.name = "Osprey 200/201/250/251", /* 0x1(8|9|E|F)-0004-C4 */
.video_inputs = 1,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(0),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
/* ---- card 0x58 ---------------------------------- */
[BTTV_BOARD_OSPREY2x0_SVID] = {
.name = "Osprey 200/250", /* 0x1(A|B)-00C4-C1 */
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = 1,
.muxsel = MUXSEL(0, 1),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY2x0] = {
.name = "Osprey 210/220/230", /* 0x1(A|B)-04C0-C1 */
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = 1,
.muxsel = MUXSEL(2, 3),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY500] = {
.name = "Osprey 500", /* 500 */
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = 1,
.muxsel = MUXSEL(2, 3),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
[BTTV_BOARD_OSPREY540] = {
.name = "Osprey 540", /* 540 */
.video_inputs = 4,
/* .audio_inputs= 1, */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
/* ---- card 0x5C ---------------------------------- */
[BTTV_BOARD_OSPREY2000] = {
.name = "Osprey 2000", /* 2000 */
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = 1,
.muxsel = MUXSEL(2, 3),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1, /* must avoid, conflicts with the bt860 */
},
[BTTV_BOARD_IDS_EAGLE] = {
/* M G Berberich <[email protected]> */
.name = "IDS Eagle",
.video_inputs = 4,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0,
.muxsel = MUXSEL(2, 2, 2, 2),
.muxsel_hook = eagle_muxsel,
.no_msp34xx = 1,
.pll = PLL_28,
},
[BTTV_BOARD_PINNACLESAT] = {
.name = "Pinnacle PCTV Sat",
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
.muxsel = MUXSEL(3, 1),
.pll = PLL_28,
.no_gpioirq = 1,
.has_dvb = 1,
},
[BTTV_BOARD_FORMAC_PROTV] = {
.name = "Formac ProTV II (bt878)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 3,
.gpiomask = 2,
/* TV, Comp1, Composite over SVID con, SVID */
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 2, 2, 0, 0 },
.pll = PLL_28,
.has_radio = 1,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
/* sound routing:
GPIO=0x00,0x01,0x03: mute (?)
0x02: both TV and radio (tuner: FM1216/I)
The card has onboard audio connectors labeled "cdrom" and "board",
not soldered here, though unknown wiring.
Card lacks: external audio in, pci subsystem id.
*/
},
/* ---- card 0x60 ---------------------------------- */
[BTTV_BOARD_MACHTV] = {
.name = "MachTV",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 3},
.gpiomute = 4,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
},
[BTTV_BOARD_EURESYS_PICOLO] = {
.name = "Euresys Picolo",
.video_inputs = 3,
/* .audio_inputs= 0, */
.svhs = 2,
.gpiomask = 0,
.no_msp34xx = 1,
.no_tda7432 = 1,
.muxsel = MUXSEL(2, 0, 1),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_PV150] = {
/* Luc Van Hoeylandt <[email protected]> */
.name = "ProVideo PV150", /* 0x4f */
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3),
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_AD_TVK503] = {
/* Hiroshi Takekawa <[email protected]> */
/* This card lacks subsystem ID */
.name = "AD-TVK503", /* 0x63 */
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x001e8007,
.muxsel = MUXSEL(2, 3, 1, 0),
/* Tuner, Radio, external, internal, off, on */
.gpiomux = { 0x08, 0x0f, 0x0a, 0x08 },
.gpiomute = 0x0f,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_NTSC,
.tuner_addr = ADDR_UNSET,
.audio_mode_gpio= adtvk503_audio,
},
/* ---- card 0x64 ---------------------------------- */
[BTTV_BOARD_HERCULES_SM_TV] = {
.name = "Hercules Smart TV Stereo",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x00,
.muxsel = MUXSEL(2, 3, 1, 1),
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
/* Notes:
- card lacks subsystem ID
- stereo variant w/ daughter board with tda9874a @0xb0
- Audio Routing:
always from tda9874 independent of GPIO (?)
external line in: unknown
- Other chips: em78p156elp @ 0x96 (probably IR remote control)
hef4053 (instead 4052) for unknown function
*/
},
[BTTV_BOARD_PACETV] = {
.name = "Pace TV & Radio Card",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
/* Tuner, CVid, SVid, CVid over SVid connector */
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomask = 0,
.no_tda7432 = 1,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
.pll = PLL_28,
/* Bt878, Bt832, FI1246 tuner; no pci subsystem id
only internal line out: (4pin header) RGGL
Radio must be decoded by msp3410d (not routed through)*/
/*
.digital_mode = DIGITAL_MODE_CAMERA, todo!
*/
},
[BTTV_BOARD_IVC200] = {
/* Chris Willing <[email protected]> */
.name = "IVC-200",
.video_inputs = 1,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0xdf,
.muxsel = MUXSEL(2),
.pll = PLL_28,
},
[BTTV_BOARD_IVCE8784] = {
.name = "IVCE-8784",
.video_inputs = 1,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0xdf,
.muxsel = MUXSEL(2),
.pll = PLL_28,
},
[BTTV_BOARD_XGUARD] = {
.name = "Grand X-Guard / Trust 814PCI",
.video_inputs = 16,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.gpiomask2 = 0xff,
.muxsel = MUXSEL(2,2,2,2, 3,3,3,3, 1,1,1,1, 0,0,0,0),
.muxsel_hook = xguard_muxsel,
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_28,
},
/* ---- card 0x68 ---------------------------------- */
[BTTV_BOARD_NEBULA_DIGITV] = {
.name = "Nebula Electronics DigiTV",
.video_inputs = 1,
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 3, 1, 0),
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.has_dvb = 1,
.has_remote = 1,
.gpiomask = 0x1b,
.no_gpioirq = 1,
},
[BTTV_BOARD_PV143] = {
/* Jorge Boncompte - DTI2 <[email protected]> */
.name = "ProVideo PV143",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_VD009X1_VD011_MINIDIN] = {
/* [email protected] */
.name = "PHYTEC VD-009-X1 VD-011 MiniDIN (bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 3,
.gpiomask = 0x00,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_VD009X1_VD011_COMBI] = {
.name = "PHYTEC VD-009-X1 VD-011 Combi (bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 3,
.gpiomask = 0x00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x6c ---------------------------------- */
[BTTV_BOARD_VD009_MINIDIN] = {
.name = "PHYTEC VD-009 MiniDIN (bt878)",
.video_inputs = 10,
/* .audio_inputs= 0, */
.svhs = 9,
.gpiomask = 0x00,
.gpiomask2 = 0x03, /* used for external video mux */
.muxsel = MUXSEL(2, 2, 2, 2, 3, 3, 3, 3, 1, 0),
.muxsel_hook = phytec_muxsel,
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_VD009_COMBI] = {
.name = "PHYTEC VD-009 Combi (bt878)",
.video_inputs = 10,
/* .audio_inputs= 0, */
.svhs = 9,
.gpiomask = 0x00,
.gpiomask2 = 0x03, /* used for external video mux */
.muxsel = MUXSEL(2, 2, 2, 2, 3, 3, 3, 3, 1, 1),
.muxsel_hook = phytec_muxsel,
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_IVC100] = {
.name = "IVC-100",
.video_inputs = 4,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0xdf,
.muxsel = MUXSEL(2, 3, 1, 0),
.pll = PLL_28,
},
[BTTV_BOARD_IVC120] = {
/* IVC-120G - Alan Garfield <[email protected]> */
.name = "IVC-120G",
.video_inputs = 16,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS, /* card has no svhs */
.no_msp34xx = 1,
.no_tda7432 = 1,
.gpiomask = 0x00,
.muxsel = MUXSEL(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
.muxsel_hook = ivc120_muxsel,
.pll = PLL_28,
},
/* ---- card 0x70 ---------------------------------- */
[BTTV_BOARD_PC_HDTV] = {
.name = "pcHDTV HD-2000 TV",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1, 0),
.tuner_type = TUNER_PHILIPS_FCV1236D,
.tuner_addr = ADDR_UNSET,
.has_dvb = 1,
},
[BTTV_BOARD_TWINHAN_DST] = {
.name = "Twinhan DST + clones",
.no_msp34xx = 1,
.no_tda7432 = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_video = 1,
.has_dvb = 1,
},
[BTTV_BOARD_WINFASTVC100] = {
.name = "Winfast VC100",
.video_inputs = 3,
/* .audio_inputs= 0, */
.svhs = 1,
/* Vid In, SVid In, Vid over SVid in connector */
.muxsel = MUXSEL(3, 1, 1, 3),
.no_msp34xx = 1,
.no_tda7432 = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
},
[BTTV_BOARD_TEV560] = {
.name = "Teppro TEV-560/InterVision IV-560",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 3,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 1, 1, 1, 1 },
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.pll = PLL_35,
},
/* ---- card 0x74 ---------------------------------- */
[BTTV_BOARD_SIMUS_GVC1100] = {
.name = "SIMUS GVC1100",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.muxsel = MUXSEL(2, 2, 2, 2),
.gpiomask = 0x3F,
.muxsel_hook = gvc1100_muxsel,
},
[BTTV_BOARD_NGSTV_PLUS] = {
/* Carlos Silva [email protected] || card 0x75 */
.name = "NGS NGSTV+",
.video_inputs = 3,
.svhs = 2,
.gpiomask = 0x008007,
.muxsel = MUXSEL(2, 3, 0, 0),
.gpiomux = { 0, 0, 0, 0 },
.gpiomute = 0x000003,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
},
[BTTV_BOARD_LMLBT4] = {
/* http://linuxmedialabs.com */
.name = "LMLBT4",
.video_inputs = 4, /* IN1,IN2,IN3,IN4 */
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 3, 1, 0),
.no_msp34xx = 1,
.no_tda7432 = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_TEKRAM_M205] = {
/* Helmroos Harri <[email protected]> */
.name = "Tekram M205 PRO",
.video_inputs = 3,
/* .audio_inputs= 1, */
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.svhs = 2,
.gpiomask = 0x68,
.muxsel = MUXSEL(2, 3, 1),
.gpiomux = { 0x68, 0x68, 0x61, 0x61 },
.pll = PLL_28,
},
/* ---- card 0x78 ---------------------------------- */
[BTTV_BOARD_CONTVFMI] = {
/* Javier Cendan Ares <[email protected]> */
/* bt878 TV + FM without subsystem ID */
.name = "Conceptronic CONTVFMi",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x008007,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 2 },
.gpiomute = 3,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
.has_radio = 1,
},
[BTTV_BOARD_PICOLO_TETRA_CHIP] = {
/*Eric DEBIEF <[email protected]>*/
/*EURESYS Picolo Tetra : 4 Conexant Fusion 878A, no audio, video input set with analog multiplexers GPIO controlled*/
/*adds picolo_tetra_muxsel(), picolo_tetra_init(), the following declaration*/
/*structure and #define BTTV_BOARD_PICOLO_TETRA_CHIP 0x79 in bttv.h*/
.name = "Euresys Picolo Tetra",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0,
.gpiomask2 = 0x3C<<16,/*Set the GPIO[18]->GPIO[21] as output pin.==> drive the video inputs through analog multiplexers*/
.no_msp34xx = 1,
.no_tda7432 = 1,
/*878A input is always MUX0, see above.*/
.muxsel = MUXSEL(2, 2, 2, 2),
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.muxsel_hook = picolo_tetra_muxsel,/*Required as it doesn't follow the classic input selection policy*/
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_SPIRIT_TV] = {
/* Spirit TV Tuner from http://spiritmodems.com.au */
/* Stafford Goodsell <[email protected]> */
.name = "Spirit TV Tuner",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x0000000f,
.muxsel = MUXSEL(2, 1, 1),
.gpiomux = { 0x02, 0x00, 0x00, 0x00 },
.tuner_type = TUNER_TEMIC_PAL,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
},
[BTTV_BOARD_AVDVBT_771] = {
/* Wolfram Joost <[email protected]> */
.name = "AVerMedia AVerTV DVB-T 771",
.video_inputs = 2,
.svhs = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.muxsel = MUXSEL(3, 3),
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_28,
.has_dvb = 1,
.no_gpioirq = 1,
.has_remote = 1,
},
/* ---- card 0x7c ---------------------------------- */
[BTTV_BOARD_AVDVBT_761] = {
/* Matt Jesson <[email protected]> */
/* Based on the Nebula card data - added remote and new card number - BTTV_BOARD_AVDVBT_761, see also ir-kbd-gpio.c */
.name = "AverMedia AverTV DVB-T 761",
.video_inputs = 2,
.svhs = 1,
.muxsel = MUXSEL(3, 1, 2, 0), /* Comp0, S-Video, ?, ? */
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.has_dvb = 1,
.no_gpioirq = 1,
.has_remote = 1,
},
[BTTV_BOARD_MATRIX_VISIONSQ] = {
/* [email protected] */
.name = "MATRIX Vision Sigma-SQ",
.video_inputs = 16,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0x0,
.muxsel = MUXSEL(2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3),
.muxsel_hook = sigmaSQ_muxsel,
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MATRIX_VISIONSLC] = {
/* [email protected] */
.name = "MATRIX Vision Sigma-SLC",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0x0,
.muxsel = MUXSEL(2, 2, 2, 2),
.muxsel_hook = sigmaSLC_muxsel,
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* BTTV_BOARD_APAC_VIEWCOMP */
[BTTV_BOARD_APAC_VIEWCOMP] = {
/* Attila Kondoros <[email protected]> */
/* bt878 TV + FM 0x00000000 subsystem ID */
.name = "APAC Viewcomp 878(AMAX)",
.video_inputs = 2,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.gpiomask = 0xFF,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 2, 0, 0, 0 },
.gpiomute = 10,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL,
.tuner_addr = ADDR_UNSET,
.has_remote = 1, /* miniremote works, see ir-kbd-gpio.c */
.has_radio = 1, /* not every card has radio */
},
/* ---- card 0x80 ---------------------------------- */
[BTTV_BOARD_DVICO_DVBT_LITE] = {
/* Chris Pascoe <[email protected]> */
.name = "DViCO FusionHDTV DVB-T Lite",
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_28,
.no_video = 1,
.has_dvb = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_VGEAR_MYVCD] = {
/* Steven <[email protected]> */
.name = "V-Gear MyVCD",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x3f,
.muxsel = MUXSEL(2, 3, 1, 0),
.gpiomux = {0x31, 0x31, 0x31, 0x31 },
.gpiomute = 0x31,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_NTSC_M,
.tuner_addr = ADDR_UNSET,
.has_radio = 0,
},
[BTTV_BOARD_SUPER_TV] = {
/* Rick C <[email protected]> */
.name = "Super TV Tuner",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1, 0),
.tuner_type = TUNER_PHILIPS_NTSC,
.tuner_addr = ADDR_UNSET,
.gpiomask = 0x008007,
.gpiomux = { 0, 0x000001,0,0 },
.has_radio = 1,
},
[BTTV_BOARD_TIBET_CS16] = {
/* Chris Fanning <[email protected]> */
.name = "Tibet Systems 'Progress DVR' CS16",
.video_inputs = 16,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
.pll = PLL_28,
.no_msp34xx = 1,
.no_tda7432 = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.muxsel_hook = tibetCS16_muxsel,
},
[BTTV_BOARD_KODICOM_4400R] = {
/* Bill Brack <[email protected]> */
/*
* Note that, because of the card's wiring, the "master"
* BT878A chip (i.e. the one which controls the analog switch
* and must use this card type) is the 2nd one detected. The
* other 3 chips should use card type 0x85, whose description
* follows this one. There is a EEPROM on the card (which is
* connected to the I2C of one of those other chips), but is
* not currently handled. There is also a facility for a
* "monitor", which is also not currently implemented.
*/
.name = "Kodicom 4400R (master)",
.video_inputs = 16,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
/* GPIO bits 0-9 used for analog switch:
* 00 - 03: camera selector
* 04 - 06: channel (controller) selector
* 07: data (1->on, 0->off)
* 08: strobe
* 09: reset
* bit 16 is input from sync separator for the channel
*/
.gpiomask = 0x0003ff,
.no_gpioirq = 1,
.muxsel = MUXSEL(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
.pll = PLL_28,
.no_msp34xx = 1,
.no_tda7432 = 1,
.muxsel_hook = kodicom4400r_muxsel,
},
[BTTV_BOARD_KODICOM_4400R_SL] = {
/* Bill Brack <[email protected]> */
/* Note that, for reasons unknown, the "master" BT878A chip (i.e. the
* one which controls the analog switch, and must use the card type)
* is the 2nd one detected. The other 3 chips should use this card
* type
*/
.name = "Kodicom 4400R (slave)",
.video_inputs = 16,
/* .audio_inputs= 0, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0x010000,
.no_gpioirq = 1,
.muxsel = MUXSEL(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
.pll = PLL_28,
.no_msp34xx = 1,
.no_tda7432 = 1,
.muxsel_hook = kodicom4400r_muxsel,
},
/* ---- card 0x86---------------------------------- */
[BTTV_BOARD_ADLINK_RTV24] = {
/* Michael Henson <[email protected]> */
/* Adlink RTV24 with special unlock codes */
.name = "Adlink RTV24",
.video_inputs = 4,
/* .audio_inputs= 1, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1, 0),
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
},
/* ---- card 0x87---------------------------------- */
[BTTV_BOARD_DVICO_FUSIONHDTV_5_LITE] = {
/* Michael Krufky <[email protected]> */
.name = "DViCO FusionHDTV 5 Lite",
.tuner_type = TUNER_LG_TDVS_H06XF, /* TDVS-H064F */
.tuner_addr = ADDR_UNSET,
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1),
.gpiomask = 0x00e00007,
.gpiomux = { 0x00400005, 0, 0x00000001, 0 },
.gpiomute = 0x00c00007,
.no_msp34xx = 1,
.no_tda7432 = 1,
.has_dvb = 1,
},
/* ---- card 0x88---------------------------------- */
[BTTV_BOARD_ACORP_Y878F] = {
/* Mauro Carvalho Chehab <[email protected]> */
.name = "Acorp Y878F",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x01fe00,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x001e00, 0, 0x018000, 0x014000 },
.gpiomute = 0x002000,
.pll = PLL_28,
.tuner_type = TUNER_YMEC_TVF66T5_B_DFF,
.tuner_addr = 0xc1 >>1,
.has_radio = 1,
},
/* ---- card 0x89 ---------------------------------- */
[BTTV_BOARD_CONCEPTRONIC_CTVFMI2] = {
.name = "Conceptronic CTVFMi v2",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x001c0007,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 2 },
.gpiomute = 3,
.pll = PLL_28,
.tuner_type = TUNER_TENA_9533_DI,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
.has_radio = 1,
},
/* ---- card 0x8a ---------------------------------- */
[BTTV_BOARD_PV_BT878P_2E] = {
.name = "Prolink Pixelview PV-BT878P+ (Rev.2E)",
.video_inputs = 5,
/* .audio_inputs= 1, */
.svhs = 3,
.has_dig_in = 1,
.gpiomask = 0x01fe00,
.muxsel = MUXSEL(2, 3, 1, 1, 0), /* in 4 is digital */
/* .digital_mode= DIGITAL_MODE_CAMERA, */
.gpiomux = { 0x00400, 0x10400, 0x04400, 0x80000 },
.gpiomute = 0x12400,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_LG_PAL_FM,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
},
/* ---- card 0x8b ---------------------------------- */
[BTTV_BOARD_PV_M4900] = {
/* Sérgio Fortier <[email protected]> */
.name = "Prolink PixelView PlayTV MPEG2 PV-M4900",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x3f,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x21, 0x20, 0x24, 0x2c },
.gpiomute = 0x29,
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_YMEC_TVF_5533MF,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
.has_remote = 1,
},
/* ---- card 0x8c ---------------------------------- */
/* Has four Bt878 chips behind a PCI bridge, each chip has:
one external BNC composite input (mux 2)
three internal composite inputs (unknown muxes)
an 18-bit stereo A/D (CS5331A), which has:
one external stereo unbalanced (RCA) audio connection
one (or 3?) internal stereo balanced (XLR) audio connection
input is selected via gpio to a 14052B mux
(mask=0x300, unbal=0x000, bal=0x100, ??=0x200,0x300)
gain is controlled via an X9221A chip on the I2C bus @0x28
sample rate is controlled via gpio to an MK1413S
(mask=0x3, 32kHz=0x0, 44.1kHz=0x1, 48kHz=0x2, ??=0x3)
There is neither a tuner nor an svideo input. */
[BTTV_BOARD_OSPREY440] = {
.name = "Osprey 440",
.video_inputs = 4,
/* .audio_inputs= 2, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 3, 0, 1), /* 3,0,1 are guesses */
.gpiomask = 0x303,
.gpiomute = 0x000, /* int + 32kHz */
.gpiomux = { 0, 0, 0x000, 0x100},
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
/* ---- card 0x8d ---------------------------------- */
[BTTV_BOARD_ASOUND_SKYEYE] = {
.name = "Asound Skyeye PCTV",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 15,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 2, 0, 0, 0 },
.gpiomute = 1,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_NTSC,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x8e ---------------------------------- */
[BTTV_BOARD_SABRENT_TVFM] = {
.name = "Sabrent TV-FM (bttv version)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x108007,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 100000, 100002, 100002, 100000 },
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_28,
.tuner_type = TUNER_TNF_5335MF,
.tuner_addr = ADDR_UNSET,
.has_radio = 1,
},
/* ---- card 0x8f ---------------------------------- */
[BTTV_BOARD_HAUPPAUGE_IMPACTVCB] = {
.name = "Hauppauge ImpactVCB (bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0x0f, /* old: 7 */
.muxsel = MUXSEL(0, 1, 3, 2), /* Composite 0-3 */
.no_msp34xx = 1,
.no_tda7432 = 1,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_MACHTV_MAGICTV] = {
/* Julian Calaby <[email protected]>
* Slightly different from original MachTV definition (0x60)
* FIXME: RegSpy says gpiomask should be "0x001c800f", but it
* stuffs up remote chip. Bug is a pin on the jaecs is not set
* properly (methinks) causing no keyup bits being set */
.name = "MagicTV", /* rebranded MachTV */
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 7,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 3 },
.gpiomute = 4,
.tuner_type = TUNER_TEMIC_4009FR5_PAL,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
.has_remote = 1,
},
[BTTV_BOARD_SSAI_SECURITY] = {
.name = "SSAI Security Video Interface",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(0, 1, 2, 3),
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_SSAI_ULTRASOUND] = {
.name = "SSAI Ultrasound Video Interface",
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = 1,
.muxsel = MUXSEL(2, 0, 1, 3),
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0x94---------------------------------- */
[BTTV_BOARD_DVICO_FUSIONHDTV_2] = {
.name = "DViCO FusionHDTV 2",
.tuner_type = TUNER_PHILIPS_FCV1236D,
.tuner_addr = ADDR_UNSET,
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1),
.gpiomask = 0x00e00007,
.gpiomux = { 0x00400005, 0, 0x00000001, 0 },
.gpiomute = 0x00c00007,
.no_msp34xx = 1,
.no_tda7432 = 1,
},
/* ---- card 0x95---------------------------------- */
[BTTV_BOARD_TYPHOON_TVTUNERPCI] = {
.name = "Typhoon TV-Tuner PCI (50684)",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x3014f,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0x20001,0x10001, 0, 0 },
.gpiomute = 10,
.pll = PLL_28,
.tuner_type = TUNER_PHILIPS_PAL_I,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_GEOVISION_GV600] = {
/* [email protected] */
.name = "Geovision GV-600",
.video_inputs = 16,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0x0,
.muxsel = MUXSEL(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
.muxsel_hook = geovision_muxsel,
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_KOZUMI_KTV_01C] = {
/* Mauro Lacy <[email protected]>
* Based on MagicTV and Conceptronic CONTVFMi */
.name = "Kozumi KTV-01C",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x008007,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 2 }, /* CONTVFMi */
.gpiomute = 3, /* CONTVFMi */
.tuner_type = TUNER_PHILIPS_FM1216ME_MK3, /* TCL MK3 */
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
.has_remote = 1,
},
[BTTV_BOARD_ENLTV_FM_2] = {
/* Encore TV Tuner Pro ENL TV-FM-2
Mauro Carvalho Chehab <[email protected]> */
.name = "Encore ENL TV-FM-2",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
/* bit 6 -> IR disabled
bit 18/17 = 00 -> mute
01 -> enable external audio input
10 -> internal audio input (mono?)
11 -> internal audio input
*/
.gpiomask = 0x060040,
.muxsel = MUXSEL(2, 3, 3),
.gpiomux = { 0x60000, 0x60000, 0x20000, 0x20000 },
.gpiomute = 0,
.tuner_type = TUNER_TCL_MF02GIP_5N,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
.has_radio = 1,
.has_remote = 1,
},
[BTTV_BOARD_VD012] = {
/* [email protected] */
.name = "PHYTEC VD-012 (bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0x00,
.muxsel = MUXSEL(0, 2, 3, 1),
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_VD012_X1] = {
/* [email protected] */
.name = "PHYTEC VD-012-X1 (bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 3,
.gpiomask = 0x00,
.muxsel = MUXSEL(2, 3, 1),
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_VD012_X2] = {
/* [email protected] */
.name = "PHYTEC VD-012-X2 (bt878)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = 3,
.gpiomask = 0x00,
.muxsel = MUXSEL(3, 2, 1),
.gpiomux = { 0, 0, 0, 0 }, /* card has no audio */
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_GEOVISION_GV800S] = {
/* Bruno Christo <[email protected]>
*
* GeoVision GV-800(S) has 4 Conexant Fusion 878A:
* 1 audio input per BT878A = 4 audio inputs
* 4 video inputs per BT878A = 16 video inputs
* This is the first BT878A chip of the GV-800(S). It's the
* "master" chip and it controls the video inputs through an
* analog multiplexer (a CD22M3494) via some GPIO pins. The
* slaves should use card type 0x9e (following this one).
* There is a EEPROM on the card which is currently not handled.
* The audio input is not working yet.
*/
.name = "Geovision GV-800(S) (master)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0xf107f,
.no_gpioirq = 1,
.muxsel = MUXSEL(2, 2, 2, 2),
.pll = PLL_28,
.no_msp34xx = 1,
.no_tda7432 = 1,
.muxsel_hook = gv800s_muxsel,
},
[BTTV_BOARD_GEOVISION_GV800S_SL] = {
/* Bruno Christo <[email protected]>
*
* GeoVision GV-800(S) has 4 Conexant Fusion 878A:
* 1 audio input per BT878A = 4 audio inputs
* 4 video inputs per BT878A = 16 video inputs
* The 3 other BT878A chips are "slave" chips of the GV-800(S)
* and should use this card type.
* The audio input is not working yet.
*/
.name = "Geovision GV-800(S) (slave)",
.video_inputs = 4,
/* .audio_inputs= 1, */
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
.svhs = NO_SVHS,
.gpiomask = 0x00,
.no_gpioirq = 1,
.muxsel = MUXSEL(2, 2, 2, 2),
.pll = PLL_28,
.no_msp34xx = 1,
.no_tda7432 = 1,
.muxsel_hook = gv800s_muxsel,
},
[BTTV_BOARD_PV183] = {
.name = "ProVideo PV183", /* 0x9f */
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.gpiomask = 0,
.muxsel = MUXSEL(2, 3),
.gpiomux = { 0 },
.no_msp34xx = 1,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
/* ---- card 0xa0---------------------------------- */
[BTTV_BOARD_TVT_TD3116] = {
.name = "Tongwei Video Technology TD-3116",
.video_inputs = 16,
.gpiomask = 0xc00ff,
.muxsel = MUXSEL(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
.muxsel_hook = td3116_muxsel,
.svhs = NO_SVHS,
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
},
[BTTV_BOARD_APOSONIC_WDVR] = {
.name = "Aposonic W-DVR",
.video_inputs = 4,
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 3, 1, 0),
.tuner_type = TUNER_ABSENT,
},
[BTTV_BOARD_ADLINK_MPG24] = {
/* Adlink MPG24 */
.name = "Adlink MPG24",
.video_inputs = 1,
/* .audio_inputs= 1, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 2, 2, 2),
.tuner_type = UNSET,
.tuner_addr = ADDR_UNSET,
.pll = PLL_28,
},
[BTTV_BOARD_BT848_CAP_14] = {
.name = "Bt848 Capture 14MHz",
.video_inputs = 4,
.svhs = 2,
.muxsel = MUXSEL(2, 3, 1, 0),
.pll = PLL_14,
.tuner_type = TUNER_ABSENT,
},
[BTTV_BOARD_CYBERVISION_CV06] = {
.name = "CyberVision CV06 (SV)",
.video_inputs = 4,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
.muxsel = MUXSEL(2, 3, 1, 0),
.pll = PLL_28,
.tuner_type = TUNER_ABSENT,
.tuner_addr = ADDR_UNSET,
},
[BTTV_BOARD_KWORLD_VSTREAM_XPERT] = {
/* Pojar George <[email protected]> */
.name = "Kworld V-Stream Xpert TV PVR878",
.video_inputs = 3,
/* .audio_inputs= 1, */
.svhs = 2,
.gpiomask = 0x001c0007,
.muxsel = MUXSEL(2, 3, 1, 1),
.gpiomux = { 0, 1, 2, 2 },
.gpiomute = 3,
.pll = PLL_28,
.tuner_type = TUNER_TENA_9533_DI,
.tuner_addr = ADDR_UNSET,
.has_remote = 1,
.has_radio = 1,
},
/* ---- card 0xa6---------------------------------- */
[BTTV_BOARD_PCI_8604PW] = {
/* PCI-8604PW with special unlock sequence */
.name = "PCI-8604PW",
.video_inputs = 2,
/* .audio_inputs= 0, */
.svhs = NO_SVHS,
/* The second input is available on CN4, if populated.
* The other 5x2 header (CN2?) connects to the same inputs
* as the on-board BNCs */
.muxsel = MUXSEL(2, 3),
.tuner_type = TUNER_ABSENT,
.no_msp34xx = 1,
.no_tda7432 = 1,
.pll = PLL_35,
},
};
static const unsigned int bttv_num_tvcards = ARRAY_SIZE(bttv_tvcards);
/* ----------------------------------------------------------------------- */
static unsigned char eeprom_data[256];
/*
* identify card
*/
void bttv_idcard(struct bttv *btv)
{
unsigned int gpiobits;
int i,type;
/* read PCI subsystem ID */
btv->cardid = btv->c.pci->subsystem_device << 16;
btv->cardid |= btv->c.pci->subsystem_vendor;
if (0 != btv->cardid && 0xffffffff != btv->cardid) {
/* look for the card */
for (type = -1, i = 0; cards[i].id != 0; i++)
if (cards[i].id == btv->cardid)
type = i;
if (type != -1) {
/* found it */
pr_info("%d: detected: %s [card=%d], PCI subsystem ID is %04x:%04x\n",
btv->c.nr, cards[type].name, cards[type].cardnr,
btv->cardid & 0xffff,
(btv->cardid >> 16) & 0xffff);
btv->c.type = cards[type].cardnr;
} else {
/* 404 */
pr_info("%d: subsystem: %04x:%04x (UNKNOWN)\n",
btv->c.nr, btv->cardid & 0xffff,
(btv->cardid >> 16) & 0xffff);
pr_debug("please mail id, board name and the correct card= insmod option to [email protected]\n");
}
}
/* let the user override the autodetected type */
if (card[btv->c.nr] < bttv_num_tvcards)
btv->c.type=card[btv->c.nr];
/* print which card config we are using */
pr_info("%d: using: %s [card=%d,%s]\n",
btv->c.nr, bttv_tvcards[btv->c.type].name, btv->c.type,
card[btv->c.nr] < bttv_num_tvcards
? "insmod option" : "autodetected");
/* overwrite gpio stuff ?? */
if (UNSET == audioall && UNSET == audiomux[0])
return;
if (UNSET != audiomux[0]) {
gpiobits = 0;
for (i = 0; i < ARRAY_SIZE(bttv_tvcards->gpiomux); i++) {
bttv_tvcards[btv->c.type].gpiomux[i] = audiomux[i];
gpiobits |= audiomux[i];
}
} else {
gpiobits = audioall;
for (i = 0; i < ARRAY_SIZE(bttv_tvcards->gpiomux); i++) {
bttv_tvcards[btv->c.type].gpiomux[i] = audioall;
}
}
bttv_tvcards[btv->c.type].gpiomask = (UNSET != gpiomask) ? gpiomask : gpiobits;
pr_info("%d: gpio config override: mask=0x%x, mux=",
btv->c.nr, bttv_tvcards[btv->c.type].gpiomask);
for (i = 0; i < ARRAY_SIZE(bttv_tvcards->gpiomux); i++) {
pr_cont("%s0x%x",
i ? "," : "", bttv_tvcards[btv->c.type].gpiomux[i]);
}
pr_cont("\n");
}
/*
* (most) board specific initialisations goes here
*/
/* Some Modular Technology cards have an eeprom, but no subsystem ID */
static void identify_by_eeprom(struct bttv *btv, unsigned char eeprom_data[256])
{
int type = -1;
if (0 == strncmp(eeprom_data,"GET MM20xPCTV",13))
type = BTTV_BOARD_MODTEC_205;
else if (0 == strncmp(eeprom_data+20,"Picolo",7))
type = BTTV_BOARD_EURESYS_PICOLO;
else if (eeprom_data[0] == 0x84 && eeprom_data[2]== 0)
type = BTTV_BOARD_HAUPPAUGE; /* old bt848 */
if (-1 != type) {
btv->c.type = type;
pr_info("%d: detected by eeprom: %s [card=%d]\n",
btv->c.nr, bttv_tvcards[btv->c.type].name, btv->c.type);
}
}
static void flyvideo_gpio(struct bttv *btv)
{
int gpio, has_remote, has_radio, is_capture_only;
int is_lr90, has_tda9820_tda9821;
int tuner_type = UNSET, ttype;
gpio_inout(0xffffff, 0);
udelay(8); /* without this we would see the 0x1800 mask */
gpio = gpio_read();
/* FIXME: must restore OUR_EN ??? */
/* all cards provide GPIO info, some have an additional eeprom
* LR50: GPIO coding can be found lower right CP1 .. CP9
* CP9=GPIO23 .. CP1=GPIO15; when OPEN, the corresponding GPIO reads 1.
* GPIO14-12: n.c.
* LR90: GP9=GPIO23 .. GP1=GPIO15 (right above the bt878)
* lowest 3 bytes are remote control codes (no handshake needed)
* xxxFFF: No remote control chip soldered
* xxxF00(LR26/LR50), xxxFE0(LR90): Remote control chip (LVA001 or CF45) soldered
* Note: Some bits are Audio_Mask !
*/
ttype = (gpio & 0x0f0000) >> 16;
switch (ttype) {
case 0x0:
tuner_type = 2; /* NTSC, e.g. TPI8NSR11P */
break;
case 0x2:
tuner_type = 39; /* LG NTSC (newer TAPC series) TAPC-H701P */
break;
case 0x4:
tuner_type = 5; /* Philips PAL TPI8PSB02P, TPI8PSB12P, TPI8PSB12D or FI1216, FM1216 */
break;
case 0x6:
tuner_type = 37; /* LG PAL (newer TAPC series) TAPC-G702P */
break;
case 0xC:
tuner_type = 3; /* Philips SECAM(+PAL) FQ1216ME or FI1216MF */
break;
default:
pr_info("%d: FlyVideo_gpio: unknown tuner type\n", btv->c.nr);
break;
}
has_remote = gpio & 0x800000;
has_radio = gpio & 0x400000;
/* unknown 0x200000;
* unknown2 0x100000; */
is_capture_only = !(gpio & 0x008000); /* GPIO15 */
has_tda9820_tda9821 = !(gpio & 0x004000);
is_lr90 = !(gpio & 0x002000); /* else LR26/LR50 (LR38/LR51 f. capture only) */
/*
* gpio & 0x001000 output bit for audio routing */
if (is_capture_only)
tuner_type = TUNER_ABSENT; /* No tuner present */
pr_info("%d: FlyVideo Radio=%s RemoteControl=%s Tuner=%d gpio=0x%06x\n",
btv->c.nr, has_radio ? "yes" : "no",
has_remote ? "yes" : "no", tuner_type, gpio);
pr_info("%d: FlyVideo LR90=%s tda9821/tda9820=%s capture_only=%s\n",
btv->c.nr, is_lr90 ? "yes" : "no",
has_tda9820_tda9821 ? "yes" : "no",
is_capture_only ? "yes" : "no");
if (tuner_type != UNSET) /* only set if known tuner autodetected, else let insmod option through */
btv->tuner_type = tuner_type;
btv->has_radio = has_radio;
/* LR90 Audio Routing is done by 2 hef4052, so Audio_Mask has 4 bits: 0x001c80
* LR26/LR50 only has 1 hef4052, Audio_Mask 0x000c00
* Audio options: from tuner, from tda9821/tda9821(mono,stereo,sap), from tda9874, ext., mute */
if (has_tda9820_tda9821)
btv->audio_mode_gpio = lt9415_audio;
/* todo: if(has_tda9874) btv->audio_mode_gpio = fv2000s_audio; */
}
static int miro_tunermap[] = { 0,6,2,3, 4,5,6,0, 3,0,4,5, 5,2,16,1,
14,2,17,1, 4,1,4,3, 1,2,16,1, 4,4,4,4 };
static int miro_fmtuner[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1,
1,1,1,1, 1,1,1,0, 0,0,0,0, 0,1,0,0 };
static void miro_pinnacle_gpio(struct bttv *btv)
{
int id,msp,gpio;
char *info;
gpio_inout(0xffffff, 0);
gpio = gpio_read();
id = ((gpio>>10) & 63) -1;
msp = bttv_I2CRead(btv, I2C_ADDR_MSP3400, "MSP34xx");
if (id < 32) {
btv->tuner_type = miro_tunermap[id];
if (0 == (gpio & 0x20)) {
btv->has_radio = 1;
if (!miro_fmtuner[id]) {
btv->has_tea575x = 1;
btv->tea_gpio.wren = 6;
btv->tea_gpio.most = 7;
btv->tea_gpio.clk = 8;
btv->tea_gpio.data = 9;
tea575x_init(btv);
}
} else {
btv->has_radio = 0;
}
if (-1 != msp) {
if (btv->c.type == BTTV_BOARD_MIRO)
btv->c.type = BTTV_BOARD_MIROPRO;
if (btv->c.type == BTTV_BOARD_PINNACLE)
btv->c.type = BTTV_BOARD_PINNACLEPRO;
}
pr_info("%d: miro: id=%d tuner=%d radio=%s stereo=%s\n",
btv->c.nr, id+1, btv->tuner_type,
!btv->has_radio ? "no" :
(btv->has_tea575x ? "tea575x" : "fmtuner"),
(-1 == msp) ? "no" : "yes");
} else {
/* new cards with microtune tuner */
id = 63 - id;
btv->has_radio = 0;
switch (id) {
case 1:
info = "PAL / mono";
btv->tda9887_conf = TDA9887_INTERCARRIER;
break;
case 2:
info = "PAL+SECAM / stereo";
btv->has_radio = 1;
btv->tda9887_conf = TDA9887_QSS;
break;
case 3:
info = "NTSC / stereo";
btv->has_radio = 1;
btv->tda9887_conf = TDA9887_QSS;
break;
case 4:
info = "PAL+SECAM / mono";
btv->tda9887_conf = TDA9887_QSS;
break;
case 5:
info = "NTSC / mono";
btv->tda9887_conf = TDA9887_INTERCARRIER;
break;
case 6:
info = "NTSC / stereo";
btv->tda9887_conf = TDA9887_INTERCARRIER;
break;
case 7:
info = "PAL / stereo";
btv->tda9887_conf = TDA9887_INTERCARRIER;
break;
default:
info = "oops: unknown card";
break;
}
if (-1 != msp)
btv->c.type = BTTV_BOARD_PINNACLEPRO;
pr_info("%d: pinnacle/mt: id=%d info=\"%s\" radio=%s\n",
btv->c.nr, id, info, btv->has_radio ? "yes" : "no");
btv->tuner_type = TUNER_MT2032;
}
}
/* GPIO21 L: Buffer aktiv, H: Buffer inaktiv */
#define LM1882_SYNC_DRIVE 0x200000L
static void init_ids_eagle(struct bttv *btv)
{
gpio_inout(0xffffff,0xFFFF37);
gpio_write(0x200020);
/* flash strobe inverter ?! */
gpio_write(0x200024);
/* switch sync drive off */
gpio_bits(LM1882_SYNC_DRIVE,LM1882_SYNC_DRIVE);
/* set BT848 muxel to 2 */
btaor((2)<<5, ~(2<<5), BT848_IFORM);
}
/* Muxsel helper for the IDS Eagle.
* the eagles does not use the standard muxsel-bits but
* has its own multiplexer */
static void eagle_muxsel(struct bttv *btv, unsigned int input)
{
gpio_bits(3, input & 3);
/* composite */
/* set chroma ADC to sleep */
btor(BT848_ADC_C_SLEEP, BT848_ADC);
/* set to composite video */
btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
/* switch sync drive off */
gpio_bits(LM1882_SYNC_DRIVE,LM1882_SYNC_DRIVE);
}
static void gvc1100_muxsel(struct bttv *btv, unsigned int input)
{
static const int masks[] = {0x30, 0x01, 0x12, 0x23};
gpio_write(masks[input%4]);
}
/* LMLBT4x initialization - to allow access to GPIO bits for sensors input and
alarms output
GPIObit | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
assignment | TI | O3|INx| O2| O1|IN4|IN3|IN2|IN1| | |
IN - sensor inputs, INx - sensor inputs and TI XORed together
O1,O2,O3 - alarm outputs (relays)
OUT ENABLE 1 1 0 . 1 1 0 0 . 0 0 0 0 = 0x6C0
*/
static void init_lmlbt4x(struct bttv *btv)
{
pr_debug("LMLBT4x init\n");
btwrite(0x000000, BT848_GPIO_REG_INP);
gpio_inout(0xffffff, 0x0006C0);
gpio_write(0x000000);
}
static void sigmaSQ_muxsel(struct bttv *btv, unsigned int input)
{
unsigned int inmux = input % 8;
gpio_inout( 0xf, 0xf );
gpio_bits( 0xf, inmux );
}
static void sigmaSLC_muxsel(struct bttv *btv, unsigned int input)
{
unsigned int inmux = input % 4;
gpio_inout( 3<<9, 3<<9 );
gpio_bits( 3<<9, inmux<<9 );
}
static void geovision_muxsel(struct bttv *btv, unsigned int input)
{
unsigned int inmux = input % 16;
gpio_inout(0xf, 0xf);
gpio_bits(0xf, inmux);
}
/*
* The TD3116 has 2 74HC4051 muxes wired to the MUX0 input of a bt878.
* The first 74HC4051 has the lower 8 inputs, the second one the higher 8.
* The muxes are controlled via a 74HC373 latch which is connected to
* GPIOs 0-7. GPIO 18 is connected to the LE signal of the latch.
* Q0 of the latch is connected to the Enable (~E) input of the first
* 74HC4051. Q1 - Q3 are connected to S0 - S2 of the same 74HC4051.
* Q4 - Q7 are connected to the second 74HC4051 in the same way.
*/
static void td3116_latch_value(struct bttv *btv, u32 value)
{
gpio_bits((1<<18) | 0xff, value);
gpio_bits((1<<18) | 0xff, (1<<18) | value);
udelay(1);
gpio_bits((1<<18) | 0xff, value);
}
static void td3116_muxsel(struct bttv *btv, unsigned int input)
{
u32 value;
u32 highbit;
highbit = (input & 0x8) >> 3 ;
/* Disable outputs and set value in the mux */
value = 0x11; /* Disable outputs */
value |= ((input & 0x7) << 1) << (4 * highbit);
td3116_latch_value(btv, value);
/* Enable the correct output */
value &= ~0x11;
value |= ((highbit ^ 0x1) << 4) | highbit;
td3116_latch_value(btv, value);
}
/* ----------------------------------------------------------------------- */
static void bttv_reset_audio(struct bttv *btv)
{
/*
* BT878A has a audio-reset register.
* 1. This register is an audio reset function but it is in
* function-0 (video capture) address space.
* 2. It is enough to do this once per power-up of the card.
* 3. There is a typo in the Conexant doc -- it is not at
* 0x5B, but at 0x058. (B is an odd-number, obviously a typo!).
* --//Shrikumar 030609
*/
if (btv->id != 878)
return;
if (bttv_debug)
pr_debug("%d: BT878A ARESET\n", btv->c.nr);
btwrite((1<<7), 0x058);
udelay(10);
btwrite( 0, 0x058);
}
/* initialization part one -- before registering i2c bus */
void bttv_init_card1(struct bttv *btv)
{
switch (btv->c.type) {
case BTTV_BOARD_HAUPPAUGE:
case BTTV_BOARD_HAUPPAUGE878:
boot_msp34xx(btv,5);
break;
case BTTV_BOARD_VOODOOTV_200:
case BTTV_BOARD_VOODOOTV_FM:
boot_msp34xx(btv,20);
break;
case BTTV_BOARD_AVERMEDIA98:
boot_msp34xx(btv,11);
break;
case BTTV_BOARD_HAUPPAUGEPVR:
pvr_boot(btv);
break;
case BTTV_BOARD_TWINHAN_DST:
case BTTV_BOARD_AVDVBT_771:
case BTTV_BOARD_PINNACLESAT:
btv->use_i2c_hw = 1;
break;
case BTTV_BOARD_ADLINK_RTV24:
init_RTV24( btv );
break;
case BTTV_BOARD_PCI_8604PW:
init_PCI8604PW(btv);
break;
}
if (!bttv_tvcards[btv->c.type].has_dvb)
bttv_reset_audio(btv);
}
/* initialization part two -- after registering i2c bus */
void bttv_init_card2(struct bttv *btv)
{
btv->tuner_type = UNSET;
if (BTTV_BOARD_UNKNOWN == btv->c.type) {
bttv_readee(btv,eeprom_data,0xa0);
identify_by_eeprom(btv,eeprom_data);
}
switch (btv->c.type) {
case BTTV_BOARD_MIRO:
case BTTV_BOARD_MIROPRO:
case BTTV_BOARD_PINNACLE:
case BTTV_BOARD_PINNACLEPRO:
/* miro/pinnacle */
miro_pinnacle_gpio(btv);
break;
case BTTV_BOARD_FLYVIDEO_98:
case BTTV_BOARD_MAXI:
case BTTV_BOARD_LIFE_FLYKIT:
case BTTV_BOARD_FLYVIDEO:
case BTTV_BOARD_TYPHOON_TVIEW:
case BTTV_BOARD_CHRONOS_VS2:
case BTTV_BOARD_FLYVIDEO_98FM:
case BTTV_BOARD_FLYVIDEO2000:
case BTTV_BOARD_FLYVIDEO98EZ:
case BTTV_BOARD_CONFERENCETV:
case BTTV_BOARD_LIFETEC_9415:
flyvideo_gpio(btv);
break;
case BTTV_BOARD_HAUPPAUGE:
case BTTV_BOARD_HAUPPAUGE878:
case BTTV_BOARD_HAUPPAUGEPVR:
/* pick up some config infos from the eeprom */
bttv_readee(btv,eeprom_data,0xa0);
hauppauge_eeprom(btv);
break;
case BTTV_BOARD_AVERMEDIA98:
case BTTV_BOARD_AVPHONE98:
bttv_readee(btv,eeprom_data,0xa0);
avermedia_eeprom(btv);
break;
case BTTV_BOARD_PXC200:
init_PXC200(btv);
break;
case BTTV_BOARD_PICOLO_TETRA_CHIP:
picolo_tetra_init(btv);
break;
case BTTV_BOARD_VHX:
btv->has_radio = 1;
btv->has_tea575x = 1;
btv->tea_gpio.wren = 5;
btv->tea_gpio.most = 6;
btv->tea_gpio.clk = 3;
btv->tea_gpio.data = 4;
tea575x_init(btv);
break;
case BTTV_BOARD_VOBIS_BOOSTAR:
case BTTV_BOARD_TERRATV:
terratec_active_radio_upgrade(btv);
break;
case BTTV_BOARD_MAGICTVIEW061:
if (btv->cardid == 0x3002144f) {
btv->has_radio=1;
pr_info("%d: radio detected by subsystem id (CPH05x)\n",
btv->c.nr);
}
break;
case BTTV_BOARD_STB2:
if (btv->cardid == 0x3060121a) {
/* Fix up entry for 3DFX VoodooTV 100,
which is an OEM STB card variant. */
btv->has_radio=0;
btv->tuner_type=TUNER_TEMIC_NTSC;
}
break;
case BTTV_BOARD_OSPREY1x0:
case BTTV_BOARD_OSPREY1x0_848:
case BTTV_BOARD_OSPREY101_848:
case BTTV_BOARD_OSPREY1x1:
case BTTV_BOARD_OSPREY1x1_SVID:
case BTTV_BOARD_OSPREY2xx:
case BTTV_BOARD_OSPREY2x0_SVID:
case BTTV_BOARD_OSPREY2x0:
case BTTV_BOARD_OSPREY440:
case BTTV_BOARD_OSPREY500:
case BTTV_BOARD_OSPREY540:
case BTTV_BOARD_OSPREY2000:
bttv_readee(btv,eeprom_data,0xa0);
osprey_eeprom(btv, eeprom_data);
break;
case BTTV_BOARD_IDS_EAGLE:
init_ids_eagle(btv);
break;
case BTTV_BOARD_MODTEC_205:
bttv_readee(btv,eeprom_data,0xa0);
modtec_eeprom(btv);
break;
case BTTV_BOARD_LMLBT4:
init_lmlbt4x(btv);
break;
case BTTV_BOARD_TIBET_CS16:
tibetCS16_init(btv);
break;
case BTTV_BOARD_KODICOM_4400R:
kodicom4400r_init(btv);
break;
case BTTV_BOARD_GEOVISION_GV800S:
gv800s_init(btv);
break;
}
/* pll configuration */
if (!(btv->id==848 && btv->revision==0x11)) {
/* defaults from card list */
if (PLL_28 == bttv_tvcards[btv->c.type].pll) {
btv->pll.pll_ifreq=28636363;
btv->pll.pll_crystal=BT848_IFORM_XT0;
}
if (PLL_35 == bttv_tvcards[btv->c.type].pll) {
btv->pll.pll_ifreq=35468950;
btv->pll.pll_crystal=BT848_IFORM_XT1;
}
if (PLL_14 == bttv_tvcards[btv->c.type].pll) {
btv->pll.pll_ifreq = 14318181;
btv->pll.pll_crystal = BT848_IFORM_XT0;
}
/* insmod options can override */
switch (pll[btv->c.nr]) {
case 0: /* none */
btv->pll.pll_crystal = 0;
btv->pll.pll_ifreq = 0;
btv->pll.pll_ofreq = 0;
break;
case 1: /* 28 MHz */
case 28:
btv->pll.pll_ifreq = 28636363;
btv->pll.pll_ofreq = 0;
btv->pll.pll_crystal = BT848_IFORM_XT0;
break;
case 2: /* 35 MHz */
case 35:
btv->pll.pll_ifreq = 35468950;
btv->pll.pll_ofreq = 0;
btv->pll.pll_crystal = BT848_IFORM_XT1;
break;
case 3: /* 14 MHz */
case 14:
btv->pll.pll_ifreq = 14318181;
btv->pll.pll_ofreq = 0;
btv->pll.pll_crystal = BT848_IFORM_XT0;
break;
}
}
btv->pll.pll_current = -1;
/* tuner configuration (from card list / autodetect / insmod option) */
if (UNSET != bttv_tvcards[btv->c.type].tuner_type)
if (UNSET == btv->tuner_type)
btv->tuner_type = bttv_tvcards[btv->c.type].tuner_type;
if (UNSET != tuner[btv->c.nr])
btv->tuner_type = tuner[btv->c.nr];
if (btv->tuner_type == TUNER_ABSENT)
pr_info("%d: tuner absent\n", btv->c.nr);
else if (btv->tuner_type == UNSET)
pr_warn("%d: tuner type unset\n", btv->c.nr);
else
pr_info("%d: tuner type=%d\n", btv->c.nr, btv->tuner_type);
if (autoload != UNSET) {
pr_warn("%d: the autoload option is obsolete\n", btv->c.nr);
pr_warn("%d: use option msp3400, tda7432 or tvaudio to override which audio module should be used\n",
btv->c.nr);
}
if (UNSET == btv->tuner_type)
btv->tuner_type = TUNER_ABSENT;
btv->dig = bttv_tvcards[btv->c.type].has_dig_in ?
bttv_tvcards[btv->c.type].video_inputs - 1 : UNSET;
btv->svhs = bttv_tvcards[btv->c.type].svhs == NO_SVHS ?
UNSET : bttv_tvcards[btv->c.type].svhs;
if (svhs[btv->c.nr] != UNSET)
btv->svhs = svhs[btv->c.nr];
if (remote[btv->c.nr] != UNSET)
btv->has_remote = remote[btv->c.nr];
if (bttv_tvcards[btv->c.type].has_radio)
btv->has_radio = 1;
if (bttv_tvcards[btv->c.type].has_remote)
btv->has_remote = 1;
if (!bttv_tvcards[btv->c.type].no_gpioirq)
btv->gpioirq = 1;
if (bttv_tvcards[btv->c.type].volume_gpio)
btv->volume_gpio = bttv_tvcards[btv->c.type].volume_gpio;
if (bttv_tvcards[btv->c.type].audio_mode_gpio)
btv->audio_mode_gpio = bttv_tvcards[btv->c.type].audio_mode_gpio;
if (btv->tuner_type == TUNER_ABSENT)
return; /* no tuner or related drivers to load */
if (btv->has_saa6588 || saa6588[btv->c.nr]) {
/* Probe for RDS receiver chip */
static const unsigned short addrs[] = {
0x20 >> 1,
0x22 >> 1,
I2C_CLIENT_END
};
struct v4l2_subdev *sd;
sd = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "saa6588", 0, addrs);
btv->has_saa6588 = (sd != NULL);
}
/* try to detect audio/fader chips */
/* First check if the user specified the audio chip via a module
option. */
switch (audiodev[btv->c.nr]) {
case -1:
return; /* do not load any audio module */
case 0: /* autodetect */
break;
case 1: {
/* The user specified that we should probe for msp3400 */
static const unsigned short addrs[] = {
I2C_ADDR_MSP3400 >> 1,
I2C_ADDR_MSP3400_ALT >> 1,
I2C_CLIENT_END
};
btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "msp3400", 0, addrs);
if (btv->sd_msp34xx)
return;
goto no_audio;
}
case 2: {
/* The user specified that we should probe for tda7432 */
static const unsigned short addrs[] = {
I2C_ADDR_TDA7432 >> 1,
I2C_CLIENT_END
};
if (v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tda7432", 0, addrs))
return;
goto no_audio;
}
case 3: {
/* The user specified that we should probe for tvaudio */
btv->sd_tvaudio = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tvaudio", 0, tvaudio_addrs());
if (btv->sd_tvaudio)
return;
goto no_audio;
}
default:
pr_warn("%d: unknown audiodev value!\n", btv->c.nr);
return;
}
/* There were no overrides, so now we try to discover this through the
card definition */
/* probe for msp3400 first: this driver can detect whether or not
it really is a msp3400, so it will return NULL when the device
found is really something else (e.g. a tea6300). */
if (!bttv_tvcards[btv->c.type].no_msp34xx) {
btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "msp3400",
0, I2C_ADDRS(I2C_ADDR_MSP3400 >> 1));
} else if (bttv_tvcards[btv->c.type].msp34xx_alt) {
btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "msp3400",
0, I2C_ADDRS(I2C_ADDR_MSP3400_ALT >> 1));
}
/* If we found a msp34xx, then we're done. */
if (btv->sd_msp34xx)
return;
/* Now see if we can find one of the tvaudio devices. */
btv->sd_tvaudio = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tvaudio", 0, tvaudio_addrs());
if (btv->sd_tvaudio) {
/* There may be two tvaudio chips on the card, so try to
find another. */
v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tvaudio", 0, tvaudio_addrs());
}
/* it might also be a tda7432. */
if (!bttv_tvcards[btv->c.type].no_tda7432) {
static const unsigned short addrs[] = {
I2C_ADDR_TDA7432 >> 1,
I2C_CLIENT_END
};
btv->sd_tda7432 = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tda7432", 0, addrs);
if (btv->sd_tda7432)
return;
}
if (btv->sd_tvaudio)
return;
no_audio:
pr_warn("%d: audio absent, no audio device found!\n", btv->c.nr);
}
/* initialize the tuner */
void bttv_init_tuner(struct bttv *btv)
{
int addr = ADDR_UNSET;
if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
addr = bttv_tvcards[btv->c.type].tuner_addr;
if (btv->tuner_type != TUNER_ABSENT) {
struct tuner_setup tun_setup;
/* Load tuner module before issuing tuner config call! */
if (btv->has_radio)
v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tuner",
0, v4l2_i2c_tuner_addrs(ADDRS_RADIO));
v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tuner",
0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
&btv->c.i2c_adap, "tuner",
0, v4l2_i2c_tuner_addrs(ADDRS_TV_WITH_DEMOD));
tun_setup.mode_mask = T_ANALOG_TV;
tun_setup.type = btv->tuner_type;
tun_setup.addr = addr;
if (btv->has_radio)
tun_setup.mode_mask |= T_RADIO;
bttv_call_all(btv, tuner, s_type_addr, &tun_setup);
}
if (btv->tda9887_conf) {
struct v4l2_priv_tun_config tda9887_cfg;
tda9887_cfg.tuner = TUNER_TDA9887;
tda9887_cfg.priv = &btv->tda9887_conf;
bttv_call_all(btv, tuner, s_config, &tda9887_cfg);
}
}
/* ----------------------------------------------------------------------- */
static void modtec_eeprom(struct bttv *btv)
{
if( strncmp(&(eeprom_data[0x1e]),"Temic 4066 FY5",14) ==0) {
btv->tuner_type=TUNER_TEMIC_4066FY5_PAL_I;
pr_info("%d: Modtec: Tuner autodetected by eeprom: %s\n",
btv->c.nr, &eeprom_data[0x1e]);
} else if (strncmp(&(eeprom_data[0x1e]),"Alps TSBB5",10) ==0) {
btv->tuner_type=TUNER_ALPS_TSBB5_PAL_I;
pr_info("%d: Modtec: Tuner autodetected by eeprom: %s\n",
btv->c.nr, &eeprom_data[0x1e]);
} else if (strncmp(&(eeprom_data[0x1e]),"Philips FM1246",14) ==0) {
btv->tuner_type=TUNER_PHILIPS_NTSC;
pr_info("%d: Modtec: Tuner autodetected by eeprom: %s\n",
btv->c.nr, &eeprom_data[0x1e]);
} else {
pr_info("%d: Modtec: Unknown TunerString: %s\n",
btv->c.nr, &eeprom_data[0x1e]);
}
}
static void hauppauge_eeprom(struct bttv *btv)
{
struct tveeprom tv;
tveeprom_hauppauge_analog(&tv, eeprom_data);
btv->tuner_type = tv.tuner_type;
btv->has_radio = tv.has_radio;
pr_info("%d: Hauppauge eeprom indicates model#%d\n",
btv->c.nr, tv.model);
/*
* Some of the 878 boards have duplicate PCI IDs. Switch the board
* type based on model #.
*/
if(tv.model == 64900) {
pr_info("%d: Switching board type from %s to %s\n",
btv->c.nr,
bttv_tvcards[btv->c.type].name,
bttv_tvcards[BTTV_BOARD_HAUPPAUGE_IMPACTVCB].name);
btv->c.type = BTTV_BOARD_HAUPPAUGE_IMPACTVCB;
}
/* The 61334 needs the msp3410 to do the radio demod to get sound */
if (tv.model == 61334)
btv->radio_uses_msp_demodulator = 1;
}
/* ----------------------------------------------------------------------- */
static void bttv_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
{
struct bttv *btv = tea->private_data;
struct bttv_tea575x_gpio gpio = btv->tea_gpio;
u16 val = 0;
val |= (pins & TEA575X_DATA) ? (1 << gpio.data) : 0;
val |= (pins & TEA575X_CLK) ? (1 << gpio.clk) : 0;
val |= (pins & TEA575X_WREN) ? (1 << gpio.wren) : 0;
gpio_bits((1 << gpio.data) | (1 << gpio.clk) | (1 << gpio.wren), val);
if (btv->mbox_ior) {
/* IOW and CSEL active */
gpio_bits(btv->mbox_iow | btv->mbox_csel, 0);
udelay(5);
/* all inactive */
gpio_bits(btv->mbox_ior | btv->mbox_iow | btv->mbox_csel,
btv->mbox_ior | btv->mbox_iow | btv->mbox_csel);
}
}
static u8 bttv_tea575x_get_pins(struct snd_tea575x *tea)
{
struct bttv *btv = tea->private_data;
struct bttv_tea575x_gpio gpio = btv->tea_gpio;
u8 ret = 0;
u16 val;
if (btv->mbox_ior) {
/* IOR and CSEL active */
gpio_bits(btv->mbox_ior | btv->mbox_csel, 0);
udelay(5);
}
val = gpio_read();
if (btv->mbox_ior) {
/* all inactive */
gpio_bits(btv->mbox_ior | btv->mbox_iow | btv->mbox_csel,
btv->mbox_ior | btv->mbox_iow | btv->mbox_csel);
}
if (val & (1 << gpio.data))
ret |= TEA575X_DATA;
if (val & (1 << gpio.most))
ret |= TEA575X_MOST;
return ret;
}
static void bttv_tea575x_set_direction(struct snd_tea575x *tea, bool output)
{
struct bttv *btv = tea->private_data;
struct bttv_tea575x_gpio gpio = btv->tea_gpio;
u32 mask = (1 << gpio.clk) | (1 << gpio.wren) | (1 << gpio.data) |
(1 << gpio.most);
if (output)
gpio_inout(mask, (1 << gpio.data) | (1 << gpio.clk) |
(1 << gpio.wren));
else
gpio_inout(mask, (1 << gpio.clk) | (1 << gpio.wren));
}
static const struct snd_tea575x_ops bttv_tea_ops = {
.set_pins = bttv_tea575x_set_pins,
.get_pins = bttv_tea575x_get_pins,
.set_direction = bttv_tea575x_set_direction,
};
static int tea575x_init(struct bttv *btv)
{
btv->tea.private_data = btv;
btv->tea.ops = &bttv_tea_ops;
if (!snd_tea575x_hw_init(&btv->tea)) {
pr_info("%d: detected TEA575x radio\n", btv->c.nr);
btv->tea.mute = false;
return 0;
}
btv->has_tea575x = 0;
btv->has_radio = 0;
return -ENODEV;
}
/* ----------------------------------------------------------------------- */
static int terratec_active_radio_upgrade(struct bttv *btv)
{
btv->has_radio = 1;
btv->has_tea575x = 1;
btv->tea_gpio.wren = 4;
btv->tea_gpio.most = 5;
btv->tea_gpio.clk = 3;
btv->tea_gpio.data = 2;
btv->mbox_iow = 1 << 8;
btv->mbox_ior = 1 << 9;
btv->mbox_csel = 1 << 10;
if (!tea575x_init(btv)) {
pr_info("%d: Terratec Active Radio Upgrade found\n", btv->c.nr);
btv->has_saa6588 = 1;
}
return 0;
}
/* ----------------------------------------------------------------------- */
/*
* minimal bootstrap for the WinTV/PVR -- upload altera firmware.
*
* The hcwamc.rbf firmware file is on the Hauppauge driver CD. Have
* a look at Pvr/pvr45xxx.EXE (self-extracting zip archive, can be
* unpacked with unzip).
*/
#define PVR_GPIO_DELAY 10
#define BTTV_ALT_DATA 0x000001
#define BTTV_ALT_DCLK 0x100000
#define BTTV_ALT_NCONFIG 0x800000
static int pvr_altera_load(struct bttv *btv, const u8 *micro, u32 microlen)
{
u32 n;
u8 bits;
int i;
gpio_inout(0xffffff,BTTV_ALT_DATA|BTTV_ALT_DCLK|BTTV_ALT_NCONFIG);
gpio_write(0);
udelay(PVR_GPIO_DELAY);
gpio_write(BTTV_ALT_NCONFIG);
udelay(PVR_GPIO_DELAY);
for (n = 0; n < microlen; n++) {
bits = micro[n];
for (i = 0 ; i < 8 ; i++) {
gpio_bits(BTTV_ALT_DCLK,0);
if (bits & 0x01)
gpio_bits(BTTV_ALT_DATA,BTTV_ALT_DATA);
else
gpio_bits(BTTV_ALT_DATA,0);
gpio_bits(BTTV_ALT_DCLK,BTTV_ALT_DCLK);
bits >>= 1;
}
}
gpio_bits(BTTV_ALT_DCLK,0);
udelay(PVR_GPIO_DELAY);
/* begin Altera init loop (Not necessary,but doesn't hurt) */
for (i = 0 ; i < 30 ; i++) {
gpio_bits(BTTV_ALT_DCLK,0);
gpio_bits(BTTV_ALT_DCLK,BTTV_ALT_DCLK);
}
gpio_bits(BTTV_ALT_DCLK,0);
return 0;
}
static int pvr_boot(struct bttv *btv)
{
const struct firmware *fw_entry;
int rc;
rc = request_firmware(&fw_entry, "hcwamc.rbf", &btv->c.pci->dev);
if (rc != 0) {
pr_warn("%d: no altera firmware [via hotplug]\n", btv->c.nr);
return rc;
}
rc = pvr_altera_load(btv, fw_entry->data, fw_entry->size);
pr_info("%d: altera firmware upload %s\n",
btv->c.nr, (rc < 0) ? "failed" : "ok");
release_firmware(fw_entry);
return rc;
}
/* ----------------------------------------------------------------------- */
/* some osprey specific stuff */
static void osprey_eeprom(struct bttv *btv, const u8 ee[256])
{
int i;
u32 serial = 0;
int cardid = -1;
/* This code will never actually get called in this case.... */
if (btv->c.type == BTTV_BOARD_UNKNOWN) {
/* this might be an antique... check for MMAC label in eeprom */
if (!strncmp(ee, "MMAC", 4)) {
u8 checksum = 0;
for (i = 0; i < 21; i++)
checksum += ee[i];
if (checksum != ee[21])
return;
cardid = BTTV_BOARD_OSPREY1x0_848;
for (i = 12; i < 21; i++) {
serial *= 10;
serial += ee[i] - '0';
}
}
} else {
unsigned short type;
for (i = 4 * 16; i < 8 * 16; i += 16) {
u16 checksum = (__force u16)ip_compute_csum(ee + i, 16);
if ((checksum & 0xff) + (checksum >> 8) == 0xff)
break;
}
if (i >= 8*16)
return;
ee += i;
/* found a valid descriptor */
type = get_unaligned_be16((__be16 *)(ee+4));
switch(type) {
/* 848 based */
case 0x0004:
cardid = BTTV_BOARD_OSPREY1x0_848;
break;
case 0x0005:
cardid = BTTV_BOARD_OSPREY101_848;
break;
/* 878 based */
case 0x0012:
case 0x0013:
cardid = BTTV_BOARD_OSPREY1x0;
break;
case 0x0014:
case 0x0015:
cardid = BTTV_BOARD_OSPREY1x1;
break;
case 0x0016:
case 0x0017:
case 0x0020:
cardid = BTTV_BOARD_OSPREY1x1_SVID;
break;
case 0x0018:
case 0x0019:
case 0x001E:
case 0x001F:
cardid = BTTV_BOARD_OSPREY2xx;
break;
case 0x001A:
case 0x001B:
cardid = BTTV_BOARD_OSPREY2x0_SVID;
break;
case 0x0040:
cardid = BTTV_BOARD_OSPREY500;
break;
case 0x0050:
case 0x0056:
cardid = BTTV_BOARD_OSPREY540;
/* bttv_osprey_540_init(btv); */
break;
case 0x0060:
case 0x0070:
case 0x00A0:
cardid = BTTV_BOARD_OSPREY2x0;
/* enable output on select control lines */
gpio_inout(0xffffff,0x000303);
break;
case 0x00D8:
cardid = BTTV_BOARD_OSPREY440;
break;
default:
/* unknown...leave generic, but get serial # */
pr_info("%d: osprey eeprom: unknown card type 0x%04x\n",
btv->c.nr, type);
break;
}
serial = get_unaligned_be32((__be32 *)(ee+6));
}
pr_info("%d: osprey eeprom: card=%d '%s' serial=%u\n",
btv->c.nr, cardid,
cardid > 0 ? bttv_tvcards[cardid].name : "Unknown", serial);
if (cardid<0 || btv->c.type == cardid)
return;
/* card type isn't set correctly */
if (card[btv->c.nr] < bttv_num_tvcards) {
pr_warn("%d: osprey eeprom: Not overriding user specified card type\n",
btv->c.nr);
} else {
pr_info("%d: osprey eeprom: Changing card type from %d to %d\n",
btv->c.nr, btv->c.type, cardid);
btv->c.type = cardid;
}
}
/* ----------------------------------------------------------------------- */
/* AVermedia specific stuff, from bktr_card.c */
static int tuner_0_table[] = {
TUNER_PHILIPS_NTSC, TUNER_PHILIPS_PAL /* PAL-BG*/,
TUNER_PHILIPS_PAL, TUNER_PHILIPS_PAL /* PAL-I*/,
TUNER_PHILIPS_PAL, TUNER_PHILIPS_PAL,
TUNER_PHILIPS_SECAM, TUNER_PHILIPS_SECAM,
TUNER_PHILIPS_SECAM, TUNER_PHILIPS_PAL,
TUNER_PHILIPS_FM1216ME_MK3 };
static int tuner_1_table[] = {
TUNER_TEMIC_NTSC, TUNER_TEMIC_PAL,
TUNER_TEMIC_PAL, TUNER_TEMIC_PAL,
TUNER_TEMIC_PAL, TUNER_TEMIC_PAL,
TUNER_TEMIC_4012FY5, TUNER_TEMIC_4012FY5, /* TUNER_TEMIC_SECAM */
TUNER_TEMIC_4012FY5, TUNER_TEMIC_PAL};
static void avermedia_eeprom(struct bttv *btv)
{
int tuner_make, tuner_tv_fm, tuner_format, tuner_type = 0;
tuner_make = (eeprom_data[0x41] & 0x7);
tuner_tv_fm = (eeprom_data[0x41] & 0x18) >> 3;
tuner_format = (eeprom_data[0x42] & 0xf0) >> 4;
btv->has_remote = (eeprom_data[0x42] & 0x01);
if (tuner_make == 0 || tuner_make == 2)
if (tuner_format <= 0x0a)
tuner_type = tuner_0_table[tuner_format];
if (tuner_make == 1)
if (tuner_format <= 9)
tuner_type = tuner_1_table[tuner_format];
if (tuner_make == 4)
if (tuner_format == 0x09)
tuner_type = TUNER_LG_NTSC_NEW_TAPC; /* TAPC-G702P */
pr_info("%d: Avermedia eeprom[0x%02x%02x]: tuner=",
btv->c.nr, eeprom_data[0x41], eeprom_data[0x42]);
if (tuner_type) {
btv->tuner_type = tuner_type;
pr_cont("%d", tuner_type);
} else
pr_cont("Unknown type");
pr_cont(" radio:%s remote control:%s\n",
tuner_tv_fm ? "yes" : "no",
btv->has_remote ? "yes" : "no");
}
/*
* For Voodoo TV/FM and Voodoo 200. These cards' tuners use a TDA9880
* analog demod, which is not I2C controlled like the newer and more common
* TDA9887 series. Instead it has two tri-state input pins, S0 and S1,
* that control the IF for the video and audio. Apparently, bttv GPIO
* 0x10000 is connected to S0. S0 low selects a 38.9 MHz VIF for B/G/D/K/I
* (i.e., PAL) while high selects 45.75 MHz for M/N (i.e., NTSC).
*/
u32 bttv_tda9880_setnorm(struct bttv *btv, u32 gpiobits)
{
if (btv->audio_input == TVAUDIO_INPUT_TUNER) {
if (bttv_tvnorms[btv->tvnorm].v4l2_id & V4L2_STD_MN)
gpiobits |= 0x10000;
else
gpiobits &= ~0x10000;
}
gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpiobits);
return gpiobits;
}
/*
* reset/enable the MSP on some Hauppauge cards
* Thanks to Kyösti Mälkki ([email protected])!
*
* Hauppauge: pin 5
* Voodoo: pin 20
*/
static void boot_msp34xx(struct bttv *btv, int pin)
{
int mask = (1 << pin);
gpio_inout(mask,mask);
gpio_bits(mask,0);
mdelay(2);
udelay(500);
gpio_bits(mask,mask);
if (bttv_gpio)
bttv_gpio_tracking(btv,"msp34xx");
if (bttv_verbose)
pr_info("%d: Hauppauge/Voodoo msp34xx: reset line init [%d]\n",
btv->c.nr, pin);
}
/* ----------------------------------------------------------------------- */
/* Imagenation L-Model PXC200 Framegrabber */
/* This is basically the same procedure as
* used by Alessandro Rubini in his pxc200
* driver, but using BTTV functions */
static void init_PXC200(struct bttv *btv)
{
static int vals[] = { 0x08, 0x09, 0x0a, 0x0b, 0x0d, 0x0d, 0x01, 0x02,
0x03, 0x04, 0x05, 0x06, 0x00 };
unsigned int i;
int tmp;
u32 val;
/* Initialise GPIO-connected stuff */
gpio_inout(0xffffff, (1<<13));
gpio_write(0);
udelay(3);
gpio_write(1<<13);
/* GPIO inputs are pulled up, so no need to drive
* reset pin any longer */
gpio_bits(0xffffff, 0);
if (bttv_gpio)
bttv_gpio_tracking(btv,"pxc200");
/* we could/should try and reset/control the AD pots? but
right now we simply turned off the crushing. Without
this the AGC drifts drifts
remember the EN is reverse logic -->
setting BT848_ADC_AGC_EN disable the AGC
[email protected]
*/
btwrite(BT848_ADC_RESERVED|BT848_ADC_AGC_EN, BT848_ADC);
/* Initialise MAX517 DAC */
pr_info("Setting DAC reference voltage level ...\n");
bttv_I2CWrite(btv,0x5E,0,0x80,1);
/* Initialise 12C508 PIC */
/* The I2CWrite and I2CRead commands are actually to the
* same chips - but the R/W bit is included in the address
* argument so the numbers are different */
pr_info("Initialising 12C508 PIC chip ...\n");
/* First of all, enable the clock line. This is used in the PXC200-F */
val = btread(BT848_GPIO_DMA_CTL);
val |= BT848_GPIO_DMA_CTL_GPCLKMODE;
btwrite(val, BT848_GPIO_DMA_CTL);
/* Then, push to 0 the reset pin long enough to reset the *
* device same as above for the reset line, but not the same
* value sent to the GPIO-connected stuff
* which one is the good one? */
gpio_inout(0xffffff,(1<<2));
gpio_write(0);
udelay(10);
gpio_write(1<<2);
for (i = 0; i < ARRAY_SIZE(vals); i++) {
tmp=bttv_I2CWrite(btv,0x1E,0,vals[i],1);
if (tmp != -1) {
pr_info("I2C Write(%2.2x) = %i\nI2C Read () = %2.2x\n\n",
vals[i],tmp,bttv_I2CRead(btv,0x1F,NULL));
}
}
pr_info("PXC200 Initialised\n");
}
/* ----------------------------------------------------------------------- */
/*
* The Adlink RTV-24 (aka Angelo) has some special initialisation to unlock
* it. This apparently involves the following procedure for each 878 chip:
*
* 1) write 0x00C3FEFF to the GPIO_OUT_EN register
*
* 2) write to GPIO_DATA
* - 0x0E
* - sleep 1ms
* - 0x10 + 0x0E
* - sleep 10ms
* - 0x0E
* read from GPIO_DATA into buf (uint_32)
* - if ( data>>18 & 0x01 != 0) || ( buf>>19 & 0x01 != 1 )
* error. ERROR_CPLD_Check_Failed stop.
*
* 3) write to GPIO_DATA
* - write 0x4400 + 0x0E
* - sleep 10ms
* - write 0x4410 + 0x0E
* - sleep 1ms
* - write 0x0E
* read from GPIO_DATA into buf (uint_32)
* - if ( buf>>18 & 0x01 ) || ( buf>>19 & 0x01 != 0 )
* error. ERROR_CPLD_Check_Failed.
*/
/* ----------------------------------------------------------------------- */
static void
init_RTV24 (struct bttv *btv)
{
uint32_t dataRead = 0;
long watchdog_value = 0x0E;
pr_info("%d: Adlink RTV-24 initialisation in progress ...\n",
btv->c.nr);
btwrite (0x00c3feff, BT848_GPIO_OUT_EN);
btwrite (0 + watchdog_value, BT848_GPIO_DATA);
msleep (1);
btwrite (0x10 + watchdog_value, BT848_GPIO_DATA);
msleep (10);
btwrite (0 + watchdog_value, BT848_GPIO_DATA);
dataRead = btread (BT848_GPIO_DATA);
if ((((dataRead >> 18) & 0x01) != 0) || (((dataRead >> 19) & 0x01) != 1)) {
pr_info("%d: Adlink RTV-24 initialisation(1) ERROR_CPLD_Check_Failed (read %d)\n",
btv->c.nr, dataRead);
}
btwrite (0x4400 + watchdog_value, BT848_GPIO_DATA);
msleep (10);
btwrite (0x4410 + watchdog_value, BT848_GPIO_DATA);
msleep (1);
btwrite (watchdog_value, BT848_GPIO_DATA);
msleep (1);
dataRead = btread (BT848_GPIO_DATA);
if ((((dataRead >> 18) & 0x01) != 0) || (((dataRead >> 19) & 0x01) != 0)) {
pr_info("%d: Adlink RTV-24 initialisation(2) ERROR_CPLD_Check_Failed (read %d)\n",
btv->c.nr, dataRead);
return;
}
pr_info("%d: Adlink RTV-24 initialisation complete\n", btv->c.nr);
}
/* ----------------------------------------------------------------------- */
/*
* The PCI-8604PW contains a CPLD, probably an ispMACH 4A, that filters
* the PCI REQ signals coming from the four BT878 chips. After power
* up, the CPLD does not forward requests to the bus, which prevents
* the BT878 from fetching RISC instructions from memory. While the
* CPLD is connected to most of the GPIOs of PCI device 0xD, only
* five appear to play a role in unlocking the REQ signal. The following
* sequence has been determined by trial and error without access to the
* original driver.
*
* Eight GPIOs of device 0xC are provided on connector CN4 (4 in, 4 out).
* Devices 0xE and 0xF do not appear to have anything connected to their
* GPIOs.
*
* The correct GPIO_OUT_EN value might have some more bits set. It should
* be possible to derive it from a boundary scan of the CPLD. Its JTAG
* pins are routed to test points.
*
*/
/* ----------------------------------------------------------------------- */
static void
init_PCI8604PW(struct bttv *btv)
{
int state;
if ((PCI_SLOT(btv->c.pci->devfn) & ~3) != 0xC) {
pr_warn("This is not a PCI-8604PW\n");
return;
}
if (PCI_SLOT(btv->c.pci->devfn) != 0xD)
return;
btwrite(0x080002, BT848_GPIO_OUT_EN);
state = (btread(BT848_GPIO_DATA) >> 21) & 7;
for (;;) {
switch (state) {
case 1:
case 5:
case 6:
case 4:
pr_debug("PCI-8604PW in state %i, toggling pin\n",
state);
btwrite(0x080000, BT848_GPIO_DATA);
msleep(1);
btwrite(0x000000, BT848_GPIO_DATA);
msleep(1);
break;
case 7:
pr_info("PCI-8604PW unlocked\n");
return;
case 0:
/* FIXME: If we are in state 7 and toggle GPIO[19] one
more time, the CPLD goes into state 0, where PCI bus
mastering is inhibited again. We have not managed to
get out of that state. */
pr_err("PCI-8604PW locked until reset\n");
return;
default:
pr_err("PCI-8604PW in unknown state %i\n", state);
return;
}
state = (state << 4) | ((btread(BT848_GPIO_DATA) >> 21) & 7);
switch (state) {
case 0x15:
case 0x56:
case 0x64:
case 0x47:
/* The transition from state 7 to state 0 is, as explained
above, valid but undesired and with this code impossible
as we exit as soon as we are in state 7.
case 0x70: */
break;
default:
pr_err("PCI-8604PW invalid transition %i -> %i\n",
state >> 4, state & 7);
return;
}
state &= 7;
}
}
/* RemoteVision MX (rv605) muxsel helper [Miguel Freitas]
*
* This is needed because rv605 don't use a normal multiplex, but a crosspoint
* switch instead (CD22M3494E). This IC can have multiple active connections
* between Xn (input) and Yn (output) pins. We need to clear any existing
* connection prior to establish a new one, pulsing the STROBE pin.
*
* The board hardwire Y0 (xpoint) to MUX1 and MUXOUT to Yin.
* GPIO pins are wired as:
* GPIO[0:3] - AX[0:3] (xpoint) - P1[0:3] (microcontroller)
* GPIO[4:6] - AY[0:2] (xpoint) - P1[4:6] (microcontroller)
* GPIO[7] - DATA (xpoint) - P1[7] (microcontroller)
* GPIO[8] - - P3[5] (microcontroller)
* GPIO[9] - RESET (xpoint) - P3[6] (microcontroller)
* GPIO[10] - STROBE (xpoint) - P3[7] (microcontroller)
* GPINTR - - P3[4] (microcontroller)
*
* The microcontroller is a 80C32 like. It should be possible to change xpoint
* configuration either directly (as we are doing) or using the microcontroller
* which is also wired to I2C interface. I have no further info on the
* microcontroller features, one would need to disassembly the firmware.
* note: the vendor refused to give any information on this product, all
* that stuff was found using a multimeter! :)
*/
static void rv605_muxsel(struct bttv *btv, unsigned int input)
{
static const u8 muxgpio[] = { 0x3, 0x1, 0x2, 0x4, 0xf, 0x7, 0xe, 0x0,
0xd, 0xb, 0xc, 0x6, 0x9, 0x5, 0x8, 0xa };
gpio_bits(0x07f, muxgpio[input]);
/* reset all connections */
gpio_bits(0x200,0x200);
mdelay(1);
gpio_bits(0x200,0x000);
mdelay(1);
/* create a new connection */
gpio_bits(0x480,0x480);
mdelay(1);
gpio_bits(0x480,0x080);
mdelay(1);
}
/* Tibet Systems 'Progress DVR' CS16 muxsel helper [Chris Fanning]
*
* The CS16 (available on eBay cheap) is a PCI board with four Fusion
* 878A chips, a PCI bridge, an Atmel microcontroller, four sync separator
* chips, ten eight input analog multiplexors, a not chip and a few
* other components.
*
* 16 inputs on a secondary bracket are provided and can be selected
* from each of the four capture chips. Two of the eight input
* multiplexors are used to select from any of the 16 input signals.
*
* Unsupported hardware capabilities:
* . A video output monitor on the secondary bracket can be selected from
* one of the 878A chips.
* . Another passthrough but I haven't spent any time investigating it.
* . Digital I/O (logic level connected to GPIO) is available from an
* onboard header.
*
* The on chip input mux should always be set to 2.
* GPIO[16:19] - Video input selection
* GPIO[0:3] - Video output monitor select (only available from one 878A)
* GPIO[?:?] - Digital I/O.
*
* There is an ATMEL microcontroller with an 8031 core on board. I have not
* determined what function (if any) it provides. With the microcontroller
* and sync separator chips a guess is that it might have to do with video
* switching and maybe some digital I/O.
*/
static void tibetCS16_muxsel(struct bttv *btv, unsigned int input)
{
/* video mux */
gpio_bits(0x0f0000, input << 16);
}
static void tibetCS16_init(struct bttv *btv)
{
/* enable gpio bits, mask obtained via btSpy */
gpio_inout(0xffffff, 0x0f7fff);
gpio_write(0x0f7fff);
}
/*
* The following routines for the Kodicom-4400r get a little mind-twisting.
* There is a "master" controller and three "slave" controllers, together
* an analog switch which connects any of 16 cameras to any of the BT87A's.
* The analog switch is controlled by the "master", but the detection order
* of the four BT878A chips is in an order which I just don't understand.
* The "master" is actually the second controller to be detected. The
* logic on the board uses logical numbers for the 4 controllers, but
* those numbers are different from the detection sequence. When working
* with the analog switch, we need to "map" from the detection sequence
* over to the board's logical controller number. This mapping sequence
* is {3, 0, 2, 1}, i.e. the first controller to be detected is logical
* unit 3, the second (which is the master) is logical unit 0, etc.
* We need to maintain the status of the analog switch (which of the 16
* cameras is connected to which of the 4 controllers) in sw_status array.
*/
/*
* First a routine to set the analog switch, which controls which camera
* is routed to which controller. The switch comprises an X-address
* (gpio bits 0-3, representing the camera, ranging from 0-15), and a
* Y-address (gpio bits 4-6, representing the controller, ranging from 0-3).
* A data value (gpio bit 7) of '1' enables the switch, and '0' disables
* the switch. A STROBE bit (gpio bit 8) latches the data value into the
* specified address. The idea is to set the address and data, then bring
* STROBE high, and finally bring STROBE back to low.
*/
static void kodicom4400r_write(struct bttv *btv,
unsigned char xaddr,
unsigned char yaddr,
unsigned char data) {
unsigned int udata;
udata = (data << 7) | ((yaddr&3) << 4) | (xaddr&0xf);
gpio_bits(0x1ff, udata); /* write ADDR and DAT */
gpio_bits(0x1ff, udata | (1 << 8)); /* strobe high */
gpio_bits(0x1ff, udata); /* strobe low */
}
/*
* Next the mux select. Both the "master" and "slave" 'cards' (controllers)
* use this routine. The routine finds the "master" for the card, maps
* the controller number from the detected position over to the logical
* number, writes the appropriate data to the analog switch, and housekeeps
* the local copy of the switch information. The parameter 'input' is the
* requested camera number (0 - 15).
*/
static void kodicom4400r_muxsel(struct bttv *btv, unsigned int input)
{
int xaddr, yaddr;
struct bttv *mctlr;
static unsigned char map[4] = {3, 0, 2, 1};
mctlr = master[btv->c.nr];
if (mctlr == NULL) { /* ignore if master not yet detected */
return;
}
yaddr = (btv->c.nr - mctlr->c.nr + 1) & 3; /* the '&' is for safety */
yaddr = map[yaddr];
xaddr = input & 0xf;
/* Check if the controller/camera pair has changed, else ignore */
if (mctlr->sw_status[yaddr] != xaddr)
{
/* "open" the old switch, "close" the new one, save the new */
kodicom4400r_write(mctlr, mctlr->sw_status[yaddr], yaddr, 0);
mctlr->sw_status[yaddr] = xaddr;
kodicom4400r_write(mctlr, xaddr, yaddr, 1);
}
}
/*
* During initialisation, we need to reset the analog switch. We
* also preset the switch to map the 4 connectors on the card to the
* *user's* (see above description of kodicom4400r_muxsel) channels
* 0 through 3
*/
static void kodicom4400r_init(struct bttv *btv)
{
int ix;
gpio_inout(0x0003ff, 0x0003ff);
gpio_write(1 << 9); /* reset MUX */
gpio_write(0);
/* Preset camera 0 to the 4 controllers */
for (ix = 0; ix < 4; ix++) {
btv->sw_status[ix] = ix;
kodicom4400r_write(btv, ix, ix, 1);
}
/*
* Since this is the "master", we need to set up the
* other three controller chips' pointers to this structure
* for later use in the muxsel routine.
*/
if ((btv->c.nr<1) || (btv->c.nr>BTTV_MAX-3))
return;
master[btv->c.nr-1] = btv;
master[btv->c.nr] = btv;
master[btv->c.nr+1] = btv;
master[btv->c.nr+2] = btv;
}
/* The Grandtec X-Guard framegrabber card uses two Dual 4-channel
* video multiplexers to provide up to 16 video inputs. These
* multiplexers are controlled by the lower 8 GPIO pins of the
* bt878. The multiplexers probably Pericom PI5V331Q or similar.
* xxx0 is pin xxx of multiplexer U5,
* yyy1 is pin yyy of multiplexer U2
*/
#define ENA0 0x01
#define ENB0 0x02
#define ENA1 0x04
#define ENB1 0x08
#define IN10 0x10
#define IN00 0x20
#define IN11 0x40
#define IN01 0x80
static void xguard_muxsel(struct bttv *btv, unsigned int input)
{
static const int masks[] = {
ENB0, ENB0|IN00, ENB0|IN10, ENB0|IN00|IN10,
ENA0, ENA0|IN00, ENA0|IN10, ENA0|IN00|IN10,
ENB1, ENB1|IN01, ENB1|IN11, ENB1|IN01|IN11,
ENA1, ENA1|IN01, ENA1|IN11, ENA1|IN01|IN11,
};
gpio_write(masks[input%16]);
}
static void picolo_tetra_init(struct bttv *btv)
{
/*This is the video input redirection functionality : I DID NOT USE IT. */
btwrite (0x08<<16,BT848_GPIO_DATA);/*GPIO[19] [==> 4053 B+C] set to 1 */
btwrite (0x04<<16,BT848_GPIO_DATA);/*GPIO[18] [==> 4053 A] set to 1*/
}
static void picolo_tetra_muxsel (struct bttv* btv, unsigned int input)
{
dprintk("%d : picolo_tetra_muxsel => input = %d\n", btv->c.nr, input);
/*Just set the right path in the analog multiplexers : channel 1 -> 4 ==> Analog Mux ==> MUX0*/
/*GPIO[20]&GPIO[21] used to choose the right input*/
btwrite (input<<20,BT848_GPIO_DATA);
}
/*
* ivc120_muxsel [Added by Alan Garfield <[email protected]>]
*
* The IVC120G security card has 4 i2c controlled TDA8540 matrix
* switchers to provide 16 channels to MUX0. The TDA8540's have
* 4 independent outputs and as such the IVC120G also has the
* optional "Monitor Out" bus. This allows the card to be looking
* at one input while the monitor is looking at another.
*
* Since I've couldn't be bothered figuring out how to add an
* independent muxsel for the monitor bus, I've just set it to
* whatever the card is looking at.
*
* OUT0 of the TDA8540's is connected to MUX0 (0x03)
* OUT1 of the TDA8540's is connected to "Monitor Out" (0x0C)
*
* TDA8540_ALT3 IN0-3 = Channel 13 - 16 (0x03)
* TDA8540_ALT4 IN0-3 = Channel 1 - 4 (0x03)
* TDA8540_ALT5 IN0-3 = Channel 5 - 8 (0x03)
* TDA8540_ALT6 IN0-3 = Channel 9 - 12 (0x03)
*
*/
/* All 7 possible sub-ids for the TDA8540 Matrix Switcher */
#define I2C_TDA8540 0x90
#define I2C_TDA8540_ALT1 0x92
#define I2C_TDA8540_ALT2 0x94
#define I2C_TDA8540_ALT3 0x96
#define I2C_TDA8540_ALT4 0x98
#define I2C_TDA8540_ALT5 0x9a
#define I2C_TDA8540_ALT6 0x9c
static void ivc120_muxsel(struct bttv *btv, unsigned int input)
{
/* Simple maths */
int key = input % 4;
int matrix = input / 4;
dprintk("%d: ivc120_muxsel: Input - %02d | TDA - %02d | In - %02d\n",
btv->c.nr, input, matrix, key);
/* Handles the input selection on the TDA8540's */
bttv_I2CWrite(btv, I2C_TDA8540_ALT3, 0x00,
((matrix == 3) ? (key | key << 2) : 0x00), 1);
bttv_I2CWrite(btv, I2C_TDA8540_ALT4, 0x00,
((matrix == 0) ? (key | key << 2) : 0x00), 1);
bttv_I2CWrite(btv, I2C_TDA8540_ALT5, 0x00,
((matrix == 1) ? (key | key << 2) : 0x00), 1);
bttv_I2CWrite(btv, I2C_TDA8540_ALT6, 0x00,
((matrix == 2) ? (key | key << 2) : 0x00), 1);
/* Handles the output enables on the TDA8540's */
bttv_I2CWrite(btv, I2C_TDA8540_ALT3, 0x02,
((matrix == 3) ? 0x03 : 0x00), 1); /* 13 - 16 */
bttv_I2CWrite(btv, I2C_TDA8540_ALT4, 0x02,
((matrix == 0) ? 0x03 : 0x00), 1); /* 1-4 */
bttv_I2CWrite(btv, I2C_TDA8540_ALT5, 0x02,
((matrix == 1) ? 0x03 : 0x00), 1); /* 5-8 */
bttv_I2CWrite(btv, I2C_TDA8540_ALT6, 0x02,
((matrix == 2) ? 0x03 : 0x00), 1); /* 9-12 */
/* 878's MUX0 is already selected for input via muxsel values */
}
/* PXC200 muxsel helper
* [email protected]
* another transplant
* from Alessandro Rubini ([email protected])
*
* There are 4 kinds of cards:
* PXC200L which is bt848
* PXC200F which is bt848 with PIC controlling mux
* PXC200AL which is bt878
* PXC200AF which is bt878 with PIC controlling mux
*/
#define PX_CFG_PXC200F 0x01
#define PX_FLAG_PXC200A 0x00001000 /* a pxc200A is bt-878 based */
#define PX_I2C_PIC 0x0f
#define PX_PXC200A_CARDID 0x200a1295
#define PX_I2C_CMD_CFG 0x00
static void PXC200_muxsel(struct bttv *btv, unsigned int input)
{
int rc;
long mux;
int bitmask;
unsigned char buf[2];
/* Read PIC config to determine if this is a PXC200F */
/* PX_I2C_CMD_CFG*/
buf[0]=0;
buf[1]=0;
rc=bttv_I2CWrite(btv,(PX_I2C_PIC<<1),buf[0],buf[1],1);
if (rc) {
pr_debug("%d: PXC200_muxsel: pic cfg write failed:%d\n",
btv->c.nr, rc);
/* not PXC ? do nothing */
return;
}
rc=bttv_I2CRead(btv,(PX_I2C_PIC<<1),NULL);
if (!(rc & PX_CFG_PXC200F)) {
pr_debug("%d: PXC200_muxsel: not PXC200F rc:%d\n",
btv->c.nr, rc);
return;
}
/* The multiplexer in the 200F is handled by the GPIO port */
/* get correct mapping between inputs */
/* mux = bttv_tvcards[btv->type].muxsel[input] & 3; */
/* ** not needed!? */
mux = input;
/* make sure output pins are enabled */
/* bitmask=0x30f; */
bitmask=0x302;
/* check whether we have a PXC200A */
if (btv->cardid == PX_PXC200A_CARDID) {
bitmask ^= 0x180; /* use 7 and 9, not 8 and 9 */
bitmask |= 7<<4; /* the DAC */
}
btwrite(bitmask, BT848_GPIO_OUT_EN);
bitmask = btread(BT848_GPIO_DATA);
if (btv->cardid == PX_PXC200A_CARDID)
bitmask = (bitmask & ~0x280) | ((mux & 2) << 8) | ((mux & 1) << 7);
else /* older device */
bitmask = (bitmask & ~0x300) | ((mux & 3) << 8);
btwrite(bitmask,BT848_GPIO_DATA);
/*
* Was "to be safe, set the bt848 to input 0"
* Actually, since it's ok at load time, better not messing
* with these bits (on PXC200AF you need to set mux 2 here)
*
* needed because bttv-driver sets mux before calling this function
*/
if (btv->cardid == PX_PXC200A_CARDID)
btaor(2<<5, ~BT848_IFORM_MUXSEL, BT848_IFORM);
else /* older device */
btand(~BT848_IFORM_MUXSEL,BT848_IFORM);
pr_debug("%d: setting input channel to:%d\n", btv->c.nr, (int)mux);
}
static void phytec_muxsel(struct bttv *btv, unsigned int input)
{
unsigned int mux = input % 4;
if (input == btv->svhs)
mux = 0;
gpio_bits(0x3, mux);
}
/*
* GeoVision GV-800(S) functions
* Bruno Christo <[email protected]>
*/
/* This is a function to control the analog switch, which determines which
* camera is routed to which controller. The switch comprises an X-address
* (gpio bits 0-3, representing the camera, ranging from 0-15), and a
* Y-address (gpio bits 4-6, representing the controller, ranging from 0-3).
* A data value (gpio bit 18) of '1' enables the switch, and '0' disables
* the switch. A STROBE bit (gpio bit 17) latches the data value into the
* specified address. There is also a chip select (gpio bit 16).
* The idea is to set the address and chip select together, bring
* STROBE high, write the data, and finally bring STROBE back to low.
*/
static void gv800s_write(struct bttv *btv,
unsigned char xaddr,
unsigned char yaddr,
unsigned char data) {
/* On the "master" 878A:
* GPIO bits 0-9 are used for the analog switch:
* 00 - 03: camera selector
* 04 - 06: 878A (controller) selector
* 16: cselect
* 17: strobe
* 18: data (1->on, 0->off)
* 19: reset
*/
const u32 ADDRESS = ((xaddr&0xf) | (yaddr&3)<<4);
const u32 CSELECT = 1<<16;
const u32 STROBE = 1<<17;
const u32 DATA = data<<18;
gpio_bits(0x1007f, ADDRESS | CSELECT); /* write ADDRESS and CSELECT */
gpio_bits(0x20000, STROBE); /* STROBE high */
gpio_bits(0x40000, DATA); /* write DATA */
gpio_bits(0x20000, ~STROBE); /* STROBE low */
}
/*
* GeoVision GV-800(S) muxsel
*
* Each of the 4 cards (controllers) use this function.
* The controller using this function selects the input through the GPIO pins
* of the "master" card. A pointer to this card is stored in master[btv->c.nr].
*
* The parameter 'input' is the requested camera number (0-4) on the controller.
* The map array has the address of each input. Note that the addresses in the
* array are in the sequence the original GeoVision driver uses, that is, set
* every controller to input 0, then to input 1, 2, 3, repeat. This means that
* the physical "camera 1" connector corresponds to controller 0 input 0,
* "camera 2" corresponds to controller 1 input 0, and so on.
*
* After getting the input address, the function then writes the appropriate
* data to the analog switch, and housekeeps the local copy of the switch
* information.
*/
static void gv800s_muxsel(struct bttv *btv, unsigned int input)
{
struct bttv *mctlr;
int xaddr, yaddr;
static unsigned int map[4][4] = { { 0x0, 0x4, 0xa, 0x6 },
{ 0x1, 0x5, 0xb, 0x7 },
{ 0x2, 0x8, 0xc, 0xe },
{ 0x3, 0x9, 0xd, 0xf } };
input = input%4;
mctlr = master[btv->c.nr];
if (mctlr == NULL) {
/* do nothing until the "master" is detected */
return;
}
yaddr = (btv->c.nr - mctlr->c.nr) & 3;
xaddr = map[yaddr][input] & 0xf;
/* Check if the controller/camera pair has changed, ignore otherwise */
if (mctlr->sw_status[yaddr] != xaddr) {
/* disable the old switch, enable the new one and save status */
gv800s_write(mctlr, mctlr->sw_status[yaddr], yaddr, 0);
mctlr->sw_status[yaddr] = xaddr;
gv800s_write(mctlr, xaddr, yaddr, 1);
}
}
/* GeoVision GV-800(S) "master" chip init */
static void gv800s_init(struct bttv *btv)
{
int ix;
gpio_inout(0xf107f, 0xf107f);
gpio_write(1<<19); /* reset the analog MUX */
gpio_write(0);
/* Preset camera 0 to the 4 controllers */
for (ix = 0; ix < 4; ix++) {
btv->sw_status[ix] = ix;
gv800s_write(btv, ix, ix, 1);
}
/* Inputs on the "master" controller need this brightness fix */
bttv_I2CWrite(btv, 0x18, 0x5, 0x90, 1);
if (btv->c.nr > BTTV_MAX-4)
return;
/*
* Store the "master" controller pointer in the master
* array for later use in the muxsel function.
*/
master[btv->c.nr] = btv;
master[btv->c.nr+1] = btv;
master[btv->c.nr+2] = btv;
master[btv->c.nr+3] = btv;
}
/* ----------------------------------------------------------------------- */
/* motherboard chipset specific stuff */
void __init bttv_check_chipset(void)
{
struct pci_dev *dev = NULL;
if (pci_pci_problems & (PCIPCI_TRITON|PCIPCI_NATOMA|PCIPCI_VIAETBF))
triton1 = 1;
if (pci_pci_problems & PCIPCI_VSFX)
vsfx = 1;
#ifdef PCIPCI_ALIMAGIK
if (pci_pci_problems & PCIPCI_ALIMAGIK)
latency = 0x0A;
#endif
/* print warnings about any quirks found */
if (triton1)
pr_info("Host bridge needs ETBF enabled\n");
if (vsfx)
pr_info("Host bridge needs VSFX enabled\n");
if (UNSET != latency)
pr_info("pci latency fixup [%d]\n", latency);
while ((dev = pci_get_device(PCI_VENDOR_ID_INTEL,
PCI_DEVICE_ID_INTEL_82441, dev))) {
unsigned char b;
pci_read_config_byte(dev, 0x53, &b);
if (bttv_debug)
pr_info("Host bridge: 82441FX Natoma, bufcon=0x%02x\n",
b);
}
}
int bttv_handle_chipset(struct bttv *btv)
{
unsigned char command;
if (!triton1 && !vsfx && UNSET == latency)
return 0;
if (bttv_verbose) {
if (triton1)
pr_info("%d: enabling ETBF (430FX/VP3 compatibility)\n",
btv->c.nr);
if (vsfx && btv->id >= 878)
pr_info("%d: enabling VSFX\n", btv->c.nr);
if (UNSET != latency)
pr_info("%d: setting pci timer to %d\n",
btv->c.nr, latency);
}
if (btv->id < 878) {
/* bt848 (mis)uses a bit in the irq mask for etbf */
if (triton1)
btv->triton1 = BT848_INT_ETBF;
} else {
/* bt878 has a bit in the pci config space for it */
pci_read_config_byte(btv->c.pci, BT878_DEVCTRL, &command);
if (triton1)
command |= BT878_EN_TBFX;
if (vsfx)
command |= BT878_EN_VSFX;
pci_write_config_byte(btv->c.pci, BT878_DEVCTRL, command);
}
if (UNSET != latency)
pci_write_config_byte(btv->c.pci, PCI_LATENCY_TIMER, latency);
return 0;
}
| linux-master | drivers/media/pci/bt8xx/bttv-cards.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv-if.c -- old gpio interface to other kernel modules
don't use in new code, will go away in 2.7
have a look at bttv-gpio.c instead.
bttv - Bt848 frame grabber driver
Copyright (C) 1996,97,98 Ralph Metzler ([email protected])
& Marcus Metzler ([email protected])
(c) 1999-2003 Gerd Knorr <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/io.h>
#include "bttvp.h"
EXPORT_SYMBOL(bttv_get_pcidev);
EXPORT_SYMBOL(bttv_gpio_enable);
EXPORT_SYMBOL(bttv_read_gpio);
EXPORT_SYMBOL(bttv_write_gpio);
/* ----------------------------------------------------------------------- */
/* Exported functions - for other modules which want to access the */
/* gpio ports (IR for example) */
/* see bttv.h for comments */
struct pci_dev* bttv_get_pcidev(unsigned int card)
{
if (card >= bttv_num)
return NULL;
if (!bttvs[card])
return NULL;
return bttvs[card]->c.pci;
}
int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
{
struct bttv *btv;
if (card >= bttv_num) {
return -EINVAL;
}
btv = bttvs[card];
if (!btv)
return -ENODEV;
gpio_inout(mask,data);
if (bttv_gpio)
bttv_gpio_tracking(btv,"extern enable");
return 0;
}
int bttv_read_gpio(unsigned int card, unsigned long *data)
{
struct bttv *btv;
if (card >= bttv_num) {
return -EINVAL;
}
btv = bttvs[card];
if (!btv)
return -ENODEV;
if(btv->shutdown) {
return -ENODEV;
}
/* prior setting BT848_GPIO_REG_INP is (probably) not needed
because we set direct input on init */
*data = gpio_read();
return 0;
}
int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
{
struct bttv *btv;
if (card >= bttv_num) {
return -EINVAL;
}
btv = bttvs[card];
if (!btv)
return -ENODEV;
/* prior setting BT848_GPIO_REG_INP is (probably) not needed
because direct input is set on init */
gpio_bits(mask,data);
if (bttv_gpio)
bttv_gpio_tracking(btv,"extern write");
return 0;
}
| linux-master | drivers/media/pci/bt8xx/bttv-if.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
CA-driver for TwinHan DST Frontend/Card
Copyright (C) 2004, 2005 Manu Abraham ([email protected])
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <linux/dvb/ca.h>
#include <media/dvbdev.h>
#include <media/dvb_frontend.h>
#include "dst_ca.h"
#include "dst_common.h"
#define DST_CA_ERROR 0
#define DST_CA_NOTICE 1
#define DST_CA_INFO 2
#define DST_CA_DEBUG 3
#define dprintk(x, y, z, format, arg...) do { \
if (z) { \
if ((x > DST_CA_ERROR) && (x > y)) \
printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
else if ((x > DST_CA_NOTICE) && (x > y)) \
printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
else if ((x > DST_CA_INFO) && (x > y)) \
printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
else if ((x > DST_CA_DEBUG) && (x > y)) \
printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
} else { \
if (x > y) \
printk(format, ## arg); \
} \
} while(0)
static DEFINE_MUTEX(dst_ca_mutex);
static unsigned int verbose = 5;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
static void put_command_and_length(u8 *data, int command, int length)
{
data[0] = (command >> 16) & 0xff;
data[1] = (command >> 8) & 0xff;
data[2] = command & 0xff;
data[3] = length;
}
static void put_checksum(u8 *check_string, int length)
{
dprintk(verbose, DST_CA_DEBUG, 1, " Computing string checksum.");
dprintk(verbose, DST_CA_DEBUG, 1, " -> string length : 0x%02x", length);
check_string[length] = dst_check_sum (check_string, length);
dprintk(verbose, DST_CA_DEBUG, 1, " -> checksum : 0x%02x", check_string[length]);
}
static int dst_ci_command(struct dst_state* state, u8 * data, u8 *ca_string, u8 len, int read)
{
u8 reply;
mutex_lock(&state->dst_mutex);
dst_comm_init(state);
msleep(65);
if (write_dst(state, data, len)) {
dprintk(verbose, DST_CA_INFO, 1, " Write not successful, trying to recover");
dst_error_recovery(state);
goto error;
}
if ((dst_pio_disable(state)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " DST PIO disable failed.");
goto error;
}
if (read_dst(state, &reply, GET_ACK) < 0) {
dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
dst_error_recovery(state);
goto error;
}
if (read) {
if (! dst_wait_dst_ready(state, LONG_DELAY)) {
dprintk(verbose, DST_CA_NOTICE, 1, " 8820 not ready");
goto error;
}
if (read_dst(state, ca_string, 128) < 0) { /* Try to make this dynamic */
dprintk(verbose, DST_CA_INFO, 1, " Read not successful, trying to recover");
dst_error_recovery(state);
goto error;
}
}
mutex_unlock(&state->dst_mutex);
return 0;
error:
mutex_unlock(&state->dst_mutex);
return -EIO;
}
static int dst_put_ci(struct dst_state *state, u8 *data, int len, u8 *ca_string, int read)
{
u8 dst_ca_comm_err = 0;
while (dst_ca_comm_err < RETRIES) {
dprintk(verbose, DST_CA_NOTICE, 1, " Put Command");
if (dst_ci_command(state, data, ca_string, len, read)) { // If error
dst_error_recovery(state);
dst_ca_comm_err++; // work required here.
} else {
break;
}
}
if(dst_ca_comm_err == RETRIES)
return -EIO;
return 0;
}
static int ca_get_app_info(struct dst_state *state)
{
int length, str_length;
static u8 command[8] = {0x07, 0x40, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff};
put_checksum(&command[0], command[0]);
if ((dst_put_ci(state, command, sizeof(command), state->messages, GET_REPLY)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
return -EIO;
}
dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
dprintk(verbose, DST_CA_INFO, 1, " ================================ CI Module Application Info ======================================");
dprintk(verbose, DST_CA_INFO, 1, " Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]",
state->messages[7], (state->messages[8] << 8) | state->messages[9],
(state->messages[10] << 8) | state->messages[11], __func__, (char *)(&state->messages[12]));
dprintk(verbose, DST_CA_INFO, 1, " ==================================================================================================");
// Transform dst message to correct application_info message
length = state->messages[5];
str_length = length - 6;
if (str_length < 0) {
str_length = 0;
dprintk(verbose, DST_CA_ERROR, 1, "Invalid string length returned in ca_get_app_info(). Recovering.");
}
// First, the command and length fields
put_command_and_length(&state->messages[0], CA_APP_INFO, length);
// Copy application_type, application_manufacturer and manufacturer_code
memmove(&state->messages[4], &state->messages[7], 5);
// Set string length and copy string
state->messages[9] = str_length;
memmove(&state->messages[10], &state->messages[12], str_length);
return 0;
}
static int ca_get_ca_info(struct dst_state *state)
{
int srcPtr, dstPtr, i, num_ids;
static u8 slot_command[8] = {0x07, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0xff};
const int in_system_id_pos = 8, out_system_id_pos = 4, in_num_ids_pos = 7;
put_checksum(&slot_command[0], slot_command[0]);
if ((dst_put_ci(state, slot_command, sizeof (slot_command), state->messages, GET_REPLY)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
return -EIO;
}
dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
// Print raw data
dprintk(verbose, DST_CA_INFO, 0, " DST data = [");
for (i = 0; i < state->messages[0] + 1; i++) {
dprintk(verbose, DST_CA_INFO, 0, " 0x%02x", state->messages[i]);
}
dprintk(verbose, DST_CA_INFO, 0, "]\n");
// Set the command and length of the output
num_ids = state->messages[in_num_ids_pos];
if (num_ids >= 100) {
num_ids = 100;
dprintk(verbose, DST_CA_ERROR, 1, "Invalid number of ids (>100). Recovering.");
}
put_command_and_length(&state->messages[0], CA_INFO, num_ids * 2);
dprintk(verbose, DST_CA_INFO, 0, " CA_INFO = [");
srcPtr = in_system_id_pos;
dstPtr = out_system_id_pos;
for(i = 0; i < num_ids; i++) {
dprintk(verbose, DST_CA_INFO, 0, " 0x%02x%02x", state->messages[srcPtr + 0], state->messages[srcPtr + 1]);
// Append to output
state->messages[dstPtr + 0] = state->messages[srcPtr + 0];
state->messages[dstPtr + 1] = state->messages[srcPtr + 1];
srcPtr += 2;
dstPtr += 2;
}
dprintk(verbose, DST_CA_INFO, 0, "]\n");
return 0;
}
static int ca_get_slot_caps(struct dst_state *state, struct ca_caps *p_ca_caps, void __user *arg)
{
int i;
u8 slot_cap[256];
static u8 slot_command[8] = {0x07, 0x40, 0x02, 0x00, 0x02, 0x00, 0x00, 0xff};
put_checksum(&slot_command[0], slot_command[0]);
if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_cap, GET_REPLY)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
return -EIO;
}
dprintk(verbose, DST_CA_NOTICE, 1, " -->dst_put_ci SUCCESS !");
/* Will implement the rest soon */
dprintk(verbose, DST_CA_INFO, 1, " Slot cap = [%d]", slot_cap[7]);
dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
for (i = 0; i < slot_cap[0] + 1; i++)
dprintk(verbose, DST_CA_INFO, 0, " %d", slot_cap[i]);
dprintk(verbose, DST_CA_INFO, 0, "\n");
p_ca_caps->slot_num = 1;
p_ca_caps->slot_type = 1;
p_ca_caps->descr_num = slot_cap[7];
p_ca_caps->descr_type = 1;
if (copy_to_user(arg, p_ca_caps, sizeof (struct ca_caps)))
return -EFAULT;
return 0;
}
/* Need some more work */
static int ca_get_slot_descr(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
{
return -EOPNOTSUPP;
}
static int ca_get_slot_info(struct dst_state *state, struct ca_slot_info *p_ca_slot_info, void __user *arg)
{
int i;
static u8 slot_command[8] = {0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
u8 *slot_info = state->messages;
put_checksum(&slot_command[0], 7);
if ((dst_put_ci(state, slot_command, sizeof (slot_command), slot_info, GET_REPLY)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->dst_put_ci FAILED !");
return -EIO;
}
dprintk(verbose, DST_CA_INFO, 1, " -->dst_put_ci SUCCESS !");
/* Will implement the rest soon */
dprintk(verbose, DST_CA_INFO, 1, " Slot info = [%d]", slot_info[3]);
dprintk(verbose, DST_CA_INFO, 0, "===================================\n");
for (i = 0; i < 8; i++)
dprintk(verbose, DST_CA_INFO, 0, " %d", slot_info[i]);
dprintk(verbose, DST_CA_INFO, 0, "\n");
if (slot_info[4] & 0x80) {
p_ca_slot_info->flags = CA_CI_MODULE_PRESENT;
p_ca_slot_info->num = 1;
p_ca_slot_info->type = CA_CI;
} else if (slot_info[4] & 0x40) {
p_ca_slot_info->flags = CA_CI_MODULE_READY;
p_ca_slot_info->num = 1;
p_ca_slot_info->type = CA_CI;
} else
p_ca_slot_info->flags = 0;
if (copy_to_user(arg, p_ca_slot_info, sizeof (struct ca_slot_info)))
return -EFAULT;
return 0;
}
static int ca_get_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
{
u8 i = 0;
u32 command = 0;
if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg)))
return -EFAULT;
dprintk(verbose, DST_CA_NOTICE, 1, " Message = [%*ph]",
3, p_ca_message->msg);
for (i = 0; i < 3; i++) {
command = command | p_ca_message->msg[i];
if (i < 2)
command = command << 8;
}
dprintk(verbose, DST_CA_NOTICE, 1, " Command=[0x%x]", command);
switch (command) {
case CA_APP_INFO:
memcpy(p_ca_message->msg, state->messages, 128);
if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
return -EFAULT;
break;
case CA_INFO:
memcpy(p_ca_message->msg, state->messages, 128);
if (copy_to_user(arg, p_ca_message, sizeof (struct ca_msg)) )
return -EFAULT;
break;
}
return 0;
}
static int handle_dst_tag(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u32 length)
{
if (state->dst_hw_cap & DST_TYPE_HAS_SESSION) {
hw_buffer->msg[2] = p_ca_message->msg[1]; /* MSB */
hw_buffer->msg[3] = p_ca_message->msg[2]; /* LSB */
} else {
if (length > 247) {
dprintk(verbose, DST_CA_ERROR, 1, " Message too long ! *** Bailing Out *** !");
return -EIO;
}
hw_buffer->msg[0] = (length & 0xff) + 7;
hw_buffer->msg[1] = 0x40;
hw_buffer->msg[2] = 0x03;
hw_buffer->msg[3] = 0x00;
hw_buffer->msg[4] = 0x03;
hw_buffer->msg[5] = length & 0xff;
hw_buffer->msg[6] = 0x00;
/*
* Need to compute length for EN50221 section 8.3.2, for the time being
* assuming 8.3.2 is not applicable
*/
memcpy(&hw_buffer->msg[7], &p_ca_message->msg[4], length);
}
return 0;
}
static int write_to_8820(struct dst_state *state, struct ca_msg *hw_buffer, u8 length, u8 reply)
{
if ((dst_put_ci(state, hw_buffer->msg, length, hw_buffer->msg, reply)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " DST-CI Command failed.");
dprintk(verbose, DST_CA_NOTICE, 1, " Resetting DST.");
rdc_reset_state(state);
return -EIO;
}
dprintk(verbose, DST_CA_NOTICE, 1, " DST-CI Command success.");
return 0;
}
static u32 asn_1_decode(u8 *asn_1_array)
{
u8 length_field = 0, word_count = 0, count = 0;
u32 length = 0;
length_field = asn_1_array[0];
dprintk(verbose, DST_CA_DEBUG, 1, " Length field=[%02x]", length_field);
if (length_field < 0x80) {
length = length_field & 0x7f;
dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%02x]\n", length);
} else {
word_count = length_field & 0x7f;
for (count = 0; count < word_count; count++) {
length = length << 8;
length += asn_1_array[count + 1];
dprintk(verbose, DST_CA_DEBUG, 1, " Length=[%04x]", length);
}
}
return length;
}
static int debug_string(u8 *msg, u32 length, u32 offset)
{
u32 i;
dprintk(verbose, DST_CA_DEBUG, 0, " String=[ ");
for (i = offset; i < length; i++)
dprintk(verbose, DST_CA_DEBUG, 0, "%02x ", msg[i]);
dprintk(verbose, DST_CA_DEBUG, 0, "]\n");
return 0;
}
static int ca_set_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer, u8 reply, u8 query)
{
u32 length = 0;
u8 tag_length = 8;
length = asn_1_decode(&p_ca_message->msg[3]);
dprintk(verbose, DST_CA_DEBUG, 1, " CA Message length=[%d]", length);
debug_string(&p_ca_message->msg[4], length, 0); /* length is excluding tag & length */
memset(hw_buffer->msg, '\0', length);
handle_dst_tag(state, p_ca_message, hw_buffer, length);
put_checksum(hw_buffer->msg, hw_buffer->msg[0]);
debug_string(hw_buffer->msg, (length + tag_length), 0); /* tags too */
write_to_8820(state, hw_buffer, (length + tag_length), reply);
return 0;
}
/* Board supports CA PMT reply ? */
static int dst_check_ca_pmt(struct dst_state *state, struct ca_msg *p_ca_message, struct ca_msg *hw_buffer)
{
int ca_pmt_reply_test = 0;
/* Do test board */
/* Not there yet but soon */
/* CA PMT Reply capable */
if (ca_pmt_reply_test) {
if ((ca_set_pmt(state, p_ca_message, hw_buffer, 1, GET_REPLY)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
return -EIO;
}
/* Process CA PMT Reply */
/* will implement soon */
dprintk(verbose, DST_CA_ERROR, 1, " Not there yet");
}
/* CA PMT Reply not capable */
if (!ca_pmt_reply_test) {
if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, NO_REPLY)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " ca_set_pmt.. failed !");
return -EIO;
}
dprintk(verbose, DST_CA_NOTICE, 1, " ca_set_pmt.. success !");
/* put a dummy message */
}
return 0;
}
static int ca_send_message(struct dst_state *state, struct ca_msg *p_ca_message, void __user *arg)
{
int i;
u32 command;
struct ca_msg *hw_buffer;
int result = 0;
hw_buffer = kmalloc(sizeof(*hw_buffer), GFP_KERNEL);
if (!hw_buffer)
return -ENOMEM;
dprintk(verbose, DST_CA_DEBUG, 1, " ");
if (copy_from_user(p_ca_message, arg, sizeof (struct ca_msg))) {
result = -EFAULT;
goto free_mem_and_exit;
}
/* EN50221 tag */
command = 0;
for (i = 0; i < 3; i++) {
command = command | p_ca_message->msg[i];
if (i < 2)
command = command << 8;
}
dprintk(verbose, DST_CA_DEBUG, 1, " Command=[0x%x]\n", command);
switch (command) {
case CA_PMT:
dprintk(verbose, DST_CA_DEBUG, 1, "Command = SEND_CA_PMT");
if ((ca_set_pmt(state, p_ca_message, hw_buffer, 0, 0)) < 0) { // code simplification started
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT Failed !");
result = -1;
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT Success !");
break;
case CA_PMT_REPLY:
dprintk(verbose, DST_CA_INFO, 1, "Command = CA_PMT_REPLY");
/* Have to handle the 2 basic types of cards here */
if ((dst_check_ca_pmt(state, p_ca_message, hw_buffer)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_PMT_REPLY Failed !");
result = -1;
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_PMT_REPLY Success !");
break;
case CA_APP_INFO_ENQUIRY: // only for debugging
dprintk(verbose, DST_CA_INFO, 1, " Getting Cam Application information");
if ((ca_get_app_info(state)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_APP_INFO_ENQUIRY Failed !");
result = -1;
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_APP_INFO_ENQUIRY Success !");
break;
case CA_INFO_ENQUIRY:
dprintk(verbose, DST_CA_INFO, 1, " Getting CA Information");
if ((ca_get_ca_info(state)) < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_INFO_ENQUIRY Failed !");
result = -1;
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_INFO_ENQUIRY Success !");
break;
}
free_mem_and_exit:
kfree (hw_buffer);
return result;
}
static long dst_ca_ioctl(struct file *file, unsigned int cmd, unsigned long ioctl_arg)
{
struct dvb_device *dvbdev;
struct dst_state *state;
struct ca_slot_info *p_ca_slot_info;
struct ca_caps *p_ca_caps;
struct ca_msg *p_ca_message;
void __user *arg = (void __user *)ioctl_arg;
int result = 0;
mutex_lock(&dst_ca_mutex);
dvbdev = file->private_data;
state = dvbdev->priv;
p_ca_message = kmalloc(sizeof (struct ca_msg), GFP_KERNEL);
p_ca_slot_info = kmalloc(sizeof (struct ca_slot_info), GFP_KERNEL);
p_ca_caps = kmalloc(sizeof (struct ca_caps), GFP_KERNEL);
if (!p_ca_message || !p_ca_slot_info || !p_ca_caps) {
result = -ENOMEM;
goto free_mem_and_exit;
}
/* We have now only the standard ioctl's, the driver is upposed to handle internals. */
switch (cmd) {
case CA_SEND_MSG:
dprintk(verbose, DST_CA_INFO, 1, " Sending message");
result = ca_send_message(state, p_ca_message, arg);
if (result < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_SEND_MSG Failed !");
goto free_mem_and_exit;
}
break;
case CA_GET_MSG:
dprintk(verbose, DST_CA_INFO, 1, " Getting message");
result = ca_get_message(state, p_ca_message, arg);
if (result < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_MSG Failed !");
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_MSG Success !");
break;
case CA_RESET:
dprintk(verbose, DST_CA_ERROR, 1, " Resetting DST");
dst_error_bailout(state);
msleep(4000);
break;
case CA_GET_SLOT_INFO:
dprintk(verbose, DST_CA_INFO, 1, " Getting Slot info");
result = ca_get_slot_info(state, p_ca_slot_info, arg);
if (result < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_SLOT_INFO Failed !");
result = -1;
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_SLOT_INFO Success !");
break;
case CA_GET_CAP:
dprintk(verbose, DST_CA_INFO, 1, " Getting Slot capabilities");
result = ca_get_slot_caps(state, p_ca_caps, arg);
if (result < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_CAP Failed !");
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_CAP Success !");
break;
case CA_GET_DESCR_INFO:
dprintk(verbose, DST_CA_INFO, 1, " Getting descrambler description");
result = ca_get_slot_descr(state, p_ca_message, arg);
if (result < 0) {
dprintk(verbose, DST_CA_ERROR, 1, " -->CA_GET_DESCR_INFO Failed !");
goto free_mem_and_exit;
}
dprintk(verbose, DST_CA_INFO, 1, " -->CA_GET_DESCR_INFO Success !");
break;
default:
result = -EOPNOTSUPP;
}
free_mem_and_exit:
kfree (p_ca_message);
kfree (p_ca_slot_info);
kfree (p_ca_caps);
mutex_unlock(&dst_ca_mutex);
return result;
}
static int dst_ca_open(struct inode *inode, struct file *file)
{
dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file);
return 0;
}
static int dst_ca_release(struct inode *inode, struct file *file)
{
dprintk(verbose, DST_CA_DEBUG, 1, " Device closed.");
return 0;
}
static ssize_t dst_ca_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
{
dprintk(verbose, DST_CA_DEBUG, 1, " Device read.");
return 0;
}
static ssize_t dst_ca_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
{
dprintk(verbose, DST_CA_DEBUG, 1, " Device write.");
return 0;
}
static const struct file_operations dst_ca_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = dst_ca_ioctl,
.open = dst_ca_open,
.release = dst_ca_release,
.read = dst_ca_read,
.write = dst_ca_write,
.llseek = noop_llseek,
};
static struct dvb_device dvbdev_ca = {
.priv = NULL,
.users = 1,
.readers = 1,
.writers = 1,
.fops = &dst_ca_fops
};
struct dvb_device *dst_ca_attach(struct dst_state *dst, struct dvb_adapter *dvb_adapter)
{
struct dvb_device *dvbdev;
dprintk(verbose, DST_CA_ERROR, 1, "registering DST-CA device");
if (dvb_register_device(dvb_adapter, &dvbdev, &dvbdev_ca, dst,
DVB_DEVICE_CA, 0) == 0) {
dst->dst_ca = dvbdev;
return dst->dst_ca;
}
return NULL;
}
EXPORT_SYMBOL_GPL(dst_ca_attach);
MODULE_DESCRIPTION("DST DVB-S/T/C Combo CA driver");
MODULE_AUTHOR("Manu Abraham");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/bt8xx/dst_ca.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* bt878.c: part of the driver for the Pinnacle PCTV Sat DVB PCI card
*
* Copyright (C) 2002 Peter Hettkamp <[email protected]>
*
* large parts based on the bttv driver
* Copyright (C) 1996,97,98 Ralph Metzler ([email protected])
* & Marcus Metzler ([email protected])
* (c) 1999,2000 Gerd Knorr <[email protected]>
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/pgtable.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <asm/page.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <media/dmxdev.h>
#include <media/dvbdev.h>
#include "bt878.h"
#include "dst_priv.h"
/**************************************/
/* Miscellaneous utility definitions */
/**************************************/
static unsigned int bt878_verbose = 1;
static unsigned int bt878_debug;
module_param_named(verbose, bt878_verbose, int, 0444);
MODULE_PARM_DESC(verbose,
"verbose startup messages, default is 1 (yes)");
module_param_named(debug, bt878_debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off debugging, default is 0 (off).");
int bt878_num;
struct bt878 bt878[BT878_MAX];
EXPORT_SYMBOL(bt878_num);
EXPORT_SYMBOL(bt878);
#define btwrite(dat,adr) bmtwrite((dat), (bt->bt878_mem+(adr)))
#define btread(adr) bmtread(bt->bt878_mem+(adr))
#define btand(dat,adr) btwrite((dat) & btread(adr), adr)
#define btor(dat,adr) btwrite((dat) | btread(adr), adr)
#define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
#if defined(dprintk)
#undef dprintk
#endif
#define dprintk(fmt, arg...) \
do { \
if (bt878_debug) \
printk(KERN_DEBUG fmt, ##arg); \
} while (0)
static void bt878_mem_free(struct bt878 *bt)
{
if (bt->buf_cpu) {
dma_free_coherent(&bt->dev->dev, bt->buf_size, bt->buf_cpu,
bt->buf_dma);
bt->buf_cpu = NULL;
}
if (bt->risc_cpu) {
dma_free_coherent(&bt->dev->dev, bt->risc_size, bt->risc_cpu,
bt->risc_dma);
bt->risc_cpu = NULL;
}
}
static int bt878_mem_alloc(struct bt878 *bt)
{
if (!bt->buf_cpu) {
bt->buf_size = 128 * 1024;
bt->buf_cpu = dma_alloc_coherent(&bt->dev->dev, bt->buf_size,
&bt->buf_dma, GFP_KERNEL);
if (!bt->buf_cpu)
return -ENOMEM;
}
if (!bt->risc_cpu) {
bt->risc_size = PAGE_SIZE;
bt->risc_cpu = dma_alloc_coherent(&bt->dev->dev, bt->risc_size,
&bt->risc_dma, GFP_KERNEL);
if (!bt->risc_cpu) {
bt878_mem_free(bt);
return -ENOMEM;
}
}
return 0;
}
/* RISC instructions */
#define RISC_WRITE (0x01 << 28)
#define RISC_JUMP (0x07 << 28)
#define RISC_SYNC (0x08 << 28)
/* RISC bits */
#define RISC_WR_SOL (1 << 27)
#define RISC_WR_EOL (1 << 26)
#define RISC_IRQ (1 << 24)
#define RISC_STATUS(status) ((((~status) & 0x0F) << 20) | ((status & 0x0F) << 16))
#define RISC_SYNC_RESYNC (1 << 15)
#define RISC_SYNC_FM1 0x06
#define RISC_SYNC_VRO 0x0C
#define RISC_FLUSH() bt->risc_pos = 0
#define RISC_INSTR(instr) bt->risc_cpu[bt->risc_pos++] = cpu_to_le32(instr)
static int bt878_make_risc(struct bt878 *bt)
{
bt->block_bytes = bt->buf_size >> 4;
bt->block_count = 1 << 4;
bt->line_bytes = bt->block_bytes;
bt->line_count = bt->block_count;
while (bt->line_bytes > 4095) {
bt->line_bytes >>= 1;
bt->line_count <<= 1;
}
if (bt->line_count > 255) {
printk(KERN_ERR "bt878: buffer size error!\n");
return -EINVAL;
}
return 0;
}
static void bt878_risc_program(struct bt878 *bt, u32 op_sync_orin)
{
u32 buf_pos = 0;
u32 line;
RISC_FLUSH();
RISC_INSTR(RISC_SYNC | RISC_SYNC_FM1 | op_sync_orin);
RISC_INSTR(0);
dprintk("bt878: risc len lines %u, bytes per line %u\n",
bt->line_count, bt->line_bytes);
for (line = 0; line < bt->line_count; line++) {
// At the beginning of every block we issue an IRQ with previous (finished) block number set
if (!(buf_pos % bt->block_bytes))
RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL |
RISC_IRQ |
RISC_STATUS(((buf_pos /
bt->block_bytes) +
(bt->block_count -
1)) %
bt->block_count) | bt->
line_bytes);
else
RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL |
bt->line_bytes);
RISC_INSTR(bt->buf_dma + buf_pos);
buf_pos += bt->line_bytes;
}
RISC_INSTR(RISC_SYNC | op_sync_orin | RISC_SYNC_VRO);
RISC_INSTR(0);
RISC_INSTR(RISC_JUMP);
RISC_INSTR(bt->risc_dma);
btwrite((bt->line_count << 16) | bt->line_bytes, BT878_APACK_LEN);
}
/*****************************/
/* Start/Stop grabbing funcs */
/*****************************/
void bt878_start(struct bt878 *bt, u32 controlreg, u32 op_sync_orin,
u32 irq_err_ignore)
{
u32 int_mask;
dprintk("bt878 debug: bt878_start (ctl=%8.8x)\n", controlreg);
/* complete the writing of the risc dma program now we have
* the card specifics
*/
bt878_risc_program(bt, op_sync_orin);
controlreg &= ~0x1f;
controlreg |= 0x1b;
btwrite(bt->risc_dma, BT878_ARISC_START);
/* original int mask had :
* 6 2 8 4 0
* 1111 1111 1000 0000 0000
* SCERR|OCERR|PABORT|RIPERR|FDSR|FTRGT|FBUS|RISCI
* Hacked for DST to:
* SCERR | OCERR | FDSR | FTRGT | FBUS | RISCI
*/
int_mask = BT878_ASCERR | BT878_AOCERR | BT878_APABORT |
BT878_ARIPERR | BT878_APPERR | BT878_AFDSR | BT878_AFTRGT |
BT878_AFBUS | BT878_ARISCI;
/* ignore pesky bits */
int_mask &= ~irq_err_ignore;
btwrite(int_mask, BT878_AINT_MASK);
btwrite(controlreg, BT878_AGPIO_DMA_CTL);
}
void bt878_stop(struct bt878 *bt)
{
u32 stat;
int i = 0;
dprintk("bt878 debug: bt878_stop\n");
btwrite(0, BT878_AINT_MASK);
btand(~0x13, BT878_AGPIO_DMA_CTL);
do {
stat = btread(BT878_AINT_STAT);
if (!(stat & BT878_ARISC_EN))
break;
i++;
} while (i < 500);
dprintk("bt878(%d) debug: bt878_stop, i=%d, stat=0x%8.8x\n",
bt->nr, i, stat);
}
EXPORT_SYMBOL(bt878_start);
EXPORT_SYMBOL(bt878_stop);
/*****************************/
/* Interrupt service routine */
/*****************************/
static irqreturn_t bt878_irq(int irq, void *dev_id)
{
u32 stat, astat, mask;
int count;
struct bt878 *bt;
bt = (struct bt878 *) dev_id;
count = 0;
while (1) {
stat = btread(BT878_AINT_STAT);
mask = btread(BT878_AINT_MASK);
if (!(astat = (stat & mask)))
return IRQ_NONE; /* this interrupt is not for me */
/* dprintk("bt878(%d) debug: irq count %d, stat 0x%8.8x, mask 0x%8.8x\n",bt->nr,count,stat,mask); */
btwrite(astat, BT878_AINT_STAT); /* try to clear interrupt condition */
if (astat & (BT878_ASCERR | BT878_AOCERR)) {
if (bt878_verbose) {
printk(KERN_INFO
"bt878(%d): irq%s%s risc_pc=%08x\n",
bt->nr,
(astat & BT878_ASCERR) ? " SCERR" :
"",
(astat & BT878_AOCERR) ? " OCERR" :
"", btread(BT878_ARISC_PC));
}
}
if (astat & (BT878_APABORT | BT878_ARIPERR | BT878_APPERR)) {
if (bt878_verbose) {
printk(KERN_INFO
"bt878(%d): irq%s%s%s risc_pc=%08x\n",
bt->nr,
(astat & BT878_APABORT) ? " PABORT" :
"",
(astat & BT878_ARIPERR) ? " RIPERR" :
"",
(astat & BT878_APPERR) ? " PPERR" :
"", btread(BT878_ARISC_PC));
}
}
if (astat & (BT878_AFDSR | BT878_AFTRGT | BT878_AFBUS)) {
if (bt878_verbose) {
printk(KERN_INFO
"bt878(%d): irq%s%s%s risc_pc=%08x\n",
bt->nr,
(astat & BT878_AFDSR) ? " FDSR" : "",
(astat & BT878_AFTRGT) ? " FTRGT" :
"",
(astat & BT878_AFBUS) ? " FBUS" : "",
btread(BT878_ARISC_PC));
}
}
if (astat & BT878_ARISCI) {
bt->finished_block = (stat & BT878_ARISCS) >> 28;
if (bt->tasklet.callback)
tasklet_schedule(&bt->tasklet);
break;
}
count++;
if (count > 20) {
btwrite(0, BT878_AINT_MASK);
printk(KERN_ERR
"bt878(%d): IRQ lockup, cleared int mask\n",
bt->nr);
break;
}
}
return IRQ_HANDLED;
}
int
bt878_device_control(struct bt878 *bt, unsigned int cmd, union dst_gpio_packet *mp)
{
int retval;
retval = 0;
if (mutex_lock_interruptible(&bt->gpio_lock))
return -ERESTARTSYS;
/* special gpio signal */
switch (cmd) {
case DST_IG_ENABLE:
// dprintk("dvb_bt8xx: dst enable mask 0x%02x enb 0x%02x \n", mp->dstg.enb.mask, mp->dstg.enb.enable);
retval = bttv_gpio_enable(bt->bttv_nr,
mp->enb.mask,
mp->enb.enable);
break;
case DST_IG_WRITE:
// dprintk("dvb_bt8xx: dst write gpio mask 0x%02x out 0x%02x\n", mp->dstg.outp.mask, mp->dstg.outp.highvals);
retval = bttv_write_gpio(bt->bttv_nr,
mp->outp.mask,
mp->outp.highvals);
break;
case DST_IG_READ:
/* read */
retval = bttv_read_gpio(bt->bttv_nr, &mp->rd.value);
// dprintk("dvb_bt8xx: dst read gpio 0x%02x\n", (unsigned)mp->dstg.rd.value);
break;
case DST_IG_TS:
/* Set packet size */
bt->TS_Size = mp->psize;
break;
default:
retval = -EINVAL;
break;
}
mutex_unlock(&bt->gpio_lock);
return retval;
}
EXPORT_SYMBOL(bt878_device_control);
#define BROOKTREE_878_DEVICE(vend, dev, name) \
{ \
.vendor = PCI_VENDOR_ID_BROOKTREE, \
.device = PCI_DEVICE_ID_BROOKTREE_878, \
.subvendor = (vend), .subdevice = (dev), \
.driver_data = (unsigned long) name \
}
static const struct pci_device_id bt878_pci_tbl[] = {
BROOKTREE_878_DEVICE(0x0071, 0x0101, "Nebula Electronics DigiTV"),
BROOKTREE_878_DEVICE(0x1461, 0x0761, "AverMedia AverTV DVB-T 761"),
BROOKTREE_878_DEVICE(0x11bd, 0x001c, "Pinnacle PCTV Sat"),
BROOKTREE_878_DEVICE(0x11bd, 0x0026, "Pinnacle PCTV SAT CI"),
BROOKTREE_878_DEVICE(0x1822, 0x0001, "Twinhan VisionPlus DVB"),
BROOKTREE_878_DEVICE(0x270f, 0xfc00,
"ChainTech digitop DST-1000 DVB-S"),
BROOKTREE_878_DEVICE(0x1461, 0x0771, "AVermedia AverTV DVB-T 771"),
BROOKTREE_878_DEVICE(0x18ac, 0xdb10, "DViCO FusionHDTV DVB-T Lite"),
BROOKTREE_878_DEVICE(0x18ac, 0xdb11, "Ultraview DVB-T Lite"),
BROOKTREE_878_DEVICE(0x18ac, 0xd500, "DViCO FusionHDTV 5 Lite"),
BROOKTREE_878_DEVICE(0x7063, 0x2000, "pcHDTV HD-2000 TV"),
BROOKTREE_878_DEVICE(0x1822, 0x0026, "DNTV Live! Mini"),
{ }
};
MODULE_DEVICE_TABLE(pci, bt878_pci_tbl);
static const char * card_name(const struct pci_device_id *id)
{
return id->driver_data ? (const char *)id->driver_data : "Unknown";
}
/***********************/
/* PCI device handling */
/***********************/
static int bt878_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
{
int result = 0;
unsigned char lat;
struct bt878 *bt;
unsigned int cardid;
printk(KERN_INFO "bt878: Bt878 AUDIO function found (%d).\n",
bt878_num);
if (bt878_num >= BT878_MAX) {
printk(KERN_ERR "bt878: Too many devices inserted\n");
return -ENOMEM;
}
if (pci_enable_device(dev))
return -EIO;
cardid = dev->subsystem_device << 16;
cardid |= dev->subsystem_vendor;
printk(KERN_INFO "%s: card id=[0x%x],[ %s ] has DVB functions.\n",
__func__, cardid, card_name(pci_id));
bt = &bt878[bt878_num];
bt->dev = dev;
bt->nr = bt878_num;
bt->shutdown = 0;
bt->id = dev->device;
bt->irq = dev->irq;
bt->bt878_adr = pci_resource_start(dev, 0);
if (!request_mem_region(pci_resource_start(dev, 0),
pci_resource_len(dev, 0), "bt878")) {
result = -EBUSY;
goto fail0;
}
bt->revision = dev->revision;
pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
printk(KERN_INFO "bt878(%d): Bt%x (rev %d) at %02x:%02x.%x, ",
bt878_num, bt->id, bt->revision, dev->bus->number,
PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
printk("irq: %d, latency: %d, memory: 0x%lx\n",
bt->irq, lat, bt->bt878_adr);
#ifdef __sparc__
bt->bt878_mem = (unsigned char *) bt->bt878_adr;
#else
bt->bt878_mem = ioremap(bt->bt878_adr, 0x1000);
#endif
/* clear interrupt mask */
btwrite(0, BT848_INT_MASK);
result = request_irq(bt->irq, bt878_irq,
IRQF_SHARED, "bt878", (void *) bt);
if (result == -EINVAL) {
printk(KERN_ERR "bt878(%d): Bad irq number or handler\n",
bt878_num);
goto fail1;
}
if (result == -EBUSY) {
printk(KERN_ERR
"bt878(%d): IRQ %d busy, change your PnP config in BIOS\n",
bt878_num, bt->irq);
goto fail1;
}
if (result < 0)
goto fail1;
pci_set_master(dev);
pci_set_drvdata(dev, bt);
if ((result = bt878_mem_alloc(bt))) {
printk(KERN_ERR "bt878: failed to allocate memory!\n");
goto fail2;
}
bt878_make_risc(bt);
btwrite(0, BT878_AINT_MASK);
bt878_num++;
if (!bt->tasklet.func)
tasklet_disable(&bt->tasklet);
return 0;
fail2:
free_irq(bt->irq, bt);
fail1:
release_mem_region(pci_resource_start(bt->dev, 0),
pci_resource_len(bt->dev, 0));
fail0:
pci_disable_device(dev);
return result;
}
static void bt878_remove(struct pci_dev *pci_dev)
{
u8 command;
struct bt878 *bt = pci_get_drvdata(pci_dev);
if (bt878_verbose)
printk(KERN_INFO "bt878(%d): unloading\n", bt->nr);
/* turn off all capturing, DMA and IRQs */
btand(~0x13, BT878_AGPIO_DMA_CTL);
/* first disable interrupts before unmapping the memory! */
btwrite(0, BT878_AINT_MASK);
btwrite(~0U, BT878_AINT_STAT);
/* disable PCI bus-mastering */
pci_read_config_byte(bt->dev, PCI_COMMAND, &command);
/* Should this be &=~ ?? */
command &= ~PCI_COMMAND_MASTER;
pci_write_config_byte(bt->dev, PCI_COMMAND, command);
free_irq(bt->irq, bt);
printk(KERN_DEBUG "bt878_mem: 0x%p.\n", bt->bt878_mem);
if (bt->bt878_mem)
iounmap(bt->bt878_mem);
release_mem_region(pci_resource_start(bt->dev, 0),
pci_resource_len(bt->dev, 0));
/* wake up any waiting processes
because shutdown flag is set, no new processes (in this queue)
are expected
*/
bt->shutdown = 1;
bt878_mem_free(bt);
pci_disable_device(pci_dev);
return;
}
static struct pci_driver bt878_pci_driver = {
.name = "bt878",
.id_table = bt878_pci_tbl,
.probe = bt878_probe,
.remove = bt878_remove,
};
/*******************************/
/* Module management functions */
/*******************************/
static int __init bt878_init_module(void)
{
bt878_num = 0;
printk(KERN_INFO "bt878: AUDIO driver version %d.%d.%d loaded\n",
(BT878_VERSION_CODE >> 16) & 0xff,
(BT878_VERSION_CODE >> 8) & 0xff,
BT878_VERSION_CODE & 0xff);
return pci_register_driver(&bt878_pci_driver);
}
static void __exit bt878_cleanup_module(void)
{
pci_unregister_driver(&bt878_pci_driver);
}
module_init(bt878_init_module);
module_exit(bt878_cleanup_module);
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/bt8xx/bt878.c |
// SPDX-License-Identifier: GPL-2.0
/*
* Handlers for board audio hooks, split from bttv-cards
*
* Copyright (c) 2006 Mauro Carvalho Chehab <[email protected]>
*/
#include "bttv-audio-hook.h"
#include <linux/delay.h>
/* ----------------------------------------------------------------------- */
/* winview */
void winview_volume(struct bttv *btv, __u16 volume)
{
/* PT2254A programming Jon Tombs, [email protected] */
int bits_out, loops, vol, data;
/* 32 levels logarithmic */
vol = 32 - ((volume>>11));
/* units */
bits_out = (PT2254_DBS_IN_2>>(vol%5));
/* tens */
bits_out |= (PT2254_DBS_IN_10>>(vol/5));
bits_out |= PT2254_L_CHANNEL | PT2254_R_CHANNEL;
data = gpio_read();
data &= ~(WINVIEW_PT2254_CLK| WINVIEW_PT2254_DATA|
WINVIEW_PT2254_STROBE);
for (loops = 17; loops >= 0 ; loops--) {
if (bits_out & (1<<loops))
data |= WINVIEW_PT2254_DATA;
else
data &= ~WINVIEW_PT2254_DATA;
gpio_write(data);
udelay(5);
data |= WINVIEW_PT2254_CLK;
gpio_write(data);
udelay(5);
data &= ~WINVIEW_PT2254_CLK;
gpio_write(data);
}
data |= WINVIEW_PT2254_STROBE;
data &= ~WINVIEW_PT2254_DATA;
gpio_write(data);
udelay(10);
data &= ~WINVIEW_PT2254_STROBE;
gpio_write(data);
}
/* ----------------------------------------------------------------------- */
/* mono/stereo control for various cards (which don't use i2c chips but */
/* connect something to the GPIO pins */
void gvbctv3pci_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned int con;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
gpio_inout(0x300, 0x300);
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG1:
default:
con = 0x000;
break;
case V4L2_TUNER_MODE_LANG2:
con = 0x300;
break;
case V4L2_TUNER_MODE_STEREO:
con = 0x200;
break;
}
gpio_bits(0x300, con);
}
void gvbctv5pci_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned int val, con;
if (btv->radio_user)
return;
val = gpio_read();
if (set) {
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG2:
con = 0x300;
break;
case V4L2_TUNER_MODE_LANG1_LANG2:
con = 0x100;
break;
default:
con = 0x000;
break;
}
if (con != (val & 0x300)) {
gpio_bits(0x300, con);
if (bttv_gpio)
bttv_gpio_tracking(btv, "gvbctv5pci");
}
} else {
switch (val & 0x70) {
case 0x10:
t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
t->audmode = V4L2_TUNER_MODE_LANG1_LANG2;
break;
case 0x30:
t->rxsubchans = V4L2_TUNER_SUB_LANG2;
t->audmode = V4L2_TUNER_MODE_LANG1_LANG2;
break;
case 0x50:
t->rxsubchans = V4L2_TUNER_SUB_LANG1;
t->audmode = V4L2_TUNER_MODE_LANG1_LANG2;
break;
case 0x60:
t->rxsubchans = V4L2_TUNER_SUB_STEREO;
t->audmode = V4L2_TUNER_MODE_STEREO;
break;
case 0x70:
t->rxsubchans = V4L2_TUNER_SUB_MONO;
t->audmode = V4L2_TUNER_MODE_MONO;
break;
default:
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
t->audmode = V4L2_TUNER_MODE_LANG1;
}
}
}
/*
* Mario Medina Nussbaum <[email protected]>
* I discover that on BT848_GPIO_DATA address a byte 0xcce enable stereo,
* 0xdde enables mono and 0xccd enables sap
*
* Petr Vandrovec <[email protected]>
* P.S.: At least mask in line above is wrong - GPIO pins 3,2 select
* input/output sound connection, so both must be set for output mode.
*
* Looks like it's needed only for the "tvphone", the "tvphone 98"
* handles this with a tda9840
*
*/
void avermedia_tvphone_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
int val;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG2: /* SAP */
val = 0x02;
break;
case V4L2_TUNER_MODE_STEREO:
val = 0x01;
break;
default:
return;
}
gpio_bits(0x03, val);
if (bttv_gpio)
bttv_gpio_tracking(btv, "avermedia");
}
void avermedia_tv_stereo_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
int val = 0;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG2: /* SAP */
val = 0x01;
break;
case V4L2_TUNER_MODE_STEREO:
val = 0x02;
break;
default:
val = 0;
break;
}
btaor(val, ~0x03, BT848_GPIO_DATA);
if (bttv_gpio)
bttv_gpio_tracking(btv, "avermedia");
}
/* Lifetec 9415 handling */
void lt9415_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
int val = 0;
if (gpio_read() & 0x4000) {
t->audmode = V4L2_TUNER_MODE_MONO;
return;
}
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG2: /* A2 SAP */
val = 0x0080;
break;
case V4L2_TUNER_MODE_STEREO: /* A2 stereo */
val = 0x0880;
break;
default:
val = 0;
break;
}
gpio_bits(0x0880, val);
if (bttv_gpio)
bttv_gpio_tracking(btv, "lt9415");
}
/* TDA9821 on TerraTV+ Bt848, Bt878 */
void terratv_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned int con = 0;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
gpio_inout(0x180000, 0x180000);
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG2:
con = 0x080000;
break;
case V4L2_TUNER_MODE_STEREO:
con = 0x180000;
break;
default:
con = 0;
break;
}
gpio_bits(0x180000, con);
if (bttv_gpio)
bttv_gpio_tracking(btv, "terratv");
}
void winfast2000_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned long val;
if (!set)
return;
/*btor (0xc32000, BT848_GPIO_OUT_EN);*/
switch (t->audmode) {
case V4L2_TUNER_MODE_MONO:
case V4L2_TUNER_MODE_LANG1:
val = 0x420000;
break;
case V4L2_TUNER_MODE_LANG2: /* SAP */
val = 0x410000;
break;
case V4L2_TUNER_MODE_STEREO:
val = 0x020000;
break;
default:
return;
}
gpio_bits(0x430000, val);
if (bttv_gpio)
bttv_gpio_tracking(btv, "winfast2000");
}
/*
* Dariusz Kowalewski <[email protected]>
* sound control for Prolink PV-BT878P+9B (PixelView PlayTV Pro FM+NICAM
* revision 9B has on-board TDA9874A sound decoder).
*
* Note: There are card variants without tda9874a. Forcing the "stereo sound route"
* will mute this cards.
*/
void pvbt878p9b_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned int val = 0;
if (btv->radio_user)
return;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
switch (t->audmode) {
case V4L2_TUNER_MODE_MONO:
val = 0x01;
break;
case V4L2_TUNER_MODE_LANG1:
case V4L2_TUNER_MODE_LANG2:
case V4L2_TUNER_MODE_STEREO:
val = 0x02;
break;
default:
return;
}
gpio_bits(0x03, val);
if (bttv_gpio)
bttv_gpio_tracking(btv, "pvbt878p9b");
}
/*
* Dariusz Kowalewski <[email protected]>
* sound control for FlyVideo 2000S (with tda9874 decoder)
* based on pvbt878p9b_audio() - this is not tested, please fix!!!
*/
void fv2000s_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned int val;
if (btv->radio_user)
return;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
switch (t->audmode) {
case V4L2_TUNER_MODE_MONO:
val = 0x0000;
break;
case V4L2_TUNER_MODE_LANG1:
case V4L2_TUNER_MODE_LANG2:
case V4L2_TUNER_MODE_STEREO:
val = 0x1080; /*-dk-???: 0x0880, 0x0080, 0x1800 ... */
break;
default:
return;
}
gpio_bits(0x1800, val);
if (bttv_gpio)
bttv_gpio_tracking(btv, "fv2000s");
}
/*
* sound control for Canopus WinDVR PCI
* Masaki Suzuki <[email protected]>
*/
void windvr_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned long val;
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
switch (t->audmode) {
case V4L2_TUNER_MODE_MONO:
val = 0x040000;
break;
case V4L2_TUNER_MODE_LANG2:
val = 0x100000;
break;
default:
return;
}
gpio_bits(0x140000, val);
if (bttv_gpio)
bttv_gpio_tracking(btv, "windvr");
}
/*
* sound control for AD-TVK503
* Hiroshi Takekawa <[email protected]>
*/
void adtvk503_audio(struct bttv *btv, struct v4l2_tuner *t, int set)
{
unsigned int con = 0xffffff;
/* btaor(0x1e0000, ~0x1e0000, BT848_GPIO_OUT_EN); */
if (!set) {
/* Not much to do here */
t->audmode = V4L2_TUNER_MODE_LANG1;
t->rxsubchans = V4L2_TUNER_SUB_MONO |
V4L2_TUNER_SUB_STEREO |
V4L2_TUNER_SUB_LANG1 |
V4L2_TUNER_SUB_LANG2;
return;
}
/* btor(***, BT848_GPIO_OUT_EN); */
switch (t->audmode) {
case V4L2_TUNER_MODE_LANG1:
con = 0x00000000;
break;
case V4L2_TUNER_MODE_LANG2:
con = 0x00180000;
break;
case V4L2_TUNER_MODE_STEREO:
con = 0x00000000;
break;
case V4L2_TUNER_MODE_MONO:
con = 0x00060000;
break;
default:
return;
}
gpio_bits(0x1e0000, con);
if (bttv_gpio)
bttv_gpio_tracking(btv, "adtvk503");
}
| linux-master | drivers/media/pci/bt8xx/bttv-audio-hook.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv-i2c.c -- all the i2c code is here
bttv - Bt848 frame grabber driver
Copyright (C) 1996,97,98 Ralph Metzler ([email protected])
& Marcus Metzler ([email protected])
(c) 1999-2003 Gerd Knorr <[email protected]>
(c) 2005 Mauro Carvalho Chehab <[email protected]>
- Multituner support and i2c address binding
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include "bttvp.h"
#include <media/v4l2-common.h>
#include <linux/jiffies.h>
#include <asm/io.h>
static int i2c_debug;
static int i2c_hw;
static int i2c_scan;
module_param(i2c_debug, int, 0644);
MODULE_PARM_DESC(i2c_debug, "configure i2c debug level");
module_param(i2c_hw, int, 0444);
MODULE_PARM_DESC(i2c_hw, "force use of hardware i2c support, instead of software bitbang");
module_param(i2c_scan, int, 0444);
MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
static unsigned int i2c_udelay = 5;
module_param(i2c_udelay, int, 0444);
MODULE_PARM_DESC(i2c_udelay, "soft i2c delay at insmod time, in usecs (should be 5 or higher). Lower value means higher bus speed.");
/* ----------------------------------------------------------------------- */
/* I2C functions - bitbanging adapter (software i2c) */
static void bttv_bit_setscl(void *data, int state)
{
struct bttv *btv = (struct bttv*)data;
if (state)
btv->i2c_state |= 0x02;
else
btv->i2c_state &= ~0x02;
btwrite(btv->i2c_state, BT848_I2C);
btread(BT848_I2C);
}
static void bttv_bit_setsda(void *data, int state)
{
struct bttv *btv = (struct bttv*)data;
if (state)
btv->i2c_state |= 0x01;
else
btv->i2c_state &= ~0x01;
btwrite(btv->i2c_state, BT848_I2C);
btread(BT848_I2C);
}
static int bttv_bit_getscl(void *data)
{
struct bttv *btv = (struct bttv*)data;
int state;
state = btread(BT848_I2C) & 0x02 ? 1 : 0;
return state;
}
static int bttv_bit_getsda(void *data)
{
struct bttv *btv = (struct bttv*)data;
int state;
state = btread(BT848_I2C) & 0x01;
return state;
}
static const struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
.setsda = bttv_bit_setsda,
.setscl = bttv_bit_setscl,
.getsda = bttv_bit_getsda,
.getscl = bttv_bit_getscl,
.udelay = 16,
.timeout = 200,
};
/* ----------------------------------------------------------------------- */
/* I2C functions - hardware i2c */
static u32 functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_SMBUS_EMUL;
}
static int
bttv_i2c_wait_done(struct bttv *btv)
{
int rc = 0;
/* timeout */
if (wait_event_interruptible_timeout(btv->i2c_queue,
btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
rc = -EIO;
if (btv->i2c_done & BT848_INT_RACK)
rc = 1;
btv->i2c_done = 0;
return rc;
}
#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
BT848_I2C_SCL | BT848_I2C_SDA)
static int
bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
{
u32 xmit;
int retval,cnt;
/* sanity checks */
if (0 == msg->len)
return -EINVAL;
/* start, address + first byte */
xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
if (msg->len > 1 || !last)
xmit |= BT878_I2C_NOSTOP;
btwrite(xmit, BT848_I2C);
retval = bttv_i2c_wait_done(btv);
if (retval < 0)
goto err;
if (retval == 0)
goto eio;
if (i2c_debug) {
pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
}
for (cnt = 1; cnt < msg->len; cnt++ ) {
/* following bytes */
xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
if (cnt < msg->len-1 || !last)
xmit |= BT878_I2C_NOSTOP;
btwrite(xmit, BT848_I2C);
retval = bttv_i2c_wait_done(btv);
if (retval < 0)
goto err;
if (retval == 0)
goto eio;
if (i2c_debug)
pr_cont(" %02x", msg->buf[cnt]);
}
if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
pr_cont(">\n");
return msg->len;
eio:
retval = -EIO;
err:
if (i2c_debug)
pr_cont(" ERR: %d\n",retval);
return retval;
}
static int
bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
{
u32 xmit;
u32 cnt;
int retval;
for (cnt = 0; cnt < msg->len; cnt++) {
xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
if (cnt < msg->len-1)
xmit |= BT848_I2C_W3B;
if (cnt < msg->len-1 || !last)
xmit |= BT878_I2C_NOSTOP;
if (cnt)
xmit |= BT878_I2C_NOSTART;
if (i2c_debug) {
if (!(xmit & BT878_I2C_NOSTART))
pr_cont(" <R %02x", (msg->addr << 1) +1);
}
btwrite(xmit, BT848_I2C);
retval = bttv_i2c_wait_done(btv);
if (retval < 0)
goto err;
if (retval == 0)
goto eio;
msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
if (i2c_debug) {
pr_cont(" =%02x", msg->buf[cnt]);
}
if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
pr_cont(" >\n");
}
return msg->len;
eio:
retval = -EIO;
err:
if (i2c_debug)
pr_cont(" ERR: %d\n",retval);
return retval;
}
static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
{
struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
struct bttv *btv = to_bttv(v4l2_dev);
int retval = 0;
int i;
if (i2c_debug)
pr_debug("bt-i2c:");
btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
for (i = 0 ; i < num; i++) {
if (msgs[i].flags & I2C_M_RD) {
/* read */
retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
if (retval < 0)
goto err;
} else {
/* write */
retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
if (retval < 0)
goto err;
}
}
return num;
err:
return retval;
}
static const struct i2c_algorithm bttv_algo = {
.master_xfer = bttv_i2c_xfer,
.functionality = functionality,
};
/* ----------------------------------------------------------------------- */
/* I2C functions - common stuff */
/* read I2C */
int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
{
unsigned char buffer = 0;
if (0 != btv->i2c_rc)
return -1;
if (bttv_verbose && NULL != probe_for)
pr_info("%d: i2c: checking for %s @ 0x%02x... ",
btv->c.nr, probe_for, addr);
btv->i2c_client.addr = addr >> 1;
if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
if (NULL != probe_for) {
if (bttv_verbose)
pr_cont("not found\n");
} else
pr_warn("%d: i2c read 0x%x: error\n",
btv->c.nr, addr);
return -1;
}
if (bttv_verbose && NULL != probe_for)
pr_cont("found\n");
return buffer;
}
/* write I2C */
int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
unsigned char b2, int both)
{
unsigned char buffer[2];
int bytes = both ? 2 : 1;
if (0 != btv->i2c_rc)
return -1;
btv->i2c_client.addr = addr >> 1;
buffer[0] = b1;
buffer[1] = b2;
if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
return -1;
return 0;
}
/* read EEPROM content */
void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
{
memset(eedata, 0, 256);
if (0 != btv->i2c_rc)
return;
btv->i2c_client.addr = addr >> 1;
tveeprom_read(&btv->i2c_client, eedata, 256);
}
static char *i2c_devs[128] = {
[ 0x1c >> 1 ] = "lgdt330x",
[ 0x30 >> 1 ] = "IR (hauppauge)",
[ 0x80 >> 1 ] = "msp34xx",
[ 0x86 >> 1 ] = "tda9887",
[ 0xa0 >> 1 ] = "eeprom",
[ 0xc0 >> 1 ] = "tuner (analog)",
[ 0xc2 >> 1 ] = "tuner (analog)",
};
static void do_i2c_scan(char *name, struct i2c_client *c)
{
unsigned char buf;
int i,rc;
for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
c->addr = i;
rc = i2c_master_recv(c,&buf,0);
if (rc < 0)
continue;
pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
}
}
/* init + register i2c adapter */
int init_bttv_i2c(struct bttv *btv)
{
strscpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
if (i2c_hw)
btv->use_i2c_hw = 1;
if (btv->use_i2c_hw) {
/* bt878 */
strscpy(btv->c.i2c_adap.name, "bt878",
sizeof(btv->c.i2c_adap.name));
btv->c.i2c_adap.algo = &bttv_algo;
} else {
/* bt848 */
/* Prevents usage of invalid delay values */
if (i2c_udelay<5)
i2c_udelay=5;
strscpy(btv->c.i2c_adap.name, "bttv",
sizeof(btv->c.i2c_adap.name));
btv->i2c_algo = bttv_i2c_algo_bit_template;
btv->i2c_algo.udelay = i2c_udelay;
btv->i2c_algo.data = btv;
btv->c.i2c_adap.algo_data = &btv->i2c_algo;
}
btv->c.i2c_adap.owner = THIS_MODULE;
btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
"bt%d #%d [%s]", btv->id, btv->c.nr,
btv->use_i2c_hw ? "hw" : "sw");
i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
btv->i2c_client.adapter = &btv->c.i2c_adap;
if (btv->use_i2c_hw) {
btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
} else {
bttv_bit_setscl(btv,1);
bttv_bit_setsda(btv,1);
btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
}
if (0 == btv->i2c_rc && i2c_scan)
do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
return btv->i2c_rc;
}
int fini_bttv_i2c(struct bttv *btv)
{
if (btv->i2c_rc == 0)
i2c_del_adapter(&btv->c.i2c_adap);
return 0;
}
| linux-master | drivers/media/pci/bt8xx/bttv-i2c.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
btcx-risc.c
bt848/bt878/cx2388x risc code generator.
(c) 2000-03 Gerd Knorr <[email protected]> [SuSE Labs]
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/videodev2.h>
#include <linux/pgtable.h>
#include <asm/page.h>
#include "btcx-risc.h"
static unsigned int btcx_debug;
module_param(btcx_debug, int, 0644);
MODULE_PARM_DESC(btcx_debug,"debug messages, default is 0 (no)");
#define dprintk(fmt, arg...) do { \
if (btcx_debug) \
printk(KERN_DEBUG pr_fmt("%s: " fmt), \
__func__, ##arg); \
} while (0)
/* ---------------------------------------------------------- */
/* allocate/free risc memory */
static int memcnt;
void btcx_riscmem_free(struct pci_dev *pci,
struct btcx_riscmem *risc)
{
if (NULL == risc->cpu)
return;
memcnt--;
dprintk("btcx: riscmem free [%d] dma=%lx\n",
memcnt, (unsigned long)risc->dma);
dma_free_coherent(&pci->dev, risc->size, risc->cpu, risc->dma);
memset(risc,0,sizeof(*risc));
}
int btcx_riscmem_alloc(struct pci_dev *pci,
struct btcx_riscmem *risc,
unsigned int size)
{
__le32 *cpu;
dma_addr_t dma = 0;
if (NULL != risc->cpu && risc->size < size)
btcx_riscmem_free(pci,risc);
if (NULL == risc->cpu) {
cpu = dma_alloc_coherent(&pci->dev, size, &dma, GFP_KERNEL);
if (NULL == cpu)
return -ENOMEM;
risc->cpu = cpu;
risc->dma = dma;
risc->size = size;
memcnt++;
dprintk("btcx: riscmem alloc [%d] dma=%lx cpu=%p size=%d\n",
memcnt, (unsigned long)dma, cpu, size);
}
return 0;
}
| linux-master | drivers/media/pci/bt8xx/btcx-risc.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv - Bt848 frame grabber driver
vbi interface
(c) 2002 Gerd Knorr <[email protected]>
Copyright (C) 2005, 2006 Michael H. Schimek <[email protected]>
Sponsored by OPQ Systems AB
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/kdev_t.h>
#include <media/v4l2-ioctl.h>
#include <asm/io.h>
#include "bttvp.h"
/* Offset from line sync pulse leading edge (0H) to start of VBI capture,
in fCLKx2 pixels. According to the datasheet, VBI capture starts
VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
(64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
Just Plain Wrong. The real value appears to be different for
different revisions of the bt8x8 chips, and to be affected by the
horizontal scaling factor. Experimentally, the value is measured
to be about 244. */
#define VBI_OFFSET 244
static unsigned int vbibufs = 4;
static unsigned int vbi_debug;
module_param(vbibufs, int, 0444);
module_param(vbi_debug, int, 0644);
MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
#ifdef dprintk
# undef dprintk
#endif
#define dprintk(fmt, ...) \
do { \
if (vbi_debug) \
pr_debug("%d: " fmt, btv->c.nr, ##__VA_ARGS__); \
} while (0)
#define IMAGE_SIZE(fmt) \
(((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
/* ----------------------------------------------------------------------- */
/* vbi risc code + mm */
static int queue_setup_vbi(struct vb2_queue *q, unsigned int *num_buffers,
unsigned int *num_planes, unsigned int sizes[],
struct device *alloc_devs[])
{
struct bttv *btv = vb2_get_drv_priv(q);
unsigned int size = IMAGE_SIZE(&btv->vbi_fmt.fmt);
if (*num_planes)
return sizes[0] < size ? -EINVAL : 0;
*num_planes = 1;
sizes[0] = size;
return 0;
}
static void buf_queue_vbi(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct bttv *btv = vb2_get_drv_priv(vq);
struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
unsigned long flags;
spin_lock_irqsave(&btv->s_lock, flags);
if (list_empty(&btv->vcapture)) {
btv->loop_irq = BT848_RISC_VBI;
if (vb2_is_streaming(&btv->capq))
btv->loop_irq |= BT848_RISC_VIDEO;
bttv_set_dma(btv, BT848_CAP_CTL_CAPTURE_VBI_ODD |
BT848_CAP_CTL_CAPTURE_VBI_EVEN);
}
list_add_tail(&buf->list, &btv->vcapture);
spin_unlock_irqrestore(&btv->s_lock, flags);
}
static int buf_prepare_vbi(struct vb2_buffer *vb)
{
int ret = 0;
struct vb2_queue *vq = vb->vb2_queue;
struct bttv *btv = vb2_get_drv_priv(vq);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
unsigned int size = IMAGE_SIZE(&btv->vbi_fmt.fmt);
if (vb2_plane_size(vb, 0) < size)
return -EINVAL;
vb2_set_plane_payload(vb, 0, size);
buf->vbuf.field = V4L2_FIELD_NONE;
ret = bttv_buffer_risc_vbi(btv, buf);
return ret;
}
static void buf_cleanup_vbi(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
struct vb2_queue *vq = vb->vb2_queue;
struct bttv *btv = vb2_get_drv_priv(vq);
btcx_riscmem_free(btv->c.pci, &buf->top);
btcx_riscmem_free(btv->c.pci, &buf->bottom);
}
static int start_streaming_vbi(struct vb2_queue *q, unsigned int count)
{
int ret;
int seqnr = 0;
struct bttv_buffer *buf;
struct bttv *btv = vb2_get_drv_priv(q);
btv->framedrop = 0;
ret = check_alloc_btres_lock(btv, RESOURCE_VBI);
if (ret == 0) {
if (btv->field_count)
seqnr++;
while (!list_empty(&btv->vcapture)) {
buf = list_entry(btv->vcapture.next,
struct bttv_buffer, list);
list_del(&buf->list);
buf->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
vb2_buffer_done(&buf->vbuf.vb2_buf,
VB2_BUF_STATE_QUEUED);
}
return !ret;
}
if (!vb2_is_streaming(&btv->capq)) {
init_irqreg(btv);
btv->field_count = 0;
}
return !ret;
}
static void stop_streaming_vbi(struct vb2_queue *q)
{
struct bttv *btv = vb2_get_drv_priv(q);
unsigned long flags;
vb2_wait_for_all_buffers(q);
spin_lock_irqsave(&btv->s_lock, flags);
free_btres_lock(btv, RESOURCE_VBI);
if (!vb2_is_streaming(&btv->capq)) {
/* stop field counter */
btand(~BT848_INT_VSYNC, BT848_INT_MASK);
}
spin_unlock_irqrestore(&btv->s_lock, flags);
}
const struct vb2_ops bttv_vbi_qops = {
.queue_setup = queue_setup_vbi,
.buf_queue = buf_queue_vbi,
.buf_prepare = buf_prepare_vbi,
.buf_cleanup = buf_cleanup_vbi,
.start_streaming = start_streaming_vbi,
.stop_streaming = stop_streaming_vbi,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
/* ----------------------------------------------------------------------- */
static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
__s32 crop_start)
{
__s32 min_start, max_start, max_end, f2_offset;
unsigned int i;
/* For compatibility with earlier driver versions we must pretend
the VBI and video capture window may overlap. In reality RISC
magic aborts VBI capturing at the first line of video capturing,
leaving the rest of the buffer unchanged, usually all zero.
VBI capturing must always start before video capturing. >> 1
because cropping counts field lines times two. */
min_start = tvnorm->vbistart[0];
max_start = (crop_start >> 1) - 1;
max_end = (tvnorm->cropcap.bounds.top
+ tvnorm->cropcap.bounds.height) >> 1;
if (min_start > max_start)
return -EBUSY;
WARN_ON(max_start >= max_end);
f->sampling_rate = tvnorm->Fsc;
f->samples_per_line = VBI_BPL;
f->sample_format = V4L2_PIX_FMT_GREY;
f->offset = VBI_OFFSET;
f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
for (i = 0; i < 2; ++i) {
if (0 == f->count[i]) {
/* No data from this field. We leave f->start[i]
alone because VIDIOCSVBIFMT is w/o and EINVALs
when a driver does not support exactly the
requested parameters. */
} else {
s64 start, count;
start = clamp(f->start[i], min_start, max_start);
/* s64 to prevent overflow. */
count = (s64) f->start[i] + f->count[i] - start;
f->start[i] = start;
f->count[i] = clamp(count, (s64) 1,
max_end - start);
}
min_start += f2_offset;
max_start += f2_offset;
max_end += f2_offset;
}
if (0 == (f->count[0] | f->count[1])) {
/* As in earlier driver versions. */
f->start[0] = tvnorm->vbistart[0];
f->start[1] = tvnorm->vbistart[1];
f->count[0] = 1;
f->count[1] = 1;
}
f->flags = 0;
f->reserved[0] = 0;
f->reserved[1] = 0;
return 0;
}
int bttv_try_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
{
struct bttv *btv = video_drvdata(file);
const struct bttv_tvnorm *tvnorm;
__s32 crop_start;
mutex_lock(&btv->lock);
tvnorm = &bttv_tvnorms[btv->tvnorm];
crop_start = btv->crop_start;
mutex_unlock(&btv->lock);
return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
}
int bttv_s_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
{
struct bttv *btv = video_drvdata(file);
const struct bttv_tvnorm *tvnorm;
__s32 start1, end;
int rc;
mutex_lock(&btv->lock);
rc = -EBUSY;
if (btv->resources & RESOURCE_VBI)
goto fail;
tvnorm = &bttv_tvnorms[btv->tvnorm];
rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
if (0 != rc)
goto fail;
start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
tvnorm->vbistart[0];
/* First possible line of video capturing. Should be
max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
when capturing both fields. But for compatibility we must
pretend the VBI and video capture window may overlap,
so end = start + 1, the lowest possible value, times two
because vbi_fmt.end counts field lines times two. */
end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
btv->vbi_fmt.fmt = frt->fmt.vbi;
btv->vbi_fmt.tvnorm = tvnorm;
btv->vbi_fmt.end = end;
rc = 0;
fail:
mutex_unlock(&btv->lock);
return rc;
}
int bttv_g_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
{
const struct bttv_tvnorm *tvnorm;
struct bttv *btv = video_drvdata(file);
frt->fmt.vbi = btv->vbi_fmt.fmt;
tvnorm = &bttv_tvnorms[btv->tvnorm];
if (tvnorm != btv->vbi_fmt.tvnorm) {
__s32 max_end;
unsigned int i;
/* As in vbi_buffer_prepare() this imitates the
behaviour of earlier driver versions after video
standard changes, with default parameters anyway. */
max_end = (tvnorm->cropcap.bounds.top
+ tvnorm->cropcap.bounds.height) >> 1;
frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
for (i = 0; i < 2; ++i) {
__s32 new_start;
new_start = frt->fmt.vbi.start[i] + tvnorm->vbistart[i]
- btv->vbi_fmt.tvnorm->vbistart[i];
frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
frt->fmt.vbi.count[i] =
min((__s32) frt->fmt.vbi.count[i],
max_end - frt->fmt.vbi.start[i]);
max_end += tvnorm->vbistart[1]
- tvnorm->vbistart[0];
}
}
return 0;
}
void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm)
{
const struct bttv_tvnorm *tvnorm;
unsigned int real_samples_per_line;
unsigned int real_count;
tvnorm = &bttv_tvnorms[norm];
f->fmt.sampling_rate = tvnorm->Fsc;
f->fmt.samples_per_line = VBI_BPL;
f->fmt.sample_format = V4L2_PIX_FMT_GREY;
f->fmt.offset = VBI_OFFSET;
f->fmt.start[0] = tvnorm->vbistart[0];
f->fmt.start[1] = tvnorm->vbistart[1];
f->fmt.count[0] = VBI_DEFLINES;
f->fmt.count[1] = VBI_DEFLINES;
f->fmt.flags = 0;
f->fmt.reserved[0] = 0;
f->fmt.reserved[1] = 0;
/* For compatibility the buffer size must be 2 * VBI_DEFLINES *
VBI_BPL regardless of the current video standard. */
real_samples_per_line = 1024 + tvnorm->vbipack * 4;
real_count = ((tvnorm->cropcap.defrect.top >> 1)
- tvnorm->vbistart[0]);
WARN_ON(real_samples_per_line > VBI_BPL);
WARN_ON(real_count > VBI_DEFLINES);
f->tvnorm = tvnorm;
/* See bttv_vbi_fmt_set(). */
f->end = tvnorm->vbistart[0] * 2 + 2;
}
| linux-master | drivers/media/pci/bt8xx/bttv-vbi.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
bttv-risc.c -- interfaces to other kernel modules
bttv risc code handling
- memory management
- generation
(c) 2000-2003 Gerd Knorr <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/pgtable.h>
#include <asm/page.h>
#include <media/v4l2-ioctl.h>
#include "bttvp.h"
#define VCR_HACK_LINES 4
/* ---------------------------------------------------------- */
/* risc code generators */
int
bttv_risc_packed(struct bttv *btv, struct btcx_riscmem *risc,
struct scatterlist *sglist,
unsigned int offset, unsigned int bpl,
unsigned int padding, unsigned int skip_lines,
unsigned int store_lines)
{
u32 instructions,line,todo;
struct scatterlist *sg;
__le32 *rp;
int rc;
/* estimate risc mem: worst case is one write per page border +
one write per scan line + sync + jump (all 2 dwords). padding
can cause next bpl to start close to a page border. First DMA
region may be smaller than PAGE_SIZE */
instructions = skip_lines * 4;
instructions += (1 + ((bpl + padding) * store_lines)
/ PAGE_SIZE + store_lines) * 8;
instructions += 2 * 8;
if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions)) < 0)
return rc;
/* sync instruction */
rp = risc->cpu;
*(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
*(rp++) = cpu_to_le32(0);
while (skip_lines-- > 0) {
*(rp++) = cpu_to_le32(BT848_RISC_SKIP | BT848_RISC_SOL |
BT848_RISC_EOL | bpl);
}
/* scan lines */
sg = sglist;
for (line = 0; line < store_lines; line++) {
if ((line >= (store_lines - VCR_HACK_LINES)) &&
btv->opt_vcr_hack)
continue;
while (offset && offset >= sg_dma_len(sg)) {
offset -= sg_dma_len(sg);
sg = sg_next(sg);
}
if (bpl <= sg_dma_len(sg)-offset) {
/* fits into current chunk */
*(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
BT848_RISC_EOL|bpl);
*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
offset+=bpl;
} else {
/* scanline needs to be split */
todo = bpl;
*(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
(sg_dma_len(sg)-offset));
*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
todo -= (sg_dma_len(sg)-offset);
offset = 0;
sg = sg_next(sg);
while (todo > sg_dma_len(sg)) {
*(rp++)=cpu_to_le32(BT848_RISC_WRITE|
sg_dma_len(sg));
*(rp++)=cpu_to_le32(sg_dma_address(sg));
todo -= sg_dma_len(sg);
sg = sg_next(sg);
}
*(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|
todo);
*(rp++)=cpu_to_le32(sg_dma_address(sg));
offset += todo;
}
offset += padding;
}
/* save pointer to jmp instruction address */
risc->jmp = rp;
WARN_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
return 0;
}
static int
bttv_risc_planar(struct bttv *btv, struct btcx_riscmem *risc,
struct scatterlist *sglist,
unsigned int yoffset, unsigned int ybpl,
unsigned int ypadding, unsigned int ylines,
unsigned int uoffset, unsigned int voffset,
unsigned int hshift, unsigned int vshift,
unsigned int cpadding)
{
unsigned int instructions,line,todo,ylen,chroma;
__le32 *rp;
u32 ri;
struct scatterlist *ysg;
struct scatterlist *usg;
struct scatterlist *vsg;
int topfield = (0 == yoffset);
int rc;
/* estimate risc mem: worst case is one write per page border +
one write per scan line (5 dwords)
plus sync + jump (2 dwords) */
instructions = ((3 + (ybpl + ypadding) * ylines * 2)
/ PAGE_SIZE) + ylines;
instructions += 2;
if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions*4*5)) < 0)
return rc;
/* sync instruction */
rp = risc->cpu;
*(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
*(rp++) = cpu_to_le32(0);
/* scan lines */
ysg = sglist;
usg = sglist;
vsg = sglist;
for (line = 0; line < ylines; line++) {
if ((btv->opt_vcr_hack) &&
(line >= (ylines - VCR_HACK_LINES)))
continue;
switch (vshift) {
case 0:
chroma = 1;
break;
case 1:
if (topfield)
chroma = ((line & 1) == 0);
else
chroma = ((line & 1) == 1);
break;
case 2:
if (topfield)
chroma = ((line & 3) == 0);
else
chroma = ((line & 3) == 2);
break;
default:
chroma = 0;
break;
}
for (todo = ybpl; todo > 0; todo -= ylen) {
/* go to next sg entry if needed */
while (yoffset && yoffset >= sg_dma_len(ysg)) {
yoffset -= sg_dma_len(ysg);
ysg = sg_next(ysg);
}
/* calculate max number of bytes we can write */
ylen = todo;
if (yoffset + ylen > sg_dma_len(ysg))
ylen = sg_dma_len(ysg) - yoffset;
if (chroma) {
while (uoffset && uoffset >= sg_dma_len(usg)) {
uoffset -= sg_dma_len(usg);
usg = sg_next(usg);
}
while (voffset && voffset >= sg_dma_len(vsg)) {
voffset -= sg_dma_len(vsg);
vsg = sg_next(vsg);
}
if (uoffset + (ylen>>hshift) > sg_dma_len(usg))
ylen = (sg_dma_len(usg) - uoffset) << hshift;
if (voffset + (ylen>>hshift) > sg_dma_len(vsg))
ylen = (sg_dma_len(vsg) - voffset) << hshift;
ri = BT848_RISC_WRITE123;
} else {
ri = BT848_RISC_WRITE1S23;
}
if (ybpl == todo)
ri |= BT848_RISC_SOL;
if (ylen == todo)
ri |= BT848_RISC_EOL;
/* write risc instruction */
*(rp++)=cpu_to_le32(ri | ylen);
*(rp++)=cpu_to_le32(((ylen >> hshift) << 16) |
(ylen >> hshift));
*(rp++)=cpu_to_le32(sg_dma_address(ysg)+yoffset);
yoffset += ylen;
if (chroma) {
*(rp++)=cpu_to_le32(sg_dma_address(usg)+uoffset);
uoffset += ylen >> hshift;
*(rp++)=cpu_to_le32(sg_dma_address(vsg)+voffset);
voffset += ylen >> hshift;
}
}
yoffset += ypadding;
if (chroma) {
uoffset += cpadding;
voffset += cpadding;
}
}
/* save pointer to jmp instruction address */
risc->jmp = rp;
WARN_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
return 0;
}
/* ---------------------------------------------------------- */
static void
bttv_calc_geo_old(struct bttv *btv, struct bttv_geometry *geo,
int width, int height, int interleaved,
const struct bttv_tvnorm *tvnorm)
{
u32 xsf, sr;
int vdelay;
int swidth = tvnorm->swidth;
int totalwidth = tvnorm->totalwidth;
int scaledtwidth = tvnorm->scaledtwidth;
if (btv->input == btv->dig) {
swidth = 720;
totalwidth = 858;
scaledtwidth = 858;
}
vdelay = tvnorm->vdelay;
xsf = (width*scaledtwidth)/swidth;
geo->hscale = ((totalwidth*4096UL)/xsf-4096);
geo->hdelay = tvnorm->hdelayx1;
geo->hdelay = (geo->hdelay*width)/swidth;
geo->hdelay &= 0x3fe;
sr = ((tvnorm->sheight >> (interleaved?0:1))*512)/height - 512;
geo->vscale = (0x10000UL-sr) & 0x1fff;
geo->crop = ((width>>8)&0x03) | ((geo->hdelay>>6)&0x0c) |
((tvnorm->sheight>>4)&0x30) | ((vdelay>>2)&0xc0);
geo->vscale |= interleaved ? (BT848_VSCALE_INT<<8) : 0;
geo->vdelay = vdelay;
geo->width = width;
geo->sheight = tvnorm->sheight;
geo->vtotal = tvnorm->vtotal;
if (btv->opt_combfilter) {
geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
geo->comb = (width < 769) ? 1 : 0;
} else {
geo->vtc = 0;
geo->comb = 0;
}
}
static void
bttv_calc_geo (struct bttv * btv,
struct bttv_geometry * geo,
unsigned int width,
unsigned int height,
int both_fields,
const struct bttv_tvnorm * tvnorm,
const struct v4l2_rect * crop)
{
unsigned int c_width;
unsigned int c_height;
u32 sr;
if ((crop->left == tvnorm->cropcap.defrect.left
&& crop->top == tvnorm->cropcap.defrect.top
&& crop->width == tvnorm->cropcap.defrect.width
&& crop->height == tvnorm->cropcap.defrect.height
&& width <= tvnorm->swidth /* see PAL-Nc et al */)
|| btv->input == btv->dig) {
bttv_calc_geo_old(btv, geo, width, height,
both_fields, tvnorm);
return;
}
/* For bug compatibility the image size checks permit scale
factors > 16. See bttv_crop_calc_limits(). */
c_width = min((unsigned int) crop->width, width * 16);
c_height = min((unsigned int) crop->height, height * 16);
geo->width = width;
geo->hscale = (c_width * 4096U + (width >> 1)) / width - 4096;
/* Even to store Cb first, odd for Cr. */
geo->hdelay = ((crop->left * width + c_width) / c_width) & ~1;
geo->sheight = c_height;
geo->vdelay = crop->top - tvnorm->cropcap.bounds.top + MIN_VDELAY;
sr = c_height >> !both_fields;
sr = (sr * 512U + (height >> 1)) / height - 512;
geo->vscale = (0x10000UL - sr) & 0x1fff;
geo->vscale |= both_fields ? (BT848_VSCALE_INT << 8) : 0;
geo->vtotal = tvnorm->vtotal;
geo->crop = (((geo->width >> 8) & 0x03) |
((geo->hdelay >> 6) & 0x0c) |
((geo->sheight >> 4) & 0x30) |
((geo->vdelay >> 2) & 0xc0));
if (btv->opt_combfilter) {
geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
geo->comb = (width < 769) ? 1 : 0;
} else {
geo->vtc = 0;
geo->comb = 0;
}
}
static void
bttv_apply_geo(struct bttv *btv, struct bttv_geometry *geo, int odd)
{
int off = odd ? 0x80 : 0x00;
if (geo->comb)
btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
else
btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
btwrite(geo->vtc, BT848_E_VTC+off);
btwrite(geo->hscale >> 8, BT848_E_HSCALE_HI+off);
btwrite(geo->hscale & 0xff, BT848_E_HSCALE_LO+off);
btaor((geo->vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
btwrite(geo->vscale & 0xff, BT848_E_VSCALE_LO+off);
btwrite(geo->width & 0xff, BT848_E_HACTIVE_LO+off);
btwrite(geo->hdelay & 0xff, BT848_E_HDELAY_LO+off);
btwrite(geo->sheight & 0xff, BT848_E_VACTIVE_LO+off);
btwrite(geo->vdelay & 0xff, BT848_E_VDELAY_LO+off);
btwrite(geo->crop, BT848_E_CROP+off);
btwrite(geo->vtotal>>8, BT848_VTOTAL_HI);
btwrite(geo->vtotal & 0xff, BT848_VTOTAL_LO);
}
/* ---------------------------------------------------------- */
/* risc group / risc main loop / dma management */
static void bttv_set_risc_status(struct bttv *btv)
{
unsigned long cmd = BT848_RISC_JUMP;
if (btv->loop_irq) {
cmd |= BT848_RISC_IRQ;
cmd |= (btv->loop_irq & 0x0f) << 16;
cmd |= (~btv->loop_irq & 0x0f) << 20;
}
btv->main.cpu[RISC_SLOT_LOOP] = cpu_to_le32(cmd);
}
static void bttv_set_irq_timer(struct bttv *btv)
{
if (btv->curr.frame_irq || btv->loop_irq || btv->cvbi)
mod_timer(&btv->timeout, jiffies + BTTV_TIMEOUT);
else
del_timer(&btv->timeout);
}
static int bttv_set_capture_control(struct bttv *btv, int start_capture)
{
int capctl = 0;
if (btv->curr.top || btv->curr.bottom)
capctl = BT848_CAP_CTL_CAPTURE_ODD |
BT848_CAP_CTL_CAPTURE_EVEN;
if (btv->cvbi)
capctl |= BT848_CAP_CTL_CAPTURE_VBI_ODD |
BT848_CAP_CTL_CAPTURE_VBI_EVEN;
capctl |= start_capture;
btaor(capctl, ~0x0f, BT848_CAP_CTL);
return capctl;
}
static void bttv_start_dma(struct bttv *btv)
{
if (btv->dma_on)
return;
btwrite(btv->main.dma, BT848_RISC_STRT_ADD);
btor(BT848_GPIO_DMA_CTL_RISC_ENABLE | BT848_GPIO_DMA_CTL_FIFO_ENABLE,
BT848_GPIO_DMA_CTL);
btv->dma_on = 1;
}
static void bttv_stop_dma(struct bttv *btv)
{
if (!btv->dma_on)
return;
btand(~(BT848_GPIO_DMA_CTL_RISC_ENABLE |
BT848_GPIO_DMA_CTL_FIFO_ENABLE), BT848_GPIO_DMA_CTL);
btv->dma_on = 0;
}
void bttv_set_dma(struct bttv *btv, int start_capture)
{
int capctl = 0;
bttv_set_risc_status(btv);
bttv_set_irq_timer(btv);
capctl = bttv_set_capture_control(btv, start_capture);
if (capctl)
bttv_start_dma(btv);
else
bttv_stop_dma(btv);
d2printk("%d: capctl=%x lirq=%d top=%08llx/%08llx even=%08llx/%08llx\n",
btv->c.nr,capctl,btv->loop_irq,
btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0,
btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
}
int
bttv_risc_init_main(struct bttv *btv)
{
int rc;
if ((rc = btcx_riscmem_alloc(btv->c.pci,&btv->main,PAGE_SIZE)) < 0)
return rc;
dprintk("%d: risc main @ %08llx\n",
btv->c.nr, (unsigned long long)btv->main.dma);
btv->main.cpu[0] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
BT848_FIFO_STATUS_VRE);
btv->main.cpu[1] = cpu_to_le32(0);
btv->main.cpu[2] = cpu_to_le32(BT848_RISC_JUMP);
btv->main.cpu[3] = cpu_to_le32(btv->main.dma + (4<<2));
/* top field */
btv->main.cpu[4] = cpu_to_le32(BT848_RISC_JUMP);
btv->main.cpu[5] = cpu_to_le32(btv->main.dma + (6<<2));
btv->main.cpu[6] = cpu_to_le32(BT848_RISC_JUMP);
btv->main.cpu[7] = cpu_to_le32(btv->main.dma + (8<<2));
btv->main.cpu[8] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
BT848_FIFO_STATUS_VRO);
btv->main.cpu[9] = cpu_to_le32(0);
/* bottom field */
btv->main.cpu[10] = cpu_to_le32(BT848_RISC_JUMP);
btv->main.cpu[11] = cpu_to_le32(btv->main.dma + (12<<2));
btv->main.cpu[12] = cpu_to_le32(BT848_RISC_JUMP);
btv->main.cpu[13] = cpu_to_le32(btv->main.dma + (14<<2));
/* jump back to top field */
btv->main.cpu[14] = cpu_to_le32(BT848_RISC_JUMP);
btv->main.cpu[15] = cpu_to_le32(btv->main.dma + (0<<2));
return 0;
}
int
bttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
int irqflags)
{
unsigned long cmd;
unsigned long next = btv->main.dma + ((slot+2) << 2);
if (NULL == risc) {
d2printk("%d: risc=%p slot[%d]=NULL\n", btv->c.nr, risc, slot);
btv->main.cpu[slot+1] = cpu_to_le32(next);
} else {
d2printk("%d: risc=%p slot[%d]=%08llx irq=%d\n",
btv->c.nr, risc, slot,
(unsigned long long)risc->dma, irqflags);
cmd = BT848_RISC_JUMP;
if (irqflags) {
cmd |= BT848_RISC_IRQ;
cmd |= (irqflags & 0x0f) << 16;
cmd |= (~irqflags & 0x0f) << 20;
}
risc->jmp[0] = cpu_to_le32(cmd);
risc->jmp[1] = cpu_to_le32(next);
btv->main.cpu[slot+1] = cpu_to_le32(risc->dma);
}
return 0;
}
int bttv_buffer_risc_vbi(struct bttv *btv, struct bttv_buffer *buf)
{
int r = 0;
unsigned int offset;
unsigned int bpl = 2044; /* max. vbipack */
unsigned int padding = VBI_BPL - bpl;
unsigned int skip_lines0 = 0;
unsigned int skip_lines1 = 0;
unsigned int min_vdelay = MIN_VDELAY;
const struct bttv_tvnorm *tvnorm = btv->vbi_fmt.tvnorm;
struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vbuf.vb2_buf, 0);
struct scatterlist *list = sgt->sgl;
if (btv->vbi_fmt.fmt.count[0] > 0)
skip_lines0 = max(0, (btv->vbi_fmt.fmt.start[0] -
tvnorm->vbistart[0]));
if (btv->vbi_fmt.fmt.count[1] > 0)
skip_lines1 = max(0, (btv->vbi_fmt.fmt.start[1] -
tvnorm->vbistart[1]));
if (btv->vbi_fmt.fmt.count[0] > 0) {
r = bttv_risc_packed(btv, &buf->top, list, 0, bpl, padding,
skip_lines0, btv->vbi_fmt.fmt.count[0]);
if (r)
return r;
}
if (btv->vbi_fmt.fmt.count[1] > 0) {
offset = btv->vbi_fmt.fmt.count[0] * VBI_BPL;
r = bttv_risc_packed(btv, &buf->bottom, list, offset, bpl,
padding, skip_lines1,
btv->vbi_fmt.fmt.count[1]);
if (r)
return r;
}
if (btv->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
min_vdelay += btv->vbi_fmt.end - tvnorm->cropcap.bounds.top;
/* For bttv_buffer_activate_vbi(). */
buf->geo.vdelay = min_vdelay;
return r;
}
int
bttv_buffer_activate_vbi(struct bttv *btv,
struct bttv_buffer *vbi)
{
struct btcx_riscmem *top;
struct btcx_riscmem *bottom;
int top_irq_flags;
int bottom_irq_flags;
top = NULL;
bottom = NULL;
top_irq_flags = 0;
bottom_irq_flags = 0;
if (vbi) {
unsigned int crop, vdelay;
list_del(&vbi->list);
/* VDELAY is start of video, end of VBI capturing. */
crop = btread(BT848_E_CROP);
vdelay = btread(BT848_E_VDELAY_LO) + ((crop & 0xc0) << 2);
if (vbi->geo.vdelay > vdelay) {
vdelay = vbi->geo.vdelay & 0xfe;
crop = (crop & 0x3f) | ((vbi->geo.vdelay >> 2) & 0xc0);
btwrite(vdelay, BT848_E_VDELAY_LO);
btwrite(crop, BT848_E_CROP);
btwrite(vdelay, BT848_O_VDELAY_LO);
btwrite(crop, BT848_O_CROP);
}
if (btv->vbi_count[0] > 0) {
top = &vbi->top;
top_irq_flags = 4;
}
if (btv->vbi_count[1] > 0) {
top_irq_flags = 0;
bottom = &vbi->bottom;
bottom_irq_flags = 4;
}
}
bttv_risc_hook(btv, RISC_SLOT_O_VBI, top, top_irq_flags);
bttv_risc_hook(btv, RISC_SLOT_E_VBI, bottom, bottom_irq_flags);
return 0;
}
int
bttv_buffer_activate_video(struct bttv *btv,
struct bttv_buffer_set *set)
{
/* video capture */
if (NULL != set->top && NULL != set->bottom) {
if (set->top == set->bottom) {
if (set->top->list.next)
list_del(&set->top->list);
} else {
if (set->top->list.next)
list_del(&set->top->list);
if (set->bottom->list.next)
list_del(&set->bottom->list);
}
bttv_apply_geo(btv, &set->top->geo, 1);
bttv_apply_geo(btv, &set->bottom->geo,0);
bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
set->top_irq);
bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
set->frame_irq);
btaor((set->top->btformat & 0xf0) | (set->bottom->btformat & 0x0f),
~0xff, BT848_COLOR_FMT);
btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05),
~0x0f, BT848_COLOR_CTL);
} else if (NULL != set->top) {
if (set->top->list.next)
list_del(&set->top->list);
bttv_apply_geo(btv, &set->top->geo,1);
bttv_apply_geo(btv, &set->top->geo,0);
bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
set->frame_irq);
bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
btaor(set->top->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
} else if (NULL != set->bottom) {
if (set->bottom->list.next)
list_del(&set->bottom->list);
bttv_apply_geo(btv, &set->bottom->geo,1);
bttv_apply_geo(btv, &set->bottom->geo,0);
bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
set->frame_irq);
btaor(set->bottom->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
btaor(set->bottom->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
} else {
bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
}
return 0;
}
/* ---------------------------------------------------------- */
/* calculate geometry, build risc code */
int
bttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf)
{
int r = 0;
const struct bttv_tvnorm *tvnorm = bttv_tvnorms + btv->tvnorm;
struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vbuf.vb2_buf, 0);
struct scatterlist *list = sgt->sgl;
unsigned long size = (btv->fmt->depth * btv->width * btv->height) >> 3;
/* packed pixel modes */
if (btv->fmt->flags & FORMAT_FLAGS_PACKED) {
int bpl = (btv->fmt->depth >> 3) * btv->width;
int bpf = bpl * (btv->height >> 1);
bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
V4L2_FIELD_HAS_BOTH(buf->vbuf.field), tvnorm,
&btv->crop[!!btv->do_crop].rect);
switch (buf->vbuf.field) {
case V4L2_FIELD_TOP:
r = bttv_risc_packed(btv, &buf->top, list, 0, bpl, 0,
0, btv->height);
break;
case V4L2_FIELD_BOTTOM:
r = bttv_risc_packed(btv, &buf->bottom, list, 0, bpl,
0, 0, btv->height);
break;
case V4L2_FIELD_INTERLACED:
r = bttv_risc_packed(btv, &buf->top, list, 0, bpl,
bpl, 0, btv->height >> 1);
r = bttv_risc_packed(btv, &buf->bottom, list, bpl,
bpl, bpl, 0, btv->height >> 1);
break;
case V4L2_FIELD_SEQ_TB:
r = bttv_risc_packed(btv, &buf->top, list, 0, bpl, 0,
0, btv->height >> 1);
r = bttv_risc_packed(btv, &buf->bottom, list, bpf,
bpl, 0, 0, btv->height >> 1);
break;
default:
WARN_ON(1);
return -EINVAL;
}
}
/* planar modes */
if (btv->fmt->flags & FORMAT_FLAGS_PLANAR) {
int uoffset, voffset;
int ypadding, cpadding, lines;
/* calculate chroma offsets */
uoffset = btv->width * btv->height;
voffset = btv->width * btv->height;
if (btv->fmt->flags & FORMAT_FLAGS_CrCb) {
/* Y-Cr-Cb plane order */
uoffset >>= btv->fmt->hshift;
uoffset >>= btv->fmt->vshift;
uoffset += voffset;
} else {
/* Y-Cb-Cr plane order */
voffset >>= btv->fmt->hshift;
voffset >>= btv->fmt->vshift;
voffset += uoffset;
}
switch (buf->vbuf.field) {
case V4L2_FIELD_TOP:
bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
0, tvnorm,
&btv->crop[!!btv->do_crop].rect);
r = bttv_risc_planar(btv, &buf->top, list, 0,
btv->width, 0, btv->height,
uoffset, voffset,
btv->fmt->hshift,
btv->fmt->vshift, 0);
break;
case V4L2_FIELD_BOTTOM:
bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
0, tvnorm,
&btv->crop[!!btv->do_crop].rect);
r = bttv_risc_planar(btv, &buf->bottom, list, 0,
btv->width, 0, btv->height,
uoffset, voffset,
btv->fmt->hshift,
btv->fmt->vshift, 0);
break;
case V4L2_FIELD_INTERLACED:
bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
1, tvnorm,
&btv->crop[!!btv->do_crop].rect);
lines = btv->height >> 1;
ypadding = btv->width;
cpadding = btv->width >> btv->fmt->hshift;
r = bttv_risc_planar(btv, &buf->top, list, 0,
btv->width, ypadding, lines,
uoffset, voffset,
btv->fmt->hshift,
btv->fmt->vshift, cpadding);
r = bttv_risc_planar(btv, &buf->bottom, list,
ypadding, btv->width, ypadding,
lines, uoffset + cpadding,
voffset + cpadding,
btv->fmt->hshift,
btv->fmt->vshift, cpadding);
break;
case V4L2_FIELD_SEQ_TB:
bttv_calc_geo(btv, &buf->geo, btv->width, btv->height,
1, tvnorm,
&btv->crop[!!btv->do_crop].rect);
lines = btv->height >> 1;
ypadding = btv->width;
cpadding = btv->width >> btv->fmt->hshift;
r = bttv_risc_planar(btv, &buf->top, list, 0,
btv->width, 0, lines,
uoffset >> 1, voffset >> 1,
btv->fmt->hshift,
btv->fmt->vshift, 0);
r = bttv_risc_planar(btv, &buf->bottom, list,
lines * ypadding,
btv->width, 0, lines,
lines * ypadding + (uoffset >> 1),
lines * ypadding + (voffset >> 1),
btv->fmt->hshift,
btv->fmt->vshift, 0);
break;
default:
WARN_ON(1);
return -EINVAL;
}
}
/* raw data */
if (btv->fmt->flags & FORMAT_FLAGS_RAW) {
/* build risc code */
buf->vbuf.field = V4L2_FIELD_SEQ_TB;
bttv_calc_geo(btv, &buf->geo, tvnorm->swidth, tvnorm->sheight,
1, tvnorm, &btv->crop[!!btv->do_crop].rect);
r = bttv_risc_packed(btv, &buf->top, list, 0, RAW_BPL, 0, 0,
RAW_LINES);
r = bttv_risc_packed(btv, &buf->bottom, list, size / 2,
RAW_BPL, 0, 0, RAW_LINES);
}
/* copy format info */
buf->btformat = btv->fmt->btformat;
buf->btswap = btv->fmt->btswap;
return r;
}
| linux-master | drivers/media/pci/bt8xx/bttv-risc.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
hexium_orion.c - v4l2 driver for the Hexium Orion frame grabber cards
Visit http://www.mihu.de/linux/saa7146/ and follow the link
to "hexium" for further details about this card.
Copyright (C) 2003 Michael Hunold <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define DEBUG_VARIABLE debug
#include <media/drv-intf/saa7146_vv.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int debug;
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "debug verbosity");
/* global variables */
static int hexium_num;
#define HEXIUM_HV_PCI6_ORION 1
#define HEXIUM_ORION_1SVHS_3BNC 2
#define HEXIUM_ORION_4BNC 3
#define HEXIUM_STD (V4L2_STD_PAL | V4L2_STD_SECAM | V4L2_STD_NTSC)
#define HEXIUM_INPUTS 9
static struct v4l2_input hexium_inputs[HEXIUM_INPUTS] = {
{ 0, "CVBS 1", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 1, "CVBS 2", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 2, "CVBS 3", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 3, "CVBS 4", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 4, "CVBS 5", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 5, "CVBS 6", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 6, "Y/C 1", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 7, "Y/C 2", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 8, "Y/C 3", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
};
#define HEXIUM_AUDIOS 0
struct hexium_data
{
s8 adr;
u8 byte;
};
struct hexium
{
int type;
struct video_device video_dev;
struct i2c_adapter i2c_adapter;
int cur_input; /* current input */
};
/* Philips SAA7110 decoder default registers */
static u8 hexium_saa7110[53]={
/*00*/ 0x4C,0x3C,0x0D,0xEF,0xBD,0xF0,0x00,0x00,
/*08*/ 0xF8,0xF8,0x60,0x60,0x40,0x86,0x18,0x90,
/*10*/ 0x00,0x2C,0x40,0x46,0x42,0x1A,0xFF,0xDA,
/*18*/ 0xF0,0x8B,0x00,0x00,0x00,0x00,0x00,0x00,
/*20*/ 0xD9,0x17,0x40,0x41,0x80,0x41,0x80,0x4F,
/*28*/ 0xFE,0x01,0x0F,0x0F,0x03,0x01,0x81,0x03,
/*30*/ 0x44,0x75,0x01,0x8C,0x03
};
static struct {
struct hexium_data data[8];
} hexium_input_select[] = {
{
{ /* cvbs 1 */
{ 0x06, 0x00 },
{ 0x20, 0xD9 },
{ 0x21, 0x17 }, // 0x16,
{ 0x22, 0x40 },
{ 0x2C, 0x03 },
{ 0x30, 0x44 },
{ 0x31, 0x75 }, // ??
{ 0x21, 0x16 }, // 0x03,
}
}, {
{ /* cvbs 2 */
{ 0x06, 0x00 },
{ 0x20, 0x78 },
{ 0x21, 0x07 }, // 0x03,
{ 0x22, 0xD2 },
{ 0x2C, 0x83 },
{ 0x30, 0x60 },
{ 0x31, 0xB5 }, // ?
{ 0x21, 0x03 },
}
}, {
{ /* cvbs 3 */
{ 0x06, 0x00 },
{ 0x20, 0xBA },
{ 0x21, 0x07 }, // 0x05,
{ 0x22, 0x91 },
{ 0x2C, 0x03 },
{ 0x30, 0x60 },
{ 0x31, 0xB5 }, // ??
{ 0x21, 0x05 }, // 0x03,
}
}, {
{ /* cvbs 4 */
{ 0x06, 0x00 },
{ 0x20, 0xD8 },
{ 0x21, 0x17 }, // 0x16,
{ 0x22, 0x40 },
{ 0x2C, 0x03 },
{ 0x30, 0x44 },
{ 0x31, 0x75 }, // ??
{ 0x21, 0x16 }, // 0x03,
}
}, {
{ /* cvbs 5 */
{ 0x06, 0x00 },
{ 0x20, 0xB8 },
{ 0x21, 0x07 }, // 0x05,
{ 0x22, 0x91 },
{ 0x2C, 0x03 },
{ 0x30, 0x60 },
{ 0x31, 0xB5 }, // ??
{ 0x21, 0x05 }, // 0x03,
}
}, {
{ /* cvbs 6 */
{ 0x06, 0x00 },
{ 0x20, 0x7C },
{ 0x21, 0x07 }, // 0x03
{ 0x22, 0xD2 },
{ 0x2C, 0x83 },
{ 0x30, 0x60 },
{ 0x31, 0xB5 }, // ??
{ 0x21, 0x03 },
}
}, {
{ /* y/c 1 */
{ 0x06, 0x80 },
{ 0x20, 0x59 },
{ 0x21, 0x17 },
{ 0x22, 0x42 },
{ 0x2C, 0xA3 },
{ 0x30, 0x44 },
{ 0x31, 0x75 },
{ 0x21, 0x12 },
}
}, {
{ /* y/c 2 */
{ 0x06, 0x80 },
{ 0x20, 0x9A },
{ 0x21, 0x17 },
{ 0x22, 0xB1 },
{ 0x2C, 0x13 },
{ 0x30, 0x60 },
{ 0x31, 0xB5 },
{ 0x21, 0x14 },
}
}, {
{ /* y/c 3 */
{ 0x06, 0x80 },
{ 0x20, 0x3C },
{ 0x21, 0x27 },
{ 0x22, 0xC1 },
{ 0x2C, 0x23 },
{ 0x30, 0x44 },
{ 0x31, 0x75 },
{ 0x21, 0x21 },
}
}
};
static struct saa7146_standard hexium_standards[] = {
{
.name = "PAL", .id = V4L2_STD_PAL,
.v_offset = 16, .v_field = 288,
.h_offset = 1, .h_pixels = 680,
.v_max_out = 576, .h_max_out = 768,
}, {
.name = "NTSC", .id = V4L2_STD_NTSC,
.v_offset = 16, .v_field = 240,
.h_offset = 1, .h_pixels = 640,
.v_max_out = 480, .h_max_out = 640,
}, {
.name = "SECAM", .id = V4L2_STD_SECAM,
.v_offset = 16, .v_field = 288,
.h_offset = 1, .h_pixels = 720,
.v_max_out = 576, .h_max_out = 768,
}
};
/* this is only called for old HV-PCI6/Orion cards
without eeprom */
static int hexium_probe(struct saa7146_dev *dev)
{
struct hexium *hexium = NULL;
union i2c_smbus_data data;
int err = 0;
DEB_EE("\n");
/* there are no hexium orion cards with revision 0 saa7146s */
if (0 == dev->revision) {
return -EFAULT;
}
hexium = kzalloc(sizeof(*hexium), GFP_KERNEL);
if (!hexium)
return -ENOMEM;
/* enable i2c-port pins */
saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
saa7146_write(dev, DD1_INIT, 0x01000100);
saa7146_write(dev, DD1_STREAM_B, 0x00000000);
saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
strscpy(hexium->i2c_adapter.name, "hexium orion",
sizeof(hexium->i2c_adapter.name));
saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {
DEB_S("cannot register i2c-device. skipping.\n");
kfree(hexium);
return -EFAULT;
}
/* set SAA7110 control GPIO 0 */
saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTHI);
/* set HWControl GPIO number 2 */
saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
mdelay(10);
/* detect newer Hexium Orion cards by subsystem ids */
if (0x17c8 == dev->pci->subsystem_vendor && 0x0101 == dev->pci->subsystem_device) {
pr_info("device is a Hexium Orion w/ 1 SVHS + 3 BNC inputs\n");
/* we store the pointer in our private data field */
dev->ext_priv = hexium;
hexium->type = HEXIUM_ORION_1SVHS_3BNC;
return 0;
}
if (0x17c8 == dev->pci->subsystem_vendor && 0x2101 == dev->pci->subsystem_device) {
pr_info("device is a Hexium Orion w/ 4 BNC inputs\n");
/* we store the pointer in our private data field */
dev->ext_priv = hexium;
hexium->type = HEXIUM_ORION_4BNC;
return 0;
}
/* check if this is an old hexium Orion card by looking at
a saa7110 at address 0x4e */
err = i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_READ,
0x00, I2C_SMBUS_BYTE_DATA, &data);
if (err == 0) {
pr_info("device is a Hexium HV-PCI6/Orion (old)\n");
/* we store the pointer in our private data field */
dev->ext_priv = hexium;
hexium->type = HEXIUM_HV_PCI6_ORION;
return 0;
}
i2c_del_adapter(&hexium->i2c_adapter);
kfree(hexium);
return -EFAULT;
}
/* bring hardware to a sane state. this has to be done, just in case someone
wants to capture from this device before it has been properly initialized.
the capture engine would badly fail, because no valid signal arrives on the
saa7146, thus leading to timeouts and stuff. */
static int hexium_init_done(struct saa7146_dev *dev)
{
struct hexium *hexium = (struct hexium *) dev->ext_priv;
union i2c_smbus_data data;
int i = 0;
DEB_D("hexium_init_done called\n");
/* initialize the helper ics to useful values */
for (i = 0; i < sizeof(hexium_saa7110); i++) {
data.byte = hexium_saa7110[i];
if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {
pr_err("failed for address 0x%02x\n", i);
}
}
return 0;
}
static int hexium_set_input(struct hexium *hexium, int input)
{
union i2c_smbus_data data;
int i = 0;
DEB_D("\n");
for (i = 0; i < 8; i++) {
int adr = hexium_input_select[input].data[i].adr;
data.byte = hexium_input_select[input].data[i].byte;
if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, adr, I2C_SMBUS_BYTE_DATA, &data)) {
return -1;
}
pr_debug("%d: 0x%02x => 0x%02x\n", input, adr, data.byte);
}
return 0;
}
static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
{
DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
if (i->index >= HEXIUM_INPUTS)
return -EINVAL;
memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));
DEB_D("v4l2_ioctl: VIDIOC_ENUMINPUT %d\n", i->index);
return 0;
}
static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
{
struct saa7146_dev *dev = video_drvdata(file);
struct hexium *hexium = (struct hexium *) dev->ext_priv;
*input = hexium->cur_input;
DEB_D("VIDIOC_G_INPUT: %d\n", *input);
return 0;
}
static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
{
struct saa7146_dev *dev = video_drvdata(file);
struct hexium *hexium = (struct hexium *) dev->ext_priv;
if (input >= HEXIUM_INPUTS)
return -EINVAL;
hexium->cur_input = input;
hexium_set_input(hexium, input);
return 0;
}
static struct saa7146_ext_vv vv_data;
/* this function only gets called when the probing was successful */
static int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
{
struct hexium *hexium = (struct hexium *) dev->ext_priv;
int ret;
DEB_EE("\n");
ret = saa7146_vv_init(dev, &vv_data);
if (ret) {
pr_err("Error in saa7146_vv_init()\n");
return ret;
}
vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
if (0 != saa7146_register_device(&hexium->video_dev, dev, "hexium orion", VFL_TYPE_VIDEO)) {
pr_err("cannot register capture v4l2 device. skipping.\n");
return -1;
}
pr_err("found 'hexium orion' frame grabber-%d\n", hexium_num);
hexium_num++;
/* the rest */
hexium->cur_input = 0;
hexium_init_done(dev);
hexium_set_input(hexium, 0);
return 0;
}
static int hexium_detach(struct saa7146_dev *dev)
{
struct hexium *hexium = (struct hexium *) dev->ext_priv;
DEB_EE("dev:%p\n", dev);
saa7146_unregister_device(&hexium->video_dev, dev);
saa7146_vv_release(dev);
hexium_num--;
i2c_del_adapter(&hexium->i2c_adapter);
kfree(hexium);
return 0;
}
static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)
{
return 0;
}
static struct saa7146_extension extension;
static struct saa7146_pci_extension_data hexium_hv_pci6 = {
.ext_priv = "Hexium HV-PCI6 / Orion",
.ext = &extension,
};
static struct saa7146_pci_extension_data hexium_orion_1svhs_3bnc = {
.ext_priv = "Hexium HV-PCI6 / Orion (1 SVHS/3 BNC)",
.ext = &extension,
};
static struct saa7146_pci_extension_data hexium_orion_4bnc = {
.ext_priv = "Hexium HV-PCI6 / Orion (4 BNC)",
.ext = &extension,
};
static const struct pci_device_id pci_tbl[] = {
{
.vendor = PCI_VENDOR_ID_PHILIPS,
.device = PCI_DEVICE_ID_PHILIPS_SAA7146,
.subvendor = 0x0000,
.subdevice = 0x0000,
.driver_data = (unsigned long) &hexium_hv_pci6,
},
{
.vendor = PCI_VENDOR_ID_PHILIPS,
.device = PCI_DEVICE_ID_PHILIPS_SAA7146,
.subvendor = 0x17c8,
.subdevice = 0x0101,
.driver_data = (unsigned long) &hexium_orion_1svhs_3bnc,
},
{
.vendor = PCI_VENDOR_ID_PHILIPS,
.device = PCI_DEVICE_ID_PHILIPS_SAA7146,
.subvendor = 0x17c8,
.subdevice = 0x2101,
.driver_data = (unsigned long) &hexium_orion_4bnc,
},
{
.vendor = 0,
}
};
MODULE_DEVICE_TABLE(pci, pci_tbl);
static struct saa7146_ext_vv vv_data = {
.inputs = HEXIUM_INPUTS,
.capabilities = 0,
.stds = &hexium_standards[0],
.num_stds = ARRAY_SIZE(hexium_standards),
.std_callback = &std_callback,
};
static struct saa7146_extension extension = {
.name = "hexium HV-PCI6 Orion",
.flags = 0, // SAA7146_USE_I2C_IRQ,
.pci_tbl = &pci_tbl[0],
.module = THIS_MODULE,
.probe = hexium_probe,
.attach = hexium_attach,
.detach = hexium_detach,
.irq_mask = 0,
.irq_func = NULL,
};
static int __init hexium_init_module(void)
{
if (0 != saa7146_register_extension(&extension)) {
DEB_S("failed to register extension\n");
return -ENODEV;
}
return 0;
}
static void __exit hexium_cleanup_module(void)
{
saa7146_unregister_extension(&extension);
}
module_init(hexium_init_module);
module_exit(hexium_cleanup_module);
MODULE_DESCRIPTION("video4linux-2 driver for Hexium Orion frame grabber cards");
MODULE_AUTHOR("Michael Hunold <[email protected]>");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/saa7146/hexium_orion.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
hexium_gemini.c - v4l2 driver for Hexium Gemini frame grabber cards
Visit http://www.mihu.de/linux/saa7146/ and follow the link
to "hexium" for further details about this card.
Copyright (C) 2003 Michael Hunold <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define DEBUG_VARIABLE debug
#include <media/drv-intf/saa7146_vv.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int debug;
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "debug verbosity");
/* global variables */
static int hexium_num;
#define HEXIUM_GEMINI 4
#define HEXIUM_GEMINI_DUAL 5
#define HEXIUM_STD (V4L2_STD_PAL | V4L2_STD_SECAM | V4L2_STD_NTSC)
#define HEXIUM_INPUTS 9
static struct v4l2_input hexium_inputs[HEXIUM_INPUTS] = {
{ 0, "CVBS 1", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 1, "CVBS 2", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 2, "CVBS 3", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 3, "CVBS 4", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 4, "CVBS 5", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 5, "CVBS 6", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 6, "Y/C 1", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 7, "Y/C 2", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
{ 8, "Y/C 3", V4L2_INPUT_TYPE_CAMERA, 0, 0, HEXIUM_STD, 0, V4L2_IN_CAP_STD },
};
#define HEXIUM_AUDIOS 0
struct hexium_data
{
s8 adr;
u8 byte;
};
#define HEXIUM_GEMINI_V_1_0 1
#define HEXIUM_GEMINI_DUAL_V_1_0 2
struct hexium
{
int type;
struct video_device video_dev;
struct i2c_adapter i2c_adapter;
int cur_input; /* current input */
v4l2_std_id cur_std; /* current standard */
};
/* Samsung KS0127B decoder default registers */
static u8 hexium_ks0127b[0x100]={
/*00*/ 0x00,0x52,0x30,0x40,0x01,0x0C,0x2A,0x10,
/*08*/ 0x00,0x00,0x00,0x60,0x00,0x00,0x0F,0x06,
/*10*/ 0x00,0x00,0xE4,0xC0,0x00,0x00,0x00,0x00,
/*18*/ 0x14,0x9B,0xFE,0xFF,0xFC,0xFF,0x03,0x22,
/*20*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*28*/ 0x00,0x00,0x00,0x00,0x00,0x2C,0x9B,0x00,
/*30*/ 0x00,0x00,0x10,0x80,0x80,0x10,0x80,0x80,
/*38*/ 0x01,0x04,0x00,0x00,0x00,0x29,0xC0,0x00,
/*40*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*48*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*50*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*58*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*60*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*68*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*70*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*78*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*80*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*88*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*90*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*98*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*A0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*A8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*B0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*B8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*C0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*C8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*D0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*D8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*E0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*E8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*F0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*F8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
static struct hexium_data hexium_pal[] = {
{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }
};
static struct hexium_data hexium_ntsc[] = {
{ 0x01, 0x53 }, { 0x12, 0x04 }, { 0x2D, 0x23 }, { 0x2E, 0x81 }, { -1 , 0xFF }
};
static struct hexium_data hexium_secam[] = {
{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }
};
static struct hexium_data hexium_input_select[] = {
{ 0x02, 0x60 },
{ 0x02, 0x64 },
{ 0x02, 0x61 },
{ 0x02, 0x65 },
{ 0x02, 0x62 },
{ 0x02, 0x66 },
{ 0x02, 0x68 },
{ 0x02, 0x69 },
{ 0x02, 0x6A },
};
/* fixme: h_offset = 0 for Hexium Gemini *Dual*, which
are currently *not* supported*/
static struct saa7146_standard hexium_standards[] = {
{
.name = "PAL", .id = V4L2_STD_PAL,
.v_offset = 28, .v_field = 288,
.h_offset = 1, .h_pixels = 680,
.v_max_out = 576, .h_max_out = 768,
}, {
.name = "NTSC", .id = V4L2_STD_NTSC,
.v_offset = 28, .v_field = 240,
.h_offset = 1, .h_pixels = 640,
.v_max_out = 480, .h_max_out = 640,
}, {
.name = "SECAM", .id = V4L2_STD_SECAM,
.v_offset = 28, .v_field = 288,
.h_offset = 1, .h_pixels = 720,
.v_max_out = 576, .h_max_out = 768,
}
};
/* bring hardware to a sane state. this has to be done, just in case someone
wants to capture from this device before it has been properly initialized.
the capture engine would badly fail, because no valid signal arrives on the
saa7146, thus leading to timeouts and stuff. */
static int hexium_init_done(struct saa7146_dev *dev)
{
struct hexium *hexium = (struct hexium *) dev->ext_priv;
union i2c_smbus_data data;
int i = 0;
DEB_D("hexium_init_done called\n");
/* initialize the helper ics to useful values */
for (i = 0; i < sizeof(hexium_ks0127b); i++) {
data.byte = hexium_ks0127b[i];
if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {
pr_err("hexium_init_done() failed for address 0x%02x\n",
i);
}
}
return 0;
}
static int hexium_set_input(struct hexium *hexium, int input)
{
union i2c_smbus_data data;
DEB_D("\n");
data.byte = hexium_input_select[input].byte;
if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, hexium_input_select[input].adr, I2C_SMBUS_BYTE_DATA, &data)) {
return -1;
}
return 0;
}
static int hexium_set_standard(struct hexium *hexium, struct hexium_data *vdec)
{
union i2c_smbus_data data;
int i = 0;
DEB_D("\n");
while (vdec[i].adr != -1) {
data.byte = vdec[i].byte;
if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, vdec[i].adr, I2C_SMBUS_BYTE_DATA, &data)) {
pr_err("hexium_init_done: hexium_set_standard() failed for address 0x%02x\n",
i);
return -1;
}
i++;
}
return 0;
}
static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
{
DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
if (i->index >= HEXIUM_INPUTS)
return -EINVAL;
memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));
DEB_D("v4l2_ioctl: VIDIOC_ENUMINPUT %d\n", i->index);
return 0;
}
static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
{
struct saa7146_dev *dev = video_drvdata(file);
struct hexium *hexium = (struct hexium *) dev->ext_priv;
*input = hexium->cur_input;
DEB_D("VIDIOC_G_INPUT: %d\n", *input);
return 0;
}
static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
{
struct saa7146_dev *dev = video_drvdata(file);
struct hexium *hexium = (struct hexium *) dev->ext_priv;
DEB_EE("VIDIOC_S_INPUT %d\n", input);
if (input >= HEXIUM_INPUTS)
return -EINVAL;
hexium->cur_input = input;
hexium_set_input(hexium, input);
return 0;
}
static struct saa7146_ext_vv vv_data;
/* this function only gets called when the probing was successful */
static int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
{
struct hexium *hexium;
int ret;
DEB_EE("\n");
hexium = kzalloc(sizeof(*hexium), GFP_KERNEL);
if (!hexium)
return -ENOMEM;
dev->ext_priv = hexium;
/* enable i2c-port pins */
saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
strscpy(hexium->i2c_adapter.name, "hexium gemini",
sizeof(hexium->i2c_adapter.name));
saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {
DEB_S("cannot register i2c-device. skipping.\n");
kfree(hexium);
return -EFAULT;
}
/* set HWControl GPIO number 2 */
saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
saa7146_write(dev, DD1_INIT, 0x07000700);
saa7146_write(dev, DD1_STREAM_B, 0x00000000);
saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
/* the rest */
hexium->cur_input = 0;
hexium_init_done(dev);
hexium_set_standard(hexium, hexium_pal);
hexium->cur_std = V4L2_STD_PAL;
hexium_set_input(hexium, 0);
hexium->cur_input = 0;
ret = saa7146_vv_init(dev, &vv_data);
if (ret) {
i2c_del_adapter(&hexium->i2c_adapter);
kfree(hexium);
return ret;
}
vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
ret = saa7146_register_device(&hexium->video_dev, dev, "hexium gemini", VFL_TYPE_VIDEO);
if (ret < 0) {
pr_err("cannot register capture v4l2 device. skipping.\n");
saa7146_vv_release(dev);
i2c_del_adapter(&hexium->i2c_adapter);
kfree(hexium);
return ret;
}
pr_info("found 'hexium gemini' frame grabber-%d\n", hexium_num);
hexium_num++;
return 0;
}
static int hexium_detach(struct saa7146_dev *dev)
{
struct hexium *hexium = (struct hexium *) dev->ext_priv;
DEB_EE("dev:%p\n", dev);
saa7146_unregister_device(&hexium->video_dev, dev);
saa7146_vv_release(dev);
hexium_num--;
i2c_del_adapter(&hexium->i2c_adapter);
kfree(hexium);
return 0;
}
static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)
{
struct hexium *hexium = (struct hexium *) dev->ext_priv;
if (V4L2_STD_PAL == std->id) {
hexium_set_standard(hexium, hexium_pal);
hexium->cur_std = V4L2_STD_PAL;
return 0;
} else if (V4L2_STD_NTSC == std->id) {
hexium_set_standard(hexium, hexium_ntsc);
hexium->cur_std = V4L2_STD_NTSC;
return 0;
} else if (V4L2_STD_SECAM == std->id) {
hexium_set_standard(hexium, hexium_secam);
hexium->cur_std = V4L2_STD_SECAM;
return 0;
}
return -1;
}
static struct saa7146_extension hexium_extension;
static struct saa7146_pci_extension_data hexium_gemini_4bnc = {
.ext_priv = "Hexium Gemini (4 BNC)",
.ext = &hexium_extension,
};
static struct saa7146_pci_extension_data hexium_gemini_dual_4bnc = {
.ext_priv = "Hexium Gemini Dual (4 BNC)",
.ext = &hexium_extension,
};
static const struct pci_device_id pci_tbl[] = {
{
.vendor = PCI_VENDOR_ID_PHILIPS,
.device = PCI_DEVICE_ID_PHILIPS_SAA7146,
.subvendor = 0x17c8,
.subdevice = 0x2401,
.driver_data = (unsigned long) &hexium_gemini_4bnc,
},
{
.vendor = PCI_VENDOR_ID_PHILIPS,
.device = PCI_DEVICE_ID_PHILIPS_SAA7146,
.subvendor = 0x17c8,
.subdevice = 0x2402,
.driver_data = (unsigned long) &hexium_gemini_dual_4bnc,
},
{
.vendor = 0,
}
};
MODULE_DEVICE_TABLE(pci, pci_tbl);
static struct saa7146_ext_vv vv_data = {
.inputs = HEXIUM_INPUTS,
.capabilities = 0,
.stds = &hexium_standards[0],
.num_stds = ARRAY_SIZE(hexium_standards),
.std_callback = &std_callback,
};
static struct saa7146_extension hexium_extension = {
.name = "hexium gemini",
.flags = SAA7146_USE_I2C_IRQ,
.pci_tbl = &pci_tbl[0],
.module = THIS_MODULE,
.attach = hexium_attach,
.detach = hexium_detach,
.irq_mask = 0,
.irq_func = NULL,
};
static int __init hexium_init_module(void)
{
if (0 != saa7146_register_extension(&hexium_extension)) {
DEB_S("failed to register extension\n");
return -ENODEV;
}
return 0;
}
static void __exit hexium_cleanup_module(void)
{
saa7146_unregister_extension(&hexium_extension);
}
module_init(hexium_init_module);
module_exit(hexium_cleanup_module);
MODULE_DESCRIPTION("video4linux-2 driver for Hexium Gemini frame grabber cards");
MODULE_AUTHOR("Michael Hunold <[email protected]>");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/saa7146/hexium_gemini.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
mxb - v4l2 driver for the Multimedia eXtension Board
Copyright (C) 1998-2006 Michael Hunold <[email protected]>
Visit http://www.themm.net/~mihu/linux/saa7146/mxb.html
for further details about this card.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define DEBUG_VARIABLE debug
#include <media/drv-intf/saa7146_vv.h>
#include <media/tuner.h>
#include <media/v4l2-common.h>
#include <media/i2c/saa7115.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include "tea6415c.h"
#include "tea6420.h"
#define MXB_AUDIOS 6
#define I2C_SAA7111A 0x24
#define I2C_TDA9840 0x42
#define I2C_TEA6415C 0x43
#define I2C_TEA6420_1 0x4c
#define I2C_TEA6420_2 0x4d
#define I2C_TUNER 0x60
#define MXB_BOARD_CAN_DO_VBI(dev) (dev->revision != 0)
/* global variable */
static int mxb_num;
/* initial frequence the tuner will be tuned to.
in verden (lower saxony, germany) 4148 is a
channel called "phoenix" */
static int freq = 4148;
module_param(freq, int, 0644);
MODULE_PARM_DESC(freq, "initial frequency the tuner will be tuned to while setup");
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
#define MXB_STD (V4L2_STD_PAL_BG | V4L2_STD_PAL_I | V4L2_STD_SECAM | V4L2_STD_NTSC)
#define MXB_INPUTS 4
enum { TUNER, AUX1, AUX3, AUX3_YC };
static struct v4l2_input mxb_inputs[MXB_INPUTS] = {
{ TUNER, "Tuner", V4L2_INPUT_TYPE_TUNER, 0x3f, 0,
V4L2_STD_PAL_BG | V4L2_STD_PAL_I, 0, V4L2_IN_CAP_STD },
{ AUX1, "AUX1", V4L2_INPUT_TYPE_CAMERA, 0x3f, 0,
MXB_STD, 0, V4L2_IN_CAP_STD },
{ AUX3, "AUX3 Composite", V4L2_INPUT_TYPE_CAMERA, 0x3f, 0,
MXB_STD, 0, V4L2_IN_CAP_STD },
{ AUX3_YC, "AUX3 S-Video", V4L2_INPUT_TYPE_CAMERA, 0x3f, 0,
MXB_STD, 0, V4L2_IN_CAP_STD },
};
/* this array holds the information, which port of the saa7146 each
input actually uses. the mxb uses port 0 for every input */
static struct {
int hps_source;
int hps_sync;
} input_port_selection[MXB_INPUTS] = {
{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
{ SAA7146_HPS_SOURCE_PORT_A, SAA7146_HPS_SYNC_PORT_A },
};
/* this array holds the information of the audio source (mxb_audios),
which has to be switched corresponding to the video source (mxb_channels) */
static int video_audio_connect[MXB_INPUTS] =
{ 0, 1, 3, 3 };
struct mxb_routing {
u32 input;
u32 output;
};
/* these are the available audio sources, which can switched
to the line- and cd-output individually */
static struct v4l2_audio mxb_audios[MXB_AUDIOS] = {
{
.index = 0,
.name = "Tuner",
.capability = V4L2_AUDCAP_STEREO,
} , {
.index = 1,
.name = "AUX1",
.capability = V4L2_AUDCAP_STEREO,
} , {
.index = 2,
.name = "AUX2",
.capability = V4L2_AUDCAP_STEREO,
} , {
.index = 3,
.name = "AUX3",
.capability = V4L2_AUDCAP_STEREO,
} , {
.index = 4,
.name = "Radio (X9)",
.capability = V4L2_AUDCAP_STEREO,
} , {
.index = 5,
.name = "CD-ROM (X10)",
.capability = V4L2_AUDCAP_STEREO,
}
};
/* These are the necessary input-output-pins for bringing one audio source
(see above) to the CD-output. Note that gain is set to 0 in this table. */
static struct mxb_routing TEA6420_cd[MXB_AUDIOS + 1][2] = {
{ { 1, 1 }, { 1, 1 } }, /* Tuner */
{ { 5, 1 }, { 6, 1 } }, /* AUX 1 */
{ { 4, 1 }, { 6, 1 } }, /* AUX 2 */
{ { 3, 1 }, { 6, 1 } }, /* AUX 3 */
{ { 1, 1 }, { 3, 1 } }, /* Radio */
{ { 1, 1 }, { 2, 1 } }, /* CD-Rom */
{ { 6, 1 }, { 6, 1 } } /* Mute */
};
/* These are the necessary input-output-pins for bringing one audio source
(see above) to the line-output. Note that gain is set to 0 in this table. */
static struct mxb_routing TEA6420_line[MXB_AUDIOS + 1][2] = {
{ { 2, 3 }, { 1, 2 } },
{ { 5, 3 }, { 6, 2 } },
{ { 4, 3 }, { 6, 2 } },
{ { 3, 3 }, { 6, 2 } },
{ { 2, 3 }, { 3, 2 } },
{ { 2, 3 }, { 2, 2 } },
{ { 6, 3 }, { 6, 2 } } /* Mute */
};
struct mxb
{
struct video_device video_dev;
struct video_device vbi_dev;
struct i2c_adapter i2c_adapter;
struct v4l2_subdev *saa7111a;
struct v4l2_subdev *tda9840;
struct v4l2_subdev *tea6415c;
struct v4l2_subdev *tuner;
struct v4l2_subdev *tea6420_1;
struct v4l2_subdev *tea6420_2;
int cur_mode; /* current audio mode (mono, stereo, ...) */
int cur_input; /* current input */
int cur_audinput; /* current audio input */
int cur_mute; /* current mute status */
struct v4l2_frequency cur_freq; /* current frequency the tuner is tuned to */
};
#define saa7111a_call(mxb, o, f, args...) \
v4l2_subdev_call(mxb->saa7111a, o, f, ##args)
#define tda9840_call(mxb, o, f, args...) \
v4l2_subdev_call(mxb->tda9840, o, f, ##args)
#define tea6415c_call(mxb, o, f, args...) \
v4l2_subdev_call(mxb->tea6415c, o, f, ##args)
#define tuner_call(mxb, o, f, args...) \
v4l2_subdev_call(mxb->tuner, o, f, ##args)
#define call_all(dev, o, f, args...) \
v4l2_device_call_until_err(&dev->v4l2_dev, 0, o, f, ##args)
static void mxb_update_audmode(struct mxb *mxb)
{
struct v4l2_tuner t = {
.audmode = mxb->cur_mode,
};
tda9840_call(mxb, tuner, s_tuner, &t);
}
static inline void tea6420_route(struct mxb *mxb, int idx)
{
v4l2_subdev_call(mxb->tea6420_1, audio, s_routing,
TEA6420_cd[idx][0].input, TEA6420_cd[idx][0].output, 0);
v4l2_subdev_call(mxb->tea6420_2, audio, s_routing,
TEA6420_cd[idx][1].input, TEA6420_cd[idx][1].output, 0);
v4l2_subdev_call(mxb->tea6420_1, audio, s_routing,
TEA6420_line[idx][0].input, TEA6420_line[idx][0].output, 0);
v4l2_subdev_call(mxb->tea6420_2, audio, s_routing,
TEA6420_line[idx][1].input, TEA6420_line[idx][1].output, 0);
}
static struct saa7146_extension extension;
static int mxb_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct saa7146_dev *dev = container_of(ctrl->handler,
struct saa7146_dev, ctrl_handler);
struct mxb *mxb = dev->ext_priv;
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
mxb->cur_mute = ctrl->val;
/* switch the audio-source */
tea6420_route(mxb, ctrl->val ? 6 :
video_audio_connect[mxb->cur_input]);
break;
default:
return -EINVAL;
}
return 0;
}
static const struct v4l2_ctrl_ops mxb_ctrl_ops = {
.s_ctrl = mxb_s_ctrl,
};
static int mxb_probe(struct saa7146_dev *dev)
{
struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
struct mxb *mxb = NULL;
v4l2_ctrl_new_std(hdl, &mxb_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
if (hdl->error)
return hdl->error;
mxb = kzalloc(sizeof(struct mxb), GFP_KERNEL);
if (mxb == NULL) {
DEB_D("not enough kernel memory\n");
return -ENOMEM;
}
snprintf(mxb->i2c_adapter.name, sizeof(mxb->i2c_adapter.name), "mxb%d", mxb_num);
saa7146_i2c_adapter_prepare(dev, &mxb->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
if (i2c_add_adapter(&mxb->i2c_adapter) < 0) {
DEB_S("cannot register i2c-device. skipping.\n");
kfree(mxb);
return -EFAULT;
}
mxb->saa7111a = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
"saa7111", I2C_SAA7111A, NULL);
mxb->tea6420_1 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
"tea6420", I2C_TEA6420_1, NULL);
mxb->tea6420_2 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
"tea6420", I2C_TEA6420_2, NULL);
mxb->tea6415c = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
"tea6415c", I2C_TEA6415C, NULL);
mxb->tda9840 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
"tda9840", I2C_TDA9840, NULL);
mxb->tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev, &mxb->i2c_adapter,
"tuner", I2C_TUNER, NULL);
/* check if all devices are present */
if (!mxb->tea6420_1 || !mxb->tea6420_2 || !mxb->tea6415c ||
!mxb->tda9840 || !mxb->saa7111a || !mxb->tuner) {
pr_err("did not find all i2c devices. aborting\n");
i2c_del_adapter(&mxb->i2c_adapter);
kfree(mxb);
return -ENODEV;
}
/* all devices are present, probe was successful */
/* we store the pointer in our private data field */
dev->ext_priv = mxb;
v4l2_ctrl_handler_setup(hdl);
return 0;
}
/* some init data for the saa7740, the so-called 'sound arena module'.
there are no specs available, so we simply use some init values */
static struct {
int length;
char data[9];
} mxb_saa7740_init[] = {
{ 3, { 0x80, 0x00, 0x00 } },{ 3, { 0x80, 0x89, 0x00 } },
{ 3, { 0x80, 0xb0, 0x0a } },{ 3, { 0x00, 0x00, 0x00 } },
{ 3, { 0x49, 0x00, 0x00 } },{ 3, { 0x4a, 0x00, 0x00 } },
{ 3, { 0x4b, 0x00, 0x00 } },{ 3, { 0x4c, 0x00, 0x00 } },
{ 3, { 0x4d, 0x00, 0x00 } },{ 3, { 0x4e, 0x00, 0x00 } },
{ 3, { 0x4f, 0x00, 0x00 } },{ 3, { 0x50, 0x00, 0x00 } },
{ 3, { 0x51, 0x00, 0x00 } },{ 3, { 0x52, 0x00, 0x00 } },
{ 3, { 0x53, 0x00, 0x00 } },{ 3, { 0x54, 0x00, 0x00 } },
{ 3, { 0x55, 0x00, 0x00 } },{ 3, { 0x56, 0x00, 0x00 } },
{ 3, { 0x57, 0x00, 0x00 } },{ 3, { 0x58, 0x00, 0x00 } },
{ 3, { 0x59, 0x00, 0x00 } },{ 3, { 0x5a, 0x00, 0x00 } },
{ 3, { 0x5b, 0x00, 0x00 } },{ 3, { 0x5c, 0x00, 0x00 } },
{ 3, { 0x5d, 0x00, 0x00 } },{ 3, { 0x5e, 0x00, 0x00 } },
{ 3, { 0x5f, 0x00, 0x00 } },{ 3, { 0x60, 0x00, 0x00 } },
{ 3, { 0x61, 0x00, 0x00 } },{ 3, { 0x62, 0x00, 0x00 } },
{ 3, { 0x63, 0x00, 0x00 } },{ 3, { 0x64, 0x00, 0x00 } },
{ 3, { 0x65, 0x00, 0x00 } },{ 3, { 0x66, 0x00, 0x00 } },
{ 3, { 0x67, 0x00, 0x00 } },{ 3, { 0x68, 0x00, 0x00 } },
{ 3, { 0x69, 0x00, 0x00 } },{ 3, { 0x6a, 0x00, 0x00 } },
{ 3, { 0x6b, 0x00, 0x00 } },{ 3, { 0x6c, 0x00, 0x00 } },
{ 3, { 0x6d, 0x00, 0x00 } },{ 3, { 0x6e, 0x00, 0x00 } },
{ 3, { 0x6f, 0x00, 0x00 } },{ 3, { 0x70, 0x00, 0x00 } },
{ 3, { 0x71, 0x00, 0x00 } },{ 3, { 0x72, 0x00, 0x00 } },
{ 3, { 0x73, 0x00, 0x00 } },{ 3, { 0x74, 0x00, 0x00 } },
{ 3, { 0x75, 0x00, 0x00 } },{ 3, { 0x76, 0x00, 0x00 } },
{ 3, { 0x77, 0x00, 0x00 } },{ 3, { 0x41, 0x00, 0x42 } },
{ 3, { 0x42, 0x10, 0x42 } },{ 3, { 0x43, 0x20, 0x42 } },
{ 3, { 0x44, 0x30, 0x42 } },{ 3, { 0x45, 0x00, 0x01 } },
{ 3, { 0x46, 0x00, 0x01 } },{ 3, { 0x47, 0x00, 0x01 } },
{ 3, { 0x48, 0x00, 0x01 } },
{ 9, { 0x01, 0x03, 0xc5, 0x5c, 0x7a, 0x85, 0x01, 0x00, 0x54 } },
{ 9, { 0x21, 0x03, 0xc5, 0x5c, 0x7a, 0x85, 0x01, 0x00, 0x54 } },
{ 9, { 0x09, 0x0b, 0xb4, 0x6b, 0x74, 0x85, 0x95, 0x00, 0x34 } },
{ 9, { 0x29, 0x0b, 0xb4, 0x6b, 0x74, 0x85, 0x95, 0x00, 0x34 } },
{ 9, { 0x11, 0x17, 0x43, 0x62, 0x68, 0x89, 0xd1, 0xff, 0xb0 } },
{ 9, { 0x31, 0x17, 0x43, 0x62, 0x68, 0x89, 0xd1, 0xff, 0xb0 } },
{ 9, { 0x19, 0x20, 0x62, 0x51, 0x5a, 0x95, 0x19, 0x01, 0x50 } },
{ 9, { 0x39, 0x20, 0x62, 0x51, 0x5a, 0x95, 0x19, 0x01, 0x50 } },
{ 9, { 0x05, 0x3e, 0xd2, 0x69, 0x4e, 0x9a, 0x51, 0x00, 0xf0 } },
{ 9, { 0x25, 0x3e, 0xd2, 0x69, 0x4e, 0x9a, 0x51, 0x00, 0xf0 } },
{ 9, { 0x0d, 0x3d, 0xa1, 0x40, 0x7d, 0x9f, 0x29, 0xfe, 0x14 } },
{ 9, { 0x2d, 0x3d, 0xa1, 0x40, 0x7d, 0x9f, 0x29, 0xfe, 0x14 } },
{ 9, { 0x15, 0x73, 0xa1, 0x50, 0x5d, 0xa6, 0xf5, 0xfe, 0x38 } },
{ 9, { 0x35, 0x73, 0xa1, 0x50, 0x5d, 0xa6, 0xf5, 0xfe, 0x38 } },
{ 9, { 0x1d, 0xed, 0xd0, 0x68, 0x29, 0xb4, 0xe1, 0x00, 0xb8 } },
{ 9, { 0x3d, 0xed, 0xd0, 0x68, 0x29, 0xb4, 0xe1, 0x00, 0xb8 } },
{ 3, { 0x80, 0xb3, 0x0a } },
{-1, { 0 } }
};
/* bring hardware to a sane state. this has to be done, just in case someone
wants to capture from this device before it has been properly initialized.
the capture engine would badly fail, because no valid signal arrives on the
saa7146, thus leading to timeouts and stuff. */
static int mxb_init_done(struct saa7146_dev* dev)
{
struct mxb* mxb = (struct mxb*)dev->ext_priv;
struct i2c_msg msg;
struct tuner_setup tun_setup;
v4l2_std_id std = V4L2_STD_PAL_BG;
int i, err = 0;
/* mute audio on tea6420s */
tea6420_route(mxb, 6);
/* select video mode in saa7111a */
saa7111a_call(mxb, video, s_std, std);
/* select tuner-output on saa7111a */
saa7111a_call(mxb, video, s_routing, SAA7115_COMPOSITE0,
SAA7111_FMT_CCIR, 0);
/* select a tuner type */
tun_setup.mode_mask = T_ANALOG_TV;
tun_setup.addr = ADDR_UNSET;
tun_setup.type = TUNER_PHILIPS_PAL;
tuner_call(mxb, tuner, s_type_addr, &tun_setup);
/* tune in some frequency on tuner */
mxb->cur_freq.tuner = 0;
mxb->cur_freq.type = V4L2_TUNER_ANALOG_TV;
mxb->cur_freq.frequency = freq;
tuner_call(mxb, tuner, s_frequency, &mxb->cur_freq);
/* set a default video standard */
/* These two gpio calls set the GPIO pins that control the tda9820 */
saa7146_write(dev, GPIO_CTRL, 0x00404050);
saa7111a_call(mxb, core, s_gpio, 1);
saa7111a_call(mxb, video, s_std, std);
tuner_call(mxb, video, s_std, std);
/* switch to tuner-channel on tea6415c */
tea6415c_call(mxb, video, s_routing, 3, 17, 0);
/* select tuner-output on multicable on tea6415c */
tea6415c_call(mxb, video, s_routing, 3, 13, 0);
/* the rest for mxb */
mxb->cur_input = 0;
mxb->cur_audinput = video_audio_connect[mxb->cur_input];
mxb->cur_mute = 1;
mxb->cur_mode = V4L2_TUNER_MODE_STEREO;
mxb_update_audmode(mxb);
/* check if the saa7740 (aka 'sound arena module') is present
on the mxb. if so, we must initialize it. due to lack of
information about the saa7740, the values were reverse
engineered. */
msg.addr = 0x1b;
msg.flags = 0;
msg.len = mxb_saa7740_init[0].length;
msg.buf = &mxb_saa7740_init[0].data[0];
err = i2c_transfer(&mxb->i2c_adapter, &msg, 1);
if (err == 1) {
/* the sound arena module is a pos, that's probably the reason
philips refuses to hand out a datasheet for the saa7740...
it seems to screw up the i2c bus, so we disable fast irq
based i2c transactions here and rely on the slow and safe
polling method ... */
extension.flags &= ~SAA7146_USE_I2C_IRQ;
for (i = 1; ; i++) {
if (-1 == mxb_saa7740_init[i].length)
break;
msg.len = mxb_saa7740_init[i].length;
msg.buf = &mxb_saa7740_init[i].data[0];
err = i2c_transfer(&mxb->i2c_adapter, &msg, 1);
if (err != 1) {
DEB_D("failed to initialize 'sound arena module'\n");
goto err;
}
}
pr_info("'sound arena module' detected\n");
}
err:
/* the rest for saa7146: you should definitely set some basic values
for the input-port handling of the saa7146. */
/* ext->saa has been filled by the core driver */
/* some stuff is done via variables */
saa7146_set_hps_source_and_sync(dev, input_port_selection[mxb->cur_input].hps_source,
input_port_selection[mxb->cur_input].hps_sync);
/* some stuff is done via direct write to the registers */
/* this is ugly, but because of the fact that this is completely
hardware dependend, it should be done directly... */
saa7146_write(dev, DD1_STREAM_B, 0x00000000);
saa7146_write(dev, DD1_INIT, 0x02000200);
saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
return 0;
}
/* interrupt-handler. this gets called when irq_mask is != 0.
it must clear the interrupt-bits in irq_mask it has handled */
/*
void mxb_irq_bh(struct saa7146_dev* dev, u32* irq_mask)
{
struct mxb* mxb = (struct mxb*)dev->ext_priv;
}
*/
static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
{
DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
if (i->index >= MXB_INPUTS)
return -EINVAL;
memcpy(i, &mxb_inputs[i->index], sizeof(struct v4l2_input));
return 0;
}
static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
*i = mxb->cur_input;
DEB_EE("VIDIOC_G_INPUT %d\n", *i);
return 0;
}
static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
int err = 0;
int i = 0;
DEB_EE("VIDIOC_S_INPUT %d\n", input);
if (input >= MXB_INPUTS)
return -EINVAL;
mxb->cur_input = input;
saa7146_set_hps_source_and_sync(dev, input_port_selection[input].hps_source,
input_port_selection[input].hps_sync);
/* prepare switching of tea6415c and saa7111a;
have a look at the 'background'-file for further information */
switch (input) {
case TUNER:
i = SAA7115_COMPOSITE0;
err = tea6415c_call(mxb, video, s_routing, 3, 17, 0);
/* connect tuner-output always to multicable */
if (!err)
err = tea6415c_call(mxb, video, s_routing, 3, 13, 0);
break;
case AUX3_YC:
/* nothing to be done here. aux3_yc is
directly connected to the saa711a */
i = SAA7115_SVIDEO1;
break;
case AUX3:
/* nothing to be done here. aux3 is
directly connected to the saa711a */
i = SAA7115_COMPOSITE1;
break;
case AUX1:
i = SAA7115_COMPOSITE0;
err = tea6415c_call(mxb, video, s_routing, 1, 17, 0);
break;
}
if (err)
return err;
mxb->video_dev.tvnorms = mxb_inputs[input].std;
mxb->vbi_dev.tvnorms = mxb_inputs[input].std;
/* switch video in saa7111a */
if (saa7111a_call(mxb, video, s_routing, i, SAA7111_FMT_CCIR, 0))
pr_err("VIDIOC_S_INPUT: could not address saa7111a\n");
mxb->cur_audinput = video_audio_connect[input];
/* switch the audio-source only if necessary */
if (0 == mxb->cur_mute)
tea6420_route(mxb, mxb->cur_audinput);
if (mxb->cur_audinput == 0)
mxb_update_audmode(mxb);
return 0;
}
static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
if (t->index) {
DEB_D("VIDIOC_G_TUNER: channel %d does not have a tuner attached\n",
t->index);
return -EINVAL;
}
DEB_EE("VIDIOC_G_TUNER: %d\n", t->index);
memset(t, 0, sizeof(*t));
strscpy(t->name, "TV Tuner", sizeof(t->name));
t->type = V4L2_TUNER_ANALOG_TV;
t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
t->audmode = mxb->cur_mode;
return call_all(dev, tuner, g_tuner, t);
}
static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *t)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
if (t->index) {
DEB_D("VIDIOC_S_TUNER: channel %d does not have a tuner attached\n",
t->index);
return -EINVAL;
}
mxb->cur_mode = t->audmode;
return call_all(dev, tuner, s_tuner, t);
}
static int vidioc_querystd(struct file *file, void *fh, v4l2_std_id *norm)
{
struct saa7146_dev *dev = video_drvdata(file);
return call_all(dev, video, querystd, norm);
}
static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
if (f->tuner)
return -EINVAL;
*f = mxb->cur_freq;
DEB_EE("VIDIOC_G_FREQ: freq:0x%08x\n", mxb->cur_freq.frequency);
return 0;
}
static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *f)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
if (f->tuner)
return -EINVAL;
if (V4L2_TUNER_ANALOG_TV != f->type)
return -EINVAL;
DEB_EE("VIDIOC_S_FREQUENCY: freq:0x%08x\n", mxb->cur_freq.frequency);
/* tune in desired frequency */
tuner_call(mxb, tuner, s_frequency, f);
/* let the tuner subdev clamp the frequency to the tuner range */
mxb->cur_freq = *f;
tuner_call(mxb, tuner, g_frequency, &mxb->cur_freq);
if (mxb->cur_audinput == 0)
mxb_update_audmode(mxb);
return 0;
}
static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
{
if (a->index >= MXB_AUDIOS)
return -EINVAL;
*a = mxb_audios[a->index];
return 0;
}
static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
DEB_EE("VIDIOC_G_AUDIO\n");
*a = mxb_audios[mxb->cur_audinput];
return 0;
}
static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
{
struct saa7146_dev *dev = video_drvdata(file);
struct mxb *mxb = (struct mxb *)dev->ext_priv;
DEB_D("VIDIOC_S_AUDIO %d\n", a->index);
if (a->index >= 32 ||
!(mxb_inputs[mxb->cur_input].audioset & (1 << a->index)))
return -EINVAL;
if (mxb->cur_audinput != a->index) {
mxb->cur_audinput = a->index;
tea6420_route(mxb, a->index);
if (mxb->cur_audinput == 0)
mxb_update_audmode(mxb);
}
return 0;
}
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int vidioc_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
{
struct saa7146_dev *dev = video_drvdata(file);
if (reg->reg > pci_resource_len(dev->pci, 0) - 4)
return -EINVAL;
reg->val = saa7146_read(dev, reg->reg);
reg->size = 4;
return 0;
}
static int vidioc_s_register(struct file *file, void *fh, const struct v4l2_dbg_register *reg)
{
struct saa7146_dev *dev = video_drvdata(file);
if (reg->reg > pci_resource_len(dev->pci, 0) - 4)
return -EINVAL;
saa7146_write(dev, reg->reg, reg->val);
return 0;
}
#endif
static struct saa7146_ext_vv vv_data;
/* this function only gets called when the probing was successful */
static int mxb_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
{
struct mxb *mxb;
int ret;
DEB_EE("dev:%p\n", dev);
ret = saa7146_vv_init(dev, &vv_data);
if (ret) {
ERR("Error in saa7146_vv_init()");
return ret;
}
if (mxb_probe(dev)) {
saa7146_vv_release(dev);
return -1;
}
mxb = (struct mxb *)dev->ext_priv;
vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
vv_data.vid_ops.vidioc_querystd = vidioc_querystd;
vv_data.vid_ops.vidioc_g_tuner = vidioc_g_tuner;
vv_data.vid_ops.vidioc_s_tuner = vidioc_s_tuner;
vv_data.vid_ops.vidioc_g_frequency = vidioc_g_frequency;
vv_data.vid_ops.vidioc_s_frequency = vidioc_s_frequency;
vv_data.vid_ops.vidioc_enumaudio = vidioc_enumaudio;
vv_data.vid_ops.vidioc_g_audio = vidioc_g_audio;
vv_data.vid_ops.vidioc_s_audio = vidioc_s_audio;
#ifdef CONFIG_VIDEO_ADV_DEBUG
vv_data.vid_ops.vidioc_g_register = vidioc_g_register;
vv_data.vid_ops.vidioc_s_register = vidioc_s_register;
#endif
vv_data.vbi_ops.vidioc_enum_input = vidioc_enum_input;
vv_data.vbi_ops.vidioc_g_input = vidioc_g_input;
vv_data.vbi_ops.vidioc_s_input = vidioc_s_input;
vv_data.vbi_ops.vidioc_querystd = vidioc_querystd;
vv_data.vbi_ops.vidioc_g_tuner = vidioc_g_tuner;
vv_data.vbi_ops.vidioc_s_tuner = vidioc_s_tuner;
vv_data.vbi_ops.vidioc_g_frequency = vidioc_g_frequency;
vv_data.vbi_ops.vidioc_s_frequency = vidioc_s_frequency;
vv_data.vbi_ops.vidioc_enumaudio = vidioc_enumaudio;
vv_data.vbi_ops.vidioc_g_audio = vidioc_g_audio;
vv_data.vbi_ops.vidioc_s_audio = vidioc_s_audio;
if (saa7146_register_device(&mxb->video_dev, dev, "mxb", VFL_TYPE_VIDEO)) {
ERR("cannot register capture v4l2 device. skipping.\n");
saa7146_vv_release(dev);
return -1;
}
/* initialization stuff (vbi) (only for revision > 0 and for extensions which want it)*/
if (MXB_BOARD_CAN_DO_VBI(dev)) {
if (saa7146_register_device(&mxb->vbi_dev, dev, "mxb", VFL_TYPE_VBI)) {
ERR("cannot register vbi v4l2 device. skipping.\n");
}
}
pr_info("found Multimedia eXtension Board #%d\n", mxb_num);
mxb_num++;
mxb_init_done(dev);
return 0;
}
static int mxb_detach(struct saa7146_dev *dev)
{
struct mxb *mxb = (struct mxb *)dev->ext_priv;
DEB_EE("dev:%p\n", dev);
/* mute audio on tea6420s */
tea6420_route(mxb, 6);
saa7146_unregister_device(&mxb->video_dev,dev);
if (MXB_BOARD_CAN_DO_VBI(dev))
saa7146_unregister_device(&mxb->vbi_dev, dev);
saa7146_vv_release(dev);
mxb_num--;
i2c_del_adapter(&mxb->i2c_adapter);
kfree(mxb);
return 0;
}
static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *standard)
{
struct mxb *mxb = (struct mxb *)dev->ext_priv;
if (V4L2_STD_PAL_I == standard->id) {
v4l2_std_id std = V4L2_STD_PAL_I;
DEB_D("VIDIOC_S_STD: setting mxb for PAL_I\n");
/* These two gpio calls set the GPIO pins that control the tda9820 */
saa7146_write(dev, GPIO_CTRL, 0x00404050);
saa7111a_call(mxb, core, s_gpio, 0);
saa7111a_call(mxb, video, s_std, std);
if (mxb->cur_input == 0)
tuner_call(mxb, video, s_std, std);
} else {
v4l2_std_id std = V4L2_STD_PAL_BG;
if (mxb->cur_input)
std = standard->id;
DEB_D("VIDIOC_S_STD: setting mxb for PAL/NTSC/SECAM\n");
/* These two gpio calls set the GPIO pins that control the tda9820 */
saa7146_write(dev, GPIO_CTRL, 0x00404050);
saa7111a_call(mxb, core, s_gpio, 1);
saa7111a_call(mxb, video, s_std, std);
if (mxb->cur_input == 0)
tuner_call(mxb, video, s_std, std);
}
return 0;
}
static struct saa7146_standard standard[] = {
{
.name = "PAL-BG", .id = V4L2_STD_PAL_BG,
.v_offset = 0x17, .v_field = 288,
.h_offset = 0x14, .h_pixels = 680,
.v_max_out = 576, .h_max_out = 768,
}, {
.name = "PAL-I", .id = V4L2_STD_PAL_I,
.v_offset = 0x17, .v_field = 288,
.h_offset = 0x14, .h_pixels = 680,
.v_max_out = 576, .h_max_out = 768,
}, {
.name = "NTSC", .id = V4L2_STD_NTSC,
.v_offset = 0x16, .v_field = 240,
.h_offset = 0x06, .h_pixels = 708,
.v_max_out = 480, .h_max_out = 640,
}, {
.name = "SECAM", .id = V4L2_STD_SECAM,
.v_offset = 0x14, .v_field = 288,
.h_offset = 0x14, .h_pixels = 720,
.v_max_out = 576, .h_max_out = 768,
}
};
static struct saa7146_pci_extension_data mxb = {
.ext_priv = "Multimedia eXtension Board",
.ext = &extension,
};
static const struct pci_device_id pci_tbl[] = {
{
.vendor = PCI_VENDOR_ID_PHILIPS,
.device = PCI_DEVICE_ID_PHILIPS_SAA7146,
.subvendor = 0x0000,
.subdevice = 0x0000,
.driver_data = (unsigned long)&mxb,
}, {
.vendor = 0,
}
};
MODULE_DEVICE_TABLE(pci, pci_tbl);
static struct saa7146_ext_vv vv_data = {
.inputs = MXB_INPUTS,
.capabilities = V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_AUDIO,
.stds = &standard[0],
.num_stds = ARRAY_SIZE(standard),
.std_callback = &std_callback,
};
static struct saa7146_extension extension = {
.name = "Multimedia eXtension Board",
.flags = SAA7146_USE_I2C_IRQ,
.pci_tbl = &pci_tbl[0],
.module = THIS_MODULE,
.attach = mxb_attach,
.detach = mxb_detach,
.irq_mask = 0,
.irq_func = NULL,
};
static int __init mxb_init_module(void)
{
if (saa7146_register_extension(&extension)) {
DEB_S("failed to register extension\n");
return -ENODEV;
}
return 0;
}
static void __exit mxb_cleanup_module(void)
{
saa7146_unregister_extension(&extension);
}
module_init(mxb_init_module);
module_exit(mxb_cleanup_module);
MODULE_DESCRIPTION("video4linux-2 driver for the Siemens-Nixdorf 'Multimedia eXtension board'");
MODULE_AUTHOR("Michael Hunold <[email protected]>");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/saa7146/mxb.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* driver for Earthsoft PT1/PT2
*
* Copyright (C) 2009 HIRANO Takahito <[email protected]>
*
* based on pt1dvr - http://pt1dvr.sourceforge.jp/
* by Tomoaki Ishikawa <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/hrtimer.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/pci.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <linux/ratelimit.h>
#include <linux/string.h>
#include <linux/i2c.h>
#include <media/dvbdev.h>
#include <media/dvb_demux.h>
#include <media/dmxdev.h>
#include <media/dvb_net.h>
#include <media/dvb_frontend.h>
#include "tc90522.h"
#include "qm1d1b0004.h"
#include "dvb-pll.h"
#define DRIVER_NAME "earth-pt1"
#define PT1_PAGE_SHIFT 12
#define PT1_PAGE_SIZE (1 << PT1_PAGE_SHIFT)
#define PT1_NR_UPACKETS 1024
#define PT1_NR_BUFS 511
struct pt1_buffer_page {
__le32 upackets[PT1_NR_UPACKETS];
};
struct pt1_table_page {
__le32 next_pfn;
__le32 buf_pfns[PT1_NR_BUFS];
};
struct pt1_buffer {
struct pt1_buffer_page *page;
dma_addr_t addr;
};
struct pt1_table {
struct pt1_table_page *page;
dma_addr_t addr;
struct pt1_buffer bufs[PT1_NR_BUFS];
};
enum pt1_fe_clk {
PT1_FE_CLK_20MHZ, /* PT1 */
PT1_FE_CLK_25MHZ, /* PT2 */
};
#define PT1_NR_ADAPS 4
struct pt1_adapter;
struct pt1 {
struct pci_dev *pdev;
void __iomem *regs;
struct i2c_adapter i2c_adap;
int i2c_running;
struct pt1_adapter *adaps[PT1_NR_ADAPS];
struct pt1_table *tables;
struct task_struct *kthread;
int table_index;
int buf_index;
struct mutex lock;
int power;
int reset;
enum pt1_fe_clk fe_clk;
};
struct pt1_adapter {
struct pt1 *pt1;
int index;
u8 *buf;
int upacket_count;
int packet_count;
int st_count;
struct dvb_adapter adap;
struct dvb_demux demux;
int users;
struct dmxdev dmxdev;
struct dvb_frontend *fe;
struct i2c_client *demod_i2c_client;
struct i2c_client *tuner_i2c_client;
int (*orig_set_voltage)(struct dvb_frontend *fe,
enum fe_sec_voltage voltage);
int (*orig_sleep)(struct dvb_frontend *fe);
int (*orig_init)(struct dvb_frontend *fe);
enum fe_sec_voltage voltage;
int sleep;
};
union pt1_tuner_config {
struct qm1d1b0004_config qm1d1b0004;
struct dvb_pll_config tda6651;
};
struct pt1_config {
struct i2c_board_info demod_info;
struct tc90522_config demod_cfg;
struct i2c_board_info tuner_info;
union pt1_tuner_config tuner_cfg;
};
static const struct pt1_config pt1_configs[PT1_NR_ADAPS] = {
{
.demod_info = {
I2C_BOARD_INFO(TC90522_I2C_DEV_SAT, 0x1b),
},
.tuner_info = {
I2C_BOARD_INFO("qm1d1b0004", 0x60),
},
},
{
.demod_info = {
I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x1a),
},
.tuner_info = {
I2C_BOARD_INFO("tda665x_earthpt1", 0x61),
},
},
{
.demod_info = {
I2C_BOARD_INFO(TC90522_I2C_DEV_SAT, 0x19),
},
.tuner_info = {
I2C_BOARD_INFO("qm1d1b0004", 0x60),
},
},
{
.demod_info = {
I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18),
},
.tuner_info = {
I2C_BOARD_INFO("tda665x_earthpt1", 0x61),
},
},
};
static const u8 va1j5jf8007s_20mhz_configs[][2] = {
{0x04, 0x02}, {0x0d, 0x55}, {0x11, 0x40}, {0x13, 0x80}, {0x17, 0x01},
{0x1c, 0x0a}, {0x1d, 0xaa}, {0x1e, 0x20}, {0x1f, 0x88}, {0x51, 0xb0},
{0x52, 0x89}, {0x53, 0xb3}, {0x5a, 0x2d}, {0x5b, 0xd3}, {0x85, 0x69},
{0x87, 0x04}, {0x8e, 0x02}, {0xa3, 0xf7}, {0xa5, 0xc0},
};
static const u8 va1j5jf8007s_25mhz_configs[][2] = {
{0x04, 0x02}, {0x11, 0x40}, {0x13, 0x80}, {0x17, 0x01}, {0x1c, 0x0a},
{0x1d, 0xaa}, {0x1e, 0x20}, {0x1f, 0x88}, {0x51, 0xb0}, {0x52, 0x89},
{0x53, 0xb3}, {0x5a, 0x2d}, {0x5b, 0xd3}, {0x85, 0x69}, {0x87, 0x04},
{0x8e, 0x26}, {0xa3, 0xf7}, {0xa5, 0xc0},
};
static const u8 va1j5jf8007t_20mhz_configs[][2] = {
{0x03, 0x90}, {0x14, 0x8f}, {0x1c, 0x2a}, {0x1d, 0xa8}, {0x1e, 0xa2},
{0x22, 0x83}, {0x31, 0x0d}, {0x32, 0xe0}, {0x39, 0xd3}, {0x3a, 0x00},
{0x3b, 0x11}, {0x3c, 0x3f},
{0x5c, 0x40}, {0x5f, 0x80}, {0x75, 0x02}, {0x76, 0x4e}, {0x77, 0x03},
{0xef, 0x01}
};
static const u8 va1j5jf8007t_25mhz_configs[][2] = {
{0x03, 0x90}, {0x1c, 0x2a}, {0x1d, 0xa8}, {0x1e, 0xa2}, {0x22, 0x83},
{0x3a, 0x04}, {0x3b, 0x11}, {0x3c, 0x3f}, {0x5c, 0x40}, {0x5f, 0x80},
{0x75, 0x0a}, {0x76, 0x4c}, {0x77, 0x03}, {0xef, 0x01}
};
static int config_demod(struct i2c_client *cl, enum pt1_fe_clk clk)
{
int ret;
bool is_sat;
const u8 (*cfg_data)[2];
int i, len;
is_sat = !strncmp(cl->name, TC90522_I2C_DEV_SAT,
strlen(TC90522_I2C_DEV_SAT));
if (is_sat) {
struct i2c_msg msg[2];
u8 wbuf, rbuf;
wbuf = 0x07;
msg[0].addr = cl->addr;
msg[0].flags = 0;
msg[0].len = 1;
msg[0].buf = &wbuf;
msg[1].addr = cl->addr;
msg[1].flags = I2C_M_RD;
msg[1].len = 1;
msg[1].buf = &rbuf;
ret = i2c_transfer(cl->adapter, msg, 2);
if (ret < 0)
return ret;
if (rbuf != 0x41)
return -EIO;
}
/* frontend init */
if (clk == PT1_FE_CLK_20MHZ) {
if (is_sat) {
cfg_data = va1j5jf8007s_20mhz_configs;
len = ARRAY_SIZE(va1j5jf8007s_20mhz_configs);
} else {
cfg_data = va1j5jf8007t_20mhz_configs;
len = ARRAY_SIZE(va1j5jf8007t_20mhz_configs);
}
} else {
if (is_sat) {
cfg_data = va1j5jf8007s_25mhz_configs;
len = ARRAY_SIZE(va1j5jf8007s_25mhz_configs);
} else {
cfg_data = va1j5jf8007t_25mhz_configs;
len = ARRAY_SIZE(va1j5jf8007t_25mhz_configs);
}
}
for (i = 0; i < len; i++) {
ret = i2c_master_send(cl, cfg_data[i], 2);
if (ret < 0)
return ret;
}
return 0;
}
/*
* Init registers for (each pair of) terrestrial/satellite block in demod.
* Note that resetting terr. block also resets its peer sat. block as well.
* This function must be called before configuring any demod block
* (before pt1_wakeup(), fe->ops.init()).
*/
static int pt1_demod_block_init(struct pt1 *pt1)
{
struct i2c_client *cl;
u8 buf[2] = {0x01, 0x80};
int ret;
int i;
/* reset all terr. & sat. pairs first */
for (i = 0; i < PT1_NR_ADAPS; i++) {
cl = pt1->adaps[i]->demod_i2c_client;
if (strncmp(cl->name, TC90522_I2C_DEV_TER,
strlen(TC90522_I2C_DEV_TER)))
continue;
ret = i2c_master_send(cl, buf, 2);
if (ret < 0)
return ret;
usleep_range(30000, 50000);
}
for (i = 0; i < PT1_NR_ADAPS; i++) {
cl = pt1->adaps[i]->demod_i2c_client;
if (strncmp(cl->name, TC90522_I2C_DEV_SAT,
strlen(TC90522_I2C_DEV_SAT)))
continue;
ret = i2c_master_send(cl, buf, 2);
if (ret < 0)
return ret;
usleep_range(30000, 50000);
}
return 0;
}
static void pt1_write_reg(struct pt1 *pt1, int reg, u32 data)
{
writel(data, pt1->regs + reg * 4);
}
static u32 pt1_read_reg(struct pt1 *pt1, int reg)
{
return readl(pt1->regs + reg * 4);
}
static unsigned int pt1_nr_tables = 8;
module_param_named(nr_tables, pt1_nr_tables, uint, 0);
static void pt1_increment_table_count(struct pt1 *pt1)
{
pt1_write_reg(pt1, 0, 0x00000020);
}
static void pt1_init_table_count(struct pt1 *pt1)
{
pt1_write_reg(pt1, 0, 0x00000010);
}
static void pt1_register_tables(struct pt1 *pt1, u32 first_pfn)
{
pt1_write_reg(pt1, 5, first_pfn);
pt1_write_reg(pt1, 0, 0x0c000040);
}
static void pt1_unregister_tables(struct pt1 *pt1)
{
pt1_write_reg(pt1, 0, 0x08080000);
}
static int pt1_sync(struct pt1 *pt1)
{
int i;
for (i = 0; i < 57; i++) {
if (pt1_read_reg(pt1, 0) & 0x20000000)
return 0;
pt1_write_reg(pt1, 0, 0x00000008);
}
dev_err(&pt1->pdev->dev, "could not sync\n");
return -EIO;
}
static u64 pt1_identify(struct pt1 *pt1)
{
int i;
u64 id = 0;
for (i = 0; i < 57; i++) {
id |= (u64)(pt1_read_reg(pt1, 0) >> 30 & 1) << i;
pt1_write_reg(pt1, 0, 0x00000008);
}
return id;
}
static int pt1_unlock(struct pt1 *pt1)
{
int i;
pt1_write_reg(pt1, 0, 0x00000008);
for (i = 0; i < 3; i++) {
if (pt1_read_reg(pt1, 0) & 0x80000000)
return 0;
usleep_range(1000, 2000);
}
dev_err(&pt1->pdev->dev, "could not unlock\n");
return -EIO;
}
static int pt1_reset_pci(struct pt1 *pt1)
{
int i;
pt1_write_reg(pt1, 0, 0x01010000);
pt1_write_reg(pt1, 0, 0x01000000);
for (i = 0; i < 10; i++) {
if (pt1_read_reg(pt1, 0) & 0x00000001)
return 0;
usleep_range(1000, 2000);
}
dev_err(&pt1->pdev->dev, "could not reset PCI\n");
return -EIO;
}
static int pt1_reset_ram(struct pt1 *pt1)
{
int i;
pt1_write_reg(pt1, 0, 0x02020000);
pt1_write_reg(pt1, 0, 0x02000000);
for (i = 0; i < 10; i++) {
if (pt1_read_reg(pt1, 0) & 0x00000002)
return 0;
usleep_range(1000, 2000);
}
dev_err(&pt1->pdev->dev, "could not reset RAM\n");
return -EIO;
}
static int pt1_do_enable_ram(struct pt1 *pt1)
{
int i, j;
u32 status;
status = pt1_read_reg(pt1, 0) & 0x00000004;
pt1_write_reg(pt1, 0, 0x00000002);
for (i = 0; i < 10; i++) {
for (j = 0; j < 1024; j++) {
if ((pt1_read_reg(pt1, 0) & 0x00000004) != status)
return 0;
}
usleep_range(1000, 2000);
}
dev_err(&pt1->pdev->dev, "could not enable RAM\n");
return -EIO;
}
static int pt1_enable_ram(struct pt1 *pt1)
{
int i, ret;
int phase;
usleep_range(1000, 2000);
phase = pt1->pdev->device == 0x211a ? 128 : 166;
for (i = 0; i < phase; i++) {
ret = pt1_do_enable_ram(pt1);
if (ret < 0)
return ret;
}
return 0;
}
static void pt1_disable_ram(struct pt1 *pt1)
{
pt1_write_reg(pt1, 0, 0x0b0b0000);
}
static void pt1_set_stream(struct pt1 *pt1, int index, int enabled)
{
pt1_write_reg(pt1, 2, 1 << (index + 8) | enabled << index);
}
static void pt1_init_streams(struct pt1 *pt1)
{
int i;
for (i = 0; i < PT1_NR_ADAPS; i++)
pt1_set_stream(pt1, i, 0);
}
static int pt1_filter(struct pt1 *pt1, struct pt1_buffer_page *page)
{
u32 upacket;
int i;
int index;
struct pt1_adapter *adap;
int offset;
u8 *buf;
int sc;
if (!page->upackets[PT1_NR_UPACKETS - 1])
return 0;
for (i = 0; i < PT1_NR_UPACKETS; i++) {
upacket = le32_to_cpu(page->upackets[i]);
index = (upacket >> 29) - 1;
if (index < 0 || index >= PT1_NR_ADAPS)
continue;
adap = pt1->adaps[index];
if (upacket >> 25 & 1)
adap->upacket_count = 0;
else if (!adap->upacket_count)
continue;
if (upacket >> 24 & 1)
printk_ratelimited(KERN_INFO "earth-pt1: device buffer overflowing. table[%d] buf[%d]\n",
pt1->table_index, pt1->buf_index);
sc = upacket >> 26 & 0x7;
if (adap->st_count != -1 && sc != ((adap->st_count + 1) & 0x7))
printk_ratelimited(KERN_INFO "earth-pt1: data loss in streamID(adapter)[%d]\n",
index);
adap->st_count = sc;
buf = adap->buf;
offset = adap->packet_count * 188 + adap->upacket_count * 3;
buf[offset] = upacket >> 16;
buf[offset + 1] = upacket >> 8;
if (adap->upacket_count != 62)
buf[offset + 2] = upacket;
if (++adap->upacket_count >= 63) {
adap->upacket_count = 0;
if (++adap->packet_count >= 21) {
dvb_dmx_swfilter_packets(&adap->demux, buf, 21);
adap->packet_count = 0;
}
}
}
page->upackets[PT1_NR_UPACKETS - 1] = 0;
return 1;
}
static int pt1_thread(void *data)
{
struct pt1 *pt1;
struct pt1_buffer_page *page;
bool was_frozen;
#define PT1_FETCH_DELAY 10
#define PT1_FETCH_DELAY_DELTA 2
pt1 = data;
set_freezable();
while (!kthread_freezable_should_stop(&was_frozen)) {
if (was_frozen) {
int i;
for (i = 0; i < PT1_NR_ADAPS; i++)
pt1_set_stream(pt1, i, !!pt1->adaps[i]->users);
}
page = pt1->tables[pt1->table_index].bufs[pt1->buf_index].page;
if (!pt1_filter(pt1, page)) {
ktime_t delay;
delay = ktime_set(0, PT1_FETCH_DELAY * NSEC_PER_MSEC);
set_current_state(TASK_INTERRUPTIBLE);
schedule_hrtimeout_range(&delay,
PT1_FETCH_DELAY_DELTA * NSEC_PER_MSEC,
HRTIMER_MODE_REL);
continue;
}
if (++pt1->buf_index >= PT1_NR_BUFS) {
pt1_increment_table_count(pt1);
pt1->buf_index = 0;
if (++pt1->table_index >= pt1_nr_tables)
pt1->table_index = 0;
}
}
return 0;
}
static void pt1_free_page(struct pt1 *pt1, void *page, dma_addr_t addr)
{
dma_free_coherent(&pt1->pdev->dev, PT1_PAGE_SIZE, page, addr);
}
static void *pt1_alloc_page(struct pt1 *pt1, dma_addr_t *addrp, u32 *pfnp)
{
void *page;
dma_addr_t addr;
page = dma_alloc_coherent(&pt1->pdev->dev, PT1_PAGE_SIZE, &addr,
GFP_KERNEL);
if (page == NULL)
return NULL;
BUG_ON(addr & (PT1_PAGE_SIZE - 1));
BUG_ON(addr >> PT1_PAGE_SHIFT >> 31 >> 1);
*addrp = addr;
*pfnp = addr >> PT1_PAGE_SHIFT;
return page;
}
static void pt1_cleanup_buffer(struct pt1 *pt1, struct pt1_buffer *buf)
{
pt1_free_page(pt1, buf->page, buf->addr);
}
static int
pt1_init_buffer(struct pt1 *pt1, struct pt1_buffer *buf, u32 *pfnp)
{
struct pt1_buffer_page *page;
dma_addr_t addr;
page = pt1_alloc_page(pt1, &addr, pfnp);
if (page == NULL)
return -ENOMEM;
page->upackets[PT1_NR_UPACKETS - 1] = 0;
buf->page = page;
buf->addr = addr;
return 0;
}
static void pt1_cleanup_table(struct pt1 *pt1, struct pt1_table *table)
{
int i;
for (i = 0; i < PT1_NR_BUFS; i++)
pt1_cleanup_buffer(pt1, &table->bufs[i]);
pt1_free_page(pt1, table->page, table->addr);
}
static int
pt1_init_table(struct pt1 *pt1, struct pt1_table *table, u32 *pfnp)
{
struct pt1_table_page *page;
dma_addr_t addr;
int i, ret;
u32 buf_pfn;
page = pt1_alloc_page(pt1, &addr, pfnp);
if (page == NULL)
return -ENOMEM;
for (i = 0; i < PT1_NR_BUFS; i++) {
ret = pt1_init_buffer(pt1, &table->bufs[i], &buf_pfn);
if (ret < 0)
goto err;
page->buf_pfns[i] = cpu_to_le32(buf_pfn);
}
pt1_increment_table_count(pt1);
table->page = page;
table->addr = addr;
return 0;
err:
while (i--)
pt1_cleanup_buffer(pt1, &table->bufs[i]);
pt1_free_page(pt1, page, addr);
return ret;
}
static void pt1_cleanup_tables(struct pt1 *pt1)
{
struct pt1_table *tables;
int i;
tables = pt1->tables;
pt1_unregister_tables(pt1);
for (i = 0; i < pt1_nr_tables; i++)
pt1_cleanup_table(pt1, &tables[i]);
vfree(tables);
}
static int pt1_init_tables(struct pt1 *pt1)
{
struct pt1_table *tables;
int i, ret;
u32 first_pfn, pfn;
if (!pt1_nr_tables)
return 0;
tables = vmalloc(array_size(pt1_nr_tables, sizeof(struct pt1_table)));
if (tables == NULL)
return -ENOMEM;
pt1_init_table_count(pt1);
i = 0;
ret = pt1_init_table(pt1, &tables[0], &first_pfn);
if (ret)
goto err;
i++;
while (i < pt1_nr_tables) {
ret = pt1_init_table(pt1, &tables[i], &pfn);
if (ret)
goto err;
tables[i - 1].page->next_pfn = cpu_to_le32(pfn);
i++;
}
tables[pt1_nr_tables - 1].page->next_pfn = cpu_to_le32(first_pfn);
pt1_register_tables(pt1, first_pfn);
pt1->tables = tables;
return 0;
err:
while (i--)
pt1_cleanup_table(pt1, &tables[i]);
vfree(tables);
return ret;
}
static int pt1_start_polling(struct pt1 *pt1)
{
int ret = 0;
mutex_lock(&pt1->lock);
if (!pt1->kthread) {
pt1->kthread = kthread_run(pt1_thread, pt1, "earth-pt1");
if (IS_ERR(pt1->kthread)) {
ret = PTR_ERR(pt1->kthread);
pt1->kthread = NULL;
}
}
mutex_unlock(&pt1->lock);
return ret;
}
static int pt1_start_feed(struct dvb_demux_feed *feed)
{
struct pt1_adapter *adap;
adap = container_of(feed->demux, struct pt1_adapter, demux);
if (!adap->users++) {
int ret;
ret = pt1_start_polling(adap->pt1);
if (ret)
return ret;
pt1_set_stream(adap->pt1, adap->index, 1);
}
return 0;
}
static void pt1_stop_polling(struct pt1 *pt1)
{
int i, count;
mutex_lock(&pt1->lock);
for (i = 0, count = 0; i < PT1_NR_ADAPS; i++)
count += pt1->adaps[i]->users;
if (count == 0 && pt1->kthread) {
kthread_stop(pt1->kthread);
pt1->kthread = NULL;
}
mutex_unlock(&pt1->lock);
}
static int pt1_stop_feed(struct dvb_demux_feed *feed)
{
struct pt1_adapter *adap;
adap = container_of(feed->demux, struct pt1_adapter, demux);
if (!--adap->users) {
pt1_set_stream(adap->pt1, adap->index, 0);
pt1_stop_polling(adap->pt1);
}
return 0;
}
static void
pt1_update_power(struct pt1 *pt1)
{
int bits;
int i;
struct pt1_adapter *adap;
static const int sleep_bits[] = {
1 << 4,
1 << 6 | 1 << 7,
1 << 5,
1 << 6 | 1 << 8,
};
bits = pt1->power | !pt1->reset << 3;
mutex_lock(&pt1->lock);
for (i = 0; i < PT1_NR_ADAPS; i++) {
adap = pt1->adaps[i];
switch (adap->voltage) {
case SEC_VOLTAGE_13: /* actually 11V */
bits |= 1 << 2;
break;
case SEC_VOLTAGE_18: /* actually 15V */
bits |= 1 << 1 | 1 << 2;
break;
default:
break;
}
/* XXX: The bits should be changed depending on adap->sleep. */
bits |= sleep_bits[i];
}
pt1_write_reg(pt1, 1, bits);
mutex_unlock(&pt1->lock);
}
static int pt1_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage voltage)
{
struct pt1_adapter *adap;
adap = container_of(fe->dvb, struct pt1_adapter, adap);
adap->voltage = voltage;
pt1_update_power(adap->pt1);
if (adap->orig_set_voltage)
return adap->orig_set_voltage(fe, voltage);
else
return 0;
}
static int pt1_sleep(struct dvb_frontend *fe)
{
struct pt1_adapter *adap;
int ret;
adap = container_of(fe->dvb, struct pt1_adapter, adap);
ret = 0;
if (adap->orig_sleep)
ret = adap->orig_sleep(fe);
adap->sleep = 1;
pt1_update_power(adap->pt1);
return ret;
}
static int pt1_wakeup(struct dvb_frontend *fe)
{
struct pt1_adapter *adap;
int ret;
adap = container_of(fe->dvb, struct pt1_adapter, adap);
adap->sleep = 0;
pt1_update_power(adap->pt1);
usleep_range(1000, 2000);
ret = config_demod(adap->demod_i2c_client, adap->pt1->fe_clk);
if (ret == 0 && adap->orig_init)
ret = adap->orig_init(fe);
return ret;
}
static void pt1_free_adapter(struct pt1_adapter *adap)
{
adap->demux.dmx.close(&adap->demux.dmx);
dvb_dmxdev_release(&adap->dmxdev);
dvb_dmx_release(&adap->demux);
dvb_unregister_adapter(&adap->adap);
free_page((unsigned long)adap->buf);
kfree(adap);
}
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
static struct pt1_adapter *
pt1_alloc_adapter(struct pt1 *pt1)
{
struct pt1_adapter *adap;
void *buf;
struct dvb_adapter *dvb_adap;
struct dvb_demux *demux;
struct dmxdev *dmxdev;
int ret;
adap = kzalloc(sizeof(struct pt1_adapter), GFP_KERNEL);
if (!adap) {
ret = -ENOMEM;
goto err;
}
adap->pt1 = pt1;
adap->voltage = SEC_VOLTAGE_OFF;
adap->sleep = 1;
buf = (u8 *)__get_free_page(GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto err_kfree;
}
adap->buf = buf;
adap->upacket_count = 0;
adap->packet_count = 0;
adap->st_count = -1;
dvb_adap = &adap->adap;
dvb_adap->priv = adap;
ret = dvb_register_adapter(dvb_adap, DRIVER_NAME, THIS_MODULE,
&pt1->pdev->dev, adapter_nr);
if (ret < 0)
goto err_free_page;
demux = &adap->demux;
demux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
demux->priv = adap;
demux->feednum = 256;
demux->filternum = 256;
demux->start_feed = pt1_start_feed;
demux->stop_feed = pt1_stop_feed;
demux->write_to_decoder = NULL;
ret = dvb_dmx_init(demux);
if (ret < 0)
goto err_unregister_adapter;
dmxdev = &adap->dmxdev;
dmxdev->filternum = 256;
dmxdev->demux = &demux->dmx;
dmxdev->capabilities = 0;
ret = dvb_dmxdev_init(dmxdev, dvb_adap);
if (ret < 0)
goto err_dmx_release;
return adap;
err_dmx_release:
dvb_dmx_release(demux);
err_unregister_adapter:
dvb_unregister_adapter(dvb_adap);
err_free_page:
free_page((unsigned long)buf);
err_kfree:
kfree(adap);
err:
return ERR_PTR(ret);
}
static void pt1_cleanup_adapters(struct pt1 *pt1)
{
int i;
for (i = 0; i < PT1_NR_ADAPS; i++)
pt1_free_adapter(pt1->adaps[i]);
}
static int pt1_init_adapters(struct pt1 *pt1)
{
int i;
struct pt1_adapter *adap;
int ret;
for (i = 0; i < PT1_NR_ADAPS; i++) {
adap = pt1_alloc_adapter(pt1);
if (IS_ERR(adap)) {
ret = PTR_ERR(adap);
goto err;
}
adap->index = i;
pt1->adaps[i] = adap;
}
return 0;
err:
while (i--)
pt1_free_adapter(pt1->adaps[i]);
return ret;
}
static void pt1_cleanup_frontend(struct pt1_adapter *adap)
{
dvb_unregister_frontend(adap->fe);
dvb_module_release(adap->tuner_i2c_client);
dvb_module_release(adap->demod_i2c_client);
}
static int pt1_init_frontend(struct pt1_adapter *adap, struct dvb_frontend *fe)
{
int ret;
adap->orig_set_voltage = fe->ops.set_voltage;
adap->orig_sleep = fe->ops.sleep;
adap->orig_init = fe->ops.init;
fe->ops.set_voltage = pt1_set_voltage;
fe->ops.sleep = pt1_sleep;
fe->ops.init = pt1_wakeup;
ret = dvb_register_frontend(&adap->adap, fe);
if (ret < 0)
return ret;
adap->fe = fe;
return 0;
}
static void pt1_cleanup_frontends(struct pt1 *pt1)
{
int i;
for (i = 0; i < PT1_NR_ADAPS; i++)
pt1_cleanup_frontend(pt1->adaps[i]);
}
static int pt1_init_frontends(struct pt1 *pt1)
{
int i;
int ret;
for (i = 0; i < ARRAY_SIZE(pt1_configs); i++) {
const struct i2c_board_info *info;
struct tc90522_config dcfg;
struct i2c_client *cl;
info = &pt1_configs[i].demod_info;
dcfg = pt1_configs[i].demod_cfg;
dcfg.tuner_i2c = NULL;
ret = -ENODEV;
cl = dvb_module_probe("tc90522", info->type, &pt1->i2c_adap,
info->addr, &dcfg);
if (!cl)
goto fe_unregister;
pt1->adaps[i]->demod_i2c_client = cl;
if (!strncmp(cl->name, TC90522_I2C_DEV_SAT,
strlen(TC90522_I2C_DEV_SAT))) {
struct qm1d1b0004_config tcfg;
info = &pt1_configs[i].tuner_info;
tcfg = pt1_configs[i].tuner_cfg.qm1d1b0004;
tcfg.fe = dcfg.fe;
cl = dvb_module_probe("qm1d1b0004",
info->type, dcfg.tuner_i2c,
info->addr, &tcfg);
} else {
struct dvb_pll_config tcfg;
info = &pt1_configs[i].tuner_info;
tcfg = pt1_configs[i].tuner_cfg.tda6651;
tcfg.fe = dcfg.fe;
cl = dvb_module_probe("dvb_pll",
info->type, dcfg.tuner_i2c,
info->addr, &tcfg);
}
if (!cl)
goto demod_release;
pt1->adaps[i]->tuner_i2c_client = cl;
ret = pt1_init_frontend(pt1->adaps[i], dcfg.fe);
if (ret < 0)
goto tuner_release;
}
ret = pt1_demod_block_init(pt1);
if (ret < 0)
goto fe_unregister;
return 0;
tuner_release:
dvb_module_release(pt1->adaps[i]->tuner_i2c_client);
demod_release:
dvb_module_release(pt1->adaps[i]->demod_i2c_client);
fe_unregister:
dev_warn(&pt1->pdev->dev, "failed to init FE(%d).\n", i);
i--;
for (; i >= 0; i--) {
dvb_unregister_frontend(pt1->adaps[i]->fe);
dvb_module_release(pt1->adaps[i]->tuner_i2c_client);
dvb_module_release(pt1->adaps[i]->demod_i2c_client);
}
return ret;
}
static void pt1_i2c_emit(struct pt1 *pt1, int addr, int busy, int read_enable,
int clock, int data, int next_addr)
{
pt1_write_reg(pt1, 4, addr << 18 | busy << 13 | read_enable << 12 |
!clock << 11 | !data << 10 | next_addr);
}
static void pt1_i2c_write_bit(struct pt1 *pt1, int addr, int *addrp, int data)
{
pt1_i2c_emit(pt1, addr, 1, 0, 0, data, addr + 1);
pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, data, addr + 2);
pt1_i2c_emit(pt1, addr + 2, 1, 0, 0, data, addr + 3);
*addrp = addr + 3;
}
static void pt1_i2c_read_bit(struct pt1 *pt1, int addr, int *addrp)
{
pt1_i2c_emit(pt1, addr, 1, 0, 0, 1, addr + 1);
pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 1, addr + 2);
pt1_i2c_emit(pt1, addr + 2, 1, 1, 1, 1, addr + 3);
pt1_i2c_emit(pt1, addr + 3, 1, 0, 0, 1, addr + 4);
*addrp = addr + 4;
}
static void pt1_i2c_write_byte(struct pt1 *pt1, int addr, int *addrp, int data)
{
int i;
for (i = 0; i < 8; i++)
pt1_i2c_write_bit(pt1, addr, &addr, data >> (7 - i) & 1);
pt1_i2c_write_bit(pt1, addr, &addr, 1);
*addrp = addr;
}
static void pt1_i2c_read_byte(struct pt1 *pt1, int addr, int *addrp, int last)
{
int i;
for (i = 0; i < 8; i++)
pt1_i2c_read_bit(pt1, addr, &addr);
pt1_i2c_write_bit(pt1, addr, &addr, last);
*addrp = addr;
}
static void pt1_i2c_prepare(struct pt1 *pt1, int addr, int *addrp)
{
pt1_i2c_emit(pt1, addr, 1, 0, 1, 1, addr + 1);
pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 0, addr + 2);
pt1_i2c_emit(pt1, addr + 2, 1, 0, 0, 0, addr + 3);
*addrp = addr + 3;
}
static void
pt1_i2c_write_msg(struct pt1 *pt1, int addr, int *addrp, struct i2c_msg *msg)
{
int i;
pt1_i2c_prepare(pt1, addr, &addr);
pt1_i2c_write_byte(pt1, addr, &addr, msg->addr << 1);
for (i = 0; i < msg->len; i++)
pt1_i2c_write_byte(pt1, addr, &addr, msg->buf[i]);
*addrp = addr;
}
static void
pt1_i2c_read_msg(struct pt1 *pt1, int addr, int *addrp, struct i2c_msg *msg)
{
int i;
pt1_i2c_prepare(pt1, addr, &addr);
pt1_i2c_write_byte(pt1, addr, &addr, msg->addr << 1 | 1);
for (i = 0; i < msg->len; i++)
pt1_i2c_read_byte(pt1, addr, &addr, i == msg->len - 1);
*addrp = addr;
}
static int pt1_i2c_end(struct pt1 *pt1, int addr)
{
pt1_i2c_emit(pt1, addr, 1, 0, 0, 0, addr + 1);
pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 0, addr + 2);
pt1_i2c_emit(pt1, addr + 2, 1, 0, 1, 1, 0);
pt1_write_reg(pt1, 0, 0x00000004);
do {
if (signal_pending(current))
return -EINTR;
usleep_range(1000, 2000);
} while (pt1_read_reg(pt1, 0) & 0x00000080);
return 0;
}
static void pt1_i2c_begin(struct pt1 *pt1, int *addrp)
{
int addr = 0;
pt1_i2c_emit(pt1, addr, 0, 0, 1, 1, addr /* itself */);
addr = addr + 1;
if (!pt1->i2c_running) {
pt1_i2c_emit(pt1, addr, 1, 0, 1, 1, addr + 1);
pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 0, addr + 2);
addr = addr + 2;
pt1->i2c_running = 1;
}
*addrp = addr;
}
static int pt1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
struct pt1 *pt1;
int i;
struct i2c_msg *msg, *next_msg;
int addr, ret;
u16 len;
u32 word;
pt1 = i2c_get_adapdata(adap);
for (i = 0; i < num; i++) {
msg = &msgs[i];
if (msg->flags & I2C_M_RD)
return -ENOTSUPP;
if (i + 1 < num)
next_msg = &msgs[i + 1];
else
next_msg = NULL;
if (next_msg && next_msg->flags & I2C_M_RD) {
i++;
len = next_msg->len;
if (len > 4)
return -ENOTSUPP;
pt1_i2c_begin(pt1, &addr);
pt1_i2c_write_msg(pt1, addr, &addr, msg);
pt1_i2c_read_msg(pt1, addr, &addr, next_msg);
ret = pt1_i2c_end(pt1, addr);
if (ret < 0)
return ret;
word = pt1_read_reg(pt1, 2);
while (len--) {
next_msg->buf[len] = word;
word >>= 8;
}
} else {
pt1_i2c_begin(pt1, &addr);
pt1_i2c_write_msg(pt1, addr, &addr, msg);
ret = pt1_i2c_end(pt1, addr);
if (ret < 0)
return ret;
}
}
return num;
}
static u32 pt1_i2c_func(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C;
}
static const struct i2c_algorithm pt1_i2c_algo = {
.master_xfer = pt1_i2c_xfer,
.functionality = pt1_i2c_func,
};
static void pt1_i2c_wait(struct pt1 *pt1)
{
int i;
for (i = 0; i < 128; i++)
pt1_i2c_emit(pt1, 0, 0, 0, 1, 1, 0);
}
static void pt1_i2c_init(struct pt1 *pt1)
{
int i;
for (i = 0; i < 1024; i++)
pt1_i2c_emit(pt1, i, 0, 0, 1, 1, 0);
}
#ifdef CONFIG_PM_SLEEP
static int pt1_suspend(struct device *dev)
{
struct pt1 *pt1 = dev_get_drvdata(dev);
pt1_init_streams(pt1);
pt1_disable_ram(pt1);
pt1->power = 0;
pt1->reset = 1;
pt1_update_power(pt1);
return 0;
}
static int pt1_resume(struct device *dev)
{
struct pt1 *pt1 = dev_get_drvdata(dev);
int ret;
int i;
pt1->power = 0;
pt1->reset = 1;
pt1_update_power(pt1);
pt1_i2c_init(pt1);
pt1_i2c_wait(pt1);
ret = pt1_sync(pt1);
if (ret < 0)
goto resume_err;
pt1_identify(pt1);
ret = pt1_unlock(pt1);
if (ret < 0)
goto resume_err;
ret = pt1_reset_pci(pt1);
if (ret < 0)
goto resume_err;
ret = pt1_reset_ram(pt1);
if (ret < 0)
goto resume_err;
ret = pt1_enable_ram(pt1);
if (ret < 0)
goto resume_err;
pt1_init_streams(pt1);
pt1->power = 1;
pt1_update_power(pt1);
msleep(20);
pt1->reset = 0;
pt1_update_power(pt1);
usleep_range(1000, 2000);
ret = pt1_demod_block_init(pt1);
if (ret < 0)
goto resume_err;
for (i = 0; i < PT1_NR_ADAPS; i++)
dvb_frontend_reinitialise(pt1->adaps[i]->fe);
pt1_init_table_count(pt1);
for (i = 0; i < pt1_nr_tables; i++) {
int j;
for (j = 0; j < PT1_NR_BUFS; j++)
pt1->tables[i].bufs[j].page->upackets[PT1_NR_UPACKETS-1]
= 0;
pt1_increment_table_count(pt1);
}
pt1_register_tables(pt1, pt1->tables[0].addr >> PT1_PAGE_SHIFT);
pt1->table_index = 0;
pt1->buf_index = 0;
for (i = 0; i < PT1_NR_ADAPS; i++) {
pt1->adaps[i]->upacket_count = 0;
pt1->adaps[i]->packet_count = 0;
pt1->adaps[i]->st_count = -1;
}
return 0;
resume_err:
dev_info(&pt1->pdev->dev, "failed to resume PT1/PT2.");
return 0; /* resume anyway */
}
#endif /* CONFIG_PM_SLEEP */
static void pt1_remove(struct pci_dev *pdev)
{
struct pt1 *pt1;
void __iomem *regs;
pt1 = pci_get_drvdata(pdev);
regs = pt1->regs;
if (pt1->kthread)
kthread_stop(pt1->kthread);
pt1_cleanup_tables(pt1);
pt1_cleanup_frontends(pt1);
pt1_disable_ram(pt1);
pt1->power = 0;
pt1->reset = 1;
pt1_update_power(pt1);
pt1_cleanup_adapters(pt1);
i2c_del_adapter(&pt1->i2c_adap);
kfree(pt1);
pci_iounmap(pdev, regs);
pci_release_regions(pdev);
pci_disable_device(pdev);
}
static int pt1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
int ret;
void __iomem *regs;
struct pt1 *pt1;
struct i2c_adapter *i2c_adap;
ret = pci_enable_device(pdev);
if (ret < 0)
goto err;
ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
if (ret < 0)
goto err_pci_disable_device;
pci_set_master(pdev);
ret = pci_request_regions(pdev, DRIVER_NAME);
if (ret < 0)
goto err_pci_disable_device;
regs = pci_iomap(pdev, 0, 0);
if (!regs) {
ret = -EIO;
goto err_pci_release_regions;
}
pt1 = kzalloc(sizeof(struct pt1), GFP_KERNEL);
if (!pt1) {
ret = -ENOMEM;
goto err_pci_iounmap;
}
mutex_init(&pt1->lock);
pt1->pdev = pdev;
pt1->regs = regs;
pt1->fe_clk = (pdev->device == 0x211a) ?
PT1_FE_CLK_20MHZ : PT1_FE_CLK_25MHZ;
pci_set_drvdata(pdev, pt1);
ret = pt1_init_adapters(pt1);
if (ret < 0)
goto err_kfree;
mutex_init(&pt1->lock);
pt1->power = 0;
pt1->reset = 1;
pt1_update_power(pt1);
i2c_adap = &pt1->i2c_adap;
i2c_adap->algo = &pt1_i2c_algo;
i2c_adap->algo_data = NULL;
i2c_adap->dev.parent = &pdev->dev;
strscpy(i2c_adap->name, DRIVER_NAME, sizeof(i2c_adap->name));
i2c_set_adapdata(i2c_adap, pt1);
ret = i2c_add_adapter(i2c_adap);
if (ret < 0)
goto err_pt1_cleanup_adapters;
pt1_i2c_init(pt1);
pt1_i2c_wait(pt1);
ret = pt1_sync(pt1);
if (ret < 0)
goto err_i2c_del_adapter;
pt1_identify(pt1);
ret = pt1_unlock(pt1);
if (ret < 0)
goto err_i2c_del_adapter;
ret = pt1_reset_pci(pt1);
if (ret < 0)
goto err_i2c_del_adapter;
ret = pt1_reset_ram(pt1);
if (ret < 0)
goto err_i2c_del_adapter;
ret = pt1_enable_ram(pt1);
if (ret < 0)
goto err_i2c_del_adapter;
pt1_init_streams(pt1);
pt1->power = 1;
pt1_update_power(pt1);
msleep(20);
pt1->reset = 0;
pt1_update_power(pt1);
usleep_range(1000, 2000);
ret = pt1_init_frontends(pt1);
if (ret < 0)
goto err_pt1_disable_ram;
ret = pt1_init_tables(pt1);
if (ret < 0)
goto err_pt1_cleanup_frontends;
return 0;
err_pt1_cleanup_frontends:
pt1_cleanup_frontends(pt1);
err_pt1_disable_ram:
pt1_disable_ram(pt1);
pt1->power = 0;
pt1->reset = 1;
pt1_update_power(pt1);
err_i2c_del_adapter:
i2c_del_adapter(i2c_adap);
err_pt1_cleanup_adapters:
pt1_cleanup_adapters(pt1);
err_kfree:
kfree(pt1);
err_pci_iounmap:
pci_iounmap(pdev, regs);
err_pci_release_regions:
pci_release_regions(pdev);
err_pci_disable_device:
pci_disable_device(pdev);
err:
return ret;
}
static const struct pci_device_id pt1_id_table[] = {
{ PCI_DEVICE(0x10ee, 0x211a) },
{ PCI_DEVICE(0x10ee, 0x222a) },
{ },
};
MODULE_DEVICE_TABLE(pci, pt1_id_table);
static SIMPLE_DEV_PM_OPS(pt1_pm_ops, pt1_suspend, pt1_resume);
static struct pci_driver pt1_driver = {
.name = DRIVER_NAME,
.probe = pt1_probe,
.remove = pt1_remove,
.id_table = pt1_id_table,
.driver.pm = &pt1_pm_ops,
};
module_pci_driver(pt1_driver);
MODULE_AUTHOR("Takahito HIRANO <[email protected]>");
MODULE_DESCRIPTION("Earthsoft PT1/PT2 Driver");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/pt1/pt1.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* pluto2.c - Satelco Easywatch Mobile Terrestrial Receiver [DVB-T]
*
* Copyright (C) 2005 Andreas Oberritter <[email protected]>
*
* based on pluto2.c 1.10 - http://instinct-wp8.no-ip.org/pluto/
* by Dany Salman <[email protected]>
* Copyright (c) 2004 TDF
*/
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <media/demux.h>
#include <media/dmxdev.h>
#include <media/dvb_demux.h>
#include <media/dvb_frontend.h>
#include <media/dvb_net.h>
#include <media/dvbdev.h>
#include "tda1004x.h"
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
#define DRIVER_NAME "pluto2"
#define REG_PIDn(n) ((n) << 2) /* PID n pattern registers */
#define REG_PCAR 0x0020 /* PC address register */
#define REG_TSCR 0x0024 /* TS ctrl & status */
#define REG_MISC 0x0028 /* miscellaneous */
#define REG_MMAC 0x002c /* MSB MAC address */
#define REG_IMAC 0x0030 /* ISB MAC address */
#define REG_LMAC 0x0034 /* LSB MAC address */
#define REG_SPID 0x0038 /* SPI data */
#define REG_SLCS 0x003c /* serial links ctrl/status */
#define PID0_NOFIL (0x0001 << 16)
#define PIDn_ENP (0x0001 << 15)
#define PID0_END (0x0001 << 14)
#define PID0_AFIL (0x0001 << 13)
#define PIDn_PID (0x1fff << 0)
#define TSCR_NBPACKETS (0x00ff << 24)
#define TSCR_DEM (0x0001 << 17)
#define TSCR_DE (0x0001 << 16)
#define TSCR_RSTN (0x0001 << 15)
#define TSCR_MSKO (0x0001 << 14)
#define TSCR_MSKA (0x0001 << 13)
#define TSCR_MSKL (0x0001 << 12)
#define TSCR_OVR (0x0001 << 11)
#define TSCR_AFUL (0x0001 << 10)
#define TSCR_LOCK (0x0001 << 9)
#define TSCR_IACK (0x0001 << 8)
#define TSCR_ADEF (0x007f << 0)
#define MISC_DVR (0x0fff << 4)
#define MISC_ALED (0x0001 << 3)
#define MISC_FRST (0x0001 << 2)
#define MISC_LED1 (0x0001 << 1)
#define MISC_LED0 (0x0001 << 0)
#define SPID_SPIDR (0x00ff << 0)
#define SLCS_SCL (0x0001 << 7)
#define SLCS_SDA (0x0001 << 6)
#define SLCS_CSN (0x0001 << 2)
#define SLCS_OVR (0x0001 << 1)
#define SLCS_SWC (0x0001 << 0)
#define TS_DMA_PACKETS (8)
#define TS_DMA_BYTES (188 * TS_DMA_PACKETS)
#define I2C_ADDR_TDA10046 0x10
#define I2C_ADDR_TUA6034 0xc2
#define NHWFILTERS 8
struct pluto {
/* pci */
struct pci_dev *pdev;
u8 __iomem *io_mem;
/* dvb */
struct dmx_frontend hw_frontend;
struct dmx_frontend mem_frontend;
struct dmxdev dmxdev;
struct dvb_adapter dvb_adapter;
struct dvb_demux demux;
struct dvb_frontend *fe;
struct dvb_net dvbnet;
unsigned int full_ts_users;
unsigned int users;
/* i2c */
struct i2c_algo_bit_data i2c_bit;
struct i2c_adapter i2c_adap;
unsigned int i2cbug;
/* irq */
unsigned int overflow;
unsigned int dead;
/* dma */
dma_addr_t dma_addr;
u8 dma_buf[TS_DMA_BYTES];
u8 dummy[4096];
};
static inline struct pluto *feed_to_pluto(struct dvb_demux_feed *feed)
{
return container_of(feed->demux, struct pluto, demux);
}
static inline struct pluto *frontend_to_pluto(struct dvb_frontend *fe)
{
return container_of(fe->dvb, struct pluto, dvb_adapter);
}
static inline u32 pluto_readreg(struct pluto *pluto, u32 reg)
{
return readl(&pluto->io_mem[reg]);
}
static inline void pluto_writereg(struct pluto *pluto, u32 reg, u32 val)
{
writel(val, &pluto->io_mem[reg]);
}
static inline void pluto_rw(struct pluto *pluto, u32 reg, u32 mask, u32 bits)
{
u32 val = readl(&pluto->io_mem[reg]);
val &= ~mask;
val |= bits;
writel(val, &pluto->io_mem[reg]);
}
static void pluto_write_tscr(struct pluto *pluto, u32 val)
{
/* set the number of packets */
val &= ~TSCR_ADEF;
val |= TS_DMA_PACKETS / 2;
pluto_writereg(pluto, REG_TSCR, val);
}
static void pluto_setsda(void *data, int state)
{
struct pluto *pluto = data;
if (state)
pluto_rw(pluto, REG_SLCS, SLCS_SDA, SLCS_SDA);
else
pluto_rw(pluto, REG_SLCS, SLCS_SDA, 0);
}
static void pluto_setscl(void *data, int state)
{
struct pluto *pluto = data;
if (state)
pluto_rw(pluto, REG_SLCS, SLCS_SCL, SLCS_SCL);
else
pluto_rw(pluto, REG_SLCS, SLCS_SCL, 0);
/* try to detect i2c_inb() to workaround hardware bug:
* reset SDA to high after SCL has been set to low */
if ((state) && (pluto->i2cbug == 0)) {
pluto->i2cbug = 1;
} else {
if ((!state) && (pluto->i2cbug == 1))
pluto_setsda(pluto, 1);
pluto->i2cbug = 0;
}
}
static int pluto_getsda(void *data)
{
struct pluto *pluto = data;
return pluto_readreg(pluto, REG_SLCS) & SLCS_SDA;
}
static int pluto_getscl(void *data)
{
struct pluto *pluto = data;
return pluto_readreg(pluto, REG_SLCS) & SLCS_SCL;
}
static void pluto_reset_frontend(struct pluto *pluto, int reenable)
{
u32 val = pluto_readreg(pluto, REG_MISC);
if (val & MISC_FRST) {
val &= ~MISC_FRST;
pluto_writereg(pluto, REG_MISC, val);
}
if (reenable) {
val |= MISC_FRST;
pluto_writereg(pluto, REG_MISC, val);
}
}
static void pluto_reset_ts(struct pluto *pluto, int reenable)
{
u32 val = pluto_readreg(pluto, REG_TSCR);
if (val & TSCR_RSTN) {
val &= ~TSCR_RSTN;
pluto_write_tscr(pluto, val);
}
if (reenable) {
val |= TSCR_RSTN;
pluto_write_tscr(pluto, val);
}
}
static void pluto_set_dma_addr(struct pluto *pluto)
{
pluto_writereg(pluto, REG_PCAR, pluto->dma_addr);
}
static int pluto_dma_map(struct pluto *pluto)
{
pluto->dma_addr = dma_map_single(&pluto->pdev->dev, pluto->dma_buf,
TS_DMA_BYTES, DMA_FROM_DEVICE);
return dma_mapping_error(&pluto->pdev->dev, pluto->dma_addr);
}
static void pluto_dma_unmap(struct pluto *pluto)
{
dma_unmap_single(&pluto->pdev->dev, pluto->dma_addr, TS_DMA_BYTES,
DMA_FROM_DEVICE);
}
static int pluto_start_feed(struct dvb_demux_feed *f)
{
struct pluto *pluto = feed_to_pluto(f);
/* enable PID filtering */
if (pluto->users++ == 0)
pluto_rw(pluto, REG_PIDn(0), PID0_AFIL | PID0_NOFIL, 0);
if ((f->pid < 0x2000) && (f->index < NHWFILTERS))
pluto_rw(pluto, REG_PIDn(f->index), PIDn_ENP | PIDn_PID, PIDn_ENP | f->pid);
else if (pluto->full_ts_users++ == 0)
pluto_rw(pluto, REG_PIDn(0), PID0_NOFIL, PID0_NOFIL);
return 0;
}
static int pluto_stop_feed(struct dvb_demux_feed *f)
{
struct pluto *pluto = feed_to_pluto(f);
/* disable PID filtering */
if (--pluto->users == 0)
pluto_rw(pluto, REG_PIDn(0), PID0_AFIL, PID0_AFIL);
if ((f->pid < 0x2000) && (f->index < NHWFILTERS))
pluto_rw(pluto, REG_PIDn(f->index), PIDn_ENP | PIDn_PID, 0x1fff);
else if (--pluto->full_ts_users == 0)
pluto_rw(pluto, REG_PIDn(0), PID0_NOFIL, 0);
return 0;
}
static void pluto_dma_end(struct pluto *pluto, unsigned int nbpackets)
{
/* synchronize the DMA transfer with the CPU
* first so that we see updated contents. */
dma_sync_single_for_cpu(&pluto->pdev->dev, pluto->dma_addr,
TS_DMA_BYTES, DMA_FROM_DEVICE);
/* Workaround for broken hardware:
* [1] On startup NBPACKETS seems to contain an uninitialized value,
* but no packets have been transferred.
* [2] Sometimes (actually very often) NBPACKETS stays at zero
* although one packet has been transferred.
* [3] Sometimes (actually rarely), the card gets into an erroneous
* mode where it continuously generates interrupts, claiming it
* has received nbpackets>TS_DMA_PACKETS packets, but no packet
* has been transferred. Only a reset seems to solve this
*/
if ((nbpackets == 0) || (nbpackets > TS_DMA_PACKETS)) {
unsigned int i = 0;
while (pluto->dma_buf[i] == 0x47)
i += 188;
nbpackets = i / 188;
if (i == 0) {
pluto_reset_ts(pluto, 1);
dev_printk(KERN_DEBUG, &pluto->pdev->dev, "resetting TS because of invalid packet counter\n");
}
}
dvb_dmx_swfilter_packets(&pluto->demux, pluto->dma_buf, nbpackets);
/* clear the dma buffer. this is needed to be able to identify
* new valid ts packets above */
memset(pluto->dma_buf, 0, nbpackets * 188);
/* reset the dma address */
pluto_set_dma_addr(pluto);
/* sync the buffer and give it back to the card */
dma_sync_single_for_device(&pluto->pdev->dev, pluto->dma_addr,
TS_DMA_BYTES, DMA_FROM_DEVICE);
}
static irqreturn_t pluto_irq(int irq, void *dev_id)
{
struct pluto *pluto = dev_id;
u32 tscr;
/* check whether an interrupt occurred on this device */
tscr = pluto_readreg(pluto, REG_TSCR);
if (!(tscr & (TSCR_DE | TSCR_OVR)))
return IRQ_NONE;
if (tscr == 0xffffffff) {
if (pluto->dead == 0)
dev_err(&pluto->pdev->dev, "card has hung or been ejected.\n");
/* It's dead Jim */
pluto->dead = 1;
return IRQ_HANDLED;
}
/* dma end interrupt */
if (tscr & TSCR_DE) {
pluto_dma_end(pluto, (tscr & TSCR_NBPACKETS) >> 24);
/* overflow interrupt */
if (tscr & TSCR_OVR)
pluto->overflow++;
if (pluto->overflow) {
dev_err(&pluto->pdev->dev, "overflow irq (%d)\n",
pluto->overflow);
pluto_reset_ts(pluto, 1);
pluto->overflow = 0;
}
} else if (tscr & TSCR_OVR) {
pluto->overflow++;
}
/* ACK the interrupt */
pluto_write_tscr(pluto, tscr | TSCR_IACK);
return IRQ_HANDLED;
}
static void pluto_enable_irqs(struct pluto *pluto)
{
u32 val = pluto_readreg(pluto, REG_TSCR);
/* disable AFUL and LOCK interrupts */
val |= (TSCR_MSKA | TSCR_MSKL);
/* enable DMA and OVERFLOW interrupts */
val &= ~(TSCR_DEM | TSCR_MSKO);
/* clear pending interrupts */
val |= TSCR_IACK;
pluto_write_tscr(pluto, val);
}
static void pluto_disable_irqs(struct pluto *pluto)
{
u32 val = pluto_readreg(pluto, REG_TSCR);
/* disable all interrupts */
val |= (TSCR_DEM | TSCR_MSKO | TSCR_MSKA | TSCR_MSKL);
/* clear pending interrupts */
val |= TSCR_IACK;
pluto_write_tscr(pluto, val);
}
static int pluto_hw_init(struct pluto *pluto)
{
pluto_reset_frontend(pluto, 1);
/* set automatic LED control by FPGA */
pluto_rw(pluto, REG_MISC, MISC_ALED, MISC_ALED);
/* set data endianness */
#ifdef __LITTLE_ENDIAN
pluto_rw(pluto, REG_PIDn(0), PID0_END, PID0_END);
#else
pluto_rw(pluto, REG_PIDn(0), PID0_END, 0);
#endif
/* map DMA and set address */
pluto_dma_map(pluto);
pluto_set_dma_addr(pluto);
/* enable interrupts */
pluto_enable_irqs(pluto);
/* reset TS logic */
pluto_reset_ts(pluto, 1);
return 0;
}
static void pluto_hw_exit(struct pluto *pluto)
{
/* disable interrupts */
pluto_disable_irqs(pluto);
pluto_reset_ts(pluto, 0);
/* LED: disable automatic control, enable yellow, disable green */
pluto_rw(pluto, REG_MISC, MISC_ALED | MISC_LED1 | MISC_LED0, MISC_LED1);
/* unmap DMA */
pluto_dma_unmap(pluto);
pluto_reset_frontend(pluto, 0);
}
static inline u32 divide(u32 numerator, u32 denominator)
{
if (denominator == 0)
return ~0;
return DIV_ROUND_CLOSEST(numerator, denominator);
}
/* LG Innotek TDTE-E001P (Infineon TUA6034) */
static int lg_tdtpe001p_tuner_set_params(struct dvb_frontend *fe)
{
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
struct pluto *pluto = frontend_to_pluto(fe);
struct i2c_msg msg;
int ret;
u8 buf[4];
u32 div;
// Fref = 166.667 Hz
// Fref * 3 = 500.000 Hz
// IF = 36166667
// IF / Fref = 217
//div = divide(p->frequency + 36166667, 166667);
div = divide(p->frequency * 3, 500000) + 217;
buf[0] = (div >> 8) & 0x7f;
buf[1] = (div >> 0) & 0xff;
if (p->frequency < 611000000)
buf[2] = 0xb4;
else if (p->frequency < 811000000)
buf[2] = 0xbc;
else
buf[2] = 0xf4;
// VHF: 174-230 MHz
// center: 350 MHz
// UHF: 470-862 MHz
if (p->frequency < 350000000)
buf[3] = 0x02;
else
buf[3] = 0x04;
if (p->bandwidth_hz == 8000000)
buf[3] |= 0x08;
msg.addr = I2C_ADDR_TUA6034 >> 1;
msg.flags = 0;
msg.buf = buf;
msg.len = sizeof(buf);
if (fe->ops.i2c_gate_ctrl)
fe->ops.i2c_gate_ctrl(fe, 1);
ret = i2c_transfer(&pluto->i2c_adap, &msg, 1);
if (ret < 0)
return ret;
else if (ret == 0)
return -EREMOTEIO;
return 0;
}
static int pluto2_request_firmware(struct dvb_frontend *fe,
const struct firmware **fw, char *name)
{
struct pluto *pluto = frontend_to_pluto(fe);
return request_firmware(fw, name, &pluto->pdev->dev);
}
static struct tda1004x_config pluto2_fe_config = {
.demod_address = I2C_ADDR_TDA10046 >> 1,
.invert = 1,
.invert_oclk = 0,
.xtal_freq = TDA10046_XTAL_16M,
.agc_config = TDA10046_AGC_DEFAULT,
.if_freq = TDA10046_FREQ_3617,
.request_firmware = pluto2_request_firmware,
};
static int frontend_init(struct pluto *pluto)
{
int ret;
pluto->fe = tda10046_attach(&pluto2_fe_config, &pluto->i2c_adap);
if (!pluto->fe) {
dev_err(&pluto->pdev->dev, "could not attach frontend\n");
return -ENODEV;
}
pluto->fe->ops.tuner_ops.set_params = lg_tdtpe001p_tuner_set_params;
ret = dvb_register_frontend(&pluto->dvb_adapter, pluto->fe);
if (ret < 0) {
if (pluto->fe->ops.release)
pluto->fe->ops.release(pluto->fe);
return ret;
}
return 0;
}
static void pluto_read_rev(struct pluto *pluto)
{
u32 val = pluto_readreg(pluto, REG_MISC) & MISC_DVR;
dev_info(&pluto->pdev->dev, "board revision %d.%d\n",
(val >> 12) & 0x0f, (val >> 4) & 0xff);
}
static void pluto_read_mac(struct pluto *pluto, u8 *mac)
{
u32 val = pluto_readreg(pluto, REG_MMAC);
mac[0] = (val >> 8) & 0xff;
mac[1] = (val >> 0) & 0xff;
val = pluto_readreg(pluto, REG_IMAC);
mac[2] = (val >> 8) & 0xff;
mac[3] = (val >> 0) & 0xff;
val = pluto_readreg(pluto, REG_LMAC);
mac[4] = (val >> 8) & 0xff;
mac[5] = (val >> 0) & 0xff;
dev_info(&pluto->pdev->dev, "MAC %pM\n", mac);
}
static int pluto_read_serial(struct pluto *pluto)
{
struct pci_dev *pdev = pluto->pdev;
unsigned int i, j;
u8 __iomem *cis;
cis = pci_iomap(pdev, 1, 0);
if (!cis)
return -EIO;
dev_info(&pdev->dev, "S/N ");
for (i = 0xe0; i < 0x100; i += 4) {
u32 val = readl(&cis[i]);
for (j = 0; j < 32; j += 8) {
if ((val & 0xff) == 0xff)
goto out;
printk(KERN_CONT "%c", val & 0xff);
val >>= 8;
}
}
out:
printk(KERN_CONT "\n");
pci_iounmap(pdev, cis);
return 0;
}
static int pluto2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
struct pluto *pluto;
struct dvb_adapter *dvb_adapter;
struct dvb_demux *dvbdemux;
struct dmx_demux *dmx;
int ret = -ENOMEM;
pluto = kzalloc(sizeof(struct pluto), GFP_KERNEL);
if (!pluto)
goto out;
pluto->pdev = pdev;
ret = pci_enable_device(pdev);
if (ret < 0)
goto err_kfree;
/* enable interrupts */
pci_write_config_dword(pdev, 0x6c, 0x8000);
ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
if (ret < 0)
goto err_pci_disable_device;
pci_set_master(pdev);
ret = pci_request_regions(pdev, DRIVER_NAME);
if (ret < 0)
goto err_pci_disable_device;
pluto->io_mem = pci_iomap(pdev, 0, 0x40);
if (!pluto->io_mem) {
ret = -EIO;
goto err_pci_release_regions;
}
pci_set_drvdata(pdev, pluto);
ret = request_irq(pdev->irq, pluto_irq, IRQF_SHARED, DRIVER_NAME, pluto);
if (ret < 0)
goto err_pci_iounmap;
ret = pluto_hw_init(pluto);
if (ret < 0)
goto err_free_irq;
/* i2c */
i2c_set_adapdata(&pluto->i2c_adap, pluto);
strscpy(pluto->i2c_adap.name, DRIVER_NAME, sizeof(pluto->i2c_adap.name));
pluto->i2c_adap.owner = THIS_MODULE;
pluto->i2c_adap.dev.parent = &pdev->dev;
pluto->i2c_adap.algo_data = &pluto->i2c_bit;
pluto->i2c_bit.data = pluto;
pluto->i2c_bit.setsda = pluto_setsda;
pluto->i2c_bit.setscl = pluto_setscl;
pluto->i2c_bit.getsda = pluto_getsda;
pluto->i2c_bit.getscl = pluto_getscl;
pluto->i2c_bit.udelay = 10;
pluto->i2c_bit.timeout = 10;
/* Raise SCL and SDA */
pluto_setsda(pluto, 1);
pluto_setscl(pluto, 1);
ret = i2c_bit_add_bus(&pluto->i2c_adap);
if (ret < 0)
goto err_pluto_hw_exit;
/* dvb */
ret = dvb_register_adapter(&pluto->dvb_adapter, DRIVER_NAME,
THIS_MODULE, &pdev->dev, adapter_nr);
if (ret < 0)
goto err_i2c_del_adapter;
dvb_adapter = &pluto->dvb_adapter;
pluto_read_rev(pluto);
pluto_read_serial(pluto);
pluto_read_mac(pluto, dvb_adapter->proposed_mac);
dvbdemux = &pluto->demux;
dvbdemux->filternum = 256;
dvbdemux->feednum = 256;
dvbdemux->start_feed = pluto_start_feed;
dvbdemux->stop_feed = pluto_stop_feed;
dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
ret = dvb_dmx_init(dvbdemux);
if (ret < 0)
goto err_dvb_unregister_adapter;
dmx = &dvbdemux->dmx;
pluto->hw_frontend.source = DMX_FRONTEND_0;
pluto->mem_frontend.source = DMX_MEMORY_FE;
pluto->dmxdev.filternum = NHWFILTERS;
pluto->dmxdev.demux = dmx;
ret = dvb_dmxdev_init(&pluto->dmxdev, dvb_adapter);
if (ret < 0)
goto err_dvb_dmx_release;
ret = dmx->add_frontend(dmx, &pluto->hw_frontend);
if (ret < 0)
goto err_dvb_dmxdev_release;
ret = dmx->add_frontend(dmx, &pluto->mem_frontend);
if (ret < 0)
goto err_remove_hw_frontend;
ret = dmx->connect_frontend(dmx, &pluto->hw_frontend);
if (ret < 0)
goto err_remove_mem_frontend;
ret = frontend_init(pluto);
if (ret < 0)
goto err_disconnect_frontend;
dvb_net_init(dvb_adapter, &pluto->dvbnet, dmx);
out:
return ret;
err_disconnect_frontend:
dmx->disconnect_frontend(dmx);
err_remove_mem_frontend:
dmx->remove_frontend(dmx, &pluto->mem_frontend);
err_remove_hw_frontend:
dmx->remove_frontend(dmx, &pluto->hw_frontend);
err_dvb_dmxdev_release:
dvb_dmxdev_release(&pluto->dmxdev);
err_dvb_dmx_release:
dvb_dmx_release(dvbdemux);
err_dvb_unregister_adapter:
dvb_unregister_adapter(dvb_adapter);
err_i2c_del_adapter:
i2c_del_adapter(&pluto->i2c_adap);
err_pluto_hw_exit:
pluto_hw_exit(pluto);
err_free_irq:
free_irq(pdev->irq, pluto);
err_pci_iounmap:
pci_iounmap(pdev, pluto->io_mem);
err_pci_release_regions:
pci_release_regions(pdev);
err_pci_disable_device:
pci_disable_device(pdev);
err_kfree:
kfree(pluto);
goto out;
}
static void pluto2_remove(struct pci_dev *pdev)
{
struct pluto *pluto = pci_get_drvdata(pdev);
struct dvb_adapter *dvb_adapter = &pluto->dvb_adapter;
struct dvb_demux *dvbdemux = &pluto->demux;
struct dmx_demux *dmx = &dvbdemux->dmx;
dmx->close(dmx);
dvb_net_release(&pluto->dvbnet);
if (pluto->fe)
dvb_unregister_frontend(pluto->fe);
dmx->disconnect_frontend(dmx);
dmx->remove_frontend(dmx, &pluto->mem_frontend);
dmx->remove_frontend(dmx, &pluto->hw_frontend);
dvb_dmxdev_release(&pluto->dmxdev);
dvb_dmx_release(dvbdemux);
dvb_unregister_adapter(dvb_adapter);
i2c_del_adapter(&pluto->i2c_adap);
pluto_hw_exit(pluto);
free_irq(pdev->irq, pluto);
pci_iounmap(pdev, pluto->io_mem);
pci_release_regions(pdev);
pci_disable_device(pdev);
kfree(pluto);
}
#ifndef PCI_VENDOR_ID_SCM
#define PCI_VENDOR_ID_SCM 0x0432
#endif
#ifndef PCI_DEVICE_ID_PLUTO2
#define PCI_DEVICE_ID_PLUTO2 0x0001
#endif
static const struct pci_device_id pluto2_id_table[] = {
{
.vendor = PCI_VENDOR_ID_SCM,
.device = PCI_DEVICE_ID_PLUTO2,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
}, {
/* empty */
},
};
MODULE_DEVICE_TABLE(pci, pluto2_id_table);
static struct pci_driver pluto2_driver = {
.name = DRIVER_NAME,
.id_table = pluto2_id_table,
.probe = pluto2_probe,
.remove = pluto2_remove,
};
module_pci_driver(pluto2_driver);
MODULE_AUTHOR("Andreas Oberritter <[email protected]>");
MODULE_DESCRIPTION("Pluto2 driver");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/pluto2/pluto2.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include "saa7164.h"
#define ENCODER_MAX_BITRATE 6500000
#define ENCODER_MIN_BITRATE 1000000
#define ENCODER_DEF_BITRATE 5000000
/*
* This is a dummy non-zero value for the sizeimage field of v4l2_pix_format.
* It is not actually used for anything since this driver does not support
* stream I/O, only read(), and because this driver produces an MPEG stream
* and not discrete frames. But the V4L2 spec doesn't allow for this value
* to be 0, so set it to 0x10000 instead.
*
* If we ever change this driver to support stream I/O, then this field
* will be the size of the streaming buffers.
*/
#define SAA7164_SIZEIMAGE (0x10000)
static struct saa7164_tvnorm saa7164_tvnorms[] = {
{
.name = "NTSC-M",
.id = V4L2_STD_NTSC_M,
}, {
.name = "NTSC-JP",
.id = V4L2_STD_NTSC_M_JP,
}
};
/* Take the encoder configuration form the port struct and
* flush it to the hardware.
*/
static void saa7164_encoder_configure(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
port->encoder_params.width = port->width;
port->encoder_params.height = port->height;
port->encoder_params.is_50hz =
(port->encodernorm.id & V4L2_STD_625_50) != 0;
/* Set up the DIF (enable it) for analog mode by default */
saa7164_api_initialize_dif(port);
/* Configure the correct video standard */
saa7164_api_configure_dif(port, port->encodernorm.id);
/* Ensure the audio decoder is correct configured */
saa7164_api_set_audio_std(port);
}
static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
{
struct list_head *c, *n, *p, *q, *l, *v;
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct saa7164_user_buffer *ubuf;
/* Remove any allocated buffers */
mutex_lock(&port->dmaqueue_lock);
dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
list_del(c);
saa7164_buffer_dealloc(buf);
}
dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
list_for_each_safe(p, q, &port->list_buf_used.list) {
ubuf = list_entry(p, struct saa7164_user_buffer, list);
list_del(p);
saa7164_buffer_dealloc_user(ubuf);
}
dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
list_for_each_safe(l, v, &port->list_buf_free.list) {
ubuf = list_entry(l, struct saa7164_user_buffer, list);
list_del(l);
saa7164_buffer_dealloc_user(ubuf);
}
mutex_unlock(&port->dmaqueue_lock);
dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
return 0;
}
/* Dynamic buffer switch at encoder start time */
static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct saa7164_user_buffer *ubuf;
struct tmHWStreamParameters *params = &port->hw_streamingparams;
int result = -ENODEV, i;
int len = 0;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
if (port->encoder_params.stream_type ==
V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
dprintk(DBGLVL_ENC,
"%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
__func__);
params->samplesperline = 128;
params->numberoflines = 256;
params->pitch = 128;
params->numpagetables = 2 +
((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
} else
if (port->encoder_params.stream_type ==
V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
dprintk(DBGLVL_ENC,
"%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
__func__);
params->samplesperline = 188;
params->numberoflines = 312;
params->pitch = 188;
params->numpagetables = 2 +
((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
} else
BUG();
/* Init and establish defaults */
params->bitspersample = 8;
params->linethreshold = 0;
params->pagetablelistvirt = NULL;
params->pagetablelistphys = NULL;
params->numpagetableentries = port->hwcfg.buffercount;
/* Allocate the PCI resources, buffers (hard) */
for (i = 0; i < port->hwcfg.buffercount; i++) {
buf = saa7164_buffer_alloc(port,
params->numberoflines *
params->pitch);
if (!buf) {
printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
__func__, result);
result = -ENOMEM;
goto failed;
} else {
mutex_lock(&port->dmaqueue_lock);
list_add_tail(&buf->list, &port->dmaqueue.list);
mutex_unlock(&port->dmaqueue_lock);
}
}
/* Allocate some kernel buffers for copying
* to userpsace.
*/
len = params->numberoflines * params->pitch;
if (encoder_buffers < 16)
encoder_buffers = 16;
if (encoder_buffers > 512)
encoder_buffers = 512;
for (i = 0; i < encoder_buffers; i++) {
ubuf = saa7164_buffer_alloc_user(dev, len);
if (ubuf) {
mutex_lock(&port->dmaqueue_lock);
list_add_tail(&ubuf->list, &port->list_buf_free.list);
mutex_unlock(&port->dmaqueue_lock);
}
}
result = 0;
failed:
return result;
}
static int saa7164_encoder_initialize(struct saa7164_port *port)
{
saa7164_encoder_configure(port);
return 0;
}
/* -- V4L2 --------------------------------------------------------- */
int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id)
{
struct saa7164_dev *dev = port->dev;
unsigned int i;
dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)id);
for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
if (id & saa7164_tvnorms[i].id)
break;
}
if (i == ARRAY_SIZE(saa7164_tvnorms))
return -EINVAL;
port->encodernorm = saa7164_tvnorms[i];
port->std = id;
/* Update the audio decoder while is not running in
* auto detect mode.
*/
saa7164_api_set_audio_std(port);
dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)id);
return 0;
}
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_s_std(fh->port, id);
}
int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id)
{
*id = port->std;
return 0;
}
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_g_std(fh->port, id);
}
int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i)
{
static const char * const inputs[] = {
"tuner", "composite", "svideo", "aux",
"composite 2", "svideo 2", "aux 2"
};
int n;
if (i->index >= 7)
return -EINVAL;
strscpy(i->name, inputs[i->index], sizeof(i->name));
if (i->index == 0)
i->type = V4L2_INPUT_TYPE_TUNER;
else
i->type = V4L2_INPUT_TYPE_CAMERA;
for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
i->std |= saa7164_tvnorms[n].id;
return 0;
}
int saa7164_g_input(struct saa7164_port *port, unsigned int *i)
{
struct saa7164_dev *dev = port->dev;
if (saa7164_api_get_videomux(port) != SAA_OK)
return -EIO;
*i = (port->mux_input - 1);
dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
return 0;
}
static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_g_input(fh->port, i);
}
int saa7164_s_input(struct saa7164_port *port, unsigned int i)
{
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
if (i >= 7)
return -EINVAL;
port->mux_input = i + 1;
if (saa7164_api_set_videomux(port) != SAA_OK)
return -EIO;
return 0;
}
static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_s_input(fh->port, i);
}
int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
{
struct saa7164_encoder_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_dev *dev = port->dev;
if (0 != t->index)
return -EINVAL;
strscpy(t->name, "tuner", sizeof(t->name));
t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
t->rangelow = SAA7164_TV_MIN_FREQ;
t->rangehigh = SAA7164_TV_MAX_FREQ;
dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
return 0;
}
int saa7164_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *t)
{
if (0 != t->index)
return -EINVAL;
/* Update the A/V core */
return 0;
}
int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f)
{
if (f->tuner)
return -EINVAL;
f->frequency = port->freq;
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_g_frequency(fh->port, f);
}
int saa7164_s_frequency(struct saa7164_port *port,
const struct v4l2_frequency *f)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_port *tsport;
struct dvb_frontend *fe;
/* TODO: Pull this for the std */
struct analog_parameters params = {
.mode = V4L2_TUNER_ANALOG_TV,
.audmode = V4L2_TUNER_MODE_STEREO,
.std = port->encodernorm.id,
.frequency = f->frequency
};
/* Stop the encoder */
dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
f->frequency, f->tuner);
if (f->tuner != 0)
return -EINVAL;
port->freq = clamp(f->frequency,
SAA7164_TV_MIN_FREQ, SAA7164_TV_MAX_FREQ);
/* Update the hardware */
if (port->nr == SAA7164_PORT_ENC1)
tsport = &dev->ports[SAA7164_PORT_TS1];
else if (port->nr == SAA7164_PORT_ENC2)
tsport = &dev->ports[SAA7164_PORT_TS2];
else
return -EINVAL; /* should not happen */
fe = tsport->dvb.frontend;
if (fe && fe->ops.tuner_ops.set_analog_params)
fe->ops.tuner_ops.set_analog_params(fe, ¶ms);
else
printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
saa7164_encoder_initialize(port);
return 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_s_frequency(fh->port, f);
}
static int saa7164_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct saa7164_port *port =
container_of(ctrl->handler, struct saa7164_port, ctrl_handler);
struct saa7164_encoder_params *params = &port->encoder_params;
int ret = 0;
switch (ctrl->id) {
case V4L2_CID_BRIGHTNESS:
port->ctl_brightness = ctrl->val;
saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
break;
case V4L2_CID_CONTRAST:
port->ctl_contrast = ctrl->val;
saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
break;
case V4L2_CID_SATURATION:
port->ctl_saturation = ctrl->val;
saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
break;
case V4L2_CID_HUE:
port->ctl_hue = ctrl->val;
saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
break;
case V4L2_CID_SHARPNESS:
port->ctl_sharpness = ctrl->val;
saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
break;
case V4L2_CID_AUDIO_VOLUME:
port->ctl_volume = ctrl->val;
saa7164_api_set_audio_volume(port, port->ctl_volume);
break;
case V4L2_CID_MPEG_VIDEO_BITRATE:
params->bitrate = ctrl->val;
break;
case V4L2_CID_MPEG_STREAM_TYPE:
params->stream_type = ctrl->val;
break;
case V4L2_CID_MPEG_AUDIO_MUTE:
params->ctl_mute = ctrl->val;
ret = saa7164_api_audio_mute(port, params->ctl_mute);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
ret);
ret = -EIO;
}
break;
case V4L2_CID_MPEG_VIDEO_ASPECT:
params->ctl_aspect = ctrl->val;
ret = saa7164_api_set_aspect_ratio(port);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
ret);
ret = -EIO;
}
break;
case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
params->bitrate_mode = ctrl->val;
break;
case V4L2_CID_MPEG_VIDEO_B_FRAMES:
params->refdist = ctrl->val;
break;
case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
params->bitrate_peak = ctrl->val;
break;
case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
params->gop_size = ctrl->val;
break;
default:
ret = -EINVAL;
}
return ret;
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct saa7164_encoder_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_dev *dev = port->dev;
strscpy(cap->driver, dev->name, sizeof(cap->driver));
strscpy(cap->card, saa7164_boards[dev->board].name,
sizeof(cap->card));
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE |
V4L2_CAP_DEVICE_CAPS;
return 0;
}
static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index != 0)
return -EINVAL;
f->pixelformat = V4L2_PIX_FMT_MPEG;
return 0;
}
static int vidioc_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct saa7164_encoder_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
f->fmt.pix.bytesperline = 0;
f->fmt.pix.sizeimage = SAA7164_SIZEIMAGE;
f->fmt.pix.field = V4L2_FIELD_INTERLACED;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
f->fmt.pix.width = port->width;
f->fmt.pix.height = port->height;
return 0;
}
static int saa7164_encoder_stop_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__);
ret = 0;
}
return ret;
}
static int saa7164_encoder_acquire_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
ret = 0;
}
return ret;
}
static int saa7164_encoder_pause_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
ret = 0;
}
return ret;
}
/* Firmware is very windows centric, meaning you have to transition
* the part through AVStream / KS Windows stages, forwards or backwards.
* States are: stopped, acquired (h/w), paused, started.
* We have to leave here will all of the soft buffers on the free list,
* else the cfg_post() func won't have soft buffers to correctly configure.
*/
static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct saa7164_user_buffer *ubuf;
struct list_head *c, *n;
int ret;
dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
ret = saa7164_encoder_pause_port(port);
ret = saa7164_encoder_acquire_port(port);
ret = saa7164_encoder_stop_port(port);
dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
port->nr);
/* Reset the state of any allocated buffer resources */
mutex_lock(&port->dmaqueue_lock);
/* Reset the hard and soft buffer state */
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
buf->flags = SAA7164_BUFFER_FREE;
buf->pos = 0;
}
list_for_each_safe(c, n, &port->list_buf_used.list) {
ubuf = list_entry(c, struct saa7164_user_buffer, list);
ubuf->pos = 0;
list_move_tail(&ubuf->list, &port->list_buf_free.list);
}
mutex_unlock(&port->dmaqueue_lock);
/* Free any allocated resources */
saa7164_encoder_buffers_dealloc(port);
dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
return ret;
}
static int saa7164_encoder_start_streaming(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int result, ret = 0;
dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
port->done_first_interrupt = 0;
/* allocate all of the PCIe DMA buffer resources on the fly,
* allowing switching between TS and PS payloads without
* requiring a complete driver reload.
*/
saa7164_encoder_buffers_alloc(port);
/* Configure the encoder with any cache values */
saa7164_api_set_encoder(port);
saa7164_api_get_encoder(port);
/* Place the empty buffers on the hardware */
saa7164_buffer_cfg_port(port);
/* Acquire the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
goto out;
} else
dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
/* Pause the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
goto out;
} else
dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
/* Start the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
} else
dprintk(DBGLVL_ENC, "%s() Running\n", __func__);
out:
return ret;
}
static int fops_open(struct file *file)
{
struct saa7164_dev *dev;
struct saa7164_port *port;
struct saa7164_encoder_fh *fh;
port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
if (!port)
return -ENODEV;
dev = port->dev;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
/* allocate + initialize per filehandle data */
fh = kzalloc(sizeof(*fh), GFP_KERNEL);
if (NULL == fh)
return -ENOMEM;
fh->port = port;
v4l2_fh_init(&fh->fh, video_devdata(file));
v4l2_fh_add(&fh->fh);
file->private_data = fh;
return 0;
}
static int fops_release(struct file *file)
{
struct saa7164_encoder_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
/* Shut device down on last close */
if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
if (atomic_dec_return(&port->v4l_reader_count) == 0) {
/* stop mpeg capture then cancel buffers */
saa7164_encoder_stop_streaming(port);
}
}
v4l2_fh_del(&fh->fh);
v4l2_fh_exit(&fh->fh);
kfree(fh);
return 0;
}
static struct
saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
{
struct saa7164_user_buffer *ubuf = NULL;
struct saa7164_dev *dev = port->dev;
u32 crc;
mutex_lock(&port->dmaqueue_lock);
if (!list_empty(&port->list_buf_used.list)) {
ubuf = list_first_entry(&port->list_buf_used.list,
struct saa7164_user_buffer, list);
if (crc_checking) {
crc = crc32(0, ubuf->data, ubuf->actual_size);
if (crc != ubuf->crc) {
printk(KERN_ERR
"%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
__func__,
ubuf, ubuf->crc, crc);
}
}
}
mutex_unlock(&port->dmaqueue_lock);
dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
return ubuf;
}
static ssize_t fops_read(struct file *file, char __user *buffer,
size_t count, loff_t *pos)
{
struct saa7164_encoder_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_user_buffer *ubuf = NULL;
struct saa7164_dev *dev = port->dev;
int ret = 0;
int rem, cnt;
u8 *p;
port->last_read_msecs_diff = port->last_read_msecs;
port->last_read_msecs = jiffies_to_msecs(jiffies);
port->last_read_msecs_diff = port->last_read_msecs -
port->last_read_msecs_diff;
saa7164_histogram_update(&port->read_interval,
port->last_read_msecs_diff);
if (*pos) {
printk(KERN_ERR "%s() ESPIPE\n", __func__);
return -ESPIPE;
}
if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
if (atomic_inc_return(&port->v4l_reader_count) == 1) {
if (saa7164_encoder_initialize(port) < 0) {
printk(KERN_ERR "%s() EINVAL\n", __func__);
return -EINVAL;
}
saa7164_encoder_start_streaming(port);
msleep(200);
}
}
/* blocking wait for buffer */
if ((file->f_flags & O_NONBLOCK) == 0) {
if (wait_event_interruptible(port->wait_read,
saa7164_enc_next_buf(port))) {
printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
return -ERESTARTSYS;
}
}
/* Pull the first buffer from the used list */
ubuf = saa7164_enc_next_buf(port);
while ((count > 0) && ubuf) {
/* set remaining bytes to copy */
rem = ubuf->actual_size - ubuf->pos;
cnt = rem > count ? count : rem;
p = ubuf->data + ubuf->pos;
dprintk(DBGLVL_ENC,
"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
__func__, (int)count, cnt, rem, ubuf, ubuf->pos);
if (copy_to_user(buffer, p, cnt)) {
printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
if (!ret) {
printk(KERN_ERR "%s() EFAULT\n", __func__);
ret = -EFAULT;
}
goto err;
}
ubuf->pos += cnt;
count -= cnt;
buffer += cnt;
ret += cnt;
if (ubuf->pos > ubuf->actual_size)
printk(KERN_ERR "read() pos > actual, huh?\n");
if (ubuf->pos == ubuf->actual_size) {
/* finished with current buffer, take next buffer */
/* Requeue the buffer on the free list */
ubuf->pos = 0;
mutex_lock(&port->dmaqueue_lock);
list_move_tail(&ubuf->list, &port->list_buf_free.list);
mutex_unlock(&port->dmaqueue_lock);
/* Dequeue next */
if ((file->f_flags & O_NONBLOCK) == 0) {
if (wait_event_interruptible(port->wait_read,
saa7164_enc_next_buf(port))) {
break;
}
}
ubuf = saa7164_enc_next_buf(port);
}
}
err:
if (!ret && !ubuf)
ret = -EAGAIN;
return ret;
}
static __poll_t fops_poll(struct file *file, poll_table *wait)
{
__poll_t req_events = poll_requested_events(wait);
struct saa7164_encoder_fh *fh =
(struct saa7164_encoder_fh *)file->private_data;
struct saa7164_port *port = fh->port;
__poll_t mask = v4l2_ctrl_poll(file, wait);
port->last_poll_msecs_diff = port->last_poll_msecs;
port->last_poll_msecs = jiffies_to_msecs(jiffies);
port->last_poll_msecs_diff = port->last_poll_msecs -
port->last_poll_msecs_diff;
saa7164_histogram_update(&port->poll_interval,
port->last_poll_msecs_diff);
if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
return mask;
if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
if (atomic_inc_return(&port->v4l_reader_count) == 1) {
if (saa7164_encoder_initialize(port) < 0)
return mask | EPOLLERR;
saa7164_encoder_start_streaming(port);
msleep(200);
}
}
/* Pull the first buffer from the used list */
if (!list_empty(&port->list_buf_used.list))
mask |= EPOLLIN | EPOLLRDNORM;
return mask;
}
static const struct v4l2_ctrl_ops saa7164_ctrl_ops = {
.s_ctrl = saa7164_s_ctrl,
};
static const struct v4l2_file_operations mpeg_fops = {
.owner = THIS_MODULE,
.open = fops_open,
.release = fops_release,
.read = fops_read,
.poll = fops_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
.vidioc_s_std = vidioc_s_std,
.vidioc_g_std = vidioc_g_std,
.vidioc_enum_input = saa7164_enum_input,
.vidioc_g_input = vidioc_g_input,
.vidioc_s_input = vidioc_s_input,
.vidioc_g_tuner = saa7164_g_tuner,
.vidioc_s_tuner = saa7164_s_tuner,
.vidioc_g_frequency = vidioc_g_frequency,
.vidioc_s_frequency = vidioc_s_frequency,
.vidioc_querycap = vidioc_querycap,
.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = vidioc_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = vidioc_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = vidioc_fmt_vid_cap,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static struct video_device saa7164_mpeg_template = {
.name = "saa7164",
.fops = &mpeg_fops,
.ioctl_ops = &mpeg_ioctl_ops,
.minor = -1,
.tvnorms = SAA7164_NORMS,
.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_TUNER,
};
static struct video_device *saa7164_encoder_alloc(
struct saa7164_port *port,
struct pci_dev *pci,
struct video_device *template,
char *type)
{
struct video_device *vfd;
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
vfd = video_device_alloc();
if (NULL == vfd)
return NULL;
*vfd = *template;
snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
type, saa7164_boards[dev->board].name);
vfd->v4l2_dev = &dev->v4l2_dev;
vfd->release = video_device_release;
return vfd;
}
int saa7164_encoder_register(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct v4l2_ctrl_handler *hdl = &port->ctrl_handler;
int result = -ENODEV;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
BUG_ON(port->type != SAA7164_MPEG_ENCODER);
/* Sanity check that the PCI configuration space is active */
if (port->hwcfg.BARLocation == 0) {
printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
__func__, result);
result = -ENOMEM;
goto fail_pci;
}
/* Establish encoder defaults here */
/* Set default TV standard */
port->encodernorm = saa7164_tvnorms[0];
port->width = 720;
port->mux_input = 1; /* Composite */
port->video_format = EU_VIDEO_FORMAT_MPEG_2;
port->audio_format = 0;
port->video_resolution = 0;
port->freq = SAA7164_TV_MIN_FREQ;
v4l2_ctrl_handler_init(hdl, 14);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_CONTRAST, 0, 255, 1, 66);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_SATURATION, 0, 255, 1, 62);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_HUE, 0, 255, 1, 128);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_SHARPNESS, 0x0, 0x0f, 1, 8);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_AUDIO_MUTE, 0x0, 0x01, 1, 0);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_AUDIO_VOLUME, -83, 24, 1, 20);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_VIDEO_BITRATE,
ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
100000, ENCODER_DEF_BITRATE);
v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_STREAM_TYPE,
V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 0,
V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_VIDEO_ASPECT,
V4L2_MPEG_VIDEO_ASPECT_221x100, 0,
V4L2_MPEG_VIDEO_ASPECT_4x3);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, 15);
v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_VIDEO_B_FRAMES, 1, 3, 1, 1);
v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
100000, ENCODER_DEF_BITRATE);
if (hdl->error) {
result = hdl->error;
goto fail_hdl;
}
port->std = V4L2_STD_NTSC_M;
if (port->encodernorm.id & V4L2_STD_525_60)
port->height = 480;
else
port->height = 576;
/* Allocate and register the video device node */
port->v4l_device = saa7164_encoder_alloc(port,
dev->pci, &saa7164_mpeg_template, "mpeg");
if (!port->v4l_device) {
printk(KERN_INFO "%s: can't allocate mpeg device\n",
dev->name);
result = -ENOMEM;
goto fail_hdl;
}
port->v4l_device->ctrl_handler = hdl;
v4l2_ctrl_handler_setup(hdl);
video_set_drvdata(port->v4l_device, port);
result = video_register_device(port->v4l_device,
VFL_TYPE_VIDEO, -1);
if (result < 0) {
printk(KERN_INFO "%s: can't register mpeg device\n",
dev->name);
goto fail_reg;
}
printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
dev->name, port->v4l_device->num);
/* Configure the hardware defaults */
saa7164_api_set_videomux(port);
saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
saa7164_api_audio_mute(port, 0);
saa7164_api_set_audio_volume(port, 20);
saa7164_api_set_aspect_ratio(port);
/* Disable audio standard detection, it's buggy */
saa7164_api_set_audio_detection(port, 0);
saa7164_api_set_encoder(port);
saa7164_api_get_encoder(port);
return 0;
fail_reg:
video_device_release(port->v4l_device);
port->v4l_device = NULL;
fail_hdl:
v4l2_ctrl_handler_free(hdl);
fail_pci:
return result;
}
void saa7164_encoder_unregister(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
BUG_ON(port->type != SAA7164_MPEG_ENCODER);
if (port->v4l_device) {
if (port->v4l_device->minor != -1)
video_unregister_device(port->v4l_device);
else
video_device_release(port->v4l_device);
port->v4l_device = NULL;
}
v4l2_ctrl_handler_free(&port->ctrl_handler);
dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
}
| linux-master | drivers/media/pci/saa7164/saa7164-encoder.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/firmware.h>
#include <linux/slab.h>
#include "saa7164.h"
#define SAA7164_REV2_FIRMWARE "NXP7164-2010-03-10.1.fw"
#define SAA7164_REV2_FIRMWARE_SIZE 4019072
#define SAA7164_REV3_FIRMWARE "NXP7164-2010-03-10.1.fw"
#define SAA7164_REV3_FIRMWARE_SIZE 4019072
struct fw_header {
u32 firmwaresize;
u32 bslsize;
u32 reserved;
u32 version;
};
static int saa7164_dl_wait_ack(struct saa7164_dev *dev, u32 reg)
{
u32 timeout = SAA_DEVICE_TIMEOUT;
while ((saa7164_readl(reg) & 0x01) == 0) {
timeout -= 10;
if (timeout == 0) {
printk(KERN_ERR "%s() timeout (no d/l ack)\n",
__func__);
return -EBUSY;
}
msleep(100);
}
return 0;
}
static int saa7164_dl_wait_clr(struct saa7164_dev *dev, u32 reg)
{
u32 timeout = SAA_DEVICE_TIMEOUT;
while (saa7164_readl(reg) & 0x01) {
timeout -= 10;
if (timeout == 0) {
printk(KERN_ERR "%s() timeout (no d/l clr)\n",
__func__);
return -EBUSY;
}
msleep(100);
}
return 0;
}
/* TODO: move dlflags into dev-> and change to write/readl/b */
/* TODO: Excessive levels of debug */
static int saa7164_downloadimage(struct saa7164_dev *dev, u8 *src, u32 srcsize,
u32 dlflags, u8 __iomem *dst, u32 dstsize)
{
u32 reg, timeout, offset;
u8 *srcbuf = NULL;
int ret;
u32 dlflag = dlflags;
u32 dlflag_ack = dlflag + 4;
u32 drflag = dlflag_ack + 4;
u32 drflag_ack = drflag + 4;
u32 bleflag = drflag_ack + 4;
dprintk(DBGLVL_FW,
"%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
__func__, src, srcsize, dlflags, dst, dstsize);
if ((src == NULL) || (dst == NULL)) {
ret = -EIO;
goto out;
}
srcbuf = kzalloc(4 * 1048576, GFP_KERNEL);
if (NULL == srcbuf) {
ret = -ENOMEM;
goto out;
}
if (srcsize > (4*1048576)) {
ret = -ENOMEM;
goto out;
}
memcpy(srcbuf, src, srcsize);
dprintk(DBGLVL_FW, "%s() dlflag = 0x%x\n", __func__, dlflag);
dprintk(DBGLVL_FW, "%s() dlflag_ack = 0x%x\n", __func__, dlflag_ack);
dprintk(DBGLVL_FW, "%s() drflag = 0x%x\n", __func__, drflag);
dprintk(DBGLVL_FW, "%s() drflag_ack = 0x%x\n", __func__, drflag_ack);
dprintk(DBGLVL_FW, "%s() bleflag = 0x%x\n", __func__, bleflag);
reg = saa7164_readl(dlflag);
dprintk(DBGLVL_FW, "%s() dlflag (0x%x)= 0x%x\n", __func__, dlflag, reg);
if (reg == 1)
dprintk(DBGLVL_FW,
"%s() Download flag already set, please reboot\n",
__func__);
/* Indicate download start */
saa7164_writel(dlflag, 1);
ret = saa7164_dl_wait_ack(dev, dlflag_ack);
if (ret < 0)
goto out;
/* Ack download start, then wait for wait */
saa7164_writel(dlflag, 0);
ret = saa7164_dl_wait_clr(dev, dlflag_ack);
if (ret < 0)
goto out;
/* Deal with the raw firmware, in the appropriate chunk size */
for (offset = 0; srcsize > dstsize;
srcsize -= dstsize, offset += dstsize) {
dprintk(DBGLVL_FW, "%s() memcpy %d\n", __func__, dstsize);
memcpy_toio(dst, srcbuf + offset, dstsize);
/* Flag the data as ready */
saa7164_writel(drflag, 1);
ret = saa7164_dl_wait_ack(dev, drflag_ack);
if (ret < 0)
goto out;
/* Wait for indication data was received */
saa7164_writel(drflag, 0);
ret = saa7164_dl_wait_clr(dev, drflag_ack);
if (ret < 0)
goto out;
}
dprintk(DBGLVL_FW, "%s() memcpy(l) %d\n", __func__, dstsize);
/* Write last block to the device */
memcpy_toio(dst, srcbuf+offset, srcsize);
/* Flag the data as ready */
saa7164_writel(drflag, 1);
ret = saa7164_dl_wait_ack(dev, drflag_ack);
if (ret < 0)
goto out;
saa7164_writel(drflag, 0);
timeout = 0;
while (saa7164_readl(bleflag) != SAA_DEVICE_IMAGE_BOOTING) {
if (saa7164_readl(bleflag) & SAA_DEVICE_IMAGE_CORRUPT) {
printk(KERN_ERR "%s() image corrupt\n", __func__);
ret = -EBUSY;
goto out;
}
if (saa7164_readl(bleflag) & SAA_DEVICE_MEMORY_CORRUPT) {
printk(KERN_ERR "%s() device memory corrupt\n",
__func__);
ret = -EBUSY;
goto out;
}
msleep(10); /* Checkpatch throws a < 20ms warning */
if (timeout++ > 60)
break;
}
printk(KERN_INFO "%s() Image downloaded, booting...\n", __func__);
ret = saa7164_dl_wait_clr(dev, drflag_ack);
if (ret < 0)
goto out;
printk(KERN_INFO "%s() Image booted successfully.\n", __func__);
ret = 0;
out:
kfree(srcbuf);
return ret;
}
/* TODO: Excessive debug */
/* Load the firmware. Optionally it can be in ROM or newer versions
* can be on disk, saving the expense of the ROM hardware. */
int saa7164_downloadfirmware(struct saa7164_dev *dev)
{
/* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
u32 tmp, filesize, version, err_flags, first_timeout, fwlength;
u32 second_timeout, updatebootloader = 1, bootloadersize = 0;
const struct firmware *fw = NULL;
struct fw_header *hdr, *boothdr = NULL, *fwhdr;
u32 bootloaderversion = 0, fwloadersize;
u8 *bootloaderoffset = NULL, *fwloaderoffset;
char *fwname;
int ret;
dprintk(DBGLVL_FW, "%s()\n", __func__);
if (saa7164_boards[dev->board].chiprev == SAA7164_CHIP_REV2) {
fwname = SAA7164_REV2_FIRMWARE;
fwlength = SAA7164_REV2_FIRMWARE_SIZE;
} else {
fwname = SAA7164_REV3_FIRMWARE;
fwlength = SAA7164_REV3_FIRMWARE_SIZE;
}
version = saa7164_getcurrentfirmwareversion(dev);
if (version == 0x00) {
second_timeout = 100;
first_timeout = 100;
err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
__func__, err_flags);
while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
__func__, err_flags);
msleep(10); /* Checkpatch throws a < 20ms warning */
if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
printk(KERN_ERR "%s() firmware corrupt\n",
__func__);
break;
}
if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
printk(KERN_ERR "%s() device memory corrupt\n",
__func__);
break;
}
if (err_flags & SAA_DEVICE_NO_IMAGE) {
printk(KERN_ERR "%s() no first image\n",
__func__);
break;
}
if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
first_timeout -= 10;
if (first_timeout == 0) {
printk(KERN_ERR
"%s() no first image\n",
__func__);
break;
}
} else if (err_flags & SAA_DEVICE_IMAGE_LOADING) {
second_timeout -= 10;
if (second_timeout == 0) {
printk(KERN_ERR
"%s() FW load time exceeded\n",
__func__);
break;
}
} else {
second_timeout -= 10;
if (second_timeout == 0) {
printk(KERN_ERR
"%s() Unknown bootloader flags 0x%x\n",
__func__, err_flags);
break;
}
}
err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
} /* While != Booting */
if (err_flags == SAA_DEVICE_IMAGE_BOOTING) {
dprintk(DBGLVL_FW, "%s() Loader 1 has loaded.\n",
__func__);
first_timeout = SAA_DEVICE_TIMEOUT;
second_timeout = 100;
err_flags = saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
__func__, err_flags);
while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
__func__, err_flags);
msleep(10); /* Checkpatch throws a < 20ms warning */
if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
printk(KERN_ERR
"%s() firmware corrupt\n",
__func__);
break;
}
if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
printk(KERN_ERR
"%s() device memory corrupt\n",
__func__);
break;
}
if (err_flags & SAA_DEVICE_NO_IMAGE) {
printk(KERN_ERR "%s() no second image\n",
__func__);
break;
}
if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
first_timeout -= 10;
if (first_timeout == 0) {
printk(KERN_ERR
"%s() no second image\n",
__func__);
break;
}
} else if (err_flags &
SAA_DEVICE_IMAGE_LOADING) {
second_timeout -= 10;
if (second_timeout == 0) {
printk(KERN_ERR
"%s() FW load time exceeded\n",
__func__);
break;
}
} else {
second_timeout -= 10;
if (second_timeout == 0) {
printk(KERN_ERR
"%s() Unknown bootloader flags 0x%x\n",
__func__, err_flags);
break;
}
}
err_flags =
saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
} /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
dprintk(DBGLVL_FW, "%s() Loader flags 1:0x%x 2:0x%x.\n",
__func__,
saa7164_readl(SAA_BOOTLOADERERROR_FLAGS),
saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS));
} /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
/* It's possible for both firmwares to have booted,
* but that doesn't mean they've finished booting yet.
*/
if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
SAA_DEVICE_IMAGE_BOOTING) &&
(saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
SAA_DEVICE_IMAGE_BOOTING)) {
dprintk(DBGLVL_FW, "%s() Loader 2 has loaded.\n",
__func__);
first_timeout = SAA_DEVICE_TIMEOUT;
while (first_timeout) {
msleep(10); /* Checkpatch throws a < 20ms warning */
version =
saa7164_getcurrentfirmwareversion(dev);
if (version) {
dprintk(DBGLVL_FW,
"%s() All f/w loaded successfully\n",
__func__);
break;
} else {
first_timeout -= 10;
if (first_timeout == 0) {
printk(KERN_ERR
"%s() FW did not boot\n",
__func__);
break;
}
}
}
}
version = saa7164_getcurrentfirmwareversion(dev);
} /* version == 0 */
/* Has the firmware really booted? */
if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
SAA_DEVICE_IMAGE_BOOTING) &&
(saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
SAA_DEVICE_IMAGE_BOOTING) && (version == 0)) {
printk(KERN_ERR
"%s() The firmware hung, probably bad firmware\n",
__func__);
/* Tell the second stage loader we have a deadlock */
saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET,
SAA_DEVICE_DEADLOCK_DETECTED);
saa7164_getfirmwarestatus(dev);
return -ENOMEM;
}
dprintk(DBGLVL_FW, "Device has Firmware Version %d.%d.%d.%d\n",
(version & 0x0000fc00) >> 10,
(version & 0x000003e0) >> 5,
(version & 0x0000001f),
(version & 0xffff0000) >> 16);
/* Load the firmware from the disk if required */
if (version == 0) {
printk(KERN_INFO "%s() Waiting for firmware upload (%s)\n",
__func__, fwname);
ret = request_firmware(&fw, fwname, &dev->pci->dev);
if (ret) {
printk(KERN_ERR "%s() Upload failed. (file not found?)\n",
__func__);
return -ENOMEM;
}
printk(KERN_INFO "%s() firmware read %zu bytes.\n",
__func__, fw->size);
if (fw->size != fwlength) {
printk(KERN_ERR "saa7164: firmware incorrect size %zu != %u\n",
fw->size, fwlength);
ret = -ENOMEM;
goto out;
}
printk(KERN_INFO "%s() firmware loaded.\n", __func__);
hdr = (struct fw_header *)fw->data;
printk(KERN_INFO "Firmware file header part 1:\n");
printk(KERN_INFO " .FirmwareSize = 0x%x\n", hdr->firmwaresize);
printk(KERN_INFO " .BSLSize = 0x%x\n", hdr->bslsize);
printk(KERN_INFO " .Reserved = 0x%x\n", hdr->reserved);
printk(KERN_INFO " .Version = 0x%x\n", hdr->version);
/* Retrieve bootloader if reqd */
if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0))
/* Second bootloader in the firmware file */
filesize = hdr->reserved * 16;
else
filesize = (hdr->firmwaresize + hdr->bslsize) *
16 + sizeof(struct fw_header);
printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n",
__func__, filesize);
/* Get bootloader (if reqd) and firmware header */
if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
/* Second boot loader is required */
/* Get the loader header */
boothdr = (struct fw_header *)(fw->data +
sizeof(struct fw_header));
bootloaderversion =
saa7164_readl(SAA_DEVICE_2ND_VERSION);
dprintk(DBGLVL_FW, "Onboard BootLoader:\n");
dprintk(DBGLVL_FW, "->Flag 0x%x\n",
saa7164_readl(SAA_BOOTLOADERERROR_FLAGS));
dprintk(DBGLVL_FW, "->Ack 0x%x\n",
saa7164_readl(SAA_DATAREADY_FLAG_ACK));
dprintk(DBGLVL_FW, "->FW Version 0x%x\n", version);
dprintk(DBGLVL_FW, "->Loader Version 0x%x\n",
bootloaderversion);
if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK)
== 0x00) && (version == 0x00)) {
dprintk(DBGLVL_FW, "BootLoader version in rom %d.%d.%d.%d\n",
(bootloaderversion & 0x0000fc00) >> 10,
(bootloaderversion & 0x000003e0) >> 5,
(bootloaderversion & 0x0000001f),
(bootloaderversion & 0xffff0000) >> 16
);
dprintk(DBGLVL_FW, "BootLoader version in file %d.%d.%d.%d\n",
(boothdr->version & 0x0000fc00) >> 10,
(boothdr->version & 0x000003e0) >> 5,
(boothdr->version & 0x0000001f),
(boothdr->version & 0xffff0000) >> 16
);
if (bootloaderversion == boothdr->version)
updatebootloader = 0;
}
/* Calculate offset to firmware header */
tmp = (boothdr->firmwaresize + boothdr->bslsize) * 16 +
(sizeof(struct fw_header) +
sizeof(struct fw_header));
fwhdr = (struct fw_header *)(fw->data+tmp);
} else {
/* No second boot loader */
fwhdr = hdr;
}
dprintk(DBGLVL_FW, "Firmware version in file %d.%d.%d.%d\n",
(fwhdr->version & 0x0000fc00) >> 10,
(fwhdr->version & 0x000003e0) >> 5,
(fwhdr->version & 0x0000001f),
(fwhdr->version & 0xffff0000) >> 16
);
if (version == fwhdr->version) {
/* No download, firmware already on board */
ret = 0;
goto out;
}
if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
if (updatebootloader) {
/* Get ready to upload the bootloader */
bootloadersize = (boothdr->firmwaresize +
boothdr->bslsize) * 16 +
sizeof(struct fw_header);
bootloaderoffset = (u8 *)(fw->data +
sizeof(struct fw_header));
dprintk(DBGLVL_FW, "bootloader d/l starts.\n");
printk(KERN_INFO "%s() FirmwareSize = 0x%x\n",
__func__, boothdr->firmwaresize);
printk(KERN_INFO "%s() BSLSize = 0x%x\n",
__func__, boothdr->bslsize);
printk(KERN_INFO "%s() Reserved = 0x%x\n",
__func__, boothdr->reserved);
printk(KERN_INFO "%s() Version = 0x%x\n",
__func__, boothdr->version);
ret = saa7164_downloadimage(
dev,
bootloaderoffset,
bootloadersize,
SAA_DOWNLOAD_FLAGS,
dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
SAA_DEVICE_BUFFERBLOCKSIZE);
if (ret < 0) {
printk(KERN_ERR
"bootloader d/l has failed\n");
goto out;
}
dprintk(DBGLVL_FW,
"bootloader download complete.\n");
}
printk(KERN_ERR "starting firmware download(2)\n");
bootloadersize = (boothdr->firmwaresize +
boothdr->bslsize) * 16 +
sizeof(struct fw_header);
bootloaderoffset =
(u8 *)(fw->data + sizeof(struct fw_header));
fwloaderoffset = bootloaderoffset + bootloadersize;
/* TODO: fix this bounds overrun here with old f/ws */
fwloadersize = (fwhdr->firmwaresize + fwhdr->bslsize) *
16 + sizeof(struct fw_header);
ret = saa7164_downloadimage(
dev,
fwloaderoffset,
fwloadersize,
SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET,
dev->bmmio + SAA_DEVICE_2ND_DOWNLOAD_OFFSET,
SAA_DEVICE_2ND_BUFFERBLOCKSIZE);
if (ret < 0) {
printk(KERN_ERR "firmware download failed\n");
goto out;
}
printk(KERN_ERR "firmware download complete.\n");
} else {
/* No bootloader update reqd, download firmware only */
printk(KERN_ERR "starting firmware download(3)\n");
ret = saa7164_downloadimage(
dev,
(u8 *)fw->data,
fw->size,
SAA_DOWNLOAD_FLAGS,
dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
SAA_DEVICE_BUFFERBLOCKSIZE);
if (ret < 0) {
printk(KERN_ERR "firmware download failed\n");
goto out;
}
printk(KERN_ERR "firmware download complete.\n");
}
}
dev->firmwareloaded = 1;
ret = 0;
out:
release_firmware(fw);
return ret;
}
| linux-master | drivers/media/pci/saa7164/saa7164-fw.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include "saa7164.h"
/* The message bus to/from the firmware is a ring buffer in PCI address
* space. Establish the defaults.
*/
int saa7164_bus_setup(struct saa7164_dev *dev)
{
struct tmComResBusInfo *b = &dev->bus;
mutex_init(&b->lock);
b->Type = TYPE_BUS_PCIe;
b->m_wMaxReqSize = SAA_DEVICE_MAXREQUESTSIZE;
b->m_pdwSetRing = (u8 __iomem *)(dev->bmmio +
((u32)dev->busdesc.CommandRing));
b->m_dwSizeSetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
b->m_pdwGetRing = (u8 __iomem *)(dev->bmmio +
((u32)dev->busdesc.ResponseRing));
b->m_dwSizeGetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
b->m_dwSetWritePos = ((u32)dev->intfdesc.BARLocation) +
(2 * sizeof(u64));
b->m_dwSetReadPos = b->m_dwSetWritePos + (1 * sizeof(u32));
b->m_dwGetWritePos = b->m_dwSetWritePos + (2 * sizeof(u32));
b->m_dwGetReadPos = b->m_dwSetWritePos + (3 * sizeof(u32));
return 0;
}
void saa7164_bus_dump(struct saa7164_dev *dev)
{
struct tmComResBusInfo *b = &dev->bus;
dprintk(DBGLVL_BUS, "Dumping the bus structure:\n");
dprintk(DBGLVL_BUS, " .type = %d\n", b->Type);
dprintk(DBGLVL_BUS, " .dev->bmmio = 0x%p\n", dev->bmmio);
dprintk(DBGLVL_BUS, " .m_wMaxReqSize = 0x%x\n", b->m_wMaxReqSize);
dprintk(DBGLVL_BUS, " .m_pdwSetRing = 0x%p\n", b->m_pdwSetRing);
dprintk(DBGLVL_BUS, " .m_dwSizeSetRing = 0x%x\n", b->m_dwSizeSetRing);
dprintk(DBGLVL_BUS, " .m_pdwGetRing = 0x%p\n", b->m_pdwGetRing);
dprintk(DBGLVL_BUS, " .m_dwSizeGetRing = 0x%x\n", b->m_dwSizeGetRing);
dprintk(DBGLVL_BUS, " .m_dwSetReadPos = 0x%x (0x%08x)\n",
b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
dprintk(DBGLVL_BUS, " .m_dwSetWritePos = 0x%x (0x%08x)\n",
b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
dprintk(DBGLVL_BUS, " .m_dwGetReadPos = 0x%x (0x%08x)\n",
b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
dprintk(DBGLVL_BUS, " .m_dwGetWritePos = 0x%x (0x%08x)\n",
b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
}
/* Intensionally throw a BUG() if the state of the message bus looks corrupt */
static void saa7164_bus_verify(struct saa7164_dev *dev)
{
struct tmComResBusInfo *b = &dev->bus;
int bug = 0;
if (saa7164_readl(b->m_dwSetReadPos) > b->m_dwSizeSetRing)
bug++;
if (saa7164_readl(b->m_dwSetWritePos) > b->m_dwSizeSetRing)
bug++;
if (saa7164_readl(b->m_dwGetReadPos) > b->m_dwSizeGetRing)
bug++;
if (saa7164_readl(b->m_dwGetWritePos) > b->m_dwSizeGetRing)
bug++;
if (bug) {
saa_debug = 0xffff; /* Ensure we get the bus dump */
saa7164_bus_dump(dev);
saa_debug = 1024; /* Ensure we get the bus dump */
BUG();
}
}
static void saa7164_bus_dumpmsg(struct saa7164_dev *dev, struct tmComResInfo *m,
void *buf)
{
dprintk(DBGLVL_BUS, "Dumping msg structure:\n");
dprintk(DBGLVL_BUS, " .id = %d\n", m->id);
dprintk(DBGLVL_BUS, " .flags = 0x%x\n", m->flags);
dprintk(DBGLVL_BUS, " .size = 0x%x\n", m->size);
dprintk(DBGLVL_BUS, " .command = 0x%x\n", m->command);
dprintk(DBGLVL_BUS, " .controlselector = 0x%x\n", m->controlselector);
dprintk(DBGLVL_BUS, " .seqno = %d\n", m->seqno);
if (buf)
dprintk(DBGLVL_BUS, " .buffer (ignored)\n");
}
/*
* Places a command or a response on the bus. The implementation does not
* know if it is a command or a response it just places the data on the
* bus depending on the bus information given in the struct tmComResBusInfo
* structure. If the command or response does not fit into the bus ring
* buffer it will be refused.
*
* Return Value:
* SAA_OK The function executed successfully.
* < 0 One or more members are not initialized.
*/
int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
void *buf)
{
struct tmComResBusInfo *bus = &dev->bus;
u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
u32 new_swp, space_rem;
int ret = SAA_ERR_BAD_PARAMETER;
u16 size;
if (!msg) {
printk(KERN_ERR "%s() !msg\n", __func__);
return SAA_ERR_BAD_PARAMETER;
}
dprintk(DBGLVL_BUS, "%s()\n", __func__);
saa7164_bus_verify(dev);
if (msg->size > dev->bus.m_wMaxReqSize) {
printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
__func__);
return SAA_ERR_BAD_PARAMETER;
}
if ((msg->size > 0) && (buf == NULL)) {
printk(KERN_ERR "%s() Missing message buffer\n", __func__);
return SAA_ERR_BAD_PARAMETER;
}
/* Lock the bus from any other access */
mutex_lock(&bus->lock);
bytes_to_write = sizeof(*msg) + msg->size;
free_write_space = 0;
timeout = SAA_BUS_TIMEOUT;
curr_srp = saa7164_readl(bus->m_dwSetReadPos);
curr_swp = saa7164_readl(bus->m_dwSetWritePos);
/* Deal with ring wrapping issues */
if (curr_srp > curr_swp)
/* Deal with the wrapped ring */
free_write_space = curr_srp - curr_swp;
else
/* The ring has not wrapped yet */
free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
bytes_to_write);
dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
free_write_space);
dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
/* Process the msg and write the content onto the bus */
while (bytes_to_write >= free_write_space) {
if (timeout-- == 0) {
printk(KERN_ERR "%s() bus timeout\n", __func__);
ret = SAA_ERR_NO_RESOURCES;
goto out;
}
/* TODO: Review this delay, efficient? */
/* Wait, allowing the hardware fetch time */
mdelay(1);
/* Check the space usage again */
curr_srp = saa7164_readl(bus->m_dwSetReadPos);
/* Deal with ring wrapping issues */
if (curr_srp > curr_swp)
/* Deal with the wrapped ring */
free_write_space = curr_srp - curr_swp;
else
/* Read didn't wrap around the buffer */
free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
curr_swp;
}
/* Calculate the new write position */
new_swp = curr_swp + bytes_to_write;
dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
bus->m_dwSizeSetRing);
/*
* Make a copy of msg->size before it is converted to le16 since it is
* used in the code below.
*/
size = msg->size;
/* Convert to le16/le32 */
msg->size = (__force u16)cpu_to_le16(msg->size);
msg->command = (__force u32)cpu_to_le32(msg->command);
msg->controlselector = (__force u16)cpu_to_le16(msg->controlselector);
/* Mental Note: line 462 tmmhComResBusPCIe.cpp */
/* Check if we're going to wrap again */
if (new_swp > bus->m_dwSizeSetRing) {
/* Ring wraps */
new_swp -= bus->m_dwSizeSetRing;
space_rem = bus->m_dwSizeSetRing - curr_swp;
dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
space_rem);
dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
(u32)sizeof(*msg));
if (space_rem < sizeof(*msg)) {
dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
/* Split the msg into pieces as the ring wraps */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, space_rem);
memcpy_toio(bus->m_pdwSetRing, (u8 *)msg + space_rem,
sizeof(*msg) - space_rem);
memcpy_toio(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
buf, size);
} else if (space_rem == sizeof(*msg)) {
dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
/* Additional data at the beginning of the ring */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
memcpy_toio(bus->m_pdwSetRing, buf, size);
} else {
/* Additional data wraps around the ring */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
if (size > 0) {
memcpy_toio(bus->m_pdwSetRing + curr_swp +
sizeof(*msg), buf, space_rem -
sizeof(*msg));
memcpy_toio(bus->m_pdwSetRing, (u8 *)buf +
space_rem - sizeof(*msg),
bytes_to_write - space_rem);
}
}
} /* (new_swp > bus->m_dwSizeSetRing) */
else {
dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
/* The ring buffer doesn't wrap, two simple copies */
memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
memcpy_toio(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
size);
}
dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
/* Update the bus write position */
saa7164_writel(bus->m_dwSetWritePos, new_swp);
/* Convert back to cpu after writing the msg to the ringbuffer. */
msg->size = le16_to_cpu((__force __le16)msg->size);
msg->command = le32_to_cpu((__force __le32)msg->command);
msg->controlselector = le16_to_cpu((__force __le16)msg->controlselector);
ret = SAA_OK;
out:
saa7164_bus_dump(dev);
mutex_unlock(&bus->lock);
saa7164_bus_verify(dev);
return ret;
}
/*
* Receive a command or a response from the bus. The implementation does not
* know if it is a command or a response it simply dequeues the data,
* depending on the bus information given in the struct tmComResBusInfo
* structure.
*
* Return Value:
* 0 The function executed successfully.
* < 0 One or more members are not initialized.
*/
int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
void *buf, int peekonly)
{
struct tmComResBusInfo *bus = &dev->bus;
u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
new_grp, buf_size, space_rem;
struct tmComResInfo msg_tmp;
int ret = SAA_ERR_BAD_PARAMETER;
saa7164_bus_verify(dev);
if (msg == NULL)
return ret;
if (msg->size > dev->bus.m_wMaxReqSize) {
printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
__func__);
return ret;
}
if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
printk(KERN_ERR
"%s() Missing msg buf, size should be %d bytes\n",
__func__, msg->size);
return ret;
}
mutex_lock(&bus->lock);
/* Peek the bus to see if a msg exists, if it's not what we're expecting
* then return cleanly else read the message from the bus.
*/
curr_gwp = saa7164_readl(bus->m_dwGetWritePos);
curr_grp = saa7164_readl(bus->m_dwGetReadPos);
if (curr_gwp == curr_grp) {
ret = SAA_ERR_EMPTY;
goto out;
}
bytes_to_read = sizeof(*msg);
/* Calculate write distance to current read position */
write_distance = 0;
if (curr_gwp >= curr_grp)
/* Write doesn't wrap around the ring */
write_distance = curr_gwp - curr_grp;
else
/* Write wraps around the ring */
write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
if (bytes_to_read > write_distance) {
printk(KERN_ERR "%s() No message/response found\n", __func__);
ret = SAA_ERR_INVALID_COMMAND;
goto out;
}
/* Calculate the new read position */
new_grp = curr_grp + bytes_to_read;
if (new_grp > bus->m_dwSizeGetRing) {
/* Ring wraps */
new_grp -= bus->m_dwSizeGetRing;
space_rem = bus->m_dwSizeGetRing - curr_grp;
memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
memcpy_fromio((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
bytes_to_read - space_rem);
} else {
/* No wrapping */
memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
}
/* Convert from little endian to CPU */
msg_tmp.size = le16_to_cpu((__force __le16)msg_tmp.size);
msg_tmp.command = le32_to_cpu((__force __le32)msg_tmp.command);
msg_tmp.controlselector = le16_to_cpu((__force __le16)msg_tmp.controlselector);
memcpy(msg, &msg_tmp, sizeof(*msg));
/* No need to update the read positions, because this was a peek */
/* If the caller specifically want to peek, return */
if (peekonly) {
goto peekout;
}
/* Check if the command/response matches what is expected */
if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
(msg_tmp.controlselector != msg->controlselector) ||
(msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
saa7164_bus_dumpmsg(dev, msg, buf);
saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
ret = SAA_ERR_INVALID_COMMAND;
goto out;
}
/* Get the actual command and response from the bus */
buf_size = msg->size;
bytes_to_read = sizeof(*msg) + msg->size;
/* Calculate write distance to current read position */
write_distance = 0;
if (curr_gwp >= curr_grp)
/* Write doesn't wrap around the ring */
write_distance = curr_gwp - curr_grp;
else
/* Write wraps around the ring */
write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
if (bytes_to_read > write_distance) {
printk(KERN_ERR "%s() Invalid bus state, missing msg or mangled ring, faulty H/W / bad code?\n",
__func__);
ret = SAA_ERR_INVALID_COMMAND;
goto out;
}
/* Calculate the new read position */
new_grp = curr_grp + bytes_to_read;
if (new_grp > bus->m_dwSizeGetRing) {
/* Ring wraps */
new_grp -= bus->m_dwSizeGetRing;
space_rem = bus->m_dwSizeGetRing - curr_grp;
if (space_rem < sizeof(*msg)) {
if (buf)
memcpy_fromio(buf, bus->m_pdwGetRing + sizeof(*msg) -
space_rem, buf_size);
} else if (space_rem == sizeof(*msg)) {
if (buf)
memcpy_fromio(buf, bus->m_pdwGetRing, buf_size);
} else {
/* Additional data wraps around the ring */
if (buf) {
memcpy_fromio(buf, bus->m_pdwGetRing + curr_grp +
sizeof(*msg), space_rem - sizeof(*msg));
memcpy_fromio(buf + space_rem - sizeof(*msg),
bus->m_pdwGetRing, bytes_to_read -
space_rem);
}
}
} else {
/* No wrapping */
if (buf)
memcpy_fromio(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
buf_size);
}
/* Update the read positions, adjusting the ring */
saa7164_writel(bus->m_dwGetReadPos, new_grp);
peekout:
ret = SAA_OK;
out:
mutex_unlock(&bus->lock);
saa7164_bus_verify(dev);
return ret;
}
| linux-master | drivers/media/pci/saa7164/saa7164-bus.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/io.h>
#include "saa7164.h"
static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
{
struct saa7164_i2c *bus = i2c_adap->algo_data;
struct saa7164_dev *dev = bus->dev;
int i, retval = 0;
dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
for (i = 0 ; i < num; i++) {
dprintk(DBGLVL_I2C, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
__func__, num, msgs[i].addr, msgs[i].len);
if (msgs[i].flags & I2C_M_RD) {
retval = saa7164_api_i2c_read(bus,
msgs[i].addr,
0 /* reglen */,
NULL /* reg */, msgs[i].len, msgs[i].buf);
} else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
msgs[i].addr == msgs[i + 1].addr) {
/* write then read from same address */
retval = saa7164_api_i2c_read(bus, msgs[i].addr,
msgs[i].len, msgs[i].buf,
msgs[i+1].len, msgs[i+1].buf
);
i++;
if (retval < 0)
goto err;
} else {
/* write */
retval = saa7164_api_i2c_write(bus, msgs[i].addr,
msgs[i].len, msgs[i].buf);
}
if (retval < 0)
goto err;
}
return num;
err:
return retval;
}
static u32 saa7164_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C;
}
static const struct i2c_algorithm saa7164_i2c_algo_template = {
.master_xfer = i2c_xfer,
.functionality = saa7164_functionality,
};
/* ----------------------------------------------------------------------- */
static const struct i2c_adapter saa7164_i2c_adap_template = {
.name = "saa7164",
.owner = THIS_MODULE,
.algo = &saa7164_i2c_algo_template,
};
static const struct i2c_client saa7164_i2c_client_template = {
.name = "saa7164 internal",
};
int saa7164_i2c_register(struct saa7164_i2c *bus)
{
struct saa7164_dev *dev = bus->dev;
dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
bus->i2c_adap = saa7164_i2c_adap_template;
bus->i2c_client = saa7164_i2c_client_template;
bus->i2c_adap.dev.parent = &dev->pci->dev;
strscpy(bus->i2c_adap.name, bus->dev->name,
sizeof(bus->i2c_adap.name));
bus->i2c_adap.algo_data = bus;
i2c_set_adapdata(&bus->i2c_adap, bus);
i2c_add_adapter(&bus->i2c_adap);
bus->i2c_client.adapter = &bus->i2c_adap;
if (0 != bus->i2c_rc)
printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
dev->name, bus->nr);
return bus->i2c_rc;
}
int saa7164_i2c_unregister(struct saa7164_i2c *bus)
{
i2c_del_adapter(&bus->i2c_adap);
return 0;
}
| linux-master | drivers/media/pci/saa7164/saa7164-i2c.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/wait.h>
#include "saa7164.h"
static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
{
int i, ret = -1;
mutex_lock(&dev->lock);
for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
if (dev->cmds[i].inuse == 0) {
dev->cmds[i].inuse = 1;
dev->cmds[i].signalled = 0;
dev->cmds[i].timeout = 0;
ret = dev->cmds[i].seqno;
break;
}
}
mutex_unlock(&dev->lock);
return ret;
}
static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
{
mutex_lock(&dev->lock);
if ((dev->cmds[seqno].inuse == 1) &&
(dev->cmds[seqno].seqno == seqno)) {
dev->cmds[seqno].inuse = 0;
dev->cmds[seqno].signalled = 0;
dev->cmds[seqno].timeout = 0;
}
mutex_unlock(&dev->lock);
}
static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
{
mutex_lock(&dev->lock);
if ((dev->cmds[seqno].inuse == 1) &&
(dev->cmds[seqno].seqno == seqno)) {
dev->cmds[seqno].timeout = 1;
}
mutex_unlock(&dev->lock);
}
static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
{
int ret = 0;
mutex_lock(&dev->lock);
if ((dev->cmds[seqno].inuse == 1) &&
(dev->cmds[seqno].seqno == seqno)) {
ret = dev->cmds[seqno].timeout;
}
mutex_unlock(&dev->lock);
return ret;
}
/* Commands to the f/w get marshelled to/from this code then onto the PCI
* -bus/c running buffer. */
int saa7164_irq_dequeue(struct saa7164_dev *dev)
{
int ret = SAA_OK, i = 0;
u32 timeout;
wait_queue_head_t *q = NULL;
u8 tmp[512];
dprintk(DBGLVL_CMD, "%s()\n", __func__);
/* While any outstand message on the bus exists... */
do {
/* Peek the msg bus */
struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
if (ret != SAA_OK)
break;
q = &dev->cmds[tRsp.seqno].wait;
timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
if (!timeout) {
dprintk(DBGLVL_CMD,
"%s() signalled seqno(%d) (for dequeue)\n",
__func__, tRsp.seqno);
dev->cmds[tRsp.seqno].signalled = 1;
wake_up(q);
} else {
printk(KERN_ERR
"%s() found timed out command on the bus\n",
__func__);
/* Clean the bus */
ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
if (ret == SAA_ERR_EMPTY)
/* Someone else already fetched the response */
return SAA_OK;
if (ret != SAA_OK)
return ret;
}
/* It's unlikely to have more than 4 or 5 pending messages,
* ensure we exit at some point regardless.
*/
} while (i++ < 32);
return ret;
}
/* Commands to the f/w get marshelled to/from this code then onto the PCI
* -bus/c running buffer. */
static int saa7164_cmd_dequeue(struct saa7164_dev *dev)
{
int ret;
u32 timeout;
wait_queue_head_t *q = NULL;
u8 tmp[512];
dprintk(DBGLVL_CMD, "%s()\n", __func__);
while (true) {
struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
if (ret == SAA_ERR_EMPTY)
return SAA_OK;
if (ret != SAA_OK)
return ret;
q = &dev->cmds[tRsp.seqno].wait;
timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
if (timeout) {
printk(KERN_ERR "found timed out command on the bus\n");
/* Clean the bus */
ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
printk(KERN_ERR "ret = %x\n", ret);
if (ret == SAA_ERR_EMPTY)
/* Someone else already fetched the response */
return SAA_OK;
if (ret != SAA_OK)
return ret;
if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
printk(KERN_ERR "split response\n");
else
saa7164_cmd_free_seqno(dev, tRsp.seqno);
printk(KERN_ERR " timeout continue\n");
continue;
}
dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
__func__, tRsp.seqno);
dev->cmds[tRsp.seqno].signalled = 1;
wake_up(q);
return SAA_OK;
}
}
static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
void *buf)
{
struct tmComResBusInfo *bus = &dev->bus;
u8 cmd_sent;
u16 size, idx;
u32 cmds;
void *tmp;
int ret = -1;
if (!msg) {
printk(KERN_ERR "%s() !msg\n", __func__);
return SAA_ERR_BAD_PARAMETER;
}
mutex_lock(&dev->cmds[msg->id].lock);
size = msg->size;
cmds = size / bus->m_wMaxReqSize;
if (size % bus->m_wMaxReqSize == 0)
cmds -= 1;
cmd_sent = 0;
/* Split the request into smaller chunks */
for (idx = 0; idx < cmds; idx++) {
msg->flags |= SAA_CMDFLAG_CONTINUE;
msg->size = bus->m_wMaxReqSize;
tmp = buf + idx * bus->m_wMaxReqSize;
ret = saa7164_bus_set(dev, msg, tmp);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
if (cmd_sent) {
ret = SAA_ERR_BUSY;
goto out;
}
ret = SAA_ERR_OVERFLOW;
goto out;
}
cmd_sent = 1;
}
/* If not the last command... */
if (idx != 0)
msg->flags &= ~SAA_CMDFLAG_CONTINUE;
msg->size = size - idx * bus->m_wMaxReqSize;
ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
if (cmd_sent) {
ret = SAA_ERR_BUSY;
goto out;
}
ret = SAA_ERR_OVERFLOW;
goto out;
}
ret = SAA_OK;
out:
mutex_unlock(&dev->cmds[msg->id].lock);
return ret;
}
/* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
* the event never occurred, or SAA_OK if it was signaled during the wait.
*/
static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
{
wait_queue_head_t *q = NULL;
int ret = SAA_BUS_TIMEOUT;
unsigned long stamp;
int r;
if (saa_debug >= 4)
saa7164_bus_dump(dev);
dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
mutex_lock(&dev->lock);
if ((dev->cmds[seqno].inuse == 1) &&
(dev->cmds[seqno].seqno == seqno)) {
q = &dev->cmds[seqno].wait;
}
mutex_unlock(&dev->lock);
if (q) {
/* If we haven't been signalled we need to wait */
if (dev->cmds[seqno].signalled == 0) {
stamp = jiffies;
dprintk(DBGLVL_CMD,
"%s(seqno=%d) Waiting (signalled=%d)\n",
__func__, seqno, dev->cmds[seqno].signalled);
/* Wait for signalled to be flagged or timeout */
/* In a highly stressed system this can easily extend
* into multiple seconds before the deferred worker
* is scheduled, and we're woken up via signal.
* We typically are signalled in < 50ms but it can
* take MUCH longer.
*/
wait_event_timeout(*q, dev->cmds[seqno].signalled,
(HZ * waitsecs));
r = time_before(jiffies, stamp + (HZ * waitsecs));
if (r)
ret = SAA_OK;
else
saa7164_cmd_timeout_seqno(dev, seqno);
dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
__func__, seqno, r,
dev->cmds[seqno].signalled);
} else
ret = SAA_OK;
} else
printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
__func__, seqno);
return ret;
}
void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
{
int i;
dprintk(DBGLVL_CMD, "%s()\n", __func__);
mutex_lock(&dev->lock);
for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
if (dev->cmds[i].inuse == 1) {
dprintk(DBGLVL_CMD,
"seqno %d inuse, sig = %d, t/out = %d\n",
dev->cmds[i].seqno,
dev->cmds[i].signalled,
dev->cmds[i].timeout);
}
}
for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
(dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
__func__, i);
dev->cmds[i].signalled = 1;
wake_up(&dev->cmds[i].wait);
}
}
mutex_unlock(&dev->lock);
}
int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
u16 controlselector, u16 size, void *buf)
{
struct tmComResInfo command_t, *pcommand_t;
struct tmComResInfo response_t, *presponse_t;
u8 errdata[256];
u16 resp_dsize;
u16 data_recd;
u32 loop;
int ret;
int safety = 0;
dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
__func__, saa7164_unitid_name(dev, id), id,
command, controlselector);
if ((size == 0) || (buf == NULL)) {
printk(KERN_ERR "%s() Invalid param\n", __func__);
return SAA_ERR_BAD_PARAMETER;
}
/* Prepare some basic command/response structures */
memset(&command_t, 0, sizeof(command_t));
memset(&response_t, 0, sizeof(response_t));
pcommand_t = &command_t;
presponse_t = &response_t;
command_t.id = id;
command_t.command = command;
command_t.controlselector = controlselector;
command_t.size = size;
/* Allocate a unique sequence number */
ret = saa7164_cmd_alloc_seqno(dev);
if (ret < 0) {
printk(KERN_ERR "%s() No free sequences\n", __func__);
ret = SAA_ERR_NO_RESOURCES;
goto out;
}
command_t.seqno = (u8)ret;
/* Send Command */
resp_dsize = size;
pcommand_t->size = size;
dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
__func__, pcommand_t->seqno);
dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
__func__, pcommand_t->size);
ret = saa7164_cmd_set(dev, pcommand_t, buf);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
if (ret != SAA_ERR_BUSY)
saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
else
/* Flag a timeout, because at least one
* command was sent */
saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
goto out;
}
/* With split responses we have to collect the msgs piece by piece */
data_recd = 0;
loop = 1;
while (loop) {
dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
/* if power is down and this is not a power command ... */
if (ret == SAA_BUS_TIMEOUT) {
printk(KERN_ERR "Event timed out\n");
saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
return ret;
}
if (ret != SAA_OK) {
printk(KERN_ERR "spurious error\n");
return ret;
}
/* Peek response */
ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
if (ret == SAA_ERR_EMPTY) {
dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
continue;
}
if (ret != SAA_OK) {
printk(KERN_ERR "peek failed\n");
return ret;
}
dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
__func__, presponse_t->seqno);
dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
__func__, presponse_t->flags);
dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
__func__, presponse_t->size);
/* Check if the response was for our command */
if (presponse_t->seqno != pcommand_t->seqno) {
dprintk(DBGLVL_CMD,
"wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
presponse_t->seqno, pcommand_t->seqno);
ret = saa7164_cmd_dequeue(dev);
if (ret != SAA_OK) {
printk(KERN_ERR "dequeue failed, ret = %d\n",
ret);
if (safety++ > 16) {
printk(KERN_ERR
"dequeue exceeded, safety exit\n");
return SAA_ERR_BUSY;
}
}
continue;
}
if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
memset(&errdata[0], 0, sizeof(errdata));
ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
if (ret != SAA_OK) {
printk(KERN_ERR "get error(2)\n");
return ret;
}
saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
__func__, errdata[0], errdata[1], errdata[2],
errdata[3]);
/* Map error codes */
dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
__func__, errdata[0]);
switch (errdata[0]) {
case PVC_ERRORCODE_INVALID_COMMAND:
dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
__func__);
ret = SAA_ERR_INVALID_COMMAND;
break;
case PVC_ERRORCODE_INVALID_DATA:
dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
__func__);
ret = SAA_ERR_BAD_PARAMETER;
break;
case PVC_ERRORCODE_TIMEOUT:
dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
ret = SAA_ERR_TIMEOUT;
break;
case PVC_ERRORCODE_NAK:
dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
ret = SAA_ERR_NULL_PACKET;
break;
case PVC_ERRORCODE_UNKNOWN:
case PVC_ERRORCODE_INVALID_CONTROL:
dprintk(DBGLVL_CMD,
"%s() UNKNOWN OR INVALID CONTROL\n",
__func__);
ret = SAA_ERR_NOT_SUPPORTED;
break;
default:
dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
ret = SAA_ERR_NOT_SUPPORTED;
}
/* See of other commands are on the bus */
if (saa7164_cmd_dequeue(dev) != SAA_OK)
printk(KERN_ERR "dequeue(2) failed\n");
return ret;
}
/* If response is invalid */
if ((presponse_t->id != pcommand_t->id) ||
(presponse_t->command != pcommand_t->command) ||
(presponse_t->controlselector !=
pcommand_t->controlselector) ||
(((resp_dsize - data_recd) != presponse_t->size) &&
!(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
((resp_dsize - data_recd) < presponse_t->size)) {
/* Invalid */
dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
if (ret != SAA_OK) {
printk(KERN_ERR "get failed\n");
return ret;
}
/* See of other commands are on the bus */
if (saa7164_cmd_dequeue(dev) != SAA_OK)
printk(KERN_ERR "dequeue(3) failed\n");
continue;
}
/* OK, now we're actually getting out correct response */
ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
if (ret != SAA_OK) {
printk(KERN_ERR "get failed\n");
return ret;
}
data_recd = presponse_t->size + data_recd;
if (resp_dsize == data_recd) {
dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
break;
}
/* See of other commands are on the bus */
if (saa7164_cmd_dequeue(dev) != SAA_OK)
printk(KERN_ERR "dequeue(3) failed\n");
} /* (loop) */
/* Release the sequence number allocation */
saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
/* if powerdown signal all pending commands */
dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
/* See of other commands are on the bus */
if (saa7164_cmd_dequeue(dev) != SAA_OK)
printk(KERN_ERR "dequeue(4) failed\n");
ret = SAA_OK;
out:
return ret;
}
| linux-master | drivers/media/pci/saa7164/saa7164-cmd.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include "saa7164.h"
/* Take the encoder configuration from the port struct and
* flush it to the hardware.
*/
static void saa7164_vbi_configure(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_VBI, "%s()\n", __func__);
port->vbi_params.width = port->enc_port->width;
port->vbi_params.height = port->enc_port->height;
port->vbi_params.is_50hz =
(port->enc_port->encodernorm.id & V4L2_STD_625_50) != 0;
/* Set up the DIF (enable it) for analog mode by default */
saa7164_api_initialize_dif(port);
dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
}
static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
{
struct list_head *c, *n, *p, *q, *l, *v;
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct saa7164_user_buffer *ubuf;
/* Remove any allocated buffers */
mutex_lock(&port->dmaqueue_lock);
dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
list_del(c);
saa7164_buffer_dealloc(buf);
}
dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
list_for_each_safe(p, q, &port->list_buf_used.list) {
ubuf = list_entry(p, struct saa7164_user_buffer, list);
list_del(p);
saa7164_buffer_dealloc_user(ubuf);
}
dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
list_for_each_safe(l, v, &port->list_buf_free.list) {
ubuf = list_entry(l, struct saa7164_user_buffer, list);
list_del(l);
saa7164_buffer_dealloc_user(ubuf);
}
mutex_unlock(&port->dmaqueue_lock);
dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
return 0;
}
/* Dynamic buffer switch at vbi start time */
static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct saa7164_user_buffer *ubuf;
struct tmHWStreamParameters *params = &port->hw_streamingparams;
int result = -ENODEV, i;
int len = 0;
dprintk(DBGLVL_VBI, "%s()\n", __func__);
/* TODO: NTSC SPECIFIC */
/* Init and establish defaults */
params->samplesperline = 1440;
params->numberoflines = 12;
params->numberoflines = 18;
params->pitch = 1600;
params->pitch = 1440;
params->numpagetables = 2 +
((params->numberoflines * params->pitch) / PAGE_SIZE);
params->bitspersample = 8;
params->linethreshold = 0;
params->pagetablelistvirt = NULL;
params->pagetablelistphys = NULL;
params->numpagetableentries = port->hwcfg.buffercount;
/* Allocate the PCI resources, buffers (hard) */
for (i = 0; i < port->hwcfg.buffercount; i++) {
buf = saa7164_buffer_alloc(port,
params->numberoflines *
params->pitch);
if (!buf) {
printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
__func__, result);
result = -ENOMEM;
goto failed;
} else {
mutex_lock(&port->dmaqueue_lock);
list_add_tail(&buf->list, &port->dmaqueue.list);
mutex_unlock(&port->dmaqueue_lock);
}
}
/* Allocate some kernel buffers for copying
* to userpsace.
*/
len = params->numberoflines * params->pitch;
if (vbi_buffers < 16)
vbi_buffers = 16;
if (vbi_buffers > 512)
vbi_buffers = 512;
for (i = 0; i < vbi_buffers; i++) {
ubuf = saa7164_buffer_alloc_user(dev, len);
if (ubuf) {
mutex_lock(&port->dmaqueue_lock);
list_add_tail(&ubuf->list, &port->list_buf_free.list);
mutex_unlock(&port->dmaqueue_lock);
}
}
result = 0;
failed:
return result;
}
static int saa7164_vbi_initialize(struct saa7164_port *port)
{
saa7164_vbi_configure(port);
return 0;
}
/* -- V4L2 --------------------------------------------------------- */
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
{
struct saa7164_vbi_fh *fh = file->private_data;
return saa7164_s_std(fh->port->enc_port, id);
}
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
{
struct saa7164_encoder_fh *fh = file->private_data;
return saa7164_g_std(fh->port->enc_port, id);
}
static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
struct saa7164_vbi_fh *fh = file->private_data;
return saa7164_g_input(fh->port->enc_port, i);
}
static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
{
struct saa7164_vbi_fh *fh = file->private_data;
return saa7164_s_input(fh->port->enc_port, i);
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct saa7164_vbi_fh *fh = file->private_data;
return saa7164_g_frequency(fh->port->enc_port, f);
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct saa7164_vbi_fh *fh = file->private_data;
int ret = saa7164_s_frequency(fh->port->enc_port, f);
if (ret == 0)
saa7164_vbi_initialize(fh->port);
return ret;
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct saa7164_vbi_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_dev *dev = port->dev;
strscpy(cap->driver, dev->name, sizeof(cap->driver));
strscpy(cap->card, saa7164_boards[dev->board].name,
sizeof(cap->card));
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE |
V4L2_CAP_DEVICE_CAPS;
return 0;
}
static int saa7164_vbi_stop_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
ret = 0;
}
return ret;
}
static int saa7164_vbi_acquire_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
ret = 0;
}
return ret;
}
static int saa7164_vbi_pause_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
ret = 0;
}
return ret;
}
/* Firmware is very windows centric, meaning you have to transition
* the part through AVStream / KS Windows stages, forwards or backwards.
* States are: stopped, acquired (h/w), paused, started.
* We have to leave here will all of the soft buffers on the free list,
* else the cfg_post() func won't have soft buffers to correctly configure.
*/
static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct saa7164_user_buffer *ubuf;
struct list_head *c, *n;
int ret;
dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
ret = saa7164_vbi_pause_port(port);
ret = saa7164_vbi_acquire_port(port);
ret = saa7164_vbi_stop_port(port);
dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
port->nr);
/* Reset the state of any allocated buffer resources */
mutex_lock(&port->dmaqueue_lock);
/* Reset the hard and soft buffer state */
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
buf->flags = SAA7164_BUFFER_FREE;
buf->pos = 0;
}
list_for_each_safe(c, n, &port->list_buf_used.list) {
ubuf = list_entry(c, struct saa7164_user_buffer, list);
ubuf->pos = 0;
list_move_tail(&ubuf->list, &port->list_buf_free.list);
}
mutex_unlock(&port->dmaqueue_lock);
/* Free any allocated resources */
saa7164_vbi_buffers_dealloc(port);
dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
return ret;
}
static int saa7164_vbi_start_streaming(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int result, ret = 0;
dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
port->done_first_interrupt = 0;
/* allocate all of the PCIe DMA buffer resources on the fly,
* allowing switching between TS and PS payloads without
* requiring a complete driver reload.
*/
saa7164_vbi_buffers_alloc(port);
/* Configure the encoder with any cache values */
#if 0
saa7164_api_set_encoder(port);
saa7164_api_get_encoder(port);
#endif
/* Place the empty buffers on the hardware */
saa7164_buffer_cfg_port(port);
/* Negotiate format */
if (saa7164_api_set_vbi_format(port) != SAA_OK) {
printk(KERN_ERR "%s() No supported VBI format\n", __func__);
ret = -EIO;
goto out;
}
/* Acquire the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
__func__, result);
ret = -EIO;
goto out;
} else
dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
/* Pause the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_vbi_stop_port(port);
if (result != SAA_OK) {
printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
goto out;
} else
dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
/* Start the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_vbi_acquire_port(port);
result = saa7164_vbi_stop_port(port);
if (result != SAA_OK) {
printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
} else
dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
out:
return ret;
}
static int saa7164_vbi_fmt(struct file *file, void *priv,
struct v4l2_format *f)
{
/* ntsc */
f->fmt.vbi.samples_per_line = 1440;
f->fmt.vbi.sampling_rate = 27000000;
f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
f->fmt.vbi.offset = 0;
f->fmt.vbi.flags = 0;
f->fmt.vbi.start[0] = 10;
f->fmt.vbi.count[0] = 18;
f->fmt.vbi.start[1] = 263 + 10 + 1;
f->fmt.vbi.count[1] = 18;
memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
return 0;
}
static int fops_open(struct file *file)
{
struct saa7164_dev *dev;
struct saa7164_port *port;
struct saa7164_vbi_fh *fh;
port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
if (!port)
return -ENODEV;
dev = port->dev;
dprintk(DBGLVL_VBI, "%s()\n", __func__);
/* allocate + initialize per filehandle data */
fh = kzalloc(sizeof(*fh), GFP_KERNEL);
if (NULL == fh)
return -ENOMEM;
fh->port = port;
v4l2_fh_init(&fh->fh, video_devdata(file));
v4l2_fh_add(&fh->fh);
file->private_data = fh;
return 0;
}
static int fops_release(struct file *file)
{
struct saa7164_vbi_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_VBI, "%s()\n", __func__);
/* Shut device down on last close */
if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
if (atomic_dec_return(&port->v4l_reader_count) == 0) {
/* stop vbi capture then cancel buffers */
saa7164_vbi_stop_streaming(port);
}
}
v4l2_fh_del(&fh->fh);
v4l2_fh_exit(&fh->fh);
kfree(fh);
return 0;
}
static struct
saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
{
struct saa7164_user_buffer *ubuf = NULL;
struct saa7164_dev *dev = port->dev;
u32 crc;
mutex_lock(&port->dmaqueue_lock);
if (!list_empty(&port->list_buf_used.list)) {
ubuf = list_first_entry(&port->list_buf_used.list,
struct saa7164_user_buffer, list);
if (crc_checking) {
crc = crc32(0, ubuf->data, ubuf->actual_size);
if (crc != ubuf->crc) {
printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
__func__,
ubuf, ubuf->crc, crc);
}
}
}
mutex_unlock(&port->dmaqueue_lock);
dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
return ubuf;
}
static ssize_t fops_read(struct file *file, char __user *buffer,
size_t count, loff_t *pos)
{
struct saa7164_vbi_fh *fh = file->private_data;
struct saa7164_port *port = fh->port;
struct saa7164_user_buffer *ubuf = NULL;
struct saa7164_dev *dev = port->dev;
int ret = 0;
int rem, cnt;
u8 *p;
port->last_read_msecs_diff = port->last_read_msecs;
port->last_read_msecs = jiffies_to_msecs(jiffies);
port->last_read_msecs_diff = port->last_read_msecs -
port->last_read_msecs_diff;
saa7164_histogram_update(&port->read_interval,
port->last_read_msecs_diff);
if (*pos) {
printk(KERN_ERR "%s() ESPIPE\n", __func__);
return -ESPIPE;
}
if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
if (atomic_inc_return(&port->v4l_reader_count) == 1) {
if (saa7164_vbi_initialize(port) < 0) {
printk(KERN_ERR "%s() EINVAL\n", __func__);
return -EINVAL;
}
saa7164_vbi_start_streaming(port);
msleep(200);
}
}
/* blocking wait for buffer */
if ((file->f_flags & O_NONBLOCK) == 0) {
if (wait_event_interruptible(port->wait_read,
saa7164_vbi_next_buf(port))) {
printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
return -ERESTARTSYS;
}
}
/* Pull the first buffer from the used list */
ubuf = saa7164_vbi_next_buf(port);
while ((count > 0) && ubuf) {
/* set remaining bytes to copy */
rem = ubuf->actual_size - ubuf->pos;
cnt = rem > count ? count : rem;
p = ubuf->data + ubuf->pos;
dprintk(DBGLVL_VBI,
"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
__func__, (int)count, cnt, rem, ubuf, ubuf->pos);
if (copy_to_user(buffer, p, cnt)) {
printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
if (!ret) {
printk(KERN_ERR "%s() EFAULT\n", __func__);
ret = -EFAULT;
}
goto err;
}
ubuf->pos += cnt;
count -= cnt;
buffer += cnt;
ret += cnt;
if (ubuf->pos > ubuf->actual_size)
printk(KERN_ERR "read() pos > actual, huh?\n");
if (ubuf->pos == ubuf->actual_size) {
/* finished with current buffer, take next buffer */
/* Requeue the buffer on the free list */
ubuf->pos = 0;
mutex_lock(&port->dmaqueue_lock);
list_move_tail(&ubuf->list, &port->list_buf_free.list);
mutex_unlock(&port->dmaqueue_lock);
/* Dequeue next */
if ((file->f_flags & O_NONBLOCK) == 0) {
if (wait_event_interruptible(port->wait_read,
saa7164_vbi_next_buf(port))) {
break;
}
}
ubuf = saa7164_vbi_next_buf(port);
}
}
err:
if (!ret && !ubuf) {
printk(KERN_ERR "%s() EAGAIN\n", __func__);
ret = -EAGAIN;
}
return ret;
}
static __poll_t fops_poll(struct file *file, poll_table *wait)
{
struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
struct saa7164_port *port = fh->port;
__poll_t mask = 0;
port->last_poll_msecs_diff = port->last_poll_msecs;
port->last_poll_msecs = jiffies_to_msecs(jiffies);
port->last_poll_msecs_diff = port->last_poll_msecs -
port->last_poll_msecs_diff;
saa7164_histogram_update(&port->poll_interval,
port->last_poll_msecs_diff);
if (!video_is_registered(port->v4l_device))
return EPOLLERR;
if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
if (atomic_inc_return(&port->v4l_reader_count) == 1) {
if (saa7164_vbi_initialize(port) < 0)
return EPOLLERR;
saa7164_vbi_start_streaming(port);
msleep(200);
}
}
/* blocking wait for buffer */
if ((file->f_flags & O_NONBLOCK) == 0) {
if (wait_event_interruptible(port->wait_read,
saa7164_vbi_next_buf(port))) {
return EPOLLERR;
}
}
/* Pull the first buffer from the used list */
if (!list_empty(&port->list_buf_used.list))
mask |= EPOLLIN | EPOLLRDNORM;
return mask;
}
static const struct v4l2_file_operations vbi_fops = {
.owner = THIS_MODULE,
.open = fops_open,
.release = fops_release,
.read = fops_read,
.poll = fops_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
.vidioc_s_std = vidioc_s_std,
.vidioc_g_std = vidioc_g_std,
.vidioc_enum_input = saa7164_enum_input,
.vidioc_g_input = vidioc_g_input,
.vidioc_s_input = vidioc_s_input,
.vidioc_g_tuner = saa7164_g_tuner,
.vidioc_s_tuner = saa7164_s_tuner,
.vidioc_g_frequency = vidioc_g_frequency,
.vidioc_s_frequency = vidioc_s_frequency,
.vidioc_querycap = vidioc_querycap,
.vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
.vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
.vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
};
static struct video_device saa7164_vbi_template = {
.name = "saa7164",
.fops = &vbi_fops,
.ioctl_ops = &vbi_ioctl_ops,
.minor = -1,
.tvnorms = SAA7164_NORMS,
.device_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_TUNER,
};
static struct video_device *saa7164_vbi_alloc(
struct saa7164_port *port,
struct pci_dev *pci,
struct video_device *template,
char *type)
{
struct video_device *vfd;
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_VBI, "%s()\n", __func__);
vfd = video_device_alloc();
if (NULL == vfd)
return NULL;
*vfd = *template;
snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
type, saa7164_boards[dev->board].name);
vfd->v4l2_dev = &dev->v4l2_dev;
vfd->release = video_device_release;
return vfd;
}
int saa7164_vbi_register(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int result = -ENODEV;
dprintk(DBGLVL_VBI, "%s()\n", __func__);
BUG_ON(port->type != SAA7164_MPEG_VBI);
/* Sanity check that the PCI configuration space is active */
if (port->hwcfg.BARLocation == 0) {
printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
__func__, result);
result = -ENOMEM;
goto failed;
}
/* Establish VBI defaults here */
/* Allocate and register the video device node */
port->v4l_device = saa7164_vbi_alloc(port,
dev->pci, &saa7164_vbi_template, "vbi");
if (!port->v4l_device) {
printk(KERN_INFO "%s: can't allocate vbi device\n",
dev->name);
result = -ENOMEM;
goto failed;
}
port->enc_port = &dev->ports[port->nr - 2];
video_set_drvdata(port->v4l_device, port);
result = video_register_device(port->v4l_device,
VFL_TYPE_VBI, -1);
if (result < 0) {
printk(KERN_INFO "%s: can't register vbi device\n",
dev->name);
/* TODO: We're going to leak here if we don't dealloc
The buffers above. The unreg function can't deal wit it.
*/
goto failed;
}
printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
dev->name, port->v4l_device->num);
/* Configure the hardware defaults */
result = 0;
failed:
return result;
}
void saa7164_vbi_unregister(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
BUG_ON(port->type != SAA7164_MPEG_VBI);
if (port->v4l_device) {
if (port->v4l_device->minor != -1)
video_unregister_device(port->v4l_device);
else
video_device_release(port->v4l_device);
port->v4l_device = NULL;
}
}
| linux-master | drivers/media/pci/saa7164/saa7164-vbi.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/slab.h>
#include "saa7164.h"
/* The PCI address space for buffer handling looks like this:
*
* +-u32 wide-------------+
* | +
* +-u64 wide------------------------------------+
* + +
* +----------------------+
* | CurrentBufferPtr + Pointer to current PCI buffer >-+
* +----------------------+ |
* | Unused + |
* +----------------------+ |
* | Pitch + = 188 (bytes) |
* +----------------------+ |
* | PCI buffer size + = pitch * number of lines (312) |
* +----------------------+ |
* |0| Buf0 Write Offset + |
* +----------------------+ v
* |1| Buf1 Write Offset + |
* +----------------------+ |
* |2| Buf2 Write Offset + |
* +----------------------+ |
* |3| Buf3 Write Offset + |
* +----------------------+ |
* ... More write offsets |
* +---------------------------------------------+ |
* +0| set of ptrs to PCI pagetables + |
* +---------------------------------------------+ |
* +1| set of ptrs to PCI pagetables + <--------+
* +---------------------------------------------+
* +2| set of ptrs to PCI pagetables +
* +---------------------------------------------+
* +3| set of ptrs to PCI pagetables + >--+
* +---------------------------------------------+ |
* ... More buffer pointers | +----------------+
* +->| pt[0] TS data |
* | +----------------+
* |
* | +----------------+
* +->| pt[1] TS data |
* | +----------------+
* | etc
*/
void saa7164_buffer_display(struct saa7164_buffer *buf)
{
struct saa7164_dev *dev = buf->port->dev;
int i;
dprintk(DBGLVL_BUF, "%s() buffer @ 0x%p nr=%d\n",
__func__, buf, buf->idx);
dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08llx len = 0x%x\n",
buf->cpu, (long long)buf->dma, buf->pci_size);
dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08llx len = 0x%x\n",
buf->pt_cpu, (long long)buf->pt_dma, buf->pt_size);
/* Format the Page Table Entries to point into the data buffer */
for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
i, buf->pt_cpu, (u64)*(buf->pt_cpu));
}
}
/* Allocate a new buffer structure and associated PCI space in bytes.
* len must be a multiple of sizeof(u64)
*/
struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port,
u32 len)
{
struct tmHWStreamParameters *params = &port->hw_streamingparams;
struct saa7164_buffer *buf = NULL;
struct saa7164_dev *dev = port->dev;
int i;
if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
goto ret;
}
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf)
goto ret;
buf->idx = -1;
buf->port = port;
buf->flags = SAA7164_BUFFER_FREE;
buf->pos = 0;
buf->actual_size = params->pitch * params->numberoflines;
buf->crc = 0;
/* TODO: arg len is being ignored */
buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
/* Allocate contiguous memory */
buf->cpu = dma_alloc_coherent(&port->dev->pci->dev, buf->pci_size,
&buf->dma, GFP_KERNEL);
if (!buf->cpu)
goto fail1;
buf->pt_cpu = dma_alloc_coherent(&port->dev->pci->dev, buf->pt_size,
&buf->pt_dma, GFP_KERNEL);
if (!buf->pt_cpu)
goto fail2;
/* init the buffers to a known pattern, easier during debugging */
memset(buf->cpu, 0xff, buf->pci_size);
buf->crc = crc32(0, buf->cpu, buf->actual_size);
memset(buf->pt_cpu, 0xff, buf->pt_size);
dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p (%d pageptrs)\n",
__func__, buf, params->numpagetables);
dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
buf->cpu, (long)buf->dma, buf->pci_size);
dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
/* Format the Page Table Entries to point into the data buffer */
for (i = 0 ; i < params->numpagetables; i++) {
*(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
i, buf->pt_cpu, (u64)*(buf->pt_cpu));
}
goto ret;
fail2:
dma_free_coherent(&port->dev->pci->dev, buf->pci_size, buf->cpu,
buf->dma);
fail1:
kfree(buf);
buf = NULL;
ret:
return buf;
}
int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
{
struct saa7164_dev *dev;
if (!buf || !buf->port)
return SAA_ERR_BAD_PARAMETER;
dev = buf->port->dev;
dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
__func__, buf);
if (buf->flags != SAA7164_BUFFER_FREE)
log_warn(" freeing a non-free buffer\n");
dma_free_coherent(&dev->pci->dev, buf->pci_size, buf->cpu, buf->dma);
dma_free_coherent(&dev->pci->dev, buf->pt_size, buf->pt_cpu,
buf->pt_dma);
kfree(buf);
return SAA_OK;
}
int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
{
struct saa7164_dev *dev = port->dev;
if ((i < 0) || (i >= port->hwcfg.buffercount))
return -EINVAL;
dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
return 0;
}
/* Write a buffer into the hardware */
int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
{
struct saa7164_port *port = buf->port;
struct saa7164_dev *dev = port->dev;
if ((i < 0) || (i >= port->hwcfg.buffercount))
return -EINVAL;
dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
buf->idx = i; /* Note of which buffer list index position we occupy */
buf->flags = SAA7164_BUFFER_BUSY;
buf->pos = 0;
/* TODO: Review this in light of 32v64 assignments */
saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
buf->idx,
(u64)port->bufoffset + (i * sizeof(u32)),
saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
(u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
(u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
buf->idx);
return 0;
}
int saa7164_buffer_cfg_port(struct saa7164_port *port)
{
struct tmHWStreamParameters *params = &port->hw_streamingparams;
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct list_head *c, *n;
int i = 0;
dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
saa7164_writel(port->bufcounter, 0);
saa7164_writel(port->pitch, params->pitch);
saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
dprintk(DBGLVL_BUF, " configured:\n");
dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
saa7164_readl(port->bufcounter));
dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
saa7164_readl(port->pitch));
dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
saa7164_readl(port->bufsize));
dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
/* Poke the buffers and offsets into PCI space */
mutex_lock(&port->dmaqueue_lock);
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
BUG_ON(buf->flags != SAA7164_BUFFER_FREE);
/* Place the buffer in the h/w queue */
saa7164_buffer_activate(buf, i);
/* Don't exceed the device maximum # bufs */
BUG_ON(i > port->hwcfg.buffercount);
i++;
}
mutex_unlock(&port->dmaqueue_lock);
return 0;
}
struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev,
u32 len)
{
struct saa7164_user_buffer *buf;
buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf)
return NULL;
buf->data = kzalloc(len, GFP_KERNEL);
if (!buf->data) {
kfree(buf);
return NULL;
}
buf->actual_size = len;
buf->pos = 0;
buf->crc = 0;
dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
__func__, buf);
return buf;
}
void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
{
if (!buf)
return;
kfree(buf->data);
buf->data = NULL;
kfree(buf);
}
| linux-master | drivers/media/pci/saa7164/saa7164-buffer.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include "saa7164.h"
#include "tda10048.h"
#include "tda18271.h"
#include "s5h1411.h"
#include "si2157.h"
#include "si2168.h"
#include "lgdt3306a.h"
#define DRIVER_NAME "saa7164"
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
/* addr is in the card struct, get it from there */
static struct tda10048_config hauppauge_hvr2200_1_config = {
.demod_address = 0x10 >> 1,
.output_mode = TDA10048_SERIAL_OUTPUT,
.fwbulkwritelen = TDA10048_BULKWRITE_200,
.inversion = TDA10048_INVERSION_ON,
.dtv6_if_freq_khz = TDA10048_IF_3300,
.dtv7_if_freq_khz = TDA10048_IF_3500,
.dtv8_if_freq_khz = TDA10048_IF_4000,
.clk_freq_khz = TDA10048_CLK_16000,
};
static struct tda10048_config hauppauge_hvr2200_2_config = {
.demod_address = 0x12 >> 1,
.output_mode = TDA10048_SERIAL_OUTPUT,
.fwbulkwritelen = TDA10048_BULKWRITE_200,
.inversion = TDA10048_INVERSION_ON,
.dtv6_if_freq_khz = TDA10048_IF_3300,
.dtv7_if_freq_khz = TDA10048_IF_3500,
.dtv8_if_freq_khz = TDA10048_IF_4000,
.clk_freq_khz = TDA10048_CLK_16000,
};
static struct tda18271_std_map hauppauge_tda18271_std_map = {
.atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 3,
.if_lvl = 6, .rfagc_top = 0x37 },
.qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
.if_lvl = 6, .rfagc_top = 0x37 },
};
static struct tda18271_config hauppauge_hvr22x0_tuner_config = {
.std_map = &hauppauge_tda18271_std_map,
.gate = TDA18271_GATE_ANALOG,
.role = TDA18271_MASTER,
};
static struct tda18271_config hauppauge_hvr22x0s_tuner_config = {
.std_map = &hauppauge_tda18271_std_map,
.gate = TDA18271_GATE_ANALOG,
.role = TDA18271_SLAVE,
.output_opt = TDA18271_OUTPUT_LT_OFF,
.rf_cal_on_startup = 1
};
static struct s5h1411_config hauppauge_s5h1411_config = {
.output_mode = S5H1411_SERIAL_OUTPUT,
.gpio = S5H1411_GPIO_ON,
.qam_if = S5H1411_IF_4000,
.vsb_if = S5H1411_IF_3250,
.inversion = S5H1411_INVERSION_ON,
.status_mode = S5H1411_DEMODLOCKING,
.mpeg_timing = S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
};
static struct lgdt3306a_config hauppauge_hvr2255a_config = {
.i2c_addr = 0xb2 >> 1,
.qam_if_khz = 4000,
.vsb_if_khz = 3250,
.deny_i2c_rptr = 1, /* Disabled */
.spectral_inversion = 0, /* Disabled */
.mpeg_mode = LGDT3306A_MPEG_SERIAL,
.tpclk_edge = LGDT3306A_TPCLK_RISING_EDGE,
.tpvalid_polarity = LGDT3306A_TP_VALID_HIGH,
.xtalMHz = 25, /* 24 or 25 */
};
static struct lgdt3306a_config hauppauge_hvr2255b_config = {
.i2c_addr = 0x1c >> 1,
.qam_if_khz = 4000,
.vsb_if_khz = 3250,
.deny_i2c_rptr = 1, /* Disabled */
.spectral_inversion = 0, /* Disabled */
.mpeg_mode = LGDT3306A_MPEG_SERIAL,
.tpclk_edge = LGDT3306A_TPCLK_RISING_EDGE,
.tpvalid_polarity = LGDT3306A_TP_VALID_HIGH,
.xtalMHz = 25, /* 24 or 25 */
};
static struct si2157_config hauppauge_hvr2255_tuner_config = {
.inversion = 1,
.if_port = 1,
};
static int si2157_attach(struct saa7164_port *port, struct i2c_adapter *adapter,
struct dvb_frontend *fe, u8 addr8bit, struct si2157_config *cfg)
{
struct i2c_board_info bi;
struct i2c_client *tuner;
cfg->fe = fe;
memset(&bi, 0, sizeof(bi));
strscpy(bi.type, "si2157", I2C_NAME_SIZE);
bi.platform_data = cfg;
bi.addr = addr8bit >> 1;
request_module(bi.type);
tuner = i2c_new_client_device(adapter, &bi);
if (!i2c_client_has_driver(tuner))
return -ENODEV;
if (!try_module_get(tuner->dev.driver->owner)) {
i2c_unregister_device(tuner);
return -ENODEV;
}
port->i2c_client_tuner = tuner;
return 0;
}
static int saa7164_dvb_stop_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_DVB, "%s() Stopped\n", __func__);
ret = 0;
}
return ret;
}
static int saa7164_dvb_acquire_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
ret = 0;
}
return ret;
}
static int saa7164_dvb_pause_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
__func__, ret);
ret = -EIO;
} else {
dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
ret = 0;
}
return ret;
}
/* Firmware is very windows centric, meaning you have to transition
* the part through AVStream / KS Windows stages, forwards or backwards.
* States are: stopped, acquired (h/w), paused, started.
*/
static int saa7164_dvb_stop_streaming(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct list_head *p, *q;
int ret;
dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
ret = saa7164_dvb_pause_port(port);
ret = saa7164_dvb_acquire_port(port);
ret = saa7164_dvb_stop_port(port);
/* Mark the hardware buffers as free */
mutex_lock(&port->dmaqueue_lock);
list_for_each_safe(p, q, &port->dmaqueue.list) {
buf = list_entry(p, struct saa7164_buffer, list);
buf->flags = SAA7164_BUFFER_FREE;
}
mutex_unlock(&port->dmaqueue_lock);
return ret;
}
static int saa7164_dvb_start_port(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret = 0, result;
dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
saa7164_buffer_cfg_port(port);
/* Acquire the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() acquire/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
goto out;
} else
dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
/* Pause the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
goto out;
} else
dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
/* Start the hardware */
result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
__func__, result);
/* Stop the hardware, regardless */
result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
__func__, result);
}
ret = -EIO;
} else
dprintk(DBGLVL_DVB, "%s() Running\n", __func__);
out:
return ret;
}
static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed)
{
struct dvb_demux *demux = feed->demux;
struct saa7164_port *port = demux->priv;
struct saa7164_dvb *dvb = &port->dvb;
struct saa7164_dev *dev = port->dev;
int ret = 0;
dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
if (!demux->dmx.frontend)
return -EINVAL;
if (dvb) {
mutex_lock(&dvb->lock);
if (dvb->feeding++ == 0) {
/* Start transport */
ret = saa7164_dvb_start_port(port);
}
mutex_unlock(&dvb->lock);
dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
__func__, port->nr, dvb->feeding);
}
return ret;
}
static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed)
{
struct dvb_demux *demux = feed->demux;
struct saa7164_port *port = demux->priv;
struct saa7164_dvb *dvb = &port->dvb;
struct saa7164_dev *dev = port->dev;
int ret = 0;
dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
if (dvb) {
mutex_lock(&dvb->lock);
if (--dvb->feeding == 0) {
/* Stop transport */
ret = saa7164_dvb_stop_streaming(port);
}
mutex_unlock(&dvb->lock);
dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
__func__, port->nr, dvb->feeding);
}
return ret;
}
static int dvb_register(struct saa7164_port *port)
{
struct saa7164_dvb *dvb = &port->dvb;
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
int result, i;
dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
BUG_ON(port->type != SAA7164_MPEG_DVB);
/* Sanity check that the PCI configuration space is active */
if (port->hwcfg.BARLocation == 0) {
result = -ENOMEM;
printk(KERN_ERR "%s: dvb_register_adapter failed (errno = %d), NO PCI configuration\n",
DRIVER_NAME, result);
goto fail_adapter;
}
/* Init and establish defaults */
port->hw_streamingparams.bitspersample = 8;
port->hw_streamingparams.samplesperline = 188;
port->hw_streamingparams.numberoflines =
(SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
port->hw_streamingparams.pitch = 188;
port->hw_streamingparams.linethreshold = 0;
port->hw_streamingparams.pagetablelistvirt = NULL;
port->hw_streamingparams.pagetablelistphys = NULL;
port->hw_streamingparams.numpagetables = 2 +
((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
/* Allocate the PCI resources */
for (i = 0; i < port->hwcfg.buffercount; i++) {
buf = saa7164_buffer_alloc(port,
port->hw_streamingparams.numberoflines *
port->hw_streamingparams.pitch);
if (!buf) {
result = -ENOMEM;
printk(KERN_ERR "%s: dvb_register_adapter failed (errno = %d), unable to allocate buffers\n",
DRIVER_NAME, result);
goto fail_adapter;
}
mutex_lock(&port->dmaqueue_lock);
list_add_tail(&buf->list, &port->dmaqueue.list);
mutex_unlock(&port->dmaqueue_lock);
}
/* register adapter */
result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
&dev->pci->dev, adapter_nr);
if (result < 0) {
printk(KERN_ERR "%s: dvb_register_adapter failed (errno = %d)\n",
DRIVER_NAME, result);
goto fail_adapter;
}
dvb->adapter.priv = port;
/* register frontend */
result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
if (result < 0) {
printk(KERN_ERR "%s: dvb_register_frontend failed (errno = %d)\n",
DRIVER_NAME, result);
goto fail_frontend;
}
/* register demux stuff */
dvb->demux.dmx.capabilities =
DMX_TS_FILTERING | DMX_SECTION_FILTERING |
DMX_MEMORY_BASED_FILTERING;
dvb->demux.priv = port;
dvb->demux.filternum = 256;
dvb->demux.feednum = 256;
dvb->demux.start_feed = saa7164_dvb_start_feed;
dvb->demux.stop_feed = saa7164_dvb_stop_feed;
result = dvb_dmx_init(&dvb->demux);
if (result < 0) {
printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
DRIVER_NAME, result);
goto fail_dmx;
}
dvb->dmxdev.filternum = 256;
dvb->dmxdev.demux = &dvb->demux.dmx;
dvb->dmxdev.capabilities = 0;
result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
if (result < 0) {
printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
DRIVER_NAME, result);
goto fail_dmxdev;
}
dvb->fe_hw.source = DMX_FRONTEND_0;
result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
if (result < 0) {
printk(KERN_ERR "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
DRIVER_NAME, result);
goto fail_fe_hw;
}
dvb->fe_mem.source = DMX_MEMORY_FE;
result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
if (result < 0) {
printk(KERN_ERR "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
DRIVER_NAME, result);
goto fail_fe_mem;
}
result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
if (result < 0) {
printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
DRIVER_NAME, result);
goto fail_fe_conn;
}
/* register network adapter */
dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
return 0;
fail_fe_conn:
dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
fail_fe_mem:
dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
fail_fe_hw:
dvb_dmxdev_release(&dvb->dmxdev);
fail_dmxdev:
dvb_dmx_release(&dvb->demux);
fail_dmx:
dvb_unregister_frontend(dvb->frontend);
fail_frontend:
dvb_frontend_detach(dvb->frontend);
dvb_unregister_adapter(&dvb->adapter);
fail_adapter:
return result;
}
int saa7164_dvb_unregister(struct saa7164_port *port)
{
struct saa7164_dvb *dvb = &port->dvb;
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *b;
struct list_head *c, *n;
struct i2c_client *client;
dprintk(DBGLVL_DVB, "%s()\n", __func__);
BUG_ON(port->type != SAA7164_MPEG_DVB);
/* Remove any allocated buffers */
mutex_lock(&port->dmaqueue_lock);
list_for_each_safe(c, n, &port->dmaqueue.list) {
b = list_entry(c, struct saa7164_buffer, list);
list_del(c);
saa7164_buffer_dealloc(b);
}
mutex_unlock(&port->dmaqueue_lock);
if (dvb->frontend == NULL)
return 0;
/* remove I2C client for tuner */
client = port->i2c_client_tuner;
if (client) {
module_put(client->dev.driver->owner);
i2c_unregister_device(client);
}
/* remove I2C client for demodulator */
client = port->i2c_client_demod;
if (client) {
module_put(client->dev.driver->owner);
i2c_unregister_device(client);
}
dvb_net_release(&dvb->net);
dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
dvb_dmxdev_release(&dvb->dmxdev);
dvb_dmx_release(&dvb->demux);
dvb_unregister_frontend(dvb->frontend);
dvb_frontend_detach(dvb->frontend);
dvb_unregister_adapter(&dvb->adapter);
return 0;
}
/* All the DVB attach calls go here, this function gets modified
* for each new card.
*/
int saa7164_dvb_register(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_dvb *dvb = &port->dvb;
struct saa7164_i2c *i2c_bus = NULL;
struct si2168_config si2168_config;
struct si2157_config si2157_config;
struct i2c_adapter *adapter;
struct i2c_board_info info;
struct i2c_client *client_demod;
struct i2c_client *client_tuner;
int ret;
dprintk(DBGLVL_DVB, "%s()\n", __func__);
/* init frontend */
switch (dev->board) {
case SAA7164_BOARD_HAUPPAUGE_HVR2200:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
i2c_bus = &dev->i2c_bus[port->nr + 1];
switch (port->nr) {
case 0:
port->dvb.frontend = dvb_attach(tda10048_attach,
&hauppauge_hvr2200_1_config,
&i2c_bus->i2c_adap);
if (port->dvb.frontend != NULL) {
/* TODO: addr is in the card struct */
dvb_attach(tda18271_attach, port->dvb.frontend,
0xc0 >> 1, &i2c_bus->i2c_adap,
&hauppauge_hvr22x0_tuner_config);
}
break;
case 1:
port->dvb.frontend = dvb_attach(tda10048_attach,
&hauppauge_hvr2200_2_config,
&i2c_bus->i2c_adap);
if (port->dvb.frontend != NULL) {
/* TODO: addr is in the card struct */
dvb_attach(tda18271_attach, port->dvb.frontend,
0xc0 >> 1, &i2c_bus->i2c_adap,
&hauppauge_hvr22x0s_tuner_config);
}
break;
}
break;
case SAA7164_BOARD_HAUPPAUGE_HVR2250:
case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
i2c_bus = &dev->i2c_bus[port->nr + 1];
port->dvb.frontend = dvb_attach(s5h1411_attach,
&hauppauge_s5h1411_config,
&i2c_bus->i2c_adap);
if (port->dvb.frontend != NULL) {
if (port->nr == 0) {
/* Master TDA18271 */
/* TODO: addr is in the card struct */
dvb_attach(tda18271_attach, port->dvb.frontend,
0xc0 >> 1, &i2c_bus->i2c_adap,
&hauppauge_hvr22x0_tuner_config);
} else {
/* Slave TDA18271 */
dvb_attach(tda18271_attach, port->dvb.frontend,
0xc0 >> 1, &i2c_bus->i2c_adap,
&hauppauge_hvr22x0s_tuner_config);
}
}
break;
case SAA7164_BOARD_HAUPPAUGE_HVR2255proto:
case SAA7164_BOARD_HAUPPAUGE_HVR2255:
i2c_bus = &dev->i2c_bus[2];
if (port->nr == 0) {
port->dvb.frontend = dvb_attach(lgdt3306a_attach,
&hauppauge_hvr2255a_config, &i2c_bus->i2c_adap);
} else {
port->dvb.frontend = dvb_attach(lgdt3306a_attach,
&hauppauge_hvr2255b_config, &i2c_bus->i2c_adap);
}
if (port->dvb.frontend != NULL) {
if (port->nr == 0) {
si2157_attach(port, &dev->i2c_bus[0].i2c_adap,
port->dvb.frontend, 0xc0,
&hauppauge_hvr2255_tuner_config);
} else {
si2157_attach(port, &dev->i2c_bus[1].i2c_adap,
port->dvb.frontend, 0xc0,
&hauppauge_hvr2255_tuner_config);
}
}
break;
case SAA7164_BOARD_HAUPPAUGE_HVR2205:
if (port->nr == 0) {
/* attach frontend */
memset(&si2168_config, 0, sizeof(si2168_config));
si2168_config.i2c_adapter = &adapter;
si2168_config.fe = &port->dvb.frontend;
si2168_config.ts_mode = SI2168_TS_SERIAL;
memset(&info, 0, sizeof(struct i2c_board_info));
strscpy(info.type, "si2168", I2C_NAME_SIZE);
info.addr = 0xc8 >> 1;
info.platform_data = &si2168_config;
request_module(info.type);
client_demod = i2c_new_client_device(&dev->i2c_bus[2].i2c_adap, &info);
if (!i2c_client_has_driver(client_demod))
goto frontend_detach;
if (!try_module_get(client_demod->dev.driver->owner)) {
i2c_unregister_device(client_demod);
goto frontend_detach;
}
port->i2c_client_demod = client_demod;
/* attach tuner */
memset(&si2157_config, 0, sizeof(si2157_config));
si2157_config.if_port = 1;
si2157_config.fe = port->dvb.frontend;
memset(&info, 0, sizeof(struct i2c_board_info));
strscpy(info.type, "si2157", I2C_NAME_SIZE);
info.addr = 0xc0 >> 1;
info.platform_data = &si2157_config;
request_module(info.type);
client_tuner = i2c_new_client_device(&dev->i2c_bus[0].i2c_adap, &info);
if (!i2c_client_has_driver(client_tuner)) {
module_put(client_demod->dev.driver->owner);
i2c_unregister_device(client_demod);
goto frontend_detach;
}
if (!try_module_get(client_tuner->dev.driver->owner)) {
i2c_unregister_device(client_tuner);
module_put(client_demod->dev.driver->owner);
i2c_unregister_device(client_demod);
goto frontend_detach;
}
port->i2c_client_tuner = client_tuner;
} else {
/* attach frontend */
memset(&si2168_config, 0, sizeof(si2168_config));
si2168_config.i2c_adapter = &adapter;
si2168_config.fe = &port->dvb.frontend;
si2168_config.ts_mode = SI2168_TS_SERIAL;
memset(&info, 0, sizeof(struct i2c_board_info));
strscpy(info.type, "si2168", I2C_NAME_SIZE);
info.addr = 0xcc >> 1;
info.platform_data = &si2168_config;
request_module(info.type);
client_demod = i2c_new_client_device(&dev->i2c_bus[2].i2c_adap, &info);
if (!i2c_client_has_driver(client_demod))
goto frontend_detach;
if (!try_module_get(client_demod->dev.driver->owner)) {
i2c_unregister_device(client_demod);
goto frontend_detach;
}
port->i2c_client_demod = client_demod;
/* attach tuner */
memset(&si2157_config, 0, sizeof(si2157_config));
si2157_config.fe = port->dvb.frontend;
si2157_config.if_port = 1;
memset(&info, 0, sizeof(struct i2c_board_info));
strscpy(info.type, "si2157", I2C_NAME_SIZE);
info.addr = 0xc0 >> 1;
info.platform_data = &si2157_config;
request_module(info.type);
client_tuner = i2c_new_client_device(&dev->i2c_bus[1].i2c_adap, &info);
if (!i2c_client_has_driver(client_tuner)) {
module_put(client_demod->dev.driver->owner);
i2c_unregister_device(client_demod);
goto frontend_detach;
}
if (!try_module_get(client_tuner->dev.driver->owner)) {
i2c_unregister_device(client_tuner);
module_put(client_demod->dev.driver->owner);
i2c_unregister_device(client_demod);
goto frontend_detach;
}
port->i2c_client_tuner = client_tuner;
}
break;
default:
printk(KERN_ERR "%s: The frontend isn't supported\n",
dev->name);
break;
}
if (NULL == dvb->frontend) {
printk(KERN_ERR "%s() Frontend initialization failed\n",
__func__);
return -1;
}
/* register everything */
ret = dvb_register(port);
if (ret < 0) {
if (dvb->frontend->ops.release)
dvb->frontend->ops.release(dvb->frontend);
return ret;
}
return 0;
frontend_detach:
printk(KERN_ERR "%s() Frontend/I2C initialization failed\n", __func__);
return -1;
}
| linux-master | drivers/media/pci/saa7164/saa7164-dvb.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include "saa7164.h"
/* The Bridge API needs to understand register widths (in bytes) for the
* attached I2C devices, so we can simplify the virtual i2c mechansms
* and keep the -i2c.c implementation clean.
*/
#define REGLEN_0bit 0
#define REGLEN_8bit 1
#define REGLEN_16bit 2
struct saa7164_board saa7164_boards[] = {
[SAA7164_BOARD_UNKNOWN] = {
/* Bridge will not load any firmware, without knowing
* the rev this would be fatal. */
.name = "Unknown",
},
[SAA7164_BOARD_UNKNOWN_REV2] = {
/* Bridge will load the v2 f/w and dump descriptors */
/* Required during new board bringup */
.name = "Generic Rev2",
.chiprev = SAA7164_CHIP_REV2,
},
[SAA7164_BOARD_UNKNOWN_REV3] = {
/* Bridge will load the v2 f/w and dump descriptors */
/* Required during new board bringup */
.name = "Generic Rev3",
.chiprev = SAA7164_CHIP_REV3,
},
[SAA7164_BOARD_HAUPPAUGE_HVR2200] = {
.name = "Hauppauge WinTV-HVR2200",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x1d,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1b,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1e,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x10 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1f,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x12 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2200_2] = {
.name = "Hauppauge WinTV-HVR2200",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV2,
.unit = {{
.id = 0x06,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x05,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x10 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1e,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1f,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x12 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2200_3] = {
.name = "Hauppauge WinTV-HVR2200",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV2,
.unit = {{
.id = 0x1d,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x05,
.type = SAA7164_UNIT_ANALOG_DEMODULATOR,
.name = "TDA8290-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x84 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1b,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1c,
.type = SAA7164_UNIT_ANALOG_DEMODULATOR,
.name = "TDA8290-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x84 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1e,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x10 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1f,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x12 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2200_4] = {
.name = "Hauppauge WinTV-HVR2200",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x1d,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x05,
.type = SAA7164_UNIT_ANALOG_DEMODULATOR,
.name = "TDA8290-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x84 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1b,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1c,
.type = SAA7164_UNIT_ANALOG_DEMODULATOR,
.name = "TDA8290-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x84 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1e,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x10 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1f,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x12 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2250] = {
.name = "Hauppauge WinTV-HVR2250",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x22,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x07,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-1 (TOP)",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x32 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x08,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-1 (QAM)",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x34 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x1e,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x20,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-2 (TOP)",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x32 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x23,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-2 (QAM)",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x34 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2250_2] = {
.name = "Hauppauge WinTV-HVR2250",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x28,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x07,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-1 (TOP)",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x32 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x08,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-1 (QAM)",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x34 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x24,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x26,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-2 (TOP)",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x32 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x29,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-2 (QAM)",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x34 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2250_3] = {
.name = "Hauppauge WinTV-HVR2250",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x26,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x07,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-1 (TOP)",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x32 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x08,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-1 (QAM)",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x34 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x22,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x24,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-2 (TOP)",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x32 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x27,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "CX24228/S5H1411-2 (QAM)",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x34 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2200_5] = {
.name = "Hauppauge WinTV-HVR2200",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x23,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x05,
.type = SAA7164_UNIT_ANALOG_DEMODULATOR,
.name = "TDA8290-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x84 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x21,
.type = SAA7164_UNIT_TUNER,
.name = "TDA18271-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x22,
.type = SAA7164_UNIT_ANALOG_DEMODULATOR,
.name = "TDA8290-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x84 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x24,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-1",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0x10 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x25,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "TDA10048-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x12 >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2255proto] = {
.name = "Hauppauge WinTV-HVR2255(proto)",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x27,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "SI2157-1",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x06,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "LGDT3306",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xb2 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x24,
.type = SAA7164_UNIT_TUNER,
.name = "SI2157-2",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x26,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "LGDT3306-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x1c >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2255] = {
.name = "Hauppauge WinTV-HVR2255",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x28,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "SI2157-1",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x06,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "LGDT3306-1",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xb2 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x25,
.type = SAA7164_UNIT_TUNER,
.name = "SI2157-2",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x27,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "LGDT3306-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0x1c >> 1,
.i2c_reg_len = REGLEN_8bit,
} },
},
[SAA7164_BOARD_HAUPPAUGE_HVR2205] = {
.name = "Hauppauge WinTV-HVR2205",
.porta = SAA7164_MPEG_DVB,
.portb = SAA7164_MPEG_DVB,
.portc = SAA7164_MPEG_ENCODER,
.portd = SAA7164_MPEG_ENCODER,
.porte = SAA7164_MPEG_VBI,
.portf = SAA7164_MPEG_VBI,
.chiprev = SAA7164_CHIP_REV3,
.unit = {{
.id = 0x28,
.type = SAA7164_UNIT_EEPROM,
.name = "4K EEPROM",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xa0 >> 1,
.i2c_reg_len = REGLEN_8bit,
}, {
.id = 0x04,
.type = SAA7164_UNIT_TUNER,
.name = "SI2157-1",
.i2c_bus_nr = SAA7164_I2C_BUS_0,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x06,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "SI2168-1",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xc8 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x25,
.type = SAA7164_UNIT_TUNER,
.name = "SI2157-2",
.i2c_bus_nr = SAA7164_I2C_BUS_1,
.i2c_bus_addr = 0xc0 >> 1,
.i2c_reg_len = REGLEN_0bit,
}, {
.id = 0x27,
.type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
.name = "SI2168-2",
.i2c_bus_nr = SAA7164_I2C_BUS_2,
.i2c_bus_addr = 0xcc >> 1,
.i2c_reg_len = REGLEN_0bit,
} },
},
};
const unsigned int saa7164_bcount = ARRAY_SIZE(saa7164_boards);
/* ------------------------------------------------------------------ */
/* PCI subsystem IDs */
struct saa7164_subid saa7164_subids[] = {
{
.subvendor = 0x0070,
.subdevice = 0x8880,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2250,
}, {
.subvendor = 0x0070,
.subdevice = 0x8810,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2250,
}, {
.subvendor = 0x0070,
.subdevice = 0x8980,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2200,
}, {
.subvendor = 0x0070,
.subdevice = 0x8900,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2200_2,
}, {
.subvendor = 0x0070,
.subdevice = 0x8901,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2200_3,
}, {
.subvendor = 0x0070,
.subdevice = 0x88A1,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2250_3,
}, {
.subvendor = 0x0070,
.subdevice = 0x8891,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2250_2,
}, {
.subvendor = 0x0070,
.subdevice = 0x8851,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2250_2,
}, {
.subvendor = 0x0070,
.subdevice = 0x8940,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2200_4,
}, {
.subvendor = 0x0070,
.subdevice = 0x8953,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2200_5,
}, {
.subvendor = 0x0070,
.subdevice = 0xf111,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2255,
/* Prototype card left here for documentation purposes.
.card = SAA7164_BOARD_HAUPPAUGE_HVR2255proto,
*/
}, {
.subvendor = 0x0070,
.subdevice = 0xf123,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2205,
}, {
.subvendor = 0x0070,
.subdevice = 0xf120,
.card = SAA7164_BOARD_HAUPPAUGE_HVR2205,
},
};
const unsigned int saa7164_idcount = ARRAY_SIZE(saa7164_subids);
void saa7164_card_list(struct saa7164_dev *dev)
{
int i;
if (0 == dev->pci->subsystem_vendor &&
0 == dev->pci->subsystem_device) {
printk(KERN_ERR
"%s: Board has no valid PCIe Subsystem ID and can't\n"
"%s: be autodetected. Pass card=<n> insmod option to\n"
"%s: workaround that. Send complaints to the vendor\n"
"%s: of the TV card. Best regards,\n"
"%s: -- tux\n",
dev->name, dev->name, dev->name, dev->name, dev->name);
} else {
printk(KERN_ERR
"%s: Your board isn't known (yet) to the driver.\n"
"%s: Try to pick one of the existing card configs via\n"
"%s: card=<n> insmod option. Updating to the latest\n"
"%s: version might help as well.\n",
dev->name, dev->name, dev->name, dev->name);
}
printk(KERN_ERR "%s: Here are valid choices for the card=<n> insmod option:\n",
dev->name);
for (i = 0; i < saa7164_bcount; i++)
printk(KERN_ERR "%s: card=%d -> %s\n",
dev->name, i, saa7164_boards[i].name);
}
/* TODO: clean this define up into the -cards.c structs */
#define PCIEBRIDGE_UNITID 2
void saa7164_gpio_setup(struct saa7164_dev *dev)
{
switch (dev->board) {
case SAA7164_BOARD_HAUPPAUGE_HVR2200:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
case SAA7164_BOARD_HAUPPAUGE_HVR2250:
case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
case SAA7164_BOARD_HAUPPAUGE_HVR2255proto:
case SAA7164_BOARD_HAUPPAUGE_HVR2255:
case SAA7164_BOARD_HAUPPAUGE_HVR2205:
/*
HVR2200 / HVR2250
GPIO 2: s5h1411 / tda10048-1 demod reset
GPIO 3: s5h1411 / tda10048-2 demod reset
GPIO 7: IRBlaster Zilog reset
*/
/* HVR2255
* GPIO 2: lgdg3306-1 demod reset
* GPIO 3: lgdt3306-2 demod reset
*/
/* HVR2205
* GPIO 2: si2168-1 demod reset
* GPIO 3: si2168-2 demod reset
*/
/* Reset parts by going in and out of reset */
saa7164_api_clear_gpiobit(dev, PCIEBRIDGE_UNITID, 2);
saa7164_api_clear_gpiobit(dev, PCIEBRIDGE_UNITID, 3);
msleep(20);
saa7164_api_set_gpiobit(dev, PCIEBRIDGE_UNITID, 2);
saa7164_api_set_gpiobit(dev, PCIEBRIDGE_UNITID, 3);
break;
}
}
static void hauppauge_eeprom(struct saa7164_dev *dev, u8 *eeprom_data)
{
struct tveeprom tv;
tveeprom_hauppauge_analog(&tv, eeprom_data);
/* Make sure we support the board model */
switch (tv.model) {
case 88001:
/* Development board - Limit circulation */
/* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
* ATSC/QAM (TDA18271/S5H1411) and basic analog, no IR, FM */
case 88021:
/* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
* ATSC/QAM (TDA18271/S5H1411) and basic analog, MCE CIR, FM */
break;
case 88041:
/* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
* ATSC/QAM (TDA18271/S5H1411) and basic analog, no IR, FM */
break;
case 88061:
/* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
* ATSC/QAM (TDA18271/S5H1411) and basic analog, FM */
break;
case 89519:
case 89609:
/* WinTV-HVR2200 (PCIe, Retail, full-height)
* DVB-T (TDA18271/TDA10048) and basic analog, no IR */
break;
case 89619:
/* WinTV-HVR2200 (PCIe, Retail, half-height)
* DVB-T (TDA18271/TDA10048) and basic analog, no IR */
break;
case 151009:
/* First production board rev B2I6 */
/* WinTV-HVR2205 (PCIe, Retail, full-height bracket)
* DVB-T/T2/C (SI2157/SI2168) and basic analog, FM */
break;
case 151609:
/* First production board rev B2I6 */
/* WinTV-HVR2205 (PCIe, Retail, half-height bracket)
* DVB-T/T2/C (SI2157/SI2168) and basic analog, FM */
break;
case 151061:
/* First production board rev B1I6 */
/* WinTV-HVR2255 (PCIe, Retail, full-height bracket)
* ATSC/QAM (SI2157/LGDT3306) and basic analog, FM */
break;
default:
printk(KERN_ERR "%s: Warning: Unknown Hauppauge model #%d\n",
dev->name, tv.model);
break;
}
printk(KERN_INFO "%s: Hauppauge eeprom: model=%d\n", dev->name,
tv.model);
}
void saa7164_card_setup(struct saa7164_dev *dev)
{
static u8 eeprom[256];
if (dev->i2c_bus[0].i2c_rc == 0) {
if (saa7164_api_read_eeprom(dev, &eeprom[0],
sizeof(eeprom)) < 0)
return;
}
switch (dev->board) {
case SAA7164_BOARD_HAUPPAUGE_HVR2200:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
case SAA7164_BOARD_HAUPPAUGE_HVR2250:
case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
case SAA7164_BOARD_HAUPPAUGE_HVR2255proto:
case SAA7164_BOARD_HAUPPAUGE_HVR2255:
case SAA7164_BOARD_HAUPPAUGE_HVR2205:
hauppauge_eeprom(dev, &eeprom[0]);
break;
}
}
/* With most other drivers, the kernel expects to communicate with subdrivers
* through i2c. This bridge does not allow that, it does not expose any direct
* access to I2C. Instead we have to communicate through the device f/w for
* register access to 'processing units'. Each unit has a unique
* id, regardless of how the physical implementation occurs across
* the three physical i2c buses. The being said if we want leverge of
* the existing kernel drivers for tuners and demods we have to 'speak i2c',
* to this bridge implements 3 virtual i2c buses. This is a helper function
* for those.
*
* Description: Translate the kernels notion of an i2c address and bus into
* the appropriate unitid.
*/
int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr)
{
/* For a given bus and i2c device address, return the saa7164 unique
* unitid. < 0 on error */
struct saa7164_dev *dev = bus->dev;
struct saa7164_unit *unit;
int i;
for (i = 0; i < SAA7164_MAX_UNITS; i++) {
unit = &saa7164_boards[dev->board].unit[i];
if (unit->type == SAA7164_UNIT_UNDEFINED)
continue;
if ((bus->nr == unit->i2c_bus_nr) &&
(addr == unit->i2c_bus_addr))
return unit->id;
}
return -1;
}
/* The 7164 API needs to know the i2c register length in advance.
* this is a helper function. Based on a specific chip addr and bus return the
* reg length.
*/
int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr)
{
/* For a given bus and i2c device address, return the
* saa7164 registry address width. < 0 on error
*/
struct saa7164_dev *dev = bus->dev;
struct saa7164_unit *unit;
int i;
for (i = 0; i < SAA7164_MAX_UNITS; i++) {
unit = &saa7164_boards[dev->board].unit[i];
if (unit->type == SAA7164_UNIT_UNDEFINED)
continue;
if ((bus->nr == unit->i2c_bus_nr) &&
(addr == unit->i2c_bus_addr))
return unit->i2c_reg_len;
}
return -1;
}
/* TODO: implement a 'findeeprom' functio like the above and fix any other
* eeprom related todo's in -api.c.
*/
/* Translate a unitid into a x readable device name, for display purposes. */
char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid)
{
char *undefed = "UNDEFINED";
char *bridge = "BRIDGE";
struct saa7164_unit *unit;
int i;
if (unitid == 0)
return bridge;
for (i = 0; i < SAA7164_MAX_UNITS; i++) {
unit = &saa7164_boards[dev->board].unit[i];
if (unit->type == SAA7164_UNIT_UNDEFINED)
continue;
if (unitid == unit->id)
return unit->name;
}
return undefed;
}
| linux-master | drivers/media/pci/saa7164/saa7164-cards.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/wait.h>
#include <linux/slab.h>
#include "saa7164.h"
int saa7164_api_get_load_info(struct saa7164_dev *dev, struct tmFwInfoStruct *i)
{
int ret;
if (!(saa_debug & DBGLVL_CPU))
return 0;
dprintk(DBGLVL_API, "%s()\n", __func__);
i->deviceinst = 0;
i->devicespec = 0;
i->mode = 0;
i->status = 0;
ret = saa7164_cmd_send(dev, 0, GET_CUR,
GET_FW_STATUS_CONTROL, sizeof(struct tmFwInfoStruct), i);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
printk(KERN_INFO "saa7164[%d]-CPU: %d percent", dev->nr, i->CPULoad);
return ret;
}
int saa7164_api_collect_debug(struct saa7164_dev *dev)
{
struct tmComResDebugGetData d;
u8 more = 255;
int ret;
dprintk(DBGLVL_API, "%s()\n", __func__);
while (more--) {
memset(&d, 0, sizeof(d));
ret = saa7164_cmd_send(dev, 0, GET_CUR,
GET_DEBUG_DATA_CONTROL, sizeof(d), &d);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n",
__func__, ret);
if (d.dwResult != SAA_OK)
break;
printk(KERN_INFO "saa7164[%d]-FWMSG: %s", dev->nr,
d.ucDebugData);
}
return 0;
}
int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level)
{
struct tmComResDebugSetLevel lvl;
int ret;
dprintk(DBGLVL_API, "%s(level=%d)\n", __func__, level);
/* Retrieve current state */
ret = saa7164_cmd_send(dev, 0, GET_CUR,
SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
dprintk(DBGLVL_API, "%s() Was %d\n", __func__, lvl.dwDebugLevel);
lvl.dwDebugLevel = level;
/* set new state */
ret = saa7164_cmd_send(dev, 0, SET_CUR,
SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
int saa7164_api_set_vbi_format(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct tmComResProbeCommit fmt, rsp;
int ret;
dprintk(DBGLVL_API, "%s(nr=%d, unitid=0x%x)\n", __func__,
port->nr, port->hwcfg.unitid);
fmt.bmHint = 0;
fmt.bFormatIndex = 1;
fmt.bFrameIndex = 1;
/* Probe, see if it can support this format */
ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
SET_CUR, SAA_PROBE_CONTROL, sizeof(fmt), &fmt);
if (ret != SAA_OK)
printk(KERN_ERR "%s() set error, ret = 0x%x\n", __func__, ret);
/* See of the format change was successful */
ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
GET_CUR, SAA_PROBE_CONTROL, sizeof(rsp), &rsp);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() get error, ret = 0x%x\n", __func__, ret);
} else {
/* Compare requested vs received, should be same */
if (memcmp(&fmt, &rsp, sizeof(rsp)) == 0) {
dprintk(DBGLVL_API, "SET/PROBE Verified\n");
/* Ask the device to select the negotiated format */
ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
SET_CUR, SAA_COMMIT_CONTROL, sizeof(fmt), &fmt);
if (ret != SAA_OK)
printk(KERN_ERR "%s() commit error, ret = 0x%x\n",
__func__, ret);
ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
GET_CUR, SAA_COMMIT_CONTROL, sizeof(rsp), &rsp);
if (ret != SAA_OK)
printk(KERN_ERR "%s() GET commit error, ret = 0x%x\n",
__func__, ret);
if (memcmp(&fmt, &rsp, sizeof(rsp)) != 0) {
printk(KERN_ERR "%s() memcmp error, ret = 0x%x\n",
__func__, ret);
} else
dprintk(DBGLVL_API, "SET/COMMIT Verified\n");
dprintk(DBGLVL_API, "rsp.bmHint = 0x%x\n", rsp.bmHint);
dprintk(DBGLVL_API, "rsp.bFormatIndex = 0x%x\n",
rsp.bFormatIndex);
dprintk(DBGLVL_API, "rsp.bFrameIndex = 0x%x\n",
rsp.bFrameIndex);
} else
printk(KERN_ERR "%s() compare failed\n", __func__);
}
if (ret == SAA_OK)
dprintk(DBGLVL_API, "%s(nr=%d) Success\n", __func__, port->nr);
return ret;
}
static int saa7164_api_set_gop_size(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct tmComResEncVideoGopStructure gs;
int ret;
dprintk(DBGLVL_ENC, "%s()\n", __func__);
gs.ucRefFrameDist = port->encoder_params.refdist;
gs.ucGOPSize = port->encoder_params.gop_size;
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
EU_VIDEO_GOP_STRUCTURE_CONTROL,
sizeof(gs), &gs);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
int saa7164_api_set_encoder(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct tmComResEncVideoBitRate vb;
struct tmComResEncAudioBitRate ab;
int ret;
dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
port->hwcfg.sourceid);
if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
port->encoder_profile = EU_PROFILE_PS_DVD;
else
port->encoder_profile = EU_PROFILE_TS_HQ;
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Resolution */
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Establish video bitrates */
if (port->encoder_params.bitrate_mode ==
V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
else
vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
vb.dwVideoBitRate = port->encoder_params.bitrate;
vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
EU_VIDEO_BIT_RATE_CONTROL,
sizeof(struct tmComResEncVideoBitRate),
&vb);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Establish audio bitrates */
ab.ucAudioBitRateMode = 0;
ab.dwAudioBitRate = 384000;
ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
EU_AUDIO_BIT_RATE_CONTROL,
sizeof(struct tmComResEncAudioBitRate),
&ab);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
ret);
saa7164_api_set_aspect_ratio(port);
saa7164_api_set_gop_size(port);
return ret;
}
int saa7164_api_get_encoder(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct tmComResEncVideoBitRate v;
struct tmComResEncAudioBitRate a;
struct tmComResEncVideoInputAspectRatio ar;
int ret;
dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
port->hwcfg.sourceid);
port->encoder_profile = 0;
port->video_format = 0;
port->video_resolution = 0;
port->audio_format = 0;
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8),
&port->video_resolution);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Aspect Ratio */
ar.width = 0;
ar.height = 0;
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
EU_VIDEO_INPUT_ASPECT_CONTROL,
sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n",
v.ucVideoBitRateMode);
dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n",
v.dwVideoBitRate);
dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n",
v.dwVideoBitRatePeak);
dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n",
a.ucAudioBitRateMode);
dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n",
a.dwAudioBitRate);
dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n",
a.dwAudioBitRatePeak);
dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n",
ar.width, ar.height);
return ret;
}
int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct tmComResEncVideoInputAspectRatio ar;
int ret;
dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
port->encoder_params.ctl_aspect);
switch (port->encoder_params.ctl_aspect) {
case V4L2_MPEG_VIDEO_ASPECT_1x1:
ar.width = 1;
ar.height = 1;
break;
case V4L2_MPEG_VIDEO_ASPECT_4x3:
ar.width = 4;
ar.height = 3;
break;
case V4L2_MPEG_VIDEO_ASPECT_16x9:
ar.width = 16;
ar.height = 9;
break;
case V4L2_MPEG_VIDEO_ASPECT_221x100:
ar.width = 221;
ar.height = 100;
break;
default:
BUG();
}
dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
port->encoder_params.ctl_aspect,
ar.width, ar.height);
/* Aspect Ratio */
ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
EU_VIDEO_INPUT_ASPECT_CONTROL,
sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
{
struct saa7164_dev *dev = port->dev;
int ret;
u16 val;
if (ctl == PU_BRIGHTNESS_CONTROL)
val = port->ctl_brightness;
else
if (ctl == PU_CONTRAST_CONTROL)
val = port->ctl_contrast;
else
if (ctl == PU_HUE_CONTROL)
val = port->ctl_hue;
else
if (ctl == PU_SATURATION_CONTROL)
val = port->ctl_saturation;
else
if (ctl == PU_SHARPNESS_CONTROL)
val = port->ctl_sharpness;
else
return -EINVAL;
dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
__func__, port->encunit.vsourceid, ctl, val);
ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
ctl, sizeof(u16), &val);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
{
struct saa7164_dev *dev = port->dev;
int ret;
u16 val;
ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
ctl, sizeof(u16), &val);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
__func__, ctl, val);
if (ctl == PU_BRIGHTNESS_CONTROL)
port->ctl_brightness = val;
else
if (ctl == PU_CONTRAST_CONTROL)
port->ctl_contrast = val;
else
if (ctl == PU_HUE_CONTROL)
port->ctl_hue = val;
else
if (ctl == PU_SATURATION_CONTROL)
port->ctl_saturation = val;
else
if (ctl == PU_SHARPNESS_CONTROL)
port->ctl_sharpness = val;
return ret;
}
int saa7164_api_set_videomux(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
int ret;
dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
__func__, port->mux_input, inputs[port->mux_input - 1]);
/* Audio Mute */
ret = saa7164_api_audio_mute(port, 1);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Video Mux */
ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Audio Mux */
ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
SU_INPUT_SELECT_CONTROL, sizeof(u8),
&inputs[port->mux_input - 1]);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Audio UnMute */
ret = saa7164_api_audio_mute(port, 0);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
{
struct saa7164_dev *dev = port->dev;
u8 v = mute;
int ret;
dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
MUTE_CONTROL, sizeof(u8), &v);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
/* 0 = silence, 0xff = full */
int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
{
struct saa7164_dev *dev = port->dev;
s16 v, min, max;
int ret;
dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
/* Obtain the min/max ranges */
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
VOLUME_CONTROL, sizeof(u16), &min);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
VOLUME_CONTROL, sizeof(u16), &max);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
(0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__,
level, min, max, v);
v = level;
if (v < min)
v = min;
if (v > max)
v = max;
/* Left */
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
(0x01 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Right */
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
(0x02 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
(0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__,
level, min, max, v);
return ret;
}
int saa7164_api_set_audio_std(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct tmComResAudioDefaults lvl;
struct tmComResTunerStandard tvaudio;
int ret;
dprintk(DBGLVL_API, "%s()\n", __func__);
/* Establish default levels */
lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
AUDIO_DEFAULT_CONTROL, sizeof(struct tmComResAudioDefaults),
&lvl);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
/* Manually select the appropriate TV audio standard */
if (port->encodernorm.id & V4L2_STD_NTSC) {
tvaudio.std = TU_STANDARD_NTSC_M;
tvaudio.country = 1;
} else {
tvaudio.std = TU_STANDARD_PAL_I;
tvaudio.country = 44;
}
ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
if (ret != SAA_OK)
printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n",
__func__, ret);
return ret;
}
int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
{
struct saa7164_dev *dev = port->dev;
struct tmComResTunerStandardAuto p;
int ret;
dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
/* Disable TV Audio autodetect if not already set (buggy) */
if (autodetect)
p.mode = TU_STANDARD_AUTO;
else
p.mode = TU_STANDARD_MANUAL;
ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
if (ret != SAA_OK)
printk(KERN_ERR
"%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n",
__func__, ret);
return ret;
}
int saa7164_api_get_videomux(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
int ret;
ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
__func__, port->mux_input);
return ret;
}
static int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
{
struct saa7164_dev *dev = port->dev;
u16 len = 0;
u8 buf[256];
int ret;
u8 mas;
dprintk(DBGLVL_API, "%s(nr=%d type=%d val=%x)\n", __func__,
port->nr, port->type, val);
if (port->nr == 0)
mas = 0xd0;
else
mas = 0xe0;
memset(buf, 0, sizeof(buf));
buf[0x00] = 0x04;
buf[0x01] = 0x00;
buf[0x02] = 0x00;
buf[0x03] = 0x00;
buf[0x04] = 0x04;
buf[0x05] = 0x00;
buf[0x06] = 0x00;
buf[0x07] = 0x00;
buf[0x08] = reg;
buf[0x09] = 0x26;
buf[0x0a] = mas;
buf[0x0b] = 0xb0;
buf[0x0c] = val;
buf[0x0d] = 0x00;
buf[0x0e] = 0x00;
buf[0x0f] = 0x00;
ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
return -EIO;
}
ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
EXU_REGISTER_ACCESS_CONTROL, len, &buf);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
#if 0
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf, 16,
false);
#endif
return ret == SAA_OK ? 0 : -EIO;
}
/* Disable the IF block AGC controls */
int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
{
struct saa7164_dev *dev = port->dev;
u8 agc_disable;
dprintk(DBGLVL_API, "%s(nr=%d, 0x%x)\n", __func__, port->nr, std);
if (std & V4L2_STD_NTSC) {
dprintk(DBGLVL_API, " NTSC\n");
saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_PAL_I) {
dprintk(DBGLVL_API, " PAL-I\n");
saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_PAL_M) {
dprintk(DBGLVL_API, " PAL-M\n");
saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_PAL_N) {
dprintk(DBGLVL_API, " PAL-N\n");
saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_PAL_Nc) {
dprintk(DBGLVL_API, " PAL-Nc\n");
saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_PAL_B) {
dprintk(DBGLVL_API, " PAL-B\n");
saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_PAL_DK) {
dprintk(DBGLVL_API, " PAL-DK\n");
saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
agc_disable = 0;
} else if (std & V4L2_STD_SECAM_L) {
dprintk(DBGLVL_API, " SECAM-L\n");
saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
agc_disable = 0;
} else {
/* Unknown standard, assume DTV */
dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
/* Undefinded Video Standard */
saa7164_api_set_dif(port, 0x00, 0x80);
agc_disable = 1;
}
saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
msleep(100);
saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
msleep(100);
return 0;
}
/* Ensure the dif is in the correct state for the operating mode
* (analog / dtv). We only configure the diff through the analog encoder
* so when we're in digital mode we need to find the appropriate encoder
* and use it to configure the DIF.
*/
int saa7164_api_initialize_dif(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_port *p = NULL;
int ret = -EINVAL;
u32 std = 0;
dprintk(DBGLVL_API, "%s(nr=%d type=%d)\n", __func__,
port->nr, port->type);
if (port->type == SAA7164_MPEG_ENCODER) {
/* Pick any analog standard to init the diff.
* we'll come back during encoder_init'
* and set the correct standard if required.
*/
std = V4L2_STD_NTSC;
} else
if (port->type == SAA7164_MPEG_DVB) {
if (port->nr == SAA7164_PORT_TS1)
p = &dev->ports[SAA7164_PORT_ENC1];
else
p = &dev->ports[SAA7164_PORT_ENC2];
} else
if (port->type == SAA7164_MPEG_VBI) {
std = V4L2_STD_NTSC;
if (port->nr == SAA7164_PORT_VBI1)
p = &dev->ports[SAA7164_PORT_ENC1];
else
p = &dev->ports[SAA7164_PORT_ENC2];
} else
BUG();
if (p)
ret = saa7164_api_configure_dif(p, std);
return ret;
}
int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
{
struct saa7164_dev *dev = port->dev;
int ret;
dprintk(DBGLVL_API, "%s(nr=%d unitid=0x%x,%d)\n",
__func__, port->nr, port->hwcfg.unitid, mode);
ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
SAA_STATE_CONTROL, sizeof(mode), &mode);
if (ret != SAA_OK)
printk(KERN_ERR "%s(portnr %d unitid 0x%x) error, ret = 0x%x\n",
__func__, port->nr, port->hwcfg.unitid, ret);
return ret;
}
int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
{
int ret;
ret = saa7164_cmd_send(dev, 0, GET_CUR,
GET_FW_VERSION_CONTROL, sizeof(u32), version);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
return ret;
}
int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
{
u8 reg[] = { 0x0f, 0x00 };
if (buflen < 128)
return -ENOMEM;
/* Assumption: Hauppauge eeprom is at 0xa0 on bus 0 */
/* TODO: Pull the details from the boards struct */
return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
®[0], 128, buf);
}
static int saa7164_api_configure_port_vbi(struct saa7164_dev *dev,
struct saa7164_port *port)
{
struct tmComResVBIFormatDescrHeader *fmt = &port->vbi_fmt_ntsc;
dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
dprintk(DBGLVL_API, " VideoStandard = 0x%x\n", fmt->VideoStandard);
dprintk(DBGLVL_API, " StartLine = %d\n", fmt->StartLine);
dprintk(DBGLVL_API, " EndLine = %d\n", fmt->EndLine);
dprintk(DBGLVL_API, " FieldRate = %d\n", fmt->FieldRate);
dprintk(DBGLVL_API, " bNumLines = %d\n", fmt->bNumLines);
/* Cache the hardware configuration in the port */
port->bufcounter = port->hwcfg.BARLocation;
port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
port->bufptr32l = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
port->bufptr32h = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount);
port->bufptr64 = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount);
dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
port->hwcfg.BARLocation);
dprintk(DBGLVL_API, " = VS_FORMAT_VBI (becomes dev->en[%d])\n",
port->nr);
return 0;
}
static int
saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
struct saa7164_port *port,
struct tmComResTSFormatDescrHeader *tsfmt)
{
dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
dprintk(DBGLVL_API, " bDataOffset = 0x%x\n", tsfmt->bDataOffset);
dprintk(DBGLVL_API, " bPacketLength= 0x%x\n", tsfmt->bPacketLength);
dprintk(DBGLVL_API, " bStrideLength= 0x%x\n", tsfmt->bStrideLength);
dprintk(DBGLVL_API, " bguid = (....)\n");
/* Cache the hardware configuration in the port */
port->bufcounter = port->hwcfg.BARLocation;
port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
port->bufptr32l = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
port->bufptr32h = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount);
port->bufptr64 = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount);
dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
port->hwcfg.BARLocation);
dprintk(DBGLVL_API, " = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
port->nr);
return 0;
}
static int
saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
struct saa7164_port *port,
struct tmComResPSFormatDescrHeader *fmt)
{
dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
/* Cache the hardware configuration in the port */
/* TODO: CHECK THIS in the port config */
port->bufcounter = port->hwcfg.BARLocation;
port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
port->bufptr32l = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
port->bufptr32h = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount);
port->bufptr64 = port->hwcfg.BARLocation +
(4 * sizeof(u32)) +
(sizeof(u32) * port->hwcfg.buffercount);
dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
port->hwcfg.BARLocation);
dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
port->nr);
return 0;
}
static int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
{
struct saa7164_port *tsport = NULL;
struct saa7164_port *encport = NULL;
struct saa7164_port *vbiport = NULL;
u32 idx, next_offset;
int i;
struct tmComResDescrHeader *hdr, *t;
struct tmComResExtDevDescrHeader *exthdr;
struct tmComResPathDescrHeader *pathhdr;
struct tmComResAntTermDescrHeader *anttermhdr;
struct tmComResTunerDescrHeader *tunerunithdr;
struct tmComResDMATermDescrHeader *vcoutputtermhdr;
struct tmComResTSFormatDescrHeader *tsfmt;
struct tmComResPSFormatDescrHeader *psfmt;
struct tmComResSelDescrHeader *psel;
struct tmComResProcDescrHeader *pdh;
struct tmComResAFeatureDescrHeader *afd;
struct tmComResEncoderDescrHeader *edh;
struct tmComResVBIFormatDescrHeader *vbifmt;
u32 currpath = 0;
dprintk(DBGLVL_API,
"%s(?,?,%d) sizeof(struct tmComResDescrHeader) = %d bytes\n",
__func__, len, (u32)sizeof(struct tmComResDescrHeader));
for (idx = 0; idx < (len - sizeof(struct tmComResDescrHeader));) {
hdr = (struct tmComResDescrHeader *)(buf + idx);
if (hdr->type != CS_INTERFACE)
return SAA_ERR_NOT_SUPPORTED;
dprintk(DBGLVL_API, "@ 0x%x =\n", idx);
switch (hdr->subtype) {
case GENERAL_REQUEST:
dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
break;
case VC_TUNER_PATH:
dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
pathhdr = (struct tmComResPathDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " pathid = 0x%x\n",
pathhdr->pathid);
currpath = pathhdr->pathid;
break;
case VC_INPUT_TERMINAL:
dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
anttermhdr =
(struct tmComResAntTermDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " terminalid = 0x%x\n",
anttermhdr->terminalid);
dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
anttermhdr->terminaltype);
switch (anttermhdr->terminaltype) {
case ITT_ANTENNA:
dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
break;
case LINE_CONNECTOR:
dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
break;
case SPDIF_CONNECTOR:
dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
break;
case COMPOSITE_CONNECTOR:
dprintk(DBGLVL_API,
" = COMPOSITE_CONNECTOR\n");
break;
case SVIDEO_CONNECTOR:
dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
break;
case COMPONENT_CONNECTOR:
dprintk(DBGLVL_API,
" = COMPONENT_CONNECTOR\n");
break;
case STANDARD_DMA:
dprintk(DBGLVL_API, " = STANDARD_DMA\n");
break;
default:
dprintk(DBGLVL_API, " = undefined (0x%x)\n",
anttermhdr->terminaltype);
}
dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
anttermhdr->assocterminal);
dprintk(DBGLVL_API, " iterminal = 0x%x\n",
anttermhdr->iterminal);
dprintk(DBGLVL_API, " controlsize = 0x%x\n",
anttermhdr->controlsize);
break;
case VC_OUTPUT_TERMINAL:
dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
vcoutputtermhdr =
(struct tmComResDMATermDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " unitid = 0x%x\n",
vcoutputtermhdr->unitid);
dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
vcoutputtermhdr->terminaltype);
switch (vcoutputtermhdr->terminaltype) {
case ITT_ANTENNA:
dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
break;
case LINE_CONNECTOR:
dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
break;
case SPDIF_CONNECTOR:
dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
break;
case COMPOSITE_CONNECTOR:
dprintk(DBGLVL_API,
" = COMPOSITE_CONNECTOR\n");
break;
case SVIDEO_CONNECTOR:
dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
break;
case COMPONENT_CONNECTOR:
dprintk(DBGLVL_API,
" = COMPONENT_CONNECTOR\n");
break;
case STANDARD_DMA:
dprintk(DBGLVL_API, " = STANDARD_DMA\n");
break;
default:
dprintk(DBGLVL_API, " = undefined (0x%x)\n",
vcoutputtermhdr->terminaltype);
}
dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
vcoutputtermhdr->assocterminal);
dprintk(DBGLVL_API, " sourceid = 0x%x\n",
vcoutputtermhdr->sourceid);
dprintk(DBGLVL_API, " iterminal = 0x%x\n",
vcoutputtermhdr->iterminal);
dprintk(DBGLVL_API, " BARLocation = 0x%x\n",
vcoutputtermhdr->BARLocation);
dprintk(DBGLVL_API, " flags = 0x%x\n",
vcoutputtermhdr->flags);
dprintk(DBGLVL_API, " interruptid = 0x%x\n",
vcoutputtermhdr->interruptid);
dprintk(DBGLVL_API, " buffercount = 0x%x\n",
vcoutputtermhdr->buffercount);
dprintk(DBGLVL_API, " metadatasize = 0x%x\n",
vcoutputtermhdr->metadatasize);
dprintk(DBGLVL_API, " controlsize = 0x%x\n",
vcoutputtermhdr->controlsize);
dprintk(DBGLVL_API, " numformats = 0x%x\n",
vcoutputtermhdr->numformats);
next_offset = idx + (vcoutputtermhdr->len);
for (i = 0; i < vcoutputtermhdr->numformats; i++) {
t = (struct tmComResDescrHeader *)
(buf + next_offset);
switch (t->subtype) {
case VS_FORMAT_MPEG2TS:
tsfmt =
(struct tmComResTSFormatDescrHeader *)t;
if (currpath == 1)
tsport = &dev->ports[SAA7164_PORT_TS1];
else
tsport = &dev->ports[SAA7164_PORT_TS2];
memcpy(&tsport->hwcfg, vcoutputtermhdr,
sizeof(*vcoutputtermhdr));
saa7164_api_configure_port_mpeg2ts(dev,
tsport, tsfmt);
break;
case VS_FORMAT_MPEG2PS:
psfmt =
(struct tmComResPSFormatDescrHeader *)t;
if (currpath == 1)
encport = &dev->ports[SAA7164_PORT_ENC1];
else
encport = &dev->ports[SAA7164_PORT_ENC2];
memcpy(&encport->hwcfg, vcoutputtermhdr,
sizeof(*vcoutputtermhdr));
saa7164_api_configure_port_mpeg2ps(dev,
encport, psfmt);
break;
case VS_FORMAT_VBI:
vbifmt =
(struct tmComResVBIFormatDescrHeader *)t;
if (currpath == 1)
vbiport = &dev->ports[SAA7164_PORT_VBI1];
else
vbiport = &dev->ports[SAA7164_PORT_VBI2];
memcpy(&vbiport->hwcfg, vcoutputtermhdr,
sizeof(*vcoutputtermhdr));
memcpy(&vbiport->vbi_fmt_ntsc, vbifmt,
sizeof(*vbifmt));
saa7164_api_configure_port_vbi(dev,
vbiport);
break;
case VS_FORMAT_RDS:
dprintk(DBGLVL_API,
" = VS_FORMAT_RDS\n");
break;
case VS_FORMAT_UNCOMPRESSED:
dprintk(DBGLVL_API,
" = VS_FORMAT_UNCOMPRESSED\n");
break;
case VS_FORMAT_TYPE:
dprintk(DBGLVL_API,
" = VS_FORMAT_TYPE\n");
break;
default:
dprintk(DBGLVL_API,
" = undefined (0x%x)\n",
t->subtype);
}
next_offset += t->len;
}
break;
case TUNER_UNIT:
dprintk(DBGLVL_API, " TUNER_UNIT\n");
tunerunithdr =
(struct tmComResTunerDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " unitid = 0x%x\n",
tunerunithdr->unitid);
dprintk(DBGLVL_API, " sourceid = 0x%x\n",
tunerunithdr->sourceid);
dprintk(DBGLVL_API, " iunit = 0x%x\n",
tunerunithdr->iunit);
dprintk(DBGLVL_API, " tuningstandards = 0x%x\n",
tunerunithdr->tuningstandards);
dprintk(DBGLVL_API, " controlsize = 0x%x\n",
tunerunithdr->controlsize);
dprintk(DBGLVL_API, " controls = 0x%x\n",
tunerunithdr->controls);
if (tunerunithdr->unitid == tunerunithdr->iunit) {
if (currpath == 1)
encport = &dev->ports[SAA7164_PORT_ENC1];
else
encport = &dev->ports[SAA7164_PORT_ENC2];
memcpy(&encport->tunerunit, tunerunithdr,
sizeof(struct tmComResTunerDescrHeader));
dprintk(DBGLVL_API,
" (becomes dev->enc[%d] tuner)\n",
encport->nr);
}
break;
case VC_SELECTOR_UNIT:
psel = (struct tmComResSelDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
dprintk(DBGLVL_API, " unitid = 0x%x\n",
psel->unitid);
dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
psel->nrinpins);
dprintk(DBGLVL_API, " sourceid = 0x%x\n",
psel->sourceid);
break;
case VC_PROCESSING_UNIT:
pdh = (struct tmComResProcDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
dprintk(DBGLVL_API, " unitid = 0x%x\n",
pdh->unitid);
dprintk(DBGLVL_API, " sourceid = 0x%x\n",
pdh->sourceid);
dprintk(DBGLVL_API, " controlsize = 0x%x\n",
pdh->controlsize);
if (pdh->controlsize == 0x04) {
if (currpath == 1)
encport = &dev->ports[SAA7164_PORT_ENC1];
else
encport = &dev->ports[SAA7164_PORT_ENC2];
memcpy(&encport->vidproc, pdh,
sizeof(struct tmComResProcDescrHeader));
dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n",
encport->nr);
}
break;
case FEATURE_UNIT:
afd = (struct tmComResAFeatureDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " FEATURE_UNIT\n");
dprintk(DBGLVL_API, " unitid = 0x%x\n",
afd->unitid);
dprintk(DBGLVL_API, " sourceid = 0x%x\n",
afd->sourceid);
dprintk(DBGLVL_API, " controlsize = 0x%x\n",
afd->controlsize);
if (currpath == 1)
encport = &dev->ports[SAA7164_PORT_ENC1];
else
encport = &dev->ports[SAA7164_PORT_ENC2];
memcpy(&encport->audfeat, afd,
sizeof(struct tmComResAFeatureDescrHeader));
dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n",
encport->nr);
break;
case ENCODER_UNIT:
edh = (struct tmComResEncoderDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " ENCODER_UNIT\n");
dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
dprintk(DBGLVL_API, " vsourceid = 0x%x\n",
edh->vsourceid);
dprintk(DBGLVL_API, " asourceid = 0x%x\n",
edh->asourceid);
dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
if (edh->iunit == edh->unitid) {
if (currpath == 1)
encport = &dev->ports[SAA7164_PORT_ENC1];
else
encport = &dev->ports[SAA7164_PORT_ENC2];
memcpy(&encport->encunit, edh,
sizeof(struct tmComResEncoderDescrHeader));
dprintk(DBGLVL_API,
" (becomes dev->enc[%d])\n",
encport->nr);
}
break;
case EXTENSION_UNIT:
dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
exthdr = (struct tmComResExtDevDescrHeader *)(buf + idx);
dprintk(DBGLVL_API, " unitid = 0x%x\n",
exthdr->unitid);
dprintk(DBGLVL_API, " deviceid = 0x%x\n",
exthdr->deviceid);
dprintk(DBGLVL_API, " devicetype = 0x%x\n",
exthdr->devicetype);
if (exthdr->devicetype & 0x1)
dprintk(DBGLVL_API, " = Decoder Device\n");
if (exthdr->devicetype & 0x2)
dprintk(DBGLVL_API, " = GPIO Source\n");
if (exthdr->devicetype & 0x4)
dprintk(DBGLVL_API, " = Video Decoder\n");
if (exthdr->devicetype & 0x8)
dprintk(DBGLVL_API, " = Audio Decoder\n");
if (exthdr->devicetype & 0x20)
dprintk(DBGLVL_API, " = Crossbar\n");
if (exthdr->devicetype & 0x40)
dprintk(DBGLVL_API, " = Tuner\n");
if (exthdr->devicetype & 0x80)
dprintk(DBGLVL_API, " = IF PLL\n");
if (exthdr->devicetype & 0x100)
dprintk(DBGLVL_API, " = Demodulator\n");
if (exthdr->devicetype & 0x200)
dprintk(DBGLVL_API, " = RDS Decoder\n");
if (exthdr->devicetype & 0x400)
dprintk(DBGLVL_API, " = Encoder\n");
if (exthdr->devicetype & 0x800)
dprintk(DBGLVL_API, " = IR Decoder\n");
if (exthdr->devicetype & 0x1000)
dprintk(DBGLVL_API, " = EEPROM\n");
if (exthdr->devicetype & 0x2000)
dprintk(DBGLVL_API,
" = VBI Decoder\n");
if (exthdr->devicetype & 0x10000)
dprintk(DBGLVL_API,
" = Streaming Device\n");
if (exthdr->devicetype & 0x20000)
dprintk(DBGLVL_API,
" = DRM Device\n");
if (exthdr->devicetype & 0x40000000)
dprintk(DBGLVL_API,
" = Generic Device\n");
if (exthdr->devicetype & 0x80000000)
dprintk(DBGLVL_API,
" = Config Space Device\n");
dprintk(DBGLVL_API, " numgpiopins = 0x%x\n",
exthdr->numgpiopins);
dprintk(DBGLVL_API, " numgpiogroups = 0x%x\n",
exthdr->numgpiogroups);
dprintk(DBGLVL_API, " controlsize = 0x%x\n",
exthdr->controlsize);
if (exthdr->devicetype & 0x80) {
if (currpath == 1)
encport = &dev->ports[SAA7164_PORT_ENC1];
else
encport = &dev->ports[SAA7164_PORT_ENC2];
memcpy(&encport->ifunit, exthdr,
sizeof(struct tmComResExtDevDescrHeader));
dprintk(DBGLVL_API,
" (becomes dev->enc[%d])\n",
encport->nr);
}
break;
case PVC_INFRARED_UNIT:
dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
break;
case DRM_UNIT:
dprintk(DBGLVL_API, " DRM_UNIT\n");
break;
default:
dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
}
dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
idx += hdr->len;
}
return 0;
}
int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
{
int ret;
u32 buflen = 0;
u8 *buf;
dprintk(DBGLVL_API, "%s()\n", __func__);
/* Get the total descriptor length */
ret = saa7164_cmd_send(dev, 0, GET_LEN,
GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
__func__, buflen);
/* Allocate enough storage for all of the descs */
buf = kzalloc(buflen, GFP_KERNEL);
if (!buf)
return SAA_ERR_NO_RESOURCES;
/* Retrieve them */
ret = saa7164_cmd_send(dev, 0, GET_CUR,
GET_DESCRIPTORS_CONTROL, buflen, buf);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
goto out;
}
if (saa_debug & DBGLVL_API)
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf,
buflen & ~15, false);
saa7164_api_dump_subdevs(dev, buf, buflen);
out:
kfree(buf);
return ret;
}
int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
u32 datalen, u8 *data)
{
struct saa7164_dev *dev = bus->dev;
u16 len = 0;
int unitid;
u8 buf[256];
int ret;
dprintk(DBGLVL_API, "%s() addr=%x reglen=%d datalen=%d\n",
__func__, addr, reglen, datalen);
if (reglen > 4)
return -EIO;
/* Prepare the send buffer */
/* Bytes 00-03 source register length
* 04-07 source bytes to read
* 08... register address
*/
memset(buf, 0, sizeof(buf));
memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
*((u32 *)(buf + 0 * sizeof(u32))) = reglen;
*((u32 *)(buf + 1 * sizeof(u32))) = datalen;
unitid = saa7164_i2caddr_to_unitid(bus, addr);
if (unitid < 0) {
printk(KERN_ERR
"%s() error, cannot translate regaddr 0x%x to unitid\n",
__func__, addr);
return -EIO;
}
ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
return -EIO;
}
dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
if (saa_debug & DBGLVL_I2C)
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf,
32, false);
ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
EXU_REGISTER_ACCESS_CONTROL, len, &buf);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
else {
if (saa_debug & DBGLVL_I2C)
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
buf, sizeof(buf), false);
memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
}
return ret == SAA_OK ? 0 : -EIO;
}
/* For a given 8 bit i2c address device, write the buffer */
int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
u8 *data)
{
struct saa7164_dev *dev = bus->dev;
u16 len = 0;
int unitid;
int reglen;
u8 buf[256];
int ret;
dprintk(DBGLVL_API, "%s() addr=0x%2x len=0x%x\n",
__func__, addr, datalen);
if ((datalen == 0) || (datalen > 232))
return -EIO;
memset(buf, 0, sizeof(buf));
unitid = saa7164_i2caddr_to_unitid(bus, addr);
if (unitid < 0) {
printk(KERN_ERR
"%s() error, cannot translate regaddr 0x%x to unitid\n",
__func__, addr);
return -EIO;
}
reglen = saa7164_i2caddr_to_reglen(bus, addr);
if (reglen < 0) {
printk(KERN_ERR
"%s() error, cannot translate regaddr to reglen\n",
__func__);
return -EIO;
}
ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
if (ret != SAA_OK) {
printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
return -EIO;
}
dprintk(DBGLVL_API, "%s() len = %d bytes unitid=0x%x\n", __func__,
len, unitid);
/* Prepare the send buffer */
/* Bytes 00-03 dest register length
* 04-07 dest bytes to write
* 08... register address
*/
*((u32 *)(buf + 0 * sizeof(u32))) = reglen;
*((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
memcpy((buf + 2 * sizeof(u32)), data, datalen);
if (saa_debug & DBGLVL_I2C)
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
buf, sizeof(buf), false);
ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
EXU_REGISTER_ACCESS_CONTROL, len, &buf);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
return ret == SAA_OK ? 0 : -EIO;
}
static int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
u8 pin, u8 state)
{
int ret;
struct tmComResGPIO t;
dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
__func__, unitid, pin, state);
if ((pin > 7) || (state > 2))
return SAA_ERR_BAD_PARAMETER;
t.pin = pin;
t.state = state;
ret = saa7164_cmd_send(dev, unitid, SET_CUR,
EXU_GPIO_CONTROL, sizeof(t), &t);
if (ret != SAA_OK)
printk(KERN_ERR "%s() error, ret = 0x%x\n",
__func__, ret);
return ret;
}
int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
u8 pin)
{
return saa7164_api_modify_gpio(dev, unitid, pin, 1);
}
int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
u8 pin)
{
return saa7164_api_modify_gpio(dev, unitid, pin, 0);
}
| linux-master | drivers/media/pci/saa7164/saa7164-api.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the NXP SAA7164 PCIe bridge
*
* Copyright (c) 2010-2015 Steven Toth <[email protected]>
*/
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kmod.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <asm/div64.h>
#include "saa7164.h"
MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
MODULE_AUTHOR("Steven Toth <[email protected]>");
MODULE_LICENSE("GPL");
/*
* 1 Basic
* 2
* 4 i2c
* 8 api
* 16 cmd
* 32 bus
*/
unsigned int saa_debug;
module_param_named(debug, saa_debug, int, 0644);
MODULE_PARM_DESC(debug, "enable debug messages");
static unsigned int fw_debug;
module_param(fw_debug, int, 0644);
MODULE_PARM_DESC(fw_debug, "Firmware debug level def:2");
unsigned int encoder_buffers = SAA7164_MAX_ENCODER_BUFFERS;
module_param(encoder_buffers, int, 0644);
MODULE_PARM_DESC(encoder_buffers, "Total buffers in read queue 16-512 def:64");
unsigned int vbi_buffers = SAA7164_MAX_VBI_BUFFERS;
module_param(vbi_buffers, int, 0644);
MODULE_PARM_DESC(vbi_buffers, "Total buffers in read queue 16-512 def:64");
unsigned int waitsecs = 10;
module_param(waitsecs, int, 0644);
MODULE_PARM_DESC(waitsecs, "timeout on firmware messages");
static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
module_param_array(card, int, NULL, 0444);
MODULE_PARM_DESC(card, "card type");
static unsigned int print_histogram = 64;
module_param(print_histogram, int, 0644);
MODULE_PARM_DESC(print_histogram, "print histogram values once");
unsigned int crc_checking = 1;
module_param(crc_checking, int, 0644);
MODULE_PARM_DESC(crc_checking, "enable crc sanity checking on buffers");
static unsigned int guard_checking = 1;
module_param(guard_checking, int, 0644);
MODULE_PARM_DESC(guard_checking,
"enable dma sanity checking for buffer overruns");
static bool enable_msi = true;
module_param(enable_msi, bool, 0444);
MODULE_PARM_DESC(enable_msi,
"enable the use of an msi interrupt if available");
static unsigned int saa7164_devcount;
static DEFINE_MUTEX(devlist);
LIST_HEAD(saa7164_devlist);
#define INT_SIZE 16
static void saa7164_pack_verifier(struct saa7164_buffer *buf)
{
u8 *p = (u8 *)buf->cpu;
int i;
for (i = 0; i < buf->actual_size; i += 2048) {
if ((*(p + i + 0) != 0x00) || (*(p + i + 1) != 0x00) ||
(*(p + i + 2) != 0x01) || (*(p + i + 3) != 0xBA)) {
printk(KERN_ERR "No pack at 0x%x\n", i);
#if 0
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
p + 1, 32, false);
#endif
}
}
}
#define FIXED_VIDEO_PID 0xf1
#define FIXED_AUDIO_PID 0xf2
static void saa7164_ts_verifier(struct saa7164_buffer *buf)
{
struct saa7164_port *port = buf->port;
u32 i;
u8 cc, a;
u16 pid;
u8 *bufcpu = (u8 *)buf->cpu;
port->sync_errors = 0;
port->v_cc_errors = 0;
port->a_cc_errors = 0;
for (i = 0; i < buf->actual_size; i += 188) {
if (*(bufcpu + i) != 0x47)
port->sync_errors++;
/* TODO: Query pid lower 8 bits, ignoring upper bits intensionally */
pid = ((*(bufcpu + i + 1) & 0x1f) << 8) | *(bufcpu + i + 2);
cc = *(bufcpu + i + 3) & 0x0f;
if (pid == FIXED_VIDEO_PID) {
a = ((port->last_v_cc + 1) & 0x0f);
if (a != cc) {
printk(KERN_ERR "video cc last = %x current = %x i = %d\n",
port->last_v_cc, cc, i);
port->v_cc_errors++;
}
port->last_v_cc = cc;
} else
if (pid == FIXED_AUDIO_PID) {
a = ((port->last_a_cc + 1) & 0x0f);
if (a != cc) {
printk(KERN_ERR "audio cc last = %x current = %x i = %d\n",
port->last_a_cc, cc, i);
port->a_cc_errors++;
}
port->last_a_cc = cc;
}
}
/* Only report errors if we've been through this function at least
* once already and the cached cc values are primed. First time through
* always generates errors.
*/
if (port->v_cc_errors && (port->done_first_interrupt > 1))
printk(KERN_ERR "video pid cc, %d errors\n", port->v_cc_errors);
if (port->a_cc_errors && (port->done_first_interrupt > 1))
printk(KERN_ERR "audio pid cc, %d errors\n", port->a_cc_errors);
if (port->sync_errors && (port->done_first_interrupt > 1))
printk(KERN_ERR "sync_errors = %d\n", port->sync_errors);
if (port->done_first_interrupt == 1)
port->done_first_interrupt++;
}
static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name)
{
int i;
memset(hg, 0, sizeof(struct saa7164_histogram));
strscpy(hg->name, name, sizeof(hg->name));
/* First 30ms x 1ms */
for (i = 0; i < 30; i++)
hg->counter1[0 + i].val = i;
/* 30 - 200ms x 10ms */
for (i = 0; i < 18; i++)
hg->counter1[30 + i].val = 30 + (i * 10);
/* 200 - 2000ms x 100ms */
for (i = 0; i < 15; i++)
hg->counter1[48 + i].val = 200 + (i * 200);
/* Catch all massive value (2secs) */
hg->counter1[55].val = 2000;
/* Catch all massive value (4secs) */
hg->counter1[56].val = 4000;
/* Catch all massive value (8secs) */
hg->counter1[57].val = 8000;
/* Catch all massive value (15secs) */
hg->counter1[58].val = 15000;
/* Catch all massive value (30secs) */
hg->counter1[59].val = 30000;
/* Catch all massive value (60secs) */
hg->counter1[60].val = 60000;
/* Catch all massive value (5mins) */
hg->counter1[61].val = 300000;
/* Catch all massive value (15mins) */
hg->counter1[62].val = 900000;
/* Catch all massive values (1hr) */
hg->counter1[63].val = 3600000;
}
void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val)
{
int i;
for (i = 0; i < 64; i++) {
if (val <= hg->counter1[i].val) {
hg->counter1[i].count++;
hg->counter1[i].update_time = jiffies;
break;
}
}
}
static void saa7164_histogram_print(struct saa7164_port *port,
struct saa7164_histogram *hg)
{
u32 entries = 0;
int i;
printk(KERN_ERR "Histogram named %s (ms, count, last_update_jiffy)\n", hg->name);
for (i = 0; i < 64; i++) {
if (hg->counter1[i].count == 0)
continue;
printk(KERN_ERR " %4d %12d %Ld\n",
hg->counter1[i].val,
hg->counter1[i].count,
hg->counter1[i].update_time);
entries++;
}
printk(KERN_ERR "Total: %d\n", entries);
}
static void saa7164_work_enchandler_helper(struct saa7164_port *port, int bufnr)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf = NULL;
struct saa7164_user_buffer *ubuf = NULL;
struct list_head *c, *n;
int i = 0;
u8 *p;
mutex_lock(&port->dmaqueue_lock);
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
if (i++ > port->hwcfg.buffercount) {
printk(KERN_ERR "%s() illegal i count %d\n",
__func__, i);
break;
}
if (buf->idx == bufnr) {
/* Found the buffer, deal with it */
dprintk(DBGLVL_IRQ, "%s() bufnr: %d\n", __func__, bufnr);
if (crc_checking) {
/* Throw a new checksum on the dma buffer */
buf->crc = crc32(0, buf->cpu, buf->actual_size);
}
if (guard_checking) {
p = (u8 *)buf->cpu;
if ((*(p + buf->actual_size + 0) != 0xff) ||
(*(p + buf->actual_size + 1) != 0xff) ||
(*(p + buf->actual_size + 2) != 0xff) ||
(*(p + buf->actual_size + 3) != 0xff) ||
(*(p + buf->actual_size + 0x10) != 0xff) ||
(*(p + buf->actual_size + 0x11) != 0xff) ||
(*(p + buf->actual_size + 0x12) != 0xff) ||
(*(p + buf->actual_size + 0x13) != 0xff)) {
printk(KERN_ERR "%s() buf %p guard buffer breach\n",
__func__, buf);
#if 0
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
p + buf->actual_size - 32, 64, false);
#endif
}
}
if ((port->nr != SAA7164_PORT_VBI1) && (port->nr != SAA7164_PORT_VBI2)) {
/* Validate the incoming buffer content */
if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
saa7164_ts_verifier(buf);
else if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
saa7164_pack_verifier(buf);
}
/* find a free user buffer and clone to it */
if (!list_empty(&port->list_buf_free.list)) {
/* Pull the first buffer from the used list */
ubuf = list_first_entry(&port->list_buf_free.list,
struct saa7164_user_buffer, list);
if (buf->actual_size <= ubuf->actual_size) {
memcpy(ubuf->data, buf->cpu, ubuf->actual_size);
if (crc_checking) {
/* Throw a new checksum on the read buffer */
ubuf->crc = crc32(0, ubuf->data, ubuf->actual_size);
}
/* Requeue the buffer on the free list */
ubuf->pos = 0;
list_move_tail(&ubuf->list,
&port->list_buf_used.list);
/* Flag any userland waiters */
wake_up_interruptible(&port->wait_read);
} else {
printk(KERN_ERR "buf %p bufsize fails match\n", buf);
}
} else
printk(KERN_ERR "encirq no free buffers, increase param encoder_buffers\n");
/* Ensure offset into buffer remains 0, fill buffer
* with known bad data. We check for this data at a later point
* in time. */
saa7164_buffer_zero_offsets(port, bufnr);
memset(buf->cpu, 0xff, buf->pci_size);
if (crc_checking) {
/* Throw yet aanother new checksum on the dma buffer */
buf->crc = crc32(0, buf->cpu, buf->actual_size);
}
break;
}
}
mutex_unlock(&port->dmaqueue_lock);
}
static void saa7164_work_enchandler(struct work_struct *w)
{
struct saa7164_port *port =
container_of(w, struct saa7164_port, workenc);
struct saa7164_dev *dev = port->dev;
u32 wp, mcb, rp;
port->last_svc_msecs_diff = port->last_svc_msecs;
port->last_svc_msecs = jiffies_to_msecs(jiffies);
port->last_svc_msecs_diff = port->last_svc_msecs -
port->last_svc_msecs_diff;
saa7164_histogram_update(&port->svc_interval,
port->last_svc_msecs_diff);
port->last_irq_svc_msecs_diff = port->last_svc_msecs -
port->last_irq_msecs;
saa7164_histogram_update(&port->irq_svc_interval,
port->last_irq_svc_msecs_diff);
dprintk(DBGLVL_IRQ,
"%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
__func__,
port->last_svc_msecs_diff,
port->last_irq_svc_msecs_diff,
port->last_svc_wp,
port->last_svc_rp
);
/* Current write position */
wp = saa7164_readl(port->bufcounter);
if (wp > (port->hwcfg.buffercount - 1)) {
printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
return;
}
/* Most current complete buffer */
if (wp == 0)
mcb = (port->hwcfg.buffercount - 1);
else
mcb = wp - 1;
while (1) {
if (port->done_first_interrupt == 0) {
port->done_first_interrupt++;
rp = mcb;
} else
rp = (port->last_svc_rp + 1) % 8;
if (rp > (port->hwcfg.buffercount - 1)) {
printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
break;
}
saa7164_work_enchandler_helper(port, rp);
port->last_svc_rp = rp;
if (rp == mcb)
break;
}
/* TODO: Convert this into a /proc/saa7164 style readable file */
if (print_histogram == port->nr) {
saa7164_histogram_print(port, &port->irq_interval);
saa7164_histogram_print(port, &port->svc_interval);
saa7164_histogram_print(port, &port->irq_svc_interval);
saa7164_histogram_print(port, &port->read_interval);
saa7164_histogram_print(port, &port->poll_interval);
/* TODO: fix this to preserve any previous state */
print_histogram = 64 + port->nr;
}
}
static void saa7164_work_vbihandler(struct work_struct *w)
{
struct saa7164_port *port =
container_of(w, struct saa7164_port, workenc);
struct saa7164_dev *dev = port->dev;
u32 wp, mcb, rp;
port->last_svc_msecs_diff = port->last_svc_msecs;
port->last_svc_msecs = jiffies_to_msecs(jiffies);
port->last_svc_msecs_diff = port->last_svc_msecs -
port->last_svc_msecs_diff;
saa7164_histogram_update(&port->svc_interval,
port->last_svc_msecs_diff);
port->last_irq_svc_msecs_diff = port->last_svc_msecs -
port->last_irq_msecs;
saa7164_histogram_update(&port->irq_svc_interval,
port->last_irq_svc_msecs_diff);
dprintk(DBGLVL_IRQ,
"%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
__func__,
port->last_svc_msecs_diff,
port->last_irq_svc_msecs_diff,
port->last_svc_wp,
port->last_svc_rp
);
/* Current write position */
wp = saa7164_readl(port->bufcounter);
if (wp > (port->hwcfg.buffercount - 1)) {
printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
return;
}
/* Most current complete buffer */
if (wp == 0)
mcb = (port->hwcfg.buffercount - 1);
else
mcb = wp - 1;
while (1) {
if (port->done_first_interrupt == 0) {
port->done_first_interrupt++;
rp = mcb;
} else
rp = (port->last_svc_rp + 1) % 8;
if (rp > (port->hwcfg.buffercount - 1)) {
printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
break;
}
saa7164_work_enchandler_helper(port, rp);
port->last_svc_rp = rp;
if (rp == mcb)
break;
}
/* TODO: Convert this into a /proc/saa7164 style readable file */
if (print_histogram == port->nr) {
saa7164_histogram_print(port, &port->irq_interval);
saa7164_histogram_print(port, &port->svc_interval);
saa7164_histogram_print(port, &port->irq_svc_interval);
saa7164_histogram_print(port, &port->read_interval);
saa7164_histogram_print(port, &port->poll_interval);
/* TODO: fix this to preserve any previous state */
print_histogram = 64 + port->nr;
}
}
static void saa7164_work_cmdhandler(struct work_struct *w)
{
struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
/* Wake up any complete commands */
saa7164_irq_dequeue(dev);
}
static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
{
struct saa7164_port *port = buf->port;
/* Feed the transport payload into the kernel demux */
dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu,
SAA7164_TS_NUMBER_OF_LINES);
}
static irqreturn_t saa7164_irq_vbi(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
/* Store old time */
port->last_irq_msecs_diff = port->last_irq_msecs;
/* Collect new stats */
port->last_irq_msecs = jiffies_to_msecs(jiffies);
/* Calculate stats */
port->last_irq_msecs_diff = port->last_irq_msecs -
port->last_irq_msecs_diff;
saa7164_histogram_update(&port->irq_interval,
port->last_irq_msecs_diff);
dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__,
port->last_irq_msecs_diff);
/* Tis calls the vbi irq handler */
schedule_work(&port->workenc);
return 0;
}
static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
/* Store old time */
port->last_irq_msecs_diff = port->last_irq_msecs;
/* Collect new stats */
port->last_irq_msecs = jiffies_to_msecs(jiffies);
/* Calculate stats */
port->last_irq_msecs_diff = port->last_irq_msecs -
port->last_irq_msecs_diff;
saa7164_histogram_update(&port->irq_interval,
port->last_irq_msecs_diff);
dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__,
port->last_irq_msecs_diff);
schedule_work(&port->workenc);
return 0;
}
static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
{
struct saa7164_dev *dev = port->dev;
struct saa7164_buffer *buf;
struct list_head *c, *n;
int wp, i = 0, rp;
/* Find the current write point from the hardware */
wp = saa7164_readl(port->bufcounter);
BUG_ON(wp > (port->hwcfg.buffercount - 1));
/* Find the previous buffer to the current write point */
if (wp == 0)
rp = (port->hwcfg.buffercount - 1);
else
rp = wp - 1;
/* Lookup the WP in the buffer list */
/* TODO: turn this into a worker thread */
list_for_each_safe(c, n, &port->dmaqueue.list) {
buf = list_entry(c, struct saa7164_buffer, list);
BUG_ON(i > port->hwcfg.buffercount);
i++;
if (buf->idx == rp) {
/* Found the buffer, deal with it */
dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
__func__, wp, rp);
saa7164_buffer_deliver(buf);
break;
}
}
return 0;
}
/* Primary IRQ handler and dispatch mechanism */
static irqreturn_t saa7164_irq(int irq, void *dev_id)
{
struct saa7164_dev *dev = dev_id;
struct saa7164_port *porta, *portb, *portc, *portd, *porte, *portf;
u32 intid, intstat[INT_SIZE/4];
int i, handled = 0, bit;
if (dev == NULL) {
printk(KERN_ERR "%s() No device specified\n", __func__);
handled = 0;
goto out;
}
porta = &dev->ports[SAA7164_PORT_TS1];
portb = &dev->ports[SAA7164_PORT_TS2];
portc = &dev->ports[SAA7164_PORT_ENC1];
portd = &dev->ports[SAA7164_PORT_ENC2];
porte = &dev->ports[SAA7164_PORT_VBI1];
portf = &dev->ports[SAA7164_PORT_VBI2];
/* Check that the hardware is accessible. If the status bytes are
* 0xFF then the device is not accessible, the IRQ belongs
* to another driver.
* 4 x u32 interrupt registers.
*/
for (i = 0; i < INT_SIZE/4; i++) {
/* TODO: Convert into saa7164_readl() */
/* Read the 4 hardware interrupt registers */
intstat[i] = saa7164_readl(dev->int_status + (i * 4));
if (intstat[i])
handled = 1;
}
if (handled == 0)
goto out;
/* For each of the HW interrupt registers */
for (i = 0; i < INT_SIZE/4; i++) {
if (intstat[i]) {
/* Each function of the board has it's own interruptid.
* Find the function that triggered then call
* it's handler.
*/
for (bit = 0; bit < 32; bit++) {
if (((intstat[i] >> bit) & 0x00000001) == 0)
continue;
/* Calculate the interrupt id (0x00 to 0x7f) */
intid = (i * 32) + bit;
if (intid == dev->intfdesc.bInterruptId) {
/* A response to an cmd/api call */
schedule_work(&dev->workcmd);
} else if (intid == porta->hwcfg.interruptid) {
/* Transport path 1 */
saa7164_irq_ts(porta);
} else if (intid == portb->hwcfg.interruptid) {
/* Transport path 2 */
saa7164_irq_ts(portb);
} else if (intid == portc->hwcfg.interruptid) {
/* Encoder path 1 */
saa7164_irq_encoder(portc);
} else if (intid == portd->hwcfg.interruptid) {
/* Encoder path 2 */
saa7164_irq_encoder(portd);
} else if (intid == porte->hwcfg.interruptid) {
/* VBI path 1 */
saa7164_irq_vbi(porte);
} else if (intid == portf->hwcfg.interruptid) {
/* VBI path 2 */
saa7164_irq_vbi(portf);
} else {
/* Find the function */
dprintk(DBGLVL_IRQ,
"%s() unhandled interrupt reg 0x%x bit 0x%x intid = 0x%x\n",
__func__, i, bit, intid);
}
}
/* Ack it */
saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
}
}
out:
return IRQ_RETVAL(handled);
}
void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
{
struct saa7164_fw_status *s = &dev->fw_status;
dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
dev->fw_status.remainheap =
saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
dprintk(1, "Firmware status:\n");
dprintk(1, " .status = 0x%08x\n", s->status);
dprintk(1, " .mode = 0x%08x\n", s->mode);
dprintk(1, " .spec = 0x%08x\n", s->spec);
dprintk(1, " .inst = 0x%08x\n", s->inst);
dprintk(1, " .cpuload = 0x%08x\n", s->cpuload);
dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
}
u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
{
u32 reg;
reg = saa7164_readl(SAA_DEVICE_VERSION);
dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
(reg & 0x0000fc00) >> 10,
(reg & 0x000003e0) >> 5,
(reg & 0x0000001f),
(reg & 0xffff0000) >> 16,
reg);
return reg;
}
/* TODO: Debugging func, remove */
void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
{
int i;
dprintk(1, "--------------------> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (i = 0; i < 0x100; i += 16)
dprintk(1, "region0[0x%08x] = %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
i,
(u8)saa7164_readb(addr + i + 0),
(u8)saa7164_readb(addr + i + 1),
(u8)saa7164_readb(addr + i + 2),
(u8)saa7164_readb(addr + i + 3),
(u8)saa7164_readb(addr + i + 4),
(u8)saa7164_readb(addr + i + 5),
(u8)saa7164_readb(addr + i + 6),
(u8)saa7164_readb(addr + i + 7),
(u8)saa7164_readb(addr + i + 8),
(u8)saa7164_readb(addr + i + 9),
(u8)saa7164_readb(addr + i + 10),
(u8)saa7164_readb(addr + i + 11),
(u8)saa7164_readb(addr + i + 12),
(u8)saa7164_readb(addr + i + 13),
(u8)saa7164_readb(addr + i + 14),
(u8)saa7164_readb(addr + i + 15)
);
}
static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
{
dprintk(1, "@0x%p hwdesc sizeof(struct tmComResHWDescr) = %d bytes\n",
&dev->hwdesc, (u32)sizeof(struct tmComResHWDescr));
dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
dprintk(1, " .bDescriptorSubtype = 0x%x\n",
dev->hwdesc.bDescriptorSubtype);
dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
dev->hwdesc.dwDeviceRegistersLocation);
dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
dev->hwdesc.dwHostMemoryRegion);
dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
dev->hwdesc.dwHostMemoryRegionSize);
dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
dev->hwdesc.dwHostHibernatMemRegion);
dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
dev->hwdesc.dwHostHibernatMemRegionSize);
}
static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
{
dprintk(1, "@0x%p intfdesc sizeof(struct tmComResInterfaceDescr) = %d bytes\n",
&dev->intfdesc, (u32)sizeof(struct tmComResInterfaceDescr));
dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
dprintk(1, " .bDescriptorSubtype = 0x%x\n",
dev->intfdesc.bDescriptorSubtype);
dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
dprintk(1, " .bDebugInterruptId = 0x%x\n",
dev->intfdesc.bDebugInterruptId);
dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
}
static void saa7164_dump_busdesc(struct saa7164_dev *dev)
{
dprintk(1, "@0x%p busdesc sizeof(struct tmComResBusDescr) = %d bytes\n",
&dev->busdesc, (u32)sizeof(struct tmComResBusDescr));
dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing);
dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing);
dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite);
dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead);
dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead);
}
/* Much of the hardware configuration and PCI registers are configured
* dynamically depending on firmware. We have to cache some initial
* structures then use these to locate other important structures
* from PCI space.
*/
static void saa7164_get_descriptors(struct saa7164_dev *dev)
{
memcpy_fromio(&dev->hwdesc, dev->bmmio, sizeof(struct tmComResHWDescr));
memcpy_fromio(&dev->intfdesc, dev->bmmio + sizeof(struct tmComResHWDescr),
sizeof(struct tmComResInterfaceDescr));
memcpy_fromio(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
sizeof(struct tmComResBusDescr));
if (dev->hwdesc.bLength != sizeof(struct tmComResHWDescr)) {
printk(KERN_ERR "Structure struct tmComResHWDescr is mangled\n");
printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
(u32)sizeof(struct tmComResHWDescr));
} else
saa7164_dump_hwdesc(dev);
if (dev->intfdesc.bLength != sizeof(struct tmComResInterfaceDescr)) {
printk(KERN_ERR "struct struct tmComResInterfaceDescr is mangled\n");
printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
(u32)sizeof(struct tmComResInterfaceDescr));
} else
saa7164_dump_intfdesc(dev);
saa7164_dump_busdesc(dev);
}
static int saa7164_pci_quirks(struct saa7164_dev *dev)
{
return 0;
}
static int get_resources(struct saa7164_dev *dev)
{
if (request_mem_region(pci_resource_start(dev->pci, 0),
pci_resource_len(dev->pci, 0), dev->name)) {
if (request_mem_region(pci_resource_start(dev->pci, 2),
pci_resource_len(dev->pci, 2), dev->name))
return 0;
}
printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
dev->name,
(u64)pci_resource_start(dev->pci, 0),
(u64)pci_resource_start(dev->pci, 2));
return -EBUSY;
}
static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
{
struct saa7164_port *port = NULL;
BUG_ON((portnr < 0) || (portnr >= SAA7164_MAX_PORTS));
port = &dev->ports[portnr];
port->dev = dev;
port->nr = portnr;
if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
port->type = SAA7164_MPEG_DVB;
else
if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2)) {
port->type = SAA7164_MPEG_ENCODER;
/* We need a deferred interrupt handler for cmd handling */
INIT_WORK(&port->workenc, saa7164_work_enchandler);
} else if ((portnr == SAA7164_PORT_VBI1) || (portnr == SAA7164_PORT_VBI2)) {
port->type = SAA7164_MPEG_VBI;
/* We need a deferred interrupt handler for cmd handling */
INIT_WORK(&port->workenc, saa7164_work_vbihandler);
} else
BUG();
/* Init all the critical resources */
mutex_init(&port->dvb.lock);
INIT_LIST_HEAD(&port->dmaqueue.list);
mutex_init(&port->dmaqueue_lock);
INIT_LIST_HEAD(&port->list_buf_used.list);
INIT_LIST_HEAD(&port->list_buf_free.list);
init_waitqueue_head(&port->wait_read);
saa7164_histogram_reset(&port->irq_interval, "irq intervals");
saa7164_histogram_reset(&port->svc_interval, "deferred intervals");
saa7164_histogram_reset(&port->irq_svc_interval,
"irq to deferred intervals");
saa7164_histogram_reset(&port->read_interval,
"encoder/vbi read() intervals");
saa7164_histogram_reset(&port->poll_interval,
"encoder/vbi poll() intervals");
return 0;
}
static int saa7164_dev_setup(struct saa7164_dev *dev)
{
int i;
mutex_init(&dev->lock);
atomic_inc(&dev->refcount);
dev->nr = saa7164_devcount++;
snprintf(dev->name, sizeof(dev->name), "saa7164[%d]", dev->nr);
mutex_lock(&devlist);
list_add_tail(&dev->devlist, &saa7164_devlist);
mutex_unlock(&devlist);
/* board config */
dev->board = UNSET;
if (card[dev->nr] < saa7164_bcount)
dev->board = card[dev->nr];
for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++)
if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
dev->pci->subsystem_device ==
saa7164_subids[i].subdevice)
dev->board = saa7164_subids[i].card;
if (UNSET == dev->board) {
dev->board = SAA7164_BOARD_UNKNOWN;
saa7164_card_list(dev);
}
dev->pci_bus = dev->pci->bus->number;
dev->pci_slot = PCI_SLOT(dev->pci->devfn);
/* I2C Defaults / setup */
dev->i2c_bus[0].dev = dev;
dev->i2c_bus[0].nr = 0;
dev->i2c_bus[1].dev = dev;
dev->i2c_bus[1].nr = 1;
dev->i2c_bus[2].dev = dev;
dev->i2c_bus[2].nr = 2;
/* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
saa7164_port_init(dev, SAA7164_PORT_TS1);
saa7164_port_init(dev, SAA7164_PORT_TS2);
saa7164_port_init(dev, SAA7164_PORT_ENC1);
saa7164_port_init(dev, SAA7164_PORT_ENC2);
saa7164_port_init(dev, SAA7164_PORT_VBI1);
saa7164_port_init(dev, SAA7164_PORT_VBI2);
if (get_resources(dev) < 0) {
printk(KERN_ERR "CORE %s No more PCIe resources for subsystem: %04x:%04x\n",
dev->name, dev->pci->subsystem_vendor,
dev->pci->subsystem_device);
saa7164_devcount--;
return -ENODEV;
}
/* PCI/e allocations */
dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
pci_resource_len(dev->pci, 0));
dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
pci_resource_len(dev->pci, 2));
dev->bmmio = (u8 __iomem *)dev->lmmio;
dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
/* Interrupt and ack register locations offset of bmmio */
dev->int_status = 0x183000 + 0xf80;
dev->int_ack = 0x183000 + 0xf90;
printk(KERN_INFO
"CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
dev->name, dev->pci->subsystem_vendor,
dev->pci->subsystem_device, saa7164_boards[dev->board].name,
dev->board, card[dev->nr] == dev->board ?
"insmod option" : "autodetected");
saa7164_pci_quirks(dev);
return 0;
}
static void saa7164_dev_unregister(struct saa7164_dev *dev)
{
dprintk(1, "%s()\n", __func__);
release_mem_region(pci_resource_start(dev->pci, 0),
pci_resource_len(dev->pci, 0));
release_mem_region(pci_resource_start(dev->pci, 2),
pci_resource_len(dev->pci, 2));
if (!atomic_dec_and_test(&dev->refcount))
return;
iounmap(dev->lmmio);
iounmap(dev->lmmio2);
return;
}
#ifdef CONFIG_DEBUG_FS
static void *saa7164_seq_start(struct seq_file *s, loff_t *pos)
{
struct saa7164_dev *dev;
loff_t index = *pos;
mutex_lock(&devlist);
list_for_each_entry(dev, &saa7164_devlist, devlist) {
if (index-- == 0) {
mutex_unlock(&devlist);
return dev;
}
}
mutex_unlock(&devlist);
return NULL;
}
static void *saa7164_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
struct saa7164_dev *dev = v;
void *ret;
mutex_lock(&devlist);
if (list_is_last(&dev->devlist, &saa7164_devlist))
ret = NULL;
else
ret = list_next_entry(dev, devlist);
mutex_unlock(&devlist);
++*pos;
return ret;
}
static void saa7164_seq_stop(struct seq_file *s, void *v)
{
}
static int saa7164_seq_show(struct seq_file *m, void *v)
{
struct saa7164_dev *dev = v;
struct tmComResBusInfo *b;
int i, c;
seq_printf(m, "%s = %p\n", dev->name, dev);
/* Lock the bus from any other access */
b = &dev->bus;
mutex_lock(&b->lock);
seq_printf(m, " .m_pdwSetWritePos = 0x%x (0x%08x)\n",
b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
seq_printf(m, " .m_pdwSetReadPos = 0x%x (0x%08x)\n",
b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
seq_printf(m, " .m_pdwGetWritePos = 0x%x (0x%08x)\n",
b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
seq_printf(m, " .m_pdwGetReadPos = 0x%x (0x%08x)\n",
b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
c = 0;
seq_puts(m, "\n Set Ring:\n");
seq_puts(m, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (i = 0; i < b->m_dwSizeSetRing; i++) {
if (c == 0)
seq_printf(m, " %04x:", i);
seq_printf(m, " %02x", readb(b->m_pdwSetRing + i));
if (++c == 16) {
seq_puts(m, "\n");
c = 0;
}
}
c = 0;
seq_puts(m, "\n Get Ring:\n");
seq_puts(m, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
for (i = 0; i < b->m_dwSizeGetRing; i++) {
if (c == 0)
seq_printf(m, " %04x:", i);
seq_printf(m, " %02x", readb(b->m_pdwGetRing + i));
if (++c == 16) {
seq_puts(m, "\n");
c = 0;
}
}
mutex_unlock(&b->lock);
return 0;
}
static const struct seq_operations saa7164_sops = {
.start = saa7164_seq_start,
.next = saa7164_seq_next,
.stop = saa7164_seq_stop,
.show = saa7164_seq_show,
};
DEFINE_SEQ_ATTRIBUTE(saa7164);
static struct dentry *saa7614_dentry;
static void __init saa7164_debugfs_create(void)
{
saa7614_dentry = debugfs_create_file("saa7164", 0444, NULL, NULL,
&saa7164_fops);
}
static void __exit saa7164_debugfs_remove(void)
{
debugfs_remove(saa7614_dentry);
}
#else
static void saa7164_debugfs_create(void) { }
static void saa7164_debugfs_remove(void) { }
#endif
static int saa7164_thread_function(void *data)
{
struct saa7164_dev *dev = data;
struct tmFwInfoStruct fwinfo;
u64 last_poll_time = 0;
dprintk(DBGLVL_THR, "thread started\n");
set_freezable();
while (1) {
msleep_interruptible(100);
if (kthread_should_stop())
break;
try_to_freeze();
dprintk(DBGLVL_THR, "thread running\n");
/* Dump the firmware debug message to console */
/* Polling this costs us 1-2% of the arm CPU */
/* convert this into a respnde to interrupt 0x7a */
saa7164_api_collect_debug(dev);
/* Monitor CPU load every 1 second */
if ((last_poll_time + 1000 /* ms */) < jiffies_to_msecs(jiffies)) {
saa7164_api_get_load_info(dev, &fwinfo);
last_poll_time = jiffies_to_msecs(jiffies);
}
}
dprintk(DBGLVL_THR, "thread exiting\n");
return 0;
}
static bool saa7164_enable_msi(struct pci_dev *pci_dev, struct saa7164_dev *dev)
{
int err;
if (!enable_msi) {
printk(KERN_WARNING "%s() MSI disabled by module parameter 'enable_msi'"
, __func__);
return false;
}
err = pci_enable_msi(pci_dev);
if (err) {
printk(KERN_ERR "%s() Failed to enable MSI interrupt. Falling back to a shared IRQ\n",
__func__);
return false;
}
/* no error - so request an msi interrupt */
err = request_irq(pci_dev->irq, saa7164_irq, 0,
dev->name, dev);
if (err) {
/* fall back to legacy interrupt */
printk(KERN_ERR "%s() Failed to get an MSI interrupt. Falling back to a shared IRQ\n",
__func__);
pci_disable_msi(pci_dev);
return false;
}
return true;
}
static int saa7164_initdev(struct pci_dev *pci_dev,
const struct pci_device_id *pci_id)
{
struct saa7164_dev *dev;
int err, i;
u32 version;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (NULL == dev)
return -ENOMEM;
err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
if (err < 0) {
dev_err(&pci_dev->dev, "v4l2_device_register failed\n");
goto fail_free;
}
/* pci init */
dev->pci = pci_dev;
if (pci_enable_device(pci_dev)) {
err = -EIO;
goto fail_free;
}
if (saa7164_dev_setup(dev) < 0) {
err = -EINVAL;
goto fail_dev;
}
/* print pci info */
dev->pci_rev = pci_dev->revision;
pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
dev->name,
pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
dev->pci_lat,
(unsigned long long)pci_resource_start(pci_dev, 0));
pci_set_master(pci_dev);
/* TODO */
err = dma_set_mask(&pci_dev->dev, 0xffffffff);
if (err) {
printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
goto fail_irq;
}
/* irq bit */
if (saa7164_enable_msi(pci_dev, dev)) {
dev->msi = true;
} else {
/* if we have an error (i.e. we don't have an interrupt)
or msi is not enabled - fallback to shared interrupt */
err = request_irq(pci_dev->irq, saa7164_irq,
IRQF_SHARED, dev->name, dev);
if (err < 0) {
printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
pci_dev->irq);
err = -EIO;
goto fail_irq;
}
}
pci_set_drvdata(pci_dev, dev);
/* Init the internal command list */
for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
dev->cmds[i].seqno = i;
dev->cmds[i].inuse = 0;
mutex_init(&dev->cmds[i].lock);
init_waitqueue_head(&dev->cmds[i].wait);
}
/* We need a deferred interrupt handler for cmd handling */
INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
/* Only load the firmware if we know the board */
if (dev->board != SAA7164_BOARD_UNKNOWN) {
err = saa7164_downloadfirmware(dev);
if (err < 0) {
printk(KERN_ERR
"Failed to boot firmware, no features registered\n");
goto fail_fw;
}
saa7164_get_descriptors(dev);
saa7164_dumpregs(dev, 0);
saa7164_getcurrentfirmwareversion(dev);
saa7164_getfirmwarestatus(dev);
err = saa7164_bus_setup(dev);
if (err < 0)
printk(KERN_ERR
"Failed to setup the bus, will continue\n");
saa7164_bus_dump(dev);
/* Ping the running firmware via the command bus and get the
* firmware version, this checks the bus is running OK.
*/
version = 0;
if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
dprintk(1, "Bus is operating correctly using version %d.%d.%d.%d (0x%x)\n",
(version & 0x0000fc00) >> 10,
(version & 0x000003e0) >> 5,
(version & 0x0000001f),
(version & 0xffff0000) >> 16,
version);
else
printk(KERN_ERR
"Failed to communicate with the firmware\n");
/* Bring up the I2C buses */
saa7164_i2c_register(&dev->i2c_bus[0]);
saa7164_i2c_register(&dev->i2c_bus[1]);
saa7164_i2c_register(&dev->i2c_bus[2]);
saa7164_gpio_setup(dev);
saa7164_card_setup(dev);
/* Parse the dynamic device configuration, find various
* media endpoints (MPEG, WMV, PS, TS) and cache their
* configuration details into the driver, so we can
* reference them later during simething_register() func,
* interrupt handlers, deferred work handlers etc.
*/
saa7164_api_enum_subdevs(dev);
/* Begin to create the video sub-systems and register funcs */
if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
if (saa7164_dvb_register(&dev->ports[SAA7164_PORT_TS1]) < 0) {
printk(KERN_ERR "%s() Failed to register dvb adapters on porta\n",
__func__);
}
}
if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
if (saa7164_dvb_register(&dev->ports[SAA7164_PORT_TS2]) < 0) {
printk(KERN_ERR"%s() Failed to register dvb adapters on portb\n",
__func__);
}
}
if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) {
if (saa7164_encoder_register(&dev->ports[SAA7164_PORT_ENC1]) < 0) {
printk(KERN_ERR"%s() Failed to register mpeg encoder\n",
__func__);
}
}
if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) {
if (saa7164_encoder_register(&dev->ports[SAA7164_PORT_ENC2]) < 0) {
printk(KERN_ERR"%s() Failed to register mpeg encoder\n",
__func__);
}
}
if (saa7164_boards[dev->board].porte == SAA7164_MPEG_VBI) {
if (saa7164_vbi_register(&dev->ports[SAA7164_PORT_VBI1]) < 0) {
printk(KERN_ERR"%s() Failed to register vbi device\n",
__func__);
}
}
if (saa7164_boards[dev->board].portf == SAA7164_MPEG_VBI) {
if (saa7164_vbi_register(&dev->ports[SAA7164_PORT_VBI2]) < 0) {
printk(KERN_ERR"%s() Failed to register vbi device\n",
__func__);
}
}
saa7164_api_set_debug(dev, fw_debug);
if (fw_debug) {
dev->kthread = kthread_run(saa7164_thread_function, dev,
"saa7164 debug");
if (IS_ERR(dev->kthread)) {
dev->kthread = NULL;
printk(KERN_ERR "%s() Failed to create debug kernel thread\n",
__func__);
}
}
} /* != BOARD_UNKNOWN */
else
printk(KERN_ERR "%s() Unsupported board detected, registering without firmware\n",
__func__);
dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug);
dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
fail_fw:
return 0;
fail_irq:
saa7164_dev_unregister(dev);
fail_dev:
pci_disable_device(pci_dev);
fail_free:
v4l2_device_unregister(&dev->v4l2_dev);
kfree(dev);
return err;
}
static void saa7164_shutdown(struct saa7164_dev *dev)
{
dprintk(1, "%s()\n", __func__);
}
static void saa7164_finidev(struct pci_dev *pci_dev)
{
struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
if (dev->board != SAA7164_BOARD_UNKNOWN) {
if (fw_debug && dev->kthread) {
kthread_stop(dev->kthread);
dev->kthread = NULL;
}
if (dev->firmwareloaded)
saa7164_api_set_debug(dev, 0x00);
}
saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
&dev->ports[SAA7164_PORT_ENC1].irq_interval);
saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
&dev->ports[SAA7164_PORT_ENC1].svc_interval);
saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
&dev->ports[SAA7164_PORT_ENC1].irq_svc_interval);
saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
&dev->ports[SAA7164_PORT_ENC1].read_interval);
saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
&dev->ports[SAA7164_PORT_ENC1].poll_interval);
saa7164_histogram_print(&dev->ports[SAA7164_PORT_VBI1],
&dev->ports[SAA7164_PORT_VBI1].read_interval);
saa7164_histogram_print(&dev->ports[SAA7164_PORT_VBI2],
&dev->ports[SAA7164_PORT_VBI2].poll_interval);
saa7164_shutdown(dev);
if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
saa7164_dvb_unregister(&dev->ports[SAA7164_PORT_TS1]);
if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
saa7164_dvb_unregister(&dev->ports[SAA7164_PORT_TS2]);
if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
saa7164_encoder_unregister(&dev->ports[SAA7164_PORT_ENC1]);
if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
saa7164_encoder_unregister(&dev->ports[SAA7164_PORT_ENC2]);
if (saa7164_boards[dev->board].porte == SAA7164_MPEG_VBI)
saa7164_vbi_unregister(&dev->ports[SAA7164_PORT_VBI1]);
if (saa7164_boards[dev->board].portf == SAA7164_MPEG_VBI)
saa7164_vbi_unregister(&dev->ports[SAA7164_PORT_VBI2]);
saa7164_i2c_unregister(&dev->i2c_bus[0]);
saa7164_i2c_unregister(&dev->i2c_bus[1]);
saa7164_i2c_unregister(&dev->i2c_bus[2]);
/* unregister stuff */
free_irq(pci_dev->irq, dev);
if (dev->msi) {
pci_disable_msi(pci_dev);
dev->msi = false;
}
pci_disable_device(pci_dev);
mutex_lock(&devlist);
list_del(&dev->devlist);
mutex_unlock(&devlist);
saa7164_dev_unregister(dev);
v4l2_device_unregister(&dev->v4l2_dev);
kfree(dev);
}
static const struct pci_device_id saa7164_pci_tbl[] = {
{
/* SAA7164 */
.vendor = 0x1131,
.device = 0x7164,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
}, {
/* --- end of list --- */
}
};
MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
static struct pci_driver saa7164_pci_driver = {
.name = "saa7164",
.id_table = saa7164_pci_tbl,
.probe = saa7164_initdev,
.remove = saa7164_finidev,
};
static int __init saa7164_init(void)
{
int ret = pci_register_driver(&saa7164_pci_driver);
if (ret)
return ret;
saa7164_debugfs_create();
pr_info("saa7164 driver loaded\n");
return 0;
}
static void __exit saa7164_fini(void)
{
saa7164_debugfs_remove();
pci_unregister_driver(&saa7164_pci_driver);
}
module_init(saa7164_init);
module_exit(saa7164_fini);
| linux-master | drivers/media/pci/saa7164/saa7164-core.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* This is the driver for the STA2x11 Video Input Port.
*
* Copyright (C) 2012 ST Microelectronics
* author: Federico Vaga <[email protected]>
* Copyright (C) 2010 WindRiver Systems, Inc.
* authors: Andreas Kies <[email protected]>
* Vlad Lungu <[email protected]>
*/
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/videodev2.h>
#include <linux/kmod.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <media/v4l2-common.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-dma-contig.h>
#include "sta2x11_vip.h"
#define DRV_VERSION "1.3"
#ifndef PCI_DEVICE_ID_STMICRO_VIP
#define PCI_DEVICE_ID_STMICRO_VIP 0xCC0D
#endif
#define MAX_FRAMES 4
/*Register offsets*/
#define DVP_CTL 0x00
#define DVP_TFO 0x04
#define DVP_TFS 0x08
#define DVP_BFO 0x0C
#define DVP_BFS 0x10
#define DVP_VTP 0x14
#define DVP_VBP 0x18
#define DVP_VMP 0x1C
#define DVP_ITM 0x98
#define DVP_ITS 0x9C
#define DVP_STA 0xA0
#define DVP_HLFLN 0xA8
#define DVP_RGB 0xC0
#define DVP_PKZ 0xF0
/*Register fields*/
#define DVP_CTL_ENA 0x00000001
#define DVP_CTL_RST 0x80000000
#define DVP_CTL_DIS (~0x00040001)
#define DVP_IT_VSB 0x00000008
#define DVP_IT_VST 0x00000010
#define DVP_IT_FIFO 0x00000020
#define DVP_HLFLN_SD 0x00000001
#define SAVE_COUNT 8
#define AUX_COUNT 3
#define IRQ_COUNT 1
struct vip_buffer {
struct vb2_v4l2_buffer vb;
struct list_head list;
dma_addr_t dma;
};
static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
{
return container_of(vb2, struct vip_buffer, vb);
}
/**
* struct sta2x11_vip - All internal data for one instance of device
* @v4l2_dev: device registered in v4l layer
* @video_dev: properties of our device
* @pdev: PCI device
* @adapter: contains I2C adapter information
* @register_save_area: All relevant register are saved here during suspend
* @decoder: contains information about video DAC
* @ctrl_hdl: handler for control framework
* @format: pixel format, fixed UYVY
* @std: video standard (e.g. PAL/NTSC)
* @input: input line for video signal ( 0 or 1 )
* @disabled: Device is in power down state
* @slock: for excluse access of registers
* @vb_vidq: queue maintained by videobuf2 layer
* @buffer_list: list of buffer in use
* @sequence: sequence number of acquired buffer
* @active: current active buffer
* @lock: used in videobuf2 callback
* @v4l_lock: serialize its video4linux ioctls
* @tcount: Number of top frames
* @bcount: Number of bottom frames
* @overflow: Number of FIFO overflows
* @iomem: hardware base address
* @config: I2C and gpio config from platform
*
* All non-local data is accessed via this structure.
*/
struct sta2x11_vip {
struct v4l2_device v4l2_dev;
struct video_device video_dev;
struct pci_dev *pdev;
struct i2c_adapter *adapter;
unsigned int register_save_area[IRQ_COUNT + SAVE_COUNT + AUX_COUNT];
struct v4l2_subdev *decoder;
struct v4l2_ctrl_handler ctrl_hdl;
struct v4l2_pix_format format;
v4l2_std_id std;
unsigned int input;
int disabled;
spinlock_t slock;
struct vb2_queue vb_vidq;
struct list_head buffer_list;
unsigned int sequence;
struct vip_buffer *active; /* current active buffer */
spinlock_t lock; /* Used in videobuf2 callback */
struct mutex v4l_lock;
/* Interrupt counters */
int tcount, bcount;
int overflow;
void __iomem *iomem; /* I/O Memory */
struct vip_config *config;
};
static const unsigned int registers_to_save[AUX_COUNT] = {
DVP_HLFLN, DVP_RGB, DVP_PKZ
};
static struct v4l2_pix_format formats_50[] = {
{ /*PAL interlaced */
.width = 720,
.height = 576,
.pixelformat = V4L2_PIX_FMT_UYVY,
.field = V4L2_FIELD_INTERLACED,
.bytesperline = 720 * 2,
.sizeimage = 720 * 2 * 576,
.colorspace = V4L2_COLORSPACE_SMPTE170M},
{ /*PAL top */
.width = 720,
.height = 288,
.pixelformat = V4L2_PIX_FMT_UYVY,
.field = V4L2_FIELD_TOP,
.bytesperline = 720 * 2,
.sizeimage = 720 * 2 * 288,
.colorspace = V4L2_COLORSPACE_SMPTE170M},
{ /*PAL bottom */
.width = 720,
.height = 288,
.pixelformat = V4L2_PIX_FMT_UYVY,
.field = V4L2_FIELD_BOTTOM,
.bytesperline = 720 * 2,
.sizeimage = 720 * 2 * 288,
.colorspace = V4L2_COLORSPACE_SMPTE170M},
};
static struct v4l2_pix_format formats_60[] = {
{ /*NTSC interlaced */
.width = 720,
.height = 480,
.pixelformat = V4L2_PIX_FMT_UYVY,
.field = V4L2_FIELD_INTERLACED,
.bytesperline = 720 * 2,
.sizeimage = 720 * 2 * 480,
.colorspace = V4L2_COLORSPACE_SMPTE170M},
{ /*NTSC top */
.width = 720,
.height = 240,
.pixelformat = V4L2_PIX_FMT_UYVY,
.field = V4L2_FIELD_TOP,
.bytesperline = 720 * 2,
.sizeimage = 720 * 2 * 240,
.colorspace = V4L2_COLORSPACE_SMPTE170M},
{ /*NTSC bottom */
.width = 720,
.height = 240,
.pixelformat = V4L2_PIX_FMT_UYVY,
.field = V4L2_FIELD_BOTTOM,
.bytesperline = 720 * 2,
.sizeimage = 720 * 2 * 240,
.colorspace = V4L2_COLORSPACE_SMPTE170M},
};
/* Write VIP register */
static inline void reg_write(struct sta2x11_vip *vip, unsigned int reg, u32 val)
{
iowrite32((val), (vip->iomem)+(reg));
}
/* Read VIP register */
static inline u32 reg_read(struct sta2x11_vip *vip, unsigned int reg)
{
return ioread32((vip->iomem)+(reg));
}
/* Start DMA acquisition */
static void start_dma(struct sta2x11_vip *vip, struct vip_buffer *vip_buf)
{
unsigned long offset = 0;
if (vip->format.field == V4L2_FIELD_INTERLACED)
offset = vip->format.width * 2;
spin_lock_irq(&vip->slock);
/* Enable acquisition */
reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) | DVP_CTL_ENA);
/* Set Top and Bottom Field memory address */
reg_write(vip, DVP_VTP, (u32)vip_buf->dma);
reg_write(vip, DVP_VBP, (u32)vip_buf->dma + offset);
spin_unlock_irq(&vip->slock);
}
/* Fetch the next buffer to activate */
static void vip_active_buf_next(struct sta2x11_vip *vip)
{
/* Get the next buffer */
spin_lock(&vip->lock);
if (list_empty(&vip->buffer_list)) {/* No available buffer */
spin_unlock(&vip->lock);
return;
}
vip->active = list_first_entry(&vip->buffer_list,
struct vip_buffer,
list);
/* Reset Top and Bottom counter */
vip->tcount = 0;
vip->bcount = 0;
spin_unlock(&vip->lock);
if (vb2_is_streaming(&vip->vb_vidq)) { /* streaming is on */
start_dma(vip, vip->active); /* start dma capture */
}
}
/* Videobuf2 Operations */
static int queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers, unsigned int *nplanes,
unsigned int sizes[], struct device *alloc_devs[])
{
struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
if (!(*nbuffers) || *nbuffers < MAX_FRAMES)
*nbuffers = MAX_FRAMES;
*nplanes = 1;
sizes[0] = vip->format.sizeimage;
vip->sequence = 0;
vip->active = NULL;
vip->tcount = 0;
vip->bcount = 0;
return 0;
};
static int buffer_init(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
vip_buf->dma = vb2_dma_contig_plane_dma_addr(vb, 0);
INIT_LIST_HEAD(&vip_buf->list);
return 0;
}
static int buffer_prepare(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
unsigned long size;
size = vip->format.sizeimage;
if (vb2_plane_size(vb, 0) < size) {
v4l2_err(&vip->v4l2_dev, "buffer too small (%lu < %lu)\n",
vb2_plane_size(vb, 0), size);
return -EINVAL;
}
vb2_set_plane_payload(&vip_buf->vb.vb2_buf, 0, size);
return 0;
}
static void buffer_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
spin_lock(&vip->lock);
list_add_tail(&vip_buf->list, &vip->buffer_list);
if (!vip->active) { /* No active buffer, active the first one */
vip->active = list_first_entry(&vip->buffer_list,
struct vip_buffer,
list);
if (vb2_is_streaming(&vip->vb_vidq)) /* streaming is on */
start_dma(vip, vip_buf); /* start dma capture */
}
spin_unlock(&vip->lock);
}
static void buffer_finish(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct sta2x11_vip *vip = vb2_get_drv_priv(vb->vb2_queue);
struct vip_buffer *vip_buf = to_vip_buffer(vbuf);
/* Buffer handled, remove it from the list */
spin_lock(&vip->lock);
list_del_init(&vip_buf->list);
spin_unlock(&vip->lock);
if (vb2_is_streaming(vb->vb2_queue))
vip_active_buf_next(vip);
}
static int start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
spin_lock_irq(&vip->slock);
/* Enable interrupt VSYNC Top and Bottom*/
reg_write(vip, DVP_ITM, DVP_IT_VSB | DVP_IT_VST);
spin_unlock_irq(&vip->slock);
if (count)
start_dma(vip, vip->active);
return 0;
}
/* abort streaming and wait for last buffer */
static void stop_streaming(struct vb2_queue *vq)
{
struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
struct vip_buffer *vip_buf, *node;
/* Disable acquisition */
reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
/* Disable all interrupts */
reg_write(vip, DVP_ITM, 0);
/* Release all active buffers */
spin_lock(&vip->lock);
list_for_each_entry_safe(vip_buf, node, &vip->buffer_list, list) {
vb2_buffer_done(&vip_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
list_del(&vip_buf->list);
}
spin_unlock(&vip->lock);
}
static const struct vb2_ops vip_video_qops = {
.queue_setup = queue_setup,
.buf_init = buffer_init,
.buf_prepare = buffer_prepare,
.buf_finish = buffer_finish,
.buf_queue = buffer_queue,
.start_streaming = start_streaming,
.stop_streaming = stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
/* File Operations */
static const struct v4l2_file_operations vip_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = vb2_fop_release,
.unlocked_ioctl = video_ioctl2,
.read = vb2_fop_read,
.mmap = vb2_fop_mmap,
.poll = vb2_fop_poll
};
/**
* vidioc_querycap - return capabilities of device
* @file: descriptor of device
* @cap: contains return values
* @priv: unused
*
* the capabilities of the device are returned
*
* return value: 0, no error.
*/
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
return 0;
}
/**
* vidioc_s_std - set video standard
* @file: descriptor of device
* @std: contains standard to be set
* @priv: unused
*
* the video standard is set
*
* return value: 0, no error.
*
* -EIO, no input signal detected
*
* other, returned from video DAC.
*/
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
{
struct sta2x11_vip *vip = video_drvdata(file);
/*
* This is here for backwards compatibility only.
* The use of V4L2_STD_ALL to trigger a querystd is non-standard.
*/
if (std == V4L2_STD_ALL) {
v4l2_subdev_call(vip->decoder, video, querystd, &std);
if (std == V4L2_STD_UNKNOWN)
return -EIO;
}
if (vip->std != std) {
vip->std = std;
if (V4L2_STD_525_60 & std)
vip->format = formats_60[0];
else
vip->format = formats_50[0];
}
return v4l2_subdev_call(vip->decoder, video, s_std, std);
}
/**
* vidioc_g_std - get video standard
* @file: descriptor of device
* @priv: unused
* @std: contains return values
*
* the current video standard is returned
*
* return value: 0, no error.
*/
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
{
struct sta2x11_vip *vip = video_drvdata(file);
*std = vip->std;
return 0;
}
/**
* vidioc_querystd - get possible video standards
* @file: descriptor of device
* @priv: unused
* @std: contains return values
*
* all possible video standards are returned
*
* return value: delivered by video DAC routine.
*/
static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
{
struct sta2x11_vip *vip = video_drvdata(file);
return v4l2_subdev_call(vip->decoder, video, querystd, std);
}
static int vidioc_enum_input(struct file *file, void *priv,
struct v4l2_input *inp)
{
if (inp->index > 1)
return -EINVAL;
inp->type = V4L2_INPUT_TYPE_CAMERA;
inp->std = V4L2_STD_ALL;
sprintf(inp->name, "Camera %u", inp->index);
return 0;
}
/**
* vidioc_s_input - set input line
* @file: descriptor of device
* @priv: unused
* @i: new input line number
*
* the current active input line is set
*
* return value: 0, no error.
*
* -EINVAL, line number out of range
*/
static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
{
struct sta2x11_vip *vip = video_drvdata(file);
int ret;
if (i > 1)
return -EINVAL;
ret = v4l2_subdev_call(vip->decoder, video, s_routing, i, 0, 0);
if (!ret)
vip->input = i;
return 0;
}
/**
* vidioc_g_input - return input line
* @file: descriptor of device
* @priv: unused
* @i: returned input line number
*
* the current active input line is returned
*
* return value: always 0.
*/
static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
struct sta2x11_vip *vip = video_drvdata(file);
*i = vip->input;
return 0;
}
/**
* vidioc_enum_fmt_vid_cap - return video capture format
* @file: descriptor of device
* @priv: unused
* @f: returned format information
*
* returns name and format of video capture
* Only UYVY is supported by hardware.
*
* return value: always 0.
*/
static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index != 0)
return -EINVAL;
f->pixelformat = V4L2_PIX_FMT_UYVY;
return 0;
}
/**
* vidioc_try_fmt_vid_cap - set video capture format
* @file: descriptor of device
* @priv: unused
* @f: new format
*
* new video format is set which includes width and
* field type. width is fixed to 720, no scaling.
* Only UYVY is supported by this hardware.
* the minimum height is 200, the maximum is 576 (PAL)
*
* return value: 0, no error
*
* -EINVAL, pixel or field format not supported
*
*/
static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct sta2x11_vip *vip = video_drvdata(file);
int interlace_lim;
if (V4L2_PIX_FMT_UYVY != f->fmt.pix.pixelformat) {
v4l2_warn(&vip->v4l2_dev, "Invalid format, only UYVY supported\n");
return -EINVAL;
}
if (V4L2_STD_525_60 & vip->std)
interlace_lim = 240;
else
interlace_lim = 288;
switch (f->fmt.pix.field) {
default:
case V4L2_FIELD_ANY:
if (interlace_lim < f->fmt.pix.height)
f->fmt.pix.field = V4L2_FIELD_INTERLACED;
else
f->fmt.pix.field = V4L2_FIELD_BOTTOM;
break;
case V4L2_FIELD_TOP:
case V4L2_FIELD_BOTTOM:
if (interlace_lim < f->fmt.pix.height)
f->fmt.pix.height = interlace_lim;
break;
case V4L2_FIELD_INTERLACED:
break;
}
/* It is the only supported format */
f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
f->fmt.pix.height &= ~1;
if (2 * interlace_lim < f->fmt.pix.height)
f->fmt.pix.height = 2 * interlace_lim;
if (200 > f->fmt.pix.height)
f->fmt.pix.height = 200;
f->fmt.pix.width = 720;
f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
f->fmt.pix.sizeimage = f->fmt.pix.width * 2 * f->fmt.pix.height;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
/**
* vidioc_s_fmt_vid_cap - set current video format parameters
* @file: descriptor of device
* @priv: unused
* @f: returned format information
*
* set new capture format
* return value: 0, no error
*
* other, delivered by video DAC routine.
*/
static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct sta2x11_vip *vip = video_drvdata(file);
unsigned int t_stop, b_stop, pitch;
int ret;
ret = vidioc_try_fmt_vid_cap(file, priv, f);
if (ret)
return ret;
if (vb2_is_busy(&vip->vb_vidq)) {
/* Can't change format during acquisition */
v4l2_err(&vip->v4l2_dev, "device busy\n");
return -EBUSY;
}
vip->format = f->fmt.pix;
switch (vip->format.field) {
case V4L2_FIELD_INTERLACED:
t_stop = ((vip->format.height / 2 - 1) << 16) |
(2 * vip->format.width - 1);
b_stop = t_stop;
pitch = 4 * vip->format.width;
break;
case V4L2_FIELD_TOP:
t_stop = ((vip->format.height - 1) << 16) |
(2 * vip->format.width - 1);
b_stop = (0 << 16) | (2 * vip->format.width - 1);
pitch = 2 * vip->format.width;
break;
case V4L2_FIELD_BOTTOM:
t_stop = (0 << 16) | (2 * vip->format.width - 1);
b_stop = (vip->format.height << 16) |
(2 * vip->format.width - 1);
pitch = 2 * vip->format.width;
break;
default:
v4l2_err(&vip->v4l2_dev, "unknown field format\n");
return -EINVAL;
}
spin_lock_irq(&vip->slock);
/* Y-X Top Field Offset */
reg_write(vip, DVP_TFO, 0);
/* Y-X Bottom Field Offset */
reg_write(vip, DVP_BFO, 0);
/* Y-X Top Field Stop*/
reg_write(vip, DVP_TFS, t_stop);
/* Y-X Bottom Field Stop */
reg_write(vip, DVP_BFS, b_stop);
/* Video Memory Pitch */
reg_write(vip, DVP_VMP, pitch);
spin_unlock_irq(&vip->slock);
return 0;
}
/**
* vidioc_g_fmt_vid_cap - get current video format parameters
* @file: descriptor of device
* @priv: unused
* @f: contains format information
*
* returns current video format parameters
*
* return value: 0, always successful
*/
static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct sta2x11_vip *vip = video_drvdata(file);
f->fmt.pix = vip->format;
return 0;
}
static const struct v4l2_ioctl_ops vip_ioctl_ops = {
.vidioc_querycap = vidioc_querycap,
/* FMT handling */
.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
/* Buffer handlers */
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
/* Stream on/off */
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
/* Standard handling */
.vidioc_g_std = vidioc_g_std,
.vidioc_s_std = vidioc_s_std,
.vidioc_querystd = vidioc_querystd,
/* Input handling */
.vidioc_enum_input = vidioc_enum_input,
.vidioc_g_input = vidioc_g_input,
.vidioc_s_input = vidioc_s_input,
/* Log status ioctl */
.vidioc_log_status = v4l2_ctrl_log_status,
/* Event handling */
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct video_device video_dev_template = {
.name = KBUILD_MODNAME,
.release = video_device_release_empty,
.fops = &vip_fops,
.ioctl_ops = &vip_ioctl_ops,
.tvnorms = V4L2_STD_ALL,
.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING,
};
/**
* vip_irq - interrupt routine
* @irq: Number of interrupt ( not used, correct number is assumed )
* @vip: local data structure containing all information
*
* check for both frame interrupts set ( top and bottom ).
* check FIFO overflow, but limit number of log messages after open.
* signal a complete buffer if done
*
* return value: IRQ_NONE, interrupt was not generated by VIP
*
* IRQ_HANDLED, interrupt done.
*/
static irqreturn_t vip_irq(int irq, struct sta2x11_vip *vip)
{
unsigned int status;
status = reg_read(vip, DVP_ITS);
if (!status) /* No interrupt to handle */
return IRQ_NONE;
if (status & DVP_IT_FIFO)
if (vip->overflow++ > 5)
pr_info("VIP: fifo overflow\n");
if ((status & DVP_IT_VST) && (status & DVP_IT_VSB)) {
/* this is bad, we are too slow, hope the condition is gone
* on the next frame */
return IRQ_HANDLED;
}
if (status & DVP_IT_VST)
if ((++vip->tcount) < 2)
return IRQ_HANDLED;
if (status & DVP_IT_VSB) {
vip->bcount++;
return IRQ_HANDLED;
}
if (vip->active) { /* Acquisition is over on this buffer */
/* Disable acquisition */
reg_write(vip, DVP_CTL, reg_read(vip, DVP_CTL) & ~DVP_CTL_ENA);
/* Remove the active buffer from the list */
vip->active->vb.vb2_buf.timestamp = ktime_get_ns();
vip->active->vb.sequence = vip->sequence++;
vb2_buffer_done(&vip->active->vb.vb2_buf, VB2_BUF_STATE_DONE);
}
return IRQ_HANDLED;
}
static void sta2x11_vip_init_register(struct sta2x11_vip *vip)
{
/* Register initialization */
spin_lock_irq(&vip->slock);
/* Clean interrupt */
reg_read(vip, DVP_ITS);
/* Enable Half Line per vertical */
reg_write(vip, DVP_HLFLN, DVP_HLFLN_SD);
/* Reset VIP control */
reg_write(vip, DVP_CTL, DVP_CTL_RST);
/* Clear VIP control */
reg_write(vip, DVP_CTL, 0);
spin_unlock_irq(&vip->slock);
}
static void sta2x11_vip_clear_register(struct sta2x11_vip *vip)
{
spin_lock_irq(&vip->slock);
/* Disable interrupt */
reg_write(vip, DVP_ITM, 0);
/* Reset VIP Control */
reg_write(vip, DVP_CTL, DVP_CTL_RST);
/* Clear VIP Control */
reg_write(vip, DVP_CTL, 0);
/* Clean VIP Interrupt */
reg_read(vip, DVP_ITS);
spin_unlock_irq(&vip->slock);
}
static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
{
int err;
err = dma_set_coherent_mask(&vip->pdev->dev, DMA_BIT_MASK(29));
if (err) {
v4l2_err(&vip->v4l2_dev, "Cannot configure coherent mask");
return err;
}
memset(&vip->vb_vidq, 0, sizeof(struct vb2_queue));
vip->vb_vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vip->vb_vidq.io_modes = VB2_MMAP | VB2_READ;
vip->vb_vidq.drv_priv = vip;
vip->vb_vidq.buf_struct_size = sizeof(struct vip_buffer);
vip->vb_vidq.ops = &vip_video_qops;
vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
vip->vb_vidq.dev = &vip->pdev->dev;
vip->vb_vidq.lock = &vip->v4l_lock;
err = vb2_queue_init(&vip->vb_vidq);
if (err)
return err;
INIT_LIST_HEAD(&vip->buffer_list);
spin_lock_init(&vip->lock);
return 0;
}
static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
{
/*
* Inititialize an empty control so VIP can inerithing controls
* from ADV7180
*/
v4l2_ctrl_handler_init(&vip->ctrl_hdl, 0);
vip->v4l2_dev.ctrl_handler = &vip->ctrl_hdl;
if (vip->ctrl_hdl.error) {
int err = vip->ctrl_hdl.error;
v4l2_ctrl_handler_free(&vip->ctrl_hdl);
return err;
}
return 0;
}
/**
* vip_gpio_reserve - reserve gpio pin
* @dev: device
* @pin: GPIO pin number
* @dir: direction, input or output
* @name: GPIO pin name
*
*/
static int vip_gpio_reserve(struct device *dev, int pin, int dir,
const char *name)
{
struct gpio_desc *desc = gpio_to_desc(pin);
int ret = -ENODEV;
if (!gpio_is_valid(pin))
return ret;
ret = gpio_request(pin, name);
if (ret) {
dev_err(dev, "Failed to allocate pin %d (%s)\n", pin, name);
return ret;
}
ret = gpiod_direction_output(desc, dir);
if (ret) {
dev_err(dev, "Failed to set direction for pin %d (%s)\n",
pin, name);
gpio_free(pin);
return ret;
}
ret = gpiod_export(desc, false);
if (ret) {
dev_err(dev, "Failed to export pin %d (%s)\n", pin, name);
gpio_free(pin);
return ret;
}
return 0;
}
/**
* vip_gpio_release - release gpio pin
* @dev: device
* @pin: GPIO pin number
* @name: GPIO pin name
*
*/
static void vip_gpio_release(struct device *dev, int pin, const char *name)
{
if (gpio_is_valid(pin)) {
struct gpio_desc *desc = gpio_to_desc(pin);
dev_dbg(dev, "releasing pin %d (%s)\n", pin, name);
gpiod_unexport(desc);
gpio_free(pin);
}
}
/**
* sta2x11_vip_init_one - init one instance of video device
* @pdev: PCI device
* @ent: (not used)
*
* allocate reset pins for DAC.
* Reset video DAC, this is done via reset line.
* allocate memory for managing device
* request interrupt
* map IO region
* register device
* find and initialize video DAC
*
* return value: 0, no error
*
* -ENOMEM, no memory
*
* -ENODEV, device could not be detected or registered
*/
static int sta2x11_vip_init_one(struct pci_dev *pdev,
const struct pci_device_id *ent)
{
int ret;
struct sta2x11_vip *vip;
struct vip_config *config;
/* Check if hardware support 26-bit DMA */
if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(26))) {
dev_err(&pdev->dev, "26-bit DMA addressing not available\n");
return -EINVAL;
}
/* Enable PCI */
ret = pci_enable_device(pdev);
if (ret)
return ret;
/* Get VIP platform data */
config = dev_get_platdata(&pdev->dev);
if (!config) {
dev_info(&pdev->dev, "VIP slot disabled\n");
ret = -EINVAL;
goto disable;
}
/* Power configuration */
ret = vip_gpio_reserve(&pdev->dev, config->pwr_pin, 0,
config->pwr_name);
if (ret)
goto disable;
ret = vip_gpio_reserve(&pdev->dev, config->reset_pin, 0,
config->reset_name);
if (ret) {
vip_gpio_release(&pdev->dev, config->pwr_pin,
config->pwr_name);
goto disable;
}
if (gpio_is_valid(config->pwr_pin)) {
/* Datasheet says 5ms between PWR and RST */
usleep_range(5000, 25000);
gpio_direction_output(config->pwr_pin, 1);
}
if (gpio_is_valid(config->reset_pin)) {
/* Datasheet says 5ms between PWR and RST */
usleep_range(5000, 25000);
gpio_direction_output(config->reset_pin, 1);
}
usleep_range(5000, 25000);
/* Allocate a new VIP instance */
vip = kzalloc(sizeof(struct sta2x11_vip), GFP_KERNEL);
if (!vip) {
ret = -ENOMEM;
goto release_gpios;
}
vip->pdev = pdev;
vip->std = V4L2_STD_PAL;
vip->format = formats_50[0];
vip->config = config;
mutex_init(&vip->v4l_lock);
ret = sta2x11_vip_init_controls(vip);
if (ret)
goto free_mem;
ret = v4l2_device_register(&pdev->dev, &vip->v4l2_dev);
if (ret)
goto free_mem;
dev_dbg(&pdev->dev, "BAR #0 at 0x%lx 0x%lx irq %d\n",
(unsigned long)pci_resource_start(pdev, 0),
(unsigned long)pci_resource_len(pdev, 0), pdev->irq);
pci_set_master(pdev);
ret = pci_request_regions(pdev, KBUILD_MODNAME);
if (ret)
goto unreg;
vip->iomem = pci_iomap(pdev, 0, 0x100);
if (!vip->iomem) {
ret = -ENOMEM;
goto release;
}
pci_enable_msi(pdev);
/* Initialize buffer */
ret = sta2x11_vip_init_buffer(vip);
if (ret)
goto unmap;
spin_lock_init(&vip->slock);
ret = request_irq(pdev->irq,
(irq_handler_t) vip_irq,
IRQF_SHARED, KBUILD_MODNAME, vip);
if (ret) {
dev_err(&pdev->dev, "request_irq failed\n");
ret = -ENODEV;
goto release_buf;
}
/* Initialize and register video device */
vip->video_dev = video_dev_template;
vip->video_dev.v4l2_dev = &vip->v4l2_dev;
vip->video_dev.queue = &vip->vb_vidq;
vip->video_dev.lock = &vip->v4l_lock;
video_set_drvdata(&vip->video_dev, vip);
ret = video_register_device(&vip->video_dev, VFL_TYPE_VIDEO, -1);
if (ret)
goto vrelease;
/* Get ADV7180 subdevice */
vip->adapter = i2c_get_adapter(vip->config->i2c_id);
if (!vip->adapter) {
ret = -ENODEV;
dev_err(&pdev->dev, "no I2C adapter found\n");
goto vunreg;
}
vip->decoder = v4l2_i2c_new_subdev(&vip->v4l2_dev, vip->adapter,
"adv7180", vip->config->i2c_addr,
NULL);
if (!vip->decoder) {
ret = -ENODEV;
dev_err(&pdev->dev, "no decoder found\n");
goto vunreg;
}
i2c_put_adapter(vip->adapter);
v4l2_subdev_call(vip->decoder, core, init, 0);
sta2x11_vip_init_register(vip);
dev_info(&pdev->dev, "STA2X11 Video Input Port (VIP) loaded\n");
return 0;
vunreg:
video_set_drvdata(&vip->video_dev, NULL);
vrelease:
vb2_video_unregister_device(&vip->video_dev);
free_irq(pdev->irq, vip);
release_buf:
pci_disable_msi(pdev);
unmap:
pci_iounmap(pdev, vip->iomem);
release:
pci_release_regions(pdev);
unreg:
v4l2_device_unregister(&vip->v4l2_dev);
free_mem:
kfree(vip);
release_gpios:
vip_gpio_release(&pdev->dev, config->reset_pin, config->reset_name);
vip_gpio_release(&pdev->dev, config->pwr_pin, config->pwr_name);
disable:
/*
* do not call pci_disable_device on sta2x11 because it break all
* other Bus masters on this EP
*/
return ret;
}
/**
* sta2x11_vip_remove_one - release device
* @pdev: PCI device
*
* Undo everything done in .._init_one
*
* unregister video device
* free interrupt
* unmap ioadresses
* free memory
* free GPIO pins
*/
static void sta2x11_vip_remove_one(struct pci_dev *pdev)
{
struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
struct sta2x11_vip *vip =
container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
sta2x11_vip_clear_register(vip);
video_set_drvdata(&vip->video_dev, NULL);
vb2_video_unregister_device(&vip->video_dev);
free_irq(pdev->irq, vip);
pci_disable_msi(pdev);
pci_iounmap(pdev, vip->iomem);
pci_release_regions(pdev);
v4l2_device_unregister(&vip->v4l2_dev);
vip_gpio_release(&pdev->dev, vip->config->pwr_pin,
vip->config->pwr_name);
vip_gpio_release(&pdev->dev, vip->config->reset_pin,
vip->config->reset_name);
kfree(vip);
/*
* do not call pci_disable_device on sta2x11 because it break all
* other Bus masters on this EP
*/
}
/**
* sta2x11_vip_suspend - set device into power save mode
* @dev_d: PCI device
*
* all relevant registers are saved and an attempt to set a new state is made.
*
* return value: 0 always indicate success,
* even if device could not be disabled. (workaround for hardware problem)
*/
static int __maybe_unused sta2x11_vip_suspend(struct device *dev_d)
{
struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
struct sta2x11_vip *vip =
container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
unsigned long flags;
int i;
spin_lock_irqsave(&vip->slock, flags);
vip->register_save_area[0] = reg_read(vip, DVP_CTL);
reg_write(vip, DVP_CTL, vip->register_save_area[0] & DVP_CTL_DIS);
vip->register_save_area[SAVE_COUNT] = reg_read(vip, DVP_ITM);
reg_write(vip, DVP_ITM, 0);
for (i = 1; i < SAVE_COUNT; i++)
vip->register_save_area[i] = reg_read(vip, 4 * i);
for (i = 0; i < AUX_COUNT; i++)
vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i] =
reg_read(vip, registers_to_save[i]);
spin_unlock_irqrestore(&vip->slock, flags);
vip->disabled = 1;
pr_info("VIP: suspend\n");
return 0;
}
/**
* sta2x11_vip_resume - resume device operation
* @dev_d : PCI device
*
* return value: 0, no error.
*
* other, could not set device to power on state.
*/
static int __maybe_unused sta2x11_vip_resume(struct device *dev_d)
{
struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
struct sta2x11_vip *vip =
container_of(v4l2_dev, struct sta2x11_vip, v4l2_dev);
unsigned long flags;
int i;
pr_info("VIP: resume\n");
vip->disabled = 0;
spin_lock_irqsave(&vip->slock, flags);
for (i = 1; i < SAVE_COUNT; i++)
reg_write(vip, 4 * i, vip->register_save_area[i]);
for (i = 0; i < AUX_COUNT; i++)
reg_write(vip, registers_to_save[i],
vip->register_save_area[SAVE_COUNT + IRQ_COUNT + i]);
reg_write(vip, DVP_CTL, vip->register_save_area[0]);
reg_write(vip, DVP_ITM, vip->register_save_area[SAVE_COUNT]);
spin_unlock_irqrestore(&vip->slock, flags);
return 0;
}
static const struct pci_device_id sta2x11_vip_pci_tbl[] = {
{PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIP)},
{0,}
};
static SIMPLE_DEV_PM_OPS(sta2x11_vip_pm_ops,
sta2x11_vip_suspend,
sta2x11_vip_resume);
static struct pci_driver sta2x11_vip_driver = {
.name = KBUILD_MODNAME,
.probe = sta2x11_vip_init_one,
.remove = sta2x11_vip_remove_one,
.id_table = sta2x11_vip_pci_tbl,
.driver.pm = &sta2x11_vip_pm_ops,
};
static int __init sta2x11_vip_init_module(void)
{
return pci_register_driver(&sta2x11_vip_driver);
}
static void __exit sta2x11_vip_exit_module(void)
{
pci_unregister_driver(&sta2x11_vip_driver);
}
#ifdef MODULE
module_init(sta2x11_vip_init_module);
module_exit(sta2x11_vip_exit_module);
#else
late_initcall_sync(sta2x11_vip_init_module);
#endif
MODULE_DESCRIPTION("STA2X11 Video Input Port driver");
MODULE_AUTHOR("Wind River");
MODULE_LICENSE("GPL v2");
MODULE_VERSION(DRV_VERSION);
MODULE_DEVICE_TABLE(pci, sta2x11_vip_pci_tbl);
| linux-master | drivers/media/pci/sta2x11/sta2x11_vip.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* dm1105.c - driver for DVB cards based on SDMC DM1105 PCI chip
*
* Copyright (C) 2008 Igor M. Liplianin <[email protected]>
*/
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <media/rc-core.h>
#include <media/demux.h>
#include <media/dmxdev.h>
#include <media/dvb_demux.h>
#include <media/dvb_frontend.h>
#include <media/dvb_net.h>
#include <media/dvbdev.h>
#include "dvb-pll.h"
#include "stv0299.h"
#include "stv0288.h"
#include "stb6000.h"
#include "si21xx.h"
#include "cx24116.h"
#include "z0194a.h"
#include "ts2020.h"
#include "ds3000.h"
#define MODULE_NAME "dm1105"
#define UNSET (-1U)
#define DM1105_BOARD_NOAUTO UNSET
#define DM1105_BOARD_UNKNOWN 0
#define DM1105_BOARD_DVBWORLD_2002 1
#define DM1105_BOARD_DVBWORLD_2004 2
#define DM1105_BOARD_AXESS_DM05 3
#define DM1105_BOARD_UNBRANDED_I2C_ON_GPIO 4
/* ----------------------------------------------- */
/*
* PCI ID's
*/
#ifndef PCI_VENDOR_ID_TRIGEM
#define PCI_VENDOR_ID_TRIGEM 0x109f
#endif
#ifndef PCI_VENDOR_ID_AXESS
#define PCI_VENDOR_ID_AXESS 0x195d
#endif
#ifndef PCI_DEVICE_ID_DM1105
#define PCI_DEVICE_ID_DM1105 0x036f
#endif
#ifndef PCI_DEVICE_ID_DW2002
#define PCI_DEVICE_ID_DW2002 0x2002
#endif
#ifndef PCI_DEVICE_ID_DW2004
#define PCI_DEVICE_ID_DW2004 0x2004
#endif
#ifndef PCI_DEVICE_ID_DM05
#define PCI_DEVICE_ID_DM05 0x1105
#endif
/* ----------------------------------------------- */
/* sdmc dm1105 registers */
/* TS Control */
#define DM1105_TSCTR 0x00
#define DM1105_DTALENTH 0x04
/* GPIO Interface */
#define DM1105_GPIOVAL 0x08
#define DM1105_GPIOCTR 0x0c
/* PID serial number */
#define DM1105_PIDN 0x10
/* Odd-even secret key select */
#define DM1105_CWSEL 0x14
/* Host Command Interface */
#define DM1105_HOST_CTR 0x18
#define DM1105_HOST_AD 0x1c
/* PCI Interface */
#define DM1105_CR 0x30
#define DM1105_RST 0x34
#define DM1105_STADR 0x38
#define DM1105_RLEN 0x3c
#define DM1105_WRP 0x40
#define DM1105_INTCNT 0x44
#define DM1105_INTMAK 0x48
#define DM1105_INTSTS 0x4c
/* CW Value */
#define DM1105_ODD 0x50
#define DM1105_EVEN 0x58
/* PID Value */
#define DM1105_PID 0x60
/* IR Control */
#define DM1105_IRCTR 0x64
#define DM1105_IRMODE 0x68
#define DM1105_SYSTEMCODE 0x6c
#define DM1105_IRCODE 0x70
/* Unknown Values */
#define DM1105_ENCRYPT 0x74
#define DM1105_VER 0x7c
/* I2C Interface */
#define DM1105_I2CCTR 0x80
#define DM1105_I2CSTS 0x81
#define DM1105_I2CDAT 0x82
#define DM1105_I2C_RA 0x83
/* ----------------------------------------------- */
/* Interrupt Mask Bits */
#define INTMAK_TSIRQM 0x01
#define INTMAK_HIRQM 0x04
#define INTMAK_IRM 0x08
#define INTMAK_ALLMASK (INTMAK_TSIRQM | \
INTMAK_HIRQM | \
INTMAK_IRM)
#define INTMAK_NONEMASK 0x00
/* Interrupt Status Bits */
#define INTSTS_TSIRQ 0x01
#define INTSTS_HIRQ 0x04
#define INTSTS_IR 0x08
/* IR Control Bits */
#define DM1105_IR_EN 0x01
#define DM1105_SYS_CHK 0x02
#define DM1105_REP_FLG 0x08
/* EEPROM addr */
#define IIC_24C01_addr 0xa0
/* Max board count */
#define DM1105_MAX 0x04
#define DRIVER_NAME "dm1105"
#define DM1105_I2C_GPIO_NAME "dm1105-gpio"
#define DM1105_DMA_PACKETS 47
#define DM1105_DMA_PACKET_LENGTH (128*4)
#define DM1105_DMA_BYTES (128 * 4 * DM1105_DMA_PACKETS)
/* */
#define GPIO08 (1 << 8)
#define GPIO13 (1 << 13)
#define GPIO14 (1 << 14)
#define GPIO15 (1 << 15)
#define GPIO16 (1 << 16)
#define GPIO17 (1 << 17)
#define GPIO_ALL 0x03ffff
/* GPIO's for LNB power control */
#define DM1105_LNB_MASK (GPIO_ALL & ~(GPIO14 | GPIO13))
#define DM1105_LNB_OFF GPIO17
#define DM1105_LNB_13V (GPIO16 | GPIO08)
#define DM1105_LNB_18V GPIO08
/* GPIO's for LNB power control for Axess DM05 */
#define DM05_LNB_MASK (GPIO_ALL & ~(GPIO14 | GPIO13))
#define DM05_LNB_OFF GPIO17/* actually 13v */
#define DM05_LNB_13V GPIO17
#define DM05_LNB_18V (GPIO17 | GPIO16)
/* GPIO's for LNB power control for unbranded with I2C on GPIO */
#define UNBR_LNB_MASK (GPIO17 | GPIO16)
#define UNBR_LNB_OFF 0
#define UNBR_LNB_13V GPIO17
#define UNBR_LNB_18V (GPIO17 | GPIO16)
static unsigned int card[] = {[0 ... 3] = UNSET };
module_param_array(card, int, NULL, 0444);
MODULE_PARM_DESC(card, "card type");
static int ir_debug;
module_param(ir_debug, int, 0644);
MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");
static unsigned int dm1105_devcount;
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
struct dm1105_board {
char *name;
struct {
u32 mask, off, v13, v18;
} lnb;
u32 gpio_scl, gpio_sda;
};
struct dm1105_subid {
u16 subvendor;
u16 subdevice;
u32 card;
};
static const struct dm1105_board dm1105_boards[] = {
[DM1105_BOARD_UNKNOWN] = {
.name = "UNKNOWN/GENERIC",
.lnb = {
.mask = DM1105_LNB_MASK,
.off = DM1105_LNB_OFF,
.v13 = DM1105_LNB_13V,
.v18 = DM1105_LNB_18V,
},
},
[DM1105_BOARD_DVBWORLD_2002] = {
.name = "DVBWorld PCI 2002",
.lnb = {
.mask = DM1105_LNB_MASK,
.off = DM1105_LNB_OFF,
.v13 = DM1105_LNB_13V,
.v18 = DM1105_LNB_18V,
},
},
[DM1105_BOARD_DVBWORLD_2004] = {
.name = "DVBWorld PCI 2004",
.lnb = {
.mask = DM1105_LNB_MASK,
.off = DM1105_LNB_OFF,
.v13 = DM1105_LNB_13V,
.v18 = DM1105_LNB_18V,
},
},
[DM1105_BOARD_AXESS_DM05] = {
.name = "Axess/EasyTv DM05",
.lnb = {
.mask = DM05_LNB_MASK,
.off = DM05_LNB_OFF,
.v13 = DM05_LNB_13V,
.v18 = DM05_LNB_18V,
},
},
[DM1105_BOARD_UNBRANDED_I2C_ON_GPIO] = {
.name = "Unbranded DM1105 with i2c on GPIOs",
.lnb = {
.mask = UNBR_LNB_MASK,
.off = UNBR_LNB_OFF,
.v13 = UNBR_LNB_13V,
.v18 = UNBR_LNB_18V,
},
.gpio_scl = GPIO14,
.gpio_sda = GPIO13,
},
};
static const struct dm1105_subid dm1105_subids[] = {
{
.subvendor = 0x0000,
.subdevice = 0x2002,
.card = DM1105_BOARD_DVBWORLD_2002,
}, {
.subvendor = 0x0001,
.subdevice = 0x2002,
.card = DM1105_BOARD_DVBWORLD_2002,
}, {
.subvendor = 0x0000,
.subdevice = 0x2004,
.card = DM1105_BOARD_DVBWORLD_2004,
}, {
.subvendor = 0x0001,
.subdevice = 0x2004,
.card = DM1105_BOARD_DVBWORLD_2004,
}, {
.subvendor = 0x195d,
.subdevice = 0x1105,
.card = DM1105_BOARD_AXESS_DM05,
},
};
static void dm1105_card_list(struct pci_dev *pci)
{
int i;
if (0 == pci->subsystem_vendor &&
0 == pci->subsystem_device) {
printk(KERN_ERR
"dm1105: Your board has no valid PCI Subsystem ID\n"
"dm1105: and thus can't be autodetected\n"
"dm1105: Please pass card=<n> insmod option to\n"
"dm1105: workaround that. Redirect complaints to\n"
"dm1105: the vendor of the TV card. Best regards,\n"
"dm1105: -- tux\n");
} else {
printk(KERN_ERR
"dm1105: Your board isn't known (yet) to the driver.\n"
"dm1105: You can try to pick one of the existing\n"
"dm1105: card configs via card=<n> insmod option.\n"
"dm1105: Updating to the latest version might help\n"
"dm1105: as well.\n");
}
printk(KERN_ERR "Here is a list of valid choices for the card=<n> insmod option:\n");
for (i = 0; i < ARRAY_SIZE(dm1105_boards); i++)
printk(KERN_ERR "dm1105: card=%d -> %s\n",
i, dm1105_boards[i].name);
}
/* infrared remote control */
struct infrared {
struct rc_dev *dev;
char input_phys[32];
struct work_struct work;
u32 ir_command;
};
struct dm1105_dev {
/* pci */
struct pci_dev *pdev;
u8 __iomem *io_mem;
/* ir */
struct infrared ir;
/* dvb */
struct dmx_frontend hw_frontend;
struct dmx_frontend mem_frontend;
struct dmxdev dmxdev;
struct dvb_adapter dvb_adapter;
struct dvb_demux demux;
struct dvb_frontend *fe;
struct dvb_net dvbnet;
unsigned int full_ts_users;
unsigned int boardnr;
int nr;
/* i2c */
struct i2c_adapter i2c_adap;
struct i2c_adapter i2c_bb_adap;
struct i2c_algo_bit_data i2c_bit;
/* irq */
struct work_struct work;
struct workqueue_struct *wq;
char wqn[16];
/* dma */
dma_addr_t dma_addr;
unsigned char *ts_buf;
u32 wrp;
u32 nextwrp;
u32 buffer_size;
unsigned int PacketErrorCount;
unsigned int dmarst;
spinlock_t lock;
};
#define dm_io_mem(reg) ((unsigned long)(&dev->io_mem[reg]))
#define dm_readb(reg) inb(dm_io_mem(reg))
#define dm_writeb(reg, value) outb((value), (dm_io_mem(reg)))
#define dm_readw(reg) inw(dm_io_mem(reg))
#define dm_writew(reg, value) outw((value), (dm_io_mem(reg)))
#define dm_readl(reg) inl(dm_io_mem(reg))
#define dm_writel(reg, value) outl((value), (dm_io_mem(reg)))
#define dm_andorl(reg, mask, value) \
outl((inl(dm_io_mem(reg)) & ~(mask)) |\
((value) & (mask)), (dm_io_mem(reg)))
#define dm_setl(reg, bit) dm_andorl((reg), (bit), (bit))
#define dm_clearl(reg, bit) dm_andorl((reg), (bit), 0)
/* The chip has 18 GPIOs. In HOST mode GPIO's used as 15 bit address lines,
so we can use only 3 GPIO's from GPIO15 to GPIO17.
Here I don't check whether HOST is enebled as it is not implemented yet.
*/
static void dm1105_gpio_set(struct dm1105_dev *dev, u32 mask)
{
if (mask & 0xfffc0000)
printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);
if (mask & 0x0003ffff)
dm_setl(DM1105_GPIOVAL, mask & 0x0003ffff);
}
static void dm1105_gpio_clear(struct dm1105_dev *dev, u32 mask)
{
if (mask & 0xfffc0000)
printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);
if (mask & 0x0003ffff)
dm_clearl(DM1105_GPIOVAL, mask & 0x0003ffff);
}
static void dm1105_gpio_andor(struct dm1105_dev *dev, u32 mask, u32 val)
{
if (mask & 0xfffc0000)
printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);
if (mask & 0x0003ffff)
dm_andorl(DM1105_GPIOVAL, mask & 0x0003ffff, val);
}
static u32 dm1105_gpio_get(struct dm1105_dev *dev, u32 mask)
{
if (mask & 0xfffc0000)
printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);
if (mask & 0x0003ffff)
return dm_readl(DM1105_GPIOVAL) & mask & 0x0003ffff;
return 0;
}
static void dm1105_gpio_enable(struct dm1105_dev *dev, u32 mask, int asoutput)
{
if (mask & 0xfffc0000)
printk(KERN_ERR "%s: Only 18 GPIO's are allowed\n", __func__);
if ((mask & 0x0003ffff) && asoutput)
dm_clearl(DM1105_GPIOCTR, mask & 0x0003ffff);
else if ((mask & 0x0003ffff) && !asoutput)
dm_setl(DM1105_GPIOCTR, mask & 0x0003ffff);
}
static void dm1105_setline(struct dm1105_dev *dev, u32 line, int state)
{
if (state)
dm1105_gpio_enable(dev, line, 0);
else {
dm1105_gpio_enable(dev, line, 1);
dm1105_gpio_clear(dev, line);
}
}
static void dm1105_setsda(void *data, int state)
{
struct dm1105_dev *dev = data;
dm1105_setline(dev, dm1105_boards[dev->boardnr].gpio_sda, state);
}
static void dm1105_setscl(void *data, int state)
{
struct dm1105_dev *dev = data;
dm1105_setline(dev, dm1105_boards[dev->boardnr].gpio_scl, state);
}
static int dm1105_getsda(void *data)
{
struct dm1105_dev *dev = data;
return dm1105_gpio_get(dev, dm1105_boards[dev->boardnr].gpio_sda)
? 1 : 0;
}
static int dm1105_getscl(void *data)
{
struct dm1105_dev *dev = data;
return dm1105_gpio_get(dev, dm1105_boards[dev->boardnr].gpio_scl)
? 1 : 0;
}
static int dm1105_i2c_xfer(struct i2c_adapter *i2c_adap,
struct i2c_msg *msgs, int num)
{
struct dm1105_dev *dev ;
int addr, rc, i, j, k, len, byte, data;
u8 status;
dev = i2c_adap->algo_data;
for (i = 0; i < num; i++) {
dm_writeb(DM1105_I2CCTR, 0x00);
if (msgs[i].flags & I2C_M_RD) {
/* read bytes */
addr = msgs[i].addr << 1;
addr |= 1;
dm_writeb(DM1105_I2CDAT, addr);
for (byte = 0; byte < msgs[i].len; byte++)
dm_writeb(DM1105_I2CDAT + byte + 1, 0);
dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
for (j = 0; j < 55; j++) {
mdelay(10);
status = dm_readb(DM1105_I2CSTS);
if ((status & 0xc0) == 0x40)
break;
}
if (j >= 55)
return -1;
for (byte = 0; byte < msgs[i].len; byte++) {
rc = dm_readb(DM1105_I2CDAT + byte + 1);
if (rc < 0)
goto err;
msgs[i].buf[byte] = rc;
}
} else if ((msgs[i].buf[0] == 0xf7) && (msgs[i].addr == 0x55)) {
/* prepared for cx24116 firmware */
/* Write in small blocks */
len = msgs[i].len - 1;
k = 1;
do {
dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
dm_writeb(DM1105_I2CDAT + 1, 0xf7);
for (byte = 0; byte < (len > 48 ? 48 : len); byte++) {
data = msgs[i].buf[k + byte];
dm_writeb(DM1105_I2CDAT + byte + 2, data);
}
dm_writeb(DM1105_I2CCTR, 0x82 + (len > 48 ? 48 : len));
for (j = 0; j < 25; j++) {
mdelay(10);
status = dm_readb(DM1105_I2CSTS);
if ((status & 0xc0) == 0x40)
break;
}
if (j >= 25)
return -1;
k += 48;
len -= 48;
} while (len > 0);
} else {
/* write bytes */
dm_writeb(DM1105_I2CDAT, msgs[i].addr << 1);
for (byte = 0; byte < msgs[i].len; byte++) {
data = msgs[i].buf[byte];
dm_writeb(DM1105_I2CDAT + byte + 1, data);
}
dm_writeb(DM1105_I2CCTR, 0x81 + msgs[i].len);
for (j = 0; j < 25; j++) {
mdelay(10);
status = dm_readb(DM1105_I2CSTS);
if ((status & 0xc0) == 0x40)
break;
}
if (j >= 25)
return -1;
}
}
return num;
err:
return rc;
}
static u32 functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C;
}
static const struct i2c_algorithm dm1105_algo = {
.master_xfer = dm1105_i2c_xfer,
.functionality = functionality,
};
static inline struct dm1105_dev *feed_to_dm1105_dev(struct dvb_demux_feed *feed)
{
return container_of(feed->demux, struct dm1105_dev, demux);
}
static inline struct dm1105_dev *frontend_to_dm1105_dev(struct dvb_frontend *fe)
{
return container_of(fe->dvb, struct dm1105_dev, dvb_adapter);
}
static int dm1105_set_voltage(struct dvb_frontend *fe,
enum fe_sec_voltage voltage)
{
struct dm1105_dev *dev = frontend_to_dm1105_dev(fe);
dm1105_gpio_enable(dev, dm1105_boards[dev->boardnr].lnb.mask, 1);
if (voltage == SEC_VOLTAGE_18)
dm1105_gpio_andor(dev,
dm1105_boards[dev->boardnr].lnb.mask,
dm1105_boards[dev->boardnr].lnb.v18);
else if (voltage == SEC_VOLTAGE_13)
dm1105_gpio_andor(dev,
dm1105_boards[dev->boardnr].lnb.mask,
dm1105_boards[dev->boardnr].lnb.v13);
else
dm1105_gpio_andor(dev,
dm1105_boards[dev->boardnr].lnb.mask,
dm1105_boards[dev->boardnr].lnb.off);
return 0;
}
static void dm1105_set_dma_addr(struct dm1105_dev *dev)
{
dm_writel(DM1105_STADR, (__force u32)cpu_to_le32(dev->dma_addr));
}
static int dm1105_dma_map(struct dm1105_dev *dev)
{
dev->ts_buf = dma_alloc_coherent(&dev->pdev->dev,
6 * DM1105_DMA_BYTES, &dev->dma_addr,
GFP_KERNEL);
return !dev->ts_buf;
}
static void dm1105_dma_unmap(struct dm1105_dev *dev)
{
dma_free_coherent(&dev->pdev->dev, 6 * DM1105_DMA_BYTES, dev->ts_buf,
dev->dma_addr);
}
static void dm1105_enable_irqs(struct dm1105_dev *dev)
{
dm_writeb(DM1105_INTMAK, INTMAK_ALLMASK);
dm_writeb(DM1105_CR, 1);
}
static void dm1105_disable_irqs(struct dm1105_dev *dev)
{
dm_writeb(DM1105_INTMAK, INTMAK_IRM);
dm_writeb(DM1105_CR, 0);
}
static int dm1105_start_feed(struct dvb_demux_feed *f)
{
struct dm1105_dev *dev = feed_to_dm1105_dev(f);
if (dev->full_ts_users++ == 0)
dm1105_enable_irqs(dev);
return 0;
}
static int dm1105_stop_feed(struct dvb_demux_feed *f)
{
struct dm1105_dev *dev = feed_to_dm1105_dev(f);
if (--dev->full_ts_users == 0)
dm1105_disable_irqs(dev);
return 0;
}
/* ir work handler */
static void dm1105_emit_key(struct work_struct *work)
{
struct infrared *ir = container_of(work, struct infrared, work);
u32 ircom = ir->ir_command;
u8 data;
if (ir_debug)
printk(KERN_INFO "%s: received byte 0x%04x\n", __func__, ircom);
data = (ircom >> 8) & 0x7f;
/* FIXME: UNKNOWN because we don't generate a full NEC scancode (yet?) */
rc_keydown(ir->dev, RC_PROTO_UNKNOWN, data, 0);
}
/* work handler */
static void dm1105_dmx_buffer(struct work_struct *work)
{
struct dm1105_dev *dev = container_of(work, struct dm1105_dev, work);
unsigned int nbpackets;
u32 oldwrp = dev->wrp;
u32 nextwrp = dev->nextwrp;
if (!((dev->ts_buf[oldwrp] == 0x47) &&
(dev->ts_buf[oldwrp + 188] == 0x47) &&
(dev->ts_buf[oldwrp + 188 * 2] == 0x47))) {
dev->PacketErrorCount++;
/* bad packet found */
if ((dev->PacketErrorCount >= 2) &&
(dev->dmarst == 0)) {
dm_writeb(DM1105_RST, 1);
dev->wrp = 0;
dev->PacketErrorCount = 0;
dev->dmarst = 0;
return;
}
}
if (nextwrp < oldwrp) {
memcpy(dev->ts_buf + dev->buffer_size, dev->ts_buf, nextwrp);
nbpackets = ((dev->buffer_size - oldwrp) + nextwrp) / 188;
} else
nbpackets = (nextwrp - oldwrp) / 188;
dev->wrp = nextwrp;
dvb_dmx_swfilter_packets(&dev->demux, &dev->ts_buf[oldwrp], nbpackets);
}
static irqreturn_t dm1105_irq(int irq, void *dev_id)
{
struct dm1105_dev *dev = dev_id;
/* Read-Write INSTS Ack's Interrupt for DM1105 chip 16.03.2008 */
unsigned int intsts = dm_readb(DM1105_INTSTS);
dm_writeb(DM1105_INTSTS, intsts);
switch (intsts) {
case INTSTS_TSIRQ:
case (INTSTS_TSIRQ | INTSTS_IR):
dev->nextwrp = dm_readl(DM1105_WRP) - dm_readl(DM1105_STADR);
queue_work(dev->wq, &dev->work);
break;
case INTSTS_IR:
dev->ir.ir_command = dm_readl(DM1105_IRCODE);
schedule_work(&dev->ir.work);
break;
}
return IRQ_HANDLED;
}
static int dm1105_ir_init(struct dm1105_dev *dm1105)
{
struct rc_dev *dev;
int err = -ENOMEM;
dev = rc_allocate_device(RC_DRIVER_SCANCODE);
if (!dev)
return -ENOMEM;
snprintf(dm1105->ir.input_phys, sizeof(dm1105->ir.input_phys),
"pci-%s/ir0", pci_name(dm1105->pdev));
dev->driver_name = MODULE_NAME;
dev->map_name = RC_MAP_DM1105_NEC;
dev->device_name = "DVB on-card IR receiver";
dev->input_phys = dm1105->ir.input_phys;
dev->input_id.bustype = BUS_PCI;
dev->input_id.version = 1;
if (dm1105->pdev->subsystem_vendor) {
dev->input_id.vendor = dm1105->pdev->subsystem_vendor;
dev->input_id.product = dm1105->pdev->subsystem_device;
} else {
dev->input_id.vendor = dm1105->pdev->vendor;
dev->input_id.product = dm1105->pdev->device;
}
dev->dev.parent = &dm1105->pdev->dev;
INIT_WORK(&dm1105->ir.work, dm1105_emit_key);
err = rc_register_device(dev);
if (err < 0) {
rc_free_device(dev);
return err;
}
dm1105->ir.dev = dev;
return 0;
}
static void dm1105_ir_exit(struct dm1105_dev *dm1105)
{
rc_unregister_device(dm1105->ir.dev);
}
static int dm1105_hw_init(struct dm1105_dev *dev)
{
dm1105_disable_irqs(dev);
dm_writeb(DM1105_HOST_CTR, 0);
/*DATALEN 188,*/
dm_writeb(DM1105_DTALENTH, 188);
/*TS_STRT TS_VALP MSBFIRST TS_MODE ALPAS TSPES*/
dm_writew(DM1105_TSCTR, 0xc10a);
/* map DMA and set address */
dm1105_dma_map(dev);
dm1105_set_dma_addr(dev);
/* big buffer */
dm_writel(DM1105_RLEN, 5 * DM1105_DMA_BYTES);
dm_writeb(DM1105_INTCNT, 47);
/* IR NEC mode enable */
dm_writeb(DM1105_IRCTR, (DM1105_IR_EN | DM1105_SYS_CHK));
dm_writeb(DM1105_IRMODE, 0);
dm_writew(DM1105_SYSTEMCODE, 0);
return 0;
}
static void dm1105_hw_exit(struct dm1105_dev *dev)
{
dm1105_disable_irqs(dev);
/* IR disable */
dm_writeb(DM1105_IRCTR, 0);
dm_writeb(DM1105_INTMAK, INTMAK_NONEMASK);
dm1105_dma_unmap(dev);
}
static const struct stv0299_config sharp_z0194a_config = {
.demod_address = 0x68,
.inittab = sharp_z0194a_inittab,
.mclk = 88000000UL,
.invert = 1,
.skip_reinit = 0,
.lock_output = STV0299_LOCKOUTPUT_1,
.volt13_op0_op1 = STV0299_VOLT13_OP1,
.min_delay_ms = 100,
.set_symbol_rate = sharp_z0194a_set_symbol_rate,
};
static struct stv0288_config earda_config = {
.demod_address = 0x68,
.min_delay_ms = 100,
};
static struct si21xx_config serit_config = {
.demod_address = 0x68,
.min_delay_ms = 100,
};
static struct cx24116_config serit_sp2633_config = {
.demod_address = 0x55,
};
static struct ds3000_config dvbworld_ds3000_config = {
.demod_address = 0x68,
};
static struct ts2020_config dvbworld_ts2020_config = {
.tuner_address = 0x60,
.clk_out_div = 1,
};
static int frontend_init(struct dm1105_dev *dev)
{
int ret;
switch (dev->boardnr) {
case DM1105_BOARD_UNBRANDED_I2C_ON_GPIO:
dm1105_gpio_enable(dev, GPIO15, 1);
dm1105_gpio_clear(dev, GPIO15);
msleep(100);
dm1105_gpio_set(dev, GPIO15);
msleep(200);
dev->fe = dvb_attach(
stv0299_attach, &sharp_z0194a_config,
&dev->i2c_bb_adap);
if (dev->fe) {
dev->fe->ops.set_voltage = dm1105_set_voltage;
dvb_attach(dvb_pll_attach, dev->fe, 0x60,
&dev->i2c_bb_adap, DVB_PLL_OPERA1);
break;
}
dev->fe = dvb_attach(
stv0288_attach, &earda_config,
&dev->i2c_bb_adap);
if (dev->fe) {
dev->fe->ops.set_voltage = dm1105_set_voltage;
dvb_attach(stb6000_attach, dev->fe, 0x61,
&dev->i2c_bb_adap);
break;
}
dev->fe = dvb_attach(
si21xx_attach, &serit_config,
&dev->i2c_bb_adap);
if (dev->fe)
dev->fe->ops.set_voltage = dm1105_set_voltage;
break;
case DM1105_BOARD_DVBWORLD_2004:
dev->fe = dvb_attach(
cx24116_attach, &serit_sp2633_config,
&dev->i2c_adap);
if (dev->fe) {
dev->fe->ops.set_voltage = dm1105_set_voltage;
break;
}
dev->fe = dvb_attach(
ds3000_attach, &dvbworld_ds3000_config,
&dev->i2c_adap);
if (dev->fe) {
dvb_attach(ts2020_attach, dev->fe,
&dvbworld_ts2020_config, &dev->i2c_adap);
dev->fe->ops.set_voltage = dm1105_set_voltage;
}
break;
case DM1105_BOARD_DVBWORLD_2002:
case DM1105_BOARD_AXESS_DM05:
default:
dev->fe = dvb_attach(
stv0299_attach, &sharp_z0194a_config,
&dev->i2c_adap);
if (dev->fe) {
dev->fe->ops.set_voltage = dm1105_set_voltage;
dvb_attach(dvb_pll_attach, dev->fe, 0x60,
&dev->i2c_adap, DVB_PLL_OPERA1);
break;
}
dev->fe = dvb_attach(
stv0288_attach, &earda_config,
&dev->i2c_adap);
if (dev->fe) {
dev->fe->ops.set_voltage = dm1105_set_voltage;
dvb_attach(stb6000_attach, dev->fe, 0x61,
&dev->i2c_adap);
break;
}
dev->fe = dvb_attach(
si21xx_attach, &serit_config,
&dev->i2c_adap);
if (dev->fe)
dev->fe->ops.set_voltage = dm1105_set_voltage;
}
if (!dev->fe) {
dev_err(&dev->pdev->dev, "could not attach frontend\n");
return -ENODEV;
}
ret = dvb_register_frontend(&dev->dvb_adapter, dev->fe);
if (ret < 0) {
if (dev->fe->ops.release)
dev->fe->ops.release(dev->fe);
dev->fe = NULL;
return ret;
}
return 0;
}
static void dm1105_read_mac(struct dm1105_dev *dev, u8 *mac)
{
static u8 command[1] = { 0x28 };
struct i2c_msg msg[] = {
{
.addr = IIC_24C01_addr >> 1,
.flags = 0,
.buf = command,
.len = 1
}, {
.addr = IIC_24C01_addr >> 1,
.flags = I2C_M_RD,
.buf = mac,
.len = 6
},
};
dm1105_i2c_xfer(&dev->i2c_adap, msg , 2);
dev_info(&dev->pdev->dev, "MAC %pM\n", mac);
}
static int dm1105_probe(struct pci_dev *pdev,
const struct pci_device_id *ent)
{
struct dm1105_dev *dev;
struct dvb_adapter *dvb_adapter;
struct dvb_demux *dvbdemux;
struct dmx_demux *dmx;
int ret = -ENOMEM;
int i;
if (dm1105_devcount >= ARRAY_SIZE(card))
return -ENODEV;
dev = kzalloc(sizeof(struct dm1105_dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
/* board config */
dev->nr = dm1105_devcount;
dev->boardnr = UNSET;
if (card[dev->nr] < ARRAY_SIZE(dm1105_boards))
dev->boardnr = card[dev->nr];
for (i = 0; UNSET == dev->boardnr &&
i < ARRAY_SIZE(dm1105_subids); i++)
if (pdev->subsystem_vendor ==
dm1105_subids[i].subvendor &&
pdev->subsystem_device ==
dm1105_subids[i].subdevice)
dev->boardnr = dm1105_subids[i].card;
if (UNSET == dev->boardnr) {
dev->boardnr = DM1105_BOARD_UNKNOWN;
dm1105_card_list(pdev);
}
dm1105_devcount++;
dev->pdev = pdev;
dev->buffer_size = 5 * DM1105_DMA_BYTES;
dev->PacketErrorCount = 0;
dev->dmarst = 0;
ret = pci_enable_device(pdev);
if (ret < 0)
goto err_kfree;
ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
if (ret < 0)
goto err_pci_disable_device;
pci_set_master(pdev);
ret = pci_request_regions(pdev, DRIVER_NAME);
if (ret < 0)
goto err_pci_disable_device;
dev->io_mem = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
if (!dev->io_mem) {
ret = -EIO;
goto err_pci_release_regions;
}
spin_lock_init(&dev->lock);
pci_set_drvdata(pdev, dev);
ret = dm1105_hw_init(dev);
if (ret < 0)
goto err_pci_iounmap;
/* i2c */
i2c_set_adapdata(&dev->i2c_adap, dev);
strscpy(dev->i2c_adap.name, DRIVER_NAME, sizeof(dev->i2c_adap.name));
dev->i2c_adap.owner = THIS_MODULE;
dev->i2c_adap.dev.parent = &pdev->dev;
dev->i2c_adap.algo = &dm1105_algo;
dev->i2c_adap.algo_data = dev;
ret = i2c_add_adapter(&dev->i2c_adap);
if (ret < 0)
goto err_dm1105_hw_exit;
i2c_set_adapdata(&dev->i2c_bb_adap, dev);
strscpy(dev->i2c_bb_adap.name, DM1105_I2C_GPIO_NAME,
sizeof(dev->i2c_bb_adap.name));
dev->i2c_bb_adap.owner = THIS_MODULE;
dev->i2c_bb_adap.dev.parent = &pdev->dev;
dev->i2c_bb_adap.algo_data = &dev->i2c_bit;
dev->i2c_bit.data = dev;
dev->i2c_bit.setsda = dm1105_setsda;
dev->i2c_bit.setscl = dm1105_setscl;
dev->i2c_bit.getsda = dm1105_getsda;
dev->i2c_bit.getscl = dm1105_getscl;
dev->i2c_bit.udelay = 10;
dev->i2c_bit.timeout = 10;
/* Raise SCL and SDA */
dm1105_setsda(dev, 1);
dm1105_setscl(dev, 1);
ret = i2c_bit_add_bus(&dev->i2c_bb_adap);
if (ret < 0)
goto err_i2c_del_adapter;
/* dvb */
ret = dvb_register_adapter(&dev->dvb_adapter, DRIVER_NAME,
THIS_MODULE, &pdev->dev, adapter_nr);
if (ret < 0)
goto err_i2c_del_adapters;
dvb_adapter = &dev->dvb_adapter;
dm1105_read_mac(dev, dvb_adapter->proposed_mac);
dvbdemux = &dev->demux;
dvbdemux->filternum = 256;
dvbdemux->feednum = 256;
dvbdemux->start_feed = dm1105_start_feed;
dvbdemux->stop_feed = dm1105_stop_feed;
dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
ret = dvb_dmx_init(dvbdemux);
if (ret < 0)
goto err_dvb_unregister_adapter;
dmx = &dvbdemux->dmx;
dev->dmxdev.filternum = 256;
dev->dmxdev.demux = dmx;
dev->dmxdev.capabilities = 0;
ret = dvb_dmxdev_init(&dev->dmxdev, dvb_adapter);
if (ret < 0)
goto err_dvb_dmx_release;
dev->hw_frontend.source = DMX_FRONTEND_0;
ret = dmx->add_frontend(dmx, &dev->hw_frontend);
if (ret < 0)
goto err_dvb_dmxdev_release;
dev->mem_frontend.source = DMX_MEMORY_FE;
ret = dmx->add_frontend(dmx, &dev->mem_frontend);
if (ret < 0)
goto err_remove_hw_frontend;
ret = dmx->connect_frontend(dmx, &dev->hw_frontend);
if (ret < 0)
goto err_remove_mem_frontend;
ret = dvb_net_init(dvb_adapter, &dev->dvbnet, dmx);
if (ret < 0)
goto err_disconnect_frontend;
ret = frontend_init(dev);
if (ret < 0)
goto err_dvb_net;
dm1105_ir_init(dev);
INIT_WORK(&dev->work, dm1105_dmx_buffer);
sprintf(dev->wqn, "%s/%d", dvb_adapter->name, dvb_adapter->num);
dev->wq = create_singlethread_workqueue(dev->wqn);
if (!dev->wq) {
ret = -ENOMEM;
goto err_dvb_net;
}
ret = request_irq(pdev->irq, dm1105_irq, IRQF_SHARED,
DRIVER_NAME, dev);
if (ret < 0)
goto err_workqueue;
return 0;
err_workqueue:
destroy_workqueue(dev->wq);
err_dvb_net:
dvb_net_release(&dev->dvbnet);
err_disconnect_frontend:
dmx->disconnect_frontend(dmx);
err_remove_mem_frontend:
dmx->remove_frontend(dmx, &dev->mem_frontend);
err_remove_hw_frontend:
dmx->remove_frontend(dmx, &dev->hw_frontend);
err_dvb_dmxdev_release:
dvb_dmxdev_release(&dev->dmxdev);
err_dvb_dmx_release:
dvb_dmx_release(dvbdemux);
err_dvb_unregister_adapter:
dvb_unregister_adapter(dvb_adapter);
err_i2c_del_adapters:
i2c_del_adapter(&dev->i2c_bb_adap);
err_i2c_del_adapter:
i2c_del_adapter(&dev->i2c_adap);
err_dm1105_hw_exit:
dm1105_hw_exit(dev);
err_pci_iounmap:
pci_iounmap(pdev, dev->io_mem);
err_pci_release_regions:
pci_release_regions(pdev);
err_pci_disable_device:
pci_disable_device(pdev);
err_kfree:
kfree(dev);
return ret;
}
static void dm1105_remove(struct pci_dev *pdev)
{
struct dm1105_dev *dev = pci_get_drvdata(pdev);
struct dvb_adapter *dvb_adapter = &dev->dvb_adapter;
struct dvb_demux *dvbdemux = &dev->demux;
struct dmx_demux *dmx = &dvbdemux->dmx;
cancel_work_sync(&dev->ir.work);
dm1105_ir_exit(dev);
dmx->close(dmx);
dvb_net_release(&dev->dvbnet);
if (dev->fe)
dvb_unregister_frontend(dev->fe);
dmx->disconnect_frontend(dmx);
dmx->remove_frontend(dmx, &dev->mem_frontend);
dmx->remove_frontend(dmx, &dev->hw_frontend);
dvb_dmxdev_release(&dev->dmxdev);
dvb_dmx_release(dvbdemux);
dvb_unregister_adapter(dvb_adapter);
i2c_del_adapter(&dev->i2c_adap);
dm1105_hw_exit(dev);
free_irq(pdev->irq, dev);
pci_iounmap(pdev, dev->io_mem);
pci_release_regions(pdev);
pci_disable_device(pdev);
dm1105_devcount--;
kfree(dev);
}
static const struct pci_device_id dm1105_id_table[] = {
{
.vendor = PCI_VENDOR_ID_TRIGEM,
.device = PCI_DEVICE_ID_DM1105,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
}, {
.vendor = PCI_VENDOR_ID_AXESS,
.device = PCI_DEVICE_ID_DM05,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
}, {
/* empty */
},
};
MODULE_DEVICE_TABLE(pci, dm1105_id_table);
static struct pci_driver dm1105_driver = {
.name = DRIVER_NAME,
.id_table = dm1105_id_table,
.probe = dm1105_probe,
.remove = dm1105_remove,
};
module_pci_driver(dm1105_driver);
MODULE_AUTHOR("Igor M. Liplianin <[email protected]>");
MODULE_DESCRIPTION("SDMC DM1105 DVB driver");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/pci/dm1105/dm1105.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/delay.h>
#include "solo6x10.h"
#include "solo6x10-tw28.h"
#define DEFAULT_HDELAY_NTSC (32 - 8)
#define DEFAULT_HACTIVE_NTSC (720 + 16)
#define DEFAULT_VDELAY_NTSC (7 - 2)
#define DEFAULT_VACTIVE_NTSC (240 + 4)
#define DEFAULT_HDELAY_PAL (32 + 4)
#define DEFAULT_HACTIVE_PAL (864-DEFAULT_HDELAY_PAL)
#define DEFAULT_VDELAY_PAL (6)
#define DEFAULT_VACTIVE_PAL (312-DEFAULT_VDELAY_PAL)
static const u8 tbl_tw2864_ntsc_template[] = {
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x00 */
0x12, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x10 */
0x12, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x20 */
0x12, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x30 */
0x12, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x50 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x60 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA3, 0x00,
0x00, 0x02, 0x00, 0xcc, 0x00, 0x80, 0x44, 0x50, /* 0x80 */
0x22, 0x01, 0xd8, 0xbc, 0xb8, 0x44, 0x38, 0x00,
0x00, 0x78, 0x72, 0x3e, 0x14, 0xa5, 0xe4, 0x05, /* 0x90 */
0x00, 0x28, 0x44, 0x44, 0xa0, 0x88, 0x5a, 0x01,
0x08, 0x08, 0x08, 0x08, 0x1a, 0x1a, 0x1a, 0x1a, /* 0xa0 */
0x00, 0x00, 0x00, 0xf0, 0xf0, 0xf0, 0xf0, 0x44,
0x44, 0x0a, 0x00, 0xff, 0xef, 0xef, 0xef, 0xef, /* 0xb0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 */
0x00, 0x00, 0x55, 0x00, 0xb1, 0xe4, 0x40, 0x00,
0x77, 0x77, 0x01, 0x13, 0x57, 0x9b, 0xdf, 0x20, /* 0xd0 */
0x64, 0xa8, 0xec, 0xc1, 0x0f, 0x11, 0x11, 0x81,
0x00, 0xe0, 0xbb, 0xbb, 0x00, 0x11, 0x00, 0x00, /* 0xe0 */
0x11, 0x00, 0x00, 0x11, 0x00, 0x00, 0x11, 0x00,
0x83, 0xb5, 0x09, 0x78, 0x85, 0x00, 0x01, 0x20, /* 0xf0 */
0x64, 0x11, 0x40, 0xaf, 0xff, 0x00, 0x00, 0x00,
};
static const u8 tbl_tw2864_pal_template[] = {
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x00 */
0x18, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x01, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x10 */
0x18, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x01, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x20 */
0x18, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x01, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x30 */
0x18, 0xf5, 0x0c, 0xd0, 0x00, 0x00, 0x01, 0x7f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x50 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x60 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA3, 0x00,
0x00, 0x02, 0x00, 0xcc, 0x00, 0x80, 0x44, 0x50, /* 0x80 */
0x22, 0x01, 0xd8, 0xbc, 0xb8, 0x44, 0x38, 0x00,
0x00, 0x78, 0x72, 0x3e, 0x14, 0xa5, 0xe4, 0x05, /* 0x90 */
0x00, 0x28, 0x44, 0x44, 0xa0, 0x90, 0x5a, 0x01,
0x0a, 0x0a, 0x0a, 0x0a, 0x1a, 0x1a, 0x1a, 0x1a, /* 0xa0 */
0x00, 0x00, 0x00, 0xf0, 0xf0, 0xf0, 0xf0, 0x44,
0x44, 0x0a, 0x00, 0xff, 0xef, 0xef, 0xef, 0xef, /* 0xb0 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 */
0x00, 0x00, 0x55, 0x00, 0xb1, 0xe4, 0x40, 0x00,
0x77, 0x77, 0x01, 0x13, 0x57, 0x9b, 0xdf, 0x20, /* 0xd0 */
0x64, 0xa8, 0xec, 0xc1, 0x0f, 0x11, 0x11, 0x81,
0x00, 0xe0, 0xbb, 0xbb, 0x00, 0x11, 0x00, 0x00, /* 0xe0 */
0x11, 0x00, 0x00, 0x11, 0x00, 0x00, 0x11, 0x00,
0x83, 0xb5, 0x09, 0x00, 0xa0, 0x00, 0x01, 0x20, /* 0xf0 */
0x64, 0x11, 0x40, 0xaf, 0xff, 0x00, 0x00, 0x00,
};
static const u8 tbl_tw2865_ntsc_template[] = {
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x00 */
0x12, 0xff, 0x09, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x10 */
0x12, 0xff, 0x09, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x02, /* 0x20 */
0x12, 0xff, 0x09, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0xf0, 0x70, 0x48, 0x80, 0x80, 0x00, 0x02, /* 0x30 */
0x12, 0xff, 0x09, 0xd0, 0x00, 0x00, 0x00, 0x7f,
0x00, 0x00, 0x90, 0x68, 0x00, 0x38, 0x80, 0x80, /* 0x40 */
0x80, 0x80, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x50 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x45, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x60 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43,
0x08, 0x00, 0x00, 0x01, 0xf1, 0x03, 0xEF, 0x03, /* 0x70 */
0xE9, 0x03, 0xD9, 0x15, 0x15, 0xE4, 0xA3, 0x80,
0x00, 0x02, 0x00, 0xCC, 0x00, 0x80, 0x44, 0x50, /* 0x80 */
0x22, 0x01, 0xD8, 0xBC, 0xB8, 0x44, 0x38, 0x00,
0x00, 0x78, 0x44, 0x3D, 0x14, 0xA5, 0xE0, 0x05, /* 0x90 */
0x00, 0x28, 0x44, 0x44, 0xA0, 0x90, 0x52, 0x13,
0x08, 0x08, 0x08, 0x08, 0x1A, 0x1A, 0x1B, 0x1A, /* 0xa0 */
0x00, 0x00, 0x00, 0xF0, 0xF0, 0xF0, 0xF0, 0x44,
0x44, 0x4A, 0x00, 0xFF, 0xEF, 0xEF, 0xEF, 0xEF, /* 0xb0 */
0xFF, 0xE7, 0xE9, 0xE9, 0xEB, 0xFF, 0xD6, 0xD8,
0xD8, 0xD7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 */
0x00, 0x00, 0x55, 0x00, 0xE4, 0x39, 0x00, 0x80,
0x77, 0x77, 0x03, 0x20, 0x57, 0x9b, 0xdf, 0x31, /* 0xd0 */
0x64, 0xa8, 0xec, 0xd1, 0x0f, 0x11, 0x11, 0x81,
0x10, 0xC0, 0xAA, 0xAA, 0x00, 0x11, 0x00, 0x00, /* 0xe0 */
0x11, 0x00, 0x00, 0x11, 0x00, 0x00, 0x11, 0x00,
0x83, 0xB5, 0x09, 0x78, 0x85, 0x00, 0x01, 0x20, /* 0xf0 */
0x64, 0x51, 0x40, 0xaf, 0xFF, 0xF0, 0x00, 0xC0,
};
static const u8 tbl_tw2865_pal_template[] = {
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x00 */
0x11, 0xff, 0x01, 0xc3, 0x00, 0x00, 0x01, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x10 */
0x11, 0xff, 0x01, 0xc3, 0x00, 0x00, 0x01, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x20 */
0x11, 0xff, 0x01, 0xc3, 0x00, 0x00, 0x01, 0x7f,
0x00, 0xf0, 0x70, 0x30, 0x80, 0x80, 0x00, 0x12, /* 0x30 */
0x11, 0xff, 0x01, 0xc3, 0x00, 0x00, 0x01, 0x7f,
0x00, 0x94, 0x90, 0x48, 0x00, 0x38, 0x7F, 0x80, /* 0x40 */
0x80, 0x80, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x50 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x45, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x60 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43,
0x08, 0x00, 0x00, 0x01, 0xf1, 0x03, 0xEF, 0x03, /* 0x70 */
0xEA, 0x03, 0xD9, 0x15, 0x15, 0xE4, 0xA3, 0x80,
0x00, 0x02, 0x00, 0xCC, 0x00, 0x80, 0x44, 0x50, /* 0x80 */
0x22, 0x01, 0xD8, 0xBC, 0xB8, 0x44, 0x38, 0x00,
0x00, 0x78, 0x44, 0x3D, 0x14, 0xA5, 0xE0, 0x05, /* 0x90 */
0x00, 0x28, 0x44, 0x44, 0xA0, 0x90, 0x52, 0x13,
0x08, 0x08, 0x08, 0x08, 0x1A, 0x1A, 0x1A, 0x1A, /* 0xa0 */
0x00, 0x00, 0x00, 0xF0, 0xF0, 0xF0, 0xF0, 0x44,
0x44, 0x4A, 0x00, 0xFF, 0xEF, 0xEF, 0xEF, 0xEF, /* 0xb0 */
0xFF, 0xE7, 0xE9, 0xE9, 0xE9, 0xFF, 0xD7, 0xD8,
0xD9, 0xD8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 */
0x00, 0x00, 0x55, 0x00, 0xE4, 0x39, 0x00, 0x80,
0x77, 0x77, 0x03, 0x20, 0x57, 0x9b, 0xdf, 0x31, /* 0xd0 */
0x64, 0xa8, 0xec, 0xd1, 0x0f, 0x11, 0x11, 0x81,
0x10, 0xC0, 0xAA, 0xAA, 0x00, 0x11, 0x00, 0x00, /* 0xe0 */
0x11, 0x00, 0x00, 0x11, 0x00, 0x00, 0x11, 0x00,
0x83, 0xB5, 0x09, 0x00, 0xA0, 0x00, 0x01, 0x20, /* 0xf0 */
0x64, 0x51, 0x40, 0xaf, 0xFF, 0xF0, 0x00, 0xC0,
};
#define is_tw286x(__solo, __id) (!(__solo->tw2815 & (1 << __id)))
static u8 tw_readbyte(struct solo_dev *solo_dev, int chip_id, u8 tw6x_off,
u8 tw_off)
{
if (is_tw286x(solo_dev, chip_id))
return solo_i2c_readbyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_id),
tw6x_off);
else
return solo_i2c_readbyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_id),
tw_off);
}
static void tw_writebyte(struct solo_dev *solo_dev, int chip_id,
u8 tw6x_off, u8 tw_off, u8 val)
{
if (is_tw286x(solo_dev, chip_id))
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_id),
tw6x_off, val);
else
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_id),
tw_off, val);
}
static void tw_write_and_verify(struct solo_dev *solo_dev, u8 addr, u8 off,
u8 val)
{
int i;
for (i = 0; i < 5; i++) {
u8 rval = solo_i2c_readbyte(solo_dev, SOLO_I2C_TW, addr, off);
if (rval == val)
return;
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW, addr, off, val);
msleep_interruptible(1);
}
/* printk("solo6x10/tw28: Error writing register: %02x->%02x [%02x]\n", */
/* addr, off, val); */
}
static int tw2865_setup(struct solo_dev *solo_dev, u8 dev_addr)
{
u8 tbl_tw2865_common[256];
int i;
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_PAL)
memcpy(tbl_tw2865_common, tbl_tw2865_pal_template,
sizeof(tbl_tw2865_common));
else
memcpy(tbl_tw2865_common, tbl_tw2865_ntsc_template,
sizeof(tbl_tw2865_common));
/* ALINK Mode */
if (solo_dev->nr_chans == 4) {
tbl_tw2865_common[0xd2] = 0x01;
tbl_tw2865_common[0xcf] = 0x00;
} else if (solo_dev->nr_chans == 8) {
tbl_tw2865_common[0xd2] = 0x02;
if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2865_common[0xcf] = 0x80;
} else if (solo_dev->nr_chans == 16) {
tbl_tw2865_common[0xd2] = 0x03;
if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2865_common[0xcf] = 0x83;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(2))
tbl_tw2865_common[0xcf] = 0x83;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(3))
tbl_tw2865_common[0xcf] = 0x80;
}
for (i = 0; i < 0xff; i++) {
/* Skip read only registers */
switch (i) {
case 0xb8 ... 0xc1:
case 0xc4 ... 0xc7:
case 0xfd:
continue;
}
switch (i & ~0x30) {
case 0x00:
case 0x0c ... 0x0d:
continue;
}
tw_write_and_verify(solo_dev, dev_addr, i,
tbl_tw2865_common[i]);
}
return 0;
}
static int tw2864_setup(struct solo_dev *solo_dev, u8 dev_addr)
{
u8 tbl_tw2864_common[256];
int i;
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_PAL)
memcpy(tbl_tw2864_common, tbl_tw2864_pal_template,
sizeof(tbl_tw2864_common));
else
memcpy(tbl_tw2864_common, tbl_tw2864_ntsc_template,
sizeof(tbl_tw2864_common));
if (solo_dev->tw2865 == 0) {
/* IRQ Mode */
if (solo_dev->nr_chans == 4) {
tbl_tw2864_common[0xd2] = 0x01;
tbl_tw2864_common[0xcf] = 0x00;
} else if (solo_dev->nr_chans == 8) {
tbl_tw2864_common[0xd2] = 0x02;
if (dev_addr == TW_CHIP_OFFSET_ADDR(0))
tbl_tw2864_common[0xcf] = 0x43;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2864_common[0xcf] = 0x40;
} else if (solo_dev->nr_chans == 16) {
tbl_tw2864_common[0xd2] = 0x03;
if (dev_addr == TW_CHIP_OFFSET_ADDR(0))
tbl_tw2864_common[0xcf] = 0x43;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2864_common[0xcf] = 0x43;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(2))
tbl_tw2864_common[0xcf] = 0x43;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(3))
tbl_tw2864_common[0xcf] = 0x40;
}
} else {
/* ALINK Mode. Assumes that the first tw28xx is a
* 2865 and these are in cascade. */
for (i = 0; i <= 4; i++)
tbl_tw2864_common[0x08 | i << 4] = 0x12;
if (solo_dev->nr_chans == 8) {
tbl_tw2864_common[0xd2] = 0x02;
if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2864_common[0xcf] = 0x80;
} else if (solo_dev->nr_chans == 16) {
tbl_tw2864_common[0xd2] = 0x03;
if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2864_common[0xcf] = 0x83;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(2))
tbl_tw2864_common[0xcf] = 0x83;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(3))
tbl_tw2864_common[0xcf] = 0x80;
}
}
for (i = 0; i < 0xff; i++) {
/* Skip read only registers */
switch (i) {
case 0xb8 ... 0xc1:
case 0xfd:
continue;
}
switch (i & ~0x30) {
case 0x00:
case 0x0c:
case 0x0d:
continue;
}
tw_write_and_verify(solo_dev, dev_addr, i,
tbl_tw2864_common[i]);
}
return 0;
}
static int tw2815_setup(struct solo_dev *solo_dev, u8 dev_addr)
{
u8 tbl_ntsc_tw2815_common[] = {
0x00, 0xc8, 0x20, 0xd0, 0x06, 0xf0, 0x08, 0x80,
0x80, 0x80, 0x80, 0x02, 0x06, 0x00, 0x11,
};
u8 tbl_pal_tw2815_common[] = {
0x00, 0x88, 0x20, 0xd0, 0x05, 0x20, 0x28, 0x80,
0x80, 0x80, 0x80, 0x82, 0x06, 0x00, 0x11,
};
u8 tbl_tw2815_sfr[] = {
0x00, 0x00, 0x00, 0xc0, 0x45, 0xa0, 0xd0, 0x2f, /* 0x00 */
0x64, 0x80, 0x80, 0x82, 0x82, 0x00, 0x00, 0x00,
0x00, 0x0f, 0x05, 0x00, 0x00, 0x80, 0x06, 0x00, /* 0x10 */
0x00, 0x00, 0x00, 0xff, 0x8f, 0x00, 0x00, 0x00,
0x88, 0x88, 0xc0, 0x00, 0x20, 0x64, 0xa8, 0xec, /* 0x20 */
0x31, 0x75, 0xb9, 0xfd, 0x00, 0x00, 0x88, 0x88,
0x88, 0x11, 0x00, 0x88, 0x88, 0x00, /* 0x30 */
};
u8 *tbl_tw2815_common;
int i;
int ch;
tbl_ntsc_tw2815_common[0x06] = 0;
/* Horizontal Delay Control */
tbl_ntsc_tw2815_common[0x02] = DEFAULT_HDELAY_NTSC & 0xff;
tbl_ntsc_tw2815_common[0x06] |= 0x03 & (DEFAULT_HDELAY_NTSC >> 8);
/* Horizontal Active Control */
tbl_ntsc_tw2815_common[0x03] = DEFAULT_HACTIVE_NTSC & 0xff;
tbl_ntsc_tw2815_common[0x06] |=
((0x03 & (DEFAULT_HACTIVE_NTSC >> 8)) << 2);
/* Vertical Delay Control */
tbl_ntsc_tw2815_common[0x04] = DEFAULT_VDELAY_NTSC & 0xff;
tbl_ntsc_tw2815_common[0x06] |=
((0x01 & (DEFAULT_VDELAY_NTSC >> 8)) << 4);
/* Vertical Active Control */
tbl_ntsc_tw2815_common[0x05] = DEFAULT_VACTIVE_NTSC & 0xff;
tbl_ntsc_tw2815_common[0x06] |=
((0x01 & (DEFAULT_VACTIVE_NTSC >> 8)) << 5);
tbl_pal_tw2815_common[0x06] = 0;
/* Horizontal Delay Control */
tbl_pal_tw2815_common[0x02] = DEFAULT_HDELAY_PAL & 0xff;
tbl_pal_tw2815_common[0x06] |= 0x03 & (DEFAULT_HDELAY_PAL >> 8);
/* Horizontal Active Control */
tbl_pal_tw2815_common[0x03] = DEFAULT_HACTIVE_PAL & 0xff;
tbl_pal_tw2815_common[0x06] |=
((0x03 & (DEFAULT_HACTIVE_PAL >> 8)) << 2);
/* Vertical Delay Control */
tbl_pal_tw2815_common[0x04] = DEFAULT_VDELAY_PAL & 0xff;
tbl_pal_tw2815_common[0x06] |=
((0x01 & (DEFAULT_VDELAY_PAL >> 8)) << 4);
/* Vertical Active Control */
tbl_pal_tw2815_common[0x05] = DEFAULT_VACTIVE_PAL & 0xff;
tbl_pal_tw2815_common[0x06] |=
((0x01 & (DEFAULT_VACTIVE_PAL >> 8)) << 5);
tbl_tw2815_common =
(solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) ?
tbl_ntsc_tw2815_common : tbl_pal_tw2815_common;
/* Dual ITU-R BT.656 format */
tbl_tw2815_common[0x0d] |= 0x04;
/* Audio configuration */
tbl_tw2815_sfr[0x62 - 0x40] &= ~(3 << 6);
if (solo_dev->nr_chans == 4) {
tbl_tw2815_sfr[0x63 - 0x40] |= 1;
tbl_tw2815_sfr[0x62 - 0x40] |= 3 << 6;
} else if (solo_dev->nr_chans == 8) {
tbl_tw2815_sfr[0x63 - 0x40] |= 2;
if (dev_addr == TW_CHIP_OFFSET_ADDR(0))
tbl_tw2815_sfr[0x62 - 0x40] |= 1 << 6;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2815_sfr[0x62 - 0x40] |= 2 << 6;
} else if (solo_dev->nr_chans == 16) {
tbl_tw2815_sfr[0x63 - 0x40] |= 3;
if (dev_addr == TW_CHIP_OFFSET_ADDR(0))
tbl_tw2815_sfr[0x62 - 0x40] |= 1 << 6;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(1))
tbl_tw2815_sfr[0x62 - 0x40] |= 0 << 6;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(2))
tbl_tw2815_sfr[0x62 - 0x40] |= 0 << 6;
else if (dev_addr == TW_CHIP_OFFSET_ADDR(3))
tbl_tw2815_sfr[0x62 - 0x40] |= 2 << 6;
}
/* Output mode of R_ADATM pin (0 mixing, 1 record) */
/* tbl_tw2815_sfr[0x63 - 0x40] |= 0 << 2; */
/* 8KHz, used to be 16KHz, but changed for remote client compat */
tbl_tw2815_sfr[0x62 - 0x40] |= 0 << 2;
tbl_tw2815_sfr[0x6c - 0x40] |= 0 << 2;
/* Playback of right channel */
tbl_tw2815_sfr[0x6c - 0x40] |= 1 << 5;
/* Reserved value (XXX ??) */
tbl_tw2815_sfr[0x5c - 0x40] |= 1 << 5;
/* Analog output gain and mix ratio playback on full */
tbl_tw2815_sfr[0x70 - 0x40] |= 0xff;
/* Select playback audio and mute all except */
tbl_tw2815_sfr[0x71 - 0x40] |= 0x10;
tbl_tw2815_sfr[0x6d - 0x40] |= 0x0f;
/* End of audio configuration */
for (ch = 0; ch < 4; ch++) {
tbl_tw2815_common[0x0d] &= ~3;
switch (ch) {
case 0:
tbl_tw2815_common[0x0d] |= 0x21;
break;
case 1:
tbl_tw2815_common[0x0d] |= 0x20;
break;
case 2:
tbl_tw2815_common[0x0d] |= 0x23;
break;
case 3:
tbl_tw2815_common[0x0d] |= 0x22;
break;
}
for (i = 0; i < 0x0f; i++) {
if (i == 0x00)
continue; /* read-only */
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW,
dev_addr, (ch * 0x10) + i,
tbl_tw2815_common[i]);
}
}
for (i = 0x40; i < 0x76; i++) {
/* Skip read-only and nop registers */
if (i == 0x40 || i == 0x59 || i == 0x5a ||
i == 0x5d || i == 0x5e || i == 0x5f)
continue;
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW, dev_addr, i,
tbl_tw2815_sfr[i - 0x40]);
}
return 0;
}
#define FIRST_ACTIVE_LINE 0x0008
#define LAST_ACTIVE_LINE 0x0102
static void saa712x_write_regs(struct solo_dev *dev, const u8 *vals,
int start, int n)
{
for (; start < n; start++, vals++) {
/* Skip read-only registers */
switch (start) {
/* case 0x00 ... 0x25: */
case 0x2e ... 0x37:
case 0x60:
case 0x7d:
continue;
}
solo_i2c_writebyte(dev, SOLO_I2C_SAA, 0x46, start, *vals);
}
}
#define SAA712x_reg7c (0x80 | ((LAST_ACTIVE_LINE & 0x100) >> 2) \
| ((FIRST_ACTIVE_LINE & 0x100) >> 4))
static void saa712x_setup(struct solo_dev *dev)
{
const int reg_start = 0x26;
static const u8 saa7128_regs_ntsc[] = {
/* :0x26 */
0x0d, 0x00,
/* :0x28 */
0x59, 0x1d, 0x75, 0x3f, 0x06, 0x3f,
/* :0x2e XXX: read-only */
0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* :0x38 */
0x1a, 0x1a, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00,
/* :0x40 */
0x00, 0x00, 0x00, 0x68, 0x10, 0x97, 0x4c, 0x18,
0x9b, 0x93, 0x9f, 0xff, 0x7c, 0x34, 0x3f, 0x3f,
/* :0x50 */
0x3f, 0x83, 0x83, 0x80, 0x0d, 0x0f, 0xc3, 0x06,
0x02, 0x80, 0x71, 0x77, 0xa7, 0x67, 0x66, 0x2e,
/* :0x60 */
0x7b, 0x11, 0x4f, 0x1f, 0x7c, 0xf0, 0x21, 0x77,
0x41, 0x88, 0x41, 0x52, 0xed, 0x10, 0x10, 0x00,
/* :0x70 */
0x41, 0xc3, 0x00, 0x3e, 0xb8, 0x02, 0x00, 0x00,
0x00, 0x00, FIRST_ACTIVE_LINE, LAST_ACTIVE_LINE & 0xff,
SAA712x_reg7c, 0x00, 0xff, 0xff,
}, saa7128_regs_pal[] = {
/* :0x26 */
0x0d, 0x00,
/* :0x28 */
0xe1, 0x1d, 0x75, 0x3f, 0x06, 0x3f,
/* :0x2e XXX: read-only */
0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* :0x38 */
0x1a, 0x1a, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00,
/* :0x40 */
0x00, 0x00, 0x00, 0x68, 0x10, 0x97, 0x4c, 0x18,
0x9b, 0x93, 0x9f, 0xff, 0x7c, 0x34, 0x3f, 0x3f,
/* :0x50 */
0x3f, 0x83, 0x83, 0x80, 0x0d, 0x0f, 0xc3, 0x06,
0x02, 0x80, 0x0f, 0x77, 0xa7, 0x67, 0x66, 0x2e,
/* :0x60 */
0x7b, 0x02, 0x35, 0xcb, 0x8a, 0x09, 0x2a, 0x77,
0x41, 0x88, 0x41, 0x52, 0xf1, 0x10, 0x20, 0x00,
/* :0x70 */
0x41, 0xc3, 0x00, 0x3e, 0xb8, 0x02, 0x00, 0x00,
0x00, 0x00, 0x12, 0x30,
SAA712x_reg7c | 0x40, 0x00, 0xff, 0xff,
};
if (dev->video_type == SOLO_VO_FMT_TYPE_PAL)
saa712x_write_regs(dev, saa7128_regs_pal, reg_start,
sizeof(saa7128_regs_pal));
else
saa712x_write_regs(dev, saa7128_regs_ntsc, reg_start,
sizeof(saa7128_regs_ntsc));
}
int solo_tw28_init(struct solo_dev *solo_dev)
{
int i;
u8 value;
solo_dev->tw28_cnt = 0;
/* Detect techwell chip type(s) */
for (i = 0; i < solo_dev->nr_chans / 4; i++) {
value = solo_i2c_readbyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(i), 0xFF);
switch (value >> 3) {
case 0x18:
solo_dev->tw2865 |= 1 << i;
solo_dev->tw28_cnt++;
break;
case 0x0c:
case 0x0d:
solo_dev->tw2864 |= 1 << i;
solo_dev->tw28_cnt++;
break;
default:
value = solo_i2c_readbyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(i),
0x59);
if ((value >> 3) == 0x04) {
solo_dev->tw2815 |= 1 << i;
solo_dev->tw28_cnt++;
}
}
}
if (solo_dev->tw28_cnt != (solo_dev->nr_chans >> 2)) {
dev_err(&solo_dev->pdev->dev,
"Could not initialize any techwell chips\n");
return -EINVAL;
}
saa712x_setup(solo_dev);
for (i = 0; i < solo_dev->tw28_cnt; i++) {
if ((solo_dev->tw2865 & (1 << i)))
tw2865_setup(solo_dev, TW_CHIP_OFFSET_ADDR(i));
else if ((solo_dev->tw2864 & (1 << i)))
tw2864_setup(solo_dev, TW_CHIP_OFFSET_ADDR(i));
else
tw2815_setup(solo_dev, TW_CHIP_OFFSET_ADDR(i));
}
return 0;
}
/*
* We accessed the video status signal in the Techwell chip through
* iic/i2c because the video status reported by register REG_VI_STATUS1
* (address 0x012C) of the SOLO6010 chip doesn't give the correct video
* status signal values.
*/
int tw28_get_video_status(struct solo_dev *solo_dev, u8 ch)
{
u8 val, chip_num;
/* Get the right chip and on-chip channel */
chip_num = ch / 4;
ch %= 4;
val = tw_readbyte(solo_dev, chip_num, TW286x_AV_STAT_ADDR,
TW_AV_STAT_ADDR) & 0x0f;
return val & (1 << ch) ? 1 : 0;
}
#if 0
/* Status of audio from up to 4 techwell chips are combined into 1 variable.
* See techwell datasheet for details. */
u16 tw28_get_audio_status(struct solo_dev *solo_dev)
{
u8 val;
u16 status = 0;
int i;
for (i = 0; i < solo_dev->tw28_cnt; i++) {
val = (tw_readbyte(solo_dev, i, TW286x_AV_STAT_ADDR,
TW_AV_STAT_ADDR) & 0xf0) >> 4;
status |= val << (i * 4);
}
return status;
}
#endif
bool tw28_has_sharpness(struct solo_dev *solo_dev, u8 ch)
{
return is_tw286x(solo_dev, ch / 4);
}
int tw28_set_ctrl_val(struct solo_dev *solo_dev, u32 ctrl, u8 ch,
s32 val)
{
char sval;
u8 chip_num;
/* Get the right chip and on-chip channel */
chip_num = ch / 4;
ch %= 4;
if (val > 255 || val < 0)
return -ERANGE;
switch (ctrl) {
case V4L2_CID_SHARPNESS:
/* Only 286x has sharpness */
if (is_tw286x(solo_dev, chip_num)) {
u8 v = solo_i2c_readbyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_num),
TW286x_SHARPNESS(chip_num));
v &= 0xf0;
v |= val;
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_num),
TW286x_SHARPNESS(chip_num), v);
} else {
return -EINVAL;
}
break;
case V4L2_CID_HUE:
if (is_tw286x(solo_dev, chip_num))
sval = val - 128;
else
sval = (char)val;
tw_writebyte(solo_dev, chip_num, TW286x_HUE_ADDR(ch),
TW_HUE_ADDR(ch), sval);
break;
case V4L2_CID_SATURATION:
/* 286x chips have a U and V component for saturation */
if (is_tw286x(solo_dev, chip_num)) {
solo_i2c_writebyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_num),
TW286x_SATURATIONU_ADDR(ch), val);
}
tw_writebyte(solo_dev, chip_num, TW286x_SATURATIONV_ADDR(ch),
TW_SATURATION_ADDR(ch), val);
break;
case V4L2_CID_CONTRAST:
tw_writebyte(solo_dev, chip_num, TW286x_CONTRAST_ADDR(ch),
TW_CONTRAST_ADDR(ch), val);
break;
case V4L2_CID_BRIGHTNESS:
if (is_tw286x(solo_dev, chip_num))
sval = val - 128;
else
sval = (char)val;
tw_writebyte(solo_dev, chip_num, TW286x_BRIGHTNESS_ADDR(ch),
TW_BRIGHTNESS_ADDR(ch), sval);
break;
default:
return -EINVAL;
}
return 0;
}
int tw28_get_ctrl_val(struct solo_dev *solo_dev, u32 ctrl, u8 ch,
s32 *val)
{
u8 rval, chip_num;
/* Get the right chip and on-chip channel */
chip_num = ch / 4;
ch %= 4;
switch (ctrl) {
case V4L2_CID_SHARPNESS:
/* Only 286x has sharpness */
if (is_tw286x(solo_dev, chip_num)) {
rval = solo_i2c_readbyte(solo_dev, SOLO_I2C_TW,
TW_CHIP_OFFSET_ADDR(chip_num),
TW286x_SHARPNESS(chip_num));
*val = rval & 0x0f;
} else
*val = 0;
break;
case V4L2_CID_HUE:
rval = tw_readbyte(solo_dev, chip_num, TW286x_HUE_ADDR(ch),
TW_HUE_ADDR(ch));
if (is_tw286x(solo_dev, chip_num))
*val = (s32)((char)rval) + 128;
else
*val = rval;
break;
case V4L2_CID_SATURATION:
*val = tw_readbyte(solo_dev, chip_num,
TW286x_SATURATIONU_ADDR(ch),
TW_SATURATION_ADDR(ch));
break;
case V4L2_CID_CONTRAST:
*val = tw_readbyte(solo_dev, chip_num,
TW286x_CONTRAST_ADDR(ch),
TW_CONTRAST_ADDR(ch));
break;
case V4L2_CID_BRIGHTNESS:
rval = tw_readbyte(solo_dev, chip_num,
TW286x_BRIGHTNESS_ADDR(ch),
TW_BRIGHTNESS_ADDR(ch));
if (is_tw286x(solo_dev, chip_num))
*val = (s32)((char)rval) + 128;
else
*val = rval;
break;
default:
return -EINVAL;
}
return 0;
}
#if 0
/*
* For audio output volume, the output channel is only 1. In this case we
* don't need to offset TW_CHIP_OFFSET_ADDR. The TW_CHIP_OFFSET_ADDR used
* is the base address of the techwell chip.
*/
void tw2815_Set_AudioOutVol(struct solo_dev *solo_dev, unsigned int u_val)
{
unsigned int val;
unsigned int chip_num;
chip_num = (solo_dev->nr_chans - 1) / 4;
val = tw_readbyte(solo_dev, chip_num, TW286x_AUDIO_OUTPUT_VOL_ADDR,
TW_AUDIO_OUTPUT_VOL_ADDR);
u_val = (val & 0x0f) | (u_val << 4);
tw_writebyte(solo_dev, chip_num, TW286x_AUDIO_OUTPUT_VOL_ADDR,
TW_AUDIO_OUTPUT_VOL_ADDR, u_val);
}
#endif
u8 tw28_get_audio_gain(struct solo_dev *solo_dev, u8 ch)
{
u8 val;
u8 chip_num;
/* Get the right chip and on-chip channel */
chip_num = ch / 4;
ch %= 4;
val = tw_readbyte(solo_dev, chip_num,
TW286x_AUDIO_INPUT_GAIN_ADDR(ch),
TW_AUDIO_INPUT_GAIN_ADDR(ch));
return (ch % 2) ? (val >> 4) : (val & 0x0f);
}
void tw28_set_audio_gain(struct solo_dev *solo_dev, u8 ch, u8 val)
{
u8 old_val;
u8 chip_num;
/* Get the right chip and on-chip channel */
chip_num = ch / 4;
ch %= 4;
old_val = tw_readbyte(solo_dev, chip_num,
TW286x_AUDIO_INPUT_GAIN_ADDR(ch),
TW_AUDIO_INPUT_GAIN_ADDR(ch));
val = (old_val & ((ch % 2) ? 0x0f : 0xf0)) |
((ch % 2) ? (val << 4) : val);
tw_writebyte(solo_dev, chip_num, TW286x_AUDIO_INPUT_GAIN_ADDR(ch),
TW_AUDIO_INPUT_GAIN_ADDR(ch), val);
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-tw28.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/videodev2.h>
#include <media/v4l2-ioctl.h>
#include "solo6x10.h"
#define SOLO_VCLK_DELAY 3
#define SOLO_PROGRESSIVE_VSIZE 1024
#define SOLO_MOT_THRESH_W 64
#define SOLO_MOT_THRESH_H 64
#define SOLO_MOT_THRESH_SIZE 8192
#define SOLO_MOT_THRESH_REAL (SOLO_MOT_THRESH_W * SOLO_MOT_THRESH_H)
#define SOLO_MOT_FLAG_SIZE 1024
#define SOLO_MOT_FLAG_AREA (SOLO_MOT_FLAG_SIZE * 16)
static void solo_vin_config(struct solo_dev *solo_dev)
{
solo_dev->vin_hstart = 8;
solo_dev->vin_vstart = 2;
solo_reg_write(solo_dev, SOLO_SYS_VCLK,
SOLO_VCLK_SELECT(2) |
SOLO_VCLK_VIN1415_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN1213_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN1011_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN0809_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN0607_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN0405_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN0203_DELAY(SOLO_VCLK_DELAY) |
SOLO_VCLK_VIN0001_DELAY(SOLO_VCLK_DELAY));
solo_reg_write(solo_dev, SOLO_VI_ACT_I_P,
SOLO_VI_H_START(solo_dev->vin_hstart) |
SOLO_VI_V_START(solo_dev->vin_vstart) |
SOLO_VI_V_STOP(solo_dev->vin_vstart +
solo_dev->video_vsize));
solo_reg_write(solo_dev, SOLO_VI_ACT_I_S,
SOLO_VI_H_START(solo_dev->vout_hstart) |
SOLO_VI_V_START(solo_dev->vout_vstart) |
SOLO_VI_V_STOP(solo_dev->vout_vstart +
solo_dev->video_vsize));
solo_reg_write(solo_dev, SOLO_VI_ACT_P,
SOLO_VI_H_START(0) |
SOLO_VI_V_START(1) |
SOLO_VI_V_STOP(SOLO_PROGRESSIVE_VSIZE));
solo_reg_write(solo_dev, SOLO_VI_CH_FORMAT,
SOLO_VI_FD_SEL_MASK(0) | SOLO_VI_PROG_MASK(0));
/* On 6110, initialize mozaic darkness strength */
if (solo_dev->type == SOLO_DEV_6010)
solo_reg_write(solo_dev, SOLO_VI_FMT_CFG, 0);
else
solo_reg_write(solo_dev, SOLO_VI_FMT_CFG, 16 << 22);
solo_reg_write(solo_dev, SOLO_VI_PAGE_SW, 2);
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
solo_reg_write(solo_dev, SOLO_VI_PB_CONFIG,
SOLO_VI_PB_USER_MODE);
solo_reg_write(solo_dev, SOLO_VI_PB_RANGE_HV,
SOLO_VI_PB_HSIZE(858) | SOLO_VI_PB_VSIZE(246));
solo_reg_write(solo_dev, SOLO_VI_PB_ACT_V,
SOLO_VI_PB_VSTART(4) |
SOLO_VI_PB_VSTOP(4 + 240));
} else {
solo_reg_write(solo_dev, SOLO_VI_PB_CONFIG,
SOLO_VI_PB_USER_MODE | SOLO_VI_PB_PAL);
solo_reg_write(solo_dev, SOLO_VI_PB_RANGE_HV,
SOLO_VI_PB_HSIZE(864) | SOLO_VI_PB_VSIZE(294));
solo_reg_write(solo_dev, SOLO_VI_PB_ACT_V,
SOLO_VI_PB_VSTART(4) |
SOLO_VI_PB_VSTOP(4 + 288));
}
solo_reg_write(solo_dev, SOLO_VI_PB_ACT_H, SOLO_VI_PB_HSTART(16) |
SOLO_VI_PB_HSTOP(16 + 720));
}
static void solo_vout_config_cursor(struct solo_dev *dev)
{
int i;
/* Load (blank) cursor bitmap mask (2bpp) */
for (i = 0; i < 20; i++)
solo_reg_write(dev, SOLO_VO_CURSOR_MASK(i), 0);
solo_reg_write(dev, SOLO_VO_CURSOR_POS, 0);
solo_reg_write(dev, SOLO_VO_CURSOR_CLR,
(0x80 << 24) | (0x80 << 16) | (0x10 << 8) | 0x80);
solo_reg_write(dev, SOLO_VO_CURSOR_CLR2, (0xe0 << 8) | 0x80);
}
static void solo_vout_config(struct solo_dev *solo_dev)
{
solo_dev->vout_hstart = 6;
solo_dev->vout_vstart = 8;
solo_reg_write(solo_dev, SOLO_VO_FMT_ENC,
solo_dev->video_type |
SOLO_VO_USER_COLOR_SET_NAV |
SOLO_VO_USER_COLOR_SET_NAH |
SOLO_VO_NA_COLOR_Y(0) |
SOLO_VO_NA_COLOR_CB(0) |
SOLO_VO_NA_COLOR_CR(0));
solo_reg_write(solo_dev, SOLO_VO_ACT_H,
SOLO_VO_H_START(solo_dev->vout_hstart) |
SOLO_VO_H_STOP(solo_dev->vout_hstart +
solo_dev->video_hsize));
solo_reg_write(solo_dev, SOLO_VO_ACT_V,
SOLO_VO_V_START(solo_dev->vout_vstart) |
SOLO_VO_V_STOP(solo_dev->vout_vstart +
solo_dev->video_vsize));
solo_reg_write(solo_dev, SOLO_VO_RANGE_HV,
SOLO_VO_H_LEN(solo_dev->video_hsize) |
SOLO_VO_V_LEN(solo_dev->video_vsize));
/* Border & background colors */
solo_reg_write(solo_dev, SOLO_VO_BORDER_LINE_COLOR,
(0xa0 << 24) | (0x88 << 16) | (0xa0 << 8) | 0x88);
solo_reg_write(solo_dev, SOLO_VO_BORDER_FILL_COLOR,
(0x10 << 24) | (0x8f << 16) | (0x10 << 8) | 0x8f);
solo_reg_write(solo_dev, SOLO_VO_BKG_COLOR,
(16 << 24) | (128 << 16) | (16 << 8) | 128);
solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
solo_reg_write(solo_dev, SOLO_VI_WIN_SW, 0);
solo_reg_write(solo_dev, SOLO_VO_ZOOM_CTRL, 0);
solo_reg_write(solo_dev, SOLO_VO_FREEZE_CTRL, 0);
solo_reg_write(solo_dev, SOLO_VO_DISP_CTRL, SOLO_VO_DISP_ON |
SOLO_VO_DISP_ERASE_COUNT(8) |
SOLO_VO_DISP_BASE(SOLO_DISP_EXT_ADDR));
solo_vout_config_cursor(solo_dev);
/* Enable channels we support */
solo_reg_write(solo_dev, SOLO_VI_CH_ENA,
(1 << solo_dev->nr_chans) - 1);
}
static int solo_dma_vin_region(struct solo_dev *solo_dev, u32 off,
u16 val, int reg_size)
{
__le16 *buf;
const int n = 64, size = n * sizeof(*buf);
int i, ret = 0;
buf = kmalloc(size, GFP_KERNEL);
if (!buf)
return -ENOMEM;
for (i = 0; i < n; i++)
buf[i] = cpu_to_le16(val);
for (i = 0; i < reg_size; i += size) {
ret = solo_p2m_dma(solo_dev, 1, buf,
SOLO_MOTION_EXT_ADDR(solo_dev) + off + i,
size, 0, 0);
if (ret)
break;
}
kfree(buf);
return ret;
}
int solo_set_motion_threshold(struct solo_dev *solo_dev, u8 ch, u16 val)
{
if (ch > solo_dev->nr_chans)
return -EINVAL;
return solo_dma_vin_region(solo_dev, SOLO_MOT_FLAG_AREA +
(ch * SOLO_MOT_THRESH_SIZE * 2),
val, SOLO_MOT_THRESH_SIZE);
}
int solo_set_motion_block(struct solo_dev *solo_dev, u8 ch,
const u16 *thresholds)
{
const unsigned size = sizeof(u16) * 64;
u32 off = SOLO_MOT_FLAG_AREA + ch * SOLO_MOT_THRESH_SIZE * 2;
__le16 *buf;
int x, y;
int ret = 0;
buf = kzalloc(size, GFP_KERNEL);
if (buf == NULL)
return -ENOMEM;
for (y = 0; y < SOLO_MOTION_SZ; y++) {
for (x = 0; x < SOLO_MOTION_SZ; x++)
buf[x] = cpu_to_le16(thresholds[y * SOLO_MOTION_SZ + x]);
ret |= solo_p2m_dma(solo_dev, 1, buf,
SOLO_MOTION_EXT_ADDR(solo_dev) + off + y * size,
size, 0, 0);
}
kfree(buf);
return ret;
}
/* First 8k is motion flag (512 bytes * 16). Following that is an 8k+8k
* threshold and working table for each channel. At least that's what the
* spec says. However, this code (taken from rdk) has some mystery 8k
* block right after the flag area, before the first thresh table. */
static void solo_motion_config(struct solo_dev *solo_dev)
{
int i;
for (i = 0; i < solo_dev->nr_chans; i++) {
/* Clear motion flag area */
solo_dma_vin_region(solo_dev, i * SOLO_MOT_FLAG_SIZE, 0x0000,
SOLO_MOT_FLAG_SIZE);
/* Clear working cache table */
solo_dma_vin_region(solo_dev, SOLO_MOT_FLAG_AREA +
(i * SOLO_MOT_THRESH_SIZE * 2) +
SOLO_MOT_THRESH_SIZE, 0x0000,
SOLO_MOT_THRESH_SIZE);
/* Set default threshold table */
solo_set_motion_threshold(solo_dev, i, SOLO_DEF_MOT_THRESH);
}
/* Default motion settings */
solo_reg_write(solo_dev, SOLO_VI_MOT_ADR, SOLO_VI_MOTION_EN(0) |
(SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
solo_reg_write(solo_dev, SOLO_VI_MOT_CTRL,
SOLO_VI_MOTION_FRAME_COUNT(3) |
SOLO_VI_MOTION_SAMPLE_LENGTH(solo_dev->video_hsize / 16)
/* | SOLO_VI_MOTION_INTR_START_STOP */
| SOLO_VI_MOTION_SAMPLE_COUNT(10));
solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
}
int solo_disp_init(struct solo_dev *solo_dev)
{
int i;
solo_dev->video_hsize = 704;
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
solo_dev->video_vsize = 240;
solo_dev->fps = 30;
} else {
solo_dev->video_vsize = 288;
solo_dev->fps = 25;
}
solo_vin_config(solo_dev);
solo_motion_config(solo_dev);
solo_vout_config(solo_dev);
for (i = 0; i < solo_dev->nr_chans; i++)
solo_reg_write(solo_dev, SOLO_VI_WIN_ON(i), 1);
return 0;
}
void solo_disp_exit(struct solo_dev *solo_dev)
{
int i;
solo_reg_write(solo_dev, SOLO_VO_DISP_CTRL, 0);
solo_reg_write(solo_dev, SOLO_VO_ZOOM_CTRL, 0);
solo_reg_write(solo_dev, SOLO_VO_FREEZE_CTRL, 0);
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(i), 0);
solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(i), 0);
solo_reg_write(solo_dev, SOLO_VI_WIN_ON(i), 0);
}
/* Set default border */
for (i = 0; i < 5; i++)
solo_reg_write(solo_dev, SOLO_VO_BORDER_X(i), 0);
for (i = 0; i < 5; i++)
solo_reg_write(solo_dev, SOLO_VO_BORDER_Y(i), 0);
solo_reg_write(solo_dev, SOLO_VO_BORDER_LINE_MASK, 0);
solo_reg_write(solo_dev, SOLO_VO_BORDER_FILL_MASK, 0);
solo_reg_write(solo_dev, SOLO_VO_RECTANGLE_CTRL(0), 0);
solo_reg_write(solo_dev, SOLO_VO_RECTANGLE_START(0), 0);
solo_reg_write(solo_dev, SOLO_VO_RECTANGLE_STOP(0), 0);
solo_reg_write(solo_dev, SOLO_VO_RECTANGLE_CTRL(1), 0);
solo_reg_write(solo_dev, SOLO_VO_RECTANGLE_START(1), 0);
solo_reg_write(solo_dev, SOLO_VO_RECTANGLE_STOP(1), 0);
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-disp.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/videodev2.h>
#include <linux/delay.h>
#include <linux/sysfs.h>
#include <linux/ktime.h>
#include <linux/slab.h>
#include "solo6x10.h"
#include "solo6x10-tw28.h"
MODULE_DESCRIPTION("Softlogic 6x10 MPEG4/H.264/G.723 CODEC V4L2/ALSA Driver");
MODULE_AUTHOR("Bluecherry <[email protected]>");
MODULE_VERSION(SOLO6X10_VERSION);
MODULE_LICENSE("GPL");
static unsigned video_nr = -1;
module_param(video_nr, uint, 0644);
MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect (default)");
static int full_eeprom; /* default is only top 64B */
module_param(full_eeprom, uint, 0644);
MODULE_PARM_DESC(full_eeprom, "Allow access to full 128B EEPROM (dangerous)");
static void solo_set_time(struct solo_dev *solo_dev)
{
struct timespec64 ts;
ktime_get_ts64(&ts);
/* no overflow because we use monotonic timestamps */
solo_reg_write(solo_dev, SOLO_TIMER_SEC, (u32)ts.tv_sec);
solo_reg_write(solo_dev, SOLO_TIMER_USEC, (u32)ts.tv_nsec / NSEC_PER_USEC);
}
static void solo_timer_sync(struct solo_dev *solo_dev)
{
u32 sec, usec;
struct timespec64 ts;
long diff;
if (solo_dev->type != SOLO_DEV_6110)
return;
if (++solo_dev->time_sync < 60)
return;
solo_dev->time_sync = 0;
sec = solo_reg_read(solo_dev, SOLO_TIMER_SEC);
usec = solo_reg_read(solo_dev, SOLO_TIMER_USEC);
ktime_get_ts64(&ts);
diff = (s32)ts.tv_sec - (s32)sec;
diff = (diff * 1000000)
+ ((s32)(ts.tv_nsec / NSEC_PER_USEC) - (s32)usec);
if (diff > 1000 || diff < -1000) {
solo_set_time(solo_dev);
} else if (diff) {
long usec_lsb = solo_dev->usec_lsb;
usec_lsb -= diff / 4;
if (usec_lsb < 0)
usec_lsb = 0;
else if (usec_lsb > 255)
usec_lsb = 255;
solo_dev->usec_lsb = usec_lsb;
solo_reg_write(solo_dev, SOLO_TIMER_USEC_LSB,
solo_dev->usec_lsb);
}
}
static irqreturn_t solo_isr(int irq, void *data)
{
struct solo_dev *solo_dev = data;
u32 status;
int i;
status = solo_reg_read(solo_dev, SOLO_IRQ_STAT);
if (!status)
return IRQ_NONE;
/* Acknowledge all interrupts immediately */
solo_reg_write(solo_dev, SOLO_IRQ_STAT, status);
if (status & SOLO_IRQ_PCI_ERR)
solo_p2m_error_isr(solo_dev);
for (i = 0; i < SOLO_NR_P2M; i++)
if (status & SOLO_IRQ_P2M(i))
solo_p2m_isr(solo_dev, i);
if (status & SOLO_IRQ_IIC)
solo_i2c_isr(solo_dev);
if (status & SOLO_IRQ_VIDEO_IN) {
solo_video_in_isr(solo_dev);
solo_timer_sync(solo_dev);
}
if (status & SOLO_IRQ_ENCODER)
solo_enc_v4l2_isr(solo_dev);
if (status & SOLO_IRQ_G723)
solo_g723_isr(solo_dev);
return IRQ_HANDLED;
}
static void free_solo_dev(struct solo_dev *solo_dev)
{
struct pci_dev *pdev = solo_dev->pdev;
if (solo_dev->dev.parent)
device_unregister(&solo_dev->dev);
if (solo_dev->reg_base) {
/* Bring down the sub-devices first */
solo_g723_exit(solo_dev);
solo_enc_v4l2_exit(solo_dev);
solo_enc_exit(solo_dev);
solo_v4l2_exit(solo_dev);
solo_disp_exit(solo_dev);
solo_gpio_exit(solo_dev);
solo_p2m_exit(solo_dev);
solo_i2c_exit(solo_dev);
/* Now cleanup the PCI device */
solo_irq_off(solo_dev, ~0);
free_irq(pdev->irq, solo_dev);
pci_iounmap(pdev, solo_dev->reg_base);
}
pci_release_regions(pdev);
pci_disable_device(pdev);
v4l2_device_unregister(&solo_dev->v4l2_dev);
pci_set_drvdata(pdev, NULL);
kfree(solo_dev);
}
static ssize_t eeprom_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
u16 *p = (u16 *)buf;
int i;
if (count & 0x1)
dev_warn(dev, "EEPROM Write not aligned (truncating)\n");
if (!full_eeprom && count > 64) {
dev_warn(dev, "EEPROM Write truncated to 64 bytes\n");
count = 64;
} else if (full_eeprom && count > 128) {
dev_warn(dev, "EEPROM Write truncated to 128 bytes\n");
count = 128;
}
solo_eeprom_ewen(solo_dev, 1);
for (i = full_eeprom ? 0 : 32; i < min((int)(full_eeprom ? 64 : 32),
(int)(count / 2)); i++)
solo_eeprom_write(solo_dev, i, cpu_to_be16(p[i]));
solo_eeprom_ewen(solo_dev, 0);
return count;
}
static ssize_t eeprom_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
u16 *p = (u16 *)buf;
int count = (full_eeprom ? 128 : 64);
int i;
for (i = (full_eeprom ? 0 : 32); i < (count / 2); i++)
p[i] = be16_to_cpu(solo_eeprom_read(solo_dev, i));
return count;
}
static ssize_t p2m_timeouts_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
return sprintf(buf, "%d\n", solo_dev->p2m_timeouts);
}
static ssize_t sdram_size_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
return sprintf(buf, "%dMegs\n", solo_dev->sdram_size >> 20);
}
static ssize_t tw28xx_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
return sprintf(buf, "tw2815[%d] tw2864[%d] tw2865[%d]\n",
hweight32(solo_dev->tw2815),
hweight32(solo_dev->tw2864),
hweight32(solo_dev->tw2865));
}
static ssize_t input_map_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
unsigned int val;
char *out = buf;
val = solo_reg_read(solo_dev, SOLO_VI_CH_SWITCH_0);
out += sprintf(out, "Channel 0 => Input %d\n", val & 0x1f);
out += sprintf(out, "Channel 1 => Input %d\n", (val >> 5) & 0x1f);
out += sprintf(out, "Channel 2 => Input %d\n", (val >> 10) & 0x1f);
out += sprintf(out, "Channel 3 => Input %d\n", (val >> 15) & 0x1f);
out += sprintf(out, "Channel 4 => Input %d\n", (val >> 20) & 0x1f);
out += sprintf(out, "Channel 5 => Input %d\n", (val >> 25) & 0x1f);
val = solo_reg_read(solo_dev, SOLO_VI_CH_SWITCH_1);
out += sprintf(out, "Channel 6 => Input %d\n", val & 0x1f);
out += sprintf(out, "Channel 7 => Input %d\n", (val >> 5) & 0x1f);
out += sprintf(out, "Channel 8 => Input %d\n", (val >> 10) & 0x1f);
out += sprintf(out, "Channel 9 => Input %d\n", (val >> 15) & 0x1f);
out += sprintf(out, "Channel 10 => Input %d\n", (val >> 20) & 0x1f);
out += sprintf(out, "Channel 11 => Input %d\n", (val >> 25) & 0x1f);
val = solo_reg_read(solo_dev, SOLO_VI_CH_SWITCH_2);
out += sprintf(out, "Channel 12 => Input %d\n", val & 0x1f);
out += sprintf(out, "Channel 13 => Input %d\n", (val >> 5) & 0x1f);
out += sprintf(out, "Channel 14 => Input %d\n", (val >> 10) & 0x1f);
out += sprintf(out, "Channel 15 => Input %d\n", (val >> 15) & 0x1f);
out += sprintf(out, "Spot Output => Input %d\n", (val >> 20) & 0x1f);
return out - buf;
}
static ssize_t p2m_timeout_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
unsigned long ms;
int ret = kstrtoul(buf, 10, &ms);
if (ret < 0 || ms > 200)
return -EINVAL;
solo_dev->p2m_jiffies = msecs_to_jiffies(ms);
return count;
}
static ssize_t p2m_timeout_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
return sprintf(buf, "%ums\n", jiffies_to_msecs(solo_dev->p2m_jiffies));
}
static ssize_t intervals_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
char *out = buf;
int fps = solo_dev->fps;
int i;
for (i = 0; i < solo_dev->nr_chans; i++) {
out += sprintf(out, "Channel %d: %d/%d (0x%08x)\n",
i, solo_dev->v4l2_enc[i]->interval, fps,
solo_reg_read(solo_dev, SOLO_CAP_CH_INTV(i)));
}
return out - buf;
}
static ssize_t sdram_offsets_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
char *out = buf;
out += sprintf(out, "DISP: 0x%08x @ 0x%08x\n",
SOLO_DISP_EXT_ADDR,
SOLO_DISP_EXT_SIZE);
out += sprintf(out, "EOSD: 0x%08x @ 0x%08x (0x%08x * %d)\n",
SOLO_EOSD_EXT_ADDR,
SOLO_EOSD_EXT_AREA(solo_dev),
SOLO_EOSD_EXT_SIZE(solo_dev),
SOLO_EOSD_EXT_AREA(solo_dev) /
SOLO_EOSD_EXT_SIZE(solo_dev));
out += sprintf(out, "MOTI: 0x%08x @ 0x%08x\n",
SOLO_MOTION_EXT_ADDR(solo_dev),
SOLO_MOTION_EXT_SIZE);
out += sprintf(out, "G723: 0x%08x @ 0x%08x\n",
SOLO_G723_EXT_ADDR(solo_dev),
SOLO_G723_EXT_SIZE);
out += sprintf(out, "CAPT: 0x%08x @ 0x%08x (0x%08x * %d)\n",
SOLO_CAP_EXT_ADDR(solo_dev),
SOLO_CAP_EXT_SIZE(solo_dev),
SOLO_CAP_PAGE_SIZE,
SOLO_CAP_EXT_SIZE(solo_dev) / SOLO_CAP_PAGE_SIZE);
out += sprintf(out, "EREF: 0x%08x @ 0x%08x (0x%08x * %d)\n",
SOLO_EREF_EXT_ADDR(solo_dev),
SOLO_EREF_EXT_AREA(solo_dev),
SOLO_EREF_EXT_SIZE,
SOLO_EREF_EXT_AREA(solo_dev) / SOLO_EREF_EXT_SIZE);
out += sprintf(out, "MPEG: 0x%08x @ 0x%08x\n",
SOLO_MP4E_EXT_ADDR(solo_dev),
SOLO_MP4E_EXT_SIZE(solo_dev));
out += sprintf(out, "JPEG: 0x%08x @ 0x%08x\n",
SOLO_JPEG_EXT_ADDR(solo_dev),
SOLO_JPEG_EXT_SIZE(solo_dev));
return out - buf;
}
static ssize_t sdram_show(struct file *file, struct kobject *kobj,
struct bin_attribute *a, char *buf,
loff_t off, size_t count)
{
struct device *dev = kobj_to_dev(kobj);
struct solo_dev *solo_dev =
container_of(dev, struct solo_dev, dev);
const int size = solo_dev->sdram_size;
if (off >= size)
return 0;
if (off + count > size)
count = size - off;
if (solo_p2m_dma(solo_dev, 0, buf, off, count, 0, 0))
return -EIO;
return count;
}
static const struct device_attribute solo_dev_attrs[] = {
__ATTR(eeprom, 0640, eeprom_show, eeprom_store),
__ATTR(p2m_timeout, 0644, p2m_timeout_show, p2m_timeout_store),
__ATTR_RO(p2m_timeouts),
__ATTR_RO(sdram_size),
__ATTR_RO(tw28xx),
__ATTR_RO(input_map),
__ATTR_RO(intervals),
__ATTR_RO(sdram_offsets),
};
static void solo_device_release(struct device *dev)
{
/* Do nothing */
}
static int solo_sysfs_init(struct solo_dev *solo_dev)
{
struct bin_attribute *sdram_attr = &solo_dev->sdram_attr;
struct device *dev = &solo_dev->dev;
const char *driver;
int i;
if (solo_dev->type == SOLO_DEV_6110)
driver = "solo6110";
else
driver = "solo6010";
dev->release = solo_device_release;
dev->parent = &solo_dev->pdev->dev;
set_dev_node(dev, dev_to_node(&solo_dev->pdev->dev));
dev_set_name(dev, "%s-%d-%d", driver, solo_dev->vfd->num,
solo_dev->nr_chans);
if (device_register(dev)) {
put_device(dev);
dev->parent = NULL;
return -ENOMEM;
}
for (i = 0; i < ARRAY_SIZE(solo_dev_attrs); i++) {
if (device_create_file(dev, &solo_dev_attrs[i])) {
device_unregister(dev);
return -ENOMEM;
}
}
sysfs_attr_init(&sdram_attr->attr);
sdram_attr->attr.name = "sdram";
sdram_attr->attr.mode = 0440;
sdram_attr->read = sdram_show;
sdram_attr->size = solo_dev->sdram_size;
if (device_create_bin_file(dev, sdram_attr)) {
device_unregister(dev);
return -ENOMEM;
}
return 0;
}
static int solo_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
struct solo_dev *solo_dev;
int ret;
u8 chip_id;
solo_dev = kzalloc(sizeof(*solo_dev), GFP_KERNEL);
if (solo_dev == NULL)
return -ENOMEM;
if (id->driver_data == SOLO_DEV_6010)
dev_info(&pdev->dev, "Probing Softlogic 6010\n");
else
dev_info(&pdev->dev, "Probing Softlogic 6110\n");
solo_dev->type = id->driver_data;
solo_dev->pdev = pdev;
ret = v4l2_device_register(&pdev->dev, &solo_dev->v4l2_dev);
if (ret)
goto fail_probe;
/* Only for during init */
solo_dev->p2m_jiffies = msecs_to_jiffies(100);
ret = pci_enable_device(pdev);
if (ret)
goto fail_probe;
pci_set_master(pdev);
/* RETRY/TRDY Timeout disabled */
pci_write_config_byte(pdev, 0x40, 0x00);
pci_write_config_byte(pdev, 0x41, 0x00);
ret = pci_request_regions(pdev, SOLO6X10_NAME);
if (ret)
goto fail_probe;
solo_dev->reg_base = pci_ioremap_bar(pdev, 0);
if (solo_dev->reg_base == NULL) {
ret = -ENOMEM;
goto fail_probe;
}
chip_id = solo_reg_read(solo_dev, SOLO_CHIP_OPTION) &
SOLO_CHIP_ID_MASK;
switch (chip_id) {
case 7:
solo_dev->nr_chans = 16;
solo_dev->nr_ext = 5;
break;
case 6:
solo_dev->nr_chans = 8;
solo_dev->nr_ext = 2;
break;
default:
dev_warn(&pdev->dev, "Invalid chip_id 0x%02x, assuming 4 ch\n",
chip_id);
fallthrough;
case 5:
solo_dev->nr_chans = 4;
solo_dev->nr_ext = 1;
}
/* Disable all interrupts to start */
solo_irq_off(solo_dev, ~0);
/* Initial global settings */
if (solo_dev->type == SOLO_DEV_6010) {
solo_dev->clock_mhz = 108;
solo_dev->sys_config = SOLO_SYS_CFG_SDRAM64BIT
| SOLO_SYS_CFG_INPUTDIV(25)
| SOLO_SYS_CFG_FEEDBACKDIV(solo_dev->clock_mhz * 2 - 2)
| SOLO_SYS_CFG_OUTDIV(3);
solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
} else {
u32 divq, divf;
solo_dev->clock_mhz = 135;
if (solo_dev->clock_mhz < 125) {
divq = 3;
divf = (solo_dev->clock_mhz * 4) / 3 - 1;
} else {
divq = 2;
divf = (solo_dev->clock_mhz * 2) / 3 - 1;
}
solo_reg_write(solo_dev, SOLO_PLL_CONFIG,
(1 << 20) | /* PLL_RANGE */
(8 << 15) | /* PLL_DIVR */
(divq << 12) |
(divf << 4) |
(1 << 1) /* PLL_FSEN */);
solo_dev->sys_config = SOLO_SYS_CFG_SDRAM64BIT;
}
solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
solo_reg_write(solo_dev, SOLO_TIMER_CLOCK_NUM,
solo_dev->clock_mhz - 1);
/* PLL locking time of 1ms */
mdelay(1);
ret = request_irq(pdev->irq, solo_isr, IRQF_SHARED, SOLO6X10_NAME,
solo_dev);
if (ret)
goto fail_probe;
/* Handle this from the start */
solo_irq_on(solo_dev, SOLO_IRQ_PCI_ERR);
ret = solo_i2c_init(solo_dev);
if (ret)
goto fail_probe;
/* Setup the DMA engine */
solo_reg_write(solo_dev, SOLO_DMA_CTRL,
SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
SOLO_DMA_CTRL_SDRAM_SIZE(2) |
SOLO_DMA_CTRL_SDRAM_CLK_INVERT |
SOLO_DMA_CTRL_READ_CLK_SELECT |
SOLO_DMA_CTRL_LATENCY(1));
/* Undocumented crap */
solo_reg_write(solo_dev, SOLO_DMA_CTRL1,
solo_dev->type == SOLO_DEV_6010 ? 0x100 : 0x300);
if (solo_dev->type != SOLO_DEV_6010) {
solo_dev->usec_lsb = 0x3f;
solo_set_time(solo_dev);
}
/* Disable watchdog */
solo_reg_write(solo_dev, SOLO_WATCHDOG, 0);
/* Initialize sub components */
ret = solo_p2m_init(solo_dev);
if (ret)
goto fail_probe;
ret = solo_disp_init(solo_dev);
if (ret)
goto fail_probe;
ret = solo_gpio_init(solo_dev);
if (ret)
goto fail_probe;
ret = solo_tw28_init(solo_dev);
if (ret)
goto fail_probe;
ret = solo_v4l2_init(solo_dev, video_nr);
if (ret)
goto fail_probe;
ret = solo_enc_init(solo_dev);
if (ret)
goto fail_probe;
ret = solo_enc_v4l2_init(solo_dev, video_nr);
if (ret)
goto fail_probe;
ret = solo_g723_init(solo_dev);
if (ret)
goto fail_probe;
ret = solo_sysfs_init(solo_dev);
if (ret)
goto fail_probe;
/* Now that init is over, set this lower */
solo_dev->p2m_jiffies = msecs_to_jiffies(20);
return 0;
fail_probe:
free_solo_dev(solo_dev);
return ret;
}
static void solo_pci_remove(struct pci_dev *pdev)
{
struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
struct solo_dev *solo_dev = container_of(v4l2_dev, struct solo_dev, v4l2_dev);
free_solo_dev(solo_dev);
}
static const struct pci_device_id solo_id_table[] = {
/* 6010 based cards */
{ PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC, PCI_DEVICE_ID_SOLO6010),
.driver_data = SOLO_DEV_6010 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_NEUSOLO_4),
.driver_data = SOLO_DEV_6010 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_NEUSOLO_9),
.driver_data = SOLO_DEV_6010 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_NEUSOLO_16),
.driver_data = SOLO_DEV_6010 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_SOLO_4),
.driver_data = SOLO_DEV_6010 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_SOLO_9),
.driver_data = SOLO_DEV_6010 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_SOLO_16),
.driver_data = SOLO_DEV_6010 },
/* 6110 based cards */
{ PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC, PCI_DEVICE_ID_SOLO6110),
.driver_data = SOLO_DEV_6110 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_6110_4),
.driver_data = SOLO_DEV_6110 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_6110_8),
.driver_data = SOLO_DEV_6110 },
{ PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_6110_16),
.driver_data = SOLO_DEV_6110 },
{0,}
};
MODULE_DEVICE_TABLE(pci, solo_id_table);
static struct pci_driver solo_pci_driver = {
.name = SOLO6X10_NAME,
.id_table = solo_id_table,
.probe = solo_pci_probe,
.remove = solo_pci_remove,
};
module_pci_driver(solo_pci_driver);
| linux-master | drivers/media/pci/solo6x10/solo6x10-core.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/font.h>
#include <linux/bitrev.h>
#include <linux/slab.h>
#include "solo6x10.h"
#define VI_PROG_HSIZE (1280 - 16)
#define VI_PROG_VSIZE (1024 - 16)
#define IRQ_LEVEL 2
static void solo_capture_config(struct solo_dev *solo_dev)
{
unsigned long height;
unsigned long width;
void *buf;
int i;
solo_reg_write(solo_dev, SOLO_CAP_BASE,
SOLO_CAP_MAX_PAGE((SOLO_CAP_EXT_SIZE(solo_dev)
- SOLO_CAP_PAGE_SIZE) >> 16)
| SOLO_CAP_BASE_ADDR(SOLO_CAP_EXT_ADDR(solo_dev) >> 16));
/* XXX: Undocumented bits at b17 and b24 */
if (solo_dev->type == SOLO_DEV_6110) {
/* NOTE: Ref driver has (62 << 24) here as well, but it causes
* wacked out frame timing on 4-port 6110. */
solo_reg_write(solo_dev, SOLO_CAP_BTW,
(1 << 17) | SOLO_CAP_PROG_BANDWIDTH(2) |
SOLO_CAP_MAX_BANDWIDTH(36));
} else {
solo_reg_write(solo_dev, SOLO_CAP_BTW,
(1 << 17) | SOLO_CAP_PROG_BANDWIDTH(2) |
SOLO_CAP_MAX_BANDWIDTH(32));
}
/* Set scale 1, 9 dimension */
width = solo_dev->video_hsize;
height = solo_dev->video_vsize;
solo_reg_write(solo_dev, SOLO_DIM_SCALE1,
SOLO_DIM_H_MB_NUM(width / 16) |
SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
SOLO_DIM_V_MB_NUM_FIELD(height / 16));
/* Set scale 2, 10 dimension */
width = solo_dev->video_hsize / 2;
height = solo_dev->video_vsize;
solo_reg_write(solo_dev, SOLO_DIM_SCALE2,
SOLO_DIM_H_MB_NUM(width / 16) |
SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
SOLO_DIM_V_MB_NUM_FIELD(height / 16));
/* Set scale 3, 11 dimension */
width = solo_dev->video_hsize / 2;
height = solo_dev->video_vsize / 2;
solo_reg_write(solo_dev, SOLO_DIM_SCALE3,
SOLO_DIM_H_MB_NUM(width / 16) |
SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
SOLO_DIM_V_MB_NUM_FIELD(height / 16));
/* Set scale 4, 12 dimension */
width = solo_dev->video_hsize / 3;
height = solo_dev->video_vsize / 3;
solo_reg_write(solo_dev, SOLO_DIM_SCALE4,
SOLO_DIM_H_MB_NUM(width / 16) |
SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
SOLO_DIM_V_MB_NUM_FIELD(height / 16));
/* Set scale 5, 13 dimension */
width = solo_dev->video_hsize / 4;
height = solo_dev->video_vsize / 2;
solo_reg_write(solo_dev, SOLO_DIM_SCALE5,
SOLO_DIM_H_MB_NUM(width / 16) |
SOLO_DIM_V_MB_NUM_FRAME(height / 8) |
SOLO_DIM_V_MB_NUM_FIELD(height / 16));
/* Progressive */
width = VI_PROG_HSIZE;
height = VI_PROG_VSIZE;
solo_reg_write(solo_dev, SOLO_DIM_PROG,
SOLO_DIM_H_MB_NUM(width / 16) |
SOLO_DIM_V_MB_NUM_FRAME(height / 16) |
SOLO_DIM_V_MB_NUM_FIELD(height / 16));
/* Clear OSD */
solo_reg_write(solo_dev, SOLO_VE_OSD_CH, 0);
solo_reg_write(solo_dev, SOLO_VE_OSD_BASE, SOLO_EOSD_EXT_ADDR >> 16);
solo_reg_write(solo_dev, SOLO_VE_OSD_CLR,
0xF0 << 16 | 0x80 << 8 | 0x80);
if (solo_dev->type == SOLO_DEV_6010)
solo_reg_write(solo_dev, SOLO_VE_OSD_OPT,
SOLO_VE_OSD_H_SHADOW | SOLO_VE_OSD_V_SHADOW);
else
solo_reg_write(solo_dev, SOLO_VE_OSD_OPT, SOLO_VE_OSD_V_DOUBLE
| SOLO_VE_OSD_H_SHADOW | SOLO_VE_OSD_V_SHADOW);
/* Clear OSG buffer */
buf = kzalloc(SOLO_EOSD_EXT_SIZE(solo_dev), GFP_KERNEL);
if (!buf)
return;
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_p2m_dma(solo_dev, 1, buf,
SOLO_EOSD_EXT_ADDR +
(SOLO_EOSD_EXT_SIZE(solo_dev) * i),
SOLO_EOSD_EXT_SIZE(solo_dev), 0, 0);
}
kfree(buf);
}
#define SOLO_OSD_WRITE_SIZE (16 * OSD_TEXT_MAX)
/* Should be called with enable_lock held */
int solo_osd_print(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
u8 *str = solo_enc->osd_text;
u8 *buf = solo_enc->osd_buf;
u32 reg;
const struct font_desc *vga = find_font("VGA8x16");
const u8 *vga_data;
int i, j;
if (WARN_ON_ONCE(!vga))
return -ENODEV;
reg = solo_reg_read(solo_dev, SOLO_VE_OSD_CH);
if (!*str) {
/* Disable OSD on this channel */
reg &= ~(1 << solo_enc->ch);
goto out;
}
memset(buf, 0, SOLO_OSD_WRITE_SIZE);
vga_data = (const u8 *)vga->data;
for (i = 0; *str; i++, str++) {
for (j = 0; j < 16; j++) {
buf[(j << 1) | (i & 1) | ((i & ~1) << 4)] =
bitrev8(vga_data[(*str << 4) | j]);
}
}
solo_p2m_dma(solo_dev, 1, buf,
SOLO_EOSD_EXT_ADDR_CHAN(solo_dev, solo_enc->ch),
SOLO_OSD_WRITE_SIZE, 0, 0);
/* Enable OSD on this channel */
reg |= (1 << solo_enc->ch);
out:
solo_reg_write(solo_dev, SOLO_VE_OSD_CH, reg);
return 0;
}
/*
* Set channel Quality Profile (0-3).
*/
void solo_s_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch,
unsigned int qp)
{
unsigned long flags;
unsigned int idx, reg;
if ((ch > 31) || (qp > 3))
return;
if (solo_dev->type == SOLO_DEV_6010)
return;
if (ch < 16) {
idx = 0;
reg = SOLO_VE_JPEG_QP_CH_L;
} else {
ch -= 16;
idx = 1;
reg = SOLO_VE_JPEG_QP_CH_H;
}
ch *= 2;
spin_lock_irqsave(&solo_dev->jpeg_qp_lock, flags);
solo_dev->jpeg_qp[idx] &= ~(3 << ch);
solo_dev->jpeg_qp[idx] |= (qp & 3) << ch;
solo_reg_write(solo_dev, reg, solo_dev->jpeg_qp[idx]);
spin_unlock_irqrestore(&solo_dev->jpeg_qp_lock, flags);
}
int solo_g_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch)
{
int idx;
if (solo_dev->type == SOLO_DEV_6010)
return 2;
if (WARN_ON_ONCE(ch > 31))
return 2;
if (ch < 16) {
idx = 0;
} else {
ch -= 16;
idx = 1;
}
ch *= 2;
return (solo_dev->jpeg_qp[idx] >> ch) & 3;
}
#define SOLO_QP_INIT 0xaaaaaaaa
static void solo_jpeg_config(struct solo_dev *solo_dev)
{
if (solo_dev->type == SOLO_DEV_6010) {
solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_TBL,
(2 << 24) | (2 << 16) | (2 << 8) | 2);
} else {
solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_TBL,
(4 << 24) | (3 << 16) | (2 << 8) | 1);
}
spin_lock_init(&solo_dev->jpeg_qp_lock);
/* Initialize Quality Profile for all channels */
solo_dev->jpeg_qp[0] = solo_dev->jpeg_qp[1] = SOLO_QP_INIT;
solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_CH_L, SOLO_QP_INIT);
solo_reg_write(solo_dev, SOLO_VE_JPEG_QP_CH_H, SOLO_QP_INIT);
solo_reg_write(solo_dev, SOLO_VE_JPEG_CFG,
(SOLO_JPEG_EXT_SIZE(solo_dev) & 0xffff0000) |
((SOLO_JPEG_EXT_ADDR(solo_dev) >> 16) & 0x0000ffff));
solo_reg_write(solo_dev, SOLO_VE_JPEG_CTRL, 0xffffffff);
if (solo_dev->type == SOLO_DEV_6110) {
solo_reg_write(solo_dev, SOLO_VE_JPEG_CFG1,
(0 << 16) | (30 << 8) | 60);
}
}
static void solo_mp4e_config(struct solo_dev *solo_dev)
{
int i;
u32 cfg;
solo_reg_write(solo_dev, SOLO_VE_CFG0,
SOLO_VE_INTR_CTRL(IRQ_LEVEL) |
SOLO_VE_BLOCK_SIZE(SOLO_MP4E_EXT_SIZE(solo_dev) >> 16) |
SOLO_VE_BLOCK_BASE(SOLO_MP4E_EXT_ADDR(solo_dev) >> 16));
cfg = SOLO_VE_BYTE_ALIGN(2) | SOLO_VE_INSERT_INDEX
| SOLO_VE_MOTION_MODE(0);
if (solo_dev->type != SOLO_DEV_6010) {
cfg |= SOLO_VE_MPEG_SIZE_H(
(SOLO_MP4E_EXT_SIZE(solo_dev) >> 24) & 0x0f);
cfg |= SOLO_VE_JPEG_SIZE_H(
(SOLO_JPEG_EXT_SIZE(solo_dev) >> 24) & 0x0f);
}
solo_reg_write(solo_dev, SOLO_VE_CFG1, cfg);
solo_reg_write(solo_dev, SOLO_VE_WMRK_POLY, 0);
solo_reg_write(solo_dev, SOLO_VE_VMRK_INIT_KEY, 0);
solo_reg_write(solo_dev, SOLO_VE_WMRK_STRL, 0);
if (solo_dev->type == SOLO_DEV_6110)
solo_reg_write(solo_dev, SOLO_VE_WMRK_ENABLE, 0);
solo_reg_write(solo_dev, SOLO_VE_ENCRYP_POLY, 0);
solo_reg_write(solo_dev, SOLO_VE_ENCRYP_INIT, 0);
solo_reg_write(solo_dev, SOLO_VE_ATTR,
SOLO_VE_LITTLE_ENDIAN |
SOLO_COMP_ATTR_FCODE(1) |
SOLO_COMP_TIME_INC(0) |
SOLO_COMP_TIME_WIDTH(15) |
SOLO_DCT_INTERVAL(solo_dev->type == SOLO_DEV_6010 ? 9 : 10));
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_reg_write(solo_dev, SOLO_VE_CH_REF_BASE(i),
(SOLO_EREF_EXT_ADDR(solo_dev) +
(i * SOLO_EREF_EXT_SIZE)) >> 16);
solo_reg_write(solo_dev, SOLO_VE_CH_REF_BASE_E(i),
(SOLO_EREF_EXT_ADDR(solo_dev) +
((i + 16) * SOLO_EREF_EXT_SIZE)) >> 16);
}
if (solo_dev->type == SOLO_DEV_6110) {
solo_reg_write(solo_dev, SOLO_VE_COMPT_MOT, 0x00040008);
} else {
for (i = 0; i < solo_dev->nr_chans; i++)
solo_reg_write(solo_dev, SOLO_VE_CH_MOT(i), 0x100);
}
}
int solo_enc_init(struct solo_dev *solo_dev)
{
int i;
solo_capture_config(solo_dev);
solo_mp4e_config(solo_dev);
solo_jpeg_config(solo_dev);
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(i), 0);
solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(i), 0);
}
return 0;
}
void solo_enc_exit(struct solo_dev *solo_dev)
{
int i;
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(i), 0);
solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(i), 0);
}
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-enc.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include "solo6x10.h"
static int multi_p2m;
module_param(multi_p2m, uint, 0644);
MODULE_PARM_DESC(multi_p2m,
"Use multiple P2M DMA channels (default: no, 6010-only)");
static int desc_mode;
module_param(desc_mode, uint, 0644);
MODULE_PARM_DESC(desc_mode,
"Allow use of descriptor mode DMA (default: no, 6010-only)");
int solo_p2m_dma(struct solo_dev *solo_dev, int wr,
void *sys_addr, u32 ext_addr, u32 size,
int repeat, u32 ext_size)
{
dma_addr_t dma_addr;
int ret;
if (WARN_ON_ONCE((unsigned long)sys_addr & 0x03))
return -EINVAL;
if (WARN_ON_ONCE(!size))
return -EINVAL;
dma_addr = dma_map_single(&solo_dev->pdev->dev, sys_addr, size,
wr ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
if (dma_mapping_error(&solo_dev->pdev->dev, dma_addr))
return -ENOMEM;
ret = solo_p2m_dma_t(solo_dev, wr, dma_addr, ext_addr, size,
repeat, ext_size);
dma_unmap_single(&solo_dev->pdev->dev, dma_addr, size,
wr ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
return ret;
}
/* Mutex must be held for p2m_id before calling this!! */
int solo_p2m_dma_desc(struct solo_dev *solo_dev,
struct solo_p2m_desc *desc, dma_addr_t desc_dma,
int desc_cnt)
{
struct solo_p2m_dev *p2m_dev;
unsigned int timeout;
unsigned int config = 0;
int ret = 0;
unsigned int p2m_id = 0;
/* Get next ID. According to Softlogic, 6110 has problems on !=0 P2M */
if (solo_dev->type != SOLO_DEV_6110 && multi_p2m)
p2m_id = atomic_inc_return(&solo_dev->p2m_count) % SOLO_NR_P2M;
p2m_dev = &solo_dev->p2m_dev[p2m_id];
if (mutex_lock_interruptible(&p2m_dev->mutex))
return -EINTR;
reinit_completion(&p2m_dev->completion);
p2m_dev->error = 0;
if (desc_cnt > 1 && solo_dev->type != SOLO_DEV_6110 && desc_mode) {
/* For 6010 with more than one desc, we can do a one-shot */
p2m_dev->desc_count = p2m_dev->desc_idx = 0;
config = solo_reg_read(solo_dev, SOLO_P2M_CONFIG(p2m_id));
solo_reg_write(solo_dev, SOLO_P2M_DES_ADR(p2m_id), desc_dma);
solo_reg_write(solo_dev, SOLO_P2M_DESC_ID(p2m_id), desc_cnt);
solo_reg_write(solo_dev, SOLO_P2M_CONFIG(p2m_id), config |
SOLO_P2M_DESC_MODE);
} else {
/* For single descriptors and 6110, we need to run each desc */
p2m_dev->desc_count = desc_cnt;
p2m_dev->desc_idx = 1;
p2m_dev->descs = desc;
solo_reg_write(solo_dev, SOLO_P2M_TAR_ADR(p2m_id),
desc[1].dma_addr);
solo_reg_write(solo_dev, SOLO_P2M_EXT_ADR(p2m_id),
desc[1].ext_addr);
solo_reg_write(solo_dev, SOLO_P2M_EXT_CFG(p2m_id),
desc[1].cfg);
solo_reg_write(solo_dev, SOLO_P2M_CONTROL(p2m_id),
desc[1].ctrl);
}
timeout = wait_for_completion_timeout(&p2m_dev->completion,
solo_dev->p2m_jiffies);
if (WARN_ON_ONCE(p2m_dev->error))
ret = -EIO;
else if (timeout == 0) {
solo_dev->p2m_timeouts++;
ret = -EAGAIN;
}
solo_reg_write(solo_dev, SOLO_P2M_CONTROL(p2m_id), 0);
/* Don't write here for the no_desc_mode case, because config is 0.
* We can't test no_desc_mode again, it might race. */
if (desc_cnt > 1 && solo_dev->type != SOLO_DEV_6110 && config)
solo_reg_write(solo_dev, SOLO_P2M_CONFIG(p2m_id), config);
mutex_unlock(&p2m_dev->mutex);
return ret;
}
void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr,
dma_addr_t dma_addr, u32 ext_addr, u32 size,
int repeat, u32 ext_size)
{
WARN_ON_ONCE(dma_addr & 0x03);
WARN_ON_ONCE(!size);
desc->cfg = SOLO_P2M_COPY_SIZE(size >> 2);
desc->ctrl = SOLO_P2M_BURST_SIZE(SOLO_P2M_BURST_256) |
(wr ? SOLO_P2M_WRITE : 0) | SOLO_P2M_TRANS_ON;
if (repeat) {
desc->cfg |= SOLO_P2M_EXT_INC(ext_size >> 2);
desc->ctrl |= SOLO_P2M_PCI_INC(size >> 2) |
SOLO_P2M_REPEAT(repeat);
}
desc->dma_addr = dma_addr;
desc->ext_addr = ext_addr;
}
int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr,
dma_addr_t dma_addr, u32 ext_addr, u32 size,
int repeat, u32 ext_size)
{
struct solo_p2m_desc desc[2];
solo_p2m_fill_desc(&desc[1], wr, dma_addr, ext_addr, size, repeat,
ext_size);
/* No need for desc_dma since we know it is a single-shot */
return solo_p2m_dma_desc(solo_dev, desc, 0, 1);
}
void solo_p2m_isr(struct solo_dev *solo_dev, int id)
{
struct solo_p2m_dev *p2m_dev = &solo_dev->p2m_dev[id];
struct solo_p2m_desc *desc;
if (p2m_dev->desc_count <= p2m_dev->desc_idx) {
complete(&p2m_dev->completion);
return;
}
/* Setup next descriptor */
p2m_dev->desc_idx++;
desc = &p2m_dev->descs[p2m_dev->desc_idx];
solo_reg_write(solo_dev, SOLO_P2M_CONTROL(id), 0);
solo_reg_write(solo_dev, SOLO_P2M_TAR_ADR(id), desc->dma_addr);
solo_reg_write(solo_dev, SOLO_P2M_EXT_ADR(id), desc->ext_addr);
solo_reg_write(solo_dev, SOLO_P2M_EXT_CFG(id), desc->cfg);
solo_reg_write(solo_dev, SOLO_P2M_CONTROL(id), desc->ctrl);
}
void solo_p2m_error_isr(struct solo_dev *solo_dev)
{
unsigned int err = solo_reg_read(solo_dev, SOLO_PCI_ERR);
struct solo_p2m_dev *p2m_dev;
int i;
if (!(err & (SOLO_PCI_ERR_P2M | SOLO_PCI_ERR_P2M_DESC)))
return;
for (i = 0; i < SOLO_NR_P2M; i++) {
p2m_dev = &solo_dev->p2m_dev[i];
p2m_dev->error = 1;
solo_reg_write(solo_dev, SOLO_P2M_CONTROL(i), 0);
complete(&p2m_dev->completion);
}
}
void solo_p2m_exit(struct solo_dev *solo_dev)
{
int i;
for (i = 0; i < SOLO_NR_P2M; i++)
solo_irq_off(solo_dev, SOLO_IRQ_P2M(i));
}
static int solo_p2m_test(struct solo_dev *solo_dev, int base, int size)
{
u32 *wr_buf;
u32 *rd_buf;
int i;
int ret = -EIO;
int order = get_order(size);
wr_buf = (u32 *)__get_free_pages(GFP_KERNEL, order);
if (wr_buf == NULL)
return -1;
rd_buf = (u32 *)__get_free_pages(GFP_KERNEL, order);
if (rd_buf == NULL) {
free_pages((unsigned long)wr_buf, order);
return -1;
}
for (i = 0; i < (size >> 3); i++)
*(wr_buf + i) = (i << 16) | (i + 1);
for (i = (size >> 3); i < (size >> 2); i++)
*(wr_buf + i) = ~((i << 16) | (i + 1));
memset(rd_buf, 0x55, size);
if (solo_p2m_dma(solo_dev, 1, wr_buf, base, size, 0, 0))
goto test_fail;
if (solo_p2m_dma(solo_dev, 0, rd_buf, base, size, 0, 0))
goto test_fail;
for (i = 0; i < (size >> 2); i++) {
if (*(wr_buf + i) != *(rd_buf + i))
goto test_fail;
}
ret = 0;
test_fail:
free_pages((unsigned long)wr_buf, order);
free_pages((unsigned long)rd_buf, order);
return ret;
}
int solo_p2m_init(struct solo_dev *solo_dev)
{
struct solo_p2m_dev *p2m_dev;
int i;
for (i = 0; i < SOLO_NR_P2M; i++) {
p2m_dev = &solo_dev->p2m_dev[i];
mutex_init(&p2m_dev->mutex);
init_completion(&p2m_dev->completion);
solo_reg_write(solo_dev, SOLO_P2M_CONTROL(i), 0);
solo_reg_write(solo_dev, SOLO_P2M_CONFIG(i),
SOLO_P2M_CSC_16BIT_565 |
SOLO_P2M_DESC_INTR_OPT |
SOLO_P2M_DMA_INTERVAL(0) |
SOLO_P2M_PCI_MASTER_MODE);
solo_irq_on(solo_dev, SOLO_IRQ_P2M(i));
}
/* Find correct SDRAM size */
for (solo_dev->sdram_size = 0, i = 2; i >= 0; i--) {
solo_reg_write(solo_dev, SOLO_DMA_CTRL,
SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
SOLO_DMA_CTRL_SDRAM_SIZE(i) |
SOLO_DMA_CTRL_SDRAM_CLK_INVERT |
SOLO_DMA_CTRL_READ_CLK_SELECT |
SOLO_DMA_CTRL_LATENCY(1));
solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config |
SOLO_SYS_CFG_RESET);
solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
switch (i) {
case 2:
if (solo_p2m_test(solo_dev, 0x07ff0000, 0x00010000) ||
solo_p2m_test(solo_dev, 0x05ff0000, 0x00010000))
continue;
break;
case 1:
if (solo_p2m_test(solo_dev, 0x03ff0000, 0x00010000))
continue;
break;
default:
if (solo_p2m_test(solo_dev, 0x01ff0000, 0x00010000))
continue;
}
solo_dev->sdram_size = (32 << 20) << i;
break;
}
if (!solo_dev->sdram_size) {
dev_err(&solo_dev->pdev->dev, "Error detecting SDRAM size\n");
return -EIO;
}
if (SOLO_SDRAM_END(solo_dev) > solo_dev->sdram_size) {
dev_err(&solo_dev->pdev->dev,
"SDRAM is not large enough (%u < %u)\n",
solo_dev->sdram_size, SOLO_SDRAM_END(solo_dev));
return -EIO;
}
return 0;
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-p2m.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-dma-contig.h>
#include "solo6x10.h"
#include "solo6x10-tw28.h"
/* Image size is two fields, SOLO_HW_BPL is one horizontal line in hardware */
#define SOLO_HW_BPL 2048
#define solo_vlines(__solo) (__solo->video_vsize * 2)
#define solo_image_size(__solo) (solo_bytesperline(__solo) * \
solo_vlines(__solo))
#define solo_bytesperline(__solo) (__solo->video_hsize * 2)
#define MIN_VID_BUFFERS 2
static inline void erase_on(struct solo_dev *solo_dev)
{
solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
solo_dev->erasing = 1;
solo_dev->frame_blank = 0;
}
static inline int erase_off(struct solo_dev *solo_dev)
{
if (!solo_dev->erasing)
return 0;
/* First time around, assert erase off */
if (!solo_dev->frame_blank)
solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
/* Keep the erasing flag on for 8 frames minimum */
if (solo_dev->frame_blank++ >= 8)
solo_dev->erasing = 0;
return 1;
}
void solo_video_in_isr(struct solo_dev *solo_dev)
{
wake_up_interruptible_all(&solo_dev->disp_thread_wait);
}
static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
int sx, int sy, int ex, int ey, int scale)
{
if (ch >= solo_dev->nr_chans)
return;
/* Here, we just keep window/channel the same */
solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
SOLO_VI_WIN_CHANNEL(ch) |
SOLO_VI_WIN_SX(sx) |
SOLO_VI_WIN_EX(ex) |
SOLO_VI_WIN_SCALE(scale));
solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
SOLO_VI_WIN_SY(sy) |
SOLO_VI_WIN_EY(ey));
}
static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
{
u8 ch = idx * 4;
if (ch >= solo_dev->nr_chans)
return -EINVAL;
if (!on) {
u8 i;
for (i = ch; i < ch + 4; i++)
solo_win_setup(solo_dev, i, solo_dev->video_hsize,
solo_vlines(solo_dev),
solo_dev->video_hsize,
solo_vlines(solo_dev), 0);
return 0;
}
/* Row 1 */
solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
solo_vlines(solo_dev) / 2, 3);
solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
/* Row 2 */
solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
solo_vlines(solo_dev), 3);
return 0;
}
static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
{
int sy, ysize, hsize, i;
if (!on) {
for (i = 0; i < 16; i++)
solo_win_setup(solo_dev, i, solo_dev->video_hsize,
solo_vlines(solo_dev),
solo_dev->video_hsize,
solo_vlines(solo_dev), 0);
return 0;
}
ysize = solo_vlines(solo_dev) / 4;
hsize = solo_dev->video_hsize / 4;
for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
sy + ysize, 5);
solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
hsize * 2, sy + ysize, 5);
solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
hsize * 3, sy + ysize, 5);
solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
solo_dev->video_hsize, sy + ysize, 5);
}
return 0;
}
static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
{
u8 ext_ch;
if (ch < solo_dev->nr_chans) {
solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
on ? 0 : solo_vlines(solo_dev),
solo_dev->video_hsize, solo_vlines(solo_dev),
on ? 1 : 0);
return 0;
}
if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
return -EINVAL;
ext_ch = ch - solo_dev->nr_chans;
/* 4up's first */
if (ext_ch < 4)
return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
/* Remaining case is 16up for 16-port */
return solo_v4l2_ch_ext_16up(solo_dev, on);
}
static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
{
if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
return -EINVAL;
erase_on(solo_dev);
solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
solo_v4l2_ch(solo_dev, ch, 1);
solo_dev->cur_disp_ch = ch;
return 0;
}
static void solo_fillbuf(struct solo_dev *solo_dev,
struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
dma_addr_t addr;
unsigned int fdma_addr;
int error = -1;
int i;
addr = vb2_dma_contig_plane_dma_addr(vb, 0);
if (!addr)
goto finish_buf;
if (erase_off(solo_dev)) {
void *p = vb2_plane_vaddr(vb, 0);
int image_size = solo_image_size(solo_dev);
for (i = 0; i < image_size; i += 2) {
((u8 *)p)[i] = 0x80;
((u8 *)p)[i + 1] = 0x00;
}
error = 0;
} else {
fdma_addr = SOLO_DISP_EXT_ADDR + (solo_dev->old_write *
(SOLO_HW_BPL * solo_vlines(solo_dev)));
error = solo_p2m_dma_t(solo_dev, 0, addr, fdma_addr,
solo_bytesperline(solo_dev),
solo_vlines(solo_dev), SOLO_HW_BPL);
}
finish_buf:
if (!error) {
vb2_set_plane_payload(vb, 0,
solo_vlines(solo_dev) * solo_bytesperline(solo_dev));
vbuf->sequence = solo_dev->sequence++;
vb->timestamp = ktime_get_ns();
}
vb2_buffer_done(vb, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
}
static void solo_thread_try(struct solo_dev *solo_dev)
{
struct solo_vb2_buf *vb;
/* Only "break" from this loop if slock is held, otherwise
* just return. */
for (;;) {
unsigned int cur_write;
cur_write = SOLO_VI_STATUS0_PAGE(
solo_reg_read(solo_dev, SOLO_VI_STATUS0));
if (cur_write == solo_dev->old_write)
return;
spin_lock(&solo_dev->slock);
if (list_empty(&solo_dev->vidq_active))
break;
vb = list_first_entry(&solo_dev->vidq_active, struct solo_vb2_buf,
list);
solo_dev->old_write = cur_write;
list_del(&vb->list);
spin_unlock(&solo_dev->slock);
solo_fillbuf(solo_dev, &vb->vb.vb2_buf);
}
assert_spin_locked(&solo_dev->slock);
spin_unlock(&solo_dev->slock);
}
static int solo_thread(void *data)
{
struct solo_dev *solo_dev = data;
DECLARE_WAITQUEUE(wait, current);
set_freezable();
add_wait_queue(&solo_dev->disp_thread_wait, &wait);
for (;;) {
long timeout = schedule_timeout_interruptible(HZ);
if (timeout == -ERESTARTSYS || kthread_should_stop())
break;
solo_thread_try(solo_dev);
try_to_freeze();
}
remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
return 0;
}
static int solo_start_thread(struct solo_dev *solo_dev)
{
int ret = 0;
solo_dev->kthread = kthread_run(solo_thread, solo_dev, SOLO6X10_NAME "_disp");
if (IS_ERR(solo_dev->kthread)) {
ret = PTR_ERR(solo_dev->kthread);
solo_dev->kthread = NULL;
return ret;
}
solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
return ret;
}
static void solo_stop_thread(struct solo_dev *solo_dev)
{
if (!solo_dev->kthread)
return;
solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
kthread_stop(solo_dev->kthread);
solo_dev->kthread = NULL;
}
static int solo_queue_setup(struct vb2_queue *q,
unsigned int *num_buffers, unsigned int *num_planes,
unsigned int sizes[], struct device *alloc_devs[])
{
struct solo_dev *solo_dev = vb2_get_drv_priv(q);
sizes[0] = solo_image_size(solo_dev);
*num_planes = 1;
if (*num_buffers < MIN_VID_BUFFERS)
*num_buffers = MIN_VID_BUFFERS;
return 0;
}
static int solo_start_streaming(struct vb2_queue *q, unsigned int count)
{
struct solo_dev *solo_dev = vb2_get_drv_priv(q);
solo_dev->sequence = 0;
return solo_start_thread(solo_dev);
}
static void solo_stop_streaming(struct vb2_queue *q)
{
struct solo_dev *solo_dev = vb2_get_drv_priv(q);
solo_stop_thread(solo_dev);
spin_lock(&solo_dev->slock);
while (!list_empty(&solo_dev->vidq_active)) {
struct solo_vb2_buf *buf = list_entry(
solo_dev->vidq_active.next,
struct solo_vb2_buf, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
spin_unlock(&solo_dev->slock);
INIT_LIST_HEAD(&solo_dev->vidq_active);
}
static void solo_buf_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
struct solo_vb2_buf *solo_vb =
container_of(vbuf, struct solo_vb2_buf, vb);
spin_lock(&solo_dev->slock);
list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
spin_unlock(&solo_dev->slock);
wake_up_interruptible(&solo_dev->disp_thread_wait);
}
static const struct vb2_ops solo_video_qops = {
.queue_setup = solo_queue_setup,
.buf_queue = solo_buf_queue,
.start_streaming = solo_start_streaming,
.stop_streaming = solo_stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
static int solo_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strscpy(cap->driver, SOLO6X10_NAME, sizeof(cap->driver));
strscpy(cap->card, "Softlogic 6x10", sizeof(cap->card));
return 0;
}
static int solo_enum_ext_input(struct solo_dev *solo_dev,
struct v4l2_input *input)
{
int ext = input->index - solo_dev->nr_chans;
unsigned int nup, first;
if (ext >= solo_dev->nr_ext)
return -EINVAL;
nup = (ext == 4) ? 16 : 4;
first = (ext & 3) << 2; /* first channel in the n-up */
snprintf(input->name, sizeof(input->name),
"Multi %d-up (cameras %d-%d)",
nup, first + 1, first + nup);
/* Possible outputs:
* Multi 4-up (cameras 1-4)
* Multi 4-up (cameras 5-8)
* Multi 4-up (cameras 9-12)
* Multi 4-up (cameras 13-16)
* Multi 16-up (cameras 1-16)
*/
return 0;
}
static int solo_enum_input(struct file *file, void *priv,
struct v4l2_input *input)
{
struct solo_dev *solo_dev = video_drvdata(file);
if (input->index >= solo_dev->nr_chans) {
int ret = solo_enum_ext_input(solo_dev, input);
if (ret < 0)
return ret;
} else {
snprintf(input->name, sizeof(input->name), "Camera %d",
input->index + 1);
/* We can only check this for normal inputs */
if (!tw28_get_video_status(solo_dev, input->index))
input->status = V4L2_IN_ST_NO_SIGNAL;
}
input->type = V4L2_INPUT_TYPE_CAMERA;
input->std = solo_dev->vfd->tvnorms;
return 0;
}
static int solo_set_input(struct file *file, void *priv, unsigned int index)
{
struct solo_dev *solo_dev = video_drvdata(file);
int ret = solo_v4l2_set_ch(solo_dev, index);
if (!ret) {
while (erase_off(solo_dev))
/* Do nothing */;
}
return ret;
}
static int solo_get_input(struct file *file, void *priv, unsigned int *index)
{
struct solo_dev *solo_dev = video_drvdata(file);
*index = solo_dev->cur_disp_ch;
return 0;
}
static int solo_enum_fmt_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index)
return -EINVAL;
f->pixelformat = V4L2_PIX_FMT_UYVY;
return 0;
}
static int solo_try_fmt_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct solo_dev *solo_dev = video_drvdata(file);
struct v4l2_pix_format *pix = &f->fmt.pix;
int image_size = solo_image_size(solo_dev);
if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
return -EINVAL;
pix->width = solo_dev->video_hsize;
pix->height = solo_vlines(solo_dev);
pix->sizeimage = image_size;
pix->field = V4L2_FIELD_INTERLACED;
pix->pixelformat = V4L2_PIX_FMT_UYVY;
pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
static int solo_set_fmt_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct solo_dev *solo_dev = video_drvdata(file);
if (vb2_is_busy(&solo_dev->vidq))
return -EBUSY;
/* For right now, if it doesn't match our running config,
* then fail */
return solo_try_fmt_cap(file, priv, f);
}
static int solo_get_fmt_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct solo_dev *solo_dev = video_drvdata(file);
struct v4l2_pix_format *pix = &f->fmt.pix;
pix->width = solo_dev->video_hsize;
pix->height = solo_vlines(solo_dev);
pix->pixelformat = V4L2_PIX_FMT_UYVY;
pix->field = V4L2_FIELD_INTERLACED;
pix->sizeimage = solo_image_size(solo_dev);
pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
pix->bytesperline = solo_bytesperline(solo_dev);
return 0;
}
static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
{
struct solo_dev *solo_dev = video_drvdata(file);
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
*i = V4L2_STD_NTSC_M;
else
*i = V4L2_STD_PAL;
return 0;
}
int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz)
{
int i;
/* Make sure all video nodes are idle */
if (vb2_is_busy(&solo_dev->vidq))
return -EBUSY;
for (i = 0; i < solo_dev->nr_chans; i++)
if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
return -EBUSY;
solo_dev->video_type = is_50hz ? SOLO_VO_FMT_TYPE_PAL :
SOLO_VO_FMT_TYPE_NTSC;
/* Reconfigure for the new standard */
solo_disp_init(solo_dev);
solo_enc_init(solo_dev);
solo_tw28_init(solo_dev);
for (i = 0; i < solo_dev->nr_chans; i++)
solo_update_mode(solo_dev->v4l2_enc[i]);
return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
}
static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
{
struct solo_dev *solo_dev = video_drvdata(file);
return solo_set_video_type(solo_dev, std & V4L2_STD_625_50);
}
static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct solo_dev *solo_dev =
container_of(ctrl->handler, struct solo_dev, disp_hdl);
switch (ctrl->id) {
case V4L2_CID_MOTION_TRACE:
if (ctrl->val) {
solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
SOLO_VI_MOTION_Y_ADD |
SOLO_VI_MOTION_Y_VALUE(0x20) |
SOLO_VI_MOTION_CB_VALUE(0x10) |
SOLO_VI_MOTION_CR_VALUE(0x10));
solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
SOLO_VI_MOTION_CR_ADD |
SOLO_VI_MOTION_Y_VALUE(0x10) |
SOLO_VI_MOTION_CB_VALUE(0x80) |
SOLO_VI_MOTION_CR_VALUE(0x10));
} else {
solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
}
return 0;
default:
break;
}
return -EINVAL;
}
static const struct v4l2_file_operations solo_v4l2_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = vb2_fop_release,
.read = vb2_fop_read,
.poll = vb2_fop_poll,
.mmap = vb2_fop_mmap,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
.vidioc_querycap = solo_querycap,
.vidioc_s_std = solo_s_std,
.vidioc_g_std = solo_g_std,
/* Input callbacks */
.vidioc_enum_input = solo_enum_input,
.vidioc_s_input = solo_set_input,
.vidioc_g_input = solo_get_input,
/* Video capture format callbacks */
.vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap,
.vidioc_try_fmt_vid_cap = solo_try_fmt_cap,
.vidioc_s_fmt_vid_cap = solo_set_fmt_cap,
.vidioc_g_fmt_vid_cap = solo_get_fmt_cap,
/* Streaming I/O */
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
/* Logging and events */
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct video_device solo_v4l2_template = {
.name = SOLO6X10_NAME,
.fops = &solo_v4l2_fops,
.ioctl_ops = &solo_v4l2_ioctl_ops,
.minor = -1,
.release = video_device_release,
.tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING,
};
static const struct v4l2_ctrl_ops solo_ctrl_ops = {
.s_ctrl = solo_s_ctrl,
};
static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
.ops = &solo_ctrl_ops,
.id = V4L2_CID_MOTION_TRACE,
.name = "Motion Detection Trace",
.type = V4L2_CTRL_TYPE_BOOLEAN,
.max = 1,
.step = 1,
};
int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
{
int ret;
int i;
init_waitqueue_head(&solo_dev->disp_thread_wait);
spin_lock_init(&solo_dev->slock);
mutex_init(&solo_dev->lock);
INIT_LIST_HEAD(&solo_dev->vidq_active);
solo_dev->vfd = video_device_alloc();
if (!solo_dev->vfd)
return -ENOMEM;
*solo_dev->vfd = solo_v4l2_template;
solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
solo_dev->vfd->queue = &solo_dev->vidq;
solo_dev->vfd->lock = &solo_dev->lock;
v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
if (solo_dev->disp_hdl.error) {
ret = solo_dev->disp_hdl.error;
goto fail;
}
solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
video_set_drvdata(solo_dev->vfd, solo_dev);
solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
solo_dev->vidq.ops = &solo_video_qops;
solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
solo_dev->vidq.drv_priv = solo_dev;
solo_dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
solo_dev->vidq.lock = &solo_dev->lock;
solo_dev->vidq.dev = &solo_dev->pdev->dev;
ret = vb2_queue_init(&solo_dev->vidq);
if (ret < 0)
goto fail;
/* Cycle all the channels and clear */
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_v4l2_set_ch(solo_dev, i);
while (erase_off(solo_dev))
/* Do nothing */;
}
/* Set the default display channel */
solo_v4l2_set_ch(solo_dev, 0);
while (erase_off(solo_dev))
/* Do nothing */;
ret = video_register_device(solo_dev->vfd, VFL_TYPE_VIDEO, nr);
if (ret < 0)
goto fail;
snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
SOLO6X10_NAME, solo_dev->vfd->num);
dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with %d inputs (%d extended)\n",
solo_dev->vfd->num,
solo_dev->nr_chans, solo_dev->nr_ext);
return 0;
fail:
video_device_release(solo_dev->vfd);
v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
solo_dev->vfd = NULL;
return ret;
}
void solo_v4l2_exit(struct solo_dev *solo_dev)
{
if (solo_dev->vfd == NULL)
return;
video_unregister_device(solo_dev->vfd);
v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
solo_dev->vfd = NULL;
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-v4l2.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-dma-sg.h>
#include "solo6x10.h"
#include "solo6x10-tw28.h"
#include "solo6x10-jpeg.h"
#define MIN_VID_BUFFERS 2
#define FRAME_BUF_SIZE (400 * 1024)
#define MP4_QS 16
#define DMA_ALIGN 4096
/* 6010 M4V */
static u8 vop_6010_ntsc_d1[] = {
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
0x1f, 0x4c, 0x58, 0x10, 0xf0, 0x71, 0x18, 0x3f,
};
static u8 vop_6010_ntsc_cif[] = {
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
0x1f, 0x4c, 0x2c, 0x10, 0x78, 0x51, 0x18, 0x3f,
};
static u8 vop_6010_pal_d1[] = {
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
0x1f, 0x4c, 0x58, 0x11, 0x20, 0x71, 0x18, 0x3f,
};
static u8 vop_6010_pal_cif[] = {
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
0x1f, 0x4c, 0x2c, 0x10, 0x90, 0x51, 0x18, 0x3f,
};
/* 6110 h.264 */
static u8 vop_6110_ntsc_d1[] = {
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
0x9a, 0x74, 0x05, 0x81, 0xec, 0x80, 0x00, 0x00,
0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
};
static u8 vop_6110_ntsc_cif[] = {
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
0x9a, 0x74, 0x0b, 0x0f, 0xc8, 0x00, 0x00, 0x00,
0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
};
static u8 vop_6110_pal_d1[] = {
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
0x9a, 0x74, 0x05, 0x80, 0x93, 0x20, 0x00, 0x00,
0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
};
static u8 vop_6110_pal_cif[] = {
0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
0x9a, 0x74, 0x0b, 0x04, 0xb2, 0x00, 0x00, 0x00,
0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
};
typedef __le32 vop_header[16];
struct solo_enc_buf {
enum solo_enc_types type;
const vop_header *vh;
int motion;
};
static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
return (solo_dev->motion_mask >> solo_enc->ch) & 1;
}
static int solo_motion_detected(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
unsigned long flags;
u32 ch_mask = 1 << solo_enc->ch;
int ret = 0;
spin_lock_irqsave(&solo_enc->motion_lock, flags);
if (solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS) & ch_mask) {
solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, ch_mask);
ret = 1;
}
spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
return ret;
}
static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
u32 mask = 1 << solo_enc->ch;
unsigned long flags;
spin_lock_irqsave(&solo_enc->motion_lock, flags);
if (on)
solo_dev->motion_mask |= mask;
else
solo_dev->motion_mask &= ~mask;
solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, mask);
solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
(SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
}
void solo_update_mode(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
int vop_len;
u8 *vop;
solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
if (solo_enc->mode == SOLO_ENC_MODE_CIF) {
solo_enc->width = solo_dev->video_hsize >> 1;
solo_enc->height = solo_dev->video_vsize;
if (solo_dev->type == SOLO_DEV_6110) {
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
vop = vop_6110_ntsc_cif;
vop_len = sizeof(vop_6110_ntsc_cif);
} else {
vop = vop_6110_pal_cif;
vop_len = sizeof(vop_6110_pal_cif);
}
} else {
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
vop = vop_6010_ntsc_cif;
vop_len = sizeof(vop_6010_ntsc_cif);
} else {
vop = vop_6010_pal_cif;
vop_len = sizeof(vop_6010_pal_cif);
}
}
} else {
solo_enc->width = solo_dev->video_hsize;
solo_enc->height = solo_dev->video_vsize << 1;
solo_enc->bw_weight <<= 2;
if (solo_dev->type == SOLO_DEV_6110) {
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
vop = vop_6110_ntsc_d1;
vop_len = sizeof(vop_6110_ntsc_d1);
} else {
vop = vop_6110_pal_d1;
vop_len = sizeof(vop_6110_pal_d1);
}
} else {
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
vop = vop_6010_ntsc_d1;
vop_len = sizeof(vop_6010_ntsc_d1);
} else {
vop = vop_6010_pal_d1;
vop_len = sizeof(vop_6010_pal_d1);
}
}
}
memcpy(solo_enc->vop, vop, vop_len);
/* Some fixups for 6010/M4V */
if (solo_dev->type == SOLO_DEV_6010) {
u16 fps = solo_dev->fps * 1000;
u16 interval = solo_enc->interval * 1000;
vop = solo_enc->vop;
/* Frame rate and interval */
vop[22] = fps >> 4;
vop[23] = ((fps << 4) & 0xf0) | 0x0c
| ((interval >> 13) & 0x3);
vop[24] = (interval >> 5) & 0xff;
vop[25] = ((interval << 3) & 0xf8) | 0x04;
}
solo_enc->vop_len = vop_len;
/* Now handle the jpeg header */
vop = solo_enc->jpeg_header;
vop[SOF0_START + 5] = 0xff & (solo_enc->height >> 8);
vop[SOF0_START + 6] = 0xff & solo_enc->height;
vop[SOF0_START + 7] = 0xff & (solo_enc->width >> 8);
vop[SOF0_START + 8] = 0xff & solo_enc->width;
memcpy(vop + DQT_START,
jpeg_dqt[solo_g_jpeg_qp(solo_dev, solo_enc->ch)], DQT_LEN);
}
static int solo_enc_on(struct solo_enc_dev *solo_enc)
{
u8 ch = solo_enc->ch;
struct solo_dev *solo_dev = solo_enc->solo_dev;
u8 interval;
solo_update_mode(solo_enc);
/* Make sure to do a bandwidth check */
if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
return -EBUSY;
solo_enc->sequence = 0;
solo_dev->enc_bw_remain -= solo_enc->bw_weight;
if (solo_enc->type == SOLO_ENC_TYPE_EXT)
solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
/* Disable all encoding for this channel */
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
/* Common for both std and ext encoding */
solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
solo_enc->interlaced ? 1 : 0);
if (solo_enc->interlaced)
interval = solo_enc->interval - 1;
else
interval = solo_enc->interval;
/* Standard encoding only */
solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
/* Extended encoding only */
solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
/* Enables the standard encoder */
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
return 0;
}
static void solo_enc_off(struct solo_enc_dev *solo_enc)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
solo_dev->enc_bw_remain += solo_enc->bw_weight;
solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
}
static int enc_get_mpeg_dma(struct solo_dev *solo_dev, dma_addr_t dma,
unsigned int off, unsigned int size)
{
int ret;
if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
return -EINVAL;
/* Single shot */
if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
return solo_p2m_dma_t(solo_dev, 0, dma,
SOLO_MP4E_EXT_ADDR(solo_dev) + off, size,
0, 0);
}
/* Buffer wrap */
ret = solo_p2m_dma_t(solo_dev, 0, dma,
SOLO_MP4E_EXT_ADDR(solo_dev) + off,
SOLO_MP4E_EXT_SIZE(solo_dev) - off, 0, 0);
if (!ret) {
ret = solo_p2m_dma_t(solo_dev, 0,
dma + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
SOLO_MP4E_EXT_ADDR(solo_dev),
size + off - SOLO_MP4E_EXT_SIZE(solo_dev), 0, 0);
}
return ret;
}
/* Build a descriptor queue out of an SG list and send it to the P2M for
* processing. */
static int solo_send_desc(struct solo_enc_dev *solo_enc, int skip,
struct sg_table *vbuf, int off, int size,
unsigned int base, unsigned int base_size)
{
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct scatterlist *sg;
int i;
int ret;
if (WARN_ON_ONCE(size > FRAME_BUF_SIZE))
return -EINVAL;
solo_enc->desc_count = 1;
for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
struct solo_p2m_desc *desc;
dma_addr_t dma;
int len;
int left = base_size - off;
desc = &solo_enc->desc_items[solo_enc->desc_count++];
dma = sg_dma_address(sg);
len = sg_dma_len(sg);
/* We assume this is smaller than the scatter size */
BUG_ON(skip >= len);
if (skip) {
len -= skip;
dma += skip;
size -= skip;
skip = 0;
}
len = min(len, size);
if (len <= left) {
/* Single descriptor */
solo_p2m_fill_desc(desc, 0, dma, base + off,
len, 0, 0);
} else {
/* Buffer wrap */
/* XXX: Do these as separate DMA requests, to avoid
timeout errors triggered by awkwardly sized
descriptors. See
<https://github.com/bluecherrydvr/solo6x10/issues/8>
*/
ret = solo_p2m_dma_t(solo_dev, 0, dma, base + off,
left, 0, 0);
if (ret)
return ret;
ret = solo_p2m_dma_t(solo_dev, 0, dma + left, base,
len - left, 0, 0);
if (ret)
return ret;
solo_enc->desc_count--;
}
size -= len;
if (size <= 0)
break;
off += len;
if (off >= base_size)
off -= base_size;
/* Because we may use two descriptors per loop */
if (solo_enc->desc_count >= (solo_enc->desc_nelts - 1)) {
ret = solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
solo_enc->desc_dma,
solo_enc->desc_count - 1);
if (ret)
return ret;
solo_enc->desc_count = 1;
}
}
if (solo_enc->desc_count <= 1)
return 0;
return solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
solo_enc->desc_dma, solo_enc->desc_count - 1);
}
/* Extract values from VOP header - VE_STATUSxx */
static inline __always_unused int vop_interlaced(const vop_header *vh)
{
return (__le32_to_cpu((*vh)[0]) >> 30) & 1;
}
static inline __always_unused u8 vop_channel(const vop_header *vh)
{
return (__le32_to_cpu((*vh)[0]) >> 24) & 0x1F;
}
static inline u8 vop_type(const vop_header *vh)
{
return (__le32_to_cpu((*vh)[0]) >> 22) & 3;
}
static inline u32 vop_mpeg_size(const vop_header *vh)
{
return __le32_to_cpu((*vh)[0]) & 0xFFFFF;
}
static inline u8 __always_unused vop_hsize(const vop_header *vh)
{
return (__le32_to_cpu((*vh)[1]) >> 8) & 0xFF;
}
static inline u8 __always_unused vop_vsize(const vop_header *vh)
{
return __le32_to_cpu((*vh)[1]) & 0xFF;
}
static inline u32 vop_mpeg_offset(const vop_header *vh)
{
return __le32_to_cpu((*vh)[2]);
}
static inline u32 vop_jpeg_offset(const vop_header *vh)
{
return __le32_to_cpu((*vh)[3]);
}
static inline u32 vop_jpeg_size(const vop_header *vh)
{
return __le32_to_cpu((*vh)[4]) & 0xFFFFF;
}
static inline u32 __always_unused vop_sec(const vop_header *vh)
{
return __le32_to_cpu((*vh)[5]);
}
static inline __always_unused u32 vop_usec(const vop_header *vh)
{
return __le32_to_cpu((*vh)[6]);
}
static int solo_fill_jpeg(struct solo_enc_dev *solo_enc,
struct vb2_buffer *vb, const vop_header *vh)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
int frame_size;
vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
if (vb2_plane_size(vb, 0) < vop_jpeg_size(vh) + solo_enc->jpeg_len)
return -EIO;
frame_size = ALIGN(vop_jpeg_size(vh) + solo_enc->jpeg_len, DMA_ALIGN);
vb2_set_plane_payload(vb, 0, vop_jpeg_size(vh) + solo_enc->jpeg_len);
return solo_send_desc(solo_enc, solo_enc->jpeg_len, sgt,
vop_jpeg_offset(vh) - SOLO_JPEG_EXT_ADDR(solo_dev),
frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
SOLO_JPEG_EXT_SIZE(solo_dev));
}
static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
struct vb2_buffer *vb, const vop_header *vh)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
int frame_off, frame_size;
int skip = 0;
if (vb2_plane_size(vb, 0) < vop_mpeg_size(vh))
return -EIO;
/* If this is a key frame, add extra header */
vbuf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME |
V4L2_BUF_FLAG_BFRAME);
if (!vop_type(vh)) {
skip = solo_enc->vop_len;
vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh) +
solo_enc->vop_len);
} else {
vbuf->flags |= V4L2_BUF_FLAG_PFRAME;
vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh));
}
/* Now get the actual mpeg payload */
frame_off = (vop_mpeg_offset(vh) - SOLO_MP4E_EXT_ADDR(solo_dev) +
sizeof(*vh)) % SOLO_MP4E_EXT_SIZE(solo_dev);
frame_size = ALIGN(vop_mpeg_size(vh) + skip, DMA_ALIGN);
return solo_send_desc(solo_enc, skip, sgt, frame_off, frame_size,
SOLO_MP4E_EXT_ADDR(solo_dev),
SOLO_MP4E_EXT_SIZE(solo_dev));
}
static int solo_enc_fillbuf(struct solo_enc_dev *solo_enc,
struct vb2_buffer *vb, struct solo_enc_buf *enc_buf)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
const vop_header *vh = enc_buf->vh;
int ret;
switch (solo_enc->fmt) {
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H264:
ret = solo_fill_mpeg(solo_enc, vb, vh);
break;
default: /* V4L2_PIX_FMT_MJPEG */
ret = solo_fill_jpeg(solo_enc, vb, vh);
break;
}
if (!ret) {
vbuf->sequence = solo_enc->sequence++;
vb->timestamp = ktime_get_ns();
/* Check for motion flags */
if (solo_is_motion_on(solo_enc) && enc_buf->motion) {
struct v4l2_event ev = {
.type = V4L2_EVENT_MOTION_DET,
.u.motion_det = {
.flags
= V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ,
.frame_sequence = vbuf->sequence,
.region_mask = enc_buf->motion ? 1 : 0,
},
};
v4l2_event_queue(solo_enc->vfd, &ev);
}
}
vb2_buffer_done(vb, ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
return ret;
}
static void solo_enc_handle_one(struct solo_enc_dev *solo_enc,
struct solo_enc_buf *enc_buf)
{
struct solo_vb2_buf *vb;
unsigned long flags;
mutex_lock(&solo_enc->lock);
if (solo_enc->type != enc_buf->type)
goto unlock;
spin_lock_irqsave(&solo_enc->av_lock, flags);
if (list_empty(&solo_enc->vidq_active)) {
spin_unlock_irqrestore(&solo_enc->av_lock, flags);
goto unlock;
}
vb = list_first_entry(&solo_enc->vidq_active, struct solo_vb2_buf,
list);
list_del(&vb->list);
spin_unlock_irqrestore(&solo_enc->av_lock, flags);
solo_enc_fillbuf(solo_enc, &vb->vb.vb2_buf, enc_buf);
unlock:
mutex_unlock(&solo_enc->lock);
}
void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
{
wake_up_interruptible_all(&solo_dev->ring_thread_wait);
}
static void solo_handle_ring(struct solo_dev *solo_dev)
{
for (;;) {
struct solo_enc_dev *solo_enc;
struct solo_enc_buf enc_buf;
u32 mpeg_current, off;
u8 ch;
u8 cur_q;
/* Check if the hardware has any new ones in the queue */
cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
if (cur_q == solo_dev->enc_idx)
break;
mpeg_current = solo_reg_read(solo_dev,
SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
ch = (mpeg_current >> 24) & 0x1f;
off = mpeg_current & 0x00ffffff;
if (ch >= SOLO_MAX_CHANNELS) {
ch -= SOLO_MAX_CHANNELS;
enc_buf.type = SOLO_ENC_TYPE_EXT;
} else
enc_buf.type = SOLO_ENC_TYPE_STD;
solo_enc = solo_dev->v4l2_enc[ch];
if (solo_enc == NULL) {
dev_err(&solo_dev->pdev->dev,
"Got spurious packet for channel %d\n", ch);
continue;
}
/* FAIL... */
if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
sizeof(vop_header)))
continue;
enc_buf.vh = solo_dev->vh_buf;
/* Sanity check */
if (vop_mpeg_offset(enc_buf.vh) !=
SOLO_MP4E_EXT_ADDR(solo_dev) + off)
continue;
if (solo_motion_detected(solo_enc))
enc_buf.motion = 1;
else
enc_buf.motion = 0;
solo_enc_handle_one(solo_enc, &enc_buf);
}
}
static int solo_ring_thread(void *data)
{
struct solo_dev *solo_dev = data;
DECLARE_WAITQUEUE(wait, current);
set_freezable();
add_wait_queue(&solo_dev->ring_thread_wait, &wait);
for (;;) {
long timeout = schedule_timeout_interruptible(HZ);
if (timeout == -ERESTARTSYS || kthread_should_stop())
break;
solo_handle_ring(solo_dev);
try_to_freeze();
}
remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
return 0;
}
static int solo_enc_queue_setup(struct vb2_queue *q,
unsigned int *num_buffers,
unsigned int *num_planes, unsigned int sizes[],
struct device *alloc_devs[])
{
sizes[0] = FRAME_BUF_SIZE;
*num_planes = 1;
if (*num_buffers < MIN_VID_BUFFERS)
*num_buffers = MIN_VID_BUFFERS;
return 0;
}
static void solo_enc_buf_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct vb2_queue *vq = vb->vb2_queue;
struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
struct solo_vb2_buf *solo_vb =
container_of(vbuf, struct solo_vb2_buf, vb);
spin_lock(&solo_enc->av_lock);
list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
spin_unlock(&solo_enc->av_lock);
}
static int solo_ring_start(struct solo_dev *solo_dev)
{
solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
SOLO6X10_NAME "_ring");
if (IS_ERR(solo_dev->ring_thread)) {
int err = PTR_ERR(solo_dev->ring_thread);
solo_dev->ring_thread = NULL;
return err;
}
solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
return 0;
}
static void solo_ring_stop(struct solo_dev *solo_dev)
{
if (solo_dev->ring_thread) {
kthread_stop(solo_dev->ring_thread);
solo_dev->ring_thread = NULL;
}
solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
}
static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
{
struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
return solo_enc_on(solo_enc);
}
static void solo_enc_stop_streaming(struct vb2_queue *q)
{
struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
unsigned long flags;
spin_lock_irqsave(&solo_enc->av_lock, flags);
solo_enc_off(solo_enc);
while (!list_empty(&solo_enc->vidq_active)) {
struct solo_vb2_buf *buf = list_entry(
solo_enc->vidq_active.next,
struct solo_vb2_buf, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
spin_unlock_irqrestore(&solo_enc->av_lock, flags);
}
static void solo_enc_buf_finish(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vb->vb2_queue);
struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
switch (solo_enc->fmt) {
case V4L2_PIX_FMT_MPEG4:
case V4L2_PIX_FMT_H264:
if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
sg_copy_from_buffer(sgt->sgl, sgt->nents,
solo_enc->vop, solo_enc->vop_len);
break;
default: /* V4L2_PIX_FMT_MJPEG */
sg_copy_from_buffer(sgt->sgl, sgt->nents,
solo_enc->jpeg_header, solo_enc->jpeg_len);
break;
}
}
static const struct vb2_ops solo_enc_video_qops = {
.queue_setup = solo_enc_queue_setup,
.buf_queue = solo_enc_buf_queue,
.buf_finish = solo_enc_buf_finish,
.start_streaming = solo_enc_start_streaming,
.stop_streaming = solo_enc_stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
static int solo_enc_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
strscpy(cap->driver, SOLO6X10_NAME, sizeof(cap->driver));
snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
solo_enc->ch);
return 0;
}
static int solo_enc_enum_input(struct file *file, void *priv,
struct v4l2_input *input)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_dev *solo_dev = solo_enc->solo_dev;
if (input->index)
return -EINVAL;
snprintf(input->name, sizeof(input->name), "Encoder %d",
solo_enc->ch + 1);
input->type = V4L2_INPUT_TYPE_CAMERA;
input->std = solo_enc->vfd->tvnorms;
if (!tw28_get_video_status(solo_dev, solo_enc->ch))
input->status = V4L2_IN_ST_NO_SIGNAL;
return 0;
}
static int solo_enc_set_input(struct file *file, void *priv,
unsigned int index)
{
if (index)
return -EINVAL;
return 0;
}
static int solo_enc_get_input(struct file *file, void *priv,
unsigned int *index)
{
*index = 0;
return 0;
}
static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
int dev_type = solo_enc->solo_dev->type;
switch (f->index) {
case 0:
switch (dev_type) {
case SOLO_DEV_6010:
f->pixelformat = V4L2_PIX_FMT_MPEG4;
break;
case SOLO_DEV_6110:
f->pixelformat = V4L2_PIX_FMT_H264;
break;
}
break;
case 1:
f->pixelformat = V4L2_PIX_FMT_MJPEG;
break;
default:
return -EINVAL;
}
return 0;
}
static inline int solo_valid_pixfmt(u32 pixfmt, int dev_type)
{
return (pixfmt == V4L2_PIX_FMT_H264 && dev_type == SOLO_DEV_6110)
|| (pixfmt == V4L2_PIX_FMT_MPEG4 && dev_type == SOLO_DEV_6010)
|| pixfmt == V4L2_PIX_FMT_MJPEG ? 0 : -EINVAL;
}
static int solo_enc_try_fmt_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct v4l2_pix_format *pix = &f->fmt.pix;
if (solo_valid_pixfmt(pix->pixelformat, solo_dev->type))
return -EINVAL;
if (pix->width < solo_dev->video_hsize ||
pix->height < solo_dev->video_vsize << 1) {
/* Default to CIF 1/2 size */
pix->width = solo_dev->video_hsize >> 1;
pix->height = solo_dev->video_vsize;
} else {
/* Full frame */
pix->width = solo_dev->video_hsize;
pix->height = solo_dev->video_vsize << 1;
}
switch (pix->field) {
case V4L2_FIELD_NONE:
case V4L2_FIELD_INTERLACED:
break;
case V4L2_FIELD_ANY:
default:
pix->field = V4L2_FIELD_INTERLACED;
break;
}
/* Just set these */
pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
pix->sizeimage = FRAME_BUF_SIZE;
pix->bytesperline = 0;
return 0;
}
static int solo_enc_set_fmt_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_dev *solo_dev = solo_enc->solo_dev;
struct v4l2_pix_format *pix = &f->fmt.pix;
int ret;
if (vb2_is_busy(&solo_enc->vidq))
return -EBUSY;
ret = solo_enc_try_fmt_cap(file, priv, f);
if (ret)
return ret;
if (pix->width == solo_dev->video_hsize)
solo_enc->mode = SOLO_ENC_MODE_D1;
else
solo_enc->mode = SOLO_ENC_MODE_CIF;
/* This does not change the encoder at all */
solo_enc->fmt = pix->pixelformat;
/*
* More information is needed about these 'extended' types. As far
* as I can tell these are basically additional video streams with
* different MPEG encoding attributes that can run in parallel with
* the main stream. If so, then this should be implemented as a
* second video node. Abusing priv like this is certainly not the
* right approach.
if (pix->priv)
solo_enc->type = SOLO_ENC_TYPE_EXT;
*/
solo_update_mode(solo_enc);
return 0;
}
static int solo_enc_get_fmt_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct v4l2_pix_format *pix = &f->fmt.pix;
pix->width = solo_enc->width;
pix->height = solo_enc->height;
pix->pixelformat = solo_enc->fmt;
pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
V4L2_FIELD_NONE;
pix->sizeimage = FRAME_BUF_SIZE;
pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
return 0;
}
static int solo_enc_g_std(struct file *file, void *priv, v4l2_std_id *i)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_dev *solo_dev = solo_enc->solo_dev;
if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
*i = V4L2_STD_NTSC_M;
else
*i = V4L2_STD_PAL;
return 0;
}
static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id std)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
return solo_set_video_type(solo_enc->solo_dev, std & V4L2_STD_625_50);
}
static int solo_enum_framesizes(struct file *file, void *priv,
struct v4l2_frmsizeenum *fsize)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_dev *solo_dev = solo_enc->solo_dev;
if (solo_valid_pixfmt(fsize->pixel_format, solo_dev->type))
return -EINVAL;
switch (fsize->index) {
case 0:
fsize->discrete.width = solo_dev->video_hsize >> 1;
fsize->discrete.height = solo_dev->video_vsize;
break;
case 1:
fsize->discrete.width = solo_dev->video_hsize;
fsize->discrete.height = solo_dev->video_vsize << 1;
break;
default:
return -EINVAL;
}
fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
return 0;
}
static int solo_enum_frameintervals(struct file *file, void *priv,
struct v4l2_frmivalenum *fintv)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct solo_dev *solo_dev = solo_enc->solo_dev;
if (solo_valid_pixfmt(fintv->pixel_format, solo_dev->type))
return -EINVAL;
if (fintv->index)
return -EINVAL;
if ((fintv->width != solo_dev->video_hsize >> 1 ||
fintv->height != solo_dev->video_vsize) &&
(fintv->width != solo_dev->video_hsize ||
fintv->height != solo_dev->video_vsize << 1))
return -EINVAL;
fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
fintv->stepwise.min.numerator = 1;
fintv->stepwise.min.denominator = solo_dev->fps;
fintv->stepwise.max.numerator = 15;
fintv->stepwise.max.denominator = solo_dev->fps;
fintv->stepwise.step.numerator = 1;
fintv->stepwise.step.denominator = solo_dev->fps;
return 0;
}
static int solo_g_parm(struct file *file, void *priv,
struct v4l2_streamparm *sp)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct v4l2_captureparm *cp = &sp->parm.capture;
cp->capability = V4L2_CAP_TIMEPERFRAME;
cp->timeperframe.numerator = solo_enc->interval;
cp->timeperframe.denominator = solo_enc->solo_dev->fps;
cp->capturemode = 0;
/* XXX: Shouldn't we be able to get/set this from vb2? */
cp->readbuffers = 2;
return 0;
}
static inline int calc_interval(u8 fps, u32 n, u32 d)
{
if (!n || !d)
return 1;
if (d == fps)
return n;
n *= fps;
return min(15U, n / d + (n % d >= (fps >> 1)));
}
static int solo_s_parm(struct file *file, void *priv,
struct v4l2_streamparm *sp)
{
struct solo_enc_dev *solo_enc = video_drvdata(file);
struct v4l2_fract *t = &sp->parm.capture.timeperframe;
u8 fps = solo_enc->solo_dev->fps;
if (vb2_is_streaming(&solo_enc->vidq))
return -EBUSY;
solo_enc->interval = calc_interval(fps, t->numerator, t->denominator);
solo_update_mode(solo_enc);
return solo_g_parm(file, priv, sp);
}
static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct solo_enc_dev *solo_enc =
container_of(ctrl->handler, struct solo_enc_dev, hdl);
struct solo_dev *solo_dev = solo_enc->solo_dev;
int err;
switch (ctrl->id) {
case V4L2_CID_BRIGHTNESS:
case V4L2_CID_CONTRAST:
case V4L2_CID_SATURATION:
case V4L2_CID_HUE:
case V4L2_CID_SHARPNESS:
return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
ctrl->val);
case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
solo_enc->gop = ctrl->val;
solo_reg_write(solo_dev, SOLO_VE_CH_GOP(solo_enc->ch), solo_enc->gop);
solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(solo_enc->ch), solo_enc->gop);
return 0;
case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
solo_enc->qp = ctrl->val;
solo_reg_write(solo_dev, SOLO_VE_CH_QP(solo_enc->ch), solo_enc->qp);
solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(solo_enc->ch), solo_enc->qp);
return 0;
case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
solo_enc->motion_thresh = ctrl->val << 8;
if (!solo_enc->motion_global || !solo_enc->motion_enabled)
return 0;
return solo_set_motion_threshold(solo_dev, solo_enc->ch,
solo_enc->motion_thresh);
case V4L2_CID_DETECT_MD_MODE:
solo_enc->motion_global = ctrl->val == V4L2_DETECT_MD_MODE_GLOBAL;
solo_enc->motion_enabled = ctrl->val > V4L2_DETECT_MD_MODE_DISABLED;
if (ctrl->val) {
if (solo_enc->motion_global)
err = solo_set_motion_threshold(solo_dev, solo_enc->ch,
solo_enc->motion_thresh);
else
err = solo_set_motion_block(solo_dev, solo_enc->ch,
solo_enc->md_thresholds->p_cur.p_u16);
if (err)
return err;
}
solo_motion_toggle(solo_enc, ctrl->val);
return 0;
case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
if (solo_enc->motion_enabled && !solo_enc->motion_global)
return solo_set_motion_block(solo_dev, solo_enc->ch,
solo_enc->md_thresholds->p_new.p_u16);
break;
case V4L2_CID_OSD_TEXT:
strscpy(solo_enc->osd_text, ctrl->p_new.p_char,
sizeof(solo_enc->osd_text));
return solo_osd_print(solo_enc);
default:
return -EINVAL;
}
return 0;
}
static int solo_subscribe_event(struct v4l2_fh *fh,
const struct v4l2_event_subscription *sub)
{
switch (sub->type) {
case V4L2_EVENT_MOTION_DET:
/* Allow for up to 30 events (1 second for NTSC) to be
* stored. */
return v4l2_event_subscribe(fh, sub, 30, NULL);
default:
return v4l2_ctrl_subscribe_event(fh, sub);
}
}
static const struct v4l2_file_operations solo_enc_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = vb2_fop_release,
.read = vb2_fop_read,
.poll = vb2_fop_poll,
.mmap = vb2_fop_mmap,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
.vidioc_querycap = solo_enc_querycap,
.vidioc_s_std = solo_enc_s_std,
.vidioc_g_std = solo_enc_g_std,
/* Input callbacks */
.vidioc_enum_input = solo_enc_enum_input,
.vidioc_s_input = solo_enc_set_input,
.vidioc_g_input = solo_enc_get_input,
/* Video capture format callbacks */
.vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
.vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
.vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
.vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
/* Streaming I/O */
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
/* Frame size and interval */
.vidioc_enum_framesizes = solo_enum_framesizes,
.vidioc_enum_frameintervals = solo_enum_frameintervals,
/* Video capture parameters */
.vidioc_s_parm = solo_s_parm,
.vidioc_g_parm = solo_g_parm,
/* Logging and events */
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = solo_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct video_device solo_enc_template = {
.name = SOLO6X10_NAME,
.fops = &solo_enc_fops,
.ioctl_ops = &solo_enc_ioctl_ops,
.minor = -1,
.release = video_device_release,
.tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
V4L2_CAP_STREAMING,
};
static const struct v4l2_ctrl_ops solo_ctrl_ops = {
.s_ctrl = solo_s_ctrl,
};
static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
.ops = &solo_ctrl_ops,
.id = V4L2_CID_OSD_TEXT,
.name = "OSD Text",
.type = V4L2_CTRL_TYPE_STRING,
.max = OSD_TEXT_MAX,
.step = 1,
};
/* Motion Detection Threshold matrix */
static const struct v4l2_ctrl_config solo_md_thresholds = {
.ops = &solo_ctrl_ops,
.id = V4L2_CID_DETECT_MD_THRESHOLD_GRID,
.dims = { SOLO_MOTION_SZ, SOLO_MOTION_SZ },
.def = SOLO_DEF_MOT_THRESH,
.max = 65535,
.step = 1,
};
static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
u8 ch, unsigned nr)
{
struct solo_enc_dev *solo_enc;
struct v4l2_ctrl_handler *hdl;
int ret;
solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
if (!solo_enc)
return ERR_PTR(-ENOMEM);
hdl = &solo_enc->hdl;
v4l2_ctrl_handler_init(hdl, 10);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_CONTRAST, 0, 255, 1, 128);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_SATURATION, 0, 255, 1, 128);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_HUE, 0, 255, 1, 128);
if (tw28_has_sharpness(solo_dev, ch))
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_SHARPNESS, 0, 15, 1, 0);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 31, 1, SOLO_DEFAULT_QP);
v4l2_ctrl_new_std_menu(hdl, &solo_ctrl_ops,
V4L2_CID_DETECT_MD_MODE,
V4L2_DETECT_MD_MODE_THRESHOLD_GRID, 0,
V4L2_DETECT_MD_MODE_DISABLED);
v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD, 0, 0xff, 1,
SOLO_DEF_MOT_THRESH >> 8);
v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
solo_enc->md_thresholds =
v4l2_ctrl_new_custom(hdl, &solo_md_thresholds, NULL);
if (hdl->error) {
ret = hdl->error;
goto hdl_free;
}
solo_enc->solo_dev = solo_dev;
solo_enc->ch = ch;
mutex_init(&solo_enc->lock);
spin_lock_init(&solo_enc->av_lock);
INIT_LIST_HEAD(&solo_enc->vidq_active);
solo_enc->fmt = (solo_dev->type == SOLO_DEV_6010) ?
V4L2_PIX_FMT_MPEG4 : V4L2_PIX_FMT_H264;
solo_enc->type = SOLO_ENC_TYPE_STD;
solo_enc->qp = SOLO_DEFAULT_QP;
solo_enc->gop = solo_dev->fps;
solo_enc->interval = 1;
solo_enc->mode = SOLO_ENC_MODE_CIF;
solo_enc->motion_global = true;
solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
solo_enc->vidq.ops = &solo_enc_video_qops;
solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
solo_enc->vidq.drv_priv = solo_enc;
solo_enc->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
solo_enc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
solo_enc->vidq.lock = &solo_enc->lock;
solo_enc->vidq.dev = &solo_dev->pdev->dev;
ret = vb2_queue_init(&solo_enc->vidq);
if (ret)
goto hdl_free;
solo_update_mode(solo_enc);
spin_lock_init(&solo_enc->motion_lock);
/* Initialize this per encoder */
solo_enc->jpeg_len = sizeof(jpeg_header);
memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
solo_enc->desc_nelts = 32;
solo_enc->desc_items = dma_alloc_coherent(&solo_dev->pdev->dev,
sizeof(struct solo_p2m_desc) *
solo_enc->desc_nelts,
&solo_enc->desc_dma,
GFP_KERNEL);
ret = -ENOMEM;
if (solo_enc->desc_items == NULL)
goto hdl_free;
solo_enc->vfd = video_device_alloc();
if (!solo_enc->vfd)
goto pci_free;
*solo_enc->vfd = solo_enc_template;
solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
solo_enc->vfd->ctrl_handler = hdl;
solo_enc->vfd->queue = &solo_enc->vidq;
solo_enc->vfd->lock = &solo_enc->lock;
video_set_drvdata(solo_enc->vfd, solo_enc);
ret = video_register_device(solo_enc->vfd, VFL_TYPE_VIDEO, nr);
if (ret < 0)
goto vdev_release;
snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
"%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
solo_enc->vfd->num);
return solo_enc;
vdev_release:
video_device_release(solo_enc->vfd);
pci_free:
dma_free_coherent(&solo_enc->solo_dev->pdev->dev,
sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
solo_enc->desc_items, solo_enc->desc_dma);
hdl_free:
v4l2_ctrl_handler_free(hdl);
kfree(solo_enc);
return ERR_PTR(ret);
}
static void solo_enc_free(struct solo_enc_dev *solo_enc)
{
if (solo_enc == NULL)
return;
dma_free_coherent(&solo_enc->solo_dev->pdev->dev,
sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
solo_enc->desc_items, solo_enc->desc_dma);
video_unregister_device(solo_enc->vfd);
v4l2_ctrl_handler_free(&solo_enc->hdl);
kfree(solo_enc);
}
int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
{
int i;
init_waitqueue_head(&solo_dev->ring_thread_wait);
solo_dev->vh_size = sizeof(vop_header);
solo_dev->vh_buf = dma_alloc_coherent(&solo_dev->pdev->dev,
solo_dev->vh_size,
&solo_dev->vh_dma, GFP_KERNEL);
if (solo_dev->vh_buf == NULL)
return -ENOMEM;
for (i = 0; i < solo_dev->nr_chans; i++) {
solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
if (IS_ERR(solo_dev->v4l2_enc[i]))
break;
}
if (i != solo_dev->nr_chans) {
int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
while (i--)
solo_enc_free(solo_dev->v4l2_enc[i]);
dma_free_coherent(&solo_dev->pdev->dev, solo_dev->vh_size,
solo_dev->vh_buf, solo_dev->vh_dma);
solo_dev->vh_buf = NULL;
return ret;
}
if (solo_dev->type == SOLO_DEV_6010)
solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
else
solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
solo_dev->v4l2_enc[0]->vfd->num,
solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
return solo_ring_start(solo_dev);
}
void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
{
int i;
solo_ring_stop(solo_dev);
for (i = 0; i < solo_dev->nr_chans; i++)
solo_enc_free(solo_dev->v4l2_enc[i]);
if (solo_dev->vh_buf)
dma_free_coherent(&solo_dev->pdev->dev, solo_dev->vh_size,
solo_dev->vh_buf, solo_dev->vh_dma);
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-v4l2-enc.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/mempool.h>
#include <linux/poll.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/control.h>
#include "solo6x10.h"
#include "solo6x10-tw28.h"
#define G723_FDMA_PAGES 32
#define G723_PERIOD_BYTES 48
#define G723_PERIOD_BLOCK 1024
#define G723_FRAMES_PER_PAGE 48
/* Sets up channels 16-19 for decoding and 0-15 for encoding */
#define OUTMODE_MASK 0x300
#define SAMPLERATE 8000
#define BITRATE 25
/* The solo writes to 1k byte pages, 32 pages, in the dma. Each 1k page
* is broken down to 20 * 48 byte regions (one for each channel possible)
* with the rest of the page being dummy data. */
#define PERIODS G723_FDMA_PAGES
#define G723_INTR_ORDER 4 /* 0 - 4 */
struct solo_snd_pcm {
int on;
spinlock_t lock;
struct solo_dev *solo_dev;
u8 *g723_buf;
dma_addr_t g723_dma;
};
static void solo_g723_config(struct solo_dev *solo_dev)
{
int clk_div;
clk_div = (solo_dev->clock_mhz * 1000000)
/ (SAMPLERATE * (BITRATE * 2) * 2);
solo_reg_write(solo_dev, SOLO_AUDIO_SAMPLE,
SOLO_AUDIO_BITRATE(BITRATE)
| SOLO_AUDIO_CLK_DIV(clk_div));
solo_reg_write(solo_dev, SOLO_AUDIO_FDMA_INTR,
SOLO_AUDIO_FDMA_INTERVAL(1)
| SOLO_AUDIO_INTR_ORDER(G723_INTR_ORDER)
| SOLO_AUDIO_FDMA_BASE(SOLO_G723_EXT_ADDR(solo_dev) >> 16));
solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL,
SOLO_AUDIO_ENABLE
| SOLO_AUDIO_I2S_MODE
| SOLO_AUDIO_I2S_MULTI(3)
| SOLO_AUDIO_MODE(OUTMODE_MASK));
}
void solo_g723_isr(struct solo_dev *solo_dev)
{
struct snd_pcm_str *pstr =
&solo_dev->snd_pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
struct snd_pcm_substream *ss;
struct solo_snd_pcm *solo_pcm;
for (ss = pstr->substream; ss != NULL; ss = ss->next) {
if (snd_pcm_substream_chip(ss) == NULL)
continue;
/* This means open() hasn't been called on this one */
if (snd_pcm_substream_chip(ss) == solo_dev)
continue;
/* Haven't triggered a start yet */
solo_pcm = snd_pcm_substream_chip(ss);
if (!solo_pcm->on)
continue;
snd_pcm_period_elapsed(ss);
}
}
static const struct snd_pcm_hardware snd_solo_pcm_hw = {
.info = (SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP_VALID),
.formats = SNDRV_PCM_FMTBIT_U8,
.rates = SNDRV_PCM_RATE_8000,
.rate_min = SAMPLERATE,
.rate_max = SAMPLERATE,
.channels_min = 1,
.channels_max = 1,
.buffer_bytes_max = G723_PERIOD_BYTES * PERIODS,
.period_bytes_min = G723_PERIOD_BYTES,
.period_bytes_max = G723_PERIOD_BYTES,
.periods_min = PERIODS,
.periods_max = PERIODS,
};
static int snd_solo_pcm_open(struct snd_pcm_substream *ss)
{
struct solo_dev *solo_dev = snd_pcm_substream_chip(ss);
struct solo_snd_pcm *solo_pcm;
solo_pcm = kzalloc(sizeof(*solo_pcm), GFP_KERNEL);
if (solo_pcm == NULL)
goto oom;
solo_pcm->g723_buf = dma_alloc_coherent(&solo_dev->pdev->dev,
G723_PERIOD_BYTES,
&solo_pcm->g723_dma,
GFP_KERNEL);
if (solo_pcm->g723_buf == NULL)
goto oom;
spin_lock_init(&solo_pcm->lock);
solo_pcm->solo_dev = solo_dev;
ss->runtime->hw = snd_solo_pcm_hw;
snd_pcm_substream_chip(ss) = solo_pcm;
return 0;
oom:
kfree(solo_pcm);
return -ENOMEM;
}
static int snd_solo_pcm_close(struct snd_pcm_substream *ss)
{
struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
snd_pcm_substream_chip(ss) = solo_pcm->solo_dev;
dma_free_coherent(&solo_pcm->solo_dev->pdev->dev, G723_PERIOD_BYTES,
solo_pcm->g723_buf, solo_pcm->g723_dma);
kfree(solo_pcm);
return 0;
}
static int snd_solo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
{
struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
struct solo_dev *solo_dev = solo_pcm->solo_dev;
int ret = 0;
spin_lock(&solo_pcm->lock);
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
if (solo_pcm->on == 0) {
/* If this is the first user, switch on interrupts */
if (atomic_inc_return(&solo_dev->snd_users) == 1)
solo_irq_on(solo_dev, SOLO_IRQ_G723);
solo_pcm->on = 1;
}
break;
case SNDRV_PCM_TRIGGER_STOP:
if (solo_pcm->on) {
/* If this was our last user, switch them off */
if (atomic_dec_return(&solo_dev->snd_users) == 0)
solo_irq_off(solo_dev, SOLO_IRQ_G723);
solo_pcm->on = 0;
}
break;
default:
ret = -EINVAL;
}
spin_unlock(&solo_pcm->lock);
return ret;
}
static int snd_solo_pcm_prepare(struct snd_pcm_substream *ss)
{
return 0;
}
static snd_pcm_uframes_t snd_solo_pcm_pointer(struct snd_pcm_substream *ss)
{
struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
struct solo_dev *solo_dev = solo_pcm->solo_dev;
snd_pcm_uframes_t idx = solo_reg_read(solo_dev, SOLO_AUDIO_STA) & 0x1f;
return idx * G723_FRAMES_PER_PAGE;
}
static int snd_solo_pcm_copy(struct snd_pcm_substream *ss, int channel,
unsigned long pos, struct iov_iter *dst,
unsigned long count)
{
struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
struct solo_dev *solo_dev = solo_pcm->solo_dev;
int err, i;
for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
int page = (pos / G723_FRAMES_PER_PAGE) + i;
err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
SOLO_G723_EXT_ADDR(solo_dev) +
(page * G723_PERIOD_BLOCK) +
(ss->number * G723_PERIOD_BYTES),
G723_PERIOD_BYTES, 0, 0);
if (err)
return err;
if (copy_to_iter(solo_pcm->g723_buf, G723_PERIOD_BYTES, dst) !=
G723_PERIOD_BYTES)
return -EFAULT;
}
return 0;
}
static const struct snd_pcm_ops snd_solo_pcm_ops = {
.open = snd_solo_pcm_open,
.close = snd_solo_pcm_close,
.prepare = snd_solo_pcm_prepare,
.trigger = snd_solo_pcm_trigger,
.pointer = snd_solo_pcm_pointer,
.copy = snd_solo_pcm_copy,
};
static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *info)
{
info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
info->count = 1;
info->value.integer.min = 0;
info->value.integer.max = 15;
info->value.integer.step = 1;
return 0;
}
static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *value)
{
struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
u8 ch = value->id.numid - 1;
value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
return 0;
}
static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *value)
{
struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
u8 ch = value->id.numid - 1;
u8 old_val;
old_val = tw28_get_audio_gain(solo_dev, ch);
if (old_val == value->value.integer.value[0])
return 0;
tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
return 1;
}
static const struct snd_kcontrol_new snd_solo_capture_volume = {
.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
.name = "Capture Volume",
.info = snd_solo_capture_volume_info,
.get = snd_solo_capture_volume_get,
.put = snd_solo_capture_volume_put,
};
static int solo_snd_pcm_init(struct solo_dev *solo_dev)
{
struct snd_card *card = solo_dev->snd_card;
struct snd_pcm *pcm;
struct snd_pcm_substream *ss;
int ret;
int i;
ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
&pcm);
if (ret < 0)
return ret;
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
&snd_solo_pcm_ops);
snd_pcm_chip(pcm) = solo_dev;
pcm->info_flags = 0;
strscpy(pcm->name, card->shortname, sizeof(pcm->name));
for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
ss; ss = ss->next, i++)
sprintf(ss->name, "Camera #%d Audio", i);
snd_pcm_set_managed_buffer_all(pcm,
SNDRV_DMA_TYPE_CONTINUOUS,
NULL,
G723_PERIOD_BYTES * PERIODS,
G723_PERIOD_BYTES * PERIODS);
solo_dev->snd_pcm = pcm;
return 0;
}
int solo_g723_init(struct solo_dev *solo_dev)
{
static struct snd_device_ops ops = { };
struct snd_card *card;
struct snd_kcontrol_new kctl;
char name[32];
int ret;
atomic_set(&solo_dev->snd_users, 0);
/* Allows for easier mapping between video and audio */
sprintf(name, "Softlogic%d", solo_dev->vfd->num);
ret = snd_card_new(&solo_dev->pdev->dev,
SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
&solo_dev->snd_card);
if (ret < 0)
return ret;
card = solo_dev->snd_card;
strscpy(card->driver, SOLO6X10_NAME, sizeof(card->driver));
strscpy(card->shortname, "SOLO-6x10 Audio", sizeof(card->shortname));
sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
pci_name(solo_dev->pdev), solo_dev->pdev->irq);
ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
if (ret < 0)
goto snd_error;
/* Mixer controls */
strscpy(card->mixername, "SOLO-6x10", sizeof(card->mixername));
kctl = snd_solo_capture_volume;
kctl.count = solo_dev->nr_chans;
ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
if (ret < 0)
goto snd_error;
ret = solo_snd_pcm_init(solo_dev);
if (ret < 0)
goto snd_error;
ret = snd_card_register(card);
if (ret < 0)
goto snd_error;
solo_g723_config(solo_dev);
dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
return 0;
snd_error:
snd_card_free(card);
return ret;
}
void solo_g723_exit(struct solo_dev *solo_dev)
{
if (!solo_dev->snd_card)
return;
solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
solo_irq_off(solo_dev, SOLO_IRQ_G723);
snd_card_free(solo_dev->snd_card);
solo_dev->snd_card = NULL;
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-g723.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include "solo6x10.h"
static void solo_gpio_mode(struct solo_dev *solo_dev,
unsigned int port_mask, unsigned int mode)
{
int port;
unsigned int ret;
ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_0);
/* To set gpio */
for (port = 0; port < 16; port++) {
if (!((1 << port) & port_mask))
continue;
ret &= (~(3 << (port << 1)));
ret |= ((mode & 3) << (port << 1));
}
solo_reg_write(solo_dev, SOLO_GPIO_CONFIG_0, ret);
/* To set extended gpio - sensor */
ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_1);
for (port = 0; port < 16; port++) {
if (!((1UL << (port + 16)) & port_mask))
continue;
if (!mode)
ret &= ~(1UL << port);
else
ret |= 1UL << port;
}
/* Enable GPIO[31:16] */
ret |= 0xffff0000;
solo_reg_write(solo_dev, SOLO_GPIO_CONFIG_1, ret);
}
static void solo_gpio_set(struct solo_dev *solo_dev, unsigned int value)
{
solo_reg_write(solo_dev, SOLO_GPIO_DATA_OUT,
solo_reg_read(solo_dev, SOLO_GPIO_DATA_OUT) | value);
}
static void solo_gpio_clear(struct solo_dev *solo_dev, unsigned int value)
{
solo_reg_write(solo_dev, SOLO_GPIO_DATA_OUT,
solo_reg_read(solo_dev, SOLO_GPIO_DATA_OUT) & ~value);
}
static void solo_gpio_config(struct solo_dev *solo_dev)
{
/* Video reset */
solo_gpio_mode(solo_dev, 0x30, 1);
solo_gpio_clear(solo_dev, 0x30);
udelay(100);
solo_gpio_set(solo_dev, 0x30);
udelay(100);
/* Warning: Don't touch the next line unless you're sure of what
* you're doing: first four gpio [0-3] are used for video. */
solo_gpio_mode(solo_dev, 0x0f, 2);
/* We use bit 8-15 of SOLO_GPIO_CONFIG_0 for relay purposes */
solo_gpio_mode(solo_dev, 0xff00, 1);
/* Initially set relay status to 0 */
solo_gpio_clear(solo_dev, 0xff00);
/* Set input pins direction */
solo_gpio_mode(solo_dev, 0xffff0000, 0);
}
#ifdef CONFIG_GPIOLIB
/* Pins 0-7 are not exported, because it seems from code above they are
* used for internal purposes. So offset 0 corresponds to pin 8, therefore
* offsets 0-7 are relay GPIOs, 8-23 - input GPIOs.
*/
static int solo_gpiochip_get_direction(struct gpio_chip *chip,
unsigned int offset)
{
int ret, mode;
struct solo_dev *solo_dev = gpiochip_get_data(chip);
if (offset < 8) {
ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_0);
mode = 3 & (ret >> ((offset + 8) * 2));
} else {
ret = solo_reg_read(solo_dev, SOLO_GPIO_CONFIG_1);
mode = 1 & (ret >> (offset - 8));
}
if (!mode)
return 1;
else if (mode == 1)
return 0;
return -1;
}
static int solo_gpiochip_direction_input(struct gpio_chip *chip,
unsigned int offset)
{
return -1;
}
static int solo_gpiochip_direction_output(struct gpio_chip *chip,
unsigned int offset, int value)
{
return -1;
}
static int solo_gpiochip_get(struct gpio_chip *chip,
unsigned int offset)
{
int ret;
struct solo_dev *solo_dev = gpiochip_get_data(chip);
ret = solo_reg_read(solo_dev, SOLO_GPIO_DATA_IN);
return 1 & (ret >> (offset + 8));
}
static void solo_gpiochip_set(struct gpio_chip *chip,
unsigned int offset, int value)
{
struct solo_dev *solo_dev = gpiochip_get_data(chip);
if (value)
solo_gpio_set(solo_dev, 1 << (offset + 8));
else
solo_gpio_clear(solo_dev, 1 << (offset + 8));
}
#endif
int solo_gpio_init(struct solo_dev *solo_dev)
{
#ifdef CONFIG_GPIOLIB
int ret;
#endif
solo_gpio_config(solo_dev);
#ifdef CONFIG_GPIOLIB
solo_dev->gpio_dev.label = SOLO6X10_NAME"_gpio";
solo_dev->gpio_dev.parent = &solo_dev->pdev->dev;
solo_dev->gpio_dev.owner = THIS_MODULE;
solo_dev->gpio_dev.base = -1;
solo_dev->gpio_dev.ngpio = 24;
solo_dev->gpio_dev.can_sleep = 0;
solo_dev->gpio_dev.get_direction = solo_gpiochip_get_direction;
solo_dev->gpio_dev.direction_input = solo_gpiochip_direction_input;
solo_dev->gpio_dev.direction_output = solo_gpiochip_direction_output;
solo_dev->gpio_dev.get = solo_gpiochip_get;
solo_dev->gpio_dev.set = solo_gpiochip_set;
ret = gpiochip_add_data(&solo_dev->gpio_dev, solo_dev);
if (ret) {
solo_dev->gpio_dev.label = NULL;
return -1;
}
#endif
return 0;
}
void solo_gpio_exit(struct solo_dev *solo_dev)
{
#ifdef CONFIG_GPIOLIB
if (solo_dev->gpio_dev.label) {
gpiochip_remove(&solo_dev->gpio_dev);
solo_dev->gpio_dev.label = NULL;
}
#endif
solo_gpio_clear(solo_dev, 0x30);
solo_gpio_config(solo_dev);
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-gpio.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/delay.h>
#include "solo6x10.h"
/* Control */
#define EE_SHIFT_CLK 0x04
#define EE_CS 0x08
#define EE_DATA_WRITE 0x02
#define EE_DATA_READ 0x01
#define EE_ENB (0x80 | EE_CS)
#define eeprom_delay() udelay(100)
#if 0
#define eeprom_delay() solo_reg_read(solo_dev, SOLO_EEPROM_CTRL)
#define eeprom_delay() ({ \
int i, ret; \
udelay(100); \
for (i = ret = 0; i < 1000 && !ret; i++) \
ret = solo_eeprom_reg_read(solo_dev); \
})
#endif
#define ADDR_LEN 6
/* Commands */
#define EE_EWEN_CMD 4
#define EE_EWDS_CMD 4
#define EE_WRITE_CMD 5
#define EE_READ_CMD 6
#define EE_ERASE_CMD 7
static unsigned int solo_eeprom_reg_read(struct solo_dev *solo_dev)
{
return solo_reg_read(solo_dev, SOLO_EEPROM_CTRL) & EE_DATA_READ;
}
static void solo_eeprom_reg_write(struct solo_dev *solo_dev, u32 data)
{
solo_reg_write(solo_dev, SOLO_EEPROM_CTRL, data);
eeprom_delay();
}
static void solo_eeprom_cmd(struct solo_dev *solo_dev, int cmd)
{
int i;
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ACCESS_EN);
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
for (i = 4 + ADDR_LEN; i >= 0; i--) {
int dataval = (cmd & (1 << i)) ? EE_DATA_WRITE : 0;
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE | dataval);
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE |
EE_SHIFT_CLK | dataval);
}
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
}
unsigned int solo_eeprom_ewen(struct solo_dev *solo_dev, int w_en)
{
int ewen_cmd = (w_en ? 0x3f : 0) | (EE_EWEN_CMD << ADDR_LEN);
unsigned int retval = 0;
int i;
solo_eeprom_cmd(solo_dev, ewen_cmd);
for (i = 0; i < 16; i++) {
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE |
EE_SHIFT_CLK);
retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
}
solo_eeprom_reg_write(solo_dev, ~EE_CS);
retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
return retval;
}
__be16 solo_eeprom_read(struct solo_dev *solo_dev, int loc)
{
int read_cmd = loc | (EE_READ_CMD << ADDR_LEN);
u16 retval = 0;
int i;
solo_eeprom_cmd(solo_dev, read_cmd);
for (i = 0; i < 16; i++) {
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE |
EE_SHIFT_CLK);
retval = (retval << 1) | solo_eeprom_reg_read(solo_dev);
solo_eeprom_reg_write(solo_dev, SOLO_EEPROM_ENABLE);
}
solo_eeprom_reg_write(solo_dev, ~EE_CS);
return (__force __be16)retval;
}
int solo_eeprom_write(struct solo_dev *solo_dev, int loc,
__be16 data)
{
int write_cmd = loc | (EE_WRITE_CMD << ADDR_LEN);
unsigned int retval;
int i;
solo_eeprom_cmd(solo_dev, write_cmd);
for (i = 15; i >= 0; i--) {
unsigned int dataval = ((__force unsigned)data >> i) & 1;
solo_eeprom_reg_write(solo_dev, EE_ENB);
solo_eeprom_reg_write(solo_dev,
EE_ENB | (dataval << 1) | EE_SHIFT_CLK);
}
solo_eeprom_reg_write(solo_dev, EE_ENB);
solo_eeprom_reg_write(solo_dev, ~EE_CS);
solo_eeprom_reg_write(solo_dev, EE_ENB);
for (i = retval = 0; i < 10000 && !retval; i++)
retval = solo_eeprom_reg_read(solo_dev);
solo_eeprom_reg_write(solo_dev, ~EE_CS);
return !retval;
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-eeprom.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
*
* Original author:
* Ben Collins <[email protected]>
*
* Additional work by:
* John Brooks <[email protected]>
*/
/* XXX: The SOLO6x10 i2c does not have separate interrupts for each i2c
* channel. The bus can only handle one i2c event at a time. The below handles
* this all wrong. We should be using the status registers to see if the bus
* is in use, and have a global lock to check the status register. Also,
* the bulk of the work should be handled out-of-interrupt. The ugly loops
* that occur during interrupt scare me. The ISR should merely signal
* thread context, ACK the interrupt, and move on. -- BenC */
#include <linux/kernel.h>
#include <linux/sched/signal.h>
#include "solo6x10.h"
u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off)
{
struct i2c_msg msgs[2];
u8 data;
msgs[0].flags = 0;
msgs[0].addr = addr;
msgs[0].len = 1;
msgs[0].buf = &off;
msgs[1].flags = I2C_M_RD;
msgs[1].addr = addr;
msgs[1].len = 1;
msgs[1].buf = &data;
i2c_transfer(&solo_dev->i2c_adap[id], msgs, 2);
return data;
}
void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr,
u8 off, u8 data)
{
struct i2c_msg msgs;
u8 buf[2];
buf[0] = off;
buf[1] = data;
msgs.flags = 0;
msgs.addr = addr;
msgs.len = 2;
msgs.buf = buf;
i2c_transfer(&solo_dev->i2c_adap[id], &msgs, 1);
}
static void solo_i2c_flush(struct solo_dev *solo_dev, int wr)
{
u32 ctrl;
ctrl = SOLO_IIC_CH_SET(solo_dev->i2c_id);
if (solo_dev->i2c_state == IIC_STATE_START)
ctrl |= SOLO_IIC_START;
if (wr) {
ctrl |= SOLO_IIC_WRITE;
} else {
ctrl |= SOLO_IIC_READ;
if (!(solo_dev->i2c_msg->flags & I2C_M_NO_RD_ACK))
ctrl |= SOLO_IIC_ACK_EN;
}
if (solo_dev->i2c_msg_ptr == solo_dev->i2c_msg->len)
ctrl |= SOLO_IIC_STOP;
solo_reg_write(solo_dev, SOLO_IIC_CTRL, ctrl);
}
static void solo_i2c_start(struct solo_dev *solo_dev)
{
u32 addr = solo_dev->i2c_msg->addr << 1;
if (solo_dev->i2c_msg->flags & I2C_M_RD)
addr |= 1;
solo_dev->i2c_state = IIC_STATE_START;
solo_reg_write(solo_dev, SOLO_IIC_TXD, addr);
solo_i2c_flush(solo_dev, 1);
}
static void solo_i2c_stop(struct solo_dev *solo_dev)
{
solo_irq_off(solo_dev, SOLO_IRQ_IIC);
solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0);
solo_dev->i2c_state = IIC_STATE_STOP;
wake_up(&solo_dev->i2c_wait);
}
static int solo_i2c_handle_read(struct solo_dev *solo_dev)
{
prepare_read:
if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) {
solo_i2c_flush(solo_dev, 0);
return 0;
}
solo_dev->i2c_msg_ptr = 0;
solo_dev->i2c_msg++;
solo_dev->i2c_msg_num--;
if (solo_dev->i2c_msg_num == 0) {
solo_i2c_stop(solo_dev);
return 0;
}
if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) {
solo_i2c_start(solo_dev);
} else {
if (solo_dev->i2c_msg->flags & I2C_M_RD)
goto prepare_read;
else
solo_i2c_stop(solo_dev);
}
return 0;
}
static int solo_i2c_handle_write(struct solo_dev *solo_dev)
{
retry_write:
if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) {
solo_reg_write(solo_dev, SOLO_IIC_TXD,
solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr]);
solo_dev->i2c_msg_ptr++;
solo_i2c_flush(solo_dev, 1);
return 0;
}
solo_dev->i2c_msg_ptr = 0;
solo_dev->i2c_msg++;
solo_dev->i2c_msg_num--;
if (solo_dev->i2c_msg_num == 0) {
solo_i2c_stop(solo_dev);
return 0;
}
if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) {
solo_i2c_start(solo_dev);
} else {
if (solo_dev->i2c_msg->flags & I2C_M_RD)
solo_i2c_stop(solo_dev);
else
goto retry_write;
}
return 0;
}
int solo_i2c_isr(struct solo_dev *solo_dev)
{
u32 status = solo_reg_read(solo_dev, SOLO_IIC_CTRL);
int ret = -EINVAL;
if (CHK_FLAGS(status, SOLO_IIC_STATE_TRNS | SOLO_IIC_STATE_SIG_ERR)
|| solo_dev->i2c_id < 0) {
solo_i2c_stop(solo_dev);
return -ENXIO;
}
switch (solo_dev->i2c_state) {
case IIC_STATE_START:
if (solo_dev->i2c_msg->flags & I2C_M_RD) {
solo_dev->i2c_state = IIC_STATE_READ;
ret = solo_i2c_handle_read(solo_dev);
break;
}
solo_dev->i2c_state = IIC_STATE_WRITE;
fallthrough;
case IIC_STATE_WRITE:
ret = solo_i2c_handle_write(solo_dev);
break;
case IIC_STATE_READ:
solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr] =
solo_reg_read(solo_dev, SOLO_IIC_RXD);
solo_dev->i2c_msg_ptr++;
ret = solo_i2c_handle_read(solo_dev);
break;
default:
solo_i2c_stop(solo_dev);
}
return ret;
}
static int solo_i2c_master_xfer(struct i2c_adapter *adap,
struct i2c_msg msgs[], int num)
{
struct solo_dev *solo_dev = adap->algo_data;
unsigned long timeout;
int ret;
int i;
DEFINE_WAIT(wait);
for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
if (&solo_dev->i2c_adap[i] == adap)
break;
}
if (i == SOLO_I2C_ADAPTERS)
return num; /* XXX Right return value for failure? */
mutex_lock(&solo_dev->i2c_mutex);
solo_dev->i2c_id = i;
solo_dev->i2c_msg = msgs;
solo_dev->i2c_msg_num = num;
solo_dev->i2c_msg_ptr = 0;
solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0);
solo_irq_on(solo_dev, SOLO_IRQ_IIC);
solo_i2c_start(solo_dev);
timeout = HZ / 2;
for (;;) {
prepare_to_wait(&solo_dev->i2c_wait, &wait,
TASK_INTERRUPTIBLE);
if (solo_dev->i2c_state == IIC_STATE_STOP)
break;
timeout = schedule_timeout(timeout);
if (!timeout)
break;
if (signal_pending(current))
break;
}
finish_wait(&solo_dev->i2c_wait, &wait);
ret = num - solo_dev->i2c_msg_num;
solo_dev->i2c_state = IIC_STATE_IDLE;
solo_dev->i2c_id = -1;
mutex_unlock(&solo_dev->i2c_mutex);
return ret;
}
static u32 solo_i2c_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C;
}
static const struct i2c_algorithm solo_i2c_algo = {
.master_xfer = solo_i2c_master_xfer,
.functionality = solo_i2c_functionality,
};
int solo_i2c_init(struct solo_dev *solo_dev)
{
int i;
int ret;
solo_reg_write(solo_dev, SOLO_IIC_CFG,
SOLO_IIC_PRESCALE(8) | SOLO_IIC_ENABLE);
solo_dev->i2c_id = -1;
solo_dev->i2c_state = IIC_STATE_IDLE;
init_waitqueue_head(&solo_dev->i2c_wait);
mutex_init(&solo_dev->i2c_mutex);
for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
struct i2c_adapter *adap = &solo_dev->i2c_adap[i];
snprintf(adap->name, I2C_NAME_SIZE, "%s I2C %d",
SOLO6X10_NAME, i);
adap->algo = &solo_i2c_algo;
adap->algo_data = solo_dev;
adap->retries = 1;
adap->dev.parent = &solo_dev->pdev->dev;
ret = i2c_add_adapter(adap);
if (ret) {
adap->algo_data = NULL;
break;
}
}
if (ret) {
for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
if (!solo_dev->i2c_adap[i].algo_data)
break;
i2c_del_adapter(&solo_dev->i2c_adap[i]);
solo_dev->i2c_adap[i].algo_data = NULL;
}
return ret;
}
return 0;
}
void solo_i2c_exit(struct solo_dev *solo_dev)
{
int i;
for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
if (!solo_dev->i2c_adap[i].algo_data)
continue;
i2c_del_adapter(&solo_dev->i2c_adap[i]);
solo_dev->i2c_adap[i].algo_data = NULL;
}
}
| linux-master | drivers/media/pci/solo6x10/solo6x10-i2c.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* drivers/media/radio/radio-si476x.c -- V4L2 driver for SI476X chips
*
* Copyright (C) 2012 Innovative Converged Devices(ICD)
* Copyright (C) 2013 Andrey Smirnov
*
* Author: Andrey Smirnov <[email protected]>
*/
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#include <linux/videodev2.h>
#include <linux/mutex.h>
#include <linux/debugfs.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <media/v4l2-device.h>
#include <media/drv-intf/si476x.h>
#include <linux/mfd/si476x-core.h>
#define FM_FREQ_RANGE_LOW 64000000
#define FM_FREQ_RANGE_HIGH 108000000
#define AM_FREQ_RANGE_LOW 520000
#define AM_FREQ_RANGE_HIGH 30000000
#define PWRLINEFLTR (1 << 8)
#define FREQ_MUL (10000000 / 625)
#define SI476X_PHDIV_STATUS_LINK_LOCKED(status) (0x80 & (status))
#define DRIVER_NAME "si476x-radio"
#define DRIVER_CARD "SI476x AM/FM Receiver"
enum si476x_freq_bands {
SI476X_BAND_FM,
SI476X_BAND_AM,
};
static const struct v4l2_frequency_band si476x_bands[] = {
[SI476X_BAND_FM] = {
.type = V4L2_TUNER_RADIO,
.index = SI476X_BAND_FM,
.capability = V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_RDS
| V4L2_TUNER_CAP_RDS_BLOCK_IO
| V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 64 * FREQ_MUL,
.rangehigh = 108 * FREQ_MUL,
.modulation = V4L2_BAND_MODULATION_FM,
},
[SI476X_BAND_AM] = {
.type = V4L2_TUNER_RADIO,
.index = SI476X_BAND_AM,
.capability = V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 0.52 * FREQ_MUL,
.rangehigh = 30 * FREQ_MUL,
.modulation = V4L2_BAND_MODULATION_AM,
},
};
static inline bool si476x_radio_freq_is_inside_of_the_band(u32 freq, int band)
{
return freq >= si476x_bands[band].rangelow &&
freq <= si476x_bands[band].rangehigh;
}
static inline bool si476x_radio_range_is_inside_of_the_band(u32 low, u32 high,
int band)
{
return low >= si476x_bands[band].rangelow &&
high <= si476x_bands[band].rangehigh;
}
static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl);
static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
enum phase_diversity_modes_idx {
SI476X_IDX_PHDIV_DISABLED,
SI476X_IDX_PHDIV_PRIMARY_COMBINING,
SI476X_IDX_PHDIV_PRIMARY_ANTENNA,
SI476X_IDX_PHDIV_SECONDARY_ANTENNA,
SI476X_IDX_PHDIV_SECONDARY_COMBINING,
};
static const char * const phase_diversity_modes[] = {
[SI476X_IDX_PHDIV_DISABLED] = "Disabled",
[SI476X_IDX_PHDIV_PRIMARY_COMBINING] = "Primary with Secondary",
[SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = "Primary Antenna",
[SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = "Secondary Antenna",
[SI476X_IDX_PHDIV_SECONDARY_COMBINING] = "Secondary with Primary",
};
static inline enum phase_diversity_modes_idx
si476x_phase_diversity_mode_to_idx(enum si476x_phase_diversity_mode mode)
{
switch (mode) {
default:
fallthrough;
case SI476X_PHDIV_DISABLED:
return SI476X_IDX_PHDIV_DISABLED;
case SI476X_PHDIV_PRIMARY_COMBINING:
return SI476X_IDX_PHDIV_PRIMARY_COMBINING;
case SI476X_PHDIV_PRIMARY_ANTENNA:
return SI476X_IDX_PHDIV_PRIMARY_ANTENNA;
case SI476X_PHDIV_SECONDARY_ANTENNA:
return SI476X_IDX_PHDIV_SECONDARY_ANTENNA;
case SI476X_PHDIV_SECONDARY_COMBINING:
return SI476X_IDX_PHDIV_SECONDARY_COMBINING;
}
}
static inline enum si476x_phase_diversity_mode
si476x_phase_diversity_idx_to_mode(enum phase_diversity_modes_idx idx)
{
static const int idx_to_value[] = {
[SI476X_IDX_PHDIV_DISABLED] = SI476X_PHDIV_DISABLED,
[SI476X_IDX_PHDIV_PRIMARY_COMBINING] = SI476X_PHDIV_PRIMARY_COMBINING,
[SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = SI476X_PHDIV_PRIMARY_ANTENNA,
[SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = SI476X_PHDIV_SECONDARY_ANTENNA,
[SI476X_IDX_PHDIV_SECONDARY_COMBINING] = SI476X_PHDIV_SECONDARY_COMBINING,
};
return idx_to_value[idx];
}
static const struct v4l2_ctrl_ops si476x_ctrl_ops = {
.g_volatile_ctrl = si476x_radio_g_volatile_ctrl,
.s_ctrl = si476x_radio_s_ctrl,
};
enum si476x_ctrl_idx {
SI476X_IDX_RSSI_THRESHOLD,
SI476X_IDX_SNR_THRESHOLD,
SI476X_IDX_MAX_TUNE_ERROR,
SI476X_IDX_HARMONICS_COUNT,
SI476X_IDX_DIVERSITY_MODE,
SI476X_IDX_INTERCHIP_LINK,
};
static struct v4l2_ctrl_config si476x_ctrls[] = {
/*
* SI476X during its station seeking(or tuning) process uses several
* parameters to determine if "the station" is valid:
*
* - Signal's SNR(in dBuV) must be lower than
* #V4L2_CID_SI476X_SNR_THRESHOLD
* - Signal's RSSI(in dBuV) must be greater than
* #V4L2_CID_SI476X_RSSI_THRESHOLD
* - Signal's frequency deviation(in units of 2ppm) must not be
* more than #V4L2_CID_SI476X_MAX_TUNE_ERROR
*/
[SI476X_IDX_RSSI_THRESHOLD] = {
.ops = &si476x_ctrl_ops,
.id = V4L2_CID_SI476X_RSSI_THRESHOLD,
.name = "Valid RSSI Threshold",
.type = V4L2_CTRL_TYPE_INTEGER,
.min = -128,
.max = 127,
.step = 1,
},
[SI476X_IDX_SNR_THRESHOLD] = {
.ops = &si476x_ctrl_ops,
.id = V4L2_CID_SI476X_SNR_THRESHOLD,
.type = V4L2_CTRL_TYPE_INTEGER,
.name = "Valid SNR Threshold",
.min = -128,
.max = 127,
.step = 1,
},
[SI476X_IDX_MAX_TUNE_ERROR] = {
.ops = &si476x_ctrl_ops,
.id = V4L2_CID_SI476X_MAX_TUNE_ERROR,
.type = V4L2_CTRL_TYPE_INTEGER,
.name = "Max Tune Errors",
.min = 0,
.max = 126 * 2,
.step = 2,
},
/*
* #V4L2_CID_SI476X_HARMONICS_COUNT -- number of harmonics
* built-in power-line noise supression filter is to reject
* during AM-mode operation.
*/
[SI476X_IDX_HARMONICS_COUNT] = {
.ops = &si476x_ctrl_ops,
.id = V4L2_CID_SI476X_HARMONICS_COUNT,
.type = V4L2_CTRL_TYPE_INTEGER,
.name = "Count of Harmonics to Reject",
.min = 0,
.max = 20,
.step = 1,
},
/*
* #V4L2_CID_SI476X_DIVERSITY_MODE -- configuration which
* two tuners working in diversity mode are to work in.
*
* - #SI476X_IDX_PHDIV_DISABLED diversity mode disabled
* - #SI476X_IDX_PHDIV_PRIMARY_COMBINING diversity mode is
* on, primary tuner's antenna is the main one.
* - #SI476X_IDX_PHDIV_PRIMARY_ANTENNA diversity mode is
* off, primary tuner's antenna is the main one.
* - #SI476X_IDX_PHDIV_SECONDARY_ANTENNA diversity mode is
* off, secondary tuner's antenna is the main one.
* - #SI476X_IDX_PHDIV_SECONDARY_COMBINING diversity mode is
* on, secondary tuner's antenna is the main one.
*/
[SI476X_IDX_DIVERSITY_MODE] = {
.ops = &si476x_ctrl_ops,
.id = V4L2_CID_SI476X_DIVERSITY_MODE,
.type = V4L2_CTRL_TYPE_MENU,
.name = "Phase Diversity Mode",
.qmenu = phase_diversity_modes,
.min = 0,
.max = ARRAY_SIZE(phase_diversity_modes) - 1,
},
/*
* #V4L2_CID_SI476X_INTERCHIP_LINK -- inter-chip link in
* diversity mode indicator. Allows user to determine if two
* chips working in diversity mode have established a link
* between each other and if the system as a whole uses
* signals from both antennas to receive FM radio.
*/
[SI476X_IDX_INTERCHIP_LINK] = {
.ops = &si476x_ctrl_ops,
.id = V4L2_CID_SI476X_INTERCHIP_LINK,
.type = V4L2_CTRL_TYPE_BOOLEAN,
.flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
.name = "Inter-Chip Link",
.min = 0,
.max = 1,
.step = 1,
},
};
struct si476x_radio;
/**
* struct si476x_radio_ops - vtable of tuner functions
*
* This table holds pointers to functions implementing particular
* operations depending on the mode in which the tuner chip was
* configured to start. If the function is not supported
* corresponding element is set to #NULL.
*
* @tune_freq: Tune chip to a specific frequency
* @seek_start: Star station seeking
* @rsq_status: Get Received Signal Quality(RSQ) status
* @rds_blckcnt: Get received RDS blocks count
* @phase_diversity: Change phase diversity mode of the tuner
* @phase_div_status: Get phase diversity mode status
* @acf_status: Get the status of Automatically Controlled
* Features(ACF)
* @agc_status: Get Automatic Gain Control(AGC) status
*/
struct si476x_radio_ops {
int (*tune_freq)(struct si476x_core *, struct si476x_tune_freq_args *);
int (*seek_start)(struct si476x_core *, bool, bool);
int (*rsq_status)(struct si476x_core *, struct si476x_rsq_status_args *,
struct si476x_rsq_status_report *);
int (*rds_blckcnt)(struct si476x_core *, bool,
struct si476x_rds_blockcount_report *);
int (*phase_diversity)(struct si476x_core *,
enum si476x_phase_diversity_mode);
int (*phase_div_status)(struct si476x_core *);
int (*acf_status)(struct si476x_core *,
struct si476x_acf_status_report *);
int (*agc_status)(struct si476x_core *,
struct si476x_agc_status_report *);
};
/**
* struct si476x_radio - radio device
*
* @v4l2dev: Pointer to V4L2 device created by V4L2 subsystem
* @videodev: Pointer to video device created by V4L2 subsystem
* @ctrl_handler: V4L2 controls handler
* @core: Pointer to underlying core device
* @ops: Vtable of functions. See struct si476x_radio_ops for details
* @debugfs: pointer to &strucd dentry for debugfs
* @audmode: audio mode, as defined for the rxsubchans field
* at videodev2.h
*
* core structure is the radio device is being used
*/
struct si476x_radio {
struct v4l2_device v4l2dev;
struct video_device videodev;
struct v4l2_ctrl_handler ctrl_handler;
struct si476x_core *core;
/* This field should not be accesses unless core lock is held */
const struct si476x_radio_ops *ops;
struct dentry *debugfs;
u32 audmode;
};
static inline struct si476x_radio *
v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
{
return container_of(d, struct si476x_radio, ctrl_handler);
}
/*
* si476x_vidioc_querycap - query device capabilities
*/
static int si476x_radio_querycap(struct file *file, void *priv,
struct v4l2_capability *capability)
{
struct si476x_radio *radio = video_drvdata(file);
strscpy(capability->driver, radio->v4l2dev.name,
sizeof(capability->driver));
strscpy(capability->card, DRIVER_CARD, sizeof(capability->card));
snprintf(capability->bus_info, sizeof(capability->bus_info),
"platform:%s", radio->v4l2dev.name);
return 0;
}
static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
int err;
struct si476x_radio *radio = video_drvdata(file);
if (band->tuner != 0)
return -EINVAL;
switch (radio->core->chip_id) {
/* AM/FM tuners -- all bands are supported */
case SI476X_CHIP_SI4761:
case SI476X_CHIP_SI4764:
if (band->index < ARRAY_SIZE(si476x_bands)) {
*band = si476x_bands[band->index];
err = 0;
} else {
err = -EINVAL;
}
break;
/* FM companion tuner chips -- only FM bands are
* supported */
case SI476X_CHIP_SI4768:
if (band->index == SI476X_BAND_FM) {
*band = si476x_bands[band->index];
err = 0;
} else {
err = -EINVAL;
}
break;
default:
err = -EINVAL;
}
return err;
}
static int si476x_radio_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *tuner)
{
int err;
struct si476x_rsq_status_report report;
struct si476x_radio *radio = video_drvdata(file);
struct si476x_rsq_status_args args = {
.primary = false,
.rsqack = false,
.attune = false,
.cancel = false,
.stcack = false,
};
if (tuner->index != 0)
return -EINVAL;
tuner->type = V4L2_TUNER_RADIO;
tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
* in multiples of
* 62.5 Hz */
| V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_HWSEEK_BOUNDED
| V4L2_TUNER_CAP_HWSEEK_WRAP
| V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
si476x_core_lock(radio->core);
if (si476x_core_is_a_secondary_tuner(radio->core)) {
strscpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
tuner->rxsubchans = 0;
tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
} else if (si476x_core_has_am(radio->core)) {
if (si476x_core_is_a_primary_tuner(radio->core))
strscpy(tuner->name, "AM/FM (primary)",
sizeof(tuner->name));
else
strscpy(tuner->name, "AM/FM", sizeof(tuner->name));
tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
| V4L2_TUNER_SUB_RDS;
tuner->capability |= V4L2_TUNER_CAP_RDS
| V4L2_TUNER_CAP_RDS_BLOCK_IO
| V4L2_TUNER_CAP_FREQ_BANDS;
tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
} else {
strscpy(tuner->name, "FM", sizeof(tuner->name));
tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
tuner->capability |= V4L2_TUNER_CAP_RDS
| V4L2_TUNER_CAP_RDS_BLOCK_IO
| V4L2_TUNER_CAP_FREQ_BANDS;
tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
}
tuner->audmode = radio->audmode;
tuner->afc = 1;
tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
err = radio->ops->rsq_status(radio->core,
&args, &report);
if (err < 0) {
tuner->signal = 0;
} else {
/*
* tuner->signal value range: 0x0000 .. 0xFFFF,
* report.rssi: -128 .. 127
*/
tuner->signal = (report.rssi + 128) * 257;
}
si476x_core_unlock(radio->core);
return err;
}
static int si476x_radio_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *tuner)
{
struct si476x_radio *radio = video_drvdata(file);
if (tuner->index != 0)
return -EINVAL;
if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
tuner->audmode == V4L2_TUNER_MODE_STEREO)
radio->audmode = tuner->audmode;
else
radio->audmode = V4L2_TUNER_MODE_STEREO;
return 0;
}
static int si476x_radio_init_vtable(struct si476x_radio *radio,
enum si476x_func func)
{
static const struct si476x_radio_ops fm_ops = {
.tune_freq = si476x_core_cmd_fm_tune_freq,
.seek_start = si476x_core_cmd_fm_seek_start,
.rsq_status = si476x_core_cmd_fm_rsq_status,
.rds_blckcnt = si476x_core_cmd_fm_rds_blockcount,
.phase_diversity = si476x_core_cmd_fm_phase_diversity,
.phase_div_status = si476x_core_cmd_fm_phase_div_status,
.acf_status = si476x_core_cmd_fm_acf_status,
.agc_status = si476x_core_cmd_agc_status,
};
static const struct si476x_radio_ops am_ops = {
.tune_freq = si476x_core_cmd_am_tune_freq,
.seek_start = si476x_core_cmd_am_seek_start,
.rsq_status = si476x_core_cmd_am_rsq_status,
.rds_blckcnt = NULL,
.phase_diversity = NULL,
.phase_div_status = NULL,
.acf_status = si476x_core_cmd_am_acf_status,
.agc_status = NULL,
};
switch (func) {
case SI476X_FUNC_FM_RECEIVER:
radio->ops = &fm_ops;
return 0;
case SI476X_FUNC_AM_RECEIVER:
radio->ops = &am_ops;
return 0;
default:
WARN(1, "Unexpected tuner function value\n");
return -EINVAL;
}
}
static int si476x_radio_pretune(struct si476x_radio *radio,
enum si476x_func func)
{
int retval;
struct si476x_tune_freq_args args = {
.zifsr = false,
.hd = false,
.injside = SI476X_INJSIDE_AUTO,
.tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE,
.smoothmetrics = SI476X_SM_INITIALIZE_AUDIO,
.antcap = 0,
};
switch (func) {
case SI476X_FUNC_FM_RECEIVER:
args.freq = v4l2_to_si476x(radio->core,
92 * FREQ_MUL);
retval = radio->ops->tune_freq(radio->core, &args);
break;
case SI476X_FUNC_AM_RECEIVER:
args.freq = v4l2_to_si476x(radio->core,
0.6 * FREQ_MUL);
retval = radio->ops->tune_freq(radio->core, &args);
break;
default:
WARN(1, "Unexpected tuner function value\n");
retval = -EINVAL;
}
return retval;
}
static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
enum si476x_func func)
{
int err;
/* regcache_mark_dirty(radio->core->regmap); */
err = regcache_sync_region(radio->core->regmap,
SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
if (err < 0)
return err;
err = regcache_sync_region(radio->core->regmap,
SI476X_PROP_AUDIO_DEEMPHASIS,
SI476X_PROP_AUDIO_PWR_LINE_FILTER);
if (err < 0)
return err;
err = regcache_sync_region(radio->core->regmap,
SI476X_PROP_INT_CTL_ENABLE,
SI476X_PROP_INT_CTL_ENABLE);
if (err < 0)
return err;
/*
* Is there any point in restoring SNR and the like
* when switching between AM/FM?
*/
err = regcache_sync_region(radio->core->regmap,
SI476X_PROP_VALID_MAX_TUNE_ERROR,
SI476X_PROP_VALID_MAX_TUNE_ERROR);
if (err < 0)
return err;
err = regcache_sync_region(radio->core->regmap,
SI476X_PROP_VALID_SNR_THRESHOLD,
SI476X_PROP_VALID_RSSI_THRESHOLD);
if (err < 0)
return err;
if (func == SI476X_FUNC_FM_RECEIVER) {
if (si476x_core_has_diversity(radio->core)) {
err = si476x_core_cmd_fm_phase_diversity(radio->core,
radio->core->diversity_mode);
if (err < 0)
return err;
}
err = regcache_sync_region(radio->core->regmap,
SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
SI476X_PROP_FM_RDS_CONFIG);
if (err < 0)
return err;
}
return si476x_radio_init_vtable(radio, func);
}
static int si476x_radio_change_func(struct si476x_radio *radio,
enum si476x_func func)
{
int err;
bool soft;
/*
* Since power/up down is a very time consuming operation,
* try to avoid doing it if the requested mode matches the one
* the tuner is in
*/
if (func == radio->core->power_up_parameters.func)
return 0;
soft = true;
err = si476x_core_stop(radio->core, soft);
if (err < 0) {
/*
* OK, if the chip does not want to play nice let's
* try to reset it in more brutal way
*/
soft = false;
err = si476x_core_stop(radio->core, soft);
if (err < 0)
return err;
}
/*
Set the desired radio tuner function
*/
radio->core->power_up_parameters.func = func;
err = si476x_core_start(radio->core, soft);
if (err < 0)
return err;
/*
* No need to do the rest of manipulations for the bootlader
* mode
*/
if (func != SI476X_FUNC_FM_RECEIVER &&
func != SI476X_FUNC_AM_RECEIVER)
return err;
return si476x_radio_do_post_powerup_init(radio, func);
}
static int si476x_radio_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
int err;
struct si476x_radio *radio = video_drvdata(file);
if (f->tuner != 0 ||
f->type != V4L2_TUNER_RADIO)
return -EINVAL;
si476x_core_lock(radio->core);
if (radio->ops->rsq_status) {
struct si476x_rsq_status_report report;
struct si476x_rsq_status_args args = {
.primary = false,
.rsqack = false,
.attune = true,
.cancel = false,
.stcack = false,
};
err = radio->ops->rsq_status(radio->core, &args, &report);
if (!err)
f->frequency = si476x_to_v4l2(radio->core,
report.readfreq);
} else {
err = -EINVAL;
}
si476x_core_unlock(radio->core);
return err;
}
static int si476x_radio_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
int err;
u32 freq = f->frequency;
struct si476x_tune_freq_args args;
struct si476x_radio *radio = video_drvdata(file);
const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
si476x_bands[SI476X_BAND_FM].rangelow) / 2;
const int band = (freq > midrange) ?
SI476X_BAND_FM : SI476X_BAND_AM;
const enum si476x_func func = (band == SI476X_BAND_AM) ?
SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
if (f->tuner != 0 ||
f->type != V4L2_TUNER_RADIO)
return -EINVAL;
si476x_core_lock(radio->core);
freq = clamp(freq,
si476x_bands[band].rangelow,
si476x_bands[band].rangehigh);
if (si476x_radio_freq_is_inside_of_the_band(freq,
SI476X_BAND_AM) &&
(!si476x_core_has_am(radio->core) ||
si476x_core_is_a_secondary_tuner(radio->core))) {
err = -EINVAL;
goto unlock;
}
err = si476x_radio_change_func(radio, func);
if (err < 0)
goto unlock;
args.zifsr = false;
args.hd = false;
args.injside = SI476X_INJSIDE_AUTO;
args.freq = v4l2_to_si476x(radio->core, freq);
args.tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE;
args.smoothmetrics = SI476X_SM_INITIALIZE_AUDIO;
args.antcap = 0;
err = radio->ops->tune_freq(radio->core, &args);
unlock:
si476x_core_unlock(radio->core);
return err;
}
static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
const struct v4l2_hw_freq_seek *seek)
{
int err;
enum si476x_func func;
u32 rangelow = seek->rangelow, rangehigh = seek->rangehigh;
struct si476x_radio *radio = video_drvdata(file);
if (file->f_flags & O_NONBLOCK)
return -EAGAIN;
if (seek->tuner != 0 ||
seek->type != V4L2_TUNER_RADIO)
return -EINVAL;
si476x_core_lock(radio->core);
if (!rangelow) {
err = regmap_read(radio->core->regmap,
SI476X_PROP_SEEK_BAND_BOTTOM,
&rangelow);
if (err)
goto unlock;
rangelow = si476x_to_v4l2(radio->core, rangelow);
}
if (!rangehigh) {
err = regmap_read(radio->core->regmap,
SI476X_PROP_SEEK_BAND_TOP,
&rangehigh);
if (err)
goto unlock;
rangehigh = si476x_to_v4l2(radio->core, rangehigh);
}
if (rangelow > rangehigh) {
err = -EINVAL;
goto unlock;
}
if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
SI476X_BAND_FM)) {
func = SI476X_FUNC_FM_RECEIVER;
} else if (si476x_core_has_am(radio->core) &&
si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
SI476X_BAND_AM)) {
func = SI476X_FUNC_AM_RECEIVER;
} else {
err = -EINVAL;
goto unlock;
}
err = si476x_radio_change_func(radio, func);
if (err < 0)
goto unlock;
if (seek->rangehigh) {
err = regmap_write(radio->core->regmap,
SI476X_PROP_SEEK_BAND_TOP,
v4l2_to_si476x(radio->core,
seek->rangehigh));
if (err)
goto unlock;
}
if (seek->rangelow) {
err = regmap_write(radio->core->regmap,
SI476X_PROP_SEEK_BAND_BOTTOM,
v4l2_to_si476x(radio->core,
seek->rangelow));
if (err)
goto unlock;
}
if (seek->spacing) {
err = regmap_write(radio->core->regmap,
SI476X_PROP_SEEK_FREQUENCY_SPACING,
v4l2_to_si476x(radio->core,
seek->spacing));
if (err)
goto unlock;
}
err = radio->ops->seek_start(radio->core,
seek->seek_upward,
seek->wrap_around);
unlock:
si476x_core_unlock(radio->core);
return err;
}
static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
int retval;
struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
si476x_core_lock(radio->core);
switch (ctrl->id) {
case V4L2_CID_SI476X_INTERCHIP_LINK:
if (si476x_core_has_diversity(radio->core)) {
if (radio->ops->phase_diversity) {
retval = radio->ops->phase_div_status(radio->core);
if (retval < 0)
break;
ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
retval = 0;
break;
} else {
retval = -ENOTTY;
break;
}
}
retval = -EINVAL;
break;
default:
retval = -EINVAL;
break;
}
si476x_core_unlock(radio->core);
return retval;
}
static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
{
int retval;
enum si476x_phase_diversity_mode mode;
struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
si476x_core_lock(radio->core);
switch (ctrl->id) {
case V4L2_CID_SI476X_HARMONICS_COUNT:
retval = regmap_update_bits(radio->core->regmap,
SI476X_PROP_AUDIO_PWR_LINE_FILTER,
SI476X_PROP_PWR_HARMONICS_MASK,
ctrl->val);
break;
case V4L2_CID_POWER_LINE_FREQUENCY:
switch (ctrl->val) {
case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
retval = regmap_update_bits(radio->core->regmap,
SI476X_PROP_AUDIO_PWR_LINE_FILTER,
SI476X_PROP_PWR_ENABLE_MASK,
0);
break;
case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
retval = regmap_update_bits(radio->core->regmap,
SI476X_PROP_AUDIO_PWR_LINE_FILTER,
SI476X_PROP_PWR_GRID_MASK,
SI476X_PROP_PWR_GRID_50HZ);
break;
case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
retval = regmap_update_bits(radio->core->regmap,
SI476X_PROP_AUDIO_PWR_LINE_FILTER,
SI476X_PROP_PWR_GRID_MASK,
SI476X_PROP_PWR_GRID_60HZ);
break;
default:
retval = -EINVAL;
break;
}
break;
case V4L2_CID_SI476X_RSSI_THRESHOLD:
retval = regmap_write(radio->core->regmap,
SI476X_PROP_VALID_RSSI_THRESHOLD,
ctrl->val);
break;
case V4L2_CID_SI476X_SNR_THRESHOLD:
retval = regmap_write(radio->core->regmap,
SI476X_PROP_VALID_SNR_THRESHOLD,
ctrl->val);
break;
case V4L2_CID_SI476X_MAX_TUNE_ERROR:
retval = regmap_write(radio->core->regmap,
SI476X_PROP_VALID_MAX_TUNE_ERROR,
ctrl->val);
break;
case V4L2_CID_RDS_RECEPTION:
/*
* It looks like RDS related properties are
* inaccessible when tuner is in AM mode, so cache the
* changes
*/
if (si476x_core_is_in_am_receiver_mode(radio->core))
regcache_cache_only(radio->core->regmap, true);
if (ctrl->val) {
retval = regmap_write(radio->core->regmap,
SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
radio->core->rds_fifo_depth);
if (retval < 0)
break;
if (radio->core->client->irq) {
retval = regmap_write(radio->core->regmap,
SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
SI476X_RDSRECV);
if (retval < 0)
break;
}
/* Drain RDS FIFO before enabling RDS processing */
retval = si476x_core_cmd_fm_rds_status(radio->core,
false,
true,
true,
NULL);
if (retval < 0)
break;
retval = regmap_update_bits(radio->core->regmap,
SI476X_PROP_FM_RDS_CONFIG,
SI476X_PROP_RDSEN_MASK,
SI476X_PROP_RDSEN);
} else {
retval = regmap_update_bits(radio->core->regmap,
SI476X_PROP_FM_RDS_CONFIG,
SI476X_PROP_RDSEN_MASK,
!SI476X_PROP_RDSEN);
}
if (si476x_core_is_in_am_receiver_mode(radio->core))
regcache_cache_only(radio->core->regmap, false);
break;
case V4L2_CID_TUNE_DEEMPHASIS:
retval = regmap_write(radio->core->regmap,
SI476X_PROP_AUDIO_DEEMPHASIS,
ctrl->val);
break;
case V4L2_CID_SI476X_DIVERSITY_MODE:
mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
if (mode == radio->core->diversity_mode) {
retval = 0;
break;
}
if (si476x_core_is_in_am_receiver_mode(radio->core)) {
/*
* Diversity cannot be configured while tuner
* is in AM mode so save the changes and carry on.
*/
radio->core->diversity_mode = mode;
retval = 0;
} else {
retval = radio->ops->phase_diversity(radio->core, mode);
if (!retval)
radio->core->diversity_mode = mode;
}
break;
default:
retval = -EINVAL;
break;
}
si476x_core_unlock(radio->core);
return retval;
}
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int si476x_radio_g_register(struct file *file, void *fh,
struct v4l2_dbg_register *reg)
{
int err;
unsigned int value;
struct si476x_radio *radio = video_drvdata(file);
si476x_core_lock(radio->core);
reg->size = 2;
err = regmap_read(radio->core->regmap,
(unsigned int)reg->reg, &value);
reg->val = value;
si476x_core_unlock(radio->core);
return err;
}
static int si476x_radio_s_register(struct file *file, void *fh,
const struct v4l2_dbg_register *reg)
{
int err;
struct si476x_radio *radio = video_drvdata(file);
si476x_core_lock(radio->core);
err = regmap_write(radio->core->regmap,
(unsigned int)reg->reg,
(unsigned int)reg->val);
si476x_core_unlock(radio->core);
return err;
}
#endif
static int si476x_radio_fops_open(struct file *file)
{
struct si476x_radio *radio = video_drvdata(file);
int err;
err = v4l2_fh_open(file);
if (err)
return err;
if (v4l2_fh_is_singular_file(file)) {
si476x_core_lock(radio->core);
err = si476x_core_set_power_state(radio->core,
SI476X_POWER_UP_FULL);
if (err < 0)
goto done;
err = si476x_radio_do_post_powerup_init(radio,
radio->core->power_up_parameters.func);
if (err < 0)
goto power_down;
err = si476x_radio_pretune(radio,
radio->core->power_up_parameters.func);
if (err < 0)
goto power_down;
si476x_core_unlock(radio->core);
/*Must be done after si476x_core_unlock to prevent a deadlock*/
v4l2_ctrl_handler_setup(&radio->ctrl_handler);
}
return err;
power_down:
si476x_core_set_power_state(radio->core,
SI476X_POWER_DOWN);
done:
si476x_core_unlock(radio->core);
v4l2_fh_release(file);
return err;
}
static int si476x_radio_fops_release(struct file *file)
{
struct si476x_radio *radio = video_drvdata(file);
if (v4l2_fh_is_singular_file(file) &&
atomic_read(&radio->core->is_alive))
si476x_core_set_power_state(radio->core,
SI476X_POWER_DOWN);
return v4l2_fh_release(file);
}
static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
ssize_t rval;
size_t fifo_len;
unsigned int copied;
struct si476x_radio *radio = video_drvdata(file);
/* block if no new data available */
if (kfifo_is_empty(&radio->core->rds_fifo)) {
if (file->f_flags & O_NONBLOCK)
return -EWOULDBLOCK;
rval = wait_event_interruptible(radio->core->rds_read_queue,
(!kfifo_is_empty(&radio->core->rds_fifo) ||
!atomic_read(&radio->core->is_alive)));
if (rval < 0)
return -EINTR;
if (!atomic_read(&radio->core->is_alive))
return -ENODEV;
}
fifo_len = kfifo_len(&radio->core->rds_fifo);
if (kfifo_to_user(&radio->core->rds_fifo, buf,
min(fifo_len, count),
&copied) != 0) {
dev_warn(&radio->videodev.dev,
"Error during FIFO to userspace copy\n");
rval = -EIO;
} else {
rval = (ssize_t)copied;
}
return rval;
}
static __poll_t si476x_radio_fops_poll(struct file *file,
struct poll_table_struct *pts)
{
struct si476x_radio *radio = video_drvdata(file);
__poll_t req_events = poll_requested_events(pts);
__poll_t err = v4l2_ctrl_poll(file, pts);
if (req_events & (EPOLLIN | EPOLLRDNORM)) {
if (atomic_read(&radio->core->is_alive))
poll_wait(file, &radio->core->rds_read_queue, pts);
if (!atomic_read(&radio->core->is_alive))
err = EPOLLHUP;
if (!kfifo_is_empty(&radio->core->rds_fifo))
err = EPOLLIN | EPOLLRDNORM;
}
return err;
}
static const struct v4l2_file_operations si476x_fops = {
.owner = THIS_MODULE,
.read = si476x_radio_fops_read,
.poll = si476x_radio_fops_poll,
.unlocked_ioctl = video_ioctl2,
.open = si476x_radio_fops_open,
.release = si476x_radio_fops_release,
};
static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
.vidioc_querycap = si476x_radio_querycap,
.vidioc_g_tuner = si476x_radio_g_tuner,
.vidioc_s_tuner = si476x_radio_s_tuner,
.vidioc_g_frequency = si476x_radio_g_frequency,
.vidioc_s_frequency = si476x_radio_s_frequency,
.vidioc_s_hw_freq_seek = si476x_radio_s_hw_freq_seek,
.vidioc_enum_freq_bands = si476x_radio_enum_freq_bands,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
#ifdef CONFIG_VIDEO_ADV_DEBUG
.vidioc_g_register = si476x_radio_g_register,
.vidioc_s_register = si476x_radio_s_register,
#endif
};
static const struct video_device si476x_viddev_template = {
.fops = &si476x_fops,
.name = DRIVER_NAME,
.release = video_device_release_empty,
};
static ssize_t si476x_radio_read_acf_blob(struct file *file,
char __user *user_buf,
size_t count, loff_t *ppos)
{
int err;
struct si476x_radio *radio = file->private_data;
struct si476x_acf_status_report report;
si476x_core_lock(radio->core);
if (radio->ops->acf_status)
err = radio->ops->acf_status(radio->core, &report);
else
err = -ENOENT;
si476x_core_unlock(radio->core);
if (err < 0)
return err;
return simple_read_from_buffer(user_buf, count, ppos, &report,
sizeof(report));
}
static const struct file_operations radio_acf_fops = {
.open = simple_open,
.llseek = default_llseek,
.read = si476x_radio_read_acf_blob,
};
static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
char __user *user_buf,
size_t count, loff_t *ppos)
{
int err;
struct si476x_radio *radio = file->private_data;
struct si476x_rds_blockcount_report report;
si476x_core_lock(radio->core);
if (radio->ops->rds_blckcnt)
err = radio->ops->rds_blckcnt(radio->core, true,
&report);
else
err = -ENOENT;
si476x_core_unlock(radio->core);
if (err < 0)
return err;
return simple_read_from_buffer(user_buf, count, ppos, &report,
sizeof(report));
}
static const struct file_operations radio_rds_blckcnt_fops = {
.open = simple_open,
.llseek = default_llseek,
.read = si476x_radio_read_rds_blckcnt_blob,
};
static ssize_t si476x_radio_read_agc_blob(struct file *file,
char __user *user_buf,
size_t count, loff_t *ppos)
{
int err;
struct si476x_radio *radio = file->private_data;
struct si476x_agc_status_report report;
si476x_core_lock(radio->core);
if (radio->ops->rds_blckcnt)
err = radio->ops->agc_status(radio->core, &report);
else
err = -ENOENT;
si476x_core_unlock(radio->core);
if (err < 0)
return err;
return simple_read_from_buffer(user_buf, count, ppos, &report,
sizeof(report));
}
static const struct file_operations radio_agc_fops = {
.open = simple_open,
.llseek = default_llseek,
.read = si476x_radio_read_agc_blob,
};
static ssize_t si476x_radio_read_rsq_blob(struct file *file,
char __user *user_buf,
size_t count, loff_t *ppos)
{
int err;
struct si476x_radio *radio = file->private_data;
struct si476x_rsq_status_report report;
struct si476x_rsq_status_args args = {
.primary = false,
.rsqack = false,
.attune = false,
.cancel = false,
.stcack = false,
};
si476x_core_lock(radio->core);
if (radio->ops->rds_blckcnt)
err = radio->ops->rsq_status(radio->core, &args, &report);
else
err = -ENOENT;
si476x_core_unlock(radio->core);
if (err < 0)
return err;
return simple_read_from_buffer(user_buf, count, ppos, &report,
sizeof(report));
}
static const struct file_operations radio_rsq_fops = {
.open = simple_open,
.llseek = default_llseek,
.read = si476x_radio_read_rsq_blob,
};
static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
char __user *user_buf,
size_t count, loff_t *ppos)
{
int err;
struct si476x_radio *radio = file->private_data;
struct si476x_rsq_status_report report;
struct si476x_rsq_status_args args = {
.primary = true,
.rsqack = false,
.attune = false,
.cancel = false,
.stcack = false,
};
si476x_core_lock(radio->core);
if (radio->ops->rds_blckcnt)
err = radio->ops->rsq_status(radio->core, &args, &report);
else
err = -ENOENT;
si476x_core_unlock(radio->core);
if (err < 0)
return err;
return simple_read_from_buffer(user_buf, count, ppos, &report,
sizeof(report));
}
static const struct file_operations radio_rsq_primary_fops = {
.open = simple_open,
.llseek = default_llseek,
.read = si476x_radio_read_rsq_primary_blob,
};
static void si476x_radio_init_debugfs(struct si476x_radio *radio)
{
radio->debugfs = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
debugfs_create_file("acf", S_IRUGO, radio->debugfs, radio,
&radio_acf_fops);
debugfs_create_file("rds_blckcnt", S_IRUGO, radio->debugfs, radio,
&radio_rds_blckcnt_fops);
debugfs_create_file("agc", S_IRUGO, radio->debugfs, radio,
&radio_agc_fops);
debugfs_create_file("rsq", S_IRUGO, radio->debugfs, radio,
&radio_rsq_fops);
debugfs_create_file("rsq_primary", S_IRUGO, radio->debugfs, radio,
&radio_rsq_primary_fops);
}
static int si476x_radio_add_new_custom(struct si476x_radio *radio,
enum si476x_ctrl_idx idx)
{
int rval;
struct v4l2_ctrl *ctrl;
ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
&si476x_ctrls[idx],
NULL);
rval = radio->ctrl_handler.error;
if (ctrl == NULL && rval)
dev_err(radio->v4l2dev.dev,
"Could not initialize '%s' control %d\n",
si476x_ctrls[idx].name, rval);
return rval;
}
static int si476x_radio_probe(struct platform_device *pdev)
{
int rval;
struct si476x_radio *radio;
struct v4l2_ctrl *ctrl;
static atomic_t instance = ATOMIC_INIT(0);
radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
if (!radio)
return -ENOMEM;
radio->core = i2c_mfd_cell_to_core(&pdev->dev);
v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
if (rval) {
dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
return rval;
}
memcpy(&radio->videodev, &si476x_viddev_template,
sizeof(struct video_device));
radio->videodev.v4l2_dev = &radio->v4l2dev;
radio->videodev.ioctl_ops = &si4761_ioctl_ops;
radio->videodev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
V4L2_CAP_HW_FREQ_SEEK;
si476x_core_lock(radio->core);
if (!si476x_core_is_a_secondary_tuner(radio->core))
radio->videodev.device_caps |= V4L2_CAP_RDS_CAPTURE |
V4L2_CAP_READWRITE;
si476x_core_unlock(radio->core);
video_set_drvdata(&radio->videodev, radio);
platform_set_drvdata(pdev, radio);
radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
v4l2_ctrl_handler_init(&radio->ctrl_handler,
1 + ARRAY_SIZE(si476x_ctrls));
if (si476x_core_has_am(radio->core)) {
ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
&si476x_ctrl_ops,
V4L2_CID_POWER_LINE_FREQUENCY,
V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
0, 0);
rval = radio->ctrl_handler.error;
if (ctrl == NULL && rval) {
dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
rval);
goto exit;
}
rval = si476x_radio_add_new_custom(radio,
SI476X_IDX_HARMONICS_COUNT);
if (rval < 0)
goto exit;
}
rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
if (rval < 0)
goto exit;
rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
if (rval < 0)
goto exit;
rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
if (rval < 0)
goto exit;
ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
&si476x_ctrl_ops,
V4L2_CID_TUNE_DEEMPHASIS,
V4L2_DEEMPHASIS_75_uS, 0, 0);
rval = radio->ctrl_handler.error;
if (ctrl == NULL && rval) {
dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
rval);
goto exit;
}
ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
V4L2_CID_RDS_RECEPTION,
0, 1, 1, 1);
rval = radio->ctrl_handler.error;
if (ctrl == NULL && rval) {
dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
rval);
goto exit;
}
if (si476x_core_has_diversity(radio->core)) {
si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
rval = si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
if (rval < 0)
goto exit;
rval = si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
if (rval < 0)
goto exit;
}
/* register video device */
rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
if (rval < 0) {
dev_err(&pdev->dev, "Could not register video device\n");
goto exit;
}
si476x_radio_init_debugfs(radio);
return 0;
exit:
v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
return rval;
}
static void si476x_radio_remove(struct platform_device *pdev)
{
struct si476x_radio *radio = platform_get_drvdata(pdev);
v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
video_unregister_device(&radio->videodev);
v4l2_device_unregister(&radio->v4l2dev);
debugfs_remove_recursive(radio->debugfs);
}
MODULE_ALIAS("platform:si476x-radio");
static struct platform_driver si476x_radio_driver = {
.driver = {
.name = DRIVER_NAME,
},
.probe = si476x_radio_probe,
.remove_new = si476x_radio_remove,
};
module_platform_driver(si476x_radio_driver);
MODULE_AUTHOR("Andrey Smirnov <[email protected]>");
MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
MODULE_LICENSE("GPL");
| linux-master | drivers/media/radio/radio-si476x.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* radio-aztech.c - Aztech radio card driver
*
* Converted to the radio-isa framework by Hans Verkuil <[email protected]>
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
* Adapted to support the Video for Linux API by
* Russell Kroll <[email protected]>. Based on original tuner code by:
*
* Quay Ly
* Donald Song
* Jason Lewis ([email protected])
* Scott McGrath ([email protected])
* William McGrath ([email protected])
*
* Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* udelay */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/io.h> /* outb, outb_p */
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include "radio-isa.h"
#include "lm7000.h"
MODULE_AUTHOR("Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
MODULE_DESCRIPTION("A driver for the Aztech radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0");
/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
#ifndef CONFIG_RADIO_AZTECH_PORT
#define CONFIG_RADIO_AZTECH_PORT -1
#endif
#define AZTECH_MAX 2
static int io[AZTECH_MAX] = { [0] = CONFIG_RADIO_AZTECH_PORT,
[1 ... (AZTECH_MAX - 1)] = -1 };
static int radio_nr[AZTECH_MAX] = { [0 ... (AZTECH_MAX - 1)] = -1 };
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "I/O addresses of the Aztech card (0x350 or 0x358)");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
struct aztech {
struct radio_isa_card isa;
int curvol;
};
/* bit definitions for register read */
#define AZTECH_BIT_NOT_TUNED (1 << 0)
#define AZTECH_BIT_MONO (1 << 1)
/* bit definitions for register write */
#define AZTECH_BIT_TUN_CE (1 << 1)
#define AZTECH_BIT_TUN_CLK (1 << 6)
#define AZTECH_BIT_TUN_DATA (1 << 7)
/* bits 0 and 2 are volume control, bits 3..5 are not connected */
static void aztech_set_pins(void *handle, u8 pins)
{
struct radio_isa_card *isa = handle;
struct aztech *az = container_of(isa, struct aztech, isa);
u8 bits = az->curvol;
if (pins & LM7000_DATA)
bits |= AZTECH_BIT_TUN_DATA;
if (pins & LM7000_CLK)
bits |= AZTECH_BIT_TUN_CLK;
if (pins & LM7000_CE)
bits |= AZTECH_BIT_TUN_CE;
outb_p(bits, az->isa.io);
}
static struct radio_isa_card *aztech_alloc(void)
{
struct aztech *az = kzalloc(sizeof(*az), GFP_KERNEL);
return az ? &az->isa : NULL;
}
static int aztech_s_frequency(struct radio_isa_card *isa, u32 freq)
{
lm7000_set_freq(freq, isa, aztech_set_pins);
return 0;
}
static u32 aztech_g_rxsubchans(struct radio_isa_card *isa)
{
if (inb(isa->io) & AZTECH_BIT_MONO)
return V4L2_TUNER_SUB_MONO;
return V4L2_TUNER_SUB_STEREO;
}
static u32 aztech_g_signal(struct radio_isa_card *isa)
{
return (inb(isa->io) & AZTECH_BIT_NOT_TUNED) ? 0 : 0xffff;
}
static int aztech_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
struct aztech *az = container_of(isa, struct aztech, isa);
if (mute)
vol = 0;
az->curvol = (vol & 1) + ((vol & 2) << 1);
outb(az->curvol, isa->io);
return 0;
}
static const struct radio_isa_ops aztech_ops = {
.alloc = aztech_alloc,
.s_mute_volume = aztech_s_mute_volume,
.s_frequency = aztech_s_frequency,
.g_rxsubchans = aztech_g_rxsubchans,
.g_signal = aztech_g_signal,
};
static const int aztech_ioports[] = { 0x350, 0x358 };
static struct radio_isa_driver aztech_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-aztech",
},
},
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = aztech_ioports,
.num_of_io_ports = ARRAY_SIZE(aztech_ioports),
.region_size = 8,
.card = "Aztech Radio",
.ops = &aztech_ops,
.has_stereo = true,
.max_volume = 3,
};
static int __init aztech_init(void)
{
return isa_register_driver(&aztech_driver.driver, AZTECH_MAX);
}
static void __exit aztech_exit(void)
{
isa_unregister_driver(&aztech_driver.driver);
}
module_init(aztech_init);
module_exit(aztech_exit);
| linux-master | drivers/media/radio/radio-aztech.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* RadioTrack II driver
* Copyright 1998 Ben Pfaff
*
* Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
* Converted to new API by Alan Cox <[email protected]>
* Various bugfixes and enhancements by Russell Kroll <[email protected]>
*
* Converted to the radio-isa framework by Hans Verkuil <[email protected]>
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
*
* Fully tested with actual hardware and the v4l2-compliance tool.
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* udelay */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/mutex.h>
#include <linux/io.h> /* outb, outb_p */
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include "radio-isa.h"
MODULE_AUTHOR("Ben Pfaff");
MODULE_DESCRIPTION("A driver for the RadioTrack II radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1.99");
#ifndef CONFIG_RADIO_RTRACK2_PORT
#define CONFIG_RADIO_RTRACK2_PORT -1
#endif
#define RTRACK2_MAX 2
static int io[RTRACK2_MAX] = { [0] = CONFIG_RADIO_RTRACK2_PORT,
[1 ... (RTRACK2_MAX - 1)] = -1 };
static int radio_nr[RTRACK2_MAX] = { [0 ... (RTRACK2_MAX - 1)] = -1 };
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "I/O addresses of the RadioTrack card (0x20f or 0x30f)");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
static struct radio_isa_card *rtrack2_alloc(void)
{
return kzalloc(sizeof(struct radio_isa_card), GFP_KERNEL);
}
static void zero(struct radio_isa_card *isa)
{
outb_p(1, isa->io);
outb_p(3, isa->io);
outb_p(1, isa->io);
}
static void one(struct radio_isa_card *isa)
{
outb_p(5, isa->io);
outb_p(7, isa->io);
outb_p(5, isa->io);
}
static int rtrack2_s_frequency(struct radio_isa_card *isa, u32 freq)
{
int i;
freq = freq / 200 + 856;
outb_p(0xc8, isa->io);
outb_p(0xc9, isa->io);
outb_p(0xc9, isa->io);
for (i = 0; i < 10; i++)
zero(isa);
for (i = 14; i >= 0; i--)
if (freq & (1 << i))
one(isa);
else
zero(isa);
outb_p(0xc8, isa->io);
outb_p(v4l2_ctrl_g_ctrl(isa->mute), isa->io);
return 0;
}
static u32 rtrack2_g_signal(struct radio_isa_card *isa)
{
/* bit set = no signal present */
return (inb(isa->io) & 2) ? 0 : 0xffff;
}
static int rtrack2_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
outb(mute, isa->io);
return 0;
}
static const struct radio_isa_ops rtrack2_ops = {
.alloc = rtrack2_alloc,
.s_mute_volume = rtrack2_s_mute_volume,
.s_frequency = rtrack2_s_frequency,
.g_signal = rtrack2_g_signal,
};
static const int rtrack2_ioports[] = { 0x20f, 0x30f };
static struct radio_isa_driver rtrack2_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-rtrack2",
},
},
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = rtrack2_ioports,
.num_of_io_ports = ARRAY_SIZE(rtrack2_ioports),
.region_size = 4,
.card = "AIMSlab RadioTrack II",
.ops = &rtrack2_ops,
.has_stereo = true,
};
static int __init rtrack2_init(void)
{
return isa_register_driver(&rtrack2_driver.driver, RTRACK2_MAX);
}
static void __exit rtrack2_exit(void)
{
isa_unregister_driver(&rtrack2_driver.driver);
}
module_init(rtrack2_init);
module_exit(rtrack2_exit);
| linux-master | drivers/media/radio/radio-rtrack2.c |
// SPDX-License-Identifier: GPL-2.0-only
/* SF16-FMI, SF16-FMP and SF16-FMD radio driver for Linux radio support
* heavily based on rtrack driver...
* (c) 1997 M. Kirkwood
* (c) 1998 Petr Vandrovec, [email protected]
*
* Fitted to new interface by Alan Cox <[email protected]>
* Made working and cleaned up functions <[email protected]>
* Support for ISAPnP by Ladislav Michl <[email protected]>
*
* Notes on the hardware
*
* Frequency control is done digitally -- ie out(port,encodefreq(95.8));
* No volume control - only mute/unmute - you have to use line volume
* control on SB-part of SF16-FMI/SF16-FMP/SF16-FMD
*
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
*/
#include <linux/kernel.h> /* __setup */
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* udelay */
#include <linux/isapnp.h>
#include <linux/mutex.h>
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/io.h> /* outb, outb_p */
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include "lm7000.h"
MODULE_AUTHOR("Petr Vandrovec, [email protected] and M. Kirkwood");
MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.0.3");
static int io = -1;
static int radio_nr = -1;
module_param(io, int, 0);
MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
module_param(radio_nr, int, 0);
struct fmi
{
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler hdl;
struct video_device vdev;
int io;
bool mute;
u32 curfreq; /* freq in kHz */
struct mutex lock;
};
static struct fmi fmi_card;
static struct pnp_dev *dev;
static bool pnp_attached;
#define RSF16_MINFREQ (87U * 16000)
#define RSF16_MAXFREQ (108U * 16000)
#define FMI_BIT_TUN_CE (1 << 0)
#define FMI_BIT_TUN_CLK (1 << 1)
#define FMI_BIT_TUN_DATA (1 << 2)
#define FMI_BIT_VOL_SW (1 << 3)
#define FMI_BIT_TUN_STRQ (1 << 4)
static void fmi_set_pins(void *handle, u8 pins)
{
struct fmi *fmi = handle;
u8 bits = FMI_BIT_TUN_STRQ;
if (!fmi->mute)
bits |= FMI_BIT_VOL_SW;
if (pins & LM7000_DATA)
bits |= FMI_BIT_TUN_DATA;
if (pins & LM7000_CLK)
bits |= FMI_BIT_TUN_CLK;
if (pins & LM7000_CE)
bits |= FMI_BIT_TUN_CE;
mutex_lock(&fmi->lock);
outb_p(bits, fmi->io);
mutex_unlock(&fmi->lock);
}
static inline void fmi_mute(struct fmi *fmi)
{
mutex_lock(&fmi->lock);
outb(0x00, fmi->io);
mutex_unlock(&fmi->lock);
}
static inline void fmi_unmute(struct fmi *fmi)
{
mutex_lock(&fmi->lock);
outb(0x08, fmi->io);
mutex_unlock(&fmi->lock);
}
static inline int fmi_getsigstr(struct fmi *fmi)
{
int val;
int res;
mutex_lock(&fmi->lock);
val = fmi->mute ? 0x00 : 0x08; /* mute/unmute */
outb(val, fmi->io);
outb(val | 0x10, fmi->io);
msleep(143); /* was schedule_timeout(HZ/7) */
res = (int)inb(fmi->io + 1);
outb(val, fmi->io);
mutex_unlock(&fmi->lock);
return (res & 2) ? 0 : 0xFFFF;
}
static void fmi_set_freq(struct fmi *fmi)
{
fmi->curfreq = clamp(fmi->curfreq, RSF16_MINFREQ, RSF16_MAXFREQ);
/* rounding in steps of 800 to match the freq
that will be used */
lm7000_set_freq((fmi->curfreq / 800) * 800, fmi, fmi_set_pins);
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
strscpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
strscpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card));
strscpy(v->bus_info, "ISA:radio-sf16fmi", sizeof(v->bus_info));
return 0;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct fmi *fmi = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = RSF16_MINFREQ;
v->rangehigh = RSF16_MAXFREQ;
v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
v->audmode = V4L2_TUNER_MODE_STEREO;
v->signal = fmi_getsigstr(fmi);
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
return v->index ? -EINVAL : 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct fmi *fmi = video_drvdata(file);
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
fmi->curfreq = f->frequency;
fmi_set_freq(fmi);
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct fmi *fmi = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = fmi->curfreq;
return 0;
}
static int fmi_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
if (ctrl->val)
fmi_mute(fmi);
else
fmi_unmute(fmi);
fmi->mute = ctrl->val;
return 0;
}
return -EINVAL;
}
static const struct v4l2_ctrl_ops fmi_ctrl_ops = {
.s_ctrl = fmi_s_ctrl,
};
static const struct v4l2_file_operations fmi_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops fmi_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_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
/* ladis: this is my card. does any other types exist? */
static struct isapnp_device_id id_table[] = {
/* SF16-FMI */
{ ISAPNP_ANY_ID, ISAPNP_ANY_ID,
ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
/* SF16-FMD */
{ ISAPNP_ANY_ID, ISAPNP_ANY_ID,
ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
{ ISAPNP_CARD_END, },
};
MODULE_DEVICE_TABLE(isapnp, id_table);
static int __init isapnp_fmi_probe(void)
{
int i = 0;
while (id_table[i].card_vendor != 0 && dev == NULL) {
dev = pnp_find_dev(NULL, id_table[i].vendor,
id_table[i].function, NULL);
i++;
}
if (!dev)
return -ENODEV;
if (pnp_device_attach(dev) < 0)
return -EAGAIN;
if (pnp_activate_dev(dev) < 0) {
printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
pnp_device_detach(dev);
return -ENOMEM;
}
if (!pnp_port_valid(dev, 0)) {
pnp_device_detach(dev);
return -ENODEV;
}
i = pnp_port_start(dev, 0);
printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
return i;
}
static int __init fmi_init(void)
{
struct fmi *fmi = &fmi_card;
struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
struct v4l2_ctrl_handler *hdl = &fmi->hdl;
int res, i;
static const int probe_ports[] = { 0, 0x284, 0x384 };
if (io < 0) {
for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
io = probe_ports[i];
if (io == 0) {
io = isapnp_fmi_probe();
if (io < 0)
continue;
pnp_attached = true;
}
if (!request_region(io, 2, "radio-sf16fmi")) {
if (pnp_attached)
pnp_device_detach(dev);
io = -1;
continue;
}
if (pnp_attached ||
((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
break;
release_region(io, 2);
io = -1;
}
} else {
if (!request_region(io, 2, "radio-sf16fmi")) {
printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
return -EBUSY;
}
if (inb(io) == 0xff) {
printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
release_region(io, 2);
return -ENODEV;
}
}
if (io < 0) {
printk(KERN_ERR "radio-sf16fmi: no cards found\n");
return -ENODEV;
}
strscpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
fmi->io = io;
res = v4l2_device_register(NULL, v4l2_dev);
if (res < 0) {
release_region(fmi->io, 2);
if (pnp_attached)
pnp_device_detach(dev);
v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
return res;
}
v4l2_ctrl_handler_init(hdl, 1);
v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
v4l2_dev->ctrl_handler = hdl;
if (hdl->error) {
res = hdl->error;
v4l2_err(v4l2_dev, "Could not register controls\n");
v4l2_ctrl_handler_free(hdl);
v4l2_device_unregister(v4l2_dev);
return res;
}
strscpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
fmi->vdev.v4l2_dev = v4l2_dev;
fmi->vdev.fops = &fmi_fops;
fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
fmi->vdev.release = video_device_release_empty;
fmi->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
video_set_drvdata(&fmi->vdev, fmi);
mutex_init(&fmi->lock);
/* mute card and set default frequency */
fmi->mute = true;
fmi->curfreq = RSF16_MINFREQ;
fmi_set_freq(fmi);
if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
v4l2_ctrl_handler_free(hdl);
v4l2_device_unregister(v4l2_dev);
release_region(fmi->io, 2);
if (pnp_attached)
pnp_device_detach(dev);
return -EINVAL;
}
v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
return 0;
}
static void __exit fmi_exit(void)
{
struct fmi *fmi = &fmi_card;
v4l2_ctrl_handler_free(&fmi->hdl);
video_unregister_device(&fmi->vdev);
v4l2_device_unregister(&fmi->v4l2_dev);
release_region(fmi->io, 2);
if (dev && pnp_attached)
pnp_device_detach(dev);
}
module_init(fmi_init);
module_exit(fmi_exit);
| linux-master | drivers/media/radio/radio-sf16fmi.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
* (C) 2001 Dimitromanolakis Apostolos <[email protected]>
*
* Based in the radio Maestro PCI driver. Actually it uses the same chip
* for radio but different pci controller.
*
* I didn't have any specs I reversed engineered the protocol from
* the windows driver (radio.dll).
*
* The card uses the TEA5757 chip that includes a search function but it
* is useless as I haven't found any way to read back the frequency. If
* anybody does please mail me.
*
* For the pdf file see:
* http://www.nxp.com/acrobat_download2/expired_datasheets/TEA5757_5759_3.pdf
*
*
* CHANGES:
* 0.75b
* - better pci interface thanks to Francois Romieu <[email protected]>
*
* 0.75 Sun Feb 4 22:51:27 EET 2001
* - tiding up
* - removed support for multiple devices as it didn't work anyway
*
* BUGS:
* - card unmutes if you change frequency
*
* (c) 2006, 2007 by Mauro Carvalho Chehab <[email protected]>:
* - Conversion to V4L2 API
* - Uses video_ioctl2 for parsing and to add debug support
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/videodev2.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <media/drv-intf/tea575x.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
MODULE_AUTHOR("Dimitromanolakis Apostolos, [email protected]");
MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000.");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0");
static int radio_nr = -1;
module_param(radio_nr, int, 0644);
MODULE_PARM_DESC(radio_nr, "Radio device number");
/* TEA5757 pin mappings */
static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16;
static atomic_t maxiradio_instance = ATOMIC_INIT(0);
#define PCI_VENDOR_ID_GUILLEMOT 0x5046
#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
struct maxiradio
{
struct snd_tea575x tea;
struct v4l2_device v4l2_dev;
struct pci_dev *pdev;
u16 io; /* base of radio io */
};
static inline struct maxiradio *to_maxiradio(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct maxiradio, v4l2_dev);
}
static void maxiradio_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
{
struct maxiradio *dev = tea->private_data;
u8 bits = 0;
bits |= (pins & TEA575X_DATA) ? data : 0;
bits |= (pins & TEA575X_CLK) ? clk : 0;
bits |= (pins & TEA575X_WREN) ? wren : 0;
bits |= power;
outb(bits, dev->io);
}
/* Note: this card cannot read out the data of the shift registers,
only the mono/stereo pin works. */
static u8 maxiradio_tea575x_get_pins(struct snd_tea575x *tea)
{
struct maxiradio *dev = tea->private_data;
u8 bits = inb(dev->io);
return ((bits & data) ? TEA575X_DATA : 0) |
((bits & mo_st) ? TEA575X_MOST : 0);
}
static void maxiradio_tea575x_set_direction(struct snd_tea575x *tea, bool output)
{
}
static const struct snd_tea575x_ops maxiradio_tea_ops = {
.set_pins = maxiradio_tea575x_set_pins,
.get_pins = maxiradio_tea575x_get_pins,
.set_direction = maxiradio_tea575x_set_direction,
};
static int maxiradio_probe(struct pci_dev *pdev,
const struct pci_device_id *ent)
{
struct maxiradio *dev;
struct v4l2_device *v4l2_dev;
int retval = -ENOMEM;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (dev == NULL) {
dev_err(&pdev->dev, "not enough memory\n");
return -ENOMEM;
}
v4l2_dev = &dev->v4l2_dev;
v4l2_device_set_name(v4l2_dev, "maxiradio", &maxiradio_instance);
retval = v4l2_device_register(&pdev->dev, v4l2_dev);
if (retval < 0) {
v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
goto errfr;
}
dev->tea.private_data = dev;
dev->tea.ops = &maxiradio_tea_ops;
/* The data pin cannot be read. This may be a hardware limitation, or
we just don't know how to read it. */
dev->tea.cannot_read_data = true;
dev->tea.v4l2_dev = v4l2_dev;
dev->tea.radio_nr = radio_nr;
strscpy(dev->tea.card, "Maxi Radio FM2000", sizeof(dev->tea.card));
retval = -ENODEV;
if (!request_region(pci_resource_start(pdev, 0),
pci_resource_len(pdev, 0), v4l2_dev->name)) {
dev_err(&pdev->dev, "can't reserve I/O ports\n");
goto err_hdl;
}
if (pci_enable_device(pdev))
goto err_out_free_region;
dev->io = pci_resource_start(pdev, 0);
if (snd_tea575x_init(&dev->tea, THIS_MODULE)) {
printk(KERN_ERR "radio-maxiradio: Unable to detect TEA575x tuner\n");
goto err_out_free_region;
}
return 0;
err_out_free_region:
release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
err_hdl:
v4l2_device_unregister(v4l2_dev);
errfr:
kfree(dev);
return retval;
}
static void maxiradio_remove(struct pci_dev *pdev)
{
struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
struct maxiradio *dev = to_maxiradio(v4l2_dev);
snd_tea575x_exit(&dev->tea);
/* Turn off power */
outb(0, dev->io);
v4l2_device_unregister(v4l2_dev);
release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
kfree(dev);
}
static const struct pci_device_id maxiradio_pci_tbl[] = {
{ PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
PCI_ANY_ID, PCI_ANY_ID, },
{ 0 }
};
MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
static struct pci_driver maxiradio_driver = {
.name = "radio-maxiradio",
.id_table = maxiradio_pci_tbl,
.probe = maxiradio_probe,
.remove = maxiradio_remove,
};
module_pci_driver(maxiradio_driver);
| linux-master | drivers/media/radio/radio-maxiradio.c |
// SPDX-License-Identifier: GPL-2.0-only
/* Terratec ActiveRadio ISA Standalone card driver for Linux radio support
* (c) 1999 R. Offermanns ([email protected])
* based on the aimslab radio driver from M. Kirkwood
* many thanks to Michael Becker and Friedhelm Birth (from TerraTec)
*
*
* History:
* 1999-05-21 First preview release
*
* Notes on the hardware:
* There are two "main" chips on the card:
* - Philips OM5610 (http://www-us.semiconductors.philips.com/acrobat/datasheets/OM5610_2.pdf)
* - Philips SAA6588 (http://www-us.semiconductors.philips.com/acrobat/datasheets/SAA6588_1.pdf)
* (you can get the datasheet at the above links)
*
* Frequency control is done digitally -- ie out(port,encodefreq(95.8));
* Volume Control is done digitally
*
* Converted to the radio-isa framework by Hans Verkuil <[email protected]>
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/mutex.h>
#include <linux/io.h> /* outb, outb_p */
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include "radio-isa.h"
MODULE_AUTHOR("R. Offermans & others");
MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1.99");
/* Note: there seems to be only one possible port (0x590), but without
hardware this is hard to verify. For now, this is the only one we will
support. */
static int io = 0x590;
static int radio_nr = -1;
module_param(radio_nr, int, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device number");
#define WRT_DIS 0x00
#define CLK_OFF 0x00
#define IIC_DATA 0x01
#define IIC_CLK 0x02
#define DATA 0x04
#define CLK_ON 0x08
#define WRT_EN 0x10
static struct radio_isa_card *terratec_alloc(void)
{
return kzalloc(sizeof(struct radio_isa_card), GFP_KERNEL);
}
static int terratec_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
int i;
if (mute)
vol = 0;
vol = vol + (vol * 32); /* change both channels */
for (i = 0; i < 8; i++) {
if (vol & (0x80 >> i))
outb(0x80, isa->io + 1);
else
outb(0x00, isa->io + 1);
}
return 0;
}
/* this is the worst part in this driver */
/* many more or less strange things are going on here, but hey, it works :) */
static int terratec_s_frequency(struct radio_isa_card *isa, u32 freq)
{
int i;
int temp;
long rest;
unsigned char buffer[25]; /* we have to bit shift 25 registers */
freq = freq / 160; /* convert the freq. to a nice to handle value */
memset(buffer, 0, sizeof(buffer));
rest = freq * 10 + 10700; /* I once had understood what is going on here */
/* maybe some wise guy (friedhelm?) can comment this stuff */
i = 13;
temp = 102400;
while (rest != 0) {
if (rest % temp == rest)
buffer[i] = 0;
else {
buffer[i] = 1;
rest = rest - temp;
}
i--;
temp = temp / 2;
}
for (i = 24; i > -1; i--) { /* bit shift the values to the radiocard */
if (buffer[i] == 1) {
outb(WRT_EN | DATA, isa->io);
outb(WRT_EN | DATA | CLK_ON, isa->io);
outb(WRT_EN | DATA, isa->io);
} else {
outb(WRT_EN | 0x00, isa->io);
outb(WRT_EN | 0x00 | CLK_ON, isa->io);
}
}
outb(0x00, isa->io);
return 0;
}
static u32 terratec_g_signal(struct radio_isa_card *isa)
{
/* bit set = no signal present */
return (inb(isa->io) & 2) ? 0 : 0xffff;
}
static const struct radio_isa_ops terratec_ops = {
.alloc = terratec_alloc,
.s_mute_volume = terratec_s_mute_volume,
.s_frequency = terratec_s_frequency,
.g_signal = terratec_g_signal,
};
static const int terratec_ioports[] = { 0x590 };
static struct radio_isa_driver terratec_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-terratec",
},
},
.io_params = &io,
.radio_nr_params = &radio_nr,
.io_ports = terratec_ioports,
.num_of_io_ports = ARRAY_SIZE(terratec_ioports),
.region_size = 2,
.card = "TerraTec ActiveRadio",
.ops = &terratec_ops,
.has_stereo = true,
.max_volume = 10,
};
static int __init terratec_init(void)
{
return isa_register_driver(&terratec_driver.driver, 1);
}
static void __exit terratec_exit(void)
{
isa_unregister_driver(&terratec_driver.driver);
}
module_init(terratec_init);
module_exit(terratec_exit);
| linux-master | drivers/media/radio/radio-terratec.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Framework for ISA radio drivers.
* This takes care of all the V4L2 scaffolding, allowing the ISA drivers
* to concentrate on the actual hardware operation.
*
* Copyright (C) 2012 Hans Verkuil <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/videodev2.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include "radio-isa.h"
MODULE_AUTHOR("Hans Verkuil");
MODULE_DESCRIPTION("A framework for ISA radio drivers.");
MODULE_LICENSE("GPL");
#define FREQ_LOW (87U * 16000U)
#define FREQ_HIGH (108U * 16000U)
static int radio_isa_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct radio_isa_card *isa = video_drvdata(file);
strscpy(v->driver, isa->drv->driver.driver.name, sizeof(v->driver));
strscpy(v->card, isa->drv->card, sizeof(v->card));
snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", isa->v4l2_dev.name);
return 0;
}
static int radio_isa_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct radio_isa_card *isa = video_drvdata(file);
const struct radio_isa_ops *ops = isa->drv->ops;
if (v->index > 0)
return -EINVAL;
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = FREQ_LOW;
v->rangehigh = FREQ_HIGH;
v->capability = V4L2_TUNER_CAP_LOW;
if (isa->drv->has_stereo)
v->capability |= V4L2_TUNER_CAP_STEREO;
if (ops->g_rxsubchans)
v->rxsubchans = ops->g_rxsubchans(isa);
else
v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
v->audmode = isa->stereo ? V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
if (ops->g_signal)
v->signal = ops->g_signal(isa);
else
v->signal = (v->rxsubchans & V4L2_TUNER_SUB_STEREO) ?
0xffff : 0;
return 0;
}
static int radio_isa_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct radio_isa_card *isa = video_drvdata(file);
const struct radio_isa_ops *ops = isa->drv->ops;
if (v->index)
return -EINVAL;
if (ops->s_stereo) {
isa->stereo = (v->audmode == V4L2_TUNER_MODE_STEREO);
return ops->s_stereo(isa, isa->stereo);
}
return 0;
}
static int radio_isa_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct radio_isa_card *isa = video_drvdata(file);
u32 freq = f->frequency;
int res;
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
freq = clamp(freq, FREQ_LOW, FREQ_HIGH);
res = isa->drv->ops->s_frequency(isa, freq);
if (res == 0)
isa->freq = freq;
return res;
}
static int radio_isa_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct radio_isa_card *isa = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = isa->freq;
return 0;
}
static int radio_isa_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct radio_isa_card *isa =
container_of(ctrl->handler, struct radio_isa_card, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
return isa->drv->ops->s_mute_volume(isa, ctrl->val,
isa->volume ? isa->volume->val : 0);
}
return -EINVAL;
}
static int radio_isa_log_status(struct file *file, void *priv)
{
struct radio_isa_card *isa = video_drvdata(file);
v4l2_info(&isa->v4l2_dev, "I/O Port = 0x%03x\n", isa->io);
v4l2_ctrl_handler_log_status(&isa->hdl, isa->v4l2_dev.name);
return 0;
}
static const struct v4l2_ctrl_ops radio_isa_ctrl_ops = {
.s_ctrl = radio_isa_s_ctrl,
};
static const struct v4l2_file_operations radio_isa_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops radio_isa_ioctl_ops = {
.vidioc_querycap = radio_isa_querycap,
.vidioc_g_tuner = radio_isa_g_tuner,
.vidioc_s_tuner = radio_isa_s_tuner,
.vidioc_g_frequency = radio_isa_g_frequency,
.vidioc_s_frequency = radio_isa_s_frequency,
.vidioc_log_status = radio_isa_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
int radio_isa_match(struct device *pdev, unsigned int dev)
{
struct radio_isa_driver *drv = pdev->platform_data;
return drv->probe || drv->io_params[dev] >= 0;
}
EXPORT_SYMBOL_GPL(radio_isa_match);
static bool radio_isa_valid_io(const struct radio_isa_driver *drv, int io)
{
int i;
for (i = 0; i < drv->num_of_io_ports; i++)
if (drv->io_ports[i] == io)
return true;
return false;
}
static struct radio_isa_card *radio_isa_alloc(struct radio_isa_driver *drv,
struct device *pdev)
{
struct v4l2_device *v4l2_dev;
struct radio_isa_card *isa = drv->ops->alloc();
if (!isa)
return NULL;
dev_set_drvdata(pdev, isa);
isa->drv = drv;
v4l2_dev = &isa->v4l2_dev;
strscpy(v4l2_dev->name, dev_name(pdev), sizeof(v4l2_dev->name));
return isa;
}
static int radio_isa_common_probe(struct radio_isa_card *isa,
struct device *pdev,
int radio_nr, unsigned region_size)
{
const struct radio_isa_driver *drv = isa->drv;
const struct radio_isa_ops *ops = drv->ops;
struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
int res;
if (!request_region(isa->io, region_size, v4l2_dev->name)) {
v4l2_err(v4l2_dev, "port 0x%x already in use\n", isa->io);
kfree(isa);
return -EBUSY;
}
res = v4l2_device_register(pdev, v4l2_dev);
if (res < 0) {
v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
goto err_dev_reg;
}
v4l2_ctrl_handler_init(&isa->hdl, 1);
isa->mute = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
if (drv->max_volume)
isa->volume = v4l2_ctrl_new_std(&isa->hdl, &radio_isa_ctrl_ops,
V4L2_CID_AUDIO_VOLUME, 0, drv->max_volume, 1,
drv->max_volume);
v4l2_dev->ctrl_handler = &isa->hdl;
if (isa->hdl.error) {
res = isa->hdl.error;
v4l2_err(v4l2_dev, "Could not register controls\n");
goto err_hdl;
}
if (drv->max_volume)
v4l2_ctrl_cluster(2, &isa->mute);
v4l2_dev->ctrl_handler = &isa->hdl;
mutex_init(&isa->lock);
isa->vdev.lock = &isa->lock;
strscpy(isa->vdev.name, v4l2_dev->name, sizeof(isa->vdev.name));
isa->vdev.v4l2_dev = v4l2_dev;
isa->vdev.fops = &radio_isa_fops;
isa->vdev.ioctl_ops = &radio_isa_ioctl_ops;
isa->vdev.release = video_device_release_empty;
isa->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
video_set_drvdata(&isa->vdev, isa);
isa->freq = FREQ_LOW;
isa->stereo = drv->has_stereo;
if (ops->init)
res = ops->init(isa);
if (!res)
res = v4l2_ctrl_handler_setup(&isa->hdl);
if (!res)
res = ops->s_frequency(isa, isa->freq);
if (!res && ops->s_stereo)
res = ops->s_stereo(isa, isa->stereo);
if (res < 0) {
v4l2_err(v4l2_dev, "Could not setup card\n");
goto err_hdl;
}
res = video_register_device(&isa->vdev, VFL_TYPE_RADIO, radio_nr);
if (res < 0) {
v4l2_err(v4l2_dev, "Could not register device node\n");
goto err_hdl;
}
v4l2_info(v4l2_dev, "Initialized radio card %s on port 0x%03x\n",
drv->card, isa->io);
return 0;
err_hdl:
v4l2_ctrl_handler_free(&isa->hdl);
err_dev_reg:
release_region(isa->io, region_size);
kfree(isa);
return res;
}
static void radio_isa_common_remove(struct radio_isa_card *isa,
unsigned region_size)
{
const struct radio_isa_ops *ops = isa->drv->ops;
ops->s_mute_volume(isa, true, isa->volume ? isa->volume->cur.val : 0);
video_unregister_device(&isa->vdev);
v4l2_ctrl_handler_free(&isa->hdl);
v4l2_device_unregister(&isa->v4l2_dev);
release_region(isa->io, region_size);
v4l2_info(&isa->v4l2_dev, "Removed radio card %s\n", isa->drv->card);
kfree(isa);
}
int radio_isa_probe(struct device *pdev, unsigned int dev)
{
struct radio_isa_driver *drv = pdev->platform_data;
const struct radio_isa_ops *ops = drv->ops;
struct v4l2_device *v4l2_dev;
struct radio_isa_card *isa;
isa = radio_isa_alloc(drv, pdev);
if (!isa)
return -ENOMEM;
isa->io = drv->io_params[dev];
v4l2_dev = &isa->v4l2_dev;
if (drv->probe && ops->probe) {
int i;
for (i = 0; i < drv->num_of_io_ports; ++i) {
int io = drv->io_ports[i];
if (request_region(io, drv->region_size, v4l2_dev->name)) {
bool found = ops->probe(isa, io);
release_region(io, drv->region_size);
if (found) {
isa->io = io;
break;
}
}
}
}
if (!radio_isa_valid_io(drv, isa->io)) {
int i;
if (isa->io < 0)
return -ENODEV;
v4l2_err(v4l2_dev, "you must set an I/O address with io=0x%03x",
drv->io_ports[0]);
for (i = 1; i < drv->num_of_io_ports; i++)
printk(KERN_CONT "/0x%03x", drv->io_ports[i]);
printk(KERN_CONT ".\n");
kfree(isa);
return -EINVAL;
}
return radio_isa_common_probe(isa, pdev, drv->radio_nr_params[dev],
drv->region_size);
}
EXPORT_SYMBOL_GPL(radio_isa_probe);
void radio_isa_remove(struct device *pdev, unsigned int dev)
{
struct radio_isa_card *isa = dev_get_drvdata(pdev);
radio_isa_common_remove(isa, isa->drv->region_size);
}
EXPORT_SYMBOL_GPL(radio_isa_remove);
#ifdef CONFIG_PNP
int radio_isa_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
{
struct pnp_driver *pnp_drv = to_pnp_driver(dev->dev.driver);
struct radio_isa_driver *drv = container_of(pnp_drv,
struct radio_isa_driver, pnp_driver);
struct radio_isa_card *isa;
if (!pnp_port_valid(dev, 0))
return -ENODEV;
isa = radio_isa_alloc(drv, &dev->dev);
if (!isa)
return -ENOMEM;
isa->io = pnp_port_start(dev, 0);
return radio_isa_common_probe(isa, &dev->dev, drv->radio_nr_params[0],
pnp_port_len(dev, 0));
}
EXPORT_SYMBOL_GPL(radio_isa_pnp_probe);
void radio_isa_pnp_remove(struct pnp_dev *dev)
{
struct radio_isa_card *isa = dev_get_drvdata(&dev->dev);
radio_isa_common_remove(isa, pnp_port_len(dev, 0));
}
EXPORT_SYMBOL_GPL(radio_isa_pnp_remove);
#endif
| linux-master | drivers/media/radio/radio-isa.c |
// SPDX-License-Identifier: GPL-2.0-only
/* radio-trust.c - Trust FM Radio card driver for Linux 2.2
* by Eric Lammerts <[email protected]>
*
* Based on radio-aztech.c. Original notes:
*
* Adapted to support the Video for Linux API by
* Russell Kroll <[email protected]>. Based on original tuner code by:
*
* Quay Ly
* Donald Song
* Jason Lewis ([email protected])
* Scott McGrath ([email protected])
* William McGrath ([email protected])
*
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/videodev2.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include "radio-isa.h"
MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1.99");
/* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
#ifndef CONFIG_RADIO_TRUST_PORT
#define CONFIG_RADIO_TRUST_PORT -1
#endif
#define TRUST_MAX 2
static int io[TRUST_MAX] = { [0] = CONFIG_RADIO_TRUST_PORT,
[1 ... (TRUST_MAX - 1)] = -1 };
static int radio_nr[TRUST_MAX] = { [0 ... (TRUST_MAX - 1)] = -1 };
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "I/O addresses of the Trust FM Radio card (0x350 or 0x358)");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
struct trust {
struct radio_isa_card isa;
int ioval;
};
static struct radio_isa_card *trust_alloc(void)
{
struct trust *tr = kzalloc(sizeof(*tr), GFP_KERNEL);
return tr ? &tr->isa : NULL;
}
/* i2c addresses */
#define TDA7318_ADDR 0x88
#define TSA6060T_ADDR 0xc4
#define TR_DELAY do { inb(tr->isa.io); inb(tr->isa.io); inb(tr->isa.io); } while (0)
#define TR_SET_SCL outb(tr->ioval |= 2, tr->isa.io)
#define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->isa.io)
#define TR_SET_SDA outb(tr->ioval |= 1, tr->isa.io)
#define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->isa.io)
static void write_i2c(struct trust *tr, int n, ...)
{
unsigned char val, mask;
va_list args;
va_start(args, n);
/* start condition */
TR_SET_SDA;
TR_SET_SCL;
TR_DELAY;
TR_CLR_SDA;
TR_CLR_SCL;
TR_DELAY;
for (; n; n--) {
val = va_arg(args, unsigned);
for (mask = 0x80; mask; mask >>= 1) {
if (val & mask)
TR_SET_SDA;
else
TR_CLR_SDA;
TR_SET_SCL;
TR_DELAY;
TR_CLR_SCL;
TR_DELAY;
}
/* acknowledge bit */
TR_SET_SDA;
TR_SET_SCL;
TR_DELAY;
TR_CLR_SCL;
TR_DELAY;
}
/* stop condition */
TR_CLR_SDA;
TR_DELAY;
TR_SET_SCL;
TR_DELAY;
TR_SET_SDA;
TR_DELAY;
va_end(args);
}
static int trust_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
struct trust *tr = container_of(isa, struct trust, isa);
tr->ioval = (tr->ioval & 0xf7) | (mute << 3);
outb(tr->ioval, isa->io);
write_i2c(tr, 2, TDA7318_ADDR, vol ^ 0x1f);
return 0;
}
static int trust_s_stereo(struct radio_isa_card *isa, bool stereo)
{
struct trust *tr = container_of(isa, struct trust, isa);
tr->ioval = (tr->ioval & 0xfb) | (!stereo << 2);
outb(tr->ioval, isa->io);
return 0;
}
static u32 trust_g_signal(struct radio_isa_card *isa)
{
int i, v;
for (i = 0, v = 0; i < 100; i++)
v |= inb(isa->io);
return (v & 1) ? 0 : 0xffff;
}
static int trust_s_frequency(struct radio_isa_card *isa, u32 freq)
{
struct trust *tr = container_of(isa, struct trust, isa);
freq /= 160; /* Convert to 10 kHz units */
freq += 1070; /* Add 10.7 MHz IF */
write_i2c(tr, 5, TSA6060T_ADDR, (freq << 1) | 1,
freq >> 7, 0x60 | ((freq >> 15) & 1), 0);
return 0;
}
static int basstreble2chip[15] = {
0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
};
static int trust_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct radio_isa_card *isa =
container_of(ctrl->handler, struct radio_isa_card, hdl);
struct trust *tr = container_of(isa, struct trust, isa);
switch (ctrl->id) {
case V4L2_CID_AUDIO_BASS:
write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[ctrl->val]);
return 0;
case V4L2_CID_AUDIO_TREBLE:
write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[ctrl->val]);
return 0;
}
return -EINVAL;
}
static const struct v4l2_ctrl_ops trust_ctrl_ops = {
.s_ctrl = trust_s_ctrl,
};
static int trust_initialize(struct radio_isa_card *isa)
{
struct trust *tr = container_of(isa, struct trust, isa);
tr->ioval = 0xf;
write_i2c(tr, 2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */
write_i2c(tr, 2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */
write_i2c(tr, 2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */
write_i2c(tr, 2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */
write_i2c(tr, 2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */
v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops,
V4L2_CID_AUDIO_BASS, 0, 15, 1, 8);
v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops,
V4L2_CID_AUDIO_TREBLE, 0, 15, 1, 8);
return isa->hdl.error;
}
static const struct radio_isa_ops trust_ops = {
.init = trust_initialize,
.alloc = trust_alloc,
.s_mute_volume = trust_s_mute_volume,
.s_frequency = trust_s_frequency,
.s_stereo = trust_s_stereo,
.g_signal = trust_g_signal,
};
static const int trust_ioports[] = { 0x350, 0x358 };
static struct radio_isa_driver trust_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-trust",
},
},
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = trust_ioports,
.num_of_io_ports = ARRAY_SIZE(trust_ioports),
.region_size = 2,
.card = "Trust FM Radio",
.ops = &trust_ops,
.has_stereo = true,
.max_volume = 31,
};
static int __init trust_init(void)
{
return isa_register_driver(&trust_driver.driver, TRUST_MAX);
}
static void __exit trust_exit(void)
{
isa_unregister_driver(&trust_driver.driver);
}
module_init(trust_init);
module_exit(trust_exit);
| linux-master | drivers/media/radio/radio-trust.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for the MasterKit MA901 USB FM radio. This device plugs
* into the USB port and an analog audio input or headphones, so this thing
* only deals with initialization, frequency setting, volume.
*
* Copyright (c) 2012 Alexey Klimov <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#define DRIVER_AUTHOR "Alexey Klimov <[email protected]>"
#define DRIVER_DESC "Masterkit MA901 USB FM radio driver"
#define DRIVER_VERSION "0.0.1"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);
#define USB_MA901_VENDOR 0x16c0
#define USB_MA901_PRODUCT 0x05df
/* dev_warn macro with driver name */
#define MA901_DRIVER_NAME "radio-ma901"
#define ma901radio_dev_warn(dev, fmt, arg...) \
dev_warn(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
#define ma901radio_dev_err(dev, fmt, arg...) \
dev_err(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
/* Probably USB_TIMEOUT should be modified in module parameter */
#define BUFFER_LENGTH 8
#define USB_TIMEOUT 500
#define FREQ_MIN 87.5
#define FREQ_MAX 108.0
#define FREQ_MUL 16000
#define MA901_VOLUME_MAX 16
#define MA901_VOLUME_MIN 0
/* Commands that device should understand
* List isn't full and will be updated with implementation of new functions
*/
#define MA901_RADIO_SET_FREQ 0x03
#define MA901_RADIO_SET_VOLUME 0x04
#define MA901_RADIO_SET_MONO_STEREO 0x05
/* Comfortable defines for ma901radio_set_stereo */
#define MA901_WANT_STEREO 0x50
#define MA901_WANT_MONO 0xd0
/* module parameter */
static int radio_nr = -1;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "Radio file number");
/* Data for one (physical) device */
struct ma901radio_device {
/* reference to USB and video device */
struct usb_device *usbdev;
struct usb_interface *intf;
struct video_device vdev;
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler hdl;
u8 *buffer;
struct mutex lock; /* buffer locking */
int curfreq;
u16 volume;
int stereo;
bool muted;
};
static inline struct ma901radio_device *to_ma901radio_dev(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct ma901radio_device, v4l2_dev);
}
/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
static int ma901radio_set_freq(struct ma901radio_device *radio, int freq)
{
unsigned int freq_send = 0x300 + (freq >> 5) / 25;
int retval;
radio->buffer[0] = 0x0a;
radio->buffer[1] = MA901_RADIO_SET_FREQ;
radio->buffer[2] = ((freq_send >> 8) & 0xff) + 0x80;
radio->buffer[3] = freq_send & 0xff;
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x0300, 0,
radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (retval < 0)
return retval;
radio->curfreq = freq;
return 0;
}
static int ma901radio_set_volume(struct ma901radio_device *radio, u16 vol_to_set)
{
int retval;
radio->buffer[0] = 0x0a;
radio->buffer[1] = MA901_RADIO_SET_VOLUME;
radio->buffer[2] = 0xc2;
radio->buffer[3] = vol_to_set + 0x20;
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x0300, 0,
radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (retval < 0)
return retval;
radio->volume = vol_to_set;
return retval;
}
static int ma901_set_stereo(struct ma901radio_device *radio, u8 stereo)
{
int retval;
radio->buffer[0] = 0x0a;
radio->buffer[1] = MA901_RADIO_SET_MONO_STEREO;
radio->buffer[2] = stereo;
radio->buffer[3] = 0x00;
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x0300, 0,
radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (retval < 0)
return retval;
if (stereo == MA901_WANT_STEREO)
radio->stereo = V4L2_TUNER_MODE_STEREO;
else
radio->stereo = V4L2_TUNER_MODE_MONO;
return retval;
}
/* Handle unplugging the device.
* We call video_unregister_device in any case.
* The last function called in this procedure is
* usb_ma901radio_device_release.
*/
static void usb_ma901radio_disconnect(struct usb_interface *intf)
{
struct ma901radio_device *radio = to_ma901radio_dev(usb_get_intfdata(intf));
mutex_lock(&radio->lock);
video_unregister_device(&radio->vdev);
usb_set_intfdata(intf, NULL);
v4l2_device_disconnect(&radio->v4l2_dev);
mutex_unlock(&radio->lock);
v4l2_device_put(&radio->v4l2_dev);
}
/* vidioc_querycap - query device capabilities */
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct ma901radio_device *radio = video_drvdata(file);
strscpy(v->driver, "radio-ma901", sizeof(v->driver));
strscpy(v->card, "Masterkit MA901 USB FM Radio", sizeof(v->card));
usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
return 0;
}
/* vidioc_g_tuner - get tuner attributes */
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct ma901radio_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
v->signal = 0;
/* TODO: the same words like in _probe() goes here.
* When receiving of stats will be implemented then we can call
* ma901radio_get_stat().
* retval = ma901radio_get_stat(radio, &is_stereo, &v->signal);
*/
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = FREQ_MIN * FREQ_MUL;
v->rangehigh = FREQ_MAX * FREQ_MUL;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
/* v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; */
v->audmode = radio->stereo ?
V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
return 0;
}
/* vidioc_s_tuner - set tuner attributes */
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct ma901radio_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
/* mono/stereo selector */
switch (v->audmode) {
case V4L2_TUNER_MODE_MONO:
return ma901_set_stereo(radio, MA901_WANT_MONO);
default:
return ma901_set_stereo(radio, MA901_WANT_STEREO);
}
}
/* vidioc_s_frequency - set tuner radio frequency */
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct ma901radio_device *radio = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
return ma901radio_set_freq(radio, clamp_t(unsigned, f->frequency,
FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
}
/* vidioc_g_frequency - get tuner radio frequency */
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct ma901radio_device *radio = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->frequency = radio->curfreq;
return 0;
}
static int usb_ma901radio_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct ma901radio_device *radio =
container_of(ctrl->handler, struct ma901radio_device, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_VOLUME: /* set volume */
return ma901radio_set_volume(radio, (u16)ctrl->val);
}
return -EINVAL;
}
/* TODO: Should we really need to implement suspend and resume functions?
* Radio has it's own memory and will continue playing if power is present
* on usb port and on resume it will start to play again based on freq, volume
* values in device memory.
*/
static int usb_ma901radio_suspend(struct usb_interface *intf, pm_message_t message)
{
return 0;
}
static int usb_ma901radio_resume(struct usb_interface *intf)
{
return 0;
}
static const struct v4l2_ctrl_ops usb_ma901radio_ctrl_ops = {
.s_ctrl = usb_ma901radio_s_ctrl,
};
/* File system interface */
static const struct v4l2_file_operations usb_ma901radio_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops usb_ma901radio_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_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static void usb_ma901radio_release(struct v4l2_device *v4l2_dev)
{
struct ma901radio_device *radio = to_ma901radio_dev(v4l2_dev);
v4l2_ctrl_handler_free(&radio->hdl);
v4l2_device_unregister(&radio->v4l2_dev);
kfree(radio->buffer);
kfree(radio);
}
/* check if the device is present and register with v4l and usb if it is */
static int usb_ma901radio_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(intf);
struct ma901radio_device *radio;
int retval = 0;
/* Masterkit MA901 usb radio has the same USB ID as many others
* Atmel V-USB devices. Let's make additional checks to be sure
* that this is our device.
*/
if (dev->product && dev->manufacturer &&
(strncmp(dev->product, "MA901", 5) != 0
|| strncmp(dev->manufacturer, "www.masterkit.ru", 16) != 0))
return -ENODEV;
radio = kzalloc(sizeof(struct ma901radio_device), GFP_KERNEL);
if (!radio) {
dev_err(&intf->dev, "kzalloc for ma901radio_device failed\n");
retval = -ENOMEM;
goto err;
}
radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
if (!radio->buffer) {
dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
retval = -ENOMEM;
goto err_nobuf;
}
retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
if (retval < 0) {
dev_err(&intf->dev, "couldn't register v4l2_device\n");
goto err_v4l2;
}
v4l2_ctrl_handler_init(&radio->hdl, 1);
/* TODO:It looks like this radio doesn't have mute/unmute control
* and windows program just emulate it using volume control.
* Let's plan to do the same in this driver.
*
* v4l2_ctrl_new_std(&radio->hdl, &usb_ma901radio_ctrl_ops,
* V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
*/
v4l2_ctrl_new_std(&radio->hdl, &usb_ma901radio_ctrl_ops,
V4L2_CID_AUDIO_VOLUME, MA901_VOLUME_MIN,
MA901_VOLUME_MAX, 1, MA901_VOLUME_MAX);
if (radio->hdl.error) {
retval = radio->hdl.error;
dev_err(&intf->dev, "couldn't register control\n");
goto err_ctrl;
}
mutex_init(&radio->lock);
radio->v4l2_dev.ctrl_handler = &radio->hdl;
radio->v4l2_dev.release = usb_ma901radio_release;
strscpy(radio->vdev.name, radio->v4l2_dev.name,
sizeof(radio->vdev.name));
radio->vdev.v4l2_dev = &radio->v4l2_dev;
radio->vdev.fops = &usb_ma901radio_fops;
radio->vdev.ioctl_ops = &usb_ma901radio_ioctl_ops;
radio->vdev.release = video_device_release_empty;
radio->vdev.lock = &radio->lock;
radio->vdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
radio->usbdev = interface_to_usbdev(intf);
radio->intf = intf;
usb_set_intfdata(intf, &radio->v4l2_dev);
radio->curfreq = 95.21 * FREQ_MUL;
video_set_drvdata(&radio->vdev, radio);
/* TODO: we can get some statistics (freq, volume) from device
* but it's not implemented yet. After insertion in usb-port radio
* setups frequency and starts playing without any initialization.
* So we don't call usb_ma901radio_init/get_stat() here.
* retval = usb_ma901radio_init(radio);
*/
retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
radio_nr);
if (retval < 0) {
dev_err(&intf->dev, "could not register video device\n");
goto err_vdev;
}
return 0;
err_vdev:
v4l2_ctrl_handler_free(&radio->hdl);
err_ctrl:
v4l2_device_unregister(&radio->v4l2_dev);
err_v4l2:
kfree(radio->buffer);
err_nobuf:
kfree(radio);
err:
return retval;
}
/* USB Device ID List */
static const struct usb_device_id usb_ma901radio_device_table[] = {
{ USB_DEVICE_AND_INTERFACE_INFO(USB_MA901_VENDOR, USB_MA901_PRODUCT,
USB_CLASS_HID, 0, 0) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_ma901radio_device_table);
/* USB subsystem interface */
static struct usb_driver usb_ma901radio_driver = {
.name = MA901_DRIVER_NAME,
.probe = usb_ma901radio_probe,
.disconnect = usb_ma901radio_disconnect,
.suspend = usb_ma901radio_suspend,
.resume = usb_ma901radio_resume,
.reset_resume = usb_ma901radio_resume,
.id_table = usb_ma901radio_device_table,
};
module_usb_driver(usb_ma901radio_driver);
| linux-master | drivers/media/radio/radio-ma901.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* saa7706.c Philips SAA7706H Car Radio DSP driver
* Copyright (c) 2009 Intel Corporation
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#define DRIVER_NAME "saa7706h"
/* the I2C memory map looks like this
$1C00 - $FFFF Not Used
$2200 - $3FFF Reserved YRAM (DSP2) space
$2000 - $21FF YRAM (DSP2)
$1FF0 - $1FFF Hardware Registers
$1280 - $1FEF Reserved XRAM (DSP2) space
$1000 - $127F XRAM (DSP2)
$0FFF DSP CONTROL
$0A00 - $0FFE Reserved
$0980 - $09FF Reserved YRAM (DSP1) space
$0800 - $097F YRAM (DSP1)
$0200 - $07FF Not Used
$0180 - $01FF Reserved XRAM (DSP1) space
$0000 - $017F XRAM (DSP1)
*/
#define SAA7706H_REG_CTRL 0x0fff
#define SAA7706H_CTRL_BYP_PLL 0x0001
#define SAA7706H_CTRL_PLL_DIV_MASK 0x003e
#define SAA7706H_CTRL_PLL3_62975MHZ 0x003e
#define SAA7706H_CTRL_DSP_TURBO 0x0040
#define SAA7706H_CTRL_PC_RESET_DSP1 0x0080
#define SAA7706H_CTRL_PC_RESET_DSP2 0x0100
#define SAA7706H_CTRL_DSP1_ROM_EN_MASK 0x0600
#define SAA7706H_CTRL_DSP1_FUNC_PROM 0x0000
#define SAA7706H_CTRL_DSP2_ROM_EN_MASK 0x1800
#define SAA7706H_CTRL_DSP2_FUNC_PROM 0x0000
#define SAA7706H_CTRL_DIG_SIL_INTERPOL 0x8000
#define SAA7706H_REG_EVALUATION 0x1ff0
#define SAA7706H_EVAL_DISABLE_CHARGE_PUMP 0x000001
#define SAA7706H_EVAL_DCS_CLOCK 0x000002
#define SAA7706H_EVAL_GNDRC1_ENABLE 0x000004
#define SAA7706H_EVAL_GNDRC2_ENABLE 0x000008
#define SAA7706H_REG_CL_GEN1 0x1ff3
#define SAA7706H_CL_GEN1_MIN_LOOPGAIN_MASK 0x00000f
#define SAA7706H_CL_GEN1_LOOPGAIN_MASK 0x0000f0
#define SAA7706H_CL_GEN1_COARSE_RATION 0xffff00
#define SAA7706H_REG_CL_GEN2 0x1ff4
#define SAA7706H_CL_GEN2_WSEDGE_FALLING 0x000001
#define SAA7706H_CL_GEN2_STOP_VCO 0x000002
#define SAA7706H_CL_GEN2_FRERUN 0x000004
#define SAA7706H_CL_GEN2_ADAPTIVE 0x000008
#define SAA7706H_CL_GEN2_FINE_RATIO_MASK 0x0ffff0
#define SAA7706H_REG_CL_GEN4 0x1ff6
#define SAA7706H_CL_GEN4_BYPASS_PLL1 0x001000
#define SAA7706H_CL_GEN4_PLL1_DIV_MASK 0x03e000
#define SAA7706H_CL_GEN4_DSP1_TURBO 0x040000
#define SAA7706H_REG_SEL 0x1ff7
#define SAA7706H_SEL_DSP2_SRCA_MASK 0x000007
#define SAA7706H_SEL_DSP2_FMTA_MASK 0x000031
#define SAA7706H_SEL_DSP2_SRCB_MASK 0x0001c0
#define SAA7706H_SEL_DSP2_FMTB_MASK 0x000e00
#define SAA7706H_SEL_DSP1_SRC_MASK 0x003000
#define SAA7706H_SEL_DSP1_FMT_MASK 0x01c003
#define SAA7706H_SEL_SPDIF2 0x020000
#define SAA7706H_SEL_HOST_IO_FMT_MASK 0x1c0000
#define SAA7706H_SEL_EN_HOST_IO 0x200000
#define SAA7706H_REG_IAC 0x1ff8
#define SAA7706H_REG_CLK_SET 0x1ff9
#define SAA7706H_REG_CLK_COEFF 0x1ffa
#define SAA7706H_REG_INPUT_SENS 0x1ffb
#define SAA7706H_INPUT_SENS_RDS_VOL_MASK 0x0003f
#define SAA7706H_INPUT_SENS_FM_VOL_MASK 0x00fc0
#define SAA7706H_INPUT_SENS_FM_MPX 0x01000
#define SAA7706H_INPUT_SENS_OFF_FILTER_A_EN 0x02000
#define SAA7706H_INPUT_SENS_OFF_FILTER_B_EN 0x04000
#define SAA7706H_REG_PHONE_NAV_AUDIO 0x1ffc
#define SAA7706H_REG_IO_CONF_DSP2 0x1ffd
#define SAA7706H_REG_STATUS_DSP2 0x1ffe
#define SAA7706H_REG_PC_DSP2 0x1fff
#define SAA7706H_DSP1_MOD0 0x0800
#define SAA7706H_DSP1_ROM_VER 0x097f
#define SAA7706H_DSP2_MPTR0 0x1000
#define SAA7706H_DSP1_MODPNTR 0x0000
#define SAA7706H_DSP2_XMEM_CONTLLCW 0x113e
#define SAA7706H_DSP2_XMEM_BUSAMP 0x114a
#define SAA7706H_DSP2_XMEM_FDACPNTR 0x11f9
#define SAA7706H_DSP2_XMEM_IIS1PNTR 0x11fb
#define SAA7706H_DSP2_YMEM_PVGA 0x212a
#define SAA7706H_DSP2_YMEM_PVAT1 0x212b
#define SAA7706H_DSP2_YMEM_PVAT 0x212c
#define SAA7706H_DSP2_YMEM_ROM_VER 0x21ff
#define SUPPORTED_DSP1_ROM_VER 0x667
struct saa7706h_state {
struct v4l2_subdev sd;
struct v4l2_ctrl_handler hdl;
unsigned muted;
};
static inline struct saa7706h_state *to_state(struct v4l2_subdev *sd)
{
return container_of(sd, struct saa7706h_state, sd);
}
static int saa7706h_i2c_send(struct i2c_client *client, const u8 *data, int len)
{
int err = i2c_master_send(client, data, len);
if (err == len)
return 0;
return err > 0 ? -EIO : err;
}
static int saa7706h_i2c_transfer(struct i2c_client *client,
struct i2c_msg *msgs, int num)
{
int err = i2c_transfer(client->adapter, msgs, num);
if (err == num)
return 0;
return err > 0 ? -EIO : err;
}
static int saa7706h_set_reg24(struct v4l2_subdev *sd, u16 reg, u32 val)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
u8 buf[5];
int pos = 0;
buf[pos++] = reg >> 8;
buf[pos++] = reg;
buf[pos++] = val >> 16;
buf[pos++] = val >> 8;
buf[pos++] = val;
return saa7706h_i2c_send(client, buf, pos);
}
static int saa7706h_set_reg24_err(struct v4l2_subdev *sd, u16 reg, u32 val,
int *err)
{
return *err ? *err : saa7706h_set_reg24(sd, reg, val);
}
static int saa7706h_set_reg16(struct v4l2_subdev *sd, u16 reg, u16 val)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
u8 buf[4];
int pos = 0;
buf[pos++] = reg >> 8;
buf[pos++] = reg;
buf[pos++] = val >> 8;
buf[pos++] = val;
return saa7706h_i2c_send(client, buf, pos);
}
static int saa7706h_set_reg16_err(struct v4l2_subdev *sd, u16 reg, u16 val,
int *err)
{
return *err ? *err : saa7706h_set_reg16(sd, reg, val);
}
static int saa7706h_get_reg16(struct v4l2_subdev *sd, u16 reg)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
u8 buf[2];
int err;
u8 regaddr[] = {reg >> 8, reg};
struct i2c_msg msg[] = {
{
.addr = client->addr,
.len = sizeof(regaddr),
.buf = regaddr
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = sizeof(buf),
.buf = buf
}
};
err = saa7706h_i2c_transfer(client, msg, ARRAY_SIZE(msg));
if (err)
return err;
return buf[0] << 8 | buf[1];
}
static int saa7706h_unmute(struct v4l2_subdev *sd)
{
struct saa7706h_state *state = to_state(sd);
int err = 0;
err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
SAA7706H_CTRL_PC_RESET_DSP2, &err);
/* newer versions of the chip requires a small sleep after reset */
msleep(1);
err = saa7706h_set_reg16_err(sd, SAA7706H_REG_CTRL,
SAA7706H_CTRL_PLL3_62975MHZ, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_EVALUATION, 0, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN1, 0x040022, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN2,
SAA7706H_CL_GEN2_WSEDGE_FALLING, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CL_GEN4, 0x024080, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_SEL, 0x200080, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IAC, 0xf4caed, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_SET, 0x124334, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_CLK_COEFF, 0x004a1a,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_INPUT_SENS, 0x0071c7,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PHONE_NAV_AUDIO,
0x0e22ff, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_IO_CONF_DSP2, 0x001ff8,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_STATUS_DSP2, 0x080003,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_REG_PC_DSP2, 0x000004, &err);
err = saa7706h_set_reg16_err(sd, SAA7706H_DSP1_MOD0, 0x0c6c, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_MPTR0, 0x000b4b, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x000600, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP1_MODPNTR, 0x0000c0, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000819,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x00085a,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_BUSAMP, 0x7fffff,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_FDACPNTR, 0x2000cb,
&err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_IIS1PNTR, 0x2000cb,
&err);
err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVGA, 0x0f80, &err);
err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT1, 0x0800,
&err);
err = saa7706h_set_reg16_err(sd, SAA7706H_DSP2_YMEM_PVAT, 0x0800, &err);
err = saa7706h_set_reg24_err(sd, SAA7706H_DSP2_XMEM_CONTLLCW, 0x000905,
&err);
if (!err)
state->muted = 0;
return err;
}
static int saa7706h_mute(struct v4l2_subdev *sd)
{
struct saa7706h_state *state = to_state(sd);
int err;
err = saa7706h_set_reg16(sd, SAA7706H_REG_CTRL,
SAA7706H_CTRL_PLL3_62975MHZ | SAA7706H_CTRL_PC_RESET_DSP1 |
SAA7706H_CTRL_PC_RESET_DSP2);
if (!err)
state->muted = 1;
return err;
}
static int saa7706h_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct saa7706h_state *state =
container_of(ctrl->handler, struct saa7706h_state, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
if (ctrl->val)
return saa7706h_mute(&state->sd);
return saa7706h_unmute(&state->sd);
}
return -EINVAL;
}
static const struct v4l2_ctrl_ops saa7706h_ctrl_ops = {
.s_ctrl = saa7706h_s_ctrl,
};
static const struct v4l2_subdev_ops empty_ops = {};
/*
* Generic i2c probe
* concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
*/
static int saa7706h_probe(struct i2c_client *client)
{
struct saa7706h_state *state;
struct v4l2_subdev *sd;
int err;
/* Check if the adapter supports the needed features */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
return -EIO;
v4l_info(client, "chip found @ 0x%02x (%s)\n",
client->addr << 1, client->adapter->name);
state = kzalloc(sizeof(struct saa7706h_state), GFP_KERNEL);
if (state == NULL)
return -ENOMEM;
sd = &state->sd;
v4l2_i2c_subdev_init(sd, client, &empty_ops);
v4l2_ctrl_handler_init(&state->hdl, 4);
v4l2_ctrl_new_std(&state->hdl, &saa7706h_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
sd->ctrl_handler = &state->hdl;
err = state->hdl.error;
if (err)
goto err;
/* check the rom versions */
err = saa7706h_get_reg16(sd, SAA7706H_DSP1_ROM_VER);
if (err < 0)
goto err;
if (err != SUPPORTED_DSP1_ROM_VER)
v4l2_warn(sd, "Unknown DSP1 ROM code version: 0x%x\n", err);
state->muted = 1;
/* startup in a muted state */
err = saa7706h_mute(sd);
if (err)
goto err;
return 0;
err:
v4l2_device_unregister_subdev(sd);
v4l2_ctrl_handler_free(&state->hdl);
kfree(to_state(sd));
printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", err);
return err;
}
static void saa7706h_remove(struct i2c_client *client)
{
struct v4l2_subdev *sd = i2c_get_clientdata(client);
struct saa7706h_state *state = to_state(sd);
saa7706h_mute(sd);
v4l2_device_unregister_subdev(sd);
v4l2_ctrl_handler_free(&state->hdl);
kfree(to_state(sd));
}
static const struct i2c_device_id saa7706h_id[] = {
{DRIVER_NAME, 0},
{},
};
MODULE_DEVICE_TABLE(i2c, saa7706h_id);
static struct i2c_driver saa7706h_driver = {
.driver = {
.name = DRIVER_NAME,
},
.probe = saa7706h_probe,
.remove = saa7706h_remove,
.id_table = saa7706h_id,
};
module_i2c_driver(saa7706h_driver);
MODULE_DESCRIPTION("SAA7706H Car Radio DSP driver");
MODULE_AUTHOR("Mocean Laboratories");
MODULE_LICENSE("GPL v2");
| linux-master | drivers/media/radio/saa7706h.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* radio-timb.c Timberdale FPGA Radio driver
* Copyright (c) 2009 Intel Corporation
*/
#include <linux/io.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/platform_data/media/timb_radio.h>
#define DRIVER_NAME "timb-radio"
struct timbradio {
struct timb_radio_platform_data pdata;
struct v4l2_subdev *sd_tuner;
struct v4l2_subdev *sd_dsp;
struct video_device video_dev;
struct v4l2_device v4l2_dev;
struct mutex lock;
};
static int timbradio_vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
strscpy(v->driver, DRIVER_NAME, sizeof(v->driver));
strscpy(v->card, "Timberdale Radio", sizeof(v->card));
snprintf(v->bus_info, sizeof(v->bus_info), "platform:"DRIVER_NAME);
return 0;
}
static int timbradio_vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct timbradio *tr = video_drvdata(file);
return v4l2_subdev_call(tr->sd_tuner, tuner, g_tuner, v);
}
static int timbradio_vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct timbradio *tr = video_drvdata(file);
return v4l2_subdev_call(tr->sd_tuner, tuner, s_tuner, v);
}
static int timbradio_vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct timbradio *tr = video_drvdata(file);
return v4l2_subdev_call(tr->sd_tuner, tuner, s_frequency, f);
}
static int timbradio_vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct timbradio *tr = video_drvdata(file);
return v4l2_subdev_call(tr->sd_tuner, tuner, g_frequency, f);
}
static const struct v4l2_ioctl_ops timbradio_ioctl_ops = {
.vidioc_querycap = timbradio_vidioc_querycap,
.vidioc_g_tuner = timbradio_vidioc_g_tuner,
.vidioc_s_tuner = timbradio_vidioc_s_tuner,
.vidioc_g_frequency = timbradio_vidioc_g_frequency,
.vidioc_s_frequency = timbradio_vidioc_s_frequency,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct v4l2_file_operations timbradio_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static int timbradio_probe(struct platform_device *pdev)
{
struct timb_radio_platform_data *pdata = pdev->dev.platform_data;
struct timbradio *tr;
int err;
if (!pdata) {
dev_err(&pdev->dev, "Platform data missing\n");
err = -EINVAL;
goto err;
}
tr = devm_kzalloc(&pdev->dev, sizeof(*tr), GFP_KERNEL);
if (!tr) {
err = -ENOMEM;
goto err;
}
tr->pdata = *pdata;
mutex_init(&tr->lock);
strscpy(tr->video_dev.name, "Timberdale Radio",
sizeof(tr->video_dev.name));
tr->video_dev.fops = &timbradio_fops;
tr->video_dev.ioctl_ops = &timbradio_ioctl_ops;
tr->video_dev.release = video_device_release_empty;
tr->video_dev.minor = -1;
tr->video_dev.lock = &tr->lock;
tr->video_dev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
strscpy(tr->v4l2_dev.name, DRIVER_NAME, sizeof(tr->v4l2_dev.name));
err = v4l2_device_register(NULL, &tr->v4l2_dev);
if (err)
goto err;
tr->video_dev.v4l2_dev = &tr->v4l2_dev;
tr->sd_tuner = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
i2c_get_adapter(pdata->i2c_adapter), pdata->tuner, NULL);
tr->sd_dsp = v4l2_i2c_new_subdev_board(&tr->v4l2_dev,
i2c_get_adapter(pdata->i2c_adapter), pdata->dsp, NULL);
if (tr->sd_tuner == NULL || tr->sd_dsp == NULL) {
err = -ENODEV;
goto err_video_req;
}
tr->v4l2_dev.ctrl_handler = tr->sd_dsp->ctrl_handler;
err = video_register_device(&tr->video_dev, VFL_TYPE_RADIO, -1);
if (err) {
dev_err(&pdev->dev, "Error reg video\n");
goto err_video_req;
}
video_set_drvdata(&tr->video_dev, tr);
platform_set_drvdata(pdev, tr);
return 0;
err_video_req:
v4l2_device_unregister(&tr->v4l2_dev);
err:
dev_err(&pdev->dev, "Failed to register: %d\n", err);
return err;
}
static void timbradio_remove(struct platform_device *pdev)
{
struct timbradio *tr = platform_get_drvdata(pdev);
video_unregister_device(&tr->video_dev);
v4l2_device_unregister(&tr->v4l2_dev);
}
static struct platform_driver timbradio_platform_driver = {
.driver = {
.name = DRIVER_NAME,
},
.probe = timbradio_probe,
.remove_new = timbradio_remove,
};
module_platform_driver(timbradio_platform_driver);
MODULE_DESCRIPTION("Timberdale Radio driver");
MODULE_AUTHOR("Mocean Laboratories <[email protected]>");
MODULE_LICENSE("GPL v2");
MODULE_VERSION("0.0.2");
MODULE_ALIAS("platform:"DRIVER_NAME);
| linux-master | drivers/media/radio/radio-timb.c |
/*
* Linux V4L2 radio driver for the Griffin radioSHARK2 USB radio receiver
*
* Note the radioSHARK2 offers the audio through a regular USB audio device,
* this driver only handles the tuning.
*
* The info necessary to drive the shark2 was taken from the small userspace
* shark2.c program by Hisaaki Shibata, which he kindly placed in the Public
* Domain.
*
* Copyright (c) 2012 Hans de Goede <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/workqueue.h>
#include <media/v4l2-device.h>
#include "radio-tea5777.h"
#if defined(CONFIG_LEDS_CLASS) || \
(defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK2_MODULE))
#define SHARK_USE_LEDS 1
#endif
MODULE_AUTHOR("Hans de Goede <[email protected]>");
MODULE_DESCRIPTION("Griffin radioSHARK2, USB radio receiver driver");
MODULE_LICENSE("GPL");
static int debug;
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Debug level (0-1)");
#define SHARK_IN_EP 0x83
#define SHARK_OUT_EP 0x05
#define TB_LEN 7
#define DRV_NAME "radioshark2"
#define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
enum { BLUE_LED, RED_LED, NO_LEDS };
struct shark_device {
struct usb_device *usbdev;
struct v4l2_device v4l2_dev;
struct radio_tea5777 tea;
#ifdef SHARK_USE_LEDS
struct work_struct led_work;
struct led_classdev leds[NO_LEDS];
char led_names[NO_LEDS][32];
atomic_t brightness[NO_LEDS];
unsigned long brightness_new;
#endif
u8 *transfer_buffer;
};
static atomic_t shark_instance = ATOMIC_INIT(0);
static int shark_write_reg(struct radio_tea5777 *tea, u64 reg)
{
struct shark_device *shark = tea->private_data;
int i, res, actual_len;
memset(shark->transfer_buffer, 0, TB_LEN);
shark->transfer_buffer[0] = 0x81; /* Write register command */
for (i = 0; i < 6; i++)
shark->transfer_buffer[i + 1] = (reg >> (40 - i * 8)) & 0xff;
v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-write: %*ph\n",
7, shark->transfer_buffer);
res = usb_interrupt_msg(shark->usbdev,
usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0) {
v4l2_err(tea->v4l2_dev, "write error: %d\n", res);
return res;
}
return 0;
}
static int shark_read_reg(struct radio_tea5777 *tea, u32 *reg_ret)
{
struct shark_device *shark = tea->private_data;
int i, res, actual_len;
u32 reg = 0;
memset(shark->transfer_buffer, 0, TB_LEN);
shark->transfer_buffer[0] = 0x82;
res = usb_interrupt_msg(shark->usbdev,
usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0) {
v4l2_err(tea->v4l2_dev, "request-read error: %d\n", res);
return res;
}
res = usb_interrupt_msg(shark->usbdev,
usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0) {
v4l2_err(tea->v4l2_dev, "read error: %d\n", res);
return res;
}
for (i = 0; i < 3; i++)
reg |= shark->transfer_buffer[i] << (16 - i * 8);
v4l2_dbg(1, debug, tea->v4l2_dev, "shark2-read: %*ph\n",
3, shark->transfer_buffer);
*reg_ret = reg;
return 0;
}
static const struct radio_tea5777_ops shark_tea_ops = {
.write_reg = shark_write_reg,
.read_reg = shark_read_reg,
};
#ifdef SHARK_USE_LEDS
static void shark_led_work(struct work_struct *work)
{
struct shark_device *shark =
container_of(work, struct shark_device, led_work);
int i, res, brightness, actual_len;
for (i = 0; i < 2; i++) {
if (!test_and_clear_bit(i, &shark->brightness_new))
continue;
brightness = atomic_read(&shark->brightness[i]);
memset(shark->transfer_buffer, 0, TB_LEN);
shark->transfer_buffer[0] = 0x83 + i;
shark->transfer_buffer[1] = brightness;
res = usb_interrupt_msg(shark->usbdev,
usb_sndintpipe(shark->usbdev,
SHARK_OUT_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0)
v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
shark->led_names[i], res);
}
}
static void shark_led_set_blue(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct shark_device *shark =
container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
atomic_set(&shark->brightness[BLUE_LED], value);
set_bit(BLUE_LED, &shark->brightness_new);
schedule_work(&shark->led_work);
}
static void shark_led_set_red(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct shark_device *shark =
container_of(led_cdev, struct shark_device, leds[RED_LED]);
atomic_set(&shark->brightness[RED_LED], value);
set_bit(RED_LED, &shark->brightness_new);
schedule_work(&shark->led_work);
}
static const struct led_classdev shark_led_templates[NO_LEDS] = {
[BLUE_LED] = {
.name = "%s:blue:",
.brightness = LED_OFF,
.max_brightness = 127,
.brightness_set = shark_led_set_blue,
},
[RED_LED] = {
.name = "%s:red:",
.brightness = LED_OFF,
.max_brightness = 1,
.brightness_set = shark_led_set_red,
},
};
static int shark_register_leds(struct shark_device *shark, struct device *dev)
{
int i, retval;
atomic_set(&shark->brightness[BLUE_LED], 127);
INIT_WORK(&shark->led_work, shark_led_work);
for (i = 0; i < NO_LEDS; i++) {
shark->leds[i] = shark_led_templates[i];
snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
shark->leds[i].name, shark->v4l2_dev.name);
shark->leds[i].name = shark->led_names[i];
retval = led_classdev_register(dev, &shark->leds[i]);
if (retval) {
v4l2_err(&shark->v4l2_dev,
"couldn't register led: %s\n",
shark->led_names[i]);
return retval;
}
}
return 0;
}
static void shark_unregister_leds(struct shark_device *shark)
{
int i;
for (i = 0; i < NO_LEDS; i++)
led_classdev_unregister(&shark->leds[i]);
cancel_work_sync(&shark->led_work);
}
static inline void shark_resume_leds(struct shark_device *shark)
{
int i;
for (i = 0; i < NO_LEDS; i++)
set_bit(i, &shark->brightness_new);
schedule_work(&shark->led_work);
}
#else
static int shark_register_leds(struct shark_device *shark, struct device *dev)
{
v4l2_warn(&shark->v4l2_dev,
"CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
return 0;
}
static inline void shark_unregister_leds(struct shark_device *shark) { }
static inline void shark_resume_leds(struct shark_device *shark) { }
#endif
static void usb_shark_disconnect(struct usb_interface *intf)
{
struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
mutex_lock(&shark->tea.mutex);
v4l2_device_disconnect(&shark->v4l2_dev);
radio_tea5777_exit(&shark->tea);
mutex_unlock(&shark->tea.mutex);
shark_unregister_leds(shark);
v4l2_device_put(&shark->v4l2_dev);
}
static void usb_shark_release(struct v4l2_device *v4l2_dev)
{
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
v4l2_device_unregister(&shark->v4l2_dev);
kfree(shark->transfer_buffer);
kfree(shark);
}
static int usb_shark_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct shark_device *shark;
int retval = -ENOMEM;
static const u8 ep_addresses[] = {
SHARK_IN_EP | USB_DIR_IN,
SHARK_OUT_EP | USB_DIR_OUT,
0};
/* Are the expected endpoints present? */
if (!usb_check_int_endpoints(intf, ep_addresses)) {
dev_err(&intf->dev, "Invalid radioSHARK2 device\n");
return -EINVAL;
}
shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
if (!shark)
return retval;
shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
if (!shark->transfer_buffer)
goto err_alloc_buffer;
v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
retval = shark_register_leds(shark, &intf->dev);
if (retval)
goto err_reg_leds;
shark->v4l2_dev.release = usb_shark_release;
retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
if (retval) {
v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
goto err_reg_dev;
}
shark->usbdev = interface_to_usbdev(intf);
shark->tea.v4l2_dev = &shark->v4l2_dev;
shark->tea.private_data = shark;
shark->tea.ops = &shark_tea_ops;
shark->tea.has_am = true;
shark->tea.write_before_read = true;
strscpy(shark->tea.card, "Griffin radioSHARK2",
sizeof(shark->tea.card));
usb_make_path(shark->usbdev, shark->tea.bus_info,
sizeof(shark->tea.bus_info));
retval = radio_tea5777_init(&shark->tea, THIS_MODULE);
if (retval) {
v4l2_err(&shark->v4l2_dev, "couldn't init tea5777\n");
goto err_init_tea;
}
return 0;
err_init_tea:
v4l2_device_unregister(&shark->v4l2_dev);
err_reg_dev:
shark_unregister_leds(shark);
err_reg_leds:
kfree(shark->transfer_buffer);
err_alloc_buffer:
kfree(shark);
return retval;
}
#ifdef CONFIG_PM
static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message)
{
return 0;
}
static int usb_shark_resume(struct usb_interface *intf)
{
struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
int ret;
mutex_lock(&shark->tea.mutex);
ret = radio_tea5777_set_freq(&shark->tea);
mutex_unlock(&shark->tea.mutex);
shark_resume_leds(shark);
return ret;
}
#endif
/* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
static const struct usb_device_id usb_shark_device_table[] = {
{ .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
USB_DEVICE_ID_MATCH_INT_CLASS,
.idVendor = 0x077d,
.idProduct = 0x627a,
.bcdDevice_lo = 0x0010,
.bcdDevice_hi = 0x0010,
.bInterfaceClass = 3,
},
{ }
};
MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
static struct usb_driver usb_shark_driver = {
.name = DRV_NAME,
.probe = usb_shark_probe,
.disconnect = usb_shark_disconnect,
.id_table = usb_shark_device_table,
#ifdef CONFIG_PM
.suspend = usb_shark_suspend,
.resume = usb_shark_resume,
.reset_resume = usb_shark_resume,
#endif
};
module_usb_driver(usb_shark_driver);
| linux-master | drivers/media/radio/radio-shark2.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/usb.h>
#include <linux/hid.h>
#include <linux/mutex.h>
#include <linux/videodev2.h>
#include <asm/unaligned.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
/*
* 'Thanko's Raremono' is a Japanese si4734-based AM/FM/SW USB receiver:
*
* http://www.raremono.jp/product/484.html/
*
* The USB protocol has been reversed engineered using wireshark, initially
* by Dinesh Ram <[email protected]> and finished by Hans Verkuil
* <[email protected]>.
*
* Sadly the firmware used in this product hides lots of goodies since the
* si4734 has more features than are supported by the firmware. Oh well...
*/
/* driver and module definitions */
MODULE_AUTHOR("Hans Verkuil <[email protected]>");
MODULE_DESCRIPTION("Thanko's Raremono AM/FM/SW Receiver USB driver");
MODULE_LICENSE("GPL v2");
/*
* The Device announces itself as Cygnal Integrated Products, Inc.
*
* The vendor and product IDs (and in fact all other lsusb information as
* well) are identical to the si470x Silicon Labs USB FM Radio Reference
* Design board, even though this card has a si4734 device. Clearly the
* designer of this product never bothered to change the USB IDs.
*/
/* USB Device ID List */
static const struct usb_device_id usb_raremono_device_table[] = {
{USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID, 0, 0) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_raremono_device_table);
#define BUFFER_LENGTH 64
/* Timeout is set to a high value, could probably be reduced. Need more tests */
#define USB_TIMEOUT 10000
/* Frequency limits in KHz */
#define FM_FREQ_RANGE_LOW 64000
#define FM_FREQ_RANGE_HIGH 108000
#define AM_FREQ_RANGE_LOW 520
#define AM_FREQ_RANGE_HIGH 1710
#define SW_FREQ_RANGE_LOW 2300
#define SW_FREQ_RANGE_HIGH 26100
enum { BAND_FM, BAND_AM, BAND_SW };
static const struct v4l2_frequency_band bands[] = {
/* Band FM */
{
.type = V4L2_TUNER_RADIO,
.index = 0,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = FM_FREQ_RANGE_LOW * 16,
.rangehigh = FM_FREQ_RANGE_HIGH * 16,
.modulation = V4L2_BAND_MODULATION_FM,
},
/* Band AM */
{
.type = V4L2_TUNER_RADIO,
.index = 1,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = AM_FREQ_RANGE_LOW * 16,
.rangehigh = AM_FREQ_RANGE_HIGH * 16,
.modulation = V4L2_BAND_MODULATION_AM,
},
/* Band SW */
{
.type = V4L2_TUNER_RADIO,
.index = 2,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = SW_FREQ_RANGE_LOW * 16,
.rangehigh = SW_FREQ_RANGE_HIGH * 16,
.modulation = V4L2_BAND_MODULATION_AM,
},
};
struct raremono_device {
struct usb_device *usbdev;
struct usb_interface *intf;
struct video_device vdev;
struct v4l2_device v4l2_dev;
struct mutex lock;
u8 *buffer;
u32 band;
unsigned curfreq;
};
static inline struct raremono_device *to_raremono_dev(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct raremono_device, v4l2_dev);
}
/* Set frequency. */
static int raremono_cmd_main(struct raremono_device *radio, unsigned band, unsigned freq)
{
unsigned band_offset;
int ret;
switch (band) {
case BAND_FM:
band_offset = 1;
freq /= 10;
break;
case BAND_AM:
band_offset = 0;
break;
default:
band_offset = 2;
break;
}
radio->buffer[0] = 0x04 + band_offset;
radio->buffer[1] = freq >> 8;
radio->buffer[2] = freq & 0xff;
ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
HID_REQ_SET_REPORT,
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0x0300 + radio->buffer[0], 2,
radio->buffer, 3, USB_TIMEOUT);
if (ret < 0) {
dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
return ret;
}
radio->curfreq = (band == BAND_FM) ? freq * 10 : freq;
return 0;
}
/* Handle unplugging the device.
* We call video_unregister_device in any case.
* The last function called in this procedure is
* usb_raremono_device_release.
*/
static void usb_raremono_disconnect(struct usb_interface *intf)
{
struct raremono_device *radio = to_raremono_dev(usb_get_intfdata(intf));
dev_info(&intf->dev, "Thanko's Raremono disconnected\n");
mutex_lock(&radio->lock);
usb_set_intfdata(intf, NULL);
video_unregister_device(&radio->vdev);
v4l2_device_disconnect(&radio->v4l2_dev);
mutex_unlock(&radio->lock);
v4l2_device_put(&radio->v4l2_dev);
}
/*
* Linux Video interface
*/
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct raremono_device *radio = video_drvdata(file);
strscpy(v->driver, "radio-raremono", sizeof(v->driver));
strscpy(v->card, "Thanko's Raremono", sizeof(v->card));
usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
return 0;
}
static int vidioc_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
if (band->tuner != 0)
return -EINVAL;
if (band->index >= ARRAY_SIZE(bands))
return -EINVAL;
*band = bands[band->index];
return 0;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct raremono_device *radio = video_drvdata(file);
int ret;
if (v->index > 0)
return -EINVAL;
strscpy(v->name, "AM/FM/SW", sizeof(v->name));
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS;
v->rangelow = AM_FREQ_RANGE_LOW * 16;
v->rangehigh = FM_FREQ_RANGE_HIGH * 16;
v->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
v->audmode = (radio->curfreq < FM_FREQ_RANGE_LOW) ?
V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
memset(radio->buffer, 1, BUFFER_LENGTH);
ret = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
1, 0xa1, 0x030d, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
dev_warn(radio->v4l2_dev.dev, "%s failed (%d)\n", __func__, ret);
return ret;
}
v->signal = ((radio->buffer[1] & 0xf) << 8 | radio->buffer[2]) << 4;
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
return v->index ? -EINVAL : 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct raremono_device *radio = video_drvdata(file);
u32 freq;
unsigned band;
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
if (f->frequency >= (FM_FREQ_RANGE_LOW + SW_FREQ_RANGE_HIGH) * 8)
band = BAND_FM;
else if (f->frequency <= (AM_FREQ_RANGE_HIGH + SW_FREQ_RANGE_LOW) * 8)
band = BAND_AM;
else
band = BAND_SW;
freq = clamp_t(u32, f->frequency, bands[band].rangelow, bands[band].rangehigh);
return raremono_cmd_main(radio, band, freq / 16);
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct raremono_device *radio = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = radio->curfreq * 16;
return 0;
}
static void raremono_device_release(struct v4l2_device *v4l2_dev)
{
struct raremono_device *radio = to_raremono_dev(v4l2_dev);
kfree(radio->buffer);
kfree(radio);
}
/* File system interface */
static const struct v4l2_file_operations usb_raremono_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops usb_raremono_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_enum_freq_bands = vidioc_enum_freq_bands,
};
/* check if the device is present and register with v4l and usb if it is */
static int usb_raremono_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct raremono_device *radio;
int retval = 0;
radio = kzalloc(sizeof(*radio), GFP_KERNEL);
if (!radio)
return -ENOMEM;
radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
if (!radio->buffer) {
kfree(radio);
return -ENOMEM;
}
radio->usbdev = interface_to_usbdev(intf);
radio->intf = intf;
/*
* This device uses the same USB IDs as the si470x SiLabs reference
* design. So do an additional check: attempt to read the device ID
* from the si470x: the lower 12 bits are 0x0242 for the si470x. The
* Raremono always returns 0x0800 (the meaning of that is unknown, but
* at least it works).
*
* We use this check to determine which device we are dealing with.
*/
msleep(20);
retval = usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
HID_REQ_GET_REPORT,
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
1, 2,
radio->buffer, 3, 500);
if (retval != 3 ||
(get_unaligned_be16(&radio->buffer[1]) & 0xfff) == 0x0242) {
dev_info(&intf->dev, "this is not Thanko's Raremono.\n");
retval = -ENODEV;
goto free_mem;
}
dev_info(&intf->dev, "Thanko's Raremono connected: (%04X:%04X)\n",
id->idVendor, id->idProduct);
retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
if (retval < 0) {
dev_err(&intf->dev, "couldn't register v4l2_device\n");
goto free_mem;
}
mutex_init(&radio->lock);
strscpy(radio->vdev.name, radio->v4l2_dev.name,
sizeof(radio->vdev.name));
radio->vdev.v4l2_dev = &radio->v4l2_dev;
radio->vdev.fops = &usb_raremono_fops;
radio->vdev.ioctl_ops = &usb_raremono_ioctl_ops;
radio->vdev.lock = &radio->lock;
radio->vdev.release = video_device_release_empty;
radio->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
radio->v4l2_dev.release = raremono_device_release;
usb_set_intfdata(intf, &radio->v4l2_dev);
video_set_drvdata(&radio->vdev, radio);
raremono_cmd_main(radio, BAND_FM, 95160);
retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
if (retval == 0) {
dev_info(&intf->dev, "V4L2 device registered as %s\n",
video_device_node_name(&radio->vdev));
return 0;
}
dev_err(&intf->dev, "could not register video device\n");
v4l2_device_unregister(&radio->v4l2_dev);
free_mem:
kfree(radio->buffer);
kfree(radio);
return retval;
}
/* USB subsystem interface */
static struct usb_driver usb_raremono_driver = {
.name = "radio-raremono",
.probe = usb_raremono_probe,
.disconnect = usb_raremono_disconnect,
.id_table = usb_raremono_device_table,
};
module_usb_driver(usb_raremono_driver);
| linux-master | drivers/media/radio/radio-raremono.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* AimsLab RadioTrack (aka RadioVeveal) driver
*
* Copyright 1997 M. Kirkwood
*
* Converted to the radio-isa framework by Hans Verkuil <[email protected]>
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
* Converted to new API by Alan Cox <[email protected]>
* Various bugfixes and enhancements by Russell Kroll <[email protected]>
*
* Notes on the hardware (reverse engineered from other peoples'
* reverse engineering of AIMS' code :-)
*
* Frequency control is done digitally -- ie out(port,encodefreq(95.8));
*
* The signal strength query is unsurprisingly inaccurate. And it seems
* to indicate that (on my card, at least) the frequency setting isn't
* too great. (I have to tune up .025MHz from what the freq should be
* to get a report that the thing is tuned.)
*
* Volume control is (ugh) analogue:
* out(port, start_increasing_volume);
* wait(a_wee_while);
* out(port, stop_changing_the_volume);
*
* Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* msleep */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/io.h> /* outb, outb_p */
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include "radio-isa.h"
#include "lm7000.h"
MODULE_AUTHOR("M. Kirkwood");
MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0");
#ifndef CONFIG_RADIO_RTRACK_PORT
#define CONFIG_RADIO_RTRACK_PORT -1
#endif
#define RTRACK_MAX 2
static int io[RTRACK_MAX] = { [0] = CONFIG_RADIO_RTRACK_PORT,
[1 ... (RTRACK_MAX - 1)] = -1 };
static int radio_nr[RTRACK_MAX] = { [0 ... (RTRACK_MAX - 1)] = -1 };
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "I/O addresses of the RadioTrack card (0x20f or 0x30f)");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
struct rtrack {
struct radio_isa_card isa;
int curvol;
};
static struct radio_isa_card *rtrack_alloc(void)
{
struct rtrack *rt = kzalloc(sizeof(struct rtrack), GFP_KERNEL);
if (rt)
rt->curvol = 0xff;
return rt ? &rt->isa : NULL;
}
#define AIMS_BIT_TUN_CE (1 << 0)
#define AIMS_BIT_TUN_CLK (1 << 1)
#define AIMS_BIT_TUN_DATA (1 << 2)
#define AIMS_BIT_VOL_CE (1 << 3)
#define AIMS_BIT_TUN_STRQ (1 << 4)
/* bit 5 is not connected */
#define AIMS_BIT_VOL_UP (1 << 6) /* active low */
#define AIMS_BIT_VOL_DN (1 << 7) /* active low */
static void rtrack_set_pins(void *handle, u8 pins)
{
struct radio_isa_card *isa = handle;
struct rtrack *rt = container_of(isa, struct rtrack, isa);
u8 bits = AIMS_BIT_VOL_DN | AIMS_BIT_VOL_UP | AIMS_BIT_TUN_STRQ;
if (!v4l2_ctrl_g_ctrl(rt->isa.mute))
bits |= AIMS_BIT_VOL_CE;
if (pins & LM7000_DATA)
bits |= AIMS_BIT_TUN_DATA;
if (pins & LM7000_CLK)
bits |= AIMS_BIT_TUN_CLK;
if (pins & LM7000_CE)
bits |= AIMS_BIT_TUN_CE;
outb_p(bits, rt->isa.io);
}
static int rtrack_s_frequency(struct radio_isa_card *isa, u32 freq)
{
lm7000_set_freq(freq, isa, rtrack_set_pins);
return 0;
}
static u32 rtrack_g_signal(struct radio_isa_card *isa)
{
/* bit set = no signal present */
return 0xffff * !(inb(isa->io) & 2);
}
static int rtrack_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
struct rtrack *rt = container_of(isa, struct rtrack, isa);
int curvol = rt->curvol;
if (mute) {
outb(0xd0, isa->io); /* volume steady + sigstr + off */
return 0;
}
if (vol == 0) { /* volume = 0 means mute the card */
outb(0x48, isa->io); /* volume down but still "on" */
msleep(curvol * 3); /* make sure it's totally down */
} else if (curvol < vol) {
outb(0x98, isa->io); /* volume up + sigstr + on */
for (; curvol < vol; curvol++)
mdelay(3);
} else if (curvol > vol) {
outb(0x58, isa->io); /* volume down + sigstr + on */
for (; curvol > vol; curvol--)
mdelay(3);
}
outb(0xd8, isa->io); /* volume steady + sigstr + on */
rt->curvol = vol;
return 0;
}
/* Mute card - prevents noisy bootups */
static int rtrack_initialize(struct radio_isa_card *isa)
{
/* this ensures that the volume is all the way up */
outb(0x90, isa->io); /* volume up but still "on" */
msleep(3000); /* make sure it's totally up */
outb(0xc0, isa->io); /* steady volume, mute card */
return 0;
}
static const struct radio_isa_ops rtrack_ops = {
.alloc = rtrack_alloc,
.init = rtrack_initialize,
.s_mute_volume = rtrack_s_mute_volume,
.s_frequency = rtrack_s_frequency,
.g_signal = rtrack_g_signal,
};
static const int rtrack_ioports[] = { 0x20f, 0x30f };
static struct radio_isa_driver rtrack_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-aimslab",
},
},
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = rtrack_ioports,
.num_of_io_ports = ARRAY_SIZE(rtrack_ioports),
.region_size = 2,
.card = "AIMSlab RadioTrack/RadioReveal",
.ops = &rtrack_ops,
.has_stereo = true,
.max_volume = 0xff,
};
static int __init rtrack_init(void)
{
return isa_register_driver(&rtrack_driver.driver, RTRACK_MAX);
}
static void __exit rtrack_exit(void)
{
isa_unregister_driver(&rtrack_driver.driver);
}
module_init(rtrack_init);
module_exit(rtrack_exit);
| linux-master | drivers/media/radio/radio-aimslab.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Zoltrix Radio Plus driver
* Copyright 1998 C. van Schaik <[email protected]>
*
* BUGS
* Due to the inconsistency in reading from the signal flags
* it is difficult to get an accurate tuned signal.
*
* It seems that the card is not linear to 0 volume. It cuts off
* at a low volume, and it is not possible (at least I have not found)
* to get fine volume control over the low volume range.
*
* Some code derived from code by Romolo Manfredini
* [email protected]
*
* 1999-05-06 - (C. van Schaik)
* - Make signal strength and stereo scans
* kinder to cpu while in delay
* 1999-01-05 - (C. van Schaik)
* - Changed tuning to 1/160Mhz accuracy
* - Added stereo support
* (card defaults to stereo)
* (can explicitly force mono on the card)
* (can detect if station is in stereo)
* - Added unmute function
* - Reworked ioctl functions
* 2002-07-15 - Fix Stereo typo
*
* 2006-07-24 - Converted to V4L2 API
* by Mauro Carvalho Chehab <[email protected]>
*
* Converted to the radio-isa framework by Hans Verkuil <[email protected]>
*
* Note that this is the driver for the Zoltrix Radio Plus.
* This driver does not work for the Zoltrix Radio Plus 108 or the
* Zoltrix Radio Plus for Windows.
*
* Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* udelay, msleep */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/mutex.h>
#include <linux/io.h> /* outb, outb_p */
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include "radio-isa.h"
MODULE_AUTHOR("C. van Schaik");
MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1.99");
#ifndef CONFIG_RADIO_ZOLTRIX_PORT
#define CONFIG_RADIO_ZOLTRIX_PORT -1
#endif
#define ZOLTRIX_MAX 2
static int io[ZOLTRIX_MAX] = { [0] = CONFIG_RADIO_ZOLTRIX_PORT,
[1 ... (ZOLTRIX_MAX - 1)] = -1 };
static int radio_nr[ZOLTRIX_MAX] = { [0 ... (ZOLTRIX_MAX - 1)] = -1 };
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "I/O addresses of the Zoltrix Radio Plus card (0x20c or 0x30c)");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
struct zoltrix {
struct radio_isa_card isa;
int curvol;
bool muted;
};
static struct radio_isa_card *zoltrix_alloc(void)
{
struct zoltrix *zol = kzalloc(sizeof(*zol), GFP_KERNEL);
return zol ? &zol->isa : NULL;
}
static int zoltrix_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
zol->curvol = vol;
zol->muted = mute;
if (mute || vol == 0) {
outb(0, isa->io);
outb(0, isa->io);
inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
return 0;
}
outb(vol - 1, isa->io);
msleep(10);
inb(isa->io + 2);
return 0;
}
/* tunes the radio to the desired frequency */
static int zoltrix_s_frequency(struct radio_isa_card *isa, u32 freq)
{
struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
struct v4l2_device *v4l2_dev = &isa->v4l2_dev;
unsigned long long bitmask, f, m;
bool stereo = isa->stereo;
int i;
if (freq == 0) {
v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
return -EINVAL;
}
m = (freq / 160 - 8800) * 2;
f = (unsigned long long)m + 0x4d1c;
bitmask = 0xc480402c10080000ull;
i = 45;
outb(0, isa->io);
outb(0, isa->io);
inb(isa->io + 3); /* Zoltrix needs to be read to confirm */
outb(0x40, isa->io);
outb(0xc0, isa->io);
bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
while (i--) {
if ((bitmask & 0x8000000000000000ull) != 0) {
outb(0x80, isa->io);
udelay(50);
outb(0x00, isa->io);
udelay(50);
outb(0x80, isa->io);
udelay(50);
} else {
outb(0xc0, isa->io);
udelay(50);
outb(0x40, isa->io);
udelay(50);
outb(0xc0, isa->io);
udelay(50);
}
bitmask *= 2;
}
/* termination sequence */
outb(0x80, isa->io);
outb(0xc0, isa->io);
outb(0x40, isa->io);
udelay(1000);
inb(isa->io + 2);
udelay(1000);
return zoltrix_s_mute_volume(isa, zol->muted, zol->curvol);
}
/* Get signal strength */
static u32 zoltrix_g_rxsubchans(struct radio_isa_card *isa)
{
struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
int a, b;
outb(0x00, isa->io); /* This stuff I found to do nothing */
outb(zol->curvol, isa->io);
msleep(20);
a = inb(isa->io);
msleep(10);
b = inb(isa->io);
return (a == b && a == 0xcf) ?
V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
}
static u32 zoltrix_g_signal(struct radio_isa_card *isa)
{
struct zoltrix *zol = container_of(isa, struct zoltrix, isa);
int a, b;
outb(0x00, isa->io); /* This stuff I found to do nothing */
outb(zol->curvol, isa->io);
msleep(20);
a = inb(isa->io);
msleep(10);
b = inb(isa->io);
if (a != b)
return 0;
/* I found this out by playing with a binary scanner on the card io */
return (a == 0xcf || a == 0xdf || a == 0xef) ? 0xffff : 0;
}
static int zoltrix_s_stereo(struct radio_isa_card *isa, bool stereo)
{
return zoltrix_s_frequency(isa, isa->freq);
}
static const struct radio_isa_ops zoltrix_ops = {
.alloc = zoltrix_alloc,
.s_mute_volume = zoltrix_s_mute_volume,
.s_frequency = zoltrix_s_frequency,
.s_stereo = zoltrix_s_stereo,
.g_rxsubchans = zoltrix_g_rxsubchans,
.g_signal = zoltrix_g_signal,
};
static const int zoltrix_ioports[] = { 0x20c, 0x30c };
static struct radio_isa_driver zoltrix_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-zoltrix",
},
},
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = zoltrix_ioports,
.num_of_io_ports = ARRAY_SIZE(zoltrix_ioports),
.region_size = 2,
.card = "Zoltrix Radio Plus",
.ops = &zoltrix_ops,
.has_stereo = true,
.max_volume = 15,
};
static int __init zoltrix_init(void)
{
return isa_register_driver(&zoltrix_driver.driver, ZOLTRIX_MAX);
}
static void __exit zoltrix_exit(void)
{
isa_unregister_driver(&zoltrix_driver.driver);
}
module_init(zoltrix_init);
module_exit(zoltrix_exit);
| linux-master | drivers/media/radio/radio-zoltrix.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2012 Hans Verkuil <[email protected]>
*/
/* kernel includes */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <linux/usb.h>
#include <linux/mutex.h>
/* driver and module definitions */
MODULE_AUTHOR("Hans Verkuil <[email protected]>");
MODULE_DESCRIPTION("Keene FM Transmitter driver");
MODULE_LICENSE("GPL");
/* Actually, it advertises itself as a Logitech */
#define USB_KEENE_VENDOR 0x046d
#define USB_KEENE_PRODUCT 0x0a0e
/* Probably USB_TIMEOUT should be modified in module parameter */
#define BUFFER_LENGTH 8
#define USB_TIMEOUT 500
/* Frequency limits in MHz */
#define FREQ_MIN 76U
#define FREQ_MAX 108U
#define FREQ_MUL 16000U
/* USB Device ID List */
static const struct usb_device_id usb_keene_device_table[] = {
{USB_DEVICE_AND_INTERFACE_INFO(USB_KEENE_VENDOR, USB_KEENE_PRODUCT,
USB_CLASS_HID, 0, 0) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_keene_device_table);
struct keene_device {
struct usb_device *usbdev;
struct usb_interface *intf;
struct video_device vdev;
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler hdl;
struct mutex lock;
u8 *buffer;
unsigned curfreq;
u8 tx;
u8 pa;
bool stereo;
bool muted;
bool preemph_75_us;
};
static inline struct keene_device *to_keene_dev(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct keene_device, v4l2_dev);
}
/* Set frequency (if non-0), PA, mute and turn on/off the FM transmitter. */
static int keene_cmd_main(struct keene_device *radio, unsigned freq, bool play)
{
unsigned short freq_send = freq ? (freq - 76 * 16000) / 800 : 0;
int ret;
radio->buffer[0] = 0x00;
radio->buffer[1] = 0x50;
radio->buffer[2] = (freq_send >> 8) & 0xff;
radio->buffer[3] = freq_send & 0xff;
radio->buffer[4] = radio->pa;
/* If bit 4 is set, then tune to the frequency.
If bit 3 is set, then unmute; if bit 2 is set, then mute.
If bit 1 is set, then enter idle mode; if bit 0 is set,
then enter transmit mode.
*/
radio->buffer[5] = (radio->muted ? 4 : 8) | (play ? 1 : 2) |
(freq ? 0x10 : 0);
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret);
return ret;
}
if (freq)
radio->curfreq = freq;
return 0;
}
/* Set TX, stereo and preemphasis mode (50 us vs 75 us). */
static int keene_cmd_set(struct keene_device *radio)
{
int ret;
radio->buffer[0] = 0x00;
radio->buffer[1] = 0x51;
radio->buffer[2] = radio->tx;
/* If bit 0 is set, then transmit mono, otherwise stereo.
If bit 2 is set, then enable 75 us preemphasis, otherwise
it is 50 us. */
radio->buffer[3] = (radio->stereo ? 0 : 1) | (radio->preemph_75_us ? 4 : 0);
radio->buffer[4] = 0x00;
radio->buffer[5] = 0x00;
radio->buffer[6] = 0x00;
radio->buffer[7] = 0x00;
ret = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
9, 0x21, 0x200, 2, radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
dev_warn(&radio->vdev.dev, "%s failed (%d)\n", __func__, ret);
return ret;
}
return 0;
}
/* Handle unplugging the device.
* We call video_unregister_device in any case.
* The last function called in this procedure is
* usb_keene_device_release.
*/
static void usb_keene_disconnect(struct usb_interface *intf)
{
struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
mutex_lock(&radio->lock);
usb_set_intfdata(intf, NULL);
video_unregister_device(&radio->vdev);
v4l2_device_disconnect(&radio->v4l2_dev);
mutex_unlock(&radio->lock);
v4l2_device_put(&radio->v4l2_dev);
}
static int usb_keene_suspend(struct usb_interface *intf, pm_message_t message)
{
struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
return keene_cmd_main(radio, 0, false);
}
static int usb_keene_resume(struct usb_interface *intf)
{
struct keene_device *radio = to_keene_dev(usb_get_intfdata(intf));
mdelay(50);
keene_cmd_set(radio);
keene_cmd_main(radio, radio->curfreq, true);
return 0;
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct keene_device *radio = video_drvdata(file);
strscpy(v->driver, "radio-keene", sizeof(v->driver));
strscpy(v->card, "Keene FM Transmitter", sizeof(v->card));
usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
return 0;
}
static int vidioc_g_modulator(struct file *file, void *priv,
struct v4l2_modulator *v)
{
struct keene_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
strscpy(v->name, "FM", sizeof(v->name));
v->rangelow = FREQ_MIN * FREQ_MUL;
v->rangehigh = FREQ_MAX * FREQ_MUL;
v->txsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
return 0;
}
static int vidioc_s_modulator(struct file *file, void *priv,
const struct v4l2_modulator *v)
{
struct keene_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
radio->stereo = (v->txsubchans == V4L2_TUNER_SUB_STEREO);
return keene_cmd_set(radio);
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct keene_device *radio = video_drvdata(file);
unsigned freq = f->frequency;
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
return keene_cmd_main(radio, freq, true);
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct keene_device *radio = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = radio->curfreq;
return 0;
}
static int keene_s_ctrl(struct v4l2_ctrl *ctrl)
{
static const u8 db2tx[] = {
/* -15, -12, -9, -6, -3, 0 dB */
0x03, 0x13, 0x02, 0x12, 0x22, 0x32,
/* 3, 6, 9, 12, 15, 18 dB */
0x21, 0x31, 0x20, 0x30, 0x40, 0x50
};
struct keene_device *radio =
container_of(ctrl->handler, struct keene_device, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
radio->muted = ctrl->val;
return keene_cmd_main(radio, 0, true);
case V4L2_CID_TUNE_POWER_LEVEL:
/* To go from dBuV to the register value we apply the
following formula: */
radio->pa = (ctrl->val - 71) * 100 / 62;
return keene_cmd_main(radio, 0, true);
case V4L2_CID_TUNE_PREEMPHASIS:
radio->preemph_75_us = ctrl->val == V4L2_PREEMPHASIS_75_uS;
return keene_cmd_set(radio);
case V4L2_CID_AUDIO_COMPRESSION_GAIN:
radio->tx = db2tx[(ctrl->val - (s32)ctrl->minimum) / (s32)ctrl->step];
return keene_cmd_set(radio);
}
return -EINVAL;
}
/* File system interface */
static const struct v4l2_file_operations usb_keene_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ctrl_ops keene_ctrl_ops = {
.s_ctrl = keene_s_ctrl,
};
static const struct v4l2_ioctl_ops usb_keene_ioctl_ops = {
.vidioc_querycap = vidioc_querycap,
.vidioc_g_modulator = vidioc_g_modulator,
.vidioc_s_modulator = vidioc_s_modulator,
.vidioc_g_frequency = vidioc_g_frequency,
.vidioc_s_frequency = vidioc_s_frequency,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static void usb_keene_video_device_release(struct v4l2_device *v4l2_dev)
{
struct keene_device *radio = to_keene_dev(v4l2_dev);
/* free rest memory */
v4l2_ctrl_handler_free(&radio->hdl);
kfree(radio->buffer);
kfree(radio);
}
/* check if the device is present and register with v4l and usb if it is */
static int usb_keene_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(intf);
struct keene_device *radio;
struct v4l2_ctrl_handler *hdl;
int retval = 0;
/*
* The Keene FM transmitter USB device has the same USB ID as
* the Logitech AudioHub Speaker, but it should ignore the hid.
* Check if the name is that of the Keene device.
* If not, then someone connected the AudioHub and we shouldn't
* attempt to handle this driver.
* For reference: the product name of the AudioHub is
* "AudioHub Speaker".
*/
if (dev->product && strcmp(dev->product, "B-LINK USB Audio "))
return -ENODEV;
radio = kzalloc(sizeof(struct keene_device), GFP_KERNEL);
if (radio)
radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
if (!radio || !radio->buffer) {
dev_err(&intf->dev, "kmalloc for keene_device failed\n");
kfree(radio);
retval = -ENOMEM;
goto err;
}
hdl = &radio->hdl;
v4l2_ctrl_handler_init(hdl, 4);
v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_MUTE,
0, 1, 1, 0);
v4l2_ctrl_new_std_menu(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_PREEMPHASIS,
V4L2_PREEMPHASIS_75_uS, 1, V4L2_PREEMPHASIS_50_uS);
v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_TUNE_POWER_LEVEL,
84, 118, 1, 118);
v4l2_ctrl_new_std(hdl, &keene_ctrl_ops, V4L2_CID_AUDIO_COMPRESSION_GAIN,
-15, 18, 3, 0);
radio->pa = 118;
radio->tx = 0x32;
radio->stereo = true;
if (hdl->error) {
retval = hdl->error;
v4l2_ctrl_handler_free(hdl);
goto err_v4l2;
}
retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
if (retval < 0) {
dev_err(&intf->dev, "couldn't register v4l2_device\n");
goto err_v4l2;
}
mutex_init(&radio->lock);
radio->v4l2_dev.ctrl_handler = hdl;
radio->v4l2_dev.release = usb_keene_video_device_release;
strscpy(radio->vdev.name, radio->v4l2_dev.name,
sizeof(radio->vdev.name));
radio->vdev.v4l2_dev = &radio->v4l2_dev;
radio->vdev.fops = &usb_keene_fops;
radio->vdev.ioctl_ops = &usb_keene_ioctl_ops;
radio->vdev.lock = &radio->lock;
radio->vdev.release = video_device_release_empty;
radio->vdev.vfl_dir = VFL_DIR_TX;
radio->vdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_MODULATOR;
radio->usbdev = interface_to_usbdev(intf);
radio->intf = intf;
usb_set_intfdata(intf, &radio->v4l2_dev);
video_set_drvdata(&radio->vdev, radio);
/* at least 11ms is needed in order to settle hardware */
msleep(20);
keene_cmd_main(radio, 95.16 * FREQ_MUL, false);
retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
if (retval < 0) {
dev_err(&intf->dev, "could not register video device\n");
goto err_vdev;
}
v4l2_ctrl_handler_setup(hdl);
dev_info(&intf->dev, "V4L2 device registered as %s\n",
video_device_node_name(&radio->vdev));
return 0;
err_vdev:
v4l2_device_unregister(&radio->v4l2_dev);
err_v4l2:
kfree(radio->buffer);
kfree(radio);
err:
return retval;
}
/* USB subsystem interface */
static struct usb_driver usb_keene_driver = {
.name = "radio-keene",
.probe = usb_keene_probe,
.disconnect = usb_keene_disconnect,
.id_table = usb_keene_device_table,
.suspend = usb_keene_suspend,
.resume = usb_keene_resume,
.reset_resume = usb_keene_resume,
};
module_usb_driver(usb_keene_driver);
| linux-master | drivers/media/radio/radio-keene.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* driver/media/radio/radio-tea5764.c
*
* Driver for TEA5764 radio chip for linux 2.6.
* This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola.
* The I2C protocol is used for communicate with chip.
*
* Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
*
* Copyright (c) 2008 Fabio Belavenuto <[email protected]>
*
* History:
* 2008-12-06 Fabio Belavenuto <[email protected]>
* initial code
*
* TODO:
* add platform_data support for IRQs platform dependencies
* add RDS support
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h> /* Initdata */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/i2c.h> /* I2C */
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#define DRIVER_VERSION "0.0.2"
#define DRIVER_AUTHOR "Fabio Belavenuto <[email protected]>"
#define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
#define PINFO(format, ...)\
printk(KERN_INFO KBUILD_MODNAME ": "\
DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
#define PWARN(format, ...)\
printk(KERN_WARNING KBUILD_MODNAME ": "\
DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
# define PDEBUG(format, ...)\
printk(KERN_DEBUG KBUILD_MODNAME ": "\
DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
/* Frequency limits in MHz -- these are European values. For Japanese
devices, that would be 76000 and 91000. */
#define FREQ_MIN 87500U
#define FREQ_MAX 108000U
#define FREQ_MUL 16
/* TEA5764 registers */
#define TEA5764_MANID 0x002b
#define TEA5764_CHIPID 0x5764
#define TEA5764_INTREG_BLMSK 0x0001
#define TEA5764_INTREG_FRRMSK 0x0002
#define TEA5764_INTREG_LEVMSK 0x0008
#define TEA5764_INTREG_IFMSK 0x0010
#define TEA5764_INTREG_BLMFLAG 0x0100
#define TEA5764_INTREG_FRRFLAG 0x0200
#define TEA5764_INTREG_LEVFLAG 0x0800
#define TEA5764_INTREG_IFFLAG 0x1000
#define TEA5764_FRQSET_SUD 0x8000
#define TEA5764_FRQSET_SM 0x4000
#define TEA5764_TNCTRL_PUPD1 0x8000
#define TEA5764_TNCTRL_PUPD0 0x4000
#define TEA5764_TNCTRL_BLIM 0x2000
#define TEA5764_TNCTRL_SWPM 0x1000
#define TEA5764_TNCTRL_IFCTC 0x0800
#define TEA5764_TNCTRL_AFM 0x0400
#define TEA5764_TNCTRL_SMUTE 0x0200
#define TEA5764_TNCTRL_SNC 0x0100
#define TEA5764_TNCTRL_MU 0x0080
#define TEA5764_TNCTRL_SSL1 0x0040
#define TEA5764_TNCTRL_SSL0 0x0020
#define TEA5764_TNCTRL_HLSI 0x0010
#define TEA5764_TNCTRL_MST 0x0008
#define TEA5764_TNCTRL_SWP 0x0004
#define TEA5764_TNCTRL_DTC 0x0002
#define TEA5764_TNCTRL_AHLSI 0x0001
#define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
#define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
#define TEA5764_TUNCHK_TUNTO 0x0100
#define TEA5764_TUNCHK_LD 0x0008
#define TEA5764_TUNCHK_STEREO 0x0004
#define TEA5764_TESTREG_TRIGFR 0x0800
struct tea5764_regs {
u16 intreg; /* INTFLAG & INTMSK */
u16 frqset; /* FRQSETMSB & FRQSETLSB */
u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */
u16 tunchk; /* IFCHK & LEVCHK */
u16 testreg; /* TESTBITS & TESTMODE */
u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */
u16 rdslb; /* RDSLBMSB & RDSLBLSB */
u16 rdspb; /* RDSPBMSB & RDSPBLSB */
u16 rdsbc; /* RDSBBC & RDSGBC */
u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
u16 rdsbbl; /* PAUSEDET & RDSBBL */
u16 manid; /* MANID1 & MANID2 */
u16 chipid; /* CHIPID1 & CHIPID2 */
} __attribute__ ((packed));
struct tea5764_write_regs {
u8 intreg; /* INTMSK */
__be16 frqset; /* FRQSETMSB & FRQSETLSB */
__be16 tnctrl; /* TNCTRL1 & TNCTRL2 */
__be16 testreg; /* TESTBITS & TESTMODE */
__be16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
__be16 rdsbbl; /* PAUSEDET & RDSBBL */
} __attribute__ ((packed));
#ifdef CONFIG_RADIO_TEA5764_XTAL
#define RADIO_TEA5764_XTAL 1
#else
#define RADIO_TEA5764_XTAL 0
#endif
static int radio_nr = -1;
static int use_xtal = RADIO_TEA5764_XTAL;
struct tea5764_device {
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler ctrl_handler;
struct i2c_client *i2c_client;
struct video_device vdev;
struct tea5764_regs regs;
struct mutex mutex;
};
/* I2C code related */
static int tea5764_i2c_read(struct tea5764_device *radio)
{
int i;
u16 *p = (u16 *) &radio->regs;
struct i2c_msg msgs[1] = {
{ .addr = radio->i2c_client->addr,
.flags = I2C_M_RD,
.len = sizeof(radio->regs),
.buf = (void *)&radio->regs
},
};
if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
return -EIO;
for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
p[i] = __be16_to_cpu((__force __be16)p[i]);
return 0;
}
static int tea5764_i2c_write(struct tea5764_device *radio)
{
struct tea5764_write_regs wr;
struct tea5764_regs *r = &radio->regs;
struct i2c_msg msgs[1] = {
{
.addr = radio->i2c_client->addr,
.len = sizeof(wr),
.buf = (void *)&wr
},
};
wr.intreg = r->intreg & 0xff;
wr.frqset = __cpu_to_be16(r->frqset);
wr.tnctrl = __cpu_to_be16(r->tnctrl);
wr.testreg = __cpu_to_be16(r->testreg);
wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
return -EIO;
return 0;
}
static void tea5764_power_up(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
TEA5764_TNCTRL_HLSI);
if (!use_xtal)
r->testreg |= TEA5764_TESTREG_TRIGFR;
else
r->testreg &= ~TEA5764_TESTREG_TRIGFR;
r->tnctrl |= TEA5764_TNCTRL_PUPD0;
tea5764_i2c_write(radio);
}
}
static void tea5764_power_down(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
tea5764_i2c_write(radio);
}
}
static void tea5764_set_freq(struct tea5764_device *radio, int freq)
{
struct tea5764_regs *r = &radio->regs;
/* formula: (freq [+ or -] 225000) / 8192 */
if (r->tnctrl & TEA5764_TNCTRL_HLSI)
r->frqset = (freq + 225000) / 8192;
else
r->frqset = (freq - 225000) / 8192;
}
static int tea5764_get_freq(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (r->tnctrl & TEA5764_TNCTRL_HLSI)
return (r->frqchk * 8192) - 225000;
else
return (r->frqchk * 8192) + 225000;
}
/* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
static void tea5764_tune(struct tea5764_device *radio, int freq)
{
tea5764_set_freq(radio, freq);
if (tea5764_i2c_write(radio))
PWARN("Could not set frequency!");
}
static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
{
struct tea5764_regs *r = &radio->regs;
int tnctrl = r->tnctrl;
if (audmode == V4L2_TUNER_MODE_MONO)
r->tnctrl |= TEA5764_TNCTRL_MST;
else
r->tnctrl &= ~TEA5764_TNCTRL_MST;
if (tnctrl != r->tnctrl)
tea5764_i2c_write(radio);
}
static int tea5764_get_audout_mode(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (r->tnctrl & TEA5764_TNCTRL_MST)
return V4L2_TUNER_MODE_MONO;
else
return V4L2_TUNER_MODE_STEREO;
}
static void tea5764_mute(struct tea5764_device *radio, int on)
{
struct tea5764_regs *r = &radio->regs;
int tnctrl = r->tnctrl;
if (on)
r->tnctrl |= TEA5764_TNCTRL_MU;
else
r->tnctrl &= ~TEA5764_TNCTRL_MU;
if (tnctrl != r->tnctrl)
tea5764_i2c_write(radio);
}
/* V4L2 vidioc */
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct tea5764_device *radio = video_drvdata(file);
struct video_device *dev = &radio->vdev;
strscpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
strscpy(v->card, dev->name, sizeof(v->card));
snprintf(v->bus_info, sizeof(v->bus_info),
"I2C:%s", dev_name(&dev->dev));
return 0;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct tea5764_device *radio = video_drvdata(file);
struct tea5764_regs *r = &radio->regs;
if (v->index > 0)
return -EINVAL;
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
tea5764_i2c_read(radio);
v->rangelow = FREQ_MIN * FREQ_MUL;
v->rangehigh = FREQ_MAX * FREQ_MUL;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
if (r->tunchk & TEA5764_TUNCHK_STEREO)
v->rxsubchans = V4L2_TUNER_SUB_STEREO;
else
v->rxsubchans = V4L2_TUNER_SUB_MONO;
v->audmode = tea5764_get_audout_mode(radio);
v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct tea5764_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
tea5764_set_audout_mode(radio, v->audmode);
return 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct tea5764_device *radio = video_drvdata(file);
unsigned freq = f->frequency;
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
if (freq == 0) {
/* We special case this as a power down control. */
tea5764_power_down(radio);
/* Yes, that's what is returned in this case. This
whole special case is non-compliant and should really
be replaced with something better, but changing this
might well break code that depends on this behavior.
So we keep it as-is. */
return -EINVAL;
}
freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
tea5764_power_up(radio);
tea5764_tune(radio, (freq * 125) / 2);
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct tea5764_device *radio = video_drvdata(file);
struct tea5764_regs *r = &radio->regs;
if (f->tuner != 0)
return -EINVAL;
tea5764_i2c_read(radio);
f->type = V4L2_TUNER_RADIO;
if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
f->frequency = (tea5764_get_freq(radio) * 2) / 125;
else
f->frequency = 0;
return 0;
}
static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct tea5764_device *radio =
container_of(ctrl->handler, struct tea5764_device, ctrl_handler);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
tea5764_mute(radio, ctrl->val);
return 0;
}
return -EINVAL;
}
static const struct v4l2_ctrl_ops tea5764_ctrl_ops = {
.s_ctrl = tea5764_s_ctrl,
};
/* File system interface */
static const struct v4l2_file_operations tea5764_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops tea5764_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_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
/* V4L2 interface */
static const struct video_device tea5764_radio_template = {
.name = "TEA5764 FM-Radio",
.fops = &tea5764_fops,
.ioctl_ops = &tea5764_ioctl_ops,
.release = video_device_release_empty,
};
/* I2C probe: check if the device exists and register with v4l if it is */
static int tea5764_i2c_probe(struct i2c_client *client)
{
struct tea5764_device *radio;
struct v4l2_device *v4l2_dev;
struct v4l2_ctrl_handler *hdl;
struct tea5764_regs *r;
int ret;
PDEBUG("probe");
radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
if (!radio)
return -ENOMEM;
v4l2_dev = &radio->v4l2_dev;
ret = v4l2_device_register(&client->dev, v4l2_dev);
if (ret < 0) {
v4l2_err(v4l2_dev, "could not register v4l2_device\n");
goto errfr;
}
hdl = &radio->ctrl_handler;
v4l2_ctrl_handler_init(hdl, 1);
v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
v4l2_dev->ctrl_handler = hdl;
if (hdl->error) {
ret = hdl->error;
v4l2_err(v4l2_dev, "Could not register controls\n");
goto errunreg;
}
mutex_init(&radio->mutex);
radio->i2c_client = client;
ret = tea5764_i2c_read(radio);
if (ret)
goto errunreg;
r = &radio->regs;
PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
if (r->chipid != TEA5764_CHIPID ||
(r->manid & 0x0fff) != TEA5764_MANID) {
PWARN("This chip is not a TEA5764!");
ret = -EINVAL;
goto errunreg;
}
radio->vdev = tea5764_radio_template;
i2c_set_clientdata(client, radio);
video_set_drvdata(&radio->vdev, radio);
radio->vdev.lock = &radio->mutex;
radio->vdev.v4l2_dev = v4l2_dev;
radio->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
/* initialize and power off the chip */
tea5764_i2c_read(radio);
tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
tea5764_mute(radio, 1);
tea5764_power_down(radio);
ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr);
if (ret < 0) {
PWARN("Could not register video device!");
goto errunreg;
}
PINFO("registered.");
return 0;
errunreg:
v4l2_ctrl_handler_free(hdl);
v4l2_device_unregister(v4l2_dev);
errfr:
kfree(radio);
return ret;
}
static void tea5764_i2c_remove(struct i2c_client *client)
{
struct tea5764_device *radio = i2c_get_clientdata(client);
PDEBUG("remove");
if (radio) {
tea5764_power_down(radio);
video_unregister_device(&radio->vdev);
v4l2_ctrl_handler_free(&radio->ctrl_handler);
v4l2_device_unregister(&radio->v4l2_dev);
kfree(radio);
}
}
/* I2C subsystem interface */
static const struct i2c_device_id tea5764_id[] = {
{ "radio-tea5764", 0 },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(i2c, tea5764_id);
static struct i2c_driver tea5764_i2c_driver = {
.driver = {
.name = "radio-tea5764",
},
.probe = tea5764_i2c_probe,
.remove = tea5764_i2c_remove,
.id_table = tea5764_id,
};
module_i2c_driver(tea5764_i2c_driver);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);
module_param(use_xtal, int, 0);
MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "video4linux device number to use");
| linux-master | drivers/media/radio/radio-tea5764.c |
// SPDX-License-Identifier: GPL-2.0-only
/* Typhoon Radio Card driver for radio support
* (c) 1999 Dr. Henrik Seidel <[email protected]>
*
* Notes on the hardware
*
* This card has two output sockets, one for speakers and one for line.
* The speaker output has volume control, but only in four discrete
* steps. The line output has neither volume control nor mute.
*
* The card has auto-stereo according to its manual, although it all
* sounds mono to me (even with the Win/DOS drivers). Maybe it's my
* antenna - I really don't know for sure.
*
* Frequency control is done digitally.
*
* Volume control is done digitally, but there are only four different
* possible values. So you should better always turn the volume up and
* use line control. I got the best results by connecting line output
* to the sound card microphone input. For such a configuration the
* volume control has no effect, since volume control only influences
* the speaker output.
*
* There is no explicit mute/unmute. So I set the radio frequency to a
* value where I do expect just noise and turn the speaker volume down.
* The frequency change is necessary since the card never seems to be
* completely silent.
*
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/io.h> /* outb, outb_p */
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include "radio-isa.h"
#define DRIVER_VERSION "0.1.2"
MODULE_AUTHOR("Dr. Henrik Seidel");
MODULE_DESCRIPTION("A driver for the Typhoon radio card (a.k.a. EcoRadio).");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1.99");
#ifndef CONFIG_RADIO_TYPHOON_PORT
#define CONFIG_RADIO_TYPHOON_PORT -1
#endif
#ifndef CONFIG_RADIO_TYPHOON_MUTEFREQ
#define CONFIG_RADIO_TYPHOON_MUTEFREQ 87000
#endif
#define TYPHOON_MAX 2
static int io[TYPHOON_MAX] = { [0] = CONFIG_RADIO_TYPHOON_PORT,
[1 ... (TYPHOON_MAX - 1)] = -1 };
static int radio_nr[TYPHOON_MAX] = { [0 ... (TYPHOON_MAX - 1)] = -1 };
static unsigned long mutefreq = CONFIG_RADIO_TYPHOON_MUTEFREQ;
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "I/O addresses of the Typhoon card (0x316 or 0x336)");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
module_param(mutefreq, ulong, 0);
MODULE_PARM_DESC(mutefreq, "Frequency used when muting the card (in kHz)");
struct typhoon {
struct radio_isa_card isa;
int muted;
};
static struct radio_isa_card *typhoon_alloc(void)
{
struct typhoon *ty = kzalloc(sizeof(*ty), GFP_KERNEL);
return ty ? &ty->isa : NULL;
}
static int typhoon_s_frequency(struct radio_isa_card *isa, u32 freq)
{
unsigned long outval;
unsigned long x;
/*
* The frequency transfer curve is not linear. The best fit I could
* get is
*
* outval = -155 + exp((f + 15.55) * 0.057))
*
* where frequency f is in MHz. Since we don't have exp in the kernel,
* I approximate this function by a third order polynomial.
*
*/
x = freq / 160;
outval = (x * x + 2500) / 5000;
outval = (outval * x + 5000) / 10000;
outval -= (10 * x * x + 10433) / 20866;
outval += 4 * x - 11505;
outb_p((outval >> 8) & 0x01, isa->io + 4);
outb_p(outval >> 9, isa->io + 6);
outb_p(outval & 0xff, isa->io + 8);
return 0;
}
static int typhoon_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
struct typhoon *ty = container_of(isa, struct typhoon, isa);
if (mute)
vol = 0;
vol >>= 14; /* Map 16 bit to 2 bit */
vol &= 3;
outb_p(vol / 2, isa->io); /* Set the volume, high bit. */
outb_p(vol % 2, isa->io + 2); /* Set the volume, low bit. */
if (vol == 0 && !ty->muted) {
ty->muted = true;
return typhoon_s_frequency(isa, mutefreq << 4);
}
if (vol && ty->muted) {
ty->muted = false;
return typhoon_s_frequency(isa, isa->freq);
}
return 0;
}
static const struct radio_isa_ops typhoon_ops = {
.alloc = typhoon_alloc,
.s_mute_volume = typhoon_s_mute_volume,
.s_frequency = typhoon_s_frequency,
};
static const int typhoon_ioports[] = { 0x316, 0x336 };
static struct radio_isa_driver typhoon_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-typhoon",
},
},
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = typhoon_ioports,
.num_of_io_ports = ARRAY_SIZE(typhoon_ioports),
.region_size = 8,
.card = "Typhoon Radio",
.ops = &typhoon_ops,
.has_stereo = true,
.max_volume = 3,
};
static int __init typhoon_init(void)
{
if (mutefreq < 87000 || mutefreq > 108000) {
printk(KERN_ERR "%s: You must set a frequency (in kHz) used when muting the card,\n",
typhoon_driver.driver.driver.name);
printk(KERN_ERR "%s: e.g. with \"mutefreq=87500\" (87000 <= mutefreq <= 108000)\n",
typhoon_driver.driver.driver.name);
return -ENODEV;
}
return isa_register_driver(&typhoon_driver.driver, TYPHOON_MAX);
}
static void __exit typhoon_exit(void)
{
isa_unregister_driver(&typhoon_driver.driver);
}
module_init(typhoon_init);
module_exit(typhoon_exit);
| linux-master | drivers/media/radio/radio-typhoon.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* tef6862.c Philips TEF6862 Car Radio Enhanced Selectivity Tuner
* Copyright (c) 2009 Intel Corporation
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#define DRIVER_NAME "tef6862"
#define FREQ_MUL 16000
#define TEF6862_LO_FREQ (875U * FREQ_MUL / 10)
#define TEF6862_HI_FREQ (108U * FREQ_MUL)
/* Write mode sub addresses */
#define WM_SUB_BANDWIDTH 0x0
#define WM_SUB_PLLM 0x1
#define WM_SUB_PLLL 0x2
#define WM_SUB_DAA 0x3
#define WM_SUB_AGC 0x4
#define WM_SUB_BAND 0x5
#define WM_SUB_CONTROL 0x6
#define WM_SUB_LEVEL 0x7
#define WM_SUB_IFCF 0x8
#define WM_SUB_IFCAP 0x9
#define WM_SUB_ACD 0xA
#define WM_SUB_TEST 0xF
/* Different modes of the MSA register */
#define MSA_MODE_BUFFER 0x0
#define MSA_MODE_PRESET 0x1
#define MSA_MODE_SEARCH 0x2
#define MSA_MODE_AF_UPDATE 0x3
#define MSA_MODE_JUMP 0x4
#define MSA_MODE_CHECK 0x5
#define MSA_MODE_LOAD 0x6
#define MSA_MODE_END 0x7
#define MSA_MODE_SHIFT 5
struct tef6862_state {
struct v4l2_subdev sd;
unsigned long freq;
};
static inline struct tef6862_state *to_state(struct v4l2_subdev *sd)
{
return container_of(sd, struct tef6862_state, sd);
}
static u16 tef6862_sigstr(struct i2c_client *client)
{
u8 buf[4];
int err = i2c_master_recv(client, buf, sizeof(buf));
if (err == sizeof(buf))
return buf[3] << 8;
return 0;
}
static int tef6862_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
{
if (v->index > 0)
return -EINVAL;
/* only support FM for now */
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = TEF6862_LO_FREQ;
v->rangehigh = TEF6862_HI_FREQ;
v->rxsubchans = V4L2_TUNER_SUB_MONO;
v->capability = V4L2_TUNER_CAP_LOW;
v->audmode = V4L2_TUNER_MODE_STEREO;
v->signal = tef6862_sigstr(v4l2_get_subdevdata(sd));
return 0;
}
static int tef6862_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
{
return v->index ? -EINVAL : 0;
}
static int tef6862_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
{
struct tef6862_state *state = to_state(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
unsigned freq = f->frequency;
u16 pll;
u8 i2cmsg[3];
int err;
if (f->tuner != 0)
return -EINVAL;
freq = clamp(freq, TEF6862_LO_FREQ, TEF6862_HI_FREQ);
pll = 1964 + ((freq - TEF6862_LO_FREQ) * 20) / FREQ_MUL;
i2cmsg[0] = (MSA_MODE_PRESET << MSA_MODE_SHIFT) | WM_SUB_PLLM;
i2cmsg[1] = (pll >> 8) & 0xff;
i2cmsg[2] = pll & 0xff;
err = i2c_master_send(client, i2cmsg, sizeof(i2cmsg));
if (err != sizeof(i2cmsg))
return err < 0 ? err : -EIO;
state->freq = freq;
return 0;
}
static int tef6862_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
{
struct tef6862_state *state = to_state(sd);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = state->freq;
return 0;
}
static const struct v4l2_subdev_tuner_ops tef6862_tuner_ops = {
.g_tuner = tef6862_g_tuner,
.s_tuner = tef6862_s_tuner,
.s_frequency = tef6862_s_frequency,
.g_frequency = tef6862_g_frequency,
};
static const struct v4l2_subdev_ops tef6862_ops = {
.tuner = &tef6862_tuner_ops,
};
/*
* Generic i2c probe
* concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
*/
static int tef6862_probe(struct i2c_client *client)
{
struct tef6862_state *state;
struct v4l2_subdev *sd;
/* Check if the adapter supports the needed features */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
return -EIO;
v4l_info(client, "chip found @ 0x%02x (%s)\n",
client->addr << 1, client->adapter->name);
state = kzalloc(sizeof(struct tef6862_state), GFP_KERNEL);
if (state == NULL)
return -ENOMEM;
state->freq = TEF6862_LO_FREQ;
sd = &state->sd;
v4l2_i2c_subdev_init(sd, client, &tef6862_ops);
return 0;
}
static void tef6862_remove(struct i2c_client *client)
{
struct v4l2_subdev *sd = i2c_get_clientdata(client);
v4l2_device_unregister_subdev(sd);
kfree(to_state(sd));
}
static const struct i2c_device_id tef6862_id[] = {
{DRIVER_NAME, 0},
{},
};
MODULE_DEVICE_TABLE(i2c, tef6862_id);
static struct i2c_driver tef6862_driver = {
.driver = {
.name = DRIVER_NAME,
},
.probe = tef6862_probe,
.remove = tef6862_remove,
.id_table = tef6862_id,
};
module_i2c_driver(tef6862_driver);
MODULE_DESCRIPTION("TEF6862 Car Radio Enhanced Selectivity Tuner");
MODULE_AUTHOR("Mocean Laboratories");
MODULE_LICENSE("GPL v2");
| linux-master | drivers/media/radio/tef6862.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* GemTek radio card driver
*
* Copyright 1998 Jonas Munsin <[email protected]>
*
* GemTek hasn't released any specs on the card, so the protocol had to
* be reverse engineered with dosemu.
*
* Besides the protocol changes, this is mostly a copy of:
*
* RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
*
* Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
* Converted to new API by Alan Cox <[email protected]>
* Various bugfixes and enhancements by Russell Kroll <[email protected]>
*
* Converted to the radio-isa framework by Hans Verkuil <[email protected]>
* Converted to V4L2 API by Mauro Carvalho Chehab <[email protected]>
*
* Note: this card seems to swap the left and right audio channels!
*
* Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* udelay */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/mutex.h>
#include <linux/io.h> /* outb, outb_p */
#include <linux/pnp.h>
#include <linux/slab.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include "radio-isa.h"
/*
* Module info.
*/
MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <[email protected]>");
MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0");
/*
* Module params.
*/
#ifndef CONFIG_RADIO_GEMTEK_PORT
#define CONFIG_RADIO_GEMTEK_PORT -1
#endif
#ifndef CONFIG_RADIO_GEMTEK_PROBE
#define CONFIG_RADIO_GEMTEK_PROBE 1
#endif
#define GEMTEK_MAX 4
static bool probe = CONFIG_RADIO_GEMTEK_PROBE;
static bool hardmute;
static int io[GEMTEK_MAX] = { [0] = CONFIG_RADIO_GEMTEK_PORT,
[1 ... (GEMTEK_MAX - 1)] = -1 };
static int radio_nr[GEMTEK_MAX] = { [0 ... (GEMTEK_MAX - 1)] = -1 };
module_param(probe, bool, 0444);
MODULE_PARM_DESC(probe, "Enable automatic device probing.");
module_param(hardmute, bool, 0644);
MODULE_PARM_DESC(hardmute, "Enable 'hard muting' by shutting down PLL, may reduce static noise.");
module_param_array(io, int, NULL, 0444);
MODULE_PARM_DESC(io, "Force I/O ports for the GemTek Radio card if automatic probing is disabled or fails. The most common I/O ports are: 0x20c 0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to work for the combined sound/radiocard).");
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
/*
* Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
* value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
*/
#define FSCALE 8
#define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
#define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
#define GEMTEK_CK 0x01 /* Clock signal */
#define GEMTEK_DA 0x02 /* Serial data */
#define GEMTEK_CE 0x04 /* Chip enable */
#define GEMTEK_NS 0x08 /* No signal */
#define GEMTEK_MT 0x10 /* Line mute */
#define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
#define GEMTEK_PLL_OFF 0x07 /* PLL off */
#define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
#define SHORT_DELAY 5 /* usec */
#define LONG_DELAY 75 /* usec */
struct gemtek {
struct radio_isa_card isa;
bool muted;
u32 bu2614data;
};
#define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
#define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
#define BU2614_VOID_BITS 4 /* unused */
#define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
#define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
#define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
#define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
#define BU2614_VOID2_BITS 1 /* unused */
#define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
#define BU2614_TEST_BITS 1 /* TS, Test data is input */
#define BU2614_FREQ_SHIFT 0
#define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
#define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
#define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
#define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
#define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
#define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
#define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
#define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
#define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
#define MKMASK(field) (((1UL<<BU2614_##field##_BITS) - 1) << \
BU2614_##field##_SHIFT)
#define BU2614_PORT_MASK MKMASK(PORT)
#define BU2614_FREQ_MASK MKMASK(FREQ)
#define BU2614_VOID_MASK MKMASK(VOID)
#define BU2614_FMES_MASK MKMASK(FMES)
#define BU2614_STDF_MASK MKMASK(STDF)
#define BU2614_SWIN_MASK MKMASK(SWIN)
#define BU2614_SWAL_MASK MKMASK(SWAL)
#define BU2614_VOID2_MASK MKMASK(VOID2)
#define BU2614_FMUN_MASK MKMASK(FMUN)
#define BU2614_TEST_MASK MKMASK(TEST)
/*
* Set data which will be sent to BU2614FS.
*/
#define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
/*
* Transmit settings to BU2614FS over GemTek IC.
*/
static void gemtek_bu2614_transmit(struct gemtek *gt)
{
struct radio_isa_card *isa = >->isa;
int i, bit, q, mute;
mute = gt->muted ? GEMTEK_MT : 0x00;
outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, isa->io);
udelay(LONG_DELAY);
for (i = 0, q = gt->bu2614data; i < 32; i++, q >>= 1) {
bit = (q & 1) ? GEMTEK_DA : 0;
outb_p(mute | GEMTEK_CE | bit, isa->io);
udelay(SHORT_DELAY);
outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, isa->io);
udelay(SHORT_DELAY);
}
outb_p(mute | GEMTEK_DA | GEMTEK_CK, isa->io);
udelay(SHORT_DELAY);
}
/*
* Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
*/
static unsigned long gemtek_convfreq(unsigned long freq)
{
return ((freq << FSCALE) + IF_OFFSET + REF_FREQ / 2) / REF_FREQ;
}
static struct radio_isa_card *gemtek_alloc(void)
{
struct gemtek *gt = kzalloc(sizeof(*gt), GFP_KERNEL);
if (gt)
gt->muted = true;
return gt ? >->isa : NULL;
}
/*
* Set FM-frequency.
*/
static int gemtek_s_frequency(struct radio_isa_card *isa, u32 freq)
{
struct gemtek *gt = container_of(isa, struct gemtek, isa);
if (hardmute && gt->muted)
return 0;
gemtek_bu2614_set(gt, BU2614_PORT, 0);
gemtek_bu2614_set(gt, BU2614_FMES, 0);
gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
gemtek_bu2614_set(gt, BU2614_SWAL, 0);
gemtek_bu2614_set(gt, BU2614_FMUN, 1); /* GT bit set */
gemtek_bu2614_set(gt, BU2614_TEST, 0);
gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
gemtek_bu2614_set(gt, BU2614_FREQ, gemtek_convfreq(freq));
gemtek_bu2614_transmit(gt);
return 0;
}
/*
* Set mute flag.
*/
static int gemtek_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
{
struct gemtek *gt = container_of(isa, struct gemtek, isa);
int i;
gt->muted = mute;
if (hardmute) {
if (!mute)
return gemtek_s_frequency(isa, isa->freq);
/* Turn off PLL, disable data output */
gemtek_bu2614_set(gt, BU2614_PORT, 0);
gemtek_bu2614_set(gt, BU2614_FMES, 0); /* CT bit off */
gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
gemtek_bu2614_set(gt, BU2614_SWAL, 0);
gemtek_bu2614_set(gt, BU2614_FMUN, 0); /* GT bit off */
gemtek_bu2614_set(gt, BU2614_TEST, 0);
gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_PLL_OFF);
gemtek_bu2614_set(gt, BU2614_FREQ, 0);
gemtek_bu2614_transmit(gt);
return 0;
}
/* Read bus contents (CE, CK and DA). */
i = inb_p(isa->io);
/* Write it back with mute flag set. */
outb_p((i >> 5) | (mute ? GEMTEK_MT : 0), isa->io);
udelay(SHORT_DELAY);
return 0;
}
static u32 gemtek_g_rxsubchans(struct radio_isa_card *isa)
{
if (inb_p(isa->io) & GEMTEK_NS)
return V4L2_TUNER_SUB_MONO;
return V4L2_TUNER_SUB_STEREO;
}
/*
* Check if requested card acts like GemTek Radio card.
*/
static bool gemtek_probe(struct radio_isa_card *isa, int io)
{
int i, q;
q = inb_p(io); /* Read bus contents before probing. */
/* Try to turn on CE, CK and DA respectively and check if card responds
properly. */
for (i = 0; i < 3; ++i) {
outb_p(1 << i, io);
udelay(SHORT_DELAY);
if ((inb_p(io) & ~GEMTEK_NS) != (0x17 | (1 << (i + 5))))
return false;
}
outb_p(q >> 5, io); /* Write bus contents back. */
udelay(SHORT_DELAY);
return true;
}
static const struct radio_isa_ops gemtek_ops = {
.alloc = gemtek_alloc,
.probe = gemtek_probe,
.s_mute_volume = gemtek_s_mute_volume,
.s_frequency = gemtek_s_frequency,
.g_rxsubchans = gemtek_g_rxsubchans,
};
static const int gemtek_ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
#ifdef CONFIG_PNP
static const struct pnp_device_id gemtek_pnp_devices[] = {
/* AOpen FX-3D/Pro Radio */
{.id = "ADS7183", .driver_data = 0},
{.id = ""}
};
MODULE_DEVICE_TABLE(pnp, gemtek_pnp_devices);
#endif
static struct radio_isa_driver gemtek_driver = {
.driver = {
.match = radio_isa_match,
.probe = radio_isa_probe,
.remove = radio_isa_remove,
.driver = {
.name = "radio-gemtek",
},
},
#ifdef CONFIG_PNP
.pnp_driver = {
.name = "radio-gemtek",
.id_table = gemtek_pnp_devices,
.probe = radio_isa_pnp_probe,
.remove = radio_isa_pnp_remove,
},
#endif
.io_params = io,
.radio_nr_params = radio_nr,
.io_ports = gemtek_ioports,
.num_of_io_ports = ARRAY_SIZE(gemtek_ioports),
.region_size = 1,
.card = "GemTek Radio",
.ops = &gemtek_ops,
.has_stereo = true,
};
static int __init gemtek_init(void)
{
gemtek_driver.probe = probe;
#ifdef CONFIG_PNP
pnp_register_driver(&gemtek_driver.pnp_driver);
#endif
return isa_register_driver(&gemtek_driver.driver, GEMTEK_MAX);
}
static void __exit gemtek_exit(void)
{
hardmute = true; /* Turn off PLL */
#ifdef CONFIG_PNP
pnp_unregister_driver(&gemtek_driver.pnp_driver);
#endif
isa_unregister_driver(&gemtek_driver.driver);
}
module_init(gemtek_init);
module_exit(gemtek_exit);
| linux-master | drivers/media/radio/radio-gemtek.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* A driver for the AverMedia MR 800 USB FM radio. This device plugs
* into both the USB and an analog audio input, so this thing
* only deals with initialization and frequency setting, the
* audio data has to be handled by a sound driver.
*
* Copyright (c) 2008 Alexey Klimov <[email protected]>
*/
/*
* Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
*
* When work was looked pretty good, i discover this:
* http://av-usbradio.sourceforge.net/index.php
* http://sourceforge.net/projects/av-usbradio/
* Latest release of theirs project was in 2005.
* Probably, this driver could be improved through using their
* achievements (specifications given).
* Also, Faidon Liambotis <[email protected]> wrote nice driver for this radio
* in 2007. He allowed to use his driver to improve current mr800 radio driver.
* http://www.spinics.net/lists/linux-usb-devel/msg10109.html
*
* Version 0.01: First working version.
* It's required to blacklist AverMedia USB Radio
* in usbhid/hid-quirks.c
* Version 0.10: A lot of cleanups and fixes: unpluging the device,
* few mutex locks were added, codinstyle issues, etc.
* Added stereo support. Thanks to
* Douglas Schilling Landgraf <[email protected]> and
* David Ellingsworth <[email protected]>
* for discussion, help and support.
* Version 0.11: Converted to v4l2_device.
*
* Many things to do:
* - Correct power management of device (suspend & resume)
* - Add code for scanning and smooth tuning
* - Add code for sensitivity value
* - Correct mistakes
* - In Japan another FREQ_MIN and FREQ_MAX
*/
/* kernel includes */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <linux/usb.h>
#include <linux/mutex.h>
/* driver and module definitions */
#define DRIVER_AUTHOR "Alexey Klimov <[email protected]>"
#define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
#define DRIVER_VERSION "0.1.2"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);
#define USB_AMRADIO_VENDOR 0x07ca
#define USB_AMRADIO_PRODUCT 0xb800
/* dev_warn macro with driver name */
#define MR800_DRIVER_NAME "radio-mr800"
#define amradio_dev_warn(dev, fmt, arg...) \
dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
#define amradio_dev_err(dev, fmt, arg...) \
dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
/* Probably USB_TIMEOUT should be modified in module parameter */
#define BUFFER_LENGTH 8
#define USB_TIMEOUT 500
/* Frequency limits in MHz -- these are European values. For Japanese
devices, that would be 76 and 91. */
#define FREQ_MIN 87.5
#define FREQ_MAX 108.0
#define FREQ_MUL 16000
/*
* Commands that device should understand
* List isn't full and will be updated with implementation of new functions
*/
#define AMRADIO_SET_FREQ 0xa4
#define AMRADIO_GET_READY_FLAG 0xa5
#define AMRADIO_GET_SIGNAL 0xa7
#define AMRADIO_GET_FREQ 0xa8
#define AMRADIO_SET_SEARCH_UP 0xa9
#define AMRADIO_SET_SEARCH_DOWN 0xaa
#define AMRADIO_SET_MUTE 0xab
#define AMRADIO_SET_RIGHT_MUTE 0xac
#define AMRADIO_SET_LEFT_MUTE 0xad
#define AMRADIO_SET_MONO 0xae
#define AMRADIO_SET_SEARCH_LVL 0xb0
#define AMRADIO_STOP_SEARCH 0xb1
/* Comfortable defines for amradio_set_stereo */
#define WANT_STEREO 0x00
#define WANT_MONO 0x01
/* module parameter */
static int radio_nr = -1;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "Radio Nr");
/* Data for one (physical) device */
struct amradio_device {
/* reference to USB and video device */
struct usb_device *usbdev;
struct usb_interface *intf;
struct video_device vdev;
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler hdl;
u8 *buffer;
struct mutex lock; /* buffer locking */
int curfreq;
int stereo;
int muted;
};
static inline struct amradio_device *to_amradio_dev(struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev, struct amradio_device, v4l2_dev);
}
static int amradio_send_cmd(struct amradio_device *radio, u8 cmd, u8 arg,
u8 *extra, u8 extralen, bool reply)
{
int retval;
int size;
radio->buffer[0] = 0x00;
radio->buffer[1] = 0x55;
radio->buffer[2] = 0xaa;
radio->buffer[3] = extralen;
radio->buffer[4] = cmd;
radio->buffer[5] = arg;
radio->buffer[6] = 0x00;
radio->buffer[7] = extra || reply ? 8 : 0;
retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
if (retval < 0 || size != BUFFER_LENGTH) {
if (video_is_registered(&radio->vdev))
amradio_dev_warn(&radio->vdev.dev,
"cmd %02x failed\n", cmd);
return retval ? retval : -EIO;
}
if (!extra && !reply)
return 0;
if (extra) {
memcpy(radio->buffer, extra, extralen);
memset(radio->buffer + extralen, 0, 8 - extralen);
retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
} else {
memset(radio->buffer, 0, 8);
retval = usb_bulk_msg(radio->usbdev, usb_rcvbulkpipe(radio->usbdev, 0x81),
radio->buffer, BUFFER_LENGTH, &size, USB_TIMEOUT);
}
if (retval == 0 && size == BUFFER_LENGTH)
return 0;
if (video_is_registered(&radio->vdev) && cmd != AMRADIO_GET_READY_FLAG)
amradio_dev_warn(&radio->vdev.dev, "follow-up to cmd %02x failed\n", cmd);
return retval ? retval : -EIO;
}
/* switch on/off the radio. Send 8 bytes to device */
static int amradio_set_mute(struct amradio_device *radio, bool mute)
{
int ret = amradio_send_cmd(radio,
AMRADIO_SET_MUTE, mute, NULL, 0, false);
if (!ret)
radio->muted = mute;
return ret;
}
/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
static int amradio_set_freq(struct amradio_device *radio, int freq)
{
unsigned short freq_send;
u8 buf[3];
int retval;
/* we need to be sure that frequency isn't out of range */
freq = clamp_t(unsigned, freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
freq_send = 0x10 + (freq >> 3) / 25;
/* frequency is calculated from freq_send and placed in first 2 bytes */
buf[0] = (freq_send >> 8) & 0xff;
buf[1] = freq_send & 0xff;
buf[2] = 0x01;
retval = amradio_send_cmd(radio, AMRADIO_SET_FREQ, 0, buf, 3, false);
if (retval)
return retval;
radio->curfreq = freq;
msleep(40);
return 0;
}
static int amradio_set_stereo(struct amradio_device *radio, bool stereo)
{
int ret = amradio_send_cmd(radio,
AMRADIO_SET_MONO, !stereo, NULL, 0, false);
if (!ret)
radio->stereo = stereo;
return ret;
}
static int amradio_get_stat(struct amradio_device *radio, bool *is_stereo, u32 *signal)
{
int ret = amradio_send_cmd(radio,
AMRADIO_GET_SIGNAL, 0, NULL, 0, true);
if (ret)
return ret;
*is_stereo = radio->buffer[2] >> 7;
*signal = (radio->buffer[3] & 0xf0) << 8;
return 0;
}
/* Handle unplugging the device.
* We call video_unregister_device in any case.
* The last function called in this procedure is
* usb_amradio_device_release.
*/
static void usb_amradio_disconnect(struct usb_interface *intf)
{
struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
mutex_lock(&radio->lock);
video_unregister_device(&radio->vdev);
amradio_set_mute(radio, true);
usb_set_intfdata(intf, NULL);
v4l2_device_disconnect(&radio->v4l2_dev);
mutex_unlock(&radio->lock);
v4l2_device_put(&radio->v4l2_dev);
}
/* vidioc_querycap - query device capabilities */
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct amradio_device *radio = video_drvdata(file);
strscpy(v->driver, "radio-mr800", sizeof(v->driver));
strscpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
return 0;
}
/* vidioc_g_tuner - get tuner attributes */
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct amradio_device *radio = video_drvdata(file);
bool is_stereo = false;
int retval;
if (v->index > 0)
return -EINVAL;
v->signal = 0;
retval = amradio_get_stat(radio, &is_stereo, &v->signal);
if (retval)
return retval;
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = FREQ_MIN * FREQ_MUL;
v->rangehigh = FREQ_MAX * FREQ_MUL;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_HWSEEK_WRAP;
v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
v->audmode = radio->stereo ?
V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
return 0;
}
/* vidioc_s_tuner - set tuner attributes */
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct amradio_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
/* mono/stereo selector */
switch (v->audmode) {
case V4L2_TUNER_MODE_MONO:
return amradio_set_stereo(radio, WANT_MONO);
default:
return amradio_set_stereo(radio, WANT_STEREO);
}
}
/* vidioc_s_frequency - set tuner radio frequency */
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct amradio_device *radio = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
return amradio_set_freq(radio, f->frequency);
}
/* vidioc_g_frequency - get tuner radio frequency */
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct amradio_device *radio = video_drvdata(file);
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = radio->curfreq;
return 0;
}
static int vidioc_s_hw_freq_seek(struct file *file, void *priv,
const struct v4l2_hw_freq_seek *seek)
{
static u8 buf[8] = {
0x3d, 0x32, 0x0f, 0x08, 0x3d, 0x32, 0x0f, 0x08
};
struct amradio_device *radio = video_drvdata(file);
unsigned long timeout;
int retval;
if (seek->tuner != 0 || !seek->wrap_around)
return -EINVAL;
if (file->f_flags & O_NONBLOCK)
return -EWOULDBLOCK;
retval = amradio_send_cmd(radio,
AMRADIO_SET_SEARCH_LVL, 0, buf, 8, false);
if (retval)
return retval;
amradio_set_freq(radio, radio->curfreq);
retval = amradio_send_cmd(radio,
seek->seek_upward ? AMRADIO_SET_SEARCH_UP : AMRADIO_SET_SEARCH_DOWN,
0, NULL, 0, false);
if (retval)
return retval;
timeout = jiffies + msecs_to_jiffies(30000);
for (;;) {
if (time_after(jiffies, timeout)) {
retval = -ENODATA;
break;
}
if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
retval = -ERESTARTSYS;
break;
}
retval = amradio_send_cmd(radio, AMRADIO_GET_READY_FLAG,
0, NULL, 0, true);
if (retval)
continue;
amradio_send_cmd(radio, AMRADIO_GET_FREQ, 0, NULL, 0, true);
if (radio->buffer[1] || radio->buffer[2]) {
/* To check: sometimes radio->curfreq is set to out of range value */
radio->curfreq = (radio->buffer[1] << 8) | radio->buffer[2];
radio->curfreq = (radio->curfreq - 0x10) * 200;
amradio_send_cmd(radio, AMRADIO_STOP_SEARCH,
0, NULL, 0, false);
amradio_set_freq(radio, radio->curfreq);
retval = 0;
break;
}
}
amradio_send_cmd(radio, AMRADIO_STOP_SEARCH, 0, NULL, 0, false);
amradio_set_freq(radio, radio->curfreq);
return retval;
}
static int usb_amradio_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct amradio_device *radio =
container_of(ctrl->handler, struct amradio_device, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
return amradio_set_mute(radio, ctrl->val);
}
return -EINVAL;
}
static int usb_amradio_init(struct amradio_device *radio)
{
int retval;
retval = amradio_set_mute(radio, true);
if (retval)
goto out_err;
retval = amradio_set_stereo(radio, true);
if (retval)
goto out_err;
retval = amradio_set_freq(radio, radio->curfreq);
if (retval)
goto out_err;
return 0;
out_err:
amradio_dev_err(&radio->vdev.dev, "initialization failed\n");
return retval;
}
/* Suspend device - stop device. Need to be checked and fixed */
static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
{
struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
mutex_lock(&radio->lock);
if (!radio->muted) {
amradio_set_mute(radio, true);
radio->muted = false;
}
mutex_unlock(&radio->lock);
dev_info(&intf->dev, "going into suspend..\n");
return 0;
}
/* Resume device - start device. Need to be checked and fixed */
static int usb_amradio_resume(struct usb_interface *intf)
{
struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
mutex_lock(&radio->lock);
amradio_set_stereo(radio, radio->stereo);
amradio_set_freq(radio, radio->curfreq);
if (!radio->muted)
amradio_set_mute(radio, false);
mutex_unlock(&radio->lock);
dev_info(&intf->dev, "coming out of suspend..\n");
return 0;
}
static const struct v4l2_ctrl_ops usb_amradio_ctrl_ops = {
.s_ctrl = usb_amradio_s_ctrl,
};
/* File system interface */
static const struct v4l2_file_operations usb_amradio_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops usb_amradio_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_s_hw_freq_seek = vidioc_s_hw_freq_seek,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static void usb_amradio_release(struct v4l2_device *v4l2_dev)
{
struct amradio_device *radio = to_amradio_dev(v4l2_dev);
/* free rest memory */
v4l2_ctrl_handler_free(&radio->hdl);
v4l2_device_unregister(&radio->v4l2_dev);
kfree(radio->buffer);
kfree(radio);
}
/* check if the device is present and register with v4l and usb if it is */
static int usb_amradio_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct amradio_device *radio;
int retval;
radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
if (!radio) {
dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
retval = -ENOMEM;
goto err;
}
radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
if (!radio->buffer) {
dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
retval = -ENOMEM;
goto err_nobuf;
}
retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
if (retval < 0) {
dev_err(&intf->dev, "couldn't register v4l2_device\n");
goto err_v4l2;
}
v4l2_ctrl_handler_init(&radio->hdl, 1);
v4l2_ctrl_new_std(&radio->hdl, &usb_amradio_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
if (radio->hdl.error) {
retval = radio->hdl.error;
dev_err(&intf->dev, "couldn't register control\n");
goto err_ctrl;
}
mutex_init(&radio->lock);
radio->v4l2_dev.ctrl_handler = &radio->hdl;
radio->v4l2_dev.release = usb_amradio_release;
strscpy(radio->vdev.name, radio->v4l2_dev.name,
sizeof(radio->vdev.name));
radio->vdev.v4l2_dev = &radio->v4l2_dev;
radio->vdev.fops = &usb_amradio_fops;
radio->vdev.ioctl_ops = &usb_amradio_ioctl_ops;
radio->vdev.release = video_device_release_empty;
radio->vdev.lock = &radio->lock;
radio->vdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER |
V4L2_CAP_HW_FREQ_SEEK;
radio->usbdev = interface_to_usbdev(intf);
radio->intf = intf;
usb_set_intfdata(intf, &radio->v4l2_dev);
radio->curfreq = 95.16 * FREQ_MUL;
video_set_drvdata(&radio->vdev, radio);
retval = usb_amradio_init(radio);
if (retval)
goto err_vdev;
retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
radio_nr);
if (retval < 0) {
dev_err(&intf->dev, "could not register video device\n");
goto err_vdev;
}
return 0;
err_vdev:
v4l2_ctrl_handler_free(&radio->hdl);
err_ctrl:
v4l2_device_unregister(&radio->v4l2_dev);
err_v4l2:
kfree(radio->buffer);
err_nobuf:
kfree(radio);
err:
return retval;
}
/* USB Device ID List */
static const struct usb_device_id usb_amradio_device_table[] = {
{ USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
USB_CLASS_HID, 0, 0) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
/* USB subsystem interface */
static struct usb_driver usb_amradio_driver = {
.name = MR800_DRIVER_NAME,
.probe = usb_amradio_probe,
.disconnect = usb_amradio_disconnect,
.suspend = usb_amradio_suspend,
.resume = usb_amradio_resume,
.reset_resume = usb_amradio_resume,
.id_table = usb_amradio_device_table,
};
module_usb_driver(usb_amradio_driver);
| linux-master | drivers/media/radio/radio-mr800.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips
*
* Copyright (c) 2004 Jaroslav Kysela <[email protected]>
*/
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <asm/io.h>
#include <media/v4l2-device.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
#include <media/drv-intf/tea575x.h>
MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
MODULE_LICENSE("GPL");
/*
* definitions
*/
#define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
#define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
#define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
#define TEA575X_BIT_BAND_MASK (3<<20)
#define TEA575X_BIT_BAND_FM (0<<20)
#define TEA575X_BIT_BAND_MW (1<<20)
#define TEA575X_BIT_BAND_LW (2<<20)
#define TEA575X_BIT_BAND_SW (3<<20)
#define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
#define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
#define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
#define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
#define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
#define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
#define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
#define TEA575X_BIT_DUMMY (1<<15) /* buffer */
#define TEA575X_BIT_FREQ_MASK 0x7fff
enum { BAND_FM, BAND_FM_JAPAN, BAND_AM };
static const struct v4l2_frequency_band bands[] = {
{
.type = V4L2_TUNER_RADIO,
.index = 0,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 87500 * 16,
.rangehigh = 108000 * 16,
.modulation = V4L2_BAND_MODULATION_FM,
},
{
.type = V4L2_TUNER_RADIO,
.index = 0,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 76000 * 16,
.rangehigh = 91000 * 16,
.modulation = V4L2_BAND_MODULATION_FM,
},
{
.type = V4L2_TUNER_RADIO,
.index = 1,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 530 * 16,
.rangehigh = 1710 * 16,
.modulation = V4L2_BAND_MODULATION_AM,
},
};
/*
* lowlevel part
*/
static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val)
{
u16 l;
u8 data;
if (tea->ops->write_val)
return tea->ops->write_val(tea, val);
tea->ops->set_direction(tea, 1);
udelay(16);
for (l = 25; l > 0; l--) {
data = (val >> 24) & TEA575X_DATA;
val <<= 1; /* shift data */
tea->ops->set_pins(tea, data | TEA575X_WREN);
udelay(2);
tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK);
udelay(2);
tea->ops->set_pins(tea, data | TEA575X_WREN);
udelay(2);
}
if (!tea->mute)
tea->ops->set_pins(tea, 0);
}
static u32 snd_tea575x_read(struct snd_tea575x *tea)
{
u16 l, rdata;
u32 data = 0;
if (tea->ops->read_val)
return tea->ops->read_val(tea);
tea->ops->set_direction(tea, 0);
tea->ops->set_pins(tea, 0);
udelay(16);
for (l = 24; l--;) {
tea->ops->set_pins(tea, TEA575X_CLK);
udelay(2);
if (!l)
tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1;
tea->ops->set_pins(tea, 0);
udelay(2);
data <<= 1; /* shift data */
rdata = tea->ops->get_pins(tea);
if (!l)
tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1;
if (rdata & TEA575X_DATA)
data++;
udelay(2);
}
if (tea->mute)
tea->ops->set_pins(tea, TEA575X_WREN);
return data;
}
static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val)
{
u32 freq = val & TEA575X_BIT_FREQ_MASK;
if (freq == 0)
return freq;
switch (tea->band) {
case BAND_FM:
/* freq *= 12.5 */
freq *= 125;
freq /= 10;
/* crystal fixup */
freq -= TEA575X_FMIF;
break;
case BAND_FM_JAPAN:
/* freq *= 12.5 */
freq *= 125;
freq /= 10;
/* crystal fixup */
freq += TEA575X_FMIF;
break;
case BAND_AM:
/* crystal fixup */
freq -= TEA575X_AMIF;
break;
}
return clamp(freq * 16, bands[tea->band].rangelow,
bands[tea->band].rangehigh); /* from kHz */
}
static u32 snd_tea575x_get_freq(struct snd_tea575x *tea)
{
return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea));
}
void snd_tea575x_set_freq(struct snd_tea575x *tea)
{
u32 freq = tea->freq / 16; /* to kHz */
u32 band = 0;
switch (tea->band) {
case BAND_FM:
band = TEA575X_BIT_BAND_FM;
/* crystal fixup */
freq += TEA575X_FMIF;
/* freq /= 12.5 */
freq *= 10;
freq /= 125;
break;
case BAND_FM_JAPAN:
band = TEA575X_BIT_BAND_FM;
/* crystal fixup */
freq -= TEA575X_FMIF;
/* freq /= 12.5 */
freq *= 10;
freq /= 125;
break;
case BAND_AM:
band = TEA575X_BIT_BAND_MW;
/* crystal fixup */
freq += TEA575X_AMIF;
break;
}
tea->val &= ~(TEA575X_BIT_FREQ_MASK | TEA575X_BIT_BAND_MASK);
tea->val |= band;
tea->val |= freq & TEA575X_BIT_FREQ_MASK;
snd_tea575x_write(tea, tea->val);
tea->freq = snd_tea575x_val_to_freq(tea, tea->val);
}
EXPORT_SYMBOL(snd_tea575x_set_freq);
/*
* Linux Video interface
*/
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct snd_tea575x *tea = video_drvdata(file);
strscpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
strscpy(v->card, tea->card, sizeof(v->card));
strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
strscpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
return 0;
}
int snd_tea575x_enum_freq_bands(struct snd_tea575x *tea,
struct v4l2_frequency_band *band)
{
int index;
if (band->tuner != 0)
return -EINVAL;
switch (band->index) {
case 0:
if (tea->tea5759)
index = BAND_FM_JAPAN;
else
index = BAND_FM;
break;
case 1:
if (tea->has_am) {
index = BAND_AM;
break;
}
fallthrough;
default:
return -EINVAL;
}
*band = bands[index];
if (!tea->cannot_read_data)
band->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
return 0;
}
EXPORT_SYMBOL(snd_tea575x_enum_freq_bands);
static int vidioc_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
struct snd_tea575x *tea = video_drvdata(file);
return snd_tea575x_enum_freq_bands(tea, band);
}
int snd_tea575x_g_tuner(struct snd_tea575x *tea, struct v4l2_tuner *v)
{
struct v4l2_frequency_band band_fm = { 0, };
if (v->index > 0)
return -EINVAL;
snd_tea575x_read(tea);
snd_tea575x_enum_freq_bands(tea, &band_fm);
memset(v, 0, sizeof(*v));
strscpy(v->name, tea->has_am ? "FM/AM" : "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->capability = band_fm.capability;
v->rangelow = tea->has_am ? bands[BAND_AM].rangelow : band_fm.rangelow;
v->rangehigh = band_fm.rangehigh;
v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
v->audmode = (tea->val & TEA575X_BIT_MONO) ?
V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO;
v->signal = tea->tuned ? 0xffff : 0;
return 0;
}
EXPORT_SYMBOL(snd_tea575x_g_tuner);
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct snd_tea575x *tea = video_drvdata(file);
return snd_tea575x_g_tuner(tea, v);
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct snd_tea575x *tea = video_drvdata(file);
u32 orig_val = tea->val;
if (v->index)
return -EINVAL;
tea->val &= ~TEA575X_BIT_MONO;
if (v->audmode == V4L2_TUNER_MODE_MONO)
tea->val |= TEA575X_BIT_MONO;
/* Only apply changes if currently tuning FM */
if (tea->band != BAND_AM && tea->val != orig_val)
snd_tea575x_set_freq(tea);
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct snd_tea575x *tea = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = tea->freq;
return 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct snd_tea575x *tea = video_drvdata(file);
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
if (tea->has_am && f->frequency < (20000 * 16))
tea->band = BAND_AM;
else if (tea->tea5759)
tea->band = BAND_FM_JAPAN;
else
tea->band = BAND_FM;
tea->freq = clamp_t(u32, f->frequency, bands[tea->band].rangelow,
bands[tea->band].rangehigh);
snd_tea575x_set_freq(tea);
return 0;
}
int snd_tea575x_s_hw_freq_seek(struct file *file, struct snd_tea575x *tea,
const struct v4l2_hw_freq_seek *a)
{
unsigned long timeout;
int i, spacing;
if (tea->cannot_read_data)
return -ENOTTY;
if (a->tuner || a->wrap_around)
return -EINVAL;
if (file->f_flags & O_NONBLOCK)
return -EWOULDBLOCK;
if (a->rangelow || a->rangehigh) {
for (i = 0; i < ARRAY_SIZE(bands); i++) {
if ((i == BAND_FM && tea->tea5759) ||
(i == BAND_FM_JAPAN && !tea->tea5759) ||
(i == BAND_AM && !tea->has_am))
continue;
if (bands[i].rangelow == a->rangelow &&
bands[i].rangehigh == a->rangehigh)
break;
}
if (i == ARRAY_SIZE(bands))
return -EINVAL; /* No matching band found */
if (i != tea->band) {
tea->band = i;
tea->freq = clamp(tea->freq, bands[i].rangelow,
bands[i].rangehigh);
snd_tea575x_set_freq(tea);
}
}
spacing = (tea->band == BAND_AM) ? 5 : 50; /* kHz */
/* clear the frequency, HW will fill it in */
tea->val &= ~TEA575X_BIT_FREQ_MASK;
tea->val |= TEA575X_BIT_SEARCH;
if (a->seek_upward)
tea->val |= TEA575X_BIT_UPDOWN;
else
tea->val &= ~TEA575X_BIT_UPDOWN;
snd_tea575x_write(tea, tea->val);
timeout = jiffies + msecs_to_jiffies(10000);
for (;;) {
if (time_after(jiffies, timeout))
break;
if (schedule_timeout_interruptible(msecs_to_jiffies(10))) {
/* some signal arrived, stop search */
tea->val &= ~TEA575X_BIT_SEARCH;
snd_tea575x_set_freq(tea);
return -ERESTARTSYS;
}
if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) {
u32 freq;
/* Found a frequency, wait until it can be read */
for (i = 0; i < 100; i++) {
msleep(10);
freq = snd_tea575x_get_freq(tea);
if (freq) /* available */
break;
}
if (freq == 0) /* shouldn't happen */
break;
/*
* if we moved by less than the spacing, or in the
* wrong direction, continue seeking
*/
if (abs(tea->freq - freq) < 16 * spacing ||
(a->seek_upward && freq < tea->freq) ||
(!a->seek_upward && freq > tea->freq)) {
snd_tea575x_write(tea, tea->val);
continue;
}
tea->freq = freq;
tea->val &= ~TEA575X_BIT_SEARCH;
return 0;
}
}
tea->val &= ~TEA575X_BIT_SEARCH;
snd_tea575x_set_freq(tea);
return -ENODATA;
}
EXPORT_SYMBOL(snd_tea575x_s_hw_freq_seek);
static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
const struct v4l2_hw_freq_seek *a)
{
struct snd_tea575x *tea = video_drvdata(file);
return snd_tea575x_s_hw_freq_seek(file, tea, a);
}
static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
tea->mute = ctrl->val;
snd_tea575x_set_freq(tea);
return 0;
}
return -EINVAL;
}
static const struct v4l2_file_operations tea575x_fops = {
.unlocked_ioctl = video_ioctl2,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
};
static const struct v4l2_ioctl_ops tea575x_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_s_hw_freq_seek = vidioc_s_hw_freq_seek,
.vidioc_enum_freq_bands = vidioc_enum_freq_bands,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct video_device tea575x_radio = {
.ioctl_ops = &tea575x_ioctl_ops,
.release = video_device_release_empty,
};
static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
.s_ctrl = tea575x_s_ctrl,
};
int snd_tea575x_hw_init(struct snd_tea575x *tea)
{
tea->mute = true;
/* Not all devices can or know how to read the data back.
Such devices can set cannot_read_data to true. */
if (!tea->cannot_read_data) {
snd_tea575x_write(tea, 0x55AA);
if (snd_tea575x_read(tea) != 0x55AA)
return -ENODEV;
}
tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28;
tea->freq = 90500 * 16; /* 90.5Mhz default */
snd_tea575x_set_freq(tea);
return 0;
}
EXPORT_SYMBOL(snd_tea575x_hw_init);
int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner)
{
int retval = snd_tea575x_hw_init(tea);
if (retval)
return retval;
tea->vd = tea575x_radio;
video_set_drvdata(&tea->vd, tea);
mutex_init(&tea->mutex);
strscpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
tea->vd.lock = &tea->mutex;
tea->vd.v4l2_dev = tea->v4l2_dev;
tea->vd.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
if (!tea->cannot_read_data)
tea->vd.device_caps |= V4L2_CAP_HW_FREQ_SEEK;
tea->fops = tea575x_fops;
tea->fops.owner = owner;
tea->vd.fops = &tea->fops;
/* disable hw_freq_seek if we can't use it */
if (tea->cannot_read_data)
v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK);
if (!tea->cannot_mute) {
tea->vd.ctrl_handler = &tea->ctrl_handler;
v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
retval = tea->ctrl_handler.error;
if (retval) {
v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
v4l2_ctrl_handler_free(&tea->ctrl_handler);
return retval;
}
if (tea->ext_init) {
retval = tea->ext_init(tea);
if (retval) {
v4l2_ctrl_handler_free(&tea->ctrl_handler);
return retval;
}
}
v4l2_ctrl_handler_setup(&tea->ctrl_handler);
}
retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr);
if (retval) {
v4l2_err(tea->v4l2_dev, "can't register video device!\n");
v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
return retval;
}
return 0;
}
EXPORT_SYMBOL(snd_tea575x_init);
void snd_tea575x_exit(struct snd_tea575x *tea)
{
video_unregister_device(&tea->vd);
v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
}
EXPORT_SYMBOL(snd_tea575x_exit);
| linux-master | drivers/media/radio/tea575x.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* v4l2 driver for TEA5777 Philips AM/FM radio tuner chips
*
* Copyright (c) 2012 Hans de Goede <[email protected]>
*
* Based on the ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips:
*
* Copyright (c) 2004 Jaroslav Kysela <[email protected]>
*/
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <media/v4l2-device.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
#include "radio-tea5777.h"
MODULE_AUTHOR("Hans de Goede <[email protected]>");
MODULE_DESCRIPTION("Routines for control of TEA5777 Philips AM/FM radio tuner chips");
MODULE_LICENSE("GPL");
#define TEA5777_FM_IF 150 /* kHz */
#define TEA5777_FM_FREQ_STEP 50 /* kHz */
#define TEA5777_AM_IF 21 /* kHz */
#define TEA5777_AM_FREQ_STEP 1 /* kHz */
/* Write reg, common bits */
#define TEA5777_W_MUTE_MASK (1LL << 47)
#define TEA5777_W_MUTE_SHIFT 47
#define TEA5777_W_AM_FM_MASK (1LL << 46)
#define TEA5777_W_AM_FM_SHIFT 46
#define TEA5777_W_STB_MASK (1LL << 45)
#define TEA5777_W_STB_SHIFT 45
#define TEA5777_W_IFCE_MASK (1LL << 29)
#define TEA5777_W_IFCE_SHIFT 29
#define TEA5777_W_IFW_MASK (1LL << 28)
#define TEA5777_W_IFW_SHIFT 28
#define TEA5777_W_HILO_MASK (1LL << 27)
#define TEA5777_W_HILO_SHIFT 27
#define TEA5777_W_DBUS_MASK (1LL << 26)
#define TEA5777_W_DBUS_SHIFT 26
#define TEA5777_W_INTEXT_MASK (1LL << 24)
#define TEA5777_W_INTEXT_SHIFT 24
#define TEA5777_W_P1_MASK (1LL << 23)
#define TEA5777_W_P1_SHIFT 23
#define TEA5777_W_P0_MASK (1LL << 22)
#define TEA5777_W_P0_SHIFT 22
#define TEA5777_W_PEN1_MASK (1LL << 21)
#define TEA5777_W_PEN1_SHIFT 21
#define TEA5777_W_PEN0_MASK (1LL << 20)
#define TEA5777_W_PEN0_SHIFT 20
#define TEA5777_W_CHP0_MASK (1LL << 18)
#define TEA5777_W_CHP0_SHIFT 18
#define TEA5777_W_DEEM_MASK (1LL << 17)
#define TEA5777_W_DEEM_SHIFT 17
#define TEA5777_W_SEARCH_MASK (1LL << 7)
#define TEA5777_W_SEARCH_SHIFT 7
#define TEA5777_W_PROGBLIM_MASK (1LL << 6)
#define TEA5777_W_PROGBLIM_SHIFT 6
#define TEA5777_W_UPDWN_MASK (1LL << 5)
#define TEA5777_W_UPDWN_SHIFT 5
#define TEA5777_W_SLEV_MASK (3LL << 3)
#define TEA5777_W_SLEV_SHIFT 3
/* Write reg, FM specific bits */
#define TEA5777_W_FM_PLL_MASK (0x1fffLL << 32)
#define TEA5777_W_FM_PLL_SHIFT 32
#define TEA5777_W_FM_FREF_MASK (0x03LL << 30)
#define TEA5777_W_FM_FREF_SHIFT 30
#define TEA5777_W_FM_FREF_VALUE 0LL /* 50k steps, 150k IF */
#define TEA5777_W_FM_FORCEMONO_MASK (1LL << 15)
#define TEA5777_W_FM_FORCEMONO_SHIFT 15
#define TEA5777_W_FM_SDSOFF_MASK (1LL << 14)
#define TEA5777_W_FM_SDSOFF_SHIFT 14
#define TEA5777_W_FM_DOFF_MASK (1LL << 13)
#define TEA5777_W_FM_DOFF_SHIFT 13
#define TEA5777_W_FM_STEP_MASK (3LL << 1)
#define TEA5777_W_FM_STEP_SHIFT 1
/* Write reg, AM specific bits */
#define TEA5777_W_AM_PLL_MASK (0x7ffLL << 34)
#define TEA5777_W_AM_PLL_SHIFT 34
#define TEA5777_W_AM_AGCRF_MASK (1LL << 33)
#define TEA5777_W_AM_AGCRF_SHIFT 33
#define TEA5777_W_AM_AGCIF_MASK (1LL << 32)
#define TEA5777_W_AM_AGCIF_SHIFT 32
#define TEA5777_W_AM_MWLW_MASK (1LL << 31)
#define TEA5777_W_AM_MWLW_SHIFT 31
#define TEA5777_W_AM_LW 0LL
#define TEA5777_W_AM_MW 1LL
#define TEA5777_W_AM_LNA_MASK (1LL << 30)
#define TEA5777_W_AM_LNA_SHIFT 30
#define TEA5777_W_AM_PEAK_MASK (1LL << 25)
#define TEA5777_W_AM_PEAK_SHIFT 25
#define TEA5777_W_AM_RFB_MASK (1LL << 16)
#define TEA5777_W_AM_RFB_SHIFT 16
#define TEA5777_W_AM_CALLIGN_MASK (1LL << 15)
#define TEA5777_W_AM_CALLIGN_SHIFT 15
#define TEA5777_W_AM_CBANK_MASK (0x7fLL << 8)
#define TEA5777_W_AM_CBANK_SHIFT 8
#define TEA5777_W_AM_DELAY_MASK (1LL << 2)
#define TEA5777_W_AM_DELAY_SHIFT 2
#define TEA5777_W_AM_STEP_MASK (1LL << 1)
#define TEA5777_W_AM_STEP_SHIFT 1
/* Read reg, common bits */
#define TEA5777_R_LEVEL_MASK (0x0f << 17)
#define TEA5777_R_LEVEL_SHIFT 17
#define TEA5777_R_SFOUND_MASK (0x01 << 16)
#define TEA5777_R_SFOUND_SHIFT 16
#define TEA5777_R_BLIM_MASK (0x01 << 15)
#define TEA5777_R_BLIM_SHIFT 15
/* Read reg, FM specific bits */
#define TEA5777_R_FM_STEREO_MASK (0x01 << 21)
#define TEA5777_R_FM_STEREO_SHIFT 21
#define TEA5777_R_FM_PLL_MASK 0x1fff
#define TEA5777_R_FM_PLL_SHIFT 0
enum { BAND_FM, BAND_AM };
static const struct v4l2_frequency_band bands[] = {
{
.type = V4L2_TUNER_RADIO,
.index = 0,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS |
V4L2_TUNER_CAP_HWSEEK_BOUNDED |
V4L2_TUNER_CAP_HWSEEK_PROG_LIM,
.rangelow = 76000 * 16,
.rangehigh = 108000 * 16,
.modulation = V4L2_BAND_MODULATION_FM,
},
{
.type = V4L2_TUNER_RADIO,
.index = 1,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS |
V4L2_TUNER_CAP_HWSEEK_BOUNDED |
V4L2_TUNER_CAP_HWSEEK_PROG_LIM,
.rangelow = 530 * 16,
.rangehigh = 1710 * 16,
.modulation = V4L2_BAND_MODULATION_AM,
},
};
static u32 tea5777_freq_to_v4l2_freq(struct radio_tea5777 *tea, u32 freq)
{
switch (tea->band) {
case BAND_FM:
return (freq * TEA5777_FM_FREQ_STEP + TEA5777_FM_IF) * 16;
case BAND_AM:
return (freq * TEA5777_AM_FREQ_STEP + TEA5777_AM_IF) * 16;
}
return 0; /* Never reached */
}
int radio_tea5777_set_freq(struct radio_tea5777 *tea)
{
u32 freq;
int res;
freq = clamp(tea->freq, bands[tea->band].rangelow,
bands[tea->band].rangehigh);
freq = (freq + 8) / 16; /* to kHz */
switch (tea->band) {
case BAND_FM:
tea->write_reg &= ~TEA5777_W_AM_FM_MASK;
freq = (freq - TEA5777_FM_IF) / TEA5777_FM_FREQ_STEP;
tea->write_reg &= ~TEA5777_W_FM_PLL_MASK;
tea->write_reg |= (u64)freq << TEA5777_W_FM_PLL_SHIFT;
tea->write_reg &= ~TEA5777_W_FM_FREF_MASK;
tea->write_reg |= TEA5777_W_FM_FREF_VALUE <<
TEA5777_W_FM_FREF_SHIFT;
tea->write_reg &= ~TEA5777_W_FM_FORCEMONO_MASK;
if (tea->audmode == V4L2_TUNER_MODE_MONO)
tea->write_reg |= 1LL << TEA5777_W_FM_FORCEMONO_SHIFT;
break;
case BAND_AM:
tea->write_reg &= ~TEA5777_W_AM_FM_MASK;
tea->write_reg |= (1LL << TEA5777_W_AM_FM_SHIFT);
freq = (freq - TEA5777_AM_IF) / TEA5777_AM_FREQ_STEP;
tea->write_reg &= ~TEA5777_W_AM_PLL_MASK;
tea->write_reg |= (u64)freq << TEA5777_W_AM_PLL_SHIFT;
tea->write_reg &= ~TEA5777_W_AM_AGCRF_MASK;
tea->write_reg &= ~TEA5777_W_AM_AGCRF_MASK;
tea->write_reg &= ~TEA5777_W_AM_MWLW_MASK;
tea->write_reg |= TEA5777_W_AM_MW << TEA5777_W_AM_MWLW_SHIFT;
tea->write_reg &= ~TEA5777_W_AM_LNA_MASK;
tea->write_reg |= 1LL << TEA5777_W_AM_LNA_SHIFT;
tea->write_reg &= ~TEA5777_W_AM_PEAK_MASK;
tea->write_reg |= 1LL << TEA5777_W_AM_PEAK_SHIFT;
tea->write_reg &= ~TEA5777_W_AM_CALLIGN_MASK;
break;
}
res = tea->ops->write_reg(tea, tea->write_reg);
if (res)
return res;
tea->needs_write = false;
tea->read_reg = -1;
tea->freq = tea5777_freq_to_v4l2_freq(tea, freq);
return 0;
}
static int radio_tea5777_update_read_reg(struct radio_tea5777 *tea, int wait)
{
int res;
if (tea->read_reg != -1)
return 0;
if (tea->write_before_read && tea->needs_write) {
res = radio_tea5777_set_freq(tea);
if (res)
return res;
}
if (wait) {
if (schedule_timeout_interruptible(msecs_to_jiffies(wait)))
return -ERESTARTSYS;
}
res = tea->ops->read_reg(tea, &tea->read_reg);
if (res)
return res;
tea->needs_write = true;
return 0;
}
/*
* Linux Video interface
*/
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct radio_tea5777 *tea = video_drvdata(file);
strscpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
strscpy(v->card, tea->card, sizeof(v->card));
strlcat(v->card, " TEA5777", sizeof(v->card));
strscpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
return 0;
}
static int vidioc_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
struct radio_tea5777 *tea = video_drvdata(file);
if (band->tuner != 0 || band->index >= ARRAY_SIZE(bands) ||
(!tea->has_am && band->index == BAND_AM))
return -EINVAL;
*band = bands[band->index];
return 0;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct radio_tea5777 *tea = video_drvdata(file);
int res;
if (v->index > 0)
return -EINVAL;
res = radio_tea5777_update_read_reg(tea, 0);
if (res)
return res;
memset(v, 0, sizeof(*v));
if (tea->has_am)
strscpy(v->name, "AM/FM", sizeof(v->name));
else
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS |
V4L2_TUNER_CAP_HWSEEK_BOUNDED |
V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
v->rangelow = tea->has_am ? bands[BAND_AM].rangelow :
bands[BAND_FM].rangelow;
v->rangehigh = bands[BAND_FM].rangehigh;
if (tea->band == BAND_FM &&
(tea->read_reg & TEA5777_R_FM_STEREO_MASK))
v->rxsubchans = V4L2_TUNER_SUB_STEREO;
else
v->rxsubchans = V4L2_TUNER_SUB_MONO;
v->audmode = tea->audmode;
/* shift - 12 to convert 4-bits (0-15) scale to 16-bits (0-65535) */
v->signal = (tea->read_reg & TEA5777_R_LEVEL_MASK) >>
(TEA5777_R_LEVEL_SHIFT - 12);
/* Invalidate read_reg, so that next call we return up2date signal */
tea->read_reg = -1;
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct radio_tea5777 *tea = video_drvdata(file);
u32 orig_audmode = tea->audmode;
if (v->index)
return -EINVAL;
tea->audmode = v->audmode;
if (tea->audmode > V4L2_TUNER_MODE_STEREO)
tea->audmode = V4L2_TUNER_MODE_STEREO;
if (tea->audmode != orig_audmode && tea->band == BAND_FM)
return radio_tea5777_set_freq(tea);
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct radio_tea5777 *tea = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = tea->freq;
return 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct radio_tea5777 *tea = video_drvdata(file);
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
if (tea->has_am && f->frequency < (20000 * 16))
tea->band = BAND_AM;
else
tea->band = BAND_FM;
tea->freq = f->frequency;
return radio_tea5777_set_freq(tea);
}
static int vidioc_s_hw_freq_seek(struct file *file, void *fh,
const struct v4l2_hw_freq_seek *a)
{
struct radio_tea5777 *tea = video_drvdata(file);
unsigned long timeout;
u32 rangelow = a->rangelow;
u32 rangehigh = a->rangehigh;
int i, res, spacing;
u32 orig_freq;
if (a->tuner || a->wrap_around)
return -EINVAL;
if (file->f_flags & O_NONBLOCK)
return -EWOULDBLOCK;
if (rangelow || rangehigh) {
for (i = 0; i < ARRAY_SIZE(bands); i++) {
if (i == BAND_AM && !tea->has_am)
continue;
if (bands[i].rangelow >= rangelow &&
bands[i].rangehigh <= rangehigh)
break;
}
if (i == ARRAY_SIZE(bands))
return -EINVAL; /* No matching band found */
tea->band = i;
if (tea->freq < rangelow || tea->freq > rangehigh) {
tea->freq = clamp(tea->freq, rangelow,
rangehigh);
res = radio_tea5777_set_freq(tea);
if (res)
return res;
}
} else {
rangelow = bands[tea->band].rangelow;
rangehigh = bands[tea->band].rangehigh;
}
spacing = (tea->band == BAND_AM) ? (5 * 16) : (200 * 16); /* kHz */
orig_freq = tea->freq;
tea->write_reg |= TEA5777_W_PROGBLIM_MASK;
if (tea->seek_rangelow != rangelow) {
tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
tea->freq = rangelow;
res = radio_tea5777_set_freq(tea);
if (res)
goto leave;
tea->seek_rangelow = rangelow;
}
if (tea->seek_rangehigh != rangehigh) {
tea->write_reg |= TEA5777_W_UPDWN_MASK;
tea->freq = rangehigh;
res = radio_tea5777_set_freq(tea);
if (res)
goto leave;
tea->seek_rangehigh = rangehigh;
}
tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
tea->write_reg |= TEA5777_W_SEARCH_MASK;
if (a->seek_upward) {
tea->write_reg |= TEA5777_W_UPDWN_MASK;
tea->freq = orig_freq + spacing;
} else {
tea->write_reg &= ~TEA5777_W_UPDWN_MASK;
tea->freq = orig_freq - spacing;
}
res = radio_tea5777_set_freq(tea);
if (res)
goto leave;
timeout = jiffies + msecs_to_jiffies(5000);
for (;;) {
if (time_after(jiffies, timeout)) {
res = -ENODATA;
break;
}
res = radio_tea5777_update_read_reg(tea, 100);
if (res)
break;
/*
* Note we use tea->freq to track how far we've searched sofar
* this is necessary to ensure we continue seeking at the right
* point, in the write_before_read case.
*/
tea->freq = (tea->read_reg & TEA5777_R_FM_PLL_MASK);
tea->freq = tea5777_freq_to_v4l2_freq(tea, tea->freq);
if ((tea->read_reg & TEA5777_R_SFOUND_MASK)) {
tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
return 0;
}
if (tea->read_reg & TEA5777_R_BLIM_MASK) {
res = -ENODATA;
break;
}
/* Force read_reg update */
tea->read_reg = -1;
}
leave:
tea->write_reg &= ~TEA5777_W_PROGBLIM_MASK;
tea->write_reg &= ~TEA5777_W_SEARCH_MASK;
tea->freq = orig_freq;
radio_tea5777_set_freq(tea);
return res;
}
static int tea575x_s_ctrl(struct v4l2_ctrl *c)
{
struct radio_tea5777 *tea =
container_of(c->handler, struct radio_tea5777, ctrl_handler);
switch (c->id) {
case V4L2_CID_AUDIO_MUTE:
if (c->val)
tea->write_reg |= TEA5777_W_MUTE_MASK;
else
tea->write_reg &= ~TEA5777_W_MUTE_MASK;
return radio_tea5777_set_freq(tea);
}
return -EINVAL;
}
static const struct v4l2_file_operations tea575x_fops = {
.unlocked_ioctl = video_ioctl2,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
};
static const struct v4l2_ioctl_ops tea575x_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_s_hw_freq_seek = vidioc_s_hw_freq_seek,
.vidioc_enum_freq_bands = vidioc_enum_freq_bands,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct video_device tea575x_radio = {
.ioctl_ops = &tea575x_ioctl_ops,
.release = video_device_release_empty,
};
static const struct v4l2_ctrl_ops tea575x_ctrl_ops = {
.s_ctrl = tea575x_s_ctrl,
};
int radio_tea5777_init(struct radio_tea5777 *tea, struct module *owner)
{
int res;
tea->write_reg = (1LL << TEA5777_W_IFCE_SHIFT) |
(1LL << TEA5777_W_IFW_SHIFT) |
(1LL << TEA5777_W_INTEXT_SHIFT) |
(1LL << TEA5777_W_CHP0_SHIFT) |
(1LL << TEA5777_W_SLEV_SHIFT);
tea->freq = 90500 * 16; /* 90.5Mhz default */
tea->audmode = V4L2_TUNER_MODE_STEREO;
res = radio_tea5777_set_freq(tea);
if (res) {
v4l2_err(tea->v4l2_dev, "can't set initial freq (%d)\n", res);
return res;
}
tea->vd = tea575x_radio;
video_set_drvdata(&tea->vd, tea);
mutex_init(&tea->mutex);
strscpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name));
tea->vd.lock = &tea->mutex;
tea->vd.v4l2_dev = tea->v4l2_dev;
tea->vd.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
V4L2_CAP_HW_FREQ_SEEK;
tea->fops = tea575x_fops;
tea->fops.owner = owner;
tea->vd.fops = &tea->fops;
tea->vd.ctrl_handler = &tea->ctrl_handler;
v4l2_ctrl_handler_init(&tea->ctrl_handler, 1);
v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
res = tea->ctrl_handler.error;
if (res) {
v4l2_err(tea->v4l2_dev, "can't initialize controls\n");
v4l2_ctrl_handler_free(&tea->ctrl_handler);
return res;
}
v4l2_ctrl_handler_setup(&tea->ctrl_handler);
res = video_register_device(&tea->vd, VFL_TYPE_RADIO, -1);
if (res) {
v4l2_err(tea->v4l2_dev, "can't register video device!\n");
v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
return res;
}
return 0;
}
EXPORT_SYMBOL_GPL(radio_tea5777_init);
void radio_tea5777_exit(struct radio_tea5777 *tea)
{
video_unregister_device(&tea->vd);
v4l2_ctrl_handler_free(tea->vd.ctrl_handler);
}
EXPORT_SYMBOL_GPL(radio_tea5777_exit);
| linux-master | drivers/media/radio/radio-tea5777.c |
// SPDX-License-Identifier: GPL-2.0-only
/* radio-cadet.c - A video4linux driver for the ADS Cadet AM/FM Radio Card
*
* by Fred Gleason <[email protected]>
* Version 0.3.3
*
* (Loosely) based on code for the Aztech radio card by
*
* Russell Kroll ([email protected])
* Quay Ly
* Donald Song
* Jason Lewis ([email protected])
* Scott McGrath ([email protected])
* William McGrath ([email protected])
*
* History:
* 2000-04-29 Russell Kroll <[email protected]>
* Added ISAPnP detection for Linux 2.3/2.4
*
* 2001-01-10 Russell Kroll <[email protected]>
* Removed dead CONFIG_RADIO_CADET_PORT code
* PnP detection on load is now default (no args necessary)
*
* 2002-01-17 Adam Belay <[email protected]>
* Updated to latest pnp code
*
* 2003-01-31 Alan Cox <[email protected]>
* Cleaned up locking, delay code, general odds and ends
*
* 2006-07-30 Hans J. Koch <[email protected]>
* Changed API to V4L2
*/
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/ioport.h> /* request_region */
#include <linux/delay.h> /* udelay */
#include <linux/videodev2.h> /* V4L2 API defs */
#include <linux/param.h>
#include <linux/pnp.h>
#include <linux/sched.h>
#include <linux/io.h> /* outb, outb_p */
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
MODULE_AUTHOR("Fred Gleason, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
MODULE_DESCRIPTION("A driver for the ADS Cadet AM/FM/RDS radio card.");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.3.4");
static int io = -1; /* default to isapnp activation */
static int radio_nr = -1;
module_param(io, int, 0);
MODULE_PARM_DESC(io, "I/O address of Cadet card (0x330,0x332,0x334,0x336,0x338,0x33a,0x33c,0x33e)");
module_param(radio_nr, int, 0);
#define RDS_BUFFER 256
#define RDS_RX_FLAG 1
#define MBS_RX_FLAG 2
struct cadet {
struct v4l2_device v4l2_dev;
struct video_device vdev;
struct v4l2_ctrl_handler ctrl_handler;
int io;
bool is_fm_band;
u32 curfreq;
int tunestat;
int sigstrength;
wait_queue_head_t read_queue;
struct timer_list readtimer;
u8 rdsin, rdsout, rdsstat;
unsigned char rdsbuf[RDS_BUFFER];
struct mutex lock;
int reading;
};
static struct cadet cadet_card;
/*
* Signal Strength Threshold Values
* The V4L API spec does not define any particular unit for the signal
* strength value. These values are in microvolts of RF at the tuner's input.
*/
static u16 sigtable[2][4] = {
{ 1835, 2621, 4128, 65535 },
{ 2185, 4369, 13107, 65535 },
};
static const struct v4l2_frequency_band bands[] = {
{
.index = 0,
.type = V4L2_TUNER_RADIO,
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 8320, /* 520 kHz */
.rangehigh = 26400, /* 1650 kHz */
.modulation = V4L2_BAND_MODULATION_AM,
}, {
.index = 1,
.type = V4L2_TUNER_RADIO,
.capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS |
V4L2_TUNER_CAP_RDS_BLOCK_IO | V4L2_TUNER_CAP_LOW |
V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = 1400000, /* 87.5 MHz */
.rangehigh = 1728000, /* 108.0 MHz */
.modulation = V4L2_BAND_MODULATION_FM,
},
};
static int cadet_getstereo(struct cadet *dev)
{
int ret = V4L2_TUNER_SUB_MONO;
if (!dev->is_fm_band) /* Only FM has stereo capability! */
return V4L2_TUNER_SUB_MONO;
outb(7, dev->io); /* Select tuner control */
if ((inb(dev->io + 1) & 0x40) == 0)
ret = V4L2_TUNER_SUB_STEREO;
return ret;
}
static unsigned cadet_gettune(struct cadet *dev)
{
int curvol, i;
unsigned fifo = 0;
/*
* Prepare for read
*/
outb(7, dev->io); /* Select tuner control */
curvol = inb(dev->io + 1); /* Save current volume/mute setting */
outb(0x00, dev->io + 1); /* Ensure WRITE-ENABLE is LOW */
dev->tunestat = 0xffff;
/*
* Read the shift register
*/
for (i = 0; i < 25; i++) {
fifo = (fifo << 1) | ((inb(dev->io + 1) >> 7) & 0x01);
if (i < 24) {
outb(0x01, dev->io + 1);
dev->tunestat &= inb(dev->io + 1);
outb(0x00, dev->io + 1);
}
}
/*
* Restore volume/mute setting
*/
outb(curvol, dev->io + 1);
return fifo;
}
static unsigned cadet_getfreq(struct cadet *dev)
{
int i;
unsigned freq = 0, test, fifo = 0;
/*
* Read current tuning
*/
fifo = cadet_gettune(dev);
/*
* Convert to actual frequency
*/
if (!dev->is_fm_band) /* AM */
return ((fifo & 0x7fff) - 450) * 16;
test = 12500;
for (i = 0; i < 14; i++) {
if ((fifo & 0x01) != 0)
freq += test;
test = test << 1;
fifo = fifo >> 1;
}
freq -= 10700000; /* IF frequency is 10.7 MHz */
freq = (freq * 16) / 1000; /* Make it 1/16 kHz */
return freq;
}
static void cadet_settune(struct cadet *dev, unsigned fifo)
{
int i;
unsigned test;
outb(7, dev->io); /* Select tuner control */
/*
* Write the shift register
*/
test = 0;
test = (fifo >> 23) & 0x02; /* Align data for SDO */
test |= 0x1c; /* SDM=1, SWE=1, SEN=1, SCK=0 */
outb(7, dev->io); /* Select tuner control */
outb(test, dev->io + 1); /* Initialize for write */
for (i = 0; i < 25; i++) {
test |= 0x01; /* Toggle SCK High */
outb(test, dev->io + 1);
test &= 0xfe; /* Toggle SCK Low */
outb(test, dev->io + 1);
fifo = fifo << 1; /* Prepare the next bit */
test = 0x1c | ((fifo >> 23) & 0x02);
outb(test, dev->io + 1);
}
}
static void cadet_setfreq(struct cadet *dev, unsigned freq)
{
unsigned fifo;
int i, j, test;
int curvol;
freq = clamp(freq, bands[dev->is_fm_band].rangelow,
bands[dev->is_fm_band].rangehigh);
dev->curfreq = freq;
/*
* Formulate a fifo command
*/
fifo = 0;
if (dev->is_fm_band) { /* FM */
test = 102400;
freq = freq / 16; /* Make it kHz */
freq += 10700; /* IF is 10700 kHz */
for (i = 0; i < 14; i++) {
fifo = fifo << 1;
if (freq >= test) {
fifo |= 0x01;
freq -= test;
}
test = test >> 1;
}
} else { /* AM */
fifo = (freq / 16) + 450; /* Make it kHz */
fifo |= 0x100000; /* Select AM Band */
}
/*
* Save current volume/mute setting
*/
outb(7, dev->io); /* Select tuner control */
curvol = inb(dev->io + 1);
/*
* Tune the card
*/
for (j = 3; j > -1; j--) {
cadet_settune(dev, fifo | (j << 16));
outb(7, dev->io); /* Select tuner control */
outb(curvol, dev->io + 1);
msleep(100);
cadet_gettune(dev);
if ((dev->tunestat & 0x40) == 0) { /* Tuned */
dev->sigstrength = sigtable[dev->is_fm_band][j];
goto reset_rds;
}
}
dev->sigstrength = 0;
reset_rds:
outb(3, dev->io);
outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
}
static bool cadet_has_rds_data(struct cadet *dev)
{
bool result;
mutex_lock(&dev->lock);
result = dev->rdsin != dev->rdsout;
mutex_unlock(&dev->lock);
return result;
}
static void cadet_handler(struct timer_list *t)
{
struct cadet *dev = from_timer(dev, t, readtimer);
/* Service the RDS fifo */
if (mutex_trylock(&dev->lock)) {
outb(0x3, dev->io); /* Select RDS Decoder Control */
if ((inb(dev->io + 1) & 0x20) != 0)
pr_err("cadet: RDS fifo overflow\n");
outb(0x80, dev->io); /* Select RDS fifo */
while ((inb(dev->io) & 0x80) != 0) {
dev->rdsbuf[dev->rdsin] = inb(dev->io + 1);
if (dev->rdsin + 1 != dev->rdsout)
dev->rdsin++;
}
mutex_unlock(&dev->lock);
}
/*
* Service pending read
*/
if (cadet_has_rds_data(dev))
wake_up_interruptible(&dev->read_queue);
/*
* Clean up and exit
*/
dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
add_timer(&dev->readtimer);
}
static void cadet_start_rds(struct cadet *dev)
{
dev->rdsstat = 1;
outb(0x80, dev->io); /* Select RDS fifo */
timer_setup(&dev->readtimer, cadet_handler, 0);
dev->readtimer.expires = jiffies + msecs_to_jiffies(50);
add_timer(&dev->readtimer);
}
static ssize_t cadet_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
struct cadet *dev = video_drvdata(file);
unsigned char readbuf[RDS_BUFFER];
int i = 0;
mutex_lock(&dev->lock);
if (dev->rdsstat == 0)
cadet_start_rds(dev);
mutex_unlock(&dev->lock);
if (!cadet_has_rds_data(dev) && (file->f_flags & O_NONBLOCK))
return -EWOULDBLOCK;
i = wait_event_interruptible(dev->read_queue, cadet_has_rds_data(dev));
if (i)
return i;
mutex_lock(&dev->lock);
while (i < count && dev->rdsin != dev->rdsout)
readbuf[i++] = dev->rdsbuf[dev->rdsout++];
mutex_unlock(&dev->lock);
if (i && copy_to_user(data, readbuf, i))
return -EFAULT;
return i;
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
strscpy(v->driver, "ADS Cadet", sizeof(v->driver));
strscpy(v->card, "ADS Cadet", sizeof(v->card));
strscpy(v->bus_info, "ISA:radio-cadet", sizeof(v->bus_info));
return 0;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct cadet *dev = video_drvdata(file);
if (v->index)
return -EINVAL;
v->type = V4L2_TUNER_RADIO;
strscpy(v->name, "Radio", sizeof(v->name));
v->capability = bands[0].capability | bands[1].capability;
v->rangelow = bands[0].rangelow; /* 520 kHz (start of AM band) */
v->rangehigh = bands[1].rangehigh; /* 108.0 MHz (end of FM band) */
if (dev->is_fm_band) {
v->rxsubchans = cadet_getstereo(dev);
outb(3, dev->io);
outb(inb(dev->io + 1) & 0x7f, dev->io + 1);
mdelay(100);
outb(3, dev->io);
if (inb(dev->io + 1) & 0x80)
v->rxsubchans |= V4L2_TUNER_SUB_RDS;
} else {
v->rangelow = 8320; /* 520 kHz */
v->rangehigh = 26400; /* 1650 kHz */
v->rxsubchans = V4L2_TUNER_SUB_MONO;
}
v->audmode = V4L2_TUNER_MODE_STEREO;
v->signal = dev->sigstrength; /* We might need to modify scaling of this */
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
return v->index ? -EINVAL : 0;
}
static int vidioc_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
if (band->tuner)
return -EINVAL;
if (band->index >= ARRAY_SIZE(bands))
return -EINVAL;
*band = bands[band->index];
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct cadet *dev = video_drvdata(file);
if (f->tuner)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = dev->curfreq;
return 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct cadet *dev = video_drvdata(file);
if (f->tuner)
return -EINVAL;
dev->is_fm_band =
f->frequency >= (bands[0].rangehigh + bands[1].rangelow) / 2;
cadet_setfreq(dev, f->frequency);
return 0;
}
static int cadet_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct cadet *dev = container_of(ctrl->handler, struct cadet, ctrl_handler);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
outb(7, dev->io); /* Select tuner control */
if (ctrl->val)
outb(0x00, dev->io + 1);
else
outb(0x20, dev->io + 1);
return 0;
}
return -EINVAL;
}
static int cadet_open(struct file *file)
{
struct cadet *dev = video_drvdata(file);
int err;
mutex_lock(&dev->lock);
err = v4l2_fh_open(file);
if (err)
goto fail;
if (v4l2_fh_is_singular_file(file))
init_waitqueue_head(&dev->read_queue);
fail:
mutex_unlock(&dev->lock);
return err;
}
static int cadet_release(struct file *file)
{
struct cadet *dev = video_drvdata(file);
mutex_lock(&dev->lock);
if (v4l2_fh_is_singular_file(file) && dev->rdsstat) {
del_timer_sync(&dev->readtimer);
dev->rdsstat = 0;
}
v4l2_fh_release(file);
mutex_unlock(&dev->lock);
return 0;
}
static __poll_t cadet_poll(struct file *file, struct poll_table_struct *wait)
{
struct cadet *dev = video_drvdata(file);
__poll_t req_events = poll_requested_events(wait);
__poll_t res = v4l2_ctrl_poll(file, wait);
poll_wait(file, &dev->read_queue, wait);
if (dev->rdsstat == 0 && (req_events & (EPOLLIN | EPOLLRDNORM))) {
mutex_lock(&dev->lock);
if (dev->rdsstat == 0)
cadet_start_rds(dev);
mutex_unlock(&dev->lock);
}
if (cadet_has_rds_data(dev))
res |= EPOLLIN | EPOLLRDNORM;
return res;
}
static const struct v4l2_file_operations cadet_fops = {
.owner = THIS_MODULE,
.open = cadet_open,
.release = cadet_release,
.read = cadet_read,
.unlocked_ioctl = video_ioctl2,
.poll = cadet_poll,
};
static const struct v4l2_ioctl_ops cadet_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_enum_freq_bands = vidioc_enum_freq_bands,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct v4l2_ctrl_ops cadet_ctrl_ops = {
.s_ctrl = cadet_s_ctrl,
};
#ifdef CONFIG_PNP
static const struct pnp_device_id cadet_pnp_devices[] = {
/* ADS Cadet AM/FM Radio Card */
{.id = "MSM0c24", .driver_data = 0},
{.id = ""}
};
MODULE_DEVICE_TABLE(pnp, cadet_pnp_devices);
static int cadet_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
{
if (!dev)
return -ENODEV;
/* only support one device */
if (io > 0)
return -EBUSY;
if (!pnp_port_valid(dev, 0))
return -ENODEV;
io = pnp_port_start(dev, 0);
printk(KERN_INFO "radio-cadet: PnP reports device at %#x\n", io);
return io;
}
static struct pnp_driver cadet_pnp_driver = {
.name = "radio-cadet",
.id_table = cadet_pnp_devices,
.probe = cadet_pnp_probe,
.remove = NULL,
};
#else
static struct pnp_driver cadet_pnp_driver;
#endif
static void cadet_probe(struct cadet *dev)
{
static int iovals[8] = { 0x330, 0x332, 0x334, 0x336, 0x338, 0x33a, 0x33c, 0x33e };
int i;
for (i = 0; i < 8; i++) {
dev->io = iovals[i];
if (request_region(dev->io, 2, "cadet-probe")) {
cadet_setfreq(dev, bands[1].rangelow);
if (cadet_getfreq(dev) == bands[1].rangelow) {
release_region(dev->io, 2);
return;
}
release_region(dev->io, 2);
}
}
dev->io = -1;
}
/*
* io should only be set if the user has used something like
* isapnp (the userspace program) to initialize this card for us
*/
static int __init cadet_init(void)
{
struct cadet *dev = &cadet_card;
struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
struct v4l2_ctrl_handler *hdl;
int res = -ENODEV;
strscpy(v4l2_dev->name, "cadet", sizeof(v4l2_dev->name));
mutex_init(&dev->lock);
/* If a probe was requested then probe ISAPnP first (safest) */
if (io < 0)
pnp_register_driver(&cadet_pnp_driver);
dev->io = io;
/* If that fails then probe unsafely if probe is requested */
if (dev->io < 0)
cadet_probe(dev);
/* Else we bail out */
if (dev->io < 0) {
#ifdef MODULE
v4l2_err(v4l2_dev, "you must set an I/O address with io=0x330, 0x332, 0x334,\n");
v4l2_err(v4l2_dev, "0x336, 0x338, 0x33a, 0x33c or 0x33e\n");
#endif
goto fail;
}
if (!request_region(dev->io, 2, "cadet"))
goto fail;
res = v4l2_device_register(NULL, v4l2_dev);
if (res < 0) {
release_region(dev->io, 2);
v4l2_err(v4l2_dev, "could not register v4l2_device\n");
goto fail;
}
hdl = &dev->ctrl_handler;
v4l2_ctrl_handler_init(hdl, 2);
v4l2_ctrl_new_std(hdl, &cadet_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
v4l2_dev->ctrl_handler = hdl;
if (hdl->error) {
res = hdl->error;
v4l2_err(v4l2_dev, "Could not register controls\n");
goto err_hdl;
}
dev->is_fm_band = true;
dev->curfreq = bands[dev->is_fm_band].rangelow;
cadet_setfreq(dev, dev->curfreq);
strscpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
dev->vdev.v4l2_dev = v4l2_dev;
dev->vdev.fops = &cadet_fops;
dev->vdev.ioctl_ops = &cadet_ioctl_ops;
dev->vdev.release = video_device_release_empty;
dev->vdev.lock = &dev->lock;
dev->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
V4L2_CAP_READWRITE | V4L2_CAP_RDS_CAPTURE;
video_set_drvdata(&dev->vdev, dev);
res = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
if (res < 0)
goto err_hdl;
v4l2_info(v4l2_dev, "ADS Cadet Radio Card at 0x%x\n", dev->io);
return 0;
err_hdl:
v4l2_ctrl_handler_free(hdl);
v4l2_device_unregister(v4l2_dev);
release_region(dev->io, 2);
fail:
pnp_unregister_driver(&cadet_pnp_driver);
return res;
}
static void __exit cadet_exit(void)
{
struct cadet *dev = &cadet_card;
video_unregister_device(&dev->vdev);
v4l2_ctrl_handler_free(&dev->ctrl_handler);
v4l2_device_unregister(&dev->v4l2_dev);
outb(7, dev->io); /* Mute */
outb(0x00, dev->io + 1);
release_region(dev->io, 2);
pnp_unregister_driver(&cadet_pnp_driver);
}
module_init(cadet_init);
module_exit(cadet_exit);
| linux-master | drivers/media/radio/radio-cadet.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Miro PCM20 radio driver for Linux radio support
* (c) 1998 Ruurd Reitsma <[email protected]>
* Thanks to Norberto Pellici for the ACI device interface specification
* The API part is based on the radiotrack driver by M. Kirkwood
* This driver relies on the aci mixer provided by the snd-miro
* ALSA driver.
* Look there for further info...
*
* From the original miro RDS sources:
*
* (c) 2001 Robert Siemer <[email protected]>
*
* Many thanks to Fred Seidel <[email protected]>, the
* designer of the RDS decoder hardware. With his help
* I was able to code this driver.
* Thanks also to Norberto Pellicci, Dominic Mounteney
* <[email protected]> and www.teleauskunft.de
* for good hints on finding Fred. It was somewhat hard
* to locate him here in Germany... [:
*
* This code has been reintroduced and converted to use
* the new V4L2 RDS API by:
*
* Hans Verkuil <[email protected]>
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/videodev2.h>
#include <linux/kthread.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
#include <sound/aci.h>
#define RDS_DATASHIFT 2 /* Bit 2 */
#define RDS_DATAMASK (1 << RDS_DATASHIFT)
#define RDS_BUSYMASK 0x10 /* Bit 4 */
#define RDS_CLOCKMASK 0x08 /* Bit 3 */
#define RDS_DATA(x) (((x) >> RDS_DATASHIFT) & 1)
#define RDS_STATUS 0x01
#define RDS_STATIONNAME 0x02
#define RDS_TEXT 0x03
#define RDS_ALTFREQ 0x04
#define RDS_TIMEDATE 0x05
#define RDS_PI_CODE 0x06
#define RDS_PTYTATP 0x07
#define RDS_RESET 0x08
#define RDS_RXVALUE 0x09
static int radio_nr = -1;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
struct pcm20 {
struct v4l2_device v4l2_dev;
struct video_device vdev;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_ctrl *rds_pty;
struct v4l2_ctrl *rds_ps_name;
struct v4l2_ctrl *rds_radio_test;
struct v4l2_ctrl *rds_ta;
struct v4l2_ctrl *rds_tp;
struct v4l2_ctrl *rds_ms;
/* thread for periodic RDS status checking */
struct task_struct *kthread;
unsigned long freq;
u32 audmode;
struct snd_miro_aci *aci;
struct mutex lock;
};
static struct pcm20 pcm20_card = {
.freq = 87 * 16000,
.audmode = V4L2_TUNER_MODE_STEREO,
};
static int rds_waitread(struct snd_miro_aci *aci)
{
u8 byte;
int i = 2000;
do {
byte = inb(aci->aci_port + ACI_REG_RDS);
i--;
} while ((byte & RDS_BUSYMASK) && i);
/*
* It's magic, but without this the data that you read later on
* is unreliable and full of bit errors. With this 1 usec delay
* everything is fine.
*/
udelay(1);
return i ? byte : -1;
}
static int rds_rawwrite(struct snd_miro_aci *aci, u8 byte)
{
if (rds_waitread(aci) >= 0) {
outb(byte, aci->aci_port + ACI_REG_RDS);
return 0;
}
return -1;
}
static int rds_write(struct snd_miro_aci *aci, u8 byte)
{
u8 sendbuffer[8];
int i;
for (i = 7; i >= 0; i--)
sendbuffer[7 - i] = (byte & (1 << i)) ? RDS_DATAMASK : 0;
sendbuffer[0] |= RDS_CLOCKMASK;
for (i = 0; i < 8; i++)
rds_rawwrite(aci, sendbuffer[i]);
return 0;
}
static int rds_readcycle_nowait(struct snd_miro_aci *aci)
{
outb(0, aci->aci_port + ACI_REG_RDS);
return rds_waitread(aci);
}
static int rds_readcycle(struct snd_miro_aci *aci)
{
if (rds_rawwrite(aci, 0) < 0)
return -1;
return rds_waitread(aci);
}
static int rds_ack(struct snd_miro_aci *aci)
{
int i = rds_readcycle(aci);
if (i < 0)
return -1;
if (i & RDS_DATAMASK)
return 0; /* ACK */
return 1; /* NACK */
}
static int rds_cmd(struct snd_miro_aci *aci, u8 cmd, u8 databuffer[], u8 datasize)
{
int i, j;
rds_write(aci, cmd);
/* RDS_RESET doesn't need further processing */
if (cmd == RDS_RESET)
return 0;
if (rds_ack(aci))
return -EIO;
if (datasize == 0)
return 0;
/* to be able to use rds_readcycle_nowait()
I have to waitread() here */
if (rds_waitread(aci) < 0)
return -1;
memset(databuffer, 0, datasize);
for (i = 0; i < 8 * datasize; i++) {
j = rds_readcycle_nowait(aci);
if (j < 0)
return -EIO;
databuffer[i / 8] |= RDS_DATA(j) << (7 - (i % 8));
}
return 0;
}
static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq)
{
unsigned char freql;
unsigned char freqh;
struct snd_miro_aci *aci = dev->aci;
freq /= 160;
if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0))
freq /= 10; /* I don't know exactly which version
* needs this hack */
freql = freq & 0xff;
freqh = freq >> 8;
rds_cmd(aci, RDS_RESET, NULL, 0);
return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh);
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct pcm20 *dev = video_drvdata(file);
strscpy(v->driver, "Miro PCM20", sizeof(v->driver));
strscpy(v->card, "Miro PCM20", sizeof(v->card));
snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", dev->v4l2_dev.name);
return 0;
}
static bool sanitize(char *p, int size)
{
int i;
bool ret = true;
for (i = 0; i < size; i++) {
if (p[i] < 32) {
p[i] = ' ';
ret = false;
}
}
return ret;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct pcm20 *dev = video_drvdata(file);
int res;
u8 buf;
if (v->index)
return -EINVAL;
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = 87*16000;
v->rangehigh = 108*16000;
res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTATION, -1, -1);
v->signal = (res & 0x80) ? 0 : 0xffff;
/* Note: stereo detection does not work if the audio is muted,
it will default to mono in that case. */
res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTEREO, -1, -1);
v->rxsubchans = (res & 0x40) ? V4L2_TUNER_SUB_MONO :
V4L2_TUNER_SUB_STEREO;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
v->audmode = dev->audmode;
res = rds_cmd(dev->aci, RDS_RXVALUE, &buf, 1);
if (res >= 0 && buf)
v->rxsubchans |= V4L2_TUNER_SUB_RDS;
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct pcm20 *dev = video_drvdata(file);
if (v->index)
return -EINVAL;
if (v->audmode > V4L2_TUNER_MODE_STEREO)
dev->audmode = V4L2_TUNER_MODE_STEREO;
else
dev->audmode = v->audmode;
snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
dev->audmode == V4L2_TUNER_MODE_MONO, -1);
return 0;
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct pcm20 *dev = video_drvdata(file);
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = dev->freq;
return 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct pcm20 *dev = video_drvdata(file);
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
dev->freq = clamp_t(u32, f->frequency, 87 * 16000U, 108 * 16000U);
pcm20_setfreq(dev, dev->freq);
return 0;
}
static int pcm20_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct pcm20 *dev = container_of(ctrl->handler, struct pcm20, ctrl_handler);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, ctrl->val, -1);
return 0;
}
return -EINVAL;
}
static int pcm20_thread(void *data)
{
struct pcm20 *dev = data;
const unsigned no_rds_start_counter = 5;
const unsigned sleep_msecs = 2000;
unsigned no_rds_counter = no_rds_start_counter;
for (;;) {
char text_buffer[66];
u8 buf;
int res;
msleep_interruptible(sleep_msecs);
if (kthread_should_stop())
break;
res = rds_cmd(dev->aci, RDS_RXVALUE, &buf, 1);
if (res)
continue;
if (buf == 0) {
if (no_rds_counter == 0)
continue;
no_rds_counter--;
if (no_rds_counter)
continue;
/*
* No RDS seen for no_rds_start_counter * sleep_msecs
* milliseconds, clear all RDS controls to their
* default values.
*/
v4l2_ctrl_s_ctrl_string(dev->rds_ps_name, "");
v4l2_ctrl_s_ctrl(dev->rds_ms, 1);
v4l2_ctrl_s_ctrl(dev->rds_ta, 0);
v4l2_ctrl_s_ctrl(dev->rds_tp, 0);
v4l2_ctrl_s_ctrl(dev->rds_pty, 0);
v4l2_ctrl_s_ctrl_string(dev->rds_radio_test, "");
continue;
}
no_rds_counter = no_rds_start_counter;
res = rds_cmd(dev->aci, RDS_STATUS, &buf, 1);
if (res)
continue;
if ((buf >> 3) & 1) {
res = rds_cmd(dev->aci, RDS_STATIONNAME, text_buffer, 8);
text_buffer[8] = 0;
if (!res && sanitize(text_buffer, 8))
v4l2_ctrl_s_ctrl_string(dev->rds_ps_name, text_buffer);
}
if ((buf >> 6) & 1) {
u8 pty;
res = rds_cmd(dev->aci, RDS_PTYTATP, &pty, 1);
if (!res) {
v4l2_ctrl_s_ctrl(dev->rds_ms, !!(pty & 0x01));
v4l2_ctrl_s_ctrl(dev->rds_ta, !!(pty & 0x02));
v4l2_ctrl_s_ctrl(dev->rds_tp, !!(pty & 0x80));
v4l2_ctrl_s_ctrl(dev->rds_pty, (pty >> 2) & 0x1f);
}
}
if ((buf >> 4) & 1) {
res = rds_cmd(dev->aci, RDS_TEXT, text_buffer, 65);
text_buffer[65] = 0;
if (!res && sanitize(text_buffer + 1, 64))
v4l2_ctrl_s_ctrl_string(dev->rds_radio_test, text_buffer + 1);
}
}
return 0;
}
static int pcm20_open(struct file *file)
{
struct pcm20 *dev = video_drvdata(file);
int res = v4l2_fh_open(file);
if (!res && v4l2_fh_is_singular_file(file) &&
IS_ERR_OR_NULL(dev->kthread)) {
dev->kthread = kthread_run(pcm20_thread, dev, "%s",
dev->v4l2_dev.name);
if (IS_ERR(dev->kthread)) {
v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
v4l2_fh_release(file);
return PTR_ERR(dev->kthread);
}
}
return res;
}
static int pcm20_release(struct file *file)
{
struct pcm20 *dev = video_drvdata(file);
if (v4l2_fh_is_singular_file(file) && !IS_ERR_OR_NULL(dev->kthread)) {
kthread_stop(dev->kthread);
dev->kthread = NULL;
}
return v4l2_fh_release(file);
}
static const struct v4l2_file_operations pcm20_fops = {
.owner = THIS_MODULE,
.open = pcm20_open,
.poll = v4l2_ctrl_poll,
.release = pcm20_release,
.unlocked_ioctl = video_ioctl2,
};
static const struct v4l2_ioctl_ops pcm20_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_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct v4l2_ctrl_ops pcm20_ctrl_ops = {
.s_ctrl = pcm20_s_ctrl,
};
static int __init pcm20_init(void)
{
struct pcm20 *dev = &pcm20_card;
struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
struct v4l2_ctrl_handler *hdl;
int res;
dev->aci = snd_aci_get_aci();
if (dev->aci == NULL) {
v4l2_err(v4l2_dev,
"you must load the snd-miro driver first!\n");
return -ENODEV;
}
strscpy(v4l2_dev->name, "radio-miropcm20", sizeof(v4l2_dev->name));
mutex_init(&dev->lock);
res = v4l2_device_register(NULL, v4l2_dev);
if (res < 0) {
v4l2_err(v4l2_dev, "could not register v4l2_device\n");
return -EINVAL;
}
hdl = &dev->ctrl_handler;
v4l2_ctrl_handler_init(hdl, 7);
v4l2_ctrl_new_std(hdl, &pcm20_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
dev->rds_pty = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_RDS_RX_PTY, 0, 0x1f, 1, 0);
dev->rds_ps_name = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_RDS_RX_PS_NAME, 0, 8, 8, 0);
dev->rds_radio_test = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_RDS_RX_RADIO_TEXT, 0, 64, 64, 0);
dev->rds_ta = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
dev->rds_tp = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_RDS_RX_TRAFFIC_PROGRAM, 0, 1, 1, 0);
dev->rds_ms = v4l2_ctrl_new_std(hdl, NULL,
V4L2_CID_RDS_RX_MUSIC_SPEECH, 0, 1, 1, 1);
v4l2_dev->ctrl_handler = hdl;
if (hdl->error) {
res = hdl->error;
v4l2_err(v4l2_dev, "Could not register control\n");
goto err_hdl;
}
strscpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
dev->vdev.v4l2_dev = v4l2_dev;
dev->vdev.fops = &pcm20_fops;
dev->vdev.ioctl_ops = &pcm20_ioctl_ops;
dev->vdev.release = video_device_release_empty;
dev->vdev.lock = &dev->lock;
dev->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
V4L2_CAP_RDS_CAPTURE;
video_set_drvdata(&dev->vdev, dev);
snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
dev->audmode == V4L2_TUNER_MODE_MONO, -1);
pcm20_setfreq(dev, dev->freq);
if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
goto err_hdl;
v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n");
return 0;
err_hdl:
v4l2_ctrl_handler_free(hdl);
v4l2_device_unregister(v4l2_dev);
return -EINVAL;
}
MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt");
MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
MODULE_LICENSE("GPL");
static void __exit pcm20_cleanup(void)
{
struct pcm20 *dev = &pcm20_card;
video_unregister_device(&dev->vdev);
snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, 1, -1);
v4l2_ctrl_handler_free(&dev->ctrl_handler);
v4l2_device_unregister(&dev->v4l2_dev);
}
module_init(pcm20_init);
module_exit(pcm20_cleanup);
| linux-master | drivers/media/radio/radio-miropcm20.c |
// SPDX-License-Identifier: GPL-2.0-only
/* SF16-FMR2 and SF16-FMD2 radio driver for Linux
* Copyright (c) 2011 Ondrej Zary
*
* Original driver was (c) 2000-2002 Ziglio Frediano, [email protected]
* but almost nothing remained here after conversion to generic TEA575x
* implementation
*/
#include <linux/delay.h>
#include <linux/module.h> /* Modules */
#include <linux/init.h> /* Initdata */
#include <linux/slab.h>
#include <linux/ioport.h> /* request_region */
#include <linux/io.h> /* outb, outb_p */
#include <linux/isa.h>
#include <linux/pnp.h>
#include <media/drv-intf/tea575x.h>
MODULE_AUTHOR("Ondrej Zary");
MODULE_DESCRIPTION("MediaForte SF16-FMR2 and SF16-FMD2 FM radio card driver");
MODULE_LICENSE("GPL");
/* these cards can only use two different ports (0x384 and 0x284) */
#define FMR2_MAX 2
static int radio_nr[FMR2_MAX] = { [0 ... (FMR2_MAX - 1)] = -1 };
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");
struct fmr2 {
int io;
struct v4l2_device v4l2_dev;
struct snd_tea575x tea;
struct v4l2_ctrl *volume;
struct v4l2_ctrl *balance;
bool is_fmd2;
};
static int num_fmr2_cards;
static struct fmr2 *fmr2_cards[FMR2_MAX];
static bool isa_registered;
static bool pnp_registered;
/* the port is hardwired on SF16-FMR2 */
#define FMR2_PORT 0x384
/* TEA575x tuner pins */
#define STR_DATA (1 << 0)
#define STR_CLK (1 << 1)
#define STR_WREN (1 << 2)
#define STR_MOST (1 << 3)
/* PT2254A/TC9154A volume control pins */
#define PT_ST (1 << 4)
#define PT_CK (1 << 5)
#define PT_DATA (1 << 6)
/* volume control presence pin */
#define FMR2_HASVOL (1 << 7)
static void fmr2_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
{
struct fmr2 *fmr2 = tea->private_data;
u8 bits = 0;
bits |= (pins & TEA575X_DATA) ? STR_DATA : 0;
bits |= (pins & TEA575X_CLK) ? STR_CLK : 0;
/* WRITE_ENABLE is inverted, DATA must be high during read */
bits |= (pins & TEA575X_WREN) ? 0 : STR_WREN | STR_DATA;
outb(bits, fmr2->io);
}
static u8 fmr2_tea575x_get_pins(struct snd_tea575x *tea)
{
struct fmr2 *fmr2 = tea->private_data;
u8 bits = inb(fmr2->io);
return ((bits & STR_DATA) ? TEA575X_DATA : 0) |
((bits & STR_MOST) ? TEA575X_MOST : 0);
}
static void fmr2_tea575x_set_direction(struct snd_tea575x *tea, bool output)
{
}
static const struct snd_tea575x_ops fmr2_tea_ops = {
.set_pins = fmr2_tea575x_set_pins,
.get_pins = fmr2_tea575x_get_pins,
.set_direction = fmr2_tea575x_set_direction,
};
/* TC9154A/PT2254A volume control */
/* 18-bit shift register bit definitions */
#define TC9154A_ATT_MAJ_0DB (1 << 0)
#define TC9154A_ATT_MAJ_10DB (1 << 1)
#define TC9154A_ATT_MAJ_20DB (1 << 2)
#define TC9154A_ATT_MAJ_30DB (1 << 3)
#define TC9154A_ATT_MAJ_40DB (1 << 4)
#define TC9154A_ATT_MAJ_50DB (1 << 5)
#define TC9154A_ATT_MAJ_60DB (1 << 6)
#define TC9154A_ATT_MIN_0DB (1 << 7)
#define TC9154A_ATT_MIN_2DB (1 << 8)
#define TC9154A_ATT_MIN_4DB (1 << 9)
#define TC9154A_ATT_MIN_6DB (1 << 10)
#define TC9154A_ATT_MIN_8DB (1 << 11)
/* bit 12 is ignored */
#define TC9154A_CHANNEL_LEFT (1 << 13)
#define TC9154A_CHANNEL_RIGHT (1 << 14)
/* bits 15, 16, 17 must be 0 */
#define TC9154A_ATT_MAJ(x) (1 << x)
#define TC9154A_ATT_MIN(x) (1 << (7 + x))
static void tc9154a_set_pins(struct fmr2 *fmr2, u8 pins)
{
if (!fmr2->tea.mute)
pins |= STR_WREN;
outb(pins, fmr2->io);
}
static void tc9154a_set_attenuation(struct fmr2 *fmr2, int att, u32 channel)
{
int i;
u32 reg;
u8 bit;
reg = TC9154A_ATT_MAJ(att / 10) | TC9154A_ATT_MIN((att % 10) / 2);
reg |= channel;
/* write 18-bit shift register, LSB first */
for (i = 0; i < 18; i++) {
bit = reg & (1 << i) ? PT_DATA : 0;
tc9154a_set_pins(fmr2, bit);
udelay(5);
tc9154a_set_pins(fmr2, bit | PT_CK);
udelay(5);
tc9154a_set_pins(fmr2, bit);
}
/* latch register data */
udelay(5);
tc9154a_set_pins(fmr2, PT_ST);
udelay(5);
tc9154a_set_pins(fmr2, 0);
}
static int fmr2_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
struct fmr2 *fmr2 = tea->private_data;
int volume, balance, left, right;
switch (ctrl->id) {
case V4L2_CID_AUDIO_VOLUME:
volume = ctrl->val;
balance = fmr2->balance->cur.val;
break;
case V4L2_CID_AUDIO_BALANCE:
balance = ctrl->val;
volume = fmr2->volume->cur.val;
break;
default:
return -EINVAL;
}
left = right = volume;
if (balance < 0)
right = max(0, right + balance);
if (balance > 0)
left = max(0, left - balance);
tc9154a_set_attenuation(fmr2, abs(left - 68), TC9154A_CHANNEL_LEFT);
tc9154a_set_attenuation(fmr2, abs(right - 68), TC9154A_CHANNEL_RIGHT);
return 0;
}
static const struct v4l2_ctrl_ops fmr2_ctrl_ops = {
.s_ctrl = fmr2_s_ctrl,
};
static int fmr2_tea_ext_init(struct snd_tea575x *tea)
{
struct fmr2 *fmr2 = tea->private_data;
/* FMR2 can have volume control, FMD2 can't (uses SB16 mixer) */
if (!fmr2->is_fmd2 && inb(fmr2->io) & FMR2_HASVOL) {
fmr2->volume = v4l2_ctrl_new_std(&tea->ctrl_handler, &fmr2_ctrl_ops, V4L2_CID_AUDIO_VOLUME, 0, 68, 2, 56);
fmr2->balance = v4l2_ctrl_new_std(&tea->ctrl_handler, &fmr2_ctrl_ops, V4L2_CID_AUDIO_BALANCE, -68, 68, 2, 0);
if (tea->ctrl_handler.error) {
printk(KERN_ERR "radio-sf16fmr2: can't initialize controls\n");
return tea->ctrl_handler.error;
}
}
return 0;
}
static const struct pnp_device_id fmr2_pnp_ids[] = {
{ .id = "MFRad13" }, /* tuner subdevice of SF16-FMD2 */
{ .id = "" }
};
MODULE_DEVICE_TABLE(pnp, fmr2_pnp_ids);
static int fmr2_probe(struct fmr2 *fmr2, struct device *pdev, int io)
{
int err, i;
char *card_name = fmr2->is_fmd2 ? "SF16-FMD2" : "SF16-FMR2";
/* avoid errors if a card was already registered at given port */
for (i = 0; i < num_fmr2_cards; i++)
if (io == fmr2_cards[i]->io)
return -EBUSY;
strscpy(fmr2->v4l2_dev.name, "radio-sf16fmr2",
sizeof(fmr2->v4l2_dev.name));
fmr2->io = io;
if (!request_region(fmr2->io, 2, fmr2->v4l2_dev.name)) {
printk(KERN_ERR "radio-sf16fmr2: I/O port 0x%x already in use\n", fmr2->io);
return -EBUSY;
}
dev_set_drvdata(pdev, fmr2);
err = v4l2_device_register(pdev, &fmr2->v4l2_dev);
if (err < 0) {
v4l2_err(&fmr2->v4l2_dev, "Could not register v4l2_device\n");
release_region(fmr2->io, 2);
return err;
}
fmr2->tea.v4l2_dev = &fmr2->v4l2_dev;
fmr2->tea.private_data = fmr2;
fmr2->tea.radio_nr = radio_nr[num_fmr2_cards];
fmr2->tea.ops = &fmr2_tea_ops;
fmr2->tea.ext_init = fmr2_tea_ext_init;
strscpy(fmr2->tea.card, card_name, sizeof(fmr2->tea.card));
snprintf(fmr2->tea.bus_info, sizeof(fmr2->tea.bus_info), "%s:%s",
fmr2->is_fmd2 ? "PnP" : "ISA", dev_name(pdev));
if (snd_tea575x_init(&fmr2->tea, THIS_MODULE)) {
printk(KERN_ERR "radio-sf16fmr2: Unable to detect TEA575x tuner\n");
release_region(fmr2->io, 2);
return -ENODEV;
}
printk(KERN_INFO "radio-sf16fmr2: %s radio card at 0x%x.\n",
card_name, fmr2->io);
return 0;
}
static int fmr2_isa_match(struct device *pdev, unsigned int ndev)
{
struct fmr2 *fmr2 = kzalloc(sizeof(*fmr2), GFP_KERNEL);
if (!fmr2)
return 0;
if (fmr2_probe(fmr2, pdev, FMR2_PORT)) {
kfree(fmr2);
return 0;
}
dev_set_drvdata(pdev, fmr2);
fmr2_cards[num_fmr2_cards++] = fmr2;
return 1;
}
static int fmr2_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
{
int ret;
struct fmr2 *fmr2 = kzalloc(sizeof(*fmr2), GFP_KERNEL);
if (!fmr2)
return -ENOMEM;
fmr2->is_fmd2 = true;
ret = fmr2_probe(fmr2, &pdev->dev, pnp_port_start(pdev, 0));
if (ret) {
kfree(fmr2);
return ret;
}
pnp_set_drvdata(pdev, fmr2);
fmr2_cards[num_fmr2_cards++] = fmr2;
return 0;
}
static void fmr2_remove(struct fmr2 *fmr2)
{
snd_tea575x_exit(&fmr2->tea);
release_region(fmr2->io, 2);
v4l2_device_unregister(&fmr2->v4l2_dev);
kfree(fmr2);
}
static void fmr2_isa_remove(struct device *pdev, unsigned int ndev)
{
fmr2_remove(dev_get_drvdata(pdev));
}
static void fmr2_pnp_remove(struct pnp_dev *pdev)
{
fmr2_remove(pnp_get_drvdata(pdev));
pnp_set_drvdata(pdev, NULL);
}
static struct isa_driver fmr2_isa_driver = {
.match = fmr2_isa_match,
.remove = fmr2_isa_remove,
.driver = {
.name = "radio-sf16fmr2",
},
};
static struct pnp_driver fmr2_pnp_driver = {
.name = "radio-sf16fmr2",
.id_table = fmr2_pnp_ids,
.probe = fmr2_pnp_probe,
.remove = fmr2_pnp_remove,
};
static int __init fmr2_init(void)
{
int ret;
ret = pnp_register_driver(&fmr2_pnp_driver);
if (!ret)
pnp_registered = true;
ret = isa_register_driver(&fmr2_isa_driver, 1);
if (!ret)
isa_registered = true;
return (pnp_registered || isa_registered) ? 0 : ret;
}
static void __exit fmr2_exit(void)
{
if (pnp_registered)
pnp_unregister_driver(&fmr2_pnp_driver);
if (isa_registered)
isa_unregister_driver(&fmr2_isa_driver);
}
module_init(fmr2_init);
module_exit(fmr2_exit);
| linux-master | drivers/media/radio/radio-sf16fmr2.c |
/*
* Linux V4L2 radio driver for the Griffin radioSHARK USB radio receiver
*
* Note the radioSHARK offers the audio through a regular USB audio device,
* this driver only handles the tuning.
*
* The info necessary to drive the shark was taken from the small userspace
* shark.c program by Michael Rolig, which he kindly placed in the Public
* Domain.
*
* Copyright (c) 2012 Hans de Goede <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/workqueue.h>
#include <media/v4l2-device.h>
#include <media/drv-intf/tea575x.h>
#if defined(CONFIG_LEDS_CLASS) || \
(defined(CONFIG_LEDS_CLASS_MODULE) && defined(CONFIG_RADIO_SHARK_MODULE))
#define SHARK_USE_LEDS 1
#endif
/*
* Version Information
*/
MODULE_AUTHOR("Hans de Goede <[email protected]>");
MODULE_DESCRIPTION("Griffin radioSHARK, USB radio receiver driver");
MODULE_LICENSE("GPL");
#define SHARK_IN_EP 0x83
#define SHARK_OUT_EP 0x05
#define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
#define TEA575X_BIT_BAND_MASK (3<<20)
#define TEA575X_BIT_BAND_FM (0<<20)
#define TB_LEN 6
#define DRV_NAME "radioshark"
#define v4l2_dev_to_shark(d) container_of(d, struct shark_device, v4l2_dev)
/* Note BLUE_IS_PULSE comes after NO_LEDS as it is a status bit, not a LED */
enum { BLUE_LED, BLUE_PULSE_LED, RED_LED, NO_LEDS, BLUE_IS_PULSE };
struct shark_device {
struct usb_device *usbdev;
struct v4l2_device v4l2_dev;
struct snd_tea575x tea;
#ifdef SHARK_USE_LEDS
struct work_struct led_work;
struct led_classdev leds[NO_LEDS];
char led_names[NO_LEDS][32];
atomic_t brightness[NO_LEDS];
unsigned long brightness_new;
#endif
u8 *transfer_buffer;
u32 last_val;
};
static atomic_t shark_instance = ATOMIC_INIT(0);
static void shark_write_val(struct snd_tea575x *tea, u32 val)
{
struct shark_device *shark = tea->private_data;
int i, res, actual_len;
/* Avoid unnecessary (slow) USB transfers */
if (shark->last_val == val)
return;
memset(shark->transfer_buffer, 0, TB_LEN);
shark->transfer_buffer[0] = 0xc0; /* Write shift register command */
for (i = 0; i < 4; i++)
shark->transfer_buffer[i] |= (val >> (24 - i * 8)) & 0xff;
res = usb_interrupt_msg(shark->usbdev,
usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res >= 0)
shark->last_val = val;
else
v4l2_err(&shark->v4l2_dev, "set-freq error: %d\n", res);
}
static u32 shark_read_val(struct snd_tea575x *tea)
{
struct shark_device *shark = tea->private_data;
int i, res, actual_len;
u32 val = 0;
memset(shark->transfer_buffer, 0, TB_LEN);
shark->transfer_buffer[0] = 0x80;
res = usb_interrupt_msg(shark->usbdev,
usb_sndintpipe(shark->usbdev, SHARK_OUT_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0) {
v4l2_err(&shark->v4l2_dev, "request-status error: %d\n", res);
return shark->last_val;
}
res = usb_interrupt_msg(shark->usbdev,
usb_rcvintpipe(shark->usbdev, SHARK_IN_EP),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0) {
v4l2_err(&shark->v4l2_dev, "get-status error: %d\n", res);
return shark->last_val;
}
for (i = 0; i < 4; i++)
val |= shark->transfer_buffer[i] << (24 - i * 8);
shark->last_val = val;
/*
* The shark does not allow actually reading the stereo / mono pin :(
* So assume that when we're tuned to an FM station and mono has not
* been requested, that we're receiving stereo.
*/
if (((val & TEA575X_BIT_BAND_MASK) == TEA575X_BIT_BAND_FM) &&
!(val & TEA575X_BIT_MONO))
shark->tea.stereo = true;
else
shark->tea.stereo = false;
return val;
}
static const struct snd_tea575x_ops shark_tea_ops = {
.write_val = shark_write_val,
.read_val = shark_read_val,
};
#ifdef SHARK_USE_LEDS
static void shark_led_work(struct work_struct *work)
{
struct shark_device *shark =
container_of(work, struct shark_device, led_work);
int i, res, brightness, actual_len;
for (i = 0; i < 3; i++) {
if (!test_and_clear_bit(i, &shark->brightness_new))
continue;
brightness = atomic_read(&shark->brightness[i]);
memset(shark->transfer_buffer, 0, TB_LEN);
if (i != RED_LED) {
shark->transfer_buffer[0] = 0xA0 + i;
shark->transfer_buffer[1] = brightness;
} else
shark->transfer_buffer[0] = brightness ? 0xA9 : 0xA8;
res = usb_interrupt_msg(shark->usbdev,
usb_sndintpipe(shark->usbdev, 0x05),
shark->transfer_buffer, TB_LEN,
&actual_len, 1000);
if (res < 0)
v4l2_err(&shark->v4l2_dev, "set LED %s error: %d\n",
shark->led_names[i], res);
}
}
static void shark_led_set_blue(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct shark_device *shark =
container_of(led_cdev, struct shark_device, leds[BLUE_LED]);
atomic_set(&shark->brightness[BLUE_LED], value);
set_bit(BLUE_LED, &shark->brightness_new);
clear_bit(BLUE_IS_PULSE, &shark->brightness_new);
schedule_work(&shark->led_work);
}
static void shark_led_set_blue_pulse(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct shark_device *shark = container_of(led_cdev,
struct shark_device, leds[BLUE_PULSE_LED]);
atomic_set(&shark->brightness[BLUE_PULSE_LED], 256 - value);
set_bit(BLUE_PULSE_LED, &shark->brightness_new);
set_bit(BLUE_IS_PULSE, &shark->brightness_new);
schedule_work(&shark->led_work);
}
static void shark_led_set_red(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct shark_device *shark =
container_of(led_cdev, struct shark_device, leds[RED_LED]);
atomic_set(&shark->brightness[RED_LED], value);
set_bit(RED_LED, &shark->brightness_new);
schedule_work(&shark->led_work);
}
static const struct led_classdev shark_led_templates[NO_LEDS] = {
[BLUE_LED] = {
.name = "%s:blue:",
.brightness = LED_OFF,
.max_brightness = 127,
.brightness_set = shark_led_set_blue,
},
[BLUE_PULSE_LED] = {
.name = "%s:blue-pulse:",
.brightness = LED_OFF,
.max_brightness = 255,
.brightness_set = shark_led_set_blue_pulse,
},
[RED_LED] = {
.name = "%s:red:",
.brightness = LED_OFF,
.max_brightness = 1,
.brightness_set = shark_led_set_red,
},
};
static int shark_register_leds(struct shark_device *shark, struct device *dev)
{
int i, retval;
atomic_set(&shark->brightness[BLUE_LED], 127);
INIT_WORK(&shark->led_work, shark_led_work);
for (i = 0; i < NO_LEDS; i++) {
shark->leds[i] = shark_led_templates[i];
snprintf(shark->led_names[i], sizeof(shark->led_names[0]),
shark->leds[i].name, shark->v4l2_dev.name);
shark->leds[i].name = shark->led_names[i];
retval = led_classdev_register(dev, &shark->leds[i]);
if (retval) {
v4l2_err(&shark->v4l2_dev,
"couldn't register led: %s\n",
shark->led_names[i]);
return retval;
}
}
return 0;
}
static void shark_unregister_leds(struct shark_device *shark)
{
int i;
for (i = 0; i < NO_LEDS; i++)
led_classdev_unregister(&shark->leds[i]);
cancel_work_sync(&shark->led_work);
}
static inline void shark_resume_leds(struct shark_device *shark)
{
if (test_bit(BLUE_IS_PULSE, &shark->brightness_new))
set_bit(BLUE_PULSE_LED, &shark->brightness_new);
else
set_bit(BLUE_LED, &shark->brightness_new);
set_bit(RED_LED, &shark->brightness_new);
schedule_work(&shark->led_work);
}
#else
static int shark_register_leds(struct shark_device *shark, struct device *dev)
{
v4l2_warn(&shark->v4l2_dev,
"CONFIG_LEDS_CLASS not enabled, LED support disabled\n");
return 0;
}
static inline void shark_unregister_leds(struct shark_device *shark) { }
static inline void shark_resume_leds(struct shark_device *shark) { }
#endif
static void usb_shark_disconnect(struct usb_interface *intf)
{
struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
mutex_lock(&shark->tea.mutex);
v4l2_device_disconnect(&shark->v4l2_dev);
snd_tea575x_exit(&shark->tea);
mutex_unlock(&shark->tea.mutex);
shark_unregister_leds(shark);
v4l2_device_put(&shark->v4l2_dev);
}
static void usb_shark_release(struct v4l2_device *v4l2_dev)
{
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
v4l2_device_unregister(&shark->v4l2_dev);
kfree(shark->transfer_buffer);
kfree(shark);
}
static int usb_shark_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct shark_device *shark;
int retval = -ENOMEM;
static const u8 ep_addresses[] = {
SHARK_IN_EP | USB_DIR_IN,
SHARK_OUT_EP | USB_DIR_OUT,
0};
/* Are the expected endpoints present? */
if (!usb_check_int_endpoints(intf, ep_addresses)) {
dev_err(&intf->dev, "Invalid radioSHARK device\n");
return -EINVAL;
}
shark = kzalloc(sizeof(struct shark_device), GFP_KERNEL);
if (!shark)
return retval;
shark->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
if (!shark->transfer_buffer)
goto err_alloc_buffer;
v4l2_device_set_name(&shark->v4l2_dev, DRV_NAME, &shark_instance);
retval = shark_register_leds(shark, &intf->dev);
if (retval)
goto err_reg_leds;
shark->v4l2_dev.release = usb_shark_release;
retval = v4l2_device_register(&intf->dev, &shark->v4l2_dev);
if (retval) {
v4l2_err(&shark->v4l2_dev, "couldn't register v4l2_device\n");
goto err_reg_dev;
}
shark->usbdev = interface_to_usbdev(intf);
shark->tea.v4l2_dev = &shark->v4l2_dev;
shark->tea.private_data = shark;
shark->tea.radio_nr = -1;
shark->tea.ops = &shark_tea_ops;
shark->tea.cannot_mute = true;
shark->tea.has_am = true;
strscpy(shark->tea.card, "Griffin radioSHARK",
sizeof(shark->tea.card));
usb_make_path(shark->usbdev, shark->tea.bus_info,
sizeof(shark->tea.bus_info));
retval = snd_tea575x_init(&shark->tea, THIS_MODULE);
if (retval) {
v4l2_err(&shark->v4l2_dev, "couldn't init tea5757\n");
goto err_init_tea;
}
return 0;
err_init_tea:
v4l2_device_unregister(&shark->v4l2_dev);
err_reg_dev:
shark_unregister_leds(shark);
err_reg_leds:
kfree(shark->transfer_buffer);
err_alloc_buffer:
kfree(shark);
return retval;
}
#ifdef CONFIG_PM
static int usb_shark_suspend(struct usb_interface *intf, pm_message_t message)
{
return 0;
}
static int usb_shark_resume(struct usb_interface *intf)
{
struct v4l2_device *v4l2_dev = usb_get_intfdata(intf);
struct shark_device *shark = v4l2_dev_to_shark(v4l2_dev);
mutex_lock(&shark->tea.mutex);
snd_tea575x_set_freq(&shark->tea);
mutex_unlock(&shark->tea.mutex);
shark_resume_leds(shark);
return 0;
}
#endif
/* Specify the bcdDevice value, as the radioSHARK and radioSHARK2 share ids */
static const struct usb_device_id usb_shark_device_table[] = {
{ .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
USB_DEVICE_ID_MATCH_INT_CLASS,
.idVendor = 0x077d,
.idProduct = 0x627a,
.bcdDevice_lo = 0x0001,
.bcdDevice_hi = 0x0001,
.bInterfaceClass = 3,
},
{ }
};
MODULE_DEVICE_TABLE(usb, usb_shark_device_table);
static struct usb_driver usb_shark_driver = {
.name = DRV_NAME,
.probe = usb_shark_probe,
.disconnect = usb_shark_disconnect,
.id_table = usb_shark_device_table,
#ifdef CONFIG_PM
.suspend = usb_shark_suspend,
.resume = usb_shark_resume,
.reset_resume = usb_shark_resume,
#endif
};
module_usb_driver(usb_shark_driver);
| linux-master | drivers/media/radio/radio-shark.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* Driver for the Texas Instruments WL1273 FM radio.
*
* Copyright (C) 2011 Nokia Corporation
* Author: Matti J. Aaltonen <[email protected]>
*/
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/mfd/wl1273-core.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#define DRIVER_DESC "Wl1273 FM Radio"
#define WL1273_POWER_SET_OFF 0
#define WL1273_POWER_SET_FM BIT(0)
#define WL1273_POWER_SET_RDS BIT(1)
#define WL1273_POWER_SET_RETENTION BIT(4)
#define WL1273_PUPD_SET_OFF 0x00
#define WL1273_PUPD_SET_ON 0x01
#define WL1273_PUPD_SET_RETENTION 0x10
#define WL1273_FREQ(x) (x * 10000 / 625)
#define WL1273_INV_FREQ(x) (x * 625 / 10000)
/*
* static int radio_nr - The number of the radio device
*
* The default is 0.
*/
static int radio_nr;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
struct wl1273_device {
char *bus_type;
u8 forbidden;
unsigned int preemphasis;
unsigned int spacing;
unsigned int tx_power;
unsigned int rx_frequency;
unsigned int tx_frequency;
unsigned int rangelow;
unsigned int rangehigh;
unsigned int band;
bool stereo;
/* RDS */
unsigned int rds_on;
wait_queue_head_t read_queue;
struct mutex lock; /* for serializing fm radio operations */
struct completion busy;
unsigned char *buffer;
unsigned int buf_size;
unsigned int rd_index;
unsigned int wr_index;
/* Selected interrupts */
u16 irq_flags;
u16 irq_received;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_device v4l2dev;
struct video_device videodev;
struct device *dev;
struct wl1273_core *core;
struct file *owner;
char *write_buf;
unsigned int rds_users;
};
#define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
WL1273_POW_ENB_EVENT)
/*
* static unsigned int rds_buf - the number of RDS buffer blocks used.
*
* The default number is 100.
*/
static unsigned int rds_buf = 100;
module_param(rds_buf, uint, 0);
MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
static int wl1273_fm_write_fw(struct wl1273_core *core,
__u8 *fw, int len)
{
struct i2c_client *client = core->client;
struct i2c_msg msg;
int i, r = 0;
msg.addr = client->addr;
msg.flags = 0;
for (i = 0; i <= len; i++) {
msg.len = fw[0];
msg.buf = fw + 1;
fw += msg.len + 1;
dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
r = i2c_transfer(client->adapter, &msg, 1);
if (r < 0 && i < len + 1)
break;
}
dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
/* Last transfer always fails. */
if (i == len || r == 1)
r = 0;
return r;
}
#define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
#define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
#define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
static int wl1273_fm_rds(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
struct i2c_client *client = core->client;
u16 val;
u8 b0 = WL1273_RDS_DATA_GET, status;
struct v4l2_rds_data rds = { 0, 0, 0 };
struct i2c_msg msg[] = {
{
.addr = client->addr,
.flags = 0,
.buf = &b0,
.len = 1,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.buf = (u8 *) &rds,
.len = sizeof(rds),
}
};
int r;
if (core->mode != WL1273_MODE_RX)
return 0;
r = core->read(core, WL1273_RDS_SYNC_GET, &val);
if (r)
return r;
if ((val & 0x01) == 0) {
/* RDS decoder not synchronized */
return -EAGAIN;
}
/* copy all four RDS blocks to internal buffer */
do {
r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if (r != ARRAY_SIZE(msg)) {
dev_err(radio->dev, WL1273_FM_DRIVER_NAME
": %s: read_rds error r == %i)\n",
__func__, r);
}
status = rds.block;
if (!WL1273_FIFO_HAS_DATA(status))
break;
/* copy bits 0-2 (the block ID) to bits 3-5 */
rds.block = V4L2_RDS_BLOCK_MSK & status;
rds.block |= rds.block << 3;
/* copy the error bits to standard positions */
if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
rds.block |= V4L2_RDS_BLOCK_ERROR;
rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
} else if (WL1273_RDS_CORRECTABLE_ERROR & status) {
rds.block &= ~V4L2_RDS_BLOCK_ERROR;
rds.block |= V4L2_RDS_BLOCK_CORRECTED;
}
/* copy RDS block to internal buffer */
memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
radio->wr_index += 3;
/* wrap write pointer */
if (radio->wr_index >= radio->buf_size)
radio->wr_index = 0;
/* check for overflow & start over */
if (radio->wr_index == radio->rd_index) {
dev_dbg(radio->dev, "RDS OVERFLOW");
radio->rd_index = 0;
radio->wr_index = 0;
break;
}
} while (WL1273_FIFO_HAS_DATA(status));
/* wake up read queue */
if (radio->wr_index != radio->rd_index)
wake_up_interruptible(&radio->read_queue);
return 0;
}
static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
{
struct wl1273_device *radio = dev_id;
struct wl1273_core *core = radio->core;
u16 flags;
int r;
r = core->read(core, WL1273_FLAG_GET, &flags);
if (r)
goto out;
if (flags & WL1273_BL_EVENT) {
radio->irq_received = flags;
dev_dbg(radio->dev, "IRQ: BL\n");
}
if (flags & WL1273_RDS_EVENT) {
msleep(200);
wl1273_fm_rds(radio);
}
if (flags & WL1273_BBLK_EVENT)
dev_dbg(radio->dev, "IRQ: BBLK\n");
if (flags & WL1273_LSYNC_EVENT)
dev_dbg(radio->dev, "IRQ: LSYNC\n");
if (flags & WL1273_LEV_EVENT) {
u16 level;
r = core->read(core, WL1273_RSSI_LVL_GET, &level);
if (r)
goto out;
if (level > 14)
dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
}
if (flags & WL1273_IFFR_EVENT)
dev_dbg(radio->dev, "IRQ: IFFR\n");
if (flags & WL1273_PI_EVENT)
dev_dbg(radio->dev, "IRQ: PI\n");
if (flags & WL1273_PD_EVENT)
dev_dbg(radio->dev, "IRQ: PD\n");
if (flags & WL1273_STIC_EVENT)
dev_dbg(radio->dev, "IRQ: STIC\n");
if (flags & WL1273_MAL_EVENT)
dev_dbg(radio->dev, "IRQ: MAL\n");
if (flags & WL1273_POW_ENB_EVENT) {
complete(&radio->busy);
dev_dbg(radio->dev, "NOT BUSY\n");
dev_dbg(radio->dev, "IRQ: POW_ENB\n");
}
if (flags & WL1273_SCAN_OVER_EVENT)
dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
if (flags & WL1273_ERROR_EVENT)
dev_dbg(radio->dev, "IRQ: ERROR\n");
if (flags & WL1273_FR_EVENT) {
u16 freq;
dev_dbg(radio->dev, "IRQ: FR:\n");
if (core->mode == WL1273_MODE_RX) {
r = core->write(core, WL1273_TUNER_MODE_SET,
TUNER_MODE_STOP_SEARCH);
if (r) {
dev_err(radio->dev,
"%s: TUNER_MODE_SET fails: %d\n",
__func__, r);
goto out;
}
r = core->read(core, WL1273_FREQ_SET, &freq);
if (r)
goto out;
if (radio->band == WL1273_BAND_JAPAN)
radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
freq * 50;
else
radio->rx_frequency = WL1273_BAND_OTHER_LOW +
freq * 50;
/*
* The driver works better with this msleep,
* the documentation doesn't mention it.
*/
usleep_range(10000, 15000);
dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
} else {
r = core->read(core, WL1273_CHANL_SET, &freq);
if (r)
goto out;
dev_dbg(radio->dev, "%dkHz\n", freq);
}
dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
}
out:
core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
complete(&radio->busy);
return IRQ_HANDLED;
}
static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
{
struct wl1273_core *core = radio->core;
int r = 0;
unsigned long t;
if (freq < WL1273_BAND_TX_LOW) {
dev_err(radio->dev,
"Frequency out of range: %d < %d\n", freq,
WL1273_BAND_TX_LOW);
return -ERANGE;
}
if (freq > WL1273_BAND_TX_HIGH) {
dev_err(radio->dev,
"Frequency out of range: %d > %d\n", freq,
WL1273_BAND_TX_HIGH);
return -ERANGE;
}
/*
* The driver works better with this sleep,
* the documentation doesn't mention it.
*/
usleep_range(5000, 10000);
dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
/* Set the current tx channel */
r = core->write(core, WL1273_CHANL_SET, freq / 10);
if (r)
return r;
reinit_completion(&radio->busy);
/* wait for the FR IRQ */
t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
if (!t)
return -ETIMEDOUT;
dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);
/* Enable the output power */
r = core->write(core, WL1273_POWER_ENB_SET, 1);
if (r)
return r;
reinit_completion(&radio->busy);
/* wait for the POWER_ENB IRQ */
t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
if (!t)
return -ETIMEDOUT;
radio->tx_frequency = freq;
dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
return 0;
}
static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
{
struct wl1273_core *core = radio->core;
int r, f;
unsigned long t;
if (freq < radio->rangelow) {
dev_err(radio->dev,
"Frequency out of range: %d < %d\n", freq,
radio->rangelow);
r = -ERANGE;
goto err;
}
if (freq > radio->rangehigh) {
dev_err(radio->dev,
"Frequency out of range: %d > %d\n", freq,
radio->rangehigh);
r = -ERANGE;
goto err;
}
dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (radio->band == WL1273_BAND_JAPAN)
f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
else
f = (freq - WL1273_BAND_OTHER_LOW) / 50;
r = core->write(core, WL1273_FREQ_SET, f);
if (r) {
dev_err(radio->dev, "FREQ_SET fails\n");
goto err;
}
r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
if (r) {
dev_err(radio->dev, "TUNER_MODE_SET fails\n");
goto err;
}
reinit_completion(&radio->busy);
t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
if (!t) {
dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
return -ETIMEDOUT;
}
radio->rd_index = 0;
radio->wr_index = 0;
radio->rx_frequency = freq;
return 0;
err:
return r;
}
static int wl1273_fm_get_freq(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
unsigned int freq;
u16 f;
int r;
if (core->mode == WL1273_MODE_RX) {
r = core->read(core, WL1273_FREQ_SET, &f);
if (r)
return r;
dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
if (radio->band == WL1273_BAND_JAPAN)
freq = WL1273_BAND_JAPAN_LOW + 50 * f;
else
freq = WL1273_BAND_OTHER_LOW + 50 * f;
} else {
r = core->read(core, WL1273_CHANL_SET, &f);
if (r)
return r;
freq = f * 10;
}
return freq;
}
/**
* wl1273_fm_upload_firmware_patch() - Upload the firmware.
* @radio: A pointer to the device struct.
*
* The firmware file consists of arrays of bytes where the first byte
* gives the array length. The first byte in the file gives the
* number of these arrays.
*/
static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
unsigned int packet_num;
const struct firmware *fw_p;
const char *fw_name = "radio-wl1273-fw.bin";
struct device *dev = radio->dev;
__u8 *ptr;
int r;
dev_dbg(dev, "%s:\n", __func__);
/*
* Uploading the firmware patch is not always necessary,
* so we only print an info message.
*/
if (request_firmware(&fw_p, fw_name, dev)) {
dev_info(dev, "%s - %s not found\n", __func__, fw_name);
return 0;
}
ptr = (__u8 *) fw_p->data;
packet_num = ptr[0];
dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
if (r) {
dev_err(dev, "FW upload error: %d\n", r);
goto out;
}
/* ignore possible error here */
core->write(core, WL1273_RESET, 0);
dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
out:
release_firmware(fw_p);
return r;
}
static int wl1273_fm_stop(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
if (core->mode == WL1273_MODE_RX) {
int r = core->write(core, WL1273_POWER_SET,
WL1273_POWER_SET_OFF);
if (r)
dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
__func__, r);
} else if (core->mode == WL1273_MODE_TX) {
int r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_OFF);
if (r)
dev_err(radio->dev,
"%s: PUPD_SET fails: %d\n", __func__, r);
}
if (core->pdata->disable) {
core->pdata->disable();
dev_dbg(radio->dev, "Back to reset\n");
}
return 0;
}
static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
{
struct wl1273_core *core = radio->core;
struct wl1273_fm_platform_data *pdata = core->pdata;
struct device *dev = radio->dev;
int r = -EINVAL;
if (pdata->enable && core->mode == WL1273_MODE_OFF) {
dev_dbg(radio->dev, "Out of reset\n");
pdata->enable();
msleep(250);
}
if (new_mode == WL1273_MODE_RX) {
u16 val = WL1273_POWER_SET_FM;
if (radio->rds_on)
val |= WL1273_POWER_SET_RDS;
/* If this fails try again */
r = core->write(core, WL1273_POWER_SET, val);
if (r) {
msleep(100);
r = core->write(core, WL1273_POWER_SET, val);
if (r) {
dev_err(dev, "%s: POWER_SET fails\n", __func__);
goto fail;
}
}
/* rds buffer configuration */
radio->wr_index = 0;
radio->rd_index = 0;
} else if (new_mode == WL1273_MODE_TX) {
/* If this fails try again once */
r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
if (r) {
msleep(100);
r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_ON);
if (r) {
dev_err(dev, "%s: PUPD_SET fails\n", __func__);
goto fail;
}
}
if (radio->rds_on) {
r = core->write(core, WL1273_RDS_DATA_ENB, 1);
if (r) {
dev_err(dev, "%s: RDS_DATA_ENB ON fails\n",
__func__);
goto fail;
}
} else {
r = core->write(core, WL1273_RDS_DATA_ENB, 0);
if (r) {
dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n",
__func__);
goto fail;
}
}
} else {
dev_warn(dev, "%s: Illegal mode.\n", __func__);
}
if (core->mode == WL1273_MODE_OFF) {
r = wl1273_fm_upload_firmware_patch(radio);
if (r)
dev_warn(dev, "Firmware upload failed.\n");
/*
* Sometimes the chip is in a wrong power state at this point.
* So we set the power once again.
*/
if (new_mode == WL1273_MODE_RX) {
u16 val = WL1273_POWER_SET_FM;
if (radio->rds_on)
val |= WL1273_POWER_SET_RDS;
r = core->write(core, WL1273_POWER_SET, val);
if (r) {
dev_err(dev, "%s: POWER_SET fails\n", __func__);
goto fail;
}
} else if (new_mode == WL1273_MODE_TX) {
r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_ON);
if (r) {
dev_err(dev, "%s: PUPD_SET fails\n", __func__);
goto fail;
}
}
}
return 0;
fail:
if (pdata->disable)
pdata->disable();
dev_dbg(dev, "%s: return: %d\n", __func__, r);
return r;
}
static int wl1273_fm_suspend(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
int r;
/* Cannot go from OFF to SUSPENDED */
if (core->mode == WL1273_MODE_RX)
r = core->write(core, WL1273_POWER_SET,
WL1273_POWER_SET_RETENTION);
else if (core->mode == WL1273_MODE_TX)
r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_RETENTION);
else
r = -EINVAL;
if (r) {
dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
goto out;
}
out:
return r;
}
static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
{
struct wl1273_core *core = radio->core;
struct device *dev = radio->dev;
int old_mode;
int r;
dev_dbg(dev, "%s\n", __func__);
dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
old_mode = core->mode;
if (mode & radio->forbidden) {
r = -EPERM;
goto out;
}
switch (mode) {
case WL1273_MODE_RX:
case WL1273_MODE_TX:
r = wl1273_fm_start(radio, mode);
if (r) {
dev_err(dev, "%s: Cannot start.\n", __func__);
wl1273_fm_stop(radio);
goto out;
}
core->mode = mode;
r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (r) {
dev_err(dev, "INT_MASK_SET fails.\n");
goto out;
}
/* remember previous settings */
if (mode == WL1273_MODE_RX) {
r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
if (r) {
dev_err(dev, "set freq fails: %d.\n", r);
goto out;
}
r = core->set_volume(core, core->volume);
if (r) {
dev_err(dev, "set volume fails: %d.\n", r);
goto out;
}
dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
core->volume);
} else {
r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
if (r) {
dev_err(dev, "set freq fails: %d.\n", r);
goto out;
}
}
dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
r = core->set_audio(core, core->audio_mode);
if (r)
dev_err(dev, "Cannot set audio mode.\n");
break;
case WL1273_MODE_OFF:
r = wl1273_fm_stop(radio);
if (r)
dev_err(dev, "%s: Off fails: %d\n", __func__, r);
else
core->mode = WL1273_MODE_OFF;
break;
case WL1273_MODE_SUSPENDED:
r = wl1273_fm_suspend(radio);
if (r)
dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
else
core->mode = WL1273_MODE_SUSPENDED;
break;
default:
dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
r = -EINVAL;
break;
}
out:
if (r)
core->mode = old_mode;
return r;
}
static int wl1273_fm_set_seek(struct wl1273_device *radio,
unsigned int wrap_around,
unsigned int seek_upward,
int level)
{
struct wl1273_core *core = radio->core;
int r = 0;
unsigned int dir = (seek_upward == 0) ? 0 : 1;
unsigned int f;
f = radio->rx_frequency;
dev_dbg(radio->dev, "rx_frequency: %d\n", f);
if (dir && f + radio->spacing <= radio->rangehigh)
r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
else if (dir && wrap_around)
r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
else if (f - radio->spacing >= radio->rangelow)
r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
else if (wrap_around)
r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
if (r)
goto out;
if (level < SCHAR_MIN || level > SCHAR_MAX)
return -EINVAL;
reinit_completion(&radio->busy);
dev_dbg(radio->dev, "%s: BUSY\n", __func__);
r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (r)
goto out;
dev_dbg(radio->dev, "%s\n", __func__);
r = core->write(core, WL1273_SEARCH_LVL_SET, level);
if (r)
goto out;
r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
if (r)
goto out;
r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
if (r)
goto out;
/* wait for the FR IRQ */
wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
if (!(radio->irq_received & WL1273_BL_EVENT)) {
r = -ETIMEDOUT;
goto out;
}
radio->irq_received &= ~WL1273_BL_EVENT;
if (!wrap_around)
goto out;
/* Wrap around */
dev_dbg(radio->dev, "Wrap around in HW seek.\n");
if (seek_upward)
f = radio->rangelow;
else
f = radio->rangehigh;
r = wl1273_fm_set_rx_freq(radio, f);
if (r)
goto out;
reinit_completion(&radio->busy);
dev_dbg(radio->dev, "%s: BUSY\n", __func__);
r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
if (r)
goto out;
/* wait for the FR IRQ */
if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
r = -ETIMEDOUT;
out:
dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
return r;
}
/**
* wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
* @radio: A pointer to the device struct.
*/
static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
struct device *dev = radio->dev;
u16 val;
int r;
if (core->mode == WL1273_MODE_OFF ||
core->mode == WL1273_MODE_SUSPENDED)
return -EPERM;
r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
if (r) {
dev_err(dev, "%s: read error: %d\n", __func__, r);
goto out;
}
out:
return val;
}
/**
* wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
* @radio: A pointer to the device struct.
* @preemphasis: The new pre-amphasis value.
*
* Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
* V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
*/
static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
unsigned int preemphasis)
{
struct wl1273_core *core = radio->core;
int r;
u16 em;
if (core->mode == WL1273_MODE_OFF ||
core->mode == WL1273_MODE_SUSPENDED)
return -EPERM;
mutex_lock(&core->lock);
switch (preemphasis) {
case V4L2_PREEMPHASIS_DISABLED:
em = 1;
break;
case V4L2_PREEMPHASIS_50_uS:
em = 0;
break;
case V4L2_PREEMPHASIS_75_uS:
em = 2;
break;
default:
r = -EINVAL;
goto out;
}
r = core->write(core, WL1273_PREMPH_SET, em);
if (r)
goto out;
radio->preemphasis = preemphasis;
out:
mutex_unlock(&core->lock);
return r;
}
static int wl1273_fm_rds_on(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
int r;
dev_dbg(radio->dev, "%s\n", __func__);
if (radio->rds_on)
return 0;
r = core->write(core, WL1273_POWER_SET,
WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
if (r)
goto out;
r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
if (r)
dev_err(radio->dev, "set freq fails: %d.\n", r);
out:
return r;
}
static int wl1273_fm_rds_off(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
int r;
if (!radio->rds_on)
return 0;
radio->irq_flags &= ~WL1273_RDS_EVENT;
r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (r)
goto out;
/* Service pending read */
wake_up_interruptible(&radio->read_queue);
dev_dbg(radio->dev, "%s\n", __func__);
r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
if (r)
goto out;
r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
if (r)
dev_err(radio->dev, "set freq fails: %d.\n", r);
out:
dev_dbg(radio->dev, "%s: exiting...\n", __func__);
return r;
}
static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
{
int r = 0;
struct wl1273_core *core = radio->core;
if (core->mode == WL1273_MODE_OFF ||
core->mode == WL1273_MODE_SUSPENDED)
return -EPERM;
if (new_mode == WL1273_RDS_RESET) {
r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
return r;
}
if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
r = core->write(core, WL1273_RDS_DATA_ENB, 0);
} else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
r = core->write(core, WL1273_RDS_DATA_ENB, 1);
} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
r = wl1273_fm_rds_off(radio);
} else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
r = wl1273_fm_rds_on(radio);
} else {
dev_err(radio->dev, "%s: Unknown mode: %d\n",
__func__, new_mode);
r = -EINVAL;
}
if (!r)
radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
return r;
}
static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
u16 val;
int r;
dev_dbg(radio->dev, "%s\n", __func__);
if (core->mode != WL1273_MODE_TX)
return count;
if (radio->rds_users == 0) {
dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
return 0;
}
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
/*
* Multiple processes can open the device, but only
* one gets to write to it.
*/
if (radio->owner && radio->owner != file) {
r = -EBUSY;
goto out;
}
radio->owner = file;
/* Manual Mode */
if (count > 255)
val = 255;
else
val = count;
core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
if (copy_from_user(radio->write_buf + 1, buf, val)) {
r = -EFAULT;
goto out;
}
dev_dbg(radio->dev, "Count: %d\n", val);
dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
radio->write_buf[0] = WL1273_RDS_DATA_SET;
core->write_data(core, radio->write_buf, val + 1);
r = val;
out:
mutex_unlock(&core->lock);
return r;
}
static __poll_t wl1273_fm_fops_poll(struct file *file,
struct poll_table_struct *pts)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
if (radio->owner && radio->owner != file)
return EPOLLERR;
radio->owner = file;
if (core->mode == WL1273_MODE_RX) {
poll_wait(file, &radio->read_queue, pts);
if (radio->rd_index != radio->wr_index)
return EPOLLIN | EPOLLRDNORM;
} else if (core->mode == WL1273_MODE_TX) {
return EPOLLOUT | EPOLLWRNORM;
}
return 0;
}
static int wl1273_fm_fops_open(struct file *file)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
int r = 0;
dev_dbg(radio->dev, "%s\n", __func__);
if (core->mode == WL1273_MODE_RX && radio->rds_on &&
!radio->rds_users) {
dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
radio->irq_flags |= WL1273_RDS_EVENT;
r = core->write(core, WL1273_INT_MASK_SET,
radio->irq_flags);
if (r) {
mutex_unlock(&core->lock);
goto out;
}
radio->rds_users++;
mutex_unlock(&core->lock);
}
out:
return r;
}
static int wl1273_fm_fops_release(struct file *file)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
int r = 0;
dev_dbg(radio->dev, "%s\n", __func__);
if (radio->rds_users > 0) {
radio->rds_users--;
if (radio->rds_users == 0) {
mutex_lock(&core->lock);
radio->irq_flags &= ~WL1273_RDS_EVENT;
if (core->mode == WL1273_MODE_RX) {
r = core->write(core,
WL1273_INT_MASK_SET,
radio->irq_flags);
if (r) {
mutex_unlock(&core->lock);
goto out;
}
}
mutex_unlock(&core->lock);
}
}
if (file == radio->owner)
radio->owner = NULL;
out:
return r;
}
static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
int r = 0;
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
unsigned int block_count = 0;
u16 val;
dev_dbg(radio->dev, "%s\n", __func__);
if (core->mode != WL1273_MODE_RX)
return 0;
if (radio->rds_users == 0) {
dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
return 0;
}
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
/*
* Multiple processes can open the device, but only
* one at a time gets read access.
*/
if (radio->owner && radio->owner != file) {
r = -EBUSY;
goto out;
}
radio->owner = file;
r = core->read(core, WL1273_RDS_SYNC_GET, &val);
if (r) {
dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
goto out;
} else if (val == 0) {
dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
r = -ENODATA;
goto out;
}
/* block if no new data available */
while (radio->wr_index == radio->rd_index) {
if (file->f_flags & O_NONBLOCK) {
r = -EWOULDBLOCK;
goto out;
}
dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
if (wait_event_interruptible(radio->read_queue,
radio->wr_index !=
radio->rd_index) < 0) {
r = -EINTR;
goto out;
}
}
/* calculate block count from byte count */
count /= RDS_BLOCK_SIZE;
/* copy RDS blocks from the internal buffer and to user buffer */
while (block_count < count) {
if (radio->rd_index == radio->wr_index)
break;
/* always transfer complete RDS blocks */
if (copy_to_user(buf, &radio->buffer[radio->rd_index],
RDS_BLOCK_SIZE))
break;
/* increment and wrap the read pointer */
radio->rd_index += RDS_BLOCK_SIZE;
if (radio->rd_index >= radio->buf_size)
radio->rd_index = 0;
/* increment counters */
block_count++;
buf += RDS_BLOCK_SIZE;
r += RDS_BLOCK_SIZE;
}
out:
dev_dbg(radio->dev, "%s: exit\n", __func__);
mutex_unlock(&core->lock);
return r;
}
static const struct v4l2_file_operations wl1273_fops = {
.owner = THIS_MODULE,
.read = wl1273_fm_fops_read,
.write = wl1273_fm_fops_write,
.poll = wl1273_fm_fops_poll,
.unlocked_ioctl = video_ioctl2,
.open = wl1273_fm_fops_open,
.release = wl1273_fm_fops_release,
};
static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *capability)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
dev_dbg(radio->dev, "%s\n", __func__);
strscpy(capability->driver, WL1273_FM_DRIVER_NAME,
sizeof(capability->driver));
strscpy(capability->card, "TI Wl1273 FM Radio",
sizeof(capability->card));
strscpy(capability->bus_info, radio->bus_type,
sizeof(capability->bus_info));
return 0;
}
static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
unsigned int *i)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
dev_dbg(radio->dev, "%s\n", __func__);
*i = 0;
return 0;
}
static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
unsigned int i)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
dev_dbg(radio->dev, "%s\n", __func__);
if (i != 0)
return -EINVAL;
return 0;
}
/**
* wl1273_fm_set_tx_power() - Set the transmission power value.
* @radio: A pointer to the device struct.
* @power: The new power value.
*/
static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
{
struct wl1273_core *core = radio->core;
int r;
if (core->mode == WL1273_MODE_OFF ||
core->mode == WL1273_MODE_SUSPENDED)
return -EPERM;
mutex_lock(&core->lock);
/* Convert the dBuV value to chip presentation */
r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
if (r)
goto out;
radio->tx_power = power;
out:
mutex_unlock(&core->lock);
return r;
}
#define WL1273_SPACING_50kHz 1
#define WL1273_SPACING_100kHz 2
#define WL1273_SPACING_200kHz 4
static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
unsigned int spacing)
{
struct wl1273_core *core = radio->core;
int r;
if (spacing == 0) {
r = core->write(core, WL1273_SCAN_SPACING_SET,
WL1273_SPACING_100kHz);
radio->spacing = 100;
} else if (spacing - 50000 < 25000) {
r = core->write(core, WL1273_SCAN_SPACING_SET,
WL1273_SPACING_50kHz);
radio->spacing = 50;
} else if (spacing - 100000 < 50000) {
r = core->write(core, WL1273_SCAN_SPACING_SET,
WL1273_SPACING_100kHz);
radio->spacing = 100;
} else {
r = core->write(core, WL1273_SCAN_SPACING_SET,
WL1273_SPACING_200kHz);
radio->spacing = 200;
}
return r;
}
static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
struct wl1273_device *radio = ctrl->priv;
struct wl1273_core *core = radio->core;
dev_dbg(radio->dev, "%s\n", __func__);
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
switch (ctrl->id) {
case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
ctrl->val = wl1273_fm_get_tx_ctune(radio);
break;
default:
dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
__func__, ctrl->id);
break;
}
mutex_unlock(&core->lock);
return 0;
}
#define WL1273_MUTE_SOFT_ENABLE (1 << 0)
#define WL1273_MUTE_AC (1 << 1)
#define WL1273_MUTE_HARD_LEFT (1 << 2)
#define WL1273_MUTE_HARD_RIGHT (1 << 3)
#define WL1273_MUTE_SOFT_FORCE (1 << 4)
static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
{
return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
}
static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct wl1273_device *radio = to_radio(ctrl);
struct wl1273_core *core = radio->core;
int r = 0;
dev_dbg(radio->dev, "%s\n", __func__);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
if (core->mode == WL1273_MODE_RX && ctrl->val)
r = core->write(core,
WL1273_MUTE_STATUS_SET,
WL1273_MUTE_HARD_LEFT |
WL1273_MUTE_HARD_RIGHT);
else if (core->mode == WL1273_MODE_RX)
r = core->write(core,
WL1273_MUTE_STATUS_SET, 0x0);
else if (core->mode == WL1273_MODE_TX && ctrl->val)
r = core->write(core, WL1273_MUTE, 1);
else if (core->mode == WL1273_MODE_TX)
r = core->write(core, WL1273_MUTE, 0);
mutex_unlock(&core->lock);
break;
case V4L2_CID_AUDIO_VOLUME:
if (ctrl->val == 0)
r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
else
r = core->set_volume(core, core->volume);
break;
case V4L2_CID_TUNE_PREEMPHASIS:
r = wl1273_fm_set_preemphasis(radio, ctrl->val);
break;
case V4L2_CID_TUNE_POWER_LEVEL:
r = wl1273_fm_set_tx_power(radio, ctrl->val);
break;
default:
dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
__func__, ctrl->id);
break;
}
dev_dbg(radio->dev, "%s\n", __func__);
return r;
}
static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
struct v4l2_audio *audio)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
dev_dbg(radio->dev, "%s\n", __func__);
if (audio->index > 1)
return -EINVAL;
strscpy(audio->name, "Radio", sizeof(audio->name));
audio->capability = V4L2_AUDCAP_STEREO;
return 0;
}
static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
const struct v4l2_audio *audio)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
dev_dbg(radio->dev, "%s\n", __func__);
if (audio->index != 0)
return -EINVAL;
return 0;
}
#define WL1273_RDS_NOT_SYNCHRONIZED 0
#define WL1273_RDS_SYNCHRONIZED 1
static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *tuner)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
u16 val;
int r;
dev_dbg(radio->dev, "%s\n", __func__);
if (tuner->index > 0)
return -EINVAL;
strscpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
tuner->type = V4L2_TUNER_RADIO;
tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO |
V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
if (radio->stereo)
tuner->audmode = V4L2_TUNER_MODE_STEREO;
else
tuner->audmode = V4L2_TUNER_MODE_MONO;
if (core->mode != WL1273_MODE_RX)
return 0;
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
r = core->read(core, WL1273_STEREO_GET, &val);
if (r)
goto out;
if (val == 1)
tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
else
tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
r = core->read(core, WL1273_RSSI_LVL_GET, &val);
if (r)
goto out;
tuner->signal = (s16) val;
dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
tuner->afc = 0;
r = core->read(core, WL1273_RDS_SYNC_GET, &val);
if (r)
goto out;
if (val == WL1273_RDS_SYNCHRONIZED)
tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
out:
mutex_unlock(&core->lock);
return r;
}
static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *tuner)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
int r = 0;
dev_dbg(radio->dev, "%s\n", __func__);
dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
if (tuner->index > 0)
return -EINVAL;
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
if (r)
goto out;
if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
else
r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
if (r)
dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
if (r < 0) {
dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
__func__, r);
goto out;
}
radio->stereo = false;
} else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
if (r < 0) {
dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
__func__, r);
goto out;
}
radio->stereo = true;
} else {
dev_err(radio->dev, "%s: tuner->audmode: %d\n",
__func__, tuner->audmode);
r = -EINVAL;
goto out;
}
out:
mutex_unlock(&core->lock);
return r;
}
static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *freq)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
dev_dbg(radio->dev, "%s\n", __func__);
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
freq->type = V4L2_TUNER_RADIO;
freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
mutex_unlock(&core->lock);
return 0;
}
static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *freq)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
int r;
dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
if (freq->type != V4L2_TUNER_RADIO) {
dev_dbg(radio->dev,
"freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
return -EINVAL;
}
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
if (core->mode == WL1273_MODE_RX) {
dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
r = wl1273_fm_set_rx_freq(radio,
WL1273_INV_FREQ(freq->frequency));
if (r)
dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
": set frequency failed with %d\n", r);
} else {
r = wl1273_fm_set_tx_freq(radio,
WL1273_INV_FREQ(freq->frequency));
if (r)
dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
": set frequency failed with %d\n", r);
}
mutex_unlock(&core->lock);
dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
return r;
}
#define WL1273_DEFAULT_SEEK_LEVEL 7
static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
const struct v4l2_hw_freq_seek *seek)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
int r;
dev_dbg(radio->dev, "%s\n", __func__);
if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
return -EINVAL;
if (file->f_flags & O_NONBLOCK)
return -EWOULDBLOCK;
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
if (r)
goto out;
r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
if (r)
dev_warn(radio->dev, "HW seek failed: %d\n", r);
r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
WL1273_DEFAULT_SEEK_LEVEL);
if (r)
dev_warn(radio->dev, "HW seek failed: %d\n", r);
out:
mutex_unlock(&core->lock);
return r;
}
static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
const struct v4l2_modulator *modulator)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
int r = 0;
dev_dbg(radio->dev, "%s\n", __func__);
if (modulator->index > 0)
return -EINVAL;
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
if (r)
goto out;
if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
else
r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
else
r = core->write(core, WL1273_MONO_SET,
WL1273_RX_STEREO);
if (r < 0)
dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
"MONO_SET fails: %d\n", r);
out:
mutex_unlock(&core->lock);
return r;
}
static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
struct v4l2_modulator *modulator)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
u16 val;
int r;
dev_dbg(radio->dev, "%s\n", __func__);
strscpy(modulator->name, WL1273_FM_DRIVER_NAME,
sizeof(modulator->name));
modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
if (core->mode != WL1273_MODE_TX)
return 0;
if (mutex_lock_interruptible(&core->lock))
return -EINTR;
r = core->read(core, WL1273_MONO_SET, &val);
if (r)
goto out;
if (val == WL1273_TX_STEREO)
modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
else
modulator->txsubchans = V4L2_TUNER_SUB_MONO;
if (radio->rds_on)
modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
out:
mutex_unlock(&core->lock);
return 0;
}
static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
{
struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
struct wl1273_core *core = radio->core;
struct device *dev = radio->dev;
u16 val;
int r;
dev_info(dev, DRIVER_DESC);
if (core->mode == WL1273_MODE_OFF) {
dev_info(dev, "Mode: Off\n");
return 0;
}
if (core->mode == WL1273_MODE_SUSPENDED) {
dev_info(dev, "Mode: Suspended\n");
return 0;
}
r = core->read(core, WL1273_ASIC_ID_GET, &val);
if (r)
dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
else
dev_info(dev, "ASIC_ID: 0x%04x\n", val);
r = core->read(core, WL1273_ASIC_VER_GET, &val);
if (r)
dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
else
dev_info(dev, "ASIC Version: 0x%04x\n", val);
r = core->read(core, WL1273_FIRM_VER_GET, &val);
if (r)
dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
else
dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
r = core->read(core, WL1273_BAND_SET, &val);
if (r)
dev_err(dev, "%s: Get BAND fails.\n", __func__);
else
dev_info(dev, "BAND: %d\n", val);
if (core->mode == WL1273_MODE_TX) {
r = core->read(core, WL1273_PUPD_SET, &val);
if (r)
dev_err(dev, "%s: Get PUPD fails.\n", __func__);
else
dev_info(dev, "PUPD: 0x%04x\n", val);
r = core->read(core, WL1273_CHANL_SET, &val);
if (r)
dev_err(dev, "%s: Get CHANL fails.\n", __func__);
else
dev_info(dev, "Tx frequency: %dkHz\n", val*10);
} else if (core->mode == WL1273_MODE_RX) {
int bf = radio->rangelow;
r = core->read(core, WL1273_FREQ_SET, &val);
if (r)
dev_err(dev, "%s: Get FREQ fails.\n", __func__);
else
dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
r = core->read(core, WL1273_MOST_MODE_SET, &val);
if (r)
dev_err(dev, "%s: Get MOST_MODE fails.\n",
__func__);
else if (val == 0)
dev_info(dev, "MOST_MODE: Stereo according to blend\n");
else if (val == 1)
dev_info(dev, "MOST_MODE: Force mono output\n");
else
dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
r = core->read(core, WL1273_MOST_BLEND_SET, &val);
if (r)
dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
else if (val == 0)
dev_info(dev,
"MOST_BLEND: Switched blend & hysteresis.\n");
else if (val == 1)
dev_info(dev, "MOST_BLEND: Soft blend.\n");
else
dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
r = core->read(core, WL1273_STEREO_GET, &val);
if (r)
dev_err(dev, "%s: Get STEREO fails.\n", __func__);
else if (val == 0)
dev_info(dev, "STEREO: Not detected\n");
else if (val == 1)
dev_info(dev, "STEREO: Detected\n");
else
dev_info(dev, "STEREO: Unexpected value: %d\n", val);
r = core->read(core, WL1273_RSSI_LVL_GET, &val);
if (r)
dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
else
dev_info(dev, "RX signal strength: %d\n", (s16) val);
r = core->read(core, WL1273_POWER_SET, &val);
if (r)
dev_err(dev, "%s: Get POWER fails.\n", __func__);
else
dev_info(dev, "POWER: 0x%04x\n", val);
r = core->read(core, WL1273_INT_MASK_SET, &val);
if (r)
dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
else
dev_info(dev, "INT_MASK: 0x%04x\n", val);
r = core->read(core, WL1273_RDS_SYNC_GET, &val);
if (r)
dev_err(dev, "%s: Get RDS_SYNC fails.\n",
__func__);
else if (val == 0)
dev_info(dev, "RDS_SYNC: Not synchronized\n");
else if (val == 1)
dev_info(dev, "RDS_SYNC: Synchronized\n");
else
dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
if (r)
dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
__func__);
else
dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
r = core->read(core, WL1273_VOLUME_SET, &val);
if (r)
dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
else
dev_info(dev, "VOLUME: 0x%04x\n", val);
}
return 0;
}
static void wl1273_vdev_release(struct video_device *dev)
{
}
static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
.s_ctrl = wl1273_fm_vidioc_s_ctrl,
.g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
};
static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
.vidioc_querycap = wl1273_fm_vidioc_querycap,
.vidioc_g_input = wl1273_fm_vidioc_g_input,
.vidioc_s_input = wl1273_fm_vidioc_s_input,
.vidioc_g_audio = wl1273_fm_vidioc_g_audio,
.vidioc_s_audio = wl1273_fm_vidioc_s_audio,
.vidioc_g_tuner = wl1273_fm_vidioc_g_tuner,
.vidioc_s_tuner = wl1273_fm_vidioc_s_tuner,
.vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
.vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
.vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek,
.vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
.vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
.vidioc_log_status = wl1273_fm_vidioc_log_status,
};
static const struct video_device wl1273_viddev_template = {
.fops = &wl1273_fops,
.ioctl_ops = &wl1273_ioctl_ops,
.name = WL1273_FM_DRIVER_NAME,
.release = wl1273_vdev_release,
.vfl_dir = VFL_DIR_TX,
.device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
V4L2_CAP_RDS_OUTPUT,
};
static void wl1273_fm_radio_remove(struct platform_device *pdev)
{
struct wl1273_device *radio = platform_get_drvdata(pdev);
struct wl1273_core *core = radio->core;
dev_info(&pdev->dev, "%s.\n", __func__);
free_irq(core->client->irq, radio);
core->pdata->free_resources();
v4l2_ctrl_handler_free(&radio->ctrl_handler);
video_unregister_device(&radio->videodev);
v4l2_device_unregister(&radio->v4l2dev);
}
static int wl1273_fm_radio_probe(struct platform_device *pdev)
{
struct wl1273_core **core = pdev->dev.platform_data;
struct wl1273_device *radio;
struct v4l2_ctrl *ctrl;
int r = 0;
pr_debug("%s\n", __func__);
if (!core) {
dev_err(&pdev->dev, "No platform data.\n");
r = -EINVAL;
goto pdata_err;
}
radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
if (!radio) {
r = -ENOMEM;
goto pdata_err;
}
/* RDS buffer allocation */
radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
if (!radio->buffer) {
pr_err("Cannot allocate memory for RDS buffer.\n");
r = -ENOMEM;
goto pdata_err;
}
radio->core = *core;
radio->irq_flags = WL1273_IRQ_MASK;
radio->dev = &radio->core->client->dev;
radio->rds_on = false;
radio->core->mode = WL1273_MODE_OFF;
radio->tx_power = 118;
radio->core->audio_mode = WL1273_AUDIO_ANALOG;
radio->band = WL1273_BAND_OTHER;
radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
radio->core->channel_number = 2;
radio->core->volume = WL1273_DEFAULT_VOLUME;
radio->rx_frequency = WL1273_BAND_OTHER_LOW;
radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
radio->rangelow = WL1273_BAND_OTHER_LOW;
radio->rangehigh = WL1273_BAND_OTHER_HIGH;
radio->stereo = true;
radio->bus_type = "I2C";
if (radio->core->pdata->request_resources) {
r = radio->core->pdata->request_resources(radio->core->client);
if (r) {
dev_err(radio->dev, WL1273_FM_DRIVER_NAME
": Cannot get platform data\n");
goto pdata_err;
}
dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
r = request_threaded_irq(radio->core->client->irq, NULL,
wl1273_fm_irq_thread_handler,
IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
"wl1273-fm", radio);
if (r < 0) {
dev_err(radio->dev, WL1273_FM_DRIVER_NAME
": Unable to register IRQ handler: %d\n", r);
goto err_request_irq;
}
} else {
dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured");
r = -EINVAL;
goto pdata_err;
}
init_completion(&radio->busy);
init_waitqueue_head(&radio->read_queue);
radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
if (!radio->write_buf) {
r = -ENOMEM;
goto write_buf_err;
}
radio->dev = &pdev->dev;
radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
radio->rds_users = 0;
r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
if (r) {
dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
goto write_buf_err;
}
/* V4L2 configuration */
radio->videodev = wl1273_viddev_template;
radio->videodev.v4l2_dev = &radio->v4l2dev;
v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
/* add in ascending ID order */
v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
WL1273_DEFAULT_VOLUME);
v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
V4L2_CID_TUNE_PREEMPHASIS,
V4L2_PREEMPHASIS_75_uS, 0x03,
V4L2_PREEMPHASIS_50_uS);
v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
V4L2_CID_TUNE_ANTENNA_CAPACITOR,
0, 255, 1, 255);
if (ctrl)
ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
if (radio->ctrl_handler.error) {
r = radio->ctrl_handler.error;
dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
goto handler_init_err;
}
video_set_drvdata(&radio->videodev, radio);
platform_set_drvdata(pdev, radio);
/* register video device */
r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
if (r) {
dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
": Could not register video device\n");
goto handler_init_err;
}
return 0;
handler_init_err:
v4l2_ctrl_handler_free(&radio->ctrl_handler);
v4l2_device_unregister(&radio->v4l2dev);
write_buf_err:
free_irq(radio->core->client->irq, radio);
err_request_irq:
radio->core->pdata->free_resources();
pdata_err:
return r;
}
static struct platform_driver wl1273_fm_radio_driver = {
.probe = wl1273_fm_radio_probe,
.remove_new = wl1273_fm_radio_remove,
.driver = {
.name = "wl1273_fm_radio",
},
};
module_platform_driver(wl1273_fm_radio_driver);
MODULE_AUTHOR("Matti Aaltonen <[email protected]>");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:wl1273_fm_radio");
| linux-master | drivers/media/radio/radio-wl1273.c |
// SPDX-License-Identifier: GPL-2.0-or-later
/* A driver for the D-Link DSB-R100 USB radio and Gemtek USB Radio 21.
* The device plugs into both the USB and an analog audio input, so this thing
* only deals with initialisation and frequency setting, the
* audio data has to be handled by a sound driver.
*
* Major issue: I can't find out where the device reports the signal
* strength, and indeed the windows software appearantly just looks
* at the stereo indicator as well. So, scanning will only find
* stereo stations. Sad, but I can't help it.
*
* Also, the windows program sends oodles of messages over to the
* device, and I couldn't figure out their meaning. My suspicion
* is that they don't have any:-)
*
* You might find some interesting stuff about this module at
* http://unimut.fsk.uni-heidelberg.de/unimut/demi/dsbr
*
* Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
*
* Copyright (c) 2000 Markus Demleitner <[email protected]>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/videodev2.h>
#include <linux/usb.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
/*
* Version Information
*/
MODULE_AUTHOR("Markus Demleitner <[email protected]>");
MODULE_DESCRIPTION("D-Link DSB-R100 USB FM radio driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.1.0");
#define DSB100_VENDOR 0x04b4
#define DSB100_PRODUCT 0x1002
/* Commands the device appears to understand */
#define DSB100_TUNE 1
#define DSB100_ONOFF 2
#define TB_LEN 16
/* Frequency limits in MHz -- these are European values. For Japanese
devices, that would be 76 and 91. */
#define FREQ_MIN 87.5
#define FREQ_MAX 108.0
#define FREQ_MUL 16000
#define v4l2_dev_to_radio(d) container_of(d, struct dsbr100_device, v4l2_dev)
static int radio_nr = -1;
module_param(radio_nr, int, 0);
/* Data for one (physical) device */
struct dsbr100_device {
struct usb_device *usbdev;
struct video_device videodev;
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler hdl;
u8 *transfer_buffer;
struct mutex v4l2_lock;
int curfreq;
bool stereo;
bool muted;
};
/* Low-level device interface begins here */
/* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
static int dsbr100_setfreq(struct dsbr100_device *radio, unsigned freq)
{
unsigned f = (freq / 16 * 80) / 1000 + 856;
int retval = 0;
if (!radio->muted) {
retval = usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
DSB100_TUNE,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
(f >> 8) & 0x00ff, f & 0xff,
radio->transfer_buffer, 8, 300);
if (retval >= 0)
mdelay(1);
}
if (retval >= 0) {
radio->curfreq = freq;
return 0;
}
dev_err(&radio->usbdev->dev,
"%s - usb_control_msg returned %i, request %i\n",
__func__, retval, DSB100_TUNE);
return retval;
}
/* switch on radio */
static int dsbr100_start(struct dsbr100_device *radio)
{
int retval = usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
DSB100_ONOFF,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0x01, 0x00, radio->transfer_buffer, 8, 300);
if (retval >= 0)
return dsbr100_setfreq(radio, radio->curfreq);
dev_err(&radio->usbdev->dev,
"%s - usb_control_msg returned %i, request %i\n",
__func__, retval, DSB100_ONOFF);
return retval;
}
/* switch off radio */
static int dsbr100_stop(struct dsbr100_device *radio)
{
int retval = usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
DSB100_ONOFF,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0x00, 0x00, radio->transfer_buffer, 8, 300);
if (retval >= 0)
return 0;
dev_err(&radio->usbdev->dev,
"%s - usb_control_msg returned %i, request %i\n",
__func__, retval, DSB100_ONOFF);
return retval;
}
/* return the device status. This is, in effect, just whether it
sees a stereo signal or not. Pity. */
static void dsbr100_getstat(struct dsbr100_device *radio)
{
int retval = usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
USB_REQ_GET_STATUS,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0x00, 0x24, radio->transfer_buffer, 8, 300);
if (retval < 0) {
radio->stereo = false;
dev_err(&radio->usbdev->dev,
"%s - usb_control_msg returned %i, request %i\n",
__func__, retval, USB_REQ_GET_STATUS);
} else {
radio->stereo = !(radio->transfer_buffer[0] & 0x01);
}
}
static int vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *v)
{
struct dsbr100_device *radio = video_drvdata(file);
strscpy(v->driver, "dsbr100", sizeof(v->driver));
strscpy(v->card, "D-Link R-100 USB FM Radio", sizeof(v->card));
usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
return 0;
}
static int vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct dsbr100_device *radio = video_drvdata(file);
if (v->index > 0)
return -EINVAL;
dsbr100_getstat(radio);
strscpy(v->name, "FM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->rangelow = FREQ_MIN * FREQ_MUL;
v->rangehigh = FREQ_MAX * FREQ_MUL;
v->rxsubchans = radio->stereo ? V4L2_TUNER_SUB_STEREO :
V4L2_TUNER_SUB_MONO;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
v->audmode = V4L2_TUNER_MODE_STEREO;
v->signal = radio->stereo ? 0xffff : 0; /* We can't get the signal strength */
return 0;
}
static int vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
return v->index ? -EINVAL : 0;
}
static int vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct dsbr100_device *radio = video_drvdata(file);
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
return dsbr100_setfreq(radio, clamp_t(unsigned, f->frequency,
FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
}
static int vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct dsbr100_device *radio = video_drvdata(file);
if (f->tuner)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
f->frequency = radio->curfreq;
return 0;
}
static int usb_dsbr100_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct dsbr100_device *radio =
container_of(ctrl->handler, struct dsbr100_device, hdl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
radio->muted = ctrl->val;
return radio->muted ? dsbr100_stop(radio) : dsbr100_start(radio);
}
return -EINVAL;
}
/* USB subsystem interface begins here */
/*
* Handle unplugging of the device.
* We call video_unregister_device in any case.
* The last function called in this procedure is
* usb_dsbr100_video_device_release
*/
static void usb_dsbr100_disconnect(struct usb_interface *intf)
{
struct dsbr100_device *radio = usb_get_intfdata(intf);
mutex_lock(&radio->v4l2_lock);
/*
* Disconnect is also called on unload, and in that case we need to
* mute the device. This call will silently fail if it is called
* after a physical disconnect.
*/
usb_control_msg(radio->usbdev,
usb_rcvctrlpipe(radio->usbdev, 0),
DSB100_ONOFF,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0x00, 0x00, radio->transfer_buffer, 8, 300);
usb_set_intfdata(intf, NULL);
video_unregister_device(&radio->videodev);
v4l2_device_disconnect(&radio->v4l2_dev);
mutex_unlock(&radio->v4l2_lock);
v4l2_device_put(&radio->v4l2_dev);
}
/* Suspend device - stop device. */
static int usb_dsbr100_suspend(struct usb_interface *intf, pm_message_t message)
{
struct dsbr100_device *radio = usb_get_intfdata(intf);
mutex_lock(&radio->v4l2_lock);
if (!radio->muted && dsbr100_stop(radio) < 0)
dev_warn(&intf->dev, "dsbr100_stop failed\n");
mutex_unlock(&radio->v4l2_lock);
dev_info(&intf->dev, "going into suspend..\n");
return 0;
}
/* Resume device - start device. */
static int usb_dsbr100_resume(struct usb_interface *intf)
{
struct dsbr100_device *radio = usb_get_intfdata(intf);
mutex_lock(&radio->v4l2_lock);
if (!radio->muted && dsbr100_start(radio) < 0)
dev_warn(&intf->dev, "dsbr100_start failed\n");
mutex_unlock(&radio->v4l2_lock);
dev_info(&intf->dev, "coming out of suspend..\n");
return 0;
}
/* free data structures */
static void usb_dsbr100_release(struct v4l2_device *v4l2_dev)
{
struct dsbr100_device *radio = v4l2_dev_to_radio(v4l2_dev);
v4l2_ctrl_handler_free(&radio->hdl);
v4l2_device_unregister(&radio->v4l2_dev);
kfree(radio->transfer_buffer);
kfree(radio);
}
static const struct v4l2_ctrl_ops usb_dsbr100_ctrl_ops = {
.s_ctrl = usb_dsbr100_s_ctrl,
};
/* File system interface */
static const struct v4l2_file_operations usb_dsbr100_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = video_ioctl2,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
};
static const struct v4l2_ioctl_ops usb_dsbr100_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_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
/* check if the device is present and register with v4l and usb if it is */
static int usb_dsbr100_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct dsbr100_device *radio;
struct v4l2_device *v4l2_dev;
int retval;
radio = kzalloc(sizeof(struct dsbr100_device), GFP_KERNEL);
if (!radio)
return -ENOMEM;
radio->transfer_buffer = kmalloc(TB_LEN, GFP_KERNEL);
if (!(radio->transfer_buffer)) {
kfree(radio);
return -ENOMEM;
}
v4l2_dev = &radio->v4l2_dev;
v4l2_dev->release = usb_dsbr100_release;
retval = v4l2_device_register(&intf->dev, v4l2_dev);
if (retval < 0) {
v4l2_err(v4l2_dev, "couldn't register v4l2_device\n");
goto err_reg_dev;
}
v4l2_ctrl_handler_init(&radio->hdl, 1);
v4l2_ctrl_new_std(&radio->hdl, &usb_dsbr100_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
if (radio->hdl.error) {
retval = radio->hdl.error;
v4l2_err(v4l2_dev, "couldn't register control\n");
goto err_reg_ctrl;
}
mutex_init(&radio->v4l2_lock);
strscpy(radio->videodev.name, v4l2_dev->name,
sizeof(radio->videodev.name));
radio->videodev.v4l2_dev = v4l2_dev;
radio->videodev.fops = &usb_dsbr100_fops;
radio->videodev.ioctl_ops = &usb_dsbr100_ioctl_ops;
radio->videodev.release = video_device_release_empty;
radio->videodev.lock = &radio->v4l2_lock;
radio->videodev.ctrl_handler = &radio->hdl;
radio->videodev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
radio->usbdev = interface_to_usbdev(intf);
radio->curfreq = FREQ_MIN * FREQ_MUL;
radio->muted = true;
video_set_drvdata(&radio->videodev, radio);
usb_set_intfdata(intf, radio);
retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
if (retval == 0)
return 0;
v4l2_err(v4l2_dev, "couldn't register video device\n");
err_reg_ctrl:
v4l2_ctrl_handler_free(&radio->hdl);
v4l2_device_unregister(v4l2_dev);
err_reg_dev:
kfree(radio->transfer_buffer);
kfree(radio);
return retval;
}
static const struct usb_device_id usb_dsbr100_device_table[] = {
{ USB_DEVICE(DSB100_VENDOR, DSB100_PRODUCT) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_dsbr100_device_table);
/* USB subsystem interface */
static struct usb_driver usb_dsbr100_driver = {
.name = "dsbr100",
.probe = usb_dsbr100_probe,
.disconnect = usb_dsbr100_disconnect,
.id_table = usb_dsbr100_device_table,
.suspend = usb_dsbr100_suspend,
.resume = usb_dsbr100_resume,
.reset_resume = usb_dsbr100_resume,
};
module_usb_driver(usb_dsbr100_driver);
| linux-master | drivers/media/radio/dsbr100.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* FM Driver for Connectivity chip of Texas Instruments.
*
* This sub-module of FM driver is common for FM RX and TX
* functionality. This module is responsible for:
* 1) Forming group of Channel-8 commands to perform particular
* functionality (eg., frequency set require more than
* one Channel-8 command to be sent to the chip).
* 2) Sending each Channel-8 command to the chip and reading
* response back over Shared Transport.
* 3) Managing TX and RX Queues and Tasklets.
* 4) Handling FM Interrupt packet and taking appropriate action.
* 5) Loading FM firmware to the chip (common, FM TX, and FM RX
* firmware files based on mode selection)
*
* Copyright (C) 2011 Texas Instruments
* Author: Raja Mani <[email protected]>
* Author: Manjunatha Halli <[email protected]>
*/
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/nospec.h>
#include <linux/jiffies.h>
#include "fmdrv.h"
#include "fmdrv_v4l2.h"
#include "fmdrv_common.h"
#include <linux/ti_wilink_st.h>
#include "fmdrv_rx.h"
#include "fmdrv_tx.h"
/* Region info */
static struct region_info region_configs[] = {
/* Europe/US */
{
.chanl_space = FM_CHANNEL_SPACING_200KHZ * FM_FREQ_MUL,
.bot_freq = 87500, /* 87.5 MHz */
.top_freq = 108000, /* 108 MHz */
.fm_band = 0,
},
/* Japan */
{
.chanl_space = FM_CHANNEL_SPACING_200KHZ * FM_FREQ_MUL,
.bot_freq = 76000, /* 76 MHz */
.top_freq = 90000, /* 90 MHz */
.fm_band = 1,
},
};
/* Band selection */
static u8 default_radio_region; /* Europe/US */
module_param(default_radio_region, byte, 0);
MODULE_PARM_DESC(default_radio_region, "Region: 0=Europe/US, 1=Japan");
/* RDS buffer blocks */
static u32 default_rds_buf = 300;
module_param(default_rds_buf, uint, 0444);
MODULE_PARM_DESC(default_rds_buf, "RDS buffer entries");
/* Radio Nr */
static u32 radio_nr = -1;
module_param(radio_nr, int, 0444);
MODULE_PARM_DESC(radio_nr, "Radio Nr");
/* FM irq handlers forward declaration */
static void fm_irq_send_flag_getcmd(struct fmdev *);
static void fm_irq_handle_flag_getcmd_resp(struct fmdev *);
static void fm_irq_handle_hw_malfunction(struct fmdev *);
static void fm_irq_handle_rds_start(struct fmdev *);
static void fm_irq_send_rdsdata_getcmd(struct fmdev *);
static void fm_irq_handle_rdsdata_getcmd_resp(struct fmdev *);
static void fm_irq_handle_rds_finish(struct fmdev *);
static void fm_irq_handle_tune_op_ended(struct fmdev *);
static void fm_irq_handle_power_enb(struct fmdev *);
static void fm_irq_handle_low_rssi_start(struct fmdev *);
static void fm_irq_afjump_set_pi(struct fmdev *);
static void fm_irq_handle_set_pi_resp(struct fmdev *);
static void fm_irq_afjump_set_pimask(struct fmdev *);
static void fm_irq_handle_set_pimask_resp(struct fmdev *);
static void fm_irq_afjump_setfreq(struct fmdev *);
static void fm_irq_handle_setfreq_resp(struct fmdev *);
static void fm_irq_afjump_enableint(struct fmdev *);
static void fm_irq_afjump_enableint_resp(struct fmdev *);
static void fm_irq_start_afjump(struct fmdev *);
static void fm_irq_handle_start_afjump_resp(struct fmdev *);
static void fm_irq_afjump_rd_freq(struct fmdev *);
static void fm_irq_afjump_rd_freq_resp(struct fmdev *);
static void fm_irq_handle_low_rssi_finish(struct fmdev *);
static void fm_irq_send_intmsk_cmd(struct fmdev *);
static void fm_irq_handle_intmsk_cmd_resp(struct fmdev *);
/*
* When FM common module receives interrupt packet, following handlers
* will be executed one after another to service the interrupt(s)
*/
enum fmc_irq_handler_index {
FM_SEND_FLAG_GETCMD_IDX,
FM_HANDLE_FLAG_GETCMD_RESP_IDX,
/* HW malfunction irq handler */
FM_HW_MAL_FUNC_IDX,
/* RDS threshold reached irq handler */
FM_RDS_START_IDX,
FM_RDS_SEND_RDS_GETCMD_IDX,
FM_RDS_HANDLE_RDS_GETCMD_RESP_IDX,
FM_RDS_FINISH_IDX,
/* Tune operation ended irq handler */
FM_HW_TUNE_OP_ENDED_IDX,
/* TX power enable irq handler */
FM_HW_POWER_ENB_IDX,
/* Low RSSI irq handler */
FM_LOW_RSSI_START_IDX,
FM_AF_JUMP_SETPI_IDX,
FM_AF_JUMP_HANDLE_SETPI_RESP_IDX,
FM_AF_JUMP_SETPI_MASK_IDX,
FM_AF_JUMP_HANDLE_SETPI_MASK_RESP_IDX,
FM_AF_JUMP_SET_AF_FREQ_IDX,
FM_AF_JUMP_HANDLE_SET_AFFREQ_RESP_IDX,
FM_AF_JUMP_ENABLE_INT_IDX,
FM_AF_JUMP_ENABLE_INT_RESP_IDX,
FM_AF_JUMP_START_AFJUMP_IDX,
FM_AF_JUMP_HANDLE_START_AFJUMP_RESP_IDX,
FM_AF_JUMP_RD_FREQ_IDX,
FM_AF_JUMP_RD_FREQ_RESP_IDX,
FM_LOW_RSSI_FINISH_IDX,
/* Interrupt process post action */
FM_SEND_INTMSK_CMD_IDX,
FM_HANDLE_INTMSK_CMD_RESP_IDX,
};
/* FM interrupt handler table */
static int_handler_prototype int_handler_table[] = {
fm_irq_send_flag_getcmd,
fm_irq_handle_flag_getcmd_resp,
fm_irq_handle_hw_malfunction,
fm_irq_handle_rds_start, /* RDS threshold reached irq handler */
fm_irq_send_rdsdata_getcmd,
fm_irq_handle_rdsdata_getcmd_resp,
fm_irq_handle_rds_finish,
fm_irq_handle_tune_op_ended,
fm_irq_handle_power_enb, /* TX power enable irq handler */
fm_irq_handle_low_rssi_start,
fm_irq_afjump_set_pi,
fm_irq_handle_set_pi_resp,
fm_irq_afjump_set_pimask,
fm_irq_handle_set_pimask_resp,
fm_irq_afjump_setfreq,
fm_irq_handle_setfreq_resp,
fm_irq_afjump_enableint,
fm_irq_afjump_enableint_resp,
fm_irq_start_afjump,
fm_irq_handle_start_afjump_resp,
fm_irq_afjump_rd_freq,
fm_irq_afjump_rd_freq_resp,
fm_irq_handle_low_rssi_finish,
fm_irq_send_intmsk_cmd, /* Interrupt process post action */
fm_irq_handle_intmsk_cmd_resp
};
static long (*g_st_write) (struct sk_buff *skb);
static struct completion wait_for_fmdrv_reg_comp;
static inline void fm_irq_call(struct fmdev *fmdev)
{
fmdev->irq_info.handlers[fmdev->irq_info.stage](fmdev);
}
/* Continue next function in interrupt handler table */
static inline void fm_irq_call_stage(struct fmdev *fmdev, u8 stage)
{
fmdev->irq_info.stage = stage;
fm_irq_call(fmdev);
}
static inline void fm_irq_timeout_stage(struct fmdev *fmdev, u8 stage)
{
fmdev->irq_info.stage = stage;
mod_timer(&fmdev->irq_info.timer, jiffies + FM_DRV_TX_TIMEOUT);
}
#ifdef FM_DUMP_TXRX_PKT
/* To dump outgoing FM Channel-8 packets */
inline void dump_tx_skb_data(struct sk_buff *skb)
{
int len, len_org;
u8 index;
struct fm_cmd_msg_hdr *cmd_hdr;
cmd_hdr = (struct fm_cmd_msg_hdr *)skb->data;
printk(KERN_INFO "<<%shdr:%02x len:%02x opcode:%02x type:%s dlen:%02x",
fm_cb(skb)->completion ? " " : "*", cmd_hdr->hdr,
cmd_hdr->len, cmd_hdr->op,
cmd_hdr->rd_wr ? "RD" : "WR", cmd_hdr->dlen);
len_org = skb->len - FM_CMD_MSG_HDR_SIZE;
if (len_org > 0) {
printk(KERN_CONT "\n data(%d): ", cmd_hdr->dlen);
len = min(len_org, 14);
for (index = 0; index < len; index++)
printk(KERN_CONT "%x ",
skb->data[FM_CMD_MSG_HDR_SIZE + index]);
printk(KERN_CONT "%s", (len_org > 14) ? ".." : "");
}
printk(KERN_CONT "\n");
}
/* To dump incoming FM Channel-8 packets */
inline void dump_rx_skb_data(struct sk_buff *skb)
{
int len, len_org;
u8 index;
struct fm_event_msg_hdr *evt_hdr;
evt_hdr = (struct fm_event_msg_hdr *)skb->data;
printk(KERN_INFO ">> hdr:%02x len:%02x sts:%02x numhci:%02x opcode:%02x type:%s dlen:%02x",
evt_hdr->hdr, evt_hdr->len,
evt_hdr->status, evt_hdr->num_fm_hci_cmds, evt_hdr->op,
(evt_hdr->rd_wr) ? "RD" : "WR", evt_hdr->dlen);
len_org = skb->len - FM_EVT_MSG_HDR_SIZE;
if (len_org > 0) {
printk(KERN_CONT "\n data(%d): ", evt_hdr->dlen);
len = min(len_org, 14);
for (index = 0; index < len; index++)
printk(KERN_CONT "%x ",
skb->data[FM_EVT_MSG_HDR_SIZE + index]);
printk(KERN_CONT "%s", (len_org > 14) ? ".." : "");
}
printk(KERN_CONT "\n");
}
#endif
void fmc_update_region_info(struct fmdev *fmdev, u8 region_to_set)
{
fmdev->rx.region = region_configs[region_to_set];
}
/*
* FM common sub-module will schedule this tasklet whenever it receives
* FM packet from ST driver.
*/
static void recv_tasklet(struct tasklet_struct *t)
{
struct fmdev *fmdev;
struct fm_irq *irq_info;
struct fm_event_msg_hdr *evt_hdr;
struct sk_buff *skb;
u8 num_fm_hci_cmds;
unsigned long flags;
fmdev = from_tasklet(fmdev, t, tx_task);
irq_info = &fmdev->irq_info;
/* Process all packets in the RX queue */
while ((skb = skb_dequeue(&fmdev->rx_q))) {
if (skb->len < sizeof(struct fm_event_msg_hdr)) {
fmerr("skb(%p) has only %d bytes, at least need %zu bytes to decode\n",
skb,
skb->len, sizeof(struct fm_event_msg_hdr));
kfree_skb(skb);
continue;
}
evt_hdr = (void *)skb->data;
num_fm_hci_cmds = evt_hdr->num_fm_hci_cmds;
/* FM interrupt packet? */
if (evt_hdr->op == FM_INTERRUPT) {
/* FM interrupt handler started already? */
if (!test_bit(FM_INTTASK_RUNNING, &fmdev->flag)) {
set_bit(FM_INTTASK_RUNNING, &fmdev->flag);
if (irq_info->stage != 0) {
fmerr("Inval stage resetting to zero\n");
irq_info->stage = 0;
}
/*
* Execute first function in interrupt handler
* table.
*/
irq_info->handlers[irq_info->stage](fmdev);
} else {
set_bit(FM_INTTASK_SCHEDULE_PENDING, &fmdev->flag);
}
kfree_skb(skb);
}
/* Anyone waiting for this with completion handler? */
else if (evt_hdr->op == fmdev->pre_op && fmdev->resp_comp != NULL) {
spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
fmdev->resp_skb = skb;
spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
complete(fmdev->resp_comp);
fmdev->resp_comp = NULL;
atomic_set(&fmdev->tx_cnt, 1);
}
/* Is this for interrupt handler? */
else if (evt_hdr->op == fmdev->pre_op && fmdev->resp_comp == NULL) {
if (fmdev->resp_skb != NULL)
fmerr("Response SKB ptr not NULL\n");
spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
fmdev->resp_skb = skb;
spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
/* Execute interrupt handler where state index points */
irq_info->handlers[irq_info->stage](fmdev);
kfree_skb(skb);
atomic_set(&fmdev->tx_cnt, 1);
} else {
fmerr("Nobody claimed SKB(%p),purging\n", skb);
}
/*
* Check flow control field. If Num_FM_HCI_Commands field is
* not zero, schedule FM TX tasklet.
*/
if (num_fm_hci_cmds && atomic_read(&fmdev->tx_cnt))
if (!skb_queue_empty(&fmdev->tx_q))
tasklet_schedule(&fmdev->tx_task);
}
}
/* FM send tasklet: is scheduled when FM packet has to be sent to chip */
static void send_tasklet(struct tasklet_struct *t)
{
struct fmdev *fmdev;
struct sk_buff *skb;
int len;
fmdev = from_tasklet(fmdev, t, tx_task);
if (!atomic_read(&fmdev->tx_cnt))
return;
/* Check, is there any timeout happened to last transmitted packet */
if (time_is_before_jiffies(fmdev->last_tx_jiffies + FM_DRV_TX_TIMEOUT)) {
fmerr("TX timeout occurred\n");
atomic_set(&fmdev->tx_cnt, 1);
}
/* Send queued FM TX packets */
skb = skb_dequeue(&fmdev->tx_q);
if (!skb)
return;
atomic_dec(&fmdev->tx_cnt);
fmdev->pre_op = fm_cb(skb)->fm_op;
if (fmdev->resp_comp != NULL)
fmerr("Response completion handler is not NULL\n");
fmdev->resp_comp = fm_cb(skb)->completion;
/* Write FM packet to ST driver */
len = g_st_write(skb);
if (len < 0) {
kfree_skb(skb);
fmdev->resp_comp = NULL;
fmerr("TX tasklet failed to send skb(%p)\n", skb);
atomic_set(&fmdev->tx_cnt, 1);
} else {
fmdev->last_tx_jiffies = jiffies;
}
}
/*
* Queues FM Channel-8 packet to FM TX queue and schedules FM TX tasklet for
* transmission
*/
static int fm_send_cmd(struct fmdev *fmdev, u8 fm_op, u16 type, void *payload,
int payload_len, struct completion *wait_completion)
{
struct sk_buff *skb;
struct fm_cmd_msg_hdr *hdr;
int size;
if (fm_op >= FM_INTERRUPT) {
fmerr("Invalid fm opcode - %d\n", fm_op);
return -EINVAL;
}
if (test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag) && payload == NULL) {
fmerr("Payload data is NULL during fw download\n");
return -EINVAL;
}
if (!test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag))
size =
FM_CMD_MSG_HDR_SIZE + ((payload == NULL) ? 0 : payload_len);
else
size = payload_len;
skb = alloc_skb(size, GFP_ATOMIC);
if (!skb) {
fmerr("No memory to create new SKB\n");
return -ENOMEM;
}
/*
* Don't fill FM header info for the commands which come from
* FM firmware file.
*/
if (!test_bit(FM_FW_DW_INPROGRESS, &fmdev->flag) ||
test_bit(FM_INTTASK_RUNNING, &fmdev->flag)) {
/* Fill command header info */
hdr = skb_put(skb, FM_CMD_MSG_HDR_SIZE);
hdr->hdr = FM_PKT_LOGICAL_CHAN_NUMBER; /* 0x08 */
/* 3 (fm_opcode,rd_wr,dlen) + payload len) */
hdr->len = ((payload == NULL) ? 0 : payload_len) + 3;
/* FM opcode */
hdr->op = fm_op;
/* read/write type */
hdr->rd_wr = type;
hdr->dlen = payload_len;
fm_cb(skb)->fm_op = fm_op;
/*
* If firmware download has finished and the command is
* not a read command then payload is != NULL - a write
* command with u16 payload - convert to be16
*/
if (payload != NULL)
*(__be16 *)payload = cpu_to_be16(*(u16 *)payload);
} else if (payload != NULL) {
fm_cb(skb)->fm_op = *((u8 *)payload + 2);
}
if (payload != NULL)
skb_put_data(skb, payload, payload_len);
fm_cb(skb)->completion = wait_completion;
skb_queue_tail(&fmdev->tx_q, skb);
tasklet_schedule(&fmdev->tx_task);
return 0;
}
/* Sends FM Channel-8 command to the chip and waits for the response */
int fmc_send_cmd(struct fmdev *fmdev, u8 fm_op, u16 type, void *payload,
unsigned int payload_len, void *response, int *response_len)
{
struct sk_buff *skb;
struct fm_event_msg_hdr *evt_hdr;
unsigned long flags;
int ret;
init_completion(&fmdev->maintask_comp);
ret = fm_send_cmd(fmdev, fm_op, type, payload, payload_len,
&fmdev->maintask_comp);
if (ret)
return ret;
if (!wait_for_completion_timeout(&fmdev->maintask_comp,
FM_DRV_TX_TIMEOUT)) {
fmerr("Timeout(%d sec),didn't get regcompletion signal from RX tasklet\n",
jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
return -ETIMEDOUT;
}
if (!fmdev->resp_skb) {
fmerr("Response SKB is missing\n");
return -EFAULT;
}
spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
skb = fmdev->resp_skb;
fmdev->resp_skb = NULL;
spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
evt_hdr = (void *)skb->data;
if (evt_hdr->status != 0) {
fmerr("Received event pkt status(%d) is not zero\n",
evt_hdr->status);
kfree_skb(skb);
return -EIO;
}
/* Send response data to caller */
if (response != NULL && response_len != NULL && evt_hdr->dlen &&
evt_hdr->dlen <= payload_len) {
/* Skip header info and copy only response data */
skb_pull(skb, sizeof(struct fm_event_msg_hdr));
memcpy(response, skb->data, evt_hdr->dlen);
*response_len = evt_hdr->dlen;
} else if (response_len != NULL && evt_hdr->dlen == 0) {
*response_len = 0;
}
kfree_skb(skb);
return 0;
}
/* --- Helper functions used in FM interrupt handlers ---*/
static inline int check_cmdresp_status(struct fmdev *fmdev,
struct sk_buff **skb)
{
struct fm_event_msg_hdr *fm_evt_hdr;
unsigned long flags;
del_timer(&fmdev->irq_info.timer);
spin_lock_irqsave(&fmdev->resp_skb_lock, flags);
*skb = fmdev->resp_skb;
fmdev->resp_skb = NULL;
spin_unlock_irqrestore(&fmdev->resp_skb_lock, flags);
fm_evt_hdr = (void *)(*skb)->data;
if (fm_evt_hdr->status != 0) {
fmerr("irq: opcode %x response status is not zero Initiating irq recovery process\n",
fm_evt_hdr->op);
mod_timer(&fmdev->irq_info.timer, jiffies + FM_DRV_TX_TIMEOUT);
return -1;
}
return 0;
}
static inline void fm_irq_common_cmd_resp_helper(struct fmdev *fmdev, u8 stage)
{
struct sk_buff *skb;
if (!check_cmdresp_status(fmdev, &skb))
fm_irq_call_stage(fmdev, stage);
}
/*
* Interrupt process timeout handler.
* One of the irq handler did not get proper response from the chip. So take
* recovery action here. FM interrupts are disabled in the beginning of
* interrupt process. Therefore reset stage index to re-enable default
* interrupts. So that next interrupt will be processed as usual.
*/
static void int_timeout_handler(struct timer_list *t)
{
struct fmdev *fmdev;
struct fm_irq *fmirq;
fmdbg("irq: timeout,trying to re-enable fm interrupts\n");
fmdev = from_timer(fmdev, t, irq_info.timer);
fmirq = &fmdev->irq_info;
fmirq->retry++;
if (fmirq->retry > FM_IRQ_TIMEOUT_RETRY_MAX) {
/* Stop recovery action (interrupt reenable process) and
* reset stage index & retry count values */
fmirq->stage = 0;
fmirq->retry = 0;
fmerr("Recovery action failed duringirq processing, max retry reached\n");
return;
}
fm_irq_call_stage(fmdev, FM_SEND_INTMSK_CMD_IDX);
}
/* --------- FM interrupt handlers ------------*/
static void fm_irq_send_flag_getcmd(struct fmdev *fmdev)
{
u16 flag;
/* Send FLAG_GET command , to know the source of interrupt */
if (!fm_send_cmd(fmdev, FLAG_GET, REG_RD, NULL, sizeof(flag), NULL))
fm_irq_timeout_stage(fmdev, FM_HANDLE_FLAG_GETCMD_RESP_IDX);
}
static void fm_irq_handle_flag_getcmd_resp(struct fmdev *fmdev)
{
struct sk_buff *skb;
struct fm_event_msg_hdr *fm_evt_hdr;
if (check_cmdresp_status(fmdev, &skb))
return;
fm_evt_hdr = (void *)skb->data;
if (fm_evt_hdr->dlen > sizeof(fmdev->irq_info.flag))
return;
/* Skip header info and copy only response data */
skb_pull(skb, sizeof(struct fm_event_msg_hdr));
memcpy(&fmdev->irq_info.flag, skb->data, fm_evt_hdr->dlen);
fmdev->irq_info.flag = be16_to_cpu((__force __be16)fmdev->irq_info.flag);
fmdbg("irq: flag register(0x%x)\n", fmdev->irq_info.flag);
/* Continue next function in interrupt handler table */
fm_irq_call_stage(fmdev, FM_HW_MAL_FUNC_IDX);
}
static void fm_irq_handle_hw_malfunction(struct fmdev *fmdev)
{
if (fmdev->irq_info.flag & FM_MAL_EVENT & fmdev->irq_info.mask)
fmerr("irq: HW MAL int received - do nothing\n");
/* Continue next function in interrupt handler table */
fm_irq_call_stage(fmdev, FM_RDS_START_IDX);
}
static void fm_irq_handle_rds_start(struct fmdev *fmdev)
{
if (fmdev->irq_info.flag & FM_RDS_EVENT & fmdev->irq_info.mask) {
fmdbg("irq: rds threshold reached\n");
fmdev->irq_info.stage = FM_RDS_SEND_RDS_GETCMD_IDX;
} else {
/* Continue next function in interrupt handler table */
fmdev->irq_info.stage = FM_HW_TUNE_OP_ENDED_IDX;
}
fm_irq_call(fmdev);
}
static void fm_irq_send_rdsdata_getcmd(struct fmdev *fmdev)
{
/* Send the command to read RDS data from the chip */
if (!fm_send_cmd(fmdev, RDS_DATA_GET, REG_RD, NULL,
(FM_RX_RDS_FIFO_THRESHOLD * 3), NULL))
fm_irq_timeout_stage(fmdev, FM_RDS_HANDLE_RDS_GETCMD_RESP_IDX);
}
/* Keeps track of current RX channel AF (Alternate Frequency) */
static void fm_rx_update_af_cache(struct fmdev *fmdev, u8 af)
{
struct tuned_station_info *stat_info = &fmdev->rx.stat_info;
u8 reg_idx = fmdev->rx.region.fm_band;
u8 index;
u32 freq;
/* First AF indicates the number of AF follows. Reset the list */
if ((af >= FM_RDS_1_AF_FOLLOWS) && (af <= FM_RDS_25_AF_FOLLOWS)) {
fmdev->rx.stat_info.af_list_max = (af - FM_RDS_1_AF_FOLLOWS + 1);
fmdev->rx.stat_info.afcache_size = 0;
fmdbg("No of expected AF : %d\n", fmdev->rx.stat_info.af_list_max);
return;
}
if (af < FM_RDS_MIN_AF)
return;
if (reg_idx == FM_BAND_EUROPE_US && af > FM_RDS_MAX_AF)
return;
if (reg_idx == FM_BAND_JAPAN && af > FM_RDS_MAX_AF_JAPAN)
return;
freq = fmdev->rx.region.bot_freq + (af * 100);
if (freq == fmdev->rx.freq) {
fmdbg("Current freq(%d) is matching with received AF(%d)\n",
fmdev->rx.freq, freq);
return;
}
/* Do check in AF cache */
for (index = 0; index < stat_info->afcache_size; index++) {
if (stat_info->af_cache[index] == freq)
break;
}
/* Reached the limit of the list - ignore the next AF */
if (index == stat_info->af_list_max) {
fmdbg("AF cache is full\n");
return;
}
/*
* If we reached the end of the list then this AF is not
* in the list - add it.
*/
if (index == stat_info->afcache_size) {
fmdbg("Storing AF %d to cache index %d\n", freq, index);
stat_info->af_cache[index] = freq;
stat_info->afcache_size++;
}
}
/*
* Converts RDS buffer data from big endian format
* to little endian format.
*/
static void fm_rdsparse_swapbytes(struct fmdev *fmdev,
struct fm_rdsdata_format *rds_format)
{
u8 index = 0;
u8 *rds_buff;
/*
* Since in Orca the 2 RDS Data bytes are in little endian and
* in Dolphin they are in big endian, the parsing of the RDS data
* is chip dependent
*/
if (fmdev->asci_id != 0x6350) {
rds_buff = &rds_format->data.groupdatabuff.buff[0];
while (index + 1 < FM_RX_RDS_INFO_FIELD_MAX) {
swap(rds_buff[index], rds_buff[index + 1]);
index += 2;
}
}
}
static void fm_irq_handle_rdsdata_getcmd_resp(struct fmdev *fmdev)
{
struct sk_buff *skb;
struct fm_rdsdata_format rds_fmt;
struct fm_rds *rds = &fmdev->rx.rds;
unsigned long group_idx, flags;
u8 *rds_data, meta_data, tmpbuf[FM_RDS_BLK_SIZE];
u8 type, blk_idx, idx;
u16 cur_picode;
u32 rds_len;
if (check_cmdresp_status(fmdev, &skb))
return;
/* Skip header info */
skb_pull(skb, sizeof(struct fm_event_msg_hdr));
rds_data = skb->data;
rds_len = skb->len;
/* Parse the RDS data */
while (rds_len >= FM_RDS_BLK_SIZE) {
meta_data = rds_data[2];
/* Get the type: 0=A, 1=B, 2=C, 3=C', 4=D, 5=E */
type = (meta_data & 0x07);
/* Transform the blk type into index sequence (0, 1, 2, 3, 4) */
blk_idx = (type <= FM_RDS_BLOCK_C ? type : (type - 1));
fmdbg("Block index:%d(%s)\n", blk_idx,
(meta_data & FM_RDS_STATUS_ERR_MASK) ? "Bad" : "Ok");
if ((meta_data & FM_RDS_STATUS_ERR_MASK) != 0)
break;
if (blk_idx > FM_RDS_BLK_IDX_D) {
fmdbg("Block sequence mismatch\n");
rds->last_blk_idx = -1;
break;
}
/* Skip checkword (control) byte and copy only data byte */
idx = array_index_nospec(blk_idx * (FM_RDS_BLK_SIZE - 1),
FM_RX_RDS_INFO_FIELD_MAX - (FM_RDS_BLK_SIZE - 1));
memcpy(&rds_fmt.data.groupdatabuff.buff[idx], rds_data,
FM_RDS_BLK_SIZE - 1);
rds->last_blk_idx = blk_idx;
/* If completed a whole group then handle it */
if (blk_idx == FM_RDS_BLK_IDX_D) {
fmdbg("Good block received\n");
fm_rdsparse_swapbytes(fmdev, &rds_fmt);
/*
* Extract PI code and store in local cache.
* We need this during AF switch processing.
*/
cur_picode = be16_to_cpu((__force __be16)rds_fmt.data.groupgeneral.pidata);
if (fmdev->rx.stat_info.picode != cur_picode)
fmdev->rx.stat_info.picode = cur_picode;
fmdbg("picode:%d\n", cur_picode);
group_idx = (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
fmdbg("(fmdrv):Group:%ld%s\n", group_idx/2,
(group_idx % 2) ? "B" : "A");
group_idx = 1 << (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
if (group_idx == FM_RDS_GROUP_TYPE_MASK_0A) {
fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[0]);
fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[1]);
}
}
rds_len -= FM_RDS_BLK_SIZE;
rds_data += FM_RDS_BLK_SIZE;
}
/* Copy raw rds data to internal rds buffer */
rds_data = skb->data;
rds_len = skb->len;
spin_lock_irqsave(&fmdev->rds_buff_lock, flags);
while (rds_len > 0) {
/*
* Fill RDS buffer as per V4L2 specification.
* Store control byte
*/
type = (rds_data[2] & 0x07);
blk_idx = (type <= FM_RDS_BLOCK_C ? type : (type - 1));
tmpbuf[2] = blk_idx; /* Offset name */
tmpbuf[2] |= blk_idx << 3; /* Received offset */
/* Store data byte */
tmpbuf[0] = rds_data[0];
tmpbuf[1] = rds_data[1];
memcpy(&rds->buff[rds->wr_idx], &tmpbuf, FM_RDS_BLK_SIZE);
rds->wr_idx = (rds->wr_idx + FM_RDS_BLK_SIZE) % rds->buf_size;
/* Check for overflow & start over */
if (rds->wr_idx == rds->rd_idx) {
fmdbg("RDS buffer overflow\n");
rds->wr_idx = 0;
rds->rd_idx = 0;
break;
}
rds_len -= FM_RDS_BLK_SIZE;
rds_data += FM_RDS_BLK_SIZE;
}
spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
/* Wakeup read queue */
if (rds->wr_idx != rds->rd_idx)
wake_up_interruptible(&rds->read_queue);
fm_irq_call_stage(fmdev, FM_RDS_FINISH_IDX);
}
static void fm_irq_handle_rds_finish(struct fmdev *fmdev)
{
fm_irq_call_stage(fmdev, FM_HW_TUNE_OP_ENDED_IDX);
}
static void fm_irq_handle_tune_op_ended(struct fmdev *fmdev)
{
if (fmdev->irq_info.flag & (FM_FR_EVENT | FM_BL_EVENT) & fmdev->
irq_info.mask) {
fmdbg("irq: tune ended/bandlimit reached\n");
if (test_and_clear_bit(FM_AF_SWITCH_INPROGRESS, &fmdev->flag)) {
fmdev->irq_info.stage = FM_AF_JUMP_RD_FREQ_IDX;
} else {
complete(&fmdev->maintask_comp);
fmdev->irq_info.stage = FM_HW_POWER_ENB_IDX;
}
} else
fmdev->irq_info.stage = FM_HW_POWER_ENB_IDX;
fm_irq_call(fmdev);
}
static void fm_irq_handle_power_enb(struct fmdev *fmdev)
{
if (fmdev->irq_info.flag & FM_POW_ENB_EVENT) {
fmdbg("irq: Power Enabled/Disabled\n");
complete(&fmdev->maintask_comp);
}
fm_irq_call_stage(fmdev, FM_LOW_RSSI_START_IDX);
}
static void fm_irq_handle_low_rssi_start(struct fmdev *fmdev)
{
if ((fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON) &&
(fmdev->irq_info.flag & FM_LEV_EVENT & fmdev->irq_info.mask) &&
(fmdev->rx.freq != FM_UNDEFINED_FREQ) &&
(fmdev->rx.stat_info.afcache_size != 0)) {
fmdbg("irq: rssi level has fallen below threshold level\n");
/* Disable further low RSSI interrupts */
fmdev->irq_info.mask &= ~FM_LEV_EVENT;
fmdev->rx.afjump_idx = 0;
fmdev->rx.freq_before_jump = fmdev->rx.freq;
fmdev->irq_info.stage = FM_AF_JUMP_SETPI_IDX;
} else {
/* Continue next function in interrupt handler table */
fmdev->irq_info.stage = FM_SEND_INTMSK_CMD_IDX;
}
fm_irq_call(fmdev);
}
static void fm_irq_afjump_set_pi(struct fmdev *fmdev)
{
u16 payload;
/* Set PI code - must be updated if the AF list is not empty */
payload = fmdev->rx.stat_info.picode;
if (!fm_send_cmd(fmdev, RDS_PI_SET, REG_WR, &payload, sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SETPI_RESP_IDX);
}
static void fm_irq_handle_set_pi_resp(struct fmdev *fmdev)
{
fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_SETPI_MASK_IDX);
}
/*
* Set PI mask.
* 0xFFFF = Enable PI code matching
* 0x0000 = Disable PI code matching
*/
static void fm_irq_afjump_set_pimask(struct fmdev *fmdev)
{
u16 payload;
payload = 0x0000;
if (!fm_send_cmd(fmdev, RDS_PI_MASK_SET, REG_WR, &payload, sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SETPI_MASK_RESP_IDX);
}
static void fm_irq_handle_set_pimask_resp(struct fmdev *fmdev)
{
fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_SET_AF_FREQ_IDX);
}
static void fm_irq_afjump_setfreq(struct fmdev *fmdev)
{
u16 frq_index;
u16 payload;
fmdbg("Switch to %d KHz\n", fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx]);
frq_index = (fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx] -
fmdev->rx.region.bot_freq) / FM_FREQ_MUL;
payload = frq_index;
if (!fm_send_cmd(fmdev, AF_FREQ_SET, REG_WR, &payload, sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_SET_AFFREQ_RESP_IDX);
}
static void fm_irq_handle_setfreq_resp(struct fmdev *fmdev)
{
fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_ENABLE_INT_IDX);
}
static void fm_irq_afjump_enableint(struct fmdev *fmdev)
{
u16 payload;
/* Enable FR (tuning operation ended) interrupt */
payload = FM_FR_EVENT;
if (!fm_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload, sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_AF_JUMP_ENABLE_INT_RESP_IDX);
}
static void fm_irq_afjump_enableint_resp(struct fmdev *fmdev)
{
fm_irq_common_cmd_resp_helper(fmdev, FM_AF_JUMP_START_AFJUMP_IDX);
}
static void fm_irq_start_afjump(struct fmdev *fmdev)
{
u16 payload;
payload = FM_TUNER_AF_JUMP_MODE;
if (!fm_send_cmd(fmdev, TUNER_MODE_SET, REG_WR, &payload,
sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_AF_JUMP_HANDLE_START_AFJUMP_RESP_IDX);
}
static void fm_irq_handle_start_afjump_resp(struct fmdev *fmdev)
{
struct sk_buff *skb;
if (check_cmdresp_status(fmdev, &skb))
return;
fmdev->irq_info.stage = FM_SEND_FLAG_GETCMD_IDX;
set_bit(FM_AF_SWITCH_INPROGRESS, &fmdev->flag);
clear_bit(FM_INTTASK_RUNNING, &fmdev->flag);
}
static void fm_irq_afjump_rd_freq(struct fmdev *fmdev)
{
u16 payload;
if (!fm_send_cmd(fmdev, FREQ_SET, REG_RD, NULL, sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_AF_JUMP_RD_FREQ_RESP_IDX);
}
static void fm_irq_afjump_rd_freq_resp(struct fmdev *fmdev)
{
struct sk_buff *skb;
u16 read_freq;
u32 curr_freq, jumped_freq;
if (check_cmdresp_status(fmdev, &skb))
return;
/* Skip header info and copy only response data */
skb_pull(skb, sizeof(struct fm_event_msg_hdr));
memcpy(&read_freq, skb->data, sizeof(read_freq));
read_freq = be16_to_cpu((__force __be16)read_freq);
curr_freq = fmdev->rx.region.bot_freq + ((u32)read_freq * FM_FREQ_MUL);
jumped_freq = fmdev->rx.stat_info.af_cache[fmdev->rx.afjump_idx];
/* If the frequency was changed the jump succeeded */
if ((curr_freq != fmdev->rx.freq_before_jump) && (curr_freq == jumped_freq)) {
fmdbg("Successfully switched to alternate freq %d\n", curr_freq);
fmdev->rx.freq = curr_freq;
fm_rx_reset_rds_cache(fmdev);
/* AF feature is on, enable low level RSSI interrupt */
if (fmdev->rx.af_mode == FM_RX_RDS_AF_SWITCH_MODE_ON)
fmdev->irq_info.mask |= FM_LEV_EVENT;
fmdev->irq_info.stage = FM_LOW_RSSI_FINISH_IDX;
} else { /* jump to the next freq in the AF list */
fmdev->rx.afjump_idx++;
/* If we reached the end of the list - stop searching */
if (fmdev->rx.afjump_idx >= fmdev->rx.stat_info.afcache_size) {
fmdbg("AF switch processing failed\n");
fmdev->irq_info.stage = FM_LOW_RSSI_FINISH_IDX;
} else { /* AF List is not over - try next one */
fmdbg("Trying next freq in AF cache\n");
fmdev->irq_info.stage = FM_AF_JUMP_SETPI_IDX;
}
}
fm_irq_call(fmdev);
}
static void fm_irq_handle_low_rssi_finish(struct fmdev *fmdev)
{
fm_irq_call_stage(fmdev, FM_SEND_INTMSK_CMD_IDX);
}
static void fm_irq_send_intmsk_cmd(struct fmdev *fmdev)
{
u16 payload;
/* Re-enable FM interrupts */
payload = fmdev->irq_info.mask;
if (!fm_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
sizeof(payload), NULL))
fm_irq_timeout_stage(fmdev, FM_HANDLE_INTMSK_CMD_RESP_IDX);
}
static void fm_irq_handle_intmsk_cmd_resp(struct fmdev *fmdev)
{
struct sk_buff *skb;
if (check_cmdresp_status(fmdev, &skb))
return;
/*
* This is last function in interrupt table to be executed.
* So, reset stage index to 0.
*/
fmdev->irq_info.stage = FM_SEND_FLAG_GETCMD_IDX;
/* Start processing any pending interrupt */
if (test_and_clear_bit(FM_INTTASK_SCHEDULE_PENDING, &fmdev->flag))
fmdev->irq_info.handlers[fmdev->irq_info.stage](fmdev);
else
clear_bit(FM_INTTASK_RUNNING, &fmdev->flag);
}
/* Returns availability of RDS data in internal buffer */
int fmc_is_rds_data_available(struct fmdev *fmdev, struct file *file,
struct poll_table_struct *pts)
{
poll_wait(file, &fmdev->rx.rds.read_queue, pts);
if (fmdev->rx.rds.rd_idx != fmdev->rx.rds.wr_idx)
return 0;
return -EAGAIN;
}
/* Copies RDS data from internal buffer to user buffer */
int fmc_transfer_rds_from_internal_buff(struct fmdev *fmdev, struct file *file,
u8 __user *buf, size_t count)
{
u32 block_count;
u8 tmpbuf[FM_RDS_BLK_SIZE];
unsigned long flags;
int ret;
if (fmdev->rx.rds.wr_idx == fmdev->rx.rds.rd_idx) {
if (file->f_flags & O_NONBLOCK)
return -EWOULDBLOCK;
ret = wait_event_interruptible(fmdev->rx.rds.read_queue,
(fmdev->rx.rds.wr_idx != fmdev->rx.rds.rd_idx));
if (ret)
return -EINTR;
}
/* Calculate block count from byte count */
count /= FM_RDS_BLK_SIZE;
block_count = 0;
ret = 0;
while (block_count < count) {
spin_lock_irqsave(&fmdev->rds_buff_lock, flags);
if (fmdev->rx.rds.wr_idx == fmdev->rx.rds.rd_idx) {
spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
break;
}
memcpy(tmpbuf, &fmdev->rx.rds.buff[fmdev->rx.rds.rd_idx],
FM_RDS_BLK_SIZE);
fmdev->rx.rds.rd_idx += FM_RDS_BLK_SIZE;
if (fmdev->rx.rds.rd_idx >= fmdev->rx.rds.buf_size)
fmdev->rx.rds.rd_idx = 0;
spin_unlock_irqrestore(&fmdev->rds_buff_lock, flags);
if (copy_to_user(buf, tmpbuf, FM_RDS_BLK_SIZE))
break;
block_count++;
buf += FM_RDS_BLK_SIZE;
ret += FM_RDS_BLK_SIZE;
}
return ret;
}
int fmc_set_freq(struct fmdev *fmdev, u32 freq_to_set)
{
switch (fmdev->curr_fmmode) {
case FM_MODE_RX:
return fm_rx_set_freq(fmdev, freq_to_set);
case FM_MODE_TX:
return fm_tx_set_freq(fmdev, freq_to_set);
default:
return -EINVAL;
}
}
int fmc_get_freq(struct fmdev *fmdev, u32 *cur_tuned_frq)
{
if (fmdev->rx.freq == FM_UNDEFINED_FREQ) {
fmerr("RX frequency is not set\n");
return -EPERM;
}
if (cur_tuned_frq == NULL) {
fmerr("Invalid memory\n");
return -ENOMEM;
}
switch (fmdev->curr_fmmode) {
case FM_MODE_RX:
*cur_tuned_frq = fmdev->rx.freq;
return 0;
case FM_MODE_TX:
*cur_tuned_frq = 0; /* TODO : Change this later */
return 0;
default:
return -EINVAL;
}
}
int fmc_set_region(struct fmdev *fmdev, u8 region_to_set)
{
switch (fmdev->curr_fmmode) {
case FM_MODE_RX:
return fm_rx_set_region(fmdev, region_to_set);
case FM_MODE_TX:
return fm_tx_set_region(fmdev, region_to_set);
default:
return -EINVAL;
}
}
int fmc_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
{
switch (fmdev->curr_fmmode) {
case FM_MODE_RX:
return fm_rx_set_mute_mode(fmdev, mute_mode_toset);
case FM_MODE_TX:
return fm_tx_set_mute_mode(fmdev, mute_mode_toset);
default:
return -EINVAL;
}
}
int fmc_set_stereo_mono(struct fmdev *fmdev, u16 mode)
{
switch (fmdev->curr_fmmode) {
case FM_MODE_RX:
return fm_rx_set_stereo_mono(fmdev, mode);
case FM_MODE_TX:
return fm_tx_set_stereo_mono(fmdev, mode);
default:
return -EINVAL;
}
}
int fmc_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
{
switch (fmdev->curr_fmmode) {
case FM_MODE_RX:
return fm_rx_set_rds_mode(fmdev, rds_en_dis);
case FM_MODE_TX:
return fm_tx_set_rds_mode(fmdev, rds_en_dis);
default:
return -EINVAL;
}
}
/* Sends power off command to the chip */
static int fm_power_down(struct fmdev *fmdev)
{
u16 payload;
int ret;
if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
fmerr("FM core is not ready\n");
return -EPERM;
}
if (fmdev->curr_fmmode == FM_MODE_OFF) {
fmdbg("FM chip is already in OFF state\n");
return 0;
}
payload = 0x0;
ret = fmc_send_cmd(fmdev, FM_POWER_MODE, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
return fmc_release(fmdev);
}
/* Reads init command from FM firmware file and loads to the chip */
static int fm_download_firmware(struct fmdev *fmdev, const u8 *fw_name)
{
const struct firmware *fw_entry;
struct bts_header *fw_header;
struct bts_action *action;
struct bts_action_delay *delay;
u8 *fw_data;
int ret, fw_len;
set_bit(FM_FW_DW_INPROGRESS, &fmdev->flag);
ret = request_firmware(&fw_entry, fw_name,
&fmdev->radio_dev->dev);
if (ret < 0) {
fmerr("Unable to read firmware(%s) content\n", fw_name);
return ret;
}
fmdbg("Firmware(%s) length : %zu bytes\n", fw_name, fw_entry->size);
fw_data = (void *)fw_entry->data;
fw_len = fw_entry->size;
fw_header = (struct bts_header *)fw_data;
if (fw_header->magic != FM_FW_FILE_HEADER_MAGIC) {
fmerr("%s not a legal TI firmware file\n", fw_name);
ret = -EINVAL;
goto rel_fw;
}
fmdbg("FW(%s) magic number : 0x%x\n", fw_name, fw_header->magic);
/* Skip file header info , we already verified it */
fw_data += sizeof(struct bts_header);
fw_len -= sizeof(struct bts_header);
while (fw_data && fw_len > 0) {
action = (struct bts_action *)fw_data;
switch (action->type) {
case ACTION_SEND_COMMAND: /* Send */
ret = fmc_send_cmd(fmdev, 0, 0, action->data,
action->size, NULL, NULL);
if (ret)
goto rel_fw;
break;
case ACTION_DELAY: /* Delay */
delay = (struct bts_action_delay *)action->data;
mdelay(delay->msec);
break;
}
fw_data += (sizeof(struct bts_action) + (action->size));
fw_len -= (sizeof(struct bts_action) + (action->size));
}
fmdbg("Transferred only %d of %d bytes of the firmware to chip\n",
fw_entry->size - fw_len, fw_entry->size);
rel_fw:
release_firmware(fw_entry);
clear_bit(FM_FW_DW_INPROGRESS, &fmdev->flag);
return ret;
}
/* Loads default RX configuration to the chip */
static int load_default_rx_configuration(struct fmdev *fmdev)
{
int ret;
ret = fm_rx_set_volume(fmdev, FM_DEFAULT_RX_VOLUME);
if (ret < 0)
return ret;
return fm_rx_set_rssi_threshold(fmdev, FM_DEFAULT_RSSI_THRESHOLD);
}
/* Does FM power on sequence */
static int fm_power_up(struct fmdev *fmdev, u8 mode)
{
u16 payload;
__be16 asic_id = 0, asic_ver = 0;
int resp_len, ret;
u8 fw_name[50];
if (mode >= FM_MODE_ENTRY_MAX) {
fmerr("Invalid firmware download option\n");
return -EINVAL;
}
/*
* Initialize FM common module. FM GPIO toggling is
* taken care in Shared Transport driver.
*/
ret = fmc_prepare(fmdev);
if (ret < 0) {
fmerr("Unable to prepare FM Common\n");
return ret;
}
payload = FM_ENABLE;
if (fmc_send_cmd(fmdev, FM_POWER_MODE, REG_WR, &payload,
sizeof(payload), NULL, NULL))
goto rel;
/* Allow the chip to settle down in Channel-8 mode */
msleep(20);
if (fmc_send_cmd(fmdev, ASIC_ID_GET, REG_RD, NULL,
sizeof(asic_id), &asic_id, &resp_len))
goto rel;
if (fmc_send_cmd(fmdev, ASIC_VER_GET, REG_RD, NULL,
sizeof(asic_ver), &asic_ver, &resp_len))
goto rel;
fmdbg("ASIC ID: 0x%x , ASIC Version: %d\n",
be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
sprintf(fw_name, "%s_%x.%d.bts", FM_FMC_FW_FILE_START,
be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
ret = fm_download_firmware(fmdev, fw_name);
if (ret < 0) {
fmdbg("Failed to download firmware file %s\n", fw_name);
goto rel;
}
sprintf(fw_name, "%s_%x.%d.bts", (mode == FM_MODE_RX) ?
FM_RX_FW_FILE_START : FM_TX_FW_FILE_START,
be16_to_cpu(asic_id), be16_to_cpu(asic_ver));
ret = fm_download_firmware(fmdev, fw_name);
if (ret < 0) {
fmdbg("Failed to download firmware file %s\n", fw_name);
goto rel;
} else
return ret;
rel:
return fmc_release(fmdev);
}
/* Set FM Modes(TX, RX, OFF) */
int fmc_set_mode(struct fmdev *fmdev, u8 fm_mode)
{
int ret = 0;
if (fm_mode >= FM_MODE_ENTRY_MAX) {
fmerr("Invalid FM mode\n");
return -EINVAL;
}
if (fmdev->curr_fmmode == fm_mode) {
fmdbg("Already fm is in mode(%d)\n", fm_mode);
return ret;
}
switch (fm_mode) {
case FM_MODE_OFF: /* OFF Mode */
ret = fm_power_down(fmdev);
if (ret < 0) {
fmerr("Failed to set OFF mode\n");
return ret;
}
break;
case FM_MODE_TX: /* TX Mode */
case FM_MODE_RX: /* RX Mode */
/* Power down before switching to TX or RX mode */
if (fmdev->curr_fmmode != FM_MODE_OFF) {
ret = fm_power_down(fmdev);
if (ret < 0) {
fmerr("Failed to set OFF mode\n");
return ret;
}
msleep(30);
}
ret = fm_power_up(fmdev, fm_mode);
if (ret < 0) {
fmerr("Failed to load firmware\n");
return ret;
}
}
fmdev->curr_fmmode = fm_mode;
/* Set default configuration */
if (fmdev->curr_fmmode == FM_MODE_RX) {
fmdbg("Loading default rx configuration..\n");
ret = load_default_rx_configuration(fmdev);
if (ret < 0)
fmerr("Failed to load default values\n");
}
return ret;
}
/* Returns current FM mode (TX, RX, OFF) */
int fmc_get_mode(struct fmdev *fmdev, u8 *fmmode)
{
if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
fmerr("FM core is not ready\n");
return -EPERM;
}
if (fmmode == NULL) {
fmerr("Invalid memory\n");
return -ENOMEM;
}
*fmmode = fmdev->curr_fmmode;
return 0;
}
/* Called by ST layer when FM packet is available */
static long fm_st_receive(void *arg, struct sk_buff *skb)
{
struct fmdev *fmdev;
fmdev = arg;
if (skb == NULL) {
fmerr("Invalid SKB received from ST\n");
return -EFAULT;
}
if (skb->cb[0] != FM_PKT_LOGICAL_CHAN_NUMBER) {
fmerr("Received SKB (%p) is not FM Channel 8 pkt\n", skb);
return -EINVAL;
}
memcpy(skb_push(skb, 1), &skb->cb[0], 1);
skb_queue_tail(&fmdev->rx_q, skb);
tasklet_schedule(&fmdev->rx_task);
return 0;
}
/*
* Called by ST layer to indicate protocol registration completion
* status.
*/
static void fm_st_reg_comp_cb(void *arg, int data)
{
struct fmdev *fmdev;
fmdev = (struct fmdev *)arg;
fmdev->streg_cbdata = data;
complete(&wait_for_fmdrv_reg_comp);
}
/*
* This function will be called from FM V4L2 open function.
* Register with ST driver and initialize driver data.
*/
int fmc_prepare(struct fmdev *fmdev)
{
static struct st_proto_s fm_st_proto;
int ret;
if (test_bit(FM_CORE_READY, &fmdev->flag)) {
fmdbg("FM Core is already up\n");
return 0;
}
memset(&fm_st_proto, 0, sizeof(fm_st_proto));
fm_st_proto.recv = fm_st_receive;
fm_st_proto.match_packet = NULL;
fm_st_proto.reg_complete_cb = fm_st_reg_comp_cb;
fm_st_proto.write = NULL; /* TI ST driver will fill write pointer */
fm_st_proto.priv_data = fmdev;
fm_st_proto.chnl_id = 0x08;
fm_st_proto.max_frame_size = 0xff;
fm_st_proto.hdr_len = 1;
fm_st_proto.offset_len_in_hdr = 0;
fm_st_proto.len_size = 1;
fm_st_proto.reserve = 1;
ret = st_register(&fm_st_proto);
if (ret == -EINPROGRESS) {
init_completion(&wait_for_fmdrv_reg_comp);
fmdev->streg_cbdata = -EINPROGRESS;
fmdbg("%s waiting for ST reg completion signal\n", __func__);
if (!wait_for_completion_timeout(&wait_for_fmdrv_reg_comp,
FM_ST_REG_TIMEOUT)) {
fmerr("Timeout(%d sec), didn't get reg completion signal from ST\n",
jiffies_to_msecs(FM_ST_REG_TIMEOUT) / 1000);
return -ETIMEDOUT;
}
if (fmdev->streg_cbdata != 0) {
fmerr("ST reg comp CB called with error status %d\n",
fmdev->streg_cbdata);
return -EAGAIN;
}
ret = 0;
} else if (ret < 0) {
fmerr("st_register failed %d\n", ret);
return -EAGAIN;
}
if (fm_st_proto.write != NULL) {
g_st_write = fm_st_proto.write;
} else {
fmerr("Failed to get ST write func pointer\n");
ret = st_unregister(&fm_st_proto);
if (ret < 0)
fmerr("st_unregister failed %d\n", ret);
return -EAGAIN;
}
spin_lock_init(&fmdev->rds_buff_lock);
spin_lock_init(&fmdev->resp_skb_lock);
/* Initialize TX queue and TX tasklet */
skb_queue_head_init(&fmdev->tx_q);
tasklet_setup(&fmdev->tx_task, send_tasklet);
/* Initialize RX Queue and RX tasklet */
skb_queue_head_init(&fmdev->rx_q);
tasklet_setup(&fmdev->rx_task, recv_tasklet);
fmdev->irq_info.stage = 0;
atomic_set(&fmdev->tx_cnt, 1);
fmdev->resp_comp = NULL;
timer_setup(&fmdev->irq_info.timer, int_timeout_handler, 0);
/*TODO: add FM_STIC_EVENT later */
fmdev->irq_info.mask = FM_MAL_EVENT;
/* Region info */
fmdev->rx.region = region_configs[default_radio_region];
fmdev->rx.mute_mode = FM_MUTE_OFF;
fmdev->rx.rf_depend_mute = FM_RX_RF_DEPENDENT_MUTE_OFF;
fmdev->rx.rds.flag = FM_RDS_DISABLE;
fmdev->rx.freq = FM_UNDEFINED_FREQ;
fmdev->rx.rds_mode = FM_RDS_SYSTEM_RDS;
fmdev->rx.af_mode = FM_RX_RDS_AF_SWITCH_MODE_OFF;
fmdev->irq_info.retry = 0;
fm_rx_reset_rds_cache(fmdev);
init_waitqueue_head(&fmdev->rx.rds.read_queue);
fm_rx_reset_station_info(fmdev);
set_bit(FM_CORE_READY, &fmdev->flag);
return ret;
}
/*
* This function will be called from FM V4L2 release function.
* Unregister from ST driver.
*/
int fmc_release(struct fmdev *fmdev)
{
static struct st_proto_s fm_st_proto;
int ret;
if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
fmdbg("FM Core is already down\n");
return 0;
}
/* Service pending read */
wake_up_interruptible(&fmdev->rx.rds.read_queue);
tasklet_kill(&fmdev->tx_task);
tasklet_kill(&fmdev->rx_task);
skb_queue_purge(&fmdev->tx_q);
skb_queue_purge(&fmdev->rx_q);
fmdev->resp_comp = NULL;
fmdev->rx.freq = 0;
memset(&fm_st_proto, 0, sizeof(fm_st_proto));
fm_st_proto.chnl_id = 0x08;
ret = st_unregister(&fm_st_proto);
if (ret < 0)
fmerr("Failed to de-register FM from ST %d\n", ret);
else
fmdbg("Successfully unregistered from ST\n");
clear_bit(FM_CORE_READY, &fmdev->flag);
return ret;
}
/*
* Module init function. Ask FM V4L module to register video device.
* Allocate memory for FM driver context and RX RDS buffer.
*/
static int __init fm_drv_init(void)
{
struct fmdev *fmdev = NULL;
int ret = -ENOMEM;
fmdbg("FM driver version %s\n", FM_DRV_VERSION);
fmdev = kzalloc(sizeof(struct fmdev), GFP_KERNEL);
if (NULL == fmdev) {
fmerr("Can't allocate operation structure memory\n");
return ret;
}
fmdev->rx.rds.buf_size = default_rds_buf * FM_RDS_BLK_SIZE;
fmdev->rx.rds.buff = kzalloc(fmdev->rx.rds.buf_size, GFP_KERNEL);
if (NULL == fmdev->rx.rds.buff) {
fmerr("Can't allocate rds ring buffer\n");
goto rel_dev;
}
ret = fm_v4l2_init_video_device(fmdev, radio_nr);
if (ret < 0)
goto rel_rdsbuf;
fmdev->irq_info.handlers = int_handler_table;
fmdev->curr_fmmode = FM_MODE_OFF;
fmdev->tx_data.pwr_lvl = FM_PWR_LVL_DEF;
fmdev->tx_data.preemph = FM_TX_PREEMPH_50US;
return ret;
rel_rdsbuf:
kfree(fmdev->rx.rds.buff);
rel_dev:
kfree(fmdev);
return ret;
}
/* Module exit function. Ask FM V4L module to unregister video device */
static void __exit fm_drv_exit(void)
{
struct fmdev *fmdev = NULL;
fmdev = fm_v4l2_deinit_video_device();
if (fmdev != NULL) {
kfree(fmdev->rx.rds.buff);
kfree(fmdev);
}
}
module_init(fm_drv_init);
module_exit(fm_drv_exit);
/* ------------- Module Info ------------- */
MODULE_AUTHOR("Manjunatha Halli <[email protected]>");
MODULE_DESCRIPTION("FM Driver for TI's Connectivity chip. " FM_DRV_VERSION);
MODULE_VERSION(FM_DRV_VERSION);
MODULE_LICENSE("GPL");
| linux-master | drivers/media/radio/wl128x/fmdrv_common.c |
// SPDX-License-Identifier: GPL-2.0-only
/*
* FM Driver for Connectivity chip of Texas Instruments.
* This sub-module of FM driver implements FM TX functionality.
*
* Copyright (C) 2011 Texas Instruments
*/
#include <linux/delay.h>
#include "fmdrv.h"
#include "fmdrv_common.h"
#include "fmdrv_tx.h"
int fm_tx_set_stereo_mono(struct fmdev *fmdev, u16 mode)
{
u16 payload;
int ret;
if (fmdev->tx_data.aud_mode == mode)
return 0;
fmdbg("stereo mode: %d\n", mode);
/* Set Stereo/Mono mode */
payload = (1 - mode);
ret = fmc_send_cmd(fmdev, MONO_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
fmdev->tx_data.aud_mode = mode;
return ret;
}
static int set_rds_text(struct fmdev *fmdev, u8 *rds_text)
{
u16 payload;
int ret;
ret = fmc_send_cmd(fmdev, RDS_DATA_SET, REG_WR, rds_text,
strlen(rds_text), NULL, NULL);
if (ret < 0)
return ret;
/* Scroll mode */
payload = (u16)0x1;
ret = fmc_send_cmd(fmdev, DISPLAY_MODE, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
return 0;
}
static int set_rds_data_mode(struct fmdev *fmdev, u8 mode)
{
u16 payload;
int ret;
/* Setting unique PI TODO: how unique? */
payload = (u16)0xcafe;
ret = fmc_send_cmd(fmdev, PI_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* Set decoder id */
payload = (u16)0xa;
ret = fmc_send_cmd(fmdev, DI_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* TODO: RDS_MODE_GET? */
return 0;
}
static int set_rds_len(struct fmdev *fmdev, u8 type, u16 len)
{
u16 payload;
int ret;
len |= type << 8;
payload = len;
ret = fmc_send_cmd(fmdev, RDS_CONFIG_DATA_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* TODO: LENGTH_GET? */
return 0;
}
int fm_tx_set_rds_mode(struct fmdev *fmdev, u8 rds_en_dis)
{
u16 payload;
int ret;
u8 rds_text[] = "Zoom2\n";
fmdbg("rds_en_dis:%d(E:%d, D:%d)\n", rds_en_dis,
FM_RDS_ENABLE, FM_RDS_DISABLE);
if (rds_en_dis == FM_RDS_ENABLE) {
/* Set RDS length */
set_rds_len(fmdev, 0, strlen(rds_text));
/* Set RDS text */
set_rds_text(fmdev, rds_text);
/* Set RDS mode */
set_rds_data_mode(fmdev, 0x0);
}
/* Send command to enable RDS */
if (rds_en_dis == FM_RDS_ENABLE)
payload = 0x01;
else
payload = 0x00;
ret = fmc_send_cmd(fmdev, RDS_DATA_ENB, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
if (rds_en_dis == FM_RDS_ENABLE) {
/* Set RDS length */
set_rds_len(fmdev, 0, strlen(rds_text));
/* Set RDS text */
set_rds_text(fmdev, rds_text);
}
fmdev->tx_data.rds.flag = rds_en_dis;
return 0;
}
int fm_tx_set_radio_text(struct fmdev *fmdev, u8 *rds_text, u8 rds_type)
{
u16 payload;
int ret;
if (fmdev->curr_fmmode != FM_MODE_TX)
return -EPERM;
fm_tx_set_rds_mode(fmdev, 0);
/* Set RDS length */
set_rds_len(fmdev, rds_type, strlen(rds_text));
/* Set RDS text */
set_rds_text(fmdev, rds_text);
/* Set RDS mode */
set_rds_data_mode(fmdev, 0x0);
payload = 1;
ret = fmc_send_cmd(fmdev, RDS_DATA_ENB, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
return 0;
}
int fm_tx_set_af(struct fmdev *fmdev, u32 af)
{
u16 payload;
int ret;
if (fmdev->curr_fmmode != FM_MODE_TX)
return -EPERM;
fmdbg("AF: %d\n", af);
af = (af - 87500) / 100;
payload = (u16)af;
ret = fmc_send_cmd(fmdev, TA_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
return 0;
}
int fm_tx_set_region(struct fmdev *fmdev, u8 region)
{
u16 payload;
int ret;
if (region != FM_BAND_EUROPE_US && region != FM_BAND_JAPAN) {
fmerr("Invalid band\n");
return -EINVAL;
}
/* Send command to set the band */
payload = (u16)region;
ret = fmc_send_cmd(fmdev, TX_BAND_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
return 0;
}
int fm_tx_set_mute_mode(struct fmdev *fmdev, u8 mute_mode_toset)
{
u16 payload;
int ret;
fmdbg("tx: mute mode %d\n", mute_mode_toset);
payload = mute_mode_toset;
ret = fmc_send_cmd(fmdev, MUTE, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
return 0;
}
/* Set TX Audio I/O */
static int set_audio_io(struct fmdev *fmdev)
{
struct fmtx_data *tx = &fmdev->tx_data;
u16 payload;
int ret;
/* Set Audio I/O Enable */
payload = tx->audio_io;
ret = fmc_send_cmd(fmdev, AUDIO_IO_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* TODO: is audio set? */
return 0;
}
/* Start TX Transmission */
static int enable_xmit(struct fmdev *fmdev, u8 new_xmit_state)
{
struct fmtx_data *tx = &fmdev->tx_data;
unsigned long timeleft;
u16 payload;
int ret;
/* Enable POWER_ENB interrupts */
payload = FM_POW_ENB_EVENT;
ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* Set Power Enable */
payload = new_xmit_state;
ret = fmc_send_cmd(fmdev, POWER_ENB_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* Wait for Power Enabled */
init_completion(&fmdev->maintask_comp);
timeleft = wait_for_completion_timeout(&fmdev->maintask_comp,
FM_DRV_TX_TIMEOUT);
if (!timeleft) {
fmerr("Timeout(%d sec),didn't get tune ended interrupt\n",
jiffies_to_msecs(FM_DRV_TX_TIMEOUT) / 1000);
return -ETIMEDOUT;
}
set_bit(FM_CORE_TX_XMITING, &fmdev->flag);
tx->xmit_state = new_xmit_state;
return 0;
}
/* Set TX power level */
int fm_tx_set_pwr_lvl(struct fmdev *fmdev, u8 new_pwr_lvl)
{
u16 payload;
struct fmtx_data *tx = &fmdev->tx_data;
int ret;
if (fmdev->curr_fmmode != FM_MODE_TX)
return -EPERM;
fmdbg("tx: pwr_level_to_set %ld\n", (long int)new_pwr_lvl);
/* If the core isn't ready update global variable */
if (!test_bit(FM_CORE_READY, &fmdev->flag)) {
tx->pwr_lvl = new_pwr_lvl;
return 0;
}
/* Set power level: Application will specify power level value in
* units of dB/uV, whereas range and step are specific to FM chip.
* For TI's WL chips, convert application specified power level value
* to chip specific value by subtracting 122 from it. Refer to TI FM
* data sheet for details.
* */
payload = (FM_PWR_LVL_HIGH - new_pwr_lvl);
ret = fmc_send_cmd(fmdev, POWER_LEV_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
/* TODO: is the power level set? */
tx->pwr_lvl = new_pwr_lvl;
return 0;
}
/*
* Sets FM TX pre-emphasis filter value (OFF, 50us, or 75us)
* Convert V4L2 specified filter values to chip specific filter values.
*/
int fm_tx_set_preemph_filter(struct fmdev *fmdev, u32 preemphasis)
{
struct fmtx_data *tx = &fmdev->tx_data;
u16 payload;
int ret;
if (fmdev->curr_fmmode != FM_MODE_TX)
return -EPERM;
switch (preemphasis) {
case V4L2_PREEMPHASIS_DISABLED:
payload = FM_TX_PREEMPH_OFF;
break;
case V4L2_PREEMPHASIS_50_uS:
payload = FM_TX_PREEMPH_50US;
break;
case V4L2_PREEMPHASIS_75_uS:
payload = FM_TX_PREEMPH_75US;
break;
}
ret = fmc_send_cmd(fmdev, PREMPH_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
tx->preemph = payload;
return ret;
}
/* Get the TX tuning capacitor value.*/
int fm_tx_get_tune_cap_val(struct fmdev *fmdev)
{
u16 curr_val;
u32 resp_len;
int ret;
if (fmdev->curr_fmmode != FM_MODE_TX)
return -EPERM;
ret = fmc_send_cmd(fmdev, READ_FMANT_TUNE_VALUE, REG_RD,
NULL, sizeof(curr_val), &curr_val, &resp_len);
if (ret < 0)
return ret;
curr_val = be16_to_cpu((__force __be16)curr_val);
return curr_val;
}
/* Set TX Frequency */
int fm_tx_set_freq(struct fmdev *fmdev, u32 freq_to_set)
{
struct fmtx_data *tx = &fmdev->tx_data;
u16 payload, chanl_index;
int ret;
if (test_bit(FM_CORE_TX_XMITING, &fmdev->flag)) {
enable_xmit(fmdev, 0);
clear_bit(FM_CORE_TX_XMITING, &fmdev->flag);
}
/* Enable FR, BL interrupts */
payload = (FM_FR_EVENT | FM_BL_EVENT);
ret = fmc_send_cmd(fmdev, INT_MASK_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
tx->tx_frq = (unsigned long)freq_to_set;
fmdbg("tx: freq_to_set %ld\n", (long int)tx->tx_frq);
chanl_index = freq_to_set / 10;
/* Set current tuner channel */
payload = chanl_index;
ret = fmc_send_cmd(fmdev, CHANL_SET, REG_WR, &payload,
sizeof(payload), NULL, NULL);
if (ret < 0)
return ret;
fm_tx_set_pwr_lvl(fmdev, tx->pwr_lvl);
fm_tx_set_preemph_filter(fmdev, tx->preemph);
tx->audio_io = 0x01; /* I2S */
set_audio_io(fmdev);
enable_xmit(fmdev, 0x01); /* Enable transmission */
tx->aud_mode = FM_STEREO_MODE;
tx->rds.flag = FM_RDS_DISABLE;
return 0;
}
| linux-master | drivers/media/radio/wl128x/fmdrv_tx.c |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.