1 /* lsm6dsl_i2c.c - I2C routines for LSM6DSL driver
2  */
3 
4 /*
5  * Copyright (c) 2018 STMicroelectronics
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #define DT_DRV_COMPAT st_lsm6dsl
11 
12 #include <string.h>
13 #include <zephyr/logging/log.h>
14 
15 #include "lsm6dsl.h"
16 
17 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
18 
19 LOG_MODULE_DECLARE(LSM6DSL, CONFIG_SENSOR_LOG_LEVEL);
20 
lsm6dsl_i2c_read_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)21 static int lsm6dsl_i2c_read_data(const struct device *dev, uint8_t reg_addr,
22 				 uint8_t *value, uint8_t len)
23 {
24 	const struct lsm6dsl_config *cfg = dev->config;
25 
26 	return i2c_burst_read_dt(&cfg->bus_cfg.i2c, reg_addr, value, len);
27 }
28 
lsm6dsl_i2c_write_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)29 static int lsm6dsl_i2c_write_data(const struct device *dev, uint8_t reg_addr,
30 				  uint8_t *value, uint8_t len)
31 {
32 	const struct lsm6dsl_config *cfg = dev->config;
33 
34 	return i2c_burst_write_dt(&cfg->bus_cfg.i2c, reg_addr, value, len);
35 }
36 
lsm6dsl_i2c_read_reg(const struct device * dev,uint8_t reg_addr,uint8_t * value)37 static int lsm6dsl_i2c_read_reg(const struct device *dev, uint8_t reg_addr,
38 				uint8_t *value)
39 {
40 	const struct lsm6dsl_config *cfg = dev->config;
41 
42 	return i2c_reg_read_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
43 }
44 
lsm6dsl_i2c_update_reg(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t value)45 static int lsm6dsl_i2c_update_reg(const struct device *dev, uint8_t reg_addr,
46 				uint8_t mask, uint8_t value)
47 {
48 	const struct lsm6dsl_config *cfg = dev->config;
49 
50 	return i2c_reg_update_byte_dt(&cfg->bus_cfg.i2c, reg_addr, mask, value);
51 }
52 
53 
54 static const struct lsm6dsl_transfer_function lsm6dsl_i2c_transfer_fn = {
55 	.read_data = lsm6dsl_i2c_read_data,
56 	.write_data = lsm6dsl_i2c_write_data,
57 	.read_reg  = lsm6dsl_i2c_read_reg,
58 	.update_reg = lsm6dsl_i2c_update_reg,
59 };
60 
lsm6dsl_i2c_init(const struct device * dev)61 int lsm6dsl_i2c_init(const struct device *dev)
62 {
63 	struct lsm6dsl_data *data = dev->data;
64 	const struct lsm6dsl_config *cfg = dev->config;
65 
66 	data->hw_tf = &lsm6dsl_i2c_transfer_fn;
67 
68 	if (!device_is_ready(cfg->bus_cfg.i2c.bus)) {
69 		return -ENODEV;
70 	}
71 
72 	return 0;
73 }
74 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
75