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 <init.h>
14 #include <sys/__assert.h>
15 #include <sys/byteorder.h>
16 #include <logging/log.h>
17 #include <drivers/sensor.h>
18 
19 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
20 #include <drivers/spi.h>
21 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
22 #include <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(struct iis2dh_data * iis2dh,uint8_t fs)54 static int iis2dh_set_fs_raw(struct iis2dh_data *iis2dh, uint8_t fs)
55 {
56 	int err;
57 
58 	err = iis2dh_full_scale_set(iis2dh->ctx, fs);
59 
60 	if (!err) {
61 		/* save internally gain for optimization */
62 		iis2dh->gain = iis2dh_gain[IIS2DH_HR_12bit][fs];
63 	}
64 
65 	return err;
66 }
67 
68 #if (CONFIG_IIS2DH_RANGE == 0)
69 /**
70  * iis2dh_set_range - set full scale range for acc
71  * @dev: Pointer to instance of struct device (I2C or SPI)
72  * @range: Full scale range (2, 4, 8 and 16 G)
73  */
iis2dh_set_range(const struct device * dev,uint16_t range)74 static int iis2dh_set_range(const struct device *dev, uint16_t range)
75 {
76 	int err;
77 	struct iis2dh_data *iis2dh = dev->data;
78 	uint8_t fs = IIS2DH_FS_TO_REG(range);
79 
80 	err = iis2dh_set_fs_raw(iis2dh, 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] = sys_le16_to_cpu(buf[0]);
216 	iis2dh->acc[1] = sys_le16_to_cpu(buf[1]);
217 	iis2dh->acc[2] = sys_le16_to_cpu(buf[2]);
218 
219 	return 0;
220 }
221 
222 static const struct sensor_driver_api 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 	struct iis2dh_data *iis2dh = dev->data;
234 	const struct iis2dh_device_config *cfg = dev->config;
235 
236 	iis2dh->bus = device_get_binding(cfg->bus_name);
237 	if (!iis2dh->bus) {
238 		LOG_DBG("master bus not found: %s", cfg->bus_name);
239 		return -EINVAL;
240 	}
241 
242 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
243 	iis2dh_spi_init(dev);
244 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
245 	iis2dh_i2c_init(dev);
246 #else
247 #error "BUS MACRO NOT DEFINED IN DTS"
248 #endif
249 
250 	return 0;
251 }
252 
iis2dh_init(const struct device * dev)253 static int iis2dh_init(const struct device *dev)
254 {
255 	struct iis2dh_data *iis2dh = dev->data;
256 	const struct iis2dh_device_config *cfg = dev->config;
257 	uint8_t wai;
258 
259 	if (iis2dh_init_interface(dev)) {
260 		return -EINVAL;
261 	}
262 
263 	/* check chip ID */
264 	if (iis2dh_device_id_get(iis2dh->ctx, &wai) < 0) {
265 		return -EIO;
266 	}
267 
268 	if (wai != IIS2DH_ID) {
269 		LOG_ERR("Invalid chip ID: %02x", wai);
270 		return -EINVAL;
271 	}
272 
273 	if (iis2dh_block_data_update_set(iis2dh->ctx, PROPERTY_ENABLE) < 0) {
274 		return -EIO;
275 	}
276 
277 	if (iis2dh_operating_mode_set(iis2dh->ctx, cfg->pm)) {
278 		return -EIO;
279 	}
280 
281 #if (CONFIG_IIS2DH_ODR != 0)
282 	/* set default odr and full scale for acc */
283 	if (iis2dh_data_rate_set(iis2dh->ctx, CONFIG_IIS2DH_ODR) < 0) {
284 		return -EIO;
285 	}
286 #endif
287 
288 #if (CONFIG_IIS2DH_RANGE != 0)
289 	iis2dh_set_fs_raw(iis2dh, CONFIG_IIS2DH_RANGE);
290 #endif
291 
292 #ifdef CONFIG_IIS2DH_TRIGGER
293 	if (iis2dh_init_interrupt(dev) < 0) {
294 		LOG_ERR("Failed to initialize interrupts");
295 		return -EIO;
296 	}
297 #endif /* CONFIG_IIS2DH_TRIGGER */
298 
299 	return 0;
300 }
301 
302 const struct iis2dh_device_config iis2dh_cfg = {
303 	.bus_name = DT_INST_BUS_LABEL(0),
304 	.pm = CONFIG_IIS2DH_POWER_MODE,
305 #ifdef CONFIG_IIS2DH_TRIGGER
306 	.int_gpio_port = DT_INST_GPIO_LABEL(0, drdy_gpios),
307 	.int_gpio_pin = DT_INST_GPIO_PIN(0, drdy_gpios),
308 	.int_gpio_flags = DT_INST_GPIO_FLAGS(0, drdy_gpios),
309 #endif /* CONFIG_IIS2DH_TRIGGER */
310 };
311 
312 struct iis2dh_data iis2dh_data;
313 
314 DEVICE_DT_INST_DEFINE(0, iis2dh_init, NULL,
315 	     &iis2dh_data, &iis2dh_cfg, POST_KERNEL,
316 	     CONFIG_SENSOR_INIT_PRIORITY, &iis2dh_driver_api);
317