ltq-cputemp.c 2.9 KB

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