Commit f5a98f37 authored by Akihiro Tsukada's avatar Akihiro Tsukada Committed by Mauro Carvalho Chehab

[media] pt3: add support for Earthsoft PT3 ISDB-S/T receiver card

This patch adds support for PT3 PCIe cards.
PT3 has an FPGA PCIe bridge chip, a TC90522 demod chip and
a VA4M6JC2103 tuner module which contains two QM1D1C0042 chips for ISDB-S
and two MxL301RF's for ISDB-T.
It can receive and deliver 4 (2x ISDB-S, 2x ISDB-T) streams simultaneously,
and a kthread is used per stream to poll incoming data,
because PT3 does not have interrupts.

As an antenna input for each delivery system is split in the tuner module
and shared between the corresponding two tuner chips,
LNB/LNA controls that the FPGA chip provides are (naturally) shared as well.
The tuner chips also share the power line in the tuner module,
which is controlled on/off by a GPIO pin of the demod chip.

As with the demod chip and the ISDB-T tuner chip,
the init sequences/register settings for those chips are not disclosed
and stored in a private memory of the FPGA,
PT3 driver executes the init of those chips on behalf of their drivers.
Signed-off-by: default avatarAkihiro Tsukada <tskd08@gmail.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent f5d82a75
......@@ -42,6 +42,7 @@ source "drivers/media/pci/b2c2/Kconfig"
source "drivers/media/pci/pluto2/Kconfig"
source "drivers/media/pci/dm1105/Kconfig"
source "drivers/media/pci/pt1/Kconfig"
source "drivers/media/pci/pt3/Kconfig"
source "drivers/media/pci/mantis/Kconfig"
source "drivers/media/pci/ngene/Kconfig"
source "drivers/media/pci/ddbridge/Kconfig"
......
......@@ -7,6 +7,7 @@ obj-y += ttpci/ \
pluto2/ \
dm1105/ \
pt1/ \
pt3/ \
mantis/ \
ngene/ \
ddbridge/ \
......
config DVB_PT3
tristate "Earthsoft PT3 cards"
depends on DVB_CORE && PCI && I2C
select DVB_TC90522 if MEDIA_SUBDRV_AUTOSELECT
select MEDIA_TUNER_QM1D1C0042 if MEDIA_SUBDRV_AUTOSELECT
select MEDIA_TUNER_MXL301RF if MEDIA_SUBDRV_AUTOSELECT
help
Support for Earthsoft PT3 PCIe cards.
Say Y or M if you own such a device and want to use it.
earth-pt3-objs += pt3.o pt3_i2c.o pt3_dma.o
obj-$(CONFIG_DVB_PT3) += earth-pt3.o
ccflags-y += -Idrivers/media/dvb-core
ccflags-y += -Idrivers/media/dvb-frontends
ccflags-y += -Idrivers/media/tuners
This diff is collapsed.
/*
* Earthsoft PT3 driver
*
* Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
*
* 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 version 2.
*
*
* 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.
*/
#ifndef PT3_H
#define PT3_H
#include <linux/atomic.h>
#include <linux/types.h>
#include "dvb_demux.h"
#include "dvb_frontend.h"
#include "dmxdev.h"
#include "tc90522.h"
#include "mxl301rf.h"
#include "qm1d1c0042.h"
#define DRV_NAME KBUILD_MODNAME
#define PT3_NUM_FE 4
/*
* register index of the FPGA chip
*/
#define REG_VERSION 0x00
#define REG_BUS 0x04
#define REG_SYSTEM_W 0x08
#define REG_SYSTEM_R 0x0c
#define REG_I2C_W 0x10
#define REG_I2C_R 0x14
#define REG_RAM_W 0x18
#define REG_RAM_R 0x1c
#define REG_DMA_BASE 0x40 /* regs for FE[i] = REG_DMA_BASE + 0x18 * i */
#define OFST_DMA_DESC_L 0x00
#define OFST_DMA_DESC_H 0x04
#define OFST_DMA_CTL 0x08
#define OFST_TS_CTL 0x0c
#define OFST_STATUS 0x10
#define OFST_TS_ERR 0x14
/*
* internal buffer for I2C
*/
#define PT3_I2C_MAX 4091
struct pt3_i2cbuf {
u8 data[PT3_I2C_MAX];
u8 tmp;
u32 num_cmds;
};
/*
* DMA things
*/
#define TS_PACKET_SZ 188
/* DMA transfers must not cross 4GiB, so use one page / transfer */
#define DATA_XFER_SZ 4096
#define DATA_BUF_XFERS 47
/* (num_bufs * DATA_BUF_SZ) % TS_PACKET_SZ must be 0 */
#define DATA_BUF_SZ (DATA_BUF_XFERS * DATA_XFER_SZ)
#define MAX_DATA_BUFS 16
#define MIN_DATA_BUFS 2
#define DESCS_IN_PAGE (PAGE_SIZE / sizeof(struct xfer_desc))
#define MAX_NUM_XFERS (MAX_DATA_BUFS * DATA_BUF_XFERS)
#define MAX_DESC_BUFS DIV_ROUND_UP(MAX_NUM_XFERS, DESCS_IN_PAGE)
/* DMA transfer description.
* device is passed a pointer to this struct, dma-reads it,
* and gets the DMA buffer ring for storing TS data.
*/
struct xfer_desc {
u32 addr_l; /* bus address of target data buffer */
u32 addr_h;
u32 size;
u32 next_l; /* bus adddress of the next xfer_desc */
u32 next_h;
};
/* A DMA mapping of a page containing xfer_desc's */
struct xfer_desc_buffer {
dma_addr_t b_addr;
struct xfer_desc *descs; /* PAGE_SIZE (xfer_desc[DESCS_IN_PAGE]) */
};
/* A DMA mapping of a data buffer */
struct dma_data_buffer {
dma_addr_t b_addr;
u8 *data; /* size: u8[PAGE_SIZE] */
};
/*
* device things
*/
struct pt3_adap_config {
struct i2c_board_info demod_info;
struct tc90522_config demod_cfg;
struct i2c_board_info tuner_info;
union tuner_config {
struct qm1d1c0042_config qm1d1c0042;
struct mxl301rf_config mxl301rf;
} tuner_cfg;
u32 init_freq;
};
struct pt3_adapter {
struct dvb_adapter dvb_adap; /* dvb_adap.priv => struct pt3_board */
int adap_idx;
struct dvb_demux demux;
struct dmxdev dmxdev;
struct dvb_frontend *fe;
struct i2c_client *i2c_demod;
struct i2c_client *i2c_tuner;
/* data fetch thread */
struct task_struct *thread;
int num_feeds;
bool cur_lna;
bool cur_lnb; /* current LNB power status (on/off) */
/* items below are for DMA */
struct dma_data_buffer buffer[MAX_DATA_BUFS];
int buf_idx;
int buf_ofs;
int num_bufs; /* == pt3_board->num_bufs */
int num_discard; /* how many access units to discard initially */
struct xfer_desc_buffer desc_buf[MAX_DESC_BUFS];
int num_desc_bufs; /* == num_bufs * DATA_BUF_XFERS / DESCS_IN_PAGE */
};
struct pt3_board {
struct pci_dev *pdev;
void __iomem *regs[2];
/* regs[0]: registers, regs[1]: internal memory, used for I2C */
struct mutex lock;
/* LNB power shared among sat-FEs */
int lnb_on_cnt; /* LNB power on count */
/* LNA shared among terr-FEs */
int lna_on_cnt; /* booster enabled count */
int num_bufs; /* number of DMA buffers allocated/mapped per FE */
struct i2c_adapter i2c_adap;
struct pt3_i2cbuf *i2c_buf;
struct pt3_adapter *adaps[PT3_NUM_FE];
};
/*
* prototypes
*/
extern int pt3_alloc_dmabuf(struct pt3_adapter *adap);
extern void pt3_init_dmabuf(struct pt3_adapter *adap);
extern void pt3_free_dmabuf(struct pt3_adapter *adap);
extern int pt3_start_dma(struct pt3_adapter *adap);
extern int pt3_stop_dma(struct pt3_adapter *adap);
extern int pt3_proc_dma(struct pt3_adapter *adap);
extern int pt3_i2c_master_xfer(struct i2c_adapter *adap,
struct i2c_msg *msgs, int num);
extern u32 pt3_i2c_functionality(struct i2c_adapter *adap);
extern void pt3_i2c_reset(struct pt3_board *pt3);
extern int pt3_init_all_demods(struct pt3_board *pt3);
extern int pt3_init_all_mxl301rf(struct pt3_board *pt3);
#endif /* PT3_H */
/*
* Earthsoft PT3 driver
*
* Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
*
* 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 version 2.
*
*
* 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/dma-mapping.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include "pt3.h"
#define PT3_ACCESS_UNIT (TS_PACKET_SZ * 128)
#define PT3_BUF_CANARY (0x74)
static u32 get_dma_base(int idx)
{
int i;
i = (idx == 1 || idx == 2) ? 3 - idx : idx;
return REG_DMA_BASE + 0x18 * i;
}
int pt3_stop_dma(struct pt3_adapter *adap)
{
struct pt3_board *pt3 = adap->dvb_adap.priv;
u32 base;
u32 stat;
int retry;
base = get_dma_base(adap->adap_idx);
stat = ioread32(pt3->regs[0] + base + OFST_STATUS);
if (!(stat & 0x01))
return 0;
iowrite32(0x02, pt3->regs[0] + base + OFST_DMA_CTL);
for (retry = 0; retry < 5; retry++) {
stat = ioread32(pt3->regs[0] + base + OFST_STATUS);
if (!(stat & 0x01))
return 0;
msleep(50);
}
return -EIO;
}
int pt3_start_dma(struct pt3_adapter *adap)
{
struct pt3_board *pt3 = adap->dvb_adap.priv;
u32 base = get_dma_base(adap->adap_idx);
iowrite32(0x02, pt3->regs[0] + base + OFST_DMA_CTL);
iowrite32(lower_32_bits(adap->desc_buf[0].b_addr),
pt3->regs[0] + base + OFST_DMA_DESC_L);
iowrite32(upper_32_bits(adap->desc_buf[0].b_addr),
pt3->regs[0] + base + OFST_DMA_DESC_H);
iowrite32(0x01, pt3->regs[0] + base + OFST_DMA_CTL);
return 0;
}
static u8 *next_unit(struct pt3_adapter *adap, int *idx, int *ofs)
{
*ofs += PT3_ACCESS_UNIT;
if (*ofs >= DATA_BUF_SZ) {
*ofs -= DATA_BUF_SZ;
(*idx)++;
if (*idx == adap->num_bufs)
*idx = 0;
}
return &adap->buffer[*idx].data[*ofs];
}
int pt3_proc_dma(struct pt3_adapter *adap)
{
int idx, ofs;
idx = adap->buf_idx;
ofs = adap->buf_ofs;
if (adap->buffer[idx].data[ofs] == PT3_BUF_CANARY)
return 0;
while (*next_unit(adap, &idx, &ofs) != PT3_BUF_CANARY) {
u8 *p;
p = &adap->buffer[adap->buf_idx].data[adap->buf_ofs];
if (adap->num_discard > 0)
adap->num_discard--;
else if (adap->buf_ofs + PT3_ACCESS_UNIT > DATA_BUF_SZ) {
dvb_dmx_swfilter_packets(&adap->demux, p,
(DATA_BUF_SZ - adap->buf_ofs) / TS_PACKET_SZ);
dvb_dmx_swfilter_packets(&adap->demux,
adap->buffer[idx].data, ofs / TS_PACKET_SZ);
} else
dvb_dmx_swfilter_packets(&adap->demux, p,
PT3_ACCESS_UNIT / TS_PACKET_SZ);
*p = PT3_BUF_CANARY;
adap->buf_idx = idx;
adap->buf_ofs = ofs;
}
return 0;
}
void pt3_init_dmabuf(struct pt3_adapter *adap)
{
int idx, ofs;
u8 *p;
idx = 0;
ofs = 0;
p = adap->buffer[0].data;
/* mark the whole buffers as "not written yet" */
while (idx < adap->num_bufs) {
p[ofs] = PT3_BUF_CANARY;
ofs += PT3_ACCESS_UNIT;
if (ofs >= DATA_BUF_SZ) {
ofs -= DATA_BUF_SZ;
idx++;
p = adap->buffer[idx].data;
}
}
adap->buf_idx = 0;
adap->buf_ofs = 0;
}
void pt3_free_dmabuf(struct pt3_adapter *adap)
{
struct pt3_board *pt3;
int i;
pt3 = adap->dvb_adap.priv;
for (i = 0; i < adap->num_bufs; i++)
dma_free_coherent(&pt3->pdev->dev, DATA_BUF_SZ,
adap->buffer[i].data, adap->buffer[i].b_addr);
adap->num_bufs = 0;
for (i = 0; i < adap->num_desc_bufs; i++)
dma_free_coherent(&pt3->pdev->dev, PAGE_SIZE,
adap->desc_buf[i].descs, adap->desc_buf[i].b_addr);
adap->num_desc_bufs = 0;
}
int pt3_alloc_dmabuf(struct pt3_adapter *adap)
{
struct pt3_board *pt3;
void *p;
int i, j;
int idx, ofs;
int num_desc_bufs;
dma_addr_t data_addr, desc_addr;
struct xfer_desc *d;
pt3 = adap->dvb_adap.priv;
adap->num_bufs = 0;
adap->num_desc_bufs = 0;
for (i = 0; i < pt3->num_bufs; i++) {
p = dma_alloc_coherent(&pt3->pdev->dev, DATA_BUF_SZ,
&adap->buffer[i].b_addr, GFP_KERNEL);
if (p == NULL)
goto failed;
adap->buffer[i].data = p;
adap->num_bufs++;
}
pt3_init_dmabuf(adap);
/* build circular-linked pointers (xfer_desc) to the data buffers*/
idx = 0;
ofs = 0;
num_desc_bufs =
DIV_ROUND_UP(adap->num_bufs * DATA_BUF_XFERS, DESCS_IN_PAGE);
for (i = 0; i < num_desc_bufs; i++) {
p = dma_alloc_coherent(&pt3->pdev->dev, PAGE_SIZE,
&desc_addr, GFP_KERNEL);
if (p == NULL)
goto failed;
adap->num_desc_bufs++;
adap->desc_buf[i].descs = p;
adap->desc_buf[i].b_addr = desc_addr;
if (i > 0) {
d = &adap->desc_buf[i - 1].descs[DESCS_IN_PAGE - 1];
d->next_l = lower_32_bits(desc_addr);
d->next_h = upper_32_bits(desc_addr);
}
for (j = 0; j < DESCS_IN_PAGE; j++) {
data_addr = adap->buffer[idx].b_addr + ofs;
d = &adap->desc_buf[i].descs[j];
d->addr_l = lower_32_bits(data_addr);
d->addr_h = upper_32_bits(data_addr);
d->size = DATA_XFER_SZ;
desc_addr += sizeof(struct xfer_desc);
d->next_l = lower_32_bits(desc_addr);
d->next_h = upper_32_bits(desc_addr);
ofs += DATA_XFER_SZ;
if (ofs >= DATA_BUF_SZ) {
ofs -= DATA_BUF_SZ;
idx++;
if (idx >= adap->num_bufs) {
desc_addr = adap->desc_buf[0].b_addr;
d->next_l = lower_32_bits(desc_addr);
d->next_h = upper_32_bits(desc_addr);
return 0;
}
}
}
}
return 0;
failed:
pt3_free_dmabuf(adap);
return -ENOMEM;
}
/*
* Earthsoft PT3 driver
*
* Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
*
* 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 version 2.
*
*
* 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/delay.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/io.h>
#include <linux/pci.h>
#include "pt3.h"
#define PT3_I2C_BASE 2048
#define PT3_CMD_ADDR_NORMAL 0
#define PT3_CMD_ADDR_INIT_DEMOD 4096
#define PT3_CMD_ADDR_INIT_TUNER (4096 + 2042)
/* masks for I2C status register */
#define STAT_SEQ_RUNNING 0x1
#define STAT_SEQ_ERROR 0x6
#define STAT_NO_SEQ 0x8
#define PT3_I2C_RUN (1 << 16)
#define PT3_I2C_RESET (1 << 17)
enum ctl_cmd {
I_END,
I_ADDRESS,
I_CLOCK_L,
I_CLOCK_H,
I_DATA_L,
I_DATA_H,
I_RESET,
I_SLEEP,
I_DATA_L_NOP = 0x08,
I_DATA_H_NOP = 0x0c,
I_DATA_H_READ = 0x0d,
I_DATA_H_ACK0 = 0x0e,
I_DATA_H_ACK1 = 0x0f,
};
static void cmdbuf_add(struct pt3_i2cbuf *cbuf, enum ctl_cmd cmd)
{
int buf_idx;
if ((cbuf->num_cmds % 2) == 0)
cbuf->tmp = cmd;
else {
cbuf->tmp |= cmd << 4;
buf_idx = cbuf->num_cmds / 2;
if (buf_idx < ARRAY_SIZE(cbuf->data))
cbuf->data[buf_idx] = cbuf->tmp;
}
cbuf->num_cmds++;
}
static void put_end(struct pt3_i2cbuf *cbuf)
{
cmdbuf_add(cbuf, I_END);
if (cbuf->num_cmds % 2)
cmdbuf_add(cbuf, I_END);
}
static void put_start(struct pt3_i2cbuf *cbuf)
{
cmdbuf_add(cbuf, I_DATA_H);
cmdbuf_add(cbuf, I_CLOCK_H);
cmdbuf_add(cbuf, I_DATA_L);
cmdbuf_add(cbuf, I_CLOCK_L);
}
static void put_byte_write(struct pt3_i2cbuf *cbuf, u8 val)
{
u8 mask;
mask = 0x80;
for (mask = 0x80; mask > 0; mask >>= 1)
cmdbuf_add(cbuf, (val & mask) ? I_DATA_H_NOP : I_DATA_L_NOP);
cmdbuf_add(cbuf, I_DATA_H_ACK0);
}
static void put_byte_read(struct pt3_i2cbuf *cbuf, u32 size)
{
int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < 8; j++)
cmdbuf_add(cbuf, I_DATA_H_READ);
cmdbuf_add(cbuf, (i == size - 1) ? I_DATA_H_NOP : I_DATA_L_NOP);
}
}
static void put_stop(struct pt3_i2cbuf *cbuf)
{
cmdbuf_add(cbuf, I_DATA_L);
cmdbuf_add(cbuf, I_CLOCK_H);
cmdbuf_add(cbuf, I_DATA_H);
}
/* translates msgs to internal commands for bit-banging */
static void translate(struct pt3_i2cbuf *cbuf, struct i2c_msg *msgs, int num)
{
int i, j;
bool rd;
cbuf->num_cmds = 0;
for (i = 0; i < num; i++) {
rd = !!(msgs[i].flags & I2C_M_RD);
put_start(cbuf);
put_byte_write(cbuf, msgs[i].addr << 1 | rd);
if (rd)
put_byte_read(cbuf, msgs[i].len);
else
for (j = 0; j < msgs[i].len; j++)
put_byte_write(cbuf, msgs[i].buf[j]);
}
if (num > 0) {
put_stop(cbuf);
put_end(cbuf);
}
}
static int wait_i2c_result(struct pt3_board *pt3, u32 *result, int max_wait)
{
int i;
u32 v;
for (i = 0; i < max_wait; i++) {
v = ioread32(pt3->regs[0] + REG_I2C_R);
if (!(v & STAT_SEQ_RUNNING))
break;
usleep_range(500, 750);
}
if (i >= max_wait)
return -EIO;
if (result)
*result = v;
return 0;
}
/* send [pre-]translated i2c msgs stored at addr */
static int send_i2c_cmd(struct pt3_board *pt3, u32 addr)
{
u32 ret;
/* make sure that previous transactions had finished */
if (wait_i2c_result(pt3, NULL, 50)) {
dev_warn(&pt3->pdev->dev, "(%s) prev. transaction stalled\n",
__func__);
return -EIO;
}
iowrite32(PT3_I2C_RUN | addr, pt3->regs[0] + REG_I2C_W);
usleep_range(200, 300);
/* wait for the current transaction to finish */
if (wait_i2c_result(pt3, &ret, 500) || (ret & STAT_SEQ_ERROR)) {
dev_warn(&pt3->pdev->dev, "(%s) failed.\n", __func__);
return -EIO;
}
return 0;
}
/* init commands for each demod are combined into one transaction
* and hidden in ROM with the address PT3_CMD_ADDR_INIT_DEMOD.
*/
int pt3_init_all_demods(struct pt3_board *pt3)
{
ioread32(pt3->regs[0] + REG_I2C_R);
return send_i2c_cmd(pt3, PT3_CMD_ADDR_INIT_DEMOD);
}
/* init commands for two ISDB-T tuners are hidden in ROM. */
int pt3_init_all_mxl301rf(struct pt3_board *pt3)
{
usleep_range(1000, 2000);
return send_i2c_cmd(pt3, PT3_CMD_ADDR_INIT_TUNER);
}
void pt3_i2c_reset(struct pt3_board *pt3)
{
iowrite32(PT3_I2C_RESET, pt3->regs[0] + REG_I2C_W);
}
/*
* I2C algorithm
*/
int
pt3_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
struct pt3_board *pt3;
struct pt3_i2cbuf *cbuf;
int i;
void __iomem *p;
pt3 = i2c_get_adapdata(adap);
cbuf = pt3->i2c_buf;
for (i = 0; i < num; i++)
if (msgs[i].flags & I2C_M_RECV_LEN) {
dev_warn(&pt3->pdev->dev,
"(%s) I2C_M_RECV_LEN not supported.\n",
__func__);
return -EINVAL;
}
translate(cbuf, msgs, num);
memcpy_toio(pt3->regs[1] + PT3_I2C_BASE + PT3_CMD_ADDR_NORMAL / 2,
cbuf->data, cbuf->num_cmds);
if (send_i2c_cmd(pt3, PT3_CMD_ADDR_NORMAL) < 0)
return -EIO;
p = pt3->regs[1] + PT3_I2C_BASE;
for (i = 0; i < num; i++)
if ((msgs[i].flags & I2C_M_RD) && msgs[i].len > 0) {
memcpy_fromio(msgs[i].buf, p, msgs[i].len);
p += msgs[i].len;
}
return num;
}
u32 pt3_i2c_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment