2016-01-26 00:23:04 +08:00
|
|
|
/* sensor_sx9500.c - Driver for Semtech SX9500 SAR proximity chip */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-03-10 01:54:42 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-01-26 00:23:04 +08:00
|
|
|
#include <nanokernel.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
#include <sensor.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <gpio.h>
|
2016-04-11 19:49:26 +08:00
|
|
|
#include <misc/__assert.h>
|
|
|
|
|
2016-01-26 00:23:04 +08:00
|
|
|
#include "sensor_sx9500.h"
|
|
|
|
|
|
|
|
static uint8_t sx9500_reg_defaults[] = {
|
|
|
|
/*
|
|
|
|
* First number is register address to write to. The chip
|
|
|
|
* auto-increments the address for subsequent values in a single
|
|
|
|
* write message.
|
|
|
|
*/
|
|
|
|
SX9500_REG_PROX_CTRL1,
|
|
|
|
|
|
|
|
0x43, /* Shield enabled, small range. */
|
|
|
|
0x77, /* x8 gain, 167kHz frequency, finest resolution. */
|
|
|
|
0x40, /* Doze enabled, 2x scan period doze, no raw filter. */
|
|
|
|
0x30, /* Average threshold. */
|
|
|
|
0x0f, /* Debouncer off, lowest average negative filter,
|
|
|
|
* highest average postive filter.
|
|
|
|
*/
|
|
|
|
0x0e, /* Proximity detection threshold: 280 */
|
|
|
|
0x00, /* No automatic compensation, compensate each pin
|
|
|
|
* independently, proximity hysteresis: 32, close
|
|
|
|
* debouncer off, far debouncer off.
|
|
|
|
*/
|
|
|
|
0x00, /* No stuck timeout, no periodic compensation. */
|
|
|
|
};
|
|
|
|
|
2016-04-11 19:49:26 +08:00
|
|
|
static int sx9500_sample_fetch(struct device *dev, enum sensor_channel chan)
|
2016-01-26 00:23:04 +08:00
|
|
|
{
|
|
|
|
struct sx9500_data *data = (struct sx9500_data *) dev->driver_data;
|
|
|
|
|
2016-04-11 19:49:26 +08:00
|
|
|
__ASSERT(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_PROX);
|
|
|
|
|
2016-04-25 23:09:42 +08:00
|
|
|
return i2c_reg_read_byte(data->i2c_master, data->i2c_slave_addr,
|
|
|
|
SX9500_REG_STAT, &data->prox_stat);
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sx9500_channel_get(struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
|
|
|
struct sx9500_data *data = (struct sx9500_data *) dev->driver_data;
|
|
|
|
|
2016-05-18 17:04:00 +08:00
|
|
|
__ASSERT(chan == SENSOR_CHAN_PROX);
|
2016-01-26 00:23:04 +08:00
|
|
|
|
2016-04-14 20:22:48 +08:00
|
|
|
val->type = SENSOR_VALUE_TYPE_INT;
|
2016-01-26 00:23:04 +08:00
|
|
|
val->val1 = !!(data->prox_stat &
|
|
|
|
(1 << (4 + CONFIG_SX9500_PROX_CHANNEL)));
|
|
|
|
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sensor_driver_api sx9500_api_funcs = {
|
|
|
|
.sample_fetch = sx9500_sample_fetch,
|
|
|
|
.channel_get = sx9500_channel_get,
|
|
|
|
#ifdef CONFIG_SX9500_TRIGGER
|
|
|
|
.trigger_set = sx9500_trigger_set,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sx9500_init_chip(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sx9500_data *data = (struct sx9500_data *) dev->driver_data;
|
|
|
|
uint8_t val;
|
|
|
|
|
2016-05-05 21:53:52 +08:00
|
|
|
if (i2c_write(data->i2c_master, sx9500_reg_defaults,
|
|
|
|
sizeof(sx9500_reg_defaults), data->i2c_slave_addr)
|
|
|
|
< 0) {
|
|
|
|
return -EIO;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No interrupts active. We only activate them when an
|
|
|
|
* application registers a trigger.
|
|
|
|
*/
|
2016-05-05 21:53:52 +08:00
|
|
|
if (i2c_reg_write_byte(data->i2c_master, data->i2c_slave_addr,
|
|
|
|
SX9500_REG_IRQ_MSK, 0) < 0) {
|
|
|
|
return -EIO;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read irq source reg to clear reset status. */
|
2016-05-05 21:53:52 +08:00
|
|
|
if (i2c_reg_read_byte(data->i2c_master, data->i2c_slave_addr,
|
|
|
|
SX9500_REG_IRQ_SRC, &val) < 0) {
|
|
|
|
return -EIO;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-25 23:09:42 +08:00
|
|
|
return i2c_reg_write_byte(data->i2c_master, data->i2c_slave_addr,
|
|
|
|
SX9500_REG_PROX_CTRL0,
|
|
|
|
1 << CONFIG_SX9500_PROX_CHANNEL);
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int sx9500_init(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sx9500_data *data = dev->driver_data;
|
|
|
|
|
|
|
|
data->i2c_master = device_get_binding(CONFIG_SX9500_I2C_DEV_NAME);
|
|
|
|
if (!data->i2c_master) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("sx9500: i2c master not found: %s",
|
2016-01-26 00:23:04 +08:00
|
|
|
CONFIG_SX9500_I2C_DEV_NAME);
|
2016-03-10 02:22:04 +08:00
|
|
|
return -EINVAL;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data->i2c_slave_addr = CONFIG_SX9500_I2C_ADDR;
|
|
|
|
|
2016-05-05 21:53:52 +08:00
|
|
|
if (sx9500_init_chip(dev) < 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("sx9500: failed to initialize chip err %d", ret);
|
2016-05-05 21:53:52 +08:00
|
|
|
return -EINVAL;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 21:53:52 +08:00
|
|
|
if (sx9500_setup_interrupt(dev) < 0) {
|
2016-04-22 00:08:51 +08:00
|
|
|
SYS_LOG_DBG("sx9500: failed to setup interrupt err %d", ret);
|
2016-05-05 21:53:52 +08:00
|
|
|
return -EINVAL;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-12 21:02:58 +08:00
|
|
|
dev->driver_api = &sx9500_api_funcs;
|
|
|
|
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2016-01-26 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 20:52:53 +08:00
|
|
|
struct sx9500_data sx9500_data;
|
|
|
|
|
2016-01-26 00:23:04 +08:00
|
|
|
DEVICE_INIT(sx9500, CONFIG_SX9500_DEV_NAME, sx9500_init, &sx9500_data,
|
|
|
|
NULL, SECONDARY, CONFIG_SX9500_INIT_PRIORITY);
|