thermal-generic-adc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_kcalloc(dev,
  78. ntable, sizeof(*gti->lookup_table),
  79. GFP_KERNEL);
  80. if (!gti->lookup_table)
  81. return -ENOMEM;
  82. ret = of_property_read_u32_array(np, "temperature-lookup-table",
  83. (u32 *)gti->lookup_table, ntable);
  84. if (ret < 0) {
  85. dev_err(dev, "Failed to read temperature lookup table: %d\n",
  86. ret);
  87. return ret;
  88. }
  89. gti->nlookup_table = ntable / 2;
  90. return 0;
  91. }
  92. static int gadc_thermal_probe(struct platform_device *pdev)
  93. {
  94. struct gadc_thermal_info *gti;
  95. int ret;
  96. if (!pdev->dev.of_node) {
  97. dev_err(&pdev->dev, "Only DT based supported\n");
  98. return -ENODEV;
  99. }
  100. gti = devm_kzalloc(&pdev->dev, sizeof(*gti), GFP_KERNEL);
  101. if (!gti)
  102. return -ENOMEM;
  103. ret = gadc_thermal_read_linear_lookup_table(&pdev->dev, gti);
  104. if (ret < 0)
  105. return ret;
  106. gti->dev = &pdev->dev;
  107. platform_set_drvdata(pdev, gti);
  108. gti->channel = devm_iio_channel_get(&pdev->dev, "sensor-channel");
  109. if (IS_ERR(gti->channel)) {
  110. ret = PTR_ERR(gti->channel);
  111. dev_err(&pdev->dev, "IIO channel not found: %d\n", ret);
  112. return ret;
  113. }
  114. gti->tz_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, gti,
  115. &gadc_thermal_ops);
  116. if (IS_ERR(gti->tz_dev)) {
  117. ret = PTR_ERR(gti->tz_dev);
  118. dev_err(&pdev->dev, "Thermal zone sensor register failed: %d\n",
  119. ret);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. static const struct of_device_id of_adc_thermal_match[] = {
  125. { .compatible = "generic-adc-thermal", },
  126. {},
  127. };
  128. MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
  129. static struct platform_driver gadc_thermal_driver = {
  130. .driver = {
  131. .name = "generic-adc-thermal",
  132. .of_match_table = of_adc_thermal_match,
  133. },
  134. .probe = gadc_thermal_probe,
  135. };
  136. module_platform_driver(gadc_thermal_driver);
  137. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  138. MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
  139. MODULE_LICENSE("GPL v2");