Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / media / dvb / dvb-usb / friio-fe.c
blobd14bd227b5027b5a0bf0afd4c281e909db12cf38
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>
17 #include "friio.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)
30 int ret;
31 u8 wbuf[3];
32 struct i2c_msg msg[2];
34 wbuf[0] = reg & 0xFF;
35 wbuf[1] = 0;
36 wbuf[2] = reg >> 8;
38 msg[0].addr = state->config.demod_address;
39 msg[0].flags = 0;
40 msg[0].buf = wbuf;
41 msg[0].len = sizeof(wbuf);
43 msg[1].addr = msg[0].addr;
44 msg[1].flags = I2C_M_RD;
45 msg[1].buf = buf;
46 msg[1].len = count;
48 ret = i2c_transfer(state->i2c, msg, 2);
49 if (ret != 2) {
50 deb_fe(" reg read failed.\n");
51 return -EREMOTEIO;
53 return 0;
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)
60 struct i2c_msg msg;
61 u8 wbuf[2];
63 wbuf[0] = reg;
64 wbuf[1] = val;
66 msg.addr = state->config.demod_address;
67 msg.flags = 0;
68 msg.buf = wbuf;
69 msg.len = sizeof(wbuf);
71 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
72 deb_fe(" reg write failed.");
73 return -EREMOTEIO;
75 return 0;
78 static int _jdvbt90502_write(struct dvb_frontend *fe, u8 *buf, int len)
80 struct jdvbt90502_state *state = fe->demodulator_priv;
81 int err, i;
82 for (i = 0; i < len - 1; i++) {
83 err = jdvbt90502_single_reg_write(state,
84 buf[0] + i, buf[i + 1]);
85 if (err)
86 return err;
89 return 0;
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)
96 int ret;
98 /* +1 for reading */
99 u8 pll_addr_byte = (state->config.pll_address << 1) + 1;
101 *result = 0;
103 ret = jdvbt90502_single_reg_write(state, JDVBT90502_2ND_I2C_REG,
104 pll_addr_byte);
105 if (ret)
106 goto error;
108 ret = jdvbt90502_reg_read(state, 0x0100, result, 1);
109 if (ret)
110 goto error;
112 deb_fe("PLL read val:%02x\n", *result);
113 return 0;
115 error:
116 deb_fe("%s:ret == %d\n", __func__, ret);
117 return -EREMOTEIO;
121 /* set pll frequency via the demodulator's I2C register */
122 static int jdvbt90502_pll_set_freq(struct jdvbt90502_state *state, u32 freq)
124 int ret;
125 int retry;
126 u8 res1;
127 u8 res2[9];
129 u8 pll_freq_cmd[PLL_CMD_LEN];
130 u8 pll_agc_cmd[PLL_CMD_LEN];
131 struct i2c_msg msg[2];
132 u32 f;
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 */
140 f += 399;
141 /* add center frequency shift if necessary */
142 if (f % 7 == 0)
143 f++;
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;
152 msg[0].flags = 0;
153 msg[0].buf = pll_freq_cmd;
154 msg[0].len = sizeof(pll_freq_cmd);
156 ret = i2c_transfer(state->i2c, &msg[0], 1);
157 if (ret != 1)
158 goto error;
160 udelay(50);
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;
171 msg[1].flags = 0;
172 msg[1].buf = pll_agc_cmd;
173 msg[1].len = sizeof(pll_agc_cmd);
175 ret = i2c_transfer(state->i2c, &msg[1], 1);
176 if (ret != 1)
177 goto error;
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);
183 if (ret)
184 goto error;
185 udelay(100);
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);
191 if (ret)
192 goto error;
193 /* if (res1 != 0x00) goto error; */
194 ret = jdvbt90502_reg_read(state, 0x00B0, res2, sizeof(res2));
195 if (ret)
196 goto error;
197 if (res2[0] >= 0xA7)
198 break;
199 msleep(100);
201 if (retry >= RETRY_COUNT) {
202 deb_fe("%s: FE does not get ready after freq setting.\n",
203 __func__);
204 return -EREMOTEIO;
207 return 0;
208 error:
209 deb_fe("%s:ret == %d\n", __func__, ret);
210 return -EREMOTEIO;
213 static int jdvbt90502_read_status(struct dvb_frontend *fe, fe_status_t *state)
215 u8 result;
216 int ret;
218 *state = FE_HAS_SIGNAL;
220 ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
221 if (ret) {
222 deb_fe("%s:ret == %d\n", __func__, ret);
223 return -EREMOTEIO;
226 *state = FE_HAS_SIGNAL
227 | FE_HAS_CARRIER
228 | FE_HAS_VITERBI
229 | FE_HAS_SYNC;
231 if (result & PLL_STATUS_LOCKED)
232 *state |= FE_HAS_LOCK;
234 return 0;
237 static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
238 u16 *strength)
240 int ret;
241 u8 rbuf[37];
243 *strength = 0;
245 /* status register (incl. signal strength) : 0x89 */
246 /* TODO: read just the necessary registers [0x8B..0x8D]? */
247 ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
248 rbuf, sizeof(rbuf));
250 if (ret) {
251 deb_fe("%s:ret == %d\n", __func__, ret);
252 return -EREMOTEIO;
255 /* signal_strength: rbuf[2-4] (24bit BE), use lower 16bit for now. */
256 *strength = (rbuf[3] << 8) + rbuf[4];
257 if (rbuf[2])
258 *strength = 0xffff;
260 return 0;
264 /* filter out un-supported properties to notify users */
265 static int jdvbt90502_set_property(struct dvb_frontend *fe,
266 struct dtv_property *tvp)
268 int r = 0;
270 switch (tvp->cmd) {
271 case DTV_DELIVERY_SYSTEM:
272 if (tvp->u.data != SYS_ISDBT)
273 r = -EINVAL;
274 break;
275 case DTV_CLEAR:
276 case DTV_TUNE:
277 case DTV_FREQUENCY:
278 break;
279 default:
280 r = -EINVAL;
282 return r;
285 static int jdvbt90502_get_frontend(struct dvb_frontend *fe,
286 struct dvb_frontend_parameters *p)
288 p->inversion = INVERSION_AUTO;
289 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
290 p->u.ofdm.code_rate_HP = FEC_AUTO;
291 p->u.ofdm.code_rate_LP = FEC_AUTO;
292 p->u.ofdm.constellation = QAM_64;
293 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
294 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
295 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
296 return 0;
299 static int jdvbt90502_set_frontend(struct dvb_frontend *fe,
300 struct dvb_frontend_parameters *p)
303 * NOTE: ignore all the paramters except frequency.
304 * others should be fixed to the proper value for ISDB-T,
305 * but don't check here.
308 struct jdvbt90502_state *state = fe->demodulator_priv;
309 int ret;
311 deb_fe("%s: Freq:%d\n", __func__, p->frequency);
313 /* for recovery from DTV_CLEAN */
314 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
316 ret = jdvbt90502_pll_set_freq(state, p->frequency);
317 if (ret) {
318 deb_fe("%s:ret == %d\n", __func__, ret);
319 return -EREMOTEIO;
322 return 0;
327 * (reg, val) commad list to initialize this module.
328 * captured on a Windows box.
330 static u8 init_code[][2] = {
331 {0x01, 0x40},
332 {0x04, 0x38},
333 {0x05, 0x40},
334 {0x07, 0x40},
335 {0x0F, 0x4F},
336 {0x11, 0x21},
337 {0x12, 0x0B},
338 {0x13, 0x2F},
339 {0x14, 0x31},
340 {0x16, 0x02},
341 {0x21, 0xC4},
342 {0x22, 0x20},
343 {0x2C, 0x79},
344 {0x2D, 0x34},
345 {0x2F, 0x00},
346 {0x30, 0x28},
347 {0x31, 0x31},
348 {0x32, 0xDF},
349 {0x38, 0x01},
350 {0x39, 0x78},
351 {0x3B, 0x33},
352 {0x3C, 0x33},
353 {0x48, 0x90},
354 {0x51, 0x68},
355 {0x5E, 0x38},
356 {0x71, 0x00},
357 {0x72, 0x08},
358 {0x77, 0x00},
359 {0xC0, 0x21},
360 {0xC1, 0x10},
361 {0xE4, 0x1A},
362 {0xEA, 0x1F},
363 {0x77, 0x00},
364 {0x71, 0x00},
365 {0x71, 0x00},
366 {0x76, 0x0C},
369 static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);
371 static int jdvbt90502_init(struct dvb_frontend *fe)
373 int i = -1;
374 int ret;
375 struct i2c_msg msg;
377 struct jdvbt90502_state *state = fe->demodulator_priv;
379 deb_fe("%s called.\n", __func__);
381 msg.addr = state->config.demod_address;
382 msg.flags = 0;
383 msg.len = 2;
384 for (i = 0; i < init_code_len; i++) {
385 msg.buf = init_code[i];
386 ret = i2c_transfer(state->i2c, &msg, 1);
387 if (ret != 1)
388 goto error;
390 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
391 msleep(100);
393 return 0;
395 error:
396 deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
397 return -EREMOTEIO;
401 static void jdvbt90502_release(struct dvb_frontend *fe)
403 struct jdvbt90502_state *state = fe->demodulator_priv;
404 kfree(state);
408 static struct dvb_frontend_ops jdvbt90502_ops;
410 struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
412 struct jdvbt90502_state *state = NULL;
414 deb_info("%s called.\n", __func__);
416 /* allocate memory for the internal state */
417 state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
418 if (state == NULL)
419 goto error;
421 /* setup the state */
422 state->i2c = &d->i2c_adap;
423 memcpy(&state->config, &friio_fe_config, sizeof(friio_fe_config));
425 /* create dvb_frontend */
426 memcpy(&state->frontend.ops, &jdvbt90502_ops,
427 sizeof(jdvbt90502_ops));
428 state->frontend.demodulator_priv = state;
430 if (jdvbt90502_init(&state->frontend) < 0)
431 goto error;
433 return &state->frontend;
435 error:
436 kfree(state);
437 return NULL;
440 static struct dvb_frontend_ops jdvbt90502_ops = {
442 .info = {
443 .name = "Comtech JDVBT90502 ISDB-T",
444 .type = FE_OFDM,
445 .frequency_min = 473000000, /* UHF 13ch, center */
446 .frequency_max = 767142857, /* UHF 62ch, center */
447 .frequency_stepsize = JDVBT90502_PLL_CLK /
448 JDVBT90502_PLL_DIVIDER,
449 .frequency_tolerance = 0,
451 /* NOTE: this driver ignores all parameters but frequency. */
452 .caps = FE_CAN_INVERSION_AUTO |
453 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
454 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
455 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
456 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
457 FE_CAN_TRANSMISSION_MODE_AUTO |
458 FE_CAN_GUARD_INTERVAL_AUTO |
459 FE_CAN_HIERARCHY_AUTO,
462 .release = jdvbt90502_release,
464 .init = jdvbt90502_init,
465 .write = _jdvbt90502_write,
467 .set_property = jdvbt90502_set_property,
469 .set_frontend = jdvbt90502_set_frontend,
470 .get_frontend = jdvbt90502_get_frontend,
472 .read_status = jdvbt90502_read_status,
473 .read_signal_strength = jdvbt90502_read_signal_strength,