blob: 3d72b973897a926e17c1fce466dbe443fd3f31eb [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>
33#include <linux/android_pmem.h>
34#include <linux/msm_audio.h>
35#include <asm/atomic.h>
36#include <asm/ioctls.h>
37#include <mach/msm_adsp.h>
38#include <mach/qdsp6v2/audio_dev_ctl.h>
39#include <sound/q6afe.h>
40
41#define SESSION_ID_FM (MAX_SESSIONS + 1)
42#define FM_ENABLE 0x1
43#define FM_DISABLE 0x0
44#define FM_COPP 0x7
45
46struct audio {
47 struct mutex lock;
48
49 int opened;
50 int enabled;
51 int running;
52
53 uint16_t fm_source;
54 uint16_t fm_src_copp_id;
55 uint16_t fm_dest;
56 uint16_t fm_dst_copp_id;
57 uint16_t dec_id;
58 uint32_t device_events;
59 uint16_t volume;
60};
61
62
63static struct audio fm_audio;
64static int fm_audio_enable(struct audio *audio)
65{
66 if (audio->enabled)
67 return 0;
68
69 pr_info("%s: fm dest= %08x fm_source = %08x\n", __func__,
70 audio->fm_dst_copp_id, audio->fm_src_copp_id);
71
72 /* do afe loopback here */
73
74 if (audio->fm_dest && audio->fm_source) {
75 if (afe_loopback(FM_ENABLE, audio->fm_dst_copp_id,
76 audio->fm_src_copp_id) < 0) {
77 pr_err("%s: afe_loopback failed\n", __func__);
78 }
79
80 audio->running = 1;
81 }
82
83 audio->enabled = 1;
84 return 0;
85}
86
87static void fm_audio_listner(u32 evt_id, union auddev_evt_data *evt_payload,
88 void *private_data)
89{
90 struct audio *audio = (struct audio *) private_data;
91 switch (evt_id) {
92 case AUDDEV_EVT_DEV_RDY:
93 pr_info("%s :AUDDEV_EVT_DEV_RDY\n", __func__);
94 if (evt_payload->routing_id == FM_COPP) {
95 audio->fm_source = 1;
96 audio->fm_src_copp_id = FM_COPP;
97 } else {
98 audio->fm_dest = 1;
99 audio->fm_dst_copp_id = evt_payload->routing_id;
100 }
101
102 if (audio->enabled &&
103 audio->fm_dest &&
104 audio->fm_source) {
105
106 afe_loopback_gain(audio->fm_src_copp_id,
107 audio->volume);
108 afe_loopback(FM_ENABLE, audio->fm_dst_copp_id,
109 audio->fm_src_copp_id);
110 audio->running = 1;
111 }
112 break;
113 case AUDDEV_EVT_DEV_RLS:
114 pr_info("%s: AUDDEV_EVT_DEV_RLS\n", __func__);
115 if (evt_payload->routing_id == audio->fm_src_copp_id)
116 audio->fm_source = 0;
117 else
118 audio->fm_dest = 0;
119 if (audio->running
120 && (!audio->fm_dest || !audio->fm_source)) {
121 afe_loopback(FM_DISABLE, audio->fm_dst_copp_id,
122 audio->fm_src_copp_id);
123 audio->running = 0;
124 } else {
125 pr_err("%s: device switch happened\n", __func__);
126 }
127 break;
128 case AUDDEV_EVT_STREAM_VOL_CHG:
129 pr_debug("%s: AUDDEV_EVT_STREAM_VOL_CHG\n", __func__);
130 if (audio->fm_source) {
131 audio->volume = evt_payload->session_vol;
132 afe_loopback_gain(audio->fm_src_copp_id,
133 audio->volume);
134 }
135 break;
136
137 default:
138 pr_err("%s: ERROR:wrong event %08x\n", __func__, evt_id);
139 break;
140 }
141}
142
143static int fm_audio_disable(struct audio *audio)
144{
145
146 /* break the AFE loopback here */
147 afe_loopback(FM_DISABLE, audio->fm_dst_copp_id, audio->fm_src_copp_id);
148 return 0;
149}
150
151static long fm_audio_ioctl(struct file *file, unsigned int cmd,
152 unsigned long arg)
153{
154 struct audio *audio = file->private_data;
155 int rc = -EINVAL;
156
157
158 mutex_lock(&audio->lock);
159 switch (cmd) {
160 case AUDIO_START:
161 pr_info("%s: AUDIO_START\n", __func__);
162 rc = fm_audio_enable(audio);
163 break;
164 case AUDIO_STOP:
165 pr_info("%s: AUDIO_STOP\n", __func__);
166 rc = fm_audio_disable(audio);
167 audio->running = 0;
168 audio->enabled = 0;
169 break;
170 case AUDIO_GET_SESSION_ID:
171 if (copy_to_user((void *) arg, &audio->dec_id,
172 sizeof(unsigned short)))
173 rc = -EFAULT;
174 else
175 rc = 0;
176 break;
177 default:
178 rc = -EINVAL;
179 pr_err("%s: Un supported IOCTL\n", __func__);
180 }
181 mutex_unlock(&audio->lock);
182 return rc;
183}
184
185static int fm_audio_release(struct inode *inode, struct file *file)
186{
187 struct audio *audio = file->private_data;
188
189 pr_debug("audio instance 0x%08x freeing\n", (int)audio);
190 mutex_lock(&audio->lock);
191 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
192 fm_audio_disable(audio);
193 audio->running = 0;
194 audio->enabled = 0;
195 audio->opened = 0;
196 mutex_unlock(&audio->lock);
197 return 0;
198}
199
200static int fm_audio_open(struct inode *inode, struct file *file)
201{
202 struct audio *audio = &fm_audio;
203 int rc = 0;
204
205
206 if (audio->opened)
207 return -EPERM;
208
209 /* Allocate the decoder */
210 audio->dec_id = SESSION_ID_FM;
211
212 audio->running = 0;
213 audio->fm_source = 0;
214 audio->fm_dest = 0;
215
216 audio->device_events = AUDDEV_EVT_DEV_RDY
217 |AUDDEV_EVT_DEV_RLS|
218 AUDDEV_EVT_STREAM_VOL_CHG;
219
220 rc = auddev_register_evt_listner(audio->device_events,
221 AUDDEV_CLNT_DEC,
222 audio->dec_id,
223 fm_audio_listner,
224 (void *)audio);
225
226 if (rc) {
227 pr_err("%s: failed to register listnet\n", __func__);
228 goto event_err;
229 }
230
231 audio->opened = 1;
232 file->private_data = audio;
233
234event_err:
235 return rc;
236}
237
238static const struct file_operations audio_fm_fops = {
239 .owner = THIS_MODULE,
240 .open = fm_audio_open,
241 .release = fm_audio_release,
242 .unlocked_ioctl = fm_audio_ioctl,
243};
244
245struct miscdevice audio_fm_misc = {
246 .minor = MISC_DYNAMIC_MINOR,
247 .name = "msm_fm",
248 .fops = &audio_fm_fops,
249};
250
251static int __init fm_audio_init(void)
252{
253 struct audio *audio = &fm_audio;
254
255 mutex_init(&audio->lock);
256 return misc_register(&audio_fm_misc);
257}
258
259device_initcall(fm_audio_init);
260
261MODULE_DESCRIPTION("MSM FM driver");
262MODULE_LICENSE("GPL v2");