1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Synopsys DesignWare I2C adapter driver (slave only).
4  *
5  * Based on the Synopsys DesignWare I2C adapter driver (master).
6  *
7  * Copyright (C) 2016 Synopsys Inc.
8  */
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 
19 #include "i2c-designware-core.h"
20 
21 static void i2c_dw_configure_fifo_slave(struct dw_i2c_dev *dev)
22 {
23 	/* Configure Tx/Rx FIFO threshold levels. */
24 	regmap_write(dev->map, DW_IC_TX_TL, 0);
25 	regmap_write(dev->map, DW_IC_RX_TL, 0);
26 
27 	/* Configure the I2C slave. */
28 	regmap_write(dev->map, DW_IC_CON, dev->slave_cfg);
29 	regmap_write(dev->map, DW_IC_INTR_MASK, DW_IC_INTR_SLAVE_MASK);
30 }
31 
32 /**
33  * i2c_dw_init_slave() - Initialize the designware i2c slave hardware
34  * @dev: device private data
35  *
36  * This function configures and enables the I2C in slave mode.
37  * This function is called during I2C init function, and in case of timeout at
38  * run time.
39  */
40 static int i2c_dw_init_slave(struct dw_i2c_dev *dev)
41 {
42 	int ret;
43 
44 	ret = i2c_dw_acquire_lock(dev);
45 	if (ret)
46 		return ret;
47 
48 	/* Disable the adapter. */
49 	__i2c_dw_disable(dev);
50 
51 	/* Write SDA hold time if supported */
52 	if (dev->sda_hold_time)
53 		regmap_write(dev->map, DW_IC_SDA_HOLD, dev->sda_hold_time);
54 
55 	i2c_dw_configure_fifo_slave(dev);
56 	i2c_dw_release_lock(dev);
57 
58 	return 0;
59 }
60 
61 static int i2c_dw_reg_slave(struct i2c_client *slave)
62 {
63 	struct dw_i2c_dev *dev = i2c_get_adapdata(slave->adapter);
64 
65 	if (dev->slave)
66 		return -EBUSY;
67 	if (slave->flags & I2C_CLIENT_TEN)
68 		return -EAFNOSUPPORT;
69 	pm_runtime_get_sync(dev->dev);
70 
71 	/*
72 	 * Set slave address in the IC_SAR register,
73 	 * the address to which the DW_apb_i2c responds.
74 	 */
75 	__i2c_dw_disable_nowait(dev);
76 	regmap_write(dev->map, DW_IC_SAR, slave->addr);
77 	dev->slave = slave;
78 
79 	__i2c_dw_enable(dev);
80 
81 	dev->cmd_err = 0;
82 	dev->msg_write_idx = 0;
83 	dev->msg_read_idx = 0;
84 	dev->msg_err = 0;
85 	dev->status = STATUS_IDLE;
86 	dev->abort_source = 0;
87 	dev->rx_outstanding = 0;
88 
89 	return 0;
90 }
91 
92 static int i2c_dw_unreg_slave(struct i2c_client *slave)
93 {
94 	struct dw_i2c_dev *dev = i2c_get_adapdata(slave->adapter);
95 
96 	dev->disable_int(dev);
97 	dev->disable(dev);
98 	synchronize_irq(dev->irq);
99 	dev->slave = NULL;
100 	pm_runtime_put(dev->dev);
101 
102 	return 0;
103 }
104 
105 static u32 i2c_dw_read_clear_intrbits_slave(struct dw_i2c_dev *dev)
106 {
107 	u32 stat, dummy;
108 
109 	/*
110 	 * The IC_INTR_STAT register just indicates "enabled" interrupts.
111 	 * The unmasked raw version of interrupt status bits is available
112 	 * in the IC_RAW_INTR_STAT register.
113 	 *
114 	 * That is,
115 	 *   stat = readl(IC_INTR_STAT);
116 	 * equals to,
117 	 *   stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
118 	 *
119 	 * The raw version might be useful for debugging purposes.
120 	 */
121 	regmap_read(dev->map, DW_IC_INTR_STAT, &stat);
122 
123 	/*
124 	 * Do not use the IC_CLR_INTR register to clear interrupts, or
125 	 * you'll miss some interrupts, triggered during the period from
126 	 * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
127 	 *
128 	 * Instead, use the separately-prepared IC_CLR_* registers.
129 	 */
130 	if (stat & DW_IC_INTR_TX_ABRT)
131 		regmap_read(dev->map, DW_IC_CLR_TX_ABRT, &dummy);
132 	if (stat & DW_IC_INTR_RX_UNDER)
133 		regmap_read(dev->map, DW_IC_CLR_RX_UNDER, &dummy);
134 	if (stat & DW_IC_INTR_RX_OVER)
135 		regmap_read(dev->map, DW_IC_CLR_RX_OVER, &dummy);
136 	if (stat & DW_IC_INTR_TX_OVER)
137 		regmap_read(dev->map, DW_IC_CLR_TX_OVER, &dummy);
138 	if (stat & DW_IC_INTR_RX_DONE)
139 		regmap_read(dev->map, DW_IC_CLR_RX_DONE, &dummy);
140 	if (stat & DW_IC_INTR_ACTIVITY)
141 		regmap_read(dev->map, DW_IC_CLR_ACTIVITY, &dummy);
142 	if (stat & DW_IC_INTR_STOP_DET)
143 		regmap_read(dev->map, DW_IC_CLR_STOP_DET, &dummy);
144 	if (stat & DW_IC_INTR_START_DET)
145 		regmap_read(dev->map, DW_IC_CLR_START_DET, &dummy);
146 	if (stat & DW_IC_INTR_GEN_CALL)
147 		regmap_read(dev->map, DW_IC_CLR_GEN_CALL, &dummy);
148 
149 	return stat;
150 }
151 
152 /*
153  * Interrupt service routine. This gets called whenever an I2C slave interrupt
154  * occurs.
155  */
156 
157 static int i2c_dw_irq_handler_slave(struct dw_i2c_dev *dev)
158 {
159 	u32 raw_stat, stat, enabled, tmp;
160 	u8 val = 0, slave_activity;
161 
162 	regmap_read(dev->map, DW_IC_INTR_STAT, &stat);
163 	regmap_read(dev->map, DW_IC_ENABLE, &enabled);
164 	regmap_read(dev->map, DW_IC_RAW_INTR_STAT, &raw_stat);
165 	regmap_read(dev->map, DW_IC_STATUS, &tmp);
166 	slave_activity = ((tmp & DW_IC_STATUS_SLAVE_ACTIVITY) >> 6);
167 
168 	if (!enabled || !(raw_stat & ~DW_IC_INTR_ACTIVITY) || !dev->slave)
169 		return 0;
170 
171 	dev_dbg(dev->dev,
172 		"%#x STATUS SLAVE_ACTIVITY=%#x : RAW_INTR_STAT=%#x : INTR_STAT=%#x\n",
173 		enabled, slave_activity, raw_stat, stat);
174 
175 	if ((stat & DW_IC_INTR_RX_FULL) && (stat & DW_IC_INTR_STOP_DET))
176 		i2c_slave_event(dev->slave, I2C_SLAVE_WRITE_REQUESTED, &val);
177 
178 	if (stat & DW_IC_INTR_RD_REQ) {
179 		if (slave_activity) {
180 			if (stat & DW_IC_INTR_RX_FULL) {
181 				regmap_read(dev->map, DW_IC_DATA_CMD, &tmp);
182 				val = tmp;
183 
184 				if (!i2c_slave_event(dev->slave,
185 						     I2C_SLAVE_WRITE_RECEIVED,
186 						     &val)) {
187 					dev_vdbg(dev->dev, "Byte %X acked!",
188 						 val);
189 				}
190 				regmap_read(dev->map, DW_IC_CLR_RD_REQ, &tmp);
191 				stat = i2c_dw_read_clear_intrbits_slave(dev);
192 			} else {
193 				regmap_read(dev->map, DW_IC_CLR_RD_REQ, &tmp);
194 				regmap_read(dev->map, DW_IC_CLR_RX_UNDER, &tmp);
195 				stat = i2c_dw_read_clear_intrbits_slave(dev);
196 			}
197 			if (!i2c_slave_event(dev->slave,
198 					     I2C_SLAVE_READ_REQUESTED,
199 					     &val))
200 				regmap_write(dev->map, DW_IC_DATA_CMD, val);
201 		}
202 	}
203 
204 	if (stat & DW_IC_INTR_RX_DONE) {
205 		if (!i2c_slave_event(dev->slave, I2C_SLAVE_READ_PROCESSED,
206 				     &val))
207 			regmap_read(dev->map, DW_IC_CLR_RX_DONE, &tmp);
208 
209 		i2c_slave_event(dev->slave, I2C_SLAVE_STOP, &val);
210 		stat = i2c_dw_read_clear_intrbits_slave(dev);
211 		return 1;
212 	}
213 
214 	if (stat & DW_IC_INTR_RX_FULL) {
215 		regmap_read(dev->map, DW_IC_DATA_CMD, &tmp);
216 		val = tmp;
217 		if (!i2c_slave_event(dev->slave, I2C_SLAVE_WRITE_RECEIVED,
218 				     &val))
219 			dev_vdbg(dev->dev, "Byte %X acked!", val);
220 	} else {
221 		i2c_slave_event(dev->slave, I2C_SLAVE_STOP, &val);
222 		stat = i2c_dw_read_clear_intrbits_slave(dev);
223 	}
224 
225 	return 1;
226 }
227 
228 static irqreturn_t i2c_dw_isr_slave(int this_irq, void *dev_id)
229 {
230 	struct dw_i2c_dev *dev = dev_id;
231 	int ret;
232 
233 	i2c_dw_read_clear_intrbits_slave(dev);
234 	ret = i2c_dw_irq_handler_slave(dev);
235 	if (ret > 0)
236 		complete(&dev->cmd_complete);
237 
238 	return IRQ_RETVAL(ret);
239 }
240 
241 static const struct i2c_algorithm i2c_dw_algo = {
242 	.functionality = i2c_dw_func,
243 	.reg_slave = i2c_dw_reg_slave,
244 	.unreg_slave = i2c_dw_unreg_slave,
245 };
246 
247 void i2c_dw_configure_slave(struct dw_i2c_dev *dev)
248 {
249 	dev->functionality = I2C_FUNC_SLAVE | DW_IC_DEFAULT_FUNCTIONALITY;
250 
251 	dev->slave_cfg = DW_IC_CON_RX_FIFO_FULL_HLD_CTRL |
252 			 DW_IC_CON_RESTART_EN | DW_IC_CON_STOP_DET_IFADDRESSED;
253 
254 	dev->mode = DW_IC_SLAVE;
255 }
256 EXPORT_SYMBOL_GPL(i2c_dw_configure_slave);
257 
258 int i2c_dw_probe_slave(struct dw_i2c_dev *dev)
259 {
260 	struct i2c_adapter *adap = &dev->adapter;
261 	int ret;
262 
263 	init_completion(&dev->cmd_complete);
264 
265 	dev->init = i2c_dw_init_slave;
266 	dev->disable = i2c_dw_disable;
267 	dev->disable_int = i2c_dw_disable_int;
268 
269 	ret = i2c_dw_init_regmap(dev);
270 	if (ret)
271 		return ret;
272 
273 	ret = i2c_dw_set_sda_hold(dev);
274 	if (ret)
275 		return ret;
276 
277 	ret = i2c_dw_set_fifo_size(dev);
278 	if (ret)
279 		return ret;
280 
281 	ret = dev->init(dev);
282 	if (ret)
283 		return ret;
284 
285 	snprintf(adap->name, sizeof(adap->name),
286 		 "Synopsys DesignWare I2C Slave adapter");
287 	adap->retries = 3;
288 	adap->algo = &i2c_dw_algo;
289 	adap->dev.parent = dev->dev;
290 	i2c_set_adapdata(adap, dev);
291 
292 	ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr_slave,
293 			       IRQF_SHARED, dev_name(dev->dev), dev);
294 	if (ret) {
295 		dev_err(dev->dev, "failure requesting irq %i: %d\n",
296 			dev->irq, ret);
297 		return ret;
298 	}
299 
300 	ret = i2c_add_numbered_adapter(adap);
301 	if (ret)
302 		dev_err(dev->dev, "failure adding adapter: %d\n", ret);
303 
304 	return ret;
305 }
306 EXPORT_SYMBOL_GPL(i2c_dw_probe_slave);
307 
308 MODULE_AUTHOR("Luis Oliveira <lolivei@synopsys.com>");
309 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus slave adapter");
310 MODULE_LICENSE("GPL v2");
311