@@ -70,4 +70,59 @@ ZTEST(device_driver_init, test_device_driver_init)
7070#endif
7171}
7272
73+ struct pm_transition_test_dev_data {
74+ enum pm_device_state state_turn_on ;
75+ enum pm_device_state state_resume ;
76+ bool state_other ;
77+ };
78+
79+ static int pm_transition_test_dev_pm_action (const struct device * dev , enum pm_device_action action )
80+ {
81+ struct pm_transition_test_dev_data * data = dev -> data ;
82+
83+ /* Preserve the observed internal state when actions ran */
84+ switch (action ) {
85+ case PM_DEVICE_ACTION_TURN_ON :
86+ pm_device_state_get (dev , & data -> state_turn_on );
87+ break ;
88+ case PM_DEVICE_ACTION_RESUME :
89+ pm_device_state_get (dev , & data -> state_resume );
90+ break ;
91+ default :
92+ data -> state_other = true;
93+ }
94+ return 0 ;
95+ }
96+
97+ static int pm_transition_test_dev_init (const struct device * dev )
98+ {
99+ struct pm_transition_test_dev_data * data = dev -> data ;
100+
101+ data -> state_turn_on = UINT8_MAX ;
102+ data -> state_resume = UINT8_MAX ;
103+ data -> state_other = false;
104+
105+ return pm_device_driver_init (dev , pm_transition_test_dev_pm_action );
106+ }
107+
108+ static struct pm_transition_test_dev_data dev_data ;
109+ 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 ,
111+ PM_DEVICE_GET (pm_transition_test_dev_pm ), & dev_data , NULL ,
112+ POST_KERNEL , 0 , NULL );
113+
114+ ZTEST (device_driver_init , test_device_driver_init_pm_state )
115+ {
116+ #ifdef CONFIG_PM_DEVICE
117+ zassert_equal (PM_DEVICE_STATE_OFF , dev_data .state_turn_on );
118+ zassert_equal (PM_DEVICE_STATE_SUSPENDED , dev_data .state_resume );
119+ zassert_false (dev_data .state_other );
120+ #else
121+ /* pm_device_state_get always returns PM_DEVICE_STATE_ACTIVE */
122+ zassert_equal (PM_DEVICE_STATE_ACTIVE , dev_data .state_turn_on );
123+ zassert_equal (PM_DEVICE_STATE_ACTIVE , dev_data .state_resume );
124+ zassert_false (dev_data .state_other );
125+ #endif /* CONFIG_PM */
126+ }
127+
73128ZTEST_SUITE (device_driver_init , NULL , NULL , NULL , NULL , NULL );
0 commit comments