1 /*
2 * Copyright (c) 2024 Gustavo Silva
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT sciosense_ens160
8
9 #include <zephyr/drivers/spi.h>
10
11 #include "ens160.h"
12
13 LOG_MODULE_DECLARE(ENS160, CONFIG_SENSOR_LOG_LEVEL);
14
15 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
16
17 #define ENS160_SPI_READ_BIT BIT(0)
18
ens160_read_reg_spi(const struct device * dev,uint8_t reg,uint8_t * val)19 static int ens160_read_reg_spi(const struct device *dev, uint8_t reg, uint8_t *val)
20 {
21 const struct ens160_config *config = dev->config;
22
23 uint8_t tx_buffer = (reg << 1) | ENS160_SPI_READ_BIT;
24
25 const struct spi_buf tx_buf = {
26 .buf = &tx_buffer,
27 .len = 1,
28 };
29
30 const struct spi_buf_set tx = {
31 .buffers = &tx_buf,
32 .count = 1,
33 };
34
35 struct spi_buf rx_buf[2] = {
36 {
37 .buf = NULL,
38 .len = 1,
39 },
40 {
41 .buf = val,
42 .len = 1,
43 }
44 };
45
46 const struct spi_buf_set rx = {
47 .buffers = rx_buf,
48 .count = 2,
49 };
50
51 return spi_transceive_dt(&config->spi, &tx, &rx);
52 }
53
ens160_read_data_spi(const struct device * dev,uint8_t start,uint8_t * data,size_t len)54 static int ens160_read_data_spi(const struct device *dev, uint8_t start, uint8_t *data, size_t len)
55 {
56 const struct ens160_config *config = dev->config;
57
58 uint8_t tx_buffer = (start << 1) | ENS160_SPI_READ_BIT;
59
60 const struct spi_buf tx_buf = {
61 .buf = &tx_buffer,
62 .len = 1,
63 };
64
65 const struct spi_buf_set tx = {
66 .buffers = &tx_buf,
67 .count = 1,
68 };
69
70 struct spi_buf rx_buf[2] = {
71 {
72 .buf = NULL,
73 .len = 1,
74 },
75 {
76 .buf = data,
77 .len = len,
78 }
79 };
80
81 const struct spi_buf_set rx = {
82 .buffers = rx_buf,
83 .count = 2,
84 };
85
86 return spi_transceive_dt(&config->spi, &tx, &rx);
87 }
88
ens160_write_reg_spi(const struct device * dev,uint8_t reg,uint8_t val)89 static int ens160_write_reg_spi(const struct device *dev, uint8_t reg, uint8_t val)
90 {
91 const struct ens160_config *config = dev->config;
92
93 uint8_t tx_buffer = reg << 1;
94
95 const struct spi_buf buf[2] = {
96 {
97 .buf = &tx_buffer,
98 .len = 1,
99 },
100 {
101 .buf = &val,
102 .len = 1,
103 }
104 };
105
106 const struct spi_buf_set tx = {
107 .buffers = buf,
108 .count = 2,
109 };
110
111 return spi_write_dt(&config->spi, &tx);
112 }
113
ens160_write_data_spi(const struct device * dev,uint8_t reg,uint8_t * data,size_t len)114 static int ens160_write_data_spi(const struct device *dev, uint8_t reg, uint8_t *data, size_t len)
115 {
116 const struct ens160_config *config = dev->config;
117
118 uint8_t tx_buffer = reg << 1;
119
120 const struct spi_buf buf[2] = {
121 {
122 .buf = &tx_buffer,
123 .len = 1,
124 },
125 {
126 .buf = &data,
127 .len = len,
128 }
129 };
130
131 const struct spi_buf_set tx = {
132 .buffers = buf,
133 .count = 2,
134 };
135
136 return spi_write_dt(&config->spi, &tx);
137 }
138
139 const struct ens160_transfer_function ens160_spi_transfer_function = {
140 .read_reg = ens160_read_reg_spi,
141 .read_data = ens160_read_data_spi,
142 .write_reg = ens160_write_reg_spi,
143 .write_data = ens160_write_data_spi,
144 };
145
ens160_spi_init(const struct device * dev)146 int ens160_spi_init(const struct device *dev)
147 {
148 const struct ens160_config *config = dev->config;
149 struct ens160_data *data = dev->data;
150
151 if (!spi_is_ready_dt(&config->spi)) {
152 LOG_DBG("SPI bus not ready");
153 return -ENODEV;
154 }
155
156 data->tf = &ens160_spi_transfer_function;
157
158 return 0;
159 }
160
161 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
162