1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018-2020 NXP.
7 #include <linux/firmware/imx/sci.h>
8 #include <linux/firmware/imx/types.h>
9 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/thermal.h>
16 #include "thermal_core.h"
18 #define IMX_SC_MISC_FUNC_GET_TEMP 13
20 static struct imx_sc_ipc
*thermal_ipc_handle
;
22 struct imx_sc_sensor
{
23 struct thermal_zone_device
*tzd
;
30 } __packed
__aligned(4);
32 struct resp_get_temp
{
35 } __packed
__aligned(4);
37 struct imx_sc_msg_misc_get_temp
{
38 struct imx_sc_rpc_msg hdr
;
40 struct req_get_temp req
;
41 struct resp_get_temp resp
;
43 } __packed
__aligned(4);
45 static int imx_sc_thermal_get_temp(void *data
, int *temp
)
47 struct imx_sc_msg_misc_get_temp msg
;
48 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
49 struct imx_sc_sensor
*sensor
= data
;
52 msg
.data
.req
.resource_id
= sensor
->resource_id
;
53 msg
.data
.req
.type
= IMX_SC_C_TEMP
;
55 hdr
->ver
= IMX_SC_RPC_VERSION
;
56 hdr
->svc
= IMX_SC_RPC_SVC_MISC
;
57 hdr
->func
= IMX_SC_MISC_FUNC_GET_TEMP
;
60 ret
= imx_scu_call_rpc(thermal_ipc_handle
, &msg
, true);
62 dev_err(&sensor
->tzd
->device
, "read temp sensor %d failed, ret %d\n",
63 sensor
->resource_id
, ret
);
67 *temp
= msg
.data
.resp
.celsius
* 1000 + msg
.data
.resp
.tenths
* 100;
72 static const struct thermal_zone_of_device_ops imx_sc_thermal_ops
= {
73 .get_temp
= imx_sc_thermal_get_temp
,
76 static int imx_sc_thermal_probe(struct platform_device
*pdev
)
78 struct device_node
*np
, *child
, *sensor_np
;
79 struct imx_sc_sensor
*sensor
;
82 ret
= imx_scu_get_handle(&thermal_ipc_handle
);
86 np
= of_find_node_by_name(NULL
, "thermal-zones");
90 sensor_np
= of_node_get(pdev
->dev
.of_node
);
92 for_each_available_child_of_node(np
, child
) {
93 sensor
= devm_kzalloc(&pdev
->dev
, sizeof(*sensor
), GFP_KERNEL
);
95 of_node_put(sensor_np
);
99 ret
= thermal_zone_of_get_sensor_id(child
,
101 &sensor
->resource_id
);
104 "failed to get valid sensor resource id: %d\n",
109 sensor
->tzd
= devm_thermal_zone_of_sensor_register(&pdev
->dev
,
112 &imx_sc_thermal_ops
);
113 if (IS_ERR(sensor
->tzd
)) {
114 dev_err(&pdev
->dev
, "failed to register thermal zone\n");
115 ret
= PTR_ERR(sensor
->tzd
);
120 of_node_put(sensor_np
);
125 static int imx_sc_thermal_remove(struct platform_device
*pdev
)
130 static const struct of_device_id imx_sc_thermal_table
[] = {
131 { .compatible
= "fsl,imx-sc-thermal", },
134 MODULE_DEVICE_TABLE(of
, imx_sc_thermal_table
);
136 static struct platform_driver imx_sc_thermal_driver
= {
137 .probe
= imx_sc_thermal_probe
,
138 .remove
= imx_sc_thermal_remove
,
140 .name
= "imx-sc-thermal",
141 .of_match_table
= imx_sc_thermal_table
,
144 module_platform_driver(imx_sc_thermal_driver
);
146 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
147 MODULE_DESCRIPTION("Thermal driver for NXP i.MX SoCs with system controller");
148 MODULE_LICENSE("GPL v2");