gpio-stp-xway.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2012 John Crispin <john@phrozen.org>
  5. */
  6. #include <linux/platform_device.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/of.h>
  12. #include <linux/mutex.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/io.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. /*
  18. * The Serial To Parallel (STP) is found on MIPS based Lantiq socs. It is a
  19. * peripheral controller used to drive external shift register cascades. At most
  20. * 3 groups of 8 bits can be driven. The hardware is able to allow the DSL modem
  21. * to drive the 2 LSBs of the cascade automatically.
  22. */
  23. /* control register 0 */
  24. #define XWAY_STP_CON0 0x00
  25. /* control register 1 */
  26. #define XWAY_STP_CON1 0x04
  27. /* data register 0 */
  28. #define XWAY_STP_CPU0 0x08
  29. /* data register 1 */
  30. #define XWAY_STP_CPU1 0x0C
  31. /* access register */
  32. #define XWAY_STP_AR 0x10
  33. /* software or hardware update select bit */
  34. #define XWAY_STP_CON_SWU BIT(31)
  35. /* automatic update rates */
  36. #define XWAY_STP_2HZ 0
  37. #define XWAY_STP_4HZ BIT(23)
  38. #define XWAY_STP_8HZ BIT(24)
  39. #define XWAY_STP_10HZ (BIT(24) | BIT(23))
  40. #define XWAY_STP_SPEED_MASK (BIT(23) | BIT(24) | BIT(25) | BIT(26) | BIT(27))
  41. #define XWAY_STP_FPIS_VALUE BIT(21)
  42. #define XWAY_STP_FPIS_MASK (BIT(20) | BIT(21))
  43. /* clock source for automatic update */
  44. #define XWAY_STP_UPD_FPI BIT(31)
  45. #define XWAY_STP_UPD_MASK (BIT(31) | BIT(30))
  46. /* let the adsl core drive the 2 LSBs */
  47. #define XWAY_STP_ADSL_SHIFT 24
  48. #define XWAY_STP_ADSL_MASK 0x3
  49. /* 2 groups of 3 bits can be driven by the phys */
  50. #define XWAY_STP_PHY_MASK 0x7
  51. #define XWAY_STP_PHY1_SHIFT 27
  52. #define XWAY_STP_PHY2_SHIFT 3
  53. #define XWAY_STP_PHY3_SHIFT 6
  54. #define XWAY_STP_PHY4_SHIFT 15
  55. /* STP has 3 groups of 8 bits */
  56. #define XWAY_STP_GROUP0 BIT(0)
  57. #define XWAY_STP_GROUP1 BIT(1)
  58. #define XWAY_STP_GROUP2 BIT(2)
  59. #define XWAY_STP_GROUP_MASK (0x7)
  60. /* Edge configuration bits */
  61. #define XWAY_STP_FALLING BIT(26)
  62. #define XWAY_STP_EDGE_MASK BIT(26)
  63. #define xway_stp_r32(m, reg) __raw_readl(m + reg)
  64. #define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg)
  65. #define xway_stp_w32_mask(m, clear, set, reg) \
  66. xway_stp_w32(m, (xway_stp_r32(m, reg) & ~(clear)) | (set), reg)
  67. struct xway_stp {
  68. struct gpio_chip gc;
  69. void __iomem *virt;
  70. u32 edge; /* rising or falling edge triggered shift register */
  71. u32 shadow; /* shadow the shift registers state */
  72. u8 groups; /* we can drive 1-3 groups of 8bit each */
  73. u8 dsl; /* the 2 LSBs can be driven by the dsl core */
  74. u8 phy1; /* 3 bits can be driven by phy1 */
  75. u8 phy2; /* 3 bits can be driven by phy2 */
  76. u8 phy3; /* 3 bits can be driven by phy3 */
  77. u8 phy4; /* 3 bits can be driven by phy4 */
  78. u8 reserved; /* mask out the hw driven bits in gpio_request */
  79. };
  80. /**
  81. * xway_stp_get() - gpio_chip->get - get gpios.
  82. * @gc: Pointer to gpio_chip device structure.
  83. * @gpio: GPIO signal number.
  84. *
  85. * Gets the shadow value.
  86. */
  87. static int xway_stp_get(struct gpio_chip *gc, unsigned int gpio)
  88. {
  89. struct xway_stp *chip = gpiochip_get_data(gc);
  90. return (xway_stp_r32(chip->virt, XWAY_STP_CPU0) & BIT(gpio));
  91. }
  92. /**
  93. * xway_stp_set() - gpio_chip->set - set gpios.
  94. * @gc: Pointer to gpio_chip device structure.
  95. * @gpio: GPIO signal number.
  96. * @val: Value to be written to specified signal.
  97. *
  98. * Set the shadow value and call ltq_ebu_apply.
  99. */
  100. static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
  101. {
  102. struct xway_stp *chip = gpiochip_get_data(gc);
  103. if (val)
  104. chip->shadow |= BIT(gpio);
  105. else
  106. chip->shadow &= ~BIT(gpio);
  107. xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
  108. if (!chip->reserved)
  109. xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
  110. }
  111. /**
  112. * xway_stp_dir_out() - gpio_chip->dir_out - set gpio direction.
  113. * @gc: Pointer to gpio_chip device structure.
  114. * @gpio: GPIO signal number.
  115. * @val: Value to be written to specified signal.
  116. *
  117. * Same as xway_stp_set, always returns 0.
  118. */
  119. static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
  120. {
  121. xway_stp_set(gc, gpio, val);
  122. return 0;
  123. }
  124. /**
  125. * xway_stp_request() - gpio_chip->request
  126. * @gc: Pointer to gpio_chip device structure.
  127. * @gpio: GPIO signal number.
  128. *
  129. * We mask out the HW driven pins
  130. */
  131. static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
  132. {
  133. struct xway_stp *chip = gpiochip_get_data(gc);
  134. if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
  135. dev_err(gc->parent, "GPIO %d is driven by hardware\n", gpio);
  136. return -ENODEV;
  137. }
  138. return 0;
  139. }
  140. /**
  141. * xway_stp_hw_init() - Configure the STP unit and enable the clock gate
  142. * @chip: Pointer to the xway_stp chip structure
  143. */
  144. static void xway_stp_hw_init(struct xway_stp *chip)
  145. {
  146. /* sane defaults */
  147. xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
  148. xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
  149. xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
  150. xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
  151. xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
  152. /* apply edge trigger settings for the shift register */
  153. xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
  154. chip->edge, XWAY_STP_CON0);
  155. /* apply led group settings */
  156. xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
  157. chip->groups, XWAY_STP_CON1);
  158. /* tell the hardware which pins are controlled by the dsl modem */
  159. xway_stp_w32_mask(chip->virt,
  160. XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
  161. chip->dsl << XWAY_STP_ADSL_SHIFT,
  162. XWAY_STP_CON0);
  163. /* tell the hardware which pins are controlled by the phys */
  164. xway_stp_w32_mask(chip->virt,
  165. XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
  166. chip->phy1 << XWAY_STP_PHY1_SHIFT,
  167. XWAY_STP_CON0);
  168. xway_stp_w32_mask(chip->virt,
  169. XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
  170. chip->phy2 << XWAY_STP_PHY2_SHIFT,
  171. XWAY_STP_CON1);
  172. if (of_machine_is_compatible("lantiq,grx390")
  173. || of_machine_is_compatible("lantiq,ar10")) {
  174. xway_stp_w32_mask(chip->virt,
  175. XWAY_STP_PHY_MASK << XWAY_STP_PHY3_SHIFT,
  176. chip->phy3 << XWAY_STP_PHY3_SHIFT,
  177. XWAY_STP_CON1);
  178. }
  179. if (of_machine_is_compatible("lantiq,grx390")) {
  180. xway_stp_w32_mask(chip->virt,
  181. XWAY_STP_PHY_MASK << XWAY_STP_PHY4_SHIFT,
  182. chip->phy4 << XWAY_STP_PHY4_SHIFT,
  183. XWAY_STP_CON1);
  184. }
  185. /* mask out the hw driven bits in gpio_request */
  186. chip->reserved = (chip->phy4 << 11) | (chip->phy3 << 8) | (chip->phy2 << 5)
  187. | (chip->phy1 << 2) | chip->dsl;
  188. /*
  189. * if we have pins that are driven by hw, we need to tell the stp what
  190. * clock to use as a timer.
  191. */
  192. if (chip->reserved) {
  193. xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
  194. XWAY_STP_UPD_FPI, XWAY_STP_CON1);
  195. xway_stp_w32_mask(chip->virt, XWAY_STP_SPEED_MASK,
  196. XWAY_STP_10HZ, XWAY_STP_CON1);
  197. xway_stp_w32_mask(chip->virt, XWAY_STP_FPIS_MASK,
  198. XWAY_STP_FPIS_VALUE, XWAY_STP_CON1);
  199. }
  200. }
  201. static int xway_stp_probe(struct platform_device *pdev)
  202. {
  203. u32 shadow, groups, dsl, phy;
  204. struct xway_stp *chip;
  205. struct clk *clk;
  206. int ret = 0;
  207. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  208. if (!chip)
  209. return -ENOMEM;
  210. chip->virt = devm_platform_ioremap_resource(pdev, 0);
  211. if (IS_ERR(chip->virt))
  212. return PTR_ERR(chip->virt);
  213. chip->gc.parent = &pdev->dev;
  214. chip->gc.label = "stp-xway";
  215. chip->gc.direction_output = xway_stp_dir_out;
  216. chip->gc.get = xway_stp_get;
  217. chip->gc.set = xway_stp_set;
  218. chip->gc.request = xway_stp_request;
  219. chip->gc.base = -1;
  220. chip->gc.owner = THIS_MODULE;
  221. /* store the shadow value if one was passed by the devicetree */
  222. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
  223. chip->shadow = shadow;
  224. /* find out which gpio groups should be enabled */
  225. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
  226. chip->groups = groups & XWAY_STP_GROUP_MASK;
  227. else
  228. chip->groups = XWAY_STP_GROUP0;
  229. chip->gc.ngpio = fls(chip->groups) * 8;
  230. /* find out which gpios are controlled by the dsl core */
  231. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
  232. chip->dsl = dsl & XWAY_STP_ADSL_MASK;
  233. /* find out which gpios are controlled by the phys */
  234. if (of_machine_is_compatible("lantiq,ar9") ||
  235. of_machine_is_compatible("lantiq,gr9") ||
  236. of_machine_is_compatible("lantiq,vr9") ||
  237. of_machine_is_compatible("lantiq,ar10") ||
  238. of_machine_is_compatible("lantiq,grx390")) {
  239. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
  240. chip->phy1 = phy & XWAY_STP_PHY_MASK;
  241. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
  242. chip->phy2 = phy & XWAY_STP_PHY_MASK;
  243. }
  244. if (of_machine_is_compatible("lantiq,ar10") ||
  245. of_machine_is_compatible("lantiq,grx390")) {
  246. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy3", &phy))
  247. chip->phy3 = phy & XWAY_STP_PHY_MASK;
  248. }
  249. if (of_machine_is_compatible("lantiq,grx390")) {
  250. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy4", &phy))
  251. chip->phy4 = phy & XWAY_STP_PHY_MASK;
  252. }
  253. /* check which edge trigger we should use, default to a falling edge */
  254. if (!of_property_read_bool(pdev->dev.of_node, "lantiq,rising"))
  255. chip->edge = XWAY_STP_FALLING;
  256. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  257. if (IS_ERR(clk)) {
  258. dev_err(&pdev->dev, "Failed to get clock\n");
  259. return PTR_ERR(clk);
  260. }
  261. xway_stp_hw_init(chip);
  262. ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
  263. if (ret)
  264. return ret;
  265. dev_info(&pdev->dev, "Init done\n");
  266. return 0;
  267. }
  268. static const struct of_device_id xway_stp_match[] = {
  269. { .compatible = "lantiq,gpio-stp-xway" },
  270. {},
  271. };
  272. MODULE_DEVICE_TABLE(of, xway_stp_match);
  273. static struct platform_driver xway_stp_driver = {
  274. .probe = xway_stp_probe,
  275. .driver = {
  276. .name = "gpio-stp-xway",
  277. .of_match_table = xway_stp_match,
  278. },
  279. };
  280. static int __init xway_stp_init(void)
  281. {
  282. return platform_driver_register(&xway_stp_driver);
  283. }
  284. subsys_initcall(xway_stp_init);