1 /*
2 * Copyright (c) 2013-2014, 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_POWER_OPT
35 #define AUDIO_PARAMETER_KEY_HANDLE_FM "handle_fm"
36 #define AUDIO_PARAMETER_KEY_FM_VOLUME "fm_volume"
37 #define AUDIO_PARAMETER_KEY_REC_PLAY_CONC "rec_play_conc_on"
38
39 static struct pcm_config pcm_config_fm = {
40 .channels = 2,
41 .rate = 48000,
42 .period_size = 256,
43 .period_count = 4,
44 .format = PCM_FORMAT_S16_LE,
45 .start_threshold = 0,
46 .stop_threshold = INT_MAX,
47 .avail_min = 0,
48 };
49
50 struct fm_module {
51 struct pcm *fm_pcm_rx;
52 struct pcm *fm_pcm_tx;
53 bool is_fm_running;
54 float fm_volume;
55 bool restart_fm;
56 int scard_state;
57 };
58
59 static struct fm_module fmmod = {
60 .fm_pcm_rx = NULL,
61 .fm_pcm_tx = NULL,
62 .fm_volume = 0,
63 .is_fm_running = 0,
64 .restart_fm = 0,
65 .scard_state = SND_CARD_STATE_ONLINE,
66 };
67
fm_set_volume(struct audio_device * adev,float value)68 static int32_t fm_set_volume(struct audio_device *adev, float value)
69 {
70 int32_t vol, ret = 0;
71 struct mixer_ctl *ctl;
72 const char *mixer_ctl_name = FM_RX_VOLUME;
73
74 ALOGV("%s: entry", __func__);
75 ALOGD("%s: (%f)\n", __func__, value);
76
77 if (value < 0.0) {
78 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
79 value = 0.0;
80 } else if (value > 1.0) {
81 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
82 value = 1.0;
83 }
84 vol = lrint((value * 0x2000) + 0.5);
85 fmmod.fm_volume = value;
86
87 if (!fmmod.is_fm_running) {
88 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
89 return -EIO;
90 }
91
92 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
93 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
94 if (!ctl) {
95 ALOGE("%s: Could not get ctl for mixer cmd - %s",
96 __func__, mixer_ctl_name);
97 return -EINVAL;
98 }
99 mixer_ctl_set_value(ctl, 0, vol);
100 ALOGV("%s: exit", __func__);
101 return ret;
102 }
103
fm_stop(struct audio_device * adev)104 static int32_t fm_stop(struct audio_device *adev)
105 {
106 int32_t i, ret = 0;
107 struct audio_usecase *uc_info;
108
109 ALOGD("%s: enter", __func__);
110 fmmod.is_fm_running = false;
111
112 /* 1. Close the PCM devices */
113 if (fmmod.fm_pcm_rx) {
114 pcm_close(fmmod.fm_pcm_rx);
115 fmmod.fm_pcm_rx = NULL;
116 }
117 if (fmmod.fm_pcm_tx) {
118 pcm_close(fmmod.fm_pcm_tx);
119 fmmod.fm_pcm_tx = NULL;
120 }
121
122 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
123 if (uc_info == NULL) {
124 ALOGE("%s: Could not find the usecase (%d) in the list",
125 __func__, USECASE_VOICE_CALL);
126 return -EINVAL;
127 }
128
129 /* 2. Get and set stream specific mixer controls */
130 disable_audio_route(adev, uc_info);
131
132 /* 3. Disable the rx and tx devices */
133 disable_snd_device(adev, uc_info->out_snd_device);
134 disable_snd_device(adev, uc_info->in_snd_device);
135
136 list_remove(&uc_info->list);
137 free(uc_info);
138
139 ALOGD("%s: exit: status(%d)", __func__, ret);
140 return ret;
141 }
142
fm_start(struct audio_device * adev)143 static int32_t fm_start(struct audio_device *adev)
144 {
145 int32_t i, ret = 0;
146 struct audio_usecase *uc_info;
147 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
148
149 ALOGD("%s: enter", __func__);
150
151 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
152
153 if (!uc_info)
154 return -ENOMEM;
155
156 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
157 uc_info->type = PCM_PLAYBACK;
158 uc_info->stream.out = adev->primary_output;
159 uc_info->devices = adev->primary_output->devices;
160 uc_info->in_snd_device = SND_DEVICE_NONE;
161 uc_info->out_snd_device = SND_DEVICE_NONE;
162
163 list_add_tail(&adev->usecase_list, &uc_info->list);
164
165 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
166
167 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
168 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
169
170 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
171 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
172 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
173 ret = -EIO;
174 goto exit;
175 }
176
177 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
178 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
179
180 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
181 __func__, adev->snd_card, pcm_dev_rx_id);
182 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
183 pcm_dev_rx_id,
184 PCM_OUT, &pcm_config_fm);
185 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
186 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
187 ret = -EIO;
188 goto exit;
189 }
190
191 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
192 __func__, adev->snd_card, pcm_dev_tx_id);
193 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
194 pcm_dev_tx_id,
195 PCM_IN, &pcm_config_fm);
196 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
197 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
198 ret = -EIO;
199 goto exit;
200 }
201 pcm_start(fmmod.fm_pcm_rx);
202 pcm_start(fmmod.fm_pcm_tx);
203
204 fmmod.is_fm_running = true;
205 fm_set_volume(adev, fmmod.fm_volume);
206
207 ALOGD("%s: exit: status(%d)", __func__, ret);
208 return 0;
209
210 exit:
211 fm_stop(adev);
212 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
213 return ret;
214 }
215
audio_extn_fm_set_parameters(struct audio_device * adev,struct str_parms * parms)216 void audio_extn_fm_set_parameters(struct audio_device *adev,
217 struct str_parms *parms)
218 {
219 int ret, val;
220 char value[32]={0};
221 float vol =0.0;
222
223 ALOGV("%s: enter", __func__);
224 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
225 if (ret >= 0) {
226 char *snd_card_status = value+2;
227 if (strstr(snd_card_status, "OFFLINE")) {
228 fmmod.scard_state = SND_CARD_STATE_OFFLINE;
229 }
230 else if (strstr(snd_card_status, "ONLINE")) {
231 fmmod.scard_state = SND_CARD_STATE_ONLINE;
232 }
233 }
234 if(fmmod.is_fm_running) {
235 if (fmmod.scard_state == SND_CARD_STATE_OFFLINE) {
236 ALOGD("sound card is OFFLINE, stop FM");
237 fm_stop(adev);
238 fmmod.restart_fm = 1;
239 }
240
241 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
242 value, sizeof(value));
243 if (ret >= 0) {
244 val = atoi(value);
245 if(val > 0)
246 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
247 }
248 }
249 if (fmmod.restart_fm && (fmmod.scard_state == SND_CARD_STATE_ONLINE)) {
250 ALOGD("sound card is ONLINE, restart FM");
251 fmmod.restart_fm = 0;
252 fm_start(adev);
253 }
254
255 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
256 value, sizeof(value));
257 if (ret >= 0) {
258 val = atoi(value);
259 ALOGD("%s: FM usecase", __func__);
260 if (val != 0) {
261 if(val & AUDIO_DEVICE_OUT_FM
262 && fmmod.is_fm_running == false) {
263 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
264 fm_start(adev);
265 } else if (!(val & AUDIO_DEVICE_OUT_FM)
266 && fmmod.is_fm_running == true)
267 fm_stop(adev);
268 }
269 }
270
271 memset(value, 0, sizeof(value));
272 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
273 value, sizeof(value));
274 if (ret >= 0) {
275 if (sscanf(value, "%f", &vol) != 1){
276 ALOGE("%s: error in retrieving fm volume", __func__);
277 ret = -EIO;
278 goto exit;
279 }
280 ALOGD("%s: set_fm_volume usecase", __func__);
281 fm_set_volume(adev, vol);
282 }
283
284 #ifdef RECORD_PLAY_CONCURRENCY
285 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_REC_PLAY_CONC,
286 value, sizeof(value));
287 if ((ret >= 0)
288 && (fmmod.is_fm_running == true)) {
289
290 if (!strncmp("true", value, sizeof("true")))
291 ALOGD("Record play concurrency ON Forcing FM device reroute");
292 else
293 ALOGD("Record play concurrency OFF Forcing FM device reroute");
294
295 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
296 fm_set_volume(adev,fmmod.fm_volume);
297 }
298 #endif
299 exit:
300 ALOGV("%s: exit", __func__);
301 }
302 #endif /* FM_POWER_OPT end */
303