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/irq.h>
27 #include <mach/hardware.h>
28 #include <mach/platform.h>
29 #include <mach/regs-pinctrl.h>
30 #include <mach/pins.h>
31 #include <mach/pinmux.h>
33 #define NR_BANKS ARRAY_SIZE(pinmux_banks)
34 static struct stmp3xxx_pinmux_bank pinmux_banks
[] = {
37 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL0
,
38 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL1
,
41 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE0
,
42 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE1
,
43 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE2
,
44 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE3
,
46 .hw_pull
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PULL0
,
47 .functions
= { 0x0, 0x1, 0x2, 0x3 },
48 .strengths
= { 0x0, 0x1, 0x2, 0x3, 0xff },
50 .hw_gpio_in
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DIN0
,
51 .hw_gpio_out
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DOUT0
,
52 .hw_gpio_doe
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DOE0
,
55 .pin2irq
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PIN2IRQ0
,
56 .irqstat
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQSTAT0
,
57 .irqlevel
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQLEVEL0
,
58 .irqpolarity
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQPOL0
,
59 .irqen
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQEN0
,
63 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL2
,
64 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL3
,
67 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE4
,
68 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE5
,
69 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE6
,
70 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE7
,
72 .hw_pull
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PULL1
,
73 .functions
= { 0x0, 0x1, 0x2, 0x3 },
74 .strengths
= { 0x0, 0x1, 0x2, 0x3, 0xff },
76 .hw_gpio_in
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DIN1
,
77 .hw_gpio_out
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DOUT1
,
78 .hw_gpio_doe
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DOE1
,
81 .pin2irq
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PIN2IRQ1
,
82 .irqstat
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQSTAT1
,
83 .irqlevel
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQLEVEL1
,
84 .irqpolarity
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQPOL1
,
85 .irqen
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQEN1
,
89 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL4
,
90 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL5
,
93 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE8
,
94 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE9
,
95 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE10
,
96 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE11
,
98 .hw_pull
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PULL2
,
99 .functions
= { 0x0, 0x1, 0x2, 0x3 },
100 .strengths
= { 0x0, 0x1, 0x2, 0x1, 0x2 },
102 .hw_gpio_in
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DIN2
,
103 .hw_gpio_out
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DOUT2
,
104 .hw_gpio_doe
= REGS_PINCTRL_BASE
+ HW_PINCTRL_DOE2
,
107 .pin2irq
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PIN2IRQ2
,
108 .irqstat
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQSTAT2
,
109 .irqlevel
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQLEVEL2
,
110 .irqpolarity
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQPOL2
,
111 .irqen
= REGS_PINCTRL_BASE
+ HW_PINCTRL_IRQEN2
,
115 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL6
,
116 REGS_PINCTRL_BASE
+ HW_PINCTRL_MUXSEL7
,
119 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE12
,
120 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE13
,
121 REGS_PINCTRL_BASE
+ HW_PINCTRL_DRIVE14
,
124 .hw_pull
= REGS_PINCTRL_BASE
+ HW_PINCTRL_PULL3
,
125 .functions
= {0x0, 0x1, 0x2, 0x3},
126 .strengths
= {0x0, 0x1, 0x2, 0x3, 0xff},
130 static inline struct stmp3xxx_pinmux_bank
*
131 stmp3xxx_pinmux_bank(unsigned id
, unsigned *bank
, unsigned *pin
)
135 b
= STMP3XXX_PINID_TO_BANK(id
);
136 p
= STMP3XXX_PINID_TO_PINNUM(id
);
137 BUG_ON(b
>= NR_BANKS
);
142 return &pinmux_banks
[b
];
145 /* Check if requested pin is owned by caller */
146 static int stmp3xxx_check_pin(unsigned id
, const char *label
)
149 struct stmp3xxx_pinmux_bank
*pm
= stmp3xxx_pinmux_bank(id
, NULL
, &pin
);
151 if (!test_bit(pin
, &pm
->pin_map
)) {
153 "%s: Accessing free pin %x, caller %s\n",
154 __func__
, id
, label
);
159 if (label
&& pm
->pin_labels
[pin
] &&
160 strcmp(label
, pm
->pin_labels
[pin
])) {
162 "%s: Wrong pin owner %x, caller %s owner %s\n",
163 __func__
, id
, label
, pm
->pin_labels
[pin
]);
170 void stmp3xxx_pin_strength(unsigned id
, enum pin_strength strength
,
173 struct stmp3xxx_pinmux_bank
*pbank
;
174 void __iomem
*hwdrive
;
178 pbank
= stmp3xxx_pinmux_bank(id
, &bank
, &pin
);
179 pr_debug("%s: label %s bank %d pin %d strength %d\n", __func__
, label
,
180 bank
, pin
, strength
);
182 hwdrive
= pbank
->hw_drive
[pin
/ HW_DRIVE_PIN_NUM
];
183 shift
= (pin
% HW_DRIVE_PIN_NUM
) * HW_DRIVE_PIN_LEN
;
184 val
= pbank
->strengths
[strength
];
187 "%s: strength is not supported for bank %d, caller %s",
188 __func__
, bank
, label
);
192 if (stmp3xxx_check_pin(id
, label
))
195 pr_debug("%s: writing 0x%x to 0x%p register\n", __func__
,
196 val
<< shift
, hwdrive
);
197 stmp3xxx_clearl(HW_DRIVE_PINDRV_MASK
<< shift
, hwdrive
);
198 stmp3xxx_setl(val
<< shift
, hwdrive
);
201 void stmp3xxx_pin_voltage(unsigned id
, enum pin_voltage voltage
,
204 struct stmp3xxx_pinmux_bank
*pbank
;
205 void __iomem
*hwdrive
;
209 pbank
= stmp3xxx_pinmux_bank(id
, &bank
, &pin
);
210 pr_debug("%s: label %s bank %d pin %d voltage %d\n", __func__
, label
,
213 hwdrive
= pbank
->hw_drive
[pin
/ HW_DRIVE_PIN_NUM
];
214 shift
= (pin
% HW_DRIVE_PIN_NUM
) * HW_DRIVE_PIN_LEN
;
216 if (stmp3xxx_check_pin(id
, label
))
219 pr_debug("%s: changing 0x%x bit in 0x%p register\n",
220 __func__
, HW_DRIVE_PINV_MASK
<< shift
, hwdrive
);
221 if (voltage
== PIN_1_8V
)
222 stmp3xxx_clearl(HW_DRIVE_PINV_MASK
<< shift
, hwdrive
);
224 stmp3xxx_setl(HW_DRIVE_PINV_MASK
<< shift
, hwdrive
);
227 void stmp3xxx_pin_pullup(unsigned id
, int enable
, const char *label
)
229 struct stmp3xxx_pinmux_bank
*pbank
;
230 void __iomem
*hwpull
;
233 pbank
= stmp3xxx_pinmux_bank(id
, &bank
, &pin
);
234 pr_debug("%s: label %s bank %d pin %d enable %d\n", __func__
, label
,
237 hwpull
= pbank
->hw_pull
;
239 if (stmp3xxx_check_pin(id
, label
))
242 pr_debug("%s: changing 0x%x bit in 0x%p register\n",
243 __func__
, 1 << pin
, hwpull
);
245 stmp3xxx_setl(1 << pin
, hwpull
);
247 stmp3xxx_clearl(1 << pin
, hwpull
);
250 int stmp3xxx_request_pin(unsigned id
, enum pin_fun fun
, const char *label
)
252 struct stmp3xxx_pinmux_bank
*pbank
;
256 pbank
= stmp3xxx_pinmux_bank(id
, &bank
, &pin
);
257 pr_debug("%s: label %s bank %d pin %d fun %d\n", __func__
, label
,
260 if (test_bit(pin
, &pbank
->pin_map
)) {
262 "%s: CONFLICT DETECTED pin %d:%d caller %s owner %s\n",
263 __func__
, bank
, pin
, label
, pbank
->pin_labels
[pin
]);
267 set_bit(pin
, &pbank
->pin_map
);
268 pbank
->pin_labels
[pin
] = label
;
270 stmp3xxx_set_pin_type(id
, fun
);
275 void stmp3xxx_set_pin_type(unsigned id
, enum pin_fun fun
)
277 struct stmp3xxx_pinmux_bank
*pbank
;
282 pbank
= stmp3xxx_pinmux_bank(id
, &bank
, &pin
);
284 hwmux
= pbank
->hw_muxsel
[pin
/ HW_MUXSEL_PIN_NUM
];
285 shift
= (pin
% HW_MUXSEL_PIN_NUM
) * HW_MUXSEL_PIN_LEN
;
287 val
= pbank
->functions
[fun
];
288 shift
= (pin
% HW_MUXSEL_PIN_NUM
) * HW_MUXSEL_PIN_LEN
;
289 pr_debug("%s: writing 0x%x to 0x%p register\n",
290 __func__
, val
<< shift
, hwmux
);
291 stmp3xxx_clearl(HW_MUXSEL_PINFUN_MASK
<< shift
, hwmux
);
292 stmp3xxx_setl(val
<< shift
, hwmux
);
295 void stmp3xxx_release_pin(unsigned id
, const char *label
)
297 struct stmp3xxx_pinmux_bank
*pbank
;
300 pbank
= stmp3xxx_pinmux_bank(id
, &bank
, &pin
);
301 pr_debug("%s: label %s bank %d pin %d\n", __func__
, label
, bank
, pin
);
303 if (stmp3xxx_check_pin(id
, label
))
306 clear_bit(pin
, &pbank
->pin_map
);
307 pbank
->pin_labels
[pin
] = NULL
;
310 int stmp3xxx_request_pin_group(struct pin_group
*pin_group
, const char *label
)
312 struct pin_desc
*pin
;
316 /* Allocate and configure pins */
317 for (p
= 0; p
< pin_group
->nr_pins
; p
++) {
318 pr_debug("%s: #%d\n", __func__
, p
);
319 pin
= &pin_group
->pins
[p
];
321 err
= stmp3xxx_request_pin(pin
->id
, pin
->fun
, label
);
325 stmp3xxx_pin_strength(pin
->id
, pin
->strength
, label
);
326 stmp3xxx_pin_voltage(pin
->id
, pin
->voltage
, label
);
327 stmp3xxx_pin_pullup(pin
->id
, pin
->pullup
, label
);
333 /* Release allocated pins in case of error */
335 pr_debug("%s: releasing #%d\n", __func__
, p
);
336 stmp3xxx_release_pin(pin_group
->pins
[p
].id
, label
);
340 EXPORT_SYMBOL(stmp3xxx_request_pin_group
);
342 void stmp3xxx_release_pin_group(struct pin_group
*pin_group
, const char *label
)
344 struct pin_desc
*pin
;
347 for (p
= 0; p
< pin_group
->nr_pins
; p
++) {
348 pin
= &pin_group
->pins
[p
];
349 stmp3xxx_release_pin(pin
->id
, label
);
352 EXPORT_SYMBOL(stmp3xxx_release_pin_group
);
354 static int stmp3xxx_irq_to_gpio(int irq
,
355 struct stmp3xxx_pinmux_bank
**bank
, unsigned *gpio
)
357 struct stmp3xxx_pinmux_bank
*pm
;
359 for (pm
= pinmux_banks
; pm
< pinmux_banks
+ NR_BANKS
; pm
++)
360 if (pm
->virq
<= irq
&& irq
< pm
->virq
+ 32) {
362 *gpio
= irq
- pm
->virq
;
368 static int stmp3xxx_set_irqtype(unsigned irq
, unsigned type
)
370 struct stmp3xxx_pinmux_bank
*pm
;
374 stmp3xxx_irq_to_gpio(irq
, &pm
, &gpio
);
376 case IRQ_TYPE_EDGE_RISING
:
378 case IRQ_TYPE_EDGE_FALLING
:
380 case IRQ_TYPE_LEVEL_HIGH
:
382 case IRQ_TYPE_LEVEL_LOW
:
385 pr_debug("%s: Incorrect GPIO interrupt type 0x%x\n",
391 stmp3xxx_setl(1 << gpio
, pm
->irqlevel
);
393 stmp3xxx_clearl(1 << gpio
, pm
->irqlevel
);
395 stmp3xxx_setl(1 << gpio
, pm
->irqpolarity
);
397 stmp3xxx_clearl(1 << gpio
, pm
->irqpolarity
);
401 static void stmp3xxx_pin_ack_irq(unsigned irq
)
404 struct stmp3xxx_pinmux_bank
*pm
;
407 stmp3xxx_irq_to_gpio(irq
, &pm
, &gpio
);
408 stat
= __raw_readl(pm
->irqstat
) & (1 << gpio
);
409 stmp3xxx_clearl(stat
, pm
->irqstat
);
412 static void stmp3xxx_pin_mask_irq(unsigned irq
)
414 struct stmp3xxx_pinmux_bank
*pm
;
417 stmp3xxx_irq_to_gpio(irq
, &pm
, &gpio
);
418 stmp3xxx_clearl(1 << gpio
, pm
->irqen
);
419 stmp3xxx_clearl(1 << gpio
, pm
->pin2irq
);
422 static void stmp3xxx_pin_unmask_irq(unsigned irq
)
424 struct stmp3xxx_pinmux_bank
*pm
;
427 stmp3xxx_irq_to_gpio(irq
, &pm
, &gpio
);
428 stmp3xxx_setl(1 << gpio
, pm
->irqen
);
429 stmp3xxx_setl(1 << gpio
, pm
->pin2irq
);
433 struct stmp3xxx_pinmux_bank
*to_pinmux_bank(struct gpio_chip
*chip
)
435 return container_of(chip
, struct stmp3xxx_pinmux_bank
, chip
);
438 static int stmp3xxx_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
440 struct stmp3xxx_pinmux_bank
*pm
= to_pinmux_bank(chip
);
441 return pm
->virq
+ offset
;
444 static int stmp3xxx_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
446 struct stmp3xxx_pinmux_bank
*pm
= to_pinmux_bank(chip
);
449 v
= __raw_readl(pm
->hw_gpio_in
) & (1 << offset
);
453 static void stmp3xxx_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int v
)
455 struct stmp3xxx_pinmux_bank
*pm
= to_pinmux_bank(chip
);
458 stmp3xxx_setl(1 << offset
, pm
->hw_gpio_out
);
460 stmp3xxx_clearl(1 << offset
, pm
->hw_gpio_out
);
463 static int stmp3xxx_gpio_output(struct gpio_chip
*chip
, unsigned offset
, int v
)
465 struct stmp3xxx_pinmux_bank
*pm
= to_pinmux_bank(chip
);
467 stmp3xxx_setl(1 << offset
, pm
->hw_gpio_doe
);
468 stmp3xxx_gpio_set(chip
, offset
, v
);
472 static int stmp3xxx_gpio_input(struct gpio_chip
*chip
, unsigned offset
)
474 struct stmp3xxx_pinmux_bank
*pm
= to_pinmux_bank(chip
);
476 stmp3xxx_clearl(1 << offset
, pm
->hw_gpio_doe
);
480 static int stmp3xxx_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
482 return stmp3xxx_request_pin(chip
->base
+ offset
, PIN_GPIO
, "gpio");
485 static void stmp3xxx_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
487 stmp3xxx_release_pin(chip
->base
+ offset
, "gpio");
490 static void stmp3xxx_gpio_irq(u32 irq
, struct irq_desc
*desc
)
492 struct stmp3xxx_pinmux_bank
*pm
= get_irq_data(irq
);
493 int gpio_irq
= pm
->virq
;
494 u32 stat
= __raw_readl(pm
->irqstat
);
498 irq_desc
[gpio_irq
].handle_irq(gpio_irq
,
499 &irq_desc
[gpio_irq
]);
505 static struct irq_chip gpio_irq_chip
= {
506 .ack
= stmp3xxx_pin_ack_irq
,
507 .mask
= stmp3xxx_pin_mask_irq
,
508 .unmask
= stmp3xxx_pin_unmask_irq
,
509 .set_type
= stmp3xxx_set_irqtype
,
512 int __init
stmp3xxx_pinmux_init(int virtual_irq_start
)
515 struct stmp3xxx_pinmux_bank
*pm
;
518 for (b
= 0; b
< 3; b
++) {
519 /* only banks 0,1,2 are allowed to GPIO */
520 pm
= pinmux_banks
+ b
;
521 pm
->chip
.base
= 32 * b
;
523 pm
->chip
.owner
= THIS_MODULE
;
524 pm
->chip
.can_sleep
= 1;
525 pm
->chip
.exported
= 1;
526 pm
->chip
.to_irq
= stmp3xxx_gpio_to_irq
;
527 pm
->chip
.direction_input
= stmp3xxx_gpio_input
;
528 pm
->chip
.direction_output
= stmp3xxx_gpio_output
;
529 pm
->chip
.get
= stmp3xxx_gpio_get
;
530 pm
->chip
.set
= stmp3xxx_gpio_set
;
531 pm
->chip
.request
= stmp3xxx_gpio_request
;
532 pm
->chip
.free
= stmp3xxx_gpio_free
;
533 pm
->virq
= virtual_irq_start
+ b
* 32;
535 for (virq
= pm
->virq
; virq
< pm
->virq
; virq
++) {
536 gpio_irq_chip
.mask(virq
);
537 set_irq_chip(virq
, &gpio_irq_chip
);
538 set_irq_handler(virq
, handle_level_irq
);
539 set_irq_flags(virq
, IRQF_VALID
);
541 r
= gpiochip_add(&pm
->chip
);
544 set_irq_chained_handler(pm
->irq
, stmp3xxx_gpio_irq
);
545 set_irq_data(pm
->irq
, pm
);
550 MODULE_AUTHOR("Vladislav Buzov");
551 MODULE_LICENSE("GPL");