Thomas Gleixner | 9f80685 | 2019-05-29 07:18:08 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Kontron PLD GPIO driver |
| 4 | * |
| 5 | * Copyright (c) 2010-2013 Kontron Europe GmbH |
| 6 | * Author: Michael Brunner <michael.brunner@kontron.com> |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/bitops.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/platform_device.h> |
Linus Walleij | 430f649 | 2018-03-12 12:16:34 +0100 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 16 | #include <linux/mfd/kempld.h> |
| 17 | |
| 18 | #define KEMPLD_GPIO_MAX_NUM 16 |
Javier Martinez Canillas | 459b818 | 2014-04-27 02:00:48 +0200 | [diff] [blame] | 19 | #define KEMPLD_GPIO_MASK(x) (BIT((x) % 8)) |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 20 | #define KEMPLD_GPIO_DIR_NUM(x) (0x40 + (x) / 8) |
| 21 | #define KEMPLD_GPIO_LVL_NUM(x) (0x42 + (x) / 8) |
| 22 | #define KEMPLD_GPIO_EVT_LVL_EDGE 0x46 |
| 23 | #define KEMPLD_GPIO_IEN 0x4A |
| 24 | |
| 25 | struct kempld_gpio_data { |
| 26 | struct gpio_chip chip; |
| 27 | struct kempld_device_data *pld; |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * Set or clear GPIO bit |
| 32 | * kempld_get_mutex must be called prior to calling this function. |
| 33 | */ |
| 34 | static void kempld_gpio_bitop(struct kempld_device_data *pld, |
| 35 | u8 reg, u8 bit, u8 val) |
| 36 | { |
| 37 | u8 status; |
| 38 | |
| 39 | status = kempld_read8(pld, reg); |
| 40 | if (val) |
Brunner Michael | 2e7f612 | 2013-07-31 20:55:39 +0200 | [diff] [blame] | 41 | status |= KEMPLD_GPIO_MASK(bit); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 42 | else |
Brunner Michael | 2e7f612 | 2013-07-31 20:55:39 +0200 | [diff] [blame] | 43 | status &= ~KEMPLD_GPIO_MASK(bit); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 44 | kempld_write8(pld, reg, status); |
| 45 | } |
| 46 | |
| 47 | static int kempld_gpio_get_bit(struct kempld_device_data *pld, u8 reg, u8 bit) |
| 48 | { |
| 49 | u8 status; |
| 50 | |
| 51 | kempld_get_mutex(pld); |
| 52 | status = kempld_read8(pld, reg); |
| 53 | kempld_release_mutex(pld); |
| 54 | |
Brunner Michael | 2e7f612 | 2013-07-31 20:55:39 +0200 | [diff] [blame] | 55 | return !!(status & KEMPLD_GPIO_MASK(bit)); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int kempld_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 59 | { |
Linus Walleij | 1f89bcc | 2015-12-07 09:06:22 +0100 | [diff] [blame] | 60 | struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 61 | struct kempld_device_data *pld = gpio->pld; |
| 62 | |
Linus Walleij | e963943 | 2015-12-21 11:04:26 +0100 | [diff] [blame] | 63 | return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset), offset); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void kempld_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 67 | { |
Linus Walleij | 1f89bcc | 2015-12-07 09:06:22 +0100 | [diff] [blame] | 68 | struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 69 | struct kempld_device_data *pld = gpio->pld; |
| 70 | |
| 71 | kempld_get_mutex(pld); |
Brunner Michael | 2e7f612 | 2013-07-31 20:55:39 +0200 | [diff] [blame] | 72 | kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 73 | kempld_release_mutex(pld); |
| 74 | } |
| 75 | |
| 76 | static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 77 | { |
Linus Walleij | 1f89bcc | 2015-12-07 09:06:22 +0100 | [diff] [blame] | 78 | struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 79 | struct kempld_device_data *pld = gpio->pld; |
| 80 | |
| 81 | kempld_get_mutex(pld); |
Brunner Michael | 2e7f612 | 2013-07-31 20:55:39 +0200 | [diff] [blame] | 82 | kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 83 | kempld_release_mutex(pld); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 89 | int value) |
| 90 | { |
Linus Walleij | 1f89bcc | 2015-12-07 09:06:22 +0100 | [diff] [blame] | 91 | struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 92 | struct kempld_device_data *pld = gpio->pld; |
| 93 | |
| 94 | kempld_get_mutex(pld); |
Brunner Michael | 2e7f612 | 2013-07-31 20:55:39 +0200 | [diff] [blame] | 95 | kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value); |
| 96 | kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 97 | kempld_release_mutex(pld); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 103 | { |
Linus Walleij | 1f89bcc | 2015-12-07 09:06:22 +0100 | [diff] [blame] | 104 | struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 105 | struct kempld_device_data *pld = gpio->pld; |
| 106 | |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 107 | if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset)) |
| 108 | return GPIO_LINE_DIRECTION_OUT; |
| 109 | |
| 110 | return GPIO_LINE_DIRECTION_IN; |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int kempld_gpio_pincount(struct kempld_device_data *pld) |
| 114 | { |
| 115 | u16 evt, evt_back; |
| 116 | |
| 117 | kempld_get_mutex(pld); |
| 118 | |
| 119 | /* Backup event register as it might be already initialized */ |
| 120 | evt_back = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE); |
| 121 | /* Clear event register */ |
| 122 | kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, 0x0000); |
| 123 | /* Read back event register */ |
| 124 | evt = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE); |
| 125 | /* Restore event register */ |
| 126 | kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, evt_back); |
| 127 | |
| 128 | kempld_release_mutex(pld); |
| 129 | |
| 130 | return evt ? __ffs(evt) : 16; |
| 131 | } |
| 132 | |
| 133 | static int kempld_gpio_probe(struct platform_device *pdev) |
| 134 | { |
| 135 | struct device *dev = &pdev->dev; |
| 136 | struct kempld_device_data *pld = dev_get_drvdata(dev->parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 137 | struct kempld_platform_data *pdata = dev_get_platdata(pld->dev); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 138 | struct kempld_gpio_data *gpio; |
| 139 | struct gpio_chip *chip; |
| 140 | int ret; |
| 141 | |
| 142 | if (pld->info.spec_major < 2) { |
| 143 | dev_err(dev, |
| 144 | "Driver only supports GPIO devices compatible to PLD spec. rev. 2.0 or higher\n"); |
| 145 | return -ENODEV; |
| 146 | } |
| 147 | |
| 148 | gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL); |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 149 | if (!gpio) |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 150 | return -ENOMEM; |
| 151 | |
| 152 | gpio->pld = pld; |
| 153 | |
| 154 | platform_set_drvdata(pdev, gpio); |
| 155 | |
| 156 | chip = &gpio->chip; |
| 157 | chip->label = "gpio-kempld"; |
| 158 | chip->owner = THIS_MODULE; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 159 | chip->parent = dev; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 160 | chip->can_sleep = true; |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 161 | if (pdata && pdata->gpio_base) |
| 162 | chip->base = pdata->gpio_base; |
| 163 | else |
| 164 | chip->base = -1; |
| 165 | chip->direction_input = kempld_gpio_direction_input; |
| 166 | chip->direction_output = kempld_gpio_direction_output; |
| 167 | chip->get_direction = kempld_gpio_get_direction; |
| 168 | chip->get = kempld_gpio_get; |
| 169 | chip->set = kempld_gpio_set; |
| 170 | chip->ngpio = kempld_gpio_pincount(pld); |
| 171 | if (chip->ngpio == 0) { |
| 172 | dev_err(dev, "No GPIO pins detected\n"); |
| 173 | return -ENODEV; |
| 174 | } |
| 175 | |
Laxman Dewangan | 7b697b3 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 176 | ret = devm_gpiochip_add_data(dev, chip, gpio); |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 177 | if (ret) { |
| 178 | dev_err(dev, "Could not register GPIO chip\n"); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | dev_info(dev, "GPIO functionality initialized with %d pins\n", |
| 183 | chip->ngpio); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 188 | static struct platform_driver kempld_gpio_driver = { |
| 189 | .driver = { |
Michael Brunner | 81e9df2 | 2013-08-09 17:33:06 +0200 | [diff] [blame] | 190 | .name = "kempld-gpio", |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 191 | }, |
| 192 | .probe = kempld_gpio_probe, |
Guenter Roeck | d22fcde | 2013-06-23 21:00:05 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | module_platform_driver(kempld_gpio_driver); |
| 196 | |
| 197 | MODULE_DESCRIPTION("KEM PLD GPIO Driver"); |
| 198 | MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>"); |
| 199 | MODULE_LICENSE("GPL"); |
Axel Lin | 9288eca | 2014-04-12 12:45:18 +0800 | [diff] [blame] | 200 | MODULE_ALIAS("platform:kempld-gpio"); |