summaryrefslogtreecommitdiff
path: root/sound/soc/msm/qdsp6v2/audio_slimslave.c
blob: e9ecfd5b2e44a59ade479e5915e3e8282d86ae34 (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
/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * 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/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <sound/audio_slimslave.h>
#include <linux/slimbus/slimbus.h>
#include <linux/pm_runtime.h>

static struct slim_device *slim;
static int vote_count;
struct mutex suspend_lock;
bool suspend;

static int audio_slim_open(struct inode *inode, struct file *file)
{
	pr_debug("%s:\n", __func__);

	if (vote_count) {
		pr_debug("%s: unvote: vote_count=%d\n", __func__, vote_count);
		pm_runtime_mark_last_busy(slim->dev.parent);
		pm_runtime_put(slim->dev.parent);
		vote_count--;
	}
	return 0;
};

static int audio_slim_release(struct inode *inode, struct file *file)
{
	pr_debug("%s:\n", __func__);

	if (vote_count) {
		pr_debug("%s: unvote: vote_count=%d\n", __func__, vote_count);
		pm_runtime_mark_last_busy(slim->dev.parent);
		pm_runtime_put(slim->dev.parent);
		vote_count--;
	} else {
		pr_debug("%s: vote: vote_count=%d\n", __func__, vote_count);
		pm_runtime_get_sync(slim->dev.parent);
		vote_count++;
	}
	return 0;
};

static long audio_slim_ioctl(struct file *file, unsigned int cmd,
			     unsigned long u_arg)
{
	switch (cmd) {
	case AUDIO_SLIMSLAVE_VOTE:
		mutex_lock(&suspend_lock);
		if (!vote_count && !suspend) {
			pr_debug("%s:AUDIO_SLIMSLAVE_VOTE\n", __func__);
			pm_runtime_get_sync(slim->dev.parent);
			vote_count++;
		} else {
			pr_err("%s:Invalid vote: vote_count=%d suspend=%d\n",
				 __func__, vote_count, suspend);
		}
		mutex_unlock(&suspend_lock);
		break;
	case AUDIO_SLIMSLAVE_UNVOTE:
		mutex_lock(&suspend_lock);
		if (vote_count && !suspend) {
			pr_debug("%s:AUDIO_SLIMSLAVE_UNVOTE\n", __func__);
			pm_runtime_mark_last_busy(slim->dev.parent);
			pm_runtime_put(slim->dev.parent);
			vote_count--;
		} else {
			pr_err("%s:Invalid unvote: vote_count=%d suspend=%d\n",
				 __func__, vote_count, suspend);
		}
		mutex_unlock(&suspend_lock);
		break;
	default:
		pr_debug("%s: Invalid ioctl cmd: %d\n", __func__, cmd);
		break;
	}
	return 0;
}

static const struct file_operations audio_slimslave_fops = {
	.open =                 audio_slim_open,
	.unlocked_ioctl =       audio_slim_ioctl,
	.release =              audio_slim_release,
};

struct miscdevice audio_slimslave_misc = {
	.minor  =       MISC_DYNAMIC_MINOR,
	.name   =       AUDIO_SLIMSLAVE_IOCTL_NAME,
	.fops   =       &audio_slimslave_fops,
};

static int audio_slimslave_probe(struct slim_device *audio_slim)
{
	pr_debug("%s:\n", __func__);

	mutex_init(&suspend_lock);
	suspend = false;
	slim = audio_slim;
	misc_register(&audio_slimslave_misc);
	return 0;
}

static int audio_slimslave_remove(struct slim_device *audio_slim)
{
	pr_debug("%s:\n", __func__);

	misc_deregister(&audio_slimslave_misc);
	return 0;
}

static int audio_slimslave_resume(struct slim_device *audio_slim)
{
	pr_debug("%s:\n", __func__);

	mutex_lock(&suspend_lock);
	suspend = false;
	mutex_unlock(&suspend_lock);
	return 0;
}

static int audio_slimslave_suspend(struct slim_device *audio_slim,
				   pm_message_t pmesg)
{
	pr_debug("%s:\n", __func__);

	mutex_lock(&suspend_lock);
	suspend = true;
	mutex_unlock(&suspend_lock);
	return 0;
}

static const struct slim_device_id audio_slimslave_dt_match[] = {
	{"audio-slimslave", 0},
	{}
};

static struct slim_driver audio_slimslave_driver = {
	.driver = {
		.name = "audio-slimslave",
		.owner = THIS_MODULE,
	},
	.probe = audio_slimslave_probe,
	.remove = audio_slimslave_remove,
	.id_table = audio_slimslave_dt_match,
	.resume = audio_slimslave_resume,
	.suspend = audio_slimslave_suspend,
};

static int __init audio_slimslave_init(void)
{
	return slim_driver_register(&audio_slimslave_driver);
}
module_init(audio_slimslave_init);

static void __exit audio_slimslave_exit(void)
{

}
module_exit(audio_slimslave_exit);

/* Module information */
MODULE_DESCRIPTION("Audio side Slimbus slave driver");
MODULE_LICENSE("GPL v2");