gpio_base.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright 2012-15 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. /*
  26. * Pre-requisites: headers required by header of this unit
  27. */
  28. #include "dm_services.h"
  29. #include "include/gpio_interface.h"
  30. #include "include/gpio_service_interface.h"
  31. #include "hw_gpio.h"
  32. #include "hw_translate.h"
  33. #include "hw_factory.h"
  34. #include "gpio_service.h"
  35. /*
  36. * Post-requisites: headers required by this unit
  37. */
  38. /*
  39. * This unit
  40. */
  41. /*
  42. * @brief
  43. * Public API
  44. */
  45. enum gpio_result dal_gpio_open(
  46. struct gpio *gpio,
  47. enum gpio_mode mode)
  48. {
  49. return dal_gpio_open_ex(gpio, mode);
  50. }
  51. enum gpio_result dal_gpio_open_ex(
  52. struct gpio *gpio,
  53. enum gpio_mode mode)
  54. {
  55. if (gpio->pin) {
  56. ASSERT_CRITICAL(false);
  57. return GPIO_RESULT_ALREADY_OPENED;
  58. }
  59. gpio->mode = mode;
  60. return dal_gpio_service_open(
  61. gpio->service, gpio->id, gpio->en, mode, &gpio->pin);
  62. }
  63. enum gpio_result dal_gpio_get_value(
  64. const struct gpio *gpio,
  65. uint32_t *value)
  66. {
  67. if (!gpio->pin) {
  68. BREAK_TO_DEBUGGER();
  69. return GPIO_RESULT_NULL_HANDLE;
  70. }
  71. return gpio->pin->funcs->get_value(gpio->pin, value);
  72. }
  73. enum gpio_result dal_gpio_set_value(
  74. const struct gpio *gpio,
  75. uint32_t value)
  76. {
  77. if (!gpio->pin) {
  78. BREAK_TO_DEBUGGER();
  79. return GPIO_RESULT_NULL_HANDLE;
  80. }
  81. return gpio->pin->funcs->set_value(gpio->pin, value);
  82. }
  83. enum gpio_mode dal_gpio_get_mode(
  84. const struct gpio *gpio)
  85. {
  86. return gpio->mode;
  87. }
  88. enum gpio_result dal_gpio_change_mode(
  89. struct gpio *gpio,
  90. enum gpio_mode mode)
  91. {
  92. if (!gpio->pin) {
  93. BREAK_TO_DEBUGGER();
  94. return GPIO_RESULT_NULL_HANDLE;
  95. }
  96. return gpio->pin->funcs->change_mode(gpio->pin, mode);
  97. }
  98. enum gpio_id dal_gpio_get_id(
  99. const struct gpio *gpio)
  100. {
  101. return gpio->id;
  102. }
  103. uint32_t dal_gpio_get_enum(
  104. const struct gpio *gpio)
  105. {
  106. return gpio->en;
  107. }
  108. enum gpio_result dal_gpio_set_config(
  109. struct gpio *gpio,
  110. const struct gpio_config_data *config_data)
  111. {
  112. if (!gpio->pin) {
  113. BREAK_TO_DEBUGGER();
  114. return GPIO_RESULT_NULL_HANDLE;
  115. }
  116. return gpio->pin->funcs->set_config(gpio->pin, config_data);
  117. }
  118. enum gpio_result dal_gpio_get_pin_info(
  119. const struct gpio *gpio,
  120. struct gpio_pin_info *pin_info)
  121. {
  122. return gpio->service->translate.funcs->id_to_offset(
  123. gpio->id, gpio->en, pin_info) ?
  124. GPIO_RESULT_OK : GPIO_RESULT_INVALID_DATA;
  125. }
  126. enum sync_source dal_gpio_get_sync_source(
  127. const struct gpio *gpio)
  128. {
  129. switch (gpio->id) {
  130. case GPIO_ID_GENERIC:
  131. switch (gpio->en) {
  132. case GPIO_GENERIC_A:
  133. return SYNC_SOURCE_IO_GENERIC_A;
  134. case GPIO_GENERIC_B:
  135. return SYNC_SOURCE_IO_GENERIC_B;
  136. case GPIO_GENERIC_C:
  137. return SYNC_SOURCE_IO_GENERIC_C;
  138. case GPIO_GENERIC_D:
  139. return SYNC_SOURCE_IO_GENERIC_D;
  140. case GPIO_GENERIC_E:
  141. return SYNC_SOURCE_IO_GENERIC_E;
  142. case GPIO_GENERIC_F:
  143. return SYNC_SOURCE_IO_GENERIC_F;
  144. default:
  145. return SYNC_SOURCE_NONE;
  146. }
  147. break;
  148. case GPIO_ID_SYNC:
  149. switch (gpio->en) {
  150. case GPIO_SYNC_HSYNC_A:
  151. return SYNC_SOURCE_IO_HSYNC_A;
  152. case GPIO_SYNC_VSYNC_A:
  153. return SYNC_SOURCE_IO_VSYNC_A;
  154. case GPIO_SYNC_HSYNC_B:
  155. return SYNC_SOURCE_IO_HSYNC_B;
  156. case GPIO_SYNC_VSYNC_B:
  157. return SYNC_SOURCE_IO_VSYNC_B;
  158. default:
  159. return SYNC_SOURCE_NONE;
  160. }
  161. break;
  162. case GPIO_ID_HPD:
  163. switch (gpio->en) {
  164. case GPIO_HPD_1:
  165. return SYNC_SOURCE_IO_HPD1;
  166. case GPIO_HPD_2:
  167. return SYNC_SOURCE_IO_HPD2;
  168. default:
  169. return SYNC_SOURCE_NONE;
  170. }
  171. break;
  172. case GPIO_ID_GSL:
  173. switch (gpio->en) {
  174. case GPIO_GSL_GENLOCK_CLOCK:
  175. return SYNC_SOURCE_GSL_IO_GENLOCK_CLOCK;
  176. case GPIO_GSL_GENLOCK_VSYNC:
  177. return SYNC_SOURCE_GSL_IO_GENLOCK_VSYNC;
  178. case GPIO_GSL_SWAPLOCK_A:
  179. return SYNC_SOURCE_GSL_IO_SWAPLOCK_A;
  180. case GPIO_GSL_SWAPLOCK_B:
  181. return SYNC_SOURCE_GSL_IO_SWAPLOCK_B;
  182. default:
  183. return SYNC_SOURCE_NONE;
  184. }
  185. break;
  186. default:
  187. return SYNC_SOURCE_NONE;
  188. }
  189. }
  190. enum gpio_pin_output_state dal_gpio_get_output_state(
  191. const struct gpio *gpio)
  192. {
  193. return gpio->output_state;
  194. }
  195. void dal_gpio_close(
  196. struct gpio *gpio)
  197. {
  198. if (!gpio)
  199. return;
  200. dal_gpio_service_close(gpio->service, &gpio->pin);
  201. gpio->mode = GPIO_MODE_UNKNOWN;
  202. }
  203. /*
  204. * @brief
  205. * Creation and destruction
  206. */
  207. struct gpio *dal_gpio_create(
  208. struct gpio_service *service,
  209. enum gpio_id id,
  210. uint32_t en,
  211. enum gpio_pin_output_state output_state)
  212. {
  213. struct gpio *gpio = kzalloc(sizeof(struct gpio), GFP_KERNEL);
  214. if (!gpio) {
  215. ASSERT_CRITICAL(false);
  216. return NULL;
  217. }
  218. gpio->service = service;
  219. gpio->pin = NULL;
  220. gpio->id = id;
  221. gpio->en = en;
  222. gpio->mode = GPIO_MODE_UNKNOWN;
  223. gpio->output_state = output_state;
  224. return gpio;
  225. }
  226. void dal_gpio_destroy(
  227. struct gpio **gpio)
  228. {
  229. if (!gpio || !*gpio) {
  230. ASSERT_CRITICAL(false);
  231. return;
  232. }
  233. dal_gpio_close(*gpio);
  234. kfree(*gpio);
  235. *gpio = NULL;
  236. }