1 /*
2  * Copyright (c) 2022-2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  *  ======== BatMonSupportLPF3.c ========
35  *
36  */
37 
38 /* Includes */
39 #include <ti/drivers/dpl/HwiP.h>
40 
41 #include <ti/drivers/batterymonitor/BatMonSupportLPF3.h>
42 #include <ti/drivers/temperature/TemperatureLPF3.h>
43 
44 #include <ti/devices/DeviceFamily.h>
45 #include DeviceFamily_constructPath(inc/hw_memmap.h)
46 #include DeviceFamily_constructPath(inc/hw_evtsvt.h)
47 #include DeviceFamily_constructPath(inc/hw_evtull.h)
48 #include DeviceFamily_constructPath(inc/hw_ints.h)
49 #include DeviceFamily_constructPath(inc/hw_pmud.h)
50 #include DeviceFamily_constructPath(inc/hw_types.h)
51 
52 /* Forward declarations */
53 static void batMonHwiFxn(uintptr_t arg0);
54 
55 /* Globals */
56 
57 /* HWI struct for the shared BATMON interrupt */
58 static HwiP_Struct batMonHwi;
59 
60 /* Allocate memory for registered event callback functions */
61 static BatMonSupportLPF3_EventCb temperatureCb = NULL;
62 static BatMonSupportLPF3_EventCb batteryCb     = NULL;
63 
64 /* Event masks for the registered event callback functions */
65 static uint32_t temperatureEventMask = 0;
66 static uint32_t batteryEventMask     = 0;
67 
68 static bool isInitialized = false;
69 
70 extern const BatMonSupportLPF3_Config BatMonSupportLPF3_config;
71 
72 /*
73  *  ======== batMonHwiFxn ========
74  *
75  *  AON_PMU_COMB Interrupt
76  */
batMonHwiFxn(uintptr_t arg0)77 static void batMonHwiFxn(uintptr_t arg0)
78 {
79     uint32_t events = HWREG(PMUD_BASE + PMUD_O_EVENT);
80 
81     if (((events & batteryEventMask) != 0) && (batteryCb != NULL))
82     {
83         batteryCb(events & batteryEventMask);
84     }
85 
86     if (((events & temperatureEventMask) != 0) && (temperatureCb != NULL))
87     {
88         temperatureCb(events & temperatureEventMask);
89     }
90 
91     HwiP_clearInterrupt(BatMonSupportLPF3_config.intNum);
92 }
93 
94 /*
95  *  ======== BatMonSupportLPF3_init ========
96  */
BatMonSupportLPF3_init(void)97 void BatMonSupportLPF3_init(void)
98 {
99     uint32_t key;
100 
101     key = HwiP_disable();
102 
103     if (isInitialized == false)
104     {
105         /* Claim configurable CPUIRQ as AON_PMU_COMB */
106         HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ0SEL + (BatMonSupportLPF3_config.intNum - INT_CPUIRQ0) * sizeof(uint32_t)) = BatMonSupportLPF3_config.intMux;
107 
108         /* Initialise the BatMon HWI. The temperature sensor shares this
109          * interrupt with the battery voltage monitoring events.
110          */
111         HwiP_Params hwiParams;
112         HwiP_Params_init(&hwiParams);
113         hwiParams.priority  = BatMonSupportLPF3_config.intPriority;
114         hwiParams.enableInt = true;
115         HwiP_construct(&batMonHwi, BatMonSupportLPF3_config.intNum, batMonHwiFxn, &hwiParams);
116 
117         /* Disable all events */
118         HWREG(PMUD_BASE + PMUD_O_EVENTMASK) = 0;
119 
120         /* Enable BATMON. Explicitly disable HW hysteresis to avoid HW bug
121          * causing poor temperature measurement accuracy.
122          */
123         HWREG(PMUD_BASE + PMUD_O_CTL) = PMUD_CTL_CALC_EN | PMUD_CTL_MEAS_EN | PMUD_CTL_HYST_EN_DIS;
124 
125         /* Set the combined BATMON interrupt as a wakeup source. This means the
126          * BATMON can bring the device out of standby when an event is
127          * triggered.
128          */
129         HWREG(EVTULL_BASE + EVTULL_O_WKUPMASK) |= EVTULL_WKUPMASK_AON_PMU_COMB;
130 
131         isInitialized = true;
132     }
133 
134     HwiP_restore(key);
135 }
136 
137 /*
138  *  ======== BatMonSupportLPF3_registerTemperatureCb ========
139  */
BatMonSupportLPF3_registerTemperatureCb(uint32_t eventMask,BatMonSupportLPF3_EventCb callback)140 void BatMonSupportLPF3_registerTemperatureCb(uint32_t eventMask, BatMonSupportLPF3_EventCb callback)
141 {
142     uint32_t key;
143 
144     key = HwiP_disable();
145 
146     temperatureEventMask = eventMask;
147     temperatureCb        = callback;
148 
149     HwiP_restore(key);
150 }
151 
152 /*
153  *  ======== BatMonSupportLPF3_registerBatteryCb ========
154  */
BatMonSupportLPF3_registerBatteryCb(uint32_t eventMask,BatMonSupportLPF3_EventCb callback)155 void BatMonSupportLPF3_registerBatteryCb(uint32_t eventMask, BatMonSupportLPF3_EventCb callback)
156 {
157     uint32_t key;
158 
159     key = HwiP_disable();
160 
161     batteryEventMask = eventMask;
162     batteryCb        = callback;
163 
164     HwiP_restore(key);
165 }