blob: ea2f689d60ef334029180e50d5eee4152efb3b14 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +05302/*
3 * (C) Copyright 2011
4 * eInfochips Ltd. <www.einfochips.com>
Ajay Bhargav0ad0b612016-12-21 13:28:06 +05305 * Written-by: Ajay Bhargav <contact@8051projects.net>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +05306 *
7 * (C) Copyright 2010
8 * Marvell Semiconductor <www.marvell.com>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +05309 */
10
11#include <common.h>
12#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053014#include "mvgpio.h"
15#include <asm/gpio.h>
16
17#ifndef MV_MAX_GPIO
18#define MV_MAX_GPIO 128
19#endif
20
Joe Hershbergerf8928f12011-11-11 15:55:36 -060021int gpio_request(unsigned gpio, const char *label)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053022{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060023 if (gpio >= MV_MAX_GPIO) {
24 printf("%s: Invalid GPIO requested %d\n", __func__, gpio);
25 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053026 }
27 return 0;
28}
29
Joe Hershbergerf8928f12011-11-11 15:55:36 -060030int gpio_free(unsigned gpio)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053031{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060032 return 0;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053033}
34
Joe Hershbergerf8928f12011-11-11 15:55:36 -060035int gpio_direction_input(unsigned gpio)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053036{
37 struct gpio_reg *gpio_reg_bank;
38
Joe Hershbergerf8928f12011-11-11 15:55:36 -060039 if (gpio >= MV_MAX_GPIO) {
40 printf("%s: Invalid GPIO %d\n", __func__, gpio);
41 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053042 }
43
Joe Hershbergerf8928f12011-11-11 15:55:36 -060044 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
45 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053046 return 0;
47}
48
Joe Hershbergerf8928f12011-11-11 15:55:36 -060049int gpio_direction_output(unsigned gpio, int value)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053050{
51 struct gpio_reg *gpio_reg_bank;
52
Joe Hershbergerf8928f12011-11-11 15:55:36 -060053 if (gpio >= MV_MAX_GPIO) {
54 printf("%s: Invalid GPIO %d\n", __func__, gpio);
55 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053056 }
57
Joe Hershbergerf8928f12011-11-11 15:55:36 -060058 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
59 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr);
60 gpio_set_value(gpio, value);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053061 return 0;
62}
63
Joe Hershbergerf8928f12011-11-11 15:55:36 -060064int gpio_get_value(unsigned gpio)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053065{
66 struct gpio_reg *gpio_reg_bank;
Joe Hershbergerf8928f12011-11-11 15:55:36 -060067 u32 gpio_val;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053068
Joe Hershbergerf8928f12011-11-11 15:55:36 -060069 if (gpio >= MV_MAX_GPIO) {
70 printf("%s: Invalid GPIO %d\n", __func__, gpio);
71 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053072 }
73
Joe Hershbergerf8928f12011-11-11 15:55:36 -060074 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
75 gpio_val = readl(&gpio_reg_bank->gplr);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053076
Joe Hershbergerf8928f12011-11-11 15:55:36 -060077 return GPIO_VAL(gpio, gpio_val);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053078}
79
Joe Hershbergerf8928f12011-11-11 15:55:36 -060080int gpio_set_value(unsigned gpio, int value)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053081{
82 struct gpio_reg *gpio_reg_bank;
83
Joe Hershbergerf8928f12011-11-11 15:55:36 -060084 if (gpio >= MV_MAX_GPIO) {
85 printf("%s: Invalid GPIO %d\n", __func__, gpio);
86 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053087 }
88
Joe Hershbergerf8928f12011-11-11 15:55:36 -060089 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053090 if (value)
Joe Hershbergerf8928f12011-11-11 15:55:36 -060091 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053092 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060093 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr);
94
95 return 0;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053096}