1 /*
2  * Copyright (C) 2021 Alibaba Group Holding Limited
3  */
4 
5 #include <errno.h>
6 #include <stdbool.h>
7 #include <aos/pwm_core.h>
8 #include <aos/pwm_csi.h>
9 
pwm_csi_unregister(aos_pwm_t * pwm)10 static void pwm_csi_unregister(aos_pwm_t *pwm)
11 {
12     aos_pwm_csi_t *aos_csi_pwm = aos_container_of(pwm, aos_pwm_csi_t, aos_pwm);
13 
14     csi_pwm_uninit(&(aos_csi_pwm->csi_pwm));
15 }
16 
pwm_csi_startup(aos_pwm_t * pwm)17 static aos_status_t pwm_csi_startup(aos_pwm_t *pwm)
18 {
19     return 0;
20 }
21 
pwm_csi_shutdown(aos_pwm_t * pwm)22 static void pwm_csi_shutdown(aos_pwm_t *pwm)
23 {
24     aos_pwm_csi_t *aos_csi_pwm = aos_container_of(pwm, aos_pwm_csi_t, aos_pwm);
25     csi_pwm_out_stop(&(aos_csi_pwm->csi_pwm), aos_csi_pwm->aos_pwm.dev.id);
26 }
27 
pwm_csi_apply(aos_pwm_t * pwm,aos_pwm_attr_t const * attr)28 static aos_status_t pwm_csi_apply(aos_pwm_t *pwm, aos_pwm_attr_t const *attr)
29 {
30     uint32_t period;
31     uint32_t duty_cycle;
32 
33     aos_pwm_csi_t *aos_csi_pwm = aos_container_of(pwm, aos_pwm_csi_t, aos_pwm);
34     period = attr->period / 1000;
35     duty_cycle = attr->duty_cycle / 1000;
36     csi_pwm_out_config(&(aos_csi_pwm->csi_pwm), aos_csi_pwm->aos_pwm.dev.id,
37                     period, duty_cycle, (csi_pwm_polarity_t)attr->polarity);
38     if (attr->enabled)
39         csi_pwm_out_start(&(aos_csi_pwm->csi_pwm), aos_csi_pwm->aos_pwm.dev.id);
40     else
41         csi_pwm_out_stop(&(aos_csi_pwm->csi_pwm), aos_csi_pwm->aos_pwm.dev.id);
42     return 0;
43 }
44 
45 static const aos_pwm_ops_t pwm_csi_ops = {
46     .unregister = pwm_csi_unregister,
47     .apply      = pwm_csi_apply,
48     .shutdown   = pwm_csi_shutdown,
49     .startup = pwm_csi_startup
50 };
51 
aos_pwm_csi_register(aos_pwm_csi_t * pwm_csi)52 aos_status_t aos_pwm_csi_register(aos_pwm_csi_t *pwm_csi)
53 {
54     if (!pwm_csi)
55         return -EINVAL;
56 
57     pwm_csi->aos_pwm.ops = &pwm_csi_ops;
58 
59     return aos_pwm_register(&pwm_csi->aos_pwm);
60 }
61 
aos_pwm_csi_unregister(uint32_t id)62 aos_status_t aos_pwm_csi_unregister(uint32_t id)
63 {
64     return aos_pwm_unregister(id);
65 }
66