wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <config.h> |
| 10 | #include <command.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 11 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 12 | #include <dtt.h> |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 13 | #include <i2c.h> |
Akshay Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 14 | #include <tmu.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 15 | |
Akshay Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 16 | #if defined CONFIG_DTT_SENSORS |
Heiko Schocher | 8df768c | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 17 | static unsigned long sensor_initialized; |
| 18 | |
Dirk Eibach | fe64eff | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 19 | static 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 | |
| 35 | void 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 Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 48 | #endif |
Dirk Eibach | fe64eff | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 49 | |
Akshay Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 50 | int dtt_i2c(void) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 51 | { |
Akshay Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 52 | #if defined CONFIG_DTT_SENSORS |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 53 | int i; |
| 54 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 55 | int old_bus; |
| 56 | |
Heiko Schocher | 8df768c | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 57 | /* Force a compilation error, if there are more then 32 sensors */ |
| 58 | BUILD_BUG_ON(sizeof(sensors) > 32); |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 59 | /* switch to correct I2C bus */ |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_SYS_I2C |
| 61 | old_bus = i2c_get_bus_num(); |
| 62 | i2c_set_bus_num(CONFIG_SYS_DTT_BUS_NUM); |
| 63 | #else |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 64 | old_bus = I2C_GET_BUS(); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM); |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 66 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 67 | |
Dirk Eibach | fe64eff | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 68 | _initialize_dtt(); |
| 69 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 70 | /* |
| 71 | * Loop through sensors, read |
| 72 | * temperature, and output it. |
| 73 | */ |
Dirk Eibach | fe64eff | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 74 | for (i = 0; i < sizeof(sensors); i++) |
Heiko Schocher | 8df768c | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 75 | printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i])); |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 76 | |
| 77 | /* switch back to original I2C bus */ |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_SYS_I2C |
| 79 | i2c_set_bus_num(old_bus); |
| 80 | #else |
Stefan Roese | 096cc9b | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 81 | I2C_SET_BUS(old_bus); |
Akshay Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 82 | #endif |
Heiko Schocher | e0e55bc | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 83 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 84 | |
| 85 | return 0; |
Akshay Saraswat | e3d7660 | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int dtt_tmu(void) |
| 89 | { |
| 90 | #if defined CONFIG_TMU_CMD_DTT |
| 91 | int cur_temp; |
| 92 | |
| 93 | /* Sense and return latest thermal info */ |
| 94 | if (tmu_monitor(&cur_temp) == TMU_STATUS_INIT) { |
| 95 | puts("TMU is in unknown state, temperature is invalid\n"); |
| 96 | return -1; |
| 97 | } |
| 98 | printf("Current temperature: %u degrees Celsius\n", cur_temp); |
| 99 | #endif |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int do_dtt(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 104 | { |
| 105 | int err = 0; |
| 106 | |
| 107 | err |= dtt_i2c(); |
| 108 | err |= dtt_tmu(); |
| 109 | |
| 110 | return err; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 111 | } /* do_dtt() */ |
| 112 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 113 | /***************************************************/ |
| 114 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 115 | U_BOOT_CMD( |
| 116 | dtt, 1, 1, do_dtt, |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 117 | "Read temperature from Digital Thermometer and Thermostat", |
| 118 | "" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 119 | ); |