[PATCH] iio: Add TH06 Humidity and Temperature sensor driver
Daniel Baluta
daniel.baluta at gmail.com
Sun Jan 10 15:47:32 EET 2016
On Sun, Jan 10, 2016 at 12:39 PM, Cristina Moraru
<cristina.moraru09 at gmail.com> wrote:
> From: Cristina Moraru <cristina9209 at yahoo.com>
>
> Add implementation for Hoperf TH06 Humidity and Temperature
> sensor driver.
>
> Signed-off-by: Cristina Moraru <cristina9209 at yahoo.com>
> ---
> drivers/iio/humidity/Kconfig | 10 +++
> drivers/iio/humidity/Makefile | 1 +
> drivers/iio/humidity/th06.c | 148 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 159 insertions(+)
> create mode 100644 drivers/iio/humidity/th06.c
>
> diff --git a/drivers/iio/humidity/Kconfig b/drivers/iio/humidity/Kconfig
> index 6a23698..b233b16 100644
> --- a/drivers/iio/humidity/Kconfig
> +++ b/drivers/iio/humidity/Kconfig
> @@ -55,4 +55,14 @@ config SI7020
> To compile this driver as a module, choose M here: the module
> will be called si7020.
>
> +config TH06
> + tristate "TH06 humidity and temperature sensor"
> + depends on I2C
> + help
> + Say yes here to build support for the TH06 measurement of
> + relative humidity and temperature.
> +
> + To compile this driver as a module, choose M here: the module
> + will be called th06.
> +
> endmenu
> diff --git a/drivers/iio/humidity/Makefile b/drivers/iio/humidity/Makefile
> index c9f089a..3667ee7 100644
> --- a/drivers/iio/humidity/Makefile
> +++ b/drivers/iio/humidity/Makefile
> @@ -7,3 +7,4 @@ obj-$(CONFIG_HDC100X) += hdc100x.o
> obj-$(CONFIG_HTU21) += htu21.o
> obj-$(CONFIG_SI7005) += si7005.o
> obj-$(CONFIG_SI7020) += si7020.o
> +obj-$(CONFIG_TH06) += th06.o
> diff --git a/drivers/iio/humidity/th06.c b/drivers/iio/humidity/th06.c
> new file mode 100644
> index 0000000..ead03326
> --- /dev/null
> +++ b/drivers/iio/humidity/th06.c
> @@ -0,0 +1,148 @@
> +/*
> + * TH06 - I2C Humidity and Temperature Sensor
> + */
> +
> +#include <linux/module.h>
> +#include <linux/init.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +
> +#define TH06_DRV_NAME "th06"
> +
> +#define TH06_READ_RH_MM 0xE5
> +#define TH06_READ_RH 0xF5
Remove this as it is not used. Also, I think to avoid confusion
we should just have TH06_READ_RH 0xE5
> +#define TH06_READ_TEMP_MM 0xE3
> +#define TH06_READ_TEMP 0xF3
The same here.
> +#define TH06_READ_RH_FROM_TEMP 0xE0
> +#define TH06_RESET 0xFE
> +#define TH06_WRITE_USER_REG 0xE6
> +#define TH06_READ_USER_REG 0xE7
> +
> +#define TH06_SLAVE_ADDR 0x40
This is not used. Please make sure we don't define unused macros.
> +
> +
> +struct th06_data {
> + struct i2c_client *client;
> +};
> +
We should directly used client, it doesn't make much sense to have
a structure for this.
> +static const struct iio_chan_spec th06_channels[] = {
> + {
> + .type = IIO_HUMIDITYRELATIVE,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE) |
> + BIT(IIO_CHAN_INFO_OFFSET),
> + },
> + {
> + .type = IIO_TEMP,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE) |
> + BIT(IIO_CHAN_INFO_OFFSET),
> + }
> +};
> +
This looks good.
> +static int th06_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan, int *val,
> + int *val2, long mask)
> +{
> + struct th06_data *data = iio_priv(indio_dev);
> + int ret = -1;
Do we need to initialize this here? Also -1, doesn't say much.
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + switch (chan->type) {
> + case IIO_HUMIDITYRELATIVE:
> + ret = i2c_smbus_read_word_swapped(data->client,
> + TH06_READ_RH_MM);
> + break;
> + case IIO_TEMP:
> + ret = i2c_smbus_read_word_swapped(data->client,
> + TH06_READ_TEMP_MM);
> + break;
> + default:
> + break;
> + }
> + if (ret < 0)
> + return ret;
> + *val = ret;
> + return IIO_VAL_INT;
> + case IIO_CHAN_INFO_SCALE:
> + switch (chan->type) {
> + case IIO_HUMIDITYRELATIVE:
> + *val = 0;
> + *val2 = 1907349;
> + break;
> + case IIO_TEMP:
> + *val = 0;
> + *val2 = 2681274;
> + break;
> + default:
> + break;
> + }
> + return IIO_VAL_INT_PLUS_NANO;
> + case IIO_CHAN_INFO_OFFSET:
> + switch (chan->type) {
> + case IIO_HUMIDITYRELATIVE:
> + *val = -6;
> + return IIO_VAL_INT;
> + case IIO_TEMP:
> + *val = -46;
> + *val2 = 850000;
> + return IIO_VAL_INT_PLUS_MICRO;
> + default:
> + break;
> + }
> + }
> + return -EINVAL;
> +}
> +
> +
> +static const struct iio_info th06_info = {
> + .driver_module = THIS_MODULE,
> + .read_raw = th06_read_raw,
> +};
> +
> +static int th06_probe(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + struct th06_data *data;
> + struct iio_dev *indio_dev;
> +
> + 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;
> +
> + indio_dev->dev.parent = &client->dev;
> + indio_dev->info = &th06_info;
> + indio_dev->name = TH06_DRV_NAME;
> + indio_dev->channels = th06_channels;
> + indio_dev->num_channels = ARRAY_SIZE(th06_channels);
> + indio_dev->modes = INDIO_DIRECT_MODE;
> +
> + return devm_iio_device_register(&client->dev, indio_dev);
> +}
> +
> +static const struct i2c_device_id th06_id[] = {
> + {"th06", 0},
> + {}
> +};
> +MODULE_DEVICE_TABLE(i2c, th06_id);
> +
> +static struct i2c_driver th06_driver = {
> + .driver = {
> + .name = TH06_DRV_NAME,
> + },
> + .probe = th06_probe,
> + .id_table = th06_id,
> +};
> +
> +module_i2c_driver(th06_driver);
> +
> +MODULE_AUTHOR("Cristina Moraru <cristina.moraru09 at gmail.com>");
> +MODULE_DESCRIPTION("TH06 Humidity and Temperature Sensor");
> +MODULE_LICENSE("GPL");
> +
Driver looks pretty neat to me. Thanks Cristina!
Daniel.
More information about the firefly
mailing list