lm63.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. /*
  25. * National LM63/LM64 Temperature Sensor
  26. * Main difference: LM 64 has -16 Kelvin temperature offset
  27. */
  28. #include <common.h>
  29. #include <i2c.h>
  30. #include <dtt.h>
  31. #define DTT_I2C_LM63_ADDR 0x4C /* National LM63 device */
  32. #define DTT_READ_TEMP_RMT_MSB 0x01
  33. #define DTT_CONFIG 0x03
  34. #define DTT_READ_TEMP_RMT_LSB 0x10
  35. #define DTT_TACHLIM_LSB 0x48
  36. #define DTT_TACHLIM_MSB 0x49
  37. #define DTT_FAN_CONFIG 0x4A
  38. #define DTT_PWM_FREQ 0x4D
  39. #define DTT_PWM_LOOKUP_BASE 0x50
  40. struct pwm_lookup_entry {
  41. u8 temp;
  42. u8 pwm;
  43. };
  44. /*
  45. * Device code
  46. */
  47. int dtt_read(int sensor, int reg)
  48. {
  49. int dlen;
  50. uchar data[2];
  51. /*
  52. * Calculate sensor address and register.
  53. */
  54. if (!sensor)
  55. sensor = DTT_I2C_LM63_ADDR; /* legacy config */
  56. dlen = 1;
  57. /*
  58. * Now try to read the register.
  59. */
  60. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  61. return -1;
  62. return (int)data[0];
  63. } /* dtt_read() */
  64. int dtt_write(int sensor, int reg, int val)
  65. {
  66. int dlen;
  67. uchar data[2];
  68. /*
  69. * Calculate sensor address and register.
  70. */
  71. if (!sensor)
  72. sensor = DTT_I2C_LM63_ADDR; /* legacy config */
  73. dlen = 1;
  74. data[0] = (char)(val & 0xff);
  75. /*
  76. * Write value to register.
  77. */
  78. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  79. return 1;
  80. return 0;
  81. } /* dtt_write() */
  82. static int is_lm64(int sensor)
  83. {
  84. return sensor && (sensor != DTT_I2C_LM63_ADDR);
  85. }
  86. static int _dtt_init(int sensor)
  87. {
  88. int i;
  89. int val;
  90. struct pwm_lookup_entry pwm_lookup[] = CONFIG_DTT_PWM_LOOKUPTABLE;
  91. /*
  92. * Set PWM Frequency to 2.5% resolution
  93. */
  94. val = 20;
  95. if (dtt_write(sensor, DTT_PWM_FREQ, val) != 0)
  96. return 1;
  97. /*
  98. * Set Tachometer Limit
  99. */
  100. val = CONFIG_DTT_TACH_LIMIT;
  101. if (dtt_write(sensor, DTT_TACHLIM_LSB, val & 0xff) != 0)
  102. return 1;
  103. if (dtt_write(sensor, DTT_TACHLIM_MSB, (val >> 8) & 0xff) != 0)
  104. return 1;
  105. /*
  106. * Make sure PWM Lookup-Table is writeable
  107. */
  108. if (dtt_write(sensor, DTT_FAN_CONFIG, 0x20) != 0)
  109. return 1;
  110. /*
  111. * Setup PWM Lookup-Table
  112. */
  113. for (i = 0; i < sizeof(pwm_lookup) / sizeof(struct pwm_lookup_entry);
  114. i++) {
  115. int address = DTT_PWM_LOOKUP_BASE + 2 * i;
  116. val = pwm_lookup[i].temp;
  117. if (is_lm64(sensor))
  118. val -= 16;
  119. if (dtt_write(sensor, address, val) != 0)
  120. return 1;
  121. val = dtt_read(sensor, address);
  122. val = pwm_lookup[i].pwm;
  123. if (dtt_write(sensor, address + 1, val) != 0)
  124. return 1;
  125. }
  126. /*
  127. * Enable PWM Lookup-Table, PWM Clock 360 kHz, Tachometer Mode 2
  128. */
  129. val = 0x02;
  130. if (dtt_write(sensor, DTT_FAN_CONFIG, val) != 0)
  131. return 1;
  132. /*
  133. * Enable Tach input
  134. */
  135. val = dtt_read(sensor, DTT_CONFIG) | 0x04;
  136. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  137. return 1;
  138. return 0;
  139. }
  140. int dtt_get_temp(int sensor)
  141. {
  142. s16 temp = (dtt_read(sensor, DTT_READ_TEMP_RMT_MSB) << 8)
  143. | (dtt_read(sensor, DTT_READ_TEMP_RMT_LSB));
  144. if (is_lm64(sensor))
  145. temp += 16 << 8;
  146. /* Ignore LSB for now, U-Boot only prints natural numbers */
  147. return temp >> 8;
  148. }
  149. int dtt_init(void)
  150. {
  151. int i;
  152. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  153. const char *const header = "DTT: ";
  154. for (i = 0; i < sizeof(sensors); i++) {
  155. if (_dtt_init(sensors[i]) != 0)
  156. printf("%s%d FAILED INIT\n", header, i + 1);
  157. else
  158. printf("%s%d is %i C\n", header, i + 1,
  159. dtt_get_temp(sensors[i]));
  160. }
  161. return 0;
  162. }