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 <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 	struct lsm6dsl_data *data = dev->data;
25 	const struct lsm6dsl_config *cfg = dev->config;
26 
27 	return i2c_burst_read(data->bus, cfg->bus_cfg.i2c_slv_addr,
28 			      reg_addr, value, len);
29 }
30 
lsm6dsl_i2c_write_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)31 static int lsm6dsl_i2c_write_data(const struct device *dev, uint8_t reg_addr,
32 				  uint8_t *value, uint8_t len)
33 {
34 	struct lsm6dsl_data *data = dev->data;
35 	const struct lsm6dsl_config *cfg = dev->config;
36 
37 	return i2c_burst_write(data->bus, cfg->bus_cfg.i2c_slv_addr,
38 			       reg_addr, value, len);
39 }
40 
lsm6dsl_i2c_read_reg(const struct device * dev,uint8_t reg_addr,uint8_t * value)41 static int lsm6dsl_i2c_read_reg(const struct device *dev, uint8_t reg_addr,
42 				uint8_t *value)
43 {
44 	struct lsm6dsl_data *data = dev->data;
45 	const struct lsm6dsl_config *cfg = dev->config;
46 
47 	return i2c_reg_read_byte(data->bus,
48 				 cfg->bus_cfg.i2c_slv_addr,
49 				 reg_addr, value);
50 }
51 
lsm6dsl_i2c_update_reg(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t value)52 static int lsm6dsl_i2c_update_reg(const struct device *dev, uint8_t reg_addr,
53 				uint8_t mask, uint8_t value)
54 {
55 	struct lsm6dsl_data *data = dev->data;
56 	const struct lsm6dsl_config *cfg = dev->config;
57 
58 	return i2c_reg_update_byte(data->bus,
59 				  cfg->bus_cfg.i2c_slv_addr,
60 				  reg_addr, mask, value);
61 }
62 
63 
64 static const struct lsm6dsl_transfer_function lsm6dsl_i2c_transfer_fn = {
65 	.read_data = lsm6dsl_i2c_read_data,
66 	.write_data = lsm6dsl_i2c_write_data,
67 	.read_reg  = lsm6dsl_i2c_read_reg,
68 	.update_reg = lsm6dsl_i2c_update_reg,
69 };
70 
lsm6dsl_i2c_init(const struct device * dev)71 int lsm6dsl_i2c_init(const struct device *dev)
72 {
73 	struct lsm6dsl_data *data = dev->data;
74 
75 	data->hw_tf = &lsm6dsl_i2c_transfer_fn;
76 
77 	return 0;
78 }
79 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
80