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> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Ajay Bhargav | 0c44229 | 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 | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 22 | int gpio_request(unsigned gpio, const char *label) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 23 | { |
Joe Hershberger | 365d607 | 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 | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 27 | } |
| 28 | return 0; |
| 29 | } |
| 30 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 31 | int gpio_free(unsigned gpio) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 32 | { |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 36 | int gpio_direction_input(unsigned gpio) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 37 | { |
| 38 | struct gpio_reg *gpio_reg_bank; |
| 39 | |
Joe Hershberger | 365d607 | 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 | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 43 | } |
| 44 | |
Joe Hershberger | 365d607 | 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 | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 50 | int gpio_direction_output(unsigned gpio, int value) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 51 | { |
| 52 | struct gpio_reg *gpio_reg_bank; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 53 | |
Joe Hershberger | 365d607 | 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 | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 57 | } |
| 58 | |
Joe Hershberger | 365d607 | 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); |
| 62 | return 0; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 65 | int gpio_get_value(unsigned gpio) |
| 66 | { |
| 67 | struct gpio_reg *gpio_reg_bank; |
| 68 | u32 gpio_val; |
| 69 | |
| 70 | if (gpio >= MV_MAX_GPIO) { |
| 71 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 76 | gpio_val = readl(&gpio_reg_bank->gplr); |
| 77 | |
| 78 | return GPIO_VAL(gpio, gpio_val); |
| 79 | } |
| 80 | |
| 81 | int gpio_set_value(unsigned gpio, int value) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 82 | { |
| 83 | struct gpio_reg *gpio_reg_bank; |
| 84 | |
Joe Hershberger | 365d607 | 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 | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 90 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 91 | if (value) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 92 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr); |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 93 | else |
Joe Hershberger | 365d607 | 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 | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 97 | } |