Ricardo Ribalda Delgado | 3c024d3 | 2008-07-23 19:10:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 |
Ricardo Ribalda Delgado | 5712d04 | 2016-01-26 11:24:08 +0100 | [diff] [blame] | 3 | * Ricado Ribalda-Universidad Autonoma de Madrid, ricardo.ribalda@gmail.com |
Ricardo Ribalda Delgado | 3c024d3 | 2008-07-23 19:10:14 +0200 | [diff] [blame] | 4 | * This work has been supported by: QTechnology http://qtec.com/ |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
Ricardo Ribalda Delgado | 3c024d3 | 2008-07-23 19:10:14 +0200 | [diff] [blame] | 7 | |
| 8 | #include <common.h> |
| 9 | #include <i2c.h> |
| 10 | #include <dtt.h> |
| 11 | |
| 12 | #define ADT7460_ADDRESS 0x2c |
| 13 | #define ADT7460_INVALID 128 |
| 14 | #define ADT7460_CONFIG 0x40 |
| 15 | #define ADT7460_REM1_TEMP 0x25 |
| 16 | #define ADT7460_LOCAL_TEMP 0x26 |
| 17 | #define ADT7460_REM2_TEMP 0x27 |
| 18 | |
| 19 | int dtt_read(int sensor, int reg) |
| 20 | { |
| 21 | u8 dir = reg; |
| 22 | u8 data; |
| 23 | |
| 24 | if (i2c_read(ADT7460_ADDRESS, dir, 1, &data, 1) == -1) |
| 25 | return -1; |
| 26 | if (data == ADT7460_INVALID) |
| 27 | return -1; |
| 28 | |
| 29 | return data; |
| 30 | } |
| 31 | |
| 32 | int dtt_write(int sensor, int reg, int val) |
| 33 | { |
| 34 | u8 dir = reg; |
| 35 | u8 data = val; |
| 36 | |
| 37 | if (i2c_write(ADT7460_ADDRESS, dir, 1, &data, 1) == -1) |
| 38 | return -1; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
Heiko Schocher | 8df768c | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 43 | int dtt_init_one(int sensor) |
Ricardo Ribalda Delgado | 3c024d3 | 2008-07-23 19:10:14 +0200 | [diff] [blame] | 44 | { |
| 45 | printf("ADT7460 at I2C address 0x%2x\n", ADT7460_ADDRESS); |
| 46 | |
| 47 | if (dtt_write(0, ADT7460_CONFIG, 1) == -1) { |
| 48 | puts("Error initialiting ADT7460\n"); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int dtt_get_temp(int sensor) |
| 56 | { |
| 57 | int aux; |
| 58 | u8 table[] = |
| 59 | { ADT7460_REM1_TEMP, ADT7460_LOCAL_TEMP, ADT7460_REM2_TEMP }; |
| 60 | |
| 61 | if (sensor > 2) { |
| 62 | puts("DTT sensor does not exist\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | aux = dtt_read(0, table[sensor]); |
| 67 | if (aux == -1) { |
| 68 | puts("DTT temperature read failed\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | return aux; |
| 73 | } |