Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / arm / common / scoop.c
blob6edb961bd6c1d246872d227585a84c6af02df389
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Support code for the SCOOP interface found on various Sharp PDAs
5 * Copyright (c) 2004 Richard Purdie
7 * Based on code written by Sharp/Lineo for 2.4 kernels
8 */
10 #include <linux/device.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/export.h>
16 #include <linux/io.h>
17 #include <asm/hardware/scoop.h>
19 /* PCMCIA to Scoop linkage
21 There is no easy way to link multiple scoop devices into one
22 single entity for the pxa2xx_pcmcia device so this structure
23 is used which is setup by the platform code.
25 This file is never modular so this symbol is always
26 accessile to the board support files.
28 struct scoop_pcmcia_config *platform_scoop_config;
29 EXPORT_SYMBOL(platform_scoop_config);
31 struct scoop_dev {
32 void __iomem *base;
33 struct gpio_chip gpio;
34 spinlock_t scoop_lock;
35 unsigned short suspend_clr;
36 unsigned short suspend_set;
37 u32 scoop_gpwr;
40 void reset_scoop(struct device *dev)
42 struct scoop_dev *sdev = dev_get_drvdata(dev);
44 iowrite16(0x0100, sdev->base + SCOOP_MCR); /* 00 */
45 iowrite16(0x0000, sdev->base + SCOOP_CDR); /* 04 */
46 iowrite16(0x0000, sdev->base + SCOOP_CCR); /* 10 */
47 iowrite16(0x0000, sdev->base + SCOOP_IMR); /* 18 */
48 iowrite16(0x00FF, sdev->base + SCOOP_IRM); /* 14 */
49 iowrite16(0x0000, sdev->base + SCOOP_ISR); /* 1C */
50 iowrite16(0x0000, sdev->base + SCOOP_IRM);
53 static void __scoop_gpio_set(struct scoop_dev *sdev,
54 unsigned offset, int value)
56 unsigned short gpwr;
58 gpwr = ioread16(sdev->base + SCOOP_GPWR);
59 if (value)
60 gpwr |= 1 << (offset + 1);
61 else
62 gpwr &= ~(1 << (offset + 1));
63 iowrite16(gpwr, sdev->base + SCOOP_GPWR);
66 static void scoop_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
68 struct scoop_dev *sdev = gpiochip_get_data(chip);
69 unsigned long flags;
71 spin_lock_irqsave(&sdev->scoop_lock, flags);
73 __scoop_gpio_set(sdev, offset, value);
75 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
78 static int scoop_gpio_get(struct gpio_chip *chip, unsigned offset)
80 struct scoop_dev *sdev = gpiochip_get_data(chip);
82 /* XXX: I'm unsure, but it seems so */
83 return !!(ioread16(sdev->base + SCOOP_GPRR) & (1 << (offset + 1)));
86 static int scoop_gpio_direction_input(struct gpio_chip *chip,
87 unsigned offset)
89 struct scoop_dev *sdev = gpiochip_get_data(chip);
90 unsigned long flags;
91 unsigned short gpcr;
93 spin_lock_irqsave(&sdev->scoop_lock, flags);
95 gpcr = ioread16(sdev->base + SCOOP_GPCR);
96 gpcr &= ~(1 << (offset + 1));
97 iowrite16(gpcr, sdev->base + SCOOP_GPCR);
99 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
101 return 0;
104 static int scoop_gpio_direction_output(struct gpio_chip *chip,
105 unsigned offset, int value)
107 struct scoop_dev *sdev = gpiochip_get_data(chip);
108 unsigned long flags;
109 unsigned short gpcr;
111 spin_lock_irqsave(&sdev->scoop_lock, flags);
113 __scoop_gpio_set(sdev, offset, value);
115 gpcr = ioread16(sdev->base + SCOOP_GPCR);
116 gpcr |= 1 << (offset + 1);
117 iowrite16(gpcr, sdev->base + SCOOP_GPCR);
119 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
121 return 0;
124 unsigned short read_scoop_reg(struct device *dev, unsigned short reg)
126 struct scoop_dev *sdev = dev_get_drvdata(dev);
127 return ioread16(sdev->base + reg);
130 void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data)
132 struct scoop_dev *sdev = dev_get_drvdata(dev);
133 iowrite16(data, sdev->base + reg);
136 EXPORT_SYMBOL(reset_scoop);
137 EXPORT_SYMBOL(read_scoop_reg);
138 EXPORT_SYMBOL(write_scoop_reg);
140 #ifdef CONFIG_PM
141 static void check_scoop_reg(struct scoop_dev *sdev)
143 unsigned short mcr;
145 mcr = ioread16(sdev->base + SCOOP_MCR);
146 if ((mcr & 0x100) == 0)
147 iowrite16(0x0101, sdev->base + SCOOP_MCR);
150 static int scoop_suspend(struct platform_device *dev, pm_message_t state)
152 struct scoop_dev *sdev = platform_get_drvdata(dev);
154 check_scoop_reg(sdev);
155 sdev->scoop_gpwr = ioread16(sdev->base + SCOOP_GPWR);
156 iowrite16((sdev->scoop_gpwr & ~sdev->suspend_clr) | sdev->suspend_set, sdev->base + SCOOP_GPWR);
158 return 0;
161 static int scoop_resume(struct platform_device *dev)
163 struct scoop_dev *sdev = platform_get_drvdata(dev);
165 check_scoop_reg(sdev);
166 iowrite16(sdev->scoop_gpwr, sdev->base + SCOOP_GPWR);
168 return 0;
170 #else
171 #define scoop_suspend NULL
172 #define scoop_resume NULL
173 #endif
175 static int scoop_probe(struct platform_device *pdev)
177 struct scoop_dev *devptr;
178 struct scoop_config *inf;
179 struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 int ret;
182 if (!mem)
183 return -EINVAL;
185 devptr = kzalloc(sizeof(struct scoop_dev), GFP_KERNEL);
186 if (!devptr)
187 return -ENOMEM;
189 spin_lock_init(&devptr->scoop_lock);
191 inf = pdev->dev.platform_data;
192 devptr->base = ioremap(mem->start, resource_size(mem));
194 if (!devptr->base) {
195 ret = -ENOMEM;
196 goto err_ioremap;
199 platform_set_drvdata(pdev, devptr);
201 printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem->start, devptr->base);
203 iowrite16(0x0140, devptr->base + SCOOP_MCR);
204 reset_scoop(&pdev->dev);
205 iowrite16(0x0000, devptr->base + SCOOP_CPR);
206 iowrite16(inf->io_dir & 0xffff, devptr->base + SCOOP_GPCR);
207 iowrite16(inf->io_out & 0xffff, devptr->base + SCOOP_GPWR);
209 devptr->suspend_clr = inf->suspend_clr;
210 devptr->suspend_set = inf->suspend_set;
212 devptr->gpio.base = -1;
214 if (inf->gpio_base != 0) {
215 devptr->gpio.label = dev_name(&pdev->dev);
216 devptr->gpio.base = inf->gpio_base;
217 devptr->gpio.ngpio = 12; /* PA11 = 0, PA12 = 1, etc. up to PA22 = 11 */
218 devptr->gpio.set = scoop_gpio_set;
219 devptr->gpio.get = scoop_gpio_get;
220 devptr->gpio.direction_input = scoop_gpio_direction_input;
221 devptr->gpio.direction_output = scoop_gpio_direction_output;
223 ret = gpiochip_add_data(&devptr->gpio, devptr);
224 if (ret)
225 goto err_gpio;
228 return 0;
230 err_gpio:
231 platform_set_drvdata(pdev, NULL);
232 err_ioremap:
233 iounmap(devptr->base);
234 kfree(devptr);
236 return ret;
239 static int scoop_remove(struct platform_device *pdev)
241 struct scoop_dev *sdev = platform_get_drvdata(pdev);
243 if (!sdev)
244 return -EINVAL;
246 if (sdev->gpio.base != -1)
247 gpiochip_remove(&sdev->gpio);
249 platform_set_drvdata(pdev, NULL);
250 iounmap(sdev->base);
251 kfree(sdev);
253 return 0;
256 static struct platform_driver scoop_driver = {
257 .probe = scoop_probe,
258 .remove = scoop_remove,
259 .suspend = scoop_suspend,
260 .resume = scoop_resume,
261 .driver = {
262 .name = "sharp-scoop",
266 static int __init scoop_init(void)
268 return platform_driver_register(&scoop_driver);
271 subsys_initcall(scoop_init);