blob: cffa7e72d1885fe5dfb4d2482f7758575ade4c38 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2009-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/kthread.h>
29#include <linux/wait.h>
30#include <linux/dma-mapping.h>
31#include <linux/delay.h>
32#include <linux/android_pmem.h>
33#include <linux/msm_audio.h>
34#include <asm/atomic.h>
35#include <asm/ioctls.h>
36#include <mach/msm_adsp.h>
37#include <mach/debug_mm.h>
38#include <mach/qdsp5v2/audio_dev_ctl.h>
39#include <mach/qdsp5v2/afe.h>
40#include <mach/qdsp5v2/acdb_commands.h>
41#include <mach/qdsp5v2/audio_acdbi.h>
42#include <mach/qdsp5v2/audio_acdb_def.h>
43
44#define SESSION_ID_FM 6
45#define FM_ENABLE 0xFFFF
46#define FM_DISABLE 0x0
47#define FM_COPP 0x2
48/* Macro specifies maximum FM routing
49 possible */
50#define FM_MAX_RX_ROUTE 0x2
51
52struct fm_rx_calib_gain {
53 uint16_t device_id;
54 struct auddev_evt_devinfo dev_details;
55 struct acdb_calib_gain_rx calib_rx;
56};
57
58struct audio {
59 struct mutex lock;
60
61 int opened;
62 int enabled;
63 int running;
64
65 uint16_t dec_id;
66 uint16_t source;
67 uint16_t fm_source;
68 uint16_t fm_mask;
69 uint32_t device_events;
70 uint16_t volume;
71 struct fm_rx_calib_gain fm_calibration_rx[FM_MAX_RX_ROUTE];
72};
73
74static struct audio fm_audio;
75
76/* must be called with audio->lock held */
77static int audio_enable(struct audio *audio)
78{
79 int rc = 0;
80 if (audio->enabled)
81 return 0;
82
83 MM_DBG("fm mask= %08x fm_source = %08x\n",
84 audio->fm_mask, audio->fm_source);
85 if (audio->fm_mask && audio->fm_source) {
86 rc = afe_config_fm_codec(FM_ENABLE, audio->fm_mask);
87 if (!rc)
88 audio->running = 1;
89 /* Routed to icodec rx path */
90 if ((audio->fm_mask & AFE_HW_PATH_CODEC_RX) ==
91 AFE_HW_PATH_CODEC_RX) {
92 afe_config_fm_calibration_gain(
93 audio->fm_calibration_rx[0].device_id,
94 audio->fm_calibration_rx[0].calib_rx.audppcalgain);
95 }
96 /* Routed to aux codec rx path */
97 if ((audio->fm_mask & AFE_HW_PATH_AUXPCM_RX) ==
98 AFE_HW_PATH_AUXPCM_RX){
99 afe_config_fm_calibration_gain(
100 audio->fm_calibration_rx[1].device_id,
101 audio->fm_calibration_rx[1].calib_rx.audppcalgain);
102 }
103 }
104
105 audio->enabled = 1;
106 return rc;
107}
108
109static void fm_listner(u32 evt_id, union auddev_evt_data *evt_payload,
110 void *private_data)
111{
112 struct audio *audio = (struct audio *) private_data;
113 struct auddev_evt_devinfo *devinfo =
114 (struct auddev_evt_devinfo *)evt_payload;
115 switch (evt_id) {
116 case AUDDEV_EVT_DEV_RDY:
117 MM_DBG(":AUDDEV_EVT_DEV_RDY\n");
118 if (evt_payload->routing_id == FM_COPP)
119 audio->fm_source = 1;
120 else
121 audio->source = (0x1 << evt_payload->routing_id);
122
123 if (audio->source & 0x1)
124 audio->fm_mask = 0x1;
125 else if (audio->source & 0x2)
126 audio->fm_mask = 0x3;
127 else
128 audio->fm_mask = 0x0;
129
130 if (!audio->enabled
131 || !audio->fm_mask
132 || !audio->fm_source)
133 break;
134 else {
135 afe_config_fm_codec(FM_ENABLE, audio->fm_mask);
136 audio->running = 1;
137 }
138 break;
139 case AUDDEV_EVT_DEV_RLS:
140 MM_DBG(":AUDDEV_EVT_DEV_RLS\n");
141 if (evt_payload->routing_id == FM_COPP)
142 audio->fm_source = 0;
143 else
144 audio->source &= ~(0x1 << evt_payload->routing_id);
145
146 if (audio->source & 0x1)
147 audio->fm_mask = 0x1;
148 else if (audio->source & 0x2)
149 audio->fm_mask = 0x3;
150 else
151 audio->fm_mask = 0x0;
152
153 if (audio->running
154 && (!audio->fm_mask || !audio->fm_source)) {
155 afe_config_fm_codec(FM_DISABLE, audio->fm_mask);
156 audio->running = 0;
157 }
158 break;
159 case AUDDEV_EVT_STREAM_VOL_CHG:
160 MM_DBG(":AUDDEV_EVT_STREAM_VOL_CHG, stream vol \n");
161 audio->volume = evt_payload->session_vol;
162 afe_config_fm_volume(audio->volume);
163 break;
164 case AUDDEV_EVT_DEVICE_INFO:{
165 struct acdb_get_block get_block;
166 int rc = 0;
167 MM_DBG(":AUDDEV_EVT_DEVICE_INFO\n");
168 MM_DBG("sample_rate = %d\n", devinfo->sample_rate);
169 MM_DBG("acdb_id = %d\n", devinfo->acdb_id);
170 /* Applucable only for icodec rx and aux codec rx path
171 and fm stream routed to it */
172 if (((devinfo->dev_id == 0x00) || (devinfo->dev_id == 0x01)) &&
173 (devinfo->sessions && (1 << audio->dec_id))) {
174 /* Query ACDB driver for calib gain, only if difference
175 in device */
176 if ((audio->fm_calibration_rx[devinfo->dev_id].
177 dev_details.acdb_id != devinfo->acdb_id) ||
178 (audio->fm_calibration_rx[devinfo->dev_id].
179 dev_details.sample_rate !=
180 devinfo->sample_rate)) {
181 audio->fm_calibration_rx[devinfo->dev_id].
182 dev_details.dev_id = devinfo->dev_id;
183 audio->fm_calibration_rx[devinfo->dev_id].
184 dev_details.sample_rate =
185 devinfo->sample_rate;
186 audio->fm_calibration_rx[devinfo->dev_id].
187 dev_details.dev_type =
188 devinfo->dev_type;
189 audio->fm_calibration_rx[devinfo->dev_id].
190 dev_details.sessions =
191 devinfo->sessions;
192 /* Query ACDB driver for calibration gain */
193 get_block.acdb_id = devinfo->acdb_id;
194 get_block.sample_rate_id = devinfo->sample_rate;
195 get_block.interface_id =
196 IID_AUDIO_CALIBRATION_GAIN_RX;
197 get_block.algorithm_block_id =
198 ABID_AUDIO_CALIBRATION_GAIN_RX;
199 get_block.total_bytes =
200 sizeof(struct acdb_calib_gain_rx);
201 get_block.buf_ptr = (u32 *)
202 &audio->fm_calibration_rx[devinfo->dev_id].
203 calib_rx;
204
205 rc = acdb_get_calibration_data(&get_block);
206 if (rc < 0) {
207 MM_ERR("Unable to get calibration"\
208 "gain\n");
209 /* Set to unity incase of error */
210 audio->\
211 fm_calibration_rx[devinfo->dev_id].
212 calib_rx.audppcalgain = 0x2000;
213 } else
214 MM_DBG("calibration gain = 0x%8x\n",
215 *(get_block.buf_ptr));
216 }
217 if (audio->running) {
218 afe_config_fm_calibration_gain(
219 audio->fm_calibration_rx[devinfo->dev_id].
220 device_id,
221 audio->fm_calibration_rx[devinfo->dev_id].
222 calib_rx.audppcalgain);
223 }
224 }
225 break;
226 }
227 default:
228 MM_DBG(":ERROR:wrong event\n");
229 break;
230 }
231}
232/* must be called with audio->lock held */
233static int audio_disable(struct audio *audio)
234{
235 MM_DBG("\n"); /* Macro prints the file name and function */
236 return afe_config_fm_codec(FM_DISABLE, audio->source);
237}
238
239static long audio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
240{
241 struct audio *audio = file->private_data;
242 int rc = -EINVAL;
243
244 MM_DBG("cmd = %d\n", cmd);
245
246 mutex_lock(&audio->lock);
247 switch (cmd) {
248 case AUDIO_START:
249 MM_DBG("AUDIO_START\n");
250 rc = audio_enable(audio);
251 break;
252 case AUDIO_STOP:
253 MM_DBG("AUDIO_STOP\n");
254 rc = audio_disable(audio);
255 audio->running = 0;
256 audio->enabled = 0;
257 break;
258 case AUDIO_GET_SESSION_ID:
259 if (copy_to_user((void *) arg, &audio->dec_id,
260 sizeof(unsigned short)))
261 rc = -EFAULT;
262 else
263 rc = 0;
264 break;
265 default:
266 rc = -EINVAL;
267 }
268 mutex_unlock(&audio->lock);
269 return rc;
270}
271
272static int audio_release(struct inode *inode, struct file *file)
273{
274 struct audio *audio = file->private_data;
275
276 MM_DBG("audio instance 0x%08x freeing\n", (int)audio);
277 mutex_lock(&audio->lock);
278 auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
279 audio_disable(audio);
280 audio->running = 0;
281 audio->enabled = 0;
282 audio->opened = 0;
283 mutex_unlock(&audio->lock);
284 return 0;
285}
286
287static int audio_open(struct inode *inode, struct file *file)
288{
289 struct audio *audio = &fm_audio;
290 int rc = 0;
291
292
293 if (audio->opened)
294 return -EPERM;
295
296 /* Allocate the decoder */
297 audio->dec_id = SESSION_ID_FM;
298
299 audio->running = 0;
300 audio->fm_source = 0;
301 audio->fm_mask = 0;
302
303 /* Initialize the calibration gain structure */
304 audio->fm_calibration_rx[0].device_id = AFE_HW_PATH_CODEC_RX;
305 audio->fm_calibration_rx[1].device_id = AFE_HW_PATH_AUXPCM_RX;
306 audio->fm_calibration_rx[0].calib_rx.audppcalgain = 0x2000;
307 audio->fm_calibration_rx[1].calib_rx.audppcalgain = 0x2000;
308 audio->fm_calibration_rx[0].dev_details.acdb_id = PSEUDO_ACDB_ID;
309 audio->fm_calibration_rx[1].dev_details.acdb_id = PSEUDO_ACDB_ID;
310
311 audio->device_events = AUDDEV_EVT_DEV_RDY
312 |AUDDEV_EVT_DEV_RLS|
313 AUDDEV_EVT_STREAM_VOL_CHG|
314 AUDDEV_EVT_DEVICE_INFO;
315
316 rc = auddev_register_evt_listner(audio->device_events,
317 AUDDEV_CLNT_DEC,
318 audio->dec_id,
319 fm_listner,
320 (void *)audio);
321
322 if (rc) {
323 MM_ERR("%s: failed to register listnet\n", __func__);
324 goto event_err;
325 }
326
327 audio->opened = 1;
328 file->private_data = audio;
329
330event_err:
331 return rc;
332}
333
334static const struct file_operations audio_fm_fops = {
335 .owner = THIS_MODULE,
336 .open = audio_open,
337 .release = audio_release,
338 .unlocked_ioctl = audio_ioctl,
339};
340
341struct miscdevice audio_fm_misc = {
342 .minor = MISC_DYNAMIC_MINOR,
343 .name = "msm_fm",
344 .fops = &audio_fm_fops,
345};
346
347static int __init audio_init(void)
348{
349 struct audio *audio = &fm_audio;
350
351 mutex_init(&audio->lock);
352 return misc_register(&audio_fm_misc);
353}
354
355device_initcall(audio_init);
356
357MODULE_DESCRIPTION("MSM FM driver");
358MODULE_LICENSE("GPL v2");