1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/export.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/interrupt.h>
17#include <linux/clk.h>
18#include <linux/gpio/consumer.h>
19#include <linux/platform_device.h>
20#include <linux/err.h>
21#include <linux/types.h>
22#include <linux/spinlock.h>
23#include <linux/reboot.h>
24#include <linux/of_device.h>
25#include <linux/of_platform.h>
26#include <linux/of_irq.h>
27#include <linux/io.h>
28
29#include "ti-bandgap.h"
30
31static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
32
33
34
35
36
37
38
39
40
41
42
43static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
44{
45 return readl(bgp->base + reg);
46}
47
48
49
50
51
52
53
54
55
56static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
57{
58 writel(val, bgp->base + reg);
59}
60
61
62
63
64
65
66
67#define RMW_BITS(bgp, id, reg, mask, val) \
68do { \
69 struct temp_sensor_registers *t; \
70 u32 r; \
71 \
72 t = bgp->conf->sensors[(id)].registers; \
73 r = ti_bandgap_readl(bgp, t->reg); \
74 r &= ~t->mask; \
75 r |= (val) << __ffs(t->mask); \
76 ti_bandgap_writel(bgp, r, t->reg); \
77} while (0)
78
79
80
81
82
83
84
85
86
87
88
89
90
91static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
92{
93 int i;
94
95 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
96 return -ENOTSUPP;
97
98 for (i = 0; i < bgp->conf->sensor_count; i++)
99
100 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
101 return 0;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg)
121{
122 u32 val1, val2;
123
124 val1 = ti_bandgap_readl(bgp, reg);
125 val2 = ti_bandgap_readl(bgp, reg);
126
127
128 if (val1 == val2)
129 return val1;
130
131
132 return ti_bandgap_readl(bgp, reg);
133}
134
135
136
137
138
139
140
141
142
143
144
145
146
147static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
148{
149 struct temp_sensor_registers *tsr;
150 u32 temp, reg;
151
152 tsr = bgp->conf->sensors[id].registers;
153 reg = tsr->temp_sensor_ctrl;
154
155 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
156 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
157
158
159
160
161 reg = tsr->ctrl_dtemp_1;
162 }
163
164
165 if (TI_BANDGAP_HAS(bgp, ERRATA_814))
166 temp = ti_errata814_bandgap_read_temp(bgp, reg);
167 else
168 temp = ti_bandgap_readl(bgp, reg);
169
170 temp &= tsr->bgap_dtemp_mask;
171
172 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
173 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
174
175 return temp;
176}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
194{
195 struct ti_bandgap *bgp = data;
196 struct temp_sensor_registers *tsr;
197 u32 t_hot = 0, t_cold = 0, ctrl;
198 int i;
199
200 spin_lock(&bgp->lock);
201 for (i = 0; i < bgp->conf->sensor_count; i++) {
202 tsr = bgp->conf->sensors[i].registers;
203 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
204
205
206 t_hot = ctrl & tsr->status_hot_mask;
207
208
209 t_cold = ctrl & tsr->status_cold_mask;
210
211 if (!t_cold && !t_hot)
212 continue;
213
214 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
215
216
217
218
219
220 if (t_hot) {
221 ctrl &= ~tsr->mask_hot_mask;
222 ctrl |= tsr->mask_cold_mask;
223 } else if (t_cold) {
224 ctrl &= ~tsr->mask_cold_mask;
225 ctrl |= tsr->mask_hot_mask;
226 }
227
228 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
229
230 dev_dbg(bgp->dev,
231 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
232 __func__, bgp->conf->sensors[i].domain,
233 t_hot, t_cold);
234
235
236 if (bgp->conf->report_temperature)
237 bgp->conf->report_temperature(bgp, i);
238 }
239 spin_unlock(&bgp->lock);
240
241 return IRQ_HANDLED;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
256{
257 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
258 __func__);
259
260 orderly_poweroff(true);
261
262 return IRQ_HANDLED;
263}
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280static
281int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
282{
283 const struct ti_bandgap_data *conf = bgp->conf;
284
285
286 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
287 return -ERANGE;
288
289 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
290 return 0;
291}
292
293
294
295
296
297
298
299
300
301
302
303
304static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
305{
306 if (!bgp || IS_ERR(bgp)) {
307 pr_err("%s: invalid bandgap pointer\n", __func__);
308 return -EINVAL;
309 }
310
311 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
312 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
313 __func__, id);
314 return -ERANGE;
315 }
316
317 return 0;
318}
319
320
321
322
323
324
325
326static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
327 int *interval)
328{
329 struct temp_sensor_registers *tsr;
330 int time;
331
332 tsr = bgp->conf->sensors[id].registers;
333 time = ti_bandgap_readl(bgp, tsr->bgap_counter);
334 time = (time & tsr->counter_mask) >>
335 __ffs(tsr->counter_mask);
336 time = time * 1000 / bgp->clk_rate;
337 *interval = time;
338}
339
340
341
342
343
344
345
346static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
347 int *interval)
348{
349 struct temp_sensor_registers *tsr;
350 int reg_val;
351
352 tsr = bgp->conf->sensors[id].registers;
353
354 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
355 reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
356 __ffs(tsr->mask_counter_delay_mask);
357 switch (reg_val) {
358 case 0:
359 *interval = 0;
360 break;
361 case 1:
362 *interval = 1;
363 break;
364 case 2:
365 *interval = 10;
366 break;
367 case 3:
368 *interval = 100;
369 break;
370 case 4:
371 *interval = 250;
372 break;
373 case 5:
374 *interval = 500;
375 break;
376 default:
377 dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
378 reg_val);
379 }
380}
381
382
383
384
385
386
387
388
389
390int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
391 int *interval)
392{
393 int ret = 0;
394
395 ret = ti_bandgap_validate(bgp, id);
396 if (ret)
397 goto exit;
398
399 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
400 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
401 ret = -ENOTSUPP;
402 goto exit;
403 }
404
405 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
406 ti_bandgap_read_counter(bgp, id, interval);
407 goto exit;
408 }
409
410 ti_bandgap_read_counter_delay(bgp, id, interval);
411exit:
412 return ret;
413}
414
415
416
417
418
419
420
421
422
423static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
424 u32 interval)
425{
426 int rval;
427
428 switch (interval) {
429 case 0:
430 rval = 0x0;
431 break;
432 case 1:
433 rval = 0x1;
434 break;
435 case 10:
436 rval = 0x2;
437 break;
438 case 100:
439 rval = 0x3;
440 break;
441 case 250:
442 rval = 0x4;
443 break;
444 case 500:
445 rval = 0x5;
446 break;
447 default:
448 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
449 return -EINVAL;
450 }
451
452 spin_lock(&bgp->lock);
453 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
454 spin_unlock(&bgp->lock);
455
456 return 0;
457}
458
459
460
461
462
463
464
465static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
466 u32 interval)
467{
468 interval = interval * bgp->clk_rate / 1000;
469 spin_lock(&bgp->lock);
470 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
471 spin_unlock(&bgp->lock);
472}
473
474
475
476
477
478
479
480
481
482int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
483 int id, u32 interval)
484{
485 int ret = ti_bandgap_validate(bgp, id);
486 if (ret)
487 goto exit;
488
489 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
490 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
491 ret = -ENOTSUPP;
492 goto exit;
493 }
494
495 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
496 ti_bandgap_write_counter(bgp, id, interval);
497 goto exit;
498 }
499
500 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
501exit:
502 return ret;
503}
504
505
506
507
508
509
510
511
512
513int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
514 int *temperature)
515{
516 u32 temp;
517 int ret;
518
519 ret = ti_bandgap_validate(bgp, id);
520 if (ret)
521 return ret;
522
523 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
524 ret = ti_bandgap_force_single_read(bgp, id);
525 if (ret)
526 return ret;
527 }
528
529 spin_lock(&bgp->lock);
530 temp = ti_bandgap_read_temp(bgp, id);
531 spin_unlock(&bgp->lock);
532
533 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
534 if (ret)
535 return -EIO;
536
537 *temperature = temp;
538
539 return 0;
540}
541
542
543
544
545
546
547
548
549
550
551int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
552{
553 int ret = ti_bandgap_validate(bgp, id);
554 if (ret)
555 return ret;
556
557 bgp->regval[id].data = data;
558
559 return 0;
560}
561
562
563
564
565
566
567
568
569
570void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
571{
572 int ret = ti_bandgap_validate(bgp, id);
573 if (ret)
574 return ERR_PTR(ret);
575
576 return bgp->regval[id].data;
577}
578
579
580
581
582
583
584
585
586
587
588
589
590
591static int
592ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
593{
594 u32 counter = 1000;
595 struct temp_sensor_registers *tsr;
596
597
598 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
599 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
600
601
602 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
603
604
605 tsr = bgp->conf->sensors[id].registers;
606
607 while (--counter) {
608 if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
609 tsr->bgap_eocz_mask)
610 break;
611 }
612
613
614 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
615
616
617 counter = 1000;
618 while (--counter) {
619 if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
620 tsr->bgap_eocz_mask))
621 break;
622 }
623
624 return 0;
625}
626
627
628
629
630
631
632
633
634
635
636
637
638static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
639{
640 int i;
641
642 for (i = 0; i < bgp->conf->sensor_count; i++) {
643
644 ti_bandgap_force_single_read(bgp, i);
645 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
646 }
647
648 return 0;
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
667{
668 struct temp_sensor_registers *tsr;
669 u32 temp1, temp2, reg1, reg2;
670 int t1, t2, interval, ret = 0;
671
672 ret = ti_bandgap_validate(bgp, id);
673 if (ret)
674 goto exit;
675
676 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
677 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
678 ret = -ENOTSUPP;
679 goto exit;
680 }
681
682 spin_lock(&bgp->lock);
683
684 tsr = bgp->conf->sensors[id].registers;
685
686
687 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
688 reg1 = tsr->ctrl_dtemp_1;
689 reg2 = tsr->ctrl_dtemp_2;
690
691
692 temp1 = ti_bandgap_readl(bgp, reg1);
693 temp1 &= tsr->bgap_dtemp_mask;
694
695 temp2 = ti_bandgap_readl(bgp, reg2);
696 temp2 &= tsr->bgap_dtemp_mask;
697
698
699 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
700 if (ret)
701 goto unfreeze;
702
703 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
704 if (ret)
705 goto unfreeze;
706
707
708 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
709 if (ret)
710 goto unfreeze;
711
712
713 if (interval == 0)
714 interval = 1;
715
716 *trend = (t1 - t2) / interval;
717
718 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
719 t1, t2, *trend);
720
721unfreeze:
722 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
723 spin_unlock(&bgp->lock);
724exit:
725 return ret;
726}
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
743 struct platform_device *pdev)
744{
745 int status;
746
747 status = request_irq(gpiod_to_irq(bgp->tshut_gpiod),
748 ti_bandgap_tshut_irq_handler,
749 IRQF_TRIGGER_RISING, "tshut", NULL);
750 if (status)
751 dev_err(bgp->dev, "request irq failed for TSHUT");
752
753 return 0;
754}
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
770 struct platform_device *pdev)
771{
772 int ret;
773
774 bgp->irq = platform_get_irq(pdev, 0);
775 if (bgp->irq < 0)
776 return bgp->irq;
777
778 ret = request_threaded_irq(bgp->irq, NULL,
779 ti_bandgap_talert_irq_handler,
780 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
781 "talert", bgp);
782 if (ret) {
783 dev_err(&pdev->dev, "Request threaded irq failed.\n");
784 return ret;
785 }
786
787 return 0;
788}
789
790static const struct of_device_id of_ti_bandgap_match[];
791
792
793
794
795
796
797
798
799
800
801
802static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
803{
804 struct device_node *node = pdev->dev.of_node;
805 const struct of_device_id *of_id;
806 struct ti_bandgap *bgp;
807 struct resource *res;
808 int i;
809
810
811 if (!node) {
812 dev_err(&pdev->dev, "no platform information available\n");
813 return ERR_PTR(-EINVAL);
814 }
815
816 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
817 if (!bgp)
818 return ERR_PTR(-ENOMEM);
819
820 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
821 if (of_id)
822 bgp->conf = of_id->data;
823
824
825 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
826 sizeof(*bgp->regval), GFP_KERNEL);
827 if (!bgp->regval)
828 return ERR_PTR(-ENOMEM);
829
830 i = 0;
831 do {
832 void __iomem *chunk;
833
834 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
835 if (!res)
836 break;
837 chunk = devm_ioremap_resource(&pdev->dev, res);
838 if (i == 0)
839 bgp->base = chunk;
840 if (IS_ERR(chunk))
841 return ERR_CAST(chunk);
842
843 i++;
844 } while (res);
845
846 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
847 bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
848 if (IS_ERR(bgp->tshut_gpiod)) {
849 dev_err(&pdev->dev, "invalid gpio for tshut\n");
850 return ERR_CAST(bgp->tshut_gpiod);
851 }
852 }
853
854 return bgp;
855}
856
857
858
859static
860int ti_bandgap_probe(struct platform_device *pdev)
861{
862 struct ti_bandgap *bgp;
863 int clk_rate, ret, i;
864
865 bgp = ti_bandgap_build(pdev);
866 if (IS_ERR(bgp)) {
867 dev_err(&pdev->dev, "failed to fetch platform data\n");
868 return PTR_ERR(bgp);
869 }
870 bgp->dev = &pdev->dev;
871
872 if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
873 dev_warn(&pdev->dev,
874 "This OMAP thermal sensor is unreliable. You've been warned\n");
875
876 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
877 ret = ti_bandgap_tshut_init(bgp, pdev);
878 if (ret) {
879 dev_err(&pdev->dev,
880 "failed to initialize system tshut IRQ\n");
881 return ret;
882 }
883 }
884
885 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
886 if (IS_ERR(bgp->fclock)) {
887 dev_err(&pdev->dev, "failed to request fclock reference\n");
888 ret = PTR_ERR(bgp->fclock);
889 goto free_irqs;
890 }
891
892 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
893 if (IS_ERR(bgp->div_clk)) {
894 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
895 ret = PTR_ERR(bgp->div_clk);
896 goto put_fclock;
897 }
898
899 for (i = 0; i < bgp->conf->sensor_count; i++) {
900 struct temp_sensor_registers *tsr;
901 u32 val;
902
903 tsr = bgp->conf->sensors[i].registers;
904
905
906
907
908
909 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
910 if (!val)
911 dev_info(&pdev->dev,
912 "Non-trimmed BGAP, Temp not accurate\n");
913 }
914
915 clk_rate = clk_round_rate(bgp->div_clk,
916 bgp->conf->sensors[0].ts_data->max_freq);
917 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
918 clk_rate <= 0) {
919 ret = -ENODEV;
920 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
921 goto put_clks;
922 }
923
924 ret = clk_set_rate(bgp->div_clk, clk_rate);
925 if (ret)
926 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
927
928 bgp->clk_rate = clk_rate;
929 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
930 clk_prepare_enable(bgp->fclock);
931
932
933 spin_lock_init(&bgp->lock);
934 bgp->dev = &pdev->dev;
935 platform_set_drvdata(pdev, bgp);
936
937 ti_bandgap_power(bgp, true);
938
939
940 if (TI_BANDGAP_HAS(bgp, COUNTER))
941 for (i = 0; i < bgp->conf->sensor_count; i++)
942 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
943
944
945 for (i = 0; i < bgp->conf->sensor_count; i++) {
946 struct temp_sensor_data *ts_data;
947
948 ts_data = bgp->conf->sensors[i].ts_data;
949
950 if (TI_BANDGAP_HAS(bgp, TALERT)) {
951
952 RMW_BITS(bgp, i, bgap_threshold,
953 threshold_tcold_mask, ts_data->t_cold);
954 RMW_BITS(bgp, i, bgap_threshold,
955 threshold_thot_mask, ts_data->t_hot);
956
957 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
958 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
959 }
960
961 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
962
963 RMW_BITS(bgp, i, tshut_threshold,
964 tshut_hot_mask, ts_data->tshut_hot);
965 RMW_BITS(bgp, i, tshut_threshold,
966 tshut_cold_mask, ts_data->tshut_cold);
967 }
968 }
969
970 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
971 ti_bandgap_set_continuous_mode(bgp);
972
973
974 if (TI_BANDGAP_HAS(bgp, COUNTER))
975 for (i = 0; i < bgp->conf->sensor_count; i++)
976 RMW_BITS(bgp, i, bgap_counter, counter_mask,
977 bgp->clk_rate / 4);
978
979
980 for (i = 0; i < bgp->conf->sensor_count; i++) {
981 char *domain;
982
983 if (bgp->conf->sensors[i].register_cooling) {
984 ret = bgp->conf->sensors[i].register_cooling(bgp, i);
985 if (ret)
986 goto remove_sensors;
987 }
988
989 if (bgp->conf->expose_sensor) {
990 domain = bgp->conf->sensors[i].domain;
991 ret = bgp->conf->expose_sensor(bgp, i, domain);
992 if (ret)
993 goto remove_last_cooling;
994 }
995 }
996
997
998
999
1000
1001
1002 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1003 ret = ti_bandgap_talert_init(bgp, pdev);
1004 if (ret) {
1005 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1006 i = bgp->conf->sensor_count;
1007 goto disable_clk;
1008 }
1009 }
1010
1011 return 0;
1012
1013remove_last_cooling:
1014 if (bgp->conf->sensors[i].unregister_cooling)
1015 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1016remove_sensors:
1017 for (i--; i >= 0; i--) {
1018 if (bgp->conf->sensors[i].unregister_cooling)
1019 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1020 if (bgp->conf->remove_sensor)
1021 bgp->conf->remove_sensor(bgp, i);
1022 }
1023 ti_bandgap_power(bgp, false);
1024disable_clk:
1025 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1026 clk_disable_unprepare(bgp->fclock);
1027put_clks:
1028 clk_put(bgp->div_clk);
1029put_fclock:
1030 clk_put(bgp->fclock);
1031free_irqs:
1032 if (TI_BANDGAP_HAS(bgp, TSHUT))
1033 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1034
1035 return ret;
1036}
1037
1038static
1039int ti_bandgap_remove(struct platform_device *pdev)
1040{
1041 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1042 int i;
1043
1044
1045 for (i = 0; i < bgp->conf->sensor_count; i++) {
1046 if (bgp->conf->sensors[i].unregister_cooling)
1047 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1048
1049 if (bgp->conf->remove_sensor)
1050 bgp->conf->remove_sensor(bgp, i);
1051 }
1052
1053 ti_bandgap_power(bgp, false);
1054
1055 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1056 clk_disable_unprepare(bgp->fclock);
1057 clk_put(bgp->fclock);
1058 clk_put(bgp->div_clk);
1059
1060 if (TI_BANDGAP_HAS(bgp, TALERT))
1061 free_irq(bgp->irq, bgp);
1062
1063 if (TI_BANDGAP_HAS(bgp, TSHUT))
1064 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1065
1066 return 0;
1067}
1068
1069#ifdef CONFIG_PM_SLEEP
1070static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1071{
1072 int i;
1073
1074 for (i = 0; i < bgp->conf->sensor_count; i++) {
1075 struct temp_sensor_registers *tsr;
1076 struct temp_sensor_regval *rval;
1077
1078 rval = &bgp->regval[i];
1079 tsr = bgp->conf->sensors[i].registers;
1080
1081 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1082 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1083 tsr->bgap_mode_ctrl);
1084 if (TI_BANDGAP_HAS(bgp, COUNTER))
1085 rval->bg_counter = ti_bandgap_readl(bgp,
1086 tsr->bgap_counter);
1087 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1088 rval->bg_threshold = ti_bandgap_readl(bgp,
1089 tsr->bgap_threshold);
1090 rval->bg_ctrl = ti_bandgap_readl(bgp,
1091 tsr->bgap_mask_ctrl);
1092 }
1093
1094 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1095 rval->tshut_threshold = ti_bandgap_readl(bgp,
1096 tsr->tshut_threshold);
1097 }
1098
1099 return 0;
1100}
1101
1102static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1103{
1104 int i;
1105
1106 for (i = 0; i < bgp->conf->sensor_count; i++) {
1107 struct temp_sensor_registers *tsr;
1108 struct temp_sensor_regval *rval;
1109 u32 val = 0;
1110
1111 rval = &bgp->regval[i];
1112 tsr = bgp->conf->sensors[i].registers;
1113
1114 if (TI_BANDGAP_HAS(bgp, COUNTER))
1115 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1116
1117 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1118 ti_bandgap_writel(bgp, rval->tshut_threshold,
1119 tsr->tshut_threshold);
1120
1121
1122
1123 ti_bandgap_force_single_read(bgp, i);
1124
1125 if (TI_BANDGAP_HAS(bgp, COUNTER))
1126 ti_bandgap_writel(bgp, rval->bg_counter,
1127 tsr->bgap_counter);
1128 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1129 ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1130 tsr->bgap_mode_ctrl);
1131 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1132 ti_bandgap_writel(bgp, rval->bg_threshold,
1133 tsr->bgap_threshold);
1134 ti_bandgap_writel(bgp, rval->bg_ctrl,
1135 tsr->bgap_mask_ctrl);
1136 }
1137 }
1138
1139 return 0;
1140}
1141
1142static int ti_bandgap_suspend(struct device *dev)
1143{
1144 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1145 int err;
1146
1147 err = ti_bandgap_save_ctxt(bgp);
1148 ti_bandgap_power(bgp, false);
1149
1150 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1151 clk_disable_unprepare(bgp->fclock);
1152
1153 return err;
1154}
1155
1156static int ti_bandgap_resume(struct device *dev)
1157{
1158 struct ti_bandgap *bgp = dev_get_drvdata(dev);
1159
1160 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1161 clk_prepare_enable(bgp->fclock);
1162
1163 ti_bandgap_power(bgp, true);
1164
1165 return ti_bandgap_restore_ctxt(bgp);
1166}
1167static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1168 ti_bandgap_resume);
1169
1170#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
1171#else
1172#define DEV_PM_OPS NULL
1173#endif
1174
1175static const struct of_device_id of_ti_bandgap_match[] = {
1176#ifdef CONFIG_OMAP3_THERMAL
1177 {
1178 .compatible = "ti,omap34xx-bandgap",
1179 .data = (void *)&omap34xx_data,
1180 },
1181 {
1182 .compatible = "ti,omap36xx-bandgap",
1183 .data = (void *)&omap36xx_data,
1184 },
1185#endif
1186#ifdef CONFIG_OMAP4_THERMAL
1187 {
1188 .compatible = "ti,omap4430-bandgap",
1189 .data = (void *)&omap4430_data,
1190 },
1191 {
1192 .compatible = "ti,omap4460-bandgap",
1193 .data = (void *)&omap4460_data,
1194 },
1195 {
1196 .compatible = "ti,omap4470-bandgap",
1197 .data = (void *)&omap4470_data,
1198 },
1199#endif
1200#ifdef CONFIG_OMAP5_THERMAL
1201 {
1202 .compatible = "ti,omap5430-bandgap",
1203 .data = (void *)&omap5430_data,
1204 },
1205#endif
1206#ifdef CONFIG_DRA752_THERMAL
1207 {
1208 .compatible = "ti,dra752-bandgap",
1209 .data = (void *)&dra752_data,
1210 },
1211#endif
1212
1213 { },
1214};
1215MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1216
1217static struct platform_driver ti_bandgap_sensor_driver = {
1218 .probe = ti_bandgap_probe,
1219 .remove = ti_bandgap_remove,
1220 .driver = {
1221 .name = "ti-soc-thermal",
1222 .pm = DEV_PM_OPS,
1223 .of_match_table = of_ti_bandgap_match,
1224 },
1225};
1226
1227module_platform_driver(ti_bandgap_sensor_driver);
1228
1229MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1230MODULE_LICENSE("GPL v2");
1231MODULE_ALIAS("platform:ti-soc-thermal");
1232MODULE_AUTHOR("Texas Instrument Inc.");
1233