1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20c442298SAjay Bhargav /*
30c442298SAjay Bhargav * (C) Copyright 2011
40c442298SAjay Bhargav * eInfochips Ltd. <www.einfochips.com>
5c7c47ca2SAjay Bhargav * Written-by: Ajay Bhargav <contact@8051projects.net>
60c442298SAjay Bhargav *
70c442298SAjay Bhargav * (C) Copyright 2010
80c442298SAjay Bhargav * Marvell Semiconductor <www.marvell.com>
90c442298SAjay Bhargav */
100c442298SAjay Bhargav
110c442298SAjay Bhargav #include <common.h>
120c442298SAjay Bhargav #include <asm/io.h>
131221ce45SMasahiro Yamada #include <linux/errno.h>
140c442298SAjay Bhargav #include "mvgpio.h"
150c442298SAjay Bhargav #include <asm/gpio.h>
160c442298SAjay Bhargav
170c442298SAjay Bhargav #ifndef MV_MAX_GPIO
180c442298SAjay Bhargav #define MV_MAX_GPIO 128
190c442298SAjay Bhargav #endif
200c442298SAjay Bhargav
gpio_request(unsigned gpio,const char * label)21365d6070SJoe Hershberger int gpio_request(unsigned gpio, const char *label)
220c442298SAjay Bhargav {
23365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) {
24365d6070SJoe Hershberger printf("%s: Invalid GPIO requested %d\n", __func__, gpio);
25365d6070SJoe Hershberger return -1;
260c442298SAjay Bhargav }
270c442298SAjay Bhargav return 0;
280c442298SAjay Bhargav }
290c442298SAjay Bhargav
gpio_free(unsigned gpio)30365d6070SJoe Hershberger int gpio_free(unsigned gpio)
310c442298SAjay Bhargav {
320c442298SAjay Bhargav return 0;
330c442298SAjay Bhargav }
340c442298SAjay Bhargav
gpio_direction_input(unsigned gpio)35365d6070SJoe Hershberger int gpio_direction_input(unsigned gpio)
360c442298SAjay Bhargav {
370c442298SAjay Bhargav struct gpio_reg *gpio_reg_bank;
380c442298SAjay Bhargav
39365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) {
40365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio);
41365d6070SJoe Hershberger return -1;
420c442298SAjay Bhargav }
430c442298SAjay Bhargav
44365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
45365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr);
460c442298SAjay Bhargav return 0;
470c442298SAjay Bhargav }
480c442298SAjay Bhargav
gpio_direction_output(unsigned gpio,int value)49365d6070SJoe Hershberger int gpio_direction_output(unsigned gpio, int value)
500c442298SAjay Bhargav {
510c442298SAjay Bhargav struct gpio_reg *gpio_reg_bank;
520c442298SAjay Bhargav
53365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) {
54365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio);
55365d6070SJoe Hershberger return -1;
560c442298SAjay Bhargav }
570c442298SAjay Bhargav
58365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
59365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr);
60365d6070SJoe Hershberger gpio_set_value(gpio, value);
61365d6070SJoe Hershberger return 0;
62365d6070SJoe Hershberger }
63365d6070SJoe Hershberger
gpio_get_value(unsigned gpio)64365d6070SJoe Hershberger int gpio_get_value(unsigned gpio)
65365d6070SJoe Hershberger {
66365d6070SJoe Hershberger struct gpio_reg *gpio_reg_bank;
67365d6070SJoe Hershberger u32 gpio_val;
68365d6070SJoe Hershberger
69365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) {
70365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio);
71365d6070SJoe Hershberger return -1;
72365d6070SJoe Hershberger }
73365d6070SJoe Hershberger
74365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
75365d6070SJoe Hershberger gpio_val = readl(&gpio_reg_bank->gplr);
76365d6070SJoe Hershberger
77365d6070SJoe Hershberger return GPIO_VAL(gpio, gpio_val);
78365d6070SJoe Hershberger }
79365d6070SJoe Hershberger
gpio_set_value(unsigned gpio,int value)80365d6070SJoe Hershberger int gpio_set_value(unsigned gpio, int value)
81365d6070SJoe Hershberger {
82365d6070SJoe Hershberger struct gpio_reg *gpio_reg_bank;
83365d6070SJoe Hershberger
84365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) {
85365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio);
86365d6070SJoe Hershberger return -1;
87365d6070SJoe Hershberger }
88365d6070SJoe Hershberger
89365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
900c442298SAjay Bhargav if (value)
91365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr);
920c442298SAjay Bhargav else
93365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr);
94365d6070SJoe Hershberger
95365d6070SJoe Hershberger return 0;
960c442298SAjay Bhargav }
97