2 * Generic driver for memory-mapped GPIO controllers.
4 * Copyright 2008 MontaVista Software, Inc.
5 * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`.......
14 * ..The simplest form of a GPIO controller that the driver supports is``
15 * `.just a single "data" register, where GPIO state can be read and/or `
16 * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.```````
19 _/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,...
20 __________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO .
21 o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`
22 `....trivial..'~`.```.```
24 * .```````~~~~`..`.``.``.
25 * . The driver supports `... ,..```.`~~~```````````````....````.``,,
26 * . big-endian notation, just`. .. A bit more sophisticated controllers ,
27 * . register the device with -be`. .with a pair of set/clear-bit registers ,
28 * `.. suffix. ```~~`````....`.` . affecting the data register and the .`
29 * ``.`.``...``` ```.. output pins are also supported.`
30 * ^^ `````.`````````.,``~``~``~~``````
32 * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`..
33 * .. The expectation is that in at least some cases . ,-~~~-,
34 * .this will be used with roll-your-own ASIC/FPGA .` \ /
35 * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ /
36 * ..````````......``````````` \o_
40 * ...`````~~`.....``.`..........``````.`.``.```........``.
41 * ` 8, 16, 32 and 64 bits registers are supported, and``.
42 * . the number of GPIOs is determined by the width of ~
43 * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~
47 #include <linux/init.h>
48 #include <linux/err.h>
49 #include <linux/bug.h>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/spinlock.h>
53 #include <linux/compiler.h>
54 #include <linux/types.h>
55 #include <linux/errno.h>
56 #include <linux/log2.h>
57 #include <linux/ioport.h>
59 #include <linux/gpio/driver.h>
60 #include <linux/slab.h>
61 #include <linux/bitops.h>
62 #include <linux/platform_device.h>
63 #include <linux/mod_devicetable.h>
65 static void bgpio_write8(void __iomem *reg, unsigned long data)
70 static unsigned long bgpio_read8(void __iomem *reg)
75 static void bgpio_write16(void __iomem *reg, unsigned long data)
80 static unsigned long bgpio_read16(void __iomem *reg)
85 static void bgpio_write32(void __iomem *reg, unsigned long data)
90 static unsigned long bgpio_read32(void __iomem *reg)
95 #if BITS_PER_LONG >= 64
96 static void bgpio_write64(void __iomem *reg, unsigned long data)
101 static unsigned long bgpio_read64(void __iomem *reg)
105 #endif /* BITS_PER_LONG >= 64 */
107 static void bgpio_write16be(void __iomem *reg, unsigned long data)
109 iowrite16be(data, reg);
112 static unsigned long bgpio_read16be(void __iomem *reg)
114 return ioread16be(reg);
117 static void bgpio_write32be(void __iomem *reg, unsigned long data)
119 iowrite32be(data, reg);
122 static unsigned long bgpio_read32be(void __iomem *reg)
124 return ioread32be(reg);
127 static unsigned long bgpio_pin2mask(struct gpio_chip *gc, unsigned int pin)
132 static unsigned long bgpio_pin2mask_be(struct gpio_chip *gc,
135 return BIT(gc->bgpio_bits - 1 - pin);
138 static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
140 unsigned long pinmask = gc->pin2mask(gc, gpio);
142 if (gc->bgpio_dir & pinmask)
143 return !!(gc->read_reg(gc->reg_set) & pinmask);
145 return !!(gc->read_reg(gc->reg_dat) & pinmask);
148 static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
150 return !!(gc->read_reg(gc->reg_dat) & gc->pin2mask(gc, gpio));
153 static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
157 static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
159 unsigned long mask = gc->pin2mask(gc, gpio);
162 spin_lock_irqsave(&gc->bgpio_lock, flags);
165 gc->bgpio_data |= mask;
167 gc->bgpio_data &= ~mask;
169 gc->write_reg(gc->reg_dat, gc->bgpio_data);
171 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
174 static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
177 unsigned long mask = gc->pin2mask(gc, gpio);
180 gc->write_reg(gc->reg_set, mask);
182 gc->write_reg(gc->reg_clr, mask);
185 static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
187 unsigned long mask = gc->pin2mask(gc, gpio);
190 spin_lock_irqsave(&gc->bgpio_lock, flags);
193 gc->bgpio_data |= mask;
195 gc->bgpio_data &= ~mask;
197 gc->write_reg(gc->reg_set, gc->bgpio_data);
199 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
202 static void bgpio_multiple_get_masks(struct gpio_chip *gc,
203 unsigned long *mask, unsigned long *bits,
204 unsigned long *set_mask,
205 unsigned long *clear_mask)
212 for (i = 0; i < gc->bgpio_bits; i++) {
215 if (__test_and_clear_bit(i, mask)) {
216 if (test_bit(i, bits))
217 *set_mask |= gc->pin2mask(gc, i);
219 *clear_mask |= gc->pin2mask(gc, i);
224 static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
230 unsigned long set_mask, clear_mask;
232 spin_lock_irqsave(&gc->bgpio_lock, flags);
234 bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
236 gc->bgpio_data |= set_mask;
237 gc->bgpio_data &= ~clear_mask;
239 gc->write_reg(reg, gc->bgpio_data);
241 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
244 static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
247 bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat);
250 static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
253 bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set);
256 static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
260 unsigned long set_mask, clear_mask;
262 bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
265 gc->write_reg(gc->reg_set, set_mask);
267 gc->write_reg(gc->reg_clr, clear_mask);
270 static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
275 static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,
281 static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
284 gc->set(gc, gpio, val);
289 static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
293 spin_lock_irqsave(&gc->bgpio_lock, flags);
295 gc->bgpio_dir &= ~gc->pin2mask(gc, gpio);
296 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
298 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
303 static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
305 /* Return 0 if output, 1 of input */
306 return !(gc->read_reg(gc->reg_dir) & gc->pin2mask(gc, gpio));
309 static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
313 gc->set(gc, gpio, val);
315 spin_lock_irqsave(&gc->bgpio_lock, flags);
317 gc->bgpio_dir |= gc->pin2mask(gc, gpio);
318 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
320 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
325 static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio)
329 spin_lock_irqsave(&gc->bgpio_lock, flags);
331 gc->bgpio_dir |= gc->pin2mask(gc, gpio);
332 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
334 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
339 static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
343 gc->set(gc, gpio, val);
345 spin_lock_irqsave(&gc->bgpio_lock, flags);
347 gc->bgpio_dir &= ~gc->pin2mask(gc, gpio);
348 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
350 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
355 static int bgpio_get_dir_inv(struct gpio_chip *gc, unsigned int gpio)
357 /* Return 0 if output, 1 if input */
358 return !!(gc->read_reg(gc->reg_dir) & gc->pin2mask(gc, gpio));
361 static int bgpio_setup_accessors(struct device *dev,
362 struct gpio_chip *gc,
367 switch (gc->bgpio_bits) {
369 gc->read_reg = bgpio_read8;
370 gc->write_reg = bgpio_write8;
374 gc->read_reg = bgpio_read16be;
375 gc->write_reg = bgpio_write16be;
377 gc->read_reg = bgpio_read16;
378 gc->write_reg = bgpio_write16;
383 gc->read_reg = bgpio_read32be;
384 gc->write_reg = bgpio_write32be;
386 gc->read_reg = bgpio_read32;
387 gc->write_reg = bgpio_write32;
390 #if BITS_PER_LONG >= 64
394 "64 bit big endian byte order unsupported\n");
397 gc->read_reg = bgpio_read64;
398 gc->write_reg = bgpio_write64;
401 #endif /* BITS_PER_LONG >= 64 */
403 dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits);
407 gc->pin2mask = bit_be ? bgpio_pin2mask_be : bgpio_pin2mask;
413 * Create the device and allocate the resources. For setting GPIO's there are
414 * three supported configurations:
416 * - single input/output register resource (named "dat").
417 * - set/clear pair (named "set" and "clr").
418 * - single output register resource and single input resource ("set" and
421 * For the single output register, this drives a 1 by setting a bit and a zero
422 * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
423 * in the set register and clears it by setting a bit in the clear register.
424 * The configuration is detected by which resources are present.
426 * For setting the GPIO direction, there are three supported configurations:
428 * - simple bidirection GPIO that requires no configuration.
429 * - an output direction register (named "dirout") where a 1 bit
430 * indicates the GPIO is an output.
431 * - an input direction register (named "dirin") where a 1 bit indicates
432 * the GPIO is an input.
434 static int bgpio_setup_io(struct gpio_chip *gc,
448 gc->set = bgpio_set_with_clear;
449 gc->set_multiple = bgpio_set_multiple_with_clear;
450 } else if (set && !clr) {
452 gc->set = bgpio_set_set;
453 gc->set_multiple = bgpio_set_multiple_set;
454 } else if (flags & BGPIOF_NO_OUTPUT) {
455 gc->set = bgpio_set_none;
456 gc->set_multiple = NULL;
459 gc->set_multiple = bgpio_set_multiple;
462 if (!(flags & BGPIOF_UNREADABLE_REG_SET) &&
463 (flags & BGPIOF_READ_OUTPUT_REG_SET))
464 gc->get = bgpio_get_set;
471 static int bgpio_setup_direction(struct gpio_chip *gc,
472 void __iomem *dirout,
476 if (dirout && dirin) {
479 gc->reg_dir = dirout;
480 gc->direction_output = bgpio_dir_out;
481 gc->direction_input = bgpio_dir_in;
482 gc->get_direction = bgpio_get_dir;
485 gc->direction_output = bgpio_dir_out_inv;
486 gc->direction_input = bgpio_dir_in_inv;
487 gc->get_direction = bgpio_get_dir_inv;
489 if (flags & BGPIOF_NO_OUTPUT)
490 gc->direction_output = bgpio_dir_out_err;
492 gc->direction_output = bgpio_simple_dir_out;
493 gc->direction_input = bgpio_simple_dir_in;
499 static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
501 if (gpio_pin < chip->ngpio)
507 int bgpio_init(struct gpio_chip *gc, struct device *dev,
508 unsigned long sz, void __iomem *dat, void __iomem *set,
509 void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
514 if (!is_power_of_2(sz))
517 gc->bgpio_bits = sz * 8;
518 if (gc->bgpio_bits > BITS_PER_LONG)
521 spin_lock_init(&gc->bgpio_lock);
523 gc->label = dev_name(dev);
525 gc->ngpio = gc->bgpio_bits;
526 gc->request = bgpio_request;
528 ret = bgpio_setup_io(gc, dat, set, clr, flags);
532 ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN,
533 flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
537 ret = bgpio_setup_direction(gc, dirout, dirin, flags);
541 gc->bgpio_data = gc->read_reg(gc->reg_dat);
542 if (gc->set == bgpio_set_set &&
543 !(flags & BGPIOF_UNREADABLE_REG_SET))
544 gc->bgpio_data = gc->read_reg(gc->reg_set);
545 if (gc->reg_dir && !(flags & BGPIOF_UNREADABLE_REG_DIR))
546 gc->bgpio_dir = gc->read_reg(gc->reg_dir);
550 EXPORT_SYMBOL_GPL(bgpio_init);
552 #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM)
554 static void __iomem *bgpio_map(struct platform_device *pdev,
556 resource_size_t sane_sz)
561 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
565 sz = resource_size(r);
567 return IOMEM_ERR_PTR(-EINVAL);
569 return devm_ioremap_resource(&pdev->dev, r);
572 static int bgpio_pdev_probe(struct platform_device *pdev)
574 struct device *dev = &pdev->dev;
579 void __iomem *dirout;
582 unsigned long flags = pdev->id_entry->driver_data;
584 struct gpio_chip *gc;
585 struct bgpio_pdata *pdata = dev_get_platdata(dev);
587 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
591 sz = resource_size(r);
593 dat = bgpio_map(pdev, "dat", sz);
597 set = bgpio_map(pdev, "set", sz);
601 clr = bgpio_map(pdev, "clr", sz);
605 dirout = bgpio_map(pdev, "dirout", sz);
607 return PTR_ERR(dirout);
609 dirin = bgpio_map(pdev, "dirin", sz);
611 return PTR_ERR(dirin);
613 gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
617 err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags);
623 gc->label = pdata->label;
624 gc->base = pdata->base;
625 if (pdata->ngpio > 0)
626 gc->ngpio = pdata->ngpio;
629 platform_set_drvdata(pdev, gc);
631 return devm_gpiochip_add_data(&pdev->dev, gc, NULL);
634 static const struct platform_device_id bgpio_id_table[] = {
636 .name = "basic-mmio-gpio",
639 .name = "basic-mmio-gpio-be",
640 .driver_data = BGPIOF_BIG_ENDIAN,
644 MODULE_DEVICE_TABLE(platform, bgpio_id_table);
646 static struct platform_driver bgpio_driver = {
648 .name = "basic-mmio-gpio",
650 .id_table = bgpio_id_table,
651 .probe = bgpio_pdev_probe,
654 module_platform_driver(bgpio_driver);
656 #endif /* CONFIG_GPIO_GENERIC_PLATFORM */
658 MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
659 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
660 MODULE_LICENSE("GPL");