Commit f571e004 authored by Antti Palosaari's avatar Antti Palosaari Committed by Mauro Carvalho Chehab

[media] af9013: rewrite whole driver

Rewrite whole af9013 demod driver in order to decrease I2C I/O.

We need to decrease I2C load since AF9015 (I2C adapter / USB-bridge used)
seems to have some problems under heavy I2C traffic.
Signed-off-by: default avatarAntti Palosaari <crope@iki.fi>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent e90ab840
...@@ -50,14 +50,14 @@ static int af9015_properties_count = ARRAY_SIZE(af9015_properties); ...@@ -50,14 +50,14 @@ static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
static struct af9013_config af9015_af9013_config[] = { static struct af9013_config af9015_af9013_config[] = {
{ {
.demod_address = AF9015_I2C_DEMOD, .i2c_addr = AF9015_I2C_DEMOD,
.output_mode = AF9013_OUTPUT_MODE_USB, .ts_mode = AF9013_TS_USB,
.api_version = { 0, 1, 9, 0 }, .api_version = { 0, 1, 9, 0 },
.gpio[0] = AF9013_GPIO_HI, .gpio[0] = AF9013_GPIO_HI,
.gpio[3] = AF9013_GPIO_TUNER_ON, .gpio[3] = AF9013_GPIO_TUNER_ON,
}, { }, {
.output_mode = AF9013_OUTPUT_MODE_SERIAL, .ts_mode = AF9013_TS_SERIAL,
.api_version = { 0, 1, 9, 0 }, .api_version = { 0, 1, 9, 0 },
.gpio[0] = AF9013_GPIO_TUNER_ON, .gpio[0] = AF9013_GPIO_TUNER_ON,
.gpio[1] = AF9013_GPIO_LO, .gpio[1] = AF9013_GPIO_LO,
...@@ -216,8 +216,8 @@ static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg, ...@@ -216,8 +216,8 @@ static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
{ {
struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val}; struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
if (addr == af9015_af9013_config[0].demod_address || if (addr == af9015_af9013_config[0].i2c_addr ||
addr == af9015_af9013_config[1].demod_address) addr == af9015_af9013_config[1].i2c_addr)
req.addr_len = 3; req.addr_len = 3;
return af9015_ctrl_msg(d, &req); return af9015_ctrl_msg(d, &req);
...@@ -228,8 +228,8 @@ static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg, ...@@ -228,8 +228,8 @@ static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
{ {
struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val}; struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
if (addr == af9015_af9013_config[0].demod_address || if (addr == af9015_af9013_config[0].i2c_addr ||
addr == af9015_af9013_config[1].demod_address) addr == af9015_af9013_config[1].i2c_addr)
req.addr_len = 3; req.addr_len = 3;
return af9015_ctrl_msg(d, &req); return af9015_ctrl_msg(d, &req);
...@@ -271,8 +271,8 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate. ...@@ -271,8 +271,8 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
return -EAGAIN; return -EAGAIN;
while (i < num) { while (i < num) {
if (msg[i].addr == af9015_af9013_config[0].demod_address || if (msg[i].addr == af9015_af9013_config[0].i2c_addr ||
msg[i].addr == af9015_af9013_config[1].demod_address) { msg[i].addr == af9015_af9013_config[1].i2c_addr) {
addr = msg[i].buf[0] << 8; addr = msg[i].buf[0] << 8;
addr += msg[i].buf[1]; addr += msg[i].buf[1];
mbox = msg[i].buf[2]; mbox = msg[i].buf[2];
...@@ -288,8 +288,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate. ...@@ -288,8 +288,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
ret = -EOPNOTSUPP; ret = -EOPNOTSUPP;
goto error; goto error;
} }
if (msg[i].addr == if (msg[i].addr == af9015_af9013_config[0].i2c_addr)
af9015_af9013_config[0].demod_address)
req.cmd = READ_MEMORY; req.cmd = READ_MEMORY;
else else
req.cmd = READ_I2C; req.cmd = READ_I2C;
...@@ -307,7 +306,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate. ...@@ -307,7 +306,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
goto error; goto error;
} }
if (msg[i].addr == if (msg[i].addr ==
af9015_af9013_config[0].demod_address) { af9015_af9013_config[0].i2c_addr) {
ret = -EINVAL; ret = -EINVAL;
goto error; goto error;
} }
...@@ -325,8 +324,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate. ...@@ -325,8 +324,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
ret = -EOPNOTSUPP; ret = -EOPNOTSUPP;
goto error; goto error;
} }
if (msg[i].addr == if (msg[i].addr == af9015_af9013_config[0].i2c_addr)
af9015_af9013_config[0].demod_address)
req.cmd = WRITE_MEMORY; req.cmd = WRITE_MEMORY;
else else
req.cmd = WRITE_I2C; req.cmd = WRITE_I2C;
...@@ -508,7 +506,7 @@ static int af9015_copy_firmware(struct dvb_usb_device *d) ...@@ -508,7 +506,7 @@ static int af9015_copy_firmware(struct dvb_usb_device *d)
msleep(100); msleep(100);
ret = af9015_read_reg_i2c(d, ret = af9015_read_reg_i2c(d,
af9015_af9013_config[1].demod_address, 0x98be, &val); af9015_af9013_config[1].i2c_addr, 0x98be, &val);
if (ret) if (ret)
goto error; goto error;
else else
...@@ -536,7 +534,7 @@ static int af9015_copy_firmware(struct dvb_usb_device *d) ...@@ -536,7 +534,7 @@ static int af9015_copy_firmware(struct dvb_usb_device *d)
goto error; goto error;
/* request boot firmware */ /* request boot firmware */
ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address, ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].i2c_addr,
0xe205, 1); 0xe205, 1);
deb_info("%s: firmware boot cmd status:%d\n", __func__, ret); deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
if (ret) if (ret)
...@@ -547,7 +545,7 @@ static int af9015_copy_firmware(struct dvb_usb_device *d) ...@@ -547,7 +545,7 @@ static int af9015_copy_firmware(struct dvb_usb_device *d)
/* check firmware status */ /* check firmware status */
ret = af9015_read_reg_i2c(d, ret = af9015_read_reg_i2c(d,
af9015_af9013_config[1].demod_address, 0x98be, &val); af9015_af9013_config[1].i2c_addr, 0x98be, &val);
deb_info("%s: firmware status cmd status:%d fw status:%02x\n", deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
__func__, ret, val); __func__, ret, val);
if (ret) if (ret)
...@@ -840,7 +838,7 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -840,7 +838,7 @@ static int af9015_read_config(struct usb_device *udev)
if (ret) if (ret)
goto error; goto error;
deb_info("%s: IR mode:%d\n", __func__, val); deb_info("%s: IR mode=%d\n", __func__, val);
for (i = 0; i < af9015_properties_count; i++) { for (i = 0; i < af9015_properties_count; i++) {
if (val == AF9015_IR_MODE_DISABLED) if (val == AF9015_IR_MODE_DISABLED)
af9015_properties[i].rc.core.rc_codes = NULL; af9015_properties[i].rc.core.rc_codes = NULL;
...@@ -854,7 +852,7 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -854,7 +852,7 @@ static int af9015_read_config(struct usb_device *udev)
if (ret) if (ret)
goto error; goto error;
af9015_config.dual_mode = val; af9015_config.dual_mode = val;
deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode); deb_info("%s: TS mode=%d\n", __func__, af9015_config.dual_mode);
/* Set adapter0 buffer size according to USB port speed, adapter1 buffer /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
size can be static because it is enabled only USB2.0 */ size can be static because it is enabled only USB2.0 */
...@@ -878,7 +876,7 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -878,7 +876,7 @@ static int af9015_read_config(struct usb_device *udev)
ret = af9015_rw_udev(udev, &req); ret = af9015_rw_udev(udev, &req);
if (ret) if (ret)
goto error; goto error;
af9015_af9013_config[1].demod_address = val; af9015_af9013_config[1].i2c_addr = val;
/* enable 2nd adapter */ /* enable 2nd adapter */
for (i = 0; i < af9015_properties_count; i++) for (i = 0; i < af9015_properties_count; i++)
...@@ -900,34 +898,38 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -900,34 +898,38 @@ static int af9015_read_config(struct usb_device *udev)
goto error; goto error;
switch (val) { switch (val) {
case 0: case 0:
af9015_af9013_config[i].adc_clock = 28800; af9015_af9013_config[i].clock = 28800000;
break; break;
case 1: case 1:
af9015_af9013_config[i].adc_clock = 20480; af9015_af9013_config[i].clock = 20480000;
break; break;
case 2: case 2:
af9015_af9013_config[i].adc_clock = 28000; af9015_af9013_config[i].clock = 28000000;
break; break;
case 3: case 3:
af9015_af9013_config[i].adc_clock = 25000; af9015_af9013_config[i].clock = 25000000;
break; break;
}; };
deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i, deb_info("%s: [%d] xtal=%d set clock=%d\n", __func__, i,
val, af9015_af9013_config[i].adc_clock); val, af9015_af9013_config[i].clock);
/* tuner IF */ /* IF frequency */
req.addr = AF9015_EEPROM_IF1H + offset; req.addr = AF9015_EEPROM_IF1H + offset;
ret = af9015_rw_udev(udev, &req); ret = af9015_rw_udev(udev, &req);
if (ret) if (ret)
goto error; goto error;
af9015_af9013_config[i].tuner_if = val << 8;
af9015_af9013_config[i].if_frequency = val << 8;
req.addr = AF9015_EEPROM_IF1L + offset; req.addr = AF9015_EEPROM_IF1L + offset;
ret = af9015_rw_udev(udev, &req); ret = af9015_rw_udev(udev, &req);
if (ret) if (ret)
goto error; goto error;
af9015_af9013_config[i].tuner_if += val;
deb_info("%s: [%d] IF1:%d\n", __func__, i, af9015_af9013_config[i].if_frequency += val;
af9015_af9013_config[0].tuner_if); af9015_af9013_config[i].if_frequency *= 1000;
deb_info("%s: [%d] IF frequency=%d\n", __func__, i,
af9015_af9013_config[0].if_frequency);
/* MT2060 IF1 */ /* MT2060 IF1 */
req.addr = AF9015_EEPROM_MT2060_IF1H + offset; req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
...@@ -940,7 +942,7 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -940,7 +942,7 @@ static int af9015_read_config(struct usb_device *udev)
if (ret) if (ret)
goto error; goto error;
af9015_config.mt2060_if1[i] += val; af9015_config.mt2060_if1[i] += val;
deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i, deb_info("%s: [%d] MT2060 IF1=%d\n", __func__, i,
af9015_config.mt2060_if1[i]); af9015_config.mt2060_if1[i]);
/* tuner */ /* tuner */
...@@ -957,30 +959,30 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -957,30 +959,30 @@ static int af9015_read_config(struct usb_device *udev)
case AF9013_TUNER_TDA18271: case AF9013_TUNER_TDA18271:
case AF9013_TUNER_QT1010A: case AF9013_TUNER_QT1010A:
case AF9013_TUNER_TDA18218: case AF9013_TUNER_TDA18218:
af9015_af9013_config[i].rf_spec_inv = 1; af9015_af9013_config[i].spec_inv = 1;
break; break;
case AF9013_TUNER_MXL5003D: case AF9013_TUNER_MXL5003D:
case AF9013_TUNER_MXL5005D: case AF9013_TUNER_MXL5005D:
case AF9013_TUNER_MXL5005R: case AF9013_TUNER_MXL5005R:
case AF9013_TUNER_MXL5007T: case AF9013_TUNER_MXL5007T:
af9015_af9013_config[i].rf_spec_inv = 0; af9015_af9013_config[i].spec_inv = 0;
break; break;
case AF9013_TUNER_MC44S803: case AF9013_TUNER_MC44S803:
af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO; af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
af9015_af9013_config[i].rf_spec_inv = 1; af9015_af9013_config[i].spec_inv = 1;
break; break;
default: default:
warn("tuner id:%d not supported, please report!", val); warn("tuner id=%d not supported, please report!", val);
return -ENODEV; return -ENODEV;
}; };
af9015_af9013_config[i].tuner = val; af9015_af9013_config[i].tuner = val;
deb_info("%s: [%d] tuner id:%d\n", __func__, i, val); deb_info("%s: [%d] tuner id=%d\n", __func__, i, val);
} }
error: error:
if (ret) if (ret)
err("eeprom read failed:%d", ret); err("eeprom read failed=%d", ret);
/* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
content :-( Override some wrong values here. Ditto for the content :-( Override some wrong values here. Ditto for the
...@@ -998,7 +1000,7 @@ static int af9015_read_config(struct usb_device *udev) ...@@ -998,7 +1000,7 @@ static int af9015_read_config(struct usb_device *udev)
af9015_properties[i].num_adapters = 1; af9015_properties[i].num_adapters = 1;
/* set correct IF */ /* set correct IF */
af9015_af9013_config[0].tuner_if = 4570; af9015_af9013_config[0].if_frequency = 4570000;
} }
return ret; return ret;
...@@ -1156,7 +1158,7 @@ static int af9015_af9013_sleep(struct dvb_frontend *fe) ...@@ -1156,7 +1158,7 @@ static int af9015_af9013_sleep(struct dvb_frontend *fe)
if (mutex_lock_interruptible(&adap->dev->usb_mutex)) if (mutex_lock_interruptible(&adap->dev->usb_mutex))
return -EAGAIN; return -EAGAIN;
ret = priv->init[adap->id](fe); ret = priv->sleep[adap->id](fe);
mutex_unlock(&adap->dev->usb_mutex); mutex_unlock(&adap->dev->usb_mutex);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* Afatech AF9013 demodulator driver * Afatech AF9013 demodulator driver
* *
* Copyright (C) 2007 Antti Palosaari <crope@iki.fi> * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
* Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
* *
* Thanks to Afatech who kindly provided information. * Thanks to Afatech who kindly provided information.
* *
...@@ -21,25 +22,15 @@ ...@@ -21,25 +22,15 @@
* *
*/ */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/firmware.h>
#include "dvb_frontend.h"
#include "af9013_priv.h" #include "af9013_priv.h"
#include "af9013.h"
int af9013_debug; int af9013_debug;
module_param_named(debug, af9013_debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
struct af9013_state { struct af9013_state {
struct i2c_adapter *i2c; struct i2c_adapter *i2c;
struct dvb_frontend frontend; struct dvb_frontend fe;
struct af9013_config config; struct af9013_config config;
/* tuner/demod RF and IF AGC limits used for signal strength calc */ /* tuner/demod RF and IF AGC limits used for signal strength calc */
...@@ -48,107 +39,178 @@ struct af9013_state { ...@@ -48,107 +39,178 @@ struct af9013_state {
u32 ber; u32 ber;
u32 ucblocks; u32 ucblocks;
u16 snr; u16 snr;
u32 frequency; u32 bandwidth_hz;
unsigned long next_statistics_check; fe_status_t fe_status;
unsigned long set_frontend_jiffies;
unsigned long read_status_jiffies;
bool first_tune;
bool i2c_gate_state;
unsigned int statistics_step:3;
struct delayed_work statistics_work;
}; };
static u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; /* write multiple registers */
static int af9013_wr_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
static int af9013_write_regs(struct af9013_state *state, u8 mbox, u16 reg, const u8 *val, int len)
u8 *val, u8 len)
{ {
int ret;
u8 buf[3+len]; u8 buf[3+len];
struct i2c_msg msg = { struct i2c_msg msg[1] = {
.addr = state->config.demod_address, {
.addr = priv->config.i2c_addr,
.flags = 0, .flags = 0,
.len = sizeof(buf), .len = sizeof(buf),
.buf = buf }; .buf = buf,
}
};
buf[0] = reg >> 8; buf[0] = (reg >> 8) & 0xff;
buf[1] = reg & 0xff; buf[1] = (reg >> 0) & 0xff;
buf[2] = mbox; buf[2] = mbox;
memcpy(&buf[3], val, len); memcpy(&buf[3], val, len);
if (i2c_transfer(state->i2c, &msg, 1) != 1) { ret = i2c_transfer(priv->i2c, msg, 1);
warn("I2C write failed reg:%04x len:%d", reg, len); if (ret == 1) {
return -EREMOTEIO; ret = 0;
} else {
warn("i2c wr failed=%d reg=%04x len=%d", ret, reg, len);
ret = -EREMOTEIO;
} }
return 0; return ret;
} }
static int af9013_write_ofdm_regs(struct af9013_state *state, u16 reg, u8 *val, /* read multiple registers */
u8 len) static int af9013_rd_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
u8 *val, int len)
{
int ret;
u8 buf[3];
struct i2c_msg msg[2] = {
{
.addr = priv->config.i2c_addr,
.flags = 0,
.len = 3,
.buf = buf,
}, {
.addr = priv->config.i2c_addr,
.flags = I2C_M_RD,
.len = len,
.buf = val,
}
};
buf[0] = (reg >> 8) & 0xff;
buf[1] = (reg >> 0) & 0xff;
buf[2] = mbox;
ret = i2c_transfer(priv->i2c, msg, 2);
if (ret == 2) {
ret = 0;
} else {
warn("i2c rd failed=%d reg=%04x len=%d", ret, reg, len);
ret = -EREMOTEIO;
}
return ret;
}
/* write multiple registers */
static int af9013_wr_regs(struct af9013_state *priv, u16 reg, const u8 *val,
int len)
{ {
u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(0 << 6)|(0 << 7); int ret, i;
return af9013_write_regs(state, mbox, reg, val, len); u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
if ((priv->config.ts_mode == AF9013_TS_USB) &
((reg & 0xff00) != 0xff00) & ((reg & 0xff00) != 0xae00)) {
mbox |= ((len - 1) << 2);
ret = af9013_wr_regs_i2c(priv, mbox, reg, val, len);
} else {
for (i = 0; i < len; i++) {
ret = af9013_wr_regs_i2c(priv, mbox, reg+i, val+i, 1);
if (ret)
goto err;
}
}
err:
return 0;
} }
static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val, /* read multiple registers */
u8 len) static int af9013_rd_regs(struct af9013_state *priv, u16 reg, u8 *val, int len)
{ {
u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(1 << 6)|(1 << 7); int ret, i;
return af9013_write_regs(state, mbox, reg, val, len); u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
if ((priv->config.ts_mode == AF9013_TS_USB) &
((reg & 0xff00) != 0xff00) & ((reg & 0xff00) != 0xae00)) {
mbox |= ((len - 1) << 2);
ret = af9013_rd_regs_i2c(priv, mbox, reg, val, len);
} else {
for (i = 0; i < len; i++) {
ret = af9013_rd_regs_i2c(priv, mbox, reg+i, val+i, 1);
if (ret)
goto err;
}
}
err:
return 0;
} }
/* write single register */ /* write single register */
static int af9013_write_reg(struct af9013_state *state, u16 reg, u8 val) static int af9013_wr_reg(struct af9013_state *priv, u16 reg, u8 val)
{ {
return af9013_write_ofdm_regs(state, reg, &val, 1); return af9013_wr_regs(priv, reg, &val, 1);
} }
/* read single register */ /* read single register */
static int af9013_read_reg(struct af9013_state *state, u16 reg, u8 *val) static int af9013_rd_reg(struct af9013_state *priv, u16 reg, u8 *val)
{ {
u8 obuf[3] = { reg >> 8, reg & 0xff, 0 }; return af9013_rd_regs(priv, reg, val, 1);
u8 ibuf[1]; }
struct i2c_msg msg[2] = {
{
.addr = state->config.demod_address,
.flags = 0,
.len = sizeof(obuf),
.buf = obuf
}, {
.addr = state->config.demod_address,
.flags = I2C_M_RD,
.len = sizeof(ibuf),
.buf = ibuf
}
};
if (i2c_transfer(state->i2c, msg, 2) != 2) { static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val,
warn("I2C read failed reg:%04x", reg); u8 len)
return -EREMOTEIO; {
} u8 mbox = (1 << 7)|(1 << 6)|((len - 1) << 2)|(1 << 1)|(1 << 0);
*val = ibuf[0]; return af9013_wr_regs_i2c(state, mbox, reg, val, len);
return 0;
} }
static int af9013_write_reg_bits(struct af9013_state *state, u16 reg, u8 pos, static int af9013_wr_reg_bits(struct af9013_state *state, u16 reg, int pos,
u8 len, u8 val) int len, u8 val)
{ {
int ret; int ret;
u8 tmp, mask; u8 tmp, mask;
ret = af9013_read_reg(state, reg, &tmp); /* no need for read if whole reg is written */
if (len != 8) {
ret = af9013_rd_reg(state, reg, &tmp);
if (ret) if (ret)
return ret; return ret;
mask = regmask[len - 1] << pos; mask = (0xff >> (8 - len)) << pos;
tmp = (tmp & ~mask) | ((val << pos) & mask); val <<= pos;
tmp &= ~mask;
val |= tmp;
}
return af9013_write_reg(state, reg, tmp); return af9013_wr_reg(state, reg, val);
} }
static int af9013_read_reg_bits(struct af9013_state *state, u16 reg, u8 pos, static int af9013_rd_reg_bits(struct af9013_state *state, u16 reg, int pos,
u8 len, u8 *val) int len, u8 *val)
{ {
int ret; int ret;
u8 tmp; u8 tmp;
ret = af9013_read_reg(state, reg, &tmp); ret = af9013_rd_reg(state, reg, &tmp);
if (ret) if (ret)
return ret; return ret;
*val = (tmp >> pos) & regmask[len - 1];
*val = (tmp >> pos);
*val &= (0xff >> (8 - len));
return 0; return 0;
} }
...@@ -157,10 +219,13 @@ static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval) ...@@ -157,10 +219,13 @@ static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
int ret; int ret;
u8 pos; u8 pos;
u16 addr; u16 addr;
deb_info("%s: gpio:%d gpioval:%02x\n", __func__, gpio, gpioval);
/* GPIO0 & GPIO1 0xd735 dbg("%s: gpio=%d gpioval=%02x", __func__, gpio, gpioval);
GPIO2 & GPIO3 0xd736 */
/*
* GPIO0 & GPIO1 0xd735
* GPIO2 & GPIO3 0xd736
*/
switch (gpio) { switch (gpio) {
case 0: case 0:
...@@ -175,7 +240,7 @@ static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval) ...@@ -175,7 +240,7 @@ static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
default: default:
err("invalid gpio:%d\n", gpio); err("invalid gpio:%d\n", gpio);
ret = -EINVAL; ret = -EINVAL;
goto error; goto err;
}; };
switch (gpio) { switch (gpio) {
...@@ -190,16 +255,21 @@ static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval) ...@@ -190,16 +255,21 @@ static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
break; break;
}; };
ret = af9013_write_reg_bits(state, addr, pos, 4, gpioval); ret = af9013_wr_reg_bits(state, addr, pos, 4, gpioval);
if (ret)
goto err;
error: return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret; return ret;
} }
static u32 af913_div(u32 a, u32 b, u32 x) static u32 af913_div(u32 a, u32 b, u32 x)
{ {
u32 r = 0, c = 0, i; u32 r = 0, c = 0, i;
deb_info("%s: a:%d b:%d x:%d\n", __func__, a, b, x);
dbg("%s: a=%d b=%d x=%d", __func__, a, b, x);
if (a > b) { if (a > b) {
c = a / b; c = a / b;
...@@ -216,182 +286,408 @@ static u32 af913_div(u32 a, u32 b, u32 x) ...@@ -216,182 +286,408 @@ static u32 af913_div(u32 a, u32 b, u32 x)
} }
r = (c << (u32)x) + r; r = (c << (u32)x) + r;
deb_info("%s: a:%d b:%d x:%d r:%d r:%x\n", __func__, a, b, x, r, r); dbg("%s: a=%d b=%d x=%d r=%x", __func__, a, b, x, r);
return r; return r;
} }
static int af9013_set_coeff(struct af9013_state *state, fe_bandwidth_t bw) static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
{ {
int ret, i, j, found; int ret, i;
deb_info("%s: adc_clock:%d bw:%d\n", __func__, u8 tmp;
state->config.adc_clock, bw);
/* lookup coeff from table */ dbg("%s: onoff=%d", __func__, onoff);
for (i = 0, found = 0; i < ARRAY_SIZE(coeff_table); i++) {
if (coeff_table[i].adc_clock == state->config.adc_clock && /* enable reset */
coeff_table[i].bw == bw) { ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 1);
found = 1; if (ret)
break; goto err;
}
/* start reset mechanism */
ret = af9013_wr_reg(state, 0xaeff, 1);
if (ret)
goto err;
/* wait reset performs */
for (i = 0; i < 150; i++) {
ret = af9013_rd_reg_bits(state, 0xd417, 1, 1, &tmp);
if (ret)
goto err;
if (tmp)
break; /* reset done */
usleep_range(5000, 25000);
} }
if (!found) { if (!tmp)
err("invalid bw or clock"); return -ETIMEDOUT;
ret = -EINVAL;
goto error; if (onoff) {
/* clear reset */
ret = af9013_wr_reg_bits(state, 0xd417, 1, 1, 0);
if (ret)
goto err;
/* disable reset */
ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 0);
/* power on */
ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 0);
} else {
/* power off */
ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 1);
} }
deb_info("%s: coeff: ", __func__); return ret;
debug_dump(coeff_table[i].val, sizeof(coeff_table[i].val), deb_info); err:
dbg("%s: failed=%d", __func__, ret);
return ret;
}
static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
dbg("%s", __func__);
/* program */ /* reset and start BER counter */
for (j = 0; j < sizeof(coeff_table[i].val); j++) { ret = af9013_wr_reg_bits(state, 0xd391, 4, 1, 1);
ret = af9013_write_reg(state, 0xae00 + j,
coeff_table[i].val[j]);
if (ret) if (ret)
break; goto err;
return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret;
}
static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
u8 buf[5];
dbg("%s", __func__);
/* check if error bit count is ready */
ret = af9013_rd_reg_bits(state, 0xd391, 4, 1, &buf[0]);
if (ret)
goto err;
if (!buf[0]) {
dbg("%s: not ready", __func__);
return 0;
} }
error: ret = af9013_rd_regs(state, 0xd387, buf, 5);
if (ret)
goto err;
state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
state->ucblocks += (buf[4] << 8) | buf[3];
return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret; return ret;
} }
static int af9013_set_adc_ctrl(struct af9013_state *state) static int af9013_statistics_snr_start(struct dvb_frontend *fe)
{ {
struct af9013_state *state = fe->demodulator_priv;
int ret; int ret;
u8 buf[3], tmp, i;
u32 adc_cw;
deb_info("%s: adc_clock:%d\n", __func__, state->config.adc_clock); dbg("%s", __func__);
/* adc frequency type */ /* start SNR meas */
switch (state->config.adc_clock) { ret = af9013_wr_reg_bits(state, 0xd2e1, 3, 1, 1);
case 28800: /* 28.800 MHz */ if (ret)
tmp = 0; goto err;
break;
case 20480: /* 20.480 MHz */ return ret;
tmp = 1; err:
dbg("%s: failed=%d", __func__, ret);
return ret;
}
static int af9013_statistics_snr_result(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret, i, len;
u8 buf[3], tmp;
u32 snr_val;
const struct af9013_snr *uninitialized_var(snr_lut);
dbg("%s", __func__);
/* check if SNR ready */
ret = af9013_rd_reg_bits(state, 0xd2e1, 3, 1, &tmp);
if (ret)
goto err;
if (!tmp) {
dbg("%s: not ready", __func__);
return 0;
}
/* read value */
ret = af9013_rd_regs(state, 0xd2e3, buf, 3);
if (ret)
goto err;
snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
/* read current modulation */
ret = af9013_rd_reg(state, 0xd3c1, &tmp);
if (ret)
goto err;
switch ((tmp >> 6) & 3) {
case 0:
len = ARRAY_SIZE(qpsk_snr_lut);
snr_lut = qpsk_snr_lut;
break; break;
case 28000: /* 28.000 MHz */ case 1:
tmp = 2; len = ARRAY_SIZE(qam16_snr_lut);
snr_lut = qam16_snr_lut;
break; break;
case 25000: /* 25.000 MHz */ case 2:
tmp = 3; len = ARRAY_SIZE(qam64_snr_lut);
snr_lut = qam64_snr_lut;
break; break;
default: default:
err("invalid xtal"); goto err;
return -EINVAL; break;
}
for (i = 0; i < len; i++) {
tmp = snr_lut[i].snr;
if (snr_val < snr_lut[i].val)
break;
} }
state->snr = tmp * 10; /* dB/10 */
adc_cw = af913_div(state->config.adc_clock*1000, 1000000ul, 19ul); return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret;
}
static int af9013_statistics_signal_strength(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret = 0;
u8 buf[2], rf_gain, if_gain;
int signal_strength;
buf[0] = (u8) ((adc_cw & 0x000000ff)); dbg("%s", __func__);
buf[1] = (u8) ((adc_cw & 0x0000ff00) >> 8);
buf[2] = (u8) ((adc_cw & 0x00ff0000) >> 16);
deb_info("%s: adc_cw:", __func__); if (!state->signal_strength_en)
debug_dump(buf, sizeof(buf), deb_info); return 0;
/* program */ ret = af9013_rd_regs(state, 0xd07c, buf, 2);
for (i = 0; i < sizeof(buf); i++) {
ret = af9013_write_reg(state, 0xd180 + i, buf[i]);
if (ret) if (ret)
goto error; goto err;
}
ret = af9013_write_reg_bits(state, 0x9bd2, 0, 4, tmp); rf_gain = buf[0];
error: if_gain = buf[1];
signal_strength = (0xffff / \
(9 * (state->rf_50 + state->if_50) - \
11 * (state->rf_80 + state->if_80))) * \
(10 * (rf_gain + if_gain) - \
11 * (state->rf_80 + state->if_80));
if (signal_strength < 0)
signal_strength = 0;
else if (signal_strength > 0xffff)
signal_strength = 0xffff;
state->signal_strength = signal_strength;
return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret; return ret;
} }
static int af9013_set_freq_ctrl(struct af9013_state *state, static void af9013_statistics_work(struct work_struct *work)
struct dvb_frontend *fe)
{ {
int ret; int ret;
u16 addr; struct af9013_state *state = container_of(work,
u8 buf[3], i, j; struct af9013_state, statistics_work.work);
u32 adc_freq, freq_cw; unsigned int next_msec;
s8 bfs_spec_inv;
int if_sample_freq; /* update only signal strength when demod is not locked */
if (!(state->fe_status & FE_HAS_LOCK)) {
for (j = 0; j < 3; j++) { state->statistics_step = 0;
if (j == 0) { state->ber = 0;
addr = 0xd140; /* fcw normal */ state->snr = 0;
bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1;
} else if (j == 1) {
addr = 0x9be7; /* fcw dummy ram */
bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1;
} else {
addr = 0x9bea; /* fcw inverted */
bfs_spec_inv = state->config.rf_spec_inv ? 1 : -1;
} }
adc_freq = state->config.adc_clock * 1000; switch (state->statistics_step) {
default:
state->statistics_step = 0;
case 0:
ret = af9013_statistics_signal_strength(&state->fe);
state->statistics_step++;
next_msec = 300;
break;
case 1:
ret = af9013_statistics_snr_start(&state->fe);
state->statistics_step++;
next_msec = 200;
break;
case 2:
ret = af9013_statistics_ber_unc_start(&state->fe);
state->statistics_step++;
next_msec = 1000;
break;
case 3:
ret = af9013_statistics_snr_result(&state->fe);
state->statistics_step++;
next_msec = 400;
break;
case 4:
ret = af9013_statistics_ber_unc_result(&state->fe);
state->statistics_step++;
next_msec = 100;
break;
}
schedule_delayed_work(&state->statistics_work,
msecs_to_jiffies(next_msec));
return;
}
static int af9013_get_tune_settings(struct dvb_frontend *fe,
struct dvb_frontend_tune_settings *fesettings)
{
fesettings->min_delay_ms = 800;
fesettings->step_size = 0;
fesettings->max_drift = 0;
return 0;
}
static int af9013_set_frontend(struct dvb_frontend *fe,
struct dvb_frontend_parameters *p)
{
struct af9013_state *state = fe->demodulator_priv;
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
int ret, i, sampling_freq;
bool auto_mode, spec_inv;
u8 buf[6];
u32 if_frequency, freq_cw;
dbg("%s: frequency=%d bandwidth_hz=%d", __func__,
c->frequency, c->bandwidth_hz);
/* program tuner */
if (fe->ops.tuner_ops.set_params)
fe->ops.tuner_ops.set_params(fe, p);
/* program CFOE coefficients */
if (c->bandwidth_hz != state->bandwidth_hz) {
for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
if (coeff_lut[i].clock == state->config.clock &&
coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
break;
}
}
ret = af9013_wr_regs(state, 0xae00, coeff_lut[i].val,
sizeof(coeff_lut[i].val));
}
/* program frequency control */
if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) {
/* get used IF frequency */ /* get used IF frequency */
if (fe->ops.tuner_ops.get_if_frequency) if (fe->ops.tuner_ops.get_if_frequency)
fe->ops.tuner_ops.get_if_frequency(fe, &if_sample_freq); fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
else else
if_sample_freq = state->config.tuner_if * 1000; if_frequency = state->config.if_frequency;
while (if_sample_freq > (adc_freq / 2)) sampling_freq = if_frequency;
if_sample_freq = if_sample_freq - adc_freq;
if (if_sample_freq >= 0) while (sampling_freq > (state->config.clock / 2))
bfs_spec_inv = bfs_spec_inv * (-1); sampling_freq -= state->config.clock;
else
if_sample_freq = if_sample_freq * (-1);
freq_cw = af913_div(if_sample_freq, adc_freq, 23ul); if (sampling_freq < 0) {
sampling_freq *= -1;
spec_inv = state->config.spec_inv;
} else {
spec_inv = !state->config.spec_inv;
}
if (bfs_spec_inv == -1) freq_cw = af913_div(sampling_freq, state->config.clock, 23);
freq_cw = 0x00800000 - freq_cw;
buf[0] = (u8) ((freq_cw & 0x000000ff)); if (spec_inv)
buf[1] = (u8) ((freq_cw & 0x0000ff00) >> 8); freq_cw = 0x800000 - freq_cw;
buf[2] = (u8) ((freq_cw & 0x007f0000) >> 16);
buf[0] = (freq_cw >> 0) & 0xff;
buf[1] = (freq_cw >> 8) & 0xff;
buf[2] = (freq_cw >> 16) & 0x7f;
deb_info("%s: freq_cw:", __func__); freq_cw = 0x800000 - freq_cw;
debug_dump(buf, sizeof(buf), deb_info);
/* program */ buf[3] = (freq_cw >> 0) & 0xff;
for (i = 0; i < sizeof(buf); i++) { buf[4] = (freq_cw >> 8) & 0xff;
ret = af9013_write_reg(state, addr++, buf[i]); buf[5] = (freq_cw >> 16) & 0x7f;
ret = af9013_wr_regs(state, 0xd140, buf, 3);
if (ret) if (ret)
goto error; goto err;
}
ret = af9013_wr_regs(state, 0x9be7, buf, 6);
if (ret)
goto err;
} }
error:
return ret;
}
static int af9013_set_ofdm_params(struct af9013_state *state, /* clear TPS lock flag */
struct dvb_ofdm_parameters *params, u8 *auto_mode) ret = af9013_wr_reg_bits(state, 0xd330, 3, 1, 1);
{ if (ret)
int ret; goto err;
u8 i, buf[3] = {0, 0, 0};
*auto_mode = 0; /* set if parameters are requested to auto set */
/* Try auto-detect transmission parameters in case of AUTO requested or /* clear MPEG2 lock flag */
garbage parameters given by application for compatibility. ret = af9013_wr_reg_bits(state, 0xd507, 6, 1, 0);
MPlayer seems to provide garbage parameters currently. */ if (ret)
goto err;
switch (params->transmission_mode) { /* empty channel function */
ret = af9013_wr_reg_bits(state, 0x9bfe, 0, 1, 0);
if (ret)
goto err;
/* empty DVB-T channel function */
ret = af9013_wr_reg_bits(state, 0x9bc2, 0, 1, 0);
if (ret)
goto err;
/* transmission parameters */
auto_mode = false;
memset(buf, 0, 3);
switch (c->transmission_mode) {
case TRANSMISSION_MODE_AUTO: case TRANSMISSION_MODE_AUTO:
*auto_mode = 1; auto_mode = 1;
break;
case TRANSMISSION_MODE_2K: case TRANSMISSION_MODE_2K:
break; break;
case TRANSMISSION_MODE_8K: case TRANSMISSION_MODE_8K:
buf[0] |= (1 << 0); buf[0] |= (1 << 0);
break; break;
default: default:
deb_info("%s: invalid transmission_mode\n", __func__); dbg("%s: invalid transmission_mode", __func__);
*auto_mode = 1; auto_mode = 1;
} }
switch (params->guard_interval) { switch (c->guard_interval) {
case GUARD_INTERVAL_AUTO: case GUARD_INTERVAL_AUTO:
*auto_mode = 1; auto_mode = 1;
break;
case GUARD_INTERVAL_1_32: case GUARD_INTERVAL_1_32:
break; break;
case GUARD_INTERVAL_1_16: case GUARD_INTERVAL_1_16:
...@@ -404,13 +700,14 @@ static int af9013_set_ofdm_params(struct af9013_state *state, ...@@ -404,13 +700,14 @@ static int af9013_set_ofdm_params(struct af9013_state *state,
buf[0] |= (3 << 2); buf[0] |= (3 << 2);
break; break;
default: default:
deb_info("%s: invalid guard_interval\n", __func__); dbg("%s: invalid guard_interval", __func__);
*auto_mode = 1; auto_mode = 1;
} }
switch (params->hierarchy_information) { switch (c->hierarchy) {
case HIERARCHY_AUTO: case HIERARCHY_AUTO:
*auto_mode = 1; auto_mode = 1;
break;
case HIERARCHY_NONE: case HIERARCHY_NONE:
break; break;
case HIERARCHY_1: case HIERARCHY_1:
...@@ -423,13 +720,14 @@ static int af9013_set_ofdm_params(struct af9013_state *state, ...@@ -423,13 +720,14 @@ static int af9013_set_ofdm_params(struct af9013_state *state,
buf[0] |= (3 << 4); buf[0] |= (3 << 4);
break; break;
default: default:
deb_info("%s: invalid hierarchy_information\n", __func__); dbg("%s: invalid hierarchy", __func__);
*auto_mode = 1; auto_mode = 1;
}; };
switch (params->constellation) { switch (c->modulation) {
case QAM_AUTO: case QAM_AUTO:
*auto_mode = 1; auto_mode = 1;
break;
case QPSK: case QPSK:
break; break;
case QAM_16: case QAM_16:
...@@ -439,16 +737,17 @@ static int af9013_set_ofdm_params(struct af9013_state *state, ...@@ -439,16 +737,17 @@ static int af9013_set_ofdm_params(struct af9013_state *state,
buf[1] |= (2 << 6); buf[1] |= (2 << 6);
break; break;
default: default:
deb_info("%s: invalid constellation\n", __func__); dbg("%s: invalid modulation", __func__);
*auto_mode = 1; auto_mode = 1;
} }
/* Use HP. How and which case we can switch to LP? */ /* Use HP. How and which case we can switch to LP? */
buf[1] |= (1 << 4); buf[1] |= (1 << 4);
switch (params->code_rate_HP) { switch (c->code_rate_HP) {
case FEC_AUTO: case FEC_AUTO:
*auto_mode = 1; auto_mode = 1;
break;
case FEC_1_2: case FEC_1_2:
break; break;
case FEC_2_3: case FEC_2_3:
...@@ -464,16 +763,14 @@ static int af9013_set_ofdm_params(struct af9013_state *state, ...@@ -464,16 +763,14 @@ static int af9013_set_ofdm_params(struct af9013_state *state,
buf[2] |= (4 << 0); buf[2] |= (4 << 0);
break; break;
default: default:
deb_info("%s: invalid code_rate_HP\n", __func__); dbg("%s: invalid code_rate_HP", __func__);
*auto_mode = 1; auto_mode = 1;
} }
switch (params->code_rate_LP) { switch (c->code_rate_LP) {
case FEC_AUTO: case FEC_AUTO:
/* if HIERARCHY_NONE and FEC_NONE then LP FEC is set to FEC_AUTO auto_mode = 1;
by dvb_frontend.c for compatibility */ break;
if (params->hierarchy_information != HIERARCHY_NONE)
*auto_mode = 1;
case FEC_1_2: case FEC_1_2:
break; break;
case FEC_2_3: case FEC_2_3:
...@@ -489,183 +786,63 @@ static int af9013_set_ofdm_params(struct af9013_state *state, ...@@ -489,183 +786,63 @@ static int af9013_set_ofdm_params(struct af9013_state *state,
buf[2] |= (4 << 3); buf[2] |= (4 << 3);
break; break;
case FEC_NONE: case FEC_NONE:
if (params->hierarchy_information == HIERARCHY_AUTO)
break; break;
default: default:
deb_info("%s: invalid code_rate_LP\n", __func__); dbg("%s: invalid code_rate_LP", __func__);
*auto_mode = 1; auto_mode = 1;
} }
switch (params->bandwidth) { switch (c->bandwidth_hz) {
case BANDWIDTH_6_MHZ: case 6000000:
break; break;
case BANDWIDTH_7_MHZ: case 7000000:
buf[1] |= (1 << 2); buf[1] |= (1 << 2);
break; break;
case BANDWIDTH_8_MHZ: case 8000000:
buf[1] |= (2 << 2); buf[1] |= (2 << 2);
break; break;
default: default:
deb_info("%s: invalid bandwidth\n", __func__); dbg("%s: invalid bandwidth_hz", __func__);
buf[1] |= (2 << 2); /* cannot auto-detect BW, try 8 MHz */ ret = -EINVAL;
} goto err;
/* program */
for (i = 0; i < sizeof(buf); i++) {
ret = af9013_write_reg(state, 0xd3c0 + i, buf[i]);
if (ret)
break;
} }
return ret; ret = af9013_wr_regs(state, 0xd3c0, buf, 3);
}
static int af9013_reset(struct af9013_state *state, u8 sleep)
{
int ret;
u8 tmp, i;
deb_info("%s\n", __func__);
/* enable OFDM reset */
ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 1);
if (ret) if (ret)
goto error; goto err;
/* start reset mechanism */ if (auto_mode) {
ret = af9013_write_reg(state, 0xaeff, 1); /* clear easy mode flag */
if (ret) ret = af9013_wr_reg(state, 0xaefd, 0);
goto error;
/* reset is done when bit 1 is set */
for (i = 0; i < 150; i++) {
ret = af9013_read_reg_bits(state, 0xd417, 1, 1, &tmp);
if (ret)
goto error;
if (tmp)
break; /* reset done */
msleep(10);
}
if (!tmp)
return -ETIMEDOUT;
/* don't clear reset when going to sleep */
if (!sleep) {
/* clear OFDM reset */
ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0);
if (ret) if (ret)
goto error; goto err;
/* disable OFDM reset */ dbg("%s: auto params", __func__);
ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0);
}
error:
return ret;
}
static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
{
int ret;
deb_info("%s: onoff:%d\n", __func__, onoff);
if (onoff) {
/* power on */
ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 0);
if (ret)
goto error;
ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0);
if (ret)
goto error;
ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0);
} else { } else {
/* power off */ /* set easy mode flag */
ret = af9013_reset(state, 1); ret = af9013_wr_reg(state, 0xaefd, 1);
if (ret)
goto error;
ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 1);
}
error:
return ret;
}
static int af9013_lock_led(struct af9013_state *state, u8 onoff)
{
deb_info("%s: onoff:%d\n", __func__, onoff);
return af9013_write_reg_bits(state, 0xd730, 0, 1, onoff);
}
static int af9013_set_frontend(struct dvb_frontend *fe,
struct dvb_frontend_parameters *params)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
u8 auto_mode; /* auto set TPS */
deb_info("%s: freq:%d bw:%d\n", __func__, params->frequency,
params->u.ofdm.bandwidth);
state->frequency = params->frequency;
/* program tuner */
if (fe->ops.tuner_ops.set_params)
fe->ops.tuner_ops.set_params(fe, params);
/* program CFOE coefficients */
ret = af9013_set_coeff(state, params->u.ofdm.bandwidth);
if (ret)
goto error;
/* program frequency control */
ret = af9013_set_freq_ctrl(state, fe);
if (ret)
goto error;
/* clear TPS lock flag (inverted flag) */
ret = af9013_write_reg_bits(state, 0xd330, 3, 1, 1);
if (ret)
goto error;
/* clear MPEG2 lock flag */
ret = af9013_write_reg_bits(state, 0xd507, 6, 1, 0);
if (ret)
goto error;
/* empty channel function */
ret = af9013_write_reg_bits(state, 0x9bfe, 0, 1, 0);
if (ret)
goto error;
/* empty DVB-T channel function */
ret = af9013_write_reg_bits(state, 0x9bc2, 0, 1, 0);
if (ret) if (ret)
goto error; goto err;
/* program TPS and bandwidth, check if auto mode needed */ ret = af9013_wr_reg(state, 0xaefe, 0);
ret = af9013_set_ofdm_params(state, &params->u.ofdm, &auto_mode);
if (ret) if (ret)
goto error; goto err;
if (auto_mode) { dbg("%s: manual params", __func__);
/* clear easy mode flag */
ret = af9013_write_reg(state, 0xaefd, 0);
deb_info("%s: auto TPS\n", __func__);
} else {
/* set easy mode flag */
ret = af9013_write_reg(state, 0xaefd, 1);
if (ret)
goto error;
ret = af9013_write_reg(state, 0xaefe, 0);
deb_info("%s: manual TPS\n", __func__);
} }
if (ret)
goto error;
/* everything is set, lets try to receive channel - OFSM GO! */ /* tune */
ret = af9013_write_reg(state, 0xffff, 0); ret = af9013_wr_reg(state, 0xffff, 0);
if (ret) if (ret)
goto error; goto err;
state->bandwidth_hz = c->bandwidth_hz;
state->set_frontend_jiffies = jiffies;
state->first_tune = false;
error: return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret; return ret;
} }
...@@ -673,525 +850,310 @@ static int af9013_get_frontend(struct dvb_frontend *fe, ...@@ -673,525 +850,310 @@ static int af9013_get_frontend(struct dvb_frontend *fe,
struct dvb_frontend_parameters *p) struct dvb_frontend_parameters *p)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
struct dtv_frontend_properties *c = &fe->dtv_property_cache;
int ret; int ret;
u8 i, buf[3]; u8 buf[3];
deb_info("%s\n", __func__);
dbg("%s", __func__);
/* read TPS registers */ ret = af9013_rd_regs(state, 0xd3c0, buf, 3);
for (i = 0; i < 3; i++) {
ret = af9013_read_reg(state, 0xd3c0 + i, &buf[i]);
if (ret) if (ret)
goto error; goto err;
}
switch ((buf[1] >> 6) & 3) { switch ((buf[1] >> 6) & 3) {
case 0: case 0:
p->u.ofdm.constellation = QPSK; c->modulation = QPSK;
break; break;
case 1: case 1:
p->u.ofdm.constellation = QAM_16; c->modulation = QAM_16;
break; break;
case 2: case 2:
p->u.ofdm.constellation = QAM_64; c->modulation = QAM_64;
break; break;
} }
switch ((buf[0] >> 0) & 3) { switch ((buf[0] >> 0) & 3) {
case 0: case 0:
p->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; c->transmission_mode = TRANSMISSION_MODE_2K;
break; break;
case 1: case 1:
p->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K; c->transmission_mode = TRANSMISSION_MODE_8K;
} }
switch ((buf[0] >> 2) & 3) { switch ((buf[0] >> 2) & 3) {
case 0: case 0:
p->u.ofdm.guard_interval = GUARD_INTERVAL_1_32; c->transmission_mode = GUARD_INTERVAL_1_32;
break; break;
case 1: case 1:
p->u.ofdm.guard_interval = GUARD_INTERVAL_1_16; c->transmission_mode = GUARD_INTERVAL_1_16;
break; break;
case 2: case 2:
p->u.ofdm.guard_interval = GUARD_INTERVAL_1_8; c->transmission_mode = GUARD_INTERVAL_1_8;
break; break;
case 3: case 3:
p->u.ofdm.guard_interval = GUARD_INTERVAL_1_4; c->transmission_mode = GUARD_INTERVAL_1_4;
break; break;
} }
switch ((buf[0] >> 4) & 7) { switch ((buf[0] >> 4) & 7) {
case 0: case 0:
p->u.ofdm.hierarchy_information = HIERARCHY_NONE; c->hierarchy = HIERARCHY_NONE;
break; break;
case 1: case 1:
p->u.ofdm.hierarchy_information = HIERARCHY_1; c->hierarchy = HIERARCHY_1;
break; break;
case 2: case 2:
p->u.ofdm.hierarchy_information = HIERARCHY_2; c->hierarchy = HIERARCHY_2;
break; break;
case 3: case 3:
p->u.ofdm.hierarchy_information = HIERARCHY_4; c->hierarchy = HIERARCHY_4;
break; break;
} }
switch ((buf[2] >> 0) & 7) { switch ((buf[2] >> 0) & 7) {
case 0: case 0:
p->u.ofdm.code_rate_HP = FEC_1_2; c->code_rate_HP = FEC_1_2;
break; break;
case 1: case 1:
p->u.ofdm.code_rate_HP = FEC_2_3; c->code_rate_HP = FEC_2_3;
break; break;
case 2: case 2:
p->u.ofdm.code_rate_HP = FEC_3_4; c->code_rate_HP = FEC_3_4;
break; break;
case 3: case 3:
p->u.ofdm.code_rate_HP = FEC_5_6; c->code_rate_HP = FEC_5_6;
break; break;
case 4: case 4:
p->u.ofdm.code_rate_HP = FEC_7_8; c->code_rate_HP = FEC_7_8;
break; break;
} }
switch ((buf[2] >> 3) & 7) { switch ((buf[2] >> 3) & 7) {
case 0: case 0:
p->u.ofdm.code_rate_LP = FEC_1_2; c->code_rate_LP = FEC_1_2;
break; break;
case 1: case 1:
p->u.ofdm.code_rate_LP = FEC_2_3; c->code_rate_LP = FEC_2_3;
break; break;
case 2: case 2:
p->u.ofdm.code_rate_LP = FEC_3_4; c->code_rate_LP = FEC_3_4;
break; break;
case 3: case 3:
p->u.ofdm.code_rate_LP = FEC_5_6; c->code_rate_LP = FEC_5_6;
break;
case 4:
p->u.ofdm.code_rate_LP = FEC_7_8;
break;
}
switch ((buf[1] >> 2) & 3) {
case 0:
p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
break;
case 1:
p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
break;
case 2:
p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
break;
}
p->inversion = INVERSION_AUTO;
p->frequency = state->frequency;
error:
return ret;
}
static int af9013_update_ber_unc(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
u8 buf[3], i;
u32 error_bit_count = 0;
u32 total_bit_count = 0;
u32 abort_packet_count = 0;
state->ber = 0;
/* check if error bit count is ready */
ret = af9013_read_reg_bits(state, 0xd391, 4, 1, &buf[0]);
if (ret)
goto error;
if (!buf[0])
goto exit;
/* get RSD packet abort count */
for (i = 0; i < 2; i++) {
ret = af9013_read_reg(state, 0xd38a + i, &buf[i]);
if (ret)
goto error;
}
abort_packet_count = (buf[1] << 8) + buf[0];
/* get error bit count */
for (i = 0; i < 3; i++) {
ret = af9013_read_reg(state, 0xd387 + i, &buf[i]);
if (ret)
goto error;
}
error_bit_count = (buf[2] << 16) + (buf[1] << 8) + buf[0];
error_bit_count = error_bit_count - abort_packet_count * 8 * 8;
/* get used RSD counting period (10000 RSD packets used) */
for (i = 0; i < 2; i++) {
ret = af9013_read_reg(state, 0xd385 + i, &buf[i]);
if (ret)
goto error;
}
total_bit_count = (buf[1] << 8) + buf[0];
total_bit_count = total_bit_count - abort_packet_count;
total_bit_count = total_bit_count * 204 * 8;
if (total_bit_count)
state->ber = error_bit_count * 1000000000 / total_bit_count;
state->ucblocks += abort_packet_count;
deb_info("%s: err bits:%d total bits:%d abort count:%d\n", __func__,
error_bit_count, total_bit_count, abort_packet_count);
/* set BER counting range */
ret = af9013_write_reg(state, 0xd385, 10000 & 0xff);
if (ret)
goto error;
ret = af9013_write_reg(state, 0xd386, 10000 >> 8);
if (ret)
goto error;
/* reset and start BER counter */
ret = af9013_write_reg_bits(state, 0xd391, 4, 1, 1);
if (ret)
goto error;
exit:
error:
return ret;
}
static int af9013_update_snr(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
u8 buf[3], i, len;
u32 quant = 0;
struct snr_table *uninitialized_var(snr_table);
/* check if quantizer ready (for snr) */
ret = af9013_read_reg_bits(state, 0xd2e1, 3, 1, &buf[0]);
if (ret)
goto error;
if (buf[0]) {
/* quantizer ready - read it */
for (i = 0; i < 3; i++) {
ret = af9013_read_reg(state, 0xd2e3 + i, &buf[i]);
if (ret)
goto error;
}
quant = (buf[2] << 16) + (buf[1] << 8) + buf[0];
/* read current constellation */
ret = af9013_read_reg(state, 0xd3c1, &buf[0]);
if (ret)
goto error;
switch ((buf[0] >> 6) & 3) {
case 0:
len = ARRAY_SIZE(qpsk_snr_table);
snr_table = qpsk_snr_table;
break;
case 1:
len = ARRAY_SIZE(qam16_snr_table);
snr_table = qam16_snr_table;
break;
case 2:
len = ARRAY_SIZE(qam64_snr_table);
snr_table = qam64_snr_table;
break;
default:
len = 0;
break; break;
} case 4:
c->code_rate_LP = FEC_7_8;
if (len) {
for (i = 0; i < len; i++) {
if (quant < snr_table[i].val) {
state->snr = snr_table[i].snr * 10;
break; break;
} }
}
}
/* set quantizer super frame count */
ret = af9013_write_reg(state, 0xd2e2, 1);
if (ret)
goto error;
/* check quantizer availability */ switch ((buf[1] >> 2) & 3) {
for (i = 0; i < 10; i++) { case 0:
msleep(10); c->bandwidth_hz = 6000000;
ret = af9013_read_reg_bits(state, 0xd2e6, 0, 1, break;
&buf[0]); case 1:
if (ret) c->bandwidth_hz = 7000000;
goto error; break;
if (!buf[0]) case 2:
c->bandwidth_hz = 8000000;
break; break;
} }
/* reset quantizer */
ret = af9013_write_reg_bits(state, 0xd2e1, 3, 1, 1);
if (ret)
goto error;
}
error:
return ret; return ret;
} err:
dbg("%s: failed=%d", __func__, ret);
static int af9013_update_signal_strength(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret = 0;
u8 rf_gain, if_gain;
int signal_strength;
deb_info("%s\n", __func__);
if (state->signal_strength_en) {
ret = af9013_read_reg(state, 0xd07c, &rf_gain);
if (ret)
goto error;
ret = af9013_read_reg(state, 0xd07d, &if_gain);
if (ret)
goto error;
signal_strength = (0xffff / \
(9 * (state->rf_50 + state->if_50) - \
11 * (state->rf_80 + state->if_80))) * \
(10 * (rf_gain + if_gain) - \
11 * (state->rf_80 + state->if_80));
if (signal_strength < 0)
signal_strength = 0;
else if (signal_strength > 0xffff)
signal_strength = 0xffff;
state->signal_strength = signal_strength;
} else {
state->signal_strength = 0;
}
error:
return ret; return ret;
} }
static int af9013_update_statistics(struct dvb_frontend *fe) static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
int ret; int ret;
u8 tmp;
if (time_before(jiffies, state->next_statistics_check)) /*
return 0; * Return status from the cache if it is younger than 2000ms with the
* exception of last tune is done during 4000ms.
/* set minimum statistic update interval */ */
state->next_statistics_check = jiffies + msecs_to_jiffies(1200); if (time_is_after_jiffies(
state->read_status_jiffies + msecs_to_jiffies(2000)) &&
ret = af9013_update_signal_strength(fe); time_is_before_jiffies(
if (ret) state->set_frontend_jiffies + msecs_to_jiffies(4000))
goto error; ) {
ret = af9013_update_snr(fe); *status = state->fe_status;
if (ret)
goto error;
ret = af9013_update_ber_unc(fe);
if (ret)
goto error;
error:
return ret;
}
static int af9013_get_tune_settings(struct dvb_frontend *fe,
struct dvb_frontend_tune_settings *fesettings)
{
fesettings->min_delay_ms = 800;
fesettings->step_size = 0;
fesettings->max_drift = 0;
return 0; return 0;
} } else {
static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status)
{
struct af9013_state *state = fe->demodulator_priv;
int ret = 0;
u8 tmp;
*status = 0; *status = 0;
}
/* MPEG2 lock */ /* MPEG2 lock */
ret = af9013_read_reg_bits(state, 0xd507, 6, 1, &tmp); ret = af9013_rd_reg_bits(state, 0xd507, 6, 1, &tmp);
if (ret) if (ret)
goto error; goto err;
if (tmp) if (tmp)
*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
FE_HAS_SYNC | FE_HAS_LOCK; FE_HAS_SYNC | FE_HAS_LOCK;
if (!*status) { if (!*status) {
/* TPS lock */ /* TPS lock */
ret = af9013_read_reg_bits(state, 0xd330, 3, 1, &tmp); ret = af9013_rd_reg_bits(state, 0xd330, 3, 1, &tmp);
if (ret) if (ret)
goto error; goto err;
if (tmp) if (tmp)
*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
FE_HAS_VITERBI; FE_HAS_VITERBI;
} }
if (!*status) { state->fe_status = *status;
/* CFO lock */ state->read_status_jiffies = jiffies;
ret = af9013_read_reg_bits(state, 0xd333, 7, 1, &tmp);
if (ret)
goto error;
if (tmp)
*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
}
if (!*status) {
/* SFOE lock */
ret = af9013_read_reg_bits(state, 0xd334, 6, 1, &tmp);
if (ret)
goto error;
if (tmp)
*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
}
if (!*status) {
/* AGC lock */
ret = af9013_read_reg_bits(state, 0xd1a0, 6, 1, &tmp);
if (ret)
goto error;
if (tmp)
*status |= FE_HAS_SIGNAL;
}
ret = af9013_update_statistics(fe); return ret;
err:
error: dbg("%s: failed=%d", __func__, ret);
return ret; return ret;
} }
static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
int ret; *snr = state->snr;
ret = af9013_update_statistics(fe); return 0;
*ber = state->ber;
return ret;
} }
static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength) static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
int ret;
ret = af9013_update_statistics(fe);
*strength = state->signal_strength; *strength = state->signal_strength;
return ret; return 0;
} }
static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr) static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
int ret; *ber = state->ber;
ret = af9013_update_statistics(fe); return 0;
*snr = state->snr;
return ret;
} }
static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
int ret;
ret = af9013_update_statistics(fe);
*ucblocks = state->ucblocks; *ucblocks = state->ucblocks;
return ret; return 0;
}
static int af9013_sleep(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
deb_info("%s\n", __func__);
ret = af9013_lock_led(state, 0);
if (ret)
goto error;
ret = af9013_power_ctrl(state, 0);
error:
return ret;
} }
static int af9013_init(struct dvb_frontend *fe) static int af9013_init(struct dvb_frontend *fe)
{ {
struct af9013_state *state = fe->demodulator_priv; struct af9013_state *state = fe->demodulator_priv;
int ret, i, len; int ret, i, len;
u8 tmp0, tmp1; u8 buf[3], tmp;
struct regdesc *init; u32 adc_cw;
deb_info("%s\n", __func__); const struct af9013_reg_bit *init;
/* reset OFDM */ dbg("%s", __func__);
ret = af9013_reset(state, 0);
if (ret)
goto error;
/* power on */ /* power on */
ret = af9013_power_ctrl(state, 1); ret = af9013_power_ctrl(state, 1);
if (ret) if (ret)
goto error; goto err;
/* enable ADC */ /* enable ADC */
ret = af9013_write_reg(state, 0xd73a, 0xa4); ret = af9013_wr_reg(state, 0xd73a, 0xa4);
if (ret) if (ret)
goto error; goto err;
/* write API version to firmware */ /* write API version to firmware */
for (i = 0; i < sizeof(state->config.api_version); i++) { ret = af9013_wr_regs(state, 0x9bf2, state->config.api_version, 4);
ret = af9013_write_reg(state, 0x9bf2 + i,
state->config.api_version[i]);
if (ret) if (ret)
goto error; goto err;
}
/* program ADC control */ /* program ADC control */
ret = af9013_set_adc_ctrl(state); switch (state->config.clock) {
case 28800000: /* 28.800 MHz */
tmp = 0;
break;
case 20480000: /* 20.480 MHz */
tmp = 1;
break;
case 28000000: /* 28.000 MHz */
tmp = 2;
break;
case 25000000: /* 25.000 MHz */
tmp = 3;
break;
default:
err("invalid clock");
return -EINVAL;
}
adc_cw = af913_div(state->config.clock, 1000000ul, 19);
buf[0] = (adc_cw >> 0) & 0xff;
buf[1] = (adc_cw >> 8) & 0xff;
buf[2] = (adc_cw >> 16) & 0xff;
ret = af9013_wr_regs(state, 0xd180, buf, 3);
if (ret)
goto err;
ret = af9013_wr_reg_bits(state, 0x9bd2, 0, 4, tmp);
if (ret) if (ret)
goto error; goto err;
/* set I2C master clock */ /* set I2C master clock */
ret = af9013_write_reg(state, 0xd416, 0x14); ret = af9013_wr_reg(state, 0xd416, 0x14);
if (ret) if (ret)
goto error; goto err;
/* set 16 embx */ /* set 16 embx */
ret = af9013_write_reg_bits(state, 0xd700, 1, 1, 1); ret = af9013_wr_reg_bits(state, 0xd700, 1, 1, 1);
if (ret) if (ret)
goto error; goto err;
/* set no trigger */ /* set no trigger */
ret = af9013_write_reg_bits(state, 0xd700, 2, 1, 0); ret = af9013_wr_reg_bits(state, 0xd700, 2, 1, 0);
if (ret) if (ret)
goto error; goto err;
/* set read-update bit for constellation */ /* set read-update bit for constellation */
ret = af9013_write_reg_bits(state, 0xd371, 1, 1, 1); ret = af9013_wr_reg_bits(state, 0xd371, 1, 1, 1);
if (ret) if (ret)
goto error; goto err;
/* enable FEC monitor */ /* settings for mp2if */
ret = af9013_write_reg_bits(state, 0xd392, 1, 1, 1); if (state->config.ts_mode == AF9013_TS_USB) {
/* AF9015 split PSB to 1.5k + 0.5k */
ret = af9013_wr_reg_bits(state, 0xd50b, 2, 1, 1);
if (ret)
goto err;
} else {
/* AF9013 change the output bit to data7 */
ret = af9013_wr_reg_bits(state, 0xd500, 3, 1, 1);
if (ret)
goto err;
/* AF9013 set mpeg to full speed */
ret = af9013_wr_reg_bits(state, 0xd502, 4, 1, 1);
if (ret)
goto err;
}
ret = af9013_wr_reg_bits(state, 0xd520, 4, 1, 1);
if (ret) if (ret)
goto error; goto err;
/* load OFSM settings */ /* load OFSM settings */
deb_info("%s: load ofsm settings\n", __func__); dbg("%s: load ofsm settings", __func__);
len = ARRAY_SIZE(ofsm_init); len = ARRAY_SIZE(ofsm_init);
init = ofsm_init; init = ofsm_init;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos, ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
init[i].len, init[i].val); init[i].len, init[i].val);
if (ret) if (ret)
goto error; goto err;
} }
/* load tuner specific settings */ /* load tuner specific settings */
deb_info("%s: load tuner specific settings\n", __func__); dbg("%s: load tuner specific settings", __func__);
switch (state->config.tuner) { switch (state->config.tuner) {
case AF9013_TUNER_MXL5003D: case AF9013_TUNER_MXL5003D:
len = ARRAY_SIZE(tuner_init_mxl5003d); len = ARRAY_SIZE(tuner_init_mxl5003d);
...@@ -1237,65 +1199,133 @@ static int af9013_init(struct dvb_frontend *fe) ...@@ -1237,65 +1199,133 @@ static int af9013_init(struct dvb_frontend *fe)
} }
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos, ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
init[i].len, init[i].val); init[i].len, init[i].val);
if (ret) if (ret)
goto error; goto err;
} }
/* set TS mode */ /* TS mode */
deb_info("%s: setting ts mode\n", __func__); ret = af9013_wr_reg_bits(state, 0xd500, 1, 2, state->config.ts_mode);
tmp0 = 0; /* parallel mode */
tmp1 = 0; /* serial mode */
switch (state->config.output_mode) {
case AF9013_OUTPUT_MODE_PARALLEL:
tmp0 = 1;
break;
case AF9013_OUTPUT_MODE_SERIAL:
tmp1 = 1;
break;
case AF9013_OUTPUT_MODE_USB:
/* usb mode for AF9015 */
default:
break;
}
ret = af9013_write_reg_bits(state, 0xd500, 1, 1, tmp0); /* parallel */
if (ret)
goto error;
ret = af9013_write_reg_bits(state, 0xd500, 2, 1, tmp1); /* serial */
if (ret) if (ret)
goto error; goto err;
/* enable lock led */ /* enable lock led */
ret = af9013_lock_led(state, 1); ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 1);
if (ret) if (ret)
goto error; goto err;
/* read values needed for signal strength calculation */ /* check if we support signal strength */
ret = af9013_read_reg_bits(state, 0x9bee, 0, 1, if (!state->signal_strength_en) {
ret = af9013_rd_reg_bits(state, 0x9bee, 0, 1,
&state->signal_strength_en); &state->signal_strength_en);
if (ret) if (ret)
goto error; goto err;
}
if (state->signal_strength_en) { /* read values needed for signal strength calculation */
ret = af9013_read_reg(state, 0x9bbd, &state->rf_50); if (state->signal_strength_en && !state->rf_50) {
ret = af9013_rd_reg(state, 0x9bbd, &state->rf_50);
if (ret) if (ret)
goto error; goto err;
ret = af9013_read_reg(state, 0x9bd0, &state->rf_80);
ret = af9013_rd_reg(state, 0x9bd0, &state->rf_80);
if (ret) if (ret)
goto error; goto err;
ret = af9013_read_reg(state, 0x9be2, &state->if_50);
ret = af9013_rd_reg(state, 0x9be2, &state->if_50);
if (ret) if (ret)
goto error; goto err;
ret = af9013_read_reg(state, 0x9be4, &state->if_80);
ret = af9013_rd_reg(state, 0x9be4, &state->if_80);
if (ret) if (ret)
goto error; goto err;
} }
error: /* SNR */
ret = af9013_wr_reg(state, 0xd2e2, 1);
if (ret)
goto err;
/* BER / UCB */
buf[0] = (10000 >> 0) & 0xff;
buf[1] = (10000 >> 8) & 0xff;
ret = af9013_wr_regs(state, 0xd385, buf, 2);
if (ret)
goto err;
/* enable FEC monitor */
ret = af9013_wr_reg_bits(state, 0xd392, 1, 1, 1);
if (ret)
goto err;
state->first_tune = true;
schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400));
return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret;
}
static int af9013_sleep(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
int ret;
dbg("%s", __func__);
/* stop statistics polling */
cancel_delayed_work_sync(&state->statistics_work);
/* disable lock led */
ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 0);
if (ret)
goto err;
/* power off */
ret = af9013_power_ctrl(state, 0);
if (ret)
goto err;
return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret;
}
static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
{
int ret;
struct af9013_state *state = fe->demodulator_priv;
dbg("%s: enable=%d", __func__, enable);
/* gate already open or close */
if (state->i2c_gate_state == enable)
return 0;
if (state->config.ts_mode == AF9013_TS_USB)
ret = af9013_wr_reg_bits(state, 0xd417, 3, 1, enable);
else
ret = af9013_wr_reg_bits(state, 0xd607, 2, 1, enable);
if (ret)
goto err;
state->i2c_gate_state = enable;
return ret;
err:
dbg("%s: failed=%d", __func__, ret);
return ret; return ret;
} }
static void af9013_release(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
kfree(state);
}
static struct dvb_frontend_ops af9013_ops; static struct dvb_frontend_ops af9013_ops;
static int af9013_download_firmware(struct af9013_state *state) static int af9013_download_firmware(struct af9013_state *state)
...@@ -1309,11 +1339,11 @@ static int af9013_download_firmware(struct af9013_state *state) ...@@ -1309,11 +1339,11 @@ static int af9013_download_firmware(struct af9013_state *state)
msleep(100); msleep(100);
/* check whether firmware is already running */ /* check whether firmware is already running */
ret = af9013_read_reg(state, 0x98be, &val); ret = af9013_rd_reg(state, 0x98be, &val);
if (ret) if (ret)
goto error; goto err;
else else
deb_info("%s: firmware status:%02x\n", __func__, val); dbg("%s: firmware status=%02x", __func__, val);
if (val == 0x0c) /* fw is running, no need for download */ if (val == 0x0c) /* fw is running, no need for download */
goto exit; goto exit;
...@@ -1328,7 +1358,7 @@ static int af9013_download_firmware(struct af9013_state *state) ...@@ -1328,7 +1358,7 @@ static int af9013_download_firmware(struct af9013_state *state)
"Please see linux/Documentation/dvb/ for more details" \ "Please see linux/Documentation/dvb/ for more details" \
" on firmware-problems. (%d)", " on firmware-problems. (%d)",
fw_file, ret); fw_file, ret);
goto error; goto err;
} }
info("downloading firmware from file '%s'", fw_file); info("downloading firmware from file '%s'", fw_file);
...@@ -1346,7 +1376,7 @@ static int af9013_download_firmware(struct af9013_state *state) ...@@ -1346,7 +1376,7 @@ static int af9013_download_firmware(struct af9013_state *state)
ret = af9013_write_ofsm_regs(state, 0x50fc, ret = af9013_write_ofsm_regs(state, 0x50fc,
fw_params, sizeof(fw_params)); fw_params, sizeof(fw_params));
if (ret) if (ret)
goto error_release; goto err_release;
#define FW_ADDR 0x5100 /* firmware start address */ #define FW_ADDR 0x5100 /* firmware start address */
#define LEN_MAX 16 /* max packet size */ #define LEN_MAX 16 /* max packet size */
...@@ -1360,24 +1390,24 @@ static int af9013_download_firmware(struct af9013_state *state) ...@@ -1360,24 +1390,24 @@ static int af9013_download_firmware(struct af9013_state *state)
(u8 *) &fw->data[fw->size - remaining], len); (u8 *) &fw->data[fw->size - remaining], len);
if (ret) { if (ret) {
err("firmware download failed:%d", ret); err("firmware download failed:%d", ret);
goto error_release; goto err_release;
} }
} }
/* request boot firmware */ /* request boot firmware */
ret = af9013_write_reg(state, 0xe205, 1); ret = af9013_wr_reg(state, 0xe205, 1);
if (ret) if (ret)
goto error_release; goto err_release;
for (i = 0; i < 15; i++) { for (i = 0; i < 15; i++) {
msleep(100); msleep(100);
/* check firmware status */ /* check firmware status */
ret = af9013_read_reg(state, 0x98be, &val); ret = af9013_rd_reg(state, 0x98be, &val);
if (ret) if (ret)
goto error_release; goto err_release;
deb_info("%s: firmware status:%02x\n", __func__, val); dbg("%s: firmware status=%02x", __func__, val);
if (val == 0x0c || val == 0x04) /* success or fail */ if (val == 0x0c || val == 0x04) /* success or fail */
break; break;
...@@ -1385,43 +1415,21 @@ static int af9013_download_firmware(struct af9013_state *state) ...@@ -1385,43 +1415,21 @@ static int af9013_download_firmware(struct af9013_state *state)
if (val == 0x04) { if (val == 0x04) {
err("firmware did not run"); err("firmware did not run");
ret = -1; ret = -ENODEV;
} else if (val != 0x0c) { } else if (val != 0x0c) {
err("firmware boot timeout"); err("firmware boot timeout");
ret = -1; ret = -ENODEV;
} }
error_release: err_release:
release_firmware(fw); release_firmware(fw);
error: err:
exit: exit:
if (!ret) if (!ret)
info("found a '%s' in warm state.", af9013_ops.info.name); info("found a '%s' in warm state.", af9013_ops.info.name);
return ret; return ret;
} }
static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
{
int ret;
struct af9013_state *state = fe->demodulator_priv;
deb_info("%s: enable:%d\n", __func__, enable);
if (state->config.output_mode == AF9013_OUTPUT_MODE_USB)
ret = af9013_write_reg_bits(state, 0xd417, 3, 1, enable);
else
ret = af9013_write_reg_bits(state, 0xd607, 2, 1, enable);
return ret;
}
static void af9013_release(struct dvb_frontend *fe)
{
struct af9013_state *state = fe->demodulator_priv;
kfree(state);
}
static struct dvb_frontend_ops af9013_ops;
struct dvb_frontend *af9013_attach(const struct af9013_config *config, struct dvb_frontend *af9013_attach(const struct af9013_config *config,
struct i2c_adapter *i2c) struct i2c_adapter *i2c)
{ {
...@@ -1432,73 +1440,42 @@ struct dvb_frontend *af9013_attach(const struct af9013_config *config, ...@@ -1432,73 +1440,42 @@ struct dvb_frontend *af9013_attach(const struct af9013_config *config,
/* allocate memory for the internal state */ /* allocate memory for the internal state */
state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL); state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL);
if (state == NULL) if (state == NULL)
goto error; goto err;
/* setup the state */ /* setup the state */
state->i2c = i2c; state->i2c = i2c;
memcpy(&state->config, config, sizeof(struct af9013_config)); memcpy(&state->config, config, sizeof(struct af9013_config));
/* download firmware */ /* download firmware */
if (state->config.output_mode != AF9013_OUTPUT_MODE_USB) { if (state->config.ts_mode != AF9013_TS_USB) {
ret = af9013_download_firmware(state); ret = af9013_download_firmware(state);
if (ret) if (ret)
goto error; goto err;
} }
/* firmware version */ /* firmware version */
for (i = 0; i < 4; i++) { ret = af9013_rd_regs(state, 0x5103, buf, 4);
ret = af9013_read_reg(state, 0x5103 + i, &buf[i]);
if (ret)
goto error;
}
info("firmware version:%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]);
/* chip version */
ret = af9013_read_reg_bits(state, 0xd733, 4, 4, &buf[2]);
if (ret)
goto error;
/* ROM version */
for (i = 0; i < 2; i++) {
ret = af9013_read_reg(state, 0x116b + i, &buf[i]);
if (ret) if (ret)
goto error; goto err;
}
deb_info("%s: chip version:%d ROM version:%d.%d\n", __func__,
buf[2], buf[0], buf[1]);
/* settings for mp2if */ info("firmware version %d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]);
if (state->config.output_mode == AF9013_OUTPUT_MODE_USB) {
/* AF9015 split PSB to 1.5k + 0.5k */
ret = af9013_write_reg_bits(state, 0xd50b, 2, 1, 1);
} else {
/* AF9013 change the output bit to data7 */
ret = af9013_write_reg_bits(state, 0xd500, 3, 1, 1);
if (ret)
goto error;
/* AF9013 set mpeg to full speed */
ret = af9013_write_reg_bits(state, 0xd502, 4, 1, 1);
}
if (ret)
goto error;
ret = af9013_write_reg_bits(state, 0xd520, 4, 1, 1);
if (ret)
goto error;
/* set GPIOs */ /* set GPIOs */
for (i = 0; i < sizeof(state->config.gpio); i++) { for (i = 0; i < sizeof(state->config.gpio); i++) {
ret = af9013_set_gpio(state, i, state->config.gpio[i]); ret = af9013_set_gpio(state, i, state->config.gpio[i]);
if (ret) if (ret)
goto error; goto err;
} }
/* create dvb_frontend */ /* create dvb_frontend */
memcpy(&state->frontend.ops, &af9013_ops, memcpy(&state->fe.ops, &af9013_ops,
sizeof(struct dvb_frontend_ops)); sizeof(struct dvb_frontend_ops));
state->frontend.demodulator_priv = state; state->fe.demodulator_priv = state;
INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work);
return &state->frontend; return &state->fe;
error: err:
kfree(state); kfree(state);
return NULL; return NULL;
} }
...@@ -1506,17 +1483,22 @@ EXPORT_SYMBOL(af9013_attach); ...@@ -1506,17 +1483,22 @@ EXPORT_SYMBOL(af9013_attach);
static struct dvb_frontend_ops af9013_ops = { static struct dvb_frontend_ops af9013_ops = {
.info = { .info = {
.name = "Afatech AF9013 DVB-T", .name = "Afatech AF9013",
.type = FE_OFDM, .type = FE_OFDM,
.frequency_min = 174000000, .frequency_min = 174000000,
.frequency_max = 862000000, .frequency_max = 862000000,
.frequency_stepsize = 250000, .frequency_stepsize = 250000,
.frequency_tolerance = 0, .frequency_tolerance = 0,
.caps = .caps = FE_CAN_FEC_1_2 |
FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | FE_CAN_FEC_2_3 |
FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | FE_CAN_FEC_3_4 |
FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_FEC_5_6 |
FE_CAN_QAM_64 | FE_CAN_QAM_AUTO | FE_CAN_FEC_7_8 |
FE_CAN_FEC_AUTO |
FE_CAN_QPSK |
FE_CAN_QAM_16 |
FE_CAN_QAM_64 |
FE_CAN_QAM_AUTO |
FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
FE_CAN_HIERARCHY_AUTO | FE_CAN_HIERARCHY_AUTO |
...@@ -1525,24 +1507,22 @@ static struct dvb_frontend_ops af9013_ops = { ...@@ -1525,24 +1507,22 @@ static struct dvb_frontend_ops af9013_ops = {
}, },
.release = af9013_release, .release = af9013_release,
.init = af9013_init, .init = af9013_init,
.sleep = af9013_sleep, .sleep = af9013_sleep,
.i2c_gate_ctrl = af9013_i2c_gate_ctrl,
.get_tune_settings = af9013_get_tune_settings,
.set_frontend = af9013_set_frontend, .set_frontend = af9013_set_frontend,
.get_frontend = af9013_get_frontend, .get_frontend = af9013_get_frontend,
.get_tune_settings = af9013_get_tune_settings,
.read_status = af9013_read_status, .read_status = af9013_read_status,
.read_ber = af9013_read_ber,
.read_signal_strength = af9013_read_signal_strength,
.read_snr = af9013_read_snr, .read_snr = af9013_read_snr,
.read_signal_strength = af9013_read_signal_strength,
.read_ber = af9013_read_ber,
.read_ucblocks = af9013_read_ucblocks, .read_ucblocks = af9013_read_ucblocks,
};
module_param_named(debug, af9013_debug, int, 0644); .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); };
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver"); MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* Afatech AF9013 demodulator driver * Afatech AF9013 demodulator driver
* *
* Copyright (C) 2007 Antti Palosaari <crope@iki.fi> * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
* Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
* *
* Thanks to Afatech who kindly provided information. * Thanks to Afatech who kindly provided information.
* *
...@@ -21,33 +22,11 @@ ...@@ -21,33 +22,11 @@
* *
*/ */
#ifndef _AF9013_H_ #ifndef AF9013_H
#define _AF9013_H_ #define AF9013_H
#include <linux/dvb/frontend.h> #include <linux/dvb/frontend.h>
enum af9013_ts_mode {
AF9013_OUTPUT_MODE_PARALLEL,
AF9013_OUTPUT_MODE_SERIAL,
AF9013_OUTPUT_MODE_USB, /* only for AF9015 */
};
enum af9013_tuner {
AF9013_TUNER_MXL5003D = 3, /* MaxLinear */
AF9013_TUNER_MXL5005D = 13, /* MaxLinear */
AF9013_TUNER_MXL5005R = 30, /* MaxLinear */
AF9013_TUNER_ENV77H11D5 = 129, /* Panasonic */
AF9013_TUNER_MT2060 = 130, /* Microtune */
AF9013_TUNER_MC44S803 = 133, /* Freescale */
AF9013_TUNER_QT1010 = 134, /* Quantek */
AF9013_TUNER_UNKNOWN = 140, /* for can tuners ? */
AF9013_TUNER_MT2060_2 = 147, /* Microtune */
AF9013_TUNER_TDA18271 = 156, /* NXP */
AF9013_TUNER_QT1010A = 162, /* Quantek */
AF9013_TUNER_MXL5007T = 177, /* MaxLinear */
AF9013_TUNER_TDA18218 = 179, /* NXP */
};
/* AF9013/5 GPIOs (mostly guessed) /* AF9013/5 GPIOs (mostly guessed)
demod#1-gpio#0 - set demod#2 i2c-addr for dual devices demod#1-gpio#0 - set demod#2 i2c-addr for dual devices
demod#1-gpio#1 - xtal setting (?) demod#1-gpio#1 - xtal setting (?)
...@@ -55,44 +34,74 @@ enum af9013_tuner { ...@@ -55,44 +34,74 @@ enum af9013_tuner {
demod#2-gpio#0 - tuner#2 demod#2-gpio#0 - tuner#2
demod#2-gpio#1 - xtal setting (?) demod#2-gpio#1 - xtal setting (?)
*/ */
#define AF9013_GPIO_ON (1 << 0)
#define AF9013_GPIO_EN (1 << 1)
#define AF9013_GPIO_O (1 << 2)
#define AF9013_GPIO_I (1 << 3)
#define AF9013_GPIO_LO (AF9013_GPIO_ON|AF9013_GPIO_EN)
#define AF9013_GPIO_HI (AF9013_GPIO_ON|AF9013_GPIO_EN|AF9013_GPIO_O)
#define AF9013_GPIO_TUNER_ON (AF9013_GPIO_ON|AF9013_GPIO_EN)
#define AF9013_GPIO_TUNER_OFF (AF9013_GPIO_ON|AF9013_GPIO_EN|AF9013_GPIO_O)
struct af9013_config { struct af9013_config {
/* demodulator's I2C address */ /*
u8 demod_address; * I2C address
*/
u8 i2c_addr;
/* frequencies in kHz */ /*
u32 adc_clock; * clock
* 20480000, 25000000, 28000000, 28800000
*/
u32 clock;
/* tuner ID */ /*
* tuner
*/
#define AF9013_TUNER_MXL5003D 3 /* MaxLinear */
#define AF9013_TUNER_MXL5005D 13 /* MaxLinear */
#define AF9013_TUNER_MXL5005R 30 /* MaxLinear */
#define AF9013_TUNER_ENV77H11D5 129 /* Panasonic */
#define AF9013_TUNER_MT2060 130 /* Microtune */
#define AF9013_TUNER_MC44S803 133 /* Freescale */
#define AF9013_TUNER_QT1010 134 /* Quantek */
#define AF9013_TUNER_UNKNOWN 140 /* for can tuners ? */
#define AF9013_TUNER_MT2060_2 147 /* Microtune */
#define AF9013_TUNER_TDA18271 156 /* NXP */
#define AF9013_TUNER_QT1010A 162 /* Quantek */
#define AF9013_TUNER_MXL5007T 177 /* MaxLinear */
#define AF9013_TUNER_TDA18218 179 /* NXP */
u8 tuner; u8 tuner;
/* tuner IF */ /*
u16 tuner_if; * IF frequency
*/
u32 if_frequency;
/* TS data output mode */ /*
u8 output_mode:2; * TS settings
*/
#define AF9013_TS_USB 0
#define AF9013_TS_PARALLEL 1
#define AF9013_TS_SERIAL 2
u8 ts_mode:2;
/* RF spectrum inversion */ /*
u8 rf_spec_inv:1; * input spectrum inversion
*/
bool spec_inv;
/* API version */ /*
* firmware API version
*/
u8 api_version[4]; u8 api_version[4];
/* GPIOs */ /*
* GPIOs
*/
#define AF9013_GPIO_ON (1 << 0)
#define AF9013_GPIO_EN (1 << 1)
#define AF9013_GPIO_O (1 << 2)
#define AF9013_GPIO_I (1 << 3)
#define AF9013_GPIO_LO (AF9013_GPIO_ON|AF9013_GPIO_EN)
#define AF9013_GPIO_HI (AF9013_GPIO_ON|AF9013_GPIO_EN|AF9013_GPIO_O)
#define AF9013_GPIO_TUNER_ON (AF9013_GPIO_ON|AF9013_GPIO_EN)
#define AF9013_GPIO_TUNER_OFF (AF9013_GPIO_ON|AF9013_GPIO_EN|AF9013_GPIO_O)
u8 gpio[4]; u8 gpio[4];
}; };
#if defined(CONFIG_DVB_AF9013) || \ #if defined(CONFIG_DVB_AF9013) || \
(defined(CONFIG_DVB_AF9013_MODULE) && defined(MODULE)) (defined(CONFIG_DVB_AF9013_MODULE) && defined(MODULE))
extern struct dvb_frontend *af9013_attach(const struct af9013_config *config, extern struct dvb_frontend *af9013_attach(const struct af9013_config *config,
...@@ -106,4 +115,4 @@ const struct af9013_config *config, struct i2c_adapter *i2c) ...@@ -106,4 +115,4 @@ const struct af9013_config *config, struct i2c_adapter *i2c)
} }
#endif /* CONFIG_DVB_AF9013 */ #endif /* CONFIG_DVB_AF9013 */
#endif /* _AF9013_H_ */ #endif /* AF9013_H */
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* Afatech AF9013 demodulator driver * Afatech AF9013 demodulator driver
* *
* Copyright (C) 2007 Antti Palosaari <crope@iki.fi> * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
* Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
* *
* Thanks to Afatech who kindly provided information. * Thanks to Afatech who kindly provided information.
* *
...@@ -21,24 +22,19 @@ ...@@ -21,24 +22,19 @@
* *
*/ */
#ifndef _AF9013_PRIV_ #ifndef AF9013_PRIV_H
#define _AF9013_PRIV_ #define AF9013_PRIV_H
#define LOG_PREFIX "af9013" #include "dvb_frontend.h"
extern int af9013_debug; #include "af9013.h"
#include <linux/firmware.h>
#define dprintk(var, level, args...) \
do { if ((var & level)) printk(args); } while (0)
#define debug_dump(b, l, func) {\ #define LOG_PREFIX "af9013"
int loop_; \
for (loop_ = 0; loop_ < l; loop_++) \
func("%02x ", b[loop_]); \
func("\n");\
}
#define deb_info(args...) dprintk(af9013_debug, 0x01, args)
#undef dbg
#define dbg(f, arg...) \
if (af9013_debug) \
printk(KERN_INFO LOG_PREFIX": " f "\n" , ## arg)
#undef err #undef err
#define err(f, arg...) printk(KERN_ERR LOG_PREFIX": " f "\n" , ## arg) #define err(f, arg...) printk(KERN_ERR LOG_PREFIX": " f "\n" , ## arg)
#undef info #undef info
...@@ -48,70 +44,71 @@ extern int af9013_debug; ...@@ -48,70 +44,71 @@ extern int af9013_debug;
#define AF9013_DEFAULT_FIRMWARE "dvb-fe-af9013.fw" #define AF9013_DEFAULT_FIRMWARE "dvb-fe-af9013.fw"
struct regdesc { struct af9013_reg_bit {
u16 addr; u16 addr;
u8 pos:4; u8 pos:4;
u8 len:4; u8 len:4;
u8 val; u8 val;
}; };
struct snr_table { struct af9013_snr {
u32 val; u32 val;
u8 snr; u8 snr;
}; };
struct coeff { struct af9013_coeff {
u32 adc_clock; u32 clock;
fe_bandwidth_t bw; u32 bandwidth_hz;
u8 val[24]; u8 val[24];
}; };
/* pre-calculated coeff lookup table */ /* pre-calculated coeff lookup table */
static struct coeff coeff_table[] = { static const struct af9013_coeff coeff_lut[] = {
/* 28.800 MHz */ /* 28.800 MHz */
{ 28800, BANDWIDTH_8_MHZ, { 0x02, 0x8a, 0x28, 0xa3, 0x05, 0x14, { 28800000, 8000000, { 0x02, 0x8a, 0x28, 0xa3, 0x05, 0x14,
0x51, 0x11, 0x00, 0xa2, 0x8f, 0x3d, 0x00, 0xa2, 0x8a, 0x51, 0x11, 0x00, 0xa2, 0x8f, 0x3d, 0x00, 0xa2, 0x8a,
0x29, 0x00, 0xa2, 0x85, 0x14, 0x01, 0x45, 0x14, 0x14 } }, 0x29, 0x00, 0xa2, 0x85, 0x14, 0x01, 0x45, 0x14, 0x14 } },
{ 28800, BANDWIDTH_7_MHZ, { 0x02, 0x38, 0xe3, 0x8e, 0x04, 0x71, { 28800000, 7000000, { 0x02, 0x38, 0xe3, 0x8e, 0x04, 0x71,
0xc7, 0x07, 0x00, 0x8e, 0x3d, 0x55, 0x00, 0x8e, 0x38, 0xc7, 0x07, 0x00, 0x8e, 0x3d, 0x55, 0x00, 0x8e, 0x38,
0xe4, 0x00, 0x8e, 0x34, 0x72, 0x01, 0x1c, 0x71, 0x32 } }, 0xe4, 0x00, 0x8e, 0x34, 0x72, 0x01, 0x1c, 0x71, 0x32 } },
{ 28800, BANDWIDTH_6_MHZ, { 0x01, 0xe7, 0x9e, 0x7a, 0x03, 0xcf, { 28800000, 6000000, { 0x01, 0xe7, 0x9e, 0x7a, 0x03, 0xcf,
0x3c, 0x3d, 0x00, 0x79, 0xeb, 0x6e, 0x00, 0x79, 0xe7, 0x3c, 0x3d, 0x00, 0x79, 0xeb, 0x6e, 0x00, 0x79, 0xe7,
0x9e, 0x00, 0x79, 0xe3, 0xcf, 0x00, 0xf3, 0xcf, 0x0f } }, 0x9e, 0x00, 0x79, 0xe3, 0xcf, 0x00, 0xf3, 0xcf, 0x0f } },
/* 20.480 MHz */ /* 20.480 MHz */
{ 20480, BANDWIDTH_8_MHZ, { 0x03, 0x92, 0x49, 0x26, 0x07, 0x24, { 20480000, 8000000, { 0x03, 0x92, 0x49, 0x26, 0x07, 0x24,
0x92, 0x13, 0x00, 0xe4, 0x99, 0x6e, 0x00, 0xe4, 0x92, 0x92, 0x13, 0x00, 0xe4, 0x99, 0x6e, 0x00, 0xe4, 0x92,
0x49, 0x00, 0xe4, 0x8b, 0x25, 0x01, 0xc9, 0x24, 0x25 } }, 0x49, 0x00, 0xe4, 0x8b, 0x25, 0x01, 0xc9, 0x24, 0x25 } },
{ 20480, BANDWIDTH_7_MHZ, { 0x03, 0x20, 0x00, 0x01, 0x06, 0x40, { 20480000, 7000000, { 0x03, 0x20, 0x00, 0x01, 0x06, 0x40,
0x00, 0x00, 0x00, 0xc8, 0x06, 0x40, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x06, 0x40, 0x00, 0xc8, 0x00,
0x00, 0x00, 0xc7, 0xf9, 0xc0, 0x01, 0x90, 0x00, 0x00 } }, 0x00, 0x00, 0xc7, 0xf9, 0xc0, 0x01, 0x90, 0x00, 0x00 } },
{ 20480, BANDWIDTH_6_MHZ, { 0x02, 0xad, 0xb6, 0xdc, 0x05, 0x5b, { 20480000, 6000000, { 0x02, 0xad, 0xb6, 0xdc, 0x05, 0x5b,
0x6d, 0x2e, 0x00, 0xab, 0x73, 0x13, 0x00, 0xab, 0x6d, 0x6d, 0x2e, 0x00, 0xab, 0x73, 0x13, 0x00, 0xab, 0x6d,
0xb7, 0x00, 0xab, 0x68, 0x5c, 0x01, 0x56, 0xdb, 0x1c } }, 0xb7, 0x00, 0xab, 0x68, 0x5c, 0x01, 0x56, 0xdb, 0x1c } },
/* 28.000 MHz */ /* 28.000 MHz */
{ 28000, BANDWIDTH_8_MHZ, { 0x02, 0x9c, 0xbc, 0x15, 0x05, 0x39, { 28000000, 8000000, { 0x02, 0x9c, 0xbc, 0x15, 0x05, 0x39,
0x78, 0x0a, 0x00, 0xa7, 0x34, 0x3f, 0x00, 0xa7, 0x2f, 0x78, 0x0a, 0x00, 0xa7, 0x34, 0x3f, 0x00, 0xa7, 0x2f,
0x05, 0x00, 0xa7, 0x29, 0xcc, 0x01, 0x4e, 0x5e, 0x03 } }, 0x05, 0x00, 0xa7, 0x29, 0xcc, 0x01, 0x4e, 0x5e, 0x03 } },
{ 28000, BANDWIDTH_7_MHZ, { 0x02, 0x49, 0x24, 0x92, 0x04, 0x92, { 28000000, 7000000, { 0x02, 0x49, 0x24, 0x92, 0x04, 0x92,
0x49, 0x09, 0x00, 0x92, 0x4d, 0xb7, 0x00, 0x92, 0x49, 0x49, 0x09, 0x00, 0x92, 0x4d, 0xb7, 0x00, 0x92, 0x49,
0x25, 0x00, 0x92, 0x44, 0x92, 0x01, 0x24, 0x92, 0x12 } }, 0x25, 0x00, 0x92, 0x44, 0x92, 0x01, 0x24, 0x92, 0x12 } },
{ 28000, BANDWIDTH_6_MHZ, { 0x01, 0xf5, 0x8d, 0x10, 0x03, 0xeb, { 28000000, 6000000, { 0x01, 0xf5, 0x8d, 0x10, 0x03, 0xeb,
0x1a, 0x08, 0x00, 0x7d, 0x67, 0x2f, 0x00, 0x7d, 0x63, 0x1a, 0x08, 0x00, 0x7d, 0x67, 0x2f, 0x00, 0x7d, 0x63,
0x44, 0x00, 0x7d, 0x5f, 0x59, 0x00, 0xfa, 0xc6, 0x22 } }, 0x44, 0x00, 0x7d, 0x5f, 0x59, 0x00, 0xfa, 0xc6, 0x22 } },
/* 25.000 MHz */ /* 25.000 MHz */
{ 25000, BANDWIDTH_8_MHZ, { 0x02, 0xec, 0xfb, 0x9d, 0x05, 0xd9, { 25000000, 8000000, { 0x02, 0xec, 0xfb, 0x9d, 0x05, 0xd9,
0xf7, 0x0e, 0x00, 0xbb, 0x44, 0xc1, 0x00, 0xbb, 0x3e, 0xf7, 0x0e, 0x00, 0xbb, 0x44, 0xc1, 0x00, 0xbb, 0x3e,
0xe7, 0x00, 0xbb, 0x39, 0x0d, 0x01, 0x76, 0x7d, 0x34 } }, 0xe7, 0x00, 0xbb, 0x39, 0x0d, 0x01, 0x76, 0x7d, 0x34 } },
{ 25000, BANDWIDTH_7_MHZ, { 0x02, 0x8f, 0x5c, 0x29, 0x05, 0x1e, { 25000000, 7000000, { 0x02, 0x8f, 0x5c, 0x29, 0x05, 0x1e,
0xb8, 0x14, 0x00, 0xa3, 0xdc, 0x29, 0x00, 0xa3, 0xd7, 0xb8, 0x14, 0x00, 0xa3, 0xdc, 0x29, 0x00, 0xa3, 0xd7,
0x0a, 0x00, 0xa3, 0xd1, 0xec, 0x01, 0x47, 0xae, 0x05 } }, 0x0a, 0x00, 0xa3, 0xd1, 0xec, 0x01, 0x47, 0xae, 0x05 } },
{ 25000, BANDWIDTH_6_MHZ, { 0x02, 0x31, 0xbc, 0xb5, 0x04, 0x63, { 25000000, 6000000, { 0x02, 0x31, 0xbc, 0xb5, 0x04, 0x63,
0x79, 0x1b, 0x00, 0x8c, 0x73, 0x91, 0x00, 0x8c, 0x6f, 0x79, 0x1b, 0x00, 0x8c, 0x73, 0x91, 0x00, 0x8c, 0x6f,
0x2d, 0x00, 0x8c, 0x6a, 0xca, 0x01, 0x18, 0xde, 0x17 } }, 0x2d, 0x00, 0x8c, 0x6a, 0xca, 0x01, 0x18, 0xde, 0x17 } },
}; };
/* QPSK SNR lookup table */ /* QPSK SNR lookup table */
static struct snr_table qpsk_snr_table[] = { static const struct af9013_snr qpsk_snr_lut[] = {
{ 0x000000, 0 },
{ 0x0b4771, 0 }, { 0x0b4771, 0 },
{ 0x0c1aed, 1 }, { 0x0c1aed, 1 },
{ 0x0d0d27, 2 }, { 0x0d0d27, 2 },
...@@ -131,7 +128,8 @@ static struct snr_table qpsk_snr_table[] = { ...@@ -131,7 +128,8 @@ static struct snr_table qpsk_snr_table[] = {
}; };
/* QAM16 SNR lookup table */ /* QAM16 SNR lookup table */
static struct snr_table qam16_snr_table[] = { static const struct af9013_snr qam16_snr_lut[] = {
{ 0x000000, 0 },
{ 0x05eb62, 5 }, { 0x05eb62, 5 },
{ 0x05fecf, 6 }, { 0x05fecf, 6 },
{ 0x060b80, 7 }, { 0x060b80, 7 },
...@@ -151,7 +149,8 @@ static struct snr_table qam16_snr_table[] = { ...@@ -151,7 +149,8 @@ static struct snr_table qam16_snr_table[] = {
}; };
/* QAM64 SNR lookup table */ /* QAM64 SNR lookup table */
static struct snr_table qam64_snr_table[] = { static const struct af9013_snr qam64_snr_lut[] = {
{ 0x000000, 0 },
{ 0x03109b, 12 }, { 0x03109b, 12 },
{ 0x0310d4, 13 }, { 0x0310d4, 13 },
{ 0x031920, 14 }, { 0x031920, 14 },
...@@ -170,7 +169,7 @@ static struct snr_table qam64_snr_table[] = { ...@@ -170,7 +169,7 @@ static struct snr_table qam64_snr_table[] = {
{ 0xffffff, 27 }, { 0xffffff, 27 },
}; };
static struct regdesc ofsm_init[] = { static const struct af9013_reg_bit ofsm_init[] = {
{ 0xd73a, 0, 8, 0xa1 }, { 0xd73a, 0, 8, 0xa1 },
{ 0xd73b, 0, 8, 0x1f }, { 0xd73b, 0, 8, 0x1f },
{ 0xd73c, 4, 4, 0x0a }, { 0xd73c, 4, 4, 0x0a },
...@@ -252,7 +251,7 @@ static struct regdesc ofsm_init[] = { ...@@ -252,7 +251,7 @@ static struct regdesc ofsm_init[] = {
/* Panasonic ENV77H11D5 tuner init /* Panasonic ENV77H11D5 tuner init
AF9013_TUNER_ENV77H11D5 = 129 */ AF9013_TUNER_ENV77H11D5 = 129 */
static struct regdesc tuner_init_env77h11d5[] = { static const struct af9013_reg_bit tuner_init_env77h11d5[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x03 }, { 0x9bd6, 0, 8, 0x03 },
{ 0x9bbe, 0, 8, 0x01 }, { 0x9bbe, 0, 8, 0x01 },
...@@ -318,7 +317,7 @@ static struct regdesc tuner_init_env77h11d5[] = { ...@@ -318,7 +317,7 @@ static struct regdesc tuner_init_env77h11d5[] = {
/* Microtune MT2060 tuner init /* Microtune MT2060 tuner init
AF9013_TUNER_MT2060 = 130 */ AF9013_TUNER_MT2060 = 130 */
static struct regdesc tuner_init_mt2060[] = { static const struct af9013_reg_bit tuner_init_mt2060[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x07 }, { 0x9bd6, 0, 8, 0x07 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -395,7 +394,7 @@ static struct regdesc tuner_init_mt2060[] = { ...@@ -395,7 +394,7 @@ static struct regdesc tuner_init_mt2060[] = {
/* Microtune MT2060 tuner init /* Microtune MT2060 tuner init
AF9013_TUNER_MT2060_2 = 147 */ AF9013_TUNER_MT2060_2 = 147 */
static struct regdesc tuner_init_mt2060_2[] = { static const struct af9013_reg_bit tuner_init_mt2060_2[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x06 }, { 0x9bd6, 0, 8, 0x06 },
{ 0x9bbe, 0, 8, 0x01 }, { 0x9bbe, 0, 8, 0x01 },
...@@ -462,7 +461,7 @@ static struct regdesc tuner_init_mt2060_2[] = { ...@@ -462,7 +461,7 @@ static struct regdesc tuner_init_mt2060_2[] = {
/* MaxLinear MXL5003 tuner init /* MaxLinear MXL5003 tuner init
AF9013_TUNER_MXL5003D = 3 */ AF9013_TUNER_MXL5003D = 3 */
static struct regdesc tuner_init_mxl5003d[] = { static const struct af9013_reg_bit tuner_init_mxl5003d[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x09 }, { 0x9bd6, 0, 8, 0x09 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -534,7 +533,7 @@ static struct regdesc tuner_init_mxl5003d[] = { ...@@ -534,7 +533,7 @@ static struct regdesc tuner_init_mxl5003d[] = {
AF9013_TUNER_MXL5005D = 13 AF9013_TUNER_MXL5005D = 13
AF9013_TUNER_MXL5005R = 30 AF9013_TUNER_MXL5005R = 30
AF9013_TUNER_MXL5007T = 177 */ AF9013_TUNER_MXL5007T = 177 */
static struct regdesc tuner_init_mxl5005[] = { static const struct af9013_reg_bit tuner_init_mxl5005[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x07 }, { 0x9bd6, 0, 8, 0x07 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -613,7 +612,7 @@ static struct regdesc tuner_init_mxl5005[] = { ...@@ -613,7 +612,7 @@ static struct regdesc tuner_init_mxl5005[] = {
/* Quantek QT1010 tuner init /* Quantek QT1010 tuner init
AF9013_TUNER_QT1010 = 134 AF9013_TUNER_QT1010 = 134
AF9013_TUNER_QT1010A = 162 */ AF9013_TUNER_QT1010A = 162 */
static struct regdesc tuner_init_qt1010[] = { static const struct af9013_reg_bit tuner_init_qt1010[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x09 }, { 0x9bd6, 0, 8, 0x09 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -690,7 +689,7 @@ static struct regdesc tuner_init_qt1010[] = { ...@@ -690,7 +689,7 @@ static struct regdesc tuner_init_qt1010[] = {
/* Freescale MC44S803 tuner init /* Freescale MC44S803 tuner init
AF9013_TUNER_MC44S803 = 133 */ AF9013_TUNER_MC44S803 = 133 */
static struct regdesc tuner_init_mc44s803[] = { static const struct af9013_reg_bit tuner_init_mc44s803[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x06 }, { 0x9bd6, 0, 8, 0x06 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -772,7 +771,7 @@ static struct regdesc tuner_init_mc44s803[] = { ...@@ -772,7 +771,7 @@ static struct regdesc tuner_init_mc44s803[] = {
/* unknown, probably for tin can tuner, tuner init /* unknown, probably for tin can tuner, tuner init
AF9013_TUNER_UNKNOWN = 140 */ AF9013_TUNER_UNKNOWN = 140 */
static struct regdesc tuner_init_unknown[] = { static const struct af9013_reg_bit tuner_init_unknown[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x02 }, { 0x9bd6, 0, 8, 0x02 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -845,7 +844,7 @@ static struct regdesc tuner_init_unknown[] = { ...@@ -845,7 +844,7 @@ static struct regdesc tuner_init_unknown[] = {
/* NXP TDA18271 & TDA18218 tuner init /* NXP TDA18271 & TDA18218 tuner init
AF9013_TUNER_TDA18271 = 156 AF9013_TUNER_TDA18271 = 156
AF9013_TUNER_TDA18218 = 179 */ AF9013_TUNER_TDA18218 = 179 */
static struct regdesc tuner_init_tda18271[] = { static const struct af9013_reg_bit tuner_init_tda18271[] = {
{ 0x9bd5, 0, 8, 0x01 }, { 0x9bd5, 0, 8, 0x01 },
{ 0x9bd6, 0, 8, 0x04 }, { 0x9bd6, 0, 8, 0x04 },
{ 0xd1a0, 1, 1, 0x01 }, { 0xd1a0, 1, 1, 0x01 },
...@@ -920,4 +919,4 @@ static struct regdesc tuner_init_tda18271[] = { ...@@ -920,4 +919,4 @@ static struct regdesc tuner_init_tda18271[] = {
{ 0x9bee, 0, 1, 0x01 }, { 0x9bee, 0, 1, 0x01 },
}; };
#endif /* _AF9013_PRIV_ */ #endif /* AF9013_PRIV_H */
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