gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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) 2010 John Crispin <blogic@openwrt.org>
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/export.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/gpio.h>
  12. #include <linux/ioport.h>
  13. #include <linux/io.h>
  14. #include <lantiq_soc.h>
  15. #define LTQ_GPIO_OUT 0x00
  16. #define LTQ_GPIO_IN 0x04
  17. #define LTQ_GPIO_DIR 0x08
  18. #define LTQ_GPIO_ALTSEL0 0x0C
  19. #define LTQ_GPIO_ALTSEL1 0x10
  20. #define LTQ_GPIO_OD 0x14
  21. #define PINS_PER_PORT 16
  22. #define MAX_PORTS 3
  23. #define ltq_gpio_getbit(m, r, p) (!!(ltq_r32(m + r) & (1 << p)))
  24. #define ltq_gpio_setbit(m, r, p) ltq_w32_mask(0, (1 << p), m + r)
  25. #define ltq_gpio_clearbit(m, r, p) ltq_w32_mask((1 << p), 0, m + r)
  26. struct ltq_gpio {
  27. void __iomem *membase;
  28. struct gpio_chip chip;
  29. };
  30. static struct ltq_gpio ltq_gpio_port[MAX_PORTS];
  31. int ltq_gpio_request(unsigned int pin, unsigned int alt0,
  32. unsigned int alt1, unsigned int dir, const char *name)
  33. {
  34. int id = 0;
  35. if (pin >= (MAX_PORTS * PINS_PER_PORT))
  36. return -EINVAL;
  37. if (gpio_request(pin, name)) {
  38. pr_err("failed to setup lantiq gpio: %s\n", name);
  39. return -EBUSY;
  40. }
  41. if (dir)
  42. gpio_direction_output(pin, 1);
  43. else
  44. gpio_direction_input(pin);
  45. while (pin >= PINS_PER_PORT) {
  46. pin -= PINS_PER_PORT;
  47. id++;
  48. }
  49. if (alt0)
  50. ltq_gpio_setbit(ltq_gpio_port[id].membase,
  51. LTQ_GPIO_ALTSEL0, pin);
  52. else
  53. ltq_gpio_clearbit(ltq_gpio_port[id].membase,
  54. LTQ_GPIO_ALTSEL0, pin);
  55. if (alt1)
  56. ltq_gpio_setbit(ltq_gpio_port[id].membase,
  57. LTQ_GPIO_ALTSEL1, pin);
  58. else
  59. ltq_gpio_clearbit(ltq_gpio_port[id].membase,
  60. LTQ_GPIO_ALTSEL1, pin);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL(ltq_gpio_request);
  64. static void ltq_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  65. {
  66. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  67. if (value)
  68. ltq_gpio_setbit(ltq_gpio->membase, LTQ_GPIO_OUT, offset);
  69. else
  70. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_OUT, offset);
  71. }
  72. static int ltq_gpio_get(struct gpio_chip *chip, unsigned int offset)
  73. {
  74. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  75. return ltq_gpio_getbit(ltq_gpio->membase, LTQ_GPIO_IN, offset);
  76. }
  77. static int ltq_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  78. {
  79. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  80. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_OD, offset);
  81. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_DIR, offset);
  82. return 0;
  83. }
  84. static int ltq_gpio_direction_output(struct gpio_chip *chip,
  85. unsigned int offset, int value)
  86. {
  87. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  88. ltq_gpio_setbit(ltq_gpio->membase, LTQ_GPIO_OD, offset);
  89. ltq_gpio_setbit(ltq_gpio->membase, LTQ_GPIO_DIR, offset);
  90. ltq_gpio_set(chip, offset, value);
  91. return 0;
  92. }
  93. static int ltq_gpio_req(struct gpio_chip *chip, unsigned offset)
  94. {
  95. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  96. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_ALTSEL0, offset);
  97. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_ALTSEL1, offset);
  98. return 0;
  99. }
  100. static int ltq_gpio_probe(struct platform_device *pdev)
  101. {
  102. struct resource *res;
  103. if (pdev->id >= MAX_PORTS) {
  104. dev_err(&pdev->dev, "invalid gpio port %d\n",
  105. pdev->id);
  106. return -EINVAL;
  107. }
  108. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. if (!res) {
  110. dev_err(&pdev->dev, "failed to get memory for gpio port %d\n",
  111. pdev->id);
  112. return -ENOENT;
  113. }
  114. res = devm_request_mem_region(&pdev->dev, res->start,
  115. resource_size(res), dev_name(&pdev->dev));
  116. if (!res) {
  117. dev_err(&pdev->dev,
  118. "failed to request memory for gpio port %d\n",
  119. pdev->id);
  120. return -EBUSY;
  121. }
  122. ltq_gpio_port[pdev->id].membase = devm_ioremap_nocache(&pdev->dev,
  123. res->start, resource_size(res));
  124. if (!ltq_gpio_port[pdev->id].membase) {
  125. dev_err(&pdev->dev, "failed to remap memory for gpio port %d\n",
  126. pdev->id);
  127. return -ENOMEM;
  128. }
  129. ltq_gpio_port[pdev->id].chip.label = "ltq_gpio";
  130. ltq_gpio_port[pdev->id].chip.direction_input = ltq_gpio_direction_input;
  131. ltq_gpio_port[pdev->id].chip.direction_output =
  132. ltq_gpio_direction_output;
  133. ltq_gpio_port[pdev->id].chip.get = ltq_gpio_get;
  134. ltq_gpio_port[pdev->id].chip.set = ltq_gpio_set;
  135. ltq_gpio_port[pdev->id].chip.request = ltq_gpio_req;
  136. ltq_gpio_port[pdev->id].chip.base = PINS_PER_PORT * pdev->id;
  137. ltq_gpio_port[pdev->id].chip.ngpio = PINS_PER_PORT;
  138. platform_set_drvdata(pdev, &ltq_gpio_port[pdev->id]);
  139. return gpiochip_add(&ltq_gpio_port[pdev->id].chip);
  140. }
  141. static struct platform_driver
  142. ltq_gpio_driver = {
  143. .probe = ltq_gpio_probe,
  144. .driver = {
  145. .name = "ltq_gpio",
  146. .owner = THIS_MODULE,
  147. },
  148. };
  149. int __init ltq_gpio_init(void)
  150. {
  151. int ret = platform_driver_register(&ltq_gpio_driver);
  152. if (ret)
  153. pr_info("ltq_gpio : Error registering platform driver!");
  154. return ret;
  155. }
  156. postcore_initcall(ltq_gpio_init);