Lines Matching +full:npcm845 +full:- +full:clk
1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/clk.h>
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 */
262 /* supported clk settings. values in Hz. */
343 u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
349 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
354 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_init_params()
355 bus->rd_size = 0; in npcm_i2c_init_params()
356 bus->wr_size = 0; in npcm_i2c_init_params()
357 bus->rd_ind = 0; in npcm_i2c_init_params()
358 bus->wr_ind = 0; in npcm_i2c_init_params()
359 bus->read_block_use = false; in npcm_i2c_init_params()
360 bus->int_time_stamp = 0; in npcm_i2c_init_params()
361 bus->PEC_use = false; in npcm_i2c_init_params()
362 bus->PEC_mask = 0; in npcm_i2c_init_params()
364 if (bus->slave) in npcm_i2c_init_params()
365 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_init_params()
371 iowrite8(data, bus->reg + NPCM_I2CSDA); in npcm_i2c_wr_byte()
376 return ioread8(bus->reg + NPCM_I2CSDA); in npcm_i2c_rd_byte()
383 return !!(I2CCTL3_SCL_LVL & ioread8(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SCL()
390 return !!(I2CCTL3_SDA_LVL & ioread8(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SDA()
395 if (bus->operation == I2C_READ_OPER) in npcm_i2c_get_index()
396 return bus->rd_ind; in npcm_i2c_get_index()
397 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_get_index()
398 return bus->wr_ind; in npcm_i2c_get_index()
405 return bus->wr_size == 0 && bus->rd_size == 0; in npcm_i2c_is_quick()
417 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_disable()
421 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
423 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
425 bus->state = I2C_DISABLE; in npcm_i2c_disable()
430 u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
433 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
434 bus->state = I2C_IDLE; in npcm_i2c_enable()
443 val = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
445 iowrite8(val, bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
447 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
453 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
460 tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_tx_fifo_empty()
462 if ((tx_fifo_sts & bus->data->txf_sts_tx_bytes) == 0) in npcm_i2c_tx_fifo_empty()
473 rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_rx_fifo_full()
475 if ((rx_fifo_sts & bus->data->rxf_sts_rx_bytes) == 0) in npcm_i2c_rx_fifo_full()
486 val = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
488 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
495 val = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
497 iowrite8(val, bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
504 val = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
506 iowrite8(val, bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
513 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
519 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
526 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
529 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
542 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
545 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
547 if (!bus->fifo_use) in npcm_i2c_master_stop()
552 if (bus->operation == I2C_READ_OPER) in npcm_i2c_master_stop()
557 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_master_stop()
564 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
570 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
577 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
580 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
589 iowrite8(val, bus->reg + NPCM_I2CST); in npcm_i2c_clear_master_status()
598 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
604 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
616 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
621 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
624 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
629 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
633 dev_err(bus->dev, "try to enable more than 2 SA not supported\n"); in npcm_i2c_slave_enable()
636 return -EFAULT; in npcm_i2c_slave_enable()
639 iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]); in npcm_i2c_slave_enable()
657 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
664 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
667 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_reset()
668 iowrite8(0xFF, bus->reg + NPCM_I2CST); in npcm_i2c_reset()
674 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_reset()
677 if (bus->slave) { in npcm_i2c_reset()
678 addr = bus->slave->addr; in npcm_i2c_reset()
686 bus->state = I2C_IDLE; in npcm_i2c_reset()
691 return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST)); in npcm_i2c_is_master()
700 msgs = bus->msgs; in npcm_i2c_callback()
701 msgs_num = bus->msgs_num; in npcm_i2c_callback()
703 * check that transaction was not timed-out, and msgs still in npcm_i2c_callback()
709 if (completion_done(&bus->cmd_complete)) in npcm_i2c_callback()
714 bus->cmd_err = bus->msgs_num; in npcm_i2c_callback()
715 if (bus->tx_complete_cnt < ULLONG_MAX) in npcm_i2c_callback()
716 bus->tx_complete_cnt++; in npcm_i2c_callback()
720 if (bus->msgs) { in npcm_i2c_callback()
727 if (completion_done(&bus->cmd_complete) == false) in npcm_i2c_callback()
728 complete(&bus->cmd_complete); in npcm_i2c_callback()
733 bus->cmd_err = -ENXIO; in npcm_i2c_callback()
734 if (bus->master_or_slave == I2C_MASTER) in npcm_i2c_callback()
735 complete(&bus->cmd_complete); in npcm_i2c_callback()
740 bus->cmd_err = -EAGAIN; in npcm_i2c_callback()
741 if (bus->master_or_slave == I2C_MASTER) in npcm_i2c_callback()
742 complete(&bus->cmd_complete); in npcm_i2c_callback()
752 bus->operation = I2C_NO_OPER; in npcm_i2c_callback()
754 if (bus->slave) in npcm_i2c_callback()
755 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_callback()
761 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_fifo_usage()
762 return (bus->data->txf_sts_tx_bytes & in npcm_i2c_fifo_usage()
763 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_fifo_usage()
764 if (bus->operation == I2C_READ_OPER) in npcm_i2c_fifo_usage()
765 return (bus->data->rxf_sts_rx_bytes & in npcm_i2c_fifo_usage()
766 ioread8(bus->reg + NPCM_I2CRXF_STS)); in npcm_i2c_fifo_usage()
778 size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
779 while (max_bytes-- && size_free_fifo) { in npcm_i2c_write_to_fifo_master()
780 if (bus->wr_ind < bus->wr_size) in npcm_i2c_write_to_fifo_master()
781 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_write_to_fifo_master()
784 size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
790 * configure the FIFO before using it. If nread is -1 RX FIFO will not be
797 if (!bus->fifo_use) in npcm_i2c_set_fifo()
805 rxf_ctl = min_t(int, nread, bus->data->fifo_size); in npcm_i2c_set_fifo()
808 if (nread <= bus->data->fifo_size) in npcm_i2c_set_fifo()
809 rxf_ctl |= bus->data->rxf_ctl_last_pec; in npcm_i2c_set_fifo()
816 if (bus->rd_ind == 0 && bus->read_block_use) { in npcm_i2c_set_fifo()
822 iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_set_fifo()
827 if (nwrite > bus->data->fifo_size) in npcm_i2c_set_fifo()
829 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
831 iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
841 while (bytes_in_fifo--) { in npcm_i2c_read_fifo()
843 if (bus->rd_ind < bus->rd_size) in npcm_i2c_read_fifo()
844 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_read_fifo()
865 dev_err(bus->dev, "get slave: try to use more than 2 SA not supported\n"); in npcm_i2c_get_slave_addr()
867 slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]); in npcm_i2c_get_slave_addr()
880 if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add) in npcm_i2c_remove_slave_addr()
881 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_remove_slave_addr()
895 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_write_fifo_slave()
896 while (max_bytes-- && bus->data->fifo_size != npcm_i2c_fifo_usage(bus)) { in npcm_i2c_write_fifo_slave()
897 if (bus->slv_wr_size <= 0) in npcm_i2c_write_fifo_slave()
899 bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1); in npcm_i2c_write_fifo_slave()
900 npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]); in npcm_i2c_write_fifo_slave()
901 bus->slv_wr_ind++; in npcm_i2c_write_fifo_slave()
902 bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1); in npcm_i2c_write_fifo_slave()
903 bus->slv_wr_size--; in npcm_i2c_write_fifo_slave()
911 if (!bus->slave) in npcm_i2c_read_fifo_slave()
914 while (bytes_in_fifo--) { in npcm_i2c_read_fifo_slave()
917 bus->slv_rd_ind = bus->slv_rd_ind & (bus->data->fifo_size - 1); in npcm_i2c_read_fifo_slave()
918 bus->slv_rd_buf[bus->slv_rd_ind] = data; in npcm_i2c_read_fifo_slave()
919 bus->slv_rd_ind++; in npcm_i2c_read_fifo_slave()
922 if (bus->slv_rd_ind == 1 && bus->read_block_use) in npcm_i2c_read_fifo_slave()
923 bus->slv_rd_size = data + bus->PEC_use + 1; in npcm_i2c_read_fifo_slave()
932 int ret = bus->slv_wr_ind; in npcm_i2c_slave_get_wr_buf()
935 for (i = 0; i < bus->data->fifo_size; i++) { in npcm_i2c_slave_get_wr_buf()
936 if (bus->slv_wr_size >= bus->data->fifo_size) in npcm_i2c_slave_get_wr_buf()
938 if (bus->state == I2C_SLAVE_MATCH) { in npcm_i2c_slave_get_wr_buf()
939 i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value); in npcm_i2c_slave_get_wr_buf()
940 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_get_wr_buf()
942 i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value); in npcm_i2c_slave_get_wr_buf()
944 ind = (bus->slv_wr_ind + bus->slv_wr_size) & (bus->data->fifo_size - 1); in npcm_i2c_slave_get_wr_buf()
945 bus->slv_wr_buf[ind] = value; in npcm_i2c_slave_get_wr_buf()
946 bus->slv_wr_size++; in npcm_i2c_slave_get_wr_buf()
948 return bus->data->fifo_size - ret; in npcm_i2c_slave_get_wr_buf()
955 for (i = 0; i < bus->slv_rd_ind; i++) in npcm_i2c_slave_send_rd_buf()
956 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED, in npcm_i2c_slave_send_rd_buf()
957 &bus->slv_rd_buf[i]); in npcm_i2c_slave_send_rd_buf()
962 if (bus->slv_rd_ind) { in npcm_i2c_slave_send_rd_buf()
963 bus->slv_wr_size = 0; in npcm_i2c_slave_send_rd_buf()
964 bus->slv_wr_ind = 0; in npcm_i2c_slave_send_rd_buf()
967 bus->slv_rd_ind = 0; in npcm_i2c_slave_send_rd_buf()
968 bus->slv_rd_size = bus->adap.quirks->max_read_len; in npcm_i2c_slave_send_rd_buf()
977 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_receive()
978 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_receive()
979 bus->slv_rd_size = nread; in npcm_i2c_slave_receive()
980 bus->slv_rd_ind = 0; in npcm_i2c_slave_receive()
982 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_slave_receive()
983 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_slave_receive()
994 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_xmit()
1007 * NACK on read will be once reached to bus->adap->quirks->max_read_len.
1016 left_in_fifo = bus->data->txf_sts_tx_bytes & in npcm_i2c_slave_wr_buf_sync()
1017 ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_slave_wr_buf_sync()
1020 if (left_in_fifo >= bus->data->fifo_size || in npcm_i2c_slave_wr_buf_sync()
1021 bus->slv_wr_size >= bus->data->fifo_size) in npcm_i2c_slave_wr_buf_sync()
1025 bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1026 bus->slv_wr_size = bus->slv_wr_size + left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1028 if (bus->slv_wr_ind < 0) in npcm_i2c_slave_wr_buf_sync()
1029 bus->slv_wr_ind += bus->data->fifo_size; in npcm_i2c_slave_wr_buf_sync()
1034 if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) { in npcm_i2c_slave_rd_wr()
1039 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_rd_wr()
1040 npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len, in npcm_i2c_slave_rd_wr()
1041 bus->slv_wr_buf); in npcm_i2c_slave_rd_wr()
1049 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_rd_wr()
1051 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_slave_rd_wr()
1053 npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len, in npcm_i2c_slave_rd_wr()
1054 bus->slv_rd_buf); in npcm_i2c_slave_rd_wr()
1062 u8 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1066 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_int_slave_handler()
1068 if (bus->fifo_use) in npcm_i2c_int_slave_handler()
1071 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1074 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1075 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1076 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1082 iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1093 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_int_slave_handler()
1096 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1098 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1104 if (completion_done(&bus->cmd_complete) == false) { in npcm_i2c_int_slave_handler()
1105 bus->cmd_err = -EIO; in npcm_i2c_int_slave_handler()
1106 complete(&bus->cmd_complete); in npcm_i2c_int_slave_handler()
1108 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1109 iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1117 bus->stop_ind = I2C_SLAVE_DONE_IND; in npcm_i2c_int_slave_handler()
1119 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1126 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1133 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1134 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1135 i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0); in npcm_i2c_int_slave_handler()
1136 iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1137 if (bus->fifo_use) { in npcm_i2c_int_slave_handler()
1143 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1145 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1149 /* restart condition occurred and Rx-FIFO was not empty */ in npcm_i2c_int_slave_handler()
1150 if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR, in npcm_i2c_int_slave_handler()
1151 ioread8(bus->reg + NPCM_I2CFIF_CTS))) { in npcm_i2c_int_slave_handler()
1152 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1153 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1154 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1156 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1157 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1160 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1170 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1174 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_int_slave_handler()
1175 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1177 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1179 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED, in npcm_i2c_int_slave_handler()
1181 bus->operation = I2C_READ_OPER; in npcm_i2c_int_slave_handler()
1183 if (bus->own_slave_addr == 0xFF) { in npcm_i2c_int_slave_handler()
1185 val = ioread8(bus->reg + NPCM_I2CCST); in npcm_i2c_int_slave_handler()
1192 i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_int_slave_handler()
1193 i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2); in npcm_i2c_int_slave_handler()
1206 bus->own_slave_addr = addr; in npcm_i2c_int_slave_handler()
1207 if (bus->PEC_mask & BIT(info)) in npcm_i2c_int_slave_handler()
1208 bus->PEC_use = true; in npcm_i2c_int_slave_handler()
1210 bus->PEC_use = false; in npcm_i2c_int_slave_handler()
1213 bus->own_slave_addr = 0; in npcm_i2c_int_slave_handler()
1215 bus->own_slave_addr = 0x61; in npcm_i2c_int_slave_handler()
1224 * (regular write-read mode) in npcm_i2c_int_slave_handler()
1226 if ((bus->state == I2C_OPER_STARTED && in npcm_i2c_int_slave_handler()
1227 bus->operation == I2C_READ_OPER && in npcm_i2c_int_slave_handler()
1228 bus->stop_ind == I2C_SLAVE_XMIT_IND) || in npcm_i2c_int_slave_handler()
1229 bus->stop_ind == I2C_SLAVE_RCV_IND) { in npcm_i2c_int_slave_handler()
1231 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1236 bus->stop_ind = I2C_SLAVE_XMIT_IND; in npcm_i2c_int_slave_handler()
1238 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_int_slave_handler()
1239 bus->state = I2C_SLAVE_MATCH; in npcm_i2c_int_slave_handler()
1241 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1245 /* Slave SDA status is set - tx or rx */ in npcm_i2c_int_slave_handler()
1247 (bus->fifo_use && in npcm_i2c_int_slave_handler()
1250 iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1269 struct npcm_i2c *bus = i2c_get_adapdata(client->adapter); in npcm_i2c_reg_slave()
1271 bus->slave = client; in npcm_i2c_reg_slave()
1273 if (!bus->slave) in npcm_i2c_reg_slave()
1274 return -EINVAL; in npcm_i2c_reg_slave()
1276 if (client->flags & I2C_CLIENT_TEN) in npcm_i2c_reg_slave()
1277 return -EAFNOSUPPORT; in npcm_i2c_reg_slave()
1279 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1282 bus->slv_rd_size = 0; in npcm_i2c_reg_slave()
1283 bus->slv_wr_size = 0; in npcm_i2c_reg_slave()
1284 bus->slv_rd_ind = 0; in npcm_i2c_reg_slave()
1285 bus->slv_wr_ind = 0; in npcm_i2c_reg_slave()
1286 if (client->flags & I2C_CLIENT_PEC) in npcm_i2c_reg_slave()
1287 bus->PEC_use = true; in npcm_i2c_reg_slave()
1289 dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num, in npcm_i2c_reg_slave()
1290 client->addr, bus->PEC_use); in npcm_i2c_reg_slave()
1292 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true); in npcm_i2c_reg_slave()
1298 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1304 struct npcm_i2c *bus = client->adapter->algo_data; in npcm_i2c_unreg_slave()
1307 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1308 if (!bus->slave) { in npcm_i2c_unreg_slave()
1309 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1310 return -EINVAL; in npcm_i2c_unreg_slave()
1313 npcm_i2c_remove_slave_addr(bus, client->addr); in npcm_i2c_unreg_slave()
1314 bus->slave = NULL; in npcm_i2c_unreg_slave()
1315 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1327 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1336 if (rcount < (2 * bus->data->fifo_size) && rcount > bus->data->fifo_size) in npcm_i2c_master_fifo_read()
1337 fifo_bytes = rcount - bus->data->fifo_size; in npcm_i2c_master_fifo_read()
1340 /* last bytes are about to be read - end of tx */ in npcm_i2c_master_fifo_read()
1341 bus->state = I2C_STOP_PENDING; in npcm_i2c_master_fifo_read()
1342 bus->stop_ind = ind; in npcm_i2c_master_fifo_read()
1349 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1350 npcm_i2c_set_fifo(bus, rcount, -1); in npcm_i2c_master_fifo_read()
1358 if (bus->fifo_use) in npcm_i2c_irq_master_handler_write()
1361 /* Master write operation - last byte handling */ in npcm_i2c_irq_master_handler_write()
1362 if (bus->wr_ind == bus->wr_size) { in npcm_i2c_irq_master_handler_write()
1363 if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0) in npcm_i2c_irq_master_handler_write()
1373 if (bus->rd_size == 0) { in npcm_i2c_irq_master_handler_write()
1376 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_write()
1377 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_master_handler_write()
1383 /* last write-byte written on previous int - restart */ in npcm_i2c_irq_master_handler_write()
1384 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_master_handler_write()
1389 * Receiving one byte only - stall after successful in npcm_i2c_irq_master_handler_write()
1392 * unintentionally NACK the next multi-byte read. in npcm_i2c_irq_master_handler_write()
1394 if (bus->rd_size == 1) in npcm_i2c_irq_master_handler_write()
1398 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_master_handler_write()
1400 npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1); in npcm_i2c_irq_master_handler_write()
1404 if (!bus->fifo_use || bus->wr_size == 1) { in npcm_i2c_irq_master_handler_write()
1405 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_irq_master_handler_write()
1407 wcount = bus->wr_size - bus->wr_ind; in npcm_i2c_irq_master_handler_write()
1408 npcm_i2c_set_fifo(bus, -1, wcount); in npcm_i2c_irq_master_handler_write()
1421 block_extra_bytes_size = bus->read_block_use + bus->PEC_use; in npcm_i2c_irq_master_handler_read()
1427 if (bus->rd_ind == 0) { /* first byte handling: */ in npcm_i2c_irq_master_handler_read()
1428 if (bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1432 bus->rd_size = data + block_extra_bytes_size; in npcm_i2c_irq_master_handler_read()
1433 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_irq_master_handler_read()
1436 if (bus->fifo_use) { in npcm_i2c_irq_master_handler_read()
1437 data = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1439 iowrite8(data, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1442 npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1); in npcm_i2c_irq_master_handler_read()
1449 if (bus->rd_size == block_extra_bytes_size && in npcm_i2c_irq_master_handler_read()
1450 bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1451 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_read()
1452 bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND; in npcm_i2c_irq_master_handler_read()
1453 bus->cmd_err = -EIO; in npcm_i2c_irq_master_handler_read()
1465 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_nmatch()
1467 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_nmatch()
1468 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_nmatch()
1476 if (bus->nack_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_nack()
1477 bus->nack_cnt++; in npcm_i2c_irq_handle_nack()
1479 if (bus->fifo_use) { in npcm_i2c_irq_handle_nack()
1484 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_irq_handle_nack()
1485 bus->wr_ind -= npcm_i2c_fifo_usage(bus); in npcm_i2c_irq_handle_nack()
1488 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_nack()
1492 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_irq_handle_nack()
1507 readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val, in npcm_i2c_irq_handle_nack()
1512 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_nack()
1519 npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind); in npcm_i2c_irq_handle_nack()
1525 if (bus->ber_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_ber()
1526 bus->ber_cnt++; in npcm_i2c_irq_handle_ber()
1527 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_ber()
1534 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_irq_handle_ber()
1536 bus->cmd_err = -EAGAIN; in npcm_i2c_irq_handle_ber()
1537 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_ber()
1539 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_ber()
1546 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_eob()
1547 npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind); in npcm_i2c_irq_handle_eob()
1554 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_handle_stall_after_start()
1555 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_handle_stall_after_start()
1558 } else if ((bus->rd_size == 1) && !bus->read_block_use) { in npcm_i2c_irq_handle_stall_after_start()
1560 * Receiving one byte only - set NACK after ensuring in npcm_i2c_irq_handle_stall_after_start()
1566 /* Reset stall-after-address-byte */ in npcm_i2c_irq_handle_stall_after_start()
1570 iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_stall_after_start()
1573 /* SDA status is set - TX or RX, master */
1581 if (bus->state == I2C_IDLE) { in npcm_i2c_irq_handle_sda()
1582 bus->stop_ind = I2C_WAKE_UP_IND; in npcm_i2c_irq_handle_sda()
1584 if (npcm_i2c_is_quick(bus) || bus->read_block_use) in npcm_i2c_irq_handle_sda()
1594 * Receiving one byte only - stall after successful completion in npcm_i2c_irq_handle_sda()
1597 * multi-byte read in npcm_i2c_irq_handle_sda()
1599 if (bus->wr_size == 0 && bus->rd_size == 1) in npcm_i2c_irq_handle_sda()
1607 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1612 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1614 /* re-enable */ in npcm_i2c_irq_handle_sda()
1616 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1625 if (bus->wr_size) in npcm_i2c_irq_handle_sda()
1626 npcm_i2c_set_fifo(bus, -1, bus->wr_size); in npcm_i2c_irq_handle_sda()
1628 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_handle_sda()
1630 bus->state = I2C_OPER_STARTED; in npcm_i2c_irq_handle_sda()
1632 if (npcm_i2c_is_quick(bus) || bus->wr_size) in npcm_i2c_irq_handle_sda()
1633 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_irq_handle_sda()
1635 npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0)); in npcm_i2c_irq_handle_sda()
1639 bus->operation = I2C_WRITE_OPER; in npcm_i2c_irq_handle_sda()
1642 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_handle_sda()
1651 int ret = -EIO; in npcm_i2c_int_master_handler()
1653 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_master_handler()
1673 ioread8(bus->reg + NPCM_I2CCTL1)) == 1) && in npcm_i2c_int_master_handler()
1675 ioread8(bus->reg + NPCM_I2CCST3)))) { in npcm_i2c_int_master_handler()
1686 /* SDA status is set - TX or RX, master */ in npcm_i2c_int_master_handler()
1688 (bus->fifo_use && in npcm_i2c_int_master_handler()
1703 int status = -ENOTRECOVERABLE; in npcm_i2c_recovery_tgclk()
1709 dev_dbg(bus->dev, "bus%d-0x%x recovery skipped, bus not stuck", in npcm_i2c_recovery_tgclk()
1710 bus->num, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1718 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1721 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_recovery_tgclk()
1722 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_recovery_tgclk()
1729 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1732 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1733 npcm_i2c_set_fifo(bus, -1, 0); in npcm_i2c_recovery_tgclk()
1738 iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1745 } while (!done && iter--); in npcm_i2c_recovery_tgclk()
1747 /* If SDA line is released: send start-addr-stop, to re-sync. */ in npcm_i2c_recovery_tgclk()
1750 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1768 status = -ENOTRECOVERABLE; in npcm_i2c_recovery_tgclk()
1770 if (bus->rec_fail_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1771 bus->rec_fail_cnt++; in npcm_i2c_recovery_tgclk()
1773 if (bus->rec_succ_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1774 bus->rec_succ_cnt++; in npcm_i2c_recovery_tgclk()
1783 struct i2c_bus_recovery_info *rinfo = &bus->rinfo; in npcm_i2c_recovery_init()
1785 rinfo->recover_bus = npcm_i2c_recovery_tgclk; in npcm_i2c_recovery_init()
1793 rinfo->get_scl = npcm_i2c_get_SCL; in npcm_i2c_recovery_init()
1794 rinfo->get_sda = npcm_i2c_get_SDA; in npcm_i2c_recovery_init()
1795 _adap->bus_recovery_info = rinfo; in npcm_i2c_recovery_init()
1805 * NPCM7XX i2c module timing parameters are dependent on module core clk (APB)
1823 src_clk_khz = bus->apb_clk / 1000; in npcm_i2c_init_clk()
1825 bus->bus_freq = bus_freq_hz; in npcm_i2c_init_clk()
1832 return -EDOM; in npcm_i2c_init_clk()
1849 return -EDOM; in npcm_i2c_init_clk()
1873 return -EDOM; in npcm_i2c_init_clk()
1878 /* Core clk > 40 MHz */ in npcm_i2c_init_clk()
1882 * SDA hold time: (HLDT-7) * T(CLK) >= 120 in npcm_i2c_init_clk()
1883 * HLDT = 120/T(CLK) + 7 = 120 * FREQ(CLK) + 7 in npcm_i2c_init_clk()
1894 return -EINVAL; in npcm_i2c_init_clk()
1901 return -EDOM; in npcm_i2c_init_clk()
1906 bus->reg + NPCM_I2CCTL2); in npcm_i2c_init_clk()
1910 bus->reg + NPCM_I2CCTL3); in npcm_i2c_init_clk()
1918 * k1 = 2 * SCLLT7-0 -> Low Time = k1 / 2 in npcm_i2c_init_clk()
1919 * k2 = 2 * SCLLT7-0 -> High Time = k2 / 2 in npcm_i2c_init_clk()
1921 iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT); in npcm_i2c_init_clk()
1922 iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT); in npcm_i2c_init_clk()
1924 iowrite8(dbnct, bus->reg + NPCM_I2CCTL5); in npcm_i2c_init_clk()
1927 iowrite8(hldt, bus->reg + NPCM_I2CCTL4); in npcm_i2c_init_clk()
1942 if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) || in npcm_i2c_init_module()
1944 return -EINVAL; in npcm_i2c_init_module()
1950 if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) { in npcm_i2c_init_module()
1951 bus->fifo_use = true; in npcm_i2c_init_module()
1953 val = ioread8(bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1955 iowrite8(val, bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1958 bus->fifo_use = false; in npcm_i2c_init_module()
1964 dev_err(bus->dev, "npcm_i2c_init_clk failed\n"); in npcm_i2c_init_module()
1970 bus->state = I2C_IDLE; in npcm_i2c_init_module()
1971 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1973 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1978 if ((npcm_i2c_get_SDA(&bus->adap) == 0) || (npcm_i2c_get_SCL(&bus->adap) == 0)) { in npcm_i2c_init_module()
1979 dev_err(bus->dev, "I2C%d init fail: lines are low\n", bus->num); in npcm_i2c_init_module()
1980 dev_err(bus->dev, "SDA=%d SCL=%d\n", npcm_i2c_get_SDA(&bus->adap), in npcm_i2c_init_module()
1981 npcm_i2c_get_SCL(&bus->adap)); in npcm_i2c_init_module()
1982 return -ENXIO; in npcm_i2c_init_module()
1995 bus->state = I2C_DISABLE; in __npcm_i2c_init()
1996 bus->master_or_slave = I2C_SLAVE; in __npcm_i2c_init()
1997 bus->int_time_stamp = 0; in __npcm_i2c_init()
1999 bus->slave = NULL; in __npcm_i2c_init()
2002 ret = device_property_read_u32(&pdev->dev, "clock-frequency", in __npcm_i2c_init()
2005 dev_info(&pdev->dev, "Could not read clock-frequency property"); in __npcm_i2c_init()
2011 dev_err(&pdev->dev, "npcm_i2c_init_module failed\n"); in __npcm_i2c_init()
2023 bus->master_or_slave = I2C_MASTER; in npcm_i2c_bus_irq()
2025 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_bus_irq()
2026 bus->int_time_stamp = jiffies; in npcm_i2c_bus_irq()
2031 if (bus->slave) { in npcm_i2c_bus_irq()
2032 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_bus_irq()
2048 if (bus->state != I2C_IDLE) { in npcm_i2c_master_start_xmit()
2049 bus->cmd_err = -EBUSY; in npcm_i2c_master_start_xmit()
2052 bus->dest_addr = slave_addr << 1; in npcm_i2c_master_start_xmit()
2053 bus->wr_buf = write_data; in npcm_i2c_master_start_xmit()
2054 bus->wr_size = nwrite; in npcm_i2c_master_start_xmit()
2055 bus->wr_ind = 0; in npcm_i2c_master_start_xmit()
2056 bus->rd_buf = read_data; in npcm_i2c_master_start_xmit()
2057 bus->rd_size = nread; in npcm_i2c_master_start_xmit()
2058 bus->rd_ind = 0; in npcm_i2c_master_start_xmit()
2059 bus->PEC_use = 0; in npcm_i2c_master_start_xmit()
2063 bus->PEC_use = use_PEC; in npcm_i2c_master_start_xmit()
2065 bus->read_block_use = use_read_block; in npcm_i2c_master_start_xmit()
2067 bus->operation = I2C_READ_OPER; in npcm_i2c_master_start_xmit()
2069 bus->operation = I2C_WRITE_OPER; in npcm_i2c_master_start_xmit()
2070 if (bus->fifo_use) { in npcm_i2c_master_start_xmit()
2075 i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2078 iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2081 bus->state = I2C_IDLE; in npcm_i2c_master_start_xmit()
2102 if (bus->state == I2C_DISABLE) { in npcm_i2c_master_xfer()
2103 dev_err(bus->dev, "I2C%d module is disabled", bus->num); in npcm_i2c_master_xfer()
2104 return -EINVAL; in npcm_i2c_master_xfer()
2108 slave_addr = msg0->addr; in npcm_i2c_master_xfer()
2109 if (msg0->flags & I2C_M_RD) { /* read */ in npcm_i2c_master_xfer()
2112 read_data = msg0->buf; in npcm_i2c_master_xfer()
2113 if (msg0->flags & I2C_M_RECV_LEN) { in npcm_i2c_master_xfer()
2116 if (msg0->flags & I2C_CLIENT_PEC) in npcm_i2c_master_xfer()
2119 nread = msg0->len; in npcm_i2c_master_xfer()
2122 nwrite = msg0->len; in npcm_i2c_master_xfer()
2123 write_data = msg0->buf; in npcm_i2c_master_xfer()
2128 read_data = msg1->buf; in npcm_i2c_master_xfer()
2129 if (msg1->flags & I2C_M_RECV_LEN) { in npcm_i2c_master_xfer()
2132 if (msg1->flags & I2C_CLIENT_PEC) in npcm_i2c_master_xfer()
2135 nread = msg1->len; in npcm_i2c_master_xfer()
2146 timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); in npcm_i2c_master_xfer()
2147 timeout = max_t(unsigned long, bus->adap.timeout, usecs_to_jiffies(timeout_usec)); in npcm_i2c_master_xfer()
2149 dev_err(bus->dev, "i2c%d buffer too big\n", bus->num); in npcm_i2c_master_xfer()
2150 return -EINVAL; in npcm_i2c_master_xfer()
2160 spin_lock_irqsave(&bus->lock, flags); in npcm_i2c_master_xfer()
2161 bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB; in npcm_i2c_master_xfer()
2163 if (!bus_busy && bus->slave) in npcm_i2c_master_xfer()
2164 iowrite8((bus->slave->addr & 0x7F), in npcm_i2c_master_xfer()
2165 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2167 spin_unlock_irqrestore(&bus->lock, flags); in npcm_i2c_master_xfer()
2172 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_master_xfer()
2175 return -EAGAIN; in npcm_i2c_master_xfer()
2179 bus->dest_addr = slave_addr; in npcm_i2c_master_xfer()
2180 bus->msgs = msgs; in npcm_i2c_master_xfer()
2181 bus->msgs_num = num; in npcm_i2c_master_xfer()
2182 bus->cmd_err = 0; in npcm_i2c_master_xfer()
2183 bus->read_block_use = read_block; in npcm_i2c_master_xfer()
2185 reinit_completion(&bus->cmd_complete); in npcm_i2c_master_xfer()
2192 time_left = wait_for_completion_timeout(&bus->cmd_complete, in npcm_i2c_master_xfer()
2196 if (bus->timeout_cnt < ULLONG_MAX) in npcm_i2c_master_xfer()
2197 bus->timeout_cnt++; in npcm_i2c_master_xfer()
2198 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_master_xfer()
2200 bus->cmd_err = -EIO; in npcm_i2c_master_xfer()
2201 bus->state = I2C_IDLE; in npcm_i2c_master_xfer()
2207 if (bus->cmd_err == -EAGAIN) in npcm_i2c_master_xfer()
2208 bus->cmd_err = i2c_recover_bus(adap); in npcm_i2c_master_xfer()
2215 else if (bus->cmd_err && in npcm_i2c_master_xfer()
2216 (bus->data->rxf_ctl_last_pec & ioread8(bus->reg + NPCM_I2CRXF_CTL))) in npcm_i2c_master_xfer()
2225 if (bus->slave) in npcm_i2c_master_xfer()
2226 iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN, in npcm_i2c_master_xfer()
2227 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2231 return bus->cmd_err; in npcm_i2c_master_xfer()
2268 d = debugfs_create_dir(dev_name(&pdev->dev), npcm_i2c_debugfs_dir); in npcm_i2c_init_debugfs()
2271 debugfs_create_u64("ber_cnt", 0444, d, &bus->ber_cnt); in npcm_i2c_init_debugfs()
2272 debugfs_create_u64("nack_cnt", 0444, d, &bus->nack_cnt); in npcm_i2c_init_debugfs()
2273 debugfs_create_u64("rec_succ_cnt", 0444, d, &bus->rec_succ_cnt); in npcm_i2c_init_debugfs()
2274 debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt); in npcm_i2c_init_debugfs()
2275 debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt); in npcm_i2c_init_debugfs()
2276 debugfs_create_u64("tx_complete_cnt", 0444, d, &bus->tx_complete_cnt); in npcm_i2c_init_debugfs()
2278 bus->debugfs = d; in npcm_i2c_init_debugfs()
2283 struct device_node *np = pdev->dev.of_node; in npcm_i2c_probe_bus()
2285 struct device *dev = &pdev->dev; in npcm_i2c_probe_bus()
2288 struct clk *i2c_clk; in npcm_i2c_probe_bus()
2292 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); in npcm_i2c_probe_bus()
2294 return -ENOMEM; in npcm_i2c_probe_bus()
2296 bus->dev = &pdev->dev; in npcm_i2c_probe_bus()
2298 bus->data = of_device_get_match_data(dev); in npcm_i2c_probe_bus()
2299 if (!bus->data) { in npcm_i2c_probe_bus()
2301 return -EINVAL; in npcm_i2c_probe_bus()
2304 bus->num = of_alias_get_id(pdev->dev.of_node, "i2c"); in npcm_i2c_probe_bus()
2305 /* core clk must be acquired to calculate module timing settings */ in npcm_i2c_probe_bus()
2306 i2c_clk = devm_clk_get(&pdev->dev, NULL); in npcm_i2c_probe_bus()
2309 bus->apb_clk = clk_get_rate(i2c_clk); in npcm_i2c_probe_bus()
2311 gcr_regmap = syscon_regmap_lookup_by_phandle(np, "nuvoton,sys-mgr"); in npcm_i2c_probe_bus()
2313 gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr"); in npcm_i2c_probe_bus()
2317 regmap_write(gcr_regmap, NPCM_I2CSEGCTL, bus->data->segctl_init_val); in npcm_i2c_probe_bus()
2319 bus->reg = devm_platform_ioremap_resource(pdev, 0); in npcm_i2c_probe_bus()
2320 if (IS_ERR(bus->reg)) in npcm_i2c_probe_bus()
2321 return PTR_ERR(bus->reg); in npcm_i2c_probe_bus()
2323 spin_lock_init(&bus->lock); in npcm_i2c_probe_bus()
2324 init_completion(&bus->cmd_complete); in npcm_i2c_probe_bus()
2326 adap = &bus->adap; in npcm_i2c_probe_bus()
2327 adap->owner = THIS_MODULE; in npcm_i2c_probe_bus()
2328 adap->retries = 3; in npcm_i2c_probe_bus()
2329 adap->timeout = msecs_to_jiffies(35); in npcm_i2c_probe_bus()
2330 adap->algo = &npcm_i2c_algo; in npcm_i2c_probe_bus()
2331 adap->quirks = &npcm_i2c_quirks; in npcm_i2c_probe_bus()
2332 adap->algo_data = bus; in npcm_i2c_probe_bus()
2333 adap->dev.parent = &pdev->dev; in npcm_i2c_probe_bus()
2334 adap->dev.of_node = pdev->dev.of_node; in npcm_i2c_probe_bus()
2335 adap->nr = pdev->id; in npcm_i2c_probe_bus()
2341 ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0, in npcm_i2c_probe_bus()
2342 dev_name(bus->dev), bus); in npcm_i2c_probe_bus()
2354 snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d", in npcm_i2c_probe_bus()
2355 bus->num); in npcm_i2c_probe_bus()
2356 ret = i2c_add_numbered_adapter(&bus->adap); in npcm_i2c_probe_bus()
2370 debugfs_remove_recursive(bus->debugfs); in npcm_i2c_remove_bus()
2371 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2373 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2374 i2c_del_adapter(&bus->adap); in npcm_i2c_remove_bus()
2379 { .compatible = "nuvoton,npcm750-i2c", .data = &npxm7xx_i2c_data },
2380 { .compatible = "nuvoton,npcm845-i2c", .data = &npxm8xx_i2c_data },
2389 .name = "nuvoton-i2c",