1 // SPDX-License-Identifier: GPL-2.0
3 * GPIO interface for Intel Poulsbo SCH
5 * Copyright (c) 2010 CompuLab Ltd
6 * Author: Denis Turischev <denis@compulab.co.il>
9 #include <linux/acpi.h>
10 #include <linux/bitops.h>
11 #include <linux/errno.h>
12 #include <linux/gpio/driver.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pci_ids.h>
18 #include <linux/platform_device.h>
19 #include <linux/types.h>
30 #define CORE_BANK_OFFSET 0x00
31 #define RESUME_BANK_OFFSET 0x20
34 * iLB datasheet describes GPE0BLK registers, in particular GPE0E.GPIO bit.
35 * Document Number: 328195-001
40 struct gpio_chip chip
;
43 unsigned short resume_base
;
47 acpi_gpe_handler gpe_handler
;
50 static unsigned int sch_gpio_offset(struct sch_gpio
*sch
, unsigned int gpio
,
53 unsigned int base
= CORE_BANK_OFFSET
;
55 if (gpio
>= sch
->resume_base
) {
56 gpio
-= sch
->resume_base
;
57 base
= RESUME_BANK_OFFSET
;
60 return base
+ reg
+ gpio
/ 8;
63 static unsigned int sch_gpio_bit(struct sch_gpio
*sch
, unsigned int gpio
)
65 if (gpio
>= sch
->resume_base
)
66 gpio
-= sch
->resume_base
;
70 static int sch_gpio_reg_get(struct sch_gpio
*sch
, unsigned int gpio
, unsigned int reg
)
72 unsigned short offset
, bit
;
75 offset
= sch_gpio_offset(sch
, gpio
, reg
);
76 bit
= sch_gpio_bit(sch
, gpio
);
78 reg_val
= !!(ioread8(sch
->regs
+ offset
) & BIT(bit
));
83 static void sch_gpio_reg_set(struct sch_gpio
*sch
, unsigned int gpio
, unsigned int reg
,
86 unsigned short offset
, bit
;
89 offset
= sch_gpio_offset(sch
, gpio
, reg
);
90 bit
= sch_gpio_bit(sch
, gpio
);
92 reg_val
= ioread8(sch
->regs
+ offset
);
99 iowrite8(reg_val
, sch
->regs
+ offset
);
102 static int sch_gpio_direction_in(struct gpio_chip
*gc
, unsigned int gpio_num
)
104 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
107 spin_lock_irqsave(&sch
->lock
, flags
);
108 sch_gpio_reg_set(sch
, gpio_num
, GIO
, 1);
109 spin_unlock_irqrestore(&sch
->lock
, flags
);
113 static int sch_gpio_get(struct gpio_chip
*gc
, unsigned int gpio_num
)
115 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
117 return sch_gpio_reg_get(sch
, gpio_num
, GLV
);
120 static void sch_gpio_set(struct gpio_chip
*gc
, unsigned int gpio_num
, int val
)
122 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
125 spin_lock_irqsave(&sch
->lock
, flags
);
126 sch_gpio_reg_set(sch
, gpio_num
, GLV
, val
);
127 spin_unlock_irqrestore(&sch
->lock
, flags
);
130 static int sch_gpio_direction_out(struct gpio_chip
*gc
, unsigned int gpio_num
,
133 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
136 spin_lock_irqsave(&sch
->lock
, flags
);
137 sch_gpio_reg_set(sch
, gpio_num
, GIO
, 0);
138 spin_unlock_irqrestore(&sch
->lock
, flags
);
141 * according to the datasheet, writing to the level register has no
142 * effect when GPIO is programmed as input.
143 * Actually the level register is read-only when configured as input.
144 * Thus presetting the output level before switching to output is _NOT_ possible.
145 * Hence we set the level after configuring the GPIO as output.
146 * But we cannot prevent a short low pulse if direction is set to high
147 * and an external pull-up is connected.
149 sch_gpio_set(gc
, gpio_num
, val
);
153 static int sch_gpio_get_direction(struct gpio_chip
*gc
, unsigned int gpio_num
)
155 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
157 if (sch_gpio_reg_get(sch
, gpio_num
, GIO
))
158 return GPIO_LINE_DIRECTION_IN
;
160 return GPIO_LINE_DIRECTION_OUT
;
163 static const struct gpio_chip sch_gpio_chip
= {
165 .owner
= THIS_MODULE
,
166 .direction_input
= sch_gpio_direction_in
,
168 .direction_output
= sch_gpio_direction_out
,
170 .get_direction
= sch_gpio_get_direction
,
173 static int sch_irq_type(struct irq_data
*d
, unsigned int type
)
175 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
176 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
177 irq_hw_number_t gpio_num
= irqd_to_hwirq(d
);
181 switch (type
& IRQ_TYPE_SENSE_MASK
) {
182 case IRQ_TYPE_EDGE_RISING
:
186 case IRQ_TYPE_EDGE_FALLING
:
190 case IRQ_TYPE_EDGE_BOTH
:
198 spin_lock_irqsave(&sch
->lock
, flags
);
200 sch_gpio_reg_set(sch
, gpio_num
, GTPE
, rising
);
201 sch_gpio_reg_set(sch
, gpio_num
, GTNE
, falling
);
203 irq_set_handler_locked(d
, handle_edge_irq
);
205 spin_unlock_irqrestore(&sch
->lock
, flags
);
210 static void sch_irq_ack(struct irq_data
*d
)
212 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
213 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
214 irq_hw_number_t gpio_num
= irqd_to_hwirq(d
);
217 spin_lock_irqsave(&sch
->lock
, flags
);
218 sch_gpio_reg_set(sch
, gpio_num
, GTS
, 1);
219 spin_unlock_irqrestore(&sch
->lock
, flags
);
222 static void sch_irq_mask_unmask(struct gpio_chip
*gc
, irq_hw_number_t gpio_num
, int val
)
224 struct sch_gpio
*sch
= gpiochip_get_data(gc
);
227 spin_lock_irqsave(&sch
->lock
, flags
);
228 sch_gpio_reg_set(sch
, gpio_num
, GGPE
, val
);
229 spin_unlock_irqrestore(&sch
->lock
, flags
);
232 static void sch_irq_mask(struct irq_data
*d
)
234 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
235 irq_hw_number_t gpio_num
= irqd_to_hwirq(d
);
237 sch_irq_mask_unmask(gc
, gpio_num
, 0);
238 gpiochip_disable_irq(gc
, gpio_num
);
241 static void sch_irq_unmask(struct irq_data
*d
)
243 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
244 irq_hw_number_t gpio_num
= irqd_to_hwirq(d
);
246 gpiochip_enable_irq(gc
, gpio_num
);
247 sch_irq_mask_unmask(gc
, gpio_num
, 1);
250 static const struct irq_chip sch_irqchip
= {
252 .irq_ack
= sch_irq_ack
,
253 .irq_mask
= sch_irq_mask
,
254 .irq_unmask
= sch_irq_unmask
,
255 .irq_set_type
= sch_irq_type
,
256 .flags
= IRQCHIP_IMMUTABLE
,
257 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
260 static u32
sch_gpio_gpe_handler(acpi_handle gpe_device
, u32 gpe
, void *context
)
262 struct sch_gpio
*sch
= context
;
263 struct gpio_chip
*gc
= &sch
->chip
;
264 unsigned long core_status
, resume_status
;
265 unsigned long pending
;
270 spin_lock_irqsave(&sch
->lock
, flags
);
272 core_status
= ioread32(sch
->regs
+ CORE_BANK_OFFSET
+ GTS
);
273 resume_status
= ioread32(sch
->regs
+ RESUME_BANK_OFFSET
+ GTS
);
275 spin_unlock_irqrestore(&sch
->lock
, flags
);
277 pending
= (resume_status
<< sch
->resume_base
) | core_status
;
278 for_each_set_bit(offset
, &pending
, sch
->chip
.ngpio
)
279 generic_handle_domain_irq(gc
->irq
.domain
, offset
);
281 /* Set returning value depending on whether we handled an interrupt */
282 ret
= pending
? ACPI_INTERRUPT_HANDLED
: ACPI_INTERRUPT_NOT_HANDLED
;
284 /* Acknowledge GPE to ACPICA */
285 ret
|= ACPI_REENABLE_GPE
;
290 static void sch_gpio_remove_gpe_handler(void *data
)
292 struct sch_gpio
*sch
= data
;
294 acpi_disable_gpe(NULL
, sch
->gpe
);
295 acpi_remove_gpe_handler(NULL
, sch
->gpe
, sch
->gpe_handler
);
298 static int sch_gpio_install_gpe_handler(struct sch_gpio
*sch
)
300 struct device
*dev
= sch
->chip
.parent
;
303 status
= acpi_install_gpe_handler(NULL
, sch
->gpe
, ACPI_GPE_LEVEL_TRIGGERED
,
304 sch
->gpe_handler
, sch
);
305 if (ACPI_FAILURE(status
)) {
306 dev_err(dev
, "Failed to install GPE handler for %u: %s\n",
307 sch
->gpe
, acpi_format_exception(status
));
311 status
= acpi_enable_gpe(NULL
, sch
->gpe
);
312 if (ACPI_FAILURE(status
)) {
313 dev_err(dev
, "Failed to enable GPE handler for %u: %s\n",
314 sch
->gpe
, acpi_format_exception(status
));
315 acpi_remove_gpe_handler(NULL
, sch
->gpe
, sch
->gpe_handler
);
319 return devm_add_action_or_reset(dev
, sch_gpio_remove_gpe_handler
, sch
);
322 static int sch_gpio_probe(struct platform_device
*pdev
)
324 struct device
*dev
= &pdev
->dev
;
325 struct gpio_irq_chip
*girq
;
326 struct sch_gpio
*sch
;
327 struct resource
*res
;
331 sch
= devm_kzalloc(dev
, sizeof(*sch
), GFP_KERNEL
);
335 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
339 regs
= devm_ioport_map(dev
, res
->start
, resource_size(res
));
345 spin_lock_init(&sch
->lock
);
346 sch
->chip
= sch_gpio_chip
;
347 sch
->chip
.label
= dev_name(dev
);
348 sch
->chip
.parent
= dev
;
351 case PCI_DEVICE_ID_INTEL_SCH_LPC
:
352 sch
->resume_base
= 10;
353 sch
->chip
.ngpio
= 14;
356 * GPIO[6:0] enabled by default
357 * GPIO7 is configured by the CMC as SLPIOVR
358 * Enable GPIO[9:8] core powered gpios explicitly
360 sch_gpio_reg_set(sch
, 8, GEN
, 1);
361 sch_gpio_reg_set(sch
, 9, GEN
, 1);
363 * SUS_GPIO[2:0] enabled by default
364 * Enable SUS_GPIO3 resume powered gpio explicitly
366 sch_gpio_reg_set(sch
, 13, GEN
, 1);
369 case PCI_DEVICE_ID_INTEL_ITC_LPC
:
370 sch
->resume_base
= 5;
371 sch
->chip
.ngpio
= 14;
374 case PCI_DEVICE_ID_INTEL_CENTERTON_ILB
:
375 sch
->resume_base
= 21;
376 sch
->chip
.ngpio
= 30;
379 case PCI_DEVICE_ID_INTEL_QUARK_X1000_ILB
:
380 sch
->resume_base
= 2;
388 girq
= &sch
->chip
.irq
;
389 gpio_irq_chip_set_chip(girq
, &sch_irqchip
);
390 girq
->num_parents
= 0;
391 girq
->parents
= NULL
;
392 girq
->parent_handler
= NULL
;
393 girq
->default_type
= IRQ_TYPE_NONE
;
394 girq
->handler
= handle_bad_irq
;
396 /* GPE setup is optional */
397 sch
->gpe
= GPE0E_GPIO
;
398 sch
->gpe_handler
= sch_gpio_gpe_handler
;
400 ret
= sch_gpio_install_gpe_handler(sch
);
402 dev_warn(dev
, "Can't setup GPE, no IRQ support\n");
404 return devm_gpiochip_add_data(dev
, &sch
->chip
, sch
);
407 static struct platform_driver sch_gpio_driver
= {
411 .probe
= sch_gpio_probe
,
414 module_platform_driver(sch_gpio_driver
);
416 MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
417 MODULE_DESCRIPTION("GPIO interface for Intel Poulsbo SCH");
418 MODULE_LICENSE("GPL v2");
419 MODULE_ALIAS("platform:sch_gpio");