1 /* ST Microelectronics STMEMS hal i/f
2  *
3  * Copyright (c) 2021 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * zephyrproject-rtos/modules/hal/st/sensor/stmemsc/
8  */
9 
10 #ifndef ZEPHYR_DRIVERS_SENSOR_STMEMSC_STMEMSC_H_
11 #define ZEPHYR_DRIVERS_SENSOR_STMEMSC_STMEMSC_H_
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/i2c.h>
15 #include <zephyr/drivers/i3c.h>
16 #include <zephyr/drivers/spi.h>
17 
18 void stmemsc_mdelay(uint32_t millisec);
19 
20 #ifdef CONFIG_I2C
21 /*
22  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
23  * standard stmemsc i2c APIs.
24  */
25 #define STMEMSC_CTX_I2C(stmdev_ctx_ptr)					\
26 	.ctx = {							\
27 		.read_reg = (stmdev_read_ptr)stmemsc_i2c_read,		\
28 		.write_reg = (stmdev_write_ptr)stmemsc_i2c_write,	\
29 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
30 		.handle = (void *)stmdev_ctx_ptr			\
31 	}
32 
33 int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
34 		     uint8_t reg_addr, uint8_t *value, uint8_t len);
35 int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
36 		      uint8_t reg_addr, uint8_t *value, uint8_t len);
37 
38 /*
39  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
40  * specific stmemsc i2c APIs that set reg_addr MSB to '1' in order to allow
41  * multiple read/write operations. This is common in some STMEMSC drivers
42  */
43 #define STMEMSC_CTX_I2C_INCR(stmdev_ctx_ptr)				\
44 	.ctx = {							\
45 		.read_reg = (stmdev_read_ptr)stmemsc_i2c_read_incr,	\
46 		.write_reg = (stmdev_write_ptr)stmemsc_i2c_write_incr,	\
47 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
48 		.handle = (void *)stmdev_ctx_ptr			\
49 	}
50 
51 int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
52 			  uint8_t reg_addr, uint8_t *value, uint8_t len);
53 int stmemsc_i2c_write_incr(const struct i2c_dt_spec *stmemsc,
54 			   uint8_t reg_addr, uint8_t *value, uint8_t len);
55 
56 /*
57  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
58  * custom stmemsc i2c APIs specified by driver.
59  */
60 #define STMEMSC_CTX_I2C_CUSTOM(stmdev_ctx_ptr, i2c_rd_api, i2c_wr_api)	\
61 	.ctx = {							\
62 		.read_reg = (stmdev_read_ptr)i2c_rd_api,		\
63 		.write_reg = (stmdev_write_ptr)i2c_wr_api,		\
64 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
65 		.handle = (void *)stmdev_ctx_ptr			\
66 	}
67 
68 #endif
69 
70 #ifdef CONFIG_I3C
71 /*
72  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
73  * standard stmemsc i3c APIs.
74  */
75 #define STMEMSC_CTX_I3C(stmdev_ctx_ptr)					\
76 	.ctx = {							\
77 		.read_reg = (stmdev_read_ptr)stmemsc_i3c_read,		\
78 		.write_reg = (stmdev_write_ptr)stmemsc_i3c_write,	\
79 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
80 		.handle = (void *)stmdev_ctx_ptr			\
81 	}
82 
83 int stmemsc_i3c_read(void *stmemsc,
84 		     uint8_t reg_addr, uint8_t *value, uint8_t len);
85 int stmemsc_i3c_write(void *stmemsc,
86 		      uint8_t reg_addr, uint8_t *value, uint8_t len);
87 #endif
88 
89 #ifdef CONFIG_SPI
90 /*
91  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
92  * stmemsc spi APIs.
93  */
94 #define STMEMSC_CTX_SPI(stmdev_ctx_ptr)					\
95 	.ctx = {							\
96 		.read_reg = (stmdev_read_ptr)stmemsc_spi_read,		\
97 		.write_reg = (stmdev_write_ptr)stmemsc_spi_write,	\
98 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
99 		.handle = (void *)stmdev_ctx_ptr			\
100 	}
101 
102 int stmemsc_spi_read(const struct spi_dt_spec *stmemsc,
103 		     uint8_t reg_addr, uint8_t *value, uint8_t len);
104 int stmemsc_spi_write(const struct spi_dt_spec *stmemsc,
105 		      uint8_t reg_addr, uint8_t *value, uint8_t len);
106 
107 /*
108  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
109  * specific stmemsc sp APIs that set reg_addr bit6 to '1' in order to allow
110  * multiple read/write operations. This is common in some STMEMSC drivers
111  */
112 #define STMEMSC_CTX_SPI_INCR(stmdev_ctx_ptr)				\
113 	.ctx = {							\
114 		.read_reg = (stmdev_read_ptr)stmemsc_spi_read_incr,	\
115 		.write_reg = (stmdev_write_ptr)stmemsc_spi_write_incr,	\
116 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
117 		.handle = (void *)stmdev_ctx_ptr			\
118 	}
119 
120 int stmemsc_spi_read_incr(const struct spi_dt_spec *stmemsc,
121 			  uint8_t reg_addr, uint8_t *value, uint8_t len);
122 int stmemsc_spi_write_incr(const struct spi_dt_spec *stmemsc,
123 			   uint8_t reg_addr, uint8_t *value, uint8_t len);
124 
125 /*
126  * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
127  * custom stmemsc spi APIs specified by driver.
128  */
129 #define STMEMSC_CTX_SPI_CUSTOM(stmdev_ctx_ptr, spi_rd_api, spi_wr_api)	\
130 	.ctx = {							\
131 		.read_reg = (stmdev_read_ptr)spi_rd_api,		\
132 		.write_reg = (stmdev_write_ptr)spi_wr_api,		\
133 		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
134 		.handle = (void *)stmdev_ctx_ptr			\
135 	}
136 
137 #endif
138 #endif /* ZEPHYR_DRIVERS_SENSOR_STMEMSC_STMEMSC_H_ */
139