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