4343#define LPI2C_MTDR 0x60 /* i2c master TX data register */
4444#define LPI2C_MRDR 0x70 /* i2c master RX data register */
4545
46+ #define LPI2C_SCR 0x110 /* i2c target control register */
47+ #define LPI2C_SSR 0x114 /* i2c target status register */
48+ #define LPI2C_SIER 0x118 /* i2c target interrupt enable */
49+ #define LPI2C_SDER 0x11C /* i2c target DMA enable */
50+ #define LPI2C_SCFGR0 0x120 /* i2c target configuration */
51+ #define LPI2C_SCFGR1 0x124 /* i2c target configuration */
52+ #define LPI2C_SCFGR2 0x128 /* i2c target configuration */
53+ #define LPI2C_SAMR 0x140 /* i2c target address match */
54+ #define LPI2C_SASR 0x150 /* i2c target address status */
55+ #define LPI2C_STAR 0x154 /* i2c target transmit ACK */
56+ #define LPI2C_STDR 0x160 /* i2c target transmit data */
57+ #define LPI2C_SRDR 0x170 /* i2c target receive data */
58+ #define LPI2C_SRDROR 0x178 /* i2c target receive data read only */
59+
4660/* i2c command */
4761#define TRAN_DATA 0X00
4862#define RECV_DATA 0X01
7690#define MDER_TDDE BIT(0)
7791#define MDER_RDDE BIT(1)
7892
93+ #define SCR_SEN BIT(0)
94+ #define SCR_RST BIT(1)
95+ #define SCR_FILTEN BIT(4)
96+ #define SCR_RTF BIT(8)
97+ #define SCR_RRF BIT(9)
98+ #define SSR_TDF BIT(0)
99+ #define SSR_RDF BIT(1)
100+ #define SSR_AVF BIT(2)
101+ #define SSR_TAF BIT(3)
102+ #define SSR_RSF BIT(8)
103+ #define SSR_SDF BIT(9)
104+ #define SSR_BEF BIT(10)
105+ #define SSR_FEF BIT(11)
106+ #define SSR_SBF BIT(24)
107+ #define SSR_BBF BIT(25)
108+ #define SSR_CLEAR_BITS (SSR_RSF | SSR_SDF | SSR_BEF | SSR_FEF)
109+ #define SIER_TDIE BIT(0)
110+ #define SIER_RDIE BIT(1)
111+ #define SIER_AVIE BIT(2)
112+ #define SIER_TAIE BIT(3)
113+ #define SIER_RSIE BIT(8)
114+ #define SIER_SDIE BIT(9)
115+ #define SIER_BEIE BIT(10)
116+ #define SIER_FEIE BIT(11)
117+ #define SIER_AM0F BIT(12)
118+ #define SCFGR1_RXSTALL BIT(1)
119+ #define SCFGR1_TXDSTALL BIT(2)
120+ #define SCFGR2_FILTSDA_SHIFT 24
121+ #define SCFGR2_FILTSCL_SHIFT 16
122+ #define SCFGR2_CLKHOLD (x ) (x)
123+ #define SCFGR2_FILTSDA (x ) ((x) << SCFGR2_FILTSDA_SHIFT)
124+ #define SCFGR2_FILTSCL (x ) ((x) << SCFGR2_FILTSCL_SHIFT)
125+ #define SASR_READ_REQ 0x1
126+ #define SLAVE_INT_FLAG (SIER_TDIE | SIER_RDIE | SIER_AVIE | \
127+ SIER_SDIE | SIER_BEIE)
128+
79129#define I2C_CLK_RATIO 2
80130#define CHUNK_DATA 256
81131
@@ -134,6 +184,7 @@ struct lpi2c_imx_struct {
134184 struct i2c_bus_recovery_info rinfo ;
135185 bool can_use_dma ;
136186 struct lpi2c_imx_dma * dma ;
187+ struct i2c_client * target ;
137188};
138189
139190static void lpi2c_imx_intctrl (struct lpi2c_imx_struct * lpi2c_imx ,
@@ -957,9 +1008,56 @@ static int lpi2c_imx_xfer(struct i2c_adapter *adapter,
9571008 return (result < 0 ) ? result : num ;
9581009}
9591010
960- static irqreturn_t lpi2c_imx_isr (int irq , void * dev_id )
1011+ static irqreturn_t lpi2c_imx_target_isr (struct lpi2c_imx_struct * lpi2c_imx ,
1012+ u32 ssr , u32 sier_filter )
1013+ {
1014+ u8 value ;
1015+ u32 sasr ;
1016+
1017+ /* Arbitration lost */
1018+ if (sier_filter & SSR_BEF ) {
1019+ writel (0 , lpi2c_imx -> base + LPI2C_SIER );
1020+ return IRQ_HANDLED ;
1021+ }
1022+
1023+ /* Address detected */
1024+ if (sier_filter & SSR_AVF ) {
1025+ sasr = readl (lpi2c_imx -> base + LPI2C_SASR );
1026+ if (SASR_READ_REQ & sasr ) {
1027+ /* Read request */
1028+ i2c_slave_event (lpi2c_imx -> target , I2C_SLAVE_READ_REQUESTED , & value );
1029+ writel (value , lpi2c_imx -> base + LPI2C_STDR );
1030+ goto ret ;
1031+ } else {
1032+ /* Write request */
1033+ i2c_slave_event (lpi2c_imx -> target , I2C_SLAVE_WRITE_REQUESTED , & value );
1034+ }
1035+ }
1036+
1037+ if (sier_filter & SSR_SDF )
1038+ /* STOP */
1039+ i2c_slave_event (lpi2c_imx -> target , I2C_SLAVE_STOP , & value );
1040+
1041+ if (sier_filter & SSR_TDF ) {
1042+ /* Target send data */
1043+ i2c_slave_event (lpi2c_imx -> target , I2C_SLAVE_READ_PROCESSED , & value );
1044+ writel (value , lpi2c_imx -> base + LPI2C_STDR );
1045+ }
1046+
1047+ if (sier_filter & SSR_RDF ) {
1048+ /* Target receive data */
1049+ value = readl (lpi2c_imx -> base + LPI2C_SRDR );
1050+ i2c_slave_event (lpi2c_imx -> target , I2C_SLAVE_WRITE_RECEIVED , & value );
1051+ }
1052+
1053+ ret :
1054+ /* Clear SSR */
1055+ writel (ssr & SSR_CLEAR_BITS , lpi2c_imx -> base + LPI2C_SSR );
1056+ return IRQ_HANDLED ;
1057+ }
1058+
1059+ static irqreturn_t lpi2c_imx_master_isr (struct lpi2c_imx_struct * lpi2c_imx )
9611060{
962- struct lpi2c_imx_struct * lpi2c_imx = dev_id ;
9631061 unsigned int enabled ;
9641062 unsigned int temp ;
9651063
@@ -979,6 +1077,124 @@ static irqreturn_t lpi2c_imx_isr(int irq, void *dev_id)
9791077 return IRQ_HANDLED ;
9801078}
9811079
1080+ static irqreturn_t lpi2c_imx_isr (int irq , void * dev_id )
1081+ {
1082+ struct lpi2c_imx_struct * lpi2c_imx = dev_id ;
1083+
1084+ if (lpi2c_imx -> target ) {
1085+ u32 scr = readl (lpi2c_imx -> base + LPI2C_SCR );
1086+ u32 ssr = readl (lpi2c_imx -> base + LPI2C_SSR );
1087+ u32 sier_filter = ssr & readl (lpi2c_imx -> base + LPI2C_SIER );
1088+
1089+ /*
1090+ * The target is enabled and an interrupt has been triggered.
1091+ * Enter the target's irq handler.
1092+ */
1093+ if ((scr & SCR_SEN ) && sier_filter )
1094+ return lpi2c_imx_target_isr (lpi2c_imx , ssr , sier_filter );
1095+ }
1096+
1097+ /*
1098+ * Otherwise the interrupt has been triggered by the master.
1099+ * Enter the master's irq handler.
1100+ */
1101+ return lpi2c_imx_master_isr (lpi2c_imx );
1102+ }
1103+
1104+ static void lpi2c_imx_target_init (struct lpi2c_imx_struct * lpi2c_imx )
1105+ {
1106+ u32 temp ;
1107+
1108+ /* reset target module */
1109+ writel (SCR_RST , lpi2c_imx -> base + LPI2C_SCR );
1110+ writel (0 , lpi2c_imx -> base + LPI2C_SCR );
1111+
1112+ /* Set target address */
1113+ writel ((lpi2c_imx -> target -> addr << 1 ), lpi2c_imx -> base + LPI2C_SAMR );
1114+
1115+ writel (SCFGR1_RXSTALL | SCFGR1_TXDSTALL , lpi2c_imx -> base + LPI2C_SCFGR1 );
1116+
1117+ /*
1118+ * set SCFGR2: FILTSDA, FILTSCL and CLKHOLD
1119+ *
1120+ * FILTSCL/FILTSDA can eliminate signal skew. It should generally be
1121+ * set to the same value and should be set >= 50ns.
1122+ *
1123+ * CLKHOLD is only used when clock stretching is enabled, but it will
1124+ * extend the clock stretching to ensure there is an additional delay
1125+ * between the target driving SDA and the target releasing the SCL pin.
1126+ *
1127+ * CLKHOLD setting is crucial for lpi2c target. When master read data
1128+ * from target, if there is a delay caused by cpu idle, excessive load,
1129+ * or other delays between two bytes in one message transmission, it
1130+ * will cause a short interval time between the driving SDA signal and
1131+ * releasing SCL signal. The lpi2c master will mistakenly think it is a stop
1132+ * signal resulting in an arbitration failure. This issue can be avoided
1133+ * by setting CLKHOLD.
1134+ *
1135+ * In order to ensure lpi2c function normally when the lpi2c speed is as
1136+ * low as 100kHz, CLKHOLD should be set to 3 and it is also compatible with
1137+ * higher clock frequency like 400kHz and 1MHz.
1138+ */
1139+ temp = SCFGR2_FILTSDA (2 ) | SCFGR2_FILTSCL (2 ) | SCFGR2_CLKHOLD (3 );
1140+ writel (temp , lpi2c_imx -> base + LPI2C_SCFGR2 );
1141+
1142+ /*
1143+ * Enable module:
1144+ * SCR_FILTEN can enable digital filter and output delay counter for LPI2C
1145+ * target mode. So SCR_FILTEN need be asserted when enable SDA/SCL FILTER
1146+ * and CLKHOLD.
1147+ */
1148+ writel (SCR_SEN | SCR_FILTEN , lpi2c_imx -> base + LPI2C_SCR );
1149+
1150+ /* Enable interrupt from i2c module */
1151+ writel (SLAVE_INT_FLAG , lpi2c_imx -> base + LPI2C_SIER );
1152+ }
1153+
1154+ static int lpi2c_imx_register_target (struct i2c_client * client )
1155+ {
1156+ struct lpi2c_imx_struct * lpi2c_imx = i2c_get_adapdata (client -> adapter );
1157+ int ret ;
1158+
1159+ if (lpi2c_imx -> target )
1160+ return - EBUSY ;
1161+
1162+ lpi2c_imx -> target = client ;
1163+
1164+ ret = pm_runtime_resume_and_get (lpi2c_imx -> adapter .dev .parent );
1165+ if (ret < 0 ) {
1166+ dev_err (& lpi2c_imx -> adapter .dev , "failed to resume i2c controller" );
1167+ return ret ;
1168+ }
1169+
1170+ lpi2c_imx_target_init (lpi2c_imx );
1171+
1172+ return 0 ;
1173+ }
1174+
1175+ static int lpi2c_imx_unregister_target (struct i2c_client * client )
1176+ {
1177+ struct lpi2c_imx_struct * lpi2c_imx = i2c_get_adapdata (client -> adapter );
1178+ int ret ;
1179+
1180+ if (!lpi2c_imx -> target )
1181+ return - EINVAL ;
1182+
1183+ /* Reset target address. */
1184+ writel (0 , lpi2c_imx -> base + LPI2C_SAMR );
1185+
1186+ writel (SCR_RST , lpi2c_imx -> base + LPI2C_SCR );
1187+ writel (0 , lpi2c_imx -> base + LPI2C_SCR );
1188+
1189+ lpi2c_imx -> target = NULL ;
1190+
1191+ ret = pm_runtime_put_sync (lpi2c_imx -> adapter .dev .parent );
1192+ if (ret < 0 )
1193+ dev_err (& lpi2c_imx -> adapter .dev , "failed to suspend i2c controller" );
1194+
1195+ return ret ;
1196+ }
1197+
9821198static int lpi2c_imx_init_recovery_info (struct lpi2c_imx_struct * lpi2c_imx ,
9831199 struct platform_device * pdev )
9841200{
@@ -1054,6 +1270,8 @@ static u32 lpi2c_imx_func(struct i2c_adapter *adapter)
10541270static const struct i2c_algorithm lpi2c_imx_algo = {
10551271 .master_xfer = lpi2c_imx_xfer ,
10561272 .functionality = lpi2c_imx_func ,
1273+ .reg_target = lpi2c_imx_register_target ,
1274+ .unreg_target = lpi2c_imx_unregister_target ,
10571275};
10581276
10591277static const struct of_device_id lpi2c_imx_of_match [] = {
@@ -1204,9 +1422,34 @@ static int __maybe_unused lpi2c_runtime_resume(struct device *dev)
12041422 return 0 ;
12051423}
12061424
1425+ static int __maybe_unused lpi2c_suspend_noirq (struct device * dev )
1426+ {
1427+ return pm_runtime_force_suspend (dev );
1428+ }
1429+
1430+ static int __maybe_unused lpi2c_resume_noirq (struct device * dev )
1431+ {
1432+ struct lpi2c_imx_struct * lpi2c_imx = dev_get_drvdata (dev );
1433+ int ret ;
1434+
1435+ ret = pm_runtime_force_resume (dev );
1436+ if (ret )
1437+ return ret ;
1438+
1439+ /*
1440+ * If the I2C module powers down during system suspend,
1441+ * the register values will be lost. Therefore, reinitialize
1442+ * the target when the system resumes.
1443+ */
1444+ if (lpi2c_imx -> target )
1445+ lpi2c_imx_target_init (lpi2c_imx );
1446+
1447+ return 0 ;
1448+ }
1449+
12071450static const struct dev_pm_ops lpi2c_pm_ops = {
1208- SET_NOIRQ_SYSTEM_SLEEP_PM_OPS (pm_runtime_force_suspend ,
1209- pm_runtime_force_resume )
1451+ SET_NOIRQ_SYSTEM_SLEEP_PM_OPS (lpi2c_suspend_noirq ,
1452+ lpi2c_resume_noirq )
12101453 SET_RUNTIME_PM_OPS (lpi2c_runtime_suspend ,
12111454 lpi2c_runtime_resume , NULL )
12121455};
0 commit comments