summaryrefslogtreecommitdiff
path: root/common/temp_sensor_g781.c
blob: 96dd18ea6071292ca4173ea1638bd8415da8c638 (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
/* Copyright (c) 2013 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.
 */

/* G781 temperature sensor module for Chrome EC */

#include "common.h"
#include "console.h"
#include "i2c.h"
#include "hooks.h"
#include "temp_sensor_g781.h"
#include "util.h"

static int g781_temp_val_local;
static int g781_temp_val_remote;

static int g781_read8(const int offset, int *data_ptr)
{
	return i2c_read8(I2C_PORT_THERMAL, G781_I2C_ADDR, offset, data_ptr);
}

static int g781_write8(const int offset, int data)
{
	return i2c_write8(I2C_PORT_THERMAL, G781_I2C_ADDR, offset, data);
}

static int g781_get_temp(const int offset, int *temp_ptr)
{
	int rv;
	int temp_raw = 0;

	rv = g781_read8(offset, &temp_raw);
	if (rv < 0)
		return rv;

	*temp_ptr = (int)(int8_t)temp_raw;
	return EC_SUCCESS;
}

static int g781_set_temp(const int offset, int temp)
{
	if (temp < -127 || temp > 127)
		return EC_ERROR_INVAL;

	return g781_write8(offset, (uint8_t)temp);
}

int g781_get_val(int idx, int *temp_ptr)
{
	if (!board_g781_has_power())
		return EC_ERROR_NOT_POWERED;

	switch (idx) {
	case G781_IDX_INTERNAL:
		*temp_ptr = g781_temp_val_local;
		break;
	case G781_IDX_EXTERNAL:
		*temp_ptr = g781_temp_val_remote;
		break;
	default:
		return EC_ERROR_UNKNOWN;
	}

	return EC_SUCCESS;
}

static void g781_temp_sensor_poll(void)
{
	if (!board_g781_has_power())
		return;

	g781_get_temp(G781_TEMP_LOCAL, &g781_temp_val_local);
	g781_temp_val_local = C_TO_K(g781_temp_val_local);

	g781_get_temp(G781_TEMP_REMOTE, &g781_temp_val_remote);
	g781_temp_val_remote = C_TO_K(g781_temp_val_remote);
}
DECLARE_HOOK(HOOK_SECOND, g781_temp_sensor_poll, HOOK_PRIO_TEMP_SENSOR);

static int g781_show_status(void)
{
	int value;
	int rv;


	rv = g781_get_temp(G781_TEMP_LOCAL, &value);
	if (rv < 0)
		return rv;
	ccprintf("Local Temp:   %3dC\n", value);

	rv = g781_get_temp(G781_LOCAL_TEMP_THERM_LIMIT, &value);
	if (rv < 0)
		return rv;
	ccprintf("  Therm Trip: %3dC\n", value);

	rv = g781_get_temp(G781_LOCAL_TEMP_HIGH_LIMIT_R, &value);
	if (rv < 0)
		return rv;
	ccprintf("  High Alarm: %3dC\n", value);

	rv = g781_get_temp(G781_LOCAL_TEMP_LOW_LIMIT_R, &value);
	if (rv < 0)
		return rv;
	ccprintf("  Low Alarm:  %3dC\n", value);

	rv = g781_get_temp(G781_TEMP_REMOTE, &value);
	if (rv < 0)
		return rv;
	ccprintf("Remote Temp:  %3dC\n", value);

	rv = g781_get_temp(G781_REMOTE_TEMP_THERM_LIMIT, &value);
	if (rv < 0)
		return rv;
	ccprintf("  Therm Trip: %3dC\n", value);

	rv = g781_get_temp(G781_REMOTE_TEMP_HIGH_LIMIT_R, &value);
	if (rv < 0)
		return rv;
	ccprintf("  High Alarm: %3dC\n", value);

	rv = g781_get_temp(G781_REMOTE_TEMP_LOW_LIMIT_R, &value);
	if (rv < 0)
		return rv;
	ccprintf("  Low Alarm:  %3dC\n", value);

	rv = g781_read8(G781_STATUS, &value);
	if (rv < 0)
		return rv;
	ccprintf("\nSTATUS: %08b\n", value);

	rv = g781_read8(G781_CONFIGURATION_R, &value);
	if (rv < 0)
		return rv;
	ccprintf("CONFIG: %08b\n", value);

	return EC_SUCCESS;
}

static int command_g781(int argc, char **argv)
{
	char *command;
	char *e;
	int data;
	int offset;
	int rv;

	if (!board_g781_has_power()) {
		ccprintf("ERROR: Temp sensor not powered.\n");
		return EC_ERROR_NOT_POWERED;
	}

	/* If no args just print status */
	if (argc == 1)
		return g781_show_status();

	if (argc < 3)
		return EC_ERROR_PARAM_COUNT;

	command = argv[1];
	offset = strtoi(argv[2], &e, 0);
	if (*e || offset < 0 || offset > 255)
		return EC_ERROR_PARAM2;

	if (!strcasecmp(command, "getbyte")) {
		rv = g781_read8(offset, &data);
		if (rv < 0)
			return rv;
		ccprintf("Byte at offset 0x%02x is %08b\n", offset, data);
		return rv;
	}

	/* Remaining commands are of the form "g781 set-command offset data" */
	if (argc != 4)
		return EC_ERROR_PARAM_COUNT;

	data = strtoi(argv[3], &e, 0);
	if (*e)
		return EC_ERROR_PARAM3;

	if (!strcasecmp(command, "settemp")) {
		ccprintf("Setting 0x%02x to %dC\n", offset, data);
		rv = g781_set_temp(offset, data);
	} else if (!strcasecmp(command, "setbyte")) {
		ccprintf("Setting 0x%02x to 0x%02x\n", offset, data);
		rv = g781_write8(offset, data);
	} else
		return EC_ERROR_PARAM1;

	return rv;
}
DECLARE_CONSOLE_COMMAND(g781, command_g781,
	"[settemp|setbyte <offset> <value>] or [getbyte <offset>]. "
	"Temps in Celsius.",
	"Print g781 temp sensor status or set parameters.", NULL);