pinctrl-bcm6368.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for BCM6368 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 BCM6368_NUM_GPIOS 38
  18. #define BCM6368_MODE_REG 0x18
  19. #define BCM6368_BASEMODE_REG 0x38
  20. #define BCM6368_BASEMODE_MASK 0x7
  21. #define BCM6368_BASEMODE_GPIO 0x0
  22. #define BCM6368_BASEMODE_UART1 0x1
  23. struct bcm6368_function {
  24. const char *name;
  25. const char * const *groups;
  26. const unsigned num_groups;
  27. unsigned dir_out:16;
  28. unsigned basemode:3;
  29. };
  30. struct bcm6368_priv {
  31. struct regmap_field *overlays;
  32. };
  33. #define BCM6368_BASEMODE_PIN(a, b) \
  34. { \
  35. .number = a, \
  36. .name = b, \
  37. .drv_data = (void *)true \
  38. }
  39. static const struct pinctrl_pin_desc bcm6368_pins[] = {
  40. PINCTRL_PIN(0, "gpio0"),
  41. PINCTRL_PIN(1, "gpio1"),
  42. PINCTRL_PIN(2, "gpio2"),
  43. PINCTRL_PIN(3, "gpio3"),
  44. PINCTRL_PIN(4, "gpio4"),
  45. PINCTRL_PIN(5, "gpio5"),
  46. PINCTRL_PIN(6, "gpio6"),
  47. PINCTRL_PIN(7, "gpio7"),
  48. PINCTRL_PIN(8, "gpio8"),
  49. PINCTRL_PIN(9, "gpio9"),
  50. PINCTRL_PIN(10, "gpio10"),
  51. PINCTRL_PIN(11, "gpio11"),
  52. PINCTRL_PIN(12, "gpio12"),
  53. PINCTRL_PIN(13, "gpio13"),
  54. PINCTRL_PIN(14, "gpio14"),
  55. PINCTRL_PIN(15, "gpio15"),
  56. PINCTRL_PIN(16, "gpio16"),
  57. PINCTRL_PIN(17, "gpio17"),
  58. PINCTRL_PIN(18, "gpio18"),
  59. PINCTRL_PIN(19, "gpio19"),
  60. PINCTRL_PIN(20, "gpio20"),
  61. PINCTRL_PIN(21, "gpio21"),
  62. PINCTRL_PIN(22, "gpio22"),
  63. PINCTRL_PIN(23, "gpio23"),
  64. PINCTRL_PIN(24, "gpio24"),
  65. PINCTRL_PIN(25, "gpio25"),
  66. PINCTRL_PIN(26, "gpio26"),
  67. PINCTRL_PIN(27, "gpio27"),
  68. PINCTRL_PIN(28, "gpio28"),
  69. PINCTRL_PIN(29, "gpio29"),
  70. BCM6368_BASEMODE_PIN(30, "gpio30"),
  71. BCM6368_BASEMODE_PIN(31, "gpio31"),
  72. BCM6368_BASEMODE_PIN(32, "gpio32"),
  73. BCM6368_BASEMODE_PIN(33, "gpio33"),
  74. PINCTRL_PIN(34, "gpio34"),
  75. PINCTRL_PIN(35, "gpio35"),
  76. PINCTRL_PIN(36, "gpio36"),
  77. PINCTRL_PIN(37, "gpio37"),
  78. };
  79. static unsigned gpio0_pins[] = { 0 };
  80. static unsigned gpio1_pins[] = { 1 };
  81. static unsigned gpio2_pins[] = { 2 };
  82. static unsigned gpio3_pins[] = { 3 };
  83. static unsigned gpio4_pins[] = { 4 };
  84. static unsigned gpio5_pins[] = { 5 };
  85. static unsigned gpio6_pins[] = { 6 };
  86. static unsigned gpio7_pins[] = { 7 };
  87. static unsigned gpio8_pins[] = { 8 };
  88. static unsigned gpio9_pins[] = { 9 };
  89. static unsigned gpio10_pins[] = { 10 };
  90. static unsigned gpio11_pins[] = { 11 };
  91. static unsigned gpio12_pins[] = { 12 };
  92. static unsigned gpio13_pins[] = { 13 };
  93. static unsigned gpio14_pins[] = { 14 };
  94. static unsigned gpio15_pins[] = { 15 };
  95. static unsigned gpio16_pins[] = { 16 };
  96. static unsigned gpio17_pins[] = { 17 };
  97. static unsigned gpio18_pins[] = { 18 };
  98. static unsigned gpio19_pins[] = { 19 };
  99. static unsigned gpio20_pins[] = { 20 };
  100. static unsigned gpio21_pins[] = { 21 };
  101. static unsigned gpio22_pins[] = { 22 };
  102. static unsigned gpio23_pins[] = { 23 };
  103. static unsigned gpio24_pins[] = { 24 };
  104. static unsigned gpio25_pins[] = { 25 };
  105. static unsigned gpio26_pins[] = { 26 };
  106. static unsigned gpio27_pins[] = { 27 };
  107. static unsigned gpio28_pins[] = { 28 };
  108. static unsigned gpio29_pins[] = { 29 };
  109. static unsigned gpio30_pins[] = { 30 };
  110. static unsigned gpio31_pins[] = { 31 };
  111. static unsigned uart1_grp_pins[] = { 30, 31, 32, 33 };
  112. static struct pingroup bcm6368_groups[] = {
  113. BCM_PIN_GROUP(gpio0),
  114. BCM_PIN_GROUP(gpio1),
  115. BCM_PIN_GROUP(gpio2),
  116. BCM_PIN_GROUP(gpio3),
  117. BCM_PIN_GROUP(gpio4),
  118. BCM_PIN_GROUP(gpio5),
  119. BCM_PIN_GROUP(gpio6),
  120. BCM_PIN_GROUP(gpio7),
  121. BCM_PIN_GROUP(gpio8),
  122. BCM_PIN_GROUP(gpio9),
  123. BCM_PIN_GROUP(gpio10),
  124. BCM_PIN_GROUP(gpio11),
  125. BCM_PIN_GROUP(gpio12),
  126. BCM_PIN_GROUP(gpio13),
  127. BCM_PIN_GROUP(gpio14),
  128. BCM_PIN_GROUP(gpio15),
  129. BCM_PIN_GROUP(gpio16),
  130. BCM_PIN_GROUP(gpio17),
  131. BCM_PIN_GROUP(gpio18),
  132. BCM_PIN_GROUP(gpio19),
  133. BCM_PIN_GROUP(gpio20),
  134. BCM_PIN_GROUP(gpio21),
  135. BCM_PIN_GROUP(gpio22),
  136. BCM_PIN_GROUP(gpio23),
  137. BCM_PIN_GROUP(gpio24),
  138. BCM_PIN_GROUP(gpio25),
  139. BCM_PIN_GROUP(gpio26),
  140. BCM_PIN_GROUP(gpio27),
  141. BCM_PIN_GROUP(gpio28),
  142. BCM_PIN_GROUP(gpio29),
  143. BCM_PIN_GROUP(gpio30),
  144. BCM_PIN_GROUP(gpio31),
  145. BCM_PIN_GROUP(uart1_grp),
  146. };
  147. static const char * const analog_afe_0_groups[] = {
  148. "gpio0",
  149. };
  150. static const char * const analog_afe_1_groups[] = {
  151. "gpio1",
  152. };
  153. static const char * const sys_irq_groups[] = {
  154. "gpio2",
  155. };
  156. static const char * const serial_led_data_groups[] = {
  157. "gpio3",
  158. };
  159. static const char * const serial_led_clk_groups[] = {
  160. "gpio4",
  161. };
  162. static const char * const inet_led_groups[] = {
  163. "gpio5",
  164. };
  165. static const char * const ephy0_led_groups[] = {
  166. "gpio6",
  167. };
  168. static const char * const ephy1_led_groups[] = {
  169. "gpio7",
  170. };
  171. static const char * const ephy2_led_groups[] = {
  172. "gpio8",
  173. };
  174. static const char * const ephy3_led_groups[] = {
  175. "gpio9",
  176. };
  177. static const char * const robosw_led_data_groups[] = {
  178. "gpio10",
  179. };
  180. static const char * const robosw_led_clk_groups[] = {
  181. "gpio11",
  182. };
  183. static const char * const robosw_led0_groups[] = {
  184. "gpio12",
  185. };
  186. static const char * const robosw_led1_groups[] = {
  187. "gpio13",
  188. };
  189. static const char * const usb_device_led_groups[] = {
  190. "gpio14",
  191. };
  192. static const char * const pci_req1_groups[] = {
  193. "gpio16",
  194. };
  195. static const char * const pci_gnt1_groups[] = {
  196. "gpio17",
  197. };
  198. static const char * const pci_intb_groups[] = {
  199. "gpio18",
  200. };
  201. static const char * const pci_req0_groups[] = {
  202. "gpio19",
  203. };
  204. static const char * const pci_gnt0_groups[] = {
  205. "gpio20",
  206. };
  207. static const char * const pcmcia_cd1_groups[] = {
  208. "gpio22",
  209. };
  210. static const char * const pcmcia_cd2_groups[] = {
  211. "gpio23",
  212. };
  213. static const char * const pcmcia_vs1_groups[] = {
  214. "gpio24",
  215. };
  216. static const char * const pcmcia_vs2_groups[] = {
  217. "gpio25",
  218. };
  219. static const char * const ebi_cs2_groups[] = {
  220. "gpio26",
  221. };
  222. static const char * const ebi_cs3_groups[] = {
  223. "gpio27",
  224. };
  225. static const char * const spi_cs2_groups[] = {
  226. "gpio28",
  227. };
  228. static const char * const spi_cs3_groups[] = {
  229. "gpio29",
  230. };
  231. static const char * const spi_cs4_groups[] = {
  232. "gpio30",
  233. };
  234. static const char * const spi_cs5_groups[] = {
  235. "gpio31",
  236. };
  237. static const char * const uart1_groups[] = {
  238. "uart1_grp",
  239. };
  240. #define BCM6368_FUN(n, out) \
  241. { \
  242. .name = #n, \
  243. .groups = n##_groups, \
  244. .num_groups = ARRAY_SIZE(n##_groups), \
  245. .dir_out = out, \
  246. }
  247. #define BCM6368_BASEMODE_FUN(n, val, out) \
  248. { \
  249. .name = #n, \
  250. .groups = n##_groups, \
  251. .num_groups = ARRAY_SIZE(n##_groups), \
  252. .basemode = BCM6368_BASEMODE_##val, \
  253. .dir_out = out, \
  254. }
  255. static const struct bcm6368_function bcm6368_funcs[] = {
  256. BCM6368_FUN(analog_afe_0, 1),
  257. BCM6368_FUN(analog_afe_1, 1),
  258. BCM6368_FUN(sys_irq, 1),
  259. BCM6368_FUN(serial_led_data, 1),
  260. BCM6368_FUN(serial_led_clk, 1),
  261. BCM6368_FUN(inet_led, 1),
  262. BCM6368_FUN(ephy0_led, 1),
  263. BCM6368_FUN(ephy1_led, 1),
  264. BCM6368_FUN(ephy2_led, 1),
  265. BCM6368_FUN(ephy3_led, 1),
  266. BCM6368_FUN(robosw_led_data, 1),
  267. BCM6368_FUN(robosw_led_clk, 1),
  268. BCM6368_FUN(robosw_led0, 1),
  269. BCM6368_FUN(robosw_led1, 1),
  270. BCM6368_FUN(usb_device_led, 1),
  271. BCM6368_FUN(pci_req1, 0),
  272. BCM6368_FUN(pci_gnt1, 0),
  273. BCM6368_FUN(pci_intb, 0),
  274. BCM6368_FUN(pci_req0, 0),
  275. BCM6368_FUN(pci_gnt0, 0),
  276. BCM6368_FUN(pcmcia_cd1, 0),
  277. BCM6368_FUN(pcmcia_cd2, 0),
  278. BCM6368_FUN(pcmcia_vs1, 0),
  279. BCM6368_FUN(pcmcia_vs2, 0),
  280. BCM6368_FUN(ebi_cs2, 1),
  281. BCM6368_FUN(ebi_cs3, 1),
  282. BCM6368_FUN(spi_cs2, 1),
  283. BCM6368_FUN(spi_cs3, 1),
  284. BCM6368_FUN(spi_cs4, 1),
  285. BCM6368_FUN(spi_cs5, 1),
  286. BCM6368_BASEMODE_FUN(uart1, UART1, 0x6),
  287. };
  288. static int bcm6368_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
  289. {
  290. return ARRAY_SIZE(bcm6368_groups);
  291. }
  292. static const char *bcm6368_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  293. unsigned group)
  294. {
  295. return bcm6368_groups[group].name;
  296. }
  297. static int bcm6368_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  298. unsigned group, const unsigned **pins,
  299. unsigned *npins)
  300. {
  301. *pins = bcm6368_groups[group].pins;
  302. *npins = bcm6368_groups[group].npins;
  303. return 0;
  304. }
  305. static int bcm6368_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
  306. {
  307. return ARRAY_SIZE(bcm6368_funcs);
  308. }
  309. static const char *bcm6368_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  310. unsigned selector)
  311. {
  312. return bcm6368_funcs[selector].name;
  313. }
  314. static int bcm6368_pinctrl_get_groups(struct pinctrl_dev *pctldev,
  315. unsigned selector,
  316. const char * const **groups,
  317. unsigned * const num_groups)
  318. {
  319. *groups = bcm6368_funcs[selector].groups;
  320. *num_groups = bcm6368_funcs[selector].num_groups;
  321. return 0;
  322. }
  323. static int bcm6368_pinctrl_set_mux(struct pinctrl_dev *pctldev,
  324. unsigned selector, unsigned group)
  325. {
  326. struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  327. struct bcm6368_priv *priv = pc->driver_data;
  328. const struct pingroup *pg = &bcm6368_groups[group];
  329. const struct bcm6368_function *fun = &bcm6368_funcs[selector];
  330. int i, pin;
  331. if (fun->basemode) {
  332. unsigned int mask = 0;
  333. for (i = 0; i < pg->npins; i++) {
  334. pin = pg->pins[i];
  335. if (pin < BCM63XX_BANK_GPIOS)
  336. mask |= BIT(pin);
  337. }
  338. regmap_update_bits(pc->regs, BCM6368_MODE_REG, mask, 0);
  339. regmap_field_write(priv->overlays, fun->basemode);
  340. } else {
  341. pin = pg->pins[0];
  342. if (bcm6368_pins[pin].drv_data)
  343. regmap_field_write(priv->overlays,
  344. BCM6368_BASEMODE_GPIO);
  345. regmap_update_bits(pc->regs, BCM6368_MODE_REG, BIT(pin),
  346. BIT(pin));
  347. }
  348. for (pin = 0; pin < pg->npins; pin++) {
  349. struct pinctrl_gpio_range *range;
  350. int hw_gpio = bcm6368_pins[pin].number;
  351. range = pinctrl_find_gpio_range_from_pin(pctldev, hw_gpio);
  352. if (range) {
  353. struct gpio_chip *gc = range->gc;
  354. if (fun->dir_out & BIT(pin))
  355. gc->direction_output(gc, hw_gpio, 0);
  356. else
  357. gc->direction_input(gc, hw_gpio);
  358. }
  359. }
  360. return 0;
  361. }
  362. static int bcm6368_gpio_request_enable(struct pinctrl_dev *pctldev,
  363. struct pinctrl_gpio_range *range,
  364. unsigned offset)
  365. {
  366. struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  367. struct bcm6368_priv *priv = pc->driver_data;
  368. if (offset >= BCM63XX_BANK_GPIOS && !bcm6368_pins[offset].drv_data)
  369. return 0;
  370. /* disable all functions using this pin */
  371. if (offset < BCM63XX_BANK_GPIOS)
  372. regmap_update_bits(pc->regs, BCM6368_MODE_REG, BIT(offset), 0);
  373. if (bcm6368_pins[offset].drv_data)
  374. regmap_field_write(priv->overlays, BCM6368_BASEMODE_GPIO);
  375. return 0;
  376. }
  377. static const struct pinctrl_ops bcm6368_pctl_ops = {
  378. .dt_free_map = pinctrl_utils_free_map,
  379. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  380. .get_group_name = bcm6368_pinctrl_get_group_name,
  381. .get_group_pins = bcm6368_pinctrl_get_group_pins,
  382. .get_groups_count = bcm6368_pinctrl_get_group_count,
  383. };
  384. static const struct pinmux_ops bcm6368_pmx_ops = {
  385. .get_function_groups = bcm6368_pinctrl_get_groups,
  386. .get_function_name = bcm6368_pinctrl_get_func_name,
  387. .get_functions_count = bcm6368_pinctrl_get_func_count,
  388. .gpio_request_enable = bcm6368_gpio_request_enable,
  389. .set_mux = bcm6368_pinctrl_set_mux,
  390. .strict = true,
  391. };
  392. static const struct bcm63xx_pinctrl_soc bcm6368_soc = {
  393. .ngpios = BCM6368_NUM_GPIOS,
  394. .npins = ARRAY_SIZE(bcm6368_pins),
  395. .pctl_ops = &bcm6368_pctl_ops,
  396. .pins = bcm6368_pins,
  397. .pmx_ops = &bcm6368_pmx_ops,
  398. };
  399. static int bcm6368_pinctrl_probe(struct platform_device *pdev)
  400. {
  401. struct reg_field overlays = REG_FIELD(BCM6368_BASEMODE_REG, 0, 15);
  402. struct device *dev = &pdev->dev;
  403. struct bcm63xx_pinctrl *pc;
  404. struct bcm6368_priv *priv;
  405. int err;
  406. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  407. if (!priv)
  408. return -ENOMEM;
  409. err = bcm63xx_pinctrl_probe(pdev, &bcm6368_soc, (void *) priv);
  410. if (err)
  411. return err;
  412. pc = platform_get_drvdata(pdev);
  413. priv->overlays = devm_regmap_field_alloc(dev, pc->regs, overlays);
  414. if (IS_ERR(priv->overlays))
  415. return PTR_ERR(priv->overlays);
  416. return 0;
  417. }
  418. static const struct of_device_id bcm6368_pinctrl_match[] = {
  419. { .compatible = "brcm,bcm6368-pinctrl", },
  420. { /* sentinel */ }
  421. };
  422. static struct platform_driver bcm6368_pinctrl_driver = {
  423. .probe = bcm6368_pinctrl_probe,
  424. .driver = {
  425. .name = "bcm6368-pinctrl",
  426. .of_match_table = bcm6368_pinctrl_match,
  427. },
  428. };
  429. builtin_platform_driver(bcm6368_pinctrl_driver);