blob: c65f35b68202716086b834cfed744f12f5418adc [file] [log] [blame]
Andy Shevchenkocb0e9a72018-11-06 14:11:42 +02001// SPDX-License-Identifier: GPL-2.0
Denis Turischevbe9b06b2010-03-02 10:48:55 +01002/*
Grant Likelyc103de22011-06-04 18:38:28 -06003 * GPIO interface for Intel Poulsbo SCH
Denis Turischevbe9b06b2010-03-02 10:48:55 +01004 *
5 * Copyright (c) 2010 CompuLab Ltd
6 * Author: Denis Turischev <denis@compulab.co.il>
Denis Turischevbe9b06b2010-03-02 10:48:55 +01007 */
8
Andy Shevchenko47091b02018-09-04 14:26:25 +03009#include <linux/acpi.h>
10#include <linux/errno.h>
11#include <linux/gpio/driver.h>
12#include <linux/io.h>
Denis Turischevbe9b06b2010-03-02 10:48:55 +010013#include <linux/kernel.h>
14#include <linux/module.h>
Denis Turischevf04ddfc2011-03-14 12:53:05 +020015#include <linux/pci_ids.h>
Andy Shevchenko47091b02018-09-04 14:26:25 +030016#include <linux/platform_device.h>
Denis Turischevbe9b06b2010-03-02 10:48:55 +010017
Mika Westerbergc479ff02014-10-21 13:33:56 +020018#define GEN 0x00
19#define GIO 0x04
20#define GLV 0x08
Denis Turischevbe9b06b2010-03-02 10:48:55 +010021
Mika Westerbergc479ff02014-10-21 13:33:56 +020022struct sch_gpio {
23 struct gpio_chip chip;
24 spinlock_t lock;
25 unsigned short iobase;
Mika Westerbergc479ff02014-10-21 13:33:56 +020026 unsigned short resume_base;
27};
Denis Turischevbe9b06b2010-03-02 10:48:55 +010028
Mika Westerbergc479ff02014-10-21 13:33:56 +020029static unsigned sch_gpio_offset(struct sch_gpio *sch, unsigned gpio,
30 unsigned reg)
Denis Turischevbe9b06b2010-03-02 10:48:55 +010031{
Mika Westerbergc479ff02014-10-21 13:33:56 +020032 unsigned base = 0;
33
34 if (gpio >= sch->resume_base) {
35 gpio -= sch->resume_base;
36 base += 0x20;
37 }
38
39 return base + reg + gpio / 8;
40}
41
42static unsigned sch_gpio_bit(struct sch_gpio *sch, unsigned gpio)
43{
44 if (gpio >= sch->resume_base)
45 gpio -= sch->resume_base;
46 return gpio % 8;
47}
48
Colin Pitrat87041a52016-06-18 19:05:04 +010049static int sch_gpio_reg_get(struct sch_gpio *sch, unsigned gpio, unsigned reg)
Mika Westerbergc479ff02014-10-21 13:33:56 +020050{
Denis Turischevbe9b06b2010-03-02 10:48:55 +010051 unsigned short offset, bit;
Chang Rebecca Swee Fun920dfd82015-01-21 18:32:21 +080052 u8 reg_val;
53
54 offset = sch_gpio_offset(sch, gpio, reg);
55 bit = sch_gpio_bit(sch, gpio);
56
57 reg_val = !!(inb(sch->iobase + offset) & BIT(bit));
58
59 return reg_val;
60}
61
Colin Pitrat87041a52016-06-18 19:05:04 +010062static void sch_gpio_reg_set(struct sch_gpio *sch, unsigned gpio, unsigned reg,
Chang Rebecca Swee Fun920dfd82015-01-21 18:32:21 +080063 int val)
64{
Chang Rebecca Swee Fun920dfd82015-01-21 18:32:21 +080065 unsigned short offset, bit;
66 u8 reg_val;
67
68 offset = sch_gpio_offset(sch, gpio, reg);
69 bit = sch_gpio_bit(sch, gpio);
70
71 reg_val = inb(sch->iobase + offset);
72
73 if (val)
74 outb(reg_val | BIT(bit), sch->iobase + offset);
75 else
76 outb((reg_val & ~BIT(bit)), sch->iobase + offset);
77}
78
79static int sch_gpio_direction_in(struct gpio_chip *gc, unsigned gpio_num)
80{
Linus Walleij737c8fc2015-12-07 14:21:49 +010081 struct sch_gpio *sch = gpiochip_get_data(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010082
Mika Westerbergc479ff02014-10-21 13:33:56 +020083 spin_lock(&sch->lock);
Colin Pitrat87041a52016-06-18 19:05:04 +010084 sch_gpio_reg_set(sch, gpio_num, GIO, 1);
Mika Westerbergc479ff02014-10-21 13:33:56 +020085 spin_unlock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010086 return 0;
87}
88
Mika Westerbergc479ff02014-10-21 13:33:56 +020089static int sch_gpio_get(struct gpio_chip *gc, unsigned gpio_num)
Denis Turischevbe9b06b2010-03-02 10:48:55 +010090{
Colin Pitrat87041a52016-06-18 19:05:04 +010091 struct sch_gpio *sch = gpiochip_get_data(gc);
92 return sch_gpio_reg_get(sch, gpio_num, GLV);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010093}
94
Mika Westerbergc479ff02014-10-21 13:33:56 +020095static void sch_gpio_set(struct gpio_chip *gc, unsigned gpio_num, int val)
Denis Turischevbe9b06b2010-03-02 10:48:55 +010096{
Linus Walleij737c8fc2015-12-07 14:21:49 +010097 struct sch_gpio *sch = gpiochip_get_data(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +010098
Mika Westerbergc479ff02014-10-21 13:33:56 +020099 spin_lock(&sch->lock);
Colin Pitrat87041a52016-06-18 19:05:04 +0100100 sch_gpio_reg_set(sch, gpio_num, GLV, val);
Mika Westerbergc479ff02014-10-21 13:33:56 +0200101 spin_unlock(&sch->lock);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100102}
103
Mika Westerbergc479ff02014-10-21 13:33:56 +0200104static int sch_gpio_direction_out(struct gpio_chip *gc, unsigned gpio_num,
105 int val)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100106{
Linus Walleij737c8fc2015-12-07 14:21:49 +0100107 struct sch_gpio *sch = gpiochip_get_data(gc);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100108
Mika Westerbergc479ff02014-10-21 13:33:56 +0200109 spin_lock(&sch->lock);
Colin Pitrat87041a52016-06-18 19:05:04 +0100110 sch_gpio_reg_set(sch, gpio_num, GIO, 0);
Mika Westerbergc479ff02014-10-21 13:33:56 +0200111 spin_unlock(&sch->lock);
Daniel Krueger1e0d9822014-04-07 14:20:32 +0200112
113 /*
114 * according to the datasheet, writing to the level register has no
115 * effect when GPIO is programmed as input.
116 * Actually the the level register is read-only when configured as input.
117 * Thus presetting the output level before switching to output is _NOT_ possible.
118 * Hence we set the level after configuring the GPIO as output.
119 * But we cannot prevent a short low pulse if direction is set to high
120 * and an external pull-up is connected.
121 */
Mika Westerbergc479ff02014-10-21 13:33:56 +0200122 sch_gpio_set(gc, gpio_num, val);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100123 return 0;
124}
125
Linus Walleijd8e764c2018-06-27 10:39:31 +0200126static int sch_gpio_get_direction(struct gpio_chip *gc, unsigned gpio_num)
127{
128 struct sch_gpio *sch = gpiochip_get_data(gc);
129
Matti Vaittinene42615e2019-11-06 10:54:12 +0200130 if (sch_gpio_reg_get(sch, gpio_num, GIO))
131 return GPIO_LINE_DIRECTION_IN;
132
133 return GPIO_LINE_DIRECTION_OUT;
Linus Walleijd8e764c2018-06-27 10:39:31 +0200134}
135
Julia Lawalle35b5ab2016-09-11 14:14:37 +0200136static const struct gpio_chip sch_gpio_chip = {
Mika Westerbergc479ff02014-10-21 13:33:56 +0200137 .label = "sch_gpio",
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100138 .owner = THIS_MODULE,
Mika Westerbergc479ff02014-10-21 13:33:56 +0200139 .direction_input = sch_gpio_direction_in,
140 .get = sch_gpio_get,
141 .direction_output = sch_gpio_direction_out,
142 .set = sch_gpio_set,
Linus Walleijd8e764c2018-06-27 10:39:31 +0200143 .get_direction = sch_gpio_get_direction,
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100144};
145
Bill Pemberton38363092012-11-19 13:22:34 -0500146static int sch_gpio_probe(struct platform_device *pdev)
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100147{
Mika Westerbergc479ff02014-10-21 13:33:56 +0200148 struct sch_gpio *sch;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100149 struct resource *res;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200150
Mika Westerbergc479ff02014-10-21 13:33:56 +0200151 sch = devm_kzalloc(&pdev->dev, sizeof(*sch), GFP_KERNEL);
152 if (!sch)
153 return -ENOMEM;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100154
155 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
156 if (!res)
157 return -EBUSY;
158
Mika Westerbergc479ff02014-10-21 13:33:56 +0200159 if (!devm_request_region(&pdev->dev, res->start, resource_size(res),
160 pdev->name))
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100161 return -EBUSY;
162
Mika Westerbergc479ff02014-10-21 13:33:56 +0200163 spin_lock_init(&sch->lock);
164 sch->iobase = res->start;
165 sch->chip = sch_gpio_chip;
166 sch->chip.label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100167 sch->chip.parent = &pdev->dev;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100168
Mika Westerbergc479ff02014-10-21 13:33:56 +0200169 switch (pdev->id) {
Laurent Navetbe41cf52013-03-20 13:16:00 +0100170 case PCI_DEVICE_ID_INTEL_SCH_LPC:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200171 sch->resume_base = 10;
172 sch->chip.ngpio = 14;
173
Laurent Navetbe41cf52013-03-20 13:16:00 +0100174 /*
175 * GPIO[6:0] enabled by default
176 * GPIO7 is configured by the CMC as SLPIOVR
177 * Enable GPIO[9:8] core powered gpios explicitly
178 */
Colin Pitrat87041a52016-06-18 19:05:04 +0100179 sch_gpio_reg_set(sch, 8, GEN, 1);
180 sch_gpio_reg_set(sch, 9, GEN, 1);
Laurent Navetbe41cf52013-03-20 13:16:00 +0100181 /*
182 * SUS_GPIO[2:0] enabled by default
183 * Enable SUS_GPIO3 resume powered gpio explicitly
184 */
Colin Pitrat87041a52016-06-18 19:05:04 +0100185 sch_gpio_reg_set(sch, 13, GEN, 1);
Laurent Navetbe41cf52013-03-20 13:16:00 +0100186 break;
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100187
Laurent Navetbe41cf52013-03-20 13:16:00 +0100188 case PCI_DEVICE_ID_INTEL_ITC_LPC:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200189 sch->resume_base = 5;
190 sch->chip.ngpio = 14;
Laurent Navetbe41cf52013-03-20 13:16:00 +0100191 break;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200192
Laurent Navetbe41cf52013-03-20 13:16:00 +0100193 case PCI_DEVICE_ID_INTEL_CENTERTON_ILB:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200194 sch->resume_base = 21;
195 sch->chip.ngpio = 30;
Laurent Navetbe41cf52013-03-20 13:16:00 +0100196 break;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200197
Chang Rebecca Swee Fun92021492014-12-08 17:38:10 +0800198 case PCI_DEVICE_ID_INTEL_QUARK_X1000_ILB:
Chang Rebecca Swee Fun92021492014-12-08 17:38:10 +0800199 sch->resume_base = 2;
200 sch->chip.ngpio = 8;
201 break;
202
Laurent Navetbe41cf52013-03-20 13:16:00 +0100203 default:
Mika Westerbergc479ff02014-10-21 13:33:56 +0200204 return -ENODEV;
Denis Turischevf04ddfc2011-03-14 12:53:05 +0200205 }
206
Mika Westerbergc479ff02014-10-21 13:33:56 +0200207 platform_set_drvdata(pdev, sch);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100208
Laxman Dewanganc1411462016-02-22 17:43:28 +0530209 return devm_gpiochip_add_data(&pdev->dev, &sch->chip, sch);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100210}
211
212static struct platform_driver sch_gpio_driver = {
213 .driver = {
214 .name = "sch_gpio",
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100215 },
216 .probe = sch_gpio_probe,
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100217};
218
Mark Brown6f614152011-12-08 00:24:00 +0800219module_platform_driver(sch_gpio_driver);
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100220
221MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
222MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
Andy Shevchenkocb0e9a72018-11-06 14:11:42 +0200223MODULE_LICENSE("GPL v2");
Denis Turischevbe9b06b2010-03-02 10:48:55 +0100224MODULE_ALIAS("platform:sch_gpio");