blob: 8d76dbfde6a9ff425f3e24b2c6e388bfad5649ba [file] [log] [blame]
Anson Huange20db702020-02-22 08:08:50 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018-2020 NXP.
4 */
5
Dong Aisheng755a7392020-04-26 16:11:43 +08006#include <dt-bindings/firmware/imx/rsrc.h>
Anson Huange20db702020-02-22 08:08:50 +08007#include <linux/err.h>
8#include <linux/firmware/imx/sci.h>
Anson Huange20db702020-02-22 08:08:50 +08009#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"
Anson Huangd2bc4dd2020-03-26 11:13:31 +080017#include "thermal_hwmon.h"
Anson Huange20db702020-02-22 08:08:50 +080018
19#define IMX_SC_MISC_FUNC_GET_TEMP 13
20
21static struct imx_sc_ipc *thermal_ipc_handle;
22
23struct imx_sc_sensor {
24 struct thermal_zone_device *tzd;
25 u32 resource_id;
26};
27
28struct req_get_temp {
29 u16 resource_id;
30 u8 type;
Anson Huang1fd213f2020-03-02 10:51:25 +080031} __packed __aligned(4);
Anson Huange20db702020-02-22 08:08:50 +080032
33struct resp_get_temp {
Anson Huang968ea0d2020-03-19 16:26:20 +080034 s16 celsius;
35 s8 tenths;
Anson Huang1fd213f2020-03-02 10:51:25 +080036} __packed __aligned(4);
Anson Huange20db702020-02-22 08:08:50 +080037
38struct imx_sc_msg_misc_get_temp {
39 struct imx_sc_rpc_msg hdr;
40 union {
41 struct req_get_temp req;
42 struct resp_get_temp resp;
43 } data;
Anson Huang1fd213f2020-03-02 10:51:25 +080044} __packed __aligned(4);
Anson Huange20db702020-02-22 08:08:50 +080045
46static int imx_sc_thermal_get_temp(void *data, int *temp)
47{
48 struct imx_sc_msg_misc_get_temp msg;
49 struct imx_sc_rpc_msg *hdr = &msg.hdr;
50 struct imx_sc_sensor *sensor = data;
51 int ret;
52
53 msg.data.req.resource_id = sensor->resource_id;
54 msg.data.req.type = IMX_SC_C_TEMP;
55
56 hdr->ver = IMX_SC_RPC_VERSION;
57 hdr->svc = IMX_SC_RPC_SVC_MISC;
58 hdr->func = IMX_SC_MISC_FUNC_GET_TEMP;
59 hdr->size = 2;
60
61 ret = imx_scu_call_rpc(thermal_ipc_handle, &msg, true);
62 if (ret) {
63 dev_err(&sensor->tzd->device, "read temp sensor %d failed, ret %d\n",
64 sensor->resource_id, ret);
65 return ret;
66 }
67
68 *temp = msg.data.resp.celsius * 1000 + msg.data.resp.tenths * 100;
69
70 return 0;
71}
72
73static const struct thermal_zone_of_device_ops imx_sc_thermal_ops = {
74 .get_temp = imx_sc_thermal_get_temp,
75};
76
77static int imx_sc_thermal_probe(struct platform_device *pdev)
78{
79 struct device_node *np, *child, *sensor_np;
80 struct imx_sc_sensor *sensor;
81 int ret;
82
83 ret = imx_scu_get_handle(&thermal_ipc_handle);
84 if (ret)
85 return ret;
86
87 np = of_find_node_by_name(NULL, "thermal-zones");
88 if (!np)
89 return -ENODEV;
90
91 sensor_np = of_node_get(pdev->dev.of_node);
92
93 for_each_available_child_of_node(np, child) {
94 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
95 if (!sensor) {
Krzysztof Kozlowski3da97622021-06-14 21:22:29 +020096 of_node_put(child);
Anson Huange20db702020-02-22 08:08:50 +080097 of_node_put(sensor_np);
98 return -ENOMEM;
99 }
100
101 ret = thermal_zone_of_get_sensor_id(child,
102 sensor_np,
103 &sensor->resource_id);
104 if (ret < 0) {
105 dev_err(&pdev->dev,
106 "failed to get valid sensor resource id: %d\n",
107 ret);
Krzysztof Kozlowski3da97622021-06-14 21:22:29 +0200108 of_node_put(child);
Anson Huange20db702020-02-22 08:08:50 +0800109 break;
110 }
111
112 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
113 sensor->resource_id,
114 sensor,
115 &imx_sc_thermal_ops);
116 if (IS_ERR(sensor->tzd)) {
117 dev_err(&pdev->dev, "failed to register thermal zone\n");
118 ret = PTR_ERR(sensor->tzd);
Krzysztof Kozlowski3da97622021-06-14 21:22:29 +0200119 of_node_put(child);
Anson Huange20db702020-02-22 08:08:50 +0800120 break;
121 }
Anson Huangd2bc4dd2020-03-26 11:13:31 +0800122
123 if (devm_thermal_add_hwmon_sysfs(sensor->tzd))
124 dev_warn(&pdev->dev, "failed to add hwmon sysfs attributes\n");
Anson Huange20db702020-02-22 08:08:50 +0800125 }
126
127 of_node_put(sensor_np);
128
129 return ret;
130}
131
132static int imx_sc_thermal_remove(struct platform_device *pdev)
133{
134 return 0;
135}
136
137static const struct of_device_id imx_sc_thermal_table[] = {
138 { .compatible = "fsl,imx-sc-thermal", },
139 {}
140};
141MODULE_DEVICE_TABLE(of, imx_sc_thermal_table);
142
143static struct platform_driver imx_sc_thermal_driver = {
144 .probe = imx_sc_thermal_probe,
145 .remove = imx_sc_thermal_remove,
146 .driver = {
147 .name = "imx-sc-thermal",
148 .of_match_table = imx_sc_thermal_table,
149 },
150};
151module_platform_driver(imx_sc_thermal_driver);
152
153MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
154MODULE_DESCRIPTION("Thermal driver for NXP i.MX SoCs with system controller");
155MODULE_LICENSE("GPL v2");