0001
0002
0003
0004
0005
0006 #include <dt-bindings/firmware/imx/rsrc.h>
0007 #include <linux/err.h>
0008 #include <linux/firmware/imx/sci.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/of_device.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 #include <linux/thermal.h>
0015
0016 #include "thermal_core.h"
0017 #include "thermal_hwmon.h"
0018
0019 #define IMX_SC_MISC_FUNC_GET_TEMP 13
0020
0021 static struct imx_sc_ipc *thermal_ipc_handle;
0022
0023 struct imx_sc_sensor {
0024 struct thermal_zone_device *tzd;
0025 u32 resource_id;
0026 };
0027
0028 struct req_get_temp {
0029 u16 resource_id;
0030 u8 type;
0031 } __packed __aligned(4);
0032
0033 struct resp_get_temp {
0034 s16 celsius;
0035 s8 tenths;
0036 } __packed __aligned(4);
0037
0038 struct imx_sc_msg_misc_get_temp {
0039 struct imx_sc_rpc_msg hdr;
0040 union {
0041 struct req_get_temp req;
0042 struct resp_get_temp resp;
0043 } data;
0044 } __packed __aligned(4);
0045
0046 static int imx_sc_thermal_get_temp(void *data, int *temp)
0047 {
0048 struct imx_sc_msg_misc_get_temp msg;
0049 struct imx_sc_rpc_msg *hdr = &msg.hdr;
0050 struct imx_sc_sensor *sensor = data;
0051 int ret;
0052
0053 msg.data.req.resource_id = sensor->resource_id;
0054 msg.data.req.type = IMX_SC_C_TEMP;
0055
0056 hdr->ver = IMX_SC_RPC_VERSION;
0057 hdr->svc = IMX_SC_RPC_SVC_MISC;
0058 hdr->func = IMX_SC_MISC_FUNC_GET_TEMP;
0059 hdr->size = 2;
0060
0061 ret = imx_scu_call_rpc(thermal_ipc_handle, &msg, true);
0062 if (ret) {
0063 dev_err(&sensor->tzd->device, "read temp sensor %d failed, ret %d\n",
0064 sensor->resource_id, ret);
0065 return ret;
0066 }
0067
0068 *temp = msg.data.resp.celsius * 1000 + msg.data.resp.tenths * 100;
0069
0070 return 0;
0071 }
0072
0073 static const struct thermal_zone_of_device_ops imx_sc_thermal_ops = {
0074 .get_temp = imx_sc_thermal_get_temp,
0075 };
0076
0077 static int imx_sc_thermal_probe(struct platform_device *pdev)
0078 {
0079 struct device_node *np, *child, *sensor_np;
0080 struct imx_sc_sensor *sensor;
0081 int ret;
0082
0083 ret = imx_scu_get_handle(&thermal_ipc_handle);
0084 if (ret)
0085 return ret;
0086
0087 np = of_find_node_by_name(NULL, "thermal-zones");
0088 if (!np)
0089 return -ENODEV;
0090
0091 sensor_np = of_node_get(pdev->dev.of_node);
0092
0093 for_each_available_child_of_node(np, child) {
0094 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
0095 if (!sensor) {
0096 of_node_put(child);
0097 ret = -ENOMEM;
0098 goto put_node;
0099 }
0100
0101 ret = thermal_zone_of_get_sensor_id(child,
0102 sensor_np,
0103 &sensor->resource_id);
0104 if (ret < 0) {
0105 dev_err(&pdev->dev,
0106 "failed to get valid sensor resource id: %d\n",
0107 ret);
0108 of_node_put(child);
0109 break;
0110 }
0111
0112 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
0113 sensor->resource_id,
0114 sensor,
0115 &imx_sc_thermal_ops);
0116 if (IS_ERR(sensor->tzd)) {
0117 dev_err(&pdev->dev, "failed to register thermal zone\n");
0118 ret = PTR_ERR(sensor->tzd);
0119 of_node_put(child);
0120 break;
0121 }
0122
0123 if (devm_thermal_add_hwmon_sysfs(sensor->tzd))
0124 dev_warn(&pdev->dev, "failed to add hwmon sysfs attributes\n");
0125 }
0126
0127 put_node:
0128 of_node_put(sensor_np);
0129 of_node_put(np);
0130
0131 return ret;
0132 }
0133
0134 static int imx_sc_thermal_remove(struct platform_device *pdev)
0135 {
0136 return 0;
0137 }
0138
0139 static const struct of_device_id imx_sc_thermal_table[] = {
0140 { .compatible = "fsl,imx-sc-thermal", },
0141 {}
0142 };
0143 MODULE_DEVICE_TABLE(of, imx_sc_thermal_table);
0144
0145 static struct platform_driver imx_sc_thermal_driver = {
0146 .probe = imx_sc_thermal_probe,
0147 .remove = imx_sc_thermal_remove,
0148 .driver = {
0149 .name = "imx-sc-thermal",
0150 .of_match_table = imx_sc_thermal_table,
0151 },
0152 };
0153 module_platform_driver(imx_sc_thermal_driver);
0154
0155 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
0156 MODULE_DESCRIPTION("Thermal driver for NXP i.MX SoCs with system controller");
0157 MODULE_LICENSE("GPL v2");