gpio-iop.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * arch/arm/plat-iop/gpio.c
  3. * GPIO handling for Intel IOP3xx processors.
  4. *
  5. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/gpio.h>
  17. #include <linux/export.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/bitops.h>
  20. #include <linux/io.h>
  21. #define IOP3XX_N_GPIOS 8
  22. #define GPIO_IN 0
  23. #define GPIO_OUT 1
  24. #define GPIO_LOW 0
  25. #define GPIO_HIGH 1
  26. /* Memory base offset */
  27. static void __iomem *base;
  28. #define IOP3XX_GPIO_REG(reg) (base + (reg))
  29. #define IOP3XX_GPOE IOP3XX_GPIO_REG(0x0000)
  30. #define IOP3XX_GPID IOP3XX_GPIO_REG(0x0004)
  31. #define IOP3XX_GPOD IOP3XX_GPIO_REG(0x0008)
  32. static void gpio_line_config(int line, int direction)
  33. {
  34. unsigned long flags;
  35. u32 val;
  36. local_irq_save(flags);
  37. val = readl(IOP3XX_GPOE);
  38. if (direction == GPIO_IN) {
  39. val |= BIT(line);
  40. } else if (direction == GPIO_OUT) {
  41. val &= ~BIT(line);
  42. }
  43. writel(val, IOP3XX_GPOE);
  44. local_irq_restore(flags);
  45. }
  46. static int gpio_line_get(int line)
  47. {
  48. return !!(readl(IOP3XX_GPID) & BIT(line));
  49. }
  50. static void gpio_line_set(int line, int value)
  51. {
  52. unsigned long flags;
  53. u32 val;
  54. local_irq_save(flags);
  55. val = readl(IOP3XX_GPOD);
  56. if (value == GPIO_LOW) {
  57. val &= ~BIT(line);
  58. } else if (value == GPIO_HIGH) {
  59. val |= BIT(line);
  60. }
  61. writel(val, IOP3XX_GPOD);
  62. local_irq_restore(flags);
  63. }
  64. static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  65. {
  66. gpio_line_config(gpio, GPIO_IN);
  67. return 0;
  68. }
  69. static int iop3xx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
  70. {
  71. gpio_line_set(gpio, level);
  72. gpio_line_config(gpio, GPIO_OUT);
  73. return 0;
  74. }
  75. static int iop3xx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  76. {
  77. return gpio_line_get(gpio);
  78. }
  79. static void iop3xx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
  80. {
  81. gpio_line_set(gpio, value);
  82. }
  83. static struct gpio_chip iop3xx_chip = {
  84. .label = "iop3xx",
  85. .direction_input = iop3xx_gpio_direction_input,
  86. .get = iop3xx_gpio_get_value,
  87. .direction_output = iop3xx_gpio_direction_output,
  88. .set = iop3xx_gpio_set_value,
  89. .base = 0,
  90. .ngpio = IOP3XX_N_GPIOS,
  91. };
  92. static int iop3xx_gpio_probe(struct platform_device *pdev)
  93. {
  94. struct resource *res;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. base = devm_ioremap_resource(&pdev->dev, res);
  97. if (IS_ERR(base))
  98. return PTR_ERR(base);
  99. return gpiochip_add(&iop3xx_chip);
  100. }
  101. static struct platform_driver iop3xx_gpio_driver = {
  102. .driver = {
  103. .name = "gpio-iop",
  104. },
  105. .probe = iop3xx_gpio_probe,
  106. };
  107. static int __init iop3xx_gpio_init(void)
  108. {
  109. return platform_driver_register(&iop3xx_gpio_driver);
  110. }
  111. arch_initcall(iop3xx_gpio_init);
  112. MODULE_DESCRIPTION("GPIO handling for Intel IOP3xx processors");
  113. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  114. MODULE_LICENSE("GPL");