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