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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
|
/*
* OMAP4 Bandgap temperature sensor driver
*
* Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
* Author: J Keerthy <j-keerthy@ti.com>
* Author: Moiz Sonasath <m-sonasath@ti.com>
* Couple of fixes, DT and MFD adaptation:
* Eduardo Valentin <eduardo.valentin@ti.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.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include <linux/module.h>
#include <linux/export.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/reboot.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/of_irq.h>
#include <linux/io.h>
#include "omap-bandgap.h"
/*** Helper functions to access registers and their bitfields ***/
/**
* omap_bandgap_readl() - simple read helper function
* @bgp: pointer to omap_bandgap structure
* @reg: desired register (offset) to be read
*
* Helper function to read bandgap registers. It uses the io remapped area.
* Returns the register value.
*/
static u32 omap_bandgap_readl(struct omap_bandgap *bgp, u32 reg)
{
return readl(bgp->base + reg);
}
/**
* omap_bandgap_writel() - simple write helper function
* @bgp: pointer to omap_bandgap structure
* @val: desired register value to be written
* @reg: desired register (offset) to be written
*
* Helper function to write bandgap registers. It uses the io remapped area.
*/
static void omap_bandgap_writel(struct omap_bandgap *bgp, u32 val, u32 reg)
{
writel(val, bgp->base + reg);
}
/**
* DOC: macro to update bits.
*
* RMW_BITS() - used to read, modify and update bandgap bitfields.
* The value passed will be shifted.
*/
#define RMW_BITS(bgp, id, reg, mask, val) \
do { \
struct temp_sensor_registers *t; \
u32 r; \
\
t = bgp->conf->sensors[(id)].registers; \
r = omap_bandgap_readl(bgp, t->reg); \
r &= ~t->mask; \
r |= (val) << __ffs(t->mask); \
omap_bandgap_writel(bgp, r, t->reg); \
} while (0)
/*** Basic helper functions ***/
/**
* omap_bandgap_power() - controls the power state of a bandgap device
* @bgp: pointer to omap_bandgap structure
* @on: desired power state (1 - on, 0 - off)
*
* Used to power on/off a bandgap device instance. Only used on those
* that features tempsoff bit.
*/
static int omap_bandgap_power(struct omap_bandgap *bgp, bool on)
{
int i;
if (!OMAP_BANDGAP_HAS(bgp, POWER_SWITCH))
goto exit;
for (i = 0; i < bgp->conf->sensor_count; i++)
/* active on 0 */
RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
exit:
return 0;
}
/**
* omap_bandgap_read_temp() - helper function to read sensor temperature
* @bgp: pointer to omap_bandgap structure
* @id: bandgap sensor id
*
* Function to concentrate the steps to read sensor temperature register.
* This function is desired because, depending on bandgap device version,
* it might be needed to freeze the bandgap state machine, before fetching
* the register value.
*/
static u32 omap_bandgap_read_temp(struct omap_bandgap *bgp, int id)
{
struct temp_sensor_registers *tsr;
u32 temp, reg;
tsr = bgp->conf->sensors[id].registers;
reg = tsr->temp_sensor_ctrl;
if (OMAP_BANDGAP_HAS(bgp, FREEZE_BIT)) {
RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
/*
* In case we cannot read from cur_dtemp / dtemp_0,
* then we read from the last valid temp read
*/
reg = tsr->ctrl_dtemp_1;
}
/* read temperature */
temp = omap_bandgap_readl(bgp, reg);
temp &= tsr->bgap_dtemp_mask;
if (OMAP_BANDGAP_HAS(bgp, FREEZE_BIT))
RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
return temp;
}
/*** IRQ handlers ***/
/**
* omap_bandgap_talert_irq_handler() - handles Temperature alert IRQs
* @irq: IRQ number
* @data: private data (struct omap_bandgap *)
*
* This is the Talert handler. Use it only if bandgap device features
* HAS(TALERT). This handler goes over all sensors and checks their
* conditions and acts accordingly. In case there are events pending,
* it will reset the event mask to wait for the opposite event (next event).
* Every time there is a new event, it will be reported to thermal layer.
*/
static irqreturn_t omap_bandgap_talert_irq_handler(int irq, void *data)
{
struct omap_bandgap *bgp = data;
struct temp_sensor_registers *tsr;
u32 t_hot = 0, t_cold = 0, ctrl;
int i;
spin_lock(&bgp->lock);
for (i = 0; i < bgp->conf->sensor_count; i++) {
tsr = bgp->conf->sensors[i].registers;
ctrl = omap_bandgap_readl(bgp, tsr->bgap_status);
/* Read the status of t_hot */
t_hot = ctrl & tsr->status_hot_mask;
/* Read the status of t_cold */
t_cold = ctrl & tsr->status_cold_mask;
if (!t_cold && !t_hot)
continue;
ctrl = omap_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
/*
* One TALERT interrupt: Two sources
* If the interrupt is due to t_hot then mask t_hot and
* and unmask t_cold else mask t_cold and unmask t_hot
*/
if (t_hot) {
ctrl &= ~tsr->mask_hot_mask;
ctrl |= tsr->mask_cold_mask;
} else if (t_cold) {
ctrl &= ~tsr->mask_cold_mask;
ctrl |= tsr->mask_hot_mask;
}
omap_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
dev_dbg(bgp->dev,
"%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
__func__, bgp->conf->sensors[i].domain,
t_hot, t_cold);
/* report temperature to whom may concern */
if (bgp->conf->report_temperature)
bgp->conf->report_temperature(bgp, i);
}
spin_unlock(&bgp->lock);
return IRQ_HANDLED;
}
/**
* omap_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
* @irq: IRQ number
* @data: private data (unused)
*
* This is the Tshut handler. Use it only if bandgap device features
* HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
* the system.
*/
static irqreturn_t omap_bandgap_tshut_irq_handler(int irq, void *data)
{
pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
__func__);
orderly_poweroff(true);
return IRQ_HANDLED;
}
/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
/**
* omap_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
* @bgp: struct omap_bandgap pointer
* @adc_val: value in ADC representation
* @t: address where to write the resulting temperature in mCelsius
*
* Simple conversion from ADC representation to mCelsius. In case the ADC value
* is out of the ADC conv table range, it returns -ERANGE, 0 on success.
* The conversion table is indexed by the ADC values.
*/
static
int omap_bandgap_adc_to_mcelsius(struct omap_bandgap *bgp,
int adc_val, int *t)
{
struct omap_bandgap_data *conf = bgp->conf;
int ret = 0;
/* look up for temperature in the table and return the temperature */
if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
ret = -ERANGE;
goto exit;
}
*t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
exit:
return ret;
}
/**
* omap_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
* @bgp: struct omap_bandgap pointer
* @temp: value in mCelsius
* @adc: address where to write the resulting temperature in ADC representation
*
* Simple conversion from mCelsius to ADC values. In case the temp value
* is out of the ADC conv table range, it returns -ERANGE, 0 on success.
* The conversion table is indexed by the ADC values.
*/
static
int omap_bandgap_mcelsius_to_adc(struct omap_bandgap *bgp, long temp,
int *adc)
{
struct omap_bandgap_data *conf = bgp->conf;
const int *conv_table = bgp->conf->conv_table;
int high, low, mid, ret = 0;
low = 0;
high = conf->adc_end_val - conf->adc_start_val;
mid = (high + low) / 2;
if (temp < conv_table[low] || temp > conv_table[high]) {
ret = -ERANGE;
goto exit;
}
while (low < high) {
if (temp < conv_table[mid])
high = mid - 1;
else
low = mid + 1;
mid = (low + high) / 2;
}
*adc = conf->adc_start_val + low;
exit:
return ret;
}
/**
* omap_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
* @bgp: struct omap_bandgap pointer
* @adc_val: temperature value in ADC representation
* @hyst_val: hysteresis value in mCelsius
* @sum: address where to write the resulting temperature (in ADC scale)
*
* Adds an hysteresis value (in mCelsius) to a ADC temperature value.
* Returns 0 on success, -ERANGE otherwise.
*/
static
int omap_bandgap_add_hyst(struct omap_bandgap *bgp, int adc_val,
int hyst_val, u32 *sum)
{
int temp, ret;
/*
* Need to add in the mcelsius domain, so we have a temperature
* the conv_table range
*/
ret = omap_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
if (ret < 0)
goto exit;
temp += hyst_val;
ret = omap_bandgap_mcelsius_to_adc(bgp, temp, sum);
exit:
return ret;
}
/*** Helper functions handling device Alert/Shutdown signals ***/
/**
* omap_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
* @bgp: struct omap_bandgap pointer
* @t_hot: hot temperature value to trigger alert signal
* @t_cold: cold temperature value to trigger alert signal
*
* Checks the requested t_hot and t_cold values and configures the IRQ event
* masks accordingly. Call this function only if bandgap features HAS(TALERT).
*/
static void omap_bandgap_unmask_interrupts(struct omap_bandgap *bgp, int id,
u32 t_hot, u32 t_cold)
{
struct temp_sensor_registers *tsr;
u32 temp, reg_val;
/* Read the current on die temperature */
temp = omap_bandgap_read_temp(bgp, id);
tsr = bgp->conf->sensors[id].registers;
reg_val = omap_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
if (temp < t_hot)
reg_val |= tsr->mask_hot_mask;
else
reg_val &= ~tsr->mask_hot_mask;
if (t_cold < temp)
reg_val |= tsr->mask_cold_mask;
else
reg_val &= ~tsr->mask_cold_mask;
omap_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
}
/**
* omap_bandgap_update_alert_threshold() - sequence to update thresholds
* @bgp: struct omap_bandgap pointer
* @id: bandgap sensor id
* @val: value (ADC) of a new threshold
* @hot: desired threshold to be updated. true if threshold hot, false if
* threshold cold
*
* It will program the required thresholds (hot and cold) for TALERT signal.
* This function can be used to update t_hot or t_cold, depending on @hot value.
* It checks the resulting t_hot and t_cold values, based on the new passed @val
* and configures the thresholds so that t_hot is always greater than t_cold.
* Call this function only if bandgap features HAS(TALERT).
*/
static
int omap_bandgap_update_alert_threshold(struct omap_bandgap *bgp, int id,
int val, bool hot)
{
struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
struct temp_sensor_registers *tsr;
u32 thresh_val, reg_val, t_hot, t_cold;
int err = 0;
tsr = bgp->conf->sensors[id].registers;
/* obtain the current value */
thresh_val = omap_bandgap_readl(bgp, tsr->bgap_threshold);
t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
__ffs(tsr->threshold_tcold_mask);
t_hot = (thresh_val & tsr->threshold_thot_mask) >>
__ffs(tsr->threshold_thot_mask);
if (hot)
t_hot = val;
else
t_cold = val;
if (t_cold < t_hot) {
if (hot)
err = omap_bandgap_add_hyst(bgp, t_hot,
-ts_data->hyst_val,
&t_cold);
else
err = omap_bandgap_add_hyst(bgp, t_cold,
ts_data->hyst_val,
&t_hot);
}
/* write the new threshold values */
reg_val = thresh_val & ~tsr->threshold_thot_mask;
reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask));
reg_val |= thresh_val & ~tsr->threshold_tcold_mask;
reg_val |= (t_cold << __ffs(tsr->threshold_tcold_mask));
omap_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
if (err) {
dev_err(bgp->dev, "failed to reprogram thot threshold\n");
err = -EIO;
goto exit;
}
omap_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
exit:
return err;
}
/**
* omap_bandgap_validate() - helper to check the sanity of a struct omap_bandgap
* @bgp: struct omap_bandgap pointer
* @id: bandgap sensor id
*
* Checks if the bandgap pointer is valid and if the sensor id is also
* applicable.
*/
static inline int omap_bandgap_validate(struct omap_bandgap *bgp, int id)
{
int ret = 0;
if (IS_ERR_OR_NULL(bgp)) {
pr_err("%s: invalid bandgap pointer\n", __func__);
ret = -EINVAL;
goto exit;
}
if ((id < 0) || (id >= bgp->conf->sensor_count)) {
dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
__func__, id);
ret = -ERANGE;
}
exit:
return ret;
}
/**
* _omap_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
* @bgp: struct omap_bandgap pointer
* @id: bandgap sensor id
* @val: value (mCelsius) of a new threshold
* @hot: desired threshold to be updated. true if threshold hot, false if
* threshold cold
*
* It will update the required thresholds (hot and cold) for TALERT signal.
* This function can be used to update t_hot or t_cold, depending on @hot value.
* Validates the mCelsius range and update the requested threshold.
* Call this function only if bandgap features HAS(TALERT).
*/
int _omap_bandgap_write_threshold(struct omap_bandgap *bgp, int id, int val,
bool hot)
{
struct temp_sensor_data *ts_data;
struct temp_sensor_registers *tsr;
u32 adc_val;
int ret;
ret = omap_bandgap_validate(bgp, id);
if (ret)
goto exit;
if (!OMAP_BANDGAP_HAS(bgp, TALERT)) {
ret = -ENOTSUPP;
goto exit;
}
ts_data = bgp->conf->sensors[id].ts_data;
tsr = bgp->conf->sensors[id].registers;
if (hot) {
if (val < ts_data->min_temp + ts_data->hyst_val)
ret = -EINVAL;
} else {
if (val > ts_data->max_temp + ts_data->hyst_val)
ret = -EINVAL;
}
if (ret)
goto exit;
ret = omap_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
if (ret < 0)
goto exit;
spin_lock(&bgp->lock);
omap_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
spin_unlock(&bgp->lock);
exit:
return ret;
}
/**
* _omap_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
* @bgp: struct omap_bandgap pointer
* @id: bandgap sensor id
* @val: value (mCelsius) of a threshold
* @hot: desired threshold to be read. true if threshold hot, false if
* threshold cold
*
* It will fetch the required thresholds (hot and cold) for TALERT signal.
* This function can be used to read t_hot or t_cold, depending on @hot value.
* Call this function only if bandgap features HAS(TALERT).
*/
int _omap_bandgap_read_threshold(struct omap_bandgap *bgp, int id,
int *val, bool hot)
{
struct temp_sensor_registers *tsr;
u32 temp, mask;
int ret = 0;
ret = omap_bandgap_validate(bgp, id);
if (ret)
goto exit;
if (!OMAP_BANDGAP_HAS(bgp, TALERT)) {
ret = -ENOTSUPP;
goto exit;
}
tsr = bgp->conf->sensors[id].registers;
if (hot)
mask = tsr->threshold_thot_mask;
else
mask = tsr->threshold_tcold_mask;
temp = omap_bandgap_readl(bgp, tsr->bgap_threshold);
temp = (temp & mask) >> __ffs(mask);
ret |= omap_bandgap_adc_to_mcelsius(bgp, temp, &temp);
if (ret) {
dev_err(bgp->dev, "failed to read thot\n");
ret = -EIO;
goto exit;
}
*val = temp;
exit:
return ret;
}
/*** Exposed APIs ***/
/**
* omap_bandgap_read_thot() - reads sensor current thot
* @bgp - pointer to bandgap instance
* @id - sensor id
* @thot - resulting current thot value
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_read_thot(struct omap_bandgap *bgp, int id,
int *thot)
{
return _omap_bandgap_read_threshold(bgp, id, thot, true);
}
/**
* omap_bandgap_write_thot() - sets sensor current thot
* @bgp - pointer to bandgap instance
* @id - sensor id
* @val - desired thot value
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_write_thot(struct omap_bandgap *bgp, int id, int val)
{
return _omap_bandgap_write_threshold(bgp, id, val, true);
}
/**
* omap_bandgap_read_tcold() - reads sensor current tcold
* @bgp - pointer to bandgap instance
* @id - sensor id
* @tcold - resulting current tcold value
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_read_tcold(struct omap_bandgap *bgp, int id,
int *tcold)
{
return _omap_bandgap_read_threshold(bgp, id, tcold, false);
}
/**
* omap_bandgap_write_tcold() - sets the sensor tcold
* @bgp - pointer to bandgap instance
* @id - sensor id
* @val - desired tcold value
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_write_tcold(struct omap_bandgap *bgp, int id, int val)
{
return _omap_bandgap_write_threshold(bgp, id, val, false);
}
/**
* omap_bandgap_read_update_interval() - read the sensor update interval
* @bgp - pointer to bandgap instance
* @id - sensor id
* @interval - resulting update interval in miliseconds
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_read_update_interval(struct omap_bandgap *bgp, int id,
int *interval)
{
struct temp_sensor_registers *tsr;
u32 time;
int ret;
ret = omap_bandgap_validate(bgp, id);
if (ret)
return ret;
if (!OMAP_BANDGAP_HAS(bgp, COUNTER))
return -ENOTSUPP;
tsr = bgp->conf->sensors[id].registers;
time = omap_bandgap_readl(bgp, tsr->bgap_counter);
time = (time & tsr->counter_mask) >> __ffs(tsr->counter_mask);
time = time * 1000 / bgp->clk_rate;
*interval = time;
return 0;
}
/**
* omap_bandgap_write_update_interval() - set the update interval
* @bgp - pointer to bandgap instance
* @id - sensor id
* @interval - desired update interval in miliseconds
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_write_update_interval(struct omap_bandgap *bgp,
int id, u32 interval)
{
int ret = omap_bandgap_validate(bgp, id);
if (ret)
return ret;
if (!OMAP_BANDGAP_HAS(bgp, COUNTER))
return -ENOTSUPP;
interval = interval * bgp->clk_rate / 1000;
spin_lock(&bgp->lock);
RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
spin_unlock(&bgp->lock);
return 0;
}
/**
* omap_bandgap_read_temperature() - report current temperature
* @bgp - pointer to bandgap instance
* @id - sensor id
* @temperature - resulting temperature
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_read_temperature(struct omap_bandgap *bgp, int id,
int *temperature)
{
u32 temp;
int ret;
ret = omap_bandgap_validate(bgp, id);
if (ret)
return ret;
spin_lock(&bgp->lock);
temp = omap_bandgap_read_temp(bgp, id);
spin_unlock(&bgp->lock);
ret |= omap_bandgap_adc_to_mcelsius(bgp, temp, &temp);
if (ret)
return -EIO;
*temperature = temp;
return 0;
}
/**
* omap_bandgap_set_sensor_data() - helper function to store thermal
* framework related data.
* @bgp - pointer to bandgap instance
* @id - sensor id
* @data - thermal framework related data to be stored
*
* returns 0 on success or the proper error code
*/
int omap_bandgap_set_sensor_data(struct omap_bandgap *bgp, int id,
void *data)
{
int ret = omap_bandgap_validate(bgp, id);
if (ret)
return ret;
bgp->conf->sensors[id].data = data;
return 0;
}
/**
* omap_bandgap_get_sensor_data() - helper function to get thermal
* framework related data.
* @bgp - pointer to bandgap instance
* @id - sensor id
*
* returns data stored by set function with sensor id on success or NULL
*/
void *omap_bandgap_get_sensor_data(struct omap_bandgap *bgp, int id)
{
int ret = omap_bandgap_validate(bgp, id);
if (ret)
return ERR_PTR(ret);
return bgp->conf->sensors[id].data;
}
/*** Helper functions used during device initialization ***/
/**
* omap_bandgap_force_single_read() - executes 1 single ADC conversion
* @bgp: pointer to struct omap_bandgap
* @id: sensor id which it is desired to read 1 temperature
*
* Used to initialize the conversion state machine and set it to a valid
* state. Called during device initialization and context restore events.
*/
static int
omap_bandgap_force_single_read(struct omap_bandgap *bgp, int id)
{
u32 temp = 0, counter = 1000;
/* Select single conversion mode */
if (OMAP_BANDGAP_HAS(bgp, MODE_CONFIG))
RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
/* Start of Conversion = 1 */
RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
/* Wait until DTEMP is updated */
temp = omap_bandgap_read_temp(bgp, id);
while ((temp == 0) && --counter)
temp = omap_bandgap_read_temp(bgp, id);
/* REVISIT: Check correct condition for end of conversion */
/* Start of Conversion = 0 */
RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
return 0;
}
/**
* omap_bandgap_set_continous_mode() - One time enabling of continuous mode
* @bgp: pointer to struct omap_bandgap
*
* Call this function only if HAS(MODE_CONFIG) is set. As this driver may
* be used for junction temperature monitoring, it is desirable that the
* sensors are operational all the time, so that alerts are generated
* properly.
*/
static int omap_bandgap_set_continuous_mode(struct omap_bandgap *bgp)
{
int i;
for (i = 0; i < bgp->conf->sensor_count; i++) {
/* Perform a single read just before enabling continuous */
omap_bandgap_force_single_read(bgp, i);
RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
}
return 0;
}
/**
* omap_bandgap_tshut_init() - setup and initialize tshut handling
* @bgp: pointer to struct omap_bandgap
* @pdev: pointer to device struct platform_device
*
* Call this function only in case the bandgap features HAS(TSHUT).
* In this case, the driver needs to handle the TSHUT signal as an IRQ.
* The IRQ is wired as a GPIO, and for this purpose, it is required
* to specify which GPIO line is used. TSHUT IRQ is fired anytime
* one of the bandgap sensors violates the TSHUT high/hot threshold.
* And in that case, the system must go off.
*/
static int omap_bandgap_tshut_init(struct omap_bandgap *bgp,
struct platform_device *pdev)
{
int gpio_nr = bgp->tshut_gpio;
int status;
/* Request for gpio_86 line */
status = gpio_request(gpio_nr, "tshut");
if (status < 0) {
dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
return status;
}
status = gpio_direction_input(gpio_nr);
if (status) {
dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
return status;
}
status = request_irq(gpio_to_irq(gpio_nr),
omap_bandgap_tshut_irq_handler,
IRQF_TRIGGER_RISING, "tshut",
NULL);
if (status) {
gpio_free(gpio_nr);
dev_err(bgp->dev, "request irq failed for TSHUT");
}
return 0;
}
/**
* omap_bandgap_alert_init() - setup and initialize talert handling
* @bgp: pointer to struct omap_bandgap
* @pdev: pointer to device struct platform_device
*
* Call this function only in case the bandgap features HAS(TALERT).
* In this case, the driver needs to handle the TALERT signals as an IRQs.
* TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
* are violated. In these situation, the driver must reprogram the thresholds,
* accordingly to specified policy.
*/
static int omap_bandgap_talert_init(struct omap_bandgap *bgp,
struct platform_device *pdev)
{
int ret;
bgp->irq = platform_get_irq(pdev, 0);
if (bgp->irq < 0) {
dev_err(&pdev->dev, "get_irq failed\n");
return bgp->irq;
}
ret = request_threaded_irq(bgp->irq, NULL,
omap_bandgap_talert_irq_handler,
IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
"talert", bgp);
if (ret) {
dev_err(&pdev->dev, "Request threaded irq failed.\n");
return ret;
}
return 0;
}
/**
* omap_bandgap_build() - parse DT and setup a struct omap_bandgap
* @bgp: pointer to struct omap_bandgap
* @pdev: pointer to device struct platform_device
*
* Used to read the device tree properties accordingly to the bandgap
* matching version. Based on bandgap version and its capabilities it
* will build a struct omap_bandgap out of the required DT entries.
*/
static const struct of_device_id of_omap_bandgap_match[];
static struct omap_bandgap *omap_bandgap_build(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
const struct of_device_id *of_id;
struct omap_bandgap *bgp;
struct resource *res;
u32 prop;
int i;
/* just for the sake */
if (!node) {
dev_err(&pdev->dev, "no platform information available\n");
return ERR_PTR(-EINVAL);
}
bgp = devm_kzalloc(&pdev->dev, sizeof(struct omap_bandgap),
GFP_KERNEL);
if (!bgp) {
dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
return ERR_PTR(-ENOMEM);
}
of_id = of_match_device(of_omap_bandgap_match, &pdev->dev);
if (of_id)
bgp->conf = of_id->data;
i = 0;
do {
void __iomem *chunk;
res = platform_get_resource(pdev, IORESOURCE_MEM, i);
if (!res)
break;
chunk = devm_ioremap_resource(&pdev->dev, res);
if (i == 0)
bgp->base = chunk;
if (IS_ERR(chunk))
return ERR_CAST(chunk);
i++;
} while (res);
if (OMAP_BANDGAP_HAS(bgp, TSHUT)) {
if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
return ERR_PTR(-EINVAL);
}
bgp->tshut_gpio = prop;
if (!gpio_is_valid(bgp->tshut_gpio)) {
dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
bgp->tshut_gpio);
return ERR_PTR(-EINVAL);
}
}
return bgp;
}
/*** Device driver call backs ***/
static
int omap_bandgap_probe(struct platform_device *pdev)
{
struct omap_bandgap *bgp;
int clk_rate, ret = 0, i;
bgp = omap_bandgap_build(pdev);
if (IS_ERR_OR_NULL(bgp)) {
dev_err(&pdev->dev, "failed to fetch platform data\n");
return PTR_ERR(bgp);
}
bgp->dev = &pdev->dev;
if (OMAP_BANDGAP_HAS(bgp, TSHUT)) {
ret = omap_bandgap_tshut_init(bgp, pdev);
if (ret) {
dev_err(&pdev->dev,
"failed to initialize system tshut IRQ\n");
return ret;
}
}
bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
ret = IS_ERR_OR_NULL(bgp->fclock);
if (ret) {
dev_err(&pdev->dev, "failed to request fclock reference\n");
goto free_irqs;
}
bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
ret = IS_ERR_OR_NULL(bgp->div_clk);
if (ret) {
dev_err(&pdev->dev,
"failed to request div_ts_ck clock ref\n");
goto free_irqs;
}
for (i = 0; i < bgp->conf->sensor_count; i++) {
struct temp_sensor_registers *tsr;
u32 val;
tsr = bgp->conf->sensors[i].registers;
/*
* check if the efuse has a non-zero value if not
* it is an untrimmed sample and the temperatures
* may not be accurate
*/
val = omap_bandgap_readl(bgp, tsr->bgap_efuse);
if (ret || !val)
dev_info(&pdev->dev,
"Non-trimmed BGAP, Temp not accurate\n");
}
clk_rate = clk_round_rate(bgp->div_clk,
bgp->conf->sensors[0].ts_data->max_freq);
if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
clk_rate == 0xffffffff) {
ret = -ENODEV;
dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
goto put_clks;
}
ret = clk_set_rate(bgp->div_clk, clk_rate);
if (ret)
dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
bgp->clk_rate = clk_rate;
if (OMAP_BANDGAP_HAS(bgp, CLK_CTRL))
clk_prepare_enable(bgp->fclock);
spin_lock_init(&bgp->lock);
bgp->dev = &pdev->dev;
platform_set_drvdata(pdev, bgp);
omap_bandgap_power(bgp, true);
/* Set default counter to 1 for now */
if (OMAP_BANDGAP_HAS(bgp, COUNTER))
for (i = 0; i < bgp->conf->sensor_count; i++)
RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
/* Set default thresholds for alert and shutdown */
for (i = 0; i < bgp->conf->sensor_count; i++) {
struct temp_sensor_data *ts_data;
ts_data = bgp->conf->sensors[i].ts_data;
if (OMAP_BANDGAP_HAS(bgp, TALERT)) {
/* Set initial Talert thresholds */
RMW_BITS(bgp, i, bgap_threshold,
threshold_tcold_mask, ts_data->t_cold);
RMW_BITS(bgp, i, bgap_threshold,
threshold_thot_mask, ts_data->t_hot);
/* Enable the alert events */
RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
}
if (OMAP_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
/* Set initial Tshut thresholds */
RMW_BITS(bgp, i, tshut_threshold,
tshut_hot_mask, ts_data->tshut_hot);
RMW_BITS(bgp, i, tshut_threshold,
tshut_cold_mask, ts_data->tshut_cold);
}
}
if (OMAP_BANDGAP_HAS(bgp, MODE_CONFIG))
omap_bandgap_set_continuous_mode(bgp);
/* Set .250 seconds time as default counter */
if (OMAP_BANDGAP_HAS(bgp, COUNTER))
for (i = 0; i < bgp->conf->sensor_count; i++)
RMW_BITS(bgp, i, bgap_counter, counter_mask,
bgp->clk_rate / 4);
/* Every thing is good? Then expose the sensors */
for (i = 0; i < bgp->conf->sensor_count; i++) {
char *domain;
if (bgp->conf->sensors[i].register_cooling)
bgp->conf->sensors[i].register_cooling(bgp, i);
domain = bgp->conf->sensors[i].domain;
if (bgp->conf->expose_sensor)
bgp->conf->expose_sensor(bgp, i, domain);
}
/*
* Enable the Interrupts once everything is set. Otherwise irq handler
* might be called as soon as it is enabled where as rest of framework
* is still getting initialised.
*/
if (OMAP_BANDGAP_HAS(bgp, TALERT)) {
ret = omap_bandgap_talert_init(bgp, pdev);
if (ret) {
dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
i = bgp->conf->sensor_count;
goto disable_clk;
}
}
return 0;
disable_clk:
if (OMAP_BANDGAP_HAS(bgp, CLK_CTRL))
clk_disable_unprepare(bgp->fclock);
put_clks:
clk_put(bgp->fclock);
clk_put(bgp->div_clk);
free_irqs:
if (OMAP_BANDGAP_HAS(bgp, TSHUT)) {
free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
gpio_free(bgp->tshut_gpio);
}
return ret;
}
static
int omap_bandgap_remove(struct platform_device *pdev)
{
struct omap_bandgap *bgp = platform_get_drvdata(pdev);
int i;
/* First thing is to remove sensor interfaces */
for (i = 0; i < bgp->conf->sensor_count; i++) {
if (bgp->conf->sensors[i].register_cooling)
bgp->conf->sensors[i].unregister_cooling(bgp, i);
if (bgp->conf->remove_sensor)
bgp->conf->remove_sensor(bgp, i);
}
omap_bandgap_power(bgp, false);
if (OMAP_BANDGAP_HAS(bgp, CLK_CTRL))
clk_disable_unprepare(bgp->fclock);
clk_put(bgp->fclock);
clk_put(bgp->div_clk);
if (OMAP_BANDGAP_HAS(bgp, TALERT))
free_irq(bgp->irq, bgp);
if (OMAP_BANDGAP_HAS(bgp, TSHUT)) {
free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
gpio_free(bgp->tshut_gpio);
}
return 0;
}
#ifdef CONFIG_PM
static int omap_bandgap_save_ctxt(struct omap_bandgap *bgp)
{
int i;
for (i = 0; i < bgp->conf->sensor_count; i++) {
struct temp_sensor_registers *tsr;
struct temp_sensor_regval *rval;
rval = &bgp->conf->sensors[i].regval;
tsr = bgp->conf->sensors[i].registers;
if (OMAP_BANDGAP_HAS(bgp, MODE_CONFIG))
rval->bg_mode_ctrl = omap_bandgap_readl(bgp,
tsr->bgap_mode_ctrl);
if (OMAP_BANDGAP_HAS(bgp, COUNTER))
rval->bg_counter = omap_bandgap_readl(bgp,
tsr->bgap_counter);
if (OMAP_BANDGAP_HAS(bgp, TALERT)) {
rval->bg_threshold = omap_bandgap_readl(bgp,
tsr->bgap_threshold);
rval->bg_ctrl = omap_bandgap_readl(bgp,
tsr->bgap_mask_ctrl);
}
if (OMAP_BANDGAP_HAS(bgp, TSHUT_CONFIG))
rval->tshut_threshold = omap_bandgap_readl(bgp,
tsr->tshut_threshold);
}
return 0;
}
static int omap_bandgap_restore_ctxt(struct omap_bandgap *bgp)
{
int i;
for (i = 0; i < bgp->conf->sensor_count; i++) {
struct temp_sensor_registers *tsr;
struct temp_sensor_regval *rval;
u32 val = 0;
rval = &bgp->conf->sensors[i].regval;
tsr = bgp->conf->sensors[i].registers;
if (OMAP_BANDGAP_HAS(bgp, COUNTER))
val = omap_bandgap_readl(bgp, tsr->bgap_counter);
if (OMAP_BANDGAP_HAS(bgp, TSHUT_CONFIG))
omap_bandgap_writel(bgp, rval->tshut_threshold,
tsr->tshut_threshold);
/* Force immediate temperature measurement and update
* of the DTEMP field
*/
omap_bandgap_force_single_read(bgp, i);
if (OMAP_BANDGAP_HAS(bgp, COUNTER))
omap_bandgap_writel(bgp, rval->bg_counter,
tsr->bgap_counter);
if (OMAP_BANDGAP_HAS(bgp, MODE_CONFIG))
omap_bandgap_writel(bgp, rval->bg_mode_ctrl,
tsr->bgap_mode_ctrl);
if (OMAP_BANDGAP_HAS(bgp, TALERT)) {
omap_bandgap_writel(bgp, rval->bg_threshold,
tsr->bgap_threshold);
omap_bandgap_writel(bgp, rval->bg_ctrl,
tsr->bgap_mask_ctrl);
}
}
return 0;
}
static int omap_bandgap_suspend(struct device *dev)
{
struct omap_bandgap *bgp = dev_get_drvdata(dev);
int err;
err = omap_bandgap_save_ctxt(bgp);
omap_bandgap_power(bgp, false);
if (OMAP_BANDGAP_HAS(bgp, CLK_CTRL))
clk_disable_unprepare(bgp->fclock);
return err;
}
static int omap_bandgap_resume(struct device *dev)
{
struct omap_bandgap *bgp = dev_get_drvdata(dev);
if (OMAP_BANDGAP_HAS(bgp, CLK_CTRL))
clk_prepare_enable(bgp->fclock);
omap_bandgap_power(bgp, true);
return omap_bandgap_restore_ctxt(bgp);
}
static const struct dev_pm_ops omap_bandgap_dev_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(omap_bandgap_suspend,
omap_bandgap_resume)
};
#define DEV_PM_OPS (&omap_bandgap_dev_pm_ops)
#else
#define DEV_PM_OPS NULL
#endif
static const struct of_device_id of_omap_bandgap_match[] = {
#ifdef CONFIG_OMAP4_THERMAL
{
.compatible = "ti,omap4430-bandgap",
.data = (void *)&omap4430_data,
},
{
.compatible = "ti,omap4460-bandgap",
.data = (void *)&omap4460_data,
},
{
.compatible = "ti,omap4470-bandgap",
.data = (void *)&omap4470_data,
},
#endif
#ifdef CONFIG_OMAP5_THERMAL
{
.compatible = "ti,omap5430-bandgap",
.data = (void *)&omap5430_data,
},
#endif
/* Sentinel */
{ },
};
MODULE_DEVICE_TABLE(of, of_omap_bandgap_match);
static struct platform_driver omap_bandgap_sensor_driver = {
.probe = omap_bandgap_probe,
.remove = omap_bandgap_remove,
.driver = {
.name = "omap-bandgap",
.pm = DEV_PM_OPS,
.of_match_table = of_omap_bandgap_match,
},
};
module_platform_driver(omap_bandgap_sensor_driver);
MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:omap-bandgap");
MODULE_AUTHOR("Texas Instrument Inc.");
|