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
11 #include <linux/errno.h>
14 #define RESOURCE_LABEL_SIZE 16
16 static struct str_ident {
17 char name[RESOURCE_LABEL_SIZE];
18 } str_ident[MAX_RESOURCES];
20 static void gpio_error(unsigned gpio)
22 printf("adi_gpio2: GPIO %d wasn't requested!\n", gpio);
25 static void set_label(unsigned short ident, const char *label)
28 strncpy(str_ident[ident].name, label,
30 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
34 static char *get_label(unsigned short ident)
36 return *str_ident[ident].name ? str_ident[ident].name : "UNKNOWN";
39 static int cmp_label(unsigned short ident, const char *label)
42 printf("adi_gpio2: please provide none-null label\n");
45 return strcmp(str_ident[ident].name, label);
50 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
51 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
52 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
53 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
54 #define DECLARE_RESERVED_MAP(m, c) unsigned short reserved_##m##_map[c]
56 static DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
57 static DECLARE_RESERVED_MAP(peri, gpio_bank(MAX_RESOURCES));
59 inline int check_gpio(unsigned gpio)
61 #if defined(CONFIG_BF54x)
62 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15 ||
63 gpio == GPIO_PH14 || gpio == GPIO_PH15 ||
64 gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
67 if (gpio >= MAX_GPIOS)
72 static void port_setup(unsigned gpio, unsigned short usage)
74 #if defined(CONFIG_BF54x)
75 if (usage == GPIO_USAGE)
76 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
78 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
80 if (usage == GPIO_USAGE)
81 gpio_array[gpio_bank(gpio)]->port_fer_clear = gpio_bit(gpio);
83 gpio_array[gpio_bank(gpio)]->port_fer_set = gpio_bit(gpio);
87 inline void portmux_setup(unsigned short per)
90 u16 ident = P_IDENT(per);
91 u16 function = P_FUNCT2MUX(per);
93 pmux = gpio_array[gpio_bank(ident)]->port_mux;
95 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
96 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
98 gpio_array[gpio_bank(ident)]->port_mux = pmux;
101 inline u16 get_portmux(unsigned short per)
104 u16 ident = P_IDENT(per);
106 pmux = gpio_array[gpio_bank(ident)]->port_mux;
108 return pmux >> (2 * gpio_sub_n(ident)) & 0x3;
111 unsigned short get_gpio_dir(unsigned gpio)
114 (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio));
117 /***********************************************************
119 * FUNCTIONS: Peripheral Resource Allocation
123 * per Peripheral Identifier
126 * DESCRIPTION: Peripheral Resource Allocation and Setup API
127 **************************************************************/
129 int peripheral_request(unsigned short per, const char *label)
131 unsigned short ident = P_IDENT(per);
134 * Don't cares are pins with only one dedicated function
137 if (per & P_DONTCARE)
140 if (!(per & P_DEFINED))
143 BUG_ON(ident >= MAX_RESOURCES);
145 /* If a pin can be muxed as either GPIO or peripheral, make
146 * sure it is not already a GPIO pin when we request it.
148 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
149 printf("%s: Peripheral %d is already reserved as GPIO by %s!\n",
150 __func__, ident, get_label(ident));
154 if (unlikely(is_reserved(peri, ident, 1))) {
156 * Pin functions like AMC address strobes my
157 * be requested and used by several drivers
160 if (!((per & P_MAYSHARE) &&
161 get_portmux(per) == P_FUNCT2MUX(per))) {
163 * Allow that the identical pin function can
164 * be requested from the same driver twice
167 if (cmp_label(ident, label) == 0)
170 printf("%s: Peripheral %d function %d is already "
171 "reserved by %s!\n", __func__, ident,
172 P_FUNCT2MUX(per), get_label(ident));
178 reserve(peri, ident);
181 port_setup(ident, PERIPHERAL_USAGE);
183 set_label(ident, label);
188 int peripheral_request_list(const unsigned short per[], const char *label)
193 for (cnt = 0; per[cnt] != 0; cnt++) {
194 ret = peripheral_request(per[cnt], label);
197 for (; cnt > 0; cnt--)
198 peripheral_free(per[cnt - 1]);
207 void peripheral_free(unsigned short per)
209 unsigned short ident = P_IDENT(per);
211 if (per & P_DONTCARE)
214 if (!(per & P_DEFINED))
217 if (unlikely(!is_reserved(peri, ident, 0)))
220 if (!(per & P_MAYSHARE))
221 port_setup(ident, GPIO_USAGE);
223 unreserve(peri, ident);
225 set_label(ident, "free");
228 void peripheral_free_list(const unsigned short per[])
231 for (cnt = 0; per[cnt] != 0; cnt++)
232 peripheral_free(per[cnt]);
235 /***********************************************************
237 * FUNCTIONS: GPIO Driver
240 * gpio PIO Number between 0 and MAX_GPIOS
243 * DESCRIPTION: GPIO Driver API
244 **************************************************************/
246 int gpio_request(unsigned gpio, const char *label)
248 if (check_gpio(gpio) < 0)
252 * Allow that the identical GPIO can
253 * be requested from the same driver twice
254 * Do nothing and return -
257 if (cmp_label(gpio, label) == 0)
260 if (unlikely(is_reserved(gpio, gpio, 1))) {
261 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
262 gpio, get_label(gpio));
265 if (unlikely(is_reserved(peri, gpio, 1))) {
266 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
267 "by %s!\n", gpio, get_label(gpio));
272 set_label(gpio, label);
274 port_setup(gpio, GPIO_USAGE);
279 int gpio_free(unsigned gpio)
281 if (check_gpio(gpio) < 0)
284 if (unlikely(!is_reserved(gpio, gpio, 0))) {
289 unreserve(gpio, gpio);
291 set_label(gpio, "free");
296 #ifdef ADI_SPECIAL_GPIO_BANKS
297 static DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
299 int special_gpio_request(unsigned gpio, const char *label)
302 * Allow that the identical GPIO can
303 * be requested from the same driver twice
304 * Do nothing and return -
307 if (cmp_label(gpio, label) == 0)
310 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
311 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
312 gpio, get_label(gpio));
315 if (unlikely(is_reserved(peri, gpio, 1))) {
316 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
317 "by %s!\n", gpio, get_label(gpio));
322 reserve(special_gpio, gpio);
325 set_label(gpio, label);
326 port_setup(gpio, GPIO_USAGE);
331 void special_gpio_free(unsigned gpio)
333 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
338 unreserve(special_gpio, gpio);
339 unreserve(peri, gpio);
340 set_label(gpio, "free");
344 static inline void __gpio_direction_input(unsigned gpio)
346 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
347 #if defined(CONFIG_BF54x)
348 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
350 gpio_array[gpio_bank(gpio)]->inen_set = gpio_bit(gpio);
354 int gpio_direction_input(unsigned gpio)
358 if (!is_reserved(gpio, gpio, 0)) {
363 local_irq_save(flags);
364 __gpio_direction_input(gpio);
365 local_irq_restore(flags);
370 int gpio_set_value(unsigned gpio, int arg)
373 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
375 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
380 int gpio_direction_output(unsigned gpio, int value)
384 if (!is_reserved(gpio, gpio, 0)) {
389 local_irq_save(flags);
391 #if defined(CONFIG_BF54x)
392 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
394 gpio_array[gpio_bank(gpio)]->inen_clear = gpio_bit(gpio);
396 gpio_set_value(gpio, value);
397 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
399 local_irq_restore(flags);
404 int gpio_get_value(unsigned gpio)
406 return 1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
409 void gpio_labels(void)
413 for (c = 0; c < MAX_RESOURCES; c++) {
414 gpio = is_reserved(gpio, c, 1);
415 if (!check_gpio(c) && gpio)
416 printf("GPIO_%d:\t%s\tGPIO %s\n", c, get_label(c),
417 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
418 else if (is_reserved(peri, c, 1))
419 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));