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 );
@@ -144,6 +145,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
144145{
145146 int rc ;
146147
148+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
149+ pm_device_runtime_get (qdec_dev );
150+ }
151+
147152 qdec_trigger .type = SENSOR_TRIG_DATA_READY ;
148153 qdec_trigger .chan = SENSOR_CHAN_ALL ;
149154 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
@@ -162,10 +167,18 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
162167 /* emulation not working, but there maybe old trigger, ignore */
163168 rc = k_sem_take (& sem , K_MSEC (200 ));
164169
170+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
171+ pm_device_runtime_put (qdec_dev );
172+ }
173+
165174 /* there should be no triggers now*/
166175 rc = k_sem_take (& sem , K_MSEC (200 ));
167176 zassert_true (rc == - EAGAIN , "qdec handler should not be triggered (%d)" , rc );
168177
178+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
179+ pm_device_runtime_get (qdec_dev );
180+ }
181+
169182 /* register empty trigger - disable trigger */
170183 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , NULL );
171184 zassert_true (rc == 0 , "sensor_trigger_set failed: %d" , rc );
@@ -175,6 +188,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
175188 /* emulation working, but handler not set, thus should not be called */
176189 rc = k_sem_take (& sem , K_MSEC (200 ));
177190 zassert_true (rc == - EAGAIN , "qdec handler should not be triggered (%d)" , rc );
191+
192+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
193+ pm_device_runtime_put (qdec_dev );
194+ }
178195}
179196
180197/**
@@ -188,6 +205,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
188205 int rc ;
189206 struct sensor_value val = {0 };
190207
208+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
209+ pm_device_runtime_get (qdec_dev );
210+ }
211+
191212 qdec_trigger .type = SENSOR_TRIG_DATA_READY ;
192213 qdec_trigger .chan = SENSOR_CHAN_ROTATION ;
193214 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
@@ -209,6 +230,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
209230
210231 TC_PRINT ("QDEC reading: %d\n" , val .val1 );
211232 zassert_true (val .val1 != 0 , "No readings from QDEC" );
233+
234+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
235+ pm_device_runtime_put (qdec_dev );
236+ }
212237}
213238
214239/**
@@ -221,6 +246,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
221246{
222247 int rc ;
223248
249+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
250+ pm_device_runtime_get (qdec_dev );
251+ }
252+
224253 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
225254 zassume_true (rc != - ENOSYS , "sensor_trigger_set not supported" );
226255
@@ -235,6 +264,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
235264
236265 rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
237266 zassume_true (rc < 0 , "sensor_trigger_set should fail due to invalid channel" );
267+
268+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
269+ pm_device_runtime_put (qdec_dev );
270+ }
238271}
239272
240273/**
@@ -245,11 +278,19 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
245278 */
246279ZTEST (qdec_sensor , test_qdec_readings )
247280{
281+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
282+ pm_device_runtime_get (qdec_dev );
283+ }
284+
248285 qenc_emulate_verify_reading (10 , 100 , true, false);
249286 qenc_emulate_verify_reading (2 , 500 , true, false);
250287 qenc_emulate_verify_reading (10 , 200 , false, false);
251288 qenc_emulate_verify_reading (1 , 1000 , false, true);
252289 qenc_emulate_verify_reading (1 , 1000 , true, true);
290+
291+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
292+ pm_device_runtime_put (qdec_dev );
293+ }
253294}
254295
255296/**
@@ -263,9 +304,16 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
263304 int rc ;
264305 struct sensor_value val = {0 };
265306
307+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
308+ pm_device_runtime_get (qdec_dev );
309+ }
310+
266311 rc = sensor_sample_fetch (qdec_dev );
267312 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
268313
314+ /* wait for potential new readings */
315+ k_msleep (100 );
316+
269317 /* get readings but ignore them, as they may include reading from time
270318 * when emulation was still working (i.e. during previous test)
271319 */
@@ -283,6 +331,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
283331 zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
284332 zassert_true (val .val1 == 0 , "Expected no readings but got: %d" , val .val1 );
285333 zassert_true (val .val2 == 0 , "Expected no readings but got: %d" , val .val2 );
334+
335+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
336+ pm_device_runtime_put (qdec_dev );
337+ }
286338}
287339
288340/**
@@ -297,6 +349,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
297349 struct sensor_value val_first = {0 };
298350 struct sensor_value val_second = {0 };
299351
352+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
353+ pm_device_runtime_get (qdec_dev );
354+ }
355+
300356 qenc_emulate_start (K_MSEC (10 ), true);
301357
302358 /* wait for some readings*/
@@ -328,6 +384,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
328384 "Expected the same readings: %d vs %d" ,
329385 val_first .val2 ,
330386 val_second .val2 );
387+
388+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
389+ pm_device_runtime_put (qdec_dev );
390+ }
331391}
332392
333393/**
@@ -341,6 +401,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
341401 int rc ;
342402 struct sensor_value val = {0 };
343403
404+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
405+ pm_device_runtime_get (qdec_dev );
406+ }
407+
344408 qenc_emulate_start (K_MSEC (10 ), true);
345409
346410 /* wait for some readings*/
@@ -353,6 +417,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
353417 zassert_true (rc < 0 , "Should failed to get sample (%d)" , rc );
354418 zassert_true (val .val1 == 0 , "Some readings from QDEC: %d" , val .val1 );
355419 zassert_true (val .val2 == 0 , "Some readings from QDEC: %d" , val .val2 );
420+
421+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
422+ pm_device_runtime_put (qdec_dev );
423+ }
356424}
357425
358426/**
@@ -365,6 +433,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
365433{
366434 int rc ;
367435
436+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
437+ pm_device_runtime_get (qdec_dev );
438+ }
439+
368440 rc = sensor_sample_fetch (qdec_dev );
369441 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
370442
@@ -373,6 +445,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
373445
374446 rc = sensor_sample_fetch_chan (qdec_dev , SENSOR_CHAN_MAX );
375447 zassert_true (rc < 0 , "Should fail to fetch sample from invalid channel (%d)" , rc );
448+
449+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
450+ pm_device_runtime_put (qdec_dev );
451+ }
376452}
377453
378454static void * setup (void )
0 commit comments