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