2 * Freescale STMP378X/STMP378X Pin Multiplexing
4 * Author: Vladislav Buzov <vbuzov@embeddedalley.com>
6 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
7 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
11 * The code contained herein is licensed under the GNU General Public
12 * License. You may obtain a copy of the GNU General Public License
13 * Version 2 or later at the following locations:
15 * http://www.opensource.org/licenses/gpl-license.html
16 * http://www.gnu.org/copyleft/gpl.html
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/sysdev.h>
23 #include <linux/string.h>
24 #include <linux/bitops.h>
25 #include <linux/sysdev.h>
26 #include <linux/irq.h>
28 #include <mach/hardware.h>
29 #include <mach/platform.h>
30 #include <mach/regs-pinctrl.h>
31 #include <mach/pins.h>
32 #include <mach/pinmux.h>
34 #define NR_BANKS ARRAY_SIZE(pinmux_banks)
35 static struct stmp3xxx_pinmux_bank pinmux_banks[] = {
38 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL0,
39 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL1,
42 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE0,
43 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE1,
44 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE2,
45 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE3,
47 .hw_pull = REGS_PINCTRL_BASE + HW_PINCTRL_PULL0,
48 .functions = { 0x0, 0x1, 0x2, 0x3 },
49 .strengths = { 0x0, 0x1, 0x2, 0x3, 0xff },
51 .hw_gpio_in = REGS_PINCTRL_BASE + HW_PINCTRL_DIN0,
52 .hw_gpio_out = REGS_PINCTRL_BASE + HW_PINCTRL_DOUT0,
53 .hw_gpio_doe = REGS_PINCTRL_BASE + HW_PINCTRL_DOE0,
56 .pin2irq = REGS_PINCTRL_BASE + HW_PINCTRL_PIN2IRQ0,
57 .irqstat = REGS_PINCTRL_BASE + HW_PINCTRL_IRQSTAT0,
58 .irqlevel = REGS_PINCTRL_BASE + HW_PINCTRL_IRQLEVEL0,
59 .irqpolarity = REGS_PINCTRL_BASE + HW_PINCTRL_IRQPOL0,
60 .irqen = REGS_PINCTRL_BASE + HW_PINCTRL_IRQEN0,
64 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL2,
65 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL3,
68 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE4,
69 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE5,
70 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE6,
71 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE7,
73 .hw_pull = REGS_PINCTRL_BASE + HW_PINCTRL_PULL1,
74 .functions = { 0x0, 0x1, 0x2, 0x3 },
75 .strengths = { 0x0, 0x1, 0x2, 0x3, 0xff },
77 .hw_gpio_in = REGS_PINCTRL_BASE + HW_PINCTRL_DIN1,
78 .hw_gpio_out = REGS_PINCTRL_BASE + HW_PINCTRL_DOUT1,
79 .hw_gpio_doe = REGS_PINCTRL_BASE + HW_PINCTRL_DOE1,
82 .pin2irq = REGS_PINCTRL_BASE + HW_PINCTRL_PIN2IRQ1,
83 .irqstat = REGS_PINCTRL_BASE + HW_PINCTRL_IRQSTAT1,
84 .irqlevel = REGS_PINCTRL_BASE + HW_PINCTRL_IRQLEVEL1,
85 .irqpolarity = REGS_PINCTRL_BASE + HW_PINCTRL_IRQPOL1,
86 .irqen = REGS_PINCTRL_BASE + HW_PINCTRL_IRQEN1,
90 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL4,
91 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL5,
94 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE8,
95 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE9,
96 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE10,
97 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE11,
99 .hw_pull = REGS_PINCTRL_BASE + HW_PINCTRL_PULL2,
100 .functions = { 0x0, 0x1, 0x2, 0x3 },
101 .strengths = { 0x0, 0x1, 0x2, 0x1, 0x2 },
103 .hw_gpio_in = REGS_PINCTRL_BASE + HW_PINCTRL_DIN2,
104 .hw_gpio_out = REGS_PINCTRL_BASE + HW_PINCTRL_DOUT2,
105 .hw_gpio_doe = REGS_PINCTRL_BASE + HW_PINCTRL_DOE2,
108 .pin2irq = REGS_PINCTRL_BASE + HW_PINCTRL_PIN2IRQ2,
109 .irqstat = REGS_PINCTRL_BASE + HW_PINCTRL_IRQSTAT2,
110 .irqlevel = REGS_PINCTRL_BASE + HW_PINCTRL_IRQLEVEL2,
111 .irqpolarity = REGS_PINCTRL_BASE + HW_PINCTRL_IRQPOL2,
112 .irqen = REGS_PINCTRL_BASE + HW_PINCTRL_IRQEN2,
116 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL6,
117 REGS_PINCTRL_BASE + HW_PINCTRL_MUXSEL7,
120 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE12,
121 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE13,
122 REGS_PINCTRL_BASE + HW_PINCTRL_DRIVE14,
125 .hw_pull = REGS_PINCTRL_BASE + HW_PINCTRL_PULL3,
126 .functions = {0x0, 0x1, 0x2, 0x3},
127 .strengths = {0x0, 0x1, 0x2, 0x3, 0xff},
131 static inline struct stmp3xxx_pinmux_bank *
132 stmp3xxx_pinmux_bank(unsigned id, unsigned *bank, unsigned *pin)
136 b = STMP3XXX_PINID_TO_BANK(id);
137 p = STMP3XXX_PINID_TO_PINNUM(id);
138 BUG_ON(b >= NR_BANKS);
143 return &pinmux_banks[b];
146 /* Check if requested pin is owned by caller */
147 static int stmp3xxx_check_pin(unsigned id, const char *label)
150 struct stmp3xxx_pinmux_bank *pm = stmp3xxx_pinmux_bank(id, NULL, &pin);
152 if (!test_bit(pin, &pm->pin_map)) {
154 "%s: Accessing free pin %x, caller %s\n",
155 __func__, id, label);
160 if (label && pm->pin_labels[pin] &&
161 strcmp(label, pm->pin_labels[pin])) {
163 "%s: Wrong pin owner %x, caller %s owner %s\n",
164 __func__, id, label, pm->pin_labels[pin]);
171 void stmp3xxx_pin_strength(unsigned id, enum pin_strength strength,
174 struct stmp3xxx_pinmux_bank *pbank;
175 void __iomem *hwdrive;
179 pbank = stmp3xxx_pinmux_bank(id, &bank, &pin);
180 pr_debug("%s: label %s bank %d pin %d strength %d\n", __func__, label,
181 bank, pin, strength);
183 hwdrive = pbank->hw_drive[pin / HW_DRIVE_PIN_NUM];
184 shift = (pin % HW_DRIVE_PIN_NUM) * HW_DRIVE_PIN_LEN;
185 val = pbank->strengths[strength];
188 "%s: strength is not supported for bank %d, caller %s",
189 __func__, bank, label);
193 if (stmp3xxx_check_pin(id, label))
196 pr_debug("%s: writing 0x%x to 0x%p register\n", __func__,
197 val << shift, hwdrive);
198 stmp3xxx_clearl(HW_DRIVE_PINDRV_MASK << shift, hwdrive);
199 stmp3xxx_setl(val << shift, hwdrive);
202 void stmp3xxx_pin_voltage(unsigned id, enum pin_voltage voltage,
205 struct stmp3xxx_pinmux_bank *pbank;
206 void __iomem *hwdrive;
210 pbank = stmp3xxx_pinmux_bank(id, &bank, &pin);
211 pr_debug("%s: label %s bank %d pin %d voltage %d\n", __func__, label,
214 hwdrive = pbank->hw_drive[pin / HW_DRIVE_PIN_NUM];
215 shift = (pin % HW_DRIVE_PIN_NUM) * HW_DRIVE_PIN_LEN;
217 if (stmp3xxx_check_pin(id, label))
220 pr_debug("%s: changing 0x%x bit in 0x%p register\n",
221 __func__, HW_DRIVE_PINV_MASK << shift, hwdrive);
222 if (voltage == PIN_1_8V)
223 stmp3xxx_clearl(HW_DRIVE_PINV_MASK << shift, hwdrive);
225 stmp3xxx_setl(HW_DRIVE_PINV_MASK << shift, hwdrive);
228 void stmp3xxx_pin_pullup(unsigned id, int enable, const char *label)
230 struct stmp3xxx_pinmux_bank *pbank;
231 void __iomem *hwpull;
234 pbank = stmp3xxx_pinmux_bank(id, &bank, &pin);
235 pr_debug("%s: label %s bank %d pin %d enable %d\n", __func__, label,
238 hwpull = pbank->hw_pull;
240 if (stmp3xxx_check_pin(id, label))
243 pr_debug("%s: changing 0x%x bit in 0x%p register\n",
244 __func__, 1 << pin, hwpull);
246 stmp3xxx_setl(1 << pin, hwpull);
248 stmp3xxx_clearl(1 << pin, hwpull);
251 int stmp3xxx_request_pin(unsigned id, enum pin_fun fun, const char *label)
253 struct stmp3xxx_pinmux_bank *pbank;
257 pbank = stmp3xxx_pinmux_bank(id, &bank, &pin);
258 pr_debug("%s: label %s bank %d pin %d fun %d\n", __func__, label,
261 if (test_bit(pin, &pbank->pin_map)) {
263 "%s: CONFLICT DETECTED pin %d:%d caller %s owner %s\n",
264 __func__, bank, pin, label, pbank->pin_labels[pin]);
268 set_bit(pin, &pbank->pin_map);
269 pbank->pin_labels[pin] = label;
271 stmp3xxx_set_pin_type(id, fun);
276 void stmp3xxx_set_pin_type(unsigned id, enum pin_fun fun)
278 struct stmp3xxx_pinmux_bank *pbank;
283 pbank = stmp3xxx_pinmux_bank(id, &bank, &pin);
285 hwmux = pbank->hw_muxsel[pin / HW_MUXSEL_PIN_NUM];
286 shift = (pin % HW_MUXSEL_PIN_NUM) * HW_MUXSEL_PIN_LEN;
288 val = pbank->functions[fun];
289 shift = (pin % HW_MUXSEL_PIN_NUM) * HW_MUXSEL_PIN_LEN;
290 pr_debug("%s: writing 0x%x to 0x%p register\n",
291 __func__, val << shift, hwmux);
292 stmp3xxx_clearl(HW_MUXSEL_PINFUN_MASK << shift, hwmux);
293 stmp3xxx_setl(val << shift, hwmux);
296 void stmp3xxx_release_pin(unsigned id, const char *label)
298 struct stmp3xxx_pinmux_bank *pbank;
301 pbank = stmp3xxx_pinmux_bank(id, &bank, &pin);
302 pr_debug("%s: label %s bank %d pin %d\n", __func__, label, bank, pin);
304 if (stmp3xxx_check_pin(id, label))
307 clear_bit(pin, &pbank->pin_map);
308 pbank->pin_labels[pin] = NULL;
311 int stmp3xxx_request_pin_group(struct pin_group *pin_group, const char *label)
313 struct pin_desc *pin;
317 /* Allocate and configure pins */
318 for (p = 0; p < pin_group->nr_pins; p++) {
319 pr_debug("%s: #%d\n", __func__, p);
320 pin = &pin_group->pins[p];
322 err = stmp3xxx_request_pin(pin->id, pin->fun, label);
326 stmp3xxx_pin_strength(pin->id, pin->strength, label);
327 stmp3xxx_pin_voltage(pin->id, pin->voltage, label);
328 stmp3xxx_pin_pullup(pin->id, pin->pullup, label);
334 /* Release allocated pins in case of error */
336 pr_debug("%s: releasing #%d\n", __func__, p);
337 stmp3xxx_release_pin(pin_group->pins[p].id, label);
341 EXPORT_SYMBOL(stmp3xxx_request_pin_group);
343 void stmp3xxx_release_pin_group(struct pin_group *pin_group, const char *label)
345 struct pin_desc *pin;
348 for (p = 0; p < pin_group->nr_pins; p++) {
349 pin = &pin_group->pins[p];
350 stmp3xxx_release_pin(pin->id, label);
353 EXPORT_SYMBOL(stmp3xxx_release_pin_group);
355 static int stmp3xxx_irq_to_gpio(int irq,
356 struct stmp3xxx_pinmux_bank **bank, unsigned *gpio)
358 struct stmp3xxx_pinmux_bank *pm;
360 for (pm = pinmux_banks; pm < pinmux_banks + NR_BANKS; pm++)
361 if (pm->virq <= irq && irq < pm->virq + 32) {
363 *gpio = irq - pm->virq;
369 static int stmp3xxx_set_irqtype(unsigned irq, unsigned type)
371 struct stmp3xxx_pinmux_bank *pm;
375 stmp3xxx_irq_to_gpio(irq, &pm, &gpio);
377 case IRQ_TYPE_EDGE_RISING:
379 case IRQ_TYPE_EDGE_FALLING:
381 case IRQ_TYPE_LEVEL_HIGH:
383 case IRQ_TYPE_LEVEL_LOW:
386 pr_debug("%s: Incorrect GPIO interrupt type 0x%x\n",
392 stmp3xxx_setl(1 << gpio, pm->irqlevel);
394 stmp3xxx_clearl(1 << gpio, pm->irqlevel);
396 stmp3xxx_setl(1 << gpio, pm->irqpolarity);
398 stmp3xxx_clearl(1 << gpio, pm->irqpolarity);
402 static void stmp3xxx_pin_ack_irq(unsigned irq)
405 struct stmp3xxx_pinmux_bank *pm;
408 stmp3xxx_irq_to_gpio(irq, &pm, &gpio);
409 stat = __raw_readl(pm->irqstat) & (1 << gpio);
410 stmp3xxx_clearl(stat, pm->irqstat);
413 static void stmp3xxx_pin_mask_irq(unsigned irq)
415 struct stmp3xxx_pinmux_bank *pm;
418 stmp3xxx_irq_to_gpio(irq, &pm, &gpio);
419 stmp3xxx_clearl(1 << gpio, pm->irqen);
420 stmp3xxx_clearl(1 << gpio, pm->pin2irq);
423 static void stmp3xxx_pin_unmask_irq(unsigned irq)
425 struct stmp3xxx_pinmux_bank *pm;
428 stmp3xxx_irq_to_gpio(irq, &pm, &gpio);
429 stmp3xxx_setl(1 << gpio, pm->irqen);
430 stmp3xxx_setl(1 << gpio, pm->pin2irq);
434 struct stmp3xxx_pinmux_bank *to_pinmux_bank(struct gpio_chip *chip)
436 return container_of(chip, struct stmp3xxx_pinmux_bank, chip);
439 static int stmp3xxx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
441 struct stmp3xxx_pinmux_bank *pm = to_pinmux_bank(chip);
442 return pm->virq + offset;
445 static int stmp3xxx_gpio_get(struct gpio_chip *chip, unsigned offset)
447 struct stmp3xxx_pinmux_bank *pm = to_pinmux_bank(chip);
450 v = __raw_readl(pm->hw_gpio_in) & (1 << offset);
454 static void stmp3xxx_gpio_set(struct gpio_chip *chip, unsigned offset, int v)
456 struct stmp3xxx_pinmux_bank *pm = to_pinmux_bank(chip);
459 stmp3xxx_setl(1 << offset, pm->hw_gpio_out);
461 stmp3xxx_clearl(1 << offset, pm->hw_gpio_out);
464 static int stmp3xxx_gpio_output(struct gpio_chip *chip, unsigned offset, int v)
466 struct stmp3xxx_pinmux_bank *pm = to_pinmux_bank(chip);
468 stmp3xxx_setl(1 << offset, pm->hw_gpio_doe);
469 stmp3xxx_gpio_set(chip, offset, v);
473 static int stmp3xxx_gpio_input(struct gpio_chip *chip, unsigned offset)
475 struct stmp3xxx_pinmux_bank *pm = to_pinmux_bank(chip);
477 stmp3xxx_clearl(1 << offset, pm->hw_gpio_doe);
481 static int stmp3xxx_gpio_request(struct gpio_chip *chip, unsigned offset)
483 return stmp3xxx_request_pin(chip->base + offset, PIN_GPIO, "gpio");
486 static void stmp3xxx_gpio_free(struct gpio_chip *chip, unsigned offset)
488 stmp3xxx_release_pin(chip->base + offset, "gpio");
491 static void stmp3xxx_gpio_irq(u32 irq, struct irq_desc *desc)
493 struct stmp3xxx_pinmux_bank *pm = get_irq_data(irq);
494 int gpio_irq = pm->virq;
495 u32 stat = __raw_readl(pm->irqstat);
499 irq_desc[gpio_irq].handle_irq(gpio_irq,
500 &irq_desc[gpio_irq]);
506 static struct irq_chip gpio_irq_chip = {
507 .ack = stmp3xxx_pin_ack_irq,
508 .mask = stmp3xxx_pin_mask_irq,
509 .unmask = stmp3xxx_pin_unmask_irq,
510 .set_type = stmp3xxx_set_irqtype,
513 int __init stmp3xxx_pinmux_init(int virtual_irq_start)
516 struct stmp3xxx_pinmux_bank *pm;
519 for (b = 0; b < 3; b++) {
520 /* only banks 0,1,2 are allowed to GPIO */
521 pm = pinmux_banks + b;
522 pm->chip.base = 32 * b;
524 pm->chip.owner = THIS_MODULE;
525 pm->chip.can_sleep = 1;
526 pm->chip.exported = 1;
527 pm->chip.to_irq = stmp3xxx_gpio_to_irq;
528 pm->chip.direction_input = stmp3xxx_gpio_input;
529 pm->chip.direction_output = stmp3xxx_gpio_output;
530 pm->chip.get = stmp3xxx_gpio_get;
531 pm->chip.set = stmp3xxx_gpio_set;
532 pm->chip.request = stmp3xxx_gpio_request;
533 pm->chip.free = stmp3xxx_gpio_free;
534 pm->virq = virtual_irq_start + b * 32;
536 for (virq = pm->virq; virq < pm->virq; virq++) {
537 gpio_irq_chip.mask(virq);
538 set_irq_chip(virq, &gpio_irq_chip);
539 set_irq_handler(virq, handle_level_irq);
540 set_irq_flags(virq, IRQF_VALID);
542 r = gpiochip_add(&pm->chip);
545 set_irq_chained_handler(pm->irq, stmp3xxx_gpio_irq);
546 set_irq_data(pm->irq, pm);
551 MODULE_AUTHOR("Vladislav Buzov");
552 MODULE_LICENSE("GPL");