pwr-mlxbf.c 2.2 KB

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