Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/gpio/gpio-mb86s7x.c |
| 4 | * |
| 5 | * Copyright (C) 2015 Fujitsu Semiconductor Limited |
| 6 | * Copyright (C) 2015 Linaro Ltd. |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 10 | #include <linux/io.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/clk.h> |
Ard Biesheuvel | e1289db | 2017-10-27 21:21:47 +0100 | [diff] [blame] | 13 | #include <linux/module.h> |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/ioport.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/gpio/driver.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/slab.h> |
| 22 | |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 23 | #include "gpiolib.h" |
Andy Shevchenko | 77cb907 | 2019-07-30 13:43:36 +0300 | [diff] [blame] | 24 | #include "gpiolib-acpi.h" |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 25 | |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 26 | /* |
| 27 | * Only first 8bits of a register correspond to each pin, |
| 28 | * so there are 4 registers for 32 pins. |
| 29 | */ |
| 30 | #define PDR(x) (0x0 + x / 8 * 4) |
| 31 | #define DDR(x) (0x10 + x / 8 * 4) |
| 32 | #define PFR(x) (0x20 + x / 8 * 4) |
| 33 | |
| 34 | #define OFFSET(x) BIT((x) % 8) |
| 35 | |
| 36 | struct mb86s70_gpio_chip { |
| 37 | struct gpio_chip gc; |
| 38 | void __iomem *base; |
| 39 | struct clk *clk; |
| 40 | spinlock_t lock; |
| 41 | }; |
| 42 | |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 43 | static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio) |
| 44 | { |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 45 | struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 46 | unsigned long flags; |
| 47 | u32 val; |
| 48 | |
| 49 | spin_lock_irqsave(&gchip->lock, flags); |
| 50 | |
| 51 | val = readl(gchip->base + PFR(gpio)); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 52 | val &= ~OFFSET(gpio); |
| 53 | writel(val, gchip->base + PFR(gpio)); |
| 54 | |
| 55 | spin_unlock_irqrestore(&gchip->lock, flags); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio) |
| 61 | { |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 62 | struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 63 | unsigned long flags; |
| 64 | u32 val; |
| 65 | |
| 66 | spin_lock_irqsave(&gchip->lock, flags); |
| 67 | |
| 68 | val = readl(gchip->base + PFR(gpio)); |
| 69 | val |= OFFSET(gpio); |
| 70 | writel(val, gchip->base + PFR(gpio)); |
| 71 | |
| 72 | spin_unlock_irqrestore(&gchip->lock, flags); |
| 73 | } |
| 74 | |
| 75 | static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio) |
| 76 | { |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 77 | struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 78 | unsigned long flags; |
| 79 | unsigned char val; |
| 80 | |
| 81 | spin_lock_irqsave(&gchip->lock, flags); |
| 82 | |
| 83 | val = readl(gchip->base + DDR(gpio)); |
| 84 | val &= ~OFFSET(gpio); |
| 85 | writel(val, gchip->base + DDR(gpio)); |
| 86 | |
| 87 | spin_unlock_irqrestore(&gchip->lock, flags); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int mb86s70_gpio_direction_output(struct gpio_chip *gc, |
| 93 | unsigned gpio, int value) |
| 94 | { |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 95 | struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 96 | unsigned long flags; |
| 97 | unsigned char val; |
| 98 | |
| 99 | spin_lock_irqsave(&gchip->lock, flags); |
| 100 | |
| 101 | val = readl(gchip->base + PDR(gpio)); |
| 102 | if (value) |
| 103 | val |= OFFSET(gpio); |
| 104 | else |
| 105 | val &= ~OFFSET(gpio); |
| 106 | writel(val, gchip->base + PDR(gpio)); |
| 107 | |
| 108 | val = readl(gchip->base + DDR(gpio)); |
| 109 | val |= OFFSET(gpio); |
| 110 | writel(val, gchip->base + DDR(gpio)); |
| 111 | |
| 112 | spin_unlock_irqrestore(&gchip->lock, flags); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio) |
| 118 | { |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 119 | struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 120 | |
| 121 | return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio)); |
| 122 | } |
| 123 | |
| 124 | static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value) |
| 125 | { |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 126 | struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 127 | unsigned long flags; |
| 128 | unsigned char val; |
| 129 | |
| 130 | spin_lock_irqsave(&gchip->lock, flags); |
| 131 | |
| 132 | val = readl(gchip->base + PDR(gpio)); |
| 133 | if (value) |
| 134 | val |= OFFSET(gpio); |
| 135 | else |
| 136 | val &= ~OFFSET(gpio); |
| 137 | writel(val, gchip->base + PDR(gpio)); |
| 138 | |
| 139 | spin_unlock_irqrestore(&gchip->lock, flags); |
| 140 | } |
| 141 | |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 142 | static int mb86s70_gpio_to_irq(struct gpio_chip *gc, unsigned int offset) |
| 143 | { |
| 144 | int irq, index; |
| 145 | |
| 146 | for (index = 0;; index++) { |
| 147 | irq = platform_get_irq(to_platform_device(gc->parent), index); |
Andy Shevchenko | db67aa3 | 2020-05-12 21:26:23 +0300 | [diff] [blame] | 148 | if (irq < 0) |
| 149 | return irq; |
| 150 | if (irq == 0) |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 151 | break; |
| 152 | if (irq_get_irq_data(irq)->hwirq == offset) |
| 153 | return irq; |
| 154 | } |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 158 | static int mb86s70_gpio_probe(struct platform_device *pdev) |
| 159 | { |
| 160 | struct mb86s70_gpio_chip *gchip; |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 161 | int ret; |
| 162 | |
| 163 | gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL); |
| 164 | if (gchip == NULL) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | platform_set_drvdata(pdev, gchip); |
| 168 | |
Enrico Weigelt, metux IT consult | 329e23f | 2019-03-11 19:54:58 +0100 | [diff] [blame] | 169 | gchip->base = devm_platform_ioremap_resource(pdev, 0); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 170 | if (IS_ERR(gchip->base)) |
| 171 | return PTR_ERR(gchip->base); |
| 172 | |
Andy Shevchenko | 672de52 | 2020-05-12 21:26:22 +0300 | [diff] [blame] | 173 | gchip->clk = devm_clk_get_optional(&pdev->dev, NULL); |
| 174 | if (IS_ERR(gchip->clk)) |
| 175 | return PTR_ERR(gchip->clk); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 176 | |
Andy Shevchenko | 672de52 | 2020-05-12 21:26:22 +0300 | [diff] [blame] | 177 | ret = clk_prepare_enable(gchip->clk); |
| 178 | if (ret) |
| 179 | return ret; |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 180 | |
| 181 | spin_lock_init(&gchip->lock); |
| 182 | |
| 183 | gchip->gc.direction_output = mb86s70_gpio_direction_output; |
| 184 | gchip->gc.direction_input = mb86s70_gpio_direction_input; |
| 185 | gchip->gc.request = mb86s70_gpio_request; |
| 186 | gchip->gc.free = mb86s70_gpio_free; |
| 187 | gchip->gc.get = mb86s70_gpio_get; |
| 188 | gchip->gc.set = mb86s70_gpio_set; |
Andy Shevchenko | db67aa3 | 2020-05-12 21:26:23 +0300 | [diff] [blame] | 189 | gchip->gc.to_irq = mb86s70_gpio_to_irq; |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 190 | gchip->gc.label = dev_name(&pdev->dev); |
| 191 | gchip->gc.ngpio = 32; |
| 192 | gchip->gc.owner = THIS_MODULE; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 193 | gchip->gc.parent = &pdev->dev; |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 194 | gchip->gc.base = -1; |
| 195 | |
Linus Walleij | 01f76b2 | 2015-12-07 10:01:36 +0100 | [diff] [blame] | 196 | ret = gpiochip_add_data(&gchip->gc, gchip); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 197 | if (ret) { |
| 198 | dev_err(&pdev->dev, "couldn't register gpio driver\n"); |
| 199 | clk_disable_unprepare(gchip->clk); |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 200 | return ret; |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Andy Shevchenko | db67aa3 | 2020-05-12 21:26:23 +0300 | [diff] [blame] | 203 | acpi_gpiochip_request_interrupts(&gchip->gc); |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 204 | |
| 205 | return 0; |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int mb86s70_gpio_remove(struct platform_device *pdev) |
| 209 | { |
| 210 | struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev); |
| 211 | |
Andy Shevchenko | db67aa3 | 2020-05-12 21:26:23 +0300 | [diff] [blame] | 212 | acpi_gpiochip_free_interrupts(&gchip->gc); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 213 | gpiochip_remove(&gchip->gc); |
| 214 | clk_disable_unprepare(gchip->clk); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static const struct of_device_id mb86s70_gpio_dt_ids[] = { |
| 220 | { .compatible = "fujitsu,mb86s70-gpio" }, |
| 221 | { /* sentinel */ } |
| 222 | }; |
Ard Biesheuvel | e1289db | 2017-10-27 21:21:47 +0100 | [diff] [blame] | 223 | MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 224 | |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 225 | #ifdef CONFIG_ACPI |
| 226 | static const struct acpi_device_id mb86s70_gpio_acpi_ids[] = { |
| 227 | { "SCX0007" }, |
| 228 | { /* sentinel */ } |
| 229 | }; |
| 230 | MODULE_DEVICE_TABLE(acpi, mb86s70_gpio_acpi_ids); |
| 231 | #endif |
| 232 | |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 233 | static struct platform_driver mb86s70_gpio_driver = { |
| 234 | .driver = { |
| 235 | .name = "mb86s70-gpio", |
| 236 | .of_match_table = mb86s70_gpio_dt_ids, |
Ard Biesheuvel | f3d705d5 | 2019-05-28 15:36:47 +0200 | [diff] [blame] | 237 | .acpi_match_table = ACPI_PTR(mb86s70_gpio_acpi_ids), |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 238 | }, |
| 239 | .probe = mb86s70_gpio_probe, |
| 240 | .remove = mb86s70_gpio_remove, |
| 241 | }; |
Ard Biesheuvel | e1289db | 2017-10-27 21:21:47 +0100 | [diff] [blame] | 242 | module_platform_driver(mb86s70_gpio_driver); |
Jassi Brar | 0da094d | 2015-01-19 18:35:53 +0800 | [diff] [blame] | 243 | |
Ard Biesheuvel | e1289db | 2017-10-27 21:21:47 +0100 | [diff] [blame] | 244 | MODULE_DESCRIPTION("MB86S7x GPIO Driver"); |
| 245 | MODULE_ALIAS("platform:mb86s70-gpio"); |
| 246 | MODULE_LICENSE("GPL"); |