blob: 60130bd7b182057e7acf4a106b9daadac59bc8d4 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Support code for the SCOOP interface found on various Sharp PDAs
4 *
5 * Copyright (c) 2004 Richard Purdie
6 *
7 * Based on code written by Sharp/Lineo for 2.4 kernels
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/device.h>
Russell King2f8163b2011-07-26 10:53:52 +010011#include <linux/gpio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080012#include <linux/string.h>
Tim Schmielaude259682006-01-08 01:02:05 -080013#include <linux/slab.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010014#include <linux/platform_device.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040015#include <linux/export.h>
Russell Kingfced80c2008-09-06 12:10:45 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/hardware/scoop.h>
18
Richard Purdie7ea3bbb2006-04-18 23:18:53 +010019/* PCMCIA to Scoop linkage
20
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.
24
25 This file is never modular so this symbol is always
26 accessile to the board support files.
27*/
28struct scoop_pcmcia_config *platform_scoop_config;
29EXPORT_SYMBOL(platform_scoop_config);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct scoop_dev {
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +010032 void __iomem *base;
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010033 struct gpio_chip gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 spinlock_t scoop_lock;
Richard Purdie7c398982005-10-10 10:20:06 +010035 unsigned short suspend_clr;
36 unsigned short suspend_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 u32 scoop_gpwr;
38};
39
40void reset_scoop(struct device *dev)
41{
42 struct scoop_dev *sdev = dev_get_drvdata(dev);
43
H Hartley Sweetenaa88bc02010-08-06 09:36:42 -070044 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 */
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +010050 iowrite16(0x0000, sdev->base + SCOOP_IRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010053static void __scoop_gpio_set(struct scoop_dev *sdev,
54 unsigned offset, int value)
55{
56 unsigned short gpwr;
57
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);
64}
65
66static void scoop_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
67{
Linus Walleij050c5422015-12-08 10:51:43 +010068 struct scoop_dev *sdev = gpiochip_get_data(chip);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010069 unsigned long flags;
70
71 spin_lock_irqsave(&sdev->scoop_lock, flags);
72
73 __scoop_gpio_set(sdev, offset, value);
74
75 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
76}
77
78static int scoop_gpio_get(struct gpio_chip *chip, unsigned offset)
79{
Linus Walleij050c5422015-12-08 10:51:43 +010080 struct scoop_dev *sdev = gpiochip_get_data(chip);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010081
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020082 /* XXX: I'm unsure, but it seems so */
Linus Walleij86d5e652015-12-22 15:35:47 +010083 return !!(ioread16(sdev->base + SCOOP_GPRR) & (1 << (offset + 1)));
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010084}
85
86static int scoop_gpio_direction_input(struct gpio_chip *chip,
87 unsigned offset)
88{
Linus Walleij050c5422015-12-08 10:51:43 +010089 struct scoop_dev *sdev = gpiochip_get_data(chip);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +010090 unsigned long flags;
91 unsigned short gpcr;
92
93 spin_lock_irqsave(&sdev->scoop_lock, flags);
94
95 gpcr = ioread16(sdev->base + SCOOP_GPCR);
96 gpcr &= ~(1 << (offset + 1));
97 iowrite16(gpcr, sdev->base + SCOOP_GPCR);
98
99 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
100
101 return 0;
102}
103
104static int scoop_gpio_direction_output(struct gpio_chip *chip,
105 unsigned offset, int value)
106{
Linus Walleij050c5422015-12-08 10:51:43 +0100107 struct scoop_dev *sdev = gpiochip_get_data(chip);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100108 unsigned long flags;
109 unsigned short gpcr;
110
111 spin_lock_irqsave(&sdev->scoop_lock, flags);
112
113 __scoop_gpio_set(sdev, offset, value);
114
115 gpcr = ioread16(sdev->base + SCOOP_GPCR);
116 gpcr |= 1 << (offset + 1);
117 iowrite16(gpcr, sdev->base + SCOOP_GPCR);
118
119 spin_unlock_irqrestore(&sdev->scoop_lock, flags);
120
121 return 0;
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124unsigned short read_scoop_reg(struct device *dev, unsigned short reg)
125{
126 struct scoop_dev *sdev = dev_get_drvdata(dev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100127 return ioread16(sdev->base + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data)
131{
132 struct scoop_dev *sdev = dev_get_drvdata(dev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100133 iowrite16(data, sdev->base + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136EXPORT_SYMBOL(reset_scoop);
137EXPORT_SYMBOL(read_scoop_reg);
138EXPORT_SYMBOL(write_scoop_reg);
139
Stefan Schmidtcfab57e2010-02-16 22:41:52 +0100140#ifdef CONFIG_PM
Richard Purdie7c398982005-10-10 10:20:06 +0100141static void check_scoop_reg(struct scoop_dev *sdev)
142{
143 unsigned short mcr;
144
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100145 mcr = ioread16(sdev->base + SCOOP_MCR);
Richard Purdie7c398982005-10-10 10:20:06 +0100146 if ((mcr & 0x100) == 0)
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100147 iowrite16(0x0101, sdev->base + SCOOP_MCR);
Richard Purdie7c398982005-10-10 10:20:06 +0100148}
149
Russell King3ae5eae2005-11-09 22:32:44 +0000150static int scoop_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Russell King3ae5eae2005-11-09 22:32:44 +0000152 struct scoop_dev *sdev = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Russell King9480e302005-10-28 09:52:56 -0700154 check_scoop_reg(sdev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100155 sdev->scoop_gpwr = ioread16(sdev->base + SCOOP_GPWR);
156 iowrite16((sdev->scoop_gpwr & ~sdev->suspend_clr) | sdev->suspend_set, sdev->base + SCOOP_GPWR);
Russell King9480e302005-10-28 09:52:56 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
159}
160
Russell King3ae5eae2005-11-09 22:32:44 +0000161static int scoop_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Russell King3ae5eae2005-11-09 22:32:44 +0000163 struct scoop_dev *sdev = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Russell King9480e302005-10-28 09:52:56 -0700165 check_scoop_reg(sdev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100166 iowrite16(sdev->scoop_gpwr, sdev->base + SCOOP_GPWR);
Russell King9480e302005-10-28 09:52:56 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 0;
169}
170#else
171#define scoop_suspend NULL
172#define scoop_resume NULL
173#endif
174
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800175static int scoop_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct scoop_dev *devptr;
178 struct scoop_config *inf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100180 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (!mem)
183 return -EINVAL;
184
Russell Kingd2a02b92006-03-20 19:46:41 +0000185 devptr = kzalloc(sizeof(struct scoop_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (!devptr)
Russell Kingd2a02b92006-03-20 19:46:41 +0000187 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 spin_lock_init(&devptr->scoop_lock);
190
Russell King3ae5eae2005-11-09 22:32:44 +0000191 inf = pdev->dev.platform_data;
Joe Perches28f65c112011-06-09 09:13:32 -0700192 devptr->base = ioremap(mem->start, resource_size(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (!devptr->base) {
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100195 ret = -ENOMEM;
196 goto err_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Russell King3ae5eae2005-11-09 22:32:44 +0000199 platform_set_drvdata(pdev, devptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100201 printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem->start, devptr->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100203 iowrite16(0x0140, devptr->base + SCOOP_MCR);
Pavel Machekc35bf4a2005-11-12 20:25:25 +0000204 reset_scoop(&pdev->dev);
Dmitry Baryshkovc353faa2008-04-09 23:05:09 +0100205 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Richard Purdie7c398982005-10-10 10:20:06 +0100209 devptr->suspend_clr = inf->suspend_clr;
210 devptr->suspend_set = inf->suspend_set;
211
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100212 devptr->gpio.base = -1;
213
214 if (inf->gpio_base != 0) {
Kay Sievers3f978702008-05-30 17:42:11 +0200215 devptr->gpio.label = dev_name(&pdev->dev);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100216 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;
222
Linus Walleij050c5422015-12-08 10:51:43 +0100223 ret = gpiochip_add_data(&devptr->gpio, devptr);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100224 if (ret)
225 goto err_gpio;
226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100229
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100230err_gpio:
231 platform_set_drvdata(pdev, NULL);
232err_ioremap:
233 iounmap(devptr->base);
234 kfree(devptr);
235
236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800239static int scoop_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Russell King3ae5eae2005-11-09 22:32:44 +0000241 struct scoop_dev *sdev = platform_get_drvdata(pdev);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100242
243 if (!sdev)
244 return -EINVAL;
245
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200246 if (sdev->gpio.base != -1)
247 gpiochip_remove(&sdev->gpio);
Dmitry Baryshkovb43a9e62008-04-10 13:36:53 +0100248
249 platform_set_drvdata(pdev, NULL);
250 iounmap(sdev->base);
251 kfree(sdev);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
Russell King3ae5eae2005-11-09 22:32:44 +0000256static struct platform_driver scoop_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .probe = scoop_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800258 .remove = scoop_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .suspend = scoop_suspend,
260 .resume = scoop_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000261 .driver = {
262 .name = "sharp-scoop",
263 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
Dmitry Baryshkov2f8c5142008-04-09 22:43:37 +0100266static int __init scoop_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Russell King3ae5eae2005-11-09 22:32:44 +0000268 return platform_driver_register(&scoop_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271subsys_initcall(scoop_init);