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