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 | 3765b3e | 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 | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 13 | #include <i2c.h> |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 14 | #include <tmu.h> |
Masahiro Yamada | 84b8bf6 | 2016-01-24 23:27:48 +0900 | [diff] [blame] | 15 | #include <linux/bug.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 16 | |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 17 | #if defined CONFIG_DTT_SENSORS |
Heiko Schocher | 780f13a | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 18 | static unsigned long sensor_initialized; |
| 19 | |
Dirk Eibach | b88e7b3 | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 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 | } |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 49 | #endif |
Dirk Eibach | b88e7b3 | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 50 | |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 51 | int dtt_i2c(void) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 52 | { |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 53 | #if defined CONFIG_DTT_SENSORS |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 54 | int i; |
| 55 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 56 | int old_bus; |
| 57 | |
Heiko Schocher | 780f13a | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 58 | /* Force a compilation error, if there are more then 32 sensors */ |
| 59 | BUILD_BUG_ON(sizeof(sensors) > 32); |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 60 | /* switch to correct I2C bus */ |
Heiko Schocher | 3f4978c | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_SYS_I2C |
| 62 | old_bus = i2c_get_bus_num(); |
| 63 | i2c_set_bus_num(CONFIG_SYS_DTT_BUS_NUM); |
| 64 | #else |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 65 | old_bus = I2C_GET_BUS(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 66 | I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM); |
Heiko Schocher | 3f4978c | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 67 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 68 | |
Dirk Eibach | b88e7b3 | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 69 | _initialize_dtt(); |
| 70 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 71 | /* |
| 72 | * Loop through sensors, read |
| 73 | * temperature, and output it. |
| 74 | */ |
Dirk Eibach | b88e7b3 | 2011-10-13 23:23:12 +0000 | [diff] [blame] | 75 | for (i = 0; i < sizeof(sensors); i++) |
Heiko Schocher | 780f13a | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 76 | printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i])); |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 77 | |
| 78 | /* switch back to original I2C bus */ |
Heiko Schocher | 3f4978c | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 79 | #ifdef CONFIG_SYS_I2C |
| 80 | i2c_set_bus_num(old_bus); |
| 81 | #else |
Stefan Roese | 0dc018e | 2007-02-20 10:51:26 +0100 | [diff] [blame] | 82 | I2C_SET_BUS(old_bus); |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 83 | #endif |
Heiko Schocher | 3f4978c | 2012-01-16 21:12:24 +0000 | [diff] [blame] | 84 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 85 | |
| 86 | return 0; |
Akshay Saraswat | bc5478b | 2013-02-25 01:13:04 +0000 | [diff] [blame] | 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; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 112 | } /* do_dtt() */ |
| 113 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 114 | /***************************************************/ |
| 115 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 116 | U_BOOT_CMD( |
| 117 | dtt, 1, 1, do_dtt, |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 118 | "Read temperature from Digital Thermometer and Thermostat", |
| 119 | "" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 120 | ); |