ltq-cputemp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Lantiq cpu temperature sensor driver
  2. *
  3. * Copyright (C) 2017 Florian Eckert <fe@dev.tdt.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version
  9. *
  10. * This program is distributed in the hope that it will be useful
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/delay.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <lantiq_soc.h>
  26. /* gphy1 configuration register contains cpu temperature */
  27. #define CGU_GPHY1_CR 0x0040
  28. #define CGU_TEMP_PD BIT(19)
  29. static void ltq_cputemp_enable(void)
  30. {
  31. ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) | CGU_TEMP_PD, CGU_GPHY1_CR);
  32. }
  33. static void ltq_cputemp_disable(void *data)
  34. {
  35. ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) & ~CGU_TEMP_PD, CGU_GPHY1_CR);
  36. }
  37. static int ltq_read(struct device *dev, enum hwmon_sensor_types type,
  38. u32 attr, int channel, long *temp)
  39. {
  40. int value;
  41. switch (attr) {
  42. case hwmon_temp_input:
  43. /* get the temperature including one decimal place */
  44. value = (ltq_cgu_r32(CGU_GPHY1_CR) >> 9) & 0x01FF;
  45. value = value * 5;
  46. /* range -38 to +154 °C, register value zero is -38.0 °C */
  47. value -= 380;
  48. /* scale temp to millidegree */
  49. value = value * 100;
  50. break;
  51. default:
  52. return -EOPNOTSUPP;
  53. }
  54. *temp = value;
  55. return 0;
  56. }
  57. static umode_t ltq_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 u32 ltq_chip_config[] = {
  70. HWMON_C_REGISTER_TZ,
  71. 0
  72. };
  73. static const struct hwmon_channel_info ltq_chip = {
  74. .type = hwmon_chip,
  75. .config = ltq_chip_config,
  76. };
  77. static const u32 ltq_temp_config[] = {
  78. HWMON_T_INPUT,
  79. 0
  80. };
  81. static const struct hwmon_channel_info ltq_temp = {
  82. .type = hwmon_temp,
  83. .config = ltq_temp_config,
  84. };
  85. static const struct hwmon_channel_info *ltq_info[] = {
  86. &ltq_chip,
  87. &ltq_temp,
  88. NULL
  89. };
  90. static const struct hwmon_ops ltq_hwmon_ops = {
  91. .is_visible = ltq_is_visible,
  92. .read = ltq_read,
  93. };
  94. static const struct hwmon_chip_info ltq_chip_info = {
  95. .ops = &ltq_hwmon_ops,
  96. .info = ltq_info,
  97. };
  98. static int ltq_cputemp_probe(struct platform_device *pdev)
  99. {
  100. struct device *hwmon_dev;
  101. int err = 0;
  102. /* available on vr9 v1.2 SoCs only */
  103. if (ltq_soc_type() != SOC_TYPE_VR9_2)
  104. return -ENODEV;
  105. err = devm_add_action(&pdev->dev, ltq_cputemp_disable, NULL);
  106. if (err)
  107. return err;
  108. ltq_cputemp_enable();
  109. hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
  110. "ltq_cputemp",
  111. NULL,
  112. &ltq_chip_info,
  113. NULL);
  114. if (IS_ERR(hwmon_dev)) {
  115. dev_err(&pdev->dev, "Failed to register as hwmon device");
  116. return PTR_ERR(hwmon_dev);
  117. }
  118. return 0;
  119. }
  120. const struct of_device_id ltq_cputemp_match[] = {
  121. { .compatible = "lantiq,cputemp" },
  122. {},
  123. };
  124. MODULE_DEVICE_TABLE(of, ltq_cputemp_match);
  125. static struct platform_driver ltq_cputemp_driver = {
  126. .probe = ltq_cputemp_probe,
  127. .driver = {
  128. .name = "ltq-cputemp",
  129. .of_match_table = ltq_cputemp_match,
  130. },
  131. };
  132. module_platform_driver(ltq_cputemp_driver);
  133. MODULE_AUTHOR("Florian Eckert <fe@dev.tdt.de>");
  134. MODULE_DESCRIPTION("Lantiq cpu temperature sensor driver");
  135. MODULE_LICENSE("GPL");