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