summaryrefslogtreecommitdiff
path: root/soc/common/power/power.c
blob: 4f94180f36c12efb12056497d973c0245bc693f6 (plain)
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
/*
 * Copyright (C)  2015. Marvell International Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>

#include <utils/Log.h>
#include <hardware/hardware.h>
#include <hardware/power.h>

#define LOG_TAG "PowerHAL"

#define POWER_HAL_CONF "/etc/powerhal.conf"

#define DEV_MAX 256
#define ERR_MAX 64
#define STATUS_MAX 32

static const char pm_control[] = "control";
static const char pm_status[] = "runtime_status";
static const char pm_state_auto[] = "auto";
static const char pm_state_on[] = "on";

struct node {
    char device[DEV_MAX];
    char status[DEV_MAX];
    int fd;
    struct node *next;
    struct node *prev;
};

struct list {
    struct node *first;
    struct node *last;
};

static struct list devices;

static int add_to_list(const char *device, int fd)
{
    struct node *node = malloc(sizeof(struct node));
    if (node == NULL) {
        ALOGE("Malloc failed for creating new node");
        return -1;
    }

    memset(node, 0, sizeof(struct node));
    if ((strlcpy(node->device, device,
        sizeof(node->device))) >= sizeof(node->device)) {
        ALOGE("Invalid name of the device %s", device);
        free(node);
        return -1;
    }

    int n = snprintf(node->status, sizeof(node->status),
                     "%s/%s", device, pm_status);
    if (n < 0 || n >= sizeof(node->status)) {
        ALOGE("Invalid status of the device %s", device);
        free(node);
        return -1;
    }

    node->fd = fd;

    if (devices.first == NULL) {
        devices.first = devices.last = node;
    } else {
        devices.last->next = node;
        node->prev = devices.last;
        devices.last = node;
    }

    return 0;
}

/**
 * Return 0 if the device is PM supported, or -1 if unsupported.
 */
static int is_pm_supported(const char *control, char *status)
{
    char buf[ERR_MAX] = {0};
    char sbuf[STATUS_MAX] = {0};

    int fd = open(status, O_RDONLY);
    if (fd < 0) {
        strerror_r(errno, buf, sizeof(buf));
        ALOGW("Error opening %s: %s", status, buf);
        return -1;
    }

    int ret = read(fd, sbuf, sizeof(sbuf) - 1);
    close(fd);

    if (ret < 0) {
        strerror_r(errno, buf, sizeof(buf));
        ALOGW("Error reading %s: %s", status, buf);
        return -1;
    }

    if (strcmp(sbuf, "unsupported") == 0) {
        ALOGW("%s is unsupported", status);
        return -1;
    }

    return 0;
}

static void wait_for_set_complete(struct node *node, int on)
{
    char buf[80] = {0};
    char sbuf[64] = {0};
    int count = 1000;
    const char *status = node->status;
    const char *device = node->device;

    do {
        int fd = open(status, O_RDONLY);
        if (fd < 0) {
            strerror_r(errno, buf, sizeof(buf));
            ALOGE("Error opening %s of %s: %s",
                  status, device, buf);
            return;
        }

        int ret = read(fd, sbuf, sizeof(sbuf) - 1);
        close(fd);

        if (ret < 0) {
            strerror_r(errno, buf, sizeof(buf));
            ALOGE("Error reading %s of %s: %s", status, device, buf);
            continue;
        }

        if (strcmp(sbuf, "unsupported") == 0) {
            ALOGE("%s of %s is unsupported", status, device);
            break;
        }

        if (strcmp(sbuf, on ? "active" : "suspended") == 0) {
            ALOGI("Set %s active/suspend successfully, "
                  "current status is %s", device, sbuf);
            break;
        }

        usleep(1000);
    } while (--count > 0);

    if (count == 0) {
        ALOGE("Device %s suspend/resume failed", device);
        return;
    }
}

static int pm_set_for_device(struct node *node, int on)
{
    char buf[ERR_MAX] = {0};

    const char *pm_value = on ? pm_state_on : pm_state_auto;

    int ret = write(node->fd, pm_value, strlen(pm_value));
    if (ret != strlen(pm_value)) {
        strerror_r(errno, buf, sizeof(buf));
        ALOGE("Error writing %s to %s: %s",
              pm_value, node->device, buf);
        return -1;
    }

    ALOGD("Set %s to %s done", node->device, pm_value);

    return 0;
}

static void power_init(struct power_module *module)
{
    FILE *conf = fopen(POWER_HAL_CONF, "r");

    if (conf == NULL) {
        ALOGE("%s does not exist! No devices for "
              "interactive mode power management", POWER_HAL_CONF);
        return;
    }

    char line[DEV_MAX] = {0};
    char control[DEV_MAX] = {0};
    char status[DEV_MAX] = {0};
    char buf[ERR_MAX] = {0};
    int fd = -1;

    while (fgets(line, DEV_MAX, conf) != NULL) {
        if (line[0] == '#' || line[0] == '\n' ||
            line[0] == ' ' || line[0] == '\0')
            continue;

        size_t line_len = strlen(line);
        if (line[line_len - 1] == '\n') {
            line[line_len - 1] = '\0';
            line_len--;
        }

        int n = snprintf(control, sizeof(control), "%s/%s", line, pm_control);
        if (n < 0 || n >= sizeof(control)) {
            ALOGE("Error copying control of the device %s", line);
            continue;
        }

        n = snprintf(status, sizeof(status), "%s/%s", line, pm_status);
        if (n < 0 || n >= sizeof(status)) {
            ALOGE("Error copying status of the device %s", line);
            continue;
        }

        if (is_pm_supported(control, status) != 0) {
            ALOGE("Device %s does not support PM", line);
            continue;
        }

        fd = open(control, O_RDWR);
        if (fd < 0) {
            strerror_r(errno, buf, sizeof(buf));
            ALOGE("Error opening %s: %s", control, buf);
            continue;
        }

        ALOGD("Add device %s to list", line);
        if (add_to_list(line, fd) != 0) {
            ALOGE("Add device %s to list failed", line);
            close(fd);
        }
    }

    fclose(conf);

    return;
}

static void power_set_interactive(struct power_module *module, int on)
{
    struct node *node;

    ALOGI("power_set_interactive called with on %d", on);
    if (on) {
        for (node = devices.last; node != NULL; node = node->prev)
            pm_set_for_device(node, on);
    } else {
        for (node = devices.first; node != NULL; node = node->next)
            pm_set_for_device(node, on);
    }

    for (node = devices.last; node != NULL; node = node->prev)
        wait_for_set_complete(node, on);

    return;
}

static void power_hint(struct power_module *module,
                       power_hint_t hint, void *data)
{
}

static struct hw_module_methods_t power_module_methods = {
    .open = NULL,
};

struct power_module HAL_MODULE_INFO_SYM = {
    .common = {
        .tag = HARDWARE_MODULE_TAG,
        .module_api_version = POWER_MODULE_API_VERSION_0_2,
        .hal_api_version = HARDWARE_HAL_API_VERSION,
        .id = POWER_HARDWARE_MODULE_ID,
        .name = "Marvell Power HAL",
        .author = "Marvell SEEDS",
        .methods = &power_module_methods,
    },

    .init = power_init,
    .setInteractive = power_set_interactive,
    .powerHint = power_hint,
};