6 Commits
0.0.1 ... 0.0.3

Author SHA1 Message Date
Arnaud Ferraris
87c7af7056 manager: actively poll GPIO when quitting
For some reason, during system the main loop exits before we call 
g_main_loop_quit(), so don't rely on it and use a simple polling loop to 
check for modem poweroff.
2020-12-10 21:33:18 +01:00
Arnaud Ferraris
fa21de07f4 src: be more careful when freeing data 2020-12-10 21:31:22 +01:00
Arnaud Ferraris
a8a1c8d161 manager: improve BH/CE detection
`Developer edition` phones have a different compatible string but should 
be considered identical to BraveHeart edition here. Therefore BH is the 
default phone, unless the compatible is that of a CE phone.
2020-12-10 17:07:36 +01:00
Arnaud Ferraris
47b2f71b6f improve poweroff sequence
When powering off the modem, we must assert the RESET line only when the 
modem has shut down (otherwise it'll cause a hard reset and won't allow 
the modem to shut down properly).

This commit also polls the STATUS pin during poweroff and quits 
immediately once this pin goes high.
2020-12-10 17:05:44 +01:00
Arnaud Ferraris
1170a2c7f7 gpio: configure STATUS input gpio
RI won't be accessible due to it being used by the kernel as an 
interrupt source, so we can check the STATUS pin (CE only) which will 
allow us to know precisely when the modem is shut down.
2020-12-10 17:03:11 +01:00
Arnaud Ferraris
e3387e85ca manager: destroy at/gpio modules after the main loop exits 2020-12-10 16:56:18 +01:00
7 changed files with 129 additions and 51 deletions

View File

@@ -206,7 +206,8 @@ int at_init(struct EG25Manager *manager)
void at_destroy(struct EG25Manager *manager)
{
g_source_remove(manager->at_source);
close(manager->at_fd);
if (manager->at_fd > 0)
close(manager->at_fd);
}
void at_sequence_configure(struct EG25Manager *manager)

View File

@@ -11,6 +11,8 @@
#define MAX_GPIOCHIP_LINES 352
#define GPIO_IDX_INVAL 0xffff
enum {
GPIO_OUT_DTR = 0,
GPIO_OUT_PWRKEY,
@@ -20,7 +22,7 @@ enum {
GPIO_OUT_COUNT
};
static unsigned gpio_idx_bh[] = {
static unsigned gpio_out_idx_bh[] = {
358,
35,
68,
@@ -28,7 +30,7 @@ static unsigned gpio_idx_bh[] = {
232
};
static unsigned gpio_idx_ce[] = {
static unsigned gpio_out_idx_ce[] = {
34,
35,
68,
@@ -36,6 +38,19 @@ static unsigned gpio_idx_ce[] = {
232
};
enum {
GPIO_IN_STATUS = 0,
GPIO_IN_COUNT
};
static unsigned gpio_in_idx_bh[] = {
GPIO_IDX_INVAL,
};
static unsigned gpio_in_idx_ce[] = {
233,
};
int gpio_sequence_poweron(struct EG25Manager *manager)
{
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_PWRKEY], 1);
@@ -49,11 +64,8 @@ int gpio_sequence_poweron(struct EG25Manager *manager)
int gpio_sequence_shutdown(struct EG25Manager *manager)
{
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_RESET], 1);
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_DISABLE], 1);
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_PWRKEY], 1);
sleep(1);
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_PWRKEY], 0);
gpio_sequence_poweron(manager);
g_message("Executed power-off sequence");
@@ -83,7 +95,8 @@ int gpio_sequence_resume(struct EG25Manager *manager)
int gpio_init(struct EG25Manager *manager)
{
int i, ret;
unsigned *gpio_idx;
unsigned *gpio_in_idx;
unsigned *gpio_out_idx;
manager->gpiochip[0] = gpiod_chip_open_by_label(GPIO_CHIP1_LABEL);
if (!manager->gpiochip[0]) {
@@ -91,53 +104,103 @@ int gpio_init(struct EG25Manager *manager)
return 1;
}
manager->gpiochip[1] = gpiod_chip_open_by_label(GPIO_CHIP2_LABEL);
if (!manager->gpiochip[1]) {
g_critical("Unable to open GPIO chip " GPIO_CHIP2_LABEL);
return 1;
}
if (manager->braveheart) {
// BH have DTR on the 2nd gpiochip
manager->gpiochip[1] = gpiod_chip_open_by_label(GPIO_CHIP2_LABEL);
if (!manager->gpiochip[1]) {
g_critical("Unable to open GPIO chip " GPIO_CHIP2_LABEL);
return 1;
}
gpio_idx = gpio_idx_bh;
gpio_in_idx = gpio_in_idx_bh;
gpio_out_idx = gpio_out_idx_bh;
} else {
gpio_idx = gpio_idx_ce;
gpio_in_idx = gpio_in_idx_ce;
gpio_out_idx = gpio_out_idx_ce;
}
for (i = 0; i < GPIO_OUT_COUNT; i++) {
unsigned int offset, chipidx;
if (gpio_idx[i] < MAX_GPIOCHIP_LINES) {
offset = gpio_idx[i];
if (gpio_out_idx[i] < MAX_GPIOCHIP_LINES) {
offset = gpio_out_idx[i];
chipidx = 0;
} else {
offset = gpio_idx[i] - MAX_GPIOCHIP_LINES;
offset = gpio_out_idx[i] - MAX_GPIOCHIP_LINES;
chipidx = 1;
}
manager->gpio_out[i] = gpiod_chip_get_line(manager->gpiochip[chipidx], offset);
if (!manager->gpio_out[i]) {
g_critical("Unable to get line %d", i);
g_critical("Unable to get output GPIO %d", i);
return 1;
}
ret = gpiod_line_request_output(manager->gpio_out[i], "eg25manager", 0);
if (ret < 0) {
g_critical("Unable to request line %d", i);
g_critical("Unable to request output GPIO %d", i);
return 1;
}
}
for (i = 0; i < GPIO_IN_COUNT; i++) {
unsigned int offset, chipidx;
if (gpio_in_idx[i] == GPIO_IDX_INVAL)
continue;
if (gpio_in_idx[i] < MAX_GPIOCHIP_LINES) {
offset = gpio_in_idx[i];
chipidx = 0;
} else {
offset = gpio_in_idx[i] - MAX_GPIOCHIP_LINES;
chipidx = 1;
}
manager->gpio_in[i] = gpiod_chip_get_line(manager->gpiochip[chipidx], offset);
if (!manager->gpio_in[i]) {
g_warning("Unable to get input GPIO %d", i);
continue;
}
ret = gpiod_line_request_input(manager->gpio_in[i], "eg25manager");
if (ret < 0) {
g_warning("Unable to request input GPIO %d", i);
manager->gpio_in[i] = NULL;
}
}
return 0;
}
gboolean gpio_check_poweroff(struct EG25Manager *manager)
{
if (manager->gpio_in[GPIO_IN_STATUS] &&
gpiod_line_get_value(manager->gpio_in[GPIO_IN_STATUS]) == 1) {
// Asserting RESET line to prevent modem from rebooting
gpiod_line_set_value(manager->gpio_out[GPIO_OUT_RESET], 1);
return TRUE;
}
return FALSE;
}
void gpio_destroy(struct EG25Manager *manager)
{
int i;
for (i = 0; i < GPIO_OUT_COUNT; i++)
gpiod_line_release(manager->gpio_out[i]);
for (i = 0; i < GPIO_OUT_COUNT; i++) {
if (manager->gpio_out[i])
gpiod_line_release(manager->gpio_out[i]);
}
gpiod_chip_close(manager->gpiochip[0]);
for (i = 0; i < GPIO_IN_COUNT; i++) {
if (manager->gpio_in[i])
gpiod_line_release(manager->gpio_in[i]);
}
if (manager->gpiochip[0])
gpiod_chip_close(manager->gpiochip[0]);
if (manager->gpiochip[1])
gpiod_chip_close(manager->gpiochip[1]);
}

View File

@@ -15,3 +15,5 @@ int gpio_sequence_poweron(struct EG25Manager *state);
int gpio_sequence_shutdown(struct EG25Manager *state);
int gpio_sequence_suspend(struct EG25Manager *state);
int gpio_sequence_resume(struct EG25Manager *state);
gboolean gpio_check_poweroff(struct EG25Manager *manager);

View File

@@ -17,35 +17,29 @@
#include <glib-unix.h>
static gboolean quit_timeout_cb(struct EG25Manager *manager)
{
g_message("Modem down, quitting...");
g_main_loop_quit(manager->loop);
at_destroy(manager);
gpio_destroy(manager);
return FALSE;
}
static gboolean quit_app(struct EG25Manager *manager)
{
int i;
g_message("Request to quit...");
at_destroy(manager);
mm_iface_destroy(manager);
suspend_destroy(manager);
if (manager->modem_state >= EG25_STATE_STARTED) {
g_message("Powering down the modem...");
gpio_sequence_shutdown(manager);
manager->modem_state = EG25_STATE_FINISHING;
/*
* TODO: add a polling function to check STATUS and RI pins state
* (that way we could reduce the poweroff delay)
*/
g_timeout_add_seconds(30, G_SOURCE_FUNC(quit_timeout_cb), manager);
for (i = 0; i < 30; i++) {
if (gpio_check_poweroff(manager))
break;
sleep(1);
}
}
g_message("Modem down, quitting...");
mm_iface_destroy(manager);
suspend_destroy(manager);
g_bus_unwatch_name(manager->mm_watch);
g_main_loop_quit(manager->loop);
return FALSE;
}
@@ -140,7 +134,7 @@ int main(int argc, char *argv[])
return 1;
}
read(fd, compatible, sizeof(compatible));
if (strstr(compatible, "pine64,pinephone-1.1"))
if (!strstr(compatible, "pine64,pinephone-1.2"))
manager.braveheart = TRUE;
close(fd);
@@ -156,5 +150,7 @@ int main(int argc, char *argv[])
g_main_loop_run(manager.loop);
gpio_destroy(&manager);
return 0;
}

View File

@@ -46,6 +46,7 @@ struct EG25Manager {
struct gpiod_chip *gpiochip[2];
struct gpiod_line *gpio_out[5];
struct gpiod_line *gpio_in[2];
};
void modem_configure(struct EG25Manager *data);

View File

@@ -47,6 +47,9 @@ static void add_modem(struct EG25Manager *manager, GDBusObject *object)
modem_configure(manager);
path = mm_modem_get_device(manager->mm_modem);
if (manager->modem_usb_id)
g_free(manager->modem_usb_id);
manager->modem_usb_id = g_strdup(strrchr(path, '/') + 1);
gdbus_modem = MM_GDBUS_MODEM(manager->mm_modem);
@@ -189,5 +192,12 @@ void mm_iface_init(struct EG25Manager *manager)
void mm_iface_destroy(struct EG25Manager *manager)
{
g_clear_object(&manager->mm_manager);
if (manager->mm_manager) {
g_clear_object(&manager->mm_manager);
manager->mm_manager = NULL;
}
if (manager->mm_watch != 0) {
g_bus_unwatch_name(manager->mm_watch);
manager->mm_watch = 0;
}
}

View File

@@ -114,11 +114,12 @@ static void name_owner_cb(GObject *object,
g_assert(proxy == manager->suspend_proxy);
owner = g_dbus_proxy_get_name_owner(proxy);
if (owner)
if (owner) {
take_inhibitor(manager);
else
g_free(owner);
} else {
drop_inhibitor(manager);
g_free(owner);
}
}
static void on_proxy_acquired(GObject *object,
@@ -139,9 +140,10 @@ static void on_proxy_acquired(GObject *object,
g_signal_connect(manager->suspend_proxy, "g-signal", G_CALLBACK(signal_cb), manager);
owner = g_dbus_proxy_get_name_owner(manager->suspend_proxy);
if (owner)
if (owner) {
take_inhibitor(manager);
g_free(owner);
g_free(owner);
}
}
void suspend_init(struct EG25Manager *manager)
@@ -156,7 +158,10 @@ void suspend_init(struct EG25Manager *manager)
void suspend_destroy(struct EG25Manager *manager)
{
drop_inhibitor(manager);
g_object_unref(manager->suspend_proxy);
if (manager->suspend_proxy) {
g_object_unref(manager->suspend_proxy);
manager->suspend_proxy = NULL;
}
}
void suspend_inhibit(struct EG25Manager *manager, gboolean inhibit)