This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / arm / common / locomo.c
1 /*
2  * linux/arch/arm/common/locomo.c
3  *
4  * Sharp LoCoMo support
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This file contains all generic LoCoMo support.
11  *
12  * All initialization functions provided here are intended to be called
13  * from machine specific code with proper arguments when required.
14  *
15  * Based on sa1111.c
16  */
17
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28
29 #include <asm/hardware.h>
30 #include <asm/mach-types.h>
31 #include <asm/io.h>
32 #include <asm/irq.h>
33 #include <asm/mach/irq.h>
34
35 #include <asm/hardware/locomo.h>
36
37 /* the following is the overall data for the locomo chip */
38 struct locomo {
39         struct device *dev;
40         unsigned long phys;
41         unsigned int irq;
42         void *base;
43 };
44
45 struct locomo_dev_info {
46         unsigned long   offset;
47         unsigned long   length;
48         unsigned int    devid;
49         unsigned int    irq[1];
50         const char *    name;
51 };
52
53 static struct locomo_dev_info locomo_devices[] = {
54 };
55
56
57 /** LoCoMo interrupt handling stuff.
58  * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
59  * that is, there is only one real hardware interrupt
60  * we determine which interrupt it is by reading some IO memory.
61  * We have two levels of expansion, first in the handler for the
62  * hardware interrupt we generate an interrupt
63  * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
64  *
65  * hardware irq reads LOCOMO_ICR & 0x0f00
66  *   IRQ_LOCOMO_KEY_BASE
67  *   IRQ_LOCOMO_GPIO_BASE
68  *   IRQ_LOCOMO_LT_BASE
69  *   IRQ_LOCOMO_SPI_BASE
70  * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
71  *   IRQ_LOCOMO_KEY
72  * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
73  *   IRQ_LOCOMO_GPIO[0-15]
74  * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
75  *   IRQ_LOCOMO_LT
76  * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
77  *   IRQ_LOCOMO_SPI_RFR
78  *   IRQ_LOCOMO_SPI_RFW
79  *   IRQ_LOCOMO_SPI_OVRN
80  *   IRQ_LOCOMO_SPI_TEND
81  */
82
83 #define LOCOMO_IRQ_START        (IRQ_LOCOMO_KEY_BASE)
84 #define LOCOMO_IRQ_KEY_START    (IRQ_LOCOMO_KEY)
85 #define LOCOMO_IRQ_GPIO_START   (IRQ_LOCOMO_GPIO0)
86 #define LOCOMO_IRQ_LT_START     (IRQ_LOCOMO_LT)
87 #define LOCOMO_IRQ_SPI_START    (IRQ_LOCOMO_SPI_RFR)
88
89 static void locomo_handler(unsigned int irq, struct irqdesc *desc,
90                         struct pt_regs *regs)
91 {
92         int req, i;
93         struct irqdesc *d;
94         void *mapbase = get_irq_chipdata(irq);
95
96         /* Acknowledge the parent IRQ */
97         desc->chip->ack(irq);
98
99         /* check why this interrupt was generated */
100         req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
101
102         if (req) {
103                 /* generate the next interrupt(s) */
104                 irq = LOCOMO_IRQ_START;
105                 d = irq_desc + irq;
106                 for (i = 0; i <= 3; i++, d++, irq++) {
107                         if (req & (0x0100 << i)) {
108                                 d->handle(irq, d, regs);
109                         }
110
111                 }
112         }
113 }
114
115 static void locomo_ack_irq(unsigned int irq)
116 {
117 }
118
119 static void locomo_mask_irq(unsigned int irq)
120 {
121         void *mapbase = get_irq_chipdata(irq);
122         unsigned int r;
123         r = locomo_readl(mapbase + LOCOMO_ICR);
124         r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
125         locomo_writel(r, mapbase + LOCOMO_ICR);
126 }
127
128 static void locomo_unmask_irq(unsigned int irq)
129 {
130         void *mapbase = get_irq_chipdata(irq);
131         unsigned int r;
132         r = locomo_readl(mapbase + LOCOMO_ICR);
133         r |= (0x0010 << (irq - LOCOMO_IRQ_START));
134         locomo_writel(r, mapbase + LOCOMO_ICR);
135 }
136
137 static struct irqchip locomo_chip = {
138         .ack    = locomo_ack_irq,
139         .mask   = locomo_mask_irq,
140         .unmask = locomo_unmask_irq,
141 };
142
143 static void locomo_key_handler(unsigned int irq, struct irqdesc *desc,
144                             struct pt_regs *regs)
145 {
146         struct irqdesc *d;
147         void *mapbase = get_irq_chipdata(irq);
148
149         if (locomo_readl(mapbase + LOCOMO_KIC) & 0x0001) {
150                 d = irq_desc + LOCOMO_IRQ_KEY_START;
151                 d->handle(LOCOMO_IRQ_KEY_START, d, regs);
152         }
153 }
154
155 static void locomo_key_ack_irq(unsigned int irq)
156 {
157         void *mapbase = get_irq_chipdata(irq);
158         unsigned int r;
159         r = locomo_readl(mapbase + LOCOMO_KIC);
160         r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
161         locomo_writel(r, mapbase + LOCOMO_KIC);
162 }
163
164 static void locomo_key_mask_irq(unsigned int irq)
165 {
166         void *mapbase = get_irq_chipdata(irq);
167         unsigned int r;
168         r = locomo_readl(mapbase + LOCOMO_KIC);
169         r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
170         locomo_writel(r, mapbase + LOCOMO_KIC);
171 }
172
173 static void locomo_key_unmask_irq(unsigned int irq)
174 {
175         void *mapbase = get_irq_chipdata(irq);
176         unsigned int r;
177         r = locomo_readl(mapbase + LOCOMO_KIC);
178         r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
179         locomo_writel(r, mapbase + LOCOMO_KIC);
180 }
181
182 static struct irqchip locomo_key_chip = {
183         .ack    = locomo_key_ack_irq,
184         .mask   = locomo_key_mask_irq,
185         .unmask = locomo_key_unmask_irq,
186 };
187
188 static void locomo_gpio_handler(unsigned int irq, struct irqdesc *desc,
189                              struct pt_regs *regs)
190 {
191         int req, i;
192         struct irqdesc *d;
193         void *mapbase = get_irq_chipdata(irq);
194
195         req =   locomo_readl(mapbase + LOCOMO_GIR) &
196                 locomo_readl(mapbase + LOCOMO_GPD) &
197                 0xffff;
198
199         if (req) {
200                 irq = LOCOMO_IRQ_GPIO_START;
201                 d = irq_desc + LOCOMO_IRQ_GPIO_START;
202                 for (i = 0; i <= 15; i++, irq++, d++) {
203                         if (req & (0x0001 << i)) {
204                                 d->handle(irq, d, regs);
205                         }
206                 }
207         }
208 }
209
210 static void locomo_gpio_ack_irq(unsigned int irq)
211 {
212         void *mapbase = get_irq_chipdata(irq);
213         unsigned int r;
214         r = locomo_readl(mapbase + LOCOMO_GWE);
215         r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
216         locomo_writel(r, mapbase + LOCOMO_GWE);
217
218         r = locomo_readl(mapbase + LOCOMO_GIS);
219         r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
220         locomo_writel(r, mapbase + LOCOMO_GIS);
221
222         r = locomo_readl(mapbase + LOCOMO_GWE);
223         r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
224         locomo_writel(r, mapbase + LOCOMO_GWE);
225 }
226
227 static void locomo_gpio_mask_irq(unsigned int irq)
228 {
229         void *mapbase = get_irq_chipdata(irq);
230         unsigned int r;
231         r = locomo_readl(mapbase + LOCOMO_GIE);
232         r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
233         locomo_writel(r, mapbase + LOCOMO_GIE);
234 }
235
236 static void locomo_gpio_unmask_irq(unsigned int irq)
237 {
238         void *mapbase = get_irq_chipdata(irq);
239         unsigned int r;
240         r = locomo_readl(mapbase + LOCOMO_GIE);
241         r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
242         locomo_writel(r, mapbase + LOCOMO_GIE);
243 }
244
245 static struct irqchip locomo_gpio_chip = {
246         .ack    = locomo_gpio_ack_irq,
247         .mask   = locomo_gpio_mask_irq,
248         .unmask = locomo_gpio_unmask_irq,
249 };
250
251 static void locomo_lt_handler(unsigned int irq, struct irqdesc *desc,
252                            struct pt_regs *regs)
253 {
254         struct irqdesc *d;
255         void *mapbase = get_irq_chipdata(irq);
256
257         if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
258                 d = irq_desc + LOCOMO_IRQ_LT_START;
259                 d->handle(LOCOMO_IRQ_LT_START, d, regs);
260         }
261 }
262
263 static void locomo_lt_ack_irq(unsigned int irq)
264 {
265         void *mapbase = get_irq_chipdata(irq);
266         unsigned int r;
267         r = locomo_readl(mapbase + LOCOMO_LTINT);
268         r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
269         locomo_writel(r, mapbase + LOCOMO_LTINT);
270 }
271
272 static void locomo_lt_mask_irq(unsigned int irq)
273 {
274         void *mapbase = get_irq_chipdata(irq);
275         unsigned int r;
276         r = locomo_readl(mapbase + LOCOMO_LTINT);
277         r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
278         locomo_writel(r, mapbase + LOCOMO_LTINT);
279 }
280
281 static void locomo_lt_unmask_irq(unsigned int irq)
282 {
283         void *mapbase = get_irq_chipdata(irq);
284         unsigned int r;
285         r = locomo_readl(mapbase + LOCOMO_LTINT);
286         r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
287         locomo_writel(r, mapbase + LOCOMO_LTINT);
288 }
289
290 static struct irqchip locomo_lt_chip = {
291         .ack    = locomo_lt_ack_irq,
292         .mask   = locomo_lt_mask_irq,
293         .unmask = locomo_lt_unmask_irq,
294 };
295
296 static void locomo_spi_handler(unsigned int irq, struct irqdesc *desc,
297                             struct pt_regs *regs)
298 {
299         int req, i;
300         struct irqdesc *d;
301         void *mapbase = get_irq_chipdata(irq);
302
303         req = locomo_readl(mapbase + LOCOMO_SPIIR) & 0x000F;
304         if (req) {
305                 irq = LOCOMO_IRQ_SPI_START;
306                 d = irq_desc + irq;
307
308                 for (i = 0; i <= 3; i++, irq++, d++) {
309                         if (req & (0x0001 << i)) {
310                                 d->handle(irq, d, regs);
311                         }
312                 }
313         }
314 }
315
316 static void locomo_spi_ack_irq(unsigned int irq)
317 {
318         void *mapbase = get_irq_chipdata(irq);
319         unsigned int r;
320         r = locomo_readl(mapbase + LOCOMO_SPIWE);
321         r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
322         locomo_writel(r, mapbase + LOCOMO_SPIWE);
323
324         r = locomo_readl(mapbase + LOCOMO_SPIIS);
325         r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
326         locomo_writel(r, mapbase + LOCOMO_SPIIS);
327
328         r = locomo_readl(mapbase + LOCOMO_SPIWE);
329         r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
330         locomo_writel(r, mapbase + LOCOMO_SPIWE);
331 }
332
333 static void locomo_spi_mask_irq(unsigned int irq)
334 {
335         void *mapbase = get_irq_chipdata(irq);
336         unsigned int r;
337         r = locomo_readl(mapbase + LOCOMO_SPIIE);
338         r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
339         locomo_writel(r, mapbase + LOCOMO_SPIIE);
340 }
341
342 static void locomo_spi_unmask_irq(unsigned int irq)
343 {
344         void *mapbase = get_irq_chipdata(irq);
345         unsigned int r;
346         r = locomo_readl(mapbase + LOCOMO_SPIIE);
347         r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
348         locomo_writel(r, mapbase + LOCOMO_SPIIE);
349 }
350
351 static struct irqchip locomo_spi_chip = {
352         .ack    = locomo_spi_ack_irq,
353         .mask   = locomo_spi_mask_irq,
354         .unmask = locomo_spi_unmask_irq,
355 };
356
357 static void locomo_setup_irq(struct locomo *lchip)
358 {
359         int irq;
360         void *irqbase = lchip->base;
361
362         /*
363          * Install handler for IRQ_LOCOMO_HW.
364          */
365         set_irq_type(lchip->irq, IRQT_FALLING);
366         set_irq_chipdata(lchip->irq, irqbase);
367         set_irq_chained_handler(lchip->irq, locomo_handler);
368
369         /* Install handlers for IRQ_LOCOMO_*_BASE */
370         set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
371         set_irq_chipdata(IRQ_LOCOMO_KEY_BASE, irqbase);
372         set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
373         set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
374
375         set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
376         set_irq_chipdata(IRQ_LOCOMO_GPIO_BASE, irqbase);
377         set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
378         set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
379
380         set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
381         set_irq_chipdata(IRQ_LOCOMO_LT_BASE, irqbase);
382         set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
383         set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
384
385         set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
386         set_irq_chipdata(IRQ_LOCOMO_SPI_BASE, irqbase);
387         set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
388         set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
389
390         /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
391         set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
392         set_irq_chipdata(LOCOMO_IRQ_KEY_START, irqbase);
393         set_irq_handler(LOCOMO_IRQ_KEY_START, do_edge_IRQ);
394         set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
395
396         /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
397         for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
398                 set_irq_chip(irq, &locomo_gpio_chip);
399                 set_irq_chipdata(irq, irqbase);
400                 set_irq_handler(irq, do_edge_IRQ);
401                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
402         }
403
404         /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
405         set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
406         set_irq_chipdata(LOCOMO_IRQ_LT_START, irqbase);
407         set_irq_handler(LOCOMO_IRQ_LT_START, do_edge_IRQ);
408         set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
409
410         /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
411         for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
412                 set_irq_chip(irq, &locomo_spi_chip);
413                 set_irq_chipdata(irq, irqbase);
414                 set_irq_handler(irq, do_edge_IRQ);
415                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
416         }
417 }
418
419
420 static void locomo_dev_release(struct device *_dev)
421 {
422         struct locomo_dev *dev = LOCOMO_DEV(_dev);
423
424         release_resource(&dev->res);
425         kfree(dev);
426 }
427
428 static int
429 locomo_init_one_child(struct locomo *lchip, struct resource *parent,
430                       struct locomo_dev_info *info)
431 {
432         struct locomo_dev *dev;
433         int ret;
434
435         dev = kmalloc(sizeof(struct locomo_dev), GFP_KERNEL);
436         if (!dev) {
437                 ret = -ENOMEM;
438                 goto out;
439         }
440         memset(dev, 0, sizeof(struct locomo_dev));
441
442         strncpy(dev->dev.bus_id,info->name,sizeof(dev->dev.bus_id));
443         /*
444          * If the parent device has a DMA mask associated with it,
445          * propagate it down to the children.
446          */
447         if (lchip->dev->dma_mask) {
448                 dev->dma_mask = *lchip->dev->dma_mask;
449                 dev->dev.dma_mask = &dev->dma_mask;
450         }
451
452         dev->devid       = info->devid;
453         dev->dev.parent  = lchip->dev;
454         dev->dev.bus     = &locomo_bus_type;
455         dev->dev.release = locomo_dev_release;
456         dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
457         dev->res.start   = lchip->phys + info->offset;
458         dev->res.end     = dev->res.start + info->length;
459         dev->res.name    = dev->dev.bus_id;
460         dev->res.flags   = IORESOURCE_MEM;
461         dev->mapbase     = lchip->base + info->offset;
462         memmove(dev->irq, info->irq, sizeof(dev->irq));
463
464         if (info->length) {
465                 ret = request_resource(parent, &dev->res);
466                 if (ret) {
467                         printk("LoCoMo: failed to allocate resource for %s\n",
468                                 dev->res.name);
469                         goto out;
470                 }
471         }
472
473         ret = device_register(&dev->dev);
474         if (ret) {
475                 release_resource(&dev->res);
476  out:
477                 kfree(dev);
478         }
479         return ret;
480 }
481
482 /**
483  *      locomo_probe - probe for a single LoCoMo chip.
484  *      @phys_addr: physical address of device.
485  *
486  *      Probe for a LoCoMo chip.  This must be called
487  *      before any other locomo-specific code.
488  *
489  *      Returns:
490  *      %-ENODEV        device not found.
491  *      %-EBUSY         physical address already marked in-use.
492  *      %0              successful.
493  */
494 static int
495 __locomo_probe(struct device *me, struct resource *mem, int irq)
496 {
497         struct locomo *lchip;
498         unsigned long r;
499         int i, ret = -ENODEV;
500
501         lchip = kmalloc(sizeof(struct locomo), GFP_KERNEL);
502         if (!lchip)
503                 return -ENOMEM;
504
505         memset(lchip, 0, sizeof(struct locomo));
506
507         lchip->dev = me;
508         dev_set_drvdata(lchip->dev, lchip);
509
510         lchip->phys = mem->start;
511         lchip->irq = irq;
512
513         /*
514          * Map the whole region.  This also maps the
515          * registers for our children.
516          */
517         lchip->base = ioremap(mem->start, PAGE_SIZE);
518         if (!lchip->base) {
519                 ret = -ENOMEM;
520                 goto out;
521         }
522
523         /* locomo initialize */
524         locomo_writel(0, lchip->base + LOCOMO_ICR);
525         /* KEYBOARD */
526         locomo_writel(0, lchip->base + LOCOMO_KIC);
527
528         /* GPIO */
529         locomo_writel(0, lchip->base + LOCOMO_GPO);
530         locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
531                         , lchip->base + LOCOMO_GPE);
532         locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
533                         , lchip->base + LOCOMO_GPD);
534         locomo_writel(0, lchip->base + LOCOMO_GIE);
535
536         /* FrontLight */
537         locomo_writel(0, lchip->base + LOCOMO_ALS);
538         locomo_writel(0, lchip->base + LOCOMO_ALD);
539         /* Longtime timer */
540         locomo_writel(0, lchip->base + LOCOMO_LTINT);
541         /* SPI */
542         locomo_writel(0, lchip->base + LOCOMO_SPIIE);
543
544         locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
545         r = locomo_readl(lchip->base + LOCOMO_ASD);
546         r |= 0x8000;
547         locomo_writel(r, lchip->base + LOCOMO_ASD);
548
549         locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
550         r = locomo_readl(lchip->base + LOCOMO_HSD);
551         r |= 0x8000;
552         locomo_writel(r, lchip->base + LOCOMO_HSD);
553
554         locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
555
556         /* XON */
557         locomo_writel(0x80, lchip->base + LOCOMO_TADC);
558         udelay(1000);
559         /* CLK9MEN */
560         r = locomo_readl(lchip->base + LOCOMO_TADC);
561         r |= 0x10;
562         locomo_writel(r, lchip->base + LOCOMO_TADC);
563         udelay(100);
564
565         /* init DAC */
566         r = locomo_readl(lchip->base + LOCOMO_DAC);
567         r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
568         locomo_writel(r, lchip->base + LOCOMO_DAC);
569
570         r = locomo_readl(lchip->base + LOCOMO_VER);
571         printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
572
573         /*
574          * The interrupt controller must be initialised before any
575          * other device to ensure that the interrupts are available.
576          */
577         if (lchip->irq != NO_IRQ)
578                 locomo_setup_irq(lchip);
579
580         for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
581                 locomo_init_one_child(lchip, mem, &locomo_devices[i]);
582
583         return 0;
584
585  out:
586         kfree(lchip);
587         return ret;
588 }
589
590 static void __locomo_remove(struct locomo *lchip)
591 {
592         struct list_head *l, *n;
593
594         list_for_each_safe(l, n, &lchip->dev->children) {
595                 struct device *d = list_to_dev(l);
596
597                 device_unregister(d);
598         }
599
600         if (lchip->irq != NO_IRQ) {
601                 set_irq_chained_handler(lchip->irq, NULL);
602                 set_irq_data(lchip->irq, NULL);
603         }
604
605         iounmap(lchip->base);
606         kfree(lchip);
607 }
608
609 static int locomo_probe(struct device *dev)
610 {
611         struct platform_device *pdev = to_platform_device(dev);
612         struct resource *mem = NULL, *irq = NULL;
613         int i;
614
615         for (i = 0; i < pdev->num_resources; i++) {
616                 if (pdev->resource[i].flags & IORESOURCE_MEM)
617                         mem = &pdev->resource[i];
618                 if (pdev->resource[i].flags & IORESOURCE_IRQ)
619                         irq = &pdev->resource[i];
620         }
621
622         return __locomo_probe(dev, mem, irq ? irq->start : NO_IRQ);
623 }
624
625 static int locomo_remove(struct device *dev)
626 {
627         struct locomo *lchip = dev_get_drvdata(dev);
628
629         if (lchip) {
630                 __locomo_remove(lchip);
631                 dev_set_drvdata(dev, NULL);
632
633                 kfree(dev->saved_state);
634                 dev->saved_state = NULL;
635         }
636
637         return 0;
638 }
639
640 /*
641  *      Not sure if this should be on the system bus or not yet.
642  *      We really want some way to register a system device at
643  *      the per-machine level, and then have this driver pick
644  *      up the registered devices.
645  */
646 static struct device_driver locomo_device_driver = {
647         .name           = "locomo",
648         .bus            = &platform_bus_type,
649         .probe          = locomo_probe,
650         .remove         = locomo_remove,
651 };
652
653 /*
654  *      Get the parent device driver (us) structure
655  *      from a child function device
656  */
657 static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
658 {
659         return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
660 }
661
662 /*
663  *      LoCoMo "Register Access Bus."
664  *
665  *      We model this as a regular bus type, and hang devices directly
666  *      off this.
667  */
668 static int locomo_match(struct device *_dev, struct device_driver *_drv)
669 {
670         struct locomo_dev *dev = LOCOMO_DEV(_dev);
671         struct locomo_driver *drv = LOCOMO_DRV(_drv);
672
673         return dev->devid == drv->devid;
674 }
675
676 static int locomo_bus_suspend(struct device *dev, u32 state)
677 {
678         struct locomo_dev *ldev = LOCOMO_DEV(dev);
679         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
680         int ret = 0;
681
682         if (drv && drv->suspend)
683                 ret = drv->suspend(ldev, state);
684         return ret;
685 }
686
687 static int locomo_bus_resume(struct device *dev)
688 {
689         struct locomo_dev *ldev = LOCOMO_DEV(dev);
690         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
691         int ret = 0;
692
693         if (drv && drv->resume)
694                 ret = drv->resume(ldev);
695         return ret;
696 }
697
698 static int locomo_bus_probe(struct device *dev)
699 {
700         struct locomo_dev *ldev = LOCOMO_DEV(dev);
701         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
702         int ret = -ENODEV;
703
704         if (drv->probe)
705                 ret = drv->probe(ldev);
706         return ret;
707 }
708
709 static int locomo_bus_remove(struct device *dev)
710 {
711         struct locomo_dev *ldev = LOCOMO_DEV(dev);
712         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
713         int ret = 0;
714
715         if (drv->remove)
716                 ret = drv->remove(ldev);
717         return ret;
718 }
719
720 struct bus_type locomo_bus_type = {
721         .name           = "locomo-bus",
722         .match          = locomo_match,
723         .suspend        = locomo_bus_suspend,
724         .resume         = locomo_bus_resume,
725 };
726
727 int locomo_driver_register(struct locomo_driver *driver)
728 {
729         driver->drv.probe = locomo_bus_probe;
730         driver->drv.remove = locomo_bus_remove;
731         driver->drv.bus = &locomo_bus_type;
732         return driver_register(&driver->drv);
733 }
734
735 void locomo_driver_unregister(struct locomo_driver *driver)
736 {
737         driver_unregister(&driver->drv);
738 }
739
740 static int __init locomo_init(void)
741 {
742         int ret = bus_register(&locomo_bus_type);
743         if (ret == 0)
744                 driver_register(&locomo_device_driver);
745         return ret;
746 }
747
748 static void __exit locomo_exit(void)
749 {
750         driver_unregister(&locomo_device_driver);
751         bus_unregister(&locomo_bus_type);
752 }
753
754 module_init(locomo_init);
755 module_exit(locomo_exit);
756
757 MODULE_DESCRIPTION("Sharp LoCoMo core driver");
758 MODULE_LICENSE("GPL");
759 MODULE_AUTHOR("John Lenz <jelenz@students.wisc.edu>");
760
761 EXPORT_SYMBOL(locomo_driver_register);
762 EXPORT_SYMBOL(locomo_driver_unregister);