V4L/DVB (13699): [Mantis, MB86A16] Initial checkin: Mantis, MB86A16
[linux-2.6/btrfs-unstable.git] / drivers / media / dvb / mantis / mantis_core.c
blob111227752452b3c9cd1197349f78e0d89cc318c2
1 /*
2 Mantis PCI bridge driver
4 Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)
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.
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.
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.
21 #include "mantis_common.h"
22 #include "mantis_core.h"
25 static int read_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)
27 int err;
28 struct i2c_msg msg[] = {
30 .addr = 0x50,
31 .flags = 0,
32 .buf = data,
33 .len = 1
34 },{
35 .addr = 0x50,
36 .flags = I2C_M_RD,
37 .buf = data,
38 .len = length
41 if ((err = i2c_transfer(&mantis->adapter, msg, 2)) < 0) {
42 dprintk(verbose, MANTIS_ERROR, 1,
43 "ERROR: i2c read: < err=%i d0=0x%02x d1=0x%02x >",
44 err, data[0], data[1]);
46 return err;
48 msleep_interruptible(2);
50 return 0;
53 static int write_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)
55 int err;
57 struct i2c_msg msg = {
58 .addr = 0x50,
59 .flags = 0,
60 .buf = data,
61 .len = length
64 if ((err = i2c_transfer(&mantis->adapter, &msg, 1)) < 0) {
65 dprintk(verbose, MANTIS_ERROR, 1,
66 "ERROR: i2c write: < err=%i length=0x%02x d0=0x%02x, d1=0x%02x >",
67 err, length, data[0], data[1]);
69 return err;
72 return 0;
75 static int get_subdevice_id(struct mantis_pci *mantis)
77 int err;
78 static u8 sub_device_id[2];
80 mantis->sub_device_id = 0;
81 sub_device_id[0] = 0xfc;
82 if ((err = read_eeprom_byte(mantis, &sub_device_id[0], 2)) < 0) {
83 dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
84 return err;
86 mantis->sub_device_id = (sub_device_id[0] << 8) | sub_device_id[1];
87 dprintk(verbose, MANTIS_ERROR, 1, "Sub Device ID=[0x%04x]",
88 mantis->sub_device_id);
90 return 0;
93 static int get_subvendor_id(struct mantis_pci *mantis)
95 int err;
96 static u8 sub_vendor_id[2];
98 mantis->sub_vendor_id = 0;
99 sub_vendor_id[0] = 0xfe;
100 if ((err = read_eeprom_byte(mantis, &sub_vendor_id[0], 2)) < 0) {
101 dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
102 return err;
104 mantis->sub_vendor_id = (sub_vendor_id[0] << 8) | sub_vendor_id[1];
105 dprintk(verbose, MANTIS_ERROR, 1, "Sub Vendor ID=[0x%04x]",
106 mantis->sub_vendor_id);
108 return 0;
111 static int get_mac_address(struct mantis_pci *mantis)
113 int err;
115 mantis->mac_address[0] = 0x08;
116 if ((err = read_eeprom_byte(mantis, &mantis->mac_address[0], 6)) < 0) {
117 dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
119 return err;
121 dprintk(verbose, MANTIS_ERROR, 1,
122 "MAC Address=[%02x:%02x:%02x:%02x:%02x:%02x]",
123 mantis->mac_address[0], mantis->mac_address[1],
124 mantis->mac_address[2], mantis->mac_address[3],
125 mantis->mac_address[4], mantis->mac_address[5]);
127 return 0;
131 int mantis_core_init(struct mantis_pci *mantis)
133 int err = 0;
135 if ((err = mantis_i2c_init(mantis)) < 0) {
136 dprintk(verbose, MANTIS_ERROR, 1, "Mantis I2C init failed");
137 return err;
139 if ((err = get_mac_address(mantis)) < 0) {
140 dprintk(verbose, MANTIS_ERROR, 1, "get MAC address failed");
141 return err;
143 if ((err = get_subvendor_id(mantis)) < 0) {
144 dprintk(verbose, MANTIS_ERROR, 1, "get Sub vendor ID failed");
145 return err;
147 if ((err = get_subdevice_id(mantis)) < 0) {
148 dprintk(verbose, MANTIS_ERROR, 1, "get Sub device ID failed");
149 return err;
151 if ((err = mantis_dma_init(mantis)) < 0) {
152 dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA init failed");
153 return err;
155 if ((err = mantis_dvb_init(mantis)) < 0) {
156 dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB init failed");
157 return err;
160 return 0;
163 int mantis_core_exit(struct mantis_pci *mantis)
166 mantis_dma_stop(mantis);
167 dprintk(verbose, MANTIS_ERROR, 1, "DMA engine stopping");
168 if (mantis_dma_exit(mantis) < 0)
169 dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");
170 if (mantis_dvb_exit(mantis) < 0)
171 dprintk(verbose, MANTIS_ERROR, 1, "DVB exit failed");
172 if (mantis_i2c_exit(mantis) < 0)
173 dprintk(verbose, MANTIS_ERROR, 1, "I2C adapter delete.. failed");
175 return 0;
178 void gpio_set_bits(struct mantis_pci *mantis, u32 bitpos, u8 value)
180 u32 reg;
182 if (value)
183 reg = 0x0000;
184 else
185 reg = 0xffff;
187 reg = (value << bitpos);
189 mmwrite(mmread(MANTIS_GPIF_ADDR) | reg, MANTIS_GPIF_ADDR);
190 mmwrite(0x00, MANTIS_GPIF_DOUT);
191 udelay(100);
192 mmwrite(mmread(MANTIS_GPIF_ADDR) | reg, MANTIS_GPIF_ADDR);
193 mmwrite(0x00, MANTIS_GPIF_DOUT);
197 //direction = 0 , no CI passthrough ; 1 , CI passthrough
198 void mantis_set_direction(struct mantis_pci *mantis, int direction)
200 u32 reg;
202 reg = mmread(0x28);
203 dprintk(verbose, MANTIS_DEBUG, 1, "TS direction setup");
204 if (direction == 0x01) { //to CI
205 reg |= 0x04;
206 mmwrite(reg, 0x28);
207 reg &= 0xff - 0x04;
208 mmwrite(reg, 0x28);
209 } else {
210 reg &= 0xff - 0x04;
211 mmwrite(reg, 0x28);
212 reg |= 0x04;
213 mmwrite(reg, 0x28);