blob: 0a0cf1307d2fa8d5cb3caae0399ef971737503d3 [file] [log] [blame]
Denis Turischevbe9b06b2010-03-02 10:48:55 +01001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * GPIO interface for Intel Poulsbo SCH
Denis Turischevbe9b06b2010-03-02 10:48:55 +01003 *
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 Turischevf04ddfc2011-03-14 12:53:05 +020028#include <linux/pci_ids.h>
Denis Turischevbe9b06b2010-03-02 10:48:55 +010029
30#include <linux/gpio.h>
31
Mika Westerbergc479ff02014-10-21 13:33:56 +020032#define GEN 0x00
33#define GIO 0x04
34#define GLV 0x08
Denis Turischevbe9b06b2010-03-02 10:48:55 +010035
Mika Westerbergc479ff02014-10-21 13:33:56 +020036struct 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 Turischevbe9b06b2010-03-02 10:48:55 +010043
Mika Westerbergc479ff02014-10-21 13:33:56 +020044#define to_sch_gpio(c) container_of(c, struct sch_gpio, chip)
Denis Turischevbe9b06b2010-03-02 10:48:55 +010045
Mika Westerbergc479ff02014-10-21 13:33:56 +020046static unsigned sch_gpio_offset(struct sch_gpio *sch, unsigned gpio,
47 unsigned reg)
Denis Turischevbe9b06b2010-03-02 10:48:55 +010048{
Mika Westerbergc479ff02014-10-21 13:33:56 +020049 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
59static 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
66static 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
83static int sch_gpio_direction_in(struct gpio_chip *gc, unsigned gpio_num)
84{
85 struct sch_gpio *sch = to_sch_gpio(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010086 u8 curr_dirs;
87 unsigned short offset, bit;
88
Mika Westerbergc479ff02014-10-21 13:33:56 +020089 spin_lock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010090
Mika Westerbergc479ff02014-10-21 13:33:56 +020091 offset = sch_gpio_offset(sch, gpio_num, GIO);
92 bit = sch_gpio_bit(sch, gpio_num);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010093
Mika Westerbergc479ff02014-10-21 13:33:56 +020094 curr_dirs = inb(sch->iobase + offset);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010095
96 if (!(curr_dirs & (1 << bit)))
Mika Westerbergc479ff02014-10-21 13:33:56 +020097 outb(curr_dirs | (1 << bit), sch->iobase + offset);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010098
Mika Westerbergc479ff02014-10-21 13:33:56 +020099 spin_unlock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100100 return 0;
101}
102
Mika Westerbergc479ff02014-10-21 13:33:56 +0200103static int sch_gpio_get(struct gpio_chip *gc, unsigned gpio_num)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100104{
Mika Westerbergc479ff02014-10-21 13:33:56 +0200105 struct sch_gpio *sch = to_sch_gpio(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100106 int res;
107 unsigned short offset, bit;
108
Mika Westerbergc479ff02014-10-21 13:33:56 +0200109 offset = sch_gpio_offset(sch, gpio_num, GLV);
110 bit = sch_gpio_bit(sch, gpio_num);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100111
Mika Westerbergc479ff02014-10-21 13:33:56 +0200112 res = !!(inb(sch->iobase + offset) & (1 << bit));
113
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100114 return res;
115}
116
Mika Westerbergc479ff02014-10-21 13:33:56 +0200117static void sch_gpio_set(struct gpio_chip *gc, unsigned gpio_num, int val)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100118{
Mika Westerbergc479ff02014-10-21 13:33:56 +0200119 struct sch_gpio *sch = to_sch_gpio(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100120 u8 curr_vals;
121 unsigned short offset, bit;
122
Mika Westerbergc479ff02014-10-21 13:33:56 +0200123 spin_lock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100124
Mika Westerbergc479ff02014-10-21 13:33:56 +0200125 offset = sch_gpio_offset(sch, gpio_num, GLV);
126 bit = sch_gpio_bit(sch, gpio_num);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100127
Mika Westerbergc479ff02014-10-21 13:33:56 +0200128 curr_vals = inb(sch->iobase + offset);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100129
130 if (val)
Mika Westerbergc479ff02014-10-21 13:33:56 +0200131 outb(curr_vals | (1 << bit), sch->iobase + offset);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100132 else
Mika Westerbergc479ff02014-10-21 13:33:56 +0200133 outb((curr_vals & ~(1 << bit)), sch->iobase + offset);
134
135 spin_unlock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100136}
137
Mika Westerbergc479ff02014-10-21 13:33:56 +0200138static int sch_gpio_direction_out(struct gpio_chip *gc, unsigned gpio_num,
139 int val)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100140{
Mika Westerbergc479ff02014-10-21 13:33:56 +0200141 struct sch_gpio *sch = to_sch_gpio(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100142 u8 curr_dirs;
143 unsigned short offset, bit;
144
Mika Westerbergc479ff02014-10-21 13:33:56 +0200145 spin_lock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100146
Mika Westerbergc479ff02014-10-21 13:33:56 +0200147 offset = sch_gpio_offset(sch, gpio_num, GIO);
148 bit = sch_gpio_bit(sch, gpio_num);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100149
Mika Westerbergc479ff02014-10-21 13:33:56 +0200150 curr_dirs = inb(sch->iobase + offset);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100151 if (curr_dirs & (1 << bit))
Mika Westerbergc479ff02014-10-21 13:33:56 +0200152 outb(curr_dirs & ~(1 << bit), sch->iobase + offset);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100153
Mika Westerbergc479ff02014-10-21 13:33:56 +0200154 spin_unlock(&sch->lock);
Daniel Krueger1e0d9822014-04-07 14:20:32 +0200155
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 Westerbergc479ff02014-10-21 13:33:56 +0200165 sch_gpio_set(gc, gpio_num, val);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100166 return 0;
167}
168
Mika Westerbergc479ff02014-10-21 13:33:56 +0200169static struct gpio_chip sch_gpio_chip = {
170 .label = "sch_gpio",
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100171 .owner = THIS_MODULE,
Mika Westerbergc479ff02014-10-21 13:33:56 +0200172 .direction_input = sch_gpio_direction_in,
173 .get = sch_gpio_get,
174 .direction_output = sch_gpio_direction_out,
175 .set = sch_gpio_set,
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100176};
177
Bill Pemberton38363092012-11-19 13:22:34 -0500178static int sch_gpio_probe(struct platform_device *pdev)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100179{
Mika Westerbergc479ff02014-10-21 13:33:56 +0200180 struct sch_gpio *sch;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100181 struct resource *res;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200182
Mika Westerbergc479ff02014-10-21 13:33:56 +0200183 sch = devm_kzalloc(&pdev->dev, sizeof(*sch), GFP_KERNEL);
184 if (!sch)
185 return -ENOMEM;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100186
187 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
188 if (!res)
189 return -EBUSY;
190
Mika Westerbergc479ff02014-10-21 13:33:56 +0200191 if (!devm_request_region(&pdev->dev, res->start, resource_size(res),
192 pdev->name))
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100193 return -EBUSY;
194
Mika Westerbergc479ff02014-10-21 13:33:56 +0200195 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 Turischevbe9b06b2010-03-02 10:48:55 +0100200
Mika Westerbergc479ff02014-10-21 13:33:56 +0200201 switch (pdev->id) {
Laurent Navetbe41cf52013-03-20 13:16:00 +0100202 case PCI_DEVICE_ID_INTEL_SCH_LPC:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200203 sch->core_base = 0;
204 sch->resume_base = 10;
205 sch->chip.ngpio = 14;
206
Laurent Navetbe41cf52013-03-20 13:16:00 +0100207 /*
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 Westerbergc479ff02014-10-21 13:33:56 +0200212 sch_gpio_enable(sch, 8);
213 sch_gpio_enable(sch, 9);
Laurent Navetbe41cf52013-03-20 13:16:00 +0100214 /*
215 * SUS_GPIO[2:0] enabled by default
216 * Enable SUS_GPIO3 resume powered gpio explicitly
217 */
Mika Westerbergc479ff02014-10-21 13:33:56 +0200218 sch_gpio_enable(sch, 13);
Laurent Navetbe41cf52013-03-20 13:16:00 +0100219 break;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100220
Laurent Navetbe41cf52013-03-20 13:16:00 +0100221 case PCI_DEVICE_ID_INTEL_ITC_LPC:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200222 sch->core_base = 0;
223 sch->resume_base = 5;
224 sch->chip.ngpio = 14;
Laurent Navetbe41cf52013-03-20 13:16:00 +0100225 break;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200226
Laurent Navetbe41cf52013-03-20 13:16:00 +0100227 case PCI_DEVICE_ID_INTEL_CENTERTON_ILB:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200228 sch->core_base = 0;
229 sch->resume_base = 21;
230 sch->chip.ngpio = 30;
Laurent Navetbe41cf52013-03-20 13:16:00 +0100231 break;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200232
Laurent Navetbe41cf52013-03-20 13:16:00 +0100233 default:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200234 return -ENODEV;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200235 }
236
Mika Westerbergc479ff02014-10-21 13:33:56 +0200237 platform_set_drvdata(pdev, sch);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100238
Mika Westerbergc479ff02014-10-21 13:33:56 +0200239 return gpiochip_add(&sch->chip);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100240}
241
Bill Pemberton206210c2012-11-19 13:25:50 -0500242static int sch_gpio_remove(struct platform_device *pdev)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100243{
Mika Westerbergc479ff02014-10-21 13:33:56 +0200244 struct sch_gpio *sch = platform_get_drvdata(pdev);
Samuel Ortiz8e7aafe2010-03-05 17:14:01 +0100245
Mika Westerbergc479ff02014-10-21 13:33:56 +0200246 gpiochip_remove(&sch->chip);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100247 return 0;
248}
249
250static struct platform_driver sch_gpio_driver = {
251 .driver = {
252 .name = "sch_gpio",
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100253 },
254 .probe = sch_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500255 .remove = sch_gpio_remove,
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100256};
257
Mark Brown6f614152011-12-08 00:24:00 +0800258module_platform_driver(sch_gpio_driver);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100259
260MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
261MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
262MODULE_LICENSE("GPL");
263MODULE_ALIAS("platform:sch_gpio");