summaryrefslogtreecommitdiff
path: root/Kernel/Arch/aarch64/RPi/GPIO.h
blob: 4f38f9c376403adfec31f0dfcb4bb74a329a3c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (c) 2021, Nico Weber <thakis@chromium.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Array.h>
#include <AK/Types.h>

namespace Prekernel {

struct GPIOControlRegisters;

// Can configure the general-purpose I/O registers on a Raspberry Pi.
class GPIO {
public:
    enum class PinFunction {
        Input = 0,
        Output = 1,
        Alternate0 = 0b100,
        Alternate1 = 0b101,
        Alternate2 = 0b110,
        Alternate3 = 0b111,
        Alternate4 = 0b011,
        Alternate5 = 0b010,
    };

    static GPIO& the();

    void set_pin_function(unsigned pin_number, PinFunction);

    enum class PullUpDownState {
        Disable = 0,
        PullDown = 1,
        PullUp = 2,
    };

    template<size_t N>
    void set_pin_pull_up_down_state(Array<int, N> pins, PullUpDownState state)
    {
        u32 enable[2] = {};
        for (int pin : pins) {
            if (pin < 32)
                enable[0] |= (1 << pin);
            else
                enable[1] |= (1 << (pin - 32));
        }
        internal_enable_pins(enable, state);
    }

private:
    GPIO();
    void internal_enable_pins(u32 enable[2], PullUpDownState state);

    GPIOControlRegisters volatile* m_registers;
};

}