Commit a5f67be5 authored by Frans Klaver's avatar Frans Klaver Committed by Sebastian Reichel

power: reset: ltc2952: reduce dependency on global variables

Documentation/CodingStyle ch.4 mentions in a side node that global
variables should only be used if you really need them. Reduce the use of
the global instance of ltc2952_poweroff so we may eventually remove it
entirely.

While at it, rename ltc2952_poweroff_data to ltc2952_poweroff, just to
save that little bit of typing.
Signed-off-by: default avatarFrans Klaver <frans.klaver@xsens.com>
Signed-off-by: default avatarSebastian Reichel <sre@kernel.org>
parent 62113b31
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
#include <linux/gpio/consumer.h> #include <linux/gpio/consumer.h>
#include <linux/reboot.h> #include <linux/reboot.h>
struct ltc2952_poweroff_data { struct ltc2952_poweroff {
struct hrtimer timer_trigger; struct hrtimer timer_trigger;
struct hrtimer timer_wde; struct hrtimer timer_wde;
...@@ -77,8 +77,14 @@ struct ltc2952_poweroff_data { ...@@ -77,8 +77,14 @@ struct ltc2952_poweroff_data {
struct gpio_desc *gpio_kill; struct gpio_desc *gpio_kill;
}; };
#define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
/*
* This global variable is only needed for pm_power_off. We should
* remove it entirely once we don't need the global state anymore.
*/
static struct ltc2952_poweroff *ltc2952_data;
static int ltc2952_poweroff_panic; static int ltc2952_poweroff_panic;
static struct ltc2952_poweroff_data *ltc2952_data;
/** /**
* ltc2952_poweroff_timer_wde - Timer callback * ltc2952_poweroff_timer_wde - Timer callback
...@@ -94,29 +100,29 @@ static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer) ...@@ -94,29 +100,29 @@ static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
ktime_t now; ktime_t now;
int state; int state;
unsigned long overruns; unsigned long overruns;
struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
if (ltc2952_poweroff_panic) if (ltc2952_poweroff_panic)
return HRTIMER_NORESTART; return HRTIMER_NORESTART;
state = gpiod_get_value(ltc2952_data->gpio_watchdog); state = gpiod_get_value(data->gpio_watchdog);
gpiod_set_value(ltc2952_data->gpio_watchdog, !state); gpiod_set_value(data->gpio_watchdog, !state);
now = hrtimer_cb_get_time(timer); now = hrtimer_cb_get_time(timer);
overruns = hrtimer_forward(timer, now, ltc2952_data->wde_interval); overruns = hrtimer_forward(timer, now, data->wde_interval);
return HRTIMER_RESTART; return HRTIMER_RESTART;
} }
static enum hrtimer_restart ltc2952_poweroff_timer_trigger( static enum hrtimer_restart
struct hrtimer *timer) ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
{ {
int ret; struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
int ret = hrtimer_start(&data->timer_wde,
ret = hrtimer_start(&ltc2952_data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
ltc2952_data->wde_interval, HRTIMER_MODE_REL);
if (ret) { if (ret) {
dev_err(ltc2952_data->dev, "unable to start the timer\n"); dev_err(data->dev, "unable to start the timer\n");
/* /*
* The device will not toggle the watchdog reset, * The device will not toggle the watchdog reset,
* thus shut down is only safe if the PowerPath controller * thus shut down is only safe if the PowerPath controller
...@@ -127,7 +133,7 @@ static enum hrtimer_restart ltc2952_poweroff_timer_trigger( ...@@ -127,7 +133,7 @@ static enum hrtimer_restart ltc2952_poweroff_timer_trigger(
*/ */
} }
dev_info(ltc2952_data->dev, "executing shutdown\n"); dev_info(data->dev, "executing shutdown\n");
orderly_poweroff(true); orderly_poweroff(true);
...@@ -146,7 +152,7 @@ static enum hrtimer_restart ltc2952_poweroff_timer_trigger( ...@@ -146,7 +152,7 @@ static enum hrtimer_restart ltc2952_poweroff_timer_trigger(
static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id) static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
{ {
int ret; int ret;
struct ltc2952_poweroff_data *data = dev_id; struct ltc2952_poweroff *data = dev_id;
if (ltc2952_poweroff_panic) if (ltc2952_poweroff_panic)
goto irq_ok; goto irq_ok;
...@@ -187,7 +193,7 @@ static int ltc2952_poweroff_resume(struct platform_device *pdev) ...@@ -187,7 +193,7 @@ static int ltc2952_poweroff_resume(struct platform_device *pdev)
return -ENOSYS; return -ENOSYS;
} }
static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data) static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
{ {
data->wde_interval = ktime_set(0, 300L*1E6L); data->wde_interval = ktime_set(0, 300L*1E6L);
data->trigger_delay = ktime_set(2, 500L*1E6L); data->trigger_delay = ktime_set(2, 500L*1E6L);
...@@ -202,28 +208,26 @@ static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data) ...@@ -202,28 +208,26 @@ static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data)
static int ltc2952_poweroff_init(struct platform_device *pdev) static int ltc2952_poweroff_init(struct platform_device *pdev)
{ {
int ret, virq; int ret, virq;
struct ltc2952_poweroff_data *data; struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
data = ltc2952_data; ltc2952_poweroff_default(data);
ltc2952_poweroff_default(ltc2952_data);
ltc2952_data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog", data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
GPIOD_OUT_LOW); GPIOD_OUT_LOW);
if (IS_ERR(ltc2952_data->gpio_watchdog)) { if (IS_ERR(data->gpio_watchdog)) {
ret = PTR_ERR(ltc2952_data->gpio_watchdog); ret = PTR_ERR(data->gpio_watchdog);
dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n"); dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
return ret; return ret;
} }
ltc2952_data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
GPIOD_OUT_LOW); if (IS_ERR(data->gpio_kill)) {
if (IS_ERR(ltc2952_data->gpio_kill)) { ret = PTR_ERR(data->gpio_kill);
ret = PTR_ERR(ltc2952_data->gpio_kill);
dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n"); dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
return ret; return ret;
} }
ltc2952_data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger", data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger",
GPIOD_IN); GPIOD_IN);
if (IS_ERR(ltc2952_data->gpio_trigger)) { if (IS_ERR(ltc2952_data->gpio_trigger)) {
ret = PTR_ERR(ltc2952_data->gpio_trigger); ret = PTR_ERR(ltc2952_data->gpio_trigger);
...@@ -231,7 +235,7 @@ static int ltc2952_poweroff_init(struct platform_device *pdev) ...@@ -231,7 +235,7 @@ static int ltc2952_poweroff_init(struct platform_device *pdev)
return ret; return ret;
} }
virq = gpiod_to_irq(ltc2952_data->gpio_trigger); virq = gpiod_to_irq(data->gpio_trigger);
if (virq < 0) { if (virq < 0) {
dev_err(&pdev->dev, "cannot map GPIO as interrupt"); dev_err(&pdev->dev, "cannot map GPIO as interrupt");
return ret; return ret;
...@@ -241,7 +245,7 @@ static int ltc2952_poweroff_init(struct platform_device *pdev) ...@@ -241,7 +245,7 @@ static int ltc2952_poweroff_init(struct platform_device *pdev)
ltc2952_poweroff_handler, ltc2952_poweroff_handler,
(IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING), (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
"ltc2952-poweroff", "ltc2952-poweroff",
ltc2952_data); data);
if (ret) { if (ret) {
dev_err(&pdev->dev, "cannot configure an interrupt handler\n"); dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
...@@ -254,23 +258,26 @@ static int ltc2952_poweroff_init(struct platform_device *pdev) ...@@ -254,23 +258,26 @@ static int ltc2952_poweroff_init(struct platform_device *pdev)
static int ltc2952_poweroff_probe(struct platform_device *pdev) static int ltc2952_poweroff_probe(struct platform_device *pdev)
{ {
int ret; int ret;
struct ltc2952_poweroff *data;
if (pm_power_off) { if (pm_power_off) {
dev_err(&pdev->dev, "pm_power_off already registered"); dev_err(&pdev->dev, "pm_power_off already registered");
return -EBUSY; return -EBUSY;
} }
ltc2952_data = devm_kzalloc(&pdev->dev, sizeof(*ltc2952_data), data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
GFP_KERNEL); if (!data)
if (!ltc2952_data)
return -ENOMEM; return -ENOMEM;
ltc2952_data->dev = &pdev->dev; data->dev = &pdev->dev;
platform_set_drvdata(pdev, data);
ret = ltc2952_poweroff_init(pdev); ret = ltc2952_poweroff_init(pdev);
if (ret) if (ret)
return ret; return ret;
/* TODO: remove ltc2952_data */
ltc2952_data = data;
pm_power_off = &ltc2952_poweroff_kill; pm_power_off = &ltc2952_poweroff_kill;
dev_info(&pdev->dev, "probe successful\n"); dev_info(&pdev->dev, "probe successful\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment