thermal-generic-adc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, 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 - 1)) {
  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 = gti->lookup_table[2 * i];
  41. temp -= ((val - adc_lo) * 1000) / (adc_hi - adc_lo);
  42. }
  43. return temp;
  44. }
  45. static int gadc_thermal_get_temp(void *data, int *temp)
  46. {
  47. struct gadc_thermal_info *gti = data;
  48. int val;
  49. int ret;
  50. ret = iio_read_channel_processed(gti->channel, &val);
  51. if (ret < 0) {
  52. dev_err(gti->dev, "IIO channel read failed %d\n", ret);
  53. return ret;
  54. }
  55. *temp = gadc_thermal_adc_to_temp(gti, val);
  56. return 0;
  57. }
  58. static const struct thermal_zone_of_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. int ntable;
  66. int ret;
  67. ntable = of_property_count_elems_of_size(np, "temperature-lookup-table",
  68. sizeof(u32));
  69. if (ntable < 0) {
  70. dev_err(dev, "Lookup table is not provided\n");
  71. return ntable;
  72. }
  73. if (ntable % 2) {
  74. dev_err(dev, "Pair of temperature vs ADC read value missing\n");
  75. return -EINVAL;
  76. }
  77. gti->lookup_table = devm_kzalloc(dev, sizeof(*gti->lookup_table) *
  78. ntable, GFP_KERNEL);
  79. if (!gti->lookup_table)
  80. return -ENOMEM;
  81. ret = of_property_read_u32_array(np, "temperature-lookup-table",
  82. (u32 *)gti->lookup_table, ntable);
  83. if (ret < 0) {
  84. dev_err(dev, "Failed to read temperature lookup table: %d\n",
  85. ret);
  86. return ret;
  87. }
  88. gti->nlookup_table = ntable / 2;
  89. return 0;
  90. }
  91. static int gadc_thermal_probe(struct platform_device *pdev)
  92. {
  93. struct gadc_thermal_info *gti;
  94. int ret;
  95. if (!pdev->dev.of_node) {
  96. dev_err(&pdev->dev, "Only DT based supported\n");
  97. return -ENODEV;
  98. }
  99. gti = devm_kzalloc(&pdev->dev, sizeof(*gti), GFP_KERNEL);
  100. if (!gti)
  101. return -ENOMEM;
  102. ret = gadc_thermal_read_linear_lookup_table(&pdev->dev, gti);
  103. if (ret < 0)
  104. return ret;
  105. gti->dev = &pdev->dev;
  106. platform_set_drvdata(pdev, gti);
  107. gti->channel = iio_channel_get(&pdev->dev, "sensor-channel");
  108. if (IS_ERR(gti->channel)) {
  109. ret = PTR_ERR(gti->channel);
  110. dev_err(&pdev->dev, "IIO channel not found: %d\n", ret);
  111. return ret;
  112. }
  113. gti->tz_dev = thermal_zone_of_sensor_register(&pdev->dev, 0,
  114. gti, &gadc_thermal_ops);
  115. if (IS_ERR(gti->tz_dev)) {
  116. ret = PTR_ERR(gti->tz_dev);
  117. dev_err(&pdev->dev, "Thermal zone sensor register failed: %d\n",
  118. ret);
  119. goto sensor_fail;
  120. }
  121. return 0;
  122. sensor_fail:
  123. iio_channel_release(gti->channel);
  124. return ret;
  125. }
  126. static int gadc_thermal_remove(struct platform_device *pdev)
  127. {
  128. struct gadc_thermal_info *gti = platform_get_drvdata(pdev);
  129. thermal_zone_of_sensor_unregister(&pdev->dev, gti->tz_dev);
  130. iio_channel_release(gti->channel);
  131. return 0;
  132. }
  133. static const struct of_device_id of_adc_thermal_match[] = {
  134. { .compatible = "generic-adc-thermal", },
  135. {},
  136. };
  137. MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
  138. static struct platform_driver gadc_thermal_driver = {
  139. .driver = {
  140. .name = "generic-adc-thermal",
  141. .of_match_table = of_adc_thermal_match,
  142. },
  143. .probe = gadc_thermal_probe,
  144. .remove = gadc_thermal_remove,
  145. };
  146. module_platform_driver(gadc_thermal_driver);
  147. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  148. MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
  149. MODULE_LICENSE("GPL v2");