1 /* Bosch BMA4xx 3-axis accelerometer driver
2  *
3  * Copyright (c) 2023 Google LLC
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define DT_DRV_COMPAT bosch_bma4xx
9 
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/logging/log.h>
12 
13 #include "bma4xx.h"
14 
15 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
16 
17 LOG_MODULE_DECLARE(bma4xx, CONFIG_SENSOR_LOG_LEVEL);
18 
bma4xx_i2c_read_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)19 static int bma4xx_i2c_read_data(const struct device *dev, uint8_t reg_addr,
20 				 uint8_t *value, uint8_t len)
21 {
22 	const struct bma4xx_config *cfg = dev->config;
23 
24 	return i2c_burst_read_dt(&cfg->bus_cfg.i2c, reg_addr, value,
25 				 len);
26 }
27 
bma4xx_i2c_write_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)28 static int bma4xx_i2c_write_data(const struct device *dev, uint8_t reg_addr,
29 				  uint8_t *value, uint8_t len)
30 {
31 	const struct bma4xx_config *cfg = dev->config;
32 
33 	return i2c_burst_write_dt(&cfg->bus_cfg.i2c, reg_addr, value,
34 				  len);
35 }
36 
bma4xx_i2c_read_reg(const struct device * dev,uint8_t reg_addr,uint8_t * value)37 static int bma4xx_i2c_read_reg(const struct device *dev, uint8_t reg_addr,
38 				uint8_t *value)
39 {
40 	const struct bma4xx_config *cfg = dev->config;
41 
42 	return i2c_reg_read_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
43 }
44 
bma4xx_i2c_write_reg(const struct device * dev,uint8_t reg_addr,uint8_t value)45 static int bma4xx_i2c_write_reg(const struct device *dev, uint8_t reg_addr,
46 				uint8_t value)
47 {
48 	const struct bma4xx_config *cfg = dev->config;
49 
50 	return i2c_reg_write_byte_dt(&cfg->bus_cfg.i2c, reg_addr, value);
51 }
52 
bma4xx_i2c_update_reg(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t value)53 static int bma4xx_i2c_update_reg(const struct device *dev, uint8_t reg_addr,
54 				  uint8_t mask, uint8_t value)
55 {
56 	const struct bma4xx_config *cfg = dev->config;
57 
58 	return i2c_reg_update_byte_dt(&cfg->bus_cfg.i2c, reg_addr, mask, value);
59 }
60 
61 static const struct bma4xx_hw_operations i2c_ops = {
62 	.read_data = bma4xx_i2c_read_data,
63 	.write_data = bma4xx_i2c_write_data,
64 	.read_reg  = bma4xx_i2c_read_reg,
65 	.write_reg  = bma4xx_i2c_write_reg,
66 	.update_reg = bma4xx_i2c_update_reg,
67 };
68 
bma4xx_i2c_init(const struct device * dev)69 int bma4xx_i2c_init(const struct device *dev)
70 {
71 	struct bma4xx_data *data = dev->data;
72 	const struct bma4xx_config *cfg = dev->config;
73 
74 	if (!device_is_ready(cfg->bus_cfg.i2c.bus)) {
75 		LOG_ERR("Bus device is not ready");
76 		return -ENODEV;
77 	}
78 
79 	data->hw_ops = &i2c_ops;
80 
81 	return 0;
82 }
83 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
84