aboutsummaryrefslogtreecommitdiff
path: root/drivers/media/dvb/dvb-usb/friio-fe.c
blob: d14bd227b5027b5a0bf0afd4c281e909db12cf38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* DVB USB compliant Linux driver for the Friio USB2.0 ISDB-T receiver.
 *
 * Copyright (C) 2009 Akihiro Tsukada <tskd2@yahoo.co.jp>
 *
 * This module is based off the the gl861 and vp702x modules.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, version 2.
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>

#include "friio.h"

struct jdvbt90502_state {
	struct i2c_adapter *i2c;
	struct dvb_frontend frontend;
	struct jdvbt90502_config config;
};

/* NOTE: TC90502 has 16bit register-address? */
/* register 0x0100 is used for reading PLL status, so reg is u16 here */
static int jdvbt90502_reg_read(struct jdvbt90502_state *state,
			       const u16 reg, u8 *buf, const size_t count)
{
	int ret;
	u8 wbuf[3];
	struct i2c_msg msg[2];

	wbuf[0] = reg & 0xFF;
	wbuf[1] = 0;
	wbuf[2] = reg >> 8;

	msg[0].addr = state->config.demod_address;
	msg[0].flags = 0;
	msg[0].buf = wbuf;
	msg[0].len = sizeof(wbuf);

	msg[1].addr = msg[0].addr;
	msg[1].flags = I2C_M_RD;
	msg[1].buf = buf;
	msg[1].len = count;

	ret = i2c_transfer(state->i2c, msg, 2);
	if (ret != 2) {
		deb_fe(" reg read failed.\n");
		return -EREMOTEIO;
	}
	return 0;
}

/* currently 16bit register-address is not used, so reg is u8 here */
static int jdvbt90502_single_reg_write(struct jdvbt90502_state *state,
				       const u8 reg, const u8 val)
{
	struct i2c_msg msg;
	u8 wbuf[2];

	wbuf[0] = reg;
	wbuf[1] = val;

	msg.addr = state->config.demod_address;
	msg.flags = 0;
	msg.buf = wbuf;
	msg.len = sizeof(wbuf);

	if (i2c_transfer(state->i2c, &msg, 1) != 1) {
		deb_fe(" reg write failed.");
		return -EREMOTEIO;
	}
	return 0;
}

static int _jdvbt90502_write(struct dvb_frontend *fe, u8 *buf, int len)
{
	struct jdvbt90502_state *state = fe->demodulator_priv;
	int err, i;
	for (i = 0; i < len - 1; i++) {
		err = jdvbt90502_single_reg_write(state,
						  buf[0] + i, buf[i + 1]);
		if (err)
			return err;
	}

	return 0;
}

/* read pll status byte via the demodulator's I2C register */
/* note: Win box reads it by 8B block at the I2C addr 0x30 from reg:0x80 */
static int jdvbt90502_pll_read(struct jdvbt90502_state *state, u8 *result)
{
	int ret;

	/* +1 for reading */
	u8 pll_addr_byte = (state->config.pll_address << 1) + 1;

	*result = 0;

	ret = jdvbt90502_single_reg_write(state, JDVBT90502_2ND_I2C_REG,
					  pll_addr_byte);
	if (ret)
		goto error;

	ret = jdvbt90502_reg_read(state, 0x0100, result, 1);
	if (ret)
		goto error;

	deb_fe("PLL read val:%02x\n", *result);
	return 0;

error:
	deb_fe("%s:ret == %d\n", __func__, ret);
	return -EREMOTEIO;
}


/* set pll frequency via the demodulator's I2C register */
static int jdvbt90502_pll_set_freq(struct jdvbt90502_state *state, u32 freq)
{
	int ret;
	int retry;
	u8 res1;
	u8 res2[9];

	u8 pll_freq_cmd[PLL_CMD_LEN];
	u8 pll_agc_cmd[PLL_CMD_LEN];
	struct i2c_msg msg[2];
	u32 f;

	deb_fe("%s: freq=%d, step=%d\n", __func__, freq,
	       state->frontend.ops.info.frequency_stepsize);
	/* freq -> oscilator frequency conversion. */
	/* freq: 473,000,000 + n*6,000,000 [+ 142857 (center freq. shift)] */
	f = freq / state->frontend.ops.info.frequency_stepsize;
	/* add 399[1/7 MHZ] = 57MHz for the IF  */
	f += 399;
	/* add center frequency shift if necessary */
	if (f % 7 == 0)
		f++;
	pll_freq_cmd[DEMOD_REDIRECT_REG] = JDVBT90502_2ND_I2C_REG; /* 0xFE */
	pll_freq_cmd[ADDRESS_BYTE] = state->config.pll_address << 1;
	pll_freq_cmd[DIVIDER_BYTE1] = (f >> 8) & 0x7F;
	pll_freq_cmd[DIVIDER_BYTE2] = f & 0xFF;
	pll_freq_cmd[CONTROL_BYTE] = 0xB2; /* ref.divider:28, 4MHz/28=1/7MHz */
	pll_freq_cmd[BANDSWITCH_BYTE] = 0x08;	/* UHF band */

	msg[0].addr = state->config.demod_address;
	msg[0].flags = 0;
	msg[0].buf = pll_freq_cmd;
	msg[0].len = sizeof(pll_freq_cmd);

	ret = i2c_transfer(state->i2c, &msg[0], 1);
	if (ret != 1)
		goto error;

	udelay(50);

	pll_agc_cmd[DEMOD_REDIRECT_REG] = pll_freq_cmd[DEMOD_REDIRECT_REG];
	pll_agc_cmd[ADDRESS_BYTE] = pll_freq_cmd[ADDRESS_BYTE];
	pll_agc_cmd[DIVIDER_BYTE1] = pll_freq_cmd[DIVIDER_BYTE1];
	pll_agc_cmd[DIVIDER_BYTE2] = pll_freq_cmd[DIVIDER_BYTE2];
	pll_agc_cmd[CONTROL_BYTE] = 0x9A; /*  AGC_CTRL instead of BANDSWITCH */
	pll_agc_cmd[AGC_CTRL_BYTE] = 0x50;
	/* AGC Time Constant 2s, AGC take-over point:103dBuV(lowest) */

	msg[1].addr = msg[0].addr;
	msg[1].flags = 0;
	msg[1].buf = pll_agc_cmd;
	msg[1].len = sizeof(pll_agc_cmd);

	ret = i2c_transfer(state->i2c, &msg[1], 1);
	if (ret != 1)
		goto error;

	/* I don't know what these cmds are for,  */
	/* but the USB log on a windows box contains them */
	ret = jdvbt90502_single_reg_write(state, 0x01, 0x40);
	ret |= jdvbt90502_single_reg_write(state, 0x01, 0x00);
	if (ret)
		goto error;
	udelay(100);

	/* wait for the demod to be ready? */
#define RETRY_COUNT 5
	for (retry = 0; retry < RETRY_COUNT; retry++) {
		ret = jdvbt90502_reg_read(state, 0x0096, &res1, 1);
		if (ret)
			goto error;
		/* if (res1 != 0x00) goto error; */
		ret = jdvbt90502_reg_read(state, 0x00B0, res2, sizeof(res2));
		if (ret)
			goto error;
		if (res2[0] >= 0xA7)
			break;
		msleep(100);
	}
	if (retry >= RETRY_COUNT) {
		deb_fe("%s: FE does not get ready after freq setting.\n",
		       __func__);
		return -EREMOTEIO;
	}

	return 0;
error:
	deb_fe("%s:ret == %d\n", __func__, ret);
	return -EREMOTEIO;
}

static int jdvbt90502_read_status(struct dvb_frontend *fe, fe_status_t *state)
{
	u8 result;
	int ret;

	*state = FE_HAS_SIGNAL;

	ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
	if (ret) {
		deb_fe("%s:ret == %d\n", __func__, ret);
		return -EREMOTEIO;
	}

	*state = FE_HAS_SIGNAL
		| FE_HAS_CARRIER
		| FE_HAS_VITERBI
		| FE_HAS_SYNC;

	if (result & PLL_STATUS_LOCKED)
		*state |= FE_HAS_LOCK;

	return 0;
}

static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
					   u16 *strength)
{
	int ret;
	u8 rbuf[37];

	*strength = 0;

	/* status register (incl. signal strength) : 0x89  */
	/* TODO: read just the necessary registers [0x8B..0x8D]? */
	ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
				  rbuf, sizeof(rbuf));

	if (ret) {
		deb_fe("%s:ret == %d\n", __func__, ret);
		return -EREMOTEIO;
	}

	/* signal_strength: rbuf[2-4] (24bit BE), use lower 16bit for now. */
	*strength = (rbuf[3] << 8) + rbuf[4];
	if (rbuf[2])
		*strength = 0xffff;

	return 0;
}


/* filter out un-supported properties to notify users */
static int jdvbt90502_set_property(struct dvb_frontend *fe,
				   struct dtv_property *tvp)
{
	int r = 0;

	switch (tvp->cmd) {
	case DTV_DELIVERY_SYSTEM:
		if (tvp->u.data != SYS_ISDBT)
			r = -EINVAL;
		break;
	case DTV_CLEAR:
	case DTV_TUNE:
	case DTV_FREQUENCY:
		break;
	default:
		r = -EINVAL;
	}
	return r;
}

static int jdvbt90502_get_frontend(struct dvb_frontend *fe,
				   struct dvb_frontend_parameters *p)
{
	p->inversion = INVERSION_AUTO;
	p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
	p->u.ofdm.code_rate_HP = FEC_AUTO;
	p->u.ofdm.code_rate_LP = FEC_AUTO;
	p->u.ofdm.constellation = QAM_64;
	p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
	p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
	p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
	return 0;
}

static int jdvbt90502_set_frontend(struct dvb_frontend *fe,
				   struct dvb_frontend_parameters *p)
{
	/**
	 * NOTE: ignore all the paramters except frequency.
	 *       others should be fixed to the proper value for ISDB-T,
	 *       but don't check here.
	 */

	struct jdvbt90502_state *state = fe->demodulator_priv;
	int ret;

	deb_fe("%s: Freq:%d\n", __func__, p->frequency);

	/* for recovery from DTV_CLEAN */
	fe->dtv_property_cache.delivery_system = SYS_ISDBT;

	ret = jdvbt90502_pll_set_freq(state, p->frequency);
	if (ret) {
		deb_fe("%s:ret == %d\n", __func__, ret);
		return -EREMOTEIO;
	}

	return 0;
}


/**
 * (reg, val) commad list to initialize this module.
 *  captured on a Windows box.
 */
static u8 init_code[][2] = {
	{0x01, 0x40},
	{0x04, 0x38},
	{0x05, 0x40},
	{0x07, 0x40},
	{0x0F, 0x4F},
	{0x11, 0x21},
	{0x12, 0x0B},
	{0x13, 0x2F},
	{0x14, 0x31},
	{0x16, 0x02},
	{0x21, 0xC4},
	{0x22, 0x20},
	{0x2C, 0x79},
	{0x2D, 0x34},
	{0x2F, 0x00},
	{0x30, 0x28},
	{0x31, 0x31},
	{0x32, 0xDF},
	{0x38, 0x01},
	{0x39, 0x78},
	{0x3B, 0x33},
	{0x3C, 0x33},
	{0x48, 0x90},
	{0x51, 0x68},
	{0x5E, 0x38},
	{0x71, 0x00},
	{0x72, 0x08},
	{0x77, 0x00},
	{0xC0, 0x21},
	{0xC1, 0x10},
	{0xE4, 0x1A},
	{0xEA, 0x1F},
	{0x77, 0x00},
	{0x71, 0x00},
	{0x71, 0x00},
	{0x76, 0x0C},
};

static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);

static int jdvbt90502_init(struct dvb_frontend *fe)
{
	int i = -1;
	int ret;
	struct i2c_msg msg;

	struct jdvbt90502_state *state = fe->demodulator_priv;

	deb_fe("%s called.\n", __func__);

	msg.addr = state->config.demod_address;
	msg.flags = 0;
	msg.len = 2;
	for (i = 0; i < init_code_len; i++) {
		msg.buf = init_code[i];
		ret = i2c_transfer(state->i2c, &msg, 1);
		if (ret != 1)
			goto error;
	}
	fe->dtv_property_cache.delivery_system = SYS_ISDBT;
	msleep(100);

	return 0;

error:
	deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
	return -EREMOTEIO;
}


static void jdvbt90502_release(struct dvb_frontend *fe)
{
	struct jdvbt90502_state *state = fe->demodulator_priv;
	kfree(state);
}


static struct dvb_frontend_ops jdvbt90502_ops;

struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
{
	struct jdvbt90502_state *state = NULL;

	deb_info("%s called.\n", __func__);

	/* allocate memory for the internal state */
	state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
	if (state == NULL)
		goto error;

	/* setup the state */
	state->i2c = &d->i2c_adap;
	memcpy(&state->config, &friio_fe_config, sizeof(friio_fe_config));

	/* create dvb_frontend */
	memcpy(&state->frontend.ops, &jdvbt90502_ops,
	       sizeof(jdvbt90502_ops));
	state->frontend.demodulator_priv = state;

	if (jdvbt90502_init(&state->frontend) < 0)
		goto error;

	return &state->frontend;

error:
	kfree(state);
	return NULL;
}

static struct dvb_frontend_ops jdvbt90502_ops = {

	.info = {
		.name			= "Comtech JDVBT90502 ISDB-T",
		.type			= FE_OFDM,
		.frequency_min		= 473000000, /* UHF 13ch, center */
		.frequency_max		= 767142857, /* UHF 62ch, center */
		.frequency_stepsize	= JDVBT90502_PLL_CLK /
							JDVBT90502_PLL_DIVIDER,
		.frequency_tolerance	= 0,

		/* NOTE: this driver ignores all parameters but frequency. */
		.caps = FE_CAN_INVERSION_AUTO |
			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
			FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
			FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
			FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
			FE_CAN_TRANSMISSION_MODE_AUTO |
			FE_CAN_GUARD_INTERVAL_AUTO |
			FE_CAN_HIERARCHY_AUTO,
	},

	.release = jdvbt90502_release,

	.init = jdvbt90502_init,
	.write = _jdvbt90502_write,

	.set_property = jdvbt90502_set_property,

	.set_frontend = jdvbt90502_set_frontend,
	.get_frontend = jdvbt90502_get_frontend,

	.read_status = jdvbt90502_read_status,
	.read_signal_strength = jdvbt90502_read_signal_strength,
};