Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / dvb / frontends / dibx000_common.c
blob977211fec137bdcf668ef7a022f23548e38ae11b
1 #include <linux/i2c.h>
2 #include <linux/module.h>
4 #include "dibx000_common.h"
6 static int debug;
7 module_param(debug, int, 0644);
8 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
10 #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiBX000: "); printk(args); printk("\n"); } } while (0)
12 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
14 mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
15 mst->i2c_write_buffer[1] = reg & 0xff;
16 mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
17 mst->i2c_write_buffer[3] = val & 0xff;
19 memset(mst->msg, 0, sizeof(struct i2c_msg));
20 mst->msg[0].addr = mst->i2c_addr;
21 mst->msg[0].flags = 0;
22 mst->msg[0].buf = mst->i2c_write_buffer;
23 mst->msg[0].len = 4;
25 return i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
28 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
30 mst->i2c_write_buffer[0] = reg >> 8;
31 mst->i2c_write_buffer[1] = reg & 0xff;
33 memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
34 mst->msg[0].addr = mst->i2c_addr;
35 mst->msg[0].flags = 0;
36 mst->msg[0].buf = mst->i2c_write_buffer;
37 mst->msg[0].len = 2;
38 mst->msg[1].addr = mst->i2c_addr;
39 mst->msg[1].flags = I2C_M_RD;
40 mst->msg[1].buf = mst->i2c_read_buffer;
41 mst->msg[1].len = 2;
43 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
44 dprintk("i2c read error on %d", reg);
46 return (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
49 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
51 int i = 100;
52 u16 status;
54 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
57 /* i2c timed out */
58 if (i == 0)
59 return -EREMOTEIO;
61 /* no acknowledge */
62 if ((status & 0x0080) == 0)
63 return -EREMOTEIO;
65 return 0;
68 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
70 u16 data;
71 u16 da;
72 u16 i;
73 u16 txlen = msg->len, len;
74 const u8 *b = msg->buf;
76 while (txlen) {
77 dibx000_read_word(mst, mst->base_reg + 2);
79 len = txlen > 8 ? 8 : txlen;
80 for (i = 0; i < len; i += 2) {
81 data = *b++ << 8;
82 if (i+1 < len)
83 data |= *b++;
84 dibx000_write_word(mst, mst->base_reg, data);
86 da = (((u8) (msg->addr)) << 9) |
87 (1 << 8) |
88 (1 << 7) |
89 (0 << 6) |
90 (0 << 5) |
91 ((len & 0x7) << 2) |
92 (0 << 1) |
93 (0 << 0);
95 if (txlen == msg->len)
96 da |= 1 << 5; /* start */
98 if (txlen-len == 0 && stop)
99 da |= 1 << 6; /* stop */
101 dibx000_write_word(mst, mst->base_reg+1, da);
103 if (dibx000_is_i2c_done(mst) != 0)
104 return -EREMOTEIO;
105 txlen -= len;
108 return 0;
111 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
113 u16 da;
114 u8 *b = msg->buf;
115 u16 rxlen = msg->len, len;
117 while (rxlen) {
118 len = rxlen > 8 ? 8 : rxlen;
119 da = (((u8) (msg->addr)) << 9) |
120 (1 << 8) |
121 (1 << 7) |
122 (0 << 6) |
123 (0 << 5) |
124 ((len & 0x7) << 2) |
125 (1 << 1) |
126 (0 << 0);
128 if (rxlen == msg->len)
129 da |= 1 << 5; /* start */
131 if (rxlen-len == 0)
132 da |= 1 << 6; /* stop */
133 dibx000_write_word(mst, mst->base_reg+1, da);
135 if (dibx000_is_i2c_done(mst) != 0)
136 return -EREMOTEIO;
138 rxlen -= len;
140 while (len) {
141 da = dibx000_read_word(mst, mst->base_reg);
142 *b++ = (da >> 8) & 0xff;
143 len--;
144 if (len >= 1) {
145 *b++ = da & 0xff;
146 len--;
151 return 0;
154 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
156 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
158 if (mst->device_rev < DIB7000MC && speed < 235)
159 speed = 235;
160 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
163 EXPORT_SYMBOL(dibx000_i2c_set_speed);
165 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
167 return I2C_FUNC_I2C;
170 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
171 enum dibx000_i2c_interface intf)
173 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
174 dprintk("selecting interface: %d", intf);
175 mst->selected_interface = intf;
176 return dibx000_write_word(mst, mst->base_reg + 4, intf);
178 return 0;
181 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
183 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
184 int msg_index;
185 int ret = 0;
187 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
188 for (msg_index = 0; msg_index < num; msg_index++) {
189 if (msg[msg_index].flags & I2C_M_RD) {
190 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
191 if (ret != 0)
192 return 0;
193 } else {
194 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
195 if (ret != 0)
196 return 0;
200 return num;
203 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
205 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
206 int msg_index;
207 int ret = 0;
209 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
210 for (msg_index = 0; msg_index < num; msg_index++) {
211 if (msg[msg_index].flags & I2C_M_RD) {
212 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
213 if (ret != 0)
214 return 0;
215 } else {
216 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
217 if (ret != 0)
218 return 0;
222 return num;
225 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
226 .master_xfer = dibx000_i2c_master_xfer_gpio12,
227 .functionality = dibx000_i2c_func,
230 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
231 .master_xfer = dibx000_i2c_master_xfer_gpio34,
232 .functionality = dibx000_i2c_func,
235 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
236 u8 addr, int onoff)
238 u16 val;
241 if (onoff)
242 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
243 else
244 val = 1 << 7;
246 if (mst->device_rev > DIB7000)
247 val <<= 1;
249 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
250 tx[1] = ((mst->base_reg + 1) & 0xff);
251 tx[2] = val >> 8;
252 tx[3] = val & 0xff;
254 return 0;
257 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
258 struct i2c_msg msg[], int num)
260 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
262 if (num > 32) {
263 dprintk("%s: too much I2C message to be transmitted (%i).\
264 Maximum is 32", __func__, num);
265 return -ENOMEM;
268 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
270 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
272 /* open the gate */
273 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
274 mst->msg[0].addr = mst->i2c_addr;
275 mst->msg[0].buf = &mst->i2c_write_buffer[0];
276 mst->msg[0].len = 4;
278 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
280 /* close the gate */
281 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
282 mst->msg[num + 1].addr = mst->i2c_addr;
283 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
284 mst->msg[num + 1].len = 4;
286 return i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? num : -EIO;
289 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
290 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
291 .functionality = dibx000_i2c_func,
294 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
295 struct i2c_msg msg[], int num)
297 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
299 if (num > 32) {
300 dprintk("%s: too much I2C message to be transmitted (%i).\
301 Maximum is 32", __func__, num);
302 return -ENOMEM;
305 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
307 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
309 /* open the gate */
310 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
311 mst->msg[0].addr = mst->i2c_addr;
312 mst->msg[0].buf = &mst->i2c_write_buffer[0];
313 mst->msg[0].len = 4;
315 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
317 /* close the gate */
318 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
319 mst->msg[num + 1].addr = mst->i2c_addr;
320 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
321 mst->msg[num + 1].len = 4;
323 return i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? num : -EIO;
326 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
327 .master_xfer = dibx000_i2c_gated_tuner_xfer,
328 .functionality = dibx000_i2c_func,
331 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
332 enum dibx000_i2c_interface intf,
333 int gating)
335 struct i2c_adapter *i2c = NULL;
337 switch (intf) {
338 case DIBX000_I2C_INTERFACE_TUNER:
339 if (gating)
340 i2c = &mst->gated_tuner_i2c_adap;
341 break;
342 case DIBX000_I2C_INTERFACE_GPIO_1_2:
343 if (!gating)
344 i2c = &mst->master_i2c_adap_gpio12;
345 break;
346 case DIBX000_I2C_INTERFACE_GPIO_3_4:
347 if (!gating)
348 i2c = &mst->master_i2c_adap_gpio34;
349 break;
350 case DIBX000_I2C_INTERFACE_GPIO_6_7:
351 if (gating)
352 i2c = &mst->master_i2c_adap_gpio67;
353 break;
354 default:
355 printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
356 break;
359 return i2c;
362 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
364 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
366 /* initialize the i2c-master by closing the gate */
367 u8 tx[4];
368 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
370 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
371 i2c_transfer(mst->i2c_adap, &m, 1);
372 mst->selected_interface = 0xff; // the first time force a select of the I2C
373 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
376 EXPORT_SYMBOL(dibx000_reset_i2c_master);
378 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
379 struct i2c_algorithm *algo, const char *name,
380 struct dibx000_i2c_master *mst)
382 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
383 i2c_adap->algo = algo;
384 i2c_adap->algo_data = NULL;
385 i2c_set_adapdata(i2c_adap, mst);
386 if (i2c_add_adapter(i2c_adap) < 0)
387 return -ENODEV;
388 return 0;
391 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
392 struct i2c_adapter *i2c_adap, u8 i2c_addr)
394 u8 tx[4];
395 struct i2c_msg m = {.addr = i2c_addr >> 1,.buf = tx,.len = 4 };
397 mst->device_rev = device_rev;
398 mst->i2c_adap = i2c_adap;
399 mst->i2c_addr = i2c_addr >> 1;
401 if (device_rev == DIB7000P || device_rev == DIB8000)
402 mst->base_reg = 1024;
403 else
404 mst->base_reg = 768;
406 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
407 if (i2c_adapter_init
408 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
409 "DiBX000 tuner I2C bus", mst) != 0)
410 printk(KERN_ERR
411 "DiBX000: could not initialize the tuner i2c_adapter\n");
413 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
414 if (i2c_adapter_init
415 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
416 "DiBX000 master GPIO12 I2C bus", mst) != 0)
417 printk(KERN_ERR
418 "DiBX000: could not initialize the master i2c_adapter\n");
420 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
421 if (i2c_adapter_init
422 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
423 "DiBX000 master GPIO34 I2C bus", mst) != 0)
424 printk(KERN_ERR
425 "DiBX000: could not initialize the master i2c_adapter\n");
427 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
428 if (i2c_adapter_init
429 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
430 "DiBX000 master GPIO67 I2C bus", mst) != 0)
431 printk(KERN_ERR
432 "DiBX000: could not initialize the master i2c_adapter\n");
434 /* initialize the i2c-master by closing the gate */
435 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
437 return i2c_transfer(i2c_adap, &m, 1) == 1;
440 EXPORT_SYMBOL(dibx000_init_i2c_master);
442 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
444 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
445 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
446 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
447 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
449 EXPORT_SYMBOL(dibx000_exit_i2c_master);
452 u32 systime(void)
454 struct timespec t;
456 t = current_kernel_time();
457 return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
459 EXPORT_SYMBOL(systime);
461 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
462 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
463 MODULE_LICENSE("GPL");