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