1 /* adxl367_i2c.c - I2C routines for ADXL367 driver
2  */
3 
4 /*
5  * Copyright (c) 2023 Analog Devices
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <string.h>
11 #include <zephyr/logging/log.h>
12 
13 #include "adxl367.h"
14 
15 #ifdef ADXL367_BUS_I2C
16 
17 LOG_MODULE_DECLARE(ADXL367, CONFIG_SENSOR_LOG_LEVEL);
18 
adxl367_bus_access(const struct device * dev,uint8_t reg,void * data,size_t length)19 static int adxl367_bus_access(const struct device *dev, uint8_t reg,
20 			      void *data, size_t length)
21 {
22 	const struct adxl367_dev_config *config = dev->config;
23 
24 	if ((reg & ADXL367_READ) != 0) {
25 		return i2c_burst_read_dt(&config->i2c,
26 					 ADXL367_TO_REG(reg),
27 					 (uint8_t *) data, length);
28 	} else {
29 		if (length != 1) {
30 			return -EINVAL;
31 		}
32 
33 		return i2c_reg_write_byte_dt(&config->i2c,
34 					     ADXL367_TO_REG(reg),
35 					     *(uint8_t *)data);
36 	}
37 }
38 
adxl367_i2c_reg_read(const struct device * dev,uint8_t reg_addr,uint8_t * reg_data)39 static int adxl367_i2c_reg_read(const struct device *dev, uint8_t reg_addr,
40 			    uint8_t *reg_data)
41 {
42 	return adxl367_bus_access(dev, ADXL367_REG_READ(reg_addr), reg_data, 1);
43 }
44 
adxl367_i2c_reg_read_multiple(const struct device * dev,uint8_t reg_addr,uint8_t * reg_data,uint16_t count)45 static int adxl367_i2c_reg_read_multiple(const struct device *dev,
46 					 uint8_t reg_addr,
47 					 uint8_t *reg_data,
48 					 uint16_t count)
49 {
50 	return adxl367_bus_access(dev, ADXL367_REG_READ(reg_addr),
51 				  reg_data, count);
52 }
53 
adxl367_i2c_reg_write(const struct device * dev,uint8_t reg_addr,uint8_t reg_data)54 static int adxl367_i2c_reg_write(const struct device *dev,
55 				 uint8_t reg_addr,
56 				 uint8_t reg_data)
57 {
58 	return adxl367_bus_access(dev, ADXL367_REG_WRITE(reg_addr),
59 				  &reg_data, 1);
60 }
61 
62 
adxl367_i2c_reg_write_mask(const struct device * dev,uint8_t reg_addr,uint32_t mask,uint8_t data)63 int adxl367_i2c_reg_write_mask(const struct device *dev,
64 			       uint8_t reg_addr,
65 			       uint32_t mask,
66 			       uint8_t data)
67 {
68 	int ret;
69 	uint8_t tmp;
70 
71 	ret = adxl367_i2c_reg_read(dev, reg_addr, &tmp);
72 	if (ret != 0) {
73 		return ret;
74 	}
75 
76 	tmp &= ~mask;
77 	tmp |= data;
78 
79 	return adxl367_i2c_reg_write(dev, reg_addr, tmp);
80 }
81 
82 static const struct adxl367_transfer_function adxl367_i2c_transfer_fn = {
83 	.read_reg_multiple = adxl367_i2c_reg_read_multiple,
84 	.write_reg = adxl367_i2c_reg_write,
85 	.read_reg  = adxl367_i2c_reg_read,
86 	.write_reg_mask = adxl367_i2c_reg_write_mask,
87 };
88 
adxl367_i2c_init(const struct device * dev)89 int adxl367_i2c_init(const struct device *dev)
90 {
91 	struct adxl367_data *data = dev->data;
92 	const struct adxl367_dev_config *config = dev->config;
93 
94 	data->hw_tf = &adxl367_i2c_transfer_fn;
95 
96 	if (!i2c_is_ready_dt(&config->i2c)) {
97 		return -ENODEV;
98 	}
99 
100 	return 0;
101 }
102 #endif /* ADXL367_BUS_I2C */
103