1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/acpi.h>
31#include <linux/bitmap.h>
32#include <linux/gpio/driver.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/platform_device.h>
38#include <linux/slab.h>
39#include <linux/suspend.h>
40
41#include <asm/cpu_device_id.h>
42#include <asm/intel-family.h>
43
44#define DRV_NAME "INT0002 Virtual GPIO"
45
46
47#define GPE0A_PME_B0_VIRT_GPIO_PIN 2
48
49#define GPE0A_PME_B0_STS_BIT BIT(13)
50#define GPE0A_PME_B0_EN_BIT BIT(13)
51#define GPE0A_STS_PORT 0x420
52#define GPE0A_EN_PORT 0x428
53
54struct int0002_data {
55 struct gpio_chip chip;
56 int parent_irq;
57 int wake_enable_count;
58};
59
60
61
62
63
64
65static int int0002_gpio_get(struct gpio_chip *chip, unsigned int offset)
66{
67 return 0;
68}
69
70static void int0002_gpio_set(struct gpio_chip *chip, unsigned int offset,
71 int value)
72{
73}
74
75static int int0002_gpio_direction_output(struct gpio_chip *chip,
76 unsigned int offset, int value)
77{
78 return 0;
79}
80
81static void int0002_irq_ack(struct irq_data *data)
82{
83 outl(GPE0A_PME_B0_STS_BIT, GPE0A_STS_PORT);
84}
85
86static void int0002_irq_unmask(struct irq_data *data)
87{
88 u32 gpe_en_reg;
89
90 gpe_en_reg = inl(GPE0A_EN_PORT);
91 gpe_en_reg |= GPE0A_PME_B0_EN_BIT;
92 outl(gpe_en_reg, GPE0A_EN_PORT);
93}
94
95static void int0002_irq_mask(struct irq_data *data)
96{
97 u32 gpe_en_reg;
98
99 gpe_en_reg = inl(GPE0A_EN_PORT);
100 gpe_en_reg &= ~GPE0A_PME_B0_EN_BIT;
101 outl(gpe_en_reg, GPE0A_EN_PORT);
102}
103
104static int int0002_irq_set_wake(struct irq_data *data, unsigned int on)
105{
106 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
107 struct int0002_data *int0002 = container_of(chip, struct int0002_data, chip);
108
109
110
111
112
113 if (on)
114 int0002->wake_enable_count++;
115 else
116 int0002->wake_enable_count--;
117
118 return 0;
119}
120
121static irqreturn_t int0002_irq(int irq, void *data)
122{
123 struct gpio_chip *chip = data;
124 u32 gpe_sts_reg;
125
126 gpe_sts_reg = inl(GPE0A_STS_PORT);
127 if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
128 return IRQ_NONE;
129
130 generic_handle_irq(irq_find_mapping(chip->irq.domain,
131 GPE0A_PME_B0_VIRT_GPIO_PIN));
132
133 pm_wakeup_hard_event(chip->parent);
134
135 return IRQ_HANDLED;
136}
137
138static bool int0002_check_wake(void *data)
139{
140 u32 gpe_sts_reg;
141
142 gpe_sts_reg = inl(GPE0A_STS_PORT);
143 return (gpe_sts_reg & GPE0A_PME_B0_STS_BIT);
144}
145
146static struct irq_chip int0002_irqchip = {
147 .name = DRV_NAME,
148 .irq_ack = int0002_irq_ack,
149 .irq_mask = int0002_irq_mask,
150 .irq_unmask = int0002_irq_unmask,
151 .irq_set_wake = int0002_irq_set_wake,
152};
153
154static const struct x86_cpu_id int0002_cpu_ids[] = {
155 X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, NULL),
156 X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, NULL),
157 {}
158};
159
160static void int0002_init_irq_valid_mask(struct gpio_chip *chip,
161 unsigned long *valid_mask,
162 unsigned int ngpios)
163{
164 bitmap_clear(valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN);
165}
166
167static int int0002_probe(struct platform_device *pdev)
168{
169 struct device *dev = &pdev->dev;
170 const struct x86_cpu_id *cpu_id;
171 struct int0002_data *int0002;
172 struct gpio_irq_chip *girq;
173 struct gpio_chip *chip;
174 int irq, ret;
175
176
177 cpu_id = x86_match_cpu(int0002_cpu_ids);
178 if (!cpu_id)
179 return -ENODEV;
180
181 irq = platform_get_irq(pdev, 0);
182 if (irq < 0)
183 return irq;
184
185 int0002 = devm_kzalloc(dev, sizeof(*int0002), GFP_KERNEL);
186 if (!int0002)
187 return -ENOMEM;
188
189 int0002->parent_irq = irq;
190
191 chip = &int0002->chip;
192 chip->label = DRV_NAME;
193 chip->parent = dev;
194 chip->owner = THIS_MODULE;
195 chip->get = int0002_gpio_get;
196 chip->set = int0002_gpio_set;
197 chip->direction_input = int0002_gpio_get;
198 chip->direction_output = int0002_gpio_direction_output;
199 chip->base = -1;
200 chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
201 chip->irq.init_valid_mask = int0002_init_irq_valid_mask;
202
203
204
205
206
207
208
209 ret = devm_request_irq(dev, irq, int0002_irq,
210 IRQF_SHARED, "INT0002", chip);
211 if (ret) {
212 dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret);
213 return ret;
214 }
215
216 girq = &chip->irq;
217 girq->chip = &int0002_irqchip;
218
219 girq->parent_handler = NULL;
220 girq->num_parents = 0;
221 girq->parents = NULL;
222 girq->default_type = IRQ_TYPE_NONE;
223 girq->handler = handle_edge_irq;
224
225 ret = devm_gpiochip_add_data(dev, chip, NULL);
226 if (ret) {
227 dev_err(dev, "Error adding gpio chip: %d\n", ret);
228 return ret;
229 }
230
231 acpi_register_wakeup_handler(irq, int0002_check_wake, NULL);
232 device_init_wakeup(dev, true);
233 dev_set_drvdata(dev, int0002);
234 return 0;
235}
236
237static int int0002_remove(struct platform_device *pdev)
238{
239 device_init_wakeup(&pdev->dev, false);
240 acpi_unregister_wakeup_handler(int0002_check_wake, NULL);
241 return 0;
242}
243
244static int int0002_suspend(struct device *dev)
245{
246 struct int0002_data *int0002 = dev_get_drvdata(dev);
247
248
249
250
251
252
253 if (!pm_suspend_via_firmware() && int0002->wake_enable_count)
254 enable_irq_wake(int0002->parent_irq);
255
256 return 0;
257}
258
259static int int0002_resume(struct device *dev)
260{
261 struct int0002_data *int0002 = dev_get_drvdata(dev);
262
263 if (!pm_suspend_via_firmware() && int0002->wake_enable_count)
264 disable_irq_wake(int0002->parent_irq);
265
266 return 0;
267}
268
269static const struct dev_pm_ops int0002_pm_ops = {
270 .suspend = int0002_suspend,
271 .resume = int0002_resume,
272};
273
274static const struct acpi_device_id int0002_acpi_ids[] = {
275 { "INT0002", 0 },
276 { },
277};
278MODULE_DEVICE_TABLE(acpi, int0002_acpi_ids);
279
280static struct platform_driver int0002_driver = {
281 .driver = {
282 .name = DRV_NAME,
283 .acpi_match_table = int0002_acpi_ids,
284 .pm = &int0002_pm_ops,
285 },
286 .probe = int0002_probe,
287 .remove = int0002_remove,
288};
289
290module_platform_driver(int0002_driver);
291
292MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
293MODULE_DESCRIPTION("Intel INT0002 Virtual GPIO driver");
294MODULE_LICENSE("GPL v2");
295