Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 15 | #include <linux/string.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 19 | #include <asm/gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/hardware/scoop.h> |
| 21 | |
Richard Purdie | 7ea3bbb | 2006-04-18 23:18:53 +0100 | [diff] [blame] | 22 | /* PCMCIA to Scoop linkage |
| 23 | |
| 24 | There is no easy way to link multiple scoop devices into one |
| 25 | single entity for the pxa2xx_pcmcia device so this structure |
| 26 | is used which is setup by the platform code. |
| 27 | |
| 28 | This file is never modular so this symbol is always |
| 29 | accessile to the board support files. |
| 30 | */ |
| 31 | struct scoop_pcmcia_config *platform_scoop_config; |
| 32 | EXPORT_SYMBOL(platform_scoop_config); |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct scoop_dev { |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 35 | void __iomem *base; |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 36 | struct gpio_chip gpio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | spinlock_t scoop_lock; |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 38 | unsigned short suspend_clr; |
| 39 | unsigned short suspend_set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | u32 scoop_gpwr; |
| 41 | }; |
| 42 | |
| 43 | void reset_scoop(struct device *dev) |
| 44 | { |
| 45 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
| 46 | |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 47 | iowrite16(0x0100, sdev->base + SCOOP_MCR); // 00 |
| 48 | iowrite16(0x0000, sdev->base + SCOOP_CDR); // 04 |
| 49 | iowrite16(0x0000, sdev->base + SCOOP_CCR); // 10 |
| 50 | iowrite16(0x0000, sdev->base + SCOOP_IMR); // 18 |
| 51 | iowrite16(0x00FF, sdev->base + SCOOP_IRM); // 14 |
| 52 | iowrite16(0x0000, sdev->base + SCOOP_ISR); // 1C |
| 53 | iowrite16(0x0000, sdev->base + SCOOP_IRM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 56 | static void __scoop_gpio_set(struct scoop_dev *sdev, |
| 57 | unsigned offset, int value) |
| 58 | { |
| 59 | unsigned short gpwr; |
| 60 | |
| 61 | gpwr = ioread16(sdev->base + SCOOP_GPWR); |
| 62 | if (value) |
| 63 | gpwr |= 1 << (offset + 1); |
| 64 | else |
| 65 | gpwr &= ~(1 << (offset + 1)); |
| 66 | iowrite16(gpwr, sdev->base + SCOOP_GPWR); |
| 67 | } |
| 68 | |
| 69 | static void scoop_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 70 | { |
| 71 | struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio); |
| 72 | unsigned long flags; |
| 73 | |
| 74 | spin_lock_irqsave(&sdev->scoop_lock, flags); |
| 75 | |
| 76 | __scoop_gpio_set(sdev, offset, value); |
| 77 | |
| 78 | spin_unlock_irqrestore(&sdev->scoop_lock, flags); |
| 79 | } |
| 80 | |
| 81 | static int scoop_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 82 | { |
| 83 | struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio); |
| 84 | |
| 85 | /* XXX: I'm usure, but it seems so */ |
| 86 | return ioread16(sdev->base + SCOOP_GPRR) & (1 << (offset + 1)); |
| 87 | } |
| 88 | |
| 89 | static int scoop_gpio_direction_input(struct gpio_chip *chip, |
| 90 | unsigned offset) |
| 91 | { |
| 92 | struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio); |
| 93 | unsigned long flags; |
| 94 | unsigned short gpcr; |
| 95 | |
| 96 | spin_lock_irqsave(&sdev->scoop_lock, flags); |
| 97 | |
| 98 | gpcr = ioread16(sdev->base + SCOOP_GPCR); |
| 99 | gpcr &= ~(1 << (offset + 1)); |
| 100 | iowrite16(gpcr, sdev->base + SCOOP_GPCR); |
| 101 | |
| 102 | spin_unlock_irqrestore(&sdev->scoop_lock, flags); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int scoop_gpio_direction_output(struct gpio_chip *chip, |
| 108 | unsigned offset, int value) |
| 109 | { |
| 110 | struct scoop_dev *sdev = container_of(chip, struct scoop_dev, gpio); |
| 111 | unsigned long flags; |
| 112 | unsigned short gpcr; |
| 113 | |
| 114 | spin_lock_irqsave(&sdev->scoop_lock, flags); |
| 115 | |
| 116 | __scoop_gpio_set(sdev, offset, value); |
| 117 | |
| 118 | gpcr = ioread16(sdev->base + SCOOP_GPCR); |
| 119 | gpcr |= 1 << (offset + 1); |
| 120 | iowrite16(gpcr, sdev->base + SCOOP_GPCR); |
| 121 | |
| 122 | spin_unlock_irqrestore(&sdev->scoop_lock, flags); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | unsigned short set_scoop_gpio(struct device *dev, unsigned short bit) |
| 128 | { |
| 129 | unsigned short gpio_bit; |
| 130 | unsigned long flag; |
| 131 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
| 132 | |
| 133 | spin_lock_irqsave(&sdev->scoop_lock, flag); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 134 | gpio_bit = ioread16(sdev->base + SCOOP_GPWR) | bit; |
| 135 | iowrite16(gpio_bit, sdev->base + SCOOP_GPWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | spin_unlock_irqrestore(&sdev->scoop_lock, flag); |
| 137 | |
| 138 | return gpio_bit; |
| 139 | } |
| 140 | |
| 141 | unsigned short reset_scoop_gpio(struct device *dev, unsigned short bit) |
| 142 | { |
| 143 | unsigned short gpio_bit; |
| 144 | unsigned long flag; |
| 145 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
| 146 | |
| 147 | spin_lock_irqsave(&sdev->scoop_lock, flag); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 148 | gpio_bit = ioread16(sdev->base + SCOOP_GPWR) & ~bit; |
| 149 | iowrite16(gpio_bit, sdev->base + SCOOP_GPWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | spin_unlock_irqrestore(&sdev->scoop_lock, flag); |
| 151 | |
| 152 | return gpio_bit; |
| 153 | } |
| 154 | |
| 155 | EXPORT_SYMBOL(set_scoop_gpio); |
| 156 | EXPORT_SYMBOL(reset_scoop_gpio); |
| 157 | |
| 158 | unsigned short read_scoop_reg(struct device *dev, unsigned short reg) |
| 159 | { |
| 160 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 161 | return ioread16(sdev->base + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data) |
| 165 | { |
| 166 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 167 | iowrite16(data, sdev->base + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | EXPORT_SYMBOL(reset_scoop); |
| 171 | EXPORT_SYMBOL(read_scoop_reg); |
| 172 | EXPORT_SYMBOL(write_scoop_reg); |
| 173 | |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 174 | static void check_scoop_reg(struct scoop_dev *sdev) |
| 175 | { |
| 176 | unsigned short mcr; |
| 177 | |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 178 | mcr = ioread16(sdev->base + SCOOP_MCR); |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 179 | if ((mcr & 0x100) == 0) |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 180 | iowrite16(0x0101, sdev->base + SCOOP_MCR); |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | #ifdef CONFIG_PM |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 184 | static int scoop_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 186 | struct scoop_dev *sdev = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 188 | check_scoop_reg(sdev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 189 | sdev->scoop_gpwr = ioread16(sdev->base + SCOOP_GPWR); |
| 190 | iowrite16((sdev->scoop_gpwr & ~sdev->suspend_clr) | sdev->suspend_set, sdev->base + SCOOP_GPWR); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 195 | static int scoop_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 197 | struct scoop_dev *sdev = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 199 | check_scoop_reg(sdev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 200 | iowrite16(sdev->scoop_gpwr, sdev->base + SCOOP_GPWR); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | #else |
| 205 | #define scoop_suspend NULL |
| 206 | #define scoop_resume NULL |
| 207 | #endif |
| 208 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 209 | static int __devinit scoop_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | struct scoop_dev *devptr; |
| 212 | struct scoop_config *inf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 214 | int ret; |
| 215 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | if (!mem) |
| 218 | return -EINVAL; |
| 219 | |
Russell King | d2a02b9 | 2006-03-20 19:46:41 +0000 | [diff] [blame] | 220 | devptr = kzalloc(sizeof(struct scoop_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (!devptr) |
Russell King | d2a02b9 | 2006-03-20 19:46:41 +0000 | [diff] [blame] | 222 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | spin_lock_init(&devptr->scoop_lock); |
| 225 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 226 | inf = pdev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | devptr->base = ioremap(mem->start, mem->end - mem->start + 1); |
| 228 | |
| 229 | if (!devptr->base) { |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 230 | ret = -ENOMEM; |
| 231 | goto err_ioremap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 234 | platform_set_drvdata(pdev, devptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 236 | printk("Sharp Scoop Device found at 0x%08x -> 0x%8p\n",(unsigned int)mem->start, devptr->base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 238 | iowrite16(0x0140, devptr->base + SCOOP_MCR); |
Pavel Machek | c35bf4a | 2005-11-12 20:25:25 +0000 | [diff] [blame] | 239 | reset_scoop(&pdev->dev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame] | 240 | iowrite16(0x0000, devptr->base + SCOOP_CPR); |
| 241 | iowrite16(inf->io_dir & 0xffff, devptr->base + SCOOP_GPCR); |
| 242 | iowrite16(inf->io_out & 0xffff, devptr->base + SCOOP_GPWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 244 | devptr->suspend_clr = inf->suspend_clr; |
| 245 | devptr->suspend_set = inf->suspend_set; |
| 246 | |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 247 | devptr->gpio.base = -1; |
| 248 | |
| 249 | if (inf->gpio_base != 0) { |
Kay Sievers | 3f97870 | 2008-05-30 17:42:11 +0200 | [diff] [blame] | 250 | devptr->gpio.label = dev_name(&pdev->dev); |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 251 | devptr->gpio.base = inf->gpio_base; |
| 252 | devptr->gpio.ngpio = 12; /* PA11 = 0, PA12 = 1, etc. up to PA22 = 11 */ |
| 253 | devptr->gpio.set = scoop_gpio_set; |
| 254 | devptr->gpio.get = scoop_gpio_get; |
| 255 | devptr->gpio.direction_input = scoop_gpio_direction_input; |
| 256 | devptr->gpio.direction_output = scoop_gpio_direction_output; |
| 257 | |
| 258 | ret = gpiochip_add(&devptr->gpio); |
| 259 | if (ret) |
| 260 | goto err_gpio; |
| 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return 0; |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 264 | |
| 265 | if (devptr->gpio.base != -1) |
| 266 | temp = gpiochip_remove(&devptr->gpio); |
| 267 | err_gpio: |
| 268 | platform_set_drvdata(pdev, NULL); |
| 269 | err_ioremap: |
| 270 | iounmap(devptr->base); |
| 271 | kfree(devptr); |
| 272 | |
| 273 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 276 | static int __devexit scoop_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 278 | struct scoop_dev *sdev = platform_get_drvdata(pdev); |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 279 | int ret; |
| 280 | |
| 281 | if (!sdev) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | if (sdev->gpio.base != -1) { |
| 285 | ret = gpiochip_remove(&sdev->gpio); |
| 286 | if (ret) { |
| 287 | dev_err(&pdev->dev, "Can't remove gpio chip: %d\n", ret); |
| 288 | return ret; |
| 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
Dmitry Baryshkov | b43a9e6 | 2008-04-10 13:36:53 +0100 | [diff] [blame] | 291 | |
| 292 | platform_set_drvdata(pdev, NULL); |
| 293 | iounmap(sdev->base); |
| 294 | kfree(sdev); |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 299 | static struct platform_driver scoop_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | .probe = scoop_probe, |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 301 | .remove = __devexit_p(scoop_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | .suspend = scoop_suspend, |
| 303 | .resume = scoop_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 304 | .driver = { |
| 305 | .name = "sharp-scoop", |
| 306 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | }; |
| 308 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 309 | static int __init scoop_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 311 | return platform_driver_register(&scoop_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | subsys_initcall(scoop_init); |