blob: dc57744f8981b82e41c0eaa161ce6584089c0717 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2001
3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00006 */
7
8#include <common.h>
9#include <config.h>
10#include <command.h>
wdenk38635852002-08-27 05:55:31 +000011
wdenk38635852002-08-27 05:55:31 +000012#include <dtt.h>
Stefan Roese096cc9b2007-02-20 10:51:26 +010013#include <i2c.h>
Akshay Saraswate3d76602013-02-25 01:13:04 +000014#include <tmu.h>
wdenk38635852002-08-27 05:55:31 +000015
Akshay Saraswate3d76602013-02-25 01:13:04 +000016#if defined CONFIG_DTT_SENSORS
Heiko Schocher8df768c2011-08-01 04:01:43 +000017static unsigned long sensor_initialized;
18
Dirk Eibachfe64eff2011-10-13 23:23:12 +000019static void _initialize_dtt(void)
20{
21 int i;
22 unsigned char sensors[] = CONFIG_DTT_SENSORS;
23
24 for (i = 0; i < sizeof(sensors); i++) {
25 if ((sensor_initialized & (1 << i)) == 0) {
26 if (dtt_init_one(sensors[i]) != 0) {
27 printf("DTT%d: Failed init!\n", i);
28 continue;
29 }
30 sensor_initialized |= (1 << i);
31 }
32 }
33}
34
35void dtt_init(void)
36{
37 int old_bus;
38
39 /* switch to correct I2C bus */
40 old_bus = I2C_GET_BUS();
41 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
42
43 _initialize_dtt();
44
45 /* switch back to original I2C bus */
46 I2C_SET_BUS(old_bus);
47}
Akshay Saraswate3d76602013-02-25 01:13:04 +000048#endif
Dirk Eibachfe64eff2011-10-13 23:23:12 +000049
Akshay Saraswate3d76602013-02-25 01:13:04 +000050int dtt_i2c(void)
wdenk38635852002-08-27 05:55:31 +000051{
Akshay Saraswate3d76602013-02-25 01:13:04 +000052#if defined CONFIG_DTT_SENSORS
wdenk38635852002-08-27 05:55:31 +000053 int i;
54 unsigned char sensors[] = CONFIG_DTT_SENSORS;
Stefan Roese096cc9b2007-02-20 10:51:26 +010055 int old_bus;
56
Heiko Schocher8df768c2011-08-01 04:01:43 +000057 /* Force a compilation error, if there are more then 32 sensors */
58 BUILD_BUG_ON(sizeof(sensors) > 32);
Stefan Roese096cc9b2007-02-20 10:51:26 +010059 /* switch to correct I2C bus */
60 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020061 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
wdenk38635852002-08-27 05:55:31 +000062
Dirk Eibachfe64eff2011-10-13 23:23:12 +000063 _initialize_dtt();
64
wdenk38635852002-08-27 05:55:31 +000065 /*
66 * Loop through sensors, read
67 * temperature, and output it.
68 */
Dirk Eibachfe64eff2011-10-13 23:23:12 +000069 for (i = 0; i < sizeof(sensors); i++)
Heiko Schocher8df768c2011-08-01 04:01:43 +000070 printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i]));
Stefan Roese096cc9b2007-02-20 10:51:26 +010071
72 /* switch back to original I2C bus */
73 I2C_SET_BUS(old_bus);
Akshay Saraswate3d76602013-02-25 01:13:04 +000074#endif
wdenk38635852002-08-27 05:55:31 +000075
76 return 0;
Akshay Saraswate3d76602013-02-25 01:13:04 +000077}
78
79int dtt_tmu(void)
80{
81#if defined CONFIG_TMU_CMD_DTT
82 int cur_temp;
83
84 /* Sense and return latest thermal info */
85 if (tmu_monitor(&cur_temp) == TMU_STATUS_INIT) {
86 puts("TMU is in unknown state, temperature is invalid\n");
87 return -1;
88 }
89 printf("Current temperature: %u degrees Celsius\n", cur_temp);
90#endif
91 return 0;
92}
93
94int do_dtt(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
95{
96 int err = 0;
97
98 err |= dtt_i2c();
99 err |= dtt_tmu();
100
101 return err;
wdenk38635852002-08-27 05:55:31 +0000102} /* do_dtt() */
103
wdenk57b2d802003-06-27 21:31:46 +0000104/***************************************************/
105
wdenkf287a242003-07-01 21:06:45 +0000106U_BOOT_CMD(
107 dtt, 1, 1, do_dtt,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200108 "Read temperature from Digital Thermometer and Thermostat",
109 ""
wdenk57b2d802003-06-27 21:31:46 +0000110);