1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #include <stdio.h>
22 #include "lpcmp.h"
23 #include "lpcmp_regs.h"
24 #include "lpcmp_reva.h"
25 #include "mxc_device.h"
26 #include "mxc_errors.h"
27 #include "mxc_assert.h"
28 #include "mxc_sys.h"
29 #include "mcr_regs.h"
30 #include "mxc_lock.h"
31 #include "mxc_pins.h"
32 
33 static int init = 0;
34 static const mxc_lpcmp_ctrl_reg_t lpcmp_ctrl_regs[] = { &MXC_MCR->cmp_ctrl, &MXC_LPCMP->ctrl[0],
35                                                         &MXC_LPCMP->ctrl[1], &MXC_LPCMP->ctrl[2] };
36 
initGPIOForComp(mxc_lpcmp_cmpsel_t cmp)37 static void initGPIOForComp(mxc_lpcmp_cmpsel_t cmp)
38 {
39     switch (cmp) {
40     case MXC_LPCMP_CMP0:
41         MXC_GPIO_Config(&gpio_cfg_cmp0);
42         break;
43     case MXC_LPCMP_CMP1:
44         MXC_GPIO_Config(&gpio_cfg_cmp1);
45         break;
46     case MXC_LPCMP_CMP2:
47         MXC_GPIO_Config(&gpio_cfg_cmp2);
48         break;
49     case MXC_LPCMP_CMP3:
50         MXC_GPIO_Config(&gpio_cfg_cmp3);
51         break;
52     default:
53         break;
54     }
55 }
56 
MXC_LPCMP_Init(mxc_lpcmp_cmpsel_t cmp)57 int MXC_LPCMP_Init(mxc_lpcmp_cmpsel_t cmp)
58 {
59     int ret;
60 
61     initGPIOForComp(cmp);
62 
63     if (cmp > MXC_LPCMP_CMP3) {
64         return E_BAD_PARAM;
65     }
66 
67     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_LPCOMP);
68 
69     if ((ret = MXC_LPCMP_RevA_Init(lpcmp_ctrl_regs[cmp])) == E_NO_ERROR) {
70         init++;
71     }
72 
73     return ret;
74 }
75 
MXC_LPCMP_Shutdown(mxc_lpcmp_cmpsel_t cmp)76 int MXC_LPCMP_Shutdown(mxc_lpcmp_cmpsel_t cmp)
77 {
78     if (cmp > MXC_LPCMP_CMP3) {
79         return E_BAD_PARAM;
80     }
81 
82     init--;
83     if (init == 0) {
84         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_LPCOMP);
85     }
86 
87     return MXC_LPCMP_RevA_Shutdown(lpcmp_ctrl_regs[cmp]);
88 }
89 
MXC_LPCMP_EnableInt(mxc_lpcmp_cmpsel_t cmp,mxc_lpcmp_polarity_t pol)90 int MXC_LPCMP_EnableInt(mxc_lpcmp_cmpsel_t cmp, mxc_lpcmp_polarity_t pol)
91 {
92     if (cmp > MXC_LPCMP_CMP3) {
93         return E_BAD_PARAM;
94     }
95 
96     MXC_LPCMP_ClearFlags(cmp);
97     MXC_LPCMP_SelectPolarity(cmp, pol);
98 
99     return MXC_LPCMP_RevA_EnableInt(lpcmp_ctrl_regs[cmp]);
100 }
101 
MXC_LPCMP_DisableInt(mxc_lpcmp_cmpsel_t cmp)102 int MXC_LPCMP_DisableInt(mxc_lpcmp_cmpsel_t cmp)
103 {
104     if (cmp > MXC_LPCMP_CMP3) {
105         return E_BAD_PARAM;
106     }
107 
108     return MXC_LPCMP_RevA_DisableInt(lpcmp_ctrl_regs[cmp]);
109 }
110 
MXC_LPCMP_GetFlags(mxc_lpcmp_cmpsel_t cmp)111 int MXC_LPCMP_GetFlags(mxc_lpcmp_cmpsel_t cmp)
112 {
113     if (cmp > MXC_LPCMP_CMP3) {
114         return E_BAD_PARAM;
115     }
116 
117     return MXC_LPCMP_RevA_GetFlags(lpcmp_ctrl_regs[cmp]);
118 }
119 
MXC_LPCMP_ClearFlags(mxc_lpcmp_cmpsel_t cmp)120 int MXC_LPCMP_ClearFlags(mxc_lpcmp_cmpsel_t cmp)
121 {
122     if (cmp > MXC_LPCMP_CMP3) {
123         return E_BAD_PARAM;
124     }
125 
126     return MXC_LPCMP_RevA_ClearFlags(lpcmp_ctrl_regs[cmp]);
127 }
128 
MXC_LPCMP_SelectPolarity(mxc_lpcmp_cmpsel_t cmp,mxc_lpcmp_polarity_t pol)129 int MXC_LPCMP_SelectPolarity(mxc_lpcmp_cmpsel_t cmp, mxc_lpcmp_polarity_t pol)
130 {
131     if (cmp > MXC_LPCMP_CMP3) {
132         return E_BAD_PARAM;
133     }
134 
135     return MXC_LPCMP_RevA_SelectPolarity(lpcmp_ctrl_regs[cmp], pol);
136 }
137