2 * (C) Copyright 2007-2008
3 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
4 * based on lm75.c by Bill Hunter
6 * SPDX-License-Identifier: GPL-2.0+
10 * National LM63/LM64 Temperature Sensor
11 * Main difference: LM 64 has -16 Kelvin temperature offset
18 #define DTT_I2C_LM63_ADDR 0x4C /* National LM63 device */
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
29 struct pwm_lookup_entry {
38 int dtt_read(int sensor, int reg)
44 * Calculate sensor address and register.
47 sensor = DTT_I2C_LM63_ADDR; /* legacy config */
52 * Now try to read the register.
54 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
60 int dtt_write(int sensor, int reg, int val)
66 * Calculate sensor address and register.
69 sensor = DTT_I2C_LM63_ADDR; /* legacy config */
72 data[0] = (char)(val & 0xff);
75 * Write value to register.
77 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
83 static int is_lm64(int sensor)
85 return sensor && (sensor != DTT_I2C_LM63_ADDR);
88 int dtt_init_one(int sensor)
93 struct pwm_lookup_entry pwm_lookup[] = CONFIG_DTT_PWM_LOOKUPTABLE;
96 * Set PWM Frequency to 2.5% resolution
99 if (dtt_write(sensor, DTT_PWM_FREQ, val) != 0)
103 * Set Tachometer Limit
105 val = CONFIG_DTT_TACH_LIMIT;
106 if (dtt_write(sensor, DTT_TACHLIM_LSB, val & 0xff) != 0)
108 if (dtt_write(sensor, DTT_TACHLIM_MSB, (val >> 8) & 0xff) != 0)
112 * Make sure PWM Lookup-Table is writeable
114 if (dtt_write(sensor, DTT_FAN_CONFIG, 0x20) != 0)
118 * Setup PWM Lookup-Table
120 for (i = 0; i < ARRAY_SIZE(pwm_lookup); i++) {
121 int address = DTT_PWM_LOOKUP_BASE + 2 * i;
122 val = pwm_lookup[i].temp;
125 if (dtt_write(sensor, address, val) != 0)
127 val = dtt_read(sensor, address);
128 val = pwm_lookup[i].pwm;
129 if (dtt_write(sensor, address + 1, val) != 0)
134 * Enable PWM Lookup-Table, PWM Clock 360 kHz, Tachometer Mode 2
137 if (dtt_write(sensor, DTT_FAN_CONFIG, val) != 0)
143 val = dtt_read(sensor, DTT_CONFIG) | 0x04;
144 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
150 int dtt_get_temp(int sensor)
152 s16 temp = (dtt_read(sensor, DTT_READ_TEMP_RMT_MSB) << 8)
153 | (dtt_read(sensor, DTT_READ_TEMP_RMT_LSB));
158 /* Ignore LSB for now, U-Boot only prints natural numbers */