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 #pragma once
6 
7 #include <unistd.h>
8 #include <zircon/compiler.h>
9 #include <ddk/protocol/platform/device.h>
10 #include <ddk/protocol/platform-device-lib.h>
11 #include <ddktl/mmio.h>
12 
13 #include "common.h"
14 #include "dw-mipi-dsi-reg.h"
15 #include "mipi-dsi.h"
16 #include <optional>
17 
18 // Assigned Virtual Channel ID for Astro
19 // TODO(payamm): Will need to generate and maintain VCID for multi-display
20 // solutions
21 
22 #define MIPI_DSI_VIRTUAL_CHAN_ID                (0)
23 
24 namespace astro_display {
25 
26 class DwMipiDsi {
27 public:
DwMipiDsi()28     DwMipiDsi() {}
29     zx_status_t Init(zx_device_t* parent);
30     zx_status_t Cmd(const uint8_t* tbuf, size_t tlen, uint8_t* rbuf, size_t rlen, bool is_dcs);
31 
32 private:
33     inline bool IsPldREmpty();
34     inline bool IsPldRFull();
35     inline bool IsPldWEmpty();
36     inline bool IsPldWFull();
37     inline bool IsCmdEmpty();
38     inline bool IsCmdFull();
39     zx_status_t WaitforFifo(uint32_t reg, uint32_t bit, uint32_t val);
40     zx_status_t WaitforPldWNotFull();
41     zx_status_t WaitforPldWEmpty();
42     zx_status_t WaitforPldRFull();
43     zx_status_t WaitforPldRNotEmpty();
44     zx_status_t WaitforCmdNotFull();
45     zx_status_t WaitforCmdEmpty();
46     void DumpCmd(const MipiDsiCmd& cmd);
47     zx_status_t GenericPayloadRead(uint32_t* data);
48     zx_status_t GenericHdrWrite(uint32_t data);
49     zx_status_t GenericPayloadWrite(uint32_t data);
50     void EnableBta();
51     void DisableBta();
52     zx_status_t WaitforBtaAck();
53     zx_status_t GenWriteShort(const MipiDsiCmd& cmd);
54     zx_status_t DcsWriteShort(const MipiDsiCmd& cmd);
55     zx_status_t GenWriteLong(const MipiDsiCmd& cmd);
56     zx_status_t GenRead(const MipiDsiCmd& cmd);
57     zx_status_t SendCmd(const MipiDsiCmd& cmd);
58 
59     std::optional<ddk::MmioBuffer>          mipi_dsi_mmio_;
60     pdev_protocol_t              pdev_ = {nullptr, nullptr};
61 
62     bool                                    initialized_ = false;
63 };
64 
65 } // namespace astro_display
66