blob: c6ef2425c4f7f7c8c7774d186f36a7a9792c2369 [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
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053037#ifdef DYNAMIC_LOG_ENABLED
38#include <log_xml_parser.h>
39#define LOG_MASK HAL_MOD_FILE_PM
40#include <log_utils.h>
41#endif
42
Divya Narayanan Poojary913a2502016-10-18 15:34:10 +053043/* Device state*/
44#define AUDIO_PARAMETER_KEY_DEV_SHUTDOWN "dev_shutdown"
45
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053046static s_audio_subsys audio_ss;
47
48int audio_extn_pm_vote(void)
49{
Surendar karka0a97d5e2016-12-08 16:31:03 +053050 int ret;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053051 enum pm_event subsys_state;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053052 bool pm_reg = false;
53 bool pm_supp = false;
54
55 platform_get_subsys_image_name((char *)&audio_ss.img_name);
56 ALOGD("%s: register with peripheral manager for %s",__func__, audio_ss.img_name);
57 ret = pm_client_register(audio_extn_pm_event_notifier,
58 &audio_ss,
59 audio_ss.img_name,
60 PM_CLIENT_NAME,
61 &subsys_state,
62 &audio_ss.pm_handle);
63 if (ret == PM_RET_SUCCESS) {
64 pm_reg = true;
65 pm_supp = true;
66 ALOGV("%s: registered with peripheral manager for %s",
67 __func__, audio_ss.img_name);
68 } else if (ret == PM_RET_UNSUPPORTED) {
69 pm_reg = true;
70 pm_supp = false;
71 ALOGV("%s: peripheral mgr unsupported for %s",
72 __func__, audio_ss.img_name);
73 return ret;
74 } else {
75 return ret;
76 }
77 if (pm_supp == true &&
78 pm_reg == true) {
79 ALOGD("%s: Voting for subsystem power up", __func__);
80 pm_client_connect(audio_ss.pm_handle);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053081 }
82 return 0;
83}
84
85void audio_extn_pm_unvote(void)
86{
87 ALOGD("%s", __func__);
88 if (audio_ss.pm_handle) {
89 pm_client_disconnect(audio_ss.pm_handle);
90 pm_client_unregister(audio_ss.pm_handle);
91 }
92}
93
94void audio_extn_pm_set_parameters(struct str_parms *parms)
95{
96 int ret;
97 char value[32];
98
99 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_DEV_SHUTDOWN, value, sizeof(value));
100 if (ret >= 0) {
101 if (strstr(value, "true")) {
102 ALOGD("Device shutdown notification received, unregister with PM");
103 audio_extn_pm_unvote();
104 }
105 }
106}
107
Divya Narayanan Poojary5ed68ff2017-04-20 15:43:48 +0530108void audio_extn_pm_event_notifier(void *client_data __unused, enum pm_event event)
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530109{
Surendar karka0a97d5e2016-12-08 16:31:03 +0530110
111 int err, intfd;
112 char halPropVal[PROPERTY_VALUE_MAX];
113 bool prop_unload_image = false;
114
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530115 pm_client_event_acknowledge(audio_ss.pm_handle, event);
116
117 /* Closing and re-opening of session is done based on snd card status given
118 * by AudioDaemon during SS offline/online (legacy code). Just return for now.
119 */
120 switch (event) {
121 case EVENT_PERIPH_GOING_OFFLINE:
122 ALOGV("%s: %s is going offline", __func__, audio_ss.img_name);
123 break;
124
125 case EVENT_PERIPH_IS_OFFLINE:
126 ALOGV("%s: %s is offline", __func__, audio_ss.img_name);
127 break;
128
129 case EVENT_PERIPH_GOING_ONLINE:
130 ALOGV("%s: %s is going online", __func__, audio_ss.img_name);
131 break;
132
133 case EVENT_PERIPH_IS_ONLINE:
134 ALOGV("%s: %s is online", __func__, audio_ss.img_name);
Surendar karka0a97d5e2016-12-08 16:31:03 +0530135
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700136 if (property_get("vendor.audio.sys.init", halPropVal, NULL)) {
Surendar karka0a97d5e2016-12-08 16:31:03 +0530137 prop_unload_image = !(strncmp("false", halPropVal, sizeof("false")));
138 }
139 /*
140 * adsp-loader loads modem/adsp image at boot up to play boot tone,
141 * before peripheral manager service is up. Once PM is up, vote to PM
142 * and unload the image to give control to PM to load/unload image
143 */
144 if (prop_unload_image) {
145 intfd = open(BOOT_IMG_SYSFS_PATH, O_WRONLY);
146 if (intfd == -1) {
147 ALOGE("failed to open fd in write mode, %d", errno);
148 } else {
149 ALOGD("%s: write to sysfs to unload image", __func__);
150 err = write(intfd, UNLOAD_IMAGE, 1);
151 close(intfd);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700152 property_set("vendor.audio.sys.init", "true");
Surendar karka0a97d5e2016-12-08 16:31:03 +0530153 }
154 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530155 break;
156
157 default:
158 ALOGV("%s: invalid event received from PM", __func__);
159 break;
160 }
161}