blob: 9520166e896b9a5e8648edcb035ed16f16ec8dbe [file] [log] [blame]
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001/*
2 * Copyright (c) 2013, Sony Mobile Communications AB.
Prasad Sodagudid0451852016-07-01 14:12:20 +05303 * Copyright (c) 2013-2017, 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>
18#include <linux/module.h>
19#include <linux/of.h>
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -060020#include <linux/of_irq.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080021#include <linux/platform_device.h>
22#include <linux/pinctrl/machine.h>
23#include <linux/pinctrl/pinctrl.h>
24#include <linux/pinctrl/pinmux.h>
25#include <linux/pinctrl/pinconf.h>
26#include <linux/pinctrl/pinconf-generic.h>
27#include <linux/slab.h>
28#include <linux/gpio.h>
29#include <linux/interrupt.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080030#include <linux/spinlock.h>
Prasad Sodagudid0451852016-07-01 14:12:20 +053031#include <linux/syscore_ops.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;
73};
74
Prasad Sodagudid0451852016-07-01 14:12:20 +053075static struct msm_pinctrl *msm_pinctrl_data;
76
Bjorn Anderssonf365be02013-12-05 18:10:03 -080077static int msm_get_groups_count(struct pinctrl_dev *pctldev)
78{
79 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
80
81 return pctrl->soc->ngroups;
82}
83
84static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
85 unsigned group)
86{
87 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
88
89 return pctrl->soc->groups[group].name;
90}
91
92static int msm_get_group_pins(struct pinctrl_dev *pctldev,
93 unsigned group,
94 const unsigned **pins,
95 unsigned *num_pins)
96{
97 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
98
99 *pins = pctrl->soc->groups[group].pins;
100 *num_pins = pctrl->soc->groups[group].npins;
101 return 0;
102}
103
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800104static const struct pinctrl_ops msm_pinctrl_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800105 .get_groups_count = msm_get_groups_count,
106 .get_group_name = msm_get_group_name,
107 .get_group_pins = msm_get_group_pins,
108 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300109 .dt_free_map = pinctrl_utils_free_map,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800110};
111
112static int msm_get_functions_count(struct pinctrl_dev *pctldev)
113{
114 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
115
116 return pctrl->soc->nfunctions;
117}
118
119static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
120 unsigned function)
121{
122 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
123
124 return pctrl->soc->functions[function].name;
125}
126
127static int msm_get_function_groups(struct pinctrl_dev *pctldev,
128 unsigned function,
129 const char * const **groups,
130 unsigned * const num_groups)
131{
132 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
133
134 *groups = pctrl->soc->functions[function].groups;
135 *num_groups = pctrl->soc->functions[function].ngroups;
136 return 0;
137}
138
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200139static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
140 unsigned function,
141 unsigned group)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800142{
143 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
144 const struct msm_pingroup *g;
145 unsigned long flags;
Stephen Boyd47a01ee2016-06-25 22:21:31 -0700146 u32 val, mask;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800147 int i;
148
149 g = &pctrl->soc->groups[group];
Stephen Boyd47a01ee2016-06-25 22:21:31 -0700150 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800151
Bjorn Andersson3c253812014-03-31 14:49:55 -0700152 for (i = 0; i < g->nfuncs; i++) {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800153 if (g->funcs[i] == function)
154 break;
155 }
156
Bjorn Andersson3c253812014-03-31 14:49:55 -0700157 if (WARN_ON(i == g->nfuncs))
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800158 return -EINVAL;
159
160 spin_lock_irqsave(&pctrl->lock, flags);
161
162 val = readl(pctrl->regs + g->ctl_reg);
John Crispin6bcf3f62016-09-12 11:36:55 +0200163 val &= ~mask;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800164 val |= i << g->mux_bit;
165 writel(val, pctrl->regs + g->ctl_reg);
166
167 spin_unlock_irqrestore(&pctrl->lock, flags);
168
169 return 0;
170}
171
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800172static const struct pinmux_ops msm_pinmux_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800173 .get_functions_count = msm_get_functions_count,
174 .get_function_name = msm_get_function_name,
175 .get_function_groups = msm_get_function_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200176 .set_mux = msm_pinmux_set_mux,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800177};
178
179static int msm_config_reg(struct msm_pinctrl *pctrl,
180 const struct msm_pingroup *g,
181 unsigned param,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800182 unsigned *mask,
183 unsigned *bit)
184{
185 switch (param) {
186 case PIN_CONFIG_BIAS_DISABLE:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800187 case PIN_CONFIG_BIAS_PULL_DOWN:
Andy Grossb831a152014-06-17 23:49:11 -0500188 case PIN_CONFIG_BIAS_BUS_HOLD:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800189 case PIN_CONFIG_BIAS_PULL_UP:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800190 *bit = g->pull_bit;
191 *mask = 3;
192 break;
193 case PIN_CONFIG_DRIVE_STRENGTH:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800194 *bit = g->drv_bit;
195 *mask = 7;
196 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800197 case PIN_CONFIG_OUTPUT:
Stanimir Varbanov407f5e32015-03-04 12:41:57 +0200198 case PIN_CONFIG_INPUT_ENABLE:
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800199 *bit = g->oe_bit;
200 *mask = 1;
201 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800202 default:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800203 return -ENOTSUPP;
204 }
205
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800206 return 0;
207}
208
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800209#define MSM_NO_PULL 0
210#define MSM_PULL_DOWN 1
Andy Grossb831a152014-06-17 23:49:11 -0500211#define MSM_KEEPER 2
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800212#define MSM_PULL_UP 3
213
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800214static unsigned msm_regval_to_drive(u32 val)
215{
216 return (val + 1) * 2;
217}
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800218
219static int msm_config_group_get(struct pinctrl_dev *pctldev,
220 unsigned int group,
221 unsigned long *config)
222{
223 const struct msm_pingroup *g;
224 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
225 unsigned param = pinconf_to_config_param(*config);
226 unsigned mask;
227 unsigned arg;
228 unsigned bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800229 int ret;
230 u32 val;
231
232 g = &pctrl->soc->groups[group];
233
Stephen Boyd051a58b2014-03-06 22:44:46 -0800234 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800235 if (ret < 0)
236 return ret;
237
Stephen Boyd051a58b2014-03-06 22:44:46 -0800238 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800239 arg = (val >> bit) & mask;
240
241 /* Convert register value to pinconf value */
242 switch (param) {
243 case PIN_CONFIG_BIAS_DISABLE:
244 arg = arg == MSM_NO_PULL;
245 break;
246 case PIN_CONFIG_BIAS_PULL_DOWN:
247 arg = arg == MSM_PULL_DOWN;
248 break;
Andy Grossb831a152014-06-17 23:49:11 -0500249 case PIN_CONFIG_BIAS_BUS_HOLD:
250 arg = arg == MSM_KEEPER;
251 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800252 case PIN_CONFIG_BIAS_PULL_UP:
253 arg = arg == MSM_PULL_UP;
254 break;
255 case PIN_CONFIG_DRIVE_STRENGTH:
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800256 arg = msm_regval_to_drive(arg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800257 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800258 case PIN_CONFIG_OUTPUT:
259 /* Pin is not output */
260 if (!arg)
261 return -EINVAL;
262
263 val = readl(pctrl->regs + g->io_reg);
264 arg = !!(val & BIT(g->in_bit));
265 break;
Stanimir Varbanov407f5e32015-03-04 12:41:57 +0200266 case PIN_CONFIG_INPUT_ENABLE:
267 /* Pin is output */
268 if (arg)
269 return -EINVAL;
270 arg = 1;
271 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800272 default:
Stanimir Varbanov38d756a2015-03-04 12:41:56 +0200273 return -ENOTSUPP;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800274 }
275
276 *config = pinconf_to_config_packed(param, arg);
277
278 return 0;
279}
280
281static int msm_config_group_set(struct pinctrl_dev *pctldev,
282 unsigned group,
283 unsigned long *configs,
284 unsigned num_configs)
285{
286 const struct msm_pingroup *g;
287 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
288 unsigned long flags;
289 unsigned param;
290 unsigned mask;
291 unsigned arg;
292 unsigned bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800293 int ret;
294 u32 val;
295 int i;
296
297 g = &pctrl->soc->groups[group];
298
299 for (i = 0; i < num_configs; i++) {
300 param = pinconf_to_config_param(configs[i]);
301 arg = pinconf_to_config_argument(configs[i]);
302
Stephen Boyd051a58b2014-03-06 22:44:46 -0800303 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800304 if (ret < 0)
305 return ret;
306
307 /* Convert pinconf values to register values */
308 switch (param) {
309 case PIN_CONFIG_BIAS_DISABLE:
310 arg = MSM_NO_PULL;
311 break;
312 case PIN_CONFIG_BIAS_PULL_DOWN:
313 arg = MSM_PULL_DOWN;
314 break;
Andy Grossb831a152014-06-17 23:49:11 -0500315 case PIN_CONFIG_BIAS_BUS_HOLD:
316 arg = MSM_KEEPER;
317 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800318 case PIN_CONFIG_BIAS_PULL_UP:
319 arg = MSM_PULL_UP;
320 break;
321 case PIN_CONFIG_DRIVE_STRENGTH:
322 /* Check for invalid values */
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800323 if (arg > 16 || arg < 2 || (arg % 2) != 0)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800324 arg = -1;
325 else
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800326 arg = (arg / 2) - 1;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800327 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800328 case PIN_CONFIG_OUTPUT:
329 /* set output value */
330 spin_lock_irqsave(&pctrl->lock, flags);
331 val = readl(pctrl->regs + g->io_reg);
332 if (arg)
333 val |= BIT(g->out_bit);
334 else
335 val &= ~BIT(g->out_bit);
336 writel(val, pctrl->regs + g->io_reg);
337 spin_unlock_irqrestore(&pctrl->lock, flags);
338
339 /* enable output */
340 arg = 1;
341 break;
Stanimir Varbanov407f5e32015-03-04 12:41:57 +0200342 case PIN_CONFIG_INPUT_ENABLE:
343 /* disable output */
344 arg = 0;
345 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800346 default:
347 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
348 param);
349 return -EINVAL;
350 }
351
352 /* Range-check user-supplied value */
353 if (arg & ~mask) {
354 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
355 return -EINVAL;
356 }
357
358 spin_lock_irqsave(&pctrl->lock, flags);
Stephen Boyd051a58b2014-03-06 22:44:46 -0800359 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800360 val &= ~(mask << bit);
361 val |= arg << bit;
Stephen Boyd051a58b2014-03-06 22:44:46 -0800362 writel(val, pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800363 spin_unlock_irqrestore(&pctrl->lock, flags);
364 }
365
366 return 0;
367}
368
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800369static const struct pinconf_ops msm_pinconf_ops = {
Stanimir Varbanov38d756a2015-03-04 12:41:56 +0200370 .is_generic = true,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800371 .pin_config_group_get = msm_config_group_get,
372 .pin_config_group_set = msm_config_group_set,
373};
374
375static struct pinctrl_desc msm_pinctrl_desc = {
376 .pctlops = &msm_pinctrl_ops,
377 .pmxops = &msm_pinmux_ops,
378 .confops = &msm_pinconf_ops,
379 .owner = THIS_MODULE,
380};
381
382static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
383{
384 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100385 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800386 unsigned long flags;
387 u32 val;
388
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800389 g = &pctrl->soc->groups[offset];
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800390
391 spin_lock_irqsave(&pctrl->lock, flags);
392
393 val = readl(pctrl->regs + g->ctl_reg);
394 val &= ~BIT(g->oe_bit);
395 writel(val, pctrl->regs + g->ctl_reg);
396
397 spin_unlock_irqrestore(&pctrl->lock, flags);
398
399 return 0;
400}
401
402static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
403{
404 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100405 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800406 unsigned long flags;
407 u32 val;
408
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800409 g = &pctrl->soc->groups[offset];
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800410
411 spin_lock_irqsave(&pctrl->lock, flags);
412
Axel Line476e772013-12-13 21:35:55 +0800413 val = readl(pctrl->regs + g->io_reg);
414 if (value)
415 val |= BIT(g->out_bit);
416 else
417 val &= ~BIT(g->out_bit);
418 writel(val, pctrl->regs + g->io_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800419
420 val = readl(pctrl->regs + g->ctl_reg);
421 val |= BIT(g->oe_bit);
422 writel(val, pctrl->regs + g->ctl_reg);
423
424 spin_unlock_irqrestore(&pctrl->lock, flags);
425
426 return 0;
427}
428
429static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
430{
431 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100432 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800433 u32 val;
434
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800435 g = &pctrl->soc->groups[offset];
436
437 val = readl(pctrl->regs + g->io_reg);
438 return !!(val & BIT(g->in_bit));
439}
440
441static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
442{
443 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100444 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800445 unsigned long flags;
446 u32 val;
447
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800448 g = &pctrl->soc->groups[offset];
449
450 spin_lock_irqsave(&pctrl->lock, flags);
451
452 val = readl(pctrl->regs + g->io_reg);
Axel Line476e772013-12-13 21:35:55 +0800453 if (value)
454 val |= BIT(g->out_bit);
455 else
456 val &= ~BIT(g->out_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800457 writel(val, pctrl->regs + g->io_reg);
458
459 spin_unlock_irqrestore(&pctrl->lock, flags);
460}
461
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800462#ifdef CONFIG_DEBUG_FS
463#include <linux/seq_file.h>
464
465static void msm_gpio_dbg_show_one(struct seq_file *s,
466 struct pinctrl_dev *pctldev,
467 struct gpio_chip *chip,
468 unsigned offset,
469 unsigned gpio)
470{
471 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100472 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800473 unsigned func;
474 int is_out;
475 int drive;
476 int pull;
477 u32 ctl_reg;
478
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800479 static const char * const pulls[] = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800480 "no pull",
481 "pull down",
482 "keeper",
483 "pull up"
484 };
485
486 g = &pctrl->soc->groups[offset];
487 ctl_reg = readl(pctrl->regs + g->ctl_reg);
488
489 is_out = !!(ctl_reg & BIT(g->oe_bit));
490 func = (ctl_reg >> g->mux_bit) & 7;
491 drive = (ctl_reg >> g->drv_bit) & 7;
492 pull = (ctl_reg >> g->pull_bit) & 3;
493
494 seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800495 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800496 seq_printf(s, " %s", pulls[pull]);
497}
498
499static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
500{
501 unsigned gpio = chip->base;
502 unsigned i;
503
504 for (i = 0; i < chip->ngpio; i++, gpio++) {
505 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800506 seq_puts(s, "\n");
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800507 }
508}
509
510#else
511#define msm_gpio_dbg_show NULL
512#endif
513
514static struct gpio_chip msm_gpio_template = {
515 .direction_input = msm_gpio_direction_input,
516 .direction_output = msm_gpio_direction_output,
517 .get = msm_gpio_get,
518 .set = msm_gpio_set,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200519 .request = gpiochip_generic_request,
520 .free = gpiochip_generic_free,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800521 .dbg_show = msm_gpio_dbg_show,
522};
523
524/* For dual-edge interrupts in software, since some hardware has no
525 * such support:
526 *
527 * At appropriate moments, this function may be called to flip the polarity
528 * settings of both-edge irq lines to try and catch the next edge.
529 *
530 * The attempt is considered successful if:
531 * - the status bit goes high, indicating that an edge was caught, or
532 * - the input value of the gpio doesn't change during the attempt.
533 * If the value changes twice during the process, that would cause the first
534 * test to fail but would force the second, as two opposite
535 * transitions would cause a detection no matter the polarity setting.
536 *
537 * The do-loop tries to sledge-hammer closed the timing hole between
538 * the initial value-read and the polarity-write - if the line value changes
539 * during that window, an interrupt is lost, the new polarity setting is
540 * incorrect, and the first success test will fail, causing a retry.
541 *
542 * Algorithm comes from Google's msmgpio driver.
543 */
544static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
545 const struct msm_pingroup *g,
546 struct irq_data *d)
547{
548 int loop_limit = 100;
549 unsigned val, val2, intstat;
550 unsigned pol;
551
552 do {
553 val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
554
555 pol = readl(pctrl->regs + g->intr_cfg_reg);
556 pol ^= BIT(g->intr_polarity_bit);
557 writel(pol, pctrl->regs + g->intr_cfg_reg);
558
559 val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
560 intstat = readl(pctrl->regs + g->intr_status_reg);
561 if (intstat || (val == val2))
562 return;
563 } while (loop_limit-- > 0);
564 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
565 val, val2);
566}
567
568static void msm_gpio_irq_mask(struct irq_data *d)
569{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700570 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100571 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800572 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800573 unsigned long flags;
574 u32 val;
575
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800576 g = &pctrl->soc->groups[d->hwirq];
577
578 spin_lock_irqsave(&pctrl->lock, flags);
579
580 val = readl(pctrl->regs + g->intr_cfg_reg);
581 val &= ~BIT(g->intr_enable_bit);
582 writel(val, pctrl->regs + g->intr_cfg_reg);
583
584 clear_bit(d->hwirq, pctrl->enabled_irqs);
585
586 spin_unlock_irqrestore(&pctrl->lock, flags);
587}
588
589static void msm_gpio_irq_unmask(struct irq_data *d)
590{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700591 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100592 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800593 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800594 unsigned long flags;
595 u32 val;
596
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800597 g = &pctrl->soc->groups[d->hwirq];
598
599 spin_lock_irqsave(&pctrl->lock, flags);
600
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800601 val = readl(pctrl->regs + g->intr_cfg_reg);
602 val |= BIT(g->intr_enable_bit);
603 writel(val, pctrl->regs + g->intr_cfg_reg);
604
605 set_bit(d->hwirq, pctrl->enabled_irqs);
606
607 spin_unlock_irqrestore(&pctrl->lock, flags);
608}
609
610static void msm_gpio_irq_ack(struct irq_data *d)
611{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700612 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100613 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800614 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800615 unsigned long flags;
616 u32 val;
617
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800618 g = &pctrl->soc->groups[d->hwirq];
619
620 spin_lock_irqsave(&pctrl->lock, flags);
621
622 val = readl(pctrl->regs + g->intr_status_reg);
Bjorn Andersson48f15e92014-03-31 14:49:54 -0700623 if (g->intr_ack_high)
624 val |= BIT(g->intr_status_bit);
625 else
626 val &= ~BIT(g->intr_status_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800627 writel(val, pctrl->regs + g->intr_status_reg);
628
629 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
630 msm_gpio_update_dual_edge_pos(pctrl, g, d);
631
632 spin_unlock_irqrestore(&pctrl->lock, flags);
633}
634
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800635static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
636{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700637 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100638 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800639 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800640 unsigned long flags;
641 u32 val;
642
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800643 g = &pctrl->soc->groups[d->hwirq];
644
645 spin_lock_irqsave(&pctrl->lock, flags);
646
647 /*
648 * For hw without possibility of detecting both edges
649 */
650 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
651 set_bit(d->hwirq, pctrl->dual_edge_irqs);
652 else
653 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
654
655 /* Route interrupts to application cpu */
656 val = readl(pctrl->regs + g->intr_target_reg);
657 val &= ~(7 << g->intr_target_bit);
Georgi Djakovf712c552014-09-03 19:28:16 +0300658 val |= g->intr_target_kpss_val << g->intr_target_bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800659 writel(val, pctrl->regs + g->intr_target_reg);
660
661 /* Update configuration for gpio.
662 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
663 * internal circuitry of TLMM, toggling the RAW_STATUS
664 * could cause the INTR_STATUS to be set for EDGE interrupts.
665 */
666 val = readl(pctrl->regs + g->intr_cfg_reg);
667 val |= BIT(g->intr_raw_status_bit);
668 if (g->intr_detection_width == 2) {
669 val &= ~(3 << g->intr_detection_bit);
670 val &= ~(1 << g->intr_polarity_bit);
671 switch (type) {
672 case IRQ_TYPE_EDGE_RISING:
673 val |= 1 << g->intr_detection_bit;
674 val |= BIT(g->intr_polarity_bit);
675 break;
676 case IRQ_TYPE_EDGE_FALLING:
677 val |= 2 << g->intr_detection_bit;
678 val |= BIT(g->intr_polarity_bit);
679 break;
680 case IRQ_TYPE_EDGE_BOTH:
681 val |= 3 << g->intr_detection_bit;
682 val |= BIT(g->intr_polarity_bit);
683 break;
684 case IRQ_TYPE_LEVEL_LOW:
685 break;
686 case IRQ_TYPE_LEVEL_HIGH:
687 val |= BIT(g->intr_polarity_bit);
688 break;
689 }
690 } else if (g->intr_detection_width == 1) {
691 val &= ~(1 << g->intr_detection_bit);
692 val &= ~(1 << g->intr_polarity_bit);
693 switch (type) {
694 case IRQ_TYPE_EDGE_RISING:
695 val |= BIT(g->intr_detection_bit);
696 val |= BIT(g->intr_polarity_bit);
697 break;
698 case IRQ_TYPE_EDGE_FALLING:
699 val |= BIT(g->intr_detection_bit);
700 break;
701 case IRQ_TYPE_EDGE_BOTH:
702 val |= BIT(g->intr_detection_bit);
Bjorn Andersson48f15e92014-03-31 14:49:54 -0700703 val |= BIT(g->intr_polarity_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800704 break;
705 case IRQ_TYPE_LEVEL_LOW:
706 break;
707 case IRQ_TYPE_LEVEL_HIGH:
708 val |= BIT(g->intr_polarity_bit);
709 break;
710 }
711 } else {
712 BUG();
713 }
714 writel(val, pctrl->regs + g->intr_cfg_reg);
715
716 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
717 msm_gpio_update_dual_edge_pos(pctrl, g, d);
718
719 spin_unlock_irqrestore(&pctrl->lock, flags);
720
721 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner34c0ad82015-06-23 15:52:51 +0200722 irq_set_handler_locked(d, handle_level_irq);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800723 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner34c0ad82015-06-23 15:52:51 +0200724 irq_set_handler_locked(d, handle_edge_irq);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800725
726 return 0;
727}
728
729static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
730{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700731 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijfded3f42015-12-08 09:49:18 +0100732 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800733 unsigned long flags;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800734
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800735 spin_lock_irqsave(&pctrl->lock, flags);
736
Josh Cartwright6aced332014-03-05 13:33:08 -0600737 irq_set_irq_wake(pctrl->irq, on);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800738
739 spin_unlock_irqrestore(&pctrl->lock, flags);
740
741 return 0;
742}
743
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800744static struct irq_chip msm_gpio_irq_chip = {
745 .name = "msmgpio",
746 .irq_mask = msm_gpio_irq_mask,
747 .irq_unmask = msm_gpio_irq_unmask,
748 .irq_ack = msm_gpio_irq_ack,
749 .irq_set_type = msm_gpio_irq_set_type,
750 .irq_set_wake = msm_gpio_irq_set_wake,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800751};
752
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600753static void msm_dirconn_irq_mask(struct irq_data *d)
754{
755 struct irq_desc *desc = irq_data_to_desc(d);
756 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
757
758 if (parent_data->chip->irq_mask)
759 parent_data->chip->irq_mask(parent_data);
760}
761
762static void msm_dirconn_irq_unmask(struct irq_data *d)
763{
764 struct irq_desc *desc = irq_data_to_desc(d);
765 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
766
767 if (parent_data->chip->irq_unmask)
768 parent_data->chip->irq_unmask(parent_data);
769}
770
771static void msm_dirconn_irq_ack(struct irq_data *d)
772{
773 struct irq_desc *desc = irq_data_to_desc(d);
774 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
775
776 if (parent_data->chip->irq_ack)
777 parent_data->chip->irq_ack(parent_data);
778}
779
780static void msm_dirconn_irq_eoi(struct irq_data *d)
781{
782 struct irq_desc *desc = irq_data_to_desc(d);
783 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
784
785 if (parent_data->chip->irq_eoi)
786 parent_data->chip->irq_eoi(parent_data);
787}
788
789static int msm_dirconn_irq_set_affinity(struct irq_data *d,
790 const struct cpumask *maskval, bool force)
791{
792 struct irq_desc *desc = irq_data_to_desc(d);
793 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
794
795 if (parent_data->chip->irq_set_affinity)
796 return parent_data->chip->irq_set_affinity(parent_data,
797 maskval, force);
798 return 0;
799}
800
801static int msm_dirconn_irq_set_vcpu_affinity(struct irq_data *d,
802 void *vcpu_info)
803{
804 struct irq_desc *desc = irq_data_to_desc(d);
805 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
806
807 if (parent_data->chip->irq_set_vcpu_affinity)
808 return parent_data->chip->irq_set_vcpu_affinity(parent_data,
809 vcpu_info);
810 return 0;
811}
812
813static int msm_dirconn_irq_set_type(struct irq_data *d, unsigned int type)
814{
815 struct irq_desc *desc = irq_data_to_desc(d);
816 struct irq_data *parent_data = irq_get_irq_data(desc->parent_irq);
817
818 if (parent_data->chip->irq_set_type)
819 return parent_data->chip->irq_set_type(parent_data, type);
820
821 return 0;
822}
823
824static struct irq_chip msm_dirconn_irq_chip = {
825 .name = "msmgpio-dc",
826 .irq_mask = msm_dirconn_irq_mask,
827 .irq_unmask = msm_dirconn_irq_unmask,
828 .irq_eoi = msm_dirconn_irq_eoi,
829 .irq_ack = msm_dirconn_irq_ack,
830 .irq_set_type = msm_dirconn_irq_set_type,
831 .irq_set_affinity = msm_dirconn_irq_set_affinity,
832 .irq_set_vcpu_affinity = msm_dirconn_irq_set_vcpu_affinity,
833 .flags = IRQCHIP_SKIP_SET_WAKE
834 | IRQCHIP_MASK_ON_SUSPEND
835 | IRQCHIP_SET_TYPE_MASKED,
836};
837
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200838static void msm_gpio_irq_handler(struct irq_desc *desc)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800839{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700840 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800841 const struct msm_pingroup *g;
Linus Walleijfded3f42015-12-08 09:49:18 +0100842 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
Jiang Liu5663bb22015-06-04 12:13:16 +0800843 struct irq_chip *chip = irq_desc_get_chip(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800844 int irq_pin;
845 int handled = 0;
846 u32 val;
847 int i;
848
849 chained_irq_enter(chip, desc);
850
851 /*
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800852 * Each pin has it's own IRQ status register, so use
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800853 * enabled_irq bitmap to limit the number of reads.
854 */
855 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
856 g = &pctrl->soc->groups[i];
857 val = readl(pctrl->regs + g->intr_status_reg);
858 if (val & BIT(g->intr_status_bit)) {
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700859 irq_pin = irq_find_mapping(gc->irqdomain, i);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800860 generic_handle_irq(irq_pin);
861 handled++;
862 }
863 }
864
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800865 /* No interrupts were flagged */
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800866 if (handled == 0)
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200867 handle_bad_irq(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800868
869 chained_irq_exit(chip, desc);
870}
871
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600872static void msm_gpio_dirconn_handler(struct irq_desc *desc)
873{
874 struct irq_data *irqd = irq_desc_get_handler_data(desc);
875 struct irq_chip *chip = irq_desc_get_chip(desc);
876
877 chained_irq_enter(chip, desc);
878 generic_handle_irq(irqd->irq);
879 chained_irq_exit(chip, desc);
880}
881
882static void msm_gpio_setup_dir_connects(struct msm_pinctrl *pctrl)
883{
884 struct device_node *parent_node;
885 struct irq_domain *parent_domain;
886 struct irq_fwspec fwspec;
887 unsigned int i;
888
889 parent_node = of_irq_find_parent(pctrl->dev->of_node);
890
891 if (!parent_node)
892 return;
893
894 parent_domain = irq_find_host(parent_node);
895 if (!parent_domain)
896 return;
897
898 fwspec.fwnode = parent_domain->fwnode;
899 for (i = 0; i < pctrl->soc->n_dir_conns; i++) {
900 const struct msm_dir_conn *dirconn = &pctrl->soc->dir_conn[i];
901 unsigned int parent_irq;
902 int irq;
903
904 fwspec.param[0] = 0; /* SPI */
905 fwspec.param[1] = dirconn->hwirq;
906 fwspec.param[2] = IRQ_TYPE_NONE;
907 fwspec.param_count = 3;
908 parent_irq = irq_create_fwspec_mapping(&fwspec);
909
910 irq = irq_find_mapping(pctrl->chip.irqdomain, dirconn->gpio);
911
912 irq_set_parent(irq, parent_irq);
913 irq_set_chip(irq, &msm_dirconn_irq_chip);
914 irq_set_chip_data(irq, irq_get_irq_data(parent_irq));
915 __irq_set_handler(parent_irq, msm_gpio_dirconn_handler,
916 false, NULL);
917 irq_set_handler_data(parent_irq, irq_get_irq_data(irq));
918 }
919}
920
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800921static int msm_gpio_init(struct msm_pinctrl *pctrl)
922{
923 struct gpio_chip *chip;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800924 int ret;
Stephen Boyddcd278b2014-03-06 22:44:41 -0800925 unsigned ngpio = pctrl->soc->ngpios;
926
927 if (WARN_ON(ngpio > MAX_NR_GPIO))
928 return -EINVAL;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800929
930 chip = &pctrl->chip;
931 chip->base = 0;
Stephen Boyddcd278b2014-03-06 22:44:41 -0800932 chip->ngpio = ngpio;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800933 chip->label = dev_name(pctrl->dev);
Linus Walleij58383c72015-11-04 09:56:26 +0100934 chip->parent = pctrl->dev;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800935 chip->owner = THIS_MODULE;
936 chip->of_node = pctrl->dev->of_node;
937
Linus Walleijfded3f42015-12-08 09:49:18 +0100938 ret = gpiochip_add_data(&pctrl->chip, pctrl);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800939 if (ret) {
940 dev_err(pctrl->dev, "Failed register gpiochip\n");
941 return ret;
942 }
943
944 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
945 if (ret) {
946 dev_err(pctrl->dev, "Failed to add pin range\n");
Pramod Guravc6e927a2014-08-29 13:41:48 +0530947 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800948 return ret;
949 }
950
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700951 ret = gpiochip_irqchip_add(chip,
952 &msm_gpio_irq_chip,
953 0,
954 handle_edge_irq,
955 IRQ_TYPE_NONE);
956 if (ret) {
957 dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
Pramod Guravc6e927a2014-08-29 13:41:48 +0530958 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800959 return -ENOSYS;
960 }
961
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700962 gpiochip_set_chained_irqchip(chip, &msm_gpio_irq_chip, pctrl->irq,
963 msm_gpio_irq_handler);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800964
Mahesh Sivasubramanianfcc313e2017-04-10 09:05:59 -0600965 msm_gpio_setup_dir_connects(pctrl);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800966 return 0;
967}
968
Josh Cartwrightcf1fc182014-09-23 15:59:53 -0500969static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
970 void *data)
Pramod Gurav32745582014-08-29 20:00:59 +0530971{
Josh Cartwrightcf1fc182014-09-23 15:59:53 -0500972 struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
973
974 writel(0, pctrl->regs + PS_HOLD_OFFSET);
975 mdelay(1000);
976 return NOTIFY_DONE;
Pramod Gurav32745582014-08-29 20:00:59 +0530977}
978
Stephen Boydad644982015-07-06 18:09:30 -0700979static struct msm_pinctrl *poweroff_pctrl;
980
981static void msm_ps_hold_poweroff(void)
982{
983 msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
984}
985
Pramod Gurav32745582014-08-29 20:00:59 +0530986static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
987{
Stephen Boydbcd53f82015-01-19 11:17:45 +0100988 int i;
Pramod Gurav32745582014-08-29 20:00:59 +0530989 const struct msm_function *func = pctrl->soc->functions;
990
Stephen Boydbcd53f82015-01-19 11:17:45 +0100991 for (i = 0; i < pctrl->soc->nfunctions; i++)
Pramod Gurav32745582014-08-29 20:00:59 +0530992 if (!strcmp(func[i].name, "ps_hold")) {
Josh Cartwrightcf1fc182014-09-23 15:59:53 -0500993 pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
994 pctrl->restart_nb.priority = 128;
995 if (register_restart_handler(&pctrl->restart_nb))
996 dev_err(pctrl->dev,
997 "failed to setup restart handler.\n");
Stephen Boydad644982015-07-06 18:09:30 -0700998 poweroff_pctrl = pctrl;
999 pm_power_off = msm_ps_hold_poweroff;
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001000 break;
Pramod Gurav32745582014-08-29 20:00:59 +05301001 }
1002}
Pramod Gurav32745582014-08-29 20:00:59 +05301003
Prasad Sodagudid0451852016-07-01 14:12:20 +05301004#ifdef CONFIG_PM
1005static int msm_pinctrl_suspend(void)
1006{
1007 return 0;
1008}
1009
1010static void msm_pinctrl_resume(void)
1011{
1012 int i, irq;
1013 u32 val;
1014 unsigned long flags;
1015 struct irq_desc *desc;
1016 const struct msm_pingroup *g;
1017 const char *name = "null";
1018 struct msm_pinctrl *pctrl = msm_pinctrl_data;
1019
1020 if (!msm_show_resume_irq_mask)
1021 return;
1022
1023 spin_lock_irqsave(&pctrl->lock, flags);
1024 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1025 g = &pctrl->soc->groups[i];
1026 val = readl_relaxed(pctrl->regs + g->intr_status_reg);
1027 if (val & BIT(g->intr_status_bit)) {
1028 irq = irq_find_mapping(pctrl->chip.irqdomain, i);
1029 desc = irq_to_desc(irq);
1030 if (desc == NULL)
1031 name = "stray irq";
1032 else if (desc->action && desc->action->name)
1033 name = desc->action->name;
1034
1035 pr_warn("%s: %d triggered %s\n", __func__, irq, name);
1036 }
1037 }
1038 spin_unlock_irqrestore(&pctrl->lock, flags);
1039}
1040#else
1041#define msm_pinctrl_suspend NULL
1042#define msm_pinctrl_resume NULL
1043#endif
1044
1045static struct syscore_ops msm_pinctrl_pm_ops = {
1046 .suspend = msm_pinctrl_suspend,
1047 .resume = msm_pinctrl_resume,
1048};
1049
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001050int msm_pinctrl_probe(struct platform_device *pdev,
1051 const struct msm_pinctrl_soc_data *soc_data)
1052{
1053 struct msm_pinctrl *pctrl;
1054 struct resource *res;
1055 int ret;
1056
Prasad Sodagudid0451852016-07-01 14:12:20 +05301057 msm_pinctrl_data = pctrl = devm_kzalloc(&pdev->dev,
1058 sizeof(*pctrl), GFP_KERNEL);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001059 if (!pctrl) {
1060 dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
1061 return -ENOMEM;
1062 }
1063 pctrl->dev = &pdev->dev;
1064 pctrl->soc = soc_data;
1065 pctrl->chip = msm_gpio_template;
1066
1067 spin_lock_init(&pctrl->lock);
1068
1069 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1070 pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
1071 if (IS_ERR(pctrl->regs))
1072 return PTR_ERR(pctrl->regs);
1073
Pramod Gurav32745582014-08-29 20:00:59 +05301074 msm_pinctrl_setup_pm_reset(pctrl);
1075
Bjorn Anderssonf393e482013-12-14 23:01:52 -08001076 pctrl->irq = platform_get_irq(pdev, 0);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001077 if (pctrl->irq < 0) {
1078 dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
1079 return pctrl->irq;
1080 }
1081
1082 msm_pinctrl_desc.name = dev_name(&pdev->dev);
1083 msm_pinctrl_desc.pins = pctrl->soc->pins;
1084 msm_pinctrl_desc.npins = pctrl->soc->npins;
Laxman Dewanganfe0267f2016-02-24 14:44:07 +05301085 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &msm_pinctrl_desc,
1086 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001087 if (IS_ERR(pctrl->pctrl)) {
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001088 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001089 return PTR_ERR(pctrl->pctrl);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001090 }
1091
1092 ret = msm_gpio_init(pctrl);
Laxman Dewanganfe0267f2016-02-24 14:44:07 +05301093 if (ret)
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001094 return ret;
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001095
1096 platform_set_drvdata(pdev, pctrl);
1097
Prasad Sodagudid0451852016-07-01 14:12:20 +05301098 register_syscore_ops(&msm_pinctrl_pm_ops);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001099 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1100
1101 return 0;
1102}
1103EXPORT_SYMBOL(msm_pinctrl_probe);
1104
1105int msm_pinctrl_remove(struct platform_device *pdev)
1106{
1107 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001108
Linus Walleij2fcea6c2014-09-16 15:05:41 -07001109 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001110
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001111 unregister_restart_handler(&pctrl->restart_nb);
Prasad Sodagudid0451852016-07-01 14:12:20 +05301112 unregister_syscore_ops(&msm_pinctrl_pm_ops);
Josh Cartwrightcf1fc182014-09-23 15:59:53 -05001113
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001114 return 0;
1115}
1116EXPORT_SYMBOL(msm_pinctrl_remove);
1117