3 #include "dibx000_common.h"
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)
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 };
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; // max_i2c_polls;
44 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0);
51 if ((status & 0x0080) == 0)
57 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
62 u16 txlen = msg->len, len;
63 const u8 *b = msg->buf;
66 dibx000_read_word(mst, mst->base_reg + 2); // reset fifo ptr
68 len = txlen > 8 ? 8 : txlen;
69 for (i = 0; i < len; i += 2) {
73 dibx000_write_word(mst, mst->base_reg, data);
75 da = (((u8) (msg->addr)) << 9) | // addr
80 ((len & 0x7) << 2) | // nb 8 bytes == 0 here
84 if (txlen == msg->len)
85 da |= 1 << 5; /* start */
87 if (txlen-len == 0 && stop)
88 da |= 1 << 6; /* stop */
90 dibx000_write_word(mst, mst->base_reg+1, da);
92 if (dibx000_is_i2c_done(mst) != 0)
100 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
104 u16 rxlen = msg->len, len;
107 len = rxlen > 8 ? 8 : rxlen;
108 da = (((u8) (msg->addr)) << 9) | // addr
113 ((len & 0x7) << 2) | // nb
117 if (rxlen == msg->len)
118 da |= 1 << 5; /* start */
121 da |= 1 << 6; /* stop */
122 dibx000_write_word(mst, mst->base_reg+1, da);
124 if (dibx000_is_i2c_done(mst) != 0)
130 da = dibx000_read_word(mst, mst->base_reg);
131 *b++ = (da >> 8) & 0xff;
143 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
145 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
147 if (mst->device_rev < DIB7000MC && speed < 235)
149 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
152 EXPORT_SYMBOL(dibx000_i2c_set_speed);
154 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
159 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
160 enum dibx000_i2c_interface intf)
162 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
163 dprintk("selecting interface: %d", intf);
164 mst->selected_interface = intf;
165 return dibx000_write_word(mst, mst->base_reg + 4, intf);
170 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
172 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
176 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
177 for (msg_index = 0; msg_index<num; msg_index++) {
178 if (msg[msg_index].flags & I2C_M_RD)
180 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
186 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
195 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
197 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
201 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
202 for (msg_index = 0; msg_index<num; msg_index++) {
203 if (msg[msg_index].flags & I2C_M_RD)
205 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
211 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
220 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
221 .master_xfer = dibx000_i2c_master_xfer_gpio12,
222 .functionality = dibx000_i2c_func,
225 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
226 .master_xfer = dibx000_i2c_master_xfer_gpio34,
227 .functionality = dibx000_i2c_func,
230 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
237 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
241 if (mst->device_rev > DIB7000)
244 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
245 tx[1] = ((mst->base_reg + 1) & 0xff);
252 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
253 struct i2c_msg msg[], int num)
255 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
256 struct i2c_msg m[2 + num];
257 u8 tx_open[4], tx_close[4];
259 memset(m, 0, sizeof(struct i2c_msg) * (2 + num));
261 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
263 dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1);
264 m[0].addr = mst->i2c_addr;
268 memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
270 dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
271 m[num + 1].addr = mst->i2c_addr;
272 m[num + 1].buf = tx_close;
275 return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO;
278 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
279 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
280 .functionality = dibx000_i2c_func,
283 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
284 struct i2c_msg msg[], int num)
286 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
287 struct i2c_msg m[2 + num];
288 u8 tx_open[4], tx_close[4];
290 memset(m, 0, sizeof(struct i2c_msg) * (2 + num));
292 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
294 dibx000_i2c_gate_ctrl(mst, tx_open, msg[0].addr, 1);
295 m[0].addr = mst->i2c_addr;
299 memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
301 dibx000_i2c_gate_ctrl(mst, tx_close, 0, 0);
302 m[num + 1].addr = mst->i2c_addr;
303 m[num + 1].buf = tx_close;
306 return i2c_transfer(mst->i2c_adap, m, 2 + num) == 2 + num ? num : -EIO;
309 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
310 .master_xfer = dibx000_i2c_gated_tuner_xfer,
311 .functionality = dibx000_i2c_func,
314 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
315 enum dibx000_i2c_interface intf,
318 struct i2c_adapter *i2c = NULL;
321 case DIBX000_I2C_INTERFACE_TUNER:
323 i2c = &mst->gated_tuner_i2c_adap;
325 case DIBX000_I2C_INTERFACE_GPIO_1_2:
327 i2c = &mst->master_i2c_adap_gpio12;
329 case DIBX000_I2C_INTERFACE_GPIO_3_4:
331 i2c = &mst->master_i2c_adap_gpio34;
333 case DIBX000_I2C_INTERFACE_GPIO_6_7:
335 i2c = &mst->master_i2c_adap_gpio67;
338 printk(KERN_ERR "DiBX000: incorrect I2C interface selected\n");
345 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
347 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
349 /* initialize the i2c-master by closing the gate */
351 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
353 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
354 i2c_transfer(mst->i2c_adap, &m, 1);
355 mst->selected_interface = 0xff; // the first time force a select of the I2C
356 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
359 EXPORT_SYMBOL(dibx000_reset_i2c_master);
361 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
362 struct i2c_algorithm *algo, const char *name,
363 struct dibx000_i2c_master *mst)
365 strncpy(i2c_adap->name, name, sizeof(i2c_adap->name));
366 i2c_adap->algo = algo;
367 i2c_adap->algo_data = NULL;
368 i2c_set_adapdata(i2c_adap, mst);
369 if (i2c_add_adapter(i2c_adap) < 0)
374 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
375 struct i2c_adapter *i2c_adap, u8 i2c_addr)
378 struct i2c_msg m = {.addr = i2c_addr >> 1,.buf = tx,.len = 4 };
380 mst->device_rev = device_rev;
381 mst->i2c_adap = i2c_adap;
382 mst->i2c_addr = i2c_addr >> 1;
384 if (device_rev == DIB7000P || device_rev == DIB8000)
385 mst->base_reg = 1024;
389 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
391 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
392 "DiBX000 tuner I2C bus", mst) != 0)
394 "DiBX000: could not initialize the tuner i2c_adapter\n");
396 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
398 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
399 "DiBX000 master GPIO12 I2C bus", mst) != 0)
401 "DiBX000: could not initialize the master i2c_adapter\n");
403 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
405 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
406 "DiBX000 master GPIO34 I2C bus", mst) != 0)
408 "DiBX000: could not initialize the master i2c_adapter\n");
410 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
412 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
413 "DiBX000 master GPIO67 I2C bus", mst) != 0)
415 "DiBX000: could not initialize the master i2c_adapter\n");
417 /* initialize the i2c-master by closing the gate */
418 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
420 return i2c_transfer(i2c_adap, &m, 1) == 1;
423 EXPORT_SYMBOL(dibx000_init_i2c_master);
425 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
427 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
428 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
429 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
430 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
432 EXPORT_SYMBOL(dibx000_exit_i2c_master);
439 t = current_kernel_time();
440 return (t.tv_sec * 10000) + (t.tv_nsec / 100000);
442 EXPORT_SYMBOL(systime);
444 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
445 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
446 MODULE_LICENSE("GPL");