Skip to content

Commit a0b957f

Browse files
plagniojlinusw
authored andcommitted
pinctrl: at91: allow to have disabled gpio bank
Today we expect that all the bank are enabled, and count the number of banks used by the pinctrl based on it instead of using the last bank id enabled. So switch to it, set the chained IRQ at runtime based on enabled banks and wait only the number of enabled gpio controllers at probe time. Cc: <stable@vger.kernel.org> # 3.18 Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Signed-off-by: Ludovic Desroches <ludovic.desroches@atmel.com> Acked-by: Nicolas Ferre <nicolas.ferre@atmel.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 26bc420 commit a0b957f

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

drivers/pinctrl/pinctrl-at91.c

+55-53
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ struct at91_pinctrl {
177177
struct device *dev;
178178
struct pinctrl_dev *pctl;
179179

180-
int nbanks;
180+
int nactive_banks;
181181

182182
uint32_t *mux_mask;
183183
int nmux;
@@ -653,12 +653,18 @@ static int pin_check_config(struct at91_pinctrl *info, const char *name,
653653
int mux;
654654

655655
/* check if it's a valid config */
656-
if (pin->bank >= info->nbanks) {
656+
if (pin->bank >= gpio_banks) {
657657
dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
658-
name, index, pin->bank, info->nbanks);
658+
name, index, pin->bank, gpio_banks);
659659
return -EINVAL;
660660
}
661661

662+
if (!gpio_chips[pin->bank]) {
663+
dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
664+
name, index, pin->bank);
665+
return -ENXIO;
666+
}
667+
662668
if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
663669
dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
664670
name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
@@ -981,7 +987,8 @@ static void at91_pinctrl_child_count(struct at91_pinctrl *info,
981987

982988
for_each_child_of_node(np, child) {
983989
if (of_device_is_compatible(child, gpio_compat)) {
984-
info->nbanks++;
990+
if (of_device_is_available(child))
991+
info->nactive_banks++;
985992
} else {
986993
info->nfunctions++;
987994
info->ngroups += of_get_child_count(child);
@@ -1003,11 +1010,11 @@ static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
10031010
}
10041011

10051012
size /= sizeof(*list);
1006-
if (!size || size % info->nbanks) {
1007-
dev_err(info->dev, "wrong mux mask array should be by %d\n", info->nbanks);
1013+
if (!size || size % gpio_banks) {
1014+
dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
10081015
return -EINVAL;
10091016
}
1010-
info->nmux = size / info->nbanks;
1017+
info->nmux = size / gpio_banks;
10111018

10121019
info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
10131020
if (!info->mux_mask) {
@@ -1131,7 +1138,7 @@ static int at91_pinctrl_probe_dt(struct platform_device *pdev,
11311138
of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
11321139
at91_pinctrl_child_count(info, np);
11331140

1134-
if (info->nbanks < 1) {
1141+
if (gpio_banks < 1) {
11351142
dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
11361143
return -EINVAL;
11371144
}
@@ -1144,7 +1151,7 @@ static int at91_pinctrl_probe_dt(struct platform_device *pdev,
11441151

11451152
dev_dbg(&pdev->dev, "mux-mask\n");
11461153
tmp = info->mux_mask;
1147-
for (i = 0; i < info->nbanks; i++) {
1154+
for (i = 0; i < gpio_banks; i++) {
11481155
for (j = 0; j < info->nmux; j++, tmp++) {
11491156
dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
11501157
}
@@ -1162,7 +1169,7 @@ static int at91_pinctrl_probe_dt(struct platform_device *pdev,
11621169
if (!info->groups)
11631170
return -ENOMEM;
11641171

1165-
dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks);
1172+
dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
11661173
dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
11671174
dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
11681175

@@ -1185,7 +1192,7 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
11851192
{
11861193
struct at91_pinctrl *info;
11871194
struct pinctrl_pin_desc *pdesc;
1188-
int ret, i, j, k;
1195+
int ret, i, j, k, ngpio_chips_enabled = 0;
11891196

11901197
info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
11911198
if (!info)
@@ -1200,23 +1207,27 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
12001207
* to obtain references to the struct gpio_chip * for them, and we
12011208
* need this to proceed.
12021209
*/
1203-
for (i = 0; i < info->nbanks; i++) {
1204-
if (!gpio_chips[i]) {
1205-
dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
1206-
devm_kfree(&pdev->dev, info);
1207-
return -EPROBE_DEFER;
1208-
}
1210+
for (i = 0; i < gpio_banks; i++)
1211+
if (gpio_chips[i])
1212+
ngpio_chips_enabled++;
1213+
1214+
if (ngpio_chips_enabled < info->nactive_banks) {
1215+
dev_warn(&pdev->dev,
1216+
"All GPIO chips are not registered yet (%d/%d)\n",
1217+
ngpio_chips_enabled, info->nactive_banks);
1218+
devm_kfree(&pdev->dev, info);
1219+
return -EPROBE_DEFER;
12091220
}
12101221

12111222
at91_pinctrl_desc.name = dev_name(&pdev->dev);
1212-
at91_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK;
1223+
at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
12131224
at91_pinctrl_desc.pins = pdesc =
12141225
devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
12151226

12161227
if (!at91_pinctrl_desc.pins)
12171228
return -ENOMEM;
12181229

1219-
for (i = 0 , k = 0; i < info->nbanks; i++) {
1230+
for (i = 0, k = 0; i < gpio_banks; i++) {
12201231
for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
12211232
pdesc->number = k;
12221233
pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
@@ -1234,8 +1245,9 @@ static int at91_pinctrl_probe(struct platform_device *pdev)
12341245
}
12351246

12361247
/* We will handle a range of GPIO pins */
1237-
for (i = 0; i < info->nbanks; i++)
1238-
pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
1248+
for (i = 0; i < gpio_banks; i++)
1249+
if (gpio_chips[i])
1250+
pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
12391251

12401252
dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
12411253

@@ -1613,9 +1625,10 @@ static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
16131625
static int at91_gpio_of_irq_setup(struct platform_device *pdev,
16141626
struct at91_gpio_chip *at91_gpio)
16151627
{
1628+
struct gpio_chip *gpiochip_prev = NULL;
16161629
struct at91_gpio_chip *prev = NULL;
16171630
struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
1618-
int ret;
1631+
int ret, i;
16191632

16201633
at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
16211634

@@ -1641,24 +1654,33 @@ static int at91_gpio_of_irq_setup(struct platform_device *pdev,
16411654
return ret;
16421655
}
16431656

1644-
/* Setup chained handler */
1645-
if (at91_gpio->pioc_idx)
1646-
prev = gpio_chips[at91_gpio->pioc_idx - 1];
1647-
16481657
/* The top level handler handles one bank of GPIOs, except
16491658
* on some SoC it can handle up to three...
16501659
* We only set up the handler for the first of the list.
16511660
*/
1652-
if (prev && prev->next == at91_gpio)
1661+
gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1662+
if (!gpiochip_prev) {
1663+
/* Then register the chain on the parent IRQ */
1664+
gpiochip_set_chained_irqchip(&at91_gpio->chip,
1665+
&gpio_irqchip,
1666+
at91_gpio->pioc_virq,
1667+
gpio_irq_handler);
16531668
return 0;
1669+
}
16541670

1655-
/* Then register the chain on the parent IRQ */
1656-
gpiochip_set_chained_irqchip(&at91_gpio->chip,
1657-
&gpio_irqchip,
1658-
at91_gpio->pioc_virq,
1659-
gpio_irq_handler);
1671+
prev = container_of(gpiochip_prev, struct at91_gpio_chip, chip);
16601672

1661-
return 0;
1673+
/* we can only have 2 banks before */
1674+
for (i = 0; i < 2; i++) {
1675+
if (prev->next) {
1676+
prev = prev->next;
1677+
} else {
1678+
prev->next = at91_gpio;
1679+
return 0;
1680+
}
1681+
}
1682+
1683+
return -EINVAL;
16621684
}
16631685

16641686
/* This structure is replicated for each GPIO block allocated at probe time */
@@ -1675,24 +1697,6 @@ static struct gpio_chip at91_gpio_template = {
16751697
.ngpio = MAX_NB_GPIO_PER_BANK,
16761698
};
16771699

1678-
static void at91_gpio_probe_fixup(void)
1679-
{
1680-
unsigned i;
1681-
struct at91_gpio_chip *at91_gpio, *last = NULL;
1682-
1683-
for (i = 0; i < gpio_banks; i++) {
1684-
at91_gpio = gpio_chips[i];
1685-
1686-
/*
1687-
* GPIO controller are grouped on some SoC:
1688-
* PIOC, PIOD and PIOE can share the same IRQ line
1689-
*/
1690-
if (last && last->pioc_virq == at91_gpio->pioc_virq)
1691-
last->next = at91_gpio;
1692-
last = at91_gpio;
1693-
}
1694-
}
1695-
16961700
static struct of_device_id at91_gpio_of_match[] = {
16971701
{ .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
16981702
{ .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
@@ -1805,8 +1809,6 @@ static int at91_gpio_probe(struct platform_device *pdev)
18051809
gpio_chips[alias_idx] = at91_chip;
18061810
gpio_banks = max(gpio_banks, alias_idx + 1);
18071811

1808-
at91_gpio_probe_fixup();
1809-
18101812
ret = at91_gpio_of_irq_setup(pdev, at91_chip);
18111813
if (ret)
18121814
goto irq_setup_err;

0 commit comments

Comments
 (0)