1 /* 2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * GPIO -- General Purpose Input/Output 7 * 8 * Defines a simple and generic interface to access GPIO device. 9 * 10 */ 11 12 #include <assert.h> 13 #include <errno.h> 14 15 #include <drivers/gpio.h> 16 17 /* 18 * The gpio implementation 19 */ 20 static const gpio_ops_t *ops; 21 22 int gpio_get_direction(int gpio) 23 { 24 assert(ops); 25 assert(ops->get_direction != 0); 26 assert(gpio >= 0); 27 28 return ops->get_direction(gpio); 29 } 30 31 void gpio_set_direction(int gpio, int direction) 32 { 33 assert(ops); 34 assert(ops->set_direction != 0); 35 assert((direction == GPIO_DIR_OUT) || (direction == GPIO_DIR_IN)); 36 assert(gpio >= 0); 37 38 ops->set_direction(gpio, direction); 39 } 40 41 int gpio_get_value(int gpio) 42 { 43 assert(ops); 44 assert(ops->get_value != 0); 45 assert(gpio >= 0); 46 47 return ops->get_value(gpio); 48 } 49 50 void gpio_set_value(int gpio, int value) 51 { 52 assert(ops); 53 assert(ops->set_value != 0); 54 assert((value == GPIO_LEVEL_LOW) || (value == GPIO_LEVEL_HIGH)); 55 assert(gpio >= 0); 56 57 ops->set_value(gpio, value); 58 } 59 60 void gpio_set_pull(int gpio, int pull) 61 { 62 assert(ops); 63 assert(ops->set_pull != 0); 64 assert((pull == GPIO_PULL_NONE) || (pull == GPIO_PULL_UP) || 65 (pull == GPIO_PULL_DOWN)); 66 assert(gpio >= 0); 67 68 ops->set_pull(gpio, pull); 69 } 70 71 int gpio_get_pull(int gpio) 72 { 73 assert(ops); 74 assert(ops->get_pull != 0); 75 assert(gpio >= 0); 76 77 return ops->get_pull(gpio); 78 } 79 80 /* 81 * Initialize the gpio. The fields in the provided gpio 82 * ops pointer must be valid. 83 */ 84 void gpio_init(const gpio_ops_t *ops_ptr) 85 { 86 assert(ops_ptr != 0 && 87 (ops_ptr->get_direction != 0) && 88 (ops_ptr->set_direction != 0) && 89 (ops_ptr->get_value != 0) && 90 (ops_ptr->set_value != 0)); 91 92 ops = ops_ptr; 93 } 94