zephyr/drivers/sensor/apds9960/apds9960_trigger.c
Tomasz Bursztyka e18fcbba5a device: Const-ify all device driver instance pointers
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.

A coccinelle rule is used for this:

@r_const_dev_1
  disable optional_qualifier
@
@@
-struct device *
+const struct device *

@r_const_dev_2
 disable optional_qualifier
@
@@
-struct device * const
+const struct device *

Fixes #27399

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-09-02 13:48:13 +02:00

104 lines
2.2 KiB
C

/*
* Copyright (c) 2018 Phytec Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/i2c.h>
#include <sys/util.h>
#include <kernel.h>
#include <drivers/sensor.h>
#include "apds9960.h"
extern struct apds9960_data apds9960_driver;
#include <logging/log.h>
LOG_MODULE_DECLARE(APDS9960, CONFIG_SENSOR_LOG_LEVEL);
void apds9960_work_cb(struct k_work *work)
{
struct apds9960_data *data = CONTAINER_OF(work,
struct apds9960_data,
work);
const struct device *dev = data->dev;
if (data->p_th_handler != NULL) {
data->p_th_handler(dev, &data->p_th_trigger);
}
apds9960_setup_int(data, true);
}
int apds9960_attr_set(const struct device *dev,
enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
const struct apds9960_config *config = dev->config;
struct apds9960_data *data = dev->data;
if (chan == SENSOR_CHAN_PROX) {
if (attr == SENSOR_ATTR_UPPER_THRESH) {
if (i2c_reg_write_byte(data->i2c,
config->i2c_address,
APDS9960_PIHT_REG,
(uint8_t)val->val1)) {
return -EIO;
}
return 0;
}
if (attr == SENSOR_ATTR_LOWER_THRESH) {
if (i2c_reg_write_byte(data->i2c,
config->i2c_address,
APDS9960_PILT_REG,
(uint8_t)val->val1)) {
return -EIO;
}
return 0;
}
}
return -ENOTSUP;
}
int apds9960_trigger_set(const struct device *dev,
const struct sensor_trigger *trig,
sensor_trigger_handler_t handler)
{
const struct apds9960_config *config = dev->config;
struct apds9960_data *data = dev->data;
apds9960_setup_int(data, false);
switch (trig->type) {
case SENSOR_TRIG_THRESHOLD:
if (trig->chan == SENSOR_CHAN_PROX) {
data->p_th_handler = handler;
if (i2c_reg_update_byte(data->i2c,
config->i2c_address,
APDS9960_ENABLE_REG,
APDS9960_ENABLE_PIEN,
APDS9960_ENABLE_PIEN)) {
return -EIO;
}
} else {
return -ENOTSUP;
}
break;
default:
LOG_ERR("Unsupported sensor trigger");
return -ENOTSUP;
}
apds9960_setup_int(data, true);
if (gpio_pin_get(data->gpio, data->gpio_pin) > 0) {
k_work_submit(&data->work);
}
return 0;
}