gpio-mm-lantiq.c 3.9 KB

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