xref: /linux/drivers/gpio/gpio-mmio.c (revision 03a975cbcfcd0b3da32a0d55da7d20e7bfdd1827)
1dae5f0afSLinus Walleij // SPDX-License-Identifier: GPL-2.0+
2e9f4d569SChristian Lamparter /*
3e9f4d569SChristian Lamparter  * Generic driver for memory-mapped GPIO controllers.
4e9f4d569SChristian Lamparter  *
5e9f4d569SChristian Lamparter  * Copyright 2008 MontaVista Software, Inc.
6e9f4d569SChristian Lamparter  * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
7e9f4d569SChristian Lamparter  *
8e9f4d569SChristian Lamparter  * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`.......
9e9f4d569SChristian Lamparter  * ...``                                                         ```````..
10e9f4d569SChristian Lamparter  * ..The simplest form of a GPIO controller that the driver supports is``
11e9f4d569SChristian Lamparter  *  `.just a single "data" register, where GPIO state can be read and/or `
12e9f4d569SChristian Lamparter  *    `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.```````
13e9f4d569SChristian Lamparter  *        `````````
14e9f4d569SChristian Lamparter                                     ___
15e9f4d569SChristian Lamparter _/~~|___/~|   . ```~~~~~~       ___/___\___     ,~.`.`.`.`````.~~...,,,,...
16e9f4d569SChristian Lamparter __________|~$@~~~        %~    /o*o*o*o*o*o\   .. Implementing such a GPIO .
17e9f4d569SChristian Lamparter o        `                     ~~~~\___/~~~~    ` controller in FPGA is ,.`
18e9f4d569SChristian Lamparter                                                  `....trivial..'~`.```.```
19e9f4d569SChristian Lamparter  *                                                    ```````
20e9f4d569SChristian Lamparter  *  .```````~~~~`..`.``.``.
21e9f4d569SChristian Lamparter  * .  The driver supports  `...       ,..```.`~~~```````````````....````.``,,
22e9f4d569SChristian Lamparter  * .   big-endian notation, just`.  .. A bit more sophisticated controllers ,
23e9f4d569SChristian Lamparter  *  . register the device with -be`. .with a pair of set/clear-bit registers ,
24e9f4d569SChristian Lamparter  *   `.. suffix.  ```~~`````....`.`   . affecting the data register and the .`
25e9f4d569SChristian Lamparter  *     ``.`.``...```                  ```.. output pins are also supported.`
26e9f4d569SChristian Lamparter  *                        ^^             `````.`````````.,``~``~``~~``````
27e9f4d569SChristian Lamparter  *                                                   .                  ^^
28e9f4d569SChristian Lamparter  *   ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`..
29e9f4d569SChristian Lamparter  * .. The expectation is that in at least some cases .    ,-~~~-,
30e9f4d569SChristian Lamparter  *  .this will be used with roll-your-own ASIC/FPGA .`     \   /
31e9f4d569SChristian Lamparter  *  .logic in Verilog or VHDL. ~~~`````````..`````~~`       \ /
32e9f4d569SChristian Lamparter  *  ..````````......```````````                             \o_
33e9f4d569SChristian Lamparter  *                                                           |
34e9f4d569SChristian Lamparter  *                              ^^                          / \
35e9f4d569SChristian Lamparter  *
36e9f4d569SChristian Lamparter  *           ...`````~~`.....``.`..........``````.`.``.```........``.
37e9f4d569SChristian Lamparter  *            `  8, 16, 32 and 64 bits registers are supported, and``.
38e9f4d569SChristian Lamparter  *            . the number of GPIOs is determined by the width of   ~
39e9f4d569SChristian Lamparter  *             .. the registers. ,............```.`.`..`.`.~~~.`.`.`~
40e9f4d569SChristian Lamparter  *               `.......````.```
41e9f4d569SChristian Lamparter  */
42e9f4d569SChristian Lamparter 
43e9f4d569SChristian Lamparter #include <linux/init.h>
44e9f4d569SChristian Lamparter #include <linux/err.h>
45e9f4d569SChristian Lamparter #include <linux/bug.h>
46e9f4d569SChristian Lamparter #include <linux/kernel.h>
47e9f4d569SChristian Lamparter #include <linux/module.h>
48e9f4d569SChristian Lamparter #include <linux/spinlock.h>
49e9f4d569SChristian Lamparter #include <linux/compiler.h>
50e9f4d569SChristian Lamparter #include <linux/types.h>
51e9f4d569SChristian Lamparter #include <linux/errno.h>
52e9f4d569SChristian Lamparter #include <linux/log2.h>
53e9f4d569SChristian Lamparter #include <linux/ioport.h>
54e9f4d569SChristian Lamparter #include <linux/io.h>
55e9f4d569SChristian Lamparter #include <linux/gpio/driver.h>
56e9f4d569SChristian Lamparter #include <linux/slab.h>
57e9f4d569SChristian Lamparter #include <linux/bitops.h>
58e9f4d569SChristian Lamparter #include <linux/platform_device.h>
59*03a975cbSRob Herring #include <linux/property.h>
60e9f4d569SChristian Lamparter #include <linux/mod_devicetable.h>
61e698613aSÁlvaro Fernández Rojas #include <linux/of.h>
62e9f4d569SChristian Lamparter 
6355b2395eSAsmaa Mnebhi #include "gpiolib.h"
6455b2395eSAsmaa Mnebhi 
65e9f4d569SChristian Lamparter static void bgpio_write8(void __iomem *reg, unsigned long data)
66e9f4d569SChristian Lamparter {
67e9f4d569SChristian Lamparter 	writeb(data, reg);
68e9f4d569SChristian Lamparter }
69e9f4d569SChristian Lamparter 
70e9f4d569SChristian Lamparter static unsigned long bgpio_read8(void __iomem *reg)
71e9f4d569SChristian Lamparter {
72e9f4d569SChristian Lamparter 	return readb(reg);
73e9f4d569SChristian Lamparter }
74e9f4d569SChristian Lamparter 
75e9f4d569SChristian Lamparter static void bgpio_write16(void __iomem *reg, unsigned long data)
76e9f4d569SChristian Lamparter {
77e9f4d569SChristian Lamparter 	writew(data, reg);
78e9f4d569SChristian Lamparter }
79e9f4d569SChristian Lamparter 
80e9f4d569SChristian Lamparter static unsigned long bgpio_read16(void __iomem *reg)
81e9f4d569SChristian Lamparter {
82e9f4d569SChristian Lamparter 	return readw(reg);
83e9f4d569SChristian Lamparter }
84e9f4d569SChristian Lamparter 
85e9f4d569SChristian Lamparter static void bgpio_write32(void __iomem *reg, unsigned long data)
86e9f4d569SChristian Lamparter {
87e9f4d569SChristian Lamparter 	writel(data, reg);
88e9f4d569SChristian Lamparter }
89e9f4d569SChristian Lamparter 
90e9f4d569SChristian Lamparter static unsigned long bgpio_read32(void __iomem *reg)
91e9f4d569SChristian Lamparter {
92e9f4d569SChristian Lamparter 	return readl(reg);
93e9f4d569SChristian Lamparter }
94e9f4d569SChristian Lamparter 
95e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64
96e9f4d569SChristian Lamparter static void bgpio_write64(void __iomem *reg, unsigned long data)
97e9f4d569SChristian Lamparter {
98e9f4d569SChristian Lamparter 	writeq(data, reg);
99e9f4d569SChristian Lamparter }
100e9f4d569SChristian Lamparter 
101e9f4d569SChristian Lamparter static unsigned long bgpio_read64(void __iomem *reg)
102e9f4d569SChristian Lamparter {
103e9f4d569SChristian Lamparter 	return readq(reg);
104e9f4d569SChristian Lamparter }
105e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */
106e9f4d569SChristian Lamparter 
107e9f4d569SChristian Lamparter static void bgpio_write16be(void __iomem *reg, unsigned long data)
108e9f4d569SChristian Lamparter {
109e9f4d569SChristian Lamparter 	iowrite16be(data, reg);
110e9f4d569SChristian Lamparter }
111e9f4d569SChristian Lamparter 
112e9f4d569SChristian Lamparter static unsigned long bgpio_read16be(void __iomem *reg)
113e9f4d569SChristian Lamparter {
114e9f4d569SChristian Lamparter 	return ioread16be(reg);
115e9f4d569SChristian Lamparter }
116e9f4d569SChristian Lamparter 
117e9f4d569SChristian Lamparter static void bgpio_write32be(void __iomem *reg, unsigned long data)
118e9f4d569SChristian Lamparter {
119e9f4d569SChristian Lamparter 	iowrite32be(data, reg);
120e9f4d569SChristian Lamparter }
121e9f4d569SChristian Lamparter 
122e9f4d569SChristian Lamparter static unsigned long bgpio_read32be(void __iomem *reg)
123e9f4d569SChristian Lamparter {
124e9f4d569SChristian Lamparter 	return ioread32be(reg);
125e9f4d569SChristian Lamparter }
126e9f4d569SChristian Lamparter 
12724efd94bSLinus Walleij static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line)
128e9f4d569SChristian Lamparter {
12924efd94bSLinus Walleij 	if (gc->be_bits)
13024efd94bSLinus Walleij 		return BIT(gc->bgpio_bits - 1 - line);
13124efd94bSLinus Walleij 	return BIT(line);
132e9f4d569SChristian Lamparter }
133e9f4d569SChristian Lamparter 
134e9f4d569SChristian Lamparter static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
135e9f4d569SChristian Lamparter {
13624efd94bSLinus Walleij 	unsigned long pinmask = bgpio_line2mask(gc, gpio);
137d799a4deSLinus Walleij 	bool dir = !!(gc->bgpio_dir & pinmask);
138e9f4d569SChristian Lamparter 
139d799a4deSLinus Walleij 	if (dir)
140e9f4d569SChristian Lamparter 		return !!(gc->read_reg(gc->reg_set) & pinmask);
141e9f4d569SChristian Lamparter 	else
142e9f4d569SChristian Lamparter 		return !!(gc->read_reg(gc->reg_dat) & pinmask);
143e9f4d569SChristian Lamparter }
144e9f4d569SChristian Lamparter 
14580057cb4SLinus Walleij /*
14680057cb4SLinus Walleij  * This assumes that the bits in the GPIO register are in native endianness.
14780057cb4SLinus Walleij  * We only assign the function pointer if we have that.
14880057cb4SLinus Walleij  */
14980057cb4SLinus Walleij static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
15080057cb4SLinus Walleij 				  unsigned long *bits)
15180057cb4SLinus Walleij {
15280057cb4SLinus Walleij 	unsigned long get_mask = 0;
15380057cb4SLinus Walleij 	unsigned long set_mask = 0;
15480057cb4SLinus Walleij 
15507c7b6a5SLinus Walleij 	/* Make sure we first clear any bits that are zero when we read the register */
15607c7b6a5SLinus Walleij 	*bits &= ~*mask;
15707c7b6a5SLinus Walleij 
15807c7b6a5SLinus Walleij 	set_mask = *mask & gc->bgpio_dir;
15907c7b6a5SLinus Walleij 	get_mask = *mask & ~gc->bgpio_dir;
16080057cb4SLinus Walleij 
16180057cb4SLinus Walleij 	if (set_mask)
16280057cb4SLinus Walleij 		*bits |= gc->read_reg(gc->reg_set) & set_mask;
16380057cb4SLinus Walleij 	if (get_mask)
16480057cb4SLinus Walleij 		*bits |= gc->read_reg(gc->reg_dat) & get_mask;
16580057cb4SLinus Walleij 
16680057cb4SLinus Walleij 	return 0;
16780057cb4SLinus Walleij }
16880057cb4SLinus Walleij 
169e9f4d569SChristian Lamparter static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
170e9f4d569SChristian Lamparter {
17124efd94bSLinus Walleij 	return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio));
172e9f4d569SChristian Lamparter }
173e9f4d569SChristian Lamparter 
17480057cb4SLinus Walleij /*
17580057cb4SLinus Walleij  * This only works if the bits in the GPIO register are in native endianness.
17680057cb4SLinus Walleij  */
17780057cb4SLinus Walleij static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask,
17880057cb4SLinus Walleij 			      unsigned long *bits)
17980057cb4SLinus Walleij {
18007c7b6a5SLinus Walleij 	/* Make sure we first clear any bits that are zero when we read the register */
18107c7b6a5SLinus Walleij 	*bits &= ~*mask;
18207c7b6a5SLinus Walleij 	*bits |= gc->read_reg(gc->reg_dat) & *mask;
18380057cb4SLinus Walleij 	return 0;
18480057cb4SLinus Walleij }
18580057cb4SLinus Walleij 
18680057cb4SLinus Walleij /*
18780057cb4SLinus Walleij  * With big endian mirrored bit order it becomes more tedious.
18880057cb4SLinus Walleij  */
18980057cb4SLinus Walleij static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask,
19080057cb4SLinus Walleij 				 unsigned long *bits)
19180057cb4SLinus Walleij {
19280057cb4SLinus Walleij 	unsigned long readmask = 0;
19380057cb4SLinus Walleij 	unsigned long val;
19480057cb4SLinus Walleij 	int bit;
19580057cb4SLinus Walleij 
19607c7b6a5SLinus Walleij 	/* Make sure we first clear any bits that are zero when we read the register */
19707c7b6a5SLinus Walleij 	*bits &= ~*mask;
19807c7b6a5SLinus Walleij 
19980057cb4SLinus Walleij 	/* Create a mirrored mask */
200761b5c30SAndy Shevchenko 	for_each_set_bit(bit, mask, gc->ngpio)
20180057cb4SLinus Walleij 		readmask |= bgpio_line2mask(gc, bit);
20280057cb4SLinus Walleij 
20380057cb4SLinus Walleij 	/* Read the register */
20480057cb4SLinus Walleij 	val = gc->read_reg(gc->reg_dat) & readmask;
20580057cb4SLinus Walleij 
20680057cb4SLinus Walleij 	/*
20780057cb4SLinus Walleij 	 * Mirror the result into the "bits" result, this will give line 0
20880057cb4SLinus Walleij 	 * in bit 0 ... line 31 in bit 31 for a 32bit register.
20980057cb4SLinus Walleij 	 */
210761b5c30SAndy Shevchenko 	for_each_set_bit(bit, &val, gc->ngpio)
21180057cb4SLinus Walleij 		*bits |= bgpio_line2mask(gc, bit);
21280057cb4SLinus Walleij 
21380057cb4SLinus Walleij 	return 0;
21480057cb4SLinus Walleij }
21580057cb4SLinus Walleij 
216e9f4d569SChristian Lamparter static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
217e9f4d569SChristian Lamparter {
218e9f4d569SChristian Lamparter }
219e9f4d569SChristian Lamparter 
220e9f4d569SChristian Lamparter static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
221e9f4d569SChristian Lamparter {
22224efd94bSLinus Walleij 	unsigned long mask = bgpio_line2mask(gc, gpio);
223e9f4d569SChristian Lamparter 	unsigned long flags;
224e9f4d569SChristian Lamparter 
2253c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
226e9f4d569SChristian Lamparter 
227e9f4d569SChristian Lamparter 	if (val)
228e9f4d569SChristian Lamparter 		gc->bgpio_data |= mask;
229e9f4d569SChristian Lamparter 	else
230e9f4d569SChristian Lamparter 		gc->bgpio_data &= ~mask;
231e9f4d569SChristian Lamparter 
232e9f4d569SChristian Lamparter 	gc->write_reg(gc->reg_dat, gc->bgpio_data);
233e9f4d569SChristian Lamparter 
2343c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
235e9f4d569SChristian Lamparter }
236e9f4d569SChristian Lamparter 
237e9f4d569SChristian Lamparter static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
238e9f4d569SChristian Lamparter 				 int val)
239e9f4d569SChristian Lamparter {
24024efd94bSLinus Walleij 	unsigned long mask = bgpio_line2mask(gc, gpio);
241e9f4d569SChristian Lamparter 
242e9f4d569SChristian Lamparter 	if (val)
243e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_set, mask);
244e9f4d569SChristian Lamparter 	else
245e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_clr, mask);
246e9f4d569SChristian Lamparter }
247e9f4d569SChristian Lamparter 
248e9f4d569SChristian Lamparter static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
249e9f4d569SChristian Lamparter {
25024efd94bSLinus Walleij 	unsigned long mask = bgpio_line2mask(gc, gpio);
251e9f4d569SChristian Lamparter 	unsigned long flags;
252e9f4d569SChristian Lamparter 
2533c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
254e9f4d569SChristian Lamparter 
255e9f4d569SChristian Lamparter 	if (val)
256e9f4d569SChristian Lamparter 		gc->bgpio_data |= mask;
257e9f4d569SChristian Lamparter 	else
258e9f4d569SChristian Lamparter 		gc->bgpio_data &= ~mask;
259e9f4d569SChristian Lamparter 
260e9f4d569SChristian Lamparter 	gc->write_reg(gc->reg_set, gc->bgpio_data);
261e9f4d569SChristian Lamparter 
2623c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
263e9f4d569SChristian Lamparter }
264e9f4d569SChristian Lamparter 
265e9f4d569SChristian Lamparter static void bgpio_multiple_get_masks(struct gpio_chip *gc,
266e9f4d569SChristian Lamparter 				     unsigned long *mask, unsigned long *bits,
267e9f4d569SChristian Lamparter 				     unsigned long *set_mask,
268e9f4d569SChristian Lamparter 				     unsigned long *clear_mask)
269e9f4d569SChristian Lamparter {
270e9f4d569SChristian Lamparter 	int i;
271e9f4d569SChristian Lamparter 
272e9f4d569SChristian Lamparter 	*set_mask = 0;
273e9f4d569SChristian Lamparter 	*clear_mask = 0;
274e9f4d569SChristian Lamparter 
275761b5c30SAndy Shevchenko 	for_each_set_bit(i, mask, gc->bgpio_bits) {
276e9f4d569SChristian Lamparter 		if (test_bit(i, bits))
27724efd94bSLinus Walleij 			*set_mask |= bgpio_line2mask(gc, i);
278e9f4d569SChristian Lamparter 		else
27924efd94bSLinus Walleij 			*clear_mask |= bgpio_line2mask(gc, i);
280e9f4d569SChristian Lamparter 	}
281e9f4d569SChristian Lamparter }
282e9f4d569SChristian Lamparter 
283e9f4d569SChristian Lamparter static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
284e9f4d569SChristian Lamparter 					  unsigned long *mask,
285e9f4d569SChristian Lamparter 					  unsigned long *bits,
286e9f4d569SChristian Lamparter 					  void __iomem *reg)
287e9f4d569SChristian Lamparter {
288e9f4d569SChristian Lamparter 	unsigned long flags;
289e9f4d569SChristian Lamparter 	unsigned long set_mask, clear_mask;
290e9f4d569SChristian Lamparter 
2913c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
292e9f4d569SChristian Lamparter 
293e9f4d569SChristian Lamparter 	bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
294e9f4d569SChristian Lamparter 
295e9f4d569SChristian Lamparter 	gc->bgpio_data |= set_mask;
296e9f4d569SChristian Lamparter 	gc->bgpio_data &= ~clear_mask;
297e9f4d569SChristian Lamparter 
298e9f4d569SChristian Lamparter 	gc->write_reg(reg, gc->bgpio_data);
299e9f4d569SChristian Lamparter 
3003c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
301e9f4d569SChristian Lamparter }
302e9f4d569SChristian Lamparter 
303e9f4d569SChristian Lamparter static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
304e9f4d569SChristian Lamparter 			       unsigned long *bits)
305e9f4d569SChristian Lamparter {
306e9f4d569SChristian Lamparter 	bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat);
307e9f4d569SChristian Lamparter }
308e9f4d569SChristian Lamparter 
309e9f4d569SChristian Lamparter static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
310e9f4d569SChristian Lamparter 				   unsigned long *bits)
311e9f4d569SChristian Lamparter {
312e9f4d569SChristian Lamparter 	bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set);
313e9f4d569SChristian Lamparter }
314e9f4d569SChristian Lamparter 
315e9f4d569SChristian Lamparter static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
316e9f4d569SChristian Lamparter 					  unsigned long *mask,
317e9f4d569SChristian Lamparter 					  unsigned long *bits)
318e9f4d569SChristian Lamparter {
319e9f4d569SChristian Lamparter 	unsigned long set_mask, clear_mask;
320e9f4d569SChristian Lamparter 
321e9f4d569SChristian Lamparter 	bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
322e9f4d569SChristian Lamparter 
323e9f4d569SChristian Lamparter 	if (set_mask)
324e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_set, set_mask);
325e9f4d569SChristian Lamparter 	if (clear_mask)
326e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_clr, clear_mask);
327e9f4d569SChristian Lamparter }
328e9f4d569SChristian Lamparter 
329e9f4d569SChristian Lamparter static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
330e9f4d569SChristian Lamparter {
331e9f4d569SChristian Lamparter 	return 0;
332e9f4d569SChristian Lamparter }
333e9f4d569SChristian Lamparter 
334e9f4d569SChristian Lamparter static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,
335e9f4d569SChristian Lamparter 				int val)
336e9f4d569SChristian Lamparter {
337e9f4d569SChristian Lamparter 	return -EINVAL;
338e9f4d569SChristian Lamparter }
339e9f4d569SChristian Lamparter 
340e9f4d569SChristian Lamparter static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
341e9f4d569SChristian Lamparter 				int val)
342e9f4d569SChristian Lamparter {
343e9f4d569SChristian Lamparter 	gc->set(gc, gpio, val);
344e9f4d569SChristian Lamparter 
345e9f4d569SChristian Lamparter 	return 0;
346e9f4d569SChristian Lamparter }
347e9f4d569SChristian Lamparter 
348e9f4d569SChristian Lamparter static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
349e9f4d569SChristian Lamparter {
350e9f4d569SChristian Lamparter 	unsigned long flags;
351e9f4d569SChristian Lamparter 
3523c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
353e9f4d569SChristian Lamparter 
35424efd94bSLinus Walleij 	gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
355f69e00bdSLinus Walleij 
356f69e00bdSLinus Walleij 	if (gc->reg_dir_in)
357f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
358f69e00bdSLinus Walleij 	if (gc->reg_dir_out)
359f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
360e9f4d569SChristian Lamparter 
3613c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
362e9f4d569SChristian Lamparter 
363e9f4d569SChristian Lamparter 	return 0;
364e9f4d569SChristian Lamparter }
365e9f4d569SChristian Lamparter 
366e9f4d569SChristian Lamparter static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
367e9f4d569SChristian Lamparter {
368f69e00bdSLinus Walleij 	/* Return 0 if output, 1 if input */
369e42615ecSMatti Vaittinen 	if (gc->bgpio_dir_unreadable) {
370e42615ecSMatti Vaittinen 		if (gc->bgpio_dir & bgpio_line2mask(gc, gpio))
371e42615ecSMatti Vaittinen 			return GPIO_LINE_DIRECTION_OUT;
372e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
373e42615ecSMatti Vaittinen 	}
374e42615ecSMatti Vaittinen 
375e42615ecSMatti Vaittinen 	if (gc->reg_dir_out) {
376e42615ecSMatti Vaittinen 		if (gc->read_reg(gc->reg_dir_out) & bgpio_line2mask(gc, gpio))
377e42615ecSMatti Vaittinen 			return GPIO_LINE_DIRECTION_OUT;
378e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
379e42615ecSMatti Vaittinen 	}
380e42615ecSMatti Vaittinen 
381f69e00bdSLinus Walleij 	if (gc->reg_dir_in)
382e42615ecSMatti Vaittinen 		if (!(gc->read_reg(gc->reg_dir_in) & bgpio_line2mask(gc, gpio)))
383e42615ecSMatti Vaittinen 			return GPIO_LINE_DIRECTION_OUT;
384f69e00bdSLinus Walleij 
385e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_IN;
386e9f4d569SChristian Lamparter }
387e9f4d569SChristian Lamparter 
388d19d2de6SChuanhong Guo static void bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
389e9f4d569SChristian Lamparter {
390e9f4d569SChristian Lamparter 	unsigned long flags;
391e9f4d569SChristian Lamparter 
3923c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
393e9f4d569SChristian Lamparter 
394d799a4deSLinus Walleij 	gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
395f69e00bdSLinus Walleij 
396f69e00bdSLinus Walleij 	if (gc->reg_dir_in)
397f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
398f69e00bdSLinus Walleij 	if (gc->reg_dir_out)
399f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
400e9f4d569SChristian Lamparter 
4013c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
402d19d2de6SChuanhong Guo }
403e9f4d569SChristian Lamparter 
404d19d2de6SChuanhong Guo static int bgpio_dir_out_dir_first(struct gpio_chip *gc, unsigned int gpio,
405d19d2de6SChuanhong Guo 				   int val)
406d19d2de6SChuanhong Guo {
407d19d2de6SChuanhong Guo 	bgpio_dir_out(gc, gpio, val);
408d19d2de6SChuanhong Guo 	gc->set(gc, gpio, val);
409d19d2de6SChuanhong Guo 	return 0;
410d19d2de6SChuanhong Guo }
411d19d2de6SChuanhong Guo 
412d19d2de6SChuanhong Guo static int bgpio_dir_out_val_first(struct gpio_chip *gc, unsigned int gpio,
413d19d2de6SChuanhong Guo 				   int val)
414d19d2de6SChuanhong Guo {
415d19d2de6SChuanhong Guo 	gc->set(gc, gpio, val);
416d19d2de6SChuanhong Guo 	bgpio_dir_out(gc, gpio, val);
417e9f4d569SChristian Lamparter 	return 0;
418e9f4d569SChristian Lamparter }
419e9f4d569SChristian Lamparter 
420e9f4d569SChristian Lamparter static int bgpio_setup_accessors(struct device *dev,
421e9f4d569SChristian Lamparter 				 struct gpio_chip *gc,
422e9f4d569SChristian Lamparter 				 bool byte_be)
423e9f4d569SChristian Lamparter {
424e9f4d569SChristian Lamparter 
425e9f4d569SChristian Lamparter 	switch (gc->bgpio_bits) {
426e9f4d569SChristian Lamparter 	case 8:
427e9f4d569SChristian Lamparter 		gc->read_reg	= bgpio_read8;
428e9f4d569SChristian Lamparter 		gc->write_reg	= bgpio_write8;
429e9f4d569SChristian Lamparter 		break;
430e9f4d569SChristian Lamparter 	case 16:
431e9f4d569SChristian Lamparter 		if (byte_be) {
432e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read16be;
433e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write16be;
434e9f4d569SChristian Lamparter 		} else {
435e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read16;
436e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write16;
437e9f4d569SChristian Lamparter 		}
438e9f4d569SChristian Lamparter 		break;
439e9f4d569SChristian Lamparter 	case 32:
440e9f4d569SChristian Lamparter 		if (byte_be) {
441e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read32be;
442e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write32be;
443e9f4d569SChristian Lamparter 		} else {
444e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read32;
445e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write32;
446e9f4d569SChristian Lamparter 		}
447e9f4d569SChristian Lamparter 		break;
448e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64
449e9f4d569SChristian Lamparter 	case 64:
450e9f4d569SChristian Lamparter 		if (byte_be) {
451e9f4d569SChristian Lamparter 			dev_err(dev,
452e9f4d569SChristian Lamparter 				"64 bit big endian byte order unsupported\n");
453e9f4d569SChristian Lamparter 			return -EINVAL;
454e9f4d569SChristian Lamparter 		} else {
455e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read64;
456e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write64;
457e9f4d569SChristian Lamparter 		}
458e9f4d569SChristian Lamparter 		break;
459e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */
460e9f4d569SChristian Lamparter 	default:
461e9f4d569SChristian Lamparter 		dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits);
462e9f4d569SChristian Lamparter 		return -EINVAL;
463e9f4d569SChristian Lamparter 	}
464e9f4d569SChristian Lamparter 
465e9f4d569SChristian Lamparter 	return 0;
466e9f4d569SChristian Lamparter }
467e9f4d569SChristian Lamparter 
468e9f4d569SChristian Lamparter /*
469e9f4d569SChristian Lamparter  * Create the device and allocate the resources.  For setting GPIO's there are
470e9f4d569SChristian Lamparter  * three supported configurations:
471e9f4d569SChristian Lamparter  *
472e9f4d569SChristian Lamparter  *	- single input/output register resource (named "dat").
473e9f4d569SChristian Lamparter  *	- set/clear pair (named "set" and "clr").
474e9f4d569SChristian Lamparter  *	- single output register resource and single input resource ("set" and
475e9f4d569SChristian Lamparter  *	dat").
476e9f4d569SChristian Lamparter  *
477e9f4d569SChristian Lamparter  * For the single output register, this drives a 1 by setting a bit and a zero
478e9f4d569SChristian Lamparter  * by clearing a bit.  For the set clr pair, this drives a 1 by setting a bit
479e9f4d569SChristian Lamparter  * in the set register and clears it by setting a bit in the clear register.
480e9f4d569SChristian Lamparter  * The configuration is detected by which resources are present.
481e9f4d569SChristian Lamparter  *
482e9f4d569SChristian Lamparter  * For setting the GPIO direction, there are three supported configurations:
483e9f4d569SChristian Lamparter  *
484e9f4d569SChristian Lamparter  *	- simple bidirection GPIO that requires no configuration.
485e9f4d569SChristian Lamparter  *	- an output direction register (named "dirout") where a 1 bit
486e9f4d569SChristian Lamparter  *	indicates the GPIO is an output.
487e9f4d569SChristian Lamparter  *	- an input direction register (named "dirin") where a 1 bit indicates
488e9f4d569SChristian Lamparter  *	the GPIO is an input.
489e9f4d569SChristian Lamparter  */
490e9f4d569SChristian Lamparter static int bgpio_setup_io(struct gpio_chip *gc,
491e9f4d569SChristian Lamparter 			  void __iomem *dat,
492e9f4d569SChristian Lamparter 			  void __iomem *set,
493e9f4d569SChristian Lamparter 			  void __iomem *clr,
494e9f4d569SChristian Lamparter 			  unsigned long flags)
495e9f4d569SChristian Lamparter {
496e9f4d569SChristian Lamparter 
497e9f4d569SChristian Lamparter 	gc->reg_dat = dat;
498e9f4d569SChristian Lamparter 	if (!gc->reg_dat)
499e9f4d569SChristian Lamparter 		return -EINVAL;
500e9f4d569SChristian Lamparter 
501e9f4d569SChristian Lamparter 	if (set && clr) {
502e9f4d569SChristian Lamparter 		gc->reg_set = set;
503e9f4d569SChristian Lamparter 		gc->reg_clr = clr;
504e9f4d569SChristian Lamparter 		gc->set = bgpio_set_with_clear;
505e9f4d569SChristian Lamparter 		gc->set_multiple = bgpio_set_multiple_with_clear;
506e9f4d569SChristian Lamparter 	} else if (set && !clr) {
507e9f4d569SChristian Lamparter 		gc->reg_set = set;
508e9f4d569SChristian Lamparter 		gc->set = bgpio_set_set;
509e9f4d569SChristian Lamparter 		gc->set_multiple = bgpio_set_multiple_set;
510e9f4d569SChristian Lamparter 	} else if (flags & BGPIOF_NO_OUTPUT) {
511e9f4d569SChristian Lamparter 		gc->set = bgpio_set_none;
512e9f4d569SChristian Lamparter 		gc->set_multiple = NULL;
513e9f4d569SChristian Lamparter 	} else {
514e9f4d569SChristian Lamparter 		gc->set = bgpio_set;
515e9f4d569SChristian Lamparter 		gc->set_multiple = bgpio_set_multiple;
516e9f4d569SChristian Lamparter 	}
517e9f4d569SChristian Lamparter 
518e9f4d569SChristian Lamparter 	if (!(flags & BGPIOF_UNREADABLE_REG_SET) &&
51980057cb4SLinus Walleij 	    (flags & BGPIOF_READ_OUTPUT_REG_SET)) {
520e9f4d569SChristian Lamparter 		gc->get = bgpio_get_set;
52180057cb4SLinus Walleij 		if (!gc->be_bits)
52280057cb4SLinus Walleij 			gc->get_multiple = bgpio_get_set_multiple;
52380057cb4SLinus Walleij 		/*
52480057cb4SLinus Walleij 		 * We deliberately avoid assigning the ->get_multiple() call
52580057cb4SLinus Walleij 		 * for big endian mirrored registers which are ALSO reflecting
52680057cb4SLinus Walleij 		 * their value in the set register when used as output. It is
52780057cb4SLinus Walleij 		 * simply too much complexity, let the GPIO core fall back to
52880057cb4SLinus Walleij 		 * reading each line individually in that fringe case.
52980057cb4SLinus Walleij 		 */
53080057cb4SLinus Walleij 	} else {
531e9f4d569SChristian Lamparter 		gc->get = bgpio_get;
53280057cb4SLinus Walleij 		if (gc->be_bits)
53380057cb4SLinus Walleij 			gc->get_multiple = bgpio_get_multiple_be;
53480057cb4SLinus Walleij 		else
53580057cb4SLinus Walleij 			gc->get_multiple = bgpio_get_multiple;
53680057cb4SLinus Walleij 	}
537e9f4d569SChristian Lamparter 
538e9f4d569SChristian Lamparter 	return 0;
539e9f4d569SChristian Lamparter }
540e9f4d569SChristian Lamparter 
541e9f4d569SChristian Lamparter static int bgpio_setup_direction(struct gpio_chip *gc,
542e9f4d569SChristian Lamparter 				 void __iomem *dirout,
543e9f4d569SChristian Lamparter 				 void __iomem *dirin,
544e9f4d569SChristian Lamparter 				 unsigned long flags)
545e9f4d569SChristian Lamparter {
546f69e00bdSLinus Walleij 	if (dirout || dirin) {
547f69e00bdSLinus Walleij 		gc->reg_dir_out = dirout;
548f69e00bdSLinus Walleij 		gc->reg_dir_in = dirin;
549d19d2de6SChuanhong Guo 		if (flags & BGPIOF_NO_SET_ON_INPUT)
550d19d2de6SChuanhong Guo 			gc->direction_output = bgpio_dir_out_dir_first;
551d19d2de6SChuanhong Guo 		else
552d19d2de6SChuanhong Guo 			gc->direction_output = bgpio_dir_out_val_first;
553e9f4d569SChristian Lamparter 		gc->direction_input = bgpio_dir_in;
554e9f4d569SChristian Lamparter 		gc->get_direction = bgpio_get_dir;
555e9f4d569SChristian Lamparter 	} else {
556e9f4d569SChristian Lamparter 		if (flags & BGPIOF_NO_OUTPUT)
557e9f4d569SChristian Lamparter 			gc->direction_output = bgpio_dir_out_err;
558e9f4d569SChristian Lamparter 		else
559e9f4d569SChristian Lamparter 			gc->direction_output = bgpio_simple_dir_out;
560e9f4d569SChristian Lamparter 		gc->direction_input = bgpio_simple_dir_in;
561e9f4d569SChristian Lamparter 	}
562e9f4d569SChristian Lamparter 
563e9f4d569SChristian Lamparter 	return 0;
564e9f4d569SChristian Lamparter }
565e9f4d569SChristian Lamparter 
566e9f4d569SChristian Lamparter static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
567e9f4d569SChristian Lamparter {
568e9f4d569SChristian Lamparter 	if (gpio_pin < chip->ngpio)
569e9f4d569SChristian Lamparter 		return 0;
570e9f4d569SChristian Lamparter 
571e9f4d569SChristian Lamparter 	return -EINVAL;
572e9f4d569SChristian Lamparter }
573e9f4d569SChristian Lamparter 
574d799a4deSLinus Walleij /**
575d799a4deSLinus Walleij  * bgpio_init() - Initialize generic GPIO accessor functions
576d799a4deSLinus Walleij  * @gc: the GPIO chip to set up
577d799a4deSLinus Walleij  * @dev: the parent device of the new GPIO chip (compulsory)
578d799a4deSLinus Walleij  * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4
579d799a4deSLinus Walleij  * @dat: MMIO address for the register to READ the value of the GPIO lines, it
580d799a4deSLinus Walleij  *	is expected that a 1 in the corresponding bit in this register means the
581d799a4deSLinus Walleij  *	line is asserted
582d799a4deSLinus Walleij  * @set: MMIO address for the register to SET the value of the GPIO lines, it is
583d799a4deSLinus Walleij  *	expected that we write the line with 1 in this register to drive the GPIO line
584d799a4deSLinus Walleij  *	high.
585d799a4deSLinus Walleij  * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is
586d799a4deSLinus Walleij  *	expected that we write the line with 1 in this register to drive the GPIO line
587d799a4deSLinus Walleij  *	low. It is allowed to leave this address as NULL, in that case the SET register
588d799a4deSLinus Walleij  *	will be assumed to also clear the GPIO lines, by actively writing the line
589d799a4deSLinus Walleij  *	with 0.
590d799a4deSLinus Walleij  * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed
591d799a4deSLinus Walleij  *	that setting a line to 1 in this register will turn that line into an
592d799a4deSLinus Walleij  *	output line. Conversely, setting the line to 0 will turn that line into
593f69e00bdSLinus Walleij  *	an input.
594d799a4deSLinus Walleij  * @dirin: MMIO address for the register to set this line as INPUT. It is assumed
595d799a4deSLinus Walleij  *	that setting a line to 1 in this register will turn that line into an
596d799a4deSLinus Walleij  *	input line. Conversely, setting the line to 0 will turn that line into
597f69e00bdSLinus Walleij  *	an output.
598d799a4deSLinus Walleij  * @flags: Different flags that will affect the behaviour of the device, such as
599d799a4deSLinus Walleij  *	endianness etc.
600d799a4deSLinus Walleij  */
601e9f4d569SChristian Lamparter int bgpio_init(struct gpio_chip *gc, struct device *dev,
602e9f4d569SChristian Lamparter 	       unsigned long sz, void __iomem *dat, void __iomem *set,
603e9f4d569SChristian Lamparter 	       void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
604e9f4d569SChristian Lamparter 	       unsigned long flags)
605e9f4d569SChristian Lamparter {
606e9f4d569SChristian Lamparter 	int ret;
607e9f4d569SChristian Lamparter 
608e9f4d569SChristian Lamparter 	if (!is_power_of_2(sz))
609e9f4d569SChristian Lamparter 		return -EINVAL;
610e9f4d569SChristian Lamparter 
611e9f4d569SChristian Lamparter 	gc->bgpio_bits = sz * 8;
612e9f4d569SChristian Lamparter 	if (gc->bgpio_bits > BITS_PER_LONG)
613e9f4d569SChristian Lamparter 		return -EINVAL;
614e9f4d569SChristian Lamparter 
6153c938cc5SSchspa Shi 	raw_spin_lock_init(&gc->bgpio_lock);
616e9f4d569SChristian Lamparter 	gc->parent = dev;
617e9f4d569SChristian Lamparter 	gc->label = dev_name(dev);
618e9f4d569SChristian Lamparter 	gc->base = -1;
619e9f4d569SChristian Lamparter 	gc->request = bgpio_request;
62080057cb4SLinus Walleij 	gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN);
621e9f4d569SChristian Lamparter 
62255b2395eSAsmaa Mnebhi 	ret = gpiochip_get_ngpios(gc, dev);
62355b2395eSAsmaa Mnebhi 	if (ret)
62455b2395eSAsmaa Mnebhi 		gc->ngpio = gc->bgpio_bits;
62555b2395eSAsmaa Mnebhi 	else
62655b2395eSAsmaa Mnebhi 		gc->bgpio_bits = roundup_pow_of_two(round_up(gc->ngpio, 8));
62755b2395eSAsmaa Mnebhi 
628e9f4d569SChristian Lamparter 	ret = bgpio_setup_io(gc, dat, set, clr, flags);
629e9f4d569SChristian Lamparter 	if (ret)
630e9f4d569SChristian Lamparter 		return ret;
631e9f4d569SChristian Lamparter 
63224efd94bSLinus Walleij 	ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
633e9f4d569SChristian Lamparter 	if (ret)
634e9f4d569SChristian Lamparter 		return ret;
635e9f4d569SChristian Lamparter 
636e9f4d569SChristian Lamparter 	ret = bgpio_setup_direction(gc, dirout, dirin, flags);
637e9f4d569SChristian Lamparter 	if (ret)
638e9f4d569SChristian Lamparter 		return ret;
639e9f4d569SChristian Lamparter 
640e9f4d569SChristian Lamparter 	gc->bgpio_data = gc->read_reg(gc->reg_dat);
641e9f4d569SChristian Lamparter 	if (gc->set == bgpio_set_set &&
642e9f4d569SChristian Lamparter 			!(flags & BGPIOF_UNREADABLE_REG_SET))
643e9f4d569SChristian Lamparter 		gc->bgpio_data = gc->read_reg(gc->reg_set);
644f69e00bdSLinus Walleij 
645f69e00bdSLinus Walleij 	if (flags & BGPIOF_UNREADABLE_REG_DIR)
646f69e00bdSLinus Walleij 		gc->bgpio_dir_unreadable = true;
647f69e00bdSLinus Walleij 
648f69e00bdSLinus Walleij 	/*
649f69e00bdSLinus Walleij 	 * Inspect hardware to find initial direction setting.
650f69e00bdSLinus Walleij 	 */
651f69e00bdSLinus Walleij 	if ((gc->reg_dir_out || gc->reg_dir_in) &&
652f69e00bdSLinus Walleij 	    !(flags & BGPIOF_UNREADABLE_REG_DIR)) {
653f69e00bdSLinus Walleij 		if (gc->reg_dir_out)
654f69e00bdSLinus Walleij 			gc->bgpio_dir = gc->read_reg(gc->reg_dir_out);
655f69e00bdSLinus Walleij 		else if (gc->reg_dir_in)
656f69e00bdSLinus Walleij 			gc->bgpio_dir = ~gc->read_reg(gc->reg_dir_in);
657f69e00bdSLinus Walleij 		/*
658f69e00bdSLinus Walleij 		 * If we have two direction registers, synchronise
659f69e00bdSLinus Walleij 		 * input setting to output setting, the library
660f69e00bdSLinus Walleij 		 * can not handle a line being input and output at
661f69e00bdSLinus Walleij 		 * the same time.
662f69e00bdSLinus Walleij 		 */
663f69e00bdSLinus Walleij 		if (gc->reg_dir_out && gc->reg_dir_in)
664f69e00bdSLinus Walleij 			gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
665f69e00bdSLinus Walleij 	}
666e9f4d569SChristian Lamparter 
667e9f4d569SChristian Lamparter 	return ret;
668e9f4d569SChristian Lamparter }
669e9f4d569SChristian Lamparter EXPORT_SYMBOL_GPL(bgpio_init);
670e9f4d569SChristian Lamparter 
671e9f4d569SChristian Lamparter #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM)
672e9f4d569SChristian Lamparter 
673e9f4d569SChristian Lamparter static void __iomem *bgpio_map(struct platform_device *pdev,
674e9f4d569SChristian Lamparter 			       const char *name,
675e9f4d569SChristian Lamparter 			       resource_size_t sane_sz)
676e9f4d569SChristian Lamparter {
677e9f4d569SChristian Lamparter 	struct resource *r;
678e9f4d569SChristian Lamparter 	resource_size_t sz;
679e9f4d569SChristian Lamparter 
680e9f4d569SChristian Lamparter 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
681e9f4d569SChristian Lamparter 	if (!r)
682e9f4d569SChristian Lamparter 		return NULL;
683e9f4d569SChristian Lamparter 
684e9f4d569SChristian Lamparter 	sz = resource_size(r);
685e9f4d569SChristian Lamparter 	if (sz != sane_sz)
686e9f4d569SChristian Lamparter 		return IOMEM_ERR_PTR(-EINVAL);
687e9f4d569SChristian Lamparter 
688e9f4d569SChristian Lamparter 	return devm_ioremap_resource(&pdev->dev, r);
689e9f4d569SChristian Lamparter }
690e9f4d569SChristian Lamparter 
691e698613aSÁlvaro Fernández Rojas #ifdef CONFIG_OF
692e698613aSÁlvaro Fernández Rojas static const struct of_device_id bgpio_of_match[] = {
69305cc995fSChristian Lamparter 	{ .compatible = "brcm,bcm6345-gpio" },
694c0d30ecfSChristian Lamparter 	{ .compatible = "wd,mbl-gpio" },
695b8c90199SNathan Sullivan 	{ .compatible = "ni,169445-nand-gpio" },
696e698613aSÁlvaro Fernández Rojas 	{ }
697e698613aSÁlvaro Fernández Rojas };
698e698613aSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bgpio_of_match);
699e698613aSÁlvaro Fernández Rojas 
700e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev,
701e698613aSÁlvaro Fernández Rojas 					  unsigned long *flags)
702e698613aSÁlvaro Fernández Rojas {
703e698613aSÁlvaro Fernández Rojas 	struct bgpio_pdata *pdata;
704e698613aSÁlvaro Fernández Rojas 
705*03a975cbSRob Herring 	if (!pdev->dev.of_node)
706e698613aSÁlvaro Fernández Rojas 		return NULL;
707e698613aSÁlvaro Fernández Rojas 
708e698613aSÁlvaro Fernández Rojas 	pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata),
709e698613aSÁlvaro Fernández Rojas 			     GFP_KERNEL);
710e698613aSÁlvaro Fernández Rojas 	if (!pdata)
711e698613aSÁlvaro Fernández Rojas 		return ERR_PTR(-ENOMEM);
712e698613aSÁlvaro Fernández Rojas 
713e698613aSÁlvaro Fernández Rojas 	pdata->base = -1;
714e698613aSÁlvaro Fernández Rojas 
71505cc995fSChristian Lamparter 	if (of_device_is_big_endian(pdev->dev.of_node))
71605cc995fSChristian Lamparter 		*flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER;
71705cc995fSChristian Lamparter 
718c0d30ecfSChristian Lamparter 	if (of_property_read_bool(pdev->dev.of_node, "no-output"))
719c0d30ecfSChristian Lamparter 		*flags |= BGPIOF_NO_OUTPUT;
720c0d30ecfSChristian Lamparter 
721e698613aSÁlvaro Fernández Rojas 	return pdata;
722e698613aSÁlvaro Fernández Rojas }
723e698613aSÁlvaro Fernández Rojas #else
724e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev,
725e698613aSÁlvaro Fernández Rojas 					  unsigned long *flags)
726e698613aSÁlvaro Fernández Rojas {
727e698613aSÁlvaro Fernández Rojas 	return NULL;
728e698613aSÁlvaro Fernández Rojas }
729e698613aSÁlvaro Fernández Rojas #endif /* CONFIG_OF */
730e698613aSÁlvaro Fernández Rojas 
731e9f4d569SChristian Lamparter static int bgpio_pdev_probe(struct platform_device *pdev)
732e9f4d569SChristian Lamparter {
733e9f4d569SChristian Lamparter 	struct device *dev = &pdev->dev;
734e9f4d569SChristian Lamparter 	struct resource *r;
735e9f4d569SChristian Lamparter 	void __iomem *dat;
736e9f4d569SChristian Lamparter 	void __iomem *set;
737e9f4d569SChristian Lamparter 	void __iomem *clr;
738e9f4d569SChristian Lamparter 	void __iomem *dirout;
739e9f4d569SChristian Lamparter 	void __iomem *dirin;
740e9f4d569SChristian Lamparter 	unsigned long sz;
741e698613aSÁlvaro Fernández Rojas 	unsigned long flags = 0;
742e9f4d569SChristian Lamparter 	int err;
743e9f4d569SChristian Lamparter 	struct gpio_chip *gc;
744e698613aSÁlvaro Fernández Rojas 	struct bgpio_pdata *pdata;
745e698613aSÁlvaro Fernández Rojas 
746e698613aSÁlvaro Fernández Rojas 	pdata = bgpio_parse_dt(pdev, &flags);
747e698613aSÁlvaro Fernández Rojas 	if (IS_ERR(pdata))
748e698613aSÁlvaro Fernández Rojas 		return PTR_ERR(pdata);
749e698613aSÁlvaro Fernández Rojas 
750e698613aSÁlvaro Fernández Rojas 	if (!pdata) {
751e698613aSÁlvaro Fernández Rojas 		pdata = dev_get_platdata(dev);
752e698613aSÁlvaro Fernández Rojas 		flags = pdev->id_entry->driver_data;
753e698613aSÁlvaro Fernández Rojas 	}
754e9f4d569SChristian Lamparter 
755e9f4d569SChristian Lamparter 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
756e9f4d569SChristian Lamparter 	if (!r)
757e9f4d569SChristian Lamparter 		return -EINVAL;
758e9f4d569SChristian Lamparter 
759e9f4d569SChristian Lamparter 	sz = resource_size(r);
760e9f4d569SChristian Lamparter 
761e9f4d569SChristian Lamparter 	dat = bgpio_map(pdev, "dat", sz);
762e9f4d569SChristian Lamparter 	if (IS_ERR(dat))
763e9f4d569SChristian Lamparter 		return PTR_ERR(dat);
764e9f4d569SChristian Lamparter 
765e9f4d569SChristian Lamparter 	set = bgpio_map(pdev, "set", sz);
766e9f4d569SChristian Lamparter 	if (IS_ERR(set))
767e9f4d569SChristian Lamparter 		return PTR_ERR(set);
768e9f4d569SChristian Lamparter 
769e9f4d569SChristian Lamparter 	clr = bgpio_map(pdev, "clr", sz);
770e9f4d569SChristian Lamparter 	if (IS_ERR(clr))
771e9f4d569SChristian Lamparter 		return PTR_ERR(clr);
772e9f4d569SChristian Lamparter 
773e9f4d569SChristian Lamparter 	dirout = bgpio_map(pdev, "dirout", sz);
774e9f4d569SChristian Lamparter 	if (IS_ERR(dirout))
775e9f4d569SChristian Lamparter 		return PTR_ERR(dirout);
776e9f4d569SChristian Lamparter 
777e9f4d569SChristian Lamparter 	dirin = bgpio_map(pdev, "dirin", sz);
778e9f4d569SChristian Lamparter 	if (IS_ERR(dirin))
779e9f4d569SChristian Lamparter 		return PTR_ERR(dirin);
780e9f4d569SChristian Lamparter 
781e9f4d569SChristian Lamparter 	gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
782e9f4d569SChristian Lamparter 	if (!gc)
783e9f4d569SChristian Lamparter 		return -ENOMEM;
784e9f4d569SChristian Lamparter 
785e9f4d569SChristian Lamparter 	err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags);
786e9f4d569SChristian Lamparter 	if (err)
787e9f4d569SChristian Lamparter 		return err;
788e9f4d569SChristian Lamparter 
789e9f4d569SChristian Lamparter 	if (pdata) {
790e9f4d569SChristian Lamparter 		if (pdata->label)
791e9f4d569SChristian Lamparter 			gc->label = pdata->label;
792e9f4d569SChristian Lamparter 		gc->base = pdata->base;
793e9f4d569SChristian Lamparter 		if (pdata->ngpio > 0)
794e9f4d569SChristian Lamparter 			gc->ngpio = pdata->ngpio;
795e9f4d569SChristian Lamparter 	}
796e9f4d569SChristian Lamparter 
797e9f4d569SChristian Lamparter 	platform_set_drvdata(pdev, gc);
798e9f4d569SChristian Lamparter 
799e9f4d569SChristian Lamparter 	return devm_gpiochip_add_data(&pdev->dev, gc, NULL);
800e9f4d569SChristian Lamparter }
801e9f4d569SChristian Lamparter 
802e9f4d569SChristian Lamparter static const struct platform_device_id bgpio_id_table[] = {
803e9f4d569SChristian Lamparter 	{
804e9f4d569SChristian Lamparter 		.name		= "basic-mmio-gpio",
805e9f4d569SChristian Lamparter 		.driver_data	= 0,
806e9f4d569SChristian Lamparter 	}, {
807e9f4d569SChristian Lamparter 		.name		= "basic-mmio-gpio-be",
808e9f4d569SChristian Lamparter 		.driver_data	= BGPIOF_BIG_ENDIAN,
809e9f4d569SChristian Lamparter 	},
810e9f4d569SChristian Lamparter 	{ }
811e9f4d569SChristian Lamparter };
812e9f4d569SChristian Lamparter MODULE_DEVICE_TABLE(platform, bgpio_id_table);
813e9f4d569SChristian Lamparter 
814e9f4d569SChristian Lamparter static struct platform_driver bgpio_driver = {
815e9f4d569SChristian Lamparter 	.driver = {
816e9f4d569SChristian Lamparter 		.name = "basic-mmio-gpio",
817e698613aSÁlvaro Fernández Rojas 		.of_match_table = of_match_ptr(bgpio_of_match),
818e9f4d569SChristian Lamparter 	},
819e9f4d569SChristian Lamparter 	.id_table = bgpio_id_table,
820e9f4d569SChristian Lamparter 	.probe = bgpio_pdev_probe,
821e9f4d569SChristian Lamparter };
822e9f4d569SChristian Lamparter 
823e9f4d569SChristian Lamparter module_platform_driver(bgpio_driver);
824e9f4d569SChristian Lamparter 
825e9f4d569SChristian Lamparter #endif /* CONFIG_GPIO_GENERIC_PLATFORM */
826e9f4d569SChristian Lamparter 
827e9f4d569SChristian Lamparter MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
828e9f4d569SChristian Lamparter MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
829e9f4d569SChristian Lamparter MODULE_LICENSE("GPL");
830