lm73.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Larry Johnson, lrj@acm.org
  4. *
  5. * based on dtt/lm75.c which is ...
  6. *
  7. * (C) Copyright 2001
  8. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. /*
  13. * National Semiconductor LM73 Temperature Sensor
  14. */
  15. #include <common.h>
  16. #include <i2c.h>
  17. #include <dtt.h>
  18. /*
  19. * Device code
  20. */
  21. #define DTT_I2C_DEV_CODE 0x48 /* National Semi's LM73 device */
  22. #define DTT_READ_TEMP 0x0
  23. #define DTT_CONFIG 0x1
  24. #define DTT_TEMP_HIGH 0x2
  25. #define DTT_TEMP_LOW 0x3
  26. #define DTT_CONTROL 0x4
  27. #define DTT_ID 0x7
  28. int dtt_read(int const sensor, int const reg)
  29. {
  30. int dlen;
  31. uint8_t data[2];
  32. /*
  33. * Validate 'reg' param and get register size.
  34. */
  35. switch (reg) {
  36. case DTT_CONFIG:
  37. case DTT_CONTROL:
  38. dlen = 1;
  39. break;
  40. case DTT_READ_TEMP:
  41. case DTT_TEMP_HIGH:
  42. case DTT_TEMP_LOW:
  43. case DTT_ID:
  44. dlen = 2;
  45. break;
  46. default:
  47. return -1;
  48. }
  49. /*
  50. * Try to read the register at the calculated sensor address.
  51. */
  52. if (0 !=
  53. i2c_read(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, dlen))
  54. return -1;
  55. /*
  56. * Handle 2 byte result.
  57. */
  58. if (2 == dlen)
  59. return (int)((unsigned)data[0] << 8 | (unsigned)data[1]);
  60. return (int)data[0];
  61. } /* dtt_read() */
  62. int dtt_write(int const sensor, int const reg, int const val)
  63. {
  64. int dlen;
  65. uint8_t data[2];
  66. /*
  67. * Validate 'reg' param and handle register size
  68. */
  69. switch (reg) {
  70. case DTT_CONFIG:
  71. case DTT_CONTROL:
  72. dlen = 1;
  73. data[0] = (uint8_t) val;
  74. break;
  75. case DTT_TEMP_HIGH:
  76. case DTT_TEMP_LOW:
  77. dlen = 2;
  78. data[0] = (uint8_t) (val >> 8); /* MSB first */
  79. data[1] = (uint8_t) val;
  80. break;
  81. default:
  82. return -1;
  83. }
  84. /*
  85. * Write value to register at the calculated sensor address.
  86. */
  87. return 0 != i2c_write(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data,
  88. dlen);
  89. } /* dtt_write() */
  90. int dtt_init_one(int const sensor)
  91. {
  92. int val;
  93. /*
  94. * Validate the Identification register
  95. */
  96. if (0x0190 != dtt_read(sensor, DTT_ID))
  97. return -1;
  98. /*
  99. * Setup THIGH (upper-limit) and TLOW (lower-limit) registers
  100. */
  101. val = CONFIG_SYS_DTT_MAX_TEMP << 7;
  102. if (dtt_write(sensor, DTT_TEMP_HIGH, val))
  103. return -1;
  104. val = CONFIG_SYS_DTT_MIN_TEMP << 7;
  105. if (dtt_write(sensor, DTT_TEMP_LOW, val))
  106. return -1;
  107. /*
  108. * Setup configuraton register
  109. */
  110. /* config = alert active low, disabled, and reset */
  111. val = 0x64;
  112. if (dtt_write(sensor, DTT_CONFIG, val))
  113. return -1;
  114. /*
  115. * Setup control/status register
  116. */
  117. /* control = temp resolution 0.25C */
  118. val = 0x00;
  119. if (dtt_write(sensor, DTT_CONTROL, val))
  120. return -1;
  121. dtt_read(sensor, DTT_CONTROL); /* clear temperature flags */
  122. return 0;
  123. } /* dtt_init_one() */
  124. int dtt_get_temp(int const sensor)
  125. {
  126. int const ret = dtt_read(sensor, DTT_READ_TEMP);
  127. if (ret < 0) {
  128. printf("DTT temperature read failed.\n");
  129. return 0;
  130. }
  131. return (int)((int16_t) ret + 0x0040) >> 7;
  132. } /* dtt_get_temp() */