drivers: sensor: Add adt7310 temperature sensor

Adds adt7310 temperature controlled via spi.

Signed-off-by: Andriy Gelman <andriy.gelman@gmail.com>
This commit is contained in:
Andriy Gelman 2022-05-13 22:50:08 -04:00 committed by Maureen Helm
parent 8fba8aa535
commit b8244fdabd
8 changed files with 570 additions and 0 deletions

View file

@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
add_subdirectory_ifdef(CONFIG_ADT7310 adt7310)
add_subdirectory_ifdef(CONFIG_ADT7420 adt7420)
add_subdirectory_ifdef(CONFIG_ADXL345 adxl345)
add_subdirectory_ifdef(CONFIG_ADXL362 adxl362)

View file

@ -41,6 +41,8 @@ config SENSOR_INFO
comment "Device Drivers"
source "drivers/sensor/adt7310/Kconfig"
source "drivers/sensor/adt7420/Kconfig"
source "drivers/sensor/adxl345/Kconfig"

View file

@ -0,0 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_ADT7310 adt7310.c)
zephyr_library_sources_ifdef(CONFIG_ADT7310_TRIGGER adt7310_trigger.c)

View file

@ -0,0 +1,62 @@
# ADT7310 temperature sensor configuration options
# Copyright (c) 2023 Andriy Gelman
# SPDX-License-Identifier: Apache-2.0
menuconfig ADT7310
bool "ADT7310 Temperature Sensor"
default y
depends on DT_HAS_ADI_ADT7310_ENABLED
select SPI
help
Enable the driver for Analog Devices ADT7310 High-Accuracy
16-bit Digital SPI Temperature Sensors.
if ADT7310
config ADT7310_TRIGGER
bool
depends on GPIO
choice
prompt "Sets trigger mode"
default ADT7310_TRIGGER_NONE
help
Sets thread type for the interrupt handler.
config ADT7310_TRIGGER_NONE
bool "No trigger"
config ADT7310_TRIGGER_GLOBAL_THREAD
bool "Use global thread"
select ADT7310_TRIGGER
help
Use a global thread for the interrupt handler.
config ADT7310_TRIGGER_OWN_THREAD
bool "Use own thread"
select ADT7310_TRIGGER
help
Use a separate thread for the interrupt handler.
endchoice
if ADT7310_TRIGGER_OWN_THREAD
config ADT7310_THREAD_PRIORITY
int "Thread priority of the interrupt handler"
default 1
help
Thread priority of the interrupt handler. A higher number implies a
higher priority. The thread is cooperative and will not be interrupted by
another thread until execution is released.
config ADT7310_THREAD_STACK_SIZE
int "Stack size of the interrupt handler thread"
default 1024
help
Stack size of the interrupt handler thread.
endif # ADT7310_TRIGGER_OWN_THREAD
endif # ADT7310

View file

@ -0,0 +1,303 @@
/*
* Copyright (c) 2023 Andriy Gelman <andriy.gelman@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT adi_adt7310
#include <string.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include "adt7310.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ADT7310, CONFIG_SENSOR_LOG_LEVEL);
#define ADT7310_READ_CMD BIT(6)
#define ADT7310_WRITE_CMD 0
#define ADT7310_REG_STATUS 0x00
#define ADT7310_REG_CONFIG 0x01
#define ADT7310_REG_TEMP 0x02
#define ADT7310_REG_ID 0x03
#define ADT7310_REG_HYST 0x05
#define ADT7310_REG_THRESH_HIGH 0x06
#define ADT7310_REG_THRESH_LOW 0x07
#define ADT7310_ID 0xc0
#define ADT7310_CONFIG_OP_MODE_MASK (0x3 << 5)
#define ADT7310_CONFIG_OP_MODE_CONTINUOUS (0x0 << 5)
#define ADT7310_CONFIG_OP_MODE_1SPS (0x2 << 5)
#define ADT7310_HYSTERESIS_TEMP_MAX 15
#define ADT7310_CONFIG_RESOLUTION_16BIT BIT(7)
#define ADT7310_CONFIG_INT_COMPARATOR_MODE BIT(4)
/* Continuous conversion time = 240ms -> 1/0.240*1000000 */
#define ADT7310_MAX_SAMPLE_RATE 4166666
/* The quantization step size at 16-bit resolution is 0.0078125. */
/* Ref ADT7310 Reference manual */
#define ADT7310_SAMPLE_TO_MICRO_DEG(x) ((x) * 15625 >> 1)
#define ADT7310_MICRO_DEG_TO_SAMPLE(x) ((x) / 15625 << 1)
static int adt7310_temp_reg_read(const struct device *dev, uint8_t reg, int16_t *val)
{
const struct adt7310_dev_config *cfg = dev->config;
uint8_t cmd_buf[3] = { ADT7310_READ_CMD | (reg << 3) };
int ret;
const struct spi_buf tx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf rx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 };
ret = spi_transceive_dt(&cfg->bus, &tx, &rx);
if (ret < 0) {
return ret;
}
memcpy(val, cmd_buf + 1, 2);
*val = sys_be16_to_cpu(*val);
return 0;
}
static int adt7310_temp_reg_write(const struct device *dev, uint8_t reg, int16_t val)
{
const struct adt7310_dev_config *cfg = dev->config;
uint8_t cmd_buf[3];
const struct spi_buf tx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf rx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 };
cmd_buf[0] = ADT7310_WRITE_CMD | (reg << 3);
val = sys_cpu_to_be16(val);
memcpy(&cmd_buf[1], &val, sizeof(val));
return spi_transceive_dt(&cfg->bus, &tx, &rx);
}
static int adt7310_reg_read(const struct device *dev, uint8_t reg, uint8_t *val)
{
const struct adt7310_dev_config *cfg = dev->config;
uint8_t cmd_buf[2] = { ADT7310_READ_CMD | (reg << 3) };
const struct spi_buf tx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf rx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 };
int ret;
ret = spi_transceive_dt(&cfg->bus, &tx, &rx);
if (ret < 0) {
return ret;
}
*val = cmd_buf[1];
return 0;
}
static int adt7310_reg_write(const struct device *dev, uint8_t reg, uint8_t val)
{
const struct adt7310_dev_config *cfg = dev->config;
uint8_t cmd_buf[2] = { ADT7310_WRITE_CMD | (reg << 3), val };
const struct spi_buf tx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf rx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1 };
const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1 };
return spi_transceive_dt(&cfg->bus, &tx, &rx);
}
static int adt7310_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
struct adt7310_data *drv_data = dev->data;
int16_t value;
int ret;
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_AMBIENT_TEMP) {
return -ENOTSUP;
}
ret = adt7310_temp_reg_read(dev, ADT7310_REG_TEMP, &value);
if (ret < 0) {
return ret;
}
drv_data->sample = value;
return 0;
}
static int adt7310_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
int32_t value;
struct adt7310_data *drv_data = dev->data;
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
return -ENOTSUP;
}
value = ADT7310_SAMPLE_TO_MICRO_DEG((int32_t)drv_data->sample);
val->val1 = value / 1000000;
val->val2 = value % 1000000;
return 0;
}
static int adt7310_update_reg(const struct device *dev, uint8_t reg, uint8_t value, uint8_t mask)
{
int ret;
uint8_t reg_value;
ret = adt7310_reg_read(dev, reg, &reg_value);
if (ret < 0) {
return ret;
}
reg_value &= ~mask;
reg_value |= value;
return adt7310_reg_write(dev, reg, reg_value);
}
static int adt7310_attr_set(const struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, const struct sensor_value *val)
{
int32_t rate, value;
uint8_t reg = 0;
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
return -ENOTSUP;
}
if (val->val1 > INT32_MAX/1000000 - 1 || val->val1 < INT32_MIN/1000000 + 1) {
return -EINVAL;
}
switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
rate = val->val1 * 1000000 + val->val2;
if (rate > ADT7310_MAX_SAMPLE_RATE || rate < 0) {
return -EINVAL;
}
if (rate > 1000000) {
return adt7310_update_reg(dev, ADT7310_REG_CONFIG,
ADT7310_CONFIG_OP_MODE_CONTINUOUS,
ADT7310_CONFIG_OP_MODE_MASK);
} else {
return adt7310_update_reg(dev, ADT7310_REG_CONFIG,
ADT7310_CONFIG_OP_MODE_1SPS,
ADT7310_CONFIG_OP_MODE_MASK);
}
case SENSOR_ATTR_HYSTERESIS:
if (val->val1 < 0 || val->val1 > ADT7310_HYSTERESIS_TEMP_MAX || val->val2 != 0) {
return -EINVAL;
}
return adt7310_reg_write(dev, ADT7310_REG_HYST, val->val1);
case SENSOR_ATTR_UPPER_THRESH:
reg = ADT7310_REG_THRESH_HIGH;
__fallthrough;
case SENSOR_ATTR_LOWER_THRESH:
if (!reg) {
reg = ADT7310_REG_THRESH_LOW;
}
value = val->val1 * 1000000 + val->val2;
value = ADT7310_MICRO_DEG_TO_SAMPLE(value);
if (value < INT16_MIN || value > INT16_MAX) {
return -EINVAL;
}
return adt7310_temp_reg_write(dev, reg, value);
default:
return -ENOTSUP;
}
return 0;
}
static int adt7310_probe(const struct device *dev)
{
uint8_t value;
int ret;
ret = adt7310_reg_read(dev, ADT7310_REG_ID, &value);
if (ret) {
return ret;
}
value &= 0xf8;
if (value != ADT7310_ID) {
LOG_ERR("Invalid device ID");
return -ENODEV;
}
return adt7310_reg_write(dev, ADT7310_REG_CONFIG,
ADT7310_CONFIG_RESOLUTION_16BIT |
ADT7310_CONFIG_INT_COMPARATOR_MODE);
}
static int adt7310_init(const struct device *dev)
{
const struct adt7310_dev_config *cfg = dev->config;
int ret;
if (!spi_is_ready_dt(&cfg->bus)) {
LOG_ERR("SPI bus %s not ready", cfg->bus.bus->name);
return -ENODEV;
}
ret = adt7310_probe(dev);
if (ret) {
return ret;
}
#if defined(CONFIG_ADT7310_TRIGGER)
if (cfg->int_gpio.port) {
ret = adt7310_init_interrupt(dev);
if (ret) {
LOG_ERR("Failed to initialize interrupt");
return ret;
}
}
#endif
return ret;
}
static const struct sensor_driver_api adt7310_driver_api = {
.attr_set = adt7310_attr_set,
.sample_fetch = adt7310_sample_fetch,
.channel_get = adt7310_channel_get,
#if defined(CONFIG_ADT7310_TRIGGER)
.trigger_set = adt7310_trigger_set,
#endif
};
#define ADT7310_DEFINE(inst) \
static struct adt7310_data adt7310_data_##inst; \
\
static const struct adt7310_dev_config adt7310_config_##inst = { \
.bus = SPI_DT_SPEC_INST_GET( \
inst, \
(SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA), 0), \
\
IF_ENABLED(CONFIG_ADT7310_TRIGGER, \
(.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0}),))}; \
\
SENSOR_DEVICE_DT_INST_DEFINE(inst, adt7310_init, NULL, &adt7310_data_##inst, \
&adt7310_config_##inst, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, &adt7310_driver_api);
DT_INST_FOREACH_STATUS_OKAY(ADT7310_DEFINE)

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 Andriy Gelman
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_SENSOR_ADT7310_H_
#define ZEPHYR_DRIVERS_SENSOR_ADT7310_H_
#include <zephyr/types.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor.h>
int adt7310_init_interrupt(const struct device *dev);
int adt7310_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
sensor_trigger_handler_t handler);
struct adt7310_data {
int16_t sample;
#ifdef CONFIG_ADT7310_TRIGGER
struct gpio_callback gpio_cb;
sensor_trigger_handler_t th_handler;
const struct sensor_trigger *th_trigger;
const struct device *dev;
#ifdef CONFIG_ADT7310_TRIGGER_OWN_THREAD
K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ADT7310_THREAD_STACK_SIZE);
struct k_sem gpio_sem;
struct k_thread thread;
#elif CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD
struct k_work work;
#endif
#endif /* CONFIG_ADT7310_TRIGGER */
};
struct adt7310_dev_config {
struct spi_dt_spec bus;
#ifdef CONFIG_ADT7310_TRIGGER
struct gpio_dt_spec int_gpio;
#endif
};
#endif /* ZEPHYR_DRIVERS_SENSOR_ADT7310_H_ */

View file

@ -0,0 +1,136 @@
/*
* Copyright (c) 2023 Andriy Gelman <andriy.gelman@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>
#include <zephyr/types.h>
#include "adt7310.h"
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(ADT7310, CONFIG_SENSOR_LOG_LEVEL);
static void adt7310_gpio_callback(const struct device *dev, struct gpio_callback *cb,
uint32_t pins)
{
struct adt7310_data *drv_data = CONTAINER_OF(cb, struct adt7310_data, gpio_cb);
#if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
k_sem_give(&drv_data->gpio_sem);
#elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
k_work_submit(&drv_data->work);
#endif
}
#if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
static void adt7310_thread(struct adt7310_data *drv_data)
{
while (true) {
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
if (drv_data->th_handler != NULL) {
drv_data->th_handler(drv_data->dev, drv_data->th_trigger);
}
}
}
#elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
static void adt7310_work_cb(struct k_work *work)
{
struct adt7310_data *drv_data = CONTAINER_OF(work, struct adt7310_data, work);
if (drv_data->th_handler != NULL) {
drv_data->th_handler(drv_data->dev, drv_data->th_trigger);
}
}
#endif
int adt7310_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
sensor_trigger_handler_t handler)
{
struct adt7310_data *drv_data = dev->data;
const struct adt7310_dev_config *cfg = dev->config;
if (!cfg->int_gpio.port) {
return -ENOTSUP;
}
if (trig->type != SENSOR_TRIG_THRESHOLD) {
LOG_ERR("Unsupported sensor trigger");
return -ENOTSUP;
}
/* disable the interrupt but ignore error if it's first */
/* time this function is called */
gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
drv_data->th_handler = handler;
if (handler != NULL) {
int value, ret;
gpio_flags_t flags = GPIO_INT_EDGE_TO_ACTIVE;
drv_data->th_trigger = trig;
ret = gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
if (ret) {
drv_data->th_handler = NULL;
return ret;
}
value = gpio_pin_get_dt(&cfg->int_gpio);
if (value > 0) {
#if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
k_sem_give(&drv_data->gpio_sem);
#elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
k_work_submit(&drv_data->work);
#endif
}
}
return 0;
}
int adt7310_init_interrupt(const struct device *dev)
{
struct adt7310_data *drv_data = dev->data;
const struct adt7310_dev_config *cfg = dev->config;
int ret;
if (!device_is_ready(cfg->int_gpio.port)) {
LOG_ERR("%s: device %s is not ready", dev->name, cfg->int_gpio.port->name);
return -ENODEV;
}
gpio_init_callback(&drv_data->gpio_cb, adt7310_gpio_callback, BIT(cfg->int_gpio.pin));
ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT | GPIO_ACTIVE_LOW);
if (ret < 0) {
return ret;
}
ret = gpio_add_callback(cfg->int_gpio.port, &drv_data->gpio_cb);
if (ret < 0) {
return ret;
}
drv_data->dev = dev;
#if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
k_sem_init(&drv_data->gpio_sem, 0, 1);
k_thread_create(&drv_data->thread, drv_data->thread_stack,
CONFIG_ADT7310_THREAD_STACK_SIZE,
(k_thread_entry_t)adt7310_thread, drv_data,
NULL, NULL, K_PRIO_COOP(CONFIG_ADT7310_THREAD_PRIORITY),
0, K_NO_WAIT);
#elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
drv_data->work.handler = adt7310_work_cb;
#endif
return 0;
}

View file

@ -0,0 +1,12 @@
description: ADT7310 16-Bit digital SPI temperature sensor
compatible: "adi,adt7310"
include: [sensor-device.yaml, spi-device.yaml]
properties:
int-gpios:
type: phandle-array
description: |
The INT signal defaults to active low open drain, so requires a
pull-up on the board or in the flags cell of this entry.