10c442298SAjay Bhargav /* 20c442298SAjay Bhargav * (C) Copyright 2011 30c442298SAjay Bhargav * eInfochips Ltd. <www.einfochips.com> 40c442298SAjay Bhargav * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com> 50c442298SAjay Bhargav * 60c442298SAjay Bhargav * (C) Copyright 2010 70c442298SAjay Bhargav * Marvell Semiconductor <www.marvell.com> 80c442298SAjay Bhargav * 9*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 100c442298SAjay Bhargav */ 110c442298SAjay Bhargav 120c442298SAjay Bhargav #include <common.h> 130c442298SAjay Bhargav #include <asm/io.h> 140c442298SAjay Bhargav #include <asm/errno.h> 150c442298SAjay Bhargav #include "mvgpio.h" 160c442298SAjay Bhargav #include <asm/gpio.h> 170c442298SAjay Bhargav 180c442298SAjay Bhargav #ifndef MV_MAX_GPIO 190c442298SAjay Bhargav #define MV_MAX_GPIO 128 200c442298SAjay Bhargav #endif 210c442298SAjay Bhargav 22365d6070SJoe Hershberger int gpio_request(unsigned gpio, const char *label) 230c442298SAjay Bhargav { 24365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) { 25365d6070SJoe Hershberger printf("%s: Invalid GPIO requested %d\n", __func__, gpio); 26365d6070SJoe Hershberger return -1; 270c442298SAjay Bhargav } 280c442298SAjay Bhargav return 0; 290c442298SAjay Bhargav } 300c442298SAjay Bhargav 31365d6070SJoe Hershberger int gpio_free(unsigned gpio) 320c442298SAjay Bhargav { 330c442298SAjay Bhargav return 0; 340c442298SAjay Bhargav } 350c442298SAjay Bhargav 36365d6070SJoe Hershberger int gpio_direction_input(unsigned gpio) 370c442298SAjay Bhargav { 380c442298SAjay Bhargav struct gpio_reg *gpio_reg_bank; 390c442298SAjay Bhargav 40365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) { 41365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio); 42365d6070SJoe Hershberger return -1; 430c442298SAjay Bhargav } 440c442298SAjay Bhargav 45365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); 46365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr); 470c442298SAjay Bhargav return 0; 480c442298SAjay Bhargav } 490c442298SAjay Bhargav 50365d6070SJoe Hershberger int gpio_direction_output(unsigned gpio, int value) 510c442298SAjay Bhargav { 520c442298SAjay Bhargav struct gpio_reg *gpio_reg_bank; 530c442298SAjay Bhargav 54365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) { 55365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio); 56365d6070SJoe Hershberger return -1; 570c442298SAjay Bhargav } 580c442298SAjay Bhargav 59365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); 60365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr); 61365d6070SJoe Hershberger gpio_set_value(gpio, value); 62365d6070SJoe Hershberger return 0; 63365d6070SJoe Hershberger } 64365d6070SJoe Hershberger 65365d6070SJoe Hershberger int gpio_get_value(unsigned gpio) 66365d6070SJoe Hershberger { 67365d6070SJoe Hershberger struct gpio_reg *gpio_reg_bank; 68365d6070SJoe Hershberger u32 gpio_val; 69365d6070SJoe Hershberger 70365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) { 71365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio); 72365d6070SJoe Hershberger return -1; 73365d6070SJoe Hershberger } 74365d6070SJoe Hershberger 75365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); 76365d6070SJoe Hershberger gpio_val = readl(&gpio_reg_bank->gplr); 77365d6070SJoe Hershberger 78365d6070SJoe Hershberger return GPIO_VAL(gpio, gpio_val); 79365d6070SJoe Hershberger } 80365d6070SJoe Hershberger 81365d6070SJoe Hershberger int gpio_set_value(unsigned gpio, int value) 82365d6070SJoe Hershberger { 83365d6070SJoe Hershberger struct gpio_reg *gpio_reg_bank; 84365d6070SJoe Hershberger 85365d6070SJoe Hershberger if (gpio >= MV_MAX_GPIO) { 86365d6070SJoe Hershberger printf("%s: Invalid GPIO %d\n", __func__, gpio); 87365d6070SJoe Hershberger return -1; 88365d6070SJoe Hershberger } 89365d6070SJoe Hershberger 90365d6070SJoe Hershberger gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); 910c442298SAjay Bhargav if (value) 92365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr); 930c442298SAjay Bhargav else 94365d6070SJoe Hershberger writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr); 95365d6070SJoe Hershberger 96365d6070SJoe Hershberger return 0; 970c442298SAjay Bhargav } 98