1 /* lsm6dsl_spi.c - SPI 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 <zephyr/logging/log.h>
14
15 #include "lsm6dsl.h"
16
17 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
18
19 #define LSM6DSL_SPI_READ (1 << 7)
20
21 LOG_MODULE_DECLARE(LSM6DSL, CONFIG_SENSOR_LOG_LEVEL);
22
lsm6dsl_raw_read(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)23 static int lsm6dsl_raw_read(const struct device *dev, uint8_t reg_addr,
24 uint8_t *value, uint8_t len)
25 {
26 const struct lsm6dsl_config *cfg = dev->config;
27 uint8_t buffer_tx[2] = { reg_addr | LSM6DSL_SPI_READ, 0 };
28 const struct spi_buf tx_buf = {
29 .buf = buffer_tx,
30 .len = 2,
31 };
32 const struct spi_buf_set tx = {
33 .buffers = &tx_buf,
34 .count = 1
35 };
36 const struct spi_buf rx_buf[2] = {
37 {
38 .buf = NULL,
39 .len = 1,
40 },
41 {
42 .buf = value,
43 .len = len,
44 }
45 };
46 const struct spi_buf_set rx = {
47 .buffers = rx_buf,
48 .count = 2
49 };
50
51
52 if (len > 64) {
53 return -EIO;
54 }
55
56 if (spi_transceive_dt(&cfg->bus_cfg.spi, &tx, &rx)) {
57 return -EIO;
58 }
59
60 return 0;
61 }
62
lsm6dsl_raw_write(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)63 static int lsm6dsl_raw_write(const struct device *dev, uint8_t reg_addr,
64 uint8_t *value, uint8_t len)
65 {
66 const struct lsm6dsl_config *cfg = dev->config;
67 uint8_t buffer_tx[1] = { reg_addr & ~LSM6DSL_SPI_READ };
68 const struct spi_buf tx_buf[2] = {
69 {
70 .buf = buffer_tx,
71 .len = 1,
72 },
73 {
74 .buf = value,
75 .len = len,
76 }
77 };
78 const struct spi_buf_set tx = {
79 .buffers = tx_buf,
80 .count = 2
81 };
82
83
84 if (len > 64) {
85 return -EIO;
86 }
87
88 if (spi_write_dt(&cfg->bus_cfg.spi, &tx)) {
89 return -EIO;
90 }
91
92 return 0;
93 }
94
lsm6dsl_spi_read_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)95 static int lsm6dsl_spi_read_data(const struct device *dev, uint8_t reg_addr,
96 uint8_t *value, uint8_t len)
97 {
98 return lsm6dsl_raw_read(dev, reg_addr, value, len);
99 }
100
lsm6dsl_spi_write_data(const struct device * dev,uint8_t reg_addr,uint8_t * value,uint8_t len)101 static int lsm6dsl_spi_write_data(const struct device *dev, uint8_t reg_addr,
102 uint8_t *value, uint8_t len)
103 {
104 return lsm6dsl_raw_write(dev, reg_addr, value, len);
105 }
106
lsm6dsl_spi_read_reg(const struct device * dev,uint8_t reg_addr,uint8_t * value)107 static int lsm6dsl_spi_read_reg(const struct device *dev, uint8_t reg_addr,
108 uint8_t *value)
109 {
110 return lsm6dsl_raw_read(dev, reg_addr, value, 1);
111 }
112
lsm6dsl_spi_update_reg(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t value)113 static int lsm6dsl_spi_update_reg(const struct device *dev, uint8_t reg_addr,
114 uint8_t mask, uint8_t value)
115 {
116 uint8_t tmp_val;
117
118 lsm6dsl_raw_read(dev, reg_addr, &tmp_val, 1);
119 tmp_val = (tmp_val & ~mask) | (value & mask);
120
121 return lsm6dsl_raw_write(dev, reg_addr, &tmp_val, 1);
122 }
123
124 static const struct lsm6dsl_transfer_function lsm6dsl_spi_transfer_fn = {
125 .read_data = lsm6dsl_spi_read_data,
126 .write_data = lsm6dsl_spi_write_data,
127 .read_reg = lsm6dsl_spi_read_reg,
128 .update_reg = lsm6dsl_spi_update_reg,
129 };
130
lsm6dsl_spi_init(const struct device * dev)131 int lsm6dsl_spi_init(const struct device *dev)
132 {
133 struct lsm6dsl_data *data = dev->data;
134 const struct lsm6dsl_config *cfg = dev->config;
135
136 data->hw_tf = &lsm6dsl_spi_transfer_fn;
137
138 if (!spi_is_ready_dt(&cfg->bus_cfg.spi)) {
139 return -ENODEV;
140 }
141
142 return 0;
143 }
144 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
145