Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Coldfire generic GPIO support. |
| 4 | * |
| 5 | * (C) Copyright 2009, Steven King <sfking@fdwdc.com> |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 10 | #include <linux/init.h> |
Kay Sievers | a6ec0c0 | 2011-12-21 15:09:54 -0800 | [diff] [blame] | 11 | #include <linux/device.h> |
Linus Walleij | 372ea26 | 2019-08-21 09:09:23 +0200 | [diff] [blame] | 12 | #include <linux/gpio/driver.h> |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 13 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 14 | #include <linux/io.h> |
| 15 | #include <asm/coldfire.h> |
| 16 | #include <asm/mcfsim.h> |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 17 | #include <asm/mcfgpio.h> |
| 18 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 19 | int __mcfgpio_get_value(unsigned gpio) |
| 20 | { |
| 21 | return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio); |
| 22 | } |
| 23 | EXPORT_SYMBOL(__mcfgpio_get_value); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 24 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 25 | void __mcfgpio_set_value(unsigned gpio, int value) |
| 26 | { |
| 27 | if (gpio < MCFGPIO_SCR_START) { |
| 28 | unsigned long flags; |
| 29 | MCFGPIO_PORTTYPE data; |
| 30 | |
| 31 | local_irq_save(flags); |
| 32 | data = mcfgpio_read(__mcfgpio_podr(gpio)); |
| 33 | if (value) |
| 34 | data |= mcfgpio_bit(gpio); |
| 35 | else |
| 36 | data &= ~mcfgpio_bit(gpio); |
| 37 | mcfgpio_write(data, __mcfgpio_podr(gpio)); |
| 38 | local_irq_restore(flags); |
| 39 | } else { |
| 40 | if (value) |
| 41 | mcfgpio_write(mcfgpio_bit(gpio), |
| 42 | MCFGPIO_SETR_PORT(gpio)); |
| 43 | else |
| 44 | mcfgpio_write(~mcfgpio_bit(gpio), |
| 45 | MCFGPIO_CLRR_PORT(gpio)); |
| 46 | } |
| 47 | } |
| 48 | EXPORT_SYMBOL(__mcfgpio_set_value); |
| 49 | |
| 50 | int __mcfgpio_direction_input(unsigned gpio) |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 51 | { |
| 52 | unsigned long flags; |
| 53 | MCFGPIO_PORTTYPE dir; |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 54 | |
| 55 | local_irq_save(flags); |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 56 | dir = mcfgpio_read(__mcfgpio_pddr(gpio)); |
| 57 | dir &= ~mcfgpio_bit(gpio); |
| 58 | mcfgpio_write(dir, __mcfgpio_pddr(gpio)); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 59 | local_irq_restore(flags); |
| 60 | |
| 61 | return 0; |
| 62 | } |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL(__mcfgpio_direction_input); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 64 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 65 | int __mcfgpio_direction_output(unsigned gpio, int value) |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 66 | { |
| 67 | unsigned long flags; |
| 68 | MCFGPIO_PORTTYPE data; |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 69 | |
| 70 | local_irq_save(flags); |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 71 | data = mcfgpio_read(__mcfgpio_pddr(gpio)); |
Steven King | ffca5af | 2014-05-21 16:00:31 -0700 | [diff] [blame] | 72 | data |= mcfgpio_bit(gpio); |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 73 | mcfgpio_write(data, __mcfgpio_pddr(gpio)); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 74 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 75 | /* now set the data to output */ |
| 76 | if (gpio < MCFGPIO_SCR_START) { |
| 77 | data = mcfgpio_read(__mcfgpio_podr(gpio)); |
| 78 | if (value) |
| 79 | data |= mcfgpio_bit(gpio); |
| 80 | else |
| 81 | data &= ~mcfgpio_bit(gpio); |
| 82 | mcfgpio_write(data, __mcfgpio_podr(gpio)); |
| 83 | } else { |
| 84 | if (value) |
| 85 | mcfgpio_write(mcfgpio_bit(gpio), |
| 86 | MCFGPIO_SETR_PORT(gpio)); |
| 87 | else |
| 88 | mcfgpio_write(~mcfgpio_bit(gpio), |
| 89 | MCFGPIO_CLRR_PORT(gpio)); |
| 90 | } |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 91 | local_irq_restore(flags); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 92 | return 0; |
| 93 | } |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 94 | EXPORT_SYMBOL(__mcfgpio_direction_output); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 95 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 96 | int __mcfgpio_request(unsigned gpio) |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 97 | { |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | EXPORT_SYMBOL(__mcfgpio_request); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 101 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 102 | void __mcfgpio_free(unsigned gpio) |
| 103 | { |
| 104 | __mcfgpio_direction_input(gpio); |
| 105 | } |
| 106 | EXPORT_SYMBOL(__mcfgpio_free); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 107 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 108 | #ifdef CONFIG_GPIOLIB |
| 109 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 110 | static int mcfgpio_direction_input(struct gpio_chip *chip, unsigned offset) |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 111 | { |
| 112 | return __mcfgpio_direction_input(offset); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 115 | static int mcfgpio_get_value(struct gpio_chip *chip, unsigned offset) |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 116 | { |
Linus Walleij | 4693c24 | 2015-12-22 15:39:05 +0100 | [diff] [blame] | 117 | return !!__mcfgpio_get_value(offset); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 120 | static int mcfgpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 121 | int value) |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 122 | { |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 123 | return __mcfgpio_direction_output(offset, value); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 126 | static void mcfgpio_set_value(struct gpio_chip *chip, unsigned offset, |
| 127 | int value) |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 128 | { |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 129 | __mcfgpio_set_value(offset, value); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 132 | static int mcfgpio_request(struct gpio_chip *chip, unsigned offset) |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 133 | { |
| 134 | return __mcfgpio_request(offset); |
| 135 | } |
| 136 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 137 | static void mcfgpio_free(struct gpio_chip *chip, unsigned offset) |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 138 | { |
| 139 | __mcfgpio_free(offset); |
| 140 | } |
| 141 | |
Steven King | ec9f848 | 2014-05-21 16:00:32 -0700 | [diff] [blame] | 142 | static int mcfgpio_to_irq(struct gpio_chip *chip, unsigned offset) |
Steven King | 60fc65f | 2014-05-21 16:00:30 -0700 | [diff] [blame] | 143 | { |
| 144 | #if defined(MCFGPIO_IRQ_MIN) |
| 145 | if ((offset >= MCFGPIO_IRQ_MIN) && (offset < MCFGPIO_IRQ_MAX)) |
| 146 | #else |
| 147 | if (offset < MCFGPIO_IRQ_MAX) |
| 148 | #endif |
| 149 | return MCFGPIO_IRQ_VECBASE + offset; |
| 150 | else |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 154 | static struct gpio_chip mcfgpio_chip = { |
| 155 | .label = "mcfgpio", |
| 156 | .request = mcfgpio_request, |
| 157 | .free = mcfgpio_free, |
| 158 | .direction_input = mcfgpio_direction_input, |
| 159 | .direction_output = mcfgpio_direction_output, |
| 160 | .get = mcfgpio_get_value, |
| 161 | .set = mcfgpio_set_value, |
Steven King | 60fc65f | 2014-05-21 16:00:30 -0700 | [diff] [blame] | 162 | .to_irq = mcfgpio_to_irq, |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 163 | .base = 0, |
| 164 | .ngpio = MCFGPIO_PIN_MAX, |
| 165 | }; |
Greg Ungerer | f23c144 | 2012-04-17 13:25:38 +1000 | [diff] [blame] | 166 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 167 | static int __init mcfgpio_sysinit(void) |
| 168 | { |
Greg Ungerer | 2763ee6 | 2016-04-07 05:26:26 +1000 | [diff] [blame] | 169 | return gpiochip_add_data(&mcfgpio_chip, NULL); |
sfking@fdwdc.com | af39bb8 | 2009-06-19 18:11:00 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Steven King | eac5794 | 2012-05-21 13:10:19 -0700 | [diff] [blame] | 172 | core_initcall(mcfgpio_sysinit); |
| 173 | #endif |