lm63.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
  4. * based on lm75.c by Bill Hunter
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * National LM63/LM64 Temperature Sensor
  10. * Main difference: LM 64 has -16 Kelvin temperature offset
  11. */
  12. #include <common.h>
  13. #include <i2c.h>
  14. #include <dtt.h>
  15. #define DTT_I2C_LM63_ADDR 0x4C /* National LM63 device */
  16. #define DTT_READ_TEMP_RMT_MSB 0x01
  17. #define DTT_CONFIG 0x03
  18. #define DTT_READ_TEMP_RMT_LSB 0x10
  19. #define DTT_TACHLIM_LSB 0x48
  20. #define DTT_TACHLIM_MSB 0x49
  21. #define DTT_FAN_CONFIG 0x4A
  22. #define DTT_PWM_FREQ 0x4D
  23. #define DTT_PWM_LOOKUP_BASE 0x50
  24. struct pwm_lookup_entry {
  25. u8 temp;
  26. u8 pwm;
  27. };
  28. /*
  29. * Device code
  30. */
  31. int dtt_read(int sensor, int reg)
  32. {
  33. int dlen;
  34. uchar data[2];
  35. /*
  36. * Calculate sensor address and register.
  37. */
  38. if (!sensor)
  39. sensor = DTT_I2C_LM63_ADDR; /* legacy config */
  40. dlen = 1;
  41. /*
  42. * Now try to read the register.
  43. */
  44. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  45. return -1;
  46. return (int)data[0];
  47. } /* dtt_read() */
  48. int dtt_write(int sensor, int reg, int val)
  49. {
  50. int dlen;
  51. uchar data[2];
  52. /*
  53. * Calculate sensor address and register.
  54. */
  55. if (!sensor)
  56. sensor = DTT_I2C_LM63_ADDR; /* legacy config */
  57. dlen = 1;
  58. data[0] = (char)(val & 0xff);
  59. /*
  60. * Write value to register.
  61. */
  62. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  63. return 1;
  64. return 0;
  65. } /* dtt_write() */
  66. static int is_lm64(int sensor)
  67. {
  68. return sensor && (sensor != DTT_I2C_LM63_ADDR);
  69. }
  70. int dtt_init_one(int sensor)
  71. {
  72. int i;
  73. int val;
  74. struct pwm_lookup_entry pwm_lookup[] = CONFIG_DTT_PWM_LOOKUPTABLE;
  75. /*
  76. * Set PWM Frequency to 2.5% resolution
  77. */
  78. val = 20;
  79. if (dtt_write(sensor, DTT_PWM_FREQ, val) != 0)
  80. return 1;
  81. /*
  82. * Set Tachometer Limit
  83. */
  84. val = CONFIG_DTT_TACH_LIMIT;
  85. if (dtt_write(sensor, DTT_TACHLIM_LSB, val & 0xff) != 0)
  86. return 1;
  87. if (dtt_write(sensor, DTT_TACHLIM_MSB, (val >> 8) & 0xff) != 0)
  88. return 1;
  89. /*
  90. * Make sure PWM Lookup-Table is writeable
  91. */
  92. if (dtt_write(sensor, DTT_FAN_CONFIG, 0x20) != 0)
  93. return 1;
  94. /*
  95. * Setup PWM Lookup-Table
  96. */
  97. for (i = 0; i < ARRAY_SIZE(pwm_lookup); i++) {
  98. int address = DTT_PWM_LOOKUP_BASE + 2 * i;
  99. val = pwm_lookup[i].temp;
  100. if (is_lm64(sensor))
  101. val -= 16;
  102. if (dtt_write(sensor, address, val) != 0)
  103. return 1;
  104. val = dtt_read(sensor, address);
  105. val = pwm_lookup[i].pwm;
  106. if (dtt_write(sensor, address + 1, val) != 0)
  107. return 1;
  108. }
  109. /*
  110. * Enable PWM Lookup-Table, PWM Clock 360 kHz, Tachometer Mode 2
  111. */
  112. val = 0x02;
  113. if (dtt_write(sensor, DTT_FAN_CONFIG, val) != 0)
  114. return 1;
  115. /*
  116. * Enable Tach input
  117. */
  118. val = dtt_read(sensor, DTT_CONFIG) | 0x04;
  119. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  120. return 1;
  121. return 0;
  122. }
  123. int dtt_get_temp(int sensor)
  124. {
  125. s16 temp = (dtt_read(sensor, DTT_READ_TEMP_RMT_MSB) << 8)
  126. | (dtt_read(sensor, DTT_READ_TEMP_RMT_LSB));
  127. if (is_lm64(sensor))
  128. temp += 16 << 8;
  129. /* Ignore LSB for now, U-Boot only prints natural numbers */
  130. return temp >> 8;
  131. }