xref: /linux/drivers/gpio/gpio-reg.c (revision ac1dc6b2e7d34dd608d082d1735e5c95f42beb77)
1*ac1dc6b2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2380639c7SRussell King /*
3380639c7SRussell King  * gpio-reg: single register individually fixed-direction GPIOs
4380639c7SRussell King  *
5380639c7SRussell King  * Copyright (C) 2016 Russell King
6380639c7SRussell King  */
7380639c7SRussell King #include <linux/gpio/driver.h>
8380639c7SRussell King #include <linux/gpio/gpio-reg.h>
9380639c7SRussell King #include <linux/io.h>
10380639c7SRussell King #include <linux/slab.h>
11380639c7SRussell King #include <linux/spinlock.h>
12380639c7SRussell King 
13380639c7SRussell King struct gpio_reg {
14380639c7SRussell King 	struct gpio_chip gc;
15380639c7SRussell King 	spinlock_t lock;
16380639c7SRussell King 	u32 direction;
17380639c7SRussell King 	u32 out;
18380639c7SRussell King 	void __iomem *reg;
190e3cb6eeSRussell King 	struct irq_domain *irqdomain;
200e3cb6eeSRussell King 	const int *irqs;
21380639c7SRussell King };
22380639c7SRussell King 
23380639c7SRussell King #define to_gpio_reg(x) container_of(x, struct gpio_reg, gc)
24380639c7SRussell King 
25380639c7SRussell King static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset)
26380639c7SRussell King {
27380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
28380639c7SRussell King 
29380639c7SRussell King 	return r->direction & BIT(offset) ? 1 : 0;
30380639c7SRussell King }
31380639c7SRussell King 
32380639c7SRussell King static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
33380639c7SRussell King 	int value)
34380639c7SRussell King {
35380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
36380639c7SRussell King 
37380639c7SRussell King 	if (r->direction & BIT(offset))
38380639c7SRussell King 		return -ENOTSUPP;
39380639c7SRussell King 
40380639c7SRussell King 	gc->set(gc, offset, value);
41380639c7SRussell King 	return 0;
42380639c7SRussell King }
43380639c7SRussell King 
44380639c7SRussell King static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
45380639c7SRussell King {
46380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
47380639c7SRussell King 
48380639c7SRussell King 	return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
49380639c7SRussell King }
50380639c7SRussell King 
51380639c7SRussell King static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
52380639c7SRussell King {
53380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
54380639c7SRussell King 	unsigned long flags;
55380639c7SRussell King 	u32 val, mask = BIT(offset);
56380639c7SRussell King 
57380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
58380639c7SRussell King 	val = r->out;
59380639c7SRussell King 	if (value)
60380639c7SRussell King 		val |= mask;
61380639c7SRussell King 	else
62380639c7SRussell King 		val &= ~mask;
63380639c7SRussell King 	r->out = val;
64380639c7SRussell King 	writel_relaxed(val, r->reg);
65380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
66380639c7SRussell King }
67380639c7SRussell King 
68380639c7SRussell King static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
69380639c7SRussell King {
70380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
71380639c7SRussell King 	u32 val, mask = BIT(offset);
72380639c7SRussell King 
73380639c7SRussell King 	if (r->direction & mask) {
74380639c7SRussell King 		/*
75380639c7SRussell King 		 * double-read the value, some registers latch after the
76380639c7SRussell King 		 * first read.
77380639c7SRussell King 		 */
78380639c7SRussell King 		readl_relaxed(r->reg);
79380639c7SRussell King 		val = readl_relaxed(r->reg);
80380639c7SRussell King 	} else {
81380639c7SRussell King 		val = r->out;
82380639c7SRussell King 	}
83380639c7SRussell King 	return !!(val & mask);
84380639c7SRussell King }
85380639c7SRussell King 
86380639c7SRussell King static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
87380639c7SRussell King 	unsigned long *bits)
88380639c7SRussell King {
89380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
90380639c7SRussell King 	unsigned long flags;
91380639c7SRussell King 
92380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
93380639c7SRussell King 	r->out = (r->out & ~*mask) | (*bits & *mask);
94380639c7SRussell King 	writel_relaxed(r->out, r->reg);
95380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
96380639c7SRussell King }
97380639c7SRussell King 
980e3cb6eeSRussell King static int gpio_reg_to_irq(struct gpio_chip *gc, unsigned offset)
990e3cb6eeSRussell King {
1000e3cb6eeSRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
1010e3cb6eeSRussell King 	int irq = r->irqs[offset];
1020e3cb6eeSRussell King 
1038bb65fc0SGrygorii Strashko 	if (irq >= 0 && r->irqdomain)
1048bb65fc0SGrygorii Strashko 		irq = irq_find_mapping(r->irqdomain, irq);
1050e3cb6eeSRussell King 
1060e3cb6eeSRussell King 	return irq;
1070e3cb6eeSRussell King }
1080e3cb6eeSRussell King 
109380639c7SRussell King /**
110380639c7SRussell King  * gpio_reg_init - add a fixed in/out register as gpio
111380639c7SRussell King  * @dev: optional struct device associated with this register
112380639c7SRussell King  * @base: start gpio number, or -1 to allocate
113380639c7SRussell King  * @num: number of GPIOs, maximum 32
114380639c7SRussell King  * @label: GPIO chip label
115380639c7SRussell King  * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
116380639c7SRussell King  * @def_out: initial GPIO output value
1170e3cb6eeSRussell King  * @names: array of %num strings describing each GPIO signal or %NULL
1180e3cb6eeSRussell King  * @irqdom: irq domain or %NULL
1190e3cb6eeSRussell King  * @irqs: array of %num ints describing the interrupt mapping for each
1200e3cb6eeSRussell King  *        GPIO signal, or %NULL.  If @irqdom is %NULL, then this
1210e3cb6eeSRussell King  *        describes the Linux interrupt number, otherwise it describes
1220e3cb6eeSRussell King  *        the hardware interrupt number in the specified irq domain.
123380639c7SRussell King  *
124380639c7SRussell King  * Add a single-register GPIO device containing up to 32 GPIO signals,
125380639c7SRussell King  * where each GPIO has a fixed input or output configuration.  Only
126380639c7SRussell King  * input GPIOs are assumed to be readable from the register, and only
127380639c7SRussell King  * then after a double-read.  Output values are assumed not to be
128380639c7SRussell King  * readable.
129380639c7SRussell King  */
130380639c7SRussell King struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
131380639c7SRussell King 	int base, int num, const char *label, u32 direction, u32 def_out,
1320e3cb6eeSRussell King 	const char *const *names, struct irq_domain *irqdom, const int *irqs)
133380639c7SRussell King {
134380639c7SRussell King 	struct gpio_reg *r;
135380639c7SRussell King 	int ret;
136380639c7SRussell King 
137380639c7SRussell King 	if (dev)
138380639c7SRussell King 		r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
139380639c7SRussell King 	else
140380639c7SRussell King 		r = kzalloc(sizeof(*r), GFP_KERNEL);
141380639c7SRussell King 
142380639c7SRussell King 	if (!r)
143380639c7SRussell King 		return ERR_PTR(-ENOMEM);
144380639c7SRussell King 
145380639c7SRussell King 	spin_lock_init(&r->lock);
146380639c7SRussell King 
147380639c7SRussell King 	r->gc.label = label;
148380639c7SRussell King 	r->gc.get_direction = gpio_reg_get_direction;
149380639c7SRussell King 	r->gc.direction_input = gpio_reg_direction_input;
150380639c7SRussell King 	r->gc.direction_output = gpio_reg_direction_output;
151380639c7SRussell King 	r->gc.set = gpio_reg_set;
152380639c7SRussell King 	r->gc.get = gpio_reg_get;
153380639c7SRussell King 	r->gc.set_multiple = gpio_reg_set_multiple;
1540e3cb6eeSRussell King 	if (irqs)
1550e3cb6eeSRussell King 		r->gc.to_irq = gpio_reg_to_irq;
156380639c7SRussell King 	r->gc.base = base;
157380639c7SRussell King 	r->gc.ngpio = num;
158380639c7SRussell King 	r->gc.names = names;
159380639c7SRussell King 	r->direction = direction;
160380639c7SRussell King 	r->out = def_out;
161380639c7SRussell King 	r->reg = reg;
1620e3cb6eeSRussell King 	r->irqs = irqs;
163380639c7SRussell King 
164380639c7SRussell King 	if (dev)
165380639c7SRussell King 		ret = devm_gpiochip_add_data(dev, &r->gc, r);
166380639c7SRussell King 	else
167380639c7SRussell King 		ret = gpiochip_add_data(&r->gc, r);
168380639c7SRussell King 
169380639c7SRussell King 	return ret ? ERR_PTR(ret) : &r->gc;
170380639c7SRussell King }
171380639c7SRussell King 
172380639c7SRussell King int gpio_reg_resume(struct gpio_chip *gc)
173380639c7SRussell King {
174380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
175380639c7SRussell King 	unsigned long flags;
176380639c7SRussell King 
177380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
178380639c7SRussell King 	writel_relaxed(r->out, r->reg);
179380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
180380639c7SRussell King 
181380639c7SRussell King 	return 0;
182380639c7SRussell King }
183