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 <ddk/protocol/platform-device.h>
8#include <fbl/macros.h>
9#include <fbl/ref_counted.h>
10#include <fbl/ref_ptr.h>
11#include <lib/zx/vmo.h>
12#include <zircon/compiler.h>
13#include <zircon/types.h>
14
15namespace audio {
16namespace vim2 {
17
18class Registers : public fbl::RefCounted<Registers> {
19  public:
20    static fbl::RefPtr<Registers> Create(const platform_device_protocol_t* pdev,
21                                         uint32_t which_mmio,
22                                         zx_status_t* out_res);
23    DISALLOW_COPY_ASSIGN_AND_MOVE(Registers);
24
25    bool valid() const { return base_ != nullptr; }
26
27    volatile uint32_t& operator[](uint32_t r) const { return base_[r]; }
28    void SetBits(uint32_t r, uint32_t bits) const { ModBits(r, bits, bits); }
29    void ClrBits(uint32_t r, uint32_t bits) const { ModBits(r, bits, 0u); }
30    void ModBits(uint32_t r, uint32_t mask, uint32_t bits) const {
31        (*this)[r] = ((*this)[r] & ~mask) | (bits & mask);
32    }
33
34  private:
35    friend class fbl::RefPtr<Registers>;
36
37    Registers() = default;
38    ~Registers();
39
40    zx_status_t Map(const platform_device_protocol_t* pdev, uint32_t which_mmio);
41
42    io_buffer_t buf_ = {};
43    volatile uint32_t* base_ = nullptr;
44};
45
46class RefCountedVmo : public fbl::RefCounted<RefCountedVmo>{
47public:
48    static fbl::RefPtr<RefCountedVmo> Create(zx::vmo vmo);
49    DISALLOW_COPY_ASSIGN_AND_MOVE(RefCountedVmo);
50
51    const zx::vmo& vmo() { return vmo_; }
52
53private:
54    friend class fbl::RefPtr<RefCountedVmo>;
55
56    explicit RefCountedVmo(zx::vmo vmo) : vmo_(fbl::move(vmo)) { }
57    ~RefCountedVmo() = default;
58
59    const zx::vmo vmo_;
60};
61
62}  // namespace vim2
63}  // namespace audio
64