1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 #include <linux/mutex.h>
5 #include <linux/module.h>
7 #include "dibx000_common.h"
10 module_param(debug
, int, 0644);
11 MODULE_PARM_DESC(debug
, "turn on debugging (default: 0)");
13 #define dprintk(fmt, arg...) do { \
15 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
19 static int dibx000_write_word(struct dibx000_i2c_master
*mst
, u16 reg
, u16 val
)
23 if (mutex_lock_interruptible(&mst
->i2c_buffer_lock
) < 0) {
24 dprintk("could not acquire lock\n");
28 mst
->i2c_write_buffer
[0] = (reg
>> 8) & 0xff;
29 mst
->i2c_write_buffer
[1] = reg
& 0xff;
30 mst
->i2c_write_buffer
[2] = (val
>> 8) & 0xff;
31 mst
->i2c_write_buffer
[3] = val
& 0xff;
33 memset(mst
->msg
, 0, 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
;
39 ret
= i2c_transfer(mst
->i2c_adap
, mst
->msg
, 1) != 1 ? -EREMOTEIO
: 0;
40 mutex_unlock(&mst
->i2c_buffer_lock
);
45 static u16
dibx000_read_word(struct dibx000_i2c_master
*mst
, u16 reg
)
49 if (mutex_lock_interruptible(&mst
->i2c_buffer_lock
) < 0) {
50 dprintk("could not acquire lock\n");
54 mst
->i2c_write_buffer
[0] = reg
>> 8;
55 mst
->i2c_write_buffer
[1] = reg
& 0xff;
57 memset(mst
->msg
, 0, 2 * sizeof(struct i2c_msg
));
58 mst
->msg
[0].addr
= mst
->i2c_addr
;
59 mst
->msg
[0].flags
= 0;
60 mst
->msg
[0].buf
= mst
->i2c_write_buffer
;
62 mst
->msg
[1].addr
= mst
->i2c_addr
;
63 mst
->msg
[1].flags
= I2C_M_RD
;
64 mst
->msg
[1].buf
= mst
->i2c_read_buffer
;
67 if (i2c_transfer(mst
->i2c_adap
, mst
->msg
, 2) != 2)
68 dprintk("i2c read error on %d\n", reg
);
70 ret
= (mst
->i2c_read_buffer
[0] << 8) | mst
->i2c_read_buffer
[1];
71 mutex_unlock(&mst
->i2c_buffer_lock
);
76 static int dibx000_is_i2c_done(struct dibx000_i2c_master
*mst
)
81 while (((status
= dibx000_read_word(mst
, mst
->base_reg
+ 2)) & 0x0100) == 0 && --i
> 0)
89 if ((status
& 0x0080) == 0)
95 static int dibx000_master_i2c_write(struct dibx000_i2c_master
*mst
, struct i2c_msg
*msg
, u8 stop
)
100 u16 txlen
= msg
->len
, len
;
101 const u8
*b
= msg
->buf
;
104 dibx000_read_word(mst
, mst
->base_reg
+ 2);
106 len
= txlen
> 8 ? 8 : txlen
;
107 for (i
= 0; i
< len
; i
+= 2) {
111 dibx000_write_word(mst
, mst
->base_reg
, data
);
113 da
= (((u8
) (msg
->addr
)) << 9) |
122 if (txlen
== msg
->len
)
123 da
|= 1 << 5; /* start */
125 if (txlen
-len
== 0 && stop
)
126 da
|= 1 << 6; /* stop */
128 dibx000_write_word(mst
, mst
->base_reg
+1, da
);
130 if (dibx000_is_i2c_done(mst
) != 0)
138 static int dibx000_master_i2c_read(struct dibx000_i2c_master
*mst
, struct i2c_msg
*msg
)
142 u16 rxlen
= msg
->len
, len
;
145 len
= rxlen
> 8 ? 8 : rxlen
;
146 da
= (((u8
) (msg
->addr
)) << 9) |
155 if (rxlen
== msg
->len
)
156 da
|= 1 << 5; /* start */
159 da
|= 1 << 6; /* stop */
160 dibx000_write_word(mst
, mst
->base_reg
+1, da
);
162 if (dibx000_is_i2c_done(mst
) != 0)
168 da
= dibx000_read_word(mst
, mst
->base_reg
);
169 *b
++ = (da
>> 8) & 0xff;
181 int dibx000_i2c_set_speed(struct i2c_adapter
*i2c_adap
, u16 speed
)
183 struct dibx000_i2c_master
*mst
= i2c_get_adapdata(i2c_adap
);
185 if (mst
->device_rev
< DIB7000MC
&& speed
< 235)
187 return dibx000_write_word(mst
, mst
->base_reg
+ 3, (u16
)(60000 / speed
));
190 EXPORT_SYMBOL(dibx000_i2c_set_speed
);
192 static u32
dibx000_i2c_func(struct i2c_adapter
*adapter
)
197 static int dibx000_i2c_select_interface(struct dibx000_i2c_master
*mst
,
198 enum dibx000_i2c_interface intf
)
200 if (mst
->device_rev
> DIB3000MC
&& mst
->selected_interface
!= intf
) {
201 dprintk("selecting interface: %d\n", intf
);
202 mst
->selected_interface
= intf
;
203 return dibx000_write_word(mst
, mst
->base_reg
+ 4, intf
);
208 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter
*i2c_adap
, struct i2c_msg msg
[], int num
)
210 struct dibx000_i2c_master
*mst
= i2c_get_adapdata(i2c_adap
);
214 dibx000_i2c_select_interface(mst
, DIBX000_I2C_INTERFACE_GPIO_1_2
);
215 for (msg_index
= 0; msg_index
< num
; msg_index
++) {
216 if (msg
[msg_index
].flags
& I2C_M_RD
) {
217 ret
= dibx000_master_i2c_read(mst
, &msg
[msg_index
]);
221 ret
= dibx000_master_i2c_write(mst
, &msg
[msg_index
], 1);
230 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter
*i2c_adap
, struct i2c_msg msg
[], int num
)
232 struct dibx000_i2c_master
*mst
= i2c_get_adapdata(i2c_adap
);
236 dibx000_i2c_select_interface(mst
, DIBX000_I2C_INTERFACE_GPIO_3_4
);
237 for (msg_index
= 0; msg_index
< num
; msg_index
++) {
238 if (msg
[msg_index
].flags
& I2C_M_RD
) {
239 ret
= dibx000_master_i2c_read(mst
, &msg
[msg_index
]);
243 ret
= dibx000_master_i2c_write(mst
, &msg
[msg_index
], 1);
252 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo
= {
253 .master_xfer
= dibx000_i2c_master_xfer_gpio12
,
254 .functionality
= dibx000_i2c_func
,
257 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo
= {
258 .master_xfer
= dibx000_i2c_master_xfer_gpio34
,
259 .functionality
= dibx000_i2c_func
,
262 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master
*mst
, u8 tx
[4],
269 val
= addr
<< 8; // bit 7 = use master or not, if 0, the gate is open
273 if (mst
->device_rev
> DIB7000
)
276 tx
[0] = (((mst
->base_reg
+ 1) >> 8) & 0xff);
277 tx
[1] = ((mst
->base_reg
+ 1) & 0xff);
284 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter
*i2c_adap
,
285 struct i2c_msg msg
[], int num
)
287 struct dibx000_i2c_master
*mst
= i2c_get_adapdata(i2c_adap
);
291 dprintk("%s: too much I2C message to be transmitted (%i).\
292 Maximum is 32", __func__
, num
);
296 dibx000_i2c_select_interface(mst
, DIBX000_I2C_INTERFACE_GPIO_6_7
);
298 if (mutex_lock_interruptible(&mst
->i2c_buffer_lock
) < 0) {
299 dprintk("could not acquire lock\n");
303 memset(mst
->msg
, 0, sizeof(struct i2c_msg
) * (2 + num
));
306 dibx000_i2c_gate_ctrl(mst
, &mst
->i2c_write_buffer
[0], msg
[0].addr
, 1);
307 mst
->msg
[0].addr
= mst
->i2c_addr
;
308 mst
->msg
[0].buf
= &mst
->i2c_write_buffer
[0];
311 memcpy(&mst
->msg
[1], msg
, sizeof(struct i2c_msg
) * num
);
314 dibx000_i2c_gate_ctrl(mst
, &mst
->i2c_write_buffer
[4], 0, 0);
315 mst
->msg
[num
+ 1].addr
= mst
->i2c_addr
;
316 mst
->msg
[num
+ 1].buf
= &mst
->i2c_write_buffer
[4];
317 mst
->msg
[num
+ 1].len
= 4;
319 ret
= (i2c_transfer(mst
->i2c_adap
, mst
->msg
, 2 + num
) == 2 + num
?
322 mutex_unlock(&mst
->i2c_buffer_lock
);
326 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo
= {
327 .master_xfer
= dibx000_i2c_gated_gpio67_xfer
,
328 .functionality
= dibx000_i2c_func
,
331 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter
*i2c_adap
,
332 struct i2c_msg msg
[], int num
)
334 struct dibx000_i2c_master
*mst
= i2c_get_adapdata(i2c_adap
);
338 dprintk("%s: too much I2C message to be transmitted (%i).\
339 Maximum is 32", __func__
, num
);
343 dibx000_i2c_select_interface(mst
, DIBX000_I2C_INTERFACE_TUNER
);
345 if (mutex_lock_interruptible(&mst
->i2c_buffer_lock
) < 0) {
346 dprintk("could not acquire lock\n");
349 memset(mst
->msg
, 0, sizeof(struct i2c_msg
) * (2 + num
));
352 dibx000_i2c_gate_ctrl(mst
, &mst
->i2c_write_buffer
[0], msg
[0].addr
, 1);
353 mst
->msg
[0].addr
= mst
->i2c_addr
;
354 mst
->msg
[0].buf
= &mst
->i2c_write_buffer
[0];
357 memcpy(&mst
->msg
[1], msg
, sizeof(struct i2c_msg
) * num
);
360 dibx000_i2c_gate_ctrl(mst
, &mst
->i2c_write_buffer
[4], 0, 0);
361 mst
->msg
[num
+ 1].addr
= mst
->i2c_addr
;
362 mst
->msg
[num
+ 1].buf
= &mst
->i2c_write_buffer
[4];
363 mst
->msg
[num
+ 1].len
= 4;
365 ret
= (i2c_transfer(mst
->i2c_adap
, mst
->msg
, 2 + num
) == 2 + num
?
367 mutex_unlock(&mst
->i2c_buffer_lock
);
371 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo
= {
372 .master_xfer
= dibx000_i2c_gated_tuner_xfer
,
373 .functionality
= dibx000_i2c_func
,
376 struct i2c_adapter
*dibx000_get_i2c_adapter(struct dibx000_i2c_master
*mst
,
377 enum dibx000_i2c_interface intf
,
380 struct i2c_adapter
*i2c
= NULL
;
383 case DIBX000_I2C_INTERFACE_TUNER
:
385 i2c
= &mst
->gated_tuner_i2c_adap
;
387 case DIBX000_I2C_INTERFACE_GPIO_1_2
:
389 i2c
= &mst
->master_i2c_adap_gpio12
;
391 case DIBX000_I2C_INTERFACE_GPIO_3_4
:
393 i2c
= &mst
->master_i2c_adap_gpio34
;
395 case DIBX000_I2C_INTERFACE_GPIO_6_7
:
397 i2c
= &mst
->master_i2c_adap_gpio67
;
400 pr_err("incorrect I2C interface selected\n");
407 EXPORT_SYMBOL(dibx000_get_i2c_adapter
);
409 void dibx000_reset_i2c_master(struct dibx000_i2c_master
*mst
)
411 /* initialize the i2c-master by closing the gate */
413 struct i2c_msg m
= {.addr
= mst
->i2c_addr
,.buf
= tx
,.len
= 4 };
415 dibx000_i2c_gate_ctrl(mst
, tx
, 0, 0);
416 i2c_transfer(mst
->i2c_adap
, &m
, 1);
417 mst
->selected_interface
= 0xff; // the first time force a select of the I2C
418 dibx000_i2c_select_interface(mst
, DIBX000_I2C_INTERFACE_TUNER
);
421 EXPORT_SYMBOL(dibx000_reset_i2c_master
);
423 static int i2c_adapter_init(struct i2c_adapter
*i2c_adap
,
424 struct i2c_algorithm
*algo
, const char *name
,
425 struct dibx000_i2c_master
*mst
)
427 strncpy(i2c_adap
->name
, name
, sizeof(i2c_adap
->name
));
428 i2c_adap
->algo
= algo
;
429 i2c_adap
->algo_data
= NULL
;
430 i2c_set_adapdata(i2c_adap
, mst
);
431 if (i2c_add_adapter(i2c_adap
) < 0)
436 int dibx000_init_i2c_master(struct dibx000_i2c_master
*mst
, u16 device_rev
,
437 struct i2c_adapter
*i2c_adap
, u8 i2c_addr
)
441 mutex_init(&mst
->i2c_buffer_lock
);
442 if (mutex_lock_interruptible(&mst
->i2c_buffer_lock
) < 0) {
443 dprintk("could not acquire lock\n");
446 memset(mst
->msg
, 0, sizeof(struct i2c_msg
));
447 mst
->msg
[0].addr
= i2c_addr
>> 1;
448 mst
->msg
[0].flags
= 0;
449 mst
->msg
[0].buf
= mst
->i2c_write_buffer
;
452 mst
->device_rev
= device_rev
;
453 mst
->i2c_adap
= i2c_adap
;
454 mst
->i2c_addr
= i2c_addr
>> 1;
456 if (device_rev
== DIB7000P
|| device_rev
== DIB8000
)
457 mst
->base_reg
= 1024;
461 mst
->gated_tuner_i2c_adap
.dev
.parent
= mst
->i2c_adap
->dev
.parent
;
463 (&mst
->gated_tuner_i2c_adap
, &dibx000_i2c_gated_tuner_algo
,
464 "DiBX000 tuner I2C bus", mst
) != 0)
465 pr_err("could not initialize the tuner i2c_adapter\n");
467 mst
->master_i2c_adap_gpio12
.dev
.parent
= mst
->i2c_adap
->dev
.parent
;
469 (&mst
->master_i2c_adap_gpio12
, &dibx000_i2c_master_gpio12_xfer_algo
,
470 "DiBX000 master GPIO12 I2C bus", mst
) != 0)
471 pr_err("could not initialize the master i2c_adapter\n");
473 mst
->master_i2c_adap_gpio34
.dev
.parent
= mst
->i2c_adap
->dev
.parent
;
475 (&mst
->master_i2c_adap_gpio34
, &dibx000_i2c_master_gpio34_xfer_algo
,
476 "DiBX000 master GPIO34 I2C bus", mst
) != 0)
477 pr_err("could not initialize the master i2c_adapter\n");
479 mst
->master_i2c_adap_gpio67
.dev
.parent
= mst
->i2c_adap
->dev
.parent
;
481 (&mst
->master_i2c_adap_gpio67
, &dibx000_i2c_gated_gpio67_algo
,
482 "DiBX000 master GPIO67 I2C bus", mst
) != 0)
483 pr_err("could not initialize the master i2c_adapter\n");
485 /* initialize the i2c-master by closing the gate */
486 dibx000_i2c_gate_ctrl(mst
, mst
->i2c_write_buffer
, 0, 0);
488 ret
= (i2c_transfer(i2c_adap
, mst
->msg
, 1) == 1);
489 mutex_unlock(&mst
->i2c_buffer_lock
);
494 EXPORT_SYMBOL(dibx000_init_i2c_master
);
496 void dibx000_exit_i2c_master(struct dibx000_i2c_master
*mst
)
498 i2c_del_adapter(&mst
->gated_tuner_i2c_adap
);
499 i2c_del_adapter(&mst
->master_i2c_adap_gpio12
);
500 i2c_del_adapter(&mst
->master_i2c_adap_gpio34
);
501 i2c_del_adapter(&mst
->master_i2c_adap_gpio67
);
503 EXPORT_SYMBOL(dibx000_exit_i2c_master
);
505 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
506 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
507 MODULE_LICENSE("GPL");