gpio-reg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * gpio-reg: single register individually fixed-direction GPIOs
  3. *
  4. * Copyright (C) 2016 Russell King
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. */
  10. #include <linux/gpio/driver.h>
  11. #include <linux/gpio/gpio-reg.h>
  12. #include <linux/io.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. struct gpio_reg {
  16. struct gpio_chip gc;
  17. spinlock_t lock;
  18. u32 direction;
  19. u32 out;
  20. void __iomem *reg;
  21. struct irq_domain *irqdomain;
  22. const int *irqs;
  23. };
  24. #define to_gpio_reg(x) container_of(x, struct gpio_reg, gc)
  25. static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset)
  26. {
  27. struct gpio_reg *r = to_gpio_reg(gc);
  28. return r->direction & BIT(offset) ? 1 : 0;
  29. }
  30. static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
  31. int value)
  32. {
  33. struct gpio_reg *r = to_gpio_reg(gc);
  34. if (r->direction & BIT(offset))
  35. return -ENOTSUPP;
  36. gc->set(gc, offset, value);
  37. return 0;
  38. }
  39. static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
  40. {
  41. struct gpio_reg *r = to_gpio_reg(gc);
  42. return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
  43. }
  44. static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
  45. {
  46. struct gpio_reg *r = to_gpio_reg(gc);
  47. unsigned long flags;
  48. u32 val, mask = BIT(offset);
  49. spin_lock_irqsave(&r->lock, flags);
  50. val = r->out;
  51. if (value)
  52. val |= mask;
  53. else
  54. val &= ~mask;
  55. r->out = val;
  56. writel_relaxed(val, r->reg);
  57. spin_unlock_irqrestore(&r->lock, flags);
  58. }
  59. static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
  60. {
  61. struct gpio_reg *r = to_gpio_reg(gc);
  62. u32 val, mask = BIT(offset);
  63. if (r->direction & mask) {
  64. /*
  65. * double-read the value, some registers latch after the
  66. * first read.
  67. */
  68. readl_relaxed(r->reg);
  69. val = readl_relaxed(r->reg);
  70. } else {
  71. val = r->out;
  72. }
  73. return !!(val & mask);
  74. }
  75. static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  76. unsigned long *bits)
  77. {
  78. struct gpio_reg *r = to_gpio_reg(gc);
  79. unsigned long flags;
  80. spin_lock_irqsave(&r->lock, flags);
  81. r->out = (r->out & ~*mask) | (*bits & *mask);
  82. writel_relaxed(r->out, r->reg);
  83. spin_unlock_irqrestore(&r->lock, flags);
  84. }
  85. static int gpio_reg_to_irq(struct gpio_chip *gc, unsigned offset)
  86. {
  87. struct gpio_reg *r = to_gpio_reg(gc);
  88. int irq = r->irqs[offset];
  89. if (irq >= 0 && r->irqdomain)
  90. irq = irq_find_mapping(r->irqdomain, irq);
  91. return irq;
  92. }
  93. /**
  94. * gpio_reg_init - add a fixed in/out register as gpio
  95. * @dev: optional struct device associated with this register
  96. * @base: start gpio number, or -1 to allocate
  97. * @num: number of GPIOs, maximum 32
  98. * @label: GPIO chip label
  99. * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
  100. * @def_out: initial GPIO output value
  101. * @names: array of %num strings describing each GPIO signal or %NULL
  102. * @irqdom: irq domain or %NULL
  103. * @irqs: array of %num ints describing the interrupt mapping for each
  104. * GPIO signal, or %NULL. If @irqdom is %NULL, then this
  105. * describes the Linux interrupt number, otherwise it describes
  106. * the hardware interrupt number in the specified irq domain.
  107. *
  108. * Add a single-register GPIO device containing up to 32 GPIO signals,
  109. * where each GPIO has a fixed input or output configuration. Only
  110. * input GPIOs are assumed to be readable from the register, and only
  111. * then after a double-read. Output values are assumed not to be
  112. * readable.
  113. */
  114. struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
  115. int base, int num, const char *label, u32 direction, u32 def_out,
  116. const char *const *names, struct irq_domain *irqdom, const int *irqs)
  117. {
  118. struct gpio_reg *r;
  119. int ret;
  120. if (dev)
  121. r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
  122. else
  123. r = kzalloc(sizeof(*r), GFP_KERNEL);
  124. if (!r)
  125. return ERR_PTR(-ENOMEM);
  126. spin_lock_init(&r->lock);
  127. r->gc.label = label;
  128. r->gc.get_direction = gpio_reg_get_direction;
  129. r->gc.direction_input = gpio_reg_direction_input;
  130. r->gc.direction_output = gpio_reg_direction_output;
  131. r->gc.set = gpio_reg_set;
  132. r->gc.get = gpio_reg_get;
  133. r->gc.set_multiple = gpio_reg_set_multiple;
  134. if (irqs)
  135. r->gc.to_irq = gpio_reg_to_irq;
  136. r->gc.base = base;
  137. r->gc.ngpio = num;
  138. r->gc.names = names;
  139. r->direction = direction;
  140. r->out = def_out;
  141. r->reg = reg;
  142. r->irqs = irqs;
  143. if (dev)
  144. ret = devm_gpiochip_add_data(dev, &r->gc, r);
  145. else
  146. ret = gpiochip_add_data(&r->gc, r);
  147. return ret ? ERR_PTR(ret) : &r->gc;
  148. }
  149. int gpio_reg_resume(struct gpio_chip *gc)
  150. {
  151. struct gpio_reg *r = to_gpio_reg(gc);
  152. unsigned long flags;
  153. spin_lock_irqsave(&r->lock, flags);
  154. writel_relaxed(r->out, r->reg);
  155. spin_unlock_irqrestore(&r->lock, flags);
  156. return 0;
  157. }