tsens.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. .compatible = "qcom,tsens-v2",
  63. .data = &data_tsens_v2,
  64. },
  65. {}
  66. };
  67. MODULE_DEVICE_TABLE(of, tsens_table);
  68. static const struct thermal_zone_of_device_ops tsens_of_ops = {
  69. .get_temp = tsens_get_temp,
  70. .get_trend = tsens_get_trend,
  71. };
  72. static int tsens_register(struct tsens_device *tmdev)
  73. {
  74. int i;
  75. struct thermal_zone_device *tzd;
  76. u32 *hw_id, n = tmdev->num_sensors;
  77. hw_id = devm_kcalloc(tmdev->dev, n, sizeof(u32), GFP_KERNEL);
  78. if (!hw_id)
  79. return -ENOMEM;
  80. for (i = 0; i < tmdev->num_sensors; i++) {
  81. tmdev->sensor[i].tmdev = tmdev;
  82. tmdev->sensor[i].id = i;
  83. tzd = devm_thermal_zone_of_sensor_register(tmdev->dev, i,
  84. &tmdev->sensor[i],
  85. &tsens_of_ops);
  86. if (IS_ERR(tzd))
  87. continue;
  88. tmdev->sensor[i].tzd = tzd;
  89. if (tmdev->ops->enable)
  90. tmdev->ops->enable(tmdev, i);
  91. }
  92. return 0;
  93. }
  94. static int tsens_probe(struct platform_device *pdev)
  95. {
  96. int ret, i;
  97. struct device *dev;
  98. struct device_node *np;
  99. struct tsens_device *tmdev;
  100. const struct tsens_data *data;
  101. const struct of_device_id *id;
  102. u32 num_sensors;
  103. if (pdev->dev.of_node)
  104. dev = &pdev->dev;
  105. else
  106. dev = pdev->dev.parent;
  107. np = dev->of_node;
  108. id = of_match_node(tsens_table, np);
  109. if (id)
  110. data = id->data;
  111. else
  112. data = &data_8960;
  113. num_sensors = data->num_sensors;
  114. if (np)
  115. of_property_read_u32(np, "#qcom,sensors", &num_sensors);
  116. if (num_sensors <= 0) {
  117. dev_err(dev, "invalid number of sensors\n");
  118. return -EINVAL;
  119. }
  120. tmdev = devm_kzalloc(dev,
  121. struct_size(tmdev, sensor, num_sensors),
  122. GFP_KERNEL);
  123. if (!tmdev)
  124. return -ENOMEM;
  125. tmdev->dev = dev;
  126. tmdev->num_sensors = num_sensors;
  127. tmdev->ops = data->ops;
  128. for (i = 0; i < tmdev->num_sensors; i++) {
  129. if (data->hw_ids)
  130. tmdev->sensor[i].hw_id = data->hw_ids[i];
  131. else
  132. tmdev->sensor[i].hw_id = i;
  133. }
  134. if (!tmdev->ops || !tmdev->ops->init || !tmdev->ops->get_temp)
  135. return -EINVAL;
  136. ret = tmdev->ops->init(tmdev);
  137. if (ret < 0) {
  138. dev_err(dev, "tsens init failed\n");
  139. return ret;
  140. }
  141. if (tmdev->ops->calibrate) {
  142. ret = tmdev->ops->calibrate(tmdev);
  143. if (ret < 0) {
  144. if (ret != -EPROBE_DEFER)
  145. dev_err(dev, "tsens calibration failed\n");
  146. return ret;
  147. }
  148. }
  149. ret = tsens_register(tmdev);
  150. platform_set_drvdata(pdev, tmdev);
  151. return ret;
  152. }
  153. static int tsens_remove(struct platform_device *pdev)
  154. {
  155. struct tsens_device *tmdev = platform_get_drvdata(pdev);
  156. if (tmdev->ops->disable)
  157. tmdev->ops->disable(tmdev);
  158. return 0;
  159. }
  160. static struct platform_driver tsens_driver = {
  161. .probe = tsens_probe,
  162. .remove = tsens_remove,
  163. .driver = {
  164. .name = "qcom-tsens",
  165. .pm = &tsens_pm_ops,
  166. .of_match_table = tsens_table,
  167. },
  168. };
  169. module_platform_driver(tsens_driver);
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
  172. MODULE_ALIAS("platform:qcom-tsens");