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> |
| 24 | #include <linux/err.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/i2c.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 30 | #include <linux/of_device.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/pm_runtime.h> |
| 33 | #include <linux/slab.h> |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 34 | |
| 35 | /* register offsets */ |
| 36 | #define ICSCR 0x00 /* slave ctrl */ |
| 37 | #define ICMCR 0x04 /* master ctrl */ |
| 38 | #define ICSSR 0x08 /* slave status */ |
| 39 | #define ICMSR 0x0C /* master status */ |
| 40 | #define ICSIER 0x10 /* slave irq enable */ |
| 41 | #define ICMIER 0x14 /* master irq enable */ |
| 42 | #define ICCCR 0x18 /* clock dividers */ |
| 43 | #define ICSAR 0x1C /* slave address */ |
| 44 | #define ICMAR 0x20 /* master address */ |
| 45 | #define ICRXTX 0x24 /* data port */ |
| 46 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 47 | /* ICSCR */ |
| 48 | #define SDBS (1 << 3) /* slave data buffer select */ |
| 49 | #define SIE (1 << 2) /* slave interface enable */ |
| 50 | #define GCAE (1 << 1) /* general call address enable */ |
| 51 | #define FNA (1 << 0) /* forced non acknowledgment */ |
| 52 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 53 | /* ICMCR */ |
| 54 | #define MDBS (1 << 7) /* non-fifo mode switch */ |
| 55 | #define FSCL (1 << 6) /* override SCL pin */ |
| 56 | #define FSDA (1 << 5) /* override SDA pin */ |
| 57 | #define OBPC (1 << 4) /* override pins */ |
| 58 | #define MIE (1 << 3) /* master if enable */ |
| 59 | #define TSBE (1 << 2) |
| 60 | #define FSB (1 << 1) /* force stop bit */ |
| 61 | #define ESG (1 << 0) /* en startbit gen */ |
| 62 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 63 | /* ICSSR (also for ICSIER) */ |
| 64 | #define GCAR (1 << 6) /* general call received */ |
| 65 | #define STM (1 << 5) /* slave transmit mode */ |
| 66 | #define SSR (1 << 4) /* stop received */ |
| 67 | #define SDE (1 << 3) /* slave data empty */ |
| 68 | #define SDT (1 << 2) /* slave data transmitted */ |
| 69 | #define SDR (1 << 1) /* slave data received */ |
| 70 | #define SAR (1 << 0) /* slave addr received */ |
| 71 | |
Wolfram Sang | 3e3aaba | 2014-05-28 09:44:44 +0200 | [diff] [blame] | 72 | /* ICMSR (also for ICMIE) */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 73 | #define MNR (1 << 6) /* nack received */ |
| 74 | #define MAL (1 << 5) /* arbitration lost */ |
| 75 | #define MST (1 << 4) /* sent a stop */ |
| 76 | #define MDE (1 << 3) |
| 77 | #define MDT (1 << 2) |
| 78 | #define MDR (1 << 1) |
| 79 | #define MAT (1 << 0) /* slave addr xfer done */ |
| 80 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 81 | |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 82 | #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG) |
| 83 | #define RCAR_BUS_PHASE_DATA (MDBS | MIE) |
Wolfram Sang | 52df445 | 2015-11-19 16:56:49 +0100 | [diff] [blame] | 84 | #define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF) |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 85 | #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 86 | |
Wolfram Sang | 3e3aaba | 2014-05-28 09:44:44 +0200 | [diff] [blame] | 87 | #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE) |
| 88 | #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR) |
| 89 | #define RCAR_IRQ_STOP (MST) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 90 | |
Sergei Shtylyov | 938916f | 2014-09-06 03:34:32 +0400 | [diff] [blame] | 91 | #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF) |
| 92 | #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF) |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 93 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 94 | #define ID_LAST_MSG (1 << 0) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 95 | #define ID_DONE (1 << 2) |
| 96 | #define ID_ARBLOST (1 << 3) |
| 97 | #define ID_NACK (1 << 4) |
| 98 | |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 99 | enum rcar_i2c_type { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 100 | I2C_RCAR_GEN1, |
| 101 | I2C_RCAR_GEN2, |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 102 | I2C_RCAR_GEN3, |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 103 | }; |
| 104 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 105 | struct rcar_i2c_priv { |
| 106 | void __iomem *io; |
| 107 | struct i2c_adapter adap; |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 108 | struct i2c_msg *msg; |
| 109 | int msgs_left; |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 110 | struct clk *clk; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 111 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 112 | wait_queue_head_t wait; |
| 113 | |
| 114 | int pos; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 115 | u32 icccr; |
| 116 | u32 flags; |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 117 | enum rcar_i2c_type devtype; |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 118 | struct i2c_client *slave; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent) |
| 122 | #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD) |
| 123 | |
| 124 | #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f)) |
| 125 | #define rcar_i2c_flags_has(p, f) ((p)->flags & (f)) |
| 126 | |
| 127 | #define LOOP_TIMEOUT 1024 |
| 128 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 129 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 130 | static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val) |
| 131 | { |
| 132 | writel(val, priv->io + reg); |
| 133 | } |
| 134 | |
| 135 | static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg) |
| 136 | { |
| 137 | return readl(priv->io + reg); |
| 138 | } |
| 139 | |
| 140 | static void rcar_i2c_init(struct rcar_i2c_priv *priv) |
| 141 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 142 | /* reset master mode */ |
| 143 | rcar_i2c_write(priv, ICMIER, 0); |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 144 | rcar_i2c_write(priv, ICMCR, MDBS); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 145 | rcar_i2c_write(priv, ICMSR, 0); |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 146 | /* start clock */ |
| 147 | rcar_i2c_write(priv, ICCCR, priv->icccr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 150 | static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv) |
| 151 | { |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0; i < LOOP_TIMEOUT; i++) { |
| 155 | /* make sure that bus is not busy */ |
| 156 | if (!(rcar_i2c_read(priv, ICMCR) & FSDA)) |
| 157 | return 0; |
| 158 | udelay(1); |
| 159 | } |
| 160 | |
| 161 | return -EBUSY; |
| 162 | } |
| 163 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 164 | static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, |
| 165 | u32 bus_speed, |
| 166 | struct device *dev) |
| 167 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 168 | u32 scgd, cdf; |
| 169 | u32 round, ick; |
| 170 | u32 scl; |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 171 | u32 cdf_width; |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 172 | unsigned long rate; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 173 | |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 174 | switch (priv->devtype) { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 175 | case I2C_RCAR_GEN1: |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 176 | cdf_width = 2; |
| 177 | break; |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 178 | case I2C_RCAR_GEN2: |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 179 | case I2C_RCAR_GEN3: |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 180 | cdf_width = 3; |
| 181 | break; |
| 182 | default: |
| 183 | dev_err(dev, "device type error\n"); |
| 184 | return -EIO; |
| 185 | } |
| 186 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 187 | /* |
| 188 | * calculate SCL clock |
| 189 | * see |
| 190 | * ICCCR |
| 191 | * |
| 192 | * ick = clkp / (1 + CDF) |
| 193 | * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) |
| 194 | * |
| 195 | * ick : I2C internal clock < 20 MHz |
| 196 | * ticf : I2C SCL falling time = 35 ns here |
| 197 | * tr : I2C SCL rising time = 200 ns here |
| 198 | * intd : LSI internal delay = 50 ns here |
| 199 | * clkp : peripheral_clk |
| 200 | * F[] : integer up-valuation |
| 201 | */ |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 202 | rate = clk_get_rate(priv->clk); |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 203 | cdf = rate / 20000000; |
Wolfram Sang | 22762cc | 2014-09-20 12:07:37 +0200 | [diff] [blame] | 204 | if (cdf >= 1U << cdf_width) { |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 205 | dev_err(dev, "Input clock %lu too high\n", rate); |
| 206 | return -EIO; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 207 | } |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 208 | ick = rate / (cdf + 1); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 209 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 210 | /* |
| 211 | * it is impossible to calculate large scale |
| 212 | * number on u32. separate it |
| 213 | * |
| 214 | * F[(ticf + tr + intd) * ick] |
| 215 | * = F[(35 + 200 + 50)ns * ick] |
| 216 | * = F[285 * ick / 1000000000] |
| 217 | * = F[(ick / 1000000) * 285 / 1000] |
| 218 | */ |
| 219 | round = (ick + 500000) / 1000000 * 285; |
| 220 | round = (round + 500) / 1000; |
| 221 | |
| 222 | /* |
| 223 | * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick]) |
| 224 | * |
| 225 | * Calculation result (= SCL) should be less than |
| 226 | * bus_speed for hardware safety |
Guennadi Liakhovetski | 8d04940 | 2013-09-12 14:36:45 +0200 | [diff] [blame] | 227 | * |
| 228 | * We could use something along the lines of |
| 229 | * div = ick / (bus_speed + 1) + 1; |
| 230 | * scgd = (div - 20 - round + 7) / 8; |
| 231 | * scl = ick / (20 + (scgd * 8) + round); |
| 232 | * (not fully verified) but that would get pretty involved |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 233 | */ |
| 234 | for (scgd = 0; scgd < 0x40; scgd++) { |
| 235 | scl = ick / (20 + (scgd * 8) + round); |
| 236 | if (scl <= bus_speed) |
| 237 | goto scgd_find; |
| 238 | } |
| 239 | dev_err(dev, "it is impossible to calculate best SCL\n"); |
| 240 | return -EIO; |
| 241 | |
| 242 | scgd_find: |
| 243 | dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n", |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 244 | scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 245 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame^] | 246 | /* keep icccr value */ |
Guennadi Liakhovetski | 14d32f1 | 2013-09-12 14:36:44 +0200 | [diff] [blame] | 247 | priv->icccr = scgd << cdf_width | cdf; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Sergei Shtylyov | 7c7117f | 2014-09-15 00:15:46 +0400 | [diff] [blame] | 252 | static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 253 | { |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 254 | int read = !!rcar_i2c_is_recv(priv); |
| 255 | |
Wolfram Sang | b9d0684c | 2015-11-19 16:56:45 +0100 | [diff] [blame] | 256 | priv->pos = 0; |
| 257 | priv->flags = 0; |
| 258 | if (priv->msgs_left == 1) |
| 259 | rcar_i2c_flags_set(priv, ID_LAST_MSG); |
| 260 | |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 261 | rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read); |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 262 | rcar_i2c_write(priv, ICMSR, 0); |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 263 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); |
Wolfram Sang | 386babf | 2014-05-28 09:44:41 +0200 | [diff] [blame] | 264 | rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 267 | static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv) |
| 268 | { |
| 269 | priv->msg++; |
| 270 | priv->msgs_left--; |
| 271 | rcar_i2c_prepare_msg(priv); |
| 272 | } |
| 273 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 274 | /* |
| 275 | * interrupt functions |
| 276 | */ |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 277 | 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] | 278 | { |
| 279 | struct i2c_msg *msg = priv->msg; |
| 280 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame^] | 281 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 282 | if (!(msr & MDE)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 283 | return; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 284 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 285 | if (priv->pos < msg->len) { |
| 286 | /* |
| 287 | * Prepare next data to ICRXTX register. |
| 288 | * This data will go to _SHIFT_ register. |
| 289 | * |
| 290 | * * |
| 291 | * [ICRXTX] -> [SHIFT] -> [I2C bus] |
| 292 | */ |
| 293 | rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]); |
| 294 | priv->pos++; |
| 295 | |
| 296 | } else { |
| 297 | /* |
| 298 | * The last data was pushed to ICRXTX on _PREV_ empty irq. |
| 299 | * It is on _SHIFT_ register, and will sent to I2C bus. |
| 300 | * |
| 301 | * * |
| 302 | * [ICRXTX] -> [SHIFT] -> [I2C bus] |
| 303 | */ |
| 304 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 305 | if (priv->flags & ID_LAST_MSG) { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 306 | /* |
| 307 | * If current msg is the _LAST_ msg, |
| 308 | * prepare stop condition here. |
| 309 | * ID_DONE will be set on STOP irq. |
| 310 | */ |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 311 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 312 | } else { |
| 313 | rcar_i2c_next_msg(priv); |
| 314 | return; |
| 315 | } |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 318 | rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 321 | 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] | 322 | { |
| 323 | struct i2c_msg *msg = priv->msg; |
| 324 | |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame^] | 325 | /* FIXME: sometimes, unknown interrupt happened. Do nothing */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 326 | if (!(msr & MDR)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 327 | return; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 328 | |
| 329 | if (msr & MAT) { |
Wolfram Sang | 52df445 | 2015-11-19 16:56:49 +0100 | [diff] [blame] | 330 | /* Address transfer phase finished, but no data at this point. */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 331 | } else if (priv->pos < msg->len) { |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame^] | 332 | /* get received data */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 333 | msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX); |
| 334 | priv->pos++; |
| 335 | } |
| 336 | |
| 337 | /* |
Wolfram Sang | 3c2b1ff | 2015-11-19 16:56:50 +0100 | [diff] [blame^] | 338 | * If next received data is the _LAST_, go to STOP phase. Might be |
| 339 | * overwritten by REP START when setting up a new msg. Not elegant |
| 340 | * 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] | 341 | */ |
| 342 | if (priv->pos + 1 >= msg->len) |
Wolfram Sang | 4f443a8 | 2014-05-28 09:44:38 +0200 | [diff] [blame] | 343 | rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 344 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 345 | if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG)) |
| 346 | rcar_i2c_next_msg(priv); |
| 347 | else |
| 348 | rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 351 | static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv) |
| 352 | { |
| 353 | u32 ssr_raw, ssr_filtered; |
| 354 | u8 value; |
| 355 | |
| 356 | ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff; |
| 357 | ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER); |
| 358 | |
| 359 | if (!ssr_filtered) |
| 360 | return false; |
| 361 | |
| 362 | /* address detected */ |
| 363 | if (ssr_filtered & SAR) { |
| 364 | /* read or write request */ |
| 365 | if (ssr_raw & STM) { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 366 | i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 367 | rcar_i2c_write(priv, ICRXTX, value); |
| 368 | rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR); |
| 369 | } else { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 370 | i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 371 | rcar_i2c_read(priv, ICRXTX); /* dummy read */ |
| 372 | rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR); |
| 373 | } |
| 374 | |
| 375 | rcar_i2c_write(priv, ICSSR, ~SAR & 0xff); |
| 376 | } |
| 377 | |
| 378 | /* master sent stop */ |
| 379 | if (ssr_filtered & SSR) { |
| 380 | i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value); |
| 381 | rcar_i2c_write(priv, ICSIER, SAR | SSR); |
| 382 | rcar_i2c_write(priv, ICSSR, ~SSR & 0xff); |
| 383 | } |
| 384 | |
| 385 | /* master wants to write to us */ |
| 386 | if (ssr_filtered & SDR) { |
| 387 | int ret; |
| 388 | |
| 389 | value = rcar_i2c_read(priv, ICRXTX); |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 390 | ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 391 | /* Send NACK in case of error */ |
| 392 | rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0)); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 393 | rcar_i2c_write(priv, ICSSR, ~SDR & 0xff); |
| 394 | } |
| 395 | |
| 396 | /* master wants to read from us */ |
| 397 | if (ssr_filtered & SDE) { |
Wolfram Sang | 5b77d16 | 2015-03-23 09:26:36 +0100 | [diff] [blame] | 398 | i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value); |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 399 | rcar_i2c_write(priv, ICRXTX, value); |
| 400 | rcar_i2c_write(priv, ICSSR, ~SDE & 0xff); |
| 401 | } |
| 402 | |
| 403 | return true; |
| 404 | } |
| 405 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 406 | static irqreturn_t rcar_i2c_irq(int irq, void *ptr) |
| 407 | { |
| 408 | struct rcar_i2c_priv *priv = ptr; |
Wolfram Sang | 52df445 | 2015-11-19 16:56:49 +0100 | [diff] [blame] | 409 | u32 msr, val; |
| 410 | |
| 411 | /* Clear START or STOP as soon as we can */ |
| 412 | val = rcar_i2c_read(priv, ICMCR); |
| 413 | rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 414 | |
Wolfram Sang | 1c176d5 | 2014-05-28 09:44:36 +0200 | [diff] [blame] | 415 | msr = rcar_i2c_read(priv, ICMSR); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 416 | |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 417 | /* Only handle interrupts that are currently enabled */ |
| 418 | msr &= rcar_i2c_read(priv, ICMIER); |
Sergei Shtylyov | aa5beaf | 2014-09-15 00:20:19 +0400 | [diff] [blame] | 419 | if (!msr) { |
Wolfram Sang | c3be0af1 | 2015-11-19 16:56:48 +0100 | [diff] [blame] | 420 | if (rcar_i2c_slave_irq(priv)) |
| 421 | return IRQ_HANDLED; |
| 422 | |
| 423 | return IRQ_NONE; |
Sergei Shtylyov | aa5beaf | 2014-09-15 00:20:19 +0400 | [diff] [blame] | 424 | } |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 425 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 426 | /* Arbitration lost */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 427 | if (msr & MAL) { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 428 | rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST)); |
| 429 | goto out; |
| 430 | } |
| 431 | |
Wolfram Sang | 51371cd | 2014-05-28 09:44:45 +0200 | [diff] [blame] | 432 | /* Nack */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 433 | if (msr & MNR) { |
Wolfram Sang | d89667b | 2015-11-19 16:56:47 +0100 | [diff] [blame] | 434 | /* HW automatically sends STOP after received NACK */ |
Wolfram Sang | f238224 | 2014-05-28 09:44:39 +0200 | [diff] [blame] | 435 | rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 436 | rcar_i2c_flags_set(priv, ID_NACK); |
| 437 | goto out; |
| 438 | } |
| 439 | |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 440 | /* Stop */ |
| 441 | if (msr & MST) { |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 442 | priv->msgs_left--; /* The last message also made it */ |
Sergei Shtylyov | dd318b0 | 2014-09-02 01:15:26 +0400 | [diff] [blame] | 443 | rcar_i2c_flags_set(priv, ID_DONE); |
| 444 | goto out; |
| 445 | } |
| 446 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 447 | if (rcar_i2c_is_recv(priv)) |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 448 | rcar_i2c_irq_recv(priv, msr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 449 | else |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 450 | rcar_i2c_irq_send(priv, msr); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 451 | |
| 452 | out: |
| 453 | if (rcar_i2c_flags_has(priv, ID_DONE)) { |
Wolfram Sang | f238224 | 2014-05-28 09:44:39 +0200 | [diff] [blame] | 454 | rcar_i2c_write(priv, ICMIER, 0); |
Wolfram Sang | 3c95de6 | 2014-05-28 09:44:42 +0200 | [diff] [blame] | 455 | rcar_i2c_write(priv, ICMSR, 0); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 456 | wake_up(&priv->wait); |
| 457 | } |
| 458 | |
Wolfram Sang | c3be0af1 | 2015-11-19 16:56:48 +0100 | [diff] [blame] | 459 | return IRQ_HANDLED; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int rcar_i2c_master_xfer(struct i2c_adapter *adap, |
| 463 | struct i2c_msg *msgs, |
| 464 | int num) |
| 465 | { |
| 466 | struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); |
| 467 | struct device *dev = rcar_i2c_priv_to_dev(priv); |
Wolfram Sang | b6763d0 | 2015-06-20 21:03:20 +0200 | [diff] [blame] | 468 | int i, ret; |
Wolfram Sang | ff2316b | 2015-11-19 16:56:44 +0100 | [diff] [blame] | 469 | long time_left; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 470 | |
| 471 | pm_runtime_get_sync(dev); |
| 472 | |
Wolfram Sang | 3f7de22 | 2014-05-28 09:44:40 +0200 | [diff] [blame] | 473 | ret = rcar_i2c_bus_barrier(priv); |
| 474 | if (ret < 0) |
| 475 | goto out; |
| 476 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 477 | for (i = 0; i < num; i++) { |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 478 | /* This HW can't send STOP after address phase */ |
| 479 | if (msgs[i].len == 0) { |
| 480 | ret = -EOPNOTSUPP; |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 481 | goto out; |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 482 | } |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 483 | } |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 484 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 485 | /* init data */ |
| 486 | priv->msg = msgs; |
| 487 | priv->msgs_left = num; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 488 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 489 | rcar_i2c_prepare_msg(priv); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 490 | |
Wolfram Sang | cc21d0b | 2015-11-19 16:56:46 +0100 | [diff] [blame] | 491 | time_left = wait_event_timeout(priv->wait, |
| 492 | rcar_i2c_flags_has(priv, ID_DONE), |
| 493 | num * adap->timeout); |
| 494 | if (!time_left) { |
| 495 | rcar_i2c_init(priv); |
| 496 | ret = -ETIMEDOUT; |
| 497 | } else if (rcar_i2c_flags_has(priv, ID_NACK)) { |
| 498 | ret = -ENXIO; |
| 499 | } else if (rcar_i2c_flags_has(priv, ID_ARBLOST)) { |
| 500 | ret = -EAGAIN; |
| 501 | } else { |
| 502 | ret = num - priv->msgs_left; /* The number of transfer */ |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 503 | } |
Wolfram Sang | 3f7de22 | 2014-05-28 09:44:40 +0200 | [diff] [blame] | 504 | out: |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 505 | pm_runtime_put(dev); |
| 506 | |
Ben Dooks | 6ff4b105 | 2014-01-26 16:05:37 +0000 | [diff] [blame] | 507 | if (ret < 0 && ret != -ENXIO) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 508 | dev_err(dev, "error %d : %x\n", ret, priv->flags); |
| 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 513 | static int rcar_reg_slave(struct i2c_client *slave) |
| 514 | { |
| 515 | struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); |
| 516 | |
| 517 | if (priv->slave) |
| 518 | return -EBUSY; |
| 519 | |
| 520 | if (slave->flags & I2C_CLIENT_TEN) |
| 521 | return -EAFNOSUPPORT; |
| 522 | |
| 523 | pm_runtime_forbid(rcar_i2c_priv_to_dev(priv)); |
| 524 | |
| 525 | priv->slave = slave; |
| 526 | rcar_i2c_write(priv, ICSAR, slave->addr); |
| 527 | rcar_i2c_write(priv, ICSSR, 0); |
| 528 | rcar_i2c_write(priv, ICSIER, SAR | SSR); |
| 529 | rcar_i2c_write(priv, ICSCR, SIE | SDBS); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static int rcar_unreg_slave(struct i2c_client *slave) |
| 535 | { |
| 536 | struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter); |
| 537 | |
| 538 | WARN_ON(!priv->slave); |
| 539 | |
| 540 | rcar_i2c_write(priv, ICSIER, 0); |
| 541 | rcar_i2c_write(priv, ICSCR, 0); |
| 542 | |
| 543 | priv->slave = NULL; |
| 544 | |
| 545 | pm_runtime_allow(rcar_i2c_priv_to_dev(priv)); |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 550 | static u32 rcar_i2c_func(struct i2c_adapter *adap) |
| 551 | { |
Wolfram Sang | d765396 | 2014-05-05 18:36:21 +0200 | [diff] [blame] | 552 | /* This HW can't do SMBUS_QUICK and NOSTART */ |
Wolfram Sang | 1fb2ad9 | 2015-05-14 14:40:03 +0200 | [diff] [blame] | 553 | return I2C_FUNC_I2C | I2C_FUNC_SLAVE | |
| 554 | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static const struct i2c_algorithm rcar_i2c_algo = { |
| 558 | .master_xfer = rcar_i2c_master_xfer, |
| 559 | .functionality = rcar_i2c_func, |
Wolfram Sang | de20d18 | 2014-11-18 17:04:55 +0100 | [diff] [blame] | 560 | .reg_slave = rcar_reg_slave, |
| 561 | .unreg_slave = rcar_unreg_slave, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 562 | }; |
| 563 | |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 564 | static const struct of_device_id rcar_i2c_dt_ids[] = { |
Kuninori Morimoto | 043a3f1 | 2013-10-21 01:04:32 -0700 | [diff] [blame] | 565 | { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 }, |
| 566 | { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 }, |
| 567 | { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 }, |
| 568 | { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | e893645 | 2014-02-20 09:03:20 +0100 | [diff] [blame] | 569 | { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | 819a395 | 2014-05-27 14:06:28 +0200 | [diff] [blame] | 570 | { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 }, |
| 571 | { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 }, |
| 572 | { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 }, |
Wolfram Sang | e7db0d3 | 2015-08-05 15:18:25 +0200 | [diff] [blame] | 573 | { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 }, |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 574 | {}, |
| 575 | }; |
| 576 | MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids); |
| 577 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 578 | static int rcar_i2c_probe(struct platform_device *pdev) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 579 | { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 580 | struct rcar_i2c_priv *priv; |
| 581 | struct i2c_adapter *adap; |
| 582 | struct resource *res; |
| 583 | struct device *dev = &pdev->dev; |
| 584 | u32 bus_speed; |
Wolfram Sang | 93e953d | 2014-05-28 09:44:37 +0200 | [diff] [blame] | 585 | int irq, ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 586 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 587 | priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 588 | if (!priv) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 589 | return -ENOMEM; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 590 | |
Ben Dooks | bc8120f | 2014-01-26 16:05:35 +0000 | [diff] [blame] | 591 | priv->clk = devm_clk_get(dev, NULL); |
| 592 | if (IS_ERR(priv->clk)) { |
| 593 | dev_err(dev, "cannot get clock\n"); |
| 594 | return PTR_ERR(priv->clk); |
| 595 | } |
| 596 | |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 597 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 598 | priv->io = devm_ioremap_resource(dev, res); |
| 599 | if (IS_ERR(priv->io)) |
| 600 | return PTR_ERR(priv->io); |
| 601 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 602 | bus_speed = 100000; /* default 100 kHz */ |
Geert Uytterhoeven | c6f1891 | 2015-10-07 10:16:31 +0200 | [diff] [blame] | 603 | of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed); |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 604 | |
Geert Uytterhoeven | c6f1891 | 2015-10-07 10:16:31 +0200 | [diff] [blame] | 605 | priv->devtype = (enum rcar_i2c_type)of_match_device(rcar_i2c_dt_ids, dev)->data; |
Nguyen Viet Dung | b720423 | 2013-09-03 09:09:25 +0900 | [diff] [blame] | 606 | |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 607 | pm_runtime_enable(dev); |
| 608 | pm_runtime_get_sync(dev); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 609 | ret = rcar_i2c_clock_calculate(priv, bus_speed, dev); |
| 610 | if (ret < 0) |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 611 | goto out_pm_put; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 612 | |
Wolfram Sang | 2c78cdc | 2015-11-19 16:56:42 +0100 | [diff] [blame] | 613 | rcar_i2c_init(priv); |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 614 | pm_runtime_put(dev); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 615 | |
Wolfram Sang | 93e953d | 2014-05-28 09:44:37 +0200 | [diff] [blame] | 616 | irq = platform_get_irq(pdev, 0); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 617 | init_waitqueue_head(&priv->wait); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 618 | |
Wolfram Sang | 929e3aba | 2014-07-10 13:46:31 +0200 | [diff] [blame] | 619 | adap = &priv->adap; |
| 620 | adap->nr = pdev->id; |
| 621 | adap->algo = &rcar_i2c_algo; |
| 622 | adap->class = I2C_CLASS_DEPRECATED; |
| 623 | adap->retries = 3; |
| 624 | adap->dev.parent = dev; |
| 625 | adap->dev.of_node = dev->of_node; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 626 | i2c_set_adapdata(adap, priv); |
| 627 | strlcpy(adap->name, pdev->name, sizeof(adap->name)); |
| 628 | |
Wolfram Sang | 93e953d | 2014-05-28 09:44:37 +0200 | [diff] [blame] | 629 | ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 630 | dev_name(dev), priv); |
| 631 | if (ret < 0) { |
Wolfram Sang | 93e953d | 2014-05-28 09:44:37 +0200 | [diff] [blame] | 632 | dev_err(dev, "cannot get irq %d\n", irq); |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 633 | goto out_pm_disable; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Wolfram Sang | 4f7effd | 2015-10-09 10:39:25 +0100 | [diff] [blame] | 636 | platform_set_drvdata(pdev, priv); |
| 637 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 638 | ret = i2c_add_numbered_adapter(adap); |
| 639 | if (ret < 0) { |
| 640 | dev_err(dev, "reg adap failed: %d\n", ret); |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 641 | goto out_pm_disable; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 644 | dev_info(dev, "probed\n"); |
| 645 | |
| 646 | return 0; |
Wolfram Sang | e43e0df | 2015-11-19 16:56:41 +0100 | [diff] [blame] | 647 | |
| 648 | out_pm_put: |
| 649 | pm_runtime_put(dev); |
| 650 | out_pm_disable: |
| 651 | pm_runtime_disable(dev); |
| 652 | return ret; |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 655 | static int rcar_i2c_remove(struct platform_device *pdev) |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 656 | { |
| 657 | struct rcar_i2c_priv *priv = platform_get_drvdata(pdev); |
| 658 | struct device *dev = &pdev->dev; |
| 659 | |
| 660 | i2c_del_adapter(&priv->adap); |
| 661 | pm_runtime_disable(dev); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Wolfram Sang | 45fd5e4 | 2012-11-13 11:24:15 +0100 | [diff] [blame] | 666 | static struct platform_driver rcar_i2c_driver = { |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 667 | .driver = { |
| 668 | .name = "i2c-rcar", |
Guennadi Liakhovetski | 7679c0e | 2013-09-12 14:36:46 +0200 | [diff] [blame] | 669 | .of_match_table = rcar_i2c_dt_ids, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 670 | }, |
| 671 | .probe = rcar_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 672 | .remove = rcar_i2c_remove, |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 673 | }; |
| 674 | |
Wolfram Sang | 45fd5e4 | 2012-11-13 11:24:15 +0100 | [diff] [blame] | 675 | module_platform_driver(rcar_i2c_driver); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 676 | |
Wolfram Sang | 3d99bea | 2014-05-28 09:44:46 +0200 | [diff] [blame] | 677 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | 6ccbe60 | 2012-09-27 23:44:25 -0700 | [diff] [blame] | 678 | MODULE_DESCRIPTION("Renesas R-Car I2C bus driver"); |
| 679 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |