@@ -192,81 +192,64 @@ static const struct sensor_driver_api qdec_nrfx_driver_api = {
192
192
.trigger_set = qdec_nrfx_trigger_set ,
193
193
};
194
194
195
- #ifdef CONFIG_PM_DEVICE
196
- static int qdec_nrfx_pm_action (const struct device * dev ,
197
- enum pm_device_action action )
195
+ static void qdec_pm_suspend (const struct device * dev )
198
196
{
199
197
const struct qdec_nrfx_config * config = dev -> config ;
200
- int ret = 0 ;
201
198
199
+ nrfx_qdec_disable (& config -> qdec );
200
+ qdec_nrfx_gpio_ctrl (dev , false);
201
+
202
+ (void )pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_SLEEP );
203
+ }
204
+
205
+ static void qdec_pm_resume (const struct device * dev )
206
+ {
207
+ const struct qdec_nrfx_config * config = dev -> config ;
208
+
209
+ (void )pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_DEFAULT );
210
+ qdec_nrfx_gpio_ctrl (dev , true);
211
+ nrfx_qdec_enable (& config -> qdec );
212
+ }
213
+
214
+ static int qdec_nrfx_pm_action (const struct device * dev , enum pm_device_action action )
215
+ {
202
216
switch (action ) {
203
217
case PM_DEVICE_ACTION_RESUME :
204
- ret = pinctrl_apply_state (config -> pcfg ,
205
- PINCTRL_STATE_DEFAULT );
206
- if (ret < 0 ) {
207
- return ret ;
208
- }
209
- qdec_nrfx_gpio_ctrl (dev , true);
210
- nrfx_qdec_enable (& config -> qdec );
211
- break ;
212
-
213
- case PM_DEVICE_ACTION_TURN_OFF :
214
- /* device must be uninitialized */
215
- nrfx_qdec_uninit (& config -> qdec );
216
- ret = pinctrl_apply_state (config -> pcfg ,
217
- PINCTRL_STATE_SLEEP );
218
- if (ret < 0 ) {
219
- return ret ;
220
- }
218
+ qdec_pm_resume (dev );
221
219
break ;
222
220
223
221
case PM_DEVICE_ACTION_SUSPEND :
224
- /* device must be suspended */
225
- nrfx_qdec_disable (& config -> qdec );
226
- qdec_nrfx_gpio_ctrl (dev , false);
227
- ret = pinctrl_apply_state (config -> pcfg ,
228
- PINCTRL_STATE_SLEEP );
229
- if (ret < 0 ) {
230
- return ret ;
222
+ if (IS_ENABLED (CONFIG_PM_DEVICE )) {
223
+ qdec_pm_suspend (dev );
231
224
}
232
225
break ;
233
226
default :
234
227
return - ENOTSUP ;
228
+ break ;
235
229
}
236
230
237
- return ret ;
231
+ return 0 ;
238
232
}
239
- #endif /* CONFIG_PM_DEVICE */
240
233
241
234
static int qdec_nrfx_init (const struct device * dev )
242
235
{
243
- const struct qdec_nrfx_config * dev_config = dev -> config ;
244
-
245
- dev_config -> irq_connect ();
236
+ const struct qdec_nrfx_config * config = dev -> config ;
237
+ nrfx_err_t nerr ;
246
238
247
- int err = pinctrl_apply_state ( dev_config -> pcfg , PINCTRL_STATE_DEFAULT );
239
+ config -> irq_connect ( );
248
240
249
- if (err < 0 ) {
250
- return err ;
241
+ nerr = nrfx_qdec_init (& config -> qdec , & config -> config , qdec_nrfx_event_handler , (void * )dev );
242
+ if (nerr != NRFX_SUCCESS ) {
243
+ return (nerr == NRFX_ERROR_INVALID_STATE ) ? - EBUSY : - EFAULT ;
251
244
}
252
245
253
- nrfx_err_t nerr = nrfx_qdec_init (& dev_config -> qdec ,
254
- & dev_config -> config ,
255
- qdec_nrfx_event_handler ,
256
- (void * )dev );
257
-
258
- if (nerr == NRFX_ERROR_INVALID_STATE ) {
259
- LOG_ERR ("qdec already in use" );
260
- return - EBUSY ;
261
- } else if (nerr != NRFX_SUCCESS ) {
262
- LOG_ERR ("failed to initialize qdec" );
263
- return - EFAULT ;
246
+ /* End up in suspend state. */
247
+ qdec_nrfx_gpio_ctrl (dev , false);
248
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
249
+ (void )pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_SLEEP );
264
250
}
265
251
266
- qdec_nrfx_gpio_ctrl (dev , true);
267
- nrfx_qdec_enable (& dev_config -> qdec );
268
-
269
- return 0 ;
252
+ return pm_device_driver_init (dev , qdec_nrfx_pm_action );
270
253
}
271
254
272
255
#define QDEC (idx ) DT_NODELABEL(qdec##idx)
@@ -301,7 +284,7 @@ static int qdec_nrfx_init(const struct device *dev)
301
284
.enable_pin = DT_PROP_OR(QDEC(idx), enable_pin, NRF_QDEC_PIN_NOT_CONNECTED), \
302
285
.steps = QDEC_PROP(idx, steps), \
303
286
}; \
304
- PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action); \
287
+ PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action, PM_DEVICE_ISR_SAFE); \
305
288
SENSOR_DEVICE_DT_DEFINE(QDEC(idx), \
306
289
qdec_nrfx_init, \
307
290
PM_DEVICE_DT_GET(QDEC(idx)), \
0 commit comments