1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic driver for memory-mapped GPIO controllers.
5 * Copyright 2008 MontaVista Software, Inc.
8 * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`.......
10 * ..The simplest form of a GPIO controller that the driver supports is``
11 * `.just a single "data" register, where GPIO state can be read and/or `
12 * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.```````
15 _/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,...
16 __________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO .
17 o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`
18 `....trivial..'~`.```.```
20 * .```````~~~~`..`.``.``.
21 * . The driver supports `... ,..```.`~~~```````````````....````.``,,
22 * . big-endian notation, just`. .. A bit more sophisticated controllers ,
23 * . register the device with -be`. .with a pair of set/clear-bit registers ,
24 * `.. suffix. ```~~`````....`.` . affecting the data register and the .`
25 * ``.`.``...``` ```.. output pins are also supported.`
26 * ^^ `````.`````````.,``~``~``~~``````
28 * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`..
29 * .. The expectation is that in at least some cases . ,-~~~-,
30 * .this will be used with roll-your-own ASIC/FPGA .` \ /
31 * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ /
32 * ..````````......``````````` \o_
36 * ...`````~~`.....``.`..........``````.`.``.```........``.
37 * ` 8, 16, 32 and 64 bits registers are supported, and``.
38 * . the number of GPIOs is determined by the width of ~
39 * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~
43 #include <linux/bitops.h>
44 #include <linux/compiler.h>
45 #include <linux/err.h>
46 #include <linux/init.h>
48 #include <linux/ioport.h>
49 #include <linux/log2.h>
50 #include <linux/mod_devicetable.h>
51 #include <linux/module.h>
52 #include <linux/platform_device.h>
53 #include <linux/property.h>
54 #include <linux/slab.h>
55 #include <linux/spinlock.h>
56 #include <linux/types.h>
58 #include <linux/gpio/driver.h>
62 static void bgpio_write8(void __iomem *reg, unsigned long data)
67 static unsigned long bgpio_read8(void __iomem *reg)
72 static void bgpio_write16(void __iomem *reg, unsigned long data)
77 static unsigned long bgpio_read16(void __iomem *reg)
82 static void bgpio_write32(void __iomem *reg, unsigned long data)
87 static unsigned long bgpio_read32(void __iomem *reg)
92 #if BITS_PER_LONG >= 64
93 static void bgpio_write64(void __iomem *reg, unsigned long data)
98 static unsigned long bgpio_read64(void __iomem *reg)
102 #endif /* BITS_PER_LONG >= 64 */
104 static void bgpio_write16be(void __iomem *reg, unsigned long data)
106 iowrite16be(data, reg);
109 static unsigned long bgpio_read16be(void __iomem *reg)
111 return ioread16be(reg);
114 static void bgpio_write32be(void __iomem *reg, unsigned long data)
116 iowrite32be(data, reg);
119 static unsigned long bgpio_read32be(void __iomem *reg)
121 return ioread32be(reg);
124 static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line)
127 return BIT(gc->bgpio_bits - 1 - line);
131 static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
133 unsigned long pinmask = bgpio_line2mask(gc, gpio);
134 bool dir = !!(gc->bgpio_dir & pinmask);
137 return !!(gc->read_reg(gc->reg_set) & pinmask);
139 return !!(gc->read_reg(gc->reg_dat) & pinmask);
143 * This assumes that the bits in the GPIO register are in native endianness.
144 * We only assign the function pointer if we have that.
146 static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
149 unsigned long get_mask = 0;
150 unsigned long set_mask = 0;
152 /* Make sure we first clear any bits that are zero when we read the register */
155 set_mask = *mask & gc->bgpio_dir;
156 get_mask = *mask & ~gc->bgpio_dir;
159 *bits |= gc->read_reg(gc->reg_set) & set_mask;
161 *bits |= gc->read_reg(gc->reg_dat) & get_mask;
166 static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
168 return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio));
172 * This only works if the bits in the GPIO register are in native endianness.
174 static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask,
177 /* Make sure we first clear any bits that are zero when we read the register */
179 *bits |= gc->read_reg(gc->reg_dat) & *mask;
184 * With big endian mirrored bit order it becomes more tedious.
186 static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask,
189 unsigned long readmask = 0;
193 /* Make sure we first clear any bits that are zero when we read the register */
196 /* Create a mirrored mask */
197 for_each_set_bit(bit, mask, gc->ngpio)
198 readmask |= bgpio_line2mask(gc, bit);
200 /* Read the register */
201 val = gc->read_reg(gc->reg_dat) & readmask;
204 * Mirror the result into the "bits" result, this will give line 0
205 * in bit 0 ... line 31 in bit 31 for a 32bit register.
207 for_each_set_bit(bit, &val, gc->ngpio)
208 *bits |= bgpio_line2mask(gc, bit);
213 static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
217 static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
219 unsigned long mask = bgpio_line2mask(gc, gpio);
222 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
225 gc->bgpio_data |= mask;
227 gc->bgpio_data &= ~mask;
229 gc->write_reg(gc->reg_dat, gc->bgpio_data);
231 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
234 static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
237 unsigned long mask = bgpio_line2mask(gc, gpio);
240 gc->write_reg(gc->reg_set, mask);
242 gc->write_reg(gc->reg_clr, mask);
245 static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
247 unsigned long mask = bgpio_line2mask(gc, gpio);
250 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
253 gc->bgpio_data |= mask;
255 gc->bgpio_data &= ~mask;
257 gc->write_reg(gc->reg_set, gc->bgpio_data);
259 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
262 static void bgpio_multiple_get_masks(struct gpio_chip *gc,
263 unsigned long *mask, unsigned long *bits,
264 unsigned long *set_mask,
265 unsigned long *clear_mask)
272 for_each_set_bit(i, mask, gc->bgpio_bits) {
273 if (test_bit(i, bits))
274 *set_mask |= bgpio_line2mask(gc, i);
276 *clear_mask |= bgpio_line2mask(gc, i);
280 static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
286 unsigned long set_mask, clear_mask;
288 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
290 bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
292 gc->bgpio_data |= set_mask;
293 gc->bgpio_data &= ~clear_mask;
295 gc->write_reg(reg, gc->bgpio_data);
297 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
300 static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
303 bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat);
306 static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
309 bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set);
312 static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
316 unsigned long set_mask, clear_mask;
318 bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
321 gc->write_reg(gc->reg_set, set_mask);
323 gc->write_reg(gc->reg_clr, clear_mask);
326 static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
331 static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,
337 static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
340 gc->set(gc, gpio, val);
345 static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
349 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
351 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
354 gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
356 gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
358 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
363 static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
365 /* Return 0 if output, 1 if input */
366 if (gc->bgpio_dir_unreadable) {
367 if (gc->bgpio_dir & bgpio_line2mask(gc, gpio))
368 return GPIO_LINE_DIRECTION_OUT;
369 return GPIO_LINE_DIRECTION_IN;
372 if (gc->reg_dir_out) {
373 if (gc->read_reg(gc->reg_dir_out) & bgpio_line2mask(gc, gpio))
374 return GPIO_LINE_DIRECTION_OUT;
375 return GPIO_LINE_DIRECTION_IN;
379 if (!(gc->read_reg(gc->reg_dir_in) & bgpio_line2mask(gc, gpio)))
380 return GPIO_LINE_DIRECTION_OUT;
382 return GPIO_LINE_DIRECTION_IN;
385 static void bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
389 raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
391 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
394 gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
396 gc->write_reg(gc->reg_dir_out, gc->bgpio_dir);
398 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
401 static int bgpio_dir_out_dir_first(struct gpio_chip *gc, unsigned int gpio,
404 bgpio_dir_out(gc, gpio, val);
405 gc->set(gc, gpio, val);
409 static int bgpio_dir_out_val_first(struct gpio_chip *gc, unsigned int gpio,
412 gc->set(gc, gpio, val);
413 bgpio_dir_out(gc, gpio, val);
417 static int bgpio_setup_accessors(struct device *dev,
418 struct gpio_chip *gc,
422 switch (gc->bgpio_bits) {
424 gc->read_reg = bgpio_read8;
425 gc->write_reg = bgpio_write8;
429 gc->read_reg = bgpio_read16be;
430 gc->write_reg = bgpio_write16be;
432 gc->read_reg = bgpio_read16;
433 gc->write_reg = bgpio_write16;
438 gc->read_reg = bgpio_read32be;
439 gc->write_reg = bgpio_write32be;
441 gc->read_reg = bgpio_read32;
442 gc->write_reg = bgpio_write32;
445 #if BITS_PER_LONG >= 64
449 "64 bit big endian byte order unsupported\n");
452 gc->read_reg = bgpio_read64;
453 gc->write_reg = bgpio_write64;
456 #endif /* BITS_PER_LONG >= 64 */
458 dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits);
466 * Create the device and allocate the resources. For setting GPIO's there are
467 * three supported configurations:
469 * - single input/output register resource (named "dat").
470 * - set/clear pair (named "set" and "clr").
471 * - single output register resource and single input resource ("set" and
474 * For the single output register, this drives a 1 by setting a bit and a zero
475 * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
476 * in the set register and clears it by setting a bit in the clear register.
477 * The configuration is detected by which resources are present.
479 * For setting the GPIO direction, there are three supported configurations:
481 * - simple bidirection GPIO that requires no configuration.
482 * - an output direction register (named "dirout") where a 1 bit
483 * indicates the GPIO is an output.
484 * - an input direction register (named "dirin") where a 1 bit indicates
485 * the GPIO is an input.
487 static int bgpio_setup_io(struct gpio_chip *gc,
501 gc->set = bgpio_set_with_clear;
502 gc->set_multiple = bgpio_set_multiple_with_clear;
503 } else if (set && !clr) {
505 gc->set = bgpio_set_set;
506 gc->set_multiple = bgpio_set_multiple_set;
507 } else if (flags & BGPIOF_NO_OUTPUT) {
508 gc->set = bgpio_set_none;
509 gc->set_multiple = NULL;
512 gc->set_multiple = bgpio_set_multiple;
515 if (!(flags & BGPIOF_UNREADABLE_REG_SET) &&
516 (flags & BGPIOF_READ_OUTPUT_REG_SET)) {
517 gc->get = bgpio_get_set;
519 gc->get_multiple = bgpio_get_set_multiple;
521 * We deliberately avoid assigning the ->get_multiple() call
522 * for big endian mirrored registers which are ALSO reflecting
523 * their value in the set register when used as output. It is
524 * simply too much complexity, let the GPIO core fall back to
525 * reading each line individually in that fringe case.
530 gc->get_multiple = bgpio_get_multiple_be;
532 gc->get_multiple = bgpio_get_multiple;
538 static int bgpio_setup_direction(struct gpio_chip *gc,
539 void __iomem *dirout,
543 if (dirout || dirin) {
544 gc->reg_dir_out = dirout;
545 gc->reg_dir_in = dirin;
546 if (flags & BGPIOF_NO_SET_ON_INPUT)
547 gc->direction_output = bgpio_dir_out_dir_first;
549 gc->direction_output = bgpio_dir_out_val_first;
550 gc->direction_input = bgpio_dir_in;
551 gc->get_direction = bgpio_get_dir;
553 if (flags & BGPIOF_NO_OUTPUT)
554 gc->direction_output = bgpio_dir_out_err;
556 gc->direction_output = bgpio_simple_dir_out;
557 gc->direction_input = bgpio_simple_dir_in;
563 static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
565 if (gpio_pin < chip->ngpio)
572 * bgpio_init() - Initialize generic GPIO accessor functions
573 * @gc: the GPIO chip to set up
574 * @dev: the parent device of the new GPIO chip (compulsory)
575 * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4
576 * @dat: MMIO address for the register to READ the value of the GPIO lines, it
577 * is expected that a 1 in the corresponding bit in this register means the
579 * @set: MMIO address for the register to SET the value of the GPIO lines, it is
580 * expected that we write the line with 1 in this register to drive the GPIO line
582 * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is
583 * expected that we write the line with 1 in this register to drive the GPIO line
584 * low. It is allowed to leave this address as NULL, in that case the SET register
585 * will be assumed to also clear the GPIO lines, by actively writing the line
587 * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed
588 * that setting a line to 1 in this register will turn that line into an
589 * output line. Conversely, setting the line to 0 will turn that line into
591 * @dirin: MMIO address for the register to set this line as INPUT. It is assumed
592 * that setting a line to 1 in this register will turn that line into an
593 * input line. Conversely, setting the line to 0 will turn that line into
595 * @flags: Different flags that will affect the behaviour of the device, such as
598 int bgpio_init(struct gpio_chip *gc, struct device *dev,
599 unsigned long sz, void __iomem *dat, void __iomem *set,
600 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
605 if (!is_power_of_2(sz))
608 gc->bgpio_bits = sz * 8;
609 if (gc->bgpio_bits > BITS_PER_LONG)
612 raw_spin_lock_init(&gc->bgpio_lock);
614 gc->label = dev_name(dev);
616 gc->request = bgpio_request;
617 gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN);
619 ret = gpiochip_get_ngpios(gc, dev);
621 gc->ngpio = gc->bgpio_bits;
623 gc->bgpio_bits = roundup_pow_of_two(round_up(gc->ngpio, 8));
625 ret = bgpio_setup_io(gc, dat, set, clr, flags);
629 ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
633 ret = bgpio_setup_direction(gc, dirout, dirin, flags);
637 gc->bgpio_data = gc->read_reg(gc->reg_dat);
638 if (gc->set == bgpio_set_set &&
639 !(flags & BGPIOF_UNREADABLE_REG_SET))
640 gc->bgpio_data = gc->read_reg(gc->reg_set);
642 if (flags & BGPIOF_UNREADABLE_REG_DIR)
643 gc->bgpio_dir_unreadable = true;
646 * Inspect hardware to find initial direction setting.
648 if ((gc->reg_dir_out || gc->reg_dir_in) &&
649 !(flags & BGPIOF_UNREADABLE_REG_DIR)) {
651 gc->bgpio_dir = gc->read_reg(gc->reg_dir_out);
652 else if (gc->reg_dir_in)
653 gc->bgpio_dir = ~gc->read_reg(gc->reg_dir_in);
655 * If we have two direction registers, synchronise
656 * input setting to output setting, the library
657 * can not handle a line being input and output at
660 if (gc->reg_dir_out && gc->reg_dir_in)
661 gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir);
666 EXPORT_SYMBOL_GPL(bgpio_init);
668 #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM)
670 static void __iomem *bgpio_map(struct platform_device *pdev,
672 resource_size_t sane_sz)
677 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
681 sz = resource_size(r);
683 return IOMEM_ERR_PTR(-EINVAL);
685 return devm_ioremap_resource(&pdev->dev, r);
688 static const struct of_device_id bgpio_of_match[] = {
689 { .compatible = "brcm,bcm6345-gpio" },
690 { .compatible = "wd,mbl-gpio" },
691 { .compatible = "ni,169445-nand-gpio" },
694 MODULE_DEVICE_TABLE(of, bgpio_of_match);
696 static struct bgpio_pdata *bgpio_parse_fw(struct device *dev, unsigned long *flags)
698 struct bgpio_pdata *pdata;
700 if (!dev_fwnode(dev))
703 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
705 return ERR_PTR(-ENOMEM);
709 if (device_is_big_endian(dev))
710 *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER;
712 if (device_property_read_bool(dev, "no-output"))
713 *flags |= BGPIOF_NO_OUTPUT;
718 static int bgpio_pdev_probe(struct platform_device *pdev)
720 struct device *dev = &pdev->dev;
725 void __iomem *dirout;
728 unsigned long flags = 0;
730 struct gpio_chip *gc;
731 struct bgpio_pdata *pdata;
733 pdata = bgpio_parse_fw(dev, &flags);
735 return PTR_ERR(pdata);
738 pdata = dev_get_platdata(dev);
739 flags = pdev->id_entry->driver_data;
742 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
746 sz = resource_size(r);
748 dat = bgpio_map(pdev, "dat", sz);
752 set = bgpio_map(pdev, "set", sz);
756 clr = bgpio_map(pdev, "clr", sz);
760 dirout = bgpio_map(pdev, "dirout", sz);
762 return PTR_ERR(dirout);
764 dirin = bgpio_map(pdev, "dirin", sz);
766 return PTR_ERR(dirin);
768 gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
772 err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags);
778 gc->label = pdata->label;
779 gc->base = pdata->base;
780 if (pdata->ngpio > 0)
781 gc->ngpio = pdata->ngpio;
784 platform_set_drvdata(pdev, gc);
786 return devm_gpiochip_add_data(&pdev->dev, gc, NULL);
789 static const struct platform_device_id bgpio_id_table[] = {
791 .name = "basic-mmio-gpio",
794 .name = "basic-mmio-gpio-be",
795 .driver_data = BGPIOF_BIG_ENDIAN,
799 MODULE_DEVICE_TABLE(platform, bgpio_id_table);
801 static struct platform_driver bgpio_driver = {
803 .name = "basic-mmio-gpio",
804 .of_match_table = bgpio_of_match,
806 .id_table = bgpio_id_table,
807 .probe = bgpio_pdev_probe,
810 module_platform_driver(bgpio_driver);
812 #endif /* CONFIG_GPIO_GENERIC_PLATFORM */
814 MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
816 MODULE_LICENSE("GPL");