@@ -192,81 +192,64 @@ static const struct sensor_driver_api qdec_nrfx_driver_api = {
192192 .trigger_set = qdec_nrfx_trigger_set ,
193193};
194194
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 )
198196{
199197 const struct qdec_nrfx_config * config = dev -> config ;
200- int ret = 0 ;
201198
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+ {
202216 switch (action ) {
203217 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 );
221219 break ;
222220
223221 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 );
231224 }
232225 break ;
233226 default :
234227 return - ENOTSUP ;
228+ break ;
235229 }
236230
237- return ret ;
231+ return 0 ;
238232}
239- #endif /* CONFIG_PM_DEVICE */
240233
241234static int qdec_nrfx_init (const struct device * dev )
242235{
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 ;
246238
247- int err = pinctrl_apply_state ( dev_config -> pcfg , PINCTRL_STATE_DEFAULT );
239+ config -> irq_connect ( );
248240
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 ;
251244 }
252245
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 );
264250 }
265251
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 );
270253}
271254
272255#define QDEC (idx ) DT_NODELABEL(qdec##idx)
@@ -301,7 +284,7 @@ static int qdec_nrfx_init(const struct device *dev)
301284 .enable_pin = DT_PROP_OR(QDEC(idx), enable_pin, NRF_QDEC_PIN_NOT_CONNECTED), \
302285 .steps = QDEC_PROP(idx, steps), \
303286 }; \
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); \
305288 SENSOR_DEVICE_DT_DEFINE(QDEC(idx), \
306289 qdec_nrfx_init, \
307290 PM_DEVICE_DT_GET(QDEC(idx)), \
0 commit comments