Skip to content

Commit

Permalink
feat(kscan): More complete PM support to drivers.
Browse files Browse the repository at this point in the history
* Update our GPIO kscan drivers to more completely support PM device,
  by doing proper hardare init/deinit in the PM action hook.
  • Loading branch information
petejohanson committed Jul 2, 2024
1 parent 3ecee59 commit ab84846
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 38 deletions.
2 changes: 2 additions & 0 deletions app/boards/shields/zmk_uno/zmk_uno.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

endpoint_sideband_behaviors {
compatible = "zmk,kscan-sideband-behaviors";

auto-enable;
kscan = <&kscan_sp3t_toggle>;

first_toggle_sideband: first_toggle_sideband {
Expand Down
3 changes: 3 additions & 0 deletions app/dts/bindings/kscan/zmk,kscan-sideband-behaviors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ compatible: "zmk,kscan-sideband-behaviors"
include: kscan.yaml

properties:
auto-enable:
type: boolean

kscan:
type: phandle
required: true
Expand Down
69 changes: 61 additions & 8 deletions app/module/drivers/kscan/kscan_gpio_charlieplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <zephyr/drivers/kscan.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/pm/device.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/util.h>

Expand Down Expand Up @@ -167,6 +168,21 @@ static int kscan_charlieplex_set_all_outputs(const struct device *dev, const int
return 0;
}

static int kscan_charlieplex_disconnect_all(const struct device *dev) {
const struct kscan_charlieplex_config *config = dev->config;

for (int i = 0; i < config->cells.len; i++) {
const struct gpio_dt_spec *gpio = &config->cells.gpios[i];
int err = gpio_pin_configure_dt(gpio, GPIO_DISCONNECTED);
if (err) {
LOG_ERR("Unable to configure pin %u on %s for input", gpio->pin, gpio->port->name);
return err;
}
}

return 0;
}

static int kscan_charlieplex_interrupt_configure(const struct device *dev,
const gpio_flags_t flags) {
const struct kscan_charlieplex_config *config = dev->config;
Expand Down Expand Up @@ -359,19 +375,54 @@ static int kscan_charlieplex_init_interrupt(const struct device *dev) {
return err;
}

static int kscan_charlieplex_init(const struct device *dev) {
struct kscan_charlieplex_data *data = dev->data;

data->dev = dev;

static void kscan_charlieplex_setup_pins(const struct device *dev) {
kscan_charlieplex_init_inputs(dev);
kscan_charlieplex_set_all_outputs(dev, 0);

const struct kscan_charlieplex_config *config = dev->config;
if (config->use_interrupt) {
kscan_charlieplex_init_interrupt(dev);
}
}

#if IS_ENABLED(CONFIG_PM_DEVICE)

static int kscan_charlieplex_pm_action(const struct device *dev, enum pm_device_action action) {
switch (action) {
case PM_DEVICE_ACTION_SUSPEND:
kscan_charlieplex_interrupt_configure(dev, GPIO_INT_DISABLE);
kscan_charlieplex_disconnect_all(dev);

return kscan_charlieplex_disable(dev);
case PM_DEVICE_ACTION_RESUME:
kscan_charlieplex_setup_pins(dev);

return kscan_charlieplex_enable(dev);
default:
return -ENOTSUP;
}
}

#endif // IS_ENABLED(CONFIG_PM_DEVICE)

static int kscan_charlieplex_init(const struct device *dev) {
struct kscan_charlieplex_data *data = dev->data;

data->dev = dev;

k_work_init_delayable(&data->work, kscan_charlieplex_work_handler);

#if IS_ENABLED(CONFIG_PM_DEVICE)
pm_device_init_suspended(dev);

#if IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)
pm_device_runtime_enable(dev);
#endif

#else
kscan_charlieplex_setup_pins(dev);
#endif

return 0;
}

Expand Down Expand Up @@ -406,8 +457,10 @@ static const struct kscan_driver_api kscan_charlieplex_api = {
COND_THIS_INTERRUPT(n, (.use_interrupt = INST_INTR_DEFINED(n), )) \
COND_THIS_INTERRUPT(n, (.interrupt = KSCAN_INTR_CFG_INIT(n), ))}; \
\
DEVICE_DT_INST_DEFINE(n, &kscan_charlieplex_init, NULL, &kscan_charlieplex_data_##n, \
&kscan_charlieplex_config_##n, POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \
&kscan_charlieplex_api);
PM_DEVICE_DT_INST_DEFINE(n, kscan_charlieplex_pm_action); \
\
DEVICE_DT_INST_DEFINE(n, &kscan_charlieplex_init, PM_DEVICE_DT_INST_GET(n), \
&kscan_charlieplex_data_##n, &kscan_charlieplex_config_##n, POST_KERNEL, \
CONFIG_KSCAN_INIT_PRIORITY, &kscan_charlieplex_api);

DT_INST_FOREACH_STATUS_OKAY(KSCAN_CHARLIEPLEX_INIT);
33 changes: 32 additions & 1 deletion app/module/drivers/kscan/kscan_gpio_direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,24 @@ static int kscan_direct_init_input_inst(const struct device *dev, const struct g
return 0;
}

#if IS_ENABLED(CONFIG_PM_DEVICE)

static int kscan_direct_disconnect_inputs(const struct device *dev) {
const struct kscan_direct_data *data = dev->data;

for (int i = 0; i < data->inputs.len; i++) {
const struct gpio_dt_spec *gpio = &data->inputs.gpios[i].spec;
int err = gpio_pin_configure_dt(gpio, GPIO_DISCONNECTED);
if (err) {
return err;
}
}

return 0;
}

#endif // IS_ENABLED(CONFIG_PM_DEVICE)

static int kscan_direct_init_inputs(const struct device *dev) {
const struct kscan_direct_data *data = dev->data;
const struct kscan_direct_config *config = dev->config;
Expand All @@ -317,9 +335,20 @@ static int kscan_direct_init(const struct device *dev) {
// Sort inputs by port so we can read each port just once per scan.
kscan_gpio_list_sort_by_port(&data->inputs);

k_work_init_delayable(&data->work, kscan_direct_work_handler);

#if IS_ENABLED(CONFIG_PM_DEVICE)
pm_device_init_suspended(dev);

#if IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)
pm_device_runtime_enable(dev);
#endif

#else

kscan_direct_init_inputs(dev);

k_work_init_delayable(&data->work, kscan_direct_work_handler);
#endif

return 0;
}
Expand All @@ -329,8 +358,10 @@ static int kscan_direct_init(const struct device *dev) {
static int kscan_direct_pm_action(const struct device *dev, enum pm_device_action action) {
switch (action) {
case PM_DEVICE_ACTION_SUSPEND:
kscan_direct_disconnect_inputs(dev);
return kscan_direct_disable(dev);
case PM_DEVICE_ACTION_RESUME:
kscan_direct_init_inputs(dev);
return kscan_direct_enable(dev);
default:
return -ENOTSUP;
Expand Down
57 changes: 53 additions & 4 deletions app/module/drivers/kscan/kscan_gpio_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,44 @@ static int kscan_matrix_init_outputs(const struct device *dev) {
return 0;
}

#if IS_ENABLED(CONFIG_PM_DEVICE)

static int kscan_matrix_disconnect_inputs(const struct device *dev) {
const struct kscan_matrix_data *data = dev->data;

for (int i = 0; i < data->inputs.len; i++) {
const struct gpio_dt_spec *gpio = &data->inputs.gpios[i].spec;
int err = gpio_pin_configure_dt(gpio, GPIO_DISCONNECTED);
if (err) {
return err;
}
}

return 0;
}

static int kscan_matrix_disconnect_outputs(const struct device *dev) {
const struct kscan_matrix_config *config = dev->config;

for (int i = 0; i < config->outputs.len; i++) {
const struct gpio_dt_spec *gpio = &config->outputs.gpios[i].spec;
int err = gpio_pin_configure_dt(gpio, GPIO_DISCONNECTED);
if (err) {
return err;
}
}

return 0;
}

#endif // IS_ENABLED(CONFIG_PM_DEVICE)

static void kscan_matrix_setup_pins(const struct device *dev) {
kscan_matrix_init_inputs(dev);
kscan_matrix_init_outputs(dev);
kscan_matrix_set_all_outputs(dev, 0);
}

static int kscan_matrix_init(const struct device *dev) {
struct kscan_matrix_data *data = dev->data;

Expand All @@ -413,12 +451,19 @@ static int kscan_matrix_init(const struct device *dev) {
// Sort inputs by port so we can read each port just once per scan.
kscan_gpio_list_sort_by_port(&data->inputs);

kscan_matrix_init_inputs(dev);
kscan_matrix_init_outputs(dev);
kscan_matrix_set_all_outputs(dev, 0);

k_work_init_delayable(&data->work, kscan_matrix_work_handler);

#if IS_ENABLED(CONFIG_PM_DEVICE)
pm_device_init_suspended(dev);

#if IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)
pm_device_runtime_enable(dev);
#endif

#else
kscan_matrix_setup_pins(dev);
#endif

return 0;
}

Expand All @@ -427,8 +472,12 @@ static int kscan_matrix_init(const struct device *dev) {
static int kscan_matrix_pm_action(const struct device *dev, enum pm_device_action action) {
switch (action) {
case PM_DEVICE_ACTION_SUSPEND:
kscan_matrix_disconnect_inputs(dev);
kscan_matrix_disconnect_outputs(dev);

return kscan_matrix_disable(dev);
case PM_DEVICE_ACTION_RESUME:
kscan_matrix_setup_pins(dev);
return kscan_matrix_enable(dev);
default:
return -ENOTSUP;
Expand Down
Loading

0 comments on commit ab84846

Please sign in to comment.