gpio-stp-xway.c 8.4 KB

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