pinctrl-ns.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Rafał Miłecki <[email protected]>
  4. */
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/pinctrl/pinconf-generic.h>
  11. #include <linux/pinctrl/pinctrl.h>
  12. #include <linux/pinctrl/pinmux.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include "../core.h"
  16. #include "../pinmux.h"
  17. #define FLAG_BCM4708 BIT(1)
  18. #define FLAG_BCM4709 BIT(2)
  19. #define FLAG_BCM53012 BIT(3)
  20. struct ns_pinctrl {
  21. struct device *dev;
  22. unsigned int chipset_flag;
  23. struct pinctrl_dev *pctldev;
  24. void __iomem *base;
  25. struct pinctrl_desc pctldesc;
  26. };
  27. /*
  28. * Pins
  29. */
  30. static const struct pinctrl_pin_desc ns_pinctrl_pins[] = {
  31. { 0, "spi_clk", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  32. { 1, "spi_ss", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  33. { 2, "spi_mosi", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  34. { 3, "spi_miso", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  35. { 4, "i2c_scl", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  36. { 5, "i2c_sda", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  37. { 6, "mdc", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  38. { 7, "mdio", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  39. { 8, "pwm0", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  40. { 9, "pwm1", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  41. { 10, "pwm2", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  42. { 11, "pwm3", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  43. { 12, "uart1_rx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  44. { 13, "uart1_tx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  45. { 14, "uart1_cts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  46. { 15, "uart1_rts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  47. { 16, "uart2_rx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  48. { 17, "uart2_tx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  49. /* TODO { ??, "xtal_out", (void *)(FLAG_BCM4709) }, */
  50. { 22, "sdio_pwr", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  51. { 23, "sdio_en_1p8v", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  52. };
  53. /*
  54. * Groups
  55. */
  56. struct ns_pinctrl_group {
  57. const char *name;
  58. unsigned int *pins;
  59. const unsigned int num_pins;
  60. unsigned int chipsets;
  61. };
  62. static unsigned int spi_pins[] = { 0, 1, 2, 3 };
  63. static unsigned int i2c_pins[] = { 4, 5 };
  64. static unsigned int mdio_pins[] = { 6, 7 };
  65. static unsigned int pwm0_pins[] = { 8 };
  66. static unsigned int pwm1_pins[] = { 9 };
  67. static unsigned int pwm2_pins[] = { 10 };
  68. static unsigned int pwm3_pins[] = { 11 };
  69. static unsigned int uart1_pins[] = { 12, 13, 14, 15 };
  70. static unsigned int uart2_pins[] = { 16, 17 };
  71. static unsigned int sdio_pwr_pins[] = { 22 };
  72. static unsigned int sdio_1p8v_pins[] = { 23 };
  73. #define NS_GROUP(_name, _pins, _chipsets) \
  74. { \
  75. .name = _name, \
  76. .pins = _pins, \
  77. .num_pins = ARRAY_SIZE(_pins), \
  78. .chipsets = _chipsets, \
  79. }
  80. static const struct ns_pinctrl_group ns_pinctrl_groups[] = {
  81. NS_GROUP("spi_grp", spi_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  82. NS_GROUP("i2c_grp", i2c_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  83. NS_GROUP("mdio_grp", mdio_pins, FLAG_BCM4709 | FLAG_BCM53012),
  84. NS_GROUP("pwm0_grp", pwm0_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  85. NS_GROUP("pwm1_grp", pwm1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  86. NS_GROUP("pwm2_grp", pwm2_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  87. NS_GROUP("pwm3_grp", pwm3_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  88. NS_GROUP("uart1_grp", uart1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  89. NS_GROUP("uart2_grp", uart2_pins, FLAG_BCM4709 | FLAG_BCM53012),
  90. NS_GROUP("sdio_pwr_grp", sdio_pwr_pins, FLAG_BCM4709 | FLAG_BCM53012),
  91. NS_GROUP("sdio_1p8v_grp", sdio_1p8v_pins, FLAG_BCM4709 | FLAG_BCM53012),
  92. };
  93. /*
  94. * Functions
  95. */
  96. struct ns_pinctrl_function {
  97. const char *name;
  98. const char * const *groups;
  99. const unsigned int num_groups;
  100. unsigned int chipsets;
  101. };
  102. static const char * const spi_groups[] = { "spi_grp" };
  103. static const char * const i2c_groups[] = { "i2c_grp" };
  104. static const char * const mdio_groups[] = { "mdio_grp" };
  105. static const char * const pwm_groups[] = { "pwm0_grp", "pwm1_grp", "pwm2_grp",
  106. "pwm3_grp" };
  107. static const char * const uart1_groups[] = { "uart1_grp" };
  108. static const char * const uart2_groups[] = { "uart2_grp" };
  109. static const char * const sdio_groups[] = { "sdio_pwr_grp", "sdio_1p8v_grp" };
  110. #define NS_FUNCTION(_name, _groups, _chipsets) \
  111. { \
  112. .name = _name, \
  113. .groups = _groups, \
  114. .num_groups = ARRAY_SIZE(_groups), \
  115. .chipsets = _chipsets, \
  116. }
  117. static const struct ns_pinctrl_function ns_pinctrl_functions[] = {
  118. NS_FUNCTION("spi", spi_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  119. NS_FUNCTION("i2c", i2c_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  120. NS_FUNCTION("mdio", mdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
  121. NS_FUNCTION("pwm", pwm_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  122. NS_FUNCTION("uart1", uart1_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  123. NS_FUNCTION("uart2", uart2_groups, FLAG_BCM4709 | FLAG_BCM53012),
  124. NS_FUNCTION("sdio", sdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
  125. };
  126. /*
  127. * Groups code
  128. */
  129. static const struct pinctrl_ops ns_pinctrl_ops = {
  130. .get_groups_count = pinctrl_generic_get_group_count,
  131. .get_group_name = pinctrl_generic_get_group_name,
  132. .get_group_pins = pinctrl_generic_get_group_pins,
  133. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  134. .dt_free_map = pinconf_generic_dt_free_map,
  135. };
  136. /*
  137. * Functions code
  138. */
  139. static int ns_pinctrl_set_mux(struct pinctrl_dev *pctrl_dev,
  140. unsigned int func_select,
  141. unsigned int group_selector)
  142. {
  143. struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
  144. struct group_desc *group;
  145. u32 unset = 0;
  146. u32 tmp;
  147. int i;
  148. group = pinctrl_generic_get_group(pctrl_dev, group_selector);
  149. if (!group)
  150. return -EINVAL;
  151. for (i = 0; i < group->num_pins; i++)
  152. unset |= BIT(group->pins[i]);
  153. tmp = readl(ns_pinctrl->base);
  154. tmp &= ~unset;
  155. writel(tmp, ns_pinctrl->base);
  156. return 0;
  157. }
  158. static const struct pinmux_ops ns_pinctrl_pmxops = {
  159. .get_functions_count = pinmux_generic_get_function_count,
  160. .get_function_name = pinmux_generic_get_function_name,
  161. .get_function_groups = pinmux_generic_get_function_groups,
  162. .set_mux = ns_pinctrl_set_mux,
  163. };
  164. /*
  165. * Controller code
  166. */
  167. static struct pinctrl_desc ns_pinctrl_desc = {
  168. .name = "pinctrl-ns",
  169. .pctlops = &ns_pinctrl_ops,
  170. .pmxops = &ns_pinctrl_pmxops,
  171. };
  172. static const struct of_device_id ns_pinctrl_of_match_table[] = {
  173. { .compatible = "brcm,bcm4708-pinmux", .data = (void *)FLAG_BCM4708, },
  174. { .compatible = "brcm,bcm4709-pinmux", .data = (void *)FLAG_BCM4709, },
  175. { .compatible = "brcm,bcm53012-pinmux", .data = (void *)FLAG_BCM53012, },
  176. { }
  177. };
  178. static int ns_pinctrl_probe(struct platform_device *pdev)
  179. {
  180. struct device *dev = &pdev->dev;
  181. const struct of_device_id *of_id;
  182. struct ns_pinctrl *ns_pinctrl;
  183. struct pinctrl_desc *pctldesc;
  184. struct pinctrl_pin_desc *pin;
  185. struct resource *res;
  186. int i;
  187. ns_pinctrl = devm_kzalloc(dev, sizeof(*ns_pinctrl), GFP_KERNEL);
  188. if (!ns_pinctrl)
  189. return -ENOMEM;
  190. pctldesc = &ns_pinctrl->pctldesc;
  191. platform_set_drvdata(pdev, ns_pinctrl);
  192. /* Set basic properties */
  193. ns_pinctrl->dev = dev;
  194. of_id = of_match_device(ns_pinctrl_of_match_table, dev);
  195. if (!of_id)
  196. return -EINVAL;
  197. ns_pinctrl->chipset_flag = (uintptr_t)of_id->data;
  198. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  199. "cru_gpio_control");
  200. ns_pinctrl->base = devm_ioremap_resource(dev, res);
  201. if (IS_ERR(ns_pinctrl->base))
  202. return PTR_ERR(ns_pinctrl->base);
  203. memcpy(pctldesc, &ns_pinctrl_desc, sizeof(*pctldesc));
  204. /* Set pinctrl properties */
  205. pctldesc->pins = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_pins),
  206. sizeof(struct pinctrl_pin_desc),
  207. GFP_KERNEL);
  208. if (!pctldesc->pins)
  209. return -ENOMEM;
  210. for (i = 0, pin = (struct pinctrl_pin_desc *)&pctldesc->pins[0];
  211. i < ARRAY_SIZE(ns_pinctrl_pins); i++) {
  212. const struct pinctrl_pin_desc *src = &ns_pinctrl_pins[i];
  213. unsigned int chipsets = (uintptr_t)src->drv_data;
  214. if (chipsets & ns_pinctrl->chipset_flag) {
  215. memcpy(pin++, src, sizeof(*src));
  216. pctldesc->npins++;
  217. }
  218. }
  219. /* Register */
  220. ns_pinctrl->pctldev = devm_pinctrl_register(dev, pctldesc, ns_pinctrl);
  221. if (IS_ERR(ns_pinctrl->pctldev)) {
  222. dev_err(dev, "Failed to register pinctrl\n");
  223. return PTR_ERR(ns_pinctrl->pctldev);
  224. }
  225. for (i = 0; i < ARRAY_SIZE(ns_pinctrl_groups); i++) {
  226. const struct ns_pinctrl_group *group = &ns_pinctrl_groups[i];
  227. if (!(group->chipsets & ns_pinctrl->chipset_flag))
  228. continue;
  229. pinctrl_generic_add_group(ns_pinctrl->pctldev, group->name,
  230. group->pins, group->num_pins, NULL);
  231. }
  232. for (i = 0; i < ARRAY_SIZE(ns_pinctrl_functions); i++) {
  233. const struct ns_pinctrl_function *function = &ns_pinctrl_functions[i];
  234. if (!(function->chipsets & ns_pinctrl->chipset_flag))
  235. continue;
  236. pinmux_generic_add_function(ns_pinctrl->pctldev, function->name,
  237. function->groups,
  238. function->num_groups, NULL);
  239. }
  240. return 0;
  241. }
  242. static struct platform_driver ns_pinctrl_driver = {
  243. .probe = ns_pinctrl_probe,
  244. .driver = {
  245. .name = "ns-pinmux",
  246. .of_match_table = ns_pinctrl_of_match_table,
  247. },
  248. };
  249. module_platform_driver(ns_pinctrl_driver);
  250. MODULE_AUTHOR("Rafał Miłecki");
  251. MODULE_LICENSE("GPL v2");
  252. MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);