xref: /linux/drivers/gpio/gpio-mmio.c (revision 3c938cc5cebcbd2291fe97f523c0705a2c24c77d)
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>
59e9f4d569SChristian Lamparter #include <linux/mod_devicetable.h>
60e698613aSÁlvaro Fernández Rojas #include <linux/of.h>
61e698613aSÁlvaro Fernández Rojas #include <linux/of_device.h>
62e9f4d569SChristian Lamparter 
63e9f4d569SChristian Lamparter static void bgpio_write8(void __iomem *reg, unsigned long data)
64e9f4d569SChristian Lamparter {
65e9f4d569SChristian Lamparter 	writeb(data, reg);
66e9f4d569SChristian Lamparter }
67e9f4d569SChristian Lamparter 
68e9f4d569SChristian Lamparter static unsigned long bgpio_read8(void __iomem *reg)
69e9f4d569SChristian Lamparter {
70e9f4d569SChristian Lamparter 	return readb(reg);
71e9f4d569SChristian Lamparter }
72e9f4d569SChristian Lamparter 
73e9f4d569SChristian Lamparter static void bgpio_write16(void __iomem *reg, unsigned long data)
74e9f4d569SChristian Lamparter {
75e9f4d569SChristian Lamparter 	writew(data, reg);
76e9f4d569SChristian Lamparter }
77e9f4d569SChristian Lamparter 
78e9f4d569SChristian Lamparter static unsigned long bgpio_read16(void __iomem *reg)
79e9f4d569SChristian Lamparter {
80e9f4d569SChristian Lamparter 	return readw(reg);
81e9f4d569SChristian Lamparter }
82e9f4d569SChristian Lamparter 
83e9f4d569SChristian Lamparter static void bgpio_write32(void __iomem *reg, unsigned long data)
84e9f4d569SChristian Lamparter {
85e9f4d569SChristian Lamparter 	writel(data, reg);
86e9f4d569SChristian Lamparter }
87e9f4d569SChristian Lamparter 
88e9f4d569SChristian Lamparter static unsigned long bgpio_read32(void __iomem *reg)
89e9f4d569SChristian Lamparter {
90e9f4d569SChristian Lamparter 	return readl(reg);
91e9f4d569SChristian Lamparter }
92e9f4d569SChristian Lamparter 
93e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64
94e9f4d569SChristian Lamparter static void bgpio_write64(void __iomem *reg, unsigned long data)
95e9f4d569SChristian Lamparter {
96e9f4d569SChristian Lamparter 	writeq(data, reg);
97e9f4d569SChristian Lamparter }
98e9f4d569SChristian Lamparter 
99e9f4d569SChristian Lamparter static unsigned long bgpio_read64(void __iomem *reg)
100e9f4d569SChristian Lamparter {
101e9f4d569SChristian Lamparter 	return readq(reg);
102e9f4d569SChristian Lamparter }
103e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */
104e9f4d569SChristian Lamparter 
105e9f4d569SChristian Lamparter static void bgpio_write16be(void __iomem *reg, unsigned long data)
106e9f4d569SChristian Lamparter {
107e9f4d569SChristian Lamparter 	iowrite16be(data, reg);
108e9f4d569SChristian Lamparter }
109e9f4d569SChristian Lamparter 
110e9f4d569SChristian Lamparter static unsigned long bgpio_read16be(void __iomem *reg)
111e9f4d569SChristian Lamparter {
112e9f4d569SChristian Lamparter 	return ioread16be(reg);
113e9f4d569SChristian Lamparter }
114e9f4d569SChristian Lamparter 
115e9f4d569SChristian Lamparter static void bgpio_write32be(void __iomem *reg, unsigned long data)
116e9f4d569SChristian Lamparter {
117e9f4d569SChristian Lamparter 	iowrite32be(data, reg);
118e9f4d569SChristian Lamparter }
119e9f4d569SChristian Lamparter 
120e9f4d569SChristian Lamparter static unsigned long bgpio_read32be(void __iomem *reg)
121e9f4d569SChristian Lamparter {
122e9f4d569SChristian Lamparter 	return ioread32be(reg);
123e9f4d569SChristian Lamparter }
124e9f4d569SChristian Lamparter 
12524efd94bSLinus Walleij static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line)
126e9f4d569SChristian Lamparter {
12724efd94bSLinus Walleij 	if (gc->be_bits)
12824efd94bSLinus Walleij 		return BIT(gc->bgpio_bits - 1 - line);
12924efd94bSLinus Walleij 	return BIT(line);
130e9f4d569SChristian Lamparter }
131e9f4d569SChristian Lamparter 
132e9f4d569SChristian Lamparter static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
133e9f4d569SChristian Lamparter {
13424efd94bSLinus Walleij 	unsigned long pinmask = bgpio_line2mask(gc, gpio);
135d799a4deSLinus Walleij 	bool dir = !!(gc->bgpio_dir & pinmask);
136e9f4d569SChristian Lamparter 
137d799a4deSLinus Walleij 	if (dir)
138e9f4d569SChristian Lamparter 		return !!(gc->read_reg(gc->reg_set) & pinmask);
139e9f4d569SChristian Lamparter 	else
140e9f4d569SChristian Lamparter 		return !!(gc->read_reg(gc->reg_dat) & pinmask);
141e9f4d569SChristian Lamparter }
142e9f4d569SChristian Lamparter 
14380057cb4SLinus Walleij /*
14480057cb4SLinus Walleij  * This assumes that the bits in the GPIO register are in native endianness.
14580057cb4SLinus Walleij  * We only assign the function pointer if we have that.
14680057cb4SLinus Walleij  */
14780057cb4SLinus Walleij static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
14880057cb4SLinus Walleij 				  unsigned long *bits)
14980057cb4SLinus Walleij {
15080057cb4SLinus Walleij 	unsigned long get_mask = 0;
15180057cb4SLinus Walleij 	unsigned long set_mask = 0;
15280057cb4SLinus Walleij 
15307c7b6a5SLinus Walleij 	/* Make sure we first clear any bits that are zero when we read the register */
15407c7b6a5SLinus Walleij 	*bits &= ~*mask;
15507c7b6a5SLinus Walleij 
15607c7b6a5SLinus Walleij 	set_mask = *mask & gc->bgpio_dir;
15707c7b6a5SLinus Walleij 	get_mask = *mask & ~gc->bgpio_dir;
15880057cb4SLinus Walleij 
15980057cb4SLinus Walleij 	if (set_mask)
16080057cb4SLinus Walleij 		*bits |= gc->read_reg(gc->reg_set) & set_mask;
16180057cb4SLinus Walleij 	if (get_mask)
16280057cb4SLinus Walleij 		*bits |= gc->read_reg(gc->reg_dat) & get_mask;
16380057cb4SLinus Walleij 
16480057cb4SLinus Walleij 	return 0;
16580057cb4SLinus Walleij }
16680057cb4SLinus Walleij 
167e9f4d569SChristian Lamparter static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
168e9f4d569SChristian Lamparter {
16924efd94bSLinus Walleij 	return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio));
170e9f4d569SChristian Lamparter }
171e9f4d569SChristian Lamparter 
17280057cb4SLinus Walleij /*
17380057cb4SLinus Walleij  * This only works if the bits in the GPIO register are in native endianness.
17480057cb4SLinus Walleij  */
17580057cb4SLinus Walleij static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask,
17680057cb4SLinus Walleij 			      unsigned long *bits)
17780057cb4SLinus Walleij {
17807c7b6a5SLinus Walleij 	/* Make sure we first clear any bits that are zero when we read the register */
17907c7b6a5SLinus Walleij 	*bits &= ~*mask;
18007c7b6a5SLinus Walleij 	*bits |= gc->read_reg(gc->reg_dat) & *mask;
18180057cb4SLinus Walleij 	return 0;
18280057cb4SLinus Walleij }
18380057cb4SLinus Walleij 
18480057cb4SLinus Walleij /*
18580057cb4SLinus Walleij  * With big endian mirrored bit order it becomes more tedious.
18680057cb4SLinus Walleij  */
18780057cb4SLinus Walleij static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask,
18880057cb4SLinus Walleij 				 unsigned long *bits)
18980057cb4SLinus Walleij {
19080057cb4SLinus Walleij 	unsigned long readmask = 0;
19180057cb4SLinus Walleij 	unsigned long val;
19280057cb4SLinus Walleij 	int bit;
19380057cb4SLinus Walleij 
19407c7b6a5SLinus Walleij 	/* Make sure we first clear any bits that are zero when we read the register */
19507c7b6a5SLinus Walleij 	*bits &= ~*mask;
19607c7b6a5SLinus Walleij 
19780057cb4SLinus Walleij 	/* Create a mirrored mask */
198761b5c30SAndy Shevchenko 	for_each_set_bit(bit, mask, gc->ngpio)
19980057cb4SLinus Walleij 		readmask |= bgpio_line2mask(gc, bit);
20080057cb4SLinus Walleij 
20180057cb4SLinus Walleij 	/* Read the register */
20280057cb4SLinus Walleij 	val = gc->read_reg(gc->reg_dat) & readmask;
20380057cb4SLinus Walleij 
20480057cb4SLinus Walleij 	/*
20580057cb4SLinus Walleij 	 * Mirror the result into the "bits" result, this will give line 0
20680057cb4SLinus Walleij 	 * in bit 0 ... line 31 in bit 31 for a 32bit register.
20780057cb4SLinus Walleij 	 */
208761b5c30SAndy Shevchenko 	for_each_set_bit(bit, &val, gc->ngpio)
20980057cb4SLinus Walleij 		*bits |= bgpio_line2mask(gc, bit);
21080057cb4SLinus Walleij 
21180057cb4SLinus Walleij 	return 0;
21280057cb4SLinus Walleij }
21380057cb4SLinus Walleij 
214e9f4d569SChristian Lamparter static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
215e9f4d569SChristian Lamparter {
216e9f4d569SChristian Lamparter }
217e9f4d569SChristian Lamparter 
218e9f4d569SChristian Lamparter static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
219e9f4d569SChristian Lamparter {
22024efd94bSLinus Walleij 	unsigned long mask = bgpio_line2mask(gc, gpio);
221e9f4d569SChristian Lamparter 	unsigned long flags;
222e9f4d569SChristian Lamparter 
223*3c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
224e9f4d569SChristian Lamparter 
225e9f4d569SChristian Lamparter 	if (val)
226e9f4d569SChristian Lamparter 		gc->bgpio_data |= mask;
227e9f4d569SChristian Lamparter 	else
228e9f4d569SChristian Lamparter 		gc->bgpio_data &= ~mask;
229e9f4d569SChristian Lamparter 
230e9f4d569SChristian Lamparter 	gc->write_reg(gc->reg_dat, gc->bgpio_data);
231e9f4d569SChristian Lamparter 
232*3c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
233e9f4d569SChristian Lamparter }
234e9f4d569SChristian Lamparter 
235e9f4d569SChristian Lamparter static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
236e9f4d569SChristian Lamparter 				 int val)
237e9f4d569SChristian Lamparter {
23824efd94bSLinus Walleij 	unsigned long mask = bgpio_line2mask(gc, gpio);
239e9f4d569SChristian Lamparter 
240e9f4d569SChristian Lamparter 	if (val)
241e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_set, mask);
242e9f4d569SChristian Lamparter 	else
243e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_clr, mask);
244e9f4d569SChristian Lamparter }
245e9f4d569SChristian Lamparter 
246e9f4d569SChristian Lamparter static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
247e9f4d569SChristian Lamparter {
24824efd94bSLinus Walleij 	unsigned long mask = bgpio_line2mask(gc, gpio);
249e9f4d569SChristian Lamparter 	unsigned long flags;
250e9f4d569SChristian Lamparter 
251*3c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
252e9f4d569SChristian Lamparter 
253e9f4d569SChristian Lamparter 	if (val)
254e9f4d569SChristian Lamparter 		gc->bgpio_data |= mask;
255e9f4d569SChristian Lamparter 	else
256e9f4d569SChristian Lamparter 		gc->bgpio_data &= ~mask;
257e9f4d569SChristian Lamparter 
258e9f4d569SChristian Lamparter 	gc->write_reg(gc->reg_set, gc->bgpio_data);
259e9f4d569SChristian Lamparter 
260*3c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
261e9f4d569SChristian Lamparter }
262e9f4d569SChristian Lamparter 
263e9f4d569SChristian Lamparter static void bgpio_multiple_get_masks(struct gpio_chip *gc,
264e9f4d569SChristian Lamparter 				     unsigned long *mask, unsigned long *bits,
265e9f4d569SChristian Lamparter 				     unsigned long *set_mask,
266e9f4d569SChristian Lamparter 				     unsigned long *clear_mask)
267e9f4d569SChristian Lamparter {
268e9f4d569SChristian Lamparter 	int i;
269e9f4d569SChristian Lamparter 
270e9f4d569SChristian Lamparter 	*set_mask = 0;
271e9f4d569SChristian Lamparter 	*clear_mask = 0;
272e9f4d569SChristian Lamparter 
273761b5c30SAndy Shevchenko 	for_each_set_bit(i, mask, gc->bgpio_bits) {
274e9f4d569SChristian Lamparter 		if (test_bit(i, bits))
27524efd94bSLinus Walleij 			*set_mask |= bgpio_line2mask(gc, i);
276e9f4d569SChristian Lamparter 		else
27724efd94bSLinus Walleij 			*clear_mask |= bgpio_line2mask(gc, i);
278e9f4d569SChristian Lamparter 	}
279e9f4d569SChristian Lamparter }
280e9f4d569SChristian Lamparter 
281e9f4d569SChristian Lamparter static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
282e9f4d569SChristian Lamparter 					  unsigned long *mask,
283e9f4d569SChristian Lamparter 					  unsigned long *bits,
284e9f4d569SChristian Lamparter 					  void __iomem *reg)
285e9f4d569SChristian Lamparter {
286e9f4d569SChristian Lamparter 	unsigned long flags;
287e9f4d569SChristian Lamparter 	unsigned long set_mask, clear_mask;
288e9f4d569SChristian Lamparter 
289*3c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
290e9f4d569SChristian Lamparter 
291e9f4d569SChristian Lamparter 	bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
292e9f4d569SChristian Lamparter 
293e9f4d569SChristian Lamparter 	gc->bgpio_data |= set_mask;
294e9f4d569SChristian Lamparter 	gc->bgpio_data &= ~clear_mask;
295e9f4d569SChristian Lamparter 
296e9f4d569SChristian Lamparter 	gc->write_reg(reg, gc->bgpio_data);
297e9f4d569SChristian Lamparter 
298*3c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
299e9f4d569SChristian Lamparter }
300e9f4d569SChristian Lamparter 
301e9f4d569SChristian Lamparter static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
302e9f4d569SChristian Lamparter 			       unsigned long *bits)
303e9f4d569SChristian Lamparter {
304e9f4d569SChristian Lamparter 	bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat);
305e9f4d569SChristian Lamparter }
306e9f4d569SChristian Lamparter 
307e9f4d569SChristian Lamparter static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
308e9f4d569SChristian Lamparter 				   unsigned long *bits)
309e9f4d569SChristian Lamparter {
310e9f4d569SChristian Lamparter 	bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set);
311e9f4d569SChristian Lamparter }
312e9f4d569SChristian Lamparter 
313e9f4d569SChristian Lamparter static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
314e9f4d569SChristian Lamparter 					  unsigned long *mask,
315e9f4d569SChristian Lamparter 					  unsigned long *bits)
316e9f4d569SChristian Lamparter {
317e9f4d569SChristian Lamparter 	unsigned long set_mask, clear_mask;
318e9f4d569SChristian Lamparter 
319e9f4d569SChristian Lamparter 	bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
320e9f4d569SChristian Lamparter 
321e9f4d569SChristian Lamparter 	if (set_mask)
322e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_set, set_mask);
323e9f4d569SChristian Lamparter 	if (clear_mask)
324e9f4d569SChristian Lamparter 		gc->write_reg(gc->reg_clr, clear_mask);
325e9f4d569SChristian Lamparter }
326e9f4d569SChristian Lamparter 
327e9f4d569SChristian Lamparter static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
328e9f4d569SChristian Lamparter {
329e9f4d569SChristian Lamparter 	return 0;
330e9f4d569SChristian Lamparter }
331e9f4d569SChristian Lamparter 
332e9f4d569SChristian Lamparter static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,
333e9f4d569SChristian Lamparter 				int val)
334e9f4d569SChristian Lamparter {
335e9f4d569SChristian Lamparter 	return -EINVAL;
336e9f4d569SChristian Lamparter }
337e9f4d569SChristian Lamparter 
338e9f4d569SChristian Lamparter static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
339e9f4d569SChristian Lamparter 				int val)
340e9f4d569SChristian Lamparter {
341e9f4d569SChristian Lamparter 	gc->set(gc, gpio, val);
342e9f4d569SChristian Lamparter 
343e9f4d569SChristian Lamparter 	return 0;
344e9f4d569SChristian Lamparter }
345e9f4d569SChristian Lamparter 
346e9f4d569SChristian Lamparter static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
347e9f4d569SChristian Lamparter {
348e9f4d569SChristian Lamparter 	unsigned long flags;
349e9f4d569SChristian Lamparter 
350*3c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
351e9f4d569SChristian Lamparter 
35224efd94bSLinus Walleij 	gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
353f69e00bdSLinus Walleij 
354f69e00bdSLinus Walleij 	if (gc->reg_dir_in)
355f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
356f69e00bdSLinus Walleij 	if (gc->reg_dir_out)
357f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
358e9f4d569SChristian Lamparter 
359*3c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
360e9f4d569SChristian Lamparter 
361e9f4d569SChristian Lamparter 	return 0;
362e9f4d569SChristian Lamparter }
363e9f4d569SChristian Lamparter 
364e9f4d569SChristian Lamparter static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
365e9f4d569SChristian Lamparter {
366f69e00bdSLinus Walleij 	/* Return 0 if output, 1 if input */
367e42615ecSMatti Vaittinen 	if (gc->bgpio_dir_unreadable) {
368e42615ecSMatti Vaittinen 		if (gc->bgpio_dir & bgpio_line2mask(gc, gpio))
369e42615ecSMatti Vaittinen 			return GPIO_LINE_DIRECTION_OUT;
370e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
371e42615ecSMatti Vaittinen 	}
372e42615ecSMatti Vaittinen 
373e42615ecSMatti Vaittinen 	if (gc->reg_dir_out) {
374e42615ecSMatti Vaittinen 		if (gc->read_reg(gc->reg_dir_out) & bgpio_line2mask(gc, gpio))
375e42615ecSMatti Vaittinen 			return GPIO_LINE_DIRECTION_OUT;
376e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
377e42615ecSMatti Vaittinen 	}
378e42615ecSMatti Vaittinen 
379f69e00bdSLinus Walleij 	if (gc->reg_dir_in)
380e42615ecSMatti Vaittinen 		if (!(gc->read_reg(gc->reg_dir_in) & bgpio_line2mask(gc, gpio)))
381e42615ecSMatti Vaittinen 			return GPIO_LINE_DIRECTION_OUT;
382f69e00bdSLinus Walleij 
383e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_IN;
384e9f4d569SChristian Lamparter }
385e9f4d569SChristian Lamparter 
386d19d2de6SChuanhong Guo static void bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
387e9f4d569SChristian Lamparter {
388e9f4d569SChristian Lamparter 	unsigned long flags;
389e9f4d569SChristian Lamparter 
390*3c938cc5SSchspa Shi 	raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
391e9f4d569SChristian Lamparter 
392d799a4deSLinus Walleij 	gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
393f69e00bdSLinus Walleij 
394f69e00bdSLinus Walleij 	if (gc->reg_dir_in)
395f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
396f69e00bdSLinus Walleij 	if (gc->reg_dir_out)
397f69e00bdSLinus Walleij 		gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
398e9f4d569SChristian Lamparter 
399*3c938cc5SSchspa Shi 	raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
400d19d2de6SChuanhong Guo }
401e9f4d569SChristian Lamparter 
402d19d2de6SChuanhong Guo static int bgpio_dir_out_dir_first(struct gpio_chip *gc, unsigned int gpio,
403d19d2de6SChuanhong Guo 				   int val)
404d19d2de6SChuanhong Guo {
405d19d2de6SChuanhong Guo 	bgpio_dir_out(gc, gpio, val);
406d19d2de6SChuanhong Guo 	gc->set(gc, gpio, val);
407d19d2de6SChuanhong Guo 	return 0;
408d19d2de6SChuanhong Guo }
409d19d2de6SChuanhong Guo 
410d19d2de6SChuanhong Guo static int bgpio_dir_out_val_first(struct gpio_chip *gc, unsigned int gpio,
411d19d2de6SChuanhong Guo 				   int val)
412d19d2de6SChuanhong Guo {
413d19d2de6SChuanhong Guo 	gc->set(gc, gpio, val);
414d19d2de6SChuanhong Guo 	bgpio_dir_out(gc, gpio, val);
415e9f4d569SChristian Lamparter 	return 0;
416e9f4d569SChristian Lamparter }
417e9f4d569SChristian Lamparter 
418e9f4d569SChristian Lamparter static int bgpio_setup_accessors(struct device *dev,
419e9f4d569SChristian Lamparter 				 struct gpio_chip *gc,
420e9f4d569SChristian Lamparter 				 bool byte_be)
421e9f4d569SChristian Lamparter {
422e9f4d569SChristian Lamparter 
423e9f4d569SChristian Lamparter 	switch (gc->bgpio_bits) {
424e9f4d569SChristian Lamparter 	case 8:
425e9f4d569SChristian Lamparter 		gc->read_reg	= bgpio_read8;
426e9f4d569SChristian Lamparter 		gc->write_reg	= bgpio_write8;
427e9f4d569SChristian Lamparter 		break;
428e9f4d569SChristian Lamparter 	case 16:
429e9f4d569SChristian Lamparter 		if (byte_be) {
430e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read16be;
431e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write16be;
432e9f4d569SChristian Lamparter 		} else {
433e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read16;
434e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write16;
435e9f4d569SChristian Lamparter 		}
436e9f4d569SChristian Lamparter 		break;
437e9f4d569SChristian Lamparter 	case 32:
438e9f4d569SChristian Lamparter 		if (byte_be) {
439e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read32be;
440e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write32be;
441e9f4d569SChristian Lamparter 		} else {
442e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read32;
443e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write32;
444e9f4d569SChristian Lamparter 		}
445e9f4d569SChristian Lamparter 		break;
446e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64
447e9f4d569SChristian Lamparter 	case 64:
448e9f4d569SChristian Lamparter 		if (byte_be) {
449e9f4d569SChristian Lamparter 			dev_err(dev,
450e9f4d569SChristian Lamparter 				"64 bit big endian byte order unsupported\n");
451e9f4d569SChristian Lamparter 			return -EINVAL;
452e9f4d569SChristian Lamparter 		} else {
453e9f4d569SChristian Lamparter 			gc->read_reg	= bgpio_read64;
454e9f4d569SChristian Lamparter 			gc->write_reg	= bgpio_write64;
455e9f4d569SChristian Lamparter 		}
456e9f4d569SChristian Lamparter 		break;
457e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */
458e9f4d569SChristian Lamparter 	default:
459e9f4d569SChristian Lamparter 		dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits);
460e9f4d569SChristian Lamparter 		return -EINVAL;
461e9f4d569SChristian Lamparter 	}
462e9f4d569SChristian Lamparter 
463e9f4d569SChristian Lamparter 	return 0;
464e9f4d569SChristian Lamparter }
465e9f4d569SChristian Lamparter 
466e9f4d569SChristian Lamparter /*
467e9f4d569SChristian Lamparter  * Create the device and allocate the resources.  For setting GPIO's there are
468e9f4d569SChristian Lamparter  * three supported configurations:
469e9f4d569SChristian Lamparter  *
470e9f4d569SChristian Lamparter  *	- single input/output register resource (named "dat").
471e9f4d569SChristian Lamparter  *	- set/clear pair (named "set" and "clr").
472e9f4d569SChristian Lamparter  *	- single output register resource and single input resource ("set" and
473e9f4d569SChristian Lamparter  *	dat").
474e9f4d569SChristian Lamparter  *
475e9f4d569SChristian Lamparter  * For the single output register, this drives a 1 by setting a bit and a zero
476e9f4d569SChristian Lamparter  * by clearing a bit.  For the set clr pair, this drives a 1 by setting a bit
477e9f4d569SChristian Lamparter  * in the set register and clears it by setting a bit in the clear register.
478e9f4d569SChristian Lamparter  * The configuration is detected by which resources are present.
479e9f4d569SChristian Lamparter  *
480e9f4d569SChristian Lamparter  * For setting the GPIO direction, there are three supported configurations:
481e9f4d569SChristian Lamparter  *
482e9f4d569SChristian Lamparter  *	- simple bidirection GPIO that requires no configuration.
483e9f4d569SChristian Lamparter  *	- an output direction register (named "dirout") where a 1 bit
484e9f4d569SChristian Lamparter  *	indicates the GPIO is an output.
485e9f4d569SChristian Lamparter  *	- an input direction register (named "dirin") where a 1 bit indicates
486e9f4d569SChristian Lamparter  *	the GPIO is an input.
487e9f4d569SChristian Lamparter  */
488e9f4d569SChristian Lamparter static int bgpio_setup_io(struct gpio_chip *gc,
489e9f4d569SChristian Lamparter 			  void __iomem *dat,
490e9f4d569SChristian Lamparter 			  void __iomem *set,
491e9f4d569SChristian Lamparter 			  void __iomem *clr,
492e9f4d569SChristian Lamparter 			  unsigned long flags)
493e9f4d569SChristian Lamparter {
494e9f4d569SChristian Lamparter 
495e9f4d569SChristian Lamparter 	gc->reg_dat = dat;
496e9f4d569SChristian Lamparter 	if (!gc->reg_dat)
497e9f4d569SChristian Lamparter 		return -EINVAL;
498e9f4d569SChristian Lamparter 
499e9f4d569SChristian Lamparter 	if (set && clr) {
500e9f4d569SChristian Lamparter 		gc->reg_set = set;
501e9f4d569SChristian Lamparter 		gc->reg_clr = clr;
502e9f4d569SChristian Lamparter 		gc->set = bgpio_set_with_clear;
503e9f4d569SChristian Lamparter 		gc->set_multiple = bgpio_set_multiple_with_clear;
504e9f4d569SChristian Lamparter 	} else if (set && !clr) {
505e9f4d569SChristian Lamparter 		gc->reg_set = set;
506e9f4d569SChristian Lamparter 		gc->set = bgpio_set_set;
507e9f4d569SChristian Lamparter 		gc->set_multiple = bgpio_set_multiple_set;
508e9f4d569SChristian Lamparter 	} else if (flags & BGPIOF_NO_OUTPUT) {
509e9f4d569SChristian Lamparter 		gc->set = bgpio_set_none;
510e9f4d569SChristian Lamparter 		gc->set_multiple = NULL;
511e9f4d569SChristian Lamparter 	} else {
512e9f4d569SChristian Lamparter 		gc->set = bgpio_set;
513e9f4d569SChristian Lamparter 		gc->set_multiple = bgpio_set_multiple;
514e9f4d569SChristian Lamparter 	}
515e9f4d569SChristian Lamparter 
516e9f4d569SChristian Lamparter 	if (!(flags & BGPIOF_UNREADABLE_REG_SET) &&
51780057cb4SLinus Walleij 	    (flags & BGPIOF_READ_OUTPUT_REG_SET)) {
518e9f4d569SChristian Lamparter 		gc->get = bgpio_get_set;
51980057cb4SLinus Walleij 		if (!gc->be_bits)
52080057cb4SLinus Walleij 			gc->get_multiple = bgpio_get_set_multiple;
52180057cb4SLinus Walleij 		/*
52280057cb4SLinus Walleij 		 * We deliberately avoid assigning the ->get_multiple() call
52380057cb4SLinus Walleij 		 * for big endian mirrored registers which are ALSO reflecting
52480057cb4SLinus Walleij 		 * their value in the set register when used as output. It is
52580057cb4SLinus Walleij 		 * simply too much complexity, let the GPIO core fall back to
52680057cb4SLinus Walleij 		 * reading each line individually in that fringe case.
52780057cb4SLinus Walleij 		 */
52880057cb4SLinus Walleij 	} else {
529e9f4d569SChristian Lamparter 		gc->get = bgpio_get;
53080057cb4SLinus Walleij 		if (gc->be_bits)
53180057cb4SLinus Walleij 			gc->get_multiple = bgpio_get_multiple_be;
53280057cb4SLinus Walleij 		else
53380057cb4SLinus Walleij 			gc->get_multiple = bgpio_get_multiple;
53480057cb4SLinus Walleij 	}
535e9f4d569SChristian Lamparter 
536e9f4d569SChristian Lamparter 	return 0;
537e9f4d569SChristian Lamparter }
538e9f4d569SChristian Lamparter 
539e9f4d569SChristian Lamparter static int bgpio_setup_direction(struct gpio_chip *gc,
540e9f4d569SChristian Lamparter 				 void __iomem *dirout,
541e9f4d569SChristian Lamparter 				 void __iomem *dirin,
542e9f4d569SChristian Lamparter 				 unsigned long flags)
543e9f4d569SChristian Lamparter {
544f69e00bdSLinus Walleij 	if (dirout || dirin) {
545f69e00bdSLinus Walleij 		gc->reg_dir_out = dirout;
546f69e00bdSLinus Walleij 		gc->reg_dir_in = dirin;
547d19d2de6SChuanhong Guo 		if (flags & BGPIOF_NO_SET_ON_INPUT)
548d19d2de6SChuanhong Guo 			gc->direction_output = bgpio_dir_out_dir_first;
549d19d2de6SChuanhong Guo 		else
550d19d2de6SChuanhong Guo 			gc->direction_output = bgpio_dir_out_val_first;
551e9f4d569SChristian Lamparter 		gc->direction_input = bgpio_dir_in;
552e9f4d569SChristian Lamparter 		gc->get_direction = bgpio_get_dir;
553e9f4d569SChristian Lamparter 	} else {
554e9f4d569SChristian Lamparter 		if (flags & BGPIOF_NO_OUTPUT)
555e9f4d569SChristian Lamparter 			gc->direction_output = bgpio_dir_out_err;
556e9f4d569SChristian Lamparter 		else
557e9f4d569SChristian Lamparter 			gc->direction_output = bgpio_simple_dir_out;
558e9f4d569SChristian Lamparter 		gc->direction_input = bgpio_simple_dir_in;
559e9f4d569SChristian Lamparter 	}
560e9f4d569SChristian Lamparter 
561e9f4d569SChristian Lamparter 	return 0;
562e9f4d569SChristian Lamparter }
563e9f4d569SChristian Lamparter 
564e9f4d569SChristian Lamparter static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
565e9f4d569SChristian Lamparter {
566e9f4d569SChristian Lamparter 	if (gpio_pin < chip->ngpio)
567e9f4d569SChristian Lamparter 		return 0;
568e9f4d569SChristian Lamparter 
569e9f4d569SChristian Lamparter 	return -EINVAL;
570e9f4d569SChristian Lamparter }
571e9f4d569SChristian Lamparter 
572d799a4deSLinus Walleij /**
573d799a4deSLinus Walleij  * bgpio_init() - Initialize generic GPIO accessor functions
574d799a4deSLinus Walleij  * @gc: the GPIO chip to set up
575d799a4deSLinus Walleij  * @dev: the parent device of the new GPIO chip (compulsory)
576d799a4deSLinus Walleij  * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4
577d799a4deSLinus Walleij  * @dat: MMIO address for the register to READ the value of the GPIO lines, it
578d799a4deSLinus Walleij  *	is expected that a 1 in the corresponding bit in this register means the
579d799a4deSLinus Walleij  *	line is asserted
580d799a4deSLinus Walleij  * @set: MMIO address for the register to SET the value of the GPIO lines, it is
581d799a4deSLinus Walleij  *	expected that we write the line with 1 in this register to drive the GPIO line
582d799a4deSLinus Walleij  *	high.
583d799a4deSLinus Walleij  * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is
584d799a4deSLinus Walleij  *	expected that we write the line with 1 in this register to drive the GPIO line
585d799a4deSLinus Walleij  *	low. It is allowed to leave this address as NULL, in that case the SET register
586d799a4deSLinus Walleij  *	will be assumed to also clear the GPIO lines, by actively writing the line
587d799a4deSLinus Walleij  *	with 0.
588d799a4deSLinus Walleij  * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed
589d799a4deSLinus Walleij  *	that setting a line to 1 in this register will turn that line into an
590d799a4deSLinus Walleij  *	output line. Conversely, setting the line to 0 will turn that line into
591f69e00bdSLinus Walleij  *	an input.
592d799a4deSLinus Walleij  * @dirin: MMIO address for the register to set this line as INPUT. It is assumed
593d799a4deSLinus Walleij  *	that setting a line to 1 in this register will turn that line into an
594d799a4deSLinus Walleij  *	input line. Conversely, setting the line to 0 will turn that line into
595f69e00bdSLinus Walleij  *	an output.
596d799a4deSLinus Walleij  * @flags: Different flags that will affect the behaviour of the device, such as
597d799a4deSLinus Walleij  *	endianness etc.
598d799a4deSLinus Walleij  */
599e9f4d569SChristian Lamparter int bgpio_init(struct gpio_chip *gc, struct device *dev,
600e9f4d569SChristian Lamparter 	       unsigned long sz, void __iomem *dat, void __iomem *set,
601e9f4d569SChristian Lamparter 	       void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
602e9f4d569SChristian Lamparter 	       unsigned long flags)
603e9f4d569SChristian Lamparter {
604e9f4d569SChristian Lamparter 	int ret;
605e9f4d569SChristian Lamparter 
606e9f4d569SChristian Lamparter 	if (!is_power_of_2(sz))
607e9f4d569SChristian Lamparter 		return -EINVAL;
608e9f4d569SChristian Lamparter 
609e9f4d569SChristian Lamparter 	gc->bgpio_bits = sz * 8;
610e9f4d569SChristian Lamparter 	if (gc->bgpio_bits > BITS_PER_LONG)
611e9f4d569SChristian Lamparter 		return -EINVAL;
612e9f4d569SChristian Lamparter 
613*3c938cc5SSchspa Shi 	raw_spin_lock_init(&gc->bgpio_lock);
614e9f4d569SChristian Lamparter 	gc->parent = dev;
615e9f4d569SChristian Lamparter 	gc->label = dev_name(dev);
616e9f4d569SChristian Lamparter 	gc->base = -1;
617e9f4d569SChristian Lamparter 	gc->ngpio = gc->bgpio_bits;
618e9f4d569SChristian Lamparter 	gc->request = bgpio_request;
61980057cb4SLinus Walleij 	gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN);
620e9f4d569SChristian Lamparter 
621e9f4d569SChristian Lamparter 	ret = bgpio_setup_io(gc, dat, set, clr, flags);
622e9f4d569SChristian Lamparter 	if (ret)
623e9f4d569SChristian Lamparter 		return ret;
624e9f4d569SChristian Lamparter 
62524efd94bSLinus Walleij 	ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
626e9f4d569SChristian Lamparter 	if (ret)
627e9f4d569SChristian Lamparter 		return ret;
628e9f4d569SChristian Lamparter 
629e9f4d569SChristian Lamparter 	ret = bgpio_setup_direction(gc, dirout, dirin, flags);
630e9f4d569SChristian Lamparter 	if (ret)
631e9f4d569SChristian Lamparter 		return ret;
632e9f4d569SChristian Lamparter 
633e9f4d569SChristian Lamparter 	gc->bgpio_data = gc->read_reg(gc->reg_dat);
634e9f4d569SChristian Lamparter 	if (gc->set == bgpio_set_set &&
635e9f4d569SChristian Lamparter 			!(flags & BGPIOF_UNREADABLE_REG_SET))
636e9f4d569SChristian Lamparter 		gc->bgpio_data = gc->read_reg(gc->reg_set);
637f69e00bdSLinus Walleij 
638f69e00bdSLinus Walleij 	if (flags & BGPIOF_UNREADABLE_REG_DIR)
639f69e00bdSLinus Walleij 		gc->bgpio_dir_unreadable = true;
640f69e00bdSLinus Walleij 
641f69e00bdSLinus Walleij 	/*
642f69e00bdSLinus Walleij 	 * Inspect hardware to find initial direction setting.
643f69e00bdSLinus Walleij 	 */
644f69e00bdSLinus Walleij 	if ((gc->reg_dir_out || gc->reg_dir_in) &&
645f69e00bdSLinus Walleij 	    !(flags & BGPIOF_UNREADABLE_REG_DIR)) {
646f69e00bdSLinus Walleij 		if (gc->reg_dir_out)
647f69e00bdSLinus Walleij 			gc->bgpio_dir = gc->read_reg(gc->reg_dir_out);
648f69e00bdSLinus Walleij 		else if (gc->reg_dir_in)
649f69e00bdSLinus Walleij 			gc->bgpio_dir = ~gc->read_reg(gc->reg_dir_in);
650f69e00bdSLinus Walleij 		/*
651f69e00bdSLinus Walleij 		 * If we have two direction registers, synchronise
652f69e00bdSLinus Walleij 		 * input setting to output setting, the library
653f69e00bdSLinus Walleij 		 * can not handle a line being input and output at
654f69e00bdSLinus Walleij 		 * the same time.
655f69e00bdSLinus Walleij 		 */
656f69e00bdSLinus Walleij 		if (gc->reg_dir_out && gc->reg_dir_in)
657f69e00bdSLinus Walleij 			gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
658f69e00bdSLinus Walleij 	}
659e9f4d569SChristian Lamparter 
660e9f4d569SChristian Lamparter 	return ret;
661e9f4d569SChristian Lamparter }
662e9f4d569SChristian Lamparter EXPORT_SYMBOL_GPL(bgpio_init);
663e9f4d569SChristian Lamparter 
664e9f4d569SChristian Lamparter #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM)
665e9f4d569SChristian Lamparter 
666e9f4d569SChristian Lamparter static void __iomem *bgpio_map(struct platform_device *pdev,
667e9f4d569SChristian Lamparter 			       const char *name,
668e9f4d569SChristian Lamparter 			       resource_size_t sane_sz)
669e9f4d569SChristian Lamparter {
670e9f4d569SChristian Lamparter 	struct resource *r;
671e9f4d569SChristian Lamparter 	resource_size_t sz;
672e9f4d569SChristian Lamparter 
673e9f4d569SChristian Lamparter 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
674e9f4d569SChristian Lamparter 	if (!r)
675e9f4d569SChristian Lamparter 		return NULL;
676e9f4d569SChristian Lamparter 
677e9f4d569SChristian Lamparter 	sz = resource_size(r);
678e9f4d569SChristian Lamparter 	if (sz != sane_sz)
679e9f4d569SChristian Lamparter 		return IOMEM_ERR_PTR(-EINVAL);
680e9f4d569SChristian Lamparter 
681e9f4d569SChristian Lamparter 	return devm_ioremap_resource(&pdev->dev, r);
682e9f4d569SChristian Lamparter }
683e9f4d569SChristian Lamparter 
684e698613aSÁlvaro Fernández Rojas #ifdef CONFIG_OF
685e698613aSÁlvaro Fernández Rojas static const struct of_device_id bgpio_of_match[] = {
68605cc995fSChristian Lamparter 	{ .compatible = "brcm,bcm6345-gpio" },
687c0d30ecfSChristian Lamparter 	{ .compatible = "wd,mbl-gpio" },
688b8c90199SNathan Sullivan 	{ .compatible = "ni,169445-nand-gpio" },
689e698613aSÁlvaro Fernández Rojas 	{ }
690e698613aSÁlvaro Fernández Rojas };
691e698613aSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bgpio_of_match);
692e698613aSÁlvaro Fernández Rojas 
693e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev,
694e698613aSÁlvaro Fernández Rojas 					  unsigned long *flags)
695e698613aSÁlvaro Fernández Rojas {
696e698613aSÁlvaro Fernández Rojas 	struct bgpio_pdata *pdata;
697e698613aSÁlvaro Fernández Rojas 
698e698613aSÁlvaro Fernández Rojas 	if (!of_match_device(bgpio_of_match, &pdev->dev))
699e698613aSÁlvaro Fernández Rojas 		return NULL;
700e698613aSÁlvaro Fernández Rojas 
701e698613aSÁlvaro Fernández Rojas 	pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata),
702e698613aSÁlvaro Fernández Rojas 			     GFP_KERNEL);
703e698613aSÁlvaro Fernández Rojas 	if (!pdata)
704e698613aSÁlvaro Fernández Rojas 		return ERR_PTR(-ENOMEM);
705e698613aSÁlvaro Fernández Rojas 
706e698613aSÁlvaro Fernández Rojas 	pdata->base = -1;
707e698613aSÁlvaro Fernández Rojas 
70805cc995fSChristian Lamparter 	if (of_device_is_big_endian(pdev->dev.of_node))
70905cc995fSChristian Lamparter 		*flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER;
71005cc995fSChristian Lamparter 
711c0d30ecfSChristian Lamparter 	if (of_property_read_bool(pdev->dev.of_node, "no-output"))
712c0d30ecfSChristian Lamparter 		*flags |= BGPIOF_NO_OUTPUT;
713c0d30ecfSChristian Lamparter 
714e698613aSÁlvaro Fernández Rojas 	return pdata;
715e698613aSÁlvaro Fernández Rojas }
716e698613aSÁlvaro Fernández Rojas #else
717e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev,
718e698613aSÁlvaro Fernández Rojas 					  unsigned long *flags)
719e698613aSÁlvaro Fernández Rojas {
720e698613aSÁlvaro Fernández Rojas 	return NULL;
721e698613aSÁlvaro Fernández Rojas }
722e698613aSÁlvaro Fernández Rojas #endif /* CONFIG_OF */
723e698613aSÁlvaro Fernández Rojas 
724e9f4d569SChristian Lamparter static int bgpio_pdev_probe(struct platform_device *pdev)
725e9f4d569SChristian Lamparter {
726e9f4d569SChristian Lamparter 	struct device *dev = &pdev->dev;
727e9f4d569SChristian Lamparter 	struct resource *r;
728e9f4d569SChristian Lamparter 	void __iomem *dat;
729e9f4d569SChristian Lamparter 	void __iomem *set;
730e9f4d569SChristian Lamparter 	void __iomem *clr;
731e9f4d569SChristian Lamparter 	void __iomem *dirout;
732e9f4d569SChristian Lamparter 	void __iomem *dirin;
733e9f4d569SChristian Lamparter 	unsigned long sz;
734e698613aSÁlvaro Fernández Rojas 	unsigned long flags = 0;
735e9f4d569SChristian Lamparter 	int err;
736e9f4d569SChristian Lamparter 	struct gpio_chip *gc;
737e698613aSÁlvaro Fernández Rojas 	struct bgpio_pdata *pdata;
738e698613aSÁlvaro Fernández Rojas 
739e698613aSÁlvaro Fernández Rojas 	pdata = bgpio_parse_dt(pdev, &flags);
740e698613aSÁlvaro Fernández Rojas 	if (IS_ERR(pdata))
741e698613aSÁlvaro Fernández Rojas 		return PTR_ERR(pdata);
742e698613aSÁlvaro Fernández Rojas 
743e698613aSÁlvaro Fernández Rojas 	if (!pdata) {
744e698613aSÁlvaro Fernández Rojas 		pdata = dev_get_platdata(dev);
745e698613aSÁlvaro Fernández Rojas 		flags = pdev->id_entry->driver_data;
746e698613aSÁlvaro Fernández Rojas 	}
747e9f4d569SChristian Lamparter 
748e9f4d569SChristian Lamparter 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
749e9f4d569SChristian Lamparter 	if (!r)
750e9f4d569SChristian Lamparter 		return -EINVAL;
751e9f4d569SChristian Lamparter 
752e9f4d569SChristian Lamparter 	sz = resource_size(r);
753e9f4d569SChristian Lamparter 
754e9f4d569SChristian Lamparter 	dat = bgpio_map(pdev, "dat", sz);
755e9f4d569SChristian Lamparter 	if (IS_ERR(dat))
756e9f4d569SChristian Lamparter 		return PTR_ERR(dat);
757e9f4d569SChristian Lamparter 
758e9f4d569SChristian Lamparter 	set = bgpio_map(pdev, "set", sz);
759e9f4d569SChristian Lamparter 	if (IS_ERR(set))
760e9f4d569SChristian Lamparter 		return PTR_ERR(set);
761e9f4d569SChristian Lamparter 
762e9f4d569SChristian Lamparter 	clr = bgpio_map(pdev, "clr", sz);
763e9f4d569SChristian Lamparter 	if (IS_ERR(clr))
764e9f4d569SChristian Lamparter 		return PTR_ERR(clr);
765e9f4d569SChristian Lamparter 
766e9f4d569SChristian Lamparter 	dirout = bgpio_map(pdev, "dirout", sz);
767e9f4d569SChristian Lamparter 	if (IS_ERR(dirout))
768e9f4d569SChristian Lamparter 		return PTR_ERR(dirout);
769e9f4d569SChristian Lamparter 
770e9f4d569SChristian Lamparter 	dirin = bgpio_map(pdev, "dirin", sz);
771e9f4d569SChristian Lamparter 	if (IS_ERR(dirin))
772e9f4d569SChristian Lamparter 		return PTR_ERR(dirin);
773e9f4d569SChristian Lamparter 
774e9f4d569SChristian Lamparter 	gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
775e9f4d569SChristian Lamparter 	if (!gc)
776e9f4d569SChristian Lamparter 		return -ENOMEM;
777e9f4d569SChristian Lamparter 
778e9f4d569SChristian Lamparter 	err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags);
779e9f4d569SChristian Lamparter 	if (err)
780e9f4d569SChristian Lamparter 		return err;
781e9f4d569SChristian Lamparter 
782e9f4d569SChristian Lamparter 	if (pdata) {
783e9f4d569SChristian Lamparter 		if (pdata->label)
784e9f4d569SChristian Lamparter 			gc->label = pdata->label;
785e9f4d569SChristian Lamparter 		gc->base = pdata->base;
786e9f4d569SChristian Lamparter 		if (pdata->ngpio > 0)
787e9f4d569SChristian Lamparter 			gc->ngpio = pdata->ngpio;
788e9f4d569SChristian Lamparter 	}
789e9f4d569SChristian Lamparter 
790e9f4d569SChristian Lamparter 	platform_set_drvdata(pdev, gc);
791e9f4d569SChristian Lamparter 
792e9f4d569SChristian Lamparter 	return devm_gpiochip_add_data(&pdev->dev, gc, NULL);
793e9f4d569SChristian Lamparter }
794e9f4d569SChristian Lamparter 
795e9f4d569SChristian Lamparter static const struct platform_device_id bgpio_id_table[] = {
796e9f4d569SChristian Lamparter 	{
797e9f4d569SChristian Lamparter 		.name		= "basic-mmio-gpio",
798e9f4d569SChristian Lamparter 		.driver_data	= 0,
799e9f4d569SChristian Lamparter 	}, {
800e9f4d569SChristian Lamparter 		.name		= "basic-mmio-gpio-be",
801e9f4d569SChristian Lamparter 		.driver_data	= BGPIOF_BIG_ENDIAN,
802e9f4d569SChristian Lamparter 	},
803e9f4d569SChristian Lamparter 	{ }
804e9f4d569SChristian Lamparter };
805e9f4d569SChristian Lamparter MODULE_DEVICE_TABLE(platform, bgpio_id_table);
806e9f4d569SChristian Lamparter 
807e9f4d569SChristian Lamparter static struct platform_driver bgpio_driver = {
808e9f4d569SChristian Lamparter 	.driver = {
809e9f4d569SChristian Lamparter 		.name = "basic-mmio-gpio",
810e698613aSÁlvaro Fernández Rojas 		.of_match_table = of_match_ptr(bgpio_of_match),
811e9f4d569SChristian Lamparter 	},
812e9f4d569SChristian Lamparter 	.id_table = bgpio_id_table,
813e9f4d569SChristian Lamparter 	.probe = bgpio_pdev_probe,
814e9f4d569SChristian Lamparter };
815e9f4d569SChristian Lamparter 
816e9f4d569SChristian Lamparter module_platform_driver(bgpio_driver);
817e9f4d569SChristian Lamparter 
818e9f4d569SChristian Lamparter #endif /* CONFIG_GPIO_GENERIC_PLATFORM */
819e9f4d569SChristian Lamparter 
820e9f4d569SChristian Lamparter MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
821e9f4d569SChristian Lamparter MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
822e9f4d569SChristian Lamparter MODULE_LICENSE("GPL");
823