/arch/powerpc/sysdev/simple_gpio.c

http://github.com/mirrors/linux · C · 147 lines · 104 code · 25 blank · 18 comment · 6 complexity · 8ee8e003f35093f749df59716768a0d0 MD5 · raw file

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