V4L/DVB (13722): [Mantis] Revert 13560
[cascardo/linux.git] / 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                 mantis->hwconfig = &vp2033_mantis_config;
115                 break;
116         case MANTIS_VP_3030_DVB_T:      // VP-3030
117                 mantis->hwconfig = &vp3030_mantis_config;
118                 break;
119         default:
120                 mantis->hwconfig = &unknown_device;
121                 break;
122         }
123 }
124
125 int mantis_core_init(struct mantis_pci *mantis)
126 {
127         int err = 0;
128
129         mantis_load_config(mantis);
130         dprintk(verbose, MANTIS_ERROR, 0, "found a %s PCI %s device on (%02x:%02x.%x),\n",
131                 mantis->hwconfig->model_name, mantis->hwconfig->dev_type,
132                 mantis->pdev->bus->number, PCI_SLOT(mantis->pdev->devfn), PCI_FUNC(mantis->pdev->devfn));
133         dprintk(verbose, MANTIS_ERROR, 0, "    Mantis Rev %d [%04x:%04x], ",
134                 mantis->revision,
135                 mantis->subsystem_vendor, mantis->subsystem_device);
136         dprintk(verbose, MANTIS_ERROR, 0,
137                 "irq: %d, latency: %d\n    memory: 0x%lx, mmio: 0x%p\n",
138                 mantis->pdev->irq, mantis->latency,
139                 mantis->mantis_addr, mantis->mantis_mmio);
140
141         if ((err = mantis_i2c_init(mantis)) < 0) {
142                 dprintk(verbose, MANTIS_ERROR, 1, "Mantis I2C init failed");
143                 return err;
144         }
145         if ((err = get_mac_address(mantis)) < 0) {
146                 dprintk(verbose, MANTIS_ERROR, 1, "get MAC address failed");
147                 return err;
148         }
149         if ((err = mantis_dma_init(mantis)) < 0) {
150                 dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA init failed");
151                 return err;
152         }
153         if ((err = mantis_dvb_init(mantis)) < 0) {
154                 dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB init failed");
155                 return err;
156         }
157
158         return 0;
159 }
160
161 int mantis_core_exit(struct mantis_pci *mantis)
162 {
163         mantis_dma_stop(mantis);
164         dprintk(verbose, MANTIS_ERROR, 1, "DMA engine stopping");
165         if (mantis_dma_exit(mantis) < 0)
166                 dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");
167         if (mantis_dvb_exit(mantis) < 0)
168                 dprintk(verbose, MANTIS_ERROR, 1, "DVB exit failed");
169         if (mantis_i2c_exit(mantis) < 0)
170                 dprintk(verbose, MANTIS_ERROR, 1, "I2C adapter delete.. failed");
171
172         return 0;
173 }
174
175 // Turn the given bit on or off.
176 void gpio_set_bits(struct mantis_pci *mantis, u32 bitpos, u8 value)
177 {
178         u32 currVal, newVal;
179
180         currVal = mmread(MANTIS_GPIF_ADDR);
181
182         if (value)
183                 newVal = currVal | (1 << bitpos);
184         else
185                 newVal = currVal & (~(1 << bitpos));
186
187         mmwrite(newVal, MANTIS_GPIF_ADDR);
188         mmwrite(0x00, MANTIS_GPIF_DOUT);
189         udelay(100);
190 }
191
192 //direction = 0 , no CI passthrough ; 1 , CI passthrough
193 void mantis_set_direction(struct mantis_pci *mantis, int direction)
194 {
195         u32 reg;
196
197         reg = mmread(0x28);
198         dprintk(verbose, MANTIS_DEBUG, 1, "TS direction setup");
199         if (direction == 0x01) { //to CI
200                 reg |= 0x04;
201                 mmwrite(reg, 0x28);
202                 reg &= 0xff - 0x04;
203                 mmwrite(reg, 0x28);
204         } else {
205                 reg &= 0xff - 0x04;
206                 mmwrite(reg, 0x28);
207                 reg |= 0x04;
208                 mmwrite(reg, 0x28);
209         }
210 }