dtt.c 2.2 KB

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