]> git.ipfire.org Git - people/ms/u-boot.git/blob - cmd/dtt.c
Merge git://git.denx.de/u-boot-mpc85xx
[people/ms/u-boot.git] / cmd / dtt.c
1 /*
2 * (C) Copyright 2001
3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8 #include <common.h>
9 #include <config.h>
10 #include <command.h>
11
12 #include <dtt.h>
13 #include <i2c.h>
14 #include <tmu.h>
15 #include <linux/bug.h>
16
17 #if defined CONFIG_DTT_SENSORS
18 static unsigned long sensor_initialized;
19
20 static void _initialize_dtt(void)
21 {
22 int i;
23 unsigned char sensors[] = CONFIG_DTT_SENSORS;
24
25 for (i = 0; i < sizeof(sensors); i++) {
26 if ((sensor_initialized & (1 << i)) == 0) {
27 if (dtt_init_one(sensors[i]) != 0) {
28 printf("DTT%d: Failed init!\n", i);
29 continue;
30 }
31 sensor_initialized |= (1 << i);
32 }
33 }
34 }
35
36 void dtt_init(void)
37 {
38 int old_bus;
39
40 /* switch to correct I2C bus */
41 old_bus = I2C_GET_BUS();
42 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
43
44 _initialize_dtt();
45
46 /* switch back to original I2C bus */
47 I2C_SET_BUS(old_bus);
48 }
49 #endif
50
51 int dtt_i2c(void)
52 {
53 #if defined CONFIG_DTT_SENSORS
54 int i;
55 unsigned char sensors[] = CONFIG_DTT_SENSORS;
56 int old_bus;
57
58 /* Force a compilation error, if there are more then 32 sensors */
59 BUILD_BUG_ON(sizeof(sensors) > 32);
60 /* switch to correct I2C bus */
61 #ifdef CONFIG_SYS_I2C
62 old_bus = i2c_get_bus_num();
63 i2c_set_bus_num(CONFIG_SYS_DTT_BUS_NUM);
64 #else
65 old_bus = I2C_GET_BUS();
66 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
67 #endif
68
69 _initialize_dtt();
70
71 /*
72 * Loop through sensors, read
73 * temperature, and output it.
74 */
75 for (i = 0; i < sizeof(sensors); i++)
76 printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i]));
77
78 /* switch back to original I2C bus */
79 #ifdef CONFIG_SYS_I2C
80 i2c_set_bus_num(old_bus);
81 #else
82 I2C_SET_BUS(old_bus);
83 #endif
84 #endif
85
86 return 0;
87 }
88
89 int dtt_tmu(void)
90 {
91 #if defined CONFIG_TMU_CMD_DTT
92 int cur_temp;
93
94 /* Sense and return latest thermal info */
95 if (tmu_monitor(&cur_temp) == TMU_STATUS_INIT) {
96 puts("TMU is in unknown state, temperature is invalid\n");
97 return -1;
98 }
99 printf("Current temperature: %u degrees Celsius\n", cur_temp);
100 #endif
101 return 0;
102 }
103
104 int do_dtt(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
105 {
106 int err = 0;
107
108 err |= dtt_i2c();
109 err |= dtt_tmu();
110
111 return err;
112 } /* do_dtt() */
113
114 /***************************************************/
115
116 U_BOOT_CMD(
117 dtt, 1, 1, do_dtt,
118 "Read temperature from Digital Thermometer and Thermostat",
119 ""
120 );