pinctrl-utils.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Utils functions to implement the pincontrol driver.
  3. *
  4. * Copyright (c) 2013, NVIDIA Corporation.
  5. *
  6. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  13. * whether express or implied; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307, USA
  21. */
  22. #include <linux/device.h>
  23. #include <linux/export.h>
  24. #include <linux/kernel.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/of.h>
  27. #include <linux/slab.h>
  28. #include "core.h"
  29. #include "pinctrl-utils.h"
  30. int pinctrl_utils_reserve_map(struct pinctrl_dev *pctldev,
  31. struct pinctrl_map **map, unsigned *reserved_maps,
  32. unsigned *num_maps, unsigned reserve)
  33. {
  34. unsigned old_num = *reserved_maps;
  35. unsigned new_num = *num_maps + reserve;
  36. struct pinctrl_map *new_map;
  37. if (old_num >= new_num)
  38. return 0;
  39. new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
  40. if (!new_map) {
  41. dev_err(pctldev->dev, "krealloc(map) failed\n");
  42. return -ENOMEM;
  43. }
  44. memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
  45. *map = new_map;
  46. *reserved_maps = new_num;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(pinctrl_utils_reserve_map);
  50. int pinctrl_utils_add_map_mux(struct pinctrl_dev *pctldev,
  51. struct pinctrl_map **map, unsigned *reserved_maps,
  52. unsigned *num_maps, const char *group,
  53. const char *function)
  54. {
  55. if (WARN_ON(*num_maps == *reserved_maps))
  56. return -ENOSPC;
  57. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  58. (*map)[*num_maps].data.mux.group = group;
  59. (*map)[*num_maps].data.mux.function = function;
  60. (*num_maps)++;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_mux);
  64. int pinctrl_utils_add_map_configs(struct pinctrl_dev *pctldev,
  65. struct pinctrl_map **map, unsigned *reserved_maps,
  66. unsigned *num_maps, const char *group,
  67. unsigned long *configs, unsigned num_configs,
  68. enum pinctrl_map_type type)
  69. {
  70. unsigned long *dup_configs;
  71. if (WARN_ON(*num_maps == *reserved_maps))
  72. return -ENOSPC;
  73. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  74. GFP_KERNEL);
  75. if (!dup_configs) {
  76. dev_err(pctldev->dev, "kmemdup(configs) failed\n");
  77. return -ENOMEM;
  78. }
  79. (*map)[*num_maps].type = type;
  80. (*map)[*num_maps].data.configs.group_or_pin = group;
  81. (*map)[*num_maps].data.configs.configs = dup_configs;
  82. (*map)[*num_maps].data.configs.num_configs = num_configs;
  83. (*num_maps)++;
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_GPL(pinctrl_utils_add_map_configs);
  87. int pinctrl_utils_add_config(struct pinctrl_dev *pctldev,
  88. unsigned long **configs, unsigned *num_configs,
  89. unsigned long config)
  90. {
  91. unsigned old_num = *num_configs;
  92. unsigned new_num = old_num + 1;
  93. unsigned long *new_configs;
  94. new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
  95. GFP_KERNEL);
  96. if (!new_configs) {
  97. dev_err(pctldev->dev, "krealloc(configs) failed\n");
  98. return -ENOMEM;
  99. }
  100. new_configs[old_num] = config;
  101. *configs = new_configs;
  102. *num_configs = new_num;
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(pinctrl_utils_add_config);
  106. void pinctrl_utils_dt_free_map(struct pinctrl_dev *pctldev,
  107. struct pinctrl_map *map, unsigned num_maps)
  108. {
  109. int i;
  110. for (i = 0; i < num_maps; i++) {
  111. switch (map[i].type) {
  112. case PIN_MAP_TYPE_CONFIGS_GROUP:
  113. case PIN_MAP_TYPE_CONFIGS_PIN:
  114. kfree(map[i].data.configs.configs);
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. kfree(map);
  121. }
  122. EXPORT_SYMBOL_GPL(pinctrl_utils_dt_free_map);