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