]> Pileus Git - ~andy/linux/blob - drivers/media/dvb/mantis/mantis_vp2033.c
V4L/DVB (13795): [Mantis/Hopper] Code overhaul, add Hopper devices into the PCI ID...
[~andy/linux] / drivers / media / dvb / mantis / mantis_vp2033.c
1 /*
2         Mantis VP-2033 driver
3
4         Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)
5
6         This program is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10
11         This program is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with this program; if not, write to the Free Software
18         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <asm/irq.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24 #include <linux/interrupt.h>
25
26 #include "dmxdev.h"
27 #include "dvbdev.h"
28 #include "dvb_demux.h"
29 #include "dvb_frontend.h"
30 #include "dvb_net.h"
31
32 #include "tda1002x.h"
33 #include "mantis_common.h"
34 #include "mantis_vp2033.h"
35
36 #define MANTIS_MODEL_NAME       "VP-2033"
37 #define MANTIS_DEV_TYPE         "DVB-C"
38
39 struct tda1002x_config vp2033_tda1002x_cu1216_config = {
40         .demod_address = 0x18 >> 1,
41         .invert = 1,
42 };
43
44 struct tda10023_config vp2033_tda10023_cu1216_config = {
45         .demod_address = 0x18 >> 1,
46         .invert = 1,
47 };
48
49 static u8 read_pwm(struct mantis_pci *mantis)
50 {
51         struct i2c_adapter *adapter = &mantis->adapter;
52
53         u8 b = 0xff;
54         u8 pwm;
55         struct i2c_msg msg[] = {
56                 {.addr = 0x50, .flags = 0, .buf = &b, .len = 1},
57                 {.addr = 0x50, .flags = I2C_M_RD, .buf = &pwm, .len = 1}
58         };
59
60         if ((i2c_transfer(adapter, msg, 2) != 2)
61             || (pwm == 0xff))
62                 pwm = 0x48;
63
64         return pwm;
65 }
66
67 static int tda1002x_cu1216_tuner_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
68 {
69         struct mantis_pci *mantis = fe->dvb->priv;
70         struct i2c_adapter *adapter = &mantis->adapter;
71
72         u8 buf[6];
73         struct i2c_msg msg = {.addr = 0x60, .flags = 0, .buf = buf, .len = sizeof (buf) };
74         int i;
75
76 #define CU1216_IF 36125000
77 #define TUNER_MUL 62500
78
79         u32 div = (params->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
80
81         buf[0] = (div >> 8) & 0x7f;
82         buf[1] = div & 0xff;
83         buf[2] = 0xce;
84         buf[3] = (params->frequency < 150000000 ? 0x01 :
85                   params->frequency < 445000000 ? 0x02 : 0x04);
86         buf[4] = 0xde;
87         buf[5] = 0x20;
88
89         if (fe->ops.i2c_gate_ctrl)
90                 fe->ops.i2c_gate_ctrl(fe, 1);
91
92         if (i2c_transfer(adapter, &msg, 1) != 1)
93                 return -EIO;
94
95         /* wait for the pll lock */
96         msg.flags = I2C_M_RD;
97         msg.len = 1;
98         for (i = 0; i < 20; i++) {
99                 if (fe->ops.i2c_gate_ctrl)
100                         fe->ops.i2c_gate_ctrl(fe, 1);
101
102                 if (i2c_transfer(adapter, &msg, 1) == 1 && (buf[0] & 0x40))
103                         break;
104
105                 msleep(10);
106         }
107
108         /* switch the charge pump to the lower current */
109         msg.flags = 0;
110         msg.len = 2;
111         msg.buf = &buf[2];
112         buf[2] &= ~0x40;
113         if (fe->ops.i2c_gate_ctrl)
114                 fe->ops.i2c_gate_ctrl(fe, 1);
115
116         if (i2c_transfer(adapter, &msg, 1) != 1)
117                 return -EIO;
118
119         return 0;
120 }
121
122 static int vp2033_frontend_init(struct mantis_pci *mantis, struct dvb_frontend *fe)
123 {
124         struct i2c_adapter *adapter = &mantis->adapter;
125
126         dprintk(MANTIS_ERROR, 1, "Probing for CU1216 (DVB-C)");
127         fe = tda10021_attach(&vp2033_tda1002x_cu1216_config,
128                              adapter,
129                              read_pwm(mantis));
130
131         if (fe) {
132                 dprintk(MANTIS_ERROR, 1,
133                         "found Philips CU1216 DVB-C frontend (TDA10021) @ 0x%02x",
134                         vp2033_tda1002x_cu1216_config.demod_address);
135         } else {
136                 fe = tda10023_attach(&vp2033_tda10023_cu1216_config,
137                                      adapter,
138                                      read_pwm(mantis));
139
140                 if (fe) {
141                         dprintk(MANTIS_ERROR, 1,
142                                 "found Philips CU1216 DVB-C frontend (TDA10023) @ 0x%02x",
143                                 vp2033_tda1002x_cu1216_config.demod_address);
144                 }
145         }
146
147         if (fe) {
148                 fe->ops.tuner_ops.set_params = tda1002x_cu1216_tuner_set;
149                 dprintk(MANTIS_ERROR, 1, "Mantis DVB-C Philips CU1216 frontend attach success");
150         } else {
151                 return -1;
152         }
153
154         mantis->fe = fe;
155         dprintk(MANTIS_DEBUG, 1, "Done!");
156
157         return 0;
158 }
159
160 struct mantis_hwconfig vp2033_config = {
161         .model_name     = MANTIS_MODEL_NAME,
162         .dev_type       = MANTIS_DEV_TYPE,
163         .ts_size        = MANTIS_TS_204,
164
165         .baud_rate      = MANTIS_BAUD_9600,
166         .parity         = MANTIS_PARITY_NONE,
167         .bytes          = 0,
168
169         .frontend_init  = vp2033_frontend_init,
170 };