sl28cpld-hwmon.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sl28cpld hardware monitoring driver
  4. *
  5. * Copyright 2020 Kontron Europe GmbH
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/hwmon.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/property.h>
  14. #include <linux/regmap.h>
  15. #define FAN_INPUT 0x00
  16. #define FAN_SCALE_X8 BIT(7)
  17. #define FAN_VALUE_MASK GENMASK(6, 0)
  18. struct sl28cpld_hwmon {
  19. struct regmap *regmap;
  20. u32 offset;
  21. };
  22. static umode_t sl28cpld_hwmon_is_visible(const void *data,
  23. enum hwmon_sensor_types type,
  24. u32 attr, int channel)
  25. {
  26. return 0444;
  27. }
  28. static int sl28cpld_hwmon_read(struct device *dev,
  29. enum hwmon_sensor_types type, u32 attr,
  30. int channel, long *input)
  31. {
  32. struct sl28cpld_hwmon *hwmon = dev_get_drvdata(dev);
  33. unsigned int value;
  34. int ret;
  35. switch (attr) {
  36. case hwmon_fan_input:
  37. ret = regmap_read(hwmon->regmap, hwmon->offset + FAN_INPUT,
  38. &value);
  39. if (ret)
  40. return ret;
  41. /*
  42. * The register has a 7 bit value and 1 bit which indicates the
  43. * scale. If the MSB is set, then the lower 7 bit has to be
  44. * multiplied by 8, to get the correct reading.
  45. */
  46. if (value & FAN_SCALE_X8)
  47. value = FIELD_GET(FAN_VALUE_MASK, value) << 3;
  48. /*
  49. * The counter period is 1000ms and the sysfs specification
  50. * says we should assume 2 pulses per revolution.
  51. */
  52. value *= 60 / 2;
  53. break;
  54. default:
  55. return -EOPNOTSUPP;
  56. }
  57. *input = value;
  58. return 0;
  59. }
  60. static const struct hwmon_channel_info *sl28cpld_hwmon_info[] = {
  61. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
  62. NULL
  63. };
  64. static const struct hwmon_ops sl28cpld_hwmon_ops = {
  65. .is_visible = sl28cpld_hwmon_is_visible,
  66. .read = sl28cpld_hwmon_read,
  67. };
  68. static const struct hwmon_chip_info sl28cpld_hwmon_chip_info = {
  69. .ops = &sl28cpld_hwmon_ops,
  70. .info = sl28cpld_hwmon_info,
  71. };
  72. static int sl28cpld_hwmon_probe(struct platform_device *pdev)
  73. {
  74. struct sl28cpld_hwmon *hwmon;
  75. struct device *hwmon_dev;
  76. int ret;
  77. if (!pdev->dev.parent)
  78. return -ENODEV;
  79. hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
  80. if (!hwmon)
  81. return -ENOMEM;
  82. hwmon->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  83. if (!hwmon->regmap)
  84. return -ENODEV;
  85. ret = device_property_read_u32(&pdev->dev, "reg", &hwmon->offset);
  86. if (ret)
  87. return -EINVAL;
  88. hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
  89. "sl28cpld_hwmon", hwmon,
  90. &sl28cpld_hwmon_chip_info, NULL);
  91. if (IS_ERR(hwmon_dev))
  92. dev_err(&pdev->dev, "failed to register as hwmon device");
  93. return PTR_ERR_OR_ZERO(hwmon_dev);
  94. }
  95. static const struct of_device_id sl28cpld_hwmon_of_match[] = {
  96. { .compatible = "kontron,sl28cpld-fan" },
  97. {}
  98. };
  99. MODULE_DEVICE_TABLE(of, sl28cpld_hwmon_of_match);
  100. static struct platform_driver sl28cpld_hwmon_driver = {
  101. .probe = sl28cpld_hwmon_probe,
  102. .driver = {
  103. .name = "sl28cpld-fan",
  104. .of_match_table = sl28cpld_hwmon_of_match,
  105. },
  106. };
  107. module_platform_driver(sl28cpld_hwmon_driver);
  108. MODULE_DESCRIPTION("sl28cpld Hardware Monitoring Driver");
  109. MODULE_AUTHOR("Michael Walle <[email protected]>");
  110. MODULE_LICENSE("GPL");