blob: dd427a3d0b6346d7031e2073bc33a2fe6732bb53 [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 Denk3765b3e2013-10-07 13:07:26 +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 Roese0dc018e2007-02-20 10:51:26 +010013#include <i2c.h>
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000014#include <tmu.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090015#include <linux/bug.h>
wdenk38635852002-08-27 05:55:31 +000016
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000017#if defined CONFIG_DTT_SENSORS
Heiko Schocher780f13a2011-08-01 04:01:43 +000018static unsigned long sensor_initialized;
19
Dirk Eibachb88e7b32011-10-13 23:23:12 +000020static 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
36void 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}
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000049#endif
Dirk Eibachb88e7b32011-10-13 23:23:12 +000050
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000051int dtt_i2c(void)
wdenk38635852002-08-27 05:55:31 +000052{
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000053#if defined CONFIG_DTT_SENSORS
wdenk38635852002-08-27 05:55:31 +000054 int i;
55 unsigned char sensors[] = CONFIG_DTT_SENSORS;
Stefan Roese0dc018e2007-02-20 10:51:26 +010056 int old_bus;
57
Heiko Schocher780f13a2011-08-01 04:01:43 +000058 /* Force a compilation error, if there are more then 32 sensors */
59 BUILD_BUG_ON(sizeof(sensors) > 32);
Stefan Roese0dc018e2007-02-20 10:51:26 +010060 /* switch to correct I2C bus */
Heiko Schocher3f4978c2012-01-16 21:12:24 +000061#ifdef CONFIG_SYS_I2C
62 old_bus = i2c_get_bus_num();
63 i2c_set_bus_num(CONFIG_SYS_DTT_BUS_NUM);
64#else
Stefan Roese0dc018e2007-02-20 10:51:26 +010065 old_bus = I2C_GET_BUS();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
Heiko Schocher3f4978c2012-01-16 21:12:24 +000067#endif
wdenk38635852002-08-27 05:55:31 +000068
Dirk Eibachb88e7b32011-10-13 23:23:12 +000069 _initialize_dtt();
70
wdenk38635852002-08-27 05:55:31 +000071 /*
72 * Loop through sensors, read
73 * temperature, and output it.
74 */
Dirk Eibachb88e7b32011-10-13 23:23:12 +000075 for (i = 0; i < sizeof(sensors); i++)
Heiko Schocher780f13a2011-08-01 04:01:43 +000076 printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i]));
Stefan Roese0dc018e2007-02-20 10:51:26 +010077
78 /* switch back to original I2C bus */
Heiko Schocher3f4978c2012-01-16 21:12:24 +000079#ifdef CONFIG_SYS_I2C
80 i2c_set_bus_num(old_bus);
81#else
Stefan Roese0dc018e2007-02-20 10:51:26 +010082 I2C_SET_BUS(old_bus);
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000083#endif
Heiko Schocher3f4978c2012-01-16 21:12:24 +000084#endif
wdenk38635852002-08-27 05:55:31 +000085
86 return 0;
Akshay Saraswatbc5478b2013-02-25 01:13:04 +000087}
88
89int 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
104int 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;
wdenk38635852002-08-27 05:55:31 +0000112} /* do_dtt() */
113
wdenk8bde7f72003-06-27 21:31:46 +0000114/***************************************************/
115
wdenk0d498392003-07-01 21:06:45 +0000116U_BOOT_CMD(
117 dtt, 1, 1, do_dtt,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200118 "Read temperature from Digital Thermometer and Thermostat",
119 ""
wdenk8bde7f72003-06-27 21:31:46 +0000120);