Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Allwinner A1X SoCs pinctrl driver. |
| 3 | * |
| 4 | * Copyright (C) 2012 Maxime Ripard |
| 5 | * |
| 6 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public |
| 9 | * License version 2. This program is licensed "as is" without any |
| 10 | * warranty of any kind, whether express or implied. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/io.h> |
Emilio López | 950707c | 2013-03-22 11:20:40 -0300 | [diff] [blame] | 14 | #include <linux/clk.h> |
Linus Walleij | 88057d6 | 2015-12-08 22:40:43 +0100 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 16 | #include <linux/irqdomain.h> |
Chen-Yu Tsai | 905a511 | 2014-02-11 00:22:37 +0800 | [diff] [blame] | 17 | #include <linux/irqchip/chained_irq.h> |
Paul Gortmaker | bcc7619 | 2016-02-29 15:48:37 -0500 | [diff] [blame] | 18 | #include <linux/export.h> |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_device.h> |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 22 | #include <linux/of_irq.h> |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 23 | #include <linux/pinctrl/consumer.h> |
| 24 | #include <linux/pinctrl/machine.h> |
| 25 | #include <linux/pinctrl/pinctrl.h> |
| 26 | #include <linux/pinctrl/pinconf-generic.h> |
| 27 | #include <linux/pinctrl/pinmux.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/slab.h> |
| 30 | |
Maxime Ripard | 42676fa | 2016-10-11 17:46:00 +0200 | [diff] [blame] | 31 | #include <dt-bindings/pinctrl/sun4i-a10.h> |
| 32 | |
Maxime Ripard | 5f91077 | 2014-04-18 18:53:02 +0200 | [diff] [blame] | 33 | #include "../core.h" |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 34 | #include "pinctrl-sunxi.h" |
Maxime Ripard | eaa3d84 | 2013-01-18 22:30:35 +0100 | [diff] [blame] | 35 | |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 36 | static struct irq_chip sunxi_pinctrl_edge_irq_chip; |
| 37 | static struct irq_chip sunxi_pinctrl_level_irq_chip; |
| 38 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 39 | static struct sunxi_pinctrl_group * |
| 40 | sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < pctl->ngroups; i++) { |
| 45 | struct sunxi_pinctrl_group *grp = pctl->groups + i; |
| 46 | |
| 47 | if (!strcmp(grp->name, group)) |
| 48 | return grp; |
| 49 | } |
| 50 | |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | static struct sunxi_pinctrl_function * |
| 55 | sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl, |
| 56 | const char *name) |
| 57 | { |
| 58 | struct sunxi_pinctrl_function *func = pctl->functions; |
| 59 | int i; |
| 60 | |
| 61 | for (i = 0; i < pctl->nfunctions; i++) { |
| 62 | if (!func[i].name) |
| 63 | break; |
| 64 | |
| 65 | if (!strcmp(func[i].name, name)) |
| 66 | return func + i; |
| 67 | } |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | static struct sunxi_desc_function * |
| 73 | sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl, |
| 74 | const char *pin_name, |
| 75 | const char *func_name) |
| 76 | { |
| 77 | int i; |
| 78 | |
| 79 | for (i = 0; i < pctl->desc->npins; i++) { |
| 80 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
| 81 | |
| 82 | if (!strcmp(pin->pin.name, pin_name)) { |
| 83 | struct sunxi_desc_function *func = pin->functions; |
| 84 | |
| 85 | while (func->name) { |
| 86 | if (!strcmp(func->name, func_name)) |
| 87 | return func; |
| 88 | |
| 89 | func++; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return NULL; |
| 95 | } |
| 96 | |
Maxime Ripard | 814d4f2 | 2013-06-08 12:05:43 +0200 | [diff] [blame] | 97 | static struct sunxi_desc_function * |
| 98 | sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl, |
| 99 | const u16 pin_num, |
| 100 | const char *func_name) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < pctl->desc->npins; i++) { |
| 105 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
| 106 | |
| 107 | if (pin->pin.number == pin_num) { |
| 108 | struct sunxi_desc_function *func = pin->functions; |
| 109 | |
| 110 | while (func->name) { |
| 111 | if (!strcmp(func->name, func_name)) |
| 112 | return func; |
| 113 | |
| 114 | func++; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 122 | static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev) |
| 123 | { |
| 124 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 125 | |
| 126 | return pctl->ngroups; |
| 127 | } |
| 128 | |
| 129 | static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev, |
| 130 | unsigned group) |
| 131 | { |
| 132 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 133 | |
| 134 | return pctl->groups[group].name; |
| 135 | } |
| 136 | |
| 137 | static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev, |
| 138 | unsigned group, |
| 139 | const unsigned **pins, |
| 140 | unsigned *num_pins) |
| 141 | { |
| 142 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 143 | |
| 144 | *pins = (unsigned *)&pctl->groups[group].pin; |
| 145 | *num_pins = 1; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 150 | static bool sunxi_pctrl_has_bias_prop(struct device_node *node) |
| 151 | { |
Maxime Ripard | cefbf1a | 2016-10-20 15:49:03 +0200 | [diff] [blame] | 152 | return of_find_property(node, "bias-pull-up", NULL) || |
| 153 | of_find_property(node, "bias-pull-down", NULL) || |
| 154 | of_find_property(node, "bias-disable", NULL) || |
| 155 | of_find_property(node, "allwinner,pull", NULL); |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static bool sunxi_pctrl_has_drive_prop(struct device_node *node) |
| 159 | { |
Maxime Ripard | cefbf1a | 2016-10-20 15:49:03 +0200 | [diff] [blame] | 160 | return of_find_property(node, "drive-strength", NULL) || |
| 161 | of_find_property(node, "allwinner,drive", NULL); |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static int sunxi_pctrl_parse_bias_prop(struct device_node *node) |
| 165 | { |
| 166 | u32 val; |
| 167 | |
Maxime Ripard | cefbf1a | 2016-10-20 15:49:03 +0200 | [diff] [blame] | 168 | /* Try the new style binding */ |
| 169 | if (of_find_property(node, "bias-pull-up", NULL)) |
| 170 | return PIN_CONFIG_BIAS_PULL_UP; |
| 171 | |
| 172 | if (of_find_property(node, "bias-pull-down", NULL)) |
| 173 | return PIN_CONFIG_BIAS_PULL_DOWN; |
| 174 | |
| 175 | if (of_find_property(node, "bias-disable", NULL)) |
| 176 | return PIN_CONFIG_BIAS_DISABLE; |
| 177 | |
| 178 | /* And fall back to the old binding */ |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 179 | if (of_property_read_u32(node, "allwinner,pull", &val)) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | switch (val) { |
Maxime Ripard | 07fe64b | 2016-10-11 17:46:01 +0200 | [diff] [blame] | 183 | case SUN4I_PINCTRL_NO_PULL: |
| 184 | return PIN_CONFIG_BIAS_DISABLE; |
Maxime Ripard | 42676fa | 2016-10-11 17:46:00 +0200 | [diff] [blame] | 185 | case SUN4I_PINCTRL_PULL_UP: |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 186 | return PIN_CONFIG_BIAS_PULL_UP; |
Maxime Ripard | 42676fa | 2016-10-11 17:46:00 +0200 | [diff] [blame] | 187 | case SUN4I_PINCTRL_PULL_DOWN: |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 188 | return PIN_CONFIG_BIAS_PULL_DOWN; |
| 189 | } |
| 190 | |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | static int sunxi_pctrl_parse_drive_prop(struct device_node *node) |
| 195 | { |
| 196 | u32 val; |
| 197 | |
Maxime Ripard | cefbf1a | 2016-10-20 15:49:03 +0200 | [diff] [blame] | 198 | /* Try the new style binding */ |
| 199 | if (!of_property_read_u32(node, "drive-strength", &val)) { |
| 200 | /* We can't go below 10mA ... */ |
| 201 | if (val < 10) |
| 202 | return -EINVAL; |
| 203 | |
| 204 | /* ... and only up to 40 mA ... */ |
| 205 | if (val > 40) |
| 206 | val = 40; |
| 207 | |
| 208 | /* by steps of 10 mA */ |
| 209 | return rounddown(val, 10); |
| 210 | } |
| 211 | |
| 212 | /* And then fall back to the old binding */ |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 213 | if (of_property_read_u32(node, "allwinner,drive", &val)) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | return (val + 1) * 10; |
| 217 | } |
| 218 | |
| 219 | static const char *sunxi_pctrl_parse_function_prop(struct device_node *node) |
| 220 | { |
| 221 | const char *function; |
| 222 | int ret; |
| 223 | |
Maxime Ripard | cefbf1a | 2016-10-20 15:49:03 +0200 | [diff] [blame] | 224 | /* Try the generic binding */ |
| 225 | ret = of_property_read_string(node, "function", &function); |
| 226 | if (!ret) |
| 227 | return function; |
| 228 | |
| 229 | /* And fall back to our legacy one */ |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 230 | ret = of_property_read_string(node, "allwinner,function", &function); |
| 231 | if (!ret) |
| 232 | return function; |
| 233 | |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | static const char *sunxi_pctrl_find_pins_prop(struct device_node *node, |
| 238 | int *npins) |
| 239 | { |
| 240 | int count; |
| 241 | |
Maxime Ripard | cefbf1a | 2016-10-20 15:49:03 +0200 | [diff] [blame] | 242 | /* Try the generic binding */ |
| 243 | count = of_property_count_strings(node, "pins"); |
| 244 | if (count > 0) { |
| 245 | *npins = count; |
| 246 | return "pins"; |
| 247 | } |
| 248 | |
| 249 | /* And fall back to our legacy one */ |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 250 | count = of_property_count_strings(node, "allwinner,pins"); |
| 251 | if (count > 0) { |
| 252 | *npins = count; |
| 253 | return "allwinner,pins"; |
| 254 | } |
| 255 | |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | static unsigned long *sunxi_pctrl_build_pin_config(struct device_node *node, |
| 260 | unsigned int *len) |
| 261 | { |
| 262 | unsigned long *pinconfig; |
| 263 | unsigned int configlen = 0, idx = 0; |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 264 | int ret; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 265 | |
| 266 | if (sunxi_pctrl_has_drive_prop(node)) |
| 267 | configlen++; |
| 268 | if (sunxi_pctrl_has_bias_prop(node)) |
| 269 | configlen++; |
| 270 | |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 271 | /* |
| 272 | * If we don't have any configuration, bail out |
| 273 | */ |
| 274 | if (!configlen) |
| 275 | return NULL; |
| 276 | |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 277 | pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL); |
| 278 | if (!pinconfig) |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 279 | return ERR_PTR(-ENOMEM); |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 280 | |
| 281 | if (sunxi_pctrl_has_drive_prop(node)) { |
| 282 | int drive = sunxi_pctrl_parse_drive_prop(node); |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 283 | if (drive < 0) { |
| 284 | ret = drive; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 285 | goto err_free; |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 286 | } |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 287 | |
| 288 | pinconfig[idx++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH, |
| 289 | drive); |
| 290 | } |
| 291 | |
| 292 | if (sunxi_pctrl_has_bias_prop(node)) { |
| 293 | int pull = sunxi_pctrl_parse_bias_prop(node); |
Chen-Yu Tsai | 223dba0 | 2016-11-11 17:50:34 +0800 | [diff] [blame] | 294 | int arg = 0; |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 295 | if (pull < 0) { |
| 296 | ret = pull; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 297 | goto err_free; |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 298 | } |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 299 | |
Chen-Yu Tsai | 223dba0 | 2016-11-11 17:50:34 +0800 | [diff] [blame] | 300 | if (pull != PIN_CONFIG_BIAS_DISABLE) |
| 301 | arg = 1; /* hardware uses weak pull resistors */ |
| 302 | |
| 303 | pinconfig[idx++] = pinconf_to_config_packed(pull, arg); |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | *len = configlen; |
| 308 | return pinconfig; |
| 309 | |
| 310 | err_free: |
| 311 | kfree(pinconfig); |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 312 | return ERR_PTR(ret); |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 315 | static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 316 | struct device_node *node, |
| 317 | struct pinctrl_map **map, |
| 318 | unsigned *num_maps) |
| 319 | { |
| 320 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 321 | unsigned long *pinconfig; |
| 322 | struct property *prop; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 323 | const char *function, *pin_prop; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 324 | const char *group; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 325 | int ret, npins, nmaps, configlen = 0, i = 0; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 326 | |
| 327 | *map = NULL; |
| 328 | *num_maps = 0; |
| 329 | |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 330 | function = sunxi_pctrl_parse_function_prop(node); |
| 331 | if (!function) { |
| 332 | dev_err(pctl->dev, "missing function property in node %s\n", |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 333 | node->name); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 337 | pin_prop = sunxi_pctrl_find_pins_prop(node, &npins); |
| 338 | if (!pin_prop) { |
| 339 | dev_err(pctl->dev, "missing pins property in node %s\n", |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 340 | node->name); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 344 | /* |
| 345 | * We have two maps for each pin: one for the function, one |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 346 | * for the configuration (bias, strength, etc). |
| 347 | * |
| 348 | * We might be slightly overshooting, since we might not have |
| 349 | * any configuration. |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 350 | */ |
| 351 | nmaps = npins * 2; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 352 | *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL); |
Sachin Kamat | 3efa921 | 2013-07-29 13:49:32 +0530 | [diff] [blame] | 353 | if (!*map) |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 354 | return -ENOMEM; |
| 355 | |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 356 | pinconfig = sunxi_pctrl_build_pin_config(node, &configlen); |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 357 | if (IS_ERR(pinconfig)) { |
| 358 | ret = PTR_ERR(pinconfig); |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 359 | goto err_free_map; |
| 360 | } |
| 361 | |
| 362 | of_property_for_each_string(node, pin_prop, prop, group) { |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 363 | struct sunxi_pinctrl_group *grp = |
| 364 | sunxi_pinctrl_find_group_by_name(pctl, group); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 365 | |
| 366 | if (!grp) { |
| 367 | dev_err(pctl->dev, "unknown pin %s", group); |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if (!sunxi_pinctrl_desc_find_function_by_name(pctl, |
| 372 | grp->name, |
| 373 | function)) { |
| 374 | dev_err(pctl->dev, "unsupported function %s on pin %s", |
| 375 | function, group); |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP; |
| 380 | (*map)[i].data.mux.group = group; |
| 381 | (*map)[i].data.mux.function = function; |
| 382 | |
| 383 | i++; |
| 384 | |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 385 | if (pinconfig) { |
| 386 | (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP; |
| 387 | (*map)[i].data.configs.group_or_pin = group; |
| 388 | (*map)[i].data.configs.configs = pinconfig; |
| 389 | (*map)[i].data.configs.num_configs = configlen; |
| 390 | i++; |
| 391 | } |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 394 | *num_maps = i; |
| 395 | |
| 396 | /* |
| 397 | * We know have the number of maps we need, we can resize our |
| 398 | * map array |
| 399 | */ |
| 400 | *map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL); |
Dan Carpenter | b3cde19 | 2016-11-18 14:35:57 +0300 | [diff] [blame] | 401 | if (!*map) |
Maxime Ripard | e11dee2 | 2016-10-20 15:49:02 +0200 | [diff] [blame] | 402 | return -ENOMEM; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 403 | |
| 404 | return 0; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 405 | |
| 406 | err_free_map: |
Dan Carpenter | b3cde19 | 2016-11-18 14:35:57 +0300 | [diff] [blame] | 407 | kfree(*map); |
| 408 | *map = NULL; |
Maxime Ripard | f233dbc | 2016-10-11 17:45:59 +0200 | [diff] [blame] | 409 | return ret; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev, |
| 413 | struct pinctrl_map *map, |
| 414 | unsigned num_maps) |
| 415 | { |
Chen-Yu Tsai | 88f01a1 | 2016-11-11 10:35:10 +0800 | [diff] [blame] | 416 | int i; |
| 417 | |
| 418 | /* pin config is never in the first map */ |
| 419 | for (i = 1; i < num_maps; i++) { |
| 420 | if (map[i].type != PIN_MAP_TYPE_CONFIGS_GROUP) |
| 421 | continue; |
| 422 | |
| 423 | /* |
| 424 | * All the maps share the same pin config, |
| 425 | * free only the first one we find. |
| 426 | */ |
| 427 | kfree(map[i].data.configs.configs); |
| 428 | break; |
| 429 | } |
| 430 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 431 | kfree(map); |
| 432 | } |
| 433 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 434 | static const struct pinctrl_ops sunxi_pctrl_ops = { |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 435 | .dt_node_to_map = sunxi_pctrl_dt_node_to_map, |
| 436 | .dt_free_map = sunxi_pctrl_dt_free_map, |
| 437 | .get_groups_count = sunxi_pctrl_get_groups_count, |
| 438 | .get_group_name = sunxi_pctrl_get_group_name, |
| 439 | .get_group_pins = sunxi_pctrl_get_group_pins, |
| 440 | }; |
| 441 | |
Chen-Yu Tsai | c5fda17 | 2016-11-11 17:50:35 +0800 | [diff] [blame] | 442 | static int sunxi_pconf_reg(unsigned pin, enum pin_config_param param, |
| 443 | u32 *offset, u32 *shift, u32 *mask) |
| 444 | { |
| 445 | switch (param) { |
| 446 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 447 | *offset = sunxi_dlevel_reg(pin); |
| 448 | *shift = sunxi_dlevel_offset(pin); |
| 449 | *mask = DLEVEL_PINS_MASK; |
| 450 | break; |
| 451 | |
| 452 | case PIN_CONFIG_BIAS_PULL_UP: |
| 453 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 454 | case PIN_CONFIG_BIAS_DISABLE: |
| 455 | *offset = sunxi_pull_reg(pin); |
| 456 | *shift = sunxi_pull_offset(pin); |
| 457 | *mask = PULL_PINS_MASK; |
| 458 | break; |
| 459 | |
| 460 | default: |
| 461 | return -ENOTSUPP; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int sunxi_pconf_get(struct pinctrl_dev *pctldev, unsigned pin, |
| 468 | unsigned long *config) |
| 469 | { |
| 470 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 471 | enum pin_config_param param = pinconf_to_config_param(*config); |
| 472 | u32 offset, shift, mask, val; |
| 473 | u16 arg; |
| 474 | int ret; |
| 475 | |
| 476 | pin -= pctl->desc->pin_base; |
| 477 | |
| 478 | ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask); |
| 479 | if (ret < 0) |
| 480 | return ret; |
| 481 | |
| 482 | val = (readl(pctl->membase + offset) >> shift) & mask; |
| 483 | |
| 484 | switch (pinconf_to_config_param(*config)) { |
| 485 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 486 | arg = (val + 1) * 10; |
| 487 | break; |
| 488 | |
| 489 | case PIN_CONFIG_BIAS_PULL_UP: |
| 490 | if (val != SUN4I_PINCTRL_PULL_UP) |
| 491 | return -EINVAL; |
| 492 | arg = 1; /* hardware is weak pull-up */ |
| 493 | break; |
| 494 | |
| 495 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 496 | if (val != SUN4I_PINCTRL_PULL_DOWN) |
| 497 | return -EINVAL; |
| 498 | arg = 1; /* hardware is weak pull-down */ |
| 499 | break; |
| 500 | |
| 501 | case PIN_CONFIG_BIAS_DISABLE: |
| 502 | if (val != SUN4I_PINCTRL_NO_PULL) |
| 503 | return -EINVAL; |
| 504 | arg = 0; |
| 505 | break; |
| 506 | |
| 507 | default: |
| 508 | /* sunxi_pconf_reg should catch anything unsupported */ |
| 509 | WARN_ON(1); |
| 510 | return -ENOTSUPP; |
| 511 | } |
| 512 | |
| 513 | *config = pinconf_to_config_packed(param, arg); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 518 | static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev, |
| 519 | unsigned group, |
| 520 | unsigned long *config) |
| 521 | { |
| 522 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Chen-Yu Tsai | c5fda17 | 2016-11-11 17:50:35 +0800 | [diff] [blame] | 523 | struct sunxi_pinctrl_group *g = &pctl->groups[group]; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 524 | |
Chen-Yu Tsai | c5fda17 | 2016-11-11 17:50:35 +0800 | [diff] [blame] | 525 | /* We only support 1 pin per group. Chain it to the pin callback */ |
| 526 | return sunxi_pconf_get(pctldev, g->pin, config); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev, |
| 530 | unsigned group, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 531 | unsigned long *configs, |
| 532 | unsigned num_configs) |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 533 | { |
| 534 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 535 | struct sunxi_pinctrl_group *g = &pctl->groups[group]; |
Chen-Yu Tsai | b4575c6 | 2014-05-22 23:20:55 +0800 | [diff] [blame] | 536 | unsigned pin = g->pin - pctl->desc->pin_base; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 537 | int i; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 538 | |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 539 | for (i = 0; i < num_configs; i++) { |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 540 | enum pin_config_param param; |
| 541 | unsigned long flags; |
| 542 | u32 offset, shift, mask, reg; |
Mika Westerberg | 58957d2 | 2017-01-23 15:34:32 +0300 | [diff] [blame] | 543 | u32 arg, val; |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 544 | int ret; |
| 545 | |
| 546 | param = pinconf_to_config_param(configs[i]); |
| 547 | arg = pinconf_to_config_argument(configs[i]); |
| 548 | |
| 549 | ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask); |
| 550 | if (ret < 0) |
| 551 | return ret; |
| 552 | |
| 553 | switch (param) { |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 554 | case PIN_CONFIG_DRIVE_STRENGTH: |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 555 | if (arg < 10 || arg > 40) |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 556 | return -EINVAL; |
| 557 | /* |
| 558 | * We convert from mA to what the register expects: |
| 559 | * 0: 10mA |
| 560 | * 1: 20mA |
| 561 | * 2: 30mA |
| 562 | * 3: 40mA |
| 563 | */ |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 564 | val = arg / 10 - 1; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 565 | break; |
Maxime Ripard | 07fe64b | 2016-10-11 17:46:01 +0200 | [diff] [blame] | 566 | case PIN_CONFIG_BIAS_DISABLE: |
Priit Laes | ac059e2 | 2017-08-27 15:55:23 +0300 | [diff] [blame] | 567 | val = 0; |
| 568 | break; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 569 | case PIN_CONFIG_BIAS_PULL_UP: |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 570 | if (arg == 0) |
| 571 | return -EINVAL; |
| 572 | val = 1; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 573 | break; |
| 574 | case PIN_CONFIG_BIAS_PULL_DOWN: |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 575 | if (arg == 0) |
| 576 | return -EINVAL; |
| 577 | val = 2; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 578 | break; |
| 579 | default: |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 580 | /* sunxi_pconf_reg should catch anything unsupported */ |
| 581 | WARN_ON(1); |
| 582 | return -ENOTSUPP; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 583 | } |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 584 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 585 | raw_spin_lock_irqsave(&pctl->lock, flags); |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 586 | reg = readl(pctl->membase + offset); |
| 587 | reg &= ~(mask << shift); |
| 588 | writel(reg | val << shift, pctl->membase + offset); |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 589 | raw_spin_unlock_irqrestore(&pctl->lock, flags); |
Chen-Yu Tsai | 5181482 | 2016-11-11 17:50:36 +0800 | [diff] [blame] | 590 | } /* for each config */ |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 595 | static const struct pinconf_ops sunxi_pconf_ops = { |
Chen-Yu Tsai | c5fda17 | 2016-11-11 17:50:35 +0800 | [diff] [blame] | 596 | .is_generic = true, |
| 597 | .pin_config_get = sunxi_pconf_get, |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 598 | .pin_config_group_get = sunxi_pconf_group_get, |
| 599 | .pin_config_group_set = sunxi_pconf_group_set, |
| 600 | }; |
| 601 | |
| 602 | static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev) |
| 603 | { |
| 604 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 605 | |
| 606 | return pctl->nfunctions; |
| 607 | } |
| 608 | |
| 609 | static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 610 | unsigned function) |
| 611 | { |
| 612 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 613 | |
| 614 | return pctl->functions[function].name; |
| 615 | } |
| 616 | |
| 617 | static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev, |
| 618 | unsigned function, |
| 619 | const char * const **groups, |
| 620 | unsigned * const num_groups) |
| 621 | { |
| 622 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 623 | |
| 624 | *groups = pctl->functions[function].groups; |
| 625 | *num_groups = pctl->functions[function].ngroups; |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static void sunxi_pmx_set(struct pinctrl_dev *pctldev, |
| 631 | unsigned pin, |
| 632 | u8 config) |
| 633 | { |
| 634 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 635 | unsigned long flags; |
| 636 | u32 val, mask; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 637 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 638 | raw_spin_lock_irqsave(&pctl->lock, flags); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 639 | |
Chen-Yu Tsai | b4575c6 | 2014-05-22 23:20:55 +0800 | [diff] [blame] | 640 | pin -= pctl->desc->pin_base; |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 641 | val = readl(pctl->membase + sunxi_mux_reg(pin)); |
| 642 | mask = MUX_PINS_MASK << sunxi_mux_offset(pin); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 643 | writel((val & ~mask) | config << sunxi_mux_offset(pin), |
| 644 | pctl->membase + sunxi_mux_reg(pin)); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 645 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 646 | raw_spin_unlock_irqrestore(&pctl->lock, flags); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 649 | static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev, |
| 650 | unsigned function, |
| 651 | unsigned group) |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 652 | { |
| 653 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 654 | struct sunxi_pinctrl_group *g = pctl->groups + group; |
| 655 | struct sunxi_pinctrl_function *func = pctl->functions + function; |
| 656 | struct sunxi_desc_function *desc = |
| 657 | sunxi_pinctrl_desc_find_function_by_name(pctl, |
| 658 | g->name, |
| 659 | func->name); |
| 660 | |
| 661 | if (!desc) |
| 662 | return -EINVAL; |
| 663 | |
| 664 | sunxi_pmx_set(pctldev, g->pin, desc->muxval); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 669 | static int |
| 670 | sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev, |
| 671 | struct pinctrl_gpio_range *range, |
| 672 | unsigned offset, |
| 673 | bool input) |
| 674 | { |
| 675 | struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev); |
| 676 | struct sunxi_desc_function *desc; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 677 | const char *func; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 678 | |
| 679 | if (input) |
| 680 | func = "gpio_in"; |
| 681 | else |
| 682 | func = "gpio_out"; |
| 683 | |
Maxime Ripard | 814d4f2 | 2013-06-08 12:05:43 +0200 | [diff] [blame] | 684 | desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func); |
| 685 | if (!desc) |
| 686 | return -EINVAL; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 687 | |
| 688 | sunxi_pmx_set(pctldev, offset, desc->muxval); |
| 689 | |
Maxime Ripard | 814d4f2 | 2013-06-08 12:05:43 +0200 | [diff] [blame] | 690 | return 0; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 693 | static const struct pinmux_ops sunxi_pmx_ops = { |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 694 | .get_functions_count = sunxi_pmx_get_funcs_cnt, |
| 695 | .get_function_name = sunxi_pmx_get_func_name, |
| 696 | .get_function_groups = sunxi_pmx_get_func_groups, |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 697 | .set_mux = sunxi_pmx_set_mux, |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 698 | .gpio_set_direction = sunxi_pmx_gpio_set_direction, |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 699 | }; |
| 700 | |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 701 | static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip, |
| 702 | unsigned offset) |
| 703 | { |
| 704 | return pinctrl_gpio_direction_input(chip->base + offset); |
| 705 | } |
| 706 | |
| 707 | static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 708 | { |
Linus Walleij | 88057d6 | 2015-12-08 22:40:43 +0100 | [diff] [blame] | 709 | struct sunxi_pinctrl *pctl = gpiochip_get_data(chip); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 710 | u32 reg = sunxi_data_reg(offset); |
| 711 | u8 index = sunxi_data_offset(offset); |
Linus Walleij | 6cee382 | 2016-02-11 20:16:45 +0100 | [diff] [blame] | 712 | bool set_mux = pctl->desc->irq_read_needs_mux && |
| 713 | gpiochip_line_is_irq(chip, offset); |
Krzysztof Adamski | be2d107 | 2016-02-09 15:58:49 +0100 | [diff] [blame] | 714 | u32 pin = offset + chip->base; |
Hans de Goede | ef6d24c | 2015-03-08 22:13:57 +0100 | [diff] [blame] | 715 | u32 val; |
| 716 | |
| 717 | if (set_mux) |
Krzysztof Adamski | be2d107 | 2016-02-09 15:58:49 +0100 | [diff] [blame] | 718 | sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT); |
Hans de Goede | ef6d24c | 2015-03-08 22:13:57 +0100 | [diff] [blame] | 719 | |
| 720 | val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK; |
| 721 | |
| 722 | if (set_mux) |
Krzysztof Adamski | be2d107 | 2016-02-09 15:58:49 +0100 | [diff] [blame] | 723 | sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 724 | |
Linus Walleij | 39e24ac | 2015-12-21 16:40:27 +0100 | [diff] [blame] | 725 | return !!val; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 728 | static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip, |
| 729 | unsigned offset, int value) |
| 730 | { |
Linus Walleij | 88057d6 | 2015-12-08 22:40:43 +0100 | [diff] [blame] | 731 | struct sunxi_pinctrl *pctl = gpiochip_get_data(chip); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 732 | u32 reg = sunxi_data_reg(offset); |
| 733 | u8 index = sunxi_data_offset(offset); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 734 | unsigned long flags; |
| 735 | u32 regval; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 736 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 737 | raw_spin_lock_irqsave(&pctl->lock, flags); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 738 | |
| 739 | regval = readl(pctl->membase + reg); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 740 | |
Maxime Ripard | df7b34f | 2013-07-25 12:41:16 +0200 | [diff] [blame] | 741 | if (value) |
| 742 | regval |= BIT(index); |
| 743 | else |
| 744 | regval &= ~(BIT(index)); |
| 745 | |
| 746 | writel(regval, pctl->membase + reg); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 747 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 748 | raw_spin_unlock_irqrestore(&pctl->lock, flags); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Chen-Yu Tsai | fa8cf57 | 2014-01-16 14:34:23 +0800 | [diff] [blame] | 751 | static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip, |
| 752 | unsigned offset, int value) |
| 753 | { |
| 754 | sunxi_pinctrl_gpio_set(chip, offset, value); |
| 755 | return pinctrl_gpio_direction_output(chip->base + offset); |
| 756 | } |
| 757 | |
Maxime Ripard | a0d7209 | 2013-02-03 12:10:11 +0100 | [diff] [blame] | 758 | static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc, |
| 759 | const struct of_phandle_args *gpiospec, |
| 760 | u32 *flags) |
| 761 | { |
| 762 | int pin, base; |
| 763 | |
| 764 | base = PINS_PER_BANK * gpiospec->args[0]; |
| 765 | pin = base + gpiospec->args[1]; |
| 766 | |
Chen-Yu Tsai | 343f132 | 2014-07-15 01:24:37 +0800 | [diff] [blame] | 767 | if (pin > gc->ngpio) |
Maxime Ripard | a0d7209 | 2013-02-03 12:10:11 +0100 | [diff] [blame] | 768 | return -EINVAL; |
| 769 | |
| 770 | if (flags) |
| 771 | *flags = gpiospec->args[2]; |
| 772 | |
| 773 | return pin; |
| 774 | } |
| 775 | |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 776 | static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 777 | { |
Linus Walleij | 88057d6 | 2015-12-08 22:40:43 +0100 | [diff] [blame] | 778 | struct sunxi_pinctrl *pctl = gpiochip_get_data(chip); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 779 | struct sunxi_desc_function *desc; |
Chen-Yu Tsai | 343f132 | 2014-07-15 01:24:37 +0800 | [diff] [blame] | 780 | unsigned pinnum = pctl->desc->pin_base + offset; |
Chen-Yu Tsai | 0d3bafa | 2014-07-01 00:04:59 +0800 | [diff] [blame] | 781 | unsigned irqnum; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 782 | |
Axel Lin | c9e3b2d | 2013-08-30 16:31:25 +0800 | [diff] [blame] | 783 | if (offset >= chip->ngpio) |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 784 | return -ENXIO; |
| 785 | |
Chen-Yu Tsai | 343f132 | 2014-07-15 01:24:37 +0800 | [diff] [blame] | 786 | desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq"); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 787 | if (!desc) |
| 788 | return -EINVAL; |
| 789 | |
Chen-Yu Tsai | 0d3bafa | 2014-07-01 00:04:59 +0800 | [diff] [blame] | 790 | irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 791 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 792 | dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n", |
Chen-Yu Tsai | 0d3bafa | 2014-07-01 00:04:59 +0800 | [diff] [blame] | 793 | chip->label, offset + chip->base, irqnum); |
| 794 | |
| 795 | return irq_find_mapping(pctl->domain, irqnum); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Hans de Goede | fea6d8e | 2014-06-29 16:11:00 +0200 | [diff] [blame] | 798 | static int sunxi_pinctrl_irq_request_resources(struct irq_data *d) |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 799 | { |
| 800 | struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d); |
Hans de Goede | fea6d8e | 2014-06-29 16:11:00 +0200 | [diff] [blame] | 801 | struct sunxi_desc_function *func; |
Chen-Yu Tsai | f83549d | 2014-07-15 01:24:36 +0800 | [diff] [blame] | 802 | int ret; |
Hans de Goede | fea6d8e | 2014-06-29 16:11:00 +0200 | [diff] [blame] | 803 | |
| 804 | func = sunxi_pinctrl_desc_find_function_by_pin(pctl, |
| 805 | pctl->irq_array[d->hwirq], "irq"); |
| 806 | if (!func) |
| 807 | return -EINVAL; |
| 808 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 809 | ret = gpiochip_lock_as_irq(pctl->chip, |
Chen-Yu Tsai | 343f132 | 2014-07-15 01:24:37 +0800 | [diff] [blame] | 810 | pctl->irq_array[d->hwirq] - pctl->desc->pin_base); |
Chen-Yu Tsai | f83549d | 2014-07-15 01:24:36 +0800 | [diff] [blame] | 811 | if (ret) { |
| 812 | dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n", |
| 813 | irqd_to_hwirq(d)); |
| 814 | return ret; |
| 815 | } |
| 816 | |
Hans de Goede | fea6d8e | 2014-06-29 16:11:00 +0200 | [diff] [blame] | 817 | /* Change muxing to INT mode */ |
| 818 | sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval); |
| 819 | |
| 820 | return 0; |
| 821 | } |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 822 | |
Chen-Yu Tsai | f83549d | 2014-07-15 01:24:36 +0800 | [diff] [blame] | 823 | static void sunxi_pinctrl_irq_release_resources(struct irq_data *d) |
| 824 | { |
| 825 | struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d); |
| 826 | |
Alexandre Courbot | e3a2e87 | 2014-10-23 17:27:07 +0900 | [diff] [blame] | 827 | gpiochip_unlock_as_irq(pctl->chip, |
| 828 | pctl->irq_array[d->hwirq] - pctl->desc->pin_base); |
Chen-Yu Tsai | f83549d | 2014-07-15 01:24:36 +0800 | [diff] [blame] | 829 | } |
| 830 | |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 831 | static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type) |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 832 | { |
| 833 | struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d); |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 834 | u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 835 | u8 index = sunxi_irq_cfg_offset(d->hwirq); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 836 | unsigned long flags; |
Maxime Ripard | 2aaaddf | 2013-08-04 12:38:47 +0200 | [diff] [blame] | 837 | u32 regval; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 838 | u8 mode; |
| 839 | |
| 840 | switch (type) { |
| 841 | case IRQ_TYPE_EDGE_RISING: |
| 842 | mode = IRQ_EDGE_RISING; |
| 843 | break; |
| 844 | case IRQ_TYPE_EDGE_FALLING: |
| 845 | mode = IRQ_EDGE_FALLING; |
| 846 | break; |
| 847 | case IRQ_TYPE_EDGE_BOTH: |
| 848 | mode = IRQ_EDGE_BOTH; |
| 849 | break; |
| 850 | case IRQ_TYPE_LEVEL_HIGH: |
| 851 | mode = IRQ_LEVEL_HIGH; |
| 852 | break; |
| 853 | case IRQ_TYPE_LEVEL_LOW: |
| 854 | mode = IRQ_LEVEL_LOW; |
| 855 | break; |
| 856 | default: |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 860 | raw_spin_lock_irqsave(&pctl->lock, flags); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 861 | |
Maxime Ripard | a0d6de9 | 2015-07-20 14:41:11 +0200 | [diff] [blame] | 862 | if (type & IRQ_TYPE_LEVEL_MASK) |
Thomas Gleixner | b9a5ec33 | 2015-09-16 12:32:40 +0200 | [diff] [blame] | 863 | irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip, |
| 864 | handle_fasteoi_irq, NULL); |
Maxime Ripard | a0d6de9 | 2015-07-20 14:41:11 +0200 | [diff] [blame] | 865 | else |
Thomas Gleixner | b9a5ec33 | 2015-09-16 12:32:40 +0200 | [diff] [blame] | 866 | irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip, |
| 867 | handle_edge_irq, NULL); |
Maxime Ripard | a0d6de9 | 2015-07-20 14:41:11 +0200 | [diff] [blame] | 868 | |
Maxime Ripard | 2aaaddf | 2013-08-04 12:38:47 +0200 | [diff] [blame] | 869 | regval = readl(pctl->membase + reg); |
Hans de Goede | d82f940 | 2014-02-17 22:19:43 +0100 | [diff] [blame] | 870 | regval &= ~(IRQ_CFG_IRQ_MASK << index); |
Maxime Ripard | 2aaaddf | 2013-08-04 12:38:47 +0200 | [diff] [blame] | 871 | writel(regval | (mode << index), pctl->membase + reg); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 872 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 873 | raw_spin_unlock_irqrestore(&pctl->lock, flags); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Maxime Ripard | 645ec71 | 2014-06-05 15:26:00 +0200 | [diff] [blame] | 878 | static void sunxi_pinctrl_irq_ack(struct irq_data *d) |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 879 | { |
| 880 | struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d); |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 881 | u32 status_reg = sunxi_irq_status_reg(d->hwirq, |
| 882 | pctl->desc->irq_bank_base); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 883 | u8 status_idx = sunxi_irq_status_offset(d->hwirq); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 884 | |
| 885 | /* Clear the IRQ */ |
| 886 | writel(1 << status_idx, pctl->membase + status_reg); |
| 887 | } |
| 888 | |
| 889 | static void sunxi_pinctrl_irq_mask(struct irq_data *d) |
| 890 | { |
| 891 | struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d); |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 892 | u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 893 | u8 idx = sunxi_irq_ctrl_offset(d->hwirq); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 894 | unsigned long flags; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 895 | u32 val; |
| 896 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 897 | raw_spin_lock_irqsave(&pctl->lock, flags); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 898 | |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 899 | /* Mask the IRQ */ |
| 900 | val = readl(pctl->membase + reg); |
| 901 | writel(val & ~(1 << idx), pctl->membase + reg); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 902 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 903 | raw_spin_unlock_irqrestore(&pctl->lock, flags); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void sunxi_pinctrl_irq_unmask(struct irq_data *d) |
| 907 | { |
| 908 | struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d); |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 909 | u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 910 | u8 idx = sunxi_irq_ctrl_offset(d->hwirq); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 911 | unsigned long flags; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 912 | u32 val; |
| 913 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 914 | raw_spin_lock_irqsave(&pctl->lock, flags); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 915 | |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 916 | /* Unmask the IRQ */ |
| 917 | val = readl(pctl->membase + reg); |
| 918 | writel(val | (1 << idx), pctl->membase + reg); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 919 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 920 | raw_spin_unlock_irqrestore(&pctl->lock, flags); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Hans de Goede | d61e23e | 2014-06-29 16:11:02 +0200 | [diff] [blame] | 923 | static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d) |
| 924 | { |
| 925 | sunxi_pinctrl_irq_ack(d); |
| 926 | sunxi_pinctrl_irq_unmask(d); |
| 927 | } |
| 928 | |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 929 | static struct irq_chip sunxi_pinctrl_edge_irq_chip = { |
Maxime Ripard | fb5b778 | 2015-07-20 14:41:12 +0200 | [diff] [blame] | 930 | .name = "sunxi_pio_edge", |
Maxime Ripard | 645ec71 | 2014-06-05 15:26:00 +0200 | [diff] [blame] | 931 | .irq_ack = sunxi_pinctrl_irq_ack, |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 932 | .irq_mask = sunxi_pinctrl_irq_mask, |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 933 | .irq_unmask = sunxi_pinctrl_irq_unmask, |
Hans de Goede | fea6d8e | 2014-06-29 16:11:00 +0200 | [diff] [blame] | 934 | .irq_request_resources = sunxi_pinctrl_irq_request_resources, |
Chen-Yu Tsai | f83549d | 2014-07-15 01:24:36 +0800 | [diff] [blame] | 935 | .irq_release_resources = sunxi_pinctrl_irq_release_resources, |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 936 | .irq_set_type = sunxi_pinctrl_irq_set_type, |
Chen-Yu Tsai | 578c0a8 | 2014-06-29 16:10:59 +0200 | [diff] [blame] | 937 | .flags = IRQCHIP_SKIP_SET_WAKE, |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 938 | }; |
| 939 | |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 940 | static struct irq_chip sunxi_pinctrl_level_irq_chip = { |
Maxime Ripard | fb5b778 | 2015-07-20 14:41:12 +0200 | [diff] [blame] | 941 | .name = "sunxi_pio_level", |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 942 | .irq_eoi = sunxi_pinctrl_irq_ack, |
| 943 | .irq_mask = sunxi_pinctrl_irq_mask, |
| 944 | .irq_unmask = sunxi_pinctrl_irq_unmask, |
Hans de Goede | d61e23e | 2014-06-29 16:11:02 +0200 | [diff] [blame] | 945 | /* Define irq_enable / disable to avoid spurious irqs for drivers |
| 946 | * using these to suppress irqs while they clear the irq source */ |
| 947 | .irq_enable = sunxi_pinctrl_irq_ack_unmask, |
| 948 | .irq_disable = sunxi_pinctrl_irq_mask, |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 949 | .irq_request_resources = sunxi_pinctrl_irq_request_resources, |
Chen-Yu Tsai | f83549d | 2014-07-15 01:24:36 +0800 | [diff] [blame] | 950 | .irq_release_resources = sunxi_pinctrl_irq_release_resources, |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 951 | .irq_set_type = sunxi_pinctrl_irq_set_type, |
| 952 | .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED | |
| 953 | IRQCHIP_EOI_IF_HANDLED, |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 954 | }; |
| 955 | |
Maxime Ripard | d8323c6 | 2015-07-27 14:41:57 +0200 | [diff] [blame] | 956 | static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d, |
| 957 | struct device_node *node, |
| 958 | const u32 *intspec, |
| 959 | unsigned int intsize, |
| 960 | unsigned long *out_hwirq, |
| 961 | unsigned int *out_type) |
| 962 | { |
Hans de Goede | 8297992 | 2015-10-16 09:46:11 +0200 | [diff] [blame] | 963 | struct sunxi_pinctrl *pctl = d->host_data; |
Maxime Ripard | d8323c6 | 2015-07-27 14:41:57 +0200 | [diff] [blame] | 964 | struct sunxi_desc_function *desc; |
| 965 | int pin, base; |
| 966 | |
| 967 | if (intsize < 3) |
| 968 | return -EINVAL; |
| 969 | |
| 970 | base = PINS_PER_BANK * intspec[0]; |
Hans de Goede | 8297992 | 2015-10-16 09:46:11 +0200 | [diff] [blame] | 971 | pin = pctl->desc->pin_base + base + intspec[1]; |
Maxime Ripard | d8323c6 | 2015-07-27 14:41:57 +0200 | [diff] [blame] | 972 | |
Hans de Goede | 8297992 | 2015-10-16 09:46:11 +0200 | [diff] [blame] | 973 | desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq"); |
Maxime Ripard | d8323c6 | 2015-07-27 14:41:57 +0200 | [diff] [blame] | 974 | if (!desc) |
| 975 | return -EINVAL; |
| 976 | |
| 977 | *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum; |
| 978 | *out_type = intspec[2]; |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
Tobias Klauser | 2421dfd | 2017-06-02 13:29:58 +0200 | [diff] [blame] | 983 | static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = { |
Maxime Ripard | d8323c6 | 2015-07-27 14:41:57 +0200 | [diff] [blame] | 984 | .xlate = sunxi_pinctrl_irq_of_xlate, |
| 985 | }; |
| 986 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 987 | static void sunxi_pinctrl_irq_handler(struct irq_desc *desc) |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 988 | { |
Thomas Gleixner | eeef97b | 2015-07-13 01:55:27 +0200 | [diff] [blame] | 989 | unsigned int irq = irq_desc_get_irq(desc); |
Jiang Liu | 5663bb2 | 2015-06-04 12:13:16 +0800 | [diff] [blame] | 990 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 991 | struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc); |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 992 | unsigned long bank, reg, val; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 993 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 994 | for (bank = 0; bank < pctl->desc->irq_banks; bank++) |
| 995 | if (irq == pctl->irq[bank]) |
| 996 | break; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 997 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 998 | if (bank == pctl->desc->irq_banks) |
| 999 | return; |
| 1000 | |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 1001 | reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base); |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1002 | val = readl(pctl->membase + reg); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1003 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1004 | if (val) { |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1005 | int irqoffset; |
| 1006 | |
Chen-Yu Tsai | 905a511 | 2014-02-11 00:22:37 +0800 | [diff] [blame] | 1007 | chained_irq_enter(chip, desc); |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1008 | for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) { |
| 1009 | int pin_irq = irq_find_mapping(pctl->domain, |
| 1010 | bank * IRQ_PER_BANK + irqoffset); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1011 | generic_handle_irq(pin_irq); |
| 1012 | } |
Chen-Yu Tsai | 905a511 | 2014-02-11 00:22:37 +0800 | [diff] [blame] | 1013 | chained_irq_exit(chip, desc); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1017 | static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl, |
| 1018 | const char *name) |
| 1019 | { |
| 1020 | struct sunxi_pinctrl_function *func = pctl->functions; |
| 1021 | |
| 1022 | while (func->name) { |
| 1023 | /* function already there */ |
| 1024 | if (strcmp(func->name, name) == 0) { |
| 1025 | func->ngroups++; |
| 1026 | return -EEXIST; |
| 1027 | } |
| 1028 | func++; |
| 1029 | } |
| 1030 | |
| 1031 | func->name = name; |
| 1032 | func->ngroups = 1; |
| 1033 | |
| 1034 | pctl->nfunctions++; |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | static int sunxi_pinctrl_build_state(struct platform_device *pdev) |
| 1040 | { |
| 1041 | struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev); |
| 1042 | int i; |
| 1043 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * Allocate groups |
| 1046 | * |
| 1047 | * We assume that the number of groups is the number of pins |
| 1048 | * given in the data array. |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1049 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1050 | * This will not always be true, since some pins might not be |
| 1051 | * available in the current variant, but fortunately for us, |
| 1052 | * this means that the number of pins is the maximum group |
| 1053 | * number we will ever see. |
| 1054 | */ |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1055 | pctl->groups = devm_kzalloc(&pdev->dev, |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1056 | pctl->desc->npins * sizeof(*pctl->groups), |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1057 | GFP_KERNEL); |
| 1058 | if (!pctl->groups) |
| 1059 | return -ENOMEM; |
| 1060 | |
| 1061 | for (i = 0; i < pctl->desc->npins; i++) { |
| 1062 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1063 | struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups; |
| 1064 | |
| 1065 | if (pin->variant && !(pctl->variant & pin->variant)) |
| 1066 | continue; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1067 | |
| 1068 | group->name = pin->pin.name; |
| 1069 | group->pin = pin->pin.number; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1070 | |
| 1071 | /* And now we count the actual number of pins / groups */ |
| 1072 | pctl->ngroups++; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * We suppose that we won't have any more functions than pins, |
| 1077 | * we'll reallocate that later anyway |
| 1078 | */ |
| 1079 | pctl->functions = devm_kzalloc(&pdev->dev, |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1080 | pctl->ngroups * sizeof(*pctl->functions), |
| 1081 | GFP_KERNEL); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1082 | if (!pctl->functions) |
| 1083 | return -ENOMEM; |
| 1084 | |
| 1085 | /* Count functions and their associated groups */ |
| 1086 | for (i = 0; i < pctl->desc->npins; i++) { |
| 1087 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1088 | struct sunxi_desc_function *func; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1089 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1090 | if (pin->variant && !(pctl->variant & pin->variant)) |
| 1091 | continue; |
| 1092 | |
| 1093 | for (func = pin->functions; func->name; func++) { |
| 1094 | if (func->variant && !(pctl->variant & func->variant)) |
| 1095 | continue; |
| 1096 | |
Chen-Yu Tsai | d54e9a2 | 2014-05-26 09:47:56 +0200 | [diff] [blame] | 1097 | /* Create interrupt mapping while we're at it */ |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1098 | if (!strcmp(func->name, "irq")) { |
| 1099 | int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK; |
| 1100 | pctl->irq_array[irqnum] = pin->pin.number; |
| 1101 | } |
| 1102 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1103 | sunxi_pinctrl_add_function(pctl, func->name); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1107 | /* And now allocated and fill the array for real */ |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1108 | pctl->functions = krealloc(pctl->functions, |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1109 | pctl->nfunctions * sizeof(*pctl->functions), |
| 1110 | GFP_KERNEL); |
| 1111 | if (!pctl->functions) { |
| 1112 | kfree(pctl->functions); |
| 1113 | return -ENOMEM; |
| 1114 | } |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1115 | |
| 1116 | for (i = 0; i < pctl->desc->npins; i++) { |
| 1117 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1118 | struct sunxi_desc_function *func; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1119 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1120 | if (pin->variant && !(pctl->variant & pin->variant)) |
| 1121 | continue; |
| 1122 | |
| 1123 | for (func = pin->functions; func->name; func++) { |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1124 | struct sunxi_pinctrl_function *func_item; |
| 1125 | const char **func_grp; |
| 1126 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1127 | if (func->variant && !(pctl->variant & func->variant)) |
| 1128 | continue; |
| 1129 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1130 | func_item = sunxi_pinctrl_find_function_by_name(pctl, |
| 1131 | func->name); |
| 1132 | if (!func_item) |
| 1133 | return -EINVAL; |
| 1134 | |
| 1135 | if (!func_item->groups) { |
| 1136 | func_item->groups = |
| 1137 | devm_kzalloc(&pdev->dev, |
| 1138 | func_item->ngroups * sizeof(*func_item->groups), |
| 1139 | GFP_KERNEL); |
| 1140 | if (!func_item->groups) |
| 1141 | return -ENOMEM; |
| 1142 | } |
| 1143 | |
| 1144 | func_grp = func_item->groups; |
| 1145 | while (*func_grp) |
| 1146 | func_grp++; |
| 1147 | |
| 1148 | *func_grp = pin->pin.name; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
Maxime Ripard | 7c92649 | 2016-11-14 21:53:03 +0100 | [diff] [blame] | 1155 | static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff) |
| 1156 | { |
| 1157 | unsigned long clock = clk_get_rate(clk); |
Arnd Bergmann | d8a2221 | 2016-11-16 15:18:18 +0100 | [diff] [blame] | 1158 | unsigned int best_diff, best_div; |
Maxime Ripard | 7c92649 | 2016-11-14 21:53:03 +0100 | [diff] [blame] | 1159 | int i; |
| 1160 | |
Arnd Bergmann | d8a2221 | 2016-11-16 15:18:18 +0100 | [diff] [blame] | 1161 | best_diff = abs(freq - clock); |
| 1162 | best_div = 0; |
| 1163 | |
| 1164 | for (i = 1; i < 8; i++) { |
Maxime Ripard | 7c92649 | 2016-11-14 21:53:03 +0100 | [diff] [blame] | 1165 | int cur_diff = abs(freq - (clock >> i)); |
| 1166 | |
| 1167 | if (cur_diff < best_diff) { |
| 1168 | best_diff = cur_diff; |
| 1169 | best_div = i; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | *diff = best_diff; |
| 1174 | return best_div; |
| 1175 | } |
| 1176 | |
| 1177 | static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl, |
| 1178 | struct device_node *node) |
| 1179 | { |
| 1180 | unsigned int hosc_diff, losc_diff; |
| 1181 | unsigned int hosc_div, losc_div; |
| 1182 | struct clk *hosc, *losc; |
| 1183 | u8 div, src; |
| 1184 | int i, ret; |
| 1185 | |
| 1186 | /* Deal with old DTs that didn't have the oscillators */ |
| 1187 | if (of_count_phandle_with_args(node, "clocks", "#clock-cells") != 3) |
| 1188 | return 0; |
| 1189 | |
| 1190 | /* If we don't have any setup, bail out */ |
| 1191 | if (!of_find_property(node, "input-debounce", NULL)) |
| 1192 | return 0; |
| 1193 | |
| 1194 | losc = devm_clk_get(pctl->dev, "losc"); |
| 1195 | if (IS_ERR(losc)) |
| 1196 | return PTR_ERR(losc); |
| 1197 | |
| 1198 | hosc = devm_clk_get(pctl->dev, "hosc"); |
| 1199 | if (IS_ERR(hosc)) |
| 1200 | return PTR_ERR(hosc); |
| 1201 | |
| 1202 | for (i = 0; i < pctl->desc->irq_banks; i++) { |
| 1203 | unsigned long debounce_freq; |
| 1204 | u32 debounce; |
| 1205 | |
| 1206 | ret = of_property_read_u32_index(node, "input-debounce", |
| 1207 | i, &debounce); |
| 1208 | if (ret) |
| 1209 | return ret; |
| 1210 | |
| 1211 | if (!debounce) |
| 1212 | continue; |
| 1213 | |
| 1214 | debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce); |
| 1215 | losc_div = sunxi_pinctrl_get_debounce_div(losc, |
| 1216 | debounce_freq, |
| 1217 | &losc_diff); |
| 1218 | |
| 1219 | hosc_div = sunxi_pinctrl_get_debounce_div(hosc, |
| 1220 | debounce_freq, |
| 1221 | &hosc_diff); |
| 1222 | |
| 1223 | if (hosc_diff < losc_diff) { |
| 1224 | div = hosc_div; |
| 1225 | src = 1; |
| 1226 | } else { |
| 1227 | div = losc_div; |
| 1228 | src = 0; |
| 1229 | } |
| 1230 | |
| 1231 | writel(src | div << 4, |
| 1232 | pctl->membase + |
| 1233 | sunxi_irq_debounce_reg_from_bank(i, |
| 1234 | pctl->desc->irq_bank_base)); |
| 1235 | } |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1240 | int sunxi_pinctrl_init_with_variant(struct platform_device *pdev, |
| 1241 | const struct sunxi_pinctrl_desc *desc, |
| 1242 | unsigned long variant) |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1243 | { |
| 1244 | struct device_node *node = pdev->dev.of_node; |
Maxime Ripard | ba6764d | 2014-05-22 16:25:27 +0200 | [diff] [blame] | 1245 | struct pinctrl_desc *pctrl_desc; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1246 | struct pinctrl_pin_desc *pins; |
| 1247 | struct sunxi_pinctrl *pctl; |
Maxime Ripard | 4409caf | 2014-04-26 21:59:50 +0200 | [diff] [blame] | 1248 | struct resource *res; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1249 | int i, ret, last_pin, pin_idx; |
Emilio López | 950707c | 2013-03-22 11:20:40 -0300 | [diff] [blame] | 1250 | struct clk *clk; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1251 | |
| 1252 | pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL); |
| 1253 | if (!pctl) |
| 1254 | return -ENOMEM; |
| 1255 | platform_set_drvdata(pdev, pctl); |
| 1256 | |
Julia Cartwright | f658ed3 | 2017-03-09 10:22:06 -0600 | [diff] [blame] | 1257 | raw_spin_lock_init(&pctl->lock); |
Maxime Ripard | 1bee963 | 2013-08-04 12:38:48 +0200 | [diff] [blame] | 1258 | |
Maxime Ripard | 4409caf | 2014-04-26 21:59:50 +0200 | [diff] [blame] | 1259 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1260 | pctl->membase = devm_ioremap_resource(&pdev->dev, res); |
| 1261 | if (IS_ERR(pctl->membase)) |
| 1262 | return PTR_ERR(pctl->membase); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1263 | |
Maxime Ripard | ba6764d | 2014-05-22 16:25:27 +0200 | [diff] [blame] | 1264 | pctl->dev = &pdev->dev; |
Maxime Ripard | 2284ba6 | 2014-04-18 20:10:41 +0200 | [diff] [blame] | 1265 | pctl->desc = desc; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1266 | pctl->variant = variant; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1267 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1268 | pctl->irq_array = devm_kcalloc(&pdev->dev, |
| 1269 | IRQ_PER_BANK * pctl->desc->irq_banks, |
| 1270 | sizeof(*pctl->irq_array), |
| 1271 | GFP_KERNEL); |
| 1272 | if (!pctl->irq_array) |
| 1273 | return -ENOMEM; |
| 1274 | |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1275 | ret = sunxi_pinctrl_build_state(pdev); |
| 1276 | if (ret) { |
| 1277 | dev_err(&pdev->dev, "dt probe failed: %d\n", ret); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | pins = devm_kzalloc(&pdev->dev, |
| 1282 | pctl->desc->npins * sizeof(*pins), |
| 1283 | GFP_KERNEL); |
| 1284 | if (!pins) |
| 1285 | return -ENOMEM; |
| 1286 | |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1287 | for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) { |
| 1288 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
| 1289 | |
| 1290 | if (pin->variant && !(pctl->variant & pin->variant)) |
| 1291 | continue; |
| 1292 | |
| 1293 | pins[pin_idx++] = pin->pin; |
| 1294 | } |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1295 | |
Maxime Ripard | ba6764d | 2014-05-22 16:25:27 +0200 | [diff] [blame] | 1296 | pctrl_desc = devm_kzalloc(&pdev->dev, |
| 1297 | sizeof(*pctrl_desc), |
| 1298 | GFP_KERNEL); |
| 1299 | if (!pctrl_desc) |
| 1300 | return -ENOMEM; |
| 1301 | |
| 1302 | pctrl_desc->name = dev_name(&pdev->dev); |
| 1303 | pctrl_desc->owner = THIS_MODULE; |
| 1304 | pctrl_desc->pins = pins; |
Maxime Ripard | 578db85 | 2017-01-08 22:31:15 +0100 | [diff] [blame] | 1305 | pctrl_desc->npins = pctl->ngroups; |
Maxime Ripard | ba6764d | 2014-05-22 16:25:27 +0200 | [diff] [blame] | 1306 | pctrl_desc->confops = &sunxi_pconf_ops; |
| 1307 | pctrl_desc->pctlops = &sunxi_pctrl_ops; |
| 1308 | pctrl_desc->pmxops = &sunxi_pmx_ops; |
| 1309 | |
Laxman Dewangan | 45078ea | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 1310 | pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1311 | if (IS_ERR(pctl->pctl_dev)) { |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1312 | dev_err(&pdev->dev, "couldn't register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1313 | return PTR_ERR(pctl->pctl_dev); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1316 | pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL); |
Laxman Dewangan | 45078ea | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 1317 | if (!pctl->chip) |
| 1318 | return -ENOMEM; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1319 | |
| 1320 | last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number; |
Boris BREZILLON | d83c82c | 2014-04-10 15:52:43 +0200 | [diff] [blame] | 1321 | pctl->chip->owner = THIS_MODULE; |
Jonas Gorski | 98c85d5 | 2015-10-11 17:34:19 +0200 | [diff] [blame] | 1322 | pctl->chip->request = gpiochip_generic_request, |
| 1323 | pctl->chip->free = gpiochip_generic_free, |
Boris BREZILLON | d83c82c | 2014-04-10 15:52:43 +0200 | [diff] [blame] | 1324 | pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input, |
| 1325 | pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output, |
| 1326 | pctl->chip->get = sunxi_pinctrl_gpio_get, |
| 1327 | pctl->chip->set = sunxi_pinctrl_gpio_set, |
| 1328 | pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate, |
| 1329 | pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq, |
| 1330 | pctl->chip->of_gpio_n_cells = 3, |
| 1331 | pctl->chip->can_sleep = false, |
| 1332 | pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) - |
| 1333 | pctl->desc->pin_base; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1334 | pctl->chip->label = dev_name(&pdev->dev); |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1335 | pctl->chip->parent = &pdev->dev; |
Boris BREZILLON | d83c82c | 2014-04-10 15:52:43 +0200 | [diff] [blame] | 1336 | pctl->chip->base = pctl->desc->pin_base; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1337 | |
Linus Walleij | 88057d6 | 2015-12-08 22:40:43 +0100 | [diff] [blame] | 1338 | ret = gpiochip_add_data(pctl->chip, pctl); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1339 | if (ret) |
Laxman Dewangan | 45078ea | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 1340 | return ret; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1341 | |
| 1342 | for (i = 0; i < pctl->desc->npins; i++) { |
| 1343 | const struct sunxi_desc_pin *pin = pctl->desc->pins + i; |
| 1344 | |
| 1345 | ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev), |
Chen-Yu Tsai | 343f132 | 2014-07-15 01:24:37 +0800 | [diff] [blame] | 1346 | pin->pin.number - pctl->desc->pin_base, |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1347 | pin->pin.number, 1); |
| 1348 | if (ret) |
| 1349 | goto gpiochip_error; |
| 1350 | } |
| 1351 | |
Emilio López | 950707c | 2013-03-22 11:20:40 -0300 | [diff] [blame] | 1352 | clk = devm_clk_get(&pdev->dev, NULL); |
Wei Yongjun | d72f88a | 2013-05-23 17:32:14 +0800 | [diff] [blame] | 1353 | if (IS_ERR(clk)) { |
| 1354 | ret = PTR_ERR(clk); |
Emilio López | 950707c | 2013-03-22 11:20:40 -0300 | [diff] [blame] | 1355 | goto gpiochip_error; |
Wei Yongjun | d72f88a | 2013-05-23 17:32:14 +0800 | [diff] [blame] | 1356 | } |
Emilio López | 950707c | 2013-03-22 11:20:40 -0300 | [diff] [blame] | 1357 | |
Boris BREZILLON | 6415093 | 2014-04-10 15:52:40 +0200 | [diff] [blame] | 1358 | ret = clk_prepare_enable(clk); |
| 1359 | if (ret) |
| 1360 | goto gpiochip_error; |
Emilio López | 950707c | 2013-03-22 11:20:40 -0300 | [diff] [blame] | 1361 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1362 | pctl->irq = devm_kcalloc(&pdev->dev, |
| 1363 | pctl->desc->irq_banks, |
| 1364 | sizeof(*pctl->irq), |
| 1365 | GFP_KERNEL); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1366 | if (!pctl->irq) { |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1367 | ret = -ENOMEM; |
Maxime Ripard | dc96910 | 2014-04-26 22:28:54 +0200 | [diff] [blame] | 1368 | goto clk_error; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1371 | for (i = 0; i < pctl->desc->irq_banks; i++) { |
| 1372 | pctl->irq[i] = platform_get_irq(pdev, i); |
| 1373 | if (pctl->irq[i] < 0) { |
| 1374 | ret = pctl->irq[i]; |
| 1375 | goto clk_error; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | pctl->domain = irq_domain_add_linear(node, |
| 1380 | pctl->desc->irq_banks * IRQ_PER_BANK, |
Maxime Ripard | d8323c6 | 2015-07-27 14:41:57 +0200 | [diff] [blame] | 1381 | &sunxi_pinctrl_irq_domain_ops, |
| 1382 | pctl); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1383 | if (!pctl->domain) { |
| 1384 | dev_err(&pdev->dev, "Couldn't register IRQ domain\n"); |
| 1385 | ret = -ENOMEM; |
Maxime Ripard | dc96910 | 2014-04-26 22:28:54 +0200 | [diff] [blame] | 1386 | goto clk_error; |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1389 | for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) { |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1390 | int irqno = irq_create_mapping(pctl->domain, i); |
| 1391 | |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 1392 | irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip, |
| 1393 | handle_edge_irq); |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1394 | irq_set_chip_data(irqno, pctl); |
Javier Martinez Canillas | 5c99c0f | 2015-09-16 10:28:29 +0200 | [diff] [blame] | 1395 | } |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1396 | |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1397 | for (i = 0; i < pctl->desc->irq_banks; i++) { |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 1398 | /* Mask and clear all IRQs before registering a handler */ |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 1399 | writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i, |
| 1400 | pctl->desc->irq_bank_base)); |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 1401 | writel(0xffffffff, |
Hans de Goede | 5e7515b | 2016-03-12 19:44:57 +0100 | [diff] [blame] | 1402 | pctl->membase + sunxi_irq_status_reg_from_bank(i, |
| 1403 | pctl->desc->irq_bank_base)); |
Hans de Goede | f4c51c1 | 2014-06-29 16:11:01 +0200 | [diff] [blame] | 1404 | |
Thomas Gleixner | ef80e87 | 2015-06-21 20:16:18 +0200 | [diff] [blame] | 1405 | irq_set_chained_handler_and_data(pctl->irq[i], |
| 1406 | sunxi_pinctrl_irq_handler, |
| 1407 | pctl); |
Maxime Ripard | aebdc8a | 2014-06-05 15:26:04 +0200 | [diff] [blame] | 1408 | } |
Maxime Ripard | 60242db | 2013-06-08 12:05:44 +0200 | [diff] [blame] | 1409 | |
Maxime Ripard | 7c92649 | 2016-11-14 21:53:03 +0100 | [diff] [blame] | 1410 | sunxi_pinctrl_setup_debounce(pctl, node); |
| 1411 | |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1412 | dev_info(&pdev->dev, "initialized sunXi PIO driver\n"); |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1413 | |
| 1414 | return 0; |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1415 | |
Boris BREZILLON | e2bddc6 | 2014-04-10 15:52:41 +0200 | [diff] [blame] | 1416 | clk_error: |
| 1417 | clk_disable_unprepare(clk); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1418 | gpiochip_error: |
abdoulaye berthe | b4e7c55 | 2014-07-12 22:30:13 +0200 | [diff] [blame] | 1419 | gpiochip_remove(pctl->chip); |
Maxime Ripard | 08e9e61 | 2013-01-28 21:33:12 +0100 | [diff] [blame] | 1420 | return ret; |
Maxime Ripard | 0e37f88 | 2013-01-18 22:30:34 +0100 | [diff] [blame] | 1421 | } |