summaryrefslogtreecommitdiffstats
path: root/drivers/power/reset/pwr-mlxbf.c
blob: 1775b318d0ef4187cd96031a5a83af3b1e94358a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause

/*
 *  Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES.
 */

#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/devm-helpers.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/reboot.h>
#include <linux/types.h>

struct pwr_mlxbf {
	struct work_struct reboot_work;
	struct work_struct shutdown_work;
	const char *hid;
};

static void pwr_mlxbf_reboot_work(struct work_struct *work)
{
	acpi_bus_generate_netlink_event("button/reboot.*", "Reboot Button", 0x80, 1);
}

static void pwr_mlxbf_shutdown_work(struct work_struct *work)
{
	acpi_bus_generate_netlink_event("button/power.*", "Power Button", 0x80, 1);
}

static irqreturn_t pwr_mlxbf_irq(int irq, void *ptr)
{
	const char *rst_pwr_hid = "MLNXBF24";
	const char *low_pwr_hid = "MLNXBF29";
	struct pwr_mlxbf *priv = ptr;

	if (!strncmp(priv->hid, rst_pwr_hid, 8))
		schedule_work(&priv->reboot_work);

	if (!strncmp(priv->hid, low_pwr_hid, 8))
		schedule_work(&priv->shutdown_work);

	return IRQ_HANDLED;
}

static int pwr_mlxbf_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct acpi_device *adev;
	struct pwr_mlxbf *priv;
	const char *hid;
	int irq, err;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	adev = ACPI_COMPANION(dev);
	if (!adev)
		return -ENXIO;

	hid = acpi_device_hid(adev);
	priv->hid = hid;

	irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
	if (irq < 0)
		return dev_err_probe(dev, irq, "Error getting %s irq.\n", priv->hid);

	err = devm_work_autocancel(dev, &priv->shutdown_work, pwr_mlxbf_shutdown_work);
	if (err)
		return err;

	err = devm_work_autocancel(dev, &priv->reboot_work, pwr_mlxbf_reboot_work);
	if (err)
		return err;

	err = devm_request_irq(dev, irq, pwr_mlxbf_irq, 0, hid, priv);
	if (err)
		dev_err(dev, "Failed request of %s irq\n", priv->hid);

	return err;
}

static const struct acpi_device_id __maybe_unused pwr_mlxbf_acpi_match[] = {
	{ "MLNXBF24", 0 },
	{ "MLNXBF29", 0 },
	{},
};
MODULE_DEVICE_TABLE(acpi, pwr_mlxbf_acpi_match);

static struct platform_driver pwr_mlxbf_driver = {
	.driver = {
		.name = "pwr_mlxbf",
		.acpi_match_table = pwr_mlxbf_acpi_match,
	},
	.probe    = pwr_mlxbf_probe,
};

module_platform_driver(pwr_mlxbf_driver);

MODULE_DESCRIPTION("Mellanox BlueField power driver");
MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
MODULE_LICENSE("Dual BSD/GPL");