|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr.h> |
| 8 | +#include <kernel.h> |
| 9 | +#include <string.h> |
| 10 | +#include <soc.h> |
| 11 | +#include <device.h> |
| 12 | +#include "pm_policy.h" |
| 13 | + |
| 14 | +#define LOG_MODULE_NAME power |
| 15 | +#define LOG_LEVEL CONFIG_PM_LOG_LEVEL /* From power module Kconfig */ |
| 16 | +#include <logging/log.h> |
| 17 | +LOG_MODULE_DECLARE(); |
| 18 | + |
| 19 | +/* |
| 20 | + * FIXME: Remove the conditional inclusion of |
| 21 | + * core_devices array once we enble the capability |
| 22 | + * to build the device list based on devices power |
| 23 | + * and clock domain dependencies. |
| 24 | + */ |
| 25 | +#ifdef CONFIG_SOC_SERIES_NRF52X |
| 26 | +#define MAX_PM_DEVICES 15 |
| 27 | +#define NUM_CORE_DEVICES 4 |
| 28 | +#define MAX_DEV_NAME_LEN 16 |
| 29 | +static const char core_devices[NUM_CORE_DEVICES][MAX_DEV_NAME_LEN] = { |
| 30 | + "clk_k32src", |
| 31 | + "clk_m16src", |
| 32 | + "sys_clock", |
| 33 | + "UART_0", |
| 34 | +}; |
| 35 | +#else |
| 36 | +#error "Add SoC's core devices list for PM" |
| 37 | +#endif |
| 38 | + |
| 39 | +/* |
| 40 | + * Ordered list to store devices on which |
| 41 | + * device power policies would be executed. |
| 42 | + */ |
| 43 | +static int device_ordered_list[MAX_PM_DEVICES]; |
| 44 | +static int device_retval[MAX_PM_DEVICES]; |
| 45 | +static struct device *pm_device_list; |
| 46 | +static int device_count; |
| 47 | + |
| 48 | +int sys_pm_suspend_devices(void) |
| 49 | +{ |
| 50 | + for (int i = device_count - 1; i >= 0; i--) { |
| 51 | + int idx = device_ordered_list[i]; |
| 52 | + |
| 53 | + /* TODO: Improve the logic by checking device status |
| 54 | + * and set the device states accordingly. |
| 55 | + */ |
| 56 | + device_retval[i] = device_set_power_state(&pm_device_list[idx], |
| 57 | + DEVICE_PM_SUSPEND_STATE); |
| 58 | + if (device_retval[i]) { |
| 59 | + LOG_ERR("%s suspend operation failed\n", |
| 60 | + pm_device_list[idx].config->name); |
| 61 | + return device_retval[i]; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +void sys_pm_resume_devices(void) |
| 69 | +{ |
| 70 | + int i; |
| 71 | + |
| 72 | + for (i = 0; i < device_count; i++) { |
| 73 | + if (!device_retval[i]) { |
| 74 | + int idx = device_ordered_list[i]; |
| 75 | + |
| 76 | + device_set_power_state(&pm_device_list[idx], |
| 77 | + DEVICE_PM_ACTIVE_STATE); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void sys_pm_create_device_list(void) |
| 83 | +{ |
| 84 | + int count; |
| 85 | + int i, j; |
| 86 | + bool is_core_dev; |
| 87 | + |
| 88 | + /* |
| 89 | + * Create an ordered list of devices that will be suspended. |
| 90 | + * Ordering should be done based on dependencies. Devices |
| 91 | + * in the beginning of the list will be resumed first. |
| 92 | + */ |
| 93 | + device_list_get(&pm_device_list, &count); |
| 94 | + |
| 95 | + /* Reserve for 32KHz, 16MHz, system clock, etc... */ |
| 96 | + device_count = NUM_CORE_DEVICES; |
| 97 | + |
| 98 | + for (i = 0; (i < count) && (device_count < MAX_PM_DEVICES); i++) { |
| 99 | + |
| 100 | + /* Check if the device is core device */ |
| 101 | + for (j = 0, is_core_dev = false; j < NUM_CORE_DEVICES; j++) { |
| 102 | + if (!strcmp(pm_device_list[i].config->name, |
| 103 | + &core_devices[j][0])) { |
| 104 | + is_core_dev = true; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (is_core_dev) { |
| 110 | + device_ordered_list[j] = i; |
| 111 | + } else { |
| 112 | + device_ordered_list[device_count++] = i; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments