raspberrypi-hwmon.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <[email protected]>
  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 umode_t rpi_is_visible(const void *_data, enum hwmon_sensor_types type,
  69. u32 attr, int channel)
  70. {
  71. return 0444;
  72. }
  73. static const struct hwmon_channel_info *rpi_info[] = {
  74. HWMON_CHANNEL_INFO(in,
  75. HWMON_I_LCRIT_ALARM),
  76. NULL
  77. };
  78. static const struct hwmon_ops rpi_hwmon_ops = {
  79. .is_visible = rpi_is_visible,
  80. .read = rpi_read,
  81. };
  82. static const struct hwmon_chip_info rpi_chip_info = {
  83. .ops = &rpi_hwmon_ops,
  84. .info = rpi_info,
  85. };
  86. static int rpi_hwmon_probe(struct platform_device *pdev)
  87. {
  88. struct device *dev = &pdev->dev;
  89. struct rpi_hwmon_data *data;
  90. int ret;
  91. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  92. if (!data)
  93. return -ENOMEM;
  94. /* Parent driver assure that firmware is correct */
  95. data->fw = dev_get_drvdata(dev->parent);
  96. data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "rpi_volt",
  97. data,
  98. &rpi_chip_info,
  99. NULL);
  100. if (IS_ERR(data->hwmon_dev))
  101. return PTR_ERR(data->hwmon_dev);
  102. ret = devm_delayed_work_autocancel(dev, &data->get_values_poll_work,
  103. get_values_poll);
  104. if (ret)
  105. return ret;
  106. platform_set_drvdata(pdev, data);
  107. schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
  108. return 0;
  109. }
  110. static struct platform_driver rpi_hwmon_driver = {
  111. .probe = rpi_hwmon_probe,
  112. .driver = {
  113. .name = "raspberrypi-hwmon",
  114. },
  115. };
  116. module_platform_driver(rpi_hwmon_driver);
  117. MODULE_AUTHOR("Stefan Wahren <[email protected]>");
  118. MODULE_DESCRIPTION("Raspberry Pi voltage sensor driver");
  119. MODULE_LICENSE("GPL v2");
  120. MODULE_ALIAS("platform:raspberrypi-hwmon");