thermal-generic-adc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic ADC thermal driver
  4. *
  5. * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  8. */
  9. #include <linux/iio/consumer.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/thermal.h>
  15. #include "thermal_hwmon.h"
  16. struct gadc_thermal_info {
  17. struct device *dev;
  18. struct thermal_zone_device *tz_dev;
  19. struct iio_channel *channel;
  20. s32 *lookup_table;
  21. int nlookup_table;
  22. };
  23. static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
  24. {
  25. int temp, temp_hi, temp_lo, adc_hi, adc_lo;
  26. int i;
  27. if (!gti->lookup_table)
  28. return val;
  29. for (i = 0; i < gti->nlookup_table; i++) {
  30. if (val >= gti->lookup_table[2 * i + 1])
  31. break;
  32. }
  33. if (i == 0) {
  34. temp = gti->lookup_table[0];
  35. } else if (i >= gti->nlookup_table) {
  36. temp = gti->lookup_table[2 * (gti->nlookup_table - 1)];
  37. } else {
  38. adc_hi = gti->lookup_table[2 * i - 1];
  39. adc_lo = gti->lookup_table[2 * i + 1];
  40. temp_hi = gti->lookup_table[2 * i - 2];
  41. temp_lo = gti->lookup_table[2 * i];
  42. temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi,
  43. adc_lo - adc_hi);
  44. }
  45. return temp;
  46. }
  47. static int gadc_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
  48. {
  49. struct gadc_thermal_info *gti = thermal_zone_device_priv(tz);
  50. int val;
  51. int ret;
  52. ret = iio_read_channel_processed(gti->channel, &val);
  53. if (ret < 0)
  54. return ret;
  55. *temp = gadc_thermal_adc_to_temp(gti, val);
  56. return 0;
  57. }
  58. static const struct thermal_zone_device_ops gadc_thermal_ops = {
  59. .get_temp = gadc_thermal_get_temp,
  60. };
  61. static int gadc_thermal_read_linear_lookup_table(struct device *dev,
  62. struct gadc_thermal_info *gti)
  63. {
  64. struct device_node *np = dev->of_node;
  65. enum iio_chan_type chan_type;
  66. int ntable;
  67. int ret;
  68. ntable = of_property_count_elems_of_size(np, "temperature-lookup-table",
  69. sizeof(u32));
  70. if (ntable <= 0) {
  71. ret = iio_get_channel_type(gti->channel, &chan_type);
  72. if (ret || chan_type != IIO_TEMP)
  73. dev_notice(dev,
  74. "no lookup table, assuming DAC channel returns milliCelcius\n");
  75. return 0;
  76. }
  77. if (ntable % 2) {
  78. dev_err(dev, "Pair of temperature vs ADC read value missing\n");
  79. return -EINVAL;
  80. }
  81. gti->lookup_table = devm_kcalloc(dev,
  82. ntable, sizeof(*gti->lookup_table),
  83. GFP_KERNEL);
  84. if (!gti->lookup_table)
  85. return -ENOMEM;
  86. ret = of_property_read_u32_array(np, "temperature-lookup-table",
  87. (u32 *)gti->lookup_table, ntable);
  88. if (ret < 0) {
  89. dev_err(dev, "Failed to read temperature lookup table: %d\n",
  90. ret);
  91. return ret;
  92. }
  93. gti->nlookup_table = ntable / 2;
  94. return 0;
  95. }
  96. static int gadc_thermal_probe(struct platform_device *pdev)
  97. {
  98. struct device *dev = &pdev->dev;
  99. struct gadc_thermal_info *gti;
  100. int ret;
  101. if (!dev->of_node) {
  102. dev_err(dev, "Only DT based supported\n");
  103. return -ENODEV;
  104. }
  105. gti = devm_kzalloc(dev, sizeof(*gti), GFP_KERNEL);
  106. if (!gti)
  107. return -ENOMEM;
  108. gti->channel = devm_iio_channel_get(dev, "sensor-channel");
  109. if (IS_ERR(gti->channel))
  110. return dev_err_probe(dev, PTR_ERR(gti->channel), "IIO channel not found\n");
  111. ret = gadc_thermal_read_linear_lookup_table(dev, gti);
  112. if (ret < 0)
  113. return ret;
  114. gti->dev = dev;
  115. gti->tz_dev = devm_thermal_of_zone_register(dev, 0, gti,
  116. &gadc_thermal_ops);
  117. if (IS_ERR(gti->tz_dev)) {
  118. ret = PTR_ERR(gti->tz_dev);
  119. if (ret != -EPROBE_DEFER)
  120. dev_err(dev,
  121. "Thermal zone sensor register failed: %d\n",
  122. ret);
  123. return ret;
  124. }
  125. devm_thermal_add_hwmon_sysfs(dev, gti->tz_dev);
  126. return 0;
  127. }
  128. static const struct of_device_id of_adc_thermal_match[] = {
  129. { .compatible = "generic-adc-thermal", },
  130. {},
  131. };
  132. MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
  133. static struct platform_driver gadc_thermal_driver = {
  134. .driver = {
  135. .name = "generic-adc-thermal",
  136. .of_match_table = of_adc_thermal_match,
  137. },
  138. .probe = gadc_thermal_probe,
  139. };
  140. module_platform_driver(gadc_thermal_driver);
  141. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  142. MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
  143. MODULE_LICENSE("GPL v2");