1 /* DVB USB compliant Linux driver for the Friio USB2.0 ISDB-T receiver.
3 * Copyright (C) 2009 Akihiro Tsukada <tskd2@yahoo.co.jp>
5 * This module is based off the the gl861 and vp702x modules.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, version 2.
11 * see Documentation/dvb/README.dvb-usb for more information
13 #include <linux/init.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
19 struct jdvbt90502_state {
20 struct i2c_adapter *i2c;
21 struct dvb_frontend frontend;
22 struct jdvbt90502_config config;
25 /* NOTE: TC90502 has 16bit register-address? */
26 /* register 0x0100 is used for reading PLL status, so reg is u16 here */
27 static int jdvbt90502_reg_read(struct jdvbt90502_state *state,
28 const u16 reg, u8 *buf, const size_t count)
32 struct i2c_msg msg[2];
38 msg[0].addr = state->config.demod_address;
41 msg[0].len = sizeof(wbuf);
43 msg[1].addr = msg[0].addr;
44 msg[1].flags = I2C_M_RD;
48 ret = i2c_transfer(state->i2c, msg, 2);
50 deb_fe(" reg read failed.\n");
56 /* currently 16bit register-address is not used, so reg is u8 here */
57 static int jdvbt90502_single_reg_write(struct jdvbt90502_state *state,
58 const u8 reg, const u8 val)
66 msg.addr = state->config.demod_address;
69 msg.len = sizeof(wbuf);
71 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
72 deb_fe(" reg write failed.");
78 static int _jdvbt90502_write(struct dvb_frontend *fe, const u8 buf[], int len)
80 struct jdvbt90502_state *state = fe->demodulator_priv;
82 for (i = 0; i < len - 1; i++) {
83 err = jdvbt90502_single_reg_write(state,
84 buf[0] + i, buf[i + 1]);
92 /* read pll status byte via the demodulator's I2C register */
93 /* note: Win box reads it by 8B block at the I2C addr 0x30 from reg:0x80 */
94 static int jdvbt90502_pll_read(struct jdvbt90502_state *state, u8 *result)
99 u8 pll_addr_byte = (state->config.pll_address << 1) + 1;
103 ret = jdvbt90502_single_reg_write(state, JDVBT90502_2ND_I2C_REG,
108 ret = jdvbt90502_reg_read(state, 0x0100, result, 1);
112 deb_fe("PLL read val:%02x\n", *result);
116 deb_fe("%s:ret == %d\n", __func__, ret);
121 /* set pll frequency via the demodulator's I2C register */
122 static int jdvbt90502_pll_set_freq(struct jdvbt90502_state *state, u32 freq)
129 u8 pll_freq_cmd[PLL_CMD_LEN];
130 u8 pll_agc_cmd[PLL_CMD_LEN];
131 struct i2c_msg msg[2];
134 deb_fe("%s: freq=%d, step=%d\n", __func__, freq,
135 state->frontend.ops.info.frequency_stepsize);
136 /* freq -> oscilator frequency conversion. */
137 /* freq: 473,000,000 + n*6,000,000 [+ 142857 (center freq. shift)] */
138 f = freq / state->frontend.ops.info.frequency_stepsize;
139 /* add 399[1/7 MHZ] = 57MHz for the IF */
141 /* add center frequency shift if necessary */
144 pll_freq_cmd[DEMOD_REDIRECT_REG] = JDVBT90502_2ND_I2C_REG; /* 0xFE */
145 pll_freq_cmd[ADDRESS_BYTE] = state->config.pll_address << 1;
146 pll_freq_cmd[DIVIDER_BYTE1] = (f >> 8) & 0x7F;
147 pll_freq_cmd[DIVIDER_BYTE2] = f & 0xFF;
148 pll_freq_cmd[CONTROL_BYTE] = 0xB2; /* ref.divider:28, 4MHz/28=1/7MHz */
149 pll_freq_cmd[BANDSWITCH_BYTE] = 0x08; /* UHF band */
151 msg[0].addr = state->config.demod_address;
153 msg[0].buf = pll_freq_cmd;
154 msg[0].len = sizeof(pll_freq_cmd);
156 ret = i2c_transfer(state->i2c, &msg[0], 1);
162 pll_agc_cmd[DEMOD_REDIRECT_REG] = pll_freq_cmd[DEMOD_REDIRECT_REG];
163 pll_agc_cmd[ADDRESS_BYTE] = pll_freq_cmd[ADDRESS_BYTE];
164 pll_agc_cmd[DIVIDER_BYTE1] = pll_freq_cmd[DIVIDER_BYTE1];
165 pll_agc_cmd[DIVIDER_BYTE2] = pll_freq_cmd[DIVIDER_BYTE2];
166 pll_agc_cmd[CONTROL_BYTE] = 0x9A; /* AGC_CTRL instead of BANDSWITCH */
167 pll_agc_cmd[AGC_CTRL_BYTE] = 0x50;
168 /* AGC Time Constant 2s, AGC take-over point:103dBuV(lowest) */
170 msg[1].addr = msg[0].addr;
172 msg[1].buf = pll_agc_cmd;
173 msg[1].len = sizeof(pll_agc_cmd);
175 ret = i2c_transfer(state->i2c, &msg[1], 1);
179 /* I don't know what these cmds are for, */
180 /* but the USB log on a windows box contains them */
181 ret = jdvbt90502_single_reg_write(state, 0x01, 0x40);
182 ret |= jdvbt90502_single_reg_write(state, 0x01, 0x00);
187 /* wait for the demod to be ready? */
188 #define RETRY_COUNT 5
189 for (retry = 0; retry < RETRY_COUNT; retry++) {
190 ret = jdvbt90502_reg_read(state, 0x0096, &res1, 1);
193 /* if (res1 != 0x00) goto error; */
194 ret = jdvbt90502_reg_read(state, 0x00B0, res2, sizeof(res2));
201 if (retry >= RETRY_COUNT) {
202 deb_fe("%s: FE does not get ready after freq setting.\n",
209 deb_fe("%s:ret == %d\n", __func__, ret);
213 static int jdvbt90502_read_status(struct dvb_frontend *fe,
214 enum fe_status *state)
219 *state = FE_HAS_SIGNAL;
221 ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
223 deb_fe("%s:ret == %d\n", __func__, ret);
227 *state = FE_HAS_SIGNAL
232 if (result & PLL_STATUS_LOCKED)
233 *state |= FE_HAS_LOCK;
238 static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
246 /* status register (incl. signal strength) : 0x89 */
247 /* TODO: read just the necessary registers [0x8B..0x8D]? */
248 ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
252 deb_fe("%s:ret == %d\n", __func__, ret);
256 /* signal_strength: rbuf[2-4] (24bit BE), use lower 16bit for now. */
257 *strength = (rbuf[3] << 8) + rbuf[4];
264 static int jdvbt90502_set_frontend(struct dvb_frontend *fe)
266 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
269 * NOTE: ignore all the parameters except frequency.
270 * others should be fixed to the proper value for ISDB-T,
271 * but don't check here.
274 struct jdvbt90502_state *state = fe->demodulator_priv;
277 deb_fe("%s: Freq:%d\n", __func__, p->frequency);
279 /* This driver only works on auto mode */
280 p->inversion = INVERSION_AUTO;
281 p->bandwidth_hz = 6000000;
282 p->code_rate_HP = FEC_AUTO;
283 p->code_rate_LP = FEC_AUTO;
284 p->modulation = QAM_64;
285 p->transmission_mode = TRANSMISSION_MODE_AUTO;
286 p->guard_interval = GUARD_INTERVAL_AUTO;
287 p->hierarchy = HIERARCHY_AUTO;
288 p->delivery_system = SYS_ISDBT;
290 ret = jdvbt90502_pll_set_freq(state, p->frequency);
292 deb_fe("%s:ret == %d\n", __func__, ret);
301 * (reg, val) commad list to initialize this module.
302 * captured on a Windows box.
304 static u8 init_code[][2] = {
343 static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);
345 static int jdvbt90502_init(struct dvb_frontend *fe)
351 struct jdvbt90502_state *state = fe->demodulator_priv;
353 deb_fe("%s called.\n", __func__);
355 msg.addr = state->config.demod_address;
358 for (i = 0; i < init_code_len; i++) {
359 msg.buf = init_code[i];
360 ret = i2c_transfer(state->i2c, &msg, 1);
364 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
370 deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
375 static void jdvbt90502_release(struct dvb_frontend *fe)
377 struct jdvbt90502_state *state = fe->demodulator_priv;
382 static struct dvb_frontend_ops jdvbt90502_ops;
384 struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
386 struct jdvbt90502_state *state = NULL;
388 deb_info("%s called.\n", __func__);
390 /* allocate memory for the internal state */
391 state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
395 /* setup the state */
396 state->i2c = &d->i2c_adap;
397 state->config = friio_fe_config;
399 /* create dvb_frontend */
400 state->frontend.ops = jdvbt90502_ops;
401 state->frontend.demodulator_priv = state;
403 if (jdvbt90502_init(&state->frontend) < 0)
406 return &state->frontend;
413 static struct dvb_frontend_ops jdvbt90502_ops = {
414 .delsys = { SYS_ISDBT },
416 .name = "Comtech JDVBT90502 ISDB-T",
417 .frequency_min = 473000000, /* UHF 13ch, center */
418 .frequency_max = 767142857, /* UHF 62ch, center */
419 .frequency_stepsize = JDVBT90502_PLL_CLK / JDVBT90502_PLL_DIVIDER,
420 .frequency_tolerance = 0,
422 /* NOTE: this driver ignores all parameters but frequency. */
423 .caps = FE_CAN_INVERSION_AUTO |
424 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
425 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
426 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
427 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
428 FE_CAN_TRANSMISSION_MODE_AUTO |
429 FE_CAN_GUARD_INTERVAL_AUTO |
430 FE_CAN_HIERARCHY_AUTO,
433 .release = jdvbt90502_release,
435 .init = jdvbt90502_init,
436 .write = _jdvbt90502_write,
438 .set_frontend = jdvbt90502_set_frontend,
440 .read_status = jdvbt90502_read_status,
441 .read_signal_strength = jdvbt90502_read_signal_strength,