blob: 8ffd237e843371a364ade39e2dbe4b7c3a73a777 [file] [log] [blame]
Mathias Nyman1d09aaa2012-12-12 17:42:38 +02001/*
2 * GPIO controller driver for Intel Lynxpoint PCH chipset>
3 * Copyright (c) 2012, Intel Corporation.
4 *
5 * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
Andy Shevchenko92c28622018-09-04 14:26:25 +030022#include <linux/acpi.h>
23#include <linux/bitops.h>
24#include <linux/gpio/driver.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
Mathias Nyman1d09aaa2012-12-12 17:42:38 +020027#include <linux/kernel.h>
28#include <linux/module.h>
Mathias Nyman1d09aaa2012-12-12 17:42:38 +020029#include <linux/platform_device.h>
30#include <linux/pm_runtime.h>
Andy Shevchenko92c28622018-09-04 14:26:25 +030031#include <linux/slab.h>
32#include <linux/types.h>
Mathias Nyman1d09aaa2012-12-12 17:42:38 +020033
34/* LynxPoint chipset has support for 94 gpio pins */
35
36#define LP_NUM_GPIO 94
37
38/* Bitmapped register offsets */
39#define LP_ACPI_OWNED 0x00 /* Bitmap, set by bios, 0: pin reserved for ACPI */
40#define LP_GC 0x7C /* set APIC IRQ to IRQ14 or IRQ15 for all pins */
41#define LP_INT_STAT 0x80
42#define LP_INT_ENABLE 0x90
43
44/* Each pin has two 32 bit config registers, starting at 0x100 */
45#define LP_CONFIG1 0x100
46#define LP_CONFIG2 0x104
47
48/* LP_CONFIG1 reg bits */
49#define OUT_LVL_BIT BIT(31)
50#define IN_LVL_BIT BIT(30)
51#define TRIG_SEL_BIT BIT(4) /* 0: Edge, 1: Level */
52#define INT_INV_BIT BIT(3) /* Invert interrupt triggering */
53#define DIR_BIT BIT(2) /* 0: Output, 1: Input */
54#define USE_SEL_BIT BIT(0) /* 0: Native, 1: GPIO */
55
56/* LP_CONFIG2 reg bits */
57#define GPINDIS_BIT BIT(2) /* disable input sensing */
58#define GPIWP_BIT (BIT(0) | BIT(1)) /* weak pull options */
59
60struct lp_gpio {
61 struct gpio_chip chip;
Mathias Nyman1d09aaa2012-12-12 17:42:38 +020062 struct platform_device *pdev;
63 spinlock_t lock;
64 unsigned long reg_base;
65};
66
67/*
68 * Lynxpoint gpios are controlled through both bitmapped registers and
69 * per gpio specific registers. The bitmapped registers are in chunks of
70 * 3 x 32bit registers to cover all 94 gpios
71 *
72 * per gpio specific registers consist of two 32bit registers per gpio
73 * (LP_CONFIG1 and LP_CONFIG2), with 94 gpios there's a total of
Colin Cronin20a8a962015-05-18 11:41:43 -070074 * 188 config registers.
Mathias Nyman1d09aaa2012-12-12 17:42:38 +020075 *
76 * A simplified view of the register layout look like this:
77 *
78 * LP_ACPI_OWNED[31:0] gpio ownerships for gpios 0-31 (bitmapped registers)
79 * LP_ACPI_OWNED[63:32] gpio ownerships for gpios 32-63
80 * LP_ACPI_OWNED[94:64] gpio ownerships for gpios 63-94
81 * ...
82 * LP_INT_ENABLE[31:0] ...
83 * LP_INT_ENABLE[63:31] ...
84 * LP_INT_ENABLE[94:64] ...
85 * LP0_CONFIG1 (gpio 0) config1 reg for gpio 0 (per gpio registers)
86 * LP0_CONFIG2 (gpio 0) config2 reg for gpio 0
87 * LP1_CONFIG1 (gpio 1) config1 reg for gpio 1
88 * LP1_CONFIG2 (gpio 1) config2 reg for gpio 1
89 * LP2_CONFIG1 (gpio 2) ...
90 * LP2_CONFIG2 (gpio 2) ...
91 * ...
92 * LP94_CONFIG1 (gpio 94) ...
93 * LP94_CONFIG2 (gpio 94) ...
94 */
95
96static unsigned long lp_gpio_reg(struct gpio_chip *chip, unsigned offset,
97 int reg)
98{
Linus Walleijf291e002015-12-07 09:21:55 +010099 struct lp_gpio *lg = gpiochip_get_data(chip);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200100 int reg_offset;
101
102 if (reg == LP_CONFIG1 || reg == LP_CONFIG2)
103 /* per gpio specific config registers */
104 reg_offset = offset * 8;
105 else
106 /* bitmapped registers */
107 reg_offset = (offset / 32) * 4;
108
109 return lg->reg_base + reg + reg_offset;
110}
111
112static int lp_gpio_request(struct gpio_chip *chip, unsigned offset)
113{
Linus Walleijf291e002015-12-07 09:21:55 +0100114 struct lp_gpio *lg = gpiochip_get_data(chip);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200115 unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
116 unsigned long conf2 = lp_gpio_reg(chip, offset, LP_CONFIG2);
117 unsigned long acpi_use = lp_gpio_reg(chip, offset, LP_ACPI_OWNED);
118
119 pm_runtime_get(&lg->pdev->dev); /* should we put if failed */
120
121 /* Fail if BIOS reserved pin for ACPI use */
122 if (!(inl(acpi_use) & BIT(offset % 32))) {
123 dev_err(&lg->pdev->dev, "gpio %d reserved for ACPI\n", offset);
124 return -EBUSY;
125 }
126 /* Fail if pin is in alternate function mode (not GPIO mode) */
127 if (!(inl(reg) & USE_SEL_BIT))
128 return -ENODEV;
129
130 /* enable input sensing */
131 outl(inl(conf2) & ~GPINDIS_BIT, conf2);
132
133
134 return 0;
135}
136
137static void lp_gpio_free(struct gpio_chip *chip, unsigned offset)
138{
Linus Walleijf291e002015-12-07 09:21:55 +0100139 struct lp_gpio *lg = gpiochip_get_data(chip);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200140 unsigned long conf2 = lp_gpio_reg(chip, offset, LP_CONFIG2);
141
142 /* disable input sensing */
143 outl(inl(conf2) | GPINDIS_BIT, conf2);
144
145 pm_runtime_put(&lg->pdev->dev);
146}
147
148static int lp_irq_type(struct irq_data *d, unsigned type)
149{
Mika Westerberg7f872102014-07-25 09:54:46 +0300150 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijf291e002015-12-07 09:21:55 +0100151 struct lp_gpio *lg = gpiochip_get_data(gc);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200152 u32 hwirq = irqd_to_hwirq(d);
153 unsigned long flags;
154 u32 value;
155 unsigned long reg = lp_gpio_reg(&lg->chip, hwirq, LP_CONFIG1);
156
157 if (hwirq >= lg->chip.ngpio)
158 return -EINVAL;
159
160 spin_lock_irqsave(&lg->lock, flags);
161 value = inl(reg);
162
163 /* set both TRIG_SEL and INV bits to 0 for rising edge */
164 if (type & IRQ_TYPE_EDGE_RISING)
165 value &= ~(TRIG_SEL_BIT | INT_INV_BIT);
166
167 /* TRIG_SEL bit 0, INV bit 1 for falling edge */
168 if (type & IRQ_TYPE_EDGE_FALLING)
169 value = (value | INT_INV_BIT) & ~TRIG_SEL_BIT;
170
171 /* TRIG_SEL bit 1, INV bit 0 for level low */
172 if (type & IRQ_TYPE_LEVEL_LOW)
173 value = (value | TRIG_SEL_BIT) & ~INT_INV_BIT;
174
175 /* TRIG_SEL bit 1, INV bit 1 for level high */
176 if (type & IRQ_TYPE_LEVEL_HIGH)
177 value |= TRIG_SEL_BIT | INT_INV_BIT;
178
179 outl(value, reg);
180 spin_unlock_irqrestore(&lg->lock, flags);
181
182 return 0;
183}
184
185static int lp_gpio_get(struct gpio_chip *chip, unsigned offset)
186{
187 unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
Mathias Nyman8650ea12014-03-27 15:02:08 +0200188 return !!(inl(reg) & IN_LVL_BIT);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200189}
190
191static void lp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
192{
Linus Walleijf291e002015-12-07 09:21:55 +0100193 struct lp_gpio *lg = gpiochip_get_data(chip);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200194 unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
195 unsigned long flags;
196
197 spin_lock_irqsave(&lg->lock, flags);
198
199 if (value)
200 outl(inl(reg) | OUT_LVL_BIT, reg);
201 else
202 outl(inl(reg) & ~OUT_LVL_BIT, reg);
203
204 spin_unlock_irqrestore(&lg->lock, flags);
205}
206
207static int lp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
208{
Linus Walleijf291e002015-12-07 09:21:55 +0100209 struct lp_gpio *lg = gpiochip_get_data(chip);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200210 unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
211 unsigned long flags;
212
213 spin_lock_irqsave(&lg->lock, flags);
214 outl(inl(reg) | DIR_BIT, reg);
215 spin_unlock_irqrestore(&lg->lock, flags);
216
217 return 0;
218}
219
220static int lp_gpio_direction_output(struct gpio_chip *chip,
221 unsigned offset, int value)
222{
Linus Walleijf291e002015-12-07 09:21:55 +0100223 struct lp_gpio *lg = gpiochip_get_data(chip);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200224 unsigned long reg = lp_gpio_reg(chip, offset, LP_CONFIG1);
225 unsigned long flags;
226
227 lp_gpio_set(chip, offset, value);
228
229 spin_lock_irqsave(&lg->lock, flags);
230 outl(inl(reg) & ~DIR_BIT, reg);
231 spin_unlock_irqrestore(&lg->lock, flags);
232
233 return 0;
234}
235
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200236static void lp_gpio_irq_handler(struct irq_desc *desc)
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200237{
238 struct irq_data *data = irq_desc_get_irq_data(desc);
Mika Westerberg7f872102014-07-25 09:54:46 +0300239 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Linus Walleijf291e002015-12-07 09:21:55 +0100240 struct lp_gpio *lg = gpiochip_get_data(gc);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200241 struct irq_chip *chip = irq_data_get_irq_chip(data);
Mika Westerberg03d152d2013-10-01 17:35:43 +0300242 unsigned long reg, ena, pending;
Andy Shevchenko502ae422018-11-06 14:38:55 +0200243 u32 base, pin;
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200244
245 /* check from GPIO controller which pin triggered the interrupt */
246 for (base = 0; base < lg->chip.ngpio; base += 32) {
247 reg = lp_gpio_reg(&lg->chip, base, LP_INT_STAT);
Mika Westerberg03d152d2013-10-01 17:35:43 +0300248 ena = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200249
Andy Shevchenko502ae422018-11-06 14:38:55 +0200250 /* Only interrupts that are enabled */
251 pending = inl(reg) & inl(ena);
252
253 for_each_set_bit(pin, &pending, 32) {
Linus Walleijb551b022013-10-11 19:32:16 +0200254 unsigned irq;
255
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200256 /* Clear before handling so we don't lose an edge */
Andy Shevchenko502ae422018-11-06 14:38:55 +0200257 outl(BIT(pin), reg);
258
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100259 irq = irq_find_mapping(lg->chip.irq.domain, base + pin);
Linus Walleijb551b022013-10-11 19:32:16 +0200260 generic_handle_irq(irq);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200261 }
262 }
263 chip->irq_eoi(data);
264}
265
266static void lp_irq_unmask(struct irq_data *d)
267{
268}
269
270static void lp_irq_mask(struct irq_data *d)
271{
272}
273
274static void lp_irq_enable(struct irq_data *d)
275{
Mika Westerberg7f872102014-07-25 09:54:46 +0300276 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijf291e002015-12-07 09:21:55 +0100277 struct lp_gpio *lg = gpiochip_get_data(gc);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200278 u32 hwirq = irqd_to_hwirq(d);
279 unsigned long reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_ENABLE);
280 unsigned long flags;
281
282 spin_lock_irqsave(&lg->lock, flags);
283 outl(inl(reg) | BIT(hwirq % 32), reg);
284 spin_unlock_irqrestore(&lg->lock, flags);
285}
286
287static void lp_irq_disable(struct irq_data *d)
288{
Mika Westerberg7f872102014-07-25 09:54:46 +0300289 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijf291e002015-12-07 09:21:55 +0100290 struct lp_gpio *lg = gpiochip_get_data(gc);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200291 u32 hwirq = irqd_to_hwirq(d);
292 unsigned long reg = lp_gpio_reg(&lg->chip, hwirq, LP_INT_ENABLE);
293 unsigned long flags;
294
295 spin_lock_irqsave(&lg->lock, flags);
296 outl(inl(reg) & ~BIT(hwirq % 32), reg);
297 spin_unlock_irqrestore(&lg->lock, flags);
298}
299
300static struct irq_chip lp_irqchip = {
301 .name = "LP-GPIO",
302 .irq_mask = lp_irq_mask,
303 .irq_unmask = lp_irq_unmask,
304 .irq_enable = lp_irq_enable,
305 .irq_disable = lp_irq_disable,
306 .irq_set_type = lp_irq_type,
307 .flags = IRQCHIP_SKIP_SET_WAKE,
308};
309
310static void lp_gpio_irq_init_hw(struct lp_gpio *lg)
311{
312 unsigned long reg;
313 unsigned base;
314
315 for (base = 0; base < lg->chip.ngpio; base += 32) {
316 /* disable gpio pin interrupts */
317 reg = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
318 outl(0, reg);
319 /* Clear interrupt status register */
320 reg = lp_gpio_reg(&lg->chip, base, LP_INT_STAT);
321 outl(0xffffffff, reg);
322 }
323}
324
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200325static int lp_gpio_probe(struct platform_device *pdev)
326{
327 struct lp_gpio *lg;
328 struct gpio_chip *gc;
329 struct resource *io_rc, *irq_rc;
330 struct device *dev = &pdev->dev;
331 unsigned long reg_len;
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200332 int ret = -ENODEV;
333
334 lg = devm_kzalloc(dev, sizeof(struct lp_gpio), GFP_KERNEL);
Jingoo Han1981d082014-04-29 17:36:26 +0900335 if (!lg)
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200336 return -ENOMEM;
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200337
338 lg->pdev = pdev;
339 platform_set_drvdata(pdev, lg);
340
341 io_rc = platform_get_resource(pdev, IORESOURCE_IO, 0);
342 irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
343
344 if (!io_rc) {
345 dev_err(dev, "missing IO resources\n");
346 return -EINVAL;
347 }
348
349 lg->reg_base = io_rc->start;
350 reg_len = resource_size(io_rc);
351
352 if (!devm_request_region(dev, lg->reg_base, reg_len, "lp-gpio")) {
353 dev_err(dev, "failed requesting IO region 0x%x\n",
354 (unsigned int)lg->reg_base);
355 return -EBUSY;
356 }
357
358 spin_lock_init(&lg->lock);
359
360 gc = &lg->chip;
361 gc->label = dev_name(dev);
362 gc->owner = THIS_MODULE;
363 gc->request = lp_gpio_request;
364 gc->free = lp_gpio_free;
365 gc->direction_input = lp_gpio_direction_input;
366 gc->direction_output = lp_gpio_direction_output;
367 gc->get = lp_gpio_get;
368 gc->set = lp_gpio_set;
369 gc->base = -1;
370 gc->ngpio = LP_NUM_GPIO;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100371 gc->can_sleep = false;
Linus Walleij58383c782015-11-04 09:56:26 +0100372 gc->parent = dev;
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200373
Laxman Dewanganefa3ffc2016-02-22 17:43:28 +0530374 ret = devm_gpiochip_add_data(dev, gc, lg);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200375 if (ret) {
376 dev_err(dev, "failed adding lp-gpio chip\n");
377 return ret;
378 }
Mika Westerberg7f872102014-07-25 09:54:46 +0300379
380 /* set up interrupts */
381 if (irq_rc && irq_rc->start) {
382 lp_gpio_irq_init_hw(lg);
383 ret = gpiochip_irqchip_add(gc, &lp_irqchip, 0,
384 handle_simple_irq, IRQ_TYPE_NONE);
385 if (ret) {
386 dev_err(dev, "failed to add irqchip\n");
Mika Westerberg7f872102014-07-25 09:54:46 +0300387 return ret;
388 }
389
390 gpiochip_set_chained_irqchip(gc, &lp_irqchip,
391 (unsigned)irq_rc->start,
392 lp_gpio_irq_handler);
393 }
394
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200395 pm_runtime_enable(dev);
396
397 return 0;
398}
399
400static int lp_gpio_runtime_suspend(struct device *dev)
401{
402 return 0;
403}
404
405static int lp_gpio_runtime_resume(struct device *dev)
406{
407 return 0;
408}
409
Mathias Nyman8117bd52014-08-19 14:00:01 +0300410static int lp_gpio_resume(struct device *dev)
411{
412 struct platform_device *pdev = to_platform_device(dev);
413 struct lp_gpio *lg = platform_get_drvdata(pdev);
414 unsigned long reg;
415 int i;
416
417 /* on some hardware suspend clears input sensing, re-enable it here */
418 for (i = 0; i < lg->chip.ngpio; i++) {
419 if (gpiochip_is_requested(&lg->chip, i) != NULL) {
420 reg = lp_gpio_reg(&lg->chip, i, LP_CONFIG2);
421 outl(inl(reg) & ~GPINDIS_BIT, reg);
422 }
423 }
424 return 0;
425}
426
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200427static const struct dev_pm_ops lp_gpio_pm_ops = {
428 .runtime_suspend = lp_gpio_runtime_suspend,
429 .runtime_resume = lp_gpio_runtime_resume,
Mathias Nyman8117bd52014-08-19 14:00:01 +0300430 .resume = lp_gpio_resume,
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200431};
432
433static const struct acpi_device_id lynxpoint_gpio_acpi_match[] = {
434 { "INT33C7", 0 },
Mika Westerberg4edd7902013-11-12 11:52:32 +0200435 { "INT3437", 0 },
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200436 { }
437};
438MODULE_DEVICE_TABLE(acpi, lynxpoint_gpio_acpi_match);
439
440static int lp_gpio_remove(struct platform_device *pdev)
441{
Mathias Nymanb1683862013-08-12 14:05:41 +0300442 pm_runtime_disable(&pdev->dev);
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200443 return 0;
444}
445
446static struct platform_driver lp_gpio_driver = {
447 .probe = lp_gpio_probe,
448 .remove = lp_gpio_remove,
449 .driver = {
450 .name = "lp_gpio",
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200451 .pm = &lp_gpio_pm_ops,
452 .acpi_match_table = ACPI_PTR(lynxpoint_gpio_acpi_match),
453 },
454};
455
456static int __init lp_gpio_init(void)
457{
458 return platform_driver_register(&lp_gpio_driver);
459}
460
Jean Delvared463c6f2013-11-27 15:46:06 +0100461static void __exit lp_gpio_exit(void)
462{
463 platform_driver_unregister(&lp_gpio_driver);
464}
465
Mathias Nyman1d09aaa2012-12-12 17:42:38 +0200466subsys_initcall(lp_gpio_init);
Jean Delvared463c6f2013-11-27 15:46:06 +0100467module_exit(lp_gpio_exit);
468
469MODULE_AUTHOR("Mathias Nyman (Intel)");
470MODULE_DESCRIPTION("GPIO interface for Intel Lynxpoint");
471MODULE_LICENSE("GPL");
472MODULE_ALIAS("platform:lp_gpio");