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