1 /*
2 * Copyright 2022 NXP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "phNxpNciHal_ULPDet.h"
18
19 #include <phNxpLog.h>
20 #include <phTmlNfc.h>
21
22 #include "phNfcCommon.h"
23 #include "phNxpNciHal_IoctlOperations.h"
24 #include "phNxpNciHal_PowerTrackerIface.h"
25
26 extern phNxpNciHal_Control_t nxpncihal_ctrl;
27 extern NFCSTATUS phNxpNciHal_ext_send_sram_config_to_flash();
28 extern PowerTrackerHandle gPowerTrackerHandle;
29
30 /*******************************************************************************
31 **
32 ** Function phNxpNciHal_isULPDetSupported()
33 **
34 ** Description this function is to check ULPDet feature is supported or not
35 **
36 ** Returns true or false
37 *******************************************************************************/
phNxpNciHal_isULPDetSupported()38 bool phNxpNciHal_isULPDetSupported() {
39 unsigned long num = 0;
40 if ((GetNxpNumValue(NAME_NXP_DEFAULT_ULPDET_MODE, &num, sizeof(num)))) {
41 if ((uint8_t)num > 0) {
42 NXPLOG_NCIHAL_E("%s: NxpNci isULPDetSupported true", __func__);
43 return true;
44 }
45 }
46 NXPLOG_NCIHAL_E("%s: NxpNci isULPDetSupported false", __func__);
47 return false;
48 }
49
50 /*******************************************************************************
51 **
52 ** Function phNxpNciHal_setULPDetFlag()
53 **
54 ** Description this function is called by Framework API to set ULPDet mode
55 ** enable/disable
56 **
57 ** Parameters flag - true to enable ULPDet, false to disable
58 **
59 ** Returns true or false
60 *******************************************************************************/
phNxpNciHal_setULPDetFlag(bool flag)61 void phNxpNciHal_setULPDetFlag(bool flag) {
62 nxpncihal_ctrl.isUlpdetModeEnabled = flag;
63 if (gPowerTrackerHandle.stateChange != NULL) {
64 RefreshNfccPowerState state = (flag) ? ULPDET_ON : ULPDET_OFF;
65 gPowerTrackerHandle.stateChange(state);
66 }
67 }
68
69 /*******************************************************************************
70 **
71 ** Function phNxpNciHal_getULPDetFlag()
72 **
73 ** Description this function get the ULPDet state, true if it is enabled
74 ** false if it is disabled
75 **
76 ** Returns true or false
77 *******************************************************************************/
phNxpNciHal_getULPDetFlag()78 bool phNxpNciHal_getULPDetFlag() { return nxpncihal_ctrl.isUlpdetModeEnabled; }
79
80 /*******************************************************************************
81 **
82 ** Function phNxpNciHal_propConfULPDetMode()
83 **
84 ** Description this function applies the configurations to enable/disable
85 ** ULPDet Mode
86 **
87 ** Parameters bEnable - true to enable, false to disable
88 **
89 ** Returns NFCSTATUS_FAILED or NFCSTATUS_SUCCESS
90 *******************************************************************************/
phNxpNciHal_propConfULPDetMode(bool bEnable)91 NFCSTATUS phNxpNciHal_propConfULPDetMode(bool bEnable) {
92 NFCSTATUS status = NFCSTATUS_SUCCESS;
93 NXPLOG_NCIHAL_E("%s flag %d", __func__, bEnable);
94 if (!phNxpNciHal_isULPDetSupported()) return false;
95
96 uint8_t cmd_coreULPDET[] = {0x2F, 0x00, 0x01, 0x03};
97 uint8_t getConfig_A015[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x15};
98 uint8_t setConfig_A015[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x15, 0x01, 0x01};
99 uint8_t getConfig_A10F[] = {0x20, 0x03, 0x03, 0x01, 0xA1, 0x0F};
100 uint8_t setConfig_A10F[] = {0x20, 0x02, 0x06, 0x01, 0xA1,
101 0x0F, 0x02, 0x00, 0x00};
102
103 uint8_t setUlpdetConfig[] = {0x20, 0x02, 0x0A, 0x02, 0xA0, 0x15, 0x01,
104 0x02, 0xA1, 0x0F, 0x02, 0x01, 0x01};
105
106 do {
107 if (bEnable) {
108 status =
109 phNxpNciHal_send_ext_cmd(sizeof(setUlpdetConfig), setUlpdetConfig);
110 if (status != NFCSTATUS_SUCCESS) {
111 NXPLOG_NCIHAL_E("ulpdet configs set: Failed");
112 break;
113 }
114
115 status = phNxpNciHal_send_ext_cmd(sizeof(cmd_coreULPDET), cmd_coreULPDET);
116 if (status != NFCSTATUS_SUCCESS) {
117 NXPLOG_NCIHAL_E("coreULPDET: Failed");
118 break;
119 }
120
121 nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
122 status = phNxpNciHal_ext_send_sram_config_to_flash();
123 if (status != NFCSTATUS_SUCCESS) {
124 NXPLOG_NCIHAL_E("Updation of the SRAM contents failed");
125 break;
126 }
127 (void)phTmlNfc_IoCtl(phTmlNfc_e_PullVenLow);
128 } else {
129 status = phNxpNciHal_send_ext_cmd(sizeof(getConfig_A015), getConfig_A015);
130 if ((status == NFCSTATUS_SUCCESS) &&
131 (nxpncihal_ctrl.p_rx_data[8] != 0x01)) {
132 status =
133 phNxpNciHal_send_ext_cmd(sizeof(setConfig_A015), setConfig_A015);
134 if (status != NFCSTATUS_SUCCESS) {
135 NXPLOG_NCIHAL_E("Set Config : Failed");
136 }
137 }
138
139 status = phNxpNciHal_send_ext_cmd(sizeof(getConfig_A10F), getConfig_A10F);
140 if ((status == NFCSTATUS_SUCCESS) &&
141 (nxpncihal_ctrl.p_rx_data[8] != 0x00)) {
142 status =
143 phNxpNciHal_send_ext_cmd(sizeof(setConfig_A10F), setConfig_A10F);
144 if (status != NFCSTATUS_SUCCESS) {
145 NXPLOG_NCIHAL_E("Set Config: Failed");
146 }
147 }
148 /* reset the flag upon exit ulpdet mode */
149 nxpncihal_ctrl.isUlpdetModeEnabled = false;
150 }
151 } while (false);
152 NXPLOG_NCIHAL_E("%s: exit. status = %d", __func__, status);
153
154 return status;
155 }
156