Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / pt1 / va1j5jf8007t.c
bloba52984a6f9b33931d9fddcf5964051a4b39b0d99
1 /*
2 * ISDB-T driver for VA1J5JF8007/VA1J5JF8011
4 * Copyright (C) 2009 HIRANO Takahito <hiranotaka@zng.info>
6 * based on pt1dvr - http://pt1dvr.sourceforge.jp/
7 * by Tomoaki Ishikawa <tomy@users.sourceforge.jp>
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.
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.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <media/dvb_frontend.h>
25 #include <media/dvb_math.h>
26 #include "va1j5jf8007t.h"
28 enum va1j5jf8007t_tune_state {
29 VA1J5JF8007T_IDLE,
30 VA1J5JF8007T_SET_FREQUENCY,
31 VA1J5JF8007T_CHECK_FREQUENCY,
32 VA1J5JF8007T_SET_MODULATION,
33 VA1J5JF8007T_CHECK_MODULATION,
34 VA1J5JF8007T_TRACK,
35 VA1J5JF8007T_ABORT,
38 struct va1j5jf8007t_state {
39 const struct va1j5jf8007t_config *config;
40 struct i2c_adapter *adap;
41 struct dvb_frontend fe;
42 enum va1j5jf8007t_tune_state tune_state;
45 static int va1j5jf8007t_read_snr(struct dvb_frontend *fe, u16 *snr)
47 struct va1j5jf8007t_state *state;
48 u8 addr;
49 int i;
50 u8 write_buf[1], read_buf[1];
51 struct i2c_msg msgs[2];
52 s32 word, x, y;
54 state = fe->demodulator_priv;
55 addr = state->config->demod_address;
57 word = 0;
58 for (i = 0; i < 3; i++) {
59 write_buf[0] = 0x8b + i;
61 msgs[0].addr = addr;
62 msgs[0].flags = 0;
63 msgs[0].len = sizeof(write_buf);
64 msgs[0].buf = write_buf;
66 msgs[1].addr = addr;
67 msgs[1].flags = I2C_M_RD;
68 msgs[1].len = sizeof(read_buf);
69 msgs[1].buf = read_buf;
71 if (i2c_transfer(state->adap, msgs, 2) != 2)
72 return -EREMOTEIO;
74 word <<= 8;
75 word |= read_buf[0];
78 if (!word)
79 return -EIO;
81 x = 10 * (intlog10(0x540000 * 100 / word) - (2 << 24));
82 y = (24ll << 46) / 1000000;
83 y = ((s64)y * x >> 30) - (16ll << 40) / 10000;
84 y = ((s64)y * x >> 29) + (398ll << 35) / 10000;
85 y = ((s64)y * x >> 30) + (5491ll << 29) / 10000;
86 y = ((s64)y * x >> 30) + (30965ll << 23) / 10000;
87 *snr = y >> 15;
88 return 0;
91 static int va1j5jf8007t_get_frontend_algo(struct dvb_frontend *fe)
93 return DVBFE_ALGO_HW;
96 static int
97 va1j5jf8007t_read_status(struct dvb_frontend *fe, enum fe_status *status)
99 struct va1j5jf8007t_state *state;
101 state = fe->demodulator_priv;
103 switch (state->tune_state) {
104 case VA1J5JF8007T_IDLE:
105 case VA1J5JF8007T_SET_FREQUENCY:
106 case VA1J5JF8007T_CHECK_FREQUENCY:
107 *status = 0;
108 return 0;
111 case VA1J5JF8007T_SET_MODULATION:
112 case VA1J5JF8007T_CHECK_MODULATION:
113 case VA1J5JF8007T_ABORT:
114 *status |= FE_HAS_SIGNAL;
115 return 0;
117 case VA1J5JF8007T_TRACK:
118 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
119 return 0;
122 BUG();
125 struct va1j5jf8007t_cb_map {
126 u32 frequency;
127 u8 cb;
130 static const struct va1j5jf8007t_cb_map va1j5jf8007t_cb_maps[] = {
131 { 90000000, 0x80 },
132 { 140000000, 0x81 },
133 { 170000000, 0xa1 },
134 { 220000000, 0x62 },
135 { 330000000, 0xa2 },
136 { 402000000, 0xe2 },
137 { 450000000, 0x64 },
138 { 550000000, 0x84 },
139 { 600000000, 0xa4 },
140 { 700000000, 0xc4 },
143 static u8 va1j5jf8007t_lookup_cb(u32 frequency)
145 int i;
146 const struct va1j5jf8007t_cb_map *map;
148 for (i = 0; i < ARRAY_SIZE(va1j5jf8007t_cb_maps); i++) {
149 map = &va1j5jf8007t_cb_maps[i];
150 if (frequency < map->frequency)
151 return map->cb;
153 return 0xe4;
156 static int va1j5jf8007t_set_frequency(struct va1j5jf8007t_state *state)
158 u32 frequency;
159 u16 word;
160 u8 buf[6];
161 struct i2c_msg msg;
163 frequency = state->fe.dtv_property_cache.frequency;
165 word = (frequency + 71428) / 142857 + 399;
166 buf[0] = 0xfe;
167 buf[1] = 0xc2;
168 buf[2] = word >> 8;
169 buf[3] = word;
170 buf[4] = 0x80;
171 buf[5] = va1j5jf8007t_lookup_cb(frequency);
173 msg.addr = state->config->demod_address;
174 msg.flags = 0;
175 msg.len = sizeof(buf);
176 msg.buf = buf;
178 if (i2c_transfer(state->adap, &msg, 1) != 1)
179 return -EREMOTEIO;
181 return 0;
184 static int
185 va1j5jf8007t_check_frequency(struct va1j5jf8007t_state *state, int *lock)
187 u8 addr;
188 u8 write_buf[2], read_buf[1];
189 struct i2c_msg msgs[2];
191 addr = state->config->demod_address;
193 write_buf[0] = 0xfe;
194 write_buf[1] = 0xc3;
196 msgs[0].addr = addr;
197 msgs[0].flags = 0;
198 msgs[0].len = sizeof(write_buf);
199 msgs[0].buf = write_buf;
201 msgs[1].addr = addr;
202 msgs[1].flags = I2C_M_RD;
203 msgs[1].len = sizeof(read_buf);
204 msgs[1].buf = read_buf;
206 if (i2c_transfer(state->adap, msgs, 2) != 2)
207 return -EREMOTEIO;
209 *lock = read_buf[0] & 0x40;
210 return 0;
213 static int va1j5jf8007t_set_modulation(struct va1j5jf8007t_state *state)
215 u8 buf[2];
216 struct i2c_msg msg;
218 buf[0] = 0x01;
219 buf[1] = 0x40;
221 msg.addr = state->config->demod_address;
222 msg.flags = 0;
223 msg.len = sizeof(buf);
224 msg.buf = buf;
226 if (i2c_transfer(state->adap, &msg, 1) != 1)
227 return -EREMOTEIO;
229 return 0;
232 static int va1j5jf8007t_check_modulation(struct va1j5jf8007t_state *state,
233 int *lock, int *retry)
235 u8 addr;
236 u8 write_buf[1], read_buf[1];
237 struct i2c_msg msgs[2];
239 addr = state->config->demod_address;
241 write_buf[0] = 0x80;
243 msgs[0].addr = addr;
244 msgs[0].flags = 0;
245 msgs[0].len = sizeof(write_buf);
246 msgs[0].buf = write_buf;
248 msgs[1].addr = addr;
249 msgs[1].flags = I2C_M_RD;
250 msgs[1].len = sizeof(read_buf);
251 msgs[1].buf = read_buf;
253 if (i2c_transfer(state->adap, msgs, 2) != 2)
254 return -EREMOTEIO;
256 *lock = !(read_buf[0] & 0x10);
257 *retry = read_buf[0] & 0x80;
258 return 0;
261 static int
262 va1j5jf8007t_tune(struct dvb_frontend *fe,
263 bool re_tune,
264 unsigned int mode_flags, unsigned int *delay,
265 enum fe_status *status)
267 struct va1j5jf8007t_state *state;
268 int ret;
269 int lock = 0, retry = 0;
271 state = fe->demodulator_priv;
273 if (re_tune)
274 state->tune_state = VA1J5JF8007T_SET_FREQUENCY;
276 switch (state->tune_state) {
277 case VA1J5JF8007T_IDLE:
278 *delay = 3 * HZ;
279 *status = 0;
280 return 0;
282 case VA1J5JF8007T_SET_FREQUENCY:
283 ret = va1j5jf8007t_set_frequency(state);
284 if (ret < 0)
285 return ret;
287 state->tune_state = VA1J5JF8007T_CHECK_FREQUENCY;
288 *delay = 0;
289 *status = 0;
290 return 0;
292 case VA1J5JF8007T_CHECK_FREQUENCY:
293 ret = va1j5jf8007t_check_frequency(state, &lock);
294 if (ret < 0)
295 return ret;
297 if (!lock) {
298 *delay = (HZ + 999) / 1000;
299 *status = 0;
300 return 0;
303 state->tune_state = VA1J5JF8007T_SET_MODULATION;
304 *delay = 0;
305 *status = FE_HAS_SIGNAL;
306 return 0;
308 case VA1J5JF8007T_SET_MODULATION:
309 ret = va1j5jf8007t_set_modulation(state);
310 if (ret < 0)
311 return ret;
313 state->tune_state = VA1J5JF8007T_CHECK_MODULATION;
314 *delay = 0;
315 *status = FE_HAS_SIGNAL;
316 return 0;
318 case VA1J5JF8007T_CHECK_MODULATION:
319 ret = va1j5jf8007t_check_modulation(state, &lock, &retry);
320 if (ret < 0)
321 return ret;
323 if (!lock) {
324 if (!retry) {
325 state->tune_state = VA1J5JF8007T_ABORT;
326 *delay = 3 * HZ;
327 *status = FE_HAS_SIGNAL;
328 return 0;
330 *delay = (HZ + 999) / 1000;
331 *status = FE_HAS_SIGNAL;
332 return 0;
335 state->tune_state = VA1J5JF8007T_TRACK;
336 /* fall through */
338 case VA1J5JF8007T_TRACK:
339 *delay = 3 * HZ;
340 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
341 return 0;
343 case VA1J5JF8007T_ABORT:
344 *delay = 3 * HZ;
345 *status = FE_HAS_SIGNAL;
346 return 0;
349 BUG();
352 static int va1j5jf8007t_init_frequency(struct va1j5jf8007t_state *state)
354 u8 buf[7];
355 struct i2c_msg msg;
357 buf[0] = 0xfe;
358 buf[1] = 0xc2;
359 buf[2] = 0x01;
360 buf[3] = 0x8f;
361 buf[4] = 0xc1;
362 buf[5] = 0x80;
363 buf[6] = 0x80;
365 msg.addr = state->config->demod_address;
366 msg.flags = 0;
367 msg.len = sizeof(buf);
368 msg.buf = buf;
370 if (i2c_transfer(state->adap, &msg, 1) != 1)
371 return -EREMOTEIO;
373 return 0;
376 static int va1j5jf8007t_set_sleep(struct va1j5jf8007t_state *state, int sleep)
378 u8 buf[2];
379 struct i2c_msg msg;
381 buf[0] = 0x03;
382 buf[1] = sleep ? 0x90 : 0x80;
384 msg.addr = state->config->demod_address;
385 msg.flags = 0;
386 msg.len = sizeof(buf);
387 msg.buf = buf;
389 if (i2c_transfer(state->adap, &msg, 1) != 1)
390 return -EREMOTEIO;
392 return 0;
395 static int va1j5jf8007t_sleep(struct dvb_frontend *fe)
397 struct va1j5jf8007t_state *state;
398 int ret;
400 state = fe->demodulator_priv;
402 ret = va1j5jf8007t_init_frequency(state);
403 if (ret < 0)
404 return ret;
406 return va1j5jf8007t_set_sleep(state, 1);
409 static int va1j5jf8007t_init(struct dvb_frontend *fe)
411 struct va1j5jf8007t_state *state;
413 state = fe->demodulator_priv;
414 state->tune_state = VA1J5JF8007T_IDLE;
416 return va1j5jf8007t_set_sleep(state, 0);
419 static void va1j5jf8007t_release(struct dvb_frontend *fe)
421 struct va1j5jf8007t_state *state;
422 state = fe->demodulator_priv;
423 kfree(state);
426 static const struct dvb_frontend_ops va1j5jf8007t_ops = {
427 .delsys = { SYS_ISDBT },
428 .info = {
429 .name = "VA1J5JF8007/VA1J5JF8011 ISDB-T",
430 .frequency_min = 90000000,
431 .frequency_max = 770000000,
432 .frequency_stepsize = 142857,
433 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_AUTO |
434 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
435 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
438 .read_snr = va1j5jf8007t_read_snr,
439 .get_frontend_algo = va1j5jf8007t_get_frontend_algo,
440 .read_status = va1j5jf8007t_read_status,
441 .tune = va1j5jf8007t_tune,
442 .sleep = va1j5jf8007t_sleep,
443 .init = va1j5jf8007t_init,
444 .release = va1j5jf8007t_release,
447 static const u8 va1j5jf8007t_20mhz_prepare_bufs[][2] = {
448 {0x03, 0x90}, {0x14, 0x8f}, {0x1c, 0x2a}, {0x1d, 0xa8}, {0x1e, 0xa2},
449 {0x22, 0x83}, {0x31, 0x0d}, {0x32, 0xe0}, {0x39, 0xd3}, {0x3a, 0x00},
450 {0x5c, 0x40}, {0x5f, 0x80}, {0x75, 0x02}, {0x76, 0x4e}, {0x77, 0x03},
451 {0xef, 0x01}
454 static const u8 va1j5jf8007t_25mhz_prepare_bufs[][2] = {
455 {0x03, 0x90}, {0x1c, 0x2a}, {0x1d, 0xa8}, {0x1e, 0xa2}, {0x22, 0x83},
456 {0x3a, 0x00}, {0x5c, 0x40}, {0x5f, 0x80}, {0x75, 0x0a}, {0x76, 0x4c},
457 {0x77, 0x03}, {0xef, 0x01}
460 int va1j5jf8007t_prepare(struct dvb_frontend *fe)
462 struct va1j5jf8007t_state *state;
463 const u8 (*bufs)[2];
464 int size;
465 u8 buf[2];
466 struct i2c_msg msg;
467 int i;
469 state = fe->demodulator_priv;
471 switch (state->config->frequency) {
472 case VA1J5JF8007T_20MHZ:
473 bufs = va1j5jf8007t_20mhz_prepare_bufs;
474 size = ARRAY_SIZE(va1j5jf8007t_20mhz_prepare_bufs);
475 break;
476 case VA1J5JF8007T_25MHZ:
477 bufs = va1j5jf8007t_25mhz_prepare_bufs;
478 size = ARRAY_SIZE(va1j5jf8007t_25mhz_prepare_bufs);
479 break;
480 default:
481 return -EINVAL;
484 msg.addr = state->config->demod_address;
485 msg.flags = 0;
486 msg.len = sizeof(buf);
487 msg.buf = buf;
489 for (i = 0; i < size; i++) {
490 memcpy(buf, bufs[i], sizeof(buf));
491 if (i2c_transfer(state->adap, &msg, 1) != 1)
492 return -EREMOTEIO;
495 return va1j5jf8007t_init_frequency(state);
498 struct dvb_frontend *
499 va1j5jf8007t_attach(const struct va1j5jf8007t_config *config,
500 struct i2c_adapter *adap)
502 struct va1j5jf8007t_state *state;
503 struct dvb_frontend *fe;
504 u8 buf[2];
505 struct i2c_msg msg;
507 state = kzalloc(sizeof(struct va1j5jf8007t_state), GFP_KERNEL);
508 if (!state)
509 return NULL;
511 state->config = config;
512 state->adap = adap;
514 fe = &state->fe;
515 memcpy(&fe->ops, &va1j5jf8007t_ops, sizeof(struct dvb_frontend_ops));
516 fe->demodulator_priv = state;
518 buf[0] = 0x01;
519 buf[1] = 0x80;
521 msg.addr = state->config->demod_address;
522 msg.flags = 0;
523 msg.len = sizeof(buf);
524 msg.buf = buf;
526 if (i2c_transfer(state->adap, &msg, 1) != 1) {
527 kfree(state);
528 return NULL;
531 return fe;