blob: ef51638f3f75937b5d0545c064c228b0e6c88e4a [file] [log] [blame]
Thomas Gleixner9f806852019-05-29 07:18:08 -07001// SPDX-License-Identifier: GPL-2.0-only
Guenter Roeckd22fcde2013-06-23 21:00:05 -07002/*
3 * Kontron PLD GPIO driver
4 *
5 * Copyright (c) 2010-2013 Kontron Europe GmbH
6 * Author: Michael Brunner <michael.brunner@kontron.com>
Guenter Roeckd22fcde2013-06-23 21:00:05 -07007 */
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 Walleij430f6492018-03-12 12:16:34 +010015#include <linux/gpio/driver.h>
Guenter Roeckd22fcde2013-06-23 21:00:05 -070016#include <linux/mfd/kempld.h>
17
18#define KEMPLD_GPIO_MAX_NUM 16
Javier Martinez Canillas459b8182014-04-27 02:00:48 +020019#define KEMPLD_GPIO_MASK(x) (BIT((x) % 8))
Guenter Roeckd22fcde2013-06-23 21:00:05 -070020#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
25struct 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 */
34static 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 Michael2e7f6122013-07-31 20:55:39 +020041 status |= KEMPLD_GPIO_MASK(bit);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070042 else
Brunner Michael2e7f6122013-07-31 20:55:39 +020043 status &= ~KEMPLD_GPIO_MASK(bit);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070044 kempld_write8(pld, reg, status);
45}
46
47static 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 Michael2e7f6122013-07-31 20:55:39 +020055 return !!(status & KEMPLD_GPIO_MASK(bit));
Guenter Roeckd22fcde2013-06-23 21:00:05 -070056}
57
58static int kempld_gpio_get(struct gpio_chip *chip, unsigned offset)
59{
Linus Walleij1f89bcc2015-12-07 09:06:22 +010060 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070061 struct kempld_device_data *pld = gpio->pld;
62
Linus Walleije9639432015-12-21 11:04:26 +010063 return !!kempld_gpio_get_bit(pld, KEMPLD_GPIO_LVL_NUM(offset), offset);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070064}
65
66static void kempld_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
67{
Linus Walleij1f89bcc2015-12-07 09:06:22 +010068 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070069 struct kempld_device_data *pld = gpio->pld;
70
71 kempld_get_mutex(pld);
Brunner Michael2e7f6122013-07-31 20:55:39 +020072 kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070073 kempld_release_mutex(pld);
74}
75
76static int kempld_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
77{
Linus Walleij1f89bcc2015-12-07 09:06:22 +010078 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070079 struct kempld_device_data *pld = gpio->pld;
80
81 kempld_get_mutex(pld);
Brunner Michael2e7f6122013-07-31 20:55:39 +020082 kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 0);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070083 kempld_release_mutex(pld);
84
85 return 0;
86}
87
88static int kempld_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
89 int value)
90{
Linus Walleij1f89bcc2015-12-07 09:06:22 +010091 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070092 struct kempld_device_data *pld = gpio->pld;
93
94 kempld_get_mutex(pld);
Brunner Michael2e7f6122013-07-31 20:55:39 +020095 kempld_gpio_bitop(pld, KEMPLD_GPIO_LVL_NUM(offset), offset, value);
96 kempld_gpio_bitop(pld, KEMPLD_GPIO_DIR_NUM(offset), offset, 1);
Guenter Roeckd22fcde2013-06-23 21:00:05 -070097 kempld_release_mutex(pld);
98
99 return 0;
100}
101
102static int kempld_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
103{
Linus Walleij1f89bcc2015-12-07 09:06:22 +0100104 struct kempld_gpio_data *gpio = gpiochip_get_data(chip);
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700105 struct kempld_device_data *pld = gpio->pld;
106
Michael Brunnerf230e8f2015-05-11 12:46:49 +0200107 return !kempld_gpio_get_bit(pld, KEMPLD_GPIO_DIR_NUM(offset), offset);
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700108}
109
110static int kempld_gpio_pincount(struct kempld_device_data *pld)
111{
112 u16 evt, evt_back;
113
114 kempld_get_mutex(pld);
115
116 /* Backup event register as it might be already initialized */
117 evt_back = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE);
118 /* Clear event register */
119 kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, 0x0000);
120 /* Read back event register */
121 evt = kempld_read16(pld, KEMPLD_GPIO_EVT_LVL_EDGE);
122 /* Restore event register */
123 kempld_write16(pld, KEMPLD_GPIO_EVT_LVL_EDGE, evt_back);
124
125 kempld_release_mutex(pld);
126
127 return evt ? __ffs(evt) : 16;
128}
129
130static int kempld_gpio_probe(struct platform_device *pdev)
131{
132 struct device *dev = &pdev->dev;
133 struct kempld_device_data *pld = dev_get_drvdata(dev->parent);
Jingoo Hane56aee12013-07-30 17:08:05 +0900134 struct kempld_platform_data *pdata = dev_get_platdata(pld->dev);
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700135 struct kempld_gpio_data *gpio;
136 struct gpio_chip *chip;
137 int ret;
138
139 if (pld->info.spec_major < 2) {
140 dev_err(dev,
141 "Driver only supports GPIO devices compatible to PLD spec. rev. 2.0 or higher\n");
142 return -ENODEV;
143 }
144
145 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
Varka Bhadramafeb7b42015-03-31 09:49:11 +0530146 if (!gpio)
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700147 return -ENOMEM;
148
149 gpio->pld = pld;
150
151 platform_set_drvdata(pdev, gpio);
152
153 chip = &gpio->chip;
154 chip->label = "gpio-kempld";
155 chip->owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +0100156 chip->parent = dev;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100157 chip->can_sleep = true;
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700158 if (pdata && pdata->gpio_base)
159 chip->base = pdata->gpio_base;
160 else
161 chip->base = -1;
162 chip->direction_input = kempld_gpio_direction_input;
163 chip->direction_output = kempld_gpio_direction_output;
164 chip->get_direction = kempld_gpio_get_direction;
165 chip->get = kempld_gpio_get;
166 chip->set = kempld_gpio_set;
167 chip->ngpio = kempld_gpio_pincount(pld);
168 if (chip->ngpio == 0) {
169 dev_err(dev, "No GPIO pins detected\n");
170 return -ENODEV;
171 }
172
Laxman Dewangan7b697b32016-02-22 17:43:28 +0530173 ret = devm_gpiochip_add_data(dev, chip, gpio);
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700174 if (ret) {
175 dev_err(dev, "Could not register GPIO chip\n");
176 return ret;
177 }
178
179 dev_info(dev, "GPIO functionality initialized with %d pins\n",
180 chip->ngpio);
181
182 return 0;
183}
184
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700185static struct platform_driver kempld_gpio_driver = {
186 .driver = {
Michael Brunner81e9df22013-08-09 17:33:06 +0200187 .name = "kempld-gpio",
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700188 },
189 .probe = kempld_gpio_probe,
Guenter Roeckd22fcde2013-06-23 21:00:05 -0700190};
191
192module_platform_driver(kempld_gpio_driver);
193
194MODULE_DESCRIPTION("KEM PLD GPIO Driver");
195MODULE_AUTHOR("Michael Brunner <michael.brunner@kontron.com>");
196MODULE_LICENSE("GPL");
Axel Lin9288eca2014-04-12 12:45:18 +0800197MODULE_ALIAS("platform:kempld-gpio");