libplatsupport: hifive ltimer refactor

- Introduce two new helper functions
  `helper_fdt_alloc_simple`, `create_ltimer_simple`
  These are only used when there's 1 reg and 1 irq
  to register.
- Refactor hifive timers to use these helper functions.
- Nit: init time_h explicitly instead of being calloc'd
diff --git a/libplatsupport/CMakeLists.txt b/libplatsupport/CMakeLists.txt
index c1e67d2..a6d845b 100644
--- a/libplatsupport/CMakeLists.txt
+++ b/libplatsupport/CMakeLists.txt
@@ -65,11 +65,7 @@
     list(APPEND deps src/arch/x86/tsc.c)
 elseif(${KernelArch} STREQUAL "riscv")
     list(APPEND deps src/arch/riscv/irqchip/plic.c)
-    list(
-        APPEND
-            irqchip_modules
-            "-Wl,--undefined=riscv_plic_ptr"
-    )
+    list(APPEND irqchip_modules "-Wl,--undefined=riscv_plic_ptr")
 endif()
 
 if(KernelPlatformQEMUArmVirt)
diff --git a/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h b/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h
index 6ee8f3a..7af74c8 100644
--- a/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h
+++ b/libplatsupport/plat_include/hifive/platsupport/plat/pwm.h
@@ -20,18 +20,14 @@
 
 /* The input frequence is 0.5 of the CPU frequency which is 1GHz by default */
 #define PWM_INPUT_FREQ (500*1000*1000)
-#define PWM0_PADDR   0x10020000
-#define PWM1_PADDR   0x10021000
 
-#define PWM0_INTERRUPT0 42
-#define PWM0_INTERRUPT1 43
-#define PWM0_INTERRUPT2 44
-#define PWM0_INTERRUPT3 45
+#define PWM0_PATH "/soc/pwm@10020000"
+#define PWM1_PATH "/soc/pwm@10021000"
 
-#define PWM1_INTERRUPT0 46
-#define PWM1_INTERRUPT1 47
-#define PWM1_INTERRUPT2 48
-#define PWM1_INTERRUPT3 49
+/* There is only one register map available in device tree */
+#define PWM_REG_CHOICE 0
+/* There are four interrupts for matches available, use the first one */
+#define PWM_IRQ_CHOICE 0
 
 /**
  * When used in UPCOUNTER, pwm_handle_irq needs to be called on each interrupt
@@ -40,13 +36,14 @@
  * if pwm_get_time is called before pwm_handle_irq the time could be incorrect.
  */
 typedef enum PWM_MODE {
-	TIMEOUT,
-	UPCOUNTER,
+    TIMEOUT,
+    UPCOUNTER,
 } pwm_mode_t;
 
 typedef struct {
-    /* vaddr pwm is mapped to */
-    void *vaddr;
+    char *fdt_path;
+    ltimer_callback_fn_t user_cb_fn;
+    void *user_cb_token;
     pwm_mode_t mode;
 } pwm_config_t;
 
@@ -67,9 +64,19 @@
 };
 
 typedef struct pwm {
-    volatile struct pwm_map *pwm_map;
-    uint64_t time_h;
+    /* set in init */
+    ps_io_ops_t ops;
     pwm_mode_t mode;
+    ltimer_callback_fn_t user_cb_fn;
+    void *user_cb_token;
+
+    /* set in fdt helper */
+    volatile struct pwm_map *pwm_map;
+    pmem_region_t pmem;
+    irq_id_t irq_id;
+
+    /* set in setup */
+    uint64_t time_h;
 } pwm_t;
 
 static UNUSED timer_properties_t pwm_properties = {
@@ -82,9 +89,9 @@
     .timeouts = true,
 };
 
-int pwm_start(pwm_t *pwm);
-int pwm_stop(pwm_t *pwm);
-void pwm_handle_irq(pwm_t *pwm, uint32_t irq);
+void pwm_start(pwm_t *pwm);
+void pwm_stop(pwm_t *pwm);
 uint64_t pwm_get_time(pwm_t *pwm);
 int pwm_set_timeout(pwm_t *pwm, uint64_t ns, bool periodic);
-int pwm_init(pwm_t *pwm, pwm_config_t config);
+int pwm_init(pwm_t *pwm, ps_io_ops_t ops, pwm_config_t config);
+void pwm_destroy(pwm_t *pwm);
diff --git a/libplatsupport/src/fdt.c b/libplatsupport/src/fdt.c
index e5af135..dbe4243 100644
--- a/libplatsupport/src/fdt.c
+++ b/libplatsupport/src/fdt.c
@@ -298,7 +298,7 @@
 static int irq_index_helper_walker(ps_irq_t irq, unsigned curr_num, size_t num_irqs, void *token)
 {
     index_helper_token_t *helper_token = token;
-    if (helper_token->desired_offset > num_irqs) {
+    if (helper_token->desired_offset >= num_irqs) {
         /* Bail early if we will never find it */
         return -ENOENT;
     }
@@ -338,6 +338,7 @@
 
     int error = ps_fdt_walk_irqs(&io_ops->io_fdt, cookie, irq_index_helper_walker, &token);
     if (error) {
+        assert(error <= PS_INVALID_IRQ_ID);
         return error;
     }
 
diff --git a/libplatsupport/src/ltimer.h b/libplatsupport/src/ltimer.h
index c6e6ee1..5651d07 100644
--- a/libplatsupport/src/ltimer.h
+++ b/libplatsupport/src/ltimer.h
@@ -12,6 +12,7 @@
 
 #include <platsupport/ltimer.h>
 #include <platsupport/irq.h>
+#include <platsupport/fdt.h>
 
 /* Per-platform ltimer IRQ handling function type */
 typedef int (*ltimer_handle_irq_fn_t)(void *data, ps_irq_t *irq);
@@ -45,3 +46,89 @@
     error = acknowledge_fn(ack_data);
     assert(!error);
 }
+
+static int helper_fdt_alloc_simple(
+    ps_io_ops_t *ops, char *fdt_path,
+    unsigned reg_choice, unsigned irq_choice,
+    void **vmap, pmem_region_t *pmem, irq_id_t *irq_id,
+    irq_callback_fn_t handler, void *handler_token
+)
+{
+    assert(fdt_path != NULL);
+    assert(vmap != NULL && pmem != NULL && irq_id != NULL);
+
+    int error;
+    void *temp_vmap;
+    pmem_region_t temp_pmem;
+    irq_id_t temp_irq_id;
+
+    *vmap = NULL;
+    *irq_id = PS_INVALID_IRQ_ID;
+
+    /* Gather FDT info */
+    ps_fdt_cookie_t *cookie = NULL;
+    error = ps_fdt_read_path(&ops->io_fdt, &ops->malloc_ops, fdt_path, &cookie);
+    if (error) {
+        ZF_LOGE("Simple FDT helper failed to read path (%d, %s)", error, fdt_path);
+        return error;
+    }
+
+    temp_vmap = ps_fdt_index_map_register(ops, cookie, reg_choice, &temp_pmem);
+    if (temp_vmap == NULL) {
+        ZF_LOGE("Simple FDT helper failed to map registers");
+        return ENODEV;
+    }
+
+    temp_irq_id = ps_fdt_index_register_irq(ops, cookie, irq_choice, handler, handler_token);
+    if (temp_irq_id <= PS_INVALID_IRQ_ID) {
+        ZF_LOGE("Simple FDT helper failed to register irqs (%d)", temp_irq_id);
+        return temp_irq_id;
+    }
+
+    error = ps_fdt_cleanup_cookie(&ops->malloc_ops, cookie);
+    if (error) {
+        ZF_LOGE("Simple FDT helper failed to clean up cookie (%d)", error);
+        return error;
+    }
+
+    *vmap = temp_vmap;
+    *pmem = temp_pmem;
+    *irq_id = temp_irq_id;
+
+    return 0;
+}
+
+static int get_resolution_dummy(void *data, uint64_t *resolution)
+{
+    return ENOSYS;
+}
+
+static int create_ltimer_simple(
+    ltimer_t *ltimer, ps_io_ops_t ops, size_t sz,
+    int (*get_time)(void *data, uint64_t *time),
+    int (*set_timeout)(void *data, uint64_t ns, timeout_type_t type),
+    int (*reset)(void *data),
+    void (*destroy)(void *data)
+)
+{
+    assert(ltimer != NULL);
+
+    ltimer->get_time = get_time;
+    ltimer->get_resolution = get_resolution_dummy;
+    ltimer->set_timeout = set_timeout;
+    ltimer->reset = reset;
+    ltimer->destroy = destroy;
+    ltimer->get_nth_irq = NULL;
+    ltimer->get_nth_pmem = NULL;
+    ltimer->get_num_irqs = NULL;
+    ltimer->get_num_pmems = NULL;
+
+    int error = ps_calloc(&ops.malloc_ops, 1, sz, &ltimer->data);
+    if (error) {
+        ZF_LOGE("Unable to allocate ltimer data");
+        return error;
+    }
+    assert(ltimer->data != NULL);
+
+    return 0;
+}
diff --git a/libplatsupport/src/plat/hifive/ltimer.c b/libplatsupport/src/plat/hifive/ltimer.c
index b0ccbb7..5614886 100644
--- a/libplatsupport/src/plat/hifive/ltimer.c
+++ b/libplatsupport/src/plat/hifive/ltimer.c
@@ -9,10 +9,6 @@
  *
  * @TAG(DATA61_BSD)
  */
-/* Implementation of a logical timer for HiFive Unleashed platform.
- *
- * We use two pwms: one for the time and the other for timeouts.
- */
 #include <platsupport/timer.h>
 #include <platsupport/ltimer.h>
 #include <platsupport/plat/pwm.h>
@@ -21,109 +17,26 @@
 
 #include "../../ltimer.h"
 
-enum {
-    COUNTER_TIMER,
-    TIMEOUT_TIMER,
-    NUM_TIMERS
-};
+/* Implementation of a logical timer for HiFive Unleashed platform.
+ *
+ * We use two pwms: one for the time and the other for timeouts.
+ *
+ * De-multiplex common interface operations into two separate timers.
+ */
 
 typedef struct {
-    pwm_t pwm;
-    void *vaddr;
-} pwm_ltimer_t;
-
-typedef struct {
-    pwm_ltimer_t pwm_ltimers[NUM_TIMERS];
-    irq_id_t timer_irq_ids[NUM_TIMERS];
-    timer_callback_data_t callback_datas[NUM_TIMERS];
-    ltimer_callback_fn_t user_callback;
-    void *user_callback_token;
     ps_io_ops_t ops;
+    pwm_t pwm_counter;
+    pwm_t pwm_timeout;
 } hifive_timers_t;
 
-static ps_irq_t irqs[] = {
-    {
-        .type = PS_INTERRUPT,
-        .irq.number = PWM0_INTERRUPT0
-
-    },
-    {
-        .type = PS_INTERRUPT,
-        .irq.number = PWM1_INTERRUPT0
-    },
-};
-
-static pmem_region_t pmems[] = {
-    {
-        .type = PMEM_TYPE_DEVICE,
-        .base_addr = PWM0_PADDR,
-        .length = PAGE_SIZE_4K
-    },
-    {
-        .type = PMEM_TYPE_DEVICE,
-        .base_addr = PWM1_PADDR,
-        .length = PAGE_SIZE_4K
-    }
-};
-
-#define N_IRQS ARRAY_SIZE(irqs)
-#define N_PMEMS ARRAY_SIZE(pmems)
-
- size_t get_num_irqs(void *data)
-{
-    return N_IRQS;
-}
-
-static int get_nth_irq(void *data, size_t n, ps_irq_t *irq)
-{
-    assert(n < N_IRQS);
-
-    *irq = irqs[n];
-    return 0;
-}
-
-static size_t get_num_pmems(void *data)
-{
-    return N_PMEMS;
-}
-
-static int get_nth_pmem(void *data, size_t n, pmem_region_t *paddr)
-{
-    assert(n < N_PMEMS);
-    *paddr = pmems[n];
-    return 0;
-}
-
-static int ltimer_handle_irq(void *data, ps_irq_t *irq)
-{
-    assert(data != NULL);
-    hifive_timers_t *timers = data;
-    long irq_number = irq->irq.number;
-    ltimer_event_t event;
-    if (irq_number == PWM0_INTERRUPT0) {
-        pwm_handle_irq(&timers->pwm_ltimers[COUNTER_TIMER].pwm, irq->irq.number);
-        event = LTIMER_OVERFLOW_EVENT;
-    } else if (irq_number == PWM1_INTERRUPT0) {
-        pwm_handle_irq(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, irq->irq.number);
-        event = LTIMER_TIMEOUT_EVENT;
-    } else {
-        ZF_LOGE("Invalid IRQ number: %d received.\n", irq_number);
-    }
-
-    if (timers->user_callback) {
-        timers->user_callback(timers->user_callback_token, event);
-    }
-
-    return 0;
-}
-
 static int get_time(void *data, uint64_t *time)
 {
     assert(data != NULL);
     assert(time != NULL);
     hifive_timers_t *timers = data;
 
-    *time = pwm_get_time(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
+    *time = pwm_get_time(&timers->pwm_counter);
     return 0;
 }
 
@@ -139,16 +52,16 @@
 
     switch (type) {
     case TIMEOUT_ABSOLUTE: {
-        uint64_t time = pwm_get_time(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
+        uint64_t time = pwm_get_time(&timers->pwm_counter);
         if (time >= ns) {
             return ETIME;
         }
-        return pwm_set_timeout(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, ns - time, false);
+        return pwm_set_timeout(&timers->pwm_timeout, ns - time, false);
     }
     case TIMEOUT_RELATIVE:
-        return pwm_set_timeout(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, ns, false);
+        return pwm_set_timeout(&timers->pwm_timeout, ns, false);
     case TIMEOUT_PERIODIC:
-        return pwm_set_timeout(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, ns, true);
+        return pwm_set_timeout(&timers->pwm_timeout, ns, true);
     }
 
     return EINVAL;
@@ -158,113 +71,67 @@
 {
     assert(data != NULL);
     hifive_timers_t *timers = data;
-    pwm_stop(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
-    pwm_start(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
-    pwm_stop(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm);
-    pwm_start(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm);
+    pwm_stop(&timers->pwm_counter);
+    pwm_stop(&timers->pwm_timeout);
+    pwm_start(&timers->pwm_counter);
+    pwm_start(&timers->pwm_timeout);
     return 0;
 }
 
 static void destroy(void *data)
 {
-    assert(data);
+    assert(data != NULL);
     hifive_timers_t *timers = data;
-    for (int i = 0; i < NUM_TIMERS; i++) {
-        if (timers->pwm_ltimers[i].vaddr) {
-            pwm_stop(&timers->pwm_ltimers[i].pwm);
-            ps_pmem_unmap(&timers->ops, pmems[i], timers->pwm_ltimers[i].vaddr);
-        }
-        if (timers->timer_irq_ids[i] > PS_INVALID_IRQ_ID) {
-            int error = ps_irq_unregister(&timers->ops.irq_ops, timers->timer_irq_ids[i]);
-            ZF_LOGF_IF(error, "Failed to unregister IRQ");
-        }
-    }
-    ps_free(&timers->ops.malloc_ops, sizeof(timers), timers);
-}
-
-static int create_ltimer(ltimer_t *ltimer, ps_io_ops_t ops)
-{
-    assert(ltimer != NULL);
-    ltimer->get_time = get_time;
-    ltimer->get_resolution = get_resolution;
-    ltimer->set_timeout = set_timeout;
-    ltimer->reset = reset;
-    ltimer->destroy = destroy;
-
-    int error = ps_calloc(&ops.malloc_ops, 1, sizeof(hifive_timers_t), &ltimer->data);
-    if (error) {
-        return error;
-    }
-    assert(ltimer->data != NULL);
-
-    return 0;
-}
-
-static int init_ltimer(ltimer_t *ltimer)
-{
-    assert(ltimer != NULL);
-    hifive_timers_t *timers = ltimer->data;
-
-    /* setup pwm */
-    pwm_config_t config_counter = {
-        .vaddr = timers->pwm_ltimers[COUNTER_TIMER].vaddr,
-        .mode = UPCOUNTER,
-    };
-    pwm_config_t config_timeout = {
-        .vaddr = timers->pwm_ltimers[TIMEOUT_TIMER].vaddr,
-        .mode = TIMEOUT,
-    };
-
-    pwm_init(&timers->pwm_ltimers[COUNTER_TIMER].pwm, config_counter);
-    pwm_init(&timers->pwm_ltimers[TIMEOUT_TIMER].pwm, config_timeout);
-    pwm_start(&timers->pwm_ltimers[COUNTER_TIMER].pwm);
-    return 0;
+    pwm_destroy(&timers->pwm_counter);
+    pwm_destroy(&timers->pwm_timeout);
+    ps_free(&timers->ops.malloc_ops, sizeof(hifive_timers_t), timers);
 }
 
 int ltimer_default_init(ltimer_t *ltimer, ps_io_ops_t ops, ltimer_callback_fn_t callback, void *callback_token)
 {
+    int error;
 
-    int error = ltimer_default_describe(ltimer, ops);
+    error = create_ltimer_simple(
+                ltimer, ops, sizeof(hifive_timers_t),
+                get_time, set_timeout, reset, destroy
+            );
     if (error) {
-        return error;
-    }
-
-    error = create_ltimer(ltimer, ops);
-    if (error) {
+        /* failed to create, no need to destroy */
+        ZF_LOGE("Failed to create ltimer using simple helper");
         return error;
     }
 
     hifive_timers_t *timers = ltimer->data;
     timers->ops = ops;
-    timers->user_callback = callback;
-    timers->user_callback_token = callback_token;
-    for (int i = 0; i < NUM_TIMERS; i++) {
-        timers->timer_irq_ids[i] = PS_INVALID_IRQ_ID;
-    }
 
-    for (int i = 0; i < NUM_TIMERS; i++) {
-        /* map the registers we need */
-        timers->pwm_ltimers[i].vaddr = ps_pmem_map(&ops, pmems[i], false, PS_MEM_NORMAL);
-        if (timers->pwm_ltimers[i].vaddr == NULL) {
-            destroy(ltimer->data);
-            return ENOMEM;
-        }
+    pwm_config_t config = {
+        .user_cb_fn = callback,
+        .user_cb_token = callback_token,
+        .fdt_path = PWM0_PATH,
+        .mode = UPCOUNTER,
+    };
 
-        /* register the IRQs we need */
-        timers->callback_datas[i].ltimer = ltimer;
-        timers->callback_datas[i].irq = &irqs[i];
-        timers->callback_datas[i].irq_handler = ltimer_handle_irq;
-        timers->timer_irq_ids[i] = ps_irq_register(&ops.irq_ops, irqs[i], handle_irq_wrapper,
-                                                   &timers->callback_datas[i]);
-        if (timers->timer_irq_ids[i] < 0) {
-            destroy(ltimer->data);
-            return EIO;
-        }
-    }
-
-    error = init_ltimer(ltimer);
+    error = pwm_init(&timers->pwm_counter, ops, config);
     if (error) {
-        destroy(ltimer->data);
+        ZF_LOGE("Failed to init pwm timer (UPCOUNTER)");
+        destroy(timers);
+        return error;
+    }
+
+    config.fdt_path = PWM1_PATH;
+    config.mode = TIMEOUT;
+
+    error = pwm_init(&timers->pwm_timeout, ops, config);
+    if (error) {
+        ZF_LOGE("Failed to init pwm timer (TIMEOUT)");
+        destroy(timers);
+        return error;
+    }
+
+    error = reset(timers);
+    if (error) {
+        ZF_LOGE("Failed to reset pwm timers during init");
+        destroy(timers);
         return error;
     }
 
@@ -272,16 +139,10 @@
     return 0;
 }
 
+/* This function is intended to be deleted,
+ * this is just left here for now so that stuff can compile */
 int ltimer_default_describe(ltimer_t *ltimer, ps_io_ops_t ops)
 {
-    if (ltimer == NULL) {
-        ZF_LOGE("Timer is NULL!");
-        return EINVAL;
-    }
-
-    ltimer->get_num_irqs = get_num_irqs;
-    ltimer->get_nth_irq = get_nth_irq;
-    ltimer->get_num_pmems = get_num_pmems;
-    ltimer->get_nth_pmem = get_nth_pmem;
-    return 0;
+    ZF_LOGE("get_(nth/num)_(irqs/pmems) are not valid");
+    return EINVAL;
 }
diff --git a/libplatsupport/src/plat/hifive/pwm.c b/libplatsupport/src/plat/hifive/pwm.c
index 87bee7f..1dc200b 100644
--- a/libplatsupport/src/plat/hifive/pwm.c
+++ b/libplatsupport/src/plat/hifive/pwm.c
@@ -18,8 +18,10 @@
 #include <utils/util.h>
 
 #include <platsupport/timer.h>
+#include <platsupport/ltimer.h>
 #include <platsupport/plat/pwm.h>
 
+#include "../../ltimer.h"
 
 #define PWMSCALE_MASK MASK(4)
 #define PWMSTICKY BIT(8)
@@ -37,25 +39,24 @@
 #define MAX_TIMEOUT_NS (BIT(31) * (NS_IN_S / PWM_INPUT_FREQ))
 
 
-int pwm_start(pwm_t *pwm)
+void pwm_start(pwm_t *pwm)
 {
+    assert(pwm != NULL && pwm->pwm_map != NULL);
     pwm->pwm_map->pwmcfg |= PWMENALWAYS;
-    return 0;
 }
 
-int pwm_stop(pwm_t *pwm)
+void pwm_stop(pwm_t *pwm)
 {
+    assert(pwm != NULL && pwm->pwm_map != NULL);
     /* Disable timer. */
     pwm->pwm_map->pwmcmp0 = PWMCMP_MASK;
-    pwm->pwm_map->pwmcfg &= ~(PWMENALWAYS|PWMENONESHOT|PWMCMP0IP|PWMCMP1IP|PWMCMP2IP|PWMCMP3IP);
+    pwm->pwm_map->pwmcfg &= ~(PWMENALWAYS | PWMENONESHOT | PWMCMP0IP | PWMCMP1IP | PWMCMP2IP | PWMCMP3IP);
     pwm->pwm_map->pwmcount = 0;
-
-    return 0;
 }
 
 int pwm_set_timeout(pwm_t *pwm, uint64_t ns, bool periodic)
 {
-    if(pwm->mode == UPCOUNTER) {
+    if (pwm->mode == UPCOUNTER) {
         ZF_LOGE("pwm is in UPCOUNTER mode and doesn't support setting timeouts.");
         return -1;
     }
@@ -75,7 +76,7 @@
      */
     size_t prescale = num_ticks >> (PWMCMP_WIDTH);
     size_t base_2 = LOG_BASE_2(prescale);
-    if (BIT(base_2)< prescale) {
+    if (BIT(base_2) < prescale) {
         base_2++;
     }
     assert(prescale < BIT(PWMSCALE_MASK));
@@ -83,9 +84,9 @@
     /* There will be a loss of resolution by this shift.
      * We add 1 so that we sleep for at least as long as needed.
      */
-    pwm->pwm_map->pwmcmp0 = (num_ticks >> base_2) +1;
+    pwm->pwm_map->pwmcmp0 = (num_ticks >> base_2) + 1;
     /* assert we didn't overflow... */
-    assert((num_ticks >> base_2) +1);
+    assert((num_ticks >> base_2) + 1);
     /* Reset the counter mode and prescaler, for some reason this doesn't work in pwm_stop */
     pwm->pwm_map->pwmcfg &= ~(PWMSCALE_MASK);
     if (periodic) {
@@ -97,13 +98,20 @@
     return 0;
 }
 
-void pwm_handle_irq(pwm_t *pwm, uint32_t irq)
+static void pwm_handle_irq(void *data, ps_irq_acknowledge_fn_t acknowledge_fn, void *ack_data)
 {
-    if(pwm->mode == UPCOUNTER) {
+    pwm_t *pwm = data;
+    if (pwm->mode == UPCOUNTER) {
         pwm->time_h++;
     }
-
-    pwm->pwm_map->pwmcfg &= ~(PWMCMP0IP|PWMCMP1IP|PWMCMP2IP|PWMCMP3IP);
+    pwm->pwm_map->pwmcfg &= ~(PWMCMP0IP | PWMCMP1IP | PWMCMP2IP | PWMCMP3IP);
+    ZF_LOGF_IF(acknowledge_fn(ack_data), "Failed to acknowledge the timer's interrupts");
+    if (pwm->user_cb_fn) {
+        pwm->user_cb_fn(
+            pwm->user_cb_token,
+            pwm->mode == UPCOUNTER ? LTIMER_OVERFLOW_EVENT : LTIMER_TIMEOUT_EVENT
+        );
+    }
 }
 
 uint64_t pwm_get_time(pwm_t *pwm)
@@ -119,19 +127,61 @@
     return time;
 }
 
-int pwm_init(pwm_t *pwm, pwm_config_t config)
+static void pwm_setup(pwm_t *pwm)
 {
-    pwm->pwm_map = (volatile struct pwm_map*) config.vaddr;
+    assert(pwm != NULL);
+    pwm->time_h = 0;
     uint8_t scale = 0;
-    if (config.mode == UPCOUNTER) {
+    if (pwm->mode == UPCOUNTER) {
         scale = PWMSCALE_MASK;
     }
-    pwm->mode = config.mode;
     pwm->pwm_map->pwmcmp0 = PWMCMP_MASK;
     pwm->pwm_map->pwmcmp1 = PWMCMP_MASK;
     pwm->pwm_map->pwmcmp2 = PWMCMP_MASK;
     pwm->pwm_map->pwmcmp3 = PWMCMP_MASK;
-    pwm->pwm_map->pwmcfg =  (scale & PWMSCALE_MASK) | PWMZEROCMP | PWMSTICKY;
+    pwm->pwm_map->pwmcfg = (scale & PWMSCALE_MASK) | PWMZEROCMP | PWMSTICKY;
+}
+
+void pwm_destroy(pwm_t *pwm)
+{
+    int error;
+    if (pwm->irq_id != PS_INVALID_IRQ_ID) {
+        error = ps_irq_unregister(&pwm->ops.irq_ops, pwm->irq_id);
+        ZF_LOGF_IF(error, "Failed to unregister IRQ");
+    }
+    if (pwm->pwm_map != NULL) {
+        pwm_stop(pwm);
+        ps_pmem_unmap(&pwm->ops, pwm->pmem, (void *) pwm->pwm_map);
+    }
+}
+
+int pwm_init(pwm_t *pwm, ps_io_ops_t ops, pwm_config_t config)
+{
+    int error;
+
+    if (pwm == NULL) {
+        return EINVAL;
+    }
+
+    /* Set up pwm */
+    pwm->ops = ops;
+    pwm->mode = config.mode;
+    pwm->user_cb_fn = config.user_cb_fn;
+    pwm->user_cb_token = config.user_cb_token;
+
+    error = helper_fdt_alloc_simple(
+                &ops, config.fdt_path,
+                PWM_REG_CHOICE, PWM_IRQ_CHOICE,
+                (void *) &pwm->pwm_map, &pwm->pmem, &pwm->irq_id,
+                pwm_handle_irq, pwm
+            );
+    if (error) {
+        ZF_LOGE("Failed to init pwm timer using simple helper");
+        pwm_destroy(pwm);
+        return error;
+    }
+
+    pwm_setup(pwm);
 
     return 0;
 }