adt7460.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * (C) Copyright 2008
  3. * Ricado Ribalda-Universidad Autonoma de Madrid, ricardo.ribalda@gmail.com
  4. * This work has been supported by: QTechnology http://qtec.com/
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <dtt.h>
  10. #define ADT7460_ADDRESS 0x2c
  11. #define ADT7460_INVALID 128
  12. #define ADT7460_CONFIG 0x40
  13. #define ADT7460_REM1_TEMP 0x25
  14. #define ADT7460_LOCAL_TEMP 0x26
  15. #define ADT7460_REM2_TEMP 0x27
  16. int dtt_read(int sensor, int reg)
  17. {
  18. u8 dir = reg;
  19. u8 data;
  20. if (i2c_read(ADT7460_ADDRESS, dir, 1, &data, 1) == -1)
  21. return -1;
  22. if (data == ADT7460_INVALID)
  23. return -1;
  24. return data;
  25. }
  26. int dtt_write(int sensor, int reg, int val)
  27. {
  28. u8 dir = reg;
  29. u8 data = val;
  30. if (i2c_write(ADT7460_ADDRESS, dir, 1, &data, 1) == -1)
  31. return -1;
  32. return 0;
  33. }
  34. int dtt_init_one(int sensor)
  35. {
  36. printf("ADT7460 at I2C address 0x%2x\n", ADT7460_ADDRESS);
  37. if (dtt_write(0, ADT7460_CONFIG, 1) == -1) {
  38. puts("Error initialiting ADT7460\n");
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. int dtt_get_temp(int sensor)
  44. {
  45. int aux;
  46. u8 table[] =
  47. { ADT7460_REM1_TEMP, ADT7460_LOCAL_TEMP, ADT7460_REM2_TEMP };
  48. if (sensor > 2) {
  49. puts("DTT sensor does not exist\n");
  50. return -1;
  51. }
  52. aux = dtt_read(0, table[sensor]);
  53. if (aux == -1) {
  54. puts("DTT temperature read failed\n");
  55. return -1;
  56. }
  57. return aux;
  58. }