Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / 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 /* M62332 output channel selection */
38 #define M62332_EVR_CH   1       /* M62332 volume channel number  */
39                                 /*   0 : CH.1 , 1 : CH. 2        */
40 /* DAC send data */
41 #define M62332_SLAVE_ADDR       0x4e    /* Slave address  */
42 #define M62332_W_BIT            0x00    /* W bit (0 only) */
43 #define M62332_SUB_ADDR         0x00    /* Sub address    */
44 #define M62332_A_BIT            0x00    /* A bit (0 only) */
45
46 /* DAC setup and hold times (expressed in us) */
47 #define DAC_BUS_FREE_TIME       5       /*   4.7 us */
48 #define DAC_START_SETUP_TIME    5       /*   4.7 us */
49 #define DAC_STOP_SETUP_TIME     4       /*   4.0 us */
50 #define DAC_START_HOLD_TIME     5       /*   4.7 us */
51 #define DAC_SCL_LOW_HOLD_TIME   5       /*   4.7 us */
52 #define DAC_SCL_HIGH_HOLD_TIME  4       /*   4.0 us */
53 #define DAC_DATA_SETUP_TIME     1       /*   250 ns */
54 #define DAC_DATA_HOLD_TIME      1       /*   300 ns */
55 #define DAC_LOW_SETUP_TIME      1       /*   300 ns */
56 #define DAC_HIGH_SETUP_TIME     1       /*  1000 ns */
57
58 /* the following is the overall data for the locomo chip */
59 struct locomo {
60         struct device *dev;
61         unsigned long phys;
62         unsigned int irq;
63         spinlock_t lock;
64         void *base;
65 };
66
67 struct locomo_dev_info {
68         unsigned long   offset;
69         unsigned long   length;
70         unsigned int    devid;
71         unsigned int    irq[1];
72         const char *    name;
73 };
74
75 /* All the locomo devices.  If offset is non-zero, the mapbase for the
76  * locomo_dev will be set to the chip base plus offset.  If offset is
77  * zero, then the mapbase for the locomo_dev will be set to zero.  An
78  * offset of zero means the device only uses GPIOs or other helper
79  * functions inside this file */
80 static struct locomo_dev_info locomo_devices[] = {
81         {
82                 .devid          = LOCOMO_DEVID_KEYBOARD,
83                 .irq = {
84                         IRQ_LOCOMO_KEY,
85                 },
86                 .name           = "locomo-keyboard",
87                 .offset         = LOCOMO_KEYBOARD,
88                 .length         = 16,
89         },
90         {
91                 .devid          = LOCOMO_DEVID_FRONTLIGHT,
92                 .irq            = {},
93                 .name           = "locomo-frontlight",
94                 .offset         = LOCOMO_FRONTLIGHT,
95                 .length         = 8,
96
97         },
98         {
99                 .devid          = LOCOMO_DEVID_BACKLIGHT,
100                 .irq            = {},
101                 .name           = "locomo-backlight",
102                 .offset         = LOCOMO_BACKLIGHT,
103                 .length         = 8,
104         },
105         {
106                 .devid          = LOCOMO_DEVID_AUDIO,
107                 .irq            = {},
108                 .name           = "locomo-audio",
109                 .offset         = LOCOMO_AUDIO,
110                 .length         = 4,
111         },
112         {
113                 .devid          = LOCOMO_DEVID_LED,
114                 .irq            = {},
115                 .name           = "locomo-led",
116                 .offset         = LOCOMO_LED,
117                 .length         = 8,
118         },
119         {
120                 .devid          = LOCOMO_DEVID_UART,
121                 .irq            = {},
122                 .name           = "locomo-uart",
123                 .offset         = 0,
124                 .length         = 0,
125         },
126 };
127
128
129 /** LoCoMo interrupt handling stuff.
130  * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
131  * that is, there is only one real hardware interrupt
132  * we determine which interrupt it is by reading some IO memory.
133  * We have two levels of expansion, first in the handler for the
134  * hardware interrupt we generate an interrupt
135  * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
136  *
137  * hardware irq reads LOCOMO_ICR & 0x0f00
138  *   IRQ_LOCOMO_KEY_BASE
139  *   IRQ_LOCOMO_GPIO_BASE
140  *   IRQ_LOCOMO_LT_BASE
141  *   IRQ_LOCOMO_SPI_BASE
142  * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
143  *   IRQ_LOCOMO_KEY
144  * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
145  *   IRQ_LOCOMO_GPIO[0-15]
146  * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
147  *   IRQ_LOCOMO_LT
148  * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
149  *   IRQ_LOCOMO_SPI_RFR
150  *   IRQ_LOCOMO_SPI_RFW
151  *   IRQ_LOCOMO_SPI_OVRN
152  *   IRQ_LOCOMO_SPI_TEND
153  */
154
155 #define LOCOMO_IRQ_START        (IRQ_LOCOMO_KEY_BASE)
156 #define LOCOMO_IRQ_KEY_START    (IRQ_LOCOMO_KEY)
157 #define LOCOMO_IRQ_GPIO_START   (IRQ_LOCOMO_GPIO0)
158 #define LOCOMO_IRQ_LT_START     (IRQ_LOCOMO_LT)
159 #define LOCOMO_IRQ_SPI_START    (IRQ_LOCOMO_SPI_RFR)
160
161 static void locomo_handler(unsigned int irq, struct irqdesc *desc,
162                         struct pt_regs *regs)
163 {
164         int req, i;
165         struct irqdesc *d;
166         void *mapbase = get_irq_chipdata(irq);
167
168         /* Acknowledge the parent IRQ */
169         desc->chip->ack(irq);
170
171         /* check why this interrupt was generated */
172         req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
173
174         if (req) {
175                 /* generate the next interrupt(s) */
176                 irq = LOCOMO_IRQ_START;
177                 d = irq_desc + irq;
178                 for (i = 0; i <= 3; i++, d++, irq++) {
179                         if (req & (0x0100 << i)) {
180                                 d->handle(irq, d, regs);
181                         }
182
183                 }
184         }
185 }
186
187 static void locomo_ack_irq(unsigned int irq)
188 {
189 }
190
191 static void locomo_mask_irq(unsigned int irq)
192 {
193         void *mapbase = get_irq_chipdata(irq);
194         unsigned int r;
195         r = locomo_readl(mapbase + LOCOMO_ICR);
196         r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
197         locomo_writel(r, mapbase + LOCOMO_ICR);
198 }
199
200 static void locomo_unmask_irq(unsigned int irq)
201 {
202         void *mapbase = get_irq_chipdata(irq);
203         unsigned int r;
204         r = locomo_readl(mapbase + LOCOMO_ICR);
205         r |= (0x0010 << (irq - LOCOMO_IRQ_START));
206         locomo_writel(r, mapbase + LOCOMO_ICR);
207 }
208
209 static struct irqchip locomo_chip = {
210         .ack    = locomo_ack_irq,
211         .mask   = locomo_mask_irq,
212         .unmask = locomo_unmask_irq,
213 };
214
215 static void locomo_key_handler(unsigned int irq, struct irqdesc *desc,
216                             struct pt_regs *regs)
217 {
218         struct irqdesc *d;
219         void *mapbase = get_irq_chipdata(irq);
220
221         if (locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC) & 0x0001) {
222                 d = irq_desc + LOCOMO_IRQ_KEY_START;
223                 d->handle(LOCOMO_IRQ_KEY_START, d, regs);
224         }
225 }
226
227 static void locomo_key_ack_irq(unsigned int irq)
228 {
229         void *mapbase = get_irq_chipdata(irq);
230         unsigned int r;
231         r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
232         r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
233         locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
234 }
235
236 static void locomo_key_mask_irq(unsigned int irq)
237 {
238         void *mapbase = get_irq_chipdata(irq);
239         unsigned int r;
240         r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
241         r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
242         locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
243 }
244
245 static void locomo_key_unmask_irq(unsigned int irq)
246 {
247         void *mapbase = get_irq_chipdata(irq);
248         unsigned int r;
249         r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
250         r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
251         locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
252 }
253
254 static struct irqchip locomo_key_chip = {
255         .ack    = locomo_key_ack_irq,
256         .mask   = locomo_key_mask_irq,
257         .unmask = locomo_key_unmask_irq,
258 };
259
260 static void locomo_gpio_handler(unsigned int irq, struct irqdesc *desc,
261                              struct pt_regs *regs)
262 {
263         int req, i;
264         struct irqdesc *d;
265         void *mapbase = get_irq_chipdata(irq);
266
267         req =   locomo_readl(mapbase + LOCOMO_GIR) &
268                 locomo_readl(mapbase + LOCOMO_GPD) &
269                 0xffff;
270
271         if (req) {
272                 irq = LOCOMO_IRQ_GPIO_START;
273                 d = irq_desc + LOCOMO_IRQ_GPIO_START;
274                 for (i = 0; i <= 15; i++, irq++, d++) {
275                         if (req & (0x0001 << i)) {
276                                 d->handle(irq, d, regs);
277                         }
278                 }
279         }
280 }
281
282 static void locomo_gpio_ack_irq(unsigned int irq)
283 {
284         void *mapbase = get_irq_chipdata(irq);
285         unsigned int r;
286         r = locomo_readl(mapbase + LOCOMO_GWE);
287         r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
288         locomo_writel(r, mapbase + LOCOMO_GWE);
289
290         r = locomo_readl(mapbase + LOCOMO_GIS);
291         r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
292         locomo_writel(r, mapbase + LOCOMO_GIS);
293
294         r = locomo_readl(mapbase + LOCOMO_GWE);
295         r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
296         locomo_writel(r, mapbase + LOCOMO_GWE);
297 }
298
299 static void locomo_gpio_mask_irq(unsigned int irq)
300 {
301         void *mapbase = get_irq_chipdata(irq);
302         unsigned int r;
303         r = locomo_readl(mapbase + LOCOMO_GIE);
304         r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
305         locomo_writel(r, mapbase + LOCOMO_GIE);
306 }
307
308 static void locomo_gpio_unmask_irq(unsigned int irq)
309 {
310         void *mapbase = get_irq_chipdata(irq);
311         unsigned int r;
312         r = locomo_readl(mapbase + LOCOMO_GIE);
313         r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
314         locomo_writel(r, mapbase + LOCOMO_GIE);
315 }
316
317 static struct irqchip locomo_gpio_chip = {
318         .ack    = locomo_gpio_ack_irq,
319         .mask   = locomo_gpio_mask_irq,
320         .unmask = locomo_gpio_unmask_irq,
321 };
322
323 static void locomo_lt_handler(unsigned int irq, struct irqdesc *desc,
324                            struct pt_regs *regs)
325 {
326         struct irqdesc *d;
327         void *mapbase = get_irq_chipdata(irq);
328
329         if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
330                 d = irq_desc + LOCOMO_IRQ_LT_START;
331                 d->handle(LOCOMO_IRQ_LT_START, d, regs);
332         }
333 }
334
335 static void locomo_lt_ack_irq(unsigned int irq)
336 {
337         void *mapbase = get_irq_chipdata(irq);
338         unsigned int r;
339         r = locomo_readl(mapbase + LOCOMO_LTINT);
340         r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
341         locomo_writel(r, mapbase + LOCOMO_LTINT);
342 }
343
344 static void locomo_lt_mask_irq(unsigned int irq)
345 {
346         void *mapbase = get_irq_chipdata(irq);
347         unsigned int r;
348         r = locomo_readl(mapbase + LOCOMO_LTINT);
349         r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
350         locomo_writel(r, mapbase + LOCOMO_LTINT);
351 }
352
353 static void locomo_lt_unmask_irq(unsigned int irq)
354 {
355         void *mapbase = get_irq_chipdata(irq);
356         unsigned int r;
357         r = locomo_readl(mapbase + LOCOMO_LTINT);
358         r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
359         locomo_writel(r, mapbase + LOCOMO_LTINT);
360 }
361
362 static struct irqchip locomo_lt_chip = {
363         .ack    = locomo_lt_ack_irq,
364         .mask   = locomo_lt_mask_irq,
365         .unmask = locomo_lt_unmask_irq,
366 };
367
368 static void locomo_spi_handler(unsigned int irq, struct irqdesc *desc,
369                             struct pt_regs *regs)
370 {
371         int req, i;
372         struct irqdesc *d;
373         void *mapbase = get_irq_chipdata(irq);
374
375         req = locomo_readl(mapbase + LOCOMO_SPIIR) & 0x000F;
376         if (req) {
377                 irq = LOCOMO_IRQ_SPI_START;
378                 d = irq_desc + irq;
379
380                 for (i = 0; i <= 3; i++, irq++, d++) {
381                         if (req & (0x0001 << i)) {
382                                 d->handle(irq, d, regs);
383                         }
384                 }
385         }
386 }
387
388 static void locomo_spi_ack_irq(unsigned int irq)
389 {
390         void *mapbase = get_irq_chipdata(irq);
391         unsigned int r;
392         r = locomo_readl(mapbase + LOCOMO_SPIWE);
393         r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
394         locomo_writel(r, mapbase + LOCOMO_SPIWE);
395
396         r = locomo_readl(mapbase + LOCOMO_SPIIS);
397         r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
398         locomo_writel(r, mapbase + LOCOMO_SPIIS);
399
400         r = locomo_readl(mapbase + LOCOMO_SPIWE);
401         r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
402         locomo_writel(r, mapbase + LOCOMO_SPIWE);
403 }
404
405 static void locomo_spi_mask_irq(unsigned int irq)
406 {
407         void *mapbase = get_irq_chipdata(irq);
408         unsigned int r;
409         r = locomo_readl(mapbase + LOCOMO_SPIIE);
410         r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
411         locomo_writel(r, mapbase + LOCOMO_SPIIE);
412 }
413
414 static void locomo_spi_unmask_irq(unsigned int irq)
415 {
416         void *mapbase = get_irq_chipdata(irq);
417         unsigned int r;
418         r = locomo_readl(mapbase + LOCOMO_SPIIE);
419         r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
420         locomo_writel(r, mapbase + LOCOMO_SPIIE);
421 }
422
423 static struct irqchip locomo_spi_chip = {
424         .ack    = locomo_spi_ack_irq,
425         .mask   = locomo_spi_mask_irq,
426         .unmask = locomo_spi_unmask_irq,
427 };
428
429 static void locomo_setup_irq(struct locomo *lchip)
430 {
431         int irq;
432         void *irqbase = lchip->base;
433
434         /*
435          * Install handler for IRQ_LOCOMO_HW.
436          */
437         set_irq_type(lchip->irq, IRQT_FALLING);
438         set_irq_chipdata(lchip->irq, irqbase);
439         set_irq_chained_handler(lchip->irq, locomo_handler);
440
441         /* Install handlers for IRQ_LOCOMO_*_BASE */
442         set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
443         set_irq_chipdata(IRQ_LOCOMO_KEY_BASE, irqbase);
444         set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
445         set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
446
447         set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
448         set_irq_chipdata(IRQ_LOCOMO_GPIO_BASE, irqbase);
449         set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
450         set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
451
452         set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
453         set_irq_chipdata(IRQ_LOCOMO_LT_BASE, irqbase);
454         set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
455         set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
456
457         set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
458         set_irq_chipdata(IRQ_LOCOMO_SPI_BASE, irqbase);
459         set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
460         set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
461
462         /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
463         set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
464         set_irq_chipdata(LOCOMO_IRQ_KEY_START, irqbase);
465         set_irq_handler(LOCOMO_IRQ_KEY_START, do_edge_IRQ);
466         set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
467
468         /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
469         for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
470                 set_irq_chip(irq, &locomo_gpio_chip);
471                 set_irq_chipdata(irq, irqbase);
472                 set_irq_handler(irq, do_edge_IRQ);
473                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
474         }
475
476         /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
477         set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
478         set_irq_chipdata(LOCOMO_IRQ_LT_START, irqbase);
479         set_irq_handler(LOCOMO_IRQ_LT_START, do_edge_IRQ);
480         set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
481
482         /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
483         for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
484                 set_irq_chip(irq, &locomo_spi_chip);
485                 set_irq_chipdata(irq, irqbase);
486                 set_irq_handler(irq, do_edge_IRQ);
487                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
488         }
489 }
490
491
492 static void locomo_dev_release(struct device *_dev)
493 {
494         struct locomo_dev *dev = LOCOMO_DEV(_dev);
495
496         kfree(dev);
497 }
498
499 static int
500 locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
501 {
502         struct locomo_dev *dev;
503         int ret;
504
505         dev = kmalloc(sizeof(struct locomo_dev), GFP_KERNEL);
506         if (!dev) {
507                 ret = -ENOMEM;
508                 goto out;
509         }
510         memset(dev, 0, sizeof(struct locomo_dev));
511
512         strncpy(dev->dev.bus_id,info->name,sizeof(dev->dev.bus_id));
513         /*
514          * If the parent device has a DMA mask associated with it,
515          * propagate it down to the children.
516          */
517         if (lchip->dev->dma_mask) {
518                 dev->dma_mask = *lchip->dev->dma_mask;
519                 dev->dev.dma_mask = &dev->dma_mask;
520         }
521
522         dev->devid       = info->devid;
523         dev->dev.parent  = lchip->dev;
524         dev->dev.bus     = &locomo_bus_type;
525         dev->dev.release = locomo_dev_release;
526         dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
527
528         if (info->offset)
529                 dev->mapbase = lchip->base + info->offset;
530         else
531                 dev->mapbase = 0;
532         dev->length = info->length;
533
534         memmove(dev->irq, info->irq, sizeof(dev->irq));
535
536         ret = device_register(&dev->dev);
537         if (ret) {
538  out:
539                 kfree(dev);
540         }
541         return ret;
542 }
543
544 /**
545  *      locomo_probe - probe for a single LoCoMo chip.
546  *      @phys_addr: physical address of device.
547  *
548  *      Probe for a LoCoMo chip.  This must be called
549  *      before any other locomo-specific code.
550  *
551  *      Returns:
552  *      %-ENODEV        device not found.
553  *      %-EBUSY         physical address already marked in-use.
554  *      %0              successful.
555  */
556 static int
557 __locomo_probe(struct device *me, struct resource *mem, int irq)
558 {
559         struct locomo *lchip;
560         unsigned long r;
561         int i, ret = -ENODEV;
562
563         lchip = kmalloc(sizeof(struct locomo), GFP_KERNEL);
564         if (!lchip)
565                 return -ENOMEM;
566
567         memset(lchip, 0, sizeof(struct locomo));
568
569         spin_lock_init(&lchip->lock);
570
571         lchip->dev = me;
572         dev_set_drvdata(lchip->dev, lchip);
573
574         lchip->phys = mem->start;
575         lchip->irq = irq;
576
577         /*
578          * Map the whole region.  This also maps the
579          * registers for our children.
580          */
581         lchip->base = ioremap(mem->start, PAGE_SIZE);
582         if (!lchip->base) {
583                 ret = -ENOMEM;
584                 goto out;
585         }
586
587         /* locomo initialize */
588         locomo_writel(0, lchip->base + LOCOMO_ICR);
589         /* KEYBOARD */
590         locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
591
592         /* GPIO */
593         locomo_writel(0, lchip->base + LOCOMO_GPO);
594         locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
595                         , lchip->base + LOCOMO_GPE);
596         locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
597                         , lchip->base + LOCOMO_GPD);
598         locomo_writel(0, lchip->base + LOCOMO_GIE);
599
600         /* FrontLight */
601         locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
602         locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
603         /* Longtime timer */
604         locomo_writel(0, lchip->base + LOCOMO_LTINT);
605         /* SPI */
606         locomo_writel(0, lchip->base + LOCOMO_SPIIE);
607
608         locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
609         r = locomo_readl(lchip->base + LOCOMO_ASD);
610         r |= 0x8000;
611         locomo_writel(r, lchip->base + LOCOMO_ASD);
612
613         locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
614         r = locomo_readl(lchip->base + LOCOMO_HSD);
615         r |= 0x8000;
616         locomo_writel(r, lchip->base + LOCOMO_HSD);
617
618         locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
619
620         /* XON */
621         locomo_writel(0x80, lchip->base + LOCOMO_TADC);
622         udelay(1000);
623         /* CLK9MEN */
624         r = locomo_readl(lchip->base + LOCOMO_TADC);
625         r |= 0x10;
626         locomo_writel(r, lchip->base + LOCOMO_TADC);
627         udelay(100);
628
629         /* init DAC */
630         r = locomo_readl(lchip->base + LOCOMO_DAC);
631         r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
632         locomo_writel(r, lchip->base + LOCOMO_DAC);
633
634         r = locomo_readl(lchip->base + LOCOMO_VER);
635         printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
636
637         /*
638          * The interrupt controller must be initialised before any
639          * other device to ensure that the interrupts are available.
640          */
641         if (lchip->irq != NO_IRQ)
642                 locomo_setup_irq(lchip);
643
644         for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
645                 locomo_init_one_child(lchip, &locomo_devices[i]);
646
647         return 0;
648
649  out:
650         kfree(lchip);
651         return ret;
652 }
653
654 static void __locomo_remove(struct locomo *lchip)
655 {
656         struct list_head *l, *n;
657
658         list_for_each_safe(l, n, &lchip->dev->children) {
659                 struct device *d = list_to_dev(l);
660
661                 device_unregister(d);
662         }
663
664         if (lchip->irq != NO_IRQ) {
665                 set_irq_chained_handler(lchip->irq, NULL);
666                 set_irq_data(lchip->irq, NULL);
667         }
668
669         iounmap(lchip->base);
670         kfree(lchip);
671 }
672
673 static int locomo_probe(struct device *dev)
674 {
675         struct platform_device *pdev = to_platform_device(dev);
676         struct resource *mem;
677         int irq;
678
679         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
680         if (!mem)
681                 return -EINVAL;
682         irq = platform_get_irq(pdev, 0);
683
684         return __locomo_probe(dev, mem, irq);
685 }
686
687 static int locomo_remove(struct device *dev)
688 {
689         struct locomo *lchip = dev_get_drvdata(dev);
690
691         if (lchip) {
692                 __locomo_remove(lchip);
693                 dev_set_drvdata(dev, NULL);
694         }
695
696         return 0;
697 }
698
699 /*
700  *      Not sure if this should be on the system bus or not yet.
701  *      We really want some way to register a system device at
702  *      the per-machine level, and then have this driver pick
703  *      up the registered devices.
704  */
705 static struct device_driver locomo_device_driver = {
706         .name           = "locomo",
707         .bus            = &platform_bus_type,
708         .probe          = locomo_probe,
709         .remove         = locomo_remove,
710 };
711
712 /*
713  *      Get the parent device driver (us) structure
714  *      from a child function device
715  */
716 static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
717 {
718         return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
719 }
720
721 void locomo_gpio_set_dir(struct locomo_dev *ldev, unsigned int bits, unsigned int dir)
722 {
723         struct locomo *lchip = locomo_chip_driver(ldev);
724         unsigned long flags;
725         unsigned int r;
726
727         spin_lock_irqsave(&lchip->lock, flags);
728
729         r = locomo_readl(lchip->base + LOCOMO_GPD);
730         r &= ~bits;
731         locomo_writel(r, lchip->base + LOCOMO_GPD);
732
733         r = locomo_readl(lchip->base + LOCOMO_GPE);
734         if (dir)
735                 r |= bits;
736         else
737                 r &= ~bits;
738         locomo_writel(r, lchip->base + LOCOMO_GPE);
739
740         spin_unlock_irqrestore(&lchip->lock, flags);
741 }
742
743 unsigned int locomo_gpio_read_level(struct locomo_dev *ldev, unsigned int bits)
744 {
745         struct locomo *lchip = locomo_chip_driver(ldev);
746         unsigned long flags;
747         unsigned int ret;
748
749         spin_lock_irqsave(&lchip->lock, flags);
750         ret = locomo_readl(lchip->base + LOCOMO_GPL);
751         spin_unlock_irqrestore(&lchip->lock, flags);
752
753         ret &= bits;
754         return ret;
755 }
756
757 unsigned int locomo_gpio_read_output(struct locomo_dev *ldev, unsigned int bits)
758 {
759         struct locomo *lchip = locomo_chip_driver(ldev);
760         unsigned long flags;
761         unsigned int ret;
762
763         spin_lock_irqsave(&lchip->lock, flags);
764         ret = locomo_readl(lchip->base + LOCOMO_GPO);
765         spin_unlock_irqrestore(&lchip->lock, flags);
766
767         ret &= bits;
768         return ret;
769 }
770
771 void locomo_gpio_write(struct locomo_dev *ldev, unsigned int bits, unsigned int set)
772 {
773         struct locomo *lchip = locomo_chip_driver(ldev);
774         unsigned long flags;
775         unsigned int r;
776
777         spin_lock_irqsave(&lchip->lock, flags);
778
779         r = locomo_readl(lchip->base + LOCOMO_GPO);
780         if (set)
781                 r |= bits;
782         else
783                 r &= ~bits;
784         locomo_writel(r, lchip->base + LOCOMO_GPO);
785
786         spin_unlock_irqrestore(&lchip->lock, flags);
787 }
788
789 static void locomo_m62332_sendbit(void *mapbase, int bit)
790 {
791         unsigned int r;
792
793         r = locomo_readl(mapbase + LOCOMO_DAC);
794         r &=  ~(LOCOMO_DAC_SCLOEB);
795         locomo_writel(r, mapbase + LOCOMO_DAC);
796         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
797         udelay(DAC_DATA_HOLD_TIME);     /* 300 nsec */
798         r = locomo_readl(mapbase + LOCOMO_DAC);
799         r &=  ~(LOCOMO_DAC_SCLOEB);
800         locomo_writel(r, mapbase + LOCOMO_DAC);
801         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
802         udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
803
804         if (bit & 1) {
805                 r = locomo_readl(mapbase + LOCOMO_DAC);
806                 r |=  LOCOMO_DAC_SDAOEB;
807                 locomo_writel(r, mapbase + LOCOMO_DAC);
808                 udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
809         } else {
810                 r = locomo_readl(mapbase + LOCOMO_DAC);
811                 r &=  ~(LOCOMO_DAC_SDAOEB);
812                 locomo_writel(r, mapbase + LOCOMO_DAC);
813                 udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
814         }
815
816         udelay(DAC_DATA_SETUP_TIME);    /* 250 nsec */
817         r = locomo_readl(mapbase + LOCOMO_DAC);
818         r |=  LOCOMO_DAC_SCLOEB;
819         locomo_writel(r, mapbase + LOCOMO_DAC);
820         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
821         udelay(DAC_SCL_HIGH_HOLD_TIME); /*  4.0 usec */
822 }
823
824 void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
825 {
826         struct locomo *lchip = locomo_chip_driver(ldev);
827         int i;
828         unsigned char data;
829         unsigned int r;
830         void *mapbase = lchip->base;
831         unsigned long flags;
832
833         spin_lock_irqsave(&lchip->lock, flags);
834
835         /* Start */
836         udelay(DAC_BUS_FREE_TIME);      /* 5.0 usec */
837         r = locomo_readl(mapbase + LOCOMO_DAC);
838         r |=  LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
839         locomo_writel(r, mapbase + LOCOMO_DAC);
840         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
841         udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
842         r = locomo_readl(mapbase + LOCOMO_DAC);
843         r &=  ~(LOCOMO_DAC_SDAOEB);
844         locomo_writel(r, mapbase + LOCOMO_DAC);
845         udelay(DAC_START_HOLD_TIME);    /* 5.0 usec */
846         udelay(DAC_DATA_HOLD_TIME);     /* 300 nsec */
847
848         /* Send slave address and W bit (LSB is W bit) */
849         data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
850         for (i = 1; i <= 8; i++) {
851                 locomo_m62332_sendbit(mapbase, data >> (8 - i));
852         }
853
854         /* Check A bit */
855         r = locomo_readl(mapbase + LOCOMO_DAC);
856         r &=  ~(LOCOMO_DAC_SCLOEB);
857         locomo_writel(r, mapbase + LOCOMO_DAC);
858         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
859         udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
860         r = locomo_readl(mapbase + LOCOMO_DAC);
861         r &=  ~(LOCOMO_DAC_SDAOEB);
862         locomo_writel(r, mapbase + LOCOMO_DAC);
863         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
864         r = locomo_readl(mapbase + LOCOMO_DAC);
865         r |=  LOCOMO_DAC_SCLOEB;
866         locomo_writel(r, mapbase + LOCOMO_DAC);
867         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
868         udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
869         if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) {   /* High is error */
870                 printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
871                 return;
872         }
873
874         /* Send Sub address (LSB is channel select) */
875         /*    channel = 0 : ch1 select              */
876         /*            = 1 : ch2 select              */
877         data = M62332_SUB_ADDR + channel;
878         for (i = 1; i <= 8; i++) {
879                 locomo_m62332_sendbit(mapbase, data >> (8 - i));
880         }
881
882         /* Check A bit */
883         r = locomo_readl(mapbase + LOCOMO_DAC);
884         r &=  ~(LOCOMO_DAC_SCLOEB);
885         locomo_writel(r, mapbase + LOCOMO_DAC);
886         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
887         udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
888         r = locomo_readl(mapbase + LOCOMO_DAC);
889         r &=  ~(LOCOMO_DAC_SDAOEB);
890         locomo_writel(r, mapbase + LOCOMO_DAC);
891         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
892         r = locomo_readl(mapbase + LOCOMO_DAC);
893         r |=  LOCOMO_DAC_SCLOEB;
894         locomo_writel(r, mapbase + LOCOMO_DAC);
895         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
896         udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
897         if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) {   /* High is error */
898                 printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
899                 return;
900         }
901
902         /* Send DAC data */
903         for (i = 1; i <= 8; i++) {
904                 locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
905         }
906
907         /* Check A bit */
908         r = locomo_readl(mapbase + LOCOMO_DAC);
909         r &=  ~(LOCOMO_DAC_SCLOEB);
910         locomo_writel(r, mapbase + LOCOMO_DAC);
911         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
912         udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
913         r = locomo_readl(mapbase + LOCOMO_DAC);
914         r &=  ~(LOCOMO_DAC_SDAOEB);
915         locomo_writel(r, mapbase + LOCOMO_DAC);
916         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
917         r = locomo_readl(mapbase + LOCOMO_DAC);
918         r |=  LOCOMO_DAC_SCLOEB;
919         locomo_writel(r, mapbase + LOCOMO_DAC);
920         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
921         udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
922         if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) {   /* High is error */
923                 printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
924                 return;
925         }
926
927         /* stop */
928         r = locomo_readl(mapbase + LOCOMO_DAC);
929         r &=  ~(LOCOMO_DAC_SCLOEB);
930         locomo_writel(r, mapbase + LOCOMO_DAC);
931         udelay(DAC_LOW_SETUP_TIME);     /* 300 nsec */
932         udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
933         r = locomo_readl(mapbase + LOCOMO_DAC);
934         r |=  LOCOMO_DAC_SCLOEB;
935         locomo_writel(r, mapbase + LOCOMO_DAC);
936         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
937         udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
938         r = locomo_readl(mapbase + LOCOMO_DAC);
939         r |=  LOCOMO_DAC_SDAOEB;
940         locomo_writel(r, mapbase + LOCOMO_DAC);
941         udelay(DAC_HIGH_SETUP_TIME);    /* 1000 nsec */
942         udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
943
944         r = locomo_readl(mapbase + LOCOMO_DAC);
945         r |=  LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
946         locomo_writel(r, mapbase + LOCOMO_DAC);
947         udelay(DAC_LOW_SETUP_TIME);     /* 1000 nsec */
948         udelay(DAC_SCL_LOW_HOLD_TIME);  /* 4.7 usec */
949
950         spin_unlock_irqrestore(&lchip->lock, flags);
951 }
952
953 /*
954  *      LoCoMo "Register Access Bus."
955  *
956  *      We model this as a regular bus type, and hang devices directly
957  *      off this.
958  */
959 static int locomo_match(struct device *_dev, struct device_driver *_drv)
960 {
961         struct locomo_dev *dev = LOCOMO_DEV(_dev);
962         struct locomo_driver *drv = LOCOMO_DRV(_drv);
963
964         return dev->devid == drv->devid;
965 }
966
967 static int locomo_bus_suspend(struct device *dev, pm_message_t state)
968 {
969         struct locomo_dev *ldev = LOCOMO_DEV(dev);
970         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
971         int ret = 0;
972
973         if (drv && drv->suspend)
974                 ret = drv->suspend(ldev, state);
975         return ret;
976 }
977
978 static int locomo_bus_resume(struct device *dev)
979 {
980         struct locomo_dev *ldev = LOCOMO_DEV(dev);
981         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
982         int ret = 0;
983
984         if (drv && drv->resume)
985                 ret = drv->resume(ldev);
986         return ret;
987 }
988
989 static int locomo_bus_probe(struct device *dev)
990 {
991         struct locomo_dev *ldev = LOCOMO_DEV(dev);
992         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
993         int ret = -ENODEV;
994
995         if (drv->probe)
996                 ret = drv->probe(ldev);
997         return ret;
998 }
999
1000 static int locomo_bus_remove(struct device *dev)
1001 {
1002         struct locomo_dev *ldev = LOCOMO_DEV(dev);
1003         struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1004         int ret = 0;
1005
1006         if (drv->remove)
1007                 ret = drv->remove(ldev);
1008         return ret;
1009 }
1010
1011 struct bus_type locomo_bus_type = {
1012         .name           = "locomo-bus",
1013         .match          = locomo_match,
1014         .suspend        = locomo_bus_suspend,
1015         .resume         = locomo_bus_resume,
1016 };
1017
1018 int locomo_driver_register(struct locomo_driver *driver)
1019 {
1020         driver->drv.probe = locomo_bus_probe;
1021         driver->drv.remove = locomo_bus_remove;
1022         driver->drv.bus = &locomo_bus_type;
1023         return driver_register(&driver->drv);
1024 }
1025
1026 void locomo_driver_unregister(struct locomo_driver *driver)
1027 {
1028         driver_unregister(&driver->drv);
1029 }
1030
1031 static int __init locomo_init(void)
1032 {
1033         int ret = bus_register(&locomo_bus_type);
1034         if (ret == 0)
1035                 driver_register(&locomo_device_driver);
1036         return ret;
1037 }
1038
1039 static void __exit locomo_exit(void)
1040 {
1041         driver_unregister(&locomo_device_driver);
1042         bus_unregister(&locomo_bus_type);
1043 }
1044
1045 module_init(locomo_init);
1046 module_exit(locomo_exit);
1047
1048 MODULE_DESCRIPTION("Sharp LoCoMo core driver");
1049 MODULE_LICENSE("GPL");
1050 MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
1051
1052 EXPORT_SYMBOL(locomo_driver_register);
1053 EXPORT_SYMBOL(locomo_driver_unregister);
1054 EXPORT_SYMBOL(locomo_gpio_set_dir);
1055 EXPORT_SYMBOL(locomo_gpio_read_level);
1056 EXPORT_SYMBOL(locomo_gpio_read_output);
1057 EXPORT_SYMBOL(locomo_gpio_write);
1058 EXPORT_SYMBOL(locomo_m62332_senddata);