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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2018-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 <linux/kernel.h>
#include <linux/suspend.h>
#include "thermal_sensor_service_v01.h"
#include "../thermal_core.h"
#define QMI_SENS_DRIVER "qmi-therm-sensors"
#define QMI_TS_RESP_TOUT msecs_to_jiffies(100)
#define QMI_CLIENT_NAME_LENGTH 40
#define QMI_FL_SIGN 0x80000000
#define QMI_FL_EXP 0x7f800000
#define QMI_FL_MANTISSA 0x007fffff
#define QMI_FL_NORM 0x00800000
#define QMI_FL_SIGN_BIT 31
#define QMI_MANTISSA_MSB 23
enum qmi_ts_sensor {
QMI_TS_PA,
QMI_TS_PA_1,
QMI_TS_PA_2,
QMI_TS_QFE_PA_0,
QMI_TS_QFE_WTR_0,
QMI_TS_MODEM_MODEM,
QMI_TS_MMW_0,
QMI_TS_MMW_1,
QMI_TS_MMW_2,
QMI_TS_MMW_3,
QMI_TS_MODEM_SKIN,
QMI_TS_QFE_PA_MDM,
QMI_TS_QFE_PA_WTR,
QMI_TS_STREAMER_0,
QMI_TS_MOD_MMW_0,
QMI_TS_MOD_MMW_1,
QMI_TS_MOD_MMW_2,
QMI_TS_MOD_MMW_3,
QMI_TS_RET_PA_0,
QMI_TS_WTR_PA_0,
QMI_TS_WTR_PA_1,
QMI_TS_WTR_PA_2,
QMI_TS_WTR_PA_3,
QMI_SYS_THERM1,
QMI_SYS_THERM2,
QMI_TS_TSENS_1,
QMI_TS_MMW_PA1,
QMI_TS_MMW_PA2,
QMI_TS_MMW_PA3,
QMI_TS_SDR_MMW,
QMI_TS_MSM_SKIN,
QMI_TS_BEAMER_N_THERM,
QMI_TS_BEAMER_E_THERM,
QMI_TS_BEAMER_W_THERM,
QMI_TS_QFE_RET_PA0_FR1,
QMI_TS_QFE_WTR_PA0_FR1,
QMI_TS_QFE_WTR_PA1_FR1,
QMI_TS_QFE_WTR_PA2_FR1,
QMI_TS_QFE_WTR_PA3_FR1,
QMI_TS_QFE_WTR0_FR1,
QMI_TS_MAX_NR
};
struct qmi_sensor {
struct device *dev;
char qmi_name[QMI_CLIENT_NAME_LENGTH];
bool connection_active;
struct list_head ts_node;
struct thermal_zone_device *tz_dev;
int32_t last_reading;
int32_t high_thresh;
int32_t low_thresh;
struct qmi_ts_instance *ts;
enum qmi_ts_sensor sens_type;
struct work_struct therm_notify_work;
};
struct qmi_ts_instance {
struct device *dev;
struct qmi_handle handle;
struct mutex mutex;
uint32_t inst_id;
struct list_head ts_sensor_list;
struct work_struct svc_arrive_work;
};
static struct qmi_ts_instance *ts_instances;
static int ts_inst_cnt;
static atomic_t in_suspend;
static char sensor_clients[QMI_TS_MAX_NR][QMI_CLIENT_NAME_LENGTH] = {
{"pa"},
{"pa_1"},
{"pa_2"},
{"qfe_pa0"},
{"qfe_wtr0"},
{"modem_tsens"},
{"qfe_mmw0"},
{"qfe_mmw1"},
{"qfe_mmw2"},
{"qfe_mmw3"},
{"xo_therm"},
{"qfe_pa_mdm"},
{"qfe_pa_wtr"},
{"qfe_mmw_streamer0"},
{"qfe_mmw0_mod"},
{"qfe_mmw1_mod"},
{"qfe_mmw2_mod"},
{"qfe_mmw3_mod"},
{"qfe_ret_pa0"},
{"qfe_wtr_pa0"},
{"qfe_wtr_pa1"},
{"qfe_wtr_pa2"},
{"qfe_wtr_pa3"},
{"sys_therm1"},
{"sys_therm2"},
{"modem_tsens1"},
{"mmw_pa1"},
{"mmw_pa2"},
{"mmw_pa3"},
{"sdr_mmw_therm"},
{"msm_skin_therm"},
{"beamer_n_therm"},
{"beamer_e_therm"},
{"beamer_w_therm"},
{"qfe_ret_pa0_fr1"},
{"qfe_wtr_pa0_fr1"},
{"qfe_wtr_pa1_fr1"},
{"qfe_wtr_pa2_fr1"},
{"qfe_wtr_pa3_fr1"},
{"qfe_wtr0_fr1"},
};
static int32_t encode_qmi(int32_t val)
{
uint32_t shift = 0, local_val = 0;
int32_t temp_val = 0;
if (val == INT_MAX || val == INT_MIN)
return 0;
temp_val = val = val / 1000;
if (val < 0) {
temp_val *= -1;
local_val |= 1 << QMI_FL_SIGN_BIT;
}
shift = find_last_bit((const unsigned long *)&temp_val,
sizeof(temp_val) * 8);
local_val |= ((shift + 127) << QMI_MANTISSA_MSB);
temp_val &= ~(1 << shift);
local_val |= temp_val << (QMI_MANTISSA_MSB - shift);
pr_debug("inp:%d shift:%d out:%x temp_val:%x\n",
val, shift, local_val, temp_val);
return local_val;
}
static int32_t decode_qmi(int32_t val)
{
int32_t sign = 0, shift = 0, local_val;
sign = (val & QMI_FL_SIGN) ? -1 : 1;
shift = (val & QMI_FL_EXP) >> QMI_MANTISSA_MSB;
shift = QMI_MANTISSA_MSB - (shift - 127);
local_val = (val & QMI_FL_MANTISSA) | QMI_FL_NORM;
pr_debug("val:0x%x sign:%d shift:%d mantissa:%x temp:%d\n",
val, sign, shift, local_val,
sign * (local_val >> shift));
return sign * (local_val >> shift);
}
static int qmi_sensor_pm_notify(struct notifier_block *nb,
unsigned long mode, void *_unused)
{
switch (mode) {
case PM_HIBERNATION_PREPARE:
case PM_RESTORE_PREPARE:
case PM_SUSPEND_PREPARE:
atomic_set(&in_suspend, 1);
break;
case PM_POST_HIBERNATION:
case PM_POST_RESTORE:
case PM_POST_SUSPEND:
atomic_set(&in_suspend, 0);
break;
default:
break;
}
return 0;
}
static struct notifier_block qmi_sensor_pm_nb = {
.notifier_call = qmi_sensor_pm_notify,
};
static void qmi_ts_thresh_notify(struct work_struct *work)
{
struct qmi_sensor *qmi_sens = container_of(work,
struct qmi_sensor,
therm_notify_work);
of_thermal_handle_trip_temp(qmi_sens->dev, qmi_sens->tz_dev,
qmi_sens->last_reading);
};
static void qmi_ts_update_temperature(struct qmi_ts_instance *ts,
const struct ts_temp_report_ind_msg_v01 *ind_msg,
uint8_t notify)
{
struct qmi_sensor *qmi_sens;
list_for_each_entry(qmi_sens, &ts->ts_sensor_list,
ts_node) {
if ((strncasecmp(qmi_sens->qmi_name,
ind_msg->sensor_id.sensor_id,
QMI_TS_SENSOR_ID_LENGTH_MAX_V01)))
continue;
qmi_sens->last_reading =
decode_qmi(ind_msg->temp) * 1000;
pr_debug("sensor:%s temperature:%d\n",
qmi_sens->qmi_name, qmi_sens->last_reading);
if (!qmi_sens->tz_dev)
return;
if (notify &&
((qmi_sens->high_thresh != INT_MAX &&
qmi_sens->last_reading >= qmi_sens->high_thresh) ||
(qmi_sens->low_thresh != INT_MIN &&
qmi_sens->last_reading <= qmi_sens->low_thresh))) {
pr_debug("Sensor:%s Notify. temp:%d\n",
ind_msg->sensor_id.sensor_id,
qmi_sens->last_reading);
queue_work(system_highpri_wq,
&qmi_sens->therm_notify_work);
}
return;
}
}
void qmi_ts_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
struct qmi_txn *txn, const void *decoded)
{
const struct ts_temp_report_ind_msg_v01 *ind_msg = decoded;
uint8_t notify = 0;
struct qmi_ts_instance *ts = container_of(qmi, struct qmi_ts_instance,
handle);
if (!txn) {
pr_err("Invalid transaction\n");
return;
}
if ((ind_msg->report_type != QMI_TS_TEMP_REPORT_CURRENT_TEMP_V01) ||
ind_msg->seq_num_valid)
notify = 1;
if (ind_msg->temp_valid)
qmi_ts_update_temperature(ts, ind_msg, notify);
else
pr_err("Error invalid temperature field.\n");
}
static int qmi_ts_request(struct qmi_sensor *qmi_sens,
bool send_current_temp_report)
{
int ret = 0;
struct ts_register_notification_temp_resp_msg_v01 resp;
struct ts_register_notification_temp_req_msg_v01 req;
struct qmi_ts_instance *ts = qmi_sens->ts;
struct qmi_txn txn;
memset(&req, 0, sizeof(req));
memset(&resp, 0, sizeof(resp));
strlcpy(req.sensor_id.sensor_id, qmi_sens->qmi_name,
QMI_TS_SENSOR_ID_LENGTH_MAX_V01);
req.seq_num = 0;
if (send_current_temp_report) {
req.send_current_temp_report = 1;
req.seq_num_valid = true;
} else {
req.seq_num_valid = false;
req.temp_threshold_high_valid =
qmi_sens->high_thresh != INT_MAX;
req.temp_threshold_high =
encode_qmi(qmi_sens->high_thresh);
req.temp_threshold_low_valid =
qmi_sens->low_thresh != INT_MIN;
req.temp_threshold_low =
encode_qmi(qmi_sens->low_thresh);
}
mutex_lock(&ts->mutex);
ret = qmi_txn_init(&ts->handle, &txn,
ts_register_notification_temp_resp_msg_v01_ei, &resp);
if (ret < 0) {
pr_err("qmi txn init failed for %s ret:%d\n",
qmi_sens->qmi_name, ret);
goto qmi_send_exit;
}
ret = qmi_send_request(&ts->handle, NULL, &txn,
QMI_TS_REGISTER_NOTIFICATION_TEMP_REQ_V01,
TS_REGISTER_NOTIFICATION_TEMP_REQ_MSG_V01_MAX_MSG_LEN,
ts_register_notification_temp_req_msg_v01_ei, &req);
if (ret < 0) {
pr_err("qmi txn send failed for %s ret:%d\n",
qmi_sens->qmi_name, ret);
qmi_txn_cancel(&txn);
goto qmi_send_exit;
}
ret = qmi_txn_wait(&txn, QMI_TS_RESP_TOUT);
if (ret < 0) {
pr_err("qmi txn wait failed for %s ret:%d\n",
qmi_sens->qmi_name, ret);
goto qmi_send_exit;
}
if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
ret = resp.resp.result;
pr_err("qmi NOT success for %s ret:%d\n",
qmi_sens->qmi_name, ret);
goto qmi_send_exit;
}
ret = 0;
qmi_send_exit:
mutex_unlock(&ts->mutex);
return ret;
}
static int qmi_sensor_read(void *data, int *temp)
{
struct qmi_sensor *qmi_sens = (struct qmi_sensor *)data;
if (qmi_sens->connection_active && !atomic_read(&in_suspend))
qmi_ts_request(qmi_sens, true);
*temp = qmi_sens->last_reading;
return 0;
}
static int qmi_sensor_set_trips(void *data, int low, int high)
{
struct qmi_sensor *qmi_sens = (struct qmi_sensor *)data;
int ret = 0;
if (qmi_sens->high_thresh == high &&
qmi_sens->low_thresh == low)
return ret;
qmi_sens->high_thresh = high;
qmi_sens->low_thresh = low;
if (!qmi_sens->connection_active)
return ret;
ret = qmi_ts_request(qmi_sens, false);
if (ret)
pr_err("Sensor:%s set high trip:%d low trip:%d error%d\n",
qmi_sens->qmi_name,
qmi_sens->high_thresh,
qmi_sens->low_thresh,
ret);
return ret;
}
static struct thermal_zone_of_device_ops qmi_sensor_ops = {
.get_temp = qmi_sensor_read,
.set_trips = qmi_sensor_set_trips,
};
static struct qmi_msg_handler handlers[] = {
{
.type = QMI_INDICATION,
.msg_id = QMI_TS_TEMP_REPORT_IND_V01,
.ei = ts_temp_report_ind_msg_v01_ei,
.decoded_size = sizeof(struct ts_temp_report_ind_msg_v01),
.fn = qmi_ts_ind_cb
},
{}
};
static int qmi_register_sensor_device(struct qmi_sensor *qmi_sens)
{
int ret = 0;
qmi_sens->tz_dev = thermal_zone_of_sensor_register(
qmi_sens->dev,
qmi_sens->sens_type + qmi_sens->ts->inst_id,
qmi_sens, &qmi_sensor_ops);
if (IS_ERR(qmi_sens->tz_dev)) {
ret = PTR_ERR(qmi_sens->tz_dev);
if (ret != -ENODEV)
pr_err("sensor register failed for %s, ret:%d\n",
qmi_sens->qmi_name, ret);
qmi_sens->tz_dev = NULL;
return ret;
}
pr_debug("Sensor register success for %s\n", qmi_sens->qmi_name);
return 0;
}
static int verify_sensor_and_register(struct qmi_ts_instance *ts)
{
struct ts_get_sensor_list_req_msg_v01 req;
struct ts_get_sensor_list_resp_msg_v01 *ts_resp;
int ret = 0, i;
struct qmi_txn txn;
memset(&req, 0, sizeof(req));
/* size of ts_resp is very high, use heap memory rather than stack */
ts_resp = kzalloc(sizeof(*ts_resp), GFP_KERNEL);
if (!ts_resp)
return -ENOMEM;
mutex_lock(&ts->mutex);
ret = qmi_txn_init(&ts->handle, &txn,
ts_get_sensor_list_resp_msg_v01_ei, ts_resp);
if (ret < 0) {
pr_err("Transaction Init error for inst_id:0x%x ret:%d\n",
ts->inst_id, ret);
goto reg_exit;
}
ret = qmi_send_request(&ts->handle, NULL, &txn,
QMI_TS_GET_SENSOR_LIST_REQ_V01,
TS_GET_SENSOR_LIST_REQ_MSG_V01_MAX_MSG_LEN,
ts_get_sensor_list_req_msg_v01_ei,
&req);
if (ret < 0) {
qmi_txn_cancel(&txn);
goto reg_exit;
}
ret = qmi_txn_wait(&txn, QMI_TS_RESP_TOUT);
if (ret < 0) {
pr_err("Transaction wait error for inst_id:0x%x ret:%d\n",
ts->inst_id, ret);
goto reg_exit;
}
if (ts_resp->resp.result != QMI_RESULT_SUCCESS_V01) {
ret = ts_resp->resp.result;
pr_err("Get sensor list NOT success for inst_id:0x%x ret:%d\n",
ts->inst_id, ret);
goto reg_exit;
}
mutex_unlock(&ts->mutex);
for (i = 0; i < ts_resp->sensor_list_len; i++) {
struct qmi_sensor *qmi_sens = NULL;
list_for_each_entry(qmi_sens, &ts->ts_sensor_list,
ts_node) {
if ((strncasecmp(qmi_sens->qmi_name,
ts_resp->sensor_list[i].sensor_id,
QMI_TS_SENSOR_ID_LENGTH_MAX_V01)))
continue;
qmi_sens->connection_active = true;
/*
* Send a temperature request notification.
*/
qmi_ts_request(qmi_sens, true);
if (!qmi_sens->tz_dev)
ret = qmi_register_sensor_device(qmi_sens);
break;
}
}
kfree(ts_resp);
return ret;
reg_exit:
mutex_unlock(&ts->mutex);
kfree(ts_resp);
return ret;
}
static void qmi_ts_svc_arrive(struct work_struct *work)
{
struct qmi_ts_instance *ts = container_of(work,
struct qmi_ts_instance,
svc_arrive_work);
verify_sensor_and_register(ts);
}
static void thermal_qmi_net_reset(struct qmi_handle *qmi)
{
struct qmi_ts_instance *ts = container_of(qmi,
struct qmi_ts_instance,
handle);
struct qmi_sensor *qmi_sens = NULL;
int ret;
pr_debug("reset QMI server\n");
list_for_each_entry(qmi_sens, &ts->ts_sensor_list,
ts_node) {
if (!qmi_sens->connection_active)
continue;
qmi_ts_request(qmi_sens, true);
ret = qmi_ts_request(qmi_sens, false);
if (ret)
pr_err("Sensor:%s set high trip:%d low trip:%d err%d\n",
qmi_sens->tz_dev->type,
qmi_sens->high_thresh,
qmi_sens->low_thresh,
ret);
}
}
static void thermal_qmi_del_server(struct qmi_handle *qmi,
struct qmi_service *service)
{
struct qmi_ts_instance *ts = container_of(qmi,
struct qmi_ts_instance,
handle);
struct qmi_sensor *qmi_sens = NULL;
pr_debug("QMI server deleted\n");
list_for_each_entry(qmi_sens, &ts->ts_sensor_list, ts_node)
qmi_sens->connection_active = false;
}
static int thermal_qmi_new_server(struct qmi_handle *qmi,
struct qmi_service *service)
{
struct qmi_ts_instance *ts = container_of(qmi,
struct qmi_ts_instance,
handle);
struct sockaddr_qrtr sq = {AF_QIPCRTR, service->node, service->port};
mutex_lock(&ts->mutex);
kernel_connect(qmi->sock, (struct sockaddr *)&sq, sizeof(sq), 0);
mutex_unlock(&ts->mutex);
queue_work(system_highpri_wq, &ts->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_ts_cleanup(void)
{
struct qmi_ts_instance *ts;
struct qmi_sensor *qmi_sens, *c_next;
int idx = 0;
for (; idx < ts_inst_cnt; idx++) {
ts = &ts_instances[idx];
mutex_lock(&ts->mutex);
list_for_each_entry_safe(qmi_sens, c_next,
&ts->ts_sensor_list, ts_node) {
qmi_sens->connection_active = false;
if (qmi_sens->tz_dev)
thermal_zone_of_sensor_unregister(
qmi_sens->dev, qmi_sens->tz_dev);
list_del(&qmi_sens->ts_node);
}
qmi_handle_release(&ts->handle);
mutex_unlock(&ts->mutex);
}
ts_inst_cnt = 0;
}
static int of_get_qmi_ts_platform_data(struct device *dev)
{
int ret = 0, i = 0, idx = 0;
struct device_node *np = dev->of_node;
struct device_node *subsys_np = NULL;
struct qmi_ts_instance *ts;
struct qmi_sensor *qmi_sens;
int sens_name_max = 0, sens_idx = 0, subsys_cnt = 0;
subsys_cnt = of_get_available_child_count(np);
if (!subsys_cnt) {
dev_err(dev, "No child node to process\n");
return -EFAULT;
}
ts = devm_kcalloc(dev, subsys_cnt, sizeof(*ts), GFP_KERNEL);
if (!ts)
return -ENOMEM;
for_each_available_child_of_node(np, subsys_np) {
if (idx >= subsys_cnt)
break;
ret = of_property_read_u32(subsys_np, "qcom,instance-id",
&ts[idx].inst_id);
if (ret) {
dev_err(dev, "error reading qcom,insance-id. ret:%d\n",
ret);
goto data_fetch_err;
}
ts[idx].dev = dev;
mutex_init(&ts[idx].mutex);
INIT_LIST_HEAD(&ts[idx].ts_sensor_list);
INIT_WORK(&ts[idx].svc_arrive_work, qmi_ts_svc_arrive);
sens_name_max = of_property_count_strings(subsys_np,
"qcom,qmi-sensor-names");
if (sens_name_max <= 0) {
dev_err(dev, "Invalid or no sensor. err:%d\n",
sens_name_max);
ret = -EINVAL;
goto data_fetch_err;
}
for (sens_idx = 0; sens_idx < sens_name_max; sens_idx++) {
const char *qmi_name;
qmi_sens = devm_kzalloc(dev, sizeof(*qmi_sens),
GFP_KERNEL);
if (!qmi_sens) {
ret = -ENOMEM;
goto data_fetch_err;
}
of_property_read_string_index(subsys_np,
"qcom,qmi-sensor-names", sens_idx,
&qmi_name);
strlcpy(qmi_sens->qmi_name, qmi_name,
QMI_CLIENT_NAME_LENGTH);
/* Check for supported qmi sensors */
for (i = 0; i < QMI_TS_MAX_NR; i++) {
if (!strcmp(sensor_clients[i],
qmi_sens->qmi_name))
break;
}
if (i >= QMI_TS_MAX_NR) {
dev_err(dev, "Unknown sensor:%s\n",
qmi_sens->qmi_name);
ret = -EINVAL;
goto data_fetch_err;
}
dev_dbg(dev, "QMI sensor:%s available\n", qmi_name);
qmi_sens->sens_type = i;
qmi_sens->ts = &ts[idx];
qmi_sens->dev = dev;
qmi_sens->last_reading = 0;
qmi_sens->high_thresh = INT_MAX;
qmi_sens->low_thresh = INT_MIN;
INIT_WORK(&qmi_sens->therm_notify_work,
qmi_ts_thresh_notify);
list_add(&qmi_sens->ts_node, &ts[idx].ts_sensor_list);
}
idx++;
}
ts_instances = ts;
ts_inst_cnt = subsys_cnt;
return 0;
data_fetch_err:
of_node_put(subsys_np);
return ret;
}
static int qmi_sens_device_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
int ret = 0, idx = 0;
struct qmi_ts_instance *ts;
ret = of_get_qmi_ts_platform_data(dev);
if (ret)
goto probe_err;
if (!ts_instances || !ts_inst_cnt) {
dev_err(dev, "Empty ts instances\n");
return -EINVAL;
}
for (; idx < ts_inst_cnt; idx++) {
ts = &ts_instances[idx];
if (list_empty(&ts->ts_sensor_list)) {
ret = -ENODEV;
goto probe_err;
}
ret = qmi_handle_init(&ts->handle,
TS_GET_SENSOR_LIST_RESP_MSG_V01_MAX_MSG_LEN,
&thermal_qmi_event_ops, handlers);
if (ret < 0) {
dev_err(dev, "QMI[0x%x] handle init failed. err:%d\n",
ts->inst_id, ret);
goto probe_err;
}
ret = qmi_add_lookup(&ts->handle, TS_SERVICE_ID_V01,
TS_SERVICE_VERS_V01, ts->inst_id);
if (ret < 0) {
dev_err(dev, "QMI register failed for 0x%x, ret:%d\n",
ts->inst_id, ret);
goto probe_err;
}
}
atomic_set(&in_suspend, 0);
register_pm_notifier(&qmi_sensor_pm_nb);
return 0;
probe_err:
qmi_ts_cleanup();
return ret;
}
static int qmi_sens_device_remove(struct platform_device *pdev)
{
qmi_ts_cleanup();
unregister_pm_notifier(&qmi_sensor_pm_nb);
return 0;
}
static const struct of_device_id qmi_sens_device_match[] = {
{.compatible = "qcom,qmi-sensors"},
{}
};
static struct platform_driver qmi_sens_device_driver = {
.probe = qmi_sens_device_probe,
.remove = qmi_sens_device_remove,
.driver = {
.name = QMI_SENS_DRIVER,
.of_match_table = qmi_sens_device_match,
},
};
module_platform_driver(qmi_sens_device_driver);
MODULE_LICENSE("GPL v2");
|