imx_sc_thermal.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(struct thermal_zone_device *tz, 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 = tz->devdata;
  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_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 imx_sc_sensor *sensor;
  64. const int *resource_id;
  65. int i, ret;
  66. ret = imx_scu_get_handle(&thermal_ipc_handle);
  67. if (ret)
  68. return ret;
  69. resource_id = of_device_get_match_data(&pdev->dev);
  70. if (!resource_id)
  71. return -EINVAL;
  72. for (i = 0; resource_id[i] >= 0; i++) {
  73. sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
  74. if (!sensor)
  75. return -ENOMEM;
  76. sensor->resource_id = resource_id[i];
  77. sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, sensor->resource_id,
  78. sensor, &imx_sc_thermal_ops);
  79. if (IS_ERR(sensor->tzd)) {
  80. /*
  81. * Save the error value before freeing the
  82. * sensor pointer, otherwise we endup with a
  83. * use-after-free error
  84. */
  85. ret = PTR_ERR(sensor->tzd);
  86. devm_kfree(&pdev->dev, sensor);
  87. /*
  88. * The thermal framework notifies us there is
  89. * no thermal zone description for such a
  90. * sensor id
  91. */
  92. if (ret == -ENODEV)
  93. continue;
  94. dev_err(&pdev->dev, "failed to register thermal zone\n");
  95. return ret;
  96. }
  97. if (devm_thermal_add_hwmon_sysfs(sensor->tzd))
  98. dev_warn(&pdev->dev, "failed to add hwmon sysfs attributes\n");
  99. }
  100. return 0;
  101. }
  102. static const int imx_sc_sensors[] = { IMX_SC_R_SYSTEM, IMX_SC_R_PMIC_0, -1 };
  103. static const struct of_device_id imx_sc_thermal_table[] = {
  104. { .compatible = "fsl,imx-sc-thermal", .data = imx_sc_sensors },
  105. {}
  106. };
  107. MODULE_DEVICE_TABLE(of, imx_sc_thermal_table);
  108. static struct platform_driver imx_sc_thermal_driver = {
  109. .probe = imx_sc_thermal_probe,
  110. .driver = {
  111. .name = "imx-sc-thermal",
  112. .of_match_table = imx_sc_thermal_table,
  113. },
  114. };
  115. module_platform_driver(imx_sc_thermal_driver);
  116. MODULE_AUTHOR("Anson Huang <[email protected]>");
  117. MODULE_DESCRIPTION("Thermal driver for NXP i.MX SoCs with system controller");
  118. MODULE_LICENSE("GPL v2");