.. | .. |
---|
2 | 2 | /* |
---|
3 | 3 | * Driver for the Renesas R-Car I2C unit |
---|
4 | 4 | * |
---|
5 | | - * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com> |
---|
6 | | - * Copyright (C) 2011-2015 Renesas Electronics Corporation |
---|
| 5 | + * Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com> |
---|
| 6 | + * Copyright (C) 2011-2019 Renesas Electronics Corporation |
---|
7 | 7 | * |
---|
8 | 8 | * Copyright (C) 2012-14 Renesas Solutions Corp. |
---|
9 | 9 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
---|
.. | .. |
---|
19 | 19 | #include <linux/err.h> |
---|
20 | 20 | #include <linux/interrupt.h> |
---|
21 | 21 | #include <linux/io.h> |
---|
| 22 | +#include <linux/iopoll.h> |
---|
22 | 23 | #include <linux/i2c.h> |
---|
| 24 | +#include <linux/i2c-smbus.h> |
---|
23 | 25 | #include <linux/kernel.h> |
---|
24 | 26 | #include <linux/module.h> |
---|
25 | 27 | #include <linux/of_device.h> |
---|
.. | .. |
---|
39 | 41 | #define ICSAR 0x1C /* slave address */ |
---|
40 | 42 | #define ICMAR 0x20 /* master address */ |
---|
41 | 43 | #define ICRXTX 0x24 /* data port */ |
---|
42 | | -#define ICDMAER 0x3c /* DMA enable */ |
---|
43 | | -#define ICFBSCR 0x38 /* first bit setup cycle */ |
---|
| 44 | +#define ICFBSCR 0x38 /* first bit setup cycle (Gen3) */ |
---|
| 45 | +#define ICDMAER 0x3c /* DMA enable (Gen3) */ |
---|
44 | 46 | |
---|
45 | 47 | /* ICSCR */ |
---|
46 | 48 | #define SDBS (1 << 3) /* slave data buffer select */ |
---|
.. | .. |
---|
83 | 85 | #define TMDMAE (1 << 0) /* DMA Master Transmitted Enable */ |
---|
84 | 86 | |
---|
85 | 87 | /* ICFBSCR */ |
---|
86 | | -#define TCYC06 0x04 /* 6*Tcyc delay 1st bit between SDA and SCL */ |
---|
87 | 88 | #define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */ |
---|
88 | 89 | |
---|
| 90 | +#define RCAR_MIN_DMA_LEN 8 |
---|
89 | 91 | |
---|
90 | 92 | #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG) |
---|
91 | 93 | #define RCAR_BUS_PHASE_DATA (MDBS | MIE) |
---|
92 | | -#define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF) |
---|
93 | 94 | #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB) |
---|
94 | 95 | |
---|
95 | 96 | #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE) |
---|
.. | .. |
---|
105 | 106 | #define ID_ARBLOST (1 << 3) |
---|
106 | 107 | #define ID_NACK (1 << 4) |
---|
107 | 108 | /* persistent flags */ |
---|
| 109 | +#define ID_P_HOST_NOTIFY BIT(28) |
---|
108 | 110 | #define ID_P_REP_AFTER_RD BIT(29) |
---|
109 | 111 | #define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */ |
---|
110 | 112 | #define ID_P_PM_BLOCKED BIT(31) |
---|
111 | | -#define ID_P_MASK GENMASK(31, 29) |
---|
| 113 | +#define ID_P_MASK GENMASK(31, 28) |
---|
112 | 114 | |
---|
113 | 115 | enum rcar_i2c_type { |
---|
114 | 116 | I2C_RCAR_GEN1, |
---|
.. | .. |
---|
140 | 142 | |
---|
141 | 143 | struct reset_control *rstc; |
---|
142 | 144 | int irq; |
---|
| 145 | + |
---|
| 146 | + struct i2c_client *host_notify_client; |
---|
143 | 147 | }; |
---|
144 | 148 | |
---|
145 | 149 | #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent) |
---|
146 | 150 | #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD) |
---|
147 | | - |
---|
148 | | -#define LOOP_TIMEOUT 1024 |
---|
149 | | - |
---|
150 | 151 | |
---|
151 | 152 | static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val) |
---|
152 | 153 | { |
---|
.. | .. |
---|
213 | 214 | rcar_i2c_write(priv, ICMSR, 0); |
---|
214 | 215 | /* start clock */ |
---|
215 | 216 | rcar_i2c_write(priv, ICCCR, priv->icccr); |
---|
| 217 | + |
---|
| 218 | + if (priv->devtype == I2C_RCAR_GEN3) |
---|
| 219 | + rcar_i2c_write(priv, ICFBSCR, TCYC17); |
---|
| 220 | + |
---|
216 | 221 | } |
---|
217 | 222 | |
---|
218 | 223 | static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv) |
---|
219 | 224 | { |
---|
220 | | - int i; |
---|
| 225 | + int ret; |
---|
| 226 | + u32 val; |
---|
221 | 227 | |
---|
222 | | - for (i = 0; i < LOOP_TIMEOUT; i++) { |
---|
223 | | - /* make sure that bus is not busy */ |
---|
224 | | - if (!(rcar_i2c_read(priv, ICMCR) & FSDA)) |
---|
225 | | - return 0; |
---|
226 | | - udelay(1); |
---|
| 228 | + ret = readl_poll_timeout(priv->io + ICMCR, val, !(val & FSDA), 10, |
---|
| 229 | + priv->adap.timeout); |
---|
| 230 | + if (ret) { |
---|
| 231 | + /* Waiting did not help, try to recover */ |
---|
| 232 | + priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL; |
---|
| 233 | + ret = i2c_recover_bus(&priv->adap); |
---|
227 | 234 | } |
---|
228 | 235 | |
---|
229 | | - /* Waiting did not help, try to recover */ |
---|
230 | | - priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL; |
---|
231 | | - return i2c_recover_bus(&priv->adap); |
---|
| 236 | + return ret; |
---|
232 | 237 | } |
---|
233 | 238 | |
---|
234 | | -static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t) |
---|
| 239 | +static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv) |
---|
235 | 240 | { |
---|
236 | 241 | u32 scgd, cdf, round, ick, sum, scl, cdf_width; |
---|
237 | 242 | unsigned long rate; |
---|
238 | 243 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
---|
| 244 | + struct i2c_timings t = { |
---|
| 245 | + .bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ, |
---|
| 246 | + .scl_fall_ns = 35, |
---|
| 247 | + .scl_rise_ns = 200, |
---|
| 248 | + .scl_int_delay_ns = 50, |
---|
| 249 | + }; |
---|
239 | 250 | |
---|
240 | 251 | /* Fall back to previously used values if not supplied */ |
---|
241 | | - t->bus_freq_hz = t->bus_freq_hz ?: 100000; |
---|
242 | | - t->scl_fall_ns = t->scl_fall_ns ?: 35; |
---|
243 | | - t->scl_rise_ns = t->scl_rise_ns ?: 200; |
---|
244 | | - t->scl_int_delay_ns = t->scl_int_delay_ns ?: 50; |
---|
| 252 | + i2c_parse_fw_timings(dev, &t, false); |
---|
245 | 253 | |
---|
246 | 254 | switch (priv->devtype) { |
---|
247 | 255 | case I2C_RCAR_GEN1: |
---|
.. | .. |
---|
287 | 295 | * = F[sum * ick / 1000000000] |
---|
288 | 296 | * = F[(ick / 1000000) * sum / 1000] |
---|
289 | 297 | */ |
---|
290 | | - sum = t->scl_fall_ns + t->scl_rise_ns + t->scl_int_delay_ns; |
---|
| 298 | + sum = t.scl_fall_ns + t.scl_rise_ns + t.scl_int_delay_ns; |
---|
291 | 299 | round = (ick + 500000) / 1000000 * sum; |
---|
292 | 300 | round = (round + 500) / 1000; |
---|
293 | 301 | |
---|
.. | .. |
---|
305 | 313 | */ |
---|
306 | 314 | for (scgd = 0; scgd < 0x40; scgd++) { |
---|
307 | 315 | scl = ick / (20 + (scgd * 8) + round); |
---|
308 | | - if (scl <= t->bus_freq_hz) |
---|
| 316 | + if (scl <= t.bus_freq_hz) |
---|
309 | 317 | goto scgd_find; |
---|
310 | 318 | } |
---|
311 | 319 | dev_err(dev, "it is impossible to calculate best SCL\n"); |
---|
.. | .. |
---|
313 | 321 | |
---|
314 | 322 | scgd_find: |
---|
315 | 323 | dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n", |
---|
316 | | - scl, t->bus_freq_hz, clk_get_rate(priv->clk), round, cdf, scgd); |
---|
| 324 | + scl, t.bus_freq_hz, rate, round, cdf, scgd); |
---|
317 | 325 | |
---|
318 | 326 | /* keep icccr value */ |
---|
319 | 327 | priv->icccr = scgd << cdf_width | cdf; |
---|
.. | .. |
---|
356 | 364 | rcar_i2c_prepare_msg(priv); |
---|
357 | 365 | } |
---|
358 | 366 | |
---|
359 | | -/* |
---|
360 | | - * interrupt functions |
---|
361 | | - */ |
---|
362 | 367 | static void rcar_i2c_dma_unmap(struct rcar_i2c_priv *priv) |
---|
363 | 368 | { |
---|
364 | 369 | struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE |
---|
365 | 370 | ? priv->dma_rx : priv->dma_tx; |
---|
366 | | - |
---|
367 | | - /* Disable DMA Master Received/Transmitted */ |
---|
368 | | - rcar_i2c_write(priv, ICDMAER, 0); |
---|
369 | | - |
---|
370 | | - /* Reset default delay */ |
---|
371 | | - rcar_i2c_write(priv, ICFBSCR, TCYC06); |
---|
372 | 371 | |
---|
373 | 372 | dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg), |
---|
374 | 373 | sg_dma_len(&priv->sg), priv->dma_direction); |
---|
.. | .. |
---|
379 | 378 | priv->flags |= ID_P_NO_RXDMA; |
---|
380 | 379 | |
---|
381 | 380 | priv->dma_direction = DMA_NONE; |
---|
| 381 | + |
---|
| 382 | + /* Disable DMA Master Received/Transmitted, must be last! */ |
---|
| 383 | + rcar_i2c_write(priv, ICDMAER, 0); |
---|
382 | 384 | } |
---|
383 | 385 | |
---|
384 | 386 | static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv) |
---|
.. | .. |
---|
402 | 404 | rcar_i2c_dma_unmap(priv); |
---|
403 | 405 | } |
---|
404 | 406 | |
---|
405 | | -static void rcar_i2c_dma(struct rcar_i2c_priv *priv) |
---|
| 407 | +static bool rcar_i2c_dma(struct rcar_i2c_priv *priv) |
---|
406 | 408 | { |
---|
407 | 409 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
---|
408 | 410 | struct i2c_msg *msg = priv->msg; |
---|
.. | .. |
---|
416 | 418 | int len; |
---|
417 | 419 | |
---|
418 | 420 | /* Do various checks to see if DMA is feasible at all */ |
---|
419 | | - if (IS_ERR(chan) || msg->len < 8 || !(msg->flags & I2C_M_DMA_SAFE) || |
---|
420 | | - (read && priv->flags & ID_P_NO_RXDMA)) |
---|
421 | | - return; |
---|
| 421 | + if (IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN || |
---|
| 422 | + !(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA)) |
---|
| 423 | + return false; |
---|
422 | 424 | |
---|
423 | 425 | if (read) { |
---|
424 | 426 | /* |
---|
.. | .. |
---|
438 | 440 | dma_addr = dma_map_single(chan->device->dev, buf, len, dir); |
---|
439 | 441 | if (dma_mapping_error(chan->device->dev, dma_addr)) { |
---|
440 | 442 | dev_dbg(dev, "dma map failed, using PIO\n"); |
---|
441 | | - return; |
---|
| 443 | + return false; |
---|
442 | 444 | } |
---|
443 | 445 | |
---|
444 | 446 | sg_dma_len(&priv->sg) = len; |
---|
.. | .. |
---|
452 | 454 | if (!txdesc) { |
---|
453 | 455 | dev_dbg(dev, "dma prep slave sg failed, using PIO\n"); |
---|
454 | 456 | rcar_i2c_cleanup_dma(priv); |
---|
455 | | - return; |
---|
| 457 | + return false; |
---|
456 | 458 | } |
---|
457 | 459 | |
---|
458 | 460 | txdesc->callback = rcar_i2c_dma_callback; |
---|
.. | .. |
---|
462 | 464 | if (dma_submit_error(cookie)) { |
---|
463 | 465 | dev_dbg(dev, "submitting dma failed, using PIO\n"); |
---|
464 | 466 | rcar_i2c_cleanup_dma(priv); |
---|
465 | | - return; |
---|
| 467 | + return false; |
---|
466 | 468 | } |
---|
467 | | - |
---|
468 | | - /* Set delay for DMA operations */ |
---|
469 | | - rcar_i2c_write(priv, ICFBSCR, TCYC17); |
---|
470 | 469 | |
---|
471 | 470 | /* Enable DMA Master Received/Transmitted */ |
---|
472 | 471 | if (read) |
---|
.. | .. |
---|
475 | 474 | rcar_i2c_write(priv, ICDMAER, TMDMAE); |
---|
476 | 475 | |
---|
477 | 476 | dma_async_issue_pending(chan); |
---|
| 477 | + return true; |
---|
478 | 478 | } |
---|
479 | 479 | |
---|
480 | 480 | static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr) |
---|
.. | .. |
---|
483 | 483 | |
---|
484 | 484 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
---|
485 | 485 | if (!(msr & MDE)) |
---|
| 486 | + return; |
---|
| 487 | + |
---|
| 488 | + /* Check if DMA can be enabled and take over */ |
---|
| 489 | + if (priv->pos == 1 && rcar_i2c_dma(priv)) |
---|
486 | 490 | return; |
---|
487 | 491 | |
---|
488 | 492 | if (priv->pos < msg->len) { |
---|
.. | .. |
---|
495 | 499 | */ |
---|
496 | 500 | rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]); |
---|
497 | 501 | priv->pos++; |
---|
498 | | - |
---|
499 | | - /* |
---|
500 | | - * Try to use DMA to transmit the rest of the data if |
---|
501 | | - * address transfer phase just finished. |
---|
502 | | - */ |
---|
503 | | - if (msr & MAT) |
---|
504 | | - rcar_i2c_dma(priv); |
---|
505 | 502 | } else { |
---|
506 | 503 | /* |
---|
507 | 504 | * The last data was pushed to ICRXTX on _PREV_ empty irq. |
---|
.. | .. |
---|
620 | 617 | return true; |
---|
621 | 618 | } |
---|
622 | 619 | |
---|
623 | | -static irqreturn_t rcar_i2c_irq(int irq, void *ptr) |
---|
| 620 | +/* |
---|
| 621 | + * This driver has a lock-free design because there are IP cores (at least |
---|
| 622 | + * R-Car Gen2) which have an inherent race condition in their hardware design. |
---|
| 623 | + * There, we need to switch to RCAR_BUS_PHASE_DATA as soon as possible after |
---|
| 624 | + * the interrupt was generated, otherwise an unwanted repeated message gets |
---|
| 625 | + * generated. It turned out that taking a spinlock at the beginning of the ISR |
---|
| 626 | + * was already causing repeated messages. Thus, this driver was converted to |
---|
| 627 | + * the now lockless behaviour. Please keep this in mind when hacking the driver. |
---|
| 628 | + * R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are |
---|
| 629 | + * likely affected. Therefore, we have different interrupt handler entries. |
---|
| 630 | + */ |
---|
| 631 | +static irqreturn_t rcar_i2c_irq(int irq, struct rcar_i2c_priv *priv, u32 msr) |
---|
624 | 632 | { |
---|
625 | | - struct rcar_i2c_priv *priv = ptr; |
---|
626 | | - u32 msr, val; |
---|
627 | | - |
---|
628 | | - /* Clear START or STOP immediately, except for REPSTART after read */ |
---|
629 | | - if (likely(!(priv->flags & ID_P_REP_AFTER_RD))) { |
---|
630 | | - val = rcar_i2c_read(priv, ICMCR); |
---|
631 | | - rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA); |
---|
632 | | - } |
---|
633 | | - |
---|
634 | | - msr = rcar_i2c_read(priv, ICMSR); |
---|
635 | | - |
---|
636 | | - /* Only handle interrupts that are currently enabled */ |
---|
637 | | - msr &= rcar_i2c_read(priv, ICMIER); |
---|
638 | 633 | if (!msr) { |
---|
639 | 634 | if (rcar_i2c_slave_irq(priv)) |
---|
640 | 635 | return IRQ_HANDLED; |
---|
.. | .. |
---|
676 | 671 | } |
---|
677 | 672 | |
---|
678 | 673 | return IRQ_HANDLED; |
---|
| 674 | +} |
---|
| 675 | + |
---|
| 676 | +static irqreturn_t rcar_i2c_gen2_irq(int irq, void *ptr) |
---|
| 677 | +{ |
---|
| 678 | + struct rcar_i2c_priv *priv = ptr; |
---|
| 679 | + u32 msr; |
---|
| 680 | + |
---|
| 681 | + /* Clear START or STOP immediately, except for REPSTART after read */ |
---|
| 682 | + if (likely(!(priv->flags & ID_P_REP_AFTER_RD))) |
---|
| 683 | + rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA); |
---|
| 684 | + |
---|
| 685 | + /* Only handle interrupts that are currently enabled */ |
---|
| 686 | + msr = rcar_i2c_read(priv, ICMSR); |
---|
| 687 | + msr &= rcar_i2c_read(priv, ICMIER); |
---|
| 688 | + |
---|
| 689 | + return rcar_i2c_irq(irq, priv, msr); |
---|
| 690 | +} |
---|
| 691 | + |
---|
| 692 | +static irqreturn_t rcar_i2c_gen3_irq(int irq, void *ptr) |
---|
| 693 | +{ |
---|
| 694 | + struct rcar_i2c_priv *priv = ptr; |
---|
| 695 | + u32 msr; |
---|
| 696 | + |
---|
| 697 | + /* Only handle interrupts that are currently enabled */ |
---|
| 698 | + msr = rcar_i2c_read(priv, ICMSR); |
---|
| 699 | + msr &= rcar_i2c_read(priv, ICMIER); |
---|
| 700 | + |
---|
| 701 | + /* |
---|
| 702 | + * Clear START or STOP immediately, except for REPSTART after read or |
---|
| 703 | + * if a spurious interrupt was detected. |
---|
| 704 | + */ |
---|
| 705 | + if (likely(!(priv->flags & ID_P_REP_AFTER_RD) && msr)) |
---|
| 706 | + rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA); |
---|
| 707 | + |
---|
| 708 | + return rcar_i2c_irq(irq, priv, msr); |
---|
679 | 709 | } |
---|
680 | 710 | |
---|
681 | 711 | static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev, |
---|
.. | .. |
---|
755 | 785 | /* I2C is a special case, we need to poll the status of a reset */ |
---|
756 | 786 | static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv) |
---|
757 | 787 | { |
---|
758 | | - int i, ret; |
---|
| 788 | + int ret; |
---|
759 | 789 | |
---|
760 | 790 | ret = reset_control_reset(priv->rstc); |
---|
761 | 791 | if (ret) |
---|
762 | 792 | return ret; |
---|
763 | 793 | |
---|
764 | | - for (i = 0; i < LOOP_TIMEOUT; i++) { |
---|
765 | | - ret = reset_control_status(priv->rstc); |
---|
766 | | - if (ret == 0) |
---|
767 | | - return 0; |
---|
768 | | - udelay(1); |
---|
769 | | - } |
---|
770 | | - |
---|
771 | | - return -ETIMEDOUT; |
---|
| 794 | + return read_poll_timeout_atomic(reset_control_status, ret, ret == 0, 1, |
---|
| 795 | + 100, false, priv->rstc); |
---|
772 | 796 | } |
---|
773 | 797 | |
---|
774 | 798 | static int rcar_i2c_master_xfer(struct i2c_adapter *adap, |
---|
.. | .. |
---|
879 | 903 | |
---|
880 | 904 | static u32 rcar_i2c_func(struct i2c_adapter *adap) |
---|
881 | 905 | { |
---|
| 906 | + struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
---|
| 907 | + |
---|
882 | 908 | /* |
---|
883 | 909 | * This HW can't do: |
---|
884 | 910 | * I2C_SMBUS_QUICK (setting FSB during START didn't work) |
---|
885 | 911 | * I2C_M_NOSTART (automatically sends address after START) |
---|
886 | 912 | * I2C_M_IGNORE_NAK (automatically sends STOP after NAK) |
---|
887 | 913 | */ |
---|
888 | | - return I2C_FUNC_I2C | I2C_FUNC_SLAVE | |
---|
889 | | - (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); |
---|
| 914 | + u32 func = I2C_FUNC_I2C | I2C_FUNC_SLAVE | |
---|
| 915 | + (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); |
---|
| 916 | + |
---|
| 917 | + if (priv->flags & ID_P_HOST_NOTIFY) |
---|
| 918 | + func |= I2C_FUNC_SMBUS_HOST_NOTIFY; |
---|
| 919 | + |
---|
| 920 | + return func; |
---|
890 | 921 | } |
---|
891 | 922 | |
---|
892 | 923 | static const struct i2c_algorithm rcar_i2c_algo = { |
---|
.. | .. |
---|
923 | 954 | struct rcar_i2c_priv *priv; |
---|
924 | 955 | struct i2c_adapter *adap; |
---|
925 | 956 | struct device *dev = &pdev->dev; |
---|
926 | | - struct i2c_timings i2c_t; |
---|
| 957 | + unsigned long irqflags = 0; |
---|
| 958 | + irqreturn_t (*irqhandler)(int irq, void *ptr) = rcar_i2c_gen3_irq; |
---|
927 | 959 | int ret; |
---|
| 960 | + |
---|
| 961 | + /* Otherwise logic will break because some bytes must always use PIO */ |
---|
| 962 | + BUILD_BUG_ON_MSG(RCAR_MIN_DMA_LEN < 3, "Invalid min DMA length"); |
---|
928 | 963 | |
---|
929 | 964 | priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL); |
---|
930 | 965 | if (!priv) |
---|
.. | .. |
---|
936 | 971 | return PTR_ERR(priv->clk); |
---|
937 | 972 | } |
---|
938 | 973 | |
---|
939 | | - priv->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
---|
940 | | - |
---|
941 | | - priv->io = devm_ioremap_resource(dev, priv->res); |
---|
| 974 | + priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res); |
---|
942 | 975 | if (IS_ERR(priv->io)) |
---|
943 | 976 | return PTR_ERR(priv->io); |
---|
944 | 977 | |
---|
.. | .. |
---|
957 | 990 | i2c_set_adapdata(adap, priv); |
---|
958 | 991 | strlcpy(adap->name, pdev->name, sizeof(adap->name)); |
---|
959 | 992 | |
---|
960 | | - i2c_parse_fw_timings(dev, &i2c_t, false); |
---|
961 | | - |
---|
962 | 993 | /* Init DMA */ |
---|
963 | 994 | sg_init_table(&priv->sg, 1); |
---|
964 | 995 | priv->dma_direction = DMA_NONE; |
---|
.. | .. |
---|
967 | 998 | /* Activate device for clock calculation */ |
---|
968 | 999 | pm_runtime_enable(dev); |
---|
969 | 1000 | pm_runtime_get_sync(dev); |
---|
970 | | - ret = rcar_i2c_clock_calculate(priv, &i2c_t); |
---|
971 | | - if (ret < 0) |
---|
972 | | - goto out_pm_put; |
---|
| 1001 | + ret = rcar_i2c_clock_calculate(priv); |
---|
| 1002 | + if (ret < 0) { |
---|
| 1003 | + pm_runtime_put(dev); |
---|
| 1004 | + goto out_pm_disable; |
---|
| 1005 | + } |
---|
973 | 1006 | |
---|
974 | 1007 | rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */ |
---|
| 1008 | + |
---|
| 1009 | + if (priv->devtype < I2C_RCAR_GEN3) { |
---|
| 1010 | + irqflags |= IRQF_NO_THREAD; |
---|
| 1011 | + irqhandler = rcar_i2c_gen2_irq; |
---|
| 1012 | + } |
---|
975 | 1013 | |
---|
976 | 1014 | if (priv->devtype == I2C_RCAR_GEN3) { |
---|
977 | 1015 | priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); |
---|
.. | .. |
---|
988 | 1026 | else |
---|
989 | 1027 | pm_runtime_put(dev); |
---|
990 | 1028 | |
---|
| 1029 | + if (of_property_read_bool(dev->of_node, "smbus")) |
---|
| 1030 | + priv->flags |= ID_P_HOST_NOTIFY; |
---|
991 | 1031 | |
---|
992 | | - priv->irq = platform_get_irq(pdev, 0); |
---|
993 | | - ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0, dev_name(dev), priv); |
---|
| 1032 | + ret = platform_get_irq(pdev, 0); |
---|
| 1033 | + if (ret < 0) |
---|
| 1034 | + goto out_pm_put; |
---|
| 1035 | + priv->irq = ret; |
---|
| 1036 | + ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv); |
---|
994 | 1037 | if (ret < 0) { |
---|
995 | 1038 | dev_err(dev, "cannot get irq %d\n", priv->irq); |
---|
996 | | - goto out_pm_disable; |
---|
| 1039 | + goto out_pm_put; |
---|
997 | 1040 | } |
---|
998 | 1041 | |
---|
999 | 1042 | platform_set_drvdata(pdev, priv); |
---|
1000 | 1043 | |
---|
1001 | 1044 | ret = i2c_add_numbered_adapter(adap); |
---|
1002 | 1045 | if (ret < 0) |
---|
1003 | | - goto out_pm_disable; |
---|
| 1046 | + goto out_pm_put; |
---|
| 1047 | + |
---|
| 1048 | + if (priv->flags & ID_P_HOST_NOTIFY) { |
---|
| 1049 | + priv->host_notify_client = i2c_new_slave_host_notify_device(adap); |
---|
| 1050 | + if (IS_ERR(priv->host_notify_client)) { |
---|
| 1051 | + ret = PTR_ERR(priv->host_notify_client); |
---|
| 1052 | + goto out_del_device; |
---|
| 1053 | + } |
---|
| 1054 | + } |
---|
1004 | 1055 | |
---|
1005 | 1056 | dev_info(dev, "probed\n"); |
---|
1006 | 1057 | |
---|
1007 | 1058 | return 0; |
---|
1008 | 1059 | |
---|
| 1060 | + out_del_device: |
---|
| 1061 | + i2c_del_adapter(&priv->adap); |
---|
1009 | 1062 | out_pm_put: |
---|
1010 | | - pm_runtime_put(dev); |
---|
| 1063 | + if (priv->flags & ID_P_PM_BLOCKED) |
---|
| 1064 | + pm_runtime_put(dev); |
---|
1011 | 1065 | out_pm_disable: |
---|
1012 | 1066 | pm_runtime_disable(dev); |
---|
1013 | 1067 | return ret; |
---|
.. | .. |
---|
1018 | 1072 | struct rcar_i2c_priv *priv = platform_get_drvdata(pdev); |
---|
1019 | 1073 | struct device *dev = &pdev->dev; |
---|
1020 | 1074 | |
---|
| 1075 | + if (priv->host_notify_client) |
---|
| 1076 | + i2c_free_slave_host_notify_device(priv->host_notify_client); |
---|
1021 | 1077 | i2c_del_adapter(&priv->adap); |
---|
1022 | 1078 | rcar_i2c_release_dma(priv); |
---|
1023 | 1079 | if (priv->flags & ID_P_PM_BLOCKED) |
---|
.. | .. |
---|
1027 | 1083 | return 0; |
---|
1028 | 1084 | } |
---|
1029 | 1085 | |
---|
| 1086 | +#ifdef CONFIG_PM_SLEEP |
---|
| 1087 | +static int rcar_i2c_suspend(struct device *dev) |
---|
| 1088 | +{ |
---|
| 1089 | + struct rcar_i2c_priv *priv = dev_get_drvdata(dev); |
---|
| 1090 | + |
---|
| 1091 | + i2c_mark_adapter_suspended(&priv->adap); |
---|
| 1092 | + return 0; |
---|
| 1093 | +} |
---|
| 1094 | + |
---|
| 1095 | +static int rcar_i2c_resume(struct device *dev) |
---|
| 1096 | +{ |
---|
| 1097 | + struct rcar_i2c_priv *priv = dev_get_drvdata(dev); |
---|
| 1098 | + |
---|
| 1099 | + i2c_mark_adapter_resumed(&priv->adap); |
---|
| 1100 | + return 0; |
---|
| 1101 | +} |
---|
| 1102 | + |
---|
| 1103 | +static const struct dev_pm_ops rcar_i2c_pm_ops = { |
---|
| 1104 | + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rcar_i2c_suspend, rcar_i2c_resume) |
---|
| 1105 | +}; |
---|
| 1106 | + |
---|
| 1107 | +#define DEV_PM_OPS (&rcar_i2c_pm_ops) |
---|
| 1108 | +#else |
---|
| 1109 | +#define DEV_PM_OPS NULL |
---|
| 1110 | +#endif /* CONFIG_PM_SLEEP */ |
---|
| 1111 | + |
---|
1030 | 1112 | static struct platform_driver rcar_i2c_driver = { |
---|
1031 | 1113 | .driver = { |
---|
1032 | 1114 | .name = "i2c-rcar", |
---|
1033 | 1115 | .of_match_table = rcar_i2c_dt_ids, |
---|
| 1116 | + .pm = DEV_PM_OPS, |
---|
1034 | 1117 | }, |
---|
1035 | 1118 | .probe = rcar_i2c_probe, |
---|
1036 | 1119 | .remove = rcar_i2c_remove, |
---|