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 <string.h>
14 #include <zephyr/drivers/i2c.h>
15 #include <zephyr/logging/log.h>
16 
17 #include "iis2dh.h"
18 
19 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
20 
21 LOG_MODULE_DECLARE(IIS2DH, CONFIG_SENSOR_LOG_LEVEL);
22 
iis2dh_i2c_read(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint16_t len)23 static int iis2dh_i2c_read(const struct device *dev, uint8_t reg_addr, uint8_t *value, uint16_t len)
24 {
25 	const struct iis2dh_device_config *config = dev->config;
26 
27 	return i2c_burst_read_dt(&config->i2c, reg_addr | 0x80, value, len);
28 }
29 
iis2dh_i2c_write(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint16_t len)30 static int iis2dh_i2c_write(const struct device *dev, uint8_t reg_addr, uint8_t *value,
31 			    uint16_t len)
32 {
33 	const struct iis2dh_device_config *config = dev->config;
34 
35 	return i2c_burst_write_dt(&config->i2c, reg_addr | 0x80, value, len);
36 }
37 
38 stmdev_ctx_t iis2dh_i2c_ctx = {
39 	.read_reg = (stmdev_read_ptr) iis2dh_i2c_read,
40 	.write_reg = (stmdev_write_ptr) iis2dh_i2c_write,
41 	.mdelay = (stmdev_mdelay_ptr) stmemsc_mdelay,
42 };
43 
iis2dh_i2c_init(const struct device * dev)44 int iis2dh_i2c_init(const struct device *dev)
45 {
46 	struct iis2dh_data *data = dev->data;
47 	const struct iis2dh_device_config *config = dev->config;
48 
49 	if (!device_is_ready(config->i2c.bus)) {
50 		LOG_ERR("Bus device is not ready");
51 		return -ENODEV;
52 	}
53 
54 	data->ctx = &iis2dh_i2c_ctx;
55 	data->ctx->handle = (void *)dev;
56 
57 	return 0;
58 }
59 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
60