pinctrl-sandbox.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
  4. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <dm/pinctrl.h>
  9. #include <dt-bindings/pinctrl/sandbox-pinmux.h>
  10. #include <log.h>
  11. #include <linux/bitops.h>
  12. /*
  13. * This driver emulates a pin controller with the following rules:
  14. * - The pinctrl config for each pin must be set individually
  15. * - The first three pins (P0-P2) must be muxed as a group
  16. * - The next two pins (P3-P4) must be muxed as a group
  17. * - The last four pins (P5-P8) must be muxed individually
  18. */
  19. static const char * const sandbox_pins[] = {
  20. #define PIN(x) \
  21. [x] = "P" #x
  22. PIN(0),
  23. PIN(1),
  24. PIN(2),
  25. PIN(3),
  26. PIN(4),
  27. PIN(5),
  28. PIN(6),
  29. PIN(7),
  30. PIN(8),
  31. #undef PIN
  32. };
  33. static const char * const sandbox_pins_muxing[][2] = {
  34. { "UART TX", "I2C SCL" },
  35. { "UART RX", "I2C SDA" },
  36. { "SPI SCLK", "I2S SCK" },
  37. { "SPI MOSI", "I2S SD" },
  38. { "SPI MISO", "I2S WS" },
  39. { "GPIO0", "SPI CS0" },
  40. { "GPIO1", "SPI CS1" },
  41. { "GPIO2", "PWM0" },
  42. { "GPIO3", "PWM1" },
  43. };
  44. #define SANDBOX_GROUP_I2C_UART 0
  45. #define SANDBOX_GROUP_SPI_I2S 1
  46. static const char * const sandbox_groups[] = {
  47. [SANDBOX_GROUP_I2C_UART] = "I2C_UART",
  48. [SANDBOX_GROUP_SPI_I2S] = "SPI_I2S",
  49. };
  50. static const char * const sandbox_functions[] = {
  51. #define FUNC(id) \
  52. [SANDBOX_PINMUX_##id] = #id
  53. FUNC(UART),
  54. FUNC(I2C),
  55. FUNC(SPI),
  56. FUNC(I2S),
  57. FUNC(GPIO),
  58. FUNC(CS),
  59. FUNC(PWM),
  60. #undef FUNC
  61. };
  62. static const struct pinconf_param sandbox_conf_params[] = {
  63. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  64. { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
  65. { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
  66. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  67. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  68. { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
  69. { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
  70. { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
  71. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  72. { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
  73. { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
  74. };
  75. /* Bitfield used to save param and value of each pin/selector */
  76. struct sandbox_pinctrl_priv {
  77. unsigned int mux;
  78. unsigned int pins_param[ARRAY_SIZE(sandbox_pins)];
  79. unsigned int pins_value[ARRAY_SIZE(sandbox_pins)];
  80. };
  81. static int sandbox_get_pins_count(struct udevice *dev)
  82. {
  83. return ARRAY_SIZE(sandbox_pins);
  84. }
  85. static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
  86. {
  87. return sandbox_pins[selector];
  88. }
  89. static int sandbox_get_pin_muxing(struct udevice *dev,
  90. unsigned int selector,
  91. char *buf, int size)
  92. {
  93. const struct pinconf_param *p;
  94. struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
  95. int i;
  96. snprintf(buf, size, "%s",
  97. sandbox_pins_muxing[selector][!!(priv->mux & BIT(selector))]);
  98. if (priv->pins_param[selector]) {
  99. for (i = 0, p = sandbox_conf_params;
  100. i < ARRAY_SIZE(sandbox_conf_params);
  101. i++, p++) {
  102. if ((priv->pins_param[selector] & BIT(p->param)) &&
  103. (!!(priv->pins_value[selector] & BIT(p->param)) ==
  104. p->default_value)) {
  105. strncat(buf, " ", size);
  106. strncat(buf, p->property, size);
  107. }
  108. }
  109. }
  110. strncat(buf, ".", size);
  111. return 0;
  112. }
  113. static int sandbox_get_groups_count(struct udevice *dev)
  114. {
  115. return ARRAY_SIZE(sandbox_groups);
  116. }
  117. static const char *sandbox_get_group_name(struct udevice *dev,
  118. unsigned selector)
  119. {
  120. return sandbox_groups[selector];
  121. }
  122. static int sandbox_get_functions_count(struct udevice *dev)
  123. {
  124. return ARRAY_SIZE(sandbox_functions);
  125. }
  126. static const char *sandbox_get_function_name(struct udevice *dev,
  127. unsigned selector)
  128. {
  129. return sandbox_functions[selector];
  130. }
  131. static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
  132. unsigned func_selector)
  133. {
  134. int mux;
  135. struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
  136. debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
  137. pin_selector, sandbox_get_pin_name(dev, pin_selector),
  138. func_selector, sandbox_get_function_name(dev, func_selector));
  139. if (pin_selector < 5)
  140. return -EINVAL;
  141. switch (func_selector) {
  142. case SANDBOX_PINMUX_GPIO:
  143. mux = 0;
  144. break;
  145. case SANDBOX_PINMUX_CS:
  146. case SANDBOX_PINMUX_PWM:
  147. mux = BIT(pin_selector);
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. priv->mux &= ~BIT(pin_selector);
  153. priv->mux |= mux;
  154. priv->pins_param[pin_selector] = 0;
  155. priv->pins_value[pin_selector] = 0;
  156. return 0;
  157. }
  158. static int sandbox_pinmux_group_set(struct udevice *dev,
  159. unsigned group_selector,
  160. unsigned func_selector)
  161. {
  162. bool mux;
  163. int i, group_start, group_end;
  164. struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
  165. unsigned int mask;
  166. debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
  167. group_selector, sandbox_get_group_name(dev, group_selector),
  168. func_selector, sandbox_get_function_name(dev, func_selector));
  169. if (group_selector == SANDBOX_GROUP_I2C_UART) {
  170. group_start = 0;
  171. group_end = 1;
  172. if (func_selector == SANDBOX_PINMUX_UART)
  173. mux = false;
  174. else if (func_selector == SANDBOX_PINMUX_I2C)
  175. mux = true;
  176. else
  177. return -EINVAL;
  178. } else if (group_selector == SANDBOX_GROUP_SPI_I2S) {
  179. group_start = 2;
  180. group_end = 4;
  181. if (func_selector == SANDBOX_PINMUX_SPI)
  182. mux = false;
  183. else if (func_selector == SANDBOX_PINMUX_I2S)
  184. mux = true;
  185. else
  186. return -EINVAL;
  187. } else {
  188. return -EINVAL;
  189. }
  190. mask = GENMASK(group_end, group_start);
  191. priv->mux &= ~mask;
  192. priv->mux |= mux ? mask : 0;
  193. for (i = group_start; i < group_end; i++) {
  194. priv->pins_param[i] = 0;
  195. priv->pins_value[i] = 0;
  196. }
  197. return 0;
  198. }
  199. static int sandbox_pinmux_property_set(struct udevice *dev, u32 pinmux_group)
  200. {
  201. int ret;
  202. unsigned pin_selector = pinmux_group & 0xFFFF;
  203. unsigned func_selector = pinmux_group >> 16;
  204. ret = sandbox_pinmux_set(dev, pin_selector, func_selector);
  205. return ret ? ret : pin_selector;
  206. }
  207. static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
  208. unsigned param, unsigned argument)
  209. {
  210. struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
  211. debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
  212. pin_selector, sandbox_get_pin_name(dev, pin_selector),
  213. param, argument);
  214. priv->pins_param[pin_selector] |= BIT(param);
  215. if (argument)
  216. priv->pins_value[pin_selector] |= BIT(param);
  217. else
  218. priv->pins_value[pin_selector] &= ~BIT(param);
  219. return 0;
  220. }
  221. static int sandbox_pinconf_group_set(struct udevice *dev,
  222. unsigned group_selector,
  223. unsigned param, unsigned argument)
  224. {
  225. debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
  226. group_selector, sandbox_get_group_name(dev, group_selector),
  227. param, argument);
  228. return 0;
  229. }
  230. const struct pinctrl_ops sandbox_pinctrl_ops = {
  231. .get_pins_count = sandbox_get_pins_count,
  232. .get_pin_name = sandbox_get_pin_name,
  233. .get_pin_muxing = sandbox_get_pin_muxing,
  234. .get_groups_count = sandbox_get_groups_count,
  235. .get_group_name = sandbox_get_group_name,
  236. .get_functions_count = sandbox_get_functions_count,
  237. .get_function_name = sandbox_get_function_name,
  238. .pinmux_set = sandbox_pinmux_set,
  239. .pinmux_group_set = sandbox_pinmux_group_set,
  240. .pinmux_property_set = sandbox_pinmux_property_set,
  241. .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
  242. .pinconf_params = sandbox_conf_params,
  243. .pinconf_set = sandbox_pinconf_set,
  244. .pinconf_group_set = sandbox_pinconf_group_set,
  245. .set_state = pinctrl_generic_set_state,
  246. };
  247. static const struct udevice_id sandbox_pinctrl_match[] = {
  248. { .compatible = "sandbox,pinctrl" },
  249. { /* sentinel */ }
  250. };
  251. U_BOOT_DRIVER(sandbox_pinctrl) = {
  252. .name = "sandbox_pinctrl",
  253. .id = UCLASS_PINCTRL,
  254. .of_match = sandbox_pinctrl_match,
  255. .priv_auto = sizeof(struct sandbox_pinctrl_priv),
  256. .ops = &sandbox_pinctrl_ops,
  257. };