summaryrefslogtreecommitdiff
path: root/driver/temp_sensor/tmp112.c
blob: 0e35ef55ac15c81c3be622d364ba2703a62d194b (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
/* Copyright (c) 2016 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.
 */

/* TMP112 temperature sensor module for Chrome EC */

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

#define TMP112_RESOLUTION 12
#define TMP112_SHIFT1 (16 - TMP112_RESOLUTION)
#define TMP112_SHIFT2 (TMP112_RESOLUTION - 8)

static int temp_val_local;

static int raw_read16(const int offset, int *data_ptr)
{
	return i2c_read16__7bf(I2C_PORT_THERMAL, TMP112_I2C_ADDR__7bf,
			  offset, data_ptr);
}

static int raw_write16(const int offset, int data)
{
	return i2c_write16__7bf(I2C_PORT_THERMAL, TMP112_I2C_ADDR__7bf,
			   offset, data);
}

static int get_temp(int *temp_ptr)
{
	int rv;
	int temp_raw = 0;

	rv = raw_read16(TMP112_REG_TEMP, &temp_raw);
	if (rv < 0)
		return rv;

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

static inline int tmp112_reg_to_c(int16_t reg)
{
	int tmp;

	tmp = (((reg >> TMP112_SHIFT1) * 1000 ) >> TMP112_SHIFT2);

	return tmp / 1000;
}

int tmp112_get_val(int idx, int *temp_ptr)
{
	*temp_ptr = temp_val_local;
	return EC_SUCCESS;
}

static void tmp112_poll(void)
{
	int temp_c = 0;

	if (get_temp(&temp_c) == EC_SUCCESS)
		temp_val_local = C_TO_K(tmp112_reg_to_c(temp_c));
}
DECLARE_HOOK(HOOK_SECOND, tmp112_poll, HOOK_PRIO_TEMP_SENSOR);

static void tmp112_init(void)
{
	int tmp;
	int set_mask, clr_mask;

	/* 12 bit mode */
	set_mask = (3 << 5);

	/* not oneshot mode */
	clr_mask = (1 << 7);

	raw_read16(TMP112_REG_CONF, &tmp);
	raw_write16(TMP112_REG_CONF, (tmp & ~clr_mask) | set_mask);
}
DECLARE_HOOK(HOOK_INIT, tmp112_init, HOOK_PRIO_DEFAULT);