gpio-mm-lantiq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ltq_mm *chip = gpiochip_get_data(gc);
  57. if (value)
  58. chip->shadow |= (1 << offset);
  59. else
  60. chip->shadow &= ~(1 << offset);
  61. ltq_mm_apply(chip);
  62. }
  63. /**
  64. * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction.
  65. * @gc: Pointer to gpio_chip device structure.
  66. * @gpio: GPIO signal number.
  67. * @val: Value to be written to specified signal.
  68. *
  69. * Same as ltq_mm_set, always returns 0.
  70. */
  71. static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value)
  72. {
  73. ltq_mm_set(gc, offset, value);
  74. return 0;
  75. }
  76. /**
  77. * ltq_mm_save_regs() - Set initial values of GPIO pins
  78. * @mm_gc: pointer to memory mapped GPIO chip structure
  79. */
  80. static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc)
  81. {
  82. struct ltq_mm *chip =
  83. container_of(mm_gc, struct ltq_mm, mmchip);
  84. /* tell the ebu controller which memory address we will be using */
  85. ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1);
  86. ltq_mm_apply(chip);
  87. }
  88. static int ltq_mm_probe(struct platform_device *pdev)
  89. {
  90. struct ltq_mm *chip;
  91. u32 shadow;
  92. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  93. if (!chip)
  94. return -ENOMEM;
  95. platform_set_drvdata(pdev, chip);
  96. chip->mmchip.gc.ngpio = 16;
  97. chip->mmchip.gc.direction_output = ltq_mm_dir_out;
  98. chip->mmchip.gc.set = ltq_mm_set;
  99. chip->mmchip.save_regs = ltq_mm_save_regs;
  100. /* store the shadow value if one was passed by the devicetree */
  101. if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
  102. chip->shadow = shadow;
  103. return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip);
  104. }
  105. static int ltq_mm_remove(struct platform_device *pdev)
  106. {
  107. struct ltq_mm *chip = platform_get_drvdata(pdev);
  108. of_mm_gpiochip_remove(&chip->mmchip);
  109. return 0;
  110. }
  111. static const struct of_device_id ltq_mm_match[] = {
  112. { .compatible = "lantiq,gpio-mm" },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, ltq_mm_match);
  116. static struct platform_driver ltq_mm_driver = {
  117. .probe = ltq_mm_probe,
  118. .remove = ltq_mm_remove,
  119. .driver = {
  120. .name = "gpio-mm-ltq",
  121. .of_match_table = ltq_mm_match,
  122. },
  123. };
  124. static int __init ltq_mm_init(void)
  125. {
  126. return platform_driver_register(&ltq_mm_driver);
  127. }
  128. subsys_initcall(ltq_mm_init);
  129. static void __exit ltq_mm_exit(void)
  130. {
  131. platform_driver_unregister(&ltq_mm_driver);
  132. }
  133. module_exit(ltq_mm_exit);