as370-hwmon.c 2.9 KB

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