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/errno.h>
15 #define RESOURCE_LABEL_SIZE 16
17 static struct str_ident {
18 char name[RESOURCE_LABEL_SIZE];
19 } str_ident[MAX_RESOURCES];
21 static void gpio_error(unsigned gpio)
23 printf("adi_gpio2: GPIO %d wasn't requested!\n", gpio);
26 static void set_label(unsigned short ident, const char *label)
29 strncpy(str_ident[ident].name, label,
31 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
35 static char *get_label(unsigned short ident)
37 return *str_ident[ident].name ? str_ident[ident].name : "UNKNOWN";
40 static int cmp_label(unsigned short ident, const char *label)
43 printf("adi_gpio2: please provide none-null label\n");
46 return strcmp(str_ident[ident].name, label);
51 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
52 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
53 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
54 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
55 #define DECLARE_RESERVED_MAP(m, c) unsigned short reserved_##m##_map[c]
57 static DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
58 static DECLARE_RESERVED_MAP(peri, gpio_bank(MAX_RESOURCES));
60 inline int check_gpio(unsigned gpio)
62 #if defined(CONFIG_BF54x)
63 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15 ||
64 gpio == GPIO_PH14 || gpio == GPIO_PH15 ||
65 gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
68 if (gpio >= MAX_GPIOS)
73 static void port_setup(unsigned gpio, unsigned short usage)
75 #if defined(CONFIG_BF54x)
76 if (usage == GPIO_USAGE)
77 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
79 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
81 if (usage == GPIO_USAGE)
82 gpio_array[gpio_bank(gpio)]->port_fer_clear = gpio_bit(gpio);
84 gpio_array[gpio_bank(gpio)]->port_fer_set = gpio_bit(gpio);
88 inline void portmux_setup(unsigned short per)
91 u16 ident = P_IDENT(per);
92 u16 function = P_FUNCT2MUX(per);
94 pmux = gpio_array[gpio_bank(ident)]->port_mux;
96 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
97 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
99 gpio_array[gpio_bank(ident)]->port_mux = pmux;
102 inline u16 get_portmux(unsigned short per)
105 u16 ident = P_IDENT(per);
107 pmux = gpio_array[gpio_bank(ident)]->port_mux;
109 return pmux >> (2 * gpio_sub_n(ident)) & 0x3;
112 unsigned short get_gpio_dir(unsigned gpio)
115 (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio));
118 /***********************************************************
120 * FUNCTIONS: Peripheral Resource Allocation
124 * per Peripheral Identifier
127 * DESCRIPTION: Peripheral Resource Allocation and Setup API
128 **************************************************************/
130 int peripheral_request(unsigned short per, const char *label)
132 unsigned short ident = P_IDENT(per);
135 * Don't cares are pins with only one dedicated function
138 if (per & P_DONTCARE)
141 if (!(per & P_DEFINED))
144 BUG_ON(ident >= MAX_RESOURCES);
146 /* If a pin can be muxed as either GPIO or peripheral, make
147 * sure it is not already a GPIO pin when we request it.
149 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
150 printf("%s: Peripheral %d is already reserved as GPIO by %s!\n",
151 __func__, ident, get_label(ident));
155 if (unlikely(is_reserved(peri, ident, 1))) {
157 * Pin functions like AMC address strobes my
158 * be requested and used by several drivers
161 if (!((per & P_MAYSHARE) &&
162 get_portmux(per) == P_FUNCT2MUX(per))) {
164 * Allow that the identical pin function can
165 * be requested from the same driver twice
168 if (cmp_label(ident, label) == 0)
171 printf("%s: Peripheral %d function %d is already "
172 "reserved by %s!\n", __func__, ident,
173 P_FUNCT2MUX(per), get_label(ident));
179 reserve(peri, ident);
182 port_setup(ident, PERIPHERAL_USAGE);
184 set_label(ident, label);
189 int peripheral_request_list(const unsigned short per[], const char *label)
194 for (cnt = 0; per[cnt] != 0; cnt++) {
195 ret = peripheral_request(per[cnt], label);
198 for (; cnt > 0; cnt--)
199 peripheral_free(per[cnt - 1]);
208 void peripheral_free(unsigned short per)
210 unsigned short ident = P_IDENT(per);
212 if (per & P_DONTCARE)
215 if (!(per & P_DEFINED))
218 if (unlikely(!is_reserved(peri, ident, 0)))
221 if (!(per & P_MAYSHARE))
222 port_setup(ident, GPIO_USAGE);
224 unreserve(peri, ident);
226 set_label(ident, "free");
229 void peripheral_free_list(const unsigned short per[])
232 for (cnt = 0; per[cnt] != 0; cnt++)
233 peripheral_free(per[cnt]);
236 /***********************************************************
238 * FUNCTIONS: GPIO Driver
241 * gpio PIO Number between 0 and MAX_GPIOS
244 * DESCRIPTION: GPIO Driver API
245 **************************************************************/
247 int gpio_request(unsigned gpio, const char *label)
249 if (check_gpio(gpio) < 0)
253 * Allow that the identical GPIO can
254 * be requested from the same driver twice
255 * Do nothing and return -
258 if (cmp_label(gpio, label) == 0)
261 if (unlikely(is_reserved(gpio, gpio, 1))) {
262 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
263 gpio, get_label(gpio));
266 if (unlikely(is_reserved(peri, gpio, 1))) {
267 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
268 "by %s!\n", gpio, get_label(gpio));
273 set_label(gpio, label);
275 port_setup(gpio, GPIO_USAGE);
280 int gpio_free(unsigned gpio)
282 if (check_gpio(gpio) < 0)
285 if (unlikely(!is_reserved(gpio, gpio, 0))) {
290 unreserve(gpio, gpio);
292 set_label(gpio, "free");
297 #ifdef ADI_SPECIAL_GPIO_BANKS
298 static DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
300 int special_gpio_request(unsigned gpio, const char *label)
303 * Allow that the identical GPIO can
304 * be requested from the same driver twice
305 * Do nothing and return -
308 if (cmp_label(gpio, label) == 0)
311 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
312 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
313 gpio, get_label(gpio));
316 if (unlikely(is_reserved(peri, gpio, 1))) {
317 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
318 "by %s!\n", gpio, get_label(gpio));
323 reserve(special_gpio, gpio);
326 set_label(gpio, label);
327 port_setup(gpio, GPIO_USAGE);
332 void special_gpio_free(unsigned gpio)
334 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
339 unreserve(special_gpio, gpio);
340 unreserve(peri, gpio);
341 set_label(gpio, "free");
345 static inline void __gpio_direction_input(unsigned gpio)
347 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
348 #if defined(CONFIG_BF54x)
349 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
351 gpio_array[gpio_bank(gpio)]->inen_set = gpio_bit(gpio);
355 int gpio_direction_input(unsigned gpio)
359 if (!is_reserved(gpio, gpio, 0)) {
364 local_irq_save(flags);
365 __gpio_direction_input(gpio);
366 local_irq_restore(flags);
371 int gpio_set_value(unsigned gpio, int arg)
374 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
376 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
381 int gpio_direction_output(unsigned gpio, int value)
385 if (!is_reserved(gpio, gpio, 0)) {
390 local_irq_save(flags);
392 #if defined(CONFIG_BF54x)
393 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
395 gpio_array[gpio_bank(gpio)]->inen_clear = gpio_bit(gpio);
397 gpio_set_value(gpio, value);
398 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
400 local_irq_restore(flags);
405 int gpio_get_value(unsigned gpio)
407 return 1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
410 void gpio_labels(void)
414 for (c = 0; c < MAX_RESOURCES; c++) {
415 gpio = is_reserved(gpio, c, 1);
416 if (!check_gpio(c) && gpio)
417 printf("GPIO_%d:\t%s\tGPIO %s\n", c, get_label(c),
418 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
419 else if (is_reserved(peri, c, 1))
420 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));