V4L/DVB (13723): [Mantis/VP-2040, Terratec Cinergy C] Add support for the Cinergy...
[safe/jmp/linux-2.6] / drivers / media / dvb / mantis / mantis_core.c
1 /*
2         Mantis PCI bridge 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 "mantis_common.h"
22 #include "mantis_core.h"
23 #include "mantis_vp1033.h"
24 #include "mantis_vp1034.h"
25 #include "mantis_vp2033.h"
26 #include "mantis_vp2040.h"
27 #include "mantis_vp3030.h"
28
29 static int read_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)
30 {
31         int err;
32         struct i2c_msg msg[] = {
33                 {
34                         .addr = 0x50,
35                         .flags = 0,
36                         .buf = data,
37                         .len = 1
38                 },{
39                         .addr = 0x50,
40                         .flags = I2C_M_RD,
41                         .buf = data,
42                         .len = length
43                 },
44         };
45         if ((err = i2c_transfer(&mantis->adapter, msg, 2)) < 0) {
46                 dprintk(verbose, MANTIS_ERROR, 1,
47                         "ERROR: i2c read: < err=%i d0=0x%02x d1=0x%02x >",
48                         err, data[0], data[1]);
49
50                 return err;
51         }
52
53         return 0;
54 }
55
56 static int write_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)
57 {
58         int err;
59
60         struct i2c_msg msg = {
61                 .addr = 0x50,
62                 .flags = 0,
63                 .buf = data,
64                 .len = length
65         };
66
67         if ((err = i2c_transfer(&mantis->adapter, &msg, 1)) < 0) {
68                 dprintk(verbose, MANTIS_ERROR, 1,
69                         "ERROR: i2c write: < err=%i length=0x%02x d0=0x%02x, d1=0x%02x >",
70                         err, length, data[0], data[1]);
71
72                 return err;
73         }
74
75         return 0;
76 }
77
78 static int get_mac_address(struct mantis_pci *mantis)
79 {
80         int err;
81
82         mantis->mac_address[0] = 0x08;
83         if ((err = read_eeprom_byte(mantis, &mantis->mac_address[0], 6)) < 0) {
84                 dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
85
86                 return err;
87         }
88         dprintk(verbose, MANTIS_ERROR, 0,
89                 "    MAC Address=[%02x:%02x:%02x:%02x:%02x:%02x]\n",
90                 mantis->mac_address[0], mantis->mac_address[1],
91                 mantis->mac_address[2], mantis->mac_address[3],
92                 mantis->mac_address[4], mantis->mac_address[5]);
93
94         return 0;
95 }
96
97 #define MANTIS_MODEL_UNKNOWN    "UNKNOWN"
98 #define MANTIS_DEV_UNKNOWN      "UNKNOWN"
99
100 struct mantis_hwconfig unknown_device = {
101         .model_name     = MANTIS_MODEL_UNKNOWN,
102         .dev_type       = MANTIS_DEV_UNKNOWN,
103 };
104
105 static void mantis_load_config(struct mantis_pci *mantis)
106 {
107         switch (mantis->subsystem_device) {
108         case MANTIS_VP_1033_DVB_S:      // VP-1033
109                 mantis->hwconfig = &vp1033_mantis_config;
110                 break;
111         case MANTIS_VP_1034_DVB_S:      // VP-1034
112                 mantis->hwconfig = &vp1034_mantis_config;
113                 break;
114         case MANTIS_VP_2033_DVB_C:      // VP-2033
115                 mantis->hwconfig = &vp2033_mantis_config;
116                 break;
117         case TERRATEC_CINERGY_C_PCI:    // VP-2040 clone
118                 mantis->hwconfig = &vp2040_mantis_config;
119                 break;
120         case MANTIS_VP_3030_DVB_T:      // VP-3030
121                 mantis->hwconfig = &vp3030_mantis_config;
122                 break;
123         default:
124                 mantis->hwconfig = &unknown_device;
125                 break;
126         }
127 }
128
129 int mantis_core_init(struct mantis_pci *mantis)
130 {
131         int err = 0;
132
133         mantis_load_config(mantis);
134         dprintk(verbose, MANTIS_ERROR, 0, "found a %s PCI %s device on (%02x:%02x.%x),\n",
135                 mantis->hwconfig->model_name, mantis->hwconfig->dev_type,
136                 mantis->pdev->bus->number, PCI_SLOT(mantis->pdev->devfn), PCI_FUNC(mantis->pdev->devfn));
137         dprintk(verbose, MANTIS_ERROR, 0, "    Mantis Rev %d [%04x:%04x], ",
138                 mantis->revision,
139                 mantis->subsystem_vendor, mantis->subsystem_device);
140         dprintk(verbose, MANTIS_ERROR, 0,
141                 "irq: %d, latency: %d\n    memory: 0x%lx, mmio: 0x%p\n",
142                 mantis->pdev->irq, mantis->latency,
143                 mantis->mantis_addr, mantis->mantis_mmio);
144
145         if ((err = mantis_i2c_init(mantis)) < 0) {
146                 dprintk(verbose, MANTIS_ERROR, 1, "Mantis I2C init failed");
147                 return err;
148         }
149         if ((err = get_mac_address(mantis)) < 0) {
150                 dprintk(verbose, MANTIS_ERROR, 1, "get MAC address failed");
151                 return err;
152         }
153         if ((err = mantis_dma_init(mantis)) < 0) {
154                 dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA init failed");
155                 return err;
156         }
157         if ((err = mantis_dvb_init(mantis)) < 0) {
158                 dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB init failed");
159                 return err;
160         }
161
162         return 0;
163 }
164
165 int mantis_core_exit(struct mantis_pci *mantis)
166 {
167         mantis_dma_stop(mantis);
168         dprintk(verbose, MANTIS_ERROR, 1, "DMA engine stopping");
169         if (mantis_dma_exit(mantis) < 0)
170                 dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");
171         if (mantis_dvb_exit(mantis) < 0)
172                 dprintk(verbose, MANTIS_ERROR, 1, "DVB exit failed");
173         if (mantis_i2c_exit(mantis) < 0)
174                 dprintk(verbose, MANTIS_ERROR, 1, "I2C adapter delete.. failed");
175
176         return 0;
177 }
178
179 // Turn the given bit on or off.
180 void gpio_set_bits(struct mantis_pci *mantis, u32 bitpos, u8 value)
181 {
182         u32 currVal, newVal;
183
184         currVal = mmread(MANTIS_GPIF_ADDR);
185
186         if (value)
187                 newVal = currVal | (1 << bitpos);
188         else
189                 newVal = currVal & (~(1 << bitpos));
190
191         mmwrite(newVal, MANTIS_GPIF_ADDR);
192         mmwrite(0x00, MANTIS_GPIF_DOUT);
193         udelay(100);
194 }
195
196 //direction = 0 , no CI passthrough ; 1 , CI passthrough
197 void mantis_set_direction(struct mantis_pci *mantis, int direction)
198 {
199         u32 reg;
200
201         reg = mmread(0x28);
202         dprintk(verbose, MANTIS_DEBUG, 1, "TS direction setup");
203         if (direction == 0x01) { //to CI
204                 reg |= 0x04;
205                 mmwrite(reg, 0x28);
206                 reg &= 0xff - 0x04;
207                 mmwrite(reg, 0x28);
208         } else {
209                 reg &= 0xff - 0x04;
210                 mmwrite(reg, 0x28);
211                 reg |= 0x04;
212                 mmwrite(reg, 0x28);
213         }
214 }