[ARM] 3357/1: enable frontlight on collie
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / common / locomo.c
blobd31b1cb7eea0d83ed2cb632ed07b42d593aad3c7
1 /*
2 * linux/arch/arm/common/locomo.c
4 * Sharp LoCoMo support
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.
10 * This file contains all generic LoCoMo support.
12 * All initialization functions provided here are intended to be called
13 * from machine specific code with proper arguments when required.
15 * Based on sa1111.c
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/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
29 #include <asm/hardware.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/mach/irq.h>
34 #include <asm/hardware/locomo.h>
36 /* M62332 output channel selection */
37 #define M62332_EVR_CH 1 /* M62332 volume channel number */
38 /* 0 : CH.1 , 1 : CH. 2 */
39 /* DAC send data */
40 #define M62332_SLAVE_ADDR 0x4e /* Slave address */
41 #define M62332_W_BIT 0x00 /* W bit (0 only) */
42 #define M62332_SUB_ADDR 0x00 /* Sub address */
43 #define M62332_A_BIT 0x00 /* A bit (0 only) */
45 /* DAC setup and hold times (expressed in us) */
46 #define DAC_BUS_FREE_TIME 5 /* 4.7 us */
47 #define DAC_START_SETUP_TIME 5 /* 4.7 us */
48 #define DAC_STOP_SETUP_TIME 4 /* 4.0 us */
49 #define DAC_START_HOLD_TIME 5 /* 4.7 us */
50 #define DAC_SCL_LOW_HOLD_TIME 5 /* 4.7 us */
51 #define DAC_SCL_HIGH_HOLD_TIME 4 /* 4.0 us */
52 #define DAC_DATA_SETUP_TIME 1 /* 250 ns */
53 #define DAC_DATA_HOLD_TIME 1 /* 300 ns */
54 #define DAC_LOW_SETUP_TIME 1 /* 300 ns */
55 #define DAC_HIGH_SETUP_TIME 1 /* 1000 ns */
57 /* the following is the overall data for the locomo chip */
58 struct locomo {
59 struct device *dev;
60 unsigned long phys;
61 unsigned int irq;
62 spinlock_t lock;
63 void *base;
66 struct locomo_dev_info {
67 unsigned long offset;
68 unsigned long length;
69 unsigned int devid;
70 unsigned int irq[1];
71 const char * name;
74 /* All the locomo devices. If offset is non-zero, the mapbase for the
75 * locomo_dev will be set to the chip base plus offset. If offset is
76 * zero, then the mapbase for the locomo_dev will be set to zero. An
77 * offset of zero means the device only uses GPIOs or other helper
78 * functions inside this file */
79 static struct locomo_dev_info locomo_devices[] = {
81 .devid = LOCOMO_DEVID_KEYBOARD,
82 .irq = {
83 IRQ_LOCOMO_KEY,
85 .name = "locomo-keyboard",
86 .offset = LOCOMO_KEYBOARD,
87 .length = 16,
90 .devid = LOCOMO_DEVID_FRONTLIGHT,
91 .irq = {},
92 .name = "locomo-frontlight",
93 .offset = LOCOMO_FRONTLIGHT,
94 .length = 8,
98 .devid = LOCOMO_DEVID_BACKLIGHT,
99 .irq = {},
100 .name = "locomo-backlight",
101 .offset = LOCOMO_BACKLIGHT,
102 .length = 8,
105 .devid = LOCOMO_DEVID_AUDIO,
106 .irq = {},
107 .name = "locomo-audio",
108 .offset = LOCOMO_AUDIO,
109 .length = 4,
112 .devid = LOCOMO_DEVID_LED,
113 .irq = {},
114 .name = "locomo-led",
115 .offset = LOCOMO_LED,
116 .length = 8,
119 .devid = LOCOMO_DEVID_UART,
120 .irq = {},
121 .name = "locomo-uart",
122 .offset = 0,
123 .length = 0,
128 /** LoCoMo interrupt handling stuff.
129 * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
130 * that is, there is only one real hardware interrupt
131 * we determine which interrupt it is by reading some IO memory.
132 * We have two levels of expansion, first in the handler for the
133 * hardware interrupt we generate an interrupt
134 * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
136 * hardware irq reads LOCOMO_ICR & 0x0f00
137 * IRQ_LOCOMO_KEY_BASE
138 * IRQ_LOCOMO_GPIO_BASE
139 * IRQ_LOCOMO_LT_BASE
140 * IRQ_LOCOMO_SPI_BASE
141 * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
142 * IRQ_LOCOMO_KEY
143 * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
144 * IRQ_LOCOMO_GPIO[0-15]
145 * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
146 * IRQ_LOCOMO_LT
147 * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
148 * IRQ_LOCOMO_SPI_RFR
149 * IRQ_LOCOMO_SPI_RFW
150 * IRQ_LOCOMO_SPI_OVRN
151 * IRQ_LOCOMO_SPI_TEND
154 #define LOCOMO_IRQ_START (IRQ_LOCOMO_KEY_BASE)
155 #define LOCOMO_IRQ_KEY_START (IRQ_LOCOMO_KEY)
156 #define LOCOMO_IRQ_GPIO_START (IRQ_LOCOMO_GPIO0)
157 #define LOCOMO_IRQ_LT_START (IRQ_LOCOMO_LT)
158 #define LOCOMO_IRQ_SPI_START (IRQ_LOCOMO_SPI_RFR)
160 static void locomo_handler(unsigned int irq, struct irqdesc *desc,
161 struct pt_regs *regs)
163 int req, i;
164 struct irqdesc *d;
165 void *mapbase = get_irq_chipdata(irq);
167 /* Acknowledge the parent IRQ */
168 desc->chip->ack(irq);
170 /* check why this interrupt was generated */
171 req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
173 if (req) {
174 /* generate the next interrupt(s) */
175 irq = LOCOMO_IRQ_START;
176 d = irq_desc + irq;
177 for (i = 0; i <= 3; i++, d++, irq++) {
178 if (req & (0x0100 << i)) {
179 desc_handle_irq(irq, d, regs);
186 static void locomo_ack_irq(unsigned int irq)
190 static void locomo_mask_irq(unsigned int irq)
192 void *mapbase = get_irq_chipdata(irq);
193 unsigned int r;
194 r = locomo_readl(mapbase + LOCOMO_ICR);
195 r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
196 locomo_writel(r, mapbase + LOCOMO_ICR);
199 static void locomo_unmask_irq(unsigned int irq)
201 void *mapbase = get_irq_chipdata(irq);
202 unsigned int r;
203 r = locomo_readl(mapbase + LOCOMO_ICR);
204 r |= (0x0010 << (irq - LOCOMO_IRQ_START));
205 locomo_writel(r, mapbase + LOCOMO_ICR);
208 static struct irqchip locomo_chip = {
209 .ack = locomo_ack_irq,
210 .mask = locomo_mask_irq,
211 .unmask = locomo_unmask_irq,
214 static void locomo_key_handler(unsigned int irq, struct irqdesc *desc,
215 struct pt_regs *regs)
217 struct irqdesc *d;
218 void *mapbase = get_irq_chipdata(irq);
220 if (locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC) & 0x0001) {
221 d = irq_desc + LOCOMO_IRQ_KEY_START;
222 desc_handle_irq(LOCOMO_IRQ_KEY_START, d, regs);
226 static void locomo_key_ack_irq(unsigned int irq)
228 void *mapbase = get_irq_chipdata(irq);
229 unsigned int r;
230 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
231 r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
232 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
235 static void locomo_key_mask_irq(unsigned int irq)
237 void *mapbase = get_irq_chipdata(irq);
238 unsigned int r;
239 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
240 r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
241 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
244 static void locomo_key_unmask_irq(unsigned int irq)
246 void *mapbase = get_irq_chipdata(irq);
247 unsigned int r;
248 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
249 r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
250 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
253 static struct irqchip locomo_key_chip = {
254 .ack = locomo_key_ack_irq,
255 .mask = locomo_key_mask_irq,
256 .unmask = locomo_key_unmask_irq,
259 static void locomo_gpio_handler(unsigned int irq, struct irqdesc *desc,
260 struct pt_regs *regs)
262 int req, i;
263 struct irqdesc *d;
264 void *mapbase = get_irq_chipdata(irq);
266 req = locomo_readl(mapbase + LOCOMO_GIR) &
267 locomo_readl(mapbase + LOCOMO_GPD) &
268 0xffff;
270 if (req) {
271 irq = LOCOMO_IRQ_GPIO_START;
272 d = irq_desc + LOCOMO_IRQ_GPIO_START;
273 for (i = 0; i <= 15; i++, irq++, d++) {
274 if (req & (0x0001 << i)) {
275 desc_handle_irq(irq, d, regs);
281 static void locomo_gpio_ack_irq(unsigned int irq)
283 void *mapbase = get_irq_chipdata(irq);
284 unsigned int r;
285 r = locomo_readl(mapbase + LOCOMO_GWE);
286 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
287 locomo_writel(r, mapbase + LOCOMO_GWE);
289 r = locomo_readl(mapbase + LOCOMO_GIS);
290 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
291 locomo_writel(r, mapbase + LOCOMO_GIS);
293 r = locomo_readl(mapbase + LOCOMO_GWE);
294 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
295 locomo_writel(r, mapbase + LOCOMO_GWE);
298 static void locomo_gpio_mask_irq(unsigned int irq)
300 void *mapbase = get_irq_chipdata(irq);
301 unsigned int r;
302 r = locomo_readl(mapbase + LOCOMO_GIE);
303 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
304 locomo_writel(r, mapbase + LOCOMO_GIE);
307 static void locomo_gpio_unmask_irq(unsigned int irq)
309 void *mapbase = get_irq_chipdata(irq);
310 unsigned int r;
311 r = locomo_readl(mapbase + LOCOMO_GIE);
312 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
313 locomo_writel(r, mapbase + LOCOMO_GIE);
316 static struct irqchip locomo_gpio_chip = {
317 .ack = locomo_gpio_ack_irq,
318 .mask = locomo_gpio_mask_irq,
319 .unmask = locomo_gpio_unmask_irq,
322 static void locomo_lt_handler(unsigned int irq, struct irqdesc *desc,
323 struct pt_regs *regs)
325 struct irqdesc *d;
326 void *mapbase = get_irq_chipdata(irq);
328 if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
329 d = irq_desc + LOCOMO_IRQ_LT_START;
330 desc_handle_irq(LOCOMO_IRQ_LT_START, d, regs);
334 static void locomo_lt_ack_irq(unsigned int irq)
336 void *mapbase = get_irq_chipdata(irq);
337 unsigned int r;
338 r = locomo_readl(mapbase + LOCOMO_LTINT);
339 r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
340 locomo_writel(r, mapbase + LOCOMO_LTINT);
343 static void locomo_lt_mask_irq(unsigned int irq)
345 void *mapbase = get_irq_chipdata(irq);
346 unsigned int r;
347 r = locomo_readl(mapbase + LOCOMO_LTINT);
348 r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
349 locomo_writel(r, mapbase + LOCOMO_LTINT);
352 static void locomo_lt_unmask_irq(unsigned int irq)
354 void *mapbase = get_irq_chipdata(irq);
355 unsigned int r;
356 r = locomo_readl(mapbase + LOCOMO_LTINT);
357 r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
358 locomo_writel(r, mapbase + LOCOMO_LTINT);
361 static struct irqchip locomo_lt_chip = {
362 .ack = locomo_lt_ack_irq,
363 .mask = locomo_lt_mask_irq,
364 .unmask = locomo_lt_unmask_irq,
367 static void locomo_spi_handler(unsigned int irq, struct irqdesc *desc,
368 struct pt_regs *regs)
370 int req, i;
371 struct irqdesc *d;
372 void *mapbase = get_irq_chipdata(irq);
374 req = locomo_readl(mapbase + LOCOMO_SPIIR) & 0x000F;
375 if (req) {
376 irq = LOCOMO_IRQ_SPI_START;
377 d = irq_desc + irq;
379 for (i = 0; i <= 3; i++, irq++, d++) {
380 if (req & (0x0001 << i)) {
381 desc_handle_irq(irq, d, regs);
387 static void locomo_spi_ack_irq(unsigned int irq)
389 void *mapbase = get_irq_chipdata(irq);
390 unsigned int r;
391 r = locomo_readl(mapbase + LOCOMO_SPIWE);
392 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
393 locomo_writel(r, mapbase + LOCOMO_SPIWE);
395 r = locomo_readl(mapbase + LOCOMO_SPIIS);
396 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
397 locomo_writel(r, mapbase + LOCOMO_SPIIS);
399 r = locomo_readl(mapbase + LOCOMO_SPIWE);
400 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
401 locomo_writel(r, mapbase + LOCOMO_SPIWE);
404 static void locomo_spi_mask_irq(unsigned int irq)
406 void *mapbase = get_irq_chipdata(irq);
407 unsigned int r;
408 r = locomo_readl(mapbase + LOCOMO_SPIIE);
409 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
410 locomo_writel(r, mapbase + LOCOMO_SPIIE);
413 static void locomo_spi_unmask_irq(unsigned int irq)
415 void *mapbase = get_irq_chipdata(irq);
416 unsigned int r;
417 r = locomo_readl(mapbase + LOCOMO_SPIIE);
418 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
419 locomo_writel(r, mapbase + LOCOMO_SPIIE);
422 static struct irqchip locomo_spi_chip = {
423 .ack = locomo_spi_ack_irq,
424 .mask = locomo_spi_mask_irq,
425 .unmask = locomo_spi_unmask_irq,
428 static void locomo_setup_irq(struct locomo *lchip)
430 int irq;
431 void *irqbase = lchip->base;
434 * Install handler for IRQ_LOCOMO_HW.
436 set_irq_type(lchip->irq, IRQT_FALLING);
437 set_irq_chipdata(lchip->irq, irqbase);
438 set_irq_chained_handler(lchip->irq, locomo_handler);
440 /* Install handlers for IRQ_LOCOMO_*_BASE */
441 set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
442 set_irq_chipdata(IRQ_LOCOMO_KEY_BASE, irqbase);
443 set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
444 set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
446 set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
447 set_irq_chipdata(IRQ_LOCOMO_GPIO_BASE, irqbase);
448 set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
449 set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
451 set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
452 set_irq_chipdata(IRQ_LOCOMO_LT_BASE, irqbase);
453 set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
454 set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
456 set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
457 set_irq_chipdata(IRQ_LOCOMO_SPI_BASE, irqbase);
458 set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
459 set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
461 /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
462 set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
463 set_irq_chipdata(LOCOMO_IRQ_KEY_START, irqbase);
464 set_irq_handler(LOCOMO_IRQ_KEY_START, do_edge_IRQ);
465 set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
467 /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
468 for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
469 set_irq_chip(irq, &locomo_gpio_chip);
470 set_irq_chipdata(irq, irqbase);
471 set_irq_handler(irq, do_edge_IRQ);
472 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
475 /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
476 set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
477 set_irq_chipdata(LOCOMO_IRQ_LT_START, irqbase);
478 set_irq_handler(LOCOMO_IRQ_LT_START, do_edge_IRQ);
479 set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
481 /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
482 for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
483 set_irq_chip(irq, &locomo_spi_chip);
484 set_irq_chipdata(irq, irqbase);
485 set_irq_handler(irq, do_edge_IRQ);
486 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
491 static void locomo_dev_release(struct device *_dev)
493 struct locomo_dev *dev = LOCOMO_DEV(_dev);
495 kfree(dev);
498 static int
499 locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
501 struct locomo_dev *dev;
502 int ret;
504 dev = kmalloc(sizeof(struct locomo_dev), GFP_KERNEL);
505 if (!dev) {
506 ret = -ENOMEM;
507 goto out;
509 memset(dev, 0, sizeof(struct locomo_dev));
511 strncpy(dev->dev.bus_id,info->name,sizeof(dev->dev.bus_id));
513 * If the parent device has a DMA mask associated with it,
514 * propagate it down to the children.
516 if (lchip->dev->dma_mask) {
517 dev->dma_mask = *lchip->dev->dma_mask;
518 dev->dev.dma_mask = &dev->dma_mask;
521 dev->devid = info->devid;
522 dev->dev.parent = lchip->dev;
523 dev->dev.bus = &locomo_bus_type;
524 dev->dev.release = locomo_dev_release;
525 dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
527 if (info->offset)
528 dev->mapbase = lchip->base + info->offset;
529 else
530 dev->mapbase = 0;
531 dev->length = info->length;
533 memmove(dev->irq, info->irq, sizeof(dev->irq));
535 ret = device_register(&dev->dev);
536 if (ret) {
537 out:
538 kfree(dev);
540 return ret;
543 #ifdef CONFIG_PM
545 struct locomo_save_data {
546 u16 LCM_GPO;
547 u16 LCM_SPICT;
548 u16 LCM_GPE;
549 u16 LCM_ASD;
550 u16 LCM_SPIMD;
553 static int locomo_suspend(struct platform_device *dev, pm_message_t state)
555 struct locomo *lchip = platform_get_drvdata(dev);
556 struct locomo_save_data *save;
557 unsigned long flags;
559 save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
560 if (!save)
561 return -ENOMEM;
563 dev->dev.power.saved_state = (void *) save;
565 spin_lock_irqsave(&lchip->lock, flags);
567 save->LCM_GPO = locomo_readl(lchip->base + LOCOMO_GPO); /* GPIO */
568 locomo_writel(0x00, lchip->base + LOCOMO_GPO);
569 save->LCM_SPICT = locomo_readl(lchip->base + LOCOMO_SPICT); /* SPI */
570 locomo_writel(0x40, lchip->base + LOCOMO_SPICT);
571 save->LCM_GPE = locomo_readl(lchip->base + LOCOMO_GPE); /* GPIO */
572 locomo_writel(0x00, lchip->base + LOCOMO_GPE);
573 save->LCM_ASD = locomo_readl(lchip->base + LOCOMO_ASD); /* ADSTART */
574 locomo_writel(0x00, lchip->base + LOCOMO_ASD);
575 save->LCM_SPIMD = locomo_readl(lchip->base + LOCOMO_SPIMD); /* SPI */
576 locomo_writel(0x3C14, lchip->base + LOCOMO_SPIMD);
578 locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
579 locomo_writel(0x00, lchip->base + LOCOMO_DAC);
580 locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
582 if ( (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88) )
583 locomo_writel(0x00, lchip->base + LOCOMO_C32K); /* CLK32 off */
584 else
585 /* 18MHz already enabled, so no wait */
586 locomo_writel(0xc1, lchip->base + LOCOMO_C32K); /* CLK32 on */
588 locomo_writel(0x00, lchip->base + LOCOMO_TADC); /* 18MHz clock off*/
589 locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC); /* 22MHz/24MHz clock off */
590 locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS); /* FL */
592 spin_unlock_irqrestore(&lchip->lock, flags);
594 return 0;
597 static int locomo_resume(struct platform_device *dev)
599 struct locomo *lchip = platform_get_drvdata(dev);
600 struct locomo_save_data *save;
601 unsigned long r;
602 unsigned long flags;
604 save = (struct locomo_save_data *) dev->dev.power.saved_state;
605 if (!save)
606 return 0;
608 spin_lock_irqsave(&lchip->lock, flags);
610 locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
611 locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPICT);
612 locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
613 locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
614 locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPIMD);
616 locomo_writel(0x00, lchip->base + LOCOMO_C32K);
617 locomo_writel(0x90, lchip->base + LOCOMO_TADC);
619 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
620 r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
621 r &= 0xFEFF;
622 locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
623 locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
625 spin_unlock_irqrestore(&lchip->lock, flags);
626 kfree(save);
628 return 0;
630 #endif
633 #define LCM_ALC_EN 0x8000
635 void frontlight_set(struct locomo *lchip, int duty, int vr, int bpwf)
637 unsigned long flags;
639 spin_lock_irqsave(&lchip->lock, flags);
640 locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
641 udelay(100);
642 locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
643 locomo_writel(bpwf | LCM_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
644 spin_unlock_irqrestore(&lchip->lock, flags);
649 * locomo_probe - probe for a single LoCoMo chip.
650 * @phys_addr: physical address of device.
652 * Probe for a LoCoMo chip. This must be called
653 * before any other locomo-specific code.
655 * Returns:
656 * %-ENODEV device not found.
657 * %-EBUSY physical address already marked in-use.
658 * %0 successful.
660 static int
661 __locomo_probe(struct device *me, struct resource *mem, int irq)
663 struct locomo *lchip;
664 unsigned long r;
665 int i, ret = -ENODEV;
667 lchip = kmalloc(sizeof(struct locomo), GFP_KERNEL);
668 if (!lchip)
669 return -ENOMEM;
671 memset(lchip, 0, sizeof(struct locomo));
673 spin_lock_init(&lchip->lock);
675 lchip->dev = me;
676 dev_set_drvdata(lchip->dev, lchip);
678 lchip->phys = mem->start;
679 lchip->irq = irq;
682 * Map the whole region. This also maps the
683 * registers for our children.
685 lchip->base = ioremap(mem->start, PAGE_SIZE);
686 if (!lchip->base) {
687 ret = -ENOMEM;
688 goto out;
691 /* locomo initialize */
692 locomo_writel(0, lchip->base + LOCOMO_ICR);
693 /* KEYBOARD */
694 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
696 /* GPIO */
697 locomo_writel(0, lchip->base + LOCOMO_GPO);
698 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
699 , lchip->base + LOCOMO_GPE);
700 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
701 , lchip->base + LOCOMO_GPD);
702 locomo_writel(0, lchip->base + LOCOMO_GIE);
704 /* FrontLight */
705 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
706 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
708 /* Same constants can be used for collie and poodle
709 (depending on CONFIG options in original sharp code)? */
710 frontlight_set(lchip, 163, 0, 148);
712 /* Longtime timer */
713 locomo_writel(0, lchip->base + LOCOMO_LTINT);
714 /* SPI */
715 locomo_writel(0, lchip->base + LOCOMO_SPIIE);
717 locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
718 r = locomo_readl(lchip->base + LOCOMO_ASD);
719 r |= 0x8000;
720 locomo_writel(r, lchip->base + LOCOMO_ASD);
722 locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
723 r = locomo_readl(lchip->base + LOCOMO_HSD);
724 r |= 0x8000;
725 locomo_writel(r, lchip->base + LOCOMO_HSD);
727 locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
729 /* XON */
730 locomo_writel(0x80, lchip->base + LOCOMO_TADC);
731 udelay(1000);
732 /* CLK9MEN */
733 r = locomo_readl(lchip->base + LOCOMO_TADC);
734 r |= 0x10;
735 locomo_writel(r, lchip->base + LOCOMO_TADC);
736 udelay(100);
738 /* init DAC */
739 r = locomo_readl(lchip->base + LOCOMO_DAC);
740 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
741 locomo_writel(r, lchip->base + LOCOMO_DAC);
743 r = locomo_readl(lchip->base + LOCOMO_VER);
744 printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
747 * The interrupt controller must be initialised before any
748 * other device to ensure that the interrupts are available.
750 if (lchip->irq != NO_IRQ)
751 locomo_setup_irq(lchip);
753 for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
754 locomo_init_one_child(lchip, &locomo_devices[i]);
756 return 0;
758 out:
759 kfree(lchip);
760 return ret;
763 static int locomo_remove_child(struct device *dev, void *data)
765 device_unregister(dev);
766 return 0;
769 static void __locomo_remove(struct locomo *lchip)
771 device_for_each_child(lchip->dev, NULL, locomo_remove_child);
773 if (lchip->irq != NO_IRQ) {
774 set_irq_chained_handler(lchip->irq, NULL);
775 set_irq_data(lchip->irq, NULL);
778 iounmap(lchip->base);
779 kfree(lchip);
782 static int locomo_probe(struct platform_device *dev)
784 struct resource *mem;
785 int irq;
787 mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
788 if (!mem)
789 return -EINVAL;
790 irq = platform_get_irq(dev, 0);
792 return __locomo_probe(&dev->dev, mem, irq);
795 static int locomo_remove(struct platform_device *dev)
797 struct locomo *lchip = platform_get_drvdata(dev);
799 if (lchip) {
800 __locomo_remove(lchip);
801 platform_set_drvdata(dev, NULL);
804 return 0;
808 * Not sure if this should be on the system bus or not yet.
809 * We really want some way to register a system device at
810 * the per-machine level, and then have this driver pick
811 * up the registered devices.
813 static struct platform_driver locomo_device_driver = {
814 .probe = locomo_probe,
815 .remove = locomo_remove,
816 #ifdef CONFIG_PM
817 .suspend = locomo_suspend,
818 .resume = locomo_resume,
819 #endif
820 .driver = {
821 .name = "locomo",
826 * Get the parent device driver (us) structure
827 * from a child function device
829 static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
831 return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
834 void locomo_gpio_set_dir(struct locomo_dev *ldev, unsigned int bits, unsigned int dir)
836 struct locomo *lchip = locomo_chip_driver(ldev);
837 unsigned long flags;
838 unsigned int r;
840 spin_lock_irqsave(&lchip->lock, flags);
842 r = locomo_readl(lchip->base + LOCOMO_GPD);
843 r &= ~bits;
844 locomo_writel(r, lchip->base + LOCOMO_GPD);
846 r = locomo_readl(lchip->base + LOCOMO_GPE);
847 if (dir)
848 r |= bits;
849 else
850 r &= ~bits;
851 locomo_writel(r, lchip->base + LOCOMO_GPE);
853 spin_unlock_irqrestore(&lchip->lock, flags);
856 unsigned int locomo_gpio_read_level(struct locomo_dev *ldev, unsigned int bits)
858 struct locomo *lchip = locomo_chip_driver(ldev);
859 unsigned long flags;
860 unsigned int ret;
862 spin_lock_irqsave(&lchip->lock, flags);
863 ret = locomo_readl(lchip->base + LOCOMO_GPL);
864 spin_unlock_irqrestore(&lchip->lock, flags);
866 ret &= bits;
867 return ret;
870 unsigned int locomo_gpio_read_output(struct locomo_dev *ldev, unsigned int bits)
872 struct locomo *lchip = locomo_chip_driver(ldev);
873 unsigned long flags;
874 unsigned int ret;
876 spin_lock_irqsave(&lchip->lock, flags);
877 ret = locomo_readl(lchip->base + LOCOMO_GPO);
878 spin_unlock_irqrestore(&lchip->lock, flags);
880 ret &= bits;
881 return ret;
884 void locomo_gpio_write(struct locomo_dev *ldev, unsigned int bits, unsigned int set)
886 struct locomo *lchip = locomo_chip_driver(ldev);
887 unsigned long flags;
888 unsigned int r;
890 spin_lock_irqsave(&lchip->lock, flags);
892 r = locomo_readl(lchip->base + LOCOMO_GPO);
893 if (set)
894 r |= bits;
895 else
896 r &= ~bits;
897 locomo_writel(r, lchip->base + LOCOMO_GPO);
899 spin_unlock_irqrestore(&lchip->lock, flags);
902 static void locomo_m62332_sendbit(void *mapbase, int bit)
904 unsigned int r;
906 r = locomo_readl(mapbase + LOCOMO_DAC);
907 r &= ~(LOCOMO_DAC_SCLOEB);
908 locomo_writel(r, mapbase + LOCOMO_DAC);
909 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
910 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
911 r = locomo_readl(mapbase + LOCOMO_DAC);
912 r &= ~(LOCOMO_DAC_SCLOEB);
913 locomo_writel(r, mapbase + LOCOMO_DAC);
914 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
915 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
917 if (bit & 1) {
918 r = locomo_readl(mapbase + LOCOMO_DAC);
919 r |= LOCOMO_DAC_SDAOEB;
920 locomo_writel(r, mapbase + LOCOMO_DAC);
921 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
922 } else {
923 r = locomo_readl(mapbase + LOCOMO_DAC);
924 r &= ~(LOCOMO_DAC_SDAOEB);
925 locomo_writel(r, mapbase + LOCOMO_DAC);
926 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
929 udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
930 r = locomo_readl(mapbase + LOCOMO_DAC);
931 r |= LOCOMO_DAC_SCLOEB;
932 locomo_writel(r, mapbase + LOCOMO_DAC);
933 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
934 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
937 void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
939 struct locomo *lchip = locomo_chip_driver(ldev);
940 int i;
941 unsigned char data;
942 unsigned int r;
943 void *mapbase = lchip->base;
944 unsigned long flags;
946 spin_lock_irqsave(&lchip->lock, flags);
948 /* Start */
949 udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
950 r = locomo_readl(mapbase + LOCOMO_DAC);
951 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
952 locomo_writel(r, mapbase + LOCOMO_DAC);
953 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
954 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
955 r = locomo_readl(mapbase + LOCOMO_DAC);
956 r &= ~(LOCOMO_DAC_SDAOEB);
957 locomo_writel(r, mapbase + LOCOMO_DAC);
958 udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
959 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
961 /* Send slave address and W bit (LSB is W bit) */
962 data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
963 for (i = 1; i <= 8; i++) {
964 locomo_m62332_sendbit(mapbase, data >> (8 - i));
967 /* Check A bit */
968 r = locomo_readl(mapbase + LOCOMO_DAC);
969 r &= ~(LOCOMO_DAC_SCLOEB);
970 locomo_writel(r, mapbase + LOCOMO_DAC);
971 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
972 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
973 r = locomo_readl(mapbase + LOCOMO_DAC);
974 r &= ~(LOCOMO_DAC_SDAOEB);
975 locomo_writel(r, mapbase + LOCOMO_DAC);
976 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
977 r = locomo_readl(mapbase + LOCOMO_DAC);
978 r |= LOCOMO_DAC_SCLOEB;
979 locomo_writel(r, mapbase + LOCOMO_DAC);
980 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
981 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
982 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
983 printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
984 return;
987 /* Send Sub address (LSB is channel select) */
988 /* channel = 0 : ch1 select */
989 /* = 1 : ch2 select */
990 data = M62332_SUB_ADDR + channel;
991 for (i = 1; i <= 8; i++) {
992 locomo_m62332_sendbit(mapbase, data >> (8 - i));
995 /* Check A bit */
996 r = locomo_readl(mapbase + LOCOMO_DAC);
997 r &= ~(LOCOMO_DAC_SCLOEB);
998 locomo_writel(r, mapbase + LOCOMO_DAC);
999 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1000 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1001 r = locomo_readl(mapbase + LOCOMO_DAC);
1002 r &= ~(LOCOMO_DAC_SDAOEB);
1003 locomo_writel(r, mapbase + LOCOMO_DAC);
1004 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1005 r = locomo_readl(mapbase + LOCOMO_DAC);
1006 r |= LOCOMO_DAC_SCLOEB;
1007 locomo_writel(r, mapbase + LOCOMO_DAC);
1008 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1009 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1010 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1011 printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
1012 return;
1015 /* Send DAC data */
1016 for (i = 1; i <= 8; i++) {
1017 locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
1020 /* Check A bit */
1021 r = locomo_readl(mapbase + LOCOMO_DAC);
1022 r &= ~(LOCOMO_DAC_SCLOEB);
1023 locomo_writel(r, mapbase + LOCOMO_DAC);
1024 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1025 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1026 r = locomo_readl(mapbase + LOCOMO_DAC);
1027 r &= ~(LOCOMO_DAC_SDAOEB);
1028 locomo_writel(r, mapbase + LOCOMO_DAC);
1029 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1030 r = locomo_readl(mapbase + LOCOMO_DAC);
1031 r |= LOCOMO_DAC_SCLOEB;
1032 locomo_writel(r, mapbase + LOCOMO_DAC);
1033 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1034 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1035 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1036 printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
1037 return;
1040 /* stop */
1041 r = locomo_readl(mapbase + LOCOMO_DAC);
1042 r &= ~(LOCOMO_DAC_SCLOEB);
1043 locomo_writel(r, mapbase + LOCOMO_DAC);
1044 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1045 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1046 r = locomo_readl(mapbase + LOCOMO_DAC);
1047 r |= LOCOMO_DAC_SCLOEB;
1048 locomo_writel(r, mapbase + LOCOMO_DAC);
1049 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1050 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1051 r = locomo_readl(mapbase + LOCOMO_DAC);
1052 r |= LOCOMO_DAC_SDAOEB;
1053 locomo_writel(r, mapbase + LOCOMO_DAC);
1054 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1055 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1057 r = locomo_readl(mapbase + LOCOMO_DAC);
1058 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
1059 locomo_writel(r, mapbase + LOCOMO_DAC);
1060 udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
1061 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1063 spin_unlock_irqrestore(&lchip->lock, flags);
1067 * LoCoMo "Register Access Bus."
1069 * We model this as a regular bus type, and hang devices directly
1070 * off this.
1072 static int locomo_match(struct device *_dev, struct device_driver *_drv)
1074 struct locomo_dev *dev = LOCOMO_DEV(_dev);
1075 struct locomo_driver *drv = LOCOMO_DRV(_drv);
1077 return dev->devid == drv->devid;
1080 static int locomo_bus_suspend(struct device *dev, pm_message_t state)
1082 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1083 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1084 int ret = 0;
1086 if (drv && drv->suspend)
1087 ret = drv->suspend(ldev, state);
1088 return ret;
1091 static int locomo_bus_resume(struct device *dev)
1093 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1094 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1095 int ret = 0;
1097 if (drv && drv->resume)
1098 ret = drv->resume(ldev);
1099 return ret;
1102 static int locomo_bus_probe(struct device *dev)
1104 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1105 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1106 int ret = -ENODEV;
1108 if (drv->probe)
1109 ret = drv->probe(ldev);
1110 return ret;
1113 static int locomo_bus_remove(struct device *dev)
1115 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1116 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1117 int ret = 0;
1119 if (drv->remove)
1120 ret = drv->remove(ldev);
1121 return ret;
1124 struct bus_type locomo_bus_type = {
1125 .name = "locomo-bus",
1126 .match = locomo_match,
1127 .probe = locomo_bus_probe,
1128 .remove = locomo_bus_remove,
1129 .suspend = locomo_bus_suspend,
1130 .resume = locomo_bus_resume,
1133 int locomo_driver_register(struct locomo_driver *driver)
1135 driver->drv.bus = &locomo_bus_type;
1136 return driver_register(&driver->drv);
1139 void locomo_driver_unregister(struct locomo_driver *driver)
1141 driver_unregister(&driver->drv);
1144 static int __init locomo_init(void)
1146 int ret = bus_register(&locomo_bus_type);
1147 if (ret == 0)
1148 platform_driver_register(&locomo_device_driver);
1149 return ret;
1152 static void __exit locomo_exit(void)
1154 platform_driver_unregister(&locomo_device_driver);
1155 bus_unregister(&locomo_bus_type);
1158 module_init(locomo_init);
1159 module_exit(locomo_exit);
1161 MODULE_DESCRIPTION("Sharp LoCoMo core driver");
1162 MODULE_LICENSE("GPL");
1163 MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
1165 EXPORT_SYMBOL(locomo_driver_register);
1166 EXPORT_SYMBOL(locomo_driver_unregister);
1167 EXPORT_SYMBOL(locomo_gpio_set_dir);
1168 EXPORT_SYMBOL(locomo_gpio_read_level);
1169 EXPORT_SYMBOL(locomo_gpio_read_output);
1170 EXPORT_SYMBOL(locomo_gpio_write);
1171 EXPORT_SYMBOL(locomo_m62332_senddata);