xref: /openbmc/linux/drivers/thermal/tegra/tegra-bpmp-thermal.c (revision d4d8516624e1042d33011cf93b6e9c220a22c9f0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2017, NVIDIA CORPORATION.  All rights reserved.
4  *
5  * Author:
6  *	Mikko Perttunen <mperttunen@nvidia.com>
7  *	Aapo Vienamo	<avienamo@nvidia.com>
8  */
9 
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/thermal.h>
14 #include <linux/workqueue.h>
15 
16 #include <soc/tegra/bpmp.h>
17 #include <soc/tegra/bpmp-abi.h>
18 
19 struct tegra_bpmp_thermal_zone {
20 	struct tegra_bpmp_thermal *tegra;
21 	struct thermal_zone_device *tzd;
22 	struct work_struct tz_device_update_work;
23 	unsigned int idx;
24 };
25 
26 struct tegra_bpmp_thermal {
27 	struct device *dev;
28 	struct tegra_bpmp *bpmp;
29 	unsigned int num_zones;
30 	struct tegra_bpmp_thermal_zone **zones;
31 };
32 
33 static int __tegra_bpmp_thermal_get_temp(struct tegra_bpmp_thermal_zone *zone,
34 					 int *out_temp)
35 {
36 	struct mrq_thermal_host_to_bpmp_request req;
37 	union mrq_thermal_bpmp_to_host_response reply;
38 	struct tegra_bpmp_message msg;
39 	int err;
40 
41 	memset(&req, 0, sizeof(req));
42 	req.type = CMD_THERMAL_GET_TEMP;
43 	req.get_temp.zone = zone->idx;
44 
45 	memset(&msg, 0, sizeof(msg));
46 	msg.mrq = MRQ_THERMAL;
47 	msg.tx.data = &req;
48 	msg.tx.size = sizeof(req);
49 	msg.rx.data = &reply;
50 	msg.rx.size = sizeof(reply);
51 
52 	err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg);
53 	if (err)
54 		return err;
55 	if (msg.rx.ret)
56 		return -EINVAL;
57 
58 	*out_temp = reply.get_temp.temp;
59 
60 	return 0;
61 }
62 
63 static int tegra_bpmp_thermal_get_temp(struct thermal_zone_device *tz, int *out_temp)
64 {
65 	struct tegra_bpmp_thermal_zone *zone = thermal_zone_device_priv(tz);
66 
67 	return __tegra_bpmp_thermal_get_temp(zone, out_temp);
68 }
69 
70 static int tegra_bpmp_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
71 {
72 	struct tegra_bpmp_thermal_zone *zone = thermal_zone_device_priv(tz);
73 	struct mrq_thermal_host_to_bpmp_request req;
74 	struct tegra_bpmp_message msg;
75 	int err;
76 
77 	memset(&req, 0, sizeof(req));
78 	req.type = CMD_THERMAL_SET_TRIP;
79 	req.set_trip.zone = zone->idx;
80 	req.set_trip.enabled = true;
81 	req.set_trip.low = low;
82 	req.set_trip.high = high;
83 
84 	memset(&msg, 0, sizeof(msg));
85 	msg.mrq = MRQ_THERMAL;
86 	msg.tx.data = &req;
87 	msg.tx.size = sizeof(req);
88 
89 	err = tegra_bpmp_transfer(zone->tegra->bpmp, &msg);
90 	if (err)
91 		return err;
92 	if (msg.rx.ret)
93 		return -EINVAL;
94 
95 	return 0;
96 }
97 
98 static void tz_device_update_work_fn(struct work_struct *work)
99 {
100 	struct tegra_bpmp_thermal_zone *zone;
101 
102 	zone = container_of(work, struct tegra_bpmp_thermal_zone,
103 			    tz_device_update_work);
104 
105 	thermal_zone_device_update(zone->tzd, THERMAL_TRIP_VIOLATED);
106 }
107 
108 static void bpmp_mrq_thermal(unsigned int mrq, struct tegra_bpmp_channel *ch,
109 			     void *data)
110 {
111 	struct mrq_thermal_bpmp_to_host_request req;
112 	struct tegra_bpmp_thermal *tegra = data;
113 	size_t offset;
114 	int i;
115 
116 	offset = offsetof(struct tegra_bpmp_mb_data, data);
117 	iosys_map_memcpy_from(&req, &ch->ib, offset, sizeof(req));
118 
119 	if (req.type != CMD_THERMAL_HOST_TRIP_REACHED) {
120 		dev_err(tegra->dev, "%s: invalid request type: %d\n", __func__, req.type);
121 		tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0);
122 		return;
123 	}
124 
125 	for (i = 0; i < tegra->num_zones; ++i) {
126 		if (tegra->zones[i]->idx != req.host_trip_reached.zone)
127 			continue;
128 
129 		schedule_work(&tegra->zones[i]->tz_device_update_work);
130 		tegra_bpmp_mrq_return(ch, 0, NULL, 0);
131 		return;
132 	}
133 
134 	dev_err(tegra->dev, "%s: invalid thermal zone: %d\n", __func__,
135 		req.host_trip_reached.zone);
136 	tegra_bpmp_mrq_return(ch, -EINVAL, NULL, 0);
137 }
138 
139 static int tegra_bpmp_thermal_get_num_zones(struct tegra_bpmp *bpmp,
140 					    int *num_zones)
141 {
142 	struct mrq_thermal_host_to_bpmp_request req;
143 	union mrq_thermal_bpmp_to_host_response reply;
144 	struct tegra_bpmp_message msg;
145 	int err;
146 
147 	memset(&req, 0, sizeof(req));
148 	req.type = CMD_THERMAL_GET_NUM_ZONES;
149 
150 	memset(&msg, 0, sizeof(msg));
151 	msg.mrq = MRQ_THERMAL;
152 	msg.tx.data = &req;
153 	msg.tx.size = sizeof(req);
154 	msg.rx.data = &reply;
155 	msg.rx.size = sizeof(reply);
156 
157 	err = tegra_bpmp_transfer(bpmp, &msg);
158 	if (err)
159 		return err;
160 	if (msg.rx.ret)
161 		return -EINVAL;
162 
163 	*num_zones = reply.get_num_zones.num;
164 
165 	return 0;
166 }
167 
168 static const struct thermal_zone_device_ops tegra_bpmp_of_thermal_ops = {
169 	.get_temp = tegra_bpmp_thermal_get_temp,
170 	.set_trips = tegra_bpmp_thermal_set_trips,
171 };
172 
173 static int tegra_bpmp_thermal_probe(struct platform_device *pdev)
174 {
175 	struct tegra_bpmp *bpmp = dev_get_drvdata(pdev->dev.parent);
176 	struct tegra_bpmp_thermal *tegra;
177 	struct thermal_zone_device *tzd;
178 	unsigned int i, max_num_zones;
179 	int err;
180 
181 	tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
182 	if (!tegra)
183 		return -ENOMEM;
184 
185 	tegra->dev = &pdev->dev;
186 	tegra->bpmp = bpmp;
187 
188 	err = tegra_bpmp_thermal_get_num_zones(bpmp, &max_num_zones);
189 	if (err) {
190 		dev_err(&pdev->dev, "failed to get the number of zones: %d\n",
191 			err);
192 		return err;
193 	}
194 
195 	tegra->zones = devm_kcalloc(&pdev->dev, max_num_zones,
196 				    sizeof(*tegra->zones), GFP_KERNEL);
197 	if (!tegra->zones)
198 		return -ENOMEM;
199 
200 	for (i = 0; i < max_num_zones; ++i) {
201 		struct tegra_bpmp_thermal_zone *zone;
202 		int temp;
203 
204 		zone = devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL);
205 		if (!zone)
206 			return -ENOMEM;
207 
208 		zone->idx = i;
209 		zone->tegra = tegra;
210 
211 		err = __tegra_bpmp_thermal_get_temp(zone, &temp);
212 		if (err < 0) {
213 			devm_kfree(&pdev->dev, zone);
214 			continue;
215 		}
216 
217 		tzd = devm_thermal_of_zone_register(
218 			&pdev->dev, i, zone, &tegra_bpmp_of_thermal_ops);
219 		if (IS_ERR(tzd)) {
220 			if (PTR_ERR(tzd) == -EPROBE_DEFER)
221 				return -EPROBE_DEFER;
222 			devm_kfree(&pdev->dev, zone);
223 			continue;
224 		}
225 
226 		zone->tzd = tzd;
227 		INIT_WORK(&zone->tz_device_update_work,
228 			  tz_device_update_work_fn);
229 
230 		tegra->zones[tegra->num_zones++] = zone;
231 	}
232 
233 	err = tegra_bpmp_request_mrq(bpmp, MRQ_THERMAL, bpmp_mrq_thermal,
234 				     tegra);
235 	if (err) {
236 		dev_err(&pdev->dev, "failed to register mrq handler: %d\n",
237 			err);
238 		return err;
239 	}
240 
241 	platform_set_drvdata(pdev, tegra);
242 
243 	return 0;
244 }
245 
246 static int tegra_bpmp_thermal_remove(struct platform_device *pdev)
247 {
248 	struct tegra_bpmp_thermal *tegra = platform_get_drvdata(pdev);
249 
250 	tegra_bpmp_free_mrq(tegra->bpmp, MRQ_THERMAL, tegra);
251 
252 	return 0;
253 }
254 
255 static const struct of_device_id tegra_bpmp_thermal_of_match[] = {
256 	{ .compatible = "nvidia,tegra186-bpmp-thermal" },
257 	{ },
258 };
259 MODULE_DEVICE_TABLE(of, tegra_bpmp_thermal_of_match);
260 
261 static struct platform_driver tegra_bpmp_thermal_driver = {
262 	.probe = tegra_bpmp_thermal_probe,
263 	.remove = tegra_bpmp_thermal_remove,
264 	.driver = {
265 		.name = "tegra-bpmp-thermal",
266 		.of_match_table = tegra_bpmp_thermal_of_match,
267 	},
268 };
269 module_platform_driver(tegra_bpmp_thermal_driver);
270 
271 MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
272 MODULE_DESCRIPTION("NVIDIA Tegra BPMP thermal sensor driver");
273 MODULE_LICENSE("GPL v2");
274