imx_sc_thermal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018-2020 NXP.
  4. */
  5. #include <dt-bindings/firmware/imx/rsrc.h>
  6. #include <linux/err.h>
  7. #include <linux/firmware/imx/sci.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/thermal.h>
  14. #include "thermal_core.h"
  15. #include "thermal_hwmon.h"
  16. #define IMX_SC_MISC_FUNC_GET_TEMP 13
  17. static struct imx_sc_ipc *thermal_ipc_handle;
  18. struct imx_sc_sensor {
  19. struct thermal_zone_device *tzd;
  20. u32 resource_id;
  21. };
  22. struct req_get_temp {
  23. u16 resource_id;
  24. u8 type;
  25. } __packed __aligned(4);
  26. struct resp_get_temp {
  27. s16 celsius;
  28. s8 tenths;
  29. } __packed __aligned(4);
  30. struct imx_sc_msg_misc_get_temp {
  31. struct imx_sc_rpc_msg hdr;
  32. union {
  33. struct req_get_temp req;
  34. struct resp_get_temp resp;
  35. } data;
  36. } __packed __aligned(4);
  37. static int imx_sc_thermal_get_temp(void *data, int *temp)
  38. {
  39. struct imx_sc_msg_misc_get_temp msg;
  40. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  41. struct imx_sc_sensor *sensor = data;
  42. int ret;
  43. msg.data.req.resource_id = sensor->resource_id;
  44. msg.data.req.type = IMX_SC_C_TEMP;
  45. hdr->ver = IMX_SC_RPC_VERSION;
  46. hdr->svc = IMX_SC_RPC_SVC_MISC;
  47. hdr->func = IMX_SC_MISC_FUNC_GET_TEMP;
  48. hdr->size = 2;
  49. ret = imx_scu_call_rpc(thermal_ipc_handle, &msg, true);
  50. if (ret) {
  51. dev_err(&sensor->tzd->device, "read temp sensor %d failed, ret %d\n",
  52. sensor->resource_id, ret);
  53. return ret;
  54. }
  55. *temp = msg.data.resp.celsius * 1000 + msg.data.resp.tenths * 100;
  56. return 0;
  57. }
  58. static const struct thermal_zone_of_device_ops imx_sc_thermal_ops = {
  59. .get_temp = imx_sc_thermal_get_temp,
  60. };
  61. static int imx_sc_thermal_probe(struct platform_device *pdev)
  62. {
  63. struct device_node *np, *child, *sensor_np;
  64. struct imx_sc_sensor *sensor;
  65. int ret;
  66. ret = imx_scu_get_handle(&thermal_ipc_handle);
  67. if (ret)
  68. return ret;
  69. np = of_find_node_by_name(NULL, "thermal-zones");
  70. if (!np)
  71. return -ENODEV;
  72. sensor_np = of_node_get(pdev->dev.of_node);
  73. for_each_available_child_of_node(np, child) {
  74. sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
  75. if (!sensor) {
  76. of_node_put(child);
  77. of_node_put(sensor_np);
  78. return -ENOMEM;
  79. }
  80. ret = thermal_zone_of_get_sensor_id(child,
  81. sensor_np,
  82. &sensor->resource_id);
  83. if (ret < 0) {
  84. dev_err(&pdev->dev,
  85. "failed to get valid sensor resource id: %d\n",
  86. ret);
  87. of_node_put(child);
  88. break;
  89. }
  90. sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
  91. sensor->resource_id,
  92. sensor,
  93. &imx_sc_thermal_ops);
  94. if (IS_ERR(sensor->tzd)) {
  95. dev_err(&pdev->dev, "failed to register thermal zone\n");
  96. ret = PTR_ERR(sensor->tzd);
  97. of_node_put(child);
  98. break;
  99. }
  100. if (devm_thermal_add_hwmon_sysfs(sensor->tzd))
  101. dev_warn(&pdev->dev, "failed to add hwmon sysfs attributes\n");
  102. }
  103. of_node_put(sensor_np);
  104. return ret;
  105. }
  106. static int imx_sc_thermal_remove(struct platform_device *pdev)
  107. {
  108. return 0;
  109. }
  110. static const struct of_device_id imx_sc_thermal_table[] = {
  111. { .compatible = "fsl,imx-sc-thermal", },
  112. {}
  113. };
  114. MODULE_DEVICE_TABLE(of, imx_sc_thermal_table);
  115. static struct platform_driver imx_sc_thermal_driver = {
  116. .probe = imx_sc_thermal_probe,
  117. .remove = imx_sc_thermal_remove,
  118. .driver = {
  119. .name = "imx-sc-thermal",
  120. .of_match_table = imx_sc_thermal_table,
  121. },
  122. };
  123. module_platform_driver(imx_sc_thermal_driver);
  124. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  125. MODULE_DESCRIPTION("Thermal driver for NXP i.MX SoCs with system controller");
  126. MODULE_LICENSE("GPL v2");