Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/arch/arm/mach-pxa/mfp-pxa2xx.c
0004  *
0005  *  PXA2xx pin mux configuration support
0006  *
0007  *  The GPIOs on PXA2xx can be configured as one of many alternate
0008  *  functions, this is by concept samilar to the MFP configuration
0009  *  on PXA3xx,  what's more important, the low power pin state and
0010  *  wakeup detection are also supported by the same framework.
0011  */
0012 #include <linux/gpio.h>
0013 #include <linux/gpio-pxa.h>
0014 #include <linux/module.h>
0015 #include <linux/kernel.h>
0016 #include <linux/init.h>
0017 #include <linux/io.h>
0018 #include <linux/syscore_ops.h>
0019 #include <linux/soc/pxa/cpu.h>
0020 
0021 #include "pxa2xx-regs.h"
0022 #include "mfp-pxa2xx.h"
0023 
0024 #include "generic.h"
0025 
0026 #define PGSR(x)     __REG2(0x40F00020, (x) << 2)
0027 #define __GAFR(u, x)    __REG2((u) ? 0x40E00058 : 0x40E00054, (x) << 3)
0028 #define GAFR_L(x)   __GAFR(0, x)
0029 #define GAFR_U(x)   __GAFR(1, x)
0030 
0031 #define BANK_OFF(n) (((n) < 3) ? (n) << 2 : 0x100 + (((n) - 3) << 2))
0032 #define GPLR(x)     __REG2(0x40E00000, BANK_OFF((x) >> 5))
0033 #define GPDR(x)     __REG2(0x40E00000, BANK_OFF((x) >> 5) + 0x0c)
0034 #define GPSR(x)     __REG2(0x40E00000, BANK_OFF((x) >> 5) + 0x18)
0035 #define GPCR(x)     __REG2(0x40E00000, BANK_OFF((x) >> 5) + 0x24)
0036 
0037 #define PWER_WE35   (1 << 24)
0038 
0039 struct gpio_desc {
0040     unsigned    valid       : 1;
0041     unsigned    can_wakeup  : 1;
0042     unsigned    keypad_gpio : 1;
0043     unsigned    dir_inverted    : 1;
0044     unsigned int    mask; /* bit mask in PWER or PKWR */
0045     unsigned int    mux_mask; /* bit mask of muxed gpio bits, 0 if no mux */
0046     unsigned long   config;
0047 };
0048 
0049 static struct gpio_desc gpio_desc[MFP_PIN_GPIO127 + 1];
0050 
0051 static unsigned long gpdr_lpm[4];
0052 
0053 static int __mfp_config_gpio(unsigned gpio, unsigned long c)
0054 {
0055     unsigned long gafr, mask = GPIO_bit(gpio);
0056     int bank = gpio_to_bank(gpio);
0057     int uorl = !!(gpio & 0x10); /* GAFRx_U or GAFRx_L ? */
0058     int shft = (gpio & 0xf) << 1;
0059     int fn = MFP_AF(c);
0060     int is_out = (c & MFP_DIR_OUT) ? 1 : 0;
0061 
0062     if (fn > 3)
0063         return -EINVAL;
0064 
0065     /* alternate function and direction at run-time */
0066     gafr = (uorl == 0) ? GAFR_L(bank) : GAFR_U(bank);
0067     gafr = (gafr & ~(0x3 << shft)) | (fn << shft);
0068 
0069     if (uorl == 0)
0070         GAFR_L(bank) = gafr;
0071     else
0072         GAFR_U(bank) = gafr;
0073 
0074     if (is_out ^ gpio_desc[gpio].dir_inverted)
0075         GPDR(gpio) |= mask;
0076     else
0077         GPDR(gpio) &= ~mask;
0078 
0079     /* alternate function and direction at low power mode */
0080     switch (c & MFP_LPM_STATE_MASK) {
0081     case MFP_LPM_DRIVE_HIGH:
0082         PGSR(bank) |= mask;
0083         is_out = 1;
0084         break;
0085     case MFP_LPM_DRIVE_LOW:
0086         PGSR(bank) &= ~mask;
0087         is_out = 1;
0088         break;
0089     case MFP_LPM_INPUT:
0090     case MFP_LPM_DEFAULT:
0091         break;
0092     default:
0093         /* warning and fall through, treat as MFP_LPM_DEFAULT */
0094         pr_warn("%s: GPIO%d: unsupported low power mode\n",
0095             __func__, gpio);
0096         break;
0097     }
0098 
0099     if (is_out ^ gpio_desc[gpio].dir_inverted)
0100         gpdr_lpm[bank] |= mask;
0101     else
0102         gpdr_lpm[bank] &= ~mask;
0103 
0104     /* give early warning if MFP_LPM_CAN_WAKEUP is set on the
0105      * configurations of those pins not able to wakeup
0106      */
0107     if ((c & MFP_LPM_CAN_WAKEUP) && !gpio_desc[gpio].can_wakeup) {
0108         pr_warn("%s: GPIO%d unable to wakeup\n", __func__, gpio);
0109         return -EINVAL;
0110     }
0111 
0112     if ((c & MFP_LPM_CAN_WAKEUP) && is_out) {
0113         pr_warn("%s: output GPIO%d unable to wakeup\n", __func__, gpio);
0114         return -EINVAL;
0115     }
0116 
0117     return 0;
0118 }
0119 
0120 static inline int __mfp_validate(int mfp)
0121 {
0122     int gpio = mfp_to_gpio(mfp);
0123 
0124     if ((mfp > MFP_PIN_GPIO127) || !gpio_desc[gpio].valid) {
0125         pr_warn("%s: GPIO%d is invalid pin\n", __func__, gpio);
0126         return -1;
0127     }
0128 
0129     return gpio;
0130 }
0131 
0132 void pxa2xx_mfp_config(unsigned long *mfp_cfgs, int num)
0133 {
0134     unsigned long flags;
0135     unsigned long *c;
0136     int i, gpio;
0137 
0138     for (i = 0, c = mfp_cfgs; i < num; i++, c++) {
0139 
0140         gpio = __mfp_validate(MFP_PIN(*c));
0141         if (gpio < 0)
0142             continue;
0143 
0144         local_irq_save(flags);
0145 
0146         gpio_desc[gpio].config = *c;
0147         __mfp_config_gpio(gpio, *c);
0148 
0149         local_irq_restore(flags);
0150     }
0151 }
0152 
0153 void pxa2xx_mfp_set_lpm(int mfp, unsigned long lpm)
0154 {
0155     unsigned long flags, c;
0156     int gpio;
0157 
0158     gpio = __mfp_validate(mfp);
0159     if (gpio < 0)
0160         return;
0161 
0162     local_irq_save(flags);
0163 
0164     c = gpio_desc[gpio].config;
0165     c = (c & ~MFP_LPM_STATE_MASK) | lpm;
0166     __mfp_config_gpio(gpio, c);
0167 
0168     local_irq_restore(flags);
0169 }
0170 
0171 int gpio_set_wake(unsigned int gpio, unsigned int on)
0172 {
0173     struct gpio_desc *d;
0174     unsigned long c, mux_taken;
0175 
0176     if (gpio > mfp_to_gpio(MFP_PIN_GPIO127))
0177         return -EINVAL;
0178 
0179     d = &gpio_desc[gpio];
0180     c = d->config;
0181 
0182     if (!d->valid)
0183         return -EINVAL;
0184 
0185     /* Allow keypad GPIOs to wakeup system when
0186      * configured as generic GPIOs.
0187      */
0188     if (d->keypad_gpio && (MFP_AF(d->config) == 0) &&
0189         (d->config & MFP_LPM_CAN_WAKEUP)) {
0190         if (on)
0191             PKWR |= d->mask;
0192         else
0193             PKWR &= ~d->mask;
0194         return 0;
0195     }
0196 
0197     mux_taken = (PWER & d->mux_mask) & (~d->mask);
0198     if (on && mux_taken)
0199         return -EBUSY;
0200 
0201     if (d->can_wakeup && (c & MFP_LPM_CAN_WAKEUP)) {
0202         if (on) {
0203             PWER = (PWER & ~d->mux_mask) | d->mask;
0204 
0205             if (c & MFP_LPM_EDGE_RISE)
0206                 PRER |= d->mask;
0207             else
0208                 PRER &= ~d->mask;
0209 
0210             if (c & MFP_LPM_EDGE_FALL)
0211                 PFER |= d->mask;
0212             else
0213                 PFER &= ~d->mask;
0214         } else {
0215             PWER &= ~d->mask;
0216             PRER &= ~d->mask;
0217             PFER &= ~d->mask;
0218         }
0219     }
0220     return 0;
0221 }
0222 
0223 #ifdef CONFIG_PXA25x
0224 static void __init pxa25x_mfp_init(void)
0225 {
0226     int i;
0227 
0228     /* running before pxa_gpio_probe() */
0229 #ifdef CONFIG_CPU_PXA26x
0230     pxa_last_gpio = 89;
0231 #else
0232     pxa_last_gpio = 84;
0233 #endif
0234     for (i = 0; i <= pxa_last_gpio; i++)
0235         gpio_desc[i].valid = 1;
0236 
0237     for (i = 0; i <= 15; i++) {
0238         gpio_desc[i].can_wakeup = 1;
0239         gpio_desc[i].mask = GPIO_bit(i);
0240     }
0241 
0242     /* PXA26x has additional 4 GPIOs (86/87/88/89) which has the
0243      * direction bit inverted in GPDR2. See PXA26x DM 4.1.1.
0244      */
0245     for (i = 86; i <= pxa_last_gpio; i++)
0246         gpio_desc[i].dir_inverted = 1;
0247 }
0248 #else
0249 static inline void pxa25x_mfp_init(void) {}
0250 #endif /* CONFIG_PXA25x */
0251 
0252 #ifdef CONFIG_PXA27x
0253 static int pxa27x_pkwr_gpio[] = {
0254     13, 16, 17, 34, 36, 37, 38, 39, 90, 91, 93, 94,
0255     95, 96, 97, 98, 99, 100, 101, 102
0256 };
0257 
0258 int keypad_set_wake(unsigned int on)
0259 {
0260     unsigned int i, gpio, mask = 0;
0261     struct gpio_desc *d;
0262 
0263     for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
0264 
0265         gpio = pxa27x_pkwr_gpio[i];
0266         d = &gpio_desc[gpio];
0267 
0268         /* skip if configured as generic GPIO */
0269         if (MFP_AF(d->config) == 0)
0270             continue;
0271 
0272         if (d->config & MFP_LPM_CAN_WAKEUP)
0273             mask |= gpio_desc[gpio].mask;
0274     }
0275 
0276     if (on)
0277         PKWR |= mask;
0278     else
0279         PKWR &= ~mask;
0280     return 0;
0281 }
0282 
0283 #define PWER_WEMUX2_GPIO38  (1 << 16)
0284 #define PWER_WEMUX2_GPIO53  (2 << 16)
0285 #define PWER_WEMUX2_GPIO40  (3 << 16)
0286 #define PWER_WEMUX2_GPIO36  (4 << 16)
0287 #define PWER_WEMUX2_MASK    (7 << 16)
0288 #define PWER_WEMUX3_GPIO31  (1 << 19)
0289 #define PWER_WEMUX3_GPIO113 (2 << 19)
0290 #define PWER_WEMUX3_MASK    (3 << 19)
0291 
0292 #define INIT_GPIO_DESC_MUXED(mux, gpio)             \
0293 do {                                \
0294     gpio_desc[(gpio)].can_wakeup = 1;           \
0295     gpio_desc[(gpio)].mask = PWER_ ## mux ## _GPIO ##gpio;  \
0296     gpio_desc[(gpio)].mux_mask = PWER_ ## mux ## _MASK; \
0297 } while (0)
0298 
0299 static void __init pxa27x_mfp_init(void)
0300 {
0301     int i, gpio;
0302 
0303     pxa_last_gpio = 120;    /* running before pxa_gpio_probe() */
0304     for (i = 0; i <= pxa_last_gpio; i++) {
0305         /* skip GPIO2, 5, 6, 7, 8, they are not
0306          * valid pins allow configuration
0307          */
0308         if (i == 2 || i == 5 || i == 6 || i == 7 || i == 8)
0309             continue;
0310 
0311         gpio_desc[i].valid = 1;
0312     }
0313 
0314     /* Keypad GPIOs */
0315     for (i = 0; i < ARRAY_SIZE(pxa27x_pkwr_gpio); i++) {
0316         gpio = pxa27x_pkwr_gpio[i];
0317         gpio_desc[gpio].can_wakeup = 1;
0318         gpio_desc[gpio].keypad_gpio = 1;
0319         gpio_desc[gpio].mask = 1 << i;
0320     }
0321 
0322     /* Overwrite GPIO13 as a PWER wakeup source */
0323     for (i = 0; i <= 15; i++) {
0324         /* skip GPIO2, 5, 6, 7, 8 */
0325         if (GPIO_bit(i) & 0x1e4)
0326             continue;
0327 
0328         gpio_desc[i].can_wakeup = 1;
0329         gpio_desc[i].mask = GPIO_bit(i);
0330     }
0331 
0332     gpio_desc[35].can_wakeup = 1;
0333     gpio_desc[35].mask = PWER_WE35;
0334 
0335     INIT_GPIO_DESC_MUXED(WEMUX3, 31);
0336     INIT_GPIO_DESC_MUXED(WEMUX3, 113);
0337     INIT_GPIO_DESC_MUXED(WEMUX2, 38);
0338     INIT_GPIO_DESC_MUXED(WEMUX2, 53);
0339     INIT_GPIO_DESC_MUXED(WEMUX2, 40);
0340     INIT_GPIO_DESC_MUXED(WEMUX2, 36);
0341 }
0342 #else
0343 static inline void pxa27x_mfp_init(void) {}
0344 #endif /* CONFIG_PXA27x */
0345 
0346 #ifdef CONFIG_PM
0347 static unsigned long saved_gafr[2][4];
0348 static unsigned long saved_gpdr[4];
0349 static unsigned long saved_gplr[4];
0350 static unsigned long saved_pgsr[4];
0351 
0352 static int pxa2xx_mfp_suspend(void)
0353 {
0354     int i;
0355 
0356     /* set corresponding PGSR bit of those marked MFP_LPM_KEEP_OUTPUT */
0357     for (i = 0; i < pxa_last_gpio; i++) {
0358         if ((gpio_desc[i].config & MFP_LPM_KEEP_OUTPUT) &&
0359             (GPDR(i) & GPIO_bit(i))) {
0360             if (GPLR(i) & GPIO_bit(i))
0361                 PGSR(gpio_to_bank(i)) |= GPIO_bit(i);
0362             else
0363                 PGSR(gpio_to_bank(i)) &= ~GPIO_bit(i);
0364         }
0365     }
0366 
0367     for (i = 0; i <= gpio_to_bank(pxa_last_gpio); i++) {
0368         saved_gafr[0][i] = GAFR_L(i);
0369         saved_gafr[1][i] = GAFR_U(i);
0370         saved_gpdr[i] = GPDR(i * 32);
0371         saved_gplr[i] = GPLR(i * 32);
0372         saved_pgsr[i] = PGSR(i);
0373 
0374         GPSR(i * 32) = PGSR(i);
0375         GPCR(i * 32) = ~PGSR(i);
0376     }
0377 
0378     /* set GPDR bits taking into account MFP_LPM_KEEP_OUTPUT */
0379     for (i = 0; i < pxa_last_gpio; i++) {
0380         if ((gpdr_lpm[gpio_to_bank(i)] & GPIO_bit(i)) ||
0381             ((gpio_desc[i].config & MFP_LPM_KEEP_OUTPUT) &&
0382              (saved_gpdr[gpio_to_bank(i)] & GPIO_bit(i))))
0383             GPDR(i) |= GPIO_bit(i);
0384         else
0385             GPDR(i) &= ~GPIO_bit(i);
0386     }
0387 
0388     return 0;
0389 }
0390 
0391 static void pxa2xx_mfp_resume(void)
0392 {
0393     int i;
0394 
0395     for (i = 0; i <= gpio_to_bank(pxa_last_gpio); i++) {
0396         GAFR_L(i) = saved_gafr[0][i];
0397         GAFR_U(i) = saved_gafr[1][i];
0398         GPSR(i * 32) = saved_gplr[i];
0399         GPCR(i * 32) = ~saved_gplr[i];
0400         GPDR(i * 32) = saved_gpdr[i];
0401         PGSR(i) = saved_pgsr[i];
0402     }
0403     PSSR = PSSR_RDH | PSSR_PH;
0404 }
0405 #else
0406 #define pxa2xx_mfp_suspend  NULL
0407 #define pxa2xx_mfp_resume   NULL
0408 #endif
0409 
0410 struct syscore_ops pxa2xx_mfp_syscore_ops = {
0411     .suspend    = pxa2xx_mfp_suspend,
0412     .resume     = pxa2xx_mfp_resume,
0413 };
0414 
0415 static int __init pxa2xx_mfp_init(void)
0416 {
0417     int i;
0418 
0419     if (!cpu_is_pxa2xx())
0420         return 0;
0421 
0422     if (cpu_is_pxa25x())
0423         pxa25x_mfp_init();
0424 
0425     if (cpu_is_pxa27x())
0426         pxa27x_mfp_init();
0427 
0428     /* clear RDH bit to enable GPIO receivers after reset/sleep exit */
0429     PSSR = PSSR_RDH;
0430 
0431     /* initialize gafr_run[], pgsr_lpm[] from existing values */
0432     for (i = 0; i <= gpio_to_bank(pxa_last_gpio); i++)
0433         gpdr_lpm[i] = GPDR(i * 32);
0434 
0435     return 0;
0436 }
0437 postcore_initcall(pxa2xx_mfp_init);