pinctrl-mlxbf3.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause
  2. /* Copyright (C) 2022 NVIDIA CORPORATION & AFFILIATES */
  3. #include <linux/bitfield.h>
  4. #include <linux/bitops.h>
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/types.h>
  11. #include <linux/pinctrl/pinctrl.h>
  12. #include <linux/pinctrl/pinmux.h>
  13. #define MLXBF3_NGPIOS_GPIO0 32
  14. #define MLXBF3_MAX_GPIO_PINS 56
  15. enum {
  16. MLXBF3_GPIO_HW_MODE,
  17. MLXBF3_GPIO_SW_MODE,
  18. };
  19. struct mlxbf3_pinctrl {
  20. void __iomem *fw_ctrl_set0;
  21. void __iomem *fw_ctrl_clr0;
  22. void __iomem *fw_ctrl_set1;
  23. void __iomem *fw_ctrl_clr1;
  24. struct device *dev;
  25. struct pinctrl_dev *pctl;
  26. struct pinctrl_gpio_range gpio_range;
  27. };
  28. #define MLXBF3_GPIO_RANGE(_id, _pinbase, _gpiobase, _npins) \
  29. { \
  30. .name = "mlxbf3_gpio_range", \
  31. .id = _id, \
  32. .base = _gpiobase, \
  33. .pin_base = _pinbase, \
  34. .npins = _npins, \
  35. }
  36. static struct pinctrl_gpio_range mlxbf3_pinctrl_gpio_ranges[] = {
  37. MLXBF3_GPIO_RANGE(0, 0, 480, 32),
  38. MLXBF3_GPIO_RANGE(1, 32, 456, 24),
  39. };
  40. static const struct pinctrl_pin_desc mlxbf3_pins[] = {
  41. PINCTRL_PIN(0, "gpio0"),
  42. PINCTRL_PIN(1, "gpio1"),
  43. PINCTRL_PIN(2, "gpio2"),
  44. PINCTRL_PIN(3, "gpio3"),
  45. PINCTRL_PIN(4, "gpio4"),
  46. PINCTRL_PIN(5, "gpio5"),
  47. PINCTRL_PIN(6, "gpio6"),
  48. PINCTRL_PIN(7, "gpio7"),
  49. PINCTRL_PIN(8, "gpio8"),
  50. PINCTRL_PIN(9, "gpio9"),
  51. PINCTRL_PIN(10, "gpio10"),
  52. PINCTRL_PIN(11, "gpio11"),
  53. PINCTRL_PIN(12, "gpio12"),
  54. PINCTRL_PIN(13, "gpio13"),
  55. PINCTRL_PIN(14, "gpio14"),
  56. PINCTRL_PIN(15, "gpio15"),
  57. PINCTRL_PIN(16, "gpio16"),
  58. PINCTRL_PIN(17, "gpio17"),
  59. PINCTRL_PIN(18, "gpio18"),
  60. PINCTRL_PIN(19, "gpio19"),
  61. PINCTRL_PIN(20, "gpio20"),
  62. PINCTRL_PIN(21, "gpio21"),
  63. PINCTRL_PIN(22, "gpio22"),
  64. PINCTRL_PIN(23, "gpio23"),
  65. PINCTRL_PIN(24, "gpio24"),
  66. PINCTRL_PIN(25, "gpio25"),
  67. PINCTRL_PIN(26, "gpio26"),
  68. PINCTRL_PIN(27, "gpio27"),
  69. PINCTRL_PIN(28, "gpio28"),
  70. PINCTRL_PIN(29, "gpio29"),
  71. PINCTRL_PIN(30, "gpio30"),
  72. PINCTRL_PIN(31, "gpio31"),
  73. PINCTRL_PIN(32, "gpio32"),
  74. PINCTRL_PIN(33, "gpio33"),
  75. PINCTRL_PIN(34, "gpio34"),
  76. PINCTRL_PIN(35, "gpio35"),
  77. PINCTRL_PIN(36, "gpio36"),
  78. PINCTRL_PIN(37, "gpio37"),
  79. PINCTRL_PIN(38, "gpio38"),
  80. PINCTRL_PIN(39, "gpio39"),
  81. PINCTRL_PIN(40, "gpio40"),
  82. PINCTRL_PIN(41, "gpio41"),
  83. PINCTRL_PIN(42, "gpio42"),
  84. PINCTRL_PIN(43, "gpio43"),
  85. PINCTRL_PIN(44, "gpio44"),
  86. PINCTRL_PIN(45, "gpio45"),
  87. PINCTRL_PIN(46, "gpio46"),
  88. PINCTRL_PIN(47, "gpio47"),
  89. PINCTRL_PIN(48, "gpio48"),
  90. PINCTRL_PIN(49, "gpio49"),
  91. PINCTRL_PIN(50, "gpio50"),
  92. PINCTRL_PIN(51, "gpio51"),
  93. PINCTRL_PIN(52, "gpio52"),
  94. PINCTRL_PIN(53, "gpio53"),
  95. PINCTRL_PIN(54, "gpio54"),
  96. PINCTRL_PIN(55, "gpio55"),
  97. };
  98. /*
  99. * All single-pin functions can be mapped to any GPIO, however pinmux applies
  100. * functions to pin groups and only those groups declared as supporting that
  101. * function. To make this work we must put each pin in its own dummy group so
  102. * that the functions can be described as applying to all pins.
  103. * We use the same name as in the datasheet.
  104. */
  105. static const char * const mlxbf3_pinctrl_single_group_names[] = {
  106. "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
  107. "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
  108. "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22", "gpio23",
  109. "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29", "gpio30", "gpio31",
  110. "gpio32", "gpio33", "gpio34", "gpio35", "gpio36", "gpio37", "gpio38", "gpio39",
  111. "gpio40", "gpio41", "gpio42", "gpio43", "gpio44", "gpio45", "gpio46", "gpio47",
  112. "gpio48", "gpio49", "gpio50", "gpio51", "gpio52", "gpio53", "gpio54", "gpio55",
  113. };
  114. static int mlxbf3_get_groups_count(struct pinctrl_dev *pctldev)
  115. {
  116. /* Number single-pin groups */
  117. return MLXBF3_MAX_GPIO_PINS;
  118. }
  119. static const char *mlxbf3_get_group_name(struct pinctrl_dev *pctldev,
  120. unsigned int selector)
  121. {
  122. return mlxbf3_pinctrl_single_group_names[selector];
  123. }
  124. static int mlxbf3_get_group_pins(struct pinctrl_dev *pctldev,
  125. unsigned int selector,
  126. const unsigned int **pins,
  127. unsigned int *num_pins)
  128. {
  129. /* return the dummy group for a single pin */
  130. *pins = &selector;
  131. *num_pins = 1;
  132. return 0;
  133. }
  134. static const struct pinctrl_ops mlxbf3_pinctrl_group_ops = {
  135. .get_groups_count = mlxbf3_get_groups_count,
  136. .get_group_name = mlxbf3_get_group_name,
  137. .get_group_pins = mlxbf3_get_group_pins,
  138. };
  139. /*
  140. * Only 2 functions are supported and they apply to all pins:
  141. * 1) Default hardware functionality
  142. * 2) Software controlled GPIO
  143. */
  144. static const char * const mlxbf3_gpiofunc_group_names[] = { "swctrl" };
  145. static const char * const mlxbf3_hwfunc_group_names[] = { "hwctrl" };
  146. static struct pinfunction mlxbf3_pmx_funcs[] = {
  147. PINCTRL_PINFUNCTION("hwfunc", mlxbf3_hwfunc_group_names, 1),
  148. PINCTRL_PINFUNCTION("gpiofunc", mlxbf3_gpiofunc_group_names, 1),
  149. };
  150. static int mlxbf3_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  151. {
  152. return ARRAY_SIZE(mlxbf3_pmx_funcs);
  153. }
  154. static const char *mlxbf3_pmx_get_func_name(struct pinctrl_dev *pctldev,
  155. unsigned int selector)
  156. {
  157. return mlxbf3_pmx_funcs[selector].name;
  158. }
  159. static int mlxbf3_pmx_get_groups(struct pinctrl_dev *pctldev,
  160. unsigned int selector,
  161. const char * const **groups,
  162. unsigned int * const num_groups)
  163. {
  164. *groups = mlxbf3_pmx_funcs[selector].groups;
  165. *num_groups = MLXBF3_MAX_GPIO_PINS;
  166. return 0;
  167. }
  168. static int mlxbf3_pmx_set(struct pinctrl_dev *pctldev,
  169. unsigned int selector,
  170. unsigned int group)
  171. {
  172. struct mlxbf3_pinctrl *priv = pinctrl_dev_get_drvdata(pctldev);
  173. if (selector == MLXBF3_GPIO_HW_MODE) {
  174. if (group < MLXBF3_NGPIOS_GPIO0)
  175. writel(BIT(group), priv->fw_ctrl_clr0);
  176. else
  177. writel(BIT(group % MLXBF3_NGPIOS_GPIO0), priv->fw_ctrl_clr1);
  178. }
  179. if (selector == MLXBF3_GPIO_SW_MODE) {
  180. if (group < MLXBF3_NGPIOS_GPIO0)
  181. writel(BIT(group), priv->fw_ctrl_set0);
  182. else
  183. writel(BIT(group % MLXBF3_NGPIOS_GPIO0), priv->fw_ctrl_set1);
  184. }
  185. return 0;
  186. }
  187. static int mlxbf3_gpio_request_enable(struct pinctrl_dev *pctldev,
  188. struct pinctrl_gpio_range *range,
  189. unsigned int offset)
  190. {
  191. struct mlxbf3_pinctrl *priv = pinctrl_dev_get_drvdata(pctldev);
  192. if (offset < MLXBF3_NGPIOS_GPIO0)
  193. writel(BIT(offset), priv->fw_ctrl_set0);
  194. else
  195. writel(BIT(offset % MLXBF3_NGPIOS_GPIO0), priv->fw_ctrl_set1);
  196. return 0;
  197. }
  198. static const struct pinmux_ops mlxbf3_pmx_ops = {
  199. .get_functions_count = mlxbf3_pmx_get_funcs_count,
  200. .get_function_name = mlxbf3_pmx_get_func_name,
  201. .get_function_groups = mlxbf3_pmx_get_groups,
  202. .set_mux = mlxbf3_pmx_set,
  203. .gpio_request_enable = mlxbf3_gpio_request_enable,
  204. };
  205. static struct pinctrl_desc mlxbf3_pin_desc = {
  206. .name = "pinctrl-mlxbf3",
  207. .pins = mlxbf3_pins,
  208. .npins = ARRAY_SIZE(mlxbf3_pins),
  209. .pctlops = &mlxbf3_pinctrl_group_ops,
  210. .pmxops = &mlxbf3_pmx_ops,
  211. .owner = THIS_MODULE,
  212. };
  213. static_assert(ARRAY_SIZE(mlxbf3_pinctrl_single_group_names) == MLXBF3_MAX_GPIO_PINS);
  214. static int mlxbf3_pinctrl_probe(struct platform_device *pdev)
  215. {
  216. struct device *dev = &pdev->dev;
  217. struct mlxbf3_pinctrl *priv;
  218. int ret;
  219. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  220. if (!priv)
  221. return -ENOMEM;
  222. priv->dev = &pdev->dev;
  223. priv->fw_ctrl_set0 = devm_platform_ioremap_resource(pdev, 0);
  224. if (IS_ERR(priv->fw_ctrl_set0))
  225. return PTR_ERR(priv->fw_ctrl_set0);
  226. priv->fw_ctrl_clr0 = devm_platform_ioremap_resource(pdev, 1);
  227. if (IS_ERR(priv->fw_ctrl_clr0))
  228. return PTR_ERR(priv->fw_ctrl_clr0);
  229. priv->fw_ctrl_set1 = devm_platform_ioremap_resource(pdev, 2);
  230. if (IS_ERR(priv->fw_ctrl_set1))
  231. return PTR_ERR(priv->fw_ctrl_set1);
  232. priv->fw_ctrl_clr1 = devm_platform_ioremap_resource(pdev, 3);
  233. if (IS_ERR(priv->fw_ctrl_clr1))
  234. return PTR_ERR(priv->fw_ctrl_clr1);
  235. ret = devm_pinctrl_register_and_init(dev,
  236. &mlxbf3_pin_desc,
  237. priv,
  238. &priv->pctl);
  239. if (ret)
  240. return dev_err_probe(dev, ret, "Failed to register pinctrl\n");
  241. ret = pinctrl_enable(priv->pctl);
  242. if (ret)
  243. return dev_err_probe(dev, ret, "Failed to enable pinctrl\n");
  244. pinctrl_add_gpio_ranges(priv->pctl, mlxbf3_pinctrl_gpio_ranges, 2);
  245. return 0;
  246. }
  247. static const struct acpi_device_id mlxbf3_pinctrl_acpi_ids[] = {
  248. { "MLNXBF34", 0 },
  249. {}
  250. };
  251. MODULE_DEVICE_TABLE(acpi, mlxbf3_pinctrl_acpi_ids);
  252. static struct platform_driver mlxbf3_pinctrl_driver = {
  253. .driver = {
  254. .name = "pinctrl-mlxbf3",
  255. .acpi_match_table = mlxbf3_pinctrl_acpi_ids,
  256. },
  257. .probe = mlxbf3_pinctrl_probe,
  258. };
  259. module_platform_driver(mlxbf3_pinctrl_driver);
  260. MODULE_DESCRIPTION("NVIDIA pinctrl driver");
  261. MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
  262. MODULE_LICENSE("Dual BSD/GPL");