blob: 62f3dc2539777236b7ecbed9dec0b6ba7db7ccb4 [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>
14#include <gpio.h>
15
16/*
17 * The gpio implementation
18 */
19static const gpio_ops_t *ops;
20
21int 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
30void 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
40int 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
49void 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
Caesar Wang8abdf1c2016-05-25 18:48:45 +080059void 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
70int 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
Haojian Zhuang70f2bd02016-01-27 13:18:21 +080079/*
80 * Initialize the gpio. The fields in the provided gpio
81 * ops pointer must be valid.
82 */
83void 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}