2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-07-17 19:28:03 +00:00
|
|
|
/*
|
|
|
|
* ccs811.c - Support for AMS CCS811 VOC Sensor
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Narcisa Vasile <narcisaanamaria12@gmail.com>
|
|
|
|
*
|
|
|
|
* Datasheet: ams.com/content/download/951091/2269479/CCS811_DS000459_3-00.pdf
|
|
|
|
*
|
|
|
|
* IIO driver for AMS CCS811 (I2C address 0x5A/0x5B set by ADDR Low/High)
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* 1. Make the drive mode selectable form userspace
|
|
|
|
* 2. Add support for interrupts
|
|
|
|
* 3. Adjust time to wait for data to be ready based on selected operation mode
|
|
|
|
* 4. Read error register and put the information in logs
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/delay.h>
|
2020-04-14 18:49:29 +00:00
|
|
|
#include <linux/gpio/consumer.h>
|
2017-07-17 19:28:03 +00:00
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/iio/iio.h>
|
2017-08-17 11:34:29 +00:00
|
|
|
#include <linux/iio/buffer.h>
|
2017-09-07 18:38:37 +00:00
|
|
|
#include <linux/iio/trigger.h>
|
2017-08-17 11:34:29 +00:00
|
|
|
#include <linux/iio/triggered_buffer.h>
|
|
|
|
#include <linux/iio/trigger_consumer.h>
|
2017-07-17 19:28:03 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#define CCS811_STATUS 0x00
|
|
|
|
#define CCS811_MEAS_MODE 0x01
|
|
|
|
#define CCS811_ALG_RESULT_DATA 0x02
|
|
|
|
#define CCS811_RAW_DATA 0x03
|
|
|
|
#define CCS811_HW_ID 0x20
|
2018-02-13 22:36:57 +00:00
|
|
|
#define CCS811_HW_ID_VALUE 0x81
|
2017-07-17 19:28:03 +00:00
|
|
|
#define CCS811_HW_VERSION 0x21
|
|
|
|
#define CCS811_HW_VERSION_VALUE 0x10
|
|
|
|
#define CCS811_HW_VERSION_MASK 0xF0
|
|
|
|
#define CCS811_ERR 0xE0
|
|
|
|
/* Used to transition from boot to application mode */
|
|
|
|
#define CCS811_APP_START 0xF4
|
2020-04-14 18:49:29 +00:00
|
|
|
#define CCS811_SW_RESET 0xFF
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
/* Status register flags */
|
|
|
|
#define CCS811_STATUS_ERROR BIT(0)
|
|
|
|
#define CCS811_STATUS_DATA_READY BIT(3)
|
|
|
|
#define CCS811_STATUS_APP_VALID_MASK BIT(4)
|
|
|
|
#define CCS811_STATUS_APP_VALID_LOADED BIT(4)
|
|
|
|
/*
|
|
|
|
* Value of FW_MODE bit of STATUS register describes the sensor's state:
|
|
|
|
* 0: Firmware is in boot mode, this allows new firmware to be loaded
|
|
|
|
* 1: Firmware is in application mode. CCS811 is ready to take ADC measurements
|
|
|
|
*/
|
|
|
|
#define CCS811_STATUS_FW_MODE_MASK BIT(7)
|
|
|
|
#define CCS811_STATUS_FW_MODE_APPLICATION BIT(7)
|
|
|
|
|
|
|
|
/* Measurement modes */
|
|
|
|
#define CCS811_MODE_IDLE 0x00
|
|
|
|
#define CCS811_MODE_IAQ_1SEC 0x10
|
|
|
|
#define CCS811_MODE_IAQ_10SEC 0x20
|
|
|
|
#define CCS811_MODE_IAQ_60SEC 0x30
|
|
|
|
#define CCS811_MODE_RAW_DATA 0x40
|
|
|
|
|
2017-09-07 18:38:37 +00:00
|
|
|
#define CCS811_MEAS_MODE_INTERRUPT BIT(3)
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
#define CCS811_VOLTAGE_MASK 0x3FF
|
|
|
|
|
|
|
|
struct ccs811_reading {
|
|
|
|
__be16 co2;
|
|
|
|
__be16 voc;
|
|
|
|
u8 status;
|
|
|
|
u8 error;
|
2018-02-14 01:08:35 +00:00
|
|
|
__be16 raw_data;
|
2017-07-17 19:28:03 +00:00
|
|
|
} __attribute__((__packed__));
|
|
|
|
|
|
|
|
struct ccs811_data {
|
|
|
|
struct i2c_client *client;
|
|
|
|
struct mutex lock; /* Protect readings */
|
|
|
|
struct ccs811_reading buffer;
|
2017-09-07 18:38:37 +00:00
|
|
|
struct iio_trigger *drdy_trig;
|
2020-04-14 18:49:29 +00:00
|
|
|
struct gpio_desc *wakeup_gpio;
|
2017-09-07 18:38:37 +00:00
|
|
|
bool drdy_trig_on;
|
2020-07-22 15:50:43 +00:00
|
|
|
/* Ensures correct alignment of timestamp if present */
|
|
|
|
struct {
|
|
|
|
s16 channels[2];
|
|
|
|
s64 ts __aligned(8);
|
|
|
|
} scan;
|
2017-07-17 19:28:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct iio_chan_spec ccs811_channels[] = {
|
|
|
|
{
|
|
|
|
.type = IIO_CURRENT,
|
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
2017-08-17 11:34:29 +00:00
|
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
|
|
.scan_index = -1,
|
2017-07-17 19:28:03 +00:00
|
|
|
}, {
|
|
|
|
.type = IIO_VOLTAGE,
|
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
2017-08-17 11:34:29 +00:00
|
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
|
|
.scan_index = -1,
|
2017-07-17 19:28:03 +00:00
|
|
|
}, {
|
|
|
|
.type = IIO_CONCENTRATION,
|
|
|
|
.channel2 = IIO_MOD_CO2,
|
|
|
|
.modified = 1,
|
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
2017-08-17 11:34:29 +00:00
|
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
|
|
.scan_index = 0,
|
|
|
|
.scan_type = {
|
|
|
|
.sign = 'u',
|
|
|
|
.realbits = 16,
|
|
|
|
.storagebits = 16,
|
|
|
|
.endianness = IIO_BE,
|
|
|
|
},
|
2017-07-17 19:28:03 +00:00
|
|
|
}, {
|
|
|
|
.type = IIO_CONCENTRATION,
|
|
|
|
.channel2 = IIO_MOD_VOC,
|
|
|
|
.modified = 1,
|
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
2017-08-17 11:34:29 +00:00
|
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
|
|
.scan_index = 1,
|
|
|
|
.scan_type = {
|
|
|
|
.sign = 'u',
|
|
|
|
.realbits = 16,
|
|
|
|
.storagebits = 16,
|
|
|
|
.endianness = IIO_BE,
|
|
|
|
},
|
2017-07-17 19:28:03 +00:00
|
|
|
},
|
2017-08-17 11:34:29 +00:00
|
|
|
IIO_CHAN_SOFT_TIMESTAMP(2),
|
2017-07-17 19:28:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The CCS811 powers-up in boot mode. A setup write to CCS811_APP_START will
|
|
|
|
* transition the sensor to application mode.
|
|
|
|
*/
|
|
|
|
static int ccs811_start_sensor_application(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
iio: chemical: ccs811: Corrected firmware boot/application mode transition
CCS811 has different I2C register maps in boot and application mode. When
CCS811 is in boot mode, register APP_START (0xF4) is used to transit the
firmware state from boot to application mode. However, APP_START is not a
valid register location when CCS811 is in application mode (refer to
"CCS811 Bootloader Register Map" and "CCS811 Application Register Map" in
CCS811 datasheet). The driver should not attempt to perform a write to
APP_START while CCS811 is in application mode, as this is not a valid or
documented register location.
When prob function is being called, the driver assumes the CCS811 sensor
is in boot mode, and attempts to perform a write to APP_START. Although
CCS811 powers-up in boot mode, it may have already been transited to
application mode by previous instances, e.g. unload and reload device
driver by the system, or explicitly by user. Depending on the system
design, CCS811 sensor may be permanently connected to system power source
rather than power controlled by GPIO, hence it is possible that the sensor
is never power reset, thus the firmware could be in either boot or
application mode at any given time when driver prob function is being
called.
This patch checks the STATUS register before attempting to send a write to
APP_START. Only if the firmware is not in application mode and has valid
firmware application loaded, then it will continue to start transiting the
firmware boot to application mode.
Signed-off-by: Richard Lai <richard@richardman.com>
Cc: <Stable@vger.kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2018-02-17 16:28:24 +00:00
|
|
|
if ((ret & CCS811_STATUS_FW_MODE_APPLICATION))
|
|
|
|
return 0;
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
if ((ret & CCS811_STATUS_APP_VALID_MASK) !=
|
|
|
|
CCS811_STATUS_APP_VALID_LOADED)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
ret = i2c_smbus_write_byte(client, CCS811_APP_START);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_byte_data(client, CCS811_STATUS);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret & CCS811_STATUS_FW_MODE_MASK) !=
|
|
|
|
CCS811_STATUS_FW_MODE_APPLICATION) {
|
|
|
|
dev_err(&client->dev, "Application failed to start. Sensor is still in boot mode.\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ccs811_setup(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ccs811_start_sensor_application(client);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
|
|
|
|
CCS811_MODE_IAQ_1SEC);
|
|
|
|
}
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
static void ccs811_set_wakeup(struct ccs811_data *data, bool enable)
|
|
|
|
{
|
|
|
|
if (!data->wakeup_gpio)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gpiod_set_value(data->wakeup_gpio, enable);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
usleep_range(50, 60);
|
|
|
|
else
|
|
|
|
usleep_range(20, 30);
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
static int ccs811_get_measurement(struct ccs811_data *data)
|
|
|
|
{
|
|
|
|
int ret, tries = 11;
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
ccs811_set_wakeup(data, true);
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
/* Maximum waiting time: 1s, as measurements are made every second */
|
|
|
|
while (tries-- > 0) {
|
|
|
|
ret = i2c_smbus_read_byte_data(data->client, CCS811_STATUS);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret & CCS811_STATUS_DATA_READY) || tries == 0)
|
|
|
|
break;
|
|
|
|
msleep(100);
|
|
|
|
}
|
|
|
|
if (!(ret & CCS811_STATUS_DATA_READY))
|
|
|
|
return -EIO;
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
ret = i2c_smbus_read_i2c_block_data(data->client,
|
2017-07-17 19:28:03 +00:00
|
|
|
CCS811_ALG_RESULT_DATA, 8,
|
|
|
|
(char *)&data->buffer);
|
2020-04-14 18:49:29 +00:00
|
|
|
ccs811_set_wakeup(data, false);
|
|
|
|
|
|
|
|
return ret;
|
2017-07-17 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ccs811_read_raw(struct iio_dev *indio_dev,
|
|
|
|
struct iio_chan_spec const *chan,
|
|
|
|
int *val, int *val2, long mask)
|
|
|
|
{
|
|
|
|
struct ccs811_data *data = iio_priv(indio_dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (mask) {
|
|
|
|
case IIO_CHAN_INFO_RAW:
|
2017-09-07 18:38:37 +00:00
|
|
|
ret = iio_device_claim_direct_mode(indio_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-07-17 19:28:03 +00:00
|
|
|
mutex_lock(&data->lock);
|
|
|
|
ret = ccs811_get_measurement(data);
|
|
|
|
if (ret < 0) {
|
|
|
|
mutex_unlock(&data->lock);
|
2017-09-07 18:38:37 +00:00
|
|
|
iio_device_release_direct_mode(indio_dev);
|
2017-07-17 19:28:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (chan->type) {
|
|
|
|
case IIO_VOLTAGE:
|
2018-02-14 01:08:35 +00:00
|
|
|
*val = be16_to_cpu(data->buffer.raw_data) &
|
2017-07-17 19:28:03 +00:00
|
|
|
CCS811_VOLTAGE_MASK;
|
|
|
|
ret = IIO_VAL_INT;
|
|
|
|
break;
|
|
|
|
case IIO_CURRENT:
|
2018-02-14 01:08:35 +00:00
|
|
|
*val = be16_to_cpu(data->buffer.raw_data) >> 10;
|
2017-07-17 19:28:03 +00:00
|
|
|
ret = IIO_VAL_INT;
|
|
|
|
break;
|
|
|
|
case IIO_CONCENTRATION:
|
|
|
|
switch (chan->channel2) {
|
|
|
|
case IIO_MOD_CO2:
|
|
|
|
*val = be16_to_cpu(data->buffer.co2);
|
|
|
|
ret = IIO_VAL_INT;
|
|
|
|
break;
|
|
|
|
case IIO_MOD_VOC:
|
|
|
|
*val = be16_to_cpu(data->buffer.voc);
|
|
|
|
ret = IIO_VAL_INT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
mutex_unlock(&data->lock);
|
2017-09-07 18:38:37 +00:00
|
|
|
iio_device_release_direct_mode(indio_dev);
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
case IIO_CHAN_INFO_SCALE:
|
|
|
|
switch (chan->type) {
|
|
|
|
case IIO_VOLTAGE:
|
|
|
|
*val = 1;
|
|
|
|
*val2 = 612903;
|
|
|
|
return IIO_VAL_INT_PLUS_MICRO;
|
|
|
|
case IIO_CURRENT:
|
|
|
|
*val = 0;
|
|
|
|
*val2 = 1000;
|
|
|
|
return IIO_VAL_INT_PLUS_MICRO;
|
|
|
|
case IIO_CONCENTRATION:
|
|
|
|
switch (chan->channel2) {
|
|
|
|
case IIO_MOD_CO2:
|
|
|
|
*val = 0;
|
2017-12-06 16:57:58 +00:00
|
|
|
*val2 = 100;
|
2017-07-17 19:28:03 +00:00
|
|
|
return IIO_VAL_INT_PLUS_MICRO;
|
|
|
|
case IIO_MOD_VOC:
|
|
|
|
*val = 0;
|
2017-12-06 16:57:58 +00:00
|
|
|
*val2 = 100;
|
|
|
|
return IIO_VAL_INT_PLUS_NANO;
|
2017-07-17 19:28:03 +00:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct iio_info ccs811_info = {
|
|
|
|
.read_raw = ccs811_read_raw,
|
|
|
|
};
|
|
|
|
|
2017-09-07 18:38:37 +00:00
|
|
|
static int ccs811_set_trigger_state(struct iio_trigger *trig,
|
|
|
|
bool state)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
|
|
|
|
struct ccs811_data *data = iio_priv(indio_dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_byte_data(data->client, CCS811_MEAS_MODE);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
ret |= CCS811_MEAS_MODE_INTERRUPT;
|
|
|
|
else
|
|
|
|
ret &= ~CCS811_MEAS_MODE_INTERRUPT;
|
|
|
|
|
|
|
|
data->drdy_trig_on = state;
|
|
|
|
|
|
|
|
return i2c_smbus_write_byte_data(data->client, CCS811_MEAS_MODE, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct iio_trigger_ops ccs811_trigger_ops = {
|
|
|
|
.set_trigger_state = ccs811_set_trigger_state,
|
|
|
|
};
|
|
|
|
|
2017-08-17 11:34:29 +00:00
|
|
|
static irqreturn_t ccs811_trigger_handler(int irq, void *p)
|
|
|
|
{
|
|
|
|
struct iio_poll_func *pf = p;
|
|
|
|
struct iio_dev *indio_dev = pf->indio_dev;
|
|
|
|
struct ccs811_data *data = iio_priv(indio_dev);
|
|
|
|
struct i2c_client *client = data->client;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-22 15:50:43 +00:00
|
|
|
ret = i2c_smbus_read_i2c_block_data(client, CCS811_ALG_RESULT_DATA,
|
|
|
|
sizeof(data->scan.channels),
|
|
|
|
(u8 *)data->scan.channels);
|
2017-08-17 11:34:29 +00:00
|
|
|
if (ret != 4) {
|
|
|
|
dev_err(&client->dev, "cannot read sensor data\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:50:43 +00:00
|
|
|
iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
|
2017-08-17 11:34:29 +00:00
|
|
|
iio_get_time_ns(indio_dev));
|
|
|
|
|
|
|
|
err:
|
|
|
|
iio_trigger_notify_done(indio_dev->trig);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2017-09-07 18:38:37 +00:00
|
|
|
static irqreturn_t ccs811_data_rdy_trigger_poll(int irq, void *private)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = private;
|
|
|
|
struct ccs811_data *data = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
if (data->drdy_trig_on)
|
|
|
|
iio_trigger_poll(data->drdy_trig);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
static int ccs811_reset(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct gpio_desc *reset_gpio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
|
|
|
|
GPIOD_OUT_LOW);
|
|
|
|
if (IS_ERR(reset_gpio))
|
|
|
|
return PTR_ERR(reset_gpio);
|
|
|
|
|
|
|
|
/* Try to reset using nRESET pin if available else do SW reset */
|
|
|
|
if (reset_gpio) {
|
|
|
|
gpiod_set_value(reset_gpio, 1);
|
|
|
|
usleep_range(20, 30);
|
|
|
|
gpiod_set_value(reset_gpio, 0);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* As per the datasheet, this sequence of values needs to be
|
|
|
|
* written to the SW_RESET register for triggering the soft
|
|
|
|
* reset in the device and placing it in boot mode.
|
|
|
|
*/
|
|
|
|
static const u8 reset_seq[] = {
|
|
|
|
0x11, 0xE5, 0x72, 0x8A,
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = i2c_smbus_write_i2c_block_data(client, CCS811_SW_RESET,
|
|
|
|
sizeof(reset_seq), reset_seq);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "Failed to reset sensor\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tSTART delay required after reset */
|
|
|
|
usleep_range(1000, 2000);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
static int ccs811_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev;
|
|
|
|
struct ccs811_data *data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
|
|
|
|
| I2C_FUNC_SMBUS_BYTE_DATA
|
|
|
|
| I2C_FUNC_SMBUS_READ_I2C_BLOCK))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
|
|
|
|
if (!indio_dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
data = iio_priv(indio_dev);
|
|
|
|
i2c_set_clientdata(client, indio_dev);
|
|
|
|
data->client = client;
|
|
|
|
|
|
|
|
data->wakeup_gpio = devm_gpiod_get_optional(&client->dev, "wakeup",
|
|
|
|
GPIOD_OUT_HIGH);
|
|
|
|
if (IS_ERR(data->wakeup_gpio))
|
|
|
|
return PTR_ERR(data->wakeup_gpio);
|
|
|
|
|
|
|
|
ccs811_set_wakeup(data, true);
|
|
|
|
|
|
|
|
ret = ccs811_reset(client);
|
|
|
|
if (ret) {
|
|
|
|
ccs811_set_wakeup(data, false);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
/* Check hardware id (should be 0x81 for this family of devices) */
|
|
|
|
ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID);
|
2020-04-14 18:49:29 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ccs811_set_wakeup(data, false);
|
2017-07-17 19:28:03 +00:00
|
|
|
return ret;
|
2020-04-14 18:49:29 +00:00
|
|
|
}
|
2017-07-17 19:28:03 +00:00
|
|
|
|
2018-02-13 22:36:57 +00:00
|
|
|
if (ret != CCS811_HW_ID_VALUE) {
|
2017-07-17 19:28:03 +00:00
|
|
|
dev_err(&client->dev, "hardware id doesn't match CCS81x\n");
|
2020-04-14 18:49:29 +00:00
|
|
|
ccs811_set_wakeup(data, false);
|
2017-07-17 19:28:03 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION);
|
2020-04-14 18:49:29 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ccs811_set_wakeup(data, false);
|
2017-07-17 19:28:03 +00:00
|
|
|
return ret;
|
2020-04-14 18:49:29 +00:00
|
|
|
}
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) {
|
|
|
|
dev_err(&client->dev, "no CCS811 sensor\n");
|
2020-04-14 18:49:29 +00:00
|
|
|
ccs811_set_wakeup(data, false);
|
2017-07-17 19:28:03 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ccs811_setup(client);
|
2020-04-14 18:49:29 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ccs811_set_wakeup(data, false);
|
2017-07-17 19:28:03 +00:00
|
|
|
return ret;
|
2020-04-14 18:49:29 +00:00
|
|
|
}
|
2017-07-17 19:28:03 +00:00
|
|
|
|
2020-04-14 18:49:29 +00:00
|
|
|
ccs811_set_wakeup(data, false);
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
mutex_init(&data->lock);
|
|
|
|
|
|
|
|
indio_dev->name = id->name;
|
|
|
|
indio_dev->info = &ccs811_info;
|
2017-09-07 18:38:37 +00:00
|
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
indio_dev->channels = ccs811_channels;
|
|
|
|
indio_dev->num_channels = ARRAY_SIZE(ccs811_channels);
|
|
|
|
|
2017-09-07 18:38:37 +00:00
|
|
|
if (client->irq > 0) {
|
|
|
|
ret = devm_request_threaded_irq(&client->dev, client->irq,
|
|
|
|
ccs811_data_rdy_trigger_poll,
|
|
|
|
NULL,
|
|
|
|
IRQF_TRIGGER_FALLING |
|
|
|
|
IRQF_ONESHOT,
|
|
|
|
"ccs811_irq", indio_dev);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&client->dev, "irq request error %d\n", -ret);
|
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->drdy_trig = devm_iio_trigger_alloc(&client->dev,
|
|
|
|
"%s-dev%d",
|
|
|
|
indio_dev->name,
|
|
|
|
indio_dev->id);
|
|
|
|
if (!data->drdy_trig) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->drdy_trig->ops = &ccs811_trigger_ops;
|
|
|
|
iio_trigger_set_drvdata(data->drdy_trig, indio_dev);
|
|
|
|
indio_dev->trig = data->drdy_trig;
|
|
|
|
iio_trigger_get(indio_dev->trig);
|
|
|
|
ret = iio_trigger_register(data->drdy_trig);
|
|
|
|
if (ret)
|
|
|
|
goto err_poweroff;
|
|
|
|
}
|
|
|
|
|
2017-08-17 11:34:29 +00:00
|
|
|
ret = iio_triggered_buffer_setup(indio_dev, NULL,
|
|
|
|
ccs811_trigger_handler, NULL);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "triggered buffer setup failed\n");
|
2017-09-07 18:38:37 +00:00
|
|
|
goto err_trigger_unregister;
|
2017-08-17 11:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = iio_device_register(indio_dev);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&client->dev, "unable to register iio device\n");
|
|
|
|
goto err_buffer_cleanup;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_buffer_cleanup:
|
|
|
|
iio_triggered_buffer_cleanup(indio_dev);
|
2017-09-07 18:38:37 +00:00
|
|
|
err_trigger_unregister:
|
|
|
|
if (data->drdy_trig)
|
|
|
|
iio_trigger_unregister(data->drdy_trig);
|
2017-08-17 11:34:29 +00:00
|
|
|
err_poweroff:
|
|
|
|
i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, CCS811_MODE_IDLE);
|
|
|
|
|
|
|
|
return ret;
|
2017-07-17 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ccs811_remove(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(client);
|
2017-09-07 18:38:37 +00:00
|
|
|
struct ccs811_data *data = iio_priv(indio_dev);
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
iio_device_unregister(indio_dev);
|
2017-08-17 11:34:29 +00:00
|
|
|
iio_triggered_buffer_cleanup(indio_dev);
|
2017-09-07 18:38:37 +00:00
|
|
|
if (data->drdy_trig)
|
|
|
|
iio_trigger_unregister(data->drdy_trig);
|
2017-07-17 19:28:03 +00:00
|
|
|
|
|
|
|
return i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
|
|
|
|
CCS811_MODE_IDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id ccs811_id[] = {
|
|
|
|
{"ccs811", 0},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, ccs811_id);
|
|
|
|
|
2020-04-14 18:49:30 +00:00
|
|
|
static const struct of_device_id ccs811_dt_ids[] = {
|
|
|
|
{ .compatible = "ams,ccs811" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, ccs811_dt_ids);
|
|
|
|
|
2017-07-17 19:28:03 +00:00
|
|
|
static struct i2c_driver ccs811_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ccs811",
|
2020-04-14 18:49:30 +00:00
|
|
|
.of_match_table = ccs811_dt_ids,
|
2017-07-17 19:28:03 +00:00
|
|
|
},
|
|
|
|
.probe = ccs811_probe,
|
|
|
|
.remove = ccs811_remove,
|
|
|
|
.id_table = ccs811_id,
|
|
|
|
};
|
|
|
|
module_i2c_driver(ccs811_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Narcisa Vasile <narcisaanamaria12@gmail.com>");
|
|
|
|
MODULE_DESCRIPTION("CCS811 volatile organic compounds sensor");
|
|
|
|
MODULE_LICENSE("GPL v2");
|