Lines Matching +full:i2c +full:- +full:gate
1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/i2c.h>
24 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { in dibx000_write_word()
26 return -EINVAL; in dibx000_write_word()
29 mst->i2c_write_buffer[0] = (reg >> 8) & 0xff; in dibx000_write_word()
30 mst->i2c_write_buffer[1] = reg & 0xff; in dibx000_write_word()
31 mst->i2c_write_buffer[2] = (val >> 8) & 0xff; in dibx000_write_word()
32 mst->i2c_write_buffer[3] = val & 0xff; in dibx000_write_word()
34 memset(mst->msg, 0, sizeof(struct i2c_msg)); in dibx000_write_word()
35 mst->msg[0].addr = mst->i2c_addr; in dibx000_write_word()
36 mst->msg[0].flags = 0; in dibx000_write_word()
37 mst->msg[0].buf = mst->i2c_write_buffer; in dibx000_write_word()
38 mst->msg[0].len = 4; in dibx000_write_word()
40 ret = i2c_transfer(mst->i2c_adap, mst->msg, 1) != 1 ? -EREMOTEIO : 0; in dibx000_write_word()
41 mutex_unlock(&mst->i2c_buffer_lock); in dibx000_write_word()
50 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { in dibx000_read_word()
55 mst->i2c_write_buffer[0] = reg >> 8; in dibx000_read_word()
56 mst->i2c_write_buffer[1] = reg & 0xff; in dibx000_read_word()
58 memset(mst->msg, 0, 2 * sizeof(struct i2c_msg)); in dibx000_read_word()
59 mst->msg[0].addr = mst->i2c_addr; in dibx000_read_word()
60 mst->msg[0].flags = 0; in dibx000_read_word()
61 mst->msg[0].buf = mst->i2c_write_buffer; in dibx000_read_word()
62 mst->msg[0].len = 2; in dibx000_read_word()
63 mst->msg[1].addr = mst->i2c_addr; in dibx000_read_word()
64 mst->msg[1].flags = I2C_M_RD; in dibx000_read_word()
65 mst->msg[1].buf = mst->i2c_read_buffer; in dibx000_read_word()
66 mst->msg[1].len = 2; in dibx000_read_word()
68 if (i2c_transfer(mst->i2c_adap, mst->msg, 2) != 2) in dibx000_read_word()
69 dprintk("i2c read error on %d\n", reg); in dibx000_read_word()
71 ret = (mst->i2c_read_buffer[0] << 8) | mst->i2c_read_buffer[1]; in dibx000_read_word()
72 mutex_unlock(&mst->i2c_buffer_lock); in dibx000_read_word()
82 while (((status = dibx000_read_word(mst, mst->base_reg + 2)) & 0x0100) == 0 && --i > 0) in dibx000_is_i2c_done()
85 /* i2c timed out */ in dibx000_is_i2c_done()
87 return -EREMOTEIO; in dibx000_is_i2c_done()
91 return -EREMOTEIO; in dibx000_is_i2c_done()
101 u16 txlen = msg->len, len; in dibx000_master_i2c_write()
102 const u8 *b = msg->buf; in dibx000_master_i2c_write()
105 dibx000_read_word(mst, mst->base_reg + 2); in dibx000_master_i2c_write()
112 dibx000_write_word(mst, mst->base_reg, data); in dibx000_master_i2c_write()
114 da = (((u8) (msg->addr)) << 9) | in dibx000_master_i2c_write()
123 if (txlen == msg->len) in dibx000_master_i2c_write()
126 if (txlen-len == 0 && stop) in dibx000_master_i2c_write()
129 dibx000_write_word(mst, mst->base_reg+1, da); in dibx000_master_i2c_write()
132 return -EREMOTEIO; in dibx000_master_i2c_write()
133 txlen -= len; in dibx000_master_i2c_write()
142 u8 *b = msg->buf; in dibx000_master_i2c_read()
143 u16 rxlen = msg->len, len; in dibx000_master_i2c_read()
147 da = (((u8) (msg->addr)) << 9) | in dibx000_master_i2c_read()
156 if (rxlen == msg->len) in dibx000_master_i2c_read()
159 if (rxlen-len == 0) in dibx000_master_i2c_read()
161 dibx000_write_word(mst, mst->base_reg+1, da); in dibx000_master_i2c_read()
164 return -EREMOTEIO; in dibx000_master_i2c_read()
166 rxlen -= len; in dibx000_master_i2c_read()
169 da = dibx000_read_word(mst, mst->base_reg); in dibx000_master_i2c_read()
171 len--; in dibx000_master_i2c_read()
174 len--; in dibx000_master_i2c_read()
186 if (mst->device_rev < DIB7000MC && speed < 235) in dibx000_i2c_set_speed()
188 return dibx000_write_word(mst, mst->base_reg + 3, (u16)(60000 / speed)); in dibx000_i2c_set_speed()
201 if (mst->device_rev > DIB3000MC && mst->selected_interface != intf) { in dibx000_i2c_select_interface()
203 mst->selected_interface = intf; in dibx000_i2c_select_interface()
204 return dibx000_write_word(mst, mst->base_reg + 4, intf); in dibx000_i2c_select_interface()
270 val = addr << 8; // bit 7 = use master or not, if 0, the gate is open in dibx000_i2c_gate_ctrl()
274 if (mst->device_rev > DIB7000) in dibx000_i2c_gate_ctrl()
277 tx[0] = (((mst->base_reg + 1) >> 8) & 0xff); in dibx000_i2c_gate_ctrl()
278 tx[1] = ((mst->base_reg + 1) & 0xff); in dibx000_i2c_gate_ctrl()
292 dprintk("%s: too much I2C message to be transmitted (%i). Maximum is 32", in dibx000_i2c_gated_gpio67_xfer()
294 return -ENOMEM; in dibx000_i2c_gated_gpio67_xfer()
299 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { in dibx000_i2c_gated_gpio67_xfer()
301 return -EINVAL; in dibx000_i2c_gated_gpio67_xfer()
304 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num)); in dibx000_i2c_gated_gpio67_xfer()
306 /* open the gate */ in dibx000_i2c_gated_gpio67_xfer()
307 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1); in dibx000_i2c_gated_gpio67_xfer()
308 mst->msg[0].addr = mst->i2c_addr; in dibx000_i2c_gated_gpio67_xfer()
309 mst->msg[0].buf = &mst->i2c_write_buffer[0]; in dibx000_i2c_gated_gpio67_xfer()
310 mst->msg[0].len = 4; in dibx000_i2c_gated_gpio67_xfer()
312 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num); in dibx000_i2c_gated_gpio67_xfer()
314 /* close the gate */ in dibx000_i2c_gated_gpio67_xfer()
315 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0); in dibx000_i2c_gated_gpio67_xfer()
316 mst->msg[num + 1].addr = mst->i2c_addr; in dibx000_i2c_gated_gpio67_xfer()
317 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4]; in dibx000_i2c_gated_gpio67_xfer()
318 mst->msg[num + 1].len = 4; in dibx000_i2c_gated_gpio67_xfer()
320 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? in dibx000_i2c_gated_gpio67_xfer()
321 num : -EIO); in dibx000_i2c_gated_gpio67_xfer()
323 mutex_unlock(&mst->i2c_buffer_lock); in dibx000_i2c_gated_gpio67_xfer()
339 dprintk("%s: too much I2C message to be transmitted (%i). Maximum is 32", in dibx000_i2c_gated_tuner_xfer()
341 return -ENOMEM; in dibx000_i2c_gated_tuner_xfer()
346 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { in dibx000_i2c_gated_tuner_xfer()
348 return -EINVAL; in dibx000_i2c_gated_tuner_xfer()
350 memset(mst->msg, 0, sizeof(struct i2c_msg) * (2 + num)); in dibx000_i2c_gated_tuner_xfer()
352 /* open the gate */ in dibx000_i2c_gated_tuner_xfer()
353 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[0], msg[0].addr, 1); in dibx000_i2c_gated_tuner_xfer()
354 mst->msg[0].addr = mst->i2c_addr; in dibx000_i2c_gated_tuner_xfer()
355 mst->msg[0].buf = &mst->i2c_write_buffer[0]; in dibx000_i2c_gated_tuner_xfer()
356 mst->msg[0].len = 4; in dibx000_i2c_gated_tuner_xfer()
358 memcpy(&mst->msg[1], msg, sizeof(struct i2c_msg) * num); in dibx000_i2c_gated_tuner_xfer()
360 /* close the gate */ in dibx000_i2c_gated_tuner_xfer()
361 dibx000_i2c_gate_ctrl(mst, &mst->i2c_write_buffer[4], 0, 0); in dibx000_i2c_gated_tuner_xfer()
362 mst->msg[num + 1].addr = mst->i2c_addr; in dibx000_i2c_gated_tuner_xfer()
363 mst->msg[num + 1].buf = &mst->i2c_write_buffer[4]; in dibx000_i2c_gated_tuner_xfer()
364 mst->msg[num + 1].len = 4; in dibx000_i2c_gated_tuner_xfer()
366 ret = (i2c_transfer(mst->i2c_adap, mst->msg, 2 + num) == 2 + num ? in dibx000_i2c_gated_tuner_xfer()
367 num : -EIO); in dibx000_i2c_gated_tuner_xfer()
368 mutex_unlock(&mst->i2c_buffer_lock); in dibx000_i2c_gated_tuner_xfer()
381 struct i2c_adapter *i2c = NULL; in dibx000_get_i2c_adapter() local
386 i2c = &mst->gated_tuner_i2c_adap; in dibx000_get_i2c_adapter()
390 i2c = &mst->master_i2c_adap_gpio12; in dibx000_get_i2c_adapter()
394 i2c = &mst->master_i2c_adap_gpio34; in dibx000_get_i2c_adapter()
398 i2c = &mst->master_i2c_adap_gpio67; in dibx000_get_i2c_adapter()
401 pr_err("incorrect I2C interface selected\n"); in dibx000_get_i2c_adapter()
405 return i2c; in dibx000_get_i2c_adapter()
412 /* initialize the i2c-master by closing the gate */ in dibx000_reset_i2c_master()
414 struct i2c_msg m = {.addr = mst->i2c_addr,.buf = tx,.len = 4 }; in dibx000_reset_i2c_master()
417 i2c_transfer(mst->i2c_adap, &m, 1); in dibx000_reset_i2c_master()
418 mst->selected_interface = 0xff; // the first time force a select of the I2C in dibx000_reset_i2c_master()
428 strscpy(i2c_adap->name, name, sizeof(i2c_adap->name)); in i2c_adapter_init()
429 i2c_adap->algo = algo; in i2c_adapter_init()
430 i2c_adap->algo_data = NULL; in i2c_adapter_init()
433 return -ENODEV; in i2c_adapter_init()
442 mutex_init(&mst->i2c_buffer_lock); in dibx000_init_i2c_master()
443 if (mutex_lock_interruptible(&mst->i2c_buffer_lock) < 0) { in dibx000_init_i2c_master()
445 return -EINVAL; in dibx000_init_i2c_master()
447 memset(mst->msg, 0, sizeof(struct i2c_msg)); in dibx000_init_i2c_master()
448 mst->msg[0].addr = i2c_addr >> 1; in dibx000_init_i2c_master()
449 mst->msg[0].flags = 0; in dibx000_init_i2c_master()
450 mst->msg[0].buf = mst->i2c_write_buffer; in dibx000_init_i2c_master()
451 mst->msg[0].len = 4; in dibx000_init_i2c_master()
453 mst->device_rev = device_rev; in dibx000_init_i2c_master()
454 mst->i2c_adap = i2c_adap; in dibx000_init_i2c_master()
455 mst->i2c_addr = i2c_addr >> 1; in dibx000_init_i2c_master()
458 mst->base_reg = 1024; in dibx000_init_i2c_master()
460 mst->base_reg = 768; in dibx000_init_i2c_master()
462 mst->gated_tuner_i2c_adap.dev.parent = mst->i2c_adap->dev.parent; in dibx000_init_i2c_master()
464 (&mst->gated_tuner_i2c_adap, &dibx000_i2c_gated_tuner_algo, in dibx000_init_i2c_master()
465 "DiBX000 tuner I2C bus", mst) != 0) in dibx000_init_i2c_master()
468 mst->master_i2c_adap_gpio12.dev.parent = mst->i2c_adap->dev.parent; in dibx000_init_i2c_master()
470 (&mst->master_i2c_adap_gpio12, &dibx000_i2c_master_gpio12_xfer_algo, in dibx000_init_i2c_master()
471 "DiBX000 master GPIO12 I2C bus", mst) != 0) in dibx000_init_i2c_master()
474 mst->master_i2c_adap_gpio34.dev.parent = mst->i2c_adap->dev.parent; in dibx000_init_i2c_master()
476 (&mst->master_i2c_adap_gpio34, &dibx000_i2c_master_gpio34_xfer_algo, in dibx000_init_i2c_master()
477 "DiBX000 master GPIO34 I2C bus", mst) != 0) in dibx000_init_i2c_master()
480 mst->master_i2c_adap_gpio67.dev.parent = mst->i2c_adap->dev.parent; in dibx000_init_i2c_master()
482 (&mst->master_i2c_adap_gpio67, &dibx000_i2c_gated_gpio67_algo, in dibx000_init_i2c_master()
483 "DiBX000 master GPIO67 I2C bus", mst) != 0) in dibx000_init_i2c_master()
486 /* initialize the i2c-master by closing the gate */ in dibx000_init_i2c_master()
487 dibx000_i2c_gate_ctrl(mst, mst->i2c_write_buffer, 0, 0); in dibx000_init_i2c_master()
489 ret = (i2c_transfer(i2c_adap, mst->msg, 1) == 1); in dibx000_init_i2c_master()
490 mutex_unlock(&mst->i2c_buffer_lock); in dibx000_init_i2c_master()
499 i2c_del_adapter(&mst->gated_tuner_i2c_adap); in dibx000_exit_i2c_master()
500 i2c_del_adapter(&mst->master_i2c_adap_gpio12); in dibx000_exit_i2c_master()
501 i2c_del_adapter(&mst->master_i2c_adap_gpio34); in dibx000_exit_i2c_master()
502 i2c_del_adapter(&mst->master_i2c_adap_gpio67); in dibx000_exit_i2c_master()