Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/qe: Implement QE Pin Multiplexing API

With this API we're able to set a QE pin to the GPIO mode or a dedicated
peripheral function.

The API relies on the fact that QE gpio controllers are registered. If
they aren't, the API won't work (gracefully though).

There is one caveat though: if anybody occupied the node->data before us,
or overwrote it, then bad things will happen. Luckily this is all in the
platform code that we fully control, so this should never happen.

I could implement more checks (for example we could create a list of
successfully registered QE controllers, and compare the node->data in the
qe_pin_request()), but this is unneeded if nobody is going to do silly
things behind our back.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
Signed-off-by: Kumar Gala <galak@kernel.crashing.org>

authored by

Anton Vorontsov and committed by
Kumar Gala
1b9e8904 78c77050

+216
+21
arch/powerpc/include/asm/qe.h
··· 17 17 #ifdef __KERNEL__ 18 18 19 19 #include <linux/spinlock.h> 20 + #include <linux/errno.h> 21 + #include <linux/err.h> 20 22 #include <asm/cpm.h> 21 23 #include <asm/immap_qe.h> 22 24 ··· 113 111 extern int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain, 114 112 int assignment, int has_irq); 115 113 extern int par_io_data_set(u8 port, u8 pin, u8 val); 114 + 115 + /* 116 + * Pin multiplexing functions. 117 + */ 118 + struct qe_pin; 119 + #ifdef CONFIG_QE_GPIO 120 + extern struct qe_pin *qe_pin_request(struct device_node *np, int index); 121 + extern void qe_pin_free(struct qe_pin *qe_pin); 122 + extern void qe_pin_set_gpio(struct qe_pin *qe_pin); 123 + extern void qe_pin_set_dedicated(struct qe_pin *pin); 124 + #else 125 + static inline struct qe_pin *qe_pin_request(struct device_node *np, int index) 126 + { 127 + return ERR_PTR(-ENOSYS); 128 + } 129 + static inline void qe_pin_free(struct qe_pin *qe_pin) {} 130 + static inline void qe_pin_set_gpio(struct qe_pin *qe_pin) {} 131 + static inline void qe_pin_set_dedicated(struct qe_pin *pin) {} 132 + #endif /* CONFIG_QE_GPIO */ 116 133 117 134 /* QE internal API */ 118 135 int qe_issue_cmd(u32 cmd, u32 device, u8 mcn_protocol, u32 cmd_input);
+195
arch/powerpc/sysdev/qe_lib/gpio.c
··· 14 14 #include <linux/kernel.h> 15 15 #include <linux/init.h> 16 16 #include <linux/spinlock.h> 17 + #include <linux/err.h> 17 18 #include <linux/io.h> 18 19 #include <linux/of.h> 19 20 #include <linux/of_gpio.h> ··· 25 24 struct of_mm_gpio_chip mm_gc; 26 25 spinlock_t lock; 27 26 27 + unsigned long pin_flags[QE_PIO_PINS]; 28 + #define QE_PIN_REQUESTED 0 29 + 28 30 /* shadowed data register to clear/set bits safely */ 29 31 u32 cpdata; 32 + 33 + /* saved_regs used to restore dedicated functions */ 34 + struct qe_pio_regs saved_regs; 30 35 }; 31 36 32 37 static inline struct qe_gpio_chip * ··· 47 40 struct qe_pio_regs __iomem *regs = mm_gc->regs; 48 41 49 42 qe_gc->cpdata = in_be32(&regs->cpdata); 43 + qe_gc->saved_regs.cpdata = qe_gc->cpdata; 44 + qe_gc->saved_regs.cpdir1 = in_be32(&regs->cpdir1); 45 + qe_gc->saved_regs.cpdir2 = in_be32(&regs->cpdir2); 46 + qe_gc->saved_regs.cppar1 = in_be32(&regs->cppar1); 47 + qe_gc->saved_regs.cppar2 = in_be32(&regs->cppar2); 48 + qe_gc->saved_regs.cpodr = in_be32(&regs->cpodr); 50 49 } 51 50 52 51 static int qe_gpio_get(struct gpio_chip *gc, unsigned int gpio) ··· 115 102 116 103 return 0; 117 104 } 105 + 106 + struct qe_pin { 107 + /* 108 + * The qe_gpio_chip name is unfortunate, we should change that to 109 + * something like qe_pio_controller. Someday. 110 + */ 111 + struct qe_gpio_chip *controller; 112 + int num; 113 + }; 114 + 115 + /** 116 + * qe_pin_request - Request a QE pin 117 + * @np: device node to get a pin from 118 + * @index: index of a pin in the device tree 119 + * Context: non-atomic 120 + * 121 + * This function return qe_pin so that you could use it with the rest of 122 + * the QE Pin Multiplexing API. 123 + */ 124 + struct qe_pin *qe_pin_request(struct device_node *np, int index) 125 + { 126 + struct qe_pin *qe_pin; 127 + struct device_node *gc; 128 + struct of_gpio_chip *of_gc = NULL; 129 + struct of_mm_gpio_chip *mm_gc; 130 + struct qe_gpio_chip *qe_gc; 131 + int err; 132 + int size; 133 + const void *gpio_spec; 134 + const u32 *gpio_cells; 135 + unsigned long flags; 136 + 137 + qe_pin = kzalloc(sizeof(*qe_pin), GFP_KERNEL); 138 + if (!qe_pin) { 139 + pr_debug("%s: can't allocate memory\n", __func__); 140 + return ERR_PTR(-ENOMEM); 141 + } 142 + 143 + err = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index, 144 + &gc, &gpio_spec); 145 + if (err) { 146 + pr_debug("%s: can't parse gpios property\n", __func__); 147 + goto err0; 148 + } 149 + 150 + if (!of_device_is_compatible(gc, "fsl,mpc8323-qe-pario-bank")) { 151 + pr_debug("%s: tried to get a non-qe pin\n", __func__); 152 + err = -EINVAL; 153 + goto err1; 154 + } 155 + 156 + of_gc = gc->data; 157 + if (!of_gc) { 158 + pr_debug("%s: gpio controller %s isn't registered\n", 159 + np->full_name, gc->full_name); 160 + err = -ENODEV; 161 + goto err1; 162 + } 163 + 164 + gpio_cells = of_get_property(gc, "#gpio-cells", &size); 165 + if (!gpio_cells || size != sizeof(*gpio_cells) || 166 + *gpio_cells != of_gc->gpio_cells) { 167 + pr_debug("%s: wrong #gpio-cells for %s\n", 168 + np->full_name, gc->full_name); 169 + err = -EINVAL; 170 + goto err1; 171 + } 172 + 173 + err = of_gc->xlate(of_gc, np, gpio_spec, NULL); 174 + if (err < 0) 175 + goto err1; 176 + 177 + mm_gc = to_of_mm_gpio_chip(&of_gc->gc); 178 + qe_gc = to_qe_gpio_chip(mm_gc); 179 + 180 + spin_lock_irqsave(&qe_gc->lock, flags); 181 + 182 + if (test_and_set_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[err]) == 0) { 183 + qe_pin->controller = qe_gc; 184 + qe_pin->num = err; 185 + err = 0; 186 + } else { 187 + err = -EBUSY; 188 + } 189 + 190 + spin_unlock_irqrestore(&qe_gc->lock, flags); 191 + 192 + if (!err) 193 + return qe_pin; 194 + err1: 195 + of_node_put(gc); 196 + err0: 197 + kfree(qe_pin); 198 + pr_debug("%s failed with status %d\n", __func__, err); 199 + return ERR_PTR(err); 200 + } 201 + EXPORT_SYMBOL(qe_pin_request); 202 + 203 + /** 204 + * qe_pin_free - Free a pin 205 + * @qe_pin: pointer to the qe_pin structure 206 + * Context: any 207 + * 208 + * This function frees the qe_pin structure and makes a pin available 209 + * for further qe_pin_request() calls. 210 + */ 211 + void qe_pin_free(struct qe_pin *qe_pin) 212 + { 213 + struct qe_gpio_chip *qe_gc = qe_pin->controller; 214 + unsigned long flags; 215 + const int pin = qe_pin->num; 216 + 217 + spin_lock_irqsave(&qe_gc->lock, flags); 218 + test_and_clear_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[pin]); 219 + spin_unlock_irqrestore(&qe_gc->lock, flags); 220 + 221 + kfree(qe_pin); 222 + } 223 + EXPORT_SYMBOL(qe_pin_free); 224 + 225 + /** 226 + * qe_pin_set_dedicated - Revert a pin to a dedicated peripheral function mode 227 + * @qe_pin: pointer to the qe_pin structure 228 + * Context: any 229 + * 230 + * This function resets a pin to a dedicated peripheral function that 231 + * has been set up by the firmware. 232 + */ 233 + void qe_pin_set_dedicated(struct qe_pin *qe_pin) 234 + { 235 + struct qe_gpio_chip *qe_gc = qe_pin->controller; 236 + struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs; 237 + struct qe_pio_regs *sregs = &qe_gc->saved_regs; 238 + int pin = qe_pin->num; 239 + u32 mask1 = 1 << (QE_PIO_PINS - (pin + 1)); 240 + u32 mask2 = 0x3 << (QE_PIO_PINS - (pin % (QE_PIO_PINS / 2) + 1) * 2); 241 + bool second_reg = pin > (QE_PIO_PINS / 2) - 1; 242 + unsigned long flags; 243 + 244 + spin_lock_irqsave(&qe_gc->lock, flags); 245 + 246 + if (second_reg) { 247 + clrsetbits_be32(&regs->cpdir2, mask2, sregs->cpdir2 & mask2); 248 + clrsetbits_be32(&regs->cppar2, mask2, sregs->cppar2 & mask2); 249 + } else { 250 + clrsetbits_be32(&regs->cpdir1, mask2, sregs->cpdir1 & mask2); 251 + clrsetbits_be32(&regs->cppar1, mask2, sregs->cppar1 & mask2); 252 + } 253 + 254 + if (sregs->cpdata & mask1) 255 + qe_gc->cpdata |= mask1; 256 + else 257 + qe_gc->cpdata &= ~mask1; 258 + 259 + out_be32(&regs->cpdata, qe_gc->cpdata); 260 + clrsetbits_be32(&regs->cpodr, mask1, sregs->cpodr & mask1); 261 + 262 + spin_unlock_irqrestore(&qe_gc->lock, flags); 263 + } 264 + EXPORT_SYMBOL(qe_pin_set_dedicated); 265 + 266 + /** 267 + * qe_pin_set_gpio - Set a pin to the GPIO mode 268 + * @qe_pin: pointer to the qe_pin structure 269 + * Context: any 270 + * 271 + * This function sets a pin to the GPIO mode. 272 + */ 273 + void qe_pin_set_gpio(struct qe_pin *qe_pin) 274 + { 275 + struct qe_gpio_chip *qe_gc = qe_pin->controller; 276 + struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs; 277 + unsigned long flags; 278 + 279 + spin_lock_irqsave(&qe_gc->lock, flags); 280 + 281 + /* Let's make it input by default, GPIO API is able to change that. */ 282 + __par_io_config_pin(regs, qe_pin->num, QE_PIO_DIR_IN, 0, 0, 0); 283 + 284 + spin_unlock_irqrestore(&qe_gc->lock, flags); 285 + } 286 + EXPORT_SYMBOL(qe_pin_set_gpio); 118 287 119 288 static int __init qe_add_gpiochips(void) 120 289 {