pinctrl-bcm6358.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for BCM6358 GPIO unit (pinctrl + GPIO)
  4. *
  5. * Copyright (C) 2021 Álvaro Fernández Rojas <[email protected]>
  6. * Copyright (C) 2016 Jonas Gorski <[email protected]>
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/pinctrl/pinmux.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include "../pinctrl-utils.h"
  16. #include "pinctrl-bcm63xx.h"
  17. #define BCM6358_NUM_GPIOS 40
  18. #define BCM6358_MODE_REG 0x18
  19. #define BCM6358_MODE_MUX_NONE 0
  20. #define BCM6358_MODE_MUX_EBI_CS BIT(5)
  21. #define BCM6358_MODE_MUX_UART1 BIT(6)
  22. #define BCM6358_MODE_MUX_SPI_CS BIT(7)
  23. #define BCM6358_MODE_MUX_ASYNC_MODEM BIT(8)
  24. #define BCM6358_MODE_MUX_LEGACY_LED BIT(9)
  25. #define BCM6358_MODE_MUX_SERIAL_LED BIT(10)
  26. #define BCM6358_MODE_MUX_LED BIT(11)
  27. #define BCM6358_MODE_MUX_UTOPIA BIT(12)
  28. #define BCM6358_MODE_MUX_CLKRST BIT(13)
  29. #define BCM6358_MODE_MUX_PWM_SYN_CLK BIT(14)
  30. #define BCM6358_MODE_MUX_SYS_IRQ BIT(15)
  31. struct bcm6358_pingroup {
  32. struct pingroup grp;
  33. const uint16_t mode_val;
  34. /* non-GPIO function muxes require the gpio direction to be set */
  35. const uint16_t direction;
  36. };
  37. struct bcm6358_function {
  38. const char *name;
  39. const char * const *groups;
  40. const unsigned num_groups;
  41. };
  42. struct bcm6358_priv {
  43. struct regmap_field *overlays;
  44. };
  45. #define BCM6358_GPIO_PIN(a, b, bit1, bit2, bit3) \
  46. { \
  47. .number = a, \
  48. .name = b, \
  49. .drv_data = (void *)(BCM6358_MODE_MUX_##bit1 | \
  50. BCM6358_MODE_MUX_##bit2 | \
  51. BCM6358_MODE_MUX_##bit3), \
  52. }
  53. static const struct pinctrl_pin_desc bcm6358_pins[] = {
  54. BCM6358_GPIO_PIN(0, "gpio0", LED, NONE, NONE),
  55. BCM6358_GPIO_PIN(1, "gpio1", LED, NONE, NONE),
  56. BCM6358_GPIO_PIN(2, "gpio2", LED, NONE, NONE),
  57. BCM6358_GPIO_PIN(3, "gpio3", LED, NONE, NONE),
  58. PINCTRL_PIN(4, "gpio4"),
  59. BCM6358_GPIO_PIN(5, "gpio5", SYS_IRQ, NONE, NONE),
  60. BCM6358_GPIO_PIN(6, "gpio6", SERIAL_LED, NONE, NONE),
  61. BCM6358_GPIO_PIN(7, "gpio7", SERIAL_LED, NONE, NONE),
  62. BCM6358_GPIO_PIN(8, "gpio8", PWM_SYN_CLK, NONE, NONE),
  63. BCM6358_GPIO_PIN(9, "gpio09", LEGACY_LED, NONE, NONE),
  64. BCM6358_GPIO_PIN(10, "gpio10", LEGACY_LED, NONE, NONE),
  65. BCM6358_GPIO_PIN(11, "gpio11", LEGACY_LED, NONE, NONE),
  66. BCM6358_GPIO_PIN(12, "gpio12", LEGACY_LED, ASYNC_MODEM, UTOPIA),
  67. BCM6358_GPIO_PIN(13, "gpio13", LEGACY_LED, ASYNC_MODEM, UTOPIA),
  68. BCM6358_GPIO_PIN(14, "gpio14", LEGACY_LED, ASYNC_MODEM, UTOPIA),
  69. BCM6358_GPIO_PIN(15, "gpio15", LEGACY_LED, ASYNC_MODEM, UTOPIA),
  70. PINCTRL_PIN(16, "gpio16"),
  71. PINCTRL_PIN(17, "gpio17"),
  72. PINCTRL_PIN(18, "gpio18"),
  73. PINCTRL_PIN(19, "gpio19"),
  74. PINCTRL_PIN(20, "gpio20"),
  75. PINCTRL_PIN(21, "gpio21"),
  76. BCM6358_GPIO_PIN(22, "gpio22", UTOPIA, NONE, NONE),
  77. BCM6358_GPIO_PIN(23, "gpio23", UTOPIA, NONE, NONE),
  78. BCM6358_GPIO_PIN(24, "gpio24", UTOPIA, NONE, NONE),
  79. BCM6358_GPIO_PIN(25, "gpio25", UTOPIA, NONE, NONE),
  80. BCM6358_GPIO_PIN(26, "gpio26", UTOPIA, NONE, NONE),
  81. BCM6358_GPIO_PIN(27, "gpio27", UTOPIA, NONE, NONE),
  82. BCM6358_GPIO_PIN(28, "gpio28", UTOPIA, UART1, NONE),
  83. BCM6358_GPIO_PIN(29, "gpio29", UTOPIA, UART1, NONE),
  84. BCM6358_GPIO_PIN(30, "gpio30", UTOPIA, UART1, EBI_CS),
  85. BCM6358_GPIO_PIN(31, "gpio31", UTOPIA, UART1, EBI_CS),
  86. BCM6358_GPIO_PIN(32, "gpio32", SPI_CS, NONE, NONE),
  87. BCM6358_GPIO_PIN(33, "gpio33", SPI_CS, NONE, NONE),
  88. PINCTRL_PIN(34, "gpio34"),
  89. PINCTRL_PIN(35, "gpio35"),
  90. PINCTRL_PIN(36, "gpio36"),
  91. PINCTRL_PIN(37, "gpio37"),
  92. PINCTRL_PIN(38, "gpio38"),
  93. PINCTRL_PIN(39, "gpio39"),
  94. };
  95. static unsigned ebi_cs_grp_pins[] = { 30, 31 };
  96. static unsigned uart1_grp_pins[] = { 28, 29, 30, 31 };
  97. static unsigned spi_cs_grp_pins[] = { 32, 33 };
  98. static unsigned async_modem_grp_pins[] = { 12, 13, 14, 15 };
  99. static unsigned serial_led_grp_pins[] = { 6, 7 };
  100. static unsigned legacy_led_grp_pins[] = { 9, 10, 11, 12, 13, 14, 15 };
  101. static unsigned led_grp_pins[] = { 0, 1, 2, 3 };
  102. static unsigned utopia_grp_pins[] = {
  103. 12, 13, 14, 15, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  104. };
  105. static unsigned pwm_syn_clk_grp_pins[] = { 8 };
  106. static unsigned sys_irq_grp_pins[] = { 5 };
  107. #define BCM6358_GPIO_MUX_GROUP(n, bit, dir) \
  108. { \
  109. .grp = BCM_PIN_GROUP(n), \
  110. .mode_val = BCM6358_MODE_MUX_##bit, \
  111. .direction = dir, \
  112. }
  113. static const struct bcm6358_pingroup bcm6358_groups[] = {
  114. BCM6358_GPIO_MUX_GROUP(ebi_cs_grp, EBI_CS, 0x3),
  115. BCM6358_GPIO_MUX_GROUP(uart1_grp, UART1, 0x2),
  116. BCM6358_GPIO_MUX_GROUP(spi_cs_grp, SPI_CS, 0x6),
  117. BCM6358_GPIO_MUX_GROUP(async_modem_grp, ASYNC_MODEM, 0x6),
  118. BCM6358_GPIO_MUX_GROUP(legacy_led_grp, LEGACY_LED, 0x7f),
  119. BCM6358_GPIO_MUX_GROUP(serial_led_grp, SERIAL_LED, 0x3),
  120. BCM6358_GPIO_MUX_GROUP(led_grp, LED, 0xf),
  121. BCM6358_GPIO_MUX_GROUP(utopia_grp, UTOPIA, 0x000f),
  122. BCM6358_GPIO_MUX_GROUP(pwm_syn_clk_grp, PWM_SYN_CLK, 0x1),
  123. BCM6358_GPIO_MUX_GROUP(sys_irq_grp, SYS_IRQ, 0x1),
  124. };
  125. static const char * const ebi_cs_groups[] = {
  126. "ebi_cs_grp"
  127. };
  128. static const char * const uart1_groups[] = {
  129. "uart1_grp"
  130. };
  131. static const char * const spi_cs_2_3_groups[] = {
  132. "spi_cs_2_3_grp"
  133. };
  134. static const char * const async_modem_groups[] = {
  135. "async_modem_grp"
  136. };
  137. static const char * const legacy_led_groups[] = {
  138. "legacy_led_grp",
  139. };
  140. static const char * const serial_led_groups[] = {
  141. "serial_led_grp",
  142. };
  143. static const char * const led_groups[] = {
  144. "led_grp",
  145. };
  146. static const char * const clkrst_groups[] = {
  147. "clkrst_grp",
  148. };
  149. static const char * const pwm_syn_clk_groups[] = {
  150. "pwm_syn_clk_grp",
  151. };
  152. static const char * const sys_irq_groups[] = {
  153. "sys_irq_grp",
  154. };
  155. #define BCM6358_FUN(n) \
  156. { \
  157. .name = #n, \
  158. .groups = n##_groups, \
  159. .num_groups = ARRAY_SIZE(n##_groups), \
  160. }
  161. static const struct bcm6358_function bcm6358_funcs[] = {
  162. BCM6358_FUN(ebi_cs),
  163. BCM6358_FUN(uart1),
  164. BCM6358_FUN(spi_cs_2_3),
  165. BCM6358_FUN(async_modem),
  166. BCM6358_FUN(legacy_led),
  167. BCM6358_FUN(serial_led),
  168. BCM6358_FUN(led),
  169. BCM6358_FUN(clkrst),
  170. BCM6358_FUN(pwm_syn_clk),
  171. BCM6358_FUN(sys_irq),
  172. };
  173. static int bcm6358_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
  174. {
  175. return ARRAY_SIZE(bcm6358_groups);
  176. }
  177. static const char *bcm6358_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  178. unsigned group)
  179. {
  180. return bcm6358_groups[group].grp.name;
  181. }
  182. static int bcm6358_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  183. unsigned group, const unsigned **pins,
  184. unsigned *npins)
  185. {
  186. *pins = bcm6358_groups[group].grp.pins;
  187. *npins = bcm6358_groups[group].grp.npins;
  188. return 0;
  189. }
  190. static int bcm6358_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
  191. {
  192. return ARRAY_SIZE(bcm6358_funcs);
  193. }
  194. static const char *bcm6358_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  195. unsigned selector)
  196. {
  197. return bcm6358_funcs[selector].name;
  198. }
  199. static int bcm6358_pinctrl_get_groups(struct pinctrl_dev *pctldev,
  200. unsigned selector,
  201. const char * const **groups,
  202. unsigned * const num_groups)
  203. {
  204. *groups = bcm6358_funcs[selector].groups;
  205. *num_groups = bcm6358_funcs[selector].num_groups;
  206. return 0;
  207. }
  208. static int bcm6358_pinctrl_set_mux(struct pinctrl_dev *pctldev,
  209. unsigned selector, unsigned group)
  210. {
  211. struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  212. struct bcm6358_priv *priv = pc->driver_data;
  213. const struct bcm6358_pingroup *pg = &bcm6358_groups[group];
  214. unsigned int val = pg->mode_val;
  215. unsigned int mask = val;
  216. unsigned pin;
  217. for (pin = 0; pin < pg->grp.npins; pin++)
  218. mask |= (unsigned long)bcm6358_pins[pin].drv_data;
  219. regmap_field_update_bits(priv->overlays, mask, val);
  220. for (pin = 0; pin < pg->grp.npins; pin++) {
  221. struct pinctrl_gpio_range *range;
  222. unsigned int hw_gpio = bcm6358_pins[pin].number;
  223. range = pinctrl_find_gpio_range_from_pin(pctldev, hw_gpio);
  224. if (range) {
  225. struct gpio_chip *gc = range->gc;
  226. if (pg->direction & BIT(pin))
  227. gc->direction_output(gc, hw_gpio, 0);
  228. else
  229. gc->direction_input(gc, hw_gpio);
  230. }
  231. }
  232. return 0;
  233. }
  234. static int bcm6358_gpio_request_enable(struct pinctrl_dev *pctldev,
  235. struct pinctrl_gpio_range *range,
  236. unsigned offset)
  237. {
  238. struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  239. struct bcm6358_priv *priv = pc->driver_data;
  240. unsigned int mask;
  241. mask = (unsigned long) bcm6358_pins[offset].drv_data;
  242. if (!mask)
  243. return 0;
  244. /* disable all functions using this pin */
  245. return regmap_field_update_bits(priv->overlays, mask, 0);
  246. }
  247. static const struct pinctrl_ops bcm6358_pctl_ops = {
  248. .dt_free_map = pinctrl_utils_free_map,
  249. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  250. .get_group_name = bcm6358_pinctrl_get_group_name,
  251. .get_group_pins = bcm6358_pinctrl_get_group_pins,
  252. .get_groups_count = bcm6358_pinctrl_get_group_count,
  253. };
  254. static const struct pinmux_ops bcm6358_pmx_ops = {
  255. .get_function_groups = bcm6358_pinctrl_get_groups,
  256. .get_function_name = bcm6358_pinctrl_get_func_name,
  257. .get_functions_count = bcm6358_pinctrl_get_func_count,
  258. .gpio_request_enable = bcm6358_gpio_request_enable,
  259. .set_mux = bcm6358_pinctrl_set_mux,
  260. .strict = true,
  261. };
  262. static const struct bcm63xx_pinctrl_soc bcm6358_soc = {
  263. .ngpios = BCM6358_NUM_GPIOS,
  264. .npins = ARRAY_SIZE(bcm6358_pins),
  265. .pctl_ops = &bcm6358_pctl_ops,
  266. .pins = bcm6358_pins,
  267. .pmx_ops = &bcm6358_pmx_ops,
  268. };
  269. static int bcm6358_pinctrl_probe(struct platform_device *pdev)
  270. {
  271. struct reg_field overlays = REG_FIELD(BCM6358_MODE_REG, 0, 15);
  272. struct device *dev = &pdev->dev;
  273. struct bcm63xx_pinctrl *pc;
  274. struct bcm6358_priv *priv;
  275. int err;
  276. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  277. if (!priv)
  278. return -ENOMEM;
  279. err = bcm63xx_pinctrl_probe(pdev, &bcm6358_soc, (void *) priv);
  280. if (err)
  281. return err;
  282. pc = platform_get_drvdata(pdev);
  283. priv->overlays = devm_regmap_field_alloc(dev, pc->regs, overlays);
  284. if (IS_ERR(priv->overlays))
  285. return PTR_ERR(priv->overlays);
  286. return 0;
  287. }
  288. static const struct of_device_id bcm6358_pinctrl_match[] = {
  289. { .compatible = "brcm,bcm6358-pinctrl", },
  290. { /* sentinel */ }
  291. };
  292. static struct platform_driver bcm6358_pinctrl_driver = {
  293. .probe = bcm6358_pinctrl_probe,
  294. .driver = {
  295. .name = "bcm6358-pinctrl",
  296. .of_match_table = bcm6358_pinctrl_match,
  297. },
  298. };
  299. builtin_platform_driver(bcm6358_pinctrl_driver);