raspberrypi-hwmon.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Raspberry Pi voltage sensor driver
  4. *
  5. * Based on firmware/raspberrypi.c by Noralf Trønnes
  6. *
  7. * Copyright (C) 2018 Stefan Wahren <stefan.wahren@i2se.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/devm-helpers.h>
  11. #include <linux/err.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/workqueue.h>
  17. #include <soc/bcm2835/raspberrypi-firmware.h>
  18. #define UNDERVOLTAGE_STICKY_BIT BIT(16)
  19. struct rpi_hwmon_data {
  20. struct device *hwmon_dev;
  21. struct rpi_firmware *fw;
  22. u32 last_throttled;
  23. struct delayed_work get_values_poll_work;
  24. };
  25. static void rpi_firmware_get_throttled(struct rpi_hwmon_data *data)
  26. {
  27. u32 new_uv, old_uv, value;
  28. int ret;
  29. /* Request firmware to clear sticky bits */
  30. value = 0xffff;
  31. ret = rpi_firmware_property(data->fw, RPI_FIRMWARE_GET_THROTTLED,
  32. &value, sizeof(value));
  33. if (ret) {
  34. dev_err_once(data->hwmon_dev, "Failed to get throttled (%d)\n",
  35. ret);
  36. return;
  37. }
  38. new_uv = value & UNDERVOLTAGE_STICKY_BIT;
  39. old_uv = data->last_throttled & UNDERVOLTAGE_STICKY_BIT;
  40. data->last_throttled = value;
  41. if (new_uv == old_uv)
  42. return;
  43. if (new_uv)
  44. dev_crit(data->hwmon_dev, "Undervoltage detected!\n");
  45. else
  46. dev_info(data->hwmon_dev, "Voltage normalised\n");
  47. hwmon_notify_event(data->hwmon_dev, hwmon_in, hwmon_in_lcrit_alarm, 0);
  48. }
  49. static void get_values_poll(struct work_struct *work)
  50. {
  51. struct rpi_hwmon_data *data;
  52. data = container_of(work, struct rpi_hwmon_data,
  53. get_values_poll_work.work);
  54. rpi_firmware_get_throttled(data);
  55. /*
  56. * We can't run faster than the sticky shift (100ms) since we get
  57. * flipping in the sticky bits that are cleared.
  58. */
  59. schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
  60. }
  61. static int rpi_read(struct device *dev, enum hwmon_sensor_types type,
  62. u32 attr, int channel, long *val)
  63. {
  64. struct rpi_hwmon_data *data = dev_get_drvdata(dev);
  65. *val = !!(data->last_throttled & UNDERVOLTAGE_STICKY_BIT);
  66. return 0;
  67. }
  68. static const struct hwmon_channel_info * const rpi_info[] = {
  69. HWMON_CHANNEL_INFO(in,
  70. HWMON_I_LCRIT_ALARM),
  71. NULL
  72. };
  73. static const struct hwmon_ops rpi_hwmon_ops = {
  74. .visible = 0444,
  75. .read = rpi_read,
  76. };
  77. static const struct hwmon_chip_info rpi_chip_info = {
  78. .ops = &rpi_hwmon_ops,
  79. .info = rpi_info,
  80. };
  81. static int rpi_hwmon_probe(struct platform_device *pdev)
  82. {
  83. struct device *dev = &pdev->dev;
  84. struct rpi_hwmon_data *data;
  85. int ret;
  86. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  87. if (!data)
  88. return -ENOMEM;
  89. /* Parent driver assure that firmware is correct */
  90. data->fw = dev_get_drvdata(dev->parent);
  91. data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "rpi_volt",
  92. data,
  93. &rpi_chip_info,
  94. NULL);
  95. if (IS_ERR(data->hwmon_dev))
  96. return PTR_ERR(data->hwmon_dev);
  97. ret = devm_delayed_work_autocancel(dev, &data->get_values_poll_work,
  98. get_values_poll);
  99. if (ret)
  100. return ret;
  101. platform_set_drvdata(pdev, data);
  102. schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
  103. return 0;
  104. }
  105. static int rpi_hwmon_suspend(struct device *dev)
  106. {
  107. struct rpi_hwmon_data *data = dev_get_drvdata(dev);
  108. cancel_delayed_work_sync(&data->get_values_poll_work);
  109. return 0;
  110. }
  111. static int rpi_hwmon_resume(struct device *dev)
  112. {
  113. struct rpi_hwmon_data *data = dev_get_drvdata(dev);
  114. get_values_poll(&data->get_values_poll_work.work);
  115. return 0;
  116. }
  117. static DEFINE_SIMPLE_DEV_PM_OPS(rpi_hwmon_pm_ops, rpi_hwmon_suspend,
  118. rpi_hwmon_resume);
  119. static struct platform_driver rpi_hwmon_driver = {
  120. .probe = rpi_hwmon_probe,
  121. .driver = {
  122. .name = "raspberrypi-hwmon",
  123. .pm = pm_ptr(&rpi_hwmon_pm_ops),
  124. },
  125. };
  126. module_platform_driver(rpi_hwmon_driver);
  127. MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net>");
  128. MODULE_DESCRIPTION("Raspberry Pi voltage sensor driver");
  129. MODULE_LICENSE("GPL v2");
  130. MODULE_ALIAS("platform:raspberrypi-hwmon");