summaryrefslogtreecommitdiff
path: root/common/temp_sensor.c
blob: 5579683aa84d7ec09dc5ba87866574837871aa8e (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
/* 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 "chip_temp_sensor.h"
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "i2c.h"
#include "host_command.h"
#include "peci.h"
#include "task.h"
#include "temp_sensor.h"
#include "thermal.h"
#include "timer.h"
#include "tmp006.h"
#include "util.h"

/* Defined in board_temp_sensor.c. Must be in the same order as
 * in enum temp_sensor_id.
 */
extern const struct temp_sensor_t temp_sensors[TEMP_SENSOR_COUNT];


int temp_sensor_read(enum temp_sensor_id id)
{
	const struct temp_sensor_t *sensor;

	if (id < 0 || id >= TEMP_SENSOR_COUNT)
		return -1;
	sensor = temp_sensors + id;
	return sensor->read(sensor->idx);
}


int temp_sensor_powered(enum temp_sensor_id id)
{
	int flag = temp_sensors[id].power_flags;

	if (flag & TEMP_SENSOR_POWER_VS &&
	    gpio_get_level(GPIO_PGOOD_1_8VS) == 0)
		return 0;

	if (flag & TEMP_SENSOR_POWER_CPU &&
	    !chipset_in_state(CHIPSET_STATE_ON))
		return 0;

	return 1;
}


void poll_slow_sensors(void)
{
	/* Poll every second */
#ifdef CONFIG_TMP006
	tmp006_poll();
#endif
#ifdef CHIP_lm4
	chip_temp_sensor_poll();
#endif
}

static void poll_fast_sensors(void)
{
	/* Poll every 1/4 second */
#ifdef CONFIG_PECI
	peci_temp_sensor_poll();
#endif
}


static void update_mapped_memory(void)
{
	int i, t;
	uint8_t *mapped = host_get_memmap(EC_MEMMAP_TEMP_SENSOR);

	memset(mapped, 0xff, 16);

	for (i = 0; i < TEMP_SENSOR_COUNT && i < 16; ++i) {
		if (!temp_sensor_powered(i)) {
			mapped[i] = 0xfd;
			continue;
		}
		t = temp_sensor_read(i);
		if (t != -1)
			mapped[i] = t - EC_TEMP_SENSOR_OFFSET;
		else
			mapped[i] = 0xfe;
	}
}


void temp_sensor_task(void)
{
	int i;

	/* Switch data is now present */
	*host_get_memmap(EC_MEMMAP_THERMAL_VERSION) = 1;

	while (1) {
		for (i = 0; i < 4; ++i) {
			usleep(250000);
			poll_fast_sensors();
		}
		poll_slow_sensors();
		update_mapped_memory();
	}
}

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

static int command_temps(int argc, char **argv)
{
	int i;
	int rv = EC_SUCCESS;
	int t;

	for (i = 0; i < TEMP_SENSOR_COUNT; ++i) {
		ccprintf("  %-20s: ", temp_sensors[i].name);
		t = temp_sensor_read(i);
		if (t < 0) {
			ccprintf("Error\n");
			rv = EC_ERROR_UNKNOWN;
		} else
			ccprintf("%d K = %d C\n", t, t - 273);
	}

	return rv;
}
DECLARE_CONSOLE_COMMAND(temps, command_temps,
			NULL,
			"Print temp sensors",
			NULL);