Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * GPIO interface for Intel Poulsbo SCH |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2010 CompuLab Ltd |
| 5 | * Author: Denis Turischev <denis@compulab.co.il> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License 2 as published |
| 9 | * by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; see the file COPYING. If not, write to |
| 18 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/acpi.h> |
| 27 | #include <linux/platform_device.h> |
Denis Turischev | f04ddfc | 2011-03-14 12:53:05 +0200 | [diff] [blame] | 28 | #include <linux/pci_ids.h> |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 29 | |
| 30 | #include <linux/gpio.h> |
| 31 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 32 | #define GEN 0x00 |
| 33 | #define GIO 0x04 |
| 34 | #define GLV 0x08 |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 35 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 36 | struct sch_gpio { |
| 37 | struct gpio_chip chip; |
| 38 | spinlock_t lock; |
| 39 | unsigned short iobase; |
| 40 | unsigned short core_base; |
| 41 | unsigned short resume_base; |
| 42 | }; |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 43 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 44 | #define to_sch_gpio(c) container_of(c, struct sch_gpio, chip) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 45 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 46 | static unsigned sch_gpio_offset(struct sch_gpio *sch, unsigned gpio, |
| 47 | unsigned reg) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 48 | { |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 49 | unsigned base = 0; |
| 50 | |
| 51 | if (gpio >= sch->resume_base) { |
| 52 | gpio -= sch->resume_base; |
| 53 | base += 0x20; |
| 54 | } |
| 55 | |
| 56 | return base + reg + gpio / 8; |
| 57 | } |
| 58 | |
| 59 | static unsigned sch_gpio_bit(struct sch_gpio *sch, unsigned gpio) |
| 60 | { |
| 61 | if (gpio >= sch->resume_base) |
| 62 | gpio -= sch->resume_base; |
| 63 | return gpio % 8; |
| 64 | } |
| 65 | |
| 66 | static void sch_gpio_enable(struct sch_gpio *sch, unsigned gpio) |
| 67 | { |
| 68 | unsigned short offset, bit; |
| 69 | u8 enable; |
| 70 | |
| 71 | spin_lock(&sch->lock); |
| 72 | |
| 73 | offset = sch_gpio_offset(sch, gpio, GEN); |
| 74 | bit = sch_gpio_bit(sch, gpio); |
| 75 | |
| 76 | enable = inb(sch->iobase + offset); |
| 77 | if (!(enable & (1 << bit))) |
| 78 | outb(enable | (1 << bit), sch->iobase + offset); |
| 79 | |
| 80 | spin_unlock(&sch->lock); |
| 81 | } |
| 82 | |
| 83 | static int sch_gpio_direction_in(struct gpio_chip *gc, unsigned gpio_num) |
| 84 | { |
| 85 | struct sch_gpio *sch = to_sch_gpio(gc); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 86 | u8 curr_dirs; |
| 87 | unsigned short offset, bit; |
| 88 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 89 | spin_lock(&sch->lock); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 90 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 91 | offset = sch_gpio_offset(sch, gpio_num, GIO); |
| 92 | bit = sch_gpio_bit(sch, gpio_num); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 93 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 94 | curr_dirs = inb(sch->iobase + offset); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 95 | |
| 96 | if (!(curr_dirs & (1 << bit))) |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 97 | outb(curr_dirs | (1 << bit), sch->iobase + offset); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 98 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 99 | spin_unlock(&sch->lock); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 103 | static int sch_gpio_get(struct gpio_chip *gc, unsigned gpio_num) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 104 | { |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 105 | struct sch_gpio *sch = to_sch_gpio(gc); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 106 | int res; |
| 107 | unsigned short offset, bit; |
| 108 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 109 | offset = sch_gpio_offset(sch, gpio_num, GLV); |
| 110 | bit = sch_gpio_bit(sch, gpio_num); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 111 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 112 | res = !!(inb(sch->iobase + offset) & (1 << bit)); |
| 113 | |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 114 | return res; |
| 115 | } |
| 116 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 117 | static void sch_gpio_set(struct gpio_chip *gc, unsigned gpio_num, int val) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 118 | { |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 119 | struct sch_gpio *sch = to_sch_gpio(gc); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 120 | u8 curr_vals; |
| 121 | unsigned short offset, bit; |
| 122 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 123 | spin_lock(&sch->lock); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 124 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 125 | offset = sch_gpio_offset(sch, gpio_num, GLV); |
| 126 | bit = sch_gpio_bit(sch, gpio_num); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 127 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 128 | curr_vals = inb(sch->iobase + offset); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 129 | |
| 130 | if (val) |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 131 | outb(curr_vals | (1 << bit), sch->iobase + offset); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 132 | else |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 133 | outb((curr_vals & ~(1 << bit)), sch->iobase + offset); |
| 134 | |
| 135 | spin_unlock(&sch->lock); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 138 | static int sch_gpio_direction_out(struct gpio_chip *gc, unsigned gpio_num, |
| 139 | int val) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 140 | { |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 141 | struct sch_gpio *sch = to_sch_gpio(gc); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 142 | u8 curr_dirs; |
| 143 | unsigned short offset, bit; |
| 144 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 145 | spin_lock(&sch->lock); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 146 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 147 | offset = sch_gpio_offset(sch, gpio_num, GIO); |
| 148 | bit = sch_gpio_bit(sch, gpio_num); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 149 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 150 | curr_dirs = inb(sch->iobase + offset); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 151 | if (curr_dirs & (1 << bit)) |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 152 | outb(curr_dirs & ~(1 << bit), sch->iobase + offset); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 153 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 154 | spin_unlock(&sch->lock); |
Daniel Krueger | 1e0d982 | 2014-04-07 14:20:32 +0200 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * according to the datasheet, writing to the level register has no |
| 158 | * effect when GPIO is programmed as input. |
| 159 | * Actually the the level register is read-only when configured as input. |
| 160 | * Thus presetting the output level before switching to output is _NOT_ possible. |
| 161 | * Hence we set the level after configuring the GPIO as output. |
| 162 | * But we cannot prevent a short low pulse if direction is set to high |
| 163 | * and an external pull-up is connected. |
| 164 | */ |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 165 | sch_gpio_set(gc, gpio_num, val); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 169 | static struct gpio_chip sch_gpio_chip = { |
| 170 | .label = "sch_gpio", |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 171 | .owner = THIS_MODULE, |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 172 | .direction_input = sch_gpio_direction_in, |
| 173 | .get = sch_gpio_get, |
| 174 | .direction_output = sch_gpio_direction_out, |
| 175 | .set = sch_gpio_set, |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 178 | static int sch_gpio_probe(struct platform_device *pdev) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 179 | { |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 180 | struct sch_gpio *sch; |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 181 | struct resource *res; |
Denis Turischev | f04ddfc | 2011-03-14 12:53:05 +0200 | [diff] [blame] | 182 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 183 | sch = devm_kzalloc(&pdev->dev, sizeof(*sch), GFP_KERNEL); |
| 184 | if (!sch) |
| 185 | return -ENOMEM; |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 186 | |
| 187 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 188 | if (!res) |
| 189 | return -EBUSY; |
| 190 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 191 | if (!devm_request_region(&pdev->dev, res->start, resource_size(res), |
| 192 | pdev->name)) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 193 | return -EBUSY; |
| 194 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 195 | spin_lock_init(&sch->lock); |
| 196 | sch->iobase = res->start; |
| 197 | sch->chip = sch_gpio_chip; |
| 198 | sch->chip.label = dev_name(&pdev->dev); |
| 199 | sch->chip.dev = &pdev->dev; |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 200 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 201 | switch (pdev->id) { |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 202 | case PCI_DEVICE_ID_INTEL_SCH_LPC: |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 203 | sch->core_base = 0; |
| 204 | sch->resume_base = 10; |
| 205 | sch->chip.ngpio = 14; |
| 206 | |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 207 | /* |
| 208 | * GPIO[6:0] enabled by default |
| 209 | * GPIO7 is configured by the CMC as SLPIOVR |
| 210 | * Enable GPIO[9:8] core powered gpios explicitly |
| 211 | */ |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 212 | sch_gpio_enable(sch, 8); |
| 213 | sch_gpio_enable(sch, 9); |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 214 | /* |
| 215 | * SUS_GPIO[2:0] enabled by default |
| 216 | * Enable SUS_GPIO3 resume powered gpio explicitly |
| 217 | */ |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 218 | sch_gpio_enable(sch, 13); |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 219 | break; |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 220 | |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 221 | case PCI_DEVICE_ID_INTEL_ITC_LPC: |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 222 | sch->core_base = 0; |
| 223 | sch->resume_base = 5; |
| 224 | sch->chip.ngpio = 14; |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 225 | break; |
Denis Turischev | f04ddfc | 2011-03-14 12:53:05 +0200 | [diff] [blame] | 226 | |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 227 | case PCI_DEVICE_ID_INTEL_CENTERTON_ILB: |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 228 | sch->core_base = 0; |
| 229 | sch->resume_base = 21; |
| 230 | sch->chip.ngpio = 30; |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 231 | break; |
Denis Turischev | f04ddfc | 2011-03-14 12:53:05 +0200 | [diff] [blame] | 232 | |
Laurent Navet | be41cf5 | 2013-03-20 13:16:00 +0100 | [diff] [blame] | 233 | default: |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 234 | return -ENODEV; |
Denis Turischev | f04ddfc | 2011-03-14 12:53:05 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 237 | platform_set_drvdata(pdev, sch); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 238 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 239 | return gpiochip_add(&sch->chip); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 242 | static int sch_gpio_remove(struct platform_device *pdev) |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 243 | { |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 244 | struct sch_gpio *sch = platform_get_drvdata(pdev); |
Samuel Ortiz | 8e7aafe | 2010-03-05 17:14:01 +0100 | [diff] [blame] | 245 | |
Mika Westerberg | c479ff0 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 246 | gpiochip_remove(&sch->chip); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static struct platform_driver sch_gpio_driver = { |
| 251 | .driver = { |
| 252 | .name = "sch_gpio", |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 253 | }, |
| 254 | .probe = sch_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 255 | .remove = sch_gpio_remove, |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 256 | }; |
| 257 | |
Mark Brown | 6f61415 | 2011-12-08 00:24:00 +0800 | [diff] [blame] | 258 | module_platform_driver(sch_gpio_driver); |
Denis Turischev | be9b06b | 2010-03-02 10:48:55 +0100 | [diff] [blame] | 259 | |
| 260 | MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>"); |
| 261 | MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH"); |
| 262 | MODULE_LICENSE("GPL"); |
| 263 | MODULE_ALIAS("platform:sch_gpio"); |