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/gpio.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/platform_device.h>
19 #include <linux/export.h>
21 #include <asm/hardware/scoop.h>
23 /* PCMCIA to Scoop linkage
25 There is no easy way to link multiple scoop devices into one
26 single entity for the pxa2xx_pcmcia device so this structure
27 is used which is setup by the platform code.
29 This file is never modular so this symbol is always
30 accessile to the board support files.
32 struct scoop_pcmcia_config
*platform_scoop_config
;
33 EXPORT_SYMBOL(platform_scoop_config
);
37 struct gpio_chip gpio
;
38 spinlock_t scoop_lock
;
39 unsigned short suspend_clr
;
40 unsigned short suspend_set
;
44 void reset_scoop(struct device
*dev
)
46 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
48 iowrite16(0x0100, sdev
->base
+ SCOOP_MCR
); /* 00 */
49 iowrite16(0x0000, sdev
->base
+ SCOOP_CDR
); /* 04 */
50 iowrite16(0x0000, sdev
->base
+ SCOOP_CCR
); /* 10 */
51 iowrite16(0x0000, sdev
->base
+ SCOOP_IMR
); /* 18 */
52 iowrite16(0x00FF, sdev
->base
+ SCOOP_IRM
); /* 14 */
53 iowrite16(0x0000, sdev
->base
+ SCOOP_ISR
); /* 1C */
54 iowrite16(0x0000, sdev
->base
+ SCOOP_IRM
);
57 static void __scoop_gpio_set(struct scoop_dev
*sdev
,
58 unsigned offset
, int value
)
62 gpwr
= ioread16(sdev
->base
+ SCOOP_GPWR
);
64 gpwr
|= 1 << (offset
+ 1);
66 gpwr
&= ~(1 << (offset
+ 1));
67 iowrite16(gpwr
, sdev
->base
+ SCOOP_GPWR
);
70 static void scoop_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
72 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
75 spin_lock_irqsave(&sdev
->scoop_lock
, flags
);
77 __scoop_gpio_set(sdev
, offset
, value
);
79 spin_unlock_irqrestore(&sdev
->scoop_lock
, flags
);
82 static int scoop_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
84 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
86 /* XXX: I'm unsure, but it seems so */
87 return !!(ioread16(sdev
->base
+ SCOOP_GPRR
) & (1 << (offset
+ 1)));
90 static int scoop_gpio_direction_input(struct gpio_chip
*chip
,
93 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
97 spin_lock_irqsave(&sdev
->scoop_lock
, flags
);
99 gpcr
= ioread16(sdev
->base
+ SCOOP_GPCR
);
100 gpcr
&= ~(1 << (offset
+ 1));
101 iowrite16(gpcr
, sdev
->base
+ SCOOP_GPCR
);
103 spin_unlock_irqrestore(&sdev
->scoop_lock
, flags
);
108 static int scoop_gpio_direction_output(struct gpio_chip
*chip
,
109 unsigned offset
, int value
)
111 struct scoop_dev
*sdev
= container_of(chip
, struct scoop_dev
, gpio
);
115 spin_lock_irqsave(&sdev
->scoop_lock
, flags
);
117 __scoop_gpio_set(sdev
, offset
, value
);
119 gpcr
= ioread16(sdev
->base
+ SCOOP_GPCR
);
120 gpcr
|= 1 << (offset
+ 1);
121 iowrite16(gpcr
, sdev
->base
+ SCOOP_GPCR
);
123 spin_unlock_irqrestore(&sdev
->scoop_lock
, flags
);
128 unsigned short read_scoop_reg(struct device
*dev
, unsigned short reg
)
130 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
131 return ioread16(sdev
->base
+ reg
);
134 void write_scoop_reg(struct device
*dev
, unsigned short reg
, unsigned short data
)
136 struct scoop_dev
*sdev
= dev_get_drvdata(dev
);
137 iowrite16(data
, sdev
->base
+ reg
);
140 EXPORT_SYMBOL(reset_scoop
);
141 EXPORT_SYMBOL(read_scoop_reg
);
142 EXPORT_SYMBOL(write_scoop_reg
);
145 static void check_scoop_reg(struct scoop_dev
*sdev
)
149 mcr
= ioread16(sdev
->base
+ SCOOP_MCR
);
150 if ((mcr
& 0x100) == 0)
151 iowrite16(0x0101, sdev
->base
+ SCOOP_MCR
);
154 static int scoop_suspend(struct platform_device
*dev
, pm_message_t state
)
156 struct scoop_dev
*sdev
= platform_get_drvdata(dev
);
158 check_scoop_reg(sdev
);
159 sdev
->scoop_gpwr
= ioread16(sdev
->base
+ SCOOP_GPWR
);
160 iowrite16((sdev
->scoop_gpwr
& ~sdev
->suspend_clr
) | sdev
->suspend_set
, sdev
->base
+ SCOOP_GPWR
);
165 static int scoop_resume(struct platform_device
*dev
)
167 struct scoop_dev
*sdev
= platform_get_drvdata(dev
);
169 check_scoop_reg(sdev
);
170 iowrite16(sdev
->scoop_gpwr
, sdev
->base
+ SCOOP_GPWR
);
175 #define scoop_suspend NULL
176 #define scoop_resume NULL
179 static int scoop_probe(struct platform_device
*pdev
)
181 struct scoop_dev
*devptr
;
182 struct scoop_config
*inf
;
183 struct resource
*mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
189 devptr
= kzalloc(sizeof(struct scoop_dev
), GFP_KERNEL
);
193 spin_lock_init(&devptr
->scoop_lock
);
195 inf
= pdev
->dev
.platform_data
;
196 devptr
->base
= ioremap(mem
->start
, resource_size(mem
));
203 platform_set_drvdata(pdev
, devptr
);
205 printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem
->start
, devptr
->base
);
207 iowrite16(0x0140, devptr
->base
+ SCOOP_MCR
);
208 reset_scoop(&pdev
->dev
);
209 iowrite16(0x0000, devptr
->base
+ SCOOP_CPR
);
210 iowrite16(inf
->io_dir
& 0xffff, devptr
->base
+ SCOOP_GPCR
);
211 iowrite16(inf
->io_out
& 0xffff, devptr
->base
+ SCOOP_GPWR
);
213 devptr
->suspend_clr
= inf
->suspend_clr
;
214 devptr
->suspend_set
= inf
->suspend_set
;
216 devptr
->gpio
.base
= -1;
218 if (inf
->gpio_base
!= 0) {
219 devptr
->gpio
.label
= dev_name(&pdev
->dev
);
220 devptr
->gpio
.base
= inf
->gpio_base
;
221 devptr
->gpio
.ngpio
= 12; /* PA11 = 0, PA12 = 1, etc. up to PA22 = 11 */
222 devptr
->gpio
.set
= scoop_gpio_set
;
223 devptr
->gpio
.get
= scoop_gpio_get
;
224 devptr
->gpio
.direction_input
= scoop_gpio_direction_input
;
225 devptr
->gpio
.direction_output
= scoop_gpio_direction_output
;
227 ret
= gpiochip_add(&devptr
->gpio
);
235 platform_set_drvdata(pdev
, NULL
);
237 iounmap(devptr
->base
);
243 static int scoop_remove(struct platform_device
*pdev
)
245 struct scoop_dev
*sdev
= platform_get_drvdata(pdev
);
250 if (sdev
->gpio
.base
!= -1)
251 gpiochip_remove(&sdev
->gpio
);
253 platform_set_drvdata(pdev
, NULL
);
260 static struct platform_driver scoop_driver
= {
261 .probe
= scoop_probe
,
262 .remove
= scoop_remove
,
263 .suspend
= scoop_suspend
,
264 .resume
= scoop_resume
,
266 .name
= "sharp-scoop",
270 static int __init
scoop_init(void)
272 return platform_driver_register(&scoop_driver
);
275 subsys_initcall(scoop_init
);