2016-04-21 18:23:48 +08:00
|
|
|
/*
|
|
|
|
* 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 <device.h>
|
|
|
|
#include <gpio.h>
|
|
|
|
#include <misc/util.h>
|
2016-11-10 21:21:34 +08:00
|
|
|
#include <kernel.h>
|
2016-04-21 18:23:48 +08:00
|
|
|
#include <sensor.h>
|
|
|
|
|
2016-10-15 20:19:51 +08:00
|
|
|
#include "bmc150_magn.h"
|
2016-04-21 18:23:48 +08:00
|
|
|
|
|
|
|
extern struct bmc150_magn_data bmc150_magn_data;
|
|
|
|
|
|
|
|
int bmc150_magn_trigger_set(struct device *dev,
|
|
|
|
const struct sensor_trigger *trig,
|
|
|
|
sensor_trigger_handler_t handler)
|
|
|
|
{
|
|
|
|
struct bmc150_magn_data *data = dev->driver_data;
|
|
|
|
const struct bmc150_magn_config * const config =
|
|
|
|
dev->config->config_info;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
#if defined(CONFIG_BMC150_MAGN_TRIGGER_DRDY)
|
|
|
|
if (trig->type == SENSOR_TRIG_DATA_READY) {
|
|
|
|
gpio_pin_disable_callback(data->gpio_drdy,
|
|
|
|
config->gpio_drdy_int_pin);
|
|
|
|
|
|
|
|
state = 0;
|
|
|
|
if (handler) {
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->handler_drdy = handler;
|
|
|
|
data->trigger_drdy = *trig;
|
|
|
|
|
|
|
|
if (i2c_reg_update_byte(data->i2c_master,
|
|
|
|
config->i2c_slave_addr,
|
|
|
|
BMC150_MAGN_REG_INT_DRDY,
|
|
|
|
BMC150_MAGN_MASK_DRDY_EN,
|
|
|
|
state << BMC150_MAGN_SHIFT_DRDY_EN)
|
2016-05-17 18:43:50 +08:00
|
|
|
< 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("failed to set DRDY interrupt");
|
2016-04-21 18:23:48 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_pin_enable_callback(data->gpio_drdy,
|
|
|
|
config->gpio_drdy_int_pin);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bmc150_magn_gpio_drdy_callback(struct device *dev,
|
|
|
|
struct gpio_callback *cb,
|
|
|
|
uint32_t pins)
|
|
|
|
{
|
|
|
|
struct bmc150_magn_data *data =
|
|
|
|
CONTAINER_OF(cb, struct bmc150_magn_data, gpio_cb);
|
|
|
|
const struct bmc150_magn_config * const config =
|
|
|
|
data->dev->config->config_info;
|
|
|
|
|
|
|
|
ARG_UNUSED(pins);
|
|
|
|
|
|
|
|
gpio_pin_disable_callback(dev, config->gpio_drdy_int_pin);
|
|
|
|
|
2016-11-10 21:21:34 +08:00
|
|
|
k_sem_give(&data->sem);
|
2016-04-21 18:23:48 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
static void bmc150_magn_thread_main(void *arg1, void *arg2, void *arg3)
|
2016-04-21 18:23:48 +08:00
|
|
|
{
|
|
|
|
struct device *dev = (struct device *) arg1;
|
|
|
|
struct bmc150_magn_data *data = dev->driver_data;
|
2016-10-07 03:31:54 +08:00
|
|
|
const struct bmc150_magn_config *config = dev->config->config_info;
|
2016-04-21 18:23:48 +08:00
|
|
|
uint8_t reg_val;
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
int gpio_pin = config->gpio_drdy_int_pin;
|
|
|
|
|
2016-04-21 18:23:48 +08:00
|
|
|
while (1) {
|
2016-11-10 21:21:34 +08:00
|
|
|
k_sem_take(&data->sem, K_FOREVER);
|
2016-04-21 18:23:48 +08:00
|
|
|
|
|
|
|
while (i2c_reg_read_byte(data->i2c_master,
|
|
|
|
config->i2c_slave_addr,
|
|
|
|
BMC150_MAGN_REG_INT_STATUS,
|
2016-05-17 18:43:50 +08:00
|
|
|
®_val) < 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("failed to clear data ready interrupt");
|
2016-04-21 18:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data->handler_drdy) {
|
|
|
|
data->handler_drdy(dev, &data->trigger_drdy);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_pin_enable_callback(data->gpio_drdy, gpio_pin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bmc150_magn_set_drdy_polarity(struct device *dev, int state)
|
|
|
|
{
|
|
|
|
struct bmc150_magn_data *data = dev->driver_data;
|
2016-10-07 03:31:54 +08:00
|
|
|
const struct bmc150_magn_config *config = dev->config->config_info;
|
2016-04-21 18:23:48 +08:00
|
|
|
|
|
|
|
if (state) {
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
|
|
|
|
BMC150_MAGN_REG_INT_DRDY,
|
|
|
|
BMC150_MAGN_MASK_DRDY_DR_POLARITY,
|
|
|
|
state << BMC150_MAGN_SHIFT_DRDY_DR_POLARITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bmc150_magn_init_interrupt(struct device *dev)
|
|
|
|
{
|
|
|
|
const struct bmc150_magn_config * const config =
|
2016-11-10 23:05:53 +08:00
|
|
|
dev->config->config_info;
|
2016-04-21 18:23:48 +08:00
|
|
|
struct bmc150_magn_data *data = dev->driver_data;
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
|
2016-04-21 18:23:48 +08:00
|
|
|
#if defined(CONFIG_BMC150_MAGN_TRIGGER_DRDY)
|
2016-05-17 18:43:50 +08:00
|
|
|
if (bmc150_magn_set_drdy_polarity(dev, 0) < 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("failed to set DR polarity");
|
2016-04-21 18:23:48 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
|
|
|
|
BMC150_MAGN_REG_INT_DRDY,
|
|
|
|
BMC150_MAGN_MASK_DRDY_EN,
|
2016-05-17 18:43:50 +08:00
|
|
|
0 << BMC150_MAGN_SHIFT_DRDY_EN) < 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("failed to set data ready interrupt enabled bit");
|
2016-04-21 18:23:48 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
data->handler_drdy = NULL;
|
|
|
|
|
2016-11-10 21:21:34 +08:00
|
|
|
k_sem_init(&data->sem, 0, UINT_MAX);
|
2016-04-21 18:23:48 +08:00
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
k_thread_spawn(data->thread_stack,
|
|
|
|
CONFIG_BMC150_MAGN_TRIGGER_THREAD_STACK,
|
|
|
|
bmc150_magn_thread_main, dev, NULL, NULL,
|
|
|
|
K_PRIO_COOP(10), 0, 0);
|
2016-04-21 18:23:48 +08:00
|
|
|
|
|
|
|
data->gpio_drdy = device_get_binding(config->gpio_drdy_dev_name);
|
|
|
|
if (!data->gpio_drdy) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("gpio controller %s not found",
|
|
|
|
config->gpio_drdy_dev_name);
|
2016-04-21 18:23:48 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_pin_configure(data->gpio_drdy, config->gpio_drdy_int_pin,
|
|
|
|
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
|
|
|
|
GPIO_INT_ACTIVE_LOW | GPIO_INT_DEBOUNCE);
|
|
|
|
|
|
|
|
gpio_init_callback(&data->gpio_cb,
|
|
|
|
bmc150_magn_gpio_drdy_callback,
|
|
|
|
BIT(config->gpio_drdy_int_pin));
|
|
|
|
|
2016-05-17 18:43:50 +08:00
|
|
|
if (gpio_add_callback(data->gpio_drdy, &data->gpio_cb) < 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("failed to set gpio callback");
|
2016-04-21 18:23:48 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->dev = dev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|