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

[media] it913x: refactor code largely

Refactor code largely.

Try to keep order of register read/write same as windows driver does as
it makes comparing sniffs easier.
Signed-off-by: default avatarAntti Palosaari <crope@iki.fi>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent 3d2f18d3
/* /*
* ITE Tech IT9137 silicon tuner driver * ITE IT913X silicon tuner driver
* *
* Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com) * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
* IT9137 Copyright (C) ITE Tech Inc. * IT9137 Copyright (C) ITE Tech Inc.
...@@ -29,79 +29,120 @@ struct it913x_dev { ...@@ -29,79 +29,120 @@ struct it913x_dev {
struct dvb_frontend *fe; struct dvb_frontend *fe;
u8 chip_ver:2; u8 chip_ver:2;
u8 role:2; u8 role:2;
u16 tun_xtal; u16 xtal;
u8 tun_fdiv; u8 fdiv;
u8 tun_clk_mode; u8 clk_mode;
u32 tun_fn_min; u32 fn_min;
bool active;
}; };
static int it913x_init(struct dvb_frontend *fe) static int it913x_init(struct dvb_frontend *fe)
{ {
struct it913x_dev *dev = fe->tuner_priv; struct it913x_dev *dev = fe->tuner_priv;
int ret, i; int ret, i;
unsigned int reg; unsigned int utmp;
u8 val, nv_val; u8 iqik_m_cal, nv_val, buf[2];
u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2}; static const u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
u8 b[2];
ret = regmap_read(dev->regmap, 0x80ec86, &reg); dev_dbg(&dev->client->dev, "role %u\n", dev->role);
switch (reg) {
ret = regmap_write(dev->regmap, 0x80ec4c, 0x68);
if (ret)
goto err;
usleep_range(10000, 100000);
ret = regmap_read(dev->regmap, 0x80ec86, &utmp);
if (ret)
goto err;
switch (utmp) {
case 0: case 0:
dev->tun_clk_mode = reg; /* 12.000 MHz */
dev->tun_xtal = 2000; dev->clk_mode = utmp;
dev->tun_fdiv = 3; dev->xtal = 2000;
val = 16; dev->fdiv = 3;
iqik_m_cal = 16;
break; break;
case 1: case 1:
default: /* I/O error too */ /* 20.480 MHz */
dev->tun_clk_mode = reg; dev->clk_mode = utmp;
dev->tun_xtal = 640; dev->xtal = 640;
dev->tun_fdiv = 1; dev->fdiv = 1;
val = 6; iqik_m_cal = 6;
break; break;
default:
dev_err(&dev->client->dev, "unknown clock identifier %d\n", utmp);
goto err;
} }
ret = regmap_read(dev->regmap, 0x80ed03, &reg); ret = regmap_read(dev->regmap, 0x80ed03, &utmp);
if (ret)
goto err;
if (reg < 0) else if (utmp < ARRAY_SIZE(nv))
return -ENODEV; nv_val = nv[utmp];
else if (reg < ARRAY_SIZE(nv))
nv_val = nv[reg];
else else
nv_val = 2; nv_val = 2;
for (i = 0; i < 50; i++) { for (i = 0; i < 50; i++) {
ret = regmap_bulk_read(dev->regmap, 0x80ed23, &b[0], sizeof(b)); ret = regmap_bulk_read(dev->regmap, 0x80ed23, buf, 2);
reg = (b[1] << 8) + b[0];
if (reg > 0)
break;
if (ret) if (ret)
return -ENODEV; goto err;
utmp = (buf[1] << 8) | (buf[0] << 0);
if (utmp)
break;
udelay(2000); udelay(2000);
} }
dev->tun_fn_min = dev->tun_xtal * reg;
dev->tun_fn_min /= (dev->tun_fdiv * nv_val);
dev_dbg(&dev->client->dev, "Tuner fn_min %d\n", dev->tun_fn_min);
if (dev->chip_ver > 1) dev_dbg(&dev->client->dev, "loop count %d, utmp %d\n", i, utmp);
msleep(50);
else { dev->fn_min = dev->xtal * utmp;
dev->fn_min /= (dev->fdiv * nv_val);
dev->fn_min *= 1000;
dev_dbg(&dev->client->dev, "fn_min %u\n", dev->fn_min);
if (dev->chip_ver == 1) {
for (i = 0; i < 50; i++) { for (i = 0; i < 50; i++) {
ret = regmap_read(dev->regmap, 0x80ec82, &reg); ret = regmap_read(dev->regmap, 0x80ec82, &utmp);
if (ret < 0) if (ret)
return -ENODEV; goto err;
if (reg > 0)
if (utmp)
break; break;
udelay(2000); udelay(2000);
} }
dev_dbg(&dev->client->dev, "loop count %d\n", i);
} else {
msleep(50);
} }
/* Power Up Tuner - common all versions */ ret = regmap_write(dev->regmap, 0x80ed81, iqik_m_cal);
ret = regmap_write(dev->regmap, 0x80ec40, 0x1); if (ret)
ret |= regmap_write(dev->regmap, 0x80ec57, 0x0); goto err;
ret |= regmap_write(dev->regmap, 0x80ec58, 0x0);
return regmap_write(dev->regmap, 0x80ed81, val); ret = regmap_write(dev->regmap, 0x80ec57, 0x00);
if (ret)
goto err;
ret = regmap_write(dev->regmap, 0x80ec58, 0x00);
if (ret)
goto err;
ret = regmap_write(dev->regmap, 0x80ec40, 0x01);
if (ret)
goto err;
dev->active = true;
return 0;
err:
dev_dbg(&dev->client->dev, "failed %d\n", ret);
return ret;
} }
static int it913x_sleep(struct dvb_frontend *fe) static int it913x_sleep(struct dvb_frontend *fe)
...@@ -109,7 +150,9 @@ static int it913x_sleep(struct dvb_frontend *fe) ...@@ -109,7 +150,9 @@ static int it913x_sleep(struct dvb_frontend *fe)
struct it913x_dev *dev = fe->tuner_priv; struct it913x_dev *dev = fe->tuner_priv;
int ret, len; int ret, len;
dev_dbg(&dev->client->dev, "role=%u\n", dev->role); dev_dbg(&dev->client->dev, "role %u\n", dev->role);
dev->active = false;
ret = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1); ret = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1);
if (ret) if (ret)
...@@ -124,7 +167,7 @@ static int it913x_sleep(struct dvb_frontend *fe) ...@@ -124,7 +167,7 @@ static int it913x_sleep(struct dvb_frontend *fe)
else else
len = 15; len = 15;
dev_dbg(&dev->client->dev, "role=%u len=%d\n", dev->role, len); dev_dbg(&dev->client->dev, "role %u, len %d\n", dev->role, len);
ret = regmap_bulk_write(dev->regmap, 0x80ec02, ret = regmap_bulk_write(dev->regmap, 0x80ec02,
"\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
...@@ -160,164 +203,159 @@ static int it913x_sleep(struct dvb_frontend *fe) ...@@ -160,164 +203,159 @@ static int it913x_sleep(struct dvb_frontend *fe)
return ret; return ret;
} }
static int it9137_set_params(struct dvb_frontend *fe) static int it913x_set_params(struct dvb_frontend *fe)
{ {
struct it913x_dev *dev = fe->tuner_priv; struct it913x_dev *dev = fe->tuner_priv;
struct dtv_frontend_properties *p = &fe->dtv_property_cache; struct dtv_frontend_properties *c = &fe->dtv_property_cache;
u32 bandwidth = p->bandwidth_hz;
u32 frequency_m = p->frequency;
int ret; int ret;
unsigned int reg; unsigned int utmp;
u32 frequency = frequency_m / 1000; u32 pre_lo_freq, t_cal_freq;
u32 freq, temp_f, tmp; u16 iqik_m_cal, n_div;
u16 iqik_m_cal; u8 u8tmp, n, l_band, lna_band;
u16 n_div;
u8 n;
u8 l_band;
u8 lna_band;
u8 bw;
dev_dbg(&dev->client->dev, "Tuner Frequency %d Bandwidth %d\n",
frequency, bandwidth);
if (frequency >= 51000 && frequency <= 440000) {
l_band = 0;
lna_band = 0;
} else if (frequency > 440000 && frequency <= 484000) {
l_band = 1;
lna_band = 1;
} else if (frequency > 484000 && frequency <= 533000) {
l_band = 1;
lna_band = 2;
} else if (frequency > 533000 && frequency <= 587000) {
l_band = 1;
lna_band = 3;
} else if (frequency > 587000 && frequency <= 645000) {
l_band = 1;
lna_band = 4;
} else if (frequency > 645000 && frequency <= 710000) {
l_band = 1;
lna_band = 5;
} else if (frequency > 710000 && frequency <= 782000) {
l_band = 1;
lna_band = 6;
} else if (frequency > 782000 && frequency <= 860000) {
l_band = 1;
lna_band = 7;
} else if (frequency > 1450000 && frequency <= 1492000) {
l_band = 1;
lna_band = 0;
} else if (frequency > 1660000 && frequency <= 1685000) {
l_band = 1;
lna_band = 1;
} else
return -EINVAL;
ret = regmap_write(dev->regmap, 0x80ee06, lna_band);
if (ret)
goto err;
switch (bandwidth) {
case 5000000:
bw = 0;
break;
case 6000000:
bw = 2;
break;
case 7000000:
bw = 4;
break;
default:
case 8000000:
bw = 6;
break;
}
ret = regmap_write(dev->regmap, 0x80ec56, bw); dev_dbg(&dev->client->dev, "role=%u, frequency %u, bandwidth_hz %u\n",
if (ret) dev->role, c->frequency, c->bandwidth_hz);
goto err;
ret = regmap_write(dev->regmap, 0x80ec4c, 0xa0 | (l_band << 3)); if (!dev->active) {
if (ret) ret = -EINVAL;
goto err; goto err;
}
if (frequency > 53000 && frequency <= 74000) { if (c->frequency <= 74000000) {
n_div = 48; n_div = 48;
n = 0; n = 0;
} else if (frequency > 74000 && frequency <= 111000) { } else if (c->frequency <= 111000000) {
n_div = 32; n_div = 32;
n = 1; n = 1;
} else if (frequency > 111000 && frequency <= 148000) { } else if (c->frequency <= 148000000) {
n_div = 24; n_div = 24;
n = 2; n = 2;
} else if (frequency > 148000 && frequency <= 222000) { } else if (c->frequency <= 222000000) {
n_div = 16; n_div = 16;
n = 3; n = 3;
} else if (frequency > 222000 && frequency <= 296000) { } else if (c->frequency <= 296000000) {
n_div = 12; n_div = 12;
n = 4; n = 4;
} else if (frequency > 296000 && frequency <= 445000) { } else if (c->frequency <= 445000000) {
n_div = 8; n_div = 8;
n = 5; n = 5;
} else if (frequency > 445000 && frequency <= dev->tun_fn_min) { } else if (c->frequency <= dev->fn_min) {
n_div = 6; n_div = 6;
n = 6; n = 6;
} else if (frequency > dev->tun_fn_min && frequency <= 950000) { } else if (c->frequency <= 950000000) {
n_div = 4; n_div = 4;
n = 7; n = 7;
} else if (frequency > 1450000 && frequency <= 1680000) { } else {
n_div = 2; n_div = 2;
n = 0; n = 0;
} else }
return -EINVAL;
ret = regmap_read(dev->regmap, 0x80ed81, &reg); ret = regmap_read(dev->regmap, 0x80ed81, &utmp);
iqik_m_cal = (u16)reg * n_div; if (ret)
goto err;
if (reg < 0x20) { iqik_m_cal = utmp * n_div;
if (dev->tun_clk_mode == 0)
if (utmp < 0x20) {
if (dev->clk_mode == 0)
iqik_m_cal = (iqik_m_cal * 9) >> 5; iqik_m_cal = (iqik_m_cal * 9) >> 5;
else else
iqik_m_cal >>= 1; iqik_m_cal >>= 1;
} else { } else {
iqik_m_cal = 0x40 - iqik_m_cal; iqik_m_cal = 0x40 - iqik_m_cal;
if (dev->tun_clk_mode == 0) if (dev->clk_mode == 0)
iqik_m_cal = ~((iqik_m_cal * 9) >> 5); iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
else else
iqik_m_cal = ~(iqik_m_cal >> 1); iqik_m_cal = ~(iqik_m_cal >> 1);
} }
temp_f = frequency * (u32)n_div * (u32)dev->tun_fdiv; t_cal_freq = (c->frequency / 1000) * n_div * dev->fdiv;
freq = temp_f / dev->tun_xtal; pre_lo_freq = t_cal_freq / dev->xtal;
tmp = freq * dev->tun_xtal; utmp = pre_lo_freq * dev->xtal;
if ((temp_f - tmp) >= (dev->tun_xtal >> 1)) if ((t_cal_freq - utmp) >= (dev->xtal >> 1))
freq++; pre_lo_freq++;
freq += (u32) n << 13; pre_lo_freq += (u32) n << 13;
/* Frequency OMEGA_IQIK_M_CAL_MID*/ /* Frequency OMEGA_IQIK_M_CAL_MID*/
temp_f = freq + (u32)iqik_m_cal; t_cal_freq = pre_lo_freq + (u32)iqik_m_cal;
dev_dbg(&dev->client->dev, "t_cal_freq %u, pre_lo_freq %u\n",
t_cal_freq, pre_lo_freq);
ret = regmap_write(dev->regmap, 0x80ec4d, temp_f & 0xff); if (c->frequency <= 440000000) {
l_band = 0;
lna_band = 0;
} else if (c->frequency <= 484000000) {
l_band = 1;
lna_band = 1;
} else if (c->frequency <= 533000000) {
l_band = 1;
lna_band = 2;
} else if (c->frequency <= 587000000) {
l_band = 1;
lna_band = 3;
} else if (c->frequency <= 645000000) {
l_band = 1;
lna_band = 4;
} else if (c->frequency <= 710000000) {
l_band = 1;
lna_band = 5;
} else if (c->frequency <= 782000000) {
l_band = 1;
lna_band = 6;
} else if (c->frequency <= 860000000) {
l_band = 1;
lna_band = 7;
} else if (c->frequency <= 1492000000) {
l_band = 1;
lna_band = 0;
} else if (c->frequency <= 1685000000) {
l_band = 1;
lna_band = 1;
} else {
ret = -EINVAL;
goto err;
}
/* XXX: latest windows driver does not set that at all */
ret = regmap_write(dev->regmap, 0x80ee06, lna_band);
if (ret) if (ret)
goto err; goto err;
ret = regmap_write(dev->regmap, 0x80ec4e, (temp_f >> 8) & 0xff); if (c->bandwidth_hz <= 5000000)
u8tmp = 0;
else if (c->bandwidth_hz <= 6000000)
u8tmp = 2;
else if (c->bandwidth_hz <= 7000000)
u8tmp = 4;
else
u8tmp = 6; /* 8000000 */
ret = regmap_write(dev->regmap, 0x80ec56, u8tmp);
if (ret)
goto err;
/* XXX: latest windows driver sets different value (a8 != 68) */
ret = regmap_write(dev->regmap, 0x80ec4c, 0xa0 | (l_band << 3));
if (ret) if (ret)
goto err; goto err;
dev_dbg(&dev->client->dev, "High Frequency = %04x\n", temp_f); ret = regmap_write(dev->regmap, 0x80ec4d, (t_cal_freq >> 0) & 0xff);
if (ret)
goto err;
/* Lower frequency */ ret = regmap_write(dev->regmap, 0x80ec4e, (t_cal_freq >> 8) & 0xff);
ret = regmap_write(dev->regmap, 0x80011e, freq & 0xff);
if (ret) if (ret)
goto err; goto err;
ret = regmap_write(dev->regmap, 0x80011f, (freq >> 8) & 0xff); ret = regmap_write(dev->regmap, 0x80011e, (pre_lo_freq >> 0) & 0xff);
if (ret)
goto err;
ret = regmap_write(dev->regmap, 0x80011f, (pre_lo_freq >> 8) & 0xff);
if (ret) if (ret)
goto err; goto err;
dev_dbg(&dev->client->dev, "low Frequency = %04x\n", freq);
return 0; return 0;
err: err:
dev_dbg(&dev->client->dev, "failed %d\n", ret); dev_dbg(&dev->client->dev, "failed %d\n", ret);
...@@ -326,14 +364,14 @@ static int it9137_set_params(struct dvb_frontend *fe) ...@@ -326,14 +364,14 @@ static int it9137_set_params(struct dvb_frontend *fe)
static const struct dvb_tuner_ops it913x_tuner_ops = { static const struct dvb_tuner_ops it913x_tuner_ops = {
.info = { .info = {
.name = "ITE Tech IT913X", .name = "ITE IT913X",
.frequency_min = 174000000, .frequency_min = 174000000,
.frequency_max = 862000000, .frequency_max = 862000000,
}, },
.init = it913x_init, .init = it913x_init,
.sleep = it913x_sleep, .sleep = it913x_sleep,
.set_params = it9137_set_params, .set_params = it913x_set_params,
}; };
static int it913x_probe(struct i2c_client *client, static int it913x_probe(struct i2c_client *client,
...@@ -366,11 +404,6 @@ static int it913x_probe(struct i2c_client *client, ...@@ -366,11 +404,6 @@ static int it913x_probe(struct i2c_client *client,
goto err_kfree; goto err_kfree;
} }
/* tuner RF initial */
ret = regmap_write(dev->regmap, 0x80ec4c, 0x68);
if (ret)
goto err_regmap_exit;
fe->tuner_priv = dev; fe->tuner_priv = dev;
memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops, memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops,
sizeof(struct dvb_tuner_ops)); sizeof(struct dvb_tuner_ops));
...@@ -385,11 +418,10 @@ static int it913x_probe(struct i2c_client *client, ...@@ -385,11 +418,10 @@ static int it913x_probe(struct i2c_client *client,
dev_info(&dev->client->dev, "ITE IT913X %s successfully attached\n", dev_info(&dev->client->dev, "ITE IT913X %s successfully attached\n",
chip_ver_str); chip_ver_str);
dev_dbg(&dev->client->dev, "chip_ver=%u role=%u\n", dev->chip_ver, dev->role); dev_dbg(&dev->client->dev, "chip_ver %u, role %u\n",
dev->chip_ver, dev->role);
return 0; return 0;
err_regmap_exit:
regmap_exit(dev->regmap);
err_kfree: err_kfree:
kfree(dev); kfree(dev);
err: err:
...@@ -430,6 +462,6 @@ static struct i2c_driver it913x_driver = { ...@@ -430,6 +462,6 @@ static struct i2c_driver it913x_driver = {
module_i2c_driver(it913x_driver); module_i2c_driver(it913x_driver);
MODULE_DESCRIPTION("ITE Tech IT913X silicon tuner driver"); MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
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