Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2008 |
| 3 | * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de |
| 4 | * based on lm75.c by Bill Hunter |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 10 | * National LM63/LM64 Temperature Sensor |
| 11 | * Main difference: LM 64 has -16 Kelvin temperature offset |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <i2c.h> |
| 16 | #include <dtt.h> |
| 17 | |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 18 | #define DTT_I2C_LM63_ADDR 0x4C /* National LM63 device */ |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 19 | |
| 20 | #define DTT_READ_TEMP_RMT_MSB 0x01 |
| 21 | #define DTT_CONFIG 0x03 |
| 22 | #define DTT_READ_TEMP_RMT_LSB 0x10 |
| 23 | #define DTT_TACHLIM_LSB 0x48 |
| 24 | #define DTT_TACHLIM_MSB 0x49 |
| 25 | #define DTT_FAN_CONFIG 0x4A |
| 26 | #define DTT_PWM_FREQ 0x4D |
| 27 | #define DTT_PWM_LOOKUP_BASE 0x50 |
| 28 | |
| 29 | struct pwm_lookup_entry { |
| 30 | u8 temp; |
| 31 | u8 pwm; |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * Device code |
| 36 | */ |
| 37 | |
| 38 | int dtt_read(int sensor, int reg) |
| 39 | { |
| 40 | int dlen; |
| 41 | uchar data[2]; |
| 42 | |
| 43 | /* |
| 44 | * Calculate sensor address and register. |
| 45 | */ |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 46 | if (!sensor) |
| 47 | sensor = DTT_I2C_LM63_ADDR; /* legacy config */ |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 48 | |
| 49 | dlen = 1; |
| 50 | |
| 51 | /* |
| 52 | * Now try to read the register. |
| 53 | */ |
| 54 | if (i2c_read(sensor, reg, 1, data, dlen) != 0) |
| 55 | return -1; |
| 56 | |
| 57 | return (int)data[0]; |
| 58 | } /* dtt_read() */ |
| 59 | |
| 60 | int dtt_write(int sensor, int reg, int val) |
| 61 | { |
| 62 | int dlen; |
| 63 | uchar data[2]; |
| 64 | |
| 65 | /* |
| 66 | * Calculate sensor address and register. |
| 67 | */ |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 68 | if (!sensor) |
| 69 | sensor = DTT_I2C_LM63_ADDR; /* legacy config */ |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 70 | |
| 71 | dlen = 1; |
| 72 | data[0] = (char)(val & 0xff); |
| 73 | |
| 74 | /* |
| 75 | * Write value to register. |
| 76 | */ |
| 77 | if (i2c_write(sensor, reg, 1, data, dlen) != 0) |
| 78 | return 1; |
| 79 | |
| 80 | return 0; |
| 81 | } /* dtt_write() */ |
| 82 | |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 83 | static int is_lm64(int sensor) |
| 84 | { |
| 85 | return sensor && (sensor != DTT_I2C_LM63_ADDR); |
| 86 | } |
| 87 | |
Heiko Schocher | 780f13a | 2011-08-01 04:01:43 +0000 | [diff] [blame] | 88 | int dtt_init_one(int sensor) |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 89 | { |
| 90 | int i; |
| 91 | int val; |
| 92 | |
| 93 | struct pwm_lookup_entry pwm_lookup[] = CONFIG_DTT_PWM_LOOKUPTABLE; |
| 94 | |
| 95 | /* |
| 96 | * Set PWM Frequency to 2.5% resolution |
| 97 | */ |
| 98 | val = 20; |
| 99 | if (dtt_write(sensor, DTT_PWM_FREQ, val) != 0) |
| 100 | return 1; |
| 101 | |
| 102 | /* |
| 103 | * Set Tachometer Limit |
| 104 | */ |
| 105 | val = CONFIG_DTT_TACH_LIMIT; |
| 106 | if (dtt_write(sensor, DTT_TACHLIM_LSB, val & 0xff) != 0) |
| 107 | return 1; |
| 108 | if (dtt_write(sensor, DTT_TACHLIM_MSB, (val >> 8) & 0xff) != 0) |
| 109 | return 1; |
| 110 | |
| 111 | /* |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 112 | * Make sure PWM Lookup-Table is writeable |
| 113 | */ |
| 114 | if (dtt_write(sensor, DTT_FAN_CONFIG, 0x20) != 0) |
| 115 | return 1; |
| 116 | |
| 117 | /* |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 118 | * Setup PWM Lookup-Table |
| 119 | */ |
Axel Lin | b98bfeb | 2013-06-23 00:55:14 +0800 | [diff] [blame] | 120 | for (i = 0; i < ARRAY_SIZE(pwm_lookup); i++) { |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 121 | int address = DTT_PWM_LOOKUP_BASE + 2 * i; |
| 122 | val = pwm_lookup[i].temp; |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 123 | if (is_lm64(sensor)) |
| 124 | val -= 16; |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 125 | if (dtt_write(sensor, address, val) != 0) |
| 126 | return 1; |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 127 | val = dtt_read(sensor, address); |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 128 | val = pwm_lookup[i].pwm; |
| 129 | if (dtt_write(sensor, address + 1, val) != 0) |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Enable PWM Lookup-Table, PWM Clock 360 kHz, Tachometer Mode 2 |
| 135 | */ |
| 136 | val = 0x02; |
| 137 | if (dtt_write(sensor, DTT_FAN_CONFIG, val) != 0) |
| 138 | return 1; |
| 139 | |
| 140 | /* |
| 141 | * Enable Tach input |
| 142 | */ |
| 143 | val = dtt_read(sensor, DTT_CONFIG) | 0x04; |
| 144 | if (dtt_write(sensor, DTT_CONFIG, val) != 0) |
| 145 | return 1; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int dtt_get_temp(int sensor) |
| 151 | { |
| 152 | s16 temp = (dtt_read(sensor, DTT_READ_TEMP_RMT_MSB) << 8) |
| 153 | | (dtt_read(sensor, DTT_READ_TEMP_RMT_LSB)); |
| 154 | |
Dirk Eibach | 31c1bdd | 2011-04-18 10:43:14 +0200 | [diff] [blame] | 155 | if (is_lm64(sensor)) |
| 156 | temp += 16 << 8; |
| 157 | |
Dirk Eibach | 1c6fe6e | 2008-10-08 13:44:27 +0200 | [diff] [blame] | 158 | /* Ignore LSB for now, U-Boot only prints natural numbers */ |
| 159 | return temp >> 8; |
| 160 | } |