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/protocol/platform-bus.h>
8#include <ddk/protocol/platform-defs.h>
9#include <soc/aml-s912/s912-hw.h>
10#include <soc/aml-s912/s912-gpio.h>
11#include "vim.h"
12
13// DMC MMIO for display driver
14static pbus_mmio_t vim_display_mmios[] = {
15    {
16        .base =     S912_PRESET_BASE,
17        .length =   S912_PRESET_LENGTH,
18    },
19    {
20        .base =     S912_HDMITX_BASE,
21        .length =   S912_HDMITX_LENGTH,
22    },
23    {
24        .base =     S912_HIU_BASE,
25        .length =   S912_HIU_LENGTH,
26    },
27    {
28        .base =     S912_VPU_BASE,
29        .length =   S912_VPU_LENGTH,
30    },
31    {
32        .base =     S912_HDMITX_SEC_BASE,
33        .length =   S912_HDMITX_SEC_LENGTH,
34    },
35    {
36        .base =     S912_DMC_REG_BASE,
37        .length =   S912_DMC_REG_LENGTH,
38    },
39    {
40        .base =     S912_CBUS_REG_BASE,
41        .length =   S912_CBUS_REG_LENGTH,
42    },
43    {
44        .base =     S912_AUDOUT_BASE,
45        .length =   S912_AUDOUT_LEN,
46    },
47};
48
49const pbus_gpio_t vim_display_gpios[] = {
50    {
51        // HPD
52        .gpio = S912_GPIOH(0),
53    },
54};
55
56static const pbus_irq_t vim_display_irqs[] = {
57    {
58        .irq = S912_VIU1_VSYNC_IRQ,
59        .mode = ZX_INTERRUPT_MODE_EDGE_HIGH,
60    },
61};
62
63static const pbus_bti_t vim_display_btis[] = {
64    {
65        .iommu_index = 0,
66        .bti_id = BTI_DISPLAY,
67    },
68    {
69        .iommu_index = 0,
70        .bti_id = BTI_AUDIO,
71    },
72};
73
74static const uint32_t vim_display_protocols[] = {
75    ZX_PROTOCOL_AMLOGIC_CANVAS,
76};
77
78static const pbus_dev_t display_dev = {
79    .name = "display",
80    .vid = PDEV_VID_KHADAS,
81    .pid = PDEV_PID_VIM2,
82    .did = PDEV_DID_VIM_DISPLAY,
83    .mmios = vim_display_mmios,
84    .mmio_count = countof(vim_display_mmios),
85    .gpios = vim_display_gpios,
86    .gpio_count = countof(vim_display_gpios),
87    .irqs = vim_display_irqs,
88    .irq_count = countof(vim_display_irqs),
89    .btis = vim_display_btis,
90    .bti_count = countof(vim_display_btis),
91    .protocols = vim_display_protocols,
92    .protocol_count = countof(vim_display_protocols),
93};
94
95zx_status_t vim_display_init(vim_bus_t* bus) {
96    zx_status_t status;
97
98    // enable this #if 0 in order to enable the SPDIF out pin for VIM2 (GPIO H4, pad M22)
99#if 0
100    gpio_set_alt_function(&bus->gpio, S912_SPDIF_H4, S912_SPDIF_H4_OUT_FN);
101#endif
102
103    if ((status = pbus_device_add(&bus->pbus, &display_dev)) != ZX_OK) {
104        zxlogf(ERROR, "vim_display_init: pbus_device_add() failed for display: %d\n", status);
105        return status;
106    }
107
108    return ZX_OK;
109}
110