blob: c52508ead2167d23c7001a03ff7b795de5dc143b [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>
35
Divya Narayanan Poojary913a2502016-10-18 15:34:10 +053036/* Device state*/
37#define AUDIO_PARAMETER_KEY_DEV_SHUTDOWN "dev_shutdown"
38
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053039static s_audio_subsys audio_ss;
40
41int audio_extn_pm_vote(void)
42{
Surendar karka0a97d5e2016-12-08 16:31:03 +053043 int ret;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053044 enum pm_event subsys_state;
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053045 bool pm_reg = false;
46 bool pm_supp = false;
47
48 platform_get_subsys_image_name((char *)&audio_ss.img_name);
49 ALOGD("%s: register with peripheral manager for %s",__func__, audio_ss.img_name);
50 ret = pm_client_register(audio_extn_pm_event_notifier,
51 &audio_ss,
52 audio_ss.img_name,
53 PM_CLIENT_NAME,
54 &subsys_state,
55 &audio_ss.pm_handle);
56 if (ret == PM_RET_SUCCESS) {
57 pm_reg = true;
58 pm_supp = true;
59 ALOGV("%s: registered with peripheral manager for %s",
60 __func__, audio_ss.img_name);
61 } else if (ret == PM_RET_UNSUPPORTED) {
62 pm_reg = true;
63 pm_supp = false;
64 ALOGV("%s: peripheral mgr unsupported for %s",
65 __func__, audio_ss.img_name);
66 return ret;
67 } else {
68 return ret;
69 }
70 if (pm_supp == true &&
71 pm_reg == true) {
72 ALOGD("%s: Voting for subsystem power up", __func__);
73 pm_client_connect(audio_ss.pm_handle);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +053074 }
75 return 0;
76}
77
78void audio_extn_pm_unvote(void)
79{
80 ALOGD("%s", __func__);
81 if (audio_ss.pm_handle) {
82 pm_client_disconnect(audio_ss.pm_handle);
83 pm_client_unregister(audio_ss.pm_handle);
84 }
85}
86
87void audio_extn_pm_set_parameters(struct str_parms *parms)
88{
89 int ret;
90 char value[32];
91
92 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_DEV_SHUTDOWN, value, sizeof(value));
93 if (ret >= 0) {
94 if (strstr(value, "true")) {
95 ALOGD("Device shutdown notification received, unregister with PM");
96 audio_extn_pm_unvote();
97 }
98 }
99}
100
101void audio_extn_pm_event_notifier(void *client_data, enum pm_event event)
102{
Surendar karka0a97d5e2016-12-08 16:31:03 +0530103
104 int err, intfd;
105 char halPropVal[PROPERTY_VALUE_MAX];
106 bool prop_unload_image = false;
107
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530108 pm_client_event_acknowledge(audio_ss.pm_handle, event);
109
110 /* Closing and re-opening of session is done based on snd card status given
111 * by AudioDaemon during SS offline/online (legacy code). Just return for now.
112 */
113 switch (event) {
114 case EVENT_PERIPH_GOING_OFFLINE:
115 ALOGV("%s: %s is going offline", __func__, audio_ss.img_name);
116 break;
117
118 case EVENT_PERIPH_IS_OFFLINE:
119 ALOGV("%s: %s is offline", __func__, audio_ss.img_name);
120 break;
121
122 case EVENT_PERIPH_GOING_ONLINE:
123 ALOGV("%s: %s is going online", __func__, audio_ss.img_name);
124 break;
125
126 case EVENT_PERIPH_IS_ONLINE:
127 ALOGV("%s: %s is online", __func__, audio_ss.img_name);
Surendar karka0a97d5e2016-12-08 16:31:03 +0530128
129 if (property_get("sys.audio.init", halPropVal, NULL)) {
130 prop_unload_image = !(strncmp("false", halPropVal, sizeof("false")));
131 }
132 /*
133 * adsp-loader loads modem/adsp image at boot up to play boot tone,
134 * before peripheral manager service is up. Once PM is up, vote to PM
135 * and unload the image to give control to PM to load/unload image
136 */
137 if (prop_unload_image) {
138 intfd = open(BOOT_IMG_SYSFS_PATH, O_WRONLY);
139 if (intfd == -1) {
140 ALOGE("failed to open fd in write mode, %d", errno);
141 } else {
142 ALOGD("%s: write to sysfs to unload image", __func__);
143 err = write(intfd, UNLOAD_IMAGE, 1);
144 close(intfd);
145 property_set("sys.audio.init", "true");
146 }
147 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +0530148 break;
149
150 default:
151 ALOGV("%s: invalid event received from PM", __func__);
152 break;
153 }
154}