wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * On Semiconductor's LM75 Temperature Sensor |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | #include <i2c.h> |
| 14 | #include <dtt.h> |
| 15 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 16 | /* |
| 17 | * Device code |
| 18 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 19 | #if defined(CONFIG_SYS_I2C_DTT_ADDR) |
| 20 | #define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR |
Victor Gallardo | 9ebbb54 | 2008-09-09 15:13:29 -0700 | [diff] [blame] | 21 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */ |
Victor Gallardo | 9ebbb54 | 2008-09-09 15:13:29 -0700 | [diff] [blame] | 23 | #endif |
Michal Simek | 6ecbb45 | 2008-07-11 11:50:53 +0200 | [diff] [blame] | 24 | #define DTT_READ_TEMP 0x0 |
| 25 | #define DTT_CONFIG 0x1 |
| 26 | #define DTT_TEMP_HYST 0x2 |
| 27 | #define DTT_TEMP_SET 0x3 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | |
| 29 | int dtt_read(int sensor, int reg) |
| 30 | { |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 31 | int dlen; |
| 32 | uchar data[2]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | |
Stefan Roese | 4d91d1d | 2008-05-16 11:06:06 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_DTT_AD7414 |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 35 | /* |
| 36 | * On AD7414 the first value upon bootup is not read correctly. |
| 37 | * This is most likely because of the 800ms update time of the |
| 38 | * temp register in normal update mode. To get current values |
| 39 | * each time we issue the "dtt" command including upon powerup |
| 40 | * we switch into one-short mode. |
| 41 | * |
| 42 | * Issue one-shot mode command |
| 43 | */ |
| 44 | dtt_write(sensor, DTT_CONFIG, 0x64); |
Stefan Roese | 4d91d1d | 2008-05-16 11:06:06 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 47 | /* Validate 'reg' param */ |
| 48 | if((reg < 0) || (reg > 3)) |
| 49 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 51 | /* Calculate sensor address and register. */ |
| 52 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 54 | /* Prepare to handle 2 byte result. */ |
| 55 | if ((reg == DTT_READ_TEMP) || |
| 56 | (reg == DTT_TEMP_HYST) || |
| 57 | (reg == DTT_TEMP_SET)) |
| 58 | dlen = 2; |
| 59 | else |
| 60 | dlen = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 61 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 62 | /* Now try to read the register. */ |
| 63 | if (i2c_read(sensor, reg, 1, data, dlen) != 0) |
| 64 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 66 | /* Handle 2 byte result. */ |
| 67 | if (dlen == 2) |
| 68 | return ((int)((short)data[1] + (((short)data[0]) << 8))); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 70 | return (int)data[0]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | } /* dtt_read() */ |
| 72 | |
| 73 | |
| 74 | int dtt_write(int sensor, int reg, int val) |
| 75 | { |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 76 | int dlen; |
| 77 | uchar data[2]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 78 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 79 | /* Validate 'reg' param */ |
| 80 | if ((reg < 0) || (reg > 3)) |
| 81 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 82 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 83 | /* Calculate sensor address and register. */ |
| 84 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 86 | /* Handle 2 byte values. */ |
| 87 | if ((reg == DTT_READ_TEMP) || |
| 88 | (reg == DTT_TEMP_HYST) || |
| 89 | (reg == DTT_TEMP_SET)) { |
| 90 | dlen = 2; |
| 91 | data[0] = (char)((val >> 8) & 0xff); /* MSB first */ |
| 92 | data[1] = (char)(val & 0xff); |
| 93 | } else { |
| 94 | dlen = 1; |
| 95 | data[0] = (char)(val & 0xff); |
| 96 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 97 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 98 | /* Write value to register. */ |
| 99 | if (i2c_write(sensor, reg, 1, data, dlen) != 0) |
| 100 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 101 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 102 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | } /* dtt_write() */ |
| 104 | |
| 105 | |
Heiko Schocher | 780f13a | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 106 | int dtt_init_one(int sensor) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 107 | { |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 108 | int val; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 110 | /* Setup TSET ( trip point ) register */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 111 | val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */ |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 112 | if (dtt_write(sensor, DTT_TEMP_SET, val) != 0) |
| 113 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 115 | /* Setup THYST ( untrip point ) register - Hysteresis */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80; |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 117 | if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0) |
| 118 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 120 | /* Setup configuraton register */ |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 121 | #ifdef CONFIG_DTT_AD7414 |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 122 | /* config = alert active low and disabled */ |
| 123 | val = 0x60; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 124 | #else |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 125 | /* config = 6 sample integration, int mode, active low, and enable */ |
| 126 | val = 0x18; |
Stefan Roese | 887e2ec | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 127 | #endif |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 128 | if (dtt_write(sensor, DTT_CONFIG, val) != 0) |
| 129 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 130 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 131 | return 0; |
Heiko Schocher | 780f13a | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 132 | } /* dtt_init_one() */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | |
| 134 | int dtt_get_temp(int sensor) |
| 135 | { |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 136 | int const ret = dtt_read(sensor, DTT_READ_TEMP); |
Larry Johnson | d01b847 | 2008-02-21 13:58:16 -0500 | [diff] [blame] | 137 | |
Heiko Schocher | 12f1678 | 2008-10-15 09:37:04 +0200 | [diff] [blame] | 138 | if (ret < 0) { |
| 139 | printf("DTT temperature read failed.\n"); |
| 140 | return 0; |
| 141 | } |
| 142 | return (int)((int16_t) ret / 256); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 143 | } /* dtt_get_temp() */ |