as370-hwmon.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/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 u32 as370_hwmon_temp_config[] = {
  69. HWMON_T_INPUT,
  70. 0
  71. };
  72. static const struct hwmon_channel_info as370_hwmon_temp = {
  73. .type = hwmon_temp,
  74. .config = as370_hwmon_temp_config,
  75. };
  76. static const struct hwmon_channel_info *as370_hwmon_info[] = {
  77. &as370_hwmon_temp,
  78. NULL
  79. };
  80. static const struct hwmon_ops as370_hwmon_ops = {
  81. .is_visible = as370_hwmon_is_visible,
  82. .read = as370_hwmon_read,
  83. };
  84. static const struct hwmon_chip_info as370_chip_info = {
  85. .ops = &as370_hwmon_ops,
  86. .info = as370_hwmon_info,
  87. };
  88. static int as370_hwmon_probe(struct platform_device *pdev)
  89. {
  90. struct device *hwmon_dev;
  91. struct as370_hwmon *hwmon;
  92. struct device *dev = &pdev->dev;
  93. hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
  94. if (!hwmon)
  95. return -ENOMEM;
  96. hwmon->base = devm_platform_ioremap_resource(pdev, 0);
  97. if (IS_ERR(hwmon->base))
  98. return PTR_ERR(hwmon->base);
  99. init_pvt(hwmon);
  100. hwmon_dev = devm_hwmon_device_register_with_info(dev,
  101. "as370",
  102. hwmon,
  103. &as370_chip_info,
  104. NULL);
  105. return PTR_ERR_OR_ZERO(hwmon_dev);
  106. }
  107. static const struct of_device_id as370_hwmon_match[] = {
  108. { .compatible = "syna,as370-hwmon" },
  109. {},
  110. };
  111. MODULE_DEVICE_TABLE(of, as370_hwmon_match);
  112. static struct platform_driver as370_hwmon_driver = {
  113. .probe = as370_hwmon_probe,
  114. .driver = {
  115. .name = "as370-hwmon",
  116. .of_match_table = as370_hwmon_match,
  117. },
  118. };
  119. module_platform_driver(as370_hwmon_driver);
  120. MODULE_AUTHOR("Jisheng Zhang<jszhang@kernel.org>");
  121. MODULE_DESCRIPTION("Synaptics AS370 SoC hardware monitor");
  122. MODULE_LICENSE("GPL v2");