1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 #include <linux/mutex.h>
6 #include <linux/module.h>
8 #include "dibx000_common.h"
11 module_param(debug, int, 0644);
12 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
14 #define dprintk(fmt, arg...) do { \
16 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
20 static int dibx000_write_word(struct dibx000_i2c_master *mst, u16 reg, u16 val)
24 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
25 dprintk("could not acquire lock\n");
29 mst->i2c_write_buffer[0] = (reg >> 8) & 0xff;
30 mst->i2c_write_buffer[1] = reg & 0xff;
31 mst->i2c_write_buffer[2] = (val >> 8) & 0xff;
32 mst->i2c_write_buffer[3] = val & 0xff;
34 memset(mst->msg, 0, sizeof(struct i2c_msg));
35 mst->msg[0].addr = mst->i2c_addr;
36 mst->msg[0].flags = 0;
37 mst->msg[0].buf = mst->i2c_write_buffer;
40 ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0;
41 mutex_unlock(&mst->i2c_buffer_lock);
46 static u16 dibx000_read_word(struct dibx000_i2c_master *mst, u16 reg)
50 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
51 dprintk("could not acquire lock\n");
55 mst->i2c_write_buffer[0] = reg >> 8;
56 mst->i2c_write_buffer[1] = reg & 0xff;
58 memset(mst->msg, 0, 2 * sizeof(struct i2c_msg));
59 mst->msg[0].addr = mst->i2c_addr;
60 mst->msg[0].flags = 0;
61 mst->msg[0].buf = mst->i2c_write_buffer;
63 mst->msg[1].addr = mst->i2c_addr;
64 mst->msg[1].flags = I2C_M_RD;
65 mst->msg[1].buf = mst->i2c_read_buffer;
68 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2)
69 dprintk("i2c read error on %d\n", reg);
71 ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1];
72 mutex_unlock(&mst->i2c_buffer_lock);
77 static int dibx000_is_i2c_done(struct dibx000_i2c_master *mst)
82 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0)
90 if ((status & 0x0080) == 0)
96 static int dibx000_master_i2c_write(struct dibx000_i2c_master *mst, struct i2c_msg *msg, u8 stop)
101 u16 txlen = msg->len, len;
102 const u8 *b = msg->buf;
105 dibx000_read_word(mst, mst->base_reg + 2);
107 len = txlen > 8 ? 8 : txlen;
108 for (i = 0; i < len; i += 2) {
112 dibx000_write_word(mst, mst->base_reg, data);
114 da = (((u8) (msg->addr)) << 9) |
123 if (txlen == msg->len)
124 da |= 1 << 5; /* start */
126 if (txlen-len == 0 && stop)
127 da |= 1 << 6; /* stop */
129 dibx000_write_word(mst, mst->base_reg+1, da);
131 if (dibx000_is_i2c_done(mst) != 0)
139 static int dibx000_master_i2c_read(struct dibx000_i2c_master *mst, struct i2c_msg *msg)
143 u16 rxlen = msg->len, len;
146 len = rxlen > 8 ? 8 : rxlen;
147 da = (((u8) (msg->addr)) << 9) |
156 if (rxlen == msg->len)
157 da |= 1 << 5; /* start */
160 da |= 1 << 6; /* stop */
161 dibx000_write_word(mst, mst->base_reg+1, da);
163 if (dibx000_is_i2c_done(mst) != 0)
169 da = dibx000_read_word(mst, mst->base_reg);
170 *b++ = (da >> 8) & 0xff;
182 int dibx000_i2c_set_speed(struct i2c_adapter *i2c_adap, u16 speed)
184 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
186 if (mst->device_rev < DIB7000MC && speed < 235)
188 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed));
191 EXPORT_SYMBOL(dibx000_i2c_set_speed);
193 static u32 dibx000_i2c_func(struct i2c_adapter *adapter)
198 static int dibx000_i2c_select_interface(struct dibx000_i2c_master *mst,
199 enum dibx000_i2c_interface intf)
201 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) {
202 dprintk("selecting interface: %d\n", intf);
203 mst->selected_interface = intf;
204 return dibx000_write_word(mst, mst->base_reg + 4, intf);
209 static int dibx000_i2c_master_xfer_gpio12(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
211 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
215 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_1_2);
216 for (msg_index = 0; msg_index < num; msg_index++) {
217 if (msg[msg_index].flags & I2C_M_RD) {
218 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
222 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
231 static int dibx000_i2c_master_xfer_gpio34(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
233 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
237 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_3_4);
238 for (msg_index = 0; msg_index < num; msg_index++) {
239 if (msg[msg_index].flags & I2C_M_RD) {
240 ret = dibx000_master_i2c_read(mst, &msg[msg_index]);
244 ret = dibx000_master_i2c_write(mst, &msg[msg_index], 1);
253 static struct i2c_algorithm dibx000_i2c_master_gpio12_xfer_algo = {
254 .master_xfer = dibx000_i2c_master_xfer_gpio12,
255 .functionality = dibx000_i2c_func,
258 static struct i2c_algorithm dibx000_i2c_master_gpio34_xfer_algo = {
259 .master_xfer = dibx000_i2c_master_xfer_gpio34,
260 .functionality = dibx000_i2c_func,
263 static int dibx000_i2c_gate_ctrl(struct dibx000_i2c_master *mst, u8 tx[4],
270 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open
274 if (mst->device_rev > DIB7000)
277 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff);
278 tx[1] = ((mst->base_reg + 1) & 0xff);
285 static int dibx000_i2c_gated_gpio67_xfer(struct i2c_adapter *i2c_adap,
286 struct i2c_msg msg[], int num)
288 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
292 dprintk("%s: too much I2C message to be transmitted (%i). Maximum is 32",
297 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_GPIO_6_7);
299 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
300 dprintk("could not acquire lock\n");
304 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
307 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
308 mst->msg[0].addr = mst->i2c_addr;
309 mst->msg[0].buf = &mst->i2c_write_buffer[0];
312 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
315 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
316 mst->msg[num + 1].addr = mst->i2c_addr;
317 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
318 mst->msg[num + 1].len = 4;
320 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
323 mutex_unlock(&mst->i2c_buffer_lock);
327 static struct i2c_algorithm dibx000_i2c_gated_gpio67_algo = {
328 .master_xfer = dibx000_i2c_gated_gpio67_xfer,
329 .functionality = dibx000_i2c_func,
332 static int dibx000_i2c_gated_tuner_xfer(struct i2c_adapter *i2c_adap,
333 struct i2c_msg msg[], int num)
335 struct dibx000_i2c_master *mst = i2c_get_adapdata(i2c_adap);
339 dprintk("%s: too much I2C message to be transmitted (%i). Maximum is 32",
344 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
346 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
347 dprintk("could not acquire lock\n");
350 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num));
353 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1);
354 mst->msg[0].addr = mst->i2c_addr;
355 mst->msg[0].buf = &mst->i2c_write_buffer[0];
358 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num);
361 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0);
362 mst->msg[num + 1].addr = mst->i2c_addr;
363 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4];
364 mst->msg[num + 1].len = 4;
366 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ?
368 mutex_unlock(&mst->i2c_buffer_lock);
372 static struct i2c_algorithm dibx000_i2c_gated_tuner_algo = {
373 .master_xfer = dibx000_i2c_gated_tuner_xfer,
374 .functionality = dibx000_i2c_func,
377 struct i2c_adapter *dibx000_get_i2c_adapter(struct dibx000_i2c_master *mst,
378 enum dibx000_i2c_interface intf,
381 struct i2c_adapter *i2c = NULL;
384 case DIBX000_I2C_INTERFACE_TUNER:
386 i2c = &mst->gated_tuner_i2c_adap;
388 case DIBX000_I2C_INTERFACE_GPIO_1_2:
390 i2c = &mst->master_i2c_adap_gpio12;
392 case DIBX000_I2C_INTERFACE_GPIO_3_4:
394 i2c = &mst->master_i2c_adap_gpio34;
396 case DIBX000_I2C_INTERFACE_GPIO_6_7:
398 i2c = &mst->master_i2c_adap_gpio67;
401 pr_err("incorrect I2C interface selected\n");
408 EXPORT_SYMBOL(dibx000_get_i2c_adapter);
410 void dibx000_reset_i2c_master(struct dibx000_i2c_master *mst)
412 /* initialize the i2c-master by closing the gate */
414 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 };
416 dibx000_i2c_gate_ctrl(mst, tx, 0, 0);
417 i2c_transfer(mst->i2c_adap, &m, 1);
418 mst->selected_interface = 0xff; // the first time force a select of the I2C
419 dibx000_i2c_select_interface(mst, DIBX000_I2C_INTERFACE_TUNER);
422 EXPORT_SYMBOL(dibx000_reset_i2c_master);
424 static int i2c_adapter_init(struct i2c_adapter *i2c_adap,
425 struct i2c_algorithm *algo, const char *name,
426 struct dibx000_i2c_master *mst)
428 strscpy(i2c_adap->name, name, sizeof(i2c_adap->name));
429 i2c_adap->algo = algo;
430 i2c_adap->algo_data = NULL;
431 i2c_set_adapdata(i2c_adap, mst);
432 if (i2c_add_adapter(i2c_adap) < 0)
437 int dibx000_init_i2c_master(struct dibx000_i2c_master *mst, u16 device_rev,
438 struct i2c_adapter *i2c_adap, u8 i2c_addr)
442 mutex_init(&mst->i2c_buffer_lock);
443 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) {
444 dprintk("could not acquire lock\n");
447 memset(mst->msg, 0, sizeof(struct i2c_msg));
448 mst->msg[0].addr = i2c_addr >> 1;
449 mst->msg[0].flags = 0;
450 mst->msg[0].buf = mst->i2c_write_buffer;
453 mst->device_rev = device_rev;
454 mst->i2c_adap = i2c_adap;
455 mst->i2c_addr = i2c_addr >> 1;
457 if (device_rev == DIB7000P || device_rev == DIB8000)
458 mst->base_reg = 1024;
462 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent;
464 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo,
465 "DiBX000 tuner I2C bus", mst) != 0)
466 pr_err("could not initialize the tuner i2c_adapter\n");
468 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent;
470 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo,
471 "DiBX000 master GPIO12 I2C bus", mst) != 0)
472 pr_err("could not initialize the master i2c_adapter\n");
474 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent;
476 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo,
477 "DiBX000 master GPIO34 I2C bus", mst) != 0)
478 pr_err("could not initialize the master i2c_adapter\n");
480 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent;
482 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo,
483 "DiBX000 master GPIO67 I2C bus", mst) != 0)
484 pr_err("could not initialize the master i2c_adapter\n");
486 /* initialize the i2c-master by closing the gate */
487 dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0);
489 ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1);
490 mutex_unlock(&mst->i2c_buffer_lock);
495 EXPORT_SYMBOL(dibx000_init_i2c_master);
497 void dibx000_exit_i2c_master(struct dibx000_i2c_master *mst)
499 i2c_del_adapter(&mst->gated_tuner_i2c_adap);
500 i2c_del_adapter(&mst->master_i2c_adap_gpio12);
501 i2c_del_adapter(&mst->master_i2c_adap_gpio34);
502 i2c_del_adapter(&mst->master_i2c_adap_gpio67);
504 EXPORT_SYMBOL(dibx000_exit_i2c_master);
507 MODULE_DESCRIPTION("Common function the DiBcom demodulator family");
508 MODULE_LICENSE("GPL");