1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/init.h>
14#include <linux/string.h>
15#include <linux/slab.h>
16
17#include "friio.h"
18
19struct jdvbt90502_state {
20 struct i2c_adapter *i2c;
21 struct dvb_frontend frontend;
22 struct jdvbt90502_config config;
23};
24
25
26
27static 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
57static 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
78static int _jdvbt90502_write(struct dvb_frontend *fe, const 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
93
94static int jdvbt90502_pll_read(struct jdvbt90502_state *state, u8 *result)
95{
96 int ret;
97
98
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
115error:
116 deb_fe("%s:ret == %d\n", __func__, ret);
117 return -EREMOTEIO;
118}
119
120
121
122static 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
137
138 f = freq / state->frontend.ops.info.frequency_stepsize;
139
140 f += 399;
141
142 if (f % 7 == 0)
143 f++;
144 pll_freq_cmd[DEMOD_REDIRECT_REG] = JDVBT90502_2ND_I2C_REG;
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;
149 pll_freq_cmd[BANDSWITCH_BYTE] = 0x08;
150
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);
155
156 ret = i2c_transfer(state->i2c, &msg[0], 1);
157 if (ret != 1)
158 goto error;
159
160 udelay(50);
161
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;
167 pll_agc_cmd[AGC_CTRL_BYTE] = 0x50;
168
169
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);
174
175 ret = i2c_transfer(state->i2c, &msg[1], 1);
176 if (ret != 1)
177 goto error;
178
179
180
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);
186
187
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
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);
200 }
201 if (retry >= RETRY_COUNT) {
202 deb_fe("%s: FE does not get ready after freq setting.\n",
203 __func__);
204 return -EREMOTEIO;
205 }
206
207 return 0;
208error:
209 deb_fe("%s:ret == %d\n", __func__, ret);
210 return -EREMOTEIO;
211}
212
213static int jdvbt90502_read_status(struct dvb_frontend *fe,
214 enum fe_status *state)
215{
216 u8 result;
217 int ret;
218
219 *state = FE_HAS_SIGNAL;
220
221 ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
222 if (ret) {
223 deb_fe("%s:ret == %d\n", __func__, ret);
224 return -EREMOTEIO;
225 }
226
227 *state = FE_HAS_SIGNAL
228 | FE_HAS_CARRIER
229 | FE_HAS_VITERBI
230 | FE_HAS_SYNC;
231
232 if (result & PLL_STATUS_LOCKED)
233 *state |= FE_HAS_LOCK;
234
235 return 0;
236}
237
238static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
239 u16 *strength)
240{
241 int ret;
242 u8 rbuf[37];
243
244 *strength = 0;
245
246
247
248 ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
249 rbuf, sizeof(rbuf));
250
251 if (ret) {
252 deb_fe("%s:ret == %d\n", __func__, ret);
253 return -EREMOTEIO;
254 }
255
256
257 *strength = (rbuf[3] << 8) + rbuf[4];
258 if (rbuf[2])
259 *strength = 0xffff;
260
261 return 0;
262}
263
264
265
266static int jdvbt90502_set_property(struct dvb_frontend *fe,
267 struct dtv_property *tvp)
268{
269 int r = 0;
270
271 switch (tvp->cmd) {
272 case DTV_DELIVERY_SYSTEM:
273 if (tvp->u.data != SYS_ISDBT)
274 r = -EINVAL;
275 break;
276 case DTV_CLEAR:
277 case DTV_TUNE:
278 case DTV_FREQUENCY:
279 break;
280 default:
281 r = -EINVAL;
282 }
283 return r;
284}
285
286static int jdvbt90502_set_frontend(struct dvb_frontend *fe)
287{
288 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
289
290
291
292
293
294
295
296 struct jdvbt90502_state *state = fe->demodulator_priv;
297 int ret;
298
299 deb_fe("%s: Freq:%d\n", __func__, p->frequency);
300
301
302 p->inversion = INVERSION_AUTO;
303 p->bandwidth_hz = 6000000;
304 p->code_rate_HP = FEC_AUTO;
305 p->code_rate_LP = FEC_AUTO;
306 p->modulation = QAM_64;
307 p->transmission_mode = TRANSMISSION_MODE_AUTO;
308 p->guard_interval = GUARD_INTERVAL_AUTO;
309 p->hierarchy = HIERARCHY_AUTO;
310 p->delivery_system = SYS_ISDBT;
311
312 ret = jdvbt90502_pll_set_freq(state, p->frequency);
313 if (ret) {
314 deb_fe("%s:ret == %d\n", __func__, ret);
315 return -EREMOTEIO;
316 }
317
318 return 0;
319}
320
321
322
323
324
325
326static u8 init_code[][2] = {
327 {0x01, 0x40},
328 {0x04, 0x38},
329 {0x05, 0x40},
330 {0x07, 0x40},
331 {0x0F, 0x4F},
332 {0x11, 0x21},
333 {0x12, 0x0B},
334 {0x13, 0x2F},
335 {0x14, 0x31},
336 {0x16, 0x02},
337 {0x21, 0xC4},
338 {0x22, 0x20},
339 {0x2C, 0x79},
340 {0x2D, 0x34},
341 {0x2F, 0x00},
342 {0x30, 0x28},
343 {0x31, 0x31},
344 {0x32, 0xDF},
345 {0x38, 0x01},
346 {0x39, 0x78},
347 {0x3B, 0x33},
348 {0x3C, 0x33},
349 {0x48, 0x90},
350 {0x51, 0x68},
351 {0x5E, 0x38},
352 {0x71, 0x00},
353 {0x72, 0x08},
354 {0x77, 0x00},
355 {0xC0, 0x21},
356 {0xC1, 0x10},
357 {0xE4, 0x1A},
358 {0xEA, 0x1F},
359 {0x77, 0x00},
360 {0x71, 0x00},
361 {0x71, 0x00},
362 {0x76, 0x0C},
363};
364
365static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);
366
367static int jdvbt90502_init(struct dvb_frontend *fe)
368{
369 int i = -1;
370 int ret;
371 struct i2c_msg msg;
372
373 struct jdvbt90502_state *state = fe->demodulator_priv;
374
375 deb_fe("%s called.\n", __func__);
376
377 msg.addr = state->config.demod_address;
378 msg.flags = 0;
379 msg.len = 2;
380 for (i = 0; i < init_code_len; i++) {
381 msg.buf = init_code[i];
382 ret = i2c_transfer(state->i2c, &msg, 1);
383 if (ret != 1)
384 goto error;
385 }
386 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
387 msleep(100);
388
389 return 0;
390
391error:
392 deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
393 return -EREMOTEIO;
394}
395
396
397static void jdvbt90502_release(struct dvb_frontend *fe)
398{
399 struct jdvbt90502_state *state = fe->demodulator_priv;
400 kfree(state);
401}
402
403
404static const struct dvb_frontend_ops jdvbt90502_ops;
405
406struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
407{
408 struct jdvbt90502_state *state = NULL;
409
410 deb_info("%s called.\n", __func__);
411
412
413 state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
414 if (state == NULL)
415 goto error;
416
417
418 state->i2c = &d->i2c_adap;
419 state->config = friio_fe_config;
420
421
422 state->frontend.ops = jdvbt90502_ops;
423 state->frontend.demodulator_priv = state;
424
425 if (jdvbt90502_init(&state->frontend) < 0)
426 goto error;
427
428 return &state->frontend;
429
430error:
431 kfree(state);
432 return NULL;
433}
434
435static const struct dvb_frontend_ops jdvbt90502_ops = {
436 .delsys = { SYS_ISDBT },
437 .info = {
438 .name = "Comtech JDVBT90502 ISDB-T",
439 .frequency_min = 473000000,
440 .frequency_max = 767142857,
441 .frequency_stepsize = JDVBT90502_PLL_CLK / JDVBT90502_PLL_DIVIDER,
442 .frequency_tolerance = 0,
443
444
445 .caps = FE_CAN_INVERSION_AUTO |
446 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
447 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
448 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
449 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
450 FE_CAN_TRANSMISSION_MODE_AUTO |
451 FE_CAN_GUARD_INTERVAL_AUTO |
452 FE_CAN_HIERARCHY_AUTO,
453 },
454
455 .release = jdvbt90502_release,
456
457 .init = jdvbt90502_init,
458 .write = _jdvbt90502_write,
459
460 .set_property = jdvbt90502_set_property,
461
462 .set_frontend = jdvbt90502_set_frontend,
463
464 .read_status = jdvbt90502_read_status,
465 .read_signal_strength = jdvbt90502_read_signal_strength,
466};
467