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