blob: 0c616d5fcb0f7bf3a6811ea08f7fd2dcc63c819b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Russell King2f8163b2011-07-26 10:53:52 +010015#include <linux/gpio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/string.h>
Tim Schmielaude259682006-01-08 01:02:05 -080017#include <linux/slab.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010018#include <linux/platform_device.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040019#include <linux/export.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/hardware/scoop.h>
22
Richard Purdie7ea3bbb2006-04-18 23:18:53 +010023/* PCMCIA to Scoop linkage
24
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.
28
29 This file is never modular so this symbol is always
30 accessile to the board support files.
31*/
32struct scoop_pcmcia_config *platform_scoop_config;
33EXPORT_SYMBOL(platform_scoop_config);
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct scoop_dev {
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +010036 void __iomem *base;
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010037 struct gpio_chip gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 spinlock_t scoop_lock;
Richard Purdie7c398982005-10-10 10:20:06 +010039 unsigned short suspend_clr;
40 unsigned short suspend_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 u32 scoop_gpwr;
42};
43
44void reset_scoop(struct device *dev)
45{
46 struct scoop_dev *sdev = dev_get_drvdata(dev);
47
H Hartley Sweetenaa88bc02010-08-06 09:36:42 -070048 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 */
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +010054 iowrite16(0x0000, sdev->base + SCOOP_IRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010057static void __scoop_gpio_set(struct scoop_dev *sdev,
58 unsigned offset, int value)
59{
60 unsigned short gpwr;
61
62 gpwr = ioread16(sdev->base + SCOOP_GPWR);
63 if (value)
64 gpwr |= 1 << (offset + 1);
65 else
66 gpwr &= ~(1 << (offset + 1));
67 iowrite16(gpwr, sdev->base + SCOOP_GPWR);
68}
69
70static void scoop_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
71{
72 struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
73 unsigned long flags;
74
75 spin_lock_irqsave(&sdev->scoop_lock, flags);
76
77 __scoop_gpio_set(sdev, offset, value);
78
79 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
80}
81
82static int scoop_gpio_get(struct gpio_chip *chip, unsigned offset)
83{
84 struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
85
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020086 /* XXX: I'm unsure, but it seems so */
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010087 return ioread16(sdev->base + SCOOP_GPRR) & (1 << (offset + 1));
88}
89
90static int scoop_gpio_direction_input(struct gpio_chip *chip,
91 unsigned offset)
92{
93 struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
94 unsigned long flags;
95 unsigned short gpcr;
96
97 spin_lock_irqsave(&sdev->scoop_lock, flags);
98
99 gpcr = ioread16(sdev->base + SCOOP_GPCR);
100 gpcr &= ~(1 << (offset + 1));
101 iowrite16(gpcr, sdev->base + SCOOP_GPCR);
102
103 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
104
105 return 0;
106}
107
108static int scoop_gpio_direction_output(struct gpio_chip *chip,
109 unsigned offset, int value)
110{
111 struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
112 unsigned long flags;
113 unsigned short gpcr;
114
115 spin_lock_irqsave(&sdev->scoop_lock, flags);
116
117 __scoop_gpio_set(sdev, offset, value);
118
119 gpcr = ioread16(sdev->base + SCOOP_GPCR);
120 gpcr |= 1 << (offset + 1);
121 iowrite16(gpcr, sdev->base + SCOOP_GPCR);
122
123 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
124
125 return 0;
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128unsigned short read_scoop_reg(struct device *dev, unsigned short reg)
129{
130 struct scoop_dev *sdev = dev_get_drvdata(dev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100131 return ioread16(sdev->base + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data)
135{
136 struct scoop_dev *sdev = dev_get_drvdata(dev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100137 iowrite16(data, sdev->base + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140EXPORT_SYMBOL(reset_scoop);
141EXPORT_SYMBOL(read_scoop_reg);
142EXPORT_SYMBOL(write_scoop_reg);
143
Stefan Schmidtcfab57e2010-02-16 22:41:52 +0100144#ifdef CONFIG_PM
Richard Purdie7c398982005-10-10 10:20:06 +0100145static void check_scoop_reg(struct scoop_dev *sdev)
146{
147 unsigned short mcr;
148
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100149 mcr = ioread16(sdev->base + SCOOP_MCR);
Richard Purdie7c398982005-10-10 10:20:06 +0100150 if ((mcr & 0x100) == 0)
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100151 iowrite16(0x0101, sdev->base + SCOOP_MCR);
Richard Purdie7c398982005-10-10 10:20:06 +0100152}
153
Russell King3ae5eae2005-11-09 22:32:44 +0000154static int scoop_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Russell King3ae5eae2005-11-09 22:32:44 +0000156 struct scoop_dev *sdev = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Russell King9480e302005-10-28 09:52:56 -0700158 check_scoop_reg(sdev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100159 sdev->scoop_gpwr = ioread16(sdev->base + SCOOP_GPWR);
160 iowrite16((sdev->scoop_gpwr & ~sdev->suspend_clr) | sdev->suspend_set, sdev->base + SCOOP_GPWR);
Russell King9480e302005-10-28 09:52:56 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return 0;
163}
164
Russell King3ae5eae2005-11-09 22:32:44 +0000165static int scoop_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Russell King3ae5eae2005-11-09 22:32:44 +0000167 struct scoop_dev *sdev = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Russell King9480e302005-10-28 09:52:56 -0700169 check_scoop_reg(sdev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100170 iowrite16(sdev->scoop_gpwr, sdev->base + SCOOP_GPWR);
Russell King9480e302005-10-28 09:52:56 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174#else
175#define scoop_suspend NULL
176#define scoop_resume NULL
177#endif
178
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100179static int __devinit scoop_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct scoop_dev *devptr;
182 struct scoop_config *inf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100184 int ret;
185 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (!mem)
188 return -EINVAL;
189
Russell Kingd2a02b92006-03-20 19:46:41 +0000190 devptr = kzalloc(sizeof(struct scoop_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!devptr)
Russell Kingd2a02b92006-03-20 19:46:41 +0000192 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 spin_lock_init(&devptr->scoop_lock);
195
Russell King3ae5eae2005-11-09 22:32:44 +0000196 inf = pdev->dev.platform_data;
Joe Perches28f65c112011-06-09 09:13:32 -0700197 devptr->base = ioremap(mem->start, resource_size(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (!devptr->base) {
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100200 ret = -ENOMEM;
201 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Russell King3ae5eae2005-11-09 22:32:44 +0000204 platform_set_drvdata(pdev, devptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100206 printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem->start, devptr->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100208 iowrite16(0x0140, devptr->base + SCOOP_MCR);
Pavel Machekc35bf4a2005-11-12 20:25:25 +0000209 reset_scoop(&pdev->dev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100210 iowrite16(0x0000, devptr->base + SCOOP_CPR);
211 iowrite16(inf->io_dir & 0xffff, devptr->base + SCOOP_GPCR);
212 iowrite16(inf->io_out & 0xffff, devptr->base + SCOOP_GPWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Richard Purdie7c398982005-10-10 10:20:06 +0100214 devptr->suspend_clr = inf->suspend_clr;
215 devptr->suspend_set = inf->suspend_set;
216
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100217 devptr->gpio.base = -1;
218
219 if (inf->gpio_base != 0) {
Kay Sievers3f978702008-05-30 17:42:11 +0200220 devptr->gpio.label = dev_name(&pdev->dev);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100221 devptr->gpio.base = inf->gpio_base;
222 devptr->gpio.ngpio = 12; /* PA11 = 0, PA12 = 1, etc. up to PA22 = 11 */
223 devptr->gpio.set = scoop_gpio_set;
224 devptr->gpio.get = scoop_gpio_get;
225 devptr->gpio.direction_input = scoop_gpio_direction_input;
226 devptr->gpio.direction_output = scoop_gpio_direction_output;
227
228 ret = gpiochip_add(&devptr->gpio);
229 if (ret)
230 goto err_gpio;
231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100234
235 if (devptr->gpio.base != -1)
236 temp = gpiochip_remove(&devptr->gpio);
237err_gpio:
238 platform_set_drvdata(pdev, NULL);
239err_ioremap:
240 iounmap(devptr->base);
241 kfree(devptr);
242
243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100246static int __devexit scoop_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Russell King3ae5eae2005-11-09 22:32:44 +0000248 struct scoop_dev *sdev = platform_get_drvdata(pdev);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100249 int ret;
250
251 if (!sdev)
252 return -EINVAL;
253
254 if (sdev->gpio.base != -1) {
255 ret = gpiochip_remove(&sdev->gpio);
256 if (ret) {
257 dev_err(&pdev->dev, "Can't remove gpio chip: %d\n", ret);
258 return ret;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100261
262 platform_set_drvdata(pdev, NULL);
263 iounmap(sdev->base);
264 kfree(sdev);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267}
268
Russell King3ae5eae2005-11-09 22:32:44 +0000269static struct platform_driver scoop_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .probe = scoop_probe,
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100271 .remove = __devexit_p(scoop_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .suspend = scoop_suspend,
273 .resume = scoop_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000274 .driver = {
275 .name = "sharp-scoop",
276 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100279static int __init scoop_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Russell King3ae5eae2005-11-09 22:32:44 +0000281 return platform_driver_register(&scoop_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284subsys_initcall(scoop_init);