gpio-reg.c 4.7 KB

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