aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/drivers/net/wireless/morsemicro/mm81x/ps.c
blob: ab67823452eeb86ec63c28e7402a7c1ef48102cc (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2017-2026 Morse Micro
 */
#include <linux/types.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
#include "hif.h"
#include "skbq.h"
#include "mac.h"
#include "bus.h"
#include "ps.h"

static void mm81x_ps_wakeup(struct mm81x_ps *mps)
{
	struct mm81x *mors = container_of(mps, struct mm81x, ps);

	if (!mps->enable || !mps->suspended)
		return;

	mm81x_set_bus_enable(mors, true);
	mps->suspended = false;
}

static void mm81x_ps_sleep(struct mm81x_ps *mps)
{
	struct mm81x *mors = container_of(mps, struct mm81x, ps);

	if (!mps->enable || mps->suspended)
		return;

	mps->suspended = true;
	mm81x_set_bus_enable(mors, false);
}

static void mm81x_ps_evaluate(struct mm81x_ps *mps)
{
	struct mm81x *mors = container_of(mps, struct mm81x, ps);
	bool needs_wake = false;
	unsigned long flags_on_entry =
		(mors->hif.event_flags &
		 ~BIT(MM81X_HIF_EVT_DATA_TRAFFIC_PAUSE_PEND));

	if (!mps->enable)
		return;

	needs_wake = (mps->wakers > 0);
	needs_wake |= (flags_on_entry > 0);
	needs_wake |= (mm81x_hif_get_tx_buffered_count(mors) > 0);

	if (needs_wake) {
		mm81x_ps_wakeup(mps);
		return;
	}

	mm81x_ps_sleep(mps);
}

static void mm81x_ps_evaluate_work(struct work_struct *work)
{
	struct mm81x_ps *mps =
		container_of(work, struct mm81x_ps, delayed_eval_work.work);

	if (mps->enable) {
		mutex_lock(&mps->lock);
		mm81x_ps_evaluate(mps);
		mutex_unlock(&mps->lock);
	}
}

void mm81x_ps_enable(struct mm81x *mors)
{
	struct mm81x_ps *mps = &mors->ps;

	if (mps->enable) {
		mutex_lock(&mps->lock);
		if (mps->wakers == 0) {
			WARN_ON_ONCE(1);
		} else {
			mps->wakers--;
			mm81x_ps_evaluate(mps);
		}
		mutex_unlock(&mps->lock);
	}
}

void mm81x_ps_disable(struct mm81x *mors)
{
	struct mm81x_ps *mps = &mors->ps;

	if (mps->enable) {
		mutex_lock(&mps->lock);
		mps->wakers++;
		mm81x_ps_evaluate(mps);
		mutex_unlock(&mps->lock);
	}
}

int mm81x_ps_init(struct mm81x *mors)
{
	struct mm81x_ps *mps = &mors->ps;

	mps->enable = (mors->bus_type == MM81X_BUS_TYPE_USB);
	mps->suspended = true;
	mps->wakers = 1; /* we default to being on */
	mutex_init(&mps->lock);
	INIT_DELAYED_WORK(&mps->delayed_eval_work, mm81x_ps_evaluate_work);

	return 0;
}

void mm81x_ps_finish(struct mm81x *mors)
{
	struct mm81x_ps *mps = &mors->ps;

	if (mps->enable) {
		mps->enable = false;
		cancel_delayed_work_sync(&mps->delayed_eval_work);
	}
}