xref: /linux/drivers/gpio/gpio-reg.c (revision e42615ec233b30dfaf117b108d4cb49455b4df1d)
1ac1dc6b2SThomas 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 
29*e42615ecSMatti Vaittinen 	return r->direction & BIT(offset) ? GPIO_LINE_DIRECTION_IN :
30*e42615ecSMatti Vaittinen 					    GPIO_LINE_DIRECTION_OUT;
31380639c7SRussell King }
32380639c7SRussell King 
33380639c7SRussell King static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
34380639c7SRussell King 	int value)
35380639c7SRussell King {
36380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
37380639c7SRussell King 
38380639c7SRussell King 	if (r->direction & BIT(offset))
39380639c7SRussell King 		return -ENOTSUPP;
40380639c7SRussell King 
41380639c7SRussell King 	gc->set(gc, offset, value);
42380639c7SRussell King 	return 0;
43380639c7SRussell King }
44380639c7SRussell King 
45380639c7SRussell King static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
46380639c7SRussell King {
47380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
48380639c7SRussell King 
49380639c7SRussell King 	return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
50380639c7SRussell King }
51380639c7SRussell King 
52380639c7SRussell King static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
53380639c7SRussell King {
54380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
55380639c7SRussell King 	unsigned long flags;
56380639c7SRussell King 	u32 val, mask = BIT(offset);
57380639c7SRussell King 
58380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
59380639c7SRussell King 	val = r->out;
60380639c7SRussell King 	if (value)
61380639c7SRussell King 		val |= mask;
62380639c7SRussell King 	else
63380639c7SRussell King 		val &= ~mask;
64380639c7SRussell King 	r->out = val;
65380639c7SRussell King 	writel_relaxed(val, r->reg);
66380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
67380639c7SRussell King }
68380639c7SRussell King 
69380639c7SRussell King static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
70380639c7SRussell King {
71380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
72380639c7SRussell King 	u32 val, mask = BIT(offset);
73380639c7SRussell King 
74380639c7SRussell King 	if (r->direction & mask) {
75380639c7SRussell King 		/*
76380639c7SRussell King 		 * double-read the value, some registers latch after the
77380639c7SRussell King 		 * first read.
78380639c7SRussell King 		 */
79380639c7SRussell King 		readl_relaxed(r->reg);
80380639c7SRussell King 		val = readl_relaxed(r->reg);
81380639c7SRussell King 	} else {
82380639c7SRussell King 		val = r->out;
83380639c7SRussell King 	}
84380639c7SRussell King 	return !!(val & mask);
85380639c7SRussell King }
86380639c7SRussell King 
87380639c7SRussell King static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
88380639c7SRussell King 	unsigned long *bits)
89380639c7SRussell King {
90380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
91380639c7SRussell King 	unsigned long flags;
92380639c7SRussell King 
93380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
94380639c7SRussell King 	r->out = (r->out & ~*mask) | (*bits & *mask);
95380639c7SRussell King 	writel_relaxed(r->out, r->reg);
96380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
97380639c7SRussell King }
98380639c7SRussell King 
990e3cb6eeSRussell King static int gpio_reg_to_irq(struct gpio_chip *gc, unsigned offset)
1000e3cb6eeSRussell King {
1010e3cb6eeSRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
1020e3cb6eeSRussell King 	int irq = r->irqs[offset];
1030e3cb6eeSRussell King 
1048bb65fc0SGrygorii Strashko 	if (irq >= 0 && r->irqdomain)
1058bb65fc0SGrygorii Strashko 		irq = irq_find_mapping(r->irqdomain, irq);
1060e3cb6eeSRussell King 
1070e3cb6eeSRussell King 	return irq;
1080e3cb6eeSRussell King }
1090e3cb6eeSRussell King 
110380639c7SRussell King /**
111380639c7SRussell King  * gpio_reg_init - add a fixed in/out register as gpio
112380639c7SRussell King  * @dev: optional struct device associated with this register
113380639c7SRussell King  * @base: start gpio number, or -1 to allocate
114380639c7SRussell King  * @num: number of GPIOs, maximum 32
115380639c7SRussell King  * @label: GPIO chip label
116380639c7SRussell King  * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
117380639c7SRussell King  * @def_out: initial GPIO output value
1180e3cb6eeSRussell King  * @names: array of %num strings describing each GPIO signal or %NULL
1190e3cb6eeSRussell King  * @irqdom: irq domain or %NULL
1200e3cb6eeSRussell King  * @irqs: array of %num ints describing the interrupt mapping for each
1210e3cb6eeSRussell King  *        GPIO signal, or %NULL.  If @irqdom is %NULL, then this
1220e3cb6eeSRussell King  *        describes the Linux interrupt number, otherwise it describes
1230e3cb6eeSRussell King  *        the hardware interrupt number in the specified irq domain.
124380639c7SRussell King  *
125380639c7SRussell King  * Add a single-register GPIO device containing up to 32 GPIO signals,
126380639c7SRussell King  * where each GPIO has a fixed input or output configuration.  Only
127380639c7SRussell King  * input GPIOs are assumed to be readable from the register, and only
128380639c7SRussell King  * then after a double-read.  Output values are assumed not to be
129380639c7SRussell King  * readable.
130380639c7SRussell King  */
131380639c7SRussell King struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
132380639c7SRussell King 	int base, int num, const char *label, u32 direction, u32 def_out,
1330e3cb6eeSRussell King 	const char *const *names, struct irq_domain *irqdom, const int *irqs)
134380639c7SRussell King {
135380639c7SRussell King 	struct gpio_reg *r;
136380639c7SRussell King 	int ret;
137380639c7SRussell King 
138380639c7SRussell King 	if (dev)
139380639c7SRussell King 		r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
140380639c7SRussell King 	else
141380639c7SRussell King 		r = kzalloc(sizeof(*r), GFP_KERNEL);
142380639c7SRussell King 
143380639c7SRussell King 	if (!r)
144380639c7SRussell King 		return ERR_PTR(-ENOMEM);
145380639c7SRussell King 
146380639c7SRussell King 	spin_lock_init(&r->lock);
147380639c7SRussell King 
148380639c7SRussell King 	r->gc.label = label;
149380639c7SRussell King 	r->gc.get_direction = gpio_reg_get_direction;
150380639c7SRussell King 	r->gc.direction_input = gpio_reg_direction_input;
151380639c7SRussell King 	r->gc.direction_output = gpio_reg_direction_output;
152380639c7SRussell King 	r->gc.set = gpio_reg_set;
153380639c7SRussell King 	r->gc.get = gpio_reg_get;
154380639c7SRussell King 	r->gc.set_multiple = gpio_reg_set_multiple;
1550e3cb6eeSRussell King 	if (irqs)
1560e3cb6eeSRussell King 		r->gc.to_irq = gpio_reg_to_irq;
157380639c7SRussell King 	r->gc.base = base;
158380639c7SRussell King 	r->gc.ngpio = num;
159380639c7SRussell King 	r->gc.names = names;
160380639c7SRussell King 	r->direction = direction;
161380639c7SRussell King 	r->out = def_out;
162380639c7SRussell King 	r->reg = reg;
1630e3cb6eeSRussell King 	r->irqs = irqs;
164380639c7SRussell King 
165380639c7SRussell King 	if (dev)
166380639c7SRussell King 		ret = devm_gpiochip_add_data(dev, &r->gc, r);
167380639c7SRussell King 	else
168380639c7SRussell King 		ret = gpiochip_add_data(&r->gc, r);
169380639c7SRussell King 
170380639c7SRussell King 	return ret ? ERR_PTR(ret) : &r->gc;
171380639c7SRussell King }
172380639c7SRussell King 
173380639c7SRussell King int gpio_reg_resume(struct gpio_chip *gc)
174380639c7SRussell King {
175380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
176380639c7SRussell King 	unsigned long flags;
177380639c7SRussell King 
178380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
179380639c7SRussell King 	writel_relaxed(r->out, r->reg);
180380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
181380639c7SRussell King 
182380639c7SRussell King 	return 0;
183380639c7SRussell King }
184