gpio-hlwd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (C) 2008-2009 The GameCube Linux Team
  3. // Copyright (C) 2008,2009 Albert Herranz
  4. // Copyright (C) 2017-2018 Jonathan Neuschäfer
  5. //
  6. // Nintendo Wii (Hollywood) GPIO driver
  7. #include <linux/gpio/driver.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/slab.h>
  14. /*
  15. * Register names and offsets courtesy of WiiBrew:
  16. * https://wiibrew.org/wiki/Hardware/Hollywood_GPIOs
  17. *
  18. * Note that for most registers, there are two versions:
  19. * - HW_GPIOB_* Is always accessible by the Broadway PowerPC core, but does
  20. * always give access to all GPIO lines
  21. * - HW_GPIO_* Is only accessible by the Broadway PowerPC code if the memory
  22. * firewall (AHBPROT) in the Hollywood chipset has been configured to allow
  23. * such access.
  24. *
  25. * The ownership of each GPIO line can be configured in the HW_GPIO_OWNER
  26. * register: A one bit configures the line for access via the HW_GPIOB_*
  27. * registers, a zero bit indicates access via HW_GPIO_*. This driver uses
  28. * HW_GPIOB_*.
  29. */
  30. #define HW_GPIOB_OUT 0x00
  31. #define HW_GPIOB_DIR 0x04
  32. #define HW_GPIOB_IN 0x08
  33. #define HW_GPIOB_INTLVL 0x0c
  34. #define HW_GPIOB_INTFLAG 0x10
  35. #define HW_GPIOB_INTMASK 0x14
  36. #define HW_GPIOB_INMIR 0x18
  37. #define HW_GPIO_ENABLE 0x1c
  38. #define HW_GPIO_OUT 0x20
  39. #define HW_GPIO_DIR 0x24
  40. #define HW_GPIO_IN 0x28
  41. #define HW_GPIO_INTLVL 0x2c
  42. #define HW_GPIO_INTFLAG 0x30
  43. #define HW_GPIO_INTMASK 0x34
  44. #define HW_GPIO_INMIR 0x38
  45. #define HW_GPIO_OWNER 0x3c
  46. struct hlwd_gpio {
  47. struct gpio_chip gpioc;
  48. void __iomem *regs;
  49. };
  50. static int hlwd_gpio_probe(struct platform_device *pdev)
  51. {
  52. struct hlwd_gpio *hlwd;
  53. struct resource *regs_resource;
  54. u32 ngpios;
  55. int res;
  56. hlwd = devm_kzalloc(&pdev->dev, sizeof(*hlwd), GFP_KERNEL);
  57. if (!hlwd)
  58. return -ENOMEM;
  59. regs_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  60. hlwd->regs = devm_ioremap_resource(&pdev->dev, regs_resource);
  61. if (IS_ERR(hlwd->regs))
  62. return PTR_ERR(hlwd->regs);
  63. /*
  64. * Claim all GPIOs using the OWNER register. This will not work on
  65. * systems where the AHBPROT memory firewall hasn't been configured to
  66. * permit PPC access to HW_GPIO_*.
  67. *
  68. * Note that this has to happen before bgpio_init reads the
  69. * HW_GPIOB_OUT and HW_GPIOB_DIR, because otherwise it reads the wrong
  70. * values.
  71. */
  72. iowrite32be(0xffffffff, hlwd->regs + HW_GPIO_OWNER);
  73. res = bgpio_init(&hlwd->gpioc, &pdev->dev, 4,
  74. hlwd->regs + HW_GPIOB_IN, hlwd->regs + HW_GPIOB_OUT,
  75. NULL, hlwd->regs + HW_GPIOB_DIR, NULL,
  76. BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  77. if (res < 0) {
  78. dev_warn(&pdev->dev, "bgpio_init failed: %d\n", res);
  79. return res;
  80. }
  81. res = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios);
  82. if (res)
  83. ngpios = 32;
  84. hlwd->gpioc.ngpio = ngpios;
  85. return devm_gpiochip_add_data(&pdev->dev, &hlwd->gpioc, hlwd);
  86. }
  87. static const struct of_device_id hlwd_gpio_match[] = {
  88. { .compatible = "nintendo,hollywood-gpio", },
  89. {},
  90. };
  91. MODULE_DEVICE_TABLE(of, hlwd_gpio_match);
  92. static struct platform_driver hlwd_gpio_driver = {
  93. .driver = {
  94. .name = "gpio-hlwd",
  95. .of_match_table = hlwd_gpio_match,
  96. },
  97. .probe = hlwd_gpio_probe,
  98. };
  99. module_platform_driver(hlwd_gpio_driver);
  100. MODULE_AUTHOR("Jonathan Neuschäfer <j.neuschaefer@gmx.net>");
  101. MODULE_DESCRIPTION("Nintendo Wii GPIO driver");
  102. MODULE_LICENSE("GPL");