2 * Support code for the SCOOP interface found on various Sharp PDAs
4 * Copyright (c) 2004 Richard Purdie
6 * Based on code written by Sharp/Lineo for 2.4 kernels
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.
14 #include <linux/device.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
20 #include <asm/hardware/scoop.h>
22 /* PCMCIA to Scoop linkage
24 There is no easy way to link multiple scoop devices into one
25 single entity for the pxa2xx_pcmcia device so this structure
26 is used which is setup by the platform code.
28 This file is never modular so this symbol is always
29 accessile to the board support files.
31 struct scoop_pcmcia_config
*platform_scoop_config
;
32 EXPORT_SYMBOL(platform_scoop_config
);
36 struct gpio_chip gpio
;
37 spinlock_t scoop_lock
;
38 unsigned short suspend_clr
;
39 unsigned short suspend_set
;
43 void reset_scoop(struct device
*dev
)
45 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
47 iowrite16(0x0100, sdev
->base
+ SCOOP_MCR
); // 00
48 iowrite16(0x0000, sdev
->base
+ SCOOP_CDR
); // 04
49 iowrite16(0x0000, sdev
->base
+ SCOOP_CCR
); // 10
50 iowrite16(0x0000, sdev
->base
+ SCOOP_IMR
); // 18
51 iowrite16(0x00FF, sdev
->base
+ SCOOP_IRM
); // 14
52 iowrite16(0x0000, sdev
->base
+ SCOOP_ISR
); // 1C
53 iowrite16(0x0000, sdev
->base
+ SCOOP_IRM
);
56 static void __scoop_gpio_set(struct scoop_dev
*sdev
,
57 unsigned offset
, int value
)
61 gpwr
= ioread16(sdev
->base
+ SCOOP_GPWR
);
63 gpwr
|= 1 << (offset
+ 1);
65 gpwr
&= ~(1 << (offset
+ 1));
66 iowrite16(gpwr
, sdev
->base
+ SCOOP_GPWR
);
69 static void scoop_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
71 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
74 spin_lock_irqsave(&sdev
->scoop_lock
, flags
);
76 __scoop_gpio_set(sdev
, offset
, value
);
78 spin_unlock_irqrestore(&sdev
->scoop_lock
, flags
);
81 static int scoop_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
83 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
85 /* XXX: I'm usure, but it seems so */
86 return ioread16(sdev
->base
+ SCOOP_GPRR
) & (1 << (offset
+ 1));
89 static int scoop_gpio_direction_input(struct gpio_chip
*chip
,
92 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
96 spin_lock_irqsave(&sdev
->scoop_lock
, flags
);
98 gpcr
= ioread16(sdev
->base
+ SCOOP_GPCR
);
99 gpcr
&= ~(1 << (offset
+ 1));
100 iowrite16(gpcr
, sdev
->base
+ SCOOP_GPCR
);
102 spin_unlock_irqrestore(&sdev
->scoop_lock
, flags
);
107 static int scoop_gpio_direction_output(struct gpio_chip
*chip
,
108 unsigned offset
, int value
)
110 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
114 spin_lock_irqsave(&sdev
->scoop_lock
, flags
);
116 __scoop_gpio_set(sdev
, offset
, value
);
118 gpcr
= ioread16(sdev
->base
+ SCOOP_GPCR
);
119 gpcr
|= 1 << (offset
+ 1);
120 iowrite16(gpcr
, sdev
->base
+ SCOOP_GPCR
);
122 spin_unlock_irqrestore(&sdev
->scoop_lock
, flags
);
127 unsigned short set_scoop_gpio(struct device
*dev
, unsigned short bit
)
129 unsigned short gpio_bit
;
131 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
133 spin_lock_irqsave(&sdev
->scoop_lock
, flag
);
134 gpio_bit
= ioread16(sdev
->base
+ SCOOP_GPWR
) | bit
;
135 iowrite16(gpio_bit
, sdev
->base
+ SCOOP_GPWR
);
136 spin_unlock_irqrestore(&sdev
->scoop_lock
, flag
);
141 unsigned short reset_scoop_gpio(struct device
*dev
, unsigned short bit
)
143 unsigned short gpio_bit
;
145 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
147 spin_lock_irqsave(&sdev
->scoop_lock
, flag
);
148 gpio_bit
= ioread16(sdev
->base
+ SCOOP_GPWR
) & ~bit
;
149 iowrite16(gpio_bit
, sdev
->base
+ SCOOP_GPWR
);
150 spin_unlock_irqrestore(&sdev
->scoop_lock
, flag
);
155 EXPORT_SYMBOL(set_scoop_gpio
);
156 EXPORT_SYMBOL(reset_scoop_gpio
);
158 unsigned short read_scoop_reg(struct device
*dev
, unsigned short reg
)
160 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
161 return ioread16(sdev
->base
+ reg
);
164 void write_scoop_reg(struct device
*dev
, unsigned short reg
, unsigned short data
)
166 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
167 iowrite16(data
, sdev
->base
+ reg
);
170 EXPORT_SYMBOL(reset_scoop
);
171 EXPORT_SYMBOL(read_scoop_reg
);
172 EXPORT_SYMBOL(write_scoop_reg
);
174 static void check_scoop_reg(struct scoop_dev
*sdev
)
178 mcr
= ioread16(sdev
->base
+ SCOOP_MCR
);
179 if ((mcr
& 0x100) == 0)
180 iowrite16(0x0101, sdev
->base
+ SCOOP_MCR
);
184 static int scoop_suspend(struct platform_device
*dev
, pm_message_t state
)
186 struct scoop_dev
*sdev
= platform_get_drvdata(dev
);
188 check_scoop_reg(sdev
);
189 sdev
->scoop_gpwr
= ioread16(sdev
->base
+ SCOOP_GPWR
);
190 iowrite16((sdev
->scoop_gpwr
& ~sdev
->suspend_clr
) | sdev
->suspend_set
, sdev
->base
+ SCOOP_GPWR
);
195 static int scoop_resume(struct platform_device
*dev
)
197 struct scoop_dev
*sdev
= platform_get_drvdata(dev
);
199 check_scoop_reg(sdev
);
200 iowrite16(sdev
->scoop_gpwr
, sdev
->base
+ SCOOP_GPWR
);
205 #define scoop_suspend NULL
206 #define scoop_resume NULL
209 static int __devinit
scoop_probe(struct platform_device
*pdev
)
211 struct scoop_dev
*devptr
;
212 struct scoop_config
*inf
;
213 struct resource
*mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
220 devptr
= kzalloc(sizeof(struct scoop_dev
), GFP_KERNEL
);
224 spin_lock_init(&devptr
->scoop_lock
);
226 inf
= pdev
->dev
.platform_data
;
227 devptr
->base
= ioremap(mem
->start
, mem
->end
- mem
->start
+ 1);
234 platform_set_drvdata(pdev
, devptr
);
236 printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem
->start
, devptr
->base
);
238 iowrite16(0x0140, devptr
->base
+ SCOOP_MCR
);
239 reset_scoop(&pdev
->dev
);
240 iowrite16(0x0000, devptr
->base
+ SCOOP_CPR
);
241 iowrite16(inf
->io_dir
& 0xffff, devptr
->base
+ SCOOP_GPCR
);
242 iowrite16(inf
->io_out
& 0xffff, devptr
->base
+ SCOOP_GPWR
);
244 devptr
->suspend_clr
= inf
->suspend_clr
;
245 devptr
->suspend_set
= inf
->suspend_set
;
247 devptr
->gpio
.base
= -1;
249 if (inf
->gpio_base
!= 0) {
250 devptr
->gpio
.label
= dev_name(&pdev
->dev
);
251 devptr
->gpio
.base
= inf
->gpio_base
;
252 devptr
->gpio
.ngpio
= 12; /* PA11 = 0, PA12 = 1, etc. up to PA22 = 11 */
253 devptr
->gpio
.set
= scoop_gpio_set
;
254 devptr
->gpio
.get
= scoop_gpio_get
;
255 devptr
->gpio
.direction_input
= scoop_gpio_direction_input
;
256 devptr
->gpio
.direction_output
= scoop_gpio_direction_output
;
258 ret
= gpiochip_add(&devptr
->gpio
);
265 if (devptr
->gpio
.base
!= -1)
266 temp
= gpiochip_remove(&devptr
->gpio
);
268 platform_set_drvdata(pdev
, NULL
);
270 iounmap(devptr
->base
);
276 static int __devexit
scoop_remove(struct platform_device
*pdev
)
278 struct scoop_dev
*sdev
= platform_get_drvdata(pdev
);
284 if (sdev
->gpio
.base
!= -1) {
285 ret
= gpiochip_remove(&sdev
->gpio
);
287 dev_err(&pdev
->dev
, "Can't remove gpio chip: %d\n", ret
);
292 platform_set_drvdata(pdev
, NULL
);
299 static struct platform_driver scoop_driver
= {
300 .probe
= scoop_probe
,
301 .remove
= __devexit_p(scoop_remove
),
302 .suspend
= scoop_suspend
,
303 .resume
= scoop_resume
,
305 .name
= "sharp-scoop",
309 static int __init
scoop_init(void)
311 return platform_driver_register(&scoop_driver
);
314 subsys_initcall(scoop_init
);