[ddk][serial] Use new protocol methods to get fragments

Preference towards lookup via name string is given, but in some cases,
the relationship is not static and we must continue to rely on indicies.

Change-Id: Ia88fbe1daeb434a9034d8c705500557be021c89b
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/438683
Reviewed-by: Brian Bosak <bbosak@google.com>
Testability-Review: Suraj Malhotra <surajmalhotra@google.com>
Commit-Queue: Suraj Malhotra <surajmalhotra@google.com>
diff --git a/src/devices/serial/drivers/aml-uart/aml-uart-test.cc b/src/devices/serial/drivers/aml-uart/aml-uart-test.cc
index 8cf2721..a4ac773 100644
--- a/src/devices/serial/drivers/aml-uart/aml-uart-test.cc
+++ b/src/devices/serial/drivers/aml-uart/aml-uart-test.cc
@@ -235,7 +235,7 @@
     info.serial_class = fuchsia_hardware_serial_Class_BLUETOOTH_HCI;
     info.serial_vid = PDEV_VID_BROADCOM;
     info.serial_pid = PDEV_PID_BCM43458;
-    auto uart = std::make_unique<serial::AmlUart>(fake_ddk::kFakeParent, *pdev_.proto(), info,
+    auto uart = std::make_unique<serial::AmlUart>(fake_ddk::kFakeParent, pdev_.proto(), info,
                                                   ddk::MmioBuffer(pdev_.GetMmio()));
     zx_status_t status = uart->Init();
     ASSERT_OK(status);
diff --git a/src/devices/serial/drivers/aml-uart/aml-uart.cc b/src/devices/serial/drivers/aml-uart/aml-uart.cc
index 559e407..857f044 100644
--- a/src/devices/serial/drivers/aml-uart/aml-uart.cc
+++ b/src/devices/serial/drivers/aml-uart/aml-uart.cc
@@ -41,22 +41,11 @@
     return ZX_ERR_NOT_SUPPORTED;
   }
 
-  zx_device_t* fragments[1];
-  size_t fragment_count;
-  composite.GetFragments(fragments, std::size(fragments), &fragment_count);
-  // Only pdev fragment is required.
-  if (fragment_count < 1) {
-    zxlogf(ERROR, "AmlUart: Could not get fragments");
-    return ZX_ERR_NOT_SUPPORTED;
-  }
-
-  ddk::PDev pdev(fragments[0]);
+  ddk::PDev pdev(composite);
   if (!pdev.is_valid()) {
     zxlogf(ERROR, "AmlUart::Create: Could not get pdev");
     return ZX_ERR_NO_RESOURCES;
   }
-  pdev_protocol_t proto;
-  pdev.GetProto(&proto);
 
   serial_port_info_t info;
   size_t actual;
@@ -71,15 +60,15 @@
     return ZX_ERR_INTERNAL;
   }
 
-  mmio_buffer_t mmio;
-  status = pdev_map_mmio_buffer(&proto, 0, ZX_CACHE_POLICY_UNCACHED_DEVICE, &mmio);
+  std::optional<ddk::MmioBuffer> mmio;
+  status = pdev.MapMmio(0, &mmio);
   if (status != ZX_OK) {
     zxlogf(ERROR, "%s: pdev_map_&mmio__buffer failed %d", __func__, status);
     return status;
   }
 
   fbl::AllocChecker ac;
-  auto* uart = new (&ac) AmlUart(parent, proto, info, ddk::MmioBuffer(mmio));
+  auto* uart = new (&ac) AmlUart(parent, pdev, info, *std::move(mmio));
   if (!ac.check()) {
     return ZX_ERR_NO_MEMORY;
   }
@@ -310,7 +299,7 @@
   fbl::AutoLock al(&enable_lock_);
 
   if (enable && !enabled_) {
-    zx_status_t status = pdev_get_interrupt(&pdev_, 0, 0, irq_.reset_and_get_address());
+    zx_status_t status = pdev_.GetInterrupt(0, &irq_);
     if (status != ZX_OK) {
       zxlogf(ERROR, "%s: pdev_get_interrupt failed %d", __func__, status);
       return status;
diff --git a/src/devices/serial/drivers/aml-uart/aml-uart.h b/src/devices/serial/drivers/aml-uart/aml-uart.h
index 266ee25..7ed233f 100644
--- a/src/devices/serial/drivers/aml-uart/aml-uart.h
+++ b/src/devices/serial/drivers/aml-uart/aml-uart.h
@@ -5,7 +5,7 @@
 #ifndef SRC_DEVICES_SERIAL_DRIVERS_AML_UART_AML_UART_H_
 #define SRC_DEVICES_SERIAL_DRIVERS_AML_UART_AML_UART_H_
 
-#include <lib/device-protocol/platform-device.h>
+#include <lib/device-protocol/pdev.h>
 #include <lib/fit/function.h>
 #include <lib/mmio/mmio.h>
 #include <lib/zircon-internal/thread_annotations.h>
@@ -53,7 +53,7 @@
 
   zx_status_t Init();
 
-  explicit AmlUart(zx_device_t* parent, const pdev_protocol_t& pdev,
+  explicit AmlUart(zx_device_t* parent, const ddk::PDev& pdev,
                    const serial_port_info_t& serial_port_info, ddk::MmioBuffer mmio)
       : DeviceType(parent),
         pdev_(pdev),
@@ -77,7 +77,7 @@
   fit::closure MakeReadCallbackLocked(zx_status_t status, void* buf, size_t len) TA_REQ(read_lock_);
   fit::closure MakeWriteCallbackLocked(zx_status_t status) TA_REQ(write_lock_);
 
-  const pdev_protocol_t pdev_;
+  ddk::PDev pdev_;
   const serial_port_info_t serial_port_info_;
   ddk::MmioBuffer mmio_;
   zx::interrupt irq_;