Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 3 | #include "file.hpp" |
| 4 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 5 | #include <linux/gpio.h> |
| 6 | |
| 7 | #include <string> |
| 8 | #include <type_traits> |
| 9 | |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 10 | namespace phosphor |
| 11 | { |
| 12 | namespace gpio |
| 13 | { |
| 14 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 15 | typedef std::remove_reference<decltype( |
| 16 | gpiohandle_request::lineoffsets[0])>::type gpioNum_t; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 17 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 18 | typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type |
| 19 | gpioValue_t; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Represents a GPIO. |
| 23 | * |
| 24 | * Operations are setting low or high. |
| 25 | * |
| 26 | * Read support may be added in the future. |
| 27 | */ |
| 28 | class GPIO |
| 29 | { |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 30 | public: |
| 31 | /** |
| 32 | * If the GPIO is an input or output |
| 33 | */ |
| 34 | enum class Direction |
| 35 | { |
| 36 | input, |
| 37 | output |
| 38 | }; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 39 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 40 | /** |
| 41 | * The possible values - low or high |
| 42 | */ |
| 43 | enum class Value |
| 44 | { |
| 45 | low, |
| 46 | high |
| 47 | }; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 48 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 49 | GPIO() = delete; |
| 50 | GPIO(const GPIO&) = delete; |
| 51 | GPIO(GPIO&&) = default; |
| 52 | GPIO& operator=(const GPIO&) = delete; |
| 53 | GPIO& operator=(GPIO&&) = default; |
| 54 | ~GPIO() = default; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 55 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Constructor |
| 58 | * |
| 59 | * @param[in] device - the GPIO device file |
| 60 | * @param[in] gpio - the GPIO number |
| 61 | * @param[in] direction - the GPIO direction |
| 62 | */ |
| 63 | GPIO(const std::string& device, gpioNum_t gpio, Direction direction) : |
| 64 | device(device), gpio(gpio), direction(direction) |
| 65 | { |
| 66 | } |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 67 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Sets the GPIO value |
| 70 | * |
| 71 | * Requests the GPIO line if it hasn't been done already. |
| 72 | */ |
| 73 | void set(Value value); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 74 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 75 | private: |
| 76 | /** |
| 77 | * Requests a GPIO line from the GPIO device |
| 78 | * |
| 79 | * @param[in] defaultValue - The default value, required for |
| 80 | * output GPIOs only. |
| 81 | */ |
| 82 | void requestLine(Value defaultValue = Value::high); |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 83 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 84 | /** |
| 85 | * The GPIO device name, like /dev/gpiochip0 |
| 86 | */ |
| 87 | const std::string device; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 88 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 89 | /** |
| 90 | * The GPIO number |
| 91 | */ |
| 92 | const gpioNum_t gpio; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 93 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 94 | /** |
| 95 | * The GPIO direction |
| 96 | */ |
| 97 | const Direction direction; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 98 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 99 | /** |
| 100 | * File descriptor for the GPIO line |
| 101 | */ |
| 102 | FileDescriptor lineFD; |
Matt Spinler | 36df197 | 2017-05-25 10:23:05 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 105 | } // namespace gpio |
| 106 | } // namespace phosphor |