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