xref: /linux/drivers/gpio/gpio-reg.c (revision 0e3cb6ee386f384a9131f0c7db52a0a961d2ded9)
1380639c7SRussell King /*
2380639c7SRussell King  * gpio-reg: single register individually fixed-direction GPIOs
3380639c7SRussell King  *
4380639c7SRussell King  * Copyright (C) 2016 Russell King
5380639c7SRussell King  *
6380639c7SRussell King  * This software is licensed under the terms of the GNU General Public
7380639c7SRussell King  * License version 2, as published by the Free Software Foundation, and
8380639c7SRussell King  * may be copied, distributed, and modified under those terms.
9380639c7SRussell King  */
10380639c7SRussell King #include <linux/gpio/driver.h>
11380639c7SRussell King #include <linux/gpio/gpio-reg.h>
12380639c7SRussell King #include <linux/io.h>
13380639c7SRussell King #include <linux/slab.h>
14380639c7SRussell King #include <linux/spinlock.h>
15380639c7SRussell King 
16380639c7SRussell King struct gpio_reg {
17380639c7SRussell King 	struct gpio_chip gc;
18380639c7SRussell King 	spinlock_t lock;
19380639c7SRussell King 	u32 direction;
20380639c7SRussell King 	u32 out;
21380639c7SRussell King 	void __iomem *reg;
22*0e3cb6eeSRussell King 	struct irq_domain *irqdomain;
23*0e3cb6eeSRussell King 	const int *irqs;
24380639c7SRussell King };
25380639c7SRussell King 
26380639c7SRussell King #define to_gpio_reg(x) container_of(x, struct gpio_reg, gc)
27380639c7SRussell King 
28380639c7SRussell King static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset)
29380639c7SRussell King {
30380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
31380639c7SRussell King 
32380639c7SRussell King 	return r->direction & BIT(offset) ? 1 : 0;
33380639c7SRussell King }
34380639c7SRussell King 
35380639c7SRussell King static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
36380639c7SRussell King 	int value)
37380639c7SRussell King {
38380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
39380639c7SRussell King 
40380639c7SRussell King 	if (r->direction & BIT(offset))
41380639c7SRussell King 		return -ENOTSUPP;
42380639c7SRussell King 
43380639c7SRussell King 	gc->set(gc, offset, value);
44380639c7SRussell King 	return 0;
45380639c7SRussell King }
46380639c7SRussell King 
47380639c7SRussell King static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
48380639c7SRussell King {
49380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
50380639c7SRussell King 
51380639c7SRussell King 	return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
52380639c7SRussell King }
53380639c7SRussell King 
54380639c7SRussell King static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
55380639c7SRussell King {
56380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
57380639c7SRussell King 	unsigned long flags;
58380639c7SRussell King 	u32 val, mask = BIT(offset);
59380639c7SRussell King 
60380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
61380639c7SRussell King 	val = r->out;
62380639c7SRussell King 	if (value)
63380639c7SRussell King 		val |= mask;
64380639c7SRussell King 	else
65380639c7SRussell King 		val &= ~mask;
66380639c7SRussell King 	r->out = val;
67380639c7SRussell King 	writel_relaxed(val, r->reg);
68380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
69380639c7SRussell King }
70380639c7SRussell King 
71380639c7SRussell King static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
72380639c7SRussell King {
73380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
74380639c7SRussell King 	u32 val, mask = BIT(offset);
75380639c7SRussell King 
76380639c7SRussell King 	if (r->direction & mask) {
77380639c7SRussell King 		/*
78380639c7SRussell King 		 * double-read the value, some registers latch after the
79380639c7SRussell King 		 * first read.
80380639c7SRussell King 		 */
81380639c7SRussell King 		readl_relaxed(r->reg);
82380639c7SRussell King 		val = readl_relaxed(r->reg);
83380639c7SRussell King 	} else {
84380639c7SRussell King 		val = r->out;
85380639c7SRussell King 	}
86380639c7SRussell King 	return !!(val & mask);
87380639c7SRussell King }
88380639c7SRussell King 
89380639c7SRussell King static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
90380639c7SRussell King 	unsigned long *bits)
91380639c7SRussell King {
92380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
93380639c7SRussell King 	unsigned long flags;
94380639c7SRussell King 
95380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
96380639c7SRussell King 	r->out = (r->out & ~*mask) | (*bits & *mask);
97380639c7SRussell King 	writel_relaxed(r->out, r->reg);
98380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
99380639c7SRussell King }
100380639c7SRussell King 
101*0e3cb6eeSRussell King static int gpio_reg_to_irq(struct gpio_chip *gc, unsigned offset)
102*0e3cb6eeSRussell King {
103*0e3cb6eeSRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
104*0e3cb6eeSRussell King 	int irq = r->irqs[offset];
105*0e3cb6eeSRussell King 
106*0e3cb6eeSRussell King 	if (irq >= 0 && r->irqdomain)
107*0e3cb6eeSRussell King 		irq = irq_find_mapping(r->irqdomain, irq);
108*0e3cb6eeSRussell King 
109*0e3cb6eeSRussell King 	return irq;
110*0e3cb6eeSRussell King }
111*0e3cb6eeSRussell King 
112380639c7SRussell King /**
113380639c7SRussell King  * gpio_reg_init - add a fixed in/out register as gpio
114380639c7SRussell King  * @dev: optional struct device associated with this register
115380639c7SRussell King  * @base: start gpio number, or -1 to allocate
116380639c7SRussell King  * @num: number of GPIOs, maximum 32
117380639c7SRussell King  * @label: GPIO chip label
118380639c7SRussell King  * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
119380639c7SRussell King  * @def_out: initial GPIO output value
120*0e3cb6eeSRussell King  * @names: array of %num strings describing each GPIO signal or %NULL
121*0e3cb6eeSRussell King  * @irqdom: irq domain or %NULL
122*0e3cb6eeSRussell King  * @irqs: array of %num ints describing the interrupt mapping for each
123*0e3cb6eeSRussell King  *        GPIO signal, or %NULL.  If @irqdom is %NULL, then this
124*0e3cb6eeSRussell King  *        describes the Linux interrupt number, otherwise it describes
125*0e3cb6eeSRussell King  *        the hardware interrupt number in the specified irq domain.
126380639c7SRussell King  *
127380639c7SRussell King  * Add a single-register GPIO device containing up to 32 GPIO signals,
128380639c7SRussell King  * where each GPIO has a fixed input or output configuration.  Only
129380639c7SRussell King  * input GPIOs are assumed to be readable from the register, and only
130380639c7SRussell King  * then after a double-read.  Output values are assumed not to be
131380639c7SRussell King  * readable.
132380639c7SRussell King  */
133380639c7SRussell King struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
134380639c7SRussell King 	int base, int num, const char *label, u32 direction, u32 def_out,
135*0e3cb6eeSRussell King 	const char *const *names, struct irq_domain *irqdom, const int *irqs)
136380639c7SRussell King {
137380639c7SRussell King 	struct gpio_reg *r;
138380639c7SRussell King 	int ret;
139380639c7SRussell King 
140380639c7SRussell King 	if (dev)
141380639c7SRussell King 		r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
142380639c7SRussell King 	else
143380639c7SRussell King 		r = kzalloc(sizeof(*r), GFP_KERNEL);
144380639c7SRussell King 
145380639c7SRussell King 	if (!r)
146380639c7SRussell King 		return ERR_PTR(-ENOMEM);
147380639c7SRussell King 
148380639c7SRussell King 	spin_lock_init(&r->lock);
149380639c7SRussell King 
150380639c7SRussell King 	r->gc.label = label;
151380639c7SRussell King 	r->gc.get_direction = gpio_reg_get_direction;
152380639c7SRussell King 	r->gc.direction_input = gpio_reg_direction_input;
153380639c7SRussell King 	r->gc.direction_output = gpio_reg_direction_output;
154380639c7SRussell King 	r->gc.set = gpio_reg_set;
155380639c7SRussell King 	r->gc.get = gpio_reg_get;
156380639c7SRussell King 	r->gc.set_multiple = gpio_reg_set_multiple;
157*0e3cb6eeSRussell King 	if (irqs)
158*0e3cb6eeSRussell King 		r->gc.to_irq = gpio_reg_to_irq;
159380639c7SRussell King 	r->gc.base = base;
160380639c7SRussell King 	r->gc.ngpio = num;
161380639c7SRussell King 	r->gc.names = names;
162380639c7SRussell King 	r->direction = direction;
163380639c7SRussell King 	r->out = def_out;
164380639c7SRussell King 	r->reg = reg;
165*0e3cb6eeSRussell King 	r->irqs = irqs;
166380639c7SRussell King 
167380639c7SRussell King 	if (dev)
168380639c7SRussell King 		ret = devm_gpiochip_add_data(dev, &r->gc, r);
169380639c7SRussell King 	else
170380639c7SRussell King 		ret = gpiochip_add_data(&r->gc, r);
171380639c7SRussell King 
172380639c7SRussell King 	return ret ? ERR_PTR(ret) : &r->gc;
173380639c7SRussell King }
174380639c7SRussell King 
175380639c7SRussell King int gpio_reg_resume(struct gpio_chip *gc)
176380639c7SRussell King {
177380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
178380639c7SRussell King 	unsigned long flags;
179380639c7SRussell King 
180380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
181380639c7SRussell King 	writel_relaxed(r->out, r->reg);
182380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
183380639c7SRussell King 
184380639c7SRussell King 	return 0;
185380639c7SRussell King }
186