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