thermal-generic-adc.c 4.1 KB

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