1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
*/
#define pr_fmt(fmt) "%s:%s " fmt, KBUILD_MODNAME, __func__
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/thermal.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/soc/qcom/qmi.h>
#include <linux/net.h>
#include "thermal_mitigation_device_service_v01.h"
#define QMI_CDEV_DRIVER "qmi-cooling-device"
#define QMI_TMD_RESP_TOUT msecs_to_jiffies(100)
#define QMI_CLIENT_NAME_LENGTH 40
struct qmi_cooling_device {
struct device_node *np;
char cdev_name[THERMAL_NAME_LENGTH];
char qmi_name[QMI_CLIENT_NAME_LENGTH];
bool connection_active;
struct list_head qmi_node;
struct thermal_cooling_device *cdev;
unsigned int mtgn_state;
unsigned int max_level;
struct qmi_tmd_instance *tmd;
};
struct qmi_tmd_instance {
struct device *dev;
struct qmi_handle handle;
struct mutex mutex;
uint32_t inst_id;
struct list_head tmd_cdev_list;
struct work_struct svc_arrive_work;
};
static struct qmi_tmd_instance *tmd_instances;
static int tmd_inst_cnt;
static char device_clients[][QMI_CLIENT_NAME_LENGTH] = {
{"pa"},
{"pa_fr1"},
{"cx_vdd_limit"},
{"modem"},
{"modem_current"},
{"modem_skin"},
{"modem_bw"},
{"modem_bw_backoff"},
{"vbatt_low"},
{"charge_state"},
{"mmw0"},
{"mmw1"},
{"mmw2"},
{"mmw3"},
{"mmw_skin0"},
{"mmw_skin1"},
{"mmw_skin2"},
{"mmw_skin3"},
{"wlan"},
{"wlan_bw"},
{"mmw_skin0_dsc"},
{"mmw_skin1_dsc"},
{"mmw_skin2_dsc"},
{"mmw_skin3_dsc"},
{"modem_skin_lte_dsc"},
{"modem_skin_nr_dsc"},
{"pa_dsc"},
{"pa_fr1_dsc"},
{"cdsp_sw"},
{"cdsp_hw"},
{"cpuv_restriction_cold"},
{"cpr_cold"}
};
static int qmi_get_max_state(struct thermal_cooling_device *cdev,
unsigned long *state)
{
struct qmi_cooling_device *qmi_cdev = cdev->devdata;
if (!qmi_cdev)
return -EINVAL;
*state = qmi_cdev->max_level;
return 0;
}
static int qmi_get_cur_state(struct thermal_cooling_device *cdev,
unsigned long *state)
{
struct qmi_cooling_device *qmi_cdev = cdev->devdata;
if (!qmi_cdev)
return -EINVAL;
*state = qmi_cdev->mtgn_state;
return 0;
}
static int qmi_tmd_send_state_request(struct qmi_cooling_device *qmi_cdev,
uint8_t state)
{
int ret = 0;
struct tmd_set_mitigation_level_req_msg_v01 req;
struct tmd_set_mitigation_level_resp_msg_v01 tmd_resp;
struct qmi_tmd_instance *tmd = qmi_cdev->tmd;
struct qmi_txn txn;
memset(&req, 0, sizeof(req));
memset(&tmd_resp, 0, sizeof(tmd_resp));
strlcpy(req.mitigation_dev_id.mitigation_dev_id, qmi_cdev->qmi_name,
QMI_TMD_MITIGATION_DEV_ID_LENGTH_MAX_V01);
req.mitigation_level = state;
mutex_lock(&tmd->mutex);
ret = qmi_txn_init(&tmd->handle, &txn,
tmd_set_mitigation_level_resp_msg_v01_ei, &tmd_resp);
if (ret < 0) {
pr_err("qmi set state:%d txn init failed for %s ret:%d\n",
state, qmi_cdev->cdev_name, ret);
goto qmi_send_exit;
}
ret = qmi_send_request(&tmd->handle, NULL, &txn,
QMI_TMD_SET_MITIGATION_LEVEL_REQ_V01,
TMD_SET_MITIGATION_LEVEL_REQ_MSG_V01_MAX_MSG_LEN,
tmd_set_mitigation_level_req_msg_v01_ei, &req);
if (ret < 0) {
pr_err("qmi set state:%d txn send failed for %s ret:%d\n",
state, qmi_cdev->cdev_name, ret);
qmi_txn_cancel(&txn);
goto qmi_send_exit;
}
ret = qmi_txn_wait(&txn, QMI_TMD_RESP_TOUT);
if (ret < 0) {
pr_err("qmi set state:%d txn wait failed for %s ret:%d\n",
state, qmi_cdev->cdev_name, ret);
goto qmi_send_exit;
}
if (tmd_resp.resp.result != QMI_RESULT_SUCCESS_V01) {
ret = tmd_resp.resp.result;
pr_err("qmi set state:%d NOT success for %s ret:%d\n",
state, qmi_cdev->cdev_name, ret);
goto qmi_send_exit;
}
ret = 0;
pr_debug("Requested qmi state:%d for %s\n", state, qmi_cdev->cdev_name);
qmi_send_exit:
mutex_unlock(&tmd->mutex);
return ret;
}
static int qmi_set_cur_state(struct thermal_cooling_device *cdev,
unsigned long state)
{
struct qmi_cooling_device *qmi_cdev = cdev->devdata;
int ret = 0;
if (!qmi_cdev)
return -EINVAL;
if (state > qmi_cdev->max_level)
return -EINVAL;
if (qmi_cdev->mtgn_state == state)
return 0;
/* save it and return if server exit */
if (!qmi_cdev->connection_active) {
qmi_cdev->mtgn_state = state;
pr_debug("Pending request:%ld for %s\n", state,
qmi_cdev->cdev_name);
return 0;
}
/* It is best effort to save state even if QMI fail */
ret = qmi_tmd_send_state_request(qmi_cdev, (uint8_t)state);
qmi_cdev->mtgn_state = state;
return ret;
}
static struct thermal_cooling_device_ops qmi_device_ops = {
.get_max_state = qmi_get_max_state,
.get_cur_state = qmi_get_cur_state,
.set_cur_state = qmi_set_cur_state,
};
static int qmi_register_cooling_device(struct qmi_cooling_device *qmi_cdev)
{
qmi_cdev->cdev = thermal_of_cooling_device_register(
qmi_cdev->np,
qmi_cdev->cdev_name,
qmi_cdev,
&qmi_device_ops);
if (IS_ERR(qmi_cdev->cdev)) {
pr_err("Cooling register failed for %s, ret:%ld\n",
qmi_cdev->cdev_name, PTR_ERR(qmi_cdev->cdev));
return PTR_ERR(qmi_cdev->cdev);
}
pr_debug("Cooling register success for %s\n", qmi_cdev->cdev_name);
return 0;
}
static int verify_devices_and_register(struct qmi_tmd_instance *tmd)
{
struct tmd_get_mitigation_device_list_req_msg_v01 req;
struct tmd_get_mitigation_device_list_resp_msg_v01 *tmd_resp;
int ret = 0, i;
struct qmi_txn txn;
memset(&req, 0, sizeof(req));
/* size of tmd_resp is very high, use heap memory rather than stack */
tmd_resp = kzalloc(sizeof(*tmd_resp), GFP_KERNEL);
if (!tmd_resp)
return -ENOMEM;
mutex_lock(&tmd->mutex);
ret = qmi_txn_init(&tmd->handle, &txn,
tmd_get_mitigation_device_list_resp_msg_v01_ei, tmd_resp);
if (ret < 0) {
pr_err("Transaction Init error for inst_id:0x%x ret:%d\n",
tmd->inst_id, ret);
goto reg_exit;
}
ret = qmi_send_request(&tmd->handle, NULL, &txn,
QMI_TMD_GET_MITIGATION_DEVICE_LIST_REQ_V01,
TMD_GET_MITIGATION_DEVICE_LIST_REQ_MSG_V01_MAX_MSG_LEN,
tmd_get_mitigation_device_list_req_msg_v01_ei,
&req);
if (ret < 0) {
qmi_txn_cancel(&txn);
goto reg_exit;
}
ret = qmi_txn_wait(&txn, QMI_TMD_RESP_TOUT);
if (ret < 0) {
pr_err("Transaction wait error for inst_id:0x%x ret:%d\n",
tmd->inst_id, ret);
goto reg_exit;
}
if (tmd_resp->resp.result != QMI_RESULT_SUCCESS_V01) {
ret = tmd_resp->resp.result;
pr_err("Get device list NOT success for inst_id:0x%x ret:%d\n",
tmd->inst_id, ret);
goto reg_exit;
}
mutex_unlock(&tmd->mutex);
for (i = 0; i < tmd_resp->mitigation_device_list_len; i++) {
struct qmi_cooling_device *qmi_cdev = NULL;
list_for_each_entry(qmi_cdev, &tmd->tmd_cdev_list,
qmi_node) {
struct tmd_mitigation_dev_list_type_v01 *device =
&tmd_resp->mitigation_device_list[i];
if ((strncasecmp(qmi_cdev->qmi_name,
device->mitigation_dev_id.mitigation_dev_id,
QMI_TMD_MITIGATION_DEV_ID_LENGTH_MAX_V01)))
continue;
qmi_cdev->connection_active = true;
qmi_cdev->max_level = device->max_mitigation_level;
/*
* It is better to set current state
* initially or during restart
*/
qmi_tmd_send_state_request(qmi_cdev,
qmi_cdev->mtgn_state);
if (!qmi_cdev->cdev)
ret = qmi_register_cooling_device(qmi_cdev);
break;
}
}
kfree(tmd_resp);
return ret;
reg_exit:
mutex_unlock(&tmd->mutex);
kfree(tmd_resp);
return ret;
}
static void qmi_tmd_svc_arrive(struct work_struct *work)
{
struct qmi_tmd_instance *tmd = container_of(work,
struct qmi_tmd_instance,
svc_arrive_work);
verify_devices_and_register(tmd);
}
static void thermal_qmi_net_reset(struct qmi_handle *qmi)
{
struct qmi_tmd_instance *tmd = container_of(qmi,
struct qmi_tmd_instance,
handle);
struct qmi_cooling_device *qmi_cdev = NULL;
list_for_each_entry(qmi_cdev, &tmd->tmd_cdev_list,
qmi_node) {
if (qmi_cdev->connection_active)
qmi_tmd_send_state_request(qmi_cdev,
qmi_cdev->mtgn_state);
}
}
static void thermal_qmi_del_server(struct qmi_handle *qmi,
struct qmi_service *service)
{
struct qmi_tmd_instance *tmd = container_of(qmi,
struct qmi_tmd_instance,
handle);
struct qmi_cooling_device *qmi_cdev = NULL;
list_for_each_entry(qmi_cdev, &tmd->tmd_cdev_list, qmi_node)
qmi_cdev->connection_active = false;
}
static int thermal_qmi_new_server(struct qmi_handle *qmi,
struct qmi_service *service)
{
struct qmi_tmd_instance *tmd = container_of(qmi,
struct qmi_tmd_instance,
handle);
struct sockaddr_qrtr sq = {AF_QIPCRTR, service->node, service->port};
mutex_lock(&tmd->mutex);
kernel_connect(qmi->sock, (struct sockaddr *)&sq, sizeof(sq), 0);
mutex_unlock(&tmd->mutex);
queue_work(system_highpri_wq, &tmd->svc_arrive_work);
return 0;
}
static struct qmi_ops thermal_qmi_event_ops = {
.new_server = thermal_qmi_new_server,
.del_server = thermal_qmi_del_server,
.net_reset = thermal_qmi_net_reset,
};
static void qmi_tmd_cleanup(void)
{
int idx = 0;
struct qmi_tmd_instance *tmd = tmd_instances;
struct qmi_cooling_device *qmi_cdev, *c_next;
for (; idx < tmd_inst_cnt; idx++) {
mutex_lock(&tmd[idx].mutex);
list_for_each_entry_safe(qmi_cdev, c_next,
&tmd[idx].tmd_cdev_list, qmi_node) {
qmi_cdev->connection_active = false;
if (qmi_cdev->cdev)
thermal_cooling_device_unregister(
qmi_cdev->cdev);
list_del(&qmi_cdev->qmi_node);
}
qmi_handle_release(&tmd[idx].handle);
mutex_unlock(&tmd[idx].mutex);
}
}
static int of_get_qmi_tmd_platform_data(struct device *dev)
{
int ret = 0, idx = 0, i = 0, subsys_cnt = 0;
struct device_node *np = dev->of_node;
struct device_node *subsys_np = NULL, *cdev_np = NULL;
struct qmi_tmd_instance *tmd;
struct qmi_cooling_device *qmi_cdev;
subsys_cnt = of_get_available_child_count(np);
if (!subsys_cnt) {
dev_err(dev, "No child node to process\n");
return -EFAULT;
}
tmd = devm_kcalloc(dev, subsys_cnt, sizeof(*tmd), GFP_KERNEL);
if (!tmd)
return -ENOMEM;
for_each_available_child_of_node(np, subsys_np) {
if (idx >= subsys_cnt) {
of_node_put(subsys_np);
break;
}
ret = of_property_read_u32(subsys_np, "qcom,instance-id",
&tmd[idx].inst_id);
if (ret) {
dev_err(dev, "error reading qcom,insance-id. ret:%d\n",
ret);
goto data_subsys_error;
}
tmd[idx].dev = dev;
mutex_init(&tmd[idx].mutex);
INIT_LIST_HEAD(&tmd[idx].tmd_cdev_list);
INIT_WORK(&tmd[idx].svc_arrive_work, qmi_tmd_svc_arrive);
for_each_available_child_of_node(subsys_np, cdev_np) {
const char *qmi_name;
qmi_cdev = devm_kzalloc(dev, sizeof(*qmi_cdev),
GFP_KERNEL);
if (!qmi_cdev) {
ret = -ENOMEM;
goto data_error;
}
strlcpy(qmi_cdev->cdev_name, cdev_np->name,
THERMAL_NAME_LENGTH);
if (!of_property_read_string(cdev_np,
"qcom,qmi-dev-name",
&qmi_name)) {
strlcpy(qmi_cdev->qmi_name, qmi_name,
QMI_CLIENT_NAME_LENGTH);
} else {
dev_err(dev, "Fail to parse dev name for %s\n",
cdev_np->name);
of_node_put(cdev_np);
break;
}
/* Check for supported qmi dev*/
for (i = 0; i < ARRAY_SIZE(device_clients); i++) {
if (strcmp(device_clients[i],
qmi_cdev->qmi_name) == 0)
break;
}
if (i >= ARRAY_SIZE(device_clients)) {
dev_err(dev, "Not supported dev name for %s\n",
cdev_np->name);
of_node_put(cdev_np);
break;
}
qmi_cdev->tmd = &tmd[idx];
qmi_cdev->np = cdev_np;
qmi_cdev->mtgn_state = 0;
list_add(&qmi_cdev->qmi_node, &tmd[idx].tmd_cdev_list);
}
idx++;
}
of_node_put(np);
tmd_instances = tmd;
tmd_inst_cnt = subsys_cnt;
return 0;
data_error:
of_node_put(cdev_np);
data_subsys_error:
of_node_put(subsys_np);
of_node_put(np);
return ret;
}
static int qmi_device_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
int ret = 0, idx = 0;
ret = of_get_qmi_tmd_platform_data(dev);
if (ret)
goto probe_err;
if (!tmd_instances || !tmd_inst_cnt) {
dev_err(dev, "Empty tmd instances\n");
return -EINVAL;
}
for (; idx < tmd_inst_cnt; idx++) {
struct qmi_tmd_instance *tmd = &tmd_instances[idx];
if (list_empty(&tmd->tmd_cdev_list))
continue;
ret = qmi_handle_init(&tmd->handle,
TMD_GET_MITIGATION_DEVICE_LIST_RESP_MSG_V01_MAX_MSG_LEN,
&thermal_qmi_event_ops, NULL);
if (ret < 0) {
dev_err(dev, "QMI[0x%x] handle init failed. err:%d\n",
tmd->inst_id, ret);
goto probe_err;
}
ret = qmi_add_lookup(&tmd->handle, TMD_SERVICE_ID_V01,
TMD_SERVICE_VERS_V01,
tmd->inst_id);
if (ret < 0) {
dev_err(dev, "QMI register failed for 0x%x, ret:%d\n",
tmd->inst_id, ret);
goto probe_err;
}
}
return 0;
probe_err:
qmi_tmd_cleanup();
return ret;
}
static int qmi_device_remove(struct platform_device *pdev)
{
qmi_tmd_cleanup();
return 0;
}
static const struct of_device_id qmi_device_match[] = {
{.compatible = "qcom,qmi-cooling-devices"},
{}
};
static struct platform_driver qmi_device_driver = {
.probe = qmi_device_probe,
.remove = qmi_device_remove,
.driver = {
.name = QMI_CDEV_DRIVER,
.of_match_table = qmi_device_match,
},
};
static int __init qmi_device_init(void)
{
return platform_driver_register(&qmi_device_driver);
}
module_init(qmi_device_init);
static void __exit qmi_device_exit(void)
{
platform_driver_unregister(&qmi_device_driver);
}
module_exit(qmi_device_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("QTI QMI cooling device driver");
|