gpio-mm-lantiq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mutex.h>
  13. #include <linux/gpio.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <lantiq_soc.h>
  19. /*
  20. * By attaching hardware latches to the EBU it is possible to create output
  21. * only gpios. This driver configures a special memory address, which when
  22. * written to outputs 16 bit to the latches.
  23. */
  24. #define LTQ_EBU_BUSCON 0x1e7ff /* 16 bit access, slowest timing */
  25. #define LTQ_EBU_WP 0x80000000 /* write protect bit */
  26. struct ltq_mm {
  27. struct of_mm_gpio_chip mmchip;
  28. u16 shadow; /* shadow the latches state */
  29. };
  30. /**
  31. * ltq_mm_apply() - write the shadow value to the ebu address.
  32. * @chip: Pointer to our private data structure.
  33. *
  34. * Write the shadow value to the EBU to set the gpios. We need to set the
  35. * global EBU lock to make sure that PCI/MTD dont break.
  36. */
  37. static void ltq_mm_apply(struct ltq_mm *chip)
  38. {
  39. unsigned long flags;
  40. spin_lock_irqsave(&ebu_lock, flags);
  41. ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1);
  42. __raw_writew(chip->shadow, chip->mmchip.regs);
  43. ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1);
  44. spin_unlock_irqrestore(&ebu_lock, flags);
  45. }
  46. /**
  47. * ltq_mm_set() - gpio_chip->set - set gpios.
  48. * @gc: Pointer to gpio_chip device structure.
  49. * @gpio: GPIO signal number.
  50. * @val: Value to be written to specified signal.
  51. *
  52. * Set the shadow value and call ltq_mm_apply.
  53. */
  54. static void ltq_mm_set(struct gpio_chip *gc, unsigned offset, int value)
  55. {
  56. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  57. struct ltq_mm *chip =
  58. container_of(mm_gc, struct ltq_mm, mmchip);
  59. if (value)
  60. chip->shadow |= (1 << offset);
  61. else
  62. chip->shadow &= ~(1 << offset);
  63. ltq_mm_apply(chip);
  64. }
  65. /**
  66. * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction.
  67. * @gc: Pointer to gpio_chip device structure.
  68. * @gpio: GPIO signal number.
  69. * @val: Value to be written to specified signal.
  70. *
  71. * Same as ltq_mm_set, always returns 0.
  72. */
  73. static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value)
  74. {
  75. ltq_mm_set(gc, offset, value);
  76. return 0;
  77. }
  78. /**
  79. * ltq_mm_save_regs() - Set initial values of GPIO pins
  80. * @mm_gc: pointer to memory mapped GPIO chip structure
  81. */
  82. static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc)
  83. {
  84. struct ltq_mm *chip =
  85. container_of(mm_gc, struct ltq_mm, mmchip);
  86. /* tell the ebu controller which memory address we will be using */
  87. ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1);
  88. ltq_mm_apply(chip);
  89. }
  90. static int ltq_mm_probe(struct platform_device *pdev)
  91. {
  92. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  93. struct ltq_mm *chip;
  94. const __be32 *shadow;
  95. int ret = 0;
  96. if (!res) {
  97. dev_err(&pdev->dev, "failed to get memory resource\n");
  98. return -ENOENT;
  99. }
  100. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  101. if (!chip)
  102. return -ENOMEM;
  103. chip->mmchip.gc.ngpio = 16;
  104. chip->mmchip.gc.label = "gpio-mm-ltq";
  105. chip->mmchip.gc.direction_output = ltq_mm_dir_out;
  106. chip->mmchip.gc.set = ltq_mm_set;
  107. chip->mmchip.save_regs = ltq_mm_save_regs;
  108. /* store the shadow value if one was passed by the devicetree */
  109. shadow = of_get_property(pdev->dev.of_node, "lantiq,shadow", NULL);
  110. if (shadow)
  111. chip->shadow = be32_to_cpu(*shadow);
  112. ret = of_mm_gpiochip_add(pdev->dev.of_node, &chip->mmchip);
  113. if (ret)
  114. kfree(chip);
  115. return ret;
  116. }
  117. static const struct of_device_id ltq_mm_match[] = {
  118. { .compatible = "lantiq,gpio-mm" },
  119. {},
  120. };
  121. MODULE_DEVICE_TABLE(of, ltq_mm_match);
  122. static struct platform_driver ltq_mm_driver = {
  123. .probe = ltq_mm_probe,
  124. .driver = {
  125. .name = "gpio-mm-ltq",
  126. .owner = THIS_MODULE,
  127. .of_match_table = ltq_mm_match,
  128. },
  129. };
  130. static int __init ltq_mm_init(void)
  131. {
  132. return platform_driver_register(&ltq_mm_driver);
  133. }
  134. subsys_initcall(ltq_mm_init);