xref: /linux/drivers/gpio/gpio-xgene.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
229cbf458SFeng Kan /*
329cbf458SFeng Kan  * AppliedMicro X-Gene SoC GPIO Driver
429cbf458SFeng Kan  *
529cbf458SFeng Kan  * Copyright (c) 2014, Applied Micro Circuits Corporation
629cbf458SFeng Kan  * Author: Feng Kan <fkan@apm.com>.
729cbf458SFeng Kan  */
829cbf458SFeng Kan 
90c60de3fSDuc Dang #include <linux/acpi.h>
1029cbf458SFeng Kan #include <linux/kernel.h>
1129cbf458SFeng Kan #include <linux/init.h>
1229cbf458SFeng Kan #include <linux/io.h>
1329cbf458SFeng Kan #include <linux/spinlock.h>
1429cbf458SFeng Kan #include <linux/platform_device.h>
1529cbf458SFeng Kan #include <linux/gpio/driver.h>
1629cbf458SFeng Kan #include <linux/types.h>
1729cbf458SFeng Kan #include <linux/bitops.h>
1829cbf458SFeng Kan 
1929cbf458SFeng Kan #define GPIO_SET_DR_OFFSET	0x0C
2029cbf458SFeng Kan #define GPIO_DATA_OFFSET	0x14
2129cbf458SFeng Kan #define GPIO_BANK_STRIDE	0x0C
2229cbf458SFeng Kan 
2329cbf458SFeng Kan #define XGENE_GPIOS_PER_BANK	16
2429cbf458SFeng Kan #define XGENE_MAX_GPIO_BANKS	3
2529cbf458SFeng Kan #define XGENE_MAX_GPIOS		(XGENE_GPIOS_PER_BANK * XGENE_MAX_GPIO_BANKS)
2629cbf458SFeng Kan 
2729cbf458SFeng Kan #define GPIO_BIT_OFFSET(x)	(x % XGENE_GPIOS_PER_BANK)
2829cbf458SFeng Kan #define GPIO_BANK_OFFSET(x)	((x / XGENE_GPIOS_PER_BANK) * GPIO_BANK_STRIDE)
2929cbf458SFeng Kan 
3029cbf458SFeng Kan struct xgene_gpio {
3129cbf458SFeng Kan 	struct gpio_chip	chip;
3229cbf458SFeng Kan 	void __iomem		*base;
3329cbf458SFeng Kan 	spinlock_t		lock;
3429cbf458SFeng Kan 	u32			set_dr_val[XGENE_MAX_GPIO_BANKS];
3529cbf458SFeng Kan };
3629cbf458SFeng Kan 
xgene_gpio_get(struct gpio_chip * gc,unsigned int offset)3729cbf458SFeng Kan static int xgene_gpio_get(struct gpio_chip *gc, unsigned int offset)
3829cbf458SFeng Kan {
39ac9dc85eSLinus Walleij 	struct xgene_gpio *chip = gpiochip_get_data(gc);
4029cbf458SFeng Kan 	unsigned long bank_offset;
4129cbf458SFeng Kan 	u32 bit_offset;
4229cbf458SFeng Kan 
4329cbf458SFeng Kan 	bank_offset = GPIO_DATA_OFFSET + GPIO_BANK_OFFSET(offset);
4429cbf458SFeng Kan 	bit_offset = GPIO_BIT_OFFSET(offset);
4529cbf458SFeng Kan 	return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
4629cbf458SFeng Kan }
4729cbf458SFeng Kan 
__xgene_gpio_set(struct gpio_chip * gc,unsigned int offset,int val)4829cbf458SFeng Kan static void __xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
4929cbf458SFeng Kan {
50ac9dc85eSLinus Walleij 	struct xgene_gpio *chip = gpiochip_get_data(gc);
5129cbf458SFeng Kan 	unsigned long bank_offset;
5229cbf458SFeng Kan 	u32 setval, bit_offset;
5329cbf458SFeng Kan 
5429cbf458SFeng Kan 	bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
5529cbf458SFeng Kan 	bit_offset = GPIO_BIT_OFFSET(offset) + XGENE_GPIOS_PER_BANK;
5629cbf458SFeng Kan 
5729cbf458SFeng Kan 	setval = ioread32(chip->base + bank_offset);
5829cbf458SFeng Kan 	if (val)
5929cbf458SFeng Kan 		setval |= BIT(bit_offset);
6029cbf458SFeng Kan 	else
6129cbf458SFeng Kan 		setval &= ~BIT(bit_offset);
6229cbf458SFeng Kan 	iowrite32(setval, chip->base + bank_offset);
6329cbf458SFeng Kan }
6429cbf458SFeng Kan 
xgene_gpio_set(struct gpio_chip * gc,unsigned int offset,int val)6529cbf458SFeng Kan static void xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
6629cbf458SFeng Kan {
67ac9dc85eSLinus Walleij 	struct xgene_gpio *chip = gpiochip_get_data(gc);
6829cbf458SFeng Kan 	unsigned long flags;
6929cbf458SFeng Kan 
7029cbf458SFeng Kan 	spin_lock_irqsave(&chip->lock, flags);
7129cbf458SFeng Kan 	__xgene_gpio_set(gc, offset, val);
7229cbf458SFeng Kan 	spin_unlock_irqrestore(&chip->lock, flags);
7329cbf458SFeng Kan }
7429cbf458SFeng Kan 
xgene_gpio_get_direction(struct gpio_chip * gc,unsigned int offset)753b711e07SLinus Walleij static int xgene_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
763b711e07SLinus Walleij {
773b711e07SLinus Walleij 	struct xgene_gpio *chip = gpiochip_get_data(gc);
783b711e07SLinus Walleij 	unsigned long bank_offset, bit_offset;
793b711e07SLinus Walleij 
803b711e07SLinus Walleij 	bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
813b711e07SLinus Walleij 	bit_offset = GPIO_BIT_OFFSET(offset);
823b711e07SLinus Walleij 
83e42615ecSMatti Vaittinen 	if (ioread32(chip->base + bank_offset) & BIT(bit_offset))
84e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
85e42615ecSMatti Vaittinen 
86e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
873b711e07SLinus Walleij }
883b711e07SLinus Walleij 
xgene_gpio_dir_in(struct gpio_chip * gc,unsigned int offset)8929cbf458SFeng Kan static int xgene_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
9029cbf458SFeng Kan {
91ac9dc85eSLinus Walleij 	struct xgene_gpio *chip = gpiochip_get_data(gc);
9229cbf458SFeng Kan 	unsigned long flags, bank_offset;
9329cbf458SFeng Kan 	u32 dirval, bit_offset;
9429cbf458SFeng Kan 
9529cbf458SFeng Kan 	bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
9629cbf458SFeng Kan 	bit_offset = GPIO_BIT_OFFSET(offset);
9729cbf458SFeng Kan 
9829cbf458SFeng Kan 	spin_lock_irqsave(&chip->lock, flags);
9929cbf458SFeng Kan 
10029cbf458SFeng Kan 	dirval = ioread32(chip->base + bank_offset);
10129cbf458SFeng Kan 	dirval |= BIT(bit_offset);
10229cbf458SFeng Kan 	iowrite32(dirval, chip->base + bank_offset);
10329cbf458SFeng Kan 
10429cbf458SFeng Kan 	spin_unlock_irqrestore(&chip->lock, flags);
10529cbf458SFeng Kan 
10629cbf458SFeng Kan 	return 0;
10729cbf458SFeng Kan }
10829cbf458SFeng Kan 
xgene_gpio_dir_out(struct gpio_chip * gc,unsigned int offset,int val)10929cbf458SFeng Kan static int xgene_gpio_dir_out(struct gpio_chip *gc,
11029cbf458SFeng Kan 					unsigned int offset, int val)
11129cbf458SFeng Kan {
112ac9dc85eSLinus Walleij 	struct xgene_gpio *chip = gpiochip_get_data(gc);
11329cbf458SFeng Kan 	unsigned long flags, bank_offset;
11429cbf458SFeng Kan 	u32 dirval, bit_offset;
11529cbf458SFeng Kan 
11629cbf458SFeng Kan 	bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
11729cbf458SFeng Kan 	bit_offset = GPIO_BIT_OFFSET(offset);
11829cbf458SFeng Kan 
11929cbf458SFeng Kan 	spin_lock_irqsave(&chip->lock, flags);
12029cbf458SFeng Kan 
12129cbf458SFeng Kan 	dirval = ioread32(chip->base + bank_offset);
12229cbf458SFeng Kan 	dirval &= ~BIT(bit_offset);
12329cbf458SFeng Kan 	iowrite32(dirval, chip->base + bank_offset);
12429cbf458SFeng Kan 	__xgene_gpio_set(gc, offset, val);
12529cbf458SFeng Kan 
12629cbf458SFeng Kan 	spin_unlock_irqrestore(&chip->lock, flags);
12729cbf458SFeng Kan 
12829cbf458SFeng Kan 	return 0;
12929cbf458SFeng Kan }
13029cbf458SFeng Kan 
xgene_gpio_suspend(struct device * dev)131b115bebcSArnd Bergmann static __maybe_unused int xgene_gpio_suspend(struct device *dev)
13229cbf458SFeng Kan {
13329cbf458SFeng Kan 	struct xgene_gpio *gpio = dev_get_drvdata(dev);
13429cbf458SFeng Kan 	unsigned long bank_offset;
13529cbf458SFeng Kan 	unsigned int bank;
13629cbf458SFeng Kan 
13729cbf458SFeng Kan 	for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
13829cbf458SFeng Kan 		bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
13929cbf458SFeng Kan 		gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset);
14029cbf458SFeng Kan 	}
14129cbf458SFeng Kan 	return 0;
14229cbf458SFeng Kan }
14329cbf458SFeng Kan 
xgene_gpio_resume(struct device * dev)144b115bebcSArnd Bergmann static __maybe_unused int xgene_gpio_resume(struct device *dev)
14529cbf458SFeng Kan {
14629cbf458SFeng Kan 	struct xgene_gpio *gpio = dev_get_drvdata(dev);
14729cbf458SFeng Kan 	unsigned long bank_offset;
14829cbf458SFeng Kan 	unsigned int bank;
14929cbf458SFeng Kan 
15029cbf458SFeng Kan 	for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
15129cbf458SFeng Kan 		bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
15229cbf458SFeng Kan 		iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset);
15329cbf458SFeng Kan 	}
15429cbf458SFeng Kan 	return 0;
15529cbf458SFeng Kan }
15629cbf458SFeng Kan 
15729cbf458SFeng Kan static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);
15829cbf458SFeng Kan 
xgene_gpio_probe(struct platform_device * pdev)15929cbf458SFeng Kan static int xgene_gpio_probe(struct platform_device *pdev)
16029cbf458SFeng Kan {
16129cbf458SFeng Kan 	struct xgene_gpio *gpio;
16229cbf458SFeng Kan 
16329cbf458SFeng Kan 	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
164cf62b4e4SBartosz Golaszewski 	if (!gpio)
165cf62b4e4SBartosz Golaszewski 		return -ENOMEM;
16629cbf458SFeng Kan 
167f63516f4SBartosz Golaszewski 	gpio->base = devm_platform_ioremap_resource(pdev, 0);
168f63516f4SBartosz Golaszewski 	if (IS_ERR(gpio->base))
169f63516f4SBartosz Golaszewski 		return PTR_ERR(gpio->base);
17029cbf458SFeng Kan 
17129cbf458SFeng Kan 	gpio->chip.ngpio = XGENE_MAX_GPIOS;
17229cbf458SFeng Kan 
1731a19864eSAxel Lin 	spin_lock_init(&gpio->lock);
17458383c78SLinus Walleij 	gpio->chip.parent = &pdev->dev;
1753b711e07SLinus Walleij 	gpio->chip.get_direction = xgene_gpio_get_direction;
17629cbf458SFeng Kan 	gpio->chip.direction_input = xgene_gpio_dir_in;
17729cbf458SFeng Kan 	gpio->chip.direction_output = xgene_gpio_dir_out;
17829cbf458SFeng Kan 	gpio->chip.get = xgene_gpio_get;
17929cbf458SFeng Kan 	gpio->chip.set = xgene_gpio_set;
18029cbf458SFeng Kan 	gpio->chip.label = dev_name(&pdev->dev);
18129cbf458SFeng Kan 	gpio->chip.base = -1;
18229cbf458SFeng Kan 
18329cbf458SFeng Kan 	platform_set_drvdata(pdev, gpio);
18429cbf458SFeng Kan 
185*94a7b669SAlexandru Ardelean 	return devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
18629cbf458SFeng Kan }
18729cbf458SFeng Kan 
18829cbf458SFeng Kan static const struct of_device_id xgene_gpio_of_match[] = {
18929cbf458SFeng Kan 	{ .compatible = "apm,xgene-gpio", },
19029cbf458SFeng Kan 	{},
19129cbf458SFeng Kan };
19229cbf458SFeng Kan 
1930c60de3fSDuc Dang #ifdef CONFIG_ACPI
1940c60de3fSDuc Dang static const struct acpi_device_id xgene_gpio_acpi_match[] = {
1950c60de3fSDuc Dang 	{ "APMC0D14", 0 },
1960c60de3fSDuc Dang 	{ },
1970c60de3fSDuc Dang };
1980c60de3fSDuc Dang #endif
19929cbf458SFeng Kan 
20029cbf458SFeng Kan static struct platform_driver xgene_gpio_driver = {
20129cbf458SFeng Kan 	.driver = {
20229cbf458SFeng Kan 		.name = "xgene-gpio",
20329cbf458SFeng Kan 		.of_match_table = xgene_gpio_of_match,
2040c60de3fSDuc Dang 		.acpi_match_table = ACPI_PTR(xgene_gpio_acpi_match),
205b115bebcSArnd Bergmann 		.pm     = &xgene_gpio_pm,
20629cbf458SFeng Kan 	},
20729cbf458SFeng Kan 	.probe = xgene_gpio_probe,
20829cbf458SFeng Kan };
209b33d12d3SPaul Gortmaker builtin_platform_driver(xgene_gpio_driver);
210