.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Generic GPIO driver for logic cells found in the Nomadik SoC |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it> |
---|
6 | 7 | * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com> |
---|
7 | 8 | * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org> |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify |
---|
10 | | - * it under the terms of the GNU General Public License version 2 as |
---|
11 | | - * published by the Free Software Foundation. |
---|
12 | 9 | */ |
---|
13 | 10 | #include <linux/kernel.h> |
---|
14 | 11 | #include <linux/init.h> |
---|
.. | .. |
---|
17 | 14 | #include <linux/io.h> |
---|
18 | 15 | #include <linux/clk.h> |
---|
19 | 16 | #include <linux/err.h> |
---|
20 | | -#include <linux/gpio.h> |
---|
| 17 | +#include <linux/gpio/driver.h> |
---|
21 | 18 | #include <linux/spinlock.h> |
---|
22 | 19 | #include <linux/interrupt.h> |
---|
23 | 20 | #include <linux/slab.h> |
---|
.. | .. |
---|
203 | 200 | |
---|
204 | 201 | #define GPIO_BLOCK_SHIFT 5 |
---|
205 | 202 | #define NMK_GPIO_PER_CHIP (1 << GPIO_BLOCK_SHIFT) |
---|
206 | | -#define NMK_MAX_BANKS DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP) |
---|
| 203 | +#define NMK_MAX_BANKS DIV_ROUND_UP(512, NMK_GPIO_PER_CHIP) |
---|
207 | 204 | |
---|
208 | 205 | /* Register in the logic block */ |
---|
209 | 206 | #define NMK_GPIO_DAT 0x00 |
---|
.. | .. |
---|
251 | 248 | void __iomem *addr; |
---|
252 | 249 | struct clk *clk; |
---|
253 | 250 | unsigned int bank; |
---|
254 | | - unsigned int parent_irq; |
---|
255 | | - int latent_parent_irq; |
---|
256 | | - u32 (*get_latent_status)(unsigned int bank); |
---|
257 | 251 | void (*set_ioforce)(bool enable); |
---|
258 | 252 | spinlock_t lock; |
---|
259 | 253 | bool sleepmode; |
---|
.. | .. |
---|
805 | 799 | clk_disable(nmk_chip->clk); |
---|
806 | 800 | } |
---|
807 | 801 | |
---|
808 | | -static void __nmk_gpio_irq_handler(struct irq_desc *desc, u32 status) |
---|
| 802 | +static void nmk_gpio_irq_handler(struct irq_desc *desc) |
---|
809 | 803 | { |
---|
810 | 804 | struct irq_chip *host_chip = irq_desc_get_chip(desc); |
---|
811 | 805 | struct gpio_chip *chip = irq_desc_get_handler_data(desc); |
---|
| 806 | + struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); |
---|
| 807 | + u32 status; |
---|
812 | 808 | |
---|
813 | 809 | chained_irq_enter(host_chip, desc); |
---|
| 810 | + |
---|
| 811 | + clk_enable(nmk_chip->clk); |
---|
| 812 | + status = readl(nmk_chip->addr + NMK_GPIO_IS); |
---|
| 813 | + clk_disable(nmk_chip->clk); |
---|
814 | 814 | |
---|
815 | 815 | while (status) { |
---|
816 | 816 | int bit = __ffs(status); |
---|
.. | .. |
---|
822 | 822 | chained_irq_exit(host_chip, desc); |
---|
823 | 823 | } |
---|
824 | 824 | |
---|
825 | | -static void nmk_gpio_irq_handler(struct irq_desc *desc) |
---|
826 | | -{ |
---|
827 | | - struct gpio_chip *chip = irq_desc_get_handler_data(desc); |
---|
828 | | - struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); |
---|
829 | | - u32 status; |
---|
830 | | - |
---|
831 | | - clk_enable(nmk_chip->clk); |
---|
832 | | - status = readl(nmk_chip->addr + NMK_GPIO_IS); |
---|
833 | | - clk_disable(nmk_chip->clk); |
---|
834 | | - |
---|
835 | | - __nmk_gpio_irq_handler(desc, status); |
---|
836 | | -} |
---|
837 | | - |
---|
838 | | -static void nmk_gpio_latent_irq_handler(struct irq_desc *desc) |
---|
839 | | -{ |
---|
840 | | - struct gpio_chip *chip = irq_desc_get_handler_data(desc); |
---|
841 | | - struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); |
---|
842 | | - u32 status = nmk_chip->get_latent_status(nmk_chip->bank); |
---|
843 | | - |
---|
844 | | - __nmk_gpio_irq_handler(desc, status); |
---|
845 | | -} |
---|
846 | | - |
---|
847 | 825 | /* I/O Functions */ |
---|
848 | 826 | |
---|
849 | 827 | static int nmk_gpio_get_dir(struct gpio_chip *chip, unsigned offset) |
---|
.. | .. |
---|
853 | 831 | |
---|
854 | 832 | clk_enable(nmk_chip->clk); |
---|
855 | 833 | |
---|
856 | | - dir = !(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset)); |
---|
| 834 | + dir = readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset); |
---|
857 | 835 | |
---|
858 | 836 | clk_disable(nmk_chip->clk); |
---|
859 | 837 | |
---|
860 | | - return dir; |
---|
| 838 | + if (dir) |
---|
| 839 | + return GPIO_LINE_DIRECTION_OUT; |
---|
| 840 | + |
---|
| 841 | + return GPIO_LINE_DIRECTION_IN; |
---|
861 | 842 | } |
---|
862 | 843 | |
---|
863 | 844 | static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset) |
---|
.. | .. |
---|
950 | 931 | [NMK_GPIO_ALT_C+3] = "altC3", |
---|
951 | 932 | [NMK_GPIO_ALT_C+4] = "altC4", |
---|
952 | 933 | }; |
---|
953 | | - const char *pulls[] = { |
---|
954 | | - "none ", |
---|
955 | | - "pull down", |
---|
956 | | - "pull up ", |
---|
957 | | - }; |
---|
958 | 934 | |
---|
959 | 935 | clk_enable(nmk_chip->clk); |
---|
960 | 936 | is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset)); |
---|
.. | .. |
---|
965 | 941 | mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio); |
---|
966 | 942 | |
---|
967 | 943 | if (is_out) { |
---|
968 | | - seq_printf(s, " gpio-%-3d (%-20.20s) out %s %s", |
---|
| 944 | + seq_printf(s, " gpio-%-3d (%-20.20s) out %s %s", |
---|
969 | 945 | gpio, |
---|
970 | 946 | label ?: "(none)", |
---|
971 | 947 | data_out ? "hi" : "lo", |
---|
972 | 948 | (mode < 0) ? "unknown" : modes[mode]); |
---|
973 | 949 | } else { |
---|
974 | | - int irq = gpio_to_irq(gpio); |
---|
| 950 | + int irq = chip->to_irq(chip, offset); |
---|
975 | 951 | struct irq_desc *desc = irq_to_desc(irq); |
---|
976 | | - int pullidx = 0; |
---|
| 952 | + const int pullidx = pull ? 1 : 0; |
---|
977 | 953 | int val; |
---|
978 | | - |
---|
979 | | - if (pull) |
---|
980 | | - pullidx = data_out ? 2 : 1; |
---|
| 954 | + static const char * const pulls[] = { |
---|
| 955 | + "none ", |
---|
| 956 | + "pull enabled", |
---|
| 957 | + }; |
---|
981 | 958 | |
---|
982 | 959 | seq_printf(s, " gpio-%-3d (%-20.20s) in %s %s", |
---|
983 | 960 | gpio, |
---|
.. | .. |
---|
1051 | 1028 | |
---|
1052 | 1029 | gpio_pdev = of_find_device_by_node(np); |
---|
1053 | 1030 | if (!gpio_pdev) { |
---|
1054 | | - pr_err("populate \"%s\": device not found\n", np->name); |
---|
| 1031 | + pr_err("populate \"%pOFn\": device not found\n", np); |
---|
1055 | 1032 | return ERR_PTR(-ENODEV); |
---|
1056 | 1033 | } |
---|
1057 | 1034 | if (of_property_read_u32(np, "gpio-bank", &id)) { |
---|
1058 | 1035 | dev_err(&pdev->dev, "populate: gpio-bank property not found\n"); |
---|
| 1036 | + platform_device_put(gpio_pdev); |
---|
1059 | 1037 | return ERR_PTR(-EINVAL); |
---|
1060 | 1038 | } |
---|
1061 | 1039 | |
---|
1062 | 1040 | /* Already populated? */ |
---|
1063 | 1041 | nmk_chip = nmk_gpio_chips[id]; |
---|
1064 | | - if (nmk_chip) |
---|
| 1042 | + if (nmk_chip) { |
---|
| 1043 | + platform_device_put(gpio_pdev); |
---|
1065 | 1044 | return nmk_chip; |
---|
| 1045 | + } |
---|
1066 | 1046 | |
---|
1067 | 1047 | nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL); |
---|
1068 | | - if (!nmk_chip) |
---|
| 1048 | + if (!nmk_chip) { |
---|
| 1049 | + platform_device_put(gpio_pdev); |
---|
1069 | 1050 | return ERR_PTR(-ENOMEM); |
---|
| 1051 | + } |
---|
1070 | 1052 | |
---|
1071 | 1053 | nmk_chip->bank = id; |
---|
1072 | 1054 | chip = &nmk_chip->chip; |
---|
.. | .. |
---|
1077 | 1059 | |
---|
1078 | 1060 | res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0); |
---|
1079 | 1061 | base = devm_ioremap_resource(&pdev->dev, res); |
---|
1080 | | - if (IS_ERR(base)) |
---|
| 1062 | + if (IS_ERR(base)) { |
---|
| 1063 | + platform_device_put(gpio_pdev); |
---|
1081 | 1064 | return ERR_CAST(base); |
---|
| 1065 | + } |
---|
1082 | 1066 | nmk_chip->addr = base; |
---|
1083 | 1067 | |
---|
1084 | 1068 | clk = clk_get(&gpio_pdev->dev, NULL); |
---|
1085 | | - if (IS_ERR(clk)) |
---|
| 1069 | + if (IS_ERR(clk)) { |
---|
| 1070 | + platform_device_put(gpio_pdev); |
---|
1086 | 1071 | return (void *) clk; |
---|
| 1072 | + } |
---|
1087 | 1073 | clk_prepare(clk); |
---|
1088 | 1074 | nmk_chip->clk = clk; |
---|
1089 | 1075 | |
---|
.. | .. |
---|
1097 | 1083 | struct device_node *np = dev->dev.of_node; |
---|
1098 | 1084 | struct nmk_gpio_chip *nmk_chip; |
---|
1099 | 1085 | struct gpio_chip *chip; |
---|
| 1086 | + struct gpio_irq_chip *girq; |
---|
1100 | 1087 | struct irq_chip *irqchip; |
---|
1101 | | - int latent_irq; |
---|
1102 | 1088 | bool supports_sleepmode; |
---|
1103 | 1089 | int irq; |
---|
1104 | 1090 | int ret; |
---|
.. | .. |
---|
1119 | 1105 | if (irq < 0) |
---|
1120 | 1106 | return irq; |
---|
1121 | 1107 | |
---|
1122 | | - /* It's OK for this IRQ not to be present */ |
---|
1123 | | - latent_irq = platform_get_irq(dev, 1); |
---|
1124 | | - |
---|
1125 | 1108 | /* |
---|
1126 | 1109 | * The virt address in nmk_chip->addr is in the nomadik register space, |
---|
1127 | 1110 | * so we can simply convert the resource address, without remapping |
---|
1128 | 1111 | */ |
---|
1129 | | - nmk_chip->parent_irq = irq; |
---|
1130 | | - nmk_chip->latent_parent_irq = latent_irq; |
---|
1131 | 1112 | nmk_chip->sleepmode = supports_sleepmode; |
---|
1132 | 1113 | spin_lock_init(&nmk_chip->lock); |
---|
1133 | 1114 | |
---|
.. | .. |
---|
1157 | 1138 | chip->base, |
---|
1158 | 1139 | chip->base + chip->ngpio - 1); |
---|
1159 | 1140 | |
---|
| 1141 | + girq = &chip->irq; |
---|
| 1142 | + girq->chip = irqchip; |
---|
| 1143 | + girq->parent_handler = nmk_gpio_irq_handler; |
---|
| 1144 | + girq->num_parents = 1; |
---|
| 1145 | + girq->parents = devm_kcalloc(&dev->dev, 1, |
---|
| 1146 | + sizeof(*girq->parents), |
---|
| 1147 | + GFP_KERNEL); |
---|
| 1148 | + if (!girq->parents) |
---|
| 1149 | + return -ENOMEM; |
---|
| 1150 | + girq->parents[0] = irq; |
---|
| 1151 | + girq->default_type = IRQ_TYPE_NONE; |
---|
| 1152 | + girq->handler = handle_edge_irq; |
---|
| 1153 | + |
---|
1160 | 1154 | clk_enable(nmk_chip->clk); |
---|
1161 | 1155 | nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI); |
---|
1162 | 1156 | clk_disable(nmk_chip->clk); |
---|
.. | .. |
---|
1168 | 1162 | |
---|
1169 | 1163 | platform_set_drvdata(dev, nmk_chip); |
---|
1170 | 1164 | |
---|
1171 | | - /* |
---|
1172 | | - * Let the generic code handle this edge IRQ, the the chained |
---|
1173 | | - * handler will perform the actual work of handling the parent |
---|
1174 | | - * interrupt. |
---|
1175 | | - */ |
---|
1176 | | - ret = gpiochip_irqchip_add(chip, |
---|
1177 | | - irqchip, |
---|
1178 | | - 0, |
---|
1179 | | - handle_edge_irq, |
---|
1180 | | - IRQ_TYPE_NONE); |
---|
1181 | | - if (ret) { |
---|
1182 | | - dev_err(&dev->dev, "could not add irqchip\n"); |
---|
1183 | | - gpiochip_remove(&nmk_chip->chip); |
---|
1184 | | - return -ENODEV; |
---|
1185 | | - } |
---|
1186 | | - /* Then register the chain on the parent IRQ */ |
---|
1187 | | - gpiochip_set_chained_irqchip(chip, |
---|
1188 | | - irqchip, |
---|
1189 | | - nmk_chip->parent_irq, |
---|
1190 | | - nmk_gpio_irq_handler); |
---|
1191 | | - if (nmk_chip->latent_parent_irq > 0) |
---|
1192 | | - gpiochip_set_chained_irqchip(chip, |
---|
1193 | | - irqchip, |
---|
1194 | | - nmk_chip->latent_parent_irq, |
---|
1195 | | - nmk_gpio_latent_irq_handler); |
---|
1196 | | - |
---|
1197 | | - dev_info(&dev->dev, "at address %p\n", nmk_chip->addr); |
---|
| 1165 | + dev_info(&dev->dev, "chip registered\n"); |
---|
1198 | 1166 | |
---|
1199 | 1167 | return 0; |
---|
1200 | 1168 | } |
---|
.. | .. |
---|
1371 | 1339 | |
---|
1372 | 1340 | static int nmk_dt_pin_config(int index, int val, unsigned long *config) |
---|
1373 | 1341 | { |
---|
1374 | | - int ret = 0; |
---|
1375 | | - |
---|
1376 | 1342 | if (nmk_cfg_params[index].choice == NULL) |
---|
1377 | 1343 | *config = nmk_cfg_params[index].config; |
---|
1378 | 1344 | else { |
---|
.. | .. |
---|
1382 | 1348 | nmk_cfg_params[index].choice[val]; |
---|
1383 | 1349 | } |
---|
1384 | 1350 | } |
---|
1385 | | - return ret; |
---|
| 1351 | + return 0; |
---|
1386 | 1352 | } |
---|
1387 | 1353 | |
---|
1388 | 1354 | static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name) |
---|
.. | .. |
---|
1455 | 1421 | |
---|
1456 | 1422 | has_config = nmk_pinctrl_dt_get_config(np, &configs); |
---|
1457 | 1423 | np_config = of_parse_phandle(np, "ste,config", 0); |
---|
1458 | | - if (np_config) |
---|
| 1424 | + if (np_config) { |
---|
1459 | 1425 | has_config |= nmk_pinctrl_dt_get_config(np_config, &configs); |
---|
| 1426 | + of_node_put(np_config); |
---|
| 1427 | + } |
---|
1460 | 1428 | if (has_config) { |
---|
1461 | 1429 | const char *gpio_name; |
---|
1462 | 1430 | const char *pin; |
---|
.. | .. |
---|
1502 | 1470 | &reserved_maps, num_maps); |
---|
1503 | 1471 | if (ret < 0) { |
---|
1504 | 1472 | pinctrl_utils_free_map(pctldev, *map, *num_maps); |
---|
| 1473 | + of_node_put(np); |
---|
1505 | 1474 | return ret; |
---|
1506 | 1475 | } |
---|
1507 | 1476 | } |
---|
.. | .. |
---|
1904 | 1873 | gpio_np = of_parse_phandle(np, "nomadik-gpio-chips", i); |
---|
1905 | 1874 | if (gpio_np) { |
---|
1906 | 1875 | dev_info(&pdev->dev, |
---|
1907 | | - "populate NMK GPIO %d \"%s\"\n", |
---|
1908 | | - i, gpio_np->name); |
---|
| 1876 | + "populate NMK GPIO %d \"%pOFn\"\n", |
---|
| 1877 | + i, gpio_np); |
---|
1909 | 1878 | nmk_chip = nmk_gpio_populate_chip(gpio_np, pdev); |
---|
1910 | 1879 | if (IS_ERR(nmk_chip)) |
---|
1911 | 1880 | dev_err(&pdev->dev, |
---|
.. | .. |
---|
1916 | 1885 | } |
---|
1917 | 1886 | |
---|
1918 | 1887 | prcm_np = of_parse_phandle(np, "prcm", 0); |
---|
1919 | | - if (prcm_np) |
---|
| 1888 | + if (prcm_np) { |
---|
1920 | 1889 | npct->prcm_base = of_iomap(prcm_np, 0); |
---|
| 1890 | + of_node_put(prcm_np); |
---|
| 1891 | + } |
---|
1921 | 1892 | if (!npct->prcm_base) { |
---|
1922 | 1893 | if (version == PINCTRL_NMK_STN8815) { |
---|
1923 | 1894 | dev_info(&pdev->dev, |
---|