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