sl28cpld-hwmon.c 2.9 KB

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