GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / media / pci / pt1 / va1j5jf8007t.c
1 /*
2  * ISDB-T driver for VA1J5JF8007/VA1J5JF8011
3  *
4  * Copyright (C) 2009 HIRANO Takahito <hiranotaka@zng.info>
5  *
6  * based on pt1dvr - http://pt1dvr.sourceforge.jp/
7  *      by Tomoaki Ishikawa <tomy@users.sourceforge.jp>
8  *
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.
13  *
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.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include "dvb_frontend.h"
25 #include "dvb_math.h"
26 #include "va1j5jf8007t.h"
27
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,
36 };
37
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;
43 };
44
45 static int va1j5jf8007t_read_snr(struct dvb_frontend *fe, u16 *snr)
46 {
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;
53
54         state = fe->demodulator_priv;
55         addr = state->config->demod_address;
56
57         word = 0;
58         for (i = 0; i < 3; i++) {
59                 write_buf[0] = 0x8b + i;
60
61                 msgs[0].addr = addr;
62                 msgs[0].flags = 0;
63                 msgs[0].len = sizeof(write_buf);
64                 msgs[0].buf = write_buf;
65
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;
70
71                 if (i2c_transfer(state->adap, msgs, 2) != 2)
72                         return -EREMOTEIO;
73
74                 word <<= 8;
75                 word |= read_buf[0];
76         }
77
78         if (!word)
79                 return -EIO;
80
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;
89 }
90
91 static int va1j5jf8007t_get_frontend_algo(struct dvb_frontend *fe)
92 {
93         return DVBFE_ALGO_HW;
94 }
95
96 static int
97 va1j5jf8007t_read_status(struct dvb_frontend *fe, enum fe_status *status)
98 {
99         struct va1j5jf8007t_state *state;
100
101         state = fe->demodulator_priv;
102
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;
109
110
111         case VA1J5JF8007T_SET_MODULATION:
112         case VA1J5JF8007T_CHECK_MODULATION:
113         case VA1J5JF8007T_ABORT:
114                 *status |= FE_HAS_SIGNAL;
115                 return 0;
116
117         case VA1J5JF8007T_TRACK:
118                 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
119                 return 0;
120         }
121
122         BUG();
123 }
124
125 struct va1j5jf8007t_cb_map {
126         u32 frequency;
127         u8 cb;
128 };
129
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 },
141 };
142
143 static u8 va1j5jf8007t_lookup_cb(u32 frequency)
144 {
145         int i;
146         const struct va1j5jf8007t_cb_map *map;
147
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;
152         }
153         return 0xe4;
154 }
155
156 static int va1j5jf8007t_set_frequency(struct va1j5jf8007t_state *state)
157 {
158         u32 frequency;
159         u16 word;
160         u8 buf[6];
161         struct i2c_msg msg;
162
163         frequency = state->fe.dtv_property_cache.frequency;
164
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);
172
173         msg.addr = state->config->demod_address;
174         msg.flags = 0;
175         msg.len = sizeof(buf);
176         msg.buf = buf;
177
178         if (i2c_transfer(state->adap, &msg, 1) != 1)
179                 return -EREMOTEIO;
180
181         return 0;
182 }
183
184 static int
185 va1j5jf8007t_check_frequency(struct va1j5jf8007t_state *state, int *lock)
186 {
187         u8 addr;
188         u8 write_buf[2], read_buf[1];
189         struct i2c_msg msgs[2];
190
191         addr = state->config->demod_address;
192
193         write_buf[0] = 0xfe;
194         write_buf[1] = 0xc3;
195
196         msgs[0].addr = addr;
197         msgs[0].flags = 0;
198         msgs[0].len = sizeof(write_buf);
199         msgs[0].buf = write_buf;
200
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;
205
206         if (i2c_transfer(state->adap, msgs, 2) != 2)
207                 return -EREMOTEIO;
208
209         *lock = read_buf[0] & 0x40;
210         return 0;
211 }
212
213 static int va1j5jf8007t_set_modulation(struct va1j5jf8007t_state *state)
214 {
215         u8 buf[2];
216         struct i2c_msg msg;
217
218         buf[0] = 0x01;
219         buf[1] = 0x40;
220
221         msg.addr = state->config->demod_address;
222         msg.flags = 0;
223         msg.len = sizeof(buf);
224         msg.buf = buf;
225
226         if (i2c_transfer(state->adap, &msg, 1) != 1)
227                 return -EREMOTEIO;
228
229         return 0;
230 }
231
232 static int va1j5jf8007t_check_modulation(struct va1j5jf8007t_state *state,
233                                          int *lock, int *retry)
234 {
235         u8 addr;
236         u8 write_buf[1], read_buf[1];
237         struct i2c_msg msgs[2];
238
239         addr = state->config->demod_address;
240
241         write_buf[0] = 0x80;
242
243         msgs[0].addr = addr;
244         msgs[0].flags = 0;
245         msgs[0].len = sizeof(write_buf);
246         msgs[0].buf = write_buf;
247
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;
252
253         if (i2c_transfer(state->adap, msgs, 2) != 2)
254                 return -EREMOTEIO;
255
256         *lock = !(read_buf[0] & 0x10);
257         *retry = read_buf[0] & 0x80;
258         return 0;
259 }
260
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)
266 {
267         struct va1j5jf8007t_state *state;
268         int ret;
269         int lock = 0, retry = 0;
270
271         state = fe->demodulator_priv;
272
273         if (re_tune)
274                 state->tune_state = VA1J5JF8007T_SET_FREQUENCY;
275
276         switch (state->tune_state) {
277         case VA1J5JF8007T_IDLE:
278                 *delay = 3 * HZ;
279                 *status = 0;
280                 return 0;
281
282         case VA1J5JF8007T_SET_FREQUENCY:
283                 ret = va1j5jf8007t_set_frequency(state);
284                 if (ret < 0)
285                         return ret;
286
287                 state->tune_state = VA1J5JF8007T_CHECK_FREQUENCY;
288                 *delay = 0;
289                 *status = 0;
290                 return 0;
291
292         case VA1J5JF8007T_CHECK_FREQUENCY:
293                 ret = va1j5jf8007t_check_frequency(state, &lock);
294                 if (ret < 0)
295                         return ret;
296
297                 if (!lock)  {
298                         *delay = (HZ + 999) / 1000;
299                         *status = 0;
300                         return 0;
301                 }
302
303                 state->tune_state = VA1J5JF8007T_SET_MODULATION;
304                 *delay = 0;
305                 *status = FE_HAS_SIGNAL;
306                 return 0;
307
308         case VA1J5JF8007T_SET_MODULATION:
309                 ret = va1j5jf8007t_set_modulation(state);
310                 if (ret < 0)
311                         return ret;
312
313                 state->tune_state = VA1J5JF8007T_CHECK_MODULATION;
314                 *delay = 0;
315                 *status = FE_HAS_SIGNAL;
316                 return 0;
317
318         case VA1J5JF8007T_CHECK_MODULATION:
319                 ret = va1j5jf8007t_check_modulation(state, &lock, &retry);
320                 if (ret < 0)
321                         return ret;
322
323                 if (!lock)  {
324                         if (!retry)  {
325                                 state->tune_state = VA1J5JF8007T_ABORT;
326                                 *delay = 3 * HZ;
327                                 *status = FE_HAS_SIGNAL;
328                                 return 0;
329                         }
330                         *delay = (HZ + 999) / 1000;
331                         *status = FE_HAS_SIGNAL;
332                         return 0;
333                 }
334
335                 state->tune_state = VA1J5JF8007T_TRACK;
336                 /* fall through */
337
338         case VA1J5JF8007T_TRACK:
339                 *delay = 3 * HZ;
340                 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
341                 return 0;
342
343         case VA1J5JF8007T_ABORT:
344                 *delay = 3 * HZ;
345                 *status = FE_HAS_SIGNAL;
346                 return 0;
347         }
348
349         BUG();
350 }
351
352 static int va1j5jf8007t_init_frequency(struct va1j5jf8007t_state *state)
353 {
354         u8 buf[7];
355         struct i2c_msg msg;
356
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;
364
365         msg.addr = state->config->demod_address;
366         msg.flags = 0;
367         msg.len = sizeof(buf);
368         msg.buf = buf;
369
370         if (i2c_transfer(state->adap, &msg, 1) != 1)
371                 return -EREMOTEIO;
372
373         return 0;
374 }
375
376 static int va1j5jf8007t_set_sleep(struct va1j5jf8007t_state *state, int sleep)
377 {
378         u8 buf[2];
379         struct i2c_msg msg;
380
381         buf[0] = 0x03;
382         buf[1] = sleep ? 0x90 : 0x80;
383
384         msg.addr = state->config->demod_address;
385         msg.flags = 0;
386         msg.len = sizeof(buf);
387         msg.buf = buf;
388
389         if (i2c_transfer(state->adap, &msg, 1) != 1)
390                 return -EREMOTEIO;
391
392         return 0;
393 }
394
395 static int va1j5jf8007t_sleep(struct dvb_frontend *fe)
396 {
397         struct va1j5jf8007t_state *state;
398         int ret;
399
400         state = fe->demodulator_priv;
401
402         ret = va1j5jf8007t_init_frequency(state);
403         if (ret < 0)
404                 return ret;
405
406         return va1j5jf8007t_set_sleep(state, 1);
407 }
408
409 static int va1j5jf8007t_init(struct dvb_frontend *fe)
410 {
411         struct va1j5jf8007t_state *state;
412
413         state = fe->demodulator_priv;
414         state->tune_state = VA1J5JF8007T_IDLE;
415
416         return va1j5jf8007t_set_sleep(state, 0);
417 }
418
419 static void va1j5jf8007t_release(struct dvb_frontend *fe)
420 {
421         struct va1j5jf8007t_state *state;
422         state = fe->demodulator_priv;
423         kfree(state);
424 }
425
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,
436         },
437
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,
445 };
446
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}
452 };
453
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}
458 };
459
460 int va1j5jf8007t_prepare(struct dvb_frontend *fe)
461 {
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;
468
469         state = fe->demodulator_priv;
470
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;
482         }
483
484         msg.addr = state->config->demod_address;
485         msg.flags = 0;
486         msg.len = sizeof(buf);
487         msg.buf = buf;
488
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;
493         }
494
495         return va1j5jf8007t_init_frequency(state);
496 }
497
498 struct dvb_frontend *
499 va1j5jf8007t_attach(const struct va1j5jf8007t_config *config,
500                     struct i2c_adapter *adap)
501 {
502         struct va1j5jf8007t_state *state;
503         struct dvb_frontend *fe;
504         u8 buf[2];
505         struct i2c_msg msg;
506
507         state = kzalloc(sizeof(struct va1j5jf8007t_state), GFP_KERNEL);
508         if (!state)
509                 return NULL;
510
511         state->config = config;
512         state->adap = adap;
513
514         fe = &state->fe;
515         memcpy(&fe->ops, &va1j5jf8007t_ops, sizeof(struct dvb_frontend_ops));
516         fe->demodulator_priv = state;
517
518         buf[0] = 0x01;
519         buf[1] = 0x80;
520
521         msg.addr = state->config->demod_address;
522         msg.flags = 0;
523         msg.len = sizeof(buf);
524         msg.buf = buf;
525
526         if (i2c_transfer(state->adap, &msg, 1) != 1) {
527                 kfree(state);
528                 return NULL;
529         }
530
531         return fe;
532 }