Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * AppliedMicro X-Gene SoC GPIO Driver |
| 4 | * |
| 5 | * Copyright (c) 2014, Applied Micro Circuits Corporation |
| 6 | * Author: Feng Kan <fkan@apm.com>. |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Duc Dang | 0c60de3 | 2016-04-30 13:49:27 -0700 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/gpio/driver.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/bitops.h> |
| 18 | |
| 19 | #define GPIO_SET_DR_OFFSET 0x0C |
| 20 | #define GPIO_DATA_OFFSET 0x14 |
| 21 | #define GPIO_BANK_STRIDE 0x0C |
| 22 | |
| 23 | #define XGENE_GPIOS_PER_BANK 16 |
| 24 | #define XGENE_MAX_GPIO_BANKS 3 |
| 25 | #define XGENE_MAX_GPIOS (XGENE_GPIOS_PER_BANK * XGENE_MAX_GPIO_BANKS) |
| 26 | |
| 27 | #define GPIO_BIT_OFFSET(x) (x % XGENE_GPIOS_PER_BANK) |
| 28 | #define GPIO_BANK_OFFSET(x) ((x / XGENE_GPIOS_PER_BANK) * GPIO_BANK_STRIDE) |
| 29 | |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 30 | struct xgene_gpio { |
| 31 | struct gpio_chip chip; |
| 32 | void __iomem *base; |
| 33 | spinlock_t lock; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 34 | u32 set_dr_val[XGENE_MAX_GPIO_BANKS]; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 37 | static int xgene_gpio_get(struct gpio_chip *gc, unsigned int offset) |
| 38 | { |
Linus Walleij | ac9dc85 | 2015-12-07 15:16:50 +0100 | [diff] [blame] | 39 | struct xgene_gpio *chip = gpiochip_get_data(gc); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 40 | unsigned long bank_offset; |
| 41 | u32 bit_offset; |
| 42 | |
| 43 | bank_offset = GPIO_DATA_OFFSET + GPIO_BANK_OFFSET(offset); |
| 44 | bit_offset = GPIO_BIT_OFFSET(offset); |
| 45 | return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset)); |
| 46 | } |
| 47 | |
| 48 | static void __xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val) |
| 49 | { |
Linus Walleij | ac9dc85 | 2015-12-07 15:16:50 +0100 | [diff] [blame] | 50 | struct xgene_gpio *chip = gpiochip_get_data(gc); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 51 | unsigned long bank_offset; |
| 52 | u32 setval, bit_offset; |
| 53 | |
| 54 | bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset); |
| 55 | bit_offset = GPIO_BIT_OFFSET(offset) + XGENE_GPIOS_PER_BANK; |
| 56 | |
| 57 | setval = ioread32(chip->base + bank_offset); |
| 58 | if (val) |
| 59 | setval |= BIT(bit_offset); |
| 60 | else |
| 61 | setval &= ~BIT(bit_offset); |
| 62 | iowrite32(setval, chip->base + bank_offset); |
| 63 | } |
| 64 | |
| 65 | static void xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val) |
| 66 | { |
Linus Walleij | ac9dc85 | 2015-12-07 15:16:50 +0100 | [diff] [blame] | 67 | struct xgene_gpio *chip = gpiochip_get_data(gc); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 68 | unsigned long flags; |
| 69 | |
| 70 | spin_lock_irqsave(&chip->lock, flags); |
| 71 | __xgene_gpio_set(gc, offset, val); |
| 72 | spin_unlock_irqrestore(&chip->lock, flags); |
| 73 | } |
| 74 | |
Linus Walleij | 3b711e0 | 2016-05-01 10:29:10 +0200 | [diff] [blame] | 75 | static int xgene_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) |
| 76 | { |
| 77 | struct xgene_gpio *chip = gpiochip_get_data(gc); |
| 78 | unsigned long bank_offset, bit_offset; |
| 79 | |
| 80 | bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset); |
| 81 | bit_offset = GPIO_BIT_OFFSET(offset); |
| 82 | |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 83 | if (ioread32(chip->base + bank_offset) & BIT(bit_offset)) |
| 84 | return GPIO_LINE_DIRECTION_IN; |
| 85 | |
| 86 | return GPIO_LINE_DIRECTION_OUT; |
Linus Walleij | 3b711e0 | 2016-05-01 10:29:10 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 89 | static int xgene_gpio_dir_in(struct gpio_chip *gc, unsigned int offset) |
| 90 | { |
Linus Walleij | ac9dc85 | 2015-12-07 15:16:50 +0100 | [diff] [blame] | 91 | struct xgene_gpio *chip = gpiochip_get_data(gc); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 92 | unsigned long flags, bank_offset; |
| 93 | u32 dirval, bit_offset; |
| 94 | |
| 95 | bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset); |
| 96 | bit_offset = GPIO_BIT_OFFSET(offset); |
| 97 | |
| 98 | spin_lock_irqsave(&chip->lock, flags); |
| 99 | |
| 100 | dirval = ioread32(chip->base + bank_offset); |
| 101 | dirval |= BIT(bit_offset); |
| 102 | iowrite32(dirval, chip->base + bank_offset); |
| 103 | |
| 104 | spin_unlock_irqrestore(&chip->lock, flags); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int xgene_gpio_dir_out(struct gpio_chip *gc, |
| 110 | unsigned int offset, int val) |
| 111 | { |
Linus Walleij | ac9dc85 | 2015-12-07 15:16:50 +0100 | [diff] [blame] | 112 | struct xgene_gpio *chip = gpiochip_get_data(gc); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 113 | unsigned long flags, bank_offset; |
| 114 | u32 dirval, bit_offset; |
| 115 | |
| 116 | bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset); |
| 117 | bit_offset = GPIO_BIT_OFFSET(offset); |
| 118 | |
| 119 | spin_lock_irqsave(&chip->lock, flags); |
| 120 | |
| 121 | dirval = ioread32(chip->base + bank_offset); |
| 122 | dirval &= ~BIT(bit_offset); |
| 123 | iowrite32(dirval, chip->base + bank_offset); |
| 124 | __xgene_gpio_set(gc, offset, val); |
| 125 | |
| 126 | spin_unlock_irqrestore(&chip->lock, flags); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Arnd Bergmann | b115beb | 2017-02-17 16:13:44 +0100 | [diff] [blame] | 131 | static __maybe_unused int xgene_gpio_suspend(struct device *dev) |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 132 | { |
| 133 | struct xgene_gpio *gpio = dev_get_drvdata(dev); |
| 134 | unsigned long bank_offset; |
| 135 | unsigned int bank; |
| 136 | |
| 137 | for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) { |
| 138 | bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE; |
| 139 | gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset); |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Arnd Bergmann | b115beb | 2017-02-17 16:13:44 +0100 | [diff] [blame] | 144 | static __maybe_unused int xgene_gpio_resume(struct device *dev) |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 145 | { |
| 146 | struct xgene_gpio *gpio = dev_get_drvdata(dev); |
| 147 | unsigned long bank_offset; |
| 148 | unsigned int bank; |
| 149 | |
| 150 | for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) { |
| 151 | bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE; |
| 152 | iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset); |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 158 | |
| 159 | static int xgene_gpio_probe(struct platform_device *pdev) |
| 160 | { |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 161 | struct xgene_gpio *gpio; |
| 162 | int err = 0; |
| 163 | |
| 164 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); |
Bartosz Golaszewski | cf62b4e | 2019-10-02 18:28:52 +0200 | [diff] [blame] | 165 | if (!gpio) |
| 166 | return -ENOMEM; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 167 | |
Bartosz Golaszewski | f63516f | 2019-10-02 18:31:10 +0200 | [diff] [blame] | 168 | gpio->base = devm_platform_ioremap_resource(pdev, 0); |
| 169 | if (IS_ERR(gpio->base)) |
| 170 | return PTR_ERR(gpio->base); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 171 | |
| 172 | gpio->chip.ngpio = XGENE_MAX_GPIOS; |
| 173 | |
Axel Lin | 1a19864 | 2014-09-21 12:31:29 +0800 | [diff] [blame] | 174 | spin_lock_init(&gpio->lock); |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 175 | gpio->chip.parent = &pdev->dev; |
Linus Walleij | 3b711e0 | 2016-05-01 10:29:10 +0200 | [diff] [blame] | 176 | gpio->chip.get_direction = xgene_gpio_get_direction; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 177 | gpio->chip.direction_input = xgene_gpio_dir_in; |
| 178 | gpio->chip.direction_output = xgene_gpio_dir_out; |
| 179 | gpio->chip.get = xgene_gpio_get; |
| 180 | gpio->chip.set = xgene_gpio_set; |
| 181 | gpio->chip.label = dev_name(&pdev->dev); |
| 182 | gpio->chip.base = -1; |
| 183 | |
| 184 | platform_set_drvdata(pdev, gpio); |
| 185 | |
Laxman Dewangan | 9d113c6 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 186 | err = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 187 | if (err) { |
| 188 | dev_err(&pdev->dev, |
| 189 | "failed to register gpiochip.\n"); |
Bartosz Golaszewski | cf62b4e | 2019-10-02 18:28:52 +0200 | [diff] [blame] | 190 | return err; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | dev_info(&pdev->dev, "X-Gene GPIO driver registered.\n"); |
| 194 | return 0; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 197 | static const struct of_device_id xgene_gpio_of_match[] = { |
| 198 | { .compatible = "apm,xgene-gpio", }, |
| 199 | {}, |
| 200 | }; |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 201 | |
Duc Dang | 0c60de3 | 2016-04-30 13:49:27 -0700 | [diff] [blame] | 202 | #ifdef CONFIG_ACPI |
| 203 | static const struct acpi_device_id xgene_gpio_acpi_match[] = { |
| 204 | { "APMC0D14", 0 }, |
| 205 | { }, |
| 206 | }; |
| 207 | #endif |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 208 | |
| 209 | static struct platform_driver xgene_gpio_driver = { |
| 210 | .driver = { |
| 211 | .name = "xgene-gpio", |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 212 | .of_match_table = xgene_gpio_of_match, |
Duc Dang | 0c60de3 | 2016-04-30 13:49:27 -0700 | [diff] [blame] | 213 | .acpi_match_table = ACPI_PTR(xgene_gpio_acpi_match), |
Arnd Bergmann | b115beb | 2017-02-17 16:13:44 +0100 | [diff] [blame] | 214 | .pm = &xgene_gpio_pm, |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 215 | }, |
| 216 | .probe = xgene_gpio_probe, |
Feng Kan | 29cbf45 | 2014-07-31 12:03:25 -0700 | [diff] [blame] | 217 | }; |
Paul Gortmaker | b33d12d | 2016-03-27 11:44:48 -0400 | [diff] [blame] | 218 | builtin_platform_driver(xgene_gpio_driver); |