Commit dadac5d0 authored by Armando Visconti's avatar Armando Visconti Committed by Henrik Brix Andersen
Browse files

drivers/sensor: stmemsc: add new sets of i2c/spi APIs



Add APIs to:

    1. read/write sensor regs on i2c/spi bus enabling adrress
       auto-increment in a stmemsc specific way.
    2. read/write sensor custom APIs.

Signed-off-by: default avatarArmando Visconti <armando.visconti@st.com>
parent 6d657381
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,3 +9,4 @@ zephyr_library()
zephyr_library_sources_ifdef(CONFIG_I2C        stmemsc_i2c.c)
zephyr_library_sources_ifdef(CONFIG_I3C        stmemsc_i3c.c)
zephyr_library_sources_ifdef(CONFIG_SPI        stmemsc_spi.c)
zephyr_library_sources(stmemsc_mdelay.c)
+74 −16
Original line number Diff line number Diff line
@@ -15,16 +15,12 @@
#include <zephyr/drivers/i3c.h>
#include <zephyr/drivers/spi.h>


static inline void stmemsc_mdelay(uint32_t millisec)
{
	k_msleep(millisec);
}
void stmemsc_mdelay(uint32_t millisec);

#ifdef CONFIG_I2C
/*
 * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
 * stmemsc i2c APIs.
 * standard stmemsc i2c APIs.
 */
#define STMEMSC_CTX_I2C(stmdev_ctx_ptr)					\
	.ctx = {							\
@@ -38,12 +34,43 @@ int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
		     uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
		      uint8_t reg_addr, uint8_t *value, uint8_t len);

/*
 * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
 * specific stmemsc i2c APIs that set reg_addr MSB to '1' in order to allow
 * multiple read/write operations. This is common in some STMEMSC drivers
 */
#define STMEMSC_CTX_I2C_INCR(stmdev_ctx_ptr)				\
	.ctx = {							\
		.read_reg = (stmdev_read_ptr)stmemsc_i2c_read_incr,	\
		.write_reg = (stmdev_write_ptr)stmemsc_i2c_write_incr,	\
		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
		.handle = (void *)stmdev_ctx_ptr			\
	}

int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
			  uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_i2c_write_incr(const struct i2c_dt_spec *stmemsc,
			   uint8_t reg_addr, uint8_t *value, uint8_t len);

/*
 * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
 * custom stmemsc i2c APIs specified by driver.
 */
#define STMEMSC_CTX_I2C_CUSTOM(stmdev_ctx_ptr, i2c_rd_api, i2c_wr_api)	\
	.ctx = {							\
		.read_reg = (stmdev_read_ptr)i2c_rd_api,		\
		.write_reg = (stmdev_write_ptr)i2c_wr_api,		\
		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
		.handle = (void *)stmdev_ctx_ptr			\
	}

#endif

#ifdef CONFIG_I3C
/*
 * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
 * stmemsc i3c APIs.
 * standard stmemsc i3c APIs.
 */
#define STMEMSC_CTX_I3C(stmdev_ctx_ptr)					\
	.ctx = {							\
@@ -76,5 +103,36 @@ int stmemsc_spi_read(const struct spi_dt_spec *stmemsc,
		     uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_spi_write(const struct spi_dt_spec *stmemsc,
		      uint8_t reg_addr, uint8_t *value, uint8_t len);

/*
 * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
 * specific stmemsc sp APIs that set reg_addr bit6 to '1' in order to allow
 * multiple read/write operations. This is common in some STMEMSC drivers
 */
#define STMEMSC_CTX_SPI_INCR(stmdev_ctx_ptr)				\
	.ctx = {							\
		.read_reg = (stmdev_read_ptr)stmemsc_spi_read_incr,	\
		.write_reg = (stmdev_write_ptr)stmemsc_spi_write_incr,	\
		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
		.handle = (void *)stmdev_ctx_ptr			\
	}

int stmemsc_spi_read_incr(const struct spi_dt_spec *stmemsc,
			  uint8_t reg_addr, uint8_t *value, uint8_t len);
int stmemsc_spi_write_incr(const struct spi_dt_spec *stmemsc,
			   uint8_t reg_addr, uint8_t *value, uint8_t len);

/*
 * Populate the stmdev_ctx_t structure pointed by stmdev_ctx_ptr with
 * custom stmemsc spi APIs specified by driver.
 */
#define STMEMSC_CTX_SPI_CUSTOM(stmdev_ctx_ptr, spi_rd_api, spi_wr_api)	\
	.ctx = {							\
		.read_reg = (stmdev_read_ptr)spi_rd_api,		\
		.write_reg = (stmdev_write_ptr)spi_wr_api,		\
		.mdelay = (stmdev_mdelay_ptr)stmemsc_mdelay,		\
		.handle = (void *)stmdev_ctx_ptr			\
	}

#endif
#endif /* ZEPHYR_DRIVERS_SENSOR_STMEMSC_STMEMSC_H_ */
+19 −2
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@

#include "stmemsc.h"

 /* Enable address auto-increment on some stmemsc sensors */
#define STMEMSC_I2C_ADDR_AUTO_INCR		BIT(7)

int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
		     uint8_t reg_addr, uint8_t *value, uint8_t len)
{
@@ -20,3 +23,17 @@ int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
{
	return i2c_burst_write_dt(stmemsc, reg_addr, value, len);
}

int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
			  uint8_t reg_addr, uint8_t *value, uint8_t len)
{
	reg_addr |= STMEMSC_I2C_ADDR_AUTO_INCR;
	return stmemsc_i2c_read(stmemsc, reg_addr, value, len);
}

int stmemsc_i2c_write_incr(const struct i2c_dt_spec *stmemsc,
			   uint8_t reg_addr, uint8_t *value, uint8_t len)
{
	reg_addr |= STMEMSC_I2C_ADDR_AUTO_INCR;
	return stmemsc_i2c_write(stmemsc, reg_addr, value, len);
}
+15 −0
Original line number Diff line number Diff line
/* ST Microelectronics STMEMS hal i/f
 *
 * Copyright (c) 2024 STMicroelectronics
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * zephyrproject-rtos/modules/hal/st/sensor/stmemsc/
 */

#include "stmemsc.h"

void stmemsc_mdelay(uint32_t millisec)
{
	k_msleep(millisec);
}
+17 −0
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@

#define SPI_READ		(1 << 7)

 /* Enable address auto-increment on some stmemsc sensors */
#define STMEMSC_SPI_ADDR_AUTO_INCR		BIT(6)

/*
 * SPI read
 */
@@ -56,3 +59,17 @@ int stmemsc_spi_write(const struct spi_dt_spec *stmemsc,

	return spi_write_dt(stmemsc, &tx);
}

int stmemsc_spi_read_incr(const struct spi_dt_spec *stmemsc,
			  uint8_t reg_addr, uint8_t *value, uint8_t len)
{
	reg_addr |= STMEMSC_SPI_ADDR_AUTO_INCR;
	return stmemsc_spi_read(stmemsc, reg_addr, value, len);
}

int stmemsc_spi_write_incr(const struct spi_dt_spec *stmemsc,
			   uint8_t reg_addr, uint8_t *value, uint8_t len)
{
	reg_addr |= STMEMSC_SPI_ADDR_AUTO_INCR;
	return stmemsc_spi_write(stmemsc, reg_addr, value, len);
}