Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 1 | /* |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 2 | * (C) Copyright 2007-2008 |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 3 | * Larry Johnson, lrj@acm.org |
| 4 | * |
| 5 | * based on dtt/lm75.c which is ... |
| 6 | * |
| 7 | * (C) Copyright 2001 |
| 8 | * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net |
| 9 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * National Semiconductor LM73 Temperature Sensor |
| 15 | */ |
| 16 | |
| 17 | #include <common.h> |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 18 | #include <i2c.h> |
| 19 | #include <dtt.h> |
| 20 | |
| 21 | /* |
| 22 | * Device code |
| 23 | */ |
| 24 | #define DTT_I2C_DEV_CODE 0x48 /* National Semi's LM73 device */ |
Michal Simek | 10de36d | 2008-07-11 11:50:53 +0200 | [diff] [blame] | 25 | #define DTT_READ_TEMP 0x0 |
| 26 | #define DTT_CONFIG 0x1 |
| 27 | #define DTT_TEMP_HIGH 0x2 |
| 28 | #define DTT_TEMP_LOW 0x3 |
| 29 | #define DTT_CONTROL 0x4 |
| 30 | #define DTT_ID 0x7 |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 31 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 32 | int dtt_read(int const sensor, int const reg) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 33 | { |
| 34 | int dlen; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 35 | uint8_t data[2]; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Validate 'reg' param and get register size. |
| 39 | */ |
| 40 | switch (reg) { |
| 41 | case DTT_CONFIG: |
| 42 | case DTT_CONTROL: |
| 43 | dlen = 1; |
| 44 | break; |
| 45 | case DTT_READ_TEMP: |
| 46 | case DTT_TEMP_HIGH: |
| 47 | case DTT_TEMP_LOW: |
| 48 | case DTT_ID: |
| 49 | dlen = 2; |
| 50 | break; |
| 51 | default: |
| 52 | return -1; |
| 53 | } |
| 54 | /* |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 55 | * Try to read the register at the calculated sensor address. |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 56 | */ |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 57 | if (0 != |
| 58 | i2c_read(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, dlen)) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 59 | return -1; |
| 60 | /* |
| 61 | * Handle 2 byte result. |
| 62 | */ |
| 63 | if (2 == dlen) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 64 | return (int)((unsigned)data[0] << 8 | (unsigned)data[1]); |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 65 | |
| 66 | return (int)data[0]; |
| 67 | } /* dtt_read() */ |
| 68 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 69 | int dtt_write(int const sensor, int const reg, int const val) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 70 | { |
| 71 | int dlen; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 72 | uint8_t data[2]; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Validate 'reg' param and handle register size |
| 76 | */ |
| 77 | switch (reg) { |
| 78 | case DTT_CONFIG: |
| 79 | case DTT_CONTROL: |
| 80 | dlen = 1; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 81 | data[0] = (uint8_t) val; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 82 | break; |
| 83 | case DTT_TEMP_HIGH: |
| 84 | case DTT_TEMP_LOW: |
| 85 | dlen = 2; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 86 | data[0] = (uint8_t) (val >> 8); /* MSB first */ |
| 87 | data[1] = (uint8_t) val; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 88 | break; |
| 89 | default: |
| 90 | return -1; |
| 91 | } |
| 92 | /* |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 93 | * Write value to register at the calculated sensor address. |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 94 | */ |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 95 | return 0 != i2c_write(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, |
| 96 | dlen); |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 97 | } /* dtt_write() */ |
| 98 | |
Heiko Schocher | 8df768c | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 99 | int dtt_init_one(int const sensor) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 100 | { |
| 101 | int val; |
| 102 | |
| 103 | /* |
| 104 | * Validate the Identification register |
| 105 | */ |
| 106 | if (0x0190 != dtt_read(sensor, DTT_ID)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 107 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 108 | /* |
| 109 | * Setup THIGH (upper-limit) and TLOW (lower-limit) registers |
| 110 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 111 | val = CONFIG_SYS_DTT_MAX_TEMP << 7; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 112 | if (dtt_write(sensor, DTT_TEMP_HIGH, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 113 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 114 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 115 | val = CONFIG_SYS_DTT_MIN_TEMP << 7; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 116 | if (dtt_write(sensor, DTT_TEMP_LOW, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 117 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 118 | /* |
| 119 | * Setup configuraton register |
| 120 | */ |
| 121 | /* config = alert active low, disabled, and reset */ |
| 122 | val = 0x64; |
| 123 | if (dtt_write(sensor, DTT_CONFIG, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 124 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 125 | /* |
| 126 | * Setup control/status register |
| 127 | */ |
| 128 | /* control = temp resolution 0.25C */ |
| 129 | val = 0x00; |
| 130 | if (dtt_write(sensor, DTT_CONTROL, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 131 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 132 | |
| 133 | dtt_read(sensor, DTT_CONTROL); /* clear temperature flags */ |
| 134 | return 0; |
Heiko Schocher | 8df768c | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 135 | } /* dtt_init_one() */ |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 136 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 137 | int dtt_get_temp(int const sensor) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 138 | { |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame] | 139 | int const ret = dtt_read(sensor, DTT_READ_TEMP); |
| 140 | |
| 141 | if (ret < 0) { |
| 142 | printf("DTT temperature read failed.\n"); |
| 143 | return 0; |
| 144 | } |
| 145 | return (int)((int16_t) ret + 0x0040) >> 7; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 146 | } /* dtt_get_temp() */ |