1 /* 2 * Copyright 2021 Google LLC. 3 * SPDX-License-Identifier: Apache-2.0 4 */ 5 6 #ifndef DRIVERS_SYSCON_SYSCON_COMMON_H_ 7 #define DRIVERS_SYSCON_SYSCON_COMMON_H_ 8 9 #include <zephyr/sys/util.h> 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 15 /** 16 * @brief Align and check register address 17 * 18 * @param reg Pointer to the register address in question. 19 * @param reg_size The size of the syscon register region. 20 * @param reg_width The width of a single register (in bytes). 21 * @return 0 if the register read is valid. 22 * @return -EINVAL is the read is invalid. 23 */ syscon_sanitize_reg(uint16_t * reg,size_t reg_size,uint8_t reg_width)24static inline int syscon_sanitize_reg(uint16_t *reg, size_t reg_size, uint8_t reg_width) 25 { 26 /* Avoid unaligned readings */ 27 *reg = ROUND_DOWN(*reg, reg_width); 28 29 /* Check for out-of-bounds readings */ 30 if (*reg >= reg_size) { 31 return -EINVAL; 32 } 33 34 return 0; 35 } 36 37 #ifdef __cplusplus 38 } 39 #endif 40 41 #endif /* DRIVERS_SYSCON_SYSCON_COMMON_H_ */ 42