2019-06-04 04:11:33 -04:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-01-04 15:28:15 -05:00
|
|
|
/*
|
|
|
|
* Atheros AR71XX/AR724X/AR913X GPIO API support
|
|
|
|
*
|
2016-01-28 14:44:33 -05:00
|
|
|
* Copyright (C) 2015 Alban Bedel <albeu@free.fr>
|
2012-03-14 05:45:23 -04:00
|
|
|
* Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
|
|
|
|
* Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
|
2011-01-04 15:28:15 -05:00
|
|
|
* Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
|
|
|
|
*/
|
|
|
|
|
2015-09-01 05:38:02 -04:00
|
|
|
#include <linux/gpio/driver.h>
|
2023-07-14 13:44:58 -04:00
|
|
|
#include <linux/platform_device.h>
|
2015-05-30 20:18:24 -04:00
|
|
|
#include <linux/platform_data/gpio-ath79.h>
|
2023-07-14 13:44:58 -04:00
|
|
|
#include <linux/of.h>
|
2016-01-28 14:44:32 -05:00
|
|
|
#include <linux/interrupt.h>
|
2016-09-12 18:16:28 -04:00
|
|
|
#include <linux/module.h>
|
2016-01-28 14:44:32 -05:00
|
|
|
#include <linux/irq.h>
|
2011-01-04 15:28:15 -05:00
|
|
|
|
2016-01-28 14:44:30 -05:00
|
|
|
#define AR71XX_GPIO_REG_OE 0x00
|
|
|
|
#define AR71XX_GPIO_REG_IN 0x04
|
|
|
|
#define AR71XX_GPIO_REG_SET 0x0c
|
|
|
|
#define AR71XX_GPIO_REG_CLEAR 0x10
|
2011-01-04 15:28:15 -05:00
|
|
|
|
2016-01-28 14:44:32 -05:00
|
|
|
#define AR71XX_GPIO_REG_INT_ENABLE 0x14
|
|
|
|
#define AR71XX_GPIO_REG_INT_TYPE 0x18
|
|
|
|
#define AR71XX_GPIO_REG_INT_POLARITY 0x1c
|
|
|
|
#define AR71XX_GPIO_REG_INT_PENDING 0x20
|
|
|
|
#define AR71XX_GPIO_REG_INT_MASK 0x24
|
|
|
|
|
2015-09-01 05:38:02 -04:00
|
|
|
struct ath79_gpio_ctrl {
|
2016-01-28 14:44:29 -05:00
|
|
|
struct gpio_chip gc;
|
2015-09-01 05:38:02 -04:00
|
|
|
void __iomem *base;
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spinlock_t lock;
|
2016-01-28 14:44:32 -05:00
|
|
|
unsigned long both_edges;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ath79_gpio_ctrl *irq_data_to_ath79_gpio(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
|
|
|
|
|
|
|
|
return container_of(gc, struct ath79_gpio_ctrl, gc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 ath79_gpio_read(struct ath79_gpio_ctrl *ctrl, unsigned reg)
|
|
|
|
{
|
|
|
|
return readl(ctrl->base + reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ath79_gpio_write(struct ath79_gpio_ctrl *ctrl,
|
|
|
|
unsigned reg, u32 val)
|
|
|
|
{
|
2018-07-24 07:57:43 -04:00
|
|
|
writel(val, ctrl->base + reg);
|
2016-01-28 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ath79_gpio_update_bits(
|
|
|
|
struct ath79_gpio_ctrl *ctrl, unsigned reg, u32 mask, u32 bits)
|
|
|
|
{
|
|
|
|
u32 old_val, new_val;
|
|
|
|
|
|
|
|
old_val = ath79_gpio_read(ctrl, reg);
|
|
|
|
new_val = (old_val & ~mask) | (bits & mask);
|
|
|
|
|
|
|
|
if (new_val != old_val)
|
|
|
|
ath79_gpio_write(ctrl, reg, new_val);
|
|
|
|
|
|
|
|
return new_val != old_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ath79_gpio_irq_unmask(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
|
|
|
|
u32 mask = BIT(irqd_to_hwirq(data));
|
|
|
|
unsigned long flags;
|
|
|
|
|
2023-03-09 02:45:54 -05:00
|
|
|
gpiochip_enable_irq(&ctrl->gc, irqd_to_hwirq(data));
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_irqsave(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_unlock_irqrestore(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ath79_gpio_irq_mask(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
|
|
|
|
u32 mask = BIT(irqd_to_hwirq(data));
|
|
|
|
unsigned long flags;
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_irqsave(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_unlock_irqrestore(&ctrl->lock, flags);
|
2023-03-09 02:45:54 -05:00
|
|
|
gpiochip_disable_irq(&ctrl->gc, irqd_to_hwirq(data));
|
2016-01-28 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ath79_gpio_irq_enable(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
|
|
|
|
u32 mask = BIT(irqd_to_hwirq(data));
|
|
|
|
unsigned long flags;
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_irqsave(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
|
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_unlock_irqrestore(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ath79_gpio_irq_disable(struct irq_data *data)
|
|
|
|
{
|
|
|
|
struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
|
|
|
|
u32 mask = BIT(irqd_to_hwirq(data));
|
|
|
|
unsigned long flags;
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_irqsave(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
|
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_unlock_irqrestore(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ath79_gpio_irq_set_type(struct irq_data *data,
|
|
|
|
unsigned int flow_type)
|
|
|
|
{
|
|
|
|
struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
|
|
|
|
u32 mask = BIT(irqd_to_hwirq(data));
|
|
|
|
u32 type = 0, polarity = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
bool disabled;
|
|
|
|
|
|
|
|
switch (flow_type) {
|
|
|
|
case IRQ_TYPE_EDGE_RISING:
|
|
|
|
polarity |= mask;
|
2020-11-19 12:09:01 -05:00
|
|
|
fallthrough;
|
2016-01-28 14:44:32 -05:00
|
|
|
case IRQ_TYPE_EDGE_FALLING:
|
|
|
|
case IRQ_TYPE_EDGE_BOTH:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRQ_TYPE_LEVEL_HIGH:
|
|
|
|
polarity |= mask;
|
2020-08-23 18:36:59 -04:00
|
|
|
fallthrough;
|
2016-01-28 14:44:32 -05:00
|
|
|
case IRQ_TYPE_LEVEL_LOW:
|
|
|
|
type |= mask;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_irqsave(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
|
|
|
|
if (flow_type == IRQ_TYPE_EDGE_BOTH) {
|
|
|
|
ctrl->both_edges |= mask;
|
|
|
|
polarity = ~ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
|
|
|
|
} else {
|
|
|
|
ctrl->both_edges &= ~mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* As the IRQ configuration can't be loaded atomically we
|
|
|
|
* have to disable the interrupt while the configuration state
|
|
|
|
* is invalid.
|
|
|
|
*/
|
|
|
|
disabled = ath79_gpio_update_bits(
|
|
|
|
ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
|
|
|
|
|
|
|
|
ath79_gpio_update_bits(
|
|
|
|
ctrl, AR71XX_GPIO_REG_INT_TYPE, mask, type);
|
|
|
|
ath79_gpio_update_bits(
|
|
|
|
ctrl, AR71XX_GPIO_REG_INT_POLARITY, mask, polarity);
|
|
|
|
|
|
|
|
if (disabled)
|
|
|
|
ath79_gpio_update_bits(
|
|
|
|
ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_unlock_irqrestore(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-09 02:45:54 -05:00
|
|
|
static const struct irq_chip ath79_gpio_irqchip = {
|
2016-01-28 14:44:32 -05:00
|
|
|
.name = "gpio-ath79",
|
|
|
|
.irq_enable = ath79_gpio_irq_enable,
|
|
|
|
.irq_disable = ath79_gpio_irq_disable,
|
|
|
|
.irq_mask = ath79_gpio_irq_mask,
|
|
|
|
.irq_unmask = ath79_gpio_irq_unmask,
|
|
|
|
.irq_set_type = ath79_gpio_irq_set_type,
|
2023-03-09 02:45:54 -05:00
|
|
|
.flags = IRQCHIP_IMMUTABLE,
|
|
|
|
GPIOCHIP_IRQ_RESOURCE_HELPERS,
|
2015-09-01 05:38:02 -04:00
|
|
|
};
|
|
|
|
|
2016-01-28 14:44:32 -05:00
|
|
|
static void ath79_gpio_irq_handler(struct irq_desc *desc)
|
|
|
|
{
|
|
|
|
struct gpio_chip *gc = irq_desc_get_handler_data(desc);
|
|
|
|
struct irq_chip *irqchip = irq_desc_get_chip(desc);
|
|
|
|
struct ath79_gpio_ctrl *ctrl =
|
|
|
|
container_of(gc, struct ath79_gpio_ctrl, gc);
|
|
|
|
unsigned long flags, pending;
|
|
|
|
u32 both_edges, state;
|
|
|
|
int irq;
|
|
|
|
|
|
|
|
chained_irq_enter(irqchip, desc);
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_irqsave(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
|
|
|
|
pending = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_INT_PENDING);
|
|
|
|
|
|
|
|
/* Update the polarity of the both edges irqs */
|
|
|
|
both_edges = ctrl->both_edges & pending;
|
|
|
|
if (both_edges) {
|
|
|
|
state = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
|
|
|
|
ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_POLARITY,
|
|
|
|
both_edges, ~state);
|
|
|
|
}
|
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_unlock_irqrestore(&ctrl->lock, flags);
|
2016-01-28 14:44:32 -05:00
|
|
|
|
2021-05-04 12:42:18 -04:00
|
|
|
for_each_set_bit(irq, &pending, gc->ngpio)
|
|
|
|
generic_handle_domain_irq(gc->irq.domain, irq);
|
2016-01-28 14:44:32 -05:00
|
|
|
|
|
|
|
chained_irq_exit(irqchip, desc);
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:18:24 -04:00
|
|
|
static const struct of_device_id ath79_gpio_of_match[] = {
|
|
|
|
{ .compatible = "qca,ar7100-gpio" },
|
|
|
|
{ .compatible = "qca,ar9340-gpio" },
|
|
|
|
{},
|
|
|
|
};
|
2016-10-18 16:44:01 -04:00
|
|
|
MODULE_DEVICE_TABLE(of, ath79_gpio_of_match);
|
2015-05-30 20:18:24 -04:00
|
|
|
|
|
|
|
static int ath79_gpio_probe(struct platform_device *pdev)
|
2011-01-04 15:28:15 -05:00
|
|
|
{
|
2015-11-23 10:23:18 -05:00
|
|
|
struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
|
2019-06-25 07:57:33 -04:00
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct device_node *np = dev->of_node;
|
2015-09-01 05:38:02 -04:00
|
|
|
struct ath79_gpio_ctrl *ctrl;
|
2019-06-25 07:57:33 -04:00
|
|
|
struct gpio_irq_chip *girq;
|
2015-09-01 05:38:02 -04:00
|
|
|
u32 ath79_gpio_count;
|
2015-05-30 20:18:24 -04:00
|
|
|
bool oe_inverted;
|
2011-01-04 15:28:15 -05:00
|
|
|
int err;
|
|
|
|
|
2019-06-25 07:57:33 -04:00
|
|
|
ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
|
2015-09-01 05:38:02 -04:00
|
|
|
if (!ctrl)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-05-30 20:18:24 -04:00
|
|
|
if (np) {
|
|
|
|
err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
|
|
|
|
if (err) {
|
2019-06-25 07:57:33 -04:00
|
|
|
dev_err(dev, "ngpios property is not valid\n");
|
2015-05-30 20:18:24 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
|
|
|
|
} else if (pdata) {
|
|
|
|
ath79_gpio_count = pdata->ngpios;
|
|
|
|
oe_inverted = pdata->oe_inverted;
|
|
|
|
} else {
|
2019-06-25 07:57:33 -04:00
|
|
|
dev_err(dev, "No DT node or platform data found\n");
|
2015-05-30 20:18:24 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-02-19 20:48:07 -05:00
|
|
|
if (ath79_gpio_count >= 32) {
|
2019-06-25 07:57:33 -04:00
|
|
|
dev_err(dev, "ngpios must be less than 32\n");
|
2016-02-19 20:48:07 -05:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-10-02 12:41:10 -04:00
|
|
|
ctrl->base = devm_platform_ioremap_resource(pdev, 0);
|
|
|
|
if (IS_ERR(ctrl->base))
|
|
|
|
return PTR_ERR(ctrl->base);
|
2011-01-04 15:28:15 -05:00
|
|
|
|
2017-03-09 11:21:53 -05:00
|
|
|
raw_spin_lock_init(&ctrl->lock);
|
2019-06-25 07:57:33 -04:00
|
|
|
err = bgpio_init(&ctrl->gc, dev, 4,
|
2016-01-28 14:44:29 -05:00
|
|
|
ctrl->base + AR71XX_GPIO_REG_IN,
|
|
|
|
ctrl->base + AR71XX_GPIO_REG_SET,
|
|
|
|
ctrl->base + AR71XX_GPIO_REG_CLEAR,
|
|
|
|
oe_inverted ? NULL : ctrl->base + AR71XX_GPIO_REG_OE,
|
|
|
|
oe_inverted ? ctrl->base + AR71XX_GPIO_REG_OE : NULL,
|
|
|
|
0);
|
|
|
|
if (err) {
|
2019-06-25 07:57:33 -04:00
|
|
|
dev_err(dev, "bgpio_init failed\n");
|
2016-01-28 14:44:29 -05:00
|
|
|
return err;
|
2012-03-14 05:45:23 -04:00
|
|
|
}
|
2016-01-28 14:44:29 -05:00
|
|
|
/* Use base 0 to stay compatible with legacy platforms */
|
|
|
|
ctrl->gc.base = 0;
|
2011-01-04 15:28:15 -05:00
|
|
|
|
2019-06-25 07:57:33 -04:00
|
|
|
/* Optional interrupt setup */
|
|
|
|
if (!np || of_property_read_bool(np, "interrupt-controller")) {
|
|
|
|
girq = &ctrl->gc.irq;
|
2023-03-09 02:45:54 -05:00
|
|
|
gpio_irq_chip_set_chip(girq, &ath79_gpio_irqchip);
|
2019-06-25 07:57:33 -04:00
|
|
|
girq->parent_handler = ath79_gpio_irq_handler;
|
|
|
|
girq->num_parents = 1;
|
|
|
|
girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!girq->parents)
|
|
|
|
return -ENOMEM;
|
|
|
|
girq->parents[0] = platform_get_irq(pdev, 0);
|
|
|
|
girq->default_type = IRQ_TYPE_NONE;
|
|
|
|
girq->handler = handle_simple_irq;
|
2015-05-30 20:18:24 -04:00
|
|
|
}
|
|
|
|
|
2021-05-17 07:37:07 -04:00
|
|
|
return devm_gpiochip_add_data(dev, &ctrl->gc, ctrl);
|
2016-01-28 14:44:31 -05:00
|
|
|
}
|
|
|
|
|
2015-05-30 20:18:24 -04:00
|
|
|
static struct platform_driver ath79_gpio_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ath79-gpio",
|
|
|
|
.of_match_table = ath79_gpio_of_match,
|
|
|
|
},
|
|
|
|
.probe = ath79_gpio_probe,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(ath79_gpio_driver);
|
2017-11-20 15:54:26 -05:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X GPIO API support");
|
|
|
|
MODULE_LICENSE("GPL v2");
|