2016-02-23 23:21:38 +08:00
|
|
|
/* sensor_mcp9808.c - Driver for MCP9808 temperature sensor */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-02-23 23:21:38 +08:00
|
|
|
*/
|
|
|
|
|
2016-03-10 01:54:42 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-11-10 21:21:34 +08:00
|
|
|
#include <kernel.h>
|
2016-02-23 23:21:38 +08:00
|
|
|
#include <i2c.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <misc/byteorder.h>
|
2016-04-11 19:49:26 +08:00
|
|
|
#include <misc/__assert.h>
|
|
|
|
|
2016-10-15 20:20:00 +08:00
|
|
|
#include "mcp9808.h"
|
2016-02-23 23:21:38 +08:00
|
|
|
|
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
int mcp9808_reg_read(struct mcp9808_data *data, u8_t reg, u16_t *val)
|
2016-02-23 23:21:38 +08:00
|
|
|
{
|
|
|
|
struct i2c_msg msgs[2] = {
|
|
|
|
{
|
|
|
|
.buf = ®,
|
|
|
|
.len = 1,
|
|
|
|
.flags = I2C_MSG_WRITE | I2C_MSG_RESTART,
|
|
|
|
},
|
|
|
|
{
|
2017-04-21 23:03:20 +08:00
|
|
|
.buf = (u8_t *)val,
|
2016-02-23 23:21:38 +08:00
|
|
|
.len = 2,
|
|
|
|
.flags = I2C_MSG_READ | I2C_MSG_STOP,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-05-05 21:53:52 +08:00
|
|
|
if (i2c_transfer(data->i2c_master, msgs, 2, data->i2c_slave_addr)
|
|
|
|
< 0) {
|
|
|
|
return -EIO;
|
2016-02-23 23:21:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*val = sys_be16_to_cpu(*val);
|
|
|
|
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2016-02-23 23:21:38 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 19:49:26 +08:00
|
|
|
static int mcp9808_sample_fetch(struct device *dev, enum sensor_channel chan)
|
2016-02-23 23:21:38 +08:00
|
|
|
{
|
|
|
|
struct mcp9808_data *data = dev->driver_data;
|
|
|
|
|
2016-07-28 07:56:08 +08:00
|
|
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_TEMP);
|
2016-04-11 19:49:26 +08:00
|
|
|
|
2016-02-23 23:21:38 +08:00
|
|
|
return mcp9808_reg_read(data, MCP9808_REG_TEMP_AMB, &data->reg_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcp9808_channel_get(struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
|
|
|
struct mcp9808_data *data = dev->driver_data;
|
|
|
|
|
2016-07-28 07:56:08 +08:00
|
|
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP);
|
2016-02-23 23:21:38 +08:00
|
|
|
|
|
|
|
val->val1 = (data->reg_val & MCP9808_TEMP_INT_MASK) >>
|
|
|
|
MCP9808_TEMP_INT_SHIFT;
|
|
|
|
val->val2 = (data->reg_val & MCP9808_TEMP_FRAC_MASK) * 62500;
|
|
|
|
|
|
|
|
if (data->reg_val & MCP9808_SIGN_BIT) {
|
|
|
|
val->val1 -= 256;
|
|
|
|
}
|
|
|
|
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2016-02-23 23:21:38 +08:00
|
|
|
}
|
|
|
|
|
2016-10-24 15:34:53 +08:00
|
|
|
static const struct sensor_driver_api mcp9808_api_funcs = {
|
2016-02-23 23:21:38 +08:00
|
|
|
.sample_fetch = mcp9808_sample_fetch,
|
|
|
|
.channel_get = mcp9808_channel_get,
|
2016-02-23 23:10:20 +08:00
|
|
|
.attr_set = mcp9808_attr_set,
|
|
|
|
.trigger_set = mcp9808_trigger_set,
|
2016-02-23 23:21:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int mcp9808_init(struct device *dev)
|
|
|
|
{
|
|
|
|
struct mcp9808_data *data = dev->driver_data;
|
|
|
|
|
|
|
|
data->i2c_master = device_get_binding(CONFIG_MCP9808_I2C_DEV_NAME);
|
|
|
|
if (!data->i2c_master) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("mcp9808: i2c master not found: %s",
|
2016-02-23 23:21:38 +08:00
|
|
|
CONFIG_MCP9808_I2C_DEV_NAME);
|
2016-03-10 02:22:04 +08:00
|
|
|
return -EINVAL;
|
2016-02-23 23:21:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data->i2c_slave_addr = CONFIG_MCP9808_I2C_ADDR;
|
|
|
|
|
2016-02-23 23:10:20 +08:00
|
|
|
mcp9808_setup_interrupt(dev);
|
|
|
|
|
2016-04-12 21:02:58 +08:00
|
|
|
dev->driver_api = &mcp9808_api_funcs;
|
|
|
|
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2016-02-23 23:21:38 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 20:52:53 +08:00
|
|
|
struct mcp9808_data mcp9808_data;
|
|
|
|
|
2016-02-23 23:21:38 +08:00
|
|
|
DEVICE_INIT(mcp9808, CONFIG_MCP9808_DEV_NAME, mcp9808_init, &mcp9808_data,
|
2016-11-09 03:06:55 +08:00
|
|
|
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY);
|