simple_gpio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/spinlock.h>
  16. #include <linux/types.h>
  17. #include <linux/ioport.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/gpio.h>
  22. #include <linux/slab.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 gpio_chip *gc;
  77. u8_gc = kzalloc(sizeof(*u8_gc), GFP_KERNEL);
  78. if (!u8_gc)
  79. return -ENOMEM;
  80. spin_lock_init(&u8_gc->lock);
  81. mm_gc = &u8_gc->mm_gc;
  82. gc = &mm_gc->gc;
  83. mm_gc->save_regs = u8_gpio_save_regs;
  84. gc->ngpio = 8;
  85. gc->direction_input = u8_gpio_dir_in;
  86. gc->direction_output = u8_gpio_dir_out;
  87. gc->get = u8_gpio_get;
  88. gc->set = u8_gpio_set;
  89. ret = of_mm_gpiochip_add(np, mm_gc);
  90. if (ret)
  91. goto err;
  92. return 0;
  93. err:
  94. kfree(u8_gc);
  95. return ret;
  96. }
  97. void __init simple_gpiochip_init(const char *compatible)
  98. {
  99. struct device_node *np;
  100. for_each_compatible_node(np, NULL, compatible) {
  101. int ret;
  102. struct resource r;
  103. ret = of_address_to_resource(np, 0, &r);
  104. if (ret)
  105. goto err;
  106. switch (resource_size(&r)) {
  107. case 1:
  108. ret = u8_simple_gpiochip_add(np);
  109. if (ret)
  110. goto err;
  111. break;
  112. default:
  113. /*
  114. * Whenever you need support for GPIO bank width > 1,
  115. * please just turn u8_ code into huge macros, and
  116. * construct needed uX_ code with it.
  117. */
  118. ret = -ENOSYS;
  119. goto err;
  120. }
  121. continue;
  122. err:
  123. pr_err("%s: registration failed, status %d\n",
  124. np->full_name, ret);
  125. }
  126. }