Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 |
| 3 | * eInfochips Ltd. <www.einfochips.com> |
Ajay Bhargav | 0ad0b61 | 2016-12-21 13:28:06 +0530 | [diff] [blame] | 4 | * Written-by: Ajay Bhargav <contact@8051projects.net> |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 5 | * |
| 6 | * (C) Copyright 2010 |
| 7 | * Marvell Semiconductor <www.marvell.com> |
| 8 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 15 | #include "mvgpio.h" |
| 16 | #include <asm/gpio.h> |
| 17 | |
| 18 | #ifndef MV_MAX_GPIO |
| 19 | #define MV_MAX_GPIO 128 |
| 20 | #endif |
| 21 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 22 | int gpio_request(unsigned gpio, const char *label) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 23 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 24 | if (gpio >= MV_MAX_GPIO) { |
| 25 | printf("%s: Invalid GPIO requested %d\n", __func__, gpio); |
| 26 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 27 | } |
| 28 | return 0; |
| 29 | } |
| 30 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 31 | int gpio_free(unsigned gpio) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 32 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 33 | return 0; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 34 | } |
| 35 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 36 | int gpio_direction_input(unsigned gpio) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 37 | { |
| 38 | struct gpio_reg *gpio_reg_bank; |
| 39 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 40 | if (gpio >= MV_MAX_GPIO) { |
| 41 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 42 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 43 | } |
| 44 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 45 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 46 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 50 | int gpio_direction_output(unsigned gpio, int value) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 51 | { |
| 52 | struct gpio_reg *gpio_reg_bank; |
| 53 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 54 | if (gpio >= MV_MAX_GPIO) { |
| 55 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 56 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 57 | } |
| 58 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 59 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 60 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr); |
| 61 | gpio_set_value(gpio, value); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 65 | int gpio_get_value(unsigned gpio) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 66 | { |
| 67 | struct gpio_reg *gpio_reg_bank; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 68 | u32 gpio_val; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 69 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 70 | if (gpio >= MV_MAX_GPIO) { |
| 71 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 72 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 73 | } |
| 74 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 75 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 76 | gpio_val = readl(&gpio_reg_bank->gplr); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 77 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 78 | return GPIO_VAL(gpio, gpio_val); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 79 | } |
| 80 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 81 | int gpio_set_value(unsigned gpio, int value) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 82 | { |
| 83 | struct gpio_reg *gpio_reg_bank; |
| 84 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 85 | if (gpio >= MV_MAX_GPIO) { |
| 86 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 87 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 90 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 91 | if (value) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 92 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 93 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 94 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr); |
| 95 | |
| 96 | return 0; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 97 | } |