1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <ddk/debug.h>
6 #include <ddk/device.h>
7 #include <ddk/metadata.h>
8 #include <ddk/metadata/buttons.h>
9 #include <ddk/platform-defs.h>
10 #include <ddk/protocol/gpio.h>
11 
12 #include <soc/mt8167/mt8167-hw.h>
13 
14 #include "mt8167.h"
15 
16 namespace board_mt8167 {
17 
ButtonsInit()18 zx_status_t Mt8167::ButtonsInit() {
19     constexpr pbus_gpio_t mt8167_buttons_gpios[] = {
20         {
21             // KPROW0.
22             .gpio = 40,
23         },
24         {
25             // KPROW1.
26             .gpio = 41,
27         },
28         {
29             // KPCOL0.
30             .gpio = 42,
31         },
32         {
33             // KPCOL1.
34             .gpio = 43,
35         },
36     };
37     // clang-format off
38     static constexpr buttons_button_config_t buttons[] = {
39         {BUTTONS_TYPE_MATRIX, BUTTONS_ID_VOLUME_UP,  0, 2, 0},
40         {BUTTONS_TYPE_MATRIX, BUTTONS_ID_KEY_A,      1, 2, 0},
41         {BUTTONS_TYPE_MATRIX, BUTTONS_ID_KEY_M,      0, 3, 0},
42         {BUTTONS_TYPE_MATRIX, BUTTONS_ID_PLAY_PAUSE, 1, 3, 0},
43     };
44     static constexpr buttons_gpio_config_t gpios[] = {
45         {BUTTONS_GPIO_TYPE_INTERRUPT,     BUTTONS_GPIO_FLAG_INVERTED, {GPIO_PULL_UP}},
46         {BUTTONS_GPIO_TYPE_INTERRUPT,     BUTTONS_GPIO_FLAG_INVERTED, {GPIO_PULL_UP}},
47         {BUTTONS_GPIO_TYPE_MATRIX_OUTPUT, BUTTONS_GPIO_FLAG_INVERTED, {0}           },
48         {BUTTONS_GPIO_TYPE_MATRIX_OUTPUT, BUTTONS_GPIO_FLAG_INVERTED, {0}           },
49     };
50     // clang-format on
51     static constexpr pbus_metadata_t available_buttons_metadata[] = {
52         {
53             .type = DEVICE_METADATA_BUTTONS_BUTTONS,
54             .data_buffer = &buttons,
55             .data_size = sizeof(buttons),
56         },
57         {
58             .type = DEVICE_METADATA_BUTTONS_GPIOS,
59             .data_buffer = &gpios,
60             .data_size = sizeof(gpios),
61         }};
62     pbus_dev_t mt8167_buttons_dev = {};
63     mt8167_buttons_dev.name = "mt8167-buttons";
64     mt8167_buttons_dev.vid = PDEV_VID_GENERIC;
65     mt8167_buttons_dev.pid = PDEV_PID_GENERIC;
66     mt8167_buttons_dev.did = PDEV_DID_HID_BUTTONS;
67     mt8167_buttons_dev.gpio_list = mt8167_buttons_gpios;
68     mt8167_buttons_dev.gpio_count = countof(mt8167_buttons_gpios);
69     mt8167_buttons_dev.metadata_list = available_buttons_metadata;
70     mt8167_buttons_dev.metadata_count = countof(available_buttons_metadata);
71 
72     zx_status_t status = pbus_.DeviceAdd(&mt8167_buttons_dev);
73     if (status != ZX_OK) {
74         zxlogf(ERROR, "%s: pbus_.DeviceAdd failed %d\n", __FUNCTION__, status);
75         return status;
76     }
77 
78     return ZX_OK;
79 }
80 
81 } // namespace board_mt8167
82