gpio-stp-xway.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 = gpiochip_get_data(gc);
  90. if (val)
  91. chip->shadow |= BIT(gpio);
  92. else
  93. chip->shadow &= ~BIT(gpio);
  94. xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
  95. xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
  96. }
  97. /**
  98. * xway_stp_dir_out() - gpio_chip->dir_out - set gpio direction.
  99. * @gc: Pointer to gpio_chip device structure.
  100. * @gpio: GPIO signal number.
  101. * @val: Value to be written to specified signal.
  102. *
  103. * Same as xway_stp_set, always returns 0.
  104. */
  105. static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
  106. {
  107. xway_stp_set(gc, gpio, val);
  108. return 0;
  109. }
  110. /**
  111. * xway_stp_request() - gpio_chip->request
  112. * @gc: Pointer to gpio_chip device structure.
  113. * @gpio: GPIO signal number.
  114. *
  115. * We mask out the HW driven pins
  116. */
  117. static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
  118. {
  119. struct xway_stp *chip = gpiochip_get_data(gc);
  120. if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
  121. dev_err(gc->parent, "GPIO %d is driven by hardware\n", gpio);
  122. return -ENODEV;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * xway_stp_hw_init() - Configure the STP unit and enable the clock gate
  128. * @virt: pointer to the remapped register range
  129. */
  130. static int xway_stp_hw_init(struct xway_stp *chip)
  131. {
  132. /* sane defaults */
  133. xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
  134. xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
  135. xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
  136. xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
  137. xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
  138. /* apply edge trigger settings for the shift register */
  139. xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
  140. chip->edge, XWAY_STP_CON0);
  141. /* apply led group settings */
  142. xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
  143. chip->groups, XWAY_STP_CON1);
  144. /* tell the hardware which pins are controlled by the dsl modem */
  145. xway_stp_w32_mask(chip->virt,
  146. XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
  147. chip->dsl << XWAY_STP_ADSL_SHIFT,
  148. XWAY_STP_CON0);
  149. /* tell the hardware which pins are controlled by the phys */
  150. xway_stp_w32_mask(chip->virt,
  151. XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
  152. chip->phy1 << XWAY_STP_PHY1_SHIFT,
  153. XWAY_STP_CON0);
  154. xway_stp_w32_mask(chip->virt,
  155. XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
  156. chip->phy2 << XWAY_STP_PHY2_SHIFT,
  157. XWAY_STP_CON1);
  158. /* mask out the hw driven bits in gpio_request */
  159. chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
  160. /*
  161. * if we have pins that are driven by hw, we need to tell the stp what
  162. * clock to use as a timer.
  163. */
  164. if (chip->reserved)
  165. xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
  166. XWAY_STP_UPD_FPI, XWAY_STP_CON1);
  167. return 0;
  168. }
  169. static int xway_stp_probe(struct platform_device *pdev)
  170. {
  171. struct resource *res;
  172. u32 shadow, groups, dsl, phy;
  173. struct xway_stp *chip;
  174. struct clk *clk;
  175. int ret = 0;
  176. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  177. if (!chip)
  178. return -ENOMEM;
  179. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  180. chip->virt = devm_ioremap_resource(&pdev->dev, res);
  181. if (IS_ERR(chip->virt))
  182. return PTR_ERR(chip->virt);
  183. chip->gc.parent = &pdev->dev;
  184. chip->gc.label = "stp-xway";
  185. chip->gc.direction_output = xway_stp_dir_out;
  186. chip->gc.set = xway_stp_set;
  187. chip->gc.request = xway_stp_request;
  188. chip->gc.base = -1;
  189. chip->gc.owner = THIS_MODULE;
  190. /* store the shadow value if one was passed by the devicetree */
  191. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
  192. chip->shadow = shadow;
  193. /* find out which gpio groups should be enabled */
  194. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
  195. chip->groups = groups & XWAY_STP_GROUP_MASK;
  196. else
  197. chip->groups = XWAY_STP_GROUP0;
  198. chip->gc.ngpio = fls(chip->groups) * 8;
  199. /* find out which gpios are controlled by the dsl core */
  200. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
  201. chip->dsl = dsl & XWAY_STP_ADSL_MASK;
  202. /* find out which gpios are controlled by the phys */
  203. if (of_machine_is_compatible("lantiq,ar9") ||
  204. of_machine_is_compatible("lantiq,gr9") ||
  205. of_machine_is_compatible("lantiq,vr9")) {
  206. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
  207. chip->phy1 = phy & XWAY_STP_PHY_MASK;
  208. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
  209. chip->phy2 = phy & XWAY_STP_PHY_MASK;
  210. }
  211. /* check which edge trigger we should use, default to a falling edge */
  212. if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
  213. chip->edge = XWAY_STP_FALLING;
  214. clk = clk_get(&pdev->dev, NULL);
  215. if (IS_ERR(clk)) {
  216. dev_err(&pdev->dev, "Failed to get clock\n");
  217. return PTR_ERR(clk);
  218. }
  219. clk_enable(clk);
  220. ret = xway_stp_hw_init(chip);
  221. if (!ret)
  222. ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
  223. if (!ret)
  224. dev_info(&pdev->dev, "Init done\n");
  225. return ret;
  226. }
  227. static const struct of_device_id xway_stp_match[] = {
  228. { .compatible = "lantiq,gpio-stp-xway" },
  229. {},
  230. };
  231. MODULE_DEVICE_TABLE(of, xway_stp_match);
  232. static struct platform_driver xway_stp_driver = {
  233. .probe = xway_stp_probe,
  234. .driver = {
  235. .name = "gpio-stp-xway",
  236. .of_match_table = xway_stp_match,
  237. },
  238. };
  239. static int __init xway_stp_init(void)
  240. {
  241. return platform_driver_register(&xway_stp_driver);
  242. }
  243. subsys_initcall(xway_stp_init);