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