blob: 69e19cb4a8c0c3e92fd641a5ffb61c83efc596d9 [file] [log] [blame]
Surendar karka0a97d5e2016-12-08 16:31:03 +05301/* Copyright (c) 2014, 2017 The Linux Foundation. All rights reserved.
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#define LOG_TAG "audio_hw_pm"
31/*#define LOG_NDEBUG 0*/
32
33#include "pm.h"
34#include <cutils/log.h>
Divya Narayanan Poojary5ed68ff2017-04-20 15:43:48 +053035#include <cutils/str_parms.h>
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053036
Divya Narayanan Poojary913a2502016-10-18 15:34:10 +053037/* Device state*/
38#define AUDIO_PARAMETER_KEY_DEV_SHUTDOWN "dev_shutdown"
39
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053040static s_audio_subsys audio_ss;
41
42int audio_extn_pm_vote(void)
43{
Surendar karka0a97d5e2016-12-08 16:31:03 +053044 int ret;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053045 enum pm_event subsys_state;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053046 bool pm_reg = false;
47 bool pm_supp = false;
48
49 platform_get_subsys_image_name((char *)&audio_ss.img_name);
50 ALOGD("%s: register with peripheral manager for %s",__func__, audio_ss.img_name);
51 ret = pm_client_register(audio_extn_pm_event_notifier,
52 &audio_ss,
53 audio_ss.img_name,
54 PM_CLIENT_NAME,
55 &subsys_state,
56 &audio_ss.pm_handle);
57 if (ret == PM_RET_SUCCESS) {
58 pm_reg = true;
59 pm_supp = true;
60 ALOGV("%s: registered with peripheral manager for %s",
61 __func__, audio_ss.img_name);
62 } else if (ret == PM_RET_UNSUPPORTED) {
63 pm_reg = true;
64 pm_supp = false;
65 ALOGV("%s: peripheral mgr unsupported for %s",
66 __func__, audio_ss.img_name);
67 return ret;
68 } else {
69 return ret;
70 }
71 if (pm_supp == true &&
72 pm_reg == true) {
73 ALOGD("%s: Voting for subsystem power up", __func__);
74 pm_client_connect(audio_ss.pm_handle);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053075 }
76 return 0;
77}
78
79void audio_extn_pm_unvote(void)
80{
81 ALOGD("%s", __func__);
82 if (audio_ss.pm_handle) {
83 pm_client_disconnect(audio_ss.pm_handle);
84 pm_client_unregister(audio_ss.pm_handle);
85 }
86}
87
88void audio_extn_pm_set_parameters(struct str_parms *parms)
89{
90 int ret;
91 char value[32];
92
93 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_DEV_SHUTDOWN, value, sizeof(value));
94 if (ret >= 0) {
95 if (strstr(value, "true")) {
96 ALOGD("Device shutdown notification received, unregister with PM");
97 audio_extn_pm_unvote();
98 }
99 }
100}
101
Divya Narayanan Poojary5ed68ff2017-04-20 15:43:48 +0530102void audio_extn_pm_event_notifier(void *client_data __unused, enum pm_event event)
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530103{
Surendar karka0a97d5e2016-12-08 16:31:03 +0530104
105 int err, intfd;
106 char halPropVal[PROPERTY_VALUE_MAX];
107 bool prop_unload_image = false;
108
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530109 pm_client_event_acknowledge(audio_ss.pm_handle, event);
110
111 /* Closing and re-opening of session is done based on snd card status given
112 * by AudioDaemon during SS offline/online (legacy code). Just return for now.
113 */
114 switch (event) {
115 case EVENT_PERIPH_GOING_OFFLINE:
116 ALOGV("%s: %s is going offline", __func__, audio_ss.img_name);
117 break;
118
119 case EVENT_PERIPH_IS_OFFLINE:
120 ALOGV("%s: %s is offline", __func__, audio_ss.img_name);
121 break;
122
123 case EVENT_PERIPH_GOING_ONLINE:
124 ALOGV("%s: %s is going online", __func__, audio_ss.img_name);
125 break;
126
127 case EVENT_PERIPH_IS_ONLINE:
128 ALOGV("%s: %s is online", __func__, audio_ss.img_name);
Surendar karka0a97d5e2016-12-08 16:31:03 +0530129
130 if (property_get("sys.audio.init", halPropVal, NULL)) {
131 prop_unload_image = !(strncmp("false", halPropVal, sizeof("false")));
132 }
133 /*
134 * adsp-loader loads modem/adsp image at boot up to play boot tone,
135 * before peripheral manager service is up. Once PM is up, vote to PM
136 * and unload the image to give control to PM to load/unload image
137 */
138 if (prop_unload_image) {
139 intfd = open(BOOT_IMG_SYSFS_PATH, O_WRONLY);
140 if (intfd == -1) {
141 ALOGE("failed to open fd in write mode, %d", errno);
142 } else {
143 ALOGD("%s: write to sysfs to unload image", __func__);
144 err = write(intfd, UNLOAD_IMAGE, 1);
145 close(intfd);
146 property_set("sys.audio.init", "true");
147 }
148 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530149 break;
150
151 default:
152 ALOGV("%s: invalid event received from PM", __func__);
153 break;
154 }
155}