V4L/DVB (13699): [Mantis, MB86A16] Initial checkin: Mantis, MB86A16
[deliverable/linux.git] / drivers / media / dvb / mantis / mantis_i2c.c
CommitLineData
41e840b1
MA
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 <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <asm/io.h>
26#include <linux/ioport.h>
27#include <asm/pgtable.h>
28#include <asm/page.h>
29#include "mantis_common.h"
30
31#define I2C_HW_B_MANTIS 0x1c
32
33static int mantis_ack_wait(struct mantis_pci *mantis)
34{
35 int rc = 0;
36
37 if (wait_event_interruptible_timeout(mantis->i2c_wq,
38 mantis->mantis_int_stat & MANTIS_INT_I2CRACK,
39 msecs_to_jiffies(50)) == -ERESTARTSYS)
40
41 rc = -EREMOTEIO;
42/*
43 // Wait till we are done
44 while (mantis->mantis_int_stat & MANTIS_INT_I2CRACK){
45 if (mantis->mantis_int_stat & MANTIS_INT_I2CDONE) {
46 mantis->mantis_int_stat &= ~MANTIS_INT_I2CRACK;
47// dprintk(verbose, MANTIS_DEBUG, 1, "SLAVE RACK 'ed .. Waiting for I2CDONE");
48 break;
49 }
50 }
51
52 if (mantis->mantis_int_stat & MANTIS_INT_I2CDONE) {
53// dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Int I2CDONE");
54 rc = 1;
55 }
56
57 mantis->mantis_int_stat &= ~MANTIS_INT_I2CDONE;
58*/
59 // ..
60 if (mantis->mantis_int_stat & MANTIS_INT_I2CRACK)
61 msleep_interruptible(10);
62
63 return rc;
64}
65
66static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
67{
68 u32 rxd, i;
69
70 dprintk(verbose, MANTIS_DEBUG, 1, "Address=[0x%02x]", msg->addr);
71 for (i = 0; i < msg->len; i++) {
72 rxd = (msg->addr << 25) | (1 << 24)
73 | MANTIS_I2C_RATE_3
74 | MANTIS_I2C_STOP
75 | MANTIS_I2C_PGMODE;
76
77 if (i == (msg->len - 1))
78 rxd &= ~MANTIS_I2C_STOP;
79
80 mmwrite(rxd, MANTIS_I2CDATA_CTL);
81 if (mantis_ack_wait(mantis) < 0) {
82 dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<R>");
83 return -EIO;
84 }
85 rxd = mmread(MANTIS_I2CDATA_CTL);
86 msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
87 dprintk(verbose, MANTIS_DEBUG, 1,
88 "Data<R[%d]>=[0x%02x]", i, msg->buf[i]);
89
90 msleep_interruptible(2);
91 }
92
93 return 0;
94}
95
96static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
97{
98 int i;
99 u32 txd = 0;
100
101 dprintk(verbose, MANTIS_DEBUG, 1, "Address=[0x%02x]", msg->addr);
102 for (i = 0; i < msg->len; i++) {
103 dprintk(verbose, MANTIS_DEBUG, 1, "Data<W[%d]>=[0x%02x]", i, msg->buf[i]);
104 txd = (msg->addr << 25) | (msg->buf[i] << 8)
105 | MANTIS_I2C_RATE_3
106 | MANTIS_I2C_STOP
107 | MANTIS_I2C_PGMODE;
108
109 if (i == (msg->len - 1))
110 txd &= ~MANTIS_I2C_STOP;
111
112 mmwrite(txd, MANTIS_I2CDATA_CTL);
113 if (mantis_ack_wait(mantis) < 0) {
114 dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<W>");
115 return -1;
116 }
117 udelay(500);
118 }
119
120 return 0;
121}
122
123static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
124{
125 int ret = 0, i;
126 struct mantis_pci *mantis;
127
128 mantis = i2c_get_adapdata(adapter);
129 for (i = 0; i < num; i++) {
130 if (msgs[i].flags & I2C_M_RD)
131 ret = mantis_i2c_read(mantis, &msgs[i]);
132 else
133 ret = mantis_i2c_write(mantis, &msgs[i]);
134
135 if (ret < 0)
136 return ret;
137 }
138
139 return num;
140}
141
142static u32 mantis_i2c_func(struct i2c_adapter *adapter)
143{
144 return I2C_FUNC_SMBUS_EMUL;
145}
146
147static struct i2c_algorithm mantis_algo = {
148 .master_xfer = mantis_i2c_xfer,
149 .functionality = mantis_i2c_func,
150};
151
152static struct i2c_adapter mantis_i2c_adapter = {
153 .owner = THIS_MODULE,
154 .name = "Mantis I2C",
155 .id = I2C_HW_B_MANTIS,
156 .class = I2C_CLASS_TV_DIGITAL,
157 .algo = &mantis_algo,
158};
159
160int __devinit mantis_i2c_init(struct mantis_pci *mantis)
161{
162 u32 intstat;
163
164 memcpy(&mantis->adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
165 i2c_set_adapdata(&mantis->adapter, mantis);
166 mantis->i2c_rc = i2c_add_adapter(&mantis->adapter);
167 if (mantis->i2c_rc < 0)
168 return mantis->i2c_rc;
169
170 dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");
171
172 // Clear all interrupts
173 intstat = mmread(MANTIS_INT_STAT);
174 mmwrite(intstat, MANTIS_INT_STAT);
175
176 mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE,
177 MANTIS_INT_MASK);
178
179 dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]",
180 mmread(MANTIS_INT_STAT), mmread(MANTIS_INT_MASK));
181
182 return 0;
183}
184
185int __devexit mantis_i2c_exit(struct mantis_pci *mantis)
186{
187 dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
188 return i2c_del_adapter(&mantis->adapter);
189}
This page took 0.029618 seconds and 5 git commands to generate.