1818
1919LOG_MODULE_REGISTER (sm_ctrl_pin , CONFIG_SM_LOG_LEVEL );
2020
21- #if DT_NODE_HAS_PROP (DT_CHOSEN (ncs_sm_uart ), dtr_gpios )
21+ #define SM_DTR_GPIOS DT_NODE_HAS_PROP(DT_CHOSEN(ncs_sm_uart), dtr_gpios)
22+ #define SM_MDM_PWR_GPIOS DT_NODE_HAS_PROP(DT_CHOSEN(ncs_sm_power_gpios), power_gpios)
23+
24+ #if SM_DTR_GPIOS
2225static const struct gpio_dt_spec dtr_gpio =
2326 GPIO_DT_SPEC_GET_OR (DT_CHOSEN (ncs_sm_uart ), dtr_gpios , {0 });
2427
25- static struct gpio_callback gpio_cb ;
28+ static struct gpio_callback dtr_gpio_cb ;
29+ #endif
30+ #if SM_MDM_PWR_GPIOS
31+ static const struct gpio_dt_spec mdm_pwr_gpio =
32+ GPIO_DT_SPEC_GET_OR (DT_CHOSEN (ncs_sm_power_gpios ), power_gpios , {0 });
33+
34+ static struct gpio_callback mdm_pwr_gpio_cb ;
2635#endif
2736
2837static int ext_xtal_control (bool xtal_on )
@@ -58,7 +67,7 @@ static int ext_xtal_control(bool xtal_on)
5867 return err ;
5968}
6069
61- #if DT_NODE_HAS_PROP ( DT_CHOSEN ( ncs_sm_uart ), dtr_gpios )
70+ #if SM_DTR_GPIOS
6271static void dtr_enable_fn (struct k_work * )
6372{
6473 LOG_INF ("DTR pin callback work function." );
@@ -82,7 +91,7 @@ static void dtr_pin_callback(const struct device *dev, struct gpio_callback *gpi
8291
8392int sm_ctrl_pin_ready (void )
8493{
85- #if DT_NODE_HAS_PROP ( DT_CHOSEN ( ncs_sm_uart ), dtr_gpios )
94+ #if SM_DTR_GPIOS
8695 if (gpio_is_ready_dt (& dtr_gpio )) {
8796 return 0 ;
8897 }
@@ -91,31 +100,15 @@ int sm_ctrl_pin_ready(void)
91100 return - EFAULT ;
92101}
93102
94- void sm_ctrl_pin_enter_sleep ( void )
103+ void sm_ctrl_pin_enter_sleep_no_uninit ( bool at_host_power_off )
95104{
96- #if DT_NODE_HAS_PROP (DT_CHOSEN (ncs_sm_uart ), dtr_gpios )
97- int err ;
98-
99- err = sm_ctrl_pin_ready ();
100- if (err ) {
101- return ;
105+ #if SM_DTR_GPIOS || SM_MDM_PWR_GPIOS
106+ if (at_host_power_off ) {
107+ sm_at_host_power_off ();
102108 }
103109
104- /* Stop threads, uninitialize host and disable DTR UART. */
105- sm_at_host_uninit ();
106-
107- /* Only power off the modem if it has not been put
108- * in flight mode to allow reducing NVM wear.
109- */
110- if (!sm_is_modem_functional_mode (LTE_LC_FUNC_MODE_OFFLINE )) {
111- sm_power_off_modem ();
112- }
113-
114- gpio_pin_interrupt_configure_dt (& dtr_gpio , GPIO_INT_DISABLE );
115-
116- LOG_INF ("Entering sleep." );
110+ LOG_INF ("Entering sleep. No uninit." );
117111 LOG_PANIC ();
118- nrf_gpio_cfg_sense_set (dtr_gpio .pin , NRF_GPIO_PIN_SENSE_LOW );
119112
120113 k_sleep (K_MSEC (100 ));
121114
@@ -124,26 +117,27 @@ void sm_ctrl_pin_enter_sleep(void)
124117#endif
125118}
126119
127- /* TODO: Cellular modem driver uses pulse triggered power management, which is not suitable with
128- * the current implementation.
129- */
130- void sm_ctrl_pin_enter_sleep_no_uninit (void )
120+ void sm_ctrl_pin_enter_sleep (void )
131121{
132- #if DT_NODE_HAS_PROP (DT_CHOSEN (ncs_sm_uart ), dtr_gpios )
133- LOG_INF ("Entering sleep." );
134- LOG_PANIC ();
135- nrf_gpio_cfg_sense_set (dtr_gpio .pin , NRF_GPIO_PIN_SENSE_LOW );
122+ #if SM_DTR_GPIOS || SM_MDM_PWR_GPIOS
136123
137- k_sleep (K_MSEC (100 ));
124+ /* Stop threads, uninitialize host and disable DTR UART. */
125+ sm_at_host_uninit ();
138126
139- nrf_regulators_system_off (NRF_REGULATORS_NS );
140- assert (false);
127+ /* Only power off the modem if it has not been put
128+ * in flight mode to allow reducing NVM wear.
129+ */
130+ if (!sm_is_modem_functional_mode (LTE_LC_FUNC_MODE_OFFLINE )) {
131+ sm_power_off_modem ();
132+ }
133+
134+ sm_ctrl_pin_enter_sleep_no_uninit (false);
141135#endif
142136}
143137
144138void sm_ctrl_pin_enter_idle (void )
145139{
146- #if DT_NODE_HAS_PROP ( DT_CHOSEN ( ncs_sm_uart ), dtr_gpios )
140+ #if SM_DTR_GPIOS
147141 LOG_INF ("Entering idle." );
148142 int err ;
149143
@@ -152,8 +146,8 @@ void sm_ctrl_pin_enter_idle(void)
152146 return ;
153147 }
154148
155- gpio_init_callback (& gpio_cb , dtr_pin_callback , BIT (dtr_gpio .pin ));
156- err = gpio_add_callback_dt (& dtr_gpio , & gpio_cb );
149+ gpio_init_callback (& dtr_gpio_cb , dtr_pin_callback , BIT (dtr_gpio .pin ));
150+ err = gpio_add_callback_dt (& dtr_gpio , & dtr_gpio_cb );
157151 if (err ) {
158152 LOG_ERR ("gpio_add_callback failed: %d" , err );
159153 return ;
@@ -175,15 +169,48 @@ void sm_ctrl_pin_enter_shutdown(void)
175169 assert (false);
176170}
177171
172+ void sm_ctrl_pin_init_gpios (void )
173+ {
174+ #if SM_MDM_PWR_GPIOS
175+ /* Configure Modem Power GPIO */
176+ if (!gpio_is_ready_dt (& mdm_pwr_gpio )) {
177+ LOG_ERR ("Modem Power GPIO not ready" );
178+ return ;
179+ }
180+ int err = gpio_pin_configure_dt (& mdm_pwr_gpio , GPIO_INPUT );
181+
182+ if (err < 0 ) {
183+ LOG_ERR ("Failed to configure Modem Power GPIO (%d)." , err );
184+ }
185+ #endif
186+ }
187+
178188int sm_ctrl_pin_init (void )
179189{
180190 int err ;
181191
182192 err = ext_xtal_control (true);
183- if (err < 0 ) {
193+ if (err ) {
184194 LOG_ERR ("Failed to enable ext XTAL: %d" , err );
185195 return err ;
186196 }
187197
188- return err ;
198+ #if SM_DTR_GPIOS
199+ nrf_gpio_cfg_sense_set (dtr_gpio .pin , NRF_GPIO_PIN_SENSE_LOW );
200+ #endif
201+ #if SM_MDM_PWR_GPIOS
202+ err = gpio_pin_interrupt_configure_dt (& mdm_pwr_gpio , GPIO_INT_EDGE_TO_ACTIVE );
203+ if (err ) {
204+ LOG_ERR ("Failed to configure Modem Power GPIO interrupt (%d)." , err );
205+ return err ;
206+ }
207+ gpio_init_callback (& mdm_pwr_gpio_cb , NULL , BIT (mdm_pwr_gpio .pin ));
208+ err = gpio_add_callback_dt (& mdm_pwr_gpio , & mdm_pwr_gpio_cb );
209+ if (err ) {
210+ LOG_ERR ("Failed to add Modem Power GPIO callback (%d)." , err );
211+ return err ;
212+ }
213+ nrf_gpio_cfg_sense_set (mdm_pwr_gpio .pin , NRF_GPIO_PIN_SENSE_LOW );
214+ #endif
215+ return 0 ;
189216}
0 commit comments