99#include <zephyr/device.h>
1010#include <zephyr/drivers/sensor.h>
1111#include <zephyr/drivers/gpio.h>
12+ #include <zephyr/pm/device_runtime.h>
1213
1314static K_SEM_DEFINE (sem , 0 , 1 ) ;
1415static const struct gpio_dt_spec phase_a = GPIO_DT_SPEC_GET (DT_ALIAS (qenca ), gpios );
@@ -140,6 +141,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
140141{
141142 int rc ;
142143
144+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
145+ pm_device_runtime_get (qdec_dev );
146+ }
147+
143148 qdec_trigger .type = SENSOR_TRIG_DATA_READY ;
144149 qdec_trigger .chan = SENSOR_CHAN_ALL ;
145150 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
@@ -158,10 +163,18 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
158163 /* emulation not working, but there maybe old trigger, ignore */
159164 rc = k_sem_take (& sem , K_MSEC (200 ));
160165
166+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
167+ pm_device_runtime_put (qdec_dev );
168+ }
169+
161170 /* there should be no triggers now*/
162171 rc = k_sem_take (& sem , K_MSEC (200 ));
163172 zassert_true (rc == - EAGAIN , "qdec handler should not be triggered (%d)" , rc );
164173
174+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
175+ pm_device_runtime_get (qdec_dev );
176+ }
177+
165178 /* register empty trigger - disable trigger */
166179 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , NULL );
167180 zassert_true (rc == 0 , "sensor_trigger_set failed: %d" , rc );
@@ -171,6 +184,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
171184 /* emulation working, but handler not set, thus should not be called */
172185 rc = k_sem_take (& sem , K_MSEC (200 ));
173186 zassert_true (rc == - EAGAIN , "qdec handler should not be triggered (%d)" , rc );
187+
188+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
189+ pm_device_runtime_put (qdec_dev );
190+ }
174191}
175192
176193/**
@@ -184,6 +201,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
184201 int rc ;
185202 struct sensor_value val = {0 };
186203
204+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
205+ pm_device_runtime_get (qdec_dev );
206+ }
207+
187208 qdec_trigger .type = SENSOR_TRIG_DATA_READY ;
188209 qdec_trigger .chan = SENSOR_CHAN_ROTATION ;
189210 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
@@ -202,6 +223,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
202223
203224 TC_PRINT ("QDEC reading: %d\n" , val .val1 );
204225 zassert_true (val .val1 != 0 , "No readings from QDEC" );
226+
227+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
228+ pm_device_runtime_put (qdec_dev );
229+ }
205230}
206231
207232/**
@@ -214,6 +239,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
214239{
215240 int rc ;
216241
242+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
243+ pm_device_runtime_get (qdec_dev );
244+ }
245+
217246 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
218247 zassume_true (rc != - ENOSYS , "sensor_trigger_set not supported" );
219248
@@ -228,6 +257,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
228257
229258 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
230259 zassume_true (rc < 0 , "sensor_trigger_set should fail due to invalid channel" );
260+
261+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
262+ pm_device_runtime_put (qdec_dev );
263+ }
231264}
232265
233266/**
@@ -238,12 +271,20 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
238271 */
239272ZTEST (qdec_sensor , test_qdec_readings )
240273{
274+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
275+ pm_device_runtime_get (qdec_dev );
276+ }
277+
241278 qenc_emulate_verify_reading (10 , 100 , true, false);
242279 qenc_emulate_verify_reading (2 , 500 , true, false);
243280 qenc_emulate_verify_reading (10 , 200 , false, false);
244281 /* may lead to overflows but currently driver does not detects that */
245282 qenc_emulate_verify_reading (1 , 1000 , false, true);
246283 qenc_emulate_verify_reading (1 , 1000 , true, true);
284+
285+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
286+ pm_device_runtime_put (qdec_dev );
287+ }
247288}
248289
249290/**
@@ -257,9 +298,16 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
257298 int rc ;
258299 struct sensor_value val = {0 };
259300
301+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
302+ pm_device_runtime_get (qdec_dev );
303+ }
304+
260305 rc = sensor_sample_fetch (qdec_dev );
261306 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
262307
308+ /* wait for potential new readings */
309+ k_msleep (100 );
310+
263311 /* get readings but ignore them, as they may include reading from time
264312 * when emulation was still working (i.e. during previous test)
265313 */
@@ -277,6 +325,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
277325 zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
278326 zassert_true (val .val1 == 0 , "Expected no readings but got: %d" , val .val1 );
279327 zassert_true (val .val2 == 0 , "Expected no readings but got: %d" , val .val2 );
328+
329+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
330+ pm_device_runtime_put (qdec_dev );
331+ }
280332}
281333
282334/**
@@ -291,6 +343,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
291343 struct sensor_value val_first = {0 };
292344 struct sensor_value val_second = {0 };
293345
346+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
347+ pm_device_runtime_get (qdec_dev );
348+ }
349+
294350 qenc_emulate_start (K_MSEC (10 ), true);
295351
296352 /* wait for some readings*/
@@ -325,6 +381,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
325381 */
326382 TC_PRINT ("Expected the same readings: %d vs %d - ignore!\n" , val_first .val2 ,
327383 val_second .val2 );
384+
385+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
386+ pm_device_runtime_put (qdec_dev );
387+ }
328388}
329389
330390/**
@@ -338,6 +398,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
338398 int rc ;
339399 struct sensor_value val = {0 };
340400
401+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
402+ pm_device_runtime_get (qdec_dev );
403+ }
404+
341405 qenc_emulate_start (K_MSEC (10 ), true);
342406
343407 /* wait for some readings*/
@@ -350,6 +414,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
350414 zassert_true (rc < 0 , "Should failed to get sample (%d)" , rc );
351415 zassert_true (val .val1 == 0 , "Some readings from QDEC: %d" , val .val1 );
352416 zassert_true (val .val2 == 0 , "Some readings from QDEC: %d" , val .val2 );
417+
418+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
419+ pm_device_runtime_put (qdec_dev );
420+ }
353421}
354422
355423/**
@@ -362,6 +430,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
362430{
363431 int rc ;
364432
433+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
434+ pm_device_runtime_get (qdec_dev );
435+ }
436+
365437 rc = sensor_sample_fetch (qdec_dev );
366438 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
367439
@@ -370,6 +442,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
370442
371443 rc = sensor_sample_fetch_chan (qdec_dev , SENSOR_CHAN_MAX );
372444 zassert_true (rc < 0 , "Should fail to fetch sample from invalid channel (%d)" , rc );
445+
446+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
447+ pm_device_runtime_put (qdec_dev );
448+ }
373449}
374450
375451static void * setup (void )
0 commit comments