1 /* ST Microelectronics LIS2DS12 3-axis accelerometer driver
2  *
3  * Copyright (c) 2019 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/lis2ds12.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_lis2ds12
12 
13 #include <zephyr/drivers/sensor.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/device.h>
16 #include <zephyr/init.h>
17 #include <string.h>
18 #include <zephyr/sys/__assert.h>
19 #include <zephyr/logging/log.h>
20 #include <zephyr/dt-bindings/sensor/lis2ds12.h>
21 
22 #include "lis2ds12.h"
23 
24 LOG_MODULE_REGISTER(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
25 
lis2ds12_set_odr(const struct device * dev,uint8_t odr)26 static int lis2ds12_set_odr(const struct device *dev, uint8_t odr)
27 {
28 	const struct lis2ds12_config *cfg = dev->config;
29 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
30 	lis2ds12_odr_t val;
31 
32 	/* check if power off */
33 	if (odr == 0U) {
34 		LOG_DBG("%s: set power-down", dev->name);
35 		return lis2ds12_xl_data_rate_set(ctx, LIS2DS12_XL_ODR_OFF);
36 	}
37 
38 	/*
39 	 * odr >= 1600Hz are available in HF mode only
40 	 * 12,5Hz <= odr <= 800Hz are available in LP and HR mode only
41 	 * odr == 1Hz is available in LP mode only
42 	 */
43 	if ((odr >= LIS2DS12_DT_ODR_1600Hz && cfg->pm != LIS2DS12_DT_HIGH_FREQUENCY) ||
44 	    (odr < LIS2DS12_DT_ODR_1600Hz && cfg->pm == LIS2DS12_DT_HIGH_FREQUENCY) ||
45 	    (odr == LIS2DS12_DT_ODR_1Hz_LP && cfg->pm != LIS2DS12_DT_LOW_POWER)) {
46 		LOG_ERR("%s: bad odr and pm combination", dev->name);
47 		return -ENOTSUP;
48 	}
49 
50 	switch (odr) {
51 	case 1:
52 		val = LIS2DS12_XL_ODR_1Hz_LP;
53 		break;
54 	case 2:
55 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_12Hz5_LP :
56 				       LIS2DS12_XL_ODR_12Hz5_HR;
57 		break;
58 	case 3:
59 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_25Hz_LP :
60 				       LIS2DS12_XL_ODR_25Hz_HR;
61 		break;
62 	case 4:
63 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_50Hz_LP :
64 				       LIS2DS12_XL_ODR_50Hz_HR;
65 		break;
66 	case 5:
67 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_100Hz_LP :
68 				       LIS2DS12_XL_ODR_100Hz_HR;
69 		break;
70 	case 6:
71 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_200Hz_LP :
72 				       LIS2DS12_XL_ODR_200Hz_HR;
73 		break;
74 	case 7:
75 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_400Hz_LP :
76 				       LIS2DS12_XL_ODR_400Hz_HR;
77 		break;
78 	case 8:
79 		val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_800Hz_LP :
80 				       LIS2DS12_XL_ODR_800Hz_HR;
81 		break;
82 	case 9:
83 		val = LIS2DS12_XL_ODR_1k6Hz_HF;
84 		break;
85 	case 10:
86 		val = LIS2DS12_XL_ODR_3k2Hz_HF;
87 		break;
88 	case 11:
89 		val = LIS2DS12_XL_ODR_6k4Hz_HF;
90 		break;
91 	default:
92 		LOG_ERR("%s: bad odr %d", dev->name, odr);
93 		return -ENOTSUP;
94 	}
95 
96 	return lis2ds12_xl_data_rate_set(ctx, val);
97 }
98 
lis2ds12_set_range(const struct device * dev,uint8_t range)99 static int lis2ds12_set_range(const struct device *dev, uint8_t range)
100 {
101 	int err;
102 	struct lis2ds12_data *data = dev->data;
103 	const struct lis2ds12_config *cfg = dev->config;
104 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
105 
106 	switch (range) {
107 	default:
108 	case 2U:
109 		err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_2g);
110 		data->gain = lis2ds12_from_fs2g_to_mg(1);
111 		break;
112 	case 4U:
113 		err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_4g);
114 		data->gain = lis2ds12_from_fs4g_to_mg(1);
115 		break;
116 	case 8U:
117 		err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_8g);
118 		data->gain = lis2ds12_from_fs8g_to_mg(1);
119 		break;
120 	case 16U:
121 		err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_16g);
122 		data->gain = lis2ds12_from_fs16g_to_mg(1);
123 		break;
124 	}
125 
126 	return err;
127 }
128 
lis2ds12_accel_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)129 static int lis2ds12_accel_config(const struct device *dev,
130 				 enum sensor_channel chan,
131 				 enum sensor_attribute attr,
132 				 const struct sensor_value *val)
133 {
134 	switch (attr) {
135 	case SENSOR_ATTR_FULL_SCALE:
136 		return lis2ds12_set_range(dev, sensor_ms2_to_g(val));
137 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
138 		LOG_DBG("%s: set odr to %d Hz", dev->name, val->val1);
139 		return lis2ds12_set_odr(dev, LIS2DS12_ODR_TO_REG(val->val1));
140 	default:
141 		LOG_DBG("Accel attribute not supported.");
142 		return -ENOTSUP;
143 	}
144 
145 	return 0;
146 }
147 
lis2ds12_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)148 static int lis2ds12_attr_set(const struct device *dev,
149 			     enum sensor_channel chan,
150 			     enum sensor_attribute attr,
151 			     const struct sensor_value *val)
152 {
153 	switch (chan) {
154 	case SENSOR_CHAN_ACCEL_XYZ:
155 		return lis2ds12_accel_config(dev, chan, attr, val);
156 	default:
157 		LOG_WRN("attr_set() not supported on this channel.");
158 		return -ENOTSUP;
159 	}
160 
161 	return 0;
162 }
163 
lis2ds12_sample_fetch_accel(const struct device * dev)164 static int lis2ds12_sample_fetch_accel(const struct device *dev)
165 {
166 	struct lis2ds12_data *data = dev->data;
167 	const struct lis2ds12_config *cfg = dev->config;
168 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
169 	int16_t buf[3];
170 
171 	/* fetch raw data sample */
172 	if (lis2ds12_acceleration_raw_get(ctx, buf) < 0) {
173 		LOG_ERR("Failed to fetch raw data sample");
174 		return -EIO;
175 	}
176 
177 	data->sample_x = buf[0];
178 	data->sample_y = buf[1];
179 	data->sample_z = buf[2];
180 
181 	return 0;
182 }
183 
lis2ds12_sample_fetch(const struct device * dev,enum sensor_channel chan)184 static int lis2ds12_sample_fetch(const struct device *dev,
185 				 enum sensor_channel chan)
186 {
187 	switch (chan) {
188 	case SENSOR_CHAN_ACCEL_XYZ:
189 		lis2ds12_sample_fetch_accel(dev);
190 		break;
191 #if defined(CONFIG_LIS2DS12_ENABLE_TEMP)
192 	case SENSOR_CHAN_DIE_TEMP:
193 		/* ToDo:
194 		lis2ds12_sample_fetch_temp(dev)
195 		*/
196 		break;
197 #endif
198 	case SENSOR_CHAN_ALL:
199 		lis2ds12_sample_fetch_accel(dev);
200 #if defined(CONFIG_LIS2DS12_ENABLE_TEMP)
201 		/* ToDo:
202 		lis2ds12_sample_fetch_temp(dev)
203 		*/
204 #endif
205 		break;
206 	default:
207 		return -ENOTSUP;
208 	}
209 
210 	return 0;
211 }
212 
lis2ds12_convert(struct sensor_value * val,int raw_val,float gain)213 static inline void lis2ds12_convert(struct sensor_value *val, int raw_val,
214 				    float gain)
215 {
216 	int64_t dval;
217 
218 	/* Gain is in mg/LSB */
219 	/* Convert to m/s^2 */
220 	dval = ((int64_t)raw_val * gain * SENSOR_G) / 1000;
221 	val->val1 = dval / 1000000LL;
222 	val->val2 = dval % 1000000LL;
223 }
224 
lis2ds12_get_channel(enum sensor_channel chan,struct sensor_value * val,struct lis2ds12_data * data,float gain)225 static inline int lis2ds12_get_channel(enum sensor_channel chan,
226 					     struct sensor_value *val,
227 					     struct lis2ds12_data *data,
228 					     float gain)
229 {
230 	switch (chan) {
231 	case SENSOR_CHAN_ACCEL_X:
232 		lis2ds12_convert(val, data->sample_x, gain);
233 		break;
234 	case SENSOR_CHAN_ACCEL_Y:
235 		lis2ds12_convert(val, data->sample_y, gain);
236 		break;
237 	case SENSOR_CHAN_ACCEL_Z:
238 		lis2ds12_convert(val, data->sample_z, gain);
239 		break;
240 	case SENSOR_CHAN_ACCEL_XYZ:
241 		lis2ds12_convert(val, data->sample_x, gain);
242 		lis2ds12_convert(val + 1, data->sample_y, gain);
243 		lis2ds12_convert(val + 2, data->sample_z, gain);
244 		break;
245 	default:
246 		return -ENOTSUP;
247 	}
248 
249 	return 0;
250 }
251 
lis2ds12_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)252 static int lis2ds12_channel_get(const struct device *dev,
253 				enum sensor_channel chan,
254 				struct sensor_value *val)
255 {
256 	struct lis2ds12_data *data = dev->data;
257 
258 	return lis2ds12_get_channel(chan, val, data, data->gain);
259 }
260 
261 static DEVICE_API(sensor, lis2ds12_driver_api) = {
262 	.attr_set = lis2ds12_attr_set,
263 #if defined(CONFIG_LIS2DS12_TRIGGER)
264 	.trigger_set = lis2ds12_trigger_set,
265 #endif
266 	.sample_fetch = lis2ds12_sample_fetch,
267 	.channel_get = lis2ds12_channel_get,
268 };
269 
lis2ds12_init(const struct device * dev)270 static int lis2ds12_init(const struct device *dev)
271 {
272 	const struct lis2ds12_config * const cfg = dev->config;
273 	stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
274 	uint8_t chip_id;
275 	int ret;
276 
277 	/* check chip ID */
278 	ret = lis2ds12_device_id_get(ctx, &chip_id);
279 	if (ret < 0) {
280 		LOG_ERR("%s: Not able to read dev id", dev->name);
281 		return ret;
282 	}
283 
284 	if (chip_id != LIS2DS12_ID) {
285 		LOG_ERR("%s: Invalid chip ID 0x%02x", dev->name, chip_id);
286 		return -EINVAL;
287 	}
288 
289 	/* reset device */
290 	ret = lis2ds12_reset_set(ctx, PROPERTY_ENABLE);
291 	if (ret < 0) {
292 		return ret;
293 	}
294 
295 	k_busy_wait(100);
296 
297 	LOG_DBG("%s: chip id 0x%x", dev->name, chip_id);
298 
299 #ifdef CONFIG_LIS2DS12_TRIGGER
300 	ret = lis2ds12_trigger_init(dev);
301 	if (ret < 0) {
302 		LOG_ERR("%s: Failed to initialize triggers", dev->name);
303 		return ret;
304 	}
305 #endif
306 
307 	/* set sensor default pm and odr */
308 	LOG_DBG("%s: pm: %d, odr: %d", dev->name, cfg->pm, cfg->odr);
309 	ret = lis2ds12_set_odr(dev, (cfg->pm == 0) ? 0 : cfg->odr);
310 	if (ret < 0) {
311 		LOG_ERR("%s: odr init error (12.5 Hz)", dev->name);
312 		return ret;
313 	}
314 
315 	/* set sensor default scale */
316 	LOG_DBG("%s: range is %d", dev->name, cfg->range);
317 	ret = lis2ds12_set_range(dev, cfg->range);
318 	if (ret < 0) {
319 		LOG_ERR("%s: range init error %d", dev->name, cfg->range);
320 		return ret;
321 	}
322 
323 	return 0;
324 }
325 
326 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
327 #warning "LIS2DS12 driver enabled without any devices"
328 #endif
329 
330 /*
331  * Device creation macro, shared by LIS2DS12_DEFINE_SPI() and
332  * LIS2DS12_DEFINE_I2C().
333  */
334 
335 #define LIS2DS12_DEVICE_INIT(inst)					\
336 	SENSOR_DEVICE_DT_INST_DEFINE(inst,				\
337 			    lis2ds12_init,				\
338 			    NULL,					\
339 			    &lis2ds12_data_##inst,			\
340 			    &lis2ds12_config_##inst,			\
341 			    POST_KERNEL,				\
342 			    CONFIG_SENSOR_INIT_PRIORITY,		\
343 			    &lis2ds12_driver_api);
344 
345 /*
346  * Instantiation macros used when a device is on a SPI bus.
347  */
348 
349 #ifdef CONFIG_LIS2DS12_TRIGGER
350 #define LIS2DS12_CFG_IRQ(inst) \
351 	.gpio_int = GPIO_DT_SPEC_INST_GET(inst, irq_gpios),
352 #else
353 #define LIS2DS12_CFG_IRQ(inst)
354 #endif /* CONFIG_LIS2DS12_TRIGGER */
355 
356 #define LIS2DS12_CONFIG_COMMON(inst)					\
357 	.range = DT_INST_PROP(inst, range),				\
358 	.pm = DT_INST_PROP(inst, power_mode),				\
359 	.odr = DT_INST_PROP(inst, odr),					\
360 	COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios),		\
361 			(LIS2DS12_CFG_IRQ(inst)), ())
362 
363 #define LIS2DS12_SPI_OPERATION (SPI_WORD_SET(8) |			\
364 				SPI_OP_MODE_MASTER |			\
365 				SPI_MODE_CPOL |				\
366 				SPI_MODE_CPHA)				\
367 
368 #define LIS2DS12_CONFIG_SPI(inst)					\
369 	{								\
370 		STMEMSC_CTX_SPI(&lis2ds12_config_##inst.stmemsc_cfg),	\
371 		.stmemsc_cfg = {					\
372 			.spi = SPI_DT_SPEC_INST_GET(inst,		\
373 					   LIS2DS12_SPI_OPERATION,	\
374 					   0),				\
375 		},							\
376 		LIS2DS12_CONFIG_COMMON(inst)				\
377 	}
378 
379 /*
380  * Instantiation macros used when a device is on an I2C bus.
381  */
382 
383 #define LIS2DS12_CONFIG_I2C(inst)					\
384 	{								\
385 		STMEMSC_CTX_I2C(&lis2ds12_config_##inst.stmemsc_cfg),	\
386 		.stmemsc_cfg = {					\
387 			.i2c = I2C_DT_SPEC_INST_GET(inst),		\
388 		},							\
389 		LIS2DS12_CONFIG_COMMON(inst)				\
390 	}
391 
392 /*
393  * Main instantiation macro. Use of COND_CODE_1() selects the right
394  * bus-specific macro at preprocessor time.
395  */
396 
397 #define LIS2DS12_DEFINE(inst)						\
398 	static struct lis2ds12_data lis2ds12_data_##inst;		\
399 	static const struct lis2ds12_config lis2ds12_config_##inst =	\
400 	COND_CODE_1(DT_INST_ON_BUS(inst, spi),				\
401 		    (LIS2DS12_CONFIG_SPI(inst)),			\
402 		    (LIS2DS12_CONFIG_I2C(inst)));			\
403 	LIS2DS12_DEVICE_INIT(inst)
404 
405 DT_INST_FOREACH_STATUS_OKAY(LIS2DS12_DEFINE)
406