lm81.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * (C) Copyright 2006
  3. * Heiko Schocher, DENX Software Enginnering <hs@denx.de>
  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. * On Semiconductor's LM81 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 0x2c /* ON Semi's LM81 device */
  22. #define DTT_READ_TEMP 0x27
  23. #define DTT_CONFIG_TEMP 0x4b
  24. #define DTT_TEMP_MAX 0x39
  25. #define DTT_TEMP_HYST 0x3a
  26. #define DTT_CONFIG 0x40
  27. int dtt_read(int sensor, int reg)
  28. {
  29. int dlen = 1;
  30. uchar data[2];
  31. /*
  32. * Calculate sensor address and register.
  33. */
  34. sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
  35. /*
  36. * Now try to read the register.
  37. */
  38. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  39. return -1;
  40. return (int)data[0];
  41. } /* dtt_read() */
  42. int dtt_write(int sensor, int reg, int val)
  43. {
  44. uchar data;
  45. /*
  46. * Calculate sensor address and register.
  47. */
  48. sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
  49. data = (char)(val & 0xff);
  50. /*
  51. * Write value to register.
  52. */
  53. if (i2c_write(sensor, reg, 1, &data, 1) != 0)
  54. return 1;
  55. return 0;
  56. } /* dtt_write() */
  57. #define DTT_MANU 0x3e
  58. #define DTT_REV 0x3f
  59. #define DTT_CONFIG 0x40
  60. #define DTT_ADR 0x48
  61. int dtt_init_one(int sensor)
  62. {
  63. int man;
  64. int adr;
  65. int rev;
  66. if (dtt_write (sensor, DTT_CONFIG, 0x01) < 0)
  67. return 1;
  68. /* The LM81 needs 400ms to get the correct values ... */
  69. udelay (400000);
  70. man = dtt_read (sensor, DTT_MANU);
  71. if (man != 0x01)
  72. return 1;
  73. adr = dtt_read (sensor, DTT_ADR);
  74. if (adr < 0)
  75. return 1;
  76. rev = dtt_read (sensor, DTT_REV);
  77. if (rev < 0)
  78. return 1;
  79. debug ("DTT: Found LM81@%x Rev: %d\n", adr, rev);
  80. return 0;
  81. } /* dtt_init_one() */
  82. #define TEMP_FROM_REG(temp) \
  83. ((temp)<256?((((temp)&0x1fe) >> 1) * 10) + ((temp) & 1) * 5: \
  84. ((((temp)&0x1fe) >> 1) -255) * 10 - ((temp) & 1) * 5) \
  85. int dtt_get_temp(int sensor)
  86. {
  87. int val = dtt_read (sensor, DTT_READ_TEMP);
  88. int tmpcnf = dtt_read (sensor, DTT_CONFIG_TEMP);
  89. return (TEMP_FROM_REG((val << 1) + ((tmpcnf & 0x80) >> 7))) / 10;
  90. } /* dtt_get_temp() */