surface_fan.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Surface Fan driver for Surface System Aggregator Module. It provides access
  4. * to the fan's rpm through the hwmon system.
  5. *
  6. * Copyright (C) 2023 Ivor Wanders <ivor@iwanders.net>
  7. */
  8. #include <linux/hwmon.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/surface_aggregator/device.h>
  12. #include <linux/types.h>
  13. // SSAM
  14. SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_fan_rpm_get, __le16, {
  15. .target_category = SSAM_SSH_TC_FAN,
  16. .command_id = 0x01,
  17. });
  18. static int surface_fan_hwmon_read(struct device *dev,
  19. enum hwmon_sensor_types type, u32 attr,
  20. int channel, long *val)
  21. {
  22. struct ssam_device *sdev = dev_get_drvdata(dev);
  23. int ret;
  24. __le16 value;
  25. ret = __ssam_fan_rpm_get(sdev, &value);
  26. if (ret)
  27. return ret;
  28. *val = le16_to_cpu(value);
  29. return 0;
  30. }
  31. static const struct hwmon_channel_info *const surface_fan_info[] = {
  32. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
  33. NULL
  34. };
  35. static const struct hwmon_ops surface_fan_hwmon_ops = {
  36. .visible = 0444,
  37. .read = surface_fan_hwmon_read,
  38. };
  39. static const struct hwmon_chip_info surface_fan_chip_info = {
  40. .ops = &surface_fan_hwmon_ops,
  41. .info = surface_fan_info,
  42. };
  43. static int surface_fan_probe(struct ssam_device *sdev)
  44. {
  45. struct device *hdev;
  46. hdev = devm_hwmon_device_register_with_info(&sdev->dev,
  47. "surface_fan", sdev,
  48. &surface_fan_chip_info,
  49. NULL);
  50. return PTR_ERR_OR_ZERO(hdev);
  51. }
  52. static const struct ssam_device_id ssam_fan_match[] = {
  53. { SSAM_SDEV(FAN, SAM, 0x01, 0x01) },
  54. {},
  55. };
  56. MODULE_DEVICE_TABLE(ssam, ssam_fan_match);
  57. static struct ssam_device_driver surface_fan = {
  58. .probe = surface_fan_probe,
  59. .match_table = ssam_fan_match,
  60. .driver = {
  61. .name = "surface_fan",
  62. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  63. },
  64. };
  65. module_ssam_device_driver(surface_fan);
  66. MODULE_AUTHOR("Ivor Wanders <ivor@iwanders.net>");
  67. MODULE_DESCRIPTION("Fan Driver for Surface System Aggregator Module");
  68. MODULE_LICENSE("GPL");