4 * Copyright (C) 2007-2012 ST-Ericsson AB
5 * License terms: GNU General Public License (GPL) version 2
6 * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
7 * Author: Linus Walleij <linus.walleij@linaro.org>
8 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio.h>
19 #include <linux/slab.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include "pinctrl-coh901.h"
24 #define U300_GPIO_PORT_STRIDE (0x30)
26 * Control Register 32bit (R/W)
27 * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores
28 * gives the number of GPIO pins.
29 * bit 8-2 (mask 0x000001FC) contains the core version ID.
31 #define U300_GPIO_CR (0x00)
32 #define U300_GPIO_CR_SYNC_SEL_ENABLE (0x00000002UL)
33 #define U300_GPIO_CR_BLOCK_CLKRQ_ENABLE (0x00000001UL)
34 #define U300_GPIO_PXPDIR (0x04)
35 #define U300_GPIO_PXPDOR (0x08)
36 #define U300_GPIO_PXPCR (0x0C)
37 #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK (0x0000FFFFUL)
38 #define U300_GPIO_PXPCR_PIN_MODE_MASK (0x00000003UL)
39 #define U300_GPIO_PXPCR_PIN_MODE_SHIFT (0x00000002UL)
40 #define U300_GPIO_PXPCR_PIN_MODE_INPUT (0x00000000UL)
41 #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL (0x00000001UL)
42 #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN (0x00000002UL)
43 #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE (0x00000003UL)
44 #define U300_GPIO_PXPER (0x10)
45 #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK (0x000000FFUL)
46 #define U300_GPIO_PXPER_PULL_UP_DISABLE (0x00000001UL)
47 #define U300_GPIO_PXIEV (0x14)
48 #define U300_GPIO_PXIEN (0x18)
49 #define U300_GPIO_PXIFR (0x1C)
50 #define U300_GPIO_PXICR (0x20)
51 #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK (0x000000FFUL)
52 #define U300_GPIO_PXICR_IRQ_CONFIG_MASK (0x00000001UL)
53 #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE (0x00000000UL)
54 #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE (0x00000001UL)
56 /* 8 bits per port, no version has more than 7 ports */
57 #define U300_GPIO_NUM_PORTS 7
58 #define U300_GPIO_PINS_PER_PORT 8
59 #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * U300_GPIO_NUM_PORTS)
61 struct u300_gpio_port {
62 struct u300_gpio *gpio;
70 struct gpio_chip chip;
71 struct u300_gpio_port ports[U300_GPIO_NUM_PORTS];
76 /* Register offsets */
87 * Macro to expand to read a specific register found in the "gpio"
88 * struct. It requires the struct u300_gpio *gpio variable to exist in
89 * its context. It calculates the port offset from the given pin
90 * offset, muliplies by the port stride and adds the register offset
91 * so it provides a pointer to the desired register.
93 #define U300_PIN_REG(pin, reg) \
94 (gpio->base + (pin >> 3) * gpio->stride + gpio->reg)
97 * Provides a bitmask for a specific gpio pin inside an 8-bit GPIO
100 #define U300_PIN_BIT(pin) \
103 struct u300_gpio_confdata {
109 #define U300_FLOATING_INPUT { \
110 .bias_mode = PIN_CONFIG_BIAS_HIGH_IMPEDANCE, \
114 #define U300_PULL_UP_INPUT { \
115 .bias_mode = PIN_CONFIG_BIAS_PULL_UP, \
119 #define U300_OUTPUT_LOW { \
124 #define U300_OUTPUT_HIGH { \
129 /* Initial configuration */
130 static const struct u300_gpio_confdata __initconst
131 bs335_gpio_config[U300_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
132 /* Port 0, pins 0-7 */
143 /* Port 1, pins 0-7 */
154 /* Port 2, pins 0-7 */
165 /* Port 3, pins 0-7 */
176 /* Port 4, pins 0-7 */
187 /* Port 5, pins 0-7 */
198 /* Port 6, pind 0-7 */
211 static int u300_gpio_get(struct gpio_chip *chip, unsigned offset)
213 struct u300_gpio *gpio = gpiochip_get_data(chip);
215 return !!(readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset));
218 static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
220 struct u300_gpio *gpio = gpiochip_get_data(chip);
224 local_irq_save(flags);
226 val = readl(U300_PIN_REG(offset, dor));
228 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
230 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
232 local_irq_restore(flags);
235 static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
237 struct u300_gpio *gpio = gpiochip_get_data(chip);
241 local_irq_save(flags);
242 val = readl(U300_PIN_REG(offset, pcr));
243 /* Mask out this pin, note 2 bits per setting */
244 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
245 writel(val, U300_PIN_REG(offset, pcr));
246 local_irq_restore(flags);
250 static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
253 struct u300_gpio *gpio = gpiochip_get_data(chip);
258 local_irq_save(flags);
259 val = readl(U300_PIN_REG(offset, pcr));
261 * Drive mode must be set by the special mode set function, set
262 * push/pull mode by default if no mode has been selected.
264 oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK <<
265 ((offset & 0x07) << 1));
266 /* mode = 0 means input, else some mode is already set */
268 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK <<
269 ((offset & 0x07) << 1));
270 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
271 << ((offset & 0x07) << 1));
272 writel(val, U300_PIN_REG(offset, pcr));
274 u300_gpio_set(chip, offset, value);
275 local_irq_restore(flags);
279 /* Returning -EINVAL means "supported but not available" */
280 int u300_gpio_config_get(struct gpio_chip *chip,
282 unsigned long *config)
284 struct u300_gpio *gpio = gpiochip_get_data(chip);
285 enum pin_config_param param = (enum pin_config_param) *config;
289 /* One bit per pin, clamp to bool range */
290 biasmode = !!(readl(U300_PIN_REG(offset, per)) & U300_PIN_BIT(offset));
292 /* Mask out the two bits for this pin and shift to bits 0,1 */
293 drmode = readl(U300_PIN_REG(offset, pcr));
294 drmode &= (U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
295 drmode >>= ((offset & 0x07) << 1);
298 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
305 case PIN_CONFIG_BIAS_PULL_UP:
312 case PIN_CONFIG_DRIVE_PUSH_PULL:
314 if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL)
319 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
321 if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN)
326 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
328 if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE)
339 int u300_gpio_config_set(struct gpio_chip *chip, unsigned offset,
340 enum pin_config_param param)
342 struct u300_gpio *gpio = gpiochip_get_data(chip);
346 local_irq_save(flags);
348 case PIN_CONFIG_BIAS_DISABLE:
349 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
350 val = readl(U300_PIN_REG(offset, per));
351 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
353 case PIN_CONFIG_BIAS_PULL_UP:
354 val = readl(U300_PIN_REG(offset, per));
355 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
357 case PIN_CONFIG_DRIVE_PUSH_PULL:
358 val = readl(U300_PIN_REG(offset, pcr));
359 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
360 << ((offset & 0x07) << 1));
361 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
362 << ((offset & 0x07) << 1));
363 writel(val, U300_PIN_REG(offset, pcr));
365 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
366 val = readl(U300_PIN_REG(offset, pcr));
367 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
368 << ((offset & 0x07) << 1));
369 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
370 << ((offset & 0x07) << 1));
371 writel(val, U300_PIN_REG(offset, pcr));
373 case PIN_CONFIG_DRIVE_OPEN_SOURCE:
374 val = readl(U300_PIN_REG(offset, pcr));
375 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
376 << ((offset & 0x07) << 1));
377 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
378 << ((offset & 0x07) << 1));
379 writel(val, U300_PIN_REG(offset, pcr));
382 local_irq_restore(flags);
383 dev_err(gpio->dev, "illegal configuration requested\n");
386 local_irq_restore(flags);
390 static struct gpio_chip u300_gpio_chip = {
391 .label = "u300-gpio-chip",
392 .owner = THIS_MODULE,
393 .request = gpiochip_generic_request,
394 .free = gpiochip_generic_free,
395 .get = u300_gpio_get,
396 .set = u300_gpio_set,
397 .direction_input = u300_gpio_direction_input,
398 .direction_output = u300_gpio_direction_output,
401 static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
405 val = readl(U300_PIN_REG(offset, icr));
406 /* Set mode depending on state */
407 if (u300_gpio_get(&gpio->chip, offset)) {
408 /* High now, let's trigger on falling edge next then */
409 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
410 dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n",
413 /* Low now, let's trigger on rising edge next then */
414 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
415 dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n",
420 static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
422 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
423 struct u300_gpio *gpio = gpiochip_get_data(chip);
424 struct u300_gpio_port *port = &gpio->ports[d->hwirq >> 3];
425 int offset = d->hwirq;
428 if ((trigger & IRQF_TRIGGER_RISING) &&
429 (trigger & IRQF_TRIGGER_FALLING)) {
431 * The GPIO block can only trigger on falling OR rising edges,
432 * not both. So we need to toggle the mode whenever the pin
433 * goes from one state to the other with a special state flag
436 "trigger on both rising and falling edge on pin %d\n",
438 port->toggle_edge_mode |= U300_PIN_BIT(offset);
439 u300_toggle_trigger(gpio, offset);
440 } else if (trigger & IRQF_TRIGGER_RISING) {
441 dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n",
443 val = readl(U300_PIN_REG(offset, icr));
444 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
445 port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
446 } else if (trigger & IRQF_TRIGGER_FALLING) {
447 dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n",
449 val = readl(U300_PIN_REG(offset, icr));
450 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
451 port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
457 static void u300_gpio_irq_enable(struct irq_data *d)
459 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
460 struct u300_gpio *gpio = gpiochip_get_data(chip);
461 struct u300_gpio_port *port = &gpio->ports[d->hwirq >> 3];
462 int offset = d->hwirq;
466 dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n",
467 d->hwirq, port->name, offset);
468 local_irq_save(flags);
469 val = readl(U300_PIN_REG(offset, ien));
470 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
471 local_irq_restore(flags);
474 static void u300_gpio_irq_disable(struct irq_data *d)
476 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
477 struct u300_gpio *gpio = gpiochip_get_data(chip);
478 int offset = d->hwirq;
482 local_irq_save(flags);
483 val = readl(U300_PIN_REG(offset, ien));
484 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
485 local_irq_restore(flags);
488 static struct irq_chip u300_gpio_irqchip = {
489 .name = "u300-gpio-irqchip",
490 .irq_enable = u300_gpio_irq_enable,
491 .irq_disable = u300_gpio_irq_disable,
492 .irq_set_type = u300_gpio_irq_type,
495 static void u300_gpio_irq_handler(struct irq_desc *desc)
497 unsigned int irq = irq_desc_get_irq(desc);
498 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
499 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
500 struct u300_gpio *gpio = gpiochip_get_data(chip);
501 struct u300_gpio_port *port = &gpio->ports[irq - chip->base];
502 int pinoffset = port->number << 3; /* get the right stride */
505 chained_irq_enter(parent_chip, desc);
507 /* Read event register */
508 val = readl(U300_PIN_REG(pinoffset, iev));
509 /* Mask relevant bits */
510 val &= 0xFFU; /* 8 bits per port */
511 /* ACK IRQ (clear event) */
512 writel(val, U300_PIN_REG(pinoffset, iev));
514 /* Call IRQ handler */
518 for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
519 int offset = pinoffset + irqoffset;
520 int pin_irq = irq_find_mapping(chip->irqdomain, offset);
522 dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
524 generic_handle_irq(pin_irq);
526 * Triggering IRQ on both rising and falling edge
529 if (port->toggle_edge_mode & U300_PIN_BIT(offset))
530 u300_toggle_trigger(gpio, offset);
534 chained_irq_exit(parent_chip, desc);
537 static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
539 const struct u300_gpio_confdata *conf)
541 /* Set mode: input or output */
543 u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
545 /* Deactivate bias mode for output */
546 u300_gpio_config_set(&gpio->chip, offset,
547 PIN_CONFIG_BIAS_HIGH_IMPEDANCE);
549 /* Set drive mode for output */
550 u300_gpio_config_set(&gpio->chip, offset,
551 PIN_CONFIG_DRIVE_PUSH_PULL);
553 dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n",
554 offset, conf->outval);
556 u300_gpio_direction_input(&gpio->chip, offset);
558 /* Always set output low on input pins */
559 u300_gpio_set(&gpio->chip, offset, 0);
561 /* Set bias mode for input */
562 u300_gpio_config_set(&gpio->chip, offset, conf->bias_mode);
564 dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n",
565 offset, conf->bias_mode);
569 static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio)
573 /* Write default config and values to all pins */
574 for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
575 for (j = 0; j < 8; j++) {
576 const struct u300_gpio_confdata *conf;
577 int offset = (i*8) + j;
579 conf = &bs335_gpio_config[i][j];
580 u300_gpio_init_pin(gpio, offset, conf);
586 * Here we map a GPIO in the local gpio_chip pin space to a pin in
587 * the local pinctrl pin space. The pin controller used is
590 struct coh901_pinpair {
592 unsigned int pin_base;
595 #define COH901_PINRANGE(a, b) { .offset = a, .pin_base = b }
597 static struct coh901_pinpair coh901_pintable[] = {
598 COH901_PINRANGE(10, 426),
599 COH901_PINRANGE(11, 180),
600 COH901_PINRANGE(12, 165), /* MS/MMC card insertion */
601 COH901_PINRANGE(13, 179),
602 COH901_PINRANGE(14, 178),
603 COH901_PINRANGE(16, 194),
604 COH901_PINRANGE(17, 193),
605 COH901_PINRANGE(18, 192),
606 COH901_PINRANGE(19, 191),
607 COH901_PINRANGE(20, 186),
608 COH901_PINRANGE(21, 185),
609 COH901_PINRANGE(22, 184),
610 COH901_PINRANGE(23, 183),
611 COH901_PINRANGE(24, 182),
612 COH901_PINRANGE(25, 181),
615 static int __init u300_gpio_probe(struct platform_device *pdev)
617 struct u300_gpio *gpio;
618 struct resource *memres;
625 gpio = devm_kzalloc(&pdev->dev, sizeof(struct u300_gpio), GFP_KERNEL);
629 gpio->chip = u300_gpio_chip;
630 gpio->chip.ngpio = U300_GPIO_NUM_PORTS * U300_GPIO_PINS_PER_PORT;
631 gpio->chip.parent = &pdev->dev;
633 gpio->dev = &pdev->dev;
635 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
636 gpio->base = devm_ioremap_resource(&pdev->dev, memres);
637 if (IS_ERR(gpio->base))
638 return PTR_ERR(gpio->base);
640 gpio->clk = devm_clk_get(gpio->dev, NULL);
641 if (IS_ERR(gpio->clk)) {
642 err = PTR_ERR(gpio->clk);
643 dev_err(gpio->dev, "could not get GPIO clock\n");
647 err = clk_prepare_enable(gpio->clk);
649 dev_err(gpio->dev, "could not enable GPIO clock\n");
654 "initializing GPIO Controller COH 901 571/3\n");
655 gpio->stride = U300_GPIO_PORT_STRIDE;
656 gpio->pcr = U300_GPIO_PXPCR;
657 gpio->dor = U300_GPIO_PXPDOR;
658 gpio->dir = U300_GPIO_PXPDIR;
659 gpio->per = U300_GPIO_PXPER;
660 gpio->icr = U300_GPIO_PXICR;
661 gpio->ien = U300_GPIO_PXIEN;
662 gpio->iev = U300_GPIO_PXIEV;
663 ifr = U300_GPIO_PXIFR;
665 val = readl(gpio->base + U300_GPIO_CR);
666 dev_info(gpio->dev, "COH901571/3 block version: %d, " \
667 "number of cores: %d totalling %d pins\n",
668 ((val & 0x000001FC) >> 2),
669 ((val & 0x0000FE00) >> 9),
670 ((val & 0x0000FE00) >> 9) * 8);
671 writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE,
672 gpio->base + U300_GPIO_CR);
673 u300_gpio_init_coh901571(gpio);
675 #ifdef CONFIG_OF_GPIO
676 gpio->chip.of_node = pdev->dev.of_node;
678 err = gpiochip_add_data(&gpio->chip, gpio);
680 dev_err(gpio->dev, "unable to add gpiochip: %d\n", err);
684 err = gpiochip_irqchip_add(&gpio->chip,
688 IRQ_TYPE_EDGE_FALLING);
690 dev_err(gpio->dev, "no GPIO irqchip\n");
694 /* Add each port with its IRQ separately */
695 for (portno = 0 ; portno < U300_GPIO_NUM_PORTS; portno++) {
696 struct u300_gpio_port *port = &gpio->ports[portno];
698 snprintf(port->name, 8, "gpio%d", portno);
699 port->number = portno;
702 port->irq = platform_get_irq(pdev, portno);
704 gpiochip_set_chained_irqchip(&gpio->chip,
707 u300_gpio_irq_handler);
709 /* Turns off irq force (test register) for this port */
710 writel(0x0, gpio->base + portno * gpio->stride + ifr);
712 dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno);
715 * Add pinctrl pin ranges, the pin controller must be registered
718 for (i = 0; i < ARRAY_SIZE(coh901_pintable); i++) {
719 struct coh901_pinpair *p = &coh901_pintable[i];
721 err = gpiochip_add_pin_range(&gpio->chip, "pinctrl-u300",
722 p->offset, p->pin_base, 1);
727 platform_set_drvdata(pdev, gpio);
733 gpiochip_remove(&gpio->chip);
735 clk_disable_unprepare(gpio->clk);
736 dev_err(&pdev->dev, "module ERROR:%d\n", err);
740 static int __exit u300_gpio_remove(struct platform_device *pdev)
742 struct u300_gpio *gpio = platform_get_drvdata(pdev);
744 /* Turn off the GPIO block */
745 writel(0x00000000U, gpio->base + U300_GPIO_CR);
747 gpiochip_remove(&gpio->chip);
748 clk_disable_unprepare(gpio->clk);
752 static const struct of_device_id u300_gpio_match[] = {
753 { .compatible = "stericsson,gpio-coh901" },
757 static struct platform_driver u300_gpio_driver = {
760 .of_match_table = u300_gpio_match,
762 .remove = __exit_p(u300_gpio_remove),
765 static int __init u300_gpio_init(void)
767 return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
770 static void __exit u300_gpio_exit(void)
772 platform_driver_unregister(&u300_gpio_driver);
775 arch_initcall(u300_gpio_init);
776 module_exit(u300_gpio_exit);
778 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
779 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
780 MODULE_LICENSE("GPL");