tsens.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pm.h>
  10. #include <linux/slab.h>
  11. #include <linux/thermal.h>
  12. #include "tsens.h"
  13. static int tsens_get_temp(void *data, int *temp)
  14. {
  15. const struct tsens_sensor *s = data;
  16. struct tsens_device *tmdev = s->tmdev;
  17. return tmdev->ops->get_temp(tmdev, s->id, temp);
  18. }
  19. static int tsens_get_trend(void *p, int trip, enum thermal_trend *trend)
  20. {
  21. const struct tsens_sensor *s = p;
  22. struct tsens_device *tmdev = s->tmdev;
  23. if (tmdev->ops->get_trend)
  24. return tmdev->ops->get_trend(tmdev, s->id, trend);
  25. return -ENOTSUPP;
  26. }
  27. static int __maybe_unused tsens_suspend(struct device *dev)
  28. {
  29. struct tsens_device *tmdev = dev_get_drvdata(dev);
  30. if (tmdev->ops && tmdev->ops->suspend)
  31. return tmdev->ops->suspend(tmdev);
  32. return 0;
  33. }
  34. static int __maybe_unused tsens_resume(struct device *dev)
  35. {
  36. struct tsens_device *tmdev = dev_get_drvdata(dev);
  37. if (tmdev->ops && tmdev->ops->resume)
  38. return tmdev->ops->resume(tmdev);
  39. return 0;
  40. }
  41. static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
  42. static const struct of_device_id tsens_table[] = {
  43. {
  44. .compatible = "qcom,msm8916-tsens",
  45. .data = &data_8916,
  46. }, {
  47. .compatible = "qcom,msm8974-tsens",
  48. .data = &data_8974,
  49. }, {
  50. .compatible = "qcom,msm8996-tsens",
  51. .data = &data_8996,
  52. }, {
  53. .compatible = "qcom,tsens-v2",
  54. .data = &data_tsens_v2,
  55. },
  56. {}
  57. };
  58. MODULE_DEVICE_TABLE(of, tsens_table);
  59. static const struct thermal_zone_of_device_ops tsens_of_ops = {
  60. .get_temp = tsens_get_temp,
  61. .get_trend = tsens_get_trend,
  62. };
  63. static int tsens_register(struct tsens_device *tmdev)
  64. {
  65. int i;
  66. struct thermal_zone_device *tzd;
  67. for (i = 0; i < tmdev->num_sensors; i++) {
  68. tmdev->sensor[i].tmdev = tmdev;
  69. tmdev->sensor[i].id = i;
  70. tzd = devm_thermal_zone_of_sensor_register(tmdev->dev, i,
  71. &tmdev->sensor[i],
  72. &tsens_of_ops);
  73. if (IS_ERR(tzd))
  74. continue;
  75. tmdev->sensor[i].tzd = tzd;
  76. if (tmdev->ops->enable)
  77. tmdev->ops->enable(tmdev, i);
  78. }
  79. return 0;
  80. }
  81. static int tsens_probe(struct platform_device *pdev)
  82. {
  83. int ret, i;
  84. struct device *dev;
  85. struct device_node *np;
  86. struct tsens_device *tmdev;
  87. const struct tsens_data *data;
  88. const struct of_device_id *id;
  89. u32 num_sensors;
  90. if (pdev->dev.of_node)
  91. dev = &pdev->dev;
  92. else
  93. dev = pdev->dev.parent;
  94. np = dev->of_node;
  95. id = of_match_node(tsens_table, np);
  96. if (id)
  97. data = id->data;
  98. else
  99. data = &data_8960;
  100. num_sensors = data->num_sensors;
  101. if (np)
  102. of_property_read_u32(np, "#qcom,sensors", &num_sensors);
  103. if (num_sensors <= 0) {
  104. dev_err(dev, "invalid number of sensors\n");
  105. return -EINVAL;
  106. }
  107. tmdev = devm_kzalloc(dev,
  108. struct_size(tmdev, sensor, num_sensors),
  109. GFP_KERNEL);
  110. if (!tmdev)
  111. return -ENOMEM;
  112. tmdev->dev = dev;
  113. tmdev->num_sensors = num_sensors;
  114. tmdev->ops = data->ops;
  115. for (i = 0; i < tmdev->num_sensors; i++) {
  116. if (data->hw_ids)
  117. tmdev->sensor[i].hw_id = data->hw_ids[i];
  118. else
  119. tmdev->sensor[i].hw_id = i;
  120. }
  121. for (i = 0; i < REG_ARRAY_SIZE; i++) {
  122. tmdev->reg_offsets[i] = data->reg_offsets[i];
  123. }
  124. if (!tmdev->ops || !tmdev->ops->init || !tmdev->ops->get_temp)
  125. return -EINVAL;
  126. ret = tmdev->ops->init(tmdev);
  127. if (ret < 0) {
  128. dev_err(dev, "tsens init failed\n");
  129. return ret;
  130. }
  131. if (tmdev->ops->calibrate) {
  132. ret = tmdev->ops->calibrate(tmdev);
  133. if (ret < 0) {
  134. dev_err(dev, "tsens calibration failed\n");
  135. return ret;
  136. }
  137. }
  138. ret = tsens_register(tmdev);
  139. platform_set_drvdata(pdev, tmdev);
  140. return ret;
  141. }
  142. static int tsens_remove(struct platform_device *pdev)
  143. {
  144. struct tsens_device *tmdev = platform_get_drvdata(pdev);
  145. if (tmdev->ops->disable)
  146. tmdev->ops->disable(tmdev);
  147. return 0;
  148. }
  149. static struct platform_driver tsens_driver = {
  150. .probe = tsens_probe,
  151. .remove = tsens_remove,
  152. .driver = {
  153. .name = "qcom-tsens",
  154. .pm = &tsens_pm_ops,
  155. .of_match_table = tsens_table,
  156. },
  157. };
  158. module_platform_driver(tsens_driver);
  159. MODULE_LICENSE("GPL v2");
  160. MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
  161. MODULE_ALIAS("platform:qcom-tsens");