gpio-mm-lantiq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ltq_mm *chip;
  93. u32 shadow;
  94. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  95. if (!chip)
  96. return -ENOMEM;
  97. platform_set_drvdata(pdev, chip);
  98. chip->mmchip.gc.ngpio = 16;
  99. chip->mmchip.gc.direction_output = ltq_mm_dir_out;
  100. chip->mmchip.gc.set = ltq_mm_set;
  101. chip->mmchip.save_regs = ltq_mm_save_regs;
  102. /* store the shadow value if one was passed by the devicetree */
  103. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
  104. chip->shadow = shadow;
  105. return of_mm_gpiochip_add(pdev->dev.of_node, &chip->mmchip);
  106. }
  107. static int ltq_mm_remove(struct platform_device *pdev)
  108. {
  109. struct ltq_mm *chip = platform_get_drvdata(pdev);
  110. of_mm_gpiochip_remove(&chip->mmchip);
  111. return 0;
  112. }
  113. static const struct of_device_id ltq_mm_match[] = {
  114. { .compatible = "lantiq,gpio-mm" },
  115. {},
  116. };
  117. MODULE_DEVICE_TABLE(of, ltq_mm_match);
  118. static struct platform_driver ltq_mm_driver = {
  119. .probe = ltq_mm_probe,
  120. .remove = ltq_mm_remove,
  121. .driver = {
  122. .name = "gpio-mm-ltq",
  123. .of_match_table = ltq_mm_match,
  124. },
  125. };
  126. static int __init ltq_mm_init(void)
  127. {
  128. return platform_driver_register(&ltq_mm_driver);
  129. }
  130. subsys_initcall(ltq_mm_init);
  131. static void __exit ltq_mm_exit(void)
  132. {
  133. platform_driver_unregister(&ltq_mm_driver);
  134. }
  135. module_exit(ltq_mm_exit);