2 * Copyright (c) 2013 Xilinx, Michal Simek
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <linux/list.h>
30 static LIST_HEAD(gpio_list);
33 GPIO_DIRECTION_OUT = 0,
34 GPIO_DIRECTION_IN = 1,
43 #define GPIO_NAME_SIZE 10
46 char name[GPIO_NAME_SIZE];
49 /* Initialized, rxbd_current, rx_first_buf must be 0 after init */
50 struct xilinx_gpio_priv {
51 struct gpio_regs *regs;
55 char name[GPIO_NAME_SIZE];
56 struct list_head list;
57 struct gpio_names *gpio_name;
60 /* Store number of allocated gpio pins */
61 static u32 xilinx_gpio_max;
63 /* Get associated gpio controller */
64 static struct xilinx_gpio_priv *gpio_get_controller(unsigned gpio)
66 struct list_head *entry;
67 struct xilinx_gpio_priv *priv = NULL;
69 list_for_each(entry, &gpio_list) {
70 priv = list_entry(entry, struct xilinx_gpio_priv, list);
71 if (gpio >= priv->gpio_min && gpio <= priv->gpio_max) {
72 debug("%s: reg: %x, min-max: %d-%d\n", __func__,
73 (u32)priv->regs, priv->gpio_min, priv->gpio_max);
77 puts("!!!Can't get gpio controller!!!\n");
81 /* Get gpio pin name if used/setup */
82 static char *get_name(unsigned gpio)
85 struct xilinx_gpio_priv *priv;
87 debug("%s\n", __func__);
89 priv = gpio_get_controller(gpio);
91 gpio_priv = gpio - priv->gpio_min;
93 return *priv->gpio_name[gpio_priv].name ?
94 priv->gpio_name[gpio_priv].name : "UNKNOWN";
99 /* Get output value */
100 static int gpio_get_output_value(unsigned gpio)
103 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
106 gpio_priv = gpio - priv->gpio_min;
107 val = !!(priv->gpiodata_store & (1 << gpio_priv));
108 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
109 (u32)priv->regs, gpio_priv, val);
116 /* Get input value */
117 static int gpio_get_input_value(unsigned gpio)
120 struct gpio_regs *regs;
121 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
125 gpio_priv = gpio - priv->gpio_min;
126 val = readl(®s->gpiodata);
127 val = !!(val & (1 << gpio_priv));
128 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
129 (u32)priv->regs, gpio_priv, val);
136 /* Set gpio direction */
137 static int gpio_set_direction(unsigned gpio, enum gpio_direction direction)
140 struct gpio_regs *regs;
141 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
145 val = readl(®s->gpiodir);
147 gpio_priv = gpio - priv->gpio_min;
148 if (direction == GPIO_DIRECTION_OUT)
149 val &= ~(1 << gpio_priv);
151 val |= 1 << gpio_priv;
153 writel(val, ®s->gpiodir);
154 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
155 (u32)priv->regs, gpio_priv, val);
163 /* Get gpio direction */
164 static int gpio_get_direction(unsigned gpio)
167 struct gpio_regs *regs;
168 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
172 gpio_priv = gpio - priv->gpio_min;
173 val = readl(®s->gpiodir);
174 val = !!(val & (1 << gpio_priv));
175 debug("%s: reg: %x, gpio_no: %d, dir: %d\n", __func__,
176 (u32)priv->regs, gpio_priv, val);
186 * for example gpio setup to output only can't get input value
187 * which is breaking gpio toggle command
189 int gpio_get_value(unsigned gpio)
193 if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
194 val = gpio_get_output_value(gpio);
196 val = gpio_get_input_value(gpio);
201 /* Set output value */
202 static int gpio_set_output_value(unsigned gpio, int value)
205 struct gpio_regs *regs;
206 struct xilinx_gpio_priv *priv = gpio_get_controller(gpio);
210 gpio_priv = gpio - priv->gpio_min;
211 val = priv->gpiodata_store;
213 val |= 1 << gpio_priv;
215 val &= ~(1 << gpio_priv);
217 writel(val, ®s->gpiodata);
218 debug("%s: reg: %x, gpio_no: %d, output_val: %d\n", __func__,
219 (u32)priv->regs, gpio_priv, val);
220 priv->gpiodata_store = val;
228 int gpio_set_value(unsigned gpio, int value)
230 if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
231 return gpio_set_output_value(gpio, value);
236 /* Set GPIO as input */
237 int gpio_direction_input(unsigned gpio)
239 debug("%s\n", __func__);
240 return gpio_set_direction(gpio, GPIO_DIRECTION_IN);
243 /* Setup GPIO as output and set output value */
244 int gpio_direction_output(unsigned gpio, int value)
246 int ret = gpio_set_direction(gpio, GPIO_DIRECTION_OUT);
248 debug("%s\n", __func__);
253 return gpio_set_output_value(gpio, value);
256 /* Show gpio status */
261 struct list_head *entry;
262 struct xilinx_gpio_priv *priv = NULL;
264 list_for_each(entry, &gpio_list) {
265 priv = list_entry(entry, struct xilinx_gpio_priv, list);
266 printf("\n%s: %s/%x (%d-%d)\n", __func__, priv->name,
267 (u32)priv->regs, priv->gpio_min, priv->gpio_max);
269 for (gpio = priv->gpio_min; gpio <= priv->gpio_max; gpio++) {
270 printf("GPIO_%d:\t%s is an ", gpio, get_name(gpio));
271 if (gpio_get_direction(gpio) == GPIO_DIRECTION_OUT)
272 printf("OUTPUT value = %d\n",
273 gpio_get_output_value(gpio));
275 printf("INPUT value = %d\n",
276 gpio_get_input_value(gpio));
281 int gpio_request(unsigned gpio, const char *label)
284 struct xilinx_gpio_priv *priv;
286 if (gpio >= xilinx_gpio_max)
289 priv = gpio_get_controller(gpio);
291 gpio_priv = gpio - priv->gpio_min;
294 strncpy(priv->gpio_name[gpio_priv].name, label,
296 priv->gpio_name[gpio_priv].name[GPIO_NAME_SIZE - 1] =
305 int gpio_free(unsigned gpio)
308 struct xilinx_gpio_priv *priv;
310 if (gpio >= xilinx_gpio_max)
313 priv = gpio_get_controller(gpio);
315 gpio_priv = gpio - priv->gpio_min;
316 priv->gpio_name[gpio_priv].name[0] = '\0';
318 /* Do nothing here */
325 int gpio_alloc(u32 baseaddr, const char *name, u32 gpio_no)
327 struct xilinx_gpio_priv *priv;
329 priv = calloc(1, sizeof(struct xilinx_gpio_priv));
331 /* Setup gpio name */
333 strncpy(priv->name, name, GPIO_NAME_SIZE);
334 priv->name[GPIO_NAME_SIZE - 1] = '\0';
336 priv->regs = (struct gpio_regs *)baseaddr;
338 priv->gpio_min = xilinx_gpio_max;
339 xilinx_gpio_max = priv->gpio_min + gpio_no;
340 priv->gpio_max = xilinx_gpio_max - 1;
342 priv->gpio_name = calloc(gpio_no, sizeof(struct gpio_names));
344 INIT_LIST_HEAD(&priv->list);
345 list_add_tail(&priv->list, &gpio_list);
347 printf("%s: Add %s (%d-%d)\n", __func__, name,
348 priv->gpio_min, priv->gpio_max);
350 /* Return the first gpio allocated for this device */
351 return priv->gpio_min;
354 /* Dual channel gpio is one IP with two independent channels */
355 int gpio_alloc_dual(u32 baseaddr, const char *name, u32 gpio_no0, u32 gpio_no1)
359 ret = gpio_alloc(baseaddr, name, gpio_no0);
360 gpio_alloc(baseaddr + 8, strcat((char *)name, "_1"), gpio_no1);
362 /* Return the first gpio allocated for this device */