1 /* ST Microelectronics IIS2DH 3-axis accelerometer driver
2  *
3  * Copyright (c) 2020 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/iis2dh.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_iis2dh
12 
13 #include <zephyr/init.h>
14 #include <zephyr/sys/__assert.h>
15 #include <zephyr/sys/util_macro.h>
16 #include <zephyr/logging/log.h>
17 #include <zephyr/drivers/sensor.h>
18 
19 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
20 #include <zephyr/drivers/spi.h>
21 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
22 #include <zephyr/drivers/i2c.h>
23 #endif
24 
25 #include "iis2dh.h"
26 
27 LOG_MODULE_REGISTER(IIS2DH, CONFIG_SENSOR_LOG_LEVEL);
28 
29 /* gains in uG/LSB */
30 static const uint32_t iis2dh_gain[3][4] = {
31 	{
32 		/* HR mode */
33 		980/16,		/* 2G */
34 		1950/16,	/* 4G */
35 		3910/16,	/* 8G */
36 		11720/16,	/* 16G */
37 	},
38 	{
39 		/* NM mode */
40 		3910/64,	/* 2G */
41 		7810/64,	/* 4G */
42 		15630/64,	/* 8G */
43 		46950/64,	/* 16G */
44 	},
45 	{
46 		/* LP mode */
47 		15630/256,	/* 2G */
48 		31250/256,	/* 4G */
49 		62500/256,	/* 8G */
50 		188680/256,	/* 16G */
51 	},
52 };
53 
iis2dh_set_fs_raw(const struct device * dev,uint8_t fs)54 static int iis2dh_set_fs_raw(const struct device *dev, uint8_t fs)
55 {
56 	struct iis2dh_data *iis2dh = dev->data;
57 	int err;
58 
59 	err = iis2dh_full_scale_set(iis2dh->ctx, fs);
60 
61 	if (!err) {
62 		/* save internally gain for optimization */
63 		iis2dh->gain = iis2dh_gain[IIS2DH_HR_12bit][fs];
64 	}
65 
66 	return err;
67 }
68 
69 #if (CONFIG_IIS2DH_RANGE == 0)
70 /**
71  * iis2dh_set_range - set full scale range for acc
72  * @dev: Pointer to instance of struct device (I2C or SPI)
73  * @range: Full scale range (2, 4, 8 and 16 G)
74  */
iis2dh_set_range(const struct device * dev,uint16_t range)75 static int iis2dh_set_range(const struct device *dev, uint16_t range)
76 {
77 	int err;
78 	uint8_t fs = IIS2DH_FS_TO_REG(range);
79 
80 	err = iis2dh_set_fs_raw(dev, fs);
81 
82 	return err;
83 }
84 #endif
85 
86 #if (CONFIG_IIS2DH_ODR == 0)
87 /**
88  * iis2dh_set_odr - set new sampling frequency
89  * @dev: Pointer to instance of struct device (I2C or SPI)
90  * @odr: Output data rate
91  */
iis2dh_set_odr(const struct device * dev,uint16_t odr)92 static int iis2dh_set_odr(const struct device *dev, uint16_t odr)
93 {
94 	struct iis2dh_data *iis2dh = dev->data;
95 	const struct iis2dh_device_config *cfg = dev->config;
96 	iis2dh_odr_t val;
97 
98 	val = IIS2DH_ODR_TO_REG_HR(cfg->pm, odr);
99 
100 	return iis2dh_data_rate_set(iis2dh->ctx, val);
101 }
102 #endif
103 
iis2dh_convert(struct sensor_value * val,int raw_val,uint32_t gain)104 static inline void iis2dh_convert(struct sensor_value *val, int raw_val,
105 				  uint32_t gain)
106 {
107 	int64_t dval;
108 
109 	/* Gain is in ug/LSB */
110 	/* Convert to m/s^2 */
111 	dval = ((int64_t)raw_val * gain * SENSOR_G) / 1000000LL;
112 	val->val1 = dval / 1000000LL;
113 	val->val2 = dval % 1000000LL;
114 }
115 
iis2dh_channel_get_acc(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)116 static inline void iis2dh_channel_get_acc(const struct device *dev,
117 					  enum sensor_channel chan,
118 					  struct sensor_value *val)
119 {
120 	int i;
121 	uint8_t ofs_start, ofs_stop;
122 	struct iis2dh_data *iis2dh = dev->data;
123 	struct sensor_value *pval = val;
124 
125 	switch (chan) {
126 	case SENSOR_CHAN_ACCEL_X:
127 		ofs_start = ofs_stop = 0U;
128 		break;
129 	case SENSOR_CHAN_ACCEL_Y:
130 		ofs_start = ofs_stop = 1U;
131 		break;
132 	case SENSOR_CHAN_ACCEL_Z:
133 		ofs_start = ofs_stop = 2U;
134 		break;
135 	default:
136 		ofs_start = 0U; ofs_stop = 2U;
137 		break;
138 	}
139 
140 	for (i = ofs_start; i <= ofs_stop ; i++) {
141 		iis2dh_convert(pval++, iis2dh->acc[i], iis2dh->gain);
142 	}
143 }
144 
iis2dh_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)145 static int iis2dh_channel_get(const struct device *dev,
146 			      enum sensor_channel chan,
147 			      struct sensor_value *val)
148 {
149 	switch (chan) {
150 	case SENSOR_CHAN_ACCEL_X:
151 	case SENSOR_CHAN_ACCEL_Y:
152 	case SENSOR_CHAN_ACCEL_Z:
153 	case SENSOR_CHAN_ACCEL_XYZ:
154 		iis2dh_channel_get_acc(dev, chan, val);
155 		return 0;
156 	default:
157 		LOG_DBG("Channel not supported");
158 		break;
159 	}
160 
161 	return -ENOTSUP;
162 }
163 
iis2dh_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)164 static int iis2dh_config(const struct device *dev, enum sensor_channel chan,
165 			 enum sensor_attribute attr,
166 			 const struct sensor_value *val)
167 {
168 	switch (attr) {
169 #if (CONFIG_IIS2DH_RANGE == 0)
170 	case SENSOR_ATTR_FULL_SCALE:
171 		return iis2dh_set_range(dev, sensor_ms2_to_g(val));
172 #endif
173 #if (CONFIG_IIS2DH_ODR == 0)
174 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
175 		return iis2dh_set_odr(dev, val->val1);
176 #endif
177 	default:
178 		LOG_DBG("Acc attribute not supported");
179 		break;
180 	}
181 
182 	return -ENOTSUP;
183 }
184 
iis2dh_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)185 static int iis2dh_attr_set(const struct device *dev, enum sensor_channel chan,
186 			   enum sensor_attribute attr,
187 			   const struct sensor_value *val)
188 {
189 	switch (chan) {
190 	case SENSOR_CHAN_ACCEL_X:
191 	case SENSOR_CHAN_ACCEL_Y:
192 	case SENSOR_CHAN_ACCEL_Z:
193 	case SENSOR_CHAN_ACCEL_XYZ:
194 		return iis2dh_config(dev, chan, attr, val);
195 	default:
196 		LOG_DBG("Attr not supported on %d channel", chan);
197 		break;
198 	}
199 
200 	return -ENOTSUP;
201 }
202 
iis2dh_sample_fetch(const struct device * dev,enum sensor_channel chan)203 static int iis2dh_sample_fetch(const struct device *dev,
204 			       enum sensor_channel chan)
205 {
206 	struct iis2dh_data *iis2dh = dev->data;
207 	int16_t buf[3];
208 
209 	/* fetch raw data sample */
210 	if (iis2dh_acceleration_raw_get(iis2dh->ctx, buf) < 0) {
211 		LOG_DBG("Failed to fetch raw data sample");
212 		return -EIO;
213 	}
214 
215 	iis2dh->acc[0] = buf[0];
216 	iis2dh->acc[1] = buf[1];
217 	iis2dh->acc[2] = buf[2];
218 
219 	return 0;
220 }
221 
222 static DEVICE_API(sensor, iis2dh_driver_api) = {
223 	.attr_set = iis2dh_attr_set,
224 #if CONFIG_IIS2DH_TRIGGER
225 	.trigger_set = iis2dh_trigger_set,
226 #endif /* CONFIG_IIS2DH_TRIGGER */
227 	.sample_fetch = iis2dh_sample_fetch,
228 	.channel_get = iis2dh_channel_get,
229 };
230 
iis2dh_init_interface(const struct device * dev)231 static int iis2dh_init_interface(const struct device *dev)
232 {
233 	int res;
234 
235 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
236 	res = iis2dh_spi_init(dev);
237 	if (res) {
238 		return res;
239 	}
240 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
241 	res = iis2dh_i2c_init(dev);
242 	if (res) {
243 		return res;
244 	}
245 #else
246 #error "BUS MACRO NOT DEFINED IN DTS"
247 #endif
248 
249 	return 0;
250 }
251 
iis2dh_init(const struct device * dev)252 static int iis2dh_init(const struct device *dev)
253 {
254 	struct iis2dh_data *iis2dh = dev->data;
255 	const struct iis2dh_device_config *cfg = dev->config;
256 	uint8_t wai;
257 
258 	if (iis2dh_init_interface(dev)) {
259 		return -EINVAL;
260 	}
261 
262 	/* check chip ID */
263 	if (iis2dh_device_id_get(iis2dh->ctx, &wai) < 0) {
264 		return -EIO;
265 	}
266 
267 	if (wai != IIS2DH_ID) {
268 		LOG_ERR("Invalid chip ID: %02x", wai);
269 		return -EINVAL;
270 	}
271 
272 	if (iis2dh_block_data_update_set(iis2dh->ctx, PROPERTY_ENABLE) < 0) {
273 		return -EIO;
274 	}
275 
276 	if (iis2dh_operating_mode_set(iis2dh->ctx, cfg->pm)) {
277 		return -EIO;
278 	}
279 
280 #if (CONFIG_IIS2DH_ODR != 0)
281 	/* set default odr and full scale for acc */
282 	if (iis2dh_data_rate_set(iis2dh->ctx, CONFIG_IIS2DH_ODR) < 0) {
283 		return -EIO;
284 	}
285 #endif
286 
287 #if (CONFIG_IIS2DH_RANGE != 0)
288 	iis2dh_set_fs_raw(dev, CONFIG_IIS2DH_RANGE);
289 #endif
290 
291 #ifdef CONFIG_IIS2DH_TRIGGER
292 	if (cfg->int_gpio.port) {
293 		if (iis2dh_init_interrupt(dev) < 0) {
294 			LOG_ERR("Failed to initialize interrupts");
295 			return -EIO;
296 		}
297 	}
298 #endif /* CONFIG_IIS2DH_TRIGGER */
299 
300 	return 0;
301 }
302 
303 #define IIS2DH_SPI(inst)                                                                           \
304 	(.spi = SPI_DT_SPEC_INST_GET(                                                              \
305 		 0, SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8), 0),)
306 
307 #define IIS2DH_I2C(inst) (.i2c = I2C_DT_SPEC_INST_GET(inst),)
308 
309 #define IIS2DH_DEFINE(inst)									\
310 	static struct iis2dh_data iis2dh_data_##inst;						\
311 												\
312 	static const struct iis2dh_device_config iis2dh_device_config_##inst = {		\
313 		COND_CODE_1(DT_INST_ON_BUS(inst, i2c), IIS2DH_I2C(inst), ())			\
314 		COND_CODE_1(DT_INST_ON_BUS(inst, spi), IIS2DH_SPI(inst), ())			\
315 		.pm = CONFIG_IIS2DH_POWER_MODE,							\
316 		IF_ENABLED(CONFIG_IIS2DH_TRIGGER,						\
317 			   (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, drdy_gpios, { 0 }),))	\
318 	};											\
319 												\
320 	SENSOR_DEVICE_DT_INST_DEFINE(inst, iis2dh_init, NULL,					\
321 			      &iis2dh_data_##inst, &iis2dh_device_config_##inst, POST_KERNEL,	\
322 			      CONFIG_SENSOR_INIT_PRIORITY, &iis2dh_driver_api);			\
323 
324 DT_INST_FOREACH_STATUS_OKAY(IIS2DH_DEFINE)
325