simple_gpio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <linux/slab.h>
  24. #include <asm/prom.h>
  25. #include "simple_gpio.h"
  26. struct u8_gpio_chip {
  27. struct of_mm_gpio_chip mm_gc;
  28. spinlock_t lock;
  29. /* shadowed data register to clear/set bits safely */
  30. u8 data;
  31. };
  32. static struct u8_gpio_chip *to_u8_gpio_chip(struct of_mm_gpio_chip *mm_gc)
  33. {
  34. return container_of(mm_gc, struct u8_gpio_chip, mm_gc);
  35. }
  36. static u8 u8_pin2mask(unsigned int pin)
  37. {
  38. return 1 << (8 - 1 - pin);
  39. }
  40. static int u8_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  41. {
  42. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  43. return in_8(mm_gc->regs) & u8_pin2mask(gpio);
  44. }
  45. static void u8_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  46. {
  47. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  48. struct u8_gpio_chip *u8_gc = to_u8_gpio_chip(mm_gc);
  49. unsigned long flags;
  50. spin_lock_irqsave(&u8_gc->lock, flags);
  51. if (val)
  52. u8_gc->data |= u8_pin2mask(gpio);
  53. else
  54. u8_gc->data &= ~u8_pin2mask(gpio);
  55. out_8(mm_gc->regs, u8_gc->data);
  56. spin_unlock_irqrestore(&u8_gc->lock, flags);
  57. }
  58. static int u8_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  59. {
  60. return 0;
  61. }
  62. static int u8_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  63. {
  64. u8_gpio_set(gc, gpio, val);
  65. return 0;
  66. }
  67. static void u8_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  68. {
  69. struct u8_gpio_chip *u8_gc = to_u8_gpio_chip(mm_gc);
  70. u8_gc->data = in_8(mm_gc->regs);
  71. }
  72. static int __init u8_simple_gpiochip_add(struct device_node *np)
  73. {
  74. int ret;
  75. struct u8_gpio_chip *u8_gc;
  76. struct of_mm_gpio_chip *mm_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. gc = &mm_gc->gc;
  84. mm_gc->save_regs = u8_gpio_save_regs;
  85. gc->ngpio = 8;
  86. gc->direction_input = u8_gpio_dir_in;
  87. gc->direction_output = u8_gpio_dir_out;
  88. gc->get = u8_gpio_get;
  89. gc->set = u8_gpio_set;
  90. ret = of_mm_gpiochip_add(np, mm_gc);
  91. if (ret)
  92. goto err;
  93. return 0;
  94. err:
  95. kfree(u8_gc);
  96. return ret;
  97. }
  98. void __init simple_gpiochip_init(const char *compatible)
  99. {
  100. struct device_node *np;
  101. for_each_compatible_node(np, NULL, compatible) {
  102. int ret;
  103. struct resource r;
  104. ret = of_address_to_resource(np, 0, &r);
  105. if (ret)
  106. goto err;
  107. switch (resource_size(&r)) {
  108. case 1:
  109. ret = u8_simple_gpiochip_add(np);
  110. if (ret)
  111. goto err;
  112. break;
  113. default:
  114. /*
  115. * Whenever you need support for GPIO bank width > 1,
  116. * please just turn u8_ code into huge macros, and
  117. * construct needed uX_ code with it.
  118. */
  119. ret = -ENOSYS;
  120. goto err;
  121. }
  122. continue;
  123. err:
  124. pr_err("%s: registration failed, status %d\n",
  125. np->full_name, ret);
  126. }
  127. }