@@ -73,6 +73,8 @@ ZTEST(device_driver_init, test_device_driver_init)
73
73
struct pm_transition_test_dev_data {
74
74
enum pm_device_state state_turn_on ;
75
75
enum pm_device_state state_resume ;
76
+ enum pm_device_state state_suspend ;
77
+ enum pm_device_state state_turn_off ;
76
78
bool state_other ;
77
79
};
78
80
@@ -88,6 +90,12 @@ static int pm_transition_test_dev_pm_action(const struct device *dev, enum pm_de
88
90
case PM_DEVICE_ACTION_RESUME :
89
91
pm_device_state_get (dev , & data -> state_resume );
90
92
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 ;
91
99
default :
92
100
data -> state_other = true;
93
101
}
@@ -100,14 +108,30 @@ static int pm_transition_test_dev_init(const struct device *dev)
100
108
101
109
data -> state_turn_on = UINT8_MAX ;
102
110
data -> state_resume = UINT8_MAX ;
111
+ data -> state_suspend = UINT8_MAX ;
112
+ data -> state_turn_off = UINT8_MAX ;
103
113
data -> state_other = false;
104
114
105
115
return pm_device_driver_init (dev , pm_transition_test_dev_pm_action );
106
116
}
107
117
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
+
108
131
static struct pm_transition_test_dev_data dev_data ;
109
132
PM_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 ,
111
135
PM_DEVICE_GET (pm_transition_test_dev_pm ), & dev_data , NULL ,
112
136
POST_KERNEL , 0 , NULL );
113
137
@@ -116,11 +140,40 @@ ZTEST(device_driver_init, test_device_driver_init_pm_state)
116
140
#ifdef CONFIG_PM_DEVICE
117
141
zassert_equal (PM_DEVICE_STATE_OFF , dev_data .state_turn_on );
118
142
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 );
119
145
zassert_false (dev_data .state_other );
120
146
#else
121
147
/* pm_device_state_get always returns PM_DEVICE_STATE_ACTIVE */
122
148
zassert_equal (PM_DEVICE_STATE_ACTIVE , dev_data .state_turn_on );
123
149
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 (PM_DEVICE_STATE_ACTIVE , dev_data .state_turn_off );
124
177
zassert_false (dev_data .state_other );
125
178
#endif /* CONFIG_PM */
126
179
}
0 commit comments