tsens.c 4.4 KB

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