soc: nrf: add DT GPIO helper macros
Add some helper macros that will be convenient to use from device drivers for accessing and error checking pin mux information in the devicetree: - NRF_DT_GPIOS_TO_PSEL(): converts a devicetree GPIO phandle <&gpioX pin flags> to the "PSEL style" registers found in nRF SoC family IPs. The conversion is done at build time and relies on devicetree properties. - NRF_DT_CHECK_GPIO_CTLR_IS_SOC(): make sure a GPIO controller in a phandle-array property is one of the SoC GPIO controllers by checking its compatible is nordic,nrf-gpio. See comments in the patch for more details. Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
parent
e031bc4b48
commit
dbad2627c8
|
@ -13,6 +13,68 @@
|
|||
#ifndef _ASMLANGUAGE
|
||||
#include <nrfx.h>
|
||||
#include <devicetree.h>
|
||||
#include <toolchain.h>
|
||||
|
||||
/**
|
||||
* @brief Convert a devicetree GPIO phandle+specifier to PSEL value
|
||||
*
|
||||
* Various peripherals in nRF SoCs have pin select registers, which
|
||||
* usually have PSEL in their names. The low bits of these registers
|
||||
* generally look like this in the register map description:
|
||||
*
|
||||
* Bit number 5 4 3 2 1 0
|
||||
* ID B A A A A A
|
||||
*
|
||||
* ID Field Value Description
|
||||
* A PIN [0..31] Pin number
|
||||
* B PORT [0..1] Port number
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* - pin P0.4 has "PSEL value" 4 (B=0 and A=4)
|
||||
* - pin P1.5 has "PSEL value" 37 (B=1 and A=5)
|
||||
*
|
||||
* This macro converts a devicetree GPIO phandle array value
|
||||
* "<&gpioX pin ...>" to a "PSEL value".
|
||||
*
|
||||
* Note: in Nordic SoC devicetrees, "gpio0" means P0, and "gpio1"
|
||||
* means P1. This is encoded in the "port" property of each GPIO node.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* foo: my-node {
|
||||
* tx-gpios = <&gpio0 4 ...>;
|
||||
* rx-gpios = <&gpio1 5 ...>;
|
||||
* };
|
||||
*
|
||||
* NRF_DT_GPIOS_TO_PSEL(DT_NODELABEL(foo), tx_gpios) // 0 + 4 = 4
|
||||
* NRF_DT_GPIOS_TO_PSEL(DT_NODELABEL(foo), rx_gpios) // 32 + 5 = 37
|
||||
*/
|
||||
#define NRF_DT_GPIOS_TO_PSEL(node_id, prop) \
|
||||
(DT_GPIO_PIN(node_id, prop) + \
|
||||
(DT_PROP_BY_PHANDLE(node_id, prop, port) << 5))
|
||||
|
||||
/**
|
||||
* Error out the build if 'prop' is set on node 'node_id' and
|
||||
* DT_GPIO_CTLR(node_id, prop) is not an SoC GPIO controller,
|
||||
* i.e. a node with compatible "nordic,nrf-gpio".
|
||||
*
|
||||
* Otherwise, do nothing.
|
||||
*
|
||||
* @param node_id node identifier
|
||||
* @param prop lowercase-and-underscores PSEL style property
|
||||
* @param prop_name human-readable string name for 'prop'
|
||||
*/
|
||||
#define NRF_DT_CHECK_GPIO_CTLR_IS_SOC(node_id, prop, prop_name) \
|
||||
BUILD_ASSERT(!DT_NODE_HAS_PROP(node_id, prop) || \
|
||||
DT_NODE_HAS_COMPAT(DT_GPIO_CTLR(node_id, prop), \
|
||||
nordic_nrf_gpio), \
|
||||
"Devicetree node " DT_NODE_PATH(node_id) \
|
||||
" property " prop_name " must refer to a GPIO " \
|
||||
" controller with compatible nordic,nrf-gpio; " \
|
||||
"got " DT_NODE_PATH(DT_GPIO_CTLR(node_id, prop)) \
|
||||
", which does not have this compatible")
|
||||
|
||||
#endif /* !_ASMLANGUAGE */
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue