ds620.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * DS620 DTT support
  3. *
  4. * (C) Copyright 2014 3ADEV <http://www.3adev.com>
  5. * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /*
  10. * Dallas Semiconductor's DS1621/1631 Digital Thermometer and Thermostat.
  11. */
  12. #include <common.h>
  13. #include <i2c.h>
  14. #include <dtt.h>
  15. /*
  16. * Device code
  17. */
  18. #define DTT_I2C_DEV_CODE 0x48
  19. #define DTT_START_CONVERT 0x51
  20. #define DTT_TEMP 0xAA
  21. #define DTT_CONFIG 0xAC
  22. /*
  23. * Config register MSB bits
  24. */
  25. #define DTT_CONFIG_1SHOT 0x01
  26. #define DTT_CONFIG_AUTOC 0x02
  27. #define DTT_CONFIG_R0 0x04 /* always 1 */
  28. #define DTT_CONFIG_R1 0x08 /* always 1 */
  29. #define DTT_CONFIG_TLF 0x10
  30. #define DTT_CONFIG_THF 0x20
  31. #define DTT_CONFIG_NVB 0x40
  32. #define DTT_CONFIG_DONE 0x80
  33. #define CHIP(sensor) (DTT_I2C_DEV_CODE + (sensor & 0x07))
  34. int dtt_init_one(int sensor)
  35. {
  36. uint8_t config = DTT_CONFIG_1SHOT
  37. | DTT_CONFIG_R0
  38. | DTT_CONFIG_R1;
  39. return i2c_write(CHIP(sensor), DTT_CONFIG, 1, &config, 1);
  40. }
  41. int dtt_get_temp(int sensor)
  42. {
  43. uint8_t status;
  44. uint8_t temp[2];
  45. /* Start a conversion, may take up to 1 second. */
  46. i2c_write(CHIP(sensor), DTT_START_CONVERT, 1, NULL, 0);
  47. do {
  48. if (i2c_read(CHIP(sensor), DTT_CONFIG, 1, &status, 1))
  49. /* bail out if I2C error */
  50. status |= DTT_CONFIG_DONE;
  51. } while (!(status & DTT_CONFIG_DONE));
  52. if (i2c_read(CHIP(sensor), DTT_TEMP, 1, temp, 2))
  53. /* bail out if I2C error */
  54. return -274; /* below absolute zero == error */
  55. return ((int16_t)(temp[1] | (temp[0] << 8))) >> 7;
  56. }