sensor: add driver for HTS221 sensor

Add driver for HTS221 temeprature and relative humidity sensor.

Datasheet:
	http://www.st.com/resource/en/datasheet/hts221.pdf

Interpreting humidity and temperature readings document:
	http://www.st.com/resource/en/technical_note/dm00208001.pdf

Change-Id: I1fee9d8372ad71b22bc85a60939c82f81569e135
Origin: Original
Signed-off-by: Bogdan Davidoaia <bogdan.m.davidoaia@intel.com>
diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig
index 74ad53a..21c7fa6 100644
--- a/drivers/sensor/Kconfig
+++ b/drivers/sensor/Kconfig
@@ -37,6 +37,8 @@
 
 source "drivers/sensor/Kconfig.hdc1008"
 
+source "drivers/sensor/Kconfig.hts221"
+
 source "drivers/sensor/Kconfig.isl29035"
 
 source "drivers/sensor/Kconfig.lis3dh"
diff --git a/drivers/sensor/Kconfig.hts221 b/drivers/sensor/Kconfig.hts221
new file mode 100644
index 0000000..6982b12
--- /dev/null
+++ b/drivers/sensor/Kconfig.hts221
@@ -0,0 +1,134 @@
+#
+# 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.
+#
+
+menuconfig HTS221
+	bool
+	prompt "HTS221 magnetometer"
+	depends on SENSOR && I2C
+	default n
+	help
+	  Enable driver for HTS221 I2C-based magnetometer.
+
+config HTS221_SYS_LOG_LEVEL
+	int "HTS221 Log level"
+	depends on SYS_LOG && HTS221
+	default 0
+	range 0 4
+	help
+	  Sets log level for HTS221 driver.
+	  Levels are:
+	  0 OFF, do not write
+	  1 ERROR, only write SYS_LOG_ERR
+	  2 WARNING, write SYS_LOG_WRN in addition to previous level
+	  3 INFO, write SYS_LOG_INF in addition to previous levels
+	  4 DEBUG, write SYS_LOG_DBG in addition to previous levels
+
+config HTS221_NAME
+	string
+	prompt "Driver name"
+	default "HTS221"
+	depends on HTS221
+	help
+	  Device name with which the HTS221 sensor is identified.
+
+config HTS221_INIT_PRIORITY
+	int
+	prompt "Init priority"
+	depends on HTS221
+	default 70
+	help
+	  Device driver initialization priority.
+
+config HTS221_I2C_MASTER_DEV_NAME
+	string
+	prompt "I2C master where HTS221 is connected"
+	depends on HTS221
+	default "I2C_0"
+	help
+	  Specify the device name of the I2C master device to which HTS221 is
+	  connected.
+
+choice
+	prompt "Trigger mode"
+	depends on HTS221
+	default HTS221_TRIGGER_GLOBAL_FIBER
+	help
+	  Specify the type of triggering to be used by the driver.
+
+config HTS221_TRIGGER_NONE
+	bool
+	prompt "No trigger"
+
+config HTS221_TRIGGER_GLOBAL_FIBER
+	bool
+	prompt "Use global fiber"
+	depends on GPIO && SYSTEM_WORKQUEUE
+	select HTS221_TRIGGER
+
+config HTS221_TRIGGER_OWN_FIBER
+	bool
+	prompt "Use own fiber"
+	depends on GPIO
+	select HTS221_TRIGGER
+
+endchoice
+
+config HTS221_TRIGGER
+	bool
+	depends on HTS221
+
+config HTS221_GPIO_DEV_NAME
+	string
+	prompt "GPIO device"
+	default "GPIO_0"
+	depends on HTS221 && HTS221_TRIGGER
+	help
+	  The device name of the GPIO device to which the HTS221 interrupt pin
+	  is connected.
+
+config HTS221_GPIO_PIN_NUM
+	int
+	prompt "Interrupt GPIO pin number"
+	default 0
+	depends on HTS221 && HTS221_TRIGGER
+	help
+	  The number of the GPIO on which the interrupt signal from the HTS221
+	  chip will be received.
+
+config HTS221_FIBER_PRIORITY
+	int
+	prompt "Fiber priority"
+	depends on HTS221 && HTS221_TRIGGER_OWN_FIBER
+	default 10
+	help
+	  Priority of fiber used by the driver to handle interrupts.
+
+config HTS221_FIBER_STACK_SIZE
+	int
+	prompt "Fiber stack size"
+	depends on HTS221 && HTS221_TRIGGER_OWN_FIBER
+	default 1024
+	help
+	  Stack size of fiber used by the driver to handle interrupts.
+
+config HTS221_ODR
+	string
+	prompt "Output data rate"
+	depends on HTS221
+	default "1"
+	help
+	  Sensor output data rate expressed in samples per second.
+	  Data rates supported by the chip are 1, 7 and 12.5.
diff --git a/drivers/sensor/Makefile b/drivers/sensor/Makefile
index 358339b..f85c064 100644
--- a/drivers/sensor/Makefile
+++ b/drivers/sensor/Makefile
@@ -11,6 +11,8 @@
 obj-$(CONFIG_BMI160_TRIGGER) += sensor_bmi160_trigger.o
 obj-$(CONFIG_DHT) += sensor_dht.o
 obj-$(CONFIG_HDC1008) += sensor_hdc1008.o
+obj-$(CONFIG_HTS221) += sensor_hts221.o
+obj-$(CONFIG_HTS221_TRIGGER) += sensor_hts221_trigger.o
 obj-$(CONFIG_ISL29035) += sensor_isl29035.o
 obj-$(CONFIG_ISL29035_TRIGGER) += sensor_isl29035_trigger.o
 obj-$(CONFIG_LIS3DH) += sensor_lis3dh.o
diff --git a/drivers/sensor/sensor_hts221.c b/drivers/sensor/sensor_hts221.c
new file mode 100644
index 0000000..c3aef83
--- /dev/null
+++ b/drivers/sensor/sensor_hts221.c
@@ -0,0 +1,179 @@
+/*
+ * 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.
+ */
+
+#include <i2c.h>
+#include <init.h>
+#include <misc/__assert.h>
+#include <misc/byteorder.h>
+#include <sensor.h>
+#include <string.h>
+
+#include "sensor_hts221.h"
+
+static int hts221_channel_get(struct device *dev,
+			       enum sensor_channel chan,
+			       struct sensor_value *val)
+{
+	struct hts221_data *drv_data = dev->driver_data;
+	int32_t conv_val;
+
+	__ASSERT(chan == SENSOR_CHAN_TEMP || SENSOR_CHAN_HUMIDITY);
+
+	/*
+	 * see "Interpreting humidity and temperature readings" document
+	 * for more details
+	 */
+	if (chan == SENSOR_CHAN_TEMP) {
+		conv_val = (int32_t)(drv_data->t1_degc_x8 -
+				     drv_data->t0_degc_x8) *
+			   (drv_data->t_sample - drv_data->t0_out) /
+			   (drv_data->t1_out - drv_data->t0_out) +
+			   drv_data->t0_degc_x8;
+
+		/* convert temperature x8 to degrees Celsius */
+		val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
+		val->val1 = conv_val / 8;
+		val->val2 = (conv_val % 8) * (1000000 / 8);
+	} else { /* SENSOR_CHAN_HUMIDITY */
+		conv_val = (int32_t)(drv_data->h1_rh_x2 - drv_data->h0_rh_x2) *
+			   (drv_data->rh_sample - drv_data->h0_t0_out) /
+			   (drv_data->h1_t0_out - drv_data->h0_t0_out) +
+			   drv_data->h0_rh_x2;
+
+		/* convert humidity x2 to mili-percent */
+		val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
+		val->val1 = conv_val * 500;
+		val->val2 = 0;
+	}
+
+	return 0;
+}
+
+static int hts221_sample_fetch(struct device *dev, enum sensor_channel chan)
+{
+	struct hts221_data *drv_data = dev->driver_data;
+	uint8_t buf[4];
+
+	__ASSERT(chan == SENSOR_CHAN_ALL);
+
+	if (i2c_burst_read(drv_data->i2c, HTS221_I2C_ADDR,
+			   HTS221_REG_DATA_START | HTS221_AUTOINCREMENT_ADDR,
+			   buf, 4) < 0) {
+		SYS_LOG_ERR("Failed to fetch data sample.");
+		return -EIO;
+	}
+
+	drv_data->rh_sample = sys_le16_to_cpu(buf[0] | (buf[1] << 8));
+	drv_data->t_sample = sys_le16_to_cpu(buf[2] | (buf[3] << 8));
+
+	return 0;
+}
+
+static int hts221_read_conversion_data(struct hts221_data *drv_data)
+{
+	uint8_t buf[16];
+
+	if (i2c_burst_read(drv_data->i2c, HTS221_I2C_ADDR,
+			   HTS221_REG_CONVERSION_START |
+			   HTS221_AUTOINCREMENT_ADDR, buf, 16) < 0) {
+		SYS_LOG_ERR("Failed to read conversion data.");
+		return -EIO;
+	}
+
+	drv_data->h0_rh_x2 = buf[0];
+	drv_data->h1_rh_x2 = buf[1];
+	drv_data->t0_degc_x8 = sys_le16_to_cpu(buf[2] | ((buf[5] & 0x3) << 8));
+	drv_data->t1_degc_x8 = sys_le16_to_cpu(buf[3] | ((buf[5] & 0xC) << 6));
+	drv_data->h0_t0_out = sys_le16_to_cpu(buf[6] | (buf[7] << 8));
+	drv_data->h1_t0_out = sys_le16_to_cpu(buf[10] | (buf[11] << 8));
+	drv_data->t0_out = sys_le16_to_cpu(buf[12] | (buf[13] << 8));
+	drv_data->t1_out = sys_le16_to_cpu(buf[14] | (buf[15] << 8));
+
+	return 0;
+}
+
+static struct sensor_driver_api hts221_driver_api = {
+#if CONFIG_HTS221_TRIGGER
+	.trigger_set = hts221_trigger_set,
+#endif
+	.sample_fetch = hts221_sample_fetch,
+	.channel_get = hts221_channel_get,
+};
+
+int hts221_init(struct device *dev)
+{
+	struct hts221_data *drv_data = dev->driver_data;
+	uint8_t id, idx;
+
+	drv_data->i2c = device_get_binding(CONFIG_HTS221_I2C_MASTER_DEV_NAME);
+	if (drv_data->i2c == NULL) {
+		SYS_LOG_ERR("Could not get pointer to %s device.",
+			    CONFIG_HTS221_I2C_MASTER_DEV_NAME);
+		return -EINVAL;
+	}
+
+	/* check chip ID */
+	if (i2c_reg_read_byte(drv_data->i2c, HTS221_I2C_ADDR,
+			      HTS221_REG_WHO_AM_I, &id) < 0) {
+		SYS_LOG_ERR("Failed to read chip ID.");
+		return -EIO;
+	}
+
+	if (id != HTS221_CHIP_ID) {
+		SYS_LOG_ERR("Invalid chip ID.");
+		return -EINVAL;
+	}
+
+	/* check if CONFIG_HTS221_ODR is valid */
+	for (idx = 0; idx < ARRAY_SIZE(hts221_odr_strings); idx++) {
+		if (!strcmp(hts221_odr_strings[idx], CONFIG_HTS221_ODR)) {
+			break;
+		}
+	}
+
+	if (idx == ARRAY_SIZE(hts221_odr_strings)) {
+		SYS_LOG_ERR("Invalid ODR value.");
+		return -EINVAL;
+	}
+
+	if (i2c_reg_write_byte(drv_data->i2c, HTS221_I2C_ADDR, HTS221_REG_CTRL1,
+			       (idx + 1) << HTS221_ODR_SHIFT | HTS221_BDU_BIT |
+			       HTS221_PD_BIT) < 0) {
+		SYS_LOG_ERR("Failed to configure chip.");
+		return -EIO;
+	}
+
+	if (hts221_read_conversion_data(drv_data) < 0) {
+		SYS_LOG_ERR("Failed to read conversion data.");
+		return -EINVAL;
+	}
+
+#ifdef CONFIG_HTS221_TRIGGER
+	if (hts221_init_interrupt(dev) < 0) {
+		SYS_LOG_ERR("Failed to initialize interrupt.");
+		return -EIO;
+	}
+#endif
+
+	dev->driver_api = &hts221_driver_api;
+
+	return 0;
+}
+
+struct hts221_data hts221_driver;
+
+DEVICE_INIT(hts221, CONFIG_HTS221_NAME, hts221_init, &hts221_driver,
+	    NULL, SECONDARY, CONFIG_HTS221_INIT_PRIORITY);
diff --git a/drivers/sensor/sensor_hts221.h b/drivers/sensor/sensor_hts221.h
new file mode 100644
index 0000000..712cde3
--- /dev/null
+++ b/drivers/sensor/sensor_hts221.h
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+#ifndef __SENSOR_HTS221_H__
+#define __SENSOR_HTS221_H__
+
+#include <device.h>
+#include <misc/util.h>
+#include <stdint.h>
+#include <gpio.h>
+#include <misc/nano_work.h>
+
+#define SYS_LOG_DOMAIN "HTS221"
+#define SYS_LOG_LEVEL CONFIG_HTS221_SYS_LOG_LEVEL
+#include <misc/sys_log.h>
+
+#define HTS221_I2C_ADDR			0x5F
+#define HTS221_AUTOINCREMENT_ADDR	BIT(7)
+
+#define HTS221_REG_WHO_AM_I		0x0F
+#define HTS221_CHIP_ID			0xBC
+
+#define HTS221_REG_CTRL1		0x20
+#define HTS221_PD_BIT			BIT(7)
+#define HTS221_BDU_BIT			BIT(3)
+#define HTS221_ODR_SHIFT		0
+
+#define HTS221_REG_CTRL3		0x22
+#define HTS221_DRDY_EN			BIT(2)
+
+#define HTS221_REG_DATA_START		0x28
+#define HTS221_REG_CONVERSION_START	0x30
+
+static const char * const hts221_odr_strings[] = {
+	"1", "7", "12.5"
+};
+
+struct hts221_data {
+	struct device *i2c;
+	int16_t rh_sample;
+	int16_t t_sample;
+
+	uint8_t h0_rh_x2;
+	uint8_t h1_rh_x2;
+	uint16_t t0_degc_x8;
+	uint16_t t1_degc_x8;
+	int16_t h0_t0_out;
+	int16_t h1_t0_out;
+	int16_t t0_out;
+	int16_t t1_out;
+
+#ifdef CONFIG_HTS221_TRIGGER
+	struct device *gpio;
+	struct gpio_callback gpio_cb;
+
+	struct sensor_trigger data_ready_trigger;
+	sensor_trigger_handler_t data_ready_handler;
+
+#if defined(CONFIG_HTS221_TRIGGER_OWN_FIBER)
+	char __stack fiber_stack[CONFIG_HTS221_FIBER_STACK_SIZE];
+	struct nano_sem gpio_sem;
+#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_FIBER)
+	struct nano_work work;
+	struct device *dev;
+#endif
+
+#endif /* CONFIG_HTS221_TRIGGER */
+};
+
+#ifdef CONFIG_HTS221_TRIGGER
+int hts221_trigger_set(struct device *dev,
+			const struct sensor_trigger *trig,
+			sensor_trigger_handler_t handler);
+
+int hts221_init_interrupt(struct device *dev);
+#endif
+
+#endif /* __SENSOR_HTS221__ */
diff --git a/drivers/sensor/sensor_hts221_trigger.c b/drivers/sensor/sensor_hts221_trigger.c
new file mode 100644
index 0000000..6077e2c
--- /dev/null
+++ b/drivers/sensor/sensor_hts221_trigger.c
@@ -0,0 +1,149 @@
+/*
+ * 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.
+ */
+
+#include <device.h>
+#include <i2c.h>
+#include <misc/__assert.h>
+#include <misc/util.h>
+#include <nanokernel.h>
+#include <sensor.h>
+
+#include "sensor_hts221.h"
+
+int hts221_trigger_set(struct device *dev,
+			const struct sensor_trigger *trig,
+			sensor_trigger_handler_t handler)
+{
+	struct hts221_data *drv_data = dev->driver_data;
+
+	__ASSERT(trig->type == SENSOR_TRIG_DATA_READY);
+
+	gpio_pin_disable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
+
+	drv_data->data_ready_handler = handler;
+	if (handler == NULL) {
+		return 0;
+	}
+
+	drv_data->data_ready_trigger = *trig;
+
+	gpio_pin_enable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
+
+	return 0;
+}
+
+static void hts221_gpio_callback(struct device *dev,
+				  struct gpio_callback *cb, uint32_t pins)
+{
+	struct hts221_data *drv_data =
+		CONTAINER_OF(cb, struct hts221_data, gpio_cb);
+
+	ARG_UNUSED(pins);
+
+	gpio_pin_disable_callback(dev, CONFIG_HTS221_GPIO_PIN_NUM);
+
+#if defined(CONFIG_HTS221_TRIGGER_OWN_FIBER)
+	nano_sem_give(&drv_data->gpio_sem);
+#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_FIBER)
+	nano_work_submit(&drv_data->work);
+#endif
+}
+
+static void hts221_fiber_cb(void *arg)
+{
+	struct device *dev = arg;
+	struct hts221_data *drv_data = dev->driver_data;
+
+	if (drv_data->data_ready_handler != NULL) {
+		drv_data->data_ready_handler(dev,
+					     &drv_data->data_ready_trigger);
+	}
+
+	gpio_pin_enable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
+}
+
+#ifdef CONFIG_HTS221_TRIGGER_OWN_FIBER
+static void hts221_fiber(int dev_ptr, int unused)
+{
+	struct device *dev = INT_TO_POINTER(dev_ptr);
+	struct hts221_data *drv_data = dev->driver_data;
+
+	ARG_UNUSED(unused);
+
+	while (1) {
+		nano_fiber_sem_take(&drv_data->gpio_sem, TICKS_UNLIMITED);
+		hts221_fiber_cb(dev);
+	}
+}
+#endif
+
+#ifdef CONFIG_HTS221_TRIGGER_GLOBAL_FIBER
+static void hts221_work_cb(struct nano_work *work)
+{
+	struct hts221_data *drv_data =
+		CONTAINER_OF(work, struct hts221_data, work);
+
+	hts221_fiber_cb(drv_data->dev);
+}
+#endif
+
+int hts221_init_interrupt(struct device *dev)
+{
+	struct hts221_data *drv_data = dev->driver_data;
+
+	/* setup data ready gpio interrupt */
+	drv_data->gpio = device_get_binding(CONFIG_HTS221_GPIO_DEV_NAME);
+	if (drv_data->gpio == NULL) {
+		SYS_LOG_ERR("Cannot get pointer to %s device.",
+			    CONFIG_HTS221_GPIO_DEV_NAME);
+		return -EINVAL;
+	}
+
+	gpio_pin_configure(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM,
+			   GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
+			   GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE);
+
+	gpio_init_callback(&drv_data->gpio_cb,
+			   hts221_gpio_callback,
+			   BIT(CONFIG_HTS221_GPIO_PIN_NUM));
+
+	if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
+		SYS_LOG_ERR("Could not set gpio callback.");
+		return -EIO;
+	}
+
+	/* enable data-ready interrupt */
+	if (i2c_reg_write_byte(drv_data->i2c, HTS221_I2C_ADDR,
+			       HTS221_REG_CTRL3, HTS221_DRDY_EN) < 0) {
+		SYS_LOG_ERR("Could not enable data-ready interrupt.");
+		return -EIO;
+	}
+
+#if defined(CONFIG_HTS221_TRIGGER_OWN_FIBER)
+	nano_sem_init(&drv_data->gpio_sem);
+
+	fiber_start(drv_data->fiber_stack, CONFIG_HTS221_FIBER_STACK_SIZE,
+		    (nano_fiber_entry_t)hts221_fiber, POINTER_TO_INT(dev),
+		    0, CONFIG_HTS221_FIBER_PRIORITY, 0);
+#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_FIBER)
+	drv_data->work.handler = hts221_work_cb;
+	drv_data->dev = dev;
+#endif
+
+	gpio_pin_enable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
+
+	return 0;
+}