gpio-stp-xway.c 8.1 KB

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