PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / sbus / char / bbc_i2c.c
blobc7763e482eb235a12d4b48887070ec88154580ff
1 /* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
2 * platforms.
4 * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
5 */
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/interrupt.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <asm/bbc.h>
18 #include <asm/io.h>
20 #include "bbc_i2c.h"
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
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)
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
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.
53 static void set_device_claimage(struct bbc_i2c_bus *bp, struct platform_device *op, int val)
55 int i;
57 for (i = 0; i < NUM_CHILDREN; i++) {
58 if (bp->devs[i].device == op) {
59 bp->devs[i].client_claimed = val;
60 return;
65 #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
66 #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
68 struct platform_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index)
70 struct platform_device *op = NULL;
71 int curidx = 0, i;
73 for (i = 0; i < NUM_CHILDREN; i++) {
74 if (!(op = bp->devs[i].device))
75 break;
76 if (curidx == index)
77 goto out;
78 op = NULL;
79 curidx++;
82 out:
83 if (curidx == index)
84 return op;
85 return NULL;
88 struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct platform_device *op)
90 struct bbc_i2c_client *client;
91 const u32 *reg;
93 client = kzalloc(sizeof(*client), GFP_KERNEL);
94 if (!client)
95 return NULL;
96 client->bp = bp;
97 client->op = op;
99 reg = of_get_property(op->dev.of_node, "reg", NULL);
100 if (!reg) {
101 kfree(client);
102 return NULL;
105 client->bus = reg[0];
106 client->address = reg[1];
108 claim_device(bp, op);
110 return client;
113 void bbc_i2c_detach(struct bbc_i2c_client *client)
115 struct bbc_i2c_bus *bp = client->bp;
116 struct platform_device *op = client->op;
118 release_device(bp, op);
119 kfree(client);
122 static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
124 DECLARE_WAITQUEUE(wait, current);
125 int limit = 32;
126 int ret = 1;
128 bp->waiting = 1;
129 add_wait_queue(&bp->wq, &wait);
130 while (limit-- > 0) {
131 long val;
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) {
139 ret = 0;
140 break;
143 remove_wait_queue(&bp->wq, &wait);
144 bp->waiting = 0;
146 return ret;
149 int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
151 struct bbc_i2c_bus *bp = client->bp;
152 int address = client->address;
153 u8 status;
154 int ret = -1;
156 if (bp->i2c_bussel_reg != NULL)
157 writeb(client->bus, bp->i2c_bussel_reg);
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;
164 writeb(off, bp->i2c_control_regs + 0x1);
165 if (wait_for_pin(bp, &status) ||
166 (status & I2C_PCF_LRB) != 0)
167 goto out;
169 writeb(val, bp->i2c_control_regs + 0x1);
170 if (wait_for_pin(bp, &status))
171 goto out;
173 ret = 0;
175 out:
176 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
177 return ret;
180 int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
182 struct bbc_i2c_bus *bp = client->bp;
183 unsigned char address = client->address, status;
184 int ret = -1;
186 if (bp->i2c_bussel_reg != NULL)
187 writeb(client->bus, bp->i2c_bussel_reg);
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;
194 writeb(off, bp->i2c_control_regs + 0x1);
195 if (wait_for_pin(bp, &status) ||
196 (status & I2C_PCF_LRB) != 0)
197 goto out;
199 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
201 address |= 0x1; /* READ */
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;
208 /* Set PIN back to one so the device sends the first
209 * byte.
211 (void) readb(bp->i2c_control_regs + 0x1);
212 if (wait_for_pin(bp, &status))
213 goto out;
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;
220 ret = 0;
222 out:
223 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
224 (void) readb(bp->i2c_control_regs + 0x1);
226 return ret;
229 int bbc_i2c_write_buf(struct bbc_i2c_client *client,
230 char *buf, int len, int off)
232 int ret = 0;
234 while (len > 0) {
235 ret = bbc_i2c_writeb(client, *buf, off);
236 if (ret < 0)
237 break;
238 len--;
239 buf++;
240 off++;
242 return ret;
245 int bbc_i2c_read_buf(struct bbc_i2c_client *client,
246 char *buf, int len, int off)
248 int ret = 0;
250 while (len > 0) {
251 ret = bbc_i2c_readb(client, buf, off);
252 if (ret < 0)
253 break;
254 len--;
255 buf++;
256 off++;
259 return ret;
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);
270 static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
272 struct bbc_i2c_bus *bp = dev_id;
274 /* PIN going from set to clear is the only event which
275 * makes the i2c assert an interrupt.
277 if (bp->waiting &&
278 !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
279 wake_up_interruptible(&bp->wq);
281 return IRQ_HANDLED;
284 static void reset_one_i2c(struct bbc_i2c_bus *bp)
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);
293 static struct bbc_i2c_bus * attach_one_i2c(struct platform_device *op, int index)
295 struct bbc_i2c_bus *bp;
296 struct device_node *dp;
297 int entry;
299 bp = kzalloc(sizeof(*bp), GFP_KERNEL);
300 if (!bp)
301 return NULL;
303 bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs");
304 if (!bp->i2c_control_regs)
305 goto fail;
307 bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel");
308 if (!bp->i2c_bussel_reg)
309 goto fail;
311 bp->waiting = 0;
312 init_waitqueue_head(&bp->wq);
313 if (request_irq(op->archdata.irqs[0], bbc_i2c_interrupt,
314 IRQF_SHARED, "bbc_i2c", bp))
315 goto fail;
317 bp->index = index;
318 bp->op = op;
320 spin_lock_init(&bp->lock);
322 entry = 0;
323 for (dp = op->dev.of_node->child;
324 dp && entry < 8;
325 dp = dp->sibling, entry++) {
326 struct platform_device *child_op;
328 child_op = of_find_device_by_node(dp);
329 bp->devs[entry].device = child_op;
330 bp->devs[entry].client_claimed = 0;
333 writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
334 bp->own = readb(bp->i2c_control_regs + 0x01);
335 writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
336 bp->clock = readb(bp->i2c_control_regs + 0x01);
338 printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
339 bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
341 reset_one_i2c(bp);
343 return bp;
345 fail:
346 if (bp->i2c_bussel_reg)
347 of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1);
348 if (bp->i2c_control_regs)
349 of_iounmap(&op->resource[0], bp->i2c_control_regs, 2);
350 kfree(bp);
351 return NULL;
354 extern int bbc_envctrl_init(struct bbc_i2c_bus *bp);
355 extern void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp);
357 static int bbc_i2c_probe(struct platform_device *op)
359 struct bbc_i2c_bus *bp;
360 int err, index = 0;
362 bp = attach_one_i2c(op, index);
363 if (!bp)
364 return -EINVAL;
366 err = bbc_envctrl_init(bp);
367 if (err) {
368 free_irq(op->archdata.irqs[0], bp);
369 if (bp->i2c_bussel_reg)
370 of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
371 if (bp->i2c_control_regs)
372 of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
373 kfree(bp);
374 } else {
375 dev_set_drvdata(&op->dev, bp);
378 return err;
381 static int bbc_i2c_remove(struct platform_device *op)
383 struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev);
385 bbc_envctrl_cleanup(bp);
387 free_irq(op->archdata.irqs[0], bp);
389 if (bp->i2c_bussel_reg)
390 of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
391 if (bp->i2c_control_regs)
392 of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
394 kfree(bp);
396 return 0;
399 static const struct of_device_id bbc_i2c_match[] = {
401 .name = "i2c",
402 .compatible = "SUNW,bbc-i2c",
406 MODULE_DEVICE_TABLE(of, bbc_i2c_match);
408 static struct platform_driver bbc_i2c_driver = {
409 .driver = {
410 .name = "bbc_i2c",
411 .owner = THIS_MODULE,
412 .of_match_table = bbc_i2c_match,
414 .probe = bbc_i2c_probe,
415 .remove = bbc_i2c_remove,
418 module_platform_driver(bbc_i2c_driver);
420 MODULE_LICENSE("GPL");