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_driver_phandle_args *a, 15 TEE_Result *res) 16 { 17 struct gpio *gpio = NULL; 18 19 if (a->args_count != 2) { 20 *res = TEE_ERROR_BAD_PARAMETERS; 21 return NULL; 22 } 23 24 gpio = calloc(1, sizeof(struct gpio)); 25 if (!gpio) { 26 *res = TEE_ERROR_OUT_OF_MEMORY; 27 return NULL; 28 } 29 30 gpio->pin = a->args[0]; 31 gpio->dt_flags = a->args[1]; 32 33 *res = TEE_SUCCESS; 34 return gpio; 35 } 36 37 static char *gpio_get_dt_prop_name(const char *gpio_name) 38 { 39 char *prop_name = NULL; 40 int max_len = strlen(gpio_name) + strlen("-gpios") + 1; 41 42 prop_name = calloc(1, max_len); 43 if (!prop_name) 44 return NULL; 45 46 snprintf(prop_name, max_len, "%s-gpios", gpio_name); 47 48 return prop_name; 49 } 50 51 TEE_Result gpio_dt_get_by_index(const void *fdt, int nodeoffset, 52 unsigned int index, const char *gpio_name, 53 struct gpio **gpio) 54 { 55 TEE_Result res = TEE_ERROR_GENERIC; 56 char *prop_name = NULL; 57 58 prop_name = gpio_get_dt_prop_name(gpio_name); 59 if (!prop_name) 60 return TEE_ERROR_OUT_OF_MEMORY; 61 62 *gpio = dt_driver_device_from_node_idx_prop(prop_name, fdt, nodeoffset, 63 index, DT_DRIVER_GPIO, 64 &res); 65 free(prop_name); 66 67 return res; 68 } 69