gpio-amdpt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * AMD Promontory GPIO driver
  3. *
  4. * Copyright (C) 2015 ASMedia Technology Inc.
  5. * Author: YD Tseng <yd_tseng@asmedia.com.tw>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/acpi.h>
  16. #include <linux/platform_device.h>
  17. #define PT_TOTAL_GPIO 8
  18. /* PCI-E MMIO register offsets */
  19. #define PT_DIRECTION_REG 0x00
  20. #define PT_INPUTDATA_REG 0x04
  21. #define PT_OUTPUTDATA_REG 0x08
  22. #define PT_CLOCKRATE_REG 0x0C
  23. #define PT_SYNC_REG 0x28
  24. struct pt_gpio_chip {
  25. struct gpio_chip gc;
  26. void __iomem *reg_base;
  27. };
  28. static int pt_gpio_request(struct gpio_chip *gc, unsigned offset)
  29. {
  30. struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
  31. unsigned long flags;
  32. u32 using_pins;
  33. dev_dbg(gc->parent, "pt_gpio_request offset=%x\n", offset);
  34. spin_lock_irqsave(&gc->bgpio_lock, flags);
  35. using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
  36. if (using_pins & BIT(offset)) {
  37. dev_warn(gc->parent, "PT GPIO pin %x reconfigured\n",
  38. offset);
  39. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  40. return -EINVAL;
  41. }
  42. writel(using_pins | BIT(offset), pt_gpio->reg_base + PT_SYNC_REG);
  43. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  44. return 0;
  45. }
  46. static void pt_gpio_free(struct gpio_chip *gc, unsigned offset)
  47. {
  48. struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
  49. unsigned long flags;
  50. u32 using_pins;
  51. spin_lock_irqsave(&gc->bgpio_lock, flags);
  52. using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
  53. using_pins &= ~BIT(offset);
  54. writel(using_pins, pt_gpio->reg_base + PT_SYNC_REG);
  55. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  56. dev_dbg(gc->parent, "pt_gpio_free offset=%x\n", offset);
  57. }
  58. static int pt_gpio_probe(struct platform_device *pdev)
  59. {
  60. struct device *dev = &pdev->dev;
  61. struct acpi_device *acpi_dev;
  62. acpi_handle handle = ACPI_HANDLE(dev);
  63. struct pt_gpio_chip *pt_gpio;
  64. struct resource *res_mem;
  65. int ret = 0;
  66. if (acpi_bus_get_device(handle, &acpi_dev)) {
  67. dev_err(dev, "PT GPIO device node not found\n");
  68. return -ENODEV;
  69. }
  70. pt_gpio = devm_kzalloc(dev, sizeof(struct pt_gpio_chip), GFP_KERNEL);
  71. if (!pt_gpio)
  72. return -ENOMEM;
  73. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  74. if (!res_mem) {
  75. dev_err(&pdev->dev, "Failed to get MMIO resource for PT GPIO.\n");
  76. return -EINVAL;
  77. }
  78. pt_gpio->reg_base = devm_ioremap_resource(dev, res_mem);
  79. if (IS_ERR(pt_gpio->reg_base)) {
  80. dev_err(&pdev->dev, "Failed to map MMIO resource for PT GPIO.\n");
  81. return PTR_ERR(pt_gpio->reg_base);
  82. }
  83. ret = bgpio_init(&pt_gpio->gc, dev, 4,
  84. pt_gpio->reg_base + PT_INPUTDATA_REG,
  85. pt_gpio->reg_base + PT_OUTPUTDATA_REG, NULL,
  86. pt_gpio->reg_base + PT_DIRECTION_REG, NULL,
  87. BGPIOF_READ_OUTPUT_REG_SET);
  88. if (ret) {
  89. dev_err(&pdev->dev, "bgpio_init failed\n");
  90. return ret;
  91. }
  92. pt_gpio->gc.owner = THIS_MODULE;
  93. pt_gpio->gc.request = pt_gpio_request;
  94. pt_gpio->gc.free = pt_gpio_free;
  95. pt_gpio->gc.ngpio = PT_TOTAL_GPIO;
  96. #if defined(CONFIG_OF_GPIO)
  97. pt_gpio->gc.of_node = pdev->dev.of_node;
  98. #endif
  99. ret = gpiochip_add_data(&pt_gpio->gc, pt_gpio);
  100. if (ret) {
  101. dev_err(&pdev->dev, "Failed to register GPIO lib\n");
  102. return ret;
  103. }
  104. platform_set_drvdata(pdev, pt_gpio);
  105. /* initialize register setting */
  106. writel(0, pt_gpio->reg_base + PT_SYNC_REG);
  107. writel(0, pt_gpio->reg_base + PT_CLOCKRATE_REG);
  108. dev_dbg(&pdev->dev, "PT GPIO driver loaded\n");
  109. return ret;
  110. }
  111. static int pt_gpio_remove(struct platform_device *pdev)
  112. {
  113. struct pt_gpio_chip *pt_gpio = platform_get_drvdata(pdev);
  114. gpiochip_remove(&pt_gpio->gc);
  115. return 0;
  116. }
  117. static const struct acpi_device_id pt_gpio_acpi_match[] = {
  118. { "AMDF030", 0 },
  119. { "AMDIF030", 0 },
  120. { },
  121. };
  122. MODULE_DEVICE_TABLE(acpi, pt_gpio_acpi_match);
  123. static struct platform_driver pt_gpio_driver = {
  124. .driver = {
  125. .name = "pt-gpio",
  126. .acpi_match_table = ACPI_PTR(pt_gpio_acpi_match),
  127. },
  128. .probe = pt_gpio_probe,
  129. .remove = pt_gpio_remove,
  130. };
  131. module_platform_driver(pt_gpio_driver);
  132. MODULE_LICENSE("GPL");
  133. MODULE_AUTHOR("YD Tseng <yd_tseng@asmedia.com.tw>");
  134. MODULE_DESCRIPTION("AMD Promontory GPIO Driver");