aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/b2c2/flexcop-i2c.c
diff options
context:
space:
mode:
authorJohannes Stezenbach <js@linuxtv.org>2005-05-17 00:54:10 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-05-17 10:59:24 -0400
commit2add87a95068d6457d4e5824d0417d39007665a4 (patch)
treef2aa0373421b0bd78ce900a41fa5a72b853e66de /drivers/media/dvb/b2c2/flexcop-i2c.c
parent1ec359729960f7896db8f642454e603d22519d20 (diff)
[PATCH] dvb: b2c2/flexcop driver refactoring part 2: add modular Flexcop driver
b2c2/flexcop driver refactoring to support PCI and USB based cards part 2: add modular Flexcop driver Signed-off-by: Patrick Boettcher <pb@linuxtv.org> Signed-off-by: Johannes Stezenbach <js@linuxtv.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/media/dvb/b2c2/flexcop-i2c.c')
-rw-r--r--drivers/media/dvb/b2c2/flexcop-i2c.c204
1 files changed, 204 insertions, 0 deletions
diff --git a/drivers/media/dvb/b2c2/flexcop-i2c.c b/drivers/media/dvb/b2c2/flexcop-i2c.c
new file mode 100644
index 000000000000..736251f393c2
--- /dev/null
+++ b/drivers/media/dvb/b2c2/flexcop-i2c.c
@@ -0,0 +1,204 @@
1/*
2 * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
3 *
4 * flexcop-i2c.c - flexcop internal 2Wire bus (I2C) and dvb i2c initialization
5 *
6 * see flexcop.c for copyright information.
7 */
8#include "flexcop.h"
9
10#define FC_MAX_I2C_RETRIES 100000
11
12static int flexcop_i2c_operation(struct flexcop_device *fc, flexcop_ibi_value *r100, int max_ack_errors)
13{
14 int i,ack_errors = 0;
15 flexcop_ibi_value r;
16
17 r100->tw_sm_c_100.working_start = 1;
18 deb_i2c("r100 before: %08x\n",r100->raw);
19
20 fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
21 fc->write_ibi_reg(fc, tw_sm_c_100, *r100); /* initiating i2c operation */
22
23 for (i = 0; i < FC_MAX_I2C_RETRIES; i++) {
24 r = fc->read_ibi_reg(fc, tw_sm_c_100);
25
26 if (!r.tw_sm_c_100.no_base_addr_ack_error) {
27 if (r.tw_sm_c_100.st_done) { /* && !r.tw_sm_c_100.working_start */
28 *r100 = r;
29 deb_i2c("i2c success\n");
30 return 0;
31 }
32 } else {
33 deb_i2c("suffering from an i2c ack_error\n");
34 if (++ack_errors >= max_ack_errors)
35 break;
36
37 fc->write_ibi_reg(fc, tw_sm_c_100, ibi_zero);
38 fc->write_ibi_reg(fc, tw_sm_c_100, *r100);
39 }
40 }
41 deb_i2c("tried %d times i2c operation, never finished or too many ack errors.\n",i);
42 return -EREMOTEIO;
43}
44
45static int flexcop_i2c_read4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
46{
47 flexcop_ibi_value r104;
48 int len = r100.tw_sm_c_100.total_bytes, /* remember total_bytes is buflen-1 */
49 ret;
50
51 if ((ret = flexcop_i2c_operation(fc,&r100,30)) != 0)
52 return ret;
53
54 r104 = fc->read_ibi_reg(fc,tw_sm_c_104);
55
56 deb_i2c("read: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
57
58 /* there is at least one byte, otherwise we wouldn't be here */
59 buf[0] = r100.tw_sm_c_100.data1_reg;
60
61 if (len > 0) buf[1] = r104.tw_sm_c_104.data2_reg;
62 if (len > 1) buf[2] = r104.tw_sm_c_104.data3_reg;
63 if (len > 2) buf[3] = r104.tw_sm_c_104.data4_reg;
64
65 return 0;
66}
67
68static int flexcop_i2c_write4(struct flexcop_device *fc, flexcop_ibi_value r100, u8 *buf)
69{
70 flexcop_ibi_value r104;
71 int len = r100.tw_sm_c_100.total_bytes; /* remember total_bytes is buflen-1 */
72 r104.raw = 0;
73
74 /* there is at least one byte, otherwise we wouldn't be here */
75 r100.tw_sm_c_100.data1_reg = buf[0];
76
77 r104.tw_sm_c_104.data2_reg = len > 0 ? buf[1] : 0;
78 r104.tw_sm_c_104.data3_reg = len > 1 ? buf[2] : 0;
79 r104.tw_sm_c_104.data4_reg = len > 2 ? buf[3] : 0;
80
81 deb_i2c("write: r100: %08x, r104: %08x\n",r100.raw,r104.raw);
82
83 /* write the additional i2c data before doing the actual i2c operation */
84 fc->write_ibi_reg(fc,tw_sm_c_104,r104);
85
86 return flexcop_i2c_operation(fc,&r100,30);
87}
88
89/* master xfer callback for demodulator */
90static int flexcop_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
91{
92 struct flexcop_device *fc = i2c_get_adapdata(i2c_adap);
93 int i, ret = 0;
94
95 if (down_interruptible(&fc->i2c_sem))
96 return -ERESTARTSYS;
97
98 /* reading */
99 if (num == 2 &&
100 msgs[0].flags == 0 &&
101 msgs[1].flags == I2C_M_RD &&
102 msgs[0].buf != NULL &&
103 msgs[1].buf != NULL) {
104
105 ret = fc->i2c_request(fc, FC_READ, FC_I2C_PORT_DEMOD, msgs[0].addr, msgs[0].buf[0], msgs[1].buf, msgs[1].len);
106
107 } else for (i = 0; i < num; i++) { /* writing command */
108 if (msgs[i].flags != 0 || msgs[i].buf == NULL || msgs[i].len < 2) {
109 ret = -EINVAL;
110 break;
111 }
112
113 ret = fc->i2c_request(fc, FC_WRITE, FC_I2C_PORT_DEMOD, msgs[i].addr, msgs[i].buf[0], &msgs[i].buf[1], msgs[i].len - 1);
114 }
115
116 if (ret < 0)
117 err("i2c master_xfer failed");
118 else
119 ret = num;
120
121 up(&fc->i2c_sem);
122
123 return ret;
124}
125
126int flexcop_i2c_request(struct flexcop_device *fc, flexcop_access_op_t op,
127 flexcop_i2c_port_t port, u8 chipaddr, u8 addr, u8 *buf, u16 len)
128{
129 int ret;
130 u16 bytes_to_transfer;
131 flexcop_ibi_value r100;
132
133 deb_i2c("op = %d\n",op);
134 r100.raw = 0;
135 r100.tw_sm_c_100.chipaddr = chipaddr;
136 r100.tw_sm_c_100.twoWS_rw = op;
137 r100.tw_sm_c_100.twoWS_port_reg = port;
138
139 while (len != 0) {
140 bytes_to_transfer = len > 4 ? 4 : len;
141
142 r100.tw_sm_c_100.total_bytes = bytes_to_transfer - 1;
143 r100.tw_sm_c_100.baseaddr = addr;
144
145 if (op == FC_READ)
146 ret = flexcop_i2c_read4(fc, r100, buf);
147 else
148 ret = flexcop_i2c_write4(fc,r100, buf);
149
150 if (ret < 0)
151 return ret;
152
153 buf += bytes_to_transfer;
154 addr += bytes_to_transfer;
155 len -= bytes_to_transfer;
156 };
157
158 return 0;
159}
160/* exported for PCI i2c */
161EXPORT_SYMBOL(flexcop_i2c_request);
162
163static u32 flexcop_i2c_func(struct i2c_adapter *adapter)
164{
165 return I2C_FUNC_I2C;
166}
167
168static struct i2c_algorithm flexcop_algo = {
169 .name = "FlexCop I2C algorithm",
170 .id = I2C_ALGO_BIT,
171 .master_xfer = flexcop_master_xfer,
172 .functionality = flexcop_i2c_func,
173};
174
175int flexcop_i2c_init(struct flexcop_device *fc)
176{
177 int ret;
178
179 sema_init(&fc->i2c_sem,1);
180
181 memset(&fc->i2c_adap, 0, sizeof(struct i2c_adapter));
182 strncpy(fc->i2c_adap.name, "B2C2 FlexCop device",I2C_NAME_SIZE);
183
184 i2c_set_adapdata(&fc->i2c_adap,fc);
185
186 fc->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
187 fc->i2c_adap.algo = &flexcop_algo;
188 fc->i2c_adap.algo_data = NULL;
189 fc->i2c_adap.id = I2C_ALGO_BIT;
190
191 if ((ret = i2c_add_adapter(&fc->i2c_adap)) < 0)
192 return ret;
193
194 fc->init_state |= FC_STATE_I2C_INIT;
195 return 0;
196}
197
198void flexcop_i2c_exit(struct flexcop_device *fc)
199{
200 if (fc->init_state & FC_STATE_I2C_INIT)
201 i2c_del_adapter(&fc->i2c_adap);
202
203 fc->init_state &= ~FC_STATE_I2C_INIT;
204}