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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <asm/hardware/scoop.h> |
| 20 | |
Richard Purdie | 7ea3bbb | 2006-04-18 23:18:53 +0100 | [diff] [blame] | 21 | /* PCMCIA to Scoop linkage |
| 22 | |
| 23 | There is no easy way to link multiple scoop devices into one |
| 24 | single entity for the pxa2xx_pcmcia device so this structure |
| 25 | is used which is setup by the platform code. |
| 26 | |
| 27 | This file is never modular so this symbol is always |
| 28 | accessile to the board support files. |
| 29 | */ |
| 30 | struct scoop_pcmcia_config *platform_scoop_config; |
| 31 | EXPORT_SYMBOL(platform_scoop_config); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | struct scoop_dev { |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 34 | void __iomem *base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | spinlock_t scoop_lock; |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 36 | unsigned short suspend_clr; |
| 37 | unsigned short suspend_set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | u32 scoop_gpwr; |
| 39 | }; |
| 40 | |
| 41 | void reset_scoop(struct device *dev) |
| 42 | { |
| 43 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
| 44 | |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 45 | iowrite16(0x0100, sdev->base + SCOOP_MCR); // 00 |
| 46 | iowrite16(0x0000, sdev->base + SCOOP_CDR); // 04 |
| 47 | iowrite16(0x0000, sdev->base + SCOOP_CCR); // 10 |
| 48 | iowrite16(0x0000, sdev->base + SCOOP_IMR); // 18 |
| 49 | iowrite16(0x00FF, sdev->base + SCOOP_IRM); // 14 |
| 50 | iowrite16(0x0000, sdev->base + SCOOP_ISR); // 1C |
| 51 | iowrite16(0x0000, sdev->base + SCOOP_IRM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | unsigned short set_scoop_gpio(struct device *dev, unsigned short bit) |
| 55 | { |
| 56 | unsigned short gpio_bit; |
| 57 | unsigned long flag; |
| 58 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
| 59 | |
| 60 | spin_lock_irqsave(&sdev->scoop_lock, flag); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 61 | gpio_bit = ioread16(sdev->base + SCOOP_GPWR) | bit; |
| 62 | iowrite16(gpio_bit, sdev->base + SCOOP_GPWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | spin_unlock_irqrestore(&sdev->scoop_lock, flag); |
| 64 | |
| 65 | return gpio_bit; |
| 66 | } |
| 67 | |
| 68 | unsigned short reset_scoop_gpio(struct device *dev, unsigned short bit) |
| 69 | { |
| 70 | unsigned short gpio_bit; |
| 71 | unsigned long flag; |
| 72 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
| 73 | |
| 74 | spin_lock_irqsave(&sdev->scoop_lock, flag); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 75 | gpio_bit = ioread16(sdev->base + SCOOP_GPWR) & ~bit; |
| 76 | iowrite16(gpio_bit, sdev->base + SCOOP_GPWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | spin_unlock_irqrestore(&sdev->scoop_lock, flag); |
| 78 | |
| 79 | return gpio_bit; |
| 80 | } |
| 81 | |
| 82 | EXPORT_SYMBOL(set_scoop_gpio); |
| 83 | EXPORT_SYMBOL(reset_scoop_gpio); |
| 84 | |
| 85 | unsigned short read_scoop_reg(struct device *dev, unsigned short reg) |
| 86 | { |
| 87 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 88 | return ioread16(sdev->base + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void write_scoop_reg(struct device *dev, unsigned short reg, unsigned short data) |
| 92 | { |
| 93 | struct scoop_dev *sdev = dev_get_drvdata(dev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 94 | iowrite16(data, sdev->base + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | EXPORT_SYMBOL(reset_scoop); |
| 98 | EXPORT_SYMBOL(read_scoop_reg); |
| 99 | EXPORT_SYMBOL(write_scoop_reg); |
| 100 | |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 101 | static void check_scoop_reg(struct scoop_dev *sdev) |
| 102 | { |
| 103 | unsigned short mcr; |
| 104 | |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 105 | mcr = ioread16(sdev->base + SCOOP_MCR); |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 106 | if ((mcr & 0x100) == 0) |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 107 | iowrite16(0x0101, sdev->base + SCOOP_MCR); |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #ifdef CONFIG_PM |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 111 | static int scoop_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 113 | struct scoop_dev *sdev = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 115 | check_scoop_reg(sdev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 116 | sdev->scoop_gpwr = ioread16(sdev->base + SCOOP_GPWR); |
| 117 | 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] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 122 | static int scoop_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 124 | struct scoop_dev *sdev = platform_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 126 | check_scoop_reg(sdev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 127 | iowrite16(sdev->scoop_gpwr, sdev->base + SCOOP_GPWR); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | #else |
| 132 | #define scoop_suspend NULL |
| 133 | #define scoop_resume NULL |
| 134 | #endif |
| 135 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 136 | static int __devinit scoop_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | struct scoop_dev *devptr; |
| 139 | struct scoop_config *inf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 141 | |
| 142 | if (!mem) |
| 143 | return -EINVAL; |
| 144 | |
Russell King | d2a02b9 | 2006-03-20 19:46:41 +0000 | [diff] [blame] | 145 | devptr = kzalloc(sizeof(struct scoop_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!devptr) |
Russell King | d2a02b9 | 2006-03-20 19:46:41 +0000 | [diff] [blame] | 147 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | spin_lock_init(&devptr->scoop_lock); |
| 150 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 151 | inf = pdev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | devptr->base = ioremap(mem->start, mem->end - mem->start + 1); |
| 153 | |
| 154 | if (!devptr->base) { |
| 155 | kfree(devptr); |
| 156 | return -ENOMEM; |
| 157 | } |
| 158 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 159 | platform_set_drvdata(pdev, devptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 161 | 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] | 162 | |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 163 | iowrite16(0x0140, devptr->base + SCOOP_MCR); |
Pavel Machek | c35bf4a | 2005-11-12 20:25:25 +0000 | [diff] [blame] | 164 | reset_scoop(&pdev->dev); |
Dmitry Baryshkov | c353faa | 2008-04-09 23:05:09 +0100 | [diff] [blame^] | 165 | iowrite16(0x0000, devptr->base + SCOOP_CPR); |
| 166 | iowrite16(inf->io_dir & 0xffff, devptr->base + SCOOP_GPCR); |
| 167 | iowrite16(inf->io_out & 0xffff, devptr->base + SCOOP_GPWR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Richard Purdie | 7c39898 | 2005-10-10 10:20:06 +0100 | [diff] [blame] | 169 | devptr->suspend_clr = inf->suspend_clr; |
| 170 | devptr->suspend_set = inf->suspend_set; |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 175 | static int __devexit scoop_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 177 | struct scoop_dev *sdev = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | if (sdev) { |
| 179 | iounmap(sdev->base); |
| 180 | kfree(sdev); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 181 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 186 | static struct platform_driver scoop_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | .probe = scoop_probe, |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 188 | .remove = __devexit_p(scoop_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | .suspend = scoop_suspend, |
| 190 | .resume = scoop_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 191 | .driver = { |
| 192 | .name = "sharp-scoop", |
| 193 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Dmitry Baryshkov | 2f8c514 | 2008-04-09 22:43:37 +0100 | [diff] [blame] | 196 | static int __init scoop_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 198 | return platform_driver_register(&scoop_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | subsys_initcall(scoop_init); |