WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpio / gpio-litex.c
blob70e05249c23bf48a8de848cca3a677db57610f4d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 Antmicro <www.antmicro.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/gpio.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/bits.h>
26 #include <linux/errno.h>
27 #include <linux/litex.h>
29 #define GPIO_PINS_MAX 32
31 struct litex_gpio {
32 void __iomem *membase;
33 int port_direction;
34 int reg_span;
35 struct gpio_chip chip;
38 /* API functions */
40 static int litex_gpio_get_value(struct gpio_chip *chip, unsigned int offset)
42 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
43 u32 regv;
45 if (offset >= chip->ngpio)
46 return -EINVAL;
48 regv = _litex_get_reg(gpio_s->membase, gpio_s->reg_span);
49 return !!(regv & BIT(offset));
52 static int litex_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
53 unsigned long *bits)
55 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
56 u32 regv;
58 if (*mask >= (1 << chip->ngpio))
59 return -EINVAL;
61 regv = _litex_get_reg(gpio_s->membase, gpio_s->reg_span);
62 *bits = (regv & *mask);
63 return 0;
66 static void litex_gpio_set_value(struct gpio_chip *chip, unsigned int offset,
67 int val)
69 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
70 u32 regv, new_regv;
72 if (offset >= chip->ngpio)
73 return;
75 regv = _litex_get_reg(gpio_s->membase, gpio_s->reg_span);
76 new_regv = (regv & ~BIT(offset)) | (!!val << offset);
77 _litex_set_reg(gpio_s->membase, gpio_s->reg_span, new_regv);
80 static void litex_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
81 unsigned long *bits)
83 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
84 u32 regv, new_regv;
86 if (*mask >= (1 << chip->ngpio))
87 return;
89 regv = _litex_get_reg(gpio_s->membase, gpio_s->reg_span);
90 new_regv = (regv & ~(*mask)) | (*bits);
91 _litex_set_reg(gpio_s->membase, gpio_s->reg_span, new_regv);
94 static int litex_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
96 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
98 return gpio_s->port_direction;
101 static int litex_gpio_direction_input(struct gpio_chip *chip,
102 unsigned int offset)
104 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
106 if (gpio_s->port_direction != GPIOF_DIR_IN)
107 return -ENOTSUPP;
108 else
109 return 0;
112 static int litex_gpio_direction_output(struct gpio_chip *chip,
113 unsigned int offset, int value)
115 struct litex_gpio *gpio_s = gpiochip_get_data(chip);
117 if (gpio_s->port_direction != GPIOF_DIR_OUT)
118 return -ENOTSUPP;
119 else
120 return 0;
123 /* Driver functions */
125 static int litex_gpio_probe(struct platform_device *pdev)
127 struct device_node *node = pdev->dev.of_node;
128 struct litex_gpio *gpio_s;
129 struct resource *res;
130 int ret_i;
132 int dt_ngpio;
133 const char *dt_direction;
135 if (!node)
136 return -ENODEV;
138 gpio_s = devm_kzalloc(&pdev->dev, sizeof(*gpio_s), GFP_KERNEL);
139 if (!gpio_s)
140 return -ENOMEM;
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 if (!res)
144 return -EBUSY;
146 gpio_s->membase = devm_of_iomap(&pdev->dev, node, 0, &res->end);
147 if (IS_ERR_OR_NULL(gpio_s->membase))
148 return -EIO;
150 ret_i = of_property_read_u32(node, "litex,ngpio", &dt_ngpio);
151 if (ret_i < 0) {
152 dev_err(&pdev->dev, "No litex,ngpio entry in the dts file\n");
153 return -ENODEV;
155 if (dt_ngpio >= GPIO_PINS_MAX) {
156 dev_err(&pdev->dev,
157 "LiteX GPIO driver cannot use more than %d pins\n",
158 GPIO_PINS_MAX);
159 return -EINVAL;
162 ret_i = of_property_read_string(node, "litex,direction",
163 &dt_direction);
164 if (ret_i < 0) {
165 dev_err(&pdev->dev, "No litex,direction entry in the dts file\n");
166 return -ENODEV;
169 if (!strcmp(dt_direction, "in"))
170 gpio_s->port_direction = GPIOF_DIR_IN;
171 else if (!strcmp(dt_direction, "out"))
172 gpio_s->port_direction = GPIOF_DIR_OUT;
173 else
174 return -ENODEV;
176 /* Assign API functions */
178 gpio_s->chip.label = "litex_gpio";
179 gpio_s->chip.owner = THIS_MODULE;
180 gpio_s->chip.get = litex_gpio_get_value;
181 gpio_s->chip.get_multiple = litex_gpio_get_multiple;
182 gpio_s->chip.set = litex_gpio_set_value;
183 gpio_s->chip.set_multiple = litex_gpio_set_multiple;
184 gpio_s->chip.get_direction = litex_gpio_get_direction;
185 gpio_s->chip.direction_input = litex_gpio_direction_input;
186 gpio_s->chip.direction_output = litex_gpio_direction_output;
187 gpio_s->chip.parent = &pdev->dev;
188 gpio_s->chip.base = -1;
189 gpio_s->chip.ngpio = dt_ngpio;
190 gpio_s->chip.can_sleep = false;
192 gpio_s->reg_span = (dt_ngpio + LITEX_SUBREG_SIZE_BIT - 1) /
193 LITEX_SUBREG_SIZE_BIT;
195 platform_set_drvdata(pdev, gpio_s);
196 return devm_gpiochip_add_data(&pdev->dev, &gpio_s->chip, gpio_s);
199 static const struct of_device_id litex_of_match[] = {
200 {.compatible = "litex,gpio"},
204 MODULE_DEVICE_TABLE(of, litex_of_match);
206 static struct platform_driver litex_gpio_driver = {
207 .driver = {
208 .name = "litex-gpio",
209 .of_match_table = of_match_ptr(litex_of_match)
211 .probe = litex_gpio_probe,
214 module_platform_driver(litex_gpio_driver);
216 MODULE_DESCRIPTION("LiteX gpio driver");
217 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
218 MODULE_LICENSE("GPL v2");