sensor: Add driver for MCP9808 temperature sensor
The driver only supports simple temperature readings. It does not support the alarm feature. Datasheet available at <http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf>. Origin: Original Change-Id: I283e6697fc6947975323d280f5ff0ba625fac5a9 Signed-off-by: Vlad Dogaru <vlad.dogaru@intel.com>
This commit is contained in:
parent
48bdd70a07
commit
68be7b7d26
4 changed files with 194 additions and 0 deletions
|
@ -43,3 +43,38 @@ config SENSOR_DEBUG
|
||||||
depends on SENSOR && PRINTK
|
depends on SENSOR && PRINTK
|
||||||
help
|
help
|
||||||
This option enables debug output for sensor drivers.
|
This option enables debug output for sensor drivers.
|
||||||
|
|
||||||
|
menuconfig MCP9808
|
||||||
|
bool "MCP9808 temperature sensor"
|
||||||
|
depends on SENSOR && I2C
|
||||||
|
default n
|
||||||
|
help
|
||||||
|
Enable driver for MCP9808 temperature sensor.
|
||||||
|
|
||||||
|
config MCP9808_DEV_NAME
|
||||||
|
string "MCP9808 device name"
|
||||||
|
depends on MCP9808
|
||||||
|
default "MCP9808"
|
||||||
|
|
||||||
|
config MCP9808_INIT_PRIORITY
|
||||||
|
int
|
||||||
|
depends on MCP9808
|
||||||
|
default 70
|
||||||
|
prompt "Init priority"
|
||||||
|
help
|
||||||
|
Device driver initialization priority.
|
||||||
|
|
||||||
|
config MCP9808_I2C_ADDR
|
||||||
|
hex "MCP9808 I2C slave address"
|
||||||
|
depends on MCP9808
|
||||||
|
default 0x18
|
||||||
|
help
|
||||||
|
Specify the I2C slave address for the MCP9808.
|
||||||
|
|
||||||
|
config MCP9808_I2C_DEV_NAME
|
||||||
|
string "I2C master where MCP9808 is connected"
|
||||||
|
depends on MCP9808
|
||||||
|
default "I2C0"
|
||||||
|
help
|
||||||
|
Specify the device name of the I2C master device to which MCP9808 is
|
||||||
|
connected.
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
ccflags-y +=-I$(srctree)/drivers
|
ccflags-y +=-I$(srctree)/drivers
|
||||||
|
|
||||||
obj-$(CONFIG_SENSOR_DELAYED_WORK) += sensor.o
|
obj-$(CONFIG_SENSOR_DELAYED_WORK) += sensor.o
|
||||||
|
|
||||||
|
obj-$(CONFIG_MCP9808) += sensor_mcp9808.o
|
||||||
|
|
119
drivers/sensor/sensor_mcp9808.c
Normal file
119
drivers/sensor/sensor_mcp9808.c
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
/* sensor_mcp9808.c - Driver for MCP9808 temperature sensor */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Intel Corporation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <nanokernel.h>
|
||||||
|
#include <i2c.h>
|
||||||
|
#include <sensor.h>
|
||||||
|
#include <init.h>
|
||||||
|
#include <gpio.h>
|
||||||
|
#include <misc/byteorder.h>
|
||||||
|
#include "sensor_mcp9808.h"
|
||||||
|
|
||||||
|
#ifndef CONFIG_SENSOR_DEBUG
|
||||||
|
#define DBG(...) { ; }
|
||||||
|
#else
|
||||||
|
#include <misc/printk.h>
|
||||||
|
#define DBG printk
|
||||||
|
#endif /* CONFIG_SENSOR_DEBUG */
|
||||||
|
|
||||||
|
static int mcp9808_reg_read(struct mcp9808_data *data, uint8_t reg, uint16_t *val)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
struct i2c_msg msgs[2] = {
|
||||||
|
{
|
||||||
|
.buf = ®,
|
||||||
|
.len = 1,
|
||||||
|
.flags = I2C_MSG_WRITE | I2C_MSG_RESTART,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.buf = (uint8_t *)val,
|
||||||
|
.len = 2,
|
||||||
|
.flags = I2C_MSG_READ | I2C_MSG_STOP,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
ret = i2c_transfer(data->i2c_master, msgs, 2, data->i2c_slave_addr);
|
||||||
|
if (ret) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
*val = sys_be16_to_cpu(*val);
|
||||||
|
|
||||||
|
return DEV_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mcp9808_sample_fetch(struct device *dev)
|
||||||
|
{
|
||||||
|
struct mcp9808_data *data = dev->driver_data;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
#ifdef CONFIG_SENSOR_DEBUG
|
||||||
|
if (chan != SENSOR_CHAN_TEMP) {
|
||||||
|
return DEV_INVALID_OP;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
val->type = SENSOR_TYPE_INT_PLUS_MICRO;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return DEV_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct sensor_driver_api mcp9808_api_funcs = {
|
||||||
|
.sample_fetch = mcp9808_sample_fetch,
|
||||||
|
.channel_get = mcp9808_channel_get,
|
||||||
|
};
|
||||||
|
|
||||||
|
int mcp9808_init(struct device *dev)
|
||||||
|
{
|
||||||
|
struct mcp9808_data *data = dev->driver_data;
|
||||||
|
|
||||||
|
dev->driver_api = &mcp9808_api_funcs;
|
||||||
|
|
||||||
|
data->i2c_master = device_get_binding(CONFIG_MCP9808_I2C_DEV_NAME);
|
||||||
|
if (!data->i2c_master) {
|
||||||
|
DBG("mcp9808: i2c master not found: %s\n",
|
||||||
|
CONFIG_MCP9808_I2C_DEV_NAME);
|
||||||
|
return DEV_INVALID_CONF;
|
||||||
|
}
|
||||||
|
|
||||||
|
data->i2c_slave_addr = CONFIG_MCP9808_I2C_ADDR;
|
||||||
|
|
||||||
|
return DEV_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct mcp9808_data mcp9808_data;
|
||||||
|
|
||||||
|
DEVICE_INIT(mcp9808, CONFIG_MCP9808_DEV_NAME, mcp9808_init, &mcp9808_data,
|
||||||
|
NULL, SECONDARY, CONFIG_MCP9808_INIT_PRIORITY);
|
38
drivers/sensor/sensor_mcp9808.h
Normal file
38
drivers/sensor/sensor_mcp9808.h
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Intel Corporation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __SENSOR_MCP9808_H__
|
||||||
|
#define __SENSOR_MCP9808_H__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <device.h>
|
||||||
|
#include <misc/util.h>
|
||||||
|
|
||||||
|
#define MCP9808_REG_TEMP_AMB 0x05
|
||||||
|
|
||||||
|
#define MCP9808_SIGN_BIT BIT(12)
|
||||||
|
#define MCP9808_TEMP_INT_MASK 0x0ff0
|
||||||
|
#define MCP9808_TEMP_INT_SHIFT 4
|
||||||
|
#define MCP9808_TEMP_FRAC_MASK 0x000f
|
||||||
|
|
||||||
|
struct mcp9808_data {
|
||||||
|
struct device *i2c_master;
|
||||||
|
uint16_t i2c_slave_addr;
|
||||||
|
|
||||||
|
uint16_t reg_val;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* __SENSOR_MCP9808_H__ */
|
Loading…
Add table
Add a link
Reference in a new issue