forked from zmkfirmware/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdevice.c
More file actions
445 lines (367 loc) · 8.72 KB
/
device.c
File metadata and controls
445 lines (367 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright (c) 2018 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <pm/device.h>
#include <pm/device_runtime.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(pm_device, CONFIG_PM_DEVICE_LOG_LEVEL);
const char *pm_device_state_str(enum pm_device_state state)
{
switch (state) {
case PM_DEVICE_STATE_ACTIVE:
return "active";
case PM_DEVICE_STATE_SUSPENDED:
return "suspended";
case PM_DEVICE_STATE_OFF:
return "off";
default:
return "";
}
}
const char *pm_device_action_str(enum pm_device_action action)
{
switch (action) {
case PM_DEVICE_ACTION_SUSPEND:
return "suspend";
case PM_DEVICE_ACTION_RESUME:
return "resume";
case PM_DEVICE_ACTION_TURN_OFF:
return "turn_off";
case PM_DEVICE_ACTION_TURN_ON:
return "turn_on";
case PM_DEVICE_ACTION_FORCE_SUSPEND:
return "force_suspend";
default:
return "unknown_action";
}
}
int pm_device_state_set(const struct device *dev,
enum pm_device_state state)
{
int ret;
enum pm_device_action action;
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return -ENOSYS;
}
if (pm_device_state_is_locked(dev)) {
return -EPERM;
}
switch (state) {
case PM_DEVICE_STATE_SUSPENDED:
if (pm->state == PM_DEVICE_STATE_SUSPENDED) {
return -EALREADY;
} else if (pm->state == PM_DEVICE_STATE_OFF) {
return -ENOTSUP;
}
action = PM_DEVICE_ACTION_SUSPEND;
break;
case PM_DEVICE_STATE_ACTIVE:
if (pm->state == PM_DEVICE_STATE_ACTIVE) {
return -EALREADY;
}
action = PM_DEVICE_ACTION_RESUME;
break;
case PM_DEVICE_STATE_OFF:
if (pm->state == state) {
return -EALREADY;
}
action = PM_DEVICE_ACTION_TURN_OFF;
break;
default:
return -ENOTSUP;
}
ret = pm->action_cb(dev, action);
if (ret < 0) {
return ret;
}
pm->state = state;
return 0;
}
int pm_device_action_run(const struct device *dev,
enum pm_device_action action)
{
int ret;
enum pm_device_state state;
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return -ENOSYS;
}
if (pm_device_state_is_locked(dev)) {
return -EPERM;
}
switch (action) {
case PM_DEVICE_ACTION_FORCE_SUSPEND:
__fallthrough;
case PM_DEVICE_ACTION_SUSPEND:
if (pm->state == PM_DEVICE_STATE_SUSPENDED) {
return -EALREADY;
} else if (pm->state == PM_DEVICE_STATE_OFF) {
return -ENOTSUP;
}
state = PM_DEVICE_STATE_SUSPENDED;
break;
case PM_DEVICE_ACTION_RESUME:
if (pm->state == PM_DEVICE_STATE_ACTIVE) {
return -EALREADY;
}
state = PM_DEVICE_STATE_ACTIVE;
break;
case PM_DEVICE_ACTION_TURN_OFF:
if (pm->state == PM_DEVICE_STATE_OFF) {
return -EALREADY;
}
state = PM_DEVICE_STATE_OFF;
break;
case PM_DEVICE_ACTION_TURN_ON:
if (pm->state != PM_DEVICE_STATE_OFF) {
return -ENOTSUP;
}
state = PM_DEVICE_STATE_SUSPENDED;
break;
default:
return -ENOTSUP;
}
ret = pm->action_cb(dev, action);
if (ret < 0) {
/*
* TURN_ON and TURN_OFF are actions triggered by a power domain
* when it is resumed or suspended, which means that the energy
* to the device will be removed or added. For this reason, even
* if the device does not handle these actions its state needs to
* updated to reflect its physical behavior.
*
* The function will still return the error code so the domain
* can take whatever action is more appropriated.
*/
if ((ret == -ENOTSUP) && ((action == PM_DEVICE_ACTION_TURN_ON)
|| (action == PM_DEVICE_ACTION_TURN_OFF))) {
pm->state = state;
}
return ret;
}
pm->state = state;
return 0;
}
static int power_domain_add_or_remove(const struct device *dev,
const struct device *domain,
bool add)
{
#if defined(CONFIG_HAS_DYNAMIC_DEVICE_HANDLES)
device_handle_t *rv = domain->handles;
device_handle_t dev_handle = -1;
extern const struct device __device_start[];
extern const struct device __device_end[];
size_t i, region = 0;
size_t numdev = __device_end - __device_start;
/*
* Supported devices are stored as device handle and not
* device pointers. So, it is necessary to find what is
* the handle associated to the given device.
*/
for (i = 0; i < numdev; i++) {
if (&__device_start[i] == dev) {
dev_handle = i + 1;
break;
}
}
/*
* The last part is to find an available slot in the
* supported section of handles array and replace it
* with the device handle.
*/
while (region != 2) {
if (*rv == DEVICE_HANDLE_SEP) {
region++;
}
rv++;
}
i = 0;
while (rv[i] != DEVICE_HANDLE_ENDS) {
if (add == false) {
if (rv[i] == dev_handle) {
dev->pm->domain = NULL;
rv[i] = DEVICE_HANDLE_NULL;
return 0;
}
} else {
if (rv[i] == DEVICE_HANDLE_NULL) {
dev->pm->domain = domain;
rv[i] = dev_handle;
return 0;
}
}
++i;
}
return add ? -ENOSPC : -ENOENT;
#else
ARG_UNUSED(dev);
ARG_UNUSED(domain);
ARG_UNUSED(add);
return -ENOSYS;
#endif
}
int pm_device_power_domain_remove(const struct device *dev,
const struct device *domain)
{
return power_domain_add_or_remove(dev, domain, false);
}
int pm_device_power_domain_add(const struct device *dev,
const struct device *domain)
{
return power_domain_add_or_remove(dev, domain, true);
}
void pm_device_children_action_run(const struct device *dev,
enum pm_device_action action,
pm_device_action_failed_cb_t failure_cb)
{
const device_handle_t *handles;
size_t handle_count = 0U;
int rc = 0;
/*
* We don't use device_supported_foreach here because we don't want the
* early exit behaviour of that function. Even if the N'th device fails
* to PM_DEVICE_ACTION_TURN_ON for example, we still want to run the
* action on the N+1'th device.
*/
handles = device_supported_handles_get(dev, &handle_count);
for (size_t i = 0U; i < handle_count; ++i) {
device_handle_t dh = handles[i];
const struct device *cdev = device_from_handle(dh);
if (cdev == NULL) {
continue;
}
rc = pm_device_action_run(cdev, action);
if ((failure_cb != NULL) && (rc < 0)) {
/* Stop the iteration if the callback requests it */
if (!failure_cb(cdev, rc)) {
break;
}
}
}
}
int pm_device_state_get(const struct device *dev,
enum pm_device_state *state)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return -ENOSYS;
}
*state = pm->state;
return 0;
}
bool pm_device_is_any_busy(void)
{
const struct device *devs;
size_t devc;
devc = z_device_get_all_static(&devs);
for (const struct device *dev = devs; dev < (devs + devc); dev++) {
struct pm_device *pm = dev->pm;
if (pm == NULL) {
continue;
}
if (atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_BUSY)) {
return true;
}
}
return false;
}
bool pm_device_is_busy(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return false;
}
return atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_BUSY);
}
void pm_device_busy_set(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return;
}
atomic_set_bit(&pm->flags, PM_DEVICE_FLAG_BUSY);
}
void pm_device_busy_clear(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return;
}
atomic_clear_bit(&pm->flags, PM_DEVICE_FLAG_BUSY);
}
bool pm_device_wakeup_enable(struct device *dev, bool enable)
{
atomic_val_t flags, new_flags;
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return false;
}
flags = atomic_get(&pm->flags);
if ((flags & BIT(PM_DEVICE_FLAG_WS_CAPABLE)) == 0U) {
return false;
}
if (enable) {
new_flags = flags |
BIT(PM_DEVICE_FLAG_WS_ENABLED);
} else {
new_flags = flags & ~BIT(PM_DEVICE_FLAG_WS_ENABLED);
}
return atomic_cas(&pm->flags, flags, new_flags);
}
bool pm_device_wakeup_is_enabled(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return false;
}
return atomic_test_bit(&pm->flags,
PM_DEVICE_FLAG_WS_ENABLED);
}
bool pm_device_wakeup_is_capable(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return false;
}
return atomic_test_bit(&pm->flags,
PM_DEVICE_FLAG_WS_CAPABLE);
}
void pm_device_state_lock(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if ((pm != NULL) && !pm_device_runtime_is_enabled(dev)) {
atomic_set_bit(&pm->flags, PM_DEVICE_FLAG_STATE_LOCKED);
}
}
void pm_device_state_unlock(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm != NULL) {
atomic_clear_bit(&pm->flags, PM_DEVICE_FLAG_STATE_LOCKED);
}
}
bool pm_device_state_is_locked(const struct device *dev)
{
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return false;
}
return atomic_test_bit(&pm->flags,
PM_DEVICE_FLAG_STATE_LOCKED);
}
bool pm_device_on_power_domain(const struct device *dev)
{
#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
struct pm_device *pm = dev->pm;
if (pm == NULL) {
return false;
}
return pm->domain != NULL;
#else
return false;
#endif
}