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
11 #include <soc/aml-t931/t931-gpio.h>
12 #include <soc/aml-t931/t931-hw.h>
13
14 #include "sherlock.h"
15
16 namespace sherlock {
17
ButtonsInit()18 zx_status_t Sherlock::ButtonsInit() {
19 constexpr pbus_gpio_t sherlock_buttons_gpios[] = {
20 {
21 // Volume up.
22 .gpio = T931_GPIOZ(4),
23 },
24 {
25 // Volume down.
26 .gpio = T931_GPIOZ(5),
27 },
28 {
29 // Both Volume up and down pressed.
30 .gpio = T931_GPIOZ(13),
31 },
32 {
33 // Mic privacy switch.
34 .gpio = T931_GPIOH(3),
35 },
36 };
37 // clang-format off
38 static constexpr buttons_button_config_t buttons[] = {
39 {BUTTONS_TYPE_DIRECT, BUTTONS_ID_VOLUME_UP, 0, 0, 0},
40 {BUTTONS_TYPE_DIRECT, BUTTONS_ID_VOLUME_DOWN, 1, 0, 0},
41 {BUTTONS_TYPE_DIRECT, BUTTONS_ID_FDR, 2, 0, 0},
42 {BUTTONS_TYPE_DIRECT, BUTTONS_ID_MIC_MUTE, 3, 0, 0},
43 };
44 // No need for internal pull, external pull-ups used.
45 static constexpr buttons_gpio_config_t gpios[] = {
46 {BUTTONS_GPIO_TYPE_INTERRUPT, BUTTONS_GPIO_FLAG_INVERTED, {GPIO_NO_PULL}},
47 {BUTTONS_GPIO_TYPE_INTERRUPT, BUTTONS_GPIO_FLAG_INVERTED, {GPIO_NO_PULL}},
48 {BUTTONS_GPIO_TYPE_INTERRUPT, BUTTONS_GPIO_FLAG_INVERTED, {GPIO_NO_PULL}},
49 {BUTTONS_GPIO_TYPE_INTERRUPT, BUTTONS_GPIO_FLAG_INVERTED, {GPIO_NO_PULL}},
50 };
51 // clang-format on
52 static constexpr pbus_metadata_t available_buttons_metadata[] = {
53 {
54 .type = DEVICE_METADATA_BUTTONS_BUTTONS,
55 .data_buffer = &buttons,
56 .data_size = sizeof(buttons),
57 },
58 {
59 .type = DEVICE_METADATA_BUTTONS_GPIOS,
60 .data_buffer = &gpios,
61 .data_size = sizeof(gpios),
62 }};
63 pbus_dev_t sherlock_buttons_dev = {};
64 sherlock_buttons_dev.name = "sherlock-buttons";
65 sherlock_buttons_dev.vid = PDEV_VID_GENERIC;
66 sherlock_buttons_dev.pid = PDEV_PID_GENERIC;
67 sherlock_buttons_dev.did = PDEV_DID_HID_BUTTONS;
68 sherlock_buttons_dev.gpio_list = sherlock_buttons_gpios;
69 sherlock_buttons_dev.gpio_count = countof(sherlock_buttons_gpios);
70 sherlock_buttons_dev.metadata_list = available_buttons_metadata;
71 sherlock_buttons_dev.metadata_count = countof(available_buttons_metadata);
72
73 zx_status_t status = pbus_.DeviceAdd(&sherlock_buttons_dev);
74 if (status != ZX_OK) {
75 zxlogf(ERROR, "%s: pbus_.DeviceAdd failed %d\n", __FUNCTION__, status);
76 return status;
77 }
78
79 return ZX_OK;
80 }
81
82 } // namespace sherlock
83