Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * TI DaVinci GPIO Support |
| 3 | * |
| 4 | * Copyright (c) 2006 David Brownell |
| 5 | * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #ifndef __DAVINCI_GPIO_H |
| 14 | #define __DAVINCI_GPIO_H |
| 15 | |
| 16 | #include <linux/io.h> |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 17 | #include <asm-generic/gpio.h> |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 18 | |
Russell King | 80b02c1 | 2009-01-08 10:01:47 +0000 | [diff] [blame] | 19 | #include <mach/irqs.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 20 | |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 21 | #define DAVINCI_GPIO_BASE 0x01C67000 |
| 22 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | /* |
| 24 | * basic gpio routines |
| 25 | * |
| 26 | * board-specific init should be done by arch/.../.../board-XXX.c (maybe |
| 27 | * initializing banks together) rather than boot loaders; kexec() won't |
| 28 | * go through boot loaders. |
| 29 | * |
| 30 | * the gpio clock will be turned on when gpios are used, and you may also |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 31 | * need to pay attention to PINMUX registers to be sure those pins are |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 32 | * used as gpios, not with other peripherals. |
| 33 | * |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 34 | * On-chip GPIOs are numbered 0..(DAVINCI_N_GPIO-1). For documentation, |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 35 | * and maybe for later updates, code may write GPIO(N). These may be |
| 36 | * all 1.8V signals, all 3.3V ones, or a mix of the two. A given chip |
| 37 | * may not support all the GPIOs in that range. |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 38 | * |
| 39 | * GPIOs can also be on external chips, numbered after the ones built-in |
| 40 | * to the DaVinci chip. For now, they won't be usable as IRQ sources. |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 41 | */ |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 42 | #define GPIO(X) (X) /* 0 <= X <= (DAVINCI_N_GPIO - 1) */ |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 43 | |
| 44 | struct gpio_controller { |
| 45 | u32 dir; |
| 46 | u32 out_data; |
| 47 | u32 set_data; |
| 48 | u32 clr_data; |
| 49 | u32 in_data; |
| 50 | u32 set_rising; |
| 51 | u32 clr_rising; |
| 52 | u32 set_falling; |
| 53 | u32 clr_falling; |
| 54 | u32 intstat; |
| 55 | }; |
| 56 | |
| 57 | /* The __gpio_to_controller() and __gpio_mask() functions inline to constants |
| 58 | * with constant parameters; or in outlined code they execute at runtime. |
| 59 | * |
| 60 | * You'd access the controller directly when reading or writing more than |
| 61 | * one gpio value at a time, and to support wired logic where the value |
| 62 | * being driven by the cpu need not match the value read back. |
| 63 | * |
| 64 | * These are NOT part of the cross-platform GPIO interface |
| 65 | */ |
| 66 | static inline struct gpio_controller *__iomem |
| 67 | __gpio_to_controller(unsigned gpio) |
| 68 | { |
| 69 | void *__iomem ptr; |
| 70 | |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 71 | if (gpio < 32 * 1) |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 72 | ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x10); |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 73 | else if (gpio < 32 * 2) |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 74 | ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x38); |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 75 | else if (gpio < 32 * 3) |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 76 | ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x60); |
David Brownell | 474dad5 | 2008-12-07 11:46:23 -0800 | [diff] [blame] | 77 | else if (gpio < 32 * 4) |
| 78 | ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x88); |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 79 | else |
| 80 | ptr = NULL; |
| 81 | return ptr; |
| 82 | } |
| 83 | |
| 84 | static inline u32 __gpio_mask(unsigned gpio) |
| 85 | { |
| 86 | return 1 << (gpio % 32); |
| 87 | } |
| 88 | |
| 89 | /* The get/set/clear functions will inline when called with constant |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 90 | * parameters referencing built-in GPIOs, for low-overhead bitbanging. |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 91 | * |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 92 | * Otherwise, calls with variable parameters or referencing external |
| 93 | * GPIOs (e.g. on GPIO expander chips) use outlined functions. |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 94 | */ |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 95 | static inline void gpio_set_value(unsigned gpio, int value) |
| 96 | { |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 97 | if (__builtin_constant_p(value) && gpio < DAVINCI_N_GPIO) { |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 98 | struct gpio_controller *__iomem g; |
| 99 | u32 mask; |
| 100 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 101 | g = __gpio_to_controller(gpio); |
| 102 | mask = __gpio_mask(gpio); |
| 103 | if (value) |
| 104 | __raw_writel(mask, &g->set_data); |
| 105 | else |
| 106 | __raw_writel(mask, &g->clr_data); |
| 107 | return; |
| 108 | } |
| 109 | |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 110 | __gpio_set_value(gpio, value); |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Returns zero or nonzero; works for gpios configured as inputs OR |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 114 | * as outputs, at least for built-in GPIOs. |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 115 | * |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 116 | * NOTE: for built-in GPIOs, changes in reported values are synchronized |
| 117 | * to the GPIO clock. This is easily seen after calling gpio_set_value() |
| 118 | * and then immediately gpio_get_value(), where the gpio_get_value() will |
| 119 | * return the old value until the GPIO clock ticks and the new value gets |
| 120 | * latched. |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 121 | */ |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 122 | static inline int gpio_get_value(unsigned gpio) |
| 123 | { |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 124 | struct gpio_controller *__iomem g; |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 125 | |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 126 | if (!__builtin_constant_p(gpio) || gpio >= DAVINCI_N_GPIO) |
| 127 | return __gpio_get_value(gpio); |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 128 | |
| 129 | g = __gpio_to_controller(gpio); |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 130 | return __gpio_mask(gpio) & __raw_readl(&g->in_data); |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 131 | } |
| 132 | |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 133 | static inline int gpio_cansleep(unsigned gpio) |
| 134 | { |
| 135 | if (__builtin_constant_p(gpio) && gpio < DAVINCI_N_GPIO) |
| 136 | return 0; |
| 137 | else |
| 138 | return __gpio_cansleep(gpio); |
| 139 | } |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 140 | |
| 141 | static inline int gpio_to_irq(unsigned gpio) |
| 142 | { |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 143 | if (gpio >= DAVINCI_N_GPIO) |
| 144 | return -EINVAL; |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 145 | return DAVINCI_N_AINTC_IRQ + gpio; |
| 146 | } |
| 147 | |
| 148 | static inline int irq_to_gpio(unsigned irq) |
| 149 | { |
David Brownell | dce1115 | 2008-09-07 23:41:04 -0700 | [diff] [blame] | 150 | /* caller guarantees gpio_to_irq() succeeded */ |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 151 | return irq - DAVINCI_N_AINTC_IRQ; |
| 152 | } |
| 153 | |
| 154 | #endif /* __DAVINCI_GPIO_H */ |