blob: 01eab47a746fd7b37766c22a0e7c081764c3ddc4 [file] [log] [blame]
Bjorn Anderssonf365be02013-12-05 18:10:03 -08001/*
2 * Copyright (c) 2013, Sony Mobile Communications AB.
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
Pramod 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>
20#include <linux/platform_device.h>
21#include <linux/pinctrl/machine.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinmux.h>
24#include <linux/pinctrl/pinconf.h>
25#include <linux/pinctrl/pinconf-generic.h>
26#include <linux/slab.h>
27#include <linux/gpio.h>
28#include <linux/interrupt.h>
Bjorn Anderssonf365be02013-12-05 18:10:03 -080029#include <linux/spinlock.h>
30
Pramod Gurav32745582014-08-29 20:00:59 +053031#include <asm/system_misc.h>
32
Linus Walleij69b78b82014-07-09 13:55:12 +020033#include "../core.h"
34#include "../pinconf.h"
Bjorn Anderssonf365be02013-12-05 18:10:03 -080035#include "pinctrl-msm.h"
Linus Walleij69b78b82014-07-09 13:55:12 +020036#include "../pinctrl-utils.h"
Bjorn Anderssonf365be02013-12-05 18:10:03 -080037
Bjorn Andersson408e3c62013-12-14 23:01:53 -080038#define MAX_NR_GPIO 300
Pramod Gurav32745582014-08-29 20:00:59 +053039#define PS_HOLD_OFFSET 0x820
Bjorn Andersson408e3c62013-12-14 23:01:53 -080040
Bjorn Anderssonf365be02013-12-05 18:10:03 -080041/**
42 * struct msm_pinctrl - state for a pinctrl-msm device
43 * @dev: device handle.
44 * @pctrl: pinctrl handle.
Bjorn Anderssonf365be02013-12-05 18:10:03 -080045 * @chip: gpiochip handle.
46 * @irq: parent irq for the TLMM irq_chip.
47 * @lock: Spinlock to protect register resources as well
48 * as msm_pinctrl data structures.
49 * @enabled_irqs: Bitmap of currently enabled irqs.
50 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
51 * detection.
Bjorn Anderssonf365be02013-12-05 18:10:03 -080052 * @soc; Reference to soc_data of platform specific data.
53 * @regs: Base address for the TLMM register map.
54 */
55struct msm_pinctrl {
56 struct device *dev;
57 struct pinctrl_dev *pctrl;
Bjorn Anderssonf365be02013-12-05 18:10:03 -080058 struct gpio_chip chip;
Bjorn Anderssonf393e482013-12-14 23:01:52 -080059 int irq;
Bjorn Anderssonf365be02013-12-05 18:10:03 -080060
61 spinlock_t lock;
62
Bjorn Andersson408e3c62013-12-14 23:01:53 -080063 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
64 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
Bjorn Anderssonf365be02013-12-05 18:10:03 -080065
66 const struct msm_pinctrl_soc_data *soc;
67 void __iomem *regs;
68};
69
Linus Walleijcdcb0ab2014-04-29 11:00:40 -070070static inline struct msm_pinctrl *to_msm_pinctrl(struct gpio_chip *gc)
71{
72 return container_of(gc, struct msm_pinctrl, chip);
73}
74
Bjorn Anderssonf365be02013-12-05 18:10:03 -080075static int msm_get_groups_count(struct pinctrl_dev *pctldev)
76{
77 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
78
79 return pctrl->soc->ngroups;
80}
81
82static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
83 unsigned group)
84{
85 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
86
87 return pctrl->soc->groups[group].name;
88}
89
90static int msm_get_group_pins(struct pinctrl_dev *pctldev,
91 unsigned group,
92 const unsigned **pins,
93 unsigned *num_pins)
94{
95 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
96
97 *pins = pctrl->soc->groups[group].pins;
98 *num_pins = pctrl->soc->groups[group].npins;
99 return 0;
100}
101
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800102static const struct pinctrl_ops msm_pinctrl_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800103 .get_groups_count = msm_get_groups_count,
104 .get_group_name = msm_get_group_name,
105 .get_group_pins = msm_get_group_pins,
106 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
107 .dt_free_map = pinctrl_utils_dt_free_map,
108};
109
110static int msm_get_functions_count(struct pinctrl_dev *pctldev)
111{
112 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
113
114 return pctrl->soc->nfunctions;
115}
116
117static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
118 unsigned function)
119{
120 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
121
122 return pctrl->soc->functions[function].name;
123}
124
125static int msm_get_function_groups(struct pinctrl_dev *pctldev,
126 unsigned function,
127 const char * const **groups,
128 unsigned * const num_groups)
129{
130 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
131
132 *groups = pctrl->soc->functions[function].groups;
133 *num_groups = pctrl->soc->functions[function].ngroups;
134 return 0;
135}
136
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200137static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
138 unsigned function,
139 unsigned group)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800140{
141 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
142 const struct msm_pingroup *g;
143 unsigned long flags;
144 u32 val;
145 int i;
146
147 g = &pctrl->soc->groups[group];
148
Bjorn Andersson3c253812014-03-31 14:49:55 -0700149 for (i = 0; i < g->nfuncs; i++) {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800150 if (g->funcs[i] == function)
151 break;
152 }
153
Bjorn Andersson3c253812014-03-31 14:49:55 -0700154 if (WARN_ON(i == g->nfuncs))
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800155 return -EINVAL;
156
157 spin_lock_irqsave(&pctrl->lock, flags);
158
159 val = readl(pctrl->regs + g->ctl_reg);
160 val &= ~(0x7 << g->mux_bit);
161 val |= i << g->mux_bit;
162 writel(val, pctrl->regs + g->ctl_reg);
163
164 spin_unlock_irqrestore(&pctrl->lock, flags);
165
166 return 0;
167}
168
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800169static const struct pinmux_ops msm_pinmux_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800170 .get_functions_count = msm_get_functions_count,
171 .get_function_name = msm_get_function_name,
172 .get_function_groups = msm_get_function_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200173 .set_mux = msm_pinmux_set_mux,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800174};
175
176static int msm_config_reg(struct msm_pinctrl *pctrl,
177 const struct msm_pingroup *g,
178 unsigned param,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800179 unsigned *mask,
180 unsigned *bit)
181{
182 switch (param) {
183 case PIN_CONFIG_BIAS_DISABLE:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800184 case PIN_CONFIG_BIAS_PULL_DOWN:
Andy Grossb831a152014-06-17 23:49:11 -0500185 case PIN_CONFIG_BIAS_BUS_HOLD:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800186 case PIN_CONFIG_BIAS_PULL_UP:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800187 *bit = g->pull_bit;
188 *mask = 3;
189 break;
190 case PIN_CONFIG_DRIVE_STRENGTH:
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800191 *bit = g->drv_bit;
192 *mask = 7;
193 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800194 case PIN_CONFIG_OUTPUT:
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800195 *bit = g->oe_bit;
196 *mask = 1;
197 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800198 default:
199 dev_err(pctrl->dev, "Invalid config param %04x\n", param);
200 return -ENOTSUPP;
201 }
202
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800203 return 0;
204}
205
206static int msm_config_get(struct pinctrl_dev *pctldev,
207 unsigned int pin,
208 unsigned long *config)
209{
210 dev_err(pctldev->dev, "pin_config_set op not supported\n");
211 return -ENOTSUPP;
212}
213
214static int msm_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
215 unsigned long *configs, unsigned num_configs)
216{
217 dev_err(pctldev->dev, "pin_config_set op not supported\n");
218 return -ENOTSUPP;
219}
220
221#define MSM_NO_PULL 0
222#define MSM_PULL_DOWN 1
Andy Grossb831a152014-06-17 23:49:11 -0500223#define MSM_KEEPER 2
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800224#define MSM_PULL_UP 3
225
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800226static unsigned msm_regval_to_drive(u32 val)
227{
228 return (val + 1) * 2;
229}
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800230
231static int msm_config_group_get(struct pinctrl_dev *pctldev,
232 unsigned int group,
233 unsigned long *config)
234{
235 const struct msm_pingroup *g;
236 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
237 unsigned param = pinconf_to_config_param(*config);
238 unsigned mask;
239 unsigned arg;
240 unsigned bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800241 int ret;
242 u32 val;
243
244 g = &pctrl->soc->groups[group];
245
Stephen Boyd051a58b2014-03-06 22:44:46 -0800246 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800247 if (ret < 0)
248 return ret;
249
Stephen Boyd051a58b2014-03-06 22:44:46 -0800250 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800251 arg = (val >> bit) & mask;
252
253 /* Convert register value to pinconf value */
254 switch (param) {
255 case PIN_CONFIG_BIAS_DISABLE:
256 arg = arg == MSM_NO_PULL;
257 break;
258 case PIN_CONFIG_BIAS_PULL_DOWN:
259 arg = arg == MSM_PULL_DOWN;
260 break;
Andy Grossb831a152014-06-17 23:49:11 -0500261 case PIN_CONFIG_BIAS_BUS_HOLD:
262 arg = arg == MSM_KEEPER;
263 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800264 case PIN_CONFIG_BIAS_PULL_UP:
265 arg = arg == MSM_PULL_UP;
266 break;
267 case PIN_CONFIG_DRIVE_STRENGTH:
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800268 arg = msm_regval_to_drive(arg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800269 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800270 case PIN_CONFIG_OUTPUT:
271 /* Pin is not output */
272 if (!arg)
273 return -EINVAL;
274
275 val = readl(pctrl->regs + g->io_reg);
276 arg = !!(val & BIT(g->in_bit));
277 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800278 default:
279 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
280 param);
281 return -EINVAL;
282 }
283
284 *config = pinconf_to_config_packed(param, arg);
285
286 return 0;
287}
288
289static int msm_config_group_set(struct pinctrl_dev *pctldev,
290 unsigned group,
291 unsigned long *configs,
292 unsigned num_configs)
293{
294 const struct msm_pingroup *g;
295 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
296 unsigned long flags;
297 unsigned param;
298 unsigned mask;
299 unsigned arg;
300 unsigned bit;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800301 int ret;
302 u32 val;
303 int i;
304
305 g = &pctrl->soc->groups[group];
306
307 for (i = 0; i < num_configs; i++) {
308 param = pinconf_to_config_param(configs[i]);
309 arg = pinconf_to_config_argument(configs[i]);
310
Stephen Boyd051a58b2014-03-06 22:44:46 -0800311 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800312 if (ret < 0)
313 return ret;
314
315 /* Convert pinconf values to register values */
316 switch (param) {
317 case PIN_CONFIG_BIAS_DISABLE:
318 arg = MSM_NO_PULL;
319 break;
320 case PIN_CONFIG_BIAS_PULL_DOWN:
321 arg = MSM_PULL_DOWN;
322 break;
Andy Grossb831a152014-06-17 23:49:11 -0500323 case PIN_CONFIG_BIAS_BUS_HOLD:
324 arg = MSM_KEEPER;
325 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800326 case PIN_CONFIG_BIAS_PULL_UP:
327 arg = MSM_PULL_UP;
328 break;
329 case PIN_CONFIG_DRIVE_STRENGTH:
330 /* Check for invalid values */
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800331 if (arg > 16 || arg < 2 || (arg % 2) != 0)
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800332 arg = -1;
333 else
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800334 arg = (arg / 2) - 1;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800335 break;
Bjorn Anderssoned118a52014-02-04 19:55:31 -0800336 case PIN_CONFIG_OUTPUT:
337 /* set output value */
338 spin_lock_irqsave(&pctrl->lock, flags);
339 val = readl(pctrl->regs + g->io_reg);
340 if (arg)
341 val |= BIT(g->out_bit);
342 else
343 val &= ~BIT(g->out_bit);
344 writel(val, pctrl->regs + g->io_reg);
345 spin_unlock_irqrestore(&pctrl->lock, flags);
346
347 /* enable output */
348 arg = 1;
349 break;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800350 default:
351 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
352 param);
353 return -EINVAL;
354 }
355
356 /* Range-check user-supplied value */
357 if (arg & ~mask) {
358 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
359 return -EINVAL;
360 }
361
362 spin_lock_irqsave(&pctrl->lock, flags);
Stephen Boyd051a58b2014-03-06 22:44:46 -0800363 val = readl(pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800364 val &= ~(mask << bit);
365 val |= arg << bit;
Stephen Boyd051a58b2014-03-06 22:44:46 -0800366 writel(val, pctrl->regs + g->ctl_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800367 spin_unlock_irqrestore(&pctrl->lock, flags);
368 }
369
370 return 0;
371}
372
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800373static const struct pinconf_ops msm_pinconf_ops = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800374 .pin_config_get = msm_config_get,
375 .pin_config_set = msm_config_set,
376 .pin_config_group_get = msm_config_group_get,
377 .pin_config_group_set = msm_config_group_set,
378};
379
380static struct pinctrl_desc msm_pinctrl_desc = {
381 .pctlops = &msm_pinctrl_ops,
382 .pmxops = &msm_pinmux_ops,
383 .confops = &msm_pinconf_ops,
384 .owner = THIS_MODULE,
385};
386
387static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
388{
389 const struct msm_pingroup *g;
390 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
391 unsigned long flags;
392 u32 val;
393
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800394 g = &pctrl->soc->groups[offset];
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800395
396 spin_lock_irqsave(&pctrl->lock, flags);
397
398 val = readl(pctrl->regs + g->ctl_reg);
399 val &= ~BIT(g->oe_bit);
400 writel(val, pctrl->regs + g->ctl_reg);
401
402 spin_unlock_irqrestore(&pctrl->lock, flags);
403
404 return 0;
405}
406
407static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
408{
409 const struct msm_pingroup *g;
410 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
411 unsigned long flags;
412 u32 val;
413
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800414 g = &pctrl->soc->groups[offset];
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800415
416 spin_lock_irqsave(&pctrl->lock, flags);
417
Axel Line476e772013-12-13 21:35:55 +0800418 val = readl(pctrl->regs + g->io_reg);
419 if (value)
420 val |= BIT(g->out_bit);
421 else
422 val &= ~BIT(g->out_bit);
423 writel(val, pctrl->regs + g->io_reg);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800424
425 val = readl(pctrl->regs + g->ctl_reg);
426 val |= BIT(g->oe_bit);
427 writel(val, pctrl->regs + g->ctl_reg);
428
429 spin_unlock_irqrestore(&pctrl->lock, flags);
430
431 return 0;
432}
433
434static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
435{
436 const struct msm_pingroup *g;
437 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
438 u32 val;
439
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800440 g = &pctrl->soc->groups[offset];
441
442 val = readl(pctrl->regs + g->io_reg);
443 return !!(val & BIT(g->in_bit));
444}
445
446static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
447{
448 const struct msm_pingroup *g;
449 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
450 unsigned long flags;
451 u32 val;
452
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800453 g = &pctrl->soc->groups[offset];
454
455 spin_lock_irqsave(&pctrl->lock, flags);
456
457 val = readl(pctrl->regs + g->io_reg);
Axel Line476e772013-12-13 21:35:55 +0800458 if (value)
459 val |= BIT(g->out_bit);
460 else
461 val &= ~BIT(g->out_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800462 writel(val, pctrl->regs + g->io_reg);
463
464 spin_unlock_irqrestore(&pctrl->lock, flags);
465}
466
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800467static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
468{
469 int gpio = chip->base + offset;
470 return pinctrl_request_gpio(gpio);
471}
472
473static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
474{
475 int gpio = chip->base + offset;
476 return pinctrl_free_gpio(gpio);
477}
478
479#ifdef CONFIG_DEBUG_FS
480#include <linux/seq_file.h>
481
482static void msm_gpio_dbg_show_one(struct seq_file *s,
483 struct pinctrl_dev *pctldev,
484 struct gpio_chip *chip,
485 unsigned offset,
486 unsigned gpio)
487{
488 const struct msm_pingroup *g;
489 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
490 unsigned func;
491 int is_out;
492 int drive;
493 int pull;
494 u32 ctl_reg;
495
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800496 static const char * const pulls[] = {
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800497 "no pull",
498 "pull down",
499 "keeper",
500 "pull up"
501 };
502
503 g = &pctrl->soc->groups[offset];
504 ctl_reg = readl(pctrl->regs + g->ctl_reg);
505
506 is_out = !!(ctl_reg & BIT(g->oe_bit));
507 func = (ctl_reg >> g->mux_bit) & 7;
508 drive = (ctl_reg >> g->drv_bit) & 7;
509 pull = (ctl_reg >> g->pull_bit) & 3;
510
511 seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
Stephen Boyd7cc34e22014-03-06 22:44:44 -0800512 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800513 seq_printf(s, " %s", pulls[pull]);
514}
515
516static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
517{
518 unsigned gpio = chip->base;
519 unsigned i;
520
521 for (i = 0; i < chip->ngpio; i++, gpio++) {
522 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800523 seq_puts(s, "\n");
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800524 }
525}
526
527#else
528#define msm_gpio_dbg_show NULL
529#endif
530
531static struct gpio_chip msm_gpio_template = {
532 .direction_input = msm_gpio_direction_input,
533 .direction_output = msm_gpio_direction_output,
534 .get = msm_gpio_get,
535 .set = msm_gpio_set,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800536 .request = msm_gpio_request,
537 .free = msm_gpio_free,
538 .dbg_show = msm_gpio_dbg_show,
539};
540
541/* For dual-edge interrupts in software, since some hardware has no
542 * such support:
543 *
544 * At appropriate moments, this function may be called to flip the polarity
545 * settings of both-edge irq lines to try and catch the next edge.
546 *
547 * The attempt is considered successful if:
548 * - the status bit goes high, indicating that an edge was caught, or
549 * - the input value of the gpio doesn't change during the attempt.
550 * If the value changes twice during the process, that would cause the first
551 * test to fail but would force the second, as two opposite
552 * transitions would cause a detection no matter the polarity setting.
553 *
554 * The do-loop tries to sledge-hammer closed the timing hole between
555 * the initial value-read and the polarity-write - if the line value changes
556 * during that window, an interrupt is lost, the new polarity setting is
557 * incorrect, and the first success test will fail, causing a retry.
558 *
559 * Algorithm comes from Google's msmgpio driver.
560 */
561static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
562 const struct msm_pingroup *g,
563 struct irq_data *d)
564{
565 int loop_limit = 100;
566 unsigned val, val2, intstat;
567 unsigned pol;
568
569 do {
570 val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
571
572 pol = readl(pctrl->regs + g->intr_cfg_reg);
573 pol ^= BIT(g->intr_polarity_bit);
574 writel(pol, pctrl->regs + g->intr_cfg_reg);
575
576 val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
577 intstat = readl(pctrl->regs + g->intr_status_reg);
578 if (intstat || (val == val2))
579 return;
580 } while (loop_limit-- > 0);
581 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
582 val, val2);
583}
584
585static void msm_gpio_irq_mask(struct irq_data *d)
586{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700587 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
588 struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800589 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800590 unsigned long flags;
591 u32 val;
592
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800593 g = &pctrl->soc->groups[d->hwirq];
594
595 spin_lock_irqsave(&pctrl->lock, flags);
596
597 val = readl(pctrl->regs + g->intr_cfg_reg);
598 val &= ~BIT(g->intr_enable_bit);
599 writel(val, pctrl->regs + g->intr_cfg_reg);
600
601 clear_bit(d->hwirq, pctrl->enabled_irqs);
602
603 spin_unlock_irqrestore(&pctrl->lock, flags);
604}
605
606static void msm_gpio_irq_unmask(struct irq_data *d)
607{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700608 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
609 struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800610 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800611 unsigned long flags;
612 u32 val;
613
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800614 g = &pctrl->soc->groups[d->hwirq];
615
616 spin_lock_irqsave(&pctrl->lock, flags);
617
618 val = readl(pctrl->regs + g->intr_status_reg);
619 val &= ~BIT(g->intr_status_bit);
620 writel(val, pctrl->regs + g->intr_status_reg);
621
622 val = readl(pctrl->regs + g->intr_cfg_reg);
623 val |= BIT(g->intr_enable_bit);
624 writel(val, pctrl->regs + g->intr_cfg_reg);
625
626 set_bit(d->hwirq, pctrl->enabled_irqs);
627
628 spin_unlock_irqrestore(&pctrl->lock, flags);
629}
630
631static void msm_gpio_irq_ack(struct irq_data *d)
632{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700633 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
634 struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800635 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800636 unsigned long flags;
637 u32 val;
638
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800639 g = &pctrl->soc->groups[d->hwirq];
640
641 spin_lock_irqsave(&pctrl->lock, flags);
642
643 val = readl(pctrl->regs + g->intr_status_reg);
Bjorn Andersson48f15e92014-03-31 14:49:54 -0700644 if (g->intr_ack_high)
645 val |= BIT(g->intr_status_bit);
646 else
647 val &= ~BIT(g->intr_status_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800648 writel(val, pctrl->regs + g->intr_status_reg);
649
650 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
651 msm_gpio_update_dual_edge_pos(pctrl, g, d);
652
653 spin_unlock_irqrestore(&pctrl->lock, flags);
654}
655
656#define INTR_TARGET_PROC_APPS 4
657
658static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
659{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700660 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
661 struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800662 const struct msm_pingroup *g;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800663 unsigned long flags;
664 u32 val;
665
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800666 g = &pctrl->soc->groups[d->hwirq];
667
668 spin_lock_irqsave(&pctrl->lock, flags);
669
670 /*
671 * For hw without possibility of detecting both edges
672 */
673 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
674 set_bit(d->hwirq, pctrl->dual_edge_irqs);
675 else
676 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
677
678 /* Route interrupts to application cpu */
679 val = readl(pctrl->regs + g->intr_target_reg);
680 val &= ~(7 << g->intr_target_bit);
681 val |= INTR_TARGET_PROC_APPS << g->intr_target_bit;
682 writel(val, pctrl->regs + g->intr_target_reg);
683
684 /* Update configuration for gpio.
685 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
686 * internal circuitry of TLMM, toggling the RAW_STATUS
687 * could cause the INTR_STATUS to be set for EDGE interrupts.
688 */
689 val = readl(pctrl->regs + g->intr_cfg_reg);
690 val |= BIT(g->intr_raw_status_bit);
691 if (g->intr_detection_width == 2) {
692 val &= ~(3 << g->intr_detection_bit);
693 val &= ~(1 << g->intr_polarity_bit);
694 switch (type) {
695 case IRQ_TYPE_EDGE_RISING:
696 val |= 1 << g->intr_detection_bit;
697 val |= BIT(g->intr_polarity_bit);
698 break;
699 case IRQ_TYPE_EDGE_FALLING:
700 val |= 2 << g->intr_detection_bit;
701 val |= BIT(g->intr_polarity_bit);
702 break;
703 case IRQ_TYPE_EDGE_BOTH:
704 val |= 3 << g->intr_detection_bit;
705 val |= BIT(g->intr_polarity_bit);
706 break;
707 case IRQ_TYPE_LEVEL_LOW:
708 break;
709 case IRQ_TYPE_LEVEL_HIGH:
710 val |= BIT(g->intr_polarity_bit);
711 break;
712 }
713 } else if (g->intr_detection_width == 1) {
714 val &= ~(1 << g->intr_detection_bit);
715 val &= ~(1 << g->intr_polarity_bit);
716 switch (type) {
717 case IRQ_TYPE_EDGE_RISING:
718 val |= BIT(g->intr_detection_bit);
719 val |= BIT(g->intr_polarity_bit);
720 break;
721 case IRQ_TYPE_EDGE_FALLING:
722 val |= BIT(g->intr_detection_bit);
723 break;
724 case IRQ_TYPE_EDGE_BOTH:
725 val |= BIT(g->intr_detection_bit);
Bjorn Andersson48f15e92014-03-31 14:49:54 -0700726 val |= BIT(g->intr_polarity_bit);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800727 break;
728 case IRQ_TYPE_LEVEL_LOW:
729 break;
730 case IRQ_TYPE_LEVEL_HIGH:
731 val |= BIT(g->intr_polarity_bit);
732 break;
733 }
734 } else {
735 BUG();
736 }
737 writel(val, pctrl->regs + g->intr_cfg_reg);
738
739 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
740 msm_gpio_update_dual_edge_pos(pctrl, g, d);
741
742 spin_unlock_irqrestore(&pctrl->lock, flags);
743
744 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
745 __irq_set_handler_locked(d->irq, handle_level_irq);
746 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
747 __irq_set_handler_locked(d->irq, handle_edge_irq);
748
749 return 0;
750}
751
752static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
753{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700754 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
755 struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800756 unsigned long flags;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800757
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800758 spin_lock_irqsave(&pctrl->lock, flags);
759
Josh Cartwright6aced332014-03-05 13:33:08 -0600760 irq_set_irq_wake(pctrl->irq, on);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800761
762 spin_unlock_irqrestore(&pctrl->lock, flags);
763
764 return 0;
765}
766
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800767static struct irq_chip msm_gpio_irq_chip = {
768 .name = "msmgpio",
769 .irq_mask = msm_gpio_irq_mask,
770 .irq_unmask = msm_gpio_irq_unmask,
771 .irq_ack = msm_gpio_irq_ack,
772 .irq_set_type = msm_gpio_irq_set_type,
773 .irq_set_wake = msm_gpio_irq_set_wake,
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800774};
775
776static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
777{
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700778 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800779 const struct msm_pingroup *g;
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700780 struct msm_pinctrl *pctrl = to_msm_pinctrl(gc);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800781 struct irq_chip *chip = irq_get_chip(irq);
782 int irq_pin;
783 int handled = 0;
784 u32 val;
785 int i;
786
787 chained_irq_enter(chip, desc);
788
789 /*
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800790 * Each pin has it's own IRQ status register, so use
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800791 * enabled_irq bitmap to limit the number of reads.
792 */
793 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
794 g = &pctrl->soc->groups[i];
795 val = readl(pctrl->regs + g->intr_status_reg);
796 if (val & BIT(g->intr_status_bit)) {
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700797 irq_pin = irq_find_mapping(gc->irqdomain, i);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800798 generic_handle_irq(irq_pin);
799 handled++;
800 }
801 }
802
Bjorn Andersson1f2b2392013-12-14 23:01:51 -0800803 /* No interrupts were flagged */
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800804 if (handled == 0)
805 handle_bad_irq(irq, desc);
806
807 chained_irq_exit(chip, desc);
808}
809
810static int msm_gpio_init(struct msm_pinctrl *pctrl)
811{
812 struct gpio_chip *chip;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800813 int ret;
Stephen Boyddcd278b2014-03-06 22:44:41 -0800814 unsigned ngpio = pctrl->soc->ngpios;
815
816 if (WARN_ON(ngpio > MAX_NR_GPIO))
817 return -EINVAL;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800818
819 chip = &pctrl->chip;
820 chip->base = 0;
Stephen Boyddcd278b2014-03-06 22:44:41 -0800821 chip->ngpio = ngpio;
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800822 chip->label = dev_name(pctrl->dev);
823 chip->dev = pctrl->dev;
824 chip->owner = THIS_MODULE;
825 chip->of_node = pctrl->dev->of_node;
826
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800827 ret = gpiochip_add(&pctrl->chip);
828 if (ret) {
829 dev_err(pctrl->dev, "Failed register gpiochip\n");
830 return ret;
831 }
832
833 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
834 if (ret) {
835 dev_err(pctrl->dev, "Failed to add pin range\n");
Pramod Guravc6e927a2014-08-29 13:41:48 +0530836 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800837 return ret;
838 }
839
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700840 ret = gpiochip_irqchip_add(chip,
841 &msm_gpio_irq_chip,
842 0,
843 handle_edge_irq,
844 IRQ_TYPE_NONE);
845 if (ret) {
846 dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
Pramod Guravc6e927a2014-08-29 13:41:48 +0530847 gpiochip_remove(&pctrl->chip);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800848 return -ENOSYS;
849 }
850
Linus Walleijcdcb0ab2014-04-29 11:00:40 -0700851 gpiochip_set_chained_irqchip(chip, &msm_gpio_irq_chip, pctrl->irq,
852 msm_gpio_irq_handler);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800853
854 return 0;
855}
856
Pramod Gurav32745582014-08-29 20:00:59 +0530857#ifdef CONFIG_ARM
858static void __iomem *msm_ps_hold;
859
860static void msm_reset(enum reboot_mode reboot_mode, const char *cmd)
861{
862 writel(0, msm_ps_hold);
863 mdelay(10000);
864}
865
866static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
867{
868 int i = 0;
869 const struct msm_function *func = pctrl->soc->functions;
870
871 for (; i <= pctrl->soc->nfunctions; i++)
872 if (!strcmp(func[i].name, "ps_hold")) {
873 msm_ps_hold = pctrl->regs + PS_HOLD_OFFSET;
874 arm_pm_restart = msm_reset;
875 }
876}
877#else
878static void msm_pinctrl_setup_pm_reset(const struct msm_pinctrl *pctrl) {}
879#endif
880
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800881int msm_pinctrl_probe(struct platform_device *pdev,
882 const struct msm_pinctrl_soc_data *soc_data)
883{
884 struct msm_pinctrl *pctrl;
885 struct resource *res;
886 int ret;
887
888 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
889 if (!pctrl) {
890 dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
891 return -ENOMEM;
892 }
893 pctrl->dev = &pdev->dev;
894 pctrl->soc = soc_data;
895 pctrl->chip = msm_gpio_template;
896
897 spin_lock_init(&pctrl->lock);
898
899 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
900 pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
901 if (IS_ERR(pctrl->regs))
902 return PTR_ERR(pctrl->regs);
903
Pramod Gurav32745582014-08-29 20:00:59 +0530904 msm_pinctrl_setup_pm_reset(pctrl);
905
Bjorn Anderssonf393e482013-12-14 23:01:52 -0800906 pctrl->irq = platform_get_irq(pdev, 0);
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800907 if (pctrl->irq < 0) {
908 dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
909 return pctrl->irq;
910 }
911
912 msm_pinctrl_desc.name = dev_name(&pdev->dev);
913 msm_pinctrl_desc.pins = pctrl->soc->pins;
914 msm_pinctrl_desc.npins = pctrl->soc->npins;
915 pctrl->pctrl = pinctrl_register(&msm_pinctrl_desc, &pdev->dev, pctrl);
916 if (!pctrl->pctrl) {
917 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
918 return -ENODEV;
919 }
920
921 ret = msm_gpio_init(pctrl);
922 if (ret) {
923 pinctrl_unregister(pctrl->pctrl);
924 return ret;
925 }
926
927 platform_set_drvdata(pdev, pctrl);
928
929 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
930
931 return 0;
932}
933EXPORT_SYMBOL(msm_pinctrl_probe);
934
935int msm_pinctrl_remove(struct platform_device *pdev)
936{
937 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
938 int ret;
939
Bjorn Anderssonf393e482013-12-14 23:01:52 -0800940 ret = gpiochip_remove(&pctrl->chip);
941 if (ret) {
942 dev_err(&pdev->dev, "Failed to remove gpiochip\n");
943 return ret;
944 }
945
Bjorn Anderssonf365be02013-12-05 18:10:03 -0800946 pinctrl_unregister(pctrl->pctrl);
947
948 return 0;
949}
950EXPORT_SYMBOL(msm_pinctrl_remove);
951