as370-hwmon.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Synaptics AS370 SoC Hardware Monitoring Driver
  4. *
  5. * Copyright (C) 2018 Synaptics Incorporated
  6. * Author: Jisheng Zhang <[email protected]>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/hwmon.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #define CTRL 0x0
  15. #define PD BIT(0)
  16. #define EN BIT(1)
  17. #define T_SEL BIT(2)
  18. #define V_SEL BIT(3)
  19. #define NMOS_SEL BIT(8)
  20. #define PMOS_SEL BIT(9)
  21. #define STS 0x4
  22. #define BN_MASK GENMASK(11, 0)
  23. #define EOC BIT(12)
  24. struct as370_hwmon {
  25. void __iomem *base;
  26. };
  27. static void init_pvt(struct as370_hwmon *hwmon)
  28. {
  29. u32 val;
  30. void __iomem *addr = hwmon->base + CTRL;
  31. val = PD;
  32. writel_relaxed(val, addr);
  33. val |= T_SEL;
  34. writel_relaxed(val, addr);
  35. val |= EN;
  36. writel_relaxed(val, addr);
  37. val &= ~PD;
  38. writel_relaxed(val, addr);
  39. }
  40. static int as370_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  41. u32 attr, int channel, long *temp)
  42. {
  43. int val;
  44. struct as370_hwmon *hwmon = dev_get_drvdata(dev);
  45. switch (attr) {
  46. case hwmon_temp_input:
  47. val = readl_relaxed(hwmon->base + STS) & BN_MASK;
  48. *temp = DIV_ROUND_CLOSEST(val * 251802, 4096) - 85525;
  49. break;
  50. default:
  51. return -EOPNOTSUPP;
  52. }
  53. return 0;
  54. }
  55. static umode_t
  56. as370_hwmon_is_visible(const void *data, enum hwmon_sensor_types type,
  57. u32 attr, int channel)
  58. {
  59. if (type != hwmon_temp)
  60. return 0;
  61. switch (attr) {
  62. case hwmon_temp_input:
  63. return 0444;
  64. default:
  65. return 0;
  66. }
  67. }
  68. static const struct hwmon_channel_info *as370_hwmon_info[] = {
  69. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
  70. NULL
  71. };
  72. static const struct hwmon_ops as370_hwmon_ops = {
  73. .is_visible = as370_hwmon_is_visible,
  74. .read = as370_hwmon_read,
  75. };
  76. static const struct hwmon_chip_info as370_chip_info = {
  77. .ops = &as370_hwmon_ops,
  78. .info = as370_hwmon_info,
  79. };
  80. static int as370_hwmon_probe(struct platform_device *pdev)
  81. {
  82. struct device *hwmon_dev;
  83. struct as370_hwmon *hwmon;
  84. struct device *dev = &pdev->dev;
  85. hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
  86. if (!hwmon)
  87. return -ENOMEM;
  88. hwmon->base = devm_platform_ioremap_resource(pdev, 0);
  89. if (IS_ERR(hwmon->base))
  90. return PTR_ERR(hwmon->base);
  91. init_pvt(hwmon);
  92. hwmon_dev = devm_hwmon_device_register_with_info(dev,
  93. "as370",
  94. hwmon,
  95. &as370_chip_info,
  96. NULL);
  97. return PTR_ERR_OR_ZERO(hwmon_dev);
  98. }
  99. static const struct of_device_id as370_hwmon_match[] = {
  100. { .compatible = "syna,as370-hwmon" },
  101. {},
  102. };
  103. MODULE_DEVICE_TABLE(of, as370_hwmon_match);
  104. static struct platform_driver as370_hwmon_driver = {
  105. .probe = as370_hwmon_probe,
  106. .driver = {
  107. .name = "as370-hwmon",
  108. .of_match_table = as370_hwmon_match,
  109. },
  110. };
  111. module_platform_driver(as370_hwmon_driver);
  112. MODULE_AUTHOR("Jisheng Zhang<[email protected]>");
  113. MODULE_DESCRIPTION("Synaptics AS370 SoC hardware monitor");
  114. MODULE_LICENSE("GPL v2");