thermal-generic-adc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Generic ADC thermal driver
  3. *
  4. * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/iio/consumer.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/thermal.h>
  18. struct gadc_thermal_info {
  19. struct device *dev;
  20. struct thermal_zone_device *tz_dev;
  21. struct iio_channel *channel;
  22. s32 *lookup_table;
  23. int nlookup_table;
  24. };
  25. static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
  26. {
  27. int temp, temp_hi, temp_lo, adc_hi, adc_lo;
  28. int i;
  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(void *data, int *temp)
  48. {
  49. struct gadc_thermal_info *gti = data;
  50. int val;
  51. int ret;
  52. ret = iio_read_channel_processed(gti->channel, &val);
  53. if (ret < 0) {
  54. dev_err(gti->dev, "IIO channel read failed %d\n", ret);
  55. return ret;
  56. }
  57. *temp = gadc_thermal_adc_to_temp(gti, val);
  58. return 0;
  59. }
  60. static const struct thermal_zone_of_device_ops gadc_thermal_ops = {
  61. .get_temp = gadc_thermal_get_temp,
  62. };
  63. static int gadc_thermal_read_linear_lookup_table(struct device *dev,
  64. struct gadc_thermal_info *gti)
  65. {
  66. struct device_node *np = dev->of_node;
  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. dev_err(dev, "Lookup table is not provided\n");
  73. return ntable;
  74. }
  75. if (ntable % 2) {
  76. dev_err(dev, "Pair of temperature vs ADC read value missing\n");
  77. return -EINVAL;
  78. }
  79. gti->lookup_table = devm_kzalloc(dev, sizeof(*gti->lookup_table) *
  80. ntable, GFP_KERNEL);
  81. if (!gti->lookup_table)
  82. return -ENOMEM;
  83. ret = of_property_read_u32_array(np, "temperature-lookup-table",
  84. (u32 *)gti->lookup_table, ntable);
  85. if (ret < 0) {
  86. dev_err(dev, "Failed to read temperature lookup table: %d\n",
  87. ret);
  88. return ret;
  89. }
  90. gti->nlookup_table = ntable / 2;
  91. return 0;
  92. }
  93. static int gadc_thermal_probe(struct platform_device *pdev)
  94. {
  95. struct gadc_thermal_info *gti;
  96. int ret;
  97. if (!pdev->dev.of_node) {
  98. dev_err(&pdev->dev, "Only DT based supported\n");
  99. return -ENODEV;
  100. }
  101. gti = devm_kzalloc(&pdev->dev, sizeof(*gti), GFP_KERNEL);
  102. if (!gti)
  103. return -ENOMEM;
  104. ret = gadc_thermal_read_linear_lookup_table(&pdev->dev, gti);
  105. if (ret < 0)
  106. return ret;
  107. gti->dev = &pdev->dev;
  108. platform_set_drvdata(pdev, gti);
  109. gti->channel = iio_channel_get(&pdev->dev, "sensor-channel");
  110. if (IS_ERR(gti->channel)) {
  111. ret = PTR_ERR(gti->channel);
  112. dev_err(&pdev->dev, "IIO channel not found: %d\n", ret);
  113. return ret;
  114. }
  115. gti->tz_dev = thermal_zone_of_sensor_register(&pdev->dev, 0,
  116. gti, &gadc_thermal_ops);
  117. if (IS_ERR(gti->tz_dev)) {
  118. ret = PTR_ERR(gti->tz_dev);
  119. dev_err(&pdev->dev, "Thermal zone sensor register failed: %d\n",
  120. ret);
  121. goto sensor_fail;
  122. }
  123. return 0;
  124. sensor_fail:
  125. iio_channel_release(gti->channel);
  126. return ret;
  127. }
  128. static int gadc_thermal_remove(struct platform_device *pdev)
  129. {
  130. struct gadc_thermal_info *gti = platform_get_drvdata(pdev);
  131. thermal_zone_of_sensor_unregister(&pdev->dev, gti->tz_dev);
  132. iio_channel_release(gti->channel);
  133. return 0;
  134. }
  135. static const struct of_device_id of_adc_thermal_match[] = {
  136. { .compatible = "generic-adc-thermal", },
  137. {},
  138. };
  139. MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
  140. static struct platform_driver gadc_thermal_driver = {
  141. .driver = {
  142. .name = "generic-adc-thermal",
  143. .of_match_table = of_adc_thermal_match,
  144. },
  145. .probe = gadc_thermal_probe,
  146. .remove = gadc_thermal_remove,
  147. };
  148. module_platform_driver(gadc_thermal_driver);
  149. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  150. MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
  151. MODULE_LICENSE("GPL v2");