2016-04-05 14:37:45 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-04-05 14:37:45 +03:00
|
|
|
*/
|
|
|
|
|
2020-03-30 10:08:08 -05:00
|
|
|
#define DT_DRV_COMPAT ti_tmp007
|
|
|
|
|
2016-04-05 14:37:45 +03:00
|
|
|
#include <device.h>
|
2019-06-25 15:53:54 -04:00
|
|
|
#include <drivers/i2c.h>
|
2019-06-25 15:53:52 -04:00
|
|
|
#include <drivers/gpio.h>
|
2019-06-26 10:33:41 -04:00
|
|
|
#include <sys/byteorder.h>
|
2019-06-26 10:33:55 -04:00
|
|
|
#include <sys/util.h>
|
2016-11-10 08:21:34 -05:00
|
|
|
#include <kernel.h>
|
2019-06-25 15:54:00 -04:00
|
|
|
#include <drivers/sensor.h>
|
2019-06-26 10:33:39 -04:00
|
|
|
#include <sys/__assert.h>
|
2018-10-10 12:08:54 +05:30
|
|
|
#include <logging/log.h>
|
2016-04-05 14:37:45 +03:00
|
|
|
|
2016-10-15 08:20:02 -04:00
|
|
|
#include "tmp007.h"
|
2016-04-05 14:37:45 +03:00
|
|
|
|
2019-10-11 12:40:57 +02:00
|
|
|
LOG_MODULE_REGISTER(TMP007, CONFIG_SENSOR_LOG_LEVEL);
|
2018-10-10 12:08:54 +05:30
|
|
|
|
2020-01-08 18:43:57 -05:00
|
|
|
int tmp007_reg_read(struct tmp007_data *drv_data,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t reg, uint16_t *val)
|
2016-04-05 14:37:45 +03:00
|
|
|
{
|
2020-01-08 18:43:57 -05:00
|
|
|
if (i2c_burst_read(drv_data->i2c, TMP007_I2C_ADDRESS,
|
2020-05-27 11:26:57 -05:00
|
|
|
reg, (uint8_t *) val, 2) < 0) {
|
2020-01-08 18:43:57 -05:00
|
|
|
LOG_ERR("I2C read failed");
|
2016-04-05 14:37:45 +03:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
*val = sys_be16_to_cpu(*val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
int tmp007_reg_write(struct tmp007_data *drv_data, uint8_t reg, uint16_t val)
|
2016-04-05 14:37:45 +03:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t tx_buf[3] = {reg, val >> 8, val & 0xFF};
|
2016-04-05 14:37:45 +03:00
|
|
|
|
|
|
|
return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf),
|
|
|
|
TMP007_I2C_ADDRESS);
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
int tmp007_reg_update(struct tmp007_data *drv_data, uint8_t reg,
|
|
|
|
uint16_t mask, uint16_t val)
|
2016-04-05 14:37:45 +03:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint16_t old_val = 0U;
|
|
|
|
uint16_t new_val;
|
2016-04-05 14:37:45 +03:00
|
|
|
|
2016-05-17 13:43:50 +03:00
|
|
|
if (tmp007_reg_read(drv_data, reg, &old_val) < 0) {
|
2016-04-05 14:37:45 +03:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_val = old_val & ~mask;
|
|
|
|
new_val |= val & mask;
|
|
|
|
|
|
|
|
return tmp007_reg_write(drv_data, reg, new_val);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int tmp007_sample_fetch(const struct device *dev,
|
|
|
|
enum sensor_channel chan)
|
2016-04-05 14:37:45 +03:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct tmp007_data *drv_data = dev->data;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint16_t val;
|
2016-04-05 14:37:45 +03:00
|
|
|
|
2018-03-23 09:41:09 +01:00
|
|
|
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
|
2016-04-11 14:49:26 +03:00
|
|
|
|
2016-05-05 16:53:52 +03:00
|
|
|
if (tmp007_reg_read(drv_data, TMP007_REG_TOBJ, &val) < 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val & TMP007_DATA_INVALID_BIT) {
|
2016-04-05 14:37:45 +03:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
drv_data->sample = arithmetic_shift_right((int16_t)val, 2);
|
2016-04-05 14:37:45 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int tmp007_channel_get(const struct device *dev,
|
2016-04-05 14:37:45 +03:00
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct tmp007_data *drv_data = dev->data;
|
2020-05-27 11:26:57 -05:00
|
|
|
int32_t uval;
|
2016-04-05 14:37:45 +03:00
|
|
|
|
2018-03-23 09:41:09 +01:00
|
|
|
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
|
2016-04-05 14:37:45 +03:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
uval = (int32_t)drv_data->sample * TMP007_TEMP_SCALE;
|
2016-04-05 14:37:45 +03:00
|
|
|
val->val1 = uval / 1000000;
|
|
|
|
val->val2 = uval % 1000000;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:36:17 +01:00
|
|
|
static const struct sensor_driver_api tmp007_driver_api = {
|
2016-04-05 14:37:45 +03:00
|
|
|
#ifdef CONFIG_TMP007_TRIGGER
|
|
|
|
.attr_set = tmp007_attr_set,
|
|
|
|
.trigger_set = tmp007_trigger_set,
|
|
|
|
#endif
|
|
|
|
.sample_fetch = tmp007_sample_fetch,
|
|
|
|
.channel_get = tmp007_channel_get,
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int tmp007_init(const struct device *dev)
|
2016-04-05 14:37:45 +03:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct tmp007_data *drv_data = dev->data;
|
2016-04-05 14:37:45 +03:00
|
|
|
|
2020-03-30 10:08:08 -05:00
|
|
|
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
|
2016-04-05 14:37:45 +03:00
|
|
|
if (drv_data->i2c == NULL) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to get pointer to %s device!",
|
2020-03-30 10:08:08 -05:00
|
|
|
DT_INST_BUS_LABEL(0));
|
2016-04-05 14:37:45 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_TMP007_TRIGGER
|
2016-05-17 13:43:50 +03:00
|
|
|
if (tmp007_init_interrupt(dev) < 0) {
|
2018-10-10 12:08:54 +05:30
|
|
|
LOG_DBG("Failed to initialize interrupt!");
|
2016-04-05 14:37:45 +03:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tmp007_data tmp007_driver;
|
|
|
|
|
2020-12-15 18:53:48 -06:00
|
|
|
DEVICE_DT_INST_DEFINE(0, tmp007_init, device_pm_control_nop,
|
2019-11-07 05:30:26 -06:00
|
|
|
&tmp007_driver,
|
2018-02-04 17:53:14 +05:30
|
|
|
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
|
|
|
|
&tmp007_driver_api);
|