blob: b5331ff0e0662f69a605ee5492e46171a5f7ea11 [file] [log] [blame]
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001/*
2 * Copyright (c) 2013, Sony Mobile Communications AB.
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06003 * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
Bjorn Anderssonf365be02013-12-05 18:10:03 -08004 *
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 Gurav32745582014-08-29 20:00:59 +053015#include <linux/delay.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080016#include <linux/err.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080017#include <linux/io.h>
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -060018#include <linux/irq.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080019#include <linux/module.h>
20#include <linux/of.h>
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -060021#include <linux/of_irq.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080022#include <linux/platform_device.h>
23#include <linux/pinctrl/machine.h>
24#include <linux/pinctrl/pinctrl.h>
25#include <linux/pinctrl/pinmux.h>
26#include <linux/pinctrl/pinconf.h>
27#include <linux/pinctrl/pinconf-generic.h>
28#include <linux/slab.h>
29#include <linux/gpio.h>
30#include <linux/interrupt.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080031#include <linux/spinlock.h>
Josh Cartwrightcf1fc182014-09-23 15:59:53 -050032#include <linux/reboot.h>
Stephen Boydad644982015-07-06 18:09:30 -070033#include <linux/pm.h>
Stephen Boyd47a01ee2016-06-25 22:21:31 -070034#include <linux/log2.h>
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -060035#include <linux/irq.h>
Linus Walleij69b78b82014-07-09 13:55:12 +020036#include "../core.h"
37#include "../pinconf.h"
Bjorn Anderssonf365be02013-12-05 18:10:03 -080038#include "pinctrl-msm.h"
Linus Walleij69b78b82014-07-09 13:55:12 +020039#include "../pinctrl-utils.h"
Bjorn Anderssonf365be02013-12-05 18:10:03 -080040
Bjorn Andersson408e3c62013-12-14 23:01:53 -080041#define MAX_NR_GPIO 300
Pramod Gurav32745582014-08-29 20:00:59 +053042#define PS_HOLD_OFFSET 0x820
Bjorn Andersson408e3c62013-12-14 23:01:53 -080043
Bjorn Anderssonf365be02013-12-05 18:10:03 -080044/**
45 * struct msm_pinctrl - state for a pinctrl-msm device
46 * @dev: device handle.
47 * @pctrl: pinctrl handle.
Bjorn Anderssonf365be02013-12-05 18:10:03 -080048 * @chip: gpiochip handle.
Josh Cartwrightcf1fc182014-09-23 15:59:53 -050049 * @restart_nb: restart notifier block.
Bjorn Anderssonf365be02013-12-05 18:10:03 -080050 * @irq: parent irq for the TLMM irq_chip.
51 * @lock: Spinlock to protect register resources as well
52 * as msm_pinctrl data structures.
53 * @enabled_irqs: Bitmap of currently enabled irqs.
54 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
55 * detection.
Bjorn Anderssonf365be02013-12-05 18:10:03 -080056 * @soc; Reference to soc_data of platform specific data.
57 * @regs: Base address for the TLMM register map.
58 */
59struct msm_pinctrl {
60 struct device *dev;
61 struct pinctrl_dev *pctrl;
Bjorn Anderssonf365be02013-12-05 18:10:03 -080062 struct gpio_chip chip;
Josh Cartwrightcf1fc182014-09-23 15:59:53 -050063 struct notifier_block restart_nb;
Bjorn Anderssonf393e482013-12-14 23:01:52 -080064 int irq;
Bjorn Anderssonf365be02013-12-05 18:10:03 -080065
66 spinlock_t lock;
67
Bjorn Andersson408e3c62013-12-14 23:01:53 -080068 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
69 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
Bjorn Anderssonf365be02013-12-05 18:10:03 -080070
71 const struct msm_pinctrl_soc_data *soc;
72 void __iomem *regs;
Archana Sathyakumar892c01c2017-10-31 13:47:20 -060073 void __iomem *pdc_regs;
Bjorn Anderssonf365be02013-12-05 18:10:03 -080074};
75
76static int msm_get_groups_count(struct pinctrl_dev *pctldev)
77{
78 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
79
80 return pctrl->soc->ngroups;
81}
82
83static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
84 unsigned group)
85{
86 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
87
88 return pctrl->soc->groups[group].name;
89}
90
91static int msm_get_group_pins(struct pinctrl_dev *pctldev,
92 unsigned group,
93 const unsigned **pins,
94 unsigned *num_pins)
95{
96 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
97
98 *pins = pctrl->soc->groups[group].pins;
99 *num_pins = pctrl->soc->groups[group].npins;
100 return 0;
101}
102
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800103static const struct pinctrl_ops msm_pinctrl_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800104 .get_groups_count = msm_get_groups_count,
105 .get_group_name = msm_get_group_name,
106 .get_group_pins = msm_get_group_pins,
107 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300108 .dt_free_map = pinctrl_utils_free_map,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800109};
110
111static int msm_get_functions_count(struct pinctrl_dev *pctldev)
112{
113 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
114
115 return pctrl->soc->nfunctions;
116}
117
118static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
119 unsigned function)
120{
121 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
122
123 return pctrl->soc->functions[function].name;
124}
125
126static int msm_get_function_groups(struct pinctrl_dev *pctldev,
127 unsigned function,
128 const char * const **groups,
129 unsigned * const num_groups)
130{
131 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
132
133 *groups = pctrl->soc->functions[function].groups;
134 *num_groups = pctrl->soc->functions[function].ngroups;
135 return 0;
136}
137
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200138static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
139 unsigned function,
140 unsigned group)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800141{
142 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
143 const struct msm_pingroup *g;
144 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700145 u32 val, mask;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800146 int i;
147
148 g = &pctrl->soc->groups[group];
Stephen Boyd47a01ee2016-06-25 22:21:31 -0700149 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800150
Bjorn Andersson3c253812014-03-31 14:49:55 -0700151 for (i = 0; i < g->nfuncs; i++) {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800152 if (g->funcs[i] == function)
153 break;
154 }
155
Bjorn Andersson3c253812014-03-31 14:49:55 -0700156 if (WARN_ON(i == g->nfuncs))
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800157 return -EINVAL;
158
159 spin_lock_irqsave(&pctrl->lock, flags);
160
Kyle Yan6c2752f2017-09-27 16:29:45 -0700161 val = readl(pctrl->regs + g->ctl_reg);
John Crispin6bcf3f62016-09-12 11:36:55 +0200162 val &= ~mask;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800163 val |= i << g->mux_bit;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700164 writel(val, pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800165
166 spin_unlock_irqrestore(&pctrl->lock, flags);
167
168 return 0;
169}
170
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800171static const struct pinmux_ops msm_pinmux_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800172 .get_functions_count = msm_get_functions_count,
173 .get_function_name = msm_get_function_name,
174 .get_function_groups = msm_get_function_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200175 .set_mux = msm_pinmux_set_mux,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800176};
177
178static int msm_config_reg(struct msm_pinctrl *pctrl,
179 const struct msm_pingroup *g,
180 unsigned param,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800181 unsigned *mask,
182 unsigned *bit)
183{
184 switch (param) {
185 case PIN_CONFIG_BIAS_DISABLE:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800186 case PIN_CONFIG_BIAS_PULL_DOWN:
Andy Grossb831a152014-06-17 23:49:11 -0500187 case PIN_CONFIG_BIAS_BUS_HOLD:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800188 case PIN_CONFIG_BIAS_PULL_UP:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800189 *bit = g->pull_bit;
190 *mask = 3;
191 break;
192 case PIN_CONFIG_DRIVE_STRENGTH:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800193 *bit = g->drv_bit;
194 *mask = 7;
195 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800196 case PIN_CONFIG_OUTPUT:
Stanimir Varbanov407f5e32015-03-04 12:41:57 +0200197 case PIN_CONFIG_INPUT_ENABLE:
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800198 *bit = g->oe_bit;
199 *mask = 1;
200 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800201 default:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800202 return -ENOTSUPP;
203 }
204
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800205 return 0;
206}
207
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800208#define MSM_NO_PULL 0
209#define MSM_PULL_DOWN 1
Andy Grossb831a152014-06-17 23:49:11 -0500210#define MSM_KEEPER 2
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800211#define MSM_PULL_UP 3
212
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800213static unsigned msm_regval_to_drive(u32 val)
214{
215 return (val + 1) * 2;
216}
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800217
218static int msm_config_group_get(struct pinctrl_dev *pctldev,
219 unsigned int group,
220 unsigned long *config)
221{
222 const struct msm_pingroup *g;
223 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
224 unsigned param = pinconf_to_config_param(*config);
225 unsigned mask;
226 unsigned arg;
227 unsigned bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800228 int ret;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700229 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800230
231 g = &pctrl->soc->groups[group];
232
Stephen Boyd051a58b2014-03-06 22:44:46 -0800233 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800234 if (ret < 0)
235 return ret;
236
Kyle Yan6c2752f2017-09-27 16:29:45 -0700237 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800238 arg = (val >> bit) & mask;
239
240 /* Convert register value to pinconf value */
241 switch (param) {
242 case PIN_CONFIG_BIAS_DISABLE:
243 arg = arg == MSM_NO_PULL;
244 break;
245 case PIN_CONFIG_BIAS_PULL_DOWN:
246 arg = arg == MSM_PULL_DOWN;
247 break;
Andy Grossb831a152014-06-17 23:49:11 -0500248 case PIN_CONFIG_BIAS_BUS_HOLD:
249 arg = arg == MSM_KEEPER;
250 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800251 case PIN_CONFIG_BIAS_PULL_UP:
252 arg = arg == MSM_PULL_UP;
253 break;
254 case PIN_CONFIG_DRIVE_STRENGTH:
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800255 arg = msm_regval_to_drive(arg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800256 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800257 case PIN_CONFIG_OUTPUT:
258 /* Pin is not output */
259 if (!arg)
260 return -EINVAL;
261
Kyle Yan6c2752f2017-09-27 16:29:45 -0700262 val = readl(pctrl->regs + g->io_reg);
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800263 arg = !!(val & BIT(g->in_bit));
264 break;
Stanimir Varbanov407f5e32015-03-04 12:41:57 +0200265 case PIN_CONFIG_INPUT_ENABLE:
266 /* Pin is output */
267 if (arg)
268 return -EINVAL;
269 arg = 1;
270 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800271 default:
Stanimir Varbanov38d756a2015-03-04 12:41:56 +0200272 return -ENOTSUPP;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800273 }
274
275 *config = pinconf_to_config_packed(param, arg);
276
277 return 0;
278}
279
280static int msm_config_group_set(struct pinctrl_dev *pctldev,
281 unsigned group,
282 unsigned long *configs,
283 unsigned num_configs)
284{
285 const struct msm_pingroup *g;
286 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
287 unsigned long flags;
288 unsigned param;
289 unsigned mask;
290 unsigned arg;
291 unsigned bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800292 int ret;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700293 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800294 int i;
295
296 g = &pctrl->soc->groups[group];
297
298 for (i = 0; i < num_configs; i++) {
299 param = pinconf_to_config_param(configs[i]);
300 arg = pinconf_to_config_argument(configs[i]);
301
Stephen Boyd051a58b2014-03-06 22:44:46 -0800302 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800303 if (ret < 0)
304 return ret;
305
306 /* Convert pinconf values to register values */
307 switch (param) {
308 case PIN_CONFIG_BIAS_DISABLE:
309 arg = MSM_NO_PULL;
310 break;
311 case PIN_CONFIG_BIAS_PULL_DOWN:
312 arg = MSM_PULL_DOWN;
313 break;
Andy Grossb831a152014-06-17 23:49:11 -0500314 case PIN_CONFIG_BIAS_BUS_HOLD:
315 arg = MSM_KEEPER;
316 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800317 case PIN_CONFIG_BIAS_PULL_UP:
318 arg = MSM_PULL_UP;
319 break;
320 case PIN_CONFIG_DRIVE_STRENGTH:
321 /* Check for invalid values */
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800322 if (arg > 16 || arg < 2 || (arg % 2) != 0)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800323 arg = -1;
324 else
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800325 arg = (arg / 2) - 1;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800326 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800327 case PIN_CONFIG_OUTPUT:
328 /* set output value */
329 spin_lock_irqsave(&pctrl->lock, flags);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700330 val = readl(pctrl->regs + g->io_reg);
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800331 if (arg)
332 val |= BIT(g->out_bit);
333 else
334 val &= ~BIT(g->out_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700335 writel(val, pctrl->regs + g->io_reg);
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800336 spin_unlock_irqrestore(&pctrl->lock, flags);
337
338 /* enable output */
339 arg = 1;
340 break;
Stanimir Varbanov407f5e32015-03-04 12:41:57 +0200341 case PIN_CONFIG_INPUT_ENABLE:
342 /* disable output */
343 arg = 0;
344 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800345 default:
346 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
347 param);
348 return -EINVAL;
349 }
350
351 /* Range-check user-supplied value */
352 if (arg & ~mask) {
353 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
354 return -EINVAL;
355 }
356
357 spin_lock_irqsave(&pctrl->lock, flags);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700358 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800359 val &= ~(mask << bit);
360 val |= arg << bit;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700361 writel(val, pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800362 spin_unlock_irqrestore(&pctrl->lock, flags);
363 }
364
365 return 0;
366}
367
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800368static const struct pinconf_ops msm_pinconf_ops = {
Stanimir Varbanov38d756a2015-03-04 12:41:56 +0200369 .is_generic = true,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800370 .pin_config_group_get = msm_config_group_get,
371 .pin_config_group_set = msm_config_group_set,
372};
373
374static struct pinctrl_desc msm_pinctrl_desc = {
375 .pctlops = &msm_pinctrl_ops,
376 .pmxops = &msm_pinmux_ops,
377 .confops = &msm_pinconf_ops,
378 .owner = THIS_MODULE,
379};
380
381static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
382{
383 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100384 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800385 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700386 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800387
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800388 g = &pctrl->soc->groups[offset];
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800389
390 spin_lock_irqsave(&pctrl->lock, flags);
391
Kyle Yan6c2752f2017-09-27 16:29:45 -0700392 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800393 val &= ~BIT(g->oe_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700394 writel(val, pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800395
396 spin_unlock_irqrestore(&pctrl->lock, flags);
397
398 return 0;
399}
400
401static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
402{
403 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100404 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800405 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700406 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800407
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800408 g = &pctrl->soc->groups[offset];
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800409
410 spin_lock_irqsave(&pctrl->lock, flags);
411
Kyle Yan6c2752f2017-09-27 16:29:45 -0700412 val = readl(pctrl->regs + g->io_reg);
Axel Line476e772013-12-13 21:35:55 +0800413 if (value)
414 val |= BIT(g->out_bit);
415 else
416 val &= ~BIT(g->out_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700417 writel(val, pctrl->regs + g->io_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800418
Kyle Yan6c2752f2017-09-27 16:29:45 -0700419 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800420 val |= BIT(g->oe_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700421 writel(val, pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800422
423 spin_unlock_irqrestore(&pctrl->lock, flags);
424
425 return 0;
426}
427
428static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
429{
430 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100431 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700432 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800433
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800434 g = &pctrl->soc->groups[offset];
435
Kyle Yan6c2752f2017-09-27 16:29:45 -0700436 val = readl(pctrl->regs + g->io_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800437 return !!(val & BIT(g->in_bit));
438}
439
440static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
441{
442 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100443 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800444 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700445 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800446
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800447 g = &pctrl->soc->groups[offset];
448
449 spin_lock_irqsave(&pctrl->lock, flags);
450
Kyle Yan6c2752f2017-09-27 16:29:45 -0700451 val = readl(pctrl->regs + g->io_reg);
Axel Line476e772013-12-13 21:35:55 +0800452 if (value)
453 val |= BIT(g->out_bit);
454 else
455 val &= ~BIT(g->out_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700456 writel(val, pctrl->regs + g->io_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800457
458 spin_unlock_irqrestore(&pctrl->lock, flags);
459}
460
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800461#ifdef CONFIG_DEBUG_FS
462#include <linux/seq_file.h>
463
464static void msm_gpio_dbg_show_one(struct seq_file *s,
465 struct pinctrl_dev *pctldev,
466 struct gpio_chip *chip,
467 unsigned offset,
468 unsigned gpio)
469{
470 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100471 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800472 unsigned func;
473 int is_out;
474 int drive;
475 int pull;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700476 u32 ctl_reg;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800477
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800478 static const char * const pulls[] = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800479 "no pull",
480 "pull down",
481 "keeper",
482 "pull up"
483 };
484
485 g = &pctrl->soc->groups[offset];
Kyle Yan6c2752f2017-09-27 16:29:45 -0700486 ctl_reg = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800487
488 is_out = !!(ctl_reg & BIT(g->oe_bit));
489 func = (ctl_reg >> g->mux_bit) & 7;
490 drive = (ctl_reg >> g->drv_bit) & 7;
491 pull = (ctl_reg >> g->pull_bit) & 3;
492
493 seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800494 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800495 seq_printf(s, " %s", pulls[pull]);
496}
497
498static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
499{
500 unsigned gpio = chip->base;
501 unsigned i;
502
503 for (i = 0; i < chip->ngpio; i++, gpio++) {
504 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800505 seq_puts(s, "\n");
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800506 }
507}
508
509#else
510#define msm_gpio_dbg_show NULL
511#endif
512
513static struct gpio_chip msm_gpio_template = {
514 .direction_input = msm_gpio_direction_input,
515 .direction_output = msm_gpio_direction_output,
516 .get = msm_gpio_get,
517 .set = msm_gpio_set,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200518 .request = gpiochip_generic_request,
519 .free = gpiochip_generic_free,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800520 .dbg_show = msm_gpio_dbg_show,
521};
522
523/* For dual-edge interrupts in software, since some hardware has no
524 * such support:
525 *
526 * At appropriate moments, this function may be called to flip the polarity
527 * settings of both-edge irq lines to try and catch the next edge.
528 *
529 * The attempt is considered successful if:
530 * - the status bit goes high, indicating that an edge was caught, or
531 * - the input value of the gpio doesn't change during the attempt.
532 * If the value changes twice during the process, that would cause the first
533 * test to fail but would force the second, as two opposite
534 * transitions would cause a detection no matter the polarity setting.
535 *
536 * The do-loop tries to sledge-hammer closed the timing hole between
537 * the initial value-read and the polarity-write - if the line value changes
538 * during that window, an interrupt is lost, the new polarity setting is
539 * incorrect, and the first success test will fail, causing a retry.
540 *
541 * Algorithm comes from Google's msmgpio driver.
542 */
543static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
544 const struct msm_pingroup *g,
Kyle Yan6c2752f2017-09-27 16:29:45 -0700545 struct irq_data *d)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800546{
547 int loop_limit = 100;
548 unsigned val, val2, intstat;
549 unsigned pol;
550
551 do {
Kyle Yan6c2752f2017-09-27 16:29:45 -0700552 val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800553
Kyle Yan6c2752f2017-09-27 16:29:45 -0700554 pol = readl(pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800555 pol ^= BIT(g->intr_polarity_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700556 writel(pol, pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800557
Kyle Yan6c2752f2017-09-27 16:29:45 -0700558 val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
559 intstat = readl(pctrl->regs + g->intr_status_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800560 if (intstat || (val == val2))
561 return;
562 } while (loop_limit-- > 0);
563 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
564 val, val2);
565}
566
567static void msm_gpio_irq_mask(struct irq_data *d)
568{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700569 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100570 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800571 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800572 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700573 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800574
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800575 g = &pctrl->soc->groups[d->hwirq];
576
577 spin_lock_irqsave(&pctrl->lock, flags);
578
Kyle Yan6c2752f2017-09-27 16:29:45 -0700579 val = readl(pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800580 val &= ~BIT(g->intr_enable_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700581 writel(val, pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800582
583 clear_bit(d->hwirq, pctrl->enabled_irqs);
584
585 spin_unlock_irqrestore(&pctrl->lock, flags);
586}
587
Srinivas Ramana69521392017-11-14 11:36:23 +0530588static void msm_gpio_irq_enable(struct irq_data *d)
589{
590 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
591 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
592 const struct msm_pingroup *g;
593 unsigned long flags;
594 u32 val;
595
596 g = &pctrl->soc->groups[d->hwirq];
597
598 spin_lock_irqsave(&pctrl->lock, flags);
599 /* clear the interrupt status bit before unmask to avoid
600 * any erraneous interrupts that would have got latched
601 * when the intterupt is not in use.
602 */
603 val = readl(pctrl->regs + g->intr_status_reg);
604 val &= ~BIT(g->intr_status_bit);
605 writel(val, pctrl->regs + g->intr_status_reg);
606
607 val = readl(pctrl->regs + g->intr_cfg_reg);
608 val |= BIT(g->intr_enable_bit);
609 writel(val, pctrl->regs + g->intr_cfg_reg);
610
611 set_bit(d->hwirq, pctrl->enabled_irqs);
612
613 spin_unlock_irqrestore(&pctrl->lock, flags);
614}
615
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800616static void msm_gpio_irq_unmask(struct irq_data *d)
617{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700618 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100619 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800620 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800621 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700622 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800623
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800624 g = &pctrl->soc->groups[d->hwirq];
625
626 spin_lock_irqsave(&pctrl->lock, flags);
627
Kyle Yan6c2752f2017-09-27 16:29:45 -0700628 val = readl(pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800629 val |= BIT(g->intr_enable_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700630 writel(val, pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800631
632 set_bit(d->hwirq, pctrl->enabled_irqs);
633
634 spin_unlock_irqrestore(&pctrl->lock, flags);
635}
636
637static void msm_gpio_irq_ack(struct irq_data *d)
638{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700639 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100640 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800641 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800642 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700643 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800644
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800645 g = &pctrl->soc->groups[d->hwirq];
646
647 spin_lock_irqsave(&pctrl->lock, flags);
648
Kyle Yan6c2752f2017-09-27 16:29:45 -0700649 val = readl(pctrl->regs + g->intr_status_reg);
Bjorn Andersson48f15e92014-03-31 14:49:54 -0700650 if (g->intr_ack_high)
651 val |= BIT(g->intr_status_bit);
652 else
653 val &= ~BIT(g->intr_status_bit);
Kyle Yan6c2752f2017-09-27 16:29:45 -0700654 writel(val, pctrl->regs + g->intr_status_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800655
656 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
Kyle Yan6c2752f2017-09-27 16:29:45 -0700657 msm_gpio_update_dual_edge_pos(pctrl, g, d);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800658
659 spin_unlock_irqrestore(&pctrl->lock, flags);
660}
661
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800662static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
663{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700664 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100665 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800666 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800667 unsigned long flags;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700668 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800669
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800670 g = &pctrl->soc->groups[d->hwirq];
Kyle Yan6c2752f2017-09-27 16:29:45 -0700671
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800672 spin_lock_irqsave(&pctrl->lock, flags);
673
674 /*
675 * For hw without possibility of detecting both edges
676 */
677 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
678 set_bit(d->hwirq, pctrl->dual_edge_irqs);
679 else
680 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
681
682 /* Route interrupts to application cpu */
Kyle Yan6c2752f2017-09-27 16:29:45 -0700683 val = readl(pctrl->regs + g->intr_target_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800684 val &= ~(7 << g->intr_target_bit);
Georgi Djakovf712c552014-09-03 19:28:16 +0300685 val |= g->intr_target_kpss_val << g->intr_target_bit;
Kyle Yan6c2752f2017-09-27 16:29:45 -0700686 writel(val, pctrl->regs + g->intr_target_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800687
688 /* Update configuration for gpio.
689 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
690 * internal circuitry of TLMM, toggling the RAW_STATUS
691 * could cause the INTR_STATUS to be set for EDGE interrupts.
692 */
Kyle Yan6c2752f2017-09-27 16:29:45 -0700693 val = readl(pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800694 val |= BIT(g->intr_raw_status_bit);
695 if (g->intr_detection_width == 2) {
696 val &= ~(3 << g->intr_detection_bit);
697 val &= ~(1 << g->intr_polarity_bit);
698 switch (type) {
699 case IRQ_TYPE_EDGE_RISING:
700 val |= 1 << g->intr_detection_bit;
701 val |= BIT(g->intr_polarity_bit);
702 break;
703 case IRQ_TYPE_EDGE_FALLING:
704 val |= 2 << g->intr_detection_bit;
705 val |= BIT(g->intr_polarity_bit);
706 break;
707 case IRQ_TYPE_EDGE_BOTH:
708 val |= 3 << g->intr_detection_bit;
709 val |= BIT(g->intr_polarity_bit);
710 break;
711 case IRQ_TYPE_LEVEL_LOW:
712 break;
713 case IRQ_TYPE_LEVEL_HIGH:
714 val |= BIT(g->intr_polarity_bit);
715 break;
716 }
717 } else if (g->intr_detection_width == 1) {
718 val &= ~(1 << g->intr_detection_bit);
719 val &= ~(1 << g->intr_polarity_bit);
720 switch (type) {
721 case IRQ_TYPE_EDGE_RISING:
722 val |= BIT(g->intr_detection_bit);
723 val |= BIT(g->intr_polarity_bit);
724 break;
725 case IRQ_TYPE_EDGE_FALLING:
726 val |= BIT(g->intr_detection_bit);
727 break;
728 case IRQ_TYPE_EDGE_BOTH:
729 val |= BIT(g->intr_detection_bit);
Bjorn Andersson48f15e92014-03-31 14:49:54 -0700730 val |= BIT(g->intr_polarity_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800731 break;
732 case IRQ_TYPE_LEVEL_LOW:
733 break;
734 case IRQ_TYPE_LEVEL_HIGH:
735 val |= BIT(g->intr_polarity_bit);
736 break;
737 }
738 } else {
739 BUG();
740 }
Kyle Yan6c2752f2017-09-27 16:29:45 -0700741 writel(val, pctrl->regs + g->intr_cfg_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800742
743 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
Kyle Yan6c2752f2017-09-27 16:29:45 -0700744 msm_gpio_update_dual_edge_pos(pctrl, g, d);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800745
746 spin_unlock_irqrestore(&pctrl->lock, flags);
747
748 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner34c0ad82015-06-23 15:52:51 +0200749 irq_set_handler_locked(d, handle_level_irq);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800750 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner34c0ad82015-06-23 15:52:51 +0200751 irq_set_handler_locked(d, handle_edge_irq);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800752
753 return 0;
754}
755
756static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
757{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700758 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100759 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800760 unsigned long flags;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800761
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800762 spin_lock_irqsave(&pctrl->lock, flags);
763
Josh Cartwright6aced332014-03-05 13:33:08 -0600764 irq_set_irq_wake(pctrl->irq, on);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800765
766 spin_unlock_irqrestore(&pctrl->lock, flags);
767
768 return 0;
769}
770
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800771static struct irq_chip msm_gpio_irq_chip = {
772 .name = "msmgpio",
Srinivas Ramana69521392017-11-14 11:36:23 +0530773 .irq_enable = msm_gpio_irq_enable,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800774 .irq_mask = msm_gpio_irq_mask,
775 .irq_unmask = msm_gpio_irq_unmask,
776 .irq_ack = msm_gpio_irq_ack,
777 .irq_set_type = msm_gpio_irq_set_type,
778 .irq_set_wake = msm_gpio_irq_set_wake,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800779};
780
Archana Sathyakumar892c01c2017-10-31 13:47:20 -0600781static struct irq_chip msm_dirconn_irq_chip;
782
783static void msm_gpio_dirconn_handler(struct irq_desc *desc)
784{
785 struct irq_data *irqd = irq_desc_get_handler_data(desc);
786 struct irq_chip *chip = irq_desc_get_chip(desc);
787
788 chained_irq_enter(chip, desc);
789 generic_handle_irq(irqd->irq);
790 chained_irq_exit(chip, desc);
791}
792
793static void setup_pdc_gpio(struct irq_domain *domain,
794 unsigned int parent_irq, unsigned int gpio)
795{
796 int irq;
797
798 if (gpio != 0) {
799 irq = irq_find_mapping(domain, gpio);
800 irq_set_parent(irq, parent_irq);
801 irq_set_chip(irq, &msm_dirconn_irq_chip);
802 irq_set_handler_data(parent_irq, irq_get_irq_data(irq));
803 }
804
805 __irq_set_handler(parent_irq, msm_gpio_dirconn_handler, false, NULL);
806}
807
808static void request_dc_interrupt(struct irq_domain *domain,
809 struct irq_domain *parent, irq_hw_number_t hwirq,
810 unsigned int gpio)
811{
812 struct irq_fwspec fwspec;
813 unsigned int parent_irq;
814
815 fwspec.fwnode = parent->fwnode;
816 fwspec.param[0] = 0; /* SPI */
817 fwspec.param[1] = hwirq;
818 fwspec.param[2] = IRQ_TYPE_NONE;
819 fwspec.param_count = 3;
820
821 parent_irq = irq_create_fwspec_mapping(&fwspec);
822
823 setup_pdc_gpio(domain, parent_irq, gpio);
824}
825
826/**
827 * gpio_muxed_to_pdc: Mux the GPIO to a PDC IRQ
828 *
829 * @pdc_domain: the PDC's domain
830 * @d: the GPIO's IRQ data
831 *
832 * Find a free PDC port for the GPIO and map the GPIO's mux information to the
833 * PDC registers; so the GPIO can be used a wakeup source.
834 */
835static void gpio_muxed_to_pdc(struct irq_domain *pdc_domain, struct irq_data *d)
836{
837 int i, j;
838 unsigned int mux;
839 struct irq_desc *desc = irq_data_to_desc(d);
840 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
841 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
842 unsigned int gpio = d->hwirq;
843 struct msm_pinctrl *pctrl;
844 unsigned int irq;
845
846 if (!gc || !parent_data)
847 return;
848
849 pctrl = gpiochip_get_data(gc);
850
851 for (i = 0; i < pctrl->soc->n_gpio_mux_in; i++) {
852 if (gpio != pctrl->soc->gpio_mux_in[i].gpio)
853 continue;
854 mux = pctrl->soc->gpio_mux_in[i].mux;
855 for (j = 0; j < pctrl->soc->n_pdc_mux_out; j++) {
856 struct msm_pdc_mux_output *pdc_out =
857 &pctrl->soc->pdc_mux_out[j];
858
859 if (pdc_out->mux == mux)
860 break;
861 if (pdc_out->mux)
862 continue;
863 pdc_out->mux = gpio;
864 irq = irq_find_mapping(pdc_domain, pdc_out->hwirq + 32);
865 /* setup the IRQ parent for the GPIO */
866 setup_pdc_gpio(pctrl->chip.irqdomain, irq, gpio);
867 /* program pdc select grp register */
868 writel_relaxed((mux & 0x3F), pctrl->pdc_regs +
869 (0x14 * j));
870 break;
871 }
872 /* We have no more PDC port available */
873 WARN_ON(j == pctrl->soc->n_pdc_mux_out);
874 }
875}
876
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600877static bool is_gpio_dual_edge(struct irq_data *d, irq_hw_number_t *dir_conn_irq)
878{
879 struct irq_desc *desc = irq_data_to_desc(d);
880 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
881 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
882 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
883 int i;
884
885 if (!parent_data)
886 return false;
887
888 for (i = 0; i < pctrl->soc->n_dir_conns; i++) {
889 const struct msm_dir_conn *dir_conn = &pctrl->soc->dir_conn[i];
890
891 if (dir_conn->gpio == d->hwirq && (dir_conn->hwirq + 32)
892 != parent_data->hwirq) {
893 *dir_conn_irq = dir_conn->hwirq + 32;
894 return true;
895 }
896 }
Archana Sathyakumar892c01c2017-10-31 13:47:20 -0600897
898 for (i = 0; i < pctrl->soc->n_pdc_mux_out; i++) {
899 struct msm_pdc_mux_output *dir_conn =
900 &pctrl->soc->pdc_mux_out[i];
901
902 if (dir_conn->mux == d->hwirq && (dir_conn->hwirq + 32)
903 != parent_data->hwirq) {
904 *dir_conn_irq = dir_conn->hwirq + 32;
905 return true;
906 }
907 }
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600908 return false;
909}
910
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600911static void msm_dirconn_irq_mask(struct irq_data *d)
912{
913 struct irq_desc *desc = irq_data_to_desc(d);
914 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600915 irq_hw_number_t dir_conn_irq = 0;
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600916
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600917 if (!parent_data)
918 return;
919
920 if (is_gpio_dual_edge(d, &dir_conn_irq)) {
921 struct irq_data *dir_conn_data =
922 irq_get_irq_data(irq_find_mapping(parent_data->domain,
923 dir_conn_irq));
924
Archana Sathyakumar892c01c2017-10-31 13:47:20 -0600925 if (!dir_conn_data)
926 return;
927 if (dir_conn_data->chip->irq_mask)
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600928 dir_conn_data->chip->irq_mask(dir_conn_data);
929 }
Archana Sathyakumar892c01c2017-10-31 13:47:20 -0600930
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600931 if (parent_data->chip->irq_mask)
932 parent_data->chip->irq_mask(parent_data);
933}
934
935static void msm_dirconn_irq_unmask(struct irq_data *d)
936{
937 struct irq_desc *desc = irq_data_to_desc(d);
938 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600939 irq_hw_number_t dir_conn_irq = 0;
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600940
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600941 if (!parent_data)
942 return;
943
944 if (is_gpio_dual_edge(d, &dir_conn_irq)) {
945 struct irq_data *dir_conn_data =
946 irq_get_irq_data(irq_find_mapping(parent_data->domain,
947 dir_conn_irq));
948
Archana Sathyakumar892c01c2017-10-31 13:47:20 -0600949 if (!dir_conn_data)
950 return;
951 if (dir_conn_data->chip->irq_unmask)
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600952 dir_conn_data->chip->irq_unmask(dir_conn_data);
953 }
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600954 if (parent_data->chip->irq_unmask)
955 parent_data->chip->irq_unmask(parent_data);
956}
957
958static void msm_dirconn_irq_ack(struct irq_data *d)
959{
960 struct irq_desc *desc = irq_data_to_desc(d);
961 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
962
Maulik Shah9180da22017-10-31 15:23:17 +0530963 if (!parent_data)
964 return;
965
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600966 if (parent_data->chip->irq_ack)
967 parent_data->chip->irq_ack(parent_data);
968}
969
970static void msm_dirconn_irq_eoi(struct irq_data *d)
971{
972 struct irq_desc *desc = irq_data_to_desc(d);
973 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
974
Maulik Shah9180da22017-10-31 15:23:17 +0530975 if (!parent_data)
976 return;
977
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600978 if (parent_data->chip->irq_eoi)
979 parent_data->chip->irq_eoi(parent_data);
980}
981
982static int msm_dirconn_irq_set_affinity(struct irq_data *d,
983 const struct cpumask *maskval, bool force)
984{
985 struct irq_desc *desc = irq_data_to_desc(d);
986 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
987
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -0600988 if (!parent_data)
989 return 0;
990
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600991 if (parent_data->chip->irq_set_affinity)
992 return parent_data->chip->irq_set_affinity(parent_data,
993 maskval, force);
994 return 0;
995}
996
997static int msm_dirconn_irq_set_vcpu_affinity(struct irq_data *d,
998 void *vcpu_info)
999{
1000 struct irq_desc *desc = irq_data_to_desc(d);
1001 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
1002
Maulik Shah9180da22017-10-31 15:23:17 +05301003 if (!parent_data)
1004 return 0;
1005
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001006 if (parent_data->chip->irq_set_vcpu_affinity)
1007 return parent_data->chip->irq_set_vcpu_affinity(parent_data,
1008 vcpu_info);
1009 return 0;
1010}
1011
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -06001012static void msm_dirconn_cfg_reg(struct irq_data *d, u32 offset)
1013{
1014 u32 val = 0;
1015 const struct msm_pingroup *g;
1016 unsigned long flags;
1017 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1018 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1019
1020 spin_lock_irqsave(&pctrl->lock, flags);
1021 g = &pctrl->soc->groups[d->hwirq];
1022
1023 val = readl_relaxed(pctrl->regs + g->dir_conn_reg + (offset * 4));
1024 val = (d->hwirq) & 0xFF;
1025
1026 writel_relaxed(val, pctrl->regs + g->dir_conn_reg + (offset * 4));
1027
1028 //write the dir_conn_en bit
1029 val = readl_relaxed(pctrl->regs + g->intr_cfg_reg);
1030 val |= BIT(g->dir_conn_en_bit);
1031 writel_relaxed(val, pctrl->regs + g->intr_cfg_reg);
1032 spin_unlock_irqrestore(&pctrl->lock, flags);
1033}
1034
1035static void msm_dirconn_uncfg_reg(struct irq_data *d, u32 offset)
1036{
1037 const struct msm_pingroup *g;
1038 unsigned long flags;
1039 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1040 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1041
1042 spin_lock_irqsave(&pctrl->lock, flags);
1043 g = &pctrl->soc->groups[d->hwirq];
1044
1045 writel_relaxed(BIT(8), pctrl->regs + g->dir_conn_reg + (offset * 4));
1046 spin_unlock_irqrestore(&pctrl->lock, flags);
1047}
1048
1049static int select_dir_conn_mux(struct irq_data *d, irq_hw_number_t *irq)
1050{
1051 struct msm_dir_conn *dc = NULL;
1052 struct irq_desc *desc = irq_data_to_desc(d);
1053 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
1054 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1055 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1056 int i;
1057
1058 if (!parent_data)
1059 return -EINVAL;
1060
1061 for (i = 0; i < pctrl->soc->n_dir_conns; i++) {
1062 struct msm_dir_conn *dir_conn =
1063 (struct msm_dir_conn *)&pctrl->soc->dir_conn[i];
1064
1065 /* Check if there is already mux assigned for this gpio */
1066 if (dir_conn->gpio == d->hwirq && (dir_conn->hwirq + 32) !=
1067 parent_data->hwirq) {
1068 *irq = dir_conn->hwirq + 32;
1069 return pctrl->soc->dir_conn_irq_base - dir_conn->hwirq;
1070 }
1071
1072 if (dir_conn->gpio)
1073 continue;
1074
1075 /* Use the first unused direct connect available */
1076 dc = dir_conn;
1077 break;
1078 }
1079
1080 if (dc) {
1081 *irq = dc->hwirq + 32;
1082 dc->gpio = (u32)d->hwirq;
1083 return pctrl->soc->dir_conn_irq_base - (u32)dc->hwirq;
1084 }
1085
1086 pr_err("%s: No direct connects available for interrupt %lu\n",
1087 __func__, d->hwirq);
1088 return -EINVAL;
1089}
1090
1091static void add_dirconn_tlmm(struct irq_data *d, irq_hw_number_t irq)
1092{
1093 struct irq_desc *desc = irq_data_to_desc(d);
1094 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
1095 struct irq_data *dir_conn_data = NULL;
1096 int offset = 0;
1097 unsigned int virt = 0;
1098
1099 offset = select_dir_conn_mux(d, &irq);
1100 if (offset < 0 || !parent_data)
1101 return;
1102
1103 virt = irq_find_mapping(parent_data->domain, irq);
1104 msm_dirconn_cfg_reg(d, offset);
1105 irq_set_handler_data(virt, d);
1106 desc = irq_to_desc(virt);
1107 if (!desc)
1108 return;
1109
1110 dir_conn_data = &(desc->irq_data);
1111
1112 if (dir_conn_data) {
1113 if (dir_conn_data->chip && dir_conn_data->chip->irq_set_type)
1114 dir_conn_data->chip->irq_set_type(dir_conn_data,
1115 IRQ_TYPE_EDGE_RISING);
1116 if (dir_conn_data->chip && dir_conn_data->chip->irq_unmask)
1117 dir_conn_data->chip->irq_unmask(dir_conn_data);
1118 }
1119}
1120
1121static void remove_dirconn_tlmm(struct irq_data *d, irq_hw_number_t irq)
1122{
1123 struct irq_desc *desc = irq_data_to_desc(d);
1124 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
1125 struct irq_data *dir_conn_data = NULL;
1126 int offset = 0;
1127 unsigned int virt = 0;
1128
1129 virt = irq_find_mapping(parent_data->domain, irq);
1130 msm_dirconn_uncfg_reg(d, offset);
1131 irq_set_handler_data(virt, NULL);
1132 desc = irq_to_desc(virt);
1133 if (!desc)
1134 return;
1135
1136 dir_conn_data = &(desc->irq_data);
1137
1138 if (dir_conn_data) {
1139 if (dir_conn_data->chip && dir_conn_data->chip->irq_mask)
1140 dir_conn_data->chip->irq_mask(dir_conn_data);
1141 }
1142}
1143
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001144static int msm_dirconn_irq_set_type(struct irq_data *d, unsigned int type)
1145{
1146 struct irq_desc *desc = irq_data_to_desc(d);
1147 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
Archana Sathyakumar8ff3ba62017-10-06 11:58:46 -06001148 irq_hw_number_t irq = 0;
1149
1150 if (!parent_data)
1151 return 0;
1152
1153 if (type == IRQ_TYPE_EDGE_BOTH) {
1154 add_dirconn_tlmm(d, irq);
1155 } else {
1156 if (is_gpio_dual_edge(d, &irq))
1157 remove_dirconn_tlmm(d, irq);
1158 }
1159
1160 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1161 irq_set_handler_locked(d, handle_level_irq);
1162 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1163 irq_set_handler_locked(d, handle_edge_irq);
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001164
1165 if (parent_data->chip->irq_set_type)
1166 return parent_data->chip->irq_set_type(parent_data, type);
1167
1168 return 0;
1169}
1170
1171static struct irq_chip msm_dirconn_irq_chip = {
1172 .name = "msmgpio-dc",
1173 .irq_mask = msm_dirconn_irq_mask,
1174 .irq_unmask = msm_dirconn_irq_unmask,
1175 .irq_eoi = msm_dirconn_irq_eoi,
1176 .irq_ack = msm_dirconn_irq_ack,
1177 .irq_set_type = msm_dirconn_irq_set_type,
1178 .irq_set_affinity = msm_dirconn_irq_set_affinity,
1179 .irq_set_vcpu_affinity = msm_dirconn_irq_set_vcpu_affinity,
1180 .flags = IRQCHIP_SKIP_SET_WAKE
1181 | IRQCHIP_MASK_ON_SUSPEND
1182 | IRQCHIP_SET_TYPE_MASKED,
1183};
1184
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001185static void msm_gpio_irq_handler(struct irq_desc *desc)
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001186{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -07001187 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001188 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +01001189 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Jiang Liu5663bb22015-06-04 12:13:16 +08001190 struct irq_chip *chip = irq_desc_get_chip(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001191 int irq_pin;
1192 int handled = 0;
Kyle Yan6c2752f2017-09-27 16:29:45 -07001193 u32 val;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001194 int i;
1195
1196 chained_irq_enter(chip, desc);
1197
1198 /*
Bjorn Andersson1f2b2392013-12-14 23:01:51 -08001199 * Each pin has it's own IRQ status register, so use
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001200 * enabled_irq bitmap to limit the number of reads.
1201 */
1202 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1203 g = &pctrl->soc->groups[i];
Kyle Yan6c2752f2017-09-27 16:29:45 -07001204 val = readl(pctrl->regs + g->intr_status_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001205 if (val & BIT(g->intr_status_bit)) {
Linus Walleijcdcb0ab2014-04-29 11:00:40 -07001206 irq_pin = irq_find_mapping(gc->irqdomain, i);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001207 generic_handle_irq(irq_pin);
1208 handled++;
1209 }
1210 }
1211
Bjorn Andersson1f2b2392013-12-14 23:01:51 -08001212 /* No interrupts were flagged */
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001213 if (handled == 0)
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001214 handle_bad_irq(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001215
1216 chained_irq_exit(chip, desc);
1217}
1218
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001219static void msm_gpio_setup_dir_connects(struct msm_pinctrl *pctrl)
1220{
1221 struct device_node *parent_node;
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06001222 struct irq_domain *pdc_domain;
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001223 unsigned int i;
1224
1225 parent_node = of_irq_find_parent(pctrl->dev->of_node);
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001226 if (!parent_node)
1227 return;
1228
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06001229 pdc_domain = irq_find_host(parent_node);
1230 if (!pdc_domain)
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001231 return;
1232
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001233 for (i = 0; i < pctrl->soc->n_dir_conns; i++) {
1234 const struct msm_dir_conn *dirconn = &pctrl->soc->dir_conn[i];
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001235
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06001236 request_dc_interrupt(pctrl->chip.irqdomain, pdc_domain,
1237 dirconn->hwirq, dirconn->gpio);
1238 }
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001239
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06001240 for (i = 0; i < pctrl->soc->n_pdc_mux_out; i++) {
1241 struct msm_pdc_mux_output *pdc_out =
1242 &pctrl->soc->pdc_mux_out[i];
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001243
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06001244 request_dc_interrupt(pctrl->chip.irqdomain, pdc_domain,
1245 pdc_out->hwirq, 0);
1246 }
1247
1248 /*
1249 * Statically choose the GPIOs for mapping to PDC. Dynamic mux mapping
1250 * is very difficult.
1251 */
1252 for (i = 0; i < pctrl->soc->n_pdc_mux_out; i++) {
1253 unsigned int irq;
1254 struct irq_data *d;
1255 struct msm_gpio_mux_input *gpio_in =
1256 &pctrl->soc->gpio_mux_in[i];
1257 if (!gpio_in->init)
1258 continue;
1259
1260 irq = irq_find_mapping(pctrl->chip.irqdomain, gpio_in->gpio);
1261 d = irq_get_irq_data(irq);
1262 if (!d)
1263 continue;
1264
1265 gpio_muxed_to_pdc(pdc_domain, d);
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001266 }
1267}
1268
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001269static int msm_gpio_init(struct msm_pinctrl *pctrl)
1270{
1271 struct gpio_chip *chip;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001272 int ret;
Stephen Boyddcd278b2014-03-06 22:44:41 -08001273 unsigned ngpio = pctrl->soc->ngpios;
1274
1275 if (WARN_ON(ngpio > MAX_NR_GPIO))
1276 return -EINVAL;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001277
1278 chip = &pctrl->chip;
1279 chip->base = 0;
Stephen Boyddcd278b2014-03-06 22:44:41 -08001280 chip->ngpio = ngpio;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001281 chip->label = dev_name(pctrl->dev);
Linus Walleij58383c72015-11-04 09:56:26 +01001282 chip->parent = pctrl->dev;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001283 chip->owner = THIS_MODULE;
1284 chip->of_node = pctrl->dev->of_node;
1285
Linus Walleijfded3f42015-12-08 09:49:18 +01001286 ret = gpiochip_add_data(&pctrl->chip, pctrl);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001287 if (ret) {
1288 dev_err(pctrl->dev, "Failed register gpiochip\n");
1289 return ret;
1290 }
1291
1292 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
1293 if (ret) {
1294 dev_err(pctrl->dev, "Failed to add pin range\n");
Pramod Guravc6e927a2014-08-29 13:41:48 +05301295 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001296 return ret;
1297 }
1298
Linus Walleijcdcb0ab2014-04-29 11:00:40 -07001299 ret = gpiochip_irqchip_add(chip,
1300 &msm_gpio_irq_chip,
1301 0,
Archana Sathyakumar558f2622017-07-26 07:37:51 -06001302 handle_fasteoi_irq,
Linus Walleijcdcb0ab2014-04-29 11:00:40 -07001303 IRQ_TYPE_NONE);
1304 if (ret) {
1305 dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
Pramod Guravc6e927a2014-08-29 13:41:48 +05301306 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001307 return -ENOSYS;
1308 }
1309
Linus Walleijcdcb0ab2014-04-29 11:00:40 -07001310 gpiochip_set_chained_irqchip(chip, &msm_gpio_irq_chip, pctrl->irq,
1311 msm_gpio_irq_handler);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001312
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -06001313 msm_gpio_setup_dir_connects(pctrl);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001314 return 0;
1315}
1316
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001317static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
1318 void *data)
Pramod Gurav32745582014-08-29 20:00:59 +05301319{
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001320 struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
1321
1322 writel(0, pctrl->regs + PS_HOLD_OFFSET);
1323 mdelay(1000);
1324 return NOTIFY_DONE;
Pramod Gurav32745582014-08-29 20:00:59 +05301325}
1326
Stephen Boydad644982015-07-06 18:09:30 -07001327static struct msm_pinctrl *poweroff_pctrl;
1328
1329static void msm_ps_hold_poweroff(void)
1330{
1331 msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
1332}
1333
Pramod Gurav32745582014-08-29 20:00:59 +05301334static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1335{
Stephen Boydbcd53f82015-01-19 11:17:45 +01001336 int i;
Pramod Gurav32745582014-08-29 20:00:59 +05301337 const struct msm_function *func = pctrl->soc->functions;
1338
Stephen Boydbcd53f82015-01-19 11:17:45 +01001339 for (i = 0; i < pctrl->soc->nfunctions; i++)
Pramod Gurav32745582014-08-29 20:00:59 +05301340 if (!strcmp(func[i].name, "ps_hold")) {
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001341 pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
1342 pctrl->restart_nb.priority = 128;
1343 if (register_restart_handler(&pctrl->restart_nb))
1344 dev_err(pctrl->dev,
1345 "failed to setup restart handler.\n");
Stephen Boydad644982015-07-06 18:09:30 -07001346 poweroff_pctrl = pctrl;
1347 pm_power_off = msm_ps_hold_poweroff;
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001348 break;
Pramod Gurav32745582014-08-29 20:00:59 +05301349 }
1350}
Pramod Gurav32745582014-08-29 20:00:59 +05301351
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001352int msm_pinctrl_probe(struct platform_device *pdev,
1353 const struct msm_pinctrl_soc_data *soc_data)
1354{
1355 struct msm_pinctrl *pctrl;
1356 struct resource *res;
1357 int ret;
1358
Stephen Boyd92f54c92017-08-14 15:36:48 -07001359 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001360 if (!pctrl) {
1361 dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
1362 return -ENOMEM;
1363 }
1364 pctrl->dev = &pdev->dev;
1365 pctrl->soc = soc_data;
1366 pctrl->chip = msm_gpio_template;
1367
1368 spin_lock_init(&pctrl->lock);
1369
1370 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1371 pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
1372 if (IS_ERR(pctrl->regs))
1373 return PTR_ERR(pctrl->regs);
1374
Archana Sathyakumar892c01c2017-10-31 13:47:20 -06001375 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1376 pctrl->pdc_regs = devm_ioremap_resource(&pdev->dev, res);
1377
Pramod Gurav32745582014-08-29 20:00:59 +05301378 msm_pinctrl_setup_pm_reset(pctrl);
1379
Bjorn Anderssonf393e482013-12-14 23:01:52 -08001380 pctrl->irq = platform_get_irq(pdev, 0);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001381 if (pctrl->irq < 0) {
1382 dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
1383 return pctrl->irq;
1384 }
1385
1386 msm_pinctrl_desc.name = dev_name(&pdev->dev);
1387 msm_pinctrl_desc.pins = pctrl->soc->pins;
1388 msm_pinctrl_desc.npins = pctrl->soc->npins;
Laxman Dewanganfe0267f2016-02-24 14:44:07 +05301389 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &msm_pinctrl_desc,
1390 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001391 if (IS_ERR(pctrl->pctrl)) {
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001392 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001393 return PTR_ERR(pctrl->pctrl);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001394 }
1395
1396 ret = msm_gpio_init(pctrl);
Laxman Dewanganfe0267f2016-02-24 14:44:07 +05301397 if (ret)
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001398 return ret;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001399
1400 platform_set_drvdata(pdev, pctrl);
1401
1402 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1403
1404 return 0;
1405}
1406EXPORT_SYMBOL(msm_pinctrl_probe);
1407
1408int msm_pinctrl_remove(struct platform_device *pdev)
1409{
1410 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001411
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001412 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001413
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001414 unregister_restart_handler(&pctrl->restart_nb);
1415
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001416 return 0;
1417}
1418EXPORT_SYMBOL(msm_pinctrl_remove);
1419