2a3c8f8a44
Several on-running cleanup efforts dominate this time, plus the DMA safety alignment issue identified due to improved understanding of the restrictions as a result of Catalin Marinas' efforts in that area. One immutable branch in here due to MFD and SPMI elements needed for the qcom-rradc driver. Device support * bmi088 - Add support for bmi085 (accelerometer part of IMU) - Add support for bmi090l (accelerometer part of IMU) * mcp4922 - Add support for single channel device MCP4921 * rzg2l-adc - Add compatible and minor tweaks to support RZ/G2UL ADC * sca3300 - Add support for scl3300 including refactoring driver to support multiple device types and cleanup noticed whilst working on driver. * spmi-rradc - New driver for Qualcomm SPMI Round Robin ADC including necessary additional utility functions in SPMI core and related MFD driver. * ti-dac55781 - Add compatible for DAC121C081 which is very similar to existing parts. Features * core - Warn on iio_trigger_get() on an unregistered IIO trigger. * bma400 - Triggered buffer support - Activity and step counting - Misc driver improvements such as devm and header ordering * cm32181 - Add PM support. * cros_ec - Sensor location support * sx9324 - Add precharge resistor setting - Add internal compensation resistor setting - Add CS idle/sleep mode. * sx9360 - Add precharge resistor setting * vl53l0x - Handle reset GPIO, regulator and relax handling of irq type. Cleanup and minor fixes: Treewide changes - Cleanup of error handling in remove functions in many drivers. - Update dt-binding maintainers for a number of ADI bindings. - Several sets of conversion of drivers from device tree specific to generic device properties. Includes fixing up various related header and Kconfig issues. - Drop include of of.h from iio.h and fix up drivers that need to include it directly. - More moves of clusters of drivers into appropriate IIO_XXX namespaces. - Tree wide fix of a long running bug around DMA safety requirements. IIO was using __cacheline_aligned to pad iio_priv() structures. This worked for a long time by coincidence, but correct alignment is ARCH_KMALLOC_MINALIGN. As there is activity around this area, introduce an IIO local IIO_DMA_MINALIGN to allow for changing it in one place rather than every driver in future. Note, there have been no reports of this bug in the wild, and it may not happen on any platforms supported by upstream, so no rush to backport these fixes. Other cleanup * core - Switch to ida_alloc()/free() - Drop unused iio_get_time_res() - Octal permissions and DEVICE_ATTR_* macros. - Cleanup bared unsigned usage. * MAINTAINERS - Add include/dt-bindings/iio/ to the main IIO entry. * ad5380 - Comment syntax fix. * ad74413r - Call to for_each_set_bit_from(), with from value as 0 replaced. * ad7768-1 - Drop explicit setting of INDIO_BUFFER_TRIGGERED as now done by the core. * adxl345 - Fix wrong address in dt-binding example. * adxl367 - Drop extra update of FIFO watermark. * at91-sama5d2 - Limit requested watermark to the hwfifo size. * bmg160, bme680 - Typos * cio-dac - Switch to iomap rather than direct use of ioports * kxsd9 - Replace CONFIG_PM guards with new PM macros that let the compiler cleanly remove the unused code and structures when !CONFIG_PM * lsm6dsx - Use new pm_sleep_ptr() and EXPORT_SIMPLE_DEV_PM_OPS(). Then move to Namespace. * meson_saradc - general cleanup. - Avoid attaching resources to iio_dev->dev - Use same struct device for all error messages - Convert to dev_err_probe() and use local struct device *dev to reduce code complexity. - Use devm_clk_get_optional() instead of hand rolling. - Use regmap_read_poll_timeout() instead of hand rolling. * mma7660 - Drop ACPI_PTR() use that is unhelpful. * mpu3050 - Stop exporting symbols not used outside of module - Switch to new DEFINE_RUNTIME_DEV_PM_OPS() macro and move to Namespace. * ping - Typo fix * qcom-spmi-rradc - Typo fix * sc27xx - Convert to generic struct u32_fract * srf08 - Drop a redundant check on !val * st_lsm6dsx - Limit the requested watermark to the hwfifo size. * stm32-adc - Use generic_handle_domain_irq() instead of opencoding. - Fix handling of ADC disable. * stm32-dac - Use str_enabled_disable() instead of open coding. * stx104 - Switch to iomap rather than direct use of ioports * tsc2046 - Drop explicit setting of INDIO_BUFFER_TRIGGERED as now done by the core. * tsl2563 - Replace flush_scheduled_work() with cancel_delayed_work_sync() - Replace cancel_delayed_work() with cancel_delayed_work_sync() * vl53l0x - Make the VDD regulator optional by allowing a dummy regulator. -----BEGIN PGP SIGNATURE----- iQJFBAABCAAvFiEEbilms4eEBlKRJoGxVIU0mcT0FogFAmLQBwARHGppYzIzQGtl cm5lbC5vcmcACgkQVIU0mcT0FohMHRAAi3nPM3IchXe886olumRgX5ke75vU+FNT sqQjqlpl6X7j5yNLU+HF6lC7zg3QzDnfWJ3Uo4C7s2sW7zO0997lkFx4jvF3yW9y 0nLDgYm1Y1te/qR84BE355pMEsYEJVZyPI/Z8UfuBh6IfnSDeTE5ZwCvlRMAchtp AX+j1114FQvZ8yfdIlFjlN4vYSxCGgEZzfWYhpch/Jv1y2Q0L17bpv/KWGt1AYaF VugmKoNTM3WzIABBNGWQ4l4ZVVuVpuiVUL90AKv6Qf+yDSqw94tuIM0NZpJ1rqLr 7tH568m7Eewh7zBp88PRB9XDiZVERvzdVQaT22+wlX+Vhn/SKeoK8PPpdLWD1YyG hXB0p+FGKncoKKgK1w9L7qRRNmsQDHowNHYMSjnzeBuyGQyEqwAXzNvvbiCqHh8y 7b8GW+CYo7//wIarg74Xl+/4LwdCGcMUJNuTm1efEgC7yGToY7JD63DNo3sylaII bfoT06/yogaay/rVLZs5n+MvDONaDt4AbTTErG48s0TzqD/O8Ys85AF0IvKwQ80N WMxe4y+gdJ6VuU+ww9+6WdUzxnx+5gt+ZA2iLxZFekOIUi3Tj+q47F+17Jvwyt7U 2wQzdtvNhHPwD5CqYpxD4iH/z5YFCtIHKjmUsVyAWGVbeuSS/QvFzpf39YWxBWO+ W1w6RStUbXo= =IcsE -----END PGP SIGNATURE----- Merge tag 'iio-for-5.20a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into char-misc-next Jonathan writes: IIO new device support, features and minor fixes for 5.20 Several on-running cleanup efforts dominate this time, plus the DMA safety alignment issue identified due to improved understanding of the restrictions as a result of Catalin Marinas' efforts in that area. One immutable branch in here due to MFD and SPMI elements needed for the qcom-rradc driver. Device support * bmi088 - Add support for bmi085 (accelerometer part of IMU) - Add support for bmi090l (accelerometer part of IMU) * mcp4922 - Add support for single channel device MCP4921 * rzg2l-adc - Add compatible and minor tweaks to support RZ/G2UL ADC * sca3300 - Add support for scl3300 including refactoring driver to support multiple device types and cleanup noticed whilst working on driver. * spmi-rradc - New driver for Qualcomm SPMI Round Robin ADC including necessary additional utility functions in SPMI core and related MFD driver. * ti-dac55781 - Add compatible for DAC121C081 which is very similar to existing parts. Features * core - Warn on iio_trigger_get() on an unregistered IIO trigger. * bma400 - Triggered buffer support - Activity and step counting - Misc driver improvements such as devm and header ordering * cm32181 - Add PM support. * cros_ec - Sensor location support * sx9324 - Add precharge resistor setting - Add internal compensation resistor setting - Add CS idle/sleep mode. * sx9360 - Add precharge resistor setting * vl53l0x - Handle reset GPIO, regulator and relax handling of irq type. Cleanup and minor fixes: Treewide changes - Cleanup of error handling in remove functions in many drivers. - Update dt-binding maintainers for a number of ADI bindings. - Several sets of conversion of drivers from device tree specific to generic device properties. Includes fixing up various related header and Kconfig issues. - Drop include of of.h from iio.h and fix up drivers that need to include it directly. - More moves of clusters of drivers into appropriate IIO_XXX namespaces. - Tree wide fix of a long running bug around DMA safety requirements. IIO was using __cacheline_aligned to pad iio_priv() structures. This worked for a long time by coincidence, but correct alignment is ARCH_KMALLOC_MINALIGN. As there is activity around this area, introduce an IIO local IIO_DMA_MINALIGN to allow for changing it in one place rather than every driver in future. Note, there have been no reports of this bug in the wild, and it may not happen on any platforms supported by upstream, so no rush to backport these fixes. Other cleanup * core - Switch to ida_alloc()/free() - Drop unused iio_get_time_res() - Octal permissions and DEVICE_ATTR_* macros. - Cleanup bared unsigned usage. * MAINTAINERS - Add include/dt-bindings/iio/ to the main IIO entry. * ad5380 - Comment syntax fix. * ad74413r - Call to for_each_set_bit_from(), with from value as 0 replaced. * ad7768-1 - Drop explicit setting of INDIO_BUFFER_TRIGGERED as now done by the core. * adxl345 - Fix wrong address in dt-binding example. * adxl367 - Drop extra update of FIFO watermark. * at91-sama5d2 - Limit requested watermark to the hwfifo size. * bmg160, bme680 - Typos * cio-dac - Switch to iomap rather than direct use of ioports * kxsd9 - Replace CONFIG_PM guards with new PM macros that let the compiler cleanly remove the unused code and structures when !CONFIG_PM * lsm6dsx - Use new pm_sleep_ptr() and EXPORT_SIMPLE_DEV_PM_OPS(). Then move to Namespace. * meson_saradc - general cleanup. - Avoid attaching resources to iio_dev->dev - Use same struct device for all error messages - Convert to dev_err_probe() and use local struct device *dev to reduce code complexity. - Use devm_clk_get_optional() instead of hand rolling. - Use regmap_read_poll_timeout() instead of hand rolling. * mma7660 - Drop ACPI_PTR() use that is unhelpful. * mpu3050 - Stop exporting symbols not used outside of module - Switch to new DEFINE_RUNTIME_DEV_PM_OPS() macro and move to Namespace. * ping - Typo fix * qcom-spmi-rradc - Typo fix * sc27xx - Convert to generic struct u32_fract * srf08 - Drop a redundant check on !val * st_lsm6dsx - Limit the requested watermark to the hwfifo size. * stm32-adc - Use generic_handle_domain_irq() instead of opencoding. - Fix handling of ADC disable. * stm32-dac - Use str_enabled_disable() instead of open coding. * stx104 - Switch to iomap rather than direct use of ioports * tsc2046 - Drop explicit setting of INDIO_BUFFER_TRIGGERED as now done by the core. * tsl2563 - Replace flush_scheduled_work() with cancel_delayed_work_sync() - Replace cancel_delayed_work() with cancel_delayed_work_sync() * vl53l0x - Make the VDD regulator optional by allowing a dummy regulator. * tag 'iio-for-5.20a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (244 commits) iio: adc: xilinx-xadc: Drop duplicate NULL check in xadc_parse_dt() iio: adc: xilinx-xadc: Make use of device properties iio: light: cm32181: Add PM support iio: adc: ad778-1: do not explicity set INDIO_BUFFER_TRIGGERED mode iio: adc: ti-tsc2046: do not explicity set INDIO_BUFFER_TRIGGERED mode iio: adc: stm32-adc: disable adc before calibration iio: adc: stm32-adc: make safe adc disable iio: dac: ad5380: align '*' each line and drop unneeded blank line iio: adc: qcom-spmi-rradc: Fix spelling mistake "coherrency" -> "coherency" iio: Don't use bare "unsigned" dt-bindings: iio: dac: mcp4922: expand for mcp4921 support iio: dac: mcp4922: add support to mcp4921 iio: chemical: sps30: Move symbol exports into IIO_SPS30 namespace iio: pressure: bmp280: Move symbol exports to IIO_BMP280 namespace iio: imu: bmi160: Move exported symbols to IIO_BMI160 namespace iio: adc: stm32-adc: Use generic_handle_domain_irq() proximity: vl53l0x: Make VDD regulator actually optional MAINTAINERS: add include/dt-bindings/iio to IIO SUBSYSTEM AND DRIVERS dt-bindings: iio/accel: Fix adi,adxl345/6 example I2C address iio: gyro: bmg160: Fix typo in comment ...
581 lines
14 KiB
C
581 lines
14 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* 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>
|
|
#include <linux/gpio/consumer.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/iio/iio.h>
|
|
#include <linux/iio/buffer.h>
|
|
#include <linux/iio/trigger.h>
|
|
#include <linux/iio/triggered_buffer.h>
|
|
#include <linux/iio/trigger_consumer.h>
|
|
#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
|
|
#define CCS811_HW_ID_VALUE 0x81
|
|
#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
|
|
#define CCS811_SW_RESET 0xFF
|
|
|
|
/* 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
|
|
|
|
#define CCS811_MEAS_MODE_INTERRUPT BIT(3)
|
|
|
|
#define CCS811_VOLTAGE_MASK 0x3FF
|
|
|
|
struct ccs811_reading {
|
|
__be16 co2;
|
|
__be16 voc;
|
|
u8 status;
|
|
u8 error;
|
|
__be16 raw_data;
|
|
} __attribute__((__packed__));
|
|
|
|
struct ccs811_data {
|
|
struct i2c_client *client;
|
|
struct mutex lock; /* Protect readings */
|
|
struct ccs811_reading buffer;
|
|
struct iio_trigger *drdy_trig;
|
|
struct gpio_desc *wakeup_gpio;
|
|
bool drdy_trig_on;
|
|
/* Ensures correct alignment of timestamp if present */
|
|
struct {
|
|
s16 channels[2];
|
|
s64 ts __aligned(8);
|
|
} scan;
|
|
};
|
|
|
|
static const struct iio_chan_spec ccs811_channels[] = {
|
|
{
|
|
.type = IIO_CURRENT,
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
.scan_index = -1,
|
|
}, {
|
|
.type = IIO_VOLTAGE,
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
.scan_index = -1,
|
|
}, {
|
|
.type = IIO_CONCENTRATION,
|
|
.channel2 = IIO_MOD_CO2,
|
|
.modified = 1,
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
.scan_index = 0,
|
|
.scan_type = {
|
|
.sign = 'u',
|
|
.realbits = 16,
|
|
.storagebits = 16,
|
|
.endianness = IIO_BE,
|
|
},
|
|
}, {
|
|
.type = IIO_CONCENTRATION,
|
|
.channel2 = IIO_MOD_VOC,
|
|
.modified = 1,
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
|
|
BIT(IIO_CHAN_INFO_SCALE),
|
|
.scan_index = 1,
|
|
.scan_type = {
|
|
.sign = 'u',
|
|
.realbits = 16,
|
|
.storagebits = 16,
|
|
.endianness = IIO_BE,
|
|
},
|
|
},
|
|
IIO_CHAN_SOFT_TIMESTAMP(2),
|
|
};
|
|
|
|
/*
|
|
* 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;
|
|
|
|
if ((ret & CCS811_STATUS_FW_MODE_APPLICATION))
|
|
return 0;
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
static int ccs811_get_measurement(struct ccs811_data *data)
|
|
{
|
|
int ret, tries = 11;
|
|
|
|
ccs811_set_wakeup(data, true);
|
|
|
|
/* 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;
|
|
|
|
ret = i2c_smbus_read_i2c_block_data(data->client,
|
|
CCS811_ALG_RESULT_DATA, 8,
|
|
(char *)&data->buffer);
|
|
ccs811_set_wakeup(data, false);
|
|
|
|
return ret;
|
|
}
|
|
|
|
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:
|
|
ret = iio_device_claim_direct_mode(indio_dev);
|
|
if (ret)
|
|
return ret;
|
|
mutex_lock(&data->lock);
|
|
ret = ccs811_get_measurement(data);
|
|
if (ret < 0) {
|
|
mutex_unlock(&data->lock);
|
|
iio_device_release_direct_mode(indio_dev);
|
|
return ret;
|
|
}
|
|
|
|
switch (chan->type) {
|
|
case IIO_VOLTAGE:
|
|
*val = be16_to_cpu(data->buffer.raw_data) &
|
|
CCS811_VOLTAGE_MASK;
|
|
ret = IIO_VAL_INT;
|
|
break;
|
|
case IIO_CURRENT:
|
|
*val = be16_to_cpu(data->buffer.raw_data) >> 10;
|
|
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);
|
|
iio_device_release_direct_mode(indio_dev);
|
|
|
|
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;
|
|
*val2 = 100;
|
|
return IIO_VAL_INT_PLUS_MICRO;
|
|
case IIO_MOD_VOC:
|
|
*val = 0;
|
|
*val2 = 100;
|
|
return IIO_VAL_INT_PLUS_NANO;
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
static const struct iio_info ccs811_info = {
|
|
.read_raw = ccs811_read_raw,
|
|
};
|
|
|
|
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,
|
|
};
|
|
|
|
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;
|
|
|
|
ret = i2c_smbus_read_i2c_block_data(client, CCS811_ALG_RESULT_DATA,
|
|
sizeof(data->scan.channels),
|
|
(u8 *)data->scan.channels);
|
|
if (ret != 4) {
|
|
dev_err(&client->dev, "cannot read sensor data\n");
|
|
goto err;
|
|
}
|
|
|
|
iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
|
|
iio_get_time_ns(indio_dev));
|
|
|
|
err:
|
|
iio_trigger_notify_done(indio_dev->trig);
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
|
|
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;
|
|
}
|
|
|
|
/* Check hardware id (should be 0x81 for this family of devices) */
|
|
ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID);
|
|
if (ret < 0) {
|
|
ccs811_set_wakeup(data, false);
|
|
return ret;
|
|
}
|
|
|
|
if (ret != CCS811_HW_ID_VALUE) {
|
|
dev_err(&client->dev, "hardware id doesn't match CCS81x\n");
|
|
ccs811_set_wakeup(data, false);
|
|
return -ENODEV;
|
|
}
|
|
|
|
ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION);
|
|
if (ret < 0) {
|
|
ccs811_set_wakeup(data, false);
|
|
return ret;
|
|
}
|
|
|
|
if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) {
|
|
dev_err(&client->dev, "no CCS811 sensor\n");
|
|
ccs811_set_wakeup(data, false);
|
|
return -ENODEV;
|
|
}
|
|
|
|
ret = ccs811_setup(client);
|
|
if (ret < 0) {
|
|
ccs811_set_wakeup(data, false);
|
|
return ret;
|
|
}
|
|
|
|
ccs811_set_wakeup(data, false);
|
|
|
|
mutex_init(&data->lock);
|
|
|
|
indio_dev->name = id->name;
|
|
indio_dev->info = &ccs811_info;
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
|
|
|
indio_dev->channels = ccs811_channels;
|
|
indio_dev->num_channels = ARRAY_SIZE(ccs811_channels);
|
|
|
|
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,
|
|
iio_device_id(indio_dev));
|
|
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);
|
|
ret = iio_trigger_register(data->drdy_trig);
|
|
if (ret)
|
|
goto err_poweroff;
|
|
|
|
indio_dev->trig = iio_trigger_get(data->drdy_trig);
|
|
}
|
|
|
|
ret = iio_triggered_buffer_setup(indio_dev, NULL,
|
|
ccs811_trigger_handler, NULL);
|
|
|
|
if (ret < 0) {
|
|
dev_err(&client->dev, "triggered buffer setup failed\n");
|
|
goto err_trigger_unregister;
|
|
}
|
|
|
|
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);
|
|
err_trigger_unregister:
|
|
if (data->drdy_trig)
|
|
iio_trigger_unregister(data->drdy_trig);
|
|
err_poweroff:
|
|
i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE, CCS811_MODE_IDLE);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int ccs811_remove(struct i2c_client *client)
|
|
{
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(client);
|
|
struct ccs811_data *data = iio_priv(indio_dev);
|
|
int ret;
|
|
|
|
iio_device_unregister(indio_dev);
|
|
iio_triggered_buffer_cleanup(indio_dev);
|
|
if (data->drdy_trig)
|
|
iio_trigger_unregister(data->drdy_trig);
|
|
|
|
ret = i2c_smbus_write_byte_data(client, CCS811_MEAS_MODE,
|
|
CCS811_MODE_IDLE);
|
|
if (ret)
|
|
dev_warn(&client->dev, "Failed to power down device (%pe)\n",
|
|
ERR_PTR(ret));
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct i2c_device_id ccs811_id[] = {
|
|
{"ccs811", 0},
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(i2c, ccs811_id);
|
|
|
|
static const struct of_device_id ccs811_dt_ids[] = {
|
|
{ .compatible = "ams,ccs811" },
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(of, ccs811_dt_ids);
|
|
|
|
static struct i2c_driver ccs811_driver = {
|
|
.driver = {
|
|
.name = "ccs811",
|
|
.of_match_table = ccs811_dt_ids,
|
|
},
|
|
.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");
|