blob: c3f8982a71bdd34d686b4564269c17d919efafc2 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2001
3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <config.h>
26#include <command.h>
wdenk38635852002-08-27 05:55:31 +000027
wdenk38635852002-08-27 05:55:31 +000028#include <dtt.h>
Stefan Roese096cc9b2007-02-20 10:51:26 +010029#include <i2c.h>
Akshay Saraswate3d76602013-02-25 01:13:04 +000030#include <tmu.h>
wdenk38635852002-08-27 05:55:31 +000031
Akshay Saraswate3d76602013-02-25 01:13:04 +000032#if defined CONFIG_DTT_SENSORS
Heiko Schocher8df768c2011-08-01 04:01:43 +000033static unsigned long sensor_initialized;
34
Dirk Eibachfe64eff2011-10-13 23:23:12 +000035static void _initialize_dtt(void)
36{
37 int i;
38 unsigned char sensors[] = CONFIG_DTT_SENSORS;
39
40 for (i = 0; i < sizeof(sensors); i++) {
41 if ((sensor_initialized & (1 << i)) == 0) {
42 if (dtt_init_one(sensors[i]) != 0) {
43 printf("DTT%d: Failed init!\n", i);
44 continue;
45 }
46 sensor_initialized |= (1 << i);
47 }
48 }
49}
50
51void dtt_init(void)
52{
53 int old_bus;
54
55 /* switch to correct I2C bus */
56 old_bus = I2C_GET_BUS();
57 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
58
59 _initialize_dtt();
60
61 /* switch back to original I2C bus */
62 I2C_SET_BUS(old_bus);
63}
Akshay Saraswate3d76602013-02-25 01:13:04 +000064#endif
Dirk Eibachfe64eff2011-10-13 23:23:12 +000065
Akshay Saraswate3d76602013-02-25 01:13:04 +000066int dtt_i2c(void)
wdenk38635852002-08-27 05:55:31 +000067{
Akshay Saraswate3d76602013-02-25 01:13:04 +000068#if defined CONFIG_DTT_SENSORS
wdenk38635852002-08-27 05:55:31 +000069 int i;
70 unsigned char sensors[] = CONFIG_DTT_SENSORS;
Stefan Roese096cc9b2007-02-20 10:51:26 +010071 int old_bus;
72
Heiko Schocher8df768c2011-08-01 04:01:43 +000073 /* Force a compilation error, if there are more then 32 sensors */
74 BUILD_BUG_ON(sizeof(sensors) > 32);
Stefan Roese096cc9b2007-02-20 10:51:26 +010075 /* switch to correct I2C bus */
Heiko Schochere0e55bc2012-01-16 21:12:24 +000076#ifdef CONFIG_SYS_I2C
77 old_bus = i2c_get_bus_num();
78 i2c_set_bus_num(CONFIG_SYS_DTT_BUS_NUM);
79#else
Stefan Roese096cc9b2007-02-20 10:51:26 +010080 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
Heiko Schochere0e55bc2012-01-16 21:12:24 +000082#endif
wdenk38635852002-08-27 05:55:31 +000083
Dirk Eibachfe64eff2011-10-13 23:23:12 +000084 _initialize_dtt();
85
wdenk38635852002-08-27 05:55:31 +000086 /*
87 * Loop through sensors, read
88 * temperature, and output it.
89 */
Dirk Eibachfe64eff2011-10-13 23:23:12 +000090 for (i = 0; i < sizeof(sensors); i++)
Heiko Schocher8df768c2011-08-01 04:01:43 +000091 printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i]));
Stefan Roese096cc9b2007-02-20 10:51:26 +010092
93 /* switch back to original I2C bus */
Heiko Schochere0e55bc2012-01-16 21:12:24 +000094#ifdef CONFIG_SYS_I2C
95 i2c_set_bus_num(old_bus);
96#else
Stefan Roese096cc9b2007-02-20 10:51:26 +010097 I2C_SET_BUS(old_bus);
Akshay Saraswate3d76602013-02-25 01:13:04 +000098#endif
Heiko Schochere0e55bc2012-01-16 21:12:24 +000099#endif
wdenk38635852002-08-27 05:55:31 +0000100
101 return 0;
Akshay Saraswate3d76602013-02-25 01:13:04 +0000102}
103
104int dtt_tmu(void)
105{
106#if defined CONFIG_TMU_CMD_DTT
107 int cur_temp;
108
109 /* Sense and return latest thermal info */
110 if (tmu_monitor(&cur_temp) == TMU_STATUS_INIT) {
111 puts("TMU is in unknown state, temperature is invalid\n");
112 return -1;
113 }
114 printf("Current temperature: %u degrees Celsius\n", cur_temp);
115#endif
116 return 0;
117}
118
119int do_dtt(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
120{
121 int err = 0;
122
123 err |= dtt_i2c();
124 err |= dtt_tmu();
125
126 return err;
wdenk38635852002-08-27 05:55:31 +0000127} /* do_dtt() */
128
wdenk57b2d802003-06-27 21:31:46 +0000129/***************************************************/
130
wdenkf287a242003-07-01 21:06:45 +0000131U_BOOT_CMD(
132 dtt, 1, 1, do_dtt,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200133 "Read temperature from Digital Thermometer and Thermostat",
134 ""
wdenk57b2d802003-06-27 21:31:46 +0000135);