blob: 53499b9b170be752407b4c8949f85fe5a9a75371 [file] [log] [blame]
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_fm"
21/*#define LOG_NDEBUG 0*/
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
25#include <math.h>
26#include <cutils/log.h>
27
28#include "audio_hw.h"
29#include "platform.h"
30#include "platform_api.h"
31#include <stdlib.h>
32#include <cutils/str_parms.h>
33
34#ifdef FM_ENABLED
35#define AUDIO_PARAMETER_KEY_HANDLE_FM "handle_fm"
36#define AUDIO_PARAMETER_KEY_FM_VOLUME "fm_volume"
37
38static struct pcm_config pcm_config_fm = {
39 .channels = 2,
40 .rate = 48000,
41 .period_size = 256,
42 .period_count = 4,
43 .format = PCM_FORMAT_S16_LE,
44 .start_threshold = 0,
45 .stop_threshold = INT_MAX,
46 .avail_min = 0,
47};
48
49struct fm_module {
50 struct pcm *fm_pcm_rx;
51 struct pcm *fm_pcm_tx;
52 bool is_fm_running;
53 int fm_volume;
54};
55
56static struct fm_module fmmod = {
57 .fm_pcm_rx = NULL,
58 .fm_pcm_tx = NULL,
59 .fm_volume = 0,
60 .is_fm_running = 0,
61};
62
63static int32_t fm_set_volume(struct audio_device *adev, float value)
64{
65 int32_t vol, ret = 0;
66 struct mixer_ctl *ctl;
67 const char *mixer_ctl_name = "Internal FM RX Volume";
68
69 ALOGD("%s: entry", __func__);
70 ALOGD("%s: (%f)\n", __func__, value);
71
72 if (value < 0.0) {
73 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
74 value = 0.0;
75 } else if (value > 1.0) {
76 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
77 value = 1.0;
78 }
79 vol = lrint((value * 0x2000) + 0.5);
80
81 fmmod.fm_volume = vol;
82
83 if (!fmmod.is_fm_running) {
84 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
85 return -EIO;
86 }
87
88 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
89 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
90 if (!ctl) {
91 ALOGE("%s: Could not get ctl for mixer cmd - %s",
92 __func__, mixer_ctl_name);
93 return -EINVAL;
94 }
95 mixer_ctl_set_value(ctl, 0, fmmod.fm_volume);
96
97 ALOGD("%s: exit", __func__);
98 return ret;
99}
100
101static int32_t fm_stop(struct audio_device *adev)
102{
103 int32_t i, ret = 0;
104 struct audio_usecase *uc_info;
105
106 ALOGD("%s: enter", __func__);
107 fmmod.is_fm_running = false;
108
109 /* 1. Close the PCM devices */
110 if (fmmod.fm_pcm_rx) {
111 pcm_close(fmmod.fm_pcm_rx);
112 fmmod.fm_pcm_rx = NULL;
113 }
114 if (fmmod.fm_pcm_tx) {
115 pcm_close(fmmod.fm_pcm_tx);
116 fmmod.fm_pcm_tx = NULL;
117 }
118
119 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
120 if (uc_info == NULL) {
121 ALOGE("%s: Could not find the usecase (%d) in the list",
122 __func__, USECASE_VOICE_CALL);
123 return -EINVAL;
124 }
125
126 /* 2. Get and set stream specific mixer controls */
127 disable_audio_route(adev, uc_info, true);
128
129 /* 3. Disable the rx and tx devices */
130 disable_snd_device(adev, uc_info->out_snd_device, false);
131 disable_snd_device(adev, uc_info->in_snd_device, true);
132
133 list_remove(&uc_info->list);
134 free(uc_info);
135
136 ALOGD("%s: exit: status(%d)", __func__, ret);
137 return ret;
138}
139
140static int32_t fm_start(struct audio_device *adev)
141{
142 int32_t i, ret = 0;
143 struct audio_usecase *uc_info;
144 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
145
146 ALOGD("%s: enter", __func__);
147
148 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
149 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
150 uc_info->type = PCM_PLAYBACK;
151 uc_info->stream.out = adev->primary_output;
152 uc_info->devices = adev->primary_output->devices;
153 uc_info->in_snd_device = SND_DEVICE_NONE;
154 uc_info->out_snd_device = SND_DEVICE_NONE;
155
156 list_add_tail(&adev->usecase_list, &uc_info->list);
157
158 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
159
160 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
161 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
162
163 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
164 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
165 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
166 ret = -EIO;
167 goto exit;
168 }
169
170 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
171 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
172
173 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
174 __func__, SOUND_CARD, pcm_dev_rx_id);
175 fmmod.fm_pcm_rx = pcm_open(SOUND_CARD,
176 pcm_dev_rx_id,
177 PCM_OUT, &pcm_config_fm);
178 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
179 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
180 ret = -EIO;
181 goto exit;
182 }
183
184 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
185 __func__, SOUND_CARD, pcm_dev_tx_id);
186 fmmod.fm_pcm_tx = pcm_open(SOUND_CARD,
187 pcm_dev_tx_id,
188 PCM_IN, &pcm_config_fm);
189 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
190 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
191 ret = -EIO;
192 goto exit;
193 }
194 pcm_start(fmmod.fm_pcm_rx);
195 pcm_start(fmmod.fm_pcm_tx);
196
197 fmmod.is_fm_running = true;
198 fm_set_volume(adev, fmmod.fm_volume);
199
200 ALOGD("%s: exit: status(%d)", __func__, ret);
201 return 0;
202
203exit:
204 fm_stop(adev);
205 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
206 return ret;
207}
208
209void audio_extn_fm_set_parameters(struct audio_device *adev,
210 struct str_parms *parms)
211{
212 int ret, val;
213 char value[32]={0};
214 float vol =0.0;
215
216 ALOGV("%s: enter", __func__);
217 if(fmmod.is_fm_running) {
218 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
219 value, sizeof(value));
220 if (ret >= 0) {
221 val = atoi(value);
222 if(val > 0)
223 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
224 }
225 }
226
227 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
228 value, sizeof(value));
229 if (ret >= 0) {
230 val = atoi(value);
231 ALOGD("%s: FM usecase", __func__);
232 if (val != 0) {
233 if(val & AUDIO_DEVICE_OUT_FM
234 && fmmod.is_fm_running == false)
235 fm_start(adev);
236 else if (!(val & AUDIO_DEVICE_OUT_FM)
237 && fmmod.is_fm_running == true)
238 fm_stop(adev);
239 }
240 }
241
242 memset(value, 0, sizeof(value));
243 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
244 value, sizeof(value));
245 if (ret >= 0) {
246 if (sscanf(value, "%f", &vol) != 1){
247 ALOGE("%s: error in retrieving fm volume", __func__);
248 ret = -EIO;
249 goto exit;
250 }
251 ALOGD("%s: set_fm_volume usecase", __func__);
252 fm_set_volume(adev, vol);
253 }
254
255exit:
256 ALOGV("%s: exit", __func__);
257}
258#endif /* FM_ENABLED end */