staging: usbip: bugfix for isochronous packets and optimization
[zen-stable.git] / drivers / media / dvb / frontends / dibx000_common.c
blobf6938f97feb49fd6f3dc258bafe253ebe06a54a2
1 #include <linux/i2c.h>
3 #include "dibx000_common.h"
5 static int debug;
6 module_param(debug, int, 0644);
7 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
9 #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiBX000: "); printk(args); printk("\n"); } } while (0)
11 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
13 u8 b[4] = {
14 (reg >> 8) & 0xff, reg & 0xff,
15 (val >> 8) & 0xff, val & 0xff,
17 struct i2c_msg msg = {
18 .addr = mst->i2c_addr,.flags = 0,.buf = b,.len = 4
21 return i2c_transfer(mst->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
24 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
26 u8 wb[2] = { reg >> 8, reg & 0xff };
27 u8 rb[2];
28 struct i2c_msg msg[2] = {
29 {.addr = mst->i2c_addr, .flags = 0, .buf = wb, .len = 2},
30 {.addr = mst->i2c_addr, .flags = I2C_M_RD, .buf = rb, .len = 2},
33 if (i2c_transfer(mst->i2c_adap, msg, 2) != 2)
34 dprintk("i2c read error on %d", reg);
36 return (rb[0] << 8) | rb[1];
39 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
41 int i = 100;
42 u16 status;
44 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
47 /* i2c timed out */
48 if (i == 0)
49 return -EREMOTEIO;
51 /* no acknowledge */
52 if ((status & 0x0080) == 0)
53 return -EREMOTEIO;
55 return 0;
58 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
60 u16 data;
61 u16 da;
62 u16 i;
63 u16 txlen = msg->len, len;
64 const u8 *b = msg->buf;
66 while (txlen) {
67 dibx000_read_word(mst, mst->base_reg + 2);
69 len = txlen > 8 ? 8 : txlen;
70 for (i = 0; i < len; i += 2) {
71 data = *b++ << 8;
72 if (i+1 < len)
73 data |= *b++;
74 dibx000_write_word(mst, mst->base_reg, data);
76 da = (((u8) (msg->addr)) << 9) |
77 (1 << 8) |
78 (1 << 7) |
79 (0 << 6) |
80 (0 << 5) |
81 ((len & 0x7) << 2) |
82 (0 << 1) |
83 (0 << 0);
85 if (txlen == msg->len)
86 da |= 1 << 5; /* start */
88 if (txlen-len == 0 && stop)
89 da |= 1 << 6; /* stop */
91 dibx000_write_word(mst, mst->base_reg+1, da);
93 if (dibx000_is_i2c_done(mst) != 0)
94 return -EREMOTEIO;
95 txlen -= len;
98 return 0;
101 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
103 u16 da;
104 u8 *b = msg->buf;
105 u16 rxlen = msg->len, len;
107 while (rxlen) {
108 len = rxlen > 8 ? 8 : rxlen;
109 da = (((u8) (msg->addr)) << 9) |
110 (1 << 8) |
111 (1 << 7) |
112 (0 << 6) |
113 (0 << 5) |
114 ((len & 0x7) << 2) |
115 (1 << 1) |
116 (0 << 0);
118 if (rxlen == msg->len)
119 da |= 1 << 5; /* start */
121 if (rxlen-len == 0)
122 da |= 1 << 6; /* stop */
123 dibx000_write_word(mst, mst->base_reg+1, da);
125 if (dibx000_is_i2c_done(mst) != 0)
126 return -EREMOTEIO;
128 rxlen -= len;
130 while (len) {
131 da = dibx000_read_word(mst, mst->base_reg);
132 *b++ = (da >> 8) & 0xff;
133 len--;
134 if (len >= 1) {
135 *b++ = da & 0xff;
136 len--;
141 return 0;
144 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
146 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
148 if (mst->device_rev < DIB7000MC && speed < 235)
149 speed = 235;
150 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
153 EXPORT_SYMBOL(dibx000_i2c_set_speed);
155 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
157 return I2C_FUNC_I2C;
160 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
161 enum dibx000_i2c_interface intf)
163 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
164 dprintk("selecting interface: %d", intf);
165 mst->selected_interface = intf;
166 return dibx000_write_word(mst, mst->base_reg + 4, intf);
168 return 0;
171 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
173 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
174 int msg_index;
175 int ret = 0;
177 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
178 for (msg_index = 0; msg_index < num; msg_index++) {
179 if (msg[msg_index].flags & I2C_M_RD) {
180 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
181 if (ret != 0)
182 return 0;
183 } else {
184 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
185 if (ret != 0)
186 return 0;
190 return num;
193 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
195 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
196 int msg_index;
197 int ret = 0;
199 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
200 for (msg_index = 0; msg_index < num; msg_index++) {
201 if (msg[msg_index].flags & I2C_M_RD) {
202 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
203 if (ret != 0)
204 return 0;
205 } else {
206 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
207 if (ret != 0)
208 return 0;
212 return num;
215 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
216 .master_xfer = dibx000_i2c_master_xfer_gpio12,
217 .functionality = dibx000_i2c_func,
220 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
221 .master_xfer = dibx000_i2c_master_xfer_gpio34,
222 .functionality = dibx000_i2c_func,
225 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
226 u8 addr, int onoff)
228 u16 val;
231 if (onoff)
232 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
233 else
234 val = 1 << 7;
236 if (mst->device_rev > DIB7000)
237 val <<= 1;
239 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
240 tx[1] = ((mst->base_reg + 1) & 0xff);
241 tx[2] = val >> 8;
242 tx[3] = val & 0xff;
244 return 0;
247 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
248 struct i2c_msg msg[], int num)
250 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
251 struct i2c_msg m[2 + num];
252 u8 tx_open[4], tx_close[4];
254 memset(m, 0, sizeof(struct i2c_msg) * (2 + num));
256 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
258 dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1);
259 m[0].addr = mst->i2c_addr;
260 m[0].buf = tx_open;
261 m[0].len = 4;
263 memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
265 dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
266 m[num + 1].addr = mst->i2c_addr;
267 m[num + 1].buf = tx_close;
268 m[num + 1].len = 4;
270 return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO;
273 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
274 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
275 .functionality = dibx000_i2c_func,
278 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
279 struct i2c_msg msg[], int num)
281 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
282 struct i2c_msg m[2 + num];
283 u8 tx_open[4], tx_close[4];
285 memset(m, 0, sizeof(struct i2c_msg) * (2 + num));
287 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
289 dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1);
290 m[0].addr = mst->i2c_addr;
291 m[0].buf = tx_open;
292 m[0].len = 4;
294 memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
296 dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
297 m[num + 1].addr = mst->i2c_addr;
298 m[num + 1].buf = tx_close;
299 m[num + 1].len = 4;
301 return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO;
304 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
305 .master_xfer = dibx000_i2c_gated_tuner_xfer,
306 .functionality = dibx000_i2c_func,
309 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
310 enum dibx000_i2c_interface intf,
311 int gating)
313 struct i2c_adapter *i2c = NULL;
315 switch (intf) {
316 case DIBX000_I2C_INTERFACE_TUNER:
317 if (gating)
318 i2c = &mst->gated_tuner_i2c_adap;
319 break;
320 case DIBX000_I2C_INTERFACE_GPIO_1_2:
321 if (!gating)
322 i2c = &mst->master_i2c_adap_gpio12;
323 break;
324 case DIBX000_I2C_INTERFACE_GPIO_3_4:
325 if (!gating)
326 i2c = &mst->master_i2c_adap_gpio34;
327 break;
328 case DIBX000_I2C_INTERFACE_GPIO_6_7:
329 if (gating)
330 i2c = &mst->master_i2c_adap_gpio67;
331 break;
332 default:
333 printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
334 break;
337 return i2c;
340 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
342 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
344 /* initialize the i2c-master by closing the gate */
345 u8 tx[4];
346 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
348 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
349 i2c_transfer(mst->i2c_adap, &m, 1);
350 mst->selected_interface = 0xff; // the first time force a select of the I2C
351 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
354 EXPORT_SYMBOL(dibx000_reset_i2c_master);
356 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
357 struct i2c_algorithm *algo, const char *name,
358 struct dibx000_i2c_master *mst)
360 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
361 i2c_adap->algo = algo;
362 i2c_adap->algo_data = NULL;
363 i2c_set_adapdata(i2c_adap, mst);
364 if (i2c_add_adapter(i2c_adap) < 0)
365 return -ENODEV;
366 return 0;
369 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
370 struct i2c_adapter *i2c_adap, u8 i2c_addr)
372 u8 tx[4];
373 struct i2c_msg m = {.addr = i2c_addr >> 1,.buf = tx,.len = 4 };
375 mst->device_rev = device_rev;
376 mst->i2c_adap = i2c_adap;
377 mst->i2c_addr = i2c_addr >> 1;
379 if (device_rev == DIB7000P || device_rev == DIB8000)
380 mst->base_reg = 1024;
381 else
382 mst->base_reg = 768;
384 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
385 if (i2c_adapter_init
386 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
387 "DiBX000 tuner I2C bus", mst) != 0)
388 printk(KERN_ERR
389 "DiBX000: could not initialize the tuner i2c_adapter\n");
391 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
392 if (i2c_adapter_init
393 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
394 "DiBX000 master GPIO12 I2C bus", mst) != 0)
395 printk(KERN_ERR
396 "DiBX000: could not initialize the master i2c_adapter\n");
398 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
399 if (i2c_adapter_init
400 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
401 "DiBX000 master GPIO34 I2C bus", mst) != 0)
402 printk(KERN_ERR
403 "DiBX000: could not initialize the master i2c_adapter\n");
405 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
406 if (i2c_adapter_init
407 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
408 "DiBX000 master GPIO67 I2C bus", mst) != 0)
409 printk(KERN_ERR
410 "DiBX000: could not initialize the master i2c_adapter\n");
412 /* initialize the i2c-master by closing the gate */
413 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
415 return i2c_transfer(i2c_adap, &m, 1) == 1;
418 EXPORT_SYMBOL(dibx000_init_i2c_master);
420 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
422 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
423 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
424 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
425 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
427 EXPORT_SYMBOL(dibx000_exit_i2c_master);
430 u32 systime(void)
432 struct timespec t;
434 t = current_kernel_time();
435 return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
437 EXPORT_SYMBOL(systime);
439 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
440 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
441 MODULE_LICENSE("GPL");