blob: 9b961becf8c530b5151480ed39ff9b4b5075545d [file] [log] [blame]
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +01001/*
2 * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_audience"
18#define LOG_NDEBUG 0
19
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23
24#include <cutils/log.h>
25#include <audience-routes.h>
26
27#include "audience.h"
28
29/*
30 * Writes an Integer to a file.
31 *
32 * @param path The absolute path string.
33 * @param value The Integer value to be written.
34 * @return 0 on success, -1 or errno on error.
35 */
36static int write_int(char const *path, const int value)
37{
38 int fd;
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +010039
40 ALOGV("write_int: path %s, value %d", path, value);
Christopher N. Hesse658f4472017-04-02 13:24:47 +020041 fd = open(path, O_WRONLY);
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +010042
43 if (fd >= 0) {
44 char buffer[20];
45 int bytes = sprintf(buffer, "%d\n", value);
46 int amt = write(fd, buffer, bytes);
47 close(fd);
48 return amt == -1 ? -errno : 0;
49 } else {
Christopher N. Hesse20368142017-04-02 13:24:24 +020050 ALOGE("write_int failed to open %s\n", path);
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +010051 return -errno;
52 }
53}
54
55/*
56 * Writes the route value to sysfs.
57 *
58 * @param value The long Integer value to be written.
59 * @return 0 on success, -1 or errno on error.
60 */
61static int es_route_value_set(int value)
62{
63 return write_int(SYSFS_PATH_PRESET, value);
64}
65
66/*
67 * Writes the veq control to sysfs.
68 *
69 * @param value The Integer value to be written.
70 * @return 0 on success, -1 or errno on error.
71 */
72static int es_veq_control_set(int value)
73{
74 return write_int(SYSFS_PATH_VEQ, value);
75}
76
77/*
78 * Writes the extra volume to sysfs.
79 *
80 * @param value The Integer value to be written.
81 * @return 0 on success, -1 or errno on error.
82 */
83static int es_extra_volume_set(int value)
84{
85 return write_int(SYSFS_PATH_EXTRAVOLUME, value);
86}
87
88/*
89 * Convertes an out_device from the session to an earSmart compatible route.
90 *
91 * @param out_device The output device to be converted.
92 * @return Audience earSmart route, coded as long Integer.
93 */
94static long es_device_to_route(struct voice_session *session)
95{
96 long ret;
97 long nb_route;
98 long wb_route;
99
100 switch(session->out_device) {
101 case AUDIO_DEVICE_OUT_EARPIECE:
102 nb_route = Call_CT_NB;
103 wb_route = Call_CT_WB;
104 break;
105 case AUDIO_DEVICE_OUT_SPEAKER:
106 nb_route = Call_FT_NB;
107 wb_route = Call_FT_WB;
108 break;
109 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
110 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
111 nb_route = Call_HS_NB;
112 wb_route = Call_HS_WB;
113 break;
114 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
115 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
116 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
117 nb_route = Call_BT_NB;
118 wb_route = Call_BT_WB;
119 break;
120 default:
121 /* if output device isn't supported, use earpiece by default */
Christopher N. Hesse70524602017-04-02 13:19:36 +0200122 ALOGE("%s: unknown output device: %d, defaulting to earpiece", __func__,
123 session->out_device);
Christopher N. Hesse0fdef0c2017-02-25 01:37:56 +0100124 nb_route = Call_CT_NB;
125 wb_route = Call_CT_WB;
126 break;
127 }
128
129 /* TODO: Handle wb_amr=2 */
130 if (session->wb_amr_type >= 1) {
131 ret = wb_route;
132 } else {
133 ret = nb_route;
134 }
135
136 ALOGV("%s: converting out_device=%d to %s route: %ld", __func__, session->out_device,
137 ret == wb_route ? "Wide Band" : "Narrow Band", ret);
138 return ret;
139}
140
141/*
142 * Configures and enables the Audience earSmart IC.
143 *
144 * @param session Reference to the active voice call session.
145 * @return @return 0 on success, -1 or errno on error.
146 */
147int es_start_voice_session(struct voice_session *session)
148{
149 int ret;
150 long es_route = es_device_to_route(session);
151
152 /* TODO: Calculate these */
153 int extra_volume = 0;
154 int veq_control = 4;
155
156 /*
157 * The control flow for audience earSmart chip is as follows:
158 *
159 * route_value >> power_control(internal) >> extra_volume >> veq_control
160 */
161 ret = es_route_value_set(es_route);
162 if (ret != 0) {
163 ALOGE("%s: es_route_value_set(%ld) failed with code: %d", __func__, es_route, ret);
164 goto exit;
165 }
166 ret = es_extra_volume_set(extra_volume);
167 if (ret != 0) {
168 ALOGE("%s: es_extra_volume_set(%d) failed with code: %d", __func__, extra_volume, ret);
169 goto exit;
170 }
171 ret = es_veq_control_set(veq_control);
172 if (ret != 0) {
173 ALOGE("%s: es_veq_control_set(%d) failed with code: %d", __func__, veq_control, ret);
174 goto exit;
175 }
176
177exit:
178 return ret;
179}
180
181/*
182 * Disables the Audience earSmart IC.
183 */
184void es_stop_voice_session()
185{
186 /* This will cancel any pending workers, stop the stream and send the IC to sleep */
187 es_route_value_set(AUDIENCE_SLEEP);
188}