Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Utils functions to implement the pincontrol driver.
0004  *
0005  * Copyright (c) 2013, NVIDIA Corporation.
0006  *
0007  * Author: Laxman Dewangan <ldewangan@nvidia.com>
0008  */
0009 #include <linux/device.h>
0010 #include <linux/export.h>
0011 #include <linux/kernel.h>
0012 #include <linux/pinctrl/pinctrl.h>
0013 #include <linux/of.h>
0014 #include <linux/slab.h>
0015 #include "core.h"
0016 #include "pinctrl-utils.h"
0017 
0018 int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,
0019         struct pinctrl_map **map, unsigned *reserved_maps,
0020         unsigned *num_maps, unsigned reserve)
0021 {
0022     unsigned old_num = *reserved_maps;
0023     unsigned new_num = *num_maps + reserve;
0024     struct pinctrl_map *new_map;
0025 
0026     if (old_num >= new_num)
0027         return 0;
0028 
0029     new_map = krealloc_array(*map, new_num, sizeof(*new_map), GFP_KERNEL);
0030     if (!new_map) {
0031         dev_err(pctldev->dev, "krealloc(map) failed\n");
0032         return -ENOMEM;
0033     }
0034 
0035     memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
0036 
0037     *map = new_map;
0038     *reserved_maps = new_num;
0039     return 0;
0040 }
0041 EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map);
0042 
0043 int pinctrl_utils_add_map_mux(struct pinctrl_dev *pctldev,
0044         struct pinctrl_map **map, unsigned *reserved_maps,
0045         unsigned *num_maps, const char *group,
0046         const char *function)
0047 {
0048     if (WARN_ON(*num_maps == *reserved_maps))
0049         return -ENOSPC;
0050 
0051     (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
0052     (*map)[*num_maps].data.mux.group = group;
0053     (*map)[*num_maps].data.mux.function = function;
0054     (*num_maps)++;
0055 
0056     return 0;
0057 }
0058 EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux);
0059 
0060 int pinctrl_utils_add_map_configs(struct pinctrl_dev *pctldev,
0061         struct pinctrl_map **map, unsigned *reserved_maps,
0062         unsigned *num_maps, const char *group,
0063         unsigned long *configs, unsigned num_configs,
0064         enum pinctrl_map_type type)
0065 {
0066     unsigned long *dup_configs;
0067 
0068     if (WARN_ON(*num_maps == *reserved_maps))
0069         return -ENOSPC;
0070 
0071     dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
0072                   GFP_KERNEL);
0073     if (!dup_configs)
0074         return -ENOMEM;
0075 
0076     (*map)[*num_maps].type = type;
0077     (*map)[*num_maps].data.configs.group_or_pin = group;
0078     (*map)[*num_maps].data.configs.configs = dup_configs;
0079     (*map)[*num_maps].data.configs.num_configs = num_configs;
0080     (*num_maps)++;
0081 
0082     return 0;
0083 }
0084 EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs);
0085 
0086 int pinctrl_utils_add_config(struct pinctrl_dev *pctldev,
0087         unsigned long **configs, unsigned *num_configs,
0088         unsigned long config)
0089 {
0090     unsigned old_num = *num_configs;
0091     unsigned new_num = old_num + 1;
0092     unsigned long *new_configs;
0093 
0094     new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
0095                    GFP_KERNEL);
0096     if (!new_configs) {
0097         dev_err(pctldev->dev, "krealloc(configs) failed\n");
0098         return -ENOMEM;
0099     }
0100 
0101     new_configs[old_num] = config;
0102 
0103     *configs = new_configs;
0104     *num_configs = new_num;
0105 
0106     return 0;
0107 }
0108 EXPORT_SYMBOL_GPL(pinctrl_utils_add_config);
0109 
0110 void pinctrl_utils_free_map(struct pinctrl_dev *pctldev,
0111           struct pinctrl_map *map, unsigned num_maps)
0112 {
0113     int i;
0114 
0115     for (i = 0; i < num_maps; i++) {
0116         switch (map[i].type) {
0117         case PIN_MAP_TYPE_CONFIGS_GROUP:
0118         case PIN_MAP_TYPE_CONFIGS_PIN:
0119             kfree(map[i].data.configs.configs);
0120             break;
0121         default:
0122             break;
0123         }
0124     }
0125     kfree(map);
0126 }
0127 EXPORT_SYMBOL_GPL(pinctrl_utils_free_map);