adt7460.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * (C) Copyright 2008
  3. * Ricado Ribalda-Universidad Autonoma de Madrid, ricardo.ribalda@uam.es
  4. * This work has been supported by: QTechnology http://qtec.com/
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <common.h>
  18. #include <i2c.h>
  19. #include <dtt.h>
  20. #define ADT7460_ADDRESS 0x2c
  21. #define ADT7460_INVALID 128
  22. #define ADT7460_CONFIG 0x40
  23. #define ADT7460_REM1_TEMP 0x25
  24. #define ADT7460_LOCAL_TEMP 0x26
  25. #define ADT7460_REM2_TEMP 0x27
  26. int dtt_read(int sensor, int reg)
  27. {
  28. u8 dir = reg;
  29. u8 data;
  30. if (i2c_read(ADT7460_ADDRESS, dir, 1, &data, 1) == -1)
  31. return -1;
  32. if (data == ADT7460_INVALID)
  33. return -1;
  34. return data;
  35. }
  36. int dtt_write(int sensor, int reg, int val)
  37. {
  38. u8 dir = reg;
  39. u8 data = val;
  40. if (i2c_write(ADT7460_ADDRESS, dir, 1, &data, 1) == -1)
  41. return -1;
  42. return 0;
  43. }
  44. int dtt_init(void)
  45. {
  46. printf("ADT7460 at I2C address 0x%2x\n", ADT7460_ADDRESS);
  47. if (dtt_write(0, ADT7460_CONFIG, 1) == -1) {
  48. puts("Error initialiting ADT7460\n");
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. int dtt_get_temp(int sensor)
  54. {
  55. int aux;
  56. u8 table[] =
  57. { ADT7460_REM1_TEMP, ADT7460_LOCAL_TEMP, ADT7460_REM2_TEMP };
  58. if (sensor > 2) {
  59. puts("DTT sensor does not exist\n");
  60. return -1;
  61. }
  62. aux = dtt_read(0, table[sensor]);
  63. if (aux == -1) {
  64. puts("DTT temperature read failed\n");
  65. return -1;
  66. }
  67. return aux;
  68. }