1/*
2    Conexant cx22700 DVB OFDM demodulator driver
3
4    Copyright (C) 2001-2002 Convergence Integrated Media GmbH
5	Holger Waechtler <holger@convergence.de>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21*/
22
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include "dvb_frontend.h"
29#include "cx22700.h"
30
31
32struct cx22700_state {
33
34	struct i2c_adapter* i2c;
35
36	const struct cx22700_config* config;
37
38	struct dvb_frontend frontend;
39};
40
41
42static int debug;
43#define dprintk(args...) \
44	do { \
45		if (debug) printk(KERN_DEBUG "cx22700: " args); \
46	} while (0)
47
48static u8 init_tab [] = {
49	0x04, 0x10,
50	0x05, 0x09,
51	0x06, 0x00,
52	0x08, 0x04,
53	0x09, 0x00,
54	0x0a, 0x01,
55	0x15, 0x40,
56	0x16, 0x10,
57	0x17, 0x87,
58	0x18, 0x17,
59	0x1a, 0x10,
60	0x25, 0x04,
61	0x2e, 0x00,
62	0x39, 0x00,
63	0x3a, 0x04,
64	0x45, 0x08,
65	0x46, 0x02,
66	0x47, 0x05,
67};
68
69
70static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data)
71{
72	int ret;
73	u8 buf [] = { reg, data };
74	struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
75
76	dprintk ("%s\n", __func__);
77
78	ret = i2c_transfer (state->i2c, &msg, 1);
79
80	if (ret != 1)
81		printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
82			__func__, reg, data, ret);
83
84	return (ret != 1) ? -1 : 0;
85}
86
87static int cx22700_readreg (struct cx22700_state* state, u8 reg)
88{
89	int ret;
90	u8 b0 [] = { reg };
91	u8 b1 [] = { 0 };
92	struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
93			   { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
94
95	dprintk ("%s\n", __func__);
96
97	ret = i2c_transfer (state->i2c, msg, 2);
98
99	if (ret != 2) return -EIO;
100
101	return b1[0];
102}
103
104static int cx22700_set_inversion (struct cx22700_state* state, int inversion)
105{
106	u8 val;
107
108	dprintk ("%s\n", __func__);
109
110	switch (inversion) {
111	case INVERSION_AUTO:
112		return -EOPNOTSUPP;
113	case INVERSION_ON:
114		val = cx22700_readreg (state, 0x09);
115		return cx22700_writereg (state, 0x09, val | 0x01);
116	case INVERSION_OFF:
117		val = cx22700_readreg (state, 0x09);
118		return cx22700_writereg (state, 0x09, val & 0xfe);
119	default:
120		return -EINVAL;
121	}
122}
123
124static int cx22700_set_tps(struct cx22700_state *state,
125			   struct dtv_frontend_properties *p)
126{
127	static const u8 qam_tab [4] = { 0, 1, 0, 2 };
128	static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };
129	u8 val;
130
131	dprintk ("%s\n", __func__);
132
133	if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)
134		return -EINVAL;
135
136	if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)
137		return -EINVAL;
138
139	if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)
140		return -EINVAL;
141
142	if ((int)p->guard_interval < GUARD_INTERVAL_1_32 ||
143	    p->guard_interval > GUARD_INTERVAL_1_4)
144		return -EINVAL;
145
146	if (p->transmission_mode != TRANSMISSION_MODE_2K &&
147	    p->transmission_mode != TRANSMISSION_MODE_8K)
148		return -EINVAL;
149
150	if (p->modulation != QPSK &&
151	    p->modulation != QAM_16 &&
152	    p->modulation != QAM_64)
153		return -EINVAL;
154
155	if ((int)p->hierarchy < HIERARCHY_NONE ||
156	    p->hierarchy > HIERARCHY_4)
157		return -EINVAL;
158
159	if (p->bandwidth_hz > 8000000 || p->bandwidth_hz < 6000000)
160		return -EINVAL;
161
162	if (p->bandwidth_hz == 7000000)
163		cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10));
164	else
165		cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10));
166
167	val = qam_tab[p->modulation - QPSK];
168	val |= p->hierarchy - HIERARCHY_NONE;
169
170	cx22700_writereg (state, 0x04, val);
171
172	if (p->code_rate_HP - FEC_1_2 >= sizeof(fec_tab) ||
173	    p->code_rate_LP - FEC_1_2 >= sizeof(fec_tab))
174		return -EINVAL;
175	val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;
176	val |= fec_tab[p->code_rate_LP - FEC_1_2];
177
178	cx22700_writereg (state, 0x05, val);
179
180	val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;
181	val |= p->transmission_mode - TRANSMISSION_MODE_2K;
182
183	cx22700_writereg (state, 0x06, val);
184
185	cx22700_writereg (state, 0x08, 0x04 | 0x02);  /* use user tps parameters */
186	cx22700_writereg (state, 0x08, 0x04);         /* restart acquisition */
187
188	return 0;
189}
190
191static int cx22700_get_tps(struct cx22700_state *state,
192			   struct dtv_frontend_properties *p)
193{
194	static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 };
195	static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4,
196						    FEC_5_6, FEC_7_8 };
197	u8 val;
198
199	dprintk ("%s\n", __func__);
200
201	if (!(cx22700_readreg(state, 0x07) & 0x20))  /*  tps valid? */
202		return -EAGAIN;
203
204	val = cx22700_readreg (state, 0x01);
205
206	if ((val & 0x7) > 4)
207		p->hierarchy = HIERARCHY_AUTO;
208	else
209		p->hierarchy = HIERARCHY_NONE + (val & 0x7);
210
211	if (((val >> 3) & 0x3) > 2)
212		p->modulation = QAM_AUTO;
213	else
214		p->modulation = qam_tab[(val >> 3) & 0x3];
215
216	val = cx22700_readreg (state, 0x02);
217
218	if (((val >> 3) & 0x07) > 4)
219		p->code_rate_HP = FEC_AUTO;
220	else
221		p->code_rate_HP = fec_tab[(val >> 3) & 0x07];
222
223	if ((val & 0x07) > 4)
224		p->code_rate_LP = FEC_AUTO;
225	else
226		p->code_rate_LP = fec_tab[val & 0x07];
227
228	val = cx22700_readreg (state, 0x03);
229
230	p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);
231	p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);
232
233	return 0;
234}
235
236static int cx22700_init (struct dvb_frontend* fe)
237
238{	struct cx22700_state* state = fe->demodulator_priv;
239	int i;
240
241	dprintk("cx22700_init: init chip\n");
242
243	cx22700_writereg (state, 0x00, 0x02);   /*  soft reset */
244	cx22700_writereg (state, 0x00, 0x00);
245
246	msleep(10);
247
248	for (i=0; i<sizeof(init_tab); i+=2)
249		cx22700_writereg (state, init_tab[i], init_tab[i+1]);
250
251	cx22700_writereg (state, 0x00, 0x01);
252
253	return 0;
254}
255
256static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status)
257{
258	struct cx22700_state* state = fe->demodulator_priv;
259
260	u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
261		   | (cx22700_readreg (state, 0x0e) << 1);
262	u8 sync = cx22700_readreg (state, 0x07);
263
264	*status = 0;
265
266	if (rs_ber < 0xff00)
267		*status |= FE_HAS_SIGNAL;
268
269	if (sync & 0x20)
270		*status |= FE_HAS_CARRIER;
271
272	if (sync & 0x10)
273		*status |= FE_HAS_VITERBI;
274
275	if (sync & 0x10)
276		*status |= FE_HAS_SYNC;
277
278	if (*status == 0x0f)
279		*status |= FE_HAS_LOCK;
280
281	return 0;
282}
283
284static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber)
285{
286	struct cx22700_state* state = fe->demodulator_priv;
287
288	*ber = cx22700_readreg (state, 0x0c) & 0x7f;
289	cx22700_writereg (state, 0x0c, 0x00);
290
291	return 0;
292}
293
294static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
295{
296	struct cx22700_state* state = fe->demodulator_priv;
297
298	u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
299		   | (cx22700_readreg (state, 0x0e) << 1);
300	*signal_strength = ~rs_ber;
301
302	return 0;
303}
304
305static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr)
306{
307	struct cx22700_state* state = fe->demodulator_priv;
308
309	u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
310		   | (cx22700_readreg (state, 0x0e) << 1);
311	*snr = ~rs_ber;
312
313	return 0;
314}
315
316static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
317{
318	struct cx22700_state* state = fe->demodulator_priv;
319
320	*ucblocks = cx22700_readreg (state, 0x0f);
321	cx22700_writereg (state, 0x0f, 0x00);
322
323	return 0;
324}
325
326static int cx22700_set_frontend(struct dvb_frontend *fe)
327{
328	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
329	struct cx22700_state* state = fe->demodulator_priv;
330
331	cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/
332	cx22700_writereg (state, 0x00, 0x00);
333
334	if (fe->ops.tuner_ops.set_params) {
335		fe->ops.tuner_ops.set_params(fe);
336		if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
337	}
338
339	cx22700_set_inversion(state, c->inversion);
340	cx22700_set_tps(state, c);
341	cx22700_writereg (state, 0x37, 0x01);  /* PAL loop filter off */
342	cx22700_writereg (state, 0x00, 0x01);  /* restart acquire */
343
344	return 0;
345}
346
347static int cx22700_get_frontend(struct dvb_frontend *fe)
348{
349	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
350	struct cx22700_state* state = fe->demodulator_priv;
351	u8 reg09 = cx22700_readreg (state, 0x09);
352
353	c->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;
354	return cx22700_get_tps(state, c);
355}
356
357static int cx22700_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
358{
359	struct cx22700_state* state = fe->demodulator_priv;
360
361	if (enable) {
362		return cx22700_writereg(state, 0x0a, 0x00);
363	} else {
364		return cx22700_writereg(state, 0x0a, 0x01);
365	}
366}
367
368static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
369{
370	fesettings->min_delay_ms = 150;
371	fesettings->step_size = 166667;
372	fesettings->max_drift = 166667*2;
373	return 0;
374}
375
376static void cx22700_release(struct dvb_frontend* fe)
377{
378	struct cx22700_state* state = fe->demodulator_priv;
379	kfree(state);
380}
381
382static struct dvb_frontend_ops cx22700_ops;
383
384struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,
385				    struct i2c_adapter* i2c)
386{
387	struct cx22700_state* state = NULL;
388
389	/* allocate memory for the internal state */
390	state = kzalloc(sizeof(struct cx22700_state), GFP_KERNEL);
391	if (state == NULL) goto error;
392
393	/* setup the state */
394	state->config = config;
395	state->i2c = i2c;
396
397	/* check if the demod is there */
398	if (cx22700_readreg(state, 0x07) < 0) goto error;
399
400	/* create dvb_frontend */
401	memcpy(&state->frontend.ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));
402	state->frontend.demodulator_priv = state;
403	return &state->frontend;
404
405error:
406	kfree(state);
407	return NULL;
408}
409
410static struct dvb_frontend_ops cx22700_ops = {
411	.delsys = { SYS_DVBT },
412	.info = {
413		.name			= "Conexant CX22700 DVB-T",
414		.frequency_min		= 470000000,
415		.frequency_max		= 860000000,
416		.frequency_stepsize	= 166667,
417		.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
418		      FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
419		      FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
420		      FE_CAN_RECOVER
421	},
422
423	.release = cx22700_release,
424
425	.init = cx22700_init,
426	.i2c_gate_ctrl = cx22700_i2c_gate_ctrl,
427
428	.set_frontend = cx22700_set_frontend,
429	.get_frontend = cx22700_get_frontend,
430	.get_tune_settings = cx22700_get_tune_settings,
431
432	.read_status = cx22700_read_status,
433	.read_ber = cx22700_read_ber,
434	.read_signal_strength = cx22700_read_signal_strength,
435	.read_snr = cx22700_read_snr,
436	.read_ucblocks = cx22700_read_ucblocks,
437};
438
439module_param(debug, int, 0644);
440MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
441
442MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");
443MODULE_AUTHOR("Holger Waechtler");
444MODULE_LICENSE("GPL");
445
446EXPORT_SYMBOL(cx22700_attach);
447