1 // SPDX-License-Identifier: GPL-2.0
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
32 void __iomem
*membase
;
35 struct gpio_chip chip
;
40 static int litex_gpio_get_value(struct gpio_chip
*chip
, unsigned int offset
)
42 struct litex_gpio
*gpio_s
= gpiochip_get_data(chip
);
45 if (offset
>= chip
->ngpio
)
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
,
55 struct litex_gpio
*gpio_s
= gpiochip_get_data(chip
);
58 if (*mask
>= (1 << chip
->ngpio
))
61 regv
= _litex_get_reg(gpio_s
->membase
, gpio_s
->reg_span
);
62 *bits
= (regv
& *mask
);
66 static void litex_gpio_set_value(struct gpio_chip
*chip
, unsigned int offset
,
69 struct litex_gpio
*gpio_s
= gpiochip_get_data(chip
);
72 if (offset
>= chip
->ngpio
)
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
,
83 struct litex_gpio
*gpio_s
= gpiochip_get_data(chip
);
86 if (*mask
>= (1 << chip
->ngpio
))
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
,
104 struct litex_gpio
*gpio_s
= gpiochip_get_data(chip
);
106 if (gpio_s
->port_direction
!= GPIOF_DIR_IN
)
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
)
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
;
133 const char *dt_direction
;
138 gpio_s
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio_s
), GFP_KERNEL
);
142 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
146 gpio_s
->membase
= devm_of_iomap(&pdev
->dev
, node
, 0, &res
->end
);
147 if (IS_ERR_OR_NULL(gpio_s
->membase
))
150 ret_i
= of_property_read_u32(node
, "litex,ngpio", &dt_ngpio
);
152 dev_err(&pdev
->dev
, "No litex,ngpio entry in the dts file\n");
155 if (dt_ngpio
>= GPIO_PINS_MAX
) {
157 "LiteX GPIO driver cannot use more than %d pins\n",
162 ret_i
= of_property_read_string(node
, "litex,direction",
165 dev_err(&pdev
->dev
, "No litex,direction entry in the dts file\n");
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
;
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
= {
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");