cmd_dtt.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * (C) Copyright 2001
  3. * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <config.h>
  9. #include <command.h>
  10. #include <dtt.h>
  11. #include <i2c.h>
  12. #include <tmu.h>
  13. #if defined CONFIG_DTT_SENSORS
  14. static unsigned long sensor_initialized;
  15. static void _initialize_dtt(void)
  16. {
  17. int i;
  18. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  19. for (i = 0; i < sizeof(sensors); i++) {
  20. if ((sensor_initialized & (1 << i)) == 0) {
  21. if (dtt_init_one(sensors[i]) != 0) {
  22. printf("DTT%d: Failed init!\n", i);
  23. continue;
  24. }
  25. sensor_initialized |= (1 << i);
  26. }
  27. }
  28. }
  29. void dtt_init(void)
  30. {
  31. int old_bus;
  32. /* switch to correct I2C bus */
  33. old_bus = I2C_GET_BUS();
  34. I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
  35. _initialize_dtt();
  36. /* switch back to original I2C bus */
  37. I2C_SET_BUS(old_bus);
  38. }
  39. #endif
  40. int dtt_i2c(void)
  41. {
  42. #if defined CONFIG_DTT_SENSORS
  43. int i;
  44. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  45. int old_bus;
  46. /* Force a compilation error, if there are more then 32 sensors */
  47. BUILD_BUG_ON(sizeof(sensors) > 32);
  48. /* switch to correct I2C bus */
  49. #ifdef CONFIG_SYS_I2C
  50. old_bus = i2c_get_bus_num();
  51. i2c_set_bus_num(CONFIG_SYS_DTT_BUS_NUM);
  52. #else
  53. old_bus = I2C_GET_BUS();
  54. I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
  55. #endif
  56. _initialize_dtt();
  57. /*
  58. * Loop through sensors, read
  59. * temperature, and output it.
  60. */
  61. for (i = 0; i < sizeof(sensors); i++)
  62. printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i]));
  63. /* switch back to original I2C bus */
  64. #ifdef CONFIG_SYS_I2C
  65. i2c_set_bus_num(old_bus);
  66. #else
  67. I2C_SET_BUS(old_bus);
  68. #endif
  69. #endif
  70. return 0;
  71. }
  72. int dtt_tmu(void)
  73. {
  74. #if defined CONFIG_TMU_CMD_DTT
  75. int cur_temp;
  76. /* Sense and return latest thermal info */
  77. if (tmu_monitor(&cur_temp) == TMU_STATUS_INIT) {
  78. puts("TMU is in unknown state, temperature is invalid\n");
  79. return -1;
  80. }
  81. printf("Current temperature: %u degrees Celsius\n", cur_temp);
  82. #endif
  83. return 0;
  84. }
  85. int do_dtt(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  86. {
  87. int err = 0;
  88. err |= dtt_i2c();
  89. err |= dtt_tmu();
  90. return err;
  91. } /* do_dtt() */
  92. /***************************************************/
  93. U_BOOT_CMD(
  94. dtt, 1, 1, do_dtt,
  95. "Read temperature from Digital Thermometer and Thermostat",
  96. ""
  97. );