blob: 3c44c81bef08466ee7c9d8dc9bdef68e952bac3e [file] [log] [blame]
Xo Wang3f87de82016-09-22 11:17:01 -07001/**
2 * Copyright © 2016 Google Inc.
3 * Copyright © 2016 IBM Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "power_gpio.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
24
25gboolean read_power_gpio(GDBusConnection *connection, PowerGpio *power_gpio)
26{
27 GDBusProxy *proxy;
28 GError *error;
29 GVariant *value;
30 gchar *power_good_in_name;
Xo Wangc1ce68b2016-09-22 16:34:37 -070031 gchar *latch_out_name;
Xo Wang3f87de82016-09-22 11:17:01 -070032 GVariantIter *power_up_outs_iter;
33 GVariantIter *reset_outs_iter;
Yi Li0475f652016-10-25 13:19:59 +080034 GVariantIter *pci_reset_outs_iter;
Xo Wang3f87de82016-09-22 11:17:01 -070035 gchar *power_up_out_name;
36 gchar *reset_out_name;
Yi Li0475f652016-10-25 13:19:59 +080037 gchar *pci_reset_out_name;
Xo Wang3f87de82016-09-22 11:17:01 -070038 gboolean power_up_polarity;
39 gboolean reset_out_polarity;
Yi Li0475f652016-10-25 13:19:59 +080040 gboolean pci_reset_out_polarity;
41 gboolean pci_reset_out_hold;
Xo Wang3f87de82016-09-22 11:17:01 -070042 int i;
43
44 proxy = g_dbus_proxy_new_sync(connection,
45 G_DBUS_PROXY_FLAGS_NONE,
46 NULL, /* GDBusInterfaceInfo */
47 "org.openbmc.managers.System", /* name */
48 "/org/openbmc/managers/System", /* object path */
49 "org.openbmc.managers.System", /* interface */
50 NULL, /* GCancellable */
51 &error);
52 if(error != NULL) {
53 fprintf(stderr, "Unable to create proxy: %s\n", error->message);
54 g_error_free(error);
55 return FALSE;
56 }
57
58 value = g_dbus_proxy_call_sync(proxy,
59 "getPowerConfiguration",
60 NULL,
61 G_DBUS_CALL_FLAGS_NONE,
62 -1,
63 NULL,
64 &error);
65 if(error != NULL) {
66 fprintf(stderr, "Power GPIO: call to getPowerConfiguration failed: %s\n",
67 error->message);
68 g_error_free(error);
69 return FALSE;
70 }
71
72 g_assert(value != NULL);
73 memset(power_gpio, 0, sizeof(*power_gpio));
Yi Li0475f652016-10-25 13:19:59 +080074 g_variant_get(value, "(&s&sa(sb)a(sb)a(sbb))", &power_good_in_name, &latch_out_name,
75 &power_up_outs_iter, &reset_outs_iter, &pci_reset_outs_iter);
Xo Wang3f87de82016-09-22 11:17:01 -070076
Xo Wangc1ce68b2016-09-22 16:34:37 -070077 g_print("Power GPIO latch output %s\n", latch_out_name);
78 if(*latch_out_name != '\0') { /* latch is optional */
79 power_gpio->latch_out.name = strdup(latch_out_name);
80 }
Xo Wang3f87de82016-09-22 11:17:01 -070081 g_print("Power GPIO power good input %s\n", power_good_in_name);
82 power_gpio->power_good_in.name = g_strdup(power_good_in_name);
83 power_gpio->num_power_up_outs = g_variant_iter_n_children(
84 power_up_outs_iter);
85 g_print("Power GPIO %d power_up outputs\n",
86 power_gpio->num_power_up_outs);
87 power_gpio->power_up_outs = g_malloc0_n(power_gpio->num_power_up_outs,
88 sizeof(GPIO));
89 power_gpio->power_up_pols = g_malloc0_n(power_gpio->num_power_up_outs,
90 sizeof(gboolean));
91 for(i = 0; g_variant_iter_next(power_up_outs_iter, "(&sb)",
92 &power_up_out_name, &power_up_polarity);
93 i++) {
94 g_print("Power GPIO power_up[%d] = %s active %s\n", i,
95 power_up_out_name, power_up_polarity ? "HIGH" : "LOW");
96 power_gpio->power_up_outs[i].name = g_strdup(power_up_out_name);
97 power_gpio->power_up_pols[i] = power_up_polarity;
98 }
99 power_gpio->num_reset_outs = g_variant_iter_n_children(reset_outs_iter);
100 g_print("Power GPIO %d reset outputs\n", power_gpio->num_reset_outs);
101 power_gpio->reset_outs = g_malloc0_n(power_gpio->num_reset_outs, sizeof(GPIO));
102 power_gpio->reset_pols = g_malloc0_n(power_gpio->num_reset_outs,
103 sizeof(gboolean));
104 for(i = 0; g_variant_iter_next(reset_outs_iter, "(&sb)", &reset_out_name,
105 &reset_out_polarity); i++) {
106 g_print("Power GPIO reset[%d] = %s active %s\n", i, reset_out_name,
107 reset_out_polarity ? "HIGH" : "LOW");
108 power_gpio->reset_outs[i].name = g_strdup(reset_out_name);
109 power_gpio->reset_pols[i] = reset_out_polarity;
110 }
111
Yi Li0475f652016-10-25 13:19:59 +0800112 power_gpio->num_pci_reset_outs = g_variant_iter_n_children(pci_reset_outs_iter);
113 g_print("Power GPIO %d pci reset outputs\n", power_gpio->num_pci_reset_outs);
114 power_gpio->pci_reset_outs = g_malloc0_n(power_gpio->num_pci_reset_outs,
115 sizeof(GPIO));
116 power_gpio->pci_reset_pols = g_malloc0_n(power_gpio->num_pci_reset_outs,
117 sizeof(gboolean));
118 power_gpio->pci_reset_holds = g_malloc0_n(power_gpio->num_pci_reset_outs,
119 sizeof(gboolean));
120 for(i = 0; g_variant_iter_next(pci_reset_outs_iter, "(&sbb)", &pci_reset_out_name,
121 &pci_reset_out_polarity, &pci_reset_out_hold); i++) {
122 g_print("Power GPIO pci reset[%d] = %s active %s, hold - %s\n", i,
123 pci_reset_out_name,
124 pci_reset_out_polarity ? "HIGH" : "LOW",
125 pci_reset_out_hold ? "Yes" : "No");
126 power_gpio->pci_reset_outs[i].name = g_strdup(pci_reset_out_name);
127 power_gpio->pci_reset_pols[i] = pci_reset_out_polarity;
128 power_gpio->pci_reset_holds[i] = pci_reset_out_hold;
129 }
130
Xo Wang3f87de82016-09-22 11:17:01 -0700131 g_variant_iter_free(power_up_outs_iter);
132 g_variant_iter_free(reset_outs_iter);
Yi Li0475f652016-10-25 13:19:59 +0800133 g_variant_iter_free(pci_reset_outs_iter);
Xo Wang3f87de82016-09-22 11:17:01 -0700134 g_variant_unref(value);
135
136 return TRUE;
137}
138
139void free_power_gpio(PowerGpio *power_gpio) {
140 int i;
Xo Wangc1ce68b2016-09-22 16:34:37 -0700141 g_free(power_gpio->latch_out.name);
Xo Wang3f87de82016-09-22 11:17:01 -0700142 g_free(power_gpio->power_good_in.name);
143 for(i = 0; i < power_gpio->num_power_up_outs; i++) {
144 g_free(power_gpio->power_up_outs[i].name);
145 }
146 g_free(power_gpio->power_up_outs);
147 g_free(power_gpio->power_up_pols);
148 for(i = 0; i < power_gpio->num_reset_outs; i++) {
149 g_free(power_gpio->reset_outs[i].name);
150 }
151 g_free(power_gpio->reset_outs);
152 g_free(power_gpio->reset_pols);
Yi Li0475f652016-10-25 13:19:59 +0800153 for(i = 0; i < power_gpio->num_pci_reset_outs; i++) {
154 g_free(power_gpio->pci_reset_outs[i].name);
155 }
156 g_free(power_gpio->pci_reset_outs);
157 g_free(power_gpio->pci_reset_pols);
158 g_free(power_gpio->pci_reset_holds);
Xo Wang3f87de82016-09-22 11:17:01 -0700159}