loongson2_thermal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Author: zhanghongchen <zhanghongchen@loongson.cn>
  4. * Yinbo Zhu <zhuyinbo@loongson.cn>
  5. * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/minmax.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/thermal.h>
  15. #include <linux/units.h>
  16. #include "thermal_hwmon.h"
  17. #define LOONGSON2_MAX_SENSOR_SEL_NUM 3
  18. #define LOONGSON2_THSENS_CTRL_HI_REG 0x0
  19. #define LOONGSON2_THSENS_CTRL_LOW_REG 0x8
  20. #define LOONGSON2_THSENS_STATUS_REG 0x10
  21. #define LOONGSON2_THSENS_OUT_REG 0x14
  22. #define LOONGSON2_THSENS_INT_LO BIT(0)
  23. #define LOONGSON2_THSENS_INT_HIGH BIT(1)
  24. #define LOONGSON2_THSENS_INT_EN (LOONGSON2_THSENS_INT_LO | \
  25. LOONGSON2_THSENS_INT_HIGH)
  26. #define LOONGSON2_THSENS_OUT_MASK 0xFF
  27. /*
  28. * This flag is used to indicate the temperature reading
  29. * method of the Loongson-2K2000
  30. */
  31. #define LS2K2000_THSENS_OUT_FLAG BIT(0)
  32. struct loongson2_thermal_chip_data {
  33. unsigned int thermal_sensor_sel;
  34. unsigned int flags;
  35. };
  36. struct loongson2_thermal_data {
  37. void __iomem *ctrl_reg;
  38. void __iomem *temp_reg;
  39. const struct loongson2_thermal_chip_data *chip_data;
  40. };
  41. static void loongson2_set_ctrl_regs(struct loongson2_thermal_data *data,
  42. int ctrl_data, bool low, bool enable)
  43. {
  44. int reg_ctrl = 0;
  45. int reg_off = data->chip_data->thermal_sensor_sel * 2;
  46. int ctrl_reg = low ? LOONGSON2_THSENS_CTRL_LOW_REG : LOONGSON2_THSENS_CTRL_HI_REG;
  47. reg_ctrl = ctrl_data + HECTO;
  48. reg_ctrl |= enable ? 0x100 : 0;
  49. writew(reg_ctrl, data->ctrl_reg + ctrl_reg + reg_off);
  50. }
  51. static int loongson2_thermal_set(struct loongson2_thermal_data *data,
  52. int low, int high, bool enable)
  53. {
  54. /* Set low temperature threshold */
  55. loongson2_set_ctrl_regs(data, clamp(-40, low, high), true, enable);
  56. /* Set high temperature threshold */
  57. loongson2_set_ctrl_regs(data, clamp(125, low, high), false, enable);
  58. return 0;
  59. }
  60. static int loongson2_2k1000_get_temp(struct thermal_zone_device *tz, int *temp)
  61. {
  62. int val;
  63. struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
  64. val = readl(data->ctrl_reg + LOONGSON2_THSENS_OUT_REG);
  65. *temp = ((val & LOONGSON2_THSENS_OUT_MASK) - HECTO) * KILO;
  66. return 0;
  67. }
  68. static int loongson2_2k2000_get_temp(struct thermal_zone_device *tz, int *temp)
  69. {
  70. int val;
  71. struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
  72. val = readl(data->temp_reg);
  73. *temp = ((val & 0xffff) * 820 / 0x4000 - 311) * KILO;
  74. return 0;
  75. }
  76. static irqreturn_t loongson2_thermal_irq_thread(int irq, void *dev)
  77. {
  78. struct thermal_zone_device *tzd = dev;
  79. struct loongson2_thermal_data *data = thermal_zone_device_priv(tzd);
  80. writeb(LOONGSON2_THSENS_INT_EN, data->ctrl_reg + LOONGSON2_THSENS_STATUS_REG);
  81. thermal_zone_device_update(tzd, THERMAL_EVENT_UNSPECIFIED);
  82. return IRQ_HANDLED;
  83. }
  84. static int loongson2_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
  85. {
  86. struct loongson2_thermal_data *data = thermal_zone_device_priv(tz);
  87. return loongson2_thermal_set(data, low/MILLI, high/MILLI, true);
  88. }
  89. static const struct thermal_zone_device_ops loongson2_2k1000_of_thermal_ops = {
  90. .get_temp = loongson2_2k1000_get_temp,
  91. .set_trips = loongson2_thermal_set_trips,
  92. };
  93. static const struct thermal_zone_device_ops loongson2_2k2000_of_thermal_ops = {
  94. .get_temp = loongson2_2k2000_get_temp,
  95. .set_trips = loongson2_thermal_set_trips,
  96. };
  97. static int loongson2_thermal_probe(struct platform_device *pdev)
  98. {
  99. const struct thermal_zone_device_ops *thermal_ops;
  100. struct device *dev = &pdev->dev;
  101. struct loongson2_thermal_data *data;
  102. struct thermal_zone_device *tzd;
  103. int ret, irq, i;
  104. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  105. if (!data)
  106. return -ENOMEM;
  107. data->chip_data = device_get_match_data(dev);
  108. data->ctrl_reg = devm_platform_ioremap_resource(pdev, 0);
  109. if (IS_ERR(data->ctrl_reg))
  110. return PTR_ERR(data->ctrl_reg);
  111. /* The temperature output register is separate for Loongson-2K2000 */
  112. if (data->chip_data->flags & LS2K2000_THSENS_OUT_FLAG) {
  113. data->temp_reg = devm_platform_ioremap_resource(pdev, 1);
  114. if (IS_ERR(data->temp_reg))
  115. return PTR_ERR(data->temp_reg);
  116. thermal_ops = &loongson2_2k2000_of_thermal_ops;
  117. } else {
  118. thermal_ops = &loongson2_2k1000_of_thermal_ops;
  119. }
  120. irq = platform_get_irq(pdev, 0);
  121. if (irq < 0)
  122. return irq;
  123. writeb(LOONGSON2_THSENS_INT_EN, data->ctrl_reg + LOONGSON2_THSENS_STATUS_REG);
  124. loongson2_thermal_set(data, 0, 0, false);
  125. for (i = 0; i <= LOONGSON2_MAX_SENSOR_SEL_NUM; i++) {
  126. tzd = devm_thermal_of_zone_register(dev, i, data, thermal_ops);
  127. if (!IS_ERR(tzd))
  128. break;
  129. if (PTR_ERR(tzd) != -ENODEV)
  130. continue;
  131. return dev_err_probe(dev, PTR_ERR(tzd), "failed to register");
  132. }
  133. ret = devm_request_threaded_irq(dev, irq, NULL, loongson2_thermal_irq_thread,
  134. IRQF_ONESHOT, "loongson2_thermal", tzd);
  135. if (ret < 0)
  136. return dev_err_probe(dev, ret, "failed to request alarm irq\n");
  137. devm_thermal_add_hwmon_sysfs(dev, tzd);
  138. return 0;
  139. }
  140. static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k1000_data = {
  141. .thermal_sensor_sel = 0,
  142. .flags = 0,
  143. };
  144. static const struct loongson2_thermal_chip_data loongson2_thermal_ls2k2000_data = {
  145. .thermal_sensor_sel = 0,
  146. .flags = LS2K2000_THSENS_OUT_FLAG,
  147. };
  148. static const struct of_device_id of_loongson2_thermal_match[] = {
  149. {
  150. .compatible = "loongson,ls2k1000-thermal",
  151. .data = &loongson2_thermal_ls2k1000_data,
  152. },
  153. {
  154. .compatible = "loongson,ls2k2000-thermal",
  155. .data = &loongson2_thermal_ls2k2000_data,
  156. },
  157. { /* end */ }
  158. };
  159. MODULE_DEVICE_TABLE(of, of_loongson2_thermal_match);
  160. static struct platform_driver loongson2_thermal_driver = {
  161. .driver = {
  162. .name = "loongson2_thermal",
  163. .of_match_table = of_loongson2_thermal_match,
  164. },
  165. .probe = loongson2_thermal_probe,
  166. };
  167. module_platform_driver(loongson2_thermal_driver);
  168. MODULE_DESCRIPTION("Loongson2 thermal driver");
  169. MODULE_AUTHOR("Loongson Technology Corporation Limited");
  170. MODULE_LICENSE("GPL");