Albert ARIBAUD \(3ADEV\) | 24d528e | 2015-03-31 11:40:48 +0200 | [diff] [blame] | 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 | /* |
| 11 | * Dallas Semiconductor's DS1621/1631 Digital Thermometer and Thermostat. |
| 12 | */ |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <i2c.h> |
| 16 | #include <dtt.h> |
| 17 | |
| 18 | /* |
| 19 | * Device code |
| 20 | */ |
| 21 | #define DTT_I2C_DEV_CODE 0x48 |
| 22 | #define DTT_START_CONVERT 0x51 |
| 23 | #define DTT_TEMP 0xAA |
| 24 | #define DTT_CONFIG 0xAC |
| 25 | |
| 26 | /* |
| 27 | * Config register MSB bits |
| 28 | */ |
| 29 | #define DTT_CONFIG_1SHOT 0x01 |
| 30 | #define DTT_CONFIG_AUTOC 0x02 |
| 31 | #define DTT_CONFIG_R0 0x04 /* always 1 */ |
| 32 | #define DTT_CONFIG_R1 0x08 /* always 1 */ |
| 33 | #define DTT_CONFIG_TLF 0x10 |
| 34 | #define DTT_CONFIG_THF 0x20 |
| 35 | #define DTT_CONFIG_NVB 0x40 |
| 36 | #define DTT_CONFIG_DONE 0x80 |
| 37 | |
| 38 | #define CHIP(sensor) (DTT_I2C_DEV_CODE + (sensor & 0x07)) |
| 39 | |
| 40 | int dtt_init_one(int sensor) |
| 41 | { |
| 42 | uint8_t config = DTT_CONFIG_1SHOT |
| 43 | | DTT_CONFIG_R0 |
| 44 | | DTT_CONFIG_R1; |
| 45 | return i2c_write(CHIP(sensor), DTT_CONFIG, 1, &config, 1); |
| 46 | } |
| 47 | |
| 48 | int dtt_get_temp(int sensor) |
| 49 | { |
| 50 | uint8_t status; |
| 51 | uint8_t temp[2]; |
| 52 | |
| 53 | /* Start a conversion, may take up to 1 second. */ |
| 54 | i2c_write(CHIP(sensor), DTT_START_CONVERT, 1, NULL, 0); |
| 55 | do { |
| 56 | if (i2c_read(CHIP(sensor), DTT_CONFIG, 1, &status, 1)) |
| 57 | /* bail out if I2C error */ |
| 58 | status |= DTT_CONFIG_DONE; |
| 59 | } while (!(status & DTT_CONFIG_DONE)); |
| 60 | if (i2c_read(CHIP(sensor), DTT_TEMP, 1, temp, 2)) |
| 61 | /* bail out if I2C error */ |
| 62 | return -274; /* below absolute zero == error */ |
| 63 | |
| 64 | return ((int16_t)(temp[1] | (temp[0] << 8))) >> 7; |
| 65 | } |