summaryrefslogtreecommitdiff
path: root/chip/lm4/temp_sensor.c
blob: bdb8b42ea64754c269c2f02f03f39b61f3cdbc24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Temperature sensor module for Chrome EC */

#include "adc.h"
#include "board.h"
#include "console.h"
#include "i2c.h"
#include "temp_sensor.h"
#include "uart.h"
#include "util.h"


/* Address of first temp sensor in system */
#define TEMP0_ADDR ((0x40 << 1) | I2C_FLAG_BIG_ENDIAN)
/* TODO: support all 6 temp sensors */

/* Address of battery charger */
#define CHARGER_ADDR 0x12

/* Address of battery */
#define BATTERY_ADDR 0x16


int temp_sensor_read(enum temp_sensor_id id)
{
	int traw, t;
	int rv;

	switch(id) {
	case TEMP_SENSOR_CASE:
		/* TODO: fix temperature correction factor.  For now,
		 * just return the die temperature. */
		return temp_sensor_read(TEMP_SENSOR_CASE_DIE);

	case TEMP_SENSOR_CASE_DIE:
		rv = i2c_read16(I2C_PORT_THERMAL, TEMP0_ADDR, 0x01, &traw);
		if (rv)
			return -1;
		t = (int)(int16_t)traw / 128;
		return t + 273;

	case TEMP_SENSOR_EC_INTERNAL:
		return adc_read_ec_temperature();
	}

	/* If we're still here, we don't handle that sensor */
	return -1;
}


/*****************************************************************************/
/* Console commands */

static int command_temps(int argc, char **argv)
{
	int vraw, v;
	int traw, t;
	int rv;
	int d;

	uart_puts("Reading temperature sensors...\n");

	uart_printf("  Temp from die:   %d K\n\n",
		    temp_sensor_read(TEMP_SENSOR_CASE_DIE));

	uart_puts("Debug data:\n");
	rv = i2c_read16(I2C_PORT_THERMAL, TEMP0_ADDR, 0xfe, &d);
	if (rv)
		return rv;
	uart_printf("  Manufacturer ID: 0x%04x\n", d);
	
	rv = i2c_read16(I2C_PORT_THERMAL, TEMP0_ADDR, 0xff, &d);
	uart_printf("  Device ID:       0x%04x\n", d);

	rv = i2c_read16(I2C_PORT_THERMAL, TEMP0_ADDR, 0x02, &d);
	uart_printf("  Config:          0x%04x\n", d);

	rv = i2c_read16(I2C_PORT_THERMAL, TEMP0_ADDR, 0x00, &vraw);
	v = ((int)(int16_t)vraw * 15625) / 100;
	uart_printf("  Voltage:         0x%04x = %d nV\n", vraw, v);

	rv = i2c_read16(I2C_PORT_THERMAL, TEMP0_ADDR, 0x01, &traw);
	t = ((int)(int16_t)traw * 100) / 128;
	uart_printf("  Temperature:     0x%04x = %d.%02d C\n",
		    traw, t / 100, t > 0 ? t % 100 : 100 - (t % 100));

	/* TODO: correction factor from voltage offset */

	return EC_SUCCESS;
}


/* TODO: the battery charger would normally be on a separate I2C bus.
 * For evaluation, it's on the same bus as the thermal sensor, so I
 * put the debug command here for now. */
static int command_charger(int argc, char **argv)
{
	int rv;
	int d;

	uart_puts("Reading battery charger...\n");

	rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, 0xfe, &d);
	if (rv)
		return rv;
	uart_printf("  Manufacturer ID: 0x%04x\n", d);
	
	rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, 0xff, &d);
	uart_printf("  Device ID:       0x%04x\n", d);

	rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, 0x12, &d);
	uart_printf("  Option:          0x%04x\n", d);

	rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, 0x14, &d);
	uart_printf("  Charge current:  0x%04x\n", d);

	rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, 0x15, &d);
	uart_printf("  Charge voltage:  0x%04x\n", d);

	rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, 0x3f, &d);
	uart_printf("  Input current:   0x%04x\n", d);

	return EC_SUCCESS;
}


/* TODO: the battery would normally be on a separate I2C bus.  For
 * evaluation, it's on the same bus as the thermal sensor so I put the
 * debug command here for now. */
static int command_battery(int argc, char **argv)
{
	int rv;
	int d;

	uart_puts("Reading battery...\n");

	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, 0x08, &d);
	if (rv)
		return rv;
	uart_printf("  Temperature:            0x%04x = %d C\n",
		    d, (d-2731)/10);

	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, 0x09, &d);
	uart_printf("  Voltage:                0x%04x = %d mV\n", d, d);

	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, 0x0f, &d);
	uart_printf("  Remaining capacity:     0x%04x = %d mAh\n", d, d);
	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, 0x10, &d);
	uart_printf("  Full charge capacity:   0x%04x = %d mAh\n", d, d);

	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, 0x14, &d);
	uart_printf("  Desired charge current: 0x%04x = %d mA\n", d, d);
	rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, 0x15, &d);
	uart_printf("  Desired charge voltage: 0x%04x = %d mV\n", d, d);

	
	return EC_SUCCESS;
}


static const struct console_command console_commands[] = {
	{"temps", command_temps},
	{"charger", command_charger},
	{"battery", command_battery},
};
static const struct console_group command_group = {
	"Temp sensor", console_commands, ARRAY_SIZE(console_commands)
};


/*****************************************************************************/
/* Initialization */

int temp_sensor_init(void)
{
	volatile uint32_t scratch  __attribute__((unused));

#ifdef CONFIG_SENSOR
	/* TODO: not necessary since these are the power-on defaults,
	 * except for the DRDY pin.  It's unclear DRDY will be used
	 * anyway. */

	/* Configure the sensor:
	 * 0x7000 = bits 14:12 = continuous conversion
	 * 0x0400 = bits 11:9  = ADC conversion rate (1/sec)
	 * 0x0100 = bit 8      = DRDY pin enabled */
	/* TODO: support shutdown mode for power-saving? */
	i2c_write16(I2C_PORT_THERMAL, TEMP0_ADDR, 0x02, 0x7500);
#endif

	console_register_commands(&command_group);
	return EC_SUCCESS;
}