blob: b6f8689fa267202afc5c85add0d394b80273a377 [file] [log] [blame]
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001/*
Banajit Goswami88d6cc52014-04-10 17:59:02 -07002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003 * 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;
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080053 float fm_volume;
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053054 bool restart_fm;
55 int scard_state;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070056};
57
58static struct fm_module fmmod = {
59 .fm_pcm_rx = NULL,
60 .fm_pcm_tx = NULL,
61 .fm_volume = 0,
62 .is_fm_running = 0,
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +053063 .restart_fm = 0,
64 .scard_state = SND_CARD_STATE_ONLINE,
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070065};
66
67static int32_t fm_set_volume(struct audio_device *adev, float value)
68{
69 int32_t vol, ret = 0;
70 struct mixer_ctl *ctl;
Banajit Goswami88d6cc52014-04-10 17:59:02 -070071 const char *mixer_ctl_name = FM_RX_VOLUME;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070072
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080073 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070074 ALOGD("%s: (%f)\n", __func__, value);
75
76 if (value < 0.0) {
77 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
78 value = 0.0;
79 } else if (value > 1.0) {
80 ALOGW("%s: (%f) Over 1.0, assuming 1.0\n", __func__, value);
81 value = 1.0;
82 }
83 vol = lrint((value * 0x2000) + 0.5);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080084 fmmod.fm_volume = value;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070085
86 if (!fmmod.is_fm_running) {
87 ALOGV("%s: FM not active, ignoring set_fm_volume call", __func__);
88 return -EIO;
89 }
90
91 ALOGD("%s: Setting FM volume to %d \n", __func__, vol);
92 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
93 if (!ctl) {
94 ALOGE("%s: Could not get ctl for mixer cmd - %s",
95 __func__, mixer_ctl_name);
96 return -EINVAL;
97 }
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080098 mixer_ctl_set_value(ctl, 0, vol);
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -080099 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700100 return ret;
101}
102
103static int32_t fm_stop(struct audio_device *adev)
104{
105 int32_t i, ret = 0;
106 struct audio_usecase *uc_info;
107
108 ALOGD("%s: enter", __func__);
109 fmmod.is_fm_running = false;
110
111 /* 1. Close the PCM devices */
112 if (fmmod.fm_pcm_rx) {
113 pcm_close(fmmod.fm_pcm_rx);
114 fmmod.fm_pcm_rx = NULL;
115 }
116 if (fmmod.fm_pcm_tx) {
117 pcm_close(fmmod.fm_pcm_tx);
118 fmmod.fm_pcm_tx = NULL;
119 }
120
121 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_FM);
122 if (uc_info == NULL) {
123 ALOGE("%s: Could not find the usecase (%d) in the list",
124 __func__, USECASE_VOICE_CALL);
125 return -EINVAL;
126 }
127
128 /* 2. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700129 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700130
131 /* 3. Disable the rx and tx devices */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700132 disable_snd_device(adev, uc_info->out_snd_device);
133 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700134
135 list_remove(&uc_info->list);
136 free(uc_info);
137
138 ALOGD("%s: exit: status(%d)", __func__, ret);
139 return ret;
140}
141
142static int32_t fm_start(struct audio_device *adev)
143{
144 int32_t i, ret = 0;
145 struct audio_usecase *uc_info;
146 int32_t pcm_dev_rx_id, pcm_dev_tx_id;
147
148 ALOGD("%s: enter", __func__);
149
150 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700151
152 if (!uc_info)
153 return -ENOMEM;
154
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700155 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
156 uc_info->type = PCM_PLAYBACK;
157 uc_info->stream.out = adev->primary_output;
158 uc_info->devices = adev->primary_output->devices;
159 uc_info->in_snd_device = SND_DEVICE_NONE;
160 uc_info->out_snd_device = SND_DEVICE_NONE;
161
162 list_add_tail(&adev->usecase_list, &uc_info->list);
163
164 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
165
166 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
167 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
168
169 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
170 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
171 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
172 ret = -EIO;
173 goto exit;
174 }
175
176 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
177 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
178
179 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800180 __func__, adev->snd_card, pcm_dev_rx_id);
181 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
182 pcm_dev_rx_id,
183 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700184 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
185 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
186 ret = -EIO;
187 goto exit;
188 }
189
190 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800191 __func__, adev->snd_card, pcm_dev_tx_id);
192 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
193 pcm_dev_tx_id,
194 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700195 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
196 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
197 ret = -EIO;
198 goto exit;
199 }
200 pcm_start(fmmod.fm_pcm_rx);
201 pcm_start(fmmod.fm_pcm_tx);
202
203 fmmod.is_fm_running = true;
204 fm_set_volume(adev, fmmod.fm_volume);
205
206 ALOGD("%s: exit: status(%d)", __func__, ret);
207 return 0;
208
209exit:
210 fm_stop(adev);
211 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
212 return ret;
213}
214
215void audio_extn_fm_set_parameters(struct audio_device *adev,
216 struct str_parms *parms)
217{
218 int ret, val;
219 char value[32]={0};
220 float vol =0.0;
221
222 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530223 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
224 if (ret >= 0) {
225 char *snd_card_status = value+2;
226 if (strstr(snd_card_status, "OFFLINE")) {
227 fmmod.scard_state = SND_CARD_STATE_OFFLINE;
228 }
229 else if (strstr(snd_card_status, "ONLINE")) {
230 fmmod.scard_state = SND_CARD_STATE_ONLINE;
231 }
232 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700233 if(fmmod.is_fm_running) {
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530234 if (fmmod.scard_state == SND_CARD_STATE_OFFLINE) {
235 ALOGD("sound card is OFFLINE, stop FM");
236 fm_stop(adev);
237 fmmod.restart_fm = 1;
238 }
239
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700240 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
241 value, sizeof(value));
242 if (ret >= 0) {
243 val = atoi(value);
244 if(val > 0)
245 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
246 }
247 }
Dhanalakshmi Siddani7b4cb782014-05-30 11:20:27 +0530248 if (fmmod.restart_fm && (fmmod.scard_state == SND_CARD_STATE_ONLINE)) {
249 ALOGD("sound card is ONLINE, restart FM");
250 fmmod.restart_fm = 0;
251 fm_start(adev);
252 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700253
254 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
255 value, sizeof(value));
256 if (ret >= 0) {
257 val = atoi(value);
258 ALOGD("%s: FM usecase", __func__);
259 if (val != 0) {
260 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700261 && fmmod.is_fm_running == false) {
262 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700263 fm_start(adev);
Ravi Kumar Alamanda014b6b92014-04-22 15:19:13 -0700264 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700265 && fmmod.is_fm_running == true)
266 fm_stop(adev);
267 }
268 }
269
270 memset(value, 0, sizeof(value));
271 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
272 value, sizeof(value));
273 if (ret >= 0) {
274 if (sscanf(value, "%f", &vol) != 1){
275 ALOGE("%s: error in retrieving fm volume", __func__);
276 ret = -EIO;
277 goto exit;
278 }
279 ALOGD("%s: set_fm_volume usecase", __func__);
280 fm_set_volume(adev, vol);
281 }
282
283exit:
284 ALOGV("%s: exit", __func__);
285}
286#endif /* FM_ENABLED end */