thermal-generic-adc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/iio/iio.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/thermal.h>
  16. #include "thermal_hwmon.h"
  17. struct gadc_thermal_info {
  18. struct device *dev;
  19. struct thermal_zone_device *tz_dev;
  20. struct iio_channel *channel;
  21. s32 *lookup_table;
  22. int nlookup_table;
  23. };
  24. static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
  25. {
  26. int temp, temp_hi, temp_lo, adc_hi, adc_lo;
  27. int i;
  28. if (!gti->lookup_table)
  29. return val;
  30. for (i = 0; i < gti->nlookup_table; i++) {
  31. if (val >= gti->lookup_table[2 * i + 1])
  32. break;
  33. }
  34. if (i == 0) {
  35. temp = gti->lookup_table[0];
  36. } else if (i >= gti->nlookup_table) {
  37. temp = gti->lookup_table[2 * (gti->nlookup_table - 1)];
  38. } else {
  39. adc_hi = gti->lookup_table[2 * i - 1];
  40. adc_lo = gti->lookup_table[2 * i + 1];
  41. temp_hi = gti->lookup_table[2 * i - 2];
  42. temp_lo = gti->lookup_table[2 * i];
  43. temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi,
  44. adc_lo - adc_hi);
  45. }
  46. return temp;
  47. }
  48. static int gadc_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
  49. {
  50. struct gadc_thermal_info *gti = thermal_zone_device_priv(tz);
  51. int val;
  52. int ret;
  53. ret = iio_read_channel_processed(gti->channel, &val);
  54. if (ret < 0)
  55. return ret;
  56. *temp = gadc_thermal_adc_to_temp(gti, val);
  57. return 0;
  58. }
  59. static const struct thermal_zone_device_ops gadc_thermal_ops = {
  60. .get_temp = gadc_thermal_get_temp,
  61. };
  62. static const struct iio_chan_spec gadc_thermal_iio_channels[] = {
  63. {
  64. .type = IIO_TEMP,
  65. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  66. }
  67. };
  68. static int gadc_thermal_read_raw(struct iio_dev *indio_dev,
  69. struct iio_chan_spec const *chan,
  70. int *val, int *val2, long mask)
  71. {
  72. struct gadc_thermal_info *gtinfo = iio_priv(indio_dev);
  73. int ret;
  74. switch (mask) {
  75. case IIO_CHAN_INFO_PROCESSED:
  76. ret = gadc_thermal_get_temp(gtinfo->tz_dev, val);
  77. if (ret)
  78. return ret;
  79. return IIO_VAL_INT;
  80. default:
  81. return -EINVAL;
  82. }
  83. }
  84. static const struct iio_info gadc_thermal_iio_info = {
  85. .read_raw = gadc_thermal_read_raw,
  86. };
  87. static int gadc_iio_register(struct device *dev, struct gadc_thermal_info *gti)
  88. {
  89. struct gadc_thermal_info *gtinfo;
  90. struct iio_dev *indio_dev;
  91. indio_dev = devm_iio_device_alloc(dev, sizeof(*gtinfo));
  92. if (!indio_dev)
  93. return -ENOMEM;
  94. gtinfo = iio_priv(indio_dev);
  95. memcpy(gtinfo, gti, sizeof(*gtinfo));
  96. indio_dev->name = dev_name(dev);
  97. indio_dev->info = &gadc_thermal_iio_info;
  98. indio_dev->modes = INDIO_DIRECT_MODE;
  99. indio_dev->channels = gadc_thermal_iio_channels;
  100. indio_dev->num_channels = ARRAY_SIZE(gadc_thermal_iio_channels);
  101. return devm_iio_device_register(dev, indio_dev);
  102. }
  103. static int gadc_thermal_read_linear_lookup_table(struct device *dev,
  104. struct gadc_thermal_info *gti)
  105. {
  106. struct device_node *np = dev->of_node;
  107. enum iio_chan_type chan_type;
  108. int ntable;
  109. int ret;
  110. ntable = of_property_count_elems_of_size(np, "temperature-lookup-table",
  111. sizeof(u32));
  112. if (ntable <= 0) {
  113. ret = iio_get_channel_type(gti->channel, &chan_type);
  114. if (ret || chan_type != IIO_TEMP)
  115. dev_notice(dev,
  116. "no lookup table, assuming DAC channel returns milliCelcius\n");
  117. return 0;
  118. }
  119. if (ntable % 2) {
  120. dev_err(dev, "Pair of temperature vs ADC read value missing\n");
  121. return -EINVAL;
  122. }
  123. gti->lookup_table = devm_kcalloc(dev,
  124. ntable, sizeof(*gti->lookup_table),
  125. GFP_KERNEL);
  126. if (!gti->lookup_table)
  127. return -ENOMEM;
  128. ret = of_property_read_u32_array(np, "temperature-lookup-table",
  129. (u32 *)gti->lookup_table, ntable);
  130. if (ret < 0) {
  131. dev_err(dev, "Failed to read temperature lookup table: %d\n",
  132. ret);
  133. return ret;
  134. }
  135. gti->nlookup_table = ntable / 2;
  136. return 0;
  137. }
  138. static int gadc_thermal_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. struct gadc_thermal_info *gti;
  142. int ret;
  143. if (!dev->of_node) {
  144. dev_err(dev, "Only DT based supported\n");
  145. return -ENODEV;
  146. }
  147. gti = devm_kzalloc(dev, sizeof(*gti), GFP_KERNEL);
  148. if (!gti)
  149. return -ENOMEM;
  150. gti->channel = devm_iio_channel_get(dev, "sensor-channel");
  151. if (IS_ERR(gti->channel))
  152. return dev_err_probe(dev, PTR_ERR(gti->channel), "IIO channel not found\n");
  153. ret = gadc_thermal_read_linear_lookup_table(dev, gti);
  154. if (ret < 0)
  155. return ret;
  156. gti->dev = dev;
  157. gti->tz_dev = devm_thermal_of_zone_register(dev, 0, gti,
  158. &gadc_thermal_ops);
  159. if (IS_ERR(gti->tz_dev)) {
  160. ret = PTR_ERR(gti->tz_dev);
  161. if (ret != -EPROBE_DEFER)
  162. dev_err(dev,
  163. "Thermal zone sensor register failed: %d\n",
  164. ret);
  165. return ret;
  166. }
  167. devm_thermal_add_hwmon_sysfs(dev, gti->tz_dev);
  168. return gadc_iio_register(&pdev->dev, gti);
  169. }
  170. static const struct of_device_id of_adc_thermal_match[] = {
  171. { .compatible = "generic-adc-thermal", },
  172. {},
  173. };
  174. MODULE_DEVICE_TABLE(of, of_adc_thermal_match);
  175. static struct platform_driver gadc_thermal_driver = {
  176. .driver = {
  177. .name = "generic-adc-thermal",
  178. .of_match_table = of_adc_thermal_match,
  179. },
  180. .probe = gadc_thermal_probe,
  181. };
  182. module_platform_driver(gadc_thermal_driver);
  183. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  184. MODULE_DESCRIPTION("Generic ADC thermal driver using IIO framework with DT");
  185. MODULE_LICENSE("GPL v2");