David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 1 | /* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * platforms. |
| 3 | * |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 4 | * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/wait.h> |
| 13 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/bbc.h> |
David S. Miller | 4b5dff7 | 2007-05-13 22:22:13 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "bbc_i2c.h" |
| 21 | |
| 22 | /* Convert this driver to use i2c bus layer someday... */ |
| 23 | #define I2C_PCF_PIN 0x80 |
| 24 | #define I2C_PCF_ESO 0x40 |
| 25 | #define I2C_PCF_ES1 0x20 |
| 26 | #define I2C_PCF_ES2 0x10 |
| 27 | #define I2C_PCF_ENI 0x08 |
| 28 | #define I2C_PCF_STA 0x04 |
| 29 | #define I2C_PCF_STO 0x02 |
| 30 | #define I2C_PCF_ACK 0x01 |
| 31 | |
| 32 | #define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK) |
| 33 | #define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK) |
| 34 | #define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK) |
| 35 | #define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK) |
| 36 | |
| 37 | #define I2C_PCF_INI 0x40 /* 1 if not initialized */ |
| 38 | #define I2C_PCF_STS 0x20 |
| 39 | #define I2C_PCF_BER 0x10 |
| 40 | #define I2C_PCF_AD0 0x08 |
| 41 | #define I2C_PCF_LRB 0x08 |
| 42 | #define I2C_PCF_AAS 0x04 |
| 43 | #define I2C_PCF_LAB 0x02 |
| 44 | #define I2C_PCF_BB 0x01 |
| 45 | |
| 46 | /* The BBC devices have two I2C controllers. The first I2C controller |
| 47 | * connects mainly to configuration proms (NVRAM, cpu configuration, |
| 48 | * dimm types, etc.). Whereas the second I2C controller connects to |
| 49 | * environmental control devices such as fans and temperature sensors. |
| 50 | * The second controller also connects to the smartcard reader, if present. |
| 51 | */ |
| 52 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 53 | static void set_device_claimage(struct bbc_i2c_bus *bp, struct platform_device *op, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < NUM_CHILDREN; i++) { |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 58 | if (bp->devs[i].device == op) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | bp->devs[i].client_claimed = val; |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1) |
| 66 | #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0) |
| 67 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 68 | struct platform_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 70 | struct platform_device *op = NULL; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 71 | int curidx = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 73 | for (i = 0; i < NUM_CHILDREN; i++) { |
| 74 | if (!(op = bp->devs[i].device)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | break; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 76 | if (curidx == index) |
| 77 | goto out; |
| 78 | op = NULL; |
| 79 | curidx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | out: |
| 83 | if (curidx == index) |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 84 | return op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
| 87 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 88 | struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct platform_device *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | struct bbc_i2c_client *client; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 91 | const u32 *reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 93 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (!client) |
| 95 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | client->bp = bp; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 97 | client->op = op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 99 | reg = of_get_property(op->dev.of_node, "reg", NULL); |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 100 | if (!reg) { |
| 101 | kfree(client); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | client->bus = reg[0]; |
| 106 | client->address = reg[1]; |
| 107 | |
| 108 | claim_device(bp, op); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | return client; |
| 111 | } |
| 112 | |
| 113 | void bbc_i2c_detach(struct bbc_i2c_client *client) |
| 114 | { |
| 115 | struct bbc_i2c_bus *bp = client->bp; |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 116 | struct platform_device *op = client->op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 118 | release_device(bp, op); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | kfree(client); |
| 120 | } |
| 121 | |
| 122 | static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status) |
| 123 | { |
| 124 | DECLARE_WAITQUEUE(wait, current); |
| 125 | int limit = 32; |
| 126 | int ret = 1; |
| 127 | |
| 128 | bp->waiting = 1; |
| 129 | add_wait_queue(&bp->wq, &wait); |
| 130 | while (limit-- > 0) { |
Roel Kluin | f4c1363 | 2009-03-04 00:19:28 -0800 | [diff] [blame] | 131 | long val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
David S. Miller | 3b36fb8 | 2007-02-26 10:11:35 -0800 | [diff] [blame] | 133 | val = wait_event_interruptible_timeout( |
| 134 | bp->wq, |
| 135 | (((*status = readb(bp->i2c_control_regs + 0)) |
| 136 | & I2C_PCF_PIN) == 0), |
| 137 | msecs_to_jiffies(250)); |
| 138 | if (val > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | ret = 0; |
| 140 | break; |
| 141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | remove_wait_queue(&bp->wq, &wait); |
| 144 | bp->waiting = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off) |
| 150 | { |
| 151 | struct bbc_i2c_bus *bp = client->bp; |
| 152 | int address = client->address; |
| 153 | u8 status; |
| 154 | int ret = -1; |
| 155 | |
| 156 | if (bp->i2c_bussel_reg != NULL) |
| 157 | writeb(client->bus, bp->i2c_bussel_reg); |
| 158 | |
| 159 | writeb(address, bp->i2c_control_regs + 0x1); |
| 160 | writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0); |
| 161 | if (wait_for_pin(bp, &status)) |
| 162 | goto out; |
| 163 | |
| 164 | writeb(off, bp->i2c_control_regs + 0x1); |
| 165 | if (wait_for_pin(bp, &status) || |
| 166 | (status & I2C_PCF_LRB) != 0) |
| 167 | goto out; |
| 168 | |
| 169 | writeb(val, bp->i2c_control_regs + 0x1); |
| 170 | if (wait_for_pin(bp, &status)) |
| 171 | goto out; |
| 172 | |
| 173 | ret = 0; |
| 174 | |
| 175 | out: |
| 176 | writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off) |
| 181 | { |
| 182 | struct bbc_i2c_bus *bp = client->bp; |
| 183 | unsigned char address = client->address, status; |
| 184 | int ret = -1; |
| 185 | |
| 186 | if (bp->i2c_bussel_reg != NULL) |
| 187 | writeb(client->bus, bp->i2c_bussel_reg); |
| 188 | |
| 189 | writeb(address, bp->i2c_control_regs + 0x1); |
| 190 | writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0); |
| 191 | if (wait_for_pin(bp, &status)) |
| 192 | goto out; |
| 193 | |
| 194 | writeb(off, bp->i2c_control_regs + 0x1); |
| 195 | if (wait_for_pin(bp, &status) || |
| 196 | (status & I2C_PCF_LRB) != 0) |
| 197 | goto out; |
| 198 | |
| 199 | writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0); |
| 200 | |
| 201 | address |= 0x1; /* READ */ |
| 202 | |
| 203 | writeb(address, bp->i2c_control_regs + 0x1); |
| 204 | writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0); |
| 205 | if (wait_for_pin(bp, &status)) |
| 206 | goto out; |
| 207 | |
| 208 | /* Set PIN back to one so the device sends the first |
| 209 | * byte. |
| 210 | */ |
| 211 | (void) readb(bp->i2c_control_regs + 0x1); |
| 212 | if (wait_for_pin(bp, &status)) |
| 213 | goto out; |
| 214 | |
| 215 | writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0); |
| 216 | *byte = readb(bp->i2c_control_regs + 0x1); |
| 217 | if (wait_for_pin(bp, &status)) |
| 218 | goto out; |
| 219 | |
| 220 | ret = 0; |
| 221 | |
| 222 | out: |
| 223 | writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0); |
| 224 | (void) readb(bp->i2c_control_regs + 0x1); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | int bbc_i2c_write_buf(struct bbc_i2c_client *client, |
| 230 | char *buf, int len, int off) |
| 231 | { |
| 232 | int ret = 0; |
| 233 | |
| 234 | while (len > 0) { |
Axel Lin | e410471 | 2011-11-21 21:42:20 +0000 | [diff] [blame] | 235 | ret = bbc_i2c_writeb(client, *buf, off); |
| 236 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | len--; |
| 239 | buf++; |
| 240 | off++; |
| 241 | } |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | int bbc_i2c_read_buf(struct bbc_i2c_client *client, |
| 246 | char *buf, int len, int off) |
| 247 | { |
| 248 | int ret = 0; |
| 249 | |
| 250 | while (len > 0) { |
Axel Lin | e410471 | 2011-11-21 21:42:20 +0000 | [diff] [blame] | 251 | ret = bbc_i2c_readb(client, buf, off); |
| 252 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | len--; |
| 255 | buf++; |
| 256 | off++; |
| 257 | } |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | EXPORT_SYMBOL(bbc_i2c_getdev); |
| 263 | EXPORT_SYMBOL(bbc_i2c_attach); |
| 264 | EXPORT_SYMBOL(bbc_i2c_detach); |
| 265 | EXPORT_SYMBOL(bbc_i2c_writeb); |
| 266 | EXPORT_SYMBOL(bbc_i2c_readb); |
| 267 | EXPORT_SYMBOL(bbc_i2c_write_buf); |
| 268 | EXPORT_SYMBOL(bbc_i2c_read_buf); |
| 269 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 270 | static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | struct bbc_i2c_bus *bp = dev_id; |
| 273 | |
| 274 | /* PIN going from set to clear is the only event which |
| 275 | * makes the i2c assert an interrupt. |
| 276 | */ |
| 277 | if (bp->waiting && |
| 278 | !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN)) |
David S. Miller | 3b36fb8 | 2007-02-26 10:11:35 -0800 | [diff] [blame] | 279 | wake_up_interruptible(&bp->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
Sam Ravnborg | 6e51f85 | 2013-03-30 11:44:24 +0000 | [diff] [blame] | 284 | static void reset_one_i2c(struct bbc_i2c_bus *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0); |
| 287 | writeb(bp->own, bp->i2c_control_regs + 0x1); |
| 288 | writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0); |
| 289 | writeb(bp->clock, bp->i2c_control_regs + 0x1); |
| 290 | writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0); |
| 291 | } |
| 292 | |
Sam Ravnborg | 6e51f85 | 2013-03-30 11:44:24 +0000 | [diff] [blame] | 293 | static struct bbc_i2c_bus * attach_one_i2c(struct platform_device *op, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Mariusz Kozlowski | 50aa485 | 2007-07-31 14:04:57 -0700 | [diff] [blame] | 295 | struct bbc_i2c_bus *bp; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 296 | struct device_node *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | int entry; |
| 298 | |
Mariusz Kozlowski | 50aa485 | 2007-07-31 14:04:57 -0700 | [diff] [blame] | 299 | bp = kzalloc(sizeof(*bp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (!bp) |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 301 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Christopher Alexander Tobias Schulze | 5cdceab | 2014-08-03 15:44:52 +0200 | [diff] [blame] | 303 | INIT_LIST_HEAD(&bp->temps); |
| 304 | INIT_LIST_HEAD(&bp->fans); |
| 305 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 306 | bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (!bp->i2c_control_regs) |
| 308 | goto fail; |
| 309 | |
Christopher Alexander Tobias Schulze | 5cdceab | 2014-08-03 15:44:52 +0200 | [diff] [blame] | 310 | if (op->num_resources == 2) { |
| 311 | bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel"); |
| 312 | if (!bp->i2c_bussel_reg) |
| 313 | goto fail; |
| 314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | bp->waiting = 0; |
| 317 | init_waitqueue_head(&bp->wq); |
Grant Likely | 1636f8a | 2010-06-18 11:09:58 -0600 | [diff] [blame] | 318 | if (request_irq(op->archdata.irqs[0], bbc_i2c_interrupt, |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 319 | IRQF_SHARED, "bbc_i2c", bp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | goto fail; |
| 321 | |
| 322 | bp->index = index; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 323 | bp->op = op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | spin_lock_init(&bp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | entry = 0; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 328 | for (dp = op->dev.of_node->child; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 329 | dp && entry < 8; |
| 330 | dp = dp->sibling, entry++) { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 331 | struct platform_device *child_op; |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 332 | |
| 333 | child_op = of_find_device_by_node(dp); |
| 334 | bp->devs[entry].device = child_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | bp->devs[entry].client_claimed = 0; |
| 336 | } |
| 337 | |
| 338 | writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0); |
| 339 | bp->own = readb(bp->i2c_control_regs + 0x01); |
| 340 | writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0); |
| 341 | bp->clock = readb(bp->i2c_control_regs + 0x01); |
| 342 | |
| 343 | printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n", |
| 344 | bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock); |
| 345 | |
| 346 | reset_one_i2c(bp); |
| 347 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 348 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | fail: |
| 351 | if (bp->i2c_bussel_reg) |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 352 | of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (bp->i2c_control_regs) |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 354 | of_iounmap(&op->resource[0], bp->i2c_control_regs, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | kfree(bp); |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 356 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 359 | extern int bbc_envctrl_init(struct bbc_i2c_bus *bp); |
| 360 | extern void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp); |
| 361 | |
Greg Kroah-Hartman | 082a200 | 2012-12-21 13:24:23 -0800 | [diff] [blame] | 362 | static int bbc_i2c_probe(struct platform_device *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 364 | struct bbc_i2c_bus *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | int err, index = 0; |
| 366 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 367 | bp = attach_one_i2c(op, index); |
| 368 | if (!bp) |
| 369 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 371 | err = bbc_envctrl_init(bp); |
| 372 | if (err) { |
Grant Likely | 1636f8a | 2010-06-18 11:09:58 -0600 | [diff] [blame] | 373 | free_irq(op->archdata.irqs[0], bp); |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 374 | if (bp->i2c_bussel_reg) |
| 375 | of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1); |
| 376 | if (bp->i2c_control_regs) |
| 377 | of_iounmap(&op->resource[1], bp->i2c_control_regs, 2); |
| 378 | kfree(bp); |
| 379 | } else { |
| 380 | dev_set_drvdata(&op->dev, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | return err; |
| 384 | } |
| 385 | |
Greg Kroah-Hartman | 082a200 | 2012-12-21 13:24:23 -0800 | [diff] [blame] | 386 | static int bbc_i2c_remove(struct platform_device *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 388 | struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 390 | bbc_envctrl_cleanup(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Grant Likely | 1636f8a | 2010-06-18 11:09:58 -0600 | [diff] [blame] | 392 | free_irq(op->archdata.irqs[0], bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 394 | if (bp->i2c_bussel_reg) |
| 395 | of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1); |
| 396 | if (bp->i2c_control_regs) |
| 397 | of_iounmap(&op->resource[1], bp->i2c_control_regs, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 399 | kfree(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 401 | return 0; |
| 402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 404 | static const struct of_device_id bbc_i2c_match[] = { |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 405 | { |
| 406 | .name = "i2c", |
| 407 | .compatible = "SUNW,bbc-i2c", |
| 408 | }, |
| 409 | {}, |
| 410 | }; |
| 411 | MODULE_DEVICE_TABLE(of, bbc_i2c_match); |
| 412 | |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 413 | static struct platform_driver bbc_i2c_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 414 | .driver = { |
| 415 | .name = "bbc_i2c", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 416 | .of_match_table = bbc_i2c_match, |
| 417 | }, |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 418 | .probe = bbc_i2c_probe, |
Greg Kroah-Hartman | 082a200 | 2012-12-21 13:24:23 -0800 | [diff] [blame] | 419 | .remove = bbc_i2c_remove, |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 420 | }; |
| 421 | |
Axel Lin | dbf2b92 | 2011-11-26 19:04:25 +0000 | [diff] [blame] | 422 | module_platform_driver(bbc_i2c_driver); |
David S. Miller | e21e245 | 2008-08-29 22:34:14 -0700 | [diff] [blame] | 423 | |
David S. Miller | b5e7ae5 | 2006-03-17 13:23:56 -0800 | [diff] [blame] | 424 | MODULE_LICENSE("GPL"); |