blob: 23bb716db640c079f6422a50d213651fe1737c11 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * Based on the mp3 native driver in arch/arm/mach-msm/qdsp5v2/audio_mp3.c
4 *
5 * Copyright (C) 2008 Google, Inc.
6 * Copyright (C) 2008 HTC Corporation
7 *
8 * All source code in this file is licensed under the following license except
9 * where indicated.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * See the GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you can find it at http://www.fsf.org
22 */
23
24#include <linux/module.h>
25#include <linux/fs.h>
26#include <linux/miscdevice.h>
27#include <linux/uaccess.h>
28#include <linux/clk.h>
29#include <linux/kthread.h>
30#include <linux/wait.h>
31#include <linux/dma-mapping.h>
32#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <linux/msm_audio.h>
34#include <asm/atomic.h>
35#include <asm/ioctls.h>
36#include <mach/msm_adsp.h>
37#include <mach/qdsp6v2/audio_dev_ctl.h>
38#include <sound/q6afe.h>
39
40#define SESSION_ID_FM (MAX_SESSIONS + 1)
41#define FM_ENABLE 0x1
42#define FM_DISABLE 0x0
43#define FM_COPP 0x7
44
45struct audio {
46 struct mutex lock;
47
48 int opened;
49 int enabled;
50 int running;
51
52 uint16_t fm_source;
53 uint16_t fm_src_copp_id;
54 uint16_t fm_dest;
55 uint16_t fm_dst_copp_id;
56 uint16_t dec_id;
57 uint32_t device_events;
58 uint16_t volume;
59};
60
61
62static struct audio fm_audio;
63static int fm_audio_enable(struct audio *audio)
64{
65 if (audio->enabled)
66 return 0;
67
68 pr_info("%s: fm dest= %08x fm_source = %08x\n", __func__,
69 audio->fm_dst_copp_id, audio->fm_src_copp_id);
70
71 /* do afe loopback here */
72
73 if (audio->fm_dest && audio->fm_source) {
74 if (afe_loopback(FM_ENABLE, audio->fm_dst_copp_id,
75 audio->fm_src_copp_id) < 0) {
76 pr_err("%s: afe_loopback failed\n", __func__);
77 }
78
79 audio->running = 1;
80 }
81
82 audio->enabled = 1;
83 return 0;
84}
85
86static void fm_audio_listner(u32 evt_id, union auddev_evt_data *evt_payload,
87 void *private_data)
88{
89 struct audio *audio = (struct audio *) private_data;
90 switch (evt_id) {
91 case AUDDEV_EVT_DEV_RDY:
92 pr_info("%s :AUDDEV_EVT_DEV_RDY\n", __func__);
93 if (evt_payload->routing_id == FM_COPP) {
94 audio->fm_source = 1;
95 audio->fm_src_copp_id = FM_COPP;
96 } else {
97 audio->fm_dest = 1;
98 audio->fm_dst_copp_id = evt_payload->routing_id;
99 }
100
101 if (audio->enabled &&
102 audio->fm_dest &&
103 audio->fm_source) {
104
105 afe_loopback_gain(audio->fm_src_copp_id,
106 audio->volume);
107 afe_loopback(FM_ENABLE, audio->fm_dst_copp_id,
108 audio->fm_src_copp_id);
109 audio->running = 1;
110 }
111 break;
112 case AUDDEV_EVT_DEV_RLS:
113 pr_info("%s: AUDDEV_EVT_DEV_RLS\n", __func__);
114 if (evt_payload->routing_id == audio->fm_src_copp_id)
115 audio->fm_source = 0;
116 else
117 audio->fm_dest = 0;
118 if (audio->running
119 && (!audio->fm_dest || !audio->fm_source)) {
120 afe_loopback(FM_DISABLE, audio->fm_dst_copp_id,
121 audio->fm_src_copp_id);
122 audio->running = 0;
123 } else {
124 pr_err("%s: device switch happened\n", __func__);
125 }
126 break;
127 case AUDDEV_EVT_STREAM_VOL_CHG:
128 pr_debug("%s: AUDDEV_EVT_STREAM_VOL_CHG\n", __func__);
129 if (audio->fm_source) {
130 audio->volume = evt_payload->session_vol;
131 afe_loopback_gain(audio->fm_src_copp_id,
132 audio->volume);
133 }
134 break;
135
136 default:
137 pr_err("%s: ERROR:wrong event %08x\n", __func__, evt_id);
138 break;
139 }
140}
141
142static int fm_audio_disable(struct audio *audio)
143{
144
145 /* break the AFE loopback here */
146 afe_loopback(FM_DISABLE, audio->fm_dst_copp_id, audio->fm_src_copp_id);
147 return 0;
148}
149
150static long fm_audio_ioctl(struct file *file, unsigned int cmd,
151 unsigned long arg)
152{
153 struct audio *audio = file->private_data;
154 int rc = -EINVAL;
155
156
157 mutex_lock(&audio->lock);
158 switch (cmd) {
159 case AUDIO_START:
160 pr_info("%s: AUDIO_START\n", __func__);
161 rc = fm_audio_enable(audio);
162 break;
163 case AUDIO_STOP:
164 pr_info("%s: AUDIO_STOP\n", __func__);
165 rc = fm_audio_disable(audio);
166 audio->running = 0;
167 audio->enabled = 0;
168 break;
169 case AUDIO_GET_SESSION_ID:
170 if (copy_to_user((void *) arg, &audio->dec_id,
171 sizeof(unsigned short)))
172 rc = -EFAULT;
173 else
174 rc = 0;
175 break;
176 default:
177 rc = -EINVAL;
178 pr_err("%s: Un supported IOCTL\n", __func__);
179 }
180 mutex_unlock(&audio->lock);
181 return rc;
182}
183
184static int fm_audio_release(struct inode *inode, struct file *file)
185{
186 struct audio *audio = file->private_data;
187
188 pr_debug("audio instance 0x%08x freeing\n", (int)audio);
189 mutex_lock(&audio->lock);
190 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
191 fm_audio_disable(audio);
192 audio->running = 0;
193 audio->enabled = 0;
194 audio->opened = 0;
195 mutex_unlock(&audio->lock);
196 return 0;
197}
198
199static int fm_audio_open(struct inode *inode, struct file *file)
200{
201 struct audio *audio = &fm_audio;
202 int rc = 0;
203
204
205 if (audio->opened)
206 return -EPERM;
207
208 /* Allocate the decoder */
209 audio->dec_id = SESSION_ID_FM;
210
211 audio->running = 0;
212 audio->fm_source = 0;
213 audio->fm_dest = 0;
214
215 audio->device_events = AUDDEV_EVT_DEV_RDY
216 |AUDDEV_EVT_DEV_RLS|
217 AUDDEV_EVT_STREAM_VOL_CHG;
218
219 rc = auddev_register_evt_listner(audio->device_events,
220 AUDDEV_CLNT_DEC,
221 audio->dec_id,
222 fm_audio_listner,
223 (void *)audio);
224
225 if (rc) {
226 pr_err("%s: failed to register listnet\n", __func__);
227 goto event_err;
228 }
229
230 audio->opened = 1;
231 file->private_data = audio;
232
233event_err:
234 return rc;
235}
236
237static const struct file_operations audio_fm_fops = {
238 .owner = THIS_MODULE,
239 .open = fm_audio_open,
240 .release = fm_audio_release,
241 .unlocked_ioctl = fm_audio_ioctl,
242};
243
244struct miscdevice audio_fm_misc = {
245 .minor = MISC_DYNAMIC_MINOR,
246 .name = "msm_fm",
247 .fops = &audio_fm_fops,
248};
249
250static int __init fm_audio_init(void)
251{
252 struct audio *audio = &fm_audio;
253
254 mutex_init(&audio->lock);
255 return misc_register(&audio_fm_misc);
256}
257
258device_initcall(fm_audio_init);
259
260MODULE_DESCRIPTION("MSM FM driver");
261MODULE_LICENSE("GPL v2");