ltq-cputemp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Lantiq cpu temperature sensor driver
  3. *
  4. * Copyright (C) 2017 Florian Eckert <fe@dev.tdt.de>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/delay.h>
  8. #include <linux/hwmon.h>
  9. #include <linux/hwmon-sysfs.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <lantiq_soc.h>
  14. /* gphy1 configuration register contains cpu temperature */
  15. #define CGU_GPHY1_CR 0x0040
  16. #define CGU_TEMP_PD BIT(19)
  17. static void ltq_cputemp_enable(void)
  18. {
  19. ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) | CGU_TEMP_PD, CGU_GPHY1_CR);
  20. }
  21. static void ltq_cputemp_disable(void *data)
  22. {
  23. ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) & ~CGU_TEMP_PD, CGU_GPHY1_CR);
  24. }
  25. static int ltq_read(struct device *dev, enum hwmon_sensor_types type,
  26. u32 attr, int channel, long *temp)
  27. {
  28. int value;
  29. switch (attr) {
  30. case hwmon_temp_input:
  31. /* get the temperature including one decimal place */
  32. value = (ltq_cgu_r32(CGU_GPHY1_CR) >> 9) & 0x01FF;
  33. value = value * 5;
  34. /* range -38 to +154 °C, register value zero is -38.0 °C */
  35. value -= 380;
  36. /* scale temp to millidegree */
  37. value = value * 100;
  38. break;
  39. default:
  40. return -EOPNOTSUPP;
  41. }
  42. *temp = value;
  43. return 0;
  44. }
  45. static umode_t ltq_is_visible(const void *_data, enum hwmon_sensor_types type,
  46. u32 attr, int channel)
  47. {
  48. if (type != hwmon_temp)
  49. return 0;
  50. switch (attr) {
  51. case hwmon_temp_input:
  52. return 0444;
  53. default:
  54. return 0;
  55. }
  56. }
  57. static const struct hwmon_channel_info *ltq_info[] = {
  58. HWMON_CHANNEL_INFO(chip,
  59. HWMON_C_REGISTER_TZ),
  60. HWMON_CHANNEL_INFO(temp,
  61. HWMON_T_INPUT),
  62. NULL
  63. };
  64. static const struct hwmon_ops ltq_hwmon_ops = {
  65. .is_visible = ltq_is_visible,
  66. .read = ltq_read,
  67. };
  68. static const struct hwmon_chip_info ltq_chip_info = {
  69. .ops = &ltq_hwmon_ops,
  70. .info = ltq_info,
  71. };
  72. static int ltq_cputemp_probe(struct platform_device *pdev)
  73. {
  74. struct device *hwmon_dev;
  75. int err = 0;
  76. /* available on vr9 v1.2 SoCs only */
  77. if (ltq_soc_type() != SOC_TYPE_VR9_2)
  78. return -ENODEV;
  79. err = devm_add_action(&pdev->dev, ltq_cputemp_disable, NULL);
  80. if (err)
  81. return err;
  82. ltq_cputemp_enable();
  83. hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
  84. "ltq_cputemp",
  85. NULL,
  86. &ltq_chip_info,
  87. NULL);
  88. if (IS_ERR(hwmon_dev)) {
  89. dev_err(&pdev->dev, "Failed to register as hwmon device");
  90. return PTR_ERR(hwmon_dev);
  91. }
  92. return 0;
  93. }
  94. const struct of_device_id ltq_cputemp_match[] = {
  95. { .compatible = "lantiq,cputemp" },
  96. {},
  97. };
  98. MODULE_DEVICE_TABLE(of, ltq_cputemp_match);
  99. static struct platform_driver ltq_cputemp_driver = {
  100. .probe = ltq_cputemp_probe,
  101. .driver = {
  102. .name = "ltq-cputemp",
  103. .of_match_table = ltq_cputemp_match,
  104. },
  105. };
  106. module_platform_driver(ltq_cputemp_driver);
  107. MODULE_AUTHOR("Florian Eckert <fe@dev.tdt.de>");
  108. MODULE_DESCRIPTION("Lantiq cpu temperature sensor driver");
  109. MODULE_LICENSE("GPL");