blob: d18c88b10606dac4511e5e4816a2237c281021ab [file] [log] [blame]
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001/*
Haynes Mathew Georgeea098922014-04-24 17:53:50 -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
Ramjee Singhfd4323f2015-11-18 13:26:59 +053034#ifdef FM_POWER_OPT
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070035#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 Siddani6f461b92014-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 Siddani6f461b92014-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;
71 const char *mixer_ctl_name = "Internal FM RX Volume";
72
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);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070099
Ravi Kumar Alamandacb065742013-11-20 18:31:58 -0800100 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700101 return ret;
102}
103
104static 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 */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700130 disable_audio_route(adev, uc_info);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700131
132 /* 3. Disable the rx and tx devices */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700133 disable_snd_device(adev, uc_info->out_snd_device);
134 disable_snd_device(adev, uc_info->in_snd_device);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700135
136 list_remove(&uc_info->list);
137 free(uc_info);
138
139 ALOGD("%s: exit: status(%d)", __func__, ret);
140 return ret;
141}
142
143static 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 uc_info->id = USECASE_AUDIO_PLAYBACK_FM;
153 uc_info->type = PCM_PLAYBACK;
154 uc_info->stream.out = adev->primary_output;
155 uc_info->devices = adev->primary_output->devices;
156 uc_info->in_snd_device = SND_DEVICE_NONE;
157 uc_info->out_snd_device = SND_DEVICE_NONE;
158
159 list_add_tail(&adev->usecase_list, &uc_info->list);
160
161 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
162
163 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
164 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
165
166 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
167 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
168 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
169 ret = -EIO;
170 goto exit;
171 }
172
173 ALOGV("%s: FM PCM devices (rx: %d tx: %d) for the usecase(%d)",
174 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
175
176 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800177 __func__, adev->snd_card, pcm_dev_rx_id);
178 fmmod.fm_pcm_rx = pcm_open(adev->snd_card,
179 pcm_dev_rx_id,
180 PCM_OUT, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700181 if (fmmod.fm_pcm_rx && !pcm_is_ready(fmmod.fm_pcm_rx)) {
182 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_rx));
183 ret = -EIO;
184 goto exit;
185 }
186
187 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800188 __func__, adev->snd_card, pcm_dev_tx_id);
189 fmmod.fm_pcm_tx = pcm_open(adev->snd_card,
190 pcm_dev_tx_id,
191 PCM_IN, &pcm_config_fm);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700192 if (fmmod.fm_pcm_tx && !pcm_is_ready(fmmod.fm_pcm_tx)) {
193 ALOGE("%s: %s", __func__, pcm_get_error(fmmod.fm_pcm_tx));
194 ret = -EIO;
195 goto exit;
196 }
197 pcm_start(fmmod.fm_pcm_rx);
198 pcm_start(fmmod.fm_pcm_tx);
199
200 fmmod.is_fm_running = true;
201 fm_set_volume(adev, fmmod.fm_volume);
202
203 ALOGD("%s: exit: status(%d)", __func__, ret);
204 return 0;
205
206exit:
207 fm_stop(adev);
208 ALOGE("%s: Problem in FM start: status(%d)", __func__, ret);
209 return ret;
210}
211
212void audio_extn_fm_set_parameters(struct audio_device *adev,
213 struct str_parms *parms)
214{
215 int ret, val;
216 char value[32]={0};
217 float vol =0.0;
218
219 ALOGV("%s: enter", __func__);
Dhanalakshmi Siddani6f461b92014-05-30 11:20:27 +0530220 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
221 if (ret >= 0) {
222 char *snd_card_status = value+2;
223 if (strstr(snd_card_status, "OFFLINE")) {
224 fmmod.scard_state = SND_CARD_STATE_OFFLINE;
225 }
226 else if (strstr(snd_card_status, "ONLINE")) {
227 fmmod.scard_state = SND_CARD_STATE_ONLINE;
228 }
229 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700230 if(fmmod.is_fm_running) {
Dhanalakshmi Siddani6f461b92014-05-30 11:20:27 +0530231 if (fmmod.scard_state == SND_CARD_STATE_OFFLINE) {
232 ALOGD("sound card is OFFLINE, stop FM");
233 fm_stop(adev);
234 fmmod.restart_fm = 1;
235 }
236
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700237 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
238 value, sizeof(value));
239 if (ret >= 0) {
240 val = atoi(value);
241 if(val > 0)
242 select_devices(adev, USECASE_AUDIO_PLAYBACK_FM);
243 }
244 }
Dhanalakshmi Siddani6f461b92014-05-30 11:20:27 +0530245 if (fmmod.restart_fm && (fmmod.scard_state == SND_CARD_STATE_ONLINE)) {
246 ALOGD("sound card is ONLINE, restart FM");
247 fmmod.restart_fm = 0;
248 fm_start(adev);
249 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700250
251 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HANDLE_FM,
252 value, sizeof(value));
253 if (ret >= 0) {
254 val = atoi(value);
255 ALOGD("%s: FM usecase", __func__);
256 if (val != 0) {
257 if(val & AUDIO_DEVICE_OUT_FM
Ravi Kumar Alamandac05cbdb2014-04-22 15:19:13 -0700258 && fmmod.is_fm_running == false) {
259 adev->primary_output->devices = val & ~AUDIO_DEVICE_OUT_FM;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700260 fm_start(adev);
Ravi Kumar Alamandac05cbdb2014-04-22 15:19:13 -0700261 } else if (!(val & AUDIO_DEVICE_OUT_FM)
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700262 && fmmod.is_fm_running == true)
263 fm_stop(adev);
264 }
265 }
266
267 memset(value, 0, sizeof(value));
268 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_FM_VOLUME,
269 value, sizeof(value));
270 if (ret >= 0) {
271 if (sscanf(value, "%f", &vol) != 1){
272 ALOGE("%s: error in retrieving fm volume", __func__);
273 ret = -EIO;
274 goto exit;
275 }
276 ALOGD("%s: set_fm_volume usecase", __func__);
277 fm_set_volume(adev, vol);
278 }
279
280exit:
281 ALOGV("%s: exit", __func__);
282}
Ramjee Singhfd4323f2015-11-18 13:26:59 +0530283#endif /* FM_POWER_OPT end */