Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 1 | /* |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 2 | * Driver for the Renesas RCar I2C unit |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 3 | * |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 4 | * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com> |
| 5 | * Copyright (C) 2011-2015 Renesas Electronics Corporation |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2012-14 Renesas Solutions Corp. |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 8 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 9 | * |
| 10 | * This file is based on the drivers/i2c/busses/i2c-sh7760.c |
| 11 | * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com> |
| 12 | * |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 15 | * the Free Software Foundation; version 2 of the License. |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 21 | */ |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/delay.h> |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 24 | #include <linux/dmaengine.h> |
| 25 | #include <linux/dma-mapping.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 26 | #include <linux/err.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/i2c.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 30 | #include <linux/kernel.h> |
| 31 | #include <linux/module.h> |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 32 | #include <linux/of_device.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/pm_runtime.h> |
| 35 | #include <linux/slab.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 36 | |
| 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 */ |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 48 | #define ICDMAER 0x3c /* DMA enable */ |
| 49 | #define ICFBSCR 0x38 /* first bit setup cycle */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 50 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 51 | /* ICSCR */ |
| 52 | #define SDBS (1 << 3) /* slave data buffer select */ |
| 53 | #define SIE (1 << 2) /* slave interface enable */ |
| 54 | #define GCAE (1 << 1) /* general call address enable */ |
| 55 | #define FNA (1 << 0) /* forced non acknowledgment */ |
| 56 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 57 | /* ICMCR */ |
| 58 | #define MDBS (1 << 7) /* non-fifo mode switch */ |
| 59 | #define FSCL (1 << 6) /* override SCL pin */ |
| 60 | #define FSDA (1 << 5) /* override SDA pin */ |
| 61 | #define OBPC (1 << 4) /* override pins */ |
| 62 | #define MIE (1 << 3) /* master if enable */ |
| 63 | #define TSBE (1 << 2) |
| 64 | #define FSB (1 << 1) /* force stop bit */ |
| 65 | #define ESG (1 << 0) /* en startbit gen */ |
| 66 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 67 | /* ICSSR (also for ICSIER) */ |
| 68 | #define GCAR (1 << 6) /* general call received */ |
| 69 | #define STM (1 << 5) /* slave transmit mode */ |
| 70 | #define SSR (1 << 4) /* stop received */ |
| 71 | #define SDE (1 << 3) /* slave data empty */ |
| 72 | #define SDT (1 << 2) /* slave data transmitted */ |
| 73 | #define SDR (1 << 1) /* slave data received */ |
| 74 | #define SAR (1 << 0) /* slave addr received */ |
| 75 | |
Wolfram Sang | 3e3aaba | 2014-05-28 09:44:44 +0200 | [diff] [blame] | 76 | /* ICMSR (also for ICMIE) */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 77 | #define MNR (1 << 6) /* nack received */ |
| 78 | #define MAL (1 << 5) /* arbitration lost */ |
| 79 | #define MST (1 << 4) /* sent a stop */ |
| 80 | #define MDE (1 << 3) |
| 81 | #define MDT (1 << 2) |
| 82 | #define MDR (1 << 1) |
| 83 | #define MAT (1 << 0) /* slave addr xfer done */ |
| 84 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 85 | /* ICDMAER */ |
| 86 | #define RSDMAE (1 << 3) /* DMA Slave Received Enable */ |
| 87 | #define TSDMAE (1 << 2) /* DMA Slave Transmitted Enable */ |
| 88 | #define RMDMAE (1 << 1) /* DMA Master Received Enable */ |
| 89 | #define TMDMAE (1 << 0) /* DMA Master Transmitted Enable */ |
| 90 | |
| 91 | /* ICFBSCR */ |
| 92 | #define TCYC06 0x04 /* 6*Tcyc delay 1st bit between SDA and SCL */ |
| 93 | #define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */ |
| 94 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 95 | |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 96 | #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG) |
| 97 | #define RCAR_BUS_PHASE_DATA (MDBS | MIE) |
Wolfram Sang | 52df445 | 2015-11-19 16:56:49 +0100 | [diff] [blame] | 98 | #define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF) |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 99 | #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 100 | |
Wolfram Sang | 3e3aaba | 2014-05-28 09:44:44 +0200 | [diff] [blame] | 101 | #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE) |
| 102 | #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR) |
| 103 | #define RCAR_IRQ_STOP (MST) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 104 | |
Sergei Shtylyov | 938916f | 2014-09-06 03:34:32 +0400 | [diff] [blame] | 105 | #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF) |
| 106 | #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF) |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 107 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 108 | #define ID_LAST_MSG (1 << 0) |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 109 | #define ID_FIRST_MSG (1 << 1) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 110 | #define ID_DONE (1 << 2) |
| 111 | #define ID_ARBLOST (1 << 3) |
| 112 | #define ID_NACK (1 << 4) |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 113 | /* persistent flags */ |
| 114 | #define ID_P_PM_BLOCKED (1 << 31) |
| 115 | #define ID_P_MASK ID_P_PM_BLOCKED |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 116 | |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 117 | enum rcar_i2c_type { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 118 | I2C_RCAR_GEN1, |
| 119 | I2C_RCAR_GEN2, |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 120 | I2C_RCAR_GEN3, |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 123 | struct rcar_i2c_priv { |
| 124 | void __iomem *io; |
| 125 | struct i2c_adapter adap; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 126 | struct i2c_msg *msg; |
| 127 | int msgs_left; |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 128 | struct clk *clk; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 129 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 130 | wait_queue_head_t wait; |
| 131 | |
| 132 | int pos; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 133 | u32 icccr; |
| 134 | u32 flags; |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 135 | enum rcar_i2c_type devtype; |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 136 | struct i2c_client *slave; |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 137 | |
| 138 | struct resource *res; |
| 139 | struct dma_chan *dma_tx; |
| 140 | struct dma_chan *dma_rx; |
| 141 | struct scatterlist sg; |
| 142 | enum dma_data_direction dma_direction; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent) |
| 146 | #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD) |
| 147 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 148 | #define LOOP_TIMEOUT 1024 |
| 149 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 150 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 151 | static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val) |
| 152 | { |
| 153 | writel(val, priv->io + reg); |
| 154 | } |
| 155 | |
| 156 | static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg) |
| 157 | { |
| 158 | return readl(priv->io + reg); |
| 159 | } |
| 160 | |
| 161 | static void rcar_i2c_init(struct rcar_i2c_priv *priv) |
| 162 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 163 | /* reset master mode */ |
| 164 | rcar_i2c_write(priv, ICMIER, 0); |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 165 | rcar_i2c_write(priv, ICMCR, MDBS); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 166 | rcar_i2c_write(priv, ICMSR, 0); |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 167 | /* start clock */ |
| 168 | rcar_i2c_write(priv, ICCCR, priv->icccr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 171 | static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv) |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | for (i = 0; i < LOOP_TIMEOUT; i++) { |
| 176 | /* make sure that bus is not busy */ |
| 177 | if (!(rcar_i2c_read(priv, ICMCR) & FSDA)) |
| 178 | return 0; |
| 179 | udelay(1); |
| 180 | } |
| 181 | |
| 182 | return -EBUSY; |
| 183 | } |
| 184 | |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 185 | static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 186 | { |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 187 | u32 scgd, cdf, round, ick, sum, scl, cdf_width; |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 188 | unsigned long rate; |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 189 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 190 | |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 191 | /* Fall back to previously used values if not supplied */ |
| 192 | t->bus_freq_hz = t->bus_freq_hz ?: 100000; |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 193 | t->scl_fall_ns = t->scl_fall_ns ?: 35; |
| 194 | t->scl_rise_ns = t->scl_rise_ns ?: 200; |
| 195 | t->scl_int_delay_ns = t->scl_int_delay_ns ?: 50; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 196 | |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 197 | switch (priv->devtype) { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 198 | case I2C_RCAR_GEN1: |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 199 | cdf_width = 2; |
| 200 | break; |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 201 | case I2C_RCAR_GEN2: |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 202 | case I2C_RCAR_GEN3: |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 203 | cdf_width = 3; |
| 204 | break; |
| 205 | default: |
| 206 | dev_err(dev, "device type error\n"); |
| 207 | return -EIO; |
| 208 | } |
| 209 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 210 | /* |
| 211 | * calculate SCL clock |
| 212 | * see |
| 213 | * ICCCR |
| 214 | * |
| 215 | * ick = clkp / (1 + CDF) |
| 216 | * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) |
| 217 | * |
| 218 | * ick : I2C internal clock < 20 MHz |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 219 | * ticf : I2C SCL falling time |
| 220 | * tr : I2C SCL rising time |
| 221 | * intd : LSI internal delay |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 222 | * clkp : peripheral_clk |
| 223 | * F[] : integer up-valuation |
| 224 | */ |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 225 | rate = clk_get_rate(priv->clk); |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 226 | cdf = rate / 20000000; |
Wolfram Sang | 22762cc | 2014-09-20 12:07:37 +0200 | [diff] [blame] | 227 | if (cdf >= 1U << cdf_width) { |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 228 | dev_err(dev, "Input clock %lu too high\n", rate); |
| 229 | return -EIO; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 230 | } |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 231 | ick = rate / (cdf + 1); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 232 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 233 | /* |
| 234 | * it is impossible to calculate large scale |
| 235 | * number on u32. separate it |
| 236 | * |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 237 | * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd) |
| 238 | * = F[sum * ick / 1000000000] |
| 239 | * = F[(ick / 1000000) * sum / 1000] |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 240 | */ |
Wolfram Sang | ca68ead | 2015-12-08 10:37:49 +0100 | [diff] [blame] | 241 | sum = t->scl_fall_ns + t->scl_rise_ns + t->scl_int_delay_ns; |
| 242 | round = (ick + 500000) / 1000000 * sum; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 243 | round = (round + 500) / 1000; |
| 244 | |
| 245 | /* |
| 246 | * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) |
| 247 | * |
| 248 | * Calculation result (= SCL) should be less than |
| 249 | * bus_speed for hardware safety |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 250 | * |
| 251 | * We could use something along the lines of |
| 252 | * div = ick / (bus_speed + 1) + 1; |
| 253 | * scgd = (div - 20 - round + 7) / 8; |
| 254 | * scl = ick / (20 + (scgd * 8) + round); |
| 255 | * (not fully verified) but that would get pretty involved |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 256 | */ |
| 257 | for (scgd = 0; scgd < 0x40; scgd++) { |
| 258 | scl = ick / (20 + (scgd * 8) + round); |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 259 | if (scl <= t->bus_freq_hz) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 260 | goto scgd_find; |
| 261 | } |
| 262 | dev_err(dev, "it is impossible to calculate best SCL\n"); |
| 263 | return -EIO; |
| 264 | |
| 265 | scgd_find: |
| 266 | dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n", |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 267 | scl, t->bus_freq_hz, clk_get_rate(priv->clk), round, cdf, scgd); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 268 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 269 | /* keep icccr value */ |
Guennadi Liakhovetski | 14d32f1 | 2013-09-12 14:36:44 +0200 | [diff] [blame] | 270 | priv->icccr = scgd << cdf_width | cdf; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Sergei Shtylyov | 7c7117f | 2014-09-15 00:15:46 +0400 | [diff] [blame] | 275 | static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 276 | { |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 277 | int read = !!rcar_i2c_is_recv(priv); |
| 278 | |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 279 | priv->pos = 0; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 280 | if (priv->msgs_left == 1) |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 281 | priv->flags |= ID_LAST_MSG; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 282 | |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 283 | rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read); |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 284 | /* |
| 285 | * We don't have a testcase but the HW engineers say that the write order |
| 286 | * of ICMSR and ICMCR depends on whether we issue START or REP_START. Since |
| 287 | * it didn't cause a drawback for me, let's rather be safe than sorry. |
| 288 | */ |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 289 | if (priv->flags & ID_FIRST_MSG) { |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 290 | rcar_i2c_write(priv, ICMSR, 0); |
| 291 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); |
| 292 | } else { |
| 293 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); |
| 294 | rcar_i2c_write(priv, ICMSR, 0); |
| 295 | } |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 296 | rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 299 | static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv) |
| 300 | { |
| 301 | priv->msg++; |
| 302 | priv->msgs_left--; |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 303 | priv->flags &= ID_P_MASK; |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 304 | rcar_i2c_prepare_msg(priv); |
| 305 | } |
| 306 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 307 | /* |
| 308 | * interrupt functions |
| 309 | */ |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 310 | static void rcar_i2c_dma_unmap(struct rcar_i2c_priv *priv) |
| 311 | { |
| 312 | struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE |
| 313 | ? priv->dma_rx : priv->dma_tx; |
| 314 | |
| 315 | /* Disable DMA Master Received/Transmitted */ |
| 316 | rcar_i2c_write(priv, ICDMAER, 0); |
| 317 | |
| 318 | /* Reset default delay */ |
| 319 | rcar_i2c_write(priv, ICFBSCR, TCYC06); |
| 320 | |
| 321 | dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg), |
| 322 | priv->msg->len, priv->dma_direction); |
| 323 | |
| 324 | priv->dma_direction = DMA_NONE; |
| 325 | } |
| 326 | |
| 327 | static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv) |
| 328 | { |
| 329 | if (priv->dma_direction == DMA_NONE) |
| 330 | return; |
| 331 | else if (priv->dma_direction == DMA_FROM_DEVICE) |
| 332 | dmaengine_terminate_all(priv->dma_rx); |
| 333 | else if (priv->dma_direction == DMA_TO_DEVICE) |
| 334 | dmaengine_terminate_all(priv->dma_tx); |
| 335 | |
| 336 | rcar_i2c_dma_unmap(priv); |
| 337 | } |
| 338 | |
| 339 | static void rcar_i2c_dma_callback(void *data) |
| 340 | { |
| 341 | struct rcar_i2c_priv *priv = data; |
| 342 | |
| 343 | priv->pos += sg_dma_len(&priv->sg); |
| 344 | |
| 345 | rcar_i2c_dma_unmap(priv); |
| 346 | } |
| 347 | |
| 348 | static void rcar_i2c_dma(struct rcar_i2c_priv *priv) |
| 349 | { |
| 350 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
| 351 | struct i2c_msg *msg = priv->msg; |
| 352 | bool read = msg->flags & I2C_M_RD; |
| 353 | enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 354 | struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx; |
| 355 | struct dma_async_tx_descriptor *txdesc; |
| 356 | dma_addr_t dma_addr; |
| 357 | dma_cookie_t cookie; |
| 358 | unsigned char *buf; |
| 359 | int len; |
| 360 | |
| 361 | /* Do not use DMA if it's not available or for messages < 8 bytes */ |
| 362 | if (IS_ERR(chan) || msg->len < 8) |
| 363 | return; |
| 364 | |
| 365 | if (read) { |
| 366 | /* |
| 367 | * The last two bytes needs to be fetched using PIO in |
| 368 | * order for the STOP phase to work. |
| 369 | */ |
| 370 | buf = priv->msg->buf; |
| 371 | len = priv->msg->len - 2; |
| 372 | } else { |
| 373 | /* |
| 374 | * First byte in message was sent using PIO. |
| 375 | */ |
| 376 | buf = priv->msg->buf + 1; |
| 377 | len = priv->msg->len - 1; |
| 378 | } |
| 379 | |
| 380 | dma_addr = dma_map_single(chan->device->dev, buf, len, dir); |
Wolfram Sang | c13c291 | 2016-08-24 11:19:29 +0200 | [diff] [blame] | 381 | if (dma_mapping_error(chan->device->dev, dma_addr)) { |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 382 | dev_dbg(dev, "dma map failed, using PIO\n"); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | sg_dma_len(&priv->sg) = len; |
| 387 | sg_dma_address(&priv->sg) = dma_addr; |
| 388 | |
| 389 | priv->dma_direction = dir; |
| 390 | |
| 391 | txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1, |
| 392 | read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV, |
| 393 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 394 | if (!txdesc) { |
| 395 | dev_dbg(dev, "dma prep slave sg failed, using PIO\n"); |
| 396 | rcar_i2c_cleanup_dma(priv); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | txdesc->callback = rcar_i2c_dma_callback; |
| 401 | txdesc->callback_param = priv; |
| 402 | |
| 403 | cookie = dmaengine_submit(txdesc); |
| 404 | if (dma_submit_error(cookie)) { |
| 405 | dev_dbg(dev, "submitting dma failed, using PIO\n"); |
| 406 | rcar_i2c_cleanup_dma(priv); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* Set delay for DMA operations */ |
| 411 | rcar_i2c_write(priv, ICFBSCR, TCYC17); |
| 412 | |
| 413 | /* Enable DMA Master Received/Transmitted */ |
| 414 | if (read) |
| 415 | rcar_i2c_write(priv, ICDMAER, RMDMAE); |
| 416 | else |
| 417 | rcar_i2c_write(priv, ICDMAER, TMDMAE); |
| 418 | |
| 419 | dma_async_issue_pending(chan); |
| 420 | } |
| 421 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 422 | static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 423 | { |
| 424 | struct i2c_msg *msg = priv->msg; |
| 425 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 426 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 427 | if (!(msr & MDE)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 428 | return; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 429 | |
| 430 | if (priv->pos < msg->len) { |
| 431 | /* |
| 432 | * Prepare next data to ICRXTX register. |
| 433 | * This data will go to _SHIFT_ register. |
| 434 | * |
| 435 | * * |
| 436 | * [ICRXTX] -> [SHIFT] -> [I2C bus] |
| 437 | */ |
| 438 | rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]); |
| 439 | priv->pos++; |
| 440 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 441 | /* |
| 442 | * Try to use DMA to transmit the rest of the data if |
| 443 | * address transfer pashe just finished. |
| 444 | */ |
| 445 | if (msr & MAT) |
| 446 | rcar_i2c_dma(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 447 | } else { |
| 448 | /* |
| 449 | * The last data was pushed to ICRXTX on _PREV_ empty irq. |
| 450 | * It is on _SHIFT_ register, and will sent to I2C bus. |
| 451 | * |
| 452 | * * |
| 453 | * [ICRXTX] -> [SHIFT] -> [I2C bus] |
| 454 | */ |
| 455 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 456 | if (priv->flags & ID_LAST_MSG) { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 457 | /* |
| 458 | * If current msg is the _LAST_ msg, |
| 459 | * prepare stop condition here. |
| 460 | * ID_DONE will be set on STOP irq. |
| 461 | */ |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 462 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 463 | } else { |
| 464 | rcar_i2c_next_msg(priv); |
| 465 | return; |
| 466 | } |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 469 | rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 472 | static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 473 | { |
| 474 | struct i2c_msg *msg = priv->msg; |
| 475 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 476 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 477 | if (!(msr & MDR)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 478 | return; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 479 | |
| 480 | if (msr & MAT) { |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 481 | /* |
| 482 | * Address transfer phase finished, but no data at this point. |
| 483 | * Try to use DMA to receive data. |
| 484 | */ |
| 485 | rcar_i2c_dma(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 486 | } else if (priv->pos < msg->len) { |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 487 | /* get received data */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 488 | msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX); |
| 489 | priv->pos++; |
| 490 | } |
| 491 | |
| 492 | /* |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame] | 493 | * If next received data is the _LAST_, go to STOP phase. Might be |
| 494 | * overwritten by REP START when setting up a new msg. Not elegant |
| 495 | * but the only stable sequence for REP START I have found so far. |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 496 | */ |
| 497 | if (priv->pos + 1 >= msg->len) |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 498 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 499 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 500 | if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG)) |
| 501 | rcar_i2c_next_msg(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 502 | else |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 503 | rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 506 | static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv) |
| 507 | { |
| 508 | u32 ssr_raw, ssr_filtered; |
| 509 | u8 value; |
| 510 | |
| 511 | ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff; |
| 512 | ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER); |
| 513 | |
| 514 | if (!ssr_filtered) |
| 515 | return false; |
| 516 | |
| 517 | /* address detected */ |
| 518 | if (ssr_filtered & SAR) { |
| 519 | /* read or write request */ |
| 520 | if (ssr_raw & STM) { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 521 | i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 522 | rcar_i2c_write(priv, ICRXTX, value); |
| 523 | rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR); |
| 524 | } else { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 525 | i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 526 | rcar_i2c_read(priv, ICRXTX); /* dummy read */ |
| 527 | rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR); |
| 528 | } |
| 529 | |
| 530 | rcar_i2c_write(priv, ICSSR, ~SAR & 0xff); |
| 531 | } |
| 532 | |
| 533 | /* master sent stop */ |
| 534 | if (ssr_filtered & SSR) { |
| 535 | i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value); |
| 536 | rcar_i2c_write(priv, ICSIER, SAR | SSR); |
| 537 | rcar_i2c_write(priv, ICSSR, ~SSR & 0xff); |
| 538 | } |
| 539 | |
| 540 | /* master wants to write to us */ |
| 541 | if (ssr_filtered & SDR) { |
| 542 | int ret; |
| 543 | |
| 544 | value = rcar_i2c_read(priv, ICRXTX); |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 545 | ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 546 | /* Send NACK in case of error */ |
| 547 | rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 548 | rcar_i2c_write(priv, ICSSR, ~SDR & 0xff); |
| 549 | } |
| 550 | |
| 551 | /* master wants to read from us */ |
| 552 | if (ssr_filtered & SDE) { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 553 | i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 554 | rcar_i2c_write(priv, ICRXTX, value); |
| 555 | rcar_i2c_write(priv, ICSSR, ~SDE & 0xff); |
| 556 | } |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 561 | static irqreturn_t rcar_i2c_irq(int irq, void *ptr) |
| 562 | { |
| 563 | struct rcar_i2c_priv *priv = ptr; |
Wolfram Sang | 52df445 | 2015-11-19 16:56:49 +0100 | [diff] [blame] | 564 | u32 msr, val; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 565 | |
Wolfram Sang | 52df445 | 2015-11-19 16:56:49 +0100 | [diff] [blame] | 566 | /* Clear START or STOP as soon as we can */ |
| 567 | val = rcar_i2c_read(priv, ICMCR); |
| 568 | rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 569 | |
Wolfram Sang | 1c176d5 | 2014-05-28 09:44:36 +0200 | [diff] [blame] | 570 | msr = rcar_i2c_read(priv, ICMSR); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 571 | |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 572 | /* Only handle interrupts that are currently enabled */ |
| 573 | msr &= rcar_i2c_read(priv, ICMIER); |
Sergei Shtylyov | aa5beaf | 2014-09-15 00:20:19 +0400 | [diff] [blame] | 574 | if (!msr) { |
Wolfram Sang | c3be0af1 | 2015-11-19 16:56:48 +0100 | [diff] [blame] | 575 | if (rcar_i2c_slave_irq(priv)) |
| 576 | return IRQ_HANDLED; |
| 577 | |
| 578 | return IRQ_NONE; |
Sergei Shtylyov | aa5beaf | 2014-09-15 00:20:19 +0400 | [diff] [blame] | 579 | } |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 580 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 581 | /* Arbitration lost */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 582 | if (msr & MAL) { |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 583 | priv->flags |= ID_DONE | ID_ARBLOST; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 584 | goto out; |
| 585 | } |
| 586 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 587 | /* Nack */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 588 | if (msr & MNR) { |
Wolfram Sang | d89667b | 2015-11-19 16:56:47 +0100 | [diff] [blame] | 589 | /* HW automatically sends STOP after received NACK */ |
Wolfram Sang | f238224 | 2014-05-28 09:44:39 +0200 | [diff] [blame] | 590 | rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP); |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 591 | priv->flags |= ID_NACK; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 592 | goto out; |
| 593 | } |
| 594 | |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 595 | /* Stop */ |
| 596 | if (msr & MST) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 597 | priv->msgs_left--; /* The last message also made it */ |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 598 | priv->flags |= ID_DONE; |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 599 | goto out; |
| 600 | } |
| 601 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 602 | if (rcar_i2c_is_recv(priv)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 603 | rcar_i2c_irq_recv(priv, msr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 604 | else |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 605 | rcar_i2c_irq_send(priv, msr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 606 | |
| 607 | out: |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 608 | if (priv->flags & ID_DONE) { |
Wolfram Sang | f238224 | 2014-05-28 09:44:39 +0200 | [diff] [blame] | 609 | rcar_i2c_write(priv, ICMIER, 0); |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 610 | rcar_i2c_write(priv, ICMSR, 0); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 611 | wake_up(&priv->wait); |
| 612 | } |
| 613 | |
Wolfram Sang | c3be0af1 | 2015-11-19 16:56:48 +0100 | [diff] [blame] | 614 | return IRQ_HANDLED; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 617 | static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev, |
| 618 | enum dma_transfer_direction dir, |
| 619 | dma_addr_t port_addr) |
| 620 | { |
| 621 | struct dma_chan *chan; |
| 622 | struct dma_slave_config cfg; |
| 623 | char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx"; |
| 624 | int ret; |
| 625 | |
Niklas Söderlund | 6aabf9d | 2016-05-19 10:29:17 +0200 | [diff] [blame] | 626 | chan = dma_request_chan(dev, chan_name); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 627 | if (IS_ERR(chan)) { |
| 628 | ret = PTR_ERR(chan); |
| 629 | dev_dbg(dev, "request_channel failed for %s (%d)\n", |
| 630 | chan_name, ret); |
| 631 | return chan; |
| 632 | } |
| 633 | |
| 634 | memset(&cfg, 0, sizeof(cfg)); |
| 635 | cfg.direction = dir; |
| 636 | if (dir == DMA_MEM_TO_DEV) { |
| 637 | cfg.dst_addr = port_addr; |
| 638 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 639 | } else { |
| 640 | cfg.src_addr = port_addr; |
| 641 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 642 | } |
| 643 | |
| 644 | ret = dmaengine_slave_config(chan, &cfg); |
| 645 | if (ret) { |
| 646 | dev_dbg(dev, "slave_config failed for %s (%d)\n", |
| 647 | chan_name, ret); |
| 648 | dma_release_channel(chan); |
| 649 | return ERR_PTR(ret); |
| 650 | } |
| 651 | |
| 652 | dev_dbg(dev, "got DMA channel for %s\n", chan_name); |
| 653 | return chan; |
| 654 | } |
| 655 | |
| 656 | static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv, |
| 657 | struct i2c_msg *msg) |
| 658 | { |
| 659 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
| 660 | bool read; |
| 661 | struct dma_chan *chan; |
| 662 | enum dma_transfer_direction dir; |
| 663 | |
| 664 | read = msg->flags & I2C_M_RD; |
| 665 | |
| 666 | chan = read ? priv->dma_rx : priv->dma_tx; |
| 667 | if (PTR_ERR(chan) != -EPROBE_DEFER) |
| 668 | return; |
| 669 | |
| 670 | dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; |
| 671 | chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX); |
| 672 | |
| 673 | if (read) |
| 674 | priv->dma_rx = chan; |
| 675 | else |
| 676 | priv->dma_tx = chan; |
| 677 | } |
| 678 | |
| 679 | static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv) |
| 680 | { |
| 681 | if (!IS_ERR(priv->dma_tx)) { |
| 682 | dma_release_channel(priv->dma_tx); |
| 683 | priv->dma_tx = ERR_PTR(-EPROBE_DEFER); |
| 684 | } |
| 685 | |
| 686 | if (!IS_ERR(priv->dma_rx)) { |
| 687 | dma_release_channel(priv->dma_rx); |
| 688 | priv->dma_rx = ERR_PTR(-EPROBE_DEFER); |
| 689 | } |
| 690 | } |
| 691 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 692 | static int rcar_i2c_master_xfer(struct i2c_adapter *adap, |
| 693 | struct i2c_msg *msgs, |
| 694 | int num) |
| 695 | { |
| 696 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 697 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
Wolfram Sang | b6763d0 | 2015-06-20 21:03:20 +0200 | [diff] [blame] | 698 | int i, ret; |
Wolfram Sang | ff2316b | 2015-11-19 16:56:44 +0100 | [diff] [blame] | 699 | long time_left; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 700 | |
| 701 | pm_runtime_get_sync(dev); |
| 702 | |
Wolfram Sang | ae481cc | 2017-04-18 20:38:35 +0200 | [diff] [blame] | 703 | rcar_i2c_init(priv); |
| 704 | |
Wolfram Sang | 3f7de22 | 2014-05-28 09:44:40 +0200 | [diff] [blame] | 705 | ret = rcar_i2c_bus_barrier(priv); |
| 706 | if (ret < 0) |
| 707 | goto out; |
| 708 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 709 | for (i = 0; i < num; i++) { |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 710 | /* This HW can't send STOP after address phase */ |
| 711 | if (msgs[i].len == 0) { |
| 712 | ret = -EOPNOTSUPP; |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 713 | goto out; |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 714 | } |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 715 | rcar_i2c_request_dma(priv, msgs + i); |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 716 | } |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 717 | |
Wolfram Sang | e49865d | 2015-11-19 16:56:51 +0100 | [diff] [blame] | 718 | /* init first message */ |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 719 | priv->msg = msgs; |
| 720 | priv->msgs_left = num; |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 721 | priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG; |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 722 | rcar_i2c_prepare_msg(priv); |
Sergei Shtylyov | 91bfe29 | 2014-08-24 00:44:09 +0400 | [diff] [blame] | 723 | |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 724 | time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE, |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 725 | num * adap->timeout); |
| 726 | if (!time_left) { |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 727 | rcar_i2c_cleanup_dma(priv); |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 728 | rcar_i2c_init(priv); |
| 729 | ret = -ETIMEDOUT; |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 730 | } else if (priv->flags & ID_NACK) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 731 | ret = -ENXIO; |
Wolfram Sang | 42c0783 | 2015-12-23 17:56:33 +0100 | [diff] [blame] | 732 | } else if (priv->flags & ID_ARBLOST) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 733 | ret = -EAGAIN; |
| 734 | } else { |
| 735 | ret = num - priv->msgs_left; /* The number of transfer */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 736 | } |
Wolfram Sang | 3f7de22 | 2014-05-28 09:44:40 +0200 | [diff] [blame] | 737 | out: |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 738 | pm_runtime_put(dev); |
| 739 | |
Ben Dooks | 6ff4b105 | 2014-01-26 16:05:37 +0000 | [diff] [blame] | 740 | if (ret < 0 && ret != -ENXIO) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 741 | dev_err(dev, "error %d : %x\n", ret, priv->flags); |
| 742 | |
| 743 | return ret; |
| 744 | } |
| 745 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 746 | static int rcar_reg_slave(struct i2c_client *slave) |
| 747 | { |
| 748 | struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); |
| 749 | |
| 750 | if (priv->slave) |
| 751 | return -EBUSY; |
| 752 | |
| 753 | if (slave->flags & I2C_CLIENT_TEN) |
| 754 | return -EAFNOSUPPORT; |
| 755 | |
Wolfram Sang | 63a761e | 2017-04-20 12:04:33 +0200 | [diff] [blame] | 756 | /* Keep device active for slave address detection logic */ |
Wolfram Sang | b4cd08a | 2015-12-16 20:05:18 +0100 | [diff] [blame] | 757 | pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 758 | |
| 759 | priv->slave = slave; |
| 760 | rcar_i2c_write(priv, ICSAR, slave->addr); |
| 761 | rcar_i2c_write(priv, ICSSR, 0); |
| 762 | rcar_i2c_write(priv, ICSIER, SAR | SSR); |
| 763 | rcar_i2c_write(priv, ICSCR, SIE | SDBS); |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | static int rcar_unreg_slave(struct i2c_client *slave) |
| 769 | { |
| 770 | struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); |
| 771 | |
| 772 | WARN_ON(!priv->slave); |
| 773 | |
| 774 | rcar_i2c_write(priv, ICSIER, 0); |
| 775 | rcar_i2c_write(priv, ICSCR, 0); |
| 776 | |
| 777 | priv->slave = NULL; |
| 778 | |
Wolfram Sang | b4cd08a | 2015-12-16 20:05:18 +0100 | [diff] [blame] | 779 | pm_runtime_put(rcar_i2c_priv_to_dev(priv)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 784 | static u32 rcar_i2c_func(struct i2c_adapter *adap) |
| 785 | { |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 786 | /* This HW can't do SMBUS_QUICK and NOSTART */ |
Wolfram Sang | 1fb2ad9 | 2015-05-14 14:40:03 +0200 | [diff] [blame] | 787 | return I2C_FUNC_I2C | I2C_FUNC_SLAVE | |
| 788 | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static const struct i2c_algorithm rcar_i2c_algo = { |
| 792 | .master_xfer = rcar_i2c_master_xfer, |
| 793 | .functionality = rcar_i2c_func, |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 794 | .reg_slave = rcar_reg_slave, |
| 795 | .unreg_slave = rcar_unreg_slave, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 796 | }; |
| 797 | |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 798 | static const struct of_device_id rcar_i2c_dt_ids[] = { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 799 | { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 }, |
| 800 | { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 }, |
| 801 | { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | e893645 | 2014-02-20 09:03:20 +0100 | [diff] [blame] | 802 | { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | 819a395 | 2014-05-27 14:06:28 +0200 | [diff] [blame] | 803 | { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 }, |
| 804 | { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 }, |
| 805 | { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 806 | { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 }, |
Ulrich Hecht | c13f743 | 2016-09-14 18:46:06 +0200 | [diff] [blame] | 807 | { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 }, |
Simon Horman | ad4a8dc | 2016-12-06 17:01:28 +0100 | [diff] [blame] | 808 | { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 }, /* Deprecated */ |
| 809 | { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 }, |
| 810 | { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 }, |
| 811 | { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 }, |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 812 | {}, |
| 813 | }; |
| 814 | MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids); |
| 815 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 816 | static int rcar_i2c_probe(struct platform_device *pdev) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 817 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 818 | struct rcar_i2c_priv *priv; |
| 819 | struct i2c_adapter *adap; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 820 | struct device *dev = &pdev->dev; |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 821 | struct i2c_timings i2c_t; |
Wolfram Sang | 93e953d | 2014-05-28 09:44:37 +0200 | [diff] [blame] | 822 | int irq, ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 823 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 824 | priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 825 | if (!priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 826 | return -ENOMEM; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 827 | |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 828 | priv->clk = devm_clk_get(dev, NULL); |
| 829 | if (IS_ERR(priv->clk)) { |
| 830 | dev_err(dev, "cannot get clock\n"); |
| 831 | return PTR_ERR(priv->clk); |
| 832 | } |
| 833 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 834 | priv->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 835 | |
| 836 | priv->io = devm_ioremap_resource(dev, priv->res); |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 837 | if (IS_ERR(priv->io)) |
| 838 | return PTR_ERR(priv->io); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 839 | |
Wolfram Sang | 69e558f | 2016-03-01 17:36:43 +0100 | [diff] [blame] | 840 | priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 841 | init_waitqueue_head(&priv->wait); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 842 | |
Wolfram Sang | 929e3aba | 2014-07-10 13:46:31 +0200 | [diff] [blame] | 843 | adap = &priv->adap; |
| 844 | adap->nr = pdev->id; |
| 845 | adap->algo = &rcar_i2c_algo; |
| 846 | adap->class = I2C_CLASS_DEPRECATED; |
| 847 | adap->retries = 3; |
| 848 | adap->dev.parent = dev; |
| 849 | adap->dev.of_node = dev->of_node; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 850 | i2c_set_adapdata(adap, priv); |
| 851 | strlcpy(adap->name, pdev->name, sizeof(adap->name)); |
| 852 | |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 853 | i2c_parse_fw_timings(dev, &i2c_t, false); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 854 | |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 855 | /* Init DMA */ |
| 856 | sg_init_table(&priv->sg, 1); |
| 857 | priv->dma_direction = DMA_NONE; |
| 858 | priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER); |
| 859 | |
Wolfram Sang | 63a761e | 2017-04-20 12:04:33 +0200 | [diff] [blame] | 860 | /* Activate device for clock calculation */ |
Wolfram Sang | 4f7effd | 2015-10-09 10:39:25 +0100 | [diff] [blame] | 861 | pm_runtime_enable(dev); |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 862 | pm_runtime_get_sync(dev); |
Wolfram Sang | c788187 | 2015-12-08 10:37:48 +0100 | [diff] [blame] | 863 | ret = rcar_i2c_clock_calculate(priv, &i2c_t); |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 864 | if (ret < 0) |
| 865 | goto out_pm_put; |
| 866 | |
Wolfram Sang | 63a761e | 2017-04-20 12:04:33 +0200 | [diff] [blame] | 867 | /* Stay always active when multi-master to keep arbitration working */ |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 868 | if (of_property_read_bool(dev->of_node, "multi-master")) |
| 869 | priv->flags |= ID_P_PM_BLOCKED; |
| 870 | else |
| 871 | pm_runtime_put(dev); |
| 872 | |
Wolfram Sang | f9c9d31 | 2015-12-08 10:37:47 +0100 | [diff] [blame] | 873 | |
| 874 | irq = platform_get_irq(pdev, 0); |
| 875 | ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0, dev_name(dev), priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 876 | if (ret < 0) { |
| 877 | dev_err(dev, "cannot get irq %d\n", irq); |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 878 | goto out_pm_disable; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Wolfram Sang | 4f7effd | 2015-10-09 10:39:25 +0100 | [diff] [blame] | 881 | platform_set_drvdata(pdev, priv); |
| 882 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 883 | ret = i2c_add_numbered_adapter(adap); |
Wolfram Sang | ea73440 | 2016-08-09 13:36:17 +0200 | [diff] [blame] | 884 | if (ret < 0) |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 885 | goto out_pm_disable; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 886 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 887 | dev_info(dev, "probed\n"); |
| 888 | |
| 889 | return 0; |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 890 | |
| 891 | out_pm_put: |
| 892 | pm_runtime_put(dev); |
| 893 | out_pm_disable: |
| 894 | pm_runtime_disable(dev); |
| 895 | return ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 898 | static int rcar_i2c_remove(struct platform_device *pdev) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 899 | { |
| 900 | struct rcar_i2c_priv *priv = platform_get_drvdata(pdev); |
| 901 | struct device *dev = &pdev->dev; |
| 902 | |
| 903 | i2c_del_adapter(&priv->adap); |
Niklas Söderlund | 73e8b05 | 2016-05-14 14:17:08 +0200 | [diff] [blame] | 904 | rcar_i2c_release_dma(priv); |
Wolfram Sang | 7ee24eb | 2015-12-23 17:56:34 +0100 | [diff] [blame] | 905 | if (priv->flags & ID_P_PM_BLOCKED) |
| 906 | pm_runtime_put(dev); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 907 | pm_runtime_disable(dev); |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
Wolfram Sang | 45fd5e4 | 2012-11-13 11:24:15 +0100 | [diff] [blame] | 912 | static struct platform_driver rcar_i2c_driver = { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 913 | .driver = { |
| 914 | .name = "i2c-rcar", |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 915 | .of_match_table = rcar_i2c_dt_ids, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 916 | }, |
| 917 | .probe = rcar_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 918 | .remove = rcar_i2c_remove, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 919 | }; |
| 920 | |
Wolfram Sang | 45fd5e4 | 2012-11-13 11:24:15 +0100 | [diff] [blame] | 921 | module_platform_driver(rcar_i2c_driver); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 922 | |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 923 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 924 | MODULE_DESCRIPTION("Renesas R-Car I2C bus driver"); |
| 925 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |