1 /* ST Microelectronics ISM330DHCX 6-axis IMU sensor 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/ism330dhcx.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_ism330dhcx
12 
13 #include <string.h>
14 #include <zephyr/drivers/i2c.h>
15 #include <zephyr/logging/log.h>
16 
17 #include "ism330dhcx.h"
18 
19 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
20 
21 LOG_MODULE_DECLARE(ISM330DHCX, CONFIG_SENSOR_LOG_LEVEL);
22 
ism330dhcx_i2c_read(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)23 static int ism330dhcx_i2c_read(const struct device *dev, uint8_t reg_addr, uint8_t *value,
24 			       uint8_t len)
25 {
26 	const struct ism330dhcx_config *cfg = dev->config;
27 
28 	return i2c_burst_read_dt(&cfg->i2c, reg_addr, value, len);
29 }
30 
ism330dhcx_i2c_write(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)31 static int ism330dhcx_i2c_write(const struct device *dev, uint8_t reg_addr, uint8_t *value,
32 				uint8_t len)
33 {
34 	const struct ism330dhcx_config *cfg = dev->config;
35 
36 	return i2c_burst_write_dt(&cfg->i2c, reg_addr, value, len);
37 }
38 
ism330dhcx_i2c_init(const struct device * dev)39 int ism330dhcx_i2c_init(const struct device *dev)
40 {
41 	struct ism330dhcx_data *data = dev->data;
42 	const struct ism330dhcx_config *cfg = dev->config;
43 
44 	if (!device_is_ready(cfg->i2c.bus)) {
45 		LOG_ERR("I2C bus device is not ready");
46 		return -ENODEV;
47 	};
48 
49 	data->ctx_i2c.read_reg = (stmdev_read_ptr) ism330dhcx_i2c_read;
50 	data->ctx_i2c.write_reg = (stmdev_write_ptr) ism330dhcx_i2c_write;
51 	data->ctx_i2c.mdelay = (stmdev_mdelay_ptr) stmemsc_mdelay;
52 
53 	data->ctx = &data->ctx_i2c;
54 	data->ctx->handle = (void *)dev;
55 
56 	return 0;
57 }
58 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
59