Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Mobile I2C Controller |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * |
| 6 | * Portions of the code based on out-of-tree driver i2c-sh7343.c |
| 7 | * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/err.h> |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 31 | #include <linux/pm_runtime.h> |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 32 | #include <linux/clk.h> |
| 33 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 35 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 36 | /* Transmit operation: */ |
| 37 | /* */ |
| 38 | /* 0 byte transmit */ |
| 39 | /* BUS: S A8 ACK P */ |
| 40 | /* IRQ: DTE WAIT */ |
| 41 | /* ICIC: */ |
| 42 | /* ICCR: 0x94 0x90 */ |
| 43 | /* ICDR: A8 */ |
| 44 | /* */ |
| 45 | /* 1 byte transmit */ |
| 46 | /* BUS: S A8 ACK D8(1) ACK P */ |
| 47 | /* IRQ: DTE WAIT WAIT */ |
| 48 | /* ICIC: -DTE */ |
| 49 | /* ICCR: 0x94 0x90 */ |
| 50 | /* ICDR: A8 D8(1) */ |
| 51 | /* */ |
| 52 | /* 2 byte transmit */ |
| 53 | /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P */ |
| 54 | /* IRQ: DTE WAIT WAIT WAIT */ |
| 55 | /* ICIC: -DTE */ |
| 56 | /* ICCR: 0x94 0x90 */ |
| 57 | /* ICDR: A8 D8(1) D8(2) */ |
| 58 | /* */ |
| 59 | /* 3 bytes or more, +---------+ gets repeated */ |
| 60 | /* */ |
| 61 | /* */ |
| 62 | /* Receive operation: */ |
| 63 | /* */ |
| 64 | /* 0 byte receive - not supported since slave may hold SDA low */ |
| 65 | /* */ |
| 66 | /* 1 byte receive [TX] | [RX] */ |
| 67 | /* BUS: S A8 ACK | D8(1) ACK P */ |
| 68 | /* IRQ: DTE WAIT | WAIT DTE */ |
| 69 | /* ICIC: -DTE | +DTE */ |
| 70 | /* ICCR: 0x94 0x81 | 0xc0 */ |
| 71 | /* ICDR: A8 | D8(1) */ |
| 72 | /* */ |
| 73 | /* 2 byte receive [TX]| [RX] */ |
| 74 | /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P */ |
| 75 | /* IRQ: DTE WAIT | WAIT WAIT DTE */ |
| 76 | /* ICIC: -DTE | +DTE */ |
| 77 | /* ICCR: 0x94 0x81 | 0xc0 */ |
| 78 | /* ICDR: A8 | D8(1) D8(2) */ |
| 79 | /* */ |
| 80 | /* 3 byte receive [TX] | [RX] */ |
| 81 | /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */ |
| 82 | /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */ |
| 83 | /* ICIC: -DTE | +DTE */ |
| 84 | /* ICCR: 0x94 0x81 | 0xc0 */ |
| 85 | /* ICDR: A8 | D8(1) D8(2) D8(3) */ |
| 86 | /* */ |
| 87 | /* 4 bytes or more, this part is repeated +---------+ */ |
| 88 | /* */ |
| 89 | /* */ |
| 90 | /* Interrupt order and BUSY flag */ |
| 91 | /* ___ _ */ |
| 92 | /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */ |
| 93 | /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */ |
| 94 | /* */ |
| 95 | /* S D7 D6 D5 D4 D3 D2 D1 D0 P */ |
| 96 | /* ___ */ |
| 97 | /* WAIT IRQ ________________________________/ \___________ */ |
| 98 | /* TACK IRQ ____________________________________/ \_______ */ |
| 99 | /* DTE IRQ __________________________________________/ \_ */ |
| 100 | /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ |
| 101 | /* _______________________________________________ */ |
| 102 | /* BUSY __/ \_ */ |
| 103 | /* */ |
| 104 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 105 | enum sh_mobile_i2c_op { |
| 106 | OP_START = 0, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 107 | OP_TX_FIRST, |
| 108 | OP_TX, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 109 | OP_TX_STOP, |
| 110 | OP_TX_TO_RX, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 111 | OP_RX, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 112 | OP_RX_STOP, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 113 | OP_RX_STOP_DATA, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct sh_mobile_i2c_data { |
| 117 | struct device *dev; |
| 118 | void __iomem *reg; |
| 119 | struct i2c_adapter adap; |
| 120 | |
| 121 | struct clk *clk; |
| 122 | u_int8_t iccl; |
| 123 | u_int8_t icch; |
| 124 | |
| 125 | spinlock_t lock; |
| 126 | wait_queue_head_t wait; |
| 127 | struct i2c_msg *msg; |
| 128 | int pos; |
| 129 | int sr; |
| 130 | }; |
| 131 | |
| 132 | #define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */ |
| 133 | |
| 134 | /* Register offsets */ |
| 135 | #define ICDR(pd) (pd->reg + 0x00) |
| 136 | #define ICCR(pd) (pd->reg + 0x04) |
| 137 | #define ICSR(pd) (pd->reg + 0x08) |
| 138 | #define ICIC(pd) (pd->reg + 0x0c) |
| 139 | #define ICCL(pd) (pd->reg + 0x10) |
| 140 | #define ICCH(pd) (pd->reg + 0x14) |
| 141 | |
| 142 | /* Register bits */ |
| 143 | #define ICCR_ICE 0x80 |
| 144 | #define ICCR_RACK 0x40 |
| 145 | #define ICCR_TRS 0x10 |
| 146 | #define ICCR_BBSY 0x04 |
| 147 | #define ICCR_SCP 0x01 |
| 148 | |
| 149 | #define ICSR_SCLM 0x80 |
| 150 | #define ICSR_SDAM 0x40 |
| 151 | #define SW_DONE 0x20 |
| 152 | #define ICSR_BUSY 0x10 |
| 153 | #define ICSR_AL 0x08 |
| 154 | #define ICSR_TACK 0x04 |
| 155 | #define ICSR_WAIT 0x02 |
| 156 | #define ICSR_DTE 0x01 |
| 157 | |
| 158 | #define ICIC_ALE 0x08 |
| 159 | #define ICIC_TACKE 0x04 |
| 160 | #define ICIC_WAITE 0x02 |
| 161 | #define ICIC_DTEE 0x01 |
| 162 | |
| 163 | static void activate_ch(struct sh_mobile_i2c_data *pd) |
| 164 | { |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 165 | unsigned long i2c_clk; |
| 166 | u_int32_t num; |
| 167 | u_int32_t denom; |
| 168 | u_int32_t tmp; |
| 169 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 170 | /* Wake up device and enable clock */ |
| 171 | pm_runtime_get_sync(pd->dev); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 172 | clk_enable(pd->clk); |
| 173 | |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 174 | /* Get clock rate after clock is enabled */ |
| 175 | i2c_clk = clk_get_rate(pd->clk); |
| 176 | |
| 177 | /* Calculate the value for iccl. From the data sheet: |
| 178 | * iccl = (p clock / transfer rate) * (L / (L + H)) |
| 179 | * where L and H are the SCL low/high ratio (5/4 in this case). |
| 180 | * We also round off the result. |
| 181 | */ |
| 182 | num = i2c_clk * 5; |
| 183 | denom = NORMAL_SPEED * 9; |
| 184 | tmp = num * 10 / denom; |
| 185 | if (tmp % 10 >= 5) |
| 186 | pd->iccl = (u_int8_t)((num/denom) + 1); |
| 187 | else |
| 188 | pd->iccl = (u_int8_t)(num/denom); |
| 189 | |
| 190 | /* Calculate the value for icch. From the data sheet: |
| 191 | icch = (p clock / transfer rate) * (H / (L + H)) */ |
| 192 | num = i2c_clk * 4; |
| 193 | tmp = num * 10 / denom; |
| 194 | if (tmp % 10 >= 5) |
| 195 | pd->icch = (u_int8_t)((num/denom) + 1); |
| 196 | else |
| 197 | pd->icch = (u_int8_t)(num/denom); |
| 198 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 199 | /* Enable channel and configure rx ack */ |
| 200 | iowrite8(ioread8(ICCR(pd)) | ICCR_ICE, ICCR(pd)); |
| 201 | |
| 202 | /* Mask all interrupts */ |
| 203 | iowrite8(0, ICIC(pd)); |
| 204 | |
| 205 | /* Set the clock */ |
| 206 | iowrite8(pd->iccl, ICCL(pd)); |
| 207 | iowrite8(pd->icch, ICCH(pd)); |
| 208 | } |
| 209 | |
| 210 | static void deactivate_ch(struct sh_mobile_i2c_data *pd) |
| 211 | { |
| 212 | /* Clear/disable interrupts */ |
| 213 | iowrite8(0, ICSR(pd)); |
| 214 | iowrite8(0, ICIC(pd)); |
| 215 | |
| 216 | /* Disable channel */ |
| 217 | iowrite8(ioread8(ICCR(pd)) & ~ICCR_ICE, ICCR(pd)); |
| 218 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 219 | /* Disable clock and mark device as idle */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 220 | clk_disable(pd->clk); |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 221 | pm_runtime_put_sync(pd->dev); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, |
| 225 | enum sh_mobile_i2c_op op, unsigned char data) |
| 226 | { |
| 227 | unsigned char ret = 0; |
| 228 | unsigned long flags; |
| 229 | |
| 230 | dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data); |
| 231 | |
| 232 | spin_lock_irqsave(&pd->lock, flags); |
| 233 | |
| 234 | switch (op) { |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 235 | case OP_START: /* issue start and trigger DTE interrupt */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 236 | iowrite8(0x94, ICCR(pd)); |
| 237 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 238 | case OP_TX_FIRST: /* disable DTE interrupt and write data */ |
| 239 | iowrite8(ICIC_WAITE | ICIC_ALE | ICIC_TACKE, ICIC(pd)); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 240 | iowrite8(data, ICDR(pd)); |
| 241 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 242 | case OP_TX: /* write data */ |
| 243 | iowrite8(data, ICDR(pd)); |
| 244 | break; |
| 245 | case OP_TX_STOP: /* write data and issue a stop afterwards */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 246 | iowrite8(data, ICDR(pd)); |
| 247 | iowrite8(0x90, ICCR(pd)); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 248 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 249 | case OP_TX_TO_RX: /* select read mode */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 250 | iowrite8(0x81, ICCR(pd)); |
| 251 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 252 | case OP_RX: /* just read data */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 253 | ret = ioread8(ICDR(pd)); |
| 254 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 255 | case OP_RX_STOP: /* enable DTE interrupt, issue stop */ |
| 256 | iowrite8(ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE, |
| 257 | ICIC(pd)); |
| 258 | iowrite8(0xc0, ICCR(pd)); |
| 259 | break; |
| 260 | case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ |
| 261 | iowrite8(ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE, |
| 262 | ICIC(pd)); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 263 | ret = ioread8(ICDR(pd)); |
| 264 | iowrite8(0xc0, ICCR(pd)); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | spin_unlock_irqrestore(&pd->lock, flags); |
| 269 | |
| 270 | dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret); |
| 271 | return ret; |
| 272 | } |
| 273 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 274 | static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd) |
| 275 | { |
| 276 | if (pd->pos == -1) |
| 277 | return 1; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd) |
| 283 | { |
| 284 | if (pd->pos == (pd->msg->len - 1)) |
| 285 | return 1; |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd, |
| 291 | unsigned char *buf) |
| 292 | { |
| 293 | switch (pd->pos) { |
| 294 | case -1: |
| 295 | *buf = (pd->msg->addr & 0x7f) << 1; |
| 296 | *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0; |
| 297 | break; |
| 298 | default: |
| 299 | *buf = pd->msg->buf[pd->pos]; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd) |
| 304 | { |
| 305 | unsigned char data; |
| 306 | |
| 307 | if (pd->pos == pd->msg->len) |
| 308 | return 1; |
| 309 | |
| 310 | sh_mobile_i2c_get_data(pd, &data); |
| 311 | |
| 312 | if (sh_mobile_i2c_is_last_byte(pd)) |
| 313 | i2c_op(pd, OP_TX_STOP, data); |
| 314 | else if (sh_mobile_i2c_is_first_byte(pd)) |
| 315 | i2c_op(pd, OP_TX_FIRST, data); |
| 316 | else |
| 317 | i2c_op(pd, OP_TX, data); |
| 318 | |
| 319 | pd->pos++; |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd) |
| 324 | { |
| 325 | unsigned char data; |
| 326 | int real_pos; |
| 327 | |
| 328 | do { |
| 329 | if (pd->pos <= -1) { |
| 330 | sh_mobile_i2c_get_data(pd, &data); |
| 331 | |
| 332 | if (sh_mobile_i2c_is_first_byte(pd)) |
| 333 | i2c_op(pd, OP_TX_FIRST, data); |
| 334 | else |
| 335 | i2c_op(pd, OP_TX, data); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | if (pd->pos == 0) { |
| 340 | i2c_op(pd, OP_TX_TO_RX, 0); |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | real_pos = pd->pos - 2; |
| 345 | |
| 346 | if (pd->pos == pd->msg->len) { |
| 347 | if (real_pos < 0) { |
| 348 | i2c_op(pd, OP_RX_STOP, 0); |
| 349 | break; |
| 350 | } |
| 351 | data = i2c_op(pd, OP_RX_STOP_DATA, 0); |
| 352 | } else |
| 353 | data = i2c_op(pd, OP_RX, 0); |
| 354 | |
Magnus Damm | bff4056c8 | 2008-11-13 15:28:21 +0900 | [diff] [blame] | 355 | if (real_pos >= 0) |
| 356 | pd->msg->buf[real_pos] = data; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 357 | } while (0); |
| 358 | |
| 359 | pd->pos++; |
| 360 | return pd->pos == (pd->msg->len + 2); |
| 361 | } |
| 362 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 363 | static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) |
| 364 | { |
| 365 | struct platform_device *dev = dev_id; |
| 366 | struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 367 | unsigned char sr; |
| 368 | int wakeup; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 369 | |
| 370 | sr = ioread8(ICSR(pd)); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 371 | pd->sr |= sr; /* remember state */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 372 | |
| 373 | dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 374 | (pd->msg->flags & I2C_M_RD) ? "read" : "write", |
| 375 | pd->pos, pd->msg->len); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 376 | |
| 377 | if (sr & (ICSR_AL | ICSR_TACK)) { |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 378 | /* don't interrupt transaction - continue to issue stop */ |
| 379 | iowrite8(sr & ~(ICSR_AL | ICSR_TACK), ICSR(pd)); |
| 380 | wakeup = 0; |
| 381 | } else if (pd->msg->flags & I2C_M_RD) |
| 382 | wakeup = sh_mobile_i2c_isr_rx(pd); |
| 383 | else |
| 384 | wakeup = sh_mobile_i2c_isr_tx(pd); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 385 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 386 | if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */ |
| 387 | iowrite8(sr & ~ICSR_WAIT, ICSR(pd)); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 388 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 389 | if (wakeup) { |
| 390 | pd->sr |= SW_DONE; |
| 391 | wake_up(&pd->wait); |
| 392 | } |
| 393 | |
| 394 | return IRQ_HANDLED; |
| 395 | } |
| 396 | |
| 397 | static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg) |
| 398 | { |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 399 | if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) { |
| 400 | dev_err(pd->dev, "Unsupported zero length i2c read\n"); |
| 401 | return -EIO; |
| 402 | } |
| 403 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 404 | /* Initialize channel registers */ |
| 405 | iowrite8(ioread8(ICCR(pd)) & ~ICCR_ICE, ICCR(pd)); |
| 406 | |
| 407 | /* Enable channel and configure rx ack */ |
| 408 | iowrite8(ioread8(ICCR(pd)) | ICCR_ICE, ICCR(pd)); |
| 409 | |
| 410 | /* Set the clock */ |
| 411 | iowrite8(pd->iccl, ICCL(pd)); |
| 412 | iowrite8(pd->icch, ICCH(pd)); |
| 413 | |
| 414 | pd->msg = usr_msg; |
| 415 | pd->pos = -1; |
| 416 | pd->sr = 0; |
| 417 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 418 | /* Enable all interrupts to begin with */ |
| 419 | iowrite8(ICIC_WAITE | ICIC_ALE | ICIC_TACKE | ICIC_DTEE, ICIC(pd)); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, |
| 424 | struct i2c_msg *msgs, |
| 425 | int num) |
| 426 | { |
| 427 | struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter); |
| 428 | struct i2c_msg *msg; |
| 429 | int err = 0; |
| 430 | u_int8_t val; |
| 431 | int i, k, retry_count; |
| 432 | |
| 433 | activate_ch(pd); |
| 434 | |
| 435 | /* Process all messages */ |
| 436 | for (i = 0; i < num; i++) { |
| 437 | msg = &msgs[i]; |
| 438 | |
| 439 | err = start_ch(pd, msg); |
| 440 | if (err) |
| 441 | break; |
| 442 | |
| 443 | i2c_op(pd, OP_START, 0); |
| 444 | |
| 445 | /* The interrupt handler takes care of the rest... */ |
| 446 | k = wait_event_timeout(pd->wait, |
| 447 | pd->sr & (ICSR_TACK | SW_DONE), |
| 448 | 5 * HZ); |
| 449 | if (!k) |
| 450 | dev_err(pd->dev, "Transfer request timed out\n"); |
| 451 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 452 | retry_count = 1000; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 453 | again: |
| 454 | val = ioread8(ICSR(pd)); |
| 455 | |
| 456 | dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); |
| 457 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 458 | /* the interrupt handler may wake us up before the |
| 459 | * transfer is finished, so poll the hardware |
| 460 | * until we're done. |
| 461 | */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 462 | if (val & ICSR_BUSY) { |
| 463 | udelay(10); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 464 | if (retry_count--) |
| 465 | goto again; |
| 466 | |
| 467 | err = -EIO; |
| 468 | dev_err(pd->dev, "Polling timed out\n"); |
| 469 | break; |
| 470 | } |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 471 | |
| 472 | /* handle missing acknowledge and arbitration lost */ |
| 473 | if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) { |
| 474 | err = -EIO; |
| 475 | break; |
| 476 | } |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | deactivate_ch(pd); |
| 480 | |
| 481 | if (!err) |
| 482 | err = num; |
| 483 | return err; |
| 484 | } |
| 485 | |
| 486 | static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter) |
| 487 | { |
| 488 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 489 | } |
| 490 | |
| 491 | static struct i2c_algorithm sh_mobile_i2c_algorithm = { |
| 492 | .functionality = sh_mobile_i2c_func, |
| 493 | .master_xfer = sh_mobile_i2c_xfer, |
| 494 | }; |
| 495 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 496 | static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook) |
| 497 | { |
| 498 | struct resource *res; |
| 499 | int ret = -ENXIO; |
Magnus Damm | 82b20d8 | 2010-08-02 07:16:37 +0000 | [diff] [blame^] | 500 | int n, k = 0; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 501 | |
| 502 | while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) { |
| 503 | for (n = res->start; hook && n <= res->end; n++) { |
| 504 | if (request_irq(n, sh_mobile_i2c_isr, IRQF_DISABLED, |
Magnus Damm | 82b20d8 | 2010-08-02 07:16:37 +0000 | [diff] [blame^] | 505 | dev_name(&dev->dev), dev)) { |
| 506 | for (n--; n >= res->start; n--) |
| 507 | free_irq(n, dev); |
| 508 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 509 | goto rollback; |
Magnus Damm | 82b20d8 | 2010-08-02 07:16:37 +0000 | [diff] [blame^] | 510 | } |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 511 | } |
| 512 | k++; |
| 513 | } |
| 514 | |
| 515 | if (hook) |
| 516 | return k > 0 ? 0 : -ENOENT; |
| 517 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 518 | ret = 0; |
| 519 | |
| 520 | rollback: |
Magnus Damm | 82b20d8 | 2010-08-02 07:16:37 +0000 | [diff] [blame^] | 521 | k--; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 522 | |
Magnus Damm | 82b20d8 | 2010-08-02 07:16:37 +0000 | [diff] [blame^] | 523 | while (k >= 0) { |
| 524 | res = platform_get_resource(dev, IORESOURCE_IRQ, k); |
| 525 | for (n = res->start; n <= res->end; n++) |
| 526 | free_irq(n, dev); |
| 527 | |
| 528 | k--; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static int sh_mobile_i2c_probe(struct platform_device *dev) |
| 535 | { |
| 536 | struct sh_mobile_i2c_data *pd; |
| 537 | struct i2c_adapter *adap; |
| 538 | struct resource *res; |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 539 | char clk_name[8]; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 540 | int size; |
| 541 | int ret; |
| 542 | |
| 543 | pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); |
| 544 | if (pd == NULL) { |
| 545 | dev_err(&dev->dev, "cannot allocate private data\n"); |
| 546 | return -ENOMEM; |
| 547 | } |
| 548 | |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 549 | snprintf(clk_name, sizeof(clk_name), "i2c%d", dev->id); |
| 550 | pd->clk = clk_get(&dev->dev, clk_name); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 551 | if (IS_ERR(pd->clk)) { |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 552 | dev_err(&dev->dev, "cannot get clock \"%s\"\n", clk_name); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 553 | ret = PTR_ERR(pd->clk); |
| 554 | goto err; |
| 555 | } |
| 556 | |
| 557 | ret = sh_mobile_i2c_hook_irqs(dev, 1); |
| 558 | if (ret) { |
| 559 | dev_err(&dev->dev, "cannot request IRQ\n"); |
| 560 | goto err_clk; |
| 561 | } |
| 562 | |
| 563 | pd->dev = &dev->dev; |
| 564 | platform_set_drvdata(dev, pd); |
| 565 | |
| 566 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 567 | if (res == NULL) { |
| 568 | dev_err(&dev->dev, "cannot find IO resource\n"); |
| 569 | ret = -ENOENT; |
| 570 | goto err_irq; |
| 571 | } |
| 572 | |
Julia Lawall | 5933082 | 2009-07-05 08:37:50 +0200 | [diff] [blame] | 573 | size = resource_size(res); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 574 | |
| 575 | pd->reg = ioremap(res->start, size); |
| 576 | if (pd->reg == NULL) { |
| 577 | dev_err(&dev->dev, "cannot map IO\n"); |
| 578 | ret = -ENXIO; |
| 579 | goto err_irq; |
| 580 | } |
| 581 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 582 | /* Enable Runtime PM for this device. |
| 583 | * |
| 584 | * Also tell the Runtime PM core to ignore children |
| 585 | * for this device since it is valid for us to suspend |
| 586 | * this I2C master driver even though the slave devices |
| 587 | * on the I2C bus may not be suspended. |
| 588 | * |
| 589 | * The state of the I2C hardware bus is unaffected by |
| 590 | * the Runtime PM state. |
| 591 | */ |
| 592 | pm_suspend_ignore_children(&dev->dev, true); |
| 593 | pm_runtime_enable(&dev->dev); |
| 594 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 595 | /* setup the private data */ |
| 596 | adap = &pd->adap; |
| 597 | i2c_set_adapdata(adap, pd); |
| 598 | |
| 599 | adap->owner = THIS_MODULE; |
| 600 | adap->algo = &sh_mobile_i2c_algorithm; |
| 601 | adap->dev.parent = &dev->dev; |
| 602 | adap->retries = 5; |
| 603 | adap->nr = dev->id; |
| 604 | |
| 605 | strlcpy(adap->name, dev->name, sizeof(adap->name)); |
| 606 | |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 607 | spin_lock_init(&pd->lock); |
| 608 | init_waitqueue_head(&pd->wait); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 609 | |
| 610 | ret = i2c_add_numbered_adapter(adap); |
| 611 | if (ret < 0) { |
| 612 | dev_err(&dev->dev, "cannot add numbered adapter\n"); |
| 613 | goto err_all; |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | |
| 618 | err_all: |
| 619 | iounmap(pd->reg); |
| 620 | err_irq: |
| 621 | sh_mobile_i2c_hook_irqs(dev, 0); |
| 622 | err_clk: |
| 623 | clk_put(pd->clk); |
| 624 | err: |
| 625 | kfree(pd); |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | static int sh_mobile_i2c_remove(struct platform_device *dev) |
| 630 | { |
| 631 | struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); |
| 632 | |
| 633 | i2c_del_adapter(&pd->adap); |
| 634 | iounmap(pd->reg); |
| 635 | sh_mobile_i2c_hook_irqs(dev, 0); |
| 636 | clk_put(pd->clk); |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 637 | pm_runtime_disable(&dev->dev); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 638 | kfree(pd); |
| 639 | return 0; |
| 640 | } |
| 641 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 642 | static int sh_mobile_i2c_runtime_nop(struct device *dev) |
| 643 | { |
| 644 | /* Runtime PM callback shared between ->runtime_suspend() |
| 645 | * and ->runtime_resume(). Simply returns success. |
| 646 | * |
| 647 | * This driver re-initializes all registers after |
| 648 | * pm_runtime_get_sync() anyway so there is no need |
| 649 | * to save and restore registers here. |
| 650 | */ |
| 651 | return 0; |
| 652 | } |
| 653 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 654 | static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = { |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 655 | .runtime_suspend = sh_mobile_i2c_runtime_nop, |
| 656 | .runtime_resume = sh_mobile_i2c_runtime_nop, |
| 657 | }; |
| 658 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 659 | static struct platform_driver sh_mobile_i2c_driver = { |
| 660 | .driver = { |
| 661 | .name = "i2c-sh_mobile", |
| 662 | .owner = THIS_MODULE, |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 663 | .pm = &sh_mobile_i2c_dev_pm_ops, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 664 | }, |
| 665 | .probe = sh_mobile_i2c_probe, |
| 666 | .remove = sh_mobile_i2c_remove, |
| 667 | }; |
| 668 | |
| 669 | static int __init sh_mobile_i2c_adap_init(void) |
| 670 | { |
| 671 | return platform_driver_register(&sh_mobile_i2c_driver); |
| 672 | } |
| 673 | |
| 674 | static void __exit sh_mobile_i2c_adap_exit(void) |
| 675 | { |
| 676 | platform_driver_unregister(&sh_mobile_i2c_driver); |
| 677 | } |
| 678 | |
Magnus Damm | ccb3bc1 | 2009-07-22 23:58:39 +0900 | [diff] [blame] | 679 | subsys_initcall(sh_mobile_i2c_adap_init); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 680 | module_exit(sh_mobile_i2c_adap_exit); |
| 681 | |
| 682 | MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver"); |
| 683 | MODULE_AUTHOR("Magnus Damm"); |
| 684 | MODULE_LICENSE("GPL v2"); |