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 <ddktl/pdev.h>
8 #include <dispatcher-pool/dispatcher-timer.h>
9 #include <lib/fzl/pinned-vmo.h>
10 #include <lib/simple-audio-stream/simple-audio-stream.h>
11 #include <lib/zx/bti.h>
12 #include <lib/zx/vmo.h>
13 
14 #include <audio-proto/audio-proto.h>
15 #include <soc/aml-common/aml-pdm-audio.h>
16 
17 #include <optional>
18 
19 namespace audio {
20 namespace astro {
21 
22 class AstroAudioStreamIn : public SimpleAudioStream {
23 
24 public:
25     AstroAudioStreamIn(zx_device_t* parent);
26 
27 protected:
28     zx_status_t Init() __TA_REQUIRES(domain_->token()) override;
29     zx_status_t ChangeFormat(const audio_proto::StreamSetFmtReq& req)
30         __TA_REQUIRES(domain_->token()) override;
31     zx_status_t GetBuffer(const audio_proto::RingBufGetBufferReq& req,
32                           uint32_t* out_num_rb_frames,
33                           zx::vmo* out_buffer) __TA_REQUIRES(domain_->token()) override;
34     zx_status_t Start(uint64_t* out_start_time) __TA_REQUIRES(domain_->token()) override;
35     zx_status_t Stop() __TA_REQUIRES(domain_->token()) override;
36     zx_status_t InitPost() override;
37 
38 private:
39     friend class fbl::RefPtr<AstroAudioStreamIn>;
40 
41     zx_status_t AddFormats() __TA_REQUIRES(domain_->token());
42     zx_status_t InitBuffer(size_t size);
43     zx_status_t InitPDev();
44     zx_status_t ProcessRingNotification();
45 
46     uint32_t us_per_notification_ = 0;
47 
48     fbl::RefPtr<dispatcher::Timer> notify_timer_;
49 
50     std::optional<ddk::PDev> pdev_;
51 
52     zx::vmo ring_buffer_vmo_;
53     fzl::PinnedVmo pinned_ring_buffer_;
54 
55     fbl::unique_ptr<AmlPdmDevice> pdm_;
56 
57     zx::bti bti_;
58 };
59 } //namespace astro
60 } //namespace audio
61