| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Kontron PLD GPIO driver |
| 4 |
* |
| 5 |
* Copyright (c) 2010-2013 Kontron Europe GmbH |
| 6 |
* Author: Michael Brunner <[email protected]> |
| 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> |
| 15 |
#include <linux/gpio/driver.h> |
| 16 |
#include <linux/mfd/kempld.h> |
| 17 |
|
| 18 |
#define KEMPLD_GPIO_MAX_NUM 16 |
| 19 |
#define KEMPLD_GPIO_MASK(x) (BIT((x) % 8)) |
| 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) |
| 41 |
status |= KEMPLD_GPIO_MASK(bit); |
| 42 |
else |
| 43 |
status &= ~KEMPLD_GPIO_MASK(bit); |
| 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 |
|
| 55 |
return !!(status & KEMPLD_GPIO_MASK(bit)); |
| 56 |
} |
| 57 |
|
| 58 |
static int kempld_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 59 |
{ |
| 60 |
struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
| 61 |
struct kempld_device_data *pld = gpio->pld; |
| 62 |
|
| 63 |
return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset), offset); |
| 64 |
} |
| 65 |
|
| 66 |
static int kempld_gpio_set(struct gpio_chip *chip, unsigned int offset, |
| 67 |
int value) |
| 68 |
{ |
| 69 |
struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
| 70 |
struct kempld_device_data *pld = gpio->pld; |
| 71 |
|
| 72 |
kempld_get_mutex(pld); |
| 73 |
kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value); |
| 74 |
kempld_release_mutex(pld); |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 80 |
{ |
| 81 |
struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
| 82 |
struct kempld_device_data *pld = gpio->pld; |
| 83 |
|
| 84 |
kempld_get_mutex(pld); |
| 85 |
kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0); |
| 86 |
kempld_release_mutex(pld); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 92 |
int value) |
| 93 |
{ |
| 94 |
struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
| 95 |
struct kempld_device_data *pld = gpio->pld; |
| 96 |
|
| 97 |
kempld_get_mutex(pld); |
| 98 |
kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value); |
| 99 |
kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1); |
| 100 |
kempld_release_mutex(pld); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 106 |
{ |
| 107 |
struct kempld_gpio_data *gpio = gpiochip_get_data(chip); |
| 108 |
struct kempld_device_data *pld = gpio->pld; |
| 109 |
|
| 110 |
if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset)) |
| 111 |
return GPIO_LINE_DIRECTION_OUT; |
| 112 |
|
| 113 |
return GPIO_LINE_DIRECTION_IN; |
| 114 |
} |
| 115 |
|
| 116 |
static int kempld_gpio_pincount(struct kempld_device_data *pld) |
| 117 |
{ |
| 118 |
u16 evt, evt_back; |
| 119 |
|
| 120 |
kempld_get_mutex(pld); |
| 121 |
|
| 122 |
/* Backup event register as it might be already initialized */ |
| 123 |
evt_back = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE); |
| 124 |
/* Clear event register */ |
| 125 |
kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, 0x0000); |
| 126 |
/* Read back event register */ |
| 127 |
evt = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE); |
| 128 |
/* Restore event register */ |
| 129 |
kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, evt_back); |
| 130 |
|
| 131 |
kempld_release_mutex(pld); |
| 132 |
|
| 133 |
return evt ? __ffs(evt) : 16; |
| 134 |
} |
| 135 |
|
| 136 |
static int kempld_gpio_probe(struct platform_device *pdev) |
| 137 |
{ |
| 138 |
struct device *dev = &pdev->dev; |
| 139 |
struct kempld_device_data *pld = dev_get_drvdata(dev->parent); |
| 140 |
struct kempld_platform_data *pdata = dev_get_platdata(pld->dev); |
| 141 |
struct kempld_gpio_data *gpio; |
| 142 |
struct gpio_chip *chip; |
| 143 |
int ret; |
| 144 |
|
| 145 |
if (pld->info.spec_major < 2) { |
| 146 |
dev_err(dev, |
| 147 |
"Driver only supports GPIO devices compatible to PLD spec. rev. 2.0 or higher\n"); |
| 148 |
return -ENODEV; |
| 149 |
} |
| 150 |
|
| 151 |
gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL); |
| 152 |
if (!gpio) |
| 153 |
return -ENOMEM; |
| 154 |
|
| 155 |
gpio->pld = pld; |
| 156 |
|
| 157 |
platform_set_drvdata(pdev, gpio); |
| 158 |
|
| 159 |
chip = &gpio->chip; |
| 160 |
chip->label = "gpio-kempld"; |
| 161 |
chip->owner = THIS_MODULE; |
| 162 |
chip->parent = dev; |
| 163 |
chip->can_sleep = true; |
| 164 |
if (pdata && pdata->gpio_base) |
| 165 |
chip->base = pdata->gpio_base; |
| 166 |
else |
| 167 |
chip->base = -1; |
| 168 |
chip->direction_input = kempld_gpio_direction_input; |
| 169 |
chip->direction_output = kempld_gpio_direction_output; |
| 170 |
chip->get_direction = kempld_gpio_get_direction; |
| 171 |
chip->get = kempld_gpio_get; |
| 172 |
chip->set = kempld_gpio_set; |
| 173 |
chip->ngpio = kempld_gpio_pincount(pld); |
| 174 |
if (chip->ngpio == 0) { |
| 175 |
dev_err(dev, "No GPIO pins detected\n"); |
| 176 |
return -ENODEV; |
| 177 |
} |
| 178 |
|
| 179 |
ret = devm_gpiochip_add_data(dev, chip, gpio); |
| 180 |
if (ret) { |
| 181 |
dev_err(dev, "Could not register GPIO chip\n"); |
| 182 |
return ret; |
| 183 |
} |
| 184 |
|
| 185 |
dev_info(dev, "GPIO functionality initialized with %d pins\n", |
| 186 |
chip->ngpio); |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
static struct platform_driver kempld_gpio_driver = { |
| 192 |
.driver = { |
| 193 |
.name = "kempld-gpio", |
| 194 |
}, |
| 195 |
.probe = kempld_gpio_probe, |
| 196 |
}; |
| 197 |
|
| 198 |
module_platform_driver(kempld_gpio_driver); |
| 199 |
|
| 200 |
MODULE_DESCRIPTION("KEM PLD GPIO Driver"); |
| 201 |
MODULE_AUTHOR("Michael Brunner <[email protected]>"); |
| 202 |
MODULE_LICENSE("GPL"); |
| 203 |
MODULE_ALIAS("platform:kempld-gpio"); |