1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kontron PLD GPIO driver
5 * Copyright (c) 2010-2013 Kontron Europe GmbH
6 * Author: Michael Brunner <michael.brunner@kontron.com>
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>
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
25 struct kempld_gpio_data {
26 struct gpio_chip chip;
27 struct kempld_device_data *pld;
31 * Set or clear GPIO bit
32 * kempld_get_mutex must be called prior to calling this function.
34 static void kempld_gpio_bitop(struct kempld_device_data *pld,
35 u8 reg, u8 bit, u8 val)
39 status = kempld_read8(pld, reg);
41 status |= KEMPLD_GPIO_MASK(bit);
43 status &= ~KEMPLD_GPIO_MASK(bit);
44 kempld_write8(pld, reg, status);
47 static int kempld_gpio_get_bit(struct kempld_device_data *pld, u8 reg, u8 bit)
51 kempld_get_mutex(pld);
52 status = kempld_read8(pld, reg);
53 kempld_release_mutex(pld);
55 return !!(status & KEMPLD_GPIO_MASK(bit));
58 static int kempld_gpio_get(struct gpio_chip *chip, unsigned offset)
60 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
61 struct kempld_device_data *pld = gpio->pld;
63 return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset), offset);
66 static void kempld_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
68 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
69 struct kempld_device_data *pld = gpio->pld;
71 kempld_get_mutex(pld);
72 kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
73 kempld_release_mutex(pld);
76 static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
78 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
79 struct kempld_device_data *pld = gpio->pld;
81 kempld_get_mutex(pld);
82 kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0);
83 kempld_release_mutex(pld);
88 static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
91 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
92 struct kempld_device_data *pld = gpio->pld;
94 kempld_get_mutex(pld);
95 kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
96 kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1);
97 kempld_release_mutex(pld);
102 static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
104 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
105 struct kempld_device_data *pld = gpio->pld;
107 if (kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset))
108 return GPIO_LINE_DIRECTION_OUT;
110 return GPIO_LINE_DIRECTION_IN;
113 static int kempld_gpio_pincount(struct kempld_device_data *pld)
117 kempld_get_mutex(pld);
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);
128 kempld_release_mutex(pld);
130 return evt ? __ffs(evt) : 16;
133 static int kempld_gpio_probe(struct platform_device *pdev)
135 struct device *dev = &pdev->dev;
136 struct kempld_device_data *pld = dev_get_drvdata(dev->parent);
137 struct kempld_platform_data *pdata = dev_get_platdata(pld->dev);
138 struct kempld_gpio_data *gpio;
139 struct gpio_chip *chip;
142 if (pld->info.spec_major < 2) {
144 "Driver only supports GPIO devices compatible to PLD spec. rev. 2.0 or higher\n");
148 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
154 platform_set_drvdata(pdev, gpio);
157 chip->label = "gpio-kempld";
158 chip->owner = THIS_MODULE;
160 chip->can_sleep = true;
161 if (pdata && pdata->gpio_base)
162 chip->base = pdata->gpio_base;
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");
176 ret = devm_gpiochip_add_data(dev, chip, gpio);
178 dev_err(dev, "Could not register GPIO chip\n");
182 dev_info(dev, "GPIO functionality initialized with %d pins\n",
188 static struct platform_driver kempld_gpio_driver = {
190 .name = "kempld-gpio",
192 .probe = kempld_gpio_probe,
195 module_platform_driver(kempld_gpio_driver);
197 MODULE_DESCRIPTION("KEM PLD GPIO Driver");
198 MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
199 MODULE_LICENSE("GPL");
200 MODULE_ALIAS("platform:kempld-gpio");