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#include <linux/module.h>
27#include <linux/export.h>
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/interrupt.h>
31#include <linux/clk.h>
32#include <linux/gpio.h>
33#include <linux/platform_device.h>
34#include <linux/err.h>
35#include <linux/types.h>
36#include <linux/spinlock.h>
37#include <linux/reboot.h>
38#include <linux/of_device.h>
39#include <linux/of_platform.h>
40#include <linux/of_irq.h>
41#include <linux/of_gpio.h>
42#include <linux/io.h>
43
44#include "ti-bandgap.h"
45
46static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
47
48
49
50
51
52
53
54
55
56
57
58static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
59{
60 return readl(bgp->base + reg);
61}
62
63
64
65
66
67
68
69
70
71static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
72{
73 writel(val, bgp->base + reg);
74}
75
76
77
78
79
80
81
82#define RMW_BITS(bgp, id, reg, mask, val) \
83do { \
84 struct temp_sensor_registers *t; \
85 u32 r; \
86 \
87 t = bgp->conf->sensors[(id)].registers; \
88 r = ti_bandgap_readl(bgp, t->reg); \
89 r &= ~t->mask; \
90 r |= (val) << __ffs(t->mask); \
91 ti_bandgap_writel(bgp, r, t->reg); \
92} while (0)
93
94
95
96
97
98
99
100
101
102
103
104
105
106static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
107{
108 int i;
109
110 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
111 return -ENOTSUPP;
112
113 for (i = 0; i < bgp->conf->sensor_count; i++)
114
115 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
116 return 0;
117}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
136{
137 u32 val1, val2;
138
139 val1 = ti_bandgap_readl(bgp, reg);
140 val2 = ti_bandgap_readl(bgp, reg);
141
142
143 if (val1 == val2)
144 return val1;
145
146
147 return ti_bandgap_readl(bgp, reg);
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
163{
164 struct temp_sensor_registers *tsr;
165 u32 temp, reg;
166
167 tsr = bgp->conf->sensors[id].registers;
168 reg = tsr->temp_sensor_ctrl;
169
170 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
171 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
172
173
174
175
176 reg = tsr->ctrl_dtemp_1;
177 }
178
179
180 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
181 temp = ti_errata814_bandgap_read_temp(bgp, reg);
182 else
183 temp = ti_bandgap_readl(bgp, reg);
184
185 temp &= tsr->bgap_dtemp_mask;
186
187 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
188 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
189
190 return temp;
191}
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
209{
210 struct ti_bandgap *bgp = data;
211 struct temp_sensor_registers *tsr;
212 u32 t_hot = 0, t_cold = 0, ctrl;
213 int i;
214
215 spin_lock(&bgp->lock);
216 for (i = 0; i < bgp->conf->sensor_count; i++) {
217 tsr = bgp->conf->sensors[i].registers;
218 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
219
220
221 t_hot = ctrl & tsr->status_hot_mask;
222
223
224 t_cold = ctrl & tsr->status_cold_mask;
225
226 if (!t_cold && !t_hot)
227 continue;
228
229 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
230
231
232
233
234
235 if (t_hot) {
236 ctrl &= ~tsr->mask_hot_mask;
237 ctrl |= tsr->mask_cold_mask;
238 } else if (t_cold) {
239 ctrl &= ~tsr->mask_cold_mask;
240 ctrl |= tsr->mask_hot_mask;
241 }
242
243 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
244
245 dev_dbg(bgp->dev,
246 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
247 __func__, bgp->conf->sensors[i].domain,
248 t_hot, t_cold);
249
250
251 if (bgp->conf->report_temperature)
252 bgp->conf->report_temperature(bgp, i);
253 }
254 spin_unlock(&bgp->lock);
255
256 return IRQ_HANDLED;
257}
258
259
260
261
262
263
264
265
266
267
268
269
270static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
271{
272 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
273 __func__);
274
275 orderly_poweroff(true);
276
277 return IRQ_HANDLED;
278}
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295static
296int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
297{
298 const struct ti_bandgap_data *conf = bgp->conf;
299
300
301 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
302 return -ERANGE;
303
304 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
305 return 0;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321static
322int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
323{
324 const struct ti_bandgap_data *conf = bgp->conf;
325 const int *conv_table = bgp->conf->conv_table;
326 int high, low, mid;
327
328 low = 0;
329 high = conf->adc_end_val - conf->adc_start_val;
330 mid = (high + low) / 2;
331
332 if (temp < conv_table[low] || temp > conv_table[high])
333 return -ERANGE;
334
335 while (low < high) {
336 if (temp < conv_table[mid])
337 high = mid - 1;
338 else
339 low = mid + 1;
340 mid = (low + high) / 2;
341 }
342
343 *adc = conf->adc_start_val + low;
344 return 0;
345}
346
347
348
349
350
351
352
353
354
355
356
357
358static
359int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
360 u32 *sum)
361{
362 int temp, ret;
363
364
365
366
367
368 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
369 if (ret < 0)
370 return ret;
371
372 temp += hyst_val;
373
374 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
375 return ret;
376}
377
378
379
380
381
382
383
384
385
386
387
388
389
390static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
391 u32 t_hot, u32 t_cold)
392{
393 struct temp_sensor_registers *tsr;
394 u32 temp, reg_val;
395
396
397 temp = ti_bandgap_read_temp(bgp, id);
398
399 tsr = bgp->conf->sensors[id].registers;
400 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
401
402 if (temp < t_hot)
403 reg_val |= tsr->mask_hot_mask;
404 else
405 reg_val &= ~tsr->mask_hot_mask;
406
407 if (t_cold < temp)
408 reg_val |= tsr->mask_cold_mask;
409 else
410 reg_val &= ~tsr->mask_cold_mask;
411 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
412}
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
431 int val, bool hot)
432{
433 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
434 struct temp_sensor_registers *tsr;
435 u32 thresh_val, reg_val, t_hot, t_cold, ctrl;
436 int err = 0;
437
438 tsr = bgp->conf->sensors[id].registers;
439
440
441 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
442 t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
443 __ffs(tsr->threshold_tcold_mask);
444 t_hot = (thresh_val & tsr->threshold_thot_mask) >>
445 __ffs(tsr->threshold_thot_mask);
446 if (hot)
447 t_hot = val;
448 else
449 t_cold = val;
450
451 if (t_cold > t_hot) {
452 if (hot)
453 err = ti_bandgap_add_hyst(bgp, t_hot,
454 -ts_data->hyst_val,
455 &t_cold);
456 else
457 err = ti_bandgap_add_hyst(bgp, t_cold,
458 ts_data->hyst_val,
459 &t_hot);
460 }
461
462
463 reg_val = thresh_val &
464 ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
465 reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
466 (t_cold << __ffs(tsr->threshold_tcold_mask));
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
483
484 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
485
486 if (hot)
487 ctrl &= ~tsr->mask_hot_mask;
488 else
489 ctrl &= ~tsr->mask_cold_mask;
490
491 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
492 }
493
494
495 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
496
497 if (TI_BANDGAP_HAS(bgp, ERRATA_813)) {
498
499 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
500 if (hot)
501 ctrl |= tsr->mask_hot_mask;
502 else
503 ctrl |= tsr->mask_cold_mask;
504
505 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
506 }
507
508 if (err) {
509 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
510 err = -EIO;
511 goto exit;
512 }
513
514 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
515exit:
516 return err;
517}
518
519
520
521
522
523
524
525
526
527
528
529
530static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
531{
532 if (!bgp || IS_ERR(bgp)) {
533 pr_err("%s: invalid bandgap pointer\n", __func__);
534 return -EINVAL;
535 }
536
537 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
538 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
539 __func__, id);
540 return -ERANGE;
541 }
542
543 return 0;
544}
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
562 bool hot)
563{
564 struct temp_sensor_data *ts_data;
565 struct temp_sensor_registers *tsr;
566 u32 adc_val;
567 int ret;
568
569 ret = ti_bandgap_validate(bgp, id);
570 if (ret)
571 return ret;
572
573 if (!TI_BANDGAP_HAS(bgp, TALERT))
574 return -ENOTSUPP;
575
576 ts_data = bgp->conf->sensors[id].ts_data;
577 tsr = bgp->conf->sensors[id].registers;
578 if (hot) {
579 if (val < ts_data->min_temp + ts_data->hyst_val)
580 ret = -EINVAL;
581 } else {
582 if (val > ts_data->max_temp + ts_data->hyst_val)
583 ret = -EINVAL;
584 }
585
586 if (ret)
587 return ret;
588
589 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
590 if (ret < 0)
591 return ret;
592
593 spin_lock(&bgp->lock);
594 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
595 spin_unlock(&bgp->lock);
596 return ret;
597}
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
615 int *val, bool hot)
616{
617 struct temp_sensor_registers *tsr;
618 u32 temp, mask;
619 int ret = 0;
620
621 ret = ti_bandgap_validate(bgp, id);
622 if (ret)
623 goto exit;
624
625 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
626 ret = -ENOTSUPP;
627 goto exit;
628 }
629
630 tsr = bgp->conf->sensors[id].registers;
631 if (hot)
632 mask = tsr->threshold_thot_mask;
633 else
634 mask = tsr->threshold_tcold_mask;
635
636 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
637 temp = (temp & mask) >> __ffs(mask);
638 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
639 if (ret) {
640 dev_err(bgp->dev, "failed to read thot\n");
641 ret = -EIO;
642 goto exit;
643 }
644
645 *val = temp;
646
647exit:
648 return ret;
649}
650
651
652
653
654
655
656
657
658
659
660
661int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
662{
663 return _ti_bandgap_read_threshold(bgp, id, thot, true);
664}
665
666
667
668
669
670
671
672
673
674int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
675{
676 return _ti_bandgap_write_threshold(bgp, id, val, true);
677}
678
679
680
681
682
683
684
685
686
687int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
688{
689 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
690}
691
692
693
694
695
696
697
698
699
700int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
701{
702 return _ti_bandgap_write_threshold(bgp, id, val, false);
703}
704
705
706
707
708
709
710
711static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
712 int *interval)
713{
714 struct temp_sensor_registers *tsr;
715 int time;
716
717 tsr = bgp->conf->sensors[id].registers;
718 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
719 time = (time & tsr->counter_mask) >>
720 __ffs(tsr->counter_mask);
721 time = time * 1000 / bgp->clk_rate;
722 *interval = time;
723}
724
725
726
727
728
729
730
731static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
732 int *interval)
733{
734 struct temp_sensor_registers *tsr;
735 int reg_val;
736
737 tsr = bgp->conf->sensors[id].registers;
738
739 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
740 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
741 __ffs(tsr->mask_counter_delay_mask);
742 switch (reg_val) {
743 case 0:
744 *interval = 0;
745 break;
746 case 1:
747 *interval = 1;
748 break;
749 case 2:
750 *interval = 10;
751 break;
752 case 3:
753 *interval = 100;
754 break;
755 case 4:
756 *interval = 250;
757 break;
758 case 5:
759 *interval = 500;
760 break;
761 default:
762 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
763 reg_val);
764 }
765}
766
767
768
769
770
771
772
773
774
775int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
776 int *interval)
777{
778 int ret = 0;
779
780 ret = ti_bandgap_validate(bgp, id);
781 if (ret)
782 goto exit;
783
784 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
785 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
786 ret = -ENOTSUPP;
787 goto exit;
788 }
789
790 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
791 ti_bandgap_read_counter(bgp, id, interval);
792 goto exit;
793 }
794
795 ti_bandgap_read_counter_delay(bgp, id, interval);
796exit:
797 return ret;
798}
799
800
801
802
803
804
805
806
807
808static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
809 u32 interval)
810{
811 int rval;
812
813 switch (interval) {
814 case 0:
815 rval = 0x0;
816 break;
817 case 1:
818 rval = 0x1;
819 break;
820 case 10:
821 rval = 0x2;
822 break;
823 case 100:
824 rval = 0x3;
825 break;
826 case 250:
827 rval = 0x4;
828 break;
829 case 500:
830 rval = 0x5;
831 break;
832 default:
833 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
834 return -EINVAL;
835 }
836
837 spin_lock(&bgp->lock);
838 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
839 spin_unlock(&bgp->lock);
840
841 return 0;
842}
843
844
845
846
847
848
849
850static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
851 u32 interval)
852{
853 interval = interval * bgp->clk_rate / 1000;
854 spin_lock(&bgp->lock);
855 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
856 spin_unlock(&bgp->lock);
857}
858
859
860
861
862
863
864
865
866
867int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
868 int id, u32 interval)
869{
870 int ret = ti_bandgap_validate(bgp, id);
871 if (ret)
872 goto exit;
873
874 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
875 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
876 ret = -ENOTSUPP;
877 goto exit;
878 }
879
880 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
881 ti_bandgap_write_counter(bgp, id, interval);
882 goto exit;
883 }
884
885 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
886exit:
887 return ret;
888}
889
890
891
892
893
894
895
896
897
898int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
899 int *temperature)
900{
901 u32 temp;
902 int ret;
903
904 ret = ti_bandgap_validate(bgp, id);
905 if (ret)
906 return ret;
907
908 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
909 ret = ti_bandgap_force_single_read(bgp, id);
910 if (ret)
911 return ret;
912 }
913
914 spin_lock(&bgp->lock);
915 temp = ti_bandgap_read_temp(bgp, id);
916 spin_unlock(&bgp->lock);
917
918 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
919 if (ret)
920 return -EIO;
921
922 *temperature = temp;
923
924 return 0;
925}
926
927
928
929
930
931
932
933
934
935
936int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
937{
938 int ret = ti_bandgap_validate(bgp, id);
939 if (ret)
940 return ret;
941
942 bgp->regval[id].data = data;
943
944 return 0;
945}
946
947
948
949
950
951
952
953
954
955void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
956{
957 int ret = ti_bandgap_validate(bgp, id);
958 if (ret)
959 return ERR_PTR(ret);
960
961 return bgp->regval[id].data;
962}
963
964
965
966
967
968
969
970
971
972
973
974
975
976static int
977ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
978{
979 u32 counter = 1000;
980 struct temp_sensor_registers *tsr;
981
982
983 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
984 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
985
986
987 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
988
989
990 tsr = bgp->conf->sensors[id].registers;
991
992 while (--counter) {
993 if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
994 tsr->bgap_eocz_mask)
995 break;
996 }
997
998
999 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
1000
1001
1002 counter = 1000;
1003 while (--counter) {
1004 if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
1005 tsr->bgap_eocz_mask))
1006 break;
1007 }
1008
1009 return 0;
1010}
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
1024{
1025 int i;
1026
1027 for (i = 0; i < bgp->conf->sensor_count; i++) {
1028
1029 ti_bandgap_force_single_read(bgp, i);
1030 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
1031 }
1032
1033 return 0;
1034}
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
1052{
1053 struct temp_sensor_registers *tsr;
1054 u32 temp1, temp2, reg1, reg2;
1055 int t1, t2, interval, ret = 0;
1056
1057 ret = ti_bandgap_validate(bgp, id);
1058 if (ret)
1059 goto exit;
1060
1061 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
1062 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
1063 ret = -ENOTSUPP;
1064 goto exit;
1065 }
1066
1067 spin_lock(&bgp->lock);
1068
1069 tsr = bgp->conf->sensors[id].registers;
1070
1071
1072 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
1073 reg1 = tsr->ctrl_dtemp_1;
1074 reg2 = tsr->ctrl_dtemp_2;
1075
1076
1077 temp1 = ti_bandgap_readl(bgp, reg1);
1078 temp1 &= tsr->bgap_dtemp_mask;
1079
1080 temp2 = ti_bandgap_readl(bgp, reg2);
1081 temp2 &= tsr->bgap_dtemp_mask;
1082
1083
1084 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
1085 if (ret)
1086 goto unfreeze;
1087
1088 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1089 if (ret)
1090 goto unfreeze;
1091
1092
1093 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
1094 if (ret)
1095 goto unfreeze;
1096
1097
1098 if (interval == 0)
1099 interval = 1;
1100
1101 *trend = (t1 - t2) / interval;
1102
1103 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
1104 t1, t2, *trend);
1105
1106unfreeze:
1107 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1108 spin_unlock(&bgp->lock);
1109exit:
1110 return ret;
1111}
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1128 struct platform_device *pdev)
1129{
1130 int gpio_nr = bgp->tshut_gpio;
1131 int status;
1132
1133
1134 status = gpio_request(gpio_nr, "tshut");
1135 if (status < 0) {
1136 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
1137 return status;
1138 }
1139 status = gpio_direction_input(gpio_nr);
1140 if (status) {
1141 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
1142 return status;
1143 }
1144
1145 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1146 IRQF_TRIGGER_RISING, "tshut", NULL);
1147 if (status) {
1148 gpio_free(gpio_nr);
1149 dev_err(bgp->dev, "request irq failed for TSHUT");
1150 }
1151
1152 return 0;
1153}
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1169 struct platform_device *pdev)
1170{
1171 int ret;
1172
1173 bgp->irq = platform_get_irq(pdev, 0);
1174 if (bgp->irq < 0) {
1175 dev_err(&pdev->dev, "get_irq failed\n");
1176 return bgp->irq;
1177 }
1178 ret = request_threaded_irq(bgp->irq, NULL,
1179 ti_bandgap_talert_irq_handler,
1180 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1181 "talert", bgp);
1182 if (ret) {
1183 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1184 return ret;
1185 }
1186
1187 return 0;
1188}
1189
1190static const struct of_device_id of_ti_bandgap_match[];
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
1203{
1204 struct device_node *node = pdev->dev.of_node;
1205 const struct of_device_id *of_id;
1206 struct ti_bandgap *bgp;
1207 struct resource *res;
1208 int i;
1209
1210
1211 if (!node) {
1212 dev_err(&pdev->dev, "no platform information available\n");
1213 return ERR_PTR(-EINVAL);
1214 }
1215
1216 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
1217 if (!bgp)
1218 return ERR_PTR(-ENOMEM);
1219
1220 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
1221 if (of_id)
1222 bgp->conf = of_id->data;
1223
1224
1225 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
1226 sizeof(*bgp->regval), GFP_KERNEL);
1227 if (!bgp->regval)
1228 return ERR_PTR(-ENOMEM);
1229
1230 i = 0;
1231 do {
1232 void __iomem *chunk;
1233
1234 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1235 if (!res)
1236 break;
1237 chunk = devm_ioremap_resource(&pdev->dev, res);
1238 if (i == 0)
1239 bgp->base = chunk;
1240 if (IS_ERR(chunk))
1241 return ERR_CAST(chunk);
1242
1243 i++;
1244 } while (res);
1245
1246 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1247 bgp->tshut_gpio = of_get_gpio(node, 0);
1248 if (!gpio_is_valid(bgp->tshut_gpio)) {
1249 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
1250 bgp->tshut_gpio);
1251 return ERR_PTR(-EINVAL);
1252 }
1253 }
1254
1255 return bgp;
1256}
1257
1258
1259
1260static
1261int ti_bandgap_probe(struct platform_device *pdev)
1262{
1263 struct ti_bandgap *bgp;
1264 int clk_rate, ret, i;
1265
1266 bgp = ti_bandgap_build(pdev);
1267 if (IS_ERR(bgp)) {
1268 dev_err(&pdev->dev, "failed to fetch platform data\n");
1269 return PTR_ERR(bgp);
1270 }
1271 bgp->dev = &pdev->dev;
1272
1273 if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
1274 dev_warn(&pdev->dev,
1275 "This OMAP thermal sensor is unreliable. You've been warned\n");
1276
1277 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1278 ret = ti_bandgap_tshut_init(bgp, pdev);
1279 if (ret) {
1280 dev_err(&pdev->dev,
1281 "failed to initialize system tshut IRQ\n");
1282 return ret;
1283 }
1284 }
1285
1286 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
1287 if (IS_ERR(bgp->fclock)) {
1288 dev_err(&pdev->dev, "failed to request fclock reference\n");
1289 ret = PTR_ERR(bgp->fclock);
1290 goto free_irqs;
1291 }
1292
1293 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
1294 if (IS_ERR(bgp->div_clk)) {
1295 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
1296 ret = PTR_ERR(bgp->div_clk);
1297 goto put_fclock;
1298 }
1299
1300 for (i = 0; i < bgp->conf->sensor_count; i++) {
1301 struct temp_sensor_registers *tsr;
1302 u32 val;
1303
1304 tsr = bgp->conf->sensors[i].registers;
1305
1306
1307
1308
1309
1310 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
1311 if (!val)
1312 dev_info(&pdev->dev,
1313 "Non-trimmed BGAP, Temp not accurate\n");
1314 }
1315
1316 clk_rate = clk_round_rate(bgp->div_clk,
1317 bgp->conf->sensors[0].ts_data->max_freq);
1318 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
1319 clk_rate <= 0) {
1320 ret = -ENODEV;
1321 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1322 goto put_clks;
1323 }
1324
1325 ret = clk_set_rate(bgp->div_clk, clk_rate);
1326 if (ret)
1327 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1328
1329 bgp->clk_rate = clk_rate;
1330 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1331 clk_prepare_enable(bgp->fclock);
1332
1333
1334 spin_lock_init(&bgp->lock);
1335 bgp->dev = &pdev->dev;
1336 platform_set_drvdata(pdev, bgp);
1337
1338 ti_bandgap_power(bgp, true);
1339
1340
1341 if (TI_BANDGAP_HAS(bgp, COUNTER))
1342 for (i = 0; i < bgp->conf->sensor_count; i++)
1343 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
1344
1345
1346 for (i = 0; i < bgp->conf->sensor_count; i++) {
1347 struct temp_sensor_data *ts_data;
1348
1349 ts_data = bgp->conf->sensors[i].ts_data;
1350
1351 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1352
1353 RMW_BITS(bgp, i, bgap_threshold,
1354 threshold_tcold_mask, ts_data->t_cold);
1355 RMW_BITS(bgp, i, bgap_threshold,
1356 threshold_thot_mask, ts_data->t_hot);
1357
1358 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1359 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
1360 }
1361
1362 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
1363
1364 RMW_BITS(bgp, i, tshut_threshold,
1365 tshut_hot_mask, ts_data->tshut_hot);
1366 RMW_BITS(bgp, i, tshut_threshold,
1367 tshut_cold_mask, ts_data->tshut_cold);
1368 }
1369 }
1370
1371 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1372 ti_bandgap_set_continuous_mode(bgp);
1373
1374
1375 if (TI_BANDGAP_HAS(bgp, COUNTER))
1376 for (i = 0; i < bgp->conf->sensor_count; i++)
1377 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1378 bgp->clk_rate / 4);
1379
1380
1381 for (i = 0; i < bgp->conf->sensor_count; i++) {
1382 char *domain;
1383
1384 if (bgp->conf->sensors[i].register_cooling) {
1385 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1386 if (ret)
1387 goto remove_sensors;
1388 }
1389
1390 if (bgp->conf->expose_sensor) {
1391 domain = bgp->conf->sensors[i].domain;
1392 ret = bgp->conf->expose_sensor(bgp, i, domain);
1393 if (ret)
1394 goto remove_last_cooling;
1395 }
1396 }
1397
1398
1399
1400
1401
1402
1403 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1404 ret = ti_bandgap_talert_init(bgp, pdev);
1405 if (ret) {
1406 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1407 i = bgp->conf->sensor_count;
1408 goto disable_clk;
1409 }
1410 }
1411
1412 return 0;
1413
1414remove_last_cooling:
1415 if (bgp->conf->sensors[i].unregister_cooling)
1416 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1417remove_sensors:
1418 for (i--; i >= 0; i--) {
1419 if (bgp->conf->sensors[i].unregister_cooling)
1420 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1421 if (bgp->conf->remove_sensor)
1422 bgp->conf->remove_sensor(bgp, i);
1423 }
1424 ti_bandgap_power(bgp, false);
1425disable_clk:
1426 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1427 clk_disable_unprepare(bgp->fclock);
1428put_clks:
1429 clk_put(bgp->div_clk);
1430put_fclock:
1431 clk_put(bgp->fclock);
1432free_irqs:
1433 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1434 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1435 gpio_free(bgp->tshut_gpio);
1436 }
1437
1438 return ret;
1439}
1440
1441static
1442int ti_bandgap_remove(struct platform_device *pdev)
1443{
1444 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1445 int i;
1446
1447
1448 for (i = 0; i < bgp->conf->sensor_count; i++) {
1449 if (bgp->conf->sensors[i].unregister_cooling)
1450 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1451
1452 if (bgp->conf->remove_sensor)
1453 bgp->conf->remove_sensor(bgp, i);
1454 }
1455
1456 ti_bandgap_power(bgp, false);
1457
1458 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1459 clk_disable_unprepare(bgp->fclock);
1460 clk_put(bgp->fclock);
1461 clk_put(bgp->div_clk);
1462
1463 if (TI_BANDGAP_HAS(bgp, TALERT))
1464 free_irq(bgp->irq, bgp);
1465
1466 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1467 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1468 gpio_free(bgp->tshut_gpio);
1469 }
1470
1471 return 0;
1472}
1473
1474#ifdef CONFIG_PM_SLEEP
1475static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1476{
1477 int i;
1478
1479 for (i = 0; i < bgp->conf->sensor_count; i++) {
1480 struct temp_sensor_registers *tsr;
1481 struct temp_sensor_regval *rval;
1482
1483 rval = &bgp->regval[i];
1484 tsr = bgp->conf->sensors[i].registers;
1485
1486 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1487 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1488 tsr->bgap_mode_ctrl);
1489 if (TI_BANDGAP_HAS(bgp, COUNTER))
1490 rval->bg_counter = ti_bandgap_readl(bgp,
1491 tsr->bgap_counter);
1492 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1493 rval->bg_threshold = ti_bandgap_readl(bgp,
1494 tsr->bgap_threshold);
1495 rval->bg_ctrl = ti_bandgap_readl(bgp,
1496 tsr->bgap_mask_ctrl);
1497 }
1498
1499 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1500 rval->tshut_threshold = ti_bandgap_readl(bgp,
1501 tsr->tshut_threshold);
1502 }
1503
1504 return 0;
1505}
1506
1507static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1508{
1509 int i;
1510
1511 for (i = 0; i < bgp->conf->sensor_count; i++) {
1512 struct temp_sensor_registers *tsr;
1513 struct temp_sensor_regval *rval;
1514 u32 val = 0;
1515
1516 rval = &bgp->regval[i];
1517 tsr = bgp->conf->sensors[i].registers;
1518
1519 if (TI_BANDGAP_HAS(bgp, COUNTER))
1520 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1521
1522 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1523 ti_bandgap_writel(bgp, rval->tshut_threshold,
1524 tsr->tshut_threshold);
1525
1526
1527
1528 ti_bandgap_force_single_read(bgp, i);
1529
1530 if (TI_BANDGAP_HAS(bgp, COUNTER))
1531 ti_bandgap_writel(bgp, rval->bg_counter,
1532 tsr->bgap_counter);
1533 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1534 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1535 tsr->bgap_mode_ctrl);
1536 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1537 ti_bandgap_writel(bgp, rval->bg_threshold,
1538 tsr->bgap_threshold);
1539 ti_bandgap_writel(bgp, rval->bg_ctrl,
1540 tsr->bgap_mask_ctrl);
1541 }
1542 }
1543
1544 return 0;
1545}
1546
1547static int ti_bandgap_suspend(struct device *dev)
1548{
1549 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1550 int err;
1551
1552 err = ti_bandgap_save_ctxt(bgp);
1553 ti_bandgap_power(bgp, false);
1554
1555 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1556 clk_disable_unprepare(bgp->fclock);
1557
1558 return err;
1559}
1560
1561static int ti_bandgap_resume(struct device *dev)
1562{
1563 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1564
1565 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1566 clk_prepare_enable(bgp->fclock);
1567
1568 ti_bandgap_power(bgp, true);
1569
1570 return ti_bandgap_restore_ctxt(bgp);
1571}
1572static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1573 ti_bandgap_resume);
1574
1575#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1576#else
1577#define DEV_PM_OPS NULL
1578#endif
1579
1580static const struct of_device_id of_ti_bandgap_match[] = {
1581#ifdef CONFIG_OMAP3_THERMAL
1582 {
1583 .compatible = "ti,omap34xx-bandgap",
1584 .data = (void *)&omap34xx_data,
1585 },
1586 {
1587 .compatible = "ti,omap36xx-bandgap",
1588 .data = (void *)&omap36xx_data,
1589 },
1590#endif
1591#ifdef CONFIG_OMAP4_THERMAL
1592 {
1593 .compatible = "ti,omap4430-bandgap",
1594 .data = (void *)&omap4430_data,
1595 },
1596 {
1597 .compatible = "ti,omap4460-bandgap",
1598 .data = (void *)&omap4460_data,
1599 },
1600 {
1601 .compatible = "ti,omap4470-bandgap",
1602 .data = (void *)&omap4470_data,
1603 },
1604#endif
1605#ifdef CONFIG_OMAP5_THERMAL
1606 {
1607 .compatible = "ti,omap5430-bandgap",
1608 .data = (void *)&omap5430_data,
1609 },
1610#endif
1611#ifdef CONFIG_DRA752_THERMAL
1612 {
1613 .compatible = "ti,dra752-bandgap",
1614 .data = (void *)&dra752_data,
1615 },
1616#endif
1617
1618 { },
1619};
1620MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1621
1622static struct platform_driver ti_bandgap_sensor_driver = {
1623 .probe = ti_bandgap_probe,
1624 .remove = ti_bandgap_remove,
1625 .driver = {
1626 .name = "ti-soc-thermal",
1627 .pm = DEV_PM_OPS,
1628 .of_match_table = of_ti_bandgap_match,
1629 },
1630};
1631
1632module_platform_driver(ti_bandgap_sensor_driver);
1633
1634MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1635MODULE_LICENSE("GPL v2");
1636MODULE_ALIAS("platform:ti-soc-thermal");
1637MODULE_AUTHOR("Texas Instrument Inc.");
1638