xref: /linux/drivers/i2c/busses/i2c-uniphier.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/i2c.h>
8 #include <linux/interrupt.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 
13 #define UNIPHIER_I2C_DTRM	0x00	/* TX register */
14 #define     UNIPHIER_I2C_DTRM_IRQEN	BIT(11)	/* enable interrupt */
15 #define     UNIPHIER_I2C_DTRM_STA	BIT(10)	/* start condition */
16 #define     UNIPHIER_I2C_DTRM_STO	BIT(9)	/* stop condition */
17 #define     UNIPHIER_I2C_DTRM_NACK	BIT(8)	/* do not return ACK */
18 #define     UNIPHIER_I2C_DTRM_RD	BIT(0)	/* read transaction */
19 #define UNIPHIER_I2C_DREC	0x04	/* RX register */
20 #define     UNIPHIER_I2C_DREC_MST	BIT(14)	/* 1 = master, 0 = slave */
21 #define     UNIPHIER_I2C_DREC_TX	BIT(13)	/* 1 = transmit, 0 = receive */
22 #define     UNIPHIER_I2C_DREC_STS	BIT(12)	/* stop condition detected */
23 #define     UNIPHIER_I2C_DREC_LRB	BIT(11)	/* no ACK */
24 #define     UNIPHIER_I2C_DREC_LAB	BIT(9)	/* arbitration lost */
25 #define     UNIPHIER_I2C_DREC_BBN	BIT(8)	/* bus not busy */
26 #define UNIPHIER_I2C_MYAD	0x08	/* slave address */
27 #define UNIPHIER_I2C_CLK	0x0c	/* clock frequency control */
28 #define UNIPHIER_I2C_BRST	0x10	/* bus reset */
29 #define     UNIPHIER_I2C_BRST_FOEN	BIT(1)	/* normal operation */
30 #define     UNIPHIER_I2C_BRST_RSCL	BIT(0)	/* release SCL */
31 #define UNIPHIER_I2C_HOLD	0x14	/* hold time control */
32 #define UNIPHIER_I2C_BSTS	0x18	/* bus status monitor */
33 #define     UNIPHIER_I2C_BSTS_SDA	BIT(1)	/* readback of SDA line */
34 #define     UNIPHIER_I2C_BSTS_SCL	BIT(0)	/* readback of SCL line */
35 #define UNIPHIER_I2C_NOISE	0x1c	/* noise filter control */
36 #define UNIPHIER_I2C_SETUP	0x20	/* setup time control */
37 
38 struct uniphier_i2c_priv {
39 	struct completion comp;
40 	struct i2c_adapter adap;
41 	void __iomem *membase;
42 	struct clk *clk;
43 	unsigned int busy_cnt;
44 	unsigned int clk_cycle;
45 };
46 
47 static irqreturn_t uniphier_i2c_interrupt(int irq, void *dev_id)
48 {
49 	struct uniphier_i2c_priv *priv = dev_id;
50 
51 	/*
52 	 * This hardware uses edge triggered interrupt.  Do not touch the
53 	 * hardware registers in this handler to make sure to catch the next
54 	 * interrupt edge.  Just send a complete signal and return.
55 	 */
56 	complete(&priv->comp);
57 
58 	return IRQ_HANDLED;
59 }
60 
61 static int uniphier_i2c_xfer_byte(struct i2c_adapter *adap, u32 txdata,
62 				  u32 *rxdatap)
63 {
64 	struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
65 	unsigned long time_left;
66 	u32 rxdata;
67 
68 	reinit_completion(&priv->comp);
69 
70 	txdata |= UNIPHIER_I2C_DTRM_IRQEN;
71 	writel(txdata, priv->membase + UNIPHIER_I2C_DTRM);
72 
73 	time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
74 	if (unlikely(!time_left))
75 		return -ETIMEDOUT;
76 
77 	rxdata = readl(priv->membase + UNIPHIER_I2C_DREC);
78 	if (rxdatap)
79 		*rxdatap = rxdata;
80 
81 	return 0;
82 }
83 
84 static int uniphier_i2c_send_byte(struct i2c_adapter *adap, u32 txdata)
85 {
86 	u32 rxdata;
87 	int ret;
88 
89 	ret = uniphier_i2c_xfer_byte(adap, txdata, &rxdata);
90 	if (ret)
91 		return ret;
92 
93 	if (unlikely(rxdata & UNIPHIER_I2C_DREC_LAB))
94 		return -EAGAIN;
95 
96 	if (unlikely(rxdata & UNIPHIER_I2C_DREC_LRB))
97 		return -ENXIO;
98 
99 	return 0;
100 }
101 
102 static int uniphier_i2c_tx(struct i2c_adapter *adap, u16 addr, u16 len,
103 			   const u8 *buf)
104 {
105 	int ret;
106 
107 	ret = uniphier_i2c_send_byte(adap, addr << 1 |
108 				     UNIPHIER_I2C_DTRM_STA |
109 				     UNIPHIER_I2C_DTRM_NACK);
110 	if (ret)
111 		return ret;
112 
113 	while (len--) {
114 		ret = uniphier_i2c_send_byte(adap,
115 					     UNIPHIER_I2C_DTRM_NACK | *buf++);
116 		if (ret)
117 			return ret;
118 	}
119 
120 	return 0;
121 }
122 
123 static int uniphier_i2c_rx(struct i2c_adapter *adap, u16 addr, u16 len,
124 			   u8 *buf)
125 {
126 	int ret;
127 
128 	ret = uniphier_i2c_send_byte(adap, addr << 1 |
129 				     UNIPHIER_I2C_DTRM_STA |
130 				     UNIPHIER_I2C_DTRM_NACK |
131 				     UNIPHIER_I2C_DTRM_RD);
132 	if (ret)
133 		return ret;
134 
135 	while (len--) {
136 		u32 rxdata;
137 
138 		ret = uniphier_i2c_xfer_byte(adap,
139 					     len ? 0 : UNIPHIER_I2C_DTRM_NACK,
140 					     &rxdata);
141 		if (ret)
142 			return ret;
143 		*buf++ = rxdata;
144 	}
145 
146 	return 0;
147 }
148 
149 static int uniphier_i2c_stop(struct i2c_adapter *adap)
150 {
151 	return uniphier_i2c_send_byte(adap, UNIPHIER_I2C_DTRM_STO |
152 				      UNIPHIER_I2C_DTRM_NACK);
153 }
154 
155 static int uniphier_i2c_master_xfer_one(struct i2c_adapter *adap,
156 					struct i2c_msg *msg, bool stop)
157 {
158 	bool is_read = msg->flags & I2C_M_RD;
159 	bool recovery = false;
160 	int ret;
161 
162 	if (is_read)
163 		ret = uniphier_i2c_rx(adap, msg->addr, msg->len, msg->buf);
164 	else
165 		ret = uniphier_i2c_tx(adap, msg->addr, msg->len, msg->buf);
166 
167 	if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */
168 		return ret;
169 
170 	if (ret == -ETIMEDOUT) {
171 		/* This error is fatal.  Needs recovery. */
172 		stop = false;
173 		recovery = true;
174 	}
175 
176 	if (stop) {
177 		int ret2 = uniphier_i2c_stop(adap);
178 
179 		if (ret2) {
180 			/* Failed to issue STOP.  The bus needs recovery. */
181 			recovery = true;
182 			ret = ret ?: ret2;
183 		}
184 	}
185 
186 	if (recovery)
187 		i2c_recover_bus(adap);
188 
189 	return ret;
190 }
191 
192 static int uniphier_i2c_check_bus_busy(struct i2c_adapter *adap)
193 {
194 	struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
195 
196 	if (!(readl(priv->membase + UNIPHIER_I2C_DREC) &
197 						UNIPHIER_I2C_DREC_BBN)) {
198 		if (priv->busy_cnt++ > 3) {
199 			/*
200 			 * If bus busy continues too long, it is probably
201 			 * in a wrong state.  Try bus recovery.
202 			 */
203 			i2c_recover_bus(adap);
204 			priv->busy_cnt = 0;
205 		}
206 
207 		return -EAGAIN;
208 	}
209 
210 	priv->busy_cnt = 0;
211 	return 0;
212 }
213 
214 static int uniphier_i2c_master_xfer(struct i2c_adapter *adap,
215 				    struct i2c_msg *msgs, int num)
216 {
217 	struct i2c_msg *msg, *emsg = msgs + num;
218 	int ret;
219 
220 	ret = uniphier_i2c_check_bus_busy(adap);
221 	if (ret)
222 		return ret;
223 
224 	for (msg = msgs; msg < emsg; msg++) {
225 		/* Emit STOP if it is the last message or I2C_M_STOP is set. */
226 		bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
227 
228 		ret = uniphier_i2c_master_xfer_one(adap, msg, stop);
229 		if (ret)
230 			return ret;
231 	}
232 
233 	return num;
234 }
235 
236 static u32 uniphier_i2c_functionality(struct i2c_adapter *adap)
237 {
238 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
239 }
240 
241 static const struct i2c_algorithm uniphier_i2c_algo = {
242 	.master_xfer = uniphier_i2c_master_xfer,
243 	.functionality = uniphier_i2c_functionality,
244 };
245 
246 static void uniphier_i2c_reset(struct uniphier_i2c_priv *priv, bool reset_on)
247 {
248 	u32 val = UNIPHIER_I2C_BRST_RSCL;
249 
250 	val |= reset_on ? 0 : UNIPHIER_I2C_BRST_FOEN;
251 	writel(val, priv->membase + UNIPHIER_I2C_BRST);
252 }
253 
254 static int uniphier_i2c_get_scl(struct i2c_adapter *adap)
255 {
256 	struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
257 
258 	return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
259 							UNIPHIER_I2C_BSTS_SCL);
260 }
261 
262 static void uniphier_i2c_set_scl(struct i2c_adapter *adap, int val)
263 {
264 	struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
265 
266 	writel(val ? UNIPHIER_I2C_BRST_RSCL : 0,
267 	       priv->membase + UNIPHIER_I2C_BRST);
268 }
269 
270 static int uniphier_i2c_get_sda(struct i2c_adapter *adap)
271 {
272 	struct uniphier_i2c_priv *priv = i2c_get_adapdata(adap);
273 
274 	return !!(readl(priv->membase + UNIPHIER_I2C_BSTS) &
275 							UNIPHIER_I2C_BSTS_SDA);
276 }
277 
278 static void uniphier_i2c_unprepare_recovery(struct i2c_adapter *adap)
279 {
280 	uniphier_i2c_reset(i2c_get_adapdata(adap), false);
281 }
282 
283 static struct i2c_bus_recovery_info uniphier_i2c_bus_recovery_info = {
284 	.recover_bus = i2c_generic_scl_recovery,
285 	.get_scl = uniphier_i2c_get_scl,
286 	.set_scl = uniphier_i2c_set_scl,
287 	.get_sda = uniphier_i2c_get_sda,
288 	.unprepare_recovery = uniphier_i2c_unprepare_recovery,
289 };
290 
291 static void uniphier_i2c_hw_init(struct uniphier_i2c_priv *priv)
292 {
293 	unsigned int cyc = priv->clk_cycle;
294 
295 	uniphier_i2c_reset(priv, true);
296 
297 	/*
298 	 * Bit30-16: clock cycles of tLOW.
299 	 *  Standard-mode: tLOW = 4.7 us, tHIGH = 4.0 us
300 	 *  Fast-mode:     tLOW = 1.3 us, tHIGH = 0.6 us
301 	 * "tLow/tHIGH = 5/4" meets both.
302 	 */
303 	writel((cyc * 5 / 9 << 16) | cyc, priv->membase + UNIPHIER_I2C_CLK);
304 
305 	uniphier_i2c_reset(priv, false);
306 }
307 
308 static int uniphier_i2c_probe(struct platform_device *pdev)
309 {
310 	struct device *dev = &pdev->dev;
311 	struct uniphier_i2c_priv *priv;
312 	u32 bus_speed;
313 	unsigned long clk_rate;
314 	int irq, ret;
315 
316 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
317 	if (!priv)
318 		return -ENOMEM;
319 
320 	priv->membase = devm_platform_ioremap_resource(pdev, 0);
321 	if (IS_ERR(priv->membase))
322 		return PTR_ERR(priv->membase);
323 
324 	irq = platform_get_irq(pdev, 0);
325 	if (irq < 0)
326 		return irq;
327 
328 	if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
329 		bus_speed = I2C_MAX_STANDARD_MODE_FREQ;
330 
331 	if (!bus_speed || bus_speed > I2C_MAX_FAST_MODE_FREQ) {
332 		dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
333 		return -EINVAL;
334 	}
335 
336 	priv->clk = devm_clk_get_enabled(dev, NULL);
337 	if (IS_ERR(priv->clk)) {
338 		dev_err(dev, "failed to enable clock\n");
339 		return PTR_ERR(priv->clk);
340 	}
341 
342 	clk_rate = clk_get_rate(priv->clk);
343 	if (!clk_rate) {
344 		dev_err(dev, "input clock rate should not be zero\n");
345 		return -EINVAL;
346 	}
347 
348 	priv->clk_cycle = clk_rate / bus_speed;
349 	init_completion(&priv->comp);
350 	priv->adap.owner = THIS_MODULE;
351 	priv->adap.algo = &uniphier_i2c_algo;
352 	priv->adap.dev.parent = dev;
353 	priv->adap.dev.of_node = dev->of_node;
354 	strscpy(priv->adap.name, "UniPhier I2C", sizeof(priv->adap.name));
355 	priv->adap.bus_recovery_info = &uniphier_i2c_bus_recovery_info;
356 	i2c_set_adapdata(&priv->adap, priv);
357 	platform_set_drvdata(pdev, priv);
358 
359 	uniphier_i2c_hw_init(priv);
360 
361 	ret = devm_request_irq(dev, irq, uniphier_i2c_interrupt, 0, pdev->name,
362 			       priv);
363 	if (ret) {
364 		dev_err(dev, "failed to request irq %d\n", irq);
365 		return ret;
366 	}
367 
368 	return i2c_add_adapter(&priv->adap);
369 }
370 
371 static void uniphier_i2c_remove(struct platform_device *pdev)
372 {
373 	struct uniphier_i2c_priv *priv = platform_get_drvdata(pdev);
374 
375 	i2c_del_adapter(&priv->adap);
376 }
377 
378 static int __maybe_unused uniphier_i2c_suspend(struct device *dev)
379 {
380 	struct uniphier_i2c_priv *priv = dev_get_drvdata(dev);
381 
382 	clk_disable_unprepare(priv->clk);
383 
384 	return 0;
385 }
386 
387 static int __maybe_unused uniphier_i2c_resume(struct device *dev)
388 {
389 	struct uniphier_i2c_priv *priv = dev_get_drvdata(dev);
390 	int ret;
391 
392 	ret = clk_prepare_enable(priv->clk);
393 	if (ret)
394 		return ret;
395 
396 	uniphier_i2c_hw_init(priv);
397 
398 	return 0;
399 }
400 
401 static const struct dev_pm_ops uniphier_i2c_pm_ops = {
402 	SET_SYSTEM_SLEEP_PM_OPS(uniphier_i2c_suspend, uniphier_i2c_resume)
403 };
404 
405 static const struct of_device_id uniphier_i2c_match[] = {
406 	{ .compatible = "socionext,uniphier-i2c" },
407 	{ /* sentinel */ }
408 };
409 MODULE_DEVICE_TABLE(of, uniphier_i2c_match);
410 
411 static struct platform_driver uniphier_i2c_drv = {
412 	.probe  = uniphier_i2c_probe,
413 	.remove_new = uniphier_i2c_remove,
414 	.driver = {
415 		.name  = "uniphier-i2c",
416 		.of_match_table = uniphier_i2c_match,
417 		.pm = &uniphier_i2c_pm_ops,
418 	},
419 };
420 module_platform_driver(uniphier_i2c_drv);
421 
422 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
423 MODULE_DESCRIPTION("UniPhier I2C bus driver");
424 MODULE_LICENSE("GPL");
425