gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 gpio_to_irq(unsigned int gpio)
  32. {
  33. return -EINVAL;
  34. }
  35. EXPORT_SYMBOL(gpio_to_irq);
  36. int irq_to_gpio(unsigned int gpio)
  37. {
  38. return -EINVAL;
  39. }
  40. EXPORT_SYMBOL(irq_to_gpio);
  41. int ltq_gpio_request(unsigned int pin, unsigned int alt0,
  42. unsigned int alt1, unsigned int dir, const char *name)
  43. {
  44. int id = 0;
  45. if (pin >= (MAX_PORTS * PINS_PER_PORT))
  46. return -EINVAL;
  47. if (gpio_request(pin, name)) {
  48. pr_err("failed to setup lantiq gpio: %s\n", name);
  49. return -EBUSY;
  50. }
  51. if (dir)
  52. gpio_direction_output(pin, 1);
  53. else
  54. gpio_direction_input(pin);
  55. while (pin >= PINS_PER_PORT) {
  56. pin -= PINS_PER_PORT;
  57. id++;
  58. }
  59. if (alt0)
  60. ltq_gpio_setbit(ltq_gpio_port[id].membase,
  61. LTQ_GPIO_ALTSEL0, pin);
  62. else
  63. ltq_gpio_clearbit(ltq_gpio_port[id].membase,
  64. LTQ_GPIO_ALTSEL0, pin);
  65. if (alt1)
  66. ltq_gpio_setbit(ltq_gpio_port[id].membase,
  67. LTQ_GPIO_ALTSEL1, pin);
  68. else
  69. ltq_gpio_clearbit(ltq_gpio_port[id].membase,
  70. LTQ_GPIO_ALTSEL1, pin);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(ltq_gpio_request);
  74. static void ltq_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
  75. {
  76. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  77. if (value)
  78. ltq_gpio_setbit(ltq_gpio->membase, LTQ_GPIO_OUT, offset);
  79. else
  80. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_OUT, offset);
  81. }
  82. static int ltq_gpio_get(struct gpio_chip *chip, unsigned int offset)
  83. {
  84. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  85. return ltq_gpio_getbit(ltq_gpio->membase, LTQ_GPIO_IN, offset);
  86. }
  87. static int ltq_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
  88. {
  89. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  90. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_OD, offset);
  91. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_DIR, offset);
  92. return 0;
  93. }
  94. static int ltq_gpio_direction_output(struct gpio_chip *chip,
  95. unsigned int offset, int value)
  96. {
  97. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  98. ltq_gpio_setbit(ltq_gpio->membase, LTQ_GPIO_OD, offset);
  99. ltq_gpio_setbit(ltq_gpio->membase, LTQ_GPIO_DIR, offset);
  100. ltq_gpio_set(chip, offset, value);
  101. return 0;
  102. }
  103. static int ltq_gpio_req(struct gpio_chip *chip, unsigned offset)
  104. {
  105. struct ltq_gpio *ltq_gpio = container_of(chip, struct ltq_gpio, chip);
  106. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_ALTSEL0, offset);
  107. ltq_gpio_clearbit(ltq_gpio->membase, LTQ_GPIO_ALTSEL1, offset);
  108. return 0;
  109. }
  110. static int ltq_gpio_probe(struct platform_device *pdev)
  111. {
  112. struct resource *res;
  113. if (pdev->id >= MAX_PORTS) {
  114. dev_err(&pdev->dev, "invalid gpio port %d\n",
  115. pdev->id);
  116. return -EINVAL;
  117. }
  118. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  119. if (!res) {
  120. dev_err(&pdev->dev, "failed to get memory for gpio port %d\n",
  121. pdev->id);
  122. return -ENOENT;
  123. }
  124. res = devm_request_mem_region(&pdev->dev, res->start,
  125. resource_size(res), dev_name(&pdev->dev));
  126. if (!res) {
  127. dev_err(&pdev->dev,
  128. "failed to request memory for gpio port %d\n",
  129. pdev->id);
  130. return -EBUSY;
  131. }
  132. ltq_gpio_port[pdev->id].membase = devm_ioremap_nocache(&pdev->dev,
  133. res->start, resource_size(res));
  134. if (!ltq_gpio_port[pdev->id].membase) {
  135. dev_err(&pdev->dev, "failed to remap memory for gpio port %d\n",
  136. pdev->id);
  137. return -ENOMEM;
  138. }
  139. ltq_gpio_port[pdev->id].chip.label = "ltq_gpio";
  140. ltq_gpio_port[pdev->id].chip.direction_input = ltq_gpio_direction_input;
  141. ltq_gpio_port[pdev->id].chip.direction_output =
  142. ltq_gpio_direction_output;
  143. ltq_gpio_port[pdev->id].chip.get = ltq_gpio_get;
  144. ltq_gpio_port[pdev->id].chip.set = ltq_gpio_set;
  145. ltq_gpio_port[pdev->id].chip.request = ltq_gpio_req;
  146. ltq_gpio_port[pdev->id].chip.base = PINS_PER_PORT * pdev->id;
  147. ltq_gpio_port[pdev->id].chip.ngpio = PINS_PER_PORT;
  148. platform_set_drvdata(pdev, &ltq_gpio_port[pdev->id]);
  149. return gpiochip_add(&ltq_gpio_port[pdev->id].chip);
  150. }
  151. static struct platform_driver
  152. ltq_gpio_driver = {
  153. .probe = ltq_gpio_probe,
  154. .driver = {
  155. .name = "ltq_gpio",
  156. .owner = THIS_MODULE,
  157. },
  158. };
  159. int __init ltq_gpio_init(void)
  160. {
  161. int ret = platform_driver_register(&ltq_gpio_driver);
  162. if (ret)
  163. pr_info("ltq_gpio : Error registering platfom driver!");
  164. return ret;
  165. }
  166. postcore_initcall(ltq_gpio_init);