blob: 76612b253e1dbdaf2992546718ce2eb68bb68a12 [file] [log] [blame]
Haojian Zhuang70f2bd02016-01-27 13:18:21 +08001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Haojian Zhuang70f2bd02016-01-27 13:18:21 +08005 *
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>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014
15#include <drivers/gpio.h>
Haojian Zhuang70f2bd02016-01-27 13:18:21 +080016
17/*
18 * The gpio implementation
19 */
20static const gpio_ops_t *ops;
21
22int 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
31void 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
41int 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
50void 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
Caesar Wang8abdf1c2016-05-25 18:48:45 +080060void 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
71int 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
Haojian Zhuang70f2bd02016-01-27 13:18:21 +080080/*
81 * Initialize the gpio. The fields in the provided gpio
82 * ops pointer must be valid.
83 */
84void 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}