1 /*
2  *    Support for OR51211 (pcHDTV HD-2000) - VSB
3  *
4  *    Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
5  *
6  *    Based on code from Jack Kelliher (kelliher@xmission.com)
7  *                           Copyright (C) 2002 & pcHDTV, inc.
8  *
9  *    This program is free software; you can redistribute it and/or modify
10  *    it under the terms of the GNU General Public License as published by
11  *    the Free Software Foundation; either version 2 of the License, or
12  *    (at your option) any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19 */
20 
21 #define pr_fmt(fmt)	KBUILD_MODNAME ": %s: " fmt, __func__
22 
23 /*
24  * This driver needs external firmware. Please use the command
25  * "<kerneldir>/scripts/get_dvb_firmware or51211" to
26  * download/extract it, and then copy it to /usr/lib/hotplug/firmware
27  * or /lib/firmware (depending on configuration of firmware hotplug).
28  */
29 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
30 
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/firmware.h>
35 #include <linux/string.h>
36 #include <linux/slab.h>
37 #include <asm/byteorder.h>
38 
39 #include <media/dvb_math.h>
40 #include <media/dvb_frontend.h>
41 #include "or51211.h"
42 
43 static int debug;
44 #define dprintk(args...) \
45 	do { if (debug) pr_debug(args); } while (0)
46 
47 static u8 run_buf[] = {0x7f,0x01};
48 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
49 
50 struct or51211_state {
51 
52 	struct i2c_adapter* i2c;
53 
54 	/* Configuration settings */
55 	const struct or51211_config* config;
56 
57 	struct dvb_frontend frontend;
58 	struct bt878* bt;
59 
60 	/* Demodulator private data */
61 	u8 initialized:1;
62 	u32 snr; /* Result of last SNR claculation */
63 
64 	/* Tuner private data */
65 	u32 current_frequency;
66 };
67 
68 static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf,
69 			   int len)
70 {
71 	int err;
72 	struct i2c_msg msg;
73 	msg.addr	= reg;
74 	msg.flags	= 0;
75 	msg.len		= len;
76 	msg.buf		= (u8 *)buf;
77 
78 	if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
79 		pr_warn("error (addr %02x, err == %i)\n", reg, err);
80 		return -EREMOTEIO;
81 	}
82 
83 	return 0;
84 }
85 
86 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
87 {
88 	int err;
89 	struct i2c_msg msg;
90 	msg.addr	= reg;
91 	msg.flags	= I2C_M_RD;
92 	msg.len		= len;
93 	msg.buf		= buf;
94 
95 	if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
96 		pr_warn("error (addr %02x, err == %i)\n", reg, err);
97 		return -EREMOTEIO;
98 	}
99 
100 	return 0;
101 }
102 
103 static int or51211_load_firmware (struct dvb_frontend* fe,
104 				  const struct firmware *fw)
105 {
106 	struct or51211_state* state = fe->demodulator_priv;
107 	u8 tudata[585];
108 	int i;
109 
110 	dprintk("Firmware is %zu bytes\n", fw->size);
111 
112 	/* Get eprom data */
113 	tudata[0] = 17;
114 	if (i2c_writebytes(state,0x50,tudata,1)) {
115 		pr_warn("error eprom addr\n");
116 		return -1;
117 	}
118 	if (i2c_readbytes(state,0x50,&tudata[145],192)) {
119 		pr_warn("error eprom\n");
120 		return -1;
121 	}
122 
123 	/* Create firmware buffer */
124 	for (i = 0; i < 145; i++)
125 		tudata[i] = fw->data[i];
126 
127 	for (i = 0; i < 248; i++)
128 		tudata[i+337] = fw->data[145+i];
129 
130 	state->config->reset(fe);
131 
132 	if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
133 		pr_warn("error 1\n");
134 		return -1;
135 	}
136 	msleep(1);
137 
138 	if (i2c_writebytes(state,state->config->demod_address,
139 			   &fw->data[393],8125)) {
140 		pr_warn("error 2\n");
141 		return -1;
142 	}
143 	msleep(1);
144 
145 	if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
146 		pr_warn("error 3\n");
147 		return -1;
148 	}
149 
150 	/* Wait at least 5 msec */
151 	msleep(10);
152 	if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
153 		pr_warn("error 4\n");
154 		return -1;
155 	}
156 	msleep(10);
157 
158 	pr_info("Done.\n");
159 	return 0;
160 };
161 
162 static int or51211_setmode(struct dvb_frontend* fe, int mode)
163 {
164 	struct or51211_state* state = fe->demodulator_priv;
165 	u8 rec_buf[14];
166 
167 	state->config->setmode(fe, mode);
168 
169 	if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
170 		pr_warn("error 1\n");
171 		return -1;
172 	}
173 
174 	/* Wait at least 5 msec */
175 	msleep(10);
176 	if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
177 		pr_warn("error 2\n");
178 		return -1;
179 	}
180 
181 	msleep(10);
182 
183 	/* Set operation mode in Receiver 1 register;
184 	 * type 1:
185 	 * data 0x50h  Automatic sets receiver channel conditions
186 	 *             Automatic NTSC rejection filter
187 	 *             Enable  MPEG serial data output
188 	 *             MPEG2tr
189 	 *             High tuner phase noise
190 	 *             normal +/-150kHz Carrier acquisition range
191 	 */
192 	if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
193 		pr_warn("error 3\n");
194 		return -1;
195 	}
196 
197 	rec_buf[0] = 0x04;
198 	rec_buf[1] = 0x00;
199 	rec_buf[2] = 0x03;
200 	rec_buf[3] = 0x00;
201 	msleep(20);
202 	if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
203 		pr_warn("error 5\n");
204 	}
205 	msleep(3);
206 	if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
207 		pr_warn("error 6\n");
208 		return -1;
209 	}
210 	dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]);
211 
212 	return 0;
213 }
214 
215 static int or51211_set_parameters(struct dvb_frontend *fe)
216 {
217 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
218 	struct or51211_state* state = fe->demodulator_priv;
219 
220 	/* Change only if we are actually changing the channel */
221 	if (state->current_frequency != p->frequency) {
222 		if (fe->ops.tuner_ops.set_params) {
223 			fe->ops.tuner_ops.set_params(fe);
224 			if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
225 		}
226 
227 		/* Set to ATSC mode */
228 		or51211_setmode(fe,0);
229 
230 		/* Update current frequency */
231 		state->current_frequency = p->frequency;
232 	}
233 	return 0;
234 }
235 
236 static int or51211_read_status(struct dvb_frontend *fe, enum fe_status *status)
237 {
238 	struct or51211_state* state = fe->demodulator_priv;
239 	unsigned char rec_buf[2];
240 	unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
241 	*status = 0;
242 
243 	/* Receiver Status */
244 	if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
245 		pr_warn("write error\n");
246 		return -1;
247 	}
248 	msleep(3);
249 	if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
250 		pr_warn("read error\n");
251 		return -1;
252 	}
253 	dprintk("%x %x\n", rec_buf[0], rec_buf[1]);
254 
255 	if (rec_buf[0] &  0x01) { /* Receiver Lock */
256 		*status |= FE_HAS_SIGNAL;
257 		*status |= FE_HAS_CARRIER;
258 		*status |= FE_HAS_VITERBI;
259 		*status |= FE_HAS_SYNC;
260 		*status |= FE_HAS_LOCK;
261 	}
262 	return 0;
263 }
264 
265 /* Calculate SNR estimation (scaled by 2^24)
266 
267    8-VSB SNR equation from Oren datasheets
268 
269    For 8-VSB:
270      SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
271 
272    We re-write the snr equation as:
273      SNR * 2^24 = 10*(c - 2*intlog10(MSE))
274    Where for 8-VSB, c = log10(219037.9454) * 2^24 */
275 
276 static u32 calculate_snr(u32 mse, u32 c)
277 {
278 	if (mse == 0) /* No signal */
279 		return 0;
280 
281 	mse = 2*intlog10(mse);
282 	if (mse > c) {
283 		/* Negative SNR, which is possible, but realisticly the
284 		demod will lose lock before the signal gets this bad.  The
285 		API only allows for unsigned values, so just return 0 */
286 		return 0;
287 	}
288 	return 10*(c - mse);
289 }
290 
291 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
292 {
293 	struct or51211_state* state = fe->demodulator_priv;
294 	u8 rec_buf[2];
295 	u8 snd_buf[3];
296 
297 	/* SNR after Equalizer */
298 	snd_buf[0] = 0x04;
299 	snd_buf[1] = 0x00;
300 	snd_buf[2] = 0x04;
301 
302 	if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
303 		pr_warn("error writing snr reg\n");
304 		return -1;
305 	}
306 	if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
307 		pr_warn("read_status read error\n");
308 		return -1;
309 	}
310 
311 	state->snr = calculate_snr(rec_buf[0], 89599047);
312 	*snr = (state->snr) >> 16;
313 
314 	dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0],
315 		state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
316 
317 	return 0;
318 }
319 
320 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
321 {
322 	/* Calculate Strength from SNR up to 35dB */
323 	/* Even though the SNR can go higher than 35dB, there is some comfort */
324 	/* factor in having a range of strong signals that can show at 100%   */
325 	struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
326 	u16 snr;
327 	int ret;
328 
329 	ret = fe->ops.read_snr(fe, &snr);
330 	if (ret != 0)
331 		return ret;
332 	/* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
333 	/* scale the range 0 - 35*2^24 into 0 - 65535 */
334 	if (state->snr >= 8960 * 0x10000)
335 		*strength = 0xffff;
336 	else
337 		*strength = state->snr / 8960;
338 
339 	return 0;
340 }
341 
342 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
343 {
344 	*ber = -ENOSYS;
345 	return 0;
346 }
347 
348 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
349 {
350 	*ucblocks = -ENOSYS;
351 	return 0;
352 }
353 
354 static int or51211_sleep(struct dvb_frontend* fe)
355 {
356 	return 0;
357 }
358 
359 static int or51211_init(struct dvb_frontend* fe)
360 {
361 	struct or51211_state* state = fe->demodulator_priv;
362 	const struct or51211_config* config = state->config;
363 	const struct firmware* fw;
364 	unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
365 	unsigned char rec_buf[14];
366 	int ret,i;
367 
368 	if (!state->initialized) {
369 		/* Request the firmware, this will block until it uploads */
370 		pr_info("Waiting for firmware upload (%s)...\n",
371 			OR51211_DEFAULT_FIRMWARE);
372 		ret = config->request_firmware(fe, &fw,
373 					       OR51211_DEFAULT_FIRMWARE);
374 		pr_info("Got Hotplug firmware\n");
375 		if (ret) {
376 			pr_warn("No firmware uploaded (timeout or file not found?)\n");
377 			return ret;
378 		}
379 
380 		ret = or51211_load_firmware(fe, fw);
381 		release_firmware(fw);
382 		if (ret) {
383 			pr_warn("Writing firmware to device failed!\n");
384 			return ret;
385 		}
386 		pr_info("Firmware upload complete.\n");
387 
388 		/* Set operation mode in Receiver 1 register;
389 		 * type 1:
390 		 * data 0x50h  Automatic sets receiver channel conditions
391 		 *             Automatic NTSC rejection filter
392 		 *             Enable  MPEG serial data output
393 		 *             MPEG2tr
394 		 *             High tuner phase noise
395 		 *             normal +/-150kHz Carrier acquisition range
396 		 */
397 		if (i2c_writebytes(state,state->config->demod_address,
398 				   cmd_buf,3)) {
399 			pr_warn("Load DVR Error 5\n");
400 			return -1;
401 		}
402 
403 		/* Read back ucode version to besure we loaded correctly */
404 		/* and are really up and running */
405 		rec_buf[0] = 0x04;
406 		rec_buf[1] = 0x00;
407 		rec_buf[2] = 0x03;
408 		rec_buf[3] = 0x00;
409 		msleep(30);
410 		if (i2c_writebytes(state,state->config->demod_address,
411 				   rec_buf,3)) {
412 			pr_warn("Load DVR Error A\n");
413 			return -1;
414 		}
415 		msleep(3);
416 		if (i2c_readbytes(state,state->config->demod_address,
417 				  &rec_buf[10],2)) {
418 			pr_warn("Load DVR Error B\n");
419 			return -1;
420 		}
421 
422 		rec_buf[0] = 0x04;
423 		rec_buf[1] = 0x00;
424 		rec_buf[2] = 0x01;
425 		rec_buf[3] = 0x00;
426 		msleep(20);
427 		if (i2c_writebytes(state,state->config->demod_address,
428 				   rec_buf,3)) {
429 			pr_warn("Load DVR Error C\n");
430 			return -1;
431 		}
432 		msleep(3);
433 		if (i2c_readbytes(state,state->config->demod_address,
434 				  &rec_buf[12],2)) {
435 			pr_warn("Load DVR Error D\n");
436 			return -1;
437 		}
438 
439 		for (i = 0; i < 8; i++)
440 			rec_buf[i]=0xed;
441 
442 		for (i = 0; i < 5; i++) {
443 			msleep(30);
444 			get_ver_buf[4] = i+1;
445 			if (i2c_writebytes(state,state->config->demod_address,
446 					   get_ver_buf,5)) {
447 				pr_warn("Load DVR Error 6 - %d\n", i);
448 				return -1;
449 			}
450 			msleep(3);
451 
452 			if (i2c_readbytes(state,state->config->demod_address,
453 					  &rec_buf[i*2],2)) {
454 				pr_warn("Load DVR Error 7 - %d\n", i);
455 				return -1;
456 			}
457 			/* If we didn't receive the right index, try again */
458 			if ((int)rec_buf[i*2+1]!=i+1){
459 			  i--;
460 			}
461 		}
462 		dprintk("read_fwbits %10ph\n", rec_buf);
463 
464 		pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
465 			rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12],
466 			rec_buf[10]);
467 
468 		rec_buf[0] = 0x04;
469 		rec_buf[1] = 0x00;
470 		rec_buf[2] = 0x03;
471 		rec_buf[3] = 0x00;
472 		msleep(20);
473 		if (i2c_writebytes(state,state->config->demod_address,
474 				   rec_buf,3)) {
475 			pr_warn("Load DVR Error 8\n");
476 			return -1;
477 		}
478 		msleep(20);
479 		if (i2c_readbytes(state,state->config->demod_address,
480 				  &rec_buf[8],2)) {
481 			pr_warn("Load DVR Error 9\n");
482 			return -1;
483 		}
484 		state->initialized = 1;
485 	}
486 
487 	return 0;
488 }
489 
490 static int or51211_get_tune_settings(struct dvb_frontend* fe,
491 				     struct dvb_frontend_tune_settings* fesettings)
492 {
493 	fesettings->min_delay_ms = 500;
494 	fesettings->step_size = 0;
495 	fesettings->max_drift = 0;
496 	return 0;
497 }
498 
499 static void or51211_release(struct dvb_frontend* fe)
500 {
501 	struct or51211_state* state = fe->demodulator_priv;
502 	state->config->sleep(fe);
503 	kfree(state);
504 }
505 
506 static const struct dvb_frontend_ops or51211_ops;
507 
508 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
509 				    struct i2c_adapter* i2c)
510 {
511 	struct or51211_state* state = NULL;
512 
513 	/* Allocate memory for the internal state */
514 	state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
515 	if (state == NULL)
516 		return NULL;
517 
518 	/* Setup the state */
519 	state->config = config;
520 	state->i2c = i2c;
521 	state->initialized = 0;
522 	state->current_frequency = 0;
523 
524 	/* Create dvb_frontend */
525 	memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
526 	state->frontend.demodulator_priv = state;
527 	return &state->frontend;
528 }
529 
530 static const struct dvb_frontend_ops or51211_ops = {
531 	.delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
532 	.info = {
533 		.name                  = "Oren OR51211 VSB Frontend",
534 		.frequency_min_hz      =  44 * MHz,
535 		.frequency_max_hz      = 958 * MHz,
536 		.frequency_stepsize_hz = 166666,
537 		.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
538 			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
539 			FE_CAN_8VSB
540 	},
541 
542 	.release = or51211_release,
543 
544 	.init = or51211_init,
545 	.sleep = or51211_sleep,
546 
547 	.set_frontend = or51211_set_parameters,
548 	.get_tune_settings = or51211_get_tune_settings,
549 
550 	.read_status = or51211_read_status,
551 	.read_ber = or51211_read_ber,
552 	.read_signal_strength = or51211_read_signal_strength,
553 	.read_snr = or51211_read_snr,
554 	.read_ucblocks = or51211_read_ucblocks,
555 };
556 
557 module_param(debug, int, 0644);
558 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
559 
560 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
561 MODULE_AUTHOR("Kirk Lapray");
562 MODULE_LICENSE("GPL");
563 
564 EXPORT_SYMBOL(or51211_attach);
565 
566