• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* DVB USB compliant Linux driver for the Friio USB2.0 ISDB-T receiver.
2  *
3  * Copyright (C) 2009 Akihiro Tsukada <tskd2@yahoo.co.jp>
4  *
5  * This module is based off the the gl861 and vp702x modules.
6  *
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.
10  *
11  * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
12  */
13 #include <linux/init.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 
18 #include "friio.h"
19 
20 struct jdvbt90502_state {
21 	struct i2c_adapter *i2c;
22 	struct dvb_frontend frontend;
23 	struct jdvbt90502_config config;
24 };
25 
26 /* NOTE: TC90502 has 16bit register-address? */
27 /* register 0x0100 is used for reading PLL status, so reg is u16 here */
jdvbt90502_reg_read(struct jdvbt90502_state * state,const u16 reg,u8 * buf,const size_t count)28 static int jdvbt90502_reg_read(struct jdvbt90502_state *state,
29 			       const u16 reg, u8 *buf, const size_t count)
30 {
31 	int ret;
32 	u8 wbuf[3];
33 	struct i2c_msg msg[2];
34 
35 	wbuf[0] = reg & 0xFF;
36 	wbuf[1] = 0;
37 	wbuf[2] = reg >> 8;
38 
39 	msg[0].addr = state->config.demod_address;
40 	msg[0].flags = 0;
41 	msg[0].buf = wbuf;
42 	msg[0].len = sizeof(wbuf);
43 
44 	msg[1].addr = msg[0].addr;
45 	msg[1].flags = I2C_M_RD;
46 	msg[1].buf = buf;
47 	msg[1].len = count;
48 
49 	ret = i2c_transfer(state->i2c, msg, 2);
50 	if (ret != 2) {
51 		deb_fe(" reg read failed.\n");
52 		return -EREMOTEIO;
53 	}
54 	return 0;
55 }
56 
57 /* currently 16bit register-address is not used, so reg is u8 here */
jdvbt90502_single_reg_write(struct jdvbt90502_state * state,const u8 reg,const u8 val)58 static int jdvbt90502_single_reg_write(struct jdvbt90502_state *state,
59 				       const u8 reg, const u8 val)
60 {
61 	struct i2c_msg msg;
62 	u8 wbuf[2];
63 
64 	wbuf[0] = reg;
65 	wbuf[1] = val;
66 
67 	msg.addr = state->config.demod_address;
68 	msg.flags = 0;
69 	msg.buf = wbuf;
70 	msg.len = sizeof(wbuf);
71 
72 	if (i2c_transfer(state->i2c, &msg, 1) != 1) {
73 		deb_fe(" reg write failed.");
74 		return -EREMOTEIO;
75 	}
76 	return 0;
77 }
78 
_jdvbt90502_write(struct dvb_frontend * fe,const u8 buf[],int len)79 static int _jdvbt90502_write(struct dvb_frontend *fe, const u8 buf[], int len)
80 {
81 	struct jdvbt90502_state *state = fe->demodulator_priv;
82 	int err, i;
83 	for (i = 0; i < len - 1; i++) {
84 		err = jdvbt90502_single_reg_write(state,
85 						  buf[0] + i, buf[i + 1]);
86 		if (err)
87 			return err;
88 	}
89 
90 	return 0;
91 }
92 
93 /* read pll status byte via the demodulator's I2C register */
94 /* note: Win box reads it by 8B block at the I2C addr 0x30 from reg:0x80 */
jdvbt90502_pll_read(struct jdvbt90502_state * state,u8 * result)95 static int jdvbt90502_pll_read(struct jdvbt90502_state *state, u8 *result)
96 {
97 	int ret;
98 
99 	/* +1 for reading */
100 	u8 pll_addr_byte = (state->config.pll_address << 1) + 1;
101 
102 	*result = 0;
103 
104 	ret = jdvbt90502_single_reg_write(state, JDVBT90502_2ND_I2C_REG,
105 					  pll_addr_byte);
106 	if (ret)
107 		goto error;
108 
109 	ret = jdvbt90502_reg_read(state, 0x0100, result, 1);
110 	if (ret)
111 		goto error;
112 
113 	deb_fe("PLL read val:%02x\n", *result);
114 	return 0;
115 
116 error:
117 	deb_fe("%s:ret == %d\n", __func__, ret);
118 	return -EREMOTEIO;
119 }
120 
121 
122 /* set pll frequency via the demodulator's I2C register */
jdvbt90502_pll_set_freq(struct jdvbt90502_state * state,u32 freq)123 static int jdvbt90502_pll_set_freq(struct jdvbt90502_state *state, u32 freq)
124 {
125 	int ret;
126 	int retry;
127 	u8 res1;
128 	u8 res2[9];
129 
130 	u8 pll_freq_cmd[PLL_CMD_LEN];
131 	u8 pll_agc_cmd[PLL_CMD_LEN];
132 	struct i2c_msg msg[2];
133 	u32 f;
134 
135 	deb_fe("%s: freq=%d, step=%d\n", __func__, freq,
136 	       state->frontend.ops.info.frequency_stepsize_hz);
137 	/* freq -> oscilator frequency conversion. */
138 	/* freq: 473,000,000 + n*6,000,000 [+ 142857 (center freq. shift)] */
139 	f = freq / state->frontend.ops.info.frequency_stepsize_hz;
140 	/* add 399[1/7 MHZ] = 57MHz for the IF  */
141 	f += 399;
142 	/* add center frequency shift if necessary */
143 	if (f % 7 == 0)
144 		f++;
145 	pll_freq_cmd[DEMOD_REDIRECT_REG] = JDVBT90502_2ND_I2C_REG; /* 0xFE */
146 	pll_freq_cmd[ADDRESS_BYTE] = state->config.pll_address << 1;
147 	pll_freq_cmd[DIVIDER_BYTE1] = (f >> 8) & 0x7F;
148 	pll_freq_cmd[DIVIDER_BYTE2] = f & 0xFF;
149 	pll_freq_cmd[CONTROL_BYTE] = 0xB2; /* ref.divider:28, 4MHz/28=1/7MHz */
150 	pll_freq_cmd[BANDSWITCH_BYTE] = 0x08;	/* UHF band */
151 
152 	msg[0].addr = state->config.demod_address;
153 	msg[0].flags = 0;
154 	msg[0].buf = pll_freq_cmd;
155 	msg[0].len = sizeof(pll_freq_cmd);
156 
157 	ret = i2c_transfer(state->i2c, &msg[0], 1);
158 	if (ret != 1)
159 		goto error;
160 
161 	udelay(50);
162 
163 	pll_agc_cmd[DEMOD_REDIRECT_REG] = pll_freq_cmd[DEMOD_REDIRECT_REG];
164 	pll_agc_cmd[ADDRESS_BYTE] = pll_freq_cmd[ADDRESS_BYTE];
165 	pll_agc_cmd[DIVIDER_BYTE1] = pll_freq_cmd[DIVIDER_BYTE1];
166 	pll_agc_cmd[DIVIDER_BYTE2] = pll_freq_cmd[DIVIDER_BYTE2];
167 	pll_agc_cmd[CONTROL_BYTE] = 0x9A; /*  AGC_CTRL instead of BANDSWITCH */
168 	pll_agc_cmd[AGC_CTRL_BYTE] = 0x50;
169 	/* AGC Time Constant 2s, AGC take-over point:103dBuV(lowest) */
170 
171 	msg[1].addr = msg[0].addr;
172 	msg[1].flags = 0;
173 	msg[1].buf = pll_agc_cmd;
174 	msg[1].len = sizeof(pll_agc_cmd);
175 
176 	ret = i2c_transfer(state->i2c, &msg[1], 1);
177 	if (ret != 1)
178 		goto error;
179 
180 	/* I don't know what these cmds are for,  */
181 	/* but the USB log on a windows box contains them */
182 	ret = jdvbt90502_single_reg_write(state, 0x01, 0x40);
183 	ret |= jdvbt90502_single_reg_write(state, 0x01, 0x00);
184 	if (ret)
185 		goto error;
186 	udelay(100);
187 
188 	/* wait for the demod to be ready? */
189 #define RETRY_COUNT 5
190 	for (retry = 0; retry < RETRY_COUNT; retry++) {
191 		ret = jdvbt90502_reg_read(state, 0x0096, &res1, 1);
192 		if (ret)
193 			goto error;
194 		/* if (res1 != 0x00) goto error; */
195 		ret = jdvbt90502_reg_read(state, 0x00B0, res2, sizeof(res2));
196 		if (ret)
197 			goto error;
198 		if (res2[0] >= 0xA7)
199 			break;
200 		msleep(100);
201 	}
202 	if (retry >= RETRY_COUNT) {
203 		deb_fe("%s: FE does not get ready after freq setting.\n",
204 		       __func__);
205 		return -EREMOTEIO;
206 	}
207 
208 	return 0;
209 error:
210 	deb_fe("%s:ret == %d\n", __func__, ret);
211 	return -EREMOTEIO;
212 }
213 
jdvbt90502_read_status(struct dvb_frontend * fe,enum fe_status * state)214 static int jdvbt90502_read_status(struct dvb_frontend *fe,
215 				  enum fe_status *state)
216 {
217 	u8 result;
218 	int ret;
219 
220 	*state = FE_HAS_SIGNAL;
221 
222 	ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
223 	if (ret) {
224 		deb_fe("%s:ret == %d\n", __func__, ret);
225 		return -EREMOTEIO;
226 	}
227 
228 	*state = FE_HAS_SIGNAL
229 		| FE_HAS_CARRIER
230 		| FE_HAS_VITERBI
231 		| FE_HAS_SYNC;
232 
233 	if (result & PLL_STATUS_LOCKED)
234 		*state |= FE_HAS_LOCK;
235 
236 	return 0;
237 }
238 
jdvbt90502_read_signal_strength(struct dvb_frontend * fe,u16 * strength)239 static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
240 					   u16 *strength)
241 {
242 	int ret;
243 	u8 rbuf[37];
244 
245 	*strength = 0;
246 
247 	/* status register (incl. signal strength) : 0x89  */
248 	/* TODO: read just the necessary registers [0x8B..0x8D]? */
249 	ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
250 				  rbuf, sizeof(rbuf));
251 
252 	if (ret) {
253 		deb_fe("%s:ret == %d\n", __func__, ret);
254 		return -EREMOTEIO;
255 	}
256 
257 	/* signal_strength: rbuf[2-4] (24bit BE), use lower 16bit for now. */
258 	*strength = (rbuf[3] << 8) + rbuf[4];
259 	if (rbuf[2])
260 		*strength = 0xffff;
261 
262 	return 0;
263 }
264 
jdvbt90502_set_frontend(struct dvb_frontend * fe)265 static int jdvbt90502_set_frontend(struct dvb_frontend *fe)
266 {
267 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
268 
269 	/**
270 	 * NOTE: ignore all the parameters except frequency.
271 	 *       others should be fixed to the proper value for ISDB-T,
272 	 *       but don't check here.
273 	 */
274 
275 	struct jdvbt90502_state *state = fe->demodulator_priv;
276 	int ret;
277 
278 	deb_fe("%s: Freq:%d\n", __func__, p->frequency);
279 
280 	/* This driver only works on auto mode */
281 	p->inversion = INVERSION_AUTO;
282 	p->bandwidth_hz = 6000000;
283 	p->code_rate_HP = FEC_AUTO;
284 	p->code_rate_LP = FEC_AUTO;
285 	p->modulation = QAM_64;
286 	p->transmission_mode = TRANSMISSION_MODE_AUTO;
287 	p->guard_interval = GUARD_INTERVAL_AUTO;
288 	p->hierarchy = HIERARCHY_AUTO;
289 	p->delivery_system = SYS_ISDBT;
290 
291 	ret = jdvbt90502_pll_set_freq(state, p->frequency);
292 	if (ret) {
293 		deb_fe("%s:ret == %d\n", __func__, ret);
294 		return -EREMOTEIO;
295 	}
296 
297 	return 0;
298 }
299 
300 
301 /*
302  * (reg, val) commad list to initialize this module.
303  *  captured on a Windows box.
304  */
305 static u8 init_code[][2] = {
306 	{0x01, 0x40},
307 	{0x04, 0x38},
308 	{0x05, 0x40},
309 	{0x07, 0x40},
310 	{0x0F, 0x4F},
311 	{0x11, 0x21},
312 	{0x12, 0x0B},
313 	{0x13, 0x2F},
314 	{0x14, 0x31},
315 	{0x16, 0x02},
316 	{0x21, 0xC4},
317 	{0x22, 0x20},
318 	{0x2C, 0x79},
319 	{0x2D, 0x34},
320 	{0x2F, 0x00},
321 	{0x30, 0x28},
322 	{0x31, 0x31},
323 	{0x32, 0xDF},
324 	{0x38, 0x01},
325 	{0x39, 0x78},
326 	{0x3B, 0x33},
327 	{0x3C, 0x33},
328 	{0x48, 0x90},
329 	{0x51, 0x68},
330 	{0x5E, 0x38},
331 	{0x71, 0x00},
332 	{0x72, 0x08},
333 	{0x77, 0x00},
334 	{0xC0, 0x21},
335 	{0xC1, 0x10},
336 	{0xE4, 0x1A},
337 	{0xEA, 0x1F},
338 	{0x77, 0x00},
339 	{0x71, 0x00},
340 	{0x71, 0x00},
341 	{0x76, 0x0C},
342 };
343 
jdvbt90502_init(struct dvb_frontend * fe)344 static int jdvbt90502_init(struct dvb_frontend *fe)
345 {
346 	int i = -1;
347 	int ret;
348 	struct i2c_msg msg;
349 
350 	struct jdvbt90502_state *state = fe->demodulator_priv;
351 
352 	deb_fe("%s called.\n", __func__);
353 
354 	msg.addr = state->config.demod_address;
355 	msg.flags = 0;
356 	msg.len = 2;
357 	for (i = 0; i < ARRAY_SIZE(init_code); i++) {
358 		msg.buf = init_code[i];
359 		ret = i2c_transfer(state->i2c, &msg, 1);
360 		if (ret != 1)
361 			goto error;
362 	}
363 	fe->dtv_property_cache.delivery_system = SYS_ISDBT;
364 	msleep(100);
365 
366 	return 0;
367 
368 error:
369 	deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
370 	return -EREMOTEIO;
371 }
372 
373 
jdvbt90502_release(struct dvb_frontend * fe)374 static void jdvbt90502_release(struct dvb_frontend *fe)
375 {
376 	struct jdvbt90502_state *state = fe->demodulator_priv;
377 	kfree(state);
378 }
379 
380 
381 static const struct dvb_frontend_ops jdvbt90502_ops;
382 
jdvbt90502_attach(struct dvb_usb_device * d)383 struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
384 {
385 	struct jdvbt90502_state *state = NULL;
386 
387 	deb_info("%s called.\n", __func__);
388 
389 	/* allocate memory for the internal state */
390 	state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
391 	if (state == NULL)
392 		goto error;
393 
394 	/* setup the state */
395 	state->i2c = &d->i2c_adap;
396 	state->config = friio_fe_config;
397 
398 	/* create dvb_frontend */
399 	state->frontend.ops = jdvbt90502_ops;
400 	state->frontend.demodulator_priv = state;
401 
402 	if (jdvbt90502_init(&state->frontend) < 0)
403 		goto error;
404 
405 	return &state->frontend;
406 
407 error:
408 	kfree(state);
409 	return NULL;
410 }
411 
412 static const struct dvb_frontend_ops jdvbt90502_ops = {
413 	.delsys = { SYS_ISDBT },
414 	.info = {
415 		.name			= "Comtech JDVBT90502 ISDB-T",
416 		.frequency_min_hz	= 473000000, /* UHF 13ch, center */
417 		.frequency_max_hz	= 767142857, /* UHF 62ch, center */
418 		.frequency_stepsize_hz	= JDVBT90502_PLL_CLK / JDVBT90502_PLL_DIVIDER,
419 
420 		/* NOTE: this driver ignores all parameters but frequency. */
421 		.caps = FE_CAN_INVERSION_AUTO |
422 			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
423 			FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
424 			FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
425 			FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
426 			FE_CAN_TRANSMISSION_MODE_AUTO |
427 			FE_CAN_GUARD_INTERVAL_AUTO |
428 			FE_CAN_HIERARCHY_AUTO,
429 	},
430 
431 	.release = jdvbt90502_release,
432 
433 	.init = jdvbt90502_init,
434 	.write = _jdvbt90502_write,
435 
436 	.set_frontend = jdvbt90502_set_frontend,
437 
438 	.read_status = jdvbt90502_read_status,
439 	.read_signal_strength = jdvbt90502_read_signal_strength,
440 };
441