13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/device.h>
26 #include <linux/slab.h>
38 #ifdef CONFIG_ARCH_U8500
41 static inline u32 prcmu_read(
unsigned int reg) {
44 static inline void prcmu_write(
unsigned int reg,
u32 value) {}
63 #define NMK_GPIO_PER_CHIP 32
100 #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
102 static void __nmk_gpio_set_mode(
struct nmk_gpio_chip *nmk_chip,
103 unsigned offset,
int gpio_mode)
118 static void __nmk_gpio_set_slpm(
struct nmk_gpio_chip *nmk_chip,
132 static void __nmk_gpio_set_pull(
struct nmk_gpio_chip *nmk_chip,
157 static void __nmk_gpio_set_lowemi(
struct nmk_gpio_chip *nmk_chip,
158 unsigned offset,
bool lowemi)
163 if (lowemi == enabled)
171 writel_relaxed(nmk_chip->
lowemi,
175 static void __nmk_gpio_make_input(
struct nmk_gpio_chip *nmk_chip,
181 static void __nmk_gpio_set_output(
struct nmk_gpio_chip *nmk_chip,
182 unsigned offset,
int val)
190 static void __nmk_gpio_make_output(
struct nmk_gpio_chip *nmk_chip,
191 unsigned offset,
int val)
194 __nmk_gpio_set_output(nmk_chip, offset, val);
197 static void __nmk_gpio_set_mode_safe(
struct nmk_gpio_chip *nmk_chip,
198 unsigned offset,
int gpio_mode,
214 __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
225 nmk_gpio_disable_lazy_irq(
struct nmk_gpio_chip *nmk_chip,
unsigned offset)
233 if (!rising && !falling)
236 if (!d || !irqd_irq_disabled(d))
241 writel_relaxed(nmk_chip->
rimsc,
247 writel_relaxed(nmk_chip->
fimsc,
251 dev_dbg(nmk_chip->
chip.dev,
"%d: clearing interrupt mask\n", gpio);
254 static void nmk_prcm_altcx_set_mode(
struct nmk_pinctrl *npct,
255 unsigned offset,
unsigned alt_num)
262 const u16 *gpiocr_regs;
265 dev_err(npct->
dev,
"PRCM GPIOCR: alternate-C%i is invalid\n",
270 for (i = 0 ; i < npct->
soc->npins_altcx ; i++) {
271 if (npct->
soc->altcx_pins[i].pin == offset)
274 if (i == npct->
soc->npins_altcx) {
275 dev_dbg(npct->
dev,
"PRCM GPIOCR: pin %i is not found\n",
280 pin_desc = npct->
soc->altcx_pins +
i;
281 gpiocr_regs = npct->
soc->prcm_gpiocr_registers;
289 if (pin_desc->
altcx[i].used ==
true) {
290 reg = gpiocr_regs[pin_desc->
altcx[
i].reg_index];
291 bit = pin_desc->
altcx[
i].control_bit;
292 if (prcmu_read(reg) &
BIT(bit)) {
293 prcmu_write_masked(reg,
BIT(bit), 0);
295 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
303 alt_index = alt_num - 1;
304 if (pin_desc->
altcx[alt_index].used ==
false) {
306 "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
318 if (pin_desc->
altcx[i].used ==
true) {
319 reg = gpiocr_regs[pin_desc->
altcx[
i].reg_index];
320 bit = pin_desc->
altcx[
i].control_bit;
321 if (prcmu_read(reg) &
BIT(bit)) {
322 prcmu_write_masked(reg,
BIT(bit), 0);
324 "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
330 reg = gpiocr_regs[pin_desc->
altcx[alt_index].reg_index];
331 bit = pin_desc->
altcx[alt_index].control_bit;
332 dev_dbg(npct->
dev,
"PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
333 offset, alt_index+1);
334 prcmu_write_masked(reg,
BIT(bit),
BIT(bit));
337 static void __nmk_config_pin(
struct nmk_gpio_chip *nmk_chip,
unsigned offset,
340 static const char *afnames[] = {
346 static const char *pullnames[] = {
352 static const char *slpmnames[] = {
365 dev_dbg(nmk_chip->
chip.dev,
"pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
366 pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
367 output ?
"output " :
"input",
368 output ? (val ?
"high" :
"low") :
"");
382 pull = slpm_pull - 1;
384 output = slpm_output - 1;
388 dev_dbg(nmk_chip->
chip.dev,
"pin %d: sleep pull %s, dir %s, val %s\n",
390 slpm_pull ? pullnames[pull] :
"same",
391 slpm_output ? (output ?
"output" :
"input") :
"same",
392 slpm_val ? (val ?
"high" :
"low") :
"same");
396 __nmk_gpio_make_output(nmk_chip, offset, val);
398 __nmk_gpio_make_input(nmk_chip, offset);
399 __nmk_gpio_set_pull(nmk_chip, offset, pull);
402 __nmk_gpio_set_lowemi(nmk_chip, offset,
PIN_LOWEMI(cfg));
411 nmk_gpio_disable_lazy_irq(nmk_chip, offset);
419 slpmregs[nmk_chip->
bank] |=
BIT(offset);
421 slpmregs[nmk_chip->
bank] &= ~
BIT(offset);
423 __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
425 __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
440 static void nmk_gpio_glitch_slpm_init(
unsigned int *slpm)
446 unsigned int temp = slpm[
i];
458 static void nmk_gpio_glitch_slpm_restore(
unsigned int *slpm)
474 static int __nmk_config_pins(
pin_cfg_t *cfgs,
int num,
bool sleep)
482 for (i = 0; i < num; i++) {
492 memset(slpm, 0xff,
sizeof(slpm));
494 for (i = 0; i < num; i++) {
502 nmk_gpio_glitch_slpm_init(slpm);
505 for (i = 0; i < num; i++) {
516 spin_lock(&nmk_chip->
lock);
518 cfgs[i], sleep, glitch ? slpm :
NULL);
519 spin_unlock(&nmk_chip->
lock);
524 nmk_gpio_glitch_slpm_restore(slpm);
526 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
547 return __nmk_config_pins(&cfg, 1, sleep);
561 return __nmk_config_pins(cfgs, num,
false);
567 return __nmk_config_pins(cfgs, num,
true);
607 spin_lock(&nmk_chip->
lock);
611 spin_unlock(&nmk_chip->
lock);
612 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
643 spin_unlock_irqrestore(&nmk_chip->
lock, flags);
671 spin_unlock_irqrestore(&nmk_chip->
lock, flags);
702 static inline int nmk_gpio_get_bitmask(
int gpio)
707 static void nmk_gpio_irq_ack(
struct irq_data *d)
711 nmk_chip = irq_data_get_irq_chip_data(d);
725 static void __nmk_gpio_irq_modify(
struct nmk_gpio_chip *nmk_chip,
738 rimscval = &nmk_chip->
rimsc;
739 fimscval = &nmk_chip->
fimsc;
743 rimscval = &nmk_chip->
rwimsc;
744 fimscval = &nmk_chip->
fwimsc;
752 *rimscval &= ~bitmask;
759 *fimscval &= ~bitmask;
764 static void __nmk_gpio_set_wake(
struct nmk_gpio_chip *nmk_chip,
777 __nmk_gpio_irq_modify(nmk_chip, gpio,
WAKE, on);
780 static int nmk_gpio_irq_maskunmask(
struct irq_data *d,
bool enable)
786 nmk_chip = irq_data_get_irq_chip_data(d);
787 bitmask = nmk_gpio_get_bitmask(d->
hwirq);
793 spin_lock(&nmk_chip->
lock);
795 __nmk_gpio_irq_modify(nmk_chip, d->
hwirq,
NORMAL, enable);
798 __nmk_gpio_set_wake(nmk_chip, d->
hwirq, enable);
800 spin_unlock(&nmk_chip->
lock);
801 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
807 static void nmk_gpio_irq_mask(
struct irq_data *d)
809 nmk_gpio_irq_maskunmask(d,
false);
812 static void nmk_gpio_irq_unmask(
struct irq_data *d)
814 nmk_gpio_irq_maskunmask(d,
true);
817 static int nmk_gpio_irq_set_wake(
struct irq_data *d,
unsigned int on)
823 nmk_chip = irq_data_get_irq_chip_data(d);
826 bitmask = nmk_gpio_get_bitmask(d->
hwirq);
830 spin_lock(&nmk_chip->
lock);
832 if (irqd_irq_disabled(d))
833 __nmk_gpio_set_wake(nmk_chip, d->
hwirq, on);
840 spin_unlock(&nmk_chip->
lock);
841 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
847 static int nmk_gpio_irq_set_type(
struct irq_data *d,
unsigned int type)
849 bool enabled = !irqd_irq_disabled(d);
850 bool wake = irqd_is_wakeup_set(d);
855 nmk_chip = irq_data_get_irq_chip_data(d);
856 bitmask = nmk_gpio_get_bitmask(d->
hwirq);
868 __nmk_gpio_irq_modify(nmk_chip, d->
hwirq,
NORMAL,
false);
871 __nmk_gpio_irq_modify(nmk_chip, d->
hwirq,
WAKE,
false);
882 __nmk_gpio_irq_modify(nmk_chip, d->
hwirq,
NORMAL,
true);
885 __nmk_gpio_irq_modify(nmk_chip, d->
hwirq,
WAKE,
true);
887 spin_unlock_irqrestore(&nmk_chip->
lock, flags);
893 static unsigned int nmk_gpio_irq_startup(
struct irq_data *d)
895 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
898 nmk_gpio_irq_unmask(d);
902 static void nmk_gpio_irq_shutdown(
struct irq_data *d)
904 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
906 nmk_gpio_irq_mask(d);
910 static struct irq_chip nmk_gpio_irq_chip = {
911 .name =
"Nomadik-GPIO",
912 .irq_ack = nmk_gpio_irq_ack,
913 .irq_mask = nmk_gpio_irq_mask,
914 .irq_unmask = nmk_gpio_irq_unmask,
915 .irq_set_type = nmk_gpio_irq_set_type,
916 .irq_set_wake = nmk_gpio_irq_set_wake,
917 .irq_startup = nmk_gpio_irq_startup,
918 .irq_shutdown = nmk_gpio_irq_shutdown,
922 static void __nmk_gpio_irq_handler(
unsigned int irq,
struct irq_desc *
desc,
926 struct irq_chip *host_chip = irq_get_chip(irq);
928 chained_irq_enter(host_chip, desc);
930 nmk_chip = irq_get_handler_data(irq);
932 int bit =
__ffs(status);
938 chained_irq_exit(host_chip, desc);
941 static void nmk_gpio_irq_handler(
unsigned int irq,
struct irq_desc *desc)
950 __nmk_gpio_irq_handler(irq, desc, status);
953 static void nmk_gpio_secondary_irq_handler(
unsigned int irq,
959 __nmk_gpio_irq_handler(irq, desc, status);
964 irq_set_chained_handler(nmk_chip->
parent_irq, nmk_gpio_irq_handler);
969 nmk_gpio_secondary_irq_handler);
978 static int nmk_gpio_request(
struct gpio_chip *
chip,
unsigned offset)
984 int gpio = chip->base +
offset;
989 static void nmk_gpio_free(
struct gpio_chip *
chip,
unsigned offset)
991 int gpio = chip->base +
offset;
996 static int nmk_gpio_make_input(
struct gpio_chip *
chip,
unsigned offset)
1010 static int nmk_gpio_get_input(
struct gpio_chip *
chip,
unsigned offset)
1026 static void nmk_gpio_set_output(
struct gpio_chip *
chip,
unsigned offset,
1034 __nmk_gpio_set_output(nmk_chip, offset, val);
1039 static int nmk_gpio_make_output(
struct gpio_chip *
chip,
unsigned offset,
1047 __nmk_gpio_make_output(nmk_chip, offset, val);
1054 static int nmk_gpio_to_irq(
struct gpio_chip *
chip,
unsigned offset)
1062 #ifdef CONFIG_DEBUG_FS
1066 static void nmk_gpio_dbg_show_one(
struct seq_file *
s,
struct gpio_chip *
chip,
1067 unsigned offset,
unsigned gpio)
1076 const char *modes[] = {
1088 seq_printf(s,
" gpio-%-3d (%-20.20s) %s %s %s %s",
1089 gpio, label ?:
"(none)",
1090 is_out ?
"out" :
"in ",
1092 ? (chip->get(chip, offset) ?
"hi" :
"lo")
1094 (mode < 0) ?
"unknown" : modes[
mode],
1095 pull ?
"pull" :
"none");
1097 if (label && !is_out) {
1104 if (irq >= 0 && desc->
action) {
1106 u32 bitmask = nmk_gpio_get_bitmask(gpio);
1109 trigger =
"edge-rising";
1111 trigger =
"edge-falling";
1113 trigger =
"edge-undefined";
1117 irqd_is_wakeup_set(&desc->
irq_data)
1127 unsigned gpio = chip->base;
1129 for (i = 0; i < chip->ngpio; i++, gpio++) {
1130 nmk_gpio_dbg_show_one(s, chip, i, gpio);
1136 static inline void nmk_gpio_dbg_show_one(
struct seq_file *s,
1137 struct gpio_chip *chip,
1138 unsigned offset,
unsigned gpio)
1141 #define nmk_gpio_dbg_show NULL
1145 static struct gpio_chip nmk_gpio_template = {
1146 .request = nmk_gpio_request,
1147 .free = nmk_gpio_free,
1148 .direction_input = nmk_gpio_make_input,
1149 .get = nmk_gpio_get_input,
1150 .direction_output = nmk_gpio_make_output,
1151 .set = nmk_gpio_set_output,
1152 .to_irq = nmk_gpio_to_irq,
1243 if (gpio_bank < NUM_BANKS) {
1279 struct gpio_chip *
chip;
1288 if (!pdata && !np) {
1289 dev_err(&dev->
dev,
"No platform data or device tree found\n");
1301 if (of_property_read_u32(np,
"gpio-bank", &dev->
id)) {
1302 dev_err(&dev->
dev,
"gpio-bank property not found\n");
1352 nmk_chip->
bank = dev->
id;
1353 nmk_chip->
clk = clk;
1354 nmk_chip->
addr = base;
1355 nmk_chip->
chip = nmk_gpio_template;
1363 chip = &nmk_chip->
chip;
1366 chip->label = pdata->
name ?: dev_name(&dev->
dev);
1367 chip->dev = &dev->
dev;
1374 #ifdef CONFIG_OF_GPIO
1384 nmk_gpio_chips[nmk_chip->
bank] = nmk_chip;
1386 platform_set_drvdata(dev, nmk_chip);
1392 &nmk_gpio_irq_simple_ops, nmk_chip);
1394 dev_err(&dev->
dev,
"failed to create irqdomain\n");
1399 nmk_gpio_init_irq(nmk_chip);
1406 dev_err(&dev->
dev,
"Failure %i for GPIO %i-%i\n", ret,
1412 static int nmk_get_groups_cnt(
struct pinctrl_dev *pctldev)
1416 return npct->
soc->ngroups;
1419 static const char *nmk_get_group_name(
struct pinctrl_dev *pctldev,
1424 return npct->
soc->groups[selector].name;
1428 const unsigned **
pins,
1433 *pins = npct->
soc->groups[selector].pins;
1434 *num_pins = npct->
soc->groups[selector].npins;
1438 static struct pinctrl_gpio_range *
1439 nmk_match_gpio_range(
struct pinctrl_dev *pctldev,
unsigned offset)
1444 for (i = 0; i < npct->
soc->gpio_num_ranges; i++) {
1445 struct pinctrl_gpio_range *
range;
1447 range = &npct->
soc->gpio_ranges[
i];
1448 if (offset >= range->pin_base &&
1449 offset <= (range->pin_base + range->npins - 1))
1458 struct pinctrl_gpio_range *
range;
1459 struct gpio_chip *
chip;
1461 range = nmk_match_gpio_range(pctldev, offset);
1462 if (!range || !range->gc) {
1467 nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
1470 static struct pinctrl_ops nmk_pinctrl_ops = {
1471 .get_groups_count = nmk_get_groups_cnt,
1472 .get_group_name = nmk_get_group_name,
1473 .get_group_pins = nmk_get_group_pins,
1474 .pin_dbg_show = nmk_pin_dbg_show,
1477 static int nmk_pmx_get_funcs_cnt(
struct pinctrl_dev *pctldev)
1481 return npct->
soc->nfunctions;
1484 static const char *nmk_pmx_get_func_name(
struct pinctrl_dev *pctldev,
1489 return npct->
soc->functions[
function].name;
1492 static int nmk_pmx_get_func_groups(
struct pinctrl_dev *pctldev,
1494 const char *
const **groups,
1495 unsigned *
const num_groups)
1499 *groups = npct->
soc->functions[
function].groups;
1500 *num_groups = npct->
soc->functions[
function].ngroups;
1505 static int nmk_pmx_enable(
struct pinctrl_dev *pctldev,
unsigned function,
1511 unsigned long flags;
1550 memset(slpm, 0xff,
sizeof(slpm));
1556 for (i = 0; i < g->
npins; i++)
1558 nmk_gpio_glitch_slpm_init(slpm);
1561 for (i = 0; i < g->
npins; i++) {
1562 struct pinctrl_gpio_range *
range;
1564 struct gpio_chip *
chip;
1567 range = nmk_match_gpio_range(pctldev, g->
pins[i]);
1570 "invalid pin offset %d in group %s at index %d\n",
1575 dev_err(npct->
dev,
"GPIO chip missing in range for pin offset %d in group %s at index %d\n",
1592 nmk_gpio_disable_lazy_irq(nmk_chip, bit);
1594 __nmk_gpio_set_mode_safe(nmk_chip, bit,
1607 nmk_prcm_altcx_set_mode(npct, g->
pins[i],
1616 nmk_gpio_glitch_slpm_restore(slpm);
1617 spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
1623 static void nmk_pmx_disable(
struct pinctrl_dev *pctldev,
1624 unsigned function,
unsigned group)
1639 struct pinctrl_gpio_range *range,
1644 struct gpio_chip *
chip;
1652 dev_err(npct->
dev,
"missing GPIO chip in range\n");
1658 dev_dbg(npct->
dev,
"enable pin %u as GPIO\n", offset);
1670 struct pinctrl_gpio_range *range,
1675 dev_dbg(npct->
dev,
"disable pin %u as GPIO\n", offset);
1679 static struct pinmux_ops nmk_pinmux_ops = {
1680 .get_functions_count = nmk_pmx_get_funcs_cnt,
1681 .get_function_name = nmk_pmx_get_func_name,
1682 .get_function_groups = nmk_pmx_get_func_groups,
1683 .enable = nmk_pmx_enable,
1684 .disable = nmk_pmx_disable,
1701 static const char *pullnames[] = {
1707 static const char *slpmnames[] = {
1713 struct pinctrl_gpio_range *
range;
1714 struct gpio_chip *
chip;
1731 range = nmk_match_gpio_range(pctldev, pin);
1733 dev_err(npct->
dev,
"invalid pin offset %d\n", pin);
1737 dev_err(npct->
dev,
"GPIO chip missing in range for pin %d\n",
1757 pull = slpm_pull - 1;
1759 output = slpm_output - 1;
1763 dev_dbg(nmk_chip->
chip.dev,
"pin %d: sleep pull %s, dir %s, val %s\n",
1765 slpm_pull ? pullnames[pull] :
"same",
1766 slpm_output ? (output ?
"output" :
"input") :
"same",
1767 slpm_val ? (val ?
"high" :
"low") :
"same");
1770 dev_dbg(nmk_chip->
chip.dev,
"pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
1771 pin, cfg, pullnames[pull], slpmnames[slpm],
1772 output ?
"output " :
"input",
1773 output ? (val ?
"high" :
"low") :
"",
1774 lowemi ?
"on" :
"off" );
1782 __nmk_gpio_make_output(nmk_chip, bit, val);
1784 __nmk_gpio_make_input(nmk_chip, bit);
1785 __nmk_gpio_set_pull(nmk_chip, bit, pull);
1788 __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
1790 __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
1795 static struct pinconf_ops nmk_pinconf_ops = {
1800 static struct pinctrl_desc nmk_pinctrl_desc = {
1801 .name =
"pinctrl-nomadik",
1802 .pctlops = &nmk_pinctrl_ops,
1803 .pmxops = &nmk_pinmux_ops,
1804 .confops = &nmk_pinconf_ops,
1808 static const struct of_device_id nmk_pinctrl_match[] = {
1810 .compatible =
"stericsson,nmk_pinctrl",
1829 version = platid->driver_data;
1831 version = (
unsigned int)
1847 for (i = 0; i < npct->
soc->gpio_num_ranges; i++) {
1848 if (!nmk_gpio_chips[i]) {
1849 dev_warn(&pdev->
dev,
"GPIO chip %d not registered yet\n", i);
1852 npct->
soc->gpio_ranges[
i].gc = &nmk_gpio_chips[
i]->
chip;
1855 nmk_pinctrl_desc.pins = npct->
soc->pins;
1856 nmk_pinctrl_desc.npins = npct->
soc->npins;
1860 dev_err(&pdev->
dev,
"could not register Nomadik pinctrl driver\n");
1865 for (i = 0; i < npct->
soc->gpio_num_ranges; i++)
1868 platform_set_drvdata(pdev, npct);
1869 dev_info(&pdev->
dev,
"initialized Nomadik pin control driver\n");
1875 { .compatible =
"st,nomadik-gpio", },
1883 .of_match_table = nmk_gpio_match,
1885 .probe = nmk_gpio_probe,
1897 .name =
"pinctrl-nomadik",
1898 .of_match_table = nmk_pinctrl_match,
1900 .probe = nmk_pinctrl_probe,
1901 .id_table = nmk_pinctrl_id,
1904 static int __init nmk_gpio_init(
void)