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 #include <gpio.h> 15 16 /* 17 * The gpio implementation 18 */ 19 static const gpio_ops_t *ops; 20 21 int gpio_get_direction(int gpio) 22 { 23 assert(ops); 24 assert(ops->get_direction != 0); 25 assert(gpio >= 0); 26 27 return ops->get_direction(gpio); 28 } 29 30 void gpio_set_direction(int gpio, int direction) 31 { 32 assert(ops); 33 assert(ops->set_direction != 0); 34 assert((direction == GPIO_DIR_OUT) || (direction == GPIO_DIR_IN)); 35 assert(gpio >= 0); 36 37 ops->set_direction(gpio, direction); 38 } 39 40 int gpio_get_value(int gpio) 41 { 42 assert(ops); 43 assert(ops->get_value != 0); 44 assert(gpio >= 0); 45 46 return ops->get_value(gpio); 47 } 48 49 void gpio_set_value(int gpio, int value) 50 { 51 assert(ops); 52 assert(ops->set_value != 0); 53 assert((value == GPIO_LEVEL_LOW) || (value == GPIO_LEVEL_HIGH)); 54 assert(gpio >= 0); 55 56 ops->set_value(gpio, value); 57 } 58 59 void gpio_set_pull(int gpio, int pull) 60 { 61 assert(ops); 62 assert(ops->set_pull != 0); 63 assert((pull == GPIO_PULL_NONE) || (pull == GPIO_PULL_UP) || 64 (pull == GPIO_PULL_DOWN)); 65 assert(gpio >= 0); 66 67 ops->set_pull(gpio, pull); 68 } 69 70 int gpio_get_pull(int gpio) 71 { 72 assert(ops); 73 assert(ops->get_pull != 0); 74 assert(gpio >= 0); 75 76 return ops->get_pull(gpio); 77 } 78 79 /* 80 * Initialize the gpio. The fields in the provided gpio 81 * ops pointer must be valid. 82 */ 83 void gpio_init(const gpio_ops_t *ops_ptr) 84 { 85 assert(ops_ptr != 0 && 86 (ops_ptr->get_direction != 0) && 87 (ops_ptr->set_direction != 0) && 88 (ops_ptr->get_value != 0) && 89 (ops_ptr->set_value != 0)); 90 91 ops = ops_ptr; 92 } 93