2 #include <linux/mutex.h>
4 #include "dibx000_common.h"
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)
16 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
17 dprintk("could not acquire lock");
21 mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
22 mst->i2c_write_buffer[1] = reg & 0xff;
23 mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
24 mst->i2c_write_buffer[3] = val & 0xff;
26 memset(mst->msg, 0, sizeof(struct i2c_msg));
27 mst->msg[0].addr = mst->i2c_addr;
28 mst->msg[0].flags = 0;
29 mst->msg[0].buf = mst->i2c_write_buffer;
32 ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
33 mutex_unlock(&mst->i2c_buffer_lock);
38 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
42 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
43 dprintk("could not acquire lock");
47 mst->i2c_write_buffer[0] = reg >> 8;
48 mst->i2c_write_buffer[1] = reg & 0xff;
50 memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
51 mst->msg[0].addr = mst->i2c_addr;
52 mst->msg[0].flags = 0;
53 mst->msg[0].buf = mst->i2c_write_buffer;
55 mst->msg[1].addr = mst->i2c_addr;
56 mst->msg[1].flags = I2C_M_RD;
57 mst->msg[1].buf = mst->i2c_read_buffer;
60 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
61 dprintk("i2c read error on %d", reg);
63 ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
64 mutex_unlock(&mst->i2c_buffer_lock);
69 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
74 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
82 if ((status & 0x0080) == 0)
88 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
93 u16 txlen = msg->len, len;
94 const u8 *b = msg->buf;
97 dibx000_read_word(mst, mst->base_reg + 2);
99 len = txlen > 8 ? 8 : txlen;
100 for (i = 0; i < len; i += 2) {
104 dibx000_write_word(mst, mst->base_reg, data);
106 da = (((u8) (msg->addr)) << 9) |
115 if (txlen == msg->len)
116 da |= 1 << 5; /* start */
118 if (txlen-len == 0 && stop)
119 da |= 1 << 6; /* stop */
121 dibx000_write_word(mst, mst->base_reg+1, da);
123 if (dibx000_is_i2c_done(mst) != 0)
131 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
135 u16 rxlen = msg->len, len;
138 len = rxlen > 8 ? 8 : rxlen;
139 da = (((u8) (msg->addr)) << 9) |
148 if (rxlen == msg->len)
149 da |= 1 << 5; /* start */
152 da |= 1 << 6; /* stop */
153 dibx000_write_word(mst, mst->base_reg+1, da);
155 if (dibx000_is_i2c_done(mst) != 0)
161 da = dibx000_read_word(mst, mst->base_reg);
162 *b++ = (da >> 8) & 0xff;
174 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
176 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
178 if (mst->device_rev < DIB7000MC && speed < 235)
180 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
183 EXPORT_SYMBOL(dibx000_i2c_set_speed);
185 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
190 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
191 enum dibx000_i2c_interface intf)
193 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
194 dprintk("selecting interface: %d", intf);
195 mst->selected_interface = intf;
196 return dibx000_write_word(mst, mst->base_reg + 4, intf);
201 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
203 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
207 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
208 for (msg_index = 0; msg_index < num; msg_index++) {
209 if (msg[msg_index].flags & I2C_M_RD) {
210 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
214 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
223 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
225 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
229 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
230 for (msg_index = 0; msg_index < num; msg_index++) {
231 if (msg[msg_index].flags & I2C_M_RD) {
232 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
236 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
245 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
246 .master_xfer = dibx000_i2c_master_xfer_gpio12,
247 .functionality = dibx000_i2c_func,
250 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
251 .master_xfer = dibx000_i2c_master_xfer_gpio34,
252 .functionality = dibx000_i2c_func,
255 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
262 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
266 if (mst->device_rev > DIB7000)
269 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
270 tx[1] = ((mst->base_reg + 1) & 0xff);
277 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
278 struct i2c_msg msg[], int num)
280 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
284 dprintk("%s: too much I2C message to be transmitted (%i).\
285 Maximum is 32", __func__, num);
289 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
291 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
292 dprintk("could not acquire lock");
296 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
299 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
300 mst->msg[0].addr = mst->i2c_addr;
301 mst->msg[0].buf = &mst->i2c_write_buffer[0];
304 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
307 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
308 mst->msg[num + 1].addr = mst->i2c_addr;
309 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
310 mst->msg[num + 1].len = 4;
312 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
315 mutex_unlock(&mst->i2c_buffer_lock);
319 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
320 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
321 .functionality = dibx000_i2c_func,
324 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
325 struct i2c_msg msg[], int num)
327 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
331 dprintk("%s: too much I2C message to be transmitted (%i).\
332 Maximum is 32", __func__, num);
336 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
338 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
339 dprintk("could not acquire lock");
342 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
345 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
346 mst->msg[0].addr = mst->i2c_addr;
347 mst->msg[0].buf = &mst->i2c_write_buffer[0];
350 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
353 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
354 mst->msg[num + 1].addr = mst->i2c_addr;
355 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
356 mst->msg[num + 1].len = 4;
358 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
360 mutex_unlock(&mst->i2c_buffer_lock);
364 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
365 .master_xfer = dibx000_i2c_gated_tuner_xfer,
366 .functionality = dibx000_i2c_func,
369 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
370 enum dibx000_i2c_interface intf,
373 struct i2c_adapter *i2c = NULL;
376 case DIBX000_I2C_INTERFACE_TUNER:
378 i2c = &mst->gated_tuner_i2c_adap;
380 case DIBX000_I2C_INTERFACE_GPIO_1_2:
382 i2c = &mst->master_i2c_adap_gpio12;
384 case DIBX000_I2C_INTERFACE_GPIO_3_4:
386 i2c = &mst->master_i2c_adap_gpio34;
388 case DIBX000_I2C_INTERFACE_GPIO_6_7:
390 i2c = &mst->master_i2c_adap_gpio67;
393 printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
400 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
402 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
404 /* initialize the i2c-master by closing the gate */
406 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
408 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
409 i2c_transfer(mst->i2c_adap, &m, 1);
410 mst->selected_interface = 0xff; // the first time force a select of the I2C
411 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
414 EXPORT_SYMBOL(dibx000_reset_i2c_master);
416 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
417 struct i2c_algorithm *algo, const char *name,
418 struct dibx000_i2c_master *mst)
420 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
421 i2c_adap->algo = algo;
422 i2c_adap->algo_data = NULL;
423 i2c_set_adapdata(i2c_adap, mst);
424 if (i2c_add_adapter(i2c_adap) < 0)
429 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
430 struct i2c_adapter *i2c_adap, u8 i2c_addr)
434 mutex_init(&mst->i2c_buffer_lock);
435 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
436 dprintk("could not acquire lock");
439 memset(mst->msg, 0, sizeof(struct i2c_msg));
440 mst->msg[0].addr = i2c_addr >> 1;
441 mst->msg[0].flags = 0;
442 mst->msg[0].buf = mst->i2c_write_buffer;
445 mst->device_rev = device_rev;
446 mst->i2c_adap = i2c_adap;
447 mst->i2c_addr = i2c_addr >> 1;
449 if (device_rev == DIB7000P || device_rev == DIB8000)
450 mst->base_reg = 1024;
454 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
456 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
457 "DiBX000 tuner I2C bus", mst) != 0)
459 "DiBX000: could not initialize the tuner i2c_adapter\n");
461 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
463 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
464 "DiBX000 master GPIO12 I2C bus", mst) != 0)
466 "DiBX000: could not initialize the master i2c_adapter\n");
468 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
470 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
471 "DiBX000 master GPIO34 I2C bus", mst) != 0)
473 "DiBX000: could not initialize the master i2c_adapter\n");
475 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
477 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
478 "DiBX000 master GPIO67 I2C bus", mst) != 0)
480 "DiBX000: could not initialize the master i2c_adapter\n");
482 /* initialize the i2c-master by closing the gate */
483 dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);
485 ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
486 mutex_unlock(&mst->i2c_buffer_lock);
491 EXPORT_SYMBOL(dibx000_init_i2c_master);
493 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
495 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
496 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
497 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
498 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
500 EXPORT_SYMBOL(dibx000_exit_i2c_master);
507 t = current_kernel_time();
508 return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
510 EXPORT_SYMBOL(systime);
512 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
513 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
514 MODULE_LICENSE("GPL");