1 // SPDX-License-Identifier: GPL-2.0-only or BSD-3-Clause 2 3 /* 4 * Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. 5 */ 6 7 #include <linux/acpi.h> 8 #include <linux/device.h> 9 #include <linux/devm-helpers.h> 10 #include <linux/interrupt.h> 11 #include <linux/kernel.h> 12 #include <linux/mod_devicetable.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <linux/pm.h> 16 #include <linux/reboot.h> 17 #include <linux/types.h> 18 19 const char *rst_pwr_hid = "MLNXBF24"; 20 const char *low_pwr_hid = "MLNXBF29"; 21 22 struct pwr_mlxbf { 23 struct work_struct send_work; 24 const char *hid; 25 }; 26 27 static void pwr_mlxbf_send_work(struct work_struct *work) 28 { 29 acpi_bus_generate_netlink_event("button/power.*", "Power Button", 0x80, 1); 30 } 31 32 static irqreturn_t pwr_mlxbf_irq(int irq, void *ptr) 33 { 34 struct pwr_mlxbf *priv = ptr; 35 36 if (!strncmp(priv->hid, rst_pwr_hid, 8)) 37 emergency_restart(); 38 39 if (!strncmp(priv->hid, low_pwr_hid, 8)) 40 schedule_work(&priv->send_work); 41 42 return IRQ_HANDLED; 43 } 44 45 static int pwr_mlxbf_probe(struct platform_device *pdev) 46 { 47 struct device *dev = &pdev->dev; 48 struct acpi_device *adev; 49 struct pwr_mlxbf *priv; 50 const char *hid; 51 int irq, err; 52 53 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 54 if (!priv) 55 return -ENOMEM; 56 57 adev = ACPI_COMPANION(dev); 58 if (!adev) 59 return -ENXIO; 60 61 hid = acpi_device_hid(adev); 62 priv->hid = hid; 63 64 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0); 65 if (irq < 0) 66 return dev_err_probe(dev, irq, "Error getting %s irq.\n", priv->hid); 67 68 err = devm_work_autocancel(dev, &priv->send_work, pwr_mlxbf_send_work); 69 if (err) 70 return err; 71 72 err = devm_request_irq(dev, irq, pwr_mlxbf_irq, 0, hid, priv); 73 if (err) 74 dev_err(dev, "Failed request of %s irq\n", priv->hid); 75 76 return err; 77 } 78 79 static const struct acpi_device_id __maybe_unused pwr_mlxbf_acpi_match[] = { 80 { "MLNXBF24", 0 }, 81 { "MLNXBF29", 0 }, 82 {}, 83 }; 84 MODULE_DEVICE_TABLE(acpi, pwr_mlxbf_acpi_match); 85 86 static struct platform_driver pwr_mlxbf_driver = { 87 .driver = { 88 .name = "pwr_mlxbf", 89 .acpi_match_table = pwr_mlxbf_acpi_match, 90 }, 91 .probe = pwr_mlxbf_probe, 92 }; 93 94 module_platform_driver(pwr_mlxbf_driver); 95 96 MODULE_DESCRIPTION("Mellanox BlueField power driver"); 97 MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>"); 98 MODULE_LICENSE("Dual BSD/GPL"); 99