32 #include <linux/i2c.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
38 #include <linux/slab.h>
98 #define ID_LAST_MSG (1 << 0)
99 #define ID_IOERROR (1 << 1)
100 #define ID_DONE (1 << 2)
101 #define ID_ARBLOST (1 << 3)
102 #define ID_NACK (1 << 4)
118 #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
119 #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
121 #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
122 #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
124 #define LOOP_TIMEOUT 1024
145 rcar_i2c_write(priv,
ICSIER, 0);
146 rcar_i2c_write(priv,
ICSAR, 0);
147 rcar_i2c_write(priv,
ICSCR, 0);
148 rcar_i2c_write(priv,
ICSSR, 0);
151 rcar_i2c_write(priv,
ICMIER, 0);
152 rcar_i2c_write(priv,
ICMCR, 0);
153 rcar_i2c_write(priv,
ICMSR, 0);
154 rcar_i2c_write(priv,
ICMAR, 0);
176 rcar_i2c_write(priv,
ICMIER, val);
181 rcar_i2c_write(priv,
ICMAR, (priv->
msg->addr << 1) | recv);
193 if (!(rcar_i2c_read(priv,
ICMCR) &
FSDA))
219 static int rcar_i2c_clock_calculate(
struct rcar_i2c_priv *priv,
229 dev_err(dev,
"there is no peripheral_clk\n");
248 for (cdf = 0; cdf < 4; cdf++) {
253 dev_err(dev,
"there is no best CDF\n");
266 round = (ick + 500000) / 1000000 * 285;
267 round = (round + 500) / 1000;
275 for (scgd = 0; scgd < 0x40; scgd++) {
276 scl = ick / (20 + (scgd * 8) + round);
277 if (scl <= bus_speed)
280 dev_err(dev,
"it is impossible to calculate best SCL\n");
284 dev_dbg(dev,
"clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
290 priv->
icccr = (scgd << 2 | cdf);
305 return rcar_i2c_read(priv,
ICMSR);
308 #define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
311 rcar_i2c_write(priv,
ICMSR, ~bit);
319 rcar_i2c_set_addr(priv, 1);
334 ret = rcar_i2c_bus_barrier(priv);
338 rcar_i2c_set_addr(priv, 0);
346 #define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
347 #define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
371 if (priv->
pos < msg->
len) {
431 }
else if (priv->
pos < msg->
len) {
444 if (priv->
pos + 1 >= msg->
len)
461 spin_lock(&priv->
lock);
463 msr = rcar_i2c_status_get(priv);
474 dev_dbg(dev,
"Arbitration Lost\n");
516 spin_unlock(&priv->
lock);
522 static int rcar_i2c_master_xfer(
struct i2c_adapter *adap,
531 pm_runtime_get_sync(dev);
537 rcar_i2c_clock_start(priv);
539 spin_unlock_irqrestore(&priv->
lock, flags);
543 for (i = 0; i < num; i++) {
548 priv->
msg = &msgs[
i];
551 if (priv->
msg == &msgs[num - 1])
556 ret = rcar_i2c_recv(priv);
558 ret = rcar_i2c_send(priv);
560 spin_unlock_irqrestore(&priv->
lock, flags);
612 .master_xfer = rcar_i2c_master_xfer,
613 .functionality = rcar_i2c_func,
628 dev_err(dev,
"no mmio resources\n");
634 dev_err(dev,
"no mem for private data\n");
641 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
647 dev_err(dev,
"cannot ioremap\n");
657 adap->
algo = &rcar_i2c_algo;
661 i2c_set_adapdata(adap, priv);
664 ret = devm_request_irq(dev, priv->
irq, rcar_i2c_irq, 0,
665 dev_name(dev), priv);
667 dev_err(dev,
"cannot get irq %d\n", priv->
irq);
673 dev_err(dev,
"reg adap failed: %d\n", ret);
678 platform_set_drvdata(pdev, priv);
691 pm_runtime_disable(dev);
701 .probe = rcar_i2c_probe,