2 * Driver for the Renesas RCar I2C unit
4 * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
6 * Copyright (C) 2012-14 Renesas Solutions Corp.
7 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
10 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
12 * This file used out-of-tree driver i2c-rcar.c
13 * Copyright (C) 2011-2012 Renesas Electronics Corporation
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; version 2 of the License.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 #include <linux/clk.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
29 #include <linux/i2c.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/of_device.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/slab.h>
37 /* register offsets */
38 #define ICSCR 0x00 /* slave ctrl */
39 #define ICMCR 0x04 /* master ctrl */
40 #define ICSSR 0x08 /* slave status */
41 #define ICMSR 0x0C /* master status */
42 #define ICSIER 0x10 /* slave irq enable */
43 #define ICMIER 0x14 /* master irq enable */
44 #define ICCCR 0x18 /* clock dividers */
45 #define ICSAR 0x1C /* slave address */
46 #define ICMAR 0x20 /* master address */
47 #define ICRXTX 0x24 /* data port */
50 #define SDBS (1 << 3) /* slave data buffer select */
51 #define SIE (1 << 2) /* slave interface enable */
52 #define GCAE (1 << 1) /* general call address enable */
53 #define FNA (1 << 0) /* forced non acknowledgment */
56 #define MDBS (1 << 7) /* non-fifo mode switch */
57 #define FSCL (1 << 6) /* override SCL pin */
58 #define FSDA (1 << 5) /* override SDA pin */
59 #define OBPC (1 << 4) /* override pins */
60 #define MIE (1 << 3) /* master if enable */
62 #define FSB (1 << 1) /* force stop bit */
63 #define ESG (1 << 0) /* en startbit gen */
65 /* ICSSR (also for ICSIER) */
66 #define GCAR (1 << 6) /* general call received */
67 #define STM (1 << 5) /* slave transmit mode */
68 #define SSR (1 << 4) /* stop received */
69 #define SDE (1 << 3) /* slave data empty */
70 #define SDT (1 << 2) /* slave data transmitted */
71 #define SDR (1 << 1) /* slave data received */
72 #define SAR (1 << 0) /* slave addr received */
74 /* ICMSR (also for ICMIE) */
75 #define MNR (1 << 6) /* nack received */
76 #define MAL (1 << 5) /* arbitration lost */
77 #define MST (1 << 4) /* sent a stop */
81 #define MAT (1 << 0) /* slave addr xfer done */
84 #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
85 #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
86 #define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
87 #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
89 #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
90 #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
91 #define RCAR_IRQ_STOP (MST)
93 #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
94 #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
96 #define ID_LAST_MSG (1 << 0)
97 #define ID_DONE (1 << 2)
98 #define ID_ARBLOST (1 << 3)
99 #define ID_NACK (1 << 4)
107 struct rcar_i2c_priv {
109 struct i2c_adapter adap;
114 wait_queue_head_t wait;
119 enum rcar_i2c_type devtype;
120 struct i2c_client *slave;
123 #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
124 #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
126 #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
127 #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
129 #define LOOP_TIMEOUT 1024
132 static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
134 writel(val, priv->io + reg);
137 static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
139 return readl(priv->io + reg);
142 static void rcar_i2c_init(struct rcar_i2c_priv *priv)
144 /* reset master mode */
145 rcar_i2c_write(priv, ICMIER, 0);
146 rcar_i2c_write(priv, ICMCR, MDBS);
147 rcar_i2c_write(priv, ICMSR, 0);
149 rcar_i2c_write(priv, ICCCR, priv->icccr);
152 static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
156 for (i = 0; i < LOOP_TIMEOUT; i++) {
157 /* make sure that bus is not busy */
158 if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
166 static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
176 switch (priv->devtype) {
185 dev_err(dev, "device type error\n");
190 * calculate SCL clock
194 * ick = clkp / (1 + CDF)
195 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
197 * ick : I2C internal clock < 20 MHz
198 * ticf : I2C SCL falling time = 35 ns here
199 * tr : I2C SCL rising time = 200 ns here
200 * intd : LSI internal delay = 50 ns here
201 * clkp : peripheral_clk
202 * F[] : integer up-valuation
204 rate = clk_get_rate(priv->clk);
205 cdf = rate / 20000000;
206 if (cdf >= 1U << cdf_width) {
207 dev_err(dev, "Input clock %lu too high\n", rate);
210 ick = rate / (cdf + 1);
213 * it is impossible to calculate large scale
214 * number on u32. separate it
216 * F[(ticf + tr + intd) * ick]
217 * = F[(35 + 200 + 50)ns * ick]
218 * = F[285 * ick / 1000000000]
219 * = F[(ick / 1000000) * 285 / 1000]
221 round = (ick + 500000) / 1000000 * 285;
222 round = (round + 500) / 1000;
225 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
227 * Calculation result (= SCL) should be less than
228 * bus_speed for hardware safety
230 * We could use something along the lines of
231 * div = ick / (bus_speed + 1) + 1;
232 * scgd = (div - 20 - round + 7) / 8;
233 * scl = ick / (20 + (scgd * 8) + round);
234 * (not fully verified) but that would get pretty involved
236 for (scgd = 0; scgd < 0x40; scgd++) {
237 scl = ick / (20 + (scgd * 8) + round);
238 if (scl <= bus_speed)
241 dev_err(dev, "it is impossible to calculate best SCL\n");
245 dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
246 scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
251 priv->icccr = scgd << cdf_width | cdf;
256 static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
258 int read = !!rcar_i2c_is_recv(priv);
262 if (priv->msgs_left == 1)
263 rcar_i2c_flags_set(priv, ID_LAST_MSG);
265 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
266 rcar_i2c_write(priv, ICMSR, 0);
267 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
268 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
271 static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
275 rcar_i2c_prepare_msg(priv);
279 * interrupt functions
281 static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
283 struct i2c_msg *msg = priv->msg;
287 * sometimes, unknown interrupt happened.
293 if (priv->pos < msg->len) {
295 * Prepare next data to ICRXTX register.
296 * This data will go to _SHIFT_ register.
299 * [ICRXTX] -> [SHIFT] -> [I2C bus]
301 rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
306 * The last data was pushed to ICRXTX on _PREV_ empty irq.
307 * It is on _SHIFT_ register, and will sent to I2C bus.
310 * [ICRXTX] -> [SHIFT] -> [I2C bus]
313 if (priv->flags & ID_LAST_MSG) {
315 * If current msg is the _LAST_ msg,
316 * prepare stop condition here.
317 * ID_DONE will be set on STOP irq.
319 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
321 rcar_i2c_next_msg(priv);
326 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
329 static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
331 struct i2c_msg *msg = priv->msg;
335 * sometimes, unknown interrupt happened.
342 /* Address transfer phase finished, but no data at this point. */
343 } else if (priv->pos < msg->len) {
347 msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
352 * If next received data is the _LAST_,
354 * otherwise, go to DATA phase.
356 if (priv->pos + 1 >= msg->len)
357 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
359 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
360 rcar_i2c_next_msg(priv);
362 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
365 static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
367 u32 ssr_raw, ssr_filtered;
370 ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
371 ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
376 /* address detected */
377 if (ssr_filtered & SAR) {
378 /* read or write request */
380 i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
381 rcar_i2c_write(priv, ICRXTX, value);
382 rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
384 i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
385 rcar_i2c_read(priv, ICRXTX); /* dummy read */
386 rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
389 /* Clear SSR, too, because of old STOPs to other clients than us */
390 rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff);
393 /* master sent stop */
394 if (ssr_filtered & SSR) {
395 i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
396 rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */
397 rcar_i2c_write(priv, ICSIER, SAR);
398 rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
401 /* master wants to write to us */
402 if (ssr_filtered & SDR) {
405 value = rcar_i2c_read(priv, ICRXTX);
406 ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
407 /* Send NACK in case of error */
408 rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
409 rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
412 /* master wants to read from us */
413 if (ssr_filtered & SDE) {
414 i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
415 rcar_i2c_write(priv, ICRXTX, value);
416 rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
422 static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
424 struct rcar_i2c_priv *priv = ptr;
427 /* Clear START or STOP as soon as we can */
428 val = rcar_i2c_read(priv, ICMCR);
429 rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
431 msr = rcar_i2c_read(priv, ICMSR);
433 /* Only handle interrupts that are currently enabled */
434 msr &= rcar_i2c_read(priv, ICMIER);
436 if (rcar_i2c_slave_irq(priv))
442 /* Arbitration lost */
444 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
450 /* HW automatically sends STOP after received NACK */
451 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
452 rcar_i2c_flags_set(priv, ID_NACK);
458 priv->msgs_left--; /* The last message also made it */
459 rcar_i2c_flags_set(priv, ID_DONE);
463 if (rcar_i2c_is_recv(priv))
464 rcar_i2c_irq_recv(priv, msr);
466 rcar_i2c_irq_send(priv, msr);
469 if (rcar_i2c_flags_has(priv, ID_DONE)) {
470 rcar_i2c_write(priv, ICMIER, 0);
471 rcar_i2c_write(priv, ICMSR, 0);
472 wake_up(&priv->wait);
478 static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
479 struct i2c_msg *msgs,
482 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
483 struct device *dev = rcar_i2c_priv_to_dev(priv);
487 pm_runtime_get_sync(dev);
491 ret = rcar_i2c_bus_barrier(priv);
495 for (i = 0; i < num; i++) {
496 /* This HW can't send STOP after address phase */
497 if (msgs[i].len == 0) {
505 priv->msgs_left = num;
507 rcar_i2c_prepare_msg(priv);
509 time_left = wait_event_timeout(priv->wait,
510 rcar_i2c_flags_has(priv, ID_DONE),
511 num * adap->timeout);
515 } else if (rcar_i2c_flags_has(priv, ID_NACK)) {
517 } else if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
520 ret = num - priv->msgs_left; /* The number of transfer */
525 if (ret < 0 && ret != -ENXIO)
526 dev_err(dev, "error %d : %x\n", ret, priv->flags);
531 static int rcar_reg_slave(struct i2c_client *slave)
533 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
538 if (slave->flags & I2C_CLIENT_TEN)
539 return -EAFNOSUPPORT;
541 pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
544 rcar_i2c_write(priv, ICSAR, slave->addr);
545 rcar_i2c_write(priv, ICSSR, 0);
546 rcar_i2c_write(priv, ICSIER, SAR);
547 rcar_i2c_write(priv, ICSCR, SIE | SDBS);
552 static int rcar_unreg_slave(struct i2c_client *slave)
554 struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
556 WARN_ON(!priv->slave);
558 rcar_i2c_write(priv, ICSIER, 0);
559 rcar_i2c_write(priv, ICSCR, 0);
563 pm_runtime_put(rcar_i2c_priv_to_dev(priv));
568 static u32 rcar_i2c_func(struct i2c_adapter *adap)
570 /* This HW can't do SMBUS_QUICK and NOSTART */
571 return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
572 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
575 static const struct i2c_algorithm rcar_i2c_algo = {
576 .master_xfer = rcar_i2c_master_xfer,
577 .functionality = rcar_i2c_func,
578 .reg_slave = rcar_reg_slave,
579 .unreg_slave = rcar_unreg_slave,
582 static const struct of_device_id rcar_i2c_dt_ids[] = {
583 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
584 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
585 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
586 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
587 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
588 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
589 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
590 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
591 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
594 MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
596 static int rcar_i2c_probe(struct platform_device *pdev)
598 struct rcar_i2c_priv *priv;
599 struct i2c_adapter *adap;
600 struct resource *res;
601 struct device *dev = &pdev->dev;
605 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
609 priv->clk = devm_clk_get(dev, NULL);
610 if (IS_ERR(priv->clk)) {
611 dev_err(dev, "cannot get clock\n");
612 return PTR_ERR(priv->clk);
615 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
616 priv->io = devm_ioremap_resource(dev, res);
617 if (IS_ERR(priv->io))
618 return PTR_ERR(priv->io);
620 bus_speed = 100000; /* default 100 kHz */
621 of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
623 priv->devtype = (enum rcar_i2c_type)of_match_device(rcar_i2c_dt_ids, dev)->data;
625 pm_runtime_enable(dev);
626 pm_runtime_get_sync(dev);
627 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
633 irq = platform_get_irq(pdev, 0);
634 init_waitqueue_head(&priv->wait);
638 adap->algo = &rcar_i2c_algo;
639 adap->class = I2C_CLASS_DEPRECATED;
641 adap->dev.parent = dev;
642 adap->dev.of_node = dev->of_node;
643 i2c_set_adapdata(adap, priv);
644 strlcpy(adap->name, pdev->name, sizeof(adap->name));
646 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
647 dev_name(dev), priv);
649 dev_err(dev, "cannot get irq %d\n", irq);
653 platform_set_drvdata(pdev, priv);
655 ret = i2c_add_numbered_adapter(adap);
657 dev_err(dev, "reg adap failed: %d\n", ret);
661 dev_info(dev, "probed\n");
668 pm_runtime_disable(dev);
672 static int rcar_i2c_remove(struct platform_device *pdev)
674 struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
675 struct device *dev = &pdev->dev;
677 i2c_del_adapter(&priv->adap);
678 pm_runtime_disable(dev);
683 static struct platform_driver rcar_i2c_driver = {
686 .of_match_table = rcar_i2c_dt_ids,
688 .probe = rcar_i2c_probe,
689 .remove = rcar_i2c_remove,
692 module_platform_driver(rcar_i2c_driver);
694 MODULE_LICENSE("GPL v2");
695 MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
696 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");