[PATCH] fix missing includes
[linux-2.6.git] / arch / arm / common / scoop.c
1 /*
2  * Support code for the SCOOP interface found on various Sharp PDAs
3  *
4  * Copyright (c) 2004 Richard Purdie
5  *
6  *      Based on code written by Sharp/Lineo for 2.4 kernels
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/device.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
17
18 #include <asm/io.h>
19 #include <asm/hardware/scoop.h>
20
21 #define SCOOP_REG(d,adr) (*(volatile unsigned short*)(d +(adr)))
22
23 /* PCMCIA to Scoop linkage structures for pxa2xx_sharpsl.c
24    There is no easy way to link multiple scoop devices into one
25    single entity for the pxa2xx_pcmcia device */
26 int scoop_num;
27 struct scoop_pcmcia_dev *scoop_devs;
28
29 struct  scoop_dev {
30         void  *base;
31         spinlock_t scoop_lock;
32         unsigned short suspend_clr;
33         unsigned short suspend_set;
34         u32 scoop_gpwr;
35 };
36
37 void reset_scoop(struct device *dev)
38 {
39         struct scoop_dev *sdev = dev_get_drvdata(dev);
40
41         SCOOP_REG(sdev->base,SCOOP_MCR) = 0x0100;  // 00
42         SCOOP_REG(sdev->base,SCOOP_CDR) = 0x0000;  // 04
43         SCOOP_REG(sdev->base,SCOOP_CPR) = 0x0000;  // 0C
44         SCOOP_REG(sdev->base,SCOOP_CCR) = 0x0000;  // 10
45         SCOOP_REG(sdev->base,SCOOP_IMR) = 0x0000;  // 18
46         SCOOP_REG(sdev->base,SCOOP_IRM) = 0x00FF;  // 14
47         SCOOP_REG(sdev->base,SCOOP_ISR) = 0x0000;  // 1C
48         SCOOP_REG(sdev->base,SCOOP_IRM) = 0x0000;
49 }
50
51 unsigned short set_scoop_gpio(struct device *dev, unsigned short bit)
52 {
53         unsigned short gpio_bit;
54         unsigned long flag;
55         struct scoop_dev *sdev = dev_get_drvdata(dev);
56
57         spin_lock_irqsave(&sdev->scoop_lock, flag);
58         gpio_bit = SCOOP_REG(sdev->base, SCOOP_GPWR) | bit;
59         SCOOP_REG(sdev->base, SCOOP_GPWR) = gpio_bit;
60         spin_unlock_irqrestore(&sdev->scoop_lock, flag);
61
62         return gpio_bit;
63 }
64
65 unsigned short reset_scoop_gpio(struct device *dev, unsigned short bit)
66 {
67         unsigned short gpio_bit;
68         unsigned long flag;
69         struct scoop_dev *sdev = dev_get_drvdata(dev);
70
71         spin_lock_irqsave(&sdev->scoop_lock, flag);
72         gpio_bit = SCOOP_REG(sdev->base, SCOOP_GPWR) & ~bit;
73         SCOOP_REG(sdev->base,SCOOP_GPWR) = gpio_bit;
74         spin_unlock_irqrestore(&sdev->scoop_lock, flag);
75
76         return gpio_bit;
77 }
78
79 EXPORT_SYMBOL(set_scoop_gpio);
80 EXPORT_SYMBOL(reset_scoop_gpio);
81
82 unsigned short read_scoop_reg(struct device *dev, unsigned short reg)
83 {
84         struct scoop_dev *sdev = dev_get_drvdata(dev);
85         return SCOOP_REG(sdev->base,reg);
86 }
87
88 void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data)
89 {
90         struct scoop_dev *sdev = dev_get_drvdata(dev);
91         SCOOP_REG(sdev->base,reg)=data;
92 }
93
94 EXPORT_SYMBOL(reset_scoop);
95 EXPORT_SYMBOL(read_scoop_reg);
96 EXPORT_SYMBOL(write_scoop_reg);
97
98 static void check_scoop_reg(struct scoop_dev *sdev)
99 {
100         unsigned short mcr;
101
102         mcr = SCOOP_REG(sdev->base, SCOOP_MCR);
103         if ((mcr & 0x100) == 0)
104                 SCOOP_REG(sdev->base, SCOOP_MCR) = 0x0101;
105 }
106
107 #ifdef CONFIG_PM
108 static int scoop_suspend(struct device *dev, pm_message_t state)
109 {
110         struct scoop_dev *sdev = dev_get_drvdata(dev);
111
112         check_scoop_reg(sdev);
113         sdev->scoop_gpwr = SCOOP_REG(sdev->base, SCOOP_GPWR);
114         SCOOP_REG(sdev->base, SCOOP_GPWR) = (sdev->scoop_gpwr & ~sdev->suspend_clr) | sdev->suspend_set;
115
116         return 0;
117 }
118
119 static int scoop_resume(struct device *dev)
120 {
121         struct scoop_dev *sdev = dev_get_drvdata(dev);
122
123         check_scoop_reg(sdev);
124         SCOOP_REG(sdev->base,SCOOP_GPWR) = sdev->scoop_gpwr;
125
126         return 0;
127 }
128 #else
129 #define scoop_suspend   NULL
130 #define scoop_resume    NULL
131 #endif
132
133 int __init scoop_probe(struct device *dev)
134 {
135         struct scoop_dev *devptr;
136         struct scoop_config *inf;
137         struct platform_device *pdev = to_platform_device(dev);
138         struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139
140         if (!mem)
141                 return -EINVAL;
142
143         devptr = kmalloc(sizeof(struct scoop_dev), GFP_KERNEL);
144
145         if (!devptr)
146                 return  -ENOMEM;
147
148         memset(devptr, 0, sizeof(struct scoop_dev));
149         spin_lock_init(&devptr->scoop_lock);
150
151         inf = dev->platform_data;
152         devptr->base = ioremap(mem->start, mem->end - mem->start + 1);
153
154         if (!devptr->base) {
155                 kfree(devptr);
156                 return -ENOMEM;
157         }
158
159         dev_set_drvdata(dev, devptr);
160
161         printk("Sharp Scoop Device found at 0x%08x -> 0x%08x\n",(unsigned int)mem->start,(unsigned int)devptr->base);
162
163         SCOOP_REG(devptr->base, SCOOP_MCR) = 0x0140;
164         reset_scoop(dev);
165         SCOOP_REG(devptr->base, SCOOP_GPCR) = inf->io_dir & 0xffff;
166         SCOOP_REG(devptr->base, SCOOP_GPWR) = inf->io_out & 0xffff;
167
168         devptr->suspend_clr = inf->suspend_clr;
169         devptr->suspend_set = inf->suspend_set;
170
171         return 0;
172 }
173
174 static int scoop_remove(struct device *dev)
175 {
176         struct scoop_dev *sdev = dev_get_drvdata(dev);
177         if (sdev) {
178                 iounmap(sdev->base);
179                 kfree(sdev);
180                 dev_set_drvdata(dev, NULL);
181         }
182         return 0;
183 }
184
185 static struct device_driver scoop_driver = {
186         .name           = "sharp-scoop",
187         .bus            = &platform_bus_type,
188         .probe          = scoop_probe,
189         .remove         = scoop_remove,
190         .suspend        = scoop_suspend,
191         .resume         = scoop_resume,
192 };
193
194 int __init scoop_init(void)
195 {
196         return driver_register(&scoop_driver);
197 }
198
199 subsys_initcall(scoop_init);