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/driver.h>
8#include <ddk/protocol/gpio.h>
9#include <ddktl/device-internal.h>
10#include <zircon/assert.h>
11
12#include "gpio-internal.h"
13
14// DDK GPIO protocol support.
15//
16// :: Proxies ::
17//
18// ddk::GpioProtocolProxy is a simple wrappers around gpio_protocol_t. It does
19// not own the pointers passed to it.
20//
21// :: Mixins ::
22//
23// ddk::GpioProtocol is a mixin class that simplifies writing DDK drivers that
24// implement the GPIO protocol.
25//
26// :: Examples ::
27//
28// // A driver that implements a ZX_PROTOCOL_GPIO device.
29// class GpioDevice;
30// using GpioDeviceType = ddk::Device<GpioDevice, /* ddk mixins */>;
31//
32// class GpioDevice : public GpioDeviceType,
33//                    public ddk::GpioProtocol<GpioDevice> {
34//   public:
35//     GpioDevice(zx_device_t* parent)
36//       : GpioDeviceType("my-gpio-device", parent) {}
37//
38//     zx_status_t GpioConfigIn(uint32_t flags);
39//     zx_status_t GpioConfigOut(uint8_t initial_value);
40//     zx_status_t GpioSetAltFunction(uint64_t function);
41//     zx_status_t GpioRead(uint8_t* out_value);
42//     zx_status_t GpioWrite(uint8_t value);
43//     zx_status_t GpioGetInterrupt(uint32_t flags, zx_handle_t *out_handle);
44//     zx_status_t GpioReleaseInterrupt();
45//     zx_status_t GpioSetPolarity(uint32_t polarity);
46//     ...
47// };
48
49namespace ddk {
50
51template <typename D>
52class GpioProtocol : public internal::base_protocol {
53public:
54    GpioProtocol() {
55        internal::CheckGpioProtocolSubclass<D>();
56        ops_.config_in = GpioConfigIn;
57        ops_.config_out = GpioConfigOut;
58        ops_.set_alt_function = GpioSetAltFunction;
59        ops_.read = GpioRead;
60        ops_.write = GpioWrite;
61        ops_.get_interrupt = GpioGetInterrupt;
62        ops_.release_interrupt = GpioReleaseInterrupt;
63        ops_.set_polarity = GpioSetPolarity;
64
65        // Can only inherit from one base_protocol implemenation
66        ZX_ASSERT(ddk_proto_id_ == 0);
67        ddk_proto_id_ = ZX_PROTOCOL_GPIO;
68        ddk_proto_ops_ = &ops_;
69    }
70
71protected:
72    gpio_protocol_ops_t ops_ = {};
73
74private:
75    static zx_status_t GpioConfigIn(void* ctx, uint32_t flags) {
76        return static_cast<D*>(ctx)->GpioConfigIn(flags);
77    }
78    static zx_status_t GpioConfigOut(void* ctx, uint8_t initial_value) {
79        return static_cast<D*>(ctx)->GpioConfigOut(initial_value);
80    }
81    static zx_status_t GpioSetAltFunction(void* ctx, uint64_t function) {
82        return static_cast<D*>(ctx)->GpioSetAltFunction(function);
83    }
84    static zx_status_t GpioRead(void* ctx, uint8_t* out_value) {
85        return static_cast<D*>(ctx)->GpioRead(out_value);
86    }
87    static zx_status_t GpioWrite(void* ctx, uint8_t value) {
88        return static_cast<D*>(ctx)->GpioWrite(value);
89    }
90    static zx_status_t GpioGetInterrupt(void* ctx, uint32_t flags,
91                                        zx_handle_t* out_handle) {
92        return static_cast<D*>(ctx)->GpioGetInterrupt(flags, out_handle);
93    }
94    static zx_status_t GpioReleaseInterrupt(void* ctx) {
95        return static_cast<D*>(ctx)->GpioReleaseInterrupt();
96    }
97    static zx_status_t GpioSetPolarity(void* ctx, uint32_t polarity) {
98        return static_cast<D*>(ctx)->GpioSetPolarity(polarity);
99    }
100};
101
102class GpioProtocolProxy {
103public:
104    GpioProtocolProxy(gpio_protocol_t* proto)
105        : ops_(proto->ops), ctx_(proto->ctx) {}
106
107    void GetProto(gpio_protocol_t* proto) {
108        proto->ctx = ctx_;
109        proto->ops = ops_;
110    }
111
112    zx_status_t ConfigIn(uint32_t flags) {
113        return ops_->config_in(ctx_, flags);
114    }
115    zx_status_t ConfigOut(uint8_t initial_value) {
116        return ops_->config_out(ctx_, initial_value);
117    }
118    zx_status_t SetAltFunction(uint64_t function) {
119        return ops_->set_alt_function(ctx_, function);
120    }
121    zx_status_t Read(uint8_t* out_value) {
122        return ops_->read(ctx_, out_value);
123    }
124    zx_status_t Write(uint8_t value) {
125        return ops_->write(ctx_, value);
126    }
127    zx_status_t GetInterrupt(uint32_t flags, zx_handle_t* out_handle) {
128        return ops_->get_interrupt(ctx_, flags, out_handle);
129    }
130    zx_status_t ReleaseInterrupt() {
131        return ops_->release_interrupt(ctx_);
132    }
133    zx_status_t SetPolarity(uint32_t polarity) {
134        return ops_->set_polarity(ctx_, polarity);
135    }
136
137private:
138    gpio_protocol_ops_t* ops_;
139    void* ctx_;
140};
141
142} // namespace ddk
143