2 * ADI GPIO2 Abstraction Layer
3 * Support BF54x, BF60x and future processors.
5 * Copyright 2008-2013 Analog Devices Inc.
7 * Licensed under the GPL-2 or later
12 #include <linux/bug.h>
13 #include <linux/errno.h>
16 #define RESOURCE_LABEL_SIZE 16
18 static struct str_ident {
19 char name[RESOURCE_LABEL_SIZE];
20 } str_ident[MAX_RESOURCES];
22 static void gpio_error(unsigned gpio)
24 printf("adi_gpio2: GPIO %d wasn't requested!\n", gpio);
27 static void set_label(unsigned short ident, const char *label)
30 strncpy(str_ident[ident].name, label,
32 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
36 static char *get_label(unsigned short ident)
38 return *str_ident[ident].name ? str_ident[ident].name : "UNKNOWN";
41 static int cmp_label(unsigned short ident, const char *label)
44 printf("adi_gpio2: please provide none-null label\n");
47 return strcmp(str_ident[ident].name, label);
52 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
53 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
54 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
55 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
56 #define DECLARE_RESERVED_MAP(m, c) unsigned short reserved_##m##_map[c]
58 static DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
59 static DECLARE_RESERVED_MAP(peri, gpio_bank(MAX_RESOURCES));
61 inline int check_gpio(unsigned gpio)
63 #if defined(CONFIG_BF54x)
64 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15 ||
65 gpio == GPIO_PH14 || gpio == GPIO_PH15 ||
66 gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
69 if (gpio >= MAX_GPIOS)
74 static void port_setup(unsigned gpio, unsigned short usage)
76 #if defined(CONFIG_BF54x)
77 if (usage == GPIO_USAGE)
78 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
80 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
82 if (usage == GPIO_USAGE)
83 gpio_array[gpio_bank(gpio)]->port_fer_clear = gpio_bit(gpio);
85 gpio_array[gpio_bank(gpio)]->port_fer_set = gpio_bit(gpio);
89 inline void portmux_setup(unsigned short per)
92 u16 ident = P_IDENT(per);
93 u16 function = P_FUNCT2MUX(per);
95 pmux = gpio_array[gpio_bank(ident)]->port_mux;
97 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
98 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
100 gpio_array[gpio_bank(ident)]->port_mux = pmux;
103 inline u16 get_portmux(unsigned short per)
106 u16 ident = P_IDENT(per);
108 pmux = gpio_array[gpio_bank(ident)]->port_mux;
110 return pmux >> (2 * gpio_sub_n(ident)) & 0x3;
113 unsigned short get_gpio_dir(unsigned gpio)
116 (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio));
119 /***********************************************************
121 * FUNCTIONS: Peripheral Resource Allocation
125 * per Peripheral Identifier
128 * DESCRIPTION: Peripheral Resource Allocation and Setup API
129 **************************************************************/
131 int peripheral_request(unsigned short per, const char *label)
133 unsigned short ident = P_IDENT(per);
136 * Don't cares are pins with only one dedicated function
139 if (per & P_DONTCARE)
142 if (!(per & P_DEFINED))
145 BUG_ON(ident >= MAX_RESOURCES);
147 /* If a pin can be muxed as either GPIO or peripheral, make
148 * sure it is not already a GPIO pin when we request it.
150 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
151 printf("%s: Peripheral %d is already reserved as GPIO by %s!\n",
152 __func__, ident, get_label(ident));
156 if (unlikely(is_reserved(peri, ident, 1))) {
158 * Pin functions like AMC address strobes my
159 * be requested and used by several drivers
162 if (!((per & P_MAYSHARE) &&
163 get_portmux(per) == P_FUNCT2MUX(per))) {
165 * Allow that the identical pin function can
166 * be requested from the same driver twice
169 if (cmp_label(ident, label) == 0)
172 printf("%s: Peripheral %d function %d is already "
173 "reserved by %s!\n", __func__, ident,
174 P_FUNCT2MUX(per), get_label(ident));
180 reserve(peri, ident);
183 port_setup(ident, PERIPHERAL_USAGE);
185 set_label(ident, label);
190 int peripheral_request_list(const unsigned short per[], const char *label)
195 for (cnt = 0; per[cnt] != 0; cnt++) {
196 ret = peripheral_request(per[cnt], label);
199 for (; cnt > 0; cnt--)
200 peripheral_free(per[cnt - 1]);
209 void peripheral_free(unsigned short per)
211 unsigned short ident = P_IDENT(per);
213 if (per & P_DONTCARE)
216 if (!(per & P_DEFINED))
219 if (unlikely(!is_reserved(peri, ident, 0)))
222 if (!(per & P_MAYSHARE))
223 port_setup(ident, GPIO_USAGE);
225 unreserve(peri, ident);
227 set_label(ident, "free");
230 void peripheral_free_list(const unsigned short per[])
233 for (cnt = 0; per[cnt] != 0; cnt++)
234 peripheral_free(per[cnt]);
237 /***********************************************************
239 * FUNCTIONS: GPIO Driver
242 * gpio PIO Number between 0 and MAX_GPIOS
245 * DESCRIPTION: GPIO Driver API
246 **************************************************************/
248 int gpio_request(unsigned gpio, const char *label)
250 if (check_gpio(gpio) < 0)
254 * Allow that the identical GPIO can
255 * be requested from the same driver twice
256 * Do nothing and return -
259 if (cmp_label(gpio, label) == 0)
262 if (unlikely(is_reserved(gpio, gpio, 1))) {
263 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
264 gpio, get_label(gpio));
267 if (unlikely(is_reserved(peri, gpio, 1))) {
268 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
269 "by %s!\n", gpio, get_label(gpio));
274 set_label(gpio, label);
276 port_setup(gpio, GPIO_USAGE);
281 int gpio_free(unsigned gpio)
283 if (check_gpio(gpio) < 0)
286 if (unlikely(!is_reserved(gpio, gpio, 0))) {
291 unreserve(gpio, gpio);
293 set_label(gpio, "free");
298 #ifdef ADI_SPECIAL_GPIO_BANKS
299 static DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
301 int special_gpio_request(unsigned gpio, const char *label)
304 * Allow that the identical GPIO can
305 * be requested from the same driver twice
306 * Do nothing and return -
309 if (cmp_label(gpio, label) == 0)
312 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
313 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
314 gpio, get_label(gpio));
317 if (unlikely(is_reserved(peri, gpio, 1))) {
318 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
319 "by %s!\n", gpio, get_label(gpio));
324 reserve(special_gpio, gpio);
327 set_label(gpio, label);
328 port_setup(gpio, GPIO_USAGE);
333 void special_gpio_free(unsigned gpio)
335 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
340 unreserve(special_gpio, gpio);
341 unreserve(peri, gpio);
342 set_label(gpio, "free");
346 static inline void __gpio_direction_input(unsigned gpio)
348 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
349 #if defined(CONFIG_BF54x)
350 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
352 gpio_array[gpio_bank(gpio)]->inen_set = gpio_bit(gpio);
356 int gpio_direction_input(unsigned gpio)
360 if (!is_reserved(gpio, gpio, 0)) {
365 local_irq_save(flags);
366 __gpio_direction_input(gpio);
367 local_irq_restore(flags);
372 int gpio_set_value(unsigned gpio, int arg)
375 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
377 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
382 int gpio_direction_output(unsigned gpio, int value)
386 if (!is_reserved(gpio, gpio, 0)) {
391 local_irq_save(flags);
393 #if defined(CONFIG_BF54x)
394 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
396 gpio_array[gpio_bank(gpio)]->inen_clear = gpio_bit(gpio);
398 gpio_set_value(gpio, value);
399 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
401 local_irq_restore(flags);
406 int gpio_get_value(unsigned gpio)
408 return 1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
411 void gpio_labels(void)
415 for (c = 0; c < MAX_RESOURCES; c++) {
416 gpio = is_reserved(gpio, c, 1);
417 if (!check_gpio(c) && gpio)
418 printf("GPIO_%d:\t%s\tGPIO %s\n", c, get_label(c),
419 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
420 else if (is_reserved(peri, c, 1))
421 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));