@@ -73,6 +73,8 @@ ZTEST(device_driver_init, test_device_driver_init)
7373struct pm_transition_test_dev_data {
7474 enum pm_device_state state_turn_on ;
7575 enum pm_device_state state_resume ;
76+ enum pm_device_state state_suspend ;
77+ enum pm_device_state state_turn_off ;
7678 bool state_other ;
7779};
7880
@@ -88,6 +90,12 @@ static int pm_transition_test_dev_pm_action(const struct device *dev, enum pm_de
8890 case PM_DEVICE_ACTION_RESUME :
8991 pm_device_state_get (dev , & data -> state_resume );
9092 break ;
93+ case PM_DEVICE_ACTION_SUSPEND :
94+ pm_device_state_get (dev , & data -> state_suspend );
95+ break ;
96+ case PM_DEVICE_ACTION_TURN_OFF :
97+ pm_device_state_get (dev , & data -> state_turn_off );
98+ break ;
9199 default :
92100 data -> state_other = true;
93101 }
@@ -100,14 +108,30 @@ static int pm_transition_test_dev_init(const struct device *dev)
100108
101109 data -> state_turn_on = UINT8_MAX ;
102110 data -> state_resume = UINT8_MAX ;
111+ data -> state_suspend = UINT8_MAX ;
112+ data -> state_turn_off = UINT8_MAX ;
103113 data -> state_other = false;
104114
105115 return pm_device_driver_init (dev , pm_transition_test_dev_pm_action );
106116}
107117
118+ static int pm_transition_test_dev_deinit (const struct device * dev )
119+ {
120+ struct pm_transition_test_dev_data * data = dev -> data ;
121+
122+ data -> state_turn_on = UINT8_MAX ;
123+ data -> state_resume = UINT8_MAX ;
124+ data -> state_suspend = UINT8_MAX ;
125+ data -> state_turn_off = UINT8_MAX ;
126+ data -> state_other = false;
127+
128+ return pm_device_driver_deinit (dev , pm_transition_test_dev_pm_action );
129+ }
130+
108131static struct pm_transition_test_dev_data dev_data ;
109132PM_DEVICE_DEFINE (pm_transition_test_dev_pm , pm_transition_test_dev_pm_action );
110- DEVICE_DEFINE (pm_transition_test_dev , "test_dev" , pm_transition_test_dev_init ,
133+ DEVICE_DEINIT_DEFINE (pm_transition_test_dev , "test_dev" ,
134+ pm_transition_test_dev_init , pm_transition_test_dev_deinit ,
111135 PM_DEVICE_GET (pm_transition_test_dev_pm ), & dev_data , NULL ,
112136 POST_KERNEL , 0 , NULL );
113137
@@ -116,11 +140,40 @@ ZTEST(device_driver_init, test_device_driver_init_pm_state)
116140#ifdef CONFIG_PM_DEVICE
117141 zassert_equal (PM_DEVICE_STATE_OFF , dev_data .state_turn_on );
118142 zassert_equal (PM_DEVICE_STATE_SUSPENDED , dev_data .state_resume );
143+ zassert_equal (UINT8_MAX , dev_data .state_suspend );
144+ zassert_equal (UINT8_MAX , dev_data .state_turn_off );
119145 zassert_false (dev_data .state_other );
120146#else
121147 /* pm_device_state_get always returns PM_DEVICE_STATE_ACTIVE */
122148 zassert_equal (PM_DEVICE_STATE_ACTIVE , dev_data .state_turn_on );
123149 zassert_equal (PM_DEVICE_STATE_ACTIVE , dev_data .state_resume );
150+ zassert_equal (UINT8_MAX , dev_data .state_suspend );
151+ zassert_equal (UINT8_MAX , dev_data .state_turn_off );
152+ zassert_false (dev_data .state_other );
153+ #endif /* CONFIG_PM */
154+
155+ #ifdef CONFIG_PM_DEVICE
156+ /* device_deinit() blocked if device is not suspended or off */
157+ zassert_not_ok (device_deinit (DEVICE_GET (pm_transition_test_dev )));
158+ zassert_ok (pm_device_action_run (DEVICE_GET (pm_transition_test_dev ),
159+ PM_DEVICE_ACTION_SUSPEND ));
160+ #endif
161+
162+ zassert_ok (device_deinit (DEVICE_GET (pm_transition_test_dev )));
163+
164+ #ifdef CONFIG_PM_DEVICE
165+ /* no action called as device is already suspended or off */
166+ zassert_equal (UINT8_MAX , dev_data .state_turn_on );
167+ zassert_equal (UINT8_MAX , dev_data .state_resume );
168+ zassert_equal (UINT8_MAX , dev_data .state_suspend );
169+ zassert_equal (UINT8_MAX , dev_data .state_turn_off );
170+ zassert_false (dev_data .state_other );
171+ #else
172+ /* pm_device_state_get always returns PM_DEVICE_STATE_ACTIVE */
173+ zassert_equal (UINT8_MAX , dev_data .state_turn_on );
174+ zassert_equal (UINT8_MAX , dev_data .state_resume );
175+ zassert_equal (PM_DEVICE_STATE_ACTIVE , dev_data .state_suspend );
176+ zassert_equal (UINT8_MAX , dev_data .state_turn_off );
124177 zassert_false (dev_data .state_other );
125178#endif /* CONFIG_PM */
126179}
0 commit comments