simple_gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Simple Memory-Mapped GPIOs
  3. *
  4. * Copyright (c) MontaVista Software, Inc. 2008.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/types.h>
  18. #include <linux/ioport.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/gpio.h>
  23. #include <asm/prom.h>
  24. #include "simple_gpio.h"
  25. struct u8_gpio_chip {
  26. struct of_mm_gpio_chip mm_gc;
  27. spinlock_t lock;
  28. /* shadowed data register to clear/set bits safely */
  29. u8 data;
  30. };
  31. static struct u8_gpio_chip *to_u8_gpio_chip(struct of_mm_gpio_chip *mm_gc)
  32. {
  33. return container_of(mm_gc, struct u8_gpio_chip, mm_gc);
  34. }
  35. static u8 u8_pin2mask(unsigned int pin)
  36. {
  37. return 1 << (8 - 1 - pin);
  38. }
  39. static int u8_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  40. {
  41. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  42. return in_8(mm_gc->regs) & u8_pin2mask(gpio);
  43. }
  44. static void u8_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  45. {
  46. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  47. struct u8_gpio_chip *u8_gc = to_u8_gpio_chip(mm_gc);
  48. unsigned long flags;
  49. spin_lock_irqsave(&u8_gc->lock, flags);
  50. if (val)
  51. u8_gc->data |= u8_pin2mask(gpio);
  52. else
  53. u8_gc->data &= ~u8_pin2mask(gpio);
  54. out_8(mm_gc->regs, u8_gc->data);
  55. spin_unlock_irqrestore(&u8_gc->lock, flags);
  56. }
  57. static int u8_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  58. {
  59. return 0;
  60. }
  61. static int u8_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  62. {
  63. u8_gpio_set(gc, gpio, val);
  64. return 0;
  65. }
  66. static void u8_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  67. {
  68. struct u8_gpio_chip *u8_gc = to_u8_gpio_chip(mm_gc);
  69. u8_gc->data = in_8(mm_gc->regs);
  70. }
  71. static int __init u8_simple_gpiochip_add(struct device_node *np)
  72. {
  73. int ret;
  74. struct u8_gpio_chip *u8_gc;
  75. struct of_mm_gpio_chip *mm_gc;
  76. struct of_gpio_chip *of_gc;
  77. struct gpio_chip *gc;
  78. u8_gc = kzalloc(sizeof(*u8_gc), GFP_KERNEL);
  79. if (!u8_gc)
  80. return -ENOMEM;
  81. spin_lock_init(&u8_gc->lock);
  82. mm_gc = &u8_gc->mm_gc;
  83. of_gc = &mm_gc->of_gc;
  84. gc = &of_gc->gc;
  85. mm_gc->save_regs = u8_gpio_save_regs;
  86. of_gc->gpio_cells = 2;
  87. gc->ngpio = 8;
  88. gc->direction_input = u8_gpio_dir_in;
  89. gc->direction_output = u8_gpio_dir_out;
  90. gc->get = u8_gpio_get;
  91. gc->set = u8_gpio_set;
  92. ret = of_mm_gpiochip_add(np, mm_gc);
  93. if (ret)
  94. goto err;
  95. return 0;
  96. err:
  97. kfree(u8_gc);
  98. return ret;
  99. }
  100. void __init simple_gpiochip_init(const char *compatible)
  101. {
  102. struct device_node *np;
  103. for_each_compatible_node(np, NULL, compatible) {
  104. int ret;
  105. struct resource r;
  106. ret = of_address_to_resource(np, 0, &r);
  107. if (ret)
  108. goto err;
  109. switch (resource_size(&r)) {
  110. case 1:
  111. ret = u8_simple_gpiochip_add(np);
  112. if (ret)
  113. goto err;
  114. break;
  115. default:
  116. /*
  117. * Whenever you need support for GPIO bank width > 1,
  118. * please just turn u8_ code into huge macros, and
  119. * construct needed uX_ code with it.
  120. */
  121. ret = -ENOSYS;
  122. goto err;
  123. }
  124. continue;
  125. err:
  126. pr_err("%s: registration failed, status %d\n",
  127. np->full_name, ret);
  128. }
  129. }