|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <misc/printk.h> |
| 8 | +#include "dummy_parent.h" |
| 9 | +#include "dummy_driver.h" |
| 10 | + |
| 11 | +static struct k_poll_event async_evt; |
| 12 | +u32_t device_power_state; |
| 13 | +static struct device *parent; |
| 14 | + |
| 15 | +static int dummy_open(struct device *dev) |
| 16 | +{ |
| 17 | + int ret; |
| 18 | + int signaled = 0, result; |
| 19 | + |
| 20 | + printk("open()\n"); |
| 21 | + |
| 22 | + /* Make sure parent is resumed */ |
| 23 | + ret = device_pm_get_sync(parent); |
| 24 | + if (ret < 0) { |
| 25 | + return ret; |
| 26 | + } |
| 27 | + |
| 28 | + ret = device_pm_get(dev); |
| 29 | + if (ret < 0) { |
| 30 | + return ret; |
| 31 | + } |
| 32 | + |
| 33 | + printk("Async wakeup request queued\n"); |
| 34 | + |
| 35 | + do { |
| 36 | + (void)k_poll(&async_evt, 1, K_FOREVER); |
| 37 | + k_poll_signal_check(&dev->config->pm->signal, |
| 38 | + &signaled, &result); |
| 39 | + } while (!signaled); |
| 40 | + |
| 41 | + async_evt.state = K_POLL_STATE_NOT_READY; |
| 42 | + k_poll_signal_reset(&dev->config->pm->signal); |
| 43 | + |
| 44 | + if (result == DEVICE_PM_ACTIVE_STATE) { |
| 45 | + printk("Dummy device resumed\n"); |
| 46 | + ret = 0; |
| 47 | + } else { |
| 48 | + printk("Dummy device Not resumed\n"); |
| 49 | + ret = -1; |
| 50 | + } |
| 51 | + |
| 52 | + return ret; |
| 53 | +} |
| 54 | + |
| 55 | +static int dummy_read(struct device *dev, u32_t *val) |
| 56 | +{ |
| 57 | + struct dummy_parent_api *api; |
| 58 | + int ret; |
| 59 | + |
| 60 | + printk("read()\n"); |
| 61 | + |
| 62 | + api = (struct dummy_parent_api *)parent->driver_api; |
| 63 | + ret = api->transfer(parent, DUMMY_PARENT_RD, val); |
| 64 | + return ret; |
| 65 | +} |
| 66 | + |
| 67 | +static int dummy_write(struct device *dev, u32_t val) |
| 68 | +{ |
| 69 | + struct dummy_parent_api *api; |
| 70 | + int ret; |
| 71 | + |
| 72 | + printk("write()\n"); |
| 73 | + api = (struct dummy_parent_api *)parent->driver_api; |
| 74 | + ret = api->transfer(parent, DUMMY_PARENT_WR, &val); |
| 75 | + return ret; |
| 76 | +} |
| 77 | + |
| 78 | +static int dummy_close(struct device *dev) |
| 79 | +{ |
| 80 | + int ret; |
| 81 | + |
| 82 | + printk("close()\n"); |
| 83 | + ret = device_pm_put_sync(dev); |
| 84 | + if (ret == 1) { |
| 85 | + printk("Async suspend request ququed\n"); |
| 86 | + } |
| 87 | + |
| 88 | + /* Parent can be suspended */ |
| 89 | + if (parent) { |
| 90 | + device_pm_put(parent); |
| 91 | + } |
| 92 | + |
| 93 | + return ret; |
| 94 | +} |
| 95 | + |
| 96 | +static u32_t dummy_get_power_state(struct device *dev) |
| 97 | +{ |
| 98 | + return device_power_state; |
| 99 | +} |
| 100 | + |
| 101 | +static int dummy_suspend(struct device *dev) |
| 102 | +{ |
| 103 | + printk("child suspending..\n"); |
| 104 | + device_power_state = DEVICE_PM_SUSPEND_STATE; |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static int dummy_resume_from_suspend(struct device *dev) |
| 110 | +{ |
| 111 | + printk("child resuming..\n"); |
| 112 | + device_power_state = DEVICE_PM_ACTIVE_STATE; |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +static int dummy_device_pm_ctrl(struct device *dev, u32_t ctrl_command, |
| 118 | + void *context, device_pm_cb cb, void *arg) |
| 119 | +{ |
| 120 | + int ret = 0; |
| 121 | + |
| 122 | + switch (ctrl_command) { |
| 123 | + case DEVICE_PM_SET_POWER_STATE: |
| 124 | + if (*((u32_t *)context) == DEVICE_PM_ACTIVE_STATE) { |
| 125 | + ret = dummy_resume_from_suspend(dev); |
| 126 | + } else { |
| 127 | + ret = dummy_suspend(dev); |
| 128 | + } |
| 129 | + break; |
| 130 | + case DEVICE_PM_GET_POWER_STATE: |
| 131 | + *((u32_t *)context) = dummy_get_power_state(dev); |
| 132 | + break; |
| 133 | + default: |
| 134 | + ret = -EINVAL; |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + cb(dev, ret, context, arg); |
| 139 | + |
| 140 | + return ret; |
| 141 | +} |
| 142 | + |
| 143 | +static const struct dummy_driver_api funcs = { |
| 144 | + .open = dummy_open, |
| 145 | + .read = dummy_read, |
| 146 | + .write = dummy_write, |
| 147 | + .close = dummy_close, |
| 148 | +}; |
| 149 | + |
| 150 | +int dummy_init(struct device *dev) |
| 151 | +{ |
| 152 | + parent = device_get_binding(DUMMY_PARENT_NAME); |
| 153 | + if (!parent) { |
| 154 | + printk("parent not found\n"); |
| 155 | + } |
| 156 | + |
| 157 | + device_pm_enable(dev); |
| 158 | + device_power_state = DEVICE_PM_ACTIVE_STATE; |
| 159 | + |
| 160 | + k_poll_event_init(&async_evt, K_POLL_TYPE_SIGNAL, |
| 161 | + K_POLL_MODE_NOTIFY_ONLY, &dev->config->pm->signal); |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +DEVICE_DEFINE(dummy_driver, DUMMY_DRIVER_NAME, &dummy_init, |
| 166 | + dummy_device_pm_ctrl, NULL, NULL, APPLICATION, |
| 167 | + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &funcs); |
0 commit comments