|
| 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 | + |
| 16 | +struct gpio_reg { |
| 17 | + struct gpio_chip gc; |
| 18 | + spinlock_t lock; |
| 19 | + u32 direction; |
| 20 | + u32 out; |
| 21 | + void __iomem *reg; |
| 22 | +}; |
| 23 | + |
| 24 | +#define to_gpio_reg(x) container_of(x, struct gpio_reg, gc) |
| 25 | + |
| 26 | +static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset) |
| 27 | +{ |
| 28 | + struct gpio_reg *r = to_gpio_reg(gc); |
| 29 | + |
| 30 | + return r->direction & BIT(offset) ? 1 : 0; |
| 31 | +} |
| 32 | + |
| 33 | +static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset, |
| 34 | + int value) |
| 35 | +{ |
| 36 | + struct gpio_reg *r = to_gpio_reg(gc); |
| 37 | + |
| 38 | + if (r->direction & BIT(offset)) |
| 39 | + return -ENOTSUPP; |
| 40 | + |
| 41 | + gc->set(gc, offset, value); |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset) |
| 46 | +{ |
| 47 | + struct gpio_reg *r = to_gpio_reg(gc); |
| 48 | + |
| 49 | + return r->direction & BIT(offset) ? 0 : -ENOTSUPP; |
| 50 | +} |
| 51 | + |
| 52 | +static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value) |
| 53 | +{ |
| 54 | + struct gpio_reg *r = to_gpio_reg(gc); |
| 55 | + unsigned long flags; |
| 56 | + u32 val, mask = BIT(offset); |
| 57 | + |
| 58 | + spin_lock_irqsave(&r->lock, flags); |
| 59 | + val = r->out; |
| 60 | + if (value) |
| 61 | + val |= mask; |
| 62 | + else |
| 63 | + val &= ~mask; |
| 64 | + r->out = val; |
| 65 | + writel_relaxed(val, r->reg); |
| 66 | + spin_unlock_irqrestore(&r->lock, flags); |
| 67 | +} |
| 68 | + |
| 69 | +static int gpio_reg_get(struct gpio_chip *gc, unsigned offset) |
| 70 | +{ |
| 71 | + struct gpio_reg *r = to_gpio_reg(gc); |
| 72 | + u32 val, mask = BIT(offset); |
| 73 | + |
| 74 | + if (r->direction & mask) { |
| 75 | + /* |
| 76 | + * double-read the value, some registers latch after the |
| 77 | + * first read. |
| 78 | + */ |
| 79 | + readl_relaxed(r->reg); |
| 80 | + val = readl_relaxed(r->reg); |
| 81 | + } else { |
| 82 | + val = r->out; |
| 83 | + } |
| 84 | + return !!(val & mask); |
| 85 | +} |
| 86 | + |
| 87 | +static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask, |
| 88 | + unsigned long *bits) |
| 89 | +{ |
| 90 | + struct gpio_reg *r = to_gpio_reg(gc); |
| 91 | + unsigned long flags; |
| 92 | + |
| 93 | + spin_lock_irqsave(&r->lock, flags); |
| 94 | + r->out = (r->out & ~*mask) | (*bits & *mask); |
| 95 | + writel_relaxed(r->out, r->reg); |
| 96 | + spin_unlock_irqrestore(&r->lock, flags); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * gpio_reg_init - add a fixed in/out register as gpio |
| 101 | + * @dev: optional struct device associated with this register |
| 102 | + * @base: start gpio number, or -1 to allocate |
| 103 | + * @num: number of GPIOs, maximum 32 |
| 104 | + * @label: GPIO chip label |
| 105 | + * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in |
| 106 | + * @def_out: initial GPIO output value |
| 107 | + * @names: array of %num strings describing each GPIO signal |
| 108 | + * |
| 109 | + * Add a single-register GPIO device containing up to 32 GPIO signals, |
| 110 | + * where each GPIO has a fixed input or output configuration. Only |
| 111 | + * input GPIOs are assumed to be readable from the register, and only |
| 112 | + * then after a double-read. Output values are assumed not to be |
| 113 | + * readable. |
| 114 | + */ |
| 115 | +struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg, |
| 116 | + int base, int num, const char *label, u32 direction, u32 def_out, |
| 117 | + const char *const *names) |
| 118 | +{ |
| 119 | + struct gpio_reg *r; |
| 120 | + int ret; |
| 121 | + |
| 122 | + if (dev) |
| 123 | + r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL); |
| 124 | + else |
| 125 | + r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 126 | + |
| 127 | + if (!r) |
| 128 | + return ERR_PTR(-ENOMEM); |
| 129 | + |
| 130 | + spin_lock_init(&r->lock); |
| 131 | + |
| 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 | + r->gc.base = base; |
| 140 | + r->gc.ngpio = num; |
| 141 | + r->gc.names = names; |
| 142 | + r->direction = direction; |
| 143 | + r->out = def_out; |
| 144 | + r->reg = reg; |
| 145 | + |
| 146 | + if (dev) |
| 147 | + ret = devm_gpiochip_add_data(dev, &r->gc, r); |
| 148 | + else |
| 149 | + ret = gpiochip_add_data(&r->gc, r); |
| 150 | + |
| 151 | + return ret ? ERR_PTR(ret) : &r->gc; |
| 152 | +} |
| 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 | + |
| 159 | + spin_lock_irqsave(&r->lock, flags); |
| 160 | + writel_relaxed(r->out, r->reg); |
| 161 | + spin_unlock_irqrestore(&r->lock, flags); |
| 162 | + |
| 163 | + return 0; |
| 164 | +} |
0 commit comments