scoop.c 6.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Support code for the SCOOP interface found on various Sharp PDAs
 *
 * Copyright (c) 2004 Richard Purdie
 *
 *	Based on code written by Sharp/Lineo for 2.4 kernels
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/device.h>
15
#include <linux/gpio.h>
T
Tim Schmielau 已提交
16
#include <linux/string.h>
17
#include <linux/slab.h>
18
#include <linux/platform_device.h>
19
#include <linux/export.h>
20
#include <linux/io.h>
L
Linus Torvalds 已提交
21 22
#include <asm/hardware/scoop.h>

23 24 25 26 27 28 29 30 31 32 33 34
/* PCMCIA to Scoop linkage

   There is no easy way to link multiple scoop devices into one
   single entity for the pxa2xx_pcmcia device so this structure
   is used which is setup by the platform code.

   This file is never modular so this symbol is always
   accessile to the board support files.
*/
struct scoop_pcmcia_config *platform_scoop_config;
EXPORT_SYMBOL(platform_scoop_config);

L
Linus Torvalds 已提交
35
struct  scoop_dev {
36
	void __iomem *base;
37
	struct gpio_chip gpio;
L
Linus Torvalds 已提交
38
	spinlock_t scoop_lock;
39 40
	unsigned short suspend_clr;
	unsigned short suspend_set;
L
Linus Torvalds 已提交
41 42 43 44 45 46 47
	u32 scoop_gpwr;
};

void reset_scoop(struct device *dev)
{
	struct scoop_dev *sdev = dev_get_drvdata(dev);

48 49 50 51 52 53
	iowrite16(0x0100, sdev->base + SCOOP_MCR);  /* 00 */
	iowrite16(0x0000, sdev->base + SCOOP_CDR);  /* 04 */
	iowrite16(0x0000, sdev->base + SCOOP_CCR);  /* 10 */
	iowrite16(0x0000, sdev->base + SCOOP_IMR);  /* 18 */
	iowrite16(0x00FF, sdev->base + SCOOP_IRM);  /* 14 */
	iowrite16(0x0000, sdev->base + SCOOP_ISR);  /* 1C */
54
	iowrite16(0x0000, sdev->base + SCOOP_IRM);
L
Linus Torvalds 已提交
55 56
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static void __scoop_gpio_set(struct scoop_dev *sdev,
			unsigned offset, int value)
{
	unsigned short gpwr;

	gpwr = ioread16(sdev->base + SCOOP_GPWR);
	if (value)
		gpwr |= 1 << (offset + 1);
	else
		gpwr &= ~(1 << (offset + 1));
	iowrite16(gpwr, sdev->base + SCOOP_GPWR);
}

static void scoop_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
	unsigned long flags;

	spin_lock_irqsave(&sdev->scoop_lock, flags);

	__scoop_gpio_set(sdev, offset, value);

	spin_unlock_irqrestore(&sdev->scoop_lock, flags);
}

static int scoop_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	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)));
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

static int scoop_gpio_direction_input(struct gpio_chip *chip,
			unsigned offset)
{
	struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
	unsigned long flags;
	unsigned short gpcr;

	spin_lock_irqsave(&sdev->scoop_lock, flags);

	gpcr = ioread16(sdev->base + SCOOP_GPCR);
	gpcr &= ~(1 << (offset + 1));
	iowrite16(gpcr, sdev->base + SCOOP_GPCR);

	spin_unlock_irqrestore(&sdev->scoop_lock, flags);

	return 0;
}

static int scoop_gpio_direction_output(struct gpio_chip *chip,
			unsigned offset, int value)
{
	struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio);
	unsigned long flags;
	unsigned short gpcr;

	spin_lock_irqsave(&sdev->scoop_lock, flags);

	__scoop_gpio_set(sdev, offset, value);

	gpcr = ioread16(sdev->base + SCOOP_GPCR);
	gpcr |= 1 << (offset + 1);
	iowrite16(gpcr, sdev->base + SCOOP_GPCR);

	spin_unlock_irqrestore(&sdev->scoop_lock, flags);

	return 0;
}

L
Linus Torvalds 已提交
128 129 130
unsigned short read_scoop_reg(struct device *dev, unsigned short reg)
{
	struct scoop_dev *sdev = dev_get_drvdata(dev);
131
	return ioread16(sdev->base + reg);
L
Linus Torvalds 已提交
132 133 134 135 136
}

void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data)
{
	struct scoop_dev *sdev = dev_get_drvdata(dev);
137
	iowrite16(data, sdev->base + reg);
L
Linus Torvalds 已提交
138 139 140 141 142 143
}

EXPORT_SYMBOL(reset_scoop);
EXPORT_SYMBOL(read_scoop_reg);
EXPORT_SYMBOL(write_scoop_reg);

144
#ifdef CONFIG_PM
145 146 147 148
static void check_scoop_reg(struct scoop_dev *sdev)
{
	unsigned short mcr;

149
	mcr = ioread16(sdev->base + SCOOP_MCR);
150
	if ((mcr & 0x100) == 0)
151
		iowrite16(0x0101, sdev->base + SCOOP_MCR);
152 153
}

154
static int scoop_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
155
{
156
	struct scoop_dev *sdev = platform_get_drvdata(dev);
157 158

	check_scoop_reg(sdev);
159 160
	sdev->scoop_gpwr = ioread16(sdev->base + SCOOP_GPWR);
	iowrite16((sdev->scoop_gpwr & ~sdev->suspend_clr) | sdev->suspend_set, sdev->base + SCOOP_GPWR);
L
Linus Torvalds 已提交
161 162 163 164

	return 0;
}

165
static int scoop_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
166
{
167
	struct scoop_dev *sdev = platform_get_drvdata(dev);
168 169

	check_scoop_reg(sdev);
170
	iowrite16(sdev->scoop_gpwr, sdev->base + SCOOP_GPWR);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178

	return 0;
}
#else
#define scoop_suspend	NULL
#define scoop_resume	NULL
#endif

179
static int scoop_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
180 181 182 183
{
	struct scoop_dev *devptr;
	struct scoop_config *inf;
	struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
184
	int ret;
L
Linus Torvalds 已提交
185 186 187 188

	if (!mem)
		return -EINVAL;

189
	devptr = kzalloc(sizeof(struct scoop_dev), GFP_KERNEL);
L
Linus Torvalds 已提交
190
	if (!devptr)
191
		return -ENOMEM;
L
Linus Torvalds 已提交
192 193 194

	spin_lock_init(&devptr->scoop_lock);

195
	inf = pdev->dev.platform_data;
196
	devptr->base = ioremap(mem->start, resource_size(mem));
L
Linus Torvalds 已提交
197 198

	if (!devptr->base) {
199 200
		ret = -ENOMEM;
		goto err_ioremap;
L
Linus Torvalds 已提交
201 202
	}

203
	platform_set_drvdata(pdev, devptr);
L
Linus Torvalds 已提交
204

205
	printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem->start, devptr->base);
L
Linus Torvalds 已提交
206

207
	iowrite16(0x0140, devptr->base + SCOOP_MCR);
P
Pavel Machek 已提交
208
	reset_scoop(&pdev->dev);
209 210 211
	iowrite16(0x0000, devptr->base + SCOOP_CPR);
	iowrite16(inf->io_dir & 0xffff, devptr->base + SCOOP_GPCR);
	iowrite16(inf->io_out & 0xffff, devptr->base + SCOOP_GPWR);
L
Linus Torvalds 已提交
212

213 214 215
	devptr->suspend_clr = inf->suspend_clr;
	devptr->suspend_set = inf->suspend_set;

216 217 218
	devptr->gpio.base = -1;

	if (inf->gpio_base != 0) {
219
		devptr->gpio.label = dev_name(&pdev->dev);
220 221 222 223 224 225 226 227 228 229 230 231
		devptr->gpio.base = inf->gpio_base;
		devptr->gpio.ngpio = 12; /* PA11 = 0, PA12 = 1, etc. up to PA22 = 11 */
		devptr->gpio.set = scoop_gpio_set;
		devptr->gpio.get = scoop_gpio_get;
		devptr->gpio.direction_input = scoop_gpio_direction_input;
		devptr->gpio.direction_output = scoop_gpio_direction_output;

		ret = gpiochip_add(&devptr->gpio);
		if (ret)
			goto err_gpio;
	}

L
Linus Torvalds 已提交
232
	return 0;
233 234 235 236 237 238 239 240

err_gpio:
	platform_set_drvdata(pdev, NULL);
err_ioremap:
	iounmap(devptr->base);
	kfree(devptr);

	return ret;
L
Linus Torvalds 已提交
241 242
}

243
static int scoop_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
244
{
245
	struct scoop_dev *sdev = platform_get_drvdata(pdev);
246 247 248 249

	if (!sdev)
		return -EINVAL;

250 251
	if (sdev->gpio.base != -1)
		gpiochip_remove(&sdev->gpio);
252 253 254 255 256

	platform_set_drvdata(pdev, NULL);
	iounmap(sdev->base);
	kfree(sdev);

L
Linus Torvalds 已提交
257 258 259
	return 0;
}

260
static struct platform_driver scoop_driver = {
L
Linus Torvalds 已提交
261
	.probe		= scoop_probe,
262
	.remove		= scoop_remove,
L
Linus Torvalds 已提交
263 264
	.suspend	= scoop_suspend,
	.resume		= scoop_resume,
265 266 267
	.driver		= {
		.name	= "sharp-scoop",
	},
L
Linus Torvalds 已提交
268 269
};

270
static int __init scoop_init(void)
L
Linus Torvalds 已提交
271
{
272
	return platform_driver_register(&scoop_driver);
L
Linus Torvalds 已提交
273 274 275
}

subsys_initcall(scoop_init);