Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Sony Mobile Communications AB. |
| 3 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 15 | #include <linux/delay.h> |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/pinctrl/machine.h> |
| 22 | #include <linux/pinctrl/pinctrl.h> |
| 23 | #include <linux/pinctrl/pinmux.h> |
| 24 | #include <linux/pinctrl/pinconf.h> |
| 25 | #include <linux/pinctrl/pinconf-generic.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/interrupt.h> |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 30 | #include <linux/reboot.h> |
Stephen Boyd | ad64498 | 2015-07-06 18:09:30 -0700 | [diff] [blame] | 31 | #include <linux/pm.h> |
Stephen Boyd | 47a01ee | 2016-06-25 22:21:31 -0700 | [diff] [blame] | 32 | #include <linux/log2.h> |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 33 | |
Linus Walleij | 69b78b8 | 2014-07-09 13:55:12 +0200 | [diff] [blame] | 34 | #include "../core.h" |
| 35 | #include "../pinconf.h" |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 36 | #include "pinctrl-msm.h" |
Linus Walleij | 69b78b8 | 2014-07-09 13:55:12 +0200 | [diff] [blame] | 37 | #include "../pinctrl-utils.h" |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 38 | |
Bjorn Andersson | 408e3c6 | 2013-12-14 23:01:53 -0800 | [diff] [blame] | 39 | #define MAX_NR_GPIO 300 |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 40 | #define PS_HOLD_OFFSET 0x820 |
Bjorn Andersson | 408e3c6 | 2013-12-14 23:01:53 -0800 | [diff] [blame] | 41 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 42 | /** |
| 43 | * struct msm_pinctrl - state for a pinctrl-msm device |
| 44 | * @dev: device handle. |
| 45 | * @pctrl: pinctrl handle. |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 46 | * @chip: gpiochip handle. |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 47 | * @restart_nb: restart notifier block. |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 48 | * @irq: parent irq for the TLMM irq_chip. |
| 49 | * @lock: Spinlock to protect register resources as well |
| 50 | * as msm_pinctrl data structures. |
| 51 | * @enabled_irqs: Bitmap of currently enabled irqs. |
| 52 | * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge |
| 53 | * detection. |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 54 | * @soc; Reference to soc_data of platform specific data. |
| 55 | * @regs: Base address for the TLMM register map. |
| 56 | */ |
| 57 | struct msm_pinctrl { |
| 58 | struct device *dev; |
| 59 | struct pinctrl_dev *pctrl; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 60 | struct gpio_chip chip; |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 61 | struct notifier_block restart_nb; |
Bjorn Andersson | f393e48 | 2013-12-14 23:01:52 -0800 | [diff] [blame] | 62 | int irq; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 63 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 64 | raw_spinlock_t lock; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 65 | |
Bjorn Andersson | 408e3c6 | 2013-12-14 23:01:53 -0800 | [diff] [blame] | 66 | DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO); |
| 67 | DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 68 | |
| 69 | const struct msm_pinctrl_soc_data *soc; |
| 70 | void __iomem *regs; |
| 71 | }; |
| 72 | |
| 73 | static int msm_get_groups_count(struct pinctrl_dev *pctldev) |
| 74 | { |
| 75 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 76 | |
| 77 | return pctrl->soc->ngroups; |
| 78 | } |
| 79 | |
| 80 | static const char *msm_get_group_name(struct pinctrl_dev *pctldev, |
| 81 | unsigned group) |
| 82 | { |
| 83 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 84 | |
| 85 | return pctrl->soc->groups[group].name; |
| 86 | } |
| 87 | |
| 88 | static int msm_get_group_pins(struct pinctrl_dev *pctldev, |
| 89 | unsigned group, |
| 90 | const unsigned **pins, |
| 91 | unsigned *num_pins) |
| 92 | { |
| 93 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 94 | |
| 95 | *pins = pctrl->soc->groups[group].pins; |
| 96 | *num_pins = pctrl->soc->groups[group].npins; |
| 97 | return 0; |
| 98 | } |
| 99 | |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 100 | static const struct pinctrl_ops msm_pinctrl_ops = { |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 101 | .get_groups_count = msm_get_groups_count, |
| 102 | .get_group_name = msm_get_group_name, |
| 103 | .get_group_pins = msm_get_group_pins, |
| 104 | .dt_node_to_map = pinconf_generic_dt_node_to_map_group, |
Irina Tirdea | d32f7fd | 2016-03-31 14:44:42 +0300 | [diff] [blame] | 105 | .dt_free_map = pinctrl_utils_free_map, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static int msm_get_functions_count(struct pinctrl_dev *pctldev) |
| 109 | { |
| 110 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 111 | |
| 112 | return pctrl->soc->nfunctions; |
| 113 | } |
| 114 | |
| 115 | static const char *msm_get_function_name(struct pinctrl_dev *pctldev, |
| 116 | unsigned function) |
| 117 | { |
| 118 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 119 | |
| 120 | return pctrl->soc->functions[function].name; |
| 121 | } |
| 122 | |
| 123 | static int msm_get_function_groups(struct pinctrl_dev *pctldev, |
| 124 | unsigned function, |
| 125 | const char * const **groups, |
| 126 | unsigned * const num_groups) |
| 127 | { |
| 128 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 129 | |
| 130 | *groups = pctrl->soc->functions[function].groups; |
| 131 | *num_groups = pctrl->soc->functions[function].ngroups; |
| 132 | return 0; |
| 133 | } |
| 134 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 135 | static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev, |
| 136 | unsigned function, |
| 137 | unsigned group) |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 138 | { |
| 139 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 140 | const struct msm_pingroup *g; |
| 141 | unsigned long flags; |
Stephen Boyd | 47a01ee | 2016-06-25 22:21:31 -0700 | [diff] [blame] | 142 | u32 val, mask; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 143 | int i; |
| 144 | |
| 145 | g = &pctrl->soc->groups[group]; |
Stephen Boyd | 47a01ee | 2016-06-25 22:21:31 -0700 | [diff] [blame] | 146 | mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 147 | |
Bjorn Andersson | 3c25381 | 2014-03-31 14:49:55 -0700 | [diff] [blame] | 148 | for (i = 0; i < g->nfuncs; i++) { |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 149 | if (g->funcs[i] == function) |
| 150 | break; |
| 151 | } |
| 152 | |
Bjorn Andersson | 3c25381 | 2014-03-31 14:49:55 -0700 | [diff] [blame] | 153 | if (WARN_ON(i == g->nfuncs)) |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 154 | return -EINVAL; |
| 155 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 156 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 157 | |
| 158 | val = readl(pctrl->regs + g->ctl_reg); |
John Crispin | 6bcf3f6 | 2016-09-12 11:36:55 +0200 | [diff] [blame] | 159 | val &= ~mask; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 160 | val |= i << g->mux_bit; |
| 161 | writel(val, pctrl->regs + g->ctl_reg); |
| 162 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 163 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 168 | static const struct pinmux_ops msm_pinmux_ops = { |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 169 | .get_functions_count = msm_get_functions_count, |
| 170 | .get_function_name = msm_get_function_name, |
| 171 | .get_function_groups = msm_get_function_groups, |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 172 | .set_mux = msm_pinmux_set_mux, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static int msm_config_reg(struct msm_pinctrl *pctrl, |
| 176 | const struct msm_pingroup *g, |
| 177 | unsigned param, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 178 | unsigned *mask, |
| 179 | unsigned *bit) |
| 180 | { |
| 181 | switch (param) { |
| 182 | case PIN_CONFIG_BIAS_DISABLE: |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 183 | case PIN_CONFIG_BIAS_PULL_DOWN: |
Andy Gross | b831a15 | 2014-06-17 23:49:11 -0500 | [diff] [blame] | 184 | case PIN_CONFIG_BIAS_BUS_HOLD: |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 185 | case PIN_CONFIG_BIAS_PULL_UP: |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 186 | *bit = g->pull_bit; |
| 187 | *mask = 3; |
| 188 | break; |
| 189 | case PIN_CONFIG_DRIVE_STRENGTH: |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 190 | *bit = g->drv_bit; |
| 191 | *mask = 7; |
| 192 | break; |
Bjorn Andersson | ed118a5 | 2014-02-04 19:55:31 -0800 | [diff] [blame] | 193 | case PIN_CONFIG_OUTPUT: |
Stanimir Varbanov | 407f5e3 | 2015-03-04 12:41:57 +0200 | [diff] [blame] | 194 | case PIN_CONFIG_INPUT_ENABLE: |
Bjorn Andersson | ed118a5 | 2014-02-04 19:55:31 -0800 | [diff] [blame] | 195 | *bit = g->oe_bit; |
| 196 | *mask = 1; |
| 197 | break; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 198 | default: |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 199 | return -ENOTSUPP; |
| 200 | } |
| 201 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 205 | #define MSM_NO_PULL 0 |
| 206 | #define MSM_PULL_DOWN 1 |
Andy Gross | b831a15 | 2014-06-17 23:49:11 -0500 | [diff] [blame] | 207 | #define MSM_KEEPER 2 |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 208 | #define MSM_PULL_UP 3 |
| 209 | |
Stephen Boyd | 7cc34e2 | 2014-03-06 22:44:44 -0800 | [diff] [blame] | 210 | static unsigned msm_regval_to_drive(u32 val) |
| 211 | { |
| 212 | return (val + 1) * 2; |
| 213 | } |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 214 | |
| 215 | static int msm_config_group_get(struct pinctrl_dev *pctldev, |
| 216 | unsigned int group, |
| 217 | unsigned long *config) |
| 218 | { |
| 219 | const struct msm_pingroup *g; |
| 220 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 221 | unsigned param = pinconf_to_config_param(*config); |
| 222 | unsigned mask; |
| 223 | unsigned arg; |
| 224 | unsigned bit; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 225 | int ret; |
| 226 | u32 val; |
| 227 | |
| 228 | g = &pctrl->soc->groups[group]; |
| 229 | |
Stephen Boyd | 051a58b | 2014-03-06 22:44:46 -0800 | [diff] [blame] | 230 | ret = msm_config_reg(pctrl, g, param, &mask, &bit); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 231 | if (ret < 0) |
| 232 | return ret; |
| 233 | |
Stephen Boyd | 051a58b | 2014-03-06 22:44:46 -0800 | [diff] [blame] | 234 | val = readl(pctrl->regs + g->ctl_reg); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 235 | arg = (val >> bit) & mask; |
| 236 | |
| 237 | /* Convert register value to pinconf value */ |
| 238 | switch (param) { |
| 239 | case PIN_CONFIG_BIAS_DISABLE: |
| 240 | arg = arg == MSM_NO_PULL; |
| 241 | break; |
| 242 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 243 | arg = arg == MSM_PULL_DOWN; |
| 244 | break; |
Andy Gross | b831a15 | 2014-06-17 23:49:11 -0500 | [diff] [blame] | 245 | case PIN_CONFIG_BIAS_BUS_HOLD: |
| 246 | arg = arg == MSM_KEEPER; |
| 247 | break; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 248 | case PIN_CONFIG_BIAS_PULL_UP: |
| 249 | arg = arg == MSM_PULL_UP; |
| 250 | break; |
| 251 | case PIN_CONFIG_DRIVE_STRENGTH: |
Stephen Boyd | 7cc34e2 | 2014-03-06 22:44:44 -0800 | [diff] [blame] | 252 | arg = msm_regval_to_drive(arg); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 253 | break; |
Bjorn Andersson | ed118a5 | 2014-02-04 19:55:31 -0800 | [diff] [blame] | 254 | case PIN_CONFIG_OUTPUT: |
| 255 | /* Pin is not output */ |
| 256 | if (!arg) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | val = readl(pctrl->regs + g->io_reg); |
| 260 | arg = !!(val & BIT(g->in_bit)); |
| 261 | break; |
Stanimir Varbanov | 407f5e3 | 2015-03-04 12:41:57 +0200 | [diff] [blame] | 262 | case PIN_CONFIG_INPUT_ENABLE: |
| 263 | /* Pin is output */ |
| 264 | if (arg) |
| 265 | return -EINVAL; |
| 266 | arg = 1; |
| 267 | break; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 268 | default: |
Stanimir Varbanov | 38d756a | 2015-03-04 12:41:56 +0200 | [diff] [blame] | 269 | return -ENOTSUPP; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | *config = pinconf_to_config_packed(param, arg); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int msm_config_group_set(struct pinctrl_dev *pctldev, |
| 278 | unsigned group, |
| 279 | unsigned long *configs, |
| 280 | unsigned num_configs) |
| 281 | { |
| 282 | const struct msm_pingroup *g; |
| 283 | struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev); |
| 284 | unsigned long flags; |
| 285 | unsigned param; |
| 286 | unsigned mask; |
| 287 | unsigned arg; |
| 288 | unsigned bit; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 289 | int ret; |
| 290 | u32 val; |
| 291 | int i; |
| 292 | |
| 293 | g = &pctrl->soc->groups[group]; |
| 294 | |
| 295 | for (i = 0; i < num_configs; i++) { |
| 296 | param = pinconf_to_config_param(configs[i]); |
| 297 | arg = pinconf_to_config_argument(configs[i]); |
| 298 | |
Stephen Boyd | 051a58b | 2014-03-06 22:44:46 -0800 | [diff] [blame] | 299 | ret = msm_config_reg(pctrl, g, param, &mask, &bit); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 300 | if (ret < 0) |
| 301 | return ret; |
| 302 | |
| 303 | /* Convert pinconf values to register values */ |
| 304 | switch (param) { |
| 305 | case PIN_CONFIG_BIAS_DISABLE: |
| 306 | arg = MSM_NO_PULL; |
| 307 | break; |
| 308 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 309 | arg = MSM_PULL_DOWN; |
| 310 | break; |
Andy Gross | b831a15 | 2014-06-17 23:49:11 -0500 | [diff] [blame] | 311 | case PIN_CONFIG_BIAS_BUS_HOLD: |
| 312 | arg = MSM_KEEPER; |
| 313 | break; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 314 | case PIN_CONFIG_BIAS_PULL_UP: |
| 315 | arg = MSM_PULL_UP; |
| 316 | break; |
| 317 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 318 | /* Check for invalid values */ |
Stephen Boyd | 7cc34e2 | 2014-03-06 22:44:44 -0800 | [diff] [blame] | 319 | if (arg > 16 || arg < 2 || (arg % 2) != 0) |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 320 | arg = -1; |
| 321 | else |
Stephen Boyd | 7cc34e2 | 2014-03-06 22:44:44 -0800 | [diff] [blame] | 322 | arg = (arg / 2) - 1; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 323 | break; |
Bjorn Andersson | ed118a5 | 2014-02-04 19:55:31 -0800 | [diff] [blame] | 324 | case PIN_CONFIG_OUTPUT: |
| 325 | /* set output value */ |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 326 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | ed118a5 | 2014-02-04 19:55:31 -0800 | [diff] [blame] | 327 | val = readl(pctrl->regs + g->io_reg); |
| 328 | if (arg) |
| 329 | val |= BIT(g->out_bit); |
| 330 | else |
| 331 | val &= ~BIT(g->out_bit); |
| 332 | writel(val, pctrl->regs + g->io_reg); |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 333 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | ed118a5 | 2014-02-04 19:55:31 -0800 | [diff] [blame] | 334 | |
| 335 | /* enable output */ |
| 336 | arg = 1; |
| 337 | break; |
Stanimir Varbanov | 407f5e3 | 2015-03-04 12:41:57 +0200 | [diff] [blame] | 338 | case PIN_CONFIG_INPUT_ENABLE: |
| 339 | /* disable output */ |
| 340 | arg = 0; |
| 341 | break; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 342 | default: |
| 343 | dev_err(pctrl->dev, "Unsupported config parameter: %x\n", |
| 344 | param); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | /* Range-check user-supplied value */ |
| 349 | if (arg & ~mask) { |
| 350 | dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg); |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 354 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Stephen Boyd | 051a58b | 2014-03-06 22:44:46 -0800 | [diff] [blame] | 355 | val = readl(pctrl->regs + g->ctl_reg); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 356 | val &= ~(mask << bit); |
| 357 | val |= arg << bit; |
Stephen Boyd | 051a58b | 2014-03-06 22:44:46 -0800 | [diff] [blame] | 358 | writel(val, pctrl->regs + g->ctl_reg); |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 359 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 365 | static const struct pinconf_ops msm_pinconf_ops = { |
Stanimir Varbanov | 38d756a | 2015-03-04 12:41:56 +0200 | [diff] [blame] | 366 | .is_generic = true, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 367 | .pin_config_group_get = msm_config_group_get, |
| 368 | .pin_config_group_set = msm_config_group_set, |
| 369 | }; |
| 370 | |
| 371 | static struct pinctrl_desc msm_pinctrl_desc = { |
| 372 | .pctlops = &msm_pinctrl_ops, |
| 373 | .pmxops = &msm_pinmux_ops, |
| 374 | .confops = &msm_pinconf_ops, |
| 375 | .owner = THIS_MODULE, |
| 376 | }; |
| 377 | |
| 378 | static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 379 | { |
| 380 | const struct msm_pingroup *g; |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 381 | struct msm_pinctrl *pctrl = gpiochip_get_data(chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 382 | unsigned long flags; |
| 383 | u32 val; |
| 384 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 385 | g = &pctrl->soc->groups[offset]; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 386 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 387 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 388 | |
| 389 | val = readl(pctrl->regs + g->ctl_reg); |
| 390 | val &= ~BIT(g->oe_bit); |
| 391 | writel(val, pctrl->regs + g->ctl_reg); |
| 392 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 393 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value) |
| 399 | { |
| 400 | const struct msm_pingroup *g; |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 401 | struct msm_pinctrl *pctrl = gpiochip_get_data(chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 402 | unsigned long flags; |
| 403 | u32 val; |
| 404 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 405 | g = &pctrl->soc->groups[offset]; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 406 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 407 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 408 | |
Axel Lin | e476e77 | 2013-12-13 21:35:55 +0800 | [diff] [blame] | 409 | val = readl(pctrl->regs + g->io_reg); |
| 410 | if (value) |
| 411 | val |= BIT(g->out_bit); |
| 412 | else |
| 413 | val &= ~BIT(g->out_bit); |
| 414 | writel(val, pctrl->regs + g->io_reg); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 415 | |
| 416 | val = readl(pctrl->regs + g->ctl_reg); |
| 417 | val |= BIT(g->oe_bit); |
| 418 | writel(val, pctrl->regs + g->ctl_reg); |
| 419 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 420 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
Timur Tabi | 8e51533 | 2017-02-10 17:21:00 -0600 | [diff] [blame] | 425 | static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) |
| 426 | { |
| 427 | struct msm_pinctrl *pctrl = gpiochip_get_data(chip); |
| 428 | const struct msm_pingroup *g; |
| 429 | u32 val; |
| 430 | |
| 431 | g = &pctrl->soc->groups[offset]; |
| 432 | |
| 433 | val = readl(pctrl->regs + g->ctl_reg); |
| 434 | |
| 435 | /* 0 = output, 1 = input */ |
| 436 | return val & BIT(g->oe_bit) ? 0 : 1; |
| 437 | } |
| 438 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 439 | static int msm_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 440 | { |
| 441 | const struct msm_pingroup *g; |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 442 | struct msm_pinctrl *pctrl = gpiochip_get_data(chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 443 | u32 val; |
| 444 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 445 | g = &pctrl->soc->groups[offset]; |
| 446 | |
| 447 | val = readl(pctrl->regs + g->io_reg); |
| 448 | return !!(val & BIT(g->in_bit)); |
| 449 | } |
| 450 | |
| 451 | static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 452 | { |
| 453 | const struct msm_pingroup *g; |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 454 | struct msm_pinctrl *pctrl = gpiochip_get_data(chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 455 | unsigned long flags; |
| 456 | u32 val; |
| 457 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 458 | g = &pctrl->soc->groups[offset]; |
| 459 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 460 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 461 | |
| 462 | val = readl(pctrl->regs + g->io_reg); |
Axel Lin | e476e77 | 2013-12-13 21:35:55 +0800 | [diff] [blame] | 463 | if (value) |
| 464 | val |= BIT(g->out_bit); |
| 465 | else |
| 466 | val &= ~BIT(g->out_bit); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 467 | writel(val, pctrl->regs + g->io_reg); |
| 468 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 469 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 472 | #ifdef CONFIG_DEBUG_FS |
| 473 | #include <linux/seq_file.h> |
| 474 | |
| 475 | static void msm_gpio_dbg_show_one(struct seq_file *s, |
| 476 | struct pinctrl_dev *pctldev, |
| 477 | struct gpio_chip *chip, |
| 478 | unsigned offset, |
| 479 | unsigned gpio) |
| 480 | { |
| 481 | const struct msm_pingroup *g; |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 482 | struct msm_pinctrl *pctrl = gpiochip_get_data(chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 483 | unsigned func; |
| 484 | int is_out; |
| 485 | int drive; |
| 486 | int pull; |
| 487 | u32 ctl_reg; |
| 488 | |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 489 | static const char * const pulls[] = { |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 490 | "no pull", |
| 491 | "pull down", |
| 492 | "keeper", |
| 493 | "pull up" |
| 494 | }; |
| 495 | |
| 496 | g = &pctrl->soc->groups[offset]; |
| 497 | ctl_reg = readl(pctrl->regs + g->ctl_reg); |
| 498 | |
| 499 | is_out = !!(ctl_reg & BIT(g->oe_bit)); |
| 500 | func = (ctl_reg >> g->mux_bit) & 7; |
| 501 | drive = (ctl_reg >> g->drv_bit) & 7; |
| 502 | pull = (ctl_reg >> g->pull_bit) & 3; |
| 503 | |
| 504 | seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func); |
Stephen Boyd | 7cc34e2 | 2014-03-06 22:44:44 -0800 | [diff] [blame] | 505 | seq_printf(s, " %dmA", msm_regval_to_drive(drive)); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 506 | seq_printf(s, " %s", pulls[pull]); |
| 507 | } |
| 508 | |
| 509 | static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 510 | { |
| 511 | unsigned gpio = chip->base; |
| 512 | unsigned i; |
| 513 | |
| 514 | for (i = 0; i < chip->ngpio; i++, gpio++) { |
| 515 | msm_gpio_dbg_show_one(s, NULL, chip, i, gpio); |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 516 | seq_puts(s, "\n"); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | #else |
| 521 | #define msm_gpio_dbg_show NULL |
| 522 | #endif |
| 523 | |
| 524 | static struct gpio_chip msm_gpio_template = { |
| 525 | .direction_input = msm_gpio_direction_input, |
| 526 | .direction_output = msm_gpio_direction_output, |
Timur Tabi | 8e51533 | 2017-02-10 17:21:00 -0600 | [diff] [blame] | 527 | .get_direction = msm_gpio_get_direction, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 528 | .get = msm_gpio_get, |
| 529 | .set = msm_gpio_set, |
Jonas Gorski | 98c85d5 | 2015-10-11 17:34:19 +0200 | [diff] [blame] | 530 | .request = gpiochip_generic_request, |
| 531 | .free = gpiochip_generic_free, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 532 | .dbg_show = msm_gpio_dbg_show, |
| 533 | }; |
| 534 | |
| 535 | /* For dual-edge interrupts in software, since some hardware has no |
| 536 | * such support: |
| 537 | * |
| 538 | * At appropriate moments, this function may be called to flip the polarity |
| 539 | * settings of both-edge irq lines to try and catch the next edge. |
| 540 | * |
| 541 | * The attempt is considered successful if: |
| 542 | * - the status bit goes high, indicating that an edge was caught, or |
| 543 | * - the input value of the gpio doesn't change during the attempt. |
| 544 | * If the value changes twice during the process, that would cause the first |
| 545 | * test to fail but would force the second, as two opposite |
| 546 | * transitions would cause a detection no matter the polarity setting. |
| 547 | * |
| 548 | * The do-loop tries to sledge-hammer closed the timing hole between |
| 549 | * the initial value-read and the polarity-write - if the line value changes |
| 550 | * during that window, an interrupt is lost, the new polarity setting is |
| 551 | * incorrect, and the first success test will fail, causing a retry. |
| 552 | * |
| 553 | * Algorithm comes from Google's msmgpio driver. |
| 554 | */ |
| 555 | static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl, |
| 556 | const struct msm_pingroup *g, |
| 557 | struct irq_data *d) |
| 558 | { |
| 559 | int loop_limit = 100; |
| 560 | unsigned val, val2, intstat; |
| 561 | unsigned pol; |
| 562 | |
| 563 | do { |
| 564 | val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit); |
| 565 | |
| 566 | pol = readl(pctrl->regs + g->intr_cfg_reg); |
| 567 | pol ^= BIT(g->intr_polarity_bit); |
| 568 | writel(pol, pctrl->regs + g->intr_cfg_reg); |
| 569 | |
| 570 | val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit); |
| 571 | intstat = readl(pctrl->regs + g->intr_status_reg); |
| 572 | if (intstat || (val == val2)) |
| 573 | return; |
| 574 | } while (loop_limit-- > 0); |
| 575 | dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n", |
| 576 | val, val2); |
| 577 | } |
| 578 | |
| 579 | static void msm_gpio_irq_mask(struct irq_data *d) |
| 580 | { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 581 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 582 | struct msm_pinctrl *pctrl = gpiochip_get_data(gc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 583 | const struct msm_pingroup *g; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 584 | unsigned long flags; |
| 585 | u32 val; |
| 586 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 587 | g = &pctrl->soc->groups[d->hwirq]; |
| 588 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 589 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 590 | |
| 591 | val = readl(pctrl->regs + g->intr_cfg_reg); |
| 592 | val &= ~BIT(g->intr_enable_bit); |
| 593 | writel(val, pctrl->regs + g->intr_cfg_reg); |
| 594 | |
| 595 | clear_bit(d->hwirq, pctrl->enabled_irqs); |
| 596 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 597 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void msm_gpio_irq_unmask(struct irq_data *d) |
| 601 | { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 602 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 603 | struct msm_pinctrl *pctrl = gpiochip_get_data(gc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 604 | const struct msm_pingroup *g; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 605 | unsigned long flags; |
| 606 | u32 val; |
| 607 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 608 | g = &pctrl->soc->groups[d->hwirq]; |
| 609 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 610 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 611 | |
| 612 | val = readl(pctrl->regs + g->intr_status_reg); |
| 613 | val &= ~BIT(g->intr_status_bit); |
| 614 | writel(val, pctrl->regs + g->intr_status_reg); |
| 615 | |
| 616 | val = readl(pctrl->regs + g->intr_cfg_reg); |
| 617 | val |= BIT(g->intr_enable_bit); |
| 618 | writel(val, pctrl->regs + g->intr_cfg_reg); |
| 619 | |
| 620 | set_bit(d->hwirq, pctrl->enabled_irqs); |
| 621 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 622 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static void msm_gpio_irq_ack(struct irq_data *d) |
| 626 | { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 627 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 628 | struct msm_pinctrl *pctrl = gpiochip_get_data(gc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 629 | const struct msm_pingroup *g; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 630 | unsigned long flags; |
| 631 | u32 val; |
| 632 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 633 | g = &pctrl->soc->groups[d->hwirq]; |
| 634 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 635 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 636 | |
| 637 | val = readl(pctrl->regs + g->intr_status_reg); |
Bjorn Andersson | 48f15e9 | 2014-03-31 14:49:54 -0700 | [diff] [blame] | 638 | if (g->intr_ack_high) |
| 639 | val |= BIT(g->intr_status_bit); |
| 640 | else |
| 641 | val &= ~BIT(g->intr_status_bit); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 642 | writel(val, pctrl->regs + g->intr_status_reg); |
| 643 | |
| 644 | if (test_bit(d->hwirq, pctrl->dual_edge_irqs)) |
| 645 | msm_gpio_update_dual_edge_pos(pctrl, g, d); |
| 646 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 647 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 650 | static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 651 | { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 652 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 653 | struct msm_pinctrl *pctrl = gpiochip_get_data(gc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 654 | const struct msm_pingroup *g; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 655 | unsigned long flags; |
| 656 | u32 val; |
| 657 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 658 | g = &pctrl->soc->groups[d->hwirq]; |
| 659 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 660 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 661 | |
| 662 | /* |
| 663 | * For hw without possibility of detecting both edges |
| 664 | */ |
| 665 | if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH) |
| 666 | set_bit(d->hwirq, pctrl->dual_edge_irqs); |
| 667 | else |
| 668 | clear_bit(d->hwirq, pctrl->dual_edge_irqs); |
| 669 | |
| 670 | /* Route interrupts to application cpu */ |
| 671 | val = readl(pctrl->regs + g->intr_target_reg); |
| 672 | val &= ~(7 << g->intr_target_bit); |
Georgi Djakov | f712c55 | 2014-09-03 19:28:16 +0300 | [diff] [blame] | 673 | val |= g->intr_target_kpss_val << g->intr_target_bit; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 674 | writel(val, pctrl->regs + g->intr_target_reg); |
| 675 | |
| 676 | /* Update configuration for gpio. |
| 677 | * RAW_STATUS_EN is left on for all gpio irqs. Due to the |
| 678 | * internal circuitry of TLMM, toggling the RAW_STATUS |
| 679 | * could cause the INTR_STATUS to be set for EDGE interrupts. |
| 680 | */ |
| 681 | val = readl(pctrl->regs + g->intr_cfg_reg); |
| 682 | val |= BIT(g->intr_raw_status_bit); |
| 683 | if (g->intr_detection_width == 2) { |
| 684 | val &= ~(3 << g->intr_detection_bit); |
| 685 | val &= ~(1 << g->intr_polarity_bit); |
| 686 | switch (type) { |
| 687 | case IRQ_TYPE_EDGE_RISING: |
| 688 | val |= 1 << g->intr_detection_bit; |
| 689 | val |= BIT(g->intr_polarity_bit); |
| 690 | break; |
| 691 | case IRQ_TYPE_EDGE_FALLING: |
| 692 | val |= 2 << g->intr_detection_bit; |
| 693 | val |= BIT(g->intr_polarity_bit); |
| 694 | break; |
| 695 | case IRQ_TYPE_EDGE_BOTH: |
| 696 | val |= 3 << g->intr_detection_bit; |
| 697 | val |= BIT(g->intr_polarity_bit); |
| 698 | break; |
| 699 | case IRQ_TYPE_LEVEL_LOW: |
| 700 | break; |
| 701 | case IRQ_TYPE_LEVEL_HIGH: |
| 702 | val |= BIT(g->intr_polarity_bit); |
| 703 | break; |
| 704 | } |
| 705 | } else if (g->intr_detection_width == 1) { |
| 706 | val &= ~(1 << g->intr_detection_bit); |
| 707 | val &= ~(1 << g->intr_polarity_bit); |
| 708 | switch (type) { |
| 709 | case IRQ_TYPE_EDGE_RISING: |
| 710 | val |= BIT(g->intr_detection_bit); |
| 711 | val |= BIT(g->intr_polarity_bit); |
| 712 | break; |
| 713 | case IRQ_TYPE_EDGE_FALLING: |
| 714 | val |= BIT(g->intr_detection_bit); |
| 715 | break; |
| 716 | case IRQ_TYPE_EDGE_BOTH: |
| 717 | val |= BIT(g->intr_detection_bit); |
Bjorn Andersson | 48f15e9 | 2014-03-31 14:49:54 -0700 | [diff] [blame] | 718 | val |= BIT(g->intr_polarity_bit); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 719 | break; |
| 720 | case IRQ_TYPE_LEVEL_LOW: |
| 721 | break; |
| 722 | case IRQ_TYPE_LEVEL_HIGH: |
| 723 | val |= BIT(g->intr_polarity_bit); |
| 724 | break; |
| 725 | } |
| 726 | } else { |
| 727 | BUG(); |
| 728 | } |
| 729 | writel(val, pctrl->regs + g->intr_cfg_reg); |
| 730 | |
| 731 | if (test_bit(d->hwirq, pctrl->dual_edge_irqs)) |
| 732 | msm_gpio_update_dual_edge_pos(pctrl, g, d); |
| 733 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 734 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 735 | |
| 736 | if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) |
Thomas Gleixner | 34c0ad8 | 2015-06-23 15:52:51 +0200 | [diff] [blame] | 737 | irq_set_handler_locked(d, handle_level_irq); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 738 | else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) |
Thomas Gleixner | 34c0ad8 | 2015-06-23 15:52:51 +0200 | [diff] [blame] | 739 | irq_set_handler_locked(d, handle_edge_irq); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on) |
| 745 | { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 746 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 747 | struct msm_pinctrl *pctrl = gpiochip_get_data(gc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 748 | unsigned long flags; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 749 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 750 | raw_spin_lock_irqsave(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 751 | |
Josh Cartwright | 6aced33 | 2014-03-05 13:33:08 -0600 | [diff] [blame] | 752 | irq_set_irq_wake(pctrl->irq, on); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 753 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 754 | raw_spin_unlock_irqrestore(&pctrl->lock, flags); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 759 | static struct irq_chip msm_gpio_irq_chip = { |
| 760 | .name = "msmgpio", |
| 761 | .irq_mask = msm_gpio_irq_mask, |
| 762 | .irq_unmask = msm_gpio_irq_unmask, |
| 763 | .irq_ack = msm_gpio_irq_ack, |
| 764 | .irq_set_type = msm_gpio_irq_set_type, |
| 765 | .irq_set_wake = msm_gpio_irq_set_wake, |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 766 | }; |
| 767 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 768 | static void msm_gpio_irq_handler(struct irq_desc *desc) |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 769 | { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 770 | struct gpio_chip *gc = irq_desc_get_handler_data(desc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 771 | const struct msm_pingroup *g; |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 772 | struct msm_pinctrl *pctrl = gpiochip_get_data(gc); |
Jiang Liu | 5663bb2 | 2015-06-04 12:13:16 +0800 | [diff] [blame] | 773 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 774 | int irq_pin; |
| 775 | int handled = 0; |
| 776 | u32 val; |
| 777 | int i; |
| 778 | |
| 779 | chained_irq_enter(chip, desc); |
| 780 | |
| 781 | /* |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 782 | * Each pin has it's own IRQ status register, so use |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 783 | * enabled_irq bitmap to limit the number of reads. |
| 784 | */ |
| 785 | for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) { |
| 786 | g = &pctrl->soc->groups[i]; |
| 787 | val = readl(pctrl->regs + g->intr_status_reg); |
| 788 | if (val & BIT(g->intr_status_bit)) { |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 789 | irq_pin = irq_find_mapping(gc->irqdomain, i); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 790 | generic_handle_irq(irq_pin); |
| 791 | handled++; |
| 792 | } |
| 793 | } |
| 794 | |
Bjorn Andersson | 1f2b239 | 2013-12-14 23:01:51 -0800 | [diff] [blame] | 795 | /* No interrupts were flagged */ |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 796 | if (handled == 0) |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 797 | handle_bad_irq(desc); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 798 | |
| 799 | chained_irq_exit(chip, desc); |
| 800 | } |
| 801 | |
| 802 | static int msm_gpio_init(struct msm_pinctrl *pctrl) |
| 803 | { |
| 804 | struct gpio_chip *chip; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 805 | int ret; |
Stephen Boyd | dcd278b | 2014-03-06 22:44:41 -0800 | [diff] [blame] | 806 | unsigned ngpio = pctrl->soc->ngpios; |
| 807 | |
| 808 | if (WARN_ON(ngpio > MAX_NR_GPIO)) |
| 809 | return -EINVAL; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 810 | |
| 811 | chip = &pctrl->chip; |
| 812 | chip->base = 0; |
Stephen Boyd | dcd278b | 2014-03-06 22:44:41 -0800 | [diff] [blame] | 813 | chip->ngpio = ngpio; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 814 | chip->label = dev_name(pctrl->dev); |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 815 | chip->parent = pctrl->dev; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 816 | chip->owner = THIS_MODULE; |
| 817 | chip->of_node = pctrl->dev->of_node; |
| 818 | |
Linus Walleij | fded3f4 | 2015-12-08 09:49:18 +0100 | [diff] [blame] | 819 | ret = gpiochip_add_data(&pctrl->chip, pctrl); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 820 | if (ret) { |
| 821 | dev_err(pctrl->dev, "Failed register gpiochip\n"); |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio); |
| 826 | if (ret) { |
| 827 | dev_err(pctrl->dev, "Failed to add pin range\n"); |
Pramod Gurav | c6e927a | 2014-08-29 13:41:48 +0530 | [diff] [blame] | 828 | gpiochip_remove(&pctrl->chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 829 | return ret; |
| 830 | } |
| 831 | |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 832 | ret = gpiochip_irqchip_add(chip, |
| 833 | &msm_gpio_irq_chip, |
| 834 | 0, |
| 835 | handle_edge_irq, |
| 836 | IRQ_TYPE_NONE); |
| 837 | if (ret) { |
| 838 | dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n"); |
Pramod Gurav | c6e927a | 2014-08-29 13:41:48 +0530 | [diff] [blame] | 839 | gpiochip_remove(&pctrl->chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 840 | return -ENOSYS; |
| 841 | } |
| 842 | |
Linus Walleij | cdcb0ab | 2014-04-29 11:00:40 -0700 | [diff] [blame] | 843 | gpiochip_set_chained_irqchip(chip, &msm_gpio_irq_chip, pctrl->irq, |
| 844 | msm_gpio_irq_handler); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 849 | static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action, |
| 850 | void *data) |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 851 | { |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 852 | struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb); |
| 853 | |
| 854 | writel(0, pctrl->regs + PS_HOLD_OFFSET); |
| 855 | mdelay(1000); |
| 856 | return NOTIFY_DONE; |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 857 | } |
| 858 | |
Stephen Boyd | ad64498 | 2015-07-06 18:09:30 -0700 | [diff] [blame] | 859 | static struct msm_pinctrl *poweroff_pctrl; |
| 860 | |
| 861 | static void msm_ps_hold_poweroff(void) |
| 862 | { |
| 863 | msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL); |
| 864 | } |
| 865 | |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 866 | static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl) |
| 867 | { |
Stephen Boyd | bcd53f8 | 2015-01-19 11:17:45 +0100 | [diff] [blame] | 868 | int i; |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 869 | const struct msm_function *func = pctrl->soc->functions; |
| 870 | |
Stephen Boyd | bcd53f8 | 2015-01-19 11:17:45 +0100 | [diff] [blame] | 871 | for (i = 0; i < pctrl->soc->nfunctions; i++) |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 872 | if (!strcmp(func[i].name, "ps_hold")) { |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 873 | pctrl->restart_nb.notifier_call = msm_ps_hold_restart; |
| 874 | pctrl->restart_nb.priority = 128; |
| 875 | if (register_restart_handler(&pctrl->restart_nb)) |
| 876 | dev_err(pctrl->dev, |
| 877 | "failed to setup restart handler.\n"); |
Stephen Boyd | ad64498 | 2015-07-06 18:09:30 -0700 | [diff] [blame] | 878 | poweroff_pctrl = pctrl; |
| 879 | pm_power_off = msm_ps_hold_poweroff; |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 880 | break; |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 881 | } |
| 882 | } |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 883 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 884 | int msm_pinctrl_probe(struct platform_device *pdev, |
| 885 | const struct msm_pinctrl_soc_data *soc_data) |
| 886 | { |
| 887 | struct msm_pinctrl *pctrl; |
| 888 | struct resource *res; |
| 889 | int ret; |
| 890 | |
| 891 | pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL); |
| 892 | if (!pctrl) { |
| 893 | dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n"); |
| 894 | return -ENOMEM; |
| 895 | } |
| 896 | pctrl->dev = &pdev->dev; |
| 897 | pctrl->soc = soc_data; |
| 898 | pctrl->chip = msm_gpio_template; |
| 899 | |
Julia Cartwright | 47b03ca | 2017-01-20 10:13:47 -0600 | [diff] [blame] | 900 | raw_spin_lock_init(&pctrl->lock); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 901 | |
| 902 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 903 | pctrl->regs = devm_ioremap_resource(&pdev->dev, res); |
| 904 | if (IS_ERR(pctrl->regs)) |
| 905 | return PTR_ERR(pctrl->regs); |
| 906 | |
Pramod Gurav | 3274558 | 2014-08-29 20:00:59 +0530 | [diff] [blame] | 907 | msm_pinctrl_setup_pm_reset(pctrl); |
| 908 | |
Bjorn Andersson | f393e48 | 2013-12-14 23:01:52 -0800 | [diff] [blame] | 909 | pctrl->irq = platform_get_irq(pdev, 0); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 910 | if (pctrl->irq < 0) { |
| 911 | dev_err(&pdev->dev, "No interrupt defined for msmgpio\n"); |
| 912 | return pctrl->irq; |
| 913 | } |
| 914 | |
| 915 | msm_pinctrl_desc.name = dev_name(&pdev->dev); |
| 916 | msm_pinctrl_desc.pins = pctrl->soc->pins; |
| 917 | msm_pinctrl_desc.npins = pctrl->soc->npins; |
Laxman Dewangan | fe0267f | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 918 | pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &msm_pinctrl_desc, |
| 919 | pctrl); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 920 | if (IS_ERR(pctrl->pctrl)) { |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 921 | dev_err(&pdev->dev, "Couldn't register pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 922 | return PTR_ERR(pctrl->pctrl); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | ret = msm_gpio_init(pctrl); |
Laxman Dewangan | fe0267f | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 926 | if (ret) |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 927 | return ret; |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 928 | |
| 929 | platform_set_drvdata(pdev, pctrl); |
| 930 | |
| 931 | dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n"); |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | EXPORT_SYMBOL(msm_pinctrl_probe); |
| 936 | |
| 937 | int msm_pinctrl_remove(struct platform_device *pdev) |
| 938 | { |
| 939 | struct msm_pinctrl *pctrl = platform_get_drvdata(pdev); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 940 | |
Linus Walleij | 2fcea6c | 2014-09-16 15:05:41 -0700 | [diff] [blame] | 941 | gpiochip_remove(&pctrl->chip); |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 942 | |
Josh Cartwright | cf1fc18 | 2014-09-23 15:59:53 -0500 | [diff] [blame] | 943 | unregister_restart_handler(&pctrl->restart_nb); |
| 944 | |
Bjorn Andersson | f365be0 | 2013-12-05 18:10:03 -0800 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | EXPORT_SYMBOL(msm_pinctrl_remove); |
| 948 | |