2 * mxl111sf-i2c.c - driver for the MaxLinear MXL111SF
4 * Copyright (C) 2010-2014 Michael Krufky <mkrufky@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include "mxl111sf-i2c.h"
20 /* SW-I2C ----------------------------------------------------------------- */
22 #define SW_I2C_ADDR 0x1a
23 #define SW_I2C_EN 0x02
24 #define SW_SCL_OUT 0x04
25 #define SW_SDA_OUT 0x08
26 #define SW_SDA_IN 0x04
28 #define SW_I2C_BUSY_ADDR 0x2f
29 #define SW_I2C_BUSY 0x02
31 static int mxl111sf_i2c_bitbang_sendbyte(struct mxl111sf_state *state,
37 mxl_i2c("(0x%02x)", byte);
39 ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
43 for (i = 0; i < 8; i++) {
45 data = (byte & (0x80 >> i)) ? SW_SDA_OUT : 0;
47 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
48 0x10 | SW_I2C_EN | data);
52 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
53 0x10 | SW_I2C_EN | data | SW_SCL_OUT);
57 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
58 0x10 | SW_I2C_EN | data);
63 /* last bit was 0 so we need to release SDA */
65 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
66 0x10 | SW_I2C_EN | SW_SDA_OUT);
71 /* CLK high for ACK readback */
72 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
73 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
77 ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
81 /* drop the CLK after getting ACK, SDA will go high right away */
82 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
83 0x10 | SW_I2C_EN | SW_SDA_OUT);
93 static int mxl111sf_i2c_bitbang_recvbyte(struct mxl111sf_state *state,
104 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
105 0x10 | SW_I2C_EN | SW_SDA_OUT);
109 for (i = 0; i < 8; i++) {
110 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
112 SW_SCL_OUT | SW_SDA_OUT);
116 ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
120 if (data & SW_SDA_IN)
123 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
124 0x10 | SW_I2C_EN | SW_SDA_OUT);
133 static int mxl111sf_i2c_start(struct mxl111sf_state *state)
139 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
140 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
144 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
145 0x10 | SW_I2C_EN | SW_SCL_OUT);
149 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
150 0x10 | SW_I2C_EN); /* start */
156 static int mxl111sf_i2c_stop(struct mxl111sf_state *state)
162 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
163 0x10 | SW_I2C_EN); /* stop */
167 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
168 0x10 | SW_I2C_EN | SW_SCL_OUT);
172 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
173 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
177 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
178 0x10 | SW_SCL_OUT | SW_SDA_OUT);
184 static int mxl111sf_i2c_ack(struct mxl111sf_state *state)
191 ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &b);
195 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
201 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
202 0x10 | SW_I2C_EN | SW_SCL_OUT);
206 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
207 0x10 | SW_I2C_EN | SW_SDA_OUT);
213 static int mxl111sf_i2c_nack(struct mxl111sf_state *state)
219 /* SDA high to signal last byte read from slave */
220 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
221 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
225 ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
226 0x10 | SW_I2C_EN | SW_SDA_OUT);
232 /* ------------------------------------------------------------------------ */
234 static int mxl111sf_i2c_sw_xfer_msg(struct mxl111sf_state *state,
241 if (msg->flags & I2C_M_RD) {
243 ret = mxl111sf_i2c_start(state);
247 ret = mxl111sf_i2c_bitbang_sendbyte(state,
248 (msg->addr << 1) | 0x01);
250 mxl111sf_i2c_stop(state);
254 for (i = 0; i < msg->len; i++) {
255 ret = mxl111sf_i2c_bitbang_recvbyte(state,
258 mxl111sf_i2c_stop(state);
262 if (i < msg->len - 1)
263 mxl111sf_i2c_ack(state);
266 mxl111sf_i2c_nack(state);
268 ret = mxl111sf_i2c_stop(state);
274 ret = mxl111sf_i2c_start(state);
278 ret = mxl111sf_i2c_bitbang_sendbyte(state,
279 (msg->addr << 1) & 0xfe);
281 mxl111sf_i2c_stop(state);
285 for (i = 0; i < msg->len; i++) {
286 ret = mxl111sf_i2c_bitbang_sendbyte(state,
289 mxl111sf_i2c_stop(state);
294 /* FIXME: we only want to do this on the last transaction */
295 mxl111sf_i2c_stop(state);
301 /* HW-I2C ----------------------------------------------------------------- */
303 #define USB_WRITE_I2C_CMD 0x99
304 #define USB_READ_I2C_CMD 0xdd
305 #define USB_END_I2C_CMD 0xfe
307 #define USB_WRITE_I2C_CMD_LEN 26
308 #define USB_READ_I2C_CMD_LEN 24
310 #define I2C_MUX_REG 0x30
311 #define I2C_CONTROL_REG 0x00
312 #define I2C_SLAVE_ADDR_REG 0x08
313 #define I2C_DATA_REG 0x0c
314 #define I2C_INT_STATUS_REG 0x10
316 static int mxl111sf_i2c_send_data(struct mxl111sf_state *state,
319 int ret = mxl111sf_ctrl_msg(state, wdata[0],
320 &wdata[1], 25, NULL, 0);
326 static int mxl111sf_i2c_get_data(struct mxl111sf_state *state,
327 u8 index, u8 *wdata, u8 *rdata)
329 int ret = mxl111sf_ctrl_msg(state, wdata[0],
330 &wdata[1], 25, rdata, 24);
336 static u8 mxl111sf_i2c_check_status(struct mxl111sf_state *state)
343 buf[0] = USB_READ_I2C_CMD;
346 buf[2] = I2C_INT_STATUS_REG;
350 buf[5] = USB_END_I2C_CMD;
352 mxl111sf_i2c_get_data(state, 0, buf, buf);
360 static u8 mxl111sf_i2c_check_fifo(struct mxl111sf_state *state)
367 buf[0] = USB_READ_I2C_CMD;
370 buf[2] = I2C_MUX_REG;
374 buf[5] = I2C_INT_STATUS_REG;
377 buf[8] = USB_END_I2C_CMD;
379 mxl111sf_i2c_get_data(state, 0, buf, buf);
381 if (0x08 == (buf[1] & 0x08))
384 if ((buf[5] & 0x02) == 0x02)
385 mxl_i2c("(buf[5] & 0x02) == 0x02"); /* FIXME */
390 static int mxl111sf_i2c_readagain(struct mxl111sf_state *state,
399 mxl_i2c("read %d bytes", count);
401 while ((fifo_status == 0) && (i++ < 5))
402 fifo_status = mxl111sf_i2c_check_fifo(state);
404 i2c_w_data[0] = 0xDD;
405 i2c_w_data[1] = 0x00;
407 for (i = 2; i < 26; i++)
408 i2c_w_data[i] = 0xFE;
410 for (i = 0; i < count; i++) {
411 i2c_w_data[2+(i*3)] = 0x0C;
412 i2c_w_data[3+(i*3)] = 0x00;
413 i2c_w_data[4+(i*3)] = 0x00;
416 mxl111sf_i2c_get_data(state, 0, i2c_w_data, i2c_r_data);
418 /* Check for I2C NACK status */
419 if (mxl111sf_i2c_check_status(state) == 1) {
422 for (i = 0; i < count; i++) {
423 rbuf[i] = i2c_r_data[(i*3)+1];
424 mxl_i2c("%02x\t %02x",
426 i2c_r_data[(i*3)+2]);
436 static int mxl111sf_i2c_hw_xfer_msg(struct mxl111sf_state *state,
449 mxl_i2c("addr: 0x%02x, read buff len: %d, write buff len: %d",
450 msg->addr, (msg->flags & I2C_M_RD) ? msg->len : 0,
451 (!(msg->flags & I2C_M_RD)) ? msg->len : 0);
453 for (index = 0; index < 26; index++)
454 buf[index] = USB_END_I2C_CMD;
456 /* command to indicate data payload is destined for I2C interface */
457 buf[0] = USB_WRITE_I2C_CMD;
460 /* enable I2C interface */
461 buf[2] = I2C_MUX_REG;
465 /* enable I2C interface */
466 buf[5] = I2C_MUX_REG;
470 /* set Timeout register on I2C interface */
475 /* enable Interrupts on I2C interface */
484 ret = mxl111sf_i2c_send_data(state, 0, buf);
486 /* write data on I2C bus */
487 if (!(msg->flags & I2C_M_RD) && (msg->len > 0)) {
488 mxl_i2c("%d\t%02x", msg->len, msg->buf[0]);
490 /* control register on I2C interface to initialize I2C bus */
491 buf[2] = I2C_CONTROL_REG;
493 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
495 /* I2C Slave device Address */
496 buf[5] = I2C_SLAVE_ADDR_REG;
497 buf[6] = (msg->addr);
499 buf[8] = USB_END_I2C_CMD;
500 ret = mxl111sf_i2c_send_data(state, 0, buf);
502 /* check for slave device status */
503 if (mxl111sf_i2c_check_status(state) == 1) {
504 mxl_i2c("NACK writing slave address %02x",
506 /* if NACK, stop I2C bus and exit */
507 buf[2] = I2C_CONTROL_REG;
509 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
514 /* I2C interface can do I2C operations in block of 8 bytes of
515 I2C data. calculation to figure out number of blocks of i2c
516 data required to program */
517 block_len = (msg->len / 8);
518 left_over_len = (msg->len % 8);
521 mxl_i2c("block_len %d, left_over_len %d",
522 block_len, left_over_len);
524 for (index = 0; index < block_len; index++) {
525 for (i = 0; i < 8; i++) {
526 /* write data on I2C interface */
527 buf[2+(i*3)] = I2C_DATA_REG;
528 buf[3+(i*3)] = msg->buf[(index*8)+i];
532 ret = mxl111sf_i2c_send_data(state, 0, buf);
534 /* check for I2C NACK status */
535 if (mxl111sf_i2c_check_status(state) == 1) {
536 mxl_i2c("NACK writing slave address %02x",
539 /* if NACK, stop I2C bus and exit */
540 buf[2] = I2C_CONTROL_REG;
542 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
550 for (k = 0; k < 26; k++)
551 buf[k] = USB_END_I2C_CMD;
556 for (i = 0; i < left_over_len; i++) {
557 buf[2+(i*3)] = I2C_DATA_REG;
558 buf[3+(i*3)] = msg->buf[(index*8)+i];
559 mxl_i2c("index = %d %d data %d",
560 index, i, msg->buf[(index*8)+i]);
563 ret = mxl111sf_i2c_send_data(state, 0, buf);
565 /* check for I2C NACK status */
566 if (mxl111sf_i2c_check_status(state) == 1) {
567 mxl_i2c("NACK writing slave address %02x",
570 /* if NACK, stop I2C bus and exit */
571 buf[2] = I2C_CONTROL_REG;
573 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
580 /* issue I2C STOP after write */
581 buf[2] = I2C_CONTROL_REG;
583 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
587 /* read data from I2C bus */
588 if ((msg->flags & I2C_M_RD) && (msg->len > 0)) {
589 mxl_i2c("read buf len %d", msg->len);
591 /* command to indicate data payload is
592 destined for I2C interface */
593 buf[2] = I2C_CONTROL_REG;
595 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
597 /* I2C xfer length */
599 buf[6] = (msg->len & 0xFF);
602 /* I2C slave device Address */
603 buf[8] = I2C_SLAVE_ADDR_REG;
606 buf[11] = USB_END_I2C_CMD;
607 ret = mxl111sf_i2c_send_data(state, 0, buf);
609 /* check for I2C NACK status */
610 if (mxl111sf_i2c_check_status(state) == 1) {
611 mxl_i2c("NACK reading slave address %02x",
614 /* if NACK, stop I2C bus and exit */
615 buf[2] = I2C_CONTROL_REG;
617 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
622 /* I2C interface can do I2C operations in block of 8 bytes of
623 I2C data. calculation to figure out number of blocks of
624 i2c data required to program */
625 block_len = ((msg->len) / 8);
626 left_over_len = ((msg->len) % 8);
629 mxl_i2c("block_len %d, left_over_len %d",
630 block_len, left_over_len);
632 /* command to read data from I2C interface */
633 buf[0] = USB_READ_I2C_CMD;
636 for (index = 0; index < block_len; index++) {
637 /* setup I2C read request packet on I2C interface */
638 for (i = 0; i < 8; i++) {
639 buf[2+(i*3)] = I2C_DATA_REG;
644 ret = mxl111sf_i2c_get_data(state, 0, buf, i2c_r_data);
646 /* check for I2C NACK status */
647 if (mxl111sf_i2c_check_status(state) == 1) {
648 mxl_i2c("NACK reading slave address %02x",
651 /* if NACK, stop I2C bus and exit */
652 buf[2] = I2C_CONTROL_REG;
654 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
659 /* copy data from i2c data payload to read buffer */
660 for (i = 0; i < 8; i++) {
661 rd_status[i] = i2c_r_data[(i*3)+2];
663 if (rd_status[i] == 0x04) {
665 mxl_i2c("i2c fifo empty! @ %d",
667 msg->buf[(index*8)+i] =
671 mxl111sf_i2c_readagain(
674 if (ret_status == 1) {
679 msg->buf[(index*8)+(k+i+1)] =
681 mxl_i2c("read data: %02x\t %02x",
682 msg->buf[(index*8)+(k+i)],
684 mxl_i2c("read data: %02x\t %02x",
685 msg->buf[(index*8)+(k+i+1)],
691 mxl_i2c("readagain ERROR!");
694 msg->buf[(index*8)+i] =
698 msg->buf[(index*8)+i] =
708 for (k = 0; k < 26; k++)
709 buf[k] = USB_END_I2C_CMD;
714 for (i = 0; i < left_over_len; i++) {
715 buf[2+(i*3)] = I2C_DATA_REG;
719 ret = mxl111sf_i2c_get_data(state, 0, buf,
722 /* check for I2C NACK status */
723 if (mxl111sf_i2c_check_status(state) == 1) {
724 mxl_i2c("NACK reading slave address %02x",
727 /* if NACK, stop I2C bus and exit */
728 buf[2] = I2C_CONTROL_REG;
730 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
735 for (i = 0; i < left_over_len; i++) {
736 msg->buf[(block_len*8)+i] =
738 mxl_i2c("read data: %02x\t %02x",
740 i2c_r_data[(i*3)+2]);
744 /* indicate I2C interface to issue NACK
745 after next I2C read op */
746 buf[0] = USB_WRITE_I2C_CMD;
749 /* control register */
750 buf[2] = I2C_CONTROL_REG;
752 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
754 buf[5] = USB_END_I2C_CMD;
755 ret = mxl111sf_i2c_send_data(state, 0, buf);
757 /* control register */
758 buf[2] = I2C_CONTROL_REG;
760 buf[4] = (HWI2C400) ? 0x03 : 0x0D;
764 /* STOP and disable I2C MUX */
765 buf[0] = USB_WRITE_I2C_CMD;
768 /* de-initilize I2C BUS */
769 buf[5] = USB_END_I2C_CMD;
770 mxl111sf_i2c_send_data(state, 0, buf);
772 /* Control Register */
773 buf[2] = I2C_CONTROL_REG;
777 /* disable I2C interface */
778 buf[5] = I2C_MUX_REG;
782 /* de-initilize I2C BUS */
783 buf[8] = USB_END_I2C_CMD;
784 mxl111sf_i2c_send_data(state, 0, buf);
786 /* disable I2C interface */
787 buf[2] = I2C_MUX_REG;
791 /* disable I2C interface */
792 buf[5] = I2C_MUX_REG;
796 /* disable I2C interface */
797 buf[8] = I2C_MUX_REG;
801 buf[11] = USB_END_I2C_CMD;
802 mxl111sf_i2c_send_data(state, 0, buf);
807 /* ------------------------------------------------------------------------ */
809 int mxl111sf_i2c_xfer(struct i2c_adapter *adap,
810 struct i2c_msg msg[], int num)
812 struct dvb_usb_device *d = i2c_get_adapdata(adap);
813 struct mxl111sf_state *state = d->priv;
814 int hwi2c = (state->chip_rev > MXL111SF_V6);
817 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
820 for (i = 0; i < num; i++) {
822 mxl111sf_i2c_hw_xfer_msg(state, &msg[i]) :
823 mxl111sf_i2c_sw_xfer_msg(state, &msg[i]);
825 mxl_debug_adv("failed with error %d on i2c transaction %d of %d, %sing %d bytes to/from 0x%02x",
827 (msg[i].flags & I2C_M_RD) ?
829 msg[i].len, msg[i].addr);
835 mutex_unlock(&d->i2c_mutex);
837 return i == num ? num : -EREMOTEIO;