aboutsummaryrefslogtreecommitdiff
path: root/kernel/power/sbsuspend.c
blob: 3a1753c531ef3223add53d8c06d11b306d61e874 (plain) (blame)
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
/* kernel/power/sbsuspend.c
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/sbsuspend.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/rtc.h>
//#include <linux/leds-mt65xx.h>


enum {
    DEBUG_USER_STATE = 1U << 0,
    DEBUG_SUSPEND = 1U << 2,
    DEBUG_VERBOSE = 1U << 3,
};
int sbsuspend_debug_mask = DEBUG_USER_STATE | DEBUG_SUSPEND | DEBUG_VERBOSE;;
module_param_named(sbsuspend_debug_mask, sbsuspend_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);

#define _TAG_PM_M "SB_PM"
#define pm_warn(fmt, ...)    \
    if (sbsuspend_debug_mask) pr_warn("[%s][%s]" fmt, _TAG_PM_M, __func__, ##__VA_ARGS__);

#define sb_attr(_name)                         \
static struct kobj_attribute _name##_attr = {   \
    .attr = {                                   \
        .name = __stringify(_name),             \
        .mode = 0644,                           \
    },                                          \
    .show = _name##_show,                       \
    .store = _name##_store,                     \
}



//sb_state_t sb_state = SB_STATE_DISABLE;
static DEFINE_MUTEX(sb_mutex);
static LIST_HEAD(sb_handlers);
static int sb_bypass = 0;
int sb_handler_count = 0;
int sb_handler_forbid_id = 0x0;
const char *const sb_states[SB_STATE_MAX] = {
    [SB_STATE_DISABLE]  = "disable",
    [SB_STATE_ENABLE]   = "enable",
    [SB_STATE_SUSPEND]  = "suspend",
    [SB_STATE_RESUME]   = "resume",
};



extern struct kobject *power_kobj;



void register_sb_handler(struct sb_handler *handler)
{
    struct list_head *pos;

    mutex_lock(&sb_mutex);

    list_for_each(pos, &sb_handlers) {
        struct sb_handler *e;
        e = list_entry(pos, struct sb_handler, link);
        if (e->level > handler->level)
            break;
    }
    list_add_tail(&handler->link, pos);
    sb_handler_count++;

    //if ((state & SUSPENDED) && handler->suspend)
    //    handler->suspend(handler);

    mutex_unlock(&sb_mutex);
}
EXPORT_SYMBOL(register_sb_handler);

void unregister_sb_handler(struct sb_handler *handler)
{
    mutex_lock(&sb_mutex);

    list_del(&handler->link);
    sb_handler_count--;

    mutex_unlock(&sb_mutex);
}
EXPORT_SYMBOL(unregister_sb_handler);

void sb_enable(void)
{
    struct sb_handler *pos;
    int count = 0;

    pr_warn("@@@@@@@@@@@@@@@@@@@\n@@@__sb_enable__@@@\n@@@@@@@@@@@@@@@@@@@\n");

    mutex_lock(&sb_mutex);

    //pm_warn("turn backlight off\n");
    //mt65xx_leds_brightness_set(MT65XX_LED_TYPE_BUTTON, 0);
    //mt65xx_leds_brightness_set(MT65XX_LED_TYPE_LCD, 0);
    
    pm_warn("sb_handler_count = %d, sb_handler_forbid_id = 0x%x\n", sb_handler_count, sb_handler_forbid_id);
    list_for_each_entry(pos, &sb_handlers, link) {
        if (pos->enable != NULL) {
            if (!(sb_handler_forbid_id & (0x1 << count))) {
                if (sbsuspend_debug_mask & DEBUG_SUSPEND)
                    pm_warn("sb enable handler %d: [%pf], level: %d\n", count, pos->enable, pos->level);
                pos->enable(pos);
            }
            count++; 
        }
    }

    if (sbsuspend_debug_mask & DEBUG_SUSPEND)
        pm_warn("sb enable handler done\n");

    mutex_unlock(&sb_mutex);

}
EXPORT_SYMBOL(sb_enable);

void sb_disable(void)
{
    struct sb_handler *pos;
    int count = 0;

    pr_warn("@@@@@@@@@@@@@@@@@@@@\n@@@__sb_disable__@@@\n@@@@@@@@@@@@@@@@@@@@\n");

    mutex_lock(&sb_mutex);

    pm_warn("sb_handler_count = %d, sb_handler_forbid_id = 0x%x\n", sb_handler_count, sb_handler_forbid_id);
    list_for_each_entry_reverse(pos, &sb_handlers, link) {
        if (pos->disable != NULL) {
            if (!(sb_handler_forbid_id & (0x1 << (sb_handler_count - 1 - count)))) {
                if (sbsuspend_debug_mask & DEBUG_SUSPEND)
                    pm_warn("sb disable handler %d: [%pf], level: %d\n", count, pos->disable, pos->level);
                pos->disable(pos);
            }
            count++; 
        }
    }

    if (sbsuspend_debug_mask & DEBUG_SUSPEND)
        pm_warn("sb disable handler done\n");

    //pm_warn("turn backlight on\n");
    //mt65xx_leds_brightness_set(MT65XX_LED_TYPE_LCD, 255);
    //we don't need to turn keypad light on when SmartBook plug-out
    //mt65xx_leds_brightness_set(MT65XX_LED_TYPE_BUTTON, 255);

    mutex_unlock(&sb_mutex);
}
EXPORT_SYMBOL(sb_disable);

void sb_suspend(void)
{
    struct sb_handler *pos;
    int count = 0;

    pr_warn("@@@@@@@@@@@@@@@@@@@\n@@@__sb_suspend__@@@\n@@@@@@@@@@@@@@@@@@@\n");

    mutex_lock(&sb_mutex);

    pm_warn("sb_handler_count = %d, sb_handler_forbid_id = 0x%x\n", sb_handler_count, sb_handler_forbid_id);
    list_for_each_entry(pos, &sb_handlers, link) {
        if (pos->suspend != NULL) {
            if (!(sb_handler_forbid_id & (0x1 << count))) {
                if (sbsuspend_debug_mask & DEBUG_SUSPEND)
                    pm_warn("sb enable handler %d: [%pf], level: %d\n", count, pos->suspend, pos->level);
                pos->suspend(pos);
            }
            count++; 
        }
    }

    if (sbsuspend_debug_mask & DEBUG_SUSPEND)
        pm_warn("sb enable handler done\n");

    mutex_unlock(&sb_mutex);

}
EXPORT_SYMBOL(sb_suspend);

void sb_resume(void)
{
    struct sb_handler *pos;
    int count = 0;

    pr_warn("@@@@@@@@@@@@@@@@@@@@\n@@@__sb_resume__@@@\n@@@@@@@@@@@@@@@@@@@@\n");

    mutex_lock(&sb_mutex);

    pm_warn("sb_handler_count = %d, sb_handler_forbid_id = 0x%x\n", sb_handler_count, sb_handler_forbid_id);
    list_for_each_entry_reverse(pos, &sb_handlers, link) {
        if (pos->resume != NULL) {
            if (!(sb_handler_forbid_id & (0x1 << (sb_handler_count - 1 - count)))) {
                if (sbsuspend_debug_mask & DEBUG_SUSPEND)
                    pm_warn("sb disable handler %d: [%pf], level: %d\n", count, pos->resume, pos->level);
                pos->resume(pos);
            }
            count++; 
        }
    }

    if (sbsuspend_debug_mask & DEBUG_SUSPEND)
        pm_warn("sb disable handler done\n");

    mutex_unlock(&sb_mutex);
}
EXPORT_SYMBOL(sb_resume);

void sb_plug_in(void)
{
    struct sb_handler *pos;
    int count = 0;

    if (sb_bypass)
        return;

    pr_warn("@@@@@@@@@@@@@@@@@@@\n@@@__sb_plug_in__@@@\n@@@@@@@@@@@@@@@@@@@\n");

    mutex_lock(&sb_mutex);

    pm_warn("sb_handler_count = %d, sb_handler_forbid_id = 0x%x\n", sb_handler_count, sb_handler_forbid_id);
    list_for_each_entry(pos, &sb_handlers, link) {
        if (pos->plug_in != NULL) {
            if (!(sb_handler_forbid_id & (0x1 << count))) {
                if (sbsuspend_debug_mask & DEBUG_SUSPEND)
                    pm_warn("sb plug_in handler %d: [%pf], level: %d\n", count, pos->plug_in, pos->level);
                pos->plug_in(pos);
            }
            count++; 
        }
    }

    if (sbsuspend_debug_mask & DEBUG_SUSPEND)
        pm_warn("sb plug_in handler done\n");

    mutex_unlock(&sb_mutex);

}
EXPORT_SYMBOL(sb_plug_in);

void sb_plug_out(void)
{
    struct sb_handler *pos;
    int count = 0;

    if (sb_bypass)
        return;

    pr_warn("@@@@@@@@@@@@@@@@@@@@\n@@@__sb_plug_out__@@@\n@@@@@@@@@@@@@@@@@@@@\n");

    mutex_lock(&sb_mutex);

    pm_warn("sb_handler_count = %d, sb_handler_forbid_id = 0x%x\n", sb_handler_count, sb_handler_forbid_id);
    list_for_each_entry_reverse(pos, &sb_handlers, link) {
        if (pos->plug_out != NULL) {
            if (!(sb_handler_forbid_id & (0x1 << (sb_handler_count - 1 - count)))) {
                if (sbsuspend_debug_mask & DEBUG_SUSPEND)
                    pm_warn("sb plug_out handler %d: [%pf], level: %d\n", count, pos->plug_out, pos->level);
                pos->plug_out(pos);
            }
            count++; 
        }
    }

    if (sbsuspend_debug_mask & DEBUG_SUSPEND)
        pm_warn("sb plug_out handler done\n");

    mutex_unlock(&sb_mutex);
}
EXPORT_SYMBOL(sb_plug_out);

/*
void sb_event(sb_event_t event)
{
    mutex_lock(&sb_mutex);
    
    if (sbsuspend_debug_mask & DEBUG_USER_STATE) {
        struct timespec ts;
        struct rtc_time tm;
        getnstimeofday(&ts);
        rtc_time_to_tm(ts.tv_sec, &tm);
        pm_warn("%s (%d->%d) at %lld "
            "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
            event != PM_SUSPEND_ON ? "sleep" : "wakeup",
            sb_state, event,
            ktime_to_ns(ktime_get()),
            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
            tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
    }
    
    switch (sb_state)
    {
        case SB_STATE_DISABLE:
            
        case SB_STATE_ENABLE:
            
    if (!old_sleep && new_state != PM_SUSPEND_ON) {
        state |= SUSPEND_REQUESTED;
        sb_enable();
    } else if (old_sleep && new_state == PM_SUSPEND_ON) {
        state &= ~SUSPEND_REQUESTED;
        sb_disable();
    }
    sb_state = new_state;

    mutex_unlock(&sb_mutex);
    
}
*/

/**
 *    state - control system power state.
 *
 *    show() returns what states are supported, which is hard-coded to
 *    'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
 *    'disk' (Suspend-to-Disk).
 *
 *    store() accepts one of those strings, translates it into the
 *    proper enumerated value, and initiates a suspend transition.
 */
static ssize_t sb_state_show(struct kobject *kobj, struct kobj_attribute *attr,
              char *buf)
{
    char *s = buf;
    int i;

    for (i = 0; i < SB_STATE_MAX; i++) {
        if (sb_states[i])
            s += sprintf(s,"%s ", sb_states[i]);
    }
    if (s != buf)
        /* convert the last space to a newline */
        *(s-1) = '\n';

    return (s - buf);
}

static sb_state_t decode_sb_state(const char *buf, size_t n)
{
    sb_state_t state = SB_STATE_DISABLE;
    const char * const *s;
    char *p;
    int len;

    p = memchr(buf, '\n', n);
    len = p ? p - buf : n;

    for (s = &sb_states[state]; state < SB_STATE_MAX; s++, state++)
        if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
            return state;

    return SB_STATE_MAX;
}

static ssize_t sb_state_store(struct kobject *kobj, struct kobj_attribute *attr,
               const char *buf, size_t n)
{
    sb_state_t state;
    int error = 0;
    char cmd[32];
    int param;

    if (sscanf(buf, "%s %d", cmd, &param) == 2)
    {
        if (!strcmp(cmd, "bypass"))
            sb_bypass = param;
    }
    else
    {
        if (!sb_bypass)
        {
            state = decode_sb_state(buf, n);
            
            switch (state)
            {
                case SB_STATE_DISABLE:
                    sb_disable();
                    break;
                case SB_STATE_ENABLE:
                    sb_enable();
                    break;
                case SB_STATE_SUSPEND:
                    sb_suspend();
                    break;
                case SB_STATE_RESUME:
                    sb_resume();
                    break;
                default:
                    error = -EINVAL;
                    break;
            }
        }
    }

    return error ? error : n;
}

//FIXME  walk around for FPGA Bring up, need owner to review
#ifndef CONFIG_MTK_FPGA
sb_attr(sb_state);
#endif

static int __init sbsuspend_init(void)
{
//FIXME  walk around for FPGA Bring up, need owner to review
#ifndef CONFIG_MTK_FPGA
    int err = 0;
    err |= sysfs_create_file(power_kobj, &sb_state_attr.attr);
    if (err) {
        printk("[%s]: fail to create sysfs\n", __func__);
    }
#endif
    return 0;
}

static void  __exit sbsuspend_exit(void)
{
}

core_initcall(sbsuspend_init);
module_exit(sbsuspend_exit);