BigW Consortium Gitlab

Commit 6011319c by David Frey

Introduce bme680 backport work in separate commit

parent fea8fa44
/*
* Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
* Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _LINUX_BITFIELD_H
#define _LINUX_BITFIELD_H
#include <asm/byteorder.h>
/*
* Bitfield access macros
*
* FIELD_{GET,PREP} macros take as first parameter shifted mask
* from which they extract the base mask and shift amount.
* Mask must be a compilation time constant.
*
* Example:
*
* #define REG_FIELD_A GENMASK(6, 0)
* #define REG_FIELD_B BIT(7)
* #define REG_FIELD_C GENMASK(15, 8)
* #define REG_FIELD_D GENMASK(31, 16)
*
* Get:
* a = FIELD_GET(REG_FIELD_A, reg);
* b = FIELD_GET(REG_FIELD_B, reg);
*
* Set:
* reg = FIELD_PREP(REG_FIELD_A, 1) |
* FIELD_PREP(REG_FIELD_B, 0) |
* FIELD_PREP(REG_FIELD_C, c) |
* FIELD_PREP(REG_FIELD_D, 0x40);
*
* Modify:
* reg &= ~REG_FIELD_C;
* reg |= FIELD_PREP(REG_FIELD_C, c);
*/
#define __bf_shf(x) (__builtin_ffsll(x) - 1)
/**
* FIELD_PREP() - prepare a bitfield element
* @_mask: shifted mask defining the field's length and position
* @_val: value to put in the field
*
* FIELD_PREP() masks and shifts up the value. The result should
* be combined with other fields of the bitfield using logical OR.
*/
#define FIELD_PREP(_mask, _val) \
({ \
((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
})
/**
* FIELD_GET() - extract a bitfield element
* @_mask: shifted mask defining the field's length and position
* @_reg: value of entire bitfield
*
* FIELD_GET() extracts the field specified by @_mask from the
* bitfield passed in as @_reg by masking and shifting it down.
*/
#define FIELD_GET(_mask, _reg) \
({ \
(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
})
#endif
cflags:
{
// This driver depends on IIO
-DCONFIG_IIO
-DCONFIG_IIO_BUFFER
-DCONFIG_IIO_TRIGGERRED_BUFFER
-DDEBUG
-DREGMAP
-DREGMAP_I2C
}
sources:
{
bme680_i2c.c
bme680_core.c
}
requires:
{
kernelModules:
{
#if ${MANGOH_KERNEL_LACKS_IIO} = 1
$CURDIR/../iio/iio
#endif // MANGOH_KERNEL_LACKS_IIO
}
}
......@@ -9,16 +9,22 @@
* https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680-DS001-00.pdf
*/
#include <linux/acpi.h>
#include <linux/bitfield.h>
#include "bitfield.h"
#include <linux/device.h>
#include <linux/module.h>
#include <linux/log2.h>
#include <linux/regmap.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/version.h>
#include "bme680.h"
#define regmap_write_bits regmap_update_bits
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
#define IIO_SUPPORTS_OVERSAMPLING_RATIO
#endif
struct bme680_calib {
u16 par_t1;
s16 par_t2;
......@@ -72,18 +78,24 @@ EXPORT_SYMBOL(bme680_regmap_config);
static const struct iio_chan_spec bme680_channels[] = {
{
.type = IIO_TEMP,
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED)
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
| BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
#endif
},
{
.type = IIO_PRESSURE,
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED)
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
| BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
#endif
},
{
.type = IIO_HUMIDITYRELATIVE,
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED)
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
| BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
#endif
},
{
.type = IIO_RESISTANCE,
......@@ -771,6 +783,7 @@ static int bme680_read_raw(struct iio_dev *indio_dev,
default:
return -EINVAL;
}
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
switch (chan->type) {
case IIO_TEMP:
......@@ -785,26 +798,32 @@ static int bme680_read_raw(struct iio_dev *indio_dev,
default:
return -EINVAL;
}
#endif
default:
return -EINVAL;
}
}
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
static bool bme680_is_valid_oversampling(int rate)
{
return (rate > 0 && rate <= 16 && is_power_of_2(rate));
}
#endif
static int bme680_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
struct bme680_data *data = iio_priv(indio_dev);
#endif
if (val2 != 0)
return -EINVAL;
switch (mask) {
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
{
if (!bme680_is_valid_oversampling(val))
......@@ -826,18 +845,23 @@ static int bme680_write_raw(struct iio_dev *indio_dev,
return bme680_chip_config(data);
}
#endif
default:
return -EINVAL;
}
}
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
static const char bme680_oversampling_ratio_show[] = "1 2 4 8 16";
static IIO_CONST_ATTR(oversampling_ratio_available,
bme680_oversampling_ratio_show);
#endif
static struct attribute *bme680_attributes[] = {
#ifdef IIO_SUPPORTS_OVERSAMPLING_RATIO
&iio_const_attr_oversampling_ratio_available.dev_attr.attr,
#endif
NULL,
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment