Lines Matching +full:npcm750 +full:- +full:i2c

1 // SPDX-License-Identifier: GPL-2.0
3 * Nuvoton NPCM7xx I2C Controller driver
11 #include <linux/i2c.h>
29 * External I2C Interface driver xfer indication values, which indicate status
58 /* I2C Bank (module had 2 banks of registers) */
64 /* Internal I2C states values (for the I2C module state machine). */
142 #define NPCM_I2CTXF_CTL 0x12 /* Tx-FIFO Control */
145 #define NPCM_I2CTXF_STS 0x1A /* Tx-FIFO Status */
146 #define NPCM_I2CRXF_STS 0x1C /* Rx-FIFO Status */
147 #define NPCM_I2CRXF_CTL 0x1E /* Rx-FIFO Control */
271 /* Status of one I2C module */
322 u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
328 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
333 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_init_params()
334 bus->rd_size = 0; in npcm_i2c_init_params()
335 bus->wr_size = 0; in npcm_i2c_init_params()
336 bus->rd_ind = 0; in npcm_i2c_init_params()
337 bus->wr_ind = 0; in npcm_i2c_init_params()
338 bus->read_block_use = false; in npcm_i2c_init_params()
339 bus->int_time_stamp = 0; in npcm_i2c_init_params()
340 bus->PEC_use = false; in npcm_i2c_init_params()
341 bus->PEC_mask = 0; in npcm_i2c_init_params()
343 if (bus->slave) in npcm_i2c_init_params()
344 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_init_params()
350 iowrite8(data, bus->reg + NPCM_I2CSDA); in npcm_i2c_wr_byte()
355 return ioread8(bus->reg + NPCM_I2CSDA); in npcm_i2c_rd_byte()
362 return !!(I2CCTL3_SCL_LVL & ioread32(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SCL()
369 return !!(I2CCTL3_SDA_LVL & ioread32(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SDA()
374 if (bus->operation == I2C_READ_OPER) in npcm_i2c_get_index()
375 return bus->rd_ind; in npcm_i2c_get_index()
376 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_get_index()
377 return bus->wr_ind; in npcm_i2c_get_index()
384 return bus->wr_size == 0 && bus->rd_size == 0; in npcm_i2c_is_quick()
394 /* select bank 0 for I2C addresses */ in npcm_i2c_disable()
399 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_disable()
404 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
406 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
408 bus->state = I2C_DISABLE; in npcm_i2c_disable()
413 u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
416 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
417 bus->state = I2C_IDLE; in npcm_i2c_enable()
426 val = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
428 iowrite8(val, bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
430 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
436 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
443 tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_tx_fifo_empty()
456 rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_rx_fifo_full()
469 val = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
471 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
478 val = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
480 iowrite8(val, bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
487 val = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
489 iowrite8(val, bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
496 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
502 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
509 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
512 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
520 * override HW issue: I2C may fail to supply stop condition in Master in npcm_i2c_master_stop()
525 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
528 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
530 if (!bus->fifo_use) in npcm_i2c_master_stop()
535 if (bus->operation == I2C_READ_OPER) in npcm_i2c_master_stop()
540 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_master_stop()
547 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
553 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
560 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
563 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
572 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
578 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
590 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
595 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
599 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
604 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
608 return -EFAULT; in npcm_i2c_slave_enable()
613 iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]); in npcm_i2c_slave_enable()
632 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
639 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
642 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_reset()
643 iowrite8(0xFF, bus->reg + NPCM_I2CST); in npcm_i2c_reset()
646 iowrite8(NPCM_I2CCST3_EO_BUSY, bus->reg + NPCM_I2CCST3); in npcm_i2c_reset()
649 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_reset()
652 if (bus->slave) { in npcm_i2c_reset()
653 addr = bus->slave->addr; in npcm_i2c_reset()
658 bus->state = I2C_IDLE; in npcm_i2c_reset()
663 return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST)); in npcm_i2c_is_master()
672 msgs = bus->msgs; in npcm_i2c_callback()
673 msgs_num = bus->msgs_num; in npcm_i2c_callback()
675 * check that transaction was not timed-out, and msgs still in npcm_i2c_callback()
681 if (completion_done(&bus->cmd_complete)) in npcm_i2c_callback()
686 bus->cmd_err = bus->msgs_num; in npcm_i2c_callback()
690 if (bus->msgs) { in npcm_i2c_callback()
697 if (completion_done(&bus->cmd_complete) == false) in npcm_i2c_callback()
698 complete(&bus->cmd_complete); in npcm_i2c_callback()
703 bus->cmd_err = -ENXIO; in npcm_i2c_callback()
704 if (bus->master_or_slave == I2C_MASTER) in npcm_i2c_callback()
705 complete(&bus->cmd_complete); in npcm_i2c_callback()
710 bus->cmd_err = -EAGAIN; in npcm_i2c_callback()
711 if (bus->master_or_slave == I2C_MASTER) in npcm_i2c_callback()
712 complete(&bus->cmd_complete); in npcm_i2c_callback()
716 /* I2C wake up */ in npcm_i2c_callback()
722 bus->operation = I2C_NO_OPER; in npcm_i2c_callback()
724 if (bus->slave) in npcm_i2c_callback()
725 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_callback()
731 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_fifo_usage()
733 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_fifo_usage()
734 if (bus->operation == I2C_READ_OPER) in npcm_i2c_fifo_usage()
736 ioread8(bus->reg + NPCM_I2CRXF_STS)); in npcm_i2c_fifo_usage()
748 size_free_fifo = I2C_HW_FIFO_SIZE - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
749 while (max_bytes-- && size_free_fifo) { in npcm_i2c_write_to_fifo_master()
750 if (bus->wr_ind < bus->wr_size) in npcm_i2c_write_to_fifo_master()
751 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_write_to_fifo_master()
754 size_free_fifo = I2C_HW_FIFO_SIZE - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
760 * configure the FIFO before using it. If nread is -1 RX FIFO will not be
767 if (!bus->fifo_use) in npcm_i2c_set_fifo()
786 if (bus->rd_ind == 0 && bus->read_block_use) { in npcm_i2c_set_fifo()
792 iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_set_fifo()
799 iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
801 iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
811 while (bytes_in_fifo--) { in npcm_i2c_read_fifo()
813 if (bus->rd_ind < bus->rd_size) in npcm_i2c_read_fifo()
814 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_read_fifo()
824 iowrite8(val, bus->reg + NPCM_I2CST); in npcm_i2c_clear_master_status()
847 slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]); in npcm_i2c_get_slave_addr()
863 if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add) in npcm_i2c_remove_slave_addr()
864 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_remove_slave_addr()
878 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_write_fifo_slave()
879 while (max_bytes-- && I2C_HW_FIFO_SIZE != npcm_i2c_fifo_usage(bus)) { in npcm_i2c_write_fifo_slave()
880 if (bus->slv_wr_size <= 0) in npcm_i2c_write_fifo_slave()
882 bus->slv_wr_ind = bus->slv_wr_ind % I2C_HW_FIFO_SIZE; in npcm_i2c_write_fifo_slave()
883 npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]); in npcm_i2c_write_fifo_slave()
884 bus->slv_wr_ind++; in npcm_i2c_write_fifo_slave()
885 bus->slv_wr_ind = bus->slv_wr_ind % I2C_HW_FIFO_SIZE; in npcm_i2c_write_fifo_slave()
886 bus->slv_wr_size--; in npcm_i2c_write_fifo_slave()
894 if (!bus->slave) in npcm_i2c_read_fifo_slave()
897 while (bytes_in_fifo--) { in npcm_i2c_read_fifo_slave()
900 bus->slv_rd_ind = bus->slv_rd_ind % I2C_HW_FIFO_SIZE; in npcm_i2c_read_fifo_slave()
901 bus->slv_rd_buf[bus->slv_rd_ind] = data; in npcm_i2c_read_fifo_slave()
902 bus->slv_rd_ind++; in npcm_i2c_read_fifo_slave()
905 if (bus->slv_rd_ind == 1 && bus->read_block_use) in npcm_i2c_read_fifo_slave()
906 bus->slv_rd_size = data + bus->PEC_use + 1; in npcm_i2c_read_fifo_slave()
915 int ret = bus->slv_wr_ind; in npcm_i2c_slave_get_wr_buf()
919 if (bus->slv_wr_size >= I2C_HW_FIFO_SIZE) in npcm_i2c_slave_get_wr_buf()
921 i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value); in npcm_i2c_slave_get_wr_buf()
922 ind = (bus->slv_wr_ind + bus->slv_wr_size) % I2C_HW_FIFO_SIZE; in npcm_i2c_slave_get_wr_buf()
923 bus->slv_wr_buf[ind] = value; in npcm_i2c_slave_get_wr_buf()
924 bus->slv_wr_size++; in npcm_i2c_slave_get_wr_buf()
925 i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value); in npcm_i2c_slave_get_wr_buf()
927 return I2C_HW_FIFO_SIZE - ret; in npcm_i2c_slave_get_wr_buf()
934 for (i = 0; i < bus->slv_rd_ind; i++) in npcm_i2c_slave_send_rd_buf()
935 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED, in npcm_i2c_slave_send_rd_buf()
936 &bus->slv_rd_buf[i]); in npcm_i2c_slave_send_rd_buf()
941 if (bus->slv_rd_ind) { in npcm_i2c_slave_send_rd_buf()
942 bus->slv_wr_size = 0; in npcm_i2c_slave_send_rd_buf()
943 bus->slv_wr_ind = 0; in npcm_i2c_slave_send_rd_buf()
946 bus->slv_rd_ind = 0; in npcm_i2c_slave_send_rd_buf()
947 bus->slv_rd_size = bus->adap.quirks->max_read_len; in npcm_i2c_slave_send_rd_buf()
956 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_receive()
957 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_receive()
958 bus->slv_rd_size = nread; in npcm_i2c_slave_receive()
959 bus->slv_rd_ind = 0; in npcm_i2c_slave_receive()
961 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_slave_receive()
962 iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_slave_receive()
973 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_xmit()
974 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_xmit()
987 * NACK on read will be once reached to bus->adap->quirks->max_read_len.
997 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_slave_wr_buf_sync()
1001 bus->slv_wr_size >= I2C_HW_FIFO_SIZE) in npcm_i2c_slave_wr_buf_sync()
1005 bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1006 bus->slv_wr_size = bus->slv_wr_size + left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1008 if (bus->slv_wr_ind < 0) in npcm_i2c_slave_wr_buf_sync()
1009 bus->slv_wr_ind += I2C_HW_FIFO_SIZE; in npcm_i2c_slave_wr_buf_sync()
1014 if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) { in npcm_i2c_slave_rd_wr()
1019 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_rd_wr()
1020 npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len, in npcm_i2c_slave_rd_wr()
1021 bus->slv_wr_buf); in npcm_i2c_slave_rd_wr()
1029 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_rd_wr()
1031 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_slave_rd_wr()
1033 npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len, in npcm_i2c_slave_rd_wr()
1034 bus->slv_rd_buf); in npcm_i2c_slave_rd_wr()
1042 u8 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1046 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_int_slave_handler()
1048 if (bus->fifo_use) in npcm_i2c_int_slave_handler()
1051 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1054 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1055 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1056 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1062 iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1073 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_int_slave_handler()
1076 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1078 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1084 if (completion_done(&bus->cmd_complete) == false) { in npcm_i2c_int_slave_handler()
1085 bus->cmd_err = -EIO; in npcm_i2c_int_slave_handler()
1086 complete(&bus->cmd_complete); in npcm_i2c_int_slave_handler()
1088 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1089 iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1097 bus->stop_ind = I2C_SLAVE_DONE_IND; in npcm_i2c_int_slave_handler()
1099 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1106 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1113 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1114 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1115 i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0); in npcm_i2c_int_slave_handler()
1116 iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1117 if (bus->fifo_use) { in npcm_i2c_int_slave_handler()
1123 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1125 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1129 /* restart condition occurred and Rx-FIFO was not empty */ in npcm_i2c_int_slave_handler()
1130 if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR, in npcm_i2c_int_slave_handler()
1131 ioread8(bus->reg + NPCM_I2CFIF_CTS))) { in npcm_i2c_int_slave_handler()
1132 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1133 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1134 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1136 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1137 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1140 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1150 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1154 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_int_slave_handler()
1155 iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1157 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1159 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED, in npcm_i2c_int_slave_handler()
1161 bus->operation = I2C_READ_OPER; in npcm_i2c_int_slave_handler()
1163 if (bus->own_slave_addr == 0xFF) { in npcm_i2c_int_slave_handler()
1165 val = ioread8(bus->reg + NPCM_I2CCST); in npcm_i2c_int_slave_handler()
1172 i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_int_slave_handler()
1173 i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2); in npcm_i2c_int_slave_handler()
1176 * the i2c module can response to 10 own SA. in npcm_i2c_int_slave_handler()
1186 bus->own_slave_addr = addr; in npcm_i2c_int_slave_handler()
1187 if (bus->PEC_mask & BIT(info)) in npcm_i2c_int_slave_handler()
1188 bus->PEC_use = true; in npcm_i2c_int_slave_handler()
1190 bus->PEC_use = false; in npcm_i2c_int_slave_handler()
1193 bus->own_slave_addr = 0; in npcm_i2c_int_slave_handler()
1195 bus->own_slave_addr = 0x61; in npcm_i2c_int_slave_handler()
1204 * (regular write-read mode) in npcm_i2c_int_slave_handler()
1206 if ((bus->state == I2C_OPER_STARTED && in npcm_i2c_int_slave_handler()
1207 bus->operation == I2C_READ_OPER && in npcm_i2c_int_slave_handler()
1208 bus->stop_ind == I2C_SLAVE_XMIT_IND) || in npcm_i2c_int_slave_handler()
1209 bus->stop_ind == I2C_SLAVE_RCV_IND) { in npcm_i2c_int_slave_handler()
1211 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1216 bus->stop_ind = I2C_SLAVE_XMIT_IND; in npcm_i2c_int_slave_handler()
1218 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_int_slave_handler()
1219 bus->state = I2C_SLAVE_MATCH; in npcm_i2c_int_slave_handler()
1221 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1225 /* Slave SDA status is set - tx or rx */ in npcm_i2c_int_slave_handler()
1227 (bus->fifo_use && in npcm_i2c_int_slave_handler()
1230 iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1240 struct npcm_i2c *bus = i2c_get_adapdata(client->adapter); in npcm_i2c_reg_slave()
1242 bus->slave = client; in npcm_i2c_reg_slave()
1244 if (!bus->slave) in npcm_i2c_reg_slave()
1245 return -EINVAL; in npcm_i2c_reg_slave()
1247 if (client->flags & I2C_CLIENT_TEN) in npcm_i2c_reg_slave()
1248 return -EAFNOSUPPORT; in npcm_i2c_reg_slave()
1250 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1253 bus->slv_rd_size = 0; in npcm_i2c_reg_slave()
1254 bus->slv_wr_size = 0; in npcm_i2c_reg_slave()
1255 bus->slv_rd_ind = 0; in npcm_i2c_reg_slave()
1256 bus->slv_wr_ind = 0; in npcm_i2c_reg_slave()
1257 if (client->flags & I2C_CLIENT_PEC) in npcm_i2c_reg_slave()
1258 bus->PEC_use = true; in npcm_i2c_reg_slave()
1260 dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num, in npcm_i2c_reg_slave()
1261 client->addr, bus->PEC_use); in npcm_i2c_reg_slave()
1263 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true); in npcm_i2c_reg_slave()
1269 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1275 struct npcm_i2c *bus = client->adapter->algo_data; in npcm_i2c_unreg_slave()
1278 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1279 if (!bus->slave) { in npcm_i2c_unreg_slave()
1280 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1281 return -EINVAL; in npcm_i2c_unreg_slave()
1284 npcm_i2c_remove_slave_addr(bus, client->addr); in npcm_i2c_unreg_slave()
1285 bus->slave = NULL; in npcm_i2c_unreg_slave()
1286 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1298 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1308 fifo_bytes = rcount - I2C_HW_FIFO_SIZE; in npcm_i2c_master_fifo_read()
1311 /* last bytes are about to be read - end of tx */ in npcm_i2c_master_fifo_read()
1312 bus->state = I2C_STOP_PENDING; in npcm_i2c_master_fifo_read()
1313 bus->stop_ind = ind; in npcm_i2c_master_fifo_read()
1320 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1321 npcm_i2c_set_fifo(bus, rcount, -1); in npcm_i2c_master_fifo_read()
1329 if (bus->fifo_use) in npcm_i2c_irq_master_handler_write()
1332 /* Master write operation - last byte handling */ in npcm_i2c_irq_master_handler_write()
1333 if (bus->wr_ind == bus->wr_size) { in npcm_i2c_irq_master_handler_write()
1334 if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0) in npcm_i2c_irq_master_handler_write()
1344 if (bus->rd_size == 0) { in npcm_i2c_irq_master_handler_write()
1347 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_write()
1348 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_master_handler_write()
1354 /* last write-byte written on previous int - restart */ in npcm_i2c_irq_master_handler_write()
1355 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_master_handler_write()
1360 * Receiving one byte only - stall after successful in npcm_i2c_irq_master_handler_write()
1363 * unintentionally NACK the next multi-byte read. in npcm_i2c_irq_master_handler_write()
1365 if (bus->rd_size == 1) in npcm_i2c_irq_master_handler_write()
1369 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_master_handler_write()
1371 npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1); in npcm_i2c_irq_master_handler_write()
1375 if (!bus->fifo_use || bus->wr_size == 1) { in npcm_i2c_irq_master_handler_write()
1376 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_irq_master_handler_write()
1378 wcount = bus->wr_size - bus->wr_ind; in npcm_i2c_irq_master_handler_write()
1379 npcm_i2c_set_fifo(bus, -1, wcount); in npcm_i2c_irq_master_handler_write()
1392 block_extra_bytes_size = bus->read_block_use + bus->PEC_use; in npcm_i2c_irq_master_handler_read()
1398 if (bus->rd_ind == 0) { /* first byte handling: */ in npcm_i2c_irq_master_handler_read()
1399 if (bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1403 bus->rd_size = data + block_extra_bytes_size; in npcm_i2c_irq_master_handler_read()
1404 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_irq_master_handler_read()
1407 if (bus->fifo_use) { in npcm_i2c_irq_master_handler_read()
1408 data = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1410 iowrite8(data, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1413 npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1); in npcm_i2c_irq_master_handler_read()
1420 if (bus->rd_size == block_extra_bytes_size && in npcm_i2c_irq_master_handler_read()
1421 bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1422 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_read()
1423 bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND; in npcm_i2c_irq_master_handler_read()
1424 bus->cmd_err = -EIO; in npcm_i2c_irq_master_handler_read()
1436 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_nmatch()
1438 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_nmatch()
1439 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_nmatch()
1447 if (bus->nack_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_nack()
1448 bus->nack_cnt++; in npcm_i2c_irq_handle_nack()
1450 if (bus->fifo_use) { in npcm_i2c_irq_handle_nack()
1455 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_irq_handle_nack()
1456 bus->wr_ind -= npcm_i2c_fifo_usage(bus); in npcm_i2c_irq_handle_nack()
1459 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_nack()
1463 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_irq_handle_nack()
1475 readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val, in npcm_i2c_irq_handle_nack()
1478 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_nack()
1485 npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind); in npcm_i2c_irq_handle_nack()
1491 if (bus->ber_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_ber()
1492 bus->ber_cnt++; in npcm_i2c_irq_handle_ber()
1493 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_ber()
1500 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_irq_handle_ber()
1502 bus->cmd_err = -EAGAIN; in npcm_i2c_irq_handle_ber()
1503 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_ber()
1505 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_ber()
1512 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_eob()
1513 npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind); in npcm_i2c_irq_handle_eob()
1520 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_handle_stall_after_start()
1521 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_handle_stall_after_start()
1524 } else if ((bus->rd_size == 1) && !bus->read_block_use) { in npcm_i2c_irq_handle_stall_after_start()
1526 * Receiving one byte only - set NACK after ensuring in npcm_i2c_irq_handle_stall_after_start()
1532 /* Reset stall-after-address-byte */ in npcm_i2c_irq_handle_stall_after_start()
1536 iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_stall_after_start()
1539 /* SDA status is set - TX or RX, master */
1547 if (bus->state == I2C_IDLE) { in npcm_i2c_irq_handle_sda()
1548 bus->stop_ind = I2C_WAKE_UP_IND; in npcm_i2c_irq_handle_sda()
1550 if (npcm_i2c_is_quick(bus) || bus->read_block_use) in npcm_i2c_irq_handle_sda()
1560 * Receiving one byte only - stall after successful completion in npcm_i2c_irq_handle_sda()
1563 * multi-byte read in npcm_i2c_irq_handle_sda()
1565 if (bus->wr_size == 0 && bus->rd_size == 1) in npcm_i2c_irq_handle_sda()
1568 /* Initiate I2C master tx */ in npcm_i2c_irq_handle_sda()
1573 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1578 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1580 /* re-enable */ in npcm_i2c_irq_handle_sda()
1582 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1591 if (bus->wr_size) in npcm_i2c_irq_handle_sda()
1592 npcm_i2c_set_fifo(bus, -1, bus->wr_size); in npcm_i2c_irq_handle_sda()
1594 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_handle_sda()
1596 bus->state = I2C_OPER_STARTED; in npcm_i2c_irq_handle_sda()
1598 if (npcm_i2c_is_quick(bus) || bus->wr_size) in npcm_i2c_irq_handle_sda()
1599 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_irq_handle_sda()
1601 npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0)); in npcm_i2c_irq_handle_sda()
1605 bus->operation = I2C_WRITE_OPER; in npcm_i2c_irq_handle_sda()
1608 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_handle_sda()
1617 int ret = -EIO; in npcm_i2c_int_master_handler()
1619 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_master_handler()
1639 ioread8(bus->reg + NPCM_I2CCTL1)) == 1) && in npcm_i2c_int_master_handler()
1641 ioread8(bus->reg + NPCM_I2CCST3)))) { in npcm_i2c_int_master_handler()
1652 /* SDA status is set - TX or RX, master */ in npcm_i2c_int_master_handler()
1654 (bus->fifo_use && in npcm_i2c_int_master_handler()
1669 int status = -ENOTRECOVERABLE; in npcm_i2c_recovery_tgclk()
1675 dev_dbg(bus->dev, "bus%d recovery skipped, bus not stuck", in npcm_i2c_recovery_tgclk()
1676 bus->num); in npcm_i2c_recovery_tgclk()
1684 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1687 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_recovery_tgclk()
1688 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_recovery_tgclk()
1695 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1698 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1699 npcm_i2c_set_fifo(bus, -1, 0); in npcm_i2c_recovery_tgclk()
1704 iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1711 } while (!done && iter--); in npcm_i2c_recovery_tgclk()
1713 /* If SDA line is released: send start-addr-stop, to re-sync. */ in npcm_i2c_recovery_tgclk()
1716 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1734 status = -ENOTRECOVERABLE; in npcm_i2c_recovery_tgclk()
1736 if (bus->rec_fail_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1737 bus->rec_fail_cnt++; in npcm_i2c_recovery_tgclk()
1739 if (bus->rec_succ_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1740 bus->rec_succ_cnt++; in npcm_i2c_recovery_tgclk()
1749 struct i2c_bus_recovery_info *rinfo = &bus->rinfo; in npcm_i2c_recovery_init()
1751 rinfo->recover_bus = npcm_i2c_recovery_tgclk; in npcm_i2c_recovery_init()
1754 * npcm i2c HW allows direct reading of SCL and SDA. in npcm_i2c_recovery_init()
1759 rinfo->get_scl = npcm_i2c_get_SCL; in npcm_i2c_recovery_init()
1760 rinfo->get_sda = npcm_i2c_get_SDA; in npcm_i2c_recovery_init()
1761 _adap->bus_recovery_info = rinfo; in npcm_i2c_recovery_init()
1771 * NPCM7XX i2c module timing parameters are depenent on module core clk (APB)
1789 src_clk_khz = bus->apb_clk / 1000; in npcm_i2c_init_clk()
1791 bus->bus_freq = bus_freq_hz; in npcm_i2c_init_clk()
1798 return -EDOM; in npcm_i2c_init_clk()
1815 return -EDOM; in npcm_i2c_init_clk()
1839 return -EDOM; in npcm_i2c_init_clk()
1848 * SDA hold time: (HLDT-7) * T(CLK) >= 120 in npcm_i2c_init_clk()
1860 return -EINVAL; in npcm_i2c_init_clk()
1867 return -EDOM; in npcm_i2c_init_clk()
1872 bus->reg + NPCM_I2CCTL2); in npcm_i2c_init_clk()
1876 bus->reg + NPCM_I2CCTL3); in npcm_i2c_init_clk()
1884 * k1 = 2 * SCLLT7-0 -> Low Time = k1 / 2 in npcm_i2c_init_clk()
1885 * k2 = 2 * SCLLT7-0 -> High Time = k2 / 2 in npcm_i2c_init_clk()
1887 iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT); in npcm_i2c_init_clk()
1888 iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT); in npcm_i2c_init_clk()
1890 iowrite8(dbnct, bus->reg + NPCM_I2CCTL5); in npcm_i2c_init_clk()
1893 iowrite8(hldt, bus->reg + NPCM_I2CCTL4); in npcm_i2c_init_clk()
1908 if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) || in npcm_i2c_init_module()
1910 return -EINVAL; in npcm_i2c_init_module()
1915 if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) { in npcm_i2c_init_module()
1916 bus->fifo_use = true; in npcm_i2c_init_module()
1918 val = ioread8(bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1920 iowrite8(val, bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1923 bus->fifo_use = false; in npcm_i2c_init_module()
1926 /* Configure I2C module clock frequency */ in npcm_i2c_init_module()
1929 dev_err(bus->dev, "npcm_i2c_init_clk failed\n"); in npcm_i2c_init_module()
1935 bus->state = I2C_IDLE; in npcm_i2c_init_module()
1936 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1938 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1953 bus->state = I2C_DISABLE; in __npcm_i2c_init()
1954 bus->master_or_slave = I2C_SLAVE; in __npcm_i2c_init()
1955 bus->int_time_stamp = 0; in __npcm_i2c_init()
1957 bus->slave = NULL; in __npcm_i2c_init()
1960 ret = device_property_read_u32(&pdev->dev, "clock-frequency", in __npcm_i2c_init()
1963 dev_info(&pdev->dev, "Could not read clock-frequency property"); in __npcm_i2c_init()
1969 dev_err(&pdev->dev, "npcm_i2c_init_module failed\n"); in __npcm_i2c_init()
1981 bus->master_or_slave = I2C_MASTER; in npcm_i2c_bus_irq()
1983 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_bus_irq()
1984 bus->int_time_stamp = jiffies; in npcm_i2c_bus_irq()
1989 if (bus->slave) { in npcm_i2c_bus_irq()
1990 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_bus_irq()
2002 if (bus->state != I2C_IDLE) { in npcm_i2c_master_start_xmit()
2003 bus->cmd_err = -EBUSY; in npcm_i2c_master_start_xmit()
2006 bus->dest_addr = slave_addr << 1; in npcm_i2c_master_start_xmit()
2007 bus->wr_buf = write_data; in npcm_i2c_master_start_xmit()
2008 bus->wr_size = nwrite; in npcm_i2c_master_start_xmit()
2009 bus->wr_ind = 0; in npcm_i2c_master_start_xmit()
2010 bus->rd_buf = read_data; in npcm_i2c_master_start_xmit()
2011 bus->rd_size = nread; in npcm_i2c_master_start_xmit()
2012 bus->rd_ind = 0; in npcm_i2c_master_start_xmit()
2013 bus->PEC_use = 0; in npcm_i2c_master_start_xmit()
2015 /* for tx PEC is appended to buffer from i2c IF. PEC flag is ignored */ in npcm_i2c_master_start_xmit()
2017 bus->PEC_use = use_PEC; in npcm_i2c_master_start_xmit()
2019 bus->read_block_use = use_read_block; in npcm_i2c_master_start_xmit()
2021 bus->operation = I2C_READ_OPER; in npcm_i2c_master_start_xmit()
2023 bus->operation = I2C_WRITE_OPER; in npcm_i2c_master_start_xmit()
2024 if (bus->fifo_use) { in npcm_i2c_master_start_xmit()
2029 i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2032 iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2035 bus->state = I2C_IDLE; in npcm_i2c_master_start_xmit()
2057 if (bus->state == I2C_DISABLE) { in npcm_i2c_master_xfer()
2058 dev_err(bus->dev, "I2C%d module is disabled", bus->num); in npcm_i2c_master_xfer()
2059 return -EINVAL; in npcm_i2c_master_xfer()
2063 slave_addr = msg0->addr; in npcm_i2c_master_xfer()
2064 if (msg0->flags & I2C_M_RD) { /* read */ in npcm_i2c_master_xfer()
2067 read_data = msg0->buf; in npcm_i2c_master_xfer()
2068 if (msg0->flags & I2C_M_RECV_LEN) { in npcm_i2c_master_xfer()
2071 if (msg0->flags & I2C_CLIENT_PEC) in npcm_i2c_master_xfer()
2074 nread = msg0->len; in npcm_i2c_master_xfer()
2077 nwrite = msg0->len; in npcm_i2c_master_xfer()
2078 write_data = msg0->buf; in npcm_i2c_master_xfer()
2083 read_data = msg1->buf; in npcm_i2c_master_xfer()
2084 if (msg1->flags & I2C_M_RECV_LEN) { in npcm_i2c_master_xfer()
2087 if (msg1->flags & I2C_CLIENT_PEC) in npcm_i2c_master_xfer()
2090 nread = msg1->len; in npcm_i2c_master_xfer()
2101 timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); in npcm_i2c_master_xfer()
2104 dev_err(bus->dev, "i2c%d buffer too big\n", bus->num); in npcm_i2c_master_xfer()
2105 return -EINVAL; in npcm_i2c_master_xfer()
2115 spin_lock_irqsave(&bus->lock, flags); in npcm_i2c_master_xfer()
2116 bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB; in npcm_i2c_master_xfer()
2118 if (!bus_busy && bus->slave) in npcm_i2c_master_xfer()
2119 iowrite8((bus->slave->addr & 0x7F), in npcm_i2c_master_xfer()
2120 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2122 spin_unlock_irqrestore(&bus->lock, flags); in npcm_i2c_master_xfer()
2127 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_master_xfer()
2130 return -EAGAIN; in npcm_i2c_master_xfer()
2134 bus->dest_addr = slave_addr; in npcm_i2c_master_xfer()
2135 bus->msgs = msgs; in npcm_i2c_master_xfer()
2136 bus->msgs_num = num; in npcm_i2c_master_xfer()
2137 bus->cmd_err = 0; in npcm_i2c_master_xfer()
2138 bus->read_block_use = read_block; in npcm_i2c_master_xfer()
2140 reinit_completion(&bus->cmd_complete); in npcm_i2c_master_xfer()
2144 ret = -EBUSY; in npcm_i2c_master_xfer()
2146 if (ret != -EBUSY) { in npcm_i2c_master_xfer()
2147 time_left = wait_for_completion_timeout(&bus->cmd_complete, in npcm_i2c_master_xfer()
2151 if (bus->timeout_cnt < ULLONG_MAX) in npcm_i2c_master_xfer()
2152 bus->timeout_cnt++; in npcm_i2c_master_xfer()
2153 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_master_xfer()
2155 bus->cmd_err = -EIO; in npcm_i2c_master_xfer()
2156 bus->state = I2C_IDLE; in npcm_i2c_master_xfer()
2160 ret = bus->cmd_err; in npcm_i2c_master_xfer()
2163 if (bus->cmd_err == -EAGAIN) in npcm_i2c_master_xfer()
2171 if (bus->cmd_err && in npcm_i2c_master_xfer()
2172 (NPCM_I2CRXF_CTL_LAST_PEC & ioread8(bus->reg + NPCM_I2CRXF_CTL))) in npcm_i2c_master_xfer()
2177 if (bus->slave) in npcm_i2c_master_xfer()
2178 iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN, in npcm_i2c_master_xfer()
2179 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2181 return bus->cmd_err; in npcm_i2c_master_xfer()
2208 /* i2c debugfs directory: used to keep health monitor of i2c devices */
2218 d = debugfs_create_dir(dev_name(&pdev->dev), npcm_i2c_debugfs_dir); in npcm_i2c_init_debugfs()
2221 debugfs_create_u64("ber_cnt", 0444, d, &bus->ber_cnt); in npcm_i2c_init_debugfs()
2222 debugfs_create_u64("nack_cnt", 0444, d, &bus->nack_cnt); in npcm_i2c_init_debugfs()
2223 debugfs_create_u64("rec_succ_cnt", 0444, d, &bus->rec_succ_cnt); in npcm_i2c_init_debugfs()
2224 debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt); in npcm_i2c_init_debugfs()
2225 debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt); in npcm_i2c_init_debugfs()
2227 bus->debugfs = d; in npcm_i2c_init_debugfs()
2240 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); in npcm_i2c_probe_bus()
2242 return -ENOMEM; in npcm_i2c_probe_bus()
2244 bus->dev = &pdev->dev; in npcm_i2c_probe_bus()
2246 bus->num = of_alias_get_id(pdev->dev.of_node, "i2c"); in npcm_i2c_probe_bus()
2248 i2c_clk = devm_clk_get(&pdev->dev, NULL); in npcm_i2c_probe_bus()
2251 bus->apb_clk = clk_get_rate(i2c_clk); in npcm_i2c_probe_bus()
2253 gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr"); in npcm_i2c_probe_bus()
2258 clk_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-clk"); in npcm_i2c_probe_bus()
2262 bus->reg = devm_platform_ioremap_resource(pdev, 0); in npcm_i2c_probe_bus()
2263 if (IS_ERR(bus->reg)) in npcm_i2c_probe_bus()
2264 return PTR_ERR(bus->reg); in npcm_i2c_probe_bus()
2266 spin_lock_init(&bus->lock); in npcm_i2c_probe_bus()
2267 init_completion(&bus->cmd_complete); in npcm_i2c_probe_bus()
2269 adap = &bus->adap; in npcm_i2c_probe_bus()
2270 adap->owner = THIS_MODULE; in npcm_i2c_probe_bus()
2271 adap->retries = 3; in npcm_i2c_probe_bus()
2272 adap->timeout = HZ; in npcm_i2c_probe_bus()
2273 adap->algo = &npcm_i2c_algo; in npcm_i2c_probe_bus()
2274 adap->quirks = &npcm_i2c_quirks; in npcm_i2c_probe_bus()
2275 adap->algo_data = bus; in npcm_i2c_probe_bus()
2276 adap->dev.parent = &pdev->dev; in npcm_i2c_probe_bus()
2277 adap->dev.of_node = pdev->dev.of_node; in npcm_i2c_probe_bus()
2278 adap->nr = pdev->id; in npcm_i2c_probe_bus()
2284 ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0, in npcm_i2c_probe_bus()
2285 dev_name(bus->dev), bus); in npcm_i2c_probe_bus()
2297 snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d", in npcm_i2c_probe_bus()
2298 bus->num); in npcm_i2c_probe_bus()
2299 ret = i2c_add_numbered_adapter(&bus->adap); in npcm_i2c_probe_bus()
2313 debugfs_remove_recursive(bus->debugfs); in npcm_i2c_remove_bus()
2314 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2316 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2317 i2c_del_adapter(&bus->adap); in npcm_i2c_remove_bus()
2322 { .compatible = "nuvoton,npcm750-i2c", },
2331 .name = "nuvoton-i2c",
2354 MODULE_DESCRIPTION("Nuvoton I2C Bus Driver");