1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ti_hdc
8 
9 #include <device.h>
10 #include <drivers/i2c.h>
11 #include <drivers/gpio.h>
12 #include <kernel.h>
13 #include <drivers/sensor.h>
14 #include <sys/util.h>
15 #include <sys/__assert.h>
16 #include <logging/log.h>
17 
18 #include "ti_hdc.h"
19 
20 LOG_MODULE_REGISTER(TI_HDC, CONFIG_SENSOR_LOG_LEVEL);
21 
22 #if DT_INST_NODE_HAS_PROP(0, drdy_gpios)
ti_hdc_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)23 static void ti_hdc_gpio_callback(const struct device *dev,
24 				  struct gpio_callback *cb, uint32_t pins)
25 {
26 	struct ti_hdc_data *drv_data =
27 		CONTAINER_OF(cb, struct ti_hdc_data, gpio_cb);
28 
29 	ARG_UNUSED(pins);
30 
31 	gpio_pin_interrupt_configure(drv_data->gpio,
32 				     DT_INST_GPIO_PIN(0, drdy_gpios),
33 				     GPIO_INT_DISABLE);
34 	k_sem_give(&drv_data->data_sem);
35 }
36 #endif
37 
ti_hdc_sample_fetch(const struct device * dev,enum sensor_channel chan)38 static int ti_hdc_sample_fetch(const struct device *dev,
39 			       enum sensor_channel chan)
40 {
41 	struct ti_hdc_data *drv_data = dev->data;
42 	uint8_t buf[4];
43 
44 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
45 
46 #if DT_INST_NODE_HAS_PROP(0, drdy_gpios)
47 	gpio_pin_interrupt_configure(drv_data->gpio,
48 				     DT_INST_GPIO_PIN(0, drdy_gpios),
49 				     GPIO_INT_EDGE_TO_ACTIVE);
50 #endif
51 
52 	buf[0] = TI_HDC_REG_TEMP;
53 	if (i2c_write(drv_data->i2c, buf, 1,
54 		      DT_INST_REG_ADDR(0)) < 0) {
55 		LOG_DBG("Failed to write address pointer");
56 		return -EIO;
57 	}
58 
59 #if DT_INST_NODE_HAS_PROP(0, drdy_gpios)
60 	k_sem_take(&drv_data->data_sem, K_FOREVER);
61 #else
62 	/* wait for the conversion to finish */
63 	k_msleep(HDC_CONVERSION_TIME);
64 #endif
65 
66 	if (i2c_read(drv_data->i2c, buf, 4, DT_INST_REG_ADDR(0)) < 0) {
67 		LOG_DBG("Failed to read sample data");
68 		return -EIO;
69 	}
70 
71 	drv_data->t_sample = (buf[0] << 8) + buf[1];
72 	drv_data->rh_sample = (buf[2] << 8) + buf[3];
73 
74 	return 0;
75 }
76 
77 
ti_hdc_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)78 static int ti_hdc_channel_get(const struct device *dev,
79 			      enum sensor_channel chan,
80 			      struct sensor_value *val)
81 {
82 	struct ti_hdc_data *drv_data = dev->data;
83 	uint64_t tmp;
84 
85 	/*
86 	 * See datasheet "Temperature Register" and "Humidity
87 	 * Register" sections for more details on processing
88 	 * sample data.
89 	 */
90 	if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
91 		/* val = -40 + 165 * sample / 2^16 */
92 		tmp = (uint64_t)drv_data->t_sample * 165U;
93 		val->val1 = (int32_t)(tmp >> 16) - 40;
94 		val->val2 = ((tmp & 0xFFFF) * 1000000U) >> 16;
95 	} else if (chan == SENSOR_CHAN_HUMIDITY) {
96 		/* val = 100 * sample / 2^16 */
97 		tmp = (uint64_t)drv_data->rh_sample * 100U;
98 		val->val1 = tmp >> 16;
99 		/* x * 1000000 / 65536 == x * 15625 / 1024 */
100 		val->val2 = ((tmp & 0xFFFF) * 15625U) >> 10;
101 	} else {
102 		return -ENOTSUP;
103 	}
104 
105 	return 0;
106 }
107 
108 static const struct sensor_driver_api ti_hdc_driver_api = {
109 	.sample_fetch = ti_hdc_sample_fetch,
110 	.channel_get = ti_hdc_channel_get,
111 };
112 
read16(const struct device * dev,uint8_t a,uint8_t d)113 static uint16_t read16(const struct device *dev, uint8_t a, uint8_t d)
114 {
115 	uint8_t buf[2];
116 	if (i2c_burst_read(dev, a, d, (uint8_t *)buf, 2) < 0) {
117 		LOG_ERR("Error reading register.");
118 	}
119 	return (buf[0] << 8 | buf[1]);
120 }
121 
ti_hdc_init(const struct device * dev)122 static int ti_hdc_init(const struct device *dev)
123 {
124 	struct ti_hdc_data *drv_data = dev->data;
125 	uint16_t tmp;
126 
127 	drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
128 
129 	if (drv_data->i2c == NULL) {
130 		LOG_DBG("Failed to get pointer to %s device!",
131 			DT_INST_BUS_LABEL(0));
132 		return -EINVAL;
133 	}
134 
135 	if (read16(drv_data->i2c, DT_INST_REG_ADDR(0),
136 		   TI_HDC_REG_MANUFID) != TI_HDC_MANUFID) {
137 		LOG_ERR("Failed to get correct manufacturer ID");
138 		return -EINVAL;
139 	}
140 	tmp = read16(drv_data->i2c, DT_INST_REG_ADDR(0),
141 		     TI_HDC_REG_DEVICEID);
142 	if (tmp != TI_HDC1000_DEVID && tmp != TI_HDC1050_DEVID) {
143 		LOG_ERR("Unsupported device ID");
144 		return -EINVAL;
145 	}
146 
147 #if DT_INST_NODE_HAS_PROP(0, drdy_gpios)
148 	k_sem_init(&drv_data->data_sem, 0, K_SEM_MAX_LIMIT);
149 
150 	/* setup data ready gpio interrupt */
151 	drv_data->gpio = device_get_binding(
152 				DT_INST_GPIO_LABEL(0, drdy_gpios));
153 	if (drv_data->gpio == NULL) {
154 		LOG_DBG("Failed to get pointer to %s device",
155 			 DT_INST_GPIO_LABEL(0, drdy_gpios));
156 		return -EINVAL;
157 	}
158 
159 	gpio_pin_configure(drv_data->gpio, DT_INST_GPIO_PIN(0, drdy_gpios),
160 			   GPIO_INPUT | DT_INST_GPIO_FLAGS(0, drdy_gpios));
161 
162 	gpio_init_callback(&drv_data->gpio_cb,
163 			   ti_hdc_gpio_callback,
164 			   BIT(DT_INST_GPIO_PIN(0, drdy_gpios)));
165 
166 	if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
167 		LOG_DBG("Failed to set GPIO callback");
168 		return -EIO;
169 	}
170 
171 	gpio_pin_interrupt_configure(drv_data->gpio,
172 				     DT_INST_GPIO_PIN(0, drdy_gpios),
173 				     GPIO_INT_EDGE_TO_ACTIVE);
174 #endif
175 
176 	LOG_INF("Initialized device successfully");
177 
178 	return 0;
179 }
180 
181 static struct ti_hdc_data ti_hdc_data;
182 
183 DEVICE_DT_INST_DEFINE(0, ti_hdc_init, NULL, &ti_hdc_data,
184 		    NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
185 		    &ti_hdc_driver_api);
186