xref: /linux/drivers/gpio/gpio-reg.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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  */
7*d74c0863SAndy Shevchenko #include <linux/bits.h>
8*d74c0863SAndy Shevchenko #include <linux/container_of.h>
9*d74c0863SAndy Shevchenko #include <linux/device.h>
10*d74c0863SAndy Shevchenko #include <linux/err.h>
11*d74c0863SAndy Shevchenko #include <linux/errno.h>
12380639c7SRussell King #include <linux/io.h>
13*d74c0863SAndy Shevchenko #include <linux/irqdomain.h>
14380639c7SRussell King #include <linux/slab.h>
15380639c7SRussell King #include <linux/spinlock.h>
16*d74c0863SAndy Shevchenko #include <linux/types.h>
17*d74c0863SAndy Shevchenko 
18*d74c0863SAndy Shevchenko #include <linux/gpio/driver.h>
19*d74c0863SAndy Shevchenko #include <linux/gpio/gpio-reg.h>
20380639c7SRussell King 
21380639c7SRussell King struct gpio_reg {
22380639c7SRussell King 	struct gpio_chip gc;
23380639c7SRussell King 	spinlock_t lock;
24380639c7SRussell King 	u32 direction;
25380639c7SRussell King 	u32 out;
26380639c7SRussell King 	void __iomem *reg;
270e3cb6eeSRussell King 	struct irq_domain *irqdomain;
280e3cb6eeSRussell King 	const int *irqs;
29380639c7SRussell King };
30380639c7SRussell King 
31380639c7SRussell King #define to_gpio_reg(x) container_of(x, struct gpio_reg, gc)
32380639c7SRussell King 
gpio_reg_get_direction(struct gpio_chip * gc,unsigned offset)33380639c7SRussell King static int gpio_reg_get_direction(struct gpio_chip *gc, unsigned offset)
34380639c7SRussell King {
35380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
36380639c7SRussell King 
37e42615ecSMatti Vaittinen 	return r->direction & BIT(offset) ? GPIO_LINE_DIRECTION_IN :
38e42615ecSMatti Vaittinen 					    GPIO_LINE_DIRECTION_OUT;
39380639c7SRussell King }
40380639c7SRussell King 
gpio_reg_direction_output(struct gpio_chip * gc,unsigned offset,int value)41380639c7SRussell King static int gpio_reg_direction_output(struct gpio_chip *gc, unsigned offset,
42380639c7SRussell King 	int value)
43380639c7SRussell King {
44380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
45380639c7SRussell King 
46380639c7SRussell King 	if (r->direction & BIT(offset))
47380639c7SRussell King 		return -ENOTSUPP;
48380639c7SRussell King 
49380639c7SRussell King 	gc->set(gc, offset, value);
50380639c7SRussell King 	return 0;
51380639c7SRussell King }
52380639c7SRussell King 
gpio_reg_direction_input(struct gpio_chip * gc,unsigned offset)53380639c7SRussell King static int gpio_reg_direction_input(struct gpio_chip *gc, unsigned offset)
54380639c7SRussell King {
55380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
56380639c7SRussell King 
57380639c7SRussell King 	return r->direction & BIT(offset) ? 0 : -ENOTSUPP;
58380639c7SRussell King }
59380639c7SRussell King 
gpio_reg_set(struct gpio_chip * gc,unsigned offset,int value)60380639c7SRussell King static void gpio_reg_set(struct gpio_chip *gc, unsigned offset, int value)
61380639c7SRussell King {
62380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
63380639c7SRussell King 	unsigned long flags;
64380639c7SRussell King 	u32 val, mask = BIT(offset);
65380639c7SRussell King 
66380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
67380639c7SRussell King 	val = r->out;
68380639c7SRussell King 	if (value)
69380639c7SRussell King 		val |= mask;
70380639c7SRussell King 	else
71380639c7SRussell King 		val &= ~mask;
72380639c7SRussell King 	r->out = val;
73380639c7SRussell King 	writel_relaxed(val, r->reg);
74380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
75380639c7SRussell King }
76380639c7SRussell King 
gpio_reg_get(struct gpio_chip * gc,unsigned offset)77380639c7SRussell King static int gpio_reg_get(struct gpio_chip *gc, unsigned offset)
78380639c7SRussell King {
79380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
80380639c7SRussell King 	u32 val, mask = BIT(offset);
81380639c7SRussell King 
82380639c7SRussell King 	if (r->direction & mask) {
83380639c7SRussell King 		/*
84380639c7SRussell King 		 * double-read the value, some registers latch after the
85380639c7SRussell King 		 * first read.
86380639c7SRussell King 		 */
87380639c7SRussell King 		readl_relaxed(r->reg);
88380639c7SRussell King 		val = readl_relaxed(r->reg);
89380639c7SRussell King 	} else {
90380639c7SRussell King 		val = r->out;
91380639c7SRussell King 	}
92380639c7SRussell King 	return !!(val & mask);
93380639c7SRussell King }
94380639c7SRussell King 
gpio_reg_set_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)95380639c7SRussell King static void gpio_reg_set_multiple(struct gpio_chip *gc, unsigned long *mask,
96380639c7SRussell King 	unsigned long *bits)
97380639c7SRussell King {
98380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
99380639c7SRussell King 	unsigned long flags;
100380639c7SRussell King 
101380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
102380639c7SRussell King 	r->out = (r->out & ~*mask) | (*bits & *mask);
103380639c7SRussell King 	writel_relaxed(r->out, r->reg);
104380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
105380639c7SRussell King }
106380639c7SRussell King 
gpio_reg_to_irq(struct gpio_chip * gc,unsigned offset)1070e3cb6eeSRussell King static int gpio_reg_to_irq(struct gpio_chip *gc, unsigned offset)
1080e3cb6eeSRussell King {
1090e3cb6eeSRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
1100e3cb6eeSRussell King 	int irq = r->irqs[offset];
1110e3cb6eeSRussell King 
1128bb65fc0SGrygorii Strashko 	if (irq >= 0 && r->irqdomain)
1138bb65fc0SGrygorii Strashko 		irq = irq_find_mapping(r->irqdomain, irq);
1140e3cb6eeSRussell King 
1150e3cb6eeSRussell King 	return irq;
1160e3cb6eeSRussell King }
1170e3cb6eeSRussell King 
118380639c7SRussell King /**
119380639c7SRussell King  * gpio_reg_init - add a fixed in/out register as gpio
120380639c7SRussell King  * @dev: optional struct device associated with this register
121380639c7SRussell King  * @base: start gpio number, or -1 to allocate
122380639c7SRussell King  * @num: number of GPIOs, maximum 32
123380639c7SRussell King  * @label: GPIO chip label
124380639c7SRussell King  * @direction: bitmask of fixed direction, one per GPIO signal, 1 = in
125380639c7SRussell King  * @def_out: initial GPIO output value
1260e3cb6eeSRussell King  * @names: array of %num strings describing each GPIO signal or %NULL
1270e3cb6eeSRussell King  * @irqdom: irq domain or %NULL
1280e3cb6eeSRussell King  * @irqs: array of %num ints describing the interrupt mapping for each
1290e3cb6eeSRussell King  *        GPIO signal, or %NULL.  If @irqdom is %NULL, then this
1300e3cb6eeSRussell King  *        describes the Linux interrupt number, otherwise it describes
1310e3cb6eeSRussell King  *        the hardware interrupt number in the specified irq domain.
132380639c7SRussell King  *
133380639c7SRussell King  * Add a single-register GPIO device containing up to 32 GPIO signals,
134380639c7SRussell King  * where each GPIO has a fixed input or output configuration.  Only
135380639c7SRussell King  * input GPIOs are assumed to be readable from the register, and only
136380639c7SRussell King  * then after a double-read.  Output values are assumed not to be
137380639c7SRussell King  * readable.
138380639c7SRussell King  */
gpio_reg_init(struct device * dev,void __iomem * reg,int base,int num,const char * label,u32 direction,u32 def_out,const char * const * names,struct irq_domain * irqdom,const int * irqs)139380639c7SRussell King struct gpio_chip *gpio_reg_init(struct device *dev, void __iomem *reg,
140380639c7SRussell King 	int base, int num, const char *label, u32 direction, u32 def_out,
1410e3cb6eeSRussell King 	const char *const *names, struct irq_domain *irqdom, const int *irqs)
142380639c7SRussell King {
143380639c7SRussell King 	struct gpio_reg *r;
144380639c7SRussell King 	int ret;
145380639c7SRussell King 
146380639c7SRussell King 	if (dev)
147380639c7SRussell King 		r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
148380639c7SRussell King 	else
149380639c7SRussell King 		r = kzalloc(sizeof(*r), GFP_KERNEL);
150380639c7SRussell King 
151380639c7SRussell King 	if (!r)
152380639c7SRussell King 		return ERR_PTR(-ENOMEM);
153380639c7SRussell King 
154380639c7SRussell King 	spin_lock_init(&r->lock);
155380639c7SRussell King 
156380639c7SRussell King 	r->gc.label = label;
157380639c7SRussell King 	r->gc.get_direction = gpio_reg_get_direction;
158380639c7SRussell King 	r->gc.direction_input = gpio_reg_direction_input;
159380639c7SRussell King 	r->gc.direction_output = gpio_reg_direction_output;
160380639c7SRussell King 	r->gc.set = gpio_reg_set;
161380639c7SRussell King 	r->gc.get = gpio_reg_get;
162380639c7SRussell King 	r->gc.set_multiple = gpio_reg_set_multiple;
1630e3cb6eeSRussell King 	if (irqs)
1640e3cb6eeSRussell King 		r->gc.to_irq = gpio_reg_to_irq;
165380639c7SRussell King 	r->gc.base = base;
166380639c7SRussell King 	r->gc.ngpio = num;
167380639c7SRussell King 	r->gc.names = names;
168380639c7SRussell King 	r->direction = direction;
169380639c7SRussell King 	r->out = def_out;
170380639c7SRussell King 	r->reg = reg;
1710e3cb6eeSRussell King 	r->irqs = irqs;
172380639c7SRussell King 
173380639c7SRussell King 	if (dev)
174380639c7SRussell King 		ret = devm_gpiochip_add_data(dev, &r->gc, r);
175380639c7SRussell King 	else
176380639c7SRussell King 		ret = gpiochip_add_data(&r->gc, r);
177380639c7SRussell King 
178380639c7SRussell King 	return ret ? ERR_PTR(ret) : &r->gc;
179380639c7SRussell King }
180380639c7SRussell King 
gpio_reg_resume(struct gpio_chip * gc)181380639c7SRussell King int gpio_reg_resume(struct gpio_chip *gc)
182380639c7SRussell King {
183380639c7SRussell King 	struct gpio_reg *r = to_gpio_reg(gc);
184380639c7SRussell King 	unsigned long flags;
185380639c7SRussell King 
186380639c7SRussell King 	spin_lock_irqsave(&r->lock, flags);
187380639c7SRussell King 	writel_relaxed(r->out, r->reg);
188380639c7SRussell King 	spin_unlock_irqrestore(&r->lock, flags);
189380639c7SRussell King 
190380639c7SRussell King 	return 0;
191380639c7SRussell King }
192