1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2013, Sony Mobile Communications AB.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 */
6
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/log2.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm.h>
17 #include <linux/firmware/qcom/qcom_scm.h>
18 #include <linux/reboot.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/string_choices.h>
23
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/pinctrl/pinconf.h>
27 #include <linux/pinctrl/pinmux.h>
28
29 #include <linux/soc/qcom/irq.h>
30
31 #include "../core.h"
32 #include "../pinconf.h"
33 #include "../pinctrl-utils.h"
34
35 #include "pinctrl-msm.h"
36
37 #define MAX_NR_GPIO 300
38 #define MAX_NR_TILES 4
39 #define PS_HOLD_OFFSET 0x820
40
41 /**
42 * struct msm_pinctrl - state for a pinctrl-msm device
43 * @dev: device handle.
44 * @pctrl: pinctrl handle.
45 * @chip: gpiochip handle.
46 * @desc: pin controller descriptor
47 * @irq: parent irq for the TLMM irq_chip.
48 * @intr_target_use_scm: route irq to application cpu using scm calls
49 * @lock: Spinlock to protect register resources as well
50 * as msm_pinctrl data structures.
51 * @enabled_irqs: Bitmap of currently enabled irqs.
52 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
53 * detection.
54 * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
55 * @disabled_for_mux: These IRQs were disabled because we muxed away.
56 * @ever_gpio: This bit is set the first time we mux a pin to gpio_func.
57 * @soc: Reference to soc_data of platform specific data.
58 * @regs: Base addresses for the TLMM tiles.
59 * @phys_base: Physical base address
60 */
61 struct msm_pinctrl {
62 struct device *dev;
63 struct pinctrl_dev *pctrl;
64 struct gpio_chip chip;
65 struct pinctrl_desc desc;
66
67 int irq;
68
69 bool intr_target_use_scm;
70
71 raw_spinlock_t lock;
72
73 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
74 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
75 DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
76 DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
77 DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
78
79 const struct msm_pinctrl_soc_data *soc;
80 void __iomem *regs[MAX_NR_TILES];
81 u32 phys_base[MAX_NR_TILES];
82 };
83
84 #define MSM_ACCESSOR(name) \
85 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
86 const struct msm_pingroup *g) \
87 { \
88 return readl(pctrl->regs[g->tile] + g->name##_reg); \
89 } \
90 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
91 const struct msm_pingroup *g) \
92 { \
93 writel(val, pctrl->regs[g->tile] + g->name##_reg); \
94 }
95
96 MSM_ACCESSOR(ctl)
MSM_ACCESSOR(io)97 MSM_ACCESSOR(io)
98 MSM_ACCESSOR(intr_cfg)
99 MSM_ACCESSOR(intr_status)
100 MSM_ACCESSOR(intr_target)
101
102 static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
103 const struct msm_pingroup *g)
104 {
105 u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
106
107 msm_writel_intr_status(val, pctrl, g);
108 }
109
msm_get_groups_count(struct pinctrl_dev * pctldev)110 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
111 {
112 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
113
114 return pctrl->soc->ngroups;
115 }
116
msm_get_group_name(struct pinctrl_dev * pctldev,unsigned group)117 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
118 unsigned group)
119 {
120 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
121
122 return pctrl->soc->groups[group].grp.name;
123 }
124
msm_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * num_pins)125 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
126 unsigned group,
127 const unsigned **pins,
128 unsigned *num_pins)
129 {
130 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
131
132 *pins = pctrl->soc->groups[group].grp.pins;
133 *num_pins = pctrl->soc->groups[group].grp.npins;
134 return 0;
135 }
136
137 static const struct pinctrl_ops msm_pinctrl_ops = {
138 .get_groups_count = msm_get_groups_count,
139 .get_group_name = msm_get_group_name,
140 .get_group_pins = msm_get_group_pins,
141 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
142 .dt_free_map = pinctrl_utils_free_map,
143 };
144
msm_pinmux_request(struct pinctrl_dev * pctldev,unsigned offset)145 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
146 {
147 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
148 struct gpio_chip *chip = &pctrl->chip;
149
150 return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
151 }
152
msm_get_functions_count(struct pinctrl_dev * pctldev)153 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
154 {
155 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
156
157 return pctrl->soc->nfunctions;
158 }
159
msm_get_function_name(struct pinctrl_dev * pctldev,unsigned function)160 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
161 unsigned function)
162 {
163 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
164
165 return pctrl->soc->functions[function].name;
166 }
167
msm_get_function_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const num_groups)168 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
169 unsigned function,
170 const char * const **groups,
171 unsigned * const num_groups)
172 {
173 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
174
175 *groups = pctrl->soc->functions[function].groups;
176 *num_groups = pctrl->soc->functions[function].ngroups;
177 return 0;
178 }
179
msm_pinmux_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)180 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
181 unsigned function,
182 unsigned group)
183 {
184 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
185 struct gpio_chip *gc = &pctrl->chip;
186 unsigned int irq = irq_find_mapping(gc->irq.domain, group);
187 struct irq_data *d = irq_get_irq_data(irq);
188 unsigned int gpio_func = pctrl->soc->gpio_func;
189 unsigned int egpio_func = pctrl->soc->egpio_func;
190 const struct msm_pingroup *g;
191 unsigned long flags;
192 u32 val, mask;
193 int i;
194
195 g = &pctrl->soc->groups[group];
196 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
197
198 for (i = 0; i < g->nfuncs; i++) {
199 if (g->funcs[i] == function)
200 break;
201 }
202
203 if (WARN_ON(i == g->nfuncs))
204 return -EINVAL;
205
206 /*
207 * If an GPIO interrupt is setup on this pin then we need special
208 * handling. Specifically interrupt detection logic will still see
209 * the pin twiddle even when we're muxed away.
210 *
211 * When we see a pin with an interrupt setup on it then we'll disable
212 * (mask) interrupts on it when we mux away until we mux back. Note
213 * that disable_irq() refcounts and interrupts are disabled as long as
214 * at least one disable_irq() has been called.
215 */
216 if (d && i != gpio_func &&
217 !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
218 disable_irq(irq);
219
220 raw_spin_lock_irqsave(&pctrl->lock, flags);
221
222 val = msm_readl_ctl(pctrl, g);
223
224 /*
225 * If this is the first time muxing to GPIO and the direction is
226 * output, make sure that we're not going to be glitching the pin
227 * by reading the current state of the pin and setting it as the
228 * output.
229 */
230 if (i == gpio_func && (val & BIT(g->oe_bit)) &&
231 !test_and_set_bit(group, pctrl->ever_gpio)) {
232 u32 io_val = msm_readl_io(pctrl, g);
233
234 if (io_val & BIT(g->in_bit)) {
235 if (!(io_val & BIT(g->out_bit)))
236 msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
237 } else {
238 if (io_val & BIT(g->out_bit))
239 msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
240 }
241 }
242
243 if (egpio_func && i == egpio_func) {
244 if (val & BIT(g->egpio_present))
245 val &= ~BIT(g->egpio_enable);
246 } else {
247 val &= ~mask;
248 val |= i << g->mux_bit;
249 /* Claim ownership of pin if egpio capable */
250 if (egpio_func && val & BIT(g->egpio_present))
251 val |= BIT(g->egpio_enable);
252 }
253
254 msm_writel_ctl(val, pctrl, g);
255
256 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
257
258 if (d && i == gpio_func &&
259 test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
260 /*
261 * Clear interrupts detected while not GPIO since we only
262 * masked things.
263 */
264 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
265 irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
266 else
267 msm_ack_intr_status(pctrl, g);
268
269 enable_irq(irq);
270 }
271
272 return 0;
273 }
274
msm_pinmux_request_gpio(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)275 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
276 struct pinctrl_gpio_range *range,
277 unsigned offset)
278 {
279 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
280 const struct msm_pingroup *g = &pctrl->soc->groups[offset];
281
282 /* No funcs? Probably ACPI so can't do anything here */
283 if (!g->nfuncs)
284 return 0;
285
286 return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
287 }
288
289 static const struct pinmux_ops msm_pinmux_ops = {
290 .request = msm_pinmux_request,
291 .get_functions_count = msm_get_functions_count,
292 .get_function_name = msm_get_function_name,
293 .get_function_groups = msm_get_function_groups,
294 .gpio_request_enable = msm_pinmux_request_gpio,
295 .set_mux = msm_pinmux_set_mux,
296 };
297
msm_config_reg(struct msm_pinctrl * pctrl,const struct msm_pingroup * g,unsigned param,unsigned * mask,unsigned * bit)298 static int msm_config_reg(struct msm_pinctrl *pctrl,
299 const struct msm_pingroup *g,
300 unsigned param,
301 unsigned *mask,
302 unsigned *bit)
303 {
304 switch (param) {
305 case PIN_CONFIG_BIAS_DISABLE:
306 case PIN_CONFIG_BIAS_PULL_DOWN:
307 case PIN_CONFIG_BIAS_BUS_HOLD:
308 case PIN_CONFIG_BIAS_PULL_UP:
309 *bit = g->pull_bit;
310 *mask = 3;
311 if (g->i2c_pull_bit)
312 *mask |= BIT(g->i2c_pull_bit) >> *bit;
313 break;
314 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
315 *bit = g->od_bit;
316 *mask = 1;
317 break;
318 case PIN_CONFIG_DRIVE_STRENGTH:
319 *bit = g->drv_bit;
320 *mask = 7;
321 break;
322 case PIN_CONFIG_OUTPUT:
323 case PIN_CONFIG_INPUT_ENABLE:
324 case PIN_CONFIG_OUTPUT_ENABLE:
325 *bit = g->oe_bit;
326 *mask = 1;
327 break;
328 default:
329 return -ENOTSUPP;
330 }
331
332 return 0;
333 }
334
335 #define MSM_NO_PULL 0
336 #define MSM_PULL_DOWN 1
337 #define MSM_KEEPER 2
338 #define MSM_PULL_UP_NO_KEEPER 2
339 #define MSM_PULL_UP 3
340 #define MSM_I2C_STRONG_PULL_UP 2200
341
msm_regval_to_drive(u32 val)342 static unsigned msm_regval_to_drive(u32 val)
343 {
344 return (val + 1) * 2;
345 }
346
msm_config_group_get(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * config)347 static int msm_config_group_get(struct pinctrl_dev *pctldev,
348 unsigned int group,
349 unsigned long *config)
350 {
351 const struct msm_pingroup *g;
352 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
353 unsigned param = pinconf_to_config_param(*config);
354 unsigned mask;
355 unsigned arg;
356 unsigned bit;
357 int ret;
358 u32 val;
359
360 /* Pin information can only be requested from valid pin groups */
361 if (!gpiochip_line_is_valid(&pctrl->chip, group))
362 return -EINVAL;
363
364 g = &pctrl->soc->groups[group];
365
366 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
367 if (ret < 0)
368 return ret;
369
370 val = msm_readl_ctl(pctrl, g);
371 arg = (val >> bit) & mask;
372
373 /* Convert register value to pinconf value */
374 switch (param) {
375 case PIN_CONFIG_BIAS_DISABLE:
376 if (arg != MSM_NO_PULL)
377 return -EINVAL;
378 arg = 1;
379 break;
380 case PIN_CONFIG_BIAS_PULL_DOWN:
381 if (arg != MSM_PULL_DOWN)
382 return -EINVAL;
383 arg = 1;
384 break;
385 case PIN_CONFIG_BIAS_BUS_HOLD:
386 if (pctrl->soc->pull_no_keeper)
387 return -ENOTSUPP;
388
389 if (arg != MSM_KEEPER)
390 return -EINVAL;
391 arg = 1;
392 break;
393 case PIN_CONFIG_BIAS_PULL_UP:
394 if (pctrl->soc->pull_no_keeper)
395 arg = arg == MSM_PULL_UP_NO_KEEPER;
396 else if (arg & BIT(g->i2c_pull_bit))
397 arg = MSM_I2C_STRONG_PULL_UP;
398 else
399 arg = arg == MSM_PULL_UP;
400 if (!arg)
401 return -EINVAL;
402 break;
403 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
404 /* Pin is not open-drain */
405 if (!arg)
406 return -EINVAL;
407 arg = 1;
408 break;
409 case PIN_CONFIG_DRIVE_STRENGTH:
410 arg = msm_regval_to_drive(arg);
411 break;
412 case PIN_CONFIG_OUTPUT:
413 /* Pin is not output */
414 if (!arg)
415 return -EINVAL;
416
417 val = msm_readl_io(pctrl, g);
418 arg = !!(val & BIT(g->in_bit));
419 break;
420 case PIN_CONFIG_OUTPUT_ENABLE:
421 if (!arg)
422 return -EINVAL;
423 break;
424 default:
425 return -ENOTSUPP;
426 }
427
428 *config = pinconf_to_config_packed(param, arg);
429
430 return 0;
431 }
432
msm_config_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)433 static int msm_config_group_set(struct pinctrl_dev *pctldev,
434 unsigned group,
435 unsigned long *configs,
436 unsigned num_configs)
437 {
438 const struct msm_pingroup *g;
439 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
440 unsigned long flags;
441 unsigned param;
442 unsigned mask;
443 unsigned arg;
444 unsigned bit;
445 int ret;
446 u32 val;
447 int i;
448
449 g = &pctrl->soc->groups[group];
450
451 for (i = 0; i < num_configs; i++) {
452 param = pinconf_to_config_param(configs[i]);
453 arg = pinconf_to_config_argument(configs[i]);
454
455 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
456 if (ret < 0)
457 return ret;
458
459 /* Convert pinconf values to register values */
460 switch (param) {
461 case PIN_CONFIG_BIAS_DISABLE:
462 arg = MSM_NO_PULL;
463 break;
464 case PIN_CONFIG_BIAS_PULL_DOWN:
465 arg = MSM_PULL_DOWN;
466 break;
467 case PIN_CONFIG_BIAS_BUS_HOLD:
468 if (pctrl->soc->pull_no_keeper)
469 return -ENOTSUPP;
470
471 arg = MSM_KEEPER;
472 break;
473 case PIN_CONFIG_BIAS_PULL_UP:
474 if (pctrl->soc->pull_no_keeper)
475 arg = MSM_PULL_UP_NO_KEEPER;
476 else if (g->i2c_pull_bit && arg == MSM_I2C_STRONG_PULL_UP)
477 arg = BIT(g->i2c_pull_bit) | MSM_PULL_UP;
478 else
479 arg = MSM_PULL_UP;
480 break;
481 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
482 arg = 1;
483 break;
484 case PIN_CONFIG_DRIVE_STRENGTH:
485 /* Check for invalid values */
486 if (arg > 16 || arg < 2 || (arg % 2) != 0)
487 arg = -1;
488 else
489 arg = (arg / 2) - 1;
490 break;
491 case PIN_CONFIG_OUTPUT:
492 /* set output value */
493 raw_spin_lock_irqsave(&pctrl->lock, flags);
494 val = msm_readl_io(pctrl, g);
495 if (arg)
496 val |= BIT(g->out_bit);
497 else
498 val &= ~BIT(g->out_bit);
499 msm_writel_io(val, pctrl, g);
500 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
501
502 /* enable output */
503 arg = 1;
504 break;
505 case PIN_CONFIG_INPUT_ENABLE:
506 /*
507 * According to pinctrl documentation this should
508 * actually be a no-op.
509 *
510 * The docs are explicit that "this does not affect
511 * the pin's ability to drive output" but what we do
512 * here is to modify the output enable bit. Thus, to
513 * follow the docs we should remove that.
514 *
515 * The docs say that we should enable any relevant
516 * input buffer, but TLMM there is no input buffer that
517 * can be enabled/disabled. It's always on.
518 *
519 * The points above, explain why this _should_ be a
520 * no-op. However, for historical reasons and to
521 * support old device trees, we'll violate the docs
522 * and still affect the output.
523 *
524 * It should further be noted that this old historical
525 * behavior actually overrides arg to 0. That means
526 * that "input-enable" and "input-disable" in a device
527 * tree would _both_ disable the output. We'll
528 * continue to preserve this behavior as well since
529 * we have no other use for this attribute.
530 */
531 arg = 0;
532 break;
533 case PIN_CONFIG_OUTPUT_ENABLE:
534 arg = !!arg;
535 break;
536 default:
537 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
538 param);
539 return -EINVAL;
540 }
541
542 /* Range-check user-supplied value */
543 if (arg & ~mask) {
544 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
545 return -EINVAL;
546 }
547
548 raw_spin_lock_irqsave(&pctrl->lock, flags);
549 val = msm_readl_ctl(pctrl, g);
550 val &= ~(mask << bit);
551 val |= arg << bit;
552 msm_writel_ctl(val, pctrl, g);
553 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
554 }
555
556 return 0;
557 }
558
559 static const struct pinconf_ops msm_pinconf_ops = {
560 .is_generic = true,
561 .pin_config_group_get = msm_config_group_get,
562 .pin_config_group_set = msm_config_group_set,
563 };
564
msm_gpio_direction_input(struct gpio_chip * chip,unsigned offset)565 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
566 {
567 const struct msm_pingroup *g;
568 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
569 unsigned long flags;
570 u32 val;
571
572 g = &pctrl->soc->groups[offset];
573
574 raw_spin_lock_irqsave(&pctrl->lock, flags);
575
576 val = msm_readl_ctl(pctrl, g);
577 val &= ~BIT(g->oe_bit);
578 msm_writel_ctl(val, pctrl, g);
579
580 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
581
582 return 0;
583 }
584
msm_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)585 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
586 {
587 const struct msm_pingroup *g;
588 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
589 unsigned long flags;
590 u32 val;
591
592 g = &pctrl->soc->groups[offset];
593
594 raw_spin_lock_irqsave(&pctrl->lock, flags);
595
596 val = msm_readl_io(pctrl, g);
597 if (value)
598 val |= BIT(g->out_bit);
599 else
600 val &= ~BIT(g->out_bit);
601 msm_writel_io(val, pctrl, g);
602
603 val = msm_readl_ctl(pctrl, g);
604 val |= BIT(g->oe_bit);
605 msm_writel_ctl(val, pctrl, g);
606
607 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
608
609 return 0;
610 }
611
msm_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)612 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
613 {
614 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
615 const struct msm_pingroup *g;
616 u32 val;
617
618 g = &pctrl->soc->groups[offset];
619
620 val = msm_readl_ctl(pctrl, g);
621
622 return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
623 GPIO_LINE_DIRECTION_IN;
624 }
625
msm_gpio_get(struct gpio_chip * chip,unsigned offset)626 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
627 {
628 const struct msm_pingroup *g;
629 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
630 u32 val;
631
632 g = &pctrl->soc->groups[offset];
633
634 val = msm_readl_io(pctrl, g);
635 return !!(val & BIT(g->in_bit));
636 }
637
msm_gpio_set(struct gpio_chip * chip,unsigned offset,int value)638 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
639 {
640 const struct msm_pingroup *g;
641 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
642 unsigned long flags;
643 u32 val;
644
645 g = &pctrl->soc->groups[offset];
646
647 raw_spin_lock_irqsave(&pctrl->lock, flags);
648
649 val = msm_readl_io(pctrl, g);
650 if (value)
651 val |= BIT(g->out_bit);
652 else
653 val &= ~BIT(g->out_bit);
654 msm_writel_io(val, pctrl, g);
655
656 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
657 }
658
659 #ifdef CONFIG_DEBUG_FS
660
msm_gpio_dbg_show_one(struct seq_file * s,struct pinctrl_dev * pctldev,struct gpio_chip * chip,unsigned offset,unsigned gpio)661 static void msm_gpio_dbg_show_one(struct seq_file *s,
662 struct pinctrl_dev *pctldev,
663 struct gpio_chip *chip,
664 unsigned offset,
665 unsigned gpio)
666 {
667 const struct msm_pingroup *g;
668 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
669 unsigned func;
670 int is_out;
671 int drive;
672 int pull;
673 int val;
674 int egpio_enable;
675 u32 ctl_reg, io_reg;
676
677 static const char * const pulls_keeper[] = {
678 "no pull",
679 "pull down",
680 "keeper",
681 "pull up"
682 };
683
684 static const char * const pulls_no_keeper[] = {
685 "no pull",
686 "pull down",
687 "pull up",
688 };
689
690 if (!gpiochip_line_is_valid(chip, offset))
691 return;
692
693 g = &pctrl->soc->groups[offset];
694 ctl_reg = msm_readl_ctl(pctrl, g);
695 io_reg = msm_readl_io(pctrl, g);
696
697 is_out = !!(ctl_reg & BIT(g->oe_bit));
698 func = (ctl_reg >> g->mux_bit) & 7;
699 drive = (ctl_reg >> g->drv_bit) & 7;
700 pull = (ctl_reg >> g->pull_bit) & 3;
701 egpio_enable = 0;
702 if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
703 egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
704
705 if (is_out)
706 val = !!(io_reg & BIT(g->out_bit));
707 else
708 val = !!(io_reg & BIT(g->in_bit));
709
710 if (egpio_enable) {
711 seq_printf(s, " %-8s: egpio\n", g->grp.name);
712 return;
713 }
714
715 seq_printf(s, " %-8s: %-3s", g->grp.name, is_out ? "out" : "in");
716 seq_printf(s, " %-4s func%d", str_high_low(val), func);
717 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
718 if (pctrl->soc->pull_no_keeper)
719 seq_printf(s, " %s", pulls_no_keeper[pull]);
720 else
721 seq_printf(s, " %s", pulls_keeper[pull]);
722 seq_puts(s, "\n");
723 }
724
msm_gpio_dbg_show(struct seq_file * s,struct gpio_chip * chip)725 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
726 {
727 unsigned gpio = chip->base;
728 unsigned i;
729
730 for (i = 0; i < chip->ngpio; i++, gpio++)
731 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
732 }
733
734 #else
735 #define msm_gpio_dbg_show NULL
736 #endif
737
msm_gpio_init_valid_mask(struct gpio_chip * gc,unsigned long * valid_mask,unsigned int ngpios)738 static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
739 unsigned long *valid_mask,
740 unsigned int ngpios)
741 {
742 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
743 int ret;
744 unsigned int len, i;
745 const int *reserved = pctrl->soc->reserved_gpios;
746 u16 *tmp;
747
748 /* Remove driver-provided reserved GPIOs from valid_mask */
749 if (reserved) {
750 for (i = 0; reserved[i] >= 0; i++) {
751 if (i >= ngpios || reserved[i] >= ngpios) {
752 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
753 return -EINVAL;
754 }
755 clear_bit(reserved[i], valid_mask);
756 }
757
758 return 0;
759 }
760
761 /* The number of GPIOs in the ACPI tables */
762 len = ret = device_property_count_u16(pctrl->dev, "gpios");
763 if (ret < 0)
764 return 0;
765
766 if (ret > ngpios)
767 return -EINVAL;
768
769 tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
770 if (!tmp)
771 return -ENOMEM;
772
773 ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
774 if (ret < 0) {
775 dev_err(pctrl->dev, "could not read list of GPIOs\n");
776 goto out;
777 }
778
779 bitmap_zero(valid_mask, ngpios);
780 for (i = 0; i < len; i++)
781 set_bit(tmp[i], valid_mask);
782
783 out:
784 kfree(tmp);
785 return ret;
786 }
787
788 static const struct gpio_chip msm_gpio_template = {
789 .direction_input = msm_gpio_direction_input,
790 .direction_output = msm_gpio_direction_output,
791 .get_direction = msm_gpio_get_direction,
792 .get = msm_gpio_get,
793 .set = msm_gpio_set,
794 .request = gpiochip_generic_request,
795 .free = gpiochip_generic_free,
796 .dbg_show = msm_gpio_dbg_show,
797 };
798
799 /* For dual-edge interrupts in software, since some hardware has no
800 * such support:
801 *
802 * At appropriate moments, this function may be called to flip the polarity
803 * settings of both-edge irq lines to try and catch the next edge.
804 *
805 * The attempt is considered successful if:
806 * - the status bit goes high, indicating that an edge was caught, or
807 * - the input value of the gpio doesn't change during the attempt.
808 * If the value changes twice during the process, that would cause the first
809 * test to fail but would force the second, as two opposite
810 * transitions would cause a detection no matter the polarity setting.
811 *
812 * The do-loop tries to sledge-hammer closed the timing hole between
813 * the initial value-read and the polarity-write - if the line value changes
814 * during that window, an interrupt is lost, the new polarity setting is
815 * incorrect, and the first success test will fail, causing a retry.
816 *
817 * Algorithm comes from Google's msmgpio driver.
818 */
msm_gpio_update_dual_edge_pos(struct msm_pinctrl * pctrl,const struct msm_pingroup * g,struct irq_data * d)819 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
820 const struct msm_pingroup *g,
821 struct irq_data *d)
822 {
823 int loop_limit = 100;
824 unsigned val, val2, intstat;
825 unsigned pol;
826
827 do {
828 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
829
830 pol = msm_readl_intr_cfg(pctrl, g);
831 pol ^= BIT(g->intr_polarity_bit);
832 msm_writel_intr_cfg(pol, pctrl, g);
833
834 val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
835 intstat = msm_readl_intr_status(pctrl, g);
836 if (intstat || (val == val2))
837 return;
838 } while (loop_limit-- > 0);
839 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
840 val, val2);
841 }
842
msm_gpio_irq_mask(struct irq_data * d)843 static void msm_gpio_irq_mask(struct irq_data *d)
844 {
845 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
846 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
847 const struct msm_pingroup *g;
848 unsigned long flags;
849 u32 val;
850
851 if (d->parent_data)
852 irq_chip_mask_parent(d);
853
854 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
855 return;
856
857 g = &pctrl->soc->groups[d->hwirq];
858
859 raw_spin_lock_irqsave(&pctrl->lock, flags);
860
861 val = msm_readl_intr_cfg(pctrl, g);
862 /*
863 * There are two bits that control interrupt forwarding to the CPU. The
864 * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
865 * latched into the interrupt status register when the hardware detects
866 * an irq that it's configured for (either edge for edge type or level
867 * for level type irq). The 'non-raw' status enable bit causes the
868 * hardware to assert the summary interrupt to the CPU if the latched
869 * status bit is set. There's a bug though, the edge detection logic
870 * seems to have a problem where toggling the RAW_STATUS_EN bit may
871 * cause the status bit to latch spuriously when there isn't any edge
872 * so we can't touch that bit for edge type irqs and we have to keep
873 * the bit set anyway so that edges are latched while the line is masked.
874 *
875 * To make matters more complicated, leaving the RAW_STATUS_EN bit
876 * enabled all the time causes level interrupts to re-latch into the
877 * status register because the level is still present on the line after
878 * we ack it. We clear the raw status enable bit during mask here and
879 * set the bit on unmask so the interrupt can't latch into the hardware
880 * while it's masked.
881 */
882 if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
883 val &= ~BIT(g->intr_raw_status_bit);
884
885 val &= ~BIT(g->intr_enable_bit);
886 msm_writel_intr_cfg(val, pctrl, g);
887
888 clear_bit(d->hwirq, pctrl->enabled_irqs);
889
890 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
891 }
892
msm_gpio_irq_unmask(struct irq_data * d)893 static void msm_gpio_irq_unmask(struct irq_data *d)
894 {
895 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
896 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
897 const struct msm_pingroup *g;
898 unsigned long flags;
899 u32 val;
900
901 if (d->parent_data)
902 irq_chip_unmask_parent(d);
903
904 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
905 return;
906
907 g = &pctrl->soc->groups[d->hwirq];
908
909 raw_spin_lock_irqsave(&pctrl->lock, flags);
910
911 val = msm_readl_intr_cfg(pctrl, g);
912 val |= BIT(g->intr_raw_status_bit);
913 val |= BIT(g->intr_enable_bit);
914 msm_writel_intr_cfg(val, pctrl, g);
915
916 set_bit(d->hwirq, pctrl->enabled_irqs);
917
918 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
919 }
920
msm_gpio_irq_enable(struct irq_data * d)921 static void msm_gpio_irq_enable(struct irq_data *d)
922 {
923 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
924 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
925
926 gpiochip_enable_irq(gc, d->hwirq);
927
928 if (d->parent_data)
929 irq_chip_enable_parent(d);
930
931 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
932 msm_gpio_irq_unmask(d);
933 }
934
msm_gpio_irq_disable(struct irq_data * d)935 static void msm_gpio_irq_disable(struct irq_data *d)
936 {
937 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
938 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
939
940 if (d->parent_data)
941 irq_chip_disable_parent(d);
942
943 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
944 msm_gpio_irq_mask(d);
945
946 gpiochip_disable_irq(gc, d->hwirq);
947 }
948
949 /**
950 * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
951 * @d: The irq dta.
952 *
953 * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
954 * normally handled by the parent irqchip. The logic here is slightly
955 * different due to what's easy to do with our parent, but in principle it's
956 * the same.
957 */
msm_gpio_update_dual_edge_parent(struct irq_data * d)958 static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
959 {
960 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
961 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
962 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
963 int loop_limit = 100;
964 unsigned int val;
965 unsigned int type;
966
967 /* Read the value and make a guess about what edge we need to catch */
968 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
969 type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
970
971 do {
972 /* Set the parent to catch the next edge */
973 irq_chip_set_type_parent(d, type);
974
975 /*
976 * Possibly the line changed between when we last read "val"
977 * (and decided what edge we needed) and when set the edge.
978 * If the value didn't change (or changed and then changed
979 * back) then we're done.
980 */
981 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
982 if (type == IRQ_TYPE_EDGE_RISING) {
983 if (!val)
984 return;
985 type = IRQ_TYPE_EDGE_FALLING;
986 } else if (type == IRQ_TYPE_EDGE_FALLING) {
987 if (val)
988 return;
989 type = IRQ_TYPE_EDGE_RISING;
990 }
991 } while (loop_limit-- > 0);
992 dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
993 }
994
msm_gpio_irq_ack(struct irq_data * d)995 static void msm_gpio_irq_ack(struct irq_data *d)
996 {
997 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
998 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
999 const struct msm_pingroup *g;
1000 unsigned long flags;
1001
1002 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1003 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1004 msm_gpio_update_dual_edge_parent(d);
1005 return;
1006 }
1007
1008 g = &pctrl->soc->groups[d->hwirq];
1009
1010 raw_spin_lock_irqsave(&pctrl->lock, flags);
1011
1012 msm_ack_intr_status(pctrl, g);
1013
1014 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1015 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1016
1017 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1018 }
1019
msm_gpio_irq_eoi(struct irq_data * d)1020 static void msm_gpio_irq_eoi(struct irq_data *d)
1021 {
1022 d = d->parent_data;
1023
1024 if (d)
1025 d->chip->irq_eoi(d);
1026 }
1027
msm_gpio_needs_dual_edge_parent_workaround(struct irq_data * d,unsigned int type)1028 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
1029 unsigned int type)
1030 {
1031 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1032 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1033
1034 return type == IRQ_TYPE_EDGE_BOTH &&
1035 pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
1036 test_bit(d->hwirq, pctrl->skip_wake_irqs);
1037 }
1038
msm_gpio_irq_set_type(struct irq_data * d,unsigned int type)1039 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
1040 {
1041 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1042 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1043 const struct msm_pingroup *g;
1044 u32 intr_target_mask = GENMASK(2, 0);
1045 unsigned long flags;
1046 u32 val, oldval;
1047
1048 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
1049 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1050 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
1051 msm_gpio_update_dual_edge_parent(d);
1052 return 0;
1053 }
1054
1055 if (d->parent_data)
1056 irq_chip_set_type_parent(d, type);
1057
1058 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1059 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1060 irq_set_handler_locked(d, handle_fasteoi_irq);
1061 return 0;
1062 }
1063
1064 g = &pctrl->soc->groups[d->hwirq];
1065
1066 raw_spin_lock_irqsave(&pctrl->lock, flags);
1067
1068 /*
1069 * For hw without possibility of detecting both edges
1070 */
1071 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
1072 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1073 else
1074 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1075
1076 /* Route interrupts to application cpu.
1077 * With intr_target_use_scm interrupts are routed to
1078 * application cpu using scm calls.
1079 */
1080 if (g->intr_target_width)
1081 intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
1082
1083 if (pctrl->intr_target_use_scm) {
1084 u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
1085 int ret;
1086
1087 qcom_scm_io_readl(addr, &val);
1088 val &= ~(intr_target_mask << g->intr_target_bit);
1089 val |= g->intr_target_kpss_val << g->intr_target_bit;
1090
1091 ret = qcom_scm_io_writel(addr, val);
1092 if (ret)
1093 dev_err(pctrl->dev,
1094 "Failed routing %lu interrupt to Apps proc",
1095 d->hwirq);
1096 } else {
1097 val = msm_readl_intr_target(pctrl, g);
1098 val &= ~(intr_target_mask << g->intr_target_bit);
1099 val |= g->intr_target_kpss_val << g->intr_target_bit;
1100 msm_writel_intr_target(val, pctrl, g);
1101 }
1102
1103 /* Update configuration for gpio.
1104 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
1105 * internal circuitry of TLMM, toggling the RAW_STATUS
1106 * could cause the INTR_STATUS to be set for EDGE interrupts.
1107 */
1108 val = oldval = msm_readl_intr_cfg(pctrl, g);
1109 val |= BIT(g->intr_raw_status_bit);
1110 if (g->intr_detection_width == 2) {
1111 val &= ~(3 << g->intr_detection_bit);
1112 val &= ~(1 << g->intr_polarity_bit);
1113 switch (type) {
1114 case IRQ_TYPE_EDGE_RISING:
1115 val |= 1 << g->intr_detection_bit;
1116 val |= BIT(g->intr_polarity_bit);
1117 break;
1118 case IRQ_TYPE_EDGE_FALLING:
1119 val |= 2 << g->intr_detection_bit;
1120 val |= BIT(g->intr_polarity_bit);
1121 break;
1122 case IRQ_TYPE_EDGE_BOTH:
1123 val |= 3 << g->intr_detection_bit;
1124 val |= BIT(g->intr_polarity_bit);
1125 break;
1126 case IRQ_TYPE_LEVEL_LOW:
1127 break;
1128 case IRQ_TYPE_LEVEL_HIGH:
1129 val |= BIT(g->intr_polarity_bit);
1130 break;
1131 }
1132 } else if (g->intr_detection_width == 1) {
1133 val &= ~(1 << g->intr_detection_bit);
1134 val &= ~(1 << g->intr_polarity_bit);
1135 switch (type) {
1136 case IRQ_TYPE_EDGE_RISING:
1137 val |= BIT(g->intr_detection_bit);
1138 val |= BIT(g->intr_polarity_bit);
1139 break;
1140 case IRQ_TYPE_EDGE_FALLING:
1141 val |= BIT(g->intr_detection_bit);
1142 break;
1143 case IRQ_TYPE_EDGE_BOTH:
1144 val |= BIT(g->intr_detection_bit);
1145 val |= BIT(g->intr_polarity_bit);
1146 break;
1147 case IRQ_TYPE_LEVEL_LOW:
1148 break;
1149 case IRQ_TYPE_LEVEL_HIGH:
1150 val |= BIT(g->intr_polarity_bit);
1151 break;
1152 }
1153 } else {
1154 BUG();
1155 }
1156 msm_writel_intr_cfg(val, pctrl, g);
1157
1158 /*
1159 * The first time we set RAW_STATUS_EN it could trigger an interrupt.
1160 * Clear the interrupt. This is safe because we have
1161 * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could
1162 * also still have a non-matching interrupt latched, so clear whenever
1163 * making changes to the interrupt configuration.
1164 */
1165 if (val != oldval)
1166 msm_ack_intr_status(pctrl, g);
1167
1168 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1169 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1170
1171 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1172
1173 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1174 irq_set_handler_locked(d, handle_level_irq);
1175 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1176 irq_set_handler_locked(d, handle_edge_irq);
1177
1178 return 0;
1179 }
1180
msm_gpio_irq_set_wake(struct irq_data * d,unsigned int on)1181 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1182 {
1183 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1184 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1185
1186 /*
1187 * While they may not wake up when the TLMM is powered off,
1188 * some GPIOs would like to wakeup the system from suspend
1189 * when TLMM is powered on. To allow that, enable the GPIO
1190 * summary line to be wakeup capable at GIC.
1191 */
1192 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1193 return irq_chip_set_wake_parent(d, on);
1194
1195 return irq_set_irq_wake(pctrl->irq, on);
1196 }
1197
msm_gpio_irq_reqres(struct irq_data * d)1198 static int msm_gpio_irq_reqres(struct irq_data *d)
1199 {
1200 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1201 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1202 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
1203 unsigned long flags;
1204 int ret;
1205
1206 if (!try_module_get(gc->owner))
1207 return -ENODEV;
1208
1209 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1210 if (ret)
1211 goto out;
1212 msm_gpio_direction_input(gc, d->hwirq);
1213
1214 if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1215 dev_err(gc->parent,
1216 "unable to lock HW IRQ %lu for IRQ\n",
1217 d->hwirq);
1218 ret = -EINVAL;
1219 goto out;
1220 }
1221
1222 /*
1223 * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
1224 * only works if disable is not lazy since we only clear any bogus
1225 * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
1226 */
1227 irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
1228
1229 /*
1230 * If the wakeup_enable bit is present and marked as available for the
1231 * requested GPIO, it should be enabled when the GPIO is marked as
1232 * wake irq in order to allow the interrupt event to be transfered to
1233 * the PDC HW.
1234 * While the name implies only the wakeup event, it's also required for
1235 * the interrupt event.
1236 */
1237 if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) {
1238 u32 intr_cfg;
1239
1240 raw_spin_lock_irqsave(&pctrl->lock, flags);
1241
1242 intr_cfg = msm_readl_intr_cfg(pctrl, g);
1243 if (intr_cfg & BIT(g->intr_wakeup_present_bit)) {
1244 intr_cfg |= BIT(g->intr_wakeup_enable_bit);
1245 msm_writel_intr_cfg(intr_cfg, pctrl, g);
1246 }
1247
1248 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1249 }
1250
1251 return 0;
1252 out:
1253 module_put(gc->owner);
1254 return ret;
1255 }
1256
msm_gpio_irq_relres(struct irq_data * d)1257 static void msm_gpio_irq_relres(struct irq_data *d)
1258 {
1259 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1260 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1261 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
1262 unsigned long flags;
1263
1264 /* Disable the wakeup_enable bit if it has been set in msm_gpio_irq_reqres() */
1265 if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) {
1266 u32 intr_cfg;
1267
1268 raw_spin_lock_irqsave(&pctrl->lock, flags);
1269
1270 intr_cfg = msm_readl_intr_cfg(pctrl, g);
1271 if (intr_cfg & BIT(g->intr_wakeup_present_bit)) {
1272 intr_cfg &= ~BIT(g->intr_wakeup_enable_bit);
1273 msm_writel_intr_cfg(intr_cfg, pctrl, g);
1274 }
1275
1276 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1277 }
1278
1279 gpiochip_unlock_as_irq(gc, d->hwirq);
1280 module_put(gc->owner);
1281 }
1282
msm_gpio_irq_set_affinity(struct irq_data * d,const struct cpumask * dest,bool force)1283 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1284 const struct cpumask *dest, bool force)
1285 {
1286 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1287 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1288
1289 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1290 return irq_chip_set_affinity_parent(d, dest, force);
1291
1292 return -EINVAL;
1293 }
1294
msm_gpio_irq_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)1295 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1296 {
1297 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1298 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1299
1300 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1301 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1302
1303 return -EINVAL;
1304 }
1305
msm_gpio_irq_handler(struct irq_desc * desc)1306 static void msm_gpio_irq_handler(struct irq_desc *desc)
1307 {
1308 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1309 const struct msm_pingroup *g;
1310 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1311 struct irq_chip *chip = irq_desc_get_chip(desc);
1312 int handled = 0;
1313 u32 val;
1314 int i;
1315
1316 chained_irq_enter(chip, desc);
1317
1318 /*
1319 * Each pin has it's own IRQ status register, so use
1320 * enabled_irq bitmap to limit the number of reads.
1321 */
1322 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1323 g = &pctrl->soc->groups[i];
1324 val = msm_readl_intr_status(pctrl, g);
1325 if (val & BIT(g->intr_status_bit)) {
1326 generic_handle_domain_irq(gc->irq.domain, i);
1327 handled++;
1328 }
1329 }
1330
1331 /* No interrupts were flagged */
1332 if (handled == 0)
1333 handle_bad_irq(desc);
1334
1335 chained_irq_exit(chip, desc);
1336 }
1337
msm_gpio_wakeirq(struct gpio_chip * gc,unsigned int child,unsigned int child_type,unsigned int * parent,unsigned int * parent_type)1338 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1339 unsigned int child,
1340 unsigned int child_type,
1341 unsigned int *parent,
1342 unsigned int *parent_type)
1343 {
1344 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1345 const struct msm_gpio_wakeirq_map *map;
1346 int i;
1347
1348 *parent = GPIO_NO_WAKE_IRQ;
1349 *parent_type = IRQ_TYPE_EDGE_RISING;
1350
1351 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1352 map = &pctrl->soc->wakeirq_map[i];
1353 if (map->gpio == child) {
1354 *parent = map->wakeirq;
1355 break;
1356 }
1357 }
1358
1359 return 0;
1360 }
1361
msm_gpio_needs_valid_mask(struct msm_pinctrl * pctrl)1362 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1363 {
1364 if (pctrl->soc->reserved_gpios)
1365 return true;
1366
1367 return device_property_count_u16(pctrl->dev, "gpios") > 0;
1368 }
1369
1370 static const struct irq_chip msm_gpio_irq_chip = {
1371 .name = "msmgpio",
1372 .irq_enable = msm_gpio_irq_enable,
1373 .irq_disable = msm_gpio_irq_disable,
1374 .irq_mask = msm_gpio_irq_mask,
1375 .irq_unmask = msm_gpio_irq_unmask,
1376 .irq_ack = msm_gpio_irq_ack,
1377 .irq_eoi = msm_gpio_irq_eoi,
1378 .irq_set_type = msm_gpio_irq_set_type,
1379 .irq_set_wake = msm_gpio_irq_set_wake,
1380 .irq_request_resources = msm_gpio_irq_reqres,
1381 .irq_release_resources = msm_gpio_irq_relres,
1382 .irq_set_affinity = msm_gpio_irq_set_affinity,
1383 .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
1384 .flags = (IRQCHIP_MASK_ON_SUSPEND |
1385 IRQCHIP_SET_TYPE_MASKED |
1386 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
1387 IRQCHIP_IMMUTABLE),
1388 };
1389
msm_gpio_init(struct msm_pinctrl * pctrl)1390 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1391 {
1392 struct gpio_chip *chip;
1393 struct gpio_irq_chip *girq;
1394 int i, ret;
1395 unsigned gpio, ngpio = pctrl->soc->ngpios;
1396 struct device_node *np;
1397 bool skip;
1398
1399 if (WARN_ON(ngpio > MAX_NR_GPIO))
1400 return -EINVAL;
1401
1402 chip = &pctrl->chip;
1403 chip->base = -1;
1404 chip->ngpio = ngpio;
1405 chip->label = dev_name(pctrl->dev);
1406 chip->parent = pctrl->dev;
1407 chip->owner = THIS_MODULE;
1408 if (msm_gpio_needs_valid_mask(pctrl))
1409 chip->init_valid_mask = msm_gpio_init_valid_mask;
1410
1411 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1412 if (np) {
1413 chip->irq.parent_domain = irq_find_matching_host(np,
1414 DOMAIN_BUS_WAKEUP);
1415 of_node_put(np);
1416 if (!chip->irq.parent_domain)
1417 return -EPROBE_DEFER;
1418 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1419 /*
1420 * Let's skip handling the GPIOs, if the parent irqchip
1421 * is handling the direct connect IRQ of the GPIO.
1422 */
1423 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1424 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1425 gpio = pctrl->soc->wakeirq_map[i].gpio;
1426 set_bit(gpio, pctrl->skip_wake_irqs);
1427 }
1428 }
1429
1430 girq = &chip->irq;
1431 gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
1432 girq->parent_handler = msm_gpio_irq_handler;
1433 girq->fwnode = dev_fwnode(pctrl->dev);
1434 girq->num_parents = 1;
1435 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1436 GFP_KERNEL);
1437 if (!girq->parents)
1438 return -ENOMEM;
1439 girq->default_type = IRQ_TYPE_NONE;
1440 girq->handler = handle_bad_irq;
1441 girq->parents[0] = pctrl->irq;
1442
1443 ret = gpiochip_add_data(&pctrl->chip, pctrl);
1444 if (ret) {
1445 dev_err(pctrl->dev, "Failed register gpiochip\n");
1446 return ret;
1447 }
1448
1449 /*
1450 * For DeviceTree-supported systems, the gpio core checks the
1451 * pinctrl's device node for the "gpio-ranges" property.
1452 * If it is present, it takes care of adding the pin ranges
1453 * for the driver. In this case the driver can skip ahead.
1454 *
1455 * In order to remain compatible with older, existing DeviceTree
1456 * files which don't set the "gpio-ranges" property or systems that
1457 * utilize ACPI the driver has to call gpiochip_add_pin_range().
1458 */
1459 if (!of_property_present(pctrl->dev->of_node, "gpio-ranges")) {
1460 ret = gpiochip_add_pin_range(&pctrl->chip,
1461 dev_name(pctrl->dev), 0, 0, chip->ngpio);
1462 if (ret) {
1463 dev_err(pctrl->dev, "Failed to add pin range\n");
1464 gpiochip_remove(&pctrl->chip);
1465 return ret;
1466 }
1467 }
1468
1469 return 0;
1470 }
1471
msm_ps_hold_restart(struct sys_off_data * data)1472 static int msm_ps_hold_restart(struct sys_off_data *data)
1473 {
1474 struct msm_pinctrl *pctrl = data->cb_data;
1475
1476 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1477 mdelay(1000);
1478 return NOTIFY_DONE;
1479 }
1480
1481 static struct msm_pinctrl *poweroff_pctrl;
1482
msm_ps_hold_poweroff(void)1483 static void msm_ps_hold_poweroff(void)
1484 {
1485 struct sys_off_data data = {
1486 .cb_data = poweroff_pctrl,
1487 };
1488
1489 msm_ps_hold_restart(&data);
1490 }
1491
msm_pinctrl_setup_pm_reset(struct msm_pinctrl * pctrl)1492 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1493 {
1494 int i;
1495 const struct pinfunction *func = pctrl->soc->functions;
1496
1497 for (i = 0; i < pctrl->soc->nfunctions; i++)
1498 if (!strcmp(func[i].name, "ps_hold")) {
1499 if (devm_register_sys_off_handler(pctrl->dev,
1500 SYS_OFF_MODE_RESTART,
1501 128,
1502 msm_ps_hold_restart,
1503 pctrl))
1504 dev_err(pctrl->dev,
1505 "failed to setup restart handler.\n");
1506 poweroff_pctrl = pctrl;
1507 pm_power_off = msm_ps_hold_poweroff;
1508 break;
1509 }
1510 }
1511
msm_pinctrl_suspend(struct device * dev)1512 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1513 {
1514 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1515
1516 return pinctrl_force_sleep(pctrl->pctrl);
1517 }
1518
msm_pinctrl_resume(struct device * dev)1519 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1520 {
1521 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1522
1523 return pinctrl_force_default(pctrl->pctrl);
1524 }
1525
1526 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1527 msm_pinctrl_resume);
1528
1529 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1530
msm_pinctrl_probe(struct platform_device * pdev,const struct msm_pinctrl_soc_data * soc_data)1531 int msm_pinctrl_probe(struct platform_device *pdev,
1532 const struct msm_pinctrl_soc_data *soc_data)
1533 {
1534 struct msm_pinctrl *pctrl;
1535 struct resource *res;
1536 int ret;
1537 int i;
1538
1539 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1540 if (!pctrl)
1541 return -ENOMEM;
1542
1543 pctrl->dev = &pdev->dev;
1544 pctrl->soc = soc_data;
1545 pctrl->chip = msm_gpio_template;
1546 pctrl->intr_target_use_scm = of_device_is_compatible(
1547 pctrl->dev->of_node,
1548 "qcom,ipq8064-pinctrl");
1549
1550 raw_spin_lock_init(&pctrl->lock);
1551
1552 if (soc_data->tiles) {
1553 for (i = 0; i < soc_data->ntiles; i++) {
1554 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1555 soc_data->tiles[i]);
1556 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1557 if (IS_ERR(pctrl->regs[i]))
1558 return PTR_ERR(pctrl->regs[i]);
1559 }
1560 } else {
1561 pctrl->regs[0] = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1562 if (IS_ERR(pctrl->regs[0]))
1563 return PTR_ERR(pctrl->regs[0]);
1564
1565 pctrl->phys_base[0] = res->start;
1566 }
1567
1568 msm_pinctrl_setup_pm_reset(pctrl);
1569
1570 pctrl->irq = platform_get_irq(pdev, 0);
1571 if (pctrl->irq < 0)
1572 return pctrl->irq;
1573
1574 pctrl->desc.owner = THIS_MODULE;
1575 pctrl->desc.pctlops = &msm_pinctrl_ops;
1576 pctrl->desc.pmxops = &msm_pinmux_ops;
1577 pctrl->desc.confops = &msm_pinconf_ops;
1578 pctrl->desc.name = dev_name(&pdev->dev);
1579 pctrl->desc.pins = pctrl->soc->pins;
1580 pctrl->desc.npins = pctrl->soc->npins;
1581
1582 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1583 if (IS_ERR(pctrl->pctrl)) {
1584 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1585 return PTR_ERR(pctrl->pctrl);
1586 }
1587
1588 ret = msm_gpio_init(pctrl);
1589 if (ret)
1590 return ret;
1591
1592 platform_set_drvdata(pdev, pctrl);
1593
1594 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1595
1596 return 0;
1597 }
1598 EXPORT_SYMBOL(msm_pinctrl_probe);
1599
msm_pinctrl_remove(struct platform_device * pdev)1600 void msm_pinctrl_remove(struct platform_device *pdev)
1601 {
1602 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1603
1604 gpiochip_remove(&pctrl->chip);
1605 }
1606 EXPORT_SYMBOL(msm_pinctrl_remove);
1607
1608 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1609 MODULE_LICENSE("GPL v2");
1610