raspberrypi-hwmon.c 3.8 KB

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