-
Notifications
You must be signed in to change notification settings - Fork 13
/
sunxi_ths_core.c
784 lines (671 loc) · 21.3 KB
/
sunxi_ths_core.c
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
* drivers/thermal/sunxi_thermal/sunxi_ths_core.c
*
* Copyright (C) 2013-2024 allwinner.
* JiaRui Xiao<xiaojiarui@allwinnertech.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#define NEED_DEBUG (0)
#if NEED_DEBUG
#define DEBUG
#endif
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/of_gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/of_platform.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/sunxi-sid.h>
#include <linux/sunxi-smc.h>
#include "sunxi_ths.h"
#include "sunxi_ths_core.h"
#include "sunxi_ths_efuse.h"
#define DISABLE_EN_REG 0
static struct thermal_reg *status_reg;
static struct thermal_reg *data_reg;
static struct thermal_reg *enable_reg;
static ssize_t
thermal_sensor_info_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct sunxi_ths_data *ths_data = platform_get_drvdata(pdev);
int indx;
struct thermal_sensor_info *sensor_info =
(struct thermal_sensor_info *)ths_data->data;
for (indx = 0; indx < ths_data->sensor_cnt; indx++) {
sprintf(buf, "sensor_%d_%s_temp", indx, sensor_info[indx].ths_name);
if (!strcmp(attr->attr.name, buf)) {
return sprintf(buf, "sensor%d located in %s, temp is = %d\n",
indx,
sensor_info[indx].ths_name,
sensor_info[indx].temp);
}
}
return -EINVAL;
}
int ths_driver_create_sensor_info_attrs(struct sunxi_ths_data *ths_data,
struct thermal_sensor_info *sensor_info)
{
int indx, size;
size = sizeof(struct ths_info_attr) * ths_data->sensor_cnt;
ths_data->ths_info_attrs = kzalloc(size, GFP_KERNEL);
if (!ths_data->ths_info_attrs) {
pr_err("ths_info_attrs: not enough memory for sensor_info\n");
return -ENOMEM;
}
for (indx = 0; indx < ths_data->sensor_cnt; indx++) {
/* create trip type attribute */
snprintf(ths_data->ths_info_attrs[indx].name, THERMAL_ATTR_LENGTH,
"sensor_%d_%s_temp", indx, sensor_info[indx].ths_name);
sysfs_attr_init(&ths_data->ths_info_attrs[indx].attr.attr);
ths_data->ths_info_attrs[indx].attr.attr.name =
ths_data->ths_info_attrs[indx].name;
ths_data->ths_info_attrs[indx].attr.attr.mode = S_IRUGO;
ths_data->ths_info_attrs[indx].attr.show = thermal_sensor_info_show;
device_create_file(&ths_data->pdev->dev,
&ths_data->ths_info_attrs[indx].attr);
}
return 0;
}
void ths_drvier_remove_trip_attrs(struct sunxi_ths_data *ths_data)
{
int indx;
for (indx = 0; indx < ths_data->sensor_cnt; indx++) {
device_remove_file(&ths_data->pdev->dev,
&ths_data->ths_info_attrs[indx].attr);
}
kfree(ths_data->ths_info_attrs);
}
/*
* @interface name: reg_to_temp_version_1
* @function: change the value what read from register to the temp for thermal
* version 1.
*/
static int reg_to_temp_version_1(u32 reg_data, u16 id,
struct temp_calculate_coefficent *para)
{
int t = 0;
t = (int)(reg_data * (para->nt_para[id].MUL_PARA)) - (int)(para->nt_para[id].MINU_PARA);
t = t / (int)para->nt_para[id].DIV_PARA;
return t;
}
/*
* @interface name: reg_to_temp_version_2_3
* @function: change the value what read from register to the temp for thermal
* version 2 or 3.
*/
static int reg_to_temp_version_2_3(u32 reg_data, u16 id,
struct temp_calculate_coefficent *para)
{
int t = 0;
t = (int)(para->nt_para[id].MINU_PARA) - (int)(reg_data * (para->nt_para[id].MUL_PARA));
/* rounding for variable t */
t += (int)para->nt_para[id].DIV_PARA/2;
t = t / (int)para->nt_para[id].DIV_PARA;
return t;
}
/*
* ths_calculate_temp_x10_version_2_3() --using ths data reg to calculate temp
* reg_data: ths data register
* id: ths sensor id
* para: calculate coefficent
*
* calculrate temp and return tempx10 to reduce deviation.
*
* return: real temp * 10,for ex:real-temp = 25.6, then return 256
*/
static int ths_calculate_temp_x10_version_2_3(u32 reg_data, u16 id,
struct temp_calculate_coefficent *para)
{
int t = 0;
t = (int)(para->nt_para[id].MINU_PARA) -
(int)(reg_data * (para->nt_para[id].MUL_PARA));
t = (t * 10) / (int)para->nt_para[id].DIV_PARA;
return t;
}
u32 ths_driver_temp_to_reg(int temp, u16 id,
struct temp_calculate_coefficent *para)
{
u32 reg;
reg = (para->nt_para[id].MINU_PARA -
(temp * para->nt_para[id].DIV_PARA));
reg = reg / para->nt_para[id].MUL_PARA;
return (u32) reg;
}
static int get_ths_driver_version(struct sunxi_ths_data *ths_data)
{
return ths_data->ths_driver_version;
}
void ths_driver_clk_cfg(struct sunxi_ths_data *ths_data)
{
unsigned long rate = 0;
if (ths_data->parent_clk == false) {
rate = clk_get_rate(ths_data->pclk);
thsprintk("get ths_clk_source rate %dHZ\n", (__u32) rate);
return;
}
rate = clk_get_rate(ths_data->pclk);
thsprintk("get ths_clk_source rate %dHZ\n", (__u32) rate);
if (ths_data->ths_driver_version != 3) {
if (clk_set_parent(ths_data->mclk, ths_data->pclk)) {
pr_err("%s: set ths_clk parent to ths_clk_source failed!\n",
__func__);
return;
}
rate = clk_get_rate(ths_data->mclk);
thsprintk("get ths_clk rate %dHZ\n", (__u32) rate);
}
/*if (clk_set_rate(ths_data->mclk, ths_clk))
* pr_err("set ths clock freq to %uM failed!\n", ths_clk);
*/
if (clk_prepare_enable(ths_data->mclk))
pr_err("try to enable ths_clk failed!\n");
return;
}
void ths_driver_clk_uncfg(struct sunxi_ths_data *ths_data)
{
if (ths_data->parent_clk == true) {
if (!(ths_data->mclk == NULL || IS_ERR(ths_data->mclk))) {
clk_disable_unprepare(ths_data->mclk);
clk_put(ths_data->mclk);
ths_data->mclk = NULL;
}
}
if (!(ths_data->pclk == NULL || IS_ERR(ths_data->pclk))) {
clk_put(ths_data->pclk);
ths_data->pclk = NULL;
}
}
int ths_driver_startup(struct sunxi_ths_data *ths_data,
struct device *dev)
{
struct device_node *np = NULL;
np = dev->of_node;
ths_data->base_addr = of_iomap(np, 0);
if (ths_data->base_addr != NULL) {
thsprintk("ths base: %p !\n", ths_data->base_addr);
} else {
pr_err("%s:Failed to ioremap() io memory region.\n", __func__);
return -EBUSY;
}
ths_data->irq = irq_of_parse_and_map(np, 0);
if (ths_data->irq != 0) {
thsprintk("ths irq num: %d !\n", ths_data->irq);
} else {
pr_err("%s:Failed to map irq.\n", __func__);
return -EBUSY;
}
if (of_property_read_u32(np, "sensor_num", &ths_data->sensor_cnt)) {
pr_err("%s: get sensor_num failed\n", __func__);
return -EBUSY;
}
if (of_property_read_u32(np, "combine_num", &ths_data->combine_cnt))
pr_err("%s: get combine_num failed\n", __func__);
if (of_property_read_u32(np, "alarm_low_temp",
&ths_data->alarm_low_temp)) {
pr_err("%s: get alarm_low_temp failed\n", __func__);
}
if (of_property_read_u32(np, "alarm_high_temp",
&ths_data->alarm_high_temp)) {
pr_err("%s: get alarm_high_temp failed\n", __func__);
}
if (of_property_read_u32(np, "alarm_temp_hysteresis",
&ths_data->alarm_temp_hysteresis)) {
pr_err("%s: get alarm_temp_hysteresis failed\n", __func__);
}
if (of_property_read_u32(np, "shut_temp", &ths_data->shut_temp)) {
pr_err("%s: get int temp failed\n", __func__);
ths_data->shut_temp = 120;
}
thsprintk("ths have parent clk: %d, thermal version is: %d !\n",
ths_data->parent_clk, ths_data->ths_driver_version);
ths_data->pclk = of_clk_get(np, 0);
if (ths_data->pclk == NULL || IS_ERR(ths_data->pclk)) {
pr_err("%s:Failed to get pclk.\n", __func__);
return -EBUSY;
}
if (ths_data->parent_clk == true) {
ths_data->mclk = of_clk_get(np, 1);
if (ths_data->mclk == NULL || IS_ERR(ths_data->mclk)) {
pr_err("%s:Failed to get mclk.\n", __func__);
return -EBUSY;
}
}
return 0;
}
static int ths_driver_reg_to_temp(u32 reg_data, u16 id,
u32 ths_driver_version,
struct temp_calculate_coefficent *para)
{
int t = 0;
/**when reg_data bigger than before,
* the real temp is smaller than before.
*/
if (reg_data > para->down_limit.split_reg) {
pr_err("%s:Thermal sensor calculate reg to temp is error.\n",
__func__);
return OUT_THE_THERMAL_LIMIT;
} else if ((reg_data <= para->down_limit.split_reg) &&
(reg_data >= para->up_limit.split_reg)) {
thsprintk("THIS :MUL_PARA=%d DIV_PARA=%d MINU_PARA=%d.\n",
para->nt_para[id].MUL_PARA,
para->nt_para[id].DIV_PARA,
para->nt_para[id].MINU_PARA);
if (ths_driver_version == 1)
t = reg_to_temp_version_1(reg_data, id, para);
else
t = reg_to_temp_version_2_3(reg_data, id, para);
} else if (reg_data < para->up_limit.split_reg) {
thsprintk("NEXT:MUL_PARA=%d DIV_PARA=%d MINU_PARA=%d.\n",
para->nt_para[id].MUL_PARA,
para->nt_para[id].DIV_PARA,
para->nt_para[id].MINU_PARA);
t = ths_driver_reg_to_temp(reg_data, id, ths_driver_version, para + 1);
}
return t;
}
/*
* @interface name: reg_to_temp_to_reg_efuse
* @function: change the value what read from register to the temp for thermal
* version 3. we use float type, because we need to calcula calibration efuse
* info.
*/
static u32 reg_to_temp_to_reg_efuse(u32 reg_data, u16 id, u16 environment_temp, struct temp_calculate_coefficent *para)
{
u32 t = 0;
u32 write_reg_data;
s32 reg_to_temp;
u16 envir_temp;
envir_temp = (u16)environment_temp;
reg_to_temp = ths_calculate_temp_x10_version_2_3(reg_data, id, para);
t = ((reg_to_temp - envir_temp) * CONST_DIV) /
SENSOR_CP_EUFSE_PER_REG_TO_TEMP;
write_reg_data = THS_EFUSE_DEFAULT_VALUE - t;
if ((write_reg_data & (~THS_EFUSE_ENVIROMENT_MASK)) == 0) {
thsprintk("efuse register data will write 0x%x", write_reg_data);
return write_reg_data;
} else {
thsprintk("efuse register data is 12-bit but write 0x%x", write_reg_data);
return WRONG_EFUSE_REG_DATA;
}
}
int ths_driver_get_temp(struct sunxi_ths_data *ths_data, int id)
{
u32 reg_data;
u32 reg_flag = 0;
int ths_drv_ver;
int t = 0;
if (id + 1 > ths_data->sensor_cnt) {
thsprintk("the sensor id has writed wrong, beyond sensor %d\n",
ths_data->sensor_cnt);
return WRONG_SENSOR_ID;
}
mutex_lock(&ths_data->ths_mutex_lock);
reg_data = readl(ths_data->base_addr + status_reg->offset);
ths_drv_ver = get_ths_driver_version(ths_data);
if (ths_drv_ver == 1)
reg_flag = (0x40000 << id);
else if (ths_drv_ver == 2)
reg_flag = (0x100 << id);
else if (ths_drv_ver == 3)
reg_flag = (0x1 << id);
if (!(reg_data & reg_flag)) {
thsprintk("reg_data=0x%x,reg_flag=0x%x\n", reg_data, reg_flag);
mutex_unlock(&ths_data->ths_mutex_lock);
return NO_DATA_INTTERUPT;
}
reg_data = readl(ths_data->base_addr
+ data_reg->offset + id * 4);
reg_data &= THERMAL_DATA_REG_MAX;
t = ths_driver_reg_to_temp(reg_data, id, ths_data->ths_driver_version,
ths_data->ths_coefficent->calcular_para);
/* clear the status */
writel(reg_flag, ths_data->base_addr + status_reg->offset);
mutex_unlock(&ths_data->ths_mutex_lock);
if ((ths_data->ths_driver_version == 3) && (ths_data->cp_ft_flag == THS_CALIBRATION_IN_FT))
t = t + FT_CALIBRATION_DEVIATION;
if (t >= ths_data->shut_temp)
printk(KERN_EMERG "ths data[%d] = 0x%x, temp is %d\n",
id,
reg_data,
t);
thsprintk("THS data[%d] = 0x%x, temp is %d\n",
id,
reg_data,
t);
return t;
}
/*this api can calcular power of the base */
u32 unsigned_int_pow(u32 base, u32 power)
{
u32 value = 1;
if (power == 0)
return 1;
while (power != 0) {
value *= base;
power--;
}
return value;
}
/*
* return 0, is CP calibration.
* return 1, is FT calibration.
*/
static u16 get_cp_ft_flag(u16 reg_value)
{
return (reg_value & THS_EFUSE_CP_FT_MASK) >> THS_EFUSE_CP_FT_BIT;
}
/*
* the new test method add CP calibration and
* support the old FT calibration. but the version3
* thermal divide the enviroment temp reg from the FT,
* so the FT method different to version_1_2.
*/
static void write_efuse_to_reg_ver_3(struct sunxi_ths_data *ths_data,
int cal_reg_num,
struct thermal_reg *reg,
u32 *ths_cal_data)
{
int i, calcu_times;
u16 first_16bit;
u16 environment_temp;
u32 sensor_temp;
u32 write_value = 0;
u16 *efuse_sensor0_reg_data;
struct temp_calculate_coefficent *para = ths_data->ths_coefficent->calcular_para;
if (*ths_cal_data == 0)
pr_info("%s:reading environment_temp is zero\n", __func__);
else
thsprintk("thermal sensor efuse fisrt 32-bit info 0x%x\n", *ths_cal_data);
first_16bit = (u16)*ths_cal_data;
/* last 4-bit is CP or FT method in all of 16-bit */
ths_data->cp_ft_flag = get_cp_ft_flag(first_16bit);
/* first 12-bit is environment temp in all of 16-bit */
environment_temp = first_16bit & THS_EFUSE_ENVIROMENT_MASK;
/* make the pointer point to the sensor0 efuse address */
efuse_sensor0_reg_data = (u16 *)ths_cal_data;
efuse_sensor0_reg_data++;
thsprintk("efuse_sensor0_reg_data 0x%x\n", *efuse_sensor0_reg_data);
thsprintk("first_16 0x%x, environment_temp 0x%x, cp_ft_flag 0x%x\n", first_16bit, environment_temp, ths_data->cp_ft_flag);
for (i = 0, calcu_times = 0; i < ths_data->sensor_cnt; i++) {
if (*(efuse_sensor0_reg_data + i) == 0) {
pr_err("%s:reading calibration data%d is zero\n",
__func__, i);
return;
}
sensor_temp = reg_to_temp_to_reg_efuse((u32)*(efuse_sensor0_reg_data + i), i,
environment_temp, para);
if (sensor_temp == WRONG_EFUSE_REG_DATA) {
pr_err("%s:getting efuse data bit out of 12-bit limit\n", __func__);
return;
}
/**
* calcu_times is used to distinguish the operation
* num which is odd number or even number.
*/
if (calcu_times == 0) {
/* we calcular the sensor efuse when isn't last sensor*/
if ((i + 1) < ths_data->sensor_cnt) {
write_value = sensor_temp;
thsprintk("sensor:%d efuse_reg[%d] =0x%x\n", i, i / 2, write_value);
calcu_times++;
continue;
} else {
write_value = sensor_temp;
writel(write_value, ths_data->base_addr +
reg->offset + (i / 2) * 4);
thsprintk("sensor:%d efuse_reg[%d] =0x%x\n", i, i / 2, write_value);
break;
}
}
/* calcular the calibration efuse data high 16-bit and write to register */
if (calcu_times == 1) {
calcu_times = 0;
write_value |= (sensor_temp << 16);
writel(write_value, ths_data->base_addr +
reg->offset + (i / 2) * 4);
thsprintk("sensor:%d efuse_reg[%d] =0x%x\n", i, i / 2, write_value);
}
}
}
static void write_efuse_to_reg_ver_1_2(struct sunxi_ths_data *ths_data,
int cal_reg_num,
struct thermal_reg *reg,
u32 *ths_cal_data)
{
int i;
u32 reg_data;
for (i = 0; i < cal_reg_num; i++) {
if (*(ths_cal_data + i) == 0) {
pr_err("%s:reading calibration data%d is zero\n",
__func__, i);
break;
}
thsprintk("ths_cal_data[%d] =0x%x\n",
i, *(ths_cal_data + i));
writel(*(ths_cal_data + i), ths_data->base_addr
+ reg->offset + i * 4);
reg_data = readl(ths_data->base_addr
+ reg->offset + i * 4);
if (reg_data != *(ths_cal_data + i))
thsprintk("write efuse fail ths_cal_data[%d] =0x%x,reg_data = 0x%x\n",
i, *(ths_cal_data + i), reg_data);
}
}
static void ths_driver_write_efuse_to_reg(struct sunxi_ths_data *ths_data,
int cal_reg_num,
struct thermal_reg *reg)
{
u32 *ths_cal_data;
int ret;
int ths_drv_ver;
ths_cal_data = kmalloc(sizeof(u32), GFP_KERNEL);
ret = sunxi_efuse_readn(EFUSE_THM_SENSOR_NAME, (void *)(ths_cal_data),
2 * sizeof(u32));
if (ret) {
thsprintk("%s:read the efuse key fail\n", __func__);
} else {
ths_drv_ver = get_ths_driver_version(ths_data);
if (ths_drv_ver == 1 || ths_drv_ver == 2) {
write_efuse_to_reg_ver_1_2(ths_data, cal_reg_num, reg,
ths_cal_data);
} else if (ths_drv_ver == 3) {
write_efuse_to_reg_ver_3(ths_data, cal_reg_num, reg,
ths_cal_data);
}
}
kfree(ths_cal_data);
}
/**
* old_reg need the mask to protect the data,
* writed to reg before enbale sensor.
*/
static void ths_driver_enable_enreg(struct sunxi_ths_data *ths_data,
struct thermal_reg *reg)
{
u32 reg_value;
u32 reg_enable_sensor_bit = (u32)0xffffffff;
int ths_drv_ver;
ths_drv_ver = get_ths_driver_version(ths_data);
reg_value = readl(ths_data->base_addr + reg->offset);
if (ths_drv_ver == 1) {
reg_enable_sensor_bit &=
((unsigned_int_pow(2, ths_data->sensor_cnt) - 1) << 18);
reg_value |= reg_enable_sensor_bit;
} else if (ths_drv_ver == 2) {
reg_enable_sensor_bit &=
(unsigned_int_pow(2, ths_data->sensor_cnt) - 1);
reg_value |= reg_enable_sensor_bit;
thsprintk("reg->name=%s,value=0x%x,reg_enable_sensor_bit=0x%x\n",
reg->name, reg_value, reg_enable_sensor_bit);
} else if (ths_drv_ver == 3) {
reg_value = unsigned_int_pow(2, ths_data->sensor_cnt) - 1;
}
writel(reg_value, ths_data->base_addr + reg->offset);
}
void ths_driver_reg_debug(struct sunxi_ths_data *ths_data,
struct thermal_sensor_coefficent *ths_coefficent)
{
struct thermal_reg *thermal_reg = NULL;
thermal_reg = ths_coefficent->reg_para;
while (strcmp("", thermal_reg->name)) {
thsprintk("reg_name=%s, value=0x%x\n",
thermal_reg->name,
readl(ths_data->base_addr +
thermal_reg->offset));
thermal_reg++;
}
}
static void ths_driver_init_old_shut_reg(struct sunxi_ths_data *ths_data,
struct thermal_reg *reg,
struct temp_calculate_coefficent *cpara)
{
u32 reg_value;
int i;
for (i = 0; i < ths_data->sensor_cnt; i++) {
reg_value = ths_driver_temp_to_reg(ths_data->shut_temp, i, cpara);
reg_value = (reg_value << 16);
writel(reg_value, ths_data->base_addr + reg->offset + i * 4);
}
}
static void ths_driver_init_new_shut_reg(struct sunxi_ths_data *ths_data,
struct thermal_reg *reg,
struct temp_calculate_coefficent *cpara)
{
u32 write_value, reg_value_store;
int i, calcu_times;
for (i = 0, calcu_times = 0; i < ths_data->sensor_cnt; i++) {
reg_value_store =
ths_driver_temp_to_reg(ths_data->shut_temp, i, cpara);
/**
* calcu_times is used to distinguish the operation
* num which is odd number or even number.
*/
if (calcu_times == 0) {
/* we calcular the sensor efuse when isn't last sensor*/
if ((i + 1) < ths_data->sensor_cnt) {
write_value = (u32)reg_value_store;
thsprintk("sensor:%d shut_temp_reg[%d] =0x%x\n", i, i / 2, write_value);
calcu_times++;
continue;
} else {
write_value = (u32)reg_value_store;
writel(write_value, ths_data->base_addr +
reg->offset + (i / 2) * 4);
thsprintk("sensor:%d shut_temp_reg[%d] =0x%x\n", i, i / 2, write_value);
break;
}
}
/* calcular the calibration efuse data high 16-bit and write to register */
if (calcu_times == 1) {
calcu_times = 0;
write_value |= (reg_value_store << 16);
writel(write_value, ths_data->base_addr +
reg->offset + (i / 2) * 4);
thsprintk("sensor:%d shut_temp_reg[%d] =0x%x\n", i, i / 2, write_value);
}
}
}
static void ths_driver_init_shut_temp_reg(struct sunxi_ths_data *ths_data,
struct thermal_reg *reg, struct temp_calculate_coefficent *para)
{
int ths_drv_ver;
ths_drv_ver = get_ths_driver_version(ths_data);
if (ths_drv_ver == 2)
ths_driver_init_old_shut_reg(ths_data, reg, para);
else if (ths_drv_ver == 3)
ths_driver_init_new_shut_reg(ths_data, reg, para);
}
int ths_driver_init_reg(struct sunxi_ths_data *ths_data,
struct thermal_sensor_coefficent *ths_coefficent)
{
int calibration_reg_num = 0;
int interrupt_sta_reg_num = 0;
struct thermal_reg *cali_reg = NULL;
struct thermal_reg *thermal_reg = NULL;
struct temp_calculate_coefficent *cpara = NULL;
int ths_drv_ver;
status_reg = NULL;
data_reg = NULL;
enable_reg = NULL;
cpara = ths_coefficent->calcular_para;
thermal_reg = ths_coefficent->reg_para;
ths_drv_ver = get_ths_driver_version(ths_data);
/*do the init until the name is nothing*/
while (strcmp("", thermal_reg->name)) {
switch (thermal_reg->init_type) {
case NO_INIT:
break;
case NORMAL_REG:
writel((thermal_reg->value),
ths_data->base_addr + (thermal_reg->offset));
break;
case ENABLE_REG:
if ((ths_drv_ver == 1) || (ths_drv_ver == 2))
writel(thermal_reg->value,
ths_data->base_addr + thermal_reg->offset);
enable_reg = thermal_reg;
break;
case CDATA_REG:
calibration_reg_num++;
if (calibration_reg_num == 1)
cali_reg = thermal_reg;
break;
case INT_STA_REG:
/**
*Here clear interrupt status,and find the data
*interrupt status reg.
*/
interrupt_sta_reg_num++;
if (interrupt_sta_reg_num == 1)
status_reg = thermal_reg;
writel((thermal_reg->value),
ths_data->base_addr + (thermal_reg->offset));
break;
case SHT_TMP_REG:
ths_driver_init_shut_temp_reg(ths_data, thermal_reg,
cpara);
break;
case TDATA_REG:
if (!strcmp("THS_0_DATA_REG", thermal_reg->name))
data_reg = thermal_reg;
break;
default:
break;
}
thsprintk("reg_name=%s, offset=0x%x, value=%x, reg_value=%x\n",
thermal_reg->name,
thermal_reg->offset,
thermal_reg->value,
readl(ths_data->base_addr + (thermal_reg->offset)));
thermal_reg++;
}
ths_driver_write_efuse_to_reg(ths_data, calibration_reg_num, cali_reg);
ths_driver_enable_enreg(ths_data, enable_reg);
return 0;
}
static void ths_driver_disable_old_new_enreg(struct sunxi_ths_data *ths_data,
struct thermal_reg *reg)
{
writel(DISABLE_EN_REG, ths_data->base_addr + reg->offset);
}
static void ths_driver_disable_chop_reg(struct sunxi_ths_data *ths_data)
{
writel(0, ths_data->base_addr + 0x04);
}
void ths_driver_disable_reg(struct sunxi_ths_data *ths_data)
{
thsprintk("clear enbale config!\n");
if (ths_data->ths_driver_version == 1)
ths_driver_disable_chop_reg(ths_data);
ths_driver_disable_old_new_enreg(ths_data, enable_reg);
}