1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2022, Microchip 4 */ 5 6 #include <drivers/gpio.h> 7 #include <libfdt.h> 8 #include <stdio.h> 9 #include <tee_api_defines.h> 10 #include <tee_api_defines_extensions.h> 11 #include <tee_api_types.h> 12 #include <util.h> 13 14 struct gpio *gpio_dt_alloc_pin(struct dt_pargs *pargs, TEE_Result *res) 15 { 16 struct gpio *gpio = NULL; 17 18 if (pargs->args_count != 2) { 19 *res = TEE_ERROR_BAD_PARAMETERS; 20 return NULL; 21 } 22 23 gpio = calloc(1, sizeof(struct gpio)); 24 if (!gpio) { 25 *res = TEE_ERROR_OUT_OF_MEMORY; 26 return NULL; 27 } 28 29 gpio->pin = pargs->args[0]; 30 gpio->dt_flags = pargs->args[1]; 31 32 *res = TEE_SUCCESS; 33 return gpio; 34 } 35 36 static char *gpio_get_dt_prop_name(const char *gpio_name) 37 { 38 char *prop_name = NULL; 39 int max_len = strlen(gpio_name) + strlen("-gpios") + 1; 40 41 prop_name = calloc(1, max_len); 42 if (!prop_name) 43 return NULL; 44 45 snprintf(prop_name, max_len, "%s-gpios", gpio_name); 46 47 return prop_name; 48 } 49 50 TEE_Result gpio_dt_get_by_index(const void *fdt, int nodeoffset, 51 unsigned int index, const char *gpio_name, 52 struct gpio **gpio) 53 { 54 TEE_Result res = TEE_ERROR_GENERIC; 55 char *prop_name = NULL; 56 57 prop_name = gpio_get_dt_prop_name(gpio_name); 58 if (!prop_name) 59 return TEE_ERROR_OUT_OF_MEMORY; 60 61 *gpio = dt_driver_device_from_node_idx_prop(prop_name, fdt, nodeoffset, 62 index, DT_DRIVER_GPIO, 63 &res); 64 free(prop_name); 65 66 return res; 67 } 68